sound_core.c 14 KB

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