sound_core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * Sound core. This file is composed of two parts. sound_class
  3. * which is common to both OSS and ALSA and OSS sound core which
  4. * is used OSS or emulation of it.
  5. */
  6. /*
  7. * First, the common part.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/kdev_t.h>
  13. #include <linux/major.h>
  14. #include <sound/core.h>
  15. #ifdef CONFIG_SOUND_OSS_CORE
  16. static int __init init_oss_soundcore(void);
  17. static void cleanup_oss_soundcore(void);
  18. #else
  19. static inline int init_oss_soundcore(void) { return 0; }
  20. static inline void cleanup_oss_soundcore(void) { }
  21. #endif
  22. struct class *sound_class;
  23. EXPORT_SYMBOL(sound_class);
  24. MODULE_DESCRIPTION("Core sound module");
  25. MODULE_AUTHOR("Alan Cox");
  26. MODULE_LICENSE("GPL");
  27. static char *sound_devnode(struct device *dev, umode_t *mode)
  28. {
  29. if (MAJOR(dev->devt) == SOUND_MAJOR)
  30. return NULL;
  31. return kasprintf(GFP_KERNEL, "snd/%s", dev_name(dev));
  32. }
  33. static int __init init_soundcore(void)
  34. {
  35. int rc;
  36. rc = init_oss_soundcore();
  37. if (rc)
  38. return rc;
  39. sound_class = class_create(THIS_MODULE, "sound");
  40. if (IS_ERR(sound_class)) {
  41. cleanup_oss_soundcore();
  42. return PTR_ERR(sound_class);
  43. }
  44. sound_class->devnode = sound_devnode;
  45. return 0;
  46. }
  47. static void __exit cleanup_soundcore(void)
  48. {
  49. cleanup_oss_soundcore();
  50. class_destroy(sound_class);
  51. }
  52. subsys_initcall(init_soundcore);
  53. module_exit(cleanup_soundcore);
  54. #ifdef CONFIG_SOUND_OSS_CORE
  55. /*
  56. * OSS sound core handling. Breaks out sound functions to submodules
  57. *
  58. * Author: Alan Cox <alan@lxorguk.ukuu.org.uk>
  59. *
  60. * Fixes:
  61. *
  62. *
  63. * This program is free software; you can redistribute it and/or
  64. * modify it under the terms of the GNU General Public License
  65. * as published by the Free Software Foundation; either version
  66. * 2 of the License, or (at your option) any later version.
  67. *
  68. * --------------------
  69. *
  70. * Top level handler for the sound subsystem. Various devices can
  71. * plug into this. The fact they don't all go via OSS doesn't mean
  72. * they don't have to implement the OSS API. There is a lot of logic
  73. * to keeping much of the OSS weight out of the code in a compatibility
  74. * module, but it's up to the driver to rember to load it...
  75. *
  76. * The code provides a set of functions for registration of devices
  77. * by type. This is done rather than providing a single call so that
  78. * we can hide any future changes in the internals (eg when we go to
  79. * 32bit dev_t) from the modules and their interface.
  80. *
  81. * Secondly we need to allocate the dsp, dsp16 and audio devices as
  82. * one. Thus we misuse the chains a bit to simplify this.
  83. *
  84. * Thirdly to make it more fun and for 2.3.x and above we do all
  85. * of this using fine grained locking.
  86. *
  87. * FIXME: we have to resolve modules and fine grained load/unload
  88. * locking at some point in 2.3.x.
  89. */
  90. #include <linux/init.h>
  91. #include <linux/slab.h>
  92. #include <linux/types.h>
  93. #include <linux/kernel.h>
  94. #include <linux/sound.h>
  95. #include <linux/kmod.h>
  96. #define SOUND_STEP 16
  97. struct sound_unit
  98. {
  99. int unit_minor;
  100. const struct file_operations *unit_fops;
  101. struct sound_unit *next;
  102. char name[32];
  103. };
  104. #ifdef CONFIG_SOUND_MSNDCLAS
  105. extern int msnd_classic_init(void);
  106. #endif
  107. #ifdef CONFIG_SOUND_MSNDPIN
  108. extern int msnd_pinnacle_init(void);
  109. #endif
  110. /*
  111. * By default, OSS sound_core claims full legacy minor range (0-255)
  112. * of SOUND_MAJOR to trap open attempts to any sound minor and
  113. * requests modules using custom sound-slot/service-* module aliases.
  114. * The only benefit of doing this is allowing use of custom module
  115. * aliases instead of the standard char-major-* ones. This behavior
  116. * prevents alternative OSS implementation and is scheduled to be
  117. * removed.
  118. *
  119. * CONFIG_SOUND_OSS_CORE_PRECLAIM and soundcore.preclaim_oss kernel
  120. * parameter are added to allow distros and developers to try and
  121. * switch to alternative implementations without needing to rebuild
  122. * the kernel in the meantime. If preclaim_oss is non-zero, the
  123. * kernel will behave the same as before. All SOUND_MAJOR minors are
  124. * preclaimed and the custom module aliases along with standard chrdev
  125. * ones are emitted if a missing device is opened. If preclaim_oss is
  126. * zero, sound_core only grabs what's actually in use and for missing
  127. * devices only the standard chrdev aliases are requested.
  128. *
  129. * All these clutters are scheduled to be removed along with
  130. * sound-slot/service-* module aliases.
  131. */
  132. #ifdef CONFIG_SOUND_OSS_CORE_PRECLAIM
  133. static int preclaim_oss = 1;
  134. #else
  135. static int preclaim_oss = 0;
  136. #endif
  137. module_param(preclaim_oss, int, 0444);
  138. static int soundcore_open(struct inode *, struct file *);
  139. static const struct file_operations soundcore_fops =
  140. {
  141. /* We must have an owner or the module locking fails */
  142. .owner = THIS_MODULE,
  143. .open = soundcore_open,
  144. .llseek = noop_llseek,
  145. };
  146. /*
  147. * Low level list operator. Scan the ordered list, find a hole and
  148. * join into it. Called with the lock asserted
  149. */
  150. static int __sound_insert_unit(struct sound_unit * s, struct sound_unit **list, const struct file_operations *fops, int index, int low, int top)
  151. {
  152. int n=low;
  153. if (index < 0) { /* first free */
  154. while (*list && (*list)->unit_minor<n)
  155. list=&((*list)->next);
  156. while(n<top)
  157. {
  158. /* Found a hole ? */
  159. if(*list==NULL || (*list)->unit_minor>n)
  160. break;
  161. list=&((*list)->next);
  162. n+=SOUND_STEP;
  163. }
  164. if(n>=top)
  165. return -ENOENT;
  166. } else {
  167. n = low+(index*16);
  168. while (*list) {
  169. if ((*list)->unit_minor==n)
  170. return -EBUSY;
  171. if ((*list)->unit_minor>n)
  172. break;
  173. list=&((*list)->next);
  174. }
  175. }
  176. /*
  177. * Fill it in
  178. */
  179. s->unit_minor=n;
  180. s->unit_fops=fops;
  181. /*
  182. * Link it
  183. */
  184. s->next=*list;
  185. *list=s;
  186. return n;
  187. }
  188. /*
  189. * Remove a node from the chain. Called with the lock asserted
  190. */
  191. static struct sound_unit *__sound_remove_unit(struct sound_unit **list, int unit)
  192. {
  193. while(*list)
  194. {
  195. struct sound_unit *p=*list;
  196. if(p->unit_minor==unit)
  197. {
  198. *list=p->next;
  199. return p;
  200. }
  201. list=&(p->next);
  202. }
  203. printk(KERN_ERR "Sound device %d went missing!\n", unit);
  204. return NULL;
  205. }
  206. /*
  207. * This lock guards the sound loader list.
  208. */
  209. static DEFINE_SPINLOCK(sound_loader_lock);
  210. /*
  211. * Allocate the controlling structure and add it to the sound driver
  212. * list. Acquires locks as needed
  213. */
  214. static int sound_insert_unit(struct sound_unit **list, const struct file_operations *fops, int index, int low, int top, const char *name, umode_t mode, struct device *dev)
  215. {
  216. struct sound_unit *s = kmalloc(sizeof(*s), GFP_KERNEL);
  217. int r;
  218. if (!s)
  219. return -ENOMEM;
  220. spin_lock(&sound_loader_lock);
  221. retry:
  222. r = __sound_insert_unit(s, list, fops, index, low, top);
  223. spin_unlock(&sound_loader_lock);
  224. if (r < 0)
  225. goto fail;
  226. else if (r < SOUND_STEP)
  227. sprintf(s->name, "sound/%s", name);
  228. else
  229. sprintf(s->name, "sound/%s%d", name, r / SOUND_STEP);
  230. if (!preclaim_oss) {
  231. /*
  232. * Something else might have grabbed the minor. If
  233. * first free slot is requested, rescan with @low set
  234. * to the next unit; otherwise, -EBUSY.
  235. */
  236. r = __register_chrdev(SOUND_MAJOR, s->unit_minor, 1, s->name,
  237. &soundcore_fops);
  238. if (r < 0) {
  239. spin_lock(&sound_loader_lock);
  240. __sound_remove_unit(list, s->unit_minor);
  241. if (index < 0) {
  242. low = s->unit_minor + SOUND_STEP;
  243. goto retry;
  244. }
  245. spin_unlock(&sound_loader_lock);
  246. return -EBUSY;
  247. }
  248. }
  249. device_create(sound_class, dev, MKDEV(SOUND_MAJOR, s->unit_minor),
  250. NULL, "%s", s->name+6);
  251. return s->unit_minor;
  252. fail:
  253. kfree(s);
  254. return r;
  255. }
  256. /*
  257. * Remove a unit. Acquires locks as needed. The drivers MUST have
  258. * completed the removal before their file operations become
  259. * invalid.
  260. */
  261. static void sound_remove_unit(struct sound_unit **list, int unit)
  262. {
  263. struct sound_unit *p;
  264. spin_lock(&sound_loader_lock);
  265. p = __sound_remove_unit(list, unit);
  266. spin_unlock(&sound_loader_lock);
  267. if (p) {
  268. if (!preclaim_oss)
  269. __unregister_chrdev(SOUND_MAJOR, p->unit_minor, 1,
  270. p->name);
  271. device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
  272. kfree(p);
  273. }
  274. }
  275. /*
  276. * Allocations
  277. *
  278. * 0 *16 Mixers
  279. * 1 *8 Sequencers
  280. * 2 *16 Midi
  281. * 3 *16 DSP
  282. * 4 *16 SunDSP
  283. * 5 *16 DSP16
  284. * 6 -- sndstat (obsolete)
  285. * 7 *16 unused
  286. * 8 -- alternate sequencer (see above)
  287. * 9 *16 raw synthesizer access
  288. * 10 *16 unused
  289. * 11 *16 unused
  290. * 12 *16 unused
  291. * 13 *16 unused
  292. * 14 *16 unused
  293. * 15 *16 unused
  294. */
  295. static struct sound_unit *chains[SOUND_STEP];
  296. /**
  297. * register_sound_special_device - register a special sound node
  298. * @fops: File operations for the driver
  299. * @unit: Unit number to allocate
  300. * @dev: device pointer
  301. *
  302. * Allocate a special sound device by minor number from the sound
  303. * subsystem.
  304. *
  305. * Return: The allocated number is returned on success. On failure,
  306. * a negative error code is returned.
  307. */
  308. int register_sound_special_device(const struct file_operations *fops, int unit,
  309. struct device *dev)
  310. {
  311. const int chain = unit % SOUND_STEP;
  312. int max_unit = 256;
  313. const char *name;
  314. char _name[16];
  315. switch (chain) {
  316. case 0:
  317. name = "mixer";
  318. break;
  319. case 1:
  320. name = "sequencer";
  321. if (unit >= SOUND_STEP)
  322. goto __unknown;
  323. max_unit = unit + 1;
  324. break;
  325. case 2:
  326. name = "midi";
  327. break;
  328. case 3:
  329. name = "dsp";
  330. break;
  331. case 4:
  332. name = "audio";
  333. break;
  334. case 5:
  335. name = "dspW";
  336. break;
  337. case 8:
  338. name = "sequencer2";
  339. if (unit >= SOUND_STEP)
  340. goto __unknown;
  341. max_unit = unit + 1;
  342. break;
  343. case 9:
  344. name = "dmmidi";
  345. break;
  346. case 10:
  347. name = "dmfm";
  348. break;
  349. case 12:
  350. name = "adsp";
  351. break;
  352. case 13:
  353. name = "amidi";
  354. break;
  355. case 14:
  356. name = "admmidi";
  357. break;
  358. default:
  359. {
  360. __unknown:
  361. sprintf(_name, "unknown%d", chain);
  362. if (unit >= SOUND_STEP)
  363. strcat(_name, "-");
  364. name = _name;
  365. }
  366. break;
  367. }
  368. return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
  369. name, S_IRUSR | S_IWUSR, dev);
  370. }
  371. EXPORT_SYMBOL(register_sound_special_device);
  372. int register_sound_special(const struct file_operations *fops, int unit)
  373. {
  374. return register_sound_special_device(fops, unit, NULL);
  375. }
  376. EXPORT_SYMBOL(register_sound_special);
  377. /**
  378. * register_sound_mixer - register a mixer device
  379. * @fops: File operations for the driver
  380. * @dev: Unit number to allocate
  381. *
  382. * Allocate a mixer device. Unit is the number of the mixer requested.
  383. * Pass -1 to request the next free mixer unit.
  384. *
  385. * Return: On success, the allocated number is returned. On failure,
  386. * a negative error code is returned.
  387. */
  388. int register_sound_mixer(const struct file_operations *fops, int dev)
  389. {
  390. return sound_insert_unit(&chains[0], fops, dev, 0, 128,
  391. "mixer", S_IRUSR | S_IWUSR, NULL);
  392. }
  393. EXPORT_SYMBOL(register_sound_mixer);
  394. /**
  395. * register_sound_midi - register a midi device
  396. * @fops: File operations for the driver
  397. * @dev: Unit number to allocate
  398. *
  399. * Allocate a midi device. Unit is the number of the midi device requested.
  400. * Pass -1 to request the next free midi unit.
  401. *
  402. * Return: On success, the allocated number is returned. On failure,
  403. * a negative error code is returned.
  404. */
  405. int register_sound_midi(const struct file_operations *fops, int dev)
  406. {
  407. return sound_insert_unit(&chains[2], fops, dev, 2, 130,
  408. "midi", S_IRUSR | S_IWUSR, NULL);
  409. }
  410. EXPORT_SYMBOL(register_sound_midi);
  411. /*
  412. * DSP's are registered as a triple. Register only one and cheat
  413. * in open - see below.
  414. */
  415. /**
  416. * register_sound_dsp - register a DSP device
  417. * @fops: File operations for the driver
  418. * @dev: Unit number to allocate
  419. *
  420. * Allocate a DSP device. Unit is the number of the DSP requested.
  421. * Pass -1 to request the next free DSP unit.
  422. *
  423. * This function allocates both the audio and dsp device entries together
  424. * and will always allocate them as a matching pair - eg dsp3/audio3
  425. *
  426. * Return: On success, the allocated number is returned. On failure,
  427. * a negative error code is returned.
  428. */
  429. int register_sound_dsp(const struct file_operations *fops, int dev)
  430. {
  431. return sound_insert_unit(&chains[3], fops, dev, 3, 131,
  432. "dsp", S_IWUSR | S_IRUSR, NULL);
  433. }
  434. EXPORT_SYMBOL(register_sound_dsp);
  435. /**
  436. * unregister_sound_special - unregister a special sound device
  437. * @unit: unit number to allocate
  438. *
  439. * Release a sound device that was allocated with
  440. * register_sound_special(). The unit passed is the return value from
  441. * the register function.
  442. */
  443. void unregister_sound_special(int unit)
  444. {
  445. sound_remove_unit(&chains[unit % SOUND_STEP], unit);
  446. }
  447. EXPORT_SYMBOL(unregister_sound_special);
  448. /**
  449. * unregister_sound_mixer - unregister a mixer
  450. * @unit: unit number to allocate
  451. *
  452. * Release a sound device that was allocated with register_sound_mixer().
  453. * The unit passed is the return value from the register function.
  454. */
  455. void unregister_sound_mixer(int unit)
  456. {
  457. sound_remove_unit(&chains[0], unit);
  458. }
  459. EXPORT_SYMBOL(unregister_sound_mixer);
  460. /**
  461. * unregister_sound_midi - unregister a midi device
  462. * @unit: unit number to allocate
  463. *
  464. * Release a sound device that was allocated with register_sound_midi().
  465. * The unit passed is the return value from the register function.
  466. */
  467. void unregister_sound_midi(int unit)
  468. {
  469. sound_remove_unit(&chains[2], unit);
  470. }
  471. EXPORT_SYMBOL(unregister_sound_midi);
  472. /**
  473. * unregister_sound_dsp - unregister a DSP device
  474. * @unit: unit number to allocate
  475. *
  476. * Release a sound device that was allocated with register_sound_dsp().
  477. * The unit passed is the return value from the register function.
  478. *
  479. * Both of the allocated units are released together automatically.
  480. */
  481. void unregister_sound_dsp(int unit)
  482. {
  483. sound_remove_unit(&chains[3], unit);
  484. }
  485. EXPORT_SYMBOL(unregister_sound_dsp);
  486. static struct sound_unit *__look_for_unit(int chain, int unit)
  487. {
  488. struct sound_unit *s;
  489. s=chains[chain];
  490. while(s && s->unit_minor <= unit)
  491. {
  492. if(s->unit_minor==unit)
  493. return s;
  494. s=s->next;
  495. }
  496. return NULL;
  497. }
  498. static int soundcore_open(struct inode *inode, struct file *file)
  499. {
  500. int chain;
  501. int unit = iminor(inode);
  502. struct sound_unit *s;
  503. const struct file_operations *new_fops = NULL;
  504. chain=unit&0x0F;
  505. if(chain==4 || chain==5) /* dsp/audio/dsp16 */
  506. {
  507. unit&=0xF0;
  508. unit|=3;
  509. chain=3;
  510. }
  511. spin_lock(&sound_loader_lock);
  512. s = __look_for_unit(chain, unit);
  513. if (s)
  514. new_fops = fops_get(s->unit_fops);
  515. if (preclaim_oss && !new_fops) {
  516. spin_unlock(&sound_loader_lock);
  517. /*
  518. * Please, don't change this order or code.
  519. * For ALSA slot means soundcard and OSS emulation code
  520. * comes as add-on modules which aren't depend on
  521. * ALSA toplevel modules for soundcards, thus we need
  522. * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
  523. */
  524. request_module("sound-slot-%i", unit>>4);
  525. request_module("sound-service-%i-%i", unit>>4, chain);
  526. /*
  527. * sound-slot/service-* module aliases are scheduled
  528. * for removal in favor of the standard char-major-*
  529. * module aliases. For the time being, generate both
  530. * the legacy and standard module aliases to ease
  531. * transition.
  532. */
  533. if (request_module("char-major-%d-%d", SOUND_MAJOR, unit) > 0)
  534. request_module("char-major-%d", SOUND_MAJOR);
  535. spin_lock(&sound_loader_lock);
  536. s = __look_for_unit(chain, unit);
  537. if (s)
  538. new_fops = fops_get(s->unit_fops);
  539. }
  540. spin_unlock(&sound_loader_lock);
  541. if (new_fops) {
  542. /*
  543. * We rely upon the fact that we can't be unloaded while the
  544. * subdriver is there.
  545. */
  546. int err = 0;
  547. replace_fops(file, new_fops);
  548. if (file->f_op->open)
  549. err = file->f_op->open(inode,file);
  550. return err;
  551. }
  552. return -ENODEV;
  553. }
  554. MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
  555. static void cleanup_oss_soundcore(void)
  556. {
  557. /* We have nothing to really do here - we know the lists must be
  558. empty */
  559. unregister_chrdev(SOUND_MAJOR, "sound");
  560. }
  561. static int __init init_oss_soundcore(void)
  562. {
  563. if (preclaim_oss &&
  564. register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops) < 0) {
  565. printk(KERN_ERR "soundcore: sound device already in use.\n");
  566. return -EBUSY;
  567. }
  568. return 0;
  569. }
  570. #endif /* CONFIG_SOUND_OSS_CORE */