init.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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. static void snd_card_set_id_no_lock(struct snd_card *card, const char *nid)
  429. {
  430. int i, len, idx_flag = 0, loops = SNDRV_CARDS;
  431. const char *spos, *src;
  432. char *id;
  433. if (nid == NULL) {
  434. id = card->shortname;
  435. spos = src = id;
  436. while (*id != '\0') {
  437. if (*id == ' ')
  438. spos = id + 1;
  439. id++;
  440. }
  441. } else {
  442. spos = src = nid;
  443. }
  444. id = card->id;
  445. while (*spos != '\0' && !isalnum(*spos))
  446. spos++;
  447. if (isdigit(*spos))
  448. *id++ = isalpha(src[0]) ? src[0] : 'D';
  449. while (*spos != '\0' && (size_t)(id - card->id) < sizeof(card->id) - 1) {
  450. if (isalnum(*spos))
  451. *id++ = *spos;
  452. spos++;
  453. }
  454. *id = '\0';
  455. id = card->id;
  456. if (*id == '\0')
  457. strcpy(id, "Default");
  458. while (1) {
  459. if (loops-- == 0) {
  460. snd_printk(KERN_ERR "unable to set card id (%s)\n", id);
  461. strcpy(card->id, card->proc_root->name);
  462. return;
  463. }
  464. if (!snd_info_check_reserved_words(id))
  465. goto __change;
  466. for (i = 0; i < snd_ecards_limit; i++) {
  467. if (snd_cards[i] && !strcmp(snd_cards[i]->id, id))
  468. goto __change;
  469. }
  470. break;
  471. __change:
  472. len = strlen(id);
  473. if (idx_flag) {
  474. if (id[len-1] != '9')
  475. id[len-1]++;
  476. else
  477. id[len-1] = 'A';
  478. } else if ((size_t)len <= sizeof(card->id) - 3) {
  479. strcat(id, "_1");
  480. idx_flag++;
  481. } else {
  482. spos = id + len - 2;
  483. if ((size_t)len <= sizeof(card->id) - 2)
  484. spos++;
  485. *(char *)spos++ = '_';
  486. *(char *)spos++ = '1';
  487. *(char *)spos++ = '\0';
  488. idx_flag++;
  489. }
  490. }
  491. }
  492. /**
  493. * snd_card_set_id - set card identification name
  494. * @card: soundcard structure
  495. * @nid: new identification string
  496. *
  497. * This function sets the card identification and checks for name
  498. * collisions.
  499. */
  500. void snd_card_set_id(struct snd_card *card, const char *nid)
  501. {
  502. /* check if user specified own card->id */
  503. if (card->id[0] != '\0')
  504. return;
  505. mutex_lock(&snd_card_mutex);
  506. snd_card_set_id_no_lock(card, nid);
  507. mutex_unlock(&snd_card_mutex);
  508. }
  509. EXPORT_SYMBOL(snd_card_set_id);
  510. static ssize_t
  511. card_id_show_attr(struct device *dev,
  512. struct device_attribute *attr, char *buf)
  513. {
  514. struct snd_card *card = dev_get_drvdata(dev);
  515. return snprintf(buf, PAGE_SIZE, "%s\n", card ? card->id : "(null)");
  516. }
  517. static ssize_t
  518. card_id_store_attr(struct device *dev, struct device_attribute *attr,
  519. const char *buf, size_t count)
  520. {
  521. struct snd_card *card = dev_get_drvdata(dev);
  522. char buf1[sizeof(card->id)];
  523. size_t copy = count > sizeof(card->id) - 1 ?
  524. sizeof(card->id) - 1 : count;
  525. size_t idx;
  526. int c;
  527. for (idx = 0; idx < copy; idx++) {
  528. c = buf[idx];
  529. if (!isalnum(c) && c != '_' && c != '-')
  530. return -EINVAL;
  531. }
  532. memcpy(buf1, buf, copy);
  533. buf1[copy] = '\0';
  534. mutex_lock(&snd_card_mutex);
  535. if (!snd_info_check_reserved_words(buf1)) {
  536. __exist:
  537. mutex_unlock(&snd_card_mutex);
  538. return -EEXIST;
  539. }
  540. for (idx = 0; idx < snd_ecards_limit; idx++) {
  541. if (snd_cards[idx] && !strcmp(snd_cards[idx]->id, buf1)) {
  542. if (card == snd_cards[idx])
  543. goto __ok;
  544. else
  545. goto __exist;
  546. }
  547. }
  548. strcpy(card->id, buf1);
  549. snd_info_card_id_change(card);
  550. __ok:
  551. mutex_unlock(&snd_card_mutex);
  552. return count;
  553. }
  554. static struct device_attribute card_id_attrs =
  555. __ATTR(id, S_IRUGO | S_IWUSR, card_id_show_attr, card_id_store_attr);
  556. static ssize_t
  557. card_number_show_attr(struct device *dev,
  558. struct device_attribute *attr, char *buf)
  559. {
  560. struct snd_card *card = dev_get_drvdata(dev);
  561. return snprintf(buf, PAGE_SIZE, "%i\n", card ? card->number : -1);
  562. }
  563. static struct device_attribute card_number_attrs =
  564. __ATTR(number, S_IRUGO, card_number_show_attr, NULL);
  565. /**
  566. * snd_card_register - register the soundcard
  567. * @card: soundcard structure
  568. *
  569. * This function registers all the devices assigned to the soundcard.
  570. * Until calling this, the ALSA control interface is blocked from the
  571. * external accesses. Thus, you should call this function at the end
  572. * of the initialization of the card.
  573. *
  574. * Returns zero otherwise a negative error code if the registration failed.
  575. */
  576. int snd_card_register(struct snd_card *card)
  577. {
  578. int err;
  579. if (snd_BUG_ON(!card))
  580. return -EINVAL;
  581. if (!card->card_dev) {
  582. card->card_dev = device_create(sound_class, card->dev,
  583. MKDEV(0, 0), card,
  584. "card%i", card->number);
  585. if (IS_ERR(card->card_dev))
  586. card->card_dev = NULL;
  587. }
  588. if ((err = snd_device_register_all(card)) < 0)
  589. return err;
  590. mutex_lock(&snd_card_mutex);
  591. if (snd_cards[card->number]) {
  592. /* already registered */
  593. mutex_unlock(&snd_card_mutex);
  594. return 0;
  595. }
  596. snd_card_set_id_no_lock(card, card->id[0] == '\0' ? NULL : card->id);
  597. snd_cards[card->number] = card;
  598. mutex_unlock(&snd_card_mutex);
  599. init_info_for_card(card);
  600. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
  601. if (snd_mixer_oss_notify_callback)
  602. snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
  603. #endif
  604. if (card->card_dev) {
  605. err = device_create_file(card->card_dev, &card_id_attrs);
  606. if (err < 0)
  607. return err;
  608. err = device_create_file(card->card_dev, &card_number_attrs);
  609. if (err < 0)
  610. return err;
  611. }
  612. return 0;
  613. }
  614. EXPORT_SYMBOL(snd_card_register);
  615. #ifdef CONFIG_PROC_FS
  616. static struct snd_info_entry *snd_card_info_entry;
  617. static void snd_card_info_read(struct snd_info_entry *entry,
  618. struct snd_info_buffer *buffer)
  619. {
  620. int idx, count;
  621. struct snd_card *card;
  622. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  623. mutex_lock(&snd_card_mutex);
  624. if ((card = snd_cards[idx]) != NULL) {
  625. count++;
  626. snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
  627. idx,
  628. card->id,
  629. card->driver,
  630. card->shortname);
  631. snd_iprintf(buffer, " %s\n",
  632. card->longname);
  633. }
  634. mutex_unlock(&snd_card_mutex);
  635. }
  636. if (!count)
  637. snd_iprintf(buffer, "--- no soundcards ---\n");
  638. }
  639. #ifdef CONFIG_SND_OSSEMUL
  640. void snd_card_info_read_oss(struct snd_info_buffer *buffer)
  641. {
  642. int idx, count;
  643. struct snd_card *card;
  644. for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
  645. mutex_lock(&snd_card_mutex);
  646. if ((card = snd_cards[idx]) != NULL) {
  647. count++;
  648. snd_iprintf(buffer, "%s\n", card->longname);
  649. }
  650. mutex_unlock(&snd_card_mutex);
  651. }
  652. if (!count) {
  653. snd_iprintf(buffer, "--- no soundcards ---\n");
  654. }
  655. }
  656. #endif
  657. #ifdef MODULE
  658. static struct snd_info_entry *snd_card_module_info_entry;
  659. static void snd_card_module_info_read(struct snd_info_entry *entry,
  660. struct snd_info_buffer *buffer)
  661. {
  662. int idx;
  663. struct snd_card *card;
  664. for (idx = 0; idx < SNDRV_CARDS; idx++) {
  665. mutex_lock(&snd_card_mutex);
  666. if ((card = snd_cards[idx]) != NULL)
  667. snd_iprintf(buffer, "%2i %s\n",
  668. idx, card->module->name);
  669. mutex_unlock(&snd_card_mutex);
  670. }
  671. }
  672. #endif
  673. int __init snd_card_info_init(void)
  674. {
  675. struct snd_info_entry *entry;
  676. entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
  677. if (! entry)
  678. return -ENOMEM;
  679. entry->c.text.read = snd_card_info_read;
  680. if (snd_info_register(entry) < 0) {
  681. snd_info_free_entry(entry);
  682. return -ENOMEM;
  683. }
  684. snd_card_info_entry = entry;
  685. #ifdef MODULE
  686. entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
  687. if (entry) {
  688. entry->c.text.read = snd_card_module_info_read;
  689. if (snd_info_register(entry) < 0)
  690. snd_info_free_entry(entry);
  691. else
  692. snd_card_module_info_entry = entry;
  693. }
  694. #endif
  695. return 0;
  696. }
  697. int __exit snd_card_info_done(void)
  698. {
  699. snd_info_free_entry(snd_card_info_entry);
  700. #ifdef MODULE
  701. snd_info_free_entry(snd_card_module_info_entry);
  702. #endif
  703. return 0;
  704. }
  705. #endif /* CONFIG_PROC_FS */
  706. /**
  707. * snd_component_add - add a component string
  708. * @card: soundcard structure
  709. * @component: the component id string
  710. *
  711. * This function adds the component id string to the supported list.
  712. * The component can be referred from the alsa-lib.
  713. *
  714. * Returns zero otherwise a negative error code.
  715. */
  716. int snd_component_add(struct snd_card *card, const char *component)
  717. {
  718. char *ptr;
  719. int len = strlen(component);
  720. ptr = strstr(card->components, component);
  721. if (ptr != NULL) {
  722. if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
  723. return 1;
  724. }
  725. if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
  726. snd_BUG();
  727. return -ENOMEM;
  728. }
  729. if (card->components[0] != '\0')
  730. strcat(card->components, " ");
  731. strcat(card->components, component);
  732. return 0;
  733. }
  734. EXPORT_SYMBOL(snd_component_add);
  735. /**
  736. * snd_card_file_add - add the file to the file list of the card
  737. * @card: soundcard structure
  738. * @file: file pointer
  739. *
  740. * This function adds the file to the file linked-list of the card.
  741. * This linked-list is used to keep tracking the connection state,
  742. * and to avoid the release of busy resources by hotplug.
  743. *
  744. * Returns zero or a negative error code.
  745. */
  746. int snd_card_file_add(struct snd_card *card, struct file *file)
  747. {
  748. struct snd_monitor_file *mfile;
  749. mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
  750. if (mfile == NULL)
  751. return -ENOMEM;
  752. mfile->file = file;
  753. mfile->disconnected_f_op = NULL;
  754. INIT_LIST_HEAD(&mfile->shutdown_list);
  755. spin_lock(&card->files_lock);
  756. if (card->shutdown) {
  757. spin_unlock(&card->files_lock);
  758. kfree(mfile);
  759. return -ENODEV;
  760. }
  761. list_add(&mfile->list, &card->files_list);
  762. spin_unlock(&card->files_lock);
  763. return 0;
  764. }
  765. EXPORT_SYMBOL(snd_card_file_add);
  766. /**
  767. * snd_card_file_remove - remove the file from the file list
  768. * @card: soundcard structure
  769. * @file: file pointer
  770. *
  771. * This function removes the file formerly added to the card via
  772. * snd_card_file_add() function.
  773. * If all files are removed and snd_card_free_when_closed() was
  774. * called beforehand, it processes the pending release of
  775. * resources.
  776. *
  777. * Returns zero or a negative error code.
  778. */
  779. int snd_card_file_remove(struct snd_card *card, struct file *file)
  780. {
  781. struct snd_monitor_file *mfile, *found = NULL;
  782. int last_close = 0;
  783. spin_lock(&card->files_lock);
  784. list_for_each_entry(mfile, &card->files_list, list) {
  785. if (mfile->file == file) {
  786. list_del(&mfile->list);
  787. spin_lock(&shutdown_lock);
  788. list_del(&mfile->shutdown_list);
  789. spin_unlock(&shutdown_lock);
  790. if (mfile->disconnected_f_op)
  791. fops_put(mfile->disconnected_f_op);
  792. found = mfile;
  793. break;
  794. }
  795. }
  796. if (list_empty(&card->files_list))
  797. last_close = 1;
  798. spin_unlock(&card->files_lock);
  799. if (last_close) {
  800. wake_up(&card->shutdown_sleep);
  801. if (card->free_on_last_close)
  802. snd_card_do_free(card);
  803. }
  804. if (!found) {
  805. snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
  806. return -ENOENT;
  807. }
  808. kfree(found);
  809. return 0;
  810. }
  811. EXPORT_SYMBOL(snd_card_file_remove);
  812. #ifdef CONFIG_PM
  813. /**
  814. * snd_power_wait - wait until the power-state is changed.
  815. * @card: soundcard structure
  816. * @power_state: expected power state
  817. *
  818. * Waits until the power-state is changed.
  819. *
  820. * Note: the power lock must be active before call.
  821. */
  822. int snd_power_wait(struct snd_card *card, unsigned int power_state)
  823. {
  824. wait_queue_t wait;
  825. int result = 0;
  826. /* fastpath */
  827. if (snd_power_get_state(card) == power_state)
  828. return 0;
  829. init_waitqueue_entry(&wait, current);
  830. add_wait_queue(&card->power_sleep, &wait);
  831. while (1) {
  832. if (card->shutdown) {
  833. result = -ENODEV;
  834. break;
  835. }
  836. if (snd_power_get_state(card) == power_state)
  837. break;
  838. set_current_state(TASK_UNINTERRUPTIBLE);
  839. snd_power_unlock(card);
  840. schedule_timeout(30 * HZ);
  841. snd_power_lock(card);
  842. }
  843. remove_wait_queue(&card->power_sleep, &wait);
  844. return result;
  845. }
  846. EXPORT_SYMBOL(snd_power_wait);
  847. #endif /* CONFIG_PM */