init.c 25 KB

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