info.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*
  2. * Information interface for ALSA driver
  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/time.h>
  23. #include <linux/mm.h>
  24. #include <linux/slab.h>
  25. #include <linux/string.h>
  26. #include <linux/module.h>
  27. #include <sound/core.h>
  28. #include <sound/minors.h>
  29. #include <sound/info.h>
  30. #include <linux/utsname.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/mutex.h>
  33. #include <stdarg.h>
  34. int snd_info_check_reserved_words(const char *str)
  35. {
  36. static char *reserved[] =
  37. {
  38. "version",
  39. "meminfo",
  40. "memdebug",
  41. "detect",
  42. "devices",
  43. "oss",
  44. "cards",
  45. "timers",
  46. "synth",
  47. "pcm",
  48. "seq",
  49. NULL
  50. };
  51. char **xstr = reserved;
  52. while (*xstr) {
  53. if (!strcmp(*xstr, str))
  54. return 0;
  55. xstr++;
  56. }
  57. if (!strncmp(str, "card", 4))
  58. return 0;
  59. return 1;
  60. }
  61. static DEFINE_MUTEX(info_mutex);
  62. struct snd_info_private_data {
  63. struct snd_info_buffer *rbuffer;
  64. struct snd_info_buffer *wbuffer;
  65. struct snd_info_entry *entry;
  66. void *file_private_data;
  67. };
  68. static int snd_info_version_init(void);
  69. static void snd_info_disconnect(struct snd_info_entry *entry);
  70. /*
  71. */
  72. static struct snd_info_entry *snd_proc_root;
  73. struct snd_info_entry *snd_seq_root;
  74. EXPORT_SYMBOL(snd_seq_root);
  75. #ifdef CONFIG_SND_OSSEMUL
  76. struct snd_info_entry *snd_oss_root;
  77. #endif
  78. static int alloc_info_private(struct snd_info_entry *entry,
  79. struct snd_info_private_data **ret)
  80. {
  81. struct snd_info_private_data *data;
  82. if (!entry || !entry->p)
  83. return -ENODEV;
  84. if (!try_module_get(entry->module))
  85. return -EFAULT;
  86. data = kzalloc(sizeof(*data), GFP_KERNEL);
  87. if (!data) {
  88. module_put(entry->module);
  89. return -ENOMEM;
  90. }
  91. data->entry = entry;
  92. *ret = data;
  93. return 0;
  94. }
  95. static bool valid_pos(loff_t pos, size_t count)
  96. {
  97. if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
  98. return false;
  99. if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
  100. return false;
  101. return true;
  102. }
  103. /*
  104. * file ops for binary proc files
  105. */
  106. static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
  107. {
  108. struct snd_info_private_data *data;
  109. struct snd_info_entry *entry;
  110. loff_t ret = -EINVAL, size;
  111. data = file->private_data;
  112. entry = data->entry;
  113. mutex_lock(&entry->access);
  114. if (entry->c.ops->llseek) {
  115. offset = entry->c.ops->llseek(entry,
  116. data->file_private_data,
  117. file, offset, orig);
  118. goto out;
  119. }
  120. size = entry->size;
  121. switch (orig) {
  122. case SEEK_SET:
  123. break;
  124. case SEEK_CUR:
  125. offset += file->f_pos;
  126. break;
  127. case SEEK_END:
  128. if (!size)
  129. goto out;
  130. offset += size;
  131. break;
  132. default:
  133. goto out;
  134. }
  135. if (offset < 0)
  136. goto out;
  137. if (size && offset > size)
  138. offset = size;
  139. file->f_pos = offset;
  140. ret = offset;
  141. out:
  142. mutex_unlock(&entry->access);
  143. return ret;
  144. }
  145. static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
  146. size_t count, loff_t * offset)
  147. {
  148. struct snd_info_private_data *data = file->private_data;
  149. struct snd_info_entry *entry = data->entry;
  150. size_t size;
  151. loff_t pos;
  152. pos = *offset;
  153. if (!valid_pos(pos, count))
  154. return -EIO;
  155. if (pos >= entry->size)
  156. return 0;
  157. size = entry->size - pos;
  158. size = min(count, size);
  159. size = entry->c.ops->read(entry, data->file_private_data,
  160. file, buffer, size, pos);
  161. if ((ssize_t) size > 0)
  162. *offset = pos + size;
  163. return size;
  164. }
  165. static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
  166. size_t count, loff_t * offset)
  167. {
  168. struct snd_info_private_data *data = file->private_data;
  169. struct snd_info_entry *entry = data->entry;
  170. ssize_t size = 0;
  171. loff_t pos;
  172. pos = *offset;
  173. if (!valid_pos(pos, count))
  174. return -EIO;
  175. if (count > 0) {
  176. size_t maxsize = entry->size - pos;
  177. count = min(count, maxsize);
  178. size = entry->c.ops->write(entry, data->file_private_data,
  179. file, buffer, count, pos);
  180. }
  181. if (size > 0)
  182. *offset = pos + size;
  183. return size;
  184. }
  185. static unsigned int snd_info_entry_poll(struct file *file, poll_table *wait)
  186. {
  187. struct snd_info_private_data *data = file->private_data;
  188. struct snd_info_entry *entry = data->entry;
  189. unsigned int mask = 0;
  190. if (entry->c.ops->poll)
  191. return entry->c.ops->poll(entry,
  192. data->file_private_data,
  193. file, wait);
  194. if (entry->c.ops->read)
  195. mask |= POLLIN | POLLRDNORM;
  196. if (entry->c.ops->write)
  197. mask |= POLLOUT | POLLWRNORM;
  198. return mask;
  199. }
  200. static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
  201. unsigned long arg)
  202. {
  203. struct snd_info_private_data *data = file->private_data;
  204. struct snd_info_entry *entry = data->entry;
  205. if (!entry->c.ops->ioctl)
  206. return -ENOTTY;
  207. return entry->c.ops->ioctl(entry, data->file_private_data,
  208. file, cmd, arg);
  209. }
  210. static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
  211. {
  212. struct inode *inode = file_inode(file);
  213. struct snd_info_private_data *data;
  214. struct snd_info_entry *entry;
  215. data = file->private_data;
  216. if (data == NULL)
  217. return 0;
  218. entry = data->entry;
  219. if (!entry->c.ops->mmap)
  220. return -ENXIO;
  221. return entry->c.ops->mmap(entry, data->file_private_data,
  222. inode, file, vma);
  223. }
  224. static int snd_info_entry_open(struct inode *inode, struct file *file)
  225. {
  226. struct snd_info_entry *entry = PDE_DATA(inode);
  227. struct snd_info_private_data *data;
  228. int mode, err;
  229. mutex_lock(&info_mutex);
  230. err = alloc_info_private(entry, &data);
  231. if (err < 0)
  232. goto unlock;
  233. mode = file->f_flags & O_ACCMODE;
  234. if (((mode == O_RDONLY || mode == O_RDWR) && !entry->c.ops->read) ||
  235. ((mode == O_WRONLY || mode == O_RDWR) && !entry->c.ops->write)) {
  236. err = -ENODEV;
  237. goto error;
  238. }
  239. if (entry->c.ops->open) {
  240. err = entry->c.ops->open(entry, mode, &data->file_private_data);
  241. if (err < 0)
  242. goto error;
  243. }
  244. file->private_data = data;
  245. mutex_unlock(&info_mutex);
  246. return 0;
  247. error:
  248. kfree(data);
  249. module_put(entry->module);
  250. unlock:
  251. mutex_unlock(&info_mutex);
  252. return err;
  253. }
  254. static int snd_info_entry_release(struct inode *inode, struct file *file)
  255. {
  256. struct snd_info_private_data *data = file->private_data;
  257. struct snd_info_entry *entry = data->entry;
  258. if (entry->c.ops->release)
  259. entry->c.ops->release(entry, file->f_flags & O_ACCMODE,
  260. data->file_private_data);
  261. module_put(entry->module);
  262. kfree(data);
  263. return 0;
  264. }
  265. static const struct file_operations snd_info_entry_operations =
  266. {
  267. .owner = THIS_MODULE,
  268. .llseek = snd_info_entry_llseek,
  269. .read = snd_info_entry_read,
  270. .write = snd_info_entry_write,
  271. .poll = snd_info_entry_poll,
  272. .unlocked_ioctl = snd_info_entry_ioctl,
  273. .mmap = snd_info_entry_mmap,
  274. .open = snd_info_entry_open,
  275. .release = snd_info_entry_release,
  276. };
  277. /*
  278. * file ops for text proc files
  279. */
  280. static ssize_t snd_info_text_entry_write(struct file *file,
  281. const char __user *buffer,
  282. size_t count, loff_t *offset)
  283. {
  284. struct seq_file *m = file->private_data;
  285. struct snd_info_private_data *data = m->private;
  286. struct snd_info_entry *entry = data->entry;
  287. struct snd_info_buffer *buf;
  288. loff_t pos;
  289. size_t next;
  290. int err = 0;
  291. pos = *offset;
  292. if (!valid_pos(pos, count))
  293. return -EIO;
  294. next = pos + count;
  295. mutex_lock(&entry->access);
  296. buf = data->wbuffer;
  297. if (!buf) {
  298. data->wbuffer = buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  299. if (!buf) {
  300. err = -ENOMEM;
  301. goto error;
  302. }
  303. }
  304. if (next > buf->len) {
  305. char *nbuf = krealloc(buf->buffer, PAGE_ALIGN(next),
  306. GFP_KERNEL | __GFP_ZERO);
  307. if (!nbuf) {
  308. err = -ENOMEM;
  309. goto error;
  310. }
  311. buf->buffer = nbuf;
  312. buf->len = PAGE_ALIGN(next);
  313. }
  314. if (copy_from_user(buf->buffer + pos, buffer, count)) {
  315. err = -EFAULT;
  316. goto error;
  317. }
  318. buf->size = next;
  319. error:
  320. mutex_unlock(&entry->access);
  321. if (err < 0)
  322. return err;
  323. *offset = next;
  324. return count;
  325. }
  326. static int snd_info_seq_show(struct seq_file *seq, void *p)
  327. {
  328. struct snd_info_private_data *data = seq->private;
  329. struct snd_info_entry *entry = data->entry;
  330. if (entry->c.text.read) {
  331. data->rbuffer->buffer = (char *)seq; /* XXX hack! */
  332. entry->c.text.read(entry, data->rbuffer);
  333. }
  334. return 0;
  335. }
  336. static int snd_info_text_entry_open(struct inode *inode, struct file *file)
  337. {
  338. struct snd_info_entry *entry = PDE_DATA(inode);
  339. struct snd_info_private_data *data;
  340. int err;
  341. mutex_lock(&info_mutex);
  342. err = alloc_info_private(entry, &data);
  343. if (err < 0)
  344. goto unlock;
  345. data->rbuffer = kzalloc(sizeof(*data->rbuffer), GFP_KERNEL);
  346. if (!data->rbuffer) {
  347. err = -ENOMEM;
  348. goto error;
  349. }
  350. if (entry->size)
  351. err = single_open_size(file, snd_info_seq_show, data,
  352. entry->size);
  353. else
  354. err = single_open(file, snd_info_seq_show, data);
  355. if (err < 0)
  356. goto error;
  357. mutex_unlock(&info_mutex);
  358. return 0;
  359. error:
  360. kfree(data->rbuffer);
  361. kfree(data);
  362. module_put(entry->module);
  363. unlock:
  364. mutex_unlock(&info_mutex);
  365. return err;
  366. }
  367. static int snd_info_text_entry_release(struct inode *inode, struct file *file)
  368. {
  369. struct seq_file *m = file->private_data;
  370. struct snd_info_private_data *data = m->private;
  371. struct snd_info_entry *entry = data->entry;
  372. if (data->wbuffer && entry->c.text.write)
  373. entry->c.text.write(entry, data->wbuffer);
  374. single_release(inode, file);
  375. kfree(data->rbuffer);
  376. if (data->wbuffer) {
  377. kfree(data->wbuffer->buffer);
  378. kfree(data->wbuffer);
  379. }
  380. module_put(entry->module);
  381. kfree(data);
  382. return 0;
  383. }
  384. static const struct file_operations snd_info_text_entry_ops =
  385. {
  386. .owner = THIS_MODULE,
  387. .open = snd_info_text_entry_open,
  388. .release = snd_info_text_entry_release,
  389. .write = snd_info_text_entry_write,
  390. .llseek = seq_lseek,
  391. .read = seq_read,
  392. };
  393. static struct snd_info_entry *create_subdir(struct module *mod,
  394. const char *name)
  395. {
  396. struct snd_info_entry *entry;
  397. entry = snd_info_create_module_entry(mod, name, NULL);
  398. if (!entry)
  399. return NULL;
  400. entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  401. if (snd_info_register(entry) < 0) {
  402. snd_info_free_entry(entry);
  403. return NULL;
  404. }
  405. return entry;
  406. }
  407. static struct snd_info_entry *snd_info_create_entry(const char *name);
  408. int __init snd_info_init(void)
  409. {
  410. snd_proc_root = snd_info_create_entry("asound");
  411. if (!snd_proc_root)
  412. return -ENOMEM;
  413. snd_proc_root->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  414. snd_proc_root->p = proc_mkdir("asound", NULL);
  415. if (!snd_proc_root->p)
  416. goto error;
  417. #ifdef CONFIG_SND_OSSEMUL
  418. snd_oss_root = create_subdir(THIS_MODULE, "oss");
  419. if (!snd_oss_root)
  420. goto error;
  421. #endif
  422. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  423. snd_seq_root = create_subdir(THIS_MODULE, "seq");
  424. if (!snd_seq_root)
  425. goto error;
  426. #endif
  427. if (snd_info_version_init() < 0 ||
  428. snd_minor_info_init() < 0 ||
  429. snd_minor_info_oss_init() < 0 ||
  430. snd_card_info_init() < 0 ||
  431. snd_info_minor_register() < 0)
  432. goto error;
  433. return 0;
  434. error:
  435. snd_info_free_entry(snd_proc_root);
  436. return -ENOMEM;
  437. }
  438. int __exit snd_info_done(void)
  439. {
  440. snd_info_free_entry(snd_proc_root);
  441. return 0;
  442. }
  443. /*
  444. * create a card proc file
  445. * called from init.c
  446. */
  447. int snd_info_card_create(struct snd_card *card)
  448. {
  449. char str[8];
  450. struct snd_info_entry *entry;
  451. if (snd_BUG_ON(!card))
  452. return -ENXIO;
  453. sprintf(str, "card%i", card->number);
  454. entry = create_subdir(card->module, str);
  455. if (!entry)
  456. return -ENOMEM;
  457. card->proc_root = entry;
  458. return 0;
  459. }
  460. /*
  461. * register the card proc file
  462. * called from init.c
  463. */
  464. int snd_info_card_register(struct snd_card *card)
  465. {
  466. struct proc_dir_entry *p;
  467. if (snd_BUG_ON(!card))
  468. return -ENXIO;
  469. if (!strcmp(card->id, card->proc_root->name))
  470. return 0;
  471. p = proc_symlink(card->id, snd_proc_root->p, card->proc_root->name);
  472. if (p == NULL)
  473. return -ENOMEM;
  474. card->proc_root_link = p;
  475. return 0;
  476. }
  477. /*
  478. * called on card->id change
  479. */
  480. void snd_info_card_id_change(struct snd_card *card)
  481. {
  482. mutex_lock(&info_mutex);
  483. if (card->proc_root_link) {
  484. proc_remove(card->proc_root_link);
  485. card->proc_root_link = NULL;
  486. }
  487. if (strcmp(card->id, card->proc_root->name))
  488. card->proc_root_link = proc_symlink(card->id,
  489. snd_proc_root->p,
  490. card->proc_root->name);
  491. mutex_unlock(&info_mutex);
  492. }
  493. /*
  494. * de-register the card proc file
  495. * called from init.c
  496. */
  497. void snd_info_card_disconnect(struct snd_card *card)
  498. {
  499. if (!card)
  500. return;
  501. mutex_lock(&info_mutex);
  502. proc_remove(card->proc_root_link);
  503. card->proc_root_link = NULL;
  504. if (card->proc_root)
  505. snd_info_disconnect(card->proc_root);
  506. mutex_unlock(&info_mutex);
  507. }
  508. /*
  509. * release the card proc file resources
  510. * called from init.c
  511. */
  512. int snd_info_card_free(struct snd_card *card)
  513. {
  514. if (!card)
  515. return 0;
  516. snd_info_free_entry(card->proc_root);
  517. card->proc_root = NULL;
  518. return 0;
  519. }
  520. /**
  521. * snd_info_get_line - read one line from the procfs buffer
  522. * @buffer: the procfs buffer
  523. * @line: the buffer to store
  524. * @len: the max. buffer size
  525. *
  526. * Reads one line from the buffer and stores the string.
  527. *
  528. * Return: Zero if successful, or 1 if error or EOF.
  529. */
  530. int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
  531. {
  532. int c = -1;
  533. if (snd_BUG_ON(!buffer || !buffer->buffer))
  534. return 1;
  535. if (len <= 0 || buffer->stop || buffer->error)
  536. return 1;
  537. while (!buffer->stop) {
  538. c = buffer->buffer[buffer->curr++];
  539. if (buffer->curr >= buffer->size)
  540. buffer->stop = 1;
  541. if (c == '\n')
  542. break;
  543. if (len > 1) {
  544. len--;
  545. *line++ = c;
  546. }
  547. }
  548. *line = '\0';
  549. return 0;
  550. }
  551. EXPORT_SYMBOL(snd_info_get_line);
  552. /**
  553. * snd_info_get_str - parse a string token
  554. * @dest: the buffer to store the string token
  555. * @src: the original string
  556. * @len: the max. length of token - 1
  557. *
  558. * Parses the original string and copy a token to the given
  559. * string buffer.
  560. *
  561. * Return: The updated pointer of the original string so that
  562. * it can be used for the next call.
  563. */
  564. const char *snd_info_get_str(char *dest, const char *src, int len)
  565. {
  566. int c;
  567. while (*src == ' ' || *src == '\t')
  568. src++;
  569. if (*src == '"' || *src == '\'') {
  570. c = *src++;
  571. while (--len > 0 && *src && *src != c) {
  572. *dest++ = *src++;
  573. }
  574. if (*src == c)
  575. src++;
  576. } else {
  577. while (--len > 0 && *src && *src != ' ' && *src != '\t') {
  578. *dest++ = *src++;
  579. }
  580. }
  581. *dest = 0;
  582. while (*src == ' ' || *src == '\t')
  583. src++;
  584. return src;
  585. }
  586. EXPORT_SYMBOL(snd_info_get_str);
  587. /**
  588. * snd_info_create_entry - create an info entry
  589. * @name: the proc file name
  590. *
  591. * Creates an info entry with the given file name and initializes as
  592. * the default state.
  593. *
  594. * Usually called from other functions such as
  595. * snd_info_create_card_entry().
  596. *
  597. * Return: The pointer of the new instance, or %NULL on failure.
  598. */
  599. static struct snd_info_entry *snd_info_create_entry(const char *name)
  600. {
  601. struct snd_info_entry *entry;
  602. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  603. if (entry == NULL)
  604. return NULL;
  605. entry->name = kstrdup(name, GFP_KERNEL);
  606. if (entry->name == NULL) {
  607. kfree(entry);
  608. return NULL;
  609. }
  610. entry->mode = S_IFREG | S_IRUGO;
  611. entry->content = SNDRV_INFO_CONTENT_TEXT;
  612. mutex_init(&entry->access);
  613. INIT_LIST_HEAD(&entry->children);
  614. INIT_LIST_HEAD(&entry->list);
  615. return entry;
  616. }
  617. /**
  618. * snd_info_create_module_entry - create an info entry for the given module
  619. * @module: the module pointer
  620. * @name: the file name
  621. * @parent: the parent directory
  622. *
  623. * Creates a new info entry and assigns it to the given module.
  624. *
  625. * Return: The pointer of the new instance, or %NULL on failure.
  626. */
  627. struct snd_info_entry *snd_info_create_module_entry(struct module * module,
  628. const char *name,
  629. struct snd_info_entry *parent)
  630. {
  631. struct snd_info_entry *entry = snd_info_create_entry(name);
  632. if (entry) {
  633. entry->module = module;
  634. entry->parent = parent;
  635. }
  636. return entry;
  637. }
  638. EXPORT_SYMBOL(snd_info_create_module_entry);
  639. /**
  640. * snd_info_create_card_entry - create an info entry for the given card
  641. * @card: the card instance
  642. * @name: the file name
  643. * @parent: the parent directory
  644. *
  645. * Creates a new info entry and assigns it to the given card.
  646. *
  647. * Return: The pointer of the new instance, or %NULL on failure.
  648. */
  649. struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
  650. const char *name,
  651. struct snd_info_entry * parent)
  652. {
  653. struct snd_info_entry *entry = snd_info_create_entry(name);
  654. if (entry) {
  655. entry->module = card->module;
  656. entry->card = card;
  657. entry->parent = parent;
  658. }
  659. return entry;
  660. }
  661. EXPORT_SYMBOL(snd_info_create_card_entry);
  662. static void snd_info_disconnect(struct snd_info_entry *entry)
  663. {
  664. struct snd_info_entry *p;
  665. if (!entry->p)
  666. return;
  667. list_for_each_entry(p, &entry->children, list)
  668. snd_info_disconnect(p);
  669. proc_remove(entry->p);
  670. entry->p = NULL;
  671. }
  672. /**
  673. * snd_info_free_entry - release the info entry
  674. * @entry: the info entry
  675. *
  676. * Releases the info entry.
  677. */
  678. void snd_info_free_entry(struct snd_info_entry * entry)
  679. {
  680. struct snd_info_entry *p, *n;
  681. if (!entry)
  682. return;
  683. if (entry->p) {
  684. mutex_lock(&info_mutex);
  685. snd_info_disconnect(entry);
  686. mutex_unlock(&info_mutex);
  687. }
  688. /* free all children at first */
  689. list_for_each_entry_safe(p, n, &entry->children, list)
  690. snd_info_free_entry(p);
  691. list_del(&entry->list);
  692. kfree(entry->name);
  693. if (entry->private_free)
  694. entry->private_free(entry);
  695. kfree(entry);
  696. }
  697. EXPORT_SYMBOL(snd_info_free_entry);
  698. /**
  699. * snd_info_register - register the info entry
  700. * @entry: the info entry
  701. *
  702. * Registers the proc info entry.
  703. *
  704. * Return: Zero if successful, or a negative error code on failure.
  705. */
  706. int snd_info_register(struct snd_info_entry * entry)
  707. {
  708. struct proc_dir_entry *root, *p = NULL;
  709. if (snd_BUG_ON(!entry))
  710. return -ENXIO;
  711. root = entry->parent == NULL ? snd_proc_root->p : entry->parent->p;
  712. mutex_lock(&info_mutex);
  713. if (S_ISDIR(entry->mode)) {
  714. p = proc_mkdir_mode(entry->name, entry->mode, root);
  715. if (!p) {
  716. mutex_unlock(&info_mutex);
  717. return -ENOMEM;
  718. }
  719. } else {
  720. const struct file_operations *ops;
  721. if (entry->content == SNDRV_INFO_CONTENT_DATA)
  722. ops = &snd_info_entry_operations;
  723. else
  724. ops = &snd_info_text_entry_ops;
  725. p = proc_create_data(entry->name, entry->mode, root,
  726. ops, entry);
  727. if (!p) {
  728. mutex_unlock(&info_mutex);
  729. return -ENOMEM;
  730. }
  731. proc_set_size(p, entry->size);
  732. }
  733. entry->p = p;
  734. if (entry->parent)
  735. list_add_tail(&entry->list, &entry->parent->children);
  736. mutex_unlock(&info_mutex);
  737. return 0;
  738. }
  739. EXPORT_SYMBOL(snd_info_register);
  740. /*
  741. */
  742. static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  743. {
  744. snd_iprintf(buffer,
  745. "Advanced Linux Sound Architecture Driver Version k%s.\n",
  746. init_utsname()->release);
  747. }
  748. static int __init snd_info_version_init(void)
  749. {
  750. struct snd_info_entry *entry;
  751. entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
  752. if (entry == NULL)
  753. return -ENOMEM;
  754. entry->c.text.read = snd_info_version_read;
  755. return snd_info_register(entry); /* freed in error path */
  756. }