info.c 23 KB

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