info.c 20 KB

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