info.c 23 KB

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