info.c 23 KB

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