sound_core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. r = -EBUSY;
  247. goto fail;
  248. }
  249. }
  250. device_create(sound_class, dev, MKDEV(SOUND_MAJOR, s->unit_minor),
  251. NULL, "%s", 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.
  305. *
  306. * Return: The allocated number is returned on success. On failure,
  307. * a negative error code is returned.
  308. */
  309. int register_sound_special_device(const struct file_operations *fops, int unit,
  310. struct device *dev)
  311. {
  312. const int chain = unit % SOUND_STEP;
  313. int max_unit = 256;
  314. const char *name;
  315. char _name[16];
  316. switch (chain) {
  317. case 0:
  318. name = "mixer";
  319. break;
  320. case 1:
  321. name = "sequencer";
  322. if (unit >= SOUND_STEP)
  323. goto __unknown;
  324. max_unit = unit + 1;
  325. break;
  326. case 2:
  327. name = "midi";
  328. break;
  329. case 3:
  330. name = "dsp";
  331. break;
  332. case 4:
  333. name = "audio";
  334. break;
  335. case 5:
  336. name = "dspW";
  337. break;
  338. case 8:
  339. name = "sequencer2";
  340. if (unit >= SOUND_STEP)
  341. goto __unknown;
  342. max_unit = unit + 1;
  343. break;
  344. case 9:
  345. name = "dmmidi";
  346. break;
  347. case 10:
  348. name = "dmfm";
  349. break;
  350. case 12:
  351. name = "adsp";
  352. break;
  353. case 13:
  354. name = "amidi";
  355. break;
  356. case 14:
  357. name = "admmidi";
  358. break;
  359. default:
  360. {
  361. __unknown:
  362. sprintf(_name, "unknown%d", chain);
  363. if (unit >= SOUND_STEP)
  364. strcat(_name, "-");
  365. name = _name;
  366. }
  367. break;
  368. }
  369. return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
  370. name, S_IRUSR | S_IWUSR, dev);
  371. }
  372. EXPORT_SYMBOL(register_sound_special_device);
  373. int register_sound_special(const struct file_operations *fops, int unit)
  374. {
  375. return register_sound_special_device(fops, unit, NULL);
  376. }
  377. EXPORT_SYMBOL(register_sound_special);
  378. /**
  379. * register_sound_mixer - register a mixer device
  380. * @fops: File operations for the driver
  381. * @dev: Unit number to allocate
  382. *
  383. * Allocate a mixer device. Unit is the number of the mixer requested.
  384. * Pass -1 to request the next free mixer unit.
  385. *
  386. * Return: On success, the allocated number is returned. On failure,
  387. * a negative error code is returned.
  388. */
  389. int register_sound_mixer(const struct file_operations *fops, int dev)
  390. {
  391. return sound_insert_unit(&chains[0], fops, dev, 0, 128,
  392. "mixer", S_IRUSR | S_IWUSR, NULL);
  393. }
  394. EXPORT_SYMBOL(register_sound_mixer);
  395. /**
  396. * register_sound_midi - register a midi device
  397. * @fops: File operations for the driver
  398. * @dev: Unit number to allocate
  399. *
  400. * Allocate a midi device. Unit is the number of the midi device requested.
  401. * Pass -1 to request the next free midi unit.
  402. *
  403. * Return: On success, the allocated number is returned. On failure,
  404. * a negative error code is returned.
  405. */
  406. int register_sound_midi(const struct file_operations *fops, int dev)
  407. {
  408. return sound_insert_unit(&chains[2], fops, dev, 2, 130,
  409. "midi", S_IRUSR | S_IWUSR, NULL);
  410. }
  411. EXPORT_SYMBOL(register_sound_midi);
  412. /*
  413. * DSP's are registered as a triple. Register only one and cheat
  414. * in open - see below.
  415. */
  416. /**
  417. * register_sound_dsp - register a DSP device
  418. * @fops: File operations for the driver
  419. * @dev: Unit number to allocate
  420. *
  421. * Allocate a DSP device. Unit is the number of the DSP requested.
  422. * Pass -1 to request the next free DSP unit.
  423. *
  424. * This function allocates both the audio and dsp device entries together
  425. * and will always allocate them as a matching pair - eg dsp3/audio3
  426. *
  427. * Return: On success, the allocated number is returned. On failure,
  428. * a negative error code is returned.
  429. */
  430. int register_sound_dsp(const struct file_operations *fops, int dev)
  431. {
  432. return sound_insert_unit(&chains[3], fops, dev, 3, 131,
  433. "dsp", S_IWUSR | S_IRUSR, NULL);
  434. }
  435. EXPORT_SYMBOL(register_sound_dsp);
  436. /**
  437. * unregister_sound_special - unregister a special sound device
  438. * @unit: unit number to allocate
  439. *
  440. * Release a sound device that was allocated with
  441. * register_sound_special(). The unit passed is the return value from
  442. * the register function.
  443. */
  444. void unregister_sound_special(int unit)
  445. {
  446. sound_remove_unit(&chains[unit % SOUND_STEP], unit);
  447. }
  448. EXPORT_SYMBOL(unregister_sound_special);
  449. /**
  450. * unregister_sound_mixer - unregister a mixer
  451. * @unit: unit number to allocate
  452. *
  453. * Release a sound device that was allocated with register_sound_mixer().
  454. * The unit passed is the return value from the register function.
  455. */
  456. void unregister_sound_mixer(int unit)
  457. {
  458. sound_remove_unit(&chains[0], unit);
  459. }
  460. EXPORT_SYMBOL(unregister_sound_mixer);
  461. /**
  462. * unregister_sound_midi - unregister a midi device
  463. * @unit: unit number to allocate
  464. *
  465. * Release a sound device that was allocated with register_sound_midi().
  466. * The unit passed is the return value from the register function.
  467. */
  468. void unregister_sound_midi(int unit)
  469. {
  470. sound_remove_unit(&chains[2], unit);
  471. }
  472. EXPORT_SYMBOL(unregister_sound_midi);
  473. /**
  474. * unregister_sound_dsp - unregister a DSP device
  475. * @unit: unit number to allocate
  476. *
  477. * Release a sound device that was allocated with register_sound_dsp().
  478. * The unit passed is the return value from the register function.
  479. *
  480. * Both of the allocated units are released together automatically.
  481. */
  482. void unregister_sound_dsp(int unit)
  483. {
  484. sound_remove_unit(&chains[3], unit);
  485. }
  486. EXPORT_SYMBOL(unregister_sound_dsp);
  487. static struct sound_unit *__look_for_unit(int chain, int unit)
  488. {
  489. struct sound_unit *s;
  490. s=chains[chain];
  491. while(s && s->unit_minor <= unit)
  492. {
  493. if(s->unit_minor==unit)
  494. return s;
  495. s=s->next;
  496. }
  497. return NULL;
  498. }
  499. static int soundcore_open(struct inode *inode, struct file *file)
  500. {
  501. int chain;
  502. int unit = iminor(inode);
  503. struct sound_unit *s;
  504. const struct file_operations *new_fops = NULL;
  505. chain=unit&0x0F;
  506. if(chain==4 || chain==5) /* dsp/audio/dsp16 */
  507. {
  508. unit&=0xF0;
  509. unit|=3;
  510. chain=3;
  511. }
  512. spin_lock(&sound_loader_lock);
  513. s = __look_for_unit(chain, unit);
  514. if (s)
  515. new_fops = fops_get(s->unit_fops);
  516. if (preclaim_oss && !new_fops) {
  517. spin_unlock(&sound_loader_lock);
  518. /*
  519. * Please, don't change this order or code.
  520. * For ALSA slot means soundcard and OSS emulation code
  521. * comes as add-on modules which aren't depend on
  522. * ALSA toplevel modules for soundcards, thus we need
  523. * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
  524. */
  525. request_module("sound-slot-%i", unit>>4);
  526. request_module("sound-service-%i-%i", unit>>4, chain);
  527. /*
  528. * sound-slot/service-* module aliases are scheduled
  529. * for removal in favor of the standard char-major-*
  530. * module aliases. For the time being, generate both
  531. * the legacy and standard module aliases to ease
  532. * transition.
  533. */
  534. if (request_module("char-major-%d-%d", SOUND_MAJOR, unit) > 0)
  535. request_module("char-major-%d", SOUND_MAJOR);
  536. spin_lock(&sound_loader_lock);
  537. s = __look_for_unit(chain, unit);
  538. if (s)
  539. new_fops = fops_get(s->unit_fops);
  540. }
  541. spin_unlock(&sound_loader_lock);
  542. if (new_fops) {
  543. /*
  544. * We rely upon the fact that we can't be unloaded while the
  545. * subdriver is there.
  546. */
  547. int err = 0;
  548. replace_fops(file, new_fops);
  549. if (file->f_op->open)
  550. err = file->f_op->open(inode,file);
  551. return err;
  552. }
  553. return -ENODEV;
  554. }
  555. MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
  556. static void cleanup_oss_soundcore(void)
  557. {
  558. /* We have nothing to really do here - we know the lists must be
  559. empty */
  560. unregister_chrdev(SOUND_MAJOR, "sound");
  561. }
  562. static int __init init_oss_soundcore(void)
  563. {
  564. if (preclaim_oss &&
  565. register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops) < 0) {
  566. printk(KERN_ERR "soundcore: sound device already in use.\n");
  567. return -EBUSY;
  568. }
  569. return 0;
  570. }
  571. #endif /* CONFIG_SOUND_OSS_CORE */