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