WIP FPC-III support
[linux/fpc-iii.git] / drivers / media / usb / dvb-usb-v2 / af9015.c
blobc70b3cef3176c64230d293b092c286f0befc50e1
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * DVB USB Linux driver for Afatech AF9015 DVB-T USB2.0 receiver
5 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
7 * Thanks to Afatech who kindly provided information.
8 */
10 #include "af9015.h"
12 static int dvb_usb_af9015_remote;
13 module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
14 MODULE_PARM_DESC(remote, "select remote");
15 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
17 static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
19 #define REQ_HDR_LEN 8 /* send header size */
20 #define ACK_HDR_LEN 2 /* rece header size */
21 struct af9015_state *state = d_to_priv(d);
22 struct usb_interface *intf = d->intf;
23 int ret, wlen, rlen;
24 u8 write = 1;
26 mutex_lock(&d->usb_mutex);
28 state->buf[0] = req->cmd;
29 state->buf[1] = state->seq++;
30 state->buf[2] = req->i2c_addr << 1;
31 state->buf[3] = req->addr >> 8;
32 state->buf[4] = req->addr & 0xff;
33 state->buf[5] = req->mbox;
34 state->buf[6] = req->addr_len;
35 state->buf[7] = req->data_len;
37 switch (req->cmd) {
38 case GET_CONFIG:
39 case READ_MEMORY:
40 case RECONNECT_USB:
41 write = 0;
42 break;
43 case READ_I2C:
44 write = 0;
45 state->buf[2] |= 0x01; /* set I2C direction */
46 fallthrough;
47 case WRITE_I2C:
48 state->buf[0] = READ_WRITE_I2C;
49 break;
50 case WRITE_MEMORY:
51 if (((req->addr & 0xff00) == 0xff00) ||
52 ((req->addr & 0xff00) == 0xae00))
53 state->buf[0] = WRITE_VIRTUAL_MEMORY;
54 case WRITE_VIRTUAL_MEMORY:
55 case COPY_FIRMWARE:
56 case DOWNLOAD_FIRMWARE:
57 case BOOT:
58 break;
59 default:
60 dev_err(&intf->dev, "unknown cmd %d\n", req->cmd);
61 ret = -EIO;
62 goto error;
65 /* Buffer overflow check */
66 if ((write && (req->data_len > BUF_LEN - REQ_HDR_LEN)) ||
67 (!write && (req->data_len > BUF_LEN - ACK_HDR_LEN))) {
68 dev_err(&intf->dev, "too much data, cmd %u, len %u\n",
69 req->cmd, req->data_len);
70 ret = -EINVAL;
71 goto error;
75 * Write receives seq + status = 2 bytes
76 * Read receives seq + status + data = 2 + N bytes
78 wlen = REQ_HDR_LEN;
79 rlen = ACK_HDR_LEN;
80 if (write) {
81 wlen += req->data_len;
82 memcpy(&state->buf[REQ_HDR_LEN], req->data, req->data_len);
83 } else {
84 rlen += req->data_len;
87 /* no ack for these packets */
88 if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
89 rlen = 0;
91 ret = dvb_usbv2_generic_rw_locked(d, state->buf, wlen,
92 state->buf, rlen);
93 if (ret)
94 goto error;
96 /* check status */
97 if (rlen && state->buf[1]) {
98 dev_err(&intf->dev, "cmd failed %u\n", state->buf[1]);
99 ret = -EIO;
100 goto error;
103 /* read request, copy returned data to return buf */
104 if (!write)
105 memcpy(req->data, &state->buf[ACK_HDR_LEN], req->data_len);
106 error:
107 mutex_unlock(&d->usb_mutex);
109 return ret;
112 static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
113 u8 val)
115 struct af9015_state *state = d_to_priv(d);
116 struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
118 if (addr == state->af9013_i2c_addr[0] ||
119 addr == state->af9013_i2c_addr[1])
120 req.addr_len = 3;
122 return af9015_ctrl_msg(d, &req);
125 static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
126 u8 *val)
128 struct af9015_state *state = d_to_priv(d);
129 struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
131 if (addr == state->af9013_i2c_addr[0] ||
132 addr == state->af9013_i2c_addr[1])
133 req.addr_len = 3;
135 return af9015_ctrl_msg(d, &req);
138 static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
139 int num)
141 struct dvb_usb_device *d = i2c_get_adapdata(adap);
142 struct af9015_state *state = d_to_priv(d);
143 struct usb_interface *intf = d->intf;
144 int ret;
145 u16 addr;
146 u8 mbox, addr_len;
147 struct req_t req;
150 * I2C multiplexing:
151 * There could be two tuners, both using same I2C address. Demodulator
152 * I2C-gate is only possibility to select correct tuner.
154 * ...........................................
155 * . AF9015 integrates AF9013 demodulator .
156 * . ____________ ____________ . ____________
157 * .| USB IF | | demod |. | tuner |
158 * .|------------| |------------|. |------------|
159 * .| AF9015 | | AF9013 |. | MXL5003 |
160 * .| |--+--I2C-----|-----/ -----|.----I2C-----| |
161 * .| | | | addr 0x1c |. | addr 0x63 |
162 * .|____________| | |____________|. |____________|
163 * .................|.........................
164 * | ____________ ____________
165 * | | demod | | tuner |
166 * | |------------| |------------|
167 * | | AF9013 | | MXL5003 |
168 * +--I2C-----|-----/ -----|-----I2C-----| |
169 * | addr 0x1d | | addr 0x63 |
170 * |____________| |____________|
173 if (msg[0].len == 0 || msg[0].flags & I2C_M_RD) {
174 addr = 0x0000;
175 mbox = 0;
176 addr_len = 0;
177 } else if (msg[0].len == 1) {
178 addr = msg[0].buf[0];
179 mbox = 0;
180 addr_len = 1;
181 } else if (msg[0].len == 2) {
182 addr = msg[0].buf[0] << 8 | msg[0].buf[1] << 0;
183 mbox = 0;
184 addr_len = 2;
185 } else {
186 addr = msg[0].buf[0] << 8 | msg[0].buf[1] << 0;
187 mbox = msg[0].buf[2];
188 addr_len = 3;
191 if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
192 /* i2c write */
193 if (msg[0].len > 21) {
194 ret = -EOPNOTSUPP;
195 goto err;
197 if (msg[0].addr == state->af9013_i2c_addr[0])
198 req.cmd = WRITE_MEMORY;
199 else
200 req.cmd = WRITE_I2C;
201 req.i2c_addr = msg[0].addr;
202 req.addr = addr;
203 req.mbox = mbox;
204 req.addr_len = addr_len;
205 req.data_len = msg[0].len - addr_len;
206 req.data = &msg[0].buf[addr_len];
207 ret = af9015_ctrl_msg(d, &req);
208 } else if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
209 (msg[1].flags & I2C_M_RD)) {
210 /* i2c write + read */
211 if (msg[0].len > 3 || msg[1].len > 61) {
212 ret = -EOPNOTSUPP;
213 goto err;
215 if (msg[0].addr == state->af9013_i2c_addr[0])
216 req.cmd = READ_MEMORY;
217 else
218 req.cmd = READ_I2C;
219 req.i2c_addr = msg[0].addr;
220 req.addr = addr;
221 req.mbox = mbox;
222 req.addr_len = addr_len;
223 req.data_len = msg[1].len;
224 req.data = &msg[1].buf[0];
225 ret = af9015_ctrl_msg(d, &req);
226 } else if (num == 1 && (msg[0].flags & I2C_M_RD)) {
227 /* i2c read */
228 if (msg[0].len > 61) {
229 ret = -EOPNOTSUPP;
230 goto err;
232 if (msg[0].addr == state->af9013_i2c_addr[0]) {
233 ret = -EINVAL;
234 goto err;
236 req.cmd = READ_I2C;
237 req.i2c_addr = msg[0].addr;
238 req.addr = addr;
239 req.mbox = mbox;
240 req.addr_len = addr_len;
241 req.data_len = msg[0].len;
242 req.data = &msg[0].buf[0];
243 ret = af9015_ctrl_msg(d, &req);
244 } else {
245 ret = -EOPNOTSUPP;
246 dev_dbg(&intf->dev, "unknown msg, num %u\n", num);
248 if (ret)
249 goto err;
251 return num;
252 err:
253 dev_dbg(&intf->dev, "failed %d\n", ret);
254 return ret;
257 static u32 af9015_i2c_func(struct i2c_adapter *adapter)
259 return I2C_FUNC_I2C;
262 static struct i2c_algorithm af9015_i2c_algo = {
263 .master_xfer = af9015_i2c_xfer,
264 .functionality = af9015_i2c_func,
267 static int af9015_identify_state(struct dvb_usb_device *d, const char **name)
269 struct usb_interface *intf = d->intf;
270 int ret;
271 u8 reply;
272 struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
274 ret = af9015_ctrl_msg(d, &req);
275 if (ret)
276 return ret;
278 dev_dbg(&intf->dev, "reply %02x\n", reply);
280 if (reply == 0x02)
281 ret = WARM;
282 else
283 ret = COLD;
285 return ret;
288 static int af9015_download_firmware(struct dvb_usb_device *d,
289 const struct firmware *firmware)
291 struct af9015_state *state = d_to_priv(d);
292 struct usb_interface *intf = d->intf;
293 int ret, i, rem;
294 struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
295 u16 checksum;
297 dev_dbg(&intf->dev, "\n");
299 /* Calc checksum, we need it when copy firmware to slave demod */
300 for (i = 0, checksum = 0; i < firmware->size; i++)
301 checksum += firmware->data[i];
303 state->firmware_size = firmware->size;
304 state->firmware_checksum = checksum;
306 #define LEN_MAX (BUF_LEN - REQ_HDR_LEN) /* Max payload size */
307 for (rem = firmware->size; rem > 0; rem -= LEN_MAX) {
308 req.data_len = min(LEN_MAX, rem);
309 req.data = (u8 *)&firmware->data[firmware->size - rem];
310 req.addr = 0x5100 + firmware->size - rem;
311 ret = af9015_ctrl_msg(d, &req);
312 if (ret) {
313 dev_err(&intf->dev, "firmware download failed %d\n",
314 ret);
315 goto err;
319 req.cmd = BOOT;
320 req.data_len = 0;
321 ret = af9015_ctrl_msg(d, &req);
322 if (ret) {
323 dev_err(&intf->dev, "firmware boot failed %d\n", ret);
324 goto err;
327 return 0;
328 err:
329 dev_dbg(&intf->dev, "failed %d\n", ret);
330 return ret;
333 #define AF9015_EEPROM_SIZE 256
334 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
335 #define GOLDEN_RATIO_PRIME_32 0x9e370001UL
337 /* hash (and dump) eeprom */
338 static int af9015_eeprom_hash(struct dvb_usb_device *d)
340 struct af9015_state *state = d_to_priv(d);
341 struct usb_interface *intf = d->intf;
342 int ret, i;
343 u8 buf[AF9015_EEPROM_SIZE];
344 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, NULL};
346 /* read eeprom */
347 for (i = 0; i < AF9015_EEPROM_SIZE; i++) {
348 req.addr = i;
349 req.data = &buf[i];
350 ret = af9015_ctrl_msg(d, &req);
351 if (ret < 0)
352 goto err;
355 /* calculate checksum */
356 for (i = 0; i < AF9015_EEPROM_SIZE / sizeof(u32); i++) {
357 state->eeprom_sum *= GOLDEN_RATIO_PRIME_32;
358 state->eeprom_sum += le32_to_cpu(((__le32 *)buf)[i]);
361 for (i = 0; i < AF9015_EEPROM_SIZE; i += 16)
362 dev_dbg(&intf->dev, "%*ph\n", 16, buf + i);
364 dev_dbg(&intf->dev, "eeprom sum %.8x\n", state->eeprom_sum);
365 return 0;
366 err:
367 dev_dbg(&intf->dev, "failed %d\n", ret);
368 return ret;
371 static int af9015_read_config(struct dvb_usb_device *d)
373 struct af9015_state *state = d_to_priv(d);
374 struct usb_interface *intf = d->intf;
375 int ret;
376 u8 val, i, offset = 0;
377 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
379 dev_dbg(&intf->dev, "\n");
381 /* IR remote controller */
382 req.addr = AF9015_EEPROM_IR_MODE;
383 /* first message will timeout often due to possible hw bug */
384 for (i = 0; i < 4; i++) {
385 ret = af9015_ctrl_msg(d, &req);
386 if (!ret)
387 break;
389 if (ret)
390 goto error;
392 ret = af9015_eeprom_hash(d);
393 if (ret)
394 goto error;
396 state->ir_mode = val;
397 dev_dbg(&intf->dev, "ir mode %02x\n", val);
399 /* TS mode - one or two receivers */
400 req.addr = AF9015_EEPROM_TS_MODE;
401 ret = af9015_ctrl_msg(d, &req);
402 if (ret)
403 goto error;
405 state->dual_mode = val;
406 dev_dbg(&intf->dev, "ts mode %02x\n", state->dual_mode);
408 state->af9013_i2c_addr[0] = AF9015_I2C_DEMOD;
410 if (state->dual_mode) {
411 /* read 2nd demodulator I2C address */
412 req.addr = AF9015_EEPROM_DEMOD2_I2C;
413 ret = af9015_ctrl_msg(d, &req);
414 if (ret)
415 goto error;
417 state->af9013_i2c_addr[1] = val >> 1;
420 for (i = 0; i < state->dual_mode + 1; i++) {
421 if (i == 1)
422 offset = AF9015_EEPROM_OFFSET;
423 /* xtal */
424 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
425 ret = af9015_ctrl_msg(d, &req);
426 if (ret)
427 goto error;
428 switch (val) {
429 case 0:
430 state->af9013_pdata[i].clk = 28800000;
431 break;
432 case 1:
433 state->af9013_pdata[i].clk = 20480000;
434 break;
435 case 2:
436 state->af9013_pdata[i].clk = 28000000;
437 break;
438 case 3:
439 state->af9013_pdata[i].clk = 25000000;
440 break;
442 dev_dbg(&intf->dev, "[%d] xtal %02x, clk %u\n",
443 i, val, state->af9013_pdata[i].clk);
445 /* IF frequency */
446 req.addr = AF9015_EEPROM_IF1H + offset;
447 ret = af9015_ctrl_msg(d, &req);
448 if (ret)
449 goto error;
451 state->af9013_pdata[i].if_frequency = val << 8;
453 req.addr = AF9015_EEPROM_IF1L + offset;
454 ret = af9015_ctrl_msg(d, &req);
455 if (ret)
456 goto error;
458 state->af9013_pdata[i].if_frequency += val;
459 state->af9013_pdata[i].if_frequency *= 1000;
460 dev_dbg(&intf->dev, "[%d] if frequency %u\n",
461 i, state->af9013_pdata[i].if_frequency);
463 /* MT2060 IF1 */
464 req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
465 ret = af9015_ctrl_msg(d, &req);
466 if (ret)
467 goto error;
468 state->mt2060_if1[i] = val << 8;
469 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
470 ret = af9015_ctrl_msg(d, &req);
471 if (ret)
472 goto error;
473 state->mt2060_if1[i] += val;
474 dev_dbg(&intf->dev, "[%d] MT2060 IF1 %u\n",
475 i, state->mt2060_if1[i]);
477 /* tuner */
478 req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
479 ret = af9015_ctrl_msg(d, &req);
480 if (ret)
481 goto error;
482 switch (val) {
483 case AF9013_TUNER_ENV77H11D5:
484 case AF9013_TUNER_MT2060:
485 case AF9013_TUNER_QT1010:
486 case AF9013_TUNER_UNKNOWN:
487 case AF9013_TUNER_MT2060_2:
488 case AF9013_TUNER_TDA18271:
489 case AF9013_TUNER_QT1010A:
490 case AF9013_TUNER_TDA18218:
491 state->af9013_pdata[i].spec_inv = 1;
492 break;
493 case AF9013_TUNER_MXL5003D:
494 case AF9013_TUNER_MXL5005D:
495 case AF9013_TUNER_MXL5005R:
496 case AF9013_TUNER_MXL5007T:
497 state->af9013_pdata[i].spec_inv = 0;
498 break;
499 case AF9013_TUNER_MC44S803:
500 state->af9013_pdata[i].gpio[1] = AF9013_GPIO_LO;
501 state->af9013_pdata[i].spec_inv = 1;
502 break;
503 default:
504 dev_err(&intf->dev,
505 "tuner id %02x not supported, please report!\n",
506 val);
507 return -ENODEV;
510 state->af9013_pdata[i].tuner = val;
511 dev_dbg(&intf->dev, "[%d] tuner id %02x\n", i, val);
514 error:
515 if (ret)
516 dev_err(&intf->dev, "eeprom read failed %d\n", ret);
519 * AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
520 * content :-( Override some wrong values here. Ditto for the
521 * AVerTV Red HD+ (A850T) device.
523 if (le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
524 ((le16_to_cpu(d->udev->descriptor.idProduct) == USB_PID_AVERMEDIA_A850) ||
525 (le16_to_cpu(d->udev->descriptor.idProduct) == USB_PID_AVERMEDIA_A850T))) {
526 dev_dbg(&intf->dev, "AverMedia A850: overriding config\n");
527 /* disable dual mode */
528 state->dual_mode = 0;
530 /* set correct IF */
531 state->af9013_pdata[0].if_frequency = 4570000;
534 return ret;
537 static int af9015_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
538 struct usb_data_stream_properties *stream)
540 struct dvb_usb_device *d = fe_to_d(fe);
541 struct usb_interface *intf = d->intf;
543 dev_dbg(&intf->dev, "adap %u\n", fe_to_adap(fe)->id);
545 if (d->udev->speed == USB_SPEED_FULL)
546 stream->u.bulk.buffersize = 5 * 188;
548 return 0;
551 static int af9015_streaming_ctrl(struct dvb_frontend *fe, int onoff)
553 struct dvb_usb_device *d = fe_to_d(fe);
554 struct af9015_state *state = d_to_priv(d);
555 struct usb_interface *intf = d->intf;
556 int ret;
557 unsigned int utmp1, utmp2, reg1, reg2;
558 u8 buf[2];
559 const unsigned int adap_id = fe_to_adap(fe)->id;
561 dev_dbg(&intf->dev, "adap id %d, onoff %d\n", adap_id, onoff);
563 if (!state->usb_ts_if_configured[adap_id]) {
564 dev_dbg(&intf->dev, "set usb and ts interface\n");
566 /* USB IF stream settings */
567 utmp1 = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
568 utmp2 = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
570 buf[0] = (utmp1 >> 0) & 0xff;
571 buf[1] = (utmp1 >> 8) & 0xff;
572 if (adap_id == 0) {
573 /* 1st USB IF (EP4) stream settings */
574 reg1 = 0xdd88;
575 reg2 = 0xdd0c;
576 } else {
577 /* 2nd USB IF (EP5) stream settings */
578 reg1 = 0xdd8a;
579 reg2 = 0xdd0d;
581 ret = regmap_bulk_write(state->regmap, reg1, buf, 2);
582 if (ret)
583 goto err;
584 ret = regmap_write(state->regmap, reg2, utmp2);
585 if (ret)
586 goto err;
588 /* TS IF settings */
589 if (state->dual_mode) {
590 utmp1 = 0x01;
591 utmp2 = 0x10;
592 } else {
593 utmp1 = 0x00;
594 utmp2 = 0x00;
596 ret = regmap_update_bits(state->regmap, 0xd50b, 0x01, utmp1);
597 if (ret)
598 goto err;
599 ret = regmap_update_bits(state->regmap, 0xd520, 0x10, utmp2);
600 if (ret)
601 goto err;
603 state->usb_ts_if_configured[adap_id] = true;
606 if (adap_id == 0 && onoff) {
607 /* Adapter 0 stream on. EP4: clear NAK, enable, clear reset */
608 ret = regmap_update_bits(state->regmap, 0xdd13, 0x20, 0x00);
609 if (ret)
610 goto err;
611 ret = regmap_update_bits(state->regmap, 0xdd11, 0x20, 0x20);
612 if (ret)
613 goto err;
614 ret = regmap_update_bits(state->regmap, 0xd507, 0x04, 0x00);
615 if (ret)
616 goto err;
617 } else if (adap_id == 1 && onoff) {
618 /* Adapter 1 stream on. EP5: clear NAK, enable, clear reset */
619 ret = regmap_update_bits(state->regmap, 0xdd13, 0x40, 0x00);
620 if (ret)
621 goto err;
622 ret = regmap_update_bits(state->regmap, 0xdd11, 0x40, 0x40);
623 if (ret)
624 goto err;
625 ret = regmap_update_bits(state->regmap, 0xd50b, 0x02, 0x00);
626 if (ret)
627 goto err;
628 } else if (adap_id == 0 && !onoff) {
629 /* Adapter 0 stream off. EP4: set reset, disable, set NAK */
630 ret = regmap_update_bits(state->regmap, 0xd507, 0x04, 0x04);
631 if (ret)
632 goto err;
633 ret = regmap_update_bits(state->regmap, 0xdd11, 0x20, 0x00);
634 if (ret)
635 goto err;
636 ret = regmap_update_bits(state->regmap, 0xdd13, 0x20, 0x20);
637 if (ret)
638 goto err;
639 } else if (adap_id == 1 && !onoff) {
640 /* Adapter 1 stream off. EP5: set reset, disable, set NAK */
641 ret = regmap_update_bits(state->regmap, 0xd50b, 0x02, 0x02);
642 if (ret)
643 goto err;
644 ret = regmap_update_bits(state->regmap, 0xdd11, 0x40, 0x00);
645 if (ret)
646 goto err;
647 ret = regmap_update_bits(state->regmap, 0xdd13, 0x40, 0x40);
648 if (ret)
649 goto err;
652 return 0;
653 err:
654 dev_dbg(&intf->dev, "failed %d\n", ret);
655 return ret;
658 static int af9015_get_adapter_count(struct dvb_usb_device *d)
660 struct af9015_state *state = d_to_priv(d);
662 return state->dual_mode + 1;
665 /* override demod callbacks for resource locking */
666 static int af9015_af9013_set_frontend(struct dvb_frontend *fe)
668 int ret;
669 struct af9015_state *state = fe_to_priv(fe);
671 if (mutex_lock_interruptible(&state->fe_mutex))
672 return -EAGAIN;
674 ret = state->set_frontend[fe_to_adap(fe)->id](fe);
676 mutex_unlock(&state->fe_mutex);
678 return ret;
681 /* override demod callbacks for resource locking */
682 static int af9015_af9013_read_status(struct dvb_frontend *fe,
683 enum fe_status *status)
685 int ret;
686 struct af9015_state *state = fe_to_priv(fe);
688 if (mutex_lock_interruptible(&state->fe_mutex))
689 return -EAGAIN;
691 ret = state->read_status[fe_to_adap(fe)->id](fe, status);
693 mutex_unlock(&state->fe_mutex);
695 return ret;
698 /* override demod callbacks for resource locking */
699 static int af9015_af9013_init(struct dvb_frontend *fe)
701 int ret;
702 struct af9015_state *state = fe_to_priv(fe);
704 if (mutex_lock_interruptible(&state->fe_mutex))
705 return -EAGAIN;
707 ret = state->init[fe_to_adap(fe)->id](fe);
709 mutex_unlock(&state->fe_mutex);
711 return ret;
714 /* override demod callbacks for resource locking */
715 static int af9015_af9013_sleep(struct dvb_frontend *fe)
717 int ret;
718 struct af9015_state *state = fe_to_priv(fe);
720 if (mutex_lock_interruptible(&state->fe_mutex))
721 return -EAGAIN;
723 ret = state->sleep[fe_to_adap(fe)->id](fe);
725 mutex_unlock(&state->fe_mutex);
727 return ret;
730 /* override tuner callbacks for resource locking */
731 static int af9015_tuner_init(struct dvb_frontend *fe)
733 int ret;
734 struct af9015_state *state = fe_to_priv(fe);
736 if (mutex_lock_interruptible(&state->fe_mutex))
737 return -EAGAIN;
739 ret = state->tuner_init[fe_to_adap(fe)->id](fe);
741 mutex_unlock(&state->fe_mutex);
743 return ret;
746 /* override tuner callbacks for resource locking */
747 static int af9015_tuner_sleep(struct dvb_frontend *fe)
749 int ret;
750 struct af9015_state *state = fe_to_priv(fe);
752 if (mutex_lock_interruptible(&state->fe_mutex))
753 return -EAGAIN;
755 ret = state->tuner_sleep[fe_to_adap(fe)->id](fe);
757 mutex_unlock(&state->fe_mutex);
759 return ret;
762 static int af9015_copy_firmware(struct dvb_usb_device *d)
764 struct af9015_state *state = d_to_priv(d);
765 struct usb_interface *intf = d->intf;
766 int ret;
767 unsigned long timeout;
768 u8 val, firmware_info[4];
769 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, 4, firmware_info};
771 dev_dbg(&intf->dev, "\n");
773 firmware_info[0] = (state->firmware_size >> 8) & 0xff;
774 firmware_info[1] = (state->firmware_size >> 0) & 0xff;
775 firmware_info[2] = (state->firmware_checksum >> 8) & 0xff;
776 firmware_info[3] = (state->firmware_checksum >> 0) & 0xff;
778 /* Check whether firmware is already running */
779 ret = af9015_read_reg_i2c(d, state->af9013_i2c_addr[1], 0x98be, &val);
780 if (ret)
781 goto err;
783 dev_dbg(&intf->dev, "firmware status %02x\n", val);
785 if (val == 0x0c)
786 return 0;
788 /* Set i2c clock to 625kHz to speed up firmware copy */
789 ret = regmap_write(state->regmap, 0xd416, 0x04);
790 if (ret)
791 goto err;
793 /* Copy firmware from master demod to slave demod */
794 ret = af9015_ctrl_msg(d, &req);
795 if (ret) {
796 dev_err(&intf->dev, "firmware copy cmd failed %d\n", ret);
797 goto err;
800 /* Set i2c clock to 125kHz */
801 ret = regmap_write(state->regmap, 0xd416, 0x14);
802 if (ret)
803 goto err;
805 /* Boot firmware */
806 ret = af9015_write_reg_i2c(d, state->af9013_i2c_addr[1], 0xe205, 0x01);
807 if (ret)
808 goto err;
810 /* Poll firmware ready */
811 for (val = 0x00, timeout = jiffies + msecs_to_jiffies(1000);
812 !time_after(jiffies, timeout) && val != 0x0c && val != 0x04;) {
813 msleep(20);
815 /* Check firmware status. 0c=OK, 04=fail */
816 ret = af9015_read_reg_i2c(d, state->af9013_i2c_addr[1],
817 0x98be, &val);
818 if (ret)
819 goto err;
821 dev_dbg(&intf->dev, "firmware status %02x\n", val);
824 dev_dbg(&intf->dev, "firmware boot took %u ms\n",
825 jiffies_to_msecs(jiffies) - (jiffies_to_msecs(timeout) - 1000));
827 if (val == 0x04) {
828 ret = -ENODEV;
829 dev_err(&intf->dev, "firmware did not run\n");
830 goto err;
831 } else if (val != 0x0c) {
832 ret = -ETIMEDOUT;
833 dev_err(&intf->dev, "firmware boot timeout\n");
834 goto err;
837 return 0;
838 err:
839 dev_dbg(&intf->dev, "failed %d\n", ret);
840 return ret;
843 static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
845 struct af9015_state *state = adap_to_priv(adap);
846 struct dvb_usb_device *d = adap_to_d(adap);
847 struct usb_interface *intf = d->intf;
848 struct i2c_client *client;
849 int ret;
851 dev_dbg(&intf->dev, "adap id %u\n", adap->id);
853 if (adap->id == 0) {
854 state->af9013_pdata[0].ts_mode = AF9013_TS_MODE_USB;
855 memcpy(state->af9013_pdata[0].api_version, "\x0\x1\x9\x0", 4);
856 state->af9013_pdata[0].gpio[0] = AF9013_GPIO_HI;
857 state->af9013_pdata[0].gpio[3] = AF9013_GPIO_TUNER_ON;
858 } else if (adap->id == 1) {
859 state->af9013_pdata[1].ts_mode = AF9013_TS_MODE_SERIAL;
860 state->af9013_pdata[1].ts_output_pin = 7;
861 memcpy(state->af9013_pdata[1].api_version, "\x0\x1\x9\x0", 4);
862 state->af9013_pdata[1].gpio[0] = AF9013_GPIO_TUNER_ON;
863 state->af9013_pdata[1].gpio[1] = AF9013_GPIO_LO;
865 /* copy firmware to 2nd demodulator */
866 if (state->dual_mode) {
867 /* Wait 2nd demodulator ready */
868 msleep(100);
870 ret = af9015_copy_firmware(adap_to_d(adap));
871 if (ret) {
872 dev_err(&intf->dev,
873 "firmware copy to 2nd frontend failed, will disable it\n");
874 state->dual_mode = 0;
875 goto err;
877 } else {
878 ret = -ENODEV;
879 goto err;
883 /* Add I2C demod */
884 client = dvb_module_probe("af9013", NULL, &d->i2c_adap,
885 state->af9013_i2c_addr[adap->id],
886 &state->af9013_pdata[adap->id]);
887 if (!client) {
888 ret = -ENODEV;
889 goto err;
891 adap->fe[0] = state->af9013_pdata[adap->id].get_dvb_frontend(client);
892 state->demod_i2c_client[adap->id] = client;
895 * AF9015 firmware does not like if it gets interrupted by I2C adapter
896 * request on some critical phases. During normal operation I2C adapter
897 * is used only 2nd demodulator and tuner on dual tuner devices.
898 * Override demodulator callbacks and use mutex for limit access to
899 * those "critical" paths to keep AF9015 happy.
901 if (adap->fe[0]) {
902 state->set_frontend[adap->id] = adap->fe[0]->ops.set_frontend;
903 adap->fe[0]->ops.set_frontend = af9015_af9013_set_frontend;
904 state->read_status[adap->id] = adap->fe[0]->ops.read_status;
905 adap->fe[0]->ops.read_status = af9015_af9013_read_status;
906 state->init[adap->id] = adap->fe[0]->ops.init;
907 adap->fe[0]->ops.init = af9015_af9013_init;
908 state->sleep[adap->id] = adap->fe[0]->ops.sleep;
909 adap->fe[0]->ops.sleep = af9015_af9013_sleep;
912 return 0;
913 err:
914 dev_dbg(&intf->dev, "failed %d\n", ret);
915 return ret;
918 static int af9015_frontend_detach(struct dvb_usb_adapter *adap)
920 struct af9015_state *state = adap_to_priv(adap);
921 struct dvb_usb_device *d = adap_to_d(adap);
922 struct usb_interface *intf = d->intf;
923 struct i2c_client *client;
925 dev_dbg(&intf->dev, "adap id %u\n", adap->id);
927 /* Remove I2C demod */
928 client = state->demod_i2c_client[adap->id];
929 dvb_module_release(client);
931 return 0;
934 static struct mt2060_config af9015_mt2060_config = {
935 .i2c_address = 0x60,
936 .clock_out = 0,
939 static struct qt1010_config af9015_qt1010_config = {
940 .i2c_address = 0x62,
943 static struct tda18271_config af9015_tda18271_config = {
944 .gate = TDA18271_GATE_DIGITAL,
945 .small_i2c = TDA18271_16_BYTE_CHUNK_INIT,
948 static struct mxl5005s_config af9015_mxl5003_config = {
949 .i2c_address = 0x63,
950 .if_freq = IF_FREQ_4570000HZ,
951 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
952 .agc_mode = MXL_SINGLE_AGC,
953 .tracking_filter = MXL_TF_DEFAULT,
954 .rssi_enable = MXL_RSSI_ENABLE,
955 .cap_select = MXL_CAP_SEL_ENABLE,
956 .div_out = MXL_DIV_OUT_4,
957 .clock_out = MXL_CLOCK_OUT_DISABLE,
958 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
959 .top = MXL5005S_TOP_25P2,
960 .mod_mode = MXL_DIGITAL_MODE,
961 .if_mode = MXL_ZERO_IF,
962 .AgcMasterByte = 0x00,
965 static struct mxl5005s_config af9015_mxl5005_config = {
966 .i2c_address = 0x63,
967 .if_freq = IF_FREQ_4570000HZ,
968 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
969 .agc_mode = MXL_SINGLE_AGC,
970 .tracking_filter = MXL_TF_OFF,
971 .rssi_enable = MXL_RSSI_ENABLE,
972 .cap_select = MXL_CAP_SEL_ENABLE,
973 .div_out = MXL_DIV_OUT_4,
974 .clock_out = MXL_CLOCK_OUT_DISABLE,
975 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
976 .top = MXL5005S_TOP_25P2,
977 .mod_mode = MXL_DIGITAL_MODE,
978 .if_mode = MXL_ZERO_IF,
979 .AgcMasterByte = 0x00,
982 static struct mc44s803_config af9015_mc44s803_config = {
983 .i2c_address = 0x60,
984 .dig_out = 1,
987 static struct tda18218_config af9015_tda18218_config = {
988 .i2c_address = 0x60,
989 .i2c_wr_max = 21, /* max wr bytes AF9015 I2C adap can handle at once */
992 static struct mxl5007t_config af9015_mxl5007t_config = {
993 .xtal_freq_hz = MxL_XTAL_24_MHZ,
994 .if_freq_hz = MxL_IF_4_57_MHZ,
997 static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
999 struct dvb_usb_device *d = adap_to_d(adap);
1000 struct af9015_state *state = d_to_priv(d);
1001 struct usb_interface *intf = d->intf;
1002 struct i2c_client *client;
1003 struct i2c_adapter *adapter;
1004 int ret;
1006 dev_dbg(&intf->dev, "adap id %u\n", adap->id);
1008 client = state->demod_i2c_client[adap->id];
1009 adapter = state->af9013_pdata[adap->id].get_i2c_adapter(client);
1011 switch (state->af9013_pdata[adap->id].tuner) {
1012 case AF9013_TUNER_MT2060:
1013 case AF9013_TUNER_MT2060_2:
1014 ret = dvb_attach(mt2060_attach, adap->fe[0], adapter,
1015 &af9015_mt2060_config,
1016 state->mt2060_if1[adap->id]) == NULL ? -ENODEV : 0;
1017 break;
1018 case AF9013_TUNER_QT1010:
1019 case AF9013_TUNER_QT1010A:
1020 ret = dvb_attach(qt1010_attach, adap->fe[0], adapter,
1021 &af9015_qt1010_config) == NULL ? -ENODEV : 0;
1022 break;
1023 case AF9013_TUNER_TDA18271:
1024 ret = dvb_attach(tda18271_attach, adap->fe[0], 0x60, adapter,
1025 &af9015_tda18271_config) == NULL ? -ENODEV : 0;
1026 break;
1027 case AF9013_TUNER_TDA18218:
1028 ret = dvb_attach(tda18218_attach, adap->fe[0], adapter,
1029 &af9015_tda18218_config) == NULL ? -ENODEV : 0;
1030 break;
1031 case AF9013_TUNER_MXL5003D:
1032 ret = dvb_attach(mxl5005s_attach, adap->fe[0], adapter,
1033 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
1034 break;
1035 case AF9013_TUNER_MXL5005D:
1036 case AF9013_TUNER_MXL5005R:
1037 ret = dvb_attach(mxl5005s_attach, adap->fe[0], adapter,
1038 &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
1039 break;
1040 case AF9013_TUNER_ENV77H11D5:
1041 ret = dvb_attach(dvb_pll_attach, adap->fe[0], 0x60, adapter,
1042 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
1043 break;
1044 case AF9013_TUNER_MC44S803:
1045 ret = dvb_attach(mc44s803_attach, adap->fe[0], adapter,
1046 &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
1047 break;
1048 case AF9013_TUNER_MXL5007T:
1049 ret = dvb_attach(mxl5007t_attach, adap->fe[0], adapter,
1050 0x60, &af9015_mxl5007t_config) == NULL ? -ENODEV : 0;
1051 break;
1052 case AF9013_TUNER_UNKNOWN:
1053 default:
1054 dev_err(&intf->dev, "unknown tuner, tuner id %02x\n",
1055 state->af9013_pdata[adap->id].tuner);
1056 ret = -ENODEV;
1059 if (adap->fe[0]->ops.tuner_ops.init) {
1060 state->tuner_init[adap->id] =
1061 adap->fe[0]->ops.tuner_ops.init;
1062 adap->fe[0]->ops.tuner_ops.init = af9015_tuner_init;
1065 if (adap->fe[0]->ops.tuner_ops.sleep) {
1066 state->tuner_sleep[adap->id] =
1067 adap->fe[0]->ops.tuner_ops.sleep;
1068 adap->fe[0]->ops.tuner_ops.sleep = af9015_tuner_sleep;
1071 return ret;
1074 static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1076 struct af9015_state *state = adap_to_priv(adap);
1077 struct af9013_platform_data *pdata = &state->af9013_pdata[adap->id];
1078 int ret;
1080 mutex_lock(&state->fe_mutex);
1081 ret = pdata->pid_filter_ctrl(adap->fe[0], onoff);
1082 mutex_unlock(&state->fe_mutex);
1084 return ret;
1087 static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index,
1088 u16 pid, int onoff)
1090 struct af9015_state *state = adap_to_priv(adap);
1091 struct af9013_platform_data *pdata = &state->af9013_pdata[adap->id];
1092 int ret;
1094 mutex_lock(&state->fe_mutex);
1095 ret = pdata->pid_filter(adap->fe[0], index, pid, onoff);
1096 mutex_unlock(&state->fe_mutex);
1098 return ret;
1101 static int af9015_init(struct dvb_usb_device *d)
1103 struct af9015_state *state = d_to_priv(d);
1104 struct usb_interface *intf = d->intf;
1105 int ret;
1107 dev_dbg(&intf->dev, "\n");
1109 mutex_init(&state->fe_mutex);
1111 /* init RC canary */
1112 ret = regmap_write(state->regmap, 0x98e9, 0xff);
1113 if (ret)
1114 goto error;
1116 error:
1117 return ret;
1120 #if IS_ENABLED(CONFIG_RC_CORE)
1121 struct af9015_rc_setup {
1122 unsigned int id;
1123 char *rc_codes;
1126 static char *af9015_rc_setup_match(unsigned int id,
1127 const struct af9015_rc_setup *table)
1129 for (; table->rc_codes; table++)
1130 if (table->id == id)
1131 return table->rc_codes;
1132 return NULL;
1135 static const struct af9015_rc_setup af9015_rc_setup_modparam[] = {
1136 { AF9015_REMOTE_A_LINK_DTU_M, RC_MAP_ALINK_DTU_M },
1137 { AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3, RC_MAP_MSI_DIGIVOX_II },
1138 { AF9015_REMOTE_MYGICTV_U718, RC_MAP_TOTAL_MEDIA_IN_HAND },
1139 { AF9015_REMOTE_DIGITTRADE_DVB_T, RC_MAP_DIGITTRADE },
1140 { AF9015_REMOTE_AVERMEDIA_KS, RC_MAP_AVERMEDIA_RM_KS },
1144 static const struct af9015_rc_setup af9015_rc_setup_hashes[] = {
1145 { 0xb8feb708, RC_MAP_MSI_DIGIVOX_II },
1146 { 0xa3703d00, RC_MAP_ALINK_DTU_M },
1147 { 0x9b7dc64e, RC_MAP_TOTAL_MEDIA_IN_HAND }, /* MYGICTV U718 */
1148 { 0x5d49e3db, RC_MAP_DIGITTRADE }, /* LC-Power LC-USB-DVBT */
1152 static int af9015_rc_query(struct dvb_usb_device *d)
1154 struct af9015_state *state = d_to_priv(d);
1155 struct usb_interface *intf = d->intf;
1156 int ret;
1157 u8 buf[17];
1159 /* read registers needed to detect remote controller code */
1160 ret = regmap_bulk_read(state->regmap, 0x98d9, buf, sizeof(buf));
1161 if (ret)
1162 goto error;
1164 /* If any of these are non-zero, assume invalid data */
1165 if (buf[1] || buf[2] || buf[3]) {
1166 dev_dbg(&intf->dev, "invalid data\n");
1167 return ret;
1170 /* Check for repeat of previous code */
1171 if ((state->rc_repeat != buf[6] || buf[0]) &&
1172 !memcmp(&buf[12], state->rc_last, 4)) {
1173 dev_dbg(&intf->dev, "key repeated\n");
1174 rc_repeat(d->rc_dev);
1175 state->rc_repeat = buf[6];
1176 return ret;
1179 /* Only process key if canary killed */
1180 if (buf[16] != 0xff && buf[0] != 0x01) {
1181 enum rc_proto proto;
1183 dev_dbg(&intf->dev, "key pressed %*ph\n", 4, buf + 12);
1185 /* Reset the canary */
1186 ret = regmap_write(state->regmap, 0x98e9, 0xff);
1187 if (ret)
1188 goto error;
1190 /* Remember this key */
1191 memcpy(state->rc_last, &buf[12], 4);
1192 if (buf[14] == (u8)~buf[15]) {
1193 if (buf[12] == (u8)~buf[13]) {
1194 /* NEC */
1195 state->rc_keycode = RC_SCANCODE_NEC(buf[12],
1196 buf[14]);
1197 proto = RC_PROTO_NEC;
1198 } else {
1199 /* NEC extended*/
1200 state->rc_keycode = RC_SCANCODE_NECX(buf[12] << 8 |
1201 buf[13],
1202 buf[14]);
1203 proto = RC_PROTO_NECX;
1205 } else {
1206 /* 32 bit NEC */
1207 state->rc_keycode = RC_SCANCODE_NEC32(buf[12] << 24 |
1208 buf[13] << 16 |
1209 buf[14] << 8 |
1210 buf[15]);
1211 proto = RC_PROTO_NEC32;
1213 rc_keydown(d->rc_dev, proto, state->rc_keycode, 0);
1214 } else {
1215 dev_dbg(&intf->dev, "no key press\n");
1216 /* Invalidate last keypress */
1217 /* Not really needed, but helps with debug */
1218 state->rc_last[2] = state->rc_last[3];
1221 state->rc_repeat = buf[6];
1222 state->rc_failed = false;
1224 error:
1225 if (ret) {
1226 dev_warn(&intf->dev, "rc query failed %d\n", ret);
1228 /* allow random errors as dvb-usb will stop polling on error */
1229 if (!state->rc_failed)
1230 ret = 0;
1232 state->rc_failed = true;
1235 return ret;
1238 static int af9015_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1240 struct af9015_state *state = d_to_priv(d);
1241 u16 vid = le16_to_cpu(d->udev->descriptor.idVendor);
1243 if (state->ir_mode == AF9015_IR_MODE_DISABLED)
1244 return 0;
1246 /* try to load remote based module param */
1247 if (!rc->map_name)
1248 rc->map_name = af9015_rc_setup_match(dvb_usb_af9015_remote,
1249 af9015_rc_setup_modparam);
1251 /* try to load remote based eeprom hash */
1252 if (!rc->map_name)
1253 rc->map_name = af9015_rc_setup_match(state->eeprom_sum,
1254 af9015_rc_setup_hashes);
1256 /* try to load remote based USB iManufacturer string */
1257 if (!rc->map_name && vid == USB_VID_AFATECH) {
1259 * Check USB manufacturer and product strings and try
1260 * to determine correct remote in case of chip vendor
1261 * reference IDs are used.
1262 * DO NOT ADD ANYTHING NEW HERE. Use hashes instead.
1264 char manufacturer[10];
1266 memset(manufacturer, 0, sizeof(manufacturer));
1267 usb_string(d->udev, d->udev->descriptor.iManufacturer,
1268 manufacturer, sizeof(manufacturer));
1269 if (!strcmp("MSI", manufacturer)) {
1271 * iManufacturer 1 MSI
1272 * iProduct 2 MSI K-VOX
1274 rc->map_name = af9015_rc_setup_match(AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
1275 af9015_rc_setup_modparam);
1279 /* load empty to enable rc */
1280 if (!rc->map_name)
1281 rc->map_name = RC_MAP_EMPTY;
1283 rc->allowed_protos = RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
1284 RC_PROTO_BIT_NEC32;
1285 rc->query = af9015_rc_query;
1286 rc->interval = 500;
1288 return 0;
1290 #else
1291 #define af9015_get_rc_config NULL
1292 #endif
1294 static int af9015_regmap_write(void *context, const void *data, size_t count)
1296 struct dvb_usb_device *d = context;
1297 struct usb_interface *intf = d->intf;
1298 int ret;
1299 u16 reg = ((u8 *)data)[0] << 8 | ((u8 *)data)[1] << 0;
1300 u8 *val = &((u8 *)data)[2];
1301 const unsigned int len = count - 2;
1302 struct req_t req = {WRITE_MEMORY, 0, reg, 0, 0, len, val};
1304 ret = af9015_ctrl_msg(d, &req);
1305 if (ret)
1306 goto err;
1308 return 0;
1309 err:
1310 dev_dbg(&intf->dev, "failed %d\n", ret);
1311 return ret;
1314 static int af9015_regmap_read(void *context, const void *reg_buf,
1315 size_t reg_size, void *val_buf, size_t val_size)
1317 struct dvb_usb_device *d = context;
1318 struct usb_interface *intf = d->intf;
1319 int ret;
1320 u16 reg = ((u8 *)reg_buf)[0] << 8 | ((u8 *)reg_buf)[1] << 0;
1321 u8 *val = &((u8 *)val_buf)[0];
1322 const unsigned int len = val_size;
1323 struct req_t req = {READ_MEMORY, 0, reg, 0, 0, len, val};
1325 ret = af9015_ctrl_msg(d, &req);
1326 if (ret)
1327 goto err;
1329 return 0;
1330 err:
1331 dev_dbg(&intf->dev, "failed %d\n", ret);
1332 return ret;
1335 static int af9015_probe(struct dvb_usb_device *d)
1337 struct af9015_state *state = d_to_priv(d);
1338 struct usb_interface *intf = d->intf;
1339 struct usb_device *udev = interface_to_usbdev(intf);
1340 int ret;
1341 char manufacturer[sizeof("ITE Technologies, Inc.")];
1342 static const struct regmap_config regmap_config = {
1343 .reg_bits = 16,
1344 .val_bits = 8,
1346 static const struct regmap_bus regmap_bus = {
1347 .read = af9015_regmap_read,
1348 .write = af9015_regmap_write,
1351 dev_dbg(&intf->dev, "\n");
1353 memset(manufacturer, 0, sizeof(manufacturer));
1354 usb_string(udev, udev->descriptor.iManufacturer,
1355 manufacturer, sizeof(manufacturer));
1357 * There is two devices having same ID but different chipset. One uses
1358 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1359 * is iManufacturer string.
1361 * idVendor 0x0ccd TerraTec Electronic GmbH
1362 * idProduct 0x0099
1363 * bcdDevice 2.00
1364 * iManufacturer 1 Afatech
1365 * iProduct 2 DVB-T 2
1367 * idVendor 0x0ccd TerraTec Electronic GmbH
1368 * idProduct 0x0099
1369 * bcdDevice 2.00
1370 * iManufacturer 1 ITE Technologies, Inc.
1371 * iProduct 2 DVB-T TV Stick
1373 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1374 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1375 if (!strcmp("ITE Technologies, Inc.", manufacturer)) {
1376 ret = -ENODEV;
1377 dev_dbg(&intf->dev, "rejecting device\n");
1378 goto err;
1382 state->regmap = regmap_init(&intf->dev, &regmap_bus, d, &regmap_config);
1383 if (IS_ERR(state->regmap)) {
1384 ret = PTR_ERR(state->regmap);
1385 goto err;
1388 return 0;
1389 err:
1390 dev_dbg(&intf->dev, "failed %d\n", ret);
1391 return ret;
1394 static void af9015_disconnect(struct dvb_usb_device *d)
1396 struct af9015_state *state = d_to_priv(d);
1397 struct usb_interface *intf = d->intf;
1399 dev_dbg(&intf->dev, "\n");
1401 regmap_exit(state->regmap);
1405 * Interface 0 is used by DVB-T receiver and
1406 * interface 1 is for remote controller (HID)
1408 static const struct dvb_usb_device_properties af9015_props = {
1409 .driver_name = KBUILD_MODNAME,
1410 .owner = THIS_MODULE,
1411 .adapter_nr = adapter_nr,
1412 .size_of_priv = sizeof(struct af9015_state),
1414 .generic_bulk_ctrl_endpoint = 0x02,
1415 .generic_bulk_ctrl_endpoint_response = 0x81,
1417 .probe = af9015_probe,
1418 .disconnect = af9015_disconnect,
1419 .identify_state = af9015_identify_state,
1420 .firmware = AF9015_FIRMWARE,
1421 .download_firmware = af9015_download_firmware,
1423 .i2c_algo = &af9015_i2c_algo,
1424 .read_config = af9015_read_config,
1425 .frontend_attach = af9015_af9013_frontend_attach,
1426 .frontend_detach = af9015_frontend_detach,
1427 .tuner_attach = af9015_tuner_attach,
1428 .init = af9015_init,
1429 .get_rc_config = af9015_get_rc_config,
1430 .get_stream_config = af9015_get_stream_config,
1431 .streaming_ctrl = af9015_streaming_ctrl,
1433 .get_adapter_count = af9015_get_adapter_count,
1434 .adapter = {
1436 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1437 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1438 .pid_filter_count = 32,
1439 .pid_filter = af9015_pid_filter,
1440 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1442 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1443 }, {
1444 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1445 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1446 .pid_filter_count = 32,
1447 .pid_filter = af9015_pid_filter,
1448 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1450 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1455 static const struct usb_device_id af9015_id_table[] = {
1456 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015,
1457 &af9015_props, "Afatech AF9015 reference design", NULL) },
1458 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016,
1459 &af9015_props, "Afatech AF9015 reference design", NULL) },
1460 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD,
1461 &af9015_props, "Leadtek WinFast DTV Dongle Gold", RC_MAP_LEADTEK_Y04G0051) },
1462 { DVB_USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E,
1463 &af9015_props, "Pinnacle PCTV 71e", NULL) },
1464 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U,
1465 &af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) },
1466 { DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TINYTWIN,
1467 &af9015_props, "DigitalNow TinyTwin", RC_MAP_AZUREWAVE_AD_TU700) },
1468 { DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_AZUREWAVE_AD_TU700,
1469 &af9015_props, "TwinHan AzureWave AD-TU700(704J)", RC_MAP_AZUREWAVE_AD_TU700) },
1470 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2,
1471 &af9015_props, "TerraTec Cinergy T USB XE", NULL) },
1472 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T,
1473 &af9015_props, "KWorld PlusTV Dual DVB-T PCI (DVB-T PC160-2T)", NULL) },
1474 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X,
1475 &af9015_props, "AVerMedia AVerTV DVB-T Volar X", RC_MAP_AVERMEDIA_M135A) },
1476 { DVB_USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380,
1477 &af9015_props, "Xtensions XD-380", NULL) },
1478 { DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO,
1479 &af9015_props, "MSI DIGIVOX Duo", RC_MAP_MSI_DIGIVOX_III) },
1480 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2,
1481 &af9015_props, "Fujitsu-Siemens Slim Mobile USB DVB-T", NULL) },
1482 { DVB_USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2,
1483 &af9015_props, "Telestar Starstick 2", NULL) },
1484 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309,
1485 &af9015_props, "AVerMedia A309", NULL) },
1486 { DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III,
1487 &af9015_props, "MSI Digi VOX mini III", RC_MAP_MSI_DIGIVOX_III) },
1488 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U,
1489 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1490 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2,
1491 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1492 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3,
1493 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1494 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT,
1495 &af9015_props, "TrekStor DVB-T USB Stick", RC_MAP_TREKSTOR) },
1496 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850,
1497 &af9015_props, "AverMedia AVerTV Volar Black HD (A850)", NULL) },
1498 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805,
1499 &af9015_props, "AverMedia AVerTV Volar GPS 805 (A805)", NULL) },
1500 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU,
1501 &af9015_props, "Conceptronic USB2.0 DVB-T CTVDIGRCU V3.0", NULL) },
1502 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810,
1503 &af9015_props, "KWorld Digital MC-810", NULL) },
1504 { DVB_USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03,
1505 &af9015_props, "Genius TVGo DVB-T03", NULL) },
1506 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2,
1507 &af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) },
1508 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T,
1509 &af9015_props, "KWorld PlusTV DVB-T PCI Pro Card (DVB-T PC160-T)", NULL) },
1510 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20,
1511 &af9015_props, "Sveon STV20 Tuner USB DVB-T HDTV", NULL) },
1512 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2,
1513 &af9015_props, "DigitalNow TinyTwin v2", RC_MAP_DIGITALNOW_TINYTWIN) },
1514 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS,
1515 &af9015_props, "Leadtek WinFast DTV2000DS", RC_MAP_LEADTEK_Y04G0051) },
1516 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T,
1517 &af9015_props, "KWorld USB DVB-T Stick Mobile (UB383-T)", NULL) },
1518 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4,
1519 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1520 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M,
1521 &af9015_props, "AverMedia AVerTV Volar M (A815Mac)", NULL) },
1522 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_RC,
1523 &af9015_props, "TerraTec Cinergy T Stick RC", RC_MAP_TERRATEC_SLIM_2) },
1524 /* XXX: that same ID [0ccd:0099] is used by af9035 driver too */
1525 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC,
1526 &af9015_props, "TerraTec Cinergy T Stick Dual RC", RC_MAP_TERRATEC_SLIM) },
1527 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850T,
1528 &af9015_props, "AverMedia AVerTV Red HD+ (A850T)", NULL) },
1529 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TINYTWIN_3,
1530 &af9015_props, "DigitalNow TinyTwin v3", RC_MAP_DIGITALNOW_TINYTWIN) },
1531 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22,
1532 &af9015_props, "Sveon STV22 Dual USB DVB-T Tuner HDTV", RC_MAP_MSI_DIGIVOX_III) },
1535 MODULE_DEVICE_TABLE(usb, af9015_id_table);
1537 /* usb specific object needed to register this driver with the usb subsystem */
1538 static struct usb_driver af9015_usb_driver = {
1539 .name = KBUILD_MODNAME,
1540 .id_table = af9015_id_table,
1541 .probe = dvb_usbv2_probe,
1542 .disconnect = dvb_usbv2_disconnect,
1543 .suspend = dvb_usbv2_suspend,
1544 .resume = dvb_usbv2_resume,
1545 .reset_resume = dvb_usbv2_reset_resume,
1546 .no_dynamic_id = 1,
1547 .soft_unbind = 1,
1550 module_usb_driver(af9015_usb_driver);
1552 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1553 MODULE_DESCRIPTION("Afatech AF9015 driver");
1554 MODULE_LICENSE("GPL");
1555 MODULE_FIRMWARE(AF9015_FIRMWARE);