cx18-alsa-main.c 7.1 KB

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