sound_core.c 14 KB

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