sound_core.c 14 KB

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