info.c 23 KB

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