cx18-alsa-main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * ALSA interface to cx18 PCM capture streams
  3. *
  4. * Copyright (C) 2009 Andy Walls <awalls@radix.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/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/device.h>
  28. #include <linux/spinlock.h>
  29. #include <media/v4l2-device.h>
  30. #include <sound/core.h>
  31. #include <sound/initval.h>
  32. #include "cx18-driver.h"
  33. #include "cx18-version.h"
  34. #include "cx18-alsa.h"
  35. #include "cx18-alsa-mixer.h"
  36. #include "cx18-alsa-pcm.h"
  37. int cx18_alsa_debug;
  38. #define CX18_DEBUG_ALSA_INFO(fmt, arg...) \
  39. do { \
  40. if (cx18_alsa_debug & 2) \
  41. printk(KERN_INFO "%s: " fmt, "cx18-alsa", ## arg); \
  42. } while (0);
  43. module_param_named(debug, cx18_alsa_debug, int, 0644);
  44. MODULE_PARM_DESC(debug,
  45. "Debug level (bitmask). Default: 0\n"
  46. "\t\t\t 1/0x0001: warning\n"
  47. "\t\t\t 2/0x0002: info\n");
  48. MODULE_AUTHOR("Andy Walls");
  49. MODULE_DESCRIPTION("CX23418 ALSA Interface");
  50. MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
  51. MODULE_LICENSE("GPL");
  52. MODULE_VERSION(CX18_VERSION);
  53. static inline
  54. struct snd_cx18_card *to_snd_cx18_card(struct v4l2_device *v4l2_dev)
  55. {
  56. return to_cx18(v4l2_dev)->alsa;
  57. }
  58. static inline
  59. struct snd_cx18_card *p_to_snd_cx18_card(struct v4l2_device **v4l2_dev)
  60. {
  61. return container_of(v4l2_dev, struct snd_cx18_card, v4l2_dev);
  62. }
  63. static void snd_cx18_card_free(struct snd_cx18_card *cxsc)
  64. {
  65. if (cxsc == NULL)
  66. return;
  67. if (cxsc->v4l2_dev != NULL)
  68. to_cx18(cxsc->v4l2_dev)->alsa = NULL;
  69. /* FIXME - take any other stopping actions needed */
  70. kfree(cxsc);
  71. }
  72. static void snd_cx18_card_private_free(struct snd_card *sc)
  73. {
  74. if (sc == NULL)
  75. return;
  76. snd_cx18_card_free(sc->private_data);
  77. sc->private_data = NULL;
  78. sc->private_free = NULL;
  79. }
  80. static int snd_cx18_card_create(struct v4l2_device *v4l2_dev,
  81. struct snd_card *sc,
  82. struct snd_cx18_card **cxsc)
  83. {
  84. *cxsc = kzalloc(sizeof(struct snd_cx18_card), GFP_KERNEL);
  85. if (*cxsc == NULL)
  86. return -ENOMEM;
  87. (*cxsc)->v4l2_dev = v4l2_dev;
  88. (*cxsc)->sc = sc;
  89. sc->private_data = *cxsc;
  90. sc->private_free = snd_cx18_card_private_free;
  91. return 0;
  92. }
  93. static int snd_cx18_card_set_names(struct snd_cx18_card *cxsc)
  94. {
  95. struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
  96. struct snd_card *sc = cxsc->sc;
  97. /* sc->driver is used by alsa-lib's configurator: simple, unique */
  98. strlcpy(sc->driver, "CX23418", sizeof(sc->driver));
  99. /* sc->shortname is a symlink in /proc/asound: CX18-M -> cardN */
  100. snprintf(sc->shortname, sizeof(sc->shortname), "CX18-%d",
  101. cx->instance);
  102. /* sc->longname is read from /proc/asound/cards */
  103. snprintf(sc->longname, sizeof(sc->longname),
  104. "CX23418 #%d %s TV/FM Radio/Line-In Capture",
  105. cx->instance, cx->card_name);
  106. return 0;
  107. }
  108. static int snd_cx18_init(struct v4l2_device *v4l2_dev)
  109. {
  110. struct cx18 *cx = to_cx18(v4l2_dev);
  111. struct snd_card *sc = NULL;
  112. struct snd_cx18_card *cxsc;
  113. int ret;
  114. /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
  115. /* (1) Check and increment the device index */
  116. /* This is a no-op for us. We'll use the cx->instance */
  117. /* (2) Create a card instance */
  118. ret = snd_card_create(SNDRV_DEFAULT_IDX1, /* use first available id */
  119. SNDRV_DEFAULT_STR1, /* xid from end of shortname*/
  120. THIS_MODULE, 0, &sc);
  121. if (ret) {
  122. CX18_ALSA_ERR("%s: snd_card_create() failed with err %d\n",
  123. __func__, ret);
  124. goto err_exit;
  125. }
  126. /* (3) Create a main component */
  127. ret = snd_cx18_card_create(v4l2_dev, sc, &cxsc);
  128. if (ret) {
  129. CX18_ALSA_ERR("%s: snd_cx18_card_create() failed with err %d\n",
  130. __func__, ret);
  131. goto err_exit_free;
  132. }
  133. /* (4) Set the driver ID and name strings */
  134. snd_cx18_card_set_names(cxsc);
  135. ret = snd_cx18_pcm_create(cxsc);
  136. if (ret) {
  137. CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n",
  138. __func__, ret);
  139. goto err_exit_free;
  140. }
  141. /* FIXME - proc files */
  142. /* (7) Set the driver data and return 0 */
  143. /* We do this out of normal order for PCI drivers to avoid races */
  144. cx->alsa = cxsc;
  145. /* (6) Register the card instance */
  146. ret = snd_card_register(sc);
  147. if (ret) {
  148. cx->alsa = NULL;
  149. CX18_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
  150. __func__, ret);
  151. goto err_exit_free;
  152. }
  153. return 0;
  154. err_exit_free:
  155. if (sc != NULL)
  156. snd_card_free(sc);
  157. err_exit:
  158. return ret;
  159. }
  160. int cx18_alsa_load(struct cx18 *cx)
  161. {
  162. struct v4l2_device *v4l2_dev = &cx->v4l2_dev;
  163. struct cx18_stream *s;
  164. if (v4l2_dev == NULL) {
  165. printk(KERN_ERR "cx18-alsa: %s: struct v4l2_device * is NULL\n",
  166. __func__);
  167. return 0;
  168. }
  169. cx = to_cx18(v4l2_dev);
  170. if (cx == NULL) {
  171. printk(KERN_ERR "cx18-alsa cx is NULL\n");
  172. return 0;
  173. }
  174. s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
  175. if (s->video_dev == NULL) {
  176. CX18_DEBUG_ALSA_INFO("%s: PCM stream for card is disabled - "
  177. "skipping\n", __func__);
  178. return 0;
  179. }
  180. if (cx->alsa != NULL) {
  181. CX18_ALSA_ERR("%s: struct snd_cx18_card * already exists\n",
  182. __func__);
  183. return 0;
  184. }
  185. if (snd_cx18_init(v4l2_dev)) {
  186. CX18_ALSA_ERR("%s: failed to create struct snd_cx18_card\n",
  187. __func__);
  188. } else {
  189. CX18_DEBUG_ALSA_INFO("%s: created cx18 ALSA interface instance "
  190. "\n", __func__);
  191. }
  192. return 0;
  193. }
  194. static int __init cx18_alsa_init(void)
  195. {
  196. printk(KERN_INFO "cx18-alsa: module loading...\n");
  197. cx18_ext_init = &cx18_alsa_load;
  198. return 0;
  199. }
  200. static void __exit snd_cx18_exit(struct snd_cx18_card *cxsc)
  201. {
  202. struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
  203. /* FIXME - pointer checks & shutdown cxsc */
  204. snd_card_free(cxsc->sc);
  205. cx->alsa = NULL;
  206. }
  207. static int __exit cx18_alsa_exit_callback(struct device *dev, void *data)
  208. {
  209. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
  210. struct snd_cx18_card *cxsc;
  211. if (v4l2_dev == NULL) {
  212. printk(KERN_ERR "cx18-alsa: %s: struct v4l2_device * is NULL\n",
  213. __func__);
  214. return 0;
  215. }
  216. cxsc = to_snd_cx18_card(v4l2_dev);
  217. if (cxsc == NULL) {
  218. CX18_ALSA_WARN("%s: struct snd_cx18_card * is NULL\n",
  219. __func__);
  220. return 0;
  221. }
  222. snd_cx18_exit(cxsc);
  223. return 0;
  224. }
  225. static void __exit cx18_alsa_exit(void)
  226. {
  227. struct device_driver *drv;
  228. int ret;
  229. printk(KERN_INFO "cx18-alsa: module unloading...\n");
  230. drv = driver_find("cx18", &pci_bus_type);
  231. ret = driver_for_each_device(drv, NULL, NULL, cx18_alsa_exit_callback);
  232. put_driver(drv);
  233. cx18_ext_init = NULL;
  234. printk(KERN_INFO "cx18-alsa: module unload complete\n");
  235. }
  236. module_init(cx18_alsa_init);
  237. module_exit(cx18_alsa_exit);