blob: b4989ba8897d5fd5dbea9e55c3786b4a07fd2446 [file] [log] [blame]
Antti Palosaari2bf290b2009-11-13 22:38:55 -03001/*
2 * E3C EC168 DVB USB driver
3 *
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 *
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.
10 *
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.
15 *
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.
19 *
20 */
21
22#include "ec168.h"
23#include "ec100.h"
24#include "mxl5005s.h"
25
26/* debug */
27static int dvb_usb_ec168_debug;
28module_param_named(debug, dvb_usb_ec168_debug, int, 0644);
29MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
30DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
31
32static struct ec100_config ec168_ec100_config;
33
34static int ec168_rw_udev(struct usb_device *udev, struct ec168_req *req)
35{
36 int ret;
37 unsigned int pipe;
38 u8 request, requesttype;
Florian Mickler26b72c62011-03-20 18:50:48 -030039 u8 *buf;
40
41
Antti Palosaari2bf290b2009-11-13 22:38:55 -030042
43 switch (req->cmd) {
44 case DOWNLOAD_FIRMWARE:
45 case GPIO:
46 case WRITE_I2C:
47 case STREAMING_CTRL:
48 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
49 request = req->cmd;
50 break;
51 case READ_I2C:
52 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
53 request = req->cmd;
54 break;
55 case GET_CONFIG:
56 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
57 request = CONFIG;
58 break;
59 case SET_CONFIG:
60 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
61 request = CONFIG;
62 break;
63 case WRITE_DEMOD:
64 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
65 request = DEMOD_RW;
66 break;
67 case READ_DEMOD:
68 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
69 request = DEMOD_RW;
70 break;
71 default:
72 err("unknown command:%02x", req->cmd);
73 ret = -EPERM;
74 goto error;
75 }
76
Florian Mickler26b72c62011-03-20 18:50:48 -030077 buf = kmalloc(req->size, GFP_KERNEL);
78 if (!buf) {
79 ret = -ENOMEM;
80 goto error;
81 }
82
Antti Palosaari2bf290b2009-11-13 22:38:55 -030083 if (requesttype == (USB_TYPE_VENDOR | USB_DIR_OUT)) {
84 /* write */
85 memcpy(buf, req->data, req->size);
86 pipe = usb_sndctrlpipe(udev, 0);
87 } else {
88 /* read */
89 pipe = usb_rcvctrlpipe(udev, 0);
90 }
91
92 msleep(1); /* avoid I2C errors */
93
94 ret = usb_control_msg(udev, pipe, request, requesttype, req->value,
Florian Mickler26b72c62011-03-20 18:50:48 -030095 req->index, buf, req->size, EC168_USB_TIMEOUT);
Antti Palosaari2bf290b2009-11-13 22:38:55 -030096
97 ec168_debug_dump(request, requesttype, req->value, req->index, buf,
98 req->size, deb_xfer);
99
100 if (ret < 0)
Florian Mickler26b72c62011-03-20 18:50:48 -0300101 goto err_dealloc;
Antti Palosaari2bf290b2009-11-13 22:38:55 -0300102 else
103 ret = 0;
104
105 /* read request, copy returned data to return buf */
106 if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
107 memcpy(req->data, buf, req->size);
108
Florian Mickler26b72c62011-03-20 18:50:48 -0300109 kfree(buf);
Antti Palosaari2bf290b2009-11-13 22:38:55 -0300110 return ret;
Florian Mickler26b72c62011-03-20 18:50:48 -0300111
112err_dealloc:
113 kfree(buf);
Antti Palosaari2bf290b2009-11-13 22:38:55 -0300114error:
115 deb_info("%s: failed:%d\n", __func__, ret);
116 return ret;
117}
118
119static int ec168_ctrl_msg(struct dvb_usb_device *d, struct ec168_req *req)
120{
121 return ec168_rw_udev(d->udev, req);
122}
123
124/* I2C */
125static int ec168_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
126 int num)
127{
128 struct dvb_usb_device *d = i2c_get_adapdata(adap);
129 struct ec168_req req;
130 int i = 0;
131 int ret;
132
133 if (num > 2)
134 return -EINVAL;
135
136 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
137 return -EAGAIN;
138
139 while (i < num) {
140 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
141 if (msg[i].addr == ec168_ec100_config.demod_address) {
142 req.cmd = READ_DEMOD;
143 req.value = 0;
144 req.index = 0xff00 + msg[i].buf[0]; /* reg */
145 req.size = msg[i+1].len; /* bytes to read */
146 req.data = &msg[i+1].buf[0];
147 ret = ec168_ctrl_msg(d, &req);
148 i += 2;
149 } else {
150 err("I2C read not implemented");
151 ret = -ENOSYS;
152 i += 2;
153 }
154 } else {
155 if (msg[i].addr == ec168_ec100_config.demod_address) {
156 req.cmd = WRITE_DEMOD;
157 req.value = msg[i].buf[1]; /* val */
158 req.index = 0xff00 + msg[i].buf[0]; /* reg */
159 req.size = 0;
160 req.data = NULL;
161 ret = ec168_ctrl_msg(d, &req);
162 i += 1;
163 } else {
164 req.cmd = WRITE_I2C;
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);
170 i += 1;
171 }
172 }
173 if (ret)
174 goto error;
175
176 }
177 ret = i;
178
179error:
180 mutex_unlock(&d->i2c_mutex);
181 return i;
182}
183
184
185static u32 ec168_i2c_func(struct i2c_adapter *adapter)
186{
187 return I2C_FUNC_I2C;
188}
189
190static struct i2c_algorithm ec168_i2c_algo = {
191 .master_xfer = ec168_i2c_xfer,
192 .functionality = ec168_i2c_func,
193};
194
195/* Callbacks for DVB USB */
196static struct ec100_config ec168_ec100_config = {
197 .demod_address = 0xff, /* not real address, demod is integrated */
198};
199
200static int ec168_ec100_frontend_attach(struct dvb_usb_adapter *adap)
201{
202 deb_info("%s:\n", __func__);
Michael Krufky77eed212011-09-06 09:31:57 -0300203 adap->fe_adap[0].fe = dvb_attach(ec100_attach, &ec168_ec100_config,
Antti Palosaari2bf290b2009-11-13 22:38:55 -0300204 &adap->dev->i2c_adap);
Michael Krufky77eed212011-09-06 09:31:57 -0300205 if (adap->fe_adap[0].fe == NULL)
Antti Palosaari2bf290b2009-11-13 22:38:55 -0300206 return -ENODEV;
207
208 return 0;
209}
210
211static struct mxl5005s_config ec168_mxl5003s_config = {
212 .i2c_address = 0xc6,
213 .if_freq = IF_FREQ_4570000HZ,
214 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
215 .agc_mode = MXL_SINGLE_AGC,
216 .tracking_filter = MXL_TF_OFF,
217 .rssi_enable = MXL_RSSI_ENABLE,
218 .cap_select = MXL_CAP_SEL_ENABLE,
219 .div_out = MXL_DIV_OUT_4,
220 .clock_out = MXL_CLOCK_OUT_DISABLE,
221 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
222 .top = MXL5005S_TOP_25P2,
223 .mod_mode = MXL_DIGITAL_MODE,
224 .if_mode = MXL_ZERO_IF,
225 .AgcMasterByte = 0x00,
226};
227
228static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
229{
230 deb_info("%s:\n", __func__);
Michael Krufky77eed212011-09-06 09:31:57 -0300231 return dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe, &adap->dev->i2c_adap,
Antti Palosaari2bf290b2009-11-13 22:38:55 -0300232 &ec168_mxl5003s_config) == NULL ? -ENODEV : 0;
233}
234
235static int ec168_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
236{
237 struct ec168_req req = {STREAMING_CTRL, 0x7f01, 0x0202, 0, NULL};
238 deb_info("%s: onoff:%d\n", __func__, onoff);
239 if (onoff)
240 req.index = 0x0102;
241 return ec168_ctrl_msg(adap->dev, &req);
242}
243
244static int ec168_download_firmware(struct usb_device *udev,
245 const struct firmware *fw)
246{
247 int i, len, packets, remainder, ret;
248 u16 addr = 0x0000; /* firmware start address */
249 struct ec168_req req = {DOWNLOAD_FIRMWARE, 0, 0, 0, NULL};
250 deb_info("%s:\n", __func__);
251
252 #define FW_PACKET_MAX_DATA 2048
253 packets = fw->size / FW_PACKET_MAX_DATA;
254 remainder = fw->size % FW_PACKET_MAX_DATA;
255 len = FW_PACKET_MAX_DATA;
256 for (i = 0; i <= packets; i++) {
257 if (i == packets) /* set size of the last packet */
258 len = remainder;
259
260 req.size = len;
261 req.data = (u8 *)(fw->data + i * FW_PACKET_MAX_DATA);
262 req.index = addr;
263 addr += FW_PACKET_MAX_DATA;
264
265 ret = ec168_rw_udev(udev, &req);
266 if (ret) {
267 err("firmware download failed:%d packet:%d", ret, i);
268 goto error;
269 }
270 }
271 req.size = 0;
272
273 /* set "warm"? */
274 req.cmd = SET_CONFIG;
275 req.value = 0;
276 req.index = 0x0001;
277 ret = ec168_rw_udev(udev, &req);
278 if (ret)
279 goto error;
280
281 /* really needed - no idea what does */
282 req.cmd = GPIO;
283 req.value = 0;
284 req.index = 0x0206;
285 ret = ec168_rw_udev(udev, &req);
286 if (ret)
287 goto error;
288
289 /* activate tuner I2C? */
290 req.cmd = WRITE_I2C;
291 req.value = 0;
292 req.index = 0x00c6;
293 ret = ec168_rw_udev(udev, &req);
294 if (ret)
295 goto error;
296
297 return ret;
298error:
299 deb_info("%s: failed:%d\n", __func__, ret);
300 return ret;
301}
302
303static int ec168_identify_state(struct usb_device *udev,
304 struct dvb_usb_device_properties *props,
305 struct dvb_usb_device_description **desc, int *cold)
306{
307 int ret;
308 u8 reply;
309 struct ec168_req req = {GET_CONFIG, 0, 1, sizeof(reply), &reply};
310 deb_info("%s:\n", __func__);
311
312 ret = ec168_rw_udev(udev, &req);
313 if (ret)
314 goto error;
315
316 deb_info("%s: reply:%02x\n", __func__, reply);
317
318 if (reply == 0x01)
319 *cold = 0;
320 else
321 *cold = 1;
322
323 return ret;
324error:
325 deb_info("%s: failed:%d\n", __func__, ret);
326 return ret;
327}
328
329/* DVB USB Driver stuff */
330static struct dvb_usb_device_properties ec168_properties;
331
332static int ec168_probe(struct usb_interface *intf,
333 const struct usb_device_id *id)
334{
335 int ret;
336 deb_info("%s: interface:%d\n", __func__,
337 intf->cur_altsetting->desc.bInterfaceNumber);
338
339 ret = dvb_usb_device_init(intf, &ec168_properties, THIS_MODULE, NULL,
340 adapter_nr);
341 if (ret)
342 goto error;
343
344 return ret;
345error:
346 deb_info("%s: failed:%d\n", __func__, ret);
347 return ret;
348}
349
350#define E3C_EC168_1689 0
351#define E3C_EC168_FFFA 1
352#define E3C_EC168_FFFB 2
353#define E3C_EC168_1001 3
354#define E3C_EC168_1002 4
355
356static struct usb_device_id ec168_id[] = {
357 [E3C_EC168_1689] =
358 {USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168)},
359 [E3C_EC168_FFFA] =
360 {USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_2)},
361 [E3C_EC168_FFFB] =
362 {USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_3)},
363 [E3C_EC168_1001] =
364 {USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_4)},
365 [E3C_EC168_1002] =
366 {USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_5)},
367 {} /* terminating entry */
368};
369
370MODULE_DEVICE_TABLE(usb, ec168_id);
371
372static struct dvb_usb_device_properties ec168_properties = {
373 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
374
375 .usb_ctrl = DEVICE_SPECIFIC,
376 .download_firmware = ec168_download_firmware,
377 .firmware = "dvb-usb-ec168.fw",
378 .no_reconnect = 1,
379
380 .size_of_priv = 0,
381
382 .num_adapters = 1,
383 .adapter = {
384 {
Michael Krufky77eed212011-09-06 09:31:57 -0300385 .num_frontends = 1,
386 .fe = {{
Antti Palosaari2bf290b2009-11-13 22:38:55 -0300387 .streaming_ctrl = ec168_streaming_ctrl,
388 .frontend_attach = ec168_ec100_frontend_attach,
389 .tuner_attach = ec168_mxl5003s_tuner_attach,
390 .stream = {
391 .type = USB_BULK,
392 .count = 6,
393 .endpoint = 0x82,
394 .u = {
395 .bulk = {
396 .buffersize = (32*512),
397 }
398 }
399 },
Michael Krufky77eed212011-09-06 09:31:57 -0300400 }},
Antti Palosaari2bf290b2009-11-13 22:38:55 -0300401 }
402 },
403
404 .identify_state = ec168_identify_state,
405
406 .i2c_algo = &ec168_i2c_algo,
407
408 .num_device_descs = 1,
409 .devices = {
410 {
411 .name = "E3C EC168 DVB-T USB2.0 reference design",
412 .cold_ids = {
413 &ec168_id[E3C_EC168_1689],
414 &ec168_id[E3C_EC168_FFFA],
415 &ec168_id[E3C_EC168_FFFB],
416 &ec168_id[E3C_EC168_1001],
417 &ec168_id[E3C_EC168_1002],
418 NULL},
419 .warm_ids = {NULL},
420 },
421 }
422};
423
424static struct usb_driver ec168_driver = {
425 .name = "dvb_usb_ec168",
426 .probe = ec168_probe,
427 .disconnect = dvb_usb_device_exit,
428 .id_table = ec168_id,
429};
430
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800431module_usb_driver(ec168_driver);
Antti Palosaari2bf290b2009-11-13 22:38:55 -0300432
433MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
434MODULE_DESCRIPTION("E3C EC168 DVB-T USB2.0 driver");
435MODULE_LICENSE("GPL");