init.c 25 KB

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