init.c 27 KB

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