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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 static int dvb_usb_ec168_debug
;
28 module_param_named(debug
, dvb_usb_ec168_debug
, int, 0644);
29 MODULE_PARM_DESC(debug
, "set debugging level" DVB_USB_DEBUG_STATUS
);
30 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
32 static struct ec100_config ec168_ec100_config
;
34 static int ec168_rw_udev(struct usb_device
*udev
, struct ec168_req
*req
)
38 u8 request
, requesttype
;
42 case DOWNLOAD_FIRMWARE
:
46 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
50 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
54 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
58 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
62 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
66 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
70 err("unknown command:%02x", req
->cmd
);
75 if (requesttype
== (USB_TYPE_VENDOR
| USB_DIR_OUT
)) {
77 memcpy(buf
, req
->data
, req
->size
);
78 pipe
= usb_sndctrlpipe(udev
, 0);
81 pipe
= usb_rcvctrlpipe(udev
, 0);
84 msleep(1); /* avoid I2C errors */
86 ret
= usb_control_msg(udev
, pipe
, request
, requesttype
, req
->value
,
87 req
->index
, buf
, sizeof(buf
), EC168_USB_TIMEOUT
);
89 ec168_debug_dump(request
, requesttype
, req
->value
, req
->index
, buf
,
97 /* read request, copy returned data to return buf */
98 if (!ret
&& requesttype
== (USB_TYPE_VENDOR
| USB_DIR_IN
))
99 memcpy(req
->data
, buf
, req
->size
);
103 deb_info("%s: failed:%d\n", __func__
, ret
);
107 static int ec168_ctrl_msg(struct dvb_usb_device
*d
, struct ec168_req
*req
)
109 return ec168_rw_udev(d
->udev
, req
);
113 static int ec168_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg msg
[],
116 struct dvb_usb_device
*d
= i2c_get_adapdata(adap
);
117 struct ec168_req req
;
124 if (mutex_lock_interruptible(&d
->i2c_mutex
) < 0)
128 if (num
> i
+ 1 && (msg
[i
+1].flags
& I2C_M_RD
)) {
129 if (msg
[i
].addr
== ec168_ec100_config
.demod_address
) {
130 req
.cmd
= READ_DEMOD
;
132 req
.index
= 0xff00 + msg
[i
].buf
[0]; /* reg */
133 req
.size
= msg
[i
+1].len
; /* bytes to read */
134 req
.data
= &msg
[i
+1].buf
[0];
135 ret
= ec168_ctrl_msg(d
, &req
);
138 err("I2C read not implemented");
143 if (msg
[i
].addr
== ec168_ec100_config
.demod_address
) {
144 req
.cmd
= WRITE_DEMOD
;
145 req
.value
= msg
[i
].buf
[1]; /* val */
146 req
.index
= 0xff00 + msg
[i
].buf
[0]; /* reg */
149 ret
= ec168_ctrl_msg(d
, &req
);
153 req
.value
= msg
[i
].buf
[0]; /* val */
154 req
.index
= 0x0100 + msg
[i
].addr
; /* I2C addr */
155 req
.size
= msg
[i
].len
-1;
156 req
.data
= &msg
[i
].buf
[1];
157 ret
= ec168_ctrl_msg(d
, &req
);
168 mutex_unlock(&d
->i2c_mutex
);
173 static u32
ec168_i2c_func(struct i2c_adapter
*adapter
)
178 static struct i2c_algorithm ec168_i2c_algo
= {
179 .master_xfer
= ec168_i2c_xfer
,
180 .functionality
= ec168_i2c_func
,
183 /* Callbacks for DVB USB */
184 static struct ec100_config ec168_ec100_config
= {
185 .demod_address
= 0xff, /* not real address, demod is integrated */
188 static int ec168_ec100_frontend_attach(struct dvb_usb_adapter
*adap
)
190 deb_info("%s:\n", __func__
);
191 adap
->fe
= dvb_attach(ec100_attach
, &ec168_ec100_config
,
192 &adap
->dev
->i2c_adap
);
193 if (adap
->fe
== NULL
)
199 static struct mxl5005s_config ec168_mxl5003s_config
= {
201 .if_freq
= IF_FREQ_4570000HZ
,
202 .xtal_freq
= CRYSTAL_FREQ_16000000HZ
,
203 .agc_mode
= MXL_SINGLE_AGC
,
204 .tracking_filter
= MXL_TF_OFF
,
205 .rssi_enable
= MXL_RSSI_ENABLE
,
206 .cap_select
= MXL_CAP_SEL_ENABLE
,
207 .div_out
= MXL_DIV_OUT_4
,
208 .clock_out
= MXL_CLOCK_OUT_DISABLE
,
209 .output_load
= MXL5005S_IF_OUTPUT_LOAD_200_OHM
,
210 .top
= MXL5005S_TOP_25P2
,
211 .mod_mode
= MXL_DIGITAL_MODE
,
212 .if_mode
= MXL_ZERO_IF
,
213 .AgcMasterByte
= 0x00,
216 static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter
*adap
)
218 deb_info("%s:\n", __func__
);
219 return dvb_attach(mxl5005s_attach
, adap
->fe
, &adap
->dev
->i2c_adap
,
220 &ec168_mxl5003s_config
) == NULL
? -ENODEV
: 0;
223 static int ec168_streaming_ctrl(struct dvb_usb_adapter
*adap
, int onoff
)
225 struct ec168_req req
= {STREAMING_CTRL
, 0x7f01, 0x0202, 0, NULL
};
226 deb_info("%s: onoff:%d\n", __func__
, onoff
);
229 return ec168_ctrl_msg(adap
->dev
, &req
);
232 static int ec168_download_firmware(struct usb_device
*udev
,
233 const struct firmware
*fw
)
235 int i
, len
, packets
, remainder
, ret
;
236 u16 addr
= 0x0000; /* firmware start address */
237 struct ec168_req req
= {DOWNLOAD_FIRMWARE
, 0, 0, 0, NULL
};
238 deb_info("%s:\n", __func__
);
240 #define FW_PACKET_MAX_DATA 2048
241 packets
= fw
->size
/ FW_PACKET_MAX_DATA
;
242 remainder
= fw
->size
% FW_PACKET_MAX_DATA
;
243 len
= FW_PACKET_MAX_DATA
;
244 for (i
= 0; i
<= packets
; i
++) {
245 if (i
== packets
) /* set size of the last packet */
249 req
.data
= (u8
*)(fw
->data
+ i
* FW_PACKET_MAX_DATA
);
251 addr
+= FW_PACKET_MAX_DATA
;
253 ret
= ec168_rw_udev(udev
, &req
);
255 err("firmware download failed:%d packet:%d", ret
, i
);
262 req
.cmd
= SET_CONFIG
;
265 ret
= ec168_rw_udev(udev
, &req
);
269 /* really needed - no idea what does */
273 ret
= ec168_rw_udev(udev
, &req
);
277 /* activate tuner I2C? */
281 ret
= ec168_rw_udev(udev
, &req
);
287 deb_info("%s: failed:%d\n", __func__
, ret
);
291 static int ec168_identify_state(struct usb_device
*udev
,
292 struct dvb_usb_device_properties
*props
,
293 struct dvb_usb_device_description
**desc
, int *cold
)
297 struct ec168_req req
= {GET_CONFIG
, 0, 1, sizeof(reply
), &reply
};
298 deb_info("%s:\n", __func__
);
300 ret
= ec168_rw_udev(udev
, &req
);
304 deb_info("%s: reply:%02x\n", __func__
, reply
);
313 deb_info("%s: failed:%d\n", __func__
, ret
);
317 /* DVB USB Driver stuff */
318 static struct dvb_usb_device_properties ec168_properties
;
320 static int ec168_probe(struct usb_interface
*intf
,
321 const struct usb_device_id
*id
)
324 deb_info("%s: interface:%d\n", __func__
,
325 intf
->cur_altsetting
->desc
.bInterfaceNumber
);
327 ret
= dvb_usb_device_init(intf
, &ec168_properties
, THIS_MODULE
, NULL
,
334 deb_info("%s: failed:%d\n", __func__
, ret
);
338 #define E3C_EC168_1689 0
339 #define E3C_EC168_FFFA 1
340 #define E3C_EC168_FFFB 2
341 #define E3C_EC168_1001 3
342 #define E3C_EC168_1002 4
344 static struct usb_device_id ec168_id
[] = {
346 {USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168
)},
348 {USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_2
)},
350 {USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_3
)},
352 {USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_4
)},
354 {USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_5
)},
355 {} /* terminating entry */
358 MODULE_DEVICE_TABLE(usb
, ec168_id
);
360 static struct dvb_usb_device_properties ec168_properties
= {
361 .caps
= DVB_USB_IS_AN_I2C_ADAPTER
,
363 .usb_ctrl
= DEVICE_SPECIFIC
,
364 .download_firmware
= ec168_download_firmware
,
365 .firmware
= "dvb-usb-ec168.fw",
373 .streaming_ctrl
= ec168_streaming_ctrl
,
374 .frontend_attach
= ec168_ec100_frontend_attach
,
375 .tuner_attach
= ec168_mxl5003s_tuner_attach
,
382 .buffersize
= (32*512),
389 .identify_state
= ec168_identify_state
,
391 .i2c_algo
= &ec168_i2c_algo
,
393 .num_device_descs
= 1,
396 .name
= "E3C EC168 DVB-T USB2.0 reference design",
398 &ec168_id
[E3C_EC168_1689
],
399 &ec168_id
[E3C_EC168_FFFA
],
400 &ec168_id
[E3C_EC168_FFFB
],
401 &ec168_id
[E3C_EC168_1001
],
402 &ec168_id
[E3C_EC168_1002
],
409 static struct usb_driver ec168_driver
= {
410 .name
= "dvb_usb_ec168",
411 .probe
= ec168_probe
,
412 .disconnect
= dvb_usb_device_exit
,
413 .id_table
= ec168_id
,
417 static int __init
ec168_module_init(void)
420 deb_info("%s:\n", __func__
);
421 ret
= usb_register(&ec168_driver
);
423 err("module init failed:%d", ret
);
428 static void __exit
ec168_module_exit(void)
430 deb_info("%s:\n", __func__
);
431 /* deregister this driver from the USB subsystem */
432 usb_deregister(&ec168_driver
);
435 module_init(ec168_module_init
);
436 module_exit(ec168_module_exit
);
438 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
439 MODULE_DESCRIPTION("E3C EC168 DVB-T USB2.0 driver");
440 MODULE_LICENSE("GPL");