sound_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. r = -EBUSY;
  241. goto fail;
  242. }
  243. }
  244. device_create(sound_class, dev, MKDEV(SOUND_MAJOR, s->unit_minor),
  245. NULL, "%s", s->name+6);
  246. return s->unit_minor;
  247. fail:
  248. kfree(s);
  249. return r;
  250. }
  251. /*
  252. * Remove a unit. Acquires locks as needed. The drivers MUST have
  253. * completed the removal before their file operations become
  254. * invalid.
  255. */
  256. static void sound_remove_unit(struct sound_unit **list, int unit)
  257. {
  258. struct sound_unit *p;
  259. spin_lock(&sound_loader_lock);
  260. p = __sound_remove_unit(list, unit);
  261. spin_unlock(&sound_loader_lock);
  262. if (p) {
  263. if (!preclaim_oss)
  264. __unregister_chrdev(SOUND_MAJOR, p->unit_minor, 1,
  265. p->name);
  266. device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
  267. kfree(p);
  268. }
  269. }
  270. /*
  271. * Allocations
  272. *
  273. * 0 *16 Mixers
  274. * 1 *8 Sequencers
  275. * 2 *16 Midi
  276. * 3 *16 DSP
  277. * 4 *16 SunDSP
  278. * 5 *16 DSP16
  279. * 6 -- sndstat (obsolete)
  280. * 7 *16 unused
  281. * 8 -- alternate sequencer (see above)
  282. * 9 *16 raw synthesizer access
  283. * 10 *16 unused
  284. * 11 *16 unused
  285. * 12 *16 unused
  286. * 13 *16 unused
  287. * 14 *16 unused
  288. * 15 *16 unused
  289. */
  290. static struct sound_unit *chains[SOUND_STEP];
  291. /**
  292. * register_sound_special_device - register a special sound node
  293. * @fops: File operations for the driver
  294. * @unit: Unit number to allocate
  295. * @dev: device pointer
  296. *
  297. * Allocate a special sound device by minor number from the sound
  298. * subsystem.
  299. *
  300. * Return: The allocated number is returned on success. On failure,
  301. * a negative error code is returned.
  302. */
  303. int register_sound_special_device(const struct file_operations *fops, int unit,
  304. struct device *dev)
  305. {
  306. const int chain = unit % SOUND_STEP;
  307. int max_unit = 256;
  308. const char *name;
  309. char _name[16];
  310. switch (chain) {
  311. case 0:
  312. name = "mixer";
  313. break;
  314. case 1:
  315. name = "sequencer";
  316. if (unit >= SOUND_STEP)
  317. goto __unknown;
  318. max_unit = unit + 1;
  319. break;
  320. case 2:
  321. name = "midi";
  322. break;
  323. case 3:
  324. name = "dsp";
  325. break;
  326. case 4:
  327. name = "audio";
  328. break;
  329. case 5:
  330. name = "dspW";
  331. break;
  332. case 8:
  333. name = "sequencer2";
  334. if (unit >= SOUND_STEP)
  335. goto __unknown;
  336. max_unit = unit + 1;
  337. break;
  338. case 9:
  339. name = "dmmidi";
  340. break;
  341. case 10:
  342. name = "dmfm";
  343. break;
  344. case 12:
  345. name = "adsp";
  346. break;
  347. case 13:
  348. name = "amidi";
  349. break;
  350. case 14:
  351. name = "admmidi";
  352. break;
  353. default:
  354. {
  355. __unknown:
  356. sprintf(_name, "unknown%d", chain);
  357. if (unit >= SOUND_STEP)
  358. strcat(_name, "-");
  359. name = _name;
  360. }
  361. break;
  362. }
  363. return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
  364. name, 0600, dev);
  365. }
  366. EXPORT_SYMBOL(register_sound_special_device);
  367. int register_sound_special(const struct file_operations *fops, int unit)
  368. {
  369. return register_sound_special_device(fops, unit, NULL);
  370. }
  371. EXPORT_SYMBOL(register_sound_special);
  372. /**
  373. * register_sound_mixer - register a mixer device
  374. * @fops: File operations for the driver
  375. * @dev: Unit number to allocate
  376. *
  377. * Allocate a mixer device. Unit is the number of the mixer requested.
  378. * Pass -1 to request the next free mixer unit.
  379. *
  380. * Return: On success, the allocated number is returned. On failure,
  381. * a negative error code is returned.
  382. */
  383. int register_sound_mixer(const struct file_operations *fops, int dev)
  384. {
  385. return sound_insert_unit(&chains[0], fops, dev, 0, 128,
  386. "mixer", 0600, NULL);
  387. }
  388. EXPORT_SYMBOL(register_sound_mixer);
  389. /*
  390. * DSP's are registered as a triple. Register only one and cheat
  391. * in open - see below.
  392. */
  393. /**
  394. * register_sound_dsp - register a DSP device
  395. * @fops: File operations for the driver
  396. * @dev: Unit number to allocate
  397. *
  398. * Allocate a DSP device. Unit is the number of the DSP requested.
  399. * Pass -1 to request the next free DSP unit.
  400. *
  401. * This function allocates both the audio and dsp device entries together
  402. * and will always allocate them as a matching pair - eg dsp3/audio3
  403. *
  404. * Return: On success, the allocated number is returned. On failure,
  405. * a negative error code is returned.
  406. */
  407. int register_sound_dsp(const struct file_operations *fops, int dev)
  408. {
  409. return sound_insert_unit(&chains[3], fops, dev, 3, 131,
  410. "dsp", 0600, NULL);
  411. }
  412. EXPORT_SYMBOL(register_sound_dsp);
  413. /**
  414. * unregister_sound_special - unregister a special sound device
  415. * @unit: unit number to allocate
  416. *
  417. * Release a sound device that was allocated with
  418. * register_sound_special(). The unit passed is the return value from
  419. * the register function.
  420. */
  421. void unregister_sound_special(int unit)
  422. {
  423. sound_remove_unit(&chains[unit % SOUND_STEP], unit);
  424. }
  425. EXPORT_SYMBOL(unregister_sound_special);
  426. /**
  427. * unregister_sound_mixer - unregister a mixer
  428. * @unit: unit number to allocate
  429. *
  430. * Release a sound device that was allocated with register_sound_mixer().
  431. * The unit passed is the return value from the register function.
  432. */
  433. void unregister_sound_mixer(int unit)
  434. {
  435. sound_remove_unit(&chains[0], unit);
  436. }
  437. EXPORT_SYMBOL(unregister_sound_mixer);
  438. /**
  439. * unregister_sound_dsp - unregister a DSP device
  440. * @unit: unit number to allocate
  441. *
  442. * Release a sound device that was allocated with register_sound_dsp().
  443. * The unit passed is the return value from the register function.
  444. *
  445. * Both of the allocated units are released together automatically.
  446. */
  447. void unregister_sound_dsp(int unit)
  448. {
  449. sound_remove_unit(&chains[3], unit);
  450. }
  451. EXPORT_SYMBOL(unregister_sound_dsp);
  452. static struct sound_unit *__look_for_unit(int chain, int unit)
  453. {
  454. struct sound_unit *s;
  455. s=chains[chain];
  456. while(s && s->unit_minor <= unit)
  457. {
  458. if(s->unit_minor==unit)
  459. return s;
  460. s=s->next;
  461. }
  462. return NULL;
  463. }
  464. static int soundcore_open(struct inode *inode, struct file *file)
  465. {
  466. int chain;
  467. int unit = iminor(inode);
  468. struct sound_unit *s;
  469. const struct file_operations *new_fops = NULL;
  470. chain=unit&0x0F;
  471. if(chain==4 || chain==5) /* dsp/audio/dsp16 */
  472. {
  473. unit&=0xF0;
  474. unit|=3;
  475. chain=3;
  476. }
  477. spin_lock(&sound_loader_lock);
  478. s = __look_for_unit(chain, unit);
  479. if (s)
  480. new_fops = fops_get(s->unit_fops);
  481. if (preclaim_oss && !new_fops) {
  482. spin_unlock(&sound_loader_lock);
  483. /*
  484. * Please, don't change this order or code.
  485. * For ALSA slot means soundcard and OSS emulation code
  486. * comes as add-on modules which aren't depend on
  487. * ALSA toplevel modules for soundcards, thus we need
  488. * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
  489. */
  490. request_module("sound-slot-%i", unit>>4);
  491. request_module("sound-service-%i-%i", unit>>4, chain);
  492. /*
  493. * sound-slot/service-* module aliases are scheduled
  494. * for removal in favor of the standard char-major-*
  495. * module aliases. For the time being, generate both
  496. * the legacy and standard module aliases to ease
  497. * transition.
  498. */
  499. if (request_module("char-major-%d-%d", SOUND_MAJOR, unit) > 0)
  500. request_module("char-major-%d", SOUND_MAJOR);
  501. spin_lock(&sound_loader_lock);
  502. s = __look_for_unit(chain, unit);
  503. if (s)
  504. new_fops = fops_get(s->unit_fops);
  505. }
  506. spin_unlock(&sound_loader_lock);
  507. if (new_fops) {
  508. /*
  509. * We rely upon the fact that we can't be unloaded while the
  510. * subdriver is there.
  511. */
  512. int err = 0;
  513. replace_fops(file, new_fops);
  514. if (file->f_op->open)
  515. err = file->f_op->open(inode,file);
  516. return err;
  517. }
  518. return -ENODEV;
  519. }
  520. MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
  521. static void cleanup_oss_soundcore(void)
  522. {
  523. /* We have nothing to really do here - we know the lists must be
  524. empty */
  525. unregister_chrdev(SOUND_MAJOR, "sound");
  526. }
  527. static int __init init_oss_soundcore(void)
  528. {
  529. if (preclaim_oss &&
  530. register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops) < 0) {
  531. printk(KERN_ERR "soundcore: sound device already in use.\n");
  532. return -EBUSY;
  533. }
  534. return 0;
  535. }
  536. #endif /* CONFIG_SOUND_OSS_CORE */