dmabuf.c 35 KB

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