cx18-alsa-main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * ALSA interface to cx18 PCM capture streams
  3. *
  4. * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
  5. * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
  6. *
  7. * Portions of this work were sponsored by ONELAN Limited.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/slab.h>
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/device.h>
  24. #include <linux/spinlock.h>
  25. #include <media/v4l2-device.h>
  26. #include <sound/core.h>
  27. #include <sound/initval.h>
  28. #include "cx18-driver.h"
  29. #include "cx18-version.h"
  30. #include "cx18-alsa.h"
  31. #include "cx18-alsa-pcm.h"
  32. int cx18_alsa_debug;
  33. #define CX18_DEBUG_ALSA_INFO(fmt, arg...) \
  34. do { \
  35. if (cx18_alsa_debug & 2) \
  36. printk(KERN_INFO "%s: " fmt, "cx18-alsa", ## arg); \
  37. } while (0);
  38. module_param_named(debug, cx18_alsa_debug, int, 0644);
  39. MODULE_PARM_DESC(debug,
  40. "Debug level (bitmask). Default: 0\n"
  41. "\t\t\t 1/0x0001: warning\n"
  42. "\t\t\t 2/0x0002: info\n");
  43. MODULE_AUTHOR("Andy Walls");
  44. MODULE_DESCRIPTION("CX23418 ALSA Interface");
  45. MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
  46. MODULE_LICENSE("GPL");
  47. MODULE_VERSION(CX18_VERSION);
  48. static inline
  49. struct snd_cx18_card *to_snd_cx18_card(struct v4l2_device *v4l2_dev)
  50. {
  51. return to_cx18(v4l2_dev)->alsa;
  52. }
  53. static inline
  54. struct snd_cx18_card *p_to_snd_cx18_card(struct v4l2_device **v4l2_dev)
  55. {
  56. return container_of(v4l2_dev, struct snd_cx18_card, v4l2_dev);
  57. }
  58. static void snd_cx18_card_free(struct snd_cx18_card *cxsc)
  59. {
  60. if (cxsc == NULL)
  61. return;
  62. if (cxsc->v4l2_dev != NULL)
  63. to_cx18(cxsc->v4l2_dev)->alsa = NULL;
  64. /* FIXME - take any other stopping actions needed */
  65. kfree(cxsc);
  66. }
  67. static void snd_cx18_card_private_free(struct snd_card *sc)
  68. {
  69. if (sc == NULL)
  70. return;
  71. snd_cx18_card_free(sc->private_data);
  72. sc->private_data = NULL;
  73. sc->private_free = NULL;
  74. }
  75. static int snd_cx18_card_create(struct v4l2_device *v4l2_dev,
  76. struct snd_card *sc,
  77. struct snd_cx18_card **cxsc)
  78. {
  79. *cxsc = kzalloc(sizeof(struct snd_cx18_card), GFP_KERNEL);
  80. if (*cxsc == NULL)
  81. return -ENOMEM;
  82. (*cxsc)->v4l2_dev = v4l2_dev;
  83. (*cxsc)->sc = sc;
  84. sc->private_data = *cxsc;
  85. sc->private_free = snd_cx18_card_private_free;
  86. return 0;
  87. }
  88. static int snd_cx18_card_set_names(struct snd_cx18_card *cxsc)
  89. {
  90. struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
  91. struct snd_card *sc = cxsc->sc;
  92. /* sc->driver is used by alsa-lib's configurator: simple, unique */
  93. strlcpy(sc->driver, "CX23418", sizeof(sc->driver));
  94. /* sc->shortname is a symlink in /proc/asound: CX18-M -> cardN */
  95. snprintf(sc->shortname, sizeof(sc->shortname), "CX18-%d",
  96. cx->instance);
  97. /* sc->longname is read from /proc/asound/cards */
  98. snprintf(sc->longname, sizeof(sc->longname),
  99. "CX23418 #%d %s TV/FM Radio/Line-In Capture",
  100. cx->instance, cx->card_name);
  101. return 0;
  102. }
  103. static int snd_cx18_init(struct v4l2_device *v4l2_dev)
  104. {
  105. struct cx18 *cx = to_cx18(v4l2_dev);
  106. struct snd_card *sc = NULL;
  107. struct snd_cx18_card *cxsc;
  108. int ret;
  109. /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
  110. /* (1) Check and increment the device index */
  111. /* This is a no-op for us. We'll use the cx->instance */
  112. /* (2) Create a card instance */
  113. ret = snd_card_new(&cx->pci_dev->dev,
  114. SNDRV_DEFAULT_IDX1, /* use first available id */
  115. SNDRV_DEFAULT_STR1, /* xid from end of shortname*/
  116. THIS_MODULE, 0, &sc);
  117. if (ret) {
  118. CX18_ALSA_ERR("%s: snd_card_new() failed with err %d\n",
  119. __func__, ret);
  120. goto err_exit;
  121. }
  122. /* (3) Create a main component */
  123. ret = snd_cx18_card_create(v4l2_dev, sc, &cxsc);
  124. if (ret) {
  125. CX18_ALSA_ERR("%s: snd_cx18_card_create() failed with err %d\n",
  126. __func__, ret);
  127. goto err_exit_free;
  128. }
  129. /* (4) Set the driver ID and name strings */
  130. snd_cx18_card_set_names(cxsc);
  131. ret = snd_cx18_pcm_create(cxsc);
  132. if (ret) {
  133. CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n",
  134. __func__, ret);
  135. goto err_exit_free;
  136. }
  137. /* FIXME - proc files */
  138. /* (7) Set the driver data and return 0 */
  139. /* We do this out of normal order for PCI drivers to avoid races */
  140. cx->alsa = cxsc;
  141. /* (6) Register the card instance */
  142. ret = snd_card_register(sc);
  143. if (ret) {
  144. cx->alsa = NULL;
  145. CX18_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
  146. __func__, ret);
  147. goto err_exit_free;
  148. }
  149. return 0;
  150. err_exit_free:
  151. if (sc != NULL)
  152. snd_card_free(sc);
  153. kfree(cxsc);
  154. err_exit:
  155. return ret;
  156. }
  157. static int cx18_alsa_load(struct cx18 *cx)
  158. {
  159. struct v4l2_device *v4l2_dev = &cx->v4l2_dev;
  160. struct cx18_stream *s;
  161. if (v4l2_dev == NULL) {
  162. printk(KERN_ERR "cx18-alsa: %s: struct v4l2_device * is NULL\n",
  163. __func__);
  164. return 0;
  165. }
  166. cx = to_cx18(v4l2_dev);
  167. if (cx == NULL) {
  168. printk(KERN_ERR "cx18-alsa cx is NULL\n");
  169. return 0;
  170. }
  171. s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
  172. if (s->video_dev.v4l2_dev == NULL) {
  173. CX18_DEBUG_ALSA_INFO("%s: PCM stream for card is disabled - skipping\n",
  174. __func__);
  175. return 0;
  176. }
  177. if (cx->alsa != NULL) {
  178. CX18_ALSA_ERR("%s: struct snd_cx18_card * already exists\n",
  179. __func__);
  180. return 0;
  181. }
  182. if (snd_cx18_init(v4l2_dev)) {
  183. CX18_ALSA_ERR("%s: failed to create struct snd_cx18_card\n",
  184. __func__);
  185. } else {
  186. CX18_DEBUG_ALSA_INFO("%s: created cx18 ALSA interface instance\n",
  187. __func__);
  188. }
  189. return 0;
  190. }
  191. static int __init cx18_alsa_init(void)
  192. {
  193. printk(KERN_INFO "cx18-alsa: module loading...\n");
  194. cx18_ext_init = &cx18_alsa_load;
  195. return 0;
  196. }
  197. static void __exit snd_cx18_exit(struct snd_cx18_card *cxsc)
  198. {
  199. struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
  200. /* FIXME - pointer checks & shutdown cxsc */
  201. snd_card_free(cxsc->sc);
  202. cx->alsa = NULL;
  203. }
  204. static int __exit cx18_alsa_exit_callback(struct device *dev, void *data)
  205. {
  206. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
  207. struct snd_cx18_card *cxsc;
  208. if (v4l2_dev == NULL) {
  209. printk(KERN_ERR "cx18-alsa: %s: struct v4l2_device * is NULL\n",
  210. __func__);
  211. return 0;
  212. }
  213. cxsc = to_snd_cx18_card(v4l2_dev);
  214. if (cxsc == NULL) {
  215. CX18_ALSA_WARN("%s: struct snd_cx18_card * is NULL\n",
  216. __func__);
  217. return 0;
  218. }
  219. snd_cx18_exit(cxsc);
  220. return 0;
  221. }
  222. static void __exit cx18_alsa_exit(void)
  223. {
  224. struct device_driver *drv;
  225. int ret;
  226. printk(KERN_INFO "cx18-alsa: module unloading...\n");
  227. drv = driver_find("cx18", &pci_bus_type);
  228. ret = driver_for_each_device(drv, NULL, NULL, cx18_alsa_exit_callback);
  229. (void)ret; /* suppress compiler warning */
  230. cx18_ext_init = NULL;
  231. printk(KERN_INFO "cx18-alsa: module unload complete\n");
  232. }
  233. module_init(cx18_alsa_init);
  234. module_exit(cx18_alsa_exit);