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