blob: ba3f11f6febb88d5482fbdcf76d4308fe98a1bd5 [file] [log] [blame]
Malcolm Priestleyf6d87352011-07-25 15:35:03 -03001/* DVB USB compliant linux driver for IT9137
2 *
3 * Copyright (C) 2011 Malcolm Priestley (tvboxspy@gmail.com)
4 * IT9137 (C) ITE Tech Inc.
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 Version 2, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 *
20 * see Documentation/dvb/README.dvb-usb for more information
21 * see Documentation/dvb/it9137.txt for firmware information
22 *
23 */
24#define DVB_USB_LOG_PREFIX "it913x"
25
26#include <linux/usb.h>
27#include <linux/usb/input.h>
28#include <media/rc-core.h>
29
30#include "dvb-usb.h"
31#include "it913x-fe.h"
32
33/* debug */
34static int dvb_usb_it913x_debug;
35#define l_dprintk(var, level, args...) do { \
36 if ((var >= level)) \
37 printk(KERN_DEBUG DVB_USB_LOG_PREFIX ": " args); \
38} while (0)
39
40#define deb_info(level, args...) l_dprintk(dvb_usb_it913x_debug, level, args)
41#define debug_data_snipet(level, name, p) \
42 deb_info(level, name" (%02x%02x%02x%02x%02x%02x%02x%02x)", \
43 *p, *(p+1), *(p+2), *(p+3), *(p+4), \
44 *(p+5), *(p+6), *(p+7));
45
46
47module_param_named(debug, dvb_usb_it913x_debug, int, 0644);
48MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able))."
49 DVB_USB_DEBUG_STATUS);
50
51static int pid_filter;
52module_param_named(pid, pid_filter, int, 0644);
53MODULE_PARM_DESC(pid, "set default 0=on 1=off");
54
Malcolm Priestley5e642c02011-11-30 17:16:09 -030055static int dvb_usb_it913x_firmware;
56module_param_named(firmware, dvb_usb_it913x_firmware, int, 0644);
57MODULE_PARM_DESC(firmware, "set firmware 0=auto 1=IT9137 2=IT9135V1");
58
59
Malcolm Priestleyf6d87352011-07-25 15:35:03 -030060int cmd_counter;
61
62DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
63
64struct it913x_state {
65 u8 id;
Malcolm Priestley50815702011-12-04 08:20:37 -030066 struct ite_config it913x_config;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -030067};
68
Malcolm Priestleybc549192011-10-14 19:54:11 -030069struct ite_config it913x_config;
70
Malcolm Priestley15157c52011-12-01 17:35:48 -030071#define IT913X_RETRY 10
72#define IT913X_SND_TIMEOUT 100
73#define IT913X_RCV_TIMEOUT 200
74
Malcolm Priestleyf6d87352011-07-25 15:35:03 -030075static int it913x_bulk_write(struct usb_device *dev,
76 u8 *snd, int len, u8 pipe)
77{
Malcolm Priestley15157c52011-12-01 17:35:48 -030078 int ret, actual_l, i;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -030079
Malcolm Priestley15157c52011-12-01 17:35:48 -030080 for (i = 0; i < IT913X_RETRY; i++) {
81 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
82 snd, len , &actual_l, IT913X_SND_TIMEOUT);
83 if (ret == 0 || ret != -EBUSY || ret != -ETIMEDOUT)
84 break;
85 }
86
87 if (len != actual_l && ret == 0)
88 ret = -EAGAIN;
89
Malcolm Priestleyf6d87352011-07-25 15:35:03 -030090 return ret;
91}
92
93static int it913x_bulk_read(struct usb_device *dev,
94 u8 *rev, int len, u8 pipe)
95{
Malcolm Priestley15157c52011-12-01 17:35:48 -030096 int ret, actual_l, i;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -030097
Malcolm Priestley15157c52011-12-01 17:35:48 -030098 for (i = 0; i < IT913X_RETRY; i++) {
99 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
100 rev, len , &actual_l, IT913X_RCV_TIMEOUT);
101 if (ret == 0 || ret != -EBUSY || ret != -ETIMEDOUT)
102 break;
103 }
104
105 if (len != actual_l && ret == 0)
106 ret = -EAGAIN;
107
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300108 return ret;
109}
110
111static u16 check_sum(u8 *p, u8 len)
112{
113 u16 sum = 0;
114 u8 i = 1;
115 while (i < len)
116 sum += (i++ & 1) ? (*p++) << 8 : *p++;
117 return ~sum;
118}
119
Malcolm Priestley15157c52011-12-01 17:35:48 -0300120static int it913x_usb_talk(struct usb_device *udev, u8 mode, u8 pro,
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300121 u8 cmd, u32 reg, u8 addr, u8 *data, u8 len)
122{
123 int ret = 0, i, buf_size = 1;
124 u8 *buff;
125 u8 rlen;
126 u16 chk_sum;
127
128 buff = kzalloc(256, GFP_KERNEL);
129 if (!buff) {
130 info("USB Buffer Failed");
131 return -ENOMEM;
132 }
133
134 buff[buf_size++] = pro;
135 buff[buf_size++] = cmd;
136 buff[buf_size++] = cmd_counter;
137
138 switch (mode) {
139 case READ_LONG:
140 case WRITE_LONG:
141 buff[buf_size++] = len;
142 buff[buf_size++] = 2;
143 buff[buf_size++] = (reg >> 24);
144 buff[buf_size++] = (reg >> 16) & 0xff;
145 buff[buf_size++] = (reg >> 8) & 0xff;
146 buff[buf_size++] = reg & 0xff;
147 break;
148 case READ_SHORT:
149 buff[buf_size++] = addr;
150 break;
151 case WRITE_SHORT:
152 buff[buf_size++] = len;
153 buff[buf_size++] = addr;
154 buff[buf_size++] = (reg >> 8) & 0xff;
155 buff[buf_size++] = reg & 0xff;
156 break;
157 case READ_DATA:
158 case WRITE_DATA:
159 break;
160 case WRITE_CMD:
161 mode = 7;
162 break;
163 default:
164 kfree(buff);
165 return -EINVAL;
166 }
167
168 if (mode & 1) {
169 for (i = 0; i < len ; i++)
170 buff[buf_size++] = data[i];
171 }
172 chk_sum = check_sum(&buff[1], buf_size);
173
174 buff[buf_size++] = chk_sum >> 8;
175 buff[0] = buf_size;
176 buff[buf_size++] = (chk_sum & 0xff);
177
178 ret = it913x_bulk_write(udev, buff, buf_size , 0x02);
Malcolm Priestley15157c52011-12-01 17:35:48 -0300179 if (ret < 0)
180 goto error;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300181
Malcolm Priestley15157c52011-12-01 17:35:48 -0300182 ret = it913x_bulk_read(udev, buff, (mode & 1) ?
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300183 5 : len + 5 , 0x01);
Malcolm Priestley15157c52011-12-01 17:35:48 -0300184 if (ret < 0)
185 goto error;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300186
187 rlen = (mode & 0x1) ? 0x1 : len;
188
189 if (mode & 1)
Malcolm Priestley15157c52011-12-01 17:35:48 -0300190 ret = buff[2];
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300191 else
192 memcpy(data, &buff[3], rlen);
193
194 cmd_counter++;
195
Malcolm Priestley15157c52011-12-01 17:35:48 -0300196error: kfree(buff);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300197
Malcolm Priestley15157c52011-12-01 17:35:48 -0300198 return ret;
199}
200
201static int it913x_io(struct usb_device *udev, u8 mode, u8 pro,
202 u8 cmd, u32 reg, u8 addr, u8 *data, u8 len)
203{
204 int ret, i;
205
206 for (i = 0; i < IT913X_RETRY; i++) {
207 ret = it913x_usb_talk(udev, mode, pro,
208 cmd, reg, addr, data, len);
209 if (ret != -EAGAIN)
210 break;
211 }
212
213 return ret;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300214}
215
216static int it913x_wr_reg(struct usb_device *udev, u8 pro, u32 reg , u8 data)
217{
218 int ret;
219 u8 b[1];
220 b[0] = data;
221 ret = it913x_io(udev, WRITE_LONG, pro,
222 CMD_DEMOD_WRITE, reg, 0, b, sizeof(b));
223
224 return ret;
225}
226
227static int it913x_read_reg(struct usb_device *udev, u32 reg)
228{
229 int ret;
230 u8 data[1];
231
232 ret = it913x_io(udev, READ_LONG, DEV_0,
233 CMD_DEMOD_READ, reg, 0, &data[0], 1);
234
235 return (ret < 0) ? ret : data[0];
236}
237
238static u32 it913x_query(struct usb_device *udev, u8 pro)
239{
240 int ret;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300241 u8 data[4];
242 ret = it913x_io(udev, READ_LONG, pro, CMD_DEMOD_READ,
Malcolm Priestleybc549192011-10-14 19:54:11 -0300243 0x1222, 0, &data[0], 3);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300244
Malcolm Priestleybc549192011-10-14 19:54:11 -0300245 it913x_config.chip_ver = data[0];
246 it913x_config.chip_type = (u16)(data[2] << 8) + data[1];
247
248 info("Chip Version=%02x Chip Type=%04x", it913x_config.chip_ver,
249 it913x_config.chip_type);
250
251 ret |= it913x_io(udev, READ_SHORT, pro,
252 CMD_QUERYINFO, 0, 0x1, &data[0], 4);
253
254 it913x_config.firmware = (data[0] << 24) + (data[1] << 16) +
255 (data[2] << 8) + data[3];
256
257 return (ret < 0) ? 0 : it913x_config.firmware;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300258}
259
260static int it913x_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
261{
262 int ret = 0;
263 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
264
265 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
266 return -EAGAIN;
267 deb_info(1, "PID_C (%02x)", onoff);
268
269 if (!onoff)
270 ret = it913x_wr_reg(adap->dev->udev, pro, PID_RST, 0x1);
271
272 mutex_unlock(&adap->dev->i2c_mutex);
273 return ret;
274}
275
276static int it913x_pid_filter(struct dvb_usb_adapter *adap,
277 int index, u16 pid, int onoff)
278{
279 struct usb_device *udev = adap->dev->udev;
280 int ret = 0;
281 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
282
283 if (pid_filter > 0)
284 return 0;
285
286 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
287 return -EAGAIN;
288 deb_info(1, "PID_F (%02x)", onoff);
289 if (onoff) {
290 ret = it913x_wr_reg(udev, pro, PID_EN, 0x1);
291
292 ret |= it913x_wr_reg(udev, pro, PID_LSB, (u8)(pid & 0xff));
293
294 ret |= it913x_wr_reg(udev, pro, PID_MSB, (u8)(pid >> 8));
295
296 ret |= it913x_wr_reg(udev, pro, PID_INX_EN, (u8)onoff);
297
298 ret |= it913x_wr_reg(udev, pro, PID_INX, (u8)(index & 0x1f));
299
300 }
301
302 mutex_unlock(&adap->dev->i2c_mutex);
303 return 0;
304}
305
306
307static int it913x_return_status(struct usb_device *udev)
308{
309 u32 firm = 0;
310
311 firm = it913x_query(udev, DEV_0);
312 if (firm > 0)
313 info("Firmware Version %d", firm);
314
315 return (firm > 0) ? firm : 0;
316}
317
318static int it913x_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
319 int num)
320{
321 struct dvb_usb_device *d = i2c_get_adapdata(adap);
322 static u8 data[256];
323 int ret;
324 u32 reg;
325 u8 pro;
326 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
327 return -EAGAIN;
328
329 debug_data_snipet(1, "Message out", msg[0].buf);
330 deb_info(2, "num of messages %d address %02x", num, msg[0].addr);
331
332 pro = (msg[0].addr & 0x2) ? DEV_0_DMOD : 0x0;
333 pro |= (msg[0].addr & 0x20) ? DEV_1 : DEV_0;
334 memcpy(data, msg[0].buf, msg[0].len);
335 reg = (data[0] << 24) + (data[1] << 16) +
336 (data[2] << 8) + data[3];
337 if (num == 2) {
338 ret = it913x_io(d->udev, READ_LONG, pro,
339 CMD_DEMOD_READ, reg, 0, data, msg[1].len);
340 memcpy(msg[1].buf, data, msg[1].len);
341 } else
342 ret = it913x_io(d->udev, WRITE_LONG, pro, CMD_DEMOD_WRITE,
343 reg, 0, &data[4], msg[0].len - 4);
344
345 mutex_unlock(&d->i2c_mutex);
346
347 return ret;
348}
349
350static u32 it913x_i2c_func(struct i2c_adapter *adapter)
351{
352 return I2C_FUNC_I2C;
353}
354
355static struct i2c_algorithm it913x_i2c_algo = {
356 .master_xfer = it913x_i2c_xfer,
357 .functionality = it913x_i2c_func,
358};
359
360/* Callbacks for DVB USB */
tvboxspy2ba0f942011-09-21 18:57:41 -0300361#define IT913X_POLL 250
362static int it913x_rc_query(struct dvb_usb_device *d)
363{
364 u8 ibuf[4];
365 int ret;
366 u32 key;
367 /* Avoid conflict with frontends*/
368 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
369 return -EAGAIN;
370
371 ret = it913x_io(d->udev, READ_LONG, PRO_LINK, CMD_IR_GET,
372 0, 0, &ibuf[0], sizeof(ibuf));
373
374 if ((ibuf[2] + ibuf[3]) == 0xff) {
375 key = ibuf[2];
Malcolm Priestleyc725ff62011-11-28 18:22:41 -0300376 key += ibuf[0] << 16;
377 key += ibuf[1] << 8;
378 deb_info(1, "NEC Extended Key =%08x", key);
tvboxspy2ba0f942011-09-21 18:57:41 -0300379 if (d->rc_dev != NULL)
380 rc_keydown(d->rc_dev, key, 0);
381 }
Malcolm Priestleyc725ff62011-11-28 18:22:41 -0300382
tvboxspy2ba0f942011-09-21 18:57:41 -0300383 mutex_unlock(&d->i2c_mutex);
384
385 return ret;
386}
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300387
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300388/* Firmware sets raw */
389const char fw_it9135_v1[] = "dvb-usb-it9135-01.fw";
390const char fw_it9137[] = "dvb-usb-it9137-01.fw";
391
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300392static int ite_firmware_select(struct usb_device *udev,
393 struct dvb_usb_device_properties *props)
394{
395 int sw;
396 /* auto switch */
397 if (le16_to_cpu(udev->descriptor.idProduct) ==
398 USB_PID_ITETECH_IT9135)
399 sw = IT9135_V1_FW;
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300400 else if (le16_to_cpu(udev->descriptor.idProduct) ==
401 USB_PID_ITETECH_IT9135_9005)
402 sw = IT9135_V1_FW;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300403 else
404 sw = IT9137_FW;
405
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300406 /* force switch */
407 if (dvb_usb_it913x_firmware != IT9135_AUTO)
408 sw = dvb_usb_it913x_firmware;
409
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300410 switch (sw) {
411 case IT9135_V1_FW:
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300412 it913x_config.firmware_ver = 1;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300413 it913x_config.adc_x2 = 1;
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300414 props->firmware = fw_it9135_v1;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300415 break;
416 case IT9137_FW:
417 default:
418 it913x_config.firmware_ver = 0;
419 it913x_config.adc_x2 = 0;
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300420 props->firmware = fw_it9137;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300421 }
422
423 return 0;
424}
425
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300426#define TS_MPEG_PKT_SIZE 188
427#define EP_LOW 21
428#define TS_BUFFER_SIZE_PID (EP_LOW*TS_MPEG_PKT_SIZE)
429#define EP_HIGH 348
430#define TS_BUFFER_SIZE_MAX (EP_HIGH*TS_MPEG_PKT_SIZE)
431
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300432static int it913x_identify_state(struct usb_device *udev,
433 struct dvb_usb_device_properties *props,
434 struct dvb_usb_device_description **desc,
435 int *cold)
436{
437 int ret = 0, firm_no;
Malcolm Priestleybc549192011-10-14 19:54:11 -0300438 u8 reg, remote;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300439
440 firm_no = it913x_return_status(udev);
441
Malcolm Priestleybc549192011-10-14 19:54:11 -0300442 /* checnk for dual mode */
443 it913x_config.dual_mode = it913x_read_reg(udev, 0x49c5);
444
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300445 if (udev->speed != USB_SPEED_HIGH) {
446 props->adapter[0].fe[0].pid_filter_count = 5;
447 info("USB 1 low speed mode - connect to USB 2 port");
448 if (pid_filter > 0)
449 pid_filter = 0;
450 if (it913x_config.dual_mode) {
451 it913x_config.dual_mode = 0;
452 info("Dual mode not supported in USB 1");
453 }
454 } else /* For replugging */
455 if(props->adapter[0].fe[0].pid_filter_count == 5)
456 props->adapter[0].fe[0].pid_filter_count = 31;
457
Malcolm Priestleybc549192011-10-14 19:54:11 -0300458 /* TODO different remotes */
459 remote = it913x_read_reg(udev, 0x49ac); /* Remote */
460 if (remote == 0)
461 props->rc.core.rc_codes = NULL;
462
463 /* TODO at the moment tuner_id is always assigned to 0x38 */
464 it913x_config.tuner_id_0 = it913x_read_reg(udev, 0x49d0);
465
466 info("Dual mode=%x Remote=%x Tuner Type=%x", it913x_config.dual_mode
467 , remote, it913x_config.tuner_id_0);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300468
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300469 /* Select Stream Buffer Size */
470 if (pid_filter)
471 props->adapter[0].fe[0].stream.u.bulk.buffersize =
472 TS_BUFFER_SIZE_MAX;
473 else
474 props->adapter[0].fe[0].stream.u.bulk.buffersize =
475 TS_BUFFER_SIZE_PID;
Malcolm Priestleyf36472d2011-12-11 17:09:46 -0300476 if (it913x_config.dual_mode) {
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300477 props->adapter[1].fe[0].stream.u.bulk.buffersize =
478 props->adapter[0].fe[0].stream.u.bulk.buffersize;
Malcolm Priestleyf36472d2011-12-11 17:09:46 -0300479 props->num_adapters = 2;
480 } else
481 props->num_adapters = 1;
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300482
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300483 ret = ite_firmware_select(udev, props);
484
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300485 if (firm_no > 0) {
486 *cold = 0;
487 return 0;
488 }
489
Malcolm Priestleybc549192011-10-14 19:54:11 -0300490 if (it913x_config.dual_mode) {
491 it913x_config.tuner_id_1 = it913x_read_reg(udev, 0x49e0);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300492 ret = it913x_wr_reg(udev, DEV_0, GPIOH1_EN, 0x1);
493 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_ON, 0x1);
494 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300495 msleep(50);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300496 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x0);
497 msleep(50);
498 reg = it913x_read_reg(udev, GPIOH1_O);
499 if (reg == 0) {
500 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1);
501 ret |= it913x_return_status(udev);
502 if (ret != 0)
503 ret = it913x_wr_reg(udev, DEV_0,
504 GPIOH1_O, 0x0);
505 }
Malcolm Priestleyf36472d2011-12-11 17:09:46 -0300506 }
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300507
508 reg = it913x_read_reg(udev, IO_MUX_POWER_CLK);
509
Malcolm Priestleybc549192011-10-14 19:54:11 -0300510 if (it913x_config.dual_mode) {
511 ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, CHIP2_I2C_ADDR);
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300512 if (it913x_config.firmware_ver == 1)
513 ret |= it913x_wr_reg(udev, DEV_0, 0xcfff, 0x1);
514 else
515 ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x1);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300516 } else {
517 ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, 0x0);
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300518 if (it913x_config.firmware_ver == 1)
519 ret |= it913x_wr_reg(udev, DEV_0, 0xcfff, 0x0);
520 else
521 ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x0);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300522 }
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300523
524 *cold = 1;
525
526 return (ret < 0) ? -ENODEV : 0;
527}
528
529static int it913x_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
530{
531 int ret = 0;
532 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
533
534 if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
535 return -EAGAIN;
536 deb_info(1, "STM (%02x)", onoff);
537
538 if (!onoff)
539 ret = it913x_wr_reg(adap->dev->udev, pro, PID_RST, 0x1);
540
541
542 mutex_unlock(&adap->dev->i2c_mutex);
543
544 return ret;
545}
546
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300547static int it913x_download_firmware(struct usb_device *udev,
548 const struct firmware *fw)
549{
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300550 int ret = 0, i = 0, pos = 0;
551 u8 packet_size;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300552 u8 *fw_data;
553
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300554 ret = it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_100);
555
556 info("FRM Starting Firmware Download");
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300557
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300558 /* Multi firmware loader */
559 /* This uses scatter write firmware headers */
560 /* The firmware must start with 03 XX 00 */
561 /* and be the extact firmware length */
562
563 while (i <= fw->size) {
564 if (((fw->data[i] == 0x3) && (fw->data[i + 2] == 0x0))
565 || (i == fw->size)) {
566 packet_size = i - pos;
567 if ((packet_size > 0x19) || (i == fw->size)) {
568 fw_data = (u8 *)(fw->data + pos);
569 pos += packet_size;
570 if (packet_size > 0)
571 ret |= it913x_io(udev, WRITE_DATA,
572 DEV_0, CMD_SCATTER_WRITE, 0,
573 0, fw_data, packet_size);
574 udelay(1000);
575 }
576 }
577 i++;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300578 }
579
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300580 ret |= it913x_io(udev, WRITE_CMD, DEV_0, CMD_BOOT, 0, 0, NULL, 0);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300581
582 msleep(100);
583
584 if (ret < 0)
585 info("FRM Firmware Download Failed (%04x)" , ret);
586 else
587 info("FRM Firmware Download Completed - Resetting Device");
588
589 ret |= it913x_return_status(udev);
590
591 msleep(30);
592
593 ret |= it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_400);
594
595 /* Tuner function */
Malcolm Priestleybc549192011-10-14 19:54:11 -0300596 if (it913x_config.dual_mode)
597 ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0xa0);
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300598 else
599 ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0x68);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300600
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300601 if ((it913x_config.chip_ver == 1) &&
602 (it913x_config.chip_type == 0x9135)) {
603 ret |= it913x_wr_reg(udev, DEV_0, PADODPU, 0x0);
604 ret |= it913x_wr_reg(udev, DEV_0, AGC_O_D, 0x0);
605 if (it913x_config.dual_mode) {
606 ret |= it913x_wr_reg(udev, DEV_1, PADODPU, 0x0);
607 ret |= it913x_wr_reg(udev, DEV_1, AGC_O_D, 0x0);
608 }
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300609 }
610
611 return (ret < 0) ? -ENODEV : 0;
612}
613
614static int it913x_name(struct dvb_usb_adapter *adap)
615{
616 const char *desc = adap->dev->desc->name;
617 char *fe_name[] = {"_1", "_2", "_3", "_4"};
Michael Krufky77eed212011-09-06 09:31:57 -0300618 char *name = adap->fe_adap[0].fe->ops.info.name;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300619
620 strlcpy(name, desc, 128);
621 strlcat(name, fe_name[adap->id], 128);
622
623 return 0;
624}
625
626static int it913x_frontend_attach(struct dvb_usb_adapter *adap)
627{
628 struct usb_device *udev = adap->dev->udev;
Malcolm Priestley50815702011-12-04 08:20:37 -0300629 struct it913x_state *st = adap->dev->priv;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300630 int ret = 0;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300631 u8 adap_addr = I2C_BASE_ADDR + (adap->id << 5);
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300632 u16 ep_size = adap->props.fe[0].stream.u.bulk.buffersize / 4;
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300633 u8 pkt_size = 0x80;
634
635 if (adap->dev->udev->speed != USB_SPEED_HIGH)
636 pkt_size = 0x10;
Malcolm Priestleybc549192011-10-14 19:54:11 -0300637
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300638 it913x_config.adf = it913x_read_reg(udev, IO_MUX_POWER_CLK);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300639
Malcolm Priestley50815702011-12-04 08:20:37 -0300640 if (adap->id == 0)
641 memcpy(&st->it913x_config, &it913x_config,
642 sizeof(struct ite_config));
643
Michael Krufky77eed212011-09-06 09:31:57 -0300644 adap->fe_adap[0].fe = dvb_attach(it913x_fe_attach,
Malcolm Priestley50815702011-12-04 08:20:37 -0300645 &adap->dev->i2c_adap, adap_addr, &st->it913x_config);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300646
Michael Krufky77eed212011-09-06 09:31:57 -0300647 if (adap->id == 0 && adap->fe_adap[0].fe) {
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300648 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2_SW_RST, 0x1);
649 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_SW_RST, 0x1);
650 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x0f);
651 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_NAK, 0x1b);
652 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x2f);
653 ret = it913x_wr_reg(udev, DEV_0, EP4_TX_LEN_LSB,
654 ep_size & 0xff);
655 ret = it913x_wr_reg(udev, DEV_0, EP4_TX_LEN_MSB, ep_size >> 8);
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300656 ret = it913x_wr_reg(udev, DEV_0, EP4_MAX_PKT, pkt_size);
Michael Krufky77eed212011-09-06 09:31:57 -0300657 } else if (adap->id == 1 && adap->fe_adap[0].fe) {
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300658 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x6f);
659 ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_LSB,
660 ep_size & 0xff);
661 ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_MSB, ep_size >> 8);
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300662 ret = it913x_wr_reg(udev, DEV_0, EP5_MAX_PKT, pkt_size);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300663 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_EN, 0x1);
664 ret = it913x_wr_reg(udev, DEV_1_DMOD, MP2IF_SERIAL, 0x1);
665 ret = it913x_wr_reg(udev, DEV_1, TOP_HOSTB_SER_MODE, 0x1);
666 ret = it913x_wr_reg(udev, DEV_0_DMOD, TSIS_ENABLE, 0x1);
667 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2_SW_RST, 0x0);
668 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_SW_RST, 0x0);
669 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_HALF_PSB, 0x0);
670 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF_STOP_EN, 0x1);
671 ret = it913x_wr_reg(udev, DEV_1_DMOD, MPEG_FULL_SPEED, 0x0);
672 ret = it913x_wr_reg(udev, DEV_1_DMOD, MP2IF_STOP_EN, 0x0);
673 } else
674 return -ENODEV;
675
676 ret = it913x_name(adap);
677
678 return ret;
679}
680
681/* DVB USB Driver */
682static struct dvb_usb_device_properties it913x_properties;
683
684static int it913x_probe(struct usb_interface *intf,
685 const struct usb_device_id *id)
686{
687 cmd_counter = 0;
688 if (0 == dvb_usb_device_init(intf, &it913x_properties,
689 THIS_MODULE, NULL, adapter_nr)) {
690 info("DEV registering device driver");
691 return 0;
692 }
693
694 info("DEV it913x Error");
695 return -ENODEV;
696
697}
698
699static struct usb_device_id it913x_table[] = {
700 { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09) },
Malcolm Priestleybc549192011-10-14 19:54:11 -0300701 { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135) },
Malcolm Priestleyfdb5a912011-11-06 18:30:26 -0300702 { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137) },
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300703 { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005) },
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300704 {} /* Terminating entry */
705};
706
707MODULE_DEVICE_TABLE(usb, it913x_table);
708
709static struct dvb_usb_device_properties it913x_properties = {
710 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
711 .usb_ctrl = DEVICE_SPECIFIC,
712 .download_firmware = it913x_download_firmware,
713 .firmware = "dvb-usb-it9137-01.fw",
714 .no_reconnect = 1,
715 .size_of_priv = sizeof(struct it913x_state),
716 .num_adapters = 2,
717 .adapter = {
718 {
Michael Krufky77eed212011-09-06 09:31:57 -0300719 .num_frontends = 1,
720 .fe = {{
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300721 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
722 DVB_USB_ADAP_NEED_PID_FILTERING|
723 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
724 .streaming_ctrl = it913x_streaming_ctrl,
725 .pid_filter_count = 31,
726 .pid_filter = it913x_pid_filter,
727 .pid_filter_ctrl = it913x_pid_filter_ctrl,
728 .frontend_attach = it913x_frontend_attach,
729 /* parameter for the MPEG2-data transfer */
730 .stream = {
731 .type = USB_BULK,
732 .count = 10,
733 .endpoint = 0x04,
734 .u = {/* Keep Low if PID filter on */
735 .bulk = {
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300736 .buffersize =
737 TS_BUFFER_SIZE_PID,
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300738 }
739 }
740 }
Michael Krufky77eed212011-09-06 09:31:57 -0300741 }},
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300742 },
743 {
Michael Krufky77eed212011-09-06 09:31:57 -0300744 .num_frontends = 1,
745 .fe = {{
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300746 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
747 DVB_USB_ADAP_NEED_PID_FILTERING|
748 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
749 .streaming_ctrl = it913x_streaming_ctrl,
750 .pid_filter_count = 31,
751 .pid_filter = it913x_pid_filter,
752 .pid_filter_ctrl = it913x_pid_filter_ctrl,
753 .frontend_attach = it913x_frontend_attach,
754 /* parameter for the MPEG2-data transfer */
755 .stream = {
756 .type = USB_BULK,
757 .count = 5,
758 .endpoint = 0x05,
759 .u = {
760 .bulk = {
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300761 .buffersize =
762 TS_BUFFER_SIZE_PID,
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300763 }
764 }
765 }
Michael Krufky77eed212011-09-06 09:31:57 -0300766 }},
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300767 }
768 },
769 .identify_state = it913x_identify_state,
tvboxspy2ba0f942011-09-21 18:57:41 -0300770 .rc.core = {
771 .protocol = RC_TYPE_NEC,
772 .module_name = "it913x",
773 .rc_query = it913x_rc_query,
774 .rc_interval = IT913X_POLL,
775 .allowed_protos = RC_TYPE_NEC,
Malcolm Priestleyc725ff62011-11-28 18:22:41 -0300776 .rc_codes = RC_MAP_MSI_DIGIVOX_III,
tvboxspy2ba0f942011-09-21 18:57:41 -0300777 },
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300778 .i2c_algo = &it913x_i2c_algo,
Malcolm Priestleya7187c32011-12-11 12:20:28 -0300779 .num_device_descs = 4,
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300780 .devices = {
781 { "Kworld UB499-2T T09(IT9137)",
782 { &it913x_table[0], NULL },
783 },
Malcolm Priestleybc549192011-10-14 19:54:11 -0300784 { "ITE 9135 Generic",
785 { &it913x_table[1], NULL },
786 },
Malcolm Priestleyfdb5a912011-11-06 18:30:26 -0300787 { "Sveon STV22 Dual DVB-T HDTV(IT9137)",
788 { &it913x_table[2], NULL },
789 },
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300790 { "ITE 9135(9005) Generic",
791 { &it913x_table[3], NULL },
792 },
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300793 }
794};
795
796static struct usb_driver it913x_driver = {
797 .name = "it913x",
798 .probe = it913x_probe,
799 .disconnect = dvb_usb_device_exit,
800 .id_table = it913x_table,
801};
802
803/* module stuff */
804static int __init it913x_module_init(void)
805{
806 int result = usb_register(&it913x_driver);
807 if (result) {
808 err("usb_register failed. Error number %d", result);
809 return result;
810 }
811
812 return 0;
813}
814
815static void __exit it913x_module_exit(void)
816{
817 /* deregister this driver from the USB subsystem */
818 usb_deregister(&it913x_driver);
819}
820
821module_init(it913x_module_init);
822module_exit(it913x_module_exit);
823
824MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
825MODULE_DESCRIPTION("it913x USB 2 Driver");
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300826MODULE_VERSION("1.14");
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300827MODULE_LICENSE("GPL");