blob: bfadf12986cab46d69cc4814a580a12127ff917b [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 Priestleybc549192011-10-14 19:54:11 -030067};
68
69struct 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{
Malcolm Priestleyd4d5a402012-01-02 14:46:32 -0300262 struct usb_device *udev = adap->dev->udev;
263 int ret;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300264 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
265
Malcolm Priestley46f3da92012-02-12 07:29:38 -0300266 mutex_lock(&adap->dev->i2c_mutex);
267
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300268 deb_info(1, "PID_C (%02x)", onoff);
269
Malcolm Priestleyd4d5a402012-01-02 14:46:32 -0300270 ret = it913x_wr_reg(udev, pro, PID_EN, onoff);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300271
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;
Malcolm Priestleyd4d5a402012-01-02 14:46:32 -0300280 int ret;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300281 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
282
Malcolm Priestley46f3da92012-02-12 07:29:38 -0300283 mutex_lock(&adap->dev->i2c_mutex);
284
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300285 deb_info(1, "PID_F (%02x)", onoff);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300286
Malcolm Priestleyd4d5a402012-01-02 14:46:32 -0300287 ret = it913x_wr_reg(udev, pro, PID_LSB, (u8)(pid & 0xff));
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300288
Malcolm Priestleyd4d5a402012-01-02 14:46:32 -0300289 ret |= it913x_wr_reg(udev, pro, PID_MSB, (u8)(pid >> 8));
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300290
Malcolm Priestleyd4d5a402012-01-02 14:46:32 -0300291 ret |= it913x_wr_reg(udev, pro, PID_INX_EN, (u8)onoff);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300292
Malcolm Priestleyd4d5a402012-01-02 14:46:32 -0300293 ret |= it913x_wr_reg(udev, pro, PID_INX, (u8)(index & 0x1f));
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300294
295 mutex_unlock(&adap->dev->i2c_mutex);
296 return 0;
297}
298
299
300static int it913x_return_status(struct usb_device *udev)
301{
302 u32 firm = 0;
303
304 firm = it913x_query(udev, DEV_0);
305 if (firm > 0)
306 info("Firmware Version %d", firm);
307
308 return (firm > 0) ? firm : 0;
309}
310
311static int it913x_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
312 int num)
313{
314 struct dvb_usb_device *d = i2c_get_adapdata(adap);
315 static u8 data[256];
316 int ret;
317 u32 reg;
318 u8 pro;
Malcolm Priestley46f3da92012-02-12 07:29:38 -0300319
320 mutex_lock(&d->i2c_mutex);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300321
322 debug_data_snipet(1, "Message out", msg[0].buf);
323 deb_info(2, "num of messages %d address %02x", num, msg[0].addr);
324
325 pro = (msg[0].addr & 0x2) ? DEV_0_DMOD : 0x0;
326 pro |= (msg[0].addr & 0x20) ? DEV_1 : DEV_0;
327 memcpy(data, msg[0].buf, msg[0].len);
328 reg = (data[0] << 24) + (data[1] << 16) +
329 (data[2] << 8) + data[3];
330 if (num == 2) {
331 ret = it913x_io(d->udev, READ_LONG, pro,
332 CMD_DEMOD_READ, reg, 0, data, msg[1].len);
333 memcpy(msg[1].buf, data, msg[1].len);
334 } else
335 ret = it913x_io(d->udev, WRITE_LONG, pro, CMD_DEMOD_WRITE,
336 reg, 0, &data[4], msg[0].len - 4);
337
338 mutex_unlock(&d->i2c_mutex);
339
340 return ret;
341}
342
343static u32 it913x_i2c_func(struct i2c_adapter *adapter)
344{
345 return I2C_FUNC_I2C;
346}
347
348static struct i2c_algorithm it913x_i2c_algo = {
349 .master_xfer = it913x_i2c_xfer,
350 .functionality = it913x_i2c_func,
351};
352
353/* Callbacks for DVB USB */
tvboxspy2ba0f942011-09-21 18:57:41 -0300354#define IT913X_POLL 250
355static int it913x_rc_query(struct dvb_usb_device *d)
356{
357 u8 ibuf[4];
358 int ret;
359 u32 key;
360 /* Avoid conflict with frontends*/
Malcolm Priestley46f3da92012-02-12 07:29:38 -0300361 mutex_lock(&d->i2c_mutex);
tvboxspy2ba0f942011-09-21 18:57:41 -0300362
363 ret = it913x_io(d->udev, READ_LONG, PRO_LINK, CMD_IR_GET,
364 0, 0, &ibuf[0], sizeof(ibuf));
365
366 if ((ibuf[2] + ibuf[3]) == 0xff) {
367 key = ibuf[2];
Malcolm Priestleyc725ff62011-11-28 18:22:41 -0300368 key += ibuf[0] << 16;
369 key += ibuf[1] << 8;
370 deb_info(1, "NEC Extended Key =%08x", key);
tvboxspy2ba0f942011-09-21 18:57:41 -0300371 if (d->rc_dev != NULL)
372 rc_keydown(d->rc_dev, key, 0);
373 }
Malcolm Priestleyc725ff62011-11-28 18:22:41 -0300374
tvboxspy2ba0f942011-09-21 18:57:41 -0300375 mutex_unlock(&d->i2c_mutex);
376
377 return ret;
378}
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300379
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300380/* Firmware sets raw */
381const char fw_it9135_v1[] = "dvb-usb-it9135-01.fw";
Malcolm Priestley53844c42011-12-12 15:53:00 -0300382const char fw_it9135_v2[] = "dvb-usb-it9135-02.fw";
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300383const char fw_it9137[] = "dvb-usb-it9137-01.fw";
384
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300385static int ite_firmware_select(struct usb_device *udev,
386 struct dvb_usb_device_properties *props)
387{
388 int sw;
389 /* auto switch */
Malcolm Priestleya8ea0212012-01-20 19:07:18 -0300390 if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_KWORLD_2)
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300391 sw = IT9137_FW;
Malcolm Priestleya8ea0212012-01-20 19:07:18 -0300392 else if (it913x_config.chip_ver == 1)
393 sw = IT9135_V1_FW;
394 else
395 sw = IT9135_V2_FW;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300396
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300397 /* force switch */
398 if (dvb_usb_it913x_firmware != IT9135_AUTO)
399 sw = dvb_usb_it913x_firmware;
400
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300401 switch (sw) {
402 case IT9135_V1_FW:
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300403 it913x_config.firmware_ver = 1;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300404 it913x_config.adc_x2 = 1;
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300405 props->firmware = fw_it9135_v1;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300406 break;
Malcolm Priestley53844c42011-12-12 15:53:00 -0300407 case IT9135_V2_FW:
408 it913x_config.firmware_ver = 1;
409 it913x_config.adc_x2 = 1;
410 props->firmware = fw_it9135_v2;
Malcolm Priestleyfc594e32012-01-28 18:02:34 -0300411 switch (it913x_config.tuner_id_0) {
412 case IT9135_61:
413 case IT9135_62:
414 break;
415 default:
416 info("Unknown tuner ID applying default 0x60");
417 case IT9135_60:
Malcolm Priestleya8ea0212012-01-20 19:07:18 -0300418 it913x_config.tuner_id_0 = IT9135_60;
Malcolm Priestleyfc594e32012-01-28 18:02:34 -0300419 }
Malcolm Priestley53844c42011-12-12 15:53:00 -0300420 break;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300421 case IT9137_FW:
422 default:
423 it913x_config.firmware_ver = 0;
424 it913x_config.adc_x2 = 0;
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300425 props->firmware = fw_it9137;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300426 }
427
428 return 0;
429}
430
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300431static void it913x_select_remote(struct usb_device *udev,
432 struct dvb_usb_device_properties *props)
433{
434 switch (le16_to_cpu(udev->descriptor.idProduct)) {
435 case USB_PID_ITETECH_IT9135_9005:
436 props->rc.core.rc_codes = RC_MAP_IT913X_V2;
437 return;
438 default:
439 props->rc.core.rc_codes = RC_MAP_IT913X_V1;
440 }
441 return;
442}
443
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300444#define TS_MPEG_PKT_SIZE 188
445#define EP_LOW 21
446#define TS_BUFFER_SIZE_PID (EP_LOW*TS_MPEG_PKT_SIZE)
447#define EP_HIGH 348
448#define TS_BUFFER_SIZE_MAX (EP_HIGH*TS_MPEG_PKT_SIZE)
449
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300450static int it913x_select_config(struct usb_device *udev,
451 struct dvb_usb_device_properties *props)
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300452{
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300453 int ret = 0, reg;
454 bool proprietary_ir = false;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300455
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300456 if (it913x_config.chip_ver == 0x02
457 && it913x_config.chip_type == 0x9135)
458 reg = it913x_read_reg(udev, 0x461d);
459 else
460 reg = it913x_read_reg(udev, 0x461b);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300461
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300462 if (reg < 0)
463 return reg;
464
465 if (reg == 0) {
466 it913x_config.dual_mode = 0;
467 it913x_config.tuner_id_0 = IT9135_38;
468 proprietary_ir = true;
469 } else {
470 /* TS mode */
471 reg = it913x_read_reg(udev, 0x49c5);
472 if (reg < 0)
473 return reg;
474 it913x_config.dual_mode = reg;
475
476 /* IR mode type */
477 reg = it913x_read_reg(udev, 0x49ac);
478 if (reg < 0)
479 return reg;
480 if (reg == 5) {
481 info("Remote propriety (raw) mode");
482 proprietary_ir = true;
483 } else if (reg == 1) {
484 info("Remote HID mode NOT SUPPORTED");
485 proprietary_ir = false;
486 props->rc.core.rc_codes = NULL;
487 } else
488 props->rc.core.rc_codes = NULL;
489
490 /* Tuner_id */
491 reg = it913x_read_reg(udev, 0x49d0);
492 if (reg < 0)
493 return reg;
494 it913x_config.tuner_id_0 = reg;
495 }
496
497 if (proprietary_ir)
498 it913x_select_remote(udev, props);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300499
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300500 if (udev->speed != USB_SPEED_HIGH) {
501 props->adapter[0].fe[0].pid_filter_count = 5;
502 info("USB 1 low speed mode - connect to USB 2 port");
503 if (pid_filter > 0)
504 pid_filter = 0;
505 if (it913x_config.dual_mode) {
506 it913x_config.dual_mode = 0;
507 info("Dual mode not supported in USB 1");
508 }
509 } else /* For replugging */
510 if(props->adapter[0].fe[0].pid_filter_count == 5)
511 props->adapter[0].fe[0].pid_filter_count = 31;
512
Malcolm Priestleyed3189c2011-12-30 18:48:00 -0300513 /* Select Stream Buffer Size and pid filter option*/
514 if (pid_filter) {
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300515 props->adapter[0].fe[0].stream.u.bulk.buffersize =
516 TS_BUFFER_SIZE_MAX;
Malcolm Priestleyed3189c2011-12-30 18:48:00 -0300517 props->adapter[0].fe[0].caps &=
518 ~DVB_USB_ADAP_NEED_PID_FILTERING;
519 } else
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300520 props->adapter[0].fe[0].stream.u.bulk.buffersize =
521 TS_BUFFER_SIZE_PID;
Malcolm Priestleyed3189c2011-12-30 18:48:00 -0300522
Malcolm Priestleyf36472d2011-12-11 17:09:46 -0300523 if (it913x_config.dual_mode) {
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300524 props->adapter[1].fe[0].stream.u.bulk.buffersize =
525 props->adapter[0].fe[0].stream.u.bulk.buffersize;
Malcolm Priestleyf36472d2011-12-11 17:09:46 -0300526 props->num_adapters = 2;
Malcolm Priestleyed3189c2011-12-30 18:48:00 -0300527 if (pid_filter)
528 props->adapter[1].fe[0].caps =
529 props->adapter[0].fe[0].caps;
Malcolm Priestleyf36472d2011-12-11 17:09:46 -0300530 } else
531 props->num_adapters = 1;
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300532
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300533 info("Dual mode=%x Tuner Type=%x", it913x_config.dual_mode,
534 it913x_config.tuner_id_0);
535
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300536 ret = ite_firmware_select(udev, props);
537
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300538 return ret;
539}
540
541static int it913x_identify_state(struct usb_device *udev,
542 struct dvb_usb_device_properties *props,
543 struct dvb_usb_device_description **desc,
544 int *cold)
545{
546 int ret = 0, firm_no;
547 u8 reg;
548
549 firm_no = it913x_return_status(udev);
550
551 /* Read and select config */
552 ret = it913x_select_config(udev, props);
553 if (ret < 0)
554 return ret;
555
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300556 if (firm_no > 0) {
557 *cold = 0;
558 return 0;
559 }
560
Malcolm Priestleybc549192011-10-14 19:54:11 -0300561 if (it913x_config.dual_mode) {
562 it913x_config.tuner_id_1 = it913x_read_reg(udev, 0x49e0);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300563 ret = it913x_wr_reg(udev, DEV_0, GPIOH1_EN, 0x1);
564 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_ON, 0x1);
565 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300566 msleep(50);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300567 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x0);
568 msleep(50);
569 reg = it913x_read_reg(udev, GPIOH1_O);
570 if (reg == 0) {
571 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1);
572 ret |= it913x_return_status(udev);
573 if (ret != 0)
574 ret = it913x_wr_reg(udev, DEV_0,
575 GPIOH1_O, 0x0);
576 }
Malcolm Priestleyf36472d2011-12-11 17:09:46 -0300577 }
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300578
579 reg = it913x_read_reg(udev, IO_MUX_POWER_CLK);
580
Malcolm Priestleybc549192011-10-14 19:54:11 -0300581 if (it913x_config.dual_mode) {
582 ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, CHIP2_I2C_ADDR);
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300583 if (it913x_config.firmware_ver == 1)
584 ret |= it913x_wr_reg(udev, DEV_0, 0xcfff, 0x1);
585 else
586 ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x1);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300587 } else {
588 ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, 0x0);
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300589 if (it913x_config.firmware_ver == 1)
590 ret |= it913x_wr_reg(udev, DEV_0, 0xcfff, 0x0);
591 else
592 ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x0);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300593 }
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300594
595 *cold = 1;
596
597 return (ret < 0) ? -ENODEV : 0;
598}
599
600static int it913x_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
601{
602 int ret = 0;
603 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
604
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300605 deb_info(1, "STM (%02x)", onoff);
606
Malcolm Priestley46f3da92012-02-12 07:29:38 -0300607 if (!onoff) {
608 mutex_lock(&adap->dev->i2c_mutex);
609
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300610 ret = it913x_wr_reg(adap->dev->udev, pro, PID_RST, 0x1);
611
Malcolm Priestley46f3da92012-02-12 07:29:38 -0300612 mutex_unlock(&adap->dev->i2c_mutex);
613 }
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300614
615 return ret;
616}
617
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300618static int it913x_download_firmware(struct usb_device *udev,
619 const struct firmware *fw)
620{
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300621 int ret = 0, i = 0, pos = 0;
Malcolm Priestleyf0e07d72011-12-15 18:43:44 -0300622 u8 packet_size, min_pkt;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300623 u8 *fw_data;
624
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300625 ret = it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_100);
626
627 info("FRM Starting Firmware Download");
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300628
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300629 /* Multi firmware loader */
630 /* This uses scatter write firmware headers */
631 /* The firmware must start with 03 XX 00 */
632 /* and be the extact firmware length */
633
Malcolm Priestleyf0e07d72011-12-15 18:43:44 -0300634 if (it913x_config.chip_ver == 2)
635 min_pkt = 0x11;
636 else
637 min_pkt = 0x19;
638
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300639 while (i <= fw->size) {
640 if (((fw->data[i] == 0x3) && (fw->data[i + 2] == 0x0))
641 || (i == fw->size)) {
642 packet_size = i - pos;
Malcolm Priestleyf0e07d72011-12-15 18:43:44 -0300643 if ((packet_size > min_pkt) || (i == fw->size)) {
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300644 fw_data = (u8 *)(fw->data + pos);
645 pos += packet_size;
646 if (packet_size > 0)
647 ret |= it913x_io(udev, WRITE_DATA,
648 DEV_0, CMD_SCATTER_WRITE, 0,
649 0, fw_data, packet_size);
650 udelay(1000);
651 }
652 }
653 i++;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300654 }
655
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300656 ret |= it913x_io(udev, WRITE_CMD, DEV_0, CMD_BOOT, 0, 0, NULL, 0);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300657
658 msleep(100);
659
660 if (ret < 0)
661 info("FRM Firmware Download Failed (%04x)" , ret);
662 else
663 info("FRM Firmware Download Completed - Resetting Device");
664
665 ret |= it913x_return_status(udev);
666
667 msleep(30);
668
669 ret |= it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_400);
670
671 /* Tuner function */
Malcolm Priestleybc549192011-10-14 19:54:11 -0300672 if (it913x_config.dual_mode)
673 ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0xa0);
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300674 else
675 ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0x68);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300676
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300677 if ((it913x_config.chip_ver == 1) &&
678 (it913x_config.chip_type == 0x9135)) {
679 ret |= it913x_wr_reg(udev, DEV_0, PADODPU, 0x0);
680 ret |= it913x_wr_reg(udev, DEV_0, AGC_O_D, 0x0);
681 if (it913x_config.dual_mode) {
682 ret |= it913x_wr_reg(udev, DEV_1, PADODPU, 0x0);
683 ret |= it913x_wr_reg(udev, DEV_1, AGC_O_D, 0x0);
684 }
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300685 }
686
687 return (ret < 0) ? -ENODEV : 0;
688}
689
690static int it913x_name(struct dvb_usb_adapter *adap)
691{
692 const char *desc = adap->dev->desc->name;
693 char *fe_name[] = {"_1", "_2", "_3", "_4"};
Michael Krufky77eed212011-09-06 09:31:57 -0300694 char *name = adap->fe_adap[0].fe->ops.info.name;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300695
696 strlcpy(name, desc, 128);
697 strlcat(name, fe_name[adap->id], 128);
698
699 return 0;
700}
701
702static int it913x_frontend_attach(struct dvb_usb_adapter *adap)
703{
704 struct usb_device *udev = adap->dev->udev;
Malcolm Priestley50815702011-12-04 08:20:37 -0300705 struct it913x_state *st = adap->dev->priv;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300706 int ret = 0;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300707 u8 adap_addr = I2C_BASE_ADDR + (adap->id << 5);
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300708 u16 ep_size = adap->props.fe[0].stream.u.bulk.buffersize / 4;
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300709 u8 pkt_size = 0x80;
710
711 if (adap->dev->udev->speed != USB_SPEED_HIGH)
712 pkt_size = 0x10;
Malcolm Priestleybc549192011-10-14 19:54:11 -0300713
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300714 it913x_config.adf = it913x_read_reg(udev, IO_MUX_POWER_CLK);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300715
716 if (adap->id == 0)
Malcolm Priestley50815702011-12-04 08:20:37 -0300717 memcpy(&st->it913x_config, &it913x_config,
718 sizeof(struct ite_config));
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300719
Michael Krufky77eed212011-09-06 09:31:57 -0300720 adap->fe_adap[0].fe = dvb_attach(it913x_fe_attach,
Malcolm Priestley50815702011-12-04 08:20:37 -0300721 &adap->dev->i2c_adap, adap_addr, &st->it913x_config);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300722
Michael Krufky77eed212011-09-06 09:31:57 -0300723 if (adap->id == 0 && adap->fe_adap[0].fe) {
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300724 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2_SW_RST, 0x1);
725 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_SW_RST, 0x1);
726 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x0f);
727 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_NAK, 0x1b);
728 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x2f);
729 ret = it913x_wr_reg(udev, DEV_0, EP4_TX_LEN_LSB,
730 ep_size & 0xff);
731 ret = it913x_wr_reg(udev, DEV_0, EP4_TX_LEN_MSB, ep_size >> 8);
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300732 ret = it913x_wr_reg(udev, DEV_0, EP4_MAX_PKT, pkt_size);
Michael Krufky77eed212011-09-06 09:31:57 -0300733 } else if (adap->id == 1 && adap->fe_adap[0].fe) {
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300734 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x6f);
735 ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_LSB,
736 ep_size & 0xff);
737 ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_MSB, ep_size >> 8);
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300738 ret = it913x_wr_reg(udev, DEV_0, EP5_MAX_PKT, pkt_size);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300739 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_EN, 0x1);
740 ret = it913x_wr_reg(udev, DEV_1_DMOD, MP2IF_SERIAL, 0x1);
741 ret = it913x_wr_reg(udev, DEV_1, TOP_HOSTB_SER_MODE, 0x1);
742 ret = it913x_wr_reg(udev, DEV_0_DMOD, TSIS_ENABLE, 0x1);
743 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2_SW_RST, 0x0);
744 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_SW_RST, 0x0);
745 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_HALF_PSB, 0x0);
746 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF_STOP_EN, 0x1);
747 ret = it913x_wr_reg(udev, DEV_1_DMOD, MPEG_FULL_SPEED, 0x0);
748 ret = it913x_wr_reg(udev, DEV_1_DMOD, MP2IF_STOP_EN, 0x0);
749 } else
750 return -ENODEV;
751
752 ret = it913x_name(adap);
753
754 return ret;
755}
756
757/* DVB USB Driver */
758static struct dvb_usb_device_properties it913x_properties;
759
760static int it913x_probe(struct usb_interface *intf,
761 const struct usb_device_id *id)
762{
763 cmd_counter = 0;
764 if (0 == dvb_usb_device_init(intf, &it913x_properties,
765 THIS_MODULE, NULL, adapter_nr)) {
766 info("DEV registering device driver");
767 return 0;
768 }
769
770 info("DEV it913x Error");
771 return -ENODEV;
772
773}
774
775static struct usb_device_id it913x_table[] = {
776 { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09) },
Malcolm Priestleybc549192011-10-14 19:54:11 -0300777 { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135) },
Malcolm Priestleyfdb5a912011-11-06 18:30:26 -0300778 { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137) },
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300779 { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005) },
Malcolm Priestley53844c42011-12-12 15:53:00 -0300780 { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006) },
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300781 {} /* Terminating entry */
782};
783
784MODULE_DEVICE_TABLE(usb, it913x_table);
785
786static struct dvb_usb_device_properties it913x_properties = {
787 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
788 .usb_ctrl = DEVICE_SPECIFIC,
789 .download_firmware = it913x_download_firmware,
790 .firmware = "dvb-usb-it9137-01.fw",
791 .no_reconnect = 1,
792 .size_of_priv = sizeof(struct it913x_state),
793 .num_adapters = 2,
794 .adapter = {
795 {
Michael Krufky77eed212011-09-06 09:31:57 -0300796 .num_frontends = 1,
797 .fe = {{
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300798 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
799 DVB_USB_ADAP_NEED_PID_FILTERING|
800 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
801 .streaming_ctrl = it913x_streaming_ctrl,
802 .pid_filter_count = 31,
803 .pid_filter = it913x_pid_filter,
804 .pid_filter_ctrl = it913x_pid_filter_ctrl,
805 .frontend_attach = it913x_frontend_attach,
806 /* parameter for the MPEG2-data transfer */
807 .stream = {
808 .type = USB_BULK,
809 .count = 10,
810 .endpoint = 0x04,
811 .u = {/* Keep Low if PID filter on */
812 .bulk = {
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300813 .buffersize =
814 TS_BUFFER_SIZE_PID,
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300815 }
816 }
817 }
Michael Krufky77eed212011-09-06 09:31:57 -0300818 }},
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300819 },
820 {
Michael Krufky77eed212011-09-06 09:31:57 -0300821 .num_frontends = 1,
822 .fe = {{
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300823 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
824 DVB_USB_ADAP_NEED_PID_FILTERING|
825 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
826 .streaming_ctrl = it913x_streaming_ctrl,
827 .pid_filter_count = 31,
828 .pid_filter = it913x_pid_filter,
829 .pid_filter_ctrl = it913x_pid_filter_ctrl,
830 .frontend_attach = it913x_frontend_attach,
831 /* parameter for the MPEG2-data transfer */
832 .stream = {
833 .type = USB_BULK,
834 .count = 5,
835 .endpoint = 0x05,
836 .u = {
837 .bulk = {
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300838 .buffersize =
839 TS_BUFFER_SIZE_PID,
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300840 }
841 }
842 }
Michael Krufky77eed212011-09-06 09:31:57 -0300843 }},
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300844 }
845 },
846 .identify_state = it913x_identify_state,
tvboxspy2ba0f942011-09-21 18:57:41 -0300847 .rc.core = {
848 .protocol = RC_TYPE_NEC,
849 .module_name = "it913x",
850 .rc_query = it913x_rc_query,
851 .rc_interval = IT913X_POLL,
852 .allowed_protos = RC_TYPE_NEC,
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300853 .rc_codes = RC_MAP_IT913X_V1,
tvboxspy2ba0f942011-09-21 18:57:41 -0300854 },
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300855 .i2c_algo = &it913x_i2c_algo,
Malcolm Priestley53844c42011-12-12 15:53:00 -0300856 .num_device_descs = 5,
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300857 .devices = {
858 { "Kworld UB499-2T T09(IT9137)",
859 { &it913x_table[0], NULL },
860 },
Malcolm Priestleybc549192011-10-14 19:54:11 -0300861 { "ITE 9135 Generic",
862 { &it913x_table[1], NULL },
863 },
Malcolm Priestleyfdb5a912011-11-06 18:30:26 -0300864 { "Sveon STV22 Dual DVB-T HDTV(IT9137)",
865 { &it913x_table[2], NULL },
866 },
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300867 { "ITE 9135(9005) Generic",
868 { &it913x_table[3], NULL },
869 },
Malcolm Priestley53844c42011-12-12 15:53:00 -0300870 { "ITE 9135(9006) Generic",
871 { &it913x_table[4], NULL },
872 },
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300873 }
874};
875
876static struct usb_driver it913x_driver = {
877 .name = "it913x",
878 .probe = it913x_probe,
879 .disconnect = dvb_usb_device_exit,
880 .id_table = it913x_table,
881};
882
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800883module_usb_driver(it913x_driver);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300884
885MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
886MODULE_DESCRIPTION("it913x USB 2 Driver");
Malcolm Priestley46f3da92012-02-12 07:29:38 -0300887MODULE_VERSION("1.26");
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300888MODULE_LICENSE("GPL");