1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * E3C EC168 DVB USB driver
12 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
14 static int ec168_ctrl_msg(struct dvb_usb_device *d, struct ec168_req *req)
18 u8 request, requesttype;
22 case DOWNLOAD_FIRMWARE:
26 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
30 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
34 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
38 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
42 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
46 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
50 dev_err(&d->udev->dev, "%s: unknown command=%02x\n",
51 KBUILD_MODNAME, req->cmd);
56 buf = kmalloc(req->size, GFP_KERNEL);
62 if (requesttype == (USB_TYPE_VENDOR | USB_DIR_OUT)) {
64 memcpy(buf, req->data, req->size);
65 pipe = usb_sndctrlpipe(d->udev, 0);
68 pipe = usb_rcvctrlpipe(d->udev, 0);
71 msleep(1); /* avoid I2C errors */
73 ret = usb_control_msg(d->udev, pipe, request, requesttype, req->value,
74 req->index, buf, req->size, EC168_USB_TIMEOUT);
76 dvb_usb_dbg_usb_control_msg(d->udev, request, requesttype, req->value,
77 req->index, buf, req->size);
84 /* read request, copy returned data to return buf */
85 if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
86 memcpy(req->data, buf, req->size);
94 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
99 static struct ec100_config ec168_ec100_config;
101 static int ec168_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
104 struct dvb_usb_device *d = i2c_get_adapdata(adap);
105 struct ec168_req req;
112 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
116 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
117 if (msg[i].addr == ec168_ec100_config.demod_address) {
118 if (msg[i].len < 1) {
122 req.cmd = READ_DEMOD;
124 req.index = 0xff00 + msg[i].buf[0]; /* reg */
125 req.size = msg[i+1].len; /* bytes to read */
126 req.data = &msg[i+1].buf[0];
127 ret = ec168_ctrl_msg(d, &req);
130 dev_err(&d->udev->dev, "%s: I2C read not " \
137 if (msg[i].addr == ec168_ec100_config.demod_address) {
138 if (msg[i].len < 1) {
142 req.cmd = WRITE_DEMOD;
143 req.value = msg[i].buf[1]; /* val */
144 req.index = 0xff00 + msg[i].buf[0]; /* reg */
147 ret = ec168_ctrl_msg(d, &req);
150 if (msg[i].len < 1) {
155 req.value = msg[i].buf[0]; /* val */
156 req.index = 0x0100 + msg[i].addr; /* I2C addr */
157 req.size = msg[i].len-1;
158 req.data = &msg[i].buf[1];
159 ret = ec168_ctrl_msg(d, &req);
170 mutex_unlock(&d->i2c_mutex);
174 static u32 ec168_i2c_func(struct i2c_adapter *adapter)
179 static struct i2c_algorithm ec168_i2c_algo = {
180 .master_xfer = ec168_i2c_xfer,
181 .functionality = ec168_i2c_func,
184 /* Callbacks for DVB USB */
185 static int ec168_identify_state(struct dvb_usb_device *d, const char **name)
189 struct ec168_req req = {GET_CONFIG, 0, 1, sizeof(reply), &reply};
190 dev_dbg(&d->udev->dev, "%s:\n", __func__);
192 ret = ec168_ctrl_msg(d, &req);
196 dev_dbg(&d->udev->dev, "%s: reply=%02x\n", __func__, reply);
205 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
209 static int ec168_download_firmware(struct dvb_usb_device *d,
210 const struct firmware *fw)
212 int ret, len, remaining;
213 struct ec168_req req = {DOWNLOAD_FIRMWARE, 0, 0, 0, NULL};
214 dev_dbg(&d->udev->dev, "%s:\n", __func__);
216 #define LEN_MAX 2048 /* max packet size */
217 for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
223 req.data = (u8 *) &fw->data[fw->size - remaining];
224 req.index = fw->size - remaining;
226 ret = ec168_ctrl_msg(d, &req);
228 dev_err(&d->udev->dev,
229 "%s: firmware download failed=%d\n",
230 KBUILD_MODNAME, ret);
238 req.cmd = SET_CONFIG;
241 ret = ec168_ctrl_msg(d, &req);
245 /* really needed - no idea what does */
249 ret = ec168_ctrl_msg(d, &req);
253 /* activate tuner I2C? */
257 ret = ec168_ctrl_msg(d, &req);
263 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
267 static struct ec100_config ec168_ec100_config = {
268 .demod_address = 0xff, /* not real address, demod is integrated */
271 static int ec168_ec100_frontend_attach(struct dvb_usb_adapter *adap)
273 struct dvb_usb_device *d = adap_to_d(adap);
274 dev_dbg(&d->udev->dev, "%s:\n", __func__);
276 adap->fe[0] = dvb_attach(ec100_attach, &ec168_ec100_config,
278 if (adap->fe[0] == NULL)
284 static struct mxl5005s_config ec168_mxl5003s_config = {
286 .if_freq = IF_FREQ_4570000HZ,
287 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
288 .agc_mode = MXL_SINGLE_AGC,
289 .tracking_filter = MXL_TF_OFF,
290 .rssi_enable = MXL_RSSI_ENABLE,
291 .cap_select = MXL_CAP_SEL_ENABLE,
292 .div_out = MXL_DIV_OUT_4,
293 .clock_out = MXL_CLOCK_OUT_DISABLE,
294 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
295 .top = MXL5005S_TOP_25P2,
296 .mod_mode = MXL_DIGITAL_MODE,
297 .if_mode = MXL_ZERO_IF,
298 .AgcMasterByte = 0x00,
301 static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
303 struct dvb_usb_device *d = adap_to_d(adap);
304 dev_dbg(&d->udev->dev, "%s:\n", __func__);
306 return dvb_attach(mxl5005s_attach, adap->fe[0], &d->i2c_adap,
307 &ec168_mxl5003s_config) == NULL ? -ENODEV : 0;
310 static int ec168_streaming_ctrl(struct dvb_frontend *fe, int onoff)
312 struct dvb_usb_device *d = fe_to_d(fe);
313 struct ec168_req req = {STREAMING_CTRL, 0x7f01, 0x0202, 0, NULL};
314 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
318 return ec168_ctrl_msg(d, &req);
321 /* DVB USB Driver stuff */
322 /* bInterfaceNumber 0 is HID
323 * bInterfaceNumber 1 is DVB-T */
324 static const struct dvb_usb_device_properties ec168_props = {
325 .driver_name = KBUILD_MODNAME,
326 .owner = THIS_MODULE,
327 .adapter_nr = adapter_nr,
328 .bInterfaceNumber = 1,
330 .identify_state = ec168_identify_state,
331 .firmware = EC168_FIRMWARE,
332 .download_firmware = ec168_download_firmware,
334 .i2c_algo = &ec168_i2c_algo,
335 .frontend_attach = ec168_ec100_frontend_attach,
336 .tuner_attach = ec168_mxl5003s_tuner_attach,
337 .streaming_ctrl = ec168_streaming_ctrl,
342 .stream = DVB_USB_STREAM_BULK(0x82, 6, 32 * 512),
347 static const struct usb_device_id ec168_id[] = {
348 { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168,
349 &ec168_props, "E3C EC168 reference design", NULL)},
350 { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_2,
351 &ec168_props, "E3C EC168 reference design", NULL)},
352 { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_3,
353 &ec168_props, "E3C EC168 reference design", NULL)},
354 { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_4,
355 &ec168_props, "E3C EC168 reference design", NULL)},
356 { DVB_USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_5,
357 &ec168_props, "E3C EC168 reference design", NULL)},
360 MODULE_DEVICE_TABLE(usb, ec168_id);
362 static struct usb_driver ec168_driver = {
363 .name = KBUILD_MODNAME,
364 .id_table = ec168_id,
365 .probe = dvb_usbv2_probe,
366 .disconnect = dvb_usbv2_disconnect,
367 .suspend = dvb_usbv2_suspend,
368 .resume = dvb_usbv2_resume,
373 module_usb_driver(ec168_driver);
376 MODULE_DESCRIPTION("E3C EC168 driver");
377 MODULE_LICENSE("GPL");
378 MODULE_FIRMWARE(EC168_FIRMWARE);