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