info.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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. if (!entry->c.text.write)
  292. return -EIO;
  293. pos = *offset;
  294. if (!valid_pos(pos, count))
  295. return -EIO;
  296. next = pos + count;
  297. /* don't handle too large text inputs */
  298. if (next > 16 * 1024)
  299. return -EIO;
  300. mutex_lock(&entry->access);
  301. buf = data->wbuffer;
  302. if (!buf) {
  303. data->wbuffer = buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  304. if (!buf) {
  305. err = -ENOMEM;
  306. goto error;
  307. }
  308. }
  309. if (next > buf->len) {
  310. char *nbuf = kvzalloc(PAGE_ALIGN(next), GFP_KERNEL);
  311. if (!nbuf) {
  312. err = -ENOMEM;
  313. goto error;
  314. }
  315. kvfree(buf->buffer);
  316. buf->buffer = nbuf;
  317. buf->len = PAGE_ALIGN(next);
  318. }
  319. if (copy_from_user(buf->buffer + pos, buffer, count)) {
  320. err = -EFAULT;
  321. goto error;
  322. }
  323. buf->size = next;
  324. error:
  325. mutex_unlock(&entry->access);
  326. if (err < 0)
  327. return err;
  328. *offset = next;
  329. return count;
  330. }
  331. static int snd_info_seq_show(struct seq_file *seq, void *p)
  332. {
  333. struct snd_info_private_data *data = seq->private;
  334. struct snd_info_entry *entry = data->entry;
  335. if (!entry->c.text.read) {
  336. return -EIO;
  337. } else {
  338. data->rbuffer->buffer = (char *)seq; /* XXX hack! */
  339. entry->c.text.read(entry, data->rbuffer);
  340. }
  341. return 0;
  342. }
  343. static int snd_info_text_entry_open(struct inode *inode, struct file *file)
  344. {
  345. struct snd_info_entry *entry = PDE_DATA(inode);
  346. struct snd_info_private_data *data;
  347. int err;
  348. mutex_lock(&info_mutex);
  349. err = alloc_info_private(entry, &data);
  350. if (err < 0)
  351. goto unlock;
  352. data->rbuffer = kzalloc(sizeof(*data->rbuffer), GFP_KERNEL);
  353. if (!data->rbuffer) {
  354. err = -ENOMEM;
  355. goto error;
  356. }
  357. if (entry->size)
  358. err = single_open_size(file, snd_info_seq_show, data,
  359. entry->size);
  360. else
  361. err = single_open(file, snd_info_seq_show, data);
  362. if (err < 0)
  363. goto error;
  364. mutex_unlock(&info_mutex);
  365. return 0;
  366. error:
  367. kfree(data->rbuffer);
  368. kfree(data);
  369. module_put(entry->module);
  370. unlock:
  371. mutex_unlock(&info_mutex);
  372. return err;
  373. }
  374. static int snd_info_text_entry_release(struct inode *inode, struct file *file)
  375. {
  376. struct seq_file *m = file->private_data;
  377. struct snd_info_private_data *data = m->private;
  378. struct snd_info_entry *entry = data->entry;
  379. if (data->wbuffer && entry->c.text.write)
  380. entry->c.text.write(entry, data->wbuffer);
  381. single_release(inode, file);
  382. kfree(data->rbuffer);
  383. if (data->wbuffer) {
  384. kvfree(data->wbuffer->buffer);
  385. kfree(data->wbuffer);
  386. }
  387. module_put(entry->module);
  388. kfree(data);
  389. return 0;
  390. }
  391. static const struct file_operations snd_info_text_entry_ops =
  392. {
  393. .owner = THIS_MODULE,
  394. .open = snd_info_text_entry_open,
  395. .release = snd_info_text_entry_release,
  396. .write = snd_info_text_entry_write,
  397. .llseek = seq_lseek,
  398. .read = seq_read,
  399. };
  400. static struct snd_info_entry *create_subdir(struct module *mod,
  401. const char *name)
  402. {
  403. struct snd_info_entry *entry;
  404. entry = snd_info_create_module_entry(mod, name, NULL);
  405. if (!entry)
  406. return NULL;
  407. entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  408. if (snd_info_register(entry) < 0) {
  409. snd_info_free_entry(entry);
  410. return NULL;
  411. }
  412. return entry;
  413. }
  414. static struct snd_info_entry *
  415. snd_info_create_entry(const char *name, struct snd_info_entry *parent);
  416. int __init snd_info_init(void)
  417. {
  418. snd_proc_root = snd_info_create_entry("asound", NULL);
  419. if (!snd_proc_root)
  420. return -ENOMEM;
  421. snd_proc_root->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  422. snd_proc_root->p = proc_mkdir("asound", NULL);
  423. if (!snd_proc_root->p)
  424. goto error;
  425. #ifdef CONFIG_SND_OSSEMUL
  426. snd_oss_root = create_subdir(THIS_MODULE, "oss");
  427. if (!snd_oss_root)
  428. goto error;
  429. #endif
  430. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  431. snd_seq_root = create_subdir(THIS_MODULE, "seq");
  432. if (!snd_seq_root)
  433. goto error;
  434. #endif
  435. if (snd_info_version_init() < 0 ||
  436. snd_minor_info_init() < 0 ||
  437. snd_minor_info_oss_init() < 0 ||
  438. snd_card_info_init() < 0 ||
  439. snd_info_minor_register() < 0)
  440. goto error;
  441. return 0;
  442. error:
  443. snd_info_free_entry(snd_proc_root);
  444. return -ENOMEM;
  445. }
  446. int __exit snd_info_done(void)
  447. {
  448. snd_info_free_entry(snd_proc_root);
  449. return 0;
  450. }
  451. /*
  452. * create a card proc file
  453. * called from init.c
  454. */
  455. int snd_info_card_create(struct snd_card *card)
  456. {
  457. char str[8];
  458. struct snd_info_entry *entry;
  459. if (snd_BUG_ON(!card))
  460. return -ENXIO;
  461. sprintf(str, "card%i", card->number);
  462. entry = create_subdir(card->module, str);
  463. if (!entry)
  464. return -ENOMEM;
  465. card->proc_root = entry;
  466. return 0;
  467. }
  468. /* register all pending info entries */
  469. static int snd_info_register_recursive(struct snd_info_entry *entry)
  470. {
  471. struct snd_info_entry *p;
  472. int err;
  473. if (!entry->p) {
  474. err = snd_info_register(entry);
  475. if (err < 0)
  476. return err;
  477. }
  478. list_for_each_entry(p, &entry->children, list) {
  479. err = snd_info_register_recursive(p);
  480. if (err < 0)
  481. return err;
  482. }
  483. return 0;
  484. }
  485. /*
  486. * register the card proc file
  487. * called from init.c
  488. * can be called multiple times for reinitialization
  489. */
  490. int snd_info_card_register(struct snd_card *card)
  491. {
  492. struct proc_dir_entry *p;
  493. int err;
  494. if (snd_BUG_ON(!card))
  495. return -ENXIO;
  496. err = snd_info_register_recursive(card->proc_root);
  497. if (err < 0)
  498. return err;
  499. if (!strcmp(card->id, card->proc_root->name))
  500. return 0;
  501. if (card->proc_root_link)
  502. return 0;
  503. p = proc_symlink(card->id, snd_proc_root->p, card->proc_root->name);
  504. if (!p)
  505. return -ENOMEM;
  506. card->proc_root_link = p;
  507. return 0;
  508. }
  509. /*
  510. * called on card->id change
  511. */
  512. void snd_info_card_id_change(struct snd_card *card)
  513. {
  514. mutex_lock(&info_mutex);
  515. if (card->proc_root_link) {
  516. proc_remove(card->proc_root_link);
  517. card->proc_root_link = NULL;
  518. }
  519. if (strcmp(card->id, card->proc_root->name))
  520. card->proc_root_link = proc_symlink(card->id,
  521. snd_proc_root->p,
  522. card->proc_root->name);
  523. mutex_unlock(&info_mutex);
  524. }
  525. /*
  526. * de-register the card proc file
  527. * called from init.c
  528. */
  529. void snd_info_card_disconnect(struct snd_card *card)
  530. {
  531. if (!card)
  532. return;
  533. mutex_lock(&info_mutex);
  534. proc_remove(card->proc_root_link);
  535. card->proc_root_link = NULL;
  536. if (card->proc_root)
  537. snd_info_disconnect(card->proc_root);
  538. mutex_unlock(&info_mutex);
  539. }
  540. /*
  541. * release the card proc file resources
  542. * called from init.c
  543. */
  544. int snd_info_card_free(struct snd_card *card)
  545. {
  546. if (!card)
  547. return 0;
  548. snd_info_free_entry(card->proc_root);
  549. card->proc_root = NULL;
  550. return 0;
  551. }
  552. /**
  553. * snd_info_get_line - read one line from the procfs buffer
  554. * @buffer: the procfs buffer
  555. * @line: the buffer to store
  556. * @len: the max. buffer size
  557. *
  558. * Reads one line from the buffer and stores the string.
  559. *
  560. * Return: Zero if successful, or 1 if error or EOF.
  561. */
  562. int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
  563. {
  564. int c = -1;
  565. if (snd_BUG_ON(!buffer))
  566. return 1;
  567. if (!buffer->buffer)
  568. return 1;
  569. if (len <= 0 || buffer->stop || buffer->error)
  570. return 1;
  571. while (!buffer->stop) {
  572. c = buffer->buffer[buffer->curr++];
  573. if (buffer->curr >= buffer->size)
  574. buffer->stop = 1;
  575. if (c == '\n')
  576. break;
  577. if (len > 1) {
  578. len--;
  579. *line++ = c;
  580. }
  581. }
  582. *line = '\0';
  583. return 0;
  584. }
  585. EXPORT_SYMBOL(snd_info_get_line);
  586. /**
  587. * snd_info_get_str - parse a string token
  588. * @dest: the buffer to store the string token
  589. * @src: the original string
  590. * @len: the max. length of token - 1
  591. *
  592. * Parses the original string and copy a token to the given
  593. * string buffer.
  594. *
  595. * Return: The updated pointer of the original string so that
  596. * it can be used for the next call.
  597. */
  598. const char *snd_info_get_str(char *dest, const char *src, int len)
  599. {
  600. int c;
  601. while (*src == ' ' || *src == '\t')
  602. src++;
  603. if (*src == '"' || *src == '\'') {
  604. c = *src++;
  605. while (--len > 0 && *src && *src != c) {
  606. *dest++ = *src++;
  607. }
  608. if (*src == c)
  609. src++;
  610. } else {
  611. while (--len > 0 && *src && *src != ' ' && *src != '\t') {
  612. *dest++ = *src++;
  613. }
  614. }
  615. *dest = 0;
  616. while (*src == ' ' || *src == '\t')
  617. src++;
  618. return src;
  619. }
  620. EXPORT_SYMBOL(snd_info_get_str);
  621. /*
  622. * snd_info_create_entry - create an info entry
  623. * @name: the proc file name
  624. * @parent: the parent directory
  625. *
  626. * Creates an info entry with the given file name and initializes as
  627. * the default state.
  628. *
  629. * Usually called from other functions such as
  630. * snd_info_create_card_entry().
  631. *
  632. * Return: The pointer of the new instance, or %NULL on failure.
  633. */
  634. static struct snd_info_entry *
  635. snd_info_create_entry(const char *name, struct snd_info_entry *parent)
  636. {
  637. struct snd_info_entry *entry;
  638. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  639. if (entry == NULL)
  640. return NULL;
  641. entry->name = kstrdup(name, GFP_KERNEL);
  642. if (entry->name == NULL) {
  643. kfree(entry);
  644. return NULL;
  645. }
  646. entry->mode = S_IFREG | S_IRUGO;
  647. entry->content = SNDRV_INFO_CONTENT_TEXT;
  648. mutex_init(&entry->access);
  649. INIT_LIST_HEAD(&entry->children);
  650. INIT_LIST_HEAD(&entry->list);
  651. entry->parent = parent;
  652. if (parent) {
  653. mutex_lock(&parent->access);
  654. list_add_tail(&entry->list, &parent->children);
  655. mutex_unlock(&parent->access);
  656. }
  657. return entry;
  658. }
  659. /**
  660. * snd_info_create_module_entry - create an info entry for the given module
  661. * @module: the module pointer
  662. * @name: the file name
  663. * @parent: the parent directory
  664. *
  665. * Creates a new info entry and assigns it to the given module.
  666. *
  667. * Return: The pointer of the new instance, or %NULL on failure.
  668. */
  669. struct snd_info_entry *snd_info_create_module_entry(struct module * module,
  670. const char *name,
  671. struct snd_info_entry *parent)
  672. {
  673. struct snd_info_entry *entry = snd_info_create_entry(name, parent);
  674. if (entry)
  675. entry->module = module;
  676. return entry;
  677. }
  678. EXPORT_SYMBOL(snd_info_create_module_entry);
  679. /**
  680. * snd_info_create_card_entry - create an info entry for the given card
  681. * @card: the card instance
  682. * @name: the file name
  683. * @parent: the parent directory
  684. *
  685. * Creates a new info entry and assigns it to the given card.
  686. *
  687. * Return: The pointer of the new instance, or %NULL on failure.
  688. */
  689. struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
  690. const char *name,
  691. struct snd_info_entry * parent)
  692. {
  693. struct snd_info_entry *entry = snd_info_create_entry(name, parent);
  694. if (entry) {
  695. entry->module = card->module;
  696. entry->card = card;
  697. }
  698. return entry;
  699. }
  700. EXPORT_SYMBOL(snd_info_create_card_entry);
  701. static void snd_info_disconnect(struct snd_info_entry *entry)
  702. {
  703. struct snd_info_entry *p;
  704. if (!entry->p)
  705. return;
  706. list_for_each_entry(p, &entry->children, list)
  707. snd_info_disconnect(p);
  708. proc_remove(entry->p);
  709. entry->p = NULL;
  710. }
  711. /**
  712. * snd_info_free_entry - release the info entry
  713. * @entry: the info entry
  714. *
  715. * Releases the info entry.
  716. */
  717. void snd_info_free_entry(struct snd_info_entry * entry)
  718. {
  719. struct snd_info_entry *p, *n;
  720. if (!entry)
  721. return;
  722. if (entry->p) {
  723. mutex_lock(&info_mutex);
  724. snd_info_disconnect(entry);
  725. mutex_unlock(&info_mutex);
  726. }
  727. /* free all children at first */
  728. list_for_each_entry_safe(p, n, &entry->children, list)
  729. snd_info_free_entry(p);
  730. p = entry->parent;
  731. if (p) {
  732. mutex_lock(&p->access);
  733. list_del(&entry->list);
  734. mutex_unlock(&p->access);
  735. }
  736. kfree(entry->name);
  737. if (entry->private_free)
  738. entry->private_free(entry);
  739. kfree(entry);
  740. }
  741. EXPORT_SYMBOL(snd_info_free_entry);
  742. /**
  743. * snd_info_register - register the info entry
  744. * @entry: the info entry
  745. *
  746. * Registers the proc info entry.
  747. *
  748. * Return: Zero if successful, or a negative error code on failure.
  749. */
  750. int snd_info_register(struct snd_info_entry * entry)
  751. {
  752. struct proc_dir_entry *root, *p = NULL;
  753. if (snd_BUG_ON(!entry))
  754. return -ENXIO;
  755. root = entry->parent == NULL ? snd_proc_root->p : entry->parent->p;
  756. mutex_lock(&info_mutex);
  757. if (S_ISDIR(entry->mode)) {
  758. p = proc_mkdir_mode(entry->name, entry->mode, root);
  759. if (!p) {
  760. mutex_unlock(&info_mutex);
  761. return -ENOMEM;
  762. }
  763. } else {
  764. const struct file_operations *ops;
  765. if (entry->content == SNDRV_INFO_CONTENT_DATA)
  766. ops = &snd_info_entry_operations;
  767. else
  768. ops = &snd_info_text_entry_ops;
  769. p = proc_create_data(entry->name, entry->mode, root,
  770. ops, entry);
  771. if (!p) {
  772. mutex_unlock(&info_mutex);
  773. return -ENOMEM;
  774. }
  775. proc_set_size(p, entry->size);
  776. }
  777. entry->p = p;
  778. mutex_unlock(&info_mutex);
  779. return 0;
  780. }
  781. EXPORT_SYMBOL(snd_info_register);
  782. /*
  783. */
  784. static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  785. {
  786. snd_iprintf(buffer,
  787. "Advanced Linux Sound Architecture Driver Version k%s.\n",
  788. init_utsname()->release);
  789. }
  790. static int __init snd_info_version_init(void)
  791. {
  792. struct snd_info_entry *entry;
  793. entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
  794. if (entry == NULL)
  795. return -ENOMEM;
  796. entry->c.text.read = snd_info_version_read;
  797. return snd_info_register(entry); /* freed in error path */
  798. }