info.c 23 KB

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