info.c 23 KB

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