init.c 26 KB

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