Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 1 | /* |
| 2 | DVB device driver for em28xx |
| 3 | |
| 4 | (c) 2008 Mauro Carvalho Chehab <mchehab@infradead.org> |
| 5 | |
Devin Heitmueller | bdfbf95 | 2008-04-17 21:38:27 -0300 | [diff] [blame] | 6 | (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com> |
| 7 | - Fixes for the driver to properly work with HVR-950 |
Devin Heitmueller | 4fd305b | 2008-06-04 13:43:46 -0300 | [diff] [blame] | 8 | - Fixes for the driver to properly work with Pinnacle PCTV HD Pro Stick |
Devin Heitmueller | e14b365 | 2008-07-26 11:04:33 -0300 | [diff] [blame] | 9 | - Fixes for the driver to properly work with AMD ATI TV Wonder HD 600 |
Devin Heitmueller | bdfbf95 | 2008-04-17 21:38:27 -0300 | [diff] [blame] | 10 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 11 | (c) 2008 Aidan Thornton <makosoft@googlemail.com> |
| 12 | |
| 13 | Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by: |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 14 | (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au> |
| 15 | (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs] |
| 16 | |
| 17 | This program is free software; you can redistribute it and/or modify |
| 18 | it under the terms of the GNU General Public License as published by |
| 19 | the Free Software Foundation; either version 2 of the License. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/usb.h> |
| 24 | |
| 25 | #include "em28xx.h" |
| 26 | #include <media/v4l2-common.h> |
| 27 | #include <media/videobuf-vmalloc.h> |
Franklin Meng | d7de5d8 | 2009-06-06 17:05:02 -0300 | [diff] [blame] | 28 | #include <media/tuner.h> |
| 29 | #include "tuner-simple.h" |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 30 | |
| 31 | #include "lgdt330x.h" |
Aidan Thornton | 7e6388a | 2008-04-17 21:40:03 -0300 | [diff] [blame] | 32 | #include "zl10353.h" |
Robert Krakora | 6e7b9ea | 2009-01-18 21:59:34 -0300 | [diff] [blame] | 33 | #include "s5h1409.h" |
Devin Heitmueller | 4fb202a | 2009-07-12 17:51:12 -0300 | [diff] [blame] | 34 | #include "mt352.h" |
| 35 | #include "mt352_priv.h" /* FIXME */ |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 36 | |
| 37 | MODULE_DESCRIPTION("driver for em28xx based DVB cards"); |
| 38 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); |
| 39 | MODULE_LICENSE("GPL"); |
| 40 | |
| 41 | static unsigned int debug; |
| 42 | module_param(debug, int, 0644); |
| 43 | MODULE_PARM_DESC(debug, "enable debug messages [dvb]"); |
| 44 | |
| 45 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 46 | |
| 47 | #define dprintk(level, fmt, arg...) do { \ |
| 48 | if (debug >= level) \ |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 49 | printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \ |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 50 | } while (0) |
| 51 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 52 | #define EM28XX_DVB_NUM_BUFS 5 |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 53 | #define EM28XX_DVB_MAX_PACKETS 64 |
| 54 | |
| 55 | struct em28xx_dvb { |
| 56 | struct dvb_frontend *frontend; |
| 57 | |
| 58 | /* feed count management */ |
| 59 | struct mutex lock; |
| 60 | int nfeeds; |
| 61 | |
| 62 | /* general boilerplate stuff */ |
| 63 | struct dvb_adapter adapter; |
| 64 | struct dvb_demux demux; |
| 65 | struct dmxdev dmxdev; |
| 66 | struct dmx_frontend fe_hw; |
| 67 | struct dmx_frontend fe_mem; |
| 68 | struct dvb_net net; |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | static inline void print_err_status(struct em28xx *dev, |
| 73 | int packet, int status) |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 74 | { |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 75 | char *errmsg = "Unknown"; |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 76 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 77 | switch (status) { |
| 78 | case -ENOENT: |
| 79 | errmsg = "unlinked synchronuously"; |
| 80 | break; |
| 81 | case -ECONNRESET: |
| 82 | errmsg = "unlinked asynchronuously"; |
| 83 | break; |
| 84 | case -ENOSR: |
| 85 | errmsg = "Buffer error (overrun)"; |
| 86 | break; |
| 87 | case -EPIPE: |
| 88 | errmsg = "Stalled (device not responding)"; |
| 89 | break; |
| 90 | case -EOVERFLOW: |
| 91 | errmsg = "Babble (bad cable?)"; |
| 92 | break; |
| 93 | case -EPROTO: |
| 94 | errmsg = "Bit-stuff error (bad cable?)"; |
| 95 | break; |
| 96 | case -EILSEQ: |
| 97 | errmsg = "CRC/Timeout (could be anything)"; |
| 98 | break; |
| 99 | case -ETIME: |
| 100 | errmsg = "Device does not respond"; |
| 101 | break; |
| 102 | } |
| 103 | if (packet < 0) { |
| 104 | dprintk(1, "URB status %d [%s].\n", status, errmsg); |
| 105 | } else { |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 106 | dprintk(1, "URB packet %d, status %d [%s].\n", |
| 107 | packet, status, errmsg); |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 108 | } |
| 109 | } |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 110 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 111 | static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb) |
| 112 | { |
| 113 | int i; |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 114 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 115 | if (!dev) |
| 116 | return 0; |
| 117 | |
| 118 | if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED)) |
| 119 | return 0; |
| 120 | |
| 121 | if (urb->status < 0) { |
| 122 | print_err_status(dev, -1, urb->status); |
| 123 | if (urb->status == -ENOENT) |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | for (i = 0; i < urb->number_of_packets; i++) { |
| 128 | int status = urb->iso_frame_desc[i].status; |
| 129 | |
| 130 | if (status < 0) { |
| 131 | print_err_status(dev, i, status); |
| 132 | if (urb->iso_frame_desc[i].status != -EPROTO) |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer + |
| 137 | urb->iso_frame_desc[i].offset, |
| 138 | urb->iso_frame_desc[i].actual_length); |
| 139 | } |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 144 | static int start_streaming(struct em28xx_dvb *dvb) |
| 145 | { |
Mauro Carvalho Chehab | c67ec53 | 2008-04-17 21:48:00 -0300 | [diff] [blame] | 146 | int rc; |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 147 | struct em28xx *dev = dvb->adapter.priv; |
Devin Heitmueller | d18e2fd | 2009-05-16 17:09:28 -0300 | [diff] [blame] | 148 | int max_dvb_packet_size; |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 149 | |
| 150 | usb_set_interface(dev->udev, 0, 1); |
Mauro Carvalho Chehab | c67ec53 | 2008-04-17 21:48:00 -0300 | [diff] [blame] | 151 | rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE); |
| 152 | if (rc < 0) |
| 153 | return rc; |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 154 | |
Devin Heitmueller | d18e2fd | 2009-05-16 17:09:28 -0300 | [diff] [blame] | 155 | max_dvb_packet_size = em28xx_isoc_dvb_max_packetsize(dev); |
| 156 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 157 | return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS, |
Devin Heitmueller | d18e2fd | 2009-05-16 17:09:28 -0300 | [diff] [blame] | 158 | EM28XX_DVB_NUM_BUFS, max_dvb_packet_size, |
Mauro Carvalho Chehab | c67ec53 | 2008-04-17 21:48:00 -0300 | [diff] [blame] | 159 | dvb_isoc_copy); |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 160 | } |
| 161 | |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 162 | static int stop_streaming(struct em28xx_dvb *dvb) |
| 163 | { |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 164 | struct em28xx *dev = dvb->adapter.priv; |
| 165 | |
| 166 | em28xx_uninit_isoc(dev); |
Mauro Carvalho Chehab | c67ec53 | 2008-04-17 21:48:00 -0300 | [diff] [blame] | 167 | |
Mauro Carvalho Chehab | 2fe3e2e | 2008-11-27 09:10:40 -0300 | [diff] [blame] | 168 | em28xx_set_mode(dev, EM28XX_SUSPEND); |
Mauro Carvalho Chehab | c67ec53 | 2008-04-17 21:48:00 -0300 | [diff] [blame] | 169 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int start_feed(struct dvb_demux_feed *feed) |
| 174 | { |
| 175 | struct dvb_demux *demux = feed->demux; |
| 176 | struct em28xx_dvb *dvb = demux->priv; |
| 177 | int rc, ret; |
| 178 | |
| 179 | if (!demux->dmx.frontend) |
| 180 | return -EINVAL; |
| 181 | |
| 182 | mutex_lock(&dvb->lock); |
| 183 | dvb->nfeeds++; |
| 184 | rc = dvb->nfeeds; |
| 185 | |
| 186 | if (dvb->nfeeds == 1) { |
| 187 | ret = start_streaming(dvb); |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 188 | if (ret < 0) |
| 189 | rc = ret; |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | mutex_unlock(&dvb->lock); |
| 193 | return rc; |
| 194 | } |
| 195 | |
| 196 | static int stop_feed(struct dvb_demux_feed *feed) |
| 197 | { |
| 198 | struct dvb_demux *demux = feed->demux; |
| 199 | struct em28xx_dvb *dvb = demux->priv; |
| 200 | int err = 0; |
| 201 | |
| 202 | mutex_lock(&dvb->lock); |
| 203 | dvb->nfeeds--; |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 204 | |
| 205 | if (0 == dvb->nfeeds) |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 206 | err = stop_streaming(dvb); |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 207 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 208 | mutex_unlock(&dvb->lock); |
| 209 | return err; |
| 210 | } |
| 211 | |
| 212 | |
Mauro Carvalho Chehab | e3569ab | 2008-04-17 21:49:20 -0300 | [diff] [blame] | 213 | |
| 214 | /* ------------------------------------------------------------------ */ |
| 215 | static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire) |
| 216 | { |
| 217 | struct em28xx *dev = fe->dvb->priv; |
| 218 | |
| 219 | if (acquire) |
| 220 | return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE); |
| 221 | else |
Mauro Carvalho Chehab | 2fe3e2e | 2008-11-27 09:10:40 -0300 | [diff] [blame] | 222 | return em28xx_set_mode(dev, EM28XX_SUSPEND); |
Mauro Carvalho Chehab | e3569ab | 2008-04-17 21:49:20 -0300 | [diff] [blame] | 223 | } |
| 224 | |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 225 | /* ------------------------------------------------------------------ */ |
| 226 | |
Mauro Carvalho Chehab | 227ad4a | 2008-04-17 21:37:40 -0300 | [diff] [blame] | 227 | static struct lgdt330x_config em2880_lgdt3303_dev = { |
| 228 | .demod_address = 0x0e, |
| 229 | .demod_chip = LGDT3303, |
| 230 | }; |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 231 | |
Aidan Thornton | 7e6388a | 2008-04-17 21:40:03 -0300 | [diff] [blame] | 232 | static struct zl10353_config em28xx_zl10353_with_xc3028 = { |
| 233 | .demod_address = (0x1e >> 1), |
| 234 | .no_tuner = 1, |
| 235 | .parallel_ts = 1, |
| 236 | .if2 = 45600, |
| 237 | }; |
| 238 | |
Robert Krakora | 6e7b9ea | 2009-01-18 21:59:34 -0300 | [diff] [blame] | 239 | static struct s5h1409_config em28xx_s5h1409_with_xc3028 = { |
| 240 | .demod_address = 0x32 >> 1, |
| 241 | .output_mode = S5H1409_PARALLEL_OUTPUT, |
| 242 | .gpio = S5H1409_GPIO_OFF, |
| 243 | .inversion = S5H1409_INVERSION_OFF, |
| 244 | .status_mode = S5H1409_DEMODLOCKING, |
| 245 | .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK |
| 246 | }; |
| 247 | |
Devin Heitmueller | a84f79a | 2009-07-12 17:05:02 -0300 | [diff] [blame] | 248 | static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = { |
Devin Heitmueller | f797608 | 2009-06-22 22:32:32 -0300 | [diff] [blame] | 249 | .demod_address = (0x1e >> 1), |
| 250 | .no_tuner = 1, |
| 251 | .disable_i2c_gate_ctrl = 1, |
| 252 | .parallel_ts = 1, |
| 253 | .if2 = 45600, |
| 254 | }; |
| 255 | |
Devin Heitmueller | 17d9d55 | 2008-06-08 10:22:03 -0300 | [diff] [blame] | 256 | #ifdef EM28XX_DRX397XD_SUPPORT |
| 257 | /* [TODO] djh - not sure yet what the device config needs to contain */ |
| 258 | static struct drx397xD_config em28xx_drx397xD_with_xc3028 = { |
| 259 | .demod_address = (0xe0 >> 1), |
| 260 | }; |
| 261 | #endif |
| 262 | |
Devin Heitmueller | 4fb202a | 2009-07-12 17:51:12 -0300 | [diff] [blame] | 263 | static int mt352_terratec_xs_init(struct dvb_frontend *fe) |
| 264 | { |
| 265 | /* Values extracted from a USB trace of the Terratec Windows driver */ |
| 266 | static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c }; |
| 267 | static u8 reset[] = { RESET, 0x80 }; |
| 268 | static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 }; |
| 269 | static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 }; |
| 270 | static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 }; |
| 271 | static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d }; |
| 272 | static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 }; |
| 273 | static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 }; |
| 274 | static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 }; |
Devin Heitmueller | ff69786 | 2009-07-12 18:44:19 -0300 | [diff] [blame] | 275 | static u8 tuner_go[] = { TUNER_GO, 0x01}; |
Devin Heitmueller | 4fb202a | 2009-07-12 17:51:12 -0300 | [diff] [blame] | 276 | |
| 277 | mt352_write(fe, clock_config, sizeof(clock_config)); |
| 278 | udelay(200); |
| 279 | mt352_write(fe, reset, sizeof(reset)); |
| 280 | mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg)); |
| 281 | mt352_write(fe, agc_cfg, sizeof(agc_cfg)); |
| 282 | mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg)); |
| 283 | mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg)); |
| 284 | mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg)); |
| 285 | mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg)); |
| 286 | mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg)); |
| 287 | mt352_write(fe, tuner_go, sizeof(tuner_go)); |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static struct mt352_config terratec_xs_mt352_cfg = { |
| 292 | .demod_address = (0x1e >> 1), |
| 293 | .no_tuner = 1, |
| 294 | .if2 = 45600, |
| 295 | .demod_init = mt352_terratec_xs_init, |
| 296 | }; |
| 297 | |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 298 | /* ------------------------------------------------------------------ */ |
| 299 | |
| 300 | static int attach_xc3028(u8 addr, struct em28xx *dev) |
| 301 | { |
| 302 | struct dvb_frontend *fe; |
Mauro Carvalho Chehab | 3ca9c09 | 2008-04-17 21:37:53 -0300 | [diff] [blame] | 303 | struct xc2028_config cfg; |
| 304 | |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 305 | memset(&cfg, 0, sizeof(cfg)); |
Mauro Carvalho Chehab | 3ca9c09 | 2008-04-17 21:37:53 -0300 | [diff] [blame] | 306 | cfg.i2c_adap = &dev->i2c_adap; |
| 307 | cfg.i2c_addr = addr; |
Mauro Carvalho Chehab | 3ca9c09 | 2008-04-17 21:37:53 -0300 | [diff] [blame] | 308 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 309 | if (!dev->dvb->frontend) { |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 310 | printk(KERN_ERR "%s/2: dvb frontend not attached. " |
| 311 | "Can't attach xc3028\n", |
| 312 | dev->name); |
| 313 | return -EINVAL; |
| 314 | } |
| 315 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 316 | fe = dvb_attach(xc2028_attach, dev->dvb->frontend, &cfg); |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 317 | if (!fe) { |
| 318 | printk(KERN_ERR "%s/2: xc3028 attach failed\n", |
| 319 | dev->name); |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 320 | dvb_frontend_detach(dev->dvb->frontend); |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 321 | dev->dvb->frontend = NULL; |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 322 | return -EINVAL; |
| 323 | } |
| 324 | |
| 325 | printk(KERN_INFO "%s/2: xc3028 attached\n", dev->name); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 330 | /* ------------------------------------------------------------------ */ |
| 331 | |
Hans Verkuil | d45b9b8 | 2008-09-04 03:33:43 -0300 | [diff] [blame] | 332 | static int register_dvb(struct em28xx_dvb *dvb, |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 333 | struct module *module, |
| 334 | struct em28xx *dev, |
| 335 | struct device *device) |
| 336 | { |
| 337 | int result; |
| 338 | |
| 339 | mutex_init(&dvb->lock); |
| 340 | |
| 341 | /* register adapter */ |
| 342 | result = dvb_register_adapter(&dvb->adapter, dev->name, module, device, |
| 343 | adapter_nr); |
| 344 | if (result < 0) { |
| 345 | printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n", |
| 346 | dev->name, result); |
| 347 | goto fail_adapter; |
| 348 | } |
Mauro Carvalho Chehab | e3569ab | 2008-04-17 21:49:20 -0300 | [diff] [blame] | 349 | |
| 350 | /* Ensure all frontends negotiate bus access */ |
| 351 | dvb->frontend->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl; |
| 352 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 353 | dvb->adapter.priv = dev; |
| 354 | |
| 355 | /* register frontend */ |
| 356 | result = dvb_register_frontend(&dvb->adapter, dvb->frontend); |
| 357 | if (result < 0) { |
| 358 | printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n", |
| 359 | dev->name, result); |
| 360 | goto fail_frontend; |
| 361 | } |
| 362 | |
| 363 | /* register demux stuff */ |
| 364 | dvb->demux.dmx.capabilities = |
| 365 | DMX_TS_FILTERING | DMX_SECTION_FILTERING | |
| 366 | DMX_MEMORY_BASED_FILTERING; |
| 367 | dvb->demux.priv = dvb; |
| 368 | dvb->demux.filternum = 256; |
| 369 | dvb->demux.feednum = 256; |
| 370 | dvb->demux.start_feed = start_feed; |
| 371 | dvb->demux.stop_feed = stop_feed; |
Mauro Carvalho Chehab | e3569ab | 2008-04-17 21:49:20 -0300 | [diff] [blame] | 372 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 373 | result = dvb_dmx_init(&dvb->demux); |
| 374 | if (result < 0) { |
| 375 | printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n", |
| 376 | dev->name, result); |
| 377 | goto fail_dmx; |
| 378 | } |
| 379 | |
| 380 | dvb->dmxdev.filternum = 256; |
| 381 | dvb->dmxdev.demux = &dvb->demux.dmx; |
| 382 | dvb->dmxdev.capabilities = 0; |
| 383 | result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter); |
| 384 | if (result < 0) { |
| 385 | printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n", |
| 386 | dev->name, result); |
| 387 | goto fail_dmxdev; |
| 388 | } |
| 389 | |
| 390 | dvb->fe_hw.source = DMX_FRONTEND_0; |
| 391 | result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw); |
| 392 | if (result < 0) { |
| 393 | printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n", |
| 394 | dev->name, result); |
| 395 | goto fail_fe_hw; |
| 396 | } |
| 397 | |
| 398 | dvb->fe_mem.source = DMX_MEMORY_FE; |
| 399 | result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem); |
| 400 | if (result < 0) { |
| 401 | printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n", |
| 402 | dev->name, result); |
| 403 | goto fail_fe_mem; |
| 404 | } |
| 405 | |
| 406 | result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw); |
| 407 | if (result < 0) { |
| 408 | printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n", |
| 409 | dev->name, result); |
| 410 | goto fail_fe_conn; |
| 411 | } |
| 412 | |
| 413 | /* register network adapter */ |
| 414 | dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx); |
| 415 | return 0; |
| 416 | |
| 417 | fail_fe_conn: |
| 418 | dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem); |
| 419 | fail_fe_mem: |
| 420 | dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw); |
| 421 | fail_fe_hw: |
| 422 | dvb_dmxdev_release(&dvb->dmxdev); |
| 423 | fail_dmxdev: |
| 424 | dvb_dmx_release(&dvb->demux); |
| 425 | fail_dmx: |
| 426 | dvb_unregister_frontend(dvb->frontend); |
| 427 | fail_frontend: |
| 428 | dvb_frontend_detach(dvb->frontend); |
| 429 | dvb_unregister_adapter(&dvb->adapter); |
| 430 | fail_adapter: |
| 431 | return result; |
| 432 | } |
| 433 | |
| 434 | static void unregister_dvb(struct em28xx_dvb *dvb) |
| 435 | { |
| 436 | dvb_net_release(&dvb->net); |
| 437 | dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem); |
| 438 | dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw); |
| 439 | dvb_dmxdev_release(&dvb->dmxdev); |
| 440 | dvb_dmx_release(&dvb->demux); |
| 441 | dvb_unregister_frontend(dvb->frontend); |
| 442 | dvb_frontend_detach(dvb->frontend); |
| 443 | dvb_unregister_adapter(&dvb->adapter); |
| 444 | } |
| 445 | |
| 446 | |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 447 | static int dvb_init(struct em28xx *dev) |
| 448 | { |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 449 | int result = 0; |
| 450 | struct em28xx_dvb *dvb; |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 451 | |
Mauro Carvalho Chehab | 505b6d0 | 2008-11-25 09:39:50 -0300 | [diff] [blame] | 452 | if (!dev->board.has_dvb) { |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 453 | /* This device does not support the extension */ |
| 454 | return 0; |
| 455 | } |
| 456 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 457 | dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL); |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 458 | |
| 459 | if (dvb == NULL) { |
| 460 | printk(KERN_INFO "em28xx_dvb: memory allocation failed\n"); |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 461 | return -ENOMEM; |
| 462 | } |
| 463 | dev->dvb = dvb; |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 464 | |
Mauro Carvalho Chehab | c67ec53 | 2008-04-17 21:48:00 -0300 | [diff] [blame] | 465 | em28xx_set_mode(dev, EM28XX_DIGITAL_MODE); |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 466 | /* init frontend */ |
| 467 | switch (dev->model) { |
Douglas Schilling Landgraf | f89bc32 | 2008-12-01 21:01:04 -0300 | [diff] [blame] | 468 | case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850: |
Mauro Carvalho Chehab | 10ac660 | 2008-07-27 14:58:58 -0300 | [diff] [blame] | 469 | case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950: |
Devin Heitmueller | 4fd305b | 2008-06-04 13:43:46 -0300 | [diff] [blame] | 470 | case EM2880_BOARD_PINNACLE_PCTV_HD_PRO: |
Devin Heitmueller | e14b365 | 2008-07-26 11:04:33 -0300 | [diff] [blame] | 471 | case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600: |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 472 | dvb->frontend = dvb_attach(lgdt330x_attach, |
| 473 | &em2880_lgdt3303_dev, |
| 474 | &dev->i2c_adap); |
| 475 | if (attach_xc3028(0x61, dev) < 0) { |
| 476 | result = -EINVAL; |
| 477 | goto out_free; |
| 478 | } |
Mauro Carvalho Chehab | 227ad4a | 2008-04-17 21:37:40 -0300 | [diff] [blame] | 479 | break; |
Thierry MERLE | 46510b5 | 2008-10-11 16:56:13 -0300 | [diff] [blame] | 480 | case EM2880_BOARD_KWORLD_DVB_310U: |
Douglas Schilling Landgraf | e5db5d4 | 2009-04-09 18:24:34 -0300 | [diff] [blame] | 481 | case EM2880_BOARD_EMPIRE_DUAL_TV: |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 482 | dvb->frontend = dvb_attach(zl10353_attach, |
| 483 | &em28xx_zl10353_with_xc3028, |
| 484 | &dev->i2c_adap); |
| 485 | if (attach_xc3028(0x61, dev) < 0) { |
| 486 | result = -EINVAL; |
| 487 | goto out_free; |
| 488 | } |
Aidan Thornton | 7e6388a | 2008-04-17 21:40:03 -0300 | [diff] [blame] | 489 | break; |
Devin Heitmueller | a84f79a | 2009-07-12 17:05:02 -0300 | [diff] [blame] | 490 | case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900: |
| 491 | dvb->frontend = dvb_attach(zl10353_attach, |
| 492 | &em28xx_zl10353_xc3028_no_i2c_gate, |
| 493 | &dev->i2c_adap); |
| 494 | if (attach_xc3028(0x61, dev) < 0) { |
| 495 | result = -EINVAL; |
| 496 | goto out_free; |
| 497 | } |
| 498 | break; |
Devin Heitmueller | f797608 | 2009-06-22 22:32:32 -0300 | [diff] [blame] | 499 | case EM2880_BOARD_TERRATEC_HYBRID_XS: |
Devin Heitmueller | d5b3ba9 | 2009-07-08 21:51:35 -0300 | [diff] [blame] | 500 | case EM2881_BOARD_PINNACLE_HYBRID_PRO: |
Devin Heitmueller | f797608 | 2009-06-22 22:32:32 -0300 | [diff] [blame] | 501 | dvb->frontend = dvb_attach(zl10353_attach, |
Devin Heitmueller | a84f79a | 2009-07-12 17:05:02 -0300 | [diff] [blame] | 502 | &em28xx_zl10353_xc3028_no_i2c_gate, |
Devin Heitmueller | f797608 | 2009-06-22 22:32:32 -0300 | [diff] [blame] | 503 | &dev->i2c_adap); |
| 504 | if (dvb->frontend == NULL) { |
| 505 | /* This board could have either a zl10353 or a mt352. |
| 506 | If the chip id isn't for zl10353, try mt352 */ |
Devin Heitmueller | 4fb202a | 2009-07-12 17:51:12 -0300 | [diff] [blame] | 507 | dvb->frontend = dvb_attach(mt352_attach, |
| 508 | &terratec_xs_mt352_cfg, |
| 509 | &dev->i2c_adap); |
Devin Heitmueller | f797608 | 2009-06-22 22:32:32 -0300 | [diff] [blame] | 510 | } |
Devin Heitmueller | 4fb202a | 2009-07-12 17:51:12 -0300 | [diff] [blame] | 511 | |
Devin Heitmueller | f797608 | 2009-06-22 22:32:32 -0300 | [diff] [blame] | 512 | if (attach_xc3028(0x61, dev) < 0) { |
| 513 | result = -EINVAL; |
| 514 | goto out_free; |
| 515 | } |
| 516 | break; |
Robert Krakora | 6e7b9ea | 2009-01-18 21:59:34 -0300 | [diff] [blame] | 517 | case EM2883_BOARD_KWORLD_HYBRID_330U: |
Devin Heitmueller | 1985922 | 2009-06-19 00:33:54 -0300 | [diff] [blame] | 518 | case EM2882_BOARD_EVGA_INDTUBE: |
Robert Krakora | 6e7b9ea | 2009-01-18 21:59:34 -0300 | [diff] [blame] | 519 | dvb->frontend = dvb_attach(s5h1409_attach, |
| 520 | &em28xx_s5h1409_with_xc3028, |
| 521 | &dev->i2c_adap); |
| 522 | if (attach_xc3028(0x61, dev) < 0) { |
| 523 | result = -EINVAL; |
| 524 | goto out_free; |
| 525 | } |
| 526 | break; |
Franklin Meng | d7de5d8 | 2009-06-06 17:05:02 -0300 | [diff] [blame] | 527 | case EM2882_BOARD_KWORLD_ATSC_315U: |
| 528 | dvb->frontend = dvb_attach(lgdt330x_attach, |
| 529 | &em2880_lgdt3303_dev, |
| 530 | &dev->i2c_adap); |
| 531 | if (dvb->frontend != NULL) { |
| 532 | if (!dvb_attach(simple_tuner_attach, dvb->frontend, |
| 533 | &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) { |
| 534 | result = -EINVAL; |
| 535 | goto out_free; |
| 536 | } |
| 537 | } |
| 538 | break; |
Devin Heitmueller | 17d9d55 | 2008-06-08 10:22:03 -0300 | [diff] [blame] | 539 | case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2: |
| 540 | #ifdef EM28XX_DRX397XD_SUPPORT |
| 541 | /* We don't have the config structure properly populated, so |
| 542 | this is commented out for now */ |
| 543 | dvb->frontend = dvb_attach(drx397xD_attach, |
| 544 | &em28xx_drx397xD_with_xc3028, |
| 545 | &dev->i2c_adap); |
| 546 | if (attach_xc3028(0x61, dev) < 0) { |
| 547 | result = -EINVAL; |
| 548 | goto out_free; |
| 549 | } |
| 550 | break; |
| 551 | #endif |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 552 | default: |
| 553 | printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card" |
| 554 | " isn't supported yet\n", |
| 555 | dev->name); |
| 556 | break; |
| 557 | } |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 558 | if (NULL == dvb->frontend) { |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 559 | printk(KERN_ERR |
| 560 | "%s/2: frontend initialization failed\n", |
| 561 | dev->name); |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 562 | result = -EINVAL; |
| 563 | goto out_free; |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 564 | } |
Michael Krufky | d7cba04 | 2008-09-12 13:31:45 -0300 | [diff] [blame] | 565 | /* define general-purpose callback pointer */ |
| 566 | dvb->frontend->callback = em28xx_tuner_callback; |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 567 | |
| 568 | /* register everything */ |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 569 | result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev); |
| 570 | |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 571 | if (result < 0) |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 572 | goto out_free; |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 573 | |
Mauro Carvalho Chehab | 2fe3e2e | 2008-11-27 09:10:40 -0300 | [diff] [blame] | 574 | em28xx_set_mode(dev, EM28XX_SUSPEND); |
Mauro Carvalho Chehab | 102a0b0 | 2008-04-17 21:40:45 -0300 | [diff] [blame] | 575 | printk(KERN_INFO "Successfully loaded em28xx-dvb\n"); |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 576 | return 0; |
| 577 | |
| 578 | out_free: |
Mauro Carvalho Chehab | 2fe3e2e | 2008-11-27 09:10:40 -0300 | [diff] [blame] | 579 | em28xx_set_mode(dev, EM28XX_SUSPEND); |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 580 | kfree(dvb); |
| 581 | dev->dvb = NULL; |
| 582 | return result; |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | static int dvb_fini(struct em28xx *dev) |
| 586 | { |
Mauro Carvalho Chehab | 505b6d0 | 2008-11-25 09:39:50 -0300 | [diff] [blame] | 587 | if (!dev->board.has_dvb) { |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 588 | /* This device does not support the extension */ |
| 589 | return 0; |
| 590 | } |
| 591 | |
Aidan Thornton | 3421b77 | 2008-04-17 21:40:36 -0300 | [diff] [blame] | 592 | if (dev->dvb) { |
| 593 | unregister_dvb(dev->dvb); |
| 594 | dev->dvb = NULL; |
| 595 | } |
Mauro Carvalho Chehab | 3aefb79 | 2008-04-17 21:36:41 -0300 | [diff] [blame] | 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static struct em28xx_ops dvb_ops = { |
| 601 | .id = EM28XX_DVB, |
| 602 | .name = "Em28xx dvb Extension", |
| 603 | .init = dvb_init, |
| 604 | .fini = dvb_fini, |
| 605 | }; |
| 606 | |
| 607 | static int __init em28xx_dvb_register(void) |
| 608 | { |
| 609 | return em28xx_register_extension(&dvb_ops); |
| 610 | } |
| 611 | |
| 612 | static void __exit em28xx_dvb_unregister(void) |
| 613 | { |
| 614 | em28xx_unregister_extension(&dvb_ops); |
| 615 | } |
| 616 | |
| 617 | module_init(em28xx_dvb_register); |
| 618 | module_exit(em28xx_dvb_unregister); |