init.c 25 KB

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