info.c 23 KB

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