dmasound_core.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. /*
  2. * linux/sound/oss/dmasound/dmasound_core.c
  3. *
  4. *
  5. * OSS/Free compatible Atari TT/Falcon and Amiga DMA sound driver for
  6. * Linux/m68k
  7. * Extended to support Power Macintosh for Linux/ppc by Paul Mackerras
  8. *
  9. * (c) 1995 by Michael Schlueter & Michael Marte
  10. *
  11. * Michael Schlueter (michael@duck.syd.de) did the basic structure of the VFS
  12. * interface and the u-law to signed byte conversion.
  13. *
  14. * Michael Marte (marte@informatik.uni-muenchen.de) did the sound queue,
  15. * /dev/mixer, /dev/sndstat and complemented the VFS interface. He would like
  16. * to thank:
  17. * - Michael Schlueter for initial ideas and documentation on the MFP and
  18. * the DMA sound hardware.
  19. * - Therapy? for their CD 'Troublegum' which really made me rock.
  20. *
  21. * /dev/sndstat is based on code by Hannu Savolainen, the author of the
  22. * VoxWare family of drivers.
  23. *
  24. * This file is subject to the terms and conditions of the GNU General Public
  25. * License. See the file COPYING in the main directory of this archive
  26. * for more details.
  27. *
  28. * History:
  29. *
  30. * 1995/8/25 First release
  31. *
  32. * 1995/9/02 Roman Hodek:
  33. * - Fixed atari_stram_alloc() call, the timer
  34. * programming and several race conditions
  35. * 1995/9/14 Roman Hodek:
  36. * - After some discussion with Michael Schlueter,
  37. * revised the interrupt disabling
  38. * - Slightly speeded up U8->S8 translation by using
  39. * long operations where possible
  40. * - Added 4:3 interpolation for /dev/audio
  41. *
  42. * 1995/9/20 Torsten Scherer:
  43. * - Fixed a bug in sq_write and changed /dev/audio
  44. * converting to play at 12517Hz instead of 6258Hz.
  45. *
  46. * 1995/9/23 Torsten Scherer:
  47. * - Changed sq_interrupt() and sq_play() to pre-program
  48. * the DMA for another frame while there's still one
  49. * running. This allows the IRQ response to be
  50. * arbitrarily delayed and playing will still continue.
  51. *
  52. * 1995/10/14 Guenther Kelleter, Torsten Scherer:
  53. * - Better support for Falcon audio (the Falcon doesn't
  54. * raise an IRQ at the end of a frame, but at the
  55. * beginning instead!). uses 'if (codec_dma)' in lots
  56. * of places to simply switch between Falcon and TT
  57. * code.
  58. *
  59. * 1995/11/06 Torsten Scherer:
  60. * - Started introducing a hardware abstraction scheme
  61. * (may perhaps also serve for Amigas?)
  62. * - Can now play samples at almost all frequencies by
  63. * means of a more generalized expand routine
  64. * - Takes a good deal of care to cut data only at
  65. * sample sizes
  66. * - Buffer size is now a kernel runtime option
  67. * - Implemented fsync() & several minor improvements
  68. * Guenther Kelleter:
  69. * - Useful hints and bug fixes
  70. * - Cross-checked it for Falcons
  71. *
  72. * 1996/3/9 Geert Uytterhoeven:
  73. * - Support added for Amiga, A-law, 16-bit little
  74. * endian.
  75. * - Unification to drivers/sound/dmasound.c.
  76. *
  77. * 1996/4/6 Martin Mitchell:
  78. * - Updated to 1.3 kernel.
  79. *
  80. * 1996/6/13 Topi Kanerva:
  81. * - Fixed things that were broken (mainly the amiga
  82. * 14-bit routines)
  83. * - /dev/sndstat shows now the real hardware frequency
  84. * - The lowpass filter is disabled by default now
  85. *
  86. * 1996/9/25 Geert Uytterhoeven:
  87. * - Modularization
  88. *
  89. * 1998/6/10 Andreas Schwab:
  90. * - Converted to use sound_core
  91. *
  92. * 1999/12/28 Richard Zidlicky:
  93. * - Added support for Q40
  94. *
  95. * 2000/2/27 Geert Uytterhoeven:
  96. * - Clean up and split the code into 4 parts:
  97. * o dmasound_core: machine-independent code
  98. * o dmasound_atari: Atari TT and Falcon support
  99. * o dmasound_awacs: Apple PowerMac support
  100. * o dmasound_paula: Amiga support
  101. *
  102. * 2000/3/25 Geert Uytterhoeven:
  103. * - Integration of dmasound_q40
  104. * - Small clean ups
  105. *
  106. * 2001/01/26 [1.0] Iain Sandoe
  107. * - make /dev/sndstat show revision & edition info.
  108. * - since dmasound.mach.sq_setup() can fail on pmac
  109. * its type has been changed to int and the returns
  110. * are checked.
  111. * [1.1] - stop missing translations from being called.
  112. * 2001/02/08 [1.2] - remove unused translation tables & move machine-
  113. * specific tables to low-level.
  114. * - return correct info. for SNDCTL_DSP_GETFMTS.
  115. * [1.3] - implement SNDCTL_DSP_GETCAPS fully.
  116. * [1.4] - make /dev/sndstat text length usage deterministic.
  117. * - make /dev/sndstat call to low-level
  118. * dmasound.mach.state_info() pass max space to ll driver.
  119. * - tidy startup banners and output info.
  120. * [1.5] - tidy up a little (removed some unused #defines in
  121. * dmasound.h)
  122. * - fix up HAS_RECORD conditionalisation.
  123. * - add record code in places it is missing...
  124. * - change buf-sizes to bytes to allow < 1kb for pmac
  125. * if user param entry is < 256 the value is taken to
  126. * be in kb > 256 is taken to be in bytes.
  127. * - make default buff/frag params conditional on
  128. * machine to allow smaller values for pmac.
  129. * - made the ioctls, read & write comply with the OSS
  130. * rules on setting params.
  131. * - added parsing of _setup() params for record.
  132. * 2001/04/04 [1.6] - fix bug where sample rates higher than maximum were
  133. * being reported as OK.
  134. * - fix open() to return -EBUSY as per OSS doc. when
  135. * audio is in use - this is independent of O_NOBLOCK.
  136. * - fix bug where SNDCTL_DSP_POST was blocking.
  137. */
  138. /* Record capability notes 30/01/2001:
  139. * At present these observations apply only to pmac LL driver (the only one
  140. * that can do record, at present). However, if other LL drivers for machines
  141. * with record are added they may apply.
  142. *
  143. * The fragment parameters for the record and play channels are separate.
  144. * However, if the driver is opened O_RDWR there is no way (in the current OSS
  145. * API) to specify their values independently for the record and playback
  146. * channels. Since the only common factor between the input & output is the
  147. * sample rate (on pmac) it should be possible to open /dev/dspX O_WRONLY and
  148. * /dev/dspY O_RDONLY. The input & output channels could then have different
  149. * characteristics (other than the first that sets sample rate claiming the
  150. * right to set it for ever). As it stands, the format, channels, number of
  151. * bits & sample rate are assumed to be common. In the future perhaps these
  152. * should be the responsibility of the LL driver - and then if a card really
  153. * does not share items between record & playback they can be specified
  154. * separately.
  155. */
  156. /* Thread-safeness of shared_resources notes: 31/01/2001
  157. * If the user opens O_RDWR and then splits record & play between two threads
  158. * both of which inherit the fd - and then starts changing things from both
  159. * - we will have difficulty telling.
  160. *
  161. * It's bad application coding - but ...
  162. * TODO: think about how to sort this out... without bogging everything down in
  163. * semaphores.
  164. *
  165. * Similarly, the OSS spec says "all changes to parameters must be between
  166. * open() and the first read() or write(). - and a bit later on (by
  167. * implication) "between SNDCTL_DSP_RESET and the first read() or write() after
  168. * it". If the app is multi-threaded and this rule is broken between threads
  169. * we will have trouble spotting it - and the fault will be rather obscure :-(
  170. *
  171. * We will try and put out at least a kmsg if we see it happen... but I think
  172. * it will be quite hard to trap it with an -EXXX return... because we can't
  173. * see the fault until after the damage is done.
  174. */
  175. #include <linux/module.h>
  176. #include <linux/slab.h>
  177. #include <linux/sound.h>
  178. #include <linux/init.h>
  179. #include <linux/soundcard.h>
  180. #include <linux/poll.h>
  181. #include <linux/mutex.h>
  182. #include <asm/uaccess.h>
  183. #include "dmasound.h"
  184. #define DMASOUND_CORE_REVISION 1
  185. #define DMASOUND_CORE_EDITION 6
  186. /*
  187. * Declarations
  188. */
  189. static DEFINE_MUTEX(dmasound_core_mutex);
  190. int dmasound_catchRadius = 0;
  191. module_param(dmasound_catchRadius, int, 0);
  192. static unsigned int numWriteBufs = DEFAULT_N_BUFFERS;
  193. module_param(numWriteBufs, int, 0);
  194. static unsigned int writeBufSize = DEFAULT_BUFF_SIZE ; /* in bytes */
  195. module_param(writeBufSize, int, 0);
  196. MODULE_LICENSE("GPL");
  197. #ifdef MODULE
  198. static int sq_unit = -1;
  199. static int mixer_unit = -1;
  200. static int state_unit = -1;
  201. static int irq_installed;
  202. #endif /* MODULE */
  203. /* control over who can modify resources shared between play/record */
  204. static fmode_t shared_resource_owner;
  205. static int shared_resources_initialised;
  206. /*
  207. * Mid level stuff
  208. */
  209. struct sound_settings dmasound = {
  210. .lock = __SPIN_LOCK_UNLOCKED(dmasound.lock)
  211. };
  212. static inline void sound_silence(void)
  213. {
  214. dmasound.mach.silence(); /* _MUST_ stop DMA */
  215. }
  216. static inline int sound_set_format(int format)
  217. {
  218. return dmasound.mach.setFormat(format);
  219. }
  220. static int sound_set_speed(int speed)
  221. {
  222. if (speed < 0)
  223. return dmasound.soft.speed;
  224. /* trap out-of-range speed settings.
  225. at present we allow (arbitrarily) low rates - using soft
  226. up-conversion - but we can't allow > max because there is
  227. no soft down-conversion.
  228. */
  229. if (dmasound.mach.max_dsp_speed &&
  230. (speed > dmasound.mach.max_dsp_speed))
  231. speed = dmasound.mach.max_dsp_speed ;
  232. dmasound.soft.speed = speed;
  233. if (dmasound.minDev == SND_DEV_DSP)
  234. dmasound.dsp.speed = dmasound.soft.speed;
  235. return dmasound.soft.speed;
  236. }
  237. static int sound_set_stereo(int stereo)
  238. {
  239. if (stereo < 0)
  240. return dmasound.soft.stereo;
  241. stereo = !!stereo; /* should be 0 or 1 now */
  242. dmasound.soft.stereo = stereo;
  243. if (dmasound.minDev == SND_DEV_DSP)
  244. dmasound.dsp.stereo = stereo;
  245. return stereo;
  246. }
  247. static ssize_t sound_copy_translate(TRANS *trans, const u_char __user *userPtr,
  248. size_t userCount, u_char frame[],
  249. ssize_t *frameUsed, ssize_t frameLeft)
  250. {
  251. ssize_t (*ct_func)(const u_char __user *, size_t, u_char *, ssize_t *, ssize_t);
  252. switch (dmasound.soft.format) {
  253. case AFMT_MU_LAW:
  254. ct_func = trans->ct_ulaw;
  255. break;
  256. case AFMT_A_LAW:
  257. ct_func = trans->ct_alaw;
  258. break;
  259. case AFMT_S8:
  260. ct_func = trans->ct_s8;
  261. break;
  262. case AFMT_U8:
  263. ct_func = trans->ct_u8;
  264. break;
  265. case AFMT_S16_BE:
  266. ct_func = trans->ct_s16be;
  267. break;
  268. case AFMT_U16_BE:
  269. ct_func = trans->ct_u16be;
  270. break;
  271. case AFMT_S16_LE:
  272. ct_func = trans->ct_s16le;
  273. break;
  274. case AFMT_U16_LE:
  275. ct_func = trans->ct_u16le;
  276. break;
  277. default:
  278. return 0;
  279. }
  280. /* if the user has requested a non-existent translation don't try
  281. to call it but just return 0 bytes moved
  282. */
  283. if (ct_func)
  284. return ct_func(userPtr, userCount, frame, frameUsed, frameLeft);
  285. return 0;
  286. }
  287. /*
  288. * /dev/mixer abstraction
  289. */
  290. static struct {
  291. int busy;
  292. int modify_counter;
  293. } mixer;
  294. static int mixer_open(struct inode *inode, struct file *file)
  295. {
  296. mutex_lock(&dmasound_core_mutex);
  297. if (!try_module_get(dmasound.mach.owner)) {
  298. mutex_unlock(&dmasound_core_mutex);
  299. return -ENODEV;
  300. }
  301. mixer.busy = 1;
  302. mutex_unlock(&dmasound_core_mutex);
  303. return 0;
  304. }
  305. static int mixer_release(struct inode *inode, struct file *file)
  306. {
  307. mutex_lock(&dmasound_core_mutex);
  308. mixer.busy = 0;
  309. module_put(dmasound.mach.owner);
  310. mutex_unlock(&dmasound_core_mutex);
  311. return 0;
  312. }
  313. static int mixer_ioctl(struct file *file, u_int cmd, u_long arg)
  314. {
  315. if (_SIOC_DIR(cmd) & _SIOC_WRITE)
  316. mixer.modify_counter++;
  317. switch (cmd) {
  318. case OSS_GETVERSION:
  319. return IOCTL_OUT(arg, SOUND_VERSION);
  320. case SOUND_MIXER_INFO:
  321. {
  322. mixer_info info;
  323. memset(&info, 0, sizeof(info));
  324. strlcpy(info.id, dmasound.mach.name2, sizeof(info.id));
  325. strlcpy(info.name, dmasound.mach.name2, sizeof(info.name));
  326. info.modify_counter = mixer.modify_counter;
  327. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  328. return -EFAULT;
  329. return 0;
  330. }
  331. }
  332. if (dmasound.mach.mixer_ioctl)
  333. return dmasound.mach.mixer_ioctl(cmd, arg);
  334. return -EINVAL;
  335. }
  336. static long mixer_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
  337. {
  338. int ret;
  339. mutex_lock(&dmasound_core_mutex);
  340. ret = mixer_ioctl(file, cmd, arg);
  341. mutex_unlock(&dmasound_core_mutex);
  342. return ret;
  343. }
  344. static const struct file_operations mixer_fops =
  345. {
  346. .owner = THIS_MODULE,
  347. .llseek = no_llseek,
  348. .unlocked_ioctl = mixer_unlocked_ioctl,
  349. .open = mixer_open,
  350. .release = mixer_release,
  351. };
  352. static void mixer_init(void)
  353. {
  354. #ifndef MODULE
  355. int mixer_unit;
  356. #endif
  357. mixer_unit = register_sound_mixer(&mixer_fops, -1);
  358. if (mixer_unit < 0)
  359. return;
  360. mixer.busy = 0;
  361. dmasound.treble = 0;
  362. dmasound.bass = 0;
  363. if (dmasound.mach.mixer_init)
  364. dmasound.mach.mixer_init();
  365. }
  366. /*
  367. * Sound queue stuff, the heart of the driver
  368. */
  369. struct sound_queue dmasound_write_sq;
  370. static void sq_reset_output(void) ;
  371. static int sq_allocate_buffers(struct sound_queue *sq, int num, int size)
  372. {
  373. int i;
  374. if (sq->buffers)
  375. return 0;
  376. sq->numBufs = num;
  377. sq->bufSize = size;
  378. sq->buffers = kmalloc (num * sizeof(char *), GFP_KERNEL);
  379. if (!sq->buffers)
  380. return -ENOMEM;
  381. for (i = 0; i < num; i++) {
  382. sq->buffers[i] = dmasound.mach.dma_alloc(size, GFP_KERNEL);
  383. if (!sq->buffers[i]) {
  384. while (i--)
  385. dmasound.mach.dma_free(sq->buffers[i], size);
  386. kfree(sq->buffers);
  387. sq->buffers = NULL;
  388. return -ENOMEM;
  389. }
  390. }
  391. return 0;
  392. }
  393. static void sq_release_buffers(struct sound_queue *sq)
  394. {
  395. int i;
  396. if (sq->buffers) {
  397. for (i = 0; i < sq->numBufs; i++)
  398. dmasound.mach.dma_free(sq->buffers[i], sq->bufSize);
  399. kfree(sq->buffers);
  400. sq->buffers = NULL;
  401. }
  402. }
  403. static int sq_setup(struct sound_queue *sq)
  404. {
  405. int (*setup_func)(void) = NULL;
  406. int hard_frame ;
  407. if (sq->locked) { /* are we already set? - and not changeable */
  408. #ifdef DEBUG_DMASOUND
  409. printk("dmasound_core: tried to sq_setup a locked queue\n") ;
  410. #endif
  411. return -EINVAL ;
  412. }
  413. sq->locked = 1 ; /* don't think we have a race prob. here _check_ */
  414. /* make sure that the parameters are set up
  415. This should have been done already...
  416. */
  417. dmasound.mach.init();
  418. /* OK. If the user has set fragment parameters explicitly, then we
  419. should leave them alone... as long as they are valid.
  420. Invalid user fragment params can occur if we allow the whole buffer
  421. to be used when the user requests the fragments sizes (with no soft
  422. x-lation) and then the user subsequently sets a soft x-lation that
  423. requires increased internal buffering.
  424. Othwerwise (if the user did not set them) OSS says that we should
  425. select frag params on the basis of 0.5 s output & 0.1 s input
  426. latency. (TODO. For now we will copy in the defaults.)
  427. */
  428. if (sq->user_frags <= 0) {
  429. sq->max_count = sq->numBufs ;
  430. sq->max_active = sq->numBufs ;
  431. sq->block_size = sq->bufSize;
  432. /* set up the user info */
  433. sq->user_frags = sq->numBufs ;
  434. sq->user_frag_size = sq->bufSize ;
  435. sq->user_frag_size *=
  436. (dmasound.soft.size * (dmasound.soft.stereo+1) ) ;
  437. sq->user_frag_size /=
  438. (dmasound.hard.size * (dmasound.hard.stereo+1) ) ;
  439. } else {
  440. /* work out requested block size */
  441. sq->block_size = sq->user_frag_size ;
  442. sq->block_size *=
  443. (dmasound.hard.size * (dmasound.hard.stereo+1) ) ;
  444. sq->block_size /=
  445. (dmasound.soft.size * (dmasound.soft.stereo+1) ) ;
  446. /* the user wants to write frag-size chunks */
  447. sq->block_size *= dmasound.hard.speed ;
  448. sq->block_size /= dmasound.soft.speed ;
  449. /* this only works for size values which are powers of 2 */
  450. hard_frame =
  451. (dmasound.hard.size * (dmasound.hard.stereo+1))/8 ;
  452. sq->block_size += (hard_frame - 1) ;
  453. sq->block_size &= ~(hard_frame - 1) ; /* make sure we are aligned */
  454. /* let's just check for obvious mistakes */
  455. if ( sq->block_size <= 0 || sq->block_size > sq->bufSize) {
  456. #ifdef DEBUG_DMASOUND
  457. printk("dmasound_core: invalid frag size (user set %d)\n", sq->user_frag_size) ;
  458. #endif
  459. sq->block_size = sq->bufSize ;
  460. }
  461. if ( sq->user_frags <= sq->numBufs ) {
  462. sq->max_count = sq->user_frags ;
  463. /* if user has set max_active - then use it */
  464. sq->max_active = (sq->max_active <= sq->max_count) ?
  465. sq->max_active : sq->max_count ;
  466. } else {
  467. #ifdef DEBUG_DMASOUND
  468. printk("dmasound_core: invalid frag count (user set %d)\n", sq->user_frags) ;
  469. #endif
  470. sq->max_count =
  471. sq->max_active = sq->numBufs ;
  472. }
  473. }
  474. sq->front = sq->count = sq->rear_size = 0;
  475. sq->syncing = 0;
  476. sq->active = 0;
  477. if (sq == &write_sq) {
  478. sq->rear = -1;
  479. setup_func = dmasound.mach.write_sq_setup;
  480. }
  481. if (setup_func)
  482. return setup_func();
  483. return 0 ;
  484. }
  485. static inline void sq_play(void)
  486. {
  487. dmasound.mach.play();
  488. }
  489. static ssize_t sq_write(struct file *file, const char __user *src, size_t uLeft,
  490. loff_t *ppos)
  491. {
  492. ssize_t uWritten = 0;
  493. u_char *dest;
  494. ssize_t uUsed = 0, bUsed, bLeft;
  495. unsigned long flags ;
  496. /* ++TeSche: Is something like this necessary?
  497. * Hey, that's an honest question! Or does any other part of the
  498. * filesystem already checks this situation? I really don't know.
  499. */
  500. if (uLeft == 0)
  501. return 0;
  502. /* implement any changes we have made to the soft/hard params.
  503. this is not satisfactory really, all we have done up to now is to
  504. say what we would like - there hasn't been any real checking of capability
  505. */
  506. if (shared_resources_initialised == 0) {
  507. dmasound.mach.init() ;
  508. shared_resources_initialised = 1 ;
  509. }
  510. /* set up the sq if it is not already done. This may seem a dumb place
  511. to do it - but it is what OSS requires. It means that write() can
  512. return memory allocation errors. To avoid this possibility use the
  513. GETBLKSIZE or GETOSPACE ioctls (after you've fiddled with all the
  514. params you want to change) - these ioctls also force the setup.
  515. */
  516. if (write_sq.locked == 0) {
  517. if ((uWritten = sq_setup(&write_sq)) < 0) return uWritten ;
  518. uWritten = 0 ;
  519. }
  520. /* FIXME: I think that this may be the wrong behaviour when we get strapped
  521. for time and the cpu is close to being (or actually) behind in sending data.
  522. - because we've lost the time that the N samples, already in the buffer,
  523. would have given us to get here with the next lot from the user.
  524. */
  525. /* The interrupt doesn't start to play the last, incomplete frame.
  526. * Thus we can append to it without disabling the interrupts! (Note
  527. * also that write_sq.rear isn't affected by the interrupt.)
  528. */
  529. /* as of 1.6 this behaviour changes if SNDCTL_DSP_POST has been issued:
  530. this will mimic the behaviour of syncing and allow the sq_play() to
  531. queue a partial fragment. Since sq_play() may/will be called from
  532. the IRQ handler - at least on Pmac we have to deal with it.
  533. The strategy - possibly not optimum - is to kill _POST status if we
  534. get here. This seems, at least, reasonable - in the sense that POST
  535. is supposed to indicate that we might not write before the queue
  536. is drained - and if we get here in time then it does not apply.
  537. */
  538. spin_lock_irqsave(&dmasound.lock, flags);
  539. write_sq.syncing &= ~2 ; /* take out POST status */
  540. spin_unlock_irqrestore(&dmasound.lock, flags);
  541. if (write_sq.count > 0 &&
  542. (bLeft = write_sq.block_size-write_sq.rear_size) > 0) {
  543. dest = write_sq.buffers[write_sq.rear];
  544. bUsed = write_sq.rear_size;
  545. uUsed = sound_copy_translate(dmasound.trans_write, src, uLeft,
  546. dest, &bUsed, bLeft);
  547. if (uUsed <= 0)
  548. return uUsed;
  549. src += uUsed;
  550. uWritten += uUsed;
  551. uLeft = (uUsed <= uLeft) ? (uLeft - uUsed) : 0 ; /* paranoia */
  552. write_sq.rear_size = bUsed;
  553. }
  554. while (uLeft) {
  555. DEFINE_WAIT(wait);
  556. while (write_sq.count >= write_sq.max_active) {
  557. prepare_to_wait(&write_sq.action_queue, &wait, TASK_INTERRUPTIBLE);
  558. sq_play();
  559. if (write_sq.non_blocking) {
  560. finish_wait(&write_sq.action_queue, &wait);
  561. return uWritten > 0 ? uWritten : -EAGAIN;
  562. }
  563. if (write_sq.count < write_sq.max_active)
  564. break;
  565. schedule_timeout(HZ);
  566. if (signal_pending(current)) {
  567. finish_wait(&write_sq.action_queue, &wait);
  568. return uWritten > 0 ? uWritten : -EINTR;
  569. }
  570. }
  571. finish_wait(&write_sq.action_queue, &wait);
  572. /* Here, we can avoid disabling the interrupt by first
  573. * copying and translating the data, and then updating
  574. * the write_sq variables. Until this is done, the interrupt
  575. * won't see the new frame and we can work on it
  576. * undisturbed.
  577. */
  578. dest = write_sq.buffers[(write_sq.rear+1) % write_sq.max_count];
  579. bUsed = 0;
  580. bLeft = write_sq.block_size;
  581. uUsed = sound_copy_translate(dmasound.trans_write, src, uLeft,
  582. dest, &bUsed, bLeft);
  583. if (uUsed <= 0)
  584. break;
  585. src += uUsed;
  586. uWritten += uUsed;
  587. uLeft = (uUsed <= uLeft) ? (uLeft - uUsed) : 0 ; /* paranoia */
  588. if (bUsed) {
  589. write_sq.rear = (write_sq.rear+1) % write_sq.max_count;
  590. write_sq.rear_size = bUsed;
  591. write_sq.count++;
  592. }
  593. } /* uUsed may have been 0 */
  594. sq_play();
  595. return uUsed < 0? uUsed: uWritten;
  596. }
  597. static unsigned int sq_poll(struct file *file, struct poll_table_struct *wait)
  598. {
  599. unsigned int mask = 0;
  600. int retVal;
  601. if (write_sq.locked == 0) {
  602. if ((retVal = sq_setup(&write_sq)) < 0)
  603. return retVal;
  604. return 0;
  605. }
  606. if (file->f_mode & FMODE_WRITE )
  607. poll_wait(file, &write_sq.action_queue, wait);
  608. if (file->f_mode & FMODE_WRITE)
  609. if (write_sq.count < write_sq.max_active || write_sq.block_size - write_sq.rear_size > 0)
  610. mask |= POLLOUT | POLLWRNORM;
  611. return mask;
  612. }
  613. static inline void sq_init_waitqueue(struct sound_queue *sq)
  614. {
  615. init_waitqueue_head(&sq->action_queue);
  616. init_waitqueue_head(&sq->open_queue);
  617. init_waitqueue_head(&sq->sync_queue);
  618. sq->busy = 0;
  619. }
  620. #if 0 /* blocking open() */
  621. static inline void sq_wake_up(struct sound_queue *sq, struct file *file,
  622. fmode_t mode)
  623. {
  624. if (file->f_mode & mode) {
  625. sq->busy = 0; /* CHECK: IS THIS OK??? */
  626. WAKE_UP(sq->open_queue);
  627. }
  628. }
  629. #endif
  630. static int sq_open2(struct sound_queue *sq, struct file *file, fmode_t mode,
  631. int numbufs, int bufsize)
  632. {
  633. int rc = 0;
  634. if (file->f_mode & mode) {
  635. if (sq->busy) {
  636. #if 0 /* blocking open() */
  637. rc = -EBUSY;
  638. if (file->f_flags & O_NONBLOCK)
  639. return rc;
  640. rc = -EINTR;
  641. if (wait_event_interruptible(sq->open_queue, !sq->busy))
  642. return rc;
  643. rc = 0;
  644. #else
  645. /* OSS manual says we will return EBUSY regardless
  646. of O_NOBLOCK.
  647. */
  648. return -EBUSY ;
  649. #endif
  650. }
  651. sq->busy = 1; /* Let's play spot-the-race-condition */
  652. /* allocate the default number & size of buffers.
  653. (i.e. specified in _setup() or as module params)
  654. can't be changed at the moment - but _could_ be perhaps
  655. in the setfragments ioctl.
  656. */
  657. if (( rc = sq_allocate_buffers(sq, numbufs, bufsize))) {
  658. #if 0 /* blocking open() */
  659. sq_wake_up(sq, file, mode);
  660. #else
  661. sq->busy = 0 ;
  662. #endif
  663. return rc;
  664. }
  665. sq->non_blocking = file->f_flags & O_NONBLOCK;
  666. }
  667. return rc;
  668. }
  669. #define write_sq_init_waitqueue() sq_init_waitqueue(&write_sq)
  670. #if 0 /* blocking open() */
  671. #define write_sq_wake_up(file) sq_wake_up(&write_sq, file, FMODE_WRITE)
  672. #endif
  673. #define write_sq_release_buffers() sq_release_buffers(&write_sq)
  674. #define write_sq_open(file) \
  675. sq_open2(&write_sq, file, FMODE_WRITE, numWriteBufs, writeBufSize )
  676. static int sq_open(struct inode *inode, struct file *file)
  677. {
  678. int rc;
  679. mutex_lock(&dmasound_core_mutex);
  680. if (!try_module_get(dmasound.mach.owner)) {
  681. mutex_unlock(&dmasound_core_mutex);
  682. return -ENODEV;
  683. }
  684. rc = write_sq_open(file); /* checks the f_mode */
  685. if (rc)
  686. goto out;
  687. if (file->f_mode & FMODE_READ) {
  688. /* TODO: if O_RDWR, release any resources grabbed by write part */
  689. rc = -ENXIO ; /* I think this is what is required by open(2) */
  690. goto out;
  691. }
  692. if (dmasound.mach.sq_open)
  693. dmasound.mach.sq_open(file->f_mode);
  694. /* CHECK whether this is sensible - in the case that dsp0 could be opened
  695. O_RDONLY and dsp1 could be opened O_WRONLY
  696. */
  697. dmasound.minDev = iminor(inode) & 0x0f;
  698. /* OK. - we should make some attempt at consistency. At least the H'ware
  699. options should be set with a valid mode. We will make it that the LL
  700. driver must supply defaults for hard & soft params.
  701. */
  702. if (shared_resource_owner == 0) {
  703. /* you can make this AFMT_U8/mono/8K if you want to mimic old
  704. OSS behaviour - while we still have soft translations ;-) */
  705. dmasound.soft = dmasound.mach.default_soft ;
  706. dmasound.dsp = dmasound.mach.default_soft ;
  707. dmasound.hard = dmasound.mach.default_hard ;
  708. }
  709. #ifndef DMASOUND_STRICT_OSS_COMPLIANCE
  710. /* none of the current LL drivers can actually do this "native" at the moment
  711. OSS does not really require us to supply /dev/audio if we can't do it.
  712. */
  713. if (dmasound.minDev == SND_DEV_AUDIO) {
  714. sound_set_speed(8000);
  715. sound_set_stereo(0);
  716. sound_set_format(AFMT_MU_LAW);
  717. }
  718. #endif
  719. mutex_unlock(&dmasound_core_mutex);
  720. return 0;
  721. out:
  722. module_put(dmasound.mach.owner);
  723. mutex_unlock(&dmasound_core_mutex);
  724. return rc;
  725. }
  726. static void sq_reset_output(void)
  727. {
  728. sound_silence(); /* this _must_ stop DMA, we might be about to lose the buffers */
  729. write_sq.active = 0;
  730. write_sq.count = 0;
  731. write_sq.rear_size = 0;
  732. /* write_sq.front = (write_sq.rear+1) % write_sq.max_count;*/
  733. write_sq.front = 0 ;
  734. write_sq.rear = -1 ; /* same as for set-up */
  735. /* OK - we can unlock the parameters and fragment settings */
  736. write_sq.locked = 0 ;
  737. write_sq.user_frags = 0 ;
  738. write_sq.user_frag_size = 0 ;
  739. }
  740. static void sq_reset(void)
  741. {
  742. sq_reset_output() ;
  743. /* we could consider resetting the shared_resources_owner here... but I
  744. think it is probably still rather non-obvious to application writer
  745. */
  746. /* we release everything else though */
  747. shared_resources_initialised = 0 ;
  748. }
  749. static int sq_fsync(void)
  750. {
  751. int rc = 0;
  752. int timeout = 5;
  753. write_sq.syncing |= 1;
  754. sq_play(); /* there may be an incomplete frame waiting */
  755. while (write_sq.active) {
  756. wait_event_interruptible_timeout(write_sq.sync_queue,
  757. !write_sq.active, HZ);
  758. if (signal_pending(current)) {
  759. /* While waiting for audio output to drain, an
  760. * interrupt occurred. Stop audio output immediately
  761. * and clear the queue. */
  762. sq_reset_output();
  763. rc = -EINTR;
  764. break;
  765. }
  766. if (!--timeout) {
  767. printk(KERN_WARNING "dmasound: Timeout draining output\n");
  768. sq_reset_output();
  769. rc = -EIO;
  770. break;
  771. }
  772. }
  773. /* flag no sync regardless of whether we had a DSP_POST or not */
  774. write_sq.syncing = 0 ;
  775. return rc;
  776. }
  777. static int sq_release(struct inode *inode, struct file *file)
  778. {
  779. int rc = 0;
  780. mutex_lock(&dmasound_core_mutex);
  781. if (file->f_mode & FMODE_WRITE) {
  782. if (write_sq.busy)
  783. rc = sq_fsync();
  784. sq_reset_output() ; /* make sure dma is stopped and all is quiet */
  785. write_sq_release_buffers();
  786. write_sq.busy = 0;
  787. }
  788. if (file->f_mode & shared_resource_owner) { /* it's us that has them */
  789. shared_resource_owner = 0 ;
  790. shared_resources_initialised = 0 ;
  791. dmasound.hard = dmasound.mach.default_hard ;
  792. }
  793. module_put(dmasound.mach.owner);
  794. #if 0 /* blocking open() */
  795. /* Wake up a process waiting for the queue being released.
  796. * Note: There may be several processes waiting for a call
  797. * to open() returning. */
  798. /* Iain: hmm I don't understand this next comment ... */
  799. /* There is probably a DOS atack here. They change the mode flag. */
  800. /* XXX add check here,*/
  801. read_sq_wake_up(file); /* checks f_mode */
  802. write_sq_wake_up(file); /* checks f_mode */
  803. #endif /* blocking open() */
  804. mutex_unlock(&dmasound_core_mutex);
  805. return rc;
  806. }
  807. /* here we see if we have a right to modify format, channels, size and so on
  808. if no-one else has claimed it already then we do...
  809. TODO: We might change this to mask O_RDWR such that only one or the other channel
  810. is the owner - if we have problems.
  811. */
  812. static int shared_resources_are_mine(fmode_t md)
  813. {
  814. if (shared_resource_owner)
  815. return (shared_resource_owner & md) != 0;
  816. else {
  817. shared_resource_owner = md ;
  818. return 1 ;
  819. }
  820. }
  821. /* if either queue is locked we must deny the right to change shared params
  822. */
  823. static int queues_are_quiescent(void)
  824. {
  825. if (write_sq.locked)
  826. return 0 ;
  827. return 1 ;
  828. }
  829. /* check and set a queue's fragments per user's wishes...
  830. we will check against the pre-defined literals and the actual sizes.
  831. This is a bit fraught - because soft translations can mess with our
  832. buffer requirements *after* this call - OSS says "call setfrags first"
  833. */
  834. /* It is possible to replace all the -EINVAL returns with an override that
  835. just puts the allowable value in. This may be what many OSS apps require
  836. */
  837. static int set_queue_frags(struct sound_queue *sq, int bufs, int size)
  838. {
  839. if (sq->locked) {
  840. #ifdef DEBUG_DMASOUND
  841. printk("dmasound_core: tried to set_queue_frags on a locked queue\n") ;
  842. #endif
  843. return -EINVAL ;
  844. }
  845. if ((size < MIN_FRAG_SIZE) || (size > MAX_FRAG_SIZE))
  846. return -EINVAL ;
  847. size = (1<<size) ; /* now in bytes */
  848. if (size > sq->bufSize)
  849. return -EINVAL ; /* this might still not work */
  850. if (bufs <= 0)
  851. return -EINVAL ;
  852. if (bufs > sq->numBufs) /* the user is allowed say "don't care" with 0x7fff */
  853. bufs = sq->numBufs ;
  854. /* there is, currently, no way to specify max_active separately
  855. from max_count. This could be a LL driver issue - I guess
  856. if there is a requirement for these values to be different then
  857. we will have to pass that info. up to this level.
  858. */
  859. sq->user_frags =
  860. sq->max_active = bufs ;
  861. sq->user_frag_size = size ;
  862. return 0 ;
  863. }
  864. static int sq_ioctl(struct file *file, u_int cmd, u_long arg)
  865. {
  866. int val, result;
  867. u_long fmt;
  868. int data;
  869. int size, nbufs;
  870. audio_buf_info info;
  871. switch (cmd) {
  872. case SNDCTL_DSP_RESET:
  873. sq_reset();
  874. return 0;
  875. break ;
  876. case SNDCTL_DSP_GETFMTS:
  877. fmt = dmasound.mach.hardware_afmts ; /* this is what OSS says.. */
  878. return IOCTL_OUT(arg, fmt);
  879. break ;
  880. case SNDCTL_DSP_GETBLKSIZE:
  881. /* this should tell the caller about bytes that the app can
  882. read/write - the app doesn't care about our internal buffers.
  883. We force sq_setup() here as per OSS 1.1 (which should
  884. compute the values necessary).
  885. Since there is no mechanism to specify read/write separately, for
  886. fds opened O_RDWR, the write_sq values will, arbitrarily, overwrite
  887. the read_sq ones.
  888. */
  889. size = 0 ;
  890. if (file->f_mode & FMODE_WRITE) {
  891. if ( !write_sq.locked )
  892. sq_setup(&write_sq) ;
  893. size = write_sq.user_frag_size ;
  894. }
  895. return IOCTL_OUT(arg, size);
  896. break ;
  897. case SNDCTL_DSP_POST:
  898. /* all we are going to do is to tell the LL that any
  899. partial frags can be queued for output.
  900. The LL will have to clear this flag when last output
  901. is queued.
  902. */
  903. write_sq.syncing |= 0x2 ;
  904. sq_play() ;
  905. return 0 ;
  906. case SNDCTL_DSP_SYNC:
  907. /* This call, effectively, has the same behaviour as SNDCTL_DSP_RESET
  908. except that it waits for output to finish before resetting
  909. everything - read, however, is killed immediately.
  910. */
  911. result = 0 ;
  912. if (file->f_mode & FMODE_WRITE) {
  913. result = sq_fsync();
  914. sq_reset_output() ;
  915. }
  916. /* if we are the shared resource owner then release them */
  917. if (file->f_mode & shared_resource_owner)
  918. shared_resources_initialised = 0 ;
  919. return result ;
  920. break ;
  921. case SOUND_PCM_READ_RATE:
  922. return IOCTL_OUT(arg, dmasound.soft.speed);
  923. case SNDCTL_DSP_SPEED:
  924. /* changing this on the fly will have weird effects on the sound.
  925. Where there are rate conversions implemented in soft form - it
  926. will cause the _ctx_xxx() functions to be substituted.
  927. However, there doesn't appear to be any reason to dis-allow it from
  928. a driver pov.
  929. */
  930. if (shared_resources_are_mine(file->f_mode)) {
  931. IOCTL_IN(arg, data);
  932. data = sound_set_speed(data) ;
  933. shared_resources_initialised = 0 ;
  934. return IOCTL_OUT(arg, data);
  935. } else
  936. return -EINVAL ;
  937. break ;
  938. /* OSS says these next 4 actions are undefined when the device is
  939. busy/active - we will just return -EINVAL.
  940. To be allowed to change one - (a) you have to own the right
  941. (b) the queue(s) must be quiescent
  942. */
  943. case SNDCTL_DSP_STEREO:
  944. if (shared_resources_are_mine(file->f_mode) &&
  945. queues_are_quiescent()) {
  946. IOCTL_IN(arg, data);
  947. shared_resources_initialised = 0 ;
  948. return IOCTL_OUT(arg, sound_set_stereo(data));
  949. } else
  950. return -EINVAL ;
  951. break ;
  952. case SOUND_PCM_WRITE_CHANNELS:
  953. if (shared_resources_are_mine(file->f_mode) &&
  954. queues_are_quiescent()) {
  955. IOCTL_IN(arg, data);
  956. /* the user might ask for 20 channels, we will return 1 or 2 */
  957. shared_resources_initialised = 0 ;
  958. return IOCTL_OUT(arg, sound_set_stereo(data-1)+1);
  959. } else
  960. return -EINVAL ;
  961. break ;
  962. case SNDCTL_DSP_SETFMT:
  963. if (shared_resources_are_mine(file->f_mode) &&
  964. queues_are_quiescent()) {
  965. int format;
  966. IOCTL_IN(arg, data);
  967. shared_resources_initialised = 0 ;
  968. format = sound_set_format(data);
  969. result = IOCTL_OUT(arg, format);
  970. if (result < 0)
  971. return result;
  972. if (format != data && data != AFMT_QUERY)
  973. return -EINVAL;
  974. return 0;
  975. } else
  976. return -EINVAL ;
  977. case SNDCTL_DSP_SUBDIVIDE:
  978. return -EINVAL ;
  979. case SNDCTL_DSP_SETFRAGMENT:
  980. /* we can do this independently for the two queues - with the
  981. proviso that for fds opened O_RDWR we cannot separate the
  982. actions and both queues will be set per the last call.
  983. NOTE: this does *NOT* actually set the queue up - merely
  984. registers our intentions.
  985. */
  986. IOCTL_IN(arg, data);
  987. result = 0 ;
  988. nbufs = (data >> 16) & 0x7fff ; /* 0x7fff is 'use maximum' */
  989. size = data & 0xffff;
  990. if (file->f_mode & FMODE_WRITE) {
  991. result = set_queue_frags(&write_sq, nbufs, size) ;
  992. if (result)
  993. return result ;
  994. }
  995. /* NOTE: this return value is irrelevant - OSS specifically says that
  996. the value is 'random' and that the user _must_ check the actual
  997. frags values using SNDCTL_DSP_GETBLKSIZE or similar */
  998. return IOCTL_OUT(arg, data);
  999. break ;
  1000. case SNDCTL_DSP_GETOSPACE:
  1001. /*
  1002. */
  1003. if (file->f_mode & FMODE_WRITE) {
  1004. if ( !write_sq.locked )
  1005. sq_setup(&write_sq) ;
  1006. info.fragments = write_sq.max_active - write_sq.count;
  1007. info.fragstotal = write_sq.max_active;
  1008. info.fragsize = write_sq.user_frag_size;
  1009. info.bytes = info.fragments * info.fragsize;
  1010. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  1011. return -EFAULT;
  1012. return 0;
  1013. } else
  1014. return -EINVAL ;
  1015. break ;
  1016. case SNDCTL_DSP_GETCAPS:
  1017. val = dmasound.mach.capabilities & 0xffffff00;
  1018. return IOCTL_OUT(arg,val);
  1019. default:
  1020. return mixer_ioctl(file, cmd, arg);
  1021. }
  1022. return -EINVAL;
  1023. }
  1024. static long sq_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
  1025. {
  1026. int ret;
  1027. mutex_lock(&dmasound_core_mutex);
  1028. ret = sq_ioctl(file, cmd, arg);
  1029. mutex_unlock(&dmasound_core_mutex);
  1030. return ret;
  1031. }
  1032. static const struct file_operations sq_fops =
  1033. {
  1034. .owner = THIS_MODULE,
  1035. .llseek = no_llseek,
  1036. .write = sq_write,
  1037. .poll = sq_poll,
  1038. .unlocked_ioctl = sq_unlocked_ioctl,
  1039. .open = sq_open,
  1040. .release = sq_release,
  1041. };
  1042. static int sq_init(void)
  1043. {
  1044. const struct file_operations *fops = &sq_fops;
  1045. #ifndef MODULE
  1046. int sq_unit;
  1047. #endif
  1048. sq_unit = register_sound_dsp(fops, -1);
  1049. if (sq_unit < 0) {
  1050. printk(KERN_ERR "dmasound_core: couldn't register fops\n") ;
  1051. return sq_unit ;
  1052. }
  1053. write_sq_init_waitqueue();
  1054. /* These parameters will be restored for every clean open()
  1055. * in the case of multiple open()s (e.g. dsp0 & dsp1) they
  1056. * will be set so long as the shared resources have no owner.
  1057. */
  1058. if (shared_resource_owner == 0) {
  1059. dmasound.soft = dmasound.mach.default_soft ;
  1060. dmasound.hard = dmasound.mach.default_hard ;
  1061. dmasound.dsp = dmasound.mach.default_soft ;
  1062. shared_resources_initialised = 0 ;
  1063. }
  1064. return 0 ;
  1065. }
  1066. /*
  1067. * /dev/sndstat
  1068. */
  1069. /* we allow more space for record-enabled because there are extra output lines.
  1070. the number here must include the amount we are prepared to give to the low-level
  1071. driver.
  1072. */
  1073. #define STAT_BUFF_LEN 768
  1074. /* this is how much space we will allow the low-level driver to use
  1075. in the stat buffer. Currently, 2 * (80 character line + <NL>).
  1076. We do not police this (it is up to the ll driver to be honest).
  1077. */
  1078. #define LOW_LEVEL_STAT_ALLOC 162
  1079. static struct {
  1080. int busy;
  1081. char buf[STAT_BUFF_LEN]; /* state.buf should not overflow! */
  1082. int len, ptr;
  1083. } state;
  1084. /* publish this function for use by low-level code, if required */
  1085. static char *get_afmt_string(int afmt)
  1086. {
  1087. switch(afmt) {
  1088. case AFMT_MU_LAW:
  1089. return "mu-law";
  1090. break;
  1091. case AFMT_A_LAW:
  1092. return "A-law";
  1093. break;
  1094. case AFMT_U8:
  1095. return "unsigned 8 bit";
  1096. break;
  1097. case AFMT_S8:
  1098. return "signed 8 bit";
  1099. break;
  1100. case AFMT_S16_BE:
  1101. return "signed 16 bit BE";
  1102. break;
  1103. case AFMT_U16_BE:
  1104. return "unsigned 16 bit BE";
  1105. break;
  1106. case AFMT_S16_LE:
  1107. return "signed 16 bit LE";
  1108. break;
  1109. case AFMT_U16_LE:
  1110. return "unsigned 16 bit LE";
  1111. break;
  1112. case 0:
  1113. return "format not set" ;
  1114. break ;
  1115. default:
  1116. break ;
  1117. }
  1118. return "ERROR: Unsupported AFMT_XXXX code" ;
  1119. }
  1120. static int state_open(struct inode *inode, struct file *file)
  1121. {
  1122. char *buffer = state.buf;
  1123. int len = 0;
  1124. int ret;
  1125. mutex_lock(&dmasound_core_mutex);
  1126. ret = -EBUSY;
  1127. if (state.busy)
  1128. goto out;
  1129. ret = -ENODEV;
  1130. if (!try_module_get(dmasound.mach.owner))
  1131. goto out;
  1132. state.ptr = 0;
  1133. state.busy = 1;
  1134. len += sprintf(buffer+len, "%sDMA sound driver rev %03d :\n",
  1135. dmasound.mach.name, (DMASOUND_CORE_REVISION<<4) +
  1136. ((dmasound.mach.version>>8) & 0x0f));
  1137. len += sprintf(buffer+len,
  1138. "Core driver edition %02d.%02d : %s driver edition %02d.%02d\n",
  1139. DMASOUND_CORE_REVISION, DMASOUND_CORE_EDITION, dmasound.mach.name2,
  1140. (dmasound.mach.version >> 8), (dmasound.mach.version & 0xff)) ;
  1141. /* call the low-level module to fill in any stat info. that it has
  1142. if present. Maximum buffer usage is specified.
  1143. */
  1144. if (dmasound.mach.state_info)
  1145. len += dmasound.mach.state_info(buffer+len,
  1146. (size_t) LOW_LEVEL_STAT_ALLOC) ;
  1147. /* make usage of the state buffer as deterministic as poss.
  1148. exceptional conditions could cause overrun - and this is flagged as
  1149. a kernel error.
  1150. */
  1151. /* formats and settings */
  1152. len += sprintf(buffer+len,"\t\t === Formats & settings ===\n") ;
  1153. len += sprintf(buffer+len,"Parameter %20s%20s\n","soft","hard") ;
  1154. len += sprintf(buffer+len,"Format :%20s%20s\n",
  1155. get_afmt_string(dmasound.soft.format),
  1156. get_afmt_string(dmasound.hard.format));
  1157. len += sprintf(buffer+len,"Samp Rate:%14d s/sec%14d s/sec\n",
  1158. dmasound.soft.speed, dmasound.hard.speed);
  1159. len += sprintf(buffer+len,"Channels :%20s%20s\n",
  1160. dmasound.soft.stereo ? "stereo" : "mono",
  1161. dmasound.hard.stereo ? "stereo" : "mono" );
  1162. /* sound queue status */
  1163. len += sprintf(buffer+len,"\t\t === Sound Queue status ===\n");
  1164. len += sprintf(buffer+len,"Allocated:%8s%6s\n","Buffers","Size") ;
  1165. len += sprintf(buffer+len,"%9s:%8d%6d\n",
  1166. "write", write_sq.numBufs, write_sq.bufSize) ;
  1167. len += sprintf(buffer+len,
  1168. "Current : MaxFrg FragSiz MaxAct Frnt Rear "
  1169. "Cnt RrSize A B S L xruns\n") ;
  1170. len += sprintf(buffer+len,"%9s:%7d%8d%7d%5d%5d%4d%7d%2d%2d%2d%2d%7d\n",
  1171. "write", write_sq.max_count, write_sq.block_size,
  1172. write_sq.max_active, write_sq.front, write_sq.rear,
  1173. write_sq.count, write_sq.rear_size, write_sq.active,
  1174. write_sq.busy, write_sq.syncing, write_sq.locked, write_sq.xruns) ;
  1175. #ifdef DEBUG_DMASOUND
  1176. printk("dmasound: stat buffer used %d bytes\n", len) ;
  1177. #endif
  1178. if (len >= STAT_BUFF_LEN)
  1179. printk(KERN_ERR "dmasound_core: stat buffer overflowed!\n");
  1180. state.len = len;
  1181. ret = 0;
  1182. out:
  1183. mutex_unlock(&dmasound_core_mutex);
  1184. return ret;
  1185. }
  1186. static int state_release(struct inode *inode, struct file *file)
  1187. {
  1188. mutex_lock(&dmasound_core_mutex);
  1189. state.busy = 0;
  1190. module_put(dmasound.mach.owner);
  1191. mutex_unlock(&dmasound_core_mutex);
  1192. return 0;
  1193. }
  1194. static ssize_t state_read(struct file *file, char __user *buf, size_t count,
  1195. loff_t *ppos)
  1196. {
  1197. int n = state.len - state.ptr;
  1198. if (n > count)
  1199. n = count;
  1200. if (n <= 0)
  1201. return 0;
  1202. if (copy_to_user(buf, &state.buf[state.ptr], n))
  1203. return -EFAULT;
  1204. state.ptr += n;
  1205. return n;
  1206. }
  1207. static const struct file_operations state_fops = {
  1208. .owner = THIS_MODULE,
  1209. .llseek = no_llseek,
  1210. .read = state_read,
  1211. .open = state_open,
  1212. .release = state_release,
  1213. };
  1214. static int state_init(void)
  1215. {
  1216. #ifndef MODULE
  1217. int state_unit;
  1218. #endif
  1219. state_unit = register_sound_special(&state_fops, SND_DEV_STATUS);
  1220. if (state_unit < 0)
  1221. return state_unit ;
  1222. state.busy = 0;
  1223. return 0 ;
  1224. }
  1225. /*
  1226. * Config & Setup
  1227. *
  1228. * This function is called by _one_ chipset-specific driver
  1229. */
  1230. int dmasound_init(void)
  1231. {
  1232. int res ;
  1233. #ifdef MODULE
  1234. if (irq_installed)
  1235. return -EBUSY;
  1236. #endif
  1237. /* Set up sound queue, /dev/audio and /dev/dsp. */
  1238. /* Set default settings. */
  1239. if ((res = sq_init()) < 0)
  1240. return res ;
  1241. /* Set up /dev/sndstat. */
  1242. if ((res = state_init()) < 0)
  1243. return res ;
  1244. /* Set up /dev/mixer. */
  1245. mixer_init();
  1246. if (!dmasound.mach.irqinit()) {
  1247. printk(KERN_ERR "DMA sound driver: Interrupt initialization failed\n");
  1248. return -ENODEV;
  1249. }
  1250. #ifdef MODULE
  1251. irq_installed = 1;
  1252. #endif
  1253. printk(KERN_INFO "%s DMA sound driver rev %03d installed\n",
  1254. dmasound.mach.name, (DMASOUND_CORE_REVISION<<4) +
  1255. ((dmasound.mach.version>>8) & 0x0f));
  1256. printk(KERN_INFO
  1257. "Core driver edition %02d.%02d : %s driver edition %02d.%02d\n",
  1258. DMASOUND_CORE_REVISION, DMASOUND_CORE_EDITION, dmasound.mach.name2,
  1259. (dmasound.mach.version >> 8), (dmasound.mach.version & 0xff)) ;
  1260. printk(KERN_INFO "Write will use %4d fragments of %7d bytes as default\n",
  1261. numWriteBufs, writeBufSize) ;
  1262. return 0;
  1263. }
  1264. #ifdef MODULE
  1265. void dmasound_deinit(void)
  1266. {
  1267. if (irq_installed) {
  1268. sound_silence();
  1269. dmasound.mach.irqcleanup();
  1270. irq_installed = 0;
  1271. }
  1272. write_sq_release_buffers();
  1273. if (mixer_unit >= 0)
  1274. unregister_sound_mixer(mixer_unit);
  1275. if (state_unit >= 0)
  1276. unregister_sound_special(state_unit);
  1277. if (sq_unit >= 0)
  1278. unregister_sound_dsp(sq_unit);
  1279. }
  1280. #else /* !MODULE */
  1281. static int dmasound_setup(char *str)
  1282. {
  1283. int ints[6], size;
  1284. str = get_options(str, ARRAY_SIZE(ints), ints);
  1285. /* check the bootstrap parameter for "dmasound=" */
  1286. /* FIXME: other than in the most naive of cases there is no sense in these
  1287. * buffers being other than powers of two. This is not checked yet.
  1288. */
  1289. switch (ints[0]) {
  1290. case 3:
  1291. if ((ints[3] < 0) || (ints[3] > MAX_CATCH_RADIUS))
  1292. printk("dmasound_setup: invalid catch radius, using default = %d\n", catchRadius);
  1293. else
  1294. catchRadius = ints[3];
  1295. /* fall through */
  1296. case 2:
  1297. if (ints[1] < MIN_BUFFERS)
  1298. printk("dmasound_setup: invalid number of buffers, using default = %d\n", numWriteBufs);
  1299. else
  1300. numWriteBufs = ints[1];
  1301. /* fall through */
  1302. case 1:
  1303. if ((size = ints[2]) < 256) /* check for small buffer specs */
  1304. size <<= 10 ;
  1305. if (size < MIN_BUFSIZE || size > MAX_BUFSIZE)
  1306. printk("dmasound_setup: invalid write buffer size, using default = %d\n", writeBufSize);
  1307. else
  1308. writeBufSize = size;
  1309. case 0:
  1310. break;
  1311. default:
  1312. printk("dmasound_setup: invalid number of arguments\n");
  1313. return 0;
  1314. }
  1315. return 1;
  1316. }
  1317. __setup("dmasound=", dmasound_setup);
  1318. #endif /* !MODULE */
  1319. /*
  1320. * Conversion tables
  1321. */
  1322. #ifdef HAS_8BIT_TABLES
  1323. /* 8 bit mu-law */
  1324. char dmasound_ulaw2dma8[] = {
  1325. -126, -122, -118, -114, -110, -106, -102, -98,
  1326. -94, -90, -86, -82, -78, -74, -70, -66,
  1327. -63, -61, -59, -57, -55, -53, -51, -49,
  1328. -47, -45, -43, -41, -39, -37, -35, -33,
  1329. -31, -30, -29, -28, -27, -26, -25, -24,
  1330. -23, -22, -21, -20, -19, -18, -17, -16,
  1331. -16, -15, -15, -14, -14, -13, -13, -12,
  1332. -12, -11, -11, -10, -10, -9, -9, -8,
  1333. -8, -8, -7, -7, -7, -7, -6, -6,
  1334. -6, -6, -5, -5, -5, -5, -4, -4,
  1335. -4, -4, -4, -4, -3, -3, -3, -3,
  1336. -3, -3, -3, -3, -2, -2, -2, -2,
  1337. -2, -2, -2, -2, -2, -2, -2, -2,
  1338. -1, -1, -1, -1, -1, -1, -1, -1,
  1339. -1, -1, -1, -1, -1, -1, -1, -1,
  1340. -1, -1, -1, -1, -1, -1, -1, 0,
  1341. 125, 121, 117, 113, 109, 105, 101, 97,
  1342. 93, 89, 85, 81, 77, 73, 69, 65,
  1343. 62, 60, 58, 56, 54, 52, 50, 48,
  1344. 46, 44, 42, 40, 38, 36, 34, 32,
  1345. 30, 29, 28, 27, 26, 25, 24, 23,
  1346. 22, 21, 20, 19, 18, 17, 16, 15,
  1347. 15, 14, 14, 13, 13, 12, 12, 11,
  1348. 11, 10, 10, 9, 9, 8, 8, 7,
  1349. 7, 7, 6, 6, 6, 6, 5, 5,
  1350. 5, 5, 4, 4, 4, 4, 3, 3,
  1351. 3, 3, 3, 3, 2, 2, 2, 2,
  1352. 2, 2, 2, 2, 1, 1, 1, 1,
  1353. 1, 1, 1, 1, 1, 1, 1, 1,
  1354. 0, 0, 0, 0, 0, 0, 0, 0,
  1355. 0, 0, 0, 0, 0, 0, 0, 0,
  1356. 0, 0, 0, 0, 0, 0, 0, 0
  1357. };
  1358. /* 8 bit A-law */
  1359. char dmasound_alaw2dma8[] = {
  1360. -22, -21, -24, -23, -18, -17, -20, -19,
  1361. -30, -29, -32, -31, -26, -25, -28, -27,
  1362. -11, -11, -12, -12, -9, -9, -10, -10,
  1363. -15, -15, -16, -16, -13, -13, -14, -14,
  1364. -86, -82, -94, -90, -70, -66, -78, -74,
  1365. -118, -114, -126, -122, -102, -98, -110, -106,
  1366. -43, -41, -47, -45, -35, -33, -39, -37,
  1367. -59, -57, -63, -61, -51, -49, -55, -53,
  1368. -2, -2, -2, -2, -2, -2, -2, -2,
  1369. -2, -2, -2, -2, -2, -2, -2, -2,
  1370. -1, -1, -1, -1, -1, -1, -1, -1,
  1371. -1, -1, -1, -1, -1, -1, -1, -1,
  1372. -6, -6, -6, -6, -5, -5, -5, -5,
  1373. -8, -8, -8, -8, -7, -7, -7, -7,
  1374. -3, -3, -3, -3, -3, -3, -3, -3,
  1375. -4, -4, -4, -4, -4, -4, -4, -4,
  1376. 21, 20, 23, 22, 17, 16, 19, 18,
  1377. 29, 28, 31, 30, 25, 24, 27, 26,
  1378. 10, 10, 11, 11, 8, 8, 9, 9,
  1379. 14, 14, 15, 15, 12, 12, 13, 13,
  1380. 86, 82, 94, 90, 70, 66, 78, 74,
  1381. 118, 114, 126, 122, 102, 98, 110, 106,
  1382. 43, 41, 47, 45, 35, 33, 39, 37,
  1383. 59, 57, 63, 61, 51, 49, 55, 53,
  1384. 1, 1, 1, 1, 1, 1, 1, 1,
  1385. 1, 1, 1, 1, 1, 1, 1, 1,
  1386. 0, 0, 0, 0, 0, 0, 0, 0,
  1387. 0, 0, 0, 0, 0, 0, 0, 0,
  1388. 5, 5, 5, 5, 4, 4, 4, 4,
  1389. 7, 7, 7, 7, 6, 6, 6, 6,
  1390. 2, 2, 2, 2, 2, 2, 2, 2,
  1391. 3, 3, 3, 3, 3, 3, 3, 3
  1392. };
  1393. #endif /* HAS_8BIT_TABLES */
  1394. /*
  1395. * Visible symbols for modules
  1396. */
  1397. EXPORT_SYMBOL(dmasound);
  1398. EXPORT_SYMBOL(dmasound_init);
  1399. #ifdef MODULE
  1400. EXPORT_SYMBOL(dmasound_deinit);
  1401. #endif
  1402. EXPORT_SYMBOL(dmasound_write_sq);
  1403. EXPORT_SYMBOL(dmasound_catchRadius);
  1404. #ifdef HAS_8BIT_TABLES
  1405. EXPORT_SYMBOL(dmasound_ulaw2dma8);
  1406. EXPORT_SYMBOL(dmasound_alaw2dma8);
  1407. #endif