blob: 41a7c430f9c14561f781c46ddcbd889480fc8565 [file] [log] [blame]
Antti Palosaari80619de2008-09-15 17:18:09 -03001/*
2 * DVB USB Linux driver for Afatech AF9015 DVB-T USB2.0 receiver
3 *
4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
5 *
6 * Thanks to Afatech who kindly provided information.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 */
23
Jiri Slaby6c614042010-01-22 12:10:52 -030024#include <linux/hash.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Jiri Slaby6c614042010-01-22 12:10:52 -030026
Antti Palosaari80619de2008-09-15 17:18:09 -030027#include "af9015.h"
28#include "af9013.h"
29#include "mt2060.h"
30#include "qt1010.h"
31#include "tda18271.h"
32#include "mxl5005s.h"
Jochen Friedrichd5633992009-02-02 14:59:50 -030033#include "mc44s803.h"
Antti Palosaariee3d4402010-08-13 03:51:26 -030034#include "tda18218.h"
Antti Palosaariab07fdd2010-09-09 14:59:10 -030035#include "mxl5007t.h"
Antti Palosaari80619de2008-09-15 17:18:09 -030036
Antti Palosaari349d0422008-11-05 16:31:24 -030037static int dvb_usb_af9015_debug;
Antti Palosaari80619de2008-09-15 17:18:09 -030038module_param_named(debug, dvb_usb_af9015_debug, int, 0644);
39MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
Antti Palosaari349d0422008-11-05 16:31:24 -030040static int dvb_usb_af9015_remote;
Antti Palosaari80619de2008-09-15 17:18:09 -030041module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
42MODULE_PARM_DESC(remote, "select remote");
Antti Palosaari80619de2008-09-15 17:18:09 -030043DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
44
45static DEFINE_MUTEX(af9015_usb_mutex);
46
47static struct af9015_config af9015_config;
Antti Palosaari85d7d7c2009-04-09 09:16:12 -030048static struct dvb_usb_device_properties af9015_properties[3];
Antti Palosaari349d0422008-11-05 16:31:24 -030049static int af9015_properties_count = ARRAY_SIZE(af9015_properties);
Antti Palosaari80619de2008-09-15 17:18:09 -030050
51static struct af9013_config af9015_af9013_config[] = {
52 {
53 .demod_address = AF9015_I2C_DEMOD,
54 .output_mode = AF9013_OUTPUT_MODE_USB,
55 .api_version = { 0, 1, 9, 0 },
56 .gpio[0] = AF9013_GPIO_HI,
Antti Palosaari80619de2008-09-15 17:18:09 -030057 .gpio[3] = AF9013_GPIO_TUNER_ON,
58
59 }, {
60 .output_mode = AF9013_OUTPUT_MODE_SERIAL,
61 .api_version = { 0, 1, 9, 0 },
62 .gpio[0] = AF9013_GPIO_TUNER_ON,
63 .gpio[1] = AF9013_GPIO_LO,
64 }
65};
66
67static int af9015_rw_udev(struct usb_device *udev, struct req_t *req)
68{
Antti Palosaari06565d72009-09-12 20:46:30 -030069#define BUF_LEN 63
70#define REQ_HDR_LEN 8 /* send header size */
71#define ACK_HDR_LEN 2 /* rece header size */
Antti Palosaari80619de2008-09-15 17:18:09 -030072 int act_len, ret;
Antti Palosaari06565d72009-09-12 20:46:30 -030073 u8 buf[BUF_LEN];
Antti Palosaari80619de2008-09-15 17:18:09 -030074 u8 write = 1;
Antti Palosaari06565d72009-09-12 20:46:30 -030075 u8 msg_len = REQ_HDR_LEN;
Antti Palosaari80619de2008-09-15 17:18:09 -030076 static u8 seq; /* packet sequence number */
77
78 if (mutex_lock_interruptible(&af9015_usb_mutex) < 0)
79 return -EAGAIN;
80
81 buf[0] = req->cmd;
82 buf[1] = seq++;
83 buf[2] = req->i2c_addr;
84 buf[3] = req->addr >> 8;
85 buf[4] = req->addr & 0xff;
86 buf[5] = req->mbox;
87 buf[6] = req->addr_len;
88 buf[7] = req->data_len;
89
90 switch (req->cmd) {
91 case GET_CONFIG:
Antti Palosaari80619de2008-09-15 17:18:09 -030092 case READ_MEMORY:
93 case RECONNECT_USB:
94 case GET_IR_CODE:
95 write = 0;
96 break;
97 case READ_I2C:
98 write = 0;
99 buf[2] |= 0x01; /* set I2C direction */
100 case WRITE_I2C:
101 buf[0] = READ_WRITE_I2C;
102 break;
103 case WRITE_MEMORY:
104 if (((req->addr & 0xff00) == 0xff00) ||
Antti Palosaarif4e96de2009-09-12 21:25:59 -0300105 ((req->addr & 0xff00) == 0xae00))
Antti Palosaari80619de2008-09-15 17:18:09 -0300106 buf[0] = WRITE_VIRTUAL_MEMORY;
107 case WRITE_VIRTUAL_MEMORY:
108 case COPY_FIRMWARE:
109 case DOWNLOAD_FIRMWARE:
Nils Kassubeba1bc642009-07-28 11:54:52 -0300110 case BOOT:
Antti Palosaari80619de2008-09-15 17:18:09 -0300111 break;
112 default:
113 err("unknown command:%d", req->cmd);
114 ret = -1;
115 goto error_unlock;
116 }
117
Antti Palosaari06565d72009-09-12 20:46:30 -0300118 /* buffer overflow check */
119 if ((write && (req->data_len > BUF_LEN - REQ_HDR_LEN)) ||
120 (!write && (req->data_len > BUF_LEN - ACK_HDR_LEN))) {
121 err("too much data; cmd:%d len:%d", req->cmd, req->data_len);
122 ret = -EINVAL;
123 goto error_unlock;
124 }
125
Antti Palosaari80619de2008-09-15 17:18:09 -0300126 /* write requested */
127 if (write) {
Antti Palosaari06565d72009-09-12 20:46:30 -0300128 memcpy(&buf[REQ_HDR_LEN], req->data, req->data_len);
Antti Palosaari80619de2008-09-15 17:18:09 -0300129 msg_len += req->data_len;
130 }
Antti Palosaari06565d72009-09-12 20:46:30 -0300131
Antti Palosaari80619de2008-09-15 17:18:09 -0300132 deb_xfer(">>> ");
133 debug_dump(buf, msg_len, deb_xfer);
134
135 /* send req */
136 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
Antti Palosaari06565d72009-09-12 20:46:30 -0300137 &act_len, AF9015_USB_TIMEOUT);
Antti Palosaari80619de2008-09-15 17:18:09 -0300138 if (ret)
139 err("bulk message failed:%d (%d/%d)", ret, msg_len, act_len);
140 else
141 if (act_len != msg_len)
142 ret = -1; /* all data is not send */
143 if (ret)
144 goto error_unlock;
145
146 /* no ack for those packets */
147 if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
148 goto exit_unlock;
149
Antti Palosaari06565d72009-09-12 20:46:30 -0300150 /* write receives seq + status = 2 bytes
151 read receives seq + status + data = 2 + N bytes */
152 msg_len = ACK_HDR_LEN;
153 if (!write)
154 msg_len += req->data_len;
155
Antti Palosaari80619de2008-09-15 17:18:09 -0300156 ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
Antti Palosaari06565d72009-09-12 20:46:30 -0300157 &act_len, AF9015_USB_TIMEOUT);
Antti Palosaari80619de2008-09-15 17:18:09 -0300158 if (ret) {
159 err("recv bulk message failed:%d", ret);
160 ret = -1;
161 goto error_unlock;
162 }
163
164 deb_xfer("<<< ");
165 debug_dump(buf, act_len, deb_xfer);
166
167 /* remote controller query status is 1 if remote code is not received */
168 if (req->cmd == GET_IR_CODE && buf[1] == 1) {
169 buf[1] = 0; /* clear command "error" status */
170 memset(&buf[2], 0, req->data_len);
171 buf[3] = 1; /* no remote code received mark */
172 }
173
174 /* check status */
175 if (buf[1]) {
176 err("command failed:%d", buf[1]);
177 ret = -1;
178 goto error_unlock;
179 }
180
181 /* read request, copy returned data to return buf */
182 if (!write)
Antti Palosaari06565d72009-09-12 20:46:30 -0300183 memcpy(req->data, &buf[ACK_HDR_LEN], req->data_len);
Antti Palosaari80619de2008-09-15 17:18:09 -0300184
185error_unlock:
186exit_unlock:
187 mutex_unlock(&af9015_usb_mutex);
188
189 return ret;
190}
191
192static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
193{
194 return af9015_rw_udev(d->udev, req);
195}
196
197static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
198 u8 len)
199{
200 struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
201 val};
202 return af9015_ctrl_msg(d, &req);
203}
204
205static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
206{
207 return af9015_write_regs(d, addr, &val, 1);
208}
209
Antti Palosaari74c8e3a2010-10-22 18:45:18 -0300210static int af9015_read_regs(struct dvb_usb_device *d, u16 addr, u8 *val, u8 len)
211{
212 struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
213 val};
214 return af9015_ctrl_msg(d, &req);
215}
216
Antti Palosaari80619de2008-09-15 17:18:09 -0300217static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
218{
Antti Palosaari74c8e3a2010-10-22 18:45:18 -0300219 return af9015_read_regs(d, addr, val, 1);
Antti Palosaari80619de2008-09-15 17:18:09 -0300220}
221
222static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
223 u8 val)
224{
225 struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
226
227 if (addr == af9015_af9013_config[0].demod_address ||
228 addr == af9015_af9013_config[1].demod_address)
229 req.addr_len = 3;
230
231 return af9015_ctrl_msg(d, &req);
232}
233
234static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
235 u8 *val)
236{
237 struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
238
239 if (addr == af9015_af9013_config[0].demod_address ||
240 addr == af9015_af9013_config[1].demod_address)
241 req.addr_len = 3;
242
243 return af9015_ctrl_msg(d, &req);
244}
245
246static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
247 int num)
248{
249 struct dvb_usb_device *d = i2c_get_adapdata(adap);
250 int ret = 0, i = 0;
251 u16 addr;
Antti Palosaari675375d2010-10-07 21:46:41 -0300252 u8 uninitialized_var(mbox), addr_len;
Antti Palosaari80619de2008-09-15 17:18:09 -0300253 struct req_t req;
254
255/* TODO: implement bus lock
256
257The bus lock is needed because there is two tuners both using same I2C-address.
258Due to that the only way to select correct tuner is use demodulator I2C-gate.
259
260................................................
261. AF9015 includes integrated AF9013 demodulator.
262. ____________ ____________ . ____________
263.| uC | | demod | . | tuner |
264.|------------| |------------| . |------------|
265.| AF9015 | | AF9013/5 | . | MXL5003 |
266.| |--+----I2C-------|-----/ -----|-.-----I2C-------| |
267.| | | | addr 0x38 | . | addr 0xc6 |
268.|____________| | |____________| . |____________|
269.................|..............................
270 | ____________ ____________
271 | | demod | | tuner |
272 | |------------| |------------|
273 | | AF9013 | | MXL5003 |
274 +----I2C-------|-----/ -----|-------I2C-------| |
275 | addr 0x3a | | addr 0xc6 |
276 |____________| |____________|
277*/
278 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
279 return -EAGAIN;
280
281 while (i < num) {
282 if (msg[i].addr == af9015_af9013_config[0].demod_address ||
283 msg[i].addr == af9015_af9013_config[1].demod_address) {
284 addr = msg[i].buf[0] << 8;
285 addr += msg[i].buf[1];
286 mbox = msg[i].buf[2];
287 addr_len = 3;
288 } else {
289 addr = msg[i].buf[0];
290 addr_len = 1;
Antti Palosaari675375d2010-10-07 21:46:41 -0300291 /* mbox is don't care in that case */
Antti Palosaari80619de2008-09-15 17:18:09 -0300292 }
293
294 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
Antti Palosaari709d9202011-06-17 21:16:38 -0300295 if (msg[i].len > 3 || msg[i+1].len > 61) {
296 ret = -EOPNOTSUPP;
297 goto error;
298 }
Antti Palosaari80619de2008-09-15 17:18:09 -0300299 if (msg[i].addr ==
300 af9015_af9013_config[0].demod_address)
301 req.cmd = READ_MEMORY;
302 else
303 req.cmd = READ_I2C;
304 req.i2c_addr = msg[i].addr;
305 req.addr = addr;
306 req.mbox = mbox;
307 req.addr_len = addr_len;
308 req.data_len = msg[i+1].len;
309 req.data = &msg[i+1].buf[0];
310 ret = af9015_ctrl_msg(d, &req);
311 i += 2;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300312 } else if (msg[i].flags & I2C_M_RD) {
Antti Palosaari709d9202011-06-17 21:16:38 -0300313 if (msg[i].len > 61) {
314 ret = -EOPNOTSUPP;
315 goto error;
316 }
Jochen Friedrichd5633992009-02-02 14:59:50 -0300317 if (msg[i].addr ==
Antti Palosaari16b2dc22011-06-16 20:02:41 -0300318 af9015_af9013_config[0].demod_address) {
319 ret = -EINVAL;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300320 goto error;
Antti Palosaari16b2dc22011-06-16 20:02:41 -0300321 }
322 req.cmd = READ_I2C;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300323 req.i2c_addr = msg[i].addr;
324 req.addr = addr;
325 req.mbox = mbox;
326 req.addr_len = addr_len;
327 req.data_len = msg[i].len;
328 req.data = &msg[i].buf[0];
329 ret = af9015_ctrl_msg(d, &req);
330 i += 1;
Antti Palosaari80619de2008-09-15 17:18:09 -0300331 } else {
Antti Palosaari709d9202011-06-17 21:16:38 -0300332 if (msg[i].len > 21) {
333 ret = -EOPNOTSUPP;
334 goto error;
335 }
Antti Palosaari80619de2008-09-15 17:18:09 -0300336 if (msg[i].addr ==
337 af9015_af9013_config[0].demod_address)
338 req.cmd = WRITE_MEMORY;
339 else
340 req.cmd = WRITE_I2C;
341 req.i2c_addr = msg[i].addr;
342 req.addr = addr;
343 req.mbox = mbox;
344 req.addr_len = addr_len;
345 req.data_len = msg[i].len-addr_len;
346 req.data = &msg[i].buf[addr_len];
347 ret = af9015_ctrl_msg(d, &req);
348 i += 1;
349 }
350 if (ret)
351 goto error;
352
353 }
354 ret = i;
355
356error:
357 mutex_unlock(&d->i2c_mutex);
358
359 return ret;
360}
361
362static u32 af9015_i2c_func(struct i2c_adapter *adapter)
363{
364 return I2C_FUNC_I2C;
365}
366
367static struct i2c_algorithm af9015_i2c_algo = {
368 .master_xfer = af9015_i2c_xfer,
369 .functionality = af9015_i2c_func,
370};
371
372static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
373{
374 int ret;
375 u8 val, mask = 0x01;
376
377 ret = af9015_read_reg(d, addr, &val);
378 if (ret)
379 return ret;
380
381 mask <<= bit;
382 if (op) {
383 /* set bit */
384 val |= mask;
385 } else {
386 /* clear bit */
387 mask ^= 0xff;
388 val &= mask;
389 }
390
391 return af9015_write_reg(d, addr, val);
392}
393
394static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
395{
396 return af9015_do_reg_bit(d, addr, bit, 1);
397}
398
399static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
400{
401 return af9015_do_reg_bit(d, addr, bit, 0);
402}
403
404static int af9015_init_endpoint(struct dvb_usb_device *d)
405{
406 int ret;
407 u16 frame_size;
408 u8 packet_size;
409 deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);
410
Antti Palosaari9c863272009-09-12 13:35:29 -0300411 /* Windows driver uses packet count 21 for USB1.1 and 348 for USB2.0.
412 We use smaller - about 1/4 from the original, 5 and 87. */
Antti Palosaari80619de2008-09-15 17:18:09 -0300413#define TS_PACKET_SIZE 188
414
Antti Palosaari9c863272009-09-12 13:35:29 -0300415#define TS_USB20_PACKET_COUNT 87
Antti Palosaari80619de2008-09-15 17:18:09 -0300416#define TS_USB20_FRAME_SIZE (TS_PACKET_SIZE*TS_USB20_PACKET_COUNT)
417
Antti Palosaari9c863272009-09-12 13:35:29 -0300418#define TS_USB11_PACKET_COUNT 5
Antti Palosaari80619de2008-09-15 17:18:09 -0300419#define TS_USB11_FRAME_SIZE (TS_PACKET_SIZE*TS_USB11_PACKET_COUNT)
420
421#define TS_USB20_MAX_PACKET_SIZE 512
422#define TS_USB11_MAX_PACKET_SIZE 64
423
424 if (d->udev->speed == USB_SPEED_FULL) {
425 frame_size = TS_USB11_FRAME_SIZE/4;
426 packet_size = TS_USB11_MAX_PACKET_SIZE/4;
427 } else {
428 frame_size = TS_USB20_FRAME_SIZE/4;
429 packet_size = TS_USB20_MAX_PACKET_SIZE/4;
430 }
431
432 ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
433 if (ret)
434 goto error;
435 ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
436 if (ret)
437 goto error;
438 ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
439 if (ret)
440 goto error;
441 ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
442 if (ret)
443 goto error;
444 ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
445 if (ret)
446 goto error;
447 if (af9015_config.dual_mode) {
448 ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
449 if (ret)
450 goto error;
451 }
452 ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
453 if (ret)
454 goto error;
455 if (af9015_config.dual_mode) {
456 ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
457 if (ret)
458 goto error;
459 }
460 /* EP4 xfer length */
461 ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
462 if (ret)
463 goto error;
464 ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
465 if (ret)
466 goto error;
467 /* EP5 xfer length */
468 ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
469 if (ret)
470 goto error;
471 ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
472 if (ret)
473 goto error;
474 ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
475 if (ret)
476 goto error;
477 ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
478 if (ret)
479 goto error;
480 ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
481 if (ret)
482 goto error;
483 if (af9015_config.dual_mode) {
484 ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
485 if (ret)
486 goto error;
487 }
488
489 /* enable / disable mp2if2 */
490 if (af9015_config.dual_mode)
491 ret = af9015_set_reg_bit(d, 0xd50b, 0);
492 else
493 ret = af9015_clear_reg_bit(d, 0xd50b, 0);
Antti Palosaari1e8750c2011-03-18 19:36:42 -0300494
Antti Palosaari80619de2008-09-15 17:18:09 -0300495error:
496 if (ret)
497 err("endpoint init failed:%d", ret);
498 return ret;
499}
500
501static int af9015_copy_firmware(struct dvb_usb_device *d)
502{
503 int ret;
504 u8 fw_params[4];
505 u8 val, i;
506 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
507 fw_params };
508 deb_info("%s:\n", __func__);
509
510 fw_params[0] = af9015_config.firmware_size >> 8;
511 fw_params[1] = af9015_config.firmware_size & 0xff;
512 fw_params[2] = af9015_config.firmware_checksum >> 8;
513 fw_params[3] = af9015_config.firmware_checksum & 0xff;
514
515 /* wait 2nd demodulator ready */
516 msleep(100);
517
Antti Palosaaried19a5d2010-09-12 21:02:55 -0300518 ret = af9015_read_reg_i2c(d,
519 af9015_af9013_config[1].demod_address, 0x98be, &val);
Antti Palosaari80619de2008-09-15 17:18:09 -0300520 if (ret)
521 goto error;
522 else
523 deb_info("%s: firmware status:%02x\n", __func__, val);
524
525 if (val == 0x0c) /* fw is running, no need for download */
526 goto exit;
527
528 /* set I2C master clock to fast (to speed up firmware copy) */
529 ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
530 if (ret)
531 goto error;
532
533 msleep(50);
534
535 /* copy firmware */
536 ret = af9015_ctrl_msg(d, &req);
537 if (ret)
538 err("firmware copy cmd failed:%d", ret);
539 deb_info("%s: firmware copy done\n", __func__);
540
541 /* set I2C master clock back to normal */
542 ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
543 if (ret)
544 goto error;
545
546 /* request boot firmware */
547 ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].demod_address,
548 0xe205, 1);
549 deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
550 if (ret)
551 goto error;
552
553 for (i = 0; i < 15; i++) {
554 msleep(100);
555
556 /* check firmware status */
557 ret = af9015_read_reg_i2c(d,
558 af9015_af9013_config[1].demod_address, 0x98be, &val);
559 deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
560 __func__, ret, val);
561 if (ret)
562 goto error;
563
564 if (val == 0x0c || val == 0x04) /* success or fail */
565 break;
566 }
567
568 if (val == 0x04) {
569 err("firmware did not run");
570 ret = -1;
571 } else if (val != 0x0c) {
572 err("firmware boot timeout");
573 ret = -1;
574 }
575
576error:
577exit:
578 return ret;
579}
580
Jiri Slaby6c614042010-01-22 12:10:52 -0300581/* hash (and dump) eeprom */
582static int af9015_eeprom_hash(struct usb_device *udev)
Antti Palosaari80619de2008-09-15 17:18:09 -0300583{
Jiri Slaby6c614042010-01-22 12:10:52 -0300584 static const unsigned int eeprom_size = 256;
585 unsigned int reg;
586 int ret;
587 u8 val, *eeprom;
588 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
Antti Palosaari80619de2008-09-15 17:18:09 -0300589
Jiri Slaby6c614042010-01-22 12:10:52 -0300590 eeprom = kmalloc(eeprom_size, GFP_KERNEL);
591 if (eeprom == NULL)
592 return -ENOMEM;
593
594 for (reg = 0; reg < eeprom_size; reg++) {
595 req.addr = reg;
596 ret = af9015_rw_udev(udev, &req);
597 if (ret)
598 goto free;
599 eeprom[reg] = val;
Antti Palosaari80619de2008-09-15 17:18:09 -0300600 }
Jiri Slaby6c614042010-01-22 12:10:52 -0300601
602 if (dvb_usb_af9015_debug & 0x01)
603 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, eeprom,
604 eeprom_size);
605
606 BUG_ON(eeprom_size % 4);
607
608 af9015_config.eeprom_sum = 0;
609 for (reg = 0; reg < eeprom_size / sizeof(u32); reg++) {
610 af9015_config.eeprom_sum *= GOLDEN_RATIO_PRIME_32;
611 af9015_config.eeprom_sum += le32_to_cpu(((u32 *)eeprom)[reg]);
612 }
613
614 deb_info("%s: eeprom sum=%.8x\n", __func__, af9015_config.eeprom_sum);
615
616 ret = 0;
617free:
618 kfree(eeprom);
619 return ret;
Antti Palosaari80619de2008-09-15 17:18:09 -0300620}
621
Antti Palosaari80619de2008-09-15 17:18:09 -0300622static int af9015_init(struct dvb_usb_device *d)
623{
624 int ret;
625 deb_info("%s:\n", __func__);
626
Antti Palosaari1e8750c2011-03-18 19:36:42 -0300627 /* init RC canary */
628 ret = af9015_write_reg(d, 0x98e9, 0xff);
629 if (ret)
630 goto error;
631
Antti Palosaari80619de2008-09-15 17:18:09 -0300632 ret = af9015_init_endpoint(d);
633 if (ret)
634 goto error;
635
Antti Palosaari80619de2008-09-15 17:18:09 -0300636error:
637 return ret;
638}
639
640static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
641{
642 int ret;
643 deb_info("%s: onoff:%d\n", __func__, onoff);
644
645 if (onoff)
646 ret = af9015_set_reg_bit(adap->dev, 0xd503, 0);
647 else
648 ret = af9015_clear_reg_bit(adap->dev, 0xd503, 0);
649
650 return ret;
651}
652
653static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
654 int onoff)
655{
656 int ret;
657 u8 idx;
658
659 deb_info("%s: set pid filter, index %d, pid %x, onoff %d\n",
660 __func__, index, pid, onoff);
661
662 ret = af9015_write_reg(adap->dev, 0xd505, (pid & 0xff));
663 if (ret)
664 goto error;
665
666 ret = af9015_write_reg(adap->dev, 0xd506, (pid >> 8));
667 if (ret)
668 goto error;
669
670 idx = ((index & 0x1f) | (1 << 5));
671 ret = af9015_write_reg(adap->dev, 0xd504, idx);
672
673error:
674 return ret;
675}
676
677static int af9015_download_firmware(struct usb_device *udev,
678 const struct firmware *fw)
679{
Antti Palosaari582e5652011-03-19 16:51:43 -0300680 int i, len, remaining, ret;
Antti Palosaari80619de2008-09-15 17:18:09 -0300681 struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
Antti Palosaari80619de2008-09-15 17:18:09 -0300682 u16 checksum = 0;
683
684 deb_info("%s:\n", __func__);
685
686 /* calc checksum */
687 for (i = 0; i < fw->size; i++)
688 checksum += fw->data[i];
689
690 af9015_config.firmware_size = fw->size;
691 af9015_config.firmware_checksum = checksum;
692
Antti Palosaari582e5652011-03-19 16:51:43 -0300693 #define FW_ADDR 0x5100 /* firmware start address */
694 #define LEN_MAX 55 /* max packet size */
695 for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
696 len = remaining;
697 if (len > LEN_MAX)
698 len = LEN_MAX;
Antti Palosaari80619de2008-09-15 17:18:09 -0300699
700 req.data_len = len;
Antti Palosaari582e5652011-03-19 16:51:43 -0300701 req.data = (u8 *) &fw->data[fw->size - remaining];
702 req.addr = FW_ADDR + fw->size - remaining;
Antti Palosaari80619de2008-09-15 17:18:09 -0300703
704 ret = af9015_rw_udev(udev, &req);
705 if (ret) {
Antti Palosaari582e5652011-03-19 16:51:43 -0300706 err("firmware download failed:%d", ret);
Antti Palosaari80619de2008-09-15 17:18:09 -0300707 goto error;
708 }
709 }
Antti Palosaari80619de2008-09-15 17:18:09 -0300710
711 /* firmware loaded, request boot */
712 req.cmd = BOOT;
713 ret = af9015_rw_udev(udev, &req);
714 if (ret) {
715 err("firmware boot failed:%d", ret);
716 goto error;
717 }
718
Antti Palosaari80619de2008-09-15 17:18:09 -0300719error:
720 return ret;
721}
722
Antti Palosaari1cd72782010-10-12 17:22:32 -0300723struct af9015_rc_setup {
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300724 unsigned int id;
Antti Palosaari1cd72782010-10-12 17:22:32 -0300725 char *rc_codes;
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300726};
727
Antti Palosaari1cd72782010-10-12 17:22:32 -0300728static char *af9015_rc_setup_match(unsigned int id,
729 const struct af9015_rc_setup *table)
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300730{
Antti Palosaari1cd72782010-10-12 17:22:32 -0300731 for (; table->rc_codes; table++)
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300732 if (table->id == id)
Antti Palosaari1cd72782010-10-12 17:22:32 -0300733 return table->rc_codes;
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300734 return NULL;
735}
736
Antti Palosaari1cd72782010-10-12 17:22:32 -0300737static const struct af9015_rc_setup af9015_rc_setup_modparam[] = {
738 { AF9015_REMOTE_A_LINK_DTU_M, RC_MAP_ALINK_DTU_M },
739 { AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3, RC_MAP_MSI_DIGIVOX_II },
740 { AF9015_REMOTE_MYGICTV_U718, RC_MAP_TOTAL_MEDIA_IN_HAND },
741 { AF9015_REMOTE_DIGITTRADE_DVB_T, RC_MAP_DIGITTRADE },
742 { AF9015_REMOTE_AVERMEDIA_KS, RC_MAP_AVERMEDIA_RM_KS },
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300743 { }
744};
745
Antti Palosaari1cd72782010-10-12 17:22:32 -0300746static const struct af9015_rc_setup af9015_rc_setup_hashes[] = {
747 { 0xb8feb708, RC_MAP_MSI_DIGIVOX_II },
748 { 0xa3703d00, RC_MAP_ALINK_DTU_M },
749 { 0x9b7dc64e, RC_MAP_TOTAL_MEDIA_IN_HAND }, /* MYGICTV U718 */
Juergen Lock879b0d72011-06-12 17:25:12 -0300750 { 0x5d49e3db, RC_MAP_DIGITTRADE }, /* LC-Power LC-USB-DVBT */
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300751 { }
752};
753
Antti Palosaari1cd72782010-10-12 17:22:32 -0300754static const struct af9015_rc_setup af9015_rc_setup_usbids[] = {
Antti Palosaari04599c22011-03-19 14:25:36 -0300755 { (USB_VID_TERRATEC << 16) + USB_PID_TERRATEC_CINERGY_T_STICK_RC,
756 RC_MAP_TERRATEC_SLIM_2 },
Antti Palosaari1cd72782010-10-12 17:22:32 -0300757 { (USB_VID_TERRATEC << 16) + USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC,
758 RC_MAP_TERRATEC_SLIM },
759 { (USB_VID_VISIONPLUS << 16) + USB_PID_AZUREWAVE_AD_TU700,
760 RC_MAP_AZUREWAVE_AD_TU700 },
761 { (USB_VID_VISIONPLUS << 16) + USB_PID_TINYTWIN,
762 RC_MAP_AZUREWAVE_AD_TU700 },
763 { (USB_VID_MSI_2 << 16) + USB_PID_MSI_DIGI_VOX_MINI_III,
764 RC_MAP_MSI_DIGIVOX_III },
Antti Palosaariae81aab2011-06-15 11:29:47 -0300765 { (USB_VID_MSI_2 << 16) + USB_PID_MSI_DIGIVOX_DUO,
766 RC_MAP_MSI_DIGIVOX_III },
Antti Palosaari1cd72782010-10-12 17:22:32 -0300767 { (USB_VID_LEADTEK << 16) + USB_PID_WINFAST_DTV_DONGLE_GOLD,
768 RC_MAP_LEADTEK_Y04G0051 },
769 { (USB_VID_AVERMEDIA << 16) + USB_PID_AVERMEDIA_VOLAR_X,
770 RC_MAP_AVERMEDIA_M135A },
771 { (USB_VID_AFATECH << 16) + USB_PID_TREKSTOR_DVBT,
772 RC_MAP_TREKSTOR },
Antti Palosaaribd864ce2010-10-22 20:37:11 -0300773 { (USB_VID_KWORLD_2 << 16) + USB_PID_TINYTWIN_2,
774 RC_MAP_DIGITALNOW_TINYTWIN },
Antti Palosaarif8c61272010-10-23 07:35:31 -0300775 { (USB_VID_GTEK << 16) + USB_PID_TINYTWIN_3,
776 RC_MAP_DIGITALNOW_TINYTWIN },
Jiri Slabye3a0cc62010-01-22 12:10:55 -0300777 { }
778};
779
Jiri Slaby634d2d72010-01-22 12:10:53 -0300780static void af9015_set_remote_config(struct usb_device *udev,
781 struct dvb_usb_device_properties *props)
782{
Antti Palosaari1cd72782010-10-12 17:22:32 -0300783 u16 vid = le16_to_cpu(udev->descriptor.idVendor);
784 u16 pid = le16_to_cpu(udev->descriptor.idProduct);
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300785
Antti Palosaari1cd72782010-10-12 17:22:32 -0300786 /* try to load remote based module param */
787 props->rc.core.rc_codes = af9015_rc_setup_match(
788 dvb_usb_af9015_remote, af9015_rc_setup_modparam);
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300789
Antti Palosaari1cd72782010-10-12 17:22:32 -0300790 /* try to load remote based eeprom hash */
791 if (!props->rc.core.rc_codes)
792 props->rc.core.rc_codes = af9015_rc_setup_match(
793 af9015_config.eeprom_sum, af9015_rc_setup_hashes);
Jiri Slabye3a0cc62010-01-22 12:10:55 -0300794
Antti Palosaari1cd72782010-10-12 17:22:32 -0300795 /* try to load remote based USB ID */
796 if (!props->rc.core.rc_codes)
797 props->rc.core.rc_codes = af9015_rc_setup_match(
798 (vid << 16) + pid, af9015_rc_setup_usbids);
799
800 /* try to load remote based USB iManufacturer string */
801 if (!props->rc.core.rc_codes && vid == USB_VID_AFATECH) {
802 /* Check USB manufacturer and product strings and try
803 to determine correct remote in case of chip vendor
804 reference IDs are used.
805 DO NOT ADD ANYTHING NEW HERE. Use hashes instead. */
806 char manufacturer[10];
807 memset(manufacturer, 0, sizeof(manufacturer));
808 usb_string(udev, udev->descriptor.iManufacturer,
809 manufacturer, sizeof(manufacturer));
810 if (!strcmp("MSI", manufacturer)) {
811 /* iManufacturer 1 MSI
812 iProduct 2 MSI K-VOX */
813 props->rc.core.rc_codes = af9015_rc_setup_match(
814 AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
815 af9015_rc_setup_modparam);
816 }
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300817 }
Antti Palosaari74c8e3a2010-10-22 18:45:18 -0300818
819 /* finally load "empty" just for leaving IR receiver enabled */
820 if (!props->rc.core.rc_codes)
821 props->rc.core.rc_codes = RC_MAP_EMPTY;
822
Antti Palosaari1cd72782010-10-12 17:22:32 -0300823 return;
Jiri Slaby634d2d72010-01-22 12:10:53 -0300824}
825
Antti Palosaari80619de2008-09-15 17:18:09 -0300826static int af9015_read_config(struct usb_device *udev)
827{
828 int ret;
829 u8 val, i, offset = 0;
830 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
Antti Palosaari80619de2008-09-15 17:18:09 -0300831
832 /* IR remote controller */
833 req.addr = AF9015_EEPROM_IR_MODE;
Antti Palosaarid1a470f2009-01-20 14:56:20 -0300834 /* first message will timeout often due to possible hw bug */
835 for (i = 0; i < 4; i++) {
836 ret = af9015_rw_udev(udev, &req);
837 if (!ret)
838 break;
839 }
Antti Palosaari80619de2008-09-15 17:18:09 -0300840 if (ret)
841 goto error;
Jiri Slaby6c614042010-01-22 12:10:52 -0300842
843 ret = af9015_eeprom_hash(udev);
844 if (ret)
845 goto error;
846
Antti Palosaari80619de2008-09-15 17:18:09 -0300847 deb_info("%s: IR mode:%d\n", __func__, val);
848 for (i = 0; i < af9015_properties_count; i++) {
Antti Palosaari74c8e3a2010-10-22 18:45:18 -0300849 if (val == AF9015_IR_MODE_DISABLED)
850 af9015_properties[i].rc.core.rc_codes = NULL;
851 else
Jiri Slaby634d2d72010-01-22 12:10:53 -0300852 af9015_set_remote_config(udev, &af9015_properties[i]);
Antti Palosaari80619de2008-09-15 17:18:09 -0300853 }
854
855 /* TS mode - one or two receivers */
856 req.addr = AF9015_EEPROM_TS_MODE;
857 ret = af9015_rw_udev(udev, &req);
858 if (ret)
859 goto error;
860 af9015_config.dual_mode = val;
861 deb_info("%s: TS mode:%d\n", __func__, af9015_config.dual_mode);
Antti Palosaari80619de2008-09-15 17:18:09 -0300862
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300863 /* Set adapter0 buffer size according to USB port speed, adapter1 buffer
864 size can be static because it is enabled only USB2.0 */
Antti Palosaari80619de2008-09-15 17:18:09 -0300865 for (i = 0; i < af9015_properties_count; i++) {
866 /* USB1.1 set smaller buffersize and disable 2nd adapter */
867 if (udev->speed == USB_SPEED_FULL) {
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300868 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
Antti Palosaari9c863272009-09-12 13:35:29 -0300869 = TS_USB11_FRAME_SIZE;
Antti Palosaari80619de2008-09-15 17:18:09 -0300870 /* disable 2nd adapter because we don't have
871 PID-filters */
872 af9015_config.dual_mode = 0;
873 } else {
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300874 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
Jose Alberto Reguero353330c2009-09-12 09:51:36 -0300875 = TS_USB20_FRAME_SIZE;
Antti Palosaari80619de2008-09-15 17:18:09 -0300876 }
877 }
878
879 if (af9015_config.dual_mode) {
880 /* read 2nd demodulator I2C address */
881 req.addr = AF9015_EEPROM_DEMOD2_I2C;
882 ret = af9015_rw_udev(udev, &req);
883 if (ret)
884 goto error;
885 af9015_af9013_config[1].demod_address = val;
886
887 /* enable 2nd adapter */
888 for (i = 0; i < af9015_properties_count; i++)
889 af9015_properties[i].num_adapters = 2;
890
891 } else {
892 /* disable 2nd adapter */
893 for (i = 0; i < af9015_properties_count; i++)
894 af9015_properties[i].num_adapters = 1;
895 }
896
897 for (i = 0; i < af9015_properties[0].num_adapters; i++) {
898 if (i == 1)
899 offset = AF9015_EEPROM_OFFSET;
900 /* xtal */
901 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
902 ret = af9015_rw_udev(udev, &req);
903 if (ret)
904 goto error;
905 switch (val) {
906 case 0:
907 af9015_af9013_config[i].adc_clock = 28800;
908 break;
909 case 1:
910 af9015_af9013_config[i].adc_clock = 20480;
911 break;
912 case 2:
913 af9015_af9013_config[i].adc_clock = 28000;
914 break;
915 case 3:
916 af9015_af9013_config[i].adc_clock = 25000;
917 break;
918 };
919 deb_info("%s: [%d] xtal:%d set adc_clock:%d\n", __func__, i,
920 val, af9015_af9013_config[i].adc_clock);
921
922 /* tuner IF */
923 req.addr = AF9015_EEPROM_IF1H + offset;
924 ret = af9015_rw_udev(udev, &req);
925 if (ret)
926 goto error;
927 af9015_af9013_config[i].tuner_if = val << 8;
928 req.addr = AF9015_EEPROM_IF1L + offset;
929 ret = af9015_rw_udev(udev, &req);
930 if (ret)
931 goto error;
932 af9015_af9013_config[i].tuner_if += val;
933 deb_info("%s: [%d] IF1:%d\n", __func__, i,
934 af9015_af9013_config[0].tuner_if);
935
936 /* MT2060 IF1 */
937 req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
938 ret = af9015_rw_udev(udev, &req);
939 if (ret)
940 goto error;
941 af9015_config.mt2060_if1[i] = val << 8;
942 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
943 ret = af9015_rw_udev(udev, &req);
944 if (ret)
945 goto error;
946 af9015_config.mt2060_if1[i] += val;
947 deb_info("%s: [%d] MT2060 IF1:%d\n", __func__, i,
948 af9015_config.mt2060_if1[i]);
949
950 /* tuner */
951 req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
952 ret = af9015_rw_udev(udev, &req);
953 if (ret)
954 goto error;
955 switch (val) {
956 case AF9013_TUNER_ENV77H11D5:
957 case AF9013_TUNER_MT2060:
Antti Palosaari80619de2008-09-15 17:18:09 -0300958 case AF9013_TUNER_QT1010:
959 case AF9013_TUNER_UNKNOWN:
960 case AF9013_TUNER_MT2060_2:
961 case AF9013_TUNER_TDA18271:
962 case AF9013_TUNER_QT1010A:
Antti Palosaariee3d4402010-08-13 03:51:26 -0300963 case AF9013_TUNER_TDA18218:
Antti Palosaari80619de2008-09-15 17:18:09 -0300964 af9015_af9013_config[i].rf_spec_inv = 1;
965 break;
966 case AF9013_TUNER_MXL5003D:
967 case AF9013_TUNER_MXL5005D:
968 case AF9013_TUNER_MXL5005R:
Antti Palosaariab07fdd2010-09-09 14:59:10 -0300969 case AF9013_TUNER_MXL5007T:
Antti Palosaari80619de2008-09-15 17:18:09 -0300970 af9015_af9013_config[i].rf_spec_inv = 0;
971 break;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300972 case AF9013_TUNER_MC44S803:
973 af9015_af9013_config[i].gpio[1] = AF9013_GPIO_LO;
974 af9015_af9013_config[i].rf_spec_inv = 1;
975 break;
Antti Palosaari80619de2008-09-15 17:18:09 -0300976 default:
977 warn("tuner id:%d not supported, please report!", val);
978 return -ENODEV;
979 };
980
981 af9015_af9013_config[i].tuner = val;
982 deb_info("%s: [%d] tuner id:%d\n", __func__, i, val);
983 }
984
985error:
986 if (ret)
987 err("eeprom read failed:%d", ret);
988
Antti Palosaari3956fef2009-03-31 17:01:02 -0300989 /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
Yann E. MORIN8ccdf1a2010-10-01 16:55:43 -0300990 content :-( Override some wrong values here. Ditto for the
991 AVerTV Red HD+ (A850T) device. */
Antti Palosaari3956fef2009-03-31 17:01:02 -0300992 if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
Antti Palosaari9a3ecc72010-10-07 21:37:06 -0300993 ((le16_to_cpu(udev->descriptor.idProduct) ==
994 USB_PID_AVERMEDIA_A850) ||
995 (le16_to_cpu(udev->descriptor.idProduct) ==
996 USB_PID_AVERMEDIA_A850T))) {
Antti Palosaari3956fef2009-03-31 17:01:02 -0300997 deb_info("%s: AverMedia A850: overriding config\n", __func__);
998 /* disable dual mode */
999 af9015_config.dual_mode = 0;
1000 /* disable 2nd adapter */
1001 for (i = 0; i < af9015_properties_count; i++)
1002 af9015_properties[i].num_adapters = 1;
1003
1004 /* set correct IF */
1005 af9015_af9013_config[0].tuner_if = 4570;
1006 }
1007
Antti Palosaari80619de2008-09-15 17:18:09 -03001008 return ret;
1009}
1010
1011static int af9015_identify_state(struct usb_device *udev,
1012 struct dvb_usb_device_properties *props,
1013 struct dvb_usb_device_description **desc,
1014 int *cold)
1015{
1016 int ret;
1017 u8 reply;
1018 struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
1019
1020 ret = af9015_rw_udev(udev, &req);
1021 if (ret)
1022 return ret;
1023
1024 deb_info("%s: reply:%02x\n", __func__, reply);
1025 if (reply == 0x02)
1026 *cold = 0;
1027 else
1028 *cold = 1;
1029
1030 return ret;
1031}
1032
Antti Palosaari1cd72782010-10-12 17:22:32 -03001033static int af9015_rc_query(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -03001034{
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001035 struct af9015_state *priv = d->priv;
1036 int ret;
Ian Armstrongc1e13972011-03-18 19:23:05 -03001037 u8 buf[17];
Antti Palosaari80619de2008-09-15 17:18:09 -03001038
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001039 /* read registers needed to detect remote controller code */
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001040 ret = af9015_read_regs(d, 0x98d9, buf, sizeof(buf));
Antti Palosaari80619de2008-09-15 17:18:09 -03001041 if (ret)
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001042 goto error;
Antti Palosaari80619de2008-09-15 17:18:09 -03001043
Ian Armstrongc1e13972011-03-18 19:23:05 -03001044 /* If any of these are non-zero, assume invalid data */
1045 if (buf[1] || buf[2] || buf[3])
1046 return ret;
1047
1048 /* Check for repeat of previous code */
1049 if ((priv->rc_repeat != buf[6] || buf[0]) &&
1050 !memcmp(&buf[12], priv->rc_last, 4)) {
1051 deb_rc("%s: key repeated\n", __func__);
1052 rc_keydown(d->rc_dev, priv->rc_keycode, 0);
1053 priv->rc_repeat = buf[6];
1054 return ret;
1055 }
1056
1057 /* Only process key if canary killed */
1058 if (buf[16] != 0xff && buf[0] != 0x01) {
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001059 deb_rc("%s: key pressed %02x %02x %02x %02x\n", __func__,
1060 buf[12], buf[13], buf[14], buf[15]);
Antti Palosaari80619de2008-09-15 17:18:09 -03001061
Antti Palosaari1e8750c2011-03-18 19:36:42 -03001062 /* Reset the canary */
1063 ret = af9015_write_reg(d, 0x98e9, 0xff);
1064 if (ret)
1065 goto error;
1066
Ian Armstrongc1e13972011-03-18 19:23:05 -03001067 /* Remember this key */
1068 memcpy(priv->rc_last, &buf[12], 4);
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001069 if (buf[14] == (u8) ~buf[15]) {
1070 if (buf[12] == (u8) ~buf[13]) {
Antti Palosaari1cd72782010-10-12 17:22:32 -03001071 /* NEC */
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001072 priv->rc_keycode = buf[12] << 8 | buf[14];
Antti Palosaari1cd72782010-10-12 17:22:32 -03001073 } else {
1074 /* NEC extended*/
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001075 priv->rc_keycode = buf[12] << 16 |
1076 buf[13] << 8 | buf[14];
Antti Palosaari1cd72782010-10-12 17:22:32 -03001077 }
Antti Palosaari1cd72782010-10-12 17:22:32 -03001078 } else {
Antti Palosaari1e8750c2011-03-18 19:36:42 -03001079 /* 32 bit NEC */
Ian Armstrongc1e13972011-03-18 19:23:05 -03001080 priv->rc_keycode = buf[12] << 24 | buf[13] << 16 |
1081 buf[14] << 8 | buf[15];
Antti Palosaari1cd72782010-10-12 17:22:32 -03001082 }
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -03001083 rc_keydown(d->rc_dev, priv->rc_keycode, 0);
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001084 } else {
1085 deb_rc("%s: no key press\n", __func__);
Ian Armstrongc1e13972011-03-18 19:23:05 -03001086 /* Invalidate last keypress */
1087 /* Not really needed, but helps with debug */
1088 priv->rc_last[2] = priv->rc_last[3];
Antti Palosaari80619de2008-09-15 17:18:09 -03001089 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001090
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001091 priv->rc_repeat = buf[6];
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001092
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001093error:
1094 if (ret)
1095 err("%s: failed:%d", __func__, ret);
1096
1097 return ret;
Antti Palosaari80619de2008-09-15 17:18:09 -03001098}
1099
1100/* init 2nd I2C adapter */
Antti Palosaari349d0422008-11-05 16:31:24 -03001101static int af9015_i2c_init(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -03001102{
1103 int ret;
1104 struct af9015_state *state = d->priv;
1105 deb_info("%s:\n", __func__);
1106
1107 strncpy(state->i2c_adap.name, d->desc->name,
1108 sizeof(state->i2c_adap.name));
Antti Palosaari80619de2008-09-15 17:18:09 -03001109 state->i2c_adap.algo = d->props.i2c_algo;
1110 state->i2c_adap.algo_data = NULL;
1111 state->i2c_adap.dev.parent = &d->udev->dev;
1112
1113 i2c_set_adapdata(&state->i2c_adap, d);
1114
1115 ret = i2c_add_adapter(&state->i2c_adap);
1116 if (ret < 0)
1117 err("could not add i2c adapter");
1118
1119 return ret;
1120}
1121
1122static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
1123{
1124 int ret;
1125 struct af9015_state *state = adap->dev->priv;
1126 struct i2c_adapter *i2c_adap;
1127
1128 if (adap->id == 0) {
1129 /* select I2C adapter */
1130 i2c_adap = &adap->dev->i2c_adap;
1131
1132 deb_info("%s: init I2C\n", __func__);
1133 ret = af9015_i2c_init(adap->dev);
Antti Palosaari80619de2008-09-15 17:18:09 -03001134 } else {
1135 /* select I2C adapter */
1136 i2c_adap = &state->i2c_adap;
1137
1138 /* copy firmware to 2nd demodulator */
1139 if (af9015_config.dual_mode) {
1140 ret = af9015_copy_firmware(adap->dev);
1141 if (ret) {
1142 err("firmware copy to 2nd frontend " \
1143 "failed, will disable it");
1144 af9015_config.dual_mode = 0;
1145 return -ENODEV;
1146 }
1147 } else {
1148 return -ENODEV;
1149 }
1150 }
1151
1152 /* attach demodulator */
1153 adap->fe = dvb_attach(af9013_attach, &af9015_af9013_config[adap->id],
1154 i2c_adap);
1155
1156 return adap->fe == NULL ? -ENODEV : 0;
1157}
1158
1159static struct mt2060_config af9015_mt2060_config = {
1160 .i2c_address = 0xc0,
1161 .clock_out = 0,
1162};
1163
1164static struct qt1010_config af9015_qt1010_config = {
1165 .i2c_address = 0xc4,
1166};
1167
1168static struct tda18271_config af9015_tda18271_config = {
1169 .gate = TDA18271_GATE_DIGITAL,
Mauro Carvalho Chehab7655e592010-10-27 14:55:34 -02001170 .small_i2c = TDA18271_16_BYTE_CHUNK_INIT,
Antti Palosaari80619de2008-09-15 17:18:09 -03001171};
1172
1173static struct mxl5005s_config af9015_mxl5003_config = {
1174 .i2c_address = 0xc6,
1175 .if_freq = IF_FREQ_4570000HZ,
1176 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1177 .agc_mode = MXL_SINGLE_AGC,
1178 .tracking_filter = MXL_TF_DEFAULT,
Antti Palosaaria1310772008-09-22 13:59:25 -03001179 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -03001180 .cap_select = MXL_CAP_SEL_ENABLE,
1181 .div_out = MXL_DIV_OUT_4,
1182 .clock_out = MXL_CLOCK_OUT_DISABLE,
1183 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1184 .top = MXL5005S_TOP_25P2,
1185 .mod_mode = MXL_DIGITAL_MODE,
1186 .if_mode = MXL_ZERO_IF,
1187 .AgcMasterByte = 0x00,
1188};
1189
1190static struct mxl5005s_config af9015_mxl5005_config = {
1191 .i2c_address = 0xc6,
1192 .if_freq = IF_FREQ_4570000HZ,
1193 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1194 .agc_mode = MXL_SINGLE_AGC,
1195 .tracking_filter = MXL_TF_OFF,
Antti Palosaaria1310772008-09-22 13:59:25 -03001196 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -03001197 .cap_select = MXL_CAP_SEL_ENABLE,
1198 .div_out = MXL_DIV_OUT_4,
1199 .clock_out = MXL_CLOCK_OUT_DISABLE,
1200 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1201 .top = MXL5005S_TOP_25P2,
1202 .mod_mode = MXL_DIGITAL_MODE,
1203 .if_mode = MXL_ZERO_IF,
1204 .AgcMasterByte = 0x00,
1205};
1206
Jochen Friedrichd5633992009-02-02 14:59:50 -03001207static struct mc44s803_config af9015_mc44s803_config = {
1208 .i2c_address = 0xc0,
1209 .dig_out = 1,
1210};
1211
Antti Palosaariee3d4402010-08-13 03:51:26 -03001212static struct tda18218_config af9015_tda18218_config = {
1213 .i2c_address = 0xc0,
1214 .i2c_wr_max = 21, /* max wr bytes AF9015 I2C adap can handle at once */
1215};
1216
Antti Palosaariab07fdd2010-09-09 14:59:10 -03001217static struct mxl5007t_config af9015_mxl5007t_config = {
1218 .xtal_freq_hz = MxL_XTAL_24_MHZ,
1219 .if_freq_hz = MxL_IF_4_57_MHZ,
1220};
1221
Antti Palosaari80619de2008-09-15 17:18:09 -03001222static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
1223{
1224 struct af9015_state *state = adap->dev->priv;
1225 struct i2c_adapter *i2c_adap;
1226 int ret;
Antti Palosaari9a3ecc72010-10-07 21:37:06 -03001227 deb_info("%s:\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -03001228
1229 /* select I2C adapter */
1230 if (adap->id == 0)
1231 i2c_adap = &adap->dev->i2c_adap;
1232 else
1233 i2c_adap = &state->i2c_adap;
1234
1235 switch (af9015_af9013_config[adap->id].tuner) {
1236 case AF9013_TUNER_MT2060:
1237 case AF9013_TUNER_MT2060_2:
1238 ret = dvb_attach(mt2060_attach, adap->fe, i2c_adap,
1239 &af9015_mt2060_config,
1240 af9015_config.mt2060_if1[adap->id])
1241 == NULL ? -ENODEV : 0;
1242 break;
1243 case AF9013_TUNER_QT1010:
1244 case AF9013_TUNER_QT1010A:
1245 ret = dvb_attach(qt1010_attach, adap->fe, i2c_adap,
1246 &af9015_qt1010_config) == NULL ? -ENODEV : 0;
1247 break;
1248 case AF9013_TUNER_TDA18271:
1249 ret = dvb_attach(tda18271_attach, adap->fe, 0xc0, i2c_adap,
1250 &af9015_tda18271_config) == NULL ? -ENODEV : 0;
1251 break;
Antti Palosaariee3d4402010-08-13 03:51:26 -03001252 case AF9013_TUNER_TDA18218:
1253 ret = dvb_attach(tda18218_attach, adap->fe, i2c_adap,
1254 &af9015_tda18218_config) == NULL ? -ENODEV : 0;
1255 break;
Antti Palosaari80619de2008-09-15 17:18:09 -03001256 case AF9013_TUNER_MXL5003D:
1257 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1258 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
1259 break;
1260 case AF9013_TUNER_MXL5005D:
1261 case AF9013_TUNER_MXL5005R:
1262 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1263 &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
1264 break;
1265 case AF9013_TUNER_ENV77H11D5:
1266 ret = dvb_attach(dvb_pll_attach, adap->fe, 0xc0, i2c_adap,
1267 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
1268 break;
1269 case AF9013_TUNER_MC44S803:
Jochen Friedrichd5633992009-02-02 14:59:50 -03001270 ret = dvb_attach(mc44s803_attach, adap->fe, i2c_adap,
1271 &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
Antti Palosaari80619de2008-09-15 17:18:09 -03001272 break;
Antti Palosaariab07fdd2010-09-09 14:59:10 -03001273 case AF9013_TUNER_MXL5007T:
1274 ret = dvb_attach(mxl5007t_attach, adap->fe, i2c_adap,
1275 0xc0, &af9015_mxl5007t_config) == NULL ? -ENODEV : 0;
1276 break;
Antti Palosaari80619de2008-09-15 17:18:09 -03001277 case AF9013_TUNER_UNKNOWN:
1278 default:
1279 ret = -ENODEV;
1280 err("Unknown tuner id:%d",
1281 af9015_af9013_config[adap->id].tuner);
1282 }
1283 return ret;
1284}
1285
1286static struct usb_device_id af9015_usb_table[] = {
1287/* 0 */{USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015)},
1288 {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016)},
1289 {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD)},
1290 {USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E)},
1291 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U)},
1292/* 5 */{USB_DEVICE(USB_VID_VISIONPLUS,
1293 USB_PID_TINYTWIN)},
1294 {USB_DEVICE(USB_VID_VISIONPLUS,
1295 USB_PID_AZUREWAVE_AD_TU700)},
1296 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2)},
1297 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T)},
1298 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X)},
1299/* 10 */{USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380)},
1300 {USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO)},
1301 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2)},
Antti Palosaaria3765882008-09-19 18:34:06 -03001302 {USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2)},
Antti Palosaari05c1cab2008-09-22 12:32:37 -03001303 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309)},
Herbert Graeber641015a2008-10-07 10:06:36 -03001304/* 15 */{USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III)},
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001305 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U)},
Antti Palosaari71bf2e02009-01-13 12:47:28 -03001306 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2)},
Antti Palosaari58fe1592009-03-26 20:41:05 -03001307 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3)},
Marc Schneider26144842009-03-26 21:07:18 -03001308 {USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT)},
Antti Palosaari1ed5fad2009-04-09 15:14:18 -03001309/* 20 */{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850)},
1310 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805)},
Marcel Jueling734dd232009-04-09 17:16:41 -03001311 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU)},
Wen-chien Jesse Sung6e9c1a22009-04-28 01:11:22 -03001312 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810)},
Antti Palosaari22d46452009-05-31 17:07:01 -03001313 {USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03)},
Mart Raudseppc92f0562009-07-24 13:45:41 -03001314/* 25 */{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2)},
Antti Palosaari486ba122009-09-18 13:37:57 -03001315 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T)},
Ignacio de Miguel Diaz52322632009-11-13 23:13:34 -03001316 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20)},
Antti Palosaarifa1df552010-02-10 20:05:48 -03001317 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2)},
Antti Palosaari809c1e82010-02-10 20:07:30 -03001318 {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS)},
Antti Palosaariab9b4f22010-03-01 13:50:40 -03001319/* 30 */{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T)},
Antti Palosaari7fc87092010-03-01 14:06:52 -03001320 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4)},
Antti Palosaari2606cfa2010-05-23 18:26:37 -03001321 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M)},
Stefan Lippers-Hollmannd7ef4852010-08-25 10:08:48 -03001322 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_RC)},
Antti Palosaariab07fdd2010-09-09 14:59:10 -03001323 {USB_DEVICE(USB_VID_TERRATEC,
1324 USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC)},
Yann E. MORIN8ccdf1a2010-10-01 16:55:43 -03001325/* 35 */{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850T)},
Antti Palosaarif8c61272010-10-23 07:35:31 -03001326 {USB_DEVICE(USB_VID_GTEK, USB_PID_TINYTWIN_3)},
Antti Palosaari80619de2008-09-15 17:18:09 -03001327 {0},
1328};
1329MODULE_DEVICE_TABLE(usb, af9015_usb_table);
1330
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001331#define AF9015_RC_INTERVAL 500
Antti Palosaari80619de2008-09-15 17:18:09 -03001332static struct dvb_usb_device_properties af9015_properties[] = {
1333 {
1334 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1335
1336 .usb_ctrl = DEVICE_SPECIFIC,
1337 .download_firmware = af9015_download_firmware,
1338 .firmware = "dvb-usb-af9015.fw",
Jose Alberto Reguerocce25712008-11-13 14:14:18 -03001339 .no_reconnect = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001340
Antti Palosaari02542942009-09-16 20:33:03 -03001341 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari80619de2008-09-15 17:18:09 -03001342
1343 .num_adapters = 2,
1344 .adapter = {
1345 {
1346 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1347 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1348
1349 .pid_filter_count = 32,
1350 .pid_filter = af9015_pid_filter,
1351 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1352
1353 .frontend_attach =
1354 af9015_af9013_frontend_attach,
1355 .tuner_attach = af9015_tuner_attach,
1356 .stream = {
1357 .type = USB_BULK,
1358 .count = 6,
1359 .endpoint = 0x84,
1360 },
1361 },
1362 {
1363 .frontend_attach =
1364 af9015_af9013_frontend_attach,
1365 .tuner_attach = af9015_tuner_attach,
1366 .stream = {
1367 .type = USB_BULK,
1368 .count = 6,
1369 .endpoint = 0x85,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001370 .u = {
1371 .bulk = {
1372 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001373 TS_USB20_FRAME_SIZE,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001374 }
1375 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001376 },
1377 }
1378 },
1379
1380 .identify_state = af9015_identify_state,
1381
Antti Palosaari1cd72782010-10-12 17:22:32 -03001382 .rc.core = {
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001383 .protocol = RC_TYPE_NEC,
Antti Palosaari1cd72782010-10-12 17:22:32 -03001384 .module_name = "af9015",
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001385 .rc_query = af9015_rc_query,
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001386 .rc_interval = AF9015_RC_INTERVAL,
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001387 .allowed_protos = RC_TYPE_NEC,
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001388 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001389
1390 .i2c_algo = &af9015_i2c_algo,
1391
Yann E. MORIN8ccdf1a2010-10-01 16:55:43 -03001392 .num_device_descs = 12, /* check max from dvb-usb.h */
Antti Palosaari80619de2008-09-15 17:18:09 -03001393 .devices = {
1394 {
1395 .name = "Afatech AF9015 DVB-T USB2.0 stick",
1396 .cold_ids = {&af9015_usb_table[0],
1397 &af9015_usb_table[1], NULL},
1398 .warm_ids = {NULL},
1399 },
1400 {
1401 .name = "Leadtek WinFast DTV Dongle Gold",
1402 .cold_ids = {&af9015_usb_table[2], NULL},
1403 .warm_ids = {NULL},
1404 },
1405 {
1406 .name = "Pinnacle PCTV 71e",
1407 .cold_ids = {&af9015_usb_table[3], NULL},
1408 .warm_ids = {NULL},
1409 },
1410 {
1411 .name = "KWorld PlusTV Dual DVB-T Stick " \
1412 "(DVB-T 399U)",
Mart Raudseppc92f0562009-07-24 13:45:41 -03001413 .cold_ids = {&af9015_usb_table[4],
1414 &af9015_usb_table[25], NULL},
Antti Palosaari80619de2008-09-15 17:18:09 -03001415 .warm_ids = {NULL},
1416 },
1417 {
1418 .name = "DigitalNow TinyTwin DVB-T Receiver",
Antti Palosaarifa1df552010-02-10 20:05:48 -03001419 .cold_ids = {&af9015_usb_table[5],
Antti Palosaarif8c61272010-10-23 07:35:31 -03001420 &af9015_usb_table[28],
1421 &af9015_usb_table[36], NULL},
Antti Palosaari80619de2008-09-15 17:18:09 -03001422 .warm_ids = {NULL},
1423 },
1424 {
1425 .name = "TwinHan AzureWave AD-TU700(704J)",
1426 .cold_ids = {&af9015_usb_table[6], NULL},
1427 .warm_ids = {NULL},
1428 },
1429 {
1430 .name = "TerraTec Cinergy T USB XE",
1431 .cold_ids = {&af9015_usb_table[7], NULL},
1432 .warm_ids = {NULL},
1433 },
1434 {
1435 .name = "KWorld PlusTV Dual DVB-T PCI " \
1436 "(DVB-T PC160-2T)",
1437 .cold_ids = {&af9015_usb_table[8], NULL},
1438 .warm_ids = {NULL},
1439 },
1440 {
1441 .name = "AVerMedia AVerTV DVB-T Volar X",
1442 .cold_ids = {&af9015_usb_table[9], NULL},
1443 .warm_ids = {NULL},
1444 },
Antti Palosaari76391a72010-09-09 12:10:50 -03001445 {
1446 .name = "TerraTec Cinergy T Stick RC",
1447 .cold_ids = {&af9015_usb_table[33], NULL},
1448 .warm_ids = {NULL},
1449 },
Antti Palosaariab07fdd2010-09-09 14:59:10 -03001450 {
1451 .name = "TerraTec Cinergy T Stick Dual RC",
1452 .cold_ids = {&af9015_usb_table[34], NULL},
1453 .warm_ids = {NULL},
1454 },
Yann E. MORIN8ccdf1a2010-10-01 16:55:43 -03001455 {
1456 .name = "AverMedia AVerTV Red HD+ (A850T)",
1457 .cold_ids = {&af9015_usb_table[35], NULL},
1458 .warm_ids = {NULL},
1459 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001460 }
1461 }, {
1462 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1463
1464 .usb_ctrl = DEVICE_SPECIFIC,
1465 .download_firmware = af9015_download_firmware,
1466 .firmware = "dvb-usb-af9015.fw",
Jose Alberto Reguerocce25712008-11-13 14:14:18 -03001467 .no_reconnect = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001468
Antti Palosaari02542942009-09-16 20:33:03 -03001469 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari80619de2008-09-15 17:18:09 -03001470
1471 .num_adapters = 2,
1472 .adapter = {
1473 {
1474 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1475 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1476
1477 .pid_filter_count = 32,
1478 .pid_filter = af9015_pid_filter,
1479 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1480
1481 .frontend_attach =
1482 af9015_af9013_frontend_attach,
1483 .tuner_attach = af9015_tuner_attach,
1484 .stream = {
1485 .type = USB_BULK,
1486 .count = 6,
1487 .endpoint = 0x84,
1488 },
1489 },
1490 {
1491 .frontend_attach =
1492 af9015_af9013_frontend_attach,
1493 .tuner_attach = af9015_tuner_attach,
1494 .stream = {
1495 .type = USB_BULK,
1496 .count = 6,
1497 .endpoint = 0x85,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001498 .u = {
1499 .bulk = {
1500 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001501 TS_USB20_FRAME_SIZE,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001502 }
1503 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001504 },
1505 }
1506 },
1507
1508 .identify_state = af9015_identify_state,
1509
Antti Palosaari1cd72782010-10-12 17:22:32 -03001510 .rc.core = {
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001511 .protocol = RC_TYPE_NEC,
Antti Palosaari1cd72782010-10-12 17:22:32 -03001512 .module_name = "af9015",
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001513 .rc_query = af9015_rc_query,
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001514 .rc_interval = AF9015_RC_INTERVAL,
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001515 .allowed_protos = RC_TYPE_NEC,
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001516 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001517
1518 .i2c_algo = &af9015_i2c_algo,
1519
Antti Palosaaria44b91d2010-09-09 12:05:31 -03001520 .num_device_descs = 9, /* check max from dvb-usb.h */
Antti Palosaari80619de2008-09-15 17:18:09 -03001521 .devices = {
1522 {
1523 .name = "Xtensions XD-380",
1524 .cold_ids = {&af9015_usb_table[10], NULL},
1525 .warm_ids = {NULL},
1526 },
1527 {
1528 .name = "MSI DIGIVOX Duo",
1529 .cold_ids = {&af9015_usb_table[11], NULL},
1530 .warm_ids = {NULL},
1531 },
1532 {
1533 .name = "Fujitsu-Siemens Slim Mobile USB DVB-T",
1534 .cold_ids = {&af9015_usb_table[12], NULL},
1535 .warm_ids = {NULL},
1536 },
Mikko Ohtamaa111f9ec2008-09-19 18:26:05 -03001537 {
Antti Palosaaria3765882008-09-19 18:34:06 -03001538 .name = "Telestar Starstick 2",
Mikko Ohtamaa111f9ec2008-09-19 18:26:05 -03001539 .cold_ids = {&af9015_usb_table[13], NULL},
1540 .warm_ids = {NULL},
1541 },
Antti Palosaari05c1cab2008-09-22 12:32:37 -03001542 {
1543 .name = "AVerMedia A309",
1544 .cold_ids = {&af9015_usb_table[14], NULL},
1545 .warm_ids = {NULL},
1546 },
Herbert Graeber641015a2008-10-07 10:06:36 -03001547 {
1548 .name = "MSI Digi VOX mini III",
1549 .cold_ids = {&af9015_usb_table[15], NULL},
1550 .warm_ids = {NULL},
1551 },
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001552 {
1553 .name = "KWorld USB DVB-T TV Stick II " \
1554 "(VS-DVB-T 395U)",
Antti Palosaari71bf2e02009-01-13 12:47:28 -03001555 .cold_ids = {&af9015_usb_table[16],
Antti Palosaari58fe1592009-03-26 20:41:05 -03001556 &af9015_usb_table[17],
Antti Palosaari7fc87092010-03-01 14:06:52 -03001557 &af9015_usb_table[18],
1558 &af9015_usb_table[31], NULL},
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001559 .warm_ids = {NULL},
1560 },
Marc Schneider26144842009-03-26 21:07:18 -03001561 {
1562 .name = "TrekStor DVB-T USB Stick",
1563 .cold_ids = {&af9015_usb_table[19], NULL},
1564 .warm_ids = {NULL},
1565 },
Antti Palosaari3956fef2009-03-31 17:01:02 -03001566 {
1567 .name = "AverMedia AVerTV Volar Black HD " \
1568 "(A850)",
1569 .cold_ids = {&af9015_usb_table[20], NULL},
1570 .warm_ids = {NULL},
1571 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001572 }
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001573 }, {
1574 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1575
1576 .usb_ctrl = DEVICE_SPECIFIC,
1577 .download_firmware = af9015_download_firmware,
1578 .firmware = "dvb-usb-af9015.fw",
1579 .no_reconnect = 1,
1580
Antti Palosaari02542942009-09-16 20:33:03 -03001581 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001582
1583 .num_adapters = 2,
1584 .adapter = {
1585 {
1586 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1587 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1588
1589 .pid_filter_count = 32,
1590 .pid_filter = af9015_pid_filter,
1591 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1592
1593 .frontend_attach =
1594 af9015_af9013_frontend_attach,
1595 .tuner_attach = af9015_tuner_attach,
1596 .stream = {
1597 .type = USB_BULK,
1598 .count = 6,
1599 .endpoint = 0x84,
1600 },
1601 },
1602 {
1603 .frontend_attach =
1604 af9015_af9013_frontend_attach,
1605 .tuner_attach = af9015_tuner_attach,
1606 .stream = {
1607 .type = USB_BULK,
1608 .count = 6,
1609 .endpoint = 0x85,
1610 .u = {
1611 .bulk = {
1612 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001613 TS_USB20_FRAME_SIZE,
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001614 }
1615 }
1616 },
1617 }
1618 },
1619
1620 .identify_state = af9015_identify_state,
1621
Antti Palosaari1cd72782010-10-12 17:22:32 -03001622 .rc.core = {
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001623 .protocol = RC_TYPE_NEC,
Antti Palosaari1cd72782010-10-12 17:22:32 -03001624 .module_name = "af9015",
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001625 .rc_query = af9015_rc_query,
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001626 .rc_interval = AF9015_RC_INTERVAL,
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001627 .allowed_protos = RC_TYPE_NEC,
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001628 },
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001629
1630 .i2c_algo = &af9015_i2c_algo,
1631
Antti Palosaaria44b91d2010-09-09 12:05:31 -03001632 .num_device_descs = 9, /* check max from dvb-usb.h */
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001633 .devices = {
Antti Palosaari1ed5fad2009-04-09 15:14:18 -03001634 {
1635 .name = "AverMedia AVerTV Volar GPS 805 (A805)",
1636 .cold_ids = {&af9015_usb_table[21], NULL},
1637 .warm_ids = {NULL},
1638 },
Marcel Jueling734dd232009-04-09 17:16:41 -03001639 {
1640 .name = "Conceptronic USB2.0 DVB-T CTVDIGRCU " \
1641 "V3.0",
1642 .cold_ids = {&af9015_usb_table[22], NULL},
1643 .warm_ids = {NULL},
1644 },
Wen-chien Jesse Sung6e9c1a22009-04-28 01:11:22 -03001645 {
1646 .name = "KWorld Digial MC-810",
1647 .cold_ids = {&af9015_usb_table[23], NULL},
1648 .warm_ids = {NULL},
1649 },
Antti Palosaari22d46452009-05-31 17:07:01 -03001650 {
1651 .name = "Genius TVGo DVB-T03",
1652 .cold_ids = {&af9015_usb_table[24], NULL},
1653 .warm_ids = {NULL},
1654 },
Antti Palosaari486ba122009-09-18 13:37:57 -03001655 {
1656 .name = "KWorld PlusTV DVB-T PCI Pro Card " \
1657 "(DVB-T PC160-T)",
1658 .cold_ids = {&af9015_usb_table[26], NULL},
1659 .warm_ids = {NULL},
1660 },
Ignacio de Miguel Diaz52322632009-11-13 23:13:34 -03001661 {
1662 .name = "Sveon STV20 Tuner USB DVB-T HDTV",
1663 .cold_ids = {&af9015_usb_table[27], NULL},
1664 .warm_ids = {NULL},
1665 },
Antti Palosaari809c1e82010-02-10 20:07:30 -03001666 {
1667 .name = "Leadtek WinFast DTV2000DS",
1668 .cold_ids = {&af9015_usb_table[29], NULL},
1669 .warm_ids = {NULL},
1670 },
Antti Palosaariab9b4f22010-03-01 13:50:40 -03001671 {
1672 .name = "KWorld USB DVB-T Stick Mobile " \
1673 "(UB383-T)",
1674 .cold_ids = {&af9015_usb_table[30], NULL},
1675 .warm_ids = {NULL},
1676 },
Antti Palosaari2606cfa2010-05-23 18:26:37 -03001677 {
1678 .name = "AverMedia AVerTV Volar M (A815Mac)",
1679 .cold_ids = {&af9015_usb_table[32], NULL},
1680 .warm_ids = {NULL},
1681 },
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001682 }
1683 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001684};
Antti Palosaari80619de2008-09-15 17:18:09 -03001685
1686static int af9015_usb_probe(struct usb_interface *intf,
1687 const struct usb_device_id *id)
1688{
1689 int ret = 0;
1690 struct dvb_usb_device *d = NULL;
1691 struct usb_device *udev = interface_to_usbdev(intf);
1692 u8 i;
1693
1694 deb_info("%s: interface:%d\n", __func__,
1695 intf->cur_altsetting->desc.bInterfaceNumber);
1696
1697 /* interface 0 is used by DVB-T receiver and
1698 interface 1 is for remote controller (HID) */
1699 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
1700 ret = af9015_read_config(udev);
1701 if (ret)
1702 return ret;
1703
1704 for (i = 0; i < af9015_properties_count; i++) {
1705 ret = dvb_usb_device_init(intf, &af9015_properties[i],
1706 THIS_MODULE, &d, adapter_nr);
1707 if (!ret)
1708 break;
1709 if (ret != -ENODEV)
1710 return ret;
1711 }
1712 if (ret)
1713 return ret;
1714
1715 if (d)
1716 ret = af9015_init(d);
1717 }
1718
1719 return ret;
1720}
1721
Antti Palosaari349d0422008-11-05 16:31:24 -03001722static void af9015_i2c_exit(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -03001723{
1724 struct af9015_state *state = d->priv;
Antti Palosaari9a3ecc72010-10-07 21:37:06 -03001725 deb_info("%s:\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -03001726
1727 /* remove 2nd I2C adapter */
1728 if (d->state & DVB_USB_STATE_I2C)
1729 i2c_del_adapter(&state->i2c_adap);
1730}
1731
1732static void af9015_usb_device_exit(struct usb_interface *intf)
1733{
1734 struct dvb_usb_device *d = usb_get_intfdata(intf);
Antti Palosaari9a3ecc72010-10-07 21:37:06 -03001735 deb_info("%s:\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -03001736
1737 /* remove 2nd I2C adapter */
1738 if (d != NULL && d->desc != NULL)
1739 af9015_i2c_exit(d);
1740
1741 dvb_usb_device_exit(intf);
1742}
1743
1744/* usb specific object needed to register this driver with the usb subsystem */
1745static struct usb_driver af9015_usb_driver = {
1746 .name = "dvb_usb_af9015",
1747 .probe = af9015_usb_probe,
1748 .disconnect = af9015_usb_device_exit,
1749 .id_table = af9015_usb_table,
1750};
1751
1752/* module stuff */
1753static int __init af9015_usb_module_init(void)
1754{
1755 int ret;
1756 ret = usb_register(&af9015_usb_driver);
1757 if (ret)
1758 err("module init failed:%d", ret);
1759
1760 return ret;
1761}
1762
1763static void __exit af9015_usb_module_exit(void)
1764{
1765 /* deregister this driver from the USB subsystem */
1766 usb_deregister(&af9015_usb_driver);
1767}
1768
1769module_init(af9015_usb_module_init);
1770module_exit(af9015_usb_module_exit);
1771
1772MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1773MODULE_DESCRIPTION("Driver for Afatech AF9015 DVB-T");
1774MODULE_LICENSE("GPL");