init.c 26 KB

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