init.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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(sizeof(*dev), GFP_KERNEL);
  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. strcpy(id, "Default");
  641. is_default = true;
  642. }
  643. len = strlen(id);
  644. for (loops = 0; loops < SNDRV_CARDS; loops++) {
  645. char *spos;
  646. char sfxstr[5]; /* "_012" */
  647. int sfxlen;
  648. if (card_id_ok(card, id))
  649. return; /* OK */
  650. /* Add _XYZ suffix */
  651. sprintf(sfxstr, "_%X", loops + 1);
  652. sfxlen = strlen(sfxstr);
  653. if (len + sfxlen >= sizeof(card->id))
  654. spos = id + sizeof(card->id) - sfxlen - 1;
  655. else
  656. spos = id + len;
  657. strcpy(spos, sfxstr);
  658. }
  659. /* fallback to the default id */
  660. if (!is_default) {
  661. *id = 0;
  662. goto again;
  663. }
  664. /* last resort... */
  665. dev_err(card->dev, "unable to set card id (%s)\n", id);
  666. if (card->proc_root->name)
  667. strscpy(card->id, card->proc_root->name, sizeof(card->id));
  668. }
  669. /**
  670. * snd_card_set_id - set card identification name
  671. * @card: soundcard structure
  672. * @nid: new identification string
  673. *
  674. * This function sets the card identification and checks for name
  675. * collisions.
  676. */
  677. void snd_card_set_id(struct snd_card *card, const char *nid)
  678. {
  679. /* check if user specified own card->id */
  680. if (card->id[0] != '\0')
  681. return;
  682. guard(mutex)(&snd_card_mutex);
  683. snd_card_set_id_no_lock(card, nid, nid);
  684. }
  685. EXPORT_SYMBOL(snd_card_set_id);
  686. static ssize_t id_show(struct device *dev,
  687. struct device_attribute *attr, char *buf)
  688. {
  689. struct snd_card *card = container_of(dev, struct snd_card, card_dev);
  690. return sysfs_emit(buf, "%s\n", card->id);
  691. }
  692. static ssize_t id_store(struct device *dev, struct device_attribute *attr,
  693. const char *buf, size_t count)
  694. {
  695. struct snd_card *card = container_of(dev, struct snd_card, card_dev);
  696. char buf1[sizeof(card->id)];
  697. size_t copy = count > sizeof(card->id) - 1 ?
  698. sizeof(card->id) - 1 : count;
  699. size_t idx;
  700. int c;
  701. for (idx = 0; idx < copy; idx++) {
  702. c = buf[idx];
  703. if (!safe_ascii_char(c) && c != '_' && c != '-')
  704. return -EINVAL;
  705. }
  706. memcpy(buf1, buf, copy);
  707. buf1[copy] = '\0';
  708. guard(mutex)(&snd_card_mutex);
  709. if (!card_id_ok(NULL, buf1))
  710. return -EEXIST;
  711. strcpy(card->id, buf1);
  712. snd_info_card_id_change(card);
  713. return count;
  714. }
  715. static DEVICE_ATTR_RW(id);
  716. static ssize_t number_show(struct device *dev,
  717. struct device_attribute *attr, char *buf)
  718. {
  719. struct snd_card *card = container_of(dev, struct snd_card, card_dev);
  720. return sysfs_emit(buf, "%i\n", card->number);
  721. }
  722. static DEVICE_ATTR_RO(number);
  723. static struct attribute *card_dev_attrs[] = {
  724. &dev_attr_id.attr,
  725. &dev_attr_number.attr,
  726. NULL
  727. };
  728. static const struct attribute_group card_dev_attr_group = {
  729. .attrs = card_dev_attrs,
  730. };
  731. /**
  732. * snd_card_add_dev_attr - Append a new sysfs attribute group to card
  733. * @card: card instance
  734. * @group: attribute group to append
  735. *
  736. * Return: zero if successful, or a negative error code
  737. */
  738. int snd_card_add_dev_attr(struct snd_card *card,
  739. const struct attribute_group *group)
  740. {
  741. int i;
  742. /* loop for (arraysize-1) here to keep NULL at the last entry */
  743. for (i = 0; i < ARRAY_SIZE(card->dev_groups) - 1; i++) {
  744. if (!card->dev_groups[i]) {
  745. card->dev_groups[i] = group;
  746. return 0;
  747. }
  748. }
  749. dev_err(card->dev, "Too many groups assigned\n");
  750. return -ENOSPC;
  751. }
  752. EXPORT_SYMBOL_GPL(snd_card_add_dev_attr);
  753. static void trigger_card_free(void *data)
  754. {
  755. snd_card_free(data);
  756. }
  757. /**
  758. * snd_card_register - register the soundcard
  759. * @card: soundcard structure
  760. *
  761. * This function registers all the devices assigned to the soundcard.
  762. * Until calling this, the ALSA control interface is blocked from the
  763. * external accesses. Thus, you should call this function at the end
  764. * of the initialization of the card.
  765. *
  766. * Return: Zero otherwise a negative error code if the registration failed.
  767. */
  768. int snd_card_register(struct snd_card *card)
  769. {
  770. int err;
  771. if (snd_BUG_ON(!card))
  772. return -EINVAL;
  773. if (!card->registered) {
  774. err = device_add(&card->card_dev);
  775. if (err < 0)
  776. return err;
  777. card->registered = true;
  778. } else {
  779. if (card->managed)
  780. devm_remove_action(card->dev, trigger_card_free, card);
  781. }
  782. if (card->managed) {
  783. err = devm_add_action(card->dev, trigger_card_free, card);
  784. if (err < 0)
  785. return err;
  786. }
  787. err = snd_device_register_all(card);
  788. if (err < 0)
  789. return err;
  790. scoped_guard(mutex, &snd_card_mutex) {
  791. if (snd_cards[card->number]) {
  792. /* already registered */
  793. return snd_info_card_register(card); /* register pending info */
  794. }
  795. if (*card->id) {
  796. /* make a unique id name from the given string */
  797. char tmpid[sizeof(card->id)];
  798. memcpy(tmpid, card->id, sizeof(card->id));
  799. snd_card_set_id_no_lock(card, tmpid, tmpid);
  800. } else {
  801. /* create an id from either shortname or longname */
  802. const char *src;
  803. src = *card->shortname ? card->shortname : card->longname;
  804. snd_card_set_id_no_lock(card, src,
  805. retrieve_id_from_card_name(src));
  806. }
  807. snd_cards[card->number] = card;
  808. }
  809. err = snd_info_card_register(card);
  810. if (err < 0)
  811. return err;
  812. #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
  813. if (snd_mixer_oss_notify_callback)
  814. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
  815. #endif
  816. return 0;
  817. }
  818. EXPORT_SYMBOL(snd_card_register);
  819. #ifdef CONFIG_SND_PROC_FS
  820. static void snd_card_info_read(struct snd_info_entry *entry,
  821. struct snd_info_buffer *buffer)
  822. {
  823. int idx, count;
  824. struct snd_card *card;
  825. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  826. guard(mutex)(&snd_card_mutex);
  827. card = snd_cards[idx];
  828. if (card) {
  829. count++;
  830. snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
  831. idx,
  832. card->id,
  833. card->driver,
  834. card->shortname);
  835. snd_iprintf(buffer, " %s\n",
  836. card->longname);
  837. }
  838. }
  839. if (!count)
  840. snd_iprintf(buffer, "--- no soundcards ---\n");
  841. }
  842. #ifdef CONFIG_SND_OSSEMUL
  843. void snd_card_info_read_oss(struct snd_info_buffer *buffer)
  844. {
  845. int idx, count;
  846. struct snd_card *card;
  847. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  848. guard(mutex)(&snd_card_mutex);
  849. card = snd_cards[idx];
  850. if (card) {
  851. count++;
  852. snd_iprintf(buffer, "%s\n", card->longname);
  853. }
  854. }
  855. if (!count) {
  856. snd_iprintf(buffer, "--- no soundcards ---\n");
  857. }
  858. }
  859. #endif
  860. #ifdef CONFIG_MODULES
  861. static void snd_card_module_info_read(struct snd_info_entry *entry,
  862. struct snd_info_buffer *buffer)
  863. {
  864. int idx;
  865. struct snd_card *card;
  866. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  867. guard(mutex)(&snd_card_mutex);
  868. card = snd_cards[idx];
  869. if (card)
  870. snd_iprintf(buffer, "%2i %s\n",
  871. idx, card->module->name);
  872. }
  873. }
  874. #endif
  875. int __init snd_card_info_init(void)
  876. {
  877. struct snd_info_entry *entry;
  878. entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
  879. if (! entry)
  880. return -ENOMEM;
  881. entry->c.text.read = snd_card_info_read;
  882. if (snd_info_register(entry) < 0)
  883. return -ENOMEM; /* freed in error path */
  884. #ifdef CONFIG_MODULES
  885. entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
  886. if (!entry)
  887. return -ENOMEM;
  888. entry->c.text.read = snd_card_module_info_read;
  889. if (snd_info_register(entry) < 0)
  890. return -ENOMEM; /* freed in error path */
  891. #endif
  892. return 0;
  893. }
  894. #endif /* CONFIG_SND_PROC_FS */
  895. /**
  896. * snd_component_add - add a component string
  897. * @card: soundcard structure
  898. * @component: the component id string
  899. *
  900. * This function adds the component id string to the supported list.
  901. * The component can be referred from the alsa-lib.
  902. *
  903. * Return: Zero otherwise a negative error code.
  904. */
  905. int snd_component_add(struct snd_card *card, const char *component)
  906. {
  907. char *ptr;
  908. int len = strlen(component);
  909. ptr = strstr(card->components, component);
  910. if (ptr != NULL) {
  911. if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
  912. return 1;
  913. }
  914. if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
  915. snd_BUG();
  916. return -ENOMEM;
  917. }
  918. if (card->components[0] != '\0')
  919. strcat(card->components, " ");
  920. strcat(card->components, component);
  921. return 0;
  922. }
  923. EXPORT_SYMBOL(snd_component_add);
  924. /**
  925. * snd_card_file_add - add the file to the file list of the card
  926. * @card: soundcard structure
  927. * @file: file pointer
  928. *
  929. * This function adds the file to the file linked-list of the card.
  930. * This linked-list is used to keep tracking the connection state,
  931. * and to avoid the release of busy resources by hotplug.
  932. *
  933. * Return: zero or a negative error code.
  934. */
  935. int snd_card_file_add(struct snd_card *card, struct file *file)
  936. {
  937. struct snd_monitor_file *mfile;
  938. mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
  939. if (mfile == NULL)
  940. return -ENOMEM;
  941. mfile->file = file;
  942. mfile->disconnected_f_op = NULL;
  943. INIT_LIST_HEAD(&mfile->shutdown_list);
  944. guard(spinlock)(&card->files_lock);
  945. if (card->shutdown) {
  946. kfree(mfile);
  947. return -ENODEV;
  948. }
  949. list_add(&mfile->list, &card->files_list);
  950. get_device(&card->card_dev);
  951. return 0;
  952. }
  953. EXPORT_SYMBOL(snd_card_file_add);
  954. /**
  955. * snd_card_file_remove - remove the file from the file list
  956. * @card: soundcard structure
  957. * @file: file pointer
  958. *
  959. * This function removes the file formerly added to the card via
  960. * snd_card_file_add() function.
  961. * If all files are removed and snd_card_free_when_closed() was
  962. * called beforehand, it processes the pending release of
  963. * resources.
  964. *
  965. * Return: Zero or a negative error code.
  966. */
  967. int snd_card_file_remove(struct snd_card *card, struct file *file)
  968. {
  969. struct snd_monitor_file *mfile, *found = NULL;
  970. scoped_guard(spinlock, &card->files_lock) {
  971. list_for_each_entry(mfile, &card->files_list, list) {
  972. if (mfile->file == file) {
  973. list_del(&mfile->list);
  974. scoped_guard(spinlock, &shutdown_lock)
  975. list_del(&mfile->shutdown_list);
  976. if (mfile->disconnected_f_op)
  977. fops_put(mfile->disconnected_f_op);
  978. found = mfile;
  979. break;
  980. }
  981. }
  982. if (list_empty(&card->files_list))
  983. wake_up_all(&card->remove_sleep);
  984. }
  985. if (!found) {
  986. dev_err(card->dev, "card file remove problem (%p)\n", file);
  987. return -ENOENT;
  988. }
  989. kfree(found);
  990. put_device(&card->card_dev);
  991. return 0;
  992. }
  993. EXPORT_SYMBOL(snd_card_file_remove);
  994. #ifdef CONFIG_PM
  995. /**
  996. * snd_power_ref_and_wait - wait until the card gets powered up
  997. * @card: soundcard structure
  998. *
  999. * Take the power_ref reference count of the given card, and
  1000. * wait until the card gets powered up to SNDRV_CTL_POWER_D0 state.
  1001. * The refcount is down again while sleeping until power-up, hence this
  1002. * function can be used for syncing the floating control ops accesses,
  1003. * typically around calling control ops.
  1004. *
  1005. * The caller needs to pull down the refcount via snd_power_unref() later
  1006. * no matter whether the error is returned from this function or not.
  1007. *
  1008. * Return: Zero if successful, or a negative error code.
  1009. */
  1010. int snd_power_ref_and_wait(struct snd_card *card)
  1011. {
  1012. snd_power_ref(card);
  1013. if (snd_power_get_state(card) == SNDRV_CTL_POWER_D0)
  1014. return 0;
  1015. wait_event_cmd(card->power_sleep,
  1016. card->shutdown ||
  1017. snd_power_get_state(card) == SNDRV_CTL_POWER_D0,
  1018. snd_power_unref(card), snd_power_ref(card));
  1019. return card->shutdown ? -ENODEV : 0;
  1020. }
  1021. EXPORT_SYMBOL_GPL(snd_power_ref_and_wait);
  1022. /**
  1023. * snd_power_wait - wait until the card gets powered up (old form)
  1024. * @card: soundcard structure
  1025. *
  1026. * Wait until the card gets powered up to SNDRV_CTL_POWER_D0 state.
  1027. *
  1028. * Return: Zero if successful, or a negative error code.
  1029. */
  1030. int snd_power_wait(struct snd_card *card)
  1031. {
  1032. int ret;
  1033. ret = snd_power_ref_and_wait(card);
  1034. snd_power_unref(card);
  1035. return ret;
  1036. }
  1037. EXPORT_SYMBOL(snd_power_wait);
  1038. #endif /* CONFIG_PM */