sound_core.c 14 KB

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