dmasound_core.c 45 KB

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