blob: cfa415b0816db052549844f07752a49120b0362a [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 Priestleyaaa589f2012-02-12 07:31:41 -030067 u8 pid_filter_onoff;
Malcolm Priestleybc549192011-10-14 19:54:11 -030068};
69
70struct ite_config it913x_config;
71
Malcolm Priestley15157c52011-12-01 17:35:48 -030072#define IT913X_RETRY 10
73#define IT913X_SND_TIMEOUT 100
74#define IT913X_RCV_TIMEOUT 200
75
Malcolm Priestleyf6d87352011-07-25 15:35:03 -030076static int it913x_bulk_write(struct usb_device *dev,
77 u8 *snd, int len, u8 pipe)
78{
Malcolm Priestley15157c52011-12-01 17:35:48 -030079 int ret, actual_l, i;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -030080
Malcolm Priestley15157c52011-12-01 17:35:48 -030081 for (i = 0; i < IT913X_RETRY; i++) {
82 ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
83 snd, len , &actual_l, IT913X_SND_TIMEOUT);
84 if (ret == 0 || ret != -EBUSY || ret != -ETIMEDOUT)
85 break;
86 }
87
88 if (len != actual_l && ret == 0)
89 ret = -EAGAIN;
90
Malcolm Priestleyf6d87352011-07-25 15:35:03 -030091 return ret;
92}
93
94static int it913x_bulk_read(struct usb_device *dev,
95 u8 *rev, int len, u8 pipe)
96{
Malcolm Priestley15157c52011-12-01 17:35:48 -030097 int ret, actual_l, i;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -030098
Malcolm Priestley15157c52011-12-01 17:35:48 -030099 for (i = 0; i < IT913X_RETRY; i++) {
100 ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
101 rev, len , &actual_l, IT913X_RCV_TIMEOUT);
102 if (ret == 0 || ret != -EBUSY || ret != -ETIMEDOUT)
103 break;
104 }
105
106 if (len != actual_l && ret == 0)
107 ret = -EAGAIN;
108
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300109 return ret;
110}
111
112static u16 check_sum(u8 *p, u8 len)
113{
114 u16 sum = 0;
115 u8 i = 1;
116 while (i < len)
117 sum += (i++ & 1) ? (*p++) << 8 : *p++;
118 return ~sum;
119}
120
Malcolm Priestley15157c52011-12-01 17:35:48 -0300121static int it913x_usb_talk(struct usb_device *udev, u8 mode, u8 pro,
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300122 u8 cmd, u32 reg, u8 addr, u8 *data, u8 len)
123{
124 int ret = 0, i, buf_size = 1;
125 u8 *buff;
126 u8 rlen;
127 u16 chk_sum;
128
129 buff = kzalloc(256, GFP_KERNEL);
130 if (!buff) {
131 info("USB Buffer Failed");
132 return -ENOMEM;
133 }
134
135 buff[buf_size++] = pro;
136 buff[buf_size++] = cmd;
137 buff[buf_size++] = cmd_counter;
138
139 switch (mode) {
140 case READ_LONG:
141 case WRITE_LONG:
142 buff[buf_size++] = len;
143 buff[buf_size++] = 2;
144 buff[buf_size++] = (reg >> 24);
145 buff[buf_size++] = (reg >> 16) & 0xff;
146 buff[buf_size++] = (reg >> 8) & 0xff;
147 buff[buf_size++] = reg & 0xff;
148 break;
149 case READ_SHORT:
150 buff[buf_size++] = addr;
151 break;
152 case WRITE_SHORT:
153 buff[buf_size++] = len;
154 buff[buf_size++] = addr;
155 buff[buf_size++] = (reg >> 8) & 0xff;
156 buff[buf_size++] = reg & 0xff;
157 break;
158 case READ_DATA:
159 case WRITE_DATA:
160 break;
161 case WRITE_CMD:
162 mode = 7;
163 break;
164 default:
165 kfree(buff);
166 return -EINVAL;
167 }
168
169 if (mode & 1) {
170 for (i = 0; i < len ; i++)
171 buff[buf_size++] = data[i];
172 }
173 chk_sum = check_sum(&buff[1], buf_size);
174
175 buff[buf_size++] = chk_sum >> 8;
176 buff[0] = buf_size;
177 buff[buf_size++] = (chk_sum & 0xff);
178
179 ret = it913x_bulk_write(udev, buff, buf_size , 0x02);
Malcolm Priestley15157c52011-12-01 17:35:48 -0300180 if (ret < 0)
181 goto error;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300182
Malcolm Priestley15157c52011-12-01 17:35:48 -0300183 ret = it913x_bulk_read(udev, buff, (mode & 1) ?
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300184 5 : len + 5 , 0x01);
Malcolm Priestley15157c52011-12-01 17:35:48 -0300185 if (ret < 0)
186 goto error;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300187
188 rlen = (mode & 0x1) ? 0x1 : len;
189
190 if (mode & 1)
Malcolm Priestley15157c52011-12-01 17:35:48 -0300191 ret = buff[2];
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300192 else
193 memcpy(data, &buff[3], rlen);
194
195 cmd_counter++;
196
Malcolm Priestley15157c52011-12-01 17:35:48 -0300197error: kfree(buff);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300198
Malcolm Priestley15157c52011-12-01 17:35:48 -0300199 return ret;
200}
201
202static int it913x_io(struct usb_device *udev, u8 mode, u8 pro,
203 u8 cmd, u32 reg, u8 addr, u8 *data, u8 len)
204{
205 int ret, i;
206
207 for (i = 0; i < IT913X_RETRY; i++) {
208 ret = it913x_usb_talk(udev, mode, pro,
209 cmd, reg, addr, data, len);
210 if (ret != -EAGAIN)
211 break;
212 }
213
214 return ret;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300215}
216
217static int it913x_wr_reg(struct usb_device *udev, u8 pro, u32 reg , u8 data)
218{
219 int ret;
220 u8 b[1];
221 b[0] = data;
222 ret = it913x_io(udev, WRITE_LONG, pro,
223 CMD_DEMOD_WRITE, reg, 0, b, sizeof(b));
224
225 return ret;
226}
227
228static int it913x_read_reg(struct usb_device *udev, u32 reg)
229{
230 int ret;
231 u8 data[1];
232
233 ret = it913x_io(udev, READ_LONG, DEV_0,
234 CMD_DEMOD_READ, reg, 0, &data[0], 1);
235
236 return (ret < 0) ? ret : data[0];
237}
238
239static u32 it913x_query(struct usb_device *udev, u8 pro)
240{
241 int ret;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300242 u8 data[4];
243 ret = it913x_io(udev, READ_LONG, pro, CMD_DEMOD_READ,
Malcolm Priestleybc549192011-10-14 19:54:11 -0300244 0x1222, 0, &data[0], 3);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300245
Malcolm Priestleybc549192011-10-14 19:54:11 -0300246 it913x_config.chip_ver = data[0];
247 it913x_config.chip_type = (u16)(data[2] << 8) + data[1];
248
249 info("Chip Version=%02x Chip Type=%04x", it913x_config.chip_ver,
250 it913x_config.chip_type);
251
252 ret |= it913x_io(udev, READ_SHORT, pro,
253 CMD_QUERYINFO, 0, 0x1, &data[0], 4);
254
255 it913x_config.firmware = (data[0] << 24) + (data[1] << 16) +
256 (data[2] << 8) + data[3];
257
258 return (ret < 0) ? 0 : it913x_config.firmware;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300259}
260
261static int it913x_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
262{
Malcolm Priestleyaaa589f2012-02-12 07:31:41 -0300263 struct it913x_state *st = adap->dev->priv;
Malcolm Priestleyd4d5a402012-01-02 14:46:32 -0300264 struct usb_device *udev = adap->dev->udev;
265 int ret;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300266 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
267
Malcolm Priestley46f3da92012-02-12 07:29:38 -0300268 mutex_lock(&adap->dev->i2c_mutex);
269
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300270 deb_info(1, "PID_C (%02x)", onoff);
271
Malcolm Priestleyaaa589f2012-02-12 07:31:41 -0300272 ret = it913x_wr_reg(udev, pro, PID_EN, st->pid_filter_onoff);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300273
274 mutex_unlock(&adap->dev->i2c_mutex);
275 return ret;
276}
277
278static int it913x_pid_filter(struct dvb_usb_adapter *adap,
279 int index, u16 pid, int onoff)
280{
Malcolm Priestleyaaa589f2012-02-12 07:31:41 -0300281 struct it913x_state *st = adap->dev->priv;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300282 struct usb_device *udev = adap->dev->udev;
Malcolm Priestleyd4d5a402012-01-02 14:46:32 -0300283 int ret;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300284 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
285
Malcolm Priestley46f3da92012-02-12 07:29:38 -0300286 mutex_lock(&adap->dev->i2c_mutex);
287
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300288 deb_info(1, "PID_F (%02x)", onoff);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300289
Malcolm Priestleyd4d5a402012-01-02 14:46:32 -0300290 ret = it913x_wr_reg(udev, pro, PID_LSB, (u8)(pid & 0xff));
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300291
Malcolm Priestleyd4d5a402012-01-02 14:46:32 -0300292 ret |= it913x_wr_reg(udev, pro, PID_MSB, (u8)(pid >> 8));
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300293
Malcolm Priestleyd4d5a402012-01-02 14:46:32 -0300294 ret |= it913x_wr_reg(udev, pro, PID_INX_EN, (u8)onoff);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300295
Malcolm Priestleyd4d5a402012-01-02 14:46:32 -0300296 ret |= it913x_wr_reg(udev, pro, PID_INX, (u8)(index & 0x1f));
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300297
Malcolm Priestleyaaa589f2012-02-12 07:31:41 -0300298 if (udev->speed == USB_SPEED_HIGH && pid == 0x2000) {
299 ret |= it913x_wr_reg(udev, pro, PID_EN, !onoff);
300 st->pid_filter_onoff = !onoff;
301 } else
302 st->pid_filter_onoff =
303 adap->fe_adap[adap->active_fe].pid_filtering;
304
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300305 mutex_unlock(&adap->dev->i2c_mutex);
306 return 0;
307}
308
309
310static int it913x_return_status(struct usb_device *udev)
311{
312 u32 firm = 0;
313
314 firm = it913x_query(udev, DEV_0);
315 if (firm > 0)
316 info("Firmware Version %d", firm);
317
318 return (firm > 0) ? firm : 0;
319}
320
321static int it913x_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
322 int num)
323{
324 struct dvb_usb_device *d = i2c_get_adapdata(adap);
325 static u8 data[256];
326 int ret;
327 u32 reg;
328 u8 pro;
Malcolm Priestley46f3da92012-02-12 07:29:38 -0300329
330 mutex_lock(&d->i2c_mutex);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300331
332 debug_data_snipet(1, "Message out", msg[0].buf);
333 deb_info(2, "num of messages %d address %02x", num, msg[0].addr);
334
335 pro = (msg[0].addr & 0x2) ? DEV_0_DMOD : 0x0;
336 pro |= (msg[0].addr & 0x20) ? DEV_1 : DEV_0;
337 memcpy(data, msg[0].buf, msg[0].len);
338 reg = (data[0] << 24) + (data[1] << 16) +
339 (data[2] << 8) + data[3];
340 if (num == 2) {
341 ret = it913x_io(d->udev, READ_LONG, pro,
342 CMD_DEMOD_READ, reg, 0, data, msg[1].len);
343 memcpy(msg[1].buf, data, msg[1].len);
344 } else
345 ret = it913x_io(d->udev, WRITE_LONG, pro, CMD_DEMOD_WRITE,
346 reg, 0, &data[4], msg[0].len - 4);
347
348 mutex_unlock(&d->i2c_mutex);
349
350 return ret;
351}
352
353static u32 it913x_i2c_func(struct i2c_adapter *adapter)
354{
355 return I2C_FUNC_I2C;
356}
357
358static struct i2c_algorithm it913x_i2c_algo = {
359 .master_xfer = it913x_i2c_xfer,
360 .functionality = it913x_i2c_func,
361};
362
363/* Callbacks for DVB USB */
tvboxspy2ba0f942011-09-21 18:57:41 -0300364#define IT913X_POLL 250
365static int it913x_rc_query(struct dvb_usb_device *d)
366{
367 u8 ibuf[4];
368 int ret;
369 u32 key;
370 /* Avoid conflict with frontends*/
Malcolm Priestley46f3da92012-02-12 07:29:38 -0300371 mutex_lock(&d->i2c_mutex);
tvboxspy2ba0f942011-09-21 18:57:41 -0300372
373 ret = it913x_io(d->udev, READ_LONG, PRO_LINK, CMD_IR_GET,
374 0, 0, &ibuf[0], sizeof(ibuf));
375
376 if ((ibuf[2] + ibuf[3]) == 0xff) {
377 key = ibuf[2];
Malcolm Priestleyc725ff62011-11-28 18:22:41 -0300378 key += ibuf[0] << 16;
379 key += ibuf[1] << 8;
380 deb_info(1, "NEC Extended Key =%08x", key);
tvboxspy2ba0f942011-09-21 18:57:41 -0300381 if (d->rc_dev != NULL)
382 rc_keydown(d->rc_dev, key, 0);
383 }
Malcolm Priestleyc725ff62011-11-28 18:22:41 -0300384
tvboxspy2ba0f942011-09-21 18:57:41 -0300385 mutex_unlock(&d->i2c_mutex);
386
387 return ret;
388}
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300389
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300390/* Firmware sets raw */
391const char fw_it9135_v1[] = "dvb-usb-it9135-01.fw";
Malcolm Priestley53844c42011-12-12 15:53:00 -0300392const char fw_it9135_v2[] = "dvb-usb-it9135-02.fw";
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300393const char fw_it9137[] = "dvb-usb-it9137-01.fw";
394
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300395static int ite_firmware_select(struct usb_device *udev,
396 struct dvb_usb_device_properties *props)
397{
398 int sw;
399 /* auto switch */
Malcolm Priestleya8ea0212012-01-20 19:07:18 -0300400 if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_KWORLD_2)
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300401 sw = IT9137_FW;
Malcolm Priestleya8ea0212012-01-20 19:07:18 -0300402 else if (it913x_config.chip_ver == 1)
403 sw = IT9135_V1_FW;
404 else
405 sw = IT9135_V2_FW;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300406
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300407 /* force switch */
408 if (dvb_usb_it913x_firmware != IT9135_AUTO)
409 sw = dvb_usb_it913x_firmware;
410
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300411 switch (sw) {
412 case IT9135_V1_FW:
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300413 it913x_config.firmware_ver = 1;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300414 it913x_config.adc_x2 = 1;
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300415 props->firmware = fw_it9135_v1;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300416 break;
Malcolm Priestley53844c42011-12-12 15:53:00 -0300417 case IT9135_V2_FW:
418 it913x_config.firmware_ver = 1;
419 it913x_config.adc_x2 = 1;
420 props->firmware = fw_it9135_v2;
Malcolm Priestleyfc594e32012-01-28 18:02:34 -0300421 switch (it913x_config.tuner_id_0) {
422 case IT9135_61:
423 case IT9135_62:
424 break;
425 default:
426 info("Unknown tuner ID applying default 0x60");
427 case IT9135_60:
Malcolm Priestleya8ea0212012-01-20 19:07:18 -0300428 it913x_config.tuner_id_0 = IT9135_60;
Malcolm Priestleyfc594e32012-01-28 18:02:34 -0300429 }
Malcolm Priestley53844c42011-12-12 15:53:00 -0300430 break;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300431 case IT9137_FW:
432 default:
433 it913x_config.firmware_ver = 0;
434 it913x_config.adc_x2 = 0;
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300435 props->firmware = fw_it9137;
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300436 }
437
438 return 0;
439}
440
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300441static void it913x_select_remote(struct usb_device *udev,
442 struct dvb_usb_device_properties *props)
443{
444 switch (le16_to_cpu(udev->descriptor.idProduct)) {
445 case USB_PID_ITETECH_IT9135_9005:
446 props->rc.core.rc_codes = RC_MAP_IT913X_V2;
447 return;
448 default:
449 props->rc.core.rc_codes = RC_MAP_IT913X_V1;
450 }
451 return;
452}
453
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300454#define TS_MPEG_PKT_SIZE 188
455#define EP_LOW 21
456#define TS_BUFFER_SIZE_PID (EP_LOW*TS_MPEG_PKT_SIZE)
457#define EP_HIGH 348
458#define TS_BUFFER_SIZE_MAX (EP_HIGH*TS_MPEG_PKT_SIZE)
459
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300460static int it913x_select_config(struct usb_device *udev,
461 struct dvb_usb_device_properties *props)
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300462{
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300463 int ret = 0, reg;
464 bool proprietary_ir = false;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300465
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300466 if (it913x_config.chip_ver == 0x02
467 && it913x_config.chip_type == 0x9135)
468 reg = it913x_read_reg(udev, 0x461d);
469 else
470 reg = it913x_read_reg(udev, 0x461b);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300471
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300472 if (reg < 0)
473 return reg;
474
475 if (reg == 0) {
476 it913x_config.dual_mode = 0;
477 it913x_config.tuner_id_0 = IT9135_38;
478 proprietary_ir = true;
479 } else {
480 /* TS mode */
481 reg = it913x_read_reg(udev, 0x49c5);
482 if (reg < 0)
483 return reg;
484 it913x_config.dual_mode = reg;
485
486 /* IR mode type */
487 reg = it913x_read_reg(udev, 0x49ac);
488 if (reg < 0)
489 return reg;
490 if (reg == 5) {
491 info("Remote propriety (raw) mode");
492 proprietary_ir = true;
493 } else if (reg == 1) {
494 info("Remote HID mode NOT SUPPORTED");
495 proprietary_ir = false;
496 props->rc.core.rc_codes = NULL;
497 } else
498 props->rc.core.rc_codes = NULL;
499
500 /* Tuner_id */
501 reg = it913x_read_reg(udev, 0x49d0);
502 if (reg < 0)
503 return reg;
504 it913x_config.tuner_id_0 = reg;
505 }
506
507 if (proprietary_ir)
508 it913x_select_remote(udev, props);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300509
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300510 if (udev->speed != USB_SPEED_HIGH) {
511 props->adapter[0].fe[0].pid_filter_count = 5;
512 info("USB 1 low speed mode - connect to USB 2 port");
513 if (pid_filter > 0)
514 pid_filter = 0;
515 if (it913x_config.dual_mode) {
516 it913x_config.dual_mode = 0;
517 info("Dual mode not supported in USB 1");
518 }
519 } else /* For replugging */
520 if(props->adapter[0].fe[0].pid_filter_count == 5)
521 props->adapter[0].fe[0].pid_filter_count = 31;
522
Malcolm Priestleyed3189c2011-12-30 18:48:00 -0300523 /* Select Stream Buffer Size and pid filter option*/
524 if (pid_filter) {
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300525 props->adapter[0].fe[0].stream.u.bulk.buffersize =
526 TS_BUFFER_SIZE_MAX;
Malcolm Priestleyed3189c2011-12-30 18:48:00 -0300527 props->adapter[0].fe[0].caps &=
528 ~DVB_USB_ADAP_NEED_PID_FILTERING;
529 } else
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300530 props->adapter[0].fe[0].stream.u.bulk.buffersize =
531 TS_BUFFER_SIZE_PID;
Malcolm Priestleyed3189c2011-12-30 18:48:00 -0300532
Malcolm Priestleyf36472d2011-12-11 17:09:46 -0300533 if (it913x_config.dual_mode) {
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300534 props->adapter[1].fe[0].stream.u.bulk.buffersize =
535 props->adapter[0].fe[0].stream.u.bulk.buffersize;
Malcolm Priestleyf36472d2011-12-11 17:09:46 -0300536 props->num_adapters = 2;
Malcolm Priestleyed3189c2011-12-30 18:48:00 -0300537 if (pid_filter)
538 props->adapter[1].fe[0].caps =
539 props->adapter[0].fe[0].caps;
Malcolm Priestleyf36472d2011-12-11 17:09:46 -0300540 } else
541 props->num_adapters = 1;
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300542
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300543 info("Dual mode=%x Tuner Type=%x", it913x_config.dual_mode,
544 it913x_config.tuner_id_0);
545
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300546 ret = ite_firmware_select(udev, props);
547
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300548 return ret;
549}
550
551static int it913x_identify_state(struct usb_device *udev,
552 struct dvb_usb_device_properties *props,
553 struct dvb_usb_device_description **desc,
554 int *cold)
555{
556 int ret = 0, firm_no;
557 u8 reg;
558
559 firm_no = it913x_return_status(udev);
560
561 /* Read and select config */
562 ret = it913x_select_config(udev, props);
563 if (ret < 0)
564 return ret;
565
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300566 if (firm_no > 0) {
567 *cold = 0;
568 return 0;
569 }
570
Malcolm Priestleybc549192011-10-14 19:54:11 -0300571 if (it913x_config.dual_mode) {
572 it913x_config.tuner_id_1 = it913x_read_reg(udev, 0x49e0);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300573 ret = it913x_wr_reg(udev, DEV_0, GPIOH1_EN, 0x1);
574 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_ON, 0x1);
575 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300576 msleep(50);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300577 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x0);
578 msleep(50);
579 reg = it913x_read_reg(udev, GPIOH1_O);
580 if (reg == 0) {
581 ret |= it913x_wr_reg(udev, DEV_0, GPIOH1_O, 0x1);
582 ret |= it913x_return_status(udev);
583 if (ret != 0)
584 ret = it913x_wr_reg(udev, DEV_0,
585 GPIOH1_O, 0x0);
586 }
Malcolm Priestleyf36472d2011-12-11 17:09:46 -0300587 }
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300588
589 reg = it913x_read_reg(udev, IO_MUX_POWER_CLK);
590
Malcolm Priestleybc549192011-10-14 19:54:11 -0300591 if (it913x_config.dual_mode) {
592 ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, CHIP2_I2C_ADDR);
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300593 if (it913x_config.firmware_ver == 1)
594 ret |= it913x_wr_reg(udev, DEV_0, 0xcfff, 0x1);
595 else
596 ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x1);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300597 } else {
598 ret |= it913x_wr_reg(udev, DEV_0, 0x4bfb, 0x0);
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300599 if (it913x_config.firmware_ver == 1)
600 ret |= it913x_wr_reg(udev, DEV_0, 0xcfff, 0x0);
601 else
602 ret |= it913x_wr_reg(udev, DEV_0, CLK_O_EN, 0x0);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300603 }
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300604
605 *cold = 1;
606
607 return (ret < 0) ? -ENODEV : 0;
608}
609
610static int it913x_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
611{
Malcolm Priestleyaaa589f2012-02-12 07:31:41 -0300612 struct it913x_state *st = adap->dev->priv;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300613 int ret = 0;
614 u8 pro = (adap->id == 0) ? DEV_0_DMOD : DEV_1_DMOD;
615
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300616 deb_info(1, "STM (%02x)", onoff);
617
Malcolm Priestley46f3da92012-02-12 07:29:38 -0300618 if (!onoff) {
619 mutex_lock(&adap->dev->i2c_mutex);
620
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300621 ret = it913x_wr_reg(adap->dev->udev, pro, PID_RST, 0x1);
622
Malcolm Priestley46f3da92012-02-12 07:29:38 -0300623 mutex_unlock(&adap->dev->i2c_mutex);
Malcolm Priestleyaaa589f2012-02-12 07:31:41 -0300624 st->pid_filter_onoff =
625 adap->fe_adap[adap->active_fe].pid_filtering;
626
Malcolm Priestley46f3da92012-02-12 07:29:38 -0300627 }
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300628
629 return ret;
630}
631
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300632static int it913x_download_firmware(struct usb_device *udev,
633 const struct firmware *fw)
634{
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300635 int ret = 0, i = 0, pos = 0;
Malcolm Priestleyf0e07d72011-12-15 18:43:44 -0300636 u8 packet_size, min_pkt;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300637 u8 *fw_data;
638
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300639 ret = it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_100);
640
641 info("FRM Starting Firmware Download");
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300642
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300643 /* Multi firmware loader */
644 /* This uses scatter write firmware headers */
645 /* The firmware must start with 03 XX 00 */
646 /* and be the extact firmware length */
647
Malcolm Priestleyf0e07d72011-12-15 18:43:44 -0300648 if (it913x_config.chip_ver == 2)
649 min_pkt = 0x11;
650 else
651 min_pkt = 0x19;
652
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300653 while (i <= fw->size) {
654 if (((fw->data[i] == 0x3) && (fw->data[i + 2] == 0x0))
655 || (i == fw->size)) {
656 packet_size = i - pos;
Malcolm Priestleyf0e07d72011-12-15 18:43:44 -0300657 if ((packet_size > min_pkt) || (i == fw->size)) {
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300658 fw_data = (u8 *)(fw->data + pos);
659 pos += packet_size;
660 if (packet_size > 0)
661 ret |= it913x_io(udev, WRITE_DATA,
662 DEV_0, CMD_SCATTER_WRITE, 0,
663 0, fw_data, packet_size);
664 udelay(1000);
665 }
666 }
667 i++;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300668 }
669
Malcolm Priestleyb6990292011-11-30 17:14:47 -0300670 ret |= it913x_io(udev, WRITE_CMD, DEV_0, CMD_BOOT, 0, 0, NULL, 0);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300671
672 msleep(100);
673
674 if (ret < 0)
675 info("FRM Firmware Download Failed (%04x)" , ret);
676 else
677 info("FRM Firmware Download Completed - Resetting Device");
678
679 ret |= it913x_return_status(udev);
680
681 msleep(30);
682
683 ret |= it913x_wr_reg(udev, DEV_0, I2C_CLK, I2C_CLK_400);
684
685 /* Tuner function */
Malcolm Priestleybc549192011-10-14 19:54:11 -0300686 if (it913x_config.dual_mode)
687 ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0xa0);
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300688 else
689 ret |= it913x_wr_reg(udev, DEV_0_DMOD , 0xec4c, 0x68);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300690
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300691 if ((it913x_config.chip_ver == 1) &&
692 (it913x_config.chip_type == 0x9135)) {
693 ret |= it913x_wr_reg(udev, DEV_0, PADODPU, 0x0);
694 ret |= it913x_wr_reg(udev, DEV_0, AGC_O_D, 0x0);
695 if (it913x_config.dual_mode) {
696 ret |= it913x_wr_reg(udev, DEV_1, PADODPU, 0x0);
697 ret |= it913x_wr_reg(udev, DEV_1, AGC_O_D, 0x0);
698 }
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300699 }
700
701 return (ret < 0) ? -ENODEV : 0;
702}
703
704static int it913x_name(struct dvb_usb_adapter *adap)
705{
706 const char *desc = adap->dev->desc->name;
707 char *fe_name[] = {"_1", "_2", "_3", "_4"};
Michael Krufky77eed212011-09-06 09:31:57 -0300708 char *name = adap->fe_adap[0].fe->ops.info.name;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300709
710 strlcpy(name, desc, 128);
711 strlcat(name, fe_name[adap->id], 128);
712
713 return 0;
714}
715
716static int it913x_frontend_attach(struct dvb_usb_adapter *adap)
717{
718 struct usb_device *udev = adap->dev->udev;
Malcolm Priestley50815702011-12-04 08:20:37 -0300719 struct it913x_state *st = adap->dev->priv;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300720 int ret = 0;
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300721 u8 adap_addr = I2C_BASE_ADDR + (adap->id << 5);
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300722 u16 ep_size = adap->props.fe[0].stream.u.bulk.buffersize / 4;
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300723 u8 pkt_size = 0x80;
724
725 if (adap->dev->udev->speed != USB_SPEED_HIGH)
726 pkt_size = 0x10;
Malcolm Priestleybc549192011-10-14 19:54:11 -0300727
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300728 it913x_config.adf = it913x_read_reg(udev, IO_MUX_POWER_CLK);
Malcolm Priestleybc549192011-10-14 19:54:11 -0300729
730 if (adap->id == 0)
Malcolm Priestley50815702011-12-04 08:20:37 -0300731 memcpy(&st->it913x_config, &it913x_config,
732 sizeof(struct ite_config));
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300733
Michael Krufky77eed212011-09-06 09:31:57 -0300734 adap->fe_adap[0].fe = dvb_attach(it913x_fe_attach,
Malcolm Priestley50815702011-12-04 08:20:37 -0300735 &adap->dev->i2c_adap, adap_addr, &st->it913x_config);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300736
Michael Krufky77eed212011-09-06 09:31:57 -0300737 if (adap->id == 0 && adap->fe_adap[0].fe) {
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300738 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2_SW_RST, 0x1);
739 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_SW_RST, 0x1);
740 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x0f);
741 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_NAK, 0x1b);
742 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x2f);
743 ret = it913x_wr_reg(udev, DEV_0, EP4_TX_LEN_LSB,
744 ep_size & 0xff);
745 ret = it913x_wr_reg(udev, DEV_0, EP4_TX_LEN_MSB, ep_size >> 8);
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300746 ret = it913x_wr_reg(udev, DEV_0, EP4_MAX_PKT, pkt_size);
Michael Krufky77eed212011-09-06 09:31:57 -0300747 } else if (adap->id == 1 && adap->fe_adap[0].fe) {
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300748 ret = it913x_wr_reg(udev, DEV_0, EP0_TX_EN, 0x6f);
749 ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_LSB,
750 ep_size & 0xff);
751 ret = it913x_wr_reg(udev, DEV_0, EP5_TX_LEN_MSB, ep_size >> 8);
Malcolm Priestley3822c7c2011-11-06 10:24:30 -0300752 ret = it913x_wr_reg(udev, DEV_0, EP5_MAX_PKT, pkt_size);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300753 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_EN, 0x1);
754 ret = it913x_wr_reg(udev, DEV_1_DMOD, MP2IF_SERIAL, 0x1);
755 ret = it913x_wr_reg(udev, DEV_1, TOP_HOSTB_SER_MODE, 0x1);
756 ret = it913x_wr_reg(udev, DEV_0_DMOD, TSIS_ENABLE, 0x1);
757 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2_SW_RST, 0x0);
758 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_SW_RST, 0x0);
759 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF2_HALF_PSB, 0x0);
760 ret = it913x_wr_reg(udev, DEV_0_DMOD, MP2IF_STOP_EN, 0x1);
761 ret = it913x_wr_reg(udev, DEV_1_DMOD, MPEG_FULL_SPEED, 0x0);
762 ret = it913x_wr_reg(udev, DEV_1_DMOD, MP2IF_STOP_EN, 0x0);
763 } else
764 return -ENODEV;
765
766 ret = it913x_name(adap);
767
768 return ret;
769}
770
771/* DVB USB Driver */
772static struct dvb_usb_device_properties it913x_properties;
773
774static int it913x_probe(struct usb_interface *intf,
775 const struct usb_device_id *id)
776{
777 cmd_counter = 0;
778 if (0 == dvb_usb_device_init(intf, &it913x_properties,
779 THIS_MODULE, NULL, adapter_nr)) {
780 info("DEV registering device driver");
781 return 0;
782 }
783
784 info("DEV it913x Error");
785 return -ENODEV;
786
787}
788
789static struct usb_device_id it913x_table[] = {
790 { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09) },
Malcolm Priestleybc549192011-10-14 19:54:11 -0300791 { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135) },
Malcolm Priestleyfdb5a912011-11-06 18:30:26 -0300792 { USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137) },
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300793 { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005) },
Malcolm Priestley53844c42011-12-12 15:53:00 -0300794 { USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006) },
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300795 {} /* Terminating entry */
796};
797
798MODULE_DEVICE_TABLE(usb, it913x_table);
799
800static struct dvb_usb_device_properties it913x_properties = {
801 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
802 .usb_ctrl = DEVICE_SPECIFIC,
803 .download_firmware = it913x_download_firmware,
804 .firmware = "dvb-usb-it9137-01.fw",
805 .no_reconnect = 1,
806 .size_of_priv = sizeof(struct it913x_state),
807 .num_adapters = 2,
808 .adapter = {
809 {
Michael Krufky77eed212011-09-06 09:31:57 -0300810 .num_frontends = 1,
811 .fe = {{
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300812 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
813 DVB_USB_ADAP_NEED_PID_FILTERING|
814 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
815 .streaming_ctrl = it913x_streaming_ctrl,
816 .pid_filter_count = 31,
817 .pid_filter = it913x_pid_filter,
818 .pid_filter_ctrl = it913x_pid_filter_ctrl,
819 .frontend_attach = it913x_frontend_attach,
820 /* parameter for the MPEG2-data transfer */
821 .stream = {
822 .type = USB_BULK,
823 .count = 10,
824 .endpoint = 0x04,
825 .u = {/* Keep Low if PID filter on */
826 .bulk = {
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300827 .buffersize =
828 TS_BUFFER_SIZE_PID,
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300829 }
830 }
831 }
Michael Krufky77eed212011-09-06 09:31:57 -0300832 }},
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300833 },
834 {
Michael Krufky77eed212011-09-06 09:31:57 -0300835 .num_frontends = 1,
836 .fe = {{
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300837 .caps = DVB_USB_ADAP_HAS_PID_FILTER|
838 DVB_USB_ADAP_NEED_PID_FILTERING|
839 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
840 .streaming_ctrl = it913x_streaming_ctrl,
841 .pid_filter_count = 31,
842 .pid_filter = it913x_pid_filter,
843 .pid_filter_ctrl = it913x_pid_filter_ctrl,
844 .frontend_attach = it913x_frontend_attach,
845 /* parameter for the MPEG2-data transfer */
846 .stream = {
847 .type = USB_BULK,
848 .count = 5,
849 .endpoint = 0x05,
850 .u = {
851 .bulk = {
Malcolm Priestley9c1133c2011-11-27 17:35:06 -0300852 .buffersize =
853 TS_BUFFER_SIZE_PID,
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300854 }
855 }
856 }
Michael Krufky77eed212011-09-06 09:31:57 -0300857 }},
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300858 }
859 },
860 .identify_state = it913x_identify_state,
tvboxspy2ba0f942011-09-21 18:57:41 -0300861 .rc.core = {
862 .protocol = RC_TYPE_NEC,
863 .module_name = "it913x",
864 .rc_query = it913x_rc_query,
865 .rc_interval = IT913X_POLL,
866 .allowed_protos = RC_TYPE_NEC,
Malcolm Priestley3691a0d2012-02-01 18:27:42 -0300867 .rc_codes = RC_MAP_IT913X_V1,
tvboxspy2ba0f942011-09-21 18:57:41 -0300868 },
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300869 .i2c_algo = &it913x_i2c_algo,
Malcolm Priestley53844c42011-12-12 15:53:00 -0300870 .num_device_descs = 5,
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300871 .devices = {
872 { "Kworld UB499-2T T09(IT9137)",
873 { &it913x_table[0], NULL },
874 },
Malcolm Priestleybc549192011-10-14 19:54:11 -0300875 { "ITE 9135 Generic",
876 { &it913x_table[1], NULL },
877 },
Malcolm Priestleyfdb5a912011-11-06 18:30:26 -0300878 { "Sveon STV22 Dual DVB-T HDTV(IT9137)",
879 { &it913x_table[2], NULL },
880 },
Malcolm Priestley5e642c02011-11-30 17:16:09 -0300881 { "ITE 9135(9005) Generic",
882 { &it913x_table[3], NULL },
883 },
Malcolm Priestley53844c42011-12-12 15:53:00 -0300884 { "ITE 9135(9006) Generic",
885 { &it913x_table[4], NULL },
886 },
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300887 }
888};
889
890static struct usb_driver it913x_driver = {
891 .name = "it913x",
892 .probe = it913x_probe,
893 .disconnect = dvb_usb_device_exit,
894 .id_table = it913x_table,
895};
896
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800897module_usb_driver(it913x_driver);
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300898
899MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
900MODULE_DESCRIPTION("it913x USB 2 Driver");
Malcolm Priestleyaaa589f2012-02-12 07:31:41 -0300901MODULE_VERSION("1.27");
Malcolm Priestleyf6d87352011-07-25 15:35:03 -0300902MODULE_LICENSE("GPL");