GNU Linux-libre 4.9.317-gnu1
[releases.git] / drivers / media / usb / dvb-usb-v2 / mxl111sf.c
1 /*
2  * Copyright (C) 2010-2014 Michael Krufky (mkrufky@linuxtv.org)
3  *
4  *   This program is free software; you can redistribute it and/or modify it
5  *   under the terms of the GNU General Public License as published by the Free
6  *   Software Foundation, version 2.
7  *
8  * see Documentation/dvb/README.dvb-usb for more information
9  */
10
11 #include <linux/vmalloc.h>
12 #include <linux/i2c.h>
13 #include <media/tuner.h>
14
15 #include "mxl111sf.h"
16 #include "mxl111sf-reg.h"
17 #include "mxl111sf-phy.h"
18 #include "mxl111sf-i2c.h"
19 #include "mxl111sf-gpio.h"
20
21 #include "mxl111sf-demod.h"
22 #include "mxl111sf-tuner.h"
23
24 #include "lgdt3305.h"
25 #include "lg2160.h"
26
27 int dvb_usb_mxl111sf_debug;
28 module_param_named(debug, dvb_usb_mxl111sf_debug, int, 0644);
29 MODULE_PARM_DESC(debug, "set debugging level "
30                  "(1=info, 2=xfer, 4=i2c, 8=reg, 16=adv (or-able)).");
31
32 static int dvb_usb_mxl111sf_isoc;
33 module_param_named(isoc, dvb_usb_mxl111sf_isoc, int, 0644);
34 MODULE_PARM_DESC(isoc, "enable usb isoc xfer (0=bulk, 1=isoc).");
35
36 static int dvb_usb_mxl111sf_spi;
37 module_param_named(spi, dvb_usb_mxl111sf_spi, int, 0644);
38 MODULE_PARM_DESC(spi, "use spi rather than tp for data xfer (0=tp, 1=spi).");
39
40 #define ANT_PATH_AUTO 0
41 #define ANT_PATH_EXTERNAL 1
42 #define ANT_PATH_INTERNAL 2
43
44 static int dvb_usb_mxl111sf_rfswitch =
45 #if 0
46                 ANT_PATH_AUTO;
47 #else
48                 ANT_PATH_EXTERNAL;
49 #endif
50
51 module_param_named(rfswitch, dvb_usb_mxl111sf_rfswitch, int, 0644);
52 MODULE_PARM_DESC(rfswitch, "force rf switch position (0=auto, 1=ext, 2=int).");
53
54 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
55
56 int mxl111sf_ctrl_msg(struct mxl111sf_state *state,
57                       u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen)
58 {
59         struct dvb_usb_device *d = state->d;
60         int wo = (rbuf == NULL || rlen == 0); /* write-only */
61         int ret;
62
63         if (1 + wlen > MXL_MAX_XFER_SIZE) {
64                 pr_warn("%s: len=%d is too big!\n", __func__, wlen);
65                 return -EOPNOTSUPP;
66         }
67
68         pr_debug("%s(wlen = %d, rlen = %d)\n", __func__, wlen, rlen);
69
70         mutex_lock(&state->msg_lock);
71         memset(state->sndbuf, 0, 1+wlen);
72         memset(state->rcvbuf, 0, rlen);
73
74         state->sndbuf[0] = cmd;
75         memcpy(&state->sndbuf[1], wbuf, wlen);
76
77         ret = (wo) ? dvb_usbv2_generic_write(d, state->sndbuf, 1+wlen) :
78                 dvb_usbv2_generic_rw(d, state->sndbuf, 1+wlen, state->rcvbuf,
79                                      rlen);
80
81         memcpy(rbuf, state->rcvbuf, rlen);
82         mutex_unlock(&state->msg_lock);
83
84         mxl_fail(ret);
85
86         return ret;
87 }
88
89 /* ------------------------------------------------------------------------ */
90
91 #define MXL_CMD_REG_READ        0xaa
92 #define MXL_CMD_REG_WRITE       0x55
93
94 int mxl111sf_read_reg(struct mxl111sf_state *state, u8 addr, u8 *data)
95 {
96         u8 buf[2];
97         int ret;
98
99         ret = mxl111sf_ctrl_msg(state, MXL_CMD_REG_READ, &addr, 1, buf, 2);
100         if (mxl_fail(ret)) {
101                 mxl_debug("error reading reg: 0x%02x", addr);
102                 goto fail;
103         }
104
105         if (buf[0] == addr)
106                 *data = buf[1];
107         else {
108                 pr_err("invalid response reading reg: 0x%02x != 0x%02x, 0x%02x",
109                     addr, buf[0], buf[1]);
110                 ret = -EINVAL;
111         }
112
113         pr_debug("R: (0x%02x, 0x%02x)\n", addr, buf[1]);
114 fail:
115         return ret;
116 }
117
118 int mxl111sf_write_reg(struct mxl111sf_state *state, u8 addr, u8 data)
119 {
120         u8 buf[] = { addr, data };
121         int ret;
122
123         pr_debug("W: (0x%02x, 0x%02x)\n", addr, data);
124
125         ret = mxl111sf_ctrl_msg(state, MXL_CMD_REG_WRITE, buf, 2, NULL, 0);
126         if (mxl_fail(ret))
127                 pr_err("error writing reg: 0x%02x, val: 0x%02x", addr, data);
128         return ret;
129 }
130
131 /* ------------------------------------------------------------------------ */
132
133 int mxl111sf_write_reg_mask(struct mxl111sf_state *state,
134                                    u8 addr, u8 mask, u8 data)
135 {
136         int ret;
137         u8 val = 0;
138
139         if (mask != 0xff) {
140                 ret = mxl111sf_read_reg(state, addr, &val);
141 #if 1
142                 /* dont know why this usually errors out on the first try */
143                 if (mxl_fail(ret))
144                         pr_err("error writing addr: 0x%02x, mask: 0x%02x, "
145                             "data: 0x%02x, retrying...", addr, mask, data);
146
147                 ret = mxl111sf_read_reg(state, addr, &val);
148 #endif
149                 if (mxl_fail(ret))
150                         goto fail;
151         }
152         val &= ~mask;
153         val |= data;
154
155         ret = mxl111sf_write_reg(state, addr, val);
156         mxl_fail(ret);
157 fail:
158         return ret;
159 }
160
161 /* ------------------------------------------------------------------------ */
162
163 int mxl111sf_ctrl_program_regs(struct mxl111sf_state *state,
164                                struct mxl111sf_reg_ctrl_info *ctrl_reg_info)
165 {
166         int i, ret = 0;
167
168         for (i = 0;  ctrl_reg_info[i].addr |
169                      ctrl_reg_info[i].mask |
170                      ctrl_reg_info[i].data;  i++) {
171
172                 ret = mxl111sf_write_reg_mask(state,
173                                               ctrl_reg_info[i].addr,
174                                               ctrl_reg_info[i].mask,
175                                               ctrl_reg_info[i].data);
176                 if (mxl_fail(ret)) {
177                         pr_err("failed on reg #%d (0x%02x)", i,
178                             ctrl_reg_info[i].addr);
179                         break;
180                 }
181         }
182         return ret;
183 }
184
185 /* ------------------------------------------------------------------------ */
186
187 static int mxl1x1sf_get_chip_info(struct mxl111sf_state *state)
188 {
189         int ret;
190         u8 id, ver;
191         char *mxl_chip, *mxl_rev;
192
193         if ((state->chip_id) && (state->chip_ver))
194                 return 0;
195
196         ret = mxl111sf_read_reg(state, CHIP_ID_REG, &id);
197         if (mxl_fail(ret))
198                 goto fail;
199         state->chip_id = id;
200
201         ret = mxl111sf_read_reg(state, TOP_CHIP_REV_ID_REG, &ver);
202         if (mxl_fail(ret))
203                 goto fail;
204         state->chip_ver = ver;
205
206         switch (id) {
207         case 0x61:
208                 mxl_chip = "MxL101SF";
209                 break;
210         case 0x63:
211                 mxl_chip = "MxL111SF";
212                 break;
213         default:
214                 mxl_chip = "UNKNOWN MxL1X1";
215                 break;
216         }
217         switch (ver) {
218         case 0x36:
219                 state->chip_rev = MXL111SF_V6;
220                 mxl_rev = "v6";
221                 break;
222         case 0x08:
223                 state->chip_rev = MXL111SF_V8_100;
224                 mxl_rev = "v8_100";
225                 break;
226         case 0x18:
227                 state->chip_rev = MXL111SF_V8_200;
228                 mxl_rev = "v8_200";
229                 break;
230         default:
231                 state->chip_rev = 0;
232                 mxl_rev = "UNKNOWN REVISION";
233                 break;
234         }
235         pr_info("%s detected, %s (0x%x)", mxl_chip, mxl_rev, ver);
236 fail:
237         return ret;
238 }
239
240 #define get_chip_info(state)                                            \
241 ({                                                                      \
242         int ___ret;                                                     \
243         ___ret = mxl1x1sf_get_chip_info(state);                         \
244         if (mxl_fail(___ret)) {                                         \
245                 mxl_debug("failed to get chip info"                     \
246                           " on first probe attempt");                   \
247                 ___ret = mxl1x1sf_get_chip_info(state);                 \
248                 if (mxl_fail(___ret))                                   \
249                         pr_err("failed to get chip info during probe"); \
250                 else                                                    \
251                         mxl_debug("probe needed a retry "               \
252                                   "in order to succeed.");              \
253         }                                                               \
254         ___ret;                                                         \
255 })
256
257 /* ------------------------------------------------------------------------ */
258 #if 0
259 static int mxl111sf_power_ctrl(struct dvb_usb_device *d, int onoff)
260 {
261         /* power control depends on which adapter is being woken:
262          * save this for init, instead, via mxl111sf_adap_fe_init */
263         return 0;
264 }
265 #endif
266
267 static int mxl111sf_adap_fe_init(struct dvb_frontend *fe)
268 {
269         struct dvb_usb_device *d = fe_to_d(fe);
270         struct mxl111sf_state *state = fe_to_priv(fe);
271         struct mxl111sf_adap_state *adap_state = &state->adap_state[fe->id];
272         int err;
273
274         /* exit if we didn't initialize the driver yet */
275         if (!state->chip_id) {
276                 mxl_debug("driver not yet initialized, exit.");
277                 goto fail;
278         }
279
280         pr_debug("%s()\n", __func__);
281
282         mutex_lock(&state->fe_lock);
283
284         state->alt_mode = adap_state->alt_mode;
285
286         if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
287                 pr_err("set interface failed");
288
289         err = mxl1x1sf_soft_reset(state);
290         mxl_fail(err);
291         err = mxl111sf_init_tuner_demod(state);
292         mxl_fail(err);
293         err = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
294
295         mxl_fail(err);
296         err = mxl111sf_enable_usb_output(state);
297         mxl_fail(err);
298         err = mxl1x1sf_top_master_ctrl(state, 1);
299         mxl_fail(err);
300
301         if ((MXL111SF_GPIO_MOD_DVBT != adap_state->gpio_mode) &&
302             (state->chip_rev > MXL111SF_V6)) {
303                 mxl111sf_config_pin_mux_modes(state,
304                                               PIN_MUX_TS_SPI_IN_MODE_1);
305                 mxl_fail(err);
306         }
307         err = mxl111sf_init_port_expander(state);
308         if (!mxl_fail(err)) {
309                 state->gpio_mode = adap_state->gpio_mode;
310                 err = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
311                 mxl_fail(err);
312 #if 0
313                 err = fe->ops.init(fe);
314 #endif
315                 msleep(100); /* add short delay after enabling
316                               * the demod before touching it */
317         }
318
319         return (adap_state->fe_init) ? adap_state->fe_init(fe) : 0;
320 fail:
321         return -ENODEV;
322 }
323
324 static int mxl111sf_adap_fe_sleep(struct dvb_frontend *fe)
325 {
326         struct mxl111sf_state *state = fe_to_priv(fe);
327         struct mxl111sf_adap_state *adap_state = &state->adap_state[fe->id];
328         int err;
329
330         /* exit if we didn't initialize the driver yet */
331         if (!state->chip_id) {
332                 mxl_debug("driver not yet initialized, exit.");
333                 goto fail;
334         }
335
336         pr_debug("%s()\n", __func__);
337
338         err = (adap_state->fe_sleep) ? adap_state->fe_sleep(fe) : 0;
339
340         mutex_unlock(&state->fe_lock);
341
342         return err;
343 fail:
344         return -ENODEV;
345 }
346
347
348 static int mxl111sf_ep6_streaming_ctrl(struct dvb_frontend *fe, int onoff)
349 {
350         struct mxl111sf_state *state = fe_to_priv(fe);
351         struct mxl111sf_adap_state *adap_state = &state->adap_state[fe->id];
352         int ret = 0;
353
354         pr_debug("%s(%d)\n", __func__, onoff);
355
356         if (onoff) {
357                 ret = mxl111sf_enable_usb_output(state);
358                 mxl_fail(ret);
359                 ret = mxl111sf_config_mpeg_in(state, 1, 1,
360                                               adap_state->ep6_clockphase,
361                                               0, 0);
362                 mxl_fail(ret);
363 #if 0
364         } else {
365                 ret = mxl111sf_disable_656_port(state);
366                 mxl_fail(ret);
367 #endif
368         }
369
370         return ret;
371 }
372
373 static int mxl111sf_ep5_streaming_ctrl(struct dvb_frontend *fe, int onoff)
374 {
375         struct mxl111sf_state *state = fe_to_priv(fe);
376         int ret = 0;
377
378         pr_debug("%s(%d)\n", __func__, onoff);
379
380         if (onoff) {
381                 ret = mxl111sf_enable_usb_output(state);
382                 mxl_fail(ret);
383
384                 ret = mxl111sf_init_i2s_port(state, 200);
385                 mxl_fail(ret);
386                 ret = mxl111sf_config_i2s(state, 0, 15);
387                 mxl_fail(ret);
388         } else {
389                 ret = mxl111sf_disable_i2s_port(state);
390                 mxl_fail(ret);
391         }
392         if (state->chip_rev > MXL111SF_V6)
393                 ret = mxl111sf_config_spi(state, onoff);
394         mxl_fail(ret);
395
396         return ret;
397 }
398
399 static int mxl111sf_ep4_streaming_ctrl(struct dvb_frontend *fe, int onoff)
400 {
401         struct mxl111sf_state *state = fe_to_priv(fe);
402         int ret = 0;
403
404         pr_debug("%s(%d)\n", __func__, onoff);
405
406         if (onoff) {
407                 ret = mxl111sf_enable_usb_output(state);
408                 mxl_fail(ret);
409         }
410
411         return ret;
412 }
413
414 /* ------------------------------------------------------------------------ */
415
416 static struct lgdt3305_config hauppauge_lgdt3305_config = {
417         .i2c_addr           = 0xb2 >> 1,
418         .mpeg_mode          = LGDT3305_MPEG_SERIAL,
419         .tpclk_edge         = LGDT3305_TPCLK_RISING_EDGE,
420         .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
421         .deny_i2c_rptr      = 1,
422         .spectral_inversion = 0,
423         .qam_if_khz         = 6000,
424         .vsb_if_khz         = 6000,
425 };
426
427 static int mxl111sf_lgdt3305_frontend_attach(struct dvb_usb_adapter *adap, u8 fe_id)
428 {
429         struct dvb_usb_device *d = adap_to_d(adap);
430         struct mxl111sf_state *state = d_to_priv(d);
431         struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
432         int ret;
433
434         pr_debug("%s()\n", __func__);
435
436         /* save a pointer to the dvb_usb_device in device state */
437         state->d = d;
438         adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 2 : 1;
439         state->alt_mode = adap_state->alt_mode;
440
441         if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
442                 pr_err("set interface failed");
443
444         state->gpio_mode = MXL111SF_GPIO_MOD_ATSC;
445         adap_state->gpio_mode = state->gpio_mode;
446         adap_state->device_mode = MXL_TUNER_MODE;
447         adap_state->ep6_clockphase = 1;
448
449         ret = mxl1x1sf_soft_reset(state);
450         if (mxl_fail(ret))
451                 goto fail;
452         ret = mxl111sf_init_tuner_demod(state);
453         if (mxl_fail(ret))
454                 goto fail;
455
456         ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
457         if (mxl_fail(ret))
458                 goto fail;
459
460         ret = mxl111sf_enable_usb_output(state);
461         if (mxl_fail(ret))
462                 goto fail;
463         ret = mxl1x1sf_top_master_ctrl(state, 1);
464         if (mxl_fail(ret))
465                 goto fail;
466
467         ret = mxl111sf_init_port_expander(state);
468         if (mxl_fail(ret))
469                 goto fail;
470         ret = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
471         if (mxl_fail(ret))
472                 goto fail;
473
474         adap->fe[fe_id] = dvb_attach(lgdt3305_attach,
475                                  &hauppauge_lgdt3305_config,
476                                  &d->i2c_adap);
477         if (adap->fe[fe_id]) {
478                 state->num_frontends++;
479                 adap_state->fe_init = adap->fe[fe_id]->ops.init;
480                 adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
481                 adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
482                 adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
483                 return 0;
484         }
485         ret = -EIO;
486 fail:
487         return ret;
488 }
489
490 static struct lg2160_config hauppauge_lg2160_config = {
491         .lg_chip            = LG2160,
492         .i2c_addr           = 0x1c >> 1,
493         .deny_i2c_rptr      = 1,
494         .spectral_inversion = 0,
495         .if_khz             = 6000,
496 };
497
498 static int mxl111sf_lg2160_frontend_attach(struct dvb_usb_adapter *adap, u8 fe_id)
499 {
500         struct dvb_usb_device *d = adap_to_d(adap);
501         struct mxl111sf_state *state = d_to_priv(d);
502         struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
503         int ret;
504
505         pr_debug("%s()\n", __func__);
506
507         /* save a pointer to the dvb_usb_device in device state */
508         state->d = d;
509         adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 2 : 1;
510         state->alt_mode = adap_state->alt_mode;
511
512         if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
513                 pr_err("set interface failed");
514
515         state->gpio_mode = MXL111SF_GPIO_MOD_MH;
516         adap_state->gpio_mode = state->gpio_mode;
517         adap_state->device_mode = MXL_TUNER_MODE;
518         adap_state->ep6_clockphase = 1;
519
520         ret = mxl1x1sf_soft_reset(state);
521         if (mxl_fail(ret))
522                 goto fail;
523         ret = mxl111sf_init_tuner_demod(state);
524         if (mxl_fail(ret))
525                 goto fail;
526
527         ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
528         if (mxl_fail(ret))
529                 goto fail;
530
531         ret = mxl111sf_enable_usb_output(state);
532         if (mxl_fail(ret))
533                 goto fail;
534         ret = mxl1x1sf_top_master_ctrl(state, 1);
535         if (mxl_fail(ret))
536                 goto fail;
537
538         ret = mxl111sf_init_port_expander(state);
539         if (mxl_fail(ret))
540                 goto fail;
541         ret = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
542         if (mxl_fail(ret))
543                 goto fail;
544
545         ret = get_chip_info(state);
546         if (mxl_fail(ret))
547                 goto fail;
548
549         adap->fe[fe_id] = dvb_attach(lg2160_attach,
550                               &hauppauge_lg2160_config,
551                               &d->i2c_adap);
552         if (adap->fe[fe_id]) {
553                 state->num_frontends++;
554                 adap_state->fe_init = adap->fe[fe_id]->ops.init;
555                 adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
556                 adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
557                 adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
558                 return 0;
559         }
560         ret = -EIO;
561 fail:
562         return ret;
563 }
564
565 static struct lg2160_config hauppauge_lg2161_1019_config = {
566         .lg_chip            = LG2161_1019,
567         .i2c_addr           = 0x1c >> 1,
568         .deny_i2c_rptr      = 1,
569         .spectral_inversion = 0,
570         .if_khz             = 6000,
571         .output_if          = 2, /* LG2161_OIF_SPI_MAS */
572 };
573
574 static struct lg2160_config hauppauge_lg2161_1040_config = {
575         .lg_chip            = LG2161_1040,
576         .i2c_addr           = 0x1c >> 1,
577         .deny_i2c_rptr      = 1,
578         .spectral_inversion = 0,
579         .if_khz             = 6000,
580         .output_if          = 4, /* LG2161_OIF_SPI_MAS */
581 };
582
583 static int mxl111sf_lg2161_frontend_attach(struct dvb_usb_adapter *adap, u8 fe_id)
584 {
585         struct dvb_usb_device *d = adap_to_d(adap);
586         struct mxl111sf_state *state = d_to_priv(d);
587         struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
588         int ret;
589
590         pr_debug("%s()\n", __func__);
591
592         /* save a pointer to the dvb_usb_device in device state */
593         state->d = d;
594         adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 2 : 1;
595         state->alt_mode = adap_state->alt_mode;
596
597         if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
598                 pr_err("set interface failed");
599
600         state->gpio_mode = MXL111SF_GPIO_MOD_MH;
601         adap_state->gpio_mode = state->gpio_mode;
602         adap_state->device_mode = MXL_TUNER_MODE;
603         adap_state->ep6_clockphase = 1;
604
605         ret = mxl1x1sf_soft_reset(state);
606         if (mxl_fail(ret))
607                 goto fail;
608         ret = mxl111sf_init_tuner_demod(state);
609         if (mxl_fail(ret))
610                 goto fail;
611
612         ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
613         if (mxl_fail(ret))
614                 goto fail;
615
616         ret = mxl111sf_enable_usb_output(state);
617         if (mxl_fail(ret))
618                 goto fail;
619         ret = mxl1x1sf_top_master_ctrl(state, 1);
620         if (mxl_fail(ret))
621                 goto fail;
622
623         ret = mxl111sf_init_port_expander(state);
624         if (mxl_fail(ret))
625                 goto fail;
626         ret = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
627         if (mxl_fail(ret))
628                 goto fail;
629
630         ret = get_chip_info(state);
631         if (mxl_fail(ret))
632                 goto fail;
633
634         adap->fe[fe_id] = dvb_attach(lg2160_attach,
635                               (MXL111SF_V8_200 == state->chip_rev) ?
636                               &hauppauge_lg2161_1040_config :
637                               &hauppauge_lg2161_1019_config,
638                               &d->i2c_adap);
639         if (adap->fe[fe_id]) {
640                 state->num_frontends++;
641                 adap_state->fe_init = adap->fe[fe_id]->ops.init;
642                 adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
643                 adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
644                 adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
645                 return 0;
646         }
647         ret = -EIO;
648 fail:
649         return ret;
650 }
651
652 static struct lg2160_config hauppauge_lg2161_1019_ep6_config = {
653         .lg_chip            = LG2161_1019,
654         .i2c_addr           = 0x1c >> 1,
655         .deny_i2c_rptr      = 1,
656         .spectral_inversion = 0,
657         .if_khz             = 6000,
658         .output_if          = 1, /* LG2161_OIF_SERIAL_TS */
659 };
660
661 static struct lg2160_config hauppauge_lg2161_1040_ep6_config = {
662         .lg_chip            = LG2161_1040,
663         .i2c_addr           = 0x1c >> 1,
664         .deny_i2c_rptr      = 1,
665         .spectral_inversion = 0,
666         .if_khz             = 6000,
667         .output_if          = 7, /* LG2161_OIF_SERIAL_TS */
668 };
669
670 static int mxl111sf_lg2161_ep6_frontend_attach(struct dvb_usb_adapter *adap, u8 fe_id)
671 {
672         struct dvb_usb_device *d = adap_to_d(adap);
673         struct mxl111sf_state *state = d_to_priv(d);
674         struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
675         int ret;
676
677         pr_debug("%s()\n", __func__);
678
679         /* save a pointer to the dvb_usb_device in device state */
680         state->d = d;
681         adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 2 : 1;
682         state->alt_mode = adap_state->alt_mode;
683
684         if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
685                 pr_err("set interface failed");
686
687         state->gpio_mode = MXL111SF_GPIO_MOD_MH;
688         adap_state->gpio_mode = state->gpio_mode;
689         adap_state->device_mode = MXL_TUNER_MODE;
690         adap_state->ep6_clockphase = 0;
691
692         ret = mxl1x1sf_soft_reset(state);
693         if (mxl_fail(ret))
694                 goto fail;
695         ret = mxl111sf_init_tuner_demod(state);
696         if (mxl_fail(ret))
697                 goto fail;
698
699         ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
700         if (mxl_fail(ret))
701                 goto fail;
702
703         ret = mxl111sf_enable_usb_output(state);
704         if (mxl_fail(ret))
705                 goto fail;
706         ret = mxl1x1sf_top_master_ctrl(state, 1);
707         if (mxl_fail(ret))
708                 goto fail;
709
710         ret = mxl111sf_init_port_expander(state);
711         if (mxl_fail(ret))
712                 goto fail;
713         ret = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
714         if (mxl_fail(ret))
715                 goto fail;
716
717         ret = get_chip_info(state);
718         if (mxl_fail(ret))
719                 goto fail;
720
721         adap->fe[fe_id] = dvb_attach(lg2160_attach,
722                               (MXL111SF_V8_200 == state->chip_rev) ?
723                               &hauppauge_lg2161_1040_ep6_config :
724                               &hauppauge_lg2161_1019_ep6_config,
725                               &d->i2c_adap);
726         if (adap->fe[fe_id]) {
727                 state->num_frontends++;
728                 adap_state->fe_init = adap->fe[fe_id]->ops.init;
729                 adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
730                 adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
731                 adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
732                 return 0;
733         }
734         ret = -EIO;
735 fail:
736         return ret;
737 }
738
739 static const struct mxl111sf_demod_config mxl_demod_config = {
740         .read_reg        = mxl111sf_read_reg,
741         .write_reg       = mxl111sf_write_reg,
742         .program_regs    = mxl111sf_ctrl_program_regs,
743 };
744
745 static int mxl111sf_attach_demod(struct dvb_usb_adapter *adap, u8 fe_id)
746 {
747         struct dvb_usb_device *d = adap_to_d(adap);
748         struct mxl111sf_state *state = d_to_priv(d);
749         struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
750         int ret;
751
752         pr_debug("%s()\n", __func__);
753
754         /* save a pointer to the dvb_usb_device in device state */
755         state->d = d;
756         adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 1 : 2;
757         state->alt_mode = adap_state->alt_mode;
758
759         if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
760                 pr_err("set interface failed");
761
762         state->gpio_mode = MXL111SF_GPIO_MOD_DVBT;
763         adap_state->gpio_mode = state->gpio_mode;
764         adap_state->device_mode = MXL_SOC_MODE;
765         adap_state->ep6_clockphase = 1;
766
767         ret = mxl1x1sf_soft_reset(state);
768         if (mxl_fail(ret))
769                 goto fail;
770         ret = mxl111sf_init_tuner_demod(state);
771         if (mxl_fail(ret))
772                 goto fail;
773
774         ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
775         if (mxl_fail(ret))
776                 goto fail;
777
778         ret = mxl111sf_enable_usb_output(state);
779         if (mxl_fail(ret))
780                 goto fail;
781         ret = mxl1x1sf_top_master_ctrl(state, 1);
782         if (mxl_fail(ret))
783                 goto fail;
784
785         /* dont care if this fails */
786         mxl111sf_init_port_expander(state);
787
788         adap->fe[fe_id] = dvb_attach(mxl111sf_demod_attach, state,
789                               &mxl_demod_config);
790         if (adap->fe[fe_id]) {
791                 state->num_frontends++;
792                 adap_state->fe_init = adap->fe[fe_id]->ops.init;
793                 adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
794                 adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
795                 adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
796                 return 0;
797         }
798         ret = -EIO;
799 fail:
800         return ret;
801 }
802
803 static inline int mxl111sf_set_ant_path(struct mxl111sf_state *state,
804                                         int antpath)
805 {
806         return mxl111sf_idac_config(state, 1, 1,
807                                     (antpath == ANT_PATH_INTERNAL) ?
808                                     0x3f : 0x00, 0);
809 }
810
811 #define DbgAntHunt(x, pwr0, pwr1, pwr2, pwr3) \
812         pr_err("%s(%d) FINAL input set to %s rxPwr:%d|%d|%d|%d\n", \
813             __func__, __LINE__, \
814             (ANT_PATH_EXTERNAL == x) ? "EXTERNAL" : "INTERNAL", \
815             pwr0, pwr1, pwr2, pwr3)
816
817 #define ANT_HUNT_SLEEP 90
818 #define ANT_EXT_TWEAK 0
819
820 static int mxl111sf_ant_hunt(struct dvb_frontend *fe)
821 {
822         struct mxl111sf_state *state = fe_to_priv(fe);
823         int antctrl = dvb_usb_mxl111sf_rfswitch;
824
825         u16 rxPwrA, rxPwr0, rxPwr1, rxPwr2;
826
827         /* FIXME: must force EXTERNAL for QAM - done elsewhere */
828         mxl111sf_set_ant_path(state, antctrl == ANT_PATH_AUTO ?
829                               ANT_PATH_EXTERNAL : antctrl);
830
831         if (antctrl == ANT_PATH_AUTO) {
832 #if 0
833                 msleep(ANT_HUNT_SLEEP);
834 #endif
835                 fe->ops.tuner_ops.get_rf_strength(fe, &rxPwrA);
836
837                 mxl111sf_set_ant_path(state, ANT_PATH_EXTERNAL);
838                 msleep(ANT_HUNT_SLEEP);
839                 fe->ops.tuner_ops.get_rf_strength(fe, &rxPwr0);
840
841                 mxl111sf_set_ant_path(state, ANT_PATH_EXTERNAL);
842                 msleep(ANT_HUNT_SLEEP);
843                 fe->ops.tuner_ops.get_rf_strength(fe, &rxPwr1);
844
845                 mxl111sf_set_ant_path(state, ANT_PATH_INTERNAL);
846                 msleep(ANT_HUNT_SLEEP);
847                 fe->ops.tuner_ops.get_rf_strength(fe, &rxPwr2);
848
849                 if (rxPwr1+ANT_EXT_TWEAK >= rxPwr2) {
850                         /* return with EXTERNAL enabled */
851                         mxl111sf_set_ant_path(state, ANT_PATH_EXTERNAL);
852                         DbgAntHunt(ANT_PATH_EXTERNAL, rxPwrA,
853                                    rxPwr0, rxPwr1, rxPwr2);
854                 } else {
855                         /* return with INTERNAL enabled */
856                         DbgAntHunt(ANT_PATH_INTERNAL, rxPwrA,
857                                    rxPwr0, rxPwr1, rxPwr2);
858                 }
859         }
860         return 0;
861 }
862
863 static const struct mxl111sf_tuner_config mxl_tuner_config = {
864         .if_freq         = MXL_IF_6_0, /* applies to external IF output, only */
865         .invert_spectrum = 0,
866         .read_reg        = mxl111sf_read_reg,
867         .write_reg       = mxl111sf_write_reg,
868         .program_regs    = mxl111sf_ctrl_program_regs,
869         .top_master_ctrl = mxl1x1sf_top_master_ctrl,
870         .ant_hunt        = mxl111sf_ant_hunt,
871 };
872
873 static int mxl111sf_attach_tuner(struct dvb_usb_adapter *adap)
874 {
875         struct mxl111sf_state *state = adap_to_priv(adap);
876 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
877         struct media_device *mdev = dvb_get_media_controller(&adap->dvb_adap);
878         int ret;
879 #endif
880         int i;
881
882         pr_debug("%s()\n", __func__);
883
884         for (i = 0; i < state->num_frontends; i++) {
885                 if (dvb_attach(mxl111sf_tuner_attach, adap->fe[i], state,
886                                 &mxl_tuner_config) == NULL)
887                         return -EIO;
888                 adap->fe[i]->ops.read_signal_strength = adap->fe[i]->ops.tuner_ops.get_rf_strength;
889         }
890
891 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
892         state->tuner.function = MEDIA_ENT_F_TUNER;
893         state->tuner.name = "mxl111sf tuner";
894         state->tuner_pads[TUNER_PAD_RF_INPUT].flags = MEDIA_PAD_FL_SINK;
895         state->tuner_pads[TUNER_PAD_OUTPUT].flags = MEDIA_PAD_FL_SOURCE;
896
897         ret = media_entity_pads_init(&state->tuner,
898                                      TUNER_NUM_PADS, state->tuner_pads);
899         if (ret)
900                 return ret;
901
902         ret = media_device_register_entity(mdev, &state->tuner);
903         if (ret)
904                 return ret;
905 #endif
906         return 0;
907 }
908
909 static u32 mxl111sf_i2c_func(struct i2c_adapter *adapter)
910 {
911         return I2C_FUNC_I2C;
912 }
913
914 static struct i2c_algorithm mxl111sf_i2c_algo = {
915         .master_xfer   = mxl111sf_i2c_xfer,
916         .functionality = mxl111sf_i2c_func,
917 #ifdef NEED_ALGO_CONTROL
918         .algo_control = dummy_algo_control,
919 #endif
920 };
921
922 static int mxl111sf_init(struct dvb_usb_device *d)
923 {
924         struct mxl111sf_state *state = d_to_priv(d);
925         int ret;
926         static u8 eeprom[256];
927         struct i2c_client c;
928
929         mutex_init(&state->msg_lock);
930
931         ret = get_chip_info(state);
932         if (mxl_fail(ret))
933                 pr_err("failed to get chip info during probe");
934
935         mutex_init(&state->fe_lock);
936
937         if (state->chip_rev > MXL111SF_V6)
938                 mxl111sf_config_pin_mux_modes(state, PIN_MUX_TS_SPI_IN_MODE_1);
939
940         c.adapter = &d->i2c_adap;
941         c.addr = 0xa0 >> 1;
942
943         ret = tveeprom_read(&c, eeprom, sizeof(eeprom));
944         if (mxl_fail(ret))
945                 return 0;
946         tveeprom_hauppauge_analog(&c, &state->tv, (0x84 == eeprom[0xa0]) ?
947                         eeprom + 0xa0 : eeprom + 0x80);
948 #if 0
949         switch (state->tv.model) {
950         case 117001:
951         case 126001:
952         case 138001:
953                 break;
954         default:
955                 printk(KERN_WARNING "%s: warning: "
956                        "unknown hauppauge model #%d\n",
957                        __func__, state->tv.model);
958         }
959 #endif
960         return 0;
961 }
962
963 static int mxl111sf_frontend_attach_dvbt(struct dvb_usb_adapter *adap)
964 {
965         return mxl111sf_attach_demod(adap, 0);
966 }
967
968 static int mxl111sf_frontend_attach_atsc(struct dvb_usb_adapter *adap)
969 {
970         return mxl111sf_lgdt3305_frontend_attach(adap, 0);
971 }
972
973 static int mxl111sf_frontend_attach_mh(struct dvb_usb_adapter *adap)
974 {
975         return mxl111sf_lg2160_frontend_attach(adap, 0);
976 }
977
978 static int mxl111sf_frontend_attach_atsc_mh(struct dvb_usb_adapter *adap)
979 {
980         int ret;
981         pr_debug("%s\n", __func__);
982
983         ret = mxl111sf_lgdt3305_frontend_attach(adap, 0);
984         if (ret < 0)
985                 return ret;
986
987         ret = mxl111sf_attach_demod(adap, 1);
988         if (ret < 0)
989                 return ret;
990
991         ret = mxl111sf_lg2160_frontend_attach(adap, 2);
992         if (ret < 0)
993                 return ret;
994
995         return ret;
996 }
997
998 static int mxl111sf_frontend_attach_mercury(struct dvb_usb_adapter *adap)
999 {
1000         int ret;
1001         pr_debug("%s\n", __func__);
1002
1003         ret = mxl111sf_lgdt3305_frontend_attach(adap, 0);
1004         if (ret < 0)
1005                 return ret;
1006
1007         ret = mxl111sf_attach_demod(adap, 1);
1008         if (ret < 0)
1009                 return ret;
1010
1011         ret = mxl111sf_lg2161_ep6_frontend_attach(adap, 2);
1012         if (ret < 0)
1013                 return ret;
1014
1015         return ret;
1016 }
1017
1018 static int mxl111sf_frontend_attach_mercury_mh(struct dvb_usb_adapter *adap)
1019 {
1020         int ret;
1021         pr_debug("%s\n", __func__);
1022
1023         ret = mxl111sf_attach_demod(adap, 0);
1024         if (ret < 0)
1025                 return ret;
1026
1027         if (dvb_usb_mxl111sf_spi)
1028                 ret = mxl111sf_lg2161_frontend_attach(adap, 1);
1029         else
1030                 ret = mxl111sf_lg2161_ep6_frontend_attach(adap, 1);
1031
1032         return ret;
1033 }
1034
1035 static void mxl111sf_stream_config_bulk(struct usb_data_stream_properties *stream, u8 endpoint)
1036 {
1037         pr_debug("%s: endpoint=%d size=8192\n", __func__, endpoint);
1038         stream->type = USB_BULK;
1039         stream->count = 5;
1040         stream->endpoint = endpoint;
1041         stream->u.bulk.buffersize = 8192;
1042 }
1043
1044 static void mxl111sf_stream_config_isoc(struct usb_data_stream_properties *stream,
1045                 u8 endpoint, int framesperurb, int framesize)
1046 {
1047         pr_debug("%s: endpoint=%d size=%d\n", __func__, endpoint,
1048                         framesperurb * framesize);
1049         stream->type = USB_ISOC;
1050         stream->count = 5;
1051         stream->endpoint = endpoint;
1052         stream->u.isoc.framesperurb = framesperurb;
1053         stream->u.isoc.framesize = framesize;
1054         stream->u.isoc.interval = 1;
1055 }
1056
1057 /* DVB USB Driver stuff */
1058
1059 /* dvbt       mxl111sf
1060  * bulk       EP4/BULK/5/8192
1061  * isoc       EP4/ISOC/5/96/564
1062  */
1063 static int mxl111sf_get_stream_config_dvbt(struct dvb_frontend *fe,
1064                 u8 *ts_type, struct usb_data_stream_properties *stream)
1065 {
1066         pr_debug("%s: fe=%d\n", __func__, fe->id);
1067
1068         *ts_type = DVB_USB_FE_TS_TYPE_188;
1069         if (dvb_usb_mxl111sf_isoc)
1070                 mxl111sf_stream_config_isoc(stream, 4, 96, 564);
1071         else
1072                 mxl111sf_stream_config_bulk(stream, 4);
1073         return 0;
1074 }
1075
1076 static struct dvb_usb_device_properties mxl111sf_props_dvbt = {
1077         .driver_name = KBUILD_MODNAME,
1078         .owner = THIS_MODULE,
1079         .adapter_nr = adapter_nr,
1080         .size_of_priv = sizeof(struct mxl111sf_state),
1081
1082         .generic_bulk_ctrl_endpoint = 0x02,
1083         .generic_bulk_ctrl_endpoint_response = 0x81,
1084
1085         .i2c_algo          = &mxl111sf_i2c_algo,
1086         .frontend_attach   = mxl111sf_frontend_attach_dvbt,
1087         .tuner_attach      = mxl111sf_attach_tuner,
1088         .init              = mxl111sf_init,
1089         .streaming_ctrl    = mxl111sf_ep4_streaming_ctrl,
1090         .get_stream_config = mxl111sf_get_stream_config_dvbt,
1091
1092         .num_adapters = 1,
1093         .adapter = {
1094                 {
1095                         .stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1096                 }
1097         }
1098 };
1099
1100 /* atsc       lgdt3305
1101  * bulk       EP6/BULK/5/8192
1102  * isoc       EP6/ISOC/5/24/3072
1103  */
1104 static int mxl111sf_get_stream_config_atsc(struct dvb_frontend *fe,
1105                 u8 *ts_type, struct usb_data_stream_properties *stream)
1106 {
1107         pr_debug("%s: fe=%d\n", __func__, fe->id);
1108
1109         *ts_type = DVB_USB_FE_TS_TYPE_188;
1110         if (dvb_usb_mxl111sf_isoc)
1111                 mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1112         else
1113                 mxl111sf_stream_config_bulk(stream, 6);
1114         return 0;
1115 }
1116
1117 static struct dvb_usb_device_properties mxl111sf_props_atsc = {
1118         .driver_name = KBUILD_MODNAME,
1119         .owner = THIS_MODULE,
1120         .adapter_nr = adapter_nr,
1121         .size_of_priv = sizeof(struct mxl111sf_state),
1122
1123         .generic_bulk_ctrl_endpoint = 0x02,
1124         .generic_bulk_ctrl_endpoint_response = 0x81,
1125
1126         .i2c_algo          = &mxl111sf_i2c_algo,
1127         .frontend_attach   = mxl111sf_frontend_attach_atsc,
1128         .tuner_attach      = mxl111sf_attach_tuner,
1129         .init              = mxl111sf_init,
1130         .streaming_ctrl    = mxl111sf_ep6_streaming_ctrl,
1131         .get_stream_config = mxl111sf_get_stream_config_atsc,
1132
1133         .num_adapters = 1,
1134         .adapter = {
1135                 {
1136                         .stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1137                 }
1138         }
1139 };
1140
1141 /* mh         lg2160
1142  * bulk       EP5/BULK/5/8192/RAW
1143  * isoc       EP5/ISOC/5/96/200/RAW
1144  */
1145 static int mxl111sf_get_stream_config_mh(struct dvb_frontend *fe,
1146                 u8 *ts_type, struct usb_data_stream_properties *stream)
1147 {
1148         pr_debug("%s: fe=%d\n", __func__, fe->id);
1149
1150         *ts_type = DVB_USB_FE_TS_TYPE_RAW;
1151         if (dvb_usb_mxl111sf_isoc)
1152                 mxl111sf_stream_config_isoc(stream, 5, 96, 200);
1153         else
1154                 mxl111sf_stream_config_bulk(stream, 5);
1155         return 0;
1156 }
1157
1158 static struct dvb_usb_device_properties mxl111sf_props_mh = {
1159         .driver_name = KBUILD_MODNAME,
1160         .owner = THIS_MODULE,
1161         .adapter_nr = adapter_nr,
1162         .size_of_priv = sizeof(struct mxl111sf_state),
1163
1164         .generic_bulk_ctrl_endpoint = 0x02,
1165         .generic_bulk_ctrl_endpoint_response = 0x81,
1166
1167         .i2c_algo          = &mxl111sf_i2c_algo,
1168         .frontend_attach   = mxl111sf_frontend_attach_mh,
1169         .tuner_attach      = mxl111sf_attach_tuner,
1170         .init              = mxl111sf_init,
1171         .streaming_ctrl    = mxl111sf_ep5_streaming_ctrl,
1172         .get_stream_config = mxl111sf_get_stream_config_mh,
1173
1174         .num_adapters = 1,
1175         .adapter = {
1176                 {
1177                         .stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1178                 }
1179         }
1180 };
1181
1182 /* atsc mh    lgdt3305           mxl111sf          lg2160
1183  * bulk       EP6/BULK/5/8192    EP4/BULK/5/8192   EP5/BULK/5/8192/RAW
1184  * isoc       EP6/ISOC/5/24/3072 EP4/ISOC/5/96/564 EP5/ISOC/5/96/200/RAW
1185  */
1186 static int mxl111sf_get_stream_config_atsc_mh(struct dvb_frontend *fe,
1187                 u8 *ts_type, struct usb_data_stream_properties *stream)
1188 {
1189         pr_debug("%s: fe=%d\n", __func__, fe->id);
1190
1191         if (fe->id == 0) {
1192                 *ts_type = DVB_USB_FE_TS_TYPE_188;
1193                 if (dvb_usb_mxl111sf_isoc)
1194                         mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1195                 else
1196                         mxl111sf_stream_config_bulk(stream, 6);
1197         } else if (fe->id == 1) {
1198                 *ts_type = DVB_USB_FE_TS_TYPE_188;
1199                 if (dvb_usb_mxl111sf_isoc)
1200                         mxl111sf_stream_config_isoc(stream, 4, 96, 564);
1201                 else
1202                         mxl111sf_stream_config_bulk(stream, 4);
1203         } else if (fe->id == 2) {
1204                 *ts_type = DVB_USB_FE_TS_TYPE_RAW;
1205                 if (dvb_usb_mxl111sf_isoc)
1206                         mxl111sf_stream_config_isoc(stream, 5, 96, 200);
1207                 else
1208                         mxl111sf_stream_config_bulk(stream, 5);
1209         }
1210         return 0;
1211 }
1212
1213 static int mxl111sf_streaming_ctrl_atsc_mh(struct dvb_frontend *fe, int onoff)
1214 {
1215         pr_debug("%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
1216
1217         if (fe->id == 0)
1218                 return mxl111sf_ep6_streaming_ctrl(fe, onoff);
1219         else if (fe->id == 1)
1220                 return mxl111sf_ep4_streaming_ctrl(fe, onoff);
1221         else if (fe->id == 2)
1222                 return mxl111sf_ep5_streaming_ctrl(fe, onoff);
1223         return 0;
1224 }
1225
1226 static struct dvb_usb_device_properties mxl111sf_props_atsc_mh = {
1227         .driver_name = KBUILD_MODNAME,
1228         .owner = THIS_MODULE,
1229         .adapter_nr = adapter_nr,
1230         .size_of_priv = sizeof(struct mxl111sf_state),
1231
1232         .generic_bulk_ctrl_endpoint = 0x02,
1233         .generic_bulk_ctrl_endpoint_response = 0x81,
1234
1235         .i2c_algo          = &mxl111sf_i2c_algo,
1236         .frontend_attach   = mxl111sf_frontend_attach_atsc_mh,
1237         .tuner_attach      = mxl111sf_attach_tuner,
1238         .init              = mxl111sf_init,
1239         .streaming_ctrl    = mxl111sf_streaming_ctrl_atsc_mh,
1240         .get_stream_config = mxl111sf_get_stream_config_atsc_mh,
1241
1242         .num_adapters = 1,
1243         .adapter = {
1244                 {
1245                         .stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1246                 }
1247         }
1248 };
1249
1250 /* mercury    lgdt3305           mxl111sf          lg2161
1251  * tp bulk    EP6/BULK/5/8192    EP4/BULK/5/8192   EP6/BULK/5/8192/RAW
1252  * tp isoc    EP6/ISOC/5/24/3072 EP4/ISOC/5/96/564 EP6/ISOC/5/24/3072/RAW
1253  * spi bulk   EP6/BULK/5/8192    EP4/BULK/5/8192   EP5/BULK/5/8192/RAW
1254  * spi isoc   EP6/ISOC/5/24/3072 EP4/ISOC/5/96/564 EP5/ISOC/5/96/200/RAW
1255  */
1256 static int mxl111sf_get_stream_config_mercury(struct dvb_frontend *fe,
1257                 u8 *ts_type, struct usb_data_stream_properties *stream)
1258 {
1259         pr_debug("%s: fe=%d\n", __func__, fe->id);
1260
1261         if (fe->id == 0) {
1262                 *ts_type = DVB_USB_FE_TS_TYPE_188;
1263                 if (dvb_usb_mxl111sf_isoc)
1264                         mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1265                 else
1266                         mxl111sf_stream_config_bulk(stream, 6);
1267         } else if (fe->id == 1) {
1268                 *ts_type = DVB_USB_FE_TS_TYPE_188;
1269                 if (dvb_usb_mxl111sf_isoc)
1270                         mxl111sf_stream_config_isoc(stream, 4, 96, 564);
1271                 else
1272                         mxl111sf_stream_config_bulk(stream, 4);
1273         } else if (fe->id == 2 && dvb_usb_mxl111sf_spi) {
1274                 *ts_type = DVB_USB_FE_TS_TYPE_RAW;
1275                 if (dvb_usb_mxl111sf_isoc)
1276                         mxl111sf_stream_config_isoc(stream, 5, 96, 200);
1277                 else
1278                         mxl111sf_stream_config_bulk(stream, 5);
1279         } else if (fe->id == 2 && !dvb_usb_mxl111sf_spi) {
1280                 *ts_type = DVB_USB_FE_TS_TYPE_RAW;
1281                 if (dvb_usb_mxl111sf_isoc)
1282                         mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1283                 else
1284                         mxl111sf_stream_config_bulk(stream, 6);
1285         }
1286         return 0;
1287 }
1288
1289 static int mxl111sf_streaming_ctrl_mercury(struct dvb_frontend *fe, int onoff)
1290 {
1291         pr_debug("%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
1292
1293         if (fe->id == 0)
1294                 return mxl111sf_ep6_streaming_ctrl(fe, onoff);
1295         else if (fe->id == 1)
1296                 return mxl111sf_ep4_streaming_ctrl(fe, onoff);
1297         else if (fe->id == 2 && dvb_usb_mxl111sf_spi)
1298                 return mxl111sf_ep5_streaming_ctrl(fe, onoff);
1299         else if (fe->id == 2 && !dvb_usb_mxl111sf_spi)
1300                 return mxl111sf_ep6_streaming_ctrl(fe, onoff);
1301         return 0;
1302 }
1303
1304 static struct dvb_usb_device_properties mxl111sf_props_mercury = {
1305         .driver_name = KBUILD_MODNAME,
1306         .owner = THIS_MODULE,
1307         .adapter_nr = adapter_nr,
1308         .size_of_priv = sizeof(struct mxl111sf_state),
1309
1310         .generic_bulk_ctrl_endpoint = 0x02,
1311         .generic_bulk_ctrl_endpoint_response = 0x81,
1312
1313         .i2c_algo          = &mxl111sf_i2c_algo,
1314         .frontend_attach   = mxl111sf_frontend_attach_mercury,
1315         .tuner_attach      = mxl111sf_attach_tuner,
1316         .init              = mxl111sf_init,
1317         .streaming_ctrl    = mxl111sf_streaming_ctrl_mercury,
1318         .get_stream_config = mxl111sf_get_stream_config_mercury,
1319
1320         .num_adapters = 1,
1321         .adapter = {
1322                 {
1323                         .stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1324                 }
1325         }
1326 };
1327
1328 /* mercury mh mxl111sf          lg2161
1329  * tp bulk    EP4/BULK/5/8192   EP6/BULK/5/8192/RAW
1330  * tp isoc    EP4/ISOC/5/96/564 EP6/ISOC/5/24/3072/RAW
1331  * spi bulk   EP4/BULK/5/8192   EP5/BULK/5/8192/RAW
1332  * spi isoc   EP4/ISOC/5/96/564 EP5/ISOC/5/96/200/RAW
1333  */
1334 static int mxl111sf_get_stream_config_mercury_mh(struct dvb_frontend *fe,
1335                 u8 *ts_type, struct usb_data_stream_properties *stream)
1336 {
1337         pr_debug("%s: fe=%d\n", __func__, fe->id);
1338
1339         if (fe->id == 0) {
1340                 *ts_type = DVB_USB_FE_TS_TYPE_188;
1341                 if (dvb_usb_mxl111sf_isoc)
1342                         mxl111sf_stream_config_isoc(stream, 4, 96, 564);
1343                 else
1344                         mxl111sf_stream_config_bulk(stream, 4);
1345         } else if (fe->id == 1 && dvb_usb_mxl111sf_spi) {
1346                 *ts_type = DVB_USB_FE_TS_TYPE_RAW;
1347                 if (dvb_usb_mxl111sf_isoc)
1348                         mxl111sf_stream_config_isoc(stream, 5, 96, 200);
1349                 else
1350                         mxl111sf_stream_config_bulk(stream, 5);
1351         } else if (fe->id == 1 && !dvb_usb_mxl111sf_spi) {
1352                 *ts_type = DVB_USB_FE_TS_TYPE_RAW;
1353                 if (dvb_usb_mxl111sf_isoc)
1354                         mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1355                 else
1356                         mxl111sf_stream_config_bulk(stream, 6);
1357         }
1358         return 0;
1359 }
1360
1361 static int mxl111sf_streaming_ctrl_mercury_mh(struct dvb_frontend *fe, int onoff)
1362 {
1363         pr_debug("%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
1364
1365         if (fe->id == 0)
1366                 return mxl111sf_ep4_streaming_ctrl(fe, onoff);
1367         else if (fe->id == 1  && dvb_usb_mxl111sf_spi)
1368                 return mxl111sf_ep5_streaming_ctrl(fe, onoff);
1369         else if (fe->id == 1 && !dvb_usb_mxl111sf_spi)
1370                 return mxl111sf_ep6_streaming_ctrl(fe, onoff);
1371         return 0;
1372 }
1373
1374 static struct dvb_usb_device_properties mxl111sf_props_mercury_mh = {
1375         .driver_name = KBUILD_MODNAME,
1376         .owner = THIS_MODULE,
1377         .adapter_nr = adapter_nr,
1378         .size_of_priv = sizeof(struct mxl111sf_state),
1379
1380         .generic_bulk_ctrl_endpoint = 0x02,
1381         .generic_bulk_ctrl_endpoint_response = 0x81,
1382
1383         .i2c_algo          = &mxl111sf_i2c_algo,
1384         .frontend_attach   = mxl111sf_frontend_attach_mercury_mh,
1385         .tuner_attach      = mxl111sf_attach_tuner,
1386         .init              = mxl111sf_init,
1387         .streaming_ctrl    = mxl111sf_streaming_ctrl_mercury_mh,
1388         .get_stream_config = mxl111sf_get_stream_config_mercury_mh,
1389
1390         .num_adapters = 1,
1391         .adapter = {
1392                 {
1393                         .stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1394                 }
1395         }
1396 };
1397
1398 static const struct usb_device_id mxl111sf_id_table[] = {
1399         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc600, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1400         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc601, &mxl111sf_props_atsc, "Hauppauge 126xxx ATSC", NULL) },
1401         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc602, &mxl111sf_props_mh, "HCW 126xxx", NULL) },
1402         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc603, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1403         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc604, &mxl111sf_props_dvbt, "Hauppauge 126xxx DVBT", NULL) },
1404         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc609, &mxl111sf_props_atsc, "Hauppauge 126xxx ATSC", NULL) },
1405         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc60a, &mxl111sf_props_mh, "HCW 126xxx", NULL) },
1406         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc60b, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1407         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc60c, &mxl111sf_props_dvbt, "Hauppauge 126xxx DVBT", NULL) },
1408         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc653, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1409         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc65b, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1410         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb700, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1411         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb701, &mxl111sf_props_atsc, "Hauppauge 126xxx ATSC", NULL) },
1412         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb702, &mxl111sf_props_mh, "HCW 117xxx", NULL) },
1413         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb703, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1414         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb704, &mxl111sf_props_dvbt, "Hauppauge 117xxx DVBT", NULL) },
1415         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb753, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1416         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb763, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1417         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb764, &mxl111sf_props_dvbt, "Hauppauge 117xxx DVBT", NULL) },
1418         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd853, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1419         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd854, &mxl111sf_props_dvbt, "Hauppauge 138xxx DVBT", NULL) },
1420         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd863, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1421         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd864, &mxl111sf_props_dvbt, "Hauppauge 138xxx DVBT", NULL) },
1422         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8d3, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1423         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8d4, &mxl111sf_props_dvbt, "Hauppauge 138xxx DVBT", NULL) },
1424         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8e3, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1425         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8e4, &mxl111sf_props_dvbt, "Hauppauge 138xxx DVBT", NULL) },
1426         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8ff, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1427         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc612, &mxl111sf_props_mercury_mh, "Hauppauge 126xxx", NULL) },
1428         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc613, &mxl111sf_props_mercury, "Hauppauge WinTV-Aero-M", NULL) },
1429         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc61a, &mxl111sf_props_mercury_mh, "Hauppauge 126xxx", NULL) },
1430         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc61b, &mxl111sf_props_mercury, "Hauppauge WinTV-Aero-M", NULL) },
1431         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb757, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1432         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb767, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1433         { }
1434 };
1435 MODULE_DEVICE_TABLE(usb, mxl111sf_id_table);
1436
1437 static struct usb_driver mxl111sf_usb_driver = {
1438         .name = KBUILD_MODNAME,
1439         .id_table = mxl111sf_id_table,
1440         .probe = dvb_usbv2_probe,
1441         .disconnect = dvb_usbv2_disconnect,
1442         .suspend = dvb_usbv2_suspend,
1443         .resume = dvb_usbv2_resume,
1444         .no_dynamic_id = 1,
1445         .soft_unbind = 1,
1446 };
1447
1448 module_usb_driver(mxl111sf_usb_driver);
1449
1450 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1451 MODULE_DESCRIPTION("Driver for MaxLinear MxL111SF");
1452 MODULE_VERSION("1.0");
1453 MODULE_LICENSE("GPL");