info.c 23 KB

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