sound_core.c 14 KB

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