2 * E3C EC168 DVB USB driver
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
22 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
24 static int ec168_ctrl_msg(struct dvb_usb_device *d, struct ec168_req *req)
28 u8 request, requesttype;
32 case DOWNLOAD_FIRMWARE:
36 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
40 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
44 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
48 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
52 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
56 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
60 dev_err(&d->udev->dev, "%s: unknown command=%02x\n",
61 KBUILD_MODNAME, req->cmd);
66 buf = kmalloc(req->size, GFP_KERNEL);
72 if (requesttype == (USB_TYPE_VENDOR | USB_DIR_OUT)) {
74 memcpy(buf, req->data, req->size);
75 pipe = usb_sndctrlpipe(d->udev, 0);
78 pipe = usb_rcvctrlpipe(d->udev, 0);
81 msleep(1); /* avoid I2C errors */
83 ret = usb_control_msg(d->udev, pipe, request, requesttype, req->value,
84 req->index, buf, req->size, EC168_USB_TIMEOUT);
86 dvb_usb_dbg_usb_control_msg(d->udev, request, requesttype, req->value,
87 req->index, buf, req->size);
94 /* read request, copy returned data to return buf */
95 if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
96 memcpy(req->data, buf, req->size);
104 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
109 static struct ec100_config ec168_ec100_config;
111 static int ec168_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
114 struct dvb_usb_device *d = i2c_get_adapdata(adap);
115 struct ec168_req req;
122 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
126 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
127 if (msg[i].addr == ec168_ec100_config.demod_address) {
128 if (msg[i].len < 1) {
132 req.cmd = READ_DEMOD;
134 req.index = 0xff00 + msg[i].buf[0]; /* reg */
135 req.size = msg[i+1].len; /* bytes to read */
136 req.data = &msg[i+1].buf[0];
137 ret = ec168_ctrl_msg(d, &req);
140 dev_err(&d->udev->dev, "%s: I2C read not " \
147 if (msg[i].addr == ec168_ec100_config.demod_address) {
148 if (msg[i].len < 1) {
152 req.cmd = WRITE_DEMOD;
153 req.value = msg[i].buf[1]; /* val */
154 req.index = 0xff00 + msg[i].buf[0]; /* reg */
157 ret = ec168_ctrl_msg(d, &req);
160 if (msg[i].len < 1) {
165 req.value = msg[i].buf[0]; /* val */
166 req.index = 0x0100 + msg[i].addr; /* I2C addr */
167 req.size = msg[i].len-1;
168 req.data = &msg[i].buf[1];
169 ret = ec168_ctrl_msg(d, &req);
180 mutex_unlock(&d->i2c_mutex);
184 static u32 ec168_i2c_func(struct i2c_adapter *adapter)
189 static struct i2c_algorithm ec168_i2c_algo = {
190 .master_xfer = ec168_i2c_xfer,
191 .functionality = ec168_i2c_func,
194 /* Callbacks for DVB USB */
195 static int ec168_identify_state(struct dvb_usb_device *d, const char **name)
199 struct ec168_req req = {GET_CONFIG, 0, 1, sizeof(reply), &reply};
200 dev_dbg(&d->udev->dev, "%s:\n", __func__);
202 ret = ec168_ctrl_msg(d, &req);
206 dev_dbg(&d->udev->dev, "%s: reply=%02x\n", __func__, reply);
215 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
219 static int ec168_download_firmware(struct dvb_usb_device *d,
220 const struct firmware *fw)
222 int ret, len, remaining;
223 struct ec168_req req = {DOWNLOAD_FIRMWARE, 0, 0, 0, NULL};
224 dev_dbg(&d->udev->dev, "%s:\n", __func__);
226 #define LEN_MAX 2048 /* max packet size */
227 for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
233 req.data = (u8 *) &fw->data[fw->size - remaining];
234 req.index = fw->size - remaining;
236 ret = ec168_ctrl_msg(d, &req);
238 dev_err(&d->udev->dev,
239 "%s: firmware download failed=%d\n",
240 KBUILD_MODNAME, ret);
248 req.cmd = SET_CONFIG;
251 ret = ec168_ctrl_msg(d, &req);
255 /* really needed - no idea what does */
259 ret = ec168_ctrl_msg(d, &req);
263 /* activate tuner I2C? */
267 ret = ec168_ctrl_msg(d, &req);
273 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
277 static struct ec100_config ec168_ec100_config = {
278 .demod_address = 0xff, /* not real address, demod is integrated */
281 static int ec168_ec100_frontend_attach(struct dvb_usb_adapter *adap)
283 struct dvb_usb_device *d = adap_to_d(adap);
284 dev_dbg(&d->udev->dev, "%s:\n", __func__);
286 adap->fe[0] = dvb_attach(ec100_attach, &ec168_ec100_config,
288 if (adap->fe[0] == NULL)
294 static struct mxl5005s_config ec168_mxl5003s_config = {
296 .if_freq = IF_FREQ_4570000HZ,
297 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
298 .agc_mode = MXL_SINGLE_AGC,
299 .tracking_filter = MXL_TF_OFF,
300 .rssi_enable = MXL_RSSI_ENABLE,
301 .cap_select = MXL_CAP_SEL_ENABLE,
302 .div_out = MXL_DIV_OUT_4,
303 .clock_out = MXL_CLOCK_OUT_DISABLE,
304 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
305 .top = MXL5005S_TOP_25P2,
306 .mod_mode = MXL_DIGITAL_MODE,
307 .if_mode = MXL_ZERO_IF,
308 .AgcMasterByte = 0x00,
311 static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
313 struct dvb_usb_device *d = adap_to_d(adap);
314 dev_dbg(&d->udev->dev, "%s:\n", __func__);
316 return dvb_attach(mxl5005s_attach, adap->fe[0], &d->i2c_adap,
317 &ec168_mxl5003s_config) == NULL ? -ENODEV : 0;
320 static int ec168_streaming_ctrl(struct dvb_frontend *fe, int onoff)
322 struct dvb_usb_device *d = fe_to_d(fe);
323 struct ec168_req req = {STREAMING_CTRL, 0x7f01, 0x0202, 0, NULL};
324 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
328 return ec168_ctrl_msg(d, &req);
331 /* DVB USB Driver stuff */
332 /* bInterfaceNumber 0 is HID
333 * bInterfaceNumber 1 is DVB-T */
334 static struct dvb_usb_device_properties ec168_props = {
335 .driver_name = KBUILD_MODNAME,
336 .owner = THIS_MODULE,
337 .adapter_nr = adapter_nr,
338 .bInterfaceNumber = 1,
340 .identify_state = ec168_identify_state,
341 .firmware = EC168_FIRMWARE,
342 .download_firmware = ec168_download_firmware,
344 .i2c_algo = &ec168_i2c_algo,
345 .frontend_attach = ec168_ec100_frontend_attach,
346 .tuner_attach = ec168_mxl5003s_tuner_attach,
347 .streaming_ctrl = ec168_streaming_ctrl,
352 .stream = DVB_USB_STREAM_BULK(0x82, 6, 32 * 512),
357 static const struct dvb_usb_driver_info ec168_driver_info = {
358 .name = "E3C EC168 reference design",
359 .props = &ec168_props,
362 static const struct usb_device_id ec168_id[] = {
363 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168),
364 .driver_info = (kernel_ulong_t) &ec168_driver_info },
365 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_2),
366 .driver_info = (kernel_ulong_t) &ec168_driver_info },
367 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_3),
368 .driver_info = (kernel_ulong_t) &ec168_driver_info },
369 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_4),
370 .driver_info = (kernel_ulong_t) &ec168_driver_info },
371 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_5),
372 .driver_info = (kernel_ulong_t) &ec168_driver_info },
375 MODULE_DEVICE_TABLE(usb, ec168_id);
377 static struct usb_driver ec168_driver = {
378 .name = KBUILD_MODNAME,
379 .id_table = ec168_id,
380 .probe = dvb_usbv2_probe,
381 .disconnect = dvb_usbv2_disconnect,
382 .suspend = dvb_usbv2_suspend,
383 .resume = dvb_usbv2_resume,
388 module_usb_driver(ec168_driver);
390 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
391 MODULE_DESCRIPTION("E3C EC168 driver");
392 MODULE_LICENSE("GPL");