info.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. /*
  35. *
  36. */
  37. #ifdef CONFIG_PROC_FS
  38. int snd_info_check_reserved_words(const char *str)
  39. {
  40. static char *reserved[] =
  41. {
  42. "version",
  43. "meminfo",
  44. "memdebug",
  45. "detect",
  46. "devices",
  47. "oss",
  48. "cards",
  49. "timers",
  50. "synth",
  51. "pcm",
  52. "seq",
  53. NULL
  54. };
  55. char **xstr = reserved;
  56. while (*xstr) {
  57. if (!strcmp(*xstr, str))
  58. return 0;
  59. xstr++;
  60. }
  61. if (!strncmp(str, "card", 4))
  62. return 0;
  63. return 1;
  64. }
  65. static DEFINE_MUTEX(info_mutex);
  66. struct snd_info_private_data {
  67. struct snd_info_buffer *rbuffer;
  68. struct snd_info_buffer *wbuffer;
  69. struct snd_info_entry *entry;
  70. void *file_private_data;
  71. };
  72. static int snd_info_version_init(void);
  73. static int snd_info_version_done(void);
  74. static void snd_info_disconnect(struct snd_info_entry *entry);
  75. /* resize the proc r/w buffer */
  76. static int resize_info_buffer(struct snd_info_buffer *buffer,
  77. unsigned int nsize)
  78. {
  79. char *nbuf;
  80. nsize = PAGE_ALIGN(nsize);
  81. nbuf = krealloc(buffer->buffer, nsize, GFP_KERNEL | __GFP_ZERO);
  82. if (! nbuf)
  83. return -ENOMEM;
  84. buffer->buffer = nbuf;
  85. buffer->len = nsize;
  86. return 0;
  87. }
  88. /**
  89. * snd_iprintf - printf on the procfs buffer
  90. * @buffer: the procfs buffer
  91. * @fmt: the printf format
  92. *
  93. * Outputs the string on the procfs buffer just like printf().
  94. *
  95. * Return: The size of output string, or a negative error code.
  96. */
  97. int snd_iprintf(struct snd_info_buffer *buffer, const char *fmt, ...)
  98. {
  99. va_list args;
  100. int len, res;
  101. int err = 0;
  102. might_sleep();
  103. if (buffer->stop || buffer->error)
  104. return 0;
  105. len = buffer->len - buffer->size;
  106. va_start(args, fmt);
  107. for (;;) {
  108. va_list ap;
  109. va_copy(ap, args);
  110. res = vsnprintf(buffer->buffer + buffer->curr, len, fmt, ap);
  111. va_end(ap);
  112. if (res < len)
  113. break;
  114. err = resize_info_buffer(buffer, buffer->len + PAGE_SIZE);
  115. if (err < 0)
  116. break;
  117. len = buffer->len - buffer->size;
  118. }
  119. va_end(args);
  120. if (err < 0)
  121. return err;
  122. buffer->curr += res;
  123. buffer->size += res;
  124. return res;
  125. }
  126. EXPORT_SYMBOL(snd_iprintf);
  127. /*
  128. */
  129. static struct proc_dir_entry *snd_proc_root;
  130. struct snd_info_entry *snd_seq_root;
  131. EXPORT_SYMBOL(snd_seq_root);
  132. #ifdef CONFIG_SND_OSSEMUL
  133. struct snd_info_entry *snd_oss_root;
  134. #endif
  135. static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
  136. {
  137. struct snd_info_private_data *data;
  138. struct snd_info_entry *entry;
  139. loff_t ret = -EINVAL, size;
  140. data = file->private_data;
  141. entry = data->entry;
  142. mutex_lock(&entry->access);
  143. if (entry->content == SNDRV_INFO_CONTENT_DATA &&
  144. entry->c.ops->llseek) {
  145. offset = entry->c.ops->llseek(entry,
  146. data->file_private_data,
  147. file, offset, orig);
  148. goto out;
  149. }
  150. if (entry->content == SNDRV_INFO_CONTENT_DATA)
  151. size = entry->size;
  152. else
  153. size = 0;
  154. switch (orig) {
  155. case SEEK_SET:
  156. break;
  157. case SEEK_CUR:
  158. offset += file->f_pos;
  159. break;
  160. case SEEK_END:
  161. if (!size)
  162. goto out;
  163. offset += size;
  164. break;
  165. default:
  166. goto out;
  167. }
  168. if (offset < 0)
  169. goto out;
  170. if (size && offset > size)
  171. offset = size;
  172. file->f_pos = offset;
  173. ret = offset;
  174. out:
  175. mutex_unlock(&entry->access);
  176. return ret;
  177. }
  178. static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
  179. size_t count, loff_t * offset)
  180. {
  181. struct snd_info_private_data *data;
  182. struct snd_info_entry *entry;
  183. struct snd_info_buffer *buf;
  184. size_t size = 0;
  185. loff_t pos;
  186. data = file->private_data;
  187. if (snd_BUG_ON(!data))
  188. return -ENXIO;
  189. pos = *offset;
  190. if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
  191. return -EIO;
  192. if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
  193. return -EIO;
  194. entry = data->entry;
  195. switch (entry->content) {
  196. case SNDRV_INFO_CONTENT_TEXT:
  197. buf = data->rbuffer;
  198. if (buf == NULL)
  199. return -EIO;
  200. if (pos >= buf->size)
  201. return 0;
  202. size = buf->size - pos;
  203. size = min(count, size);
  204. if (copy_to_user(buffer, buf->buffer + pos, size))
  205. return -EFAULT;
  206. break;
  207. case SNDRV_INFO_CONTENT_DATA:
  208. if (pos >= entry->size)
  209. return 0;
  210. if (entry->c.ops->read) {
  211. size = entry->size - pos;
  212. size = min(count, size);
  213. size = entry->c.ops->read(entry,
  214. data->file_private_data,
  215. file, buffer, size, pos);
  216. }
  217. break;
  218. }
  219. if ((ssize_t) size > 0)
  220. *offset = pos + size;
  221. return size;
  222. }
  223. static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
  224. size_t count, loff_t * offset)
  225. {
  226. struct snd_info_private_data *data;
  227. struct snd_info_entry *entry;
  228. struct snd_info_buffer *buf;
  229. ssize_t size = 0;
  230. loff_t pos;
  231. unsigned long realloc_size;
  232. data = file->private_data;
  233. if (snd_BUG_ON(!data))
  234. return -ENXIO;
  235. entry = data->entry;
  236. pos = *offset;
  237. if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
  238. return -EIO;
  239. realloc_size = (unsigned long) pos + (unsigned long) count;
  240. if (realloc_size < (unsigned long) pos || realloc_size > UINT_MAX)
  241. return -EIO;
  242. switch (entry->content) {
  243. case SNDRV_INFO_CONTENT_TEXT:
  244. buf = data->wbuffer;
  245. if (buf == NULL)
  246. return -EIO;
  247. mutex_lock(&entry->access);
  248. if (pos + count >= buf->len) {
  249. if (resize_info_buffer(buf, pos + count)) {
  250. mutex_unlock(&entry->access);
  251. return -ENOMEM;
  252. }
  253. }
  254. if (copy_from_user(buf->buffer + pos, buffer, count)) {
  255. mutex_unlock(&entry->access);
  256. return -EFAULT;
  257. }
  258. buf->size = pos + count;
  259. mutex_unlock(&entry->access);
  260. size = count;
  261. break;
  262. case SNDRV_INFO_CONTENT_DATA:
  263. if (entry->c.ops->write && count > 0) {
  264. size_t maxsize = entry->size - pos;
  265. count = min(count, maxsize);
  266. size = entry->c.ops->write(entry,
  267. data->file_private_data,
  268. file, buffer, count, pos);
  269. }
  270. break;
  271. }
  272. if ((ssize_t) size > 0)
  273. *offset = pos + size;
  274. return size;
  275. }
  276. static int snd_info_entry_open(struct inode *inode, struct file *file)
  277. {
  278. struct snd_info_entry *entry;
  279. struct snd_info_private_data *data;
  280. struct snd_info_buffer *buffer;
  281. int mode, err;
  282. mutex_lock(&info_mutex);
  283. entry = PDE_DATA(inode);
  284. if (entry == NULL || ! entry->p) {
  285. mutex_unlock(&info_mutex);
  286. return -ENODEV;
  287. }
  288. if (!try_module_get(entry->module)) {
  289. err = -EFAULT;
  290. goto __error1;
  291. }
  292. mode = file->f_flags & O_ACCMODE;
  293. if (mode == O_RDONLY || mode == O_RDWR) {
  294. if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
  295. entry->c.ops->read == NULL)) {
  296. err = -ENODEV;
  297. goto __error;
  298. }
  299. }
  300. if (mode == O_WRONLY || mode == O_RDWR) {
  301. if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
  302. entry->c.ops->write == NULL)) {
  303. err = -ENODEV;
  304. goto __error;
  305. }
  306. }
  307. data = kzalloc(sizeof(*data), GFP_KERNEL);
  308. if (data == NULL) {
  309. err = -ENOMEM;
  310. goto __error;
  311. }
  312. data->entry = entry;
  313. switch (entry->content) {
  314. case SNDRV_INFO_CONTENT_TEXT:
  315. if (mode == O_RDONLY || mode == O_RDWR) {
  316. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  317. if (buffer == NULL)
  318. goto __nomem;
  319. data->rbuffer = buffer;
  320. buffer->len = PAGE_SIZE;
  321. buffer->buffer = kzalloc(buffer->len, GFP_KERNEL);
  322. if (buffer->buffer == NULL)
  323. goto __nomem;
  324. }
  325. if (mode == O_WRONLY || mode == O_RDWR) {
  326. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  327. if (buffer == NULL)
  328. goto __nomem;
  329. data->wbuffer = buffer;
  330. buffer->len = PAGE_SIZE;
  331. buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
  332. if (buffer->buffer == NULL)
  333. goto __nomem;
  334. }
  335. break;
  336. case SNDRV_INFO_CONTENT_DATA: /* data */
  337. if (entry->c.ops->open) {
  338. if ((err = entry->c.ops->open(entry, mode,
  339. &data->file_private_data)) < 0) {
  340. kfree(data);
  341. goto __error;
  342. }
  343. }
  344. break;
  345. }
  346. file->private_data = data;
  347. mutex_unlock(&info_mutex);
  348. if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
  349. (mode == O_RDONLY || mode == O_RDWR)) {
  350. if (entry->c.text.read) {
  351. mutex_lock(&entry->access);
  352. entry->c.text.read(entry, data->rbuffer);
  353. mutex_unlock(&entry->access);
  354. }
  355. }
  356. return 0;
  357. __nomem:
  358. if (data->rbuffer) {
  359. kfree(data->rbuffer->buffer);
  360. kfree(data->rbuffer);
  361. }
  362. if (data->wbuffer) {
  363. kfree(data->wbuffer->buffer);
  364. kfree(data->wbuffer);
  365. }
  366. kfree(data);
  367. err = -ENOMEM;
  368. __error:
  369. module_put(entry->module);
  370. __error1:
  371. mutex_unlock(&info_mutex);
  372. return err;
  373. }
  374. static int snd_info_entry_release(struct inode *inode, struct file *file)
  375. {
  376. struct snd_info_entry *entry;
  377. struct snd_info_private_data *data;
  378. int mode;
  379. mode = file->f_flags & O_ACCMODE;
  380. data = file->private_data;
  381. entry = data->entry;
  382. switch (entry->content) {
  383. case SNDRV_INFO_CONTENT_TEXT:
  384. if (data->rbuffer) {
  385. kfree(data->rbuffer->buffer);
  386. kfree(data->rbuffer);
  387. }
  388. if (data->wbuffer) {
  389. if (entry->c.text.write) {
  390. entry->c.text.write(entry, data->wbuffer);
  391. if (data->wbuffer->error) {
  392. if (entry->card)
  393. dev_warn(entry->card->dev, "info: data write error to %s (%i)\n",
  394. entry->name,
  395. data->wbuffer->error);
  396. else
  397. pr_warn("ALSA: info: data write error to %s (%i)\n",
  398. entry->name,
  399. data->wbuffer->error);
  400. }
  401. }
  402. kfree(data->wbuffer->buffer);
  403. kfree(data->wbuffer);
  404. }
  405. break;
  406. case SNDRV_INFO_CONTENT_DATA:
  407. if (entry->c.ops->release)
  408. entry->c.ops->release(entry, mode,
  409. data->file_private_data);
  410. break;
  411. }
  412. module_put(entry->module);
  413. kfree(data);
  414. return 0;
  415. }
  416. static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
  417. {
  418. struct snd_info_private_data *data;
  419. struct snd_info_entry *entry;
  420. unsigned int mask;
  421. data = file->private_data;
  422. if (data == NULL)
  423. return 0;
  424. entry = data->entry;
  425. mask = 0;
  426. switch (entry->content) {
  427. case SNDRV_INFO_CONTENT_DATA:
  428. if (entry->c.ops->poll)
  429. return entry->c.ops->poll(entry,
  430. data->file_private_data,
  431. file, wait);
  432. if (entry->c.ops->read)
  433. mask |= POLLIN | POLLRDNORM;
  434. if (entry->c.ops->write)
  435. mask |= POLLOUT | POLLWRNORM;
  436. break;
  437. }
  438. return mask;
  439. }
  440. static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
  441. unsigned long arg)
  442. {
  443. struct snd_info_private_data *data;
  444. struct snd_info_entry *entry;
  445. data = file->private_data;
  446. if (data == NULL)
  447. return 0;
  448. entry = data->entry;
  449. switch (entry->content) {
  450. case SNDRV_INFO_CONTENT_DATA:
  451. if (entry->c.ops->ioctl)
  452. return entry->c.ops->ioctl(entry,
  453. data->file_private_data,
  454. file, cmd, arg);
  455. break;
  456. }
  457. return -ENOTTY;
  458. }
  459. static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
  460. {
  461. struct inode *inode = file_inode(file);
  462. struct snd_info_private_data *data;
  463. struct snd_info_entry *entry;
  464. data = file->private_data;
  465. if (data == NULL)
  466. return 0;
  467. entry = data->entry;
  468. switch (entry->content) {
  469. case SNDRV_INFO_CONTENT_DATA:
  470. if (entry->c.ops->mmap)
  471. return entry->c.ops->mmap(entry,
  472. data->file_private_data,
  473. inode, file, vma);
  474. break;
  475. }
  476. return -ENXIO;
  477. }
  478. static const struct file_operations snd_info_entry_operations =
  479. {
  480. .owner = THIS_MODULE,
  481. .llseek = snd_info_entry_llseek,
  482. .read = snd_info_entry_read,
  483. .write = snd_info_entry_write,
  484. .poll = snd_info_entry_poll,
  485. .unlocked_ioctl = snd_info_entry_ioctl,
  486. .mmap = snd_info_entry_mmap,
  487. .open = snd_info_entry_open,
  488. .release = snd_info_entry_release,
  489. };
  490. int __init snd_info_init(void)
  491. {
  492. struct proc_dir_entry *p;
  493. p = proc_mkdir("asound", NULL);
  494. if (p == NULL)
  495. return -ENOMEM;
  496. snd_proc_root = p;
  497. #ifdef CONFIG_SND_OSSEMUL
  498. {
  499. struct snd_info_entry *entry;
  500. if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
  501. return -ENOMEM;
  502. entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  503. if (snd_info_register(entry) < 0) {
  504. snd_info_free_entry(entry);
  505. return -ENOMEM;
  506. }
  507. snd_oss_root = entry;
  508. }
  509. #endif
  510. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  511. {
  512. struct snd_info_entry *entry;
  513. if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
  514. return -ENOMEM;
  515. entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  516. if (snd_info_register(entry) < 0) {
  517. snd_info_free_entry(entry);
  518. return -ENOMEM;
  519. }
  520. snd_seq_root = entry;
  521. }
  522. #endif
  523. snd_info_version_init();
  524. snd_minor_info_init();
  525. snd_minor_info_oss_init();
  526. snd_card_info_init();
  527. return 0;
  528. }
  529. int __exit snd_info_done(void)
  530. {
  531. snd_card_info_done();
  532. snd_minor_info_oss_done();
  533. snd_minor_info_done();
  534. snd_info_version_done();
  535. if (snd_proc_root) {
  536. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  537. snd_info_free_entry(snd_seq_root);
  538. #endif
  539. #ifdef CONFIG_SND_OSSEMUL
  540. snd_info_free_entry(snd_oss_root);
  541. #endif
  542. proc_remove(snd_proc_root);
  543. }
  544. return 0;
  545. }
  546. /*
  547. */
  548. /*
  549. * create a card proc file
  550. * called from init.c
  551. */
  552. int snd_info_card_create(struct snd_card *card)
  553. {
  554. char str[8];
  555. struct snd_info_entry *entry;
  556. if (snd_BUG_ON(!card))
  557. return -ENXIO;
  558. sprintf(str, "card%i", card->number);
  559. if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
  560. return -ENOMEM;
  561. entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  562. if (snd_info_register(entry) < 0) {
  563. snd_info_free_entry(entry);
  564. return -ENOMEM;
  565. }
  566. card->proc_root = entry;
  567. return 0;
  568. }
  569. /*
  570. * register the card proc file
  571. * called from init.c
  572. */
  573. int snd_info_card_register(struct snd_card *card)
  574. {
  575. struct proc_dir_entry *p;
  576. if (snd_BUG_ON(!card))
  577. return -ENXIO;
  578. if (!strcmp(card->id, card->proc_root->name))
  579. return 0;
  580. p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
  581. if (p == NULL)
  582. return -ENOMEM;
  583. card->proc_root_link = p;
  584. return 0;
  585. }
  586. /*
  587. * called on card->id change
  588. */
  589. void snd_info_card_id_change(struct snd_card *card)
  590. {
  591. mutex_lock(&info_mutex);
  592. if (card->proc_root_link) {
  593. proc_remove(card->proc_root_link);
  594. card->proc_root_link = NULL;
  595. }
  596. if (strcmp(card->id, card->proc_root->name))
  597. card->proc_root_link = proc_symlink(card->id,
  598. snd_proc_root,
  599. card->proc_root->name);
  600. mutex_unlock(&info_mutex);
  601. }
  602. /*
  603. * de-register the card proc file
  604. * called from init.c
  605. */
  606. void snd_info_card_disconnect(struct snd_card *card)
  607. {
  608. if (!card)
  609. return;
  610. mutex_lock(&info_mutex);
  611. proc_remove(card->proc_root_link);
  612. card->proc_root_link = NULL;
  613. if (card->proc_root)
  614. snd_info_disconnect(card->proc_root);
  615. mutex_unlock(&info_mutex);
  616. }
  617. /*
  618. * release the card proc file resources
  619. * called from init.c
  620. */
  621. int snd_info_card_free(struct snd_card *card)
  622. {
  623. if (!card)
  624. return 0;
  625. snd_info_free_entry(card->proc_root);
  626. card->proc_root = NULL;
  627. return 0;
  628. }
  629. /**
  630. * snd_info_get_line - read one line from the procfs buffer
  631. * @buffer: the procfs buffer
  632. * @line: the buffer to store
  633. * @len: the max. buffer size
  634. *
  635. * Reads one line from the buffer and stores the string.
  636. *
  637. * Return: Zero if successful, or 1 if error or EOF.
  638. */
  639. int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
  640. {
  641. int c = -1;
  642. if (snd_BUG_ON(!buffer || !buffer->buffer))
  643. return 1;
  644. if (len <= 0 || buffer->stop || buffer->error)
  645. return 1;
  646. while (!buffer->stop) {
  647. c = buffer->buffer[buffer->curr++];
  648. if (buffer->curr >= buffer->size)
  649. buffer->stop = 1;
  650. if (c == '\n')
  651. break;
  652. if (len > 1) {
  653. len--;
  654. *line++ = c;
  655. }
  656. }
  657. *line = '\0';
  658. return 0;
  659. }
  660. EXPORT_SYMBOL(snd_info_get_line);
  661. /**
  662. * snd_info_get_str - parse a string token
  663. * @dest: the buffer to store the string token
  664. * @src: the original string
  665. * @len: the max. length of token - 1
  666. *
  667. * Parses the original string and copy a token to the given
  668. * string buffer.
  669. *
  670. * Return: The updated pointer of the original string so that
  671. * it can be used for the next call.
  672. */
  673. const char *snd_info_get_str(char *dest, const char *src, int len)
  674. {
  675. int c;
  676. while (*src == ' ' || *src == '\t')
  677. src++;
  678. if (*src == '"' || *src == '\'') {
  679. c = *src++;
  680. while (--len > 0 && *src && *src != c) {
  681. *dest++ = *src++;
  682. }
  683. if (*src == c)
  684. src++;
  685. } else {
  686. while (--len > 0 && *src && *src != ' ' && *src != '\t') {
  687. *dest++ = *src++;
  688. }
  689. }
  690. *dest = 0;
  691. while (*src == ' ' || *src == '\t')
  692. src++;
  693. return src;
  694. }
  695. EXPORT_SYMBOL(snd_info_get_str);
  696. /**
  697. * snd_info_create_entry - create an info entry
  698. * @name: the proc file name
  699. *
  700. * Creates an info entry with the given file name and initializes as
  701. * the default state.
  702. *
  703. * Usually called from other functions such as
  704. * snd_info_create_card_entry().
  705. *
  706. * Return: The pointer of the new instance, or %NULL on failure.
  707. */
  708. static struct snd_info_entry *snd_info_create_entry(const char *name)
  709. {
  710. struct snd_info_entry *entry;
  711. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  712. if (entry == NULL)
  713. return NULL;
  714. entry->name = kstrdup(name, GFP_KERNEL);
  715. if (entry->name == NULL) {
  716. kfree(entry);
  717. return NULL;
  718. }
  719. entry->mode = S_IFREG | S_IRUGO;
  720. entry->content = SNDRV_INFO_CONTENT_TEXT;
  721. mutex_init(&entry->access);
  722. INIT_LIST_HEAD(&entry->children);
  723. INIT_LIST_HEAD(&entry->list);
  724. return entry;
  725. }
  726. /**
  727. * snd_info_create_module_entry - create an info entry for the given module
  728. * @module: the module pointer
  729. * @name: the file name
  730. * @parent: the parent directory
  731. *
  732. * Creates a new info entry and assigns it to the given module.
  733. *
  734. * Return: The pointer of the new instance, or %NULL on failure.
  735. */
  736. struct snd_info_entry *snd_info_create_module_entry(struct module * module,
  737. const char *name,
  738. struct snd_info_entry *parent)
  739. {
  740. struct snd_info_entry *entry = snd_info_create_entry(name);
  741. if (entry) {
  742. entry->module = module;
  743. entry->parent = parent;
  744. }
  745. return entry;
  746. }
  747. EXPORT_SYMBOL(snd_info_create_module_entry);
  748. /**
  749. * snd_info_create_card_entry - create an info entry for the given card
  750. * @card: the card instance
  751. * @name: the file name
  752. * @parent: the parent directory
  753. *
  754. * Creates a new info entry and assigns it to the given card.
  755. *
  756. * Return: The pointer of the new instance, or %NULL on failure.
  757. */
  758. struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
  759. const char *name,
  760. struct snd_info_entry * parent)
  761. {
  762. struct snd_info_entry *entry = snd_info_create_entry(name);
  763. if (entry) {
  764. entry->module = card->module;
  765. entry->card = card;
  766. entry->parent = parent;
  767. }
  768. return entry;
  769. }
  770. EXPORT_SYMBOL(snd_info_create_card_entry);
  771. static void snd_info_disconnect(struct snd_info_entry *entry)
  772. {
  773. struct list_head *p, *n;
  774. struct proc_dir_entry *root;
  775. list_for_each_safe(p, n, &entry->children) {
  776. snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
  777. }
  778. if (! entry->p)
  779. return;
  780. list_del_init(&entry->list);
  781. root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
  782. snd_BUG_ON(!root);
  783. proc_remove(entry->p);
  784. entry->p = NULL;
  785. }
  786. static int snd_info_dev_free_entry(struct snd_device *device)
  787. {
  788. struct snd_info_entry *entry = device->device_data;
  789. snd_info_free_entry(entry);
  790. return 0;
  791. }
  792. static int snd_info_dev_register_entry(struct snd_device *device)
  793. {
  794. struct snd_info_entry *entry = device->device_data;
  795. return snd_info_register(entry);
  796. }
  797. /**
  798. * snd_card_proc_new - create an info entry for the given card
  799. * @card: the card instance
  800. * @name: the file name
  801. * @entryp: the pointer to store the new info entry
  802. *
  803. * Creates a new info entry and assigns it to the given card.
  804. * Unlike snd_info_create_card_entry(), this function registers the
  805. * info entry as an ALSA device component, so that it can be
  806. * unregistered/released without explicit call.
  807. * Also, you don't have to register this entry via snd_info_register(),
  808. * since this will be registered by snd_card_register() automatically.
  809. *
  810. * The parent is assumed as card->proc_root.
  811. *
  812. * For releasing this entry, use snd_device_free() instead of
  813. * snd_info_free_entry().
  814. *
  815. * Return: Zero if successful, or a negative error code on failure.
  816. */
  817. int snd_card_proc_new(struct snd_card *card, const char *name,
  818. struct snd_info_entry **entryp)
  819. {
  820. static struct snd_device_ops ops = {
  821. .dev_free = snd_info_dev_free_entry,
  822. .dev_register = snd_info_dev_register_entry,
  823. /* disconnect is done via snd_info_card_disconnect() */
  824. };
  825. struct snd_info_entry *entry;
  826. int err;
  827. entry = snd_info_create_card_entry(card, name, card->proc_root);
  828. if (! entry)
  829. return -ENOMEM;
  830. if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
  831. snd_info_free_entry(entry);
  832. return err;
  833. }
  834. if (entryp)
  835. *entryp = entry;
  836. return 0;
  837. }
  838. EXPORT_SYMBOL(snd_card_proc_new);
  839. /**
  840. * snd_info_free_entry - release the info entry
  841. * @entry: the info entry
  842. *
  843. * Releases the info entry. Don't call this after registered.
  844. */
  845. void snd_info_free_entry(struct snd_info_entry * entry)
  846. {
  847. if (entry == NULL)
  848. return;
  849. if (entry->p) {
  850. mutex_lock(&info_mutex);
  851. snd_info_disconnect(entry);
  852. mutex_unlock(&info_mutex);
  853. }
  854. kfree(entry->name);
  855. if (entry->private_free)
  856. entry->private_free(entry);
  857. kfree(entry);
  858. }
  859. EXPORT_SYMBOL(snd_info_free_entry);
  860. /**
  861. * snd_info_register - register the info entry
  862. * @entry: the info entry
  863. *
  864. * Registers the proc info entry.
  865. *
  866. * Return: Zero if successful, or a negative error code on failure.
  867. */
  868. int snd_info_register(struct snd_info_entry * entry)
  869. {
  870. struct proc_dir_entry *root, *p = NULL;
  871. if (snd_BUG_ON(!entry))
  872. return -ENXIO;
  873. root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
  874. mutex_lock(&info_mutex);
  875. if (S_ISDIR(entry->mode)) {
  876. p = proc_mkdir_mode(entry->name, entry->mode, root);
  877. if (!p) {
  878. mutex_unlock(&info_mutex);
  879. return -ENOMEM;
  880. }
  881. } else {
  882. p = proc_create_data(entry->name, entry->mode, root,
  883. &snd_info_entry_operations, entry);
  884. if (!p) {
  885. mutex_unlock(&info_mutex);
  886. return -ENOMEM;
  887. }
  888. proc_set_size(p, entry->size);
  889. }
  890. entry->p = p;
  891. if (entry->parent)
  892. list_add_tail(&entry->list, &entry->parent->children);
  893. mutex_unlock(&info_mutex);
  894. return 0;
  895. }
  896. EXPORT_SYMBOL(snd_info_register);
  897. /*
  898. */
  899. static struct snd_info_entry *snd_info_version_entry;
  900. static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  901. {
  902. snd_iprintf(buffer,
  903. "Advanced Linux Sound Architecture Driver Version k%s.\n",
  904. init_utsname()->release);
  905. }
  906. static int __init snd_info_version_init(void)
  907. {
  908. struct snd_info_entry *entry;
  909. entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
  910. if (entry == NULL)
  911. return -ENOMEM;
  912. entry->c.text.read = snd_info_version_read;
  913. if (snd_info_register(entry) < 0) {
  914. snd_info_free_entry(entry);
  915. return -ENOMEM;
  916. }
  917. snd_info_version_entry = entry;
  918. return 0;
  919. }
  920. static int __exit snd_info_version_done(void)
  921. {
  922. snd_info_free_entry(snd_info_version_entry);
  923. return 0;
  924. }
  925. #endif /* CONFIG_PROC_FS */