ivtv-alsa-main.c 7.2 KB

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