init.c 25 KB

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