ivtv-alsa-main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. #define IVTV_DEBUG_ALSA_INFO(fmt, arg...) \
  40. do { \
  41. if (ivtv_alsa_debug & 2) \
  42. pr_info("%s: " fmt, "ivtv-alsa", ## arg); \
  43. } while (0)
  44. module_param_named(debug, ivtv_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("CX23415/CX23416 ALSA Interface");
  51. MODULE_SUPPORTED_DEVICE("CX23415/CX23416 MPEG2 encoder");
  52. MODULE_LICENSE("GPL");
  53. MODULE_VERSION(IVTV_VERSION);
  54. static inline
  55. struct snd_ivtv_card *to_snd_ivtv_card(struct v4l2_device *v4l2_dev)
  56. {
  57. return to_ivtv(v4l2_dev)->alsa;
  58. }
  59. static inline
  60. struct snd_ivtv_card *p_to_snd_ivtv_card(struct v4l2_device **v4l2_dev)
  61. {
  62. return container_of(v4l2_dev, struct snd_ivtv_card, v4l2_dev);
  63. }
  64. static void snd_ivtv_card_free(struct snd_ivtv_card *itvsc)
  65. {
  66. if (itvsc == NULL)
  67. return;
  68. if (itvsc->v4l2_dev != NULL)
  69. to_ivtv(itvsc->v4l2_dev)->alsa = NULL;
  70. /* FIXME - take any other stopping actions needed */
  71. kfree(itvsc);
  72. }
  73. static void snd_ivtv_card_private_free(struct snd_card *sc)
  74. {
  75. if (sc == NULL)
  76. return;
  77. snd_ivtv_card_free(sc->private_data);
  78. sc->private_data = NULL;
  79. sc->private_free = NULL;
  80. }
  81. static int snd_ivtv_card_create(struct v4l2_device *v4l2_dev,
  82. struct snd_card *sc,
  83. struct snd_ivtv_card **itvsc)
  84. {
  85. *itvsc = kzalloc(sizeof(struct snd_ivtv_card), GFP_KERNEL);
  86. if (*itvsc == NULL)
  87. return -ENOMEM;
  88. (*itvsc)->v4l2_dev = v4l2_dev;
  89. (*itvsc)->sc = sc;
  90. sc->private_data = *itvsc;
  91. sc->private_free = snd_ivtv_card_private_free;
  92. return 0;
  93. }
  94. static int snd_ivtv_card_set_names(struct snd_ivtv_card *itvsc)
  95. {
  96. struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
  97. struct snd_card *sc = itvsc->sc;
  98. /* sc->driver is used by alsa-lib's configurator: simple, unique */
  99. strlcpy(sc->driver, "CX2341[56]", sizeof(sc->driver));
  100. /* sc->shortname is a symlink in /proc/asound: IVTV-M -> cardN */
  101. snprintf(sc->shortname, sizeof(sc->shortname), "IVTV-%d",
  102. itv->instance);
  103. /* sc->longname is read from /proc/asound/cards */
  104. snprintf(sc->longname, sizeof(sc->longname),
  105. "CX2341[56] #%d %s TV/FM Radio/Line-In Capture",
  106. itv->instance, itv->card_name);
  107. return 0;
  108. }
  109. static int snd_ivtv_init(struct v4l2_device *v4l2_dev)
  110. {
  111. struct ivtv *itv = to_ivtv(v4l2_dev);
  112. struct snd_card *sc = NULL;
  113. struct snd_ivtv_card *itvsc;
  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 itv->instance */
  118. /* (2) Create a card instance */
  119. ret = snd_card_new(&itv->pdev->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. IVTV_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_ivtv_card_create(v4l2_dev, sc, &itvsc);
  130. if (ret) {
  131. IVTV_ALSA_ERR("%s: snd_ivtv_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_ivtv_card_set_names(itvsc);
  137. /* (5) Create other components: mixer, PCM, & proc files */
  138. #if 0
  139. ret = snd_ivtv_mixer_create(itvsc);
  140. if (ret) {
  141. IVTV_ALSA_WARN("%s: snd_ivtv_mixer_create() failed with err %d:"
  142. " proceeding anyway\n", __func__, ret);
  143. }
  144. #endif
  145. ret = snd_ivtv_pcm_create(itvsc);
  146. if (ret) {
  147. IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
  148. __func__, ret);
  149. goto err_exit_free;
  150. }
  151. /* FIXME - proc files */
  152. /* (7) Set the driver data and return 0 */
  153. /* We do this out of normal order for PCI drivers to avoid races */
  154. itv->alsa = itvsc;
  155. /* (6) Register the card instance */
  156. ret = snd_card_register(sc);
  157. if (ret) {
  158. itv->alsa = NULL;
  159. IVTV_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
  160. __func__, ret);
  161. goto err_exit_free;
  162. }
  163. return 0;
  164. err_exit_free:
  165. if (sc != NULL)
  166. snd_card_free(sc);
  167. kfree(itvsc);
  168. err_exit:
  169. return ret;
  170. }
  171. static int ivtv_alsa_load(struct ivtv *itv)
  172. {
  173. struct v4l2_device *v4l2_dev = &itv->v4l2_dev;
  174. struct ivtv_stream *s;
  175. if (v4l2_dev == NULL) {
  176. pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
  177. __func__);
  178. return 0;
  179. }
  180. itv = to_ivtv(v4l2_dev);
  181. if (itv == NULL) {
  182. pr_err("ivtv-alsa itv is NULL\n");
  183. return 0;
  184. }
  185. s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
  186. if (s->vdev.v4l2_dev == NULL) {
  187. IVTV_DEBUG_ALSA_INFO("%s: PCM stream for card is disabled - "
  188. "skipping\n", __func__);
  189. return 0;
  190. }
  191. if (itv->alsa != NULL) {
  192. IVTV_ALSA_ERR("%s: struct snd_ivtv_card * already exists\n",
  193. __func__);
  194. return 0;
  195. }
  196. if (snd_ivtv_init(v4l2_dev)) {
  197. IVTV_ALSA_ERR("%s: failed to create struct snd_ivtv_card\n",
  198. __func__);
  199. } else {
  200. IVTV_DEBUG_ALSA_INFO("%s: created ivtv ALSA interface instance "
  201. "\n", __func__);
  202. }
  203. return 0;
  204. }
  205. static int __init ivtv_alsa_init(void)
  206. {
  207. pr_info("ivtv-alsa: module loading...\n");
  208. ivtv_ext_init = &ivtv_alsa_load;
  209. return 0;
  210. }
  211. static void __exit snd_ivtv_exit(struct snd_ivtv_card *itvsc)
  212. {
  213. struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
  214. /* FIXME - pointer checks & shutdown itvsc */
  215. snd_card_free(itvsc->sc);
  216. itv->alsa = NULL;
  217. }
  218. static int __exit ivtv_alsa_exit_callback(struct device *dev, void *data)
  219. {
  220. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
  221. struct snd_ivtv_card *itvsc;
  222. if (v4l2_dev == NULL) {
  223. pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
  224. __func__);
  225. return 0;
  226. }
  227. itvsc = to_snd_ivtv_card(v4l2_dev);
  228. if (itvsc == NULL) {
  229. IVTV_ALSA_WARN("%s: struct snd_ivtv_card * is NULL\n",
  230. __func__);
  231. return 0;
  232. }
  233. snd_ivtv_exit(itvsc);
  234. return 0;
  235. }
  236. static void __exit ivtv_alsa_exit(void)
  237. {
  238. struct device_driver *drv;
  239. int ret;
  240. pr_info("ivtv-alsa: module unloading...\n");
  241. drv = driver_find("ivtv", &pci_bus_type);
  242. ret = driver_for_each_device(drv, NULL, NULL, ivtv_alsa_exit_callback);
  243. (void)ret; /* suppress compiler warning */
  244. ivtv_ext_init = NULL;
  245. pr_info("ivtv-alsa: module unload complete\n");
  246. }
  247. module_init(ivtv_alsa_init);
  248. module_exit(ivtv_alsa_exit);