ivtv-alsa-main.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * ALSA interface to ivtv PCM capture streams
  3. *
  4. * Copyright (C) 2009,2012 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 for the cx18 driver
  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 "ivtv-driver.h"
  34. #include "ivtv-version.h"
  35. #include "ivtv-alsa.h"
  36. #include "ivtv-alsa-mixer.h"
  37. #include "ivtv-alsa-pcm.h"
  38. int ivtv_alsa_debug;
  39. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  40. #define IVTV_DEBUG_ALSA_INFO(fmt, arg...) \
  41. do { \
  42. if (ivtv_alsa_debug & 2) \
  43. pr_info("%s: " fmt, "ivtv-alsa", ## arg); \
  44. } while (0)
  45. module_param_named(debug, ivtv_alsa_debug, int, 0644);
  46. MODULE_PARM_DESC(debug,
  47. "Debug level (bitmask). Default: 0\n"
  48. "\t\t\t 1/0x0001: warning\n"
  49. "\t\t\t 2/0x0002: info\n");
  50. module_param_array(index, int, NULL, 0444);
  51. MODULE_PARM_DESC(index,
  52. "Index value for IVTV ALSA capture interface(s).\n");
  53. MODULE_AUTHOR("Andy Walls");
  54. MODULE_DESCRIPTION("CX23415/CX23416 ALSA Interface");
  55. MODULE_SUPPORTED_DEVICE("CX23415/CX23416 MPEG2 encoder");
  56. MODULE_LICENSE("GPL");
  57. MODULE_VERSION(IVTV_VERSION);
  58. static inline
  59. struct snd_ivtv_card *to_snd_ivtv_card(struct v4l2_device *v4l2_dev)
  60. {
  61. return to_ivtv(v4l2_dev)->alsa;
  62. }
  63. static inline
  64. struct snd_ivtv_card *p_to_snd_ivtv_card(struct v4l2_device **v4l2_dev)
  65. {
  66. return container_of(v4l2_dev, struct snd_ivtv_card, v4l2_dev);
  67. }
  68. static void snd_ivtv_card_free(struct snd_ivtv_card *itvsc)
  69. {
  70. if (itvsc == NULL)
  71. return;
  72. if (itvsc->v4l2_dev != NULL)
  73. to_ivtv(itvsc->v4l2_dev)->alsa = NULL;
  74. /* FIXME - take any other stopping actions needed */
  75. kfree(itvsc);
  76. }
  77. static void snd_ivtv_card_private_free(struct snd_card *sc)
  78. {
  79. if (sc == NULL)
  80. return;
  81. snd_ivtv_card_free(sc->private_data);
  82. sc->private_data = NULL;
  83. sc->private_free = NULL;
  84. }
  85. static int snd_ivtv_card_create(struct v4l2_device *v4l2_dev,
  86. struct snd_card *sc,
  87. struct snd_ivtv_card **itvsc)
  88. {
  89. *itvsc = kzalloc(sizeof(struct snd_ivtv_card), GFP_KERNEL);
  90. if (*itvsc == NULL)
  91. return -ENOMEM;
  92. (*itvsc)->v4l2_dev = v4l2_dev;
  93. (*itvsc)->sc = sc;
  94. sc->private_data = *itvsc;
  95. sc->private_free = snd_ivtv_card_private_free;
  96. return 0;
  97. }
  98. static int snd_ivtv_card_set_names(struct snd_ivtv_card *itvsc)
  99. {
  100. struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
  101. struct snd_card *sc = itvsc->sc;
  102. /* sc->driver is used by alsa-lib's configurator: simple, unique */
  103. strlcpy(sc->driver, "CX2341[56]", sizeof(sc->driver));
  104. /* sc->shortname is a symlink in /proc/asound: IVTV-M -> cardN */
  105. snprintf(sc->shortname, sizeof(sc->shortname), "IVTV-%d",
  106. itv->instance);
  107. /* sc->longname is read from /proc/asound/cards */
  108. snprintf(sc->longname, sizeof(sc->longname),
  109. "CX2341[56] #%d %s TV/FM Radio/Line-In Capture",
  110. itv->instance, itv->card_name);
  111. return 0;
  112. }
  113. static int snd_ivtv_init(struct v4l2_device *v4l2_dev)
  114. {
  115. struct ivtv *itv = to_ivtv(v4l2_dev);
  116. struct snd_card *sc = NULL;
  117. struct snd_ivtv_card *itvsc;
  118. int ret, idx;
  119. /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
  120. /* (1) Check and increment the device index */
  121. /* This is a no-op for us. We'll use the itv->instance */
  122. /* (2) Create a card instance */
  123. /* use first available id if not specified otherwise*/
  124. idx = index[itv->instance] == -1 ? SNDRV_DEFAULT_IDX1 : index[itv->instance];
  125. ret = snd_card_new(&itv->pdev->dev,
  126. idx,
  127. SNDRV_DEFAULT_STR1, /* xid from end of shortname*/
  128. THIS_MODULE, 0, &sc);
  129. if (ret) {
  130. IVTV_ALSA_ERR("%s: snd_card_new() failed with err %d\n",
  131. __func__, ret);
  132. goto err_exit;
  133. }
  134. /* (3) Create a main component */
  135. ret = snd_ivtv_card_create(v4l2_dev, sc, &itvsc);
  136. if (ret) {
  137. IVTV_ALSA_ERR("%s: snd_ivtv_card_create() failed with err %d\n",
  138. __func__, ret);
  139. goto err_exit_free;
  140. }
  141. /* (4) Set the driver ID and name strings */
  142. snd_ivtv_card_set_names(itvsc);
  143. /* (5) Create other components: mixer, PCM, & proc files */
  144. #if 0
  145. ret = snd_ivtv_mixer_create(itvsc);
  146. if (ret) {
  147. IVTV_ALSA_WARN("%s: snd_ivtv_mixer_create() failed with err %d:"
  148. " proceeding anyway\n", __func__, ret);
  149. }
  150. #endif
  151. ret = snd_ivtv_pcm_create(itvsc);
  152. if (ret) {
  153. IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
  154. __func__, ret);
  155. goto err_exit_free;
  156. }
  157. /* FIXME - proc files */
  158. /* (7) Set the driver data and return 0 */
  159. /* We do this out of normal order for PCI drivers to avoid races */
  160. itv->alsa = itvsc;
  161. /* (6) Register the card instance */
  162. ret = snd_card_register(sc);
  163. if (ret) {
  164. itv->alsa = NULL;
  165. IVTV_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
  166. __func__, ret);
  167. goto err_exit_free;
  168. }
  169. IVTV_ALSA_INFO("%s: Instance %d registered as ALSA card %d\n",
  170. __func__, itv->instance, sc->number);
  171. return 0;
  172. err_exit_free:
  173. if (sc != NULL)
  174. snd_card_free(sc);
  175. kfree(itvsc);
  176. err_exit:
  177. return ret;
  178. }
  179. static int ivtv_alsa_load(struct ivtv *itv)
  180. {
  181. struct v4l2_device *v4l2_dev = &itv->v4l2_dev;
  182. struct ivtv_stream *s;
  183. if (v4l2_dev == NULL) {
  184. pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
  185. __func__);
  186. return 0;
  187. }
  188. itv = to_ivtv(v4l2_dev);
  189. if (itv == NULL) {
  190. pr_err("ivtv-alsa itv is NULL\n");
  191. return 0;
  192. }
  193. s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
  194. if (s->vdev.v4l2_dev == NULL) {
  195. IVTV_DEBUG_ALSA_INFO("%s: PCM stream for card is disabled - "
  196. "skipping\n", __func__);
  197. return 0;
  198. }
  199. if (itv->alsa != NULL) {
  200. IVTV_ALSA_ERR("%s: struct snd_ivtv_card * already exists\n",
  201. __func__);
  202. return 0;
  203. }
  204. if (snd_ivtv_init(v4l2_dev)) {
  205. IVTV_ALSA_ERR("%s: failed to create struct snd_ivtv_card\n",
  206. __func__);
  207. } else {
  208. IVTV_DEBUG_ALSA_INFO("%s: created ivtv ALSA interface instance "
  209. "\n", __func__);
  210. }
  211. return 0;
  212. }
  213. static int __init ivtv_alsa_init(void)
  214. {
  215. pr_info("ivtv-alsa: module loading...\n");
  216. ivtv_ext_init = &ivtv_alsa_load;
  217. return 0;
  218. }
  219. static void __exit snd_ivtv_exit(struct snd_ivtv_card *itvsc)
  220. {
  221. struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
  222. /* FIXME - pointer checks & shutdown itvsc */
  223. snd_card_free(itvsc->sc);
  224. itv->alsa = NULL;
  225. }
  226. static int __exit ivtv_alsa_exit_callback(struct device *dev, void *data)
  227. {
  228. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
  229. struct snd_ivtv_card *itvsc;
  230. if (v4l2_dev == NULL) {
  231. pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
  232. __func__);
  233. return 0;
  234. }
  235. itvsc = to_snd_ivtv_card(v4l2_dev);
  236. if (itvsc == NULL) {
  237. IVTV_ALSA_WARN("%s: struct snd_ivtv_card * is NULL\n",
  238. __func__);
  239. return 0;
  240. }
  241. snd_ivtv_exit(itvsc);
  242. return 0;
  243. }
  244. static void __exit ivtv_alsa_exit(void)
  245. {
  246. struct device_driver *drv;
  247. int ret;
  248. pr_info("ivtv-alsa: module unloading...\n");
  249. drv = driver_find("ivtv", &pci_bus_type);
  250. ret = driver_for_each_device(drv, NULL, NULL, ivtv_alsa_exit_callback);
  251. (void)ret; /* suppress compiler warning */
  252. ivtv_ext_init = NULL;
  253. pr_info("ivtv-alsa: module unload complete\n");
  254. }
  255. module_init(ivtv_alsa_init);
  256. module_exit(ivtv_alsa_exit);