init.c 23 KB

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