init.c 25 KB

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