init.c 25 KB

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