cx18-alsa-main.c 7.2 KB

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