init.c 27 KB

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