sound_core.c 13 KB

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