init.c 25 KB

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