init.c 26 KB

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