sound_core.c 13 KB

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