init.c 26 KB

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