info.c 23 KB

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