ivtv-alsa-main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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_create(SNDRV_DEFAULT_IDX1, /* use first available id */
  120. SNDRV_DEFAULT_STR1, /* xid from end of shortname*/
  121. THIS_MODULE, 0, &sc);
  122. if (ret) {
  123. IVTV_ALSA_ERR("%s: snd_card_create() failed with err %d\n",
  124. __func__, ret);
  125. goto err_exit;
  126. }
  127. /* (3) Create a main component */
  128. ret = snd_ivtv_card_create(v4l2_dev, sc, &itvsc);
  129. if (ret) {
  130. IVTV_ALSA_ERR("%s: snd_ivtv_card_create() failed with err %d\n",
  131. __func__, ret);
  132. goto err_exit_free;
  133. }
  134. /* (4) Set the driver ID and name strings */
  135. snd_ivtv_card_set_names(itvsc);
  136. /* (5) Create other components: mixer, PCM, & proc files */
  137. #if 0
  138. ret = snd_ivtv_mixer_create(itvsc);
  139. if (ret) {
  140. IVTV_ALSA_WARN("%s: snd_ivtv_mixer_create() failed with err %d:"
  141. " proceeding anyway\n", __func__, ret);
  142. }
  143. #endif
  144. ret = snd_ivtv_pcm_create(itvsc);
  145. if (ret) {
  146. IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
  147. __func__, ret);
  148. goto err_exit_free;
  149. }
  150. /* FIXME - proc files */
  151. /* (7) Set the driver data and return 0 */
  152. /* We do this out of normal order for PCI drivers to avoid races */
  153. itv->alsa = itvsc;
  154. /* (6) Register the card instance */
  155. ret = snd_card_register(sc);
  156. if (ret) {
  157. itv->alsa = NULL;
  158. IVTV_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
  159. __func__, ret);
  160. goto err_exit_free;
  161. }
  162. return 0;
  163. err_exit_free:
  164. if (sc != NULL)
  165. snd_card_free(sc);
  166. kfree(itvsc);
  167. err_exit:
  168. return ret;
  169. }
  170. static int ivtv_alsa_load(struct ivtv *itv)
  171. {
  172. struct v4l2_device *v4l2_dev = &itv->v4l2_dev;
  173. struct ivtv_stream *s;
  174. if (v4l2_dev == NULL) {
  175. pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
  176. __func__);
  177. return 0;
  178. }
  179. itv = to_ivtv(v4l2_dev);
  180. if (itv == NULL) {
  181. pr_err("ivtv-alsa itv is NULL\n");
  182. return 0;
  183. }
  184. s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
  185. if (s->vdev == NULL) {
  186. IVTV_DEBUG_ALSA_INFO("%s: PCM stream for card is disabled - "
  187. "skipping\n", __func__);
  188. return 0;
  189. }
  190. if (itv->alsa != NULL) {
  191. IVTV_ALSA_ERR("%s: struct snd_ivtv_card * already exists\n",
  192. __func__);
  193. return 0;
  194. }
  195. if (snd_ivtv_init(v4l2_dev)) {
  196. IVTV_ALSA_ERR("%s: failed to create struct snd_ivtv_card\n",
  197. __func__);
  198. } else {
  199. IVTV_DEBUG_ALSA_INFO("%s: created ivtv ALSA interface instance "
  200. "\n", __func__);
  201. }
  202. return 0;
  203. }
  204. static int __init ivtv_alsa_init(void)
  205. {
  206. pr_info("ivtv-alsa: module loading...\n");
  207. ivtv_ext_init = &ivtv_alsa_load;
  208. return 0;
  209. }
  210. static void __exit snd_ivtv_exit(struct snd_ivtv_card *itvsc)
  211. {
  212. struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
  213. /* FIXME - pointer checks & shutdown itvsc */
  214. snd_card_free(itvsc->sc);
  215. itv->alsa = NULL;
  216. }
  217. static int __exit ivtv_alsa_exit_callback(struct device *dev, void *data)
  218. {
  219. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
  220. struct snd_ivtv_card *itvsc;
  221. if (v4l2_dev == NULL) {
  222. pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
  223. __func__);
  224. return 0;
  225. }
  226. itvsc = to_snd_ivtv_card(v4l2_dev);
  227. if (itvsc == NULL) {
  228. IVTV_ALSA_WARN("%s: struct snd_ivtv_card * is NULL\n",
  229. __func__);
  230. return 0;
  231. }
  232. snd_ivtv_exit(itvsc);
  233. return 0;
  234. }
  235. static void __exit ivtv_alsa_exit(void)
  236. {
  237. struct device_driver *drv;
  238. int ret;
  239. pr_info("ivtv-alsa: module unloading...\n");
  240. drv = driver_find("ivtv", &pci_bus_type);
  241. ret = driver_for_each_device(drv, NULL, NULL, ivtv_alsa_exit_callback);
  242. (void)ret; /* suppress compiler warning */
  243. ivtv_ext_init = NULL;
  244. pr_info("ivtv-alsa: module unload complete\n");
  245. }
  246. module_init(ivtv_alsa_init);
  247. module_exit(ivtv_alsa_exit);