Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2010-2014 Michael Krufky (mkrufky@linuxtv.org)
0004  *
0005  * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
0006  */
0007 
0008 #include <linux/vmalloc.h>
0009 #include <linux/i2c.h>
0010 #include <media/tuner.h>
0011 
0012 #include "mxl111sf.h"
0013 #include "mxl111sf-reg.h"
0014 #include "mxl111sf-phy.h"
0015 #include "mxl111sf-i2c.h"
0016 #include "mxl111sf-gpio.h"
0017 
0018 #include "mxl111sf-demod.h"
0019 #include "mxl111sf-tuner.h"
0020 
0021 #include "lgdt3305.h"
0022 #include "lg2160.h"
0023 
0024 int dvb_usb_mxl111sf_debug;
0025 module_param_named(debug, dvb_usb_mxl111sf_debug, int, 0644);
0026 MODULE_PARM_DESC(debug, "set debugging level (1=info, 2=xfer, 4=i2c, 8=reg, 16=adv (or-able)).");
0027 
0028 static int dvb_usb_mxl111sf_isoc;
0029 module_param_named(isoc, dvb_usb_mxl111sf_isoc, int, 0644);
0030 MODULE_PARM_DESC(isoc, "enable usb isoc xfer (0=bulk, 1=isoc).");
0031 
0032 static int dvb_usb_mxl111sf_spi;
0033 module_param_named(spi, dvb_usb_mxl111sf_spi, int, 0644);
0034 MODULE_PARM_DESC(spi, "use spi rather than tp for data xfer (0=tp, 1=spi).");
0035 
0036 #define ANT_PATH_AUTO 0
0037 #define ANT_PATH_EXTERNAL 1
0038 #define ANT_PATH_INTERNAL 2
0039 
0040 static int dvb_usb_mxl111sf_rfswitch =
0041 #if 0
0042         ANT_PATH_AUTO;
0043 #else
0044         ANT_PATH_EXTERNAL;
0045 #endif
0046 
0047 module_param_named(rfswitch, dvb_usb_mxl111sf_rfswitch, int, 0644);
0048 MODULE_PARM_DESC(rfswitch, "force rf switch position (0=auto, 1=ext, 2=int).");
0049 
0050 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
0051 
0052 int mxl111sf_ctrl_msg(struct mxl111sf_state *state,
0053               u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen)
0054 {
0055     struct dvb_usb_device *d = state->d;
0056     int wo = (rbuf == NULL || rlen == 0); /* write-only */
0057     int ret;
0058 
0059     if (1 + wlen > MXL_MAX_XFER_SIZE) {
0060         pr_warn("%s: len=%d is too big!\n", __func__, wlen);
0061         return -EOPNOTSUPP;
0062     }
0063 
0064     pr_debug("%s(wlen = %d, rlen = %d)\n", __func__, wlen, rlen);
0065 
0066     mutex_lock(&state->msg_lock);
0067     memset(state->sndbuf, 0, 1+wlen);
0068     memset(state->rcvbuf, 0, rlen);
0069 
0070     state->sndbuf[0] = cmd;
0071     memcpy(&state->sndbuf[1], wbuf, wlen);
0072 
0073     ret = (wo) ? dvb_usbv2_generic_write(d, state->sndbuf, 1+wlen) :
0074         dvb_usbv2_generic_rw(d, state->sndbuf, 1+wlen, state->rcvbuf,
0075                      rlen);
0076 
0077     if (rbuf)
0078         memcpy(rbuf, state->rcvbuf, rlen);
0079 
0080     mutex_unlock(&state->msg_lock);
0081 
0082     mxl_fail(ret);
0083 
0084     return ret;
0085 }
0086 
0087 /* ------------------------------------------------------------------------ */
0088 
0089 #define MXL_CMD_REG_READ    0xaa
0090 #define MXL_CMD_REG_WRITE   0x55
0091 
0092 int mxl111sf_read_reg(struct mxl111sf_state *state, u8 addr, u8 *data)
0093 {
0094     u8 buf[2];
0095     int ret;
0096 
0097     ret = mxl111sf_ctrl_msg(state, MXL_CMD_REG_READ, &addr, 1, buf, 2);
0098     if (mxl_fail(ret)) {
0099         mxl_debug("error reading reg: 0x%02x", addr);
0100         goto fail;
0101     }
0102 
0103     if (buf[0] == addr)
0104         *data = buf[1];
0105     else {
0106         pr_err("invalid response reading reg: 0x%02x != 0x%02x, 0x%02x",
0107             addr, buf[0], buf[1]);
0108         ret = -EINVAL;
0109     }
0110 
0111     pr_debug("R: (0x%02x, 0x%02x)\n", addr, buf[1]);
0112 fail:
0113     return ret;
0114 }
0115 
0116 int mxl111sf_write_reg(struct mxl111sf_state *state, u8 addr, u8 data)
0117 {
0118     u8 buf[] = { addr, data };
0119     int ret;
0120 
0121     pr_debug("W: (0x%02x, 0x%02x)\n", addr, data);
0122 
0123     ret = mxl111sf_ctrl_msg(state, MXL_CMD_REG_WRITE, buf, 2, NULL, 0);
0124     if (mxl_fail(ret))
0125         pr_err("error writing reg: 0x%02x, val: 0x%02x", addr, data);
0126     return ret;
0127 }
0128 
0129 /* ------------------------------------------------------------------------ */
0130 
0131 int mxl111sf_write_reg_mask(struct mxl111sf_state *state,
0132                    u8 addr, u8 mask, u8 data)
0133 {
0134     int ret;
0135     u8 val = 0;
0136 
0137     if (mask != 0xff) {
0138         ret = mxl111sf_read_reg(state, addr, &val);
0139 #if 1
0140         /* don't know why this usually errors out on the first try */
0141         if (mxl_fail(ret))
0142             pr_err("error writing addr: 0x%02x, mask: 0x%02x, data: 0x%02x, retrying...",
0143                    addr, mask, data);
0144 
0145         ret = mxl111sf_read_reg(state, addr, &val);
0146 #endif
0147         if (mxl_fail(ret))
0148             goto fail;
0149     }
0150     val &= ~mask;
0151     val |= data;
0152 
0153     ret = mxl111sf_write_reg(state, addr, val);
0154     mxl_fail(ret);
0155 fail:
0156     return ret;
0157 }
0158 
0159 /* ------------------------------------------------------------------------ */
0160 
0161 int mxl111sf_ctrl_program_regs(struct mxl111sf_state *state,
0162                    struct mxl111sf_reg_ctrl_info *ctrl_reg_info)
0163 {
0164     int i, ret = 0;
0165 
0166     for (i = 0;  ctrl_reg_info[i].addr |
0167              ctrl_reg_info[i].mask |
0168              ctrl_reg_info[i].data;  i++) {
0169 
0170         ret = mxl111sf_write_reg_mask(state,
0171                           ctrl_reg_info[i].addr,
0172                           ctrl_reg_info[i].mask,
0173                           ctrl_reg_info[i].data);
0174         if (mxl_fail(ret)) {
0175             pr_err("failed on reg #%d (0x%02x)", i,
0176                 ctrl_reg_info[i].addr);
0177             break;
0178         }
0179     }
0180     return ret;
0181 }
0182 
0183 /* ------------------------------------------------------------------------ */
0184 
0185 static int mxl1x1sf_get_chip_info(struct mxl111sf_state *state)
0186 {
0187     int ret;
0188     u8 id, ver;
0189     char *mxl_chip, *mxl_rev;
0190 
0191     if ((state->chip_id) && (state->chip_ver))
0192         return 0;
0193 
0194     ret = mxl111sf_read_reg(state, CHIP_ID_REG, &id);
0195     if (mxl_fail(ret))
0196         goto fail;
0197     state->chip_id = id;
0198 
0199     ret = mxl111sf_read_reg(state, TOP_CHIP_REV_ID_REG, &ver);
0200     if (mxl_fail(ret))
0201         goto fail;
0202     state->chip_ver = ver;
0203 
0204     switch (id) {
0205     case 0x61:
0206         mxl_chip = "MxL101SF";
0207         break;
0208     case 0x63:
0209         mxl_chip = "MxL111SF";
0210         break;
0211     default:
0212         mxl_chip = "UNKNOWN MxL1X1";
0213         break;
0214     }
0215     switch (ver) {
0216     case 0x36:
0217         state->chip_rev = MXL111SF_V6;
0218         mxl_rev = "v6";
0219         break;
0220     case 0x08:
0221         state->chip_rev = MXL111SF_V8_100;
0222         mxl_rev = "v8_100";
0223         break;
0224     case 0x18:
0225         state->chip_rev = MXL111SF_V8_200;
0226         mxl_rev = "v8_200";
0227         break;
0228     default:
0229         state->chip_rev = 0;
0230         mxl_rev = "UNKNOWN REVISION";
0231         break;
0232     }
0233     pr_info("%s detected, %s (0x%x)", mxl_chip, mxl_rev, ver);
0234 fail:
0235     return ret;
0236 }
0237 
0238 #define get_chip_info(state)                        \
0239 ({                                  \
0240     int ___ret;                         \
0241     ___ret = mxl1x1sf_get_chip_info(state);             \
0242     if (mxl_fail(___ret)) {                     \
0243         mxl_debug("failed to get chip info"         \
0244               " on first probe attempt");           \
0245         ___ret = mxl1x1sf_get_chip_info(state);         \
0246         if (mxl_fail(___ret))                   \
0247             pr_err("failed to get chip info during probe"); \
0248         else                            \
0249             mxl_debug("probe needed a retry "       \
0250                   "in order to succeed.");      \
0251     }                               \
0252     ___ret;                             \
0253 })
0254 
0255 /* ------------------------------------------------------------------------ */
0256 #if 0
0257 static int mxl111sf_power_ctrl(struct dvb_usb_device *d, int onoff)
0258 {
0259     /* power control depends on which adapter is being woken:
0260      * save this for init, instead, via mxl111sf_adap_fe_init */
0261     return 0;
0262 }
0263 #endif
0264 
0265 static int mxl111sf_adap_fe_init(struct dvb_frontend *fe)
0266 {
0267     struct dvb_usb_device *d = fe_to_d(fe);
0268     struct mxl111sf_state *state = fe_to_priv(fe);
0269     struct mxl111sf_adap_state *adap_state = &state->adap_state[fe->id];
0270     int err;
0271 
0272     /* exit if we didn't initialize the driver yet */
0273     if (!state->chip_id) {
0274         mxl_debug("driver not yet initialized, exit.");
0275         goto fail;
0276     }
0277 
0278     pr_debug("%s()\n", __func__);
0279 
0280     mutex_lock(&state->fe_lock);
0281 
0282     state->alt_mode = adap_state->alt_mode;
0283 
0284     if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
0285         pr_err("set interface failed");
0286 
0287     err = mxl1x1sf_soft_reset(state);
0288     mxl_fail(err);
0289     err = mxl111sf_init_tuner_demod(state);
0290     mxl_fail(err);
0291     err = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
0292 
0293     mxl_fail(err);
0294     err = mxl111sf_enable_usb_output(state);
0295     mxl_fail(err);
0296     err = mxl1x1sf_top_master_ctrl(state, 1);
0297     mxl_fail(err);
0298 
0299     if ((MXL111SF_GPIO_MOD_DVBT != adap_state->gpio_mode) &&
0300         (state->chip_rev > MXL111SF_V6)) {
0301         mxl111sf_config_pin_mux_modes(state,
0302                           PIN_MUX_TS_SPI_IN_MODE_1);
0303         mxl_fail(err);
0304     }
0305     err = mxl111sf_init_port_expander(state);
0306     if (!mxl_fail(err)) {
0307         state->gpio_mode = adap_state->gpio_mode;
0308         err = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
0309         mxl_fail(err);
0310 #if 0
0311         err = fe->ops.init(fe);
0312 #endif
0313         msleep(100); /* add short delay after enabling
0314                   * the demod before touching it */
0315     }
0316 
0317     return (adap_state->fe_init) ? adap_state->fe_init(fe) : 0;
0318 fail:
0319     return -ENODEV;
0320 }
0321 
0322 static int mxl111sf_adap_fe_sleep(struct dvb_frontend *fe)
0323 {
0324     struct mxl111sf_state *state = fe_to_priv(fe);
0325     struct mxl111sf_adap_state *adap_state = &state->adap_state[fe->id];
0326     int err;
0327 
0328     /* exit if we didn't initialize the driver yet */
0329     if (!state->chip_id) {
0330         mxl_debug("driver not yet initialized, exit.");
0331         goto fail;
0332     }
0333 
0334     pr_debug("%s()\n", __func__);
0335 
0336     err = (adap_state->fe_sleep) ? adap_state->fe_sleep(fe) : 0;
0337 
0338     mutex_unlock(&state->fe_lock);
0339 
0340     return err;
0341 fail:
0342     return -ENODEV;
0343 }
0344 
0345 
0346 static int mxl111sf_ep6_streaming_ctrl(struct dvb_frontend *fe, int onoff)
0347 {
0348     struct mxl111sf_state *state = fe_to_priv(fe);
0349     struct mxl111sf_adap_state *adap_state = &state->adap_state[fe->id];
0350     int ret = 0;
0351 
0352     pr_debug("%s(%d)\n", __func__, onoff);
0353 
0354     if (onoff) {
0355         ret = mxl111sf_enable_usb_output(state);
0356         mxl_fail(ret);
0357         ret = mxl111sf_config_mpeg_in(state, 1, 1,
0358                           adap_state->ep6_clockphase,
0359                           0, 0);
0360         mxl_fail(ret);
0361 #if 0
0362     } else {
0363         ret = mxl111sf_disable_656_port(state);
0364         mxl_fail(ret);
0365 #endif
0366     }
0367 
0368     return ret;
0369 }
0370 
0371 static int mxl111sf_ep5_streaming_ctrl(struct dvb_frontend *fe, int onoff)
0372 {
0373     struct mxl111sf_state *state = fe_to_priv(fe);
0374     int ret = 0;
0375 
0376     pr_debug("%s(%d)\n", __func__, onoff);
0377 
0378     if (onoff) {
0379         ret = mxl111sf_enable_usb_output(state);
0380         mxl_fail(ret);
0381 
0382         ret = mxl111sf_init_i2s_port(state, 200);
0383         mxl_fail(ret);
0384         ret = mxl111sf_config_i2s(state, 0, 15);
0385         mxl_fail(ret);
0386     } else {
0387         ret = mxl111sf_disable_i2s_port(state);
0388         mxl_fail(ret);
0389     }
0390     if (state->chip_rev > MXL111SF_V6)
0391         ret = mxl111sf_config_spi(state, onoff);
0392     mxl_fail(ret);
0393 
0394     return ret;
0395 }
0396 
0397 static int mxl111sf_ep4_streaming_ctrl(struct dvb_frontend *fe, int onoff)
0398 {
0399     struct mxl111sf_state *state = fe_to_priv(fe);
0400     int ret = 0;
0401 
0402     pr_debug("%s(%d)\n", __func__, onoff);
0403 
0404     if (onoff) {
0405         ret = mxl111sf_enable_usb_output(state);
0406         mxl_fail(ret);
0407     }
0408 
0409     return ret;
0410 }
0411 
0412 /* ------------------------------------------------------------------------ */
0413 
0414 static struct lgdt3305_config hauppauge_lgdt3305_config = {
0415     .i2c_addr           = 0xb2 >> 1,
0416     .mpeg_mode          = LGDT3305_MPEG_SERIAL,
0417     .tpclk_edge         = LGDT3305_TPCLK_RISING_EDGE,
0418     .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
0419     .deny_i2c_rptr      = 1,
0420     .spectral_inversion = 0,
0421     .qam_if_khz         = 6000,
0422     .vsb_if_khz         = 6000,
0423 };
0424 
0425 static int mxl111sf_lgdt3305_frontend_attach(struct dvb_usb_adapter *adap, u8 fe_id)
0426 {
0427     struct dvb_usb_device *d = adap_to_d(adap);
0428     struct mxl111sf_state *state = d_to_priv(d);
0429     struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
0430     int ret;
0431 
0432     pr_debug("%s()\n", __func__);
0433 
0434     /* save a pointer to the dvb_usb_device in device state */
0435     state->d = d;
0436     adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 2 : 1;
0437     state->alt_mode = adap_state->alt_mode;
0438 
0439     if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
0440         pr_err("set interface failed");
0441 
0442     state->gpio_mode = MXL111SF_GPIO_MOD_ATSC;
0443     adap_state->gpio_mode = state->gpio_mode;
0444     adap_state->device_mode = MXL_TUNER_MODE;
0445     adap_state->ep6_clockphase = 1;
0446 
0447     ret = mxl1x1sf_soft_reset(state);
0448     if (mxl_fail(ret))
0449         goto fail;
0450     ret = mxl111sf_init_tuner_demod(state);
0451     if (mxl_fail(ret))
0452         goto fail;
0453 
0454     ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
0455     if (mxl_fail(ret))
0456         goto fail;
0457 
0458     ret = mxl111sf_enable_usb_output(state);
0459     if (mxl_fail(ret))
0460         goto fail;
0461     ret = mxl1x1sf_top_master_ctrl(state, 1);
0462     if (mxl_fail(ret))
0463         goto fail;
0464 
0465     ret = mxl111sf_init_port_expander(state);
0466     if (mxl_fail(ret))
0467         goto fail;
0468     ret = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
0469     if (mxl_fail(ret))
0470         goto fail;
0471 
0472     adap->fe[fe_id] = dvb_attach(lgdt3305_attach,
0473                  &hauppauge_lgdt3305_config,
0474                  &d->i2c_adap);
0475     if (adap->fe[fe_id]) {
0476         state->num_frontends++;
0477         adap_state->fe_init = adap->fe[fe_id]->ops.init;
0478         adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
0479         adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
0480         adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
0481         return 0;
0482     }
0483     ret = -EIO;
0484 fail:
0485     return ret;
0486 }
0487 
0488 static struct lg2160_config hauppauge_lg2160_config = {
0489     .lg_chip            = LG2160,
0490     .i2c_addr           = 0x1c >> 1,
0491     .deny_i2c_rptr      = 1,
0492     .spectral_inversion = 0,
0493     .if_khz             = 6000,
0494 };
0495 
0496 static int mxl111sf_lg2160_frontend_attach(struct dvb_usb_adapter *adap, u8 fe_id)
0497 {
0498     struct dvb_usb_device *d = adap_to_d(adap);
0499     struct mxl111sf_state *state = d_to_priv(d);
0500     struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
0501     int ret;
0502 
0503     pr_debug("%s()\n", __func__);
0504 
0505     /* save a pointer to the dvb_usb_device in device state */
0506     state->d = d;
0507     adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 2 : 1;
0508     state->alt_mode = adap_state->alt_mode;
0509 
0510     if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
0511         pr_err("set interface failed");
0512 
0513     state->gpio_mode = MXL111SF_GPIO_MOD_MH;
0514     adap_state->gpio_mode = state->gpio_mode;
0515     adap_state->device_mode = MXL_TUNER_MODE;
0516     adap_state->ep6_clockphase = 1;
0517 
0518     ret = mxl1x1sf_soft_reset(state);
0519     if (mxl_fail(ret))
0520         goto fail;
0521     ret = mxl111sf_init_tuner_demod(state);
0522     if (mxl_fail(ret))
0523         goto fail;
0524 
0525     ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
0526     if (mxl_fail(ret))
0527         goto fail;
0528 
0529     ret = mxl111sf_enable_usb_output(state);
0530     if (mxl_fail(ret))
0531         goto fail;
0532     ret = mxl1x1sf_top_master_ctrl(state, 1);
0533     if (mxl_fail(ret))
0534         goto fail;
0535 
0536     ret = mxl111sf_init_port_expander(state);
0537     if (mxl_fail(ret))
0538         goto fail;
0539     ret = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
0540     if (mxl_fail(ret))
0541         goto fail;
0542 
0543     ret = get_chip_info(state);
0544     if (mxl_fail(ret))
0545         goto fail;
0546 
0547     adap->fe[fe_id] = dvb_attach(lg2160_attach,
0548                   &hauppauge_lg2160_config,
0549                   &d->i2c_adap);
0550     if (adap->fe[fe_id]) {
0551         state->num_frontends++;
0552         adap_state->fe_init = adap->fe[fe_id]->ops.init;
0553         adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
0554         adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
0555         adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
0556         return 0;
0557     }
0558     ret = -EIO;
0559 fail:
0560     return ret;
0561 }
0562 
0563 static struct lg2160_config hauppauge_lg2161_1019_config = {
0564     .lg_chip            = LG2161_1019,
0565     .i2c_addr           = 0x1c >> 1,
0566     .deny_i2c_rptr      = 1,
0567     .spectral_inversion = 0,
0568     .if_khz             = 6000,
0569     .output_if          = 2, /* LG2161_OIF_SPI_MAS */
0570 };
0571 
0572 static struct lg2160_config hauppauge_lg2161_1040_config = {
0573     .lg_chip            = LG2161_1040,
0574     .i2c_addr           = 0x1c >> 1,
0575     .deny_i2c_rptr      = 1,
0576     .spectral_inversion = 0,
0577     .if_khz             = 6000,
0578     .output_if          = 4, /* LG2161_OIF_SPI_MAS */
0579 };
0580 
0581 static int mxl111sf_lg2161_frontend_attach(struct dvb_usb_adapter *adap, u8 fe_id)
0582 {
0583     struct dvb_usb_device *d = adap_to_d(adap);
0584     struct mxl111sf_state *state = d_to_priv(d);
0585     struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
0586     int ret;
0587 
0588     pr_debug("%s()\n", __func__);
0589 
0590     /* save a pointer to the dvb_usb_device in device state */
0591     state->d = d;
0592     adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 2 : 1;
0593     state->alt_mode = adap_state->alt_mode;
0594 
0595     if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
0596         pr_err("set interface failed");
0597 
0598     state->gpio_mode = MXL111SF_GPIO_MOD_MH;
0599     adap_state->gpio_mode = state->gpio_mode;
0600     adap_state->device_mode = MXL_TUNER_MODE;
0601     adap_state->ep6_clockphase = 1;
0602 
0603     ret = mxl1x1sf_soft_reset(state);
0604     if (mxl_fail(ret))
0605         goto fail;
0606     ret = mxl111sf_init_tuner_demod(state);
0607     if (mxl_fail(ret))
0608         goto fail;
0609 
0610     ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
0611     if (mxl_fail(ret))
0612         goto fail;
0613 
0614     ret = mxl111sf_enable_usb_output(state);
0615     if (mxl_fail(ret))
0616         goto fail;
0617     ret = mxl1x1sf_top_master_ctrl(state, 1);
0618     if (mxl_fail(ret))
0619         goto fail;
0620 
0621     ret = mxl111sf_init_port_expander(state);
0622     if (mxl_fail(ret))
0623         goto fail;
0624     ret = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
0625     if (mxl_fail(ret))
0626         goto fail;
0627 
0628     ret = get_chip_info(state);
0629     if (mxl_fail(ret))
0630         goto fail;
0631 
0632     adap->fe[fe_id] = dvb_attach(lg2160_attach,
0633                   (MXL111SF_V8_200 == state->chip_rev) ?
0634                   &hauppauge_lg2161_1040_config :
0635                   &hauppauge_lg2161_1019_config,
0636                   &d->i2c_adap);
0637     if (adap->fe[fe_id]) {
0638         state->num_frontends++;
0639         adap_state->fe_init = adap->fe[fe_id]->ops.init;
0640         adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
0641         adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
0642         adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
0643         return 0;
0644     }
0645     ret = -EIO;
0646 fail:
0647     return ret;
0648 }
0649 
0650 static struct lg2160_config hauppauge_lg2161_1019_ep6_config = {
0651     .lg_chip            = LG2161_1019,
0652     .i2c_addr           = 0x1c >> 1,
0653     .deny_i2c_rptr      = 1,
0654     .spectral_inversion = 0,
0655     .if_khz             = 6000,
0656     .output_if          = 1, /* LG2161_OIF_SERIAL_TS */
0657 };
0658 
0659 static struct lg2160_config hauppauge_lg2161_1040_ep6_config = {
0660     .lg_chip            = LG2161_1040,
0661     .i2c_addr           = 0x1c >> 1,
0662     .deny_i2c_rptr      = 1,
0663     .spectral_inversion = 0,
0664     .if_khz             = 6000,
0665     .output_if          = 7, /* LG2161_OIF_SERIAL_TS */
0666 };
0667 
0668 static int mxl111sf_lg2161_ep6_frontend_attach(struct dvb_usb_adapter *adap, u8 fe_id)
0669 {
0670     struct dvb_usb_device *d = adap_to_d(adap);
0671     struct mxl111sf_state *state = d_to_priv(d);
0672     struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
0673     int ret;
0674 
0675     pr_debug("%s()\n", __func__);
0676 
0677     /* save a pointer to the dvb_usb_device in device state */
0678     state->d = d;
0679     adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 2 : 1;
0680     state->alt_mode = adap_state->alt_mode;
0681 
0682     if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
0683         pr_err("set interface failed");
0684 
0685     state->gpio_mode = MXL111SF_GPIO_MOD_MH;
0686     adap_state->gpio_mode = state->gpio_mode;
0687     adap_state->device_mode = MXL_TUNER_MODE;
0688     adap_state->ep6_clockphase = 0;
0689 
0690     ret = mxl1x1sf_soft_reset(state);
0691     if (mxl_fail(ret))
0692         goto fail;
0693     ret = mxl111sf_init_tuner_demod(state);
0694     if (mxl_fail(ret))
0695         goto fail;
0696 
0697     ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
0698     if (mxl_fail(ret))
0699         goto fail;
0700 
0701     ret = mxl111sf_enable_usb_output(state);
0702     if (mxl_fail(ret))
0703         goto fail;
0704     ret = mxl1x1sf_top_master_ctrl(state, 1);
0705     if (mxl_fail(ret))
0706         goto fail;
0707 
0708     ret = mxl111sf_init_port_expander(state);
0709     if (mxl_fail(ret))
0710         goto fail;
0711     ret = mxl111sf_gpio_mode_switch(state, state->gpio_mode);
0712     if (mxl_fail(ret))
0713         goto fail;
0714 
0715     ret = get_chip_info(state);
0716     if (mxl_fail(ret))
0717         goto fail;
0718 
0719     adap->fe[fe_id] = dvb_attach(lg2160_attach,
0720                   (MXL111SF_V8_200 == state->chip_rev) ?
0721                   &hauppauge_lg2161_1040_ep6_config :
0722                   &hauppauge_lg2161_1019_ep6_config,
0723                   &d->i2c_adap);
0724     if (adap->fe[fe_id]) {
0725         state->num_frontends++;
0726         adap_state->fe_init = adap->fe[fe_id]->ops.init;
0727         adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
0728         adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
0729         adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
0730         return 0;
0731     }
0732     ret = -EIO;
0733 fail:
0734     return ret;
0735 }
0736 
0737 static const struct mxl111sf_demod_config mxl_demod_config = {
0738     .read_reg        = mxl111sf_read_reg,
0739     .write_reg       = mxl111sf_write_reg,
0740     .program_regs    = mxl111sf_ctrl_program_regs,
0741 };
0742 
0743 static int mxl111sf_attach_demod(struct dvb_usb_adapter *adap, u8 fe_id)
0744 {
0745     struct dvb_usb_device *d = adap_to_d(adap);
0746     struct mxl111sf_state *state = d_to_priv(d);
0747     struct mxl111sf_adap_state *adap_state = &state->adap_state[fe_id];
0748     int ret;
0749 
0750     pr_debug("%s()\n", __func__);
0751 
0752     /* save a pointer to the dvb_usb_device in device state */
0753     state->d = d;
0754     adap_state->alt_mode = (dvb_usb_mxl111sf_isoc) ? 1 : 2;
0755     state->alt_mode = adap_state->alt_mode;
0756 
0757     if (usb_set_interface(d->udev, 0, state->alt_mode) < 0)
0758         pr_err("set interface failed");
0759 
0760     state->gpio_mode = MXL111SF_GPIO_MOD_DVBT;
0761     adap_state->gpio_mode = state->gpio_mode;
0762     adap_state->device_mode = MXL_SOC_MODE;
0763     adap_state->ep6_clockphase = 1;
0764 
0765     ret = mxl1x1sf_soft_reset(state);
0766     if (mxl_fail(ret))
0767         goto fail;
0768     ret = mxl111sf_init_tuner_demod(state);
0769     if (mxl_fail(ret))
0770         goto fail;
0771 
0772     ret = mxl1x1sf_set_device_mode(state, adap_state->device_mode);
0773     if (mxl_fail(ret))
0774         goto fail;
0775 
0776     ret = mxl111sf_enable_usb_output(state);
0777     if (mxl_fail(ret))
0778         goto fail;
0779     ret = mxl1x1sf_top_master_ctrl(state, 1);
0780     if (mxl_fail(ret))
0781         goto fail;
0782 
0783     /* don't care if this fails */
0784     mxl111sf_init_port_expander(state);
0785 
0786     adap->fe[fe_id] = dvb_attach(mxl111sf_demod_attach, state,
0787                   &mxl_demod_config);
0788     if (adap->fe[fe_id]) {
0789         state->num_frontends++;
0790         adap_state->fe_init = adap->fe[fe_id]->ops.init;
0791         adap->fe[fe_id]->ops.init = mxl111sf_adap_fe_init;
0792         adap_state->fe_sleep = adap->fe[fe_id]->ops.sleep;
0793         adap->fe[fe_id]->ops.sleep = mxl111sf_adap_fe_sleep;
0794         return 0;
0795     }
0796     ret = -EIO;
0797 fail:
0798     return ret;
0799 }
0800 
0801 static inline int mxl111sf_set_ant_path(struct mxl111sf_state *state,
0802                     int antpath)
0803 {
0804     return mxl111sf_idac_config(state, 1, 1,
0805                     (antpath == ANT_PATH_INTERNAL) ?
0806                     0x3f : 0x00, 0);
0807 }
0808 
0809 #define DbgAntHunt(x, pwr0, pwr1, pwr2, pwr3) \
0810     pr_err("%s(%d) FINAL input set to %s rxPwr:%d|%d|%d|%d\n", \
0811         __func__, __LINE__, \
0812         (ANT_PATH_EXTERNAL == x) ? "EXTERNAL" : "INTERNAL", \
0813         pwr0, pwr1, pwr2, pwr3)
0814 
0815 #define ANT_HUNT_SLEEP 90
0816 #define ANT_EXT_TWEAK 0
0817 
0818 static int mxl111sf_ant_hunt(struct dvb_frontend *fe)
0819 {
0820     struct mxl111sf_state *state = fe_to_priv(fe);
0821     int antctrl = dvb_usb_mxl111sf_rfswitch;
0822 
0823     u16 rxPwrA, rxPwr0, rxPwr1, rxPwr2;
0824 
0825     /* FIXME: must force EXTERNAL for QAM - done elsewhere */
0826     mxl111sf_set_ant_path(state, antctrl == ANT_PATH_AUTO ?
0827                   ANT_PATH_EXTERNAL : antctrl);
0828 
0829     if (antctrl == ANT_PATH_AUTO) {
0830 #if 0
0831         msleep(ANT_HUNT_SLEEP);
0832 #endif
0833         fe->ops.tuner_ops.get_rf_strength(fe, &rxPwrA);
0834 
0835         mxl111sf_set_ant_path(state, ANT_PATH_EXTERNAL);
0836         msleep(ANT_HUNT_SLEEP);
0837         fe->ops.tuner_ops.get_rf_strength(fe, &rxPwr0);
0838 
0839         mxl111sf_set_ant_path(state, ANT_PATH_EXTERNAL);
0840         msleep(ANT_HUNT_SLEEP);
0841         fe->ops.tuner_ops.get_rf_strength(fe, &rxPwr1);
0842 
0843         mxl111sf_set_ant_path(state, ANT_PATH_INTERNAL);
0844         msleep(ANT_HUNT_SLEEP);
0845         fe->ops.tuner_ops.get_rf_strength(fe, &rxPwr2);
0846 
0847         if (rxPwr1+ANT_EXT_TWEAK >= rxPwr2) {
0848             /* return with EXTERNAL enabled */
0849             mxl111sf_set_ant_path(state, ANT_PATH_EXTERNAL);
0850             DbgAntHunt(ANT_PATH_EXTERNAL, rxPwrA,
0851                    rxPwr0, rxPwr1, rxPwr2);
0852         } else {
0853             /* return with INTERNAL enabled */
0854             DbgAntHunt(ANT_PATH_INTERNAL, rxPwrA,
0855                    rxPwr0, rxPwr1, rxPwr2);
0856         }
0857     }
0858     return 0;
0859 }
0860 
0861 static const struct mxl111sf_tuner_config mxl_tuner_config = {
0862     .if_freq         = MXL_IF_6_0, /* applies to external IF output, only */
0863     .invert_spectrum = 0,
0864     .read_reg        = mxl111sf_read_reg,
0865     .write_reg       = mxl111sf_write_reg,
0866     .program_regs    = mxl111sf_ctrl_program_regs,
0867     .top_master_ctrl = mxl1x1sf_top_master_ctrl,
0868     .ant_hunt        = mxl111sf_ant_hunt,
0869 };
0870 
0871 static int mxl111sf_attach_tuner(struct dvb_usb_adapter *adap)
0872 {
0873     struct mxl111sf_state *state = adap_to_priv(adap);
0874 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
0875     struct media_device *mdev = dvb_get_media_controller(&adap->dvb_adap);
0876     int ret;
0877 #endif
0878     int i;
0879 
0880     pr_debug("%s()\n", __func__);
0881 
0882     for (i = 0; i < state->num_frontends; i++) {
0883         if (dvb_attach(mxl111sf_tuner_attach, adap->fe[i], state,
0884                 &mxl_tuner_config) == NULL)
0885             return -EIO;
0886         adap->fe[i]->ops.read_signal_strength = adap->fe[i]->ops.tuner_ops.get_rf_strength;
0887     }
0888 
0889 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
0890     state->tuner.function = MEDIA_ENT_F_TUNER;
0891     state->tuner.name = "mxl111sf tuner";
0892     state->tuner_pads[MXL111SF_PAD_RF_INPUT].flags = MEDIA_PAD_FL_SINK;
0893     state->tuner_pads[MXL111SF_PAD_RF_INPUT].sig_type = PAD_SIGNAL_ANALOG;
0894     state->tuner_pads[MXL111SF_PAD_OUTPUT].flags = MEDIA_PAD_FL_SOURCE;
0895     state->tuner_pads[MXL111SF_PAD_OUTPUT].sig_type = PAD_SIGNAL_ANALOG;
0896 
0897     ret = media_entity_pads_init(&state->tuner,
0898                      MXL111SF_NUM_PADS, state->tuner_pads);
0899     if (ret)
0900         return ret;
0901 
0902     ret = media_device_register_entity(mdev, &state->tuner);
0903     if (ret)
0904         return ret;
0905 #endif
0906     return 0;
0907 }
0908 
0909 static u32 mxl111sf_i2c_func(struct i2c_adapter *adapter)
0910 {
0911     return I2C_FUNC_I2C;
0912 }
0913 
0914 static struct i2c_algorithm mxl111sf_i2c_algo = {
0915     .master_xfer   = mxl111sf_i2c_xfer,
0916     .functionality = mxl111sf_i2c_func,
0917 #ifdef NEED_ALGO_CONTROL
0918     .algo_control = dummy_algo_control,
0919 #endif
0920 };
0921 
0922 static int mxl111sf_init(struct dvb_usb_device *d)
0923 {
0924     struct mxl111sf_state *state = d_to_priv(d);
0925     int ret;
0926     static u8 eeprom[256];
0927     u8 reg = 0;
0928     struct i2c_msg msg[2] = {
0929         { .addr = 0xa0 >> 1, .len = 1, .buf = &reg },
0930         { .addr = 0xa0 >> 1, .flags = I2C_M_RD,
0931           .len = sizeof(eeprom), .buf = eeprom },
0932     };
0933 
0934     ret = get_chip_info(state);
0935     if (mxl_fail(ret))
0936         pr_err("failed to get chip info during probe");
0937 
0938     mutex_init(&state->fe_lock);
0939 
0940     if (state->chip_rev > MXL111SF_V6)
0941         mxl111sf_config_pin_mux_modes(state, PIN_MUX_TS_SPI_IN_MODE_1);
0942 
0943     ret = i2c_transfer(&d->i2c_adap, msg, 2);
0944     if (mxl_fail(ret))
0945         return 0;
0946     tveeprom_hauppauge_analog(&state->tv, (0x84 == eeprom[0xa0]) ?
0947                   eeprom + 0xa0 : eeprom + 0x80);
0948 #if 0
0949     switch (state->tv.model) {
0950     case 117001:
0951     case 126001:
0952     case 138001:
0953         break;
0954     default:
0955         printk(KERN_WARNING "%s: warning: unknown hauppauge model #%d\n",
0956                __func__, state->tv.model);
0957     }
0958 #endif
0959     return 0;
0960 }
0961 
0962 static int mxl111sf_frontend_attach_dvbt(struct dvb_usb_adapter *adap)
0963 {
0964     return mxl111sf_attach_demod(adap, 0);
0965 }
0966 
0967 static int mxl111sf_frontend_attach_atsc(struct dvb_usb_adapter *adap)
0968 {
0969     return mxl111sf_lgdt3305_frontend_attach(adap, 0);
0970 }
0971 
0972 static int mxl111sf_frontend_attach_mh(struct dvb_usb_adapter *adap)
0973 {
0974     return mxl111sf_lg2160_frontend_attach(adap, 0);
0975 }
0976 
0977 static int mxl111sf_frontend_attach_atsc_mh(struct dvb_usb_adapter *adap)
0978 {
0979     int ret;
0980     pr_debug("%s\n", __func__);
0981 
0982     ret = mxl111sf_lgdt3305_frontend_attach(adap, 0);
0983     if (ret < 0)
0984         return ret;
0985 
0986     ret = mxl111sf_attach_demod(adap, 1);
0987     if (ret < 0)
0988         return ret;
0989 
0990     ret = mxl111sf_lg2160_frontend_attach(adap, 2);
0991     if (ret < 0)
0992         return ret;
0993 
0994     return ret;
0995 }
0996 
0997 static int mxl111sf_frontend_attach_mercury(struct dvb_usb_adapter *adap)
0998 {
0999     int ret;
1000     pr_debug("%s\n", __func__);
1001 
1002     ret = mxl111sf_lgdt3305_frontend_attach(adap, 0);
1003     if (ret < 0)
1004         return ret;
1005 
1006     ret = mxl111sf_attach_demod(adap, 1);
1007     if (ret < 0)
1008         return ret;
1009 
1010     ret = mxl111sf_lg2161_ep6_frontend_attach(adap, 2);
1011     if (ret < 0)
1012         return ret;
1013 
1014     return ret;
1015 }
1016 
1017 static int mxl111sf_frontend_attach_mercury_mh(struct dvb_usb_adapter *adap)
1018 {
1019     int ret;
1020     pr_debug("%s\n", __func__);
1021 
1022     ret = mxl111sf_attach_demod(adap, 0);
1023     if (ret < 0)
1024         return ret;
1025 
1026     if (dvb_usb_mxl111sf_spi)
1027         ret = mxl111sf_lg2161_frontend_attach(adap, 1);
1028     else
1029         ret = mxl111sf_lg2161_ep6_frontend_attach(adap, 1);
1030 
1031     return ret;
1032 }
1033 
1034 static void mxl111sf_stream_config_bulk(struct usb_data_stream_properties *stream, u8 endpoint)
1035 {
1036     pr_debug("%s: endpoint=%d size=8192\n", __func__, endpoint);
1037     stream->type = USB_BULK;
1038     stream->count = 5;
1039     stream->endpoint = endpoint;
1040     stream->u.bulk.buffersize = 8192;
1041 }
1042 
1043 static void mxl111sf_stream_config_isoc(struct usb_data_stream_properties *stream,
1044         u8 endpoint, int framesperurb, int framesize)
1045 {
1046     pr_debug("%s: endpoint=%d size=%d\n", __func__, endpoint,
1047             framesperurb * framesize);
1048     stream->type = USB_ISOC;
1049     stream->count = 5;
1050     stream->endpoint = endpoint;
1051     stream->u.isoc.framesperurb = framesperurb;
1052     stream->u.isoc.framesize = framesize;
1053     stream->u.isoc.interval = 1;
1054 }
1055 
1056 /* DVB USB Driver stuff */
1057 
1058 /* dvbt       mxl111sf
1059  * bulk       EP4/BULK/5/8192
1060  * isoc       EP4/ISOC/5/96/564
1061  */
1062 static int mxl111sf_get_stream_config_dvbt(struct dvb_frontend *fe,
1063         u8 *ts_type, struct usb_data_stream_properties *stream)
1064 {
1065     pr_debug("%s: fe=%d\n", __func__, fe->id);
1066 
1067     *ts_type = DVB_USB_FE_TS_TYPE_188;
1068     if (dvb_usb_mxl111sf_isoc)
1069         mxl111sf_stream_config_isoc(stream, 4, 96, 564);
1070     else
1071         mxl111sf_stream_config_bulk(stream, 4);
1072     return 0;
1073 }
1074 
1075 static int mxl111sf_probe(struct dvb_usb_device *dev)
1076 {
1077     struct mxl111sf_state *state = d_to_priv(dev);
1078 
1079     mutex_init(&state->msg_lock);
1080     return 0;
1081 }
1082 
1083 static struct dvb_usb_device_properties mxl111sf_props_dvbt = {
1084     .driver_name = KBUILD_MODNAME,
1085     .owner = THIS_MODULE,
1086     .adapter_nr = adapter_nr,
1087     .size_of_priv = sizeof(struct mxl111sf_state),
1088 
1089     .generic_bulk_ctrl_endpoint = 0x02,
1090     .generic_bulk_ctrl_endpoint_response = 0x81,
1091 
1092     .probe             = mxl111sf_probe,
1093     .i2c_algo          = &mxl111sf_i2c_algo,
1094     .frontend_attach   = mxl111sf_frontend_attach_dvbt,
1095     .tuner_attach      = mxl111sf_attach_tuner,
1096     .init              = mxl111sf_init,
1097     .streaming_ctrl    = mxl111sf_ep4_streaming_ctrl,
1098     .get_stream_config = mxl111sf_get_stream_config_dvbt,
1099 
1100     .num_adapters = 1,
1101     .adapter = {
1102         {
1103             .stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1104         }
1105     }
1106 };
1107 
1108 /* atsc       lgdt3305
1109  * bulk       EP6/BULK/5/8192
1110  * isoc       EP6/ISOC/5/24/3072
1111  */
1112 static int mxl111sf_get_stream_config_atsc(struct dvb_frontend *fe,
1113         u8 *ts_type, struct usb_data_stream_properties *stream)
1114 {
1115     pr_debug("%s: fe=%d\n", __func__, fe->id);
1116 
1117     *ts_type = DVB_USB_FE_TS_TYPE_188;
1118     if (dvb_usb_mxl111sf_isoc)
1119         mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1120     else
1121         mxl111sf_stream_config_bulk(stream, 6);
1122     return 0;
1123 }
1124 
1125 static struct dvb_usb_device_properties mxl111sf_props_atsc = {
1126     .driver_name = KBUILD_MODNAME,
1127     .owner = THIS_MODULE,
1128     .adapter_nr = adapter_nr,
1129     .size_of_priv = sizeof(struct mxl111sf_state),
1130 
1131     .generic_bulk_ctrl_endpoint = 0x02,
1132     .generic_bulk_ctrl_endpoint_response = 0x81,
1133 
1134     .probe             = mxl111sf_probe,
1135     .i2c_algo          = &mxl111sf_i2c_algo,
1136     .frontend_attach   = mxl111sf_frontend_attach_atsc,
1137     .tuner_attach      = mxl111sf_attach_tuner,
1138     .init              = mxl111sf_init,
1139     .streaming_ctrl    = mxl111sf_ep6_streaming_ctrl,
1140     .get_stream_config = mxl111sf_get_stream_config_atsc,
1141 
1142     .num_adapters = 1,
1143     .adapter = {
1144         {
1145             .stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1146         }
1147     }
1148 };
1149 
1150 /* mh         lg2160
1151  * bulk       EP5/BULK/5/8192/RAW
1152  * isoc       EP5/ISOC/5/96/200/RAW
1153  */
1154 static int mxl111sf_get_stream_config_mh(struct dvb_frontend *fe,
1155         u8 *ts_type, struct usb_data_stream_properties *stream)
1156 {
1157     pr_debug("%s: fe=%d\n", __func__, fe->id);
1158 
1159     *ts_type = DVB_USB_FE_TS_TYPE_RAW;
1160     if (dvb_usb_mxl111sf_isoc)
1161         mxl111sf_stream_config_isoc(stream, 5, 96, 200);
1162     else
1163         mxl111sf_stream_config_bulk(stream, 5);
1164     return 0;
1165 }
1166 
1167 static struct dvb_usb_device_properties mxl111sf_props_mh = {
1168     .driver_name = KBUILD_MODNAME,
1169     .owner = THIS_MODULE,
1170     .adapter_nr = adapter_nr,
1171     .size_of_priv = sizeof(struct mxl111sf_state),
1172 
1173     .generic_bulk_ctrl_endpoint = 0x02,
1174     .generic_bulk_ctrl_endpoint_response = 0x81,
1175 
1176     .probe             = mxl111sf_probe,
1177     .i2c_algo          = &mxl111sf_i2c_algo,
1178     .frontend_attach   = mxl111sf_frontend_attach_mh,
1179     .tuner_attach      = mxl111sf_attach_tuner,
1180     .init              = mxl111sf_init,
1181     .streaming_ctrl    = mxl111sf_ep5_streaming_ctrl,
1182     .get_stream_config = mxl111sf_get_stream_config_mh,
1183 
1184     .num_adapters = 1,
1185     .adapter = {
1186         {
1187             .stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1188         }
1189     }
1190 };
1191 
1192 /* atsc mh    lgdt3305           mxl111sf          lg2160
1193  * bulk       EP6/BULK/5/8192    EP4/BULK/5/8192   EP5/BULK/5/8192/RAW
1194  * isoc       EP6/ISOC/5/24/3072 EP4/ISOC/5/96/564 EP5/ISOC/5/96/200/RAW
1195  */
1196 static int mxl111sf_get_stream_config_atsc_mh(struct dvb_frontend *fe,
1197         u8 *ts_type, struct usb_data_stream_properties *stream)
1198 {
1199     pr_debug("%s: fe=%d\n", __func__, fe->id);
1200 
1201     if (fe->id == 0) {
1202         *ts_type = DVB_USB_FE_TS_TYPE_188;
1203         if (dvb_usb_mxl111sf_isoc)
1204             mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1205         else
1206             mxl111sf_stream_config_bulk(stream, 6);
1207     } else if (fe->id == 1) {
1208         *ts_type = DVB_USB_FE_TS_TYPE_188;
1209         if (dvb_usb_mxl111sf_isoc)
1210             mxl111sf_stream_config_isoc(stream, 4, 96, 564);
1211         else
1212             mxl111sf_stream_config_bulk(stream, 4);
1213     } else if (fe->id == 2) {
1214         *ts_type = DVB_USB_FE_TS_TYPE_RAW;
1215         if (dvb_usb_mxl111sf_isoc)
1216             mxl111sf_stream_config_isoc(stream, 5, 96, 200);
1217         else
1218             mxl111sf_stream_config_bulk(stream, 5);
1219     }
1220     return 0;
1221 }
1222 
1223 static int mxl111sf_streaming_ctrl_atsc_mh(struct dvb_frontend *fe, int onoff)
1224 {
1225     pr_debug("%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
1226 
1227     if (fe->id == 0)
1228         return mxl111sf_ep6_streaming_ctrl(fe, onoff);
1229     else if (fe->id == 1)
1230         return mxl111sf_ep4_streaming_ctrl(fe, onoff);
1231     else if (fe->id == 2)
1232         return mxl111sf_ep5_streaming_ctrl(fe, onoff);
1233     return 0;
1234 }
1235 
1236 static struct dvb_usb_device_properties mxl111sf_props_atsc_mh = {
1237     .driver_name = KBUILD_MODNAME,
1238     .owner = THIS_MODULE,
1239     .adapter_nr = adapter_nr,
1240     .size_of_priv = sizeof(struct mxl111sf_state),
1241 
1242     .generic_bulk_ctrl_endpoint = 0x02,
1243     .generic_bulk_ctrl_endpoint_response = 0x81,
1244 
1245     .probe             = mxl111sf_probe,
1246     .i2c_algo          = &mxl111sf_i2c_algo,
1247     .frontend_attach   = mxl111sf_frontend_attach_atsc_mh,
1248     .tuner_attach      = mxl111sf_attach_tuner,
1249     .init              = mxl111sf_init,
1250     .streaming_ctrl    = mxl111sf_streaming_ctrl_atsc_mh,
1251     .get_stream_config = mxl111sf_get_stream_config_atsc_mh,
1252 
1253     .num_adapters = 1,
1254     .adapter = {
1255         {
1256             .stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1257         }
1258     }
1259 };
1260 
1261 /* mercury    lgdt3305           mxl111sf          lg2161
1262  * tp bulk    EP6/BULK/5/8192    EP4/BULK/5/8192   EP6/BULK/5/8192/RAW
1263  * tp isoc    EP6/ISOC/5/24/3072 EP4/ISOC/5/96/564 EP6/ISOC/5/24/3072/RAW
1264  * spi bulk   EP6/BULK/5/8192    EP4/BULK/5/8192   EP5/BULK/5/8192/RAW
1265  * spi isoc   EP6/ISOC/5/24/3072 EP4/ISOC/5/96/564 EP5/ISOC/5/96/200/RAW
1266  */
1267 static int mxl111sf_get_stream_config_mercury(struct dvb_frontend *fe,
1268         u8 *ts_type, struct usb_data_stream_properties *stream)
1269 {
1270     pr_debug("%s: fe=%d\n", __func__, fe->id);
1271 
1272     if (fe->id == 0) {
1273         *ts_type = DVB_USB_FE_TS_TYPE_188;
1274         if (dvb_usb_mxl111sf_isoc)
1275             mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1276         else
1277             mxl111sf_stream_config_bulk(stream, 6);
1278     } else if (fe->id == 1) {
1279         *ts_type = DVB_USB_FE_TS_TYPE_188;
1280         if (dvb_usb_mxl111sf_isoc)
1281             mxl111sf_stream_config_isoc(stream, 4, 96, 564);
1282         else
1283             mxl111sf_stream_config_bulk(stream, 4);
1284     } else if (fe->id == 2 && dvb_usb_mxl111sf_spi) {
1285         *ts_type = DVB_USB_FE_TS_TYPE_RAW;
1286         if (dvb_usb_mxl111sf_isoc)
1287             mxl111sf_stream_config_isoc(stream, 5, 96, 200);
1288         else
1289             mxl111sf_stream_config_bulk(stream, 5);
1290     } else if (fe->id == 2 && !dvb_usb_mxl111sf_spi) {
1291         *ts_type = DVB_USB_FE_TS_TYPE_RAW;
1292         if (dvb_usb_mxl111sf_isoc)
1293             mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1294         else
1295             mxl111sf_stream_config_bulk(stream, 6);
1296     }
1297     return 0;
1298 }
1299 
1300 static int mxl111sf_streaming_ctrl_mercury(struct dvb_frontend *fe, int onoff)
1301 {
1302     pr_debug("%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
1303 
1304     if (fe->id == 0)
1305         return mxl111sf_ep6_streaming_ctrl(fe, onoff);
1306     else if (fe->id == 1)
1307         return mxl111sf_ep4_streaming_ctrl(fe, onoff);
1308     else if (fe->id == 2 && dvb_usb_mxl111sf_spi)
1309         return mxl111sf_ep5_streaming_ctrl(fe, onoff);
1310     else if (fe->id == 2 && !dvb_usb_mxl111sf_spi)
1311         return mxl111sf_ep6_streaming_ctrl(fe, onoff);
1312     return 0;
1313 }
1314 
1315 static struct dvb_usb_device_properties mxl111sf_props_mercury = {
1316     .driver_name = KBUILD_MODNAME,
1317     .owner = THIS_MODULE,
1318     .adapter_nr = adapter_nr,
1319     .size_of_priv = sizeof(struct mxl111sf_state),
1320 
1321     .generic_bulk_ctrl_endpoint = 0x02,
1322     .generic_bulk_ctrl_endpoint_response = 0x81,
1323 
1324     .probe             = mxl111sf_probe,
1325     .i2c_algo          = &mxl111sf_i2c_algo,
1326     .frontend_attach   = mxl111sf_frontend_attach_mercury,
1327     .tuner_attach      = mxl111sf_attach_tuner,
1328     .init              = mxl111sf_init,
1329     .streaming_ctrl    = mxl111sf_streaming_ctrl_mercury,
1330     .get_stream_config = mxl111sf_get_stream_config_mercury,
1331 
1332     .num_adapters = 1,
1333     .adapter = {
1334         {
1335             .stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1336         }
1337     }
1338 };
1339 
1340 /* mercury mh mxl111sf          lg2161
1341  * tp bulk    EP4/BULK/5/8192   EP6/BULK/5/8192/RAW
1342  * tp isoc    EP4/ISOC/5/96/564 EP6/ISOC/5/24/3072/RAW
1343  * spi bulk   EP4/BULK/5/8192   EP5/BULK/5/8192/RAW
1344  * spi isoc   EP4/ISOC/5/96/564 EP5/ISOC/5/96/200/RAW
1345  */
1346 static int mxl111sf_get_stream_config_mercury_mh(struct dvb_frontend *fe,
1347         u8 *ts_type, struct usb_data_stream_properties *stream)
1348 {
1349     pr_debug("%s: fe=%d\n", __func__, fe->id);
1350 
1351     if (fe->id == 0) {
1352         *ts_type = DVB_USB_FE_TS_TYPE_188;
1353         if (dvb_usb_mxl111sf_isoc)
1354             mxl111sf_stream_config_isoc(stream, 4, 96, 564);
1355         else
1356             mxl111sf_stream_config_bulk(stream, 4);
1357     } else if (fe->id == 1 && dvb_usb_mxl111sf_spi) {
1358         *ts_type = DVB_USB_FE_TS_TYPE_RAW;
1359         if (dvb_usb_mxl111sf_isoc)
1360             mxl111sf_stream_config_isoc(stream, 5, 96, 200);
1361         else
1362             mxl111sf_stream_config_bulk(stream, 5);
1363     } else if (fe->id == 1 && !dvb_usb_mxl111sf_spi) {
1364         *ts_type = DVB_USB_FE_TS_TYPE_RAW;
1365         if (dvb_usb_mxl111sf_isoc)
1366             mxl111sf_stream_config_isoc(stream, 6, 24, 3072);
1367         else
1368             mxl111sf_stream_config_bulk(stream, 6);
1369     }
1370     return 0;
1371 }
1372 
1373 static int mxl111sf_streaming_ctrl_mercury_mh(struct dvb_frontend *fe, int onoff)
1374 {
1375     pr_debug("%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
1376 
1377     if (fe->id == 0)
1378         return mxl111sf_ep4_streaming_ctrl(fe, onoff);
1379     else if (fe->id == 1  && dvb_usb_mxl111sf_spi)
1380         return mxl111sf_ep5_streaming_ctrl(fe, onoff);
1381     else if (fe->id == 1 && !dvb_usb_mxl111sf_spi)
1382         return mxl111sf_ep6_streaming_ctrl(fe, onoff);
1383     return 0;
1384 }
1385 
1386 static struct dvb_usb_device_properties mxl111sf_props_mercury_mh = {
1387     .driver_name = KBUILD_MODNAME,
1388     .owner = THIS_MODULE,
1389     .adapter_nr = adapter_nr,
1390     .size_of_priv = sizeof(struct mxl111sf_state),
1391 
1392     .generic_bulk_ctrl_endpoint = 0x02,
1393     .generic_bulk_ctrl_endpoint_response = 0x81,
1394 
1395     .probe             = mxl111sf_probe,
1396     .i2c_algo          = &mxl111sf_i2c_algo,
1397     .frontend_attach   = mxl111sf_frontend_attach_mercury_mh,
1398     .tuner_attach      = mxl111sf_attach_tuner,
1399     .init              = mxl111sf_init,
1400     .streaming_ctrl    = mxl111sf_streaming_ctrl_mercury_mh,
1401     .get_stream_config = mxl111sf_get_stream_config_mercury_mh,
1402 
1403     .num_adapters = 1,
1404     .adapter = {
1405         {
1406             .stream = DVB_USB_STREAM_ISOC(6, 5, 24, 3072, 1),
1407         }
1408     }
1409 };
1410 
1411 static const struct usb_device_id mxl111sf_id_table[] = {
1412     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc600, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1413     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc601, &mxl111sf_props_atsc, "Hauppauge 126xxx ATSC", NULL) },
1414     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc602, &mxl111sf_props_mh, "HCW 126xxx", NULL) },
1415     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc603, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1416     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc604, &mxl111sf_props_dvbt, "Hauppauge 126xxx DVBT", NULL) },
1417     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc609, &mxl111sf_props_atsc, "Hauppauge 126xxx ATSC", NULL) },
1418     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc60a, &mxl111sf_props_mh, "HCW 126xxx", NULL) },
1419     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc60b, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1420     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc60c, &mxl111sf_props_dvbt, "Hauppauge 126xxx DVBT", NULL) },
1421     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc653, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1422     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc65b, &mxl111sf_props_atsc_mh, "Hauppauge 126xxx ATSC+", NULL) },
1423     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb700, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1424     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb701, &mxl111sf_props_atsc, "Hauppauge 126xxx ATSC", NULL) },
1425     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb702, &mxl111sf_props_mh, "HCW 117xxx", NULL) },
1426     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb703, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1427     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb704, &mxl111sf_props_dvbt, "Hauppauge 117xxx DVBT", NULL) },
1428     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb753, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1429     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb763, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1430     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb764, &mxl111sf_props_dvbt, "Hauppauge 117xxx DVBT", NULL) },
1431     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd853, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1432     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd854, &mxl111sf_props_dvbt, "Hauppauge 138xxx DVBT", NULL) },
1433     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd863, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1434     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd864, &mxl111sf_props_dvbt, "Hauppauge 138xxx DVBT", NULL) },
1435     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8d3, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1436     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8d4, &mxl111sf_props_dvbt, "Hauppauge 138xxx DVBT", NULL) },
1437     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8e3, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1438     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8e4, &mxl111sf_props_dvbt, "Hauppauge 138xxx DVBT", NULL) },
1439     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xd8ff, &mxl111sf_props_mercury, "Hauppauge Mercury", NULL) },
1440     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc612, &mxl111sf_props_mercury_mh, "Hauppauge 126xxx", NULL) },
1441     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc613, &mxl111sf_props_mercury, "Hauppauge WinTV-Aero-M", NULL) },
1442     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc61a, &mxl111sf_props_mercury_mh, "Hauppauge 126xxx", NULL) },
1443     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xc61b, &mxl111sf_props_mercury, "Hauppauge WinTV-Aero-M", NULL) },
1444     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb757, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1445     { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xb767, &mxl111sf_props_atsc_mh, "Hauppauge 117xxx ATSC+", NULL) },
1446     { }
1447 };
1448 MODULE_DEVICE_TABLE(usb, mxl111sf_id_table);
1449 
1450 static struct usb_driver mxl111sf_usb_driver = {
1451     .name = KBUILD_MODNAME,
1452     .id_table = mxl111sf_id_table,
1453     .probe = dvb_usbv2_probe,
1454     .disconnect = dvb_usbv2_disconnect,
1455     .suspend = dvb_usbv2_suspend,
1456     .resume = dvb_usbv2_resume,
1457     .no_dynamic_id = 1,
1458     .soft_unbind = 1,
1459 };
1460 
1461 module_usb_driver(mxl111sf_usb_driver);
1462 
1463 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1464 MODULE_DESCRIPTION("Driver for MaxLinear MxL111SF");
1465 MODULE_VERSION("1.0");
1466 MODULE_LICENSE("GPL");