init.c 23 KB

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