info.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. #ifdef CONFIG_PROC_FS
  38. int snd_info_check_reserved_words(const char *str)
  39. {
  40. static char *reserved[] =
  41. {
  42. "version",
  43. "meminfo",
  44. "memdebug",
  45. "detect",
  46. "devices",
  47. "oss",
  48. "cards",
  49. "timers",
  50. "synth",
  51. "pcm",
  52. "seq",
  53. NULL
  54. };
  55. char **xstr = reserved;
  56. while (*xstr) {
  57. if (!strcmp(*xstr, str))
  58. return 0;
  59. xstr++;
  60. }
  61. if (!strncmp(str, "card", 4))
  62. return 0;
  63. return 1;
  64. }
  65. static DECLARE_MUTEX(info_mutex);
  66. struct snd_info_private_data {
  67. struct snd_info_buffer *rbuffer;
  68. struct snd_info_buffer *wbuffer;
  69. struct snd_info_entry *entry;
  70. void *file_private_data;
  71. };
  72. static int snd_info_version_init(void);
  73. static int snd_info_version_done(void);
  74. /**
  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(struct snd_info_buffer *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. struct snd_info_entry *snd_seq_root = NULL;
  105. #ifdef CONFIG_SND_OSSEMUL
  106. struct snd_info_entry *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. struct snd_info_private_data *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. struct snd_info_private_data *data;
  161. struct snd_info_entry *entry;
  162. struct snd_info_buffer *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. struct snd_info_private_data *data;
  200. struct snd_info_entry *entry;
  201. struct snd_info_buffer *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. struct snd_info_entry *entry;
  240. struct snd_info_private_data *data;
  241. struct snd_info_buffer *buffer;
  242. struct proc_dir_entry *p;
  243. int mode, err;
  244. down(&info_mutex);
  245. p = PDE(inode);
  246. entry = p == NULL ? NULL : (struct snd_info_entry *)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 = kzalloc(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 = kzalloc(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 = kzalloc(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. struct snd_info_entry *entry;
  359. struct snd_info_private_data *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. struct snd_info_private_data *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. struct snd_info_private_data *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. struct snd_info_private_data *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. struct snd_info_entry *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. struct snd_info_entry *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_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_info_version_done();
  542. if (snd_proc_root) {
  543. #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
  544. snd_info_unregister(snd_seq_root);
  545. #endif
  546. #ifdef CONFIG_SND_OSSEMUL
  547. snd_info_unregister(snd_oss_root);
  548. #endif
  549. snd_remove_proc_entry(&proc_root, snd_proc_root);
  550. }
  551. return 0;
  552. }
  553. /*
  554. */
  555. /*
  556. * create a card proc file
  557. * called from init.c
  558. */
  559. int snd_info_card_create(struct snd_card *card)
  560. {
  561. char str[8];
  562. struct snd_info_entry *entry;
  563. snd_assert(card != NULL, return -ENXIO);
  564. sprintf(str, "card%i", card->number);
  565. if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
  566. return -ENOMEM;
  567. entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  568. if (snd_info_register(entry) < 0) {
  569. snd_info_free_entry(entry);
  570. return -ENOMEM;
  571. }
  572. card->proc_root = entry;
  573. return 0;
  574. }
  575. /*
  576. * register the card proc file
  577. * called from init.c
  578. */
  579. int snd_info_card_register(struct snd_card *card)
  580. {
  581. struct proc_dir_entry *p;
  582. snd_assert(card != NULL, return -ENXIO);
  583. if (!strcmp(card->id, card->proc_root->name))
  584. return 0;
  585. p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
  586. if (p == NULL)
  587. return -ENOMEM;
  588. card->proc_root_link = p;
  589. return 0;
  590. }
  591. /*
  592. * de-register the card proc file
  593. * called from init.c
  594. */
  595. int snd_info_card_free(struct snd_card *card)
  596. {
  597. snd_assert(card != NULL, return -ENXIO);
  598. if (card->proc_root_link) {
  599. snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
  600. card->proc_root_link = NULL;
  601. }
  602. if (card->proc_root) {
  603. snd_info_unregister(card->proc_root);
  604. card->proc_root = NULL;
  605. }
  606. return 0;
  607. }
  608. /**
  609. * snd_info_get_line - read one line from the procfs buffer
  610. * @buffer: the procfs buffer
  611. * @line: the buffer to store
  612. * @len: the max. buffer size - 1
  613. *
  614. * Reads one line from the buffer and stores the string.
  615. *
  616. * Returns zero if successful, or 1 if error or EOF.
  617. */
  618. int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
  619. {
  620. int c = -1;
  621. if (len <= 0 || buffer->stop || buffer->error)
  622. return 1;
  623. while (--len > 0) {
  624. c = *buffer->curr++;
  625. if (c == '\n') {
  626. if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
  627. buffer->stop = 1;
  628. }
  629. break;
  630. }
  631. *line++ = c;
  632. if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
  633. buffer->stop = 1;
  634. break;
  635. }
  636. }
  637. while (c != '\n' && !buffer->stop) {
  638. c = *buffer->curr++;
  639. if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
  640. buffer->stop = 1;
  641. }
  642. }
  643. *line = '\0';
  644. return 0;
  645. }
  646. /**
  647. * snd_info_get_str - parse a string token
  648. * @dest: the buffer to store the string token
  649. * @src: the original string
  650. * @len: the max. length of token - 1
  651. *
  652. * Parses the original string and copy a token to the given
  653. * string buffer.
  654. *
  655. * Returns the updated pointer of the original string so that
  656. * it can be used for the next call.
  657. */
  658. char *snd_info_get_str(char *dest, char *src, int len)
  659. {
  660. int c;
  661. while (*src == ' ' || *src == '\t')
  662. src++;
  663. if (*src == '"' || *src == '\'') {
  664. c = *src++;
  665. while (--len > 0 && *src && *src != c) {
  666. *dest++ = *src++;
  667. }
  668. if (*src == c)
  669. src++;
  670. } else {
  671. while (--len > 0 && *src && *src != ' ' && *src != '\t') {
  672. *dest++ = *src++;
  673. }
  674. }
  675. *dest = 0;
  676. while (*src == ' ' || *src == '\t')
  677. src++;
  678. return src;
  679. }
  680. /**
  681. * snd_info_create_entry - create an info entry
  682. * @name: the proc file name
  683. *
  684. * Creates an info entry with the given file name and initializes as
  685. * the default state.
  686. *
  687. * Usually called from other functions such as
  688. * snd_info_create_card_entry().
  689. *
  690. * Returns the pointer of the new instance, or NULL on failure.
  691. */
  692. static struct snd_info_entry *snd_info_create_entry(const char *name)
  693. {
  694. struct snd_info_entry *entry;
  695. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  696. if (entry == NULL)
  697. return NULL;
  698. entry->name = kstrdup(name, GFP_KERNEL);
  699. if (entry->name == NULL) {
  700. kfree(entry);
  701. return NULL;
  702. }
  703. entry->mode = S_IFREG | S_IRUGO;
  704. entry->content = SNDRV_INFO_CONTENT_TEXT;
  705. init_MUTEX(&entry->access);
  706. return entry;
  707. }
  708. /**
  709. * snd_info_create_module_entry - create an info entry for the given module
  710. * @module: the module pointer
  711. * @name: the file name
  712. * @parent: the parent directory
  713. *
  714. * Creates a new info entry and assigns it to the given module.
  715. *
  716. * Returns the pointer of the new instance, or NULL on failure.
  717. */
  718. struct snd_info_entry *snd_info_create_module_entry(struct module * module,
  719. const char *name,
  720. struct snd_info_entry *parent)
  721. {
  722. struct snd_info_entry *entry = snd_info_create_entry(name);
  723. if (entry) {
  724. entry->module = module;
  725. entry->parent = parent;
  726. }
  727. return entry;
  728. }
  729. /**
  730. * snd_info_create_card_entry - create an info entry for the given card
  731. * @card: the card instance
  732. * @name: the file name
  733. * @parent: the parent directory
  734. *
  735. * Creates a new info entry and assigns it to the given card.
  736. *
  737. * Returns the pointer of the new instance, or NULL on failure.
  738. */
  739. struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
  740. const char *name,
  741. struct snd_info_entry * parent)
  742. {
  743. struct snd_info_entry *entry = snd_info_create_entry(name);
  744. if (entry) {
  745. entry->module = card->module;
  746. entry->card = card;
  747. entry->parent = parent;
  748. }
  749. return entry;
  750. }
  751. static int snd_info_dev_free_entry(struct snd_device *device)
  752. {
  753. struct snd_info_entry *entry = device->device_data;
  754. snd_info_free_entry(entry);
  755. return 0;
  756. }
  757. static int snd_info_dev_register_entry(struct snd_device *device)
  758. {
  759. struct snd_info_entry *entry = device->device_data;
  760. return snd_info_register(entry);
  761. }
  762. static int snd_info_dev_disconnect_entry(struct snd_device *device)
  763. {
  764. struct snd_info_entry *entry = device->device_data;
  765. entry->disconnected = 1;
  766. return 0;
  767. }
  768. static int snd_info_dev_unregister_entry(struct snd_device *device)
  769. {
  770. struct snd_info_entry *entry = device->device_data;
  771. return snd_info_unregister(entry);
  772. }
  773. /**
  774. * snd_card_proc_new - create an info entry for the given card
  775. * @card: the card instance
  776. * @name: the file name
  777. * @entryp: the pointer to store the new info entry
  778. *
  779. * Creates a new info entry and assigns it to the given card.
  780. * Unlike snd_info_create_card_entry(), this function registers the
  781. * info entry as an ALSA device component, so that it can be
  782. * unregistered/released without explicit call.
  783. * Also, you don't have to register this entry via snd_info_register(),
  784. * since this will be registered by snd_card_register() automatically.
  785. *
  786. * The parent is assumed as card->proc_root.
  787. *
  788. * For releasing this entry, use snd_device_free() instead of
  789. * snd_info_free_entry().
  790. *
  791. * Returns zero if successful, or a negative error code on failure.
  792. */
  793. int snd_card_proc_new(struct snd_card *card, const char *name,
  794. struct snd_info_entry **entryp)
  795. {
  796. static struct snd_device_ops ops = {
  797. .dev_free = snd_info_dev_free_entry,
  798. .dev_register = snd_info_dev_register_entry,
  799. .dev_disconnect = snd_info_dev_disconnect_entry,
  800. .dev_unregister = snd_info_dev_unregister_entry
  801. };
  802. struct snd_info_entry *entry;
  803. int err;
  804. entry = snd_info_create_card_entry(card, name, card->proc_root);
  805. if (! entry)
  806. return -ENOMEM;
  807. if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
  808. snd_info_free_entry(entry);
  809. return err;
  810. }
  811. if (entryp)
  812. *entryp = entry;
  813. return 0;
  814. }
  815. /**
  816. * snd_info_free_entry - release the info entry
  817. * @entry: the info entry
  818. *
  819. * Releases the info entry. Don't call this after registered.
  820. */
  821. void snd_info_free_entry(struct snd_info_entry * entry)
  822. {
  823. if (entry == NULL)
  824. return;
  825. kfree(entry->name);
  826. if (entry->private_free)
  827. entry->private_free(entry);
  828. kfree(entry);
  829. }
  830. /**
  831. * snd_info_register - register the info entry
  832. * @entry: the info entry
  833. *
  834. * Registers the proc info entry.
  835. *
  836. * Returns zero if successful, or a negative error code on failure.
  837. */
  838. int snd_info_register(struct snd_info_entry * entry)
  839. {
  840. struct proc_dir_entry *root, *p = NULL;
  841. snd_assert(entry != NULL, return -ENXIO);
  842. root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
  843. down(&info_mutex);
  844. p = snd_create_proc_entry(entry->name, entry->mode, root);
  845. if (!p) {
  846. up(&info_mutex);
  847. return -ENOMEM;
  848. }
  849. p->owner = entry->module;
  850. if (!S_ISDIR(entry->mode))
  851. p->proc_fops = &snd_info_entry_operations;
  852. p->size = entry->size;
  853. p->data = entry;
  854. entry->p = p;
  855. up(&info_mutex);
  856. return 0;
  857. }
  858. /**
  859. * snd_info_unregister - de-register the info entry
  860. * @entry: the info entry
  861. *
  862. * De-registers the info entry and releases the instance.
  863. *
  864. * Returns zero if successful, or a negative error code on failure.
  865. */
  866. int snd_info_unregister(struct snd_info_entry * entry)
  867. {
  868. struct proc_dir_entry *root;
  869. if (! entry)
  870. return 0;
  871. snd_assert(entry->p != NULL, return -ENXIO);
  872. root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
  873. snd_assert(root, return -ENXIO);
  874. down(&info_mutex);
  875. snd_remove_proc_entry(root, entry->p);
  876. up(&info_mutex);
  877. snd_info_free_entry(entry);
  878. return 0;
  879. }
  880. /*
  881. */
  882. static struct snd_info_entry *snd_info_version_entry = NULL;
  883. static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  884. {
  885. snd_iprintf(buffer,
  886. "Advanced Linux Sound Architecture Driver Version "
  887. CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
  888. );
  889. }
  890. static int __init snd_info_version_init(void)
  891. {
  892. struct snd_info_entry *entry;
  893. entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
  894. if (entry == NULL)
  895. return -ENOMEM;
  896. entry->c.text.read_size = 256;
  897. entry->c.text.read = snd_info_version_read;
  898. if (snd_info_register(entry) < 0) {
  899. snd_info_free_entry(entry);
  900. return -ENOMEM;
  901. }
  902. snd_info_version_entry = entry;
  903. return 0;
  904. }
  905. static int __exit snd_info_version_done(void)
  906. {
  907. if (snd_info_version_entry)
  908. snd_info_unregister(snd_info_version_entry);
  909. return 0;
  910. }
  911. #endif /* CONFIG_PROC_FS */