[media] af9015: correct few error codes
[deliverable/linux.git] / drivers / media / usb / dvb-usb-v2 / af9015.c
CommitLineData
80619de8
AP
1/*
2 * DVB USB Linux driver for Afatech AF9015 DVB-T USB2.0 receiver
3 *
4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
5 *
6 * Thanks to Afatech who kindly provided information.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 */
23
24#include "af9015.h"
80619de8 25
349d042f 26static int dvb_usb_af9015_remote;
80619de8
AP
27module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
28MODULE_PARM_DESC(remote, "select remote");
80619de8
AP
29DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
30
a3645e54 31static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
80619de8 32{
06565d7a
AP
33#define BUF_LEN 63
34#define REQ_HDR_LEN 8 /* send header size */
35#define ACK_HDR_LEN 2 /* rece header size */
e8089661 36 struct af9015_state *state = d_to_priv(d);
a3645e54 37 int ret, wlen, rlen;
06565d7a 38 u8 buf[BUF_LEN];
80619de8 39 u8 write = 1;
80619de8
AP
40
41 buf[0] = req->cmd;
a3645e54 42 buf[1] = state->seq++;
80619de8
AP
43 buf[2] = req->i2c_addr;
44 buf[3] = req->addr >> 8;
45 buf[4] = req->addr & 0xff;
46 buf[5] = req->mbox;
47 buf[6] = req->addr_len;
48 buf[7] = req->data_len;
49
50 switch (req->cmd) {
51 case GET_CONFIG:
80619de8
AP
52 case READ_MEMORY:
53 case RECONNECT_USB:
80619de8
AP
54 write = 0;
55 break;
56 case READ_I2C:
57 write = 0;
58 buf[2] |= 0x01; /* set I2C direction */
59 case WRITE_I2C:
60 buf[0] = READ_WRITE_I2C;
61 break;
62 case WRITE_MEMORY:
63 if (((req->addr & 0xff00) == 0xff00) ||
f4e96deb 64 ((req->addr & 0xff00) == 0xae00))
80619de8
AP
65 buf[0] = WRITE_VIRTUAL_MEMORY;
66 case WRITE_VIRTUAL_MEMORY:
67 case COPY_FIRMWARE:
68 case DOWNLOAD_FIRMWARE:
ba1bc642 69 case BOOT:
80619de8
AP
70 break;
71 default:
f224749b
AP
72 dev_err(&d->udev->dev, "%s: unknown command=%d\n",
73 KBUILD_MODNAME, req->cmd);
e1e9e510 74 ret = -EIO;
a3645e54 75 goto error;
80619de8
AP
76 }
77
06565d7a
AP
78 /* buffer overflow check */
79 if ((write && (req->data_len > BUF_LEN - REQ_HDR_LEN)) ||
f224749b
AP
80 (!write && (req->data_len > BUF_LEN - ACK_HDR_LEN))) {
81 dev_err(&d->udev->dev, "%s: too much data; cmd=%d len=%d\n",
82 KBUILD_MODNAME, req->cmd, req->data_len);
06565d7a 83 ret = -EINVAL;
a3645e54 84 goto error;
06565d7a
AP
85 }
86
a3645e54
AP
87 /* write receives seq + status = 2 bytes
88 read receives seq + status + data = 2 + N bytes */
89 wlen = REQ_HDR_LEN;
90 rlen = ACK_HDR_LEN;
80619de8 91 if (write) {
a3645e54 92 wlen += req->data_len;
06565d7a 93 memcpy(&buf[REQ_HDR_LEN], req->data, req->data_len);
a3645e54
AP
94 } else {
95 rlen += req->data_len;
80619de8 96 }
06565d7a 97
a3645e54 98 /* no ack for these packets */
80619de8 99 if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
a3645e54 100 rlen = 0;
80619de8 101
1162c7b3 102 ret = dvb_usbv2_generic_rw(d, buf, wlen, buf, rlen);
a3645e54
AP
103 if (ret)
104 goto error;
80619de8 105
80619de8 106 /* check status */
a3645e54 107 if (rlen && buf[1]) {
f224749b
AP
108 dev_err(&d->udev->dev, "%s: command failed=%d\n",
109 KBUILD_MODNAME, buf[1]);
e1e9e510 110 ret = -EIO;
a3645e54 111 goto error;
80619de8
AP
112 }
113
114 /* read request, copy returned data to return buf */
115 if (!write)
06565d7a 116 memcpy(req->data, &buf[ACK_HDR_LEN], req->data_len);
a3645e54 117error:
80619de8
AP
118 return ret;
119}
120
80619de8
AP
121static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
122 u8 len)
123{
124 struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
125 val};
126 return af9015_ctrl_msg(d, &req);
127}
128
74c8e3ad 129static int af9015_read_regs(struct dvb_usb_device *d, u16 addr, u8 *val, u8 len)
80619de8 130{
74c8e3ad
AP
131 struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
132 val};
80619de8
AP
133 return af9015_ctrl_msg(d, &req);
134}
135
a3645e54
AP
136static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
137{
138 return af9015_write_regs(d, addr, &val, 1);
139}
140
74c8e3ad
AP
141static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
142{
143 return af9015_read_regs(d, addr, val, 1);
144}
145
80619de8
AP
146static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
147 u8 val)
148{
e8089661 149 struct af9015_state *state = d_to_priv(d);
80619de8
AP
150 struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
151
a3645e54
AP
152 if (addr == state->af9013_config[0].i2c_addr ||
153 addr == state->af9013_config[1].i2c_addr)
80619de8
AP
154 req.addr_len = 3;
155
156 return af9015_ctrl_msg(d, &req);
157}
158
159static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
160 u8 *val)
161{
e8089661 162 struct af9015_state *state = d_to_priv(d);
80619de8
AP
163 struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
164
a3645e54
AP
165 if (addr == state->af9013_config[0].i2c_addr ||
166 addr == state->af9013_config[1].i2c_addr)
80619de8
AP
167 req.addr_len = 3;
168
169 return af9015_ctrl_msg(d, &req);
170}
171
a3645e54
AP
172static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
173{
174 int ret;
175 u8 val, mask = 0x01;
176
177 ret = af9015_read_reg(d, addr, &val);
178 if (ret)
179 return ret;
180
181 mask <<= bit;
182 if (op) {
183 /* set bit */
184 val |= mask;
185 } else {
186 /* clear bit */
187 mask ^= 0xff;
188 val &= mask;
189 }
190
191 return af9015_write_reg(d, addr, val);
192}
193
194static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
195{
196 return af9015_do_reg_bit(d, addr, bit, 1);
197}
198
199static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
200{
201 return af9015_do_reg_bit(d, addr, bit, 0);
202}
203
80619de8
AP
204static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
205 int num)
206{
207 struct dvb_usb_device *d = i2c_get_adapdata(adap);
e8089661 208 struct af9015_state *state = d_to_priv(d);
80619de8
AP
209 int ret = 0, i = 0;
210 u16 addr;
675375d7 211 u8 uninitialized_var(mbox), addr_len;
80619de8
AP
212 struct req_t req;
213
bc050e67 214/*
80619de8
AP
215The bus lock is needed because there is two tuners both using same I2C-address.
216Due to that the only way to select correct tuner is use demodulator I2C-gate.
217
218................................................
219. AF9015 includes integrated AF9013 demodulator.
220. ____________ ____________ . ____________
221.| uC | | demod | . | tuner |
222.|------------| |------------| . |------------|
223.| AF9015 | | AF9013/5 | . | MXL5003 |
224.| |--+----I2C-------|-----/ -----|-.-----I2C-------| |
225.| | | | addr 0x38 | . | addr 0xc6 |
226.|____________| | |____________| . |____________|
227.................|..............................
228 | ____________ ____________
229 | | demod | | tuner |
230 | |------------| |------------|
231 | | AF9013 | | MXL5003 |
232 +----I2C-------|-----/ -----|-------I2C-------| |
233 | addr 0x3a | | addr 0xc6 |
234 |____________| |____________|
235*/
236 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
237 return -EAGAIN;
238
239 while (i < num) {
a3645e54
AP
240 if (msg[i].addr == state->af9013_config[0].i2c_addr ||
241 msg[i].addr == state->af9013_config[1].i2c_addr) {
80619de8
AP
242 addr = msg[i].buf[0] << 8;
243 addr += msg[i].buf[1];
244 mbox = msg[i].buf[2];
245 addr_len = 3;
246 } else {
247 addr = msg[i].buf[0];
248 addr_len = 1;
675375d7 249 /* mbox is don't care in that case */
80619de8
AP
250 }
251
252 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
709d9208
AP
253 if (msg[i].len > 3 || msg[i+1].len > 61) {
254 ret = -EOPNOTSUPP;
255 goto error;
256 }
a3645e54 257 if (msg[i].addr == state->af9013_config[0].i2c_addr)
80619de8
AP
258 req.cmd = READ_MEMORY;
259 else
260 req.cmd = READ_I2C;
261 req.i2c_addr = msg[i].addr;
262 req.addr = addr;
263 req.mbox = mbox;
264 req.addr_len = addr_len;
265 req.data_len = msg[i+1].len;
266 req.data = &msg[i+1].buf[0];
267 ret = af9015_ctrl_msg(d, &req);
268 i += 2;
d5633998 269 } else if (msg[i].flags & I2C_M_RD) {
709d9208
AP
270 if (msg[i].len > 61) {
271 ret = -EOPNOTSUPP;
272 goto error;
273 }
a3645e54 274 if (msg[i].addr == state->af9013_config[0].i2c_addr) {
16b2dc2a 275 ret = -EINVAL;
d5633998 276 goto error;
16b2dc2a
AP
277 }
278 req.cmd = READ_I2C;
d5633998
JF
279 req.i2c_addr = msg[i].addr;
280 req.addr = addr;
281 req.mbox = mbox;
282 req.addr_len = addr_len;
283 req.data_len = msg[i].len;
284 req.data = &msg[i].buf[0];
285 ret = af9015_ctrl_msg(d, &req);
286 i += 1;
80619de8 287 } else {
709d9208
AP
288 if (msg[i].len > 21) {
289 ret = -EOPNOTSUPP;
290 goto error;
291 }
a3645e54 292 if (msg[i].addr == state->af9013_config[0].i2c_addr)
80619de8
AP
293 req.cmd = WRITE_MEMORY;
294 else
295 req.cmd = WRITE_I2C;
296 req.i2c_addr = msg[i].addr;
297 req.addr = addr;
298 req.mbox = mbox;
299 req.addr_len = addr_len;
300 req.data_len = msg[i].len-addr_len;
301 req.data = &msg[i].buf[addr_len];
302 ret = af9015_ctrl_msg(d, &req);
303 i += 1;
304 }
305 if (ret)
306 goto error;
307
308 }
309 ret = i;
310
311error:
312 mutex_unlock(&d->i2c_mutex);
313
314 return ret;
315}
316
317static u32 af9015_i2c_func(struct i2c_adapter *adapter)
318{
319 return I2C_FUNC_I2C;
320}
321
322static struct i2c_algorithm af9015_i2c_algo = {
323 .master_xfer = af9015_i2c_xfer,
324 .functionality = af9015_i2c_func,
325};
326
a0921af7 327static int af9015_identify_state(struct dvb_usb_device *d, const char **name)
80619de8
AP
328{
329 int ret;
a3645e54
AP
330 u8 reply;
331 struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
80619de8 332
a3645e54 333 ret = af9015_ctrl_msg(d, &req);
80619de8
AP
334 if (ret)
335 return ret;
336
f224749b
AP
337 dev_dbg(&d->udev->dev, "%s: reply=%02x\n", __func__, reply);
338
a3645e54
AP
339 if (reply == 0x02)
340 ret = WARM;
341 else
342 ret = COLD;
80619de8 343
a3645e54 344 return ret;
80619de8
AP
345}
346
a3645e54
AP
347static int af9015_download_firmware(struct dvb_usb_device *d,
348 const struct firmware *fw)
80619de8 349{
e8089661 350 struct af9015_state *state = d_to_priv(d);
a3645e54
AP
351 int i, len, remaining, ret;
352 struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
353 u16 checksum = 0;
f224749b 354 dev_dbg(&d->udev->dev, "%s:\n", __func__);
80619de8 355
a3645e54
AP
356 /* calc checksum */
357 for (i = 0; i < fw->size; i++)
358 checksum += fw->data[i];
80619de8 359
a3645e54
AP
360 state->firmware_size = fw->size;
361 state->firmware_checksum = checksum;
80619de8 362
a3645e54
AP
363 #define FW_ADDR 0x5100 /* firmware start address */
364 #define LEN_MAX 55 /* max packet size */
365 for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
366 len = remaining;
367 if (len > LEN_MAX)
368 len = LEN_MAX;
80619de8 369
a3645e54
AP
370 req.data_len = len;
371 req.data = (u8 *) &fw->data[fw->size - remaining];
372 req.addr = FW_ADDR + fw->size - remaining;
80619de8 373
a3645e54
AP
374 ret = af9015_ctrl_msg(d, &req);
375 if (ret) {
f224749b
AP
376 dev_err(&d->udev->dev,
377 "%s: firmware download failed=%d\n",
378 KBUILD_MODNAME, ret);
a3645e54
AP
379 goto error;
380 }
80619de8
AP
381 }
382
a3645e54
AP
383 /* firmware loaded, request boot */
384 req.cmd = BOOT;
385 req.data_len = 0;
386 ret = af9015_ctrl_msg(d, &req);
387 if (ret) {
f224749b
AP
388 dev_err(&d->udev->dev, "%s: firmware boot failed=%d\n",
389 KBUILD_MODNAME, ret);
80619de8 390 goto error;
80619de8 391 }
a3645e54
AP
392
393error:
394 return ret;
395}
396
397/* hash (and dump) eeprom */
398static int af9015_eeprom_hash(struct dvb_usb_device *d)
399{
e8089661 400 struct af9015_state *state = d_to_priv(d);
2e35c66f
AP
401 int ret, i;
402 static const unsigned int AF9015_EEPROM_SIZE = 256;
403 u8 buf[AF9015_EEPROM_SIZE];
404 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, NULL};
405
406 /* read eeprom */
407 for (i = 0; i < AF9015_EEPROM_SIZE; i++) {
408 req.addr = i;
409 req.data = &buf[i];
a3645e54 410 ret = af9015_ctrl_msg(d, &req);
2e35c66f
AP
411 if (ret < 0)
412 goto err;
80619de8 413 }
a3645e54 414
2e35c66f
AP
415 /* calculate checksum */
416 for (i = 0; i < AF9015_EEPROM_SIZE / sizeof(u32); i++) {
a3645e54 417 state->eeprom_sum *= GOLDEN_RATIO_PRIME_32;
2e35c66f 418 state->eeprom_sum += le32_to_cpu(((u32 *)buf)[i]);
80619de8
AP
419 }
420
2e35c66f
AP
421 for (i = 0; i < AF9015_EEPROM_SIZE; i += 16)
422 dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 16, buf + i);
423
f224749b
AP
424 dev_dbg(&d->udev->dev, "%s: eeprom sum=%.8x\n",
425 __func__, state->eeprom_sum);
2e35c66f
AP
426 return 0;
427err:
428 dev_err(&d->udev->dev, "%s: eeprom failed=%d\n", KBUILD_MODNAME, ret);
80619de8
AP
429 return ret;
430}
431
a3645e54 432static int af9015_read_config(struct dvb_usb_device *d)
80619de8 433{
e8089661 434 struct af9015_state *state = d_to_priv(d);
80619de8 435 int ret;
a3645e54
AP
436 u8 val, i, offset = 0;
437 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
80619de8 438
f224749b 439 dev_dbg(&d->udev->dev, "%s:\n", __func__);
80619de8 440
a3645e54
AP
441 /* IR remote controller */
442 req.addr = AF9015_EEPROM_IR_MODE;
443 /* first message will timeout often due to possible hw bug */
444 for (i = 0; i < 4; i++) {
445 ret = af9015_ctrl_msg(d, &req);
446 if (!ret)
447 break;
448 }
449 if (ret)
450 goto error;
80619de8 451
a3645e54 452 ret = af9015_eeprom_hash(d);
80619de8
AP
453 if (ret)
454 goto error;
80619de8 455
a3645e54 456 state->ir_mode = val;
f224749b 457 dev_dbg(&d->udev->dev, "%s: IR mode=%d\n", __func__, val);
80619de8 458
a3645e54
AP
459 /* TS mode - one or two receivers */
460 req.addr = AF9015_EEPROM_TS_MODE;
80619de8 461 ret = af9015_ctrl_msg(d, &req);
80619de8
AP
462 if (ret)
463 goto error;
6c614044 464
a3645e54 465 state->dual_mode = val;
f224749b 466 dev_dbg(&d->udev->dev, "%s: TS mode=%d\n", __func__, state->dual_mode);
6c614044 467
a3645e54
AP
468 /* disable 2nd adapter because we don't have PID-filters */
469 if (d->udev->speed == USB_SPEED_FULL)
470 state->dual_mode = 0;
80619de8 471
a3645e54 472 if (state->dual_mode) {
80619de8
AP
473 /* read 2nd demodulator I2C address */
474 req.addr = AF9015_EEPROM_DEMOD2_I2C;
a3645e54 475 ret = af9015_ctrl_msg(d, &req);
80619de8
AP
476 if (ret)
477 goto error;
80619de8 478
a3645e54 479 state->af9013_config[1].i2c_addr = val;
80619de8
AP
480 }
481
a3645e54 482 for (i = 0; i < state->dual_mode + 1; i++) {
80619de8
AP
483 if (i == 1)
484 offset = AF9015_EEPROM_OFFSET;
485 /* xtal */
486 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
a3645e54 487 ret = af9015_ctrl_msg(d, &req);
80619de8
AP
488 if (ret)
489 goto error;
490 switch (val) {
491 case 0:
a3645e54 492 state->af9013_config[i].clock = 28800000;
80619de8
AP
493 break;
494 case 1:
a3645e54 495 state->af9013_config[i].clock = 20480000;
80619de8
AP
496 break;
497 case 2:
a3645e54 498 state->af9013_config[i].clock = 28000000;
80619de8
AP
499 break;
500 case 3:
a3645e54 501 state->af9013_config[i].clock = 25000000;
80619de8
AP
502 break;
503 };
f224749b
AP
504 dev_dbg(&d->udev->dev, "%s: [%d] xtal=%d set clock=%d\n",
505 __func__, i, val,
506 state->af9013_config[i].clock);
80619de8 507
f571e004 508 /* IF frequency */
80619de8 509 req.addr = AF9015_EEPROM_IF1H + offset;
a3645e54 510 ret = af9015_ctrl_msg(d, &req);
80619de8
AP
511 if (ret)
512 goto error;
f571e004 513
a3645e54 514 state->af9013_config[i].if_frequency = val << 8;
f571e004 515
80619de8 516 req.addr = AF9015_EEPROM_IF1L + offset;
a3645e54 517 ret = af9015_ctrl_msg(d, &req);
80619de8
AP
518 if (ret)
519 goto error;
f571e004 520
a3645e54
AP
521 state->af9013_config[i].if_frequency += val;
522 state->af9013_config[i].if_frequency *= 1000;
f224749b
AP
523 dev_dbg(&d->udev->dev, "%s: [%d] IF frequency=%d\n", __func__,
524 i, state->af9013_config[i].if_frequency);
80619de8
AP
525
526 /* MT2060 IF1 */
527 req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
a3645e54 528 ret = af9015_ctrl_msg(d, &req);
80619de8
AP
529 if (ret)
530 goto error;
a3645e54 531 state->mt2060_if1[i] = val << 8;
80619de8 532 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
a3645e54 533 ret = af9015_ctrl_msg(d, &req);
80619de8
AP
534 if (ret)
535 goto error;
a3645e54 536 state->mt2060_if1[i] += val;
f224749b 537 dev_dbg(&d->udev->dev, "%s: [%d] MT2060 IF1=%d\n", __func__, i,
a3645e54 538 state->mt2060_if1[i]);
80619de8
AP
539
540 /* tuner */
541 req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
a3645e54 542 ret = af9015_ctrl_msg(d, &req);
80619de8
AP
543 if (ret)
544 goto error;
545 switch (val) {
546 case AF9013_TUNER_ENV77H11D5:
547 case AF9013_TUNER_MT2060:
80619de8
AP
548 case AF9013_TUNER_QT1010:
549 case AF9013_TUNER_UNKNOWN:
550 case AF9013_TUNER_MT2060_2:
551 case AF9013_TUNER_TDA18271:
552 case AF9013_TUNER_QT1010A:
ee3d440c 553 case AF9013_TUNER_TDA18218:
a3645e54 554 state->af9013_config[i].spec_inv = 1;
80619de8
AP
555 break;
556 case AF9013_TUNER_MXL5003D:
557 case AF9013_TUNER_MXL5005D:
558 case AF9013_TUNER_MXL5005R:
ab07fdd6 559 case AF9013_TUNER_MXL5007T:
a3645e54 560 state->af9013_config[i].spec_inv = 0;
80619de8 561 break;
d5633998 562 case AF9013_TUNER_MC44S803:
a3645e54
AP
563 state->af9013_config[i].gpio[1] = AF9013_GPIO_LO;
564 state->af9013_config[i].spec_inv = 1;
d5633998 565 break;
80619de8 566 default:
f224749b
AP
567 dev_err(&d->udev->dev, "%s: tuner id=%d not " \
568 "supported, please report!\n",
569 KBUILD_MODNAME, val);
80619de8
AP
570 return -ENODEV;
571 };
572
a3645e54 573 state->af9013_config[i].tuner = val;
f224749b
AP
574 dev_dbg(&d->udev->dev, "%s: [%d] tuner id=%d\n",
575 __func__, i, val);
80619de8
AP
576 }
577
578error:
579 if (ret)
f224749b
AP
580 dev_err(&d->udev->dev, "%s: eeprom read failed=%d\n",
581 KBUILD_MODNAME, ret);
80619de8 582
3956fefc 583 /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
8ccdf1ae
YM
584 content :-( Override some wrong values here. Ditto for the
585 AVerTV Red HD+ (A850T) device. */
a3645e54
AP
586 if (le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
587 ((le16_to_cpu(d->udev->descriptor.idProduct) ==
9a3ecc73 588 USB_PID_AVERMEDIA_A850) ||
a3645e54 589 (le16_to_cpu(d->udev->descriptor.idProduct) ==
9a3ecc73 590 USB_PID_AVERMEDIA_A850T))) {
f224749b
AP
591 dev_dbg(&d->udev->dev,
592 "%s: AverMedia A850: overriding config\n",
593 __func__);
3956fefc 594 /* disable dual mode */
a3645e54 595 state->dual_mode = 0;
3956fefc
AP
596
597 /* set correct IF */
a3645e54 598 state->af9013_config[0].if_frequency = 4570000;
3956fefc
AP
599 }
600
80619de8
AP
601 return ret;
602}
603
b905a2a1 604static int af9015_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
a3645e54 605 struct usb_data_stream_properties *stream)
80619de8 606{
f224749b
AP
607 struct dvb_usb_device *d = fe_to_d(fe);
608 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
d3bb73de 609
f224749b 610 if (d->udev->speed == USB_SPEED_FULL)
a3645e54 611 stream->u.bulk.buffersize = TS_USB11_FRAME_SIZE;
1e8750c2 612
a3645e54
AP
613 return 0;
614}
d3bb73de 615
a3645e54
AP
616static int af9015_get_adapter_count(struct dvb_usb_device *d)
617{
e8089661 618 struct af9015_state *state = d_to_priv(d);
a3645e54 619 return state->dual_mode + 1;
80619de8
AP
620}
621
e90ab840 622/* override demod callbacks for resource locking */
0009e0e3 623static int af9015_af9013_set_frontend(struct dvb_frontend *fe)
e90ab840
AP
624{
625 int ret;
e8089661 626 struct af9015_state *state = fe_to_priv(fe);
e90ab840 627
a3645e54 628 if (mutex_lock_interruptible(&state->fe_mutex))
e90ab840
AP
629 return -EAGAIN;
630
e8089661 631 ret = state->set_frontend[fe_to_adap(fe)->id](fe);
e90ab840 632
a3645e54 633 mutex_unlock(&state->fe_mutex);
e90ab840
AP
634
635 return ret;
636}
637
638/* override demod callbacks for resource locking */
639static int af9015_af9013_read_status(struct dvb_frontend *fe,
640 fe_status_t *status)
641{
642 int ret;
e8089661 643 struct af9015_state *state = fe_to_priv(fe);
e90ab840 644
a3645e54 645 if (mutex_lock_interruptible(&state->fe_mutex))
e90ab840
AP
646 return -EAGAIN;
647
e8089661 648 ret = state->read_status[fe_to_adap(fe)->id](fe, status);
e90ab840 649
a3645e54 650 mutex_unlock(&state->fe_mutex);
e90ab840
AP
651
652 return ret;
653}
654
655/* override demod callbacks for resource locking */
656static int af9015_af9013_init(struct dvb_frontend *fe)
657{
658 int ret;
e8089661 659 struct af9015_state *state = fe_to_priv(fe);
e90ab840 660
a3645e54 661 if (mutex_lock_interruptible(&state->fe_mutex))
e90ab840
AP
662 return -EAGAIN;
663
e8089661 664 ret = state->init[fe_to_adap(fe)->id](fe);
e90ab840 665
a3645e54 666 mutex_unlock(&state->fe_mutex);
e90ab840
AP
667
668 return ret;
669}
670
671/* override demod callbacks for resource locking */
672static int af9015_af9013_sleep(struct dvb_frontend *fe)
673{
674 int ret;
e8089661 675 struct af9015_state *state = fe_to_priv(fe);
e90ab840 676
a3645e54 677 if (mutex_lock_interruptible(&state->fe_mutex))
e90ab840
AP
678 return -EAGAIN;
679
e8089661 680 ret = state->sleep[fe_to_adap(fe)->id](fe);
e90ab840 681
a3645e54 682 mutex_unlock(&state->fe_mutex);
e90ab840
AP
683
684 return ret;
685}
686
6d535bd8
AP
687/* override tuner callbacks for resource locking */
688static int af9015_tuner_init(struct dvb_frontend *fe)
689{
690 int ret;
e8089661 691 struct af9015_state *state = fe_to_priv(fe);
6d535bd8 692
a3645e54 693 if (mutex_lock_interruptible(&state->fe_mutex))
6d535bd8
AP
694 return -EAGAIN;
695
e8089661 696 ret = state->tuner_init[fe_to_adap(fe)->id](fe);
6d535bd8 697
a3645e54 698 mutex_unlock(&state->fe_mutex);
6d535bd8
AP
699
700 return ret;
701}
702
703/* override tuner callbacks for resource locking */
704static int af9015_tuner_sleep(struct dvb_frontend *fe)
705{
706 int ret;
e8089661 707 struct af9015_state *state = fe_to_priv(fe);
6d535bd8 708
a3645e54 709 if (mutex_lock_interruptible(&state->fe_mutex))
6d535bd8
AP
710 return -EAGAIN;
711
e8089661 712 ret = state->tuner_sleep[fe_to_adap(fe)->id](fe);
a3645e54
AP
713
714 mutex_unlock(&state->fe_mutex);
715
716 return ret;
717}
718
719static int af9015_copy_firmware(struct dvb_usb_device *d)
720{
e8089661 721 struct af9015_state *state = d_to_priv(d);
a3645e54
AP
722 int ret;
723 u8 fw_params[4];
724 u8 val, i;
725 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
726 fw_params };
f224749b 727 dev_dbg(&d->udev->dev, "%s:\n", __func__);
a3645e54
AP
728
729 fw_params[0] = state->firmware_size >> 8;
730 fw_params[1] = state->firmware_size & 0xff;
731 fw_params[2] = state->firmware_checksum >> 8;
732 fw_params[3] = state->firmware_checksum & 0xff;
733
734 /* wait 2nd demodulator ready */
735 msleep(100);
736
737 ret = af9015_read_reg_i2c(d, state->af9013_config[1].i2c_addr,
738 0x98be, &val);
739 if (ret)
740 goto error;
741 else
f224749b
AP
742 dev_dbg(&d->udev->dev, "%s: firmware status=%02x\n",
743 __func__, val);
a3645e54
AP
744
745 if (val == 0x0c) /* fw is running, no need for download */
746 goto exit;
747
748 /* set I2C master clock to fast (to speed up firmware copy) */
749 ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
750 if (ret)
751 goto error;
752
753 msleep(50);
754
755 /* copy firmware */
756 ret = af9015_ctrl_msg(d, &req);
757 if (ret)
f224749b
AP
758 dev_err(&d->udev->dev, "%s: firmware copy cmd failed=%d\n",
759 KBUILD_MODNAME, ret);
760
761 dev_dbg(&d->udev->dev, "%s: firmware copy done\n", __func__);
a3645e54
AP
762
763 /* set I2C master clock back to normal */
764 ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
765 if (ret)
766 goto error;
767
768 /* request boot firmware */
769 ret = af9015_write_reg_i2c(d, state->af9013_config[1].i2c_addr,
770 0xe205, 1);
f224749b
AP
771 dev_dbg(&d->udev->dev, "%s: firmware boot cmd status=%d\n",
772 __func__, ret);
a3645e54
AP
773 if (ret)
774 goto error;
775
776 for (i = 0; i < 15; i++) {
777 msleep(100);
6d535bd8 778
a3645e54
AP
779 /* check firmware status */
780 ret = af9015_read_reg_i2c(d, state->af9013_config[1].i2c_addr,
781 0x98be, &val);
f224749b
AP
782 dev_dbg(&d->udev->dev, "%s: firmware status cmd status=%d " \
783 "firmware status=%02x\n", __func__, ret, val);
a3645e54
AP
784 if (ret)
785 goto error;
6d535bd8 786
a3645e54
AP
787 if (val == 0x0c || val == 0x04) /* success or fail */
788 break;
789 }
790
791 if (val == 0x04) {
f224749b
AP
792 dev_err(&d->udev->dev, "%s: firmware did not run\n",
793 KBUILD_MODNAME);
e1e9e510 794 ret = -ETIMEDOUT;
a3645e54 795 } else if (val != 0x0c) {
f224749b
AP
796 dev_err(&d->udev->dev, "%s: firmware boot timeout\n",
797 KBUILD_MODNAME);
e1e9e510 798 ret = -ETIMEDOUT;
a3645e54
AP
799 }
800
801error:
802exit:
6d535bd8
AP
803 return ret;
804}
805
80619de8
AP
806static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
807{
808 int ret;
e8089661 809 struct af9015_state *state = adap_to_priv(adap);
80619de8 810
a3645e54
AP
811 if (adap->id == 0) {
812 state->af9013_config[0].ts_mode = AF9013_TS_USB;
813 memcpy(state->af9013_config[0].api_version, "\x0\x1\x9\x0", 4);
814 state->af9013_config[0].gpio[0] = AF9013_GPIO_HI;
815 state->af9013_config[0].gpio[3] = AF9013_GPIO_TUNER_ON;
816 } else if (adap->id == 1) {
817 state->af9013_config[1].ts_mode = AF9013_TS_SERIAL;
818 memcpy(state->af9013_config[1].api_version, "\x0\x1\x9\x0", 4);
819 state->af9013_config[1].gpio[0] = AF9013_GPIO_TUNER_ON;
820 state->af9013_config[1].gpio[1] = AF9013_GPIO_LO;
821
80619de8 822 /* copy firmware to 2nd demodulator */
a3645e54 823 if (state->dual_mode) {
e8089661 824 ret = af9015_copy_firmware(adap_to_d(adap));
80619de8 825 if (ret) {
f224749b
AP
826 dev_err(&adap_to_d(adap)->udev->dev,
827 "%s: firmware copy to 2nd " \
828 "frontend failed, will " \
829 "disable it\n", KBUILD_MODNAME);
a3645e54 830 state->dual_mode = 0;
80619de8
AP
831 return -ENODEV;
832 }
833 } else {
834 return -ENODEV;
835 }
836 }
837
838 /* attach demodulator */
a3645e54 839 adap->fe[0] = dvb_attach(af9013_attach,
e8089661 840 &state->af9013_config[adap->id], &adap_to_d(adap)->i2c_adap);
80619de8 841
e90ab840
AP
842 /*
843 * AF9015 firmware does not like if it gets interrupted by I2C adapter
844 * request on some critical phases. During normal operation I2C adapter
845 * is used only 2nd demodulator and tuner on dual tuner devices.
846 * Override demodulator callbacks and use mutex for limit access to
847 * those "critical" paths to keep AF9015 happy.
e90ab840 848 */
a3645e54 849 if (adap->fe[0]) {
e90ab840 850 state->set_frontend[adap->id] =
a3645e54
AP
851 adap->fe[0]->ops.set_frontend;
852 adap->fe[0]->ops.set_frontend =
e90ab840
AP
853 af9015_af9013_set_frontend;
854
855 state->read_status[adap->id] =
a3645e54
AP
856 adap->fe[0]->ops.read_status;
857 adap->fe[0]->ops.read_status =
e90ab840
AP
858 af9015_af9013_read_status;
859
a3645e54
AP
860 state->init[adap->id] = adap->fe[0]->ops.init;
861 adap->fe[0]->ops.init = af9015_af9013_init;
e90ab840 862
a3645e54
AP
863 state->sleep[adap->id] = adap->fe[0]->ops.sleep;
864 adap->fe[0]->ops.sleep = af9015_af9013_sleep;
e90ab840
AP
865 }
866
a3645e54 867 return adap->fe[0] == NULL ? -ENODEV : 0;
80619de8
AP
868}
869
870static struct mt2060_config af9015_mt2060_config = {
871 .i2c_address = 0xc0,
872 .clock_out = 0,
873};
874
875static struct qt1010_config af9015_qt1010_config = {
876 .i2c_address = 0xc4,
877};
878
879static struct tda18271_config af9015_tda18271_config = {
880 .gate = TDA18271_GATE_DIGITAL,
7655e594 881 .small_i2c = TDA18271_16_BYTE_CHUNK_INIT,
80619de8
AP
882};
883
884static struct mxl5005s_config af9015_mxl5003_config = {
885 .i2c_address = 0xc6,
886 .if_freq = IF_FREQ_4570000HZ,
887 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
888 .agc_mode = MXL_SINGLE_AGC,
889 .tracking_filter = MXL_TF_DEFAULT,
a131077d 890 .rssi_enable = MXL_RSSI_ENABLE,
80619de8
AP
891 .cap_select = MXL_CAP_SEL_ENABLE,
892 .div_out = MXL_DIV_OUT_4,
893 .clock_out = MXL_CLOCK_OUT_DISABLE,
894 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
895 .top = MXL5005S_TOP_25P2,
896 .mod_mode = MXL_DIGITAL_MODE,
897 .if_mode = MXL_ZERO_IF,
898 .AgcMasterByte = 0x00,
899};
900
901static struct mxl5005s_config af9015_mxl5005_config = {
902 .i2c_address = 0xc6,
903 .if_freq = IF_FREQ_4570000HZ,
904 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
905 .agc_mode = MXL_SINGLE_AGC,
906 .tracking_filter = MXL_TF_OFF,
a131077d 907 .rssi_enable = MXL_RSSI_ENABLE,
80619de8
AP
908 .cap_select = MXL_CAP_SEL_ENABLE,
909 .div_out = MXL_DIV_OUT_4,
910 .clock_out = MXL_CLOCK_OUT_DISABLE,
911 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
912 .top = MXL5005S_TOP_25P2,
913 .mod_mode = MXL_DIGITAL_MODE,
914 .if_mode = MXL_ZERO_IF,
915 .AgcMasterByte = 0x00,
916};
917
d5633998
JF
918static struct mc44s803_config af9015_mc44s803_config = {
919 .i2c_address = 0xc0,
920 .dig_out = 1,
921};
922
ee3d440c
AP
923static struct tda18218_config af9015_tda18218_config = {
924 .i2c_address = 0xc0,
925 .i2c_wr_max = 21, /* max wr bytes AF9015 I2C adap can handle at once */
926};
927
ab07fdd6
AP
928static struct mxl5007t_config af9015_mxl5007t_config = {
929 .xtal_freq_hz = MxL_XTAL_24_MHZ,
930 .if_freq_hz = MxL_IF_4_57_MHZ,
931};
932
80619de8
AP
933static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
934{
f224749b
AP
935 struct dvb_usb_device *d = adap_to_d(adap);
936 struct af9015_state *state = d_to_priv(d);
a3645e54 937 int ret;
f224749b 938 dev_dbg(&d->udev->dev, "%s:\n", __func__);
80619de8 939
a3645e54 940 switch (state->af9013_config[adap->id].tuner) {
80619de8
AP
941 case AF9013_TUNER_MT2060:
942 case AF9013_TUNER_MT2060_2:
a3645e54 943 ret = dvb_attach(mt2060_attach, adap->fe[0],
e8089661 944 &adap_to_d(adap)->i2c_adap, &af9015_mt2060_config,
a3645e54 945 state->mt2060_if1[adap->id])
80619de8
AP
946 == NULL ? -ENODEV : 0;
947 break;
948 case AF9013_TUNER_QT1010:
949 case AF9013_TUNER_QT1010A:
a3645e54 950 ret = dvb_attach(qt1010_attach, adap->fe[0],
e8089661 951 &adap_to_d(adap)->i2c_adap,
80619de8
AP
952 &af9015_qt1010_config) == NULL ? -ENODEV : 0;
953 break;
954 case AF9013_TUNER_TDA18271:
a3645e54 955 ret = dvb_attach(tda18271_attach, adap->fe[0], 0xc0,
e8089661 956 &adap_to_d(adap)->i2c_adap,
80619de8
AP
957 &af9015_tda18271_config) == NULL ? -ENODEV : 0;
958 break;
ee3d440c 959 case AF9013_TUNER_TDA18218:
a3645e54 960 ret = dvb_attach(tda18218_attach, adap->fe[0],
e8089661 961 &adap_to_d(adap)->i2c_adap,
ee3d440c
AP
962 &af9015_tda18218_config) == NULL ? -ENODEV : 0;
963 break;
80619de8 964 case AF9013_TUNER_MXL5003D:
a3645e54 965 ret = dvb_attach(mxl5005s_attach, adap->fe[0],
e8089661 966 &adap_to_d(adap)->i2c_adap,
80619de8
AP
967 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
968 break;
969 case AF9013_TUNER_MXL5005D:
970 case AF9013_TUNER_MXL5005R:
a3645e54 971 ret = dvb_attach(mxl5005s_attach, adap->fe[0],
e8089661 972 &adap_to_d(adap)->i2c_adap,
80619de8
AP
973 &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
974 break;
975 case AF9013_TUNER_ENV77H11D5:
a3645e54 976 ret = dvb_attach(dvb_pll_attach, adap->fe[0], 0xc0,
e8089661 977 &adap_to_d(adap)->i2c_adap,
80619de8
AP
978 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
979 break;
980 case AF9013_TUNER_MC44S803:
a3645e54 981 ret = dvb_attach(mc44s803_attach, adap->fe[0],
e8089661 982 &adap_to_d(adap)->i2c_adap,
d5633998 983 &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
80619de8 984 break;
ab07fdd6 985 case AF9013_TUNER_MXL5007T:
a3645e54 986 ret = dvb_attach(mxl5007t_attach, adap->fe[0],
e8089661 987 &adap_to_d(adap)->i2c_adap,
ab07fdd6
AP
988 0xc0, &af9015_mxl5007t_config) == NULL ? -ENODEV : 0;
989 break;
80619de8
AP
990 case AF9013_TUNER_UNKNOWN:
991 default:
f224749b
AP
992 dev_err(&d->udev->dev, "%s: unknown tuner id=%d\n",
993 KBUILD_MODNAME,
994 state->af9013_config[adap->id].tuner);
80619de8 995 ret = -ENODEV;
80619de8 996 }
be4a5e7f 997
a3645e54 998 if (adap->fe[0]->ops.tuner_ops.init) {
6d535bd8 999 state->tuner_init[adap->id] =
a3645e54
AP
1000 adap->fe[0]->ops.tuner_ops.init;
1001 adap->fe[0]->ops.tuner_ops.init = af9015_tuner_init;
6d535bd8
AP
1002 }
1003
a3645e54 1004 if (adap->fe[0]->ops.tuner_ops.sleep) {
6d535bd8 1005 state->tuner_sleep[adap->id] =
a3645e54
AP
1006 adap->fe[0]->ops.tuner_ops.sleep;
1007 adap->fe[0]->ops.tuner_ops.sleep = af9015_tuner_sleep;
1008 }
1009
1010 return ret;
1011}
1012
1013static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1014{
f224749b 1015 struct dvb_usb_device *d = adap_to_d(adap);
a3645e54 1016 int ret;
f224749b 1017 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
a3645e54
AP
1018
1019 if (onoff)
f224749b 1020 ret = af9015_set_reg_bit(d, 0xd503, 0);
a3645e54 1021 else
f224749b 1022 ret = af9015_clear_reg_bit(d, 0xd503, 0);
a3645e54
AP
1023
1024 return ret;
1025}
1026
1027static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1028 int onoff)
1029{
f224749b 1030 struct dvb_usb_device *d = adap_to_d(adap);
a3645e54
AP
1031 int ret;
1032 u8 idx;
f224749b
AP
1033 dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
1034 __func__, index, pid, onoff);
a3645e54 1035
f224749b 1036 ret = af9015_write_reg(d, 0xd505, (pid & 0xff));
a3645e54
AP
1037 if (ret)
1038 goto error;
1039
f224749b 1040 ret = af9015_write_reg(d, 0xd506, (pid >> 8));
a3645e54
AP
1041 if (ret)
1042 goto error;
1043
1044 idx = ((index & 0x1f) | (1 << 5));
f224749b 1045 ret = af9015_write_reg(d, 0xd504, idx);
a3645e54
AP
1046
1047error:
1048 return ret;
1049}
1050
1051static int af9015_init_endpoint(struct dvb_usb_device *d)
1052{
e8089661 1053 struct af9015_state *state = d_to_priv(d);
a3645e54
AP
1054 int ret;
1055 u16 frame_size;
1056 u8 packet_size;
f224749b 1057 dev_dbg(&d->udev->dev, "%s: USB speed=%d\n", __func__, d->udev->speed);
a3645e54
AP
1058
1059 if (d->udev->speed == USB_SPEED_FULL) {
1060 frame_size = TS_USB11_FRAME_SIZE/4;
1061 packet_size = TS_USB11_MAX_PACKET_SIZE/4;
1062 } else {
1063 frame_size = TS_USB20_FRAME_SIZE/4;
1064 packet_size = TS_USB20_MAX_PACKET_SIZE/4;
1065 }
1066
1067 ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
1068 if (ret)
1069 goto error;
1070 ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
1071 if (ret)
1072 goto error;
1073 ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
1074 if (ret)
1075 goto error;
1076 ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
1077 if (ret)
1078 goto error;
1079 ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
1080 if (ret)
1081 goto error;
1082 if (state->dual_mode) {
1083 ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
1084 if (ret)
1085 goto error;
1086 }
1087 ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
1088 if (ret)
1089 goto error;
1090 if (state->dual_mode) {
1091 ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
1092 if (ret)
1093 goto error;
1094 }
1095 /* EP4 xfer length */
1096 ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
1097 if (ret)
1098 goto error;
1099 ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
1100 if (ret)
1101 goto error;
1102 /* EP5 xfer length */
1103 ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
1104 if (ret)
1105 goto error;
1106 ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
1107 if (ret)
1108 goto error;
1109 ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
1110 if (ret)
1111 goto error;
1112 ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
1113 if (ret)
1114 goto error;
1115 ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
1116 if (ret)
1117 goto error;
1118 if (state->dual_mode) {
1119 ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
1120 if (ret)
1121 goto error;
6d535bd8 1122 }
be4a5e7f 1123
a3645e54
AP
1124 /* enable / disable mp2if2 */
1125 if (state->dual_mode)
1126 ret = af9015_set_reg_bit(d, 0xd50b, 0);
1127 else
1128 ret = af9015_clear_reg_bit(d, 0xd50b, 0);
1129
1130error:
1131 if (ret)
f224749b
AP
1132 dev_err(&d->udev->dev, "%s: endpoint init failed=%d\n",
1133 KBUILD_MODNAME, ret);
1134
a3645e54
AP
1135 return ret;
1136}
1137
1138static int af9015_init(struct dvb_usb_device *d)
1139{
e8089661 1140 struct af9015_state *state = d_to_priv(d);
a3645e54 1141 int ret;
f224749b 1142 dev_dbg(&d->udev->dev, "%s:\n", __func__);
a3645e54
AP
1143
1144 mutex_init(&state->fe_mutex);
1145
1146 /* init RC canary */
1147 ret = af9015_write_reg(d, 0x98e9, 0xff);
1148 if (ret)
1149 goto error;
1150
1151 ret = af9015_init_endpoint(d);
1152 if (ret)
1153 goto error;
1154
1155error:
80619de8
AP
1156 return ret;
1157}
1158
a3645e54
AP
1159struct af9015_rc_setup {
1160 unsigned int id;
1161 char *rc_codes;
d07b901f
JN
1162};
1163
a3645e54
AP
1164static char *af9015_rc_setup_match(unsigned int id,
1165 const struct af9015_rc_setup *table)
1166{
1167 for (; table->rc_codes; table++)
1168 if (table->id == id)
1169 return table->rc_codes;
1170 return NULL;
1171}
1172
1173static const struct af9015_rc_setup af9015_rc_setup_modparam[] = {
1174 { AF9015_REMOTE_A_LINK_DTU_M, RC_MAP_ALINK_DTU_M },
1175 { AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3, RC_MAP_MSI_DIGIVOX_II },
1176 { AF9015_REMOTE_MYGICTV_U718, RC_MAP_TOTAL_MEDIA_IN_HAND },
1177 { AF9015_REMOTE_DIGITTRADE_DVB_T, RC_MAP_DIGITTRADE },
1178 { AF9015_REMOTE_AVERMEDIA_KS, RC_MAP_AVERMEDIA_RM_KS },
d07b901f 1179 { }
80619de8 1180};
80619de8 1181
a3645e54
AP
1182static const struct af9015_rc_setup af9015_rc_setup_hashes[] = {
1183 { 0xb8feb708, RC_MAP_MSI_DIGIVOX_II },
1184 { 0xa3703d00, RC_MAP_ALINK_DTU_M },
1185 { 0x9b7dc64e, RC_MAP_TOTAL_MEDIA_IN_HAND }, /* MYGICTV U718 */
1186 { 0x5d49e3db, RC_MAP_DIGITTRADE }, /* LC-Power LC-USB-DVBT */
1187 { }
1188};
80619de8 1189
a3645e54
AP
1190static int af9015_rc_query(struct dvb_usb_device *d)
1191{
e8089661 1192 struct af9015_state *state = d_to_priv(d);
a3645e54
AP
1193 int ret;
1194 u8 buf[17];
80619de8 1195
a3645e54
AP
1196 /* read registers needed to detect remote controller code */
1197 ret = af9015_read_regs(d, 0x98d9, buf, sizeof(buf));
1198 if (ret)
1199 goto error;
80619de8 1200
a3645e54 1201 /* If any of these are non-zero, assume invalid data */
f224749b
AP
1202 if (buf[1] || buf[2] || buf[3]) {
1203 dev_dbg(&d->udev->dev, "%s: invalid data\n", __func__);
a3645e54 1204 return ret;
f224749b 1205 }
80619de8 1206
a3645e54
AP
1207 /* Check for repeat of previous code */
1208 if ((state->rc_repeat != buf[6] || buf[0]) &&
1209 !memcmp(&buf[12], state->rc_last, 4)) {
f224749b 1210 dev_dbg(&d->udev->dev, "%s: key repeated\n", __func__);
a3645e54
AP
1211 rc_keydown(d->rc_dev, state->rc_keycode, 0);
1212 state->rc_repeat = buf[6];
1213 return ret;
1214 }
85d7d7ca 1215
a3645e54
AP
1216 /* Only process key if canary killed */
1217 if (buf[16] != 0xff && buf[0] != 0x01) {
f224749b
AP
1218 dev_dbg(&d->udev->dev, "%s: key pressed %*ph\n",
1219 __func__, 4, buf + 12);
85d7d7ca 1220
a3645e54
AP
1221 /* Reset the canary */
1222 ret = af9015_write_reg(d, 0x98e9, 0xff);
1223 if (ret)
1224 goto error;
85d7d7ca 1225
a3645e54
AP
1226 /* Remember this key */
1227 memcpy(state->rc_last, &buf[12], 4);
1228 if (buf[14] == (u8) ~buf[15]) {
1229 if (buf[12] == (u8) ~buf[13]) {
1230 /* NEC */
1231 state->rc_keycode = buf[12] << 8 | buf[14];
1232 } else {
1233 /* NEC extended*/
1234 state->rc_keycode = buf[12] << 16 |
1235 buf[13] << 8 | buf[14];
1236 }
1237 } else {
1238 /* 32 bit NEC */
1239 state->rc_keycode = buf[12] << 24 | buf[13] << 16 |
1240 buf[14] << 8 | buf[15];
85d7d7ca 1241 }
a3645e54
AP
1242 rc_keydown(d->rc_dev, state->rc_keycode, 0);
1243 } else {
f224749b 1244 dev_dbg(&d->udev->dev, "%s: no key press\n", __func__);
a3645e54
AP
1245 /* Invalidate last keypress */
1246 /* Not really needed, but helps with debug */
1247 state->rc_last[2] = state->rc_last[3];
1248 }
1249
1250 state->rc_repeat = buf[6];
eb29fbea 1251 state->rc_failed = false;
a3645e54
AP
1252
1253error:
eb29fbea 1254 if (ret) {
f224749b
AP
1255 dev_warn(&d->udev->dev, "%s: rc query failed=%d\n",
1256 KBUILD_MODNAME, ret);
a3645e54 1257
eb29fbea
AP
1258 /* allow random errors as dvb-usb will stop polling on error */
1259 if (!state->rc_failed)
1260 ret = 0;
1261
1262 state->rc_failed = true;
1263 }
1264
a3645e54
AP
1265 return ret;
1266}
80619de8 1267
a3645e54 1268static int af9015_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
80619de8 1269{
e8089661 1270 struct af9015_state *state = d_to_priv(d);
a3645e54
AP
1271 u16 vid = le16_to_cpu(d->udev->descriptor.idVendor);
1272
1273 if (state->ir_mode == AF9015_IR_MODE_DISABLED)
1274 return 0;
1275
1276 /* try to load remote based module param */
eb29fbea
AP
1277 if (!rc->map_name)
1278 rc->map_name = af9015_rc_setup_match(dvb_usb_af9015_remote,
1279 af9015_rc_setup_modparam);
80619de8 1280
a3645e54
AP
1281 /* try to load remote based eeprom hash */
1282 if (!rc->map_name)
1283 rc->map_name = af9015_rc_setup_match(state->eeprom_sum,
1284 af9015_rc_setup_hashes);
1285
1286 /* try to load remote based USB iManufacturer string */
1287 if (!rc->map_name && vid == USB_VID_AFATECH) {
1288 /* Check USB manufacturer and product strings and try
1289 to determine correct remote in case of chip vendor
1290 reference IDs are used.
1291 DO NOT ADD ANYTHING NEW HERE. Use hashes instead. */
1292 char manufacturer[10];
1293 memset(manufacturer, 0, sizeof(manufacturer));
1294 usb_string(d->udev, d->udev->descriptor.iManufacturer,
1295 manufacturer, sizeof(manufacturer));
1296 if (!strcmp("MSI", manufacturer)) {
1297 /* iManufacturer 1 MSI
1298 iProduct 2 MSI K-VOX */
1299 rc->map_name = af9015_rc_setup_match(
1300 AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
1301 af9015_rc_setup_modparam);
1302 }
80619de8
AP
1303 }
1304
de73beee
AP
1305 /* load empty to enable rc */
1306 if (!rc->map_name)
1307 rc->map_name = RC_MAP_EMPTY;
1308
a3645e54
AP
1309 rc->allowed_protos = RC_TYPE_NEC;
1310 rc->query = af9015_rc_query;
1311 rc->interval = 500;
1312
1313 return 0;
80619de8
AP
1314}
1315
a3645e54
AP
1316/* interface 0 is used by DVB-T receiver and
1317 interface 1 is for remote controller (HID) */
1318static struct dvb_usb_device_properties af9015_props = {
1319 .driver_name = KBUILD_MODNAME,
1320 .owner = THIS_MODULE,
1321 .adapter_nr = adapter_nr,
1322 .size_of_priv = sizeof(struct af9015_state),
1323
1324 .generic_bulk_ctrl_endpoint = 0x02,
1325 .generic_bulk_ctrl_endpoint_response = 0x81,
1326
1327 .identify_state = af9015_identify_state,
bab9b4fe 1328 .firmware = AF9015_FIRMWARE,
a3645e54
AP
1329 .download_firmware = af9015_download_firmware,
1330
a3645e54 1331 .i2c_algo = &af9015_i2c_algo,
2d2b37c7
AP
1332 .read_config = af9015_read_config,
1333 .frontend_attach = af9015_af9013_frontend_attach,
1334 .tuner_attach = af9015_tuner_attach,
a3645e54
AP
1335 .init = af9015_init,
1336 .get_rc_config = af9015_get_rc_config,
b905a2a1 1337 .get_stream_config = af9015_get_stream_config,
a3645e54
AP
1338
1339 .get_adapter_count = af9015_get_adapter_count,
1340 .adapter = {
1341 {
1342 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1343 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1344 .pid_filter_count = 32,
1345 .pid_filter = af9015_pid_filter,
1346 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1a590010
AP
1347
1348 .stream = DVB_USB_STREAM_BULK(0x84, 8, TS_USB20_FRAME_SIZE),
1349 }, {
1350 .stream = DVB_USB_STREAM_BULK(0x85, 8, TS_USB20_FRAME_SIZE),
a3645e54 1351 },
a3645e54
AP
1352 },
1353};
1354
1355static const struct usb_device_id af9015_id_table[] = {
1356 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015,
1357 &af9015_props, "Afatech AF9015 reference design", NULL) },
1358 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016,
1359 &af9015_props, "Afatech AF9015 reference design", NULL) },
1360 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD,
1361 &af9015_props, "Leadtek WinFast DTV Dongle Gold", RC_MAP_LEADTEK_Y04G0051) },
1362 { DVB_USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E,
1363 &af9015_props, "Pinnacle PCTV 71e", NULL) },
1364 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U,
1365 &af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) },
1366 { DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TINYTWIN,
1367 &af9015_props, "DigitalNow TinyTwin", RC_MAP_AZUREWAVE_AD_TU700) },
1368 { DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_AZUREWAVE_AD_TU700,
1369 &af9015_props, "TwinHan AzureWave AD-TU700(704J)", RC_MAP_AZUREWAVE_AD_TU700) },
1370 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2,
1371 &af9015_props, "TerraTec Cinergy T USB XE", NULL) },
1372 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T,
1373 &af9015_props, "KWorld PlusTV Dual DVB-T PCI (DVB-T PC160-2T)", NULL) },
1374 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X,
1375 &af9015_props, "AVerMedia AVerTV DVB-T Volar X", RC_MAP_AVERMEDIA_M135A) },
1376 { DVB_USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380,
1377 &af9015_props, "Xtensions XD-380", NULL) },
1378 { DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO,
1379 &af9015_props, "MSI DIGIVOX Duo", RC_MAP_MSI_DIGIVOX_III) },
1380 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2,
1381 &af9015_props, "Fujitsu-Siemens Slim Mobile USB DVB-T", NULL) },
1382 { DVB_USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2,
1383 &af9015_props, "Telestar Starstick 2", NULL) },
1384 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309,
1385 &af9015_props, "AVerMedia A309", NULL) },
1386 { DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III,
1387 &af9015_props, "MSI Digi VOX mini III", RC_MAP_MSI_DIGIVOX_III) },
1388 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U,
1389 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1390 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2,
1391 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1392 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3,
1393 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1394 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT,
1395 &af9015_props, "TrekStor DVB-T USB Stick", RC_MAP_TREKSTOR) },
1396 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850,
1397 &af9015_props, "AverMedia AVerTV Volar Black HD (A850)", NULL) },
1398 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805,
1399 &af9015_props, "AverMedia AVerTV Volar GPS 805 (A805)", NULL) },
1400 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU,
1401 &af9015_props, "Conceptronic USB2.0 DVB-T CTVDIGRCU V3.0", NULL) },
1402 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810,
1403 &af9015_props, "KWorld Digial MC-810", NULL) },
1404 { DVB_USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03,
1405 &af9015_props, "Genius TVGo DVB-T03", NULL) },
1406 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2,
1407 &af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) },
1408 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T,
1409 &af9015_props, "KWorld PlusTV DVB-T PCI Pro Card (DVB-T PC160-T)", NULL) },
1410 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20,
1411 &af9015_props, "Sveon STV20 Tuner USB DVB-T HDTV", NULL) },
1412 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2,
1413 &af9015_props, "DigitalNow TinyTwin v2", RC_MAP_DIGITALNOW_TINYTWIN) },
1414 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS,
1415 &af9015_props, "Leadtek WinFast DTV2000DS", RC_MAP_LEADTEK_Y04G0051) },
1416 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T,
1417 &af9015_props, "KWorld USB DVB-T Stick Mobile (UB383-T)", NULL) },
1418 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4,
1419 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1420 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M,
1421 &af9015_props, "AverMedia AVerTV Volar M (A815Mac)", NULL) },
1422 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_RC,
1423 &af9015_props, "TerraTec Cinergy T Stick RC", RC_MAP_TERRATEC_SLIM_2) },
1424 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC,
1425 &af9015_props, "TerraTec Cinergy T Stick Dual RC", RC_MAP_TERRATEC_SLIM) },
1426 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850T,
1427 &af9015_props, "AverMedia AVerTV Red HD+ (A850T)", NULL) },
1428 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TINYTWIN_3,
1429 &af9015_props, "DigitalNow TinyTwin v3", RC_MAP_DIGITALNOW_TINYTWIN) },
1430 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22,
1431 &af9015_props, "Sveon STV22 Dual USB DVB-T Tuner HDTV", RC_MAP_MSI_DIGIVOX_III) },
1432 { }
1433};
1434MODULE_DEVICE_TABLE(usb, af9015_id_table);
1435
80619de8
AP
1436/* usb specific object needed to register this driver with the usb subsystem */
1437static struct usb_driver af9015_usb_driver = {
a3645e54 1438 .name = KBUILD_MODNAME,
2d2b37c7 1439 .id_table = af9015_id_table,
a3645e54
AP
1440 .probe = dvb_usbv2_probe,
1441 .disconnect = dvb_usbv2_disconnect,
53dc194f
AP
1442 .suspend = dvb_usbv2_suspend,
1443 .resume = dvb_usbv2_resume,
04966aa8 1444 .reset_resume = dvb_usbv2_reset_resume,
a3645e54 1445 .no_dynamic_id = 1,
77f54517 1446 .soft_unbind = 1,
80619de8
AP
1447};
1448
ecb3b2b3 1449module_usb_driver(af9015_usb_driver);
80619de8
AP
1450
1451MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
bc050e67 1452MODULE_DESCRIPTION("Afatech AF9015 driver");
80619de8 1453MODULE_LICENSE("GPL");
bab9b4fe 1454MODULE_FIRMWARE(AF9015_FIRMWARE);
This page took 0.436983 seconds and 5 git commands to generate.