init.c 24 KB

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