cx18-alsa-main.c 6.3 KB

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