blob: 580ed14e55431e51c840359e8d207ab4e236b619 [file] [log] [blame]
Antti Palosaari80619de2008-09-15 17:18:09 -03001/*
2 * DVB USB Linux driver for Afatech AF9015 DVB-T USB2.0 receiver
3 *
4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
5 *
6 * Thanks to Afatech who kindly provided information.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 */
23
Jiri Slaby6c614042010-01-22 12:10:52 -030024#include <linux/hash.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Jiri Slaby6c614042010-01-22 12:10:52 -030026
Antti Palosaari80619de2008-09-15 17:18:09 -030027#include "af9015.h"
28#include "af9013.h"
29#include "mt2060.h"
30#include "qt1010.h"
31#include "tda18271.h"
32#include "mxl5005s.h"
Jochen Friedrichd5633992009-02-02 14:59:50 -030033#include "mc44s803.h"
Antti Palosaariee3d4402010-08-13 03:51:26 -030034#include "tda18218.h"
Antti Palosaariab07fdd2010-09-09 14:59:10 -030035#include "mxl5007t.h"
Antti Palosaari80619de2008-09-15 17:18:09 -030036
Antti Palosaari349d0422008-11-05 16:31:24 -030037static int dvb_usb_af9015_debug;
Antti Palosaari80619de2008-09-15 17:18:09 -030038module_param_named(debug, dvb_usb_af9015_debug, int, 0644);
39MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
Antti Palosaari349d0422008-11-05 16:31:24 -030040static int dvb_usb_af9015_remote;
Antti Palosaari80619de2008-09-15 17:18:09 -030041module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
42MODULE_PARM_DESC(remote, "select remote");
Antti Palosaari80619de2008-09-15 17:18:09 -030043DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
44
45static DEFINE_MUTEX(af9015_usb_mutex);
46
47static struct af9015_config af9015_config;
Antti Palosaari85d7d7c2009-04-09 09:16:12 -030048static struct dvb_usb_device_properties af9015_properties[3];
Antti Palosaari349d0422008-11-05 16:31:24 -030049static int af9015_properties_count = ARRAY_SIZE(af9015_properties);
Antti Palosaari80619de2008-09-15 17:18:09 -030050
51static struct af9013_config af9015_af9013_config[] = {
52 {
53 .demod_address = AF9015_I2C_DEMOD,
54 .output_mode = AF9013_OUTPUT_MODE_USB,
55 .api_version = { 0, 1, 9, 0 },
56 .gpio[0] = AF9013_GPIO_HI,
Antti Palosaari80619de2008-09-15 17:18:09 -030057 .gpio[3] = AF9013_GPIO_TUNER_ON,
58
59 }, {
60 .output_mode = AF9013_OUTPUT_MODE_SERIAL,
61 .api_version = { 0, 1, 9, 0 },
62 .gpio[0] = AF9013_GPIO_TUNER_ON,
63 .gpio[1] = AF9013_GPIO_LO,
64 }
65};
66
67static int af9015_rw_udev(struct usb_device *udev, struct req_t *req)
68{
Antti Palosaari06565d72009-09-12 20:46:30 -030069#define BUF_LEN 63
70#define REQ_HDR_LEN 8 /* send header size */
71#define ACK_HDR_LEN 2 /* rece header size */
Antti Palosaari80619de2008-09-15 17:18:09 -030072 int act_len, ret;
Antti Palosaari06565d72009-09-12 20:46:30 -030073 u8 buf[BUF_LEN];
Antti Palosaari80619de2008-09-15 17:18:09 -030074 u8 write = 1;
Antti Palosaari06565d72009-09-12 20:46:30 -030075 u8 msg_len = REQ_HDR_LEN;
Antti Palosaari80619de2008-09-15 17:18:09 -030076 static u8 seq; /* packet sequence number */
77
78 if (mutex_lock_interruptible(&af9015_usb_mutex) < 0)
79 return -EAGAIN;
80
81 buf[0] = req->cmd;
82 buf[1] = seq++;
83 buf[2] = req->i2c_addr;
84 buf[3] = req->addr >> 8;
85 buf[4] = req->addr & 0xff;
86 buf[5] = req->mbox;
87 buf[6] = req->addr_len;
88 buf[7] = req->data_len;
89
90 switch (req->cmd) {
91 case GET_CONFIG:
Antti Palosaari80619de2008-09-15 17:18:09 -030092 case READ_MEMORY:
93 case RECONNECT_USB:
94 case GET_IR_CODE:
95 write = 0;
96 break;
97 case READ_I2C:
98 write = 0;
99 buf[2] |= 0x01; /* set I2C direction */
100 case WRITE_I2C:
101 buf[0] = READ_WRITE_I2C;
102 break;
103 case WRITE_MEMORY:
104 if (((req->addr & 0xff00) == 0xff00) ||
Antti Palosaarif4e96de2009-09-12 21:25:59 -0300105 ((req->addr & 0xff00) == 0xae00))
Antti Palosaari80619de2008-09-15 17:18:09 -0300106 buf[0] = WRITE_VIRTUAL_MEMORY;
107 case WRITE_VIRTUAL_MEMORY:
108 case COPY_FIRMWARE:
109 case DOWNLOAD_FIRMWARE:
Nils Kassubeba1bc642009-07-28 11:54:52 -0300110 case BOOT:
Antti Palosaari80619de2008-09-15 17:18:09 -0300111 break;
112 default:
113 err("unknown command:%d", req->cmd);
114 ret = -1;
115 goto error_unlock;
116 }
117
Antti Palosaari06565d72009-09-12 20:46:30 -0300118 /* buffer overflow check */
119 if ((write && (req->data_len > BUF_LEN - REQ_HDR_LEN)) ||
120 (!write && (req->data_len > BUF_LEN - ACK_HDR_LEN))) {
121 err("too much data; cmd:%d len:%d", req->cmd, req->data_len);
122 ret = -EINVAL;
123 goto error_unlock;
124 }
125
Antti Palosaari80619de2008-09-15 17:18:09 -0300126 /* write requested */
127 if (write) {
Antti Palosaari06565d72009-09-12 20:46:30 -0300128 memcpy(&buf[REQ_HDR_LEN], req->data, req->data_len);
Antti Palosaari80619de2008-09-15 17:18:09 -0300129 msg_len += req->data_len;
130 }
Antti Palosaari06565d72009-09-12 20:46:30 -0300131
Antti Palosaari80619de2008-09-15 17:18:09 -0300132 deb_xfer(">>> ");
133 debug_dump(buf, msg_len, deb_xfer);
134
135 /* send req */
136 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
Antti Palosaari06565d72009-09-12 20:46:30 -0300137 &act_len, AF9015_USB_TIMEOUT);
Antti Palosaari80619de2008-09-15 17:18:09 -0300138 if (ret)
139 err("bulk message failed:%d (%d/%d)", ret, msg_len, act_len);
140 else
141 if (act_len != msg_len)
142 ret = -1; /* all data is not send */
143 if (ret)
144 goto error_unlock;
145
146 /* no ack for those packets */
147 if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
148 goto exit_unlock;
149
Antti Palosaari06565d72009-09-12 20:46:30 -0300150 /* write receives seq + status = 2 bytes
151 read receives seq + status + data = 2 + N bytes */
152 msg_len = ACK_HDR_LEN;
153 if (!write)
154 msg_len += req->data_len;
155
Antti Palosaari80619de2008-09-15 17:18:09 -0300156 ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
Antti Palosaari06565d72009-09-12 20:46:30 -0300157 &act_len, AF9015_USB_TIMEOUT);
Antti Palosaari80619de2008-09-15 17:18:09 -0300158 if (ret) {
159 err("recv bulk message failed:%d", ret);
160 ret = -1;
161 goto error_unlock;
162 }
163
164 deb_xfer("<<< ");
165 debug_dump(buf, act_len, deb_xfer);
166
167 /* remote controller query status is 1 if remote code is not received */
168 if (req->cmd == GET_IR_CODE && buf[1] == 1) {
169 buf[1] = 0; /* clear command "error" status */
170 memset(&buf[2], 0, req->data_len);
171 buf[3] = 1; /* no remote code received mark */
172 }
173
174 /* check status */
175 if (buf[1]) {
176 err("command failed:%d", buf[1]);
177 ret = -1;
178 goto error_unlock;
179 }
180
181 /* read request, copy returned data to return buf */
182 if (!write)
Antti Palosaari06565d72009-09-12 20:46:30 -0300183 memcpy(req->data, &buf[ACK_HDR_LEN], req->data_len);
Antti Palosaari80619de2008-09-15 17:18:09 -0300184
185error_unlock:
186exit_unlock:
187 mutex_unlock(&af9015_usb_mutex);
188
189 return ret;
190}
191
192static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
193{
194 return af9015_rw_udev(d->udev, req);
195}
196
197static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
198 u8 len)
199{
200 struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
201 val};
202 return af9015_ctrl_msg(d, &req);
203}
204
205static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
206{
207 return af9015_write_regs(d, addr, &val, 1);
208}
209
Antti Palosaari74c8e3a2010-10-22 18:45:18 -0300210static int af9015_read_regs(struct dvb_usb_device *d, u16 addr, u8 *val, u8 len)
211{
212 struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
213 val};
214 return af9015_ctrl_msg(d, &req);
215}
216
Antti Palosaari80619de2008-09-15 17:18:09 -0300217static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
218{
Antti Palosaari74c8e3a2010-10-22 18:45:18 -0300219 return af9015_read_regs(d, addr, val, 1);
Antti Palosaari80619de2008-09-15 17:18:09 -0300220}
221
222static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
223 u8 val)
224{
225 struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
226
227 if (addr == af9015_af9013_config[0].demod_address ||
228 addr == af9015_af9013_config[1].demod_address)
229 req.addr_len = 3;
230
231 return af9015_ctrl_msg(d, &req);
232}
233
234static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
235 u8 *val)
236{
237 struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
238
239 if (addr == af9015_af9013_config[0].demod_address ||
240 addr == af9015_af9013_config[1].demod_address)
241 req.addr_len = 3;
242
243 return af9015_ctrl_msg(d, &req);
244}
245
246static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
247 int num)
248{
249 struct dvb_usb_device *d = i2c_get_adapdata(adap);
250 int ret = 0, i = 0;
251 u16 addr;
Antti Palosaari675375d2010-10-07 21:46:41 -0300252 u8 uninitialized_var(mbox), addr_len;
Antti Palosaari80619de2008-09-15 17:18:09 -0300253 struct req_t req;
254
255/* TODO: implement bus lock
256
257The bus lock is needed because there is two tuners both using same I2C-address.
258Due to that the only way to select correct tuner is use demodulator I2C-gate.
259
260................................................
261. AF9015 includes integrated AF9013 demodulator.
262. ____________ ____________ . ____________
263.| uC | | demod | . | tuner |
264.|------------| |------------| . |------------|
265.| AF9015 | | AF9013/5 | . | MXL5003 |
266.| |--+----I2C-------|-----/ -----|-.-----I2C-------| |
267.| | | | addr 0x38 | . | addr 0xc6 |
268.|____________| | |____________| . |____________|
269.................|..............................
270 | ____________ ____________
271 | | demod | | tuner |
272 | |------------| |------------|
273 | | AF9013 | | MXL5003 |
274 +----I2C-------|-----/ -----|-------I2C-------| |
275 | addr 0x3a | | addr 0xc6 |
276 |____________| |____________|
277*/
278 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
279 return -EAGAIN;
280
281 while (i < num) {
282 if (msg[i].addr == af9015_af9013_config[0].demod_address ||
283 msg[i].addr == af9015_af9013_config[1].demod_address) {
284 addr = msg[i].buf[0] << 8;
285 addr += msg[i].buf[1];
286 mbox = msg[i].buf[2];
287 addr_len = 3;
288 } else {
289 addr = msg[i].buf[0];
290 addr_len = 1;
Antti Palosaari675375d2010-10-07 21:46:41 -0300291 /* mbox is don't care in that case */
Antti Palosaari80619de2008-09-15 17:18:09 -0300292 }
293
294 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
295 if (msg[i].addr ==
296 af9015_af9013_config[0].demod_address)
297 req.cmd = READ_MEMORY;
298 else
299 req.cmd = READ_I2C;
300 req.i2c_addr = msg[i].addr;
301 req.addr = addr;
302 req.mbox = mbox;
303 req.addr_len = addr_len;
304 req.data_len = msg[i+1].len;
305 req.data = &msg[i+1].buf[0];
306 ret = af9015_ctrl_msg(d, &req);
307 i += 2;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300308 } else if (msg[i].flags & I2C_M_RD) {
309 ret = -EINVAL;
310 if (msg[i].addr ==
311 af9015_af9013_config[0].demod_address)
312 goto error;
313 else
314 req.cmd = READ_I2C;
315 req.i2c_addr = msg[i].addr;
316 req.addr = addr;
317 req.mbox = mbox;
318 req.addr_len = addr_len;
319 req.data_len = msg[i].len;
320 req.data = &msg[i].buf[0];
321 ret = af9015_ctrl_msg(d, &req);
322 i += 1;
Antti Palosaari80619de2008-09-15 17:18:09 -0300323 } else {
324 if (msg[i].addr ==
325 af9015_af9013_config[0].demod_address)
326 req.cmd = WRITE_MEMORY;
327 else
328 req.cmd = WRITE_I2C;
329 req.i2c_addr = msg[i].addr;
330 req.addr = addr;
331 req.mbox = mbox;
332 req.addr_len = addr_len;
333 req.data_len = msg[i].len-addr_len;
334 req.data = &msg[i].buf[addr_len];
335 ret = af9015_ctrl_msg(d, &req);
336 i += 1;
337 }
338 if (ret)
339 goto error;
340
341 }
342 ret = i;
343
344error:
345 mutex_unlock(&d->i2c_mutex);
346
347 return ret;
348}
349
350static u32 af9015_i2c_func(struct i2c_adapter *adapter)
351{
352 return I2C_FUNC_I2C;
353}
354
355static struct i2c_algorithm af9015_i2c_algo = {
356 .master_xfer = af9015_i2c_xfer,
357 .functionality = af9015_i2c_func,
358};
359
360static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
361{
362 int ret;
363 u8 val, mask = 0x01;
364
365 ret = af9015_read_reg(d, addr, &val);
366 if (ret)
367 return ret;
368
369 mask <<= bit;
370 if (op) {
371 /* set bit */
372 val |= mask;
373 } else {
374 /* clear bit */
375 mask ^= 0xff;
376 val &= mask;
377 }
378
379 return af9015_write_reg(d, addr, val);
380}
381
382static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
383{
384 return af9015_do_reg_bit(d, addr, bit, 1);
385}
386
387static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
388{
389 return af9015_do_reg_bit(d, addr, bit, 0);
390}
391
392static int af9015_init_endpoint(struct dvb_usb_device *d)
393{
394 int ret;
395 u16 frame_size;
396 u8 packet_size;
397 deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);
398
Antti Palosaari9c863272009-09-12 13:35:29 -0300399 /* Windows driver uses packet count 21 for USB1.1 and 348 for USB2.0.
400 We use smaller - about 1/4 from the original, 5 and 87. */
Antti Palosaari80619de2008-09-15 17:18:09 -0300401#define TS_PACKET_SIZE 188
402
Antti Palosaari9c863272009-09-12 13:35:29 -0300403#define TS_USB20_PACKET_COUNT 87
Antti Palosaari80619de2008-09-15 17:18:09 -0300404#define TS_USB20_FRAME_SIZE (TS_PACKET_SIZE*TS_USB20_PACKET_COUNT)
405
Antti Palosaari9c863272009-09-12 13:35:29 -0300406#define TS_USB11_PACKET_COUNT 5
Antti Palosaari80619de2008-09-15 17:18:09 -0300407#define TS_USB11_FRAME_SIZE (TS_PACKET_SIZE*TS_USB11_PACKET_COUNT)
408
409#define TS_USB20_MAX_PACKET_SIZE 512
410#define TS_USB11_MAX_PACKET_SIZE 64
411
412 if (d->udev->speed == USB_SPEED_FULL) {
413 frame_size = TS_USB11_FRAME_SIZE/4;
414 packet_size = TS_USB11_MAX_PACKET_SIZE/4;
415 } else {
416 frame_size = TS_USB20_FRAME_SIZE/4;
417 packet_size = TS_USB20_MAX_PACKET_SIZE/4;
418 }
419
420 ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
421 if (ret)
422 goto error;
423 ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
424 if (ret)
425 goto error;
426 ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
427 if (ret)
428 goto error;
429 ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
430 if (ret)
431 goto error;
432 ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
433 if (ret)
434 goto error;
435 if (af9015_config.dual_mode) {
436 ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
437 if (ret)
438 goto error;
439 }
440 ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
441 if (ret)
442 goto error;
443 if (af9015_config.dual_mode) {
444 ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
445 if (ret)
446 goto error;
447 }
448 /* EP4 xfer length */
449 ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
450 if (ret)
451 goto error;
452 ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
453 if (ret)
454 goto error;
455 /* EP5 xfer length */
456 ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
457 if (ret)
458 goto error;
459 ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
460 if (ret)
461 goto error;
462 ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
463 if (ret)
464 goto error;
465 ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
466 if (ret)
467 goto error;
468 ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
469 if (ret)
470 goto error;
471 if (af9015_config.dual_mode) {
472 ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
473 if (ret)
474 goto error;
475 }
476
477 /* enable / disable mp2if2 */
478 if (af9015_config.dual_mode)
479 ret = af9015_set_reg_bit(d, 0xd50b, 0);
480 else
481 ret = af9015_clear_reg_bit(d, 0xd50b, 0);
482error:
483 if (ret)
484 err("endpoint init failed:%d", ret);
485 return ret;
486}
487
488static int af9015_copy_firmware(struct dvb_usb_device *d)
489{
490 int ret;
491 u8 fw_params[4];
492 u8 val, i;
493 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
494 fw_params };
495 deb_info("%s:\n", __func__);
496
497 fw_params[0] = af9015_config.firmware_size >> 8;
498 fw_params[1] = af9015_config.firmware_size & 0xff;
499 fw_params[2] = af9015_config.firmware_checksum >> 8;
500 fw_params[3] = af9015_config.firmware_checksum & 0xff;
501
502 /* wait 2nd demodulator ready */
503 msleep(100);
504
Antti Palosaaried19a5d2010-09-12 21:02:55 -0300505 ret = af9015_read_reg_i2c(d,
506 af9015_af9013_config[1].demod_address, 0x98be, &val);
Antti Palosaari80619de2008-09-15 17:18:09 -0300507 if (ret)
508 goto error;
509 else
510 deb_info("%s: firmware status:%02x\n", __func__, val);
511
512 if (val == 0x0c) /* fw is running, no need for download */
513 goto exit;
514
515 /* set I2C master clock to fast (to speed up firmware copy) */
516 ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
517 if (ret)
518 goto error;
519
520 msleep(50);
521
522 /* copy firmware */
523 ret = af9015_ctrl_msg(d, &req);
524 if (ret)
525 err("firmware copy cmd failed:%d", ret);
526 deb_info("%s: firmware copy done\n", __func__);
527
528 /* set I2C master clock back to normal */
529 ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
530 if (ret)
531 goto error;
532
533 /* request boot firmware */
534 ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].demod_address,
535 0xe205, 1);
536 deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
537 if (ret)
538 goto error;
539
540 for (i = 0; i < 15; i++) {
541 msleep(100);
542
543 /* check firmware status */
544 ret = af9015_read_reg_i2c(d,
545 af9015_af9013_config[1].demod_address, 0x98be, &val);
546 deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
547 __func__, ret, val);
548 if (ret)
549 goto error;
550
551 if (val == 0x0c || val == 0x04) /* success or fail */
552 break;
553 }
554
555 if (val == 0x04) {
556 err("firmware did not run");
557 ret = -1;
558 } else if (val != 0x0c) {
559 err("firmware boot timeout");
560 ret = -1;
561 }
562
563error:
564exit:
565 return ret;
566}
567
Jiri Slaby6c614042010-01-22 12:10:52 -0300568/* hash (and dump) eeprom */
569static int af9015_eeprom_hash(struct usb_device *udev)
Antti Palosaari80619de2008-09-15 17:18:09 -0300570{
Jiri Slaby6c614042010-01-22 12:10:52 -0300571 static const unsigned int eeprom_size = 256;
572 unsigned int reg;
573 int ret;
574 u8 val, *eeprom;
575 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
Antti Palosaari80619de2008-09-15 17:18:09 -0300576
Jiri Slaby6c614042010-01-22 12:10:52 -0300577 eeprom = kmalloc(eeprom_size, GFP_KERNEL);
578 if (eeprom == NULL)
579 return -ENOMEM;
580
581 for (reg = 0; reg < eeprom_size; reg++) {
582 req.addr = reg;
583 ret = af9015_rw_udev(udev, &req);
584 if (ret)
585 goto free;
586 eeprom[reg] = val;
Antti Palosaari80619de2008-09-15 17:18:09 -0300587 }
Jiri Slaby6c614042010-01-22 12:10:52 -0300588
589 if (dvb_usb_af9015_debug & 0x01)
590 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, eeprom,
591 eeprom_size);
592
593 BUG_ON(eeprom_size % 4);
594
595 af9015_config.eeprom_sum = 0;
596 for (reg = 0; reg < eeprom_size / sizeof(u32); reg++) {
597 af9015_config.eeprom_sum *= GOLDEN_RATIO_PRIME_32;
598 af9015_config.eeprom_sum += le32_to_cpu(((u32 *)eeprom)[reg]);
599 }
600
601 deb_info("%s: eeprom sum=%.8x\n", __func__, af9015_config.eeprom_sum);
602
603 ret = 0;
604free:
605 kfree(eeprom);
606 return ret;
Antti Palosaari80619de2008-09-15 17:18:09 -0300607}
608
Antti Palosaari80619de2008-09-15 17:18:09 -0300609static int af9015_init(struct dvb_usb_device *d)
610{
611 int ret;
612 deb_info("%s:\n", __func__);
613
614 ret = af9015_init_endpoint(d);
615 if (ret)
616 goto error;
617
Antti Palosaari80619de2008-09-15 17:18:09 -0300618error:
619 return ret;
620}
621
622static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
623{
624 int ret;
625 deb_info("%s: onoff:%d\n", __func__, onoff);
626
627 if (onoff)
628 ret = af9015_set_reg_bit(adap->dev, 0xd503, 0);
629 else
630 ret = af9015_clear_reg_bit(adap->dev, 0xd503, 0);
631
632 return ret;
633}
634
635static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
636 int onoff)
637{
638 int ret;
639 u8 idx;
640
641 deb_info("%s: set pid filter, index %d, pid %x, onoff %d\n",
642 __func__, index, pid, onoff);
643
644 ret = af9015_write_reg(adap->dev, 0xd505, (pid & 0xff));
645 if (ret)
646 goto error;
647
648 ret = af9015_write_reg(adap->dev, 0xd506, (pid >> 8));
649 if (ret)
650 goto error;
651
652 idx = ((index & 0x1f) | (1 << 5));
653 ret = af9015_write_reg(adap->dev, 0xd504, idx);
654
655error:
656 return ret;
657}
658
659static int af9015_download_firmware(struct usb_device *udev,
660 const struct firmware *fw)
661{
662 int i, len, packets, remainder, ret;
663 struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
664 u16 addr = 0x5100; /* firmware start address */
665 u16 checksum = 0;
666
667 deb_info("%s:\n", __func__);
668
669 /* calc checksum */
670 for (i = 0; i < fw->size; i++)
671 checksum += fw->data[i];
672
673 af9015_config.firmware_size = fw->size;
674 af9015_config.firmware_checksum = checksum;
675
676 #define FW_PACKET_MAX_DATA 55
677
678 packets = fw->size / FW_PACKET_MAX_DATA;
679 remainder = fw->size % FW_PACKET_MAX_DATA;
680 len = FW_PACKET_MAX_DATA;
681 for (i = 0; i <= packets; i++) {
682 if (i == packets) /* set size of the last packet */
683 len = remainder;
684
685 req.data_len = len;
Antti Palosaari541dfa82008-10-06 13:57:45 -0300686 req.data = (u8 *)(fw->data + i * FW_PACKET_MAX_DATA);
Antti Palosaari80619de2008-09-15 17:18:09 -0300687 req.addr = addr;
688 addr += FW_PACKET_MAX_DATA;
689
690 ret = af9015_rw_udev(udev, &req);
691 if (ret) {
692 err("firmware download failed at packet %d with " \
693 "code %d", i, ret);
694 goto error;
695 }
696 }
Antti Palosaari80619de2008-09-15 17:18:09 -0300697
698 /* firmware loaded, request boot */
699 req.cmd = BOOT;
700 ret = af9015_rw_udev(udev, &req);
701 if (ret) {
702 err("firmware boot failed:%d", ret);
703 goto error;
704 }
705
Antti Palosaari80619de2008-09-15 17:18:09 -0300706error:
707 return ret;
708}
709
Antti Palosaari1cd72782010-10-12 17:22:32 -0300710struct af9015_rc_setup {
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300711 unsigned int id;
Antti Palosaari1cd72782010-10-12 17:22:32 -0300712 char *rc_codes;
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300713};
714
Antti Palosaari1cd72782010-10-12 17:22:32 -0300715static char *af9015_rc_setup_match(unsigned int id,
716 const struct af9015_rc_setup *table)
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300717{
Antti Palosaari1cd72782010-10-12 17:22:32 -0300718 for (; table->rc_codes; table++)
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300719 if (table->id == id)
Antti Palosaari1cd72782010-10-12 17:22:32 -0300720 return table->rc_codes;
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300721 return NULL;
722}
723
Antti Palosaari1cd72782010-10-12 17:22:32 -0300724static const struct af9015_rc_setup af9015_rc_setup_modparam[] = {
725 { AF9015_REMOTE_A_LINK_DTU_M, RC_MAP_ALINK_DTU_M },
726 { AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3, RC_MAP_MSI_DIGIVOX_II },
727 { AF9015_REMOTE_MYGICTV_U718, RC_MAP_TOTAL_MEDIA_IN_HAND },
728 { AF9015_REMOTE_DIGITTRADE_DVB_T, RC_MAP_DIGITTRADE },
729 { AF9015_REMOTE_AVERMEDIA_KS, RC_MAP_AVERMEDIA_RM_KS },
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300730 { }
731};
732
Antti Palosaari1cd72782010-10-12 17:22:32 -0300733static const struct af9015_rc_setup af9015_rc_setup_hashes[] = {
734 { 0xb8feb708, RC_MAP_MSI_DIGIVOX_II },
735 { 0xa3703d00, RC_MAP_ALINK_DTU_M },
736 { 0x9b7dc64e, RC_MAP_TOTAL_MEDIA_IN_HAND }, /* MYGICTV U718 */
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300737 { }
738};
739
Antti Palosaari1cd72782010-10-12 17:22:32 -0300740static const struct af9015_rc_setup af9015_rc_setup_usbids[] = {
741 { (USB_VID_TERRATEC << 16) + USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC,
742 RC_MAP_TERRATEC_SLIM },
743 { (USB_VID_VISIONPLUS << 16) + USB_PID_AZUREWAVE_AD_TU700,
744 RC_MAP_AZUREWAVE_AD_TU700 },
745 { (USB_VID_VISIONPLUS << 16) + USB_PID_TINYTWIN,
746 RC_MAP_AZUREWAVE_AD_TU700 },
747 { (USB_VID_MSI_2 << 16) + USB_PID_MSI_DIGI_VOX_MINI_III,
748 RC_MAP_MSI_DIGIVOX_III },
749 { (USB_VID_LEADTEK << 16) + USB_PID_WINFAST_DTV_DONGLE_GOLD,
750 RC_MAP_LEADTEK_Y04G0051 },
751 { (USB_VID_AVERMEDIA << 16) + USB_PID_AVERMEDIA_VOLAR_X,
752 RC_MAP_AVERMEDIA_M135A },
753 { (USB_VID_AFATECH << 16) + USB_PID_TREKSTOR_DVBT,
754 RC_MAP_TREKSTOR },
Antti Palosaaribd864ce2010-10-22 20:37:11 -0300755 { (USB_VID_KWORLD_2 << 16) + USB_PID_TINYTWIN_2,
756 RC_MAP_DIGITALNOW_TINYTWIN },
Jiri Slabye3a0cc62010-01-22 12:10:55 -0300757 { }
758};
759
Jiri Slaby634d2d72010-01-22 12:10:53 -0300760static void af9015_set_remote_config(struct usb_device *udev,
761 struct dvb_usb_device_properties *props)
762{
Antti Palosaari1cd72782010-10-12 17:22:32 -0300763 u16 vid = le16_to_cpu(udev->descriptor.idVendor);
764 u16 pid = le16_to_cpu(udev->descriptor.idProduct);
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300765
Antti Palosaari1cd72782010-10-12 17:22:32 -0300766 /* try to load remote based module param */
767 props->rc.core.rc_codes = af9015_rc_setup_match(
768 dvb_usb_af9015_remote, af9015_rc_setup_modparam);
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300769
Antti Palosaari1cd72782010-10-12 17:22:32 -0300770 /* try to load remote based eeprom hash */
771 if (!props->rc.core.rc_codes)
772 props->rc.core.rc_codes = af9015_rc_setup_match(
773 af9015_config.eeprom_sum, af9015_rc_setup_hashes);
Jiri Slabye3a0cc62010-01-22 12:10:55 -0300774
Antti Palosaari1cd72782010-10-12 17:22:32 -0300775 /* try to load remote based USB ID */
776 if (!props->rc.core.rc_codes)
777 props->rc.core.rc_codes = af9015_rc_setup_match(
778 (vid << 16) + pid, af9015_rc_setup_usbids);
779
780 /* try to load remote based USB iManufacturer string */
781 if (!props->rc.core.rc_codes && vid == USB_VID_AFATECH) {
782 /* Check USB manufacturer and product strings and try
783 to determine correct remote in case of chip vendor
784 reference IDs are used.
785 DO NOT ADD ANYTHING NEW HERE. Use hashes instead. */
786 char manufacturer[10];
787 memset(manufacturer, 0, sizeof(manufacturer));
788 usb_string(udev, udev->descriptor.iManufacturer,
789 manufacturer, sizeof(manufacturer));
790 if (!strcmp("MSI", manufacturer)) {
791 /* iManufacturer 1 MSI
792 iProduct 2 MSI K-VOX */
793 props->rc.core.rc_codes = af9015_rc_setup_match(
794 AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
795 af9015_rc_setup_modparam);
796 }
Jiri Slaby26c3b8b2010-01-22 12:10:54 -0300797 }
Antti Palosaari74c8e3a2010-10-22 18:45:18 -0300798
799 /* finally load "empty" just for leaving IR receiver enabled */
800 if (!props->rc.core.rc_codes)
801 props->rc.core.rc_codes = RC_MAP_EMPTY;
802
Antti Palosaari1cd72782010-10-12 17:22:32 -0300803 return;
Jiri Slaby634d2d72010-01-22 12:10:53 -0300804}
805
Antti Palosaari80619de2008-09-15 17:18:09 -0300806static int af9015_read_config(struct usb_device *udev)
807{
808 int ret;
809 u8 val, i, offset = 0;
810 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
Antti Palosaari80619de2008-09-15 17:18:09 -0300811
812 /* IR remote controller */
813 req.addr = AF9015_EEPROM_IR_MODE;
Antti Palosaarid1a470f2009-01-20 14:56:20 -0300814 /* first message will timeout often due to possible hw bug */
815 for (i = 0; i < 4; i++) {
816 ret = af9015_rw_udev(udev, &req);
817 if (!ret)
818 break;
819 }
Antti Palosaari80619de2008-09-15 17:18:09 -0300820 if (ret)
821 goto error;
Jiri Slaby6c614042010-01-22 12:10:52 -0300822
823 ret = af9015_eeprom_hash(udev);
824 if (ret)
825 goto error;
826
Antti Palosaari80619de2008-09-15 17:18:09 -0300827 deb_info("%s: IR mode:%d\n", __func__, val);
828 for (i = 0; i < af9015_properties_count; i++) {
Antti Palosaari74c8e3a2010-10-22 18:45:18 -0300829 if (val == AF9015_IR_MODE_DISABLED)
830 af9015_properties[i].rc.core.rc_codes = NULL;
831 else
Jiri Slaby634d2d72010-01-22 12:10:53 -0300832 af9015_set_remote_config(udev, &af9015_properties[i]);
Antti Palosaari80619de2008-09-15 17:18:09 -0300833 }
834
835 /* TS mode - one or two receivers */
836 req.addr = AF9015_EEPROM_TS_MODE;
837 ret = af9015_rw_udev(udev, &req);
838 if (ret)
839 goto error;
840 af9015_config.dual_mode = val;
841 deb_info("%s: TS mode:%d\n", __func__, af9015_config.dual_mode);
Antti Palosaari80619de2008-09-15 17:18:09 -0300842
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300843 /* Set adapter0 buffer size according to USB port speed, adapter1 buffer
844 size can be static because it is enabled only USB2.0 */
Antti Palosaari80619de2008-09-15 17:18:09 -0300845 for (i = 0; i < af9015_properties_count; i++) {
846 /* USB1.1 set smaller buffersize and disable 2nd adapter */
847 if (udev->speed == USB_SPEED_FULL) {
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300848 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
Antti Palosaari9c863272009-09-12 13:35:29 -0300849 = TS_USB11_FRAME_SIZE;
Antti Palosaari80619de2008-09-15 17:18:09 -0300850 /* disable 2nd adapter because we don't have
851 PID-filters */
852 af9015_config.dual_mode = 0;
853 } else {
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300854 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
Jose Alberto Reguero353330c2009-09-12 09:51:36 -0300855 = TS_USB20_FRAME_SIZE;
Antti Palosaari80619de2008-09-15 17:18:09 -0300856 }
857 }
858
859 if (af9015_config.dual_mode) {
860 /* read 2nd demodulator I2C address */
861 req.addr = AF9015_EEPROM_DEMOD2_I2C;
862 ret = af9015_rw_udev(udev, &req);
863 if (ret)
864 goto error;
865 af9015_af9013_config[1].demod_address = val;
866
867 /* enable 2nd adapter */
868 for (i = 0; i < af9015_properties_count; i++)
869 af9015_properties[i].num_adapters = 2;
870
871 } else {
872 /* disable 2nd adapter */
873 for (i = 0; i < af9015_properties_count; i++)
874 af9015_properties[i].num_adapters = 1;
875 }
876
877 for (i = 0; i < af9015_properties[0].num_adapters; i++) {
878 if (i == 1)
879 offset = AF9015_EEPROM_OFFSET;
880 /* xtal */
881 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
882 ret = af9015_rw_udev(udev, &req);
883 if (ret)
884 goto error;
885 switch (val) {
886 case 0:
887 af9015_af9013_config[i].adc_clock = 28800;
888 break;
889 case 1:
890 af9015_af9013_config[i].adc_clock = 20480;
891 break;
892 case 2:
893 af9015_af9013_config[i].adc_clock = 28000;
894 break;
895 case 3:
896 af9015_af9013_config[i].adc_clock = 25000;
897 break;
898 };
899 deb_info("%s: [%d] xtal:%d set adc_clock:%d\n", __func__, i,
900 val, af9015_af9013_config[i].adc_clock);
901
902 /* tuner IF */
903 req.addr = AF9015_EEPROM_IF1H + offset;
904 ret = af9015_rw_udev(udev, &req);
905 if (ret)
906 goto error;
907 af9015_af9013_config[i].tuner_if = val << 8;
908 req.addr = AF9015_EEPROM_IF1L + offset;
909 ret = af9015_rw_udev(udev, &req);
910 if (ret)
911 goto error;
912 af9015_af9013_config[i].tuner_if += val;
913 deb_info("%s: [%d] IF1:%d\n", __func__, i,
914 af9015_af9013_config[0].tuner_if);
915
916 /* MT2060 IF1 */
917 req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
918 ret = af9015_rw_udev(udev, &req);
919 if (ret)
920 goto error;
921 af9015_config.mt2060_if1[i] = val << 8;
922 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
923 ret = af9015_rw_udev(udev, &req);
924 if (ret)
925 goto error;
926 af9015_config.mt2060_if1[i] += val;
927 deb_info("%s: [%d] MT2060 IF1:%d\n", __func__, i,
928 af9015_config.mt2060_if1[i]);
929
930 /* tuner */
931 req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
932 ret = af9015_rw_udev(udev, &req);
933 if (ret)
934 goto error;
935 switch (val) {
936 case AF9013_TUNER_ENV77H11D5:
937 case AF9013_TUNER_MT2060:
Antti Palosaari80619de2008-09-15 17:18:09 -0300938 case AF9013_TUNER_QT1010:
939 case AF9013_TUNER_UNKNOWN:
940 case AF9013_TUNER_MT2060_2:
941 case AF9013_TUNER_TDA18271:
942 case AF9013_TUNER_QT1010A:
Antti Palosaariee3d4402010-08-13 03:51:26 -0300943 case AF9013_TUNER_TDA18218:
Antti Palosaari80619de2008-09-15 17:18:09 -0300944 af9015_af9013_config[i].rf_spec_inv = 1;
945 break;
946 case AF9013_TUNER_MXL5003D:
947 case AF9013_TUNER_MXL5005D:
948 case AF9013_TUNER_MXL5005R:
Antti Palosaariab07fdd2010-09-09 14:59:10 -0300949 case AF9013_TUNER_MXL5007T:
Antti Palosaari80619de2008-09-15 17:18:09 -0300950 af9015_af9013_config[i].rf_spec_inv = 0;
951 break;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300952 case AF9013_TUNER_MC44S803:
953 af9015_af9013_config[i].gpio[1] = AF9013_GPIO_LO;
954 af9015_af9013_config[i].rf_spec_inv = 1;
955 break;
Antti Palosaari80619de2008-09-15 17:18:09 -0300956 default:
957 warn("tuner id:%d not supported, please report!", val);
958 return -ENODEV;
959 };
960
961 af9015_af9013_config[i].tuner = val;
962 deb_info("%s: [%d] tuner id:%d\n", __func__, i, val);
963 }
964
965error:
966 if (ret)
967 err("eeprom read failed:%d", ret);
968
Antti Palosaari3956fef2009-03-31 17:01:02 -0300969 /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
Yann E. MORIN8ccdf1a2010-10-01 16:55:43 -0300970 content :-( Override some wrong values here. Ditto for the
971 AVerTV Red HD+ (A850T) device. */
Antti Palosaari3956fef2009-03-31 17:01:02 -0300972 if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
Antti Palosaari9a3ecc72010-10-07 21:37:06 -0300973 ((le16_to_cpu(udev->descriptor.idProduct) ==
974 USB_PID_AVERMEDIA_A850) ||
975 (le16_to_cpu(udev->descriptor.idProduct) ==
976 USB_PID_AVERMEDIA_A850T))) {
Antti Palosaari3956fef2009-03-31 17:01:02 -0300977 deb_info("%s: AverMedia A850: overriding config\n", __func__);
978 /* disable dual mode */
979 af9015_config.dual_mode = 0;
980 /* disable 2nd adapter */
981 for (i = 0; i < af9015_properties_count; i++)
982 af9015_properties[i].num_adapters = 1;
983
984 /* set correct IF */
985 af9015_af9013_config[0].tuner_if = 4570;
986 }
987
Antti Palosaari80619de2008-09-15 17:18:09 -0300988 return ret;
989}
990
991static int af9015_identify_state(struct usb_device *udev,
992 struct dvb_usb_device_properties *props,
993 struct dvb_usb_device_description **desc,
994 int *cold)
995{
996 int ret;
997 u8 reply;
998 struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
999
1000 ret = af9015_rw_udev(udev, &req);
1001 if (ret)
1002 return ret;
1003
1004 deb_info("%s: reply:%02x\n", __func__, reply);
1005 if (reply == 0x02)
1006 *cold = 0;
1007 else
1008 *cold = 1;
1009
1010 return ret;
1011}
1012
Antti Palosaari1cd72782010-10-12 17:22:32 -03001013static int af9015_rc_query(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -03001014{
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001015 struct af9015_state *priv = d->priv;
1016 int ret;
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001017 u8 buf[16];
Antti Palosaari80619de2008-09-15 17:18:09 -03001018
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001019 /* read registers needed to detect remote controller code */
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001020 ret = af9015_read_regs(d, 0x98d9, buf, sizeof(buf));
Antti Palosaari80619de2008-09-15 17:18:09 -03001021 if (ret)
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001022 goto error;
Antti Palosaari80619de2008-09-15 17:18:09 -03001023
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001024 if (buf[14] || buf[15]) {
1025 deb_rc("%s: key pressed %02x %02x %02x %02x\n", __func__,
1026 buf[12], buf[13], buf[14], buf[15]);
Antti Palosaari80619de2008-09-15 17:18:09 -03001027
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001028 /* clean IR code from mem */
1029 ret = af9015_write_regs(d, 0x98e5, "\x00\x00\x00\x00", 4);
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001030 if (ret)
1031 goto error;
1032
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001033 if (buf[14] == (u8) ~buf[15]) {
1034 if (buf[12] == (u8) ~buf[13]) {
Antti Palosaari1cd72782010-10-12 17:22:32 -03001035 /* NEC */
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001036 priv->rc_keycode = buf[12] << 8 | buf[14];
Antti Palosaari1cd72782010-10-12 17:22:32 -03001037 } else {
1038 /* NEC extended*/
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001039 priv->rc_keycode = buf[12] << 16 |
1040 buf[13] << 8 | buf[14];
Antti Palosaari1cd72782010-10-12 17:22:32 -03001041 }
1042 ir_keydown(d->rc_input_dev, priv->rc_keycode, 0);
1043 } else {
1044 priv->rc_keycode = 0; /* clear just for sure */
1045 }
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001046 } else if (priv->rc_repeat != buf[6] || buf[0]) {
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001047 deb_rc("%s: key repeated\n", __func__);
Antti Palosaari1cd72782010-10-12 17:22:32 -03001048 ir_keydown(d->rc_input_dev, priv->rc_keycode, 0);
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001049 } else {
1050 deb_rc("%s: no key press\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -03001051 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001052
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001053 priv->rc_repeat = buf[6];
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001054
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001055error:
1056 if (ret)
1057 err("%s: failed:%d", __func__, ret);
1058
1059 return ret;
Antti Palosaari80619de2008-09-15 17:18:09 -03001060}
1061
1062/* init 2nd I2C adapter */
Antti Palosaari349d0422008-11-05 16:31:24 -03001063static int af9015_i2c_init(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -03001064{
1065 int ret;
1066 struct af9015_state *state = d->priv;
1067 deb_info("%s:\n", __func__);
1068
1069 strncpy(state->i2c_adap.name, d->desc->name,
1070 sizeof(state->i2c_adap.name));
Antti Palosaari80619de2008-09-15 17:18:09 -03001071 state->i2c_adap.algo = d->props.i2c_algo;
1072 state->i2c_adap.algo_data = NULL;
1073 state->i2c_adap.dev.parent = &d->udev->dev;
1074
1075 i2c_set_adapdata(&state->i2c_adap, d);
1076
1077 ret = i2c_add_adapter(&state->i2c_adap);
1078 if (ret < 0)
1079 err("could not add i2c adapter");
1080
1081 return ret;
1082}
1083
1084static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
1085{
1086 int ret;
1087 struct af9015_state *state = adap->dev->priv;
1088 struct i2c_adapter *i2c_adap;
1089
1090 if (adap->id == 0) {
1091 /* select I2C adapter */
1092 i2c_adap = &adap->dev->i2c_adap;
1093
1094 deb_info("%s: init I2C\n", __func__);
1095 ret = af9015_i2c_init(adap->dev);
Antti Palosaari80619de2008-09-15 17:18:09 -03001096 } else {
1097 /* select I2C adapter */
1098 i2c_adap = &state->i2c_adap;
1099
1100 /* copy firmware to 2nd demodulator */
1101 if (af9015_config.dual_mode) {
1102 ret = af9015_copy_firmware(adap->dev);
1103 if (ret) {
1104 err("firmware copy to 2nd frontend " \
1105 "failed, will disable it");
1106 af9015_config.dual_mode = 0;
1107 return -ENODEV;
1108 }
1109 } else {
1110 return -ENODEV;
1111 }
1112 }
1113
1114 /* attach demodulator */
1115 adap->fe = dvb_attach(af9013_attach, &af9015_af9013_config[adap->id],
1116 i2c_adap);
1117
1118 return adap->fe == NULL ? -ENODEV : 0;
1119}
1120
1121static struct mt2060_config af9015_mt2060_config = {
1122 .i2c_address = 0xc0,
1123 .clock_out = 0,
1124};
1125
1126static struct qt1010_config af9015_qt1010_config = {
1127 .i2c_address = 0xc4,
1128};
1129
1130static struct tda18271_config af9015_tda18271_config = {
1131 .gate = TDA18271_GATE_DIGITAL,
1132 .small_i2c = 1,
1133};
1134
1135static struct mxl5005s_config af9015_mxl5003_config = {
1136 .i2c_address = 0xc6,
1137 .if_freq = IF_FREQ_4570000HZ,
1138 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1139 .agc_mode = MXL_SINGLE_AGC,
1140 .tracking_filter = MXL_TF_DEFAULT,
Antti Palosaaria1310772008-09-22 13:59:25 -03001141 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -03001142 .cap_select = MXL_CAP_SEL_ENABLE,
1143 .div_out = MXL_DIV_OUT_4,
1144 .clock_out = MXL_CLOCK_OUT_DISABLE,
1145 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1146 .top = MXL5005S_TOP_25P2,
1147 .mod_mode = MXL_DIGITAL_MODE,
1148 .if_mode = MXL_ZERO_IF,
1149 .AgcMasterByte = 0x00,
1150};
1151
1152static struct mxl5005s_config af9015_mxl5005_config = {
1153 .i2c_address = 0xc6,
1154 .if_freq = IF_FREQ_4570000HZ,
1155 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1156 .agc_mode = MXL_SINGLE_AGC,
1157 .tracking_filter = MXL_TF_OFF,
Antti Palosaaria1310772008-09-22 13:59:25 -03001158 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -03001159 .cap_select = MXL_CAP_SEL_ENABLE,
1160 .div_out = MXL_DIV_OUT_4,
1161 .clock_out = MXL_CLOCK_OUT_DISABLE,
1162 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1163 .top = MXL5005S_TOP_25P2,
1164 .mod_mode = MXL_DIGITAL_MODE,
1165 .if_mode = MXL_ZERO_IF,
1166 .AgcMasterByte = 0x00,
1167};
1168
Jochen Friedrichd5633992009-02-02 14:59:50 -03001169static struct mc44s803_config af9015_mc44s803_config = {
1170 .i2c_address = 0xc0,
1171 .dig_out = 1,
1172};
1173
Antti Palosaariee3d4402010-08-13 03:51:26 -03001174static struct tda18218_config af9015_tda18218_config = {
1175 .i2c_address = 0xc0,
1176 .i2c_wr_max = 21, /* max wr bytes AF9015 I2C adap can handle at once */
1177};
1178
Antti Palosaariab07fdd2010-09-09 14:59:10 -03001179static struct mxl5007t_config af9015_mxl5007t_config = {
1180 .xtal_freq_hz = MxL_XTAL_24_MHZ,
1181 .if_freq_hz = MxL_IF_4_57_MHZ,
1182};
1183
Antti Palosaari80619de2008-09-15 17:18:09 -03001184static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
1185{
1186 struct af9015_state *state = adap->dev->priv;
1187 struct i2c_adapter *i2c_adap;
1188 int ret;
Antti Palosaari9a3ecc72010-10-07 21:37:06 -03001189 deb_info("%s:\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -03001190
1191 /* select I2C adapter */
1192 if (adap->id == 0)
1193 i2c_adap = &adap->dev->i2c_adap;
1194 else
1195 i2c_adap = &state->i2c_adap;
1196
1197 switch (af9015_af9013_config[adap->id].tuner) {
1198 case AF9013_TUNER_MT2060:
1199 case AF9013_TUNER_MT2060_2:
1200 ret = dvb_attach(mt2060_attach, adap->fe, i2c_adap,
1201 &af9015_mt2060_config,
1202 af9015_config.mt2060_if1[adap->id])
1203 == NULL ? -ENODEV : 0;
1204 break;
1205 case AF9013_TUNER_QT1010:
1206 case AF9013_TUNER_QT1010A:
1207 ret = dvb_attach(qt1010_attach, adap->fe, i2c_adap,
1208 &af9015_qt1010_config) == NULL ? -ENODEV : 0;
1209 break;
1210 case AF9013_TUNER_TDA18271:
1211 ret = dvb_attach(tda18271_attach, adap->fe, 0xc0, i2c_adap,
1212 &af9015_tda18271_config) == NULL ? -ENODEV : 0;
1213 break;
Antti Palosaariee3d4402010-08-13 03:51:26 -03001214 case AF9013_TUNER_TDA18218:
1215 ret = dvb_attach(tda18218_attach, adap->fe, i2c_adap,
1216 &af9015_tda18218_config) == NULL ? -ENODEV : 0;
1217 break;
Antti Palosaari80619de2008-09-15 17:18:09 -03001218 case AF9013_TUNER_MXL5003D:
1219 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1220 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
1221 break;
1222 case AF9013_TUNER_MXL5005D:
1223 case AF9013_TUNER_MXL5005R:
1224 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1225 &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
1226 break;
1227 case AF9013_TUNER_ENV77H11D5:
1228 ret = dvb_attach(dvb_pll_attach, adap->fe, 0xc0, i2c_adap,
1229 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
1230 break;
1231 case AF9013_TUNER_MC44S803:
Jochen Friedrichd5633992009-02-02 14:59:50 -03001232 ret = dvb_attach(mc44s803_attach, adap->fe, i2c_adap,
1233 &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
Antti Palosaari80619de2008-09-15 17:18:09 -03001234 break;
Antti Palosaariab07fdd2010-09-09 14:59:10 -03001235 case AF9013_TUNER_MXL5007T:
1236 ret = dvb_attach(mxl5007t_attach, adap->fe, i2c_adap,
1237 0xc0, &af9015_mxl5007t_config) == NULL ? -ENODEV : 0;
1238 break;
Antti Palosaari80619de2008-09-15 17:18:09 -03001239 case AF9013_TUNER_UNKNOWN:
1240 default:
1241 ret = -ENODEV;
1242 err("Unknown tuner id:%d",
1243 af9015_af9013_config[adap->id].tuner);
1244 }
1245 return ret;
1246}
1247
1248static struct usb_device_id af9015_usb_table[] = {
1249/* 0 */{USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015)},
1250 {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016)},
1251 {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD)},
1252 {USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E)},
1253 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U)},
1254/* 5 */{USB_DEVICE(USB_VID_VISIONPLUS,
1255 USB_PID_TINYTWIN)},
1256 {USB_DEVICE(USB_VID_VISIONPLUS,
1257 USB_PID_AZUREWAVE_AD_TU700)},
1258 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2)},
1259 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T)},
1260 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X)},
1261/* 10 */{USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380)},
1262 {USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO)},
1263 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2)},
Antti Palosaaria3765882008-09-19 18:34:06 -03001264 {USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2)},
Antti Palosaari05c1cab2008-09-22 12:32:37 -03001265 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309)},
Herbert Graeber641015a2008-10-07 10:06:36 -03001266/* 15 */{USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III)},
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001267 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U)},
Antti Palosaari71bf2e02009-01-13 12:47:28 -03001268 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2)},
Antti Palosaari58fe1592009-03-26 20:41:05 -03001269 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3)},
Marc Schneider26144842009-03-26 21:07:18 -03001270 {USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT)},
Antti Palosaari1ed5fad2009-04-09 15:14:18 -03001271/* 20 */{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850)},
1272 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805)},
Marcel Jueling734dd232009-04-09 17:16:41 -03001273 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU)},
Wen-chien Jesse Sung6e9c1a22009-04-28 01:11:22 -03001274 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810)},
Antti Palosaari22d46452009-05-31 17:07:01 -03001275 {USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03)},
Mart Raudseppc92f0562009-07-24 13:45:41 -03001276/* 25 */{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2)},
Antti Palosaari486ba122009-09-18 13:37:57 -03001277 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T)},
Ignacio de Miguel Diaz52322632009-11-13 23:13:34 -03001278 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20)},
Antti Palosaarifa1df552010-02-10 20:05:48 -03001279 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2)},
Antti Palosaari809c1e82010-02-10 20:07:30 -03001280 {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS)},
Antti Palosaariab9b4f22010-03-01 13:50:40 -03001281/* 30 */{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T)},
Antti Palosaari7fc87092010-03-01 14:06:52 -03001282 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4)},
Antti Palosaari2606cfa2010-05-23 18:26:37 -03001283 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M)},
Stefan Lippers-Hollmannd7ef4852010-08-25 10:08:48 -03001284 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_RC)},
Antti Palosaariab07fdd2010-09-09 14:59:10 -03001285 {USB_DEVICE(USB_VID_TERRATEC,
1286 USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC)},
Yann E. MORIN8ccdf1a2010-10-01 16:55:43 -03001287/* 35 */{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850T)},
Antti Palosaari80619de2008-09-15 17:18:09 -03001288 {0},
1289};
1290MODULE_DEVICE_TABLE(usb, af9015_usb_table);
1291
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001292#define AF9015_RC_INTERVAL 500
Antti Palosaari80619de2008-09-15 17:18:09 -03001293static struct dvb_usb_device_properties af9015_properties[] = {
1294 {
1295 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1296
1297 .usb_ctrl = DEVICE_SPECIFIC,
1298 .download_firmware = af9015_download_firmware,
1299 .firmware = "dvb-usb-af9015.fw",
Jose Alberto Reguerocce25712008-11-13 14:14:18 -03001300 .no_reconnect = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001301
Antti Palosaari02542942009-09-16 20:33:03 -03001302 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari80619de2008-09-15 17:18:09 -03001303
1304 .num_adapters = 2,
1305 .adapter = {
1306 {
1307 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1308 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1309
1310 .pid_filter_count = 32,
1311 .pid_filter = af9015_pid_filter,
1312 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1313
1314 .frontend_attach =
1315 af9015_af9013_frontend_attach,
1316 .tuner_attach = af9015_tuner_attach,
1317 .stream = {
1318 .type = USB_BULK,
1319 .count = 6,
1320 .endpoint = 0x84,
1321 },
1322 },
1323 {
1324 .frontend_attach =
1325 af9015_af9013_frontend_attach,
1326 .tuner_attach = af9015_tuner_attach,
1327 .stream = {
1328 .type = USB_BULK,
1329 .count = 6,
1330 .endpoint = 0x85,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001331 .u = {
1332 .bulk = {
1333 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001334 TS_USB20_FRAME_SIZE,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001335 }
1336 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001337 },
1338 }
1339 },
1340
1341 .identify_state = af9015_identify_state,
1342
Antti Palosaari1cd72782010-10-12 17:22:32 -03001343 .rc.core = {
1344 .protocol = IR_TYPE_NEC,
1345 .module_name = "af9015",
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001346 .rc_query = af9015_rc_query,
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001347 .rc_interval = AF9015_RC_INTERVAL,
Antti Palosaari1cd72782010-10-12 17:22:32 -03001348 .rc_props = {
1349 .allowed_protos = IR_TYPE_NEC,
1350 },
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001351 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001352
1353 .i2c_algo = &af9015_i2c_algo,
1354
Yann E. MORIN8ccdf1a2010-10-01 16:55:43 -03001355 .num_device_descs = 12, /* check max from dvb-usb.h */
Antti Palosaari80619de2008-09-15 17:18:09 -03001356 .devices = {
1357 {
1358 .name = "Afatech AF9015 DVB-T USB2.0 stick",
1359 .cold_ids = {&af9015_usb_table[0],
1360 &af9015_usb_table[1], NULL},
1361 .warm_ids = {NULL},
1362 },
1363 {
1364 .name = "Leadtek WinFast DTV Dongle Gold",
1365 .cold_ids = {&af9015_usb_table[2], NULL},
1366 .warm_ids = {NULL},
1367 },
1368 {
1369 .name = "Pinnacle PCTV 71e",
1370 .cold_ids = {&af9015_usb_table[3], NULL},
1371 .warm_ids = {NULL},
1372 },
1373 {
1374 .name = "KWorld PlusTV Dual DVB-T Stick " \
1375 "(DVB-T 399U)",
Mart Raudseppc92f0562009-07-24 13:45:41 -03001376 .cold_ids = {&af9015_usb_table[4],
1377 &af9015_usb_table[25], NULL},
Antti Palosaari80619de2008-09-15 17:18:09 -03001378 .warm_ids = {NULL},
1379 },
1380 {
1381 .name = "DigitalNow TinyTwin DVB-T Receiver",
Antti Palosaarifa1df552010-02-10 20:05:48 -03001382 .cold_ids = {&af9015_usb_table[5],
1383 &af9015_usb_table[28], NULL},
Antti Palosaari80619de2008-09-15 17:18:09 -03001384 .warm_ids = {NULL},
1385 },
1386 {
1387 .name = "TwinHan AzureWave AD-TU700(704J)",
1388 .cold_ids = {&af9015_usb_table[6], NULL},
1389 .warm_ids = {NULL},
1390 },
1391 {
1392 .name = "TerraTec Cinergy T USB XE",
1393 .cold_ids = {&af9015_usb_table[7], NULL},
1394 .warm_ids = {NULL},
1395 },
1396 {
1397 .name = "KWorld PlusTV Dual DVB-T PCI " \
1398 "(DVB-T PC160-2T)",
1399 .cold_ids = {&af9015_usb_table[8], NULL},
1400 .warm_ids = {NULL},
1401 },
1402 {
1403 .name = "AVerMedia AVerTV DVB-T Volar X",
1404 .cold_ids = {&af9015_usb_table[9], NULL},
1405 .warm_ids = {NULL},
1406 },
Antti Palosaari76391a72010-09-09 12:10:50 -03001407 {
1408 .name = "TerraTec Cinergy T Stick RC",
1409 .cold_ids = {&af9015_usb_table[33], NULL},
1410 .warm_ids = {NULL},
1411 },
Antti Palosaariab07fdd2010-09-09 14:59:10 -03001412 {
1413 .name = "TerraTec Cinergy T Stick Dual RC",
1414 .cold_ids = {&af9015_usb_table[34], NULL},
1415 .warm_ids = {NULL},
1416 },
Yann E. MORIN8ccdf1a2010-10-01 16:55:43 -03001417 {
1418 .name = "AverMedia AVerTV Red HD+ (A850T)",
1419 .cold_ids = {&af9015_usb_table[35], NULL},
1420 .warm_ids = {NULL},
1421 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001422 }
1423 }, {
1424 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1425
1426 .usb_ctrl = DEVICE_SPECIFIC,
1427 .download_firmware = af9015_download_firmware,
1428 .firmware = "dvb-usb-af9015.fw",
Jose Alberto Reguerocce25712008-11-13 14:14:18 -03001429 .no_reconnect = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001430
Antti Palosaari02542942009-09-16 20:33:03 -03001431 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari80619de2008-09-15 17:18:09 -03001432
1433 .num_adapters = 2,
1434 .adapter = {
1435 {
1436 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1437 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1438
1439 .pid_filter_count = 32,
1440 .pid_filter = af9015_pid_filter,
1441 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1442
1443 .frontend_attach =
1444 af9015_af9013_frontend_attach,
1445 .tuner_attach = af9015_tuner_attach,
1446 .stream = {
1447 .type = USB_BULK,
1448 .count = 6,
1449 .endpoint = 0x84,
1450 },
1451 },
1452 {
1453 .frontend_attach =
1454 af9015_af9013_frontend_attach,
1455 .tuner_attach = af9015_tuner_attach,
1456 .stream = {
1457 .type = USB_BULK,
1458 .count = 6,
1459 .endpoint = 0x85,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001460 .u = {
1461 .bulk = {
1462 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001463 TS_USB20_FRAME_SIZE,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001464 }
1465 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001466 },
1467 }
1468 },
1469
1470 .identify_state = af9015_identify_state,
1471
Antti Palosaari1cd72782010-10-12 17:22:32 -03001472 .rc.core = {
1473 .protocol = IR_TYPE_NEC,
1474 .module_name = "af9015",
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001475 .rc_query = af9015_rc_query,
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001476 .rc_interval = AF9015_RC_INTERVAL,
Antti Palosaari1cd72782010-10-12 17:22:32 -03001477 .rc_props = {
1478 .allowed_protos = IR_TYPE_NEC,
1479 },
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001480 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001481
1482 .i2c_algo = &af9015_i2c_algo,
1483
Antti Palosaaria44b91d2010-09-09 12:05:31 -03001484 .num_device_descs = 9, /* check max from dvb-usb.h */
Antti Palosaari80619de2008-09-15 17:18:09 -03001485 .devices = {
1486 {
1487 .name = "Xtensions XD-380",
1488 .cold_ids = {&af9015_usb_table[10], NULL},
1489 .warm_ids = {NULL},
1490 },
1491 {
1492 .name = "MSI DIGIVOX Duo",
1493 .cold_ids = {&af9015_usb_table[11], NULL},
1494 .warm_ids = {NULL},
1495 },
1496 {
1497 .name = "Fujitsu-Siemens Slim Mobile USB DVB-T",
1498 .cold_ids = {&af9015_usb_table[12], NULL},
1499 .warm_ids = {NULL},
1500 },
Mikko Ohtamaa111f9ec2008-09-19 18:26:05 -03001501 {
Antti Palosaaria3765882008-09-19 18:34:06 -03001502 .name = "Telestar Starstick 2",
Mikko Ohtamaa111f9ec2008-09-19 18:26:05 -03001503 .cold_ids = {&af9015_usb_table[13], NULL},
1504 .warm_ids = {NULL},
1505 },
Antti Palosaari05c1cab2008-09-22 12:32:37 -03001506 {
1507 .name = "AVerMedia A309",
1508 .cold_ids = {&af9015_usb_table[14], NULL},
1509 .warm_ids = {NULL},
1510 },
Herbert Graeber641015a2008-10-07 10:06:36 -03001511 {
1512 .name = "MSI Digi VOX mini III",
1513 .cold_ids = {&af9015_usb_table[15], NULL},
1514 .warm_ids = {NULL},
1515 },
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001516 {
1517 .name = "KWorld USB DVB-T TV Stick II " \
1518 "(VS-DVB-T 395U)",
Antti Palosaari71bf2e02009-01-13 12:47:28 -03001519 .cold_ids = {&af9015_usb_table[16],
Antti Palosaari58fe1592009-03-26 20:41:05 -03001520 &af9015_usb_table[17],
Antti Palosaari7fc87092010-03-01 14:06:52 -03001521 &af9015_usb_table[18],
1522 &af9015_usb_table[31], NULL},
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001523 .warm_ids = {NULL},
1524 },
Marc Schneider26144842009-03-26 21:07:18 -03001525 {
1526 .name = "TrekStor DVB-T USB Stick",
1527 .cold_ids = {&af9015_usb_table[19], NULL},
1528 .warm_ids = {NULL},
1529 },
Antti Palosaari3956fef2009-03-31 17:01:02 -03001530 {
1531 .name = "AverMedia AVerTV Volar Black HD " \
1532 "(A850)",
1533 .cold_ids = {&af9015_usb_table[20], NULL},
1534 .warm_ids = {NULL},
1535 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001536 }
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001537 }, {
1538 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1539
1540 .usb_ctrl = DEVICE_SPECIFIC,
1541 .download_firmware = af9015_download_firmware,
1542 .firmware = "dvb-usb-af9015.fw",
1543 .no_reconnect = 1,
1544
Antti Palosaari02542942009-09-16 20:33:03 -03001545 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001546
1547 .num_adapters = 2,
1548 .adapter = {
1549 {
1550 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1551 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1552
1553 .pid_filter_count = 32,
1554 .pid_filter = af9015_pid_filter,
1555 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1556
1557 .frontend_attach =
1558 af9015_af9013_frontend_attach,
1559 .tuner_attach = af9015_tuner_attach,
1560 .stream = {
1561 .type = USB_BULK,
1562 .count = 6,
1563 .endpoint = 0x84,
1564 },
1565 },
1566 {
1567 .frontend_attach =
1568 af9015_af9013_frontend_attach,
1569 .tuner_attach = af9015_tuner_attach,
1570 .stream = {
1571 .type = USB_BULK,
1572 .count = 6,
1573 .endpoint = 0x85,
1574 .u = {
1575 .bulk = {
1576 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001577 TS_USB20_FRAME_SIZE,
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001578 }
1579 }
1580 },
1581 }
1582 },
1583
1584 .identify_state = af9015_identify_state,
1585
Antti Palosaari1cd72782010-10-12 17:22:32 -03001586 .rc.core = {
1587 .protocol = IR_TYPE_NEC,
1588 .module_name = "af9015",
Antti Palosaari74c8e3a2010-10-22 18:45:18 -03001589 .rc_query = af9015_rc_query,
Antti Palosaarid3bb73d2010-09-12 13:31:56 -03001590 .rc_interval = AF9015_RC_INTERVAL,
Antti Palosaari1cd72782010-10-12 17:22:32 -03001591 .rc_props = {
1592 .allowed_protos = IR_TYPE_NEC,
1593 },
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -03001594 },
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001595
1596 .i2c_algo = &af9015_i2c_algo,
1597
Antti Palosaaria44b91d2010-09-09 12:05:31 -03001598 .num_device_descs = 9, /* check max from dvb-usb.h */
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001599 .devices = {
Antti Palosaari1ed5fad2009-04-09 15:14:18 -03001600 {
1601 .name = "AverMedia AVerTV Volar GPS 805 (A805)",
1602 .cold_ids = {&af9015_usb_table[21], NULL},
1603 .warm_ids = {NULL},
1604 },
Marcel Jueling734dd232009-04-09 17:16:41 -03001605 {
1606 .name = "Conceptronic USB2.0 DVB-T CTVDIGRCU " \
1607 "V3.0",
1608 .cold_ids = {&af9015_usb_table[22], NULL},
1609 .warm_ids = {NULL},
1610 },
Wen-chien Jesse Sung6e9c1a22009-04-28 01:11:22 -03001611 {
1612 .name = "KWorld Digial MC-810",
1613 .cold_ids = {&af9015_usb_table[23], NULL},
1614 .warm_ids = {NULL},
1615 },
Antti Palosaari22d46452009-05-31 17:07:01 -03001616 {
1617 .name = "Genius TVGo DVB-T03",
1618 .cold_ids = {&af9015_usb_table[24], NULL},
1619 .warm_ids = {NULL},
1620 },
Antti Palosaari486ba122009-09-18 13:37:57 -03001621 {
1622 .name = "KWorld PlusTV DVB-T PCI Pro Card " \
1623 "(DVB-T PC160-T)",
1624 .cold_ids = {&af9015_usb_table[26], NULL},
1625 .warm_ids = {NULL},
1626 },
Ignacio de Miguel Diaz52322632009-11-13 23:13:34 -03001627 {
1628 .name = "Sveon STV20 Tuner USB DVB-T HDTV",
1629 .cold_ids = {&af9015_usb_table[27], NULL},
1630 .warm_ids = {NULL},
1631 },
Antti Palosaari809c1e82010-02-10 20:07:30 -03001632 {
1633 .name = "Leadtek WinFast DTV2000DS",
1634 .cold_ids = {&af9015_usb_table[29], NULL},
1635 .warm_ids = {NULL},
1636 },
Antti Palosaariab9b4f22010-03-01 13:50:40 -03001637 {
1638 .name = "KWorld USB DVB-T Stick Mobile " \
1639 "(UB383-T)",
1640 .cold_ids = {&af9015_usb_table[30], NULL},
1641 .warm_ids = {NULL},
1642 },
Antti Palosaari2606cfa2010-05-23 18:26:37 -03001643 {
1644 .name = "AverMedia AVerTV Volar M (A815Mac)",
1645 .cold_ids = {&af9015_usb_table[32], NULL},
1646 .warm_ids = {NULL},
1647 },
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001648 }
1649 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001650};
Antti Palosaari80619de2008-09-15 17:18:09 -03001651
1652static int af9015_usb_probe(struct usb_interface *intf,
1653 const struct usb_device_id *id)
1654{
1655 int ret = 0;
1656 struct dvb_usb_device *d = NULL;
1657 struct usb_device *udev = interface_to_usbdev(intf);
1658 u8 i;
1659
1660 deb_info("%s: interface:%d\n", __func__,
1661 intf->cur_altsetting->desc.bInterfaceNumber);
1662
1663 /* interface 0 is used by DVB-T receiver and
1664 interface 1 is for remote controller (HID) */
1665 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
1666 ret = af9015_read_config(udev);
1667 if (ret)
1668 return ret;
1669
1670 for (i = 0; i < af9015_properties_count; i++) {
1671 ret = dvb_usb_device_init(intf, &af9015_properties[i],
1672 THIS_MODULE, &d, adapter_nr);
1673 if (!ret)
1674 break;
1675 if (ret != -ENODEV)
1676 return ret;
1677 }
1678 if (ret)
1679 return ret;
1680
1681 if (d)
1682 ret = af9015_init(d);
1683 }
1684
1685 return ret;
1686}
1687
Antti Palosaari349d0422008-11-05 16:31:24 -03001688static void af9015_i2c_exit(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -03001689{
1690 struct af9015_state *state = d->priv;
Antti Palosaari9a3ecc72010-10-07 21:37:06 -03001691 deb_info("%s:\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -03001692
1693 /* remove 2nd I2C adapter */
1694 if (d->state & DVB_USB_STATE_I2C)
1695 i2c_del_adapter(&state->i2c_adap);
1696}
1697
1698static void af9015_usb_device_exit(struct usb_interface *intf)
1699{
1700 struct dvb_usb_device *d = usb_get_intfdata(intf);
Antti Palosaari9a3ecc72010-10-07 21:37:06 -03001701 deb_info("%s:\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -03001702
1703 /* remove 2nd I2C adapter */
1704 if (d != NULL && d->desc != NULL)
1705 af9015_i2c_exit(d);
1706
1707 dvb_usb_device_exit(intf);
1708}
1709
1710/* usb specific object needed to register this driver with the usb subsystem */
1711static struct usb_driver af9015_usb_driver = {
1712 .name = "dvb_usb_af9015",
1713 .probe = af9015_usb_probe,
1714 .disconnect = af9015_usb_device_exit,
1715 .id_table = af9015_usb_table,
1716};
1717
1718/* module stuff */
1719static int __init af9015_usb_module_init(void)
1720{
1721 int ret;
1722 ret = usb_register(&af9015_usb_driver);
1723 if (ret)
1724 err("module init failed:%d", ret);
1725
1726 return ret;
1727}
1728
1729static void __exit af9015_usb_module_exit(void)
1730{
1731 /* deregister this driver from the USB subsystem */
1732 usb_deregister(&af9015_usb_driver);
1733}
1734
1735module_init(af9015_usb_module_init);
1736module_exit(af9015_usb_module_exit);
1737
1738MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1739MODULE_DESCRIPTION("Driver for Afatech AF9015 DVB-T");
1740MODULE_LICENSE("GPL");