init.c 26 KB

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