dmasound_core.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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