init.c 26 KB

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