info.c 23 KB

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