info.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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 *
  408. snd_info_create_entry(const char *name, struct snd_info_entry *parent);
  409. int __init snd_info_init(void)
  410. {
  411. snd_proc_root = snd_info_create_entry("asound", NULL);
  412. if (!snd_proc_root)
  413. return -ENOMEM;
  414. snd_proc_root->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  415. snd_proc_root->p = proc_mkdir("asound", NULL);
  416. if (!snd_proc_root->p)
  417. goto error;
  418. #ifdef CONFIG_SND_OSSEMUL
  419. snd_oss_root = create_subdir(THIS_MODULE, "oss");
  420. if (!snd_oss_root)
  421. goto error;
  422. #endif
  423. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  424. snd_seq_root = create_subdir(THIS_MODULE, "seq");
  425. if (!snd_seq_root)
  426. goto error;
  427. #endif
  428. if (snd_info_version_init() < 0 ||
  429. snd_minor_info_init() < 0 ||
  430. snd_minor_info_oss_init() < 0 ||
  431. snd_card_info_init() < 0 ||
  432. snd_info_minor_register() < 0)
  433. goto error;
  434. return 0;
  435. error:
  436. snd_info_free_entry(snd_proc_root);
  437. return -ENOMEM;
  438. }
  439. int __exit snd_info_done(void)
  440. {
  441. snd_info_free_entry(snd_proc_root);
  442. return 0;
  443. }
  444. /*
  445. * create a card proc file
  446. * called from init.c
  447. */
  448. int snd_info_card_create(struct snd_card *card)
  449. {
  450. char str[8];
  451. struct snd_info_entry *entry;
  452. if (snd_BUG_ON(!card))
  453. return -ENXIO;
  454. sprintf(str, "card%i", card->number);
  455. entry = create_subdir(card->module, str);
  456. if (!entry)
  457. return -ENOMEM;
  458. card->proc_root = entry;
  459. return 0;
  460. }
  461. /* register all pending info entries */
  462. static int snd_info_register_recursive(struct snd_info_entry *entry)
  463. {
  464. struct snd_info_entry *p;
  465. int err;
  466. if (!entry->p) {
  467. err = snd_info_register(entry);
  468. if (err < 0)
  469. return err;
  470. }
  471. list_for_each_entry(p, &entry->children, list) {
  472. err = snd_info_register_recursive(p);
  473. if (err < 0)
  474. return err;
  475. }
  476. return 0;
  477. }
  478. /*
  479. * register the card proc file
  480. * called from init.c
  481. * can be called multiple times for reinitialization
  482. */
  483. int snd_info_card_register(struct snd_card *card)
  484. {
  485. struct proc_dir_entry *p;
  486. int err;
  487. if (snd_BUG_ON(!card))
  488. return -ENXIO;
  489. err = snd_info_register_recursive(card->proc_root);
  490. if (err < 0)
  491. return err;
  492. if (!strcmp(card->id, card->proc_root->name))
  493. return 0;
  494. if (card->proc_root_link)
  495. return 0;
  496. p = proc_symlink(card->id, snd_proc_root->p, card->proc_root->name);
  497. if (!p)
  498. return -ENOMEM;
  499. card->proc_root_link = p;
  500. return 0;
  501. }
  502. /*
  503. * called on card->id change
  504. */
  505. void snd_info_card_id_change(struct snd_card *card)
  506. {
  507. mutex_lock(&info_mutex);
  508. if (card->proc_root_link) {
  509. proc_remove(card->proc_root_link);
  510. card->proc_root_link = NULL;
  511. }
  512. if (strcmp(card->id, card->proc_root->name))
  513. card->proc_root_link = proc_symlink(card->id,
  514. snd_proc_root->p,
  515. card->proc_root->name);
  516. mutex_unlock(&info_mutex);
  517. }
  518. /*
  519. * de-register the card proc file
  520. * called from init.c
  521. */
  522. void snd_info_card_disconnect(struct snd_card *card)
  523. {
  524. if (!card)
  525. return;
  526. mutex_lock(&info_mutex);
  527. proc_remove(card->proc_root_link);
  528. card->proc_root_link = NULL;
  529. if (card->proc_root)
  530. snd_info_disconnect(card->proc_root);
  531. mutex_unlock(&info_mutex);
  532. }
  533. /*
  534. * release the card proc file resources
  535. * called from init.c
  536. */
  537. int snd_info_card_free(struct snd_card *card)
  538. {
  539. if (!card)
  540. return 0;
  541. snd_info_free_entry(card->proc_root);
  542. card->proc_root = NULL;
  543. return 0;
  544. }
  545. /**
  546. * snd_info_get_line - read one line from the procfs buffer
  547. * @buffer: the procfs buffer
  548. * @line: the buffer to store
  549. * @len: the max. buffer size
  550. *
  551. * Reads one line from the buffer and stores the string.
  552. *
  553. * Return: Zero if successful, or 1 if error or EOF.
  554. */
  555. int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
  556. {
  557. int c = -1;
  558. if (snd_BUG_ON(!buffer || !buffer->buffer))
  559. return 1;
  560. if (len <= 0 || buffer->stop || buffer->error)
  561. return 1;
  562. while (!buffer->stop) {
  563. c = buffer->buffer[buffer->curr++];
  564. if (buffer->curr >= buffer->size)
  565. buffer->stop = 1;
  566. if (c == '\n')
  567. break;
  568. if (len > 1) {
  569. len--;
  570. *line++ = c;
  571. }
  572. }
  573. *line = '\0';
  574. return 0;
  575. }
  576. EXPORT_SYMBOL(snd_info_get_line);
  577. /**
  578. * snd_info_get_str - parse a string token
  579. * @dest: the buffer to store the string token
  580. * @src: the original string
  581. * @len: the max. length of token - 1
  582. *
  583. * Parses the original string and copy a token to the given
  584. * string buffer.
  585. *
  586. * Return: The updated pointer of the original string so that
  587. * it can be used for the next call.
  588. */
  589. const char *snd_info_get_str(char *dest, const char *src, int len)
  590. {
  591. int c;
  592. while (*src == ' ' || *src == '\t')
  593. src++;
  594. if (*src == '"' || *src == '\'') {
  595. c = *src++;
  596. while (--len > 0 && *src && *src != c) {
  597. *dest++ = *src++;
  598. }
  599. if (*src == c)
  600. src++;
  601. } else {
  602. while (--len > 0 && *src && *src != ' ' && *src != '\t') {
  603. *dest++ = *src++;
  604. }
  605. }
  606. *dest = 0;
  607. while (*src == ' ' || *src == '\t')
  608. src++;
  609. return src;
  610. }
  611. EXPORT_SYMBOL(snd_info_get_str);
  612. /*
  613. * snd_info_create_entry - create an info entry
  614. * @name: the proc file name
  615. * @parent: the parent directory
  616. *
  617. * Creates an info entry with the given file name and initializes as
  618. * the default state.
  619. *
  620. * Usually called from other functions such as
  621. * snd_info_create_card_entry().
  622. *
  623. * Return: The pointer of the new instance, or %NULL on failure.
  624. */
  625. static struct snd_info_entry *
  626. snd_info_create_entry(const char *name, struct snd_info_entry *parent)
  627. {
  628. struct snd_info_entry *entry;
  629. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  630. if (entry == NULL)
  631. return NULL;
  632. entry->name = kstrdup(name, GFP_KERNEL);
  633. if (entry->name == NULL) {
  634. kfree(entry);
  635. return NULL;
  636. }
  637. entry->mode = S_IFREG | S_IRUGO;
  638. entry->content = SNDRV_INFO_CONTENT_TEXT;
  639. mutex_init(&entry->access);
  640. INIT_LIST_HEAD(&entry->children);
  641. INIT_LIST_HEAD(&entry->list);
  642. entry->parent = parent;
  643. if (parent)
  644. list_add_tail(&entry->list, &parent->children);
  645. return entry;
  646. }
  647. /**
  648. * snd_info_create_module_entry - create an info entry for the given module
  649. * @module: the module pointer
  650. * @name: the file name
  651. * @parent: the parent directory
  652. *
  653. * Creates a new info entry and assigns it to the given module.
  654. *
  655. * Return: The pointer of the new instance, or %NULL on failure.
  656. */
  657. struct snd_info_entry *snd_info_create_module_entry(struct module * module,
  658. const char *name,
  659. struct snd_info_entry *parent)
  660. {
  661. struct snd_info_entry *entry = snd_info_create_entry(name, parent);
  662. if (entry)
  663. entry->module = module;
  664. return entry;
  665. }
  666. EXPORT_SYMBOL(snd_info_create_module_entry);
  667. /**
  668. * snd_info_create_card_entry - create an info entry for the given card
  669. * @card: the card instance
  670. * @name: the file name
  671. * @parent: the parent directory
  672. *
  673. * Creates a new info entry and assigns it to the given card.
  674. *
  675. * Return: The pointer of the new instance, or %NULL on failure.
  676. */
  677. struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
  678. const char *name,
  679. struct snd_info_entry * parent)
  680. {
  681. struct snd_info_entry *entry = snd_info_create_entry(name, parent);
  682. if (entry) {
  683. entry->module = card->module;
  684. entry->card = card;
  685. }
  686. return entry;
  687. }
  688. EXPORT_SYMBOL(snd_info_create_card_entry);
  689. static void snd_info_disconnect(struct snd_info_entry *entry)
  690. {
  691. struct snd_info_entry *p;
  692. if (!entry->p)
  693. return;
  694. list_for_each_entry(p, &entry->children, list)
  695. snd_info_disconnect(p);
  696. proc_remove(entry->p);
  697. entry->p = NULL;
  698. }
  699. /**
  700. * snd_info_free_entry - release the info entry
  701. * @entry: the info entry
  702. *
  703. * Releases the info entry.
  704. */
  705. void snd_info_free_entry(struct snd_info_entry * entry)
  706. {
  707. struct snd_info_entry *p, *n;
  708. if (!entry)
  709. return;
  710. if (entry->p) {
  711. mutex_lock(&info_mutex);
  712. snd_info_disconnect(entry);
  713. mutex_unlock(&info_mutex);
  714. }
  715. /* free all children at first */
  716. list_for_each_entry_safe(p, n, &entry->children, list)
  717. snd_info_free_entry(p);
  718. list_del(&entry->list);
  719. kfree(entry->name);
  720. if (entry->private_free)
  721. entry->private_free(entry);
  722. kfree(entry);
  723. }
  724. EXPORT_SYMBOL(snd_info_free_entry);
  725. /**
  726. * snd_info_register - register the info entry
  727. * @entry: the info entry
  728. *
  729. * Registers the proc info entry.
  730. *
  731. * Return: Zero if successful, or a negative error code on failure.
  732. */
  733. int snd_info_register(struct snd_info_entry * entry)
  734. {
  735. struct proc_dir_entry *root, *p = NULL;
  736. if (snd_BUG_ON(!entry))
  737. return -ENXIO;
  738. root = entry->parent == NULL ? snd_proc_root->p : entry->parent->p;
  739. mutex_lock(&info_mutex);
  740. if (S_ISDIR(entry->mode)) {
  741. p = proc_mkdir_mode(entry->name, entry->mode, root);
  742. if (!p) {
  743. mutex_unlock(&info_mutex);
  744. return -ENOMEM;
  745. }
  746. } else {
  747. const struct file_operations *ops;
  748. if (entry->content == SNDRV_INFO_CONTENT_DATA)
  749. ops = &snd_info_entry_operations;
  750. else
  751. ops = &snd_info_text_entry_ops;
  752. p = proc_create_data(entry->name, entry->mode, root,
  753. ops, entry);
  754. if (!p) {
  755. mutex_unlock(&info_mutex);
  756. return -ENOMEM;
  757. }
  758. proc_set_size(p, entry->size);
  759. }
  760. entry->p = p;
  761. mutex_unlock(&info_mutex);
  762. return 0;
  763. }
  764. EXPORT_SYMBOL(snd_info_register);
  765. /*
  766. */
  767. static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  768. {
  769. snd_iprintf(buffer,
  770. "Advanced Linux Sound Architecture Driver Version k%s.\n",
  771. init_utsname()->release);
  772. }
  773. static int __init snd_info_version_init(void)
  774. {
  775. struct snd_info_entry *entry;
  776. entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
  777. if (entry == NULL)
  778. return -ENOMEM;
  779. entry->c.text.read = snd_info_version_read;
  780. return snd_info_register(entry); /* freed in error path */
  781. }