dmabuf.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. /*
  2. * sound/oss/dmabuf.c
  3. *
  4. * The DMA buffer manager for digitized voice applications
  5. */
  6. /*
  7. * Copyright (C) by Hannu Savolainen 1993-1997
  8. *
  9. * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  10. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  11. * for more info.
  12. *
  13. * Thomas Sailer : moved several static variables into struct audio_operations
  14. * (which is grossly misnamed btw.) because they have the same
  15. * lifetime as the rest in there and dynamic allocation saves
  16. * 12k or so
  17. * Thomas Sailer : remove {in,out}_sleep_flag. It was used for the sleeper to
  18. * determine if it was woken up by the expiring timeout or by
  19. * an explicit wake_up. The return value from schedule_timeout
  20. * can be used instead; if 0, the wakeup was due to the timeout.
  21. *
  22. * Rob Riggs Added persistent DMA buffers (1998/10/17)
  23. */
  24. #define BE_CONSERVATIVE
  25. #define SAMPLE_ROUNDUP 0
  26. #include <linux/mm.h>
  27. #include <linux/gfp.h>
  28. #include "sound_config.h"
  29. #include "sleep.h"
  30. #define DMAP_FREE_ON_CLOSE 0
  31. #define DMAP_KEEP_ON_CLOSE 1
  32. extern int sound_dmap_flag;
  33. static void dma_reset_output(int dev);
  34. static void dma_reset_input(int dev);
  35. static int local_start_dma(struct audio_operations *adev, unsigned long physaddr, int count, int dma_mode);
  36. static int debugmem; /* switched off by default */
  37. static int dma_buffsize = DSP_BUFFSIZE;
  38. static long dmabuf_timeout(struct dma_buffparms *dmap)
  39. {
  40. long tmout;
  41. tmout = (dmap->fragment_size * HZ) / dmap->data_rate;
  42. tmout += HZ / 5; /* Some safety distance */
  43. if (tmout < (HZ / 2))
  44. tmout = HZ / 2;
  45. if (tmout > 20 * HZ)
  46. tmout = 20 * HZ;
  47. return tmout;
  48. }
  49. static int sound_alloc_dmap(struct dma_buffparms *dmap)
  50. {
  51. char *start_addr, *end_addr;
  52. int dma_pagesize;
  53. int sz, size;
  54. struct page *page;
  55. dmap->mapping_flags &= ~DMA_MAP_MAPPED;
  56. if (dmap->raw_buf != NULL)
  57. return 0; /* Already done */
  58. if (dma_buffsize < 4096)
  59. dma_buffsize = 4096;
  60. dma_pagesize = (dmap->dma < 4) ? (64 * 1024) : (128 * 1024);
  61. /*
  62. * Now check for the Cyrix problem.
  63. */
  64. if(isa_dma_bridge_buggy==2)
  65. dma_pagesize=32768;
  66. dmap->raw_buf = NULL;
  67. dmap->buffsize = dma_buffsize;
  68. if (dmap->buffsize > dma_pagesize)
  69. dmap->buffsize = dma_pagesize;
  70. start_addr = NULL;
  71. /*
  72. * Now loop until we get a free buffer. Try to get smaller buffer if
  73. * it fails. Don't accept smaller than 8k buffer for performance
  74. * reasons.
  75. */
  76. while (start_addr == NULL && dmap->buffsize > PAGE_SIZE) {
  77. for (sz = 0, size = PAGE_SIZE; size < dmap->buffsize; sz++, size <<= 1);
  78. dmap->buffsize = PAGE_SIZE * (1 << sz);
  79. start_addr = (char *) __get_free_pages(GFP_ATOMIC|GFP_DMA|__GFP_NOWARN, sz);
  80. if (start_addr == NULL)
  81. dmap->buffsize /= 2;
  82. }
  83. if (start_addr == NULL) {
  84. printk(KERN_WARNING "Sound error: Couldn't allocate DMA buffer\n");
  85. return -ENOMEM;
  86. } else {
  87. /* make some checks */
  88. end_addr = start_addr + dmap->buffsize - 1;
  89. if (debugmem)
  90. printk(KERN_DEBUG "sound: start 0x%lx, end 0x%lx\n", (long) start_addr, (long) end_addr);
  91. /* now check if it fits into the same dma-pagesize */
  92. if (((long) start_addr & ~(dma_pagesize - 1)) != ((long) end_addr & ~(dma_pagesize - 1))
  93. || end_addr >= (char *) (MAX_DMA_ADDRESS)) {
  94. printk(KERN_ERR "sound: Got invalid address 0x%lx for %db DMA-buffer\n", (long) start_addr, dmap->buffsize);
  95. return -EFAULT;
  96. }
  97. }
  98. dmap->raw_buf = start_addr;
  99. dmap->raw_buf_phys = dma_map_single(NULL, start_addr, dmap->buffsize, DMA_BIDIRECTIONAL);
  100. for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
  101. SetPageReserved(page);
  102. return 0;
  103. }
  104. static void sound_free_dmap(struct dma_buffparms *dmap)
  105. {
  106. int sz, size;
  107. struct page *page;
  108. unsigned long start_addr, end_addr;
  109. if (dmap->raw_buf == NULL)
  110. return;
  111. if (dmap->mapping_flags & DMA_MAP_MAPPED)
  112. return; /* Don't free mmapped buffer. Will use it next time */
  113. for (sz = 0, size = PAGE_SIZE; size < dmap->buffsize; sz++, size <<= 1);
  114. start_addr = (unsigned long) dmap->raw_buf;
  115. end_addr = start_addr + dmap->buffsize;
  116. for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
  117. ClearPageReserved(page);
  118. dma_unmap_single(NULL, dmap->raw_buf_phys, dmap->buffsize, DMA_BIDIRECTIONAL);
  119. free_pages((unsigned long) dmap->raw_buf, sz);
  120. dmap->raw_buf = NULL;
  121. }
  122. /* Intel version !!!!!!!!! */
  123. static int sound_start_dma(struct dma_buffparms *dmap, unsigned long physaddr, int count, int dma_mode)
  124. {
  125. unsigned long flags;
  126. int chan = dmap->dma;
  127. /* printk( "Start DMA%d %d, %d\n", chan, (int)(physaddr-dmap->raw_buf_phys), count); */
  128. flags = claim_dma_lock();
  129. disable_dma(chan);
  130. clear_dma_ff(chan);
  131. set_dma_mode(chan, dma_mode);
  132. set_dma_addr(chan, physaddr);
  133. set_dma_count(chan, count);
  134. enable_dma(chan);
  135. release_dma_lock(flags);
  136. return 0;
  137. }
  138. static void dma_init_buffers(struct dma_buffparms *dmap)
  139. {
  140. dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
  141. dmap->byte_counter = 0;
  142. dmap->max_byte_counter = 8000 * 60 * 60;
  143. dmap->bytes_in_use = dmap->buffsize;
  144. dmap->dma_mode = DMODE_NONE;
  145. dmap->mapping_flags = 0;
  146. dmap->neutral_byte = 0x80;
  147. dmap->data_rate = 8000;
  148. dmap->cfrag = -1;
  149. dmap->closing = 0;
  150. dmap->nbufs = 1;
  151. dmap->flags = DMA_BUSY; /* Other flags off */
  152. }
  153. static int open_dmap(struct audio_operations *adev, int mode, struct dma_buffparms *dmap)
  154. {
  155. int err;
  156. if (dmap->flags & DMA_BUSY)
  157. return -EBUSY;
  158. if ((err = sound_alloc_dmap(dmap)) < 0)
  159. return err;
  160. if (dmap->raw_buf == NULL) {
  161. printk(KERN_WARNING "Sound: DMA buffers not available\n");
  162. return -ENOSPC; /* Memory allocation failed during boot */
  163. }
  164. if (dmap->dma >= 0 && sound_open_dma(dmap->dma, adev->name)) {
  165. printk(KERN_WARNING "Unable to grab(2) DMA%d for the audio driver\n", dmap->dma);
  166. return -EBUSY;
  167. }
  168. dma_init_buffers(dmap);
  169. spin_lock_init(&dmap->lock);
  170. dmap->open_mode = mode;
  171. dmap->subdivision = dmap->underrun_count = 0;
  172. dmap->fragment_size = 0;
  173. dmap->max_fragments = 65536; /* Just a large value */
  174. dmap->byte_counter = 0;
  175. dmap->max_byte_counter = 8000 * 60 * 60;
  176. dmap->applic_profile = APF_NORMAL;
  177. dmap->needs_reorg = 1;
  178. dmap->audio_callback = NULL;
  179. dmap->callback_parm = 0;
  180. return 0;
  181. }
  182. static void close_dmap(struct audio_operations *adev, struct dma_buffparms *dmap)
  183. {
  184. unsigned long flags;
  185. if (dmap->dma >= 0) {
  186. sound_close_dma(dmap->dma);
  187. flags=claim_dma_lock();
  188. disable_dma(dmap->dma);
  189. release_dma_lock(flags);
  190. }
  191. if (dmap->flags & DMA_BUSY)
  192. dmap->dma_mode = DMODE_NONE;
  193. dmap->flags &= ~DMA_BUSY;
  194. if (sound_dmap_flag == DMAP_FREE_ON_CLOSE)
  195. sound_free_dmap(dmap);
  196. }
  197. static unsigned int default_set_bits(int dev, unsigned int bits)
  198. {
  199. mm_segment_t fs = get_fs();
  200. set_fs(get_ds());
  201. audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SETFMT, (void __user *)&bits);
  202. set_fs(fs);
  203. return bits;
  204. }
  205. static int default_set_speed(int dev, int speed)
  206. {
  207. mm_segment_t fs = get_fs();
  208. set_fs(get_ds());
  209. audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SPEED, (void __user *)&speed);
  210. set_fs(fs);
  211. return speed;
  212. }
  213. static short default_set_channels(int dev, short channels)
  214. {
  215. int c = channels;
  216. mm_segment_t fs = get_fs();
  217. set_fs(get_ds());
  218. audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_CHANNELS, (void __user *)&c);
  219. set_fs(fs);
  220. return c;
  221. }
  222. static void check_driver(struct audio_driver *d)
  223. {
  224. if (d->set_speed == NULL)
  225. d->set_speed = default_set_speed;
  226. if (d->set_bits == NULL)
  227. d->set_bits = default_set_bits;
  228. if (d->set_channels == NULL)
  229. d->set_channels = default_set_channels;
  230. }
  231. int DMAbuf_open(int dev, int mode)
  232. {
  233. struct audio_operations *adev = audio_devs[dev];
  234. int retval;
  235. struct dma_buffparms *dmap_in = NULL;
  236. struct dma_buffparms *dmap_out = NULL;
  237. if (!adev)
  238. return -ENXIO;
  239. if (!(adev->flags & DMA_DUPLEX))
  240. adev->dmap_in = adev->dmap_out;
  241. check_driver(adev->d);
  242. if ((retval = adev->d->open(dev, mode)) < 0)
  243. return retval;
  244. dmap_out = adev->dmap_out;
  245. dmap_in = adev->dmap_in;
  246. if (dmap_in == dmap_out)
  247. adev->flags &= ~DMA_DUPLEX;
  248. if (mode & OPEN_WRITE) {
  249. if ((retval = open_dmap(adev, mode, dmap_out)) < 0) {
  250. adev->d->close(dev);
  251. return retval;
  252. }
  253. }
  254. adev->enable_bits = mode;
  255. if (mode == OPEN_READ || (mode != OPEN_WRITE && (adev->flags & DMA_DUPLEX))) {
  256. if ((retval = open_dmap(adev, mode, dmap_in)) < 0) {
  257. adev->d->close(dev);
  258. if (mode & OPEN_WRITE)
  259. close_dmap(adev, dmap_out);
  260. return retval;
  261. }
  262. }
  263. adev->open_mode = mode;
  264. adev->go = 1;
  265. adev->d->set_bits(dev, 8);
  266. adev->d->set_channels(dev, 1);
  267. adev->d->set_speed(dev, DSP_DEFAULT_SPEED);
  268. if (adev->dmap_out->dma_mode == DMODE_OUTPUT)
  269. memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
  270. adev->dmap_out->bytes_in_use);
  271. return 0;
  272. }
  273. /* MUST not hold the spinlock */
  274. void DMAbuf_reset(int dev)
  275. {
  276. if (audio_devs[dev]->open_mode & OPEN_WRITE)
  277. dma_reset_output(dev);
  278. if (audio_devs[dev]->open_mode & OPEN_READ)
  279. dma_reset_input(dev);
  280. }
  281. static void dma_reset_output(int dev)
  282. {
  283. struct audio_operations *adev = audio_devs[dev];
  284. unsigned long flags,f ;
  285. struct dma_buffparms *dmap = adev->dmap_out;
  286. if (!(dmap->flags & DMA_STARTED)) /* DMA is not active */
  287. return;
  288. /*
  289. * First wait until the current fragment has been played completely
  290. */
  291. spin_lock_irqsave(&dmap->lock,flags);
  292. adev->dmap_out->flags |= DMA_SYNCING;
  293. adev->dmap_out->underrun_count = 0;
  294. if (!signal_pending(current) && adev->dmap_out->qlen &&
  295. adev->dmap_out->underrun_count == 0){
  296. spin_unlock_irqrestore(&dmap->lock,flags);
  297. oss_broken_sleep_on(&adev->out_sleeper, dmabuf_timeout(dmap));
  298. spin_lock_irqsave(&dmap->lock,flags);
  299. }
  300. adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
  301. /*
  302. * Finally shut the device off
  303. */
  304. if (!(adev->flags & DMA_DUPLEX) || !adev->d->halt_output)
  305. adev->d->halt_io(dev);
  306. else
  307. adev->d->halt_output(dev);
  308. adev->dmap_out->flags &= ~DMA_STARTED;
  309. f=claim_dma_lock();
  310. clear_dma_ff(dmap->dma);
  311. disable_dma(dmap->dma);
  312. release_dma_lock(f);
  313. dmap->byte_counter = 0;
  314. reorganize_buffers(dev, adev->dmap_out, 0);
  315. dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
  316. spin_unlock_irqrestore(&dmap->lock,flags);
  317. }
  318. static void dma_reset_input(int dev)
  319. {
  320. struct audio_operations *adev = audio_devs[dev];
  321. unsigned long flags;
  322. struct dma_buffparms *dmap = adev->dmap_in;
  323. spin_lock_irqsave(&dmap->lock,flags);
  324. if (!(adev->flags & DMA_DUPLEX) || !adev->d->halt_input)
  325. adev->d->halt_io(dev);
  326. else
  327. adev->d->halt_input(dev);
  328. adev->dmap_in->flags &= ~DMA_STARTED;
  329. dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
  330. dmap->byte_counter = 0;
  331. reorganize_buffers(dev, adev->dmap_in, 1);
  332. spin_unlock_irqrestore(&dmap->lock,flags);
  333. }
  334. /* MUST be called with holding the dmap->lock */
  335. void DMAbuf_launch_output(int dev, struct dma_buffparms *dmap)
  336. {
  337. struct audio_operations *adev = audio_devs[dev];
  338. if (!((adev->enable_bits * adev->go) & PCM_ENABLE_OUTPUT))
  339. return; /* Don't start DMA yet */
  340. dmap->dma_mode = DMODE_OUTPUT;
  341. if (!(dmap->flags & DMA_ACTIVE) || !(adev->flags & DMA_AUTOMODE) || (dmap->flags & DMA_NODMA)) {
  342. if (!(dmap->flags & DMA_STARTED)) {
  343. reorganize_buffers(dev, dmap, 0);
  344. if (adev->d->prepare_for_output(dev, dmap->fragment_size, dmap->nbufs))
  345. return;
  346. if (!(dmap->flags & DMA_NODMA))
  347. local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use,DMA_MODE_WRITE);
  348. dmap->flags |= DMA_STARTED;
  349. }
  350. if (dmap->counts[dmap->qhead] == 0)
  351. dmap->counts[dmap->qhead] = dmap->fragment_size;
  352. dmap->dma_mode = DMODE_OUTPUT;
  353. adev->d->output_block(dev, dmap->raw_buf_phys + dmap->qhead * dmap->fragment_size,
  354. dmap->counts[dmap->qhead], 1);
  355. if (adev->d->trigger)
  356. adev->d->trigger(dev,adev->enable_bits * adev->go);
  357. }
  358. dmap->flags |= DMA_ACTIVE;
  359. }
  360. int DMAbuf_sync(int dev)
  361. {
  362. struct audio_operations *adev = audio_devs[dev];
  363. unsigned long flags;
  364. int n = 0;
  365. struct dma_buffparms *dmap;
  366. if (!adev->go && !(adev->enable_bits & PCM_ENABLE_OUTPUT))
  367. return 0;
  368. if (adev->dmap_out->dma_mode == DMODE_OUTPUT) {
  369. dmap = adev->dmap_out;
  370. spin_lock_irqsave(&dmap->lock,flags);
  371. if (dmap->qlen > 0 && !(dmap->flags & DMA_ACTIVE))
  372. DMAbuf_launch_output(dev, dmap);
  373. adev->dmap_out->flags |= DMA_SYNCING;
  374. adev->dmap_out->underrun_count = 0;
  375. while (!signal_pending(current) && n++ < adev->dmap_out->nbufs &&
  376. adev->dmap_out->qlen && adev->dmap_out->underrun_count == 0) {
  377. long t = dmabuf_timeout(dmap);
  378. spin_unlock_irqrestore(&dmap->lock,flags);
  379. /* FIXME: not safe may miss events */
  380. t = oss_broken_sleep_on(&adev->out_sleeper, t);
  381. spin_lock_irqsave(&dmap->lock,flags);
  382. if (!t) {
  383. adev->dmap_out->flags &= ~DMA_SYNCING;
  384. spin_unlock_irqrestore(&dmap->lock,flags);
  385. return adev->dmap_out->qlen;
  386. }
  387. }
  388. adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
  389. /*
  390. * Some devices such as GUS have huge amount of on board RAM for the
  391. * audio data. We have to wait until the device has finished playing.
  392. */
  393. /* still holding the lock */
  394. if (adev->d->local_qlen) { /* Device has hidden buffers */
  395. while (!signal_pending(current) &&
  396. adev->d->local_qlen(dev)){
  397. spin_unlock_irqrestore(&dmap->lock,flags);
  398. oss_broken_sleep_on(&adev->out_sleeper,
  399. dmabuf_timeout(dmap));
  400. spin_lock_irqsave(&dmap->lock,flags);
  401. }
  402. }
  403. spin_unlock_irqrestore(&dmap->lock,flags);
  404. }
  405. adev->dmap_out->dma_mode = DMODE_NONE;
  406. return adev->dmap_out->qlen;
  407. }
  408. int DMAbuf_release(int dev, int mode)
  409. {
  410. struct audio_operations *adev = audio_devs[dev];
  411. struct dma_buffparms *dmap;
  412. unsigned long flags;
  413. dmap = adev->dmap_out;
  414. if (adev->open_mode & OPEN_WRITE)
  415. adev->dmap_out->closing = 1;
  416. if (adev->open_mode & OPEN_READ){
  417. adev->dmap_in->closing = 1;
  418. dmap = adev->dmap_in;
  419. }
  420. if (adev->open_mode & OPEN_WRITE)
  421. if (!(adev->dmap_out->mapping_flags & DMA_MAP_MAPPED))
  422. if (!signal_pending(current) && (adev->dmap_out->dma_mode == DMODE_OUTPUT))
  423. DMAbuf_sync(dev);
  424. if (adev->dmap_out->dma_mode == DMODE_OUTPUT)
  425. memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte, adev->dmap_out->bytes_in_use);
  426. DMAbuf_reset(dev);
  427. spin_lock_irqsave(&dmap->lock,flags);
  428. adev->d->close(dev);
  429. if (adev->open_mode & OPEN_WRITE)
  430. close_dmap(adev, adev->dmap_out);
  431. if (adev->open_mode == OPEN_READ ||
  432. (adev->open_mode != OPEN_WRITE &&
  433. (adev->flags & DMA_DUPLEX)))
  434. close_dmap(adev, adev->dmap_in);
  435. adev->open_mode = 0;
  436. spin_unlock_irqrestore(&dmap->lock,flags);
  437. return 0;
  438. }
  439. /* called with dmap->lock dold */
  440. int DMAbuf_activate_recording(int dev, struct dma_buffparms *dmap)
  441. {
  442. struct audio_operations *adev = audio_devs[dev];
  443. int err;
  444. if (!(adev->open_mode & OPEN_READ))
  445. return 0;
  446. if (!(adev->enable_bits & PCM_ENABLE_INPUT))
  447. return 0;
  448. if (dmap->dma_mode == DMODE_OUTPUT) { /* Direction change */
  449. /* release lock - it's not recursive */
  450. spin_unlock_irq(&dmap->lock);
  451. DMAbuf_sync(dev);
  452. DMAbuf_reset(dev);
  453. spin_lock_irq(&dmap->lock);
  454. dmap->dma_mode = DMODE_NONE;
  455. }
  456. if (!dmap->dma_mode) {
  457. reorganize_buffers(dev, dmap, 1);
  458. if ((err = adev->d->prepare_for_input(dev,
  459. dmap->fragment_size, dmap->nbufs)) < 0)
  460. return err;
  461. dmap->dma_mode = DMODE_INPUT;
  462. }
  463. if (!(dmap->flags & DMA_ACTIVE)) {
  464. if (dmap->needs_reorg)
  465. reorganize_buffers(dev, dmap, 0);
  466. local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use, DMA_MODE_READ);
  467. adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size,
  468. dmap->fragment_size, 0);
  469. dmap->flags |= DMA_ACTIVE;
  470. if (adev->d->trigger)
  471. adev->d->trigger(dev, adev->enable_bits * adev->go);
  472. }
  473. return 0;
  474. }
  475. /* acquires lock */
  476. int DMAbuf_getrdbuffer(int dev, char **buf, int *len, int dontblock)
  477. {
  478. struct audio_operations *adev = audio_devs[dev];
  479. unsigned long flags;
  480. int err = 0, n = 0;
  481. struct dma_buffparms *dmap = adev->dmap_in;
  482. if (!(adev->open_mode & OPEN_READ))
  483. return -EIO;
  484. spin_lock_irqsave(&dmap->lock,flags);
  485. if (dmap->needs_reorg)
  486. reorganize_buffers(dev, dmap, 0);
  487. if (adev->dmap_in->mapping_flags & DMA_MAP_MAPPED) {
  488. /* printk(KERN_WARNING "Sound: Can't read from mmapped device (1)\n");*/
  489. spin_unlock_irqrestore(&dmap->lock,flags);
  490. return -EINVAL;
  491. } else while (dmap->qlen <= 0 && n++ < 10) {
  492. long timeout = MAX_SCHEDULE_TIMEOUT;
  493. if (!(adev->enable_bits & PCM_ENABLE_INPUT) || !adev->go) {
  494. spin_unlock_irqrestore(&dmap->lock,flags);
  495. return -EAGAIN;
  496. }
  497. if ((err = DMAbuf_activate_recording(dev, dmap)) < 0) {
  498. spin_unlock_irqrestore(&dmap->lock,flags);
  499. return err;
  500. }
  501. /* Wait for the next block */
  502. if (dontblock) {
  503. spin_unlock_irqrestore(&dmap->lock,flags);
  504. return -EAGAIN;
  505. }
  506. if (adev->go)
  507. timeout = dmabuf_timeout(dmap);
  508. spin_unlock_irqrestore(&dmap->lock,flags);
  509. timeout = oss_broken_sleep_on(&adev->in_sleeper, timeout);
  510. if (!timeout) {
  511. /* FIXME: include device name */
  512. err = -EIO;
  513. printk(KERN_WARNING "Sound: DMA (input) timed out - IRQ/DRQ config error?\n");
  514. dma_reset_input(dev);
  515. } else
  516. err = -EINTR;
  517. spin_lock_irqsave(&dmap->lock,flags);
  518. }
  519. spin_unlock_irqrestore(&dmap->lock,flags);
  520. if (dmap->qlen <= 0)
  521. return err ? err : -EINTR;
  522. *buf = &dmap->raw_buf[dmap->qhead * dmap->fragment_size + dmap->counts[dmap->qhead]];
  523. *len = dmap->fragment_size - dmap->counts[dmap->qhead];
  524. return dmap->qhead;
  525. }
  526. int DMAbuf_rmchars(int dev, int buff_no, int c)
  527. {
  528. struct audio_operations *adev = audio_devs[dev];
  529. struct dma_buffparms *dmap = adev->dmap_in;
  530. int p = dmap->counts[dmap->qhead] + c;
  531. if (dmap->mapping_flags & DMA_MAP_MAPPED)
  532. {
  533. /* printk("Sound: Can't read from mmapped device (2)\n");*/
  534. return -EINVAL;
  535. }
  536. else if (dmap->qlen <= 0)
  537. return -EIO;
  538. else if (p >= dmap->fragment_size) { /* This buffer is completely empty */
  539. dmap->counts[dmap->qhead] = 0;
  540. dmap->qlen--;
  541. dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
  542. }
  543. else dmap->counts[dmap->qhead] = p;
  544. return 0;
  545. }
  546. /* MUST be called with dmap->lock hold */
  547. int DMAbuf_get_buffer_pointer(int dev, struct dma_buffparms *dmap, int direction)
  548. {
  549. /*
  550. * Try to approximate the active byte position of the DMA pointer within the
  551. * buffer area as well as possible.
  552. */
  553. int pos;
  554. unsigned long f;
  555. if (!(dmap->flags & DMA_ACTIVE))
  556. pos = 0;
  557. else {
  558. int chan = dmap->dma;
  559. f=claim_dma_lock();
  560. clear_dma_ff(chan);
  561. if(!isa_dma_bridge_buggy)
  562. disable_dma(dmap->dma);
  563. pos = get_dma_residue(chan);
  564. pos = dmap->bytes_in_use - pos;
  565. if (!(dmap->mapping_flags & DMA_MAP_MAPPED)) {
  566. if (direction == DMODE_OUTPUT) {
  567. if (dmap->qhead == 0)
  568. if (pos > dmap->fragment_size)
  569. pos = 0;
  570. } else {
  571. if (dmap->qtail == 0)
  572. if (pos > dmap->fragment_size)
  573. pos = 0;
  574. }
  575. }
  576. if (pos < 0)
  577. pos = 0;
  578. if (pos >= dmap->bytes_in_use)
  579. pos = 0;
  580. if(!isa_dma_bridge_buggy)
  581. enable_dma(dmap->dma);
  582. release_dma_lock(f);
  583. }
  584. /* printk( "%04x ", pos); */
  585. return pos;
  586. }
  587. /*
  588. * DMAbuf_start_devices() is called by the /dev/music driver to start
  589. * one or more audio devices at desired moment.
  590. */
  591. void DMAbuf_start_devices(unsigned int devmask)
  592. {
  593. struct audio_operations *adev;
  594. int dev;
  595. for (dev = 0; dev < num_audiodevs; dev++) {
  596. if (!(devmask & (1 << dev)))
  597. continue;
  598. if (!(adev = audio_devs[dev]))
  599. continue;
  600. if (adev->open_mode == 0)
  601. continue;
  602. if (adev->go)
  603. continue;
  604. /* OK to start the device */
  605. adev->go = 1;
  606. if (adev->d->trigger)
  607. adev->d->trigger(dev,adev->enable_bits * adev->go);
  608. }
  609. }
  610. /* via poll called without a lock ?*/
  611. int DMAbuf_space_in_queue(int dev)
  612. {
  613. struct audio_operations *adev = audio_devs[dev];
  614. int len, max, tmp;
  615. struct dma_buffparms *dmap = adev->dmap_out;
  616. int lim = dmap->nbufs;
  617. if (lim < 2)
  618. lim = 2;
  619. if (dmap->qlen >= lim) /* No space at all */
  620. return 0;
  621. /*
  622. * Verify that there are no more pending buffers than the limit
  623. * defined by the process.
  624. */
  625. max = dmap->max_fragments;
  626. if (max > lim)
  627. max = lim;
  628. len = dmap->qlen;
  629. if (adev->d->local_qlen) {
  630. tmp = adev->d->local_qlen(dev);
  631. if (tmp && len)
  632. tmp--; /* This buffer has been counted twice */
  633. len += tmp;
  634. }
  635. if (dmap->byte_counter % dmap->fragment_size) /* There is a partial fragment */
  636. len = len + 1;
  637. if (len >= max)
  638. return 0;
  639. return max - len;
  640. }
  641. /* MUST not hold the spinlock - this function may sleep */
  642. static int output_sleep(int dev, int dontblock)
  643. {
  644. struct audio_operations *adev = audio_devs[dev];
  645. int err = 0;
  646. struct dma_buffparms *dmap = adev->dmap_out;
  647. long timeout;
  648. long timeout_value;
  649. if (dontblock)
  650. return -EAGAIN;
  651. if (!(adev->enable_bits & PCM_ENABLE_OUTPUT))
  652. return -EAGAIN;
  653. /*
  654. * Wait for free space
  655. */
  656. if (signal_pending(current))
  657. return -EINTR;
  658. timeout = (adev->go && !(dmap->flags & DMA_NOTIMEOUT));
  659. if (timeout)
  660. timeout_value = dmabuf_timeout(dmap);
  661. else
  662. timeout_value = MAX_SCHEDULE_TIMEOUT;
  663. timeout_value = oss_broken_sleep_on(&adev->out_sleeper, timeout_value);
  664. if (timeout != MAX_SCHEDULE_TIMEOUT && !timeout_value) {
  665. printk(KERN_WARNING "Sound: DMA (output) timed out - IRQ/DRQ config error?\n");
  666. dma_reset_output(dev);
  667. } else {
  668. if (signal_pending(current))
  669. err = -EINTR;
  670. }
  671. return err;
  672. }
  673. /* called with the lock held */
  674. static int find_output_space(int dev, char **buf, int *size)
  675. {
  676. struct audio_operations *adev = audio_devs[dev];
  677. struct dma_buffparms *dmap = adev->dmap_out;
  678. unsigned long active_offs;
  679. long len, offs;
  680. int maxfrags;
  681. int occupied_bytes = (dmap->user_counter % dmap->fragment_size);
  682. *buf = dmap->raw_buf;
  683. if (!(maxfrags = DMAbuf_space_in_queue(dev)) && !occupied_bytes)
  684. return 0;
  685. #ifdef BE_CONSERVATIVE
  686. active_offs = dmap->byte_counter + dmap->qhead * dmap->fragment_size;
  687. #else
  688. active_offs = max(DMAbuf_get_buffer_pointer(dev, dmap, DMODE_OUTPUT), 0);
  689. /* Check for pointer wrapping situation */
  690. if (active_offs >= dmap->bytes_in_use)
  691. active_offs = 0;
  692. active_offs += dmap->byte_counter;
  693. #endif
  694. offs = (dmap->user_counter % dmap->bytes_in_use) & ~SAMPLE_ROUNDUP;
  695. if (offs < 0 || offs >= dmap->bytes_in_use) {
  696. printk(KERN_ERR "Sound: Got unexpected offs %ld. Giving up.\n", offs);
  697. printk("Counter = %ld, bytes=%d\n", dmap->user_counter, dmap->bytes_in_use);
  698. return 0;
  699. }
  700. *buf = dmap->raw_buf + offs;
  701. len = active_offs + dmap->bytes_in_use - dmap->user_counter; /* Number of unused bytes in buffer */
  702. if ((offs + len) > dmap->bytes_in_use)
  703. len = dmap->bytes_in_use - offs;
  704. if (len < 0) {
  705. return 0;
  706. }
  707. if (len > ((maxfrags * dmap->fragment_size) - occupied_bytes))
  708. len = (maxfrags * dmap->fragment_size) - occupied_bytes;
  709. *size = len & ~SAMPLE_ROUNDUP;
  710. return (*size > 0);
  711. }
  712. /* acquires lock */
  713. int DMAbuf_getwrbuffer(int dev, char **buf, int *size, int dontblock)
  714. {
  715. struct audio_operations *adev = audio_devs[dev];
  716. unsigned long flags;
  717. int err = -EIO;
  718. struct dma_buffparms *dmap = adev->dmap_out;
  719. if (dmap->mapping_flags & DMA_MAP_MAPPED) {
  720. /* printk(KERN_DEBUG "Sound: Can't write to mmapped device (3)\n");*/
  721. return -EINVAL;
  722. }
  723. spin_lock_irqsave(&dmap->lock,flags);
  724. if (dmap->needs_reorg)
  725. reorganize_buffers(dev, dmap, 0);
  726. if (dmap->dma_mode == DMODE_INPUT) { /* Direction change */
  727. spin_unlock_irqrestore(&dmap->lock,flags);
  728. DMAbuf_reset(dev);
  729. spin_lock_irqsave(&dmap->lock,flags);
  730. }
  731. dmap->dma_mode = DMODE_OUTPUT;
  732. while (find_output_space(dev, buf, size) <= 0) {
  733. spin_unlock_irqrestore(&dmap->lock,flags);
  734. if ((err = output_sleep(dev, dontblock)) < 0) {
  735. return err;
  736. }
  737. spin_lock_irqsave(&dmap->lock,flags);
  738. }
  739. spin_unlock_irqrestore(&dmap->lock,flags);
  740. return 0;
  741. }
  742. /* has to acquire dmap->lock */
  743. int DMAbuf_move_wrpointer(int dev, int l)
  744. {
  745. struct audio_operations *adev = audio_devs[dev];
  746. struct dma_buffparms *dmap = adev->dmap_out;
  747. unsigned long ptr;
  748. unsigned long end_ptr, p;
  749. int post;
  750. unsigned long flags;
  751. spin_lock_irqsave(&dmap->lock,flags);
  752. post= (dmap->flags & DMA_POST);
  753. ptr = (dmap->user_counter / dmap->fragment_size) * dmap->fragment_size;
  754. dmap->flags &= ~DMA_POST;
  755. dmap->cfrag = -1;
  756. dmap->user_counter += l;
  757. dmap->flags |= DMA_DIRTY;
  758. if (dmap->byte_counter >= dmap->max_byte_counter) {
  759. /* Wrap the byte counters */
  760. long decr = dmap->byte_counter;
  761. dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
  762. decr -= dmap->byte_counter;
  763. dmap->user_counter -= decr;
  764. }
  765. end_ptr = (dmap->user_counter / dmap->fragment_size) * dmap->fragment_size;
  766. p = (dmap->user_counter - 1) % dmap->bytes_in_use;
  767. dmap->neutral_byte = dmap->raw_buf[p];
  768. /* Update the fragment based bookkeeping too */
  769. while (ptr < end_ptr) {
  770. dmap->counts[dmap->qtail] = dmap->fragment_size;
  771. dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
  772. dmap->qlen++;
  773. ptr += dmap->fragment_size;
  774. }
  775. dmap->counts[dmap->qtail] = dmap->user_counter - ptr;
  776. /*
  777. * Let the low level driver perform some postprocessing to
  778. * the written data.
  779. */
  780. if (adev->d->postprocess_write)
  781. adev->d->postprocess_write(dev);
  782. if (!(dmap->flags & DMA_ACTIVE))
  783. if (dmap->qlen > 1 || (dmap->qlen > 0 && (post || dmap->qlen >= dmap->nbufs - 1)))
  784. DMAbuf_launch_output(dev, dmap);
  785. spin_unlock_irqrestore(&dmap->lock,flags);
  786. return 0;
  787. }
  788. int DMAbuf_start_dma(int dev, unsigned long physaddr, int count, int dma_mode)
  789. {
  790. struct audio_operations *adev = audio_devs[dev];
  791. struct dma_buffparms *dmap = (dma_mode == DMA_MODE_WRITE) ? adev->dmap_out : adev->dmap_in;
  792. if (dmap->raw_buf == NULL) {
  793. printk(KERN_ERR "sound: DMA buffer(1) == NULL\n");
  794. printk("Device %d, chn=%s\n", dev, (dmap == adev->dmap_out) ? "out" : "in");
  795. return 0;
  796. }
  797. if (dmap->dma < 0)
  798. return 0;
  799. sound_start_dma(dmap, physaddr, count, dma_mode);
  800. return count;
  801. }
  802. EXPORT_SYMBOL(DMAbuf_start_dma);
  803. static int local_start_dma(struct audio_operations *adev, unsigned long physaddr, int count, int dma_mode)
  804. {
  805. struct dma_buffparms *dmap = (dma_mode == DMA_MODE_WRITE) ? adev->dmap_out : adev->dmap_in;
  806. if (dmap->raw_buf == NULL) {
  807. printk(KERN_ERR "sound: DMA buffer(2) == NULL\n");
  808. printk(KERN_ERR "Device %s, chn=%s\n", adev->name, (dmap == adev->dmap_out) ? "out" : "in");
  809. return 0;
  810. }
  811. if (dmap->flags & DMA_NODMA)
  812. return 1;
  813. if (dmap->dma < 0)
  814. return 0;
  815. sound_start_dma(dmap, dmap->raw_buf_phys, dmap->bytes_in_use, dma_mode | DMA_AUTOINIT);
  816. dmap->flags |= DMA_STARTED;
  817. return count;
  818. }
  819. static void finish_output_interrupt(int dev, struct dma_buffparms *dmap)
  820. {
  821. struct audio_operations *adev = audio_devs[dev];
  822. if (dmap->audio_callback != NULL)
  823. dmap->audio_callback(dev, dmap->callback_parm);
  824. wake_up(&adev->out_sleeper);
  825. wake_up(&adev->poll_sleeper);
  826. }
  827. /* called with dmap->lock held in irq context*/
  828. static void do_outputintr(int dev, int dummy)
  829. {
  830. struct audio_operations *adev = audio_devs[dev];
  831. struct dma_buffparms *dmap = adev->dmap_out;
  832. int this_fragment;
  833. if (dmap->raw_buf == NULL) {
  834. printk(KERN_ERR "Sound: Error. Audio interrupt (%d) after freeing buffers.\n", dev);
  835. return;
  836. }
  837. if (dmap->mapping_flags & DMA_MAP_MAPPED) { /* Virtual memory mapped access */
  838. /* mmapped access */
  839. dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
  840. if (dmap->qhead == 0) { /* Wrapped */
  841. dmap->byte_counter += dmap->bytes_in_use;
  842. if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
  843. long decr = dmap->byte_counter;
  844. dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
  845. decr -= dmap->byte_counter;
  846. dmap->user_counter -= decr;
  847. }
  848. }
  849. dmap->qlen++; /* Yes increment it (don't decrement) */
  850. if (!(adev->flags & DMA_AUTOMODE))
  851. dmap->flags &= ~DMA_ACTIVE;
  852. dmap->counts[dmap->qhead] = dmap->fragment_size;
  853. DMAbuf_launch_output(dev, dmap);
  854. finish_output_interrupt(dev, dmap);
  855. return;
  856. }
  857. dmap->qlen--;
  858. this_fragment = dmap->qhead;
  859. dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
  860. if (dmap->qhead == 0) { /* Wrapped */
  861. dmap->byte_counter += dmap->bytes_in_use;
  862. if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
  863. long decr = dmap->byte_counter;
  864. dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
  865. decr -= dmap->byte_counter;
  866. dmap->user_counter -= decr;
  867. }
  868. }
  869. if (!(adev->flags & DMA_AUTOMODE))
  870. dmap->flags &= ~DMA_ACTIVE;
  871. /*
  872. * This is dmap->qlen <= 0 except when closing when
  873. * dmap->qlen < 0
  874. */
  875. while (dmap->qlen <= -dmap->closing) {
  876. dmap->underrun_count++;
  877. dmap->qlen++;
  878. if ((dmap->flags & DMA_DIRTY) && dmap->applic_profile != APF_CPUINTENS) {
  879. dmap->flags &= ~DMA_DIRTY;
  880. memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
  881. adev->dmap_out->buffsize);
  882. }
  883. dmap->user_counter += dmap->fragment_size;
  884. dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
  885. }
  886. if (dmap->qlen > 0)
  887. DMAbuf_launch_output(dev, dmap);
  888. finish_output_interrupt(dev, dmap);
  889. }
  890. /* called in irq context */
  891. void DMAbuf_outputintr(int dev, int notify_only)
  892. {
  893. struct audio_operations *adev = audio_devs[dev];
  894. unsigned long flags;
  895. struct dma_buffparms *dmap = adev->dmap_out;
  896. spin_lock_irqsave(&dmap->lock,flags);
  897. if (!(dmap->flags & DMA_NODMA)) {
  898. int chan = dmap->dma, pos, n;
  899. unsigned long f;
  900. f=claim_dma_lock();
  901. if(!isa_dma_bridge_buggy)
  902. disable_dma(dmap->dma);
  903. clear_dma_ff(chan);
  904. pos = dmap->bytes_in_use - get_dma_residue(chan);
  905. if(!isa_dma_bridge_buggy)
  906. enable_dma(dmap->dma);
  907. release_dma_lock(f);
  908. pos = pos / dmap->fragment_size; /* Actual qhead */
  909. if (pos < 0 || pos >= dmap->nbufs)
  910. pos = 0;
  911. n = 0;
  912. while (dmap->qhead != pos && n++ < dmap->nbufs)
  913. do_outputintr(dev, notify_only);
  914. }
  915. else
  916. do_outputintr(dev, notify_only);
  917. spin_unlock_irqrestore(&dmap->lock,flags);
  918. }
  919. EXPORT_SYMBOL(DMAbuf_outputintr);
  920. /* called with dmap->lock held in irq context */
  921. static void do_inputintr(int dev)
  922. {
  923. struct audio_operations *adev = audio_devs[dev];
  924. struct dma_buffparms *dmap = adev->dmap_in;
  925. if (dmap->raw_buf == NULL) {
  926. printk(KERN_ERR "Sound: Fatal error. Audio interrupt after freeing buffers.\n");
  927. return;
  928. }
  929. if (dmap->mapping_flags & DMA_MAP_MAPPED) {
  930. dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
  931. if (dmap->qtail == 0) { /* Wrapped */
  932. dmap->byte_counter += dmap->bytes_in_use;
  933. if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
  934. long decr = dmap->byte_counter;
  935. dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use) + dmap->bytes_in_use;
  936. decr -= dmap->byte_counter;
  937. dmap->user_counter -= decr;
  938. }
  939. }
  940. dmap->qlen++;
  941. if (!(adev->flags & DMA_AUTOMODE)) {
  942. if (dmap->needs_reorg)
  943. reorganize_buffers(dev, dmap, 0);
  944. local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use,DMA_MODE_READ);
  945. adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size,
  946. dmap->fragment_size, 1);
  947. if (adev->d->trigger)
  948. adev->d->trigger(dev, adev->enable_bits * adev->go);
  949. }
  950. dmap->flags |= DMA_ACTIVE;
  951. } else if (dmap->qlen >= (dmap->nbufs - 1)) {
  952. printk(KERN_WARNING "Sound: Recording overrun\n");
  953. dmap->underrun_count++;
  954. /* Just throw away the oldest fragment but keep the engine running */
  955. dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
  956. dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
  957. } else if (dmap->qlen >= 0 && dmap->qlen < dmap->nbufs) {
  958. dmap->qlen++;
  959. dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
  960. if (dmap->qtail == 0) { /* Wrapped */
  961. dmap->byte_counter += dmap->bytes_in_use;
  962. if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
  963. long decr = dmap->byte_counter;
  964. dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use) + dmap->bytes_in_use;
  965. decr -= dmap->byte_counter;
  966. dmap->user_counter -= decr;
  967. }
  968. }
  969. }
  970. if (!(adev->flags & DMA_AUTOMODE) || (dmap->flags & DMA_NODMA)) {
  971. local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use, DMA_MODE_READ);
  972. adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size, dmap->fragment_size, 1);
  973. if (adev->d->trigger)
  974. adev->d->trigger(dev,adev->enable_bits * adev->go);
  975. }
  976. dmap->flags |= DMA_ACTIVE;
  977. if (dmap->qlen > 0)
  978. {
  979. wake_up(&adev->in_sleeper);
  980. wake_up(&adev->poll_sleeper);
  981. }
  982. }
  983. /* called in irq context */
  984. void DMAbuf_inputintr(int dev)
  985. {
  986. struct audio_operations *adev = audio_devs[dev];
  987. struct dma_buffparms *dmap = adev->dmap_in;
  988. unsigned long flags;
  989. spin_lock_irqsave(&dmap->lock,flags);
  990. if (!(dmap->flags & DMA_NODMA)) {
  991. int chan = dmap->dma, pos, n;
  992. unsigned long f;
  993. f=claim_dma_lock();
  994. if(!isa_dma_bridge_buggy)
  995. disable_dma(dmap->dma);
  996. clear_dma_ff(chan);
  997. pos = dmap->bytes_in_use - get_dma_residue(chan);
  998. if(!isa_dma_bridge_buggy)
  999. enable_dma(dmap->dma);
  1000. release_dma_lock(f);
  1001. pos = pos / dmap->fragment_size; /* Actual qhead */
  1002. if (pos < 0 || pos >= dmap->nbufs)
  1003. pos = 0;
  1004. n = 0;
  1005. while (dmap->qtail != pos && ++n < dmap->nbufs)
  1006. do_inputintr(dev);
  1007. } else
  1008. do_inputintr(dev);
  1009. spin_unlock_irqrestore(&dmap->lock,flags);
  1010. }
  1011. EXPORT_SYMBOL(DMAbuf_inputintr);
  1012. void DMAbuf_init(int dev, int dma1, int dma2)
  1013. {
  1014. struct audio_operations *adev = audio_devs[dev];
  1015. /*
  1016. * NOTE! This routine could be called several times.
  1017. */
  1018. if (adev && adev->dmap_out == NULL) {
  1019. if (adev->d == NULL)
  1020. panic("OSS: audio_devs[%d]->d == NULL\n", dev);
  1021. if (adev->parent_dev) { /* Use DMA map of the parent dev */
  1022. int parent = adev->parent_dev - 1;
  1023. adev->dmap_out = audio_devs[parent]->dmap_out;
  1024. adev->dmap_in = audio_devs[parent]->dmap_in;
  1025. } else {
  1026. adev->dmap_out = adev->dmap_in = &adev->dmaps[0];
  1027. adev->dmap_out->dma = dma1;
  1028. if (adev->flags & DMA_DUPLEX) {
  1029. adev->dmap_in = &adev->dmaps[1];
  1030. adev->dmap_in->dma = dma2;
  1031. }
  1032. }
  1033. /* Persistent DMA buffers allocated here */
  1034. if (sound_dmap_flag == DMAP_KEEP_ON_CLOSE) {
  1035. if (adev->dmap_in->raw_buf == NULL)
  1036. sound_alloc_dmap(adev->dmap_in);
  1037. if (adev->dmap_out->raw_buf == NULL)
  1038. sound_alloc_dmap(adev->dmap_out);
  1039. }
  1040. }
  1041. }
  1042. /* No kernel lock - DMAbuf_activate_recording protected by global cli/sti */
  1043. static unsigned int poll_input(struct file * file, int dev, poll_table *wait)
  1044. {
  1045. struct audio_operations *adev = audio_devs[dev];
  1046. struct dma_buffparms *dmap = adev->dmap_in;
  1047. if (!(adev->open_mode & OPEN_READ))
  1048. return 0;
  1049. if (dmap->mapping_flags & DMA_MAP_MAPPED) {
  1050. if (dmap->qlen)
  1051. return POLLIN | POLLRDNORM;
  1052. return 0;
  1053. }
  1054. if (dmap->dma_mode != DMODE_INPUT) {
  1055. if (dmap->dma_mode == DMODE_NONE &&
  1056. adev->enable_bits & PCM_ENABLE_INPUT &&
  1057. !dmap->qlen && adev->go) {
  1058. unsigned long flags;
  1059. spin_lock_irqsave(&dmap->lock,flags);
  1060. DMAbuf_activate_recording(dev, dmap);
  1061. spin_unlock_irqrestore(&dmap->lock,flags);
  1062. }
  1063. return 0;
  1064. }
  1065. if (!dmap->qlen)
  1066. return 0;
  1067. return POLLIN | POLLRDNORM;
  1068. }
  1069. static unsigned int poll_output(struct file * file, int dev, poll_table *wait)
  1070. {
  1071. struct audio_operations *adev = audio_devs[dev];
  1072. struct dma_buffparms *dmap = adev->dmap_out;
  1073. if (!(adev->open_mode & OPEN_WRITE))
  1074. return 0;
  1075. if (dmap->mapping_flags & DMA_MAP_MAPPED) {
  1076. if (dmap->qlen)
  1077. return POLLOUT | POLLWRNORM;
  1078. return 0;
  1079. }
  1080. if (dmap->dma_mode == DMODE_INPUT)
  1081. return 0;
  1082. if (dmap->dma_mode == DMODE_NONE)
  1083. return POLLOUT | POLLWRNORM;
  1084. if (!DMAbuf_space_in_queue(dev))
  1085. return 0;
  1086. return POLLOUT | POLLWRNORM;
  1087. }
  1088. unsigned int DMAbuf_poll(struct file * file, int dev, poll_table *wait)
  1089. {
  1090. struct audio_operations *adev = audio_devs[dev];
  1091. poll_wait(file, &adev->poll_sleeper, wait);
  1092. return poll_input(file, dev, wait) | poll_output(file, dev, wait);
  1093. }
  1094. void DMAbuf_deinit(int dev)
  1095. {
  1096. struct audio_operations *adev = audio_devs[dev];
  1097. /* This routine is called when driver is being unloaded */
  1098. if (!adev)
  1099. return;
  1100. /* Persistent DMA buffers deallocated here */
  1101. if (sound_dmap_flag == DMAP_KEEP_ON_CLOSE) {
  1102. sound_free_dmap(adev->dmap_out);
  1103. if (adev->flags & DMA_DUPLEX)
  1104. sound_free_dmap(adev->dmap_in);
  1105. }
  1106. }