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