init.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Initialization routines
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/sched.h>
  8. #include <linux/module.h>
  9. #include <linux/device.h>
  10. #include <linux/file.h>
  11. #include <linux/slab.h>
  12. #include <linux/time.h>
  13. #include <linux/ctype.h>
  14. #include <linux/pm.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/completion.h>
  17. #include <linux/interrupt.h>
  18. #include <sound/core.h>
  19. #include <sound/control.h>
  20. #include <sound/info.h>
  21. /* monitor files for graceful shutdown (hotplug) */
  22. struct snd_monitor_file {
  23. struct file *file;
  24. const struct file_operations *disconnected_f_op;
  25. struct list_head shutdown_list; /* still need to shutdown */
  26. struct list_head list; /* link of monitor files */
  27. };
  28. static DEFINE_SPINLOCK(shutdown_lock);
  29. static LIST_HEAD(shutdown_files);
  30. static const struct file_operations snd_shutdown_f_ops;
  31. /* locked for registering/using */
  32. static DECLARE_BITMAP(snd_cards_lock, SNDRV_CARDS);
  33. static struct snd_card *snd_cards[SNDRV_CARDS];
  34. static DEFINE_MUTEX(snd_card_mutex);
  35. static char *slots[SNDRV_CARDS];
  36. module_param_array(slots, charp, NULL, 0444);
  37. MODULE_PARM_DESC(slots, "Module names assigned to the slots.");
  38. /* return non-zero if the given index is reserved for the given
  39. * module via slots option
  40. */
  41. static int module_slot_match(struct module *module, int idx)
  42. {
  43. int match = 1;
  44. #ifdef CONFIG_MODULES
  45. const char *s1, *s2;
  46. if (!module || !*module->name || !slots[idx])
  47. return 0;
  48. s1 = module->name;
  49. s2 = slots[idx];
  50. if (*s2 == '!') {
  51. match = 0; /* negative match */
  52. s2++;
  53. }
  54. /* compare module name strings
  55. * hyphens are handled as equivalent with underscore
  56. */
  57. for (;;) {
  58. char c1 = *s1++;
  59. char c2 = *s2++;
  60. if (c1 == '-')
  61. c1 = '_';
  62. if (c2 == '-')
  63. c2 = '_';
  64. if (c1 != c2)
  65. return !match;
  66. if (!c1)
  67. break;
  68. }
  69. #endif /* CONFIG_MODULES */
  70. return match;
  71. }
  72. #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
  73. int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
  74. EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
  75. #endif
  76. static int check_empty_slot(struct module *module, int slot)
  77. {
  78. return !slots[slot] || !*slots[slot];
  79. }
  80. /* return an empty slot number (>= 0) found in the given bitmask @mask.
  81. * @mask == -1 == 0xffffffff means: take any free slot up to 32
  82. * when no slot is available, return the original @mask as is.
  83. */
  84. static int get_slot_from_bitmask(int mask, int (*check)(struct module *, int),
  85. struct module *module)
  86. {
  87. int slot;
  88. for (slot = 0; slot < SNDRV_CARDS; slot++) {
  89. if (slot < 32 && !(mask & (1U << slot)))
  90. continue;
  91. if (!test_bit(slot, snd_cards_lock)) {
  92. if (check(module, slot))
  93. return slot; /* found */
  94. }
  95. }
  96. return mask; /* unchanged */
  97. }
  98. /* the default release callback set in snd_device_alloc() */
  99. static void default_release_alloc(struct device *dev)
  100. {
  101. kfree(dev);
  102. }
  103. /**
  104. * snd_device_alloc - Allocate and initialize struct device for sound devices
  105. * @dev_p: pointer to store the allocated device
  106. * @card: card to assign, optional
  107. *
  108. * For releasing the allocated device, call put_device().
  109. */
  110. int snd_device_alloc(struct device **dev_p, struct snd_card *card)
  111. {
  112. struct device *dev;
  113. *dev_p = NULL;
  114. dev = kzalloc_obj(*dev);
  115. if (!dev)
  116. return -ENOMEM;
  117. device_initialize(dev);
  118. if (card)
  119. dev->parent = &card->card_dev;
  120. dev->class = &sound_class;
  121. dev->release = default_release_alloc;
  122. *dev_p = dev;
  123. return 0;
  124. }
  125. EXPORT_SYMBOL_GPL(snd_device_alloc);
  126. static int snd_card_init(struct snd_card *card, struct device *parent,
  127. int idx, const char *xid, struct module *module,
  128. size_t extra_size);
  129. static int snd_card_do_free(struct snd_card *card);
  130. static const struct attribute_group card_dev_attr_group;
  131. static void release_card_device(struct device *dev)
  132. {
  133. snd_card_do_free(dev_to_snd_card(dev));
  134. }
  135. /**
  136. * snd_card_new - create and initialize a soundcard structure
  137. * @parent: the parent device object
  138. * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
  139. * @xid: card identification (ASCII string)
  140. * @module: top level module for locking
  141. * @extra_size: allocate this extra size after the main soundcard structure
  142. * @card_ret: the pointer to store the created card instance
  143. *
  144. * The function allocates snd_card instance via kzalloc with the given
  145. * space for the driver to use freely. The allocated struct is stored
  146. * in the given card_ret pointer.
  147. *
  148. * Return: Zero if successful or a negative error code.
  149. */
  150. int snd_card_new(struct device *parent, int idx, const char *xid,
  151. struct module *module, int extra_size,
  152. struct snd_card **card_ret)
  153. {
  154. struct snd_card *card;
  155. int err;
  156. if (snd_BUG_ON(!card_ret))
  157. return -EINVAL;
  158. *card_ret = NULL;
  159. if (extra_size < 0)
  160. extra_size = 0;
  161. card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
  162. if (!card)
  163. return -ENOMEM;
  164. err = snd_card_init(card, parent, idx, xid, module, extra_size);
  165. if (err < 0)
  166. return err; /* card is freed by error handler */
  167. *card_ret = card;
  168. return 0;
  169. }
  170. EXPORT_SYMBOL(snd_card_new);
  171. static void __snd_card_release(struct device *dev, void *data)
  172. {
  173. snd_card_free(data);
  174. }
  175. /**
  176. * snd_devm_card_new - managed snd_card object creation
  177. * @parent: the parent device object
  178. * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
  179. * @xid: card identification (ASCII string)
  180. * @module: top level module for locking
  181. * @extra_size: allocate this extra size after the main soundcard structure
  182. * @card_ret: the pointer to store the created card instance
  183. *
  184. * This function works like snd_card_new() but manages the allocated resource
  185. * via devres, i.e. you don't need to free explicitly.
  186. *
  187. * When a snd_card object is created with this function and registered via
  188. * snd_card_register(), the very first devres action to call snd_card_free()
  189. * is added automatically. In that way, the resource disconnection is assured
  190. * at first, then released in the expected order.
  191. *
  192. * If an error happens at the probe before snd_card_register() is called and
  193. * there have been other devres resources, you'd need to free the card manually
  194. * via snd_card_free() call in the error; otherwise it may lead to UAF due to
  195. * devres call orders. You can use snd_card_free_on_error() helper for
  196. * handling it more easily.
  197. *
  198. * Return: zero if successful, or a negative error code
  199. */
  200. int snd_devm_card_new(struct device *parent, int idx, const char *xid,
  201. struct module *module, size_t extra_size,
  202. struct snd_card **card_ret)
  203. {
  204. struct snd_card *card;
  205. int err;
  206. *card_ret = NULL;
  207. card = devres_alloc(__snd_card_release, sizeof(*card) + extra_size,
  208. GFP_KERNEL);
  209. if (!card)
  210. return -ENOMEM;
  211. card->managed = true;
  212. err = snd_card_init(card, parent, idx, xid, module, extra_size);
  213. if (err < 0) {
  214. devres_free(card); /* in managed mode, we need to free manually */
  215. return err;
  216. }
  217. devres_add(parent, card);
  218. *card_ret = card;
  219. return 0;
  220. }
  221. EXPORT_SYMBOL_GPL(snd_devm_card_new);
  222. /**
  223. * snd_card_free_on_error - a small helper for handling devm probe errors
  224. * @dev: the managed device object
  225. * @ret: the return code from the probe callback
  226. *
  227. * This function handles the explicit snd_card_free() call at the error from
  228. * the probe callback. It's just a small helper for simplifying the error
  229. * handling with the managed devices.
  230. *
  231. * Return: zero if successful, or a negative error code
  232. */
  233. int snd_card_free_on_error(struct device *dev, int ret)
  234. {
  235. struct snd_card *card;
  236. if (!ret)
  237. return 0;
  238. card = devres_find(dev, __snd_card_release, NULL, NULL);
  239. if (card)
  240. snd_card_free(card);
  241. return ret;
  242. }
  243. EXPORT_SYMBOL_GPL(snd_card_free_on_error);
  244. static int snd_card_init(struct snd_card *card, struct device *parent,
  245. int idx, const char *xid, struct module *module,
  246. size_t extra_size)
  247. {
  248. int err;
  249. if (extra_size > 0)
  250. card->private_data = (char *)card + sizeof(struct snd_card);
  251. if (xid)
  252. strscpy(card->id, xid, sizeof(card->id));
  253. err = 0;
  254. scoped_guard(mutex, &snd_card_mutex) {
  255. if (idx < 0) /* first check the matching module-name slot */
  256. idx = get_slot_from_bitmask(idx, module_slot_match, module);
  257. if (idx < 0) /* if not matched, assign an empty slot */
  258. idx = get_slot_from_bitmask(idx, check_empty_slot, module);
  259. if (idx < 0)
  260. err = -ENODEV;
  261. else if (idx < snd_ecards_limit) {
  262. if (test_bit(idx, snd_cards_lock))
  263. err = -EBUSY; /* invalid */
  264. } else if (idx >= SNDRV_CARDS)
  265. err = -ENODEV;
  266. if (!err) {
  267. set_bit(idx, snd_cards_lock); /* lock it */
  268. if (idx >= snd_ecards_limit)
  269. snd_ecards_limit = idx + 1; /* increase the limit */
  270. }
  271. }
  272. if (err < 0) {
  273. dev_err(parent, "cannot find the slot for index %d (range 0-%i), error: %d\n",
  274. idx, snd_ecards_limit - 1, err);
  275. if (!card->managed)
  276. kfree(card); /* manually free here, as no destructor called */
  277. return err;
  278. }
  279. card->dev = parent;
  280. card->number = idx;
  281. WARN_ON(IS_MODULE(CONFIG_SND) && !module);
  282. card->module = module;
  283. INIT_LIST_HEAD(&card->devices);
  284. init_rwsem(&card->controls_rwsem);
  285. rwlock_init(&card->controls_rwlock);
  286. INIT_LIST_HEAD(&card->controls);
  287. INIT_LIST_HEAD(&card->ctl_files);
  288. #ifdef CONFIG_SND_CTL_FAST_LOOKUP
  289. xa_init(&card->ctl_numids);
  290. xa_init(&card->ctl_hash);
  291. #endif
  292. spin_lock_init(&card->files_lock);
  293. INIT_LIST_HEAD(&card->files_list);
  294. mutex_init(&card->memory_mutex);
  295. #ifdef CONFIG_PM
  296. init_waitqueue_head(&card->power_sleep);
  297. init_waitqueue_head(&card->power_ref_sleep);
  298. atomic_set(&card->power_ref, 0);
  299. #endif
  300. init_waitqueue_head(&card->remove_sleep);
  301. card->sync_irq = -1;
  302. device_initialize(&card->card_dev);
  303. card->card_dev.parent = parent;
  304. card->card_dev.class = &sound_class;
  305. card->card_dev.release = release_card_device;
  306. card->card_dev.groups = card->dev_groups;
  307. card->dev_groups[0] = &card_dev_attr_group;
  308. err = kobject_set_name(&card->card_dev.kobj, "card%d", idx);
  309. if (err < 0)
  310. goto __error;
  311. snprintf(card->irq_descr, sizeof(card->irq_descr), "%s:%s",
  312. dev_driver_string(card->dev), dev_name(&card->card_dev));
  313. /* the control interface cannot be accessed from the user space until */
  314. /* snd_cards_bitmask and snd_cards are set with snd_card_register */
  315. err = snd_ctl_create(card);
  316. if (err < 0) {
  317. dev_err(parent, "unable to register control minors\n");
  318. goto __error;
  319. }
  320. err = snd_info_card_create(card);
  321. if (err < 0) {
  322. dev_err(parent, "unable to create card info\n");
  323. goto __error_ctl;
  324. }
  325. #ifdef CONFIG_SND_DEBUG
  326. card->debugfs_root = debugfs_create_dir(dev_name(&card->card_dev),
  327. sound_debugfs_root);
  328. #endif
  329. return 0;
  330. __error_ctl:
  331. snd_device_free_all(card);
  332. __error:
  333. put_device(&card->card_dev);
  334. return err;
  335. }
  336. /**
  337. * snd_card_ref - Get the card object from the index
  338. * @idx: the card index
  339. *
  340. * Returns a card object corresponding to the given index or NULL if not found.
  341. * Release the object via snd_card_unref().
  342. *
  343. * Return: a card object or NULL
  344. */
  345. struct snd_card *snd_card_ref(int idx)
  346. {
  347. struct snd_card *card;
  348. guard(mutex)(&snd_card_mutex);
  349. card = snd_cards[idx];
  350. if (card)
  351. get_device(&card->card_dev);
  352. return card;
  353. }
  354. EXPORT_SYMBOL_GPL(snd_card_ref);
  355. /* return non-zero if a card is already locked */
  356. int snd_card_locked(int card)
  357. {
  358. guard(mutex)(&snd_card_mutex);
  359. return test_bit(card, snd_cards_lock);
  360. }
  361. static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
  362. {
  363. return -ENODEV;
  364. }
  365. static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
  366. size_t count, loff_t *offset)
  367. {
  368. return -ENODEV;
  369. }
  370. static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
  371. size_t count, loff_t *offset)
  372. {
  373. return -ENODEV;
  374. }
  375. static int snd_disconnect_release(struct inode *inode, struct file *file)
  376. {
  377. struct snd_monitor_file *df = NULL, *_df;
  378. scoped_guard(spinlock, &shutdown_lock) {
  379. list_for_each_entry(_df, &shutdown_files, shutdown_list) {
  380. if (_df->file == file) {
  381. df = _df;
  382. list_del_init(&df->shutdown_list);
  383. break;
  384. }
  385. }
  386. }
  387. if (likely(df)) {
  388. if ((file->f_flags & FASYNC) && df->disconnected_f_op->fasync)
  389. df->disconnected_f_op->fasync(-1, file, 0);
  390. return df->disconnected_f_op->release(inode, file);
  391. }
  392. panic("%s(%p, %p) failed!", __func__, inode, file);
  393. }
  394. static __poll_t snd_disconnect_poll(struct file * file, poll_table * wait)
  395. {
  396. return EPOLLERR | EPOLLNVAL;
  397. }
  398. static long snd_disconnect_ioctl(struct file *file,
  399. unsigned int cmd, unsigned long arg)
  400. {
  401. return -ENODEV;
  402. }
  403. static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
  404. {
  405. return -ENODEV;
  406. }
  407. static int snd_disconnect_fasync(int fd, struct file *file, int on)
  408. {
  409. return -ENODEV;
  410. }
  411. static const struct file_operations snd_shutdown_f_ops =
  412. {
  413. .owner = THIS_MODULE,
  414. .llseek = snd_disconnect_llseek,
  415. .read = snd_disconnect_read,
  416. .write = snd_disconnect_write,
  417. .release = snd_disconnect_release,
  418. .poll = snd_disconnect_poll,
  419. .unlocked_ioctl = snd_disconnect_ioctl,
  420. #ifdef CONFIG_COMPAT
  421. .compat_ioctl = snd_disconnect_ioctl,
  422. #endif
  423. .mmap = snd_disconnect_mmap,
  424. .fasync = snd_disconnect_fasync
  425. };
  426. /**
  427. * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
  428. * @card: soundcard structure
  429. *
  430. * Disconnects all APIs from the file-operations (user space).
  431. *
  432. * Return: Zero, otherwise a negative error code.
  433. *
  434. * Note: The current implementation replaces all active file->f_op with special
  435. * dummy file operations (they do nothing except release).
  436. */
  437. void snd_card_disconnect(struct snd_card *card)
  438. {
  439. struct snd_monitor_file *mfile;
  440. if (!card)
  441. return;
  442. scoped_guard(spinlock, &card->files_lock) {
  443. if (card->shutdown)
  444. return;
  445. card->shutdown = 1;
  446. /* replace file->f_op with special dummy operations */
  447. list_for_each_entry(mfile, &card->files_list, list) {
  448. /* it's critical part, use endless loop */
  449. /* we have no room to fail */
  450. mfile->disconnected_f_op = mfile->file->f_op;
  451. scoped_guard(spinlock, &shutdown_lock)
  452. list_add(&mfile->shutdown_list, &shutdown_files);
  453. mfile->file->f_op = &snd_shutdown_f_ops;
  454. fops_get(mfile->file->f_op);
  455. }
  456. }
  457. #ifdef CONFIG_PM
  458. /* wake up sleepers here before other callbacks for avoiding potential
  459. * deadlocks with other locks (e.g. in kctls);
  460. * then this notifies the shutdown and sleepers would abort immediately
  461. */
  462. wake_up_all(&card->power_sleep);
  463. #endif
  464. /* notify all connected devices about disconnection */
  465. /* at this point, they cannot respond to any calls except release() */
  466. #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
  467. if (snd_mixer_oss_notify_callback)
  468. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
  469. #endif
  470. /* notify all devices that we are disconnected */
  471. snd_device_disconnect_all(card);
  472. if (card->sync_irq > 0)
  473. synchronize_irq(card->sync_irq);
  474. snd_info_card_disconnect(card);
  475. #ifdef CONFIG_SND_DEBUG
  476. debugfs_remove(card->debugfs_root);
  477. card->debugfs_root = NULL;
  478. #endif
  479. if (card->registered) {
  480. device_del(&card->card_dev);
  481. card->registered = false;
  482. }
  483. /* disable fops (user space) operations for ALSA API */
  484. scoped_guard(mutex, &snd_card_mutex) {
  485. snd_cards[card->number] = NULL;
  486. clear_bit(card->number, snd_cards_lock);
  487. }
  488. snd_power_sync_ref(card);
  489. }
  490. EXPORT_SYMBOL(snd_card_disconnect);
  491. /**
  492. * snd_card_disconnect_sync - disconnect card and wait until files get closed
  493. * @card: card object to disconnect
  494. *
  495. * This calls snd_card_disconnect() for disconnecting all belonging components
  496. * and waits until all pending files get closed.
  497. * It assures that all accesses from user-space finished so that the driver
  498. * can release its resources gracefully.
  499. */
  500. void snd_card_disconnect_sync(struct snd_card *card)
  501. {
  502. snd_card_disconnect(card);
  503. guard(spinlock_irq)(&card->files_lock);
  504. wait_event_lock_irq(card->remove_sleep,
  505. list_empty(&card->files_list),
  506. card->files_lock);
  507. }
  508. EXPORT_SYMBOL_GPL(snd_card_disconnect_sync);
  509. static int snd_card_do_free(struct snd_card *card)
  510. {
  511. card->releasing = true;
  512. #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
  513. if (snd_mixer_oss_notify_callback)
  514. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
  515. #endif
  516. snd_device_free_all(card);
  517. if (card->private_free)
  518. card->private_free(card);
  519. if (snd_info_card_free(card) < 0) {
  520. dev_warn(card->dev, "unable to free card info\n");
  521. /* Not fatal error */
  522. }
  523. if (card->release_completion)
  524. complete(card->release_completion);
  525. if (!card->managed)
  526. kfree(card);
  527. return 0;
  528. }
  529. /**
  530. * snd_card_free_when_closed - Disconnect the card, free it later eventually
  531. * @card: soundcard structure
  532. *
  533. * Unlike snd_card_free(), this function doesn't try to release the card
  534. * resource immediately, but tries to disconnect at first. When the card
  535. * is still in use, the function returns before freeing the resources.
  536. * The card resources will be freed when the refcount gets to zero.
  537. *
  538. * Return: zero if successful, or a negative error code
  539. */
  540. void snd_card_free_when_closed(struct snd_card *card)
  541. {
  542. if (!card)
  543. return;
  544. snd_card_disconnect(card);
  545. put_device(&card->card_dev);
  546. return;
  547. }
  548. EXPORT_SYMBOL(snd_card_free_when_closed);
  549. /**
  550. * snd_card_free - frees given soundcard structure
  551. * @card: soundcard structure
  552. *
  553. * This function releases the soundcard structure and the all assigned
  554. * devices automatically. That is, you don't have to release the devices
  555. * by yourself.
  556. *
  557. * This function waits until the all resources are properly released.
  558. *
  559. * Return: Zero. Frees all associated devices and frees the control
  560. * interface associated to given soundcard.
  561. */
  562. void snd_card_free(struct snd_card *card)
  563. {
  564. DECLARE_COMPLETION_ONSTACK(released);
  565. /* The call of snd_card_free() is allowed from various code paths;
  566. * a manual call from the driver and the call via devres_free, and
  567. * we need to avoid double-free. Moreover, the release via devres
  568. * may call snd_card_free() twice due to its nature, we need to have
  569. * the check here at the beginning.
  570. */
  571. if (card->releasing)
  572. return;
  573. card->release_completion = &released;
  574. snd_card_free_when_closed(card);
  575. /* wait, until all devices are ready for the free operation */
  576. wait_for_completion(&released);
  577. }
  578. EXPORT_SYMBOL(snd_card_free);
  579. /* check, if the character is in the valid ASCII range */
  580. static inline bool safe_ascii_char(char c)
  581. {
  582. return isascii(c) && isalnum(c);
  583. }
  584. /* retrieve the last word of shortname or longname */
  585. static const char *retrieve_id_from_card_name(const char *name)
  586. {
  587. const char *spos = name;
  588. while (*name) {
  589. if (isspace(*name) && safe_ascii_char(name[1]))
  590. spos = name + 1;
  591. name++;
  592. }
  593. return spos;
  594. }
  595. /* return true if the given id string doesn't conflict any other card ids */
  596. static bool card_id_ok(struct snd_card *card, const char *id)
  597. {
  598. int i;
  599. if (!snd_info_check_reserved_words(id))
  600. return false;
  601. for (i = 0; i < snd_ecards_limit; i++) {
  602. if (snd_cards[i] && snd_cards[i] != card &&
  603. !strcmp(snd_cards[i]->id, id))
  604. return false;
  605. }
  606. return true;
  607. }
  608. /* copy to card->id only with valid letters from nid */
  609. static void copy_valid_id_string(struct snd_card *card, const char *src,
  610. const char *nid)
  611. {
  612. char *id = card->id;
  613. while (*nid && !safe_ascii_char(*nid))
  614. nid++;
  615. if (isdigit(*nid))
  616. *id++ = isalpha(*src) ? *src : 'D';
  617. while (*nid && (size_t)(id - card->id) < sizeof(card->id) - 1) {
  618. if (safe_ascii_char(*nid))
  619. *id++ = *nid;
  620. nid++;
  621. }
  622. *id = 0;
  623. }
  624. /* Set card->id from the given string
  625. * If the string conflicts with other ids, add a suffix to make it unique.
  626. */
  627. static void snd_card_set_id_no_lock(struct snd_card *card, const char *src,
  628. const char *nid)
  629. {
  630. int len, loops;
  631. bool is_default = false;
  632. char *id;
  633. copy_valid_id_string(card, src, nid);
  634. id = card->id;
  635. again:
  636. /* use "Default" for obviously invalid strings
  637. * ("card" conflicts with proc directories)
  638. */
  639. if (!*id || !strncmp(id, "card", 4)) {
  640. strscpy(card->id, "Default");
  641. is_default = true;
  642. }
  643. len = strlen(id);
  644. for (loops = 0; loops < SNDRV_CARDS; loops++) {
  645. char sfxstr[5]; /* "_012" */
  646. int sfxlen, slen;
  647. if (card_id_ok(card, id))
  648. return; /* OK */
  649. /* Add _XYZ suffix */
  650. sfxlen = scnprintf(sfxstr, sizeof(sfxstr), "_%X", loops + 1);
  651. if (len + sfxlen >= sizeof(card->id))
  652. slen = sizeof(card->id) - sfxlen - 1;
  653. else
  654. slen = len;
  655. strscpy(id + slen, sfxstr, sizeof(card->id) - slen);
  656. }
  657. /* fallback to the default id */
  658. if (!is_default) {
  659. *id = 0;
  660. goto again;
  661. }
  662. /* last resort... */
  663. dev_err(card->dev, "unable to set card id (%s)\n", id);
  664. if (card->proc_root->name)
  665. strscpy(card->id, card->proc_root->name, sizeof(card->id));
  666. }
  667. /**
  668. * snd_card_set_id - set card identification name
  669. * @card: soundcard structure
  670. * @nid: new identification string
  671. *
  672. * This function sets the card identification and checks for name
  673. * collisions.
  674. */
  675. void snd_card_set_id(struct snd_card *card, const char *nid)
  676. {
  677. /* check if user specified own card->id */
  678. if (card->id[0] != '\0')
  679. return;
  680. guard(mutex)(&snd_card_mutex);
  681. snd_card_set_id_no_lock(card, nid, nid);
  682. }
  683. EXPORT_SYMBOL(snd_card_set_id);
  684. static ssize_t id_show(struct device *dev,
  685. struct device_attribute *attr, char *buf)
  686. {
  687. struct snd_card *card = container_of(dev, struct snd_card, card_dev);
  688. return sysfs_emit(buf, "%s\n", card->id);
  689. }
  690. static ssize_t id_store(struct device *dev, struct device_attribute *attr,
  691. const char *buf, size_t count)
  692. {
  693. struct snd_card *card = container_of(dev, struct snd_card, card_dev);
  694. char buf1[sizeof(card->id)];
  695. size_t copy = count > sizeof(card->id) - 1 ?
  696. sizeof(card->id) - 1 : count;
  697. size_t idx;
  698. int c;
  699. for (idx = 0; idx < copy; idx++) {
  700. c = buf[idx];
  701. if (!safe_ascii_char(c) && c != '_' && c != '-')
  702. return -EINVAL;
  703. }
  704. memcpy(buf1, buf, copy);
  705. buf1[copy] = '\0';
  706. guard(mutex)(&snd_card_mutex);
  707. if (!card_id_ok(NULL, buf1))
  708. return -EEXIST;
  709. strscpy(card->id, buf1);
  710. snd_info_card_id_change(card);
  711. return count;
  712. }
  713. static DEVICE_ATTR_RW(id);
  714. static ssize_t number_show(struct device *dev,
  715. struct device_attribute *attr, char *buf)
  716. {
  717. struct snd_card *card = container_of(dev, struct snd_card, card_dev);
  718. return sysfs_emit(buf, "%i\n", card->number);
  719. }
  720. static DEVICE_ATTR_RO(number);
  721. static struct attribute *card_dev_attrs[] = {
  722. &dev_attr_id.attr,
  723. &dev_attr_number.attr,
  724. NULL
  725. };
  726. static const struct attribute_group card_dev_attr_group = {
  727. .attrs = card_dev_attrs,
  728. };
  729. /**
  730. * snd_card_add_dev_attr - Append a new sysfs attribute group to card
  731. * @card: card instance
  732. * @group: attribute group to append
  733. *
  734. * Return: zero if successful, or a negative error code
  735. */
  736. int snd_card_add_dev_attr(struct snd_card *card,
  737. const struct attribute_group *group)
  738. {
  739. int i;
  740. /* loop for (arraysize-1) here to keep NULL at the last entry */
  741. for (i = 0; i < ARRAY_SIZE(card->dev_groups) - 1; i++) {
  742. if (!card->dev_groups[i]) {
  743. card->dev_groups[i] = group;
  744. return 0;
  745. }
  746. }
  747. dev_err(card->dev, "Too many groups assigned\n");
  748. return -ENOSPC;
  749. }
  750. EXPORT_SYMBOL_GPL(snd_card_add_dev_attr);
  751. static void trigger_card_free(void *data)
  752. {
  753. snd_card_free(data);
  754. }
  755. /**
  756. * snd_card_register - register the soundcard
  757. * @card: soundcard structure
  758. *
  759. * This function registers all the devices assigned to the soundcard.
  760. * Until calling this, the ALSA control interface is blocked from the
  761. * external accesses. Thus, you should call this function at the end
  762. * of the initialization of the card.
  763. *
  764. * Return: Zero otherwise a negative error code if the registration failed.
  765. */
  766. int snd_card_register(struct snd_card *card)
  767. {
  768. int err;
  769. if (snd_BUG_ON(!card))
  770. return -EINVAL;
  771. if (!card->registered) {
  772. err = device_add(&card->card_dev);
  773. if (err < 0)
  774. return err;
  775. card->registered = true;
  776. } else {
  777. if (card->managed)
  778. devm_remove_action(card->dev, trigger_card_free, card);
  779. }
  780. if (card->managed) {
  781. err = devm_add_action(card->dev, trigger_card_free, card);
  782. if (err < 0)
  783. return err;
  784. }
  785. err = snd_device_register_all(card);
  786. if (err < 0)
  787. return err;
  788. scoped_guard(mutex, &snd_card_mutex) {
  789. if (snd_cards[card->number]) {
  790. /* already registered */
  791. return snd_info_card_register(card); /* register pending info */
  792. }
  793. if (*card->id) {
  794. /* make a unique id name from the given string */
  795. char tmpid[sizeof(card->id)];
  796. memcpy(tmpid, card->id, sizeof(card->id));
  797. snd_card_set_id_no_lock(card, tmpid, tmpid);
  798. } else {
  799. /* create an id from either shortname or longname */
  800. const char *src;
  801. src = *card->shortname ? card->shortname : card->longname;
  802. snd_card_set_id_no_lock(card, src,
  803. retrieve_id_from_card_name(src));
  804. }
  805. snd_cards[card->number] = card;
  806. }
  807. err = snd_info_card_register(card);
  808. if (err < 0)
  809. return err;
  810. #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
  811. if (snd_mixer_oss_notify_callback)
  812. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
  813. #endif
  814. return 0;
  815. }
  816. EXPORT_SYMBOL(snd_card_register);
  817. #ifdef CONFIG_SND_PROC_FS
  818. static void snd_card_info_read(struct snd_info_entry *entry,
  819. struct snd_info_buffer *buffer)
  820. {
  821. int idx, count;
  822. struct snd_card *card;
  823. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  824. guard(mutex)(&snd_card_mutex);
  825. card = snd_cards[idx];
  826. if (card) {
  827. count++;
  828. snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
  829. idx,
  830. card->id,
  831. card->driver,
  832. card->shortname);
  833. snd_iprintf(buffer, " %s\n",
  834. card->longname);
  835. }
  836. }
  837. if (!count)
  838. snd_iprintf(buffer, "--- no soundcards ---\n");
  839. }
  840. #ifdef CONFIG_SND_OSSEMUL
  841. void snd_card_info_read_oss(struct snd_info_buffer *buffer)
  842. {
  843. int idx, count;
  844. struct snd_card *card;
  845. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  846. guard(mutex)(&snd_card_mutex);
  847. card = snd_cards[idx];
  848. if (card) {
  849. count++;
  850. snd_iprintf(buffer, "%s\n", card->longname);
  851. }
  852. }
  853. if (!count) {
  854. snd_iprintf(buffer, "--- no soundcards ---\n");
  855. }
  856. }
  857. #endif
  858. #ifdef CONFIG_MODULES
  859. static void snd_card_module_info_read(struct snd_info_entry *entry,
  860. struct snd_info_buffer *buffer)
  861. {
  862. int idx;
  863. struct snd_card *card;
  864. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  865. guard(mutex)(&snd_card_mutex);
  866. card = snd_cards[idx];
  867. if (card)
  868. snd_iprintf(buffer, "%2i %s\n",
  869. idx, card->module->name);
  870. }
  871. }
  872. #endif
  873. int __init snd_card_info_init(void)
  874. {
  875. struct snd_info_entry *entry;
  876. entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
  877. if (! entry)
  878. return -ENOMEM;
  879. entry->c.text.read = snd_card_info_read;
  880. if (snd_info_register(entry) < 0)
  881. return -ENOMEM; /* freed in error path */
  882. #ifdef CONFIG_MODULES
  883. entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
  884. if (!entry)
  885. return -ENOMEM;
  886. entry->c.text.read = snd_card_module_info_read;
  887. if (snd_info_register(entry) < 0)
  888. return -ENOMEM; /* freed in error path */
  889. #endif
  890. return 0;
  891. }
  892. #endif /* CONFIG_SND_PROC_FS */
  893. /**
  894. * snd_component_add - add a component string
  895. * @card: soundcard structure
  896. * @component: the component id string
  897. *
  898. * This function adds the component id string to the supported list.
  899. * The component can be referred from the alsa-lib.
  900. *
  901. * Return: Zero otherwise a negative error code.
  902. */
  903. int snd_component_add(struct snd_card *card, const char *component)
  904. {
  905. char *ptr;
  906. int len = strlen(component);
  907. ptr = strstr(card->components, component);
  908. if (ptr != NULL) {
  909. if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
  910. return 1;
  911. }
  912. if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
  913. snd_BUG();
  914. return -ENOMEM;
  915. }
  916. if (card->components[0] != '\0')
  917. strcat(card->components, " ");
  918. strcat(card->components, component);
  919. return 0;
  920. }
  921. EXPORT_SYMBOL(snd_component_add);
  922. /**
  923. * snd_card_file_add - add the file to the file list of the card
  924. * @card: soundcard structure
  925. * @file: file pointer
  926. *
  927. * This function adds the file to the file linked-list of the card.
  928. * This linked-list is used to keep tracking the connection state,
  929. * and to avoid the release of busy resources by hotplug.
  930. *
  931. * Return: zero or a negative error code.
  932. */
  933. int snd_card_file_add(struct snd_card *card, struct file *file)
  934. {
  935. struct snd_monitor_file *mfile;
  936. mfile = kmalloc_obj(*mfile);
  937. if (mfile == NULL)
  938. return -ENOMEM;
  939. mfile->file = file;
  940. mfile->disconnected_f_op = NULL;
  941. INIT_LIST_HEAD(&mfile->shutdown_list);
  942. guard(spinlock)(&card->files_lock);
  943. if (card->shutdown) {
  944. kfree(mfile);
  945. return -ENODEV;
  946. }
  947. list_add(&mfile->list, &card->files_list);
  948. get_device(&card->card_dev);
  949. return 0;
  950. }
  951. EXPORT_SYMBOL(snd_card_file_add);
  952. /**
  953. * snd_card_file_remove - remove the file from the file list
  954. * @card: soundcard structure
  955. * @file: file pointer
  956. *
  957. * This function removes the file formerly added to the card via
  958. * snd_card_file_add() function.
  959. * If all files are removed and snd_card_free_when_closed() was
  960. * called beforehand, it processes the pending release of
  961. * resources.
  962. *
  963. * Return: Zero or a negative error code.
  964. */
  965. int snd_card_file_remove(struct snd_card *card, struct file *file)
  966. {
  967. struct snd_monitor_file *mfile, *found = NULL;
  968. scoped_guard(spinlock, &card->files_lock) {
  969. list_for_each_entry(mfile, &card->files_list, list) {
  970. if (mfile->file == file) {
  971. list_del(&mfile->list);
  972. scoped_guard(spinlock, &shutdown_lock)
  973. list_del(&mfile->shutdown_list);
  974. if (mfile->disconnected_f_op)
  975. fops_put(mfile->disconnected_f_op);
  976. found = mfile;
  977. break;
  978. }
  979. }
  980. if (list_empty(&card->files_list))
  981. wake_up_all(&card->remove_sleep);
  982. }
  983. if (!found) {
  984. dev_err(card->dev, "card file remove problem (%p)\n", file);
  985. return -ENOENT;
  986. }
  987. kfree(found);
  988. put_device(&card->card_dev);
  989. return 0;
  990. }
  991. EXPORT_SYMBOL(snd_card_file_remove);
  992. #ifdef CONFIG_PM
  993. /**
  994. * snd_power_ref_and_wait - wait until the card gets powered up
  995. * @card: soundcard structure
  996. *
  997. * Take the power_ref reference count of the given card, and
  998. * wait until the card gets powered up to SNDRV_CTL_POWER_D0 state.
  999. * The refcount is down again while sleeping until power-up, hence this
  1000. * function can be used for syncing the floating control ops accesses,
  1001. * typically around calling control ops.
  1002. *
  1003. * The caller needs to pull down the refcount via snd_power_unref() later
  1004. * no matter whether the error is returned from this function or not.
  1005. *
  1006. * Return: Zero if successful, or a negative error code.
  1007. */
  1008. int snd_power_ref_and_wait(struct snd_card *card)
  1009. {
  1010. snd_power_ref(card);
  1011. if (snd_power_get_state(card) == SNDRV_CTL_POWER_D0)
  1012. return 0;
  1013. wait_event_cmd(card->power_sleep,
  1014. card->shutdown ||
  1015. snd_power_get_state(card) == SNDRV_CTL_POWER_D0,
  1016. snd_power_unref(card), snd_power_ref(card));
  1017. return card->shutdown ? -ENODEV : 0;
  1018. }
  1019. EXPORT_SYMBOL_GPL(snd_power_ref_and_wait);
  1020. /**
  1021. * snd_power_wait - wait until the card gets powered up (old form)
  1022. * @card: soundcard structure
  1023. *
  1024. * Wait until the card gets powered up to SNDRV_CTL_POWER_D0 state.
  1025. *
  1026. * Return: Zero if successful, or a negative error code.
  1027. */
  1028. int snd_power_wait(struct snd_card *card)
  1029. {
  1030. int ret;
  1031. ret = snd_power_ref_and_wait(card);
  1032. snd_power_unref(card);
  1033. return ret;
  1034. }
  1035. EXPORT_SYMBOL(snd_power_wait);
  1036. #endif /* CONFIG_PM */