sound_core.c 13 KB

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