info.c 23 KB

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