audio.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /*
  2. * sound/oss/audio.c
  3. *
  4. * Device file manager for /dev/audio
  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. /*
  14. * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
  15. * Thomas Sailer : moved several static variables into struct audio_operations
  16. * (which is grossly misnamed btw.) because they have the same
  17. * lifetime as the rest in there and dynamic allocation saves
  18. * 12k or so
  19. * Thomas Sailer : use more logical O_NONBLOCK semantics
  20. * Daniel Rodriksson: reworked the use of the device specific copy_user
  21. * still generic
  22. * Horst von Brand: Add missing #include <linux/string.h>
  23. * Chris Rankin : Update the module-usage counter for the coprocessor,
  24. * and decrement the counters again if we cannot open
  25. * the audio device.
  26. */
  27. #include <linux/stddef.h>
  28. #include <linux/string.h>
  29. #include <linux/kmod.h>
  30. #include "sound_config.h"
  31. #include "ulaw.h"
  32. #include "coproc.h"
  33. #define NEUTRAL8 0x80
  34. #define NEUTRAL16 0x00
  35. static int dma_ioctl(int dev, unsigned int cmd, void __user *arg);
  36. static int set_format(int dev, int fmt)
  37. {
  38. if (fmt != AFMT_QUERY)
  39. {
  40. audio_devs[dev]->local_conversion = 0;
  41. if (!(audio_devs[dev]->format_mask & fmt)) /* Not supported */
  42. {
  43. if (fmt == AFMT_MU_LAW)
  44. {
  45. fmt = AFMT_U8;
  46. audio_devs[dev]->local_conversion = CNV_MU_LAW;
  47. }
  48. else
  49. fmt = AFMT_U8; /* This is always supported */
  50. }
  51. audio_devs[dev]->audio_format = audio_devs[dev]->d->set_bits(dev, fmt);
  52. audio_devs[dev]->local_format = fmt;
  53. }
  54. else
  55. return audio_devs[dev]->local_format;
  56. if (audio_devs[dev]->local_conversion)
  57. return audio_devs[dev]->local_conversion;
  58. else
  59. return audio_devs[dev]->local_format;
  60. }
  61. int audio_open(int dev, struct file *file)
  62. {
  63. int ret;
  64. int bits;
  65. int dev_type = dev & 0x0f;
  66. int mode = translate_mode(file);
  67. const struct audio_driver *driver;
  68. const struct coproc_operations *coprocessor;
  69. dev = dev >> 4;
  70. if (dev_type == SND_DEV_DSP16)
  71. bits = 16;
  72. else
  73. bits = 8;
  74. if (dev < 0 || dev >= num_audiodevs)
  75. return -ENXIO;
  76. driver = audio_devs[dev]->d;
  77. if (!try_module_get(driver->owner))
  78. return -ENODEV;
  79. if ((ret = DMAbuf_open(dev, mode)) < 0)
  80. goto error_1;
  81. if ( (coprocessor = audio_devs[dev]->coproc) != NULL ) {
  82. if (!try_module_get(coprocessor->owner))
  83. goto error_2;
  84. if ((ret = coprocessor->open(coprocessor->devc, COPR_PCM)) < 0) {
  85. printk(KERN_WARNING "Sound: Can't access coprocessor device\n");
  86. goto error_3;
  87. }
  88. }
  89. audio_devs[dev]->local_conversion = 0;
  90. if (dev_type == SND_DEV_AUDIO)
  91. set_format(dev, AFMT_MU_LAW);
  92. else
  93. set_format(dev, bits);
  94. audio_devs[dev]->audio_mode = AM_NONE;
  95. return 0;
  96. /*
  97. * Clean-up stack: this is what needs (un)doing if
  98. * we can't open the audio device ...
  99. */
  100. error_3:
  101. module_put(coprocessor->owner);
  102. error_2:
  103. DMAbuf_release(dev, mode);
  104. error_1:
  105. module_put(driver->owner);
  106. return ret;
  107. }
  108. static void sync_output(int dev)
  109. {
  110. int p, i;
  111. int l;
  112. struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
  113. if (dmap->fragment_size <= 0)
  114. return;
  115. dmap->flags |= DMA_POST;
  116. /* Align the write pointer with fragment boundaries */
  117. if ((l = dmap->user_counter % dmap->fragment_size) > 0)
  118. {
  119. int len;
  120. unsigned long offs = dmap->user_counter % dmap->bytes_in_use;
  121. len = dmap->fragment_size - l;
  122. memset(dmap->raw_buf + offs, dmap->neutral_byte, len);
  123. DMAbuf_move_wrpointer(dev, len);
  124. }
  125. /*
  126. * Clean all unused buffer fragments.
  127. */
  128. p = dmap->qtail;
  129. dmap->flags |= DMA_POST;
  130. for (i = dmap->qlen + 1; i < dmap->nbufs; i++)
  131. {
  132. p = (p + 1) % dmap->nbufs;
  133. if (((dmap->raw_buf + p * dmap->fragment_size) + dmap->fragment_size) >
  134. (dmap->raw_buf + dmap->buffsize))
  135. printk(KERN_ERR "audio: Buffer error 2\n");
  136. memset(dmap->raw_buf + p * dmap->fragment_size,
  137. dmap->neutral_byte,
  138. dmap->fragment_size);
  139. }
  140. dmap->flags |= DMA_DIRTY;
  141. }
  142. void audio_release(int dev, struct file *file)
  143. {
  144. const struct coproc_operations *coprocessor;
  145. int mode = translate_mode(file);
  146. dev = dev >> 4;
  147. /*
  148. * We do this in DMAbuf_release(). Why are we doing it
  149. * here? Why don't we test the file mode before setting
  150. * both flags? DMAbuf_release() does.
  151. * ...pester...pester...pester...
  152. */
  153. audio_devs[dev]->dmap_out->closing = 1;
  154. audio_devs[dev]->dmap_in->closing = 1;
  155. /*
  156. * We need to make sure we allocated the dmap_out buffer
  157. * before we go mucking around with it in sync_output().
  158. */
  159. if (mode & OPEN_WRITE)
  160. sync_output(dev);
  161. if ( (coprocessor = audio_devs[dev]->coproc) != NULL ) {
  162. coprocessor->close(coprocessor->devc, COPR_PCM);
  163. module_put(coprocessor->owner);
  164. }
  165. DMAbuf_release(dev, mode);
  166. module_put(audio_devs[dev]->d->owner);
  167. }
  168. static void translate_bytes(const unsigned char *table, unsigned char *buff, int n)
  169. {
  170. unsigned long i;
  171. if (n <= 0)
  172. return;
  173. for (i = 0; i < n; ++i)
  174. buff[i] = table[buff[i]];
  175. }
  176. int audio_write(int dev, struct file *file, const char __user *buf, int count)
  177. {
  178. int c, p, l, buf_size, used, returned;
  179. int err;
  180. char *dma_buf;
  181. dev = dev >> 4;
  182. p = 0;
  183. c = count;
  184. if(count < 0)
  185. return -EINVAL;
  186. if (!(audio_devs[dev]->open_mode & OPEN_WRITE))
  187. return -EPERM;
  188. if (audio_devs[dev]->flags & DMA_DUPLEX)
  189. audio_devs[dev]->audio_mode |= AM_WRITE;
  190. else
  191. audio_devs[dev]->audio_mode = AM_WRITE;
  192. if (!count) /* Flush output */
  193. {
  194. sync_output(dev);
  195. return 0;
  196. }
  197. while (c)
  198. {
  199. if ((err = DMAbuf_getwrbuffer(dev, &dma_buf, &buf_size, !!(file->f_flags & O_NONBLOCK))) < 0)
  200. {
  201. /* Handle nonblocking mode */
  202. if ((file->f_flags & O_NONBLOCK) && err == -EAGAIN)
  203. return p? p : -EAGAIN; /* No more space. Return # of accepted bytes */
  204. return err;
  205. }
  206. l = c;
  207. if (l > buf_size)
  208. l = buf_size;
  209. returned = l;
  210. used = l;
  211. if (!audio_devs[dev]->d->copy_user)
  212. {
  213. if ((dma_buf + l) >
  214. (audio_devs[dev]->dmap_out->raw_buf + audio_devs[dev]->dmap_out->buffsize))
  215. {
  216. printk(KERN_ERR "audio: Buffer error 3 (%lx,%d), (%lx, %d)\n", (long) dma_buf, l, (long) audio_devs[dev]->dmap_out->raw_buf, (int) audio_devs[dev]->dmap_out->buffsize);
  217. return -EDOM;
  218. }
  219. if (dma_buf < audio_devs[dev]->dmap_out->raw_buf)
  220. {
  221. printk(KERN_ERR "audio: Buffer error 13 (%lx<%lx)\n", (long) dma_buf, (long) audio_devs[dev]->dmap_out->raw_buf);
  222. return -EDOM;
  223. }
  224. if(copy_from_user(dma_buf, &(buf)[p], l))
  225. return -EFAULT;
  226. }
  227. else audio_devs[dev]->d->copy_user (dev,
  228. dma_buf, 0,
  229. buf, p,
  230. c, buf_size,
  231. &used, &returned,
  232. l);
  233. l = returned;
  234. if (audio_devs[dev]->local_conversion & CNV_MU_LAW)
  235. {
  236. translate_bytes(ulaw_dsp, (unsigned char *) dma_buf, l);
  237. }
  238. c -= used;
  239. p += used;
  240. DMAbuf_move_wrpointer(dev, l);
  241. }
  242. return count;
  243. }
  244. int audio_read(int dev, struct file *file, char __user *buf, int count)
  245. {
  246. int c, p, l;
  247. char *dmabuf;
  248. int buf_no;
  249. dev = dev >> 4;
  250. p = 0;
  251. c = count;
  252. if (!(audio_devs[dev]->open_mode & OPEN_READ))
  253. return -EPERM;
  254. if ((audio_devs[dev]->audio_mode & AM_WRITE) && !(audio_devs[dev]->flags & DMA_DUPLEX))
  255. sync_output(dev);
  256. if (audio_devs[dev]->flags & DMA_DUPLEX)
  257. audio_devs[dev]->audio_mode |= AM_READ;
  258. else
  259. audio_devs[dev]->audio_mode = AM_READ;
  260. while(c)
  261. {
  262. if ((buf_no = DMAbuf_getrdbuffer(dev, &dmabuf, &l, !!(file->f_flags & O_NONBLOCK))) < 0)
  263. {
  264. /*
  265. * Nonblocking mode handling. Return current # of bytes
  266. */
  267. if (p > 0) /* Avoid throwing away data */
  268. return p; /* Return it instead */
  269. if ((file->f_flags & O_NONBLOCK) && buf_no == -EAGAIN)
  270. return -EAGAIN;
  271. return buf_no;
  272. }
  273. if (l > c)
  274. l = c;
  275. /*
  276. * Insert any local processing here.
  277. */
  278. if (audio_devs[dev]->local_conversion & CNV_MU_LAW)
  279. {
  280. translate_bytes(dsp_ulaw, (unsigned char *) dmabuf, l);
  281. }
  282. {
  283. char *fixit = dmabuf;
  284. if(copy_to_user(&(buf)[p], fixit, l))
  285. return -EFAULT;
  286. }
  287. DMAbuf_rmchars(dev, buf_no, l);
  288. p += l;
  289. c -= l;
  290. }
  291. return count - c;
  292. }
  293. int audio_ioctl(int dev, struct file *file, unsigned int cmd, void __user *arg)
  294. {
  295. int val, count;
  296. unsigned long flags;
  297. struct dma_buffparms *dmap;
  298. int __user *p = arg;
  299. dev = dev >> 4;
  300. if (_IOC_TYPE(cmd) == 'C') {
  301. if (audio_devs[dev]->coproc) /* Coprocessor ioctl */
  302. return audio_devs[dev]->coproc->ioctl(audio_devs[dev]->coproc->devc, cmd, arg, 0);
  303. /* else
  304. printk(KERN_DEBUG"/dev/dsp%d: No coprocessor for this device\n", dev); */
  305. return -ENXIO;
  306. }
  307. else switch (cmd)
  308. {
  309. case SNDCTL_DSP_SYNC:
  310. if (!(audio_devs[dev]->open_mode & OPEN_WRITE))
  311. return 0;
  312. if (audio_devs[dev]->dmap_out->fragment_size == 0)
  313. return 0;
  314. sync_output(dev);
  315. DMAbuf_sync(dev);
  316. DMAbuf_reset(dev);
  317. return 0;
  318. case SNDCTL_DSP_POST:
  319. if (!(audio_devs[dev]->open_mode & OPEN_WRITE))
  320. return 0;
  321. if (audio_devs[dev]->dmap_out->fragment_size == 0)
  322. return 0;
  323. audio_devs[dev]->dmap_out->flags |= DMA_POST | DMA_DIRTY;
  324. sync_output(dev);
  325. dma_ioctl(dev, SNDCTL_DSP_POST, NULL);
  326. return 0;
  327. case SNDCTL_DSP_RESET:
  328. audio_devs[dev]->audio_mode = AM_NONE;
  329. DMAbuf_reset(dev);
  330. return 0;
  331. case SNDCTL_DSP_GETFMTS:
  332. val = audio_devs[dev]->format_mask | AFMT_MU_LAW;
  333. break;
  334. case SNDCTL_DSP_SETFMT:
  335. if (get_user(val, p))
  336. return -EFAULT;
  337. val = set_format(dev, val);
  338. break;
  339. case SNDCTL_DSP_GETISPACE:
  340. if (!(audio_devs[dev]->open_mode & OPEN_READ))
  341. return 0;
  342. if ((audio_devs[dev]->audio_mode & AM_WRITE) && !(audio_devs[dev]->flags & DMA_DUPLEX))
  343. return -EBUSY;
  344. return dma_ioctl(dev, cmd, arg);
  345. case SNDCTL_DSP_GETOSPACE:
  346. if (!(audio_devs[dev]->open_mode & OPEN_WRITE))
  347. return -EPERM;
  348. if ((audio_devs[dev]->audio_mode & AM_READ) && !(audio_devs[dev]->flags & DMA_DUPLEX))
  349. return -EBUSY;
  350. return dma_ioctl(dev, cmd, arg);
  351. case SNDCTL_DSP_NONBLOCK:
  352. spin_lock(&file->f_lock);
  353. file->f_flags |= O_NONBLOCK;
  354. spin_unlock(&file->f_lock);
  355. return 0;
  356. case SNDCTL_DSP_GETCAPS:
  357. val = 1 | DSP_CAP_MMAP; /* Revision level of this ioctl() */
  358. if (audio_devs[dev]->flags & DMA_DUPLEX &&
  359. audio_devs[dev]->open_mode == OPEN_READWRITE)
  360. val |= DSP_CAP_DUPLEX;
  361. if (audio_devs[dev]->coproc)
  362. val |= DSP_CAP_COPROC;
  363. if (audio_devs[dev]->d->local_qlen) /* Device has hidden buffers */
  364. val |= DSP_CAP_BATCH;
  365. if (audio_devs[dev]->d->trigger) /* Supports SETTRIGGER */
  366. val |= DSP_CAP_TRIGGER;
  367. break;
  368. case SOUND_PCM_WRITE_RATE:
  369. if (get_user(val, p))
  370. return -EFAULT;
  371. val = audio_devs[dev]->d->set_speed(dev, val);
  372. break;
  373. case SOUND_PCM_READ_RATE:
  374. val = audio_devs[dev]->d->set_speed(dev, 0);
  375. break;
  376. case SNDCTL_DSP_STEREO:
  377. if (get_user(val, p))
  378. return -EFAULT;
  379. if (val > 1 || val < 0)
  380. return -EINVAL;
  381. val = audio_devs[dev]->d->set_channels(dev, val + 1) - 1;
  382. break;
  383. case SOUND_PCM_WRITE_CHANNELS:
  384. if (get_user(val, p))
  385. return -EFAULT;
  386. val = audio_devs[dev]->d->set_channels(dev, val);
  387. break;
  388. case SOUND_PCM_READ_CHANNELS:
  389. val = audio_devs[dev]->d->set_channels(dev, 0);
  390. break;
  391. case SOUND_PCM_READ_BITS:
  392. val = audio_devs[dev]->d->set_bits(dev, 0);
  393. break;
  394. case SNDCTL_DSP_SETDUPLEX:
  395. if (audio_devs[dev]->open_mode != OPEN_READWRITE)
  396. return -EPERM;
  397. return (audio_devs[dev]->flags & DMA_DUPLEX) ? 0 : -EIO;
  398. case SNDCTL_DSP_PROFILE:
  399. if (get_user(val, p))
  400. return -EFAULT;
  401. if (audio_devs[dev]->open_mode & OPEN_WRITE)
  402. audio_devs[dev]->dmap_out->applic_profile = val;
  403. if (audio_devs[dev]->open_mode & OPEN_READ)
  404. audio_devs[dev]->dmap_in->applic_profile = val;
  405. return 0;
  406. case SNDCTL_DSP_GETODELAY:
  407. dmap = audio_devs[dev]->dmap_out;
  408. if (!(audio_devs[dev]->open_mode & OPEN_WRITE))
  409. return -EINVAL;
  410. if (!(dmap->flags & DMA_ALLOC_DONE))
  411. {
  412. val=0;
  413. break;
  414. }
  415. spin_lock_irqsave(&dmap->lock,flags);
  416. /* Compute number of bytes that have been played */
  417. count = DMAbuf_get_buffer_pointer (dev, dmap, DMODE_OUTPUT);
  418. if (count < dmap->fragment_size && dmap->qhead != 0)
  419. count += dmap->bytes_in_use; /* Pointer wrap not handled yet */
  420. count += dmap->byte_counter;
  421. /* Subtract current count from the number of bytes written by app */
  422. count = dmap->user_counter - count;
  423. if (count < 0)
  424. count = 0;
  425. spin_unlock_irqrestore(&dmap->lock,flags);
  426. val = count;
  427. break;
  428. default:
  429. return dma_ioctl(dev, cmd, arg);
  430. }
  431. return put_user(val, p);
  432. }
  433. void audio_init_devices(void)
  434. {
  435. /*
  436. * NOTE! This routine could be called several times during boot.
  437. */
  438. }
  439. void reorganize_buffers(int dev, struct dma_buffparms *dmap, int recording)
  440. {
  441. /*
  442. * This routine breaks the physical device buffers to logical ones.
  443. */
  444. struct audio_operations *dsp_dev = audio_devs[dev];
  445. unsigned i, n;
  446. unsigned sr, nc, sz, bsz;
  447. sr = dsp_dev->d->set_speed(dev, 0);
  448. nc = dsp_dev->d->set_channels(dev, 0);
  449. sz = dsp_dev->d->set_bits(dev, 0);
  450. if (sz == 8)
  451. dmap->neutral_byte = NEUTRAL8;
  452. else
  453. dmap->neutral_byte = NEUTRAL16;
  454. if (sr < 1 || nc < 1 || sz < 1)
  455. {
  456. /* printk(KERN_DEBUG "Warning: Invalid PCM parameters[%d] sr=%d, nc=%d, sz=%d\n", dev, sr, nc, sz);*/
  457. sr = DSP_DEFAULT_SPEED;
  458. nc = 1;
  459. sz = 8;
  460. }
  461. sz = sr * nc * sz;
  462. sz /= 8; /* #bits -> #bytes */
  463. dmap->data_rate = sz;
  464. if (!dmap->needs_reorg)
  465. return;
  466. dmap->needs_reorg = 0;
  467. if (dmap->fragment_size == 0)
  468. {
  469. /* Compute the fragment size using the default algorithm */
  470. /*
  471. * Compute a buffer size for time not exceeding 1 second.
  472. * Usually this algorithm gives a buffer size for 0.5 to 1.0 seconds
  473. * of sound (using the current speed, sample size and #channels).
  474. */
  475. bsz = dmap->buffsize;
  476. while (bsz > sz)
  477. bsz /= 2;
  478. if (bsz == dmap->buffsize)
  479. bsz /= 2; /* Needs at least 2 buffers */
  480. /*
  481. * Split the computed fragment to smaller parts. After 3.5a9
  482. * the default subdivision is 4 which should give better
  483. * results when recording.
  484. */
  485. if (dmap->subdivision == 0) /* Not already set */
  486. {
  487. dmap->subdivision = 4; /* Init to the default value */
  488. if ((bsz / dmap->subdivision) > 4096)
  489. dmap->subdivision *= 2;
  490. if ((bsz / dmap->subdivision) < 4096)
  491. dmap->subdivision = 1;
  492. }
  493. bsz /= dmap->subdivision;
  494. if (bsz < 16)
  495. bsz = 16; /* Just a sanity check */
  496. dmap->fragment_size = bsz;
  497. }
  498. else
  499. {
  500. /*
  501. * The process has specified the buffer size with SNDCTL_DSP_SETFRAGMENT or
  502. * the buffer size computation has already been done.
  503. */
  504. if (dmap->fragment_size > (dmap->buffsize / 2))
  505. dmap->fragment_size = (dmap->buffsize / 2);
  506. bsz = dmap->fragment_size;
  507. }
  508. if (audio_devs[dev]->min_fragment)
  509. if (bsz < (1 << audio_devs[dev]->min_fragment))
  510. bsz = 1 << audio_devs[dev]->min_fragment;
  511. if (audio_devs[dev]->max_fragment)
  512. if (bsz > (1 << audio_devs[dev]->max_fragment))
  513. bsz = 1 << audio_devs[dev]->max_fragment;
  514. bsz &= ~0x07; /* Force size which is multiple of 8 bytes */
  515. #ifdef OS_DMA_ALIGN_CHECK
  516. OS_DMA_ALIGN_CHECK(bsz);
  517. #endif
  518. n = dmap->buffsize / bsz;
  519. if (n > MAX_SUB_BUFFERS)
  520. n = MAX_SUB_BUFFERS;
  521. if (n > dmap->max_fragments)
  522. n = dmap->max_fragments;
  523. if (n < 2)
  524. {
  525. n = 2;
  526. bsz /= 2;
  527. }
  528. dmap->nbufs = n;
  529. dmap->bytes_in_use = n * bsz;
  530. dmap->fragment_size = bsz;
  531. dmap->max_byte_counter = (dmap->data_rate * 60 * 60) +
  532. dmap->bytes_in_use; /* Approximately one hour */
  533. if (dmap->raw_buf)
  534. {
  535. memset(dmap->raw_buf, dmap->neutral_byte, dmap->bytes_in_use);
  536. }
  537. for (i = 0; i < dmap->nbufs; i++)
  538. {
  539. dmap->counts[i] = 0;
  540. }
  541. dmap->flags |= DMA_ALLOC_DONE | DMA_EMPTY;
  542. }
  543. static int dma_subdivide(int dev, struct dma_buffparms *dmap, int fact)
  544. {
  545. if (fact == 0)
  546. {
  547. fact = dmap->subdivision;
  548. if (fact == 0)
  549. fact = 1;
  550. return fact;
  551. }
  552. if (dmap->subdivision != 0 || dmap->fragment_size) /* Too late to change */
  553. return -EINVAL;
  554. if (fact > MAX_REALTIME_FACTOR)
  555. return -EINVAL;
  556. if (fact != 1 && fact != 2 && fact != 4 && fact != 8 && fact != 16)
  557. return -EINVAL;
  558. dmap->subdivision = fact;
  559. return fact;
  560. }
  561. static int dma_set_fragment(int dev, struct dma_buffparms *dmap, int fact)
  562. {
  563. int bytes, count;
  564. if (fact == 0)
  565. return -EIO;
  566. if (dmap->subdivision != 0 ||
  567. dmap->fragment_size) /* Too late to change */
  568. return -EINVAL;
  569. bytes = fact & 0xffff;
  570. count = (fact >> 16) & 0x7fff;
  571. if (count == 0)
  572. count = MAX_SUB_BUFFERS;
  573. else if (count < MAX_SUB_BUFFERS)
  574. count++;
  575. if (bytes < 4 || bytes > 17) /* <16 || > 512k */
  576. return -EINVAL;
  577. if (count < 2)
  578. return -EINVAL;
  579. if (audio_devs[dev]->min_fragment > 0)
  580. if (bytes < audio_devs[dev]->min_fragment)
  581. bytes = audio_devs[dev]->min_fragment;
  582. if (audio_devs[dev]->max_fragment > 0)
  583. if (bytes > audio_devs[dev]->max_fragment)
  584. bytes = audio_devs[dev]->max_fragment;
  585. #ifdef OS_DMA_MINBITS
  586. if (bytes < OS_DMA_MINBITS)
  587. bytes = OS_DMA_MINBITS;
  588. #endif
  589. dmap->fragment_size = (1 << bytes);
  590. dmap->max_fragments = count;
  591. if (dmap->fragment_size > dmap->buffsize)
  592. dmap->fragment_size = dmap->buffsize;
  593. if (dmap->fragment_size == dmap->buffsize &&
  594. audio_devs[dev]->flags & DMA_AUTOMODE)
  595. dmap->fragment_size /= 2; /* Needs at least 2 buffers */
  596. dmap->subdivision = 1; /* Disable SNDCTL_DSP_SUBDIVIDE */
  597. return bytes | ((count - 1) << 16);
  598. }
  599. static int dma_ioctl(int dev, unsigned int cmd, void __user *arg)
  600. {
  601. struct dma_buffparms *dmap_out = audio_devs[dev]->dmap_out;
  602. struct dma_buffparms *dmap_in = audio_devs[dev]->dmap_in;
  603. struct dma_buffparms *dmap;
  604. audio_buf_info info;
  605. count_info cinfo;
  606. int fact, ret, changed, bits, count, err;
  607. unsigned long flags;
  608. switch (cmd)
  609. {
  610. case SNDCTL_DSP_SUBDIVIDE:
  611. ret = 0;
  612. if (get_user(fact, (int __user *)arg))
  613. return -EFAULT;
  614. if (audio_devs[dev]->open_mode & OPEN_WRITE)
  615. ret = dma_subdivide(dev, dmap_out, fact);
  616. if (ret < 0)
  617. return ret;
  618. if (audio_devs[dev]->open_mode != OPEN_WRITE ||
  619. (audio_devs[dev]->flags & DMA_DUPLEX &&
  620. audio_devs[dev]->open_mode & OPEN_READ))
  621. ret = dma_subdivide(dev, dmap_in, fact);
  622. if (ret < 0)
  623. return ret;
  624. break;
  625. case SNDCTL_DSP_GETISPACE:
  626. case SNDCTL_DSP_GETOSPACE:
  627. dmap = dmap_out;
  628. if (cmd == SNDCTL_DSP_GETISPACE && !(audio_devs[dev]->open_mode & OPEN_READ))
  629. return -EINVAL;
  630. if (cmd == SNDCTL_DSP_GETOSPACE && !(audio_devs[dev]->open_mode & OPEN_WRITE))
  631. return -EINVAL;
  632. if (cmd == SNDCTL_DSP_GETISPACE && audio_devs[dev]->flags & DMA_DUPLEX)
  633. dmap = dmap_in;
  634. if (dmap->mapping_flags & DMA_MAP_MAPPED)
  635. return -EINVAL;
  636. if (!(dmap->flags & DMA_ALLOC_DONE))
  637. reorganize_buffers(dev, dmap, (cmd == SNDCTL_DSP_GETISPACE));
  638. info.fragstotal = dmap->nbufs;
  639. if (cmd == SNDCTL_DSP_GETISPACE)
  640. info.fragments = dmap->qlen;
  641. else
  642. {
  643. if (!DMAbuf_space_in_queue(dev))
  644. info.fragments = 0;
  645. else
  646. {
  647. info.fragments = DMAbuf_space_in_queue(dev);
  648. if (audio_devs[dev]->d->local_qlen)
  649. {
  650. int tmp = audio_devs[dev]->d->local_qlen(dev);
  651. if (tmp && info.fragments)
  652. tmp--; /*
  653. * This buffer has been counted twice
  654. */
  655. info.fragments -= tmp;
  656. }
  657. }
  658. }
  659. if (info.fragments < 0)
  660. info.fragments = 0;
  661. else if (info.fragments > dmap->nbufs)
  662. info.fragments = dmap->nbufs;
  663. info.fragsize = dmap->fragment_size;
  664. info.bytes = info.fragments * dmap->fragment_size;
  665. if (cmd == SNDCTL_DSP_GETISPACE && dmap->qlen)
  666. info.bytes -= dmap->counts[dmap->qhead];
  667. else
  668. {
  669. info.fragments = info.bytes / dmap->fragment_size;
  670. info.bytes -= dmap->user_counter % dmap->fragment_size;
  671. }
  672. if (copy_to_user(arg, &info, sizeof(info)))
  673. return -EFAULT;
  674. return 0;
  675. case SNDCTL_DSP_SETTRIGGER:
  676. if (get_user(bits, (int __user *)arg))
  677. return -EFAULT;
  678. bits &= audio_devs[dev]->open_mode;
  679. if (audio_devs[dev]->d->trigger == NULL)
  680. return -EINVAL;
  681. if (!(audio_devs[dev]->flags & DMA_DUPLEX) && (bits & PCM_ENABLE_INPUT) &&
  682. (bits & PCM_ENABLE_OUTPUT))
  683. return -EINVAL;
  684. if (bits & PCM_ENABLE_INPUT)
  685. {
  686. spin_lock_irqsave(&dmap_in->lock,flags);
  687. changed = (audio_devs[dev]->enable_bits ^ bits) & PCM_ENABLE_INPUT;
  688. if (changed && audio_devs[dev]->go)
  689. {
  690. reorganize_buffers(dev, dmap_in, 1);
  691. if ((err = audio_devs[dev]->d->prepare_for_input(dev,
  692. dmap_in->fragment_size, dmap_in->nbufs)) < 0) {
  693. spin_unlock_irqrestore(&dmap_in->lock,flags);
  694. return err;
  695. }
  696. dmap_in->dma_mode = DMODE_INPUT;
  697. audio_devs[dev]->enable_bits |= PCM_ENABLE_INPUT;
  698. DMAbuf_activate_recording(dev, dmap_in);
  699. } else
  700. audio_devs[dev]->enable_bits &= ~PCM_ENABLE_INPUT;
  701. spin_unlock_irqrestore(&dmap_in->lock,flags);
  702. }
  703. if (bits & PCM_ENABLE_OUTPUT)
  704. {
  705. spin_lock_irqsave(&dmap_out->lock,flags);
  706. changed = (audio_devs[dev]->enable_bits ^ bits) & PCM_ENABLE_OUTPUT;
  707. if (changed &&
  708. (dmap_out->mapping_flags & DMA_MAP_MAPPED || dmap_out->qlen > 0) &&
  709. audio_devs[dev]->go)
  710. {
  711. if (!(dmap_out->flags & DMA_ALLOC_DONE))
  712. reorganize_buffers(dev, dmap_out, 0);
  713. dmap_out->dma_mode = DMODE_OUTPUT;
  714. audio_devs[dev]->enable_bits |= PCM_ENABLE_OUTPUT;
  715. dmap_out->counts[dmap_out->qhead] = dmap_out->fragment_size;
  716. DMAbuf_launch_output(dev, dmap_out);
  717. } else
  718. audio_devs[dev]->enable_bits &= ~PCM_ENABLE_OUTPUT;
  719. spin_unlock_irqrestore(&dmap_out->lock,flags);
  720. }
  721. #if 0
  722. if (changed && audio_devs[dev]->d->trigger)
  723. audio_devs[dev]->d->trigger(dev, bits * audio_devs[dev]->go);
  724. #endif
  725. /* Falls through... */
  726. case SNDCTL_DSP_GETTRIGGER:
  727. ret = audio_devs[dev]->enable_bits;
  728. break;
  729. case SNDCTL_DSP_SETSYNCRO:
  730. if (!audio_devs[dev]->d->trigger)
  731. return -EINVAL;
  732. audio_devs[dev]->d->trigger(dev, 0);
  733. audio_devs[dev]->go = 0;
  734. return 0;
  735. case SNDCTL_DSP_GETIPTR:
  736. if (!(audio_devs[dev]->open_mode & OPEN_READ))
  737. return -EINVAL;
  738. spin_lock_irqsave(&dmap_in->lock,flags);
  739. cinfo.bytes = dmap_in->byte_counter;
  740. cinfo.ptr = DMAbuf_get_buffer_pointer(dev, dmap_in, DMODE_INPUT) & ~3;
  741. if (cinfo.ptr < dmap_in->fragment_size && dmap_in->qtail != 0)
  742. cinfo.bytes += dmap_in->bytes_in_use; /* Pointer wrap not handled yet */
  743. cinfo.blocks = dmap_in->qlen;
  744. cinfo.bytes += cinfo.ptr;
  745. if (dmap_in->mapping_flags & DMA_MAP_MAPPED)
  746. dmap_in->qlen = 0; /* Reset interrupt counter */
  747. spin_unlock_irqrestore(&dmap_in->lock,flags);
  748. if (copy_to_user(arg, &cinfo, sizeof(cinfo)))
  749. return -EFAULT;
  750. return 0;
  751. case SNDCTL_DSP_GETOPTR:
  752. if (!(audio_devs[dev]->open_mode & OPEN_WRITE))
  753. return -EINVAL;
  754. spin_lock_irqsave(&dmap_out->lock,flags);
  755. cinfo.bytes = dmap_out->byte_counter;
  756. cinfo.ptr = DMAbuf_get_buffer_pointer(dev, dmap_out, DMODE_OUTPUT) & ~3;
  757. if (cinfo.ptr < dmap_out->fragment_size && dmap_out->qhead != 0)
  758. cinfo.bytes += dmap_out->bytes_in_use; /* Pointer wrap not handled yet */
  759. cinfo.blocks = dmap_out->qlen;
  760. cinfo.bytes += cinfo.ptr;
  761. if (dmap_out->mapping_flags & DMA_MAP_MAPPED)
  762. dmap_out->qlen = 0; /* Reset interrupt counter */
  763. spin_unlock_irqrestore(&dmap_out->lock,flags);
  764. if (copy_to_user(arg, &cinfo, sizeof(cinfo)))
  765. return -EFAULT;
  766. return 0;
  767. case SNDCTL_DSP_GETODELAY:
  768. if (!(audio_devs[dev]->open_mode & OPEN_WRITE))
  769. return -EINVAL;
  770. if (!(dmap_out->flags & DMA_ALLOC_DONE))
  771. {
  772. ret=0;
  773. break;
  774. }
  775. spin_lock_irqsave(&dmap_out->lock,flags);
  776. /* Compute number of bytes that have been played */
  777. count = DMAbuf_get_buffer_pointer (dev, dmap_out, DMODE_OUTPUT);
  778. if (count < dmap_out->fragment_size && dmap_out->qhead != 0)
  779. count += dmap_out->bytes_in_use; /* Pointer wrap not handled yet */
  780. count += dmap_out->byte_counter;
  781. /* Subtract current count from the number of bytes written by app */
  782. count = dmap_out->user_counter - count;
  783. if (count < 0)
  784. count = 0;
  785. spin_unlock_irqrestore(&dmap_out->lock,flags);
  786. ret = count;
  787. break;
  788. case SNDCTL_DSP_POST:
  789. if (audio_devs[dev]->dmap_out->qlen > 0)
  790. if (!(audio_devs[dev]->dmap_out->flags & DMA_ACTIVE))
  791. DMAbuf_launch_output(dev, audio_devs[dev]->dmap_out);
  792. return 0;
  793. case SNDCTL_DSP_GETBLKSIZE:
  794. dmap = dmap_out;
  795. if (audio_devs[dev]->open_mode & OPEN_WRITE)
  796. reorganize_buffers(dev, dmap_out, (audio_devs[dev]->open_mode == OPEN_READ));
  797. if (audio_devs[dev]->open_mode == OPEN_READ ||
  798. (audio_devs[dev]->flags & DMA_DUPLEX &&
  799. audio_devs[dev]->open_mode & OPEN_READ))
  800. reorganize_buffers(dev, dmap_in, (audio_devs[dev]->open_mode == OPEN_READ));
  801. if (audio_devs[dev]->open_mode == OPEN_READ)
  802. dmap = dmap_in;
  803. ret = dmap->fragment_size;
  804. break;
  805. case SNDCTL_DSP_SETFRAGMENT:
  806. ret = 0;
  807. if (get_user(fact, (int __user *)arg))
  808. return -EFAULT;
  809. if (audio_devs[dev]->open_mode & OPEN_WRITE)
  810. ret = dma_set_fragment(dev, dmap_out, fact);
  811. if (ret < 0)
  812. return ret;
  813. if (audio_devs[dev]->open_mode == OPEN_READ ||
  814. (audio_devs[dev]->flags & DMA_DUPLEX &&
  815. audio_devs[dev]->open_mode & OPEN_READ))
  816. ret = dma_set_fragment(dev, dmap_in, fact);
  817. if (ret < 0)
  818. return ret;
  819. if (!arg) /* don't know what this is good for, but preserve old semantics */
  820. return 0;
  821. break;
  822. default:
  823. if (!audio_devs[dev]->d->ioctl)
  824. return -EINVAL;
  825. return audio_devs[dev]->d->ioctl(dev, cmd, arg);
  826. }
  827. return put_user(ret, (int __user *)arg);
  828. }