info.c 23 KB

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