sound_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 <[email protected]>
  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. static int preclaim_oss = IS_ENABLED(CONFIG_SOUND_OSS_CORE_PRECLAIM);
  122. module_param(preclaim_oss, int, 0444);
  123. static int soundcore_open(struct inode *, struct file *);
  124. static const struct file_operations soundcore_fops =
  125. {
  126. /* We must have an owner or the module locking fails */
  127. .owner = THIS_MODULE,
  128. .open = soundcore_open,
  129. .llseek = noop_llseek,
  130. };
  131. /*
  132. * Low level list operator. Scan the ordered list, find a hole and
  133. * join into it. Called with the lock asserted
  134. */
  135. static int __sound_insert_unit(struct sound_unit * s, struct sound_unit **list, const struct file_operations *fops, int index, int low, int top)
  136. {
  137. int n=low;
  138. if (index < 0) { /* first free */
  139. while (*list && (*list)->unit_minor<n)
  140. list=&((*list)->next);
  141. while(n<top)
  142. {
  143. /* Found a hole ? */
  144. if(*list==NULL || (*list)->unit_minor>n)
  145. break;
  146. list=&((*list)->next);
  147. n+=SOUND_STEP;
  148. }
  149. if(n>=top)
  150. return -ENOENT;
  151. } else {
  152. n = low+(index*16);
  153. while (*list) {
  154. if ((*list)->unit_minor==n)
  155. return -EBUSY;
  156. if ((*list)->unit_minor>n)
  157. break;
  158. list=&((*list)->next);
  159. }
  160. }
  161. /*
  162. * Fill it in
  163. */
  164. s->unit_minor=n;
  165. s->unit_fops=fops;
  166. /*
  167. * Link it
  168. */
  169. s->next=*list;
  170. *list=s;
  171. return n;
  172. }
  173. /*
  174. * Remove a node from the chain. Called with the lock asserted
  175. */
  176. static struct sound_unit *__sound_remove_unit(struct sound_unit **list, int unit)
  177. {
  178. while(*list)
  179. {
  180. struct sound_unit *p=*list;
  181. if(p->unit_minor==unit)
  182. {
  183. *list=p->next;
  184. return p;
  185. }
  186. list=&(p->next);
  187. }
  188. printk(KERN_ERR "Sound device %d went missing!\n", unit);
  189. return NULL;
  190. }
  191. /*
  192. * This lock guards the sound loader list.
  193. */
  194. static DEFINE_SPINLOCK(sound_loader_lock);
  195. /*
  196. * Allocate the controlling structure and add it to the sound driver
  197. * list. Acquires locks as needed
  198. */
  199. 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)
  200. {
  201. struct sound_unit *s = kmalloc(sizeof(*s), GFP_KERNEL);
  202. int r;
  203. if (!s)
  204. return -ENOMEM;
  205. spin_lock(&sound_loader_lock);
  206. retry:
  207. r = __sound_insert_unit(s, list, fops, index, low, top);
  208. spin_unlock(&sound_loader_lock);
  209. if (r < 0)
  210. goto fail;
  211. else if (r < SOUND_STEP)
  212. sprintf(s->name, "sound/%s", name);
  213. else
  214. sprintf(s->name, "sound/%s%d", name, r / SOUND_STEP);
  215. if (!preclaim_oss) {
  216. /*
  217. * Something else might have grabbed the minor. If
  218. * first free slot is requested, rescan with @low set
  219. * to the next unit; otherwise, -EBUSY.
  220. */
  221. r = __register_chrdev(SOUND_MAJOR, s->unit_minor, 1, s->name,
  222. &soundcore_fops);
  223. if (r < 0) {
  224. spin_lock(&sound_loader_lock);
  225. __sound_remove_unit(list, s->unit_minor);
  226. if (index < 0) {
  227. low = s->unit_minor + SOUND_STEP;
  228. goto retry;
  229. }
  230. spin_unlock(&sound_loader_lock);
  231. r = -EBUSY;
  232. goto fail;
  233. }
  234. }
  235. device_create(sound_class, dev, MKDEV(SOUND_MAJOR, s->unit_minor),
  236. NULL, "%s", s->name+6);
  237. return s->unit_minor;
  238. fail:
  239. kfree(s);
  240. return r;
  241. }
  242. /*
  243. * Remove a unit. Acquires locks as needed. The drivers MUST have
  244. * completed the removal before their file operations become
  245. * invalid.
  246. */
  247. static void sound_remove_unit(struct sound_unit **list, int unit)
  248. {
  249. struct sound_unit *p;
  250. spin_lock(&sound_loader_lock);
  251. p = __sound_remove_unit(list, unit);
  252. spin_unlock(&sound_loader_lock);
  253. if (p) {
  254. if (!preclaim_oss)
  255. __unregister_chrdev(SOUND_MAJOR, p->unit_minor, 1,
  256. p->name);
  257. device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
  258. kfree(p);
  259. }
  260. }
  261. /*
  262. * Allocations
  263. *
  264. * 0 *16 Mixers
  265. * 1 *8 Sequencers
  266. * 2 *16 Midi
  267. * 3 *16 DSP
  268. * 4 *16 SunDSP
  269. * 5 *16 DSP16
  270. * 6 -- sndstat (obsolete)
  271. * 7 *16 unused
  272. * 8 -- alternate sequencer (see above)
  273. * 9 *16 raw synthesizer access
  274. * 10 *16 unused
  275. * 11 *16 unused
  276. * 12 *16 unused
  277. * 13 *16 unused
  278. * 14 *16 unused
  279. * 15 *16 unused
  280. */
  281. static struct sound_unit *chains[SOUND_STEP];
  282. /**
  283. * register_sound_special_device - register a special sound node
  284. * @fops: File operations for the driver
  285. * @unit: Unit number to allocate
  286. * @dev: device pointer
  287. *
  288. * Allocate a special sound device by minor number from the sound
  289. * subsystem.
  290. *
  291. * Return: The allocated number is returned on success. On failure,
  292. * a negative error code is returned.
  293. */
  294. int register_sound_special_device(const struct file_operations *fops, int unit,
  295. struct device *dev)
  296. {
  297. const int chain = unit % SOUND_STEP;
  298. int max_unit = 256;
  299. const char *name;
  300. char _name[16];
  301. switch (chain) {
  302. case 0:
  303. name = "mixer";
  304. break;
  305. case 1:
  306. name = "sequencer";
  307. if (unit >= SOUND_STEP)
  308. goto __unknown;
  309. max_unit = unit + 1;
  310. break;
  311. case 2:
  312. name = "midi";
  313. break;
  314. case 3:
  315. name = "dsp";
  316. break;
  317. case 4:
  318. name = "audio";
  319. break;
  320. case 5:
  321. name = "dspW";
  322. break;
  323. case 8:
  324. name = "sequencer2";
  325. if (unit >= SOUND_STEP)
  326. goto __unknown;
  327. max_unit = unit + 1;
  328. break;
  329. case 9:
  330. name = "dmmidi";
  331. break;
  332. case 10:
  333. name = "dmfm";
  334. break;
  335. case 12:
  336. name = "adsp";
  337. break;
  338. case 13:
  339. name = "amidi";
  340. break;
  341. case 14:
  342. name = "admmidi";
  343. break;
  344. default:
  345. {
  346. __unknown:
  347. sprintf(_name, "unknown%d", chain);
  348. if (unit >= SOUND_STEP)
  349. strcat(_name, "-");
  350. name = _name;
  351. }
  352. break;
  353. }
  354. return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
  355. name, 0600, dev);
  356. }
  357. EXPORT_SYMBOL(register_sound_special_device);
  358. int register_sound_special(const struct file_operations *fops, int unit)
  359. {
  360. return register_sound_special_device(fops, unit, NULL);
  361. }
  362. EXPORT_SYMBOL(register_sound_special);
  363. /**
  364. * register_sound_mixer - register a mixer device
  365. * @fops: File operations for the driver
  366. * @dev: Unit number to allocate
  367. *
  368. * Allocate a mixer device. Unit is the number of the mixer requested.
  369. * Pass -1 to request the next free mixer unit.
  370. *
  371. * Return: On success, the allocated number is returned. On failure,
  372. * a negative error code is returned.
  373. */
  374. int register_sound_mixer(const struct file_operations *fops, int dev)
  375. {
  376. return sound_insert_unit(&chains[0], fops, dev, 0, 128,
  377. "mixer", 0600, NULL);
  378. }
  379. EXPORT_SYMBOL(register_sound_mixer);
  380. /*
  381. * DSP's are registered as a triple. Register only one and cheat
  382. * in open - see below.
  383. */
  384. /**
  385. * register_sound_dsp - register a DSP device
  386. * @fops: File operations for the driver
  387. * @dev: Unit number to allocate
  388. *
  389. * Allocate a DSP device. Unit is the number of the DSP requested.
  390. * Pass -1 to request the next free DSP unit.
  391. *
  392. * This function allocates both the audio and dsp device entries together
  393. * and will always allocate them as a matching pair - eg dsp3/audio3
  394. *
  395. * Return: On success, the allocated number is returned. On failure,
  396. * a negative error code is returned.
  397. */
  398. int register_sound_dsp(const struct file_operations *fops, int dev)
  399. {
  400. return sound_insert_unit(&chains[3], fops, dev, 3, 131,
  401. "dsp", 0600, NULL);
  402. }
  403. EXPORT_SYMBOL(register_sound_dsp);
  404. /**
  405. * unregister_sound_special - unregister a special sound device
  406. * @unit: unit number to allocate
  407. *
  408. * Release a sound device that was allocated with
  409. * register_sound_special(). The unit passed is the return value from
  410. * the register function.
  411. */
  412. void unregister_sound_special(int unit)
  413. {
  414. sound_remove_unit(&chains[unit % SOUND_STEP], unit);
  415. }
  416. EXPORT_SYMBOL(unregister_sound_special);
  417. /**
  418. * unregister_sound_mixer - unregister a mixer
  419. * @unit: unit number to allocate
  420. *
  421. * Release a sound device that was allocated with register_sound_mixer().
  422. * The unit passed is the return value from the register function.
  423. */
  424. void unregister_sound_mixer(int unit)
  425. {
  426. sound_remove_unit(&chains[0], unit);
  427. }
  428. EXPORT_SYMBOL(unregister_sound_mixer);
  429. /**
  430. * unregister_sound_dsp - unregister a DSP device
  431. * @unit: unit number to allocate
  432. *
  433. * Release a sound device that was allocated with register_sound_dsp().
  434. * The unit passed is the return value from the register function.
  435. *
  436. * Both of the allocated units are released together automatically.
  437. */
  438. void unregister_sound_dsp(int unit)
  439. {
  440. sound_remove_unit(&chains[3], unit);
  441. }
  442. EXPORT_SYMBOL(unregister_sound_dsp);
  443. static struct sound_unit *__look_for_unit(int chain, int unit)
  444. {
  445. struct sound_unit *s;
  446. s=chains[chain];
  447. while(s && s->unit_minor <= unit)
  448. {
  449. if(s->unit_minor==unit)
  450. return s;
  451. s=s->next;
  452. }
  453. return NULL;
  454. }
  455. static int soundcore_open(struct inode *inode, struct file *file)
  456. {
  457. int chain;
  458. int unit = iminor(inode);
  459. struct sound_unit *s;
  460. const struct file_operations *new_fops = NULL;
  461. chain=unit&0x0F;
  462. if(chain==4 || chain==5) /* dsp/audio/dsp16 */
  463. {
  464. unit&=0xF0;
  465. unit|=3;
  466. chain=3;
  467. }
  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. if (preclaim_oss && !new_fops) {
  473. spin_unlock(&sound_loader_lock);
  474. /*
  475. * Please, don't change this order or code.
  476. * For ALSA slot means soundcard and OSS emulation code
  477. * comes as add-on modules which aren't depend on
  478. * ALSA toplevel modules for soundcards, thus we need
  479. * load them at first. [Jaroslav Kysela <[email protected]>]
  480. */
  481. request_module("sound-slot-%i", unit>>4);
  482. request_module("sound-service-%i-%i", unit>>4, chain);
  483. /*
  484. * sound-slot/service-* module aliases are scheduled
  485. * for removal in favor of the standard char-major-*
  486. * module aliases. For the time being, generate both
  487. * the legacy and standard module aliases to ease
  488. * transition.
  489. */
  490. if (request_module("char-major-%d-%d", SOUND_MAJOR, unit) > 0)
  491. request_module("char-major-%d", SOUND_MAJOR);
  492. spin_lock(&sound_loader_lock);
  493. s = __look_for_unit(chain, unit);
  494. if (s)
  495. new_fops = fops_get(s->unit_fops);
  496. }
  497. spin_unlock(&sound_loader_lock);
  498. if (!new_fops)
  499. return -ENODEV;
  500. /*
  501. * We rely upon the fact that we can't be unloaded while the
  502. * subdriver is there.
  503. */
  504. replace_fops(file, new_fops);
  505. if (!file->f_op->open)
  506. return -ENODEV;
  507. return file->f_op->open(inode, file);
  508. }
  509. MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
  510. static void cleanup_oss_soundcore(void)
  511. {
  512. /* We have nothing to really do here - we know the lists must be
  513. empty */
  514. unregister_chrdev(SOUND_MAJOR, "sound");
  515. }
  516. static int __init init_oss_soundcore(void)
  517. {
  518. if (preclaim_oss &&
  519. register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops) < 0) {
  520. printk(KERN_ERR "soundcore: sound device already in use.\n");
  521. return -EBUSY;
  522. }
  523. return 0;
  524. }
  525. #endif /* CONFIG_SOUND_OSS_CORE */