sound_core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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, mode_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. Please take a look at
  131. * feature-removal-schedule.txt for details.
  132. */
  133. #ifdef CONFIG_SOUND_OSS_CORE_PRECLAIM
  134. static int preclaim_oss = 1;
  135. #else
  136. static int preclaim_oss = 0;
  137. #endif
  138. module_param(preclaim_oss, int, 0444);
  139. static int soundcore_open(struct inode *, struct file *);
  140. static const struct file_operations soundcore_fops =
  141. {
  142. /* We must have an owner or the module locking fails */
  143. .owner = THIS_MODULE,
  144. .open = soundcore_open,
  145. .llseek = noop_llseek,
  146. };
  147. /*
  148. * Low level list operator. Scan the ordered list, find a hole and
  149. * join into it. Called with the lock asserted
  150. */
  151. static int __sound_insert_unit(struct sound_unit * s, struct sound_unit **list, const struct file_operations *fops, int index, int low, int top)
  152. {
  153. int n=low;
  154. if (index < 0) { /* first free */
  155. while (*list && (*list)->unit_minor<n)
  156. list=&((*list)->next);
  157. while(n<top)
  158. {
  159. /* Found a hole ? */
  160. if(*list==NULL || (*list)->unit_minor>n)
  161. break;
  162. list=&((*list)->next);
  163. n+=SOUND_STEP;
  164. }
  165. if(n>=top)
  166. return -ENOENT;
  167. } else {
  168. n = low+(index*16);
  169. while (*list) {
  170. if ((*list)->unit_minor==n)
  171. return -EBUSY;
  172. if ((*list)->unit_minor>n)
  173. break;
  174. list=&((*list)->next);
  175. }
  176. }
  177. /*
  178. * Fill it in
  179. */
  180. s->unit_minor=n;
  181. s->unit_fops=fops;
  182. /*
  183. * Link it
  184. */
  185. s->next=*list;
  186. *list=s;
  187. return n;
  188. }
  189. /*
  190. * Remove a node from the chain. Called with the lock asserted
  191. */
  192. static struct sound_unit *__sound_remove_unit(struct sound_unit **list, int unit)
  193. {
  194. while(*list)
  195. {
  196. struct sound_unit *p=*list;
  197. if(p->unit_minor==unit)
  198. {
  199. *list=p->next;
  200. return p;
  201. }
  202. list=&(p->next);
  203. }
  204. printk(KERN_ERR "Sound device %d went missing!\n", unit);
  205. return NULL;
  206. }
  207. /*
  208. * This lock guards the sound loader list.
  209. */
  210. static DEFINE_SPINLOCK(sound_loader_lock);
  211. /*
  212. * Allocate the controlling structure and add it to the sound driver
  213. * list. Acquires locks as needed
  214. */
  215. 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)
  216. {
  217. struct sound_unit *s = kmalloc(sizeof(*s), GFP_KERNEL);
  218. int r;
  219. if (!s)
  220. return -ENOMEM;
  221. spin_lock(&sound_loader_lock);
  222. retry:
  223. r = __sound_insert_unit(s, list, fops, index, low, top);
  224. spin_unlock(&sound_loader_lock);
  225. if (r < 0)
  226. goto fail;
  227. else if (r < SOUND_STEP)
  228. sprintf(s->name, "sound/%s", name);
  229. else
  230. sprintf(s->name, "sound/%s%d", name, r / SOUND_STEP);
  231. if (!preclaim_oss) {
  232. /*
  233. * Something else might have grabbed the minor. If
  234. * first free slot is requested, rescan with @low set
  235. * to the next unit; otherwise, -EBUSY.
  236. */
  237. r = __register_chrdev(SOUND_MAJOR, s->unit_minor, 1, s->name,
  238. &soundcore_fops);
  239. if (r < 0) {
  240. spin_lock(&sound_loader_lock);
  241. __sound_remove_unit(list, s->unit_minor);
  242. if (index < 0) {
  243. low = s->unit_minor + SOUND_STEP;
  244. goto retry;
  245. }
  246. spin_unlock(&sound_loader_lock);
  247. return -EBUSY;
  248. }
  249. }
  250. device_create(sound_class, dev, MKDEV(SOUND_MAJOR, s->unit_minor),
  251. NULL, s->name+6);
  252. return s->unit_minor;
  253. fail:
  254. kfree(s);
  255. return r;
  256. }
  257. /*
  258. * Remove a unit. Acquires locks as needed. The drivers MUST have
  259. * completed the removal before their file operations become
  260. * invalid.
  261. */
  262. static void sound_remove_unit(struct sound_unit **list, int unit)
  263. {
  264. struct sound_unit *p;
  265. spin_lock(&sound_loader_lock);
  266. p = __sound_remove_unit(list, unit);
  267. spin_unlock(&sound_loader_lock);
  268. if (p) {
  269. if (!preclaim_oss)
  270. __unregister_chrdev(SOUND_MAJOR, p->unit_minor, 1,
  271. p->name);
  272. device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
  273. kfree(p);
  274. }
  275. }
  276. /*
  277. * Allocations
  278. *
  279. * 0 *16 Mixers
  280. * 1 *8 Sequencers
  281. * 2 *16 Midi
  282. * 3 *16 DSP
  283. * 4 *16 SunDSP
  284. * 5 *16 DSP16
  285. * 6 -- sndstat (obsolete)
  286. * 7 *16 unused
  287. * 8 -- alternate sequencer (see above)
  288. * 9 *16 raw synthesizer access
  289. * 10 *16 unused
  290. * 11 *16 unused
  291. * 12 *16 unused
  292. * 13 *16 unused
  293. * 14 *16 unused
  294. * 15 *16 unused
  295. */
  296. static struct sound_unit *chains[SOUND_STEP];
  297. /**
  298. * register_sound_special_device - register a special sound node
  299. * @fops: File operations for the driver
  300. * @unit: Unit number to allocate
  301. * @dev: device pointer
  302. *
  303. * Allocate a special sound device by minor number from the sound
  304. * subsystem. The allocated number is returned on success. On failure
  305. * a negative error code is returned.
  306. */
  307. int register_sound_special_device(const struct file_operations *fops, int unit,
  308. struct device *dev)
  309. {
  310. const int chain = unit % SOUND_STEP;
  311. int max_unit = 128 + chain;
  312. const char *name;
  313. char _name[16];
  314. switch (chain) {
  315. case 0:
  316. name = "mixer";
  317. break;
  318. case 1:
  319. name = "sequencer";
  320. if (unit >= SOUND_STEP)
  321. goto __unknown;
  322. max_unit = unit + 1;
  323. break;
  324. case 2:
  325. name = "midi";
  326. break;
  327. case 3:
  328. name = "dsp";
  329. break;
  330. case 4:
  331. name = "audio";
  332. break;
  333. case 5:
  334. name = "dspW";
  335. break;
  336. case 8:
  337. name = "sequencer2";
  338. if (unit >= SOUND_STEP)
  339. goto __unknown;
  340. max_unit = unit + 1;
  341. break;
  342. case 9:
  343. name = "dmmidi";
  344. break;
  345. case 10:
  346. name = "dmfm";
  347. break;
  348. case 12:
  349. name = "adsp";
  350. break;
  351. case 13:
  352. name = "amidi";
  353. break;
  354. case 14:
  355. name = "admmidi";
  356. break;
  357. default:
  358. {
  359. __unknown:
  360. sprintf(_name, "unknown%d", chain);
  361. if (unit >= SOUND_STEP)
  362. strcat(_name, "-");
  363. name = _name;
  364. }
  365. break;
  366. }
  367. return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
  368. name, S_IRUSR | S_IWUSR, dev);
  369. }
  370. EXPORT_SYMBOL(register_sound_special_device);
  371. int register_sound_special(const struct file_operations *fops, int unit)
  372. {
  373. return register_sound_special_device(fops, unit, NULL);
  374. }
  375. EXPORT_SYMBOL(register_sound_special);
  376. /**
  377. * register_sound_mixer - register a mixer device
  378. * @fops: File operations for the driver
  379. * @dev: Unit number to allocate
  380. *
  381. * Allocate a mixer device. Unit is the number of the mixer requested.
  382. * Pass -1 to request the next free mixer unit. On success the allocated
  383. * number is returned, on failure a negative error code is returned.
  384. */
  385. int register_sound_mixer(const struct file_operations *fops, int dev)
  386. {
  387. return sound_insert_unit(&chains[0], fops, dev, 0, 128,
  388. "mixer", S_IRUSR | S_IWUSR, NULL);
  389. }
  390. EXPORT_SYMBOL(register_sound_mixer);
  391. /**
  392. * register_sound_midi - register a midi device
  393. * @fops: File operations for the driver
  394. * @dev: Unit number to allocate
  395. *
  396. * Allocate a midi device. Unit is the number of the midi device requested.
  397. * Pass -1 to request the next free midi unit. On success the allocated
  398. * number is returned, on failure a negative error code is returned.
  399. */
  400. int register_sound_midi(const struct file_operations *fops, int dev)
  401. {
  402. return sound_insert_unit(&chains[2], fops, dev, 2, 130,
  403. "midi", S_IRUSR | S_IWUSR, NULL);
  404. }
  405. EXPORT_SYMBOL(register_sound_midi);
  406. /*
  407. * DSP's are registered as a triple. Register only one and cheat
  408. * in open - see below.
  409. */
  410. /**
  411. * register_sound_dsp - register a DSP device
  412. * @fops: File operations for the driver
  413. * @dev: Unit number to allocate
  414. *
  415. * Allocate a DSP device. Unit is the number of the DSP requested.
  416. * Pass -1 to request the next free DSP unit. On success the allocated
  417. * number is returned, on failure a negative error code is returned.
  418. *
  419. * This function allocates both the audio and dsp device entries together
  420. * and will always allocate them as a matching pair - eg dsp3/audio3
  421. */
  422. int register_sound_dsp(const struct file_operations *fops, int dev)
  423. {
  424. return sound_insert_unit(&chains[3], fops, dev, 3, 131,
  425. "dsp", S_IWUSR | S_IRUSR, NULL);
  426. }
  427. EXPORT_SYMBOL(register_sound_dsp);
  428. /**
  429. * unregister_sound_special - unregister a special sound device
  430. * @unit: unit number to allocate
  431. *
  432. * Release a sound device that was allocated with
  433. * register_sound_special(). The unit passed is the return value from
  434. * the register function.
  435. */
  436. void unregister_sound_special(int unit)
  437. {
  438. sound_remove_unit(&chains[unit % SOUND_STEP], unit);
  439. }
  440. EXPORT_SYMBOL(unregister_sound_special);
  441. /**
  442. * unregister_sound_mixer - unregister a mixer
  443. * @unit: unit number to allocate
  444. *
  445. * Release a sound device that was allocated with register_sound_mixer().
  446. * The unit passed is the return value from the register function.
  447. */
  448. void unregister_sound_mixer(int unit)
  449. {
  450. sound_remove_unit(&chains[0], unit);
  451. }
  452. EXPORT_SYMBOL(unregister_sound_mixer);
  453. /**
  454. * unregister_sound_midi - unregister a midi device
  455. * @unit: unit number to allocate
  456. *
  457. * Release a sound device that was allocated with register_sound_midi().
  458. * The unit passed is the return value from the register function.
  459. */
  460. void unregister_sound_midi(int unit)
  461. {
  462. sound_remove_unit(&chains[2], unit);
  463. }
  464. EXPORT_SYMBOL(unregister_sound_midi);
  465. /**
  466. * unregister_sound_dsp - unregister a DSP device
  467. * @unit: unit number to allocate
  468. *
  469. * Release a sound device that was allocated with register_sound_dsp().
  470. * The unit passed is the return value from the register function.
  471. *
  472. * Both of the allocated units are released together automatically.
  473. */
  474. void unregister_sound_dsp(int unit)
  475. {
  476. sound_remove_unit(&chains[3], unit);
  477. }
  478. EXPORT_SYMBOL(unregister_sound_dsp);
  479. static struct sound_unit *__look_for_unit(int chain, int unit)
  480. {
  481. struct sound_unit *s;
  482. s=chains[chain];
  483. while(s && s->unit_minor <= unit)
  484. {
  485. if(s->unit_minor==unit)
  486. return s;
  487. s=s->next;
  488. }
  489. return NULL;
  490. }
  491. static int soundcore_open(struct inode *inode, struct file *file)
  492. {
  493. int chain;
  494. int unit = iminor(inode);
  495. struct sound_unit *s;
  496. const struct file_operations *new_fops = NULL;
  497. chain=unit&0x0F;
  498. if(chain==4 || chain==5) /* dsp/audio/dsp16 */
  499. {
  500. unit&=0xF0;
  501. unit|=3;
  502. chain=3;
  503. }
  504. spin_lock(&sound_loader_lock);
  505. s = __look_for_unit(chain, unit);
  506. if (s)
  507. new_fops = fops_get(s->unit_fops);
  508. if (preclaim_oss && !new_fops) {
  509. spin_unlock(&sound_loader_lock);
  510. /*
  511. * Please, don't change this order or code.
  512. * For ALSA slot means soundcard and OSS emulation code
  513. * comes as add-on modules which aren't depend on
  514. * ALSA toplevel modules for soundcards, thus we need
  515. * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
  516. */
  517. request_module("sound-slot-%i", unit>>4);
  518. request_module("sound-service-%i-%i", unit>>4, chain);
  519. /*
  520. * sound-slot/service-* module aliases are scheduled
  521. * for removal in favor of the standard char-major-*
  522. * module aliases. For the time being, generate both
  523. * the legacy and standard module aliases to ease
  524. * transition.
  525. */
  526. if (request_module("char-major-%d-%d", SOUND_MAJOR, unit) > 0)
  527. request_module("char-major-%d", SOUND_MAJOR);
  528. spin_lock(&sound_loader_lock);
  529. s = __look_for_unit(chain, unit);
  530. if (s)
  531. new_fops = fops_get(s->unit_fops);
  532. }
  533. if (new_fops) {
  534. /*
  535. * We rely upon the fact that we can't be unloaded while the
  536. * subdriver is there, so if ->open() is successful we can
  537. * safely drop the reference counter and if it is not we can
  538. * revert to old ->f_op. Ugly, indeed, but that's the cost of
  539. * switching ->f_op in the first place.
  540. */
  541. int err = 0;
  542. const struct file_operations *old_fops = file->f_op;
  543. file->f_op = new_fops;
  544. spin_unlock(&sound_loader_lock);
  545. if (file->f_op->open)
  546. err = file->f_op->open(inode,file);
  547. if (err) {
  548. fops_put(file->f_op);
  549. file->f_op = fops_get(old_fops);
  550. }
  551. fops_put(old_fops);
  552. return err;
  553. }
  554. spin_unlock(&sound_loader_lock);
  555. return -ENODEV;
  556. }
  557. MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
  558. static void cleanup_oss_soundcore(void)
  559. {
  560. /* We have nothing to really do here - we know the lists must be
  561. empty */
  562. unregister_chrdev(SOUND_MAJOR, "sound");
  563. }
  564. static int __init init_oss_soundcore(void)
  565. {
  566. if (preclaim_oss &&
  567. register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops) == -1) {
  568. printk(KERN_ERR "soundcore: sound device already in use.\n");
  569. return -EBUSY;
  570. }
  571. return 0;
  572. }
  573. #endif /* CONFIG_SOUND_OSS_CORE */