cx18-alsa-main.c 6.9 KB

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