blob: bf8075c7793017c0e8abb404659fdedff5e1e224 [file] [log] [blame]
Antti Palosaari80619de2008-09-15 17:18:09 -03001/*
2 * DVB USB Linux driver for Afatech AF9015 DVB-T USB2.0 receiver
3 *
4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
5 *
6 * Thanks to Afatech who kindly provided information.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 */
23
Jiri Slaby6c614042010-01-22 12:10:52 -030024#include <linux/hash.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Jiri Slaby6c614042010-01-22 12:10:52 -030026
Antti Palosaari80619de2008-09-15 17:18:09 -030027#include "af9015.h"
28#include "af9013.h"
29#include "mt2060.h"
30#include "qt1010.h"
31#include "tda18271.h"
32#include "mxl5005s.h"
Jochen Friedrichd5633992009-02-02 14:59:50 -030033#include "mc44s803.h"
Antti Palosaariee3d4402010-08-13 03:51:26 -030034#include "tda18218.h"
Antti Palosaariab07fdd2010-09-09 14:59:10 -030035#include "mxl5007t.h"
Antti Palosaari80619de2008-09-15 17:18:09 -030036
Antti Palosaari349d0422008-11-05 16:31:24 -030037static int dvb_usb_af9015_debug;
Antti Palosaari80619de2008-09-15 17:18:09 -030038module_param_named(debug, dvb_usb_af9015_debug, int, 0644);
39MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
Antti Palosaari349d0422008-11-05 16:31:24 -030040static int dvb_usb_af9015_remote;
Antti Palosaari80619de2008-09-15 17:18:09 -030041module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
42MODULE_PARM_DESC(remote, "select remote");
Antti Palosaari80619de2008-09-15 17:18:09 -030043DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
44
45static DEFINE_MUTEX(af9015_usb_mutex);
46
47static struct af9015_config af9015_config;
Antti Palosaari85d7d7c2009-04-09 09:16:12 -030048static struct dvb_usb_device_properties af9015_properties[3];
Antti Palosaari349d0422008-11-05 16:31:24 -030049static int af9015_properties_count = ARRAY_SIZE(af9015_properties);
Antti Palosaari80619de2008-09-15 17:18:09 -030050
51static struct af9013_config af9015_af9013_config[] = {
52 {
53 .demod_address = AF9015_I2C_DEMOD,
54 .output_mode = AF9013_OUTPUT_MODE_USB,
55 .api_version = { 0, 1, 9, 0 },
56 .gpio[0] = AF9013_GPIO_HI,
Antti Palosaari80619de2008-09-15 17:18:09 -030057 .gpio[3] = AF9013_GPIO_TUNER_ON,
58
59 }, {
60 .output_mode = AF9013_OUTPUT_MODE_SERIAL,
61 .api_version = { 0, 1, 9, 0 },
62 .gpio[0] = AF9013_GPIO_TUNER_ON,
63 .gpio[1] = AF9013_GPIO_LO,
64 }
65};
66
67static int af9015_rw_udev(struct usb_device *udev, struct req_t *req)
68{
Antti Palosaari06565d72009-09-12 20:46:30 -030069#define BUF_LEN 63
70#define REQ_HDR_LEN 8 /* send header size */
71#define ACK_HDR_LEN 2 /* rece header size */
Antti Palosaari80619de2008-09-15 17:18:09 -030072 int act_len, ret;
Antti Palosaari06565d72009-09-12 20:46:30 -030073 u8 buf[BUF_LEN];
Antti Palosaari80619de2008-09-15 17:18:09 -030074 u8 write = 1;
Antti Palosaari06565d72009-09-12 20:46:30 -030075 u8 msg_len = REQ_HDR_LEN;
Antti Palosaari80619de2008-09-15 17:18:09 -030076 static u8 seq; /* packet sequence number */
77
78 if (mutex_lock_interruptible(&af9015_usb_mutex) < 0)
79 return -EAGAIN;
80
81 buf[0] = req->cmd;
82 buf[1] = seq++;
83 buf[2] = req->i2c_addr;
84 buf[3] = req->addr >> 8;
85 buf[4] = req->addr & 0xff;
86 buf[5] = req->mbox;
87 buf[6] = req->addr_len;
88 buf[7] = req->data_len;
89
90 switch (req->cmd) {
91 case GET_CONFIG:
Antti Palosaari80619de2008-09-15 17:18:09 -030092 case READ_MEMORY:
93 case RECONNECT_USB:
94 case GET_IR_CODE:
95 write = 0;
96 break;
97 case READ_I2C:
98 write = 0;
99 buf[2] |= 0x01; /* set I2C direction */
100 case WRITE_I2C:
101 buf[0] = READ_WRITE_I2C;
102 break;
103 case WRITE_MEMORY:
104 if (((req->addr & 0xff00) == 0xff00) ||
Antti Palosaarif4e96de2009-09-12 21:25:59 -0300105 ((req->addr & 0xff00) == 0xae00))
Antti Palosaari80619de2008-09-15 17:18:09 -0300106 buf[0] = WRITE_VIRTUAL_MEMORY;
107 case WRITE_VIRTUAL_MEMORY:
108 case COPY_FIRMWARE:
109 case DOWNLOAD_FIRMWARE:
Nils Kassubeba1bc642009-07-28 11:54:52 -0300110 case BOOT:
Antti Palosaari80619de2008-09-15 17:18:09 -0300111 break;
112 default:
113 err("unknown command:%d", req->cmd);
114 ret = -1;
115 goto error_unlock;
116 }
117
Antti Palosaari06565d72009-09-12 20:46:30 -0300118 /* buffer overflow check */
119 if ((write && (req->data_len > BUF_LEN - REQ_HDR_LEN)) ||
120 (!write && (req->data_len > BUF_LEN - ACK_HDR_LEN))) {
121 err("too much data; cmd:%d len:%d", req->cmd, req->data_len);
122 ret = -EINVAL;
123 goto error_unlock;
124 }
125
Antti Palosaari80619de2008-09-15 17:18:09 -0300126 /* write requested */
127 if (write) {
Antti Palosaari06565d72009-09-12 20:46:30 -0300128 memcpy(&buf[REQ_HDR_LEN], req->data, req->data_len);
Antti Palosaari80619de2008-09-15 17:18:09 -0300129 msg_len += req->data_len;
130 }
Antti Palosaari06565d72009-09-12 20:46:30 -0300131
Antti Palosaari80619de2008-09-15 17:18:09 -0300132 deb_xfer(">>> ");
133 debug_dump(buf, msg_len, deb_xfer);
134
135 /* send req */
136 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
Antti Palosaari06565d72009-09-12 20:46:30 -0300137 &act_len, AF9015_USB_TIMEOUT);
Antti Palosaari80619de2008-09-15 17:18:09 -0300138 if (ret)
139 err("bulk message failed:%d (%d/%d)", ret, msg_len, act_len);
140 else
141 if (act_len != msg_len)
142 ret = -1; /* all data is not send */
143 if (ret)
144 goto error_unlock;
145
146 /* no ack for those packets */
147 if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
148 goto exit_unlock;
149
Antti Palosaari06565d72009-09-12 20:46:30 -0300150 /* write receives seq + status = 2 bytes
151 read receives seq + status + data = 2 + N bytes */
152 msg_len = ACK_HDR_LEN;
153 if (!write)
154 msg_len += req->data_len;
155
Antti Palosaari80619de2008-09-15 17:18:09 -0300156 ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
Antti Palosaari06565d72009-09-12 20:46:30 -0300157 &act_len, AF9015_USB_TIMEOUT);
Antti Palosaari80619de2008-09-15 17:18:09 -0300158 if (ret) {
159 err("recv bulk message failed:%d", ret);
160 ret = -1;
161 goto error_unlock;
162 }
163
164 deb_xfer("<<< ");
165 debug_dump(buf, act_len, deb_xfer);
166
167 /* remote controller query status is 1 if remote code is not received */
168 if (req->cmd == GET_IR_CODE && buf[1] == 1) {
169 buf[1] = 0; /* clear command "error" status */
170 memset(&buf[2], 0, req->data_len);
171 buf[3] = 1; /* no remote code received mark */
172 }
173
174 /* check status */
175 if (buf[1]) {
176 err("command failed:%d", buf[1]);
177 ret = -1;
178 goto error_unlock;
179 }
180
181 /* read request, copy returned data to return buf */
182 if (!write)
Antti Palosaari06565d72009-09-12 20:46:30 -0300183 memcpy(req->data, &buf[ACK_HDR_LEN], req->data_len);
Antti Palosaari80619de2008-09-15 17:18:09 -0300184
185error_unlock:
186exit_unlock:
187 mutex_unlock(&af9015_usb_mutex);
188
189 return ret;
190}
191
192static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
193{
194 return af9015_rw_udev(d->udev, req);
195}
196
197static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
198 u8 len)
199{
200 struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
201 val};
202 return af9015_ctrl_msg(d, &req);
203}
204
205static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
206{
207 return af9015_write_regs(d, addr, &val, 1);
208}
209
210static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
211{
212 struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, 1, val};
213 return af9015_ctrl_msg(d, &req);
214}
215
216static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
217 u8 val)
218{
219 struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
220
221 if (addr == af9015_af9013_config[0].demod_address ||
222 addr == af9015_af9013_config[1].demod_address)
223 req.addr_len = 3;
224
225 return af9015_ctrl_msg(d, &req);
226}
227
228static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
229 u8 *val)
230{
231 struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
232
233 if (addr == af9015_af9013_config[0].demod_address ||
234 addr == af9015_af9013_config[1].demod_address)
235 req.addr_len = 3;
236
237 return af9015_ctrl_msg(d, &req);
238}
239
240static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
241 int num)
242{
243 struct dvb_usb_device *d = i2c_get_adapdata(adap);
244 int ret = 0, i = 0;
245 u16 addr;
246 u8 mbox, addr_len;
247 struct req_t req;
248
249/* TODO: implement bus lock
250
251The bus lock is needed because there is two tuners both using same I2C-address.
252Due to that the only way to select correct tuner is use demodulator I2C-gate.
253
254................................................
255. AF9015 includes integrated AF9013 demodulator.
256. ____________ ____________ . ____________
257.| uC | | demod | . | tuner |
258.|------------| |------------| . |------------|
259.| AF9015 | | AF9013/5 | . | MXL5003 |
260.| |--+----I2C-------|-----/ -----|-.-----I2C-------| |
261.| | | | addr 0x38 | . | addr 0xc6 |
262.|____________| | |____________| . |____________|
263.................|..............................
264 | ____________ ____________
265 | | demod | | tuner |
266 | |------------| |------------|
267 | | AF9013 | | MXL5003 |
268 +----I2C-------|-----/ -----|-------I2C-------| |
269 | addr 0x3a | | addr 0xc6 |
270 |____________| |____________|
271*/
272 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
273 return -EAGAIN;
274
275 while (i < num) {
276 if (msg[i].addr == af9015_af9013_config[0].demod_address ||
277 msg[i].addr == af9015_af9013_config[1].demod_address) {
278 addr = msg[i].buf[0] << 8;
279 addr += msg[i].buf[1];
280 mbox = msg[i].buf[2];
281 addr_len = 3;
282 } else {
283 addr = msg[i].buf[0];
284 addr_len = 1;
285 mbox = 0;
286 }
287
288 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
289 if (msg[i].addr ==
290 af9015_af9013_config[0].demod_address)
291 req.cmd = READ_MEMORY;
292 else
293 req.cmd = READ_I2C;
294 req.i2c_addr = msg[i].addr;
295 req.addr = addr;
296 req.mbox = mbox;
297 req.addr_len = addr_len;
298 req.data_len = msg[i+1].len;
299 req.data = &msg[i+1].buf[0];
300 ret = af9015_ctrl_msg(d, &req);
301 i += 2;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300302 } else if (msg[i].flags & I2C_M_RD) {
303 ret = -EINVAL;
304 if (msg[i].addr ==
305 af9015_af9013_config[0].demod_address)
306 goto error;
307 else
308 req.cmd = READ_I2C;
309 req.i2c_addr = msg[i].addr;
310 req.addr = addr;
311 req.mbox = mbox;
312 req.addr_len = addr_len;
313 req.data_len = msg[i].len;
314 req.data = &msg[i].buf[0];
315 ret = af9015_ctrl_msg(d, &req);
316 i += 1;
Antti Palosaari80619de2008-09-15 17:18:09 -0300317 } else {
318 if (msg[i].addr ==
319 af9015_af9013_config[0].demod_address)
320 req.cmd = WRITE_MEMORY;
321 else
322 req.cmd = WRITE_I2C;
323 req.i2c_addr = msg[i].addr;
324 req.addr = addr;
325 req.mbox = mbox;
326 req.addr_len = addr_len;
327 req.data_len = msg[i].len-addr_len;
328 req.data = &msg[i].buf[addr_len];
329 ret = af9015_ctrl_msg(d, &req);
330 i += 1;
331 }
332 if (ret)
333 goto error;
334
335 }
336 ret = i;
337
338error:
339 mutex_unlock(&d->i2c_mutex);
340
341 return ret;
342}
343
344static u32 af9015_i2c_func(struct i2c_adapter *adapter)
345{
346 return I2C_FUNC_I2C;
347}
348
349static struct i2c_algorithm af9015_i2c_algo = {
350 .master_xfer = af9015_i2c_xfer,
351 .functionality = af9015_i2c_func,
352};
353
354static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
355{
356 int ret;
357 u8 val, mask = 0x01;
358
359 ret = af9015_read_reg(d, addr, &val);
360 if (ret)
361 return ret;
362
363 mask <<= bit;
364 if (op) {
365 /* set bit */
366 val |= mask;
367 } else {
368 /* clear bit */
369 mask ^= 0xff;
370 val &= mask;
371 }
372
373 return af9015_write_reg(d, addr, val);
374}
375
376static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
377{
378 return af9015_do_reg_bit(d, addr, bit, 1);
379}
380
381static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
382{
383 return af9015_do_reg_bit(d, addr, bit, 0);
384}
385
386static int af9015_init_endpoint(struct dvb_usb_device *d)
387{
388 int ret;
389 u16 frame_size;
390 u8 packet_size;
391 deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);
392
Antti Palosaari9c863272009-09-12 13:35:29 -0300393 /* Windows driver uses packet count 21 for USB1.1 and 348 for USB2.0.
394 We use smaller - about 1/4 from the original, 5 and 87. */
Antti Palosaari80619de2008-09-15 17:18:09 -0300395#define TS_PACKET_SIZE 188
396
Antti Palosaari9c863272009-09-12 13:35:29 -0300397#define TS_USB20_PACKET_COUNT 87
Antti Palosaari80619de2008-09-15 17:18:09 -0300398#define TS_USB20_FRAME_SIZE (TS_PACKET_SIZE*TS_USB20_PACKET_COUNT)
399
Antti Palosaari9c863272009-09-12 13:35:29 -0300400#define TS_USB11_PACKET_COUNT 5
Antti Palosaari80619de2008-09-15 17:18:09 -0300401#define TS_USB11_FRAME_SIZE (TS_PACKET_SIZE*TS_USB11_PACKET_COUNT)
402
403#define TS_USB20_MAX_PACKET_SIZE 512
404#define TS_USB11_MAX_PACKET_SIZE 64
405
406 if (d->udev->speed == USB_SPEED_FULL) {
407 frame_size = TS_USB11_FRAME_SIZE/4;
408 packet_size = TS_USB11_MAX_PACKET_SIZE/4;
409 } else {
410 frame_size = TS_USB20_FRAME_SIZE/4;
411 packet_size = TS_USB20_MAX_PACKET_SIZE/4;
412 }
413
414 ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
415 if (ret)
416 goto error;
417 ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
418 if (ret)
419 goto error;
420 ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
421 if (ret)
422 goto error;
423 ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
424 if (ret)
425 goto error;
426 ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
427 if (ret)
428 goto error;
429 if (af9015_config.dual_mode) {
430 ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
431 if (ret)
432 goto error;
433 }
434 ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
435 if (ret)
436 goto error;
437 if (af9015_config.dual_mode) {
438 ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
439 if (ret)
440 goto error;
441 }
442 /* EP4 xfer length */
443 ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
444 if (ret)
445 goto error;
446 ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
447 if (ret)
448 goto error;
449 /* EP5 xfer length */
450 ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
451 if (ret)
452 goto error;
453 ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
454 if (ret)
455 goto error;
456 ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
457 if (ret)
458 goto error;
459 ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
460 if (ret)
461 goto error;
462 ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
463 if (ret)
464 goto error;
465 if (af9015_config.dual_mode) {
466 ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
467 if (ret)
468 goto error;
469 }
470
471 /* enable / disable mp2if2 */
472 if (af9015_config.dual_mode)
473 ret = af9015_set_reg_bit(d, 0xd50b, 0);
474 else
475 ret = af9015_clear_reg_bit(d, 0xd50b, 0);
476error:
477 if (ret)
478 err("endpoint init failed:%d", ret);
479 return ret;
480}
481
482static int af9015_copy_firmware(struct dvb_usb_device *d)
483{
484 int ret;
485 u8 fw_params[4];
486 u8 val, i;
487 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
488 fw_params };
489 deb_info("%s:\n", __func__);
490
491 fw_params[0] = af9015_config.firmware_size >> 8;
492 fw_params[1] = af9015_config.firmware_size & 0xff;
493 fw_params[2] = af9015_config.firmware_checksum >> 8;
494 fw_params[3] = af9015_config.firmware_checksum & 0xff;
495
496 /* wait 2nd demodulator ready */
497 msleep(100);
498
499 ret = af9015_read_reg_i2c(d, 0x3a, 0x98be, &val);
500 if (ret)
501 goto error;
502 else
503 deb_info("%s: firmware status:%02x\n", __func__, val);
504
505 if (val == 0x0c) /* fw is running, no need for download */
506 goto exit;
507
508 /* set I2C master clock to fast (to speed up firmware copy) */
509 ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
510 if (ret)
511 goto error;
512
513 msleep(50);
514
515 /* copy firmware */
516 ret = af9015_ctrl_msg(d, &req);
517 if (ret)
518 err("firmware copy cmd failed:%d", ret);
519 deb_info("%s: firmware copy done\n", __func__);
520
521 /* set I2C master clock back to normal */
522 ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
523 if (ret)
524 goto error;
525
526 /* request boot firmware */
527 ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].demod_address,
528 0xe205, 1);
529 deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
530 if (ret)
531 goto error;
532
533 for (i = 0; i < 15; i++) {
534 msleep(100);
535
536 /* check firmware status */
537 ret = af9015_read_reg_i2c(d,
538 af9015_af9013_config[1].demod_address, 0x98be, &val);
539 deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
540 __func__, ret, val);
541 if (ret)
542 goto error;
543
544 if (val == 0x0c || val == 0x04) /* success or fail */
545 break;
546 }
547
548 if (val == 0x04) {
549 err("firmware did not run");
550 ret = -1;
551 } else if (val != 0x0c) {
552 err("firmware boot timeout");
553 ret = -1;
554 }
555
556error:
557exit:
558 return ret;
559}
560
Jiri Slaby6c614042010-01-22 12:10:52 -0300561/* hash (and dump) eeprom */
562static int af9015_eeprom_hash(struct usb_device *udev)
Antti Palosaari80619de2008-09-15 17:18:09 -0300563{
Jiri Slaby6c614042010-01-22 12:10:52 -0300564 static const unsigned int eeprom_size = 256;
565 unsigned int reg;
566 int ret;
567 u8 val, *eeprom;
568 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
Antti Palosaari80619de2008-09-15 17:18:09 -0300569
Jiri Slaby6c614042010-01-22 12:10:52 -0300570 eeprom = kmalloc(eeprom_size, GFP_KERNEL);
571 if (eeprom == NULL)
572 return -ENOMEM;
573
574 for (reg = 0; reg < eeprom_size; reg++) {
575 req.addr = reg;
576 ret = af9015_rw_udev(udev, &req);
577 if (ret)
578 goto free;
579 eeprom[reg] = val;
Antti Palosaari80619de2008-09-15 17:18:09 -0300580 }
Jiri Slaby6c614042010-01-22 12:10:52 -0300581
582 if (dvb_usb_af9015_debug & 0x01)
583 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, eeprom,
584 eeprom_size);
585
586 BUG_ON(eeprom_size % 4);
587
588 af9015_config.eeprom_sum = 0;
589 for (reg = 0; reg < eeprom_size / sizeof(u32); reg++) {
590 af9015_config.eeprom_sum *= GOLDEN_RATIO_PRIME_32;
591 af9015_config.eeprom_sum += le32_to_cpu(((u32 *)eeprom)[reg]);
592 }
593
594 deb_info("%s: eeprom sum=%.8x\n", __func__, af9015_config.eeprom_sum);
595
596 ret = 0;
597free:
598 kfree(eeprom);
599 return ret;
Antti Palosaari80619de2008-09-15 17:18:09 -0300600}
601
Antti Palosaari349d0422008-11-05 16:31:24 -0300602static int af9015_download_ir_table(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -0300603{
604 int i, packets = 0, ret;
605 u16 addr = 0x9a56; /* ir-table start address */
606 struct req_t req = {WRITE_MEMORY, 0, 0, 0, 0, 1, NULL};
607 u8 *data = NULL;
608 deb_info("%s:\n", __func__);
609
610 data = af9015_config.ir_table;
611 packets = af9015_config.ir_table_size;
612
613 /* no remote */
614 if (!packets)
615 goto exit;
616
617 /* load remote ir-table */
618 for (i = 0; i < packets; i++) {
619 req.addr = addr + i;
620 req.data = &data[i];
621 ret = af9015_ctrl_msg(d, &req);
622 if (ret) {
623 err("ir-table download failed at packet %d with " \
624 "code %d", i, ret);
625 return ret;
626 }
627 }
628
629exit:
630 return 0;
631}
632
633static int af9015_init(struct dvb_usb_device *d)
634{
635 int ret;
636 deb_info("%s:\n", __func__);
637
638 ret = af9015_init_endpoint(d);
639 if (ret)
640 goto error;
641
642 ret = af9015_download_ir_table(d);
643 if (ret)
644 goto error;
645
646error:
647 return ret;
648}
649
650static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
651{
652 int ret;
653 deb_info("%s: onoff:%d\n", __func__, onoff);
654
655 if (onoff)
656 ret = af9015_set_reg_bit(adap->dev, 0xd503, 0);
657 else
658 ret = af9015_clear_reg_bit(adap->dev, 0xd503, 0);
659
660 return ret;
661}
662
663static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
664 int onoff)
665{
666 int ret;
667 u8 idx;
668
669 deb_info("%s: set pid filter, index %d, pid %x, onoff %d\n",
670 __func__, index, pid, onoff);
671
672 ret = af9015_write_reg(adap->dev, 0xd505, (pid & 0xff));
673 if (ret)
674 goto error;
675
676 ret = af9015_write_reg(adap->dev, 0xd506, (pid >> 8));
677 if (ret)
678 goto error;
679
680 idx = ((index & 0x1f) | (1 << 5));
681 ret = af9015_write_reg(adap->dev, 0xd504, idx);
682
683error:
684 return ret;
685}
686
687static int af9015_download_firmware(struct usb_device *udev,
688 const struct firmware *fw)
689{
690 int i, len, packets, remainder, ret;
691 struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
692 u16 addr = 0x5100; /* firmware start address */
693 u16 checksum = 0;
694
695 deb_info("%s:\n", __func__);
696
697 /* calc checksum */
698 for (i = 0; i < fw->size; i++)
699 checksum += fw->data[i];
700
701 af9015_config.firmware_size = fw->size;
702 af9015_config.firmware_checksum = checksum;
703
704 #define FW_PACKET_MAX_DATA 55
705
706 packets = fw->size / FW_PACKET_MAX_DATA;
707 remainder = fw->size % FW_PACKET_MAX_DATA;
708 len = FW_PACKET_MAX_DATA;
709 for (i = 0; i <= packets; i++) {
710 if (i == packets) /* set size of the last packet */
711 len = remainder;
712
713 req.data_len = len;
Antti Palosaari541dfa82008-10-06 13:57:45 -0300714 req.data = (u8 *)(fw->data + i * FW_PACKET_MAX_DATA);
Antti Palosaari80619de2008-09-15 17:18:09 -0300715 req.addr = addr;
716 addr += FW_PACKET_MAX_DATA;
717
718 ret = af9015_rw_udev(udev, &req);
719 if (ret) {
720 err("firmware download failed at packet %d with " \
721 "code %d", i, ret);
722 goto error;
723 }
724 }
Antti Palosaari80619de2008-09-15 17:18:09 -0300725
726 /* firmware loaded, request boot */
727 req.cmd = BOOT;
728 ret = af9015_rw_udev(udev, &req);
729 if (ret) {
730 err("firmware boot failed:%d", ret);
731 goto error;
732 }
733
Antti Palosaari80619de2008-09-15 17:18:09 -0300734error:
735 return ret;
736}
737
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300738struct af9015_setup {
739 unsigned int id;
Mauro Carvalho Chehab34abf212010-07-31 11:24:57 -0300740 struct ir_scancode *rc_key_map;
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300741 unsigned int rc_key_map_size;
742 u8 *ir_table;
743 unsigned int ir_table_size;
744};
745
746static const struct af9015_setup *af9015_setup_match(unsigned int id,
747 const struct af9015_setup *table)
748{
749 for (; table->rc_key_map; table++)
750 if (table->id == id)
751 return table;
752 return NULL;
753}
754
755static const struct af9015_setup af9015_setup_modparam[] = {
756 { AF9015_REMOTE_A_LINK_DTU_M,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300757 ir_codes_af9015_table_a_link, ARRAY_SIZE(ir_codes_af9015_table_a_link),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300758 af9015_ir_table_a_link, ARRAY_SIZE(af9015_ir_table_a_link) },
759 { AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300760 ir_codes_af9015_table_msi, ARRAY_SIZE(ir_codes_af9015_table_msi),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300761 af9015_ir_table_msi, ARRAY_SIZE(af9015_ir_table_msi) },
762 { AF9015_REMOTE_MYGICTV_U718,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300763 ir_codes_af9015_table_mygictv, ARRAY_SIZE(ir_codes_af9015_table_mygictv),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300764 af9015_ir_table_mygictv, ARRAY_SIZE(af9015_ir_table_mygictv) },
765 { AF9015_REMOTE_DIGITTRADE_DVB_T,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300766 ir_codes_af9015_table_digittrade, ARRAY_SIZE(ir_codes_af9015_table_digittrade),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300767 af9015_ir_table_digittrade, ARRAY_SIZE(af9015_ir_table_digittrade) },
768 { AF9015_REMOTE_AVERMEDIA_KS,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300769 ir_codes_af9015_table_avermedia, ARRAY_SIZE(ir_codes_af9015_table_avermedia),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300770 af9015_ir_table_avermedia_ks, ARRAY_SIZE(af9015_ir_table_avermedia_ks) },
771 { }
772};
773
774/* don't add new entries here anymore, use hashes instead */
775static const struct af9015_setup af9015_setup_usbids[] = {
776 { USB_VID_LEADTEK,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300777 ir_codes_af9015_table_leadtek, ARRAY_SIZE(ir_codes_af9015_table_leadtek),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300778 af9015_ir_table_leadtek, ARRAY_SIZE(af9015_ir_table_leadtek) },
779 { USB_VID_VISIONPLUS,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300780 ir_codes_af9015_table_twinhan, ARRAY_SIZE(ir_codes_af9015_table_twinhan),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300781 af9015_ir_table_twinhan, ARRAY_SIZE(af9015_ir_table_twinhan) },
782 { USB_VID_KWORLD_2, /* TODO: use correct rc keys */
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300783 ir_codes_af9015_table_twinhan, ARRAY_SIZE(ir_codes_af9015_table_twinhan),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300784 af9015_ir_table_kworld, ARRAY_SIZE(af9015_ir_table_kworld) },
785 { USB_VID_AVERMEDIA,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300786 ir_codes_af9015_table_avermedia, ARRAY_SIZE(ir_codes_af9015_table_avermedia),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300787 af9015_ir_table_avermedia, ARRAY_SIZE(af9015_ir_table_avermedia) },
788 { USB_VID_MSI_2,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300789 ir_codes_af9015_table_msi_digivox_iii, ARRAY_SIZE(ir_codes_af9015_table_msi_digivox_iii),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300790 af9015_ir_table_msi_digivox_iii, ARRAY_SIZE(af9015_ir_table_msi_digivox_iii) },
791 { }
792};
793
Jiri Slabye3a0cc62010-01-22 12:10:55 -0300794static const struct af9015_setup af9015_setup_hashes[] = {
795 { 0xb8feb708,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300796 ir_codes_af9015_table_msi, ARRAY_SIZE(ir_codes_af9015_table_msi),
Jiri Slabye3a0cc62010-01-22 12:10:55 -0300797 af9015_ir_table_msi, ARRAY_SIZE(af9015_ir_table_msi) },
Antti Palosaaridb02d9d2010-02-10 20:20:41 -0300798 { 0xa3703d00,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300799 ir_codes_af9015_table_a_link, ARRAY_SIZE(ir_codes_af9015_table_a_link),
Antti Palosaaridb02d9d2010-02-10 20:20:41 -0300800 af9015_ir_table_a_link, ARRAY_SIZE(af9015_ir_table_a_link) },
Antti Palosaari58c811df2010-02-10 20:44:12 -0300801 { 0x9b7dc64e,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300802 ir_codes_af9015_table_mygictv, ARRAY_SIZE(ir_codes_af9015_table_mygictv),
Antti Palosaari58c811df2010-02-10 20:44:12 -0300803 af9015_ir_table_mygictv, ARRAY_SIZE(af9015_ir_table_mygictv) },
Jiri Slabye3a0cc62010-01-22 12:10:55 -0300804 { }
805};
806
Jiri Slaby634d2d72010-01-22 12:10:53 -0300807static void af9015_set_remote_config(struct usb_device *udev,
808 struct dvb_usb_device_properties *props)
809{
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300810 const struct af9015_setup *table = NULL;
811
Jiri Slaby634d2d72010-01-22 12:10:53 -0300812 if (dvb_usb_af9015_remote) {
813 /* load remote defined as module param */
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300814 table = af9015_setup_match(dvb_usb_af9015_remote,
815 af9015_setup_modparam);
Jiri Slaby634d2d72010-01-22 12:10:53 -0300816 } else {
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300817 u16 vendor = le16_to_cpu(udev->descriptor.idVendor);
818
Jiri Slabye3a0cc62010-01-22 12:10:55 -0300819 table = af9015_setup_match(af9015_config.eeprom_sum,
820 af9015_setup_hashes);
821
822 if (!table && vendor == USB_VID_AFATECH) {
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300823 /* Check USB manufacturer and product strings and try
824 to determine correct remote in case of chip vendor
825 reference IDs are used.
826 DO NOT ADD ANYTHING NEW HERE. Use hashes instead.
827 */
Jiri Slaby634d2d72010-01-22 12:10:53 -0300828 char manufacturer[10];
829 memset(manufacturer, 0, sizeof(manufacturer));
830 usb_string(udev, udev->descriptor.iManufacturer,
831 manufacturer, sizeof(manufacturer));
Antti Palosaari58c811df2010-02-10 20:44:12 -0300832 if (!strcmp("MSI", manufacturer)) {
Jiri Slaby634d2d72010-01-22 12:10:53 -0300833 /* iManufacturer 1 MSI
834 iProduct 2 MSI K-VOX */
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300835 table = af9015_setup_match(
836 AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
837 af9015_setup_modparam);
Jiri Slaby634d2d72010-01-22 12:10:53 -0300838 } else if (udev->descriptor.idProduct ==
839 cpu_to_le16(USB_PID_TREKSTOR_DVBT)) {
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300840 table = &(const struct af9015_setup){ 0,
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300841 ir_codes_af9015_table_trekstor,
842 ARRAY_SIZE(ir_codes_af9015_table_trekstor),
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300843 af9015_ir_table_trekstor,
844 ARRAY_SIZE(af9015_ir_table_trekstor)
845 };
Jiri Slaby634d2d72010-01-22 12:10:53 -0300846 }
Jiri Slabye3a0cc62010-01-22 12:10:55 -0300847 } else if (!table)
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300848 table = af9015_setup_match(vendor, af9015_setup_usbids);
849 }
850
851 if (table) {
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -0300852 props->rc.legacy.rc_key_map = table->rc_key_map;
853 props->rc.legacy.rc_key_map_size = table->rc_key_map_size;
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300854 af9015_config.ir_table = table->ir_table;
855 af9015_config.ir_table_size = table->ir_table_size;
Jiri Slaby634d2d72010-01-22 12:10:53 -0300856 }
857}
858
Antti Palosaari80619de2008-09-15 17:18:09 -0300859static int af9015_read_config(struct usb_device *udev)
860{
861 int ret;
862 u8 val, i, offset = 0;
863 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
Antti Palosaari80619de2008-09-15 17:18:09 -0300864
865 /* IR remote controller */
866 req.addr = AF9015_EEPROM_IR_MODE;
Antti Palosaarid1a470f2009-01-20 14:56:20 -0300867 /* first message will timeout often due to possible hw bug */
868 for (i = 0; i < 4; i++) {
869 ret = af9015_rw_udev(udev, &req);
870 if (!ret)
871 break;
872 }
Antti Palosaari80619de2008-09-15 17:18:09 -0300873 if (ret)
874 goto error;
Jiri Slaby6c614042010-01-22 12:10:52 -0300875
876 ret = af9015_eeprom_hash(udev);
877 if (ret)
878 goto error;
879
Antti Palosaari80619de2008-09-15 17:18:09 -0300880 deb_info("%s: IR mode:%d\n", __func__, val);
881 for (i = 0; i < af9015_properties_count; i++) {
Antti Palosaari0f017212009-09-21 21:26:37 -0300882 if (val == AF9015_IR_MODE_DISABLED) {
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -0300883 af9015_properties[i].rc.legacy.rc_key_map = NULL;
884 af9015_properties[i].rc.legacy.rc_key_map_size = 0;
Jiri Slaby634d2d72010-01-22 12:10:53 -0300885 } else
886 af9015_set_remote_config(udev, &af9015_properties[i]);
Antti Palosaari80619de2008-09-15 17:18:09 -0300887 }
888
889 /* TS mode - one or two receivers */
890 req.addr = AF9015_EEPROM_TS_MODE;
891 ret = af9015_rw_udev(udev, &req);
892 if (ret)
893 goto error;
894 af9015_config.dual_mode = val;
895 deb_info("%s: TS mode:%d\n", __func__, af9015_config.dual_mode);
Antti Palosaari80619de2008-09-15 17:18:09 -0300896
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300897 /* Set adapter0 buffer size according to USB port speed, adapter1 buffer
898 size can be static because it is enabled only USB2.0 */
Antti Palosaari80619de2008-09-15 17:18:09 -0300899 for (i = 0; i < af9015_properties_count; i++) {
900 /* USB1.1 set smaller buffersize and disable 2nd adapter */
901 if (udev->speed == USB_SPEED_FULL) {
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300902 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
Antti Palosaari9c863272009-09-12 13:35:29 -0300903 = TS_USB11_FRAME_SIZE;
Antti Palosaari80619de2008-09-15 17:18:09 -0300904 /* disable 2nd adapter because we don't have
905 PID-filters */
906 af9015_config.dual_mode = 0;
907 } else {
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300908 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
Jose Alberto Reguero353330c2009-09-12 09:51:36 -0300909 = TS_USB20_FRAME_SIZE;
Antti Palosaari80619de2008-09-15 17:18:09 -0300910 }
911 }
912
913 if (af9015_config.dual_mode) {
914 /* read 2nd demodulator I2C address */
915 req.addr = AF9015_EEPROM_DEMOD2_I2C;
916 ret = af9015_rw_udev(udev, &req);
917 if (ret)
918 goto error;
919 af9015_af9013_config[1].demod_address = val;
920
921 /* enable 2nd adapter */
922 for (i = 0; i < af9015_properties_count; i++)
923 af9015_properties[i].num_adapters = 2;
924
925 } else {
926 /* disable 2nd adapter */
927 for (i = 0; i < af9015_properties_count; i++)
928 af9015_properties[i].num_adapters = 1;
929 }
930
931 for (i = 0; i < af9015_properties[0].num_adapters; i++) {
932 if (i == 1)
933 offset = AF9015_EEPROM_OFFSET;
934 /* xtal */
935 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
936 ret = af9015_rw_udev(udev, &req);
937 if (ret)
938 goto error;
939 switch (val) {
940 case 0:
941 af9015_af9013_config[i].adc_clock = 28800;
942 break;
943 case 1:
944 af9015_af9013_config[i].adc_clock = 20480;
945 break;
946 case 2:
947 af9015_af9013_config[i].adc_clock = 28000;
948 break;
949 case 3:
950 af9015_af9013_config[i].adc_clock = 25000;
951 break;
952 };
953 deb_info("%s: [%d] xtal:%d set adc_clock:%d\n", __func__, i,
954 val, af9015_af9013_config[i].adc_clock);
955
956 /* tuner IF */
957 req.addr = AF9015_EEPROM_IF1H + offset;
958 ret = af9015_rw_udev(udev, &req);
959 if (ret)
960 goto error;
961 af9015_af9013_config[i].tuner_if = val << 8;
962 req.addr = AF9015_EEPROM_IF1L + offset;
963 ret = af9015_rw_udev(udev, &req);
964 if (ret)
965 goto error;
966 af9015_af9013_config[i].tuner_if += val;
967 deb_info("%s: [%d] IF1:%d\n", __func__, i,
968 af9015_af9013_config[0].tuner_if);
969
970 /* MT2060 IF1 */
971 req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
972 ret = af9015_rw_udev(udev, &req);
973 if (ret)
974 goto error;
975 af9015_config.mt2060_if1[i] = val << 8;
976 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
977 ret = af9015_rw_udev(udev, &req);
978 if (ret)
979 goto error;
980 af9015_config.mt2060_if1[i] += val;
981 deb_info("%s: [%d] MT2060 IF1:%d\n", __func__, i,
982 af9015_config.mt2060_if1[i]);
983
984 /* tuner */
985 req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
986 ret = af9015_rw_udev(udev, &req);
987 if (ret)
988 goto error;
989 switch (val) {
990 case AF9013_TUNER_ENV77H11D5:
991 case AF9013_TUNER_MT2060:
Antti Palosaari80619de2008-09-15 17:18:09 -0300992 case AF9013_TUNER_QT1010:
993 case AF9013_TUNER_UNKNOWN:
994 case AF9013_TUNER_MT2060_2:
995 case AF9013_TUNER_TDA18271:
996 case AF9013_TUNER_QT1010A:
Antti Palosaariee3d4402010-08-13 03:51:26 -0300997 case AF9013_TUNER_TDA18218:
Antti Palosaari80619de2008-09-15 17:18:09 -0300998 af9015_af9013_config[i].rf_spec_inv = 1;
999 break;
1000 case AF9013_TUNER_MXL5003D:
1001 case AF9013_TUNER_MXL5005D:
1002 case AF9013_TUNER_MXL5005R:
Antti Palosaariab07fdd2010-09-09 14:59:10 -03001003 case AF9013_TUNER_MXL5007T:
Antti Palosaari80619de2008-09-15 17:18:09 -03001004 af9015_af9013_config[i].rf_spec_inv = 0;
1005 break;
Jochen Friedrichd5633992009-02-02 14:59:50 -03001006 case AF9013_TUNER_MC44S803:
1007 af9015_af9013_config[i].gpio[1] = AF9013_GPIO_LO;
1008 af9015_af9013_config[i].rf_spec_inv = 1;
1009 break;
Antti Palosaari80619de2008-09-15 17:18:09 -03001010 default:
1011 warn("tuner id:%d not supported, please report!", val);
1012 return -ENODEV;
1013 };
1014
1015 af9015_af9013_config[i].tuner = val;
1016 deb_info("%s: [%d] tuner id:%d\n", __func__, i, val);
1017 }
1018
1019error:
1020 if (ret)
1021 err("eeprom read failed:%d", ret);
1022
Antti Palosaari3956fef2009-03-31 17:01:02 -03001023 /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
1024 content :-( Override some wrong values here. */
1025 if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
1026 le16_to_cpu(udev->descriptor.idProduct) == USB_PID_AVERMEDIA_A850) {
1027 deb_info("%s: AverMedia A850: overriding config\n", __func__);
1028 /* disable dual mode */
1029 af9015_config.dual_mode = 0;
1030 /* disable 2nd adapter */
1031 for (i = 0; i < af9015_properties_count; i++)
1032 af9015_properties[i].num_adapters = 1;
1033
1034 /* set correct IF */
1035 af9015_af9013_config[0].tuner_if = 4570;
1036 }
1037
Antti Palosaari80619de2008-09-15 17:18:09 -03001038 return ret;
1039}
1040
1041static int af9015_identify_state(struct usb_device *udev,
1042 struct dvb_usb_device_properties *props,
1043 struct dvb_usb_device_description **desc,
1044 int *cold)
1045{
1046 int ret;
1047 u8 reply;
1048 struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
1049
1050 ret = af9015_rw_udev(udev, &req);
1051 if (ret)
1052 return ret;
1053
1054 deb_info("%s: reply:%02x\n", __func__, reply);
1055 if (reply == 0x02)
1056 *cold = 0;
1057 else
1058 *cold = 1;
1059
1060 return ret;
1061}
1062
1063static int af9015_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
1064{
1065 u8 buf[8];
1066 struct req_t req = {GET_IR_CODE, 0, 0, 0, 0, sizeof(buf), buf};
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001067 struct ir_scancode *keymap = d->props.rc.legacy.rc_key_map;
Antti Palosaari80619de2008-09-15 17:18:09 -03001068 int i, ret;
1069
1070 memset(buf, 0, sizeof(buf));
1071
1072 ret = af9015_ctrl_msg(d, &req);
1073 if (ret)
1074 return ret;
1075
1076 *event = 0;
1077 *state = REMOTE_NO_KEY_PRESSED;
1078
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001079 for (i = 0; i < d->props.rc.legacy.rc_key_map_size; i++) {
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -03001080 if (!buf[1] && rc5_custom(&keymap[i]) == buf[0] &&
1081 rc5_data(&keymap[i]) == buf[2]) {
Mauro Carvalho Chehab34abf212010-07-31 11:24:57 -03001082 *event = keymap[i].keycode;
Antti Palosaari80619de2008-09-15 17:18:09 -03001083 *state = REMOTE_KEY_PRESSED;
1084 break;
1085 }
1086 }
1087 if (!buf[1])
1088 deb_rc("%s: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1089 __func__, buf[0], buf[1], buf[2], buf[3], buf[4],
1090 buf[5], buf[6], buf[7]);
1091
1092 return 0;
1093}
1094
1095/* init 2nd I2C adapter */
Antti Palosaari349d0422008-11-05 16:31:24 -03001096static int af9015_i2c_init(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -03001097{
1098 int ret;
1099 struct af9015_state *state = d->priv;
1100 deb_info("%s:\n", __func__);
1101
1102 strncpy(state->i2c_adap.name, d->desc->name,
1103 sizeof(state->i2c_adap.name));
1104#ifdef I2C_ADAP_CLASS_TV_DIGITAL
1105 state->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL,
1106#else
1107 state->i2c_adap.class = I2C_CLASS_TV_DIGITAL,
1108#endif
1109 state->i2c_adap.algo = d->props.i2c_algo;
1110 state->i2c_adap.algo_data = NULL;
1111 state->i2c_adap.dev.parent = &d->udev->dev;
1112
1113 i2c_set_adapdata(&state->i2c_adap, d);
1114
1115 ret = i2c_add_adapter(&state->i2c_adap);
1116 if (ret < 0)
1117 err("could not add i2c adapter");
1118
1119 return ret;
1120}
1121
1122static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
1123{
1124 int ret;
1125 struct af9015_state *state = adap->dev->priv;
1126 struct i2c_adapter *i2c_adap;
1127
1128 if (adap->id == 0) {
1129 /* select I2C adapter */
1130 i2c_adap = &adap->dev->i2c_adap;
1131
1132 deb_info("%s: init I2C\n", __func__);
1133 ret = af9015_i2c_init(adap->dev);
Antti Palosaari80619de2008-09-15 17:18:09 -03001134 } else {
1135 /* select I2C adapter */
1136 i2c_adap = &state->i2c_adap;
1137
1138 /* copy firmware to 2nd demodulator */
1139 if (af9015_config.dual_mode) {
1140 ret = af9015_copy_firmware(adap->dev);
1141 if (ret) {
1142 err("firmware copy to 2nd frontend " \
1143 "failed, will disable it");
1144 af9015_config.dual_mode = 0;
1145 return -ENODEV;
1146 }
1147 } else {
1148 return -ENODEV;
1149 }
1150 }
1151
1152 /* attach demodulator */
1153 adap->fe = dvb_attach(af9013_attach, &af9015_af9013_config[adap->id],
1154 i2c_adap);
1155
1156 return adap->fe == NULL ? -ENODEV : 0;
1157}
1158
1159static struct mt2060_config af9015_mt2060_config = {
1160 .i2c_address = 0xc0,
1161 .clock_out = 0,
1162};
1163
1164static struct qt1010_config af9015_qt1010_config = {
1165 .i2c_address = 0xc4,
1166};
1167
1168static struct tda18271_config af9015_tda18271_config = {
1169 .gate = TDA18271_GATE_DIGITAL,
1170 .small_i2c = 1,
1171};
1172
1173static struct mxl5005s_config af9015_mxl5003_config = {
1174 .i2c_address = 0xc6,
1175 .if_freq = IF_FREQ_4570000HZ,
1176 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1177 .agc_mode = MXL_SINGLE_AGC,
1178 .tracking_filter = MXL_TF_DEFAULT,
Antti Palosaaria1310772008-09-22 13:59:25 -03001179 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -03001180 .cap_select = MXL_CAP_SEL_ENABLE,
1181 .div_out = MXL_DIV_OUT_4,
1182 .clock_out = MXL_CLOCK_OUT_DISABLE,
1183 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1184 .top = MXL5005S_TOP_25P2,
1185 .mod_mode = MXL_DIGITAL_MODE,
1186 .if_mode = MXL_ZERO_IF,
1187 .AgcMasterByte = 0x00,
1188};
1189
1190static struct mxl5005s_config af9015_mxl5005_config = {
1191 .i2c_address = 0xc6,
1192 .if_freq = IF_FREQ_4570000HZ,
1193 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1194 .agc_mode = MXL_SINGLE_AGC,
1195 .tracking_filter = MXL_TF_OFF,
Antti Palosaaria1310772008-09-22 13:59:25 -03001196 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -03001197 .cap_select = MXL_CAP_SEL_ENABLE,
1198 .div_out = MXL_DIV_OUT_4,
1199 .clock_out = MXL_CLOCK_OUT_DISABLE,
1200 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1201 .top = MXL5005S_TOP_25P2,
1202 .mod_mode = MXL_DIGITAL_MODE,
1203 .if_mode = MXL_ZERO_IF,
1204 .AgcMasterByte = 0x00,
1205};
1206
Jochen Friedrichd5633992009-02-02 14:59:50 -03001207static struct mc44s803_config af9015_mc44s803_config = {
1208 .i2c_address = 0xc0,
1209 .dig_out = 1,
1210};
1211
Antti Palosaariee3d4402010-08-13 03:51:26 -03001212static struct tda18218_config af9015_tda18218_config = {
1213 .i2c_address = 0xc0,
1214 .i2c_wr_max = 21, /* max wr bytes AF9015 I2C adap can handle at once */
1215};
1216
Antti Palosaariab07fdd2010-09-09 14:59:10 -03001217static struct mxl5007t_config af9015_mxl5007t_config = {
1218 .xtal_freq_hz = MxL_XTAL_24_MHZ,
1219 .if_freq_hz = MxL_IF_4_57_MHZ,
1220};
1221
Antti Palosaari80619de2008-09-15 17:18:09 -03001222static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
1223{
1224 struct af9015_state *state = adap->dev->priv;
1225 struct i2c_adapter *i2c_adap;
1226 int ret;
1227 deb_info("%s: \n", __func__);
1228
1229 /* select I2C adapter */
1230 if (adap->id == 0)
1231 i2c_adap = &adap->dev->i2c_adap;
1232 else
1233 i2c_adap = &state->i2c_adap;
1234
1235 switch (af9015_af9013_config[adap->id].tuner) {
1236 case AF9013_TUNER_MT2060:
1237 case AF9013_TUNER_MT2060_2:
1238 ret = dvb_attach(mt2060_attach, adap->fe, i2c_adap,
1239 &af9015_mt2060_config,
1240 af9015_config.mt2060_if1[adap->id])
1241 == NULL ? -ENODEV : 0;
1242 break;
1243 case AF9013_TUNER_QT1010:
1244 case AF9013_TUNER_QT1010A:
1245 ret = dvb_attach(qt1010_attach, adap->fe, i2c_adap,
1246 &af9015_qt1010_config) == NULL ? -ENODEV : 0;
1247 break;
1248 case AF9013_TUNER_TDA18271:
1249 ret = dvb_attach(tda18271_attach, adap->fe, 0xc0, i2c_adap,
1250 &af9015_tda18271_config) == NULL ? -ENODEV : 0;
1251 break;
Antti Palosaariee3d4402010-08-13 03:51:26 -03001252 case AF9013_TUNER_TDA18218:
1253 ret = dvb_attach(tda18218_attach, adap->fe, i2c_adap,
1254 &af9015_tda18218_config) == NULL ? -ENODEV : 0;
1255 break;
Antti Palosaari80619de2008-09-15 17:18:09 -03001256 case AF9013_TUNER_MXL5003D:
1257 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1258 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
1259 break;
1260 case AF9013_TUNER_MXL5005D:
1261 case AF9013_TUNER_MXL5005R:
1262 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1263 &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
1264 break;
1265 case AF9013_TUNER_ENV77H11D5:
1266 ret = dvb_attach(dvb_pll_attach, adap->fe, 0xc0, i2c_adap,
1267 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
1268 break;
1269 case AF9013_TUNER_MC44S803:
Jochen Friedrichd5633992009-02-02 14:59:50 -03001270 ret = dvb_attach(mc44s803_attach, adap->fe, i2c_adap,
1271 &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
Antti Palosaari80619de2008-09-15 17:18:09 -03001272 break;
Antti Palosaariab07fdd2010-09-09 14:59:10 -03001273 case AF9013_TUNER_MXL5007T:
1274 ret = dvb_attach(mxl5007t_attach, adap->fe, i2c_adap,
1275 0xc0, &af9015_mxl5007t_config) == NULL ? -ENODEV : 0;
1276 break;
Antti Palosaari80619de2008-09-15 17:18:09 -03001277 case AF9013_TUNER_UNKNOWN:
1278 default:
1279 ret = -ENODEV;
1280 err("Unknown tuner id:%d",
1281 af9015_af9013_config[adap->id].tuner);
1282 }
1283 return ret;
1284}
1285
1286static struct usb_device_id af9015_usb_table[] = {
1287/* 0 */{USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015)},
1288 {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016)},
1289 {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD)},
1290 {USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E)},
1291 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U)},
1292/* 5 */{USB_DEVICE(USB_VID_VISIONPLUS,
1293 USB_PID_TINYTWIN)},
1294 {USB_DEVICE(USB_VID_VISIONPLUS,
1295 USB_PID_AZUREWAVE_AD_TU700)},
1296 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2)},
1297 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T)},
1298 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X)},
1299/* 10 */{USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380)},
1300 {USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO)},
1301 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2)},
Antti Palosaaria3765882008-09-19 18:34:06 -03001302 {USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2)},
Antti Palosaari05c1cab2008-09-22 12:32:37 -03001303 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309)},
Herbert Graeber641015a2008-10-07 10:06:36 -03001304/* 15 */{USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III)},
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001305 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U)},
Antti Palosaari71bf2e02009-01-13 12:47:28 -03001306 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2)},
Antti Palosaari58fe1592009-03-26 20:41:05 -03001307 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3)},
Marc Schneider26144842009-03-26 21:07:18 -03001308 {USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT)},
Antti Palosaari1ed5fad2009-04-09 15:14:18 -03001309/* 20 */{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850)},
1310 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805)},
Marcel Jueling734dd232009-04-09 17:16:41 -03001311 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU)},
Wen-chien Jesse Sung6e9c1a22009-04-28 01:11:22 -03001312 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810)},
Antti Palosaari22d46452009-05-31 17:07:01 -03001313 {USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03)},
Mart Raudseppc92f0562009-07-24 13:45:41 -03001314/* 25 */{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2)},
Antti Palosaari486ba122009-09-18 13:37:57 -03001315 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T)},
Ignacio de Miguel Diaz52322632009-11-13 23:13:34 -03001316 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20)},
Antti Palosaarifa1df552010-02-10 20:05:48 -03001317 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2)},
Antti Palosaari809c1e82010-02-10 20:07:30 -03001318 {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS)},
Antti Palosaariab9b4f22010-03-01 13:50:40 -03001319/* 30 */{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T)},
Antti Palosaari7fc87092010-03-01 14:06:52 -03001320 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4)},
Antti Palosaari2606cfa2010-05-23 18:26:37 -03001321 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M)},
Stefan Lippers-Hollmannd7ef4852010-08-25 10:08:48 -03001322 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_RC)},
Antti Palosaariab07fdd2010-09-09 14:59:10 -03001323 {USB_DEVICE(USB_VID_TERRATEC,
1324 USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC)},
Antti Palosaari80619de2008-09-15 17:18:09 -03001325 {0},
1326};
1327MODULE_DEVICE_TABLE(usb, af9015_usb_table);
1328
1329static struct dvb_usb_device_properties af9015_properties[] = {
1330 {
1331 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1332
1333 .usb_ctrl = DEVICE_SPECIFIC,
1334 .download_firmware = af9015_download_firmware,
1335 .firmware = "dvb-usb-af9015.fw",
Jose Alberto Reguerocce25712008-11-13 14:14:18 -03001336 .no_reconnect = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001337
Antti Palosaari02542942009-09-16 20:33:03 -03001338 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari80619de2008-09-15 17:18:09 -03001339
1340 .num_adapters = 2,
1341 .adapter = {
1342 {
1343 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1344 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1345
1346 .pid_filter_count = 32,
1347 .pid_filter = af9015_pid_filter,
1348 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1349
1350 .frontend_attach =
1351 af9015_af9013_frontend_attach,
1352 .tuner_attach = af9015_tuner_attach,
1353 .stream = {
1354 .type = USB_BULK,
1355 .count = 6,
1356 .endpoint = 0x84,
1357 },
1358 },
1359 {
1360 .frontend_attach =
1361 af9015_af9013_frontend_attach,
1362 .tuner_attach = af9015_tuner_attach,
1363 .stream = {
1364 .type = USB_BULK,
1365 .count = 6,
1366 .endpoint = 0x85,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001367 .u = {
1368 .bulk = {
1369 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001370 TS_USB20_FRAME_SIZE,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001371 }
1372 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001373 },
1374 }
1375 },
1376
1377 .identify_state = af9015_identify_state,
1378
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001379 .rc.legacy = {
1380 .rc_query = af9015_rc_query,
1381 .rc_interval = 150,
1382 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001383
1384 .i2c_algo = &af9015_i2c_algo,
1385
Antti Palosaariab07fdd2010-09-09 14:59:10 -03001386 .num_device_descs = 11, /* check max from dvb-usb.h */
Antti Palosaari80619de2008-09-15 17:18:09 -03001387 .devices = {
1388 {
1389 .name = "Afatech AF9015 DVB-T USB2.0 stick",
1390 .cold_ids = {&af9015_usb_table[0],
1391 &af9015_usb_table[1], NULL},
1392 .warm_ids = {NULL},
1393 },
1394 {
1395 .name = "Leadtek WinFast DTV Dongle Gold",
1396 .cold_ids = {&af9015_usb_table[2], NULL},
1397 .warm_ids = {NULL},
1398 },
1399 {
1400 .name = "Pinnacle PCTV 71e",
1401 .cold_ids = {&af9015_usb_table[3], NULL},
1402 .warm_ids = {NULL},
1403 },
1404 {
1405 .name = "KWorld PlusTV Dual DVB-T Stick " \
1406 "(DVB-T 399U)",
Mart Raudseppc92f0562009-07-24 13:45:41 -03001407 .cold_ids = {&af9015_usb_table[4],
1408 &af9015_usb_table[25], NULL},
Antti Palosaari80619de2008-09-15 17:18:09 -03001409 .warm_ids = {NULL},
1410 },
1411 {
1412 .name = "DigitalNow TinyTwin DVB-T Receiver",
Antti Palosaarifa1df552010-02-10 20:05:48 -03001413 .cold_ids = {&af9015_usb_table[5],
1414 &af9015_usb_table[28], NULL},
Antti Palosaari80619de2008-09-15 17:18:09 -03001415 .warm_ids = {NULL},
1416 },
1417 {
1418 .name = "TwinHan AzureWave AD-TU700(704J)",
1419 .cold_ids = {&af9015_usb_table[6], NULL},
1420 .warm_ids = {NULL},
1421 },
1422 {
1423 .name = "TerraTec Cinergy T USB XE",
1424 .cold_ids = {&af9015_usb_table[7], NULL},
1425 .warm_ids = {NULL},
1426 },
1427 {
1428 .name = "KWorld PlusTV Dual DVB-T PCI " \
1429 "(DVB-T PC160-2T)",
1430 .cold_ids = {&af9015_usb_table[8], NULL},
1431 .warm_ids = {NULL},
1432 },
1433 {
1434 .name = "AVerMedia AVerTV DVB-T Volar X",
1435 .cold_ids = {&af9015_usb_table[9], NULL},
1436 .warm_ids = {NULL},
1437 },
Antti Palosaari76391a72010-09-09 12:10:50 -03001438 {
1439 .name = "TerraTec Cinergy T Stick RC",
1440 .cold_ids = {&af9015_usb_table[33], NULL},
1441 .warm_ids = {NULL},
1442 },
Antti Palosaariab07fdd2010-09-09 14:59:10 -03001443 {
1444 .name = "TerraTec Cinergy T Stick Dual RC",
1445 .cold_ids = {&af9015_usb_table[34], NULL},
1446 .warm_ids = {NULL},
1447 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001448 }
1449 }, {
1450 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1451
1452 .usb_ctrl = DEVICE_SPECIFIC,
1453 .download_firmware = af9015_download_firmware,
1454 .firmware = "dvb-usb-af9015.fw",
Jose Alberto Reguerocce25712008-11-13 14:14:18 -03001455 .no_reconnect = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001456
Antti Palosaari02542942009-09-16 20:33:03 -03001457 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari80619de2008-09-15 17:18:09 -03001458
1459 .num_adapters = 2,
1460 .adapter = {
1461 {
1462 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1463 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1464
1465 .pid_filter_count = 32,
1466 .pid_filter = af9015_pid_filter,
1467 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1468
1469 .frontend_attach =
1470 af9015_af9013_frontend_attach,
1471 .tuner_attach = af9015_tuner_attach,
1472 .stream = {
1473 .type = USB_BULK,
1474 .count = 6,
1475 .endpoint = 0x84,
1476 },
1477 },
1478 {
1479 .frontend_attach =
1480 af9015_af9013_frontend_attach,
1481 .tuner_attach = af9015_tuner_attach,
1482 .stream = {
1483 .type = USB_BULK,
1484 .count = 6,
1485 .endpoint = 0x85,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001486 .u = {
1487 .bulk = {
1488 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001489 TS_USB20_FRAME_SIZE,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001490 }
1491 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001492 },
1493 }
1494 },
1495
1496 .identify_state = af9015_identify_state,
1497
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001498 .rc.legacy = {
1499 .rc_query = af9015_rc_query,
1500 .rc_interval = 150,
1501 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001502
1503 .i2c_algo = &af9015_i2c_algo,
1504
Antti Palosaaria44b91d2010-09-09 12:05:31 -03001505 .num_device_descs = 9, /* check max from dvb-usb.h */
Antti Palosaari80619de2008-09-15 17:18:09 -03001506 .devices = {
1507 {
1508 .name = "Xtensions XD-380",
1509 .cold_ids = {&af9015_usb_table[10], NULL},
1510 .warm_ids = {NULL},
1511 },
1512 {
1513 .name = "MSI DIGIVOX Duo",
1514 .cold_ids = {&af9015_usb_table[11], NULL},
1515 .warm_ids = {NULL},
1516 },
1517 {
1518 .name = "Fujitsu-Siemens Slim Mobile USB DVB-T",
1519 .cold_ids = {&af9015_usb_table[12], NULL},
1520 .warm_ids = {NULL},
1521 },
Mikko Ohtamaa111f9ec2008-09-19 18:26:05 -03001522 {
Antti Palosaaria3765882008-09-19 18:34:06 -03001523 .name = "Telestar Starstick 2",
Mikko Ohtamaa111f9ec2008-09-19 18:26:05 -03001524 .cold_ids = {&af9015_usb_table[13], NULL},
1525 .warm_ids = {NULL},
1526 },
Antti Palosaari05c1cab2008-09-22 12:32:37 -03001527 {
1528 .name = "AVerMedia A309",
1529 .cold_ids = {&af9015_usb_table[14], NULL},
1530 .warm_ids = {NULL},
1531 },
Herbert Graeber641015a2008-10-07 10:06:36 -03001532 {
1533 .name = "MSI Digi VOX mini III",
1534 .cold_ids = {&af9015_usb_table[15], NULL},
1535 .warm_ids = {NULL},
1536 },
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001537 {
1538 .name = "KWorld USB DVB-T TV Stick II " \
1539 "(VS-DVB-T 395U)",
Antti Palosaari71bf2e02009-01-13 12:47:28 -03001540 .cold_ids = {&af9015_usb_table[16],
Antti Palosaari58fe1592009-03-26 20:41:05 -03001541 &af9015_usb_table[17],
Antti Palosaari7fc87092010-03-01 14:06:52 -03001542 &af9015_usb_table[18],
1543 &af9015_usb_table[31], NULL},
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001544 .warm_ids = {NULL},
1545 },
Marc Schneider26144842009-03-26 21:07:18 -03001546 {
1547 .name = "TrekStor DVB-T USB Stick",
1548 .cold_ids = {&af9015_usb_table[19], NULL},
1549 .warm_ids = {NULL},
1550 },
Antti Palosaari3956fef2009-03-31 17:01:02 -03001551 {
1552 .name = "AverMedia AVerTV Volar Black HD " \
1553 "(A850)",
1554 .cold_ids = {&af9015_usb_table[20], NULL},
1555 .warm_ids = {NULL},
1556 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001557 }
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001558 }, {
1559 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1560
1561 .usb_ctrl = DEVICE_SPECIFIC,
1562 .download_firmware = af9015_download_firmware,
1563 .firmware = "dvb-usb-af9015.fw",
1564 .no_reconnect = 1,
1565
Antti Palosaari02542942009-09-16 20:33:03 -03001566 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001567
1568 .num_adapters = 2,
1569 .adapter = {
1570 {
1571 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1572 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1573
1574 .pid_filter_count = 32,
1575 .pid_filter = af9015_pid_filter,
1576 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1577
1578 .frontend_attach =
1579 af9015_af9013_frontend_attach,
1580 .tuner_attach = af9015_tuner_attach,
1581 .stream = {
1582 .type = USB_BULK,
1583 .count = 6,
1584 .endpoint = 0x84,
1585 },
1586 },
1587 {
1588 .frontend_attach =
1589 af9015_af9013_frontend_attach,
1590 .tuner_attach = af9015_tuner_attach,
1591 .stream = {
1592 .type = USB_BULK,
1593 .count = 6,
1594 .endpoint = 0x85,
1595 .u = {
1596 .bulk = {
1597 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001598 TS_USB20_FRAME_SIZE,
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001599 }
1600 }
1601 },
1602 }
1603 },
1604
1605 .identify_state = af9015_identify_state,
1606
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001607 .rc.legacy = {
1608 .rc_query = af9015_rc_query,
1609 .rc_interval = 150,
1610 },
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001611
1612 .i2c_algo = &af9015_i2c_algo,
1613
Antti Palosaaria44b91d2010-09-09 12:05:31 -03001614 .num_device_descs = 9, /* check max from dvb-usb.h */
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001615 .devices = {
Antti Palosaari1ed5fad2009-04-09 15:14:18 -03001616 {
1617 .name = "AverMedia AVerTV Volar GPS 805 (A805)",
1618 .cold_ids = {&af9015_usb_table[21], NULL},
1619 .warm_ids = {NULL},
1620 },
Marcel Jueling734dd232009-04-09 17:16:41 -03001621 {
1622 .name = "Conceptronic USB2.0 DVB-T CTVDIGRCU " \
1623 "V3.0",
1624 .cold_ids = {&af9015_usb_table[22], NULL},
1625 .warm_ids = {NULL},
1626 },
Wen-chien Jesse Sung6e9c1a22009-04-28 01:11:22 -03001627 {
1628 .name = "KWorld Digial MC-810",
1629 .cold_ids = {&af9015_usb_table[23], NULL},
1630 .warm_ids = {NULL},
1631 },
Antti Palosaari22d46452009-05-31 17:07:01 -03001632 {
1633 .name = "Genius TVGo DVB-T03",
1634 .cold_ids = {&af9015_usb_table[24], NULL},
1635 .warm_ids = {NULL},
1636 },
Antti Palosaari486ba122009-09-18 13:37:57 -03001637 {
1638 .name = "KWorld PlusTV DVB-T PCI Pro Card " \
1639 "(DVB-T PC160-T)",
1640 .cold_ids = {&af9015_usb_table[26], NULL},
1641 .warm_ids = {NULL},
1642 },
Ignacio de Miguel Diaz52322632009-11-13 23:13:34 -03001643 {
1644 .name = "Sveon STV20 Tuner USB DVB-T HDTV",
1645 .cold_ids = {&af9015_usb_table[27], NULL},
1646 .warm_ids = {NULL},
1647 },
Antti Palosaari809c1e82010-02-10 20:07:30 -03001648 {
1649 .name = "Leadtek WinFast DTV2000DS",
1650 .cold_ids = {&af9015_usb_table[29], NULL},
1651 .warm_ids = {NULL},
1652 },
Antti Palosaariab9b4f22010-03-01 13:50:40 -03001653 {
1654 .name = "KWorld USB DVB-T Stick Mobile " \
1655 "(UB383-T)",
1656 .cold_ids = {&af9015_usb_table[30], NULL},
1657 .warm_ids = {NULL},
1658 },
Antti Palosaari2606cfa2010-05-23 18:26:37 -03001659 {
1660 .name = "AverMedia AVerTV Volar M (A815Mac)",
1661 .cold_ids = {&af9015_usb_table[32], NULL},
1662 .warm_ids = {NULL},
1663 },
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001664 }
1665 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001666};
Antti Palosaari80619de2008-09-15 17:18:09 -03001667
1668static int af9015_usb_probe(struct usb_interface *intf,
1669 const struct usb_device_id *id)
1670{
1671 int ret = 0;
1672 struct dvb_usb_device *d = NULL;
1673 struct usb_device *udev = interface_to_usbdev(intf);
1674 u8 i;
1675
1676 deb_info("%s: interface:%d\n", __func__,
1677 intf->cur_altsetting->desc.bInterfaceNumber);
1678
1679 /* interface 0 is used by DVB-T receiver and
1680 interface 1 is for remote controller (HID) */
1681 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
1682 ret = af9015_read_config(udev);
1683 if (ret)
1684 return ret;
1685
1686 for (i = 0; i < af9015_properties_count; i++) {
1687 ret = dvb_usb_device_init(intf, &af9015_properties[i],
1688 THIS_MODULE, &d, adapter_nr);
1689 if (!ret)
1690 break;
1691 if (ret != -ENODEV)
1692 return ret;
1693 }
1694 if (ret)
1695 return ret;
1696
1697 if (d)
1698 ret = af9015_init(d);
1699 }
1700
1701 return ret;
1702}
1703
Antti Palosaari349d0422008-11-05 16:31:24 -03001704static void af9015_i2c_exit(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -03001705{
1706 struct af9015_state *state = d->priv;
1707 deb_info("%s: \n", __func__);
1708
1709 /* remove 2nd I2C adapter */
1710 if (d->state & DVB_USB_STATE_I2C)
1711 i2c_del_adapter(&state->i2c_adap);
1712}
1713
1714static void af9015_usb_device_exit(struct usb_interface *intf)
1715{
1716 struct dvb_usb_device *d = usb_get_intfdata(intf);
1717 deb_info("%s: \n", __func__);
1718
1719 /* remove 2nd I2C adapter */
1720 if (d != NULL && d->desc != NULL)
1721 af9015_i2c_exit(d);
1722
1723 dvb_usb_device_exit(intf);
1724}
1725
1726/* usb specific object needed to register this driver with the usb subsystem */
1727static struct usb_driver af9015_usb_driver = {
1728 .name = "dvb_usb_af9015",
1729 .probe = af9015_usb_probe,
1730 .disconnect = af9015_usb_device_exit,
1731 .id_table = af9015_usb_table,
1732};
1733
1734/* module stuff */
1735static int __init af9015_usb_module_init(void)
1736{
1737 int ret;
1738 ret = usb_register(&af9015_usb_driver);
1739 if (ret)
1740 err("module init failed:%d", ret);
1741
1742 return ret;
1743}
1744
1745static void __exit af9015_usb_module_exit(void)
1746{
1747 /* deregister this driver from the USB subsystem */
1748 usb_deregister(&af9015_usb_driver);
1749}
1750
1751module_init(af9015_usb_module_init);
1752module_exit(af9015_usb_module_exit);
1753
1754MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1755MODULE_DESCRIPTION("Driver for Afatech AF9015 DVB-T");
1756MODULE_LICENSE("GPL");