info.c 23 KB

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