blob: d6662f53a0bfdeaf6da09d527c19a7bd30432ce8 [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
24#include "af9015.h"
25#include "af9013.h"
26#include "mt2060.h"
27#include "qt1010.h"
28#include "tda18271.h"
29#include "mxl5005s.h"
Jochen Friedrichd5633992009-02-02 14:59:50 -030030#include "mc44s803.h"
Antti Palosaari80619de2008-09-15 17:18:09 -030031
Antti Palosaari349d0422008-11-05 16:31:24 -030032static int dvb_usb_af9015_debug;
Antti Palosaari80619de2008-09-15 17:18:09 -030033module_param_named(debug, dvb_usb_af9015_debug, int, 0644);
34MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
Antti Palosaari349d0422008-11-05 16:31:24 -030035static int dvb_usb_af9015_remote;
Antti Palosaari80619de2008-09-15 17:18:09 -030036module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
37MODULE_PARM_DESC(remote, "select remote");
Antti Palosaari80619de2008-09-15 17:18:09 -030038DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
39
40static DEFINE_MUTEX(af9015_usb_mutex);
41
42static struct af9015_config af9015_config;
Antti Palosaari85d7d7c2009-04-09 09:16:12 -030043static struct dvb_usb_device_properties af9015_properties[3];
Antti Palosaari349d0422008-11-05 16:31:24 -030044static int af9015_properties_count = ARRAY_SIZE(af9015_properties);
Antti Palosaari80619de2008-09-15 17:18:09 -030045
46static struct af9013_config af9015_af9013_config[] = {
47 {
48 .demod_address = AF9015_I2C_DEMOD,
49 .output_mode = AF9013_OUTPUT_MODE_USB,
50 .api_version = { 0, 1, 9, 0 },
51 .gpio[0] = AF9013_GPIO_HI,
Antti Palosaari80619de2008-09-15 17:18:09 -030052 .gpio[3] = AF9013_GPIO_TUNER_ON,
53
54 }, {
55 .output_mode = AF9013_OUTPUT_MODE_SERIAL,
56 .api_version = { 0, 1, 9, 0 },
57 .gpio[0] = AF9013_GPIO_TUNER_ON,
58 .gpio[1] = AF9013_GPIO_LO,
59 }
60};
61
62static int af9015_rw_udev(struct usb_device *udev, struct req_t *req)
63{
64 int act_len, ret;
65 u8 buf[64];
66 u8 write = 1;
67 u8 msg_len = 8;
68 static u8 seq; /* packet sequence number */
69
70 if (mutex_lock_interruptible(&af9015_usb_mutex) < 0)
71 return -EAGAIN;
72
73 buf[0] = req->cmd;
74 buf[1] = seq++;
75 buf[2] = req->i2c_addr;
76 buf[3] = req->addr >> 8;
77 buf[4] = req->addr & 0xff;
78 buf[5] = req->mbox;
79 buf[6] = req->addr_len;
80 buf[7] = req->data_len;
81
82 switch (req->cmd) {
83 case GET_CONFIG:
84 case BOOT:
85 case READ_MEMORY:
86 case RECONNECT_USB:
87 case GET_IR_CODE:
88 write = 0;
89 break;
90 case READ_I2C:
91 write = 0;
92 buf[2] |= 0x01; /* set I2C direction */
93 case WRITE_I2C:
94 buf[0] = READ_WRITE_I2C;
95 break;
96 case WRITE_MEMORY:
97 if (((req->addr & 0xff00) == 0xff00) ||
98 ((req->addr & 0xae00) == 0xae00))
99 buf[0] = WRITE_VIRTUAL_MEMORY;
100 case WRITE_VIRTUAL_MEMORY:
101 case COPY_FIRMWARE:
102 case DOWNLOAD_FIRMWARE:
103 break;
104 default:
105 err("unknown command:%d", req->cmd);
106 ret = -1;
107 goto error_unlock;
108 }
109
110 /* write requested */
111 if (write) {
112 memcpy(&buf[8], req->data, req->data_len);
113 msg_len += req->data_len;
114 }
115 deb_xfer(">>> ");
116 debug_dump(buf, msg_len, deb_xfer);
117
118 /* send req */
119 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
120 &act_len, AF9015_USB_TIMEOUT);
121 if (ret)
122 err("bulk message failed:%d (%d/%d)", ret, msg_len, act_len);
123 else
124 if (act_len != msg_len)
125 ret = -1; /* all data is not send */
126 if (ret)
127 goto error_unlock;
128
129 /* no ack for those packets */
130 if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
131 goto exit_unlock;
132
133 /* receive ack and data if read req */
134 msg_len = 1 + 1 + req->data_len; /* seq + status + data len */
135 ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
136 &act_len, AF9015_USB_TIMEOUT);
137 if (ret) {
138 err("recv bulk message failed:%d", ret);
139 ret = -1;
140 goto error_unlock;
141 }
142
143 deb_xfer("<<< ");
144 debug_dump(buf, act_len, deb_xfer);
145
146 /* remote controller query status is 1 if remote code is not received */
147 if (req->cmd == GET_IR_CODE && buf[1] == 1) {
148 buf[1] = 0; /* clear command "error" status */
149 memset(&buf[2], 0, req->data_len);
150 buf[3] = 1; /* no remote code received mark */
151 }
152
153 /* check status */
154 if (buf[1]) {
155 err("command failed:%d", buf[1]);
156 ret = -1;
157 goto error_unlock;
158 }
159
160 /* read request, copy returned data to return buf */
161 if (!write)
162 memcpy(req->data, &buf[2], req->data_len);
163
164error_unlock:
165exit_unlock:
166 mutex_unlock(&af9015_usb_mutex);
167
168 return ret;
169}
170
171static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
172{
173 return af9015_rw_udev(d->udev, req);
174}
175
176static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
177 u8 len)
178{
179 struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
180 val};
181 return af9015_ctrl_msg(d, &req);
182}
183
184static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
185{
186 return af9015_write_regs(d, addr, &val, 1);
187}
188
189static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
190{
191 struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, 1, val};
192 return af9015_ctrl_msg(d, &req);
193}
194
195static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
196 u8 val)
197{
198 struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
199
200 if (addr == af9015_af9013_config[0].demod_address ||
201 addr == af9015_af9013_config[1].demod_address)
202 req.addr_len = 3;
203
204 return af9015_ctrl_msg(d, &req);
205}
206
207static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
208 u8 *val)
209{
210 struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
211
212 if (addr == af9015_af9013_config[0].demod_address ||
213 addr == af9015_af9013_config[1].demod_address)
214 req.addr_len = 3;
215
216 return af9015_ctrl_msg(d, &req);
217}
218
219static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
220 int num)
221{
222 struct dvb_usb_device *d = i2c_get_adapdata(adap);
223 int ret = 0, i = 0;
224 u16 addr;
225 u8 mbox, addr_len;
226 struct req_t req;
227
228/* TODO: implement bus lock
229
230The bus lock is needed because there is two tuners both using same I2C-address.
231Due to that the only way to select correct tuner is use demodulator I2C-gate.
232
233................................................
234. AF9015 includes integrated AF9013 demodulator.
235. ____________ ____________ . ____________
236.| uC | | demod | . | tuner |
237.|------------| |------------| . |------------|
238.| AF9015 | | AF9013/5 | . | MXL5003 |
239.| |--+----I2C-------|-----/ -----|-.-----I2C-------| |
240.| | | | addr 0x38 | . | addr 0xc6 |
241.|____________| | |____________| . |____________|
242.................|..............................
243 | ____________ ____________
244 | | demod | | tuner |
245 | |------------| |------------|
246 | | AF9013 | | MXL5003 |
247 +----I2C-------|-----/ -----|-------I2C-------| |
248 | addr 0x3a | | addr 0xc6 |
249 |____________| |____________|
250*/
251 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
252 return -EAGAIN;
253
254 while (i < num) {
255 if (msg[i].addr == af9015_af9013_config[0].demod_address ||
256 msg[i].addr == af9015_af9013_config[1].demod_address) {
257 addr = msg[i].buf[0] << 8;
258 addr += msg[i].buf[1];
259 mbox = msg[i].buf[2];
260 addr_len = 3;
261 } else {
262 addr = msg[i].buf[0];
263 addr_len = 1;
264 mbox = 0;
265 }
266
267 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
268 if (msg[i].addr ==
269 af9015_af9013_config[0].demod_address)
270 req.cmd = READ_MEMORY;
271 else
272 req.cmd = READ_I2C;
273 req.i2c_addr = msg[i].addr;
274 req.addr = addr;
275 req.mbox = mbox;
276 req.addr_len = addr_len;
277 req.data_len = msg[i+1].len;
278 req.data = &msg[i+1].buf[0];
279 ret = af9015_ctrl_msg(d, &req);
280 i += 2;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300281 } else if (msg[i].flags & I2C_M_RD) {
282 ret = -EINVAL;
283 if (msg[i].addr ==
284 af9015_af9013_config[0].demod_address)
285 goto error;
286 else
287 req.cmd = READ_I2C;
288 req.i2c_addr = msg[i].addr;
289 req.addr = addr;
290 req.mbox = mbox;
291 req.addr_len = addr_len;
292 req.data_len = msg[i].len;
293 req.data = &msg[i].buf[0];
294 ret = af9015_ctrl_msg(d, &req);
295 i += 1;
Antti Palosaari80619de2008-09-15 17:18:09 -0300296 } else {
297 if (msg[i].addr ==
298 af9015_af9013_config[0].demod_address)
299 req.cmd = WRITE_MEMORY;
300 else
301 req.cmd = WRITE_I2C;
302 req.i2c_addr = msg[i].addr;
303 req.addr = addr;
304 req.mbox = mbox;
305 req.addr_len = addr_len;
306 req.data_len = msg[i].len-addr_len;
307 req.data = &msg[i].buf[addr_len];
308 ret = af9015_ctrl_msg(d, &req);
309 i += 1;
310 }
311 if (ret)
312 goto error;
313
314 }
315 ret = i;
316
317error:
318 mutex_unlock(&d->i2c_mutex);
319
320 return ret;
321}
322
323static u32 af9015_i2c_func(struct i2c_adapter *adapter)
324{
325 return I2C_FUNC_I2C;
326}
327
328static struct i2c_algorithm af9015_i2c_algo = {
329 .master_xfer = af9015_i2c_xfer,
330 .functionality = af9015_i2c_func,
331};
332
333static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
334{
335 int ret;
336 u8 val, mask = 0x01;
337
338 ret = af9015_read_reg(d, addr, &val);
339 if (ret)
340 return ret;
341
342 mask <<= bit;
343 if (op) {
344 /* set bit */
345 val |= mask;
346 } else {
347 /* clear bit */
348 mask ^= 0xff;
349 val &= mask;
350 }
351
352 return af9015_write_reg(d, addr, val);
353}
354
355static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
356{
357 return af9015_do_reg_bit(d, addr, bit, 1);
358}
359
360static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
361{
362 return af9015_do_reg_bit(d, addr, bit, 0);
363}
364
365static int af9015_init_endpoint(struct dvb_usb_device *d)
366{
367 int ret;
368 u16 frame_size;
369 u8 packet_size;
370 deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);
371
372#define TS_PACKET_SIZE 188
373
374#define TS_USB20_PACKET_COUNT 348
375#define TS_USB20_FRAME_SIZE (TS_PACKET_SIZE*TS_USB20_PACKET_COUNT)
376
377#define TS_USB11_PACKET_COUNT 21
378#define TS_USB11_FRAME_SIZE (TS_PACKET_SIZE*TS_USB11_PACKET_COUNT)
379
380#define TS_USB20_MAX_PACKET_SIZE 512
381#define TS_USB11_MAX_PACKET_SIZE 64
382
383 if (d->udev->speed == USB_SPEED_FULL) {
384 frame_size = TS_USB11_FRAME_SIZE/4;
385 packet_size = TS_USB11_MAX_PACKET_SIZE/4;
386 } else {
387 frame_size = TS_USB20_FRAME_SIZE/4;
388 packet_size = TS_USB20_MAX_PACKET_SIZE/4;
389 }
390
391 ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
392 if (ret)
393 goto error;
394 ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
395 if (ret)
396 goto error;
397 ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
398 if (ret)
399 goto error;
400 ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
401 if (ret)
402 goto error;
403 ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
404 if (ret)
405 goto error;
406 if (af9015_config.dual_mode) {
407 ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
408 if (ret)
409 goto error;
410 }
411 ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
412 if (ret)
413 goto error;
414 if (af9015_config.dual_mode) {
415 ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
416 if (ret)
417 goto error;
418 }
419 /* EP4 xfer length */
420 ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
421 if (ret)
422 goto error;
423 ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
424 if (ret)
425 goto error;
426 /* EP5 xfer length */
427 ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
428 if (ret)
429 goto error;
430 ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
431 if (ret)
432 goto error;
433 ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
434 if (ret)
435 goto error;
436 ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
437 if (ret)
438 goto error;
439 ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
440 if (ret)
441 goto error;
442 if (af9015_config.dual_mode) {
443 ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
444 if (ret)
445 goto error;
446 }
447
448 /* enable / disable mp2if2 */
449 if (af9015_config.dual_mode)
450 ret = af9015_set_reg_bit(d, 0xd50b, 0);
451 else
452 ret = af9015_clear_reg_bit(d, 0xd50b, 0);
453error:
454 if (ret)
455 err("endpoint init failed:%d", ret);
456 return ret;
457}
458
459static int af9015_copy_firmware(struct dvb_usb_device *d)
460{
461 int ret;
462 u8 fw_params[4];
463 u8 val, i;
464 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
465 fw_params };
466 deb_info("%s:\n", __func__);
467
468 fw_params[0] = af9015_config.firmware_size >> 8;
469 fw_params[1] = af9015_config.firmware_size & 0xff;
470 fw_params[2] = af9015_config.firmware_checksum >> 8;
471 fw_params[3] = af9015_config.firmware_checksum & 0xff;
472
473 /* wait 2nd demodulator ready */
474 msleep(100);
475
476 ret = af9015_read_reg_i2c(d, 0x3a, 0x98be, &val);
477 if (ret)
478 goto error;
479 else
480 deb_info("%s: firmware status:%02x\n", __func__, val);
481
482 if (val == 0x0c) /* fw is running, no need for download */
483 goto exit;
484
485 /* set I2C master clock to fast (to speed up firmware copy) */
486 ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
487 if (ret)
488 goto error;
489
490 msleep(50);
491
492 /* copy firmware */
493 ret = af9015_ctrl_msg(d, &req);
494 if (ret)
495 err("firmware copy cmd failed:%d", ret);
496 deb_info("%s: firmware copy done\n", __func__);
497
498 /* set I2C master clock back to normal */
499 ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
500 if (ret)
501 goto error;
502
503 /* request boot firmware */
504 ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].demod_address,
505 0xe205, 1);
506 deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
507 if (ret)
508 goto error;
509
510 for (i = 0; i < 15; i++) {
511 msleep(100);
512
513 /* check firmware status */
514 ret = af9015_read_reg_i2c(d,
515 af9015_af9013_config[1].demod_address, 0x98be, &val);
516 deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
517 __func__, ret, val);
518 if (ret)
519 goto error;
520
521 if (val == 0x0c || val == 0x04) /* success or fail */
522 break;
523 }
524
525 if (val == 0x04) {
526 err("firmware did not run");
527 ret = -1;
528 } else if (val != 0x0c) {
529 err("firmware boot timeout");
530 ret = -1;
531 }
532
533error:
534exit:
535 return ret;
536}
537
538/* dump eeprom */
539static int af9015_eeprom_dump(struct dvb_usb_device *d)
540{
541 char buf[52], buf2[4];
542 u8 reg, val;
543
544 for (reg = 0; ; reg++) {
545 if (reg % 16 == 0) {
546 if (reg)
547 deb_info("%s\n", buf);
548 sprintf(buf, "%02x: ", reg);
549 }
550 if (af9015_read_reg_i2c(d, AF9015_I2C_EEPROM, reg, &val) == 0)
551 sprintf(buf2, "%02x ", val);
552 else
553 strcpy(buf2, "-- ");
554 strcat(buf, buf2);
555 if (reg == 0xff)
556 break;
557 }
558 deb_info("%s\n", buf);
559 return 0;
560}
561
Antti Palosaari349d0422008-11-05 16:31:24 -0300562static int af9015_download_ir_table(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -0300563{
564 int i, packets = 0, ret;
565 u16 addr = 0x9a56; /* ir-table start address */
566 struct req_t req = {WRITE_MEMORY, 0, 0, 0, 0, 1, NULL};
567 u8 *data = NULL;
568 deb_info("%s:\n", __func__);
569
570 data = af9015_config.ir_table;
571 packets = af9015_config.ir_table_size;
572
573 /* no remote */
574 if (!packets)
575 goto exit;
576
577 /* load remote ir-table */
578 for (i = 0; i < packets; i++) {
579 req.addr = addr + i;
580 req.data = &data[i];
581 ret = af9015_ctrl_msg(d, &req);
582 if (ret) {
583 err("ir-table download failed at packet %d with " \
584 "code %d", i, ret);
585 return ret;
586 }
587 }
588
589exit:
590 return 0;
591}
592
593static int af9015_init(struct dvb_usb_device *d)
594{
595 int ret;
596 deb_info("%s:\n", __func__);
597
598 ret = af9015_init_endpoint(d);
599 if (ret)
600 goto error;
601
602 ret = af9015_download_ir_table(d);
603 if (ret)
604 goto error;
605
606error:
607 return ret;
608}
609
610static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
611{
612 int ret;
613 deb_info("%s: onoff:%d\n", __func__, onoff);
614
615 if (onoff)
616 ret = af9015_set_reg_bit(adap->dev, 0xd503, 0);
617 else
618 ret = af9015_clear_reg_bit(adap->dev, 0xd503, 0);
619
620 return ret;
621}
622
623static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
624 int onoff)
625{
626 int ret;
627 u8 idx;
628
629 deb_info("%s: set pid filter, index %d, pid %x, onoff %d\n",
630 __func__, index, pid, onoff);
631
632 ret = af9015_write_reg(adap->dev, 0xd505, (pid & 0xff));
633 if (ret)
634 goto error;
635
636 ret = af9015_write_reg(adap->dev, 0xd506, (pid >> 8));
637 if (ret)
638 goto error;
639
640 idx = ((index & 0x1f) | (1 << 5));
641 ret = af9015_write_reg(adap->dev, 0xd504, idx);
642
643error:
644 return ret;
645}
646
647static int af9015_download_firmware(struct usb_device *udev,
648 const struct firmware *fw)
649{
650 int i, len, packets, remainder, ret;
651 struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
652 u16 addr = 0x5100; /* firmware start address */
653 u16 checksum = 0;
654
655 deb_info("%s:\n", __func__);
656
657 /* calc checksum */
658 for (i = 0; i < fw->size; i++)
659 checksum += fw->data[i];
660
661 af9015_config.firmware_size = fw->size;
662 af9015_config.firmware_checksum = checksum;
663
664 #define FW_PACKET_MAX_DATA 55
665
666 packets = fw->size / FW_PACKET_MAX_DATA;
667 remainder = fw->size % FW_PACKET_MAX_DATA;
668 len = FW_PACKET_MAX_DATA;
669 for (i = 0; i <= packets; i++) {
670 if (i == packets) /* set size of the last packet */
671 len = remainder;
672
673 req.data_len = len;
Antti Palosaari541dfa82008-10-06 13:57:45 -0300674 req.data = (u8 *)(fw->data + i * FW_PACKET_MAX_DATA);
Antti Palosaari80619de2008-09-15 17:18:09 -0300675 req.addr = addr;
676 addr += FW_PACKET_MAX_DATA;
677
678 ret = af9015_rw_udev(udev, &req);
679 if (ret) {
680 err("firmware download failed at packet %d with " \
681 "code %d", i, ret);
682 goto error;
683 }
684 }
Antti Palosaari80619de2008-09-15 17:18:09 -0300685
686 /* firmware loaded, request boot */
687 req.cmd = BOOT;
688 ret = af9015_rw_udev(udev, &req);
689 if (ret) {
690 err("firmware boot failed:%d", ret);
691 goto error;
692 }
693
Antti Palosaari80619de2008-09-15 17:18:09 -0300694error:
695 return ret;
696}
697
698static int af9015_read_config(struct usb_device *udev)
699{
700 int ret;
701 u8 val, i, offset = 0;
702 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
703 char manufacturer[10];
704
705 /* IR remote controller */
706 req.addr = AF9015_EEPROM_IR_MODE;
Antti Palosaarid1a470f2009-01-20 14:56:20 -0300707 /* first message will timeout often due to possible hw bug */
708 for (i = 0; i < 4; i++) {
709 ret = af9015_rw_udev(udev, &req);
710 if (!ret)
711 break;
712 }
Antti Palosaari80619de2008-09-15 17:18:09 -0300713 if (ret)
714 goto error;
715 deb_info("%s: IR mode:%d\n", __func__, val);
716 for (i = 0; i < af9015_properties_count; i++) {
717 if (val == AF9015_IR_MODE_DISABLED || val == 0x04) {
718 af9015_properties[i].rc_key_map = NULL;
719 af9015_properties[i].rc_key_map_size = 0;
720 } else if (dvb_usb_af9015_remote) {
721 /* load remote defined as module param */
722 switch (dvb_usb_af9015_remote) {
723 case AF9015_REMOTE_A_LINK_DTU_M:
724 af9015_properties[i].rc_key_map =
725 af9015_rc_keys_a_link;
726 af9015_properties[i].rc_key_map_size =
727 ARRAY_SIZE(af9015_rc_keys_a_link);
728 af9015_config.ir_table = af9015_ir_table_a_link;
729 af9015_config.ir_table_size =
730 ARRAY_SIZE(af9015_ir_table_a_link);
731 break;
732 case AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3:
733 af9015_properties[i].rc_key_map =
734 af9015_rc_keys_msi;
735 af9015_properties[i].rc_key_map_size =
736 ARRAY_SIZE(af9015_rc_keys_msi);
737 af9015_config.ir_table = af9015_ir_table_msi;
738 af9015_config.ir_table_size =
739 ARRAY_SIZE(af9015_ir_table_msi);
740 break;
741 case AF9015_REMOTE_MYGICTV_U718:
742 af9015_properties[i].rc_key_map =
743 af9015_rc_keys_mygictv;
744 af9015_properties[i].rc_key_map_size =
745 ARRAY_SIZE(af9015_rc_keys_mygictv);
746 af9015_config.ir_table =
747 af9015_ir_table_mygictv;
748 af9015_config.ir_table_size =
749 ARRAY_SIZE(af9015_ir_table_mygictv);
750 break;
Alain Kalker05371562008-11-17 17:20:38 -0300751 case AF9015_REMOTE_DIGITTRADE_DVB_T:
752 af9015_properties[i].rc_key_map =
753 af9015_rc_keys_digittrade;
754 af9015_properties[i].rc_key_map_size =
755 ARRAY_SIZE(af9015_rc_keys_digittrade);
756 af9015_config.ir_table =
757 af9015_ir_table_digittrade;
758 af9015_config.ir_table_size =
759 ARRAY_SIZE(af9015_ir_table_digittrade);
760 break;
Jose Alberto Reguero90b0f692009-01-23 19:23:23 -0300761 case AF9015_REMOTE_AVERMEDIA_KS:
762 af9015_properties[i].rc_key_map =
763 af9015_rc_keys_avermedia;
764 af9015_properties[i].rc_key_map_size =
765 ARRAY_SIZE(af9015_rc_keys_avermedia);
766 af9015_config.ir_table =
767 af9015_ir_table_avermedia_ks;
768 af9015_config.ir_table_size =
769 ARRAY_SIZE(af9015_ir_table_avermedia_ks);
770 break;
Antti Palosaari80619de2008-09-15 17:18:09 -0300771 }
772 } else {
Hans Verkuilc58701b2008-11-20 05:37:05 -0300773 switch (le16_to_cpu(udev->descriptor.idVendor)) {
774 case USB_VID_LEADTEK:
Antti Palosaari80619de2008-09-15 17:18:09 -0300775 af9015_properties[i].rc_key_map =
776 af9015_rc_keys_leadtek;
777 af9015_properties[i].rc_key_map_size =
778 ARRAY_SIZE(af9015_rc_keys_leadtek);
779 af9015_config.ir_table =
780 af9015_ir_table_leadtek;
781 af9015_config.ir_table_size =
782 ARRAY_SIZE(af9015_ir_table_leadtek);
783 break;
Hans Verkuilc58701b2008-11-20 05:37:05 -0300784 case USB_VID_VISIONPLUS:
Stuart Hall6252d252009-04-02 05:25:09 -0300785 af9015_properties[i].rc_key_map =
786 af9015_rc_keys_twinhan;
787 af9015_properties[i].rc_key_map_size =
788 ARRAY_SIZE(af9015_rc_keys_twinhan);
789 af9015_config.ir_table =
790 af9015_ir_table_twinhan;
791 af9015_config.ir_table_size =
792 ARRAY_SIZE(af9015_ir_table_twinhan);
Antti Palosaari80619de2008-09-15 17:18:09 -0300793 break;
Hans Verkuilc58701b2008-11-20 05:37:05 -0300794 case USB_VID_KWORLD_2:
Antti Palosaari80619de2008-09-15 17:18:09 -0300795 /* TODO: use correct rc keys */
796 af9015_properties[i].rc_key_map =
797 af9015_rc_keys_twinhan;
798 af9015_properties[i].rc_key_map_size =
799 ARRAY_SIZE(af9015_rc_keys_twinhan);
800 af9015_config.ir_table = af9015_ir_table_kworld;
801 af9015_config.ir_table_size =
802 ARRAY_SIZE(af9015_ir_table_kworld);
803 break;
804 /* Check USB manufacturer and product strings and try
805 to determine correct remote in case of chip vendor
806 reference IDs are used. */
Hans Verkuilc58701b2008-11-20 05:37:05 -0300807 case USB_VID_AFATECH:
Antti Palosaari80619de2008-09-15 17:18:09 -0300808 memset(manufacturer, 0, sizeof(manufacturer));
809 usb_string(udev, udev->descriptor.iManufacturer,
810 manufacturer, sizeof(manufacturer));
811 if (!strcmp("Geniatech", manufacturer)) {
812 /* iManufacturer 1 Geniatech
813 iProduct 2 AF9015 */
814 af9015_properties[i].rc_key_map =
815 af9015_rc_keys_mygictv;
816 af9015_properties[i].rc_key_map_size =
817 ARRAY_SIZE(af9015_rc_keys_mygictv);
818 af9015_config.ir_table =
819 af9015_ir_table_mygictv;
820 af9015_config.ir_table_size =
821 ARRAY_SIZE(af9015_ir_table_mygictv);
822 } else if (!strcmp("MSI", manufacturer)) {
823 /* iManufacturer 1 MSI
824 iProduct 2 MSI K-VOX */
825 af9015_properties[i].rc_key_map =
826 af9015_rc_keys_msi;
827 af9015_properties[i].rc_key_map_size =
828 ARRAY_SIZE(af9015_rc_keys_msi);
829 af9015_config.ir_table =
830 af9015_ir_table_msi;
831 af9015_config.ir_table_size =
832 ARRAY_SIZE(af9015_ir_table_msi);
Marc Schneider26144842009-03-26 21:07:18 -0300833 } else if (udev->descriptor.idProduct ==
834 cpu_to_le16(USB_PID_TREKSTOR_DVBT)) {
835 af9015_properties[i].rc_key_map =
836 af9015_rc_keys_trekstor;
837 af9015_properties[i].rc_key_map_size =
838 ARRAY_SIZE(af9015_rc_keys_trekstor);
839 af9015_config.ir_table =
840 af9015_ir_table_trekstor;
841 af9015_config.ir_table_size =
842 ARRAY_SIZE(af9015_ir_table_trekstor);
Antti Palosaari80619de2008-09-15 17:18:09 -0300843 }
844 break;
Hans Verkuilc58701b2008-11-20 05:37:05 -0300845 case USB_VID_AVERMEDIA:
Jose Alberto Regueroc78de712008-11-04 11:38:01 -0300846 af9015_properties[i].rc_key_map =
847 af9015_rc_keys_avermedia;
848 af9015_properties[i].rc_key_map_size =
849 ARRAY_SIZE(af9015_rc_keys_avermedia);
850 af9015_config.ir_table =
851 af9015_ir_table_avermedia;
852 af9015_config.ir_table_size =
853 ARRAY_SIZE(af9015_ir_table_avermedia);
854 break;
Antti Palosaari80619de2008-09-15 17:18:09 -0300855 }
856 }
857 }
858
859 /* TS mode - one or two receivers */
860 req.addr = AF9015_EEPROM_TS_MODE;
861 ret = af9015_rw_udev(udev, &req);
862 if (ret)
863 goto error;
864 af9015_config.dual_mode = val;
865 deb_info("%s: TS mode:%d\n", __func__, af9015_config.dual_mode);
Antti Palosaari80619de2008-09-15 17:18:09 -0300866
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300867 /* Set adapter0 buffer size according to USB port speed, adapter1 buffer
868 size can be static because it is enabled only USB2.0 */
Antti Palosaari80619de2008-09-15 17:18:09 -0300869 for (i = 0; i < af9015_properties_count; i++) {
870 /* USB1.1 set smaller buffersize and disable 2nd adapter */
871 if (udev->speed == USB_SPEED_FULL) {
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300872 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
873 = TS_USB11_MAX_PACKET_SIZE;
Antti Palosaari80619de2008-09-15 17:18:09 -0300874 /* disable 2nd adapter because we don't have
875 PID-filters */
876 af9015_config.dual_mode = 0;
877 } else {
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300878 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
879 = TS_USB20_MAX_PACKET_SIZE;
Antti Palosaari80619de2008-09-15 17:18:09 -0300880 }
881 }
882
883 if (af9015_config.dual_mode) {
884 /* read 2nd demodulator I2C address */
885 req.addr = AF9015_EEPROM_DEMOD2_I2C;
886 ret = af9015_rw_udev(udev, &req);
887 if (ret)
888 goto error;
889 af9015_af9013_config[1].demod_address = val;
890
891 /* enable 2nd adapter */
892 for (i = 0; i < af9015_properties_count; i++)
893 af9015_properties[i].num_adapters = 2;
894
895 } else {
896 /* disable 2nd adapter */
897 for (i = 0; i < af9015_properties_count; i++)
898 af9015_properties[i].num_adapters = 1;
899 }
900
901 for (i = 0; i < af9015_properties[0].num_adapters; i++) {
902 if (i == 1)
903 offset = AF9015_EEPROM_OFFSET;
904 /* xtal */
905 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
906 ret = af9015_rw_udev(udev, &req);
907 if (ret)
908 goto error;
909 switch (val) {
910 case 0:
911 af9015_af9013_config[i].adc_clock = 28800;
912 break;
913 case 1:
914 af9015_af9013_config[i].adc_clock = 20480;
915 break;
916 case 2:
917 af9015_af9013_config[i].adc_clock = 28000;
918 break;
919 case 3:
920 af9015_af9013_config[i].adc_clock = 25000;
921 break;
922 };
923 deb_info("%s: [%d] xtal:%d set adc_clock:%d\n", __func__, i,
924 val, af9015_af9013_config[i].adc_clock);
925
926 /* tuner IF */
927 req.addr = AF9015_EEPROM_IF1H + offset;
928 ret = af9015_rw_udev(udev, &req);
929 if (ret)
930 goto error;
931 af9015_af9013_config[i].tuner_if = val << 8;
932 req.addr = AF9015_EEPROM_IF1L + offset;
933 ret = af9015_rw_udev(udev, &req);
934 if (ret)
935 goto error;
936 af9015_af9013_config[i].tuner_if += val;
937 deb_info("%s: [%d] IF1:%d\n", __func__, i,
938 af9015_af9013_config[0].tuner_if);
939
940 /* MT2060 IF1 */
941 req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
942 ret = af9015_rw_udev(udev, &req);
943 if (ret)
944 goto error;
945 af9015_config.mt2060_if1[i] = val << 8;
946 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
947 ret = af9015_rw_udev(udev, &req);
948 if (ret)
949 goto error;
950 af9015_config.mt2060_if1[i] += val;
951 deb_info("%s: [%d] MT2060 IF1:%d\n", __func__, i,
952 af9015_config.mt2060_if1[i]);
953
954 /* tuner */
955 req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
956 ret = af9015_rw_udev(udev, &req);
957 if (ret)
958 goto error;
959 switch (val) {
960 case AF9013_TUNER_ENV77H11D5:
961 case AF9013_TUNER_MT2060:
Antti Palosaari80619de2008-09-15 17:18:09 -0300962 case AF9013_TUNER_QT1010:
963 case AF9013_TUNER_UNKNOWN:
964 case AF9013_TUNER_MT2060_2:
965 case AF9013_TUNER_TDA18271:
966 case AF9013_TUNER_QT1010A:
967 af9015_af9013_config[i].rf_spec_inv = 1;
968 break;
969 case AF9013_TUNER_MXL5003D:
970 case AF9013_TUNER_MXL5005D:
971 case AF9013_TUNER_MXL5005R:
972 af9015_af9013_config[i].rf_spec_inv = 0;
973 break;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300974 case AF9013_TUNER_MC44S803:
975 af9015_af9013_config[i].gpio[1] = AF9013_GPIO_LO;
976 af9015_af9013_config[i].rf_spec_inv = 1;
977 break;
Antti Palosaari80619de2008-09-15 17:18:09 -0300978 default:
979 warn("tuner id:%d not supported, please report!", val);
980 return -ENODEV;
981 };
982
983 af9015_af9013_config[i].tuner = val;
984 deb_info("%s: [%d] tuner id:%d\n", __func__, i, val);
985 }
986
987error:
988 if (ret)
989 err("eeprom read failed:%d", ret);
990
Antti Palosaari3956fef2009-03-31 17:01:02 -0300991 /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
992 content :-( Override some wrong values here. */
993 if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
994 le16_to_cpu(udev->descriptor.idProduct) == USB_PID_AVERMEDIA_A850) {
995 deb_info("%s: AverMedia A850: overriding config\n", __func__);
996 /* disable dual mode */
997 af9015_config.dual_mode = 0;
998 /* disable 2nd adapter */
999 for (i = 0; i < af9015_properties_count; i++)
1000 af9015_properties[i].num_adapters = 1;
1001
1002 /* set correct IF */
1003 af9015_af9013_config[0].tuner_if = 4570;
1004 }
1005
Antti Palosaari80619de2008-09-15 17:18:09 -03001006 return ret;
1007}
1008
1009static int af9015_identify_state(struct usb_device *udev,
1010 struct dvb_usb_device_properties *props,
1011 struct dvb_usb_device_description **desc,
1012 int *cold)
1013{
1014 int ret;
1015 u8 reply;
1016 struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
1017
1018 ret = af9015_rw_udev(udev, &req);
1019 if (ret)
1020 return ret;
1021
1022 deb_info("%s: reply:%02x\n", __func__, reply);
1023 if (reply == 0x02)
1024 *cold = 0;
1025 else
1026 *cold = 1;
1027
1028 return ret;
1029}
1030
1031static int af9015_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
1032{
1033 u8 buf[8];
1034 struct req_t req = {GET_IR_CODE, 0, 0, 0, 0, sizeof(buf), buf};
1035 struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
1036 int i, ret;
1037
1038 memset(buf, 0, sizeof(buf));
1039
1040 ret = af9015_ctrl_msg(d, &req);
1041 if (ret)
1042 return ret;
1043
1044 *event = 0;
1045 *state = REMOTE_NO_KEY_PRESSED;
1046
1047 for (i = 0; i < d->props.rc_key_map_size; i++) {
1048 if (!buf[1] && keymap[i].custom == buf[0] &&
1049 keymap[i].data == buf[2]) {
1050 *event = keymap[i].event;
1051 *state = REMOTE_KEY_PRESSED;
1052 break;
1053 }
1054 }
1055 if (!buf[1])
1056 deb_rc("%s: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1057 __func__, buf[0], buf[1], buf[2], buf[3], buf[4],
1058 buf[5], buf[6], buf[7]);
1059
1060 return 0;
1061}
1062
1063/* init 2nd I2C adapter */
Antti Palosaari349d0422008-11-05 16:31:24 -03001064static int af9015_i2c_init(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -03001065{
1066 int ret;
1067 struct af9015_state *state = d->priv;
1068 deb_info("%s:\n", __func__);
1069
1070 strncpy(state->i2c_adap.name, d->desc->name,
1071 sizeof(state->i2c_adap.name));
1072#ifdef I2C_ADAP_CLASS_TV_DIGITAL
1073 state->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL,
1074#else
1075 state->i2c_adap.class = I2C_CLASS_TV_DIGITAL,
1076#endif
1077 state->i2c_adap.algo = d->props.i2c_algo;
1078 state->i2c_adap.algo_data = NULL;
1079 state->i2c_adap.dev.parent = &d->udev->dev;
1080
1081 i2c_set_adapdata(&state->i2c_adap, d);
1082
1083 ret = i2c_add_adapter(&state->i2c_adap);
1084 if (ret < 0)
1085 err("could not add i2c adapter");
1086
1087 return ret;
1088}
1089
1090static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
1091{
1092 int ret;
1093 struct af9015_state *state = adap->dev->priv;
1094 struct i2c_adapter *i2c_adap;
1095
1096 if (adap->id == 0) {
1097 /* select I2C adapter */
1098 i2c_adap = &adap->dev->i2c_adap;
1099
1100 deb_info("%s: init I2C\n", __func__);
1101 ret = af9015_i2c_init(adap->dev);
1102
1103 /* dump eeprom (debug) */
1104 ret = af9015_eeprom_dump(adap->dev);
1105 if (ret)
1106 return ret;
1107 } else {
1108 /* select I2C adapter */
1109 i2c_adap = &state->i2c_adap;
1110
1111 /* copy firmware to 2nd demodulator */
1112 if (af9015_config.dual_mode) {
1113 ret = af9015_copy_firmware(adap->dev);
1114 if (ret) {
1115 err("firmware copy to 2nd frontend " \
1116 "failed, will disable it");
1117 af9015_config.dual_mode = 0;
1118 return -ENODEV;
1119 }
1120 } else {
1121 return -ENODEV;
1122 }
1123 }
1124
1125 /* attach demodulator */
1126 adap->fe = dvb_attach(af9013_attach, &af9015_af9013_config[adap->id],
1127 i2c_adap);
1128
1129 return adap->fe == NULL ? -ENODEV : 0;
1130}
1131
1132static struct mt2060_config af9015_mt2060_config = {
1133 .i2c_address = 0xc0,
1134 .clock_out = 0,
1135};
1136
1137static struct qt1010_config af9015_qt1010_config = {
1138 .i2c_address = 0xc4,
1139};
1140
1141static struct tda18271_config af9015_tda18271_config = {
1142 .gate = TDA18271_GATE_DIGITAL,
1143 .small_i2c = 1,
1144};
1145
1146static struct mxl5005s_config af9015_mxl5003_config = {
1147 .i2c_address = 0xc6,
1148 .if_freq = IF_FREQ_4570000HZ,
1149 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1150 .agc_mode = MXL_SINGLE_AGC,
1151 .tracking_filter = MXL_TF_DEFAULT,
Antti Palosaaria1310772008-09-22 13:59:25 -03001152 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -03001153 .cap_select = MXL_CAP_SEL_ENABLE,
1154 .div_out = MXL_DIV_OUT_4,
1155 .clock_out = MXL_CLOCK_OUT_DISABLE,
1156 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1157 .top = MXL5005S_TOP_25P2,
1158 .mod_mode = MXL_DIGITAL_MODE,
1159 .if_mode = MXL_ZERO_IF,
1160 .AgcMasterByte = 0x00,
1161};
1162
1163static struct mxl5005s_config af9015_mxl5005_config = {
1164 .i2c_address = 0xc6,
1165 .if_freq = IF_FREQ_4570000HZ,
1166 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1167 .agc_mode = MXL_SINGLE_AGC,
1168 .tracking_filter = MXL_TF_OFF,
Antti Palosaaria1310772008-09-22 13:59:25 -03001169 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -03001170 .cap_select = MXL_CAP_SEL_ENABLE,
1171 .div_out = MXL_DIV_OUT_4,
1172 .clock_out = MXL_CLOCK_OUT_DISABLE,
1173 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1174 .top = MXL5005S_TOP_25P2,
1175 .mod_mode = MXL_DIGITAL_MODE,
1176 .if_mode = MXL_ZERO_IF,
1177 .AgcMasterByte = 0x00,
1178};
1179
Jochen Friedrichd5633992009-02-02 14:59:50 -03001180static struct mc44s803_config af9015_mc44s803_config = {
1181 .i2c_address = 0xc0,
1182 .dig_out = 1,
1183};
1184
Antti Palosaari80619de2008-09-15 17:18:09 -03001185static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
1186{
1187 struct af9015_state *state = adap->dev->priv;
1188 struct i2c_adapter *i2c_adap;
1189 int ret;
1190 deb_info("%s: \n", __func__);
1191
1192 /* select I2C adapter */
1193 if (adap->id == 0)
1194 i2c_adap = &adap->dev->i2c_adap;
1195 else
1196 i2c_adap = &state->i2c_adap;
1197
1198 switch (af9015_af9013_config[adap->id].tuner) {
1199 case AF9013_TUNER_MT2060:
1200 case AF9013_TUNER_MT2060_2:
1201 ret = dvb_attach(mt2060_attach, adap->fe, i2c_adap,
1202 &af9015_mt2060_config,
1203 af9015_config.mt2060_if1[adap->id])
1204 == NULL ? -ENODEV : 0;
1205 break;
1206 case AF9013_TUNER_QT1010:
1207 case AF9013_TUNER_QT1010A:
1208 ret = dvb_attach(qt1010_attach, adap->fe, i2c_adap,
1209 &af9015_qt1010_config) == NULL ? -ENODEV : 0;
1210 break;
1211 case AF9013_TUNER_TDA18271:
1212 ret = dvb_attach(tda18271_attach, adap->fe, 0xc0, i2c_adap,
1213 &af9015_tda18271_config) == NULL ? -ENODEV : 0;
1214 break;
1215 case AF9013_TUNER_MXL5003D:
1216 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1217 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
1218 break;
1219 case AF9013_TUNER_MXL5005D:
1220 case AF9013_TUNER_MXL5005R:
1221 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1222 &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
1223 break;
1224 case AF9013_TUNER_ENV77H11D5:
1225 ret = dvb_attach(dvb_pll_attach, adap->fe, 0xc0, i2c_adap,
1226 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
1227 break;
1228 case AF9013_TUNER_MC44S803:
Jochen Friedrichd5633992009-02-02 14:59:50 -03001229 ret = dvb_attach(mc44s803_attach, adap->fe, i2c_adap,
1230 &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
Antti Palosaari80619de2008-09-15 17:18:09 -03001231 break;
1232 case AF9013_TUNER_UNKNOWN:
1233 default:
1234 ret = -ENODEV;
1235 err("Unknown tuner id:%d",
1236 af9015_af9013_config[adap->id].tuner);
1237 }
1238 return ret;
1239}
1240
1241static struct usb_device_id af9015_usb_table[] = {
1242/* 0 */{USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015)},
1243 {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016)},
1244 {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD)},
1245 {USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E)},
1246 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U)},
1247/* 5 */{USB_DEVICE(USB_VID_VISIONPLUS,
1248 USB_PID_TINYTWIN)},
1249 {USB_DEVICE(USB_VID_VISIONPLUS,
1250 USB_PID_AZUREWAVE_AD_TU700)},
1251 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2)},
1252 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T)},
1253 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X)},
1254/* 10 */{USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380)},
1255 {USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO)},
1256 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2)},
Antti Palosaaria3765882008-09-19 18:34:06 -03001257 {USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2)},
Antti Palosaari05c1cab2008-09-22 12:32:37 -03001258 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309)},
Herbert Graeber641015a2008-10-07 10:06:36 -03001259/* 15 */{USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III)},
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001260 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U)},
Antti Palosaari71bf2e02009-01-13 12:47:28 -03001261 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2)},
Antti Palosaari58fe1592009-03-26 20:41:05 -03001262 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3)},
Marc Schneider26144842009-03-26 21:07:18 -03001263 {USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT)},
Antti Palosaari3956fef2009-03-31 17:01:02 -03001264 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850)},
Antti Palosaari80619de2008-09-15 17:18:09 -03001265 {0},
1266};
1267MODULE_DEVICE_TABLE(usb, af9015_usb_table);
1268
1269static struct dvb_usb_device_properties af9015_properties[] = {
1270 {
1271 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1272
1273 .usb_ctrl = DEVICE_SPECIFIC,
1274 .download_firmware = af9015_download_firmware,
1275 .firmware = "dvb-usb-af9015.fw",
Jose Alberto Reguerocce25712008-11-13 14:14:18 -03001276 .no_reconnect = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001277
1278 .size_of_priv = sizeof(struct af9015_state), \
1279
1280 .num_adapters = 2,
1281 .adapter = {
1282 {
1283 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1284 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1285
1286 .pid_filter_count = 32,
1287 .pid_filter = af9015_pid_filter,
1288 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1289
1290 .frontend_attach =
1291 af9015_af9013_frontend_attach,
1292 .tuner_attach = af9015_tuner_attach,
1293 .stream = {
1294 .type = USB_BULK,
1295 .count = 6,
1296 .endpoint = 0x84,
1297 },
1298 },
1299 {
1300 .frontend_attach =
1301 af9015_af9013_frontend_attach,
1302 .tuner_attach = af9015_tuner_attach,
1303 .stream = {
1304 .type = USB_BULK,
1305 .count = 6,
1306 .endpoint = 0x85,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001307 .u = {
1308 .bulk = {
1309 .buffersize =
1310 TS_USB20_MAX_PACKET_SIZE,
1311 }
1312 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001313 },
1314 }
1315 },
1316
1317 .identify_state = af9015_identify_state,
1318
1319 .rc_query = af9015_rc_query,
1320 .rc_interval = 150,
1321
1322 .i2c_algo = &af9015_i2c_algo,
1323
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001324 .num_device_descs = 9, /* max 9 */
Antti Palosaari80619de2008-09-15 17:18:09 -03001325 .devices = {
1326 {
1327 .name = "Afatech AF9015 DVB-T USB2.0 stick",
1328 .cold_ids = {&af9015_usb_table[0],
1329 &af9015_usb_table[1], NULL},
1330 .warm_ids = {NULL},
1331 },
1332 {
1333 .name = "Leadtek WinFast DTV Dongle Gold",
1334 .cold_ids = {&af9015_usb_table[2], NULL},
1335 .warm_ids = {NULL},
1336 },
1337 {
1338 .name = "Pinnacle PCTV 71e",
1339 .cold_ids = {&af9015_usb_table[3], NULL},
1340 .warm_ids = {NULL},
1341 },
1342 {
1343 .name = "KWorld PlusTV Dual DVB-T Stick " \
1344 "(DVB-T 399U)",
1345 .cold_ids = {&af9015_usb_table[4], NULL},
1346 .warm_ids = {NULL},
1347 },
1348 {
1349 .name = "DigitalNow TinyTwin DVB-T Receiver",
1350 .cold_ids = {&af9015_usb_table[5], NULL},
1351 .warm_ids = {NULL},
1352 },
1353 {
1354 .name = "TwinHan AzureWave AD-TU700(704J)",
1355 .cold_ids = {&af9015_usb_table[6], NULL},
1356 .warm_ids = {NULL},
1357 },
1358 {
1359 .name = "TerraTec Cinergy T USB XE",
1360 .cold_ids = {&af9015_usb_table[7], NULL},
1361 .warm_ids = {NULL},
1362 },
1363 {
1364 .name = "KWorld PlusTV Dual DVB-T PCI " \
1365 "(DVB-T PC160-2T)",
1366 .cold_ids = {&af9015_usb_table[8], NULL},
1367 .warm_ids = {NULL},
1368 },
1369 {
1370 .name = "AVerMedia AVerTV DVB-T Volar X",
1371 .cold_ids = {&af9015_usb_table[9], NULL},
1372 .warm_ids = {NULL},
1373 },
1374 }
1375 }, {
1376 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1377
1378 .usb_ctrl = DEVICE_SPECIFIC,
1379 .download_firmware = af9015_download_firmware,
1380 .firmware = "dvb-usb-af9015.fw",
Jose Alberto Reguerocce25712008-11-13 14:14:18 -03001381 .no_reconnect = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001382
1383 .size_of_priv = sizeof(struct af9015_state), \
1384
1385 .num_adapters = 2,
1386 .adapter = {
1387 {
1388 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1389 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1390
1391 .pid_filter_count = 32,
1392 .pid_filter = af9015_pid_filter,
1393 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1394
1395 .frontend_attach =
1396 af9015_af9013_frontend_attach,
1397 .tuner_attach = af9015_tuner_attach,
1398 .stream = {
1399 .type = USB_BULK,
1400 .count = 6,
1401 .endpoint = 0x84,
1402 },
1403 },
1404 {
1405 .frontend_attach =
1406 af9015_af9013_frontend_attach,
1407 .tuner_attach = af9015_tuner_attach,
1408 .stream = {
1409 .type = USB_BULK,
1410 .count = 6,
1411 .endpoint = 0x85,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001412 .u = {
1413 .bulk = {
1414 .buffersize =
1415 TS_USB20_MAX_PACKET_SIZE,
1416 }
1417 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001418 },
1419 }
1420 },
1421
1422 .identify_state = af9015_identify_state,
1423
1424 .rc_query = af9015_rc_query,
1425 .rc_interval = 150,
1426
1427 .i2c_algo = &af9015_i2c_algo,
1428
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001429 .num_device_descs = 9, /* max 9 */
Antti Palosaari80619de2008-09-15 17:18:09 -03001430 .devices = {
1431 {
1432 .name = "Xtensions XD-380",
1433 .cold_ids = {&af9015_usb_table[10], NULL},
1434 .warm_ids = {NULL},
1435 },
1436 {
1437 .name = "MSI DIGIVOX Duo",
1438 .cold_ids = {&af9015_usb_table[11], NULL},
1439 .warm_ids = {NULL},
1440 },
1441 {
1442 .name = "Fujitsu-Siemens Slim Mobile USB DVB-T",
1443 .cold_ids = {&af9015_usb_table[12], NULL},
1444 .warm_ids = {NULL},
1445 },
Mikko Ohtamaa111f9ec2008-09-19 18:26:05 -03001446 {
Antti Palosaaria3765882008-09-19 18:34:06 -03001447 .name = "Telestar Starstick 2",
Mikko Ohtamaa111f9ec2008-09-19 18:26:05 -03001448 .cold_ids = {&af9015_usb_table[13], NULL},
1449 .warm_ids = {NULL},
1450 },
Antti Palosaari05c1cab2008-09-22 12:32:37 -03001451 {
1452 .name = "AVerMedia A309",
1453 .cold_ids = {&af9015_usb_table[14], NULL},
1454 .warm_ids = {NULL},
1455 },
Herbert Graeber641015a2008-10-07 10:06:36 -03001456 {
1457 .name = "MSI Digi VOX mini III",
1458 .cold_ids = {&af9015_usb_table[15], NULL},
1459 .warm_ids = {NULL},
1460 },
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001461 {
1462 .name = "KWorld USB DVB-T TV Stick II " \
1463 "(VS-DVB-T 395U)",
Antti Palosaari71bf2e02009-01-13 12:47:28 -03001464 .cold_ids = {&af9015_usb_table[16],
Antti Palosaari58fe1592009-03-26 20:41:05 -03001465 &af9015_usb_table[17],
1466 &af9015_usb_table[18], NULL},
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001467 .warm_ids = {NULL},
1468 },
Marc Schneider26144842009-03-26 21:07:18 -03001469 {
1470 .name = "TrekStor DVB-T USB Stick",
1471 .cold_ids = {&af9015_usb_table[19], NULL},
1472 .warm_ids = {NULL},
1473 },
Antti Palosaari3956fef2009-03-31 17:01:02 -03001474 {
1475 .name = "AverMedia AVerTV Volar Black HD " \
1476 "(A850)",
1477 .cold_ids = {&af9015_usb_table[20], NULL},
1478 .warm_ids = {NULL},
1479 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001480 }
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001481 }, {
1482 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1483
1484 .usb_ctrl = DEVICE_SPECIFIC,
1485 .download_firmware = af9015_download_firmware,
1486 .firmware = "dvb-usb-af9015.fw",
1487 .no_reconnect = 1,
1488
1489 .size_of_priv = sizeof(struct af9015_state), \
1490
1491 .num_adapters = 2,
1492 .adapter = {
1493 {
1494 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1495 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1496
1497 .pid_filter_count = 32,
1498 .pid_filter = af9015_pid_filter,
1499 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1500
1501 .frontend_attach =
1502 af9015_af9013_frontend_attach,
1503 .tuner_attach = af9015_tuner_attach,
1504 .stream = {
1505 .type = USB_BULK,
1506 .count = 6,
1507 .endpoint = 0x84,
1508 },
1509 },
1510 {
1511 .frontend_attach =
1512 af9015_af9013_frontend_attach,
1513 .tuner_attach = af9015_tuner_attach,
1514 .stream = {
1515 .type = USB_BULK,
1516 .count = 6,
1517 .endpoint = 0x85,
1518 .u = {
1519 .bulk = {
1520 .buffersize =
1521 TS_USB20_MAX_PACKET_SIZE,
1522 }
1523 }
1524 },
1525 }
1526 },
1527
1528 .identify_state = af9015_identify_state,
1529
1530 .rc_query = af9015_rc_query,
1531 .rc_interval = 150,
1532
1533 .i2c_algo = &af9015_i2c_algo,
1534
1535 .num_device_descs = 0, /* max 9 */
1536 .devices = {
1537 }
1538 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001539};
Antti Palosaari80619de2008-09-15 17:18:09 -03001540
1541static int af9015_usb_probe(struct usb_interface *intf,
1542 const struct usb_device_id *id)
1543{
1544 int ret = 0;
1545 struct dvb_usb_device *d = NULL;
1546 struct usb_device *udev = interface_to_usbdev(intf);
1547 u8 i;
1548
1549 deb_info("%s: interface:%d\n", __func__,
1550 intf->cur_altsetting->desc.bInterfaceNumber);
1551
1552 /* interface 0 is used by DVB-T receiver and
1553 interface 1 is for remote controller (HID) */
1554 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
1555 ret = af9015_read_config(udev);
1556 if (ret)
1557 return ret;
1558
1559 for (i = 0; i < af9015_properties_count; i++) {
1560 ret = dvb_usb_device_init(intf, &af9015_properties[i],
1561 THIS_MODULE, &d, adapter_nr);
1562 if (!ret)
1563 break;
1564 if (ret != -ENODEV)
1565 return ret;
1566 }
1567 if (ret)
1568 return ret;
1569
1570 if (d)
1571 ret = af9015_init(d);
1572 }
1573
1574 return ret;
1575}
1576
Antti Palosaari349d0422008-11-05 16:31:24 -03001577static void af9015_i2c_exit(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -03001578{
1579 struct af9015_state *state = d->priv;
1580 deb_info("%s: \n", __func__);
1581
1582 /* remove 2nd I2C adapter */
1583 if (d->state & DVB_USB_STATE_I2C)
1584 i2c_del_adapter(&state->i2c_adap);
1585}
1586
1587static void af9015_usb_device_exit(struct usb_interface *intf)
1588{
1589 struct dvb_usb_device *d = usb_get_intfdata(intf);
1590 deb_info("%s: \n", __func__);
1591
1592 /* remove 2nd I2C adapter */
1593 if (d != NULL && d->desc != NULL)
1594 af9015_i2c_exit(d);
1595
1596 dvb_usb_device_exit(intf);
1597}
1598
1599/* usb specific object needed to register this driver with the usb subsystem */
1600static struct usb_driver af9015_usb_driver = {
1601 .name = "dvb_usb_af9015",
1602 .probe = af9015_usb_probe,
1603 .disconnect = af9015_usb_device_exit,
1604 .id_table = af9015_usb_table,
1605};
1606
1607/* module stuff */
1608static int __init af9015_usb_module_init(void)
1609{
1610 int ret;
1611 ret = usb_register(&af9015_usb_driver);
1612 if (ret)
1613 err("module init failed:%d", ret);
1614
1615 return ret;
1616}
1617
1618static void __exit af9015_usb_module_exit(void)
1619{
1620 /* deregister this driver from the USB subsystem */
1621 usb_deregister(&af9015_usb_driver);
1622}
1623
1624module_init(af9015_usb_module_init);
1625module_exit(af9015_usb_module_exit);
1626
1627MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1628MODULE_DESCRIPTION("Driver for Afatech AF9015 DVB-T");
1629MODULE_LICENSE("GPL");