init.c 23 KB

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