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