blob: 750a1b515139da7f59dd22d83fd23846537cda18 [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 Palosaari80619de2008-09-15 17:18:09 -030035
Antti Palosaari349d0422008-11-05 16:31:24 -030036static int dvb_usb_af9015_debug;
Antti Palosaari80619de2008-09-15 17:18:09 -030037module_param_named(debug, dvb_usb_af9015_debug, int, 0644);
38MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
Antti Palosaari349d0422008-11-05 16:31:24 -030039static int dvb_usb_af9015_remote;
Antti Palosaari80619de2008-09-15 17:18:09 -030040module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
41MODULE_PARM_DESC(remote, "select remote");
Antti Palosaari80619de2008-09-15 17:18:09 -030042DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
43
44static DEFINE_MUTEX(af9015_usb_mutex);
45
46static struct af9015_config af9015_config;
Antti Palosaari85d7d7c2009-04-09 09:16:12 -030047static struct dvb_usb_device_properties af9015_properties[3];
Antti Palosaari349d0422008-11-05 16:31:24 -030048static int af9015_properties_count = ARRAY_SIZE(af9015_properties);
Antti Palosaari80619de2008-09-15 17:18:09 -030049
50static struct af9013_config af9015_af9013_config[] = {
51 {
52 .demod_address = AF9015_I2C_DEMOD,
53 .output_mode = AF9013_OUTPUT_MODE_USB,
54 .api_version = { 0, 1, 9, 0 },
55 .gpio[0] = AF9013_GPIO_HI,
Antti Palosaari80619de2008-09-15 17:18:09 -030056 .gpio[3] = AF9013_GPIO_TUNER_ON,
57
58 }, {
59 .output_mode = AF9013_OUTPUT_MODE_SERIAL,
60 .api_version = { 0, 1, 9, 0 },
61 .gpio[0] = AF9013_GPIO_TUNER_ON,
62 .gpio[1] = AF9013_GPIO_LO,
63 }
64};
65
66static int af9015_rw_udev(struct usb_device *udev, struct req_t *req)
67{
Antti Palosaari06565d72009-09-12 20:46:30 -030068#define BUF_LEN 63
69#define REQ_HDR_LEN 8 /* send header size */
70#define ACK_HDR_LEN 2 /* rece header size */
Antti Palosaari80619de2008-09-15 17:18:09 -030071 int act_len, ret;
Antti Palosaari06565d72009-09-12 20:46:30 -030072 u8 buf[BUF_LEN];
Antti Palosaari80619de2008-09-15 17:18:09 -030073 u8 write = 1;
Antti Palosaari06565d72009-09-12 20:46:30 -030074 u8 msg_len = REQ_HDR_LEN;
Antti Palosaari80619de2008-09-15 17:18:09 -030075 static u8 seq; /* packet sequence number */
76
77 if (mutex_lock_interruptible(&af9015_usb_mutex) < 0)
78 return -EAGAIN;
79
80 buf[0] = req->cmd;
81 buf[1] = seq++;
82 buf[2] = req->i2c_addr;
83 buf[3] = req->addr >> 8;
84 buf[4] = req->addr & 0xff;
85 buf[5] = req->mbox;
86 buf[6] = req->addr_len;
87 buf[7] = req->data_len;
88
89 switch (req->cmd) {
90 case GET_CONFIG:
Antti Palosaari80619de2008-09-15 17:18:09 -030091 case READ_MEMORY:
92 case RECONNECT_USB:
93 case GET_IR_CODE:
94 write = 0;
95 break;
96 case READ_I2C:
97 write = 0;
98 buf[2] |= 0x01; /* set I2C direction */
99 case WRITE_I2C:
100 buf[0] = READ_WRITE_I2C;
101 break;
102 case WRITE_MEMORY:
103 if (((req->addr & 0xff00) == 0xff00) ||
Antti Palosaarif4e96de2009-09-12 21:25:59 -0300104 ((req->addr & 0xff00) == 0xae00))
Antti Palosaari80619de2008-09-15 17:18:09 -0300105 buf[0] = WRITE_VIRTUAL_MEMORY;
106 case WRITE_VIRTUAL_MEMORY:
107 case COPY_FIRMWARE:
108 case DOWNLOAD_FIRMWARE:
Nils Kassubeba1bc642009-07-28 11:54:52 -0300109 case BOOT:
Antti Palosaari80619de2008-09-15 17:18:09 -0300110 break;
111 default:
112 err("unknown command:%d", req->cmd);
113 ret = -1;
114 goto error_unlock;
115 }
116
Antti Palosaari06565d72009-09-12 20:46:30 -0300117 /* buffer overflow check */
118 if ((write && (req->data_len > BUF_LEN - REQ_HDR_LEN)) ||
119 (!write && (req->data_len > BUF_LEN - ACK_HDR_LEN))) {
120 err("too much data; cmd:%d len:%d", req->cmd, req->data_len);
121 ret = -EINVAL;
122 goto error_unlock;
123 }
124
Antti Palosaari80619de2008-09-15 17:18:09 -0300125 /* write requested */
126 if (write) {
Antti Palosaari06565d72009-09-12 20:46:30 -0300127 memcpy(&buf[REQ_HDR_LEN], req->data, req->data_len);
Antti Palosaari80619de2008-09-15 17:18:09 -0300128 msg_len += req->data_len;
129 }
Antti Palosaari06565d72009-09-12 20:46:30 -0300130
Antti Palosaari80619de2008-09-15 17:18:09 -0300131 deb_xfer(">>> ");
132 debug_dump(buf, msg_len, deb_xfer);
133
134 /* send req */
135 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
Antti Palosaari06565d72009-09-12 20:46:30 -0300136 &act_len, AF9015_USB_TIMEOUT);
Antti Palosaari80619de2008-09-15 17:18:09 -0300137 if (ret)
138 err("bulk message failed:%d (%d/%d)", ret, msg_len, act_len);
139 else
140 if (act_len != msg_len)
141 ret = -1; /* all data is not send */
142 if (ret)
143 goto error_unlock;
144
145 /* no ack for those packets */
146 if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
147 goto exit_unlock;
148
Antti Palosaari06565d72009-09-12 20:46:30 -0300149 /* write receives seq + status = 2 bytes
150 read receives seq + status + data = 2 + N bytes */
151 msg_len = ACK_HDR_LEN;
152 if (!write)
153 msg_len += req->data_len;
154
Antti Palosaari80619de2008-09-15 17:18:09 -0300155 ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
Antti Palosaari06565d72009-09-12 20:46:30 -0300156 &act_len, AF9015_USB_TIMEOUT);
Antti Palosaari80619de2008-09-15 17:18:09 -0300157 if (ret) {
158 err("recv bulk message failed:%d", ret);
159 ret = -1;
160 goto error_unlock;
161 }
162
163 deb_xfer("<<< ");
164 debug_dump(buf, act_len, deb_xfer);
165
166 /* remote controller query status is 1 if remote code is not received */
167 if (req->cmd == GET_IR_CODE && buf[1] == 1) {
168 buf[1] = 0; /* clear command "error" status */
169 memset(&buf[2], 0, req->data_len);
170 buf[3] = 1; /* no remote code received mark */
171 }
172
173 /* check status */
174 if (buf[1]) {
175 err("command failed:%d", buf[1]);
176 ret = -1;
177 goto error_unlock;
178 }
179
180 /* read request, copy returned data to return buf */
181 if (!write)
Antti Palosaari06565d72009-09-12 20:46:30 -0300182 memcpy(req->data, &buf[ACK_HDR_LEN], req->data_len);
Antti Palosaari80619de2008-09-15 17:18:09 -0300183
184error_unlock:
185exit_unlock:
186 mutex_unlock(&af9015_usb_mutex);
187
188 return ret;
189}
190
191static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
192{
193 return af9015_rw_udev(d->udev, req);
194}
195
196static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
197 u8 len)
198{
199 struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
200 val};
201 return af9015_ctrl_msg(d, &req);
202}
203
204static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
205{
206 return af9015_write_regs(d, addr, &val, 1);
207}
208
209static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
210{
211 struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, 1, val};
212 return af9015_ctrl_msg(d, &req);
213}
214
215static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
216 u8 val)
217{
218 struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
219
220 if (addr == af9015_af9013_config[0].demod_address ||
221 addr == af9015_af9013_config[1].demod_address)
222 req.addr_len = 3;
223
224 return af9015_ctrl_msg(d, &req);
225}
226
227static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
228 u8 *val)
229{
230 struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
231
232 if (addr == af9015_af9013_config[0].demod_address ||
233 addr == af9015_af9013_config[1].demod_address)
234 req.addr_len = 3;
235
236 return af9015_ctrl_msg(d, &req);
237}
238
239static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
240 int num)
241{
242 struct dvb_usb_device *d = i2c_get_adapdata(adap);
243 int ret = 0, i = 0;
244 u16 addr;
245 u8 mbox, addr_len;
246 struct req_t req;
247
248/* TODO: implement bus lock
249
250The bus lock is needed because there is two tuners both using same I2C-address.
251Due to that the only way to select correct tuner is use demodulator I2C-gate.
252
253................................................
254. AF9015 includes integrated AF9013 demodulator.
255. ____________ ____________ . ____________
256.| uC | | demod | . | tuner |
257.|------------| |------------| . |------------|
258.| AF9015 | | AF9013/5 | . | MXL5003 |
259.| |--+----I2C-------|-----/ -----|-.-----I2C-------| |
260.| | | | addr 0x38 | . | addr 0xc6 |
261.|____________| | |____________| . |____________|
262.................|..............................
263 | ____________ ____________
264 | | demod | | tuner |
265 | |------------| |------------|
266 | | AF9013 | | MXL5003 |
267 +----I2C-------|-----/ -----|-------I2C-------| |
268 | addr 0x3a | | addr 0xc6 |
269 |____________| |____________|
270*/
271 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
272 return -EAGAIN;
273
274 while (i < num) {
275 if (msg[i].addr == af9015_af9013_config[0].demod_address ||
276 msg[i].addr == af9015_af9013_config[1].demod_address) {
277 addr = msg[i].buf[0] << 8;
278 addr += msg[i].buf[1];
279 mbox = msg[i].buf[2];
280 addr_len = 3;
281 } else {
282 addr = msg[i].buf[0];
283 addr_len = 1;
284 mbox = 0;
285 }
286
287 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
288 if (msg[i].addr ==
289 af9015_af9013_config[0].demod_address)
290 req.cmd = READ_MEMORY;
291 else
292 req.cmd = READ_I2C;
293 req.i2c_addr = msg[i].addr;
294 req.addr = addr;
295 req.mbox = mbox;
296 req.addr_len = addr_len;
297 req.data_len = msg[i+1].len;
298 req.data = &msg[i+1].buf[0];
299 ret = af9015_ctrl_msg(d, &req);
300 i += 2;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300301 } else if (msg[i].flags & I2C_M_RD) {
302 ret = -EINVAL;
303 if (msg[i].addr ==
304 af9015_af9013_config[0].demod_address)
305 goto error;
306 else
307 req.cmd = READ_I2C;
308 req.i2c_addr = msg[i].addr;
309 req.addr = addr;
310 req.mbox = mbox;
311 req.addr_len = addr_len;
312 req.data_len = msg[i].len;
313 req.data = &msg[i].buf[0];
314 ret = af9015_ctrl_msg(d, &req);
315 i += 1;
Antti Palosaari80619de2008-09-15 17:18:09 -0300316 } else {
317 if (msg[i].addr ==
318 af9015_af9013_config[0].demod_address)
319 req.cmd = WRITE_MEMORY;
320 else
321 req.cmd = WRITE_I2C;
322 req.i2c_addr = msg[i].addr;
323 req.addr = addr;
324 req.mbox = mbox;
325 req.addr_len = addr_len;
326 req.data_len = msg[i].len-addr_len;
327 req.data = &msg[i].buf[addr_len];
328 ret = af9015_ctrl_msg(d, &req);
329 i += 1;
330 }
331 if (ret)
332 goto error;
333
334 }
335 ret = i;
336
337error:
338 mutex_unlock(&d->i2c_mutex);
339
340 return ret;
341}
342
343static u32 af9015_i2c_func(struct i2c_adapter *adapter)
344{
345 return I2C_FUNC_I2C;
346}
347
348static struct i2c_algorithm af9015_i2c_algo = {
349 .master_xfer = af9015_i2c_xfer,
350 .functionality = af9015_i2c_func,
351};
352
353static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
354{
355 int ret;
356 u8 val, mask = 0x01;
357
358 ret = af9015_read_reg(d, addr, &val);
359 if (ret)
360 return ret;
361
362 mask <<= bit;
363 if (op) {
364 /* set bit */
365 val |= mask;
366 } else {
367 /* clear bit */
368 mask ^= 0xff;
369 val &= mask;
370 }
371
372 return af9015_write_reg(d, addr, val);
373}
374
375static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
376{
377 return af9015_do_reg_bit(d, addr, bit, 1);
378}
379
380static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
381{
382 return af9015_do_reg_bit(d, addr, bit, 0);
383}
384
385static int af9015_init_endpoint(struct dvb_usb_device *d)
386{
387 int ret;
388 u16 frame_size;
389 u8 packet_size;
390 deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);
391
Antti Palosaari9c863272009-09-12 13:35:29 -0300392 /* Windows driver uses packet count 21 for USB1.1 and 348 for USB2.0.
393 We use smaller - about 1/4 from the original, 5 and 87. */
Antti Palosaari80619de2008-09-15 17:18:09 -0300394#define TS_PACKET_SIZE 188
395
Antti Palosaari9c863272009-09-12 13:35:29 -0300396#define TS_USB20_PACKET_COUNT 87
Antti Palosaari80619de2008-09-15 17:18:09 -0300397#define TS_USB20_FRAME_SIZE (TS_PACKET_SIZE*TS_USB20_PACKET_COUNT)
398
Antti Palosaari9c863272009-09-12 13:35:29 -0300399#define TS_USB11_PACKET_COUNT 5
Antti Palosaari80619de2008-09-15 17:18:09 -0300400#define TS_USB11_FRAME_SIZE (TS_PACKET_SIZE*TS_USB11_PACKET_COUNT)
401
402#define TS_USB20_MAX_PACKET_SIZE 512
403#define TS_USB11_MAX_PACKET_SIZE 64
404
405 if (d->udev->speed == USB_SPEED_FULL) {
406 frame_size = TS_USB11_FRAME_SIZE/4;
407 packet_size = TS_USB11_MAX_PACKET_SIZE/4;
408 } else {
409 frame_size = TS_USB20_FRAME_SIZE/4;
410 packet_size = TS_USB20_MAX_PACKET_SIZE/4;
411 }
412
413 ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
414 if (ret)
415 goto error;
416 ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
417 if (ret)
418 goto error;
419 ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
420 if (ret)
421 goto error;
422 ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
423 if (ret)
424 goto error;
425 ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
426 if (ret)
427 goto error;
428 if (af9015_config.dual_mode) {
429 ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
430 if (ret)
431 goto error;
432 }
433 ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
434 if (ret)
435 goto error;
436 if (af9015_config.dual_mode) {
437 ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
438 if (ret)
439 goto error;
440 }
441 /* EP4 xfer length */
442 ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
443 if (ret)
444 goto error;
445 ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
446 if (ret)
447 goto error;
448 /* EP5 xfer length */
449 ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
450 if (ret)
451 goto error;
452 ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
453 if (ret)
454 goto error;
455 ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
456 if (ret)
457 goto error;
458 ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
459 if (ret)
460 goto error;
461 ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
462 if (ret)
463 goto error;
464 if (af9015_config.dual_mode) {
465 ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
466 if (ret)
467 goto error;
468 }
469
470 /* enable / disable mp2if2 */
471 if (af9015_config.dual_mode)
472 ret = af9015_set_reg_bit(d, 0xd50b, 0);
473 else
474 ret = af9015_clear_reg_bit(d, 0xd50b, 0);
475error:
476 if (ret)
477 err("endpoint init failed:%d", ret);
478 return ret;
479}
480
481static int af9015_copy_firmware(struct dvb_usb_device *d)
482{
483 int ret;
484 u8 fw_params[4];
485 u8 val, i;
486 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
487 fw_params };
488 deb_info("%s:\n", __func__);
489
490 fw_params[0] = af9015_config.firmware_size >> 8;
491 fw_params[1] = af9015_config.firmware_size & 0xff;
492 fw_params[2] = af9015_config.firmware_checksum >> 8;
493 fw_params[3] = af9015_config.firmware_checksum & 0xff;
494
495 /* wait 2nd demodulator ready */
496 msleep(100);
497
498 ret = af9015_read_reg_i2c(d, 0x3a, 0x98be, &val);
499 if (ret)
500 goto error;
501 else
502 deb_info("%s: firmware status:%02x\n", __func__, val);
503
504 if (val == 0x0c) /* fw is running, no need for download */
505 goto exit;
506
507 /* set I2C master clock to fast (to speed up firmware copy) */
508 ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
509 if (ret)
510 goto error;
511
512 msleep(50);
513
514 /* copy firmware */
515 ret = af9015_ctrl_msg(d, &req);
516 if (ret)
517 err("firmware copy cmd failed:%d", ret);
518 deb_info("%s: firmware copy done\n", __func__);
519
520 /* set I2C master clock back to normal */
521 ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
522 if (ret)
523 goto error;
524
525 /* request boot firmware */
526 ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].demod_address,
527 0xe205, 1);
528 deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
529 if (ret)
530 goto error;
531
532 for (i = 0; i < 15; i++) {
533 msleep(100);
534
535 /* check firmware status */
536 ret = af9015_read_reg_i2c(d,
537 af9015_af9013_config[1].demod_address, 0x98be, &val);
538 deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
539 __func__, ret, val);
540 if (ret)
541 goto error;
542
543 if (val == 0x0c || val == 0x04) /* success or fail */
544 break;
545 }
546
547 if (val == 0x04) {
548 err("firmware did not run");
549 ret = -1;
550 } else if (val != 0x0c) {
551 err("firmware boot timeout");
552 ret = -1;
553 }
554
555error:
556exit:
557 return ret;
558}
559
Jiri Slaby6c614042010-01-22 12:10:52 -0300560/* hash (and dump) eeprom */
561static int af9015_eeprom_hash(struct usb_device *udev)
Antti Palosaari80619de2008-09-15 17:18:09 -0300562{
Jiri Slaby6c614042010-01-22 12:10:52 -0300563 static const unsigned int eeprom_size = 256;
564 unsigned int reg;
565 int ret;
566 u8 val, *eeprom;
567 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
Antti Palosaari80619de2008-09-15 17:18:09 -0300568
Jiri Slaby6c614042010-01-22 12:10:52 -0300569 eeprom = kmalloc(eeprom_size, GFP_KERNEL);
570 if (eeprom == NULL)
571 return -ENOMEM;
572
573 for (reg = 0; reg < eeprom_size; reg++) {
574 req.addr = reg;
575 ret = af9015_rw_udev(udev, &req);
576 if (ret)
577 goto free;
578 eeprom[reg] = val;
Antti Palosaari80619de2008-09-15 17:18:09 -0300579 }
Jiri Slaby6c614042010-01-22 12:10:52 -0300580
581 if (dvb_usb_af9015_debug & 0x01)
582 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, eeprom,
583 eeprom_size);
584
585 BUG_ON(eeprom_size % 4);
586
587 af9015_config.eeprom_sum = 0;
588 for (reg = 0; reg < eeprom_size / sizeof(u32); reg++) {
589 af9015_config.eeprom_sum *= GOLDEN_RATIO_PRIME_32;
590 af9015_config.eeprom_sum += le32_to_cpu(((u32 *)eeprom)[reg]);
591 }
592
593 deb_info("%s: eeprom sum=%.8x\n", __func__, af9015_config.eeprom_sum);
594
595 ret = 0;
596free:
597 kfree(eeprom);
598 return ret;
Antti Palosaari80619de2008-09-15 17:18:09 -0300599}
600
Antti Palosaari349d0422008-11-05 16:31:24 -0300601static int af9015_download_ir_table(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -0300602{
603 int i, packets = 0, ret;
604 u16 addr = 0x9a56; /* ir-table start address */
605 struct req_t req = {WRITE_MEMORY, 0, 0, 0, 0, 1, NULL};
606 u8 *data = NULL;
607 deb_info("%s:\n", __func__);
608
609 data = af9015_config.ir_table;
610 packets = af9015_config.ir_table_size;
611
612 /* no remote */
613 if (!packets)
614 goto exit;
615
616 /* load remote ir-table */
617 for (i = 0; i < packets; i++) {
618 req.addr = addr + i;
619 req.data = &data[i];
620 ret = af9015_ctrl_msg(d, &req);
621 if (ret) {
622 err("ir-table download failed at packet %d with " \
623 "code %d", i, ret);
624 return ret;
625 }
626 }
627
628exit:
629 return 0;
630}
631
632static int af9015_init(struct dvb_usb_device *d)
633{
634 int ret;
635 deb_info("%s:\n", __func__);
636
637 ret = af9015_init_endpoint(d);
638 if (ret)
639 goto error;
640
641 ret = af9015_download_ir_table(d);
642 if (ret)
643 goto error;
644
645error:
646 return ret;
647}
648
649static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
650{
651 int ret;
652 deb_info("%s: onoff:%d\n", __func__, onoff);
653
654 if (onoff)
655 ret = af9015_set_reg_bit(adap->dev, 0xd503, 0);
656 else
657 ret = af9015_clear_reg_bit(adap->dev, 0xd503, 0);
658
659 return ret;
660}
661
662static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
663 int onoff)
664{
665 int ret;
666 u8 idx;
667
668 deb_info("%s: set pid filter, index %d, pid %x, onoff %d\n",
669 __func__, index, pid, onoff);
670
671 ret = af9015_write_reg(adap->dev, 0xd505, (pid & 0xff));
672 if (ret)
673 goto error;
674
675 ret = af9015_write_reg(adap->dev, 0xd506, (pid >> 8));
676 if (ret)
677 goto error;
678
679 idx = ((index & 0x1f) | (1 << 5));
680 ret = af9015_write_reg(adap->dev, 0xd504, idx);
681
682error:
683 return ret;
684}
685
686static int af9015_download_firmware(struct usb_device *udev,
687 const struct firmware *fw)
688{
689 int i, len, packets, remainder, ret;
690 struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
691 u16 addr = 0x5100; /* firmware start address */
692 u16 checksum = 0;
693
694 deb_info("%s:\n", __func__);
695
696 /* calc checksum */
697 for (i = 0; i < fw->size; i++)
698 checksum += fw->data[i];
699
700 af9015_config.firmware_size = fw->size;
701 af9015_config.firmware_checksum = checksum;
702
703 #define FW_PACKET_MAX_DATA 55
704
705 packets = fw->size / FW_PACKET_MAX_DATA;
706 remainder = fw->size % FW_PACKET_MAX_DATA;
707 len = FW_PACKET_MAX_DATA;
708 for (i = 0; i <= packets; i++) {
709 if (i == packets) /* set size of the last packet */
710 len = remainder;
711
712 req.data_len = len;
Antti Palosaari541dfa82008-10-06 13:57:45 -0300713 req.data = (u8 *)(fw->data + i * FW_PACKET_MAX_DATA);
Antti Palosaari80619de2008-09-15 17:18:09 -0300714 req.addr = addr;
715 addr += FW_PACKET_MAX_DATA;
716
717 ret = af9015_rw_udev(udev, &req);
718 if (ret) {
719 err("firmware download failed at packet %d with " \
720 "code %d", i, ret);
721 goto error;
722 }
723 }
Antti Palosaari80619de2008-09-15 17:18:09 -0300724
725 /* firmware loaded, request boot */
726 req.cmd = BOOT;
727 ret = af9015_rw_udev(udev, &req);
728 if (ret) {
729 err("firmware boot failed:%d", ret);
730 goto error;
731 }
732
Antti Palosaari80619de2008-09-15 17:18:09 -0300733error:
734 return ret;
735}
736
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300737struct af9015_setup {
738 unsigned int id;
Mauro Carvalho Chehab34abf212010-07-31 11:24:57 -0300739 struct ir_scancode *rc_key_map;
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300740 unsigned int rc_key_map_size;
741 u8 *ir_table;
742 unsigned int ir_table_size;
743};
744
745static const struct af9015_setup *af9015_setup_match(unsigned int id,
746 const struct af9015_setup *table)
747{
748 for (; table->rc_key_map; table++)
749 if (table->id == id)
750 return table;
751 return NULL;
752}
753
754static const struct af9015_setup af9015_setup_modparam[] = {
755 { AF9015_REMOTE_A_LINK_DTU_M,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300756 ir_codes_af9015_table_a_link, ARRAY_SIZE(ir_codes_af9015_table_a_link),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300757 af9015_ir_table_a_link, ARRAY_SIZE(af9015_ir_table_a_link) },
758 { AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300759 ir_codes_af9015_table_msi, ARRAY_SIZE(ir_codes_af9015_table_msi),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300760 af9015_ir_table_msi, ARRAY_SIZE(af9015_ir_table_msi) },
761 { AF9015_REMOTE_MYGICTV_U718,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300762 ir_codes_af9015_table_mygictv, ARRAY_SIZE(ir_codes_af9015_table_mygictv),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300763 af9015_ir_table_mygictv, ARRAY_SIZE(af9015_ir_table_mygictv) },
764 { AF9015_REMOTE_DIGITTRADE_DVB_T,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300765 ir_codes_af9015_table_digittrade, ARRAY_SIZE(ir_codes_af9015_table_digittrade),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300766 af9015_ir_table_digittrade, ARRAY_SIZE(af9015_ir_table_digittrade) },
767 { AF9015_REMOTE_AVERMEDIA_KS,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300768 ir_codes_af9015_table_avermedia, ARRAY_SIZE(ir_codes_af9015_table_avermedia),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300769 af9015_ir_table_avermedia_ks, ARRAY_SIZE(af9015_ir_table_avermedia_ks) },
770 { }
771};
772
773/* don't add new entries here anymore, use hashes instead */
774static const struct af9015_setup af9015_setup_usbids[] = {
775 { USB_VID_LEADTEK,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300776 ir_codes_af9015_table_leadtek, ARRAY_SIZE(ir_codes_af9015_table_leadtek),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300777 af9015_ir_table_leadtek, ARRAY_SIZE(af9015_ir_table_leadtek) },
778 { USB_VID_VISIONPLUS,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300779 ir_codes_af9015_table_twinhan, ARRAY_SIZE(ir_codes_af9015_table_twinhan),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300780 af9015_ir_table_twinhan, ARRAY_SIZE(af9015_ir_table_twinhan) },
781 { USB_VID_KWORLD_2, /* TODO: use correct rc keys */
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300782 ir_codes_af9015_table_twinhan, ARRAY_SIZE(ir_codes_af9015_table_twinhan),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300783 af9015_ir_table_kworld, ARRAY_SIZE(af9015_ir_table_kworld) },
784 { USB_VID_AVERMEDIA,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300785 ir_codes_af9015_table_avermedia, ARRAY_SIZE(ir_codes_af9015_table_avermedia),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300786 af9015_ir_table_avermedia, ARRAY_SIZE(af9015_ir_table_avermedia) },
787 { USB_VID_MSI_2,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300788 ir_codes_af9015_table_msi_digivox_iii, ARRAY_SIZE(ir_codes_af9015_table_msi_digivox_iii),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300789 af9015_ir_table_msi_digivox_iii, ARRAY_SIZE(af9015_ir_table_msi_digivox_iii) },
790 { }
791};
792
Jiri Slabye3a0cc62010-01-22 12:10:55 -0300793static const struct af9015_setup af9015_setup_hashes[] = {
794 { 0xb8feb708,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300795 ir_codes_af9015_table_msi, ARRAY_SIZE(ir_codes_af9015_table_msi),
Jiri Slabye3a0cc62010-01-22 12:10:55 -0300796 af9015_ir_table_msi, ARRAY_SIZE(af9015_ir_table_msi) },
Antti Palosaaridb02d9d2010-02-10 20:20:41 -0300797 { 0xa3703d00,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300798 ir_codes_af9015_table_a_link, ARRAY_SIZE(ir_codes_af9015_table_a_link),
Antti Palosaaridb02d9d2010-02-10 20:20:41 -0300799 af9015_ir_table_a_link, ARRAY_SIZE(af9015_ir_table_a_link) },
Antti Palosaari58c811df2010-02-10 20:44:12 -0300800 { 0x9b7dc64e,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300801 ir_codes_af9015_table_mygictv, ARRAY_SIZE(ir_codes_af9015_table_mygictv),
Antti Palosaari58c811df2010-02-10 20:44:12 -0300802 af9015_ir_table_mygictv, ARRAY_SIZE(af9015_ir_table_mygictv) },
Jiri Slabye3a0cc62010-01-22 12:10:55 -0300803 { }
804};
805
Jiri Slaby634d2d72010-01-22 12:10:53 -0300806static void af9015_set_remote_config(struct usb_device *udev,
807 struct dvb_usb_device_properties *props)
808{
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300809 const struct af9015_setup *table = NULL;
810
Jiri Slaby634d2d72010-01-22 12:10:53 -0300811 if (dvb_usb_af9015_remote) {
812 /* load remote defined as module param */
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300813 table = af9015_setup_match(dvb_usb_af9015_remote,
814 af9015_setup_modparam);
Jiri Slaby634d2d72010-01-22 12:10:53 -0300815 } else {
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300816 u16 vendor = le16_to_cpu(udev->descriptor.idVendor);
817
Jiri Slabye3a0cc62010-01-22 12:10:55 -0300818 table = af9015_setup_match(af9015_config.eeprom_sum,
819 af9015_setup_hashes);
820
821 if (!table && vendor == USB_VID_AFATECH) {
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300822 /* Check USB manufacturer and product strings and try
823 to determine correct remote in case of chip vendor
824 reference IDs are used.
825 DO NOT ADD ANYTHING NEW HERE. Use hashes instead.
826 */
Jiri Slaby634d2d72010-01-22 12:10:53 -0300827 char manufacturer[10];
828 memset(manufacturer, 0, sizeof(manufacturer));
829 usb_string(udev, udev->descriptor.iManufacturer,
830 manufacturer, sizeof(manufacturer));
Antti Palosaari58c811df2010-02-10 20:44:12 -0300831 if (!strcmp("MSI", manufacturer)) {
Jiri Slaby634d2d72010-01-22 12:10:53 -0300832 /* iManufacturer 1 MSI
833 iProduct 2 MSI K-VOX */
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300834 table = af9015_setup_match(
835 AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
836 af9015_setup_modparam);
Jiri Slaby634d2d72010-01-22 12:10:53 -0300837 } else if (udev->descriptor.idProduct ==
838 cpu_to_le16(USB_PID_TREKSTOR_DVBT)) {
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300839 table = &(const struct af9015_setup){ 0,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300840 ir_codes_af9015_table_trekstor,
841 ARRAY_SIZE(ir_codes_af9015_table_trekstor),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300842 af9015_ir_table_trekstor,
843 ARRAY_SIZE(af9015_ir_table_trekstor)
844 };
Jiri Slaby634d2d72010-01-22 12:10:53 -0300845 }
Jiri Slabye3a0cc62010-01-22 12:10:55 -0300846 } else if (!table)
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300847 table = af9015_setup_match(vendor, af9015_setup_usbids);
848 }
849
850 if (table) {
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -0300851 props->rc.legacy.rc_key_map = table->rc_key_map;
852 props->rc.legacy.rc_key_map_size = table->rc_key_map_size;
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300853 af9015_config.ir_table = table->ir_table;
854 af9015_config.ir_table_size = table->ir_table_size;
Jiri Slaby634d2d72010-01-22 12:10:53 -0300855 }
856}
857
Antti Palosaari80619de2008-09-15 17:18:09 -0300858static int af9015_read_config(struct usb_device *udev)
859{
860 int ret;
861 u8 val, i, offset = 0;
862 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
Antti Palosaari80619de2008-09-15 17:18:09 -0300863
864 /* IR remote controller */
865 req.addr = AF9015_EEPROM_IR_MODE;
Antti Palosaarid1a470f2009-01-20 14:56:20 -0300866 /* first message will timeout often due to possible hw bug */
867 for (i = 0; i < 4; i++) {
868 ret = af9015_rw_udev(udev, &req);
869 if (!ret)
870 break;
871 }
Antti Palosaari80619de2008-09-15 17:18:09 -0300872 if (ret)
873 goto error;
Jiri Slaby6c614042010-01-22 12:10:52 -0300874
875 ret = af9015_eeprom_hash(udev);
876 if (ret)
877 goto error;
878
Antti Palosaari80619de2008-09-15 17:18:09 -0300879 deb_info("%s: IR mode:%d\n", __func__, val);
880 for (i = 0; i < af9015_properties_count; i++) {
Antti Palosaari0f017212009-09-21 21:26:37 -0300881 if (val == AF9015_IR_MODE_DISABLED) {
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -0300882 af9015_properties[i].rc.legacy.rc_key_map = NULL;
883 af9015_properties[i].rc.legacy.rc_key_map_size = 0;
Jiri Slaby634d2d72010-01-22 12:10:53 -0300884 } else
885 af9015_set_remote_config(udev, &af9015_properties[i]);
Antti Palosaari80619de2008-09-15 17:18:09 -0300886 }
887
888 /* TS mode - one or two receivers */
889 req.addr = AF9015_EEPROM_TS_MODE;
890 ret = af9015_rw_udev(udev, &req);
891 if (ret)
892 goto error;
893 af9015_config.dual_mode = val;
894 deb_info("%s: TS mode:%d\n", __func__, af9015_config.dual_mode);
Antti Palosaari80619de2008-09-15 17:18:09 -0300895
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300896 /* Set adapter0 buffer size according to USB port speed, adapter1 buffer
897 size can be static because it is enabled only USB2.0 */
Antti Palosaari80619de2008-09-15 17:18:09 -0300898 for (i = 0; i < af9015_properties_count; i++) {
899 /* USB1.1 set smaller buffersize and disable 2nd adapter */
900 if (udev->speed == USB_SPEED_FULL) {
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300901 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
Antti Palosaari9c863272009-09-12 13:35:29 -0300902 = TS_USB11_FRAME_SIZE;
Antti Palosaari80619de2008-09-15 17:18:09 -0300903 /* disable 2nd adapter because we don't have
904 PID-filters */
905 af9015_config.dual_mode = 0;
906 } else {
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300907 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
Jose Alberto Reguero353330c2009-09-12 09:51:36 -0300908 = TS_USB20_FRAME_SIZE;
Antti Palosaari80619de2008-09-15 17:18:09 -0300909 }
910 }
911
912 if (af9015_config.dual_mode) {
913 /* read 2nd demodulator I2C address */
914 req.addr = AF9015_EEPROM_DEMOD2_I2C;
915 ret = af9015_rw_udev(udev, &req);
916 if (ret)
917 goto error;
918 af9015_af9013_config[1].demod_address = val;
919
920 /* enable 2nd adapter */
921 for (i = 0; i < af9015_properties_count; i++)
922 af9015_properties[i].num_adapters = 2;
923
924 } else {
925 /* disable 2nd adapter */
926 for (i = 0; i < af9015_properties_count; i++)
927 af9015_properties[i].num_adapters = 1;
928 }
929
930 for (i = 0; i < af9015_properties[0].num_adapters; i++) {
931 if (i == 1)
932 offset = AF9015_EEPROM_OFFSET;
933 /* xtal */
934 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
935 ret = af9015_rw_udev(udev, &req);
936 if (ret)
937 goto error;
938 switch (val) {
939 case 0:
940 af9015_af9013_config[i].adc_clock = 28800;
941 break;
942 case 1:
943 af9015_af9013_config[i].adc_clock = 20480;
944 break;
945 case 2:
946 af9015_af9013_config[i].adc_clock = 28000;
947 break;
948 case 3:
949 af9015_af9013_config[i].adc_clock = 25000;
950 break;
951 };
952 deb_info("%s: [%d] xtal:%d set adc_clock:%d\n", __func__, i,
953 val, af9015_af9013_config[i].adc_clock);
954
955 /* tuner IF */
956 req.addr = AF9015_EEPROM_IF1H + offset;
957 ret = af9015_rw_udev(udev, &req);
958 if (ret)
959 goto error;
960 af9015_af9013_config[i].tuner_if = val << 8;
961 req.addr = AF9015_EEPROM_IF1L + offset;
962 ret = af9015_rw_udev(udev, &req);
963 if (ret)
964 goto error;
965 af9015_af9013_config[i].tuner_if += val;
966 deb_info("%s: [%d] IF1:%d\n", __func__, i,
967 af9015_af9013_config[0].tuner_if);
968
969 /* MT2060 IF1 */
970 req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
971 ret = af9015_rw_udev(udev, &req);
972 if (ret)
973 goto error;
974 af9015_config.mt2060_if1[i] = val << 8;
975 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
976 ret = af9015_rw_udev(udev, &req);
977 if (ret)
978 goto error;
979 af9015_config.mt2060_if1[i] += val;
980 deb_info("%s: [%d] MT2060 IF1:%d\n", __func__, i,
981 af9015_config.mt2060_if1[i]);
982
983 /* tuner */
984 req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
985 ret = af9015_rw_udev(udev, &req);
986 if (ret)
987 goto error;
988 switch (val) {
989 case AF9013_TUNER_ENV77H11D5:
990 case AF9013_TUNER_MT2060:
Antti Palosaari80619de2008-09-15 17:18:09 -0300991 case AF9013_TUNER_QT1010:
992 case AF9013_TUNER_UNKNOWN:
993 case AF9013_TUNER_MT2060_2:
994 case AF9013_TUNER_TDA18271:
995 case AF9013_TUNER_QT1010A:
Antti Palosaariee3d4402010-08-13 03:51:26 -0300996 case AF9013_TUNER_TDA18218:
Antti Palosaari80619de2008-09-15 17:18:09 -0300997 af9015_af9013_config[i].rf_spec_inv = 1;
998 break;
999 case AF9013_TUNER_MXL5003D:
1000 case AF9013_TUNER_MXL5005D:
1001 case AF9013_TUNER_MXL5005R:
1002 af9015_af9013_config[i].rf_spec_inv = 0;
1003 break;
Jochen Friedrichd5633992009-02-02 14:59:50 -03001004 case AF9013_TUNER_MC44S803:
1005 af9015_af9013_config[i].gpio[1] = AF9013_GPIO_LO;
1006 af9015_af9013_config[i].rf_spec_inv = 1;
1007 break;
Antti Palosaari80619de2008-09-15 17:18:09 -03001008 default:
1009 warn("tuner id:%d not supported, please report!", val);
1010 return -ENODEV;
1011 };
1012
1013 af9015_af9013_config[i].tuner = val;
1014 deb_info("%s: [%d] tuner id:%d\n", __func__, i, val);
1015 }
1016
1017error:
1018 if (ret)
1019 err("eeprom read failed:%d", ret);
1020
Antti Palosaari3956fef2009-03-31 17:01:02 -03001021 /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
1022 content :-( Override some wrong values here. */
1023 if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
1024 le16_to_cpu(udev->descriptor.idProduct) == USB_PID_AVERMEDIA_A850) {
1025 deb_info("%s: AverMedia A850: overriding config\n", __func__);
1026 /* disable dual mode */
1027 af9015_config.dual_mode = 0;
1028 /* disable 2nd adapter */
1029 for (i = 0; i < af9015_properties_count; i++)
1030 af9015_properties[i].num_adapters = 1;
1031
1032 /* set correct IF */
1033 af9015_af9013_config[0].tuner_if = 4570;
1034 }
1035
Antti Palosaari80619de2008-09-15 17:18:09 -03001036 return ret;
1037}
1038
1039static int af9015_identify_state(struct usb_device *udev,
1040 struct dvb_usb_device_properties *props,
1041 struct dvb_usb_device_description **desc,
1042 int *cold)
1043{
1044 int ret;
1045 u8 reply;
1046 struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
1047
1048 ret = af9015_rw_udev(udev, &req);
1049 if (ret)
1050 return ret;
1051
1052 deb_info("%s: reply:%02x\n", __func__, reply);
1053 if (reply == 0x02)
1054 *cold = 0;
1055 else
1056 *cold = 1;
1057
1058 return ret;
1059}
1060
1061static int af9015_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
1062{
1063 u8 buf[8];
1064 struct req_t req = {GET_IR_CODE, 0, 0, 0, 0, sizeof(buf), buf};
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001065 struct ir_scancode *keymap = d->props.rc.legacy.rc_key_map;
Antti Palosaari80619de2008-09-15 17:18:09 -03001066 int i, ret;
1067
1068 memset(buf, 0, sizeof(buf));
1069
1070 ret = af9015_ctrl_msg(d, &req);
1071 if (ret)
1072 return ret;
1073
1074 *event = 0;
1075 *state = REMOTE_NO_KEY_PRESSED;
1076
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001077 for (i = 0; i < d->props.rc.legacy.rc_key_map_size; i++) {
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -03001078 if (!buf[1] && rc5_custom(&keymap[i]) == buf[0] &&
1079 rc5_data(&keymap[i]) == buf[2]) {
Mauro Carvalho Chehab34abf212010-07-31 11:24:57 -03001080 *event = keymap[i].keycode;
Antti Palosaari80619de2008-09-15 17:18:09 -03001081 *state = REMOTE_KEY_PRESSED;
1082 break;
1083 }
1084 }
1085 if (!buf[1])
1086 deb_rc("%s: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1087 __func__, buf[0], buf[1], buf[2], buf[3], buf[4],
1088 buf[5], buf[6], buf[7]);
1089
1090 return 0;
1091}
1092
1093/* init 2nd I2C adapter */
Antti Palosaari349d0422008-11-05 16:31:24 -03001094static int af9015_i2c_init(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -03001095{
1096 int ret;
1097 struct af9015_state *state = d->priv;
1098 deb_info("%s:\n", __func__);
1099
1100 strncpy(state->i2c_adap.name, d->desc->name,
1101 sizeof(state->i2c_adap.name));
1102#ifdef I2C_ADAP_CLASS_TV_DIGITAL
1103 state->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL,
1104#else
1105 state->i2c_adap.class = I2C_CLASS_TV_DIGITAL,
1106#endif
1107 state->i2c_adap.algo = d->props.i2c_algo;
1108 state->i2c_adap.algo_data = NULL;
1109 state->i2c_adap.dev.parent = &d->udev->dev;
1110
1111 i2c_set_adapdata(&state->i2c_adap, d);
1112
1113 ret = i2c_add_adapter(&state->i2c_adap);
1114 if (ret < 0)
1115 err("could not add i2c adapter");
1116
1117 return ret;
1118}
1119
1120static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
1121{
1122 int ret;
1123 struct af9015_state *state = adap->dev->priv;
1124 struct i2c_adapter *i2c_adap;
1125
1126 if (adap->id == 0) {
1127 /* select I2C adapter */
1128 i2c_adap = &adap->dev->i2c_adap;
1129
1130 deb_info("%s: init I2C\n", __func__);
1131 ret = af9015_i2c_init(adap->dev);
Antti Palosaari80619de2008-09-15 17:18:09 -03001132 } else {
1133 /* select I2C adapter */
1134 i2c_adap = &state->i2c_adap;
1135
1136 /* copy firmware to 2nd demodulator */
1137 if (af9015_config.dual_mode) {
1138 ret = af9015_copy_firmware(adap->dev);
1139 if (ret) {
1140 err("firmware copy to 2nd frontend " \
1141 "failed, will disable it");
1142 af9015_config.dual_mode = 0;
1143 return -ENODEV;
1144 }
1145 } else {
1146 return -ENODEV;
1147 }
1148 }
1149
1150 /* attach demodulator */
1151 adap->fe = dvb_attach(af9013_attach, &af9015_af9013_config[adap->id],
1152 i2c_adap);
1153
1154 return adap->fe == NULL ? -ENODEV : 0;
1155}
1156
1157static struct mt2060_config af9015_mt2060_config = {
1158 .i2c_address = 0xc0,
1159 .clock_out = 0,
1160};
1161
1162static struct qt1010_config af9015_qt1010_config = {
1163 .i2c_address = 0xc4,
1164};
1165
1166static struct tda18271_config af9015_tda18271_config = {
1167 .gate = TDA18271_GATE_DIGITAL,
1168 .small_i2c = 1,
1169};
1170
1171static struct mxl5005s_config af9015_mxl5003_config = {
1172 .i2c_address = 0xc6,
1173 .if_freq = IF_FREQ_4570000HZ,
1174 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1175 .agc_mode = MXL_SINGLE_AGC,
1176 .tracking_filter = MXL_TF_DEFAULT,
Antti Palosaaria1310772008-09-22 13:59:25 -03001177 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -03001178 .cap_select = MXL_CAP_SEL_ENABLE,
1179 .div_out = MXL_DIV_OUT_4,
1180 .clock_out = MXL_CLOCK_OUT_DISABLE,
1181 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1182 .top = MXL5005S_TOP_25P2,
1183 .mod_mode = MXL_DIGITAL_MODE,
1184 .if_mode = MXL_ZERO_IF,
1185 .AgcMasterByte = 0x00,
1186};
1187
1188static struct mxl5005s_config af9015_mxl5005_config = {
1189 .i2c_address = 0xc6,
1190 .if_freq = IF_FREQ_4570000HZ,
1191 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1192 .agc_mode = MXL_SINGLE_AGC,
1193 .tracking_filter = MXL_TF_OFF,
Antti Palosaaria1310772008-09-22 13:59:25 -03001194 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -03001195 .cap_select = MXL_CAP_SEL_ENABLE,
1196 .div_out = MXL_DIV_OUT_4,
1197 .clock_out = MXL_CLOCK_OUT_DISABLE,
1198 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1199 .top = MXL5005S_TOP_25P2,
1200 .mod_mode = MXL_DIGITAL_MODE,
1201 .if_mode = MXL_ZERO_IF,
1202 .AgcMasterByte = 0x00,
1203};
1204
Jochen Friedrichd5633992009-02-02 14:59:50 -03001205static struct mc44s803_config af9015_mc44s803_config = {
1206 .i2c_address = 0xc0,
1207 .dig_out = 1,
1208};
1209
Antti Palosaariee3d4402010-08-13 03:51:26 -03001210static struct tda18218_config af9015_tda18218_config = {
1211 .i2c_address = 0xc0,
1212 .i2c_wr_max = 21, /* max wr bytes AF9015 I2C adap can handle at once */
1213};
1214
Antti Palosaari80619de2008-09-15 17:18:09 -03001215static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
1216{
1217 struct af9015_state *state = adap->dev->priv;
1218 struct i2c_adapter *i2c_adap;
1219 int ret;
1220 deb_info("%s: \n", __func__);
1221
1222 /* select I2C adapter */
1223 if (adap->id == 0)
1224 i2c_adap = &adap->dev->i2c_adap;
1225 else
1226 i2c_adap = &state->i2c_adap;
1227
1228 switch (af9015_af9013_config[adap->id].tuner) {
1229 case AF9013_TUNER_MT2060:
1230 case AF9013_TUNER_MT2060_2:
1231 ret = dvb_attach(mt2060_attach, adap->fe, i2c_adap,
1232 &af9015_mt2060_config,
1233 af9015_config.mt2060_if1[adap->id])
1234 == NULL ? -ENODEV : 0;
1235 break;
1236 case AF9013_TUNER_QT1010:
1237 case AF9013_TUNER_QT1010A:
1238 ret = dvb_attach(qt1010_attach, adap->fe, i2c_adap,
1239 &af9015_qt1010_config) == NULL ? -ENODEV : 0;
1240 break;
1241 case AF9013_TUNER_TDA18271:
1242 ret = dvb_attach(tda18271_attach, adap->fe, 0xc0, i2c_adap,
1243 &af9015_tda18271_config) == NULL ? -ENODEV : 0;
1244 break;
Antti Palosaariee3d4402010-08-13 03:51:26 -03001245 case AF9013_TUNER_TDA18218:
1246 ret = dvb_attach(tda18218_attach, adap->fe, i2c_adap,
1247 &af9015_tda18218_config) == NULL ? -ENODEV : 0;
1248 break;
Antti Palosaari80619de2008-09-15 17:18:09 -03001249 case AF9013_TUNER_MXL5003D:
1250 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1251 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
1252 break;
1253 case AF9013_TUNER_MXL5005D:
1254 case AF9013_TUNER_MXL5005R:
1255 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1256 &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
1257 break;
1258 case AF9013_TUNER_ENV77H11D5:
1259 ret = dvb_attach(dvb_pll_attach, adap->fe, 0xc0, i2c_adap,
1260 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
1261 break;
1262 case AF9013_TUNER_MC44S803:
Jochen Friedrichd5633992009-02-02 14:59:50 -03001263 ret = dvb_attach(mc44s803_attach, adap->fe, i2c_adap,
1264 &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
Antti Palosaari80619de2008-09-15 17:18:09 -03001265 break;
1266 case AF9013_TUNER_UNKNOWN:
1267 default:
1268 ret = -ENODEV;
1269 err("Unknown tuner id:%d",
1270 af9015_af9013_config[adap->id].tuner);
1271 }
1272 return ret;
1273}
1274
1275static struct usb_device_id af9015_usb_table[] = {
1276/* 0 */{USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015)},
1277 {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016)},
1278 {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD)},
1279 {USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E)},
1280 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U)},
1281/* 5 */{USB_DEVICE(USB_VID_VISIONPLUS,
1282 USB_PID_TINYTWIN)},
1283 {USB_DEVICE(USB_VID_VISIONPLUS,
1284 USB_PID_AZUREWAVE_AD_TU700)},
1285 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2)},
1286 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T)},
1287 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X)},
1288/* 10 */{USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380)},
1289 {USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO)},
1290 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2)},
Antti Palosaaria3765882008-09-19 18:34:06 -03001291 {USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2)},
Antti Palosaari05c1cab2008-09-22 12:32:37 -03001292 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309)},
Herbert Graeber641015a2008-10-07 10:06:36 -03001293/* 15 */{USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III)},
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001294 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U)},
Antti Palosaari71bf2e02009-01-13 12:47:28 -03001295 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2)},
Antti Palosaari58fe1592009-03-26 20:41:05 -03001296 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3)},
Marc Schneider26144842009-03-26 21:07:18 -03001297 {USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT)},
Antti Palosaari1ed5fad2009-04-09 15:14:18 -03001298/* 20 */{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850)},
1299 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805)},
Marcel Jueling734dd232009-04-09 17:16:41 -03001300 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU)},
Wen-chien Jesse Sung6e9c1a22009-04-28 01:11:22 -03001301 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810)},
Antti Palosaari22d46452009-05-31 17:07:01 -03001302 {USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03)},
Mart Raudseppc92f0562009-07-24 13:45:41 -03001303/* 25 */{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2)},
Antti Palosaari486ba122009-09-18 13:37:57 -03001304 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T)},
Ignacio de Miguel Diaz52322632009-11-13 23:13:34 -03001305 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20)},
Antti Palosaarifa1df552010-02-10 20:05:48 -03001306 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2)},
Antti Palosaari809c1e82010-02-10 20:07:30 -03001307 {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS)},
Antti Palosaariab9b4f22010-03-01 13:50:40 -03001308/* 30 */{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T)},
Antti Palosaari7fc87092010-03-01 14:06:52 -03001309 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4)},
Antti Palosaari2606cfa2010-05-23 18:26:37 -03001310 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M)},
Antti Palosaari80619de2008-09-15 17:18:09 -03001311 {0},
1312};
1313MODULE_DEVICE_TABLE(usb, af9015_usb_table);
1314
1315static struct dvb_usb_device_properties af9015_properties[] = {
1316 {
1317 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1318
1319 .usb_ctrl = DEVICE_SPECIFIC,
1320 .download_firmware = af9015_download_firmware,
1321 .firmware = "dvb-usb-af9015.fw",
Jose Alberto Reguerocce25712008-11-13 14:14:18 -03001322 .no_reconnect = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001323
Antti Palosaari02542942009-09-16 20:33:03 -03001324 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari80619de2008-09-15 17:18:09 -03001325
1326 .num_adapters = 2,
1327 .adapter = {
1328 {
1329 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1330 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1331
1332 .pid_filter_count = 32,
1333 .pid_filter = af9015_pid_filter,
1334 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1335
1336 .frontend_attach =
1337 af9015_af9013_frontend_attach,
1338 .tuner_attach = af9015_tuner_attach,
1339 .stream = {
1340 .type = USB_BULK,
1341 .count = 6,
1342 .endpoint = 0x84,
1343 },
1344 },
1345 {
1346 .frontend_attach =
1347 af9015_af9013_frontend_attach,
1348 .tuner_attach = af9015_tuner_attach,
1349 .stream = {
1350 .type = USB_BULK,
1351 .count = 6,
1352 .endpoint = 0x85,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001353 .u = {
1354 .bulk = {
1355 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001356 TS_USB20_FRAME_SIZE,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001357 }
1358 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001359 },
1360 }
1361 },
1362
1363 .identify_state = af9015_identify_state,
1364
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001365 .rc.legacy = {
1366 .rc_query = af9015_rc_query,
1367 .rc_interval = 150,
1368 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001369
1370 .i2c_algo = &af9015_i2c_algo,
1371
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001372 .num_device_descs = 9, /* max 9 */
Antti Palosaari80619de2008-09-15 17:18:09 -03001373 .devices = {
1374 {
1375 .name = "Afatech AF9015 DVB-T USB2.0 stick",
1376 .cold_ids = {&af9015_usb_table[0],
1377 &af9015_usb_table[1], NULL},
1378 .warm_ids = {NULL},
1379 },
1380 {
1381 .name = "Leadtek WinFast DTV Dongle Gold",
1382 .cold_ids = {&af9015_usb_table[2], NULL},
1383 .warm_ids = {NULL},
1384 },
1385 {
1386 .name = "Pinnacle PCTV 71e",
1387 .cold_ids = {&af9015_usb_table[3], NULL},
1388 .warm_ids = {NULL},
1389 },
1390 {
1391 .name = "KWorld PlusTV Dual DVB-T Stick " \
1392 "(DVB-T 399U)",
Mart Raudseppc92f0562009-07-24 13:45:41 -03001393 .cold_ids = {&af9015_usb_table[4],
1394 &af9015_usb_table[25], NULL},
Antti Palosaari80619de2008-09-15 17:18:09 -03001395 .warm_ids = {NULL},
1396 },
1397 {
1398 .name = "DigitalNow TinyTwin DVB-T Receiver",
Antti Palosaarifa1df552010-02-10 20:05:48 -03001399 .cold_ids = {&af9015_usb_table[5],
1400 &af9015_usb_table[28], NULL},
Antti Palosaari80619de2008-09-15 17:18:09 -03001401 .warm_ids = {NULL},
1402 },
1403 {
1404 .name = "TwinHan AzureWave AD-TU700(704J)",
1405 .cold_ids = {&af9015_usb_table[6], NULL},
1406 .warm_ids = {NULL},
1407 },
1408 {
1409 .name = "TerraTec Cinergy T USB XE",
1410 .cold_ids = {&af9015_usb_table[7], NULL},
1411 .warm_ids = {NULL},
1412 },
1413 {
1414 .name = "KWorld PlusTV Dual DVB-T PCI " \
1415 "(DVB-T PC160-2T)",
1416 .cold_ids = {&af9015_usb_table[8], NULL},
1417 .warm_ids = {NULL},
1418 },
1419 {
1420 .name = "AVerMedia AVerTV DVB-T Volar X",
1421 .cold_ids = {&af9015_usb_table[9], NULL},
1422 .warm_ids = {NULL},
1423 },
1424 }
1425 }, {
1426 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1427
1428 .usb_ctrl = DEVICE_SPECIFIC,
1429 .download_firmware = af9015_download_firmware,
1430 .firmware = "dvb-usb-af9015.fw",
Jose Alberto Reguerocce25712008-11-13 14:14:18 -03001431 .no_reconnect = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001432
Antti Palosaari02542942009-09-16 20:33:03 -03001433 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari80619de2008-09-15 17:18:09 -03001434
1435 .num_adapters = 2,
1436 .adapter = {
1437 {
1438 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1439 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1440
1441 .pid_filter_count = 32,
1442 .pid_filter = af9015_pid_filter,
1443 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1444
1445 .frontend_attach =
1446 af9015_af9013_frontend_attach,
1447 .tuner_attach = af9015_tuner_attach,
1448 .stream = {
1449 .type = USB_BULK,
1450 .count = 6,
1451 .endpoint = 0x84,
1452 },
1453 },
1454 {
1455 .frontend_attach =
1456 af9015_af9013_frontend_attach,
1457 .tuner_attach = af9015_tuner_attach,
1458 .stream = {
1459 .type = USB_BULK,
1460 .count = 6,
1461 .endpoint = 0x85,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001462 .u = {
1463 .bulk = {
1464 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001465 TS_USB20_FRAME_SIZE,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001466 }
1467 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001468 },
1469 }
1470 },
1471
1472 .identify_state = af9015_identify_state,
1473
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001474 .rc.legacy = {
1475 .rc_query = af9015_rc_query,
1476 .rc_interval = 150,
1477 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001478
1479 .i2c_algo = &af9015_i2c_algo,
1480
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001481 .num_device_descs = 9, /* max 9 */
Antti Palosaari80619de2008-09-15 17:18:09 -03001482 .devices = {
1483 {
1484 .name = "Xtensions XD-380",
1485 .cold_ids = {&af9015_usb_table[10], NULL},
1486 .warm_ids = {NULL},
1487 },
1488 {
1489 .name = "MSI DIGIVOX Duo",
1490 .cold_ids = {&af9015_usb_table[11], NULL},
1491 .warm_ids = {NULL},
1492 },
1493 {
1494 .name = "Fujitsu-Siemens Slim Mobile USB DVB-T",
1495 .cold_ids = {&af9015_usb_table[12], NULL},
1496 .warm_ids = {NULL},
1497 },
Mikko Ohtamaa111f9ec2008-09-19 18:26:05 -03001498 {
Antti Palosaaria3765882008-09-19 18:34:06 -03001499 .name = "Telestar Starstick 2",
Mikko Ohtamaa111f9ec2008-09-19 18:26:05 -03001500 .cold_ids = {&af9015_usb_table[13], NULL},
1501 .warm_ids = {NULL},
1502 },
Antti Palosaari05c1cab2008-09-22 12:32:37 -03001503 {
1504 .name = "AVerMedia A309",
1505 .cold_ids = {&af9015_usb_table[14], NULL},
1506 .warm_ids = {NULL},
1507 },
Herbert Graeber641015a2008-10-07 10:06:36 -03001508 {
1509 .name = "MSI Digi VOX mini III",
1510 .cold_ids = {&af9015_usb_table[15], NULL},
1511 .warm_ids = {NULL},
1512 },
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001513 {
1514 .name = "KWorld USB DVB-T TV Stick II " \
1515 "(VS-DVB-T 395U)",
Antti Palosaari71bf2e02009-01-13 12:47:28 -03001516 .cold_ids = {&af9015_usb_table[16],
Antti Palosaari58fe1592009-03-26 20:41:05 -03001517 &af9015_usb_table[17],
Antti Palosaari7fc87092010-03-01 14:06:52 -03001518 &af9015_usb_table[18],
1519 &af9015_usb_table[31], NULL},
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001520 .warm_ids = {NULL},
1521 },
Marc Schneider26144842009-03-26 21:07:18 -03001522 {
1523 .name = "TrekStor DVB-T USB Stick",
1524 .cold_ids = {&af9015_usb_table[19], NULL},
1525 .warm_ids = {NULL},
1526 },
Antti Palosaari3956fef2009-03-31 17:01:02 -03001527 {
1528 .name = "AverMedia AVerTV Volar Black HD " \
1529 "(A850)",
1530 .cold_ids = {&af9015_usb_table[20], NULL},
1531 .warm_ids = {NULL},
1532 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001533 }
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001534 }, {
1535 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1536
1537 .usb_ctrl = DEVICE_SPECIFIC,
1538 .download_firmware = af9015_download_firmware,
1539 .firmware = "dvb-usb-af9015.fw",
1540 .no_reconnect = 1,
1541
Antti Palosaari02542942009-09-16 20:33:03 -03001542 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001543
1544 .num_adapters = 2,
1545 .adapter = {
1546 {
1547 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1548 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1549
1550 .pid_filter_count = 32,
1551 .pid_filter = af9015_pid_filter,
1552 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1553
1554 .frontend_attach =
1555 af9015_af9013_frontend_attach,
1556 .tuner_attach = af9015_tuner_attach,
1557 .stream = {
1558 .type = USB_BULK,
1559 .count = 6,
1560 .endpoint = 0x84,
1561 },
1562 },
1563 {
1564 .frontend_attach =
1565 af9015_af9013_frontend_attach,
1566 .tuner_attach = af9015_tuner_attach,
1567 .stream = {
1568 .type = USB_BULK,
1569 .count = 6,
1570 .endpoint = 0x85,
1571 .u = {
1572 .bulk = {
1573 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001574 TS_USB20_FRAME_SIZE,
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001575 }
1576 }
1577 },
1578 }
1579 },
1580
1581 .identify_state = af9015_identify_state,
1582
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001583 .rc.legacy = {
1584 .rc_query = af9015_rc_query,
1585 .rc_interval = 150,
1586 },
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001587
1588 .i2c_algo = &af9015_i2c_algo,
1589
Antti Palosaari2606cfa2010-05-23 18:26:37 -03001590 .num_device_descs = 9, /* max 9 */
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001591 .devices = {
Antti Palosaari1ed5fad2009-04-09 15:14:18 -03001592 {
1593 .name = "AverMedia AVerTV Volar GPS 805 (A805)",
1594 .cold_ids = {&af9015_usb_table[21], NULL},
1595 .warm_ids = {NULL},
1596 },
Marcel Jueling734dd232009-04-09 17:16:41 -03001597 {
1598 .name = "Conceptronic USB2.0 DVB-T CTVDIGRCU " \
1599 "V3.0",
1600 .cold_ids = {&af9015_usb_table[22], NULL},
1601 .warm_ids = {NULL},
1602 },
Wen-chien Jesse Sung6e9c1a22009-04-28 01:11:22 -03001603 {
1604 .name = "KWorld Digial MC-810",
1605 .cold_ids = {&af9015_usb_table[23], NULL},
1606 .warm_ids = {NULL},
1607 },
Antti Palosaari22d46452009-05-31 17:07:01 -03001608 {
1609 .name = "Genius TVGo DVB-T03",
1610 .cold_ids = {&af9015_usb_table[24], NULL},
1611 .warm_ids = {NULL},
1612 },
Antti Palosaari486ba122009-09-18 13:37:57 -03001613 {
1614 .name = "KWorld PlusTV DVB-T PCI Pro Card " \
1615 "(DVB-T PC160-T)",
1616 .cold_ids = {&af9015_usb_table[26], NULL},
1617 .warm_ids = {NULL},
1618 },
Ignacio de Miguel Diaz52322632009-11-13 23:13:34 -03001619 {
1620 .name = "Sveon STV20 Tuner USB DVB-T HDTV",
1621 .cold_ids = {&af9015_usb_table[27], NULL},
1622 .warm_ids = {NULL},
1623 },
Antti Palosaari809c1e82010-02-10 20:07:30 -03001624 {
1625 .name = "Leadtek WinFast DTV2000DS",
1626 .cold_ids = {&af9015_usb_table[29], NULL},
1627 .warm_ids = {NULL},
1628 },
Antti Palosaariab9b4f22010-03-01 13:50:40 -03001629 {
1630 .name = "KWorld USB DVB-T Stick Mobile " \
1631 "(UB383-T)",
1632 .cold_ids = {&af9015_usb_table[30], NULL},
1633 .warm_ids = {NULL},
1634 },
Antti Palosaari2606cfa2010-05-23 18:26:37 -03001635 {
1636 .name = "AverMedia AVerTV Volar M (A815Mac)",
1637 .cold_ids = {&af9015_usb_table[32], NULL},
1638 .warm_ids = {NULL},
1639 },
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001640 }
1641 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001642};
Antti Palosaari80619de2008-09-15 17:18:09 -03001643
1644static int af9015_usb_probe(struct usb_interface *intf,
1645 const struct usb_device_id *id)
1646{
1647 int ret = 0;
1648 struct dvb_usb_device *d = NULL;
1649 struct usb_device *udev = interface_to_usbdev(intf);
1650 u8 i;
1651
1652 deb_info("%s: interface:%d\n", __func__,
1653 intf->cur_altsetting->desc.bInterfaceNumber);
1654
1655 /* interface 0 is used by DVB-T receiver and
1656 interface 1 is for remote controller (HID) */
1657 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
1658 ret = af9015_read_config(udev);
1659 if (ret)
1660 return ret;
1661
1662 for (i = 0; i < af9015_properties_count; i++) {
1663 ret = dvb_usb_device_init(intf, &af9015_properties[i],
1664 THIS_MODULE, &d, adapter_nr);
1665 if (!ret)
1666 break;
1667 if (ret != -ENODEV)
1668 return ret;
1669 }
1670 if (ret)
1671 return ret;
1672
1673 if (d)
1674 ret = af9015_init(d);
1675 }
1676
1677 return ret;
1678}
1679
Antti Palosaari349d0422008-11-05 16:31:24 -03001680static void af9015_i2c_exit(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -03001681{
1682 struct af9015_state *state = d->priv;
1683 deb_info("%s: \n", __func__);
1684
1685 /* remove 2nd I2C adapter */
1686 if (d->state & DVB_USB_STATE_I2C)
1687 i2c_del_adapter(&state->i2c_adap);
1688}
1689
1690static void af9015_usb_device_exit(struct usb_interface *intf)
1691{
1692 struct dvb_usb_device *d = usb_get_intfdata(intf);
1693 deb_info("%s: \n", __func__);
1694
1695 /* remove 2nd I2C adapter */
1696 if (d != NULL && d->desc != NULL)
1697 af9015_i2c_exit(d);
1698
1699 dvb_usb_device_exit(intf);
1700}
1701
1702/* usb specific object needed to register this driver with the usb subsystem */
1703static struct usb_driver af9015_usb_driver = {
1704 .name = "dvb_usb_af9015",
1705 .probe = af9015_usb_probe,
1706 .disconnect = af9015_usb_device_exit,
1707 .id_table = af9015_usb_table,
1708};
1709
1710/* module stuff */
1711static int __init af9015_usb_module_init(void)
1712{
1713 int ret;
1714 ret = usb_register(&af9015_usb_driver);
1715 if (ret)
1716 err("module init failed:%d", ret);
1717
1718 return ret;
1719}
1720
1721static void __exit af9015_usb_module_exit(void)
1722{
1723 /* deregister this driver from the USB subsystem */
1724 usb_deregister(&af9015_usb_driver);
1725}
1726
1727module_init(af9015_usb_module_init);
1728module_exit(af9015_usb_module_exit);
1729
1730MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1731MODULE_DESCRIPTION("Driver for Afatech AF9015 DVB-T");
1732MODULE_LICENSE("GPL");