init.c 25 KB

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