info.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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);
  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 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 = kmalloc(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. * Returns 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 (len <= 0 || buffer->stop || buffer->error)
  636. return 1;
  637. while (--len > 0) {
  638. c = buffer->buffer[buffer->curr++];
  639. if (c == '\n') {
  640. if (buffer->curr >= buffer->size)
  641. buffer->stop = 1;
  642. break;
  643. }
  644. *line++ = c;
  645. if (buffer->curr >= buffer->size) {
  646. buffer->stop = 1;
  647. break;
  648. }
  649. }
  650. while (c != '\n' && !buffer->stop) {
  651. c = buffer->buffer[buffer->curr++];
  652. if (buffer->curr >= buffer->size)
  653. buffer->stop = 1;
  654. }
  655. *line = '\0';
  656. return 0;
  657. }
  658. EXPORT_SYMBOL(snd_info_get_line);
  659. /**
  660. * snd_info_get_str - parse a string token
  661. * @dest: the buffer to store the string token
  662. * @src: the original string
  663. * @len: the max. length of token - 1
  664. *
  665. * Parses the original string and copy a token to the given
  666. * string buffer.
  667. *
  668. * Returns the updated pointer of the original string so that
  669. * it can be used for the next call.
  670. */
  671. const char *snd_info_get_str(char *dest, const char *src, int len)
  672. {
  673. int c;
  674. while (*src == ' ' || *src == '\t')
  675. src++;
  676. if (*src == '"' || *src == '\'') {
  677. c = *src++;
  678. while (--len > 0 && *src && *src != c) {
  679. *dest++ = *src++;
  680. }
  681. if (*src == c)
  682. src++;
  683. } else {
  684. while (--len > 0 && *src && *src != ' ' && *src != '\t') {
  685. *dest++ = *src++;
  686. }
  687. }
  688. *dest = 0;
  689. while (*src == ' ' || *src == '\t')
  690. src++;
  691. return src;
  692. }
  693. EXPORT_SYMBOL(snd_info_get_str);
  694. /**
  695. * snd_info_create_entry - create an info entry
  696. * @name: the proc file name
  697. *
  698. * Creates an info entry with the given file name and initializes as
  699. * the default state.
  700. *
  701. * Usually called from other functions such as
  702. * snd_info_create_card_entry().
  703. *
  704. * Returns the pointer of the new instance, or NULL on failure.
  705. */
  706. static struct snd_info_entry *snd_info_create_entry(const char *name)
  707. {
  708. struct snd_info_entry *entry;
  709. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  710. if (entry == NULL)
  711. return NULL;
  712. entry->name = kstrdup(name, GFP_KERNEL);
  713. if (entry->name == NULL) {
  714. kfree(entry);
  715. return NULL;
  716. }
  717. entry->mode = S_IFREG | S_IRUGO;
  718. entry->content = SNDRV_INFO_CONTENT_TEXT;
  719. mutex_init(&entry->access);
  720. INIT_LIST_HEAD(&entry->children);
  721. INIT_LIST_HEAD(&entry->list);
  722. return entry;
  723. }
  724. /**
  725. * snd_info_create_module_entry - create an info entry for the given module
  726. * @module: the module pointer
  727. * @name: the file name
  728. * @parent: the parent directory
  729. *
  730. * Creates a new info entry and assigns it to the given module.
  731. *
  732. * Returns the pointer of the new instance, or NULL on failure.
  733. */
  734. struct snd_info_entry *snd_info_create_module_entry(struct module * module,
  735. const char *name,
  736. struct snd_info_entry *parent)
  737. {
  738. struct snd_info_entry *entry = snd_info_create_entry(name);
  739. if (entry) {
  740. entry->module = module;
  741. entry->parent = parent;
  742. }
  743. return entry;
  744. }
  745. EXPORT_SYMBOL(snd_info_create_module_entry);
  746. /**
  747. * snd_info_create_card_entry - create an info entry for the given card
  748. * @card: the card instance
  749. * @name: the file name
  750. * @parent: the parent directory
  751. *
  752. * Creates a new info entry and assigns it to the given card.
  753. *
  754. * Returns the pointer of the new instance, or NULL on failure.
  755. */
  756. struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
  757. const char *name,
  758. struct snd_info_entry * parent)
  759. {
  760. struct snd_info_entry *entry = snd_info_create_entry(name);
  761. if (entry) {
  762. entry->module = card->module;
  763. entry->card = card;
  764. entry->parent = parent;
  765. }
  766. return entry;
  767. }
  768. EXPORT_SYMBOL(snd_info_create_card_entry);
  769. static void snd_info_disconnect(struct snd_info_entry *entry)
  770. {
  771. struct list_head *p, *n;
  772. struct proc_dir_entry *root;
  773. list_for_each_safe(p, n, &entry->children) {
  774. snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
  775. }
  776. if (! entry->p)
  777. return;
  778. list_del_init(&entry->list);
  779. root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
  780. snd_BUG_ON(!root);
  781. proc_remove(entry->p);
  782. entry->p = NULL;
  783. }
  784. static int snd_info_dev_free_entry(struct snd_device *device)
  785. {
  786. struct snd_info_entry *entry = device->device_data;
  787. snd_info_free_entry(entry);
  788. return 0;
  789. }
  790. static int snd_info_dev_register_entry(struct snd_device *device)
  791. {
  792. struct snd_info_entry *entry = device->device_data;
  793. return snd_info_register(entry);
  794. }
  795. /**
  796. * snd_card_proc_new - create an info entry for the given card
  797. * @card: the card instance
  798. * @name: the file name
  799. * @entryp: the pointer to store the new info entry
  800. *
  801. * Creates a new info entry and assigns it to the given card.
  802. * Unlike snd_info_create_card_entry(), this function registers the
  803. * info entry as an ALSA device component, so that it can be
  804. * unregistered/released without explicit call.
  805. * Also, you don't have to register this entry via snd_info_register(),
  806. * since this will be registered by snd_card_register() automatically.
  807. *
  808. * The parent is assumed as card->proc_root.
  809. *
  810. * For releasing this entry, use snd_device_free() instead of
  811. * snd_info_free_entry().
  812. *
  813. * Returns zero if successful, or a negative error code on failure.
  814. */
  815. int snd_card_proc_new(struct snd_card *card, const char *name,
  816. struct snd_info_entry **entryp)
  817. {
  818. static struct snd_device_ops ops = {
  819. .dev_free = snd_info_dev_free_entry,
  820. .dev_register = snd_info_dev_register_entry,
  821. /* disconnect is done via snd_info_card_disconnect() */
  822. };
  823. struct snd_info_entry *entry;
  824. int err;
  825. entry = snd_info_create_card_entry(card, name, card->proc_root);
  826. if (! entry)
  827. return -ENOMEM;
  828. if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
  829. snd_info_free_entry(entry);
  830. return err;
  831. }
  832. if (entryp)
  833. *entryp = entry;
  834. return 0;
  835. }
  836. EXPORT_SYMBOL(snd_card_proc_new);
  837. /**
  838. * snd_info_free_entry - release the info entry
  839. * @entry: the info entry
  840. *
  841. * Releases the info entry. Don't call this after registered.
  842. */
  843. void snd_info_free_entry(struct snd_info_entry * entry)
  844. {
  845. if (entry == NULL)
  846. return;
  847. if (entry->p) {
  848. mutex_lock(&info_mutex);
  849. snd_info_disconnect(entry);
  850. mutex_unlock(&info_mutex);
  851. }
  852. kfree(entry->name);
  853. if (entry->private_free)
  854. entry->private_free(entry);
  855. kfree(entry);
  856. }
  857. EXPORT_SYMBOL(snd_info_free_entry);
  858. /**
  859. * snd_info_register - register the info entry
  860. * @entry: the info entry
  861. *
  862. * Registers the proc info entry.
  863. *
  864. * Returns zero if successful, or a negative error code on failure.
  865. */
  866. int snd_info_register(struct snd_info_entry * entry)
  867. {
  868. struct proc_dir_entry *root, *p = NULL;
  869. if (snd_BUG_ON(!entry))
  870. return -ENXIO;
  871. root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
  872. mutex_lock(&info_mutex);
  873. if (S_ISDIR(entry->mode)) {
  874. p = proc_mkdir_mode(entry->name, entry->mode, root);
  875. if (!p) {
  876. mutex_unlock(&info_mutex);
  877. return -ENOMEM;
  878. }
  879. } else {
  880. p = proc_create_data(entry->name, entry->mode, root,
  881. &snd_info_entry_operations, entry);
  882. if (!p) {
  883. mutex_unlock(&info_mutex);
  884. return -ENOMEM;
  885. }
  886. proc_set_size(p, entry->size);
  887. }
  888. entry->p = p;
  889. if (entry->parent)
  890. list_add_tail(&entry->list, &entry->parent->children);
  891. mutex_unlock(&info_mutex);
  892. return 0;
  893. }
  894. EXPORT_SYMBOL(snd_info_register);
  895. /*
  896. */
  897. static struct snd_info_entry *snd_info_version_entry;
  898. static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  899. {
  900. snd_iprintf(buffer,
  901. "Advanced Linux Sound Architecture Driver Version k%s.\n",
  902. init_utsname()->release);
  903. }
  904. static int __init snd_info_version_init(void)
  905. {
  906. struct snd_info_entry *entry;
  907. entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
  908. if (entry == NULL)
  909. return -ENOMEM;
  910. entry->c.text.read = snd_info_version_read;
  911. if (snd_info_register(entry) < 0) {
  912. snd_info_free_entry(entry);
  913. return -ENOMEM;
  914. }
  915. snd_info_version_entry = entry;
  916. return 0;
  917. }
  918. static int __exit snd_info_version_done(void)
  919. {
  920. snd_info_free_entry(snd_info_version_entry);
  921. return 0;
  922. }
  923. #endif /* CONFIG_PROC_FS */