init.c 26 KB

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