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