sound_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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_drvdata(sound_class, dev,
  147. MKDEV(SOUND_MAJOR, s->unit_minor),
  148. NULL, s->name+6);
  149. return r;
  150. fail:
  151. kfree(s);
  152. return r;
  153. }
  154. /*
  155. * Remove a unit. Acquires locks as needed. The drivers MUST have
  156. * completed the removal before their file operations become
  157. * invalid.
  158. */
  159. static void sound_remove_unit(struct sound_unit **list, int unit)
  160. {
  161. struct sound_unit *p;
  162. spin_lock(&sound_loader_lock);
  163. p = __sound_remove_unit(list, unit);
  164. spin_unlock(&sound_loader_lock);
  165. if (p) {
  166. device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
  167. kfree(p);
  168. }
  169. }
  170. /*
  171. * Allocations
  172. *
  173. * 0 *16 Mixers
  174. * 1 *8 Sequencers
  175. * 2 *16 Midi
  176. * 3 *16 DSP
  177. * 4 *16 SunDSP
  178. * 5 *16 DSP16
  179. * 6 -- sndstat (obsolete)
  180. * 7 *16 unused
  181. * 8 -- alternate sequencer (see above)
  182. * 9 *16 raw synthesizer access
  183. * 10 *16 unused
  184. * 11 *16 unused
  185. * 12 *16 unused
  186. * 13 *16 unused
  187. * 14 *16 unused
  188. * 15 *16 unused
  189. */
  190. static struct sound_unit *chains[SOUND_STEP];
  191. /**
  192. * register_sound_special_device - register a special sound node
  193. * @fops: File operations for the driver
  194. * @unit: Unit number to allocate
  195. * @dev: device pointer
  196. *
  197. * Allocate a special sound device by minor number from the sound
  198. * subsystem. The allocated number is returned on succes. On failure
  199. * a negative error code is returned.
  200. */
  201. int register_sound_special_device(const struct file_operations *fops, int unit,
  202. struct device *dev)
  203. {
  204. const int chain = unit % SOUND_STEP;
  205. int max_unit = 128 + chain;
  206. const char *name;
  207. char _name[16];
  208. switch (chain) {
  209. case 0:
  210. name = "mixer";
  211. break;
  212. case 1:
  213. name = "sequencer";
  214. if (unit >= SOUND_STEP)
  215. goto __unknown;
  216. max_unit = unit + 1;
  217. break;
  218. case 2:
  219. name = "midi";
  220. break;
  221. case 3:
  222. name = "dsp";
  223. break;
  224. case 4:
  225. name = "audio";
  226. break;
  227. case 8:
  228. name = "sequencer2";
  229. if (unit >= SOUND_STEP)
  230. goto __unknown;
  231. max_unit = unit + 1;
  232. break;
  233. case 9:
  234. name = "dmmidi";
  235. break;
  236. case 10:
  237. name = "dmfm";
  238. break;
  239. case 12:
  240. name = "adsp";
  241. break;
  242. case 13:
  243. name = "amidi";
  244. break;
  245. case 14:
  246. name = "admmidi";
  247. break;
  248. default:
  249. {
  250. __unknown:
  251. sprintf(_name, "unknown%d", chain);
  252. if (unit >= SOUND_STEP)
  253. strcat(_name, "-");
  254. name = _name;
  255. }
  256. break;
  257. }
  258. return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
  259. name, S_IRUSR | S_IWUSR, dev);
  260. }
  261. EXPORT_SYMBOL(register_sound_special_device);
  262. int register_sound_special(const struct file_operations *fops, int unit)
  263. {
  264. return register_sound_special_device(fops, unit, NULL);
  265. }
  266. EXPORT_SYMBOL(register_sound_special);
  267. /**
  268. * register_sound_mixer - register a mixer device
  269. * @fops: File operations for the driver
  270. * @dev: Unit number to allocate
  271. *
  272. * Allocate a mixer device. Unit is the number of the mixer requested.
  273. * Pass -1 to request the next free mixer unit. On success the allocated
  274. * number is returned, on failure a negative error code is returned.
  275. */
  276. int register_sound_mixer(const struct file_operations *fops, int dev)
  277. {
  278. return sound_insert_unit(&chains[0], fops, dev, 0, 128,
  279. "mixer", S_IRUSR | S_IWUSR, NULL);
  280. }
  281. EXPORT_SYMBOL(register_sound_mixer);
  282. /**
  283. * register_sound_midi - register a midi device
  284. * @fops: File operations for the driver
  285. * @dev: Unit number to allocate
  286. *
  287. * Allocate a midi device. Unit is the number of the midi device requested.
  288. * Pass -1 to request the next free midi unit. On success the allocated
  289. * number is returned, on failure a negative error code is returned.
  290. */
  291. int register_sound_midi(const struct file_operations *fops, int dev)
  292. {
  293. return sound_insert_unit(&chains[2], fops, dev, 2, 130,
  294. "midi", S_IRUSR | S_IWUSR, NULL);
  295. }
  296. EXPORT_SYMBOL(register_sound_midi);
  297. /*
  298. * DSP's are registered as a triple. Register only one and cheat
  299. * in open - see below.
  300. */
  301. /**
  302. * register_sound_dsp - register a DSP device
  303. * @fops: File operations for the driver
  304. * @dev: Unit number to allocate
  305. *
  306. * Allocate a DSP device. Unit is the number of the DSP requested.
  307. * Pass -1 to request the next free DSP unit. On success the allocated
  308. * number is returned, on failure a negative error code is returned.
  309. *
  310. * This function allocates both the audio and dsp device entries together
  311. * and will always allocate them as a matching pair - eg dsp3/audio3
  312. */
  313. int register_sound_dsp(const struct file_operations *fops, int dev)
  314. {
  315. return sound_insert_unit(&chains[3], fops, dev, 3, 131,
  316. "dsp", S_IWUSR | S_IRUSR, NULL);
  317. }
  318. EXPORT_SYMBOL(register_sound_dsp);
  319. /**
  320. * unregister_sound_special - unregister a special sound device
  321. * @unit: unit number to allocate
  322. *
  323. * Release a sound device that was allocated with
  324. * register_sound_special(). The unit passed is the return value from
  325. * the register function.
  326. */
  327. void unregister_sound_special(int unit)
  328. {
  329. sound_remove_unit(&chains[unit % SOUND_STEP], unit);
  330. }
  331. EXPORT_SYMBOL(unregister_sound_special);
  332. /**
  333. * unregister_sound_mixer - unregister a mixer
  334. * @unit: unit number to allocate
  335. *
  336. * Release a sound device that was allocated with register_sound_mixer().
  337. * The unit passed is the return value from the register function.
  338. */
  339. void unregister_sound_mixer(int unit)
  340. {
  341. sound_remove_unit(&chains[0], unit);
  342. }
  343. EXPORT_SYMBOL(unregister_sound_mixer);
  344. /**
  345. * unregister_sound_midi - unregister a midi device
  346. * @unit: unit number to allocate
  347. *
  348. * Release a sound device that was allocated with register_sound_midi().
  349. * The unit passed is the return value from the register function.
  350. */
  351. void unregister_sound_midi(int unit)
  352. {
  353. return sound_remove_unit(&chains[2], unit);
  354. }
  355. EXPORT_SYMBOL(unregister_sound_midi);
  356. /**
  357. * unregister_sound_dsp - unregister a DSP device
  358. * @unit: unit number to allocate
  359. *
  360. * Release a sound device that was allocated with register_sound_dsp().
  361. * The unit passed is the return value from the register function.
  362. *
  363. * Both of the allocated units are released together automatically.
  364. */
  365. void unregister_sound_dsp(int unit)
  366. {
  367. return sound_remove_unit(&chains[3], unit);
  368. }
  369. EXPORT_SYMBOL(unregister_sound_dsp);
  370. /*
  371. * Now our file operations
  372. */
  373. static int soundcore_open(struct inode *, struct file *);
  374. static const struct file_operations soundcore_fops=
  375. {
  376. /* We must have an owner or the module locking fails */
  377. .owner = THIS_MODULE,
  378. .open = soundcore_open,
  379. };
  380. static struct sound_unit *__look_for_unit(int chain, int unit)
  381. {
  382. struct sound_unit *s;
  383. s=chains[chain];
  384. while(s && s->unit_minor <= unit)
  385. {
  386. if(s->unit_minor==unit)
  387. return s;
  388. s=s->next;
  389. }
  390. return NULL;
  391. }
  392. int soundcore_open(struct inode *inode, struct file *file)
  393. {
  394. int chain;
  395. int unit = iminor(inode);
  396. struct sound_unit *s;
  397. const struct file_operations *new_fops = NULL;
  398. lock_kernel ();
  399. chain=unit&0x0F;
  400. if(chain==4 || chain==5) /* dsp/audio/dsp16 */
  401. {
  402. unit&=0xF0;
  403. unit|=3;
  404. chain=3;
  405. }
  406. spin_lock(&sound_loader_lock);
  407. s = __look_for_unit(chain, unit);
  408. if (s)
  409. new_fops = fops_get(s->unit_fops);
  410. if (!new_fops) {
  411. spin_unlock(&sound_loader_lock);
  412. /*
  413. * Please, don't change this order or code.
  414. * For ALSA slot means soundcard and OSS emulation code
  415. * comes as add-on modules which aren't depend on
  416. * ALSA toplevel modules for soundcards, thus we need
  417. * load them at first. [Jaroslav Kysela <perex@jcu.cz>]
  418. */
  419. request_module("sound-slot-%i", unit>>4);
  420. request_module("sound-service-%i-%i", unit>>4, chain);
  421. spin_lock(&sound_loader_lock);
  422. s = __look_for_unit(chain, unit);
  423. if (s)
  424. new_fops = fops_get(s->unit_fops);
  425. }
  426. if (new_fops) {
  427. /*
  428. * We rely upon the fact that we can't be unloaded while the
  429. * subdriver is there, so if ->open() is successful we can
  430. * safely drop the reference counter and if it is not we can
  431. * revert to old ->f_op. Ugly, indeed, but that's the cost of
  432. * switching ->f_op in the first place.
  433. */
  434. int err = 0;
  435. const struct file_operations *old_fops = file->f_op;
  436. file->f_op = new_fops;
  437. spin_unlock(&sound_loader_lock);
  438. if(file->f_op->open)
  439. err = file->f_op->open(inode,file);
  440. if (err) {
  441. fops_put(file->f_op);
  442. file->f_op = fops_get(old_fops);
  443. }
  444. fops_put(old_fops);
  445. unlock_kernel();
  446. return err;
  447. }
  448. spin_unlock(&sound_loader_lock);
  449. unlock_kernel();
  450. return -ENODEV;
  451. }
  452. MODULE_DESCRIPTION("Core sound module");
  453. MODULE_AUTHOR("Alan Cox");
  454. MODULE_LICENSE("GPL");
  455. MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
  456. static void __exit cleanup_soundcore(void)
  457. {
  458. /* We have nothing to really do here - we know the lists must be
  459. empty */
  460. unregister_chrdev(SOUND_MAJOR, "sound");
  461. class_destroy(sound_class);
  462. }
  463. static int __init init_soundcore(void)
  464. {
  465. if (register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops)==-1) {
  466. printk(KERN_ERR "soundcore: sound device already in use.\n");
  467. return -EBUSY;
  468. }
  469. sound_class = class_create(THIS_MODULE, "sound");
  470. if (IS_ERR(sound_class))
  471. return PTR_ERR(sound_class);
  472. return 0;
  473. }
  474. module_init(init_soundcore);
  475. module_exit(cleanup_soundcore);