init.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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 <sound/core.h>
  31. #include <sound/control.h>
  32. #include <sound/info.h>
  33. /* monitor files for graceful shutdown (hotplug) */
  34. struct snd_monitor_file {
  35. struct file *file;
  36. const struct file_operations *disconnected_f_op;
  37. struct list_head shutdown_list; /* still need to shutdown */
  38. struct list_head list; /* link of monitor files */
  39. };
  40. static DEFINE_SPINLOCK(shutdown_lock);
  41. static LIST_HEAD(shutdown_files);
  42. static const struct file_operations snd_shutdown_f_ops;
  43. /* locked for registering/using */
  44. static DECLARE_BITMAP(snd_cards_lock, SNDRV_CARDS);
  45. struct snd_card *snd_cards[SNDRV_CARDS];
  46. EXPORT_SYMBOL(snd_cards);
  47. static DEFINE_MUTEX(snd_card_mutex);
  48. static char *slots[SNDRV_CARDS];
  49. module_param_array(slots, charp, NULL, 0444);
  50. MODULE_PARM_DESC(slots, "Module names assigned to the slots.");
  51. /* return non-zero if the given index is reserved for the given
  52. * module via slots option
  53. */
  54. static int module_slot_match(struct module *module, int idx)
  55. {
  56. int match = 1;
  57. #ifdef MODULE
  58. const char *s1, *s2;
  59. if (!module || !*module->name || !slots[idx])
  60. return 0;
  61. s1 = module->name;
  62. s2 = slots[idx];
  63. if (*s2 == '!') {
  64. match = 0; /* negative match */
  65. s2++;
  66. }
  67. /* compare module name strings
  68. * hyphens are handled as equivalent with underscore
  69. */
  70. for (;;) {
  71. char c1 = *s1++;
  72. char c2 = *s2++;
  73. if (c1 == '-')
  74. c1 = '_';
  75. if (c2 == '-')
  76. c2 = '_';
  77. if (c1 != c2)
  78. return !match;
  79. if (!c1)
  80. break;
  81. }
  82. #endif /* MODULE */
  83. return match;
  84. }
  85. #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
  86. int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
  87. EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
  88. #endif
  89. #ifdef CONFIG_PROC_FS
  90. static void snd_card_id_read(struct snd_info_entry *entry,
  91. struct snd_info_buffer *buffer)
  92. {
  93. snd_iprintf(buffer, "%s\n", entry->card->id);
  94. }
  95. static inline int init_info_for_card(struct snd_card *card)
  96. {
  97. int err;
  98. struct snd_info_entry *entry;
  99. if ((err = snd_info_card_register(card)) < 0) {
  100. snd_printd("unable to create card info\n");
  101. return err;
  102. }
  103. if ((entry = snd_info_create_card_entry(card, "id", card->proc_root)) == NULL) {
  104. snd_printd("unable to create card entry\n");
  105. return err;
  106. }
  107. entry->c.text.read = snd_card_id_read;
  108. if (snd_info_register(entry) < 0) {
  109. snd_info_free_entry(entry);
  110. entry = NULL;
  111. }
  112. card->proc_id = entry;
  113. return 0;
  114. }
  115. #else /* !CONFIG_PROC_FS */
  116. #define init_info_for_card(card)
  117. #endif
  118. static int check_empty_slot(struct module *module, int slot)
  119. {
  120. return !slots[slot] || !*slots[slot];
  121. }
  122. /* return an empty slot number (>= 0) found in the given bitmask @mask.
  123. * @mask == -1 == 0xffffffff means: take any free slot up to 32
  124. * when no slot is available, return the original @mask as is.
  125. */
  126. static int get_slot_from_bitmask(int mask, int (*check)(struct module *, int),
  127. struct module *module)
  128. {
  129. int slot;
  130. for (slot = 0; slot < SNDRV_CARDS; slot++) {
  131. if (slot < 32 && !(mask & (1U << slot)))
  132. continue;
  133. if (!test_bit(slot, snd_cards_lock)) {
  134. if (check(module, slot))
  135. return slot; /* found */
  136. }
  137. }
  138. return mask; /* unchanged */
  139. }
  140. static int snd_card_do_free(struct snd_card *card);
  141. static const struct attribute_group *card_dev_attr_groups[];
  142. static void release_card_device(struct device *dev)
  143. {
  144. snd_card_do_free(dev_to_snd_card(dev));
  145. }
  146. /**
  147. * snd_card_new - create and initialize a soundcard structure
  148. * @parent: the parent device object
  149. * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
  150. * @xid: card identification (ASCII string)
  151. * @module: top level module for locking
  152. * @extra_size: allocate this extra size after the main soundcard structure
  153. * @card_ret: the pointer to store the created card instance
  154. *
  155. * Creates and initializes a soundcard structure.
  156. *
  157. * The function allocates snd_card instance via kzalloc with the given
  158. * space for the driver to use freely. The allocated struct is stored
  159. * in the given card_ret pointer.
  160. *
  161. * Return: Zero if successful or a negative error code.
  162. */
  163. int snd_card_new(struct device *parent, int idx, const char *xid,
  164. struct module *module, int extra_size,
  165. struct snd_card **card_ret)
  166. {
  167. struct snd_card *card;
  168. int err;
  169. if (snd_BUG_ON(!card_ret))
  170. return -EINVAL;
  171. *card_ret = NULL;
  172. if (extra_size < 0)
  173. extra_size = 0;
  174. card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
  175. if (!card)
  176. return -ENOMEM;
  177. if (extra_size > 0)
  178. card->private_data = (char *)card + sizeof(struct snd_card);
  179. if (xid)
  180. strlcpy(card->id, xid, sizeof(card->id));
  181. err = 0;
  182. mutex_lock(&snd_card_mutex);
  183. if (idx < 0) /* first check the matching module-name slot */
  184. idx = get_slot_from_bitmask(idx, module_slot_match, module);
  185. if (idx < 0) /* if not matched, assign an empty slot */
  186. idx = get_slot_from_bitmask(idx, check_empty_slot, module);
  187. if (idx < 0)
  188. err = -ENODEV;
  189. else if (idx < snd_ecards_limit) {
  190. if (test_bit(idx, snd_cards_lock))
  191. err = -EBUSY; /* invalid */
  192. } else if (idx >= SNDRV_CARDS)
  193. err = -ENODEV;
  194. if (err < 0) {
  195. mutex_unlock(&snd_card_mutex);
  196. snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i), error: %d\n",
  197. idx, snd_ecards_limit - 1, err);
  198. kfree(card);
  199. return err;
  200. }
  201. set_bit(idx, snd_cards_lock); /* lock it */
  202. if (idx >= snd_ecards_limit)
  203. snd_ecards_limit = idx + 1; /* increase the limit */
  204. mutex_unlock(&snd_card_mutex);
  205. card->dev = parent;
  206. card->number = idx;
  207. card->module = module;
  208. INIT_LIST_HEAD(&card->devices);
  209. init_rwsem(&card->controls_rwsem);
  210. rwlock_init(&card->ctl_files_rwlock);
  211. INIT_LIST_HEAD(&card->controls);
  212. INIT_LIST_HEAD(&card->ctl_files);
  213. spin_lock_init(&card->files_lock);
  214. INIT_LIST_HEAD(&card->files_list);
  215. init_waitqueue_head(&card->shutdown_sleep);
  216. atomic_set(&card->refcount, 0);
  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. snd_printk(KERN_ERR "unable to register control minors\n");
  234. goto __error;
  235. }
  236. err = snd_info_card_create(card);
  237. if (err < 0) {
  238. snd_printk(KERN_ERR "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, SNDRV_DEV_CMD_PRE);
  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. snd_printk(KERN_ERR "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. if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
  405. snd_printk(KERN_ERR "unable to free all devices (pre)\n");
  406. /* Fatal, but this situation should never occur */
  407. }
  408. if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
  409. snd_printk(KERN_ERR "unable to free all devices (normal)\n");
  410. /* Fatal, but this situation should never occur */
  411. }
  412. if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
  413. snd_printk(KERN_ERR "unable to free all devices (post)\n");
  414. /* Fatal, but this situation should never occur */
  415. }
  416. if (card->private_free)
  417. card->private_free(card);
  418. snd_info_free_entry(card->proc_id);
  419. if (snd_info_card_free(card) < 0) {
  420. snd_printk(KERN_WARNING "unable to free card info\n");
  421. /* Not fatal error */
  422. }
  423. kfree(card);
  424. return 0;
  425. }
  426. /**
  427. * snd_card_unref - release the reference counter
  428. * @card: the card instance
  429. *
  430. * Decrements the reference counter. When it reaches to zero, wake up
  431. * the sleeper and call the destructor if needed.
  432. */
  433. void snd_card_unref(struct snd_card *card)
  434. {
  435. if (atomic_dec_and_test(&card->refcount)) {
  436. wake_up(&card->shutdown_sleep);
  437. if (card->free_on_last_close)
  438. put_device(&card->card_dev);
  439. }
  440. }
  441. EXPORT_SYMBOL(snd_card_unref);
  442. int snd_card_free_when_closed(struct snd_card *card)
  443. {
  444. int ret;
  445. atomic_inc(&card->refcount);
  446. ret = snd_card_disconnect(card);
  447. if (ret) {
  448. atomic_dec(&card->refcount);
  449. return ret;
  450. }
  451. card->free_on_last_close = 1;
  452. if (atomic_dec_and_test(&card->refcount))
  453. put_device(&card->card_dev);
  454. return 0;
  455. }
  456. EXPORT_SYMBOL(snd_card_free_when_closed);
  457. int snd_card_free(struct snd_card *card)
  458. {
  459. int ret = snd_card_disconnect(card);
  460. if (ret)
  461. return ret;
  462. /* wait, until all devices are ready for the free operation */
  463. wait_event(card->shutdown_sleep, !atomic_read(&card->refcount));
  464. put_device(&card->card_dev);
  465. return 0;
  466. }
  467. EXPORT_SYMBOL(snd_card_free);
  468. /* retrieve the last word of shortname or longname */
  469. static const char *retrieve_id_from_card_name(const char *name)
  470. {
  471. const char *spos = name;
  472. while (*name) {
  473. if (isspace(*name) && isalnum(name[1]))
  474. spos = name + 1;
  475. name++;
  476. }
  477. return spos;
  478. }
  479. /* return true if the given id string doesn't conflict any other card ids */
  480. static bool card_id_ok(struct snd_card *card, const char *id)
  481. {
  482. int i;
  483. if (!snd_info_check_reserved_words(id))
  484. return false;
  485. for (i = 0; i < snd_ecards_limit; i++) {
  486. if (snd_cards[i] && snd_cards[i] != card &&
  487. !strcmp(snd_cards[i]->id, id))
  488. return false;
  489. }
  490. return true;
  491. }
  492. /* copy to card->id only with valid letters from nid */
  493. static void copy_valid_id_string(struct snd_card *card, const char *src,
  494. const char *nid)
  495. {
  496. char *id = card->id;
  497. while (*nid && !isalnum(*nid))
  498. nid++;
  499. if (isdigit(*nid))
  500. *id++ = isalpha(*src) ? *src : 'D';
  501. while (*nid && (size_t)(id - card->id) < sizeof(card->id) - 1) {
  502. if (isalnum(*nid))
  503. *id++ = *nid;
  504. nid++;
  505. }
  506. *id = 0;
  507. }
  508. /* Set card->id from the given string
  509. * If the string conflicts with other ids, add a suffix to make it unique.
  510. */
  511. static void snd_card_set_id_no_lock(struct snd_card *card, const char *src,
  512. const char *nid)
  513. {
  514. int len, loops;
  515. bool is_default = false;
  516. char *id;
  517. copy_valid_id_string(card, src, nid);
  518. id = card->id;
  519. again:
  520. /* use "Default" for obviously invalid strings
  521. * ("card" conflicts with proc directories)
  522. */
  523. if (!*id || !strncmp(id, "card", 4)) {
  524. strcpy(id, "Default");
  525. is_default = true;
  526. }
  527. len = strlen(id);
  528. for (loops = 0; loops < SNDRV_CARDS; loops++) {
  529. char *spos;
  530. char sfxstr[5]; /* "_012" */
  531. int sfxlen;
  532. if (card_id_ok(card, id))
  533. return; /* OK */
  534. /* Add _XYZ suffix */
  535. sprintf(sfxstr, "_%X", loops + 1);
  536. sfxlen = strlen(sfxstr);
  537. if (len + sfxlen >= sizeof(card->id))
  538. spos = id + sizeof(card->id) - sfxlen - 1;
  539. else
  540. spos = id + len;
  541. strcpy(spos, sfxstr);
  542. }
  543. /* fallback to the default id */
  544. if (!is_default) {
  545. *id = 0;
  546. goto again;
  547. }
  548. /* last resort... */
  549. snd_printk(KERN_ERR "unable to set card id (%s)\n", id);
  550. if (card->proc_root->name)
  551. strlcpy(card->id, card->proc_root->name, sizeof(card->id));
  552. }
  553. /**
  554. * snd_card_set_id - set card identification name
  555. * @card: soundcard structure
  556. * @nid: new identification string
  557. *
  558. * This function sets the card identification and checks for name
  559. * collisions.
  560. */
  561. void snd_card_set_id(struct snd_card *card, const char *nid)
  562. {
  563. /* check if user specified own card->id */
  564. if (card->id[0] != '\0')
  565. return;
  566. mutex_lock(&snd_card_mutex);
  567. snd_card_set_id_no_lock(card, nid, nid);
  568. mutex_unlock(&snd_card_mutex);
  569. }
  570. EXPORT_SYMBOL(snd_card_set_id);
  571. static ssize_t
  572. card_id_show_attr(struct device *dev,
  573. struct device_attribute *attr, char *buf)
  574. {
  575. struct snd_card *card = dev_get_drvdata(dev);
  576. return snprintf(buf, PAGE_SIZE, "%s\n", card ? card->id : "(null)");
  577. }
  578. static ssize_t
  579. card_id_store_attr(struct device *dev, struct device_attribute *attr,
  580. const char *buf, size_t count)
  581. {
  582. struct snd_card *card = dev_get_drvdata(dev);
  583. char buf1[sizeof(card->id)];
  584. size_t copy = count > sizeof(card->id) - 1 ?
  585. sizeof(card->id) - 1 : count;
  586. size_t idx;
  587. int c;
  588. for (idx = 0; idx < copy; idx++) {
  589. c = buf[idx];
  590. if (!isalnum(c) && c != '_' && c != '-')
  591. return -EINVAL;
  592. }
  593. memcpy(buf1, buf, copy);
  594. buf1[copy] = '\0';
  595. mutex_lock(&snd_card_mutex);
  596. if (!card_id_ok(NULL, buf1)) {
  597. mutex_unlock(&snd_card_mutex);
  598. return -EEXIST;
  599. }
  600. strcpy(card->id, buf1);
  601. snd_info_card_id_change(card);
  602. mutex_unlock(&snd_card_mutex);
  603. return count;
  604. }
  605. static DEVICE_ATTR(id, S_IRUGO | S_IWUSR, card_id_show_attr, card_id_store_attr);
  606. static ssize_t
  607. card_number_show_attr(struct device *dev,
  608. struct device_attribute *attr, char *buf)
  609. {
  610. struct snd_card *card = dev_get_drvdata(dev);
  611. return snprintf(buf, PAGE_SIZE, "%i\n", card ? card->number : -1);
  612. }
  613. static DEVICE_ATTR(number, S_IRUGO, card_number_show_attr, NULL);
  614. static struct attribute *card_dev_attrs[] = {
  615. &dev_attr_id.attr,
  616. &dev_attr_number.attr,
  617. NULL
  618. };
  619. static struct attribute_group card_dev_attr_group = {
  620. .attrs = card_dev_attrs,
  621. };
  622. static const struct attribute_group *card_dev_attr_groups[] = {
  623. &card_dev_attr_group,
  624. NULL
  625. };
  626. /**
  627. * snd_card_register - register the soundcard
  628. * @card: soundcard structure
  629. *
  630. * This function registers all the devices assigned to the soundcard.
  631. * Until calling this, the ALSA control interface is blocked from the
  632. * external accesses. Thus, you should call this function at the end
  633. * of the initialization of the card.
  634. *
  635. * Return: Zero otherwise a negative error code if the registration failed.
  636. */
  637. int snd_card_register(struct snd_card *card)
  638. {
  639. int err;
  640. if (snd_BUG_ON(!card))
  641. return -EINVAL;
  642. if (!card->registered) {
  643. err = device_add(&card->card_dev);
  644. if (err < 0)
  645. return err;
  646. card->registered = true;
  647. }
  648. if ((err = snd_device_register_all(card)) < 0)
  649. return err;
  650. mutex_lock(&snd_card_mutex);
  651. if (snd_cards[card->number]) {
  652. /* already registered */
  653. mutex_unlock(&snd_card_mutex);
  654. return 0;
  655. }
  656. if (*card->id) {
  657. /* make a unique id name from the given string */
  658. char tmpid[sizeof(card->id)];
  659. memcpy(tmpid, card->id, sizeof(card->id));
  660. snd_card_set_id_no_lock(card, tmpid, tmpid);
  661. } else {
  662. /* create an id from either shortname or longname */
  663. const char *src;
  664. src = *card->shortname ? card->shortname : card->longname;
  665. snd_card_set_id_no_lock(card, src,
  666. retrieve_id_from_card_name(src));
  667. }
  668. snd_cards[card->number] = card;
  669. mutex_unlock(&snd_card_mutex);
  670. init_info_for_card(card);
  671. #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
  672. if (snd_mixer_oss_notify_callback)
  673. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
  674. #endif
  675. return 0;
  676. }
  677. EXPORT_SYMBOL(snd_card_register);
  678. #ifdef CONFIG_PROC_FS
  679. static struct snd_info_entry *snd_card_info_entry;
  680. static void snd_card_info_read(struct snd_info_entry *entry,
  681. struct snd_info_buffer *buffer)
  682. {
  683. int idx, count;
  684. struct snd_card *card;
  685. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  686. mutex_lock(&snd_card_mutex);
  687. if ((card = snd_cards[idx]) != NULL) {
  688. count++;
  689. snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
  690. idx,
  691. card->id,
  692. card->driver,
  693. card->shortname);
  694. snd_iprintf(buffer, " %s\n",
  695. card->longname);
  696. }
  697. mutex_unlock(&snd_card_mutex);
  698. }
  699. if (!count)
  700. snd_iprintf(buffer, "--- no soundcards ---\n");
  701. }
  702. #ifdef CONFIG_SND_OSSEMUL
  703. void snd_card_info_read_oss(struct snd_info_buffer *buffer)
  704. {
  705. int idx, count;
  706. struct snd_card *card;
  707. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  708. mutex_lock(&snd_card_mutex);
  709. if ((card = snd_cards[idx]) != NULL) {
  710. count++;
  711. snd_iprintf(buffer, "%s\n", card->longname);
  712. }
  713. mutex_unlock(&snd_card_mutex);
  714. }
  715. if (!count) {
  716. snd_iprintf(buffer, "--- no soundcards ---\n");
  717. }
  718. }
  719. #endif
  720. #ifdef MODULE
  721. static struct snd_info_entry *snd_card_module_info_entry;
  722. static void snd_card_module_info_read(struct snd_info_entry *entry,
  723. struct snd_info_buffer *buffer)
  724. {
  725. int idx;
  726. struct snd_card *card;
  727. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  728. mutex_lock(&snd_card_mutex);
  729. if ((card = snd_cards[idx]) != NULL)
  730. snd_iprintf(buffer, "%2i %s\n",
  731. idx, card->module->name);
  732. mutex_unlock(&snd_card_mutex);
  733. }
  734. }
  735. #endif
  736. int __init snd_card_info_init(void)
  737. {
  738. struct snd_info_entry *entry;
  739. entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
  740. if (! entry)
  741. return -ENOMEM;
  742. entry->c.text.read = snd_card_info_read;
  743. if (snd_info_register(entry) < 0) {
  744. snd_info_free_entry(entry);
  745. return -ENOMEM;
  746. }
  747. snd_card_info_entry = entry;
  748. #ifdef MODULE
  749. entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
  750. if (entry) {
  751. entry->c.text.read = snd_card_module_info_read;
  752. if (snd_info_register(entry) < 0)
  753. snd_info_free_entry(entry);
  754. else
  755. snd_card_module_info_entry = entry;
  756. }
  757. #endif
  758. return 0;
  759. }
  760. int __exit snd_card_info_done(void)
  761. {
  762. snd_info_free_entry(snd_card_info_entry);
  763. #ifdef MODULE
  764. snd_info_free_entry(snd_card_module_info_entry);
  765. #endif
  766. return 0;
  767. }
  768. #endif /* CONFIG_PROC_FS */
  769. /**
  770. * snd_component_add - add a component string
  771. * @card: soundcard structure
  772. * @component: the component id string
  773. *
  774. * This function adds the component id string to the supported list.
  775. * The component can be referred from the alsa-lib.
  776. *
  777. * Return: Zero otherwise a negative error code.
  778. */
  779. int snd_component_add(struct snd_card *card, const char *component)
  780. {
  781. char *ptr;
  782. int len = strlen(component);
  783. ptr = strstr(card->components, component);
  784. if (ptr != NULL) {
  785. if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
  786. return 1;
  787. }
  788. if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
  789. snd_BUG();
  790. return -ENOMEM;
  791. }
  792. if (card->components[0] != '\0')
  793. strcat(card->components, " ");
  794. strcat(card->components, component);
  795. return 0;
  796. }
  797. EXPORT_SYMBOL(snd_component_add);
  798. /**
  799. * snd_card_file_add - add the file to the file list of the card
  800. * @card: soundcard structure
  801. * @file: file pointer
  802. *
  803. * This function adds the file to the file linked-list of the card.
  804. * This linked-list is used to keep tracking the connection state,
  805. * and to avoid the release of busy resources by hotplug.
  806. *
  807. * Return: zero or a negative error code.
  808. */
  809. int snd_card_file_add(struct snd_card *card, struct file *file)
  810. {
  811. struct snd_monitor_file *mfile;
  812. mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
  813. if (mfile == NULL)
  814. return -ENOMEM;
  815. mfile->file = file;
  816. mfile->disconnected_f_op = NULL;
  817. INIT_LIST_HEAD(&mfile->shutdown_list);
  818. spin_lock(&card->files_lock);
  819. if (card->shutdown) {
  820. spin_unlock(&card->files_lock);
  821. kfree(mfile);
  822. return -ENODEV;
  823. }
  824. list_add(&mfile->list, &card->files_list);
  825. atomic_inc(&card->refcount);
  826. spin_unlock(&card->files_lock);
  827. return 0;
  828. }
  829. EXPORT_SYMBOL(snd_card_file_add);
  830. /**
  831. * snd_card_file_remove - remove the file from the file list
  832. * @card: soundcard structure
  833. * @file: file pointer
  834. *
  835. * This function removes the file formerly added to the card via
  836. * snd_card_file_add() function.
  837. * If all files are removed and snd_card_free_when_closed() was
  838. * called beforehand, it processes the pending release of
  839. * resources.
  840. *
  841. * Return: Zero or a negative error code.
  842. */
  843. int snd_card_file_remove(struct snd_card *card, struct file *file)
  844. {
  845. struct snd_monitor_file *mfile, *found = NULL;
  846. spin_lock(&card->files_lock);
  847. list_for_each_entry(mfile, &card->files_list, list) {
  848. if (mfile->file == file) {
  849. list_del(&mfile->list);
  850. spin_lock(&shutdown_lock);
  851. list_del(&mfile->shutdown_list);
  852. spin_unlock(&shutdown_lock);
  853. if (mfile->disconnected_f_op)
  854. fops_put(mfile->disconnected_f_op);
  855. found = mfile;
  856. break;
  857. }
  858. }
  859. spin_unlock(&card->files_lock);
  860. if (!found) {
  861. snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
  862. return -ENOENT;
  863. }
  864. kfree(found);
  865. snd_card_unref(card);
  866. return 0;
  867. }
  868. EXPORT_SYMBOL(snd_card_file_remove);
  869. #ifdef CONFIG_PM
  870. /**
  871. * snd_power_wait - wait until the power-state is changed.
  872. * @card: soundcard structure
  873. * @power_state: expected power state
  874. *
  875. * Waits until the power-state is changed.
  876. *
  877. * Return: Zero if successful, or a negative error code.
  878. *
  879. * Note: the power lock must be active before call.
  880. */
  881. int snd_power_wait(struct snd_card *card, unsigned int power_state)
  882. {
  883. wait_queue_t wait;
  884. int result = 0;
  885. /* fastpath */
  886. if (snd_power_get_state(card) == power_state)
  887. return 0;
  888. init_waitqueue_entry(&wait, current);
  889. add_wait_queue(&card->power_sleep, &wait);
  890. while (1) {
  891. if (card->shutdown) {
  892. result = -ENODEV;
  893. break;
  894. }
  895. if (snd_power_get_state(card) == power_state)
  896. break;
  897. set_current_state(TASK_UNINTERRUPTIBLE);
  898. snd_power_unlock(card);
  899. schedule_timeout(30 * HZ);
  900. snd_power_lock(card);
  901. }
  902. remove_wait_queue(&card->power_sleep, &wait);
  903. return result;
  904. }
  905. EXPORT_SYMBOL(snd_power_wait);
  906. #endif /* CONFIG_PM */