init.c 27 KB

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