sound_core.c 14 KB

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