sound_core.c 13 KB

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