info.c 23 KB

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