info.c 23 KB

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