info.c 23 KB

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