Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux...
[firefly-linux-kernel-4.4.55.git] / drivers / media / usb / dvb-usb-v2 / af9035.c
1 /*
2  * Afatech AF9035 DVB USB driver
3  *
4  * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5  * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
6  *
7  *    This program is free software; you can redistribute it and/or modify
8  *    it under the terms of the GNU General Public License as published by
9  *    the Free Software Foundation; either version 2 of the License, or
10  *    (at your option) any later version.
11  *
12  *    This program is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License along
18  *    with this program; if not, write to the Free Software Foundation, Inc.,
19  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "af9035.h"
23
24 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
25
26 static u16 af9035_checksum(const u8 *buf, size_t len)
27 {
28         size_t i;
29         u16 checksum = 0;
30
31         for (i = 1; i < len; i++) {
32                 if (i % 2)
33                         checksum += buf[i] << 8;
34                 else
35                         checksum += buf[i];
36         }
37         checksum = ~checksum;
38
39         return checksum;
40 }
41
42 static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
43 {
44 #define REQ_HDR_LEN 4 /* send header size */
45 #define ACK_HDR_LEN 3 /* rece header size */
46 #define CHECKSUM_LEN 2
47 #define USB_TIMEOUT 2000
48         struct state *state = d_to_priv(d);
49         int ret, wlen, rlen;
50         u16 checksum, tmp_checksum;
51
52         mutex_lock(&d->usb_mutex);
53
54         /* buffer overflow check */
55         if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
56                         req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
57                 dev_err(&d->udev->dev, "%s: too much data wlen=%d rlen=%d\n",
58                                 __func__, req->wlen, req->rlen);
59                 ret = -EINVAL;
60                 goto exit;
61         }
62
63         state->buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
64         state->buf[1] = req->mbox;
65         state->buf[2] = req->cmd;
66         state->buf[3] = state->seq++;
67         memcpy(&state->buf[REQ_HDR_LEN], req->wbuf, req->wlen);
68
69         wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
70         rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
71
72         /* calc and add checksum */
73         checksum = af9035_checksum(state->buf, state->buf[0] - 1);
74         state->buf[state->buf[0] - 1] = (checksum >> 8);
75         state->buf[state->buf[0] - 0] = (checksum & 0xff);
76
77         /* no ack for these packets */
78         if (req->cmd == CMD_FW_DL)
79                 rlen = 0;
80
81         ret = dvb_usbv2_generic_rw_locked(d,
82                         state->buf, wlen, state->buf, rlen);
83         if (ret)
84                 goto exit;
85
86         /* no ack for those packets */
87         if (req->cmd == CMD_FW_DL)
88                 goto exit;
89
90         /* verify checksum */
91         checksum = af9035_checksum(state->buf, rlen - 2);
92         tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
93         if (tmp_checksum != checksum) {
94                 dev_err(&d->udev->dev, "%s: command=%02x checksum mismatch " \
95                                 "(%04x != %04x)\n", KBUILD_MODNAME, req->cmd,
96                                 tmp_checksum, checksum);
97                 ret = -EIO;
98                 goto exit;
99         }
100
101         /* check status */
102         if (state->buf[2]) {
103                 /* fw returns status 1 when IR code was not received */
104                 if (req->cmd == CMD_IR_GET || state->buf[2] == 1) {
105                         ret = 1;
106                         goto exit;
107                 }
108
109                 dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n",
110                                 __func__, req->cmd, state->buf[2]);
111                 ret = -EIO;
112                 goto exit;
113         }
114
115         /* read request, copy returned data to return buf */
116         if (req->rlen)
117                 memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
118 exit:
119         mutex_unlock(&d->usb_mutex);
120         if (ret < 0)
121                 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
122         return ret;
123 }
124
125 /* write multiple registers */
126 static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
127 {
128         u8 wbuf[6 + len];
129         u8 mbox = (reg >> 16) & 0xff;
130         struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };
131
132         wbuf[0] = len;
133         wbuf[1] = 2;
134         wbuf[2] = 0;
135         wbuf[3] = 0;
136         wbuf[4] = (reg >> 8) & 0xff;
137         wbuf[5] = (reg >> 0) & 0xff;
138         memcpy(&wbuf[6], val, len);
139
140         return af9035_ctrl_msg(d, &req);
141 }
142
143 /* read multiple registers */
144 static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
145 {
146         u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
147         u8 mbox = (reg >> 16) & 0xff;
148         struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
149
150         return af9035_ctrl_msg(d, &req);
151 }
152
153 /* write single register */
154 static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
155 {
156         return af9035_wr_regs(d, reg, &val, 1);
157 }
158
159 /* read single register */
160 static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
161 {
162         return af9035_rd_regs(d, reg, val, 1);
163 }
164
165 /* write single register with mask */
166 static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
167                 u8 mask)
168 {
169         int ret;
170         u8 tmp;
171
172         /* no need for read if whole reg is written */
173         if (mask != 0xff) {
174                 ret = af9035_rd_regs(d, reg, &tmp, 1);
175                 if (ret)
176                         return ret;
177
178                 val &= mask;
179                 tmp &= ~mask;
180                 val |= tmp;
181         }
182
183         return af9035_wr_regs(d, reg, &val, 1);
184 }
185
186 static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
187                 struct i2c_msg msg[], int num)
188 {
189         struct dvb_usb_device *d = i2c_get_adapdata(adap);
190         struct state *state = d_to_priv(d);
191         int ret;
192
193         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
194                 return -EAGAIN;
195
196         /*
197          * I2C sub header is 5 bytes long. Meaning of those bytes are:
198          * 0: data len
199          * 1: I2C addr << 1
200          * 2: reg addr len
201          *    byte 3 and 4 can be used as reg addr
202          * 3: reg addr MSB
203          *    used when reg addr len is set to 2
204          * 4: reg addr LSB
205          *    used when reg addr len is set to 1 or 2
206          *
207          * For the simplify we do not use register addr at all.
208          * NOTE: As a firmware knows tuner type there is very small possibility
209          * there could be some tuner I2C hacks done by firmware and this may
210          * lead problems if firmware expects those bytes are used.
211          */
212         if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
213                         (msg[1].flags & I2C_M_RD)) {
214                 if (msg[0].len > 40 || msg[1].len > 40) {
215                         /* TODO: correct limits > 40 */
216                         ret = -EOPNOTSUPP;
217                 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
218                            (msg[0].addr == state->af9033_config[1].i2c_addr)) {
219                         /* demod access via firmware interface */
220                         u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
221                                         msg[0].buf[2];
222
223                         if (msg[0].addr == state->af9033_config[1].i2c_addr)
224                                 reg |= 0x100000;
225
226                         ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
227                                         msg[1].len);
228                 } else {
229                         /* I2C */
230                         u8 buf[5 + msg[0].len];
231                         struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
232                                         buf, msg[1].len, msg[1].buf };
233                         req.mbox |= ((msg[0].addr & 0x80)  >>  3);
234                         buf[0] = msg[1].len;
235                         buf[1] = msg[0].addr << 1;
236                         buf[2] = 0x00; /* reg addr len */
237                         buf[3] = 0x00; /* reg addr MSB */
238                         buf[4] = 0x00; /* reg addr LSB */
239                         memcpy(&buf[5], msg[0].buf, msg[0].len);
240                         ret = af9035_ctrl_msg(d, &req);
241                 }
242         } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
243                 if (msg[0].len > 40) {
244                         /* TODO: correct limits > 40 */
245                         ret = -EOPNOTSUPP;
246                 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
247                            (msg[0].addr == state->af9033_config[1].i2c_addr)) {
248                         /* demod access via firmware interface */
249                         u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
250                                         msg[0].buf[2];
251
252                         if (msg[0].addr == state->af9033_config[1].i2c_addr)
253                                 reg |= 0x100000;
254
255                         ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
256                                         msg[0].len - 3);
257                 } else {
258                         /* I2C */
259                         u8 buf[5 + msg[0].len];
260                         struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
261                                         0, NULL };
262                         req.mbox |= ((msg[0].addr & 0x80)  >>  3);
263                         buf[0] = msg[0].len;
264                         buf[1] = msg[0].addr << 1;
265                         buf[2] = 0x00; /* reg addr len */
266                         buf[3] = 0x00; /* reg addr MSB */
267                         buf[4] = 0x00; /* reg addr LSB */
268                         memcpy(&buf[5], msg[0].buf, msg[0].len);
269                         ret = af9035_ctrl_msg(d, &req);
270                 }
271         } else {
272                 /*
273                  * We support only two kind of I2C transactions:
274                  * 1) 1 x read + 1 x write
275                  * 2) 1 x write
276                  */
277                 ret = -EOPNOTSUPP;
278         }
279
280         mutex_unlock(&d->i2c_mutex);
281
282         if (ret < 0)
283                 return ret;
284         else
285                 return num;
286 }
287
288 static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
289 {
290         return I2C_FUNC_I2C;
291 }
292
293 static struct i2c_algorithm af9035_i2c_algo = {
294         .master_xfer = af9035_i2c_master_xfer,
295         .functionality = af9035_i2c_functionality,
296 };
297
298 static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
299 {
300         struct state *state = d_to_priv(d);
301         int ret;
302         u8 wbuf[1] = { 1 };
303         u8 rbuf[4];
304         struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
305                         sizeof(rbuf), rbuf };
306
307         ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
308         if (ret < 0)
309                 goto err;
310
311         state->chip_version = rbuf[0];
312         state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
313
314         ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
315         if (ret < 0)
316                 goto err;
317
318         dev_info(&d->udev->dev,
319                         "%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n",
320                         __func__, state->prechip_version, state->chip_version,
321                         state->chip_type);
322
323         if (state->chip_type == 0x9135) {
324                 if (state->chip_version == 0x02)
325                         *name = AF9035_FIRMWARE_IT9135_V2;
326                 else
327                         *name = AF9035_FIRMWARE_IT9135_V1;
328                 state->eeprom_addr = EEPROM_BASE_IT9135;
329         } else {
330                 *name = AF9035_FIRMWARE_AF9035;
331                 state->eeprom_addr = EEPROM_BASE_AF9035;
332         }
333
334         ret = af9035_ctrl_msg(d, &req);
335         if (ret < 0)
336                 goto err;
337
338         dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
339         if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
340                 ret = WARM;
341         else
342                 ret = COLD;
343
344         return ret;
345
346 err:
347         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
348
349         return ret;
350 }
351
352 static int af9035_download_firmware_old(struct dvb_usb_device *d,
353                 const struct firmware *fw)
354 {
355         int ret, i, j, len;
356         u8 wbuf[1];
357         struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
358         struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
359         u8 hdr_core;
360         u16 hdr_addr, hdr_data_len, hdr_checksum;
361         #define MAX_DATA 58
362         #define HDR_SIZE 7
363
364         /*
365          * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
366          *
367          * byte 0: MCS 51 core
368          *  There are two inside the AF9035 (1=Link and 2=OFDM) with separate
369          *  address spaces
370          * byte 1-2: Big endian destination address
371          * byte 3-4: Big endian number of data bytes following the header
372          * byte 5-6: Big endian header checksum, apparently ignored by the chip
373          *  Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
374          */
375
376         for (i = fw->size; i > HDR_SIZE;) {
377                 hdr_core = fw->data[fw->size - i + 0];
378                 hdr_addr = fw->data[fw->size - i + 1] << 8;
379                 hdr_addr |= fw->data[fw->size - i + 2] << 0;
380                 hdr_data_len = fw->data[fw->size - i + 3] << 8;
381                 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
382                 hdr_checksum = fw->data[fw->size - i + 5] << 8;
383                 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
384
385                 dev_dbg(&d->udev->dev, "%s: core=%d addr=%04x data_len=%d " \
386                                 "checksum=%04x\n", __func__, hdr_core, hdr_addr,
387                                 hdr_data_len, hdr_checksum);
388
389                 if (((hdr_core != 1) && (hdr_core != 2)) ||
390                                 (hdr_data_len > i)) {
391                         dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
392                         break;
393                 }
394
395                 /* download begin packet */
396                 req.cmd = CMD_FW_DL_BEGIN;
397                 ret = af9035_ctrl_msg(d, &req);
398                 if (ret < 0)
399                         goto err;
400
401                 /* download firmware packet(s) */
402                 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
403                         len = j;
404                         if (len > MAX_DATA)
405                                 len = MAX_DATA;
406                         req_fw_dl.wlen = len;
407                         req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
408                                         HDR_SIZE + hdr_data_len - j];
409                         ret = af9035_ctrl_msg(d, &req_fw_dl);
410                         if (ret < 0)
411                                 goto err;
412                 }
413
414                 /* download end packet */
415                 req.cmd = CMD_FW_DL_END;
416                 ret = af9035_ctrl_msg(d, &req);
417                 if (ret < 0)
418                         goto err;
419
420                 i -= hdr_data_len + HDR_SIZE;
421
422                 dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
423                                 __func__, fw->size - i);
424         }
425
426         /* print warn if firmware is bad, continue and see what happens */
427         if (i)
428                 dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME);
429
430         return 0;
431
432 err:
433         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
434
435         return ret;
436 }
437
438 static int af9035_download_firmware_new(struct dvb_usb_device *d,
439                 const struct firmware *fw)
440 {
441         int ret, i, i_prev;
442         struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
443         #define HDR_SIZE 7
444
445         /*
446          * There seems to be following firmware header. Meaning of bytes 0-3
447          * is unknown.
448          *
449          * 0: 3
450          * 1: 0, 1
451          * 2: 0
452          * 3: 1, 2, 3
453          * 4: addr MSB
454          * 5: addr LSB
455          * 6: count of data bytes ?
456          */
457         for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
458                 if (i == fw->size ||
459                                 (fw->data[i + 0] == 0x03 &&
460                                 (fw->data[i + 1] == 0x00 ||
461                                 fw->data[i + 1] == 0x01) &&
462                                 fw->data[i + 2] == 0x00)) {
463                         req_fw_dl.wlen = i - i_prev;
464                         req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
465                         i_prev = i;
466                         ret = af9035_ctrl_msg(d, &req_fw_dl);
467                         if (ret < 0)
468                                 goto err;
469
470                         dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
471                                         __func__, i);
472                 }
473         }
474
475         return 0;
476
477 err:
478         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
479
480         return ret;
481 }
482
483 static int af9035_download_firmware(struct dvb_usb_device *d,
484                 const struct firmware *fw)
485 {
486         struct state *state = d_to_priv(d);
487         int ret;
488         u8 wbuf[1];
489         u8 rbuf[4];
490         u8 tmp;
491         struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
492         struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
493         dev_dbg(&d->udev->dev, "%s:\n", __func__);
494
495         /*
496          * In case of dual tuner configuration we need to do some extra
497          * initialization in order to download firmware to slave demod too,
498          * which is done by master demod.
499          * Master feeds also clock and controls power via GPIO.
500          */
501         ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp);
502         if (ret < 0)
503                 goto err;
504
505         if (tmp) {
506                 /* configure gpioh1, reset & power slave demod */
507                 ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
508                 if (ret < 0)
509                         goto err;
510
511                 ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
512                 if (ret < 0)
513                         goto err;
514
515                 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
516                 if (ret < 0)
517                         goto err;
518
519                 usleep_range(10000, 50000);
520
521                 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
522                 if (ret < 0)
523                         goto err;
524
525                 /* tell the slave I2C address */
526                 ret = af9035_rd_reg(d,
527                                 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
528                                 &tmp);
529                 if (ret < 0)
530                         goto err;
531
532                 if (state->chip_type == 0x9135) {
533                         ret = af9035_wr_reg(d, 0x004bfb, tmp);
534                         if (ret < 0)
535                                 goto err;
536                 } else {
537                         ret = af9035_wr_reg(d, 0x00417f, tmp);
538                         if (ret < 0)
539                                 goto err;
540
541                         /* enable clock out */
542                         ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
543                         if (ret < 0)
544                                 goto err;
545                 }
546         }
547
548         if (fw->data[0] == 0x01)
549                 ret = af9035_download_firmware_old(d, fw);
550         else
551                 ret = af9035_download_firmware_new(d, fw);
552         if (ret < 0)
553                 goto err;
554
555         /* firmware loaded, request boot */
556         req.cmd = CMD_FW_BOOT;
557         ret = af9035_ctrl_msg(d, &req);
558         if (ret < 0)
559                 goto err;
560
561         /* ensure firmware starts */
562         wbuf[0] = 1;
563         ret = af9035_ctrl_msg(d, &req_fw_ver);
564         if (ret < 0)
565                 goto err;
566
567         if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
568                 dev_err(&d->udev->dev, "%s: firmware did not run\n",
569                                 KBUILD_MODNAME);
570                 ret = -ENODEV;
571                 goto err;
572         }
573
574         dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
575                         KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
576
577         return 0;
578
579 err:
580         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
581
582         return ret;
583 }
584
585 static int af9035_read_config(struct dvb_usb_device *d)
586 {
587         struct state *state = d_to_priv(d);
588         int ret, i;
589         u8 tmp;
590         u16 tmp16, addr;
591
592         /* demod I2C "address" */
593         state->af9033_config[0].i2c_addr = 0x38;
594         state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
595         state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
596         state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
597         state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
598
599         /* eeprom memory mapped location */
600         if (state->chip_type == 0x9135) {
601                 if (state->chip_version == 0x02) {
602                         state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
603                         state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
604                         tmp16 = 0x00461d;
605                 } else {
606                         state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
607                         state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
608                         tmp16 = 0x00461b;
609                 }
610
611                 /* check if eeprom exists */
612                 ret = af9035_rd_reg(d, tmp16, &tmp);
613                 if (ret < 0)
614                         goto err;
615
616                 if (tmp == 0x00) {
617                         dev_dbg(&d->udev->dev, "%s: no eeprom\n", __func__);
618                         goto skip_eeprom;
619                 }
620         }
621
622         /* check if there is dual tuners */
623         ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp);
624         if (ret < 0)
625                 goto err;
626
627         state->dual_mode = tmp;
628         dev_dbg(&d->udev->dev, "%s: dual mode=%d\n", __func__,
629                         state->dual_mode);
630
631         if (state->dual_mode) {
632                 /* read 2nd demodulator I2C address */
633                 ret = af9035_rd_reg(d,
634                                 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
635                                 &tmp);
636                 if (ret < 0)
637                         goto err;
638
639                 state->af9033_config[1].i2c_addr = tmp;
640                 dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
641                                 __func__, tmp);
642         }
643
644         addr = state->eeprom_addr;
645
646         for (i = 0; i < state->dual_mode + 1; i++) {
647                 /* tuner */
648                 ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
649                 if (ret < 0)
650                         goto err;
651
652                 if (tmp == 0x00)
653                         dev_dbg(&d->udev->dev,
654                                         "%s: [%d]tuner not set, using default\n",
655                                         __func__, i);
656                 else
657                         state->af9033_config[i].tuner = tmp;
658
659                 dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
660                                 __func__, i, state->af9033_config[i].tuner);
661
662                 switch (state->af9033_config[i].tuner) {
663                 case AF9033_TUNER_TUA9001:
664                 case AF9033_TUNER_FC0011:
665                 case AF9033_TUNER_MXL5007T:
666                 case AF9033_TUNER_TDA18218:
667                 case AF9033_TUNER_FC2580:
668                 case AF9033_TUNER_FC0012:
669                         state->af9033_config[i].spec_inv = 1;
670                         break;
671                 case AF9033_TUNER_IT9135_38:
672                 case AF9033_TUNER_IT9135_51:
673                 case AF9033_TUNER_IT9135_52:
674                 case AF9033_TUNER_IT9135_60:
675                 case AF9033_TUNER_IT9135_61:
676                 case AF9033_TUNER_IT9135_62:
677                         break;
678                 default:
679                         dev_warn(&d->udev->dev,
680                                         "%s: tuner id=%02x not supported, please report!",
681                                         KBUILD_MODNAME, tmp);
682                 }
683
684                 /* disable dual mode if driver does not support it */
685                 if (i == 1)
686                         switch (state->af9033_config[i].tuner) {
687                         case AF9033_TUNER_FC0012:
688                         case AF9033_TUNER_IT9135_38:
689                         case AF9033_TUNER_IT9135_51:
690                         case AF9033_TUNER_IT9135_52:
691                         case AF9033_TUNER_IT9135_60:
692                         case AF9033_TUNER_IT9135_61:
693                         case AF9033_TUNER_IT9135_62:
694                         case AF9033_TUNER_MXL5007T:
695                                 break;
696                         default:
697                                 state->dual_mode = false;
698                                 dev_info(&d->udev->dev,
699                                                 "%s: driver does not support 2nd tuner and will disable it",
700                                                 KBUILD_MODNAME);
701                 }
702
703                 /* tuner IF frequency */
704                 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
705                 if (ret < 0)
706                         goto err;
707
708                 tmp16 = tmp;
709
710                 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
711                 if (ret < 0)
712                         goto err;
713
714                 tmp16 |= tmp << 8;
715
716                 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
717
718                 addr += 0x10; /* shift for the 2nd tuner params */
719         }
720
721 skip_eeprom:
722         /* get demod clock */
723         ret = af9035_rd_reg(d, 0x00d800, &tmp);
724         if (ret < 0)
725                 goto err;
726
727         tmp = (tmp >> 0) & 0x0f;
728
729         for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
730                 if (state->chip_type == 0x9135)
731                         state->af9033_config[i].clock = clock_lut_it9135[tmp];
732                 else
733                         state->af9033_config[i].clock = clock_lut_af9035[tmp];
734         }
735
736         return 0;
737
738 err:
739         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
740
741         return ret;
742 }
743
744 static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
745                 int cmd, int arg)
746 {
747         int ret;
748         u8 val;
749
750         dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
751
752         /*
753          * CEN     always enabled by hardware wiring
754          * RESETN  GPIOT3
755          * RXEN    GPIOT2
756          */
757
758         switch (cmd) {
759         case TUA9001_CMD_RESETN:
760                 if (arg)
761                         val = 0x00;
762                 else
763                         val = 0x01;
764
765                 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
766                 if (ret < 0)
767                         goto err;
768                 break;
769         case TUA9001_CMD_RXEN:
770                 if (arg)
771                         val = 0x01;
772                 else
773                         val = 0x00;
774
775                 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
776                 if (ret < 0)
777                         goto err;
778                 break;
779         }
780
781         return 0;
782
783 err:
784         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
785
786         return ret;
787 }
788
789
790 static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
791                 int cmd, int arg)
792 {
793         int ret;
794
795         switch (cmd) {
796         case FC0011_FE_CALLBACK_POWER:
797                 /* Tuner enable */
798                 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
799                 if (ret < 0)
800                         goto err;
801
802                 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
803                 if (ret < 0)
804                         goto err;
805
806                 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
807                 if (ret < 0)
808                         goto err;
809
810                 /* LED */
811                 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
812                 if (ret < 0)
813                         goto err;
814
815                 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
816                 if (ret < 0)
817                         goto err;
818
819                 usleep_range(10000, 50000);
820                 break;
821         case FC0011_FE_CALLBACK_RESET:
822                 ret = af9035_wr_reg(d, 0xd8e9, 1);
823                 if (ret < 0)
824                         goto err;
825
826                 ret = af9035_wr_reg(d, 0xd8e8, 1);
827                 if (ret < 0)
828                         goto err;
829
830                 ret = af9035_wr_reg(d, 0xd8e7, 1);
831                 if (ret < 0)
832                         goto err;
833
834                 usleep_range(10000, 20000);
835
836                 ret = af9035_wr_reg(d, 0xd8e7, 0);
837                 if (ret < 0)
838                         goto err;
839
840                 usleep_range(10000, 20000);
841                 break;
842         default:
843                 ret = -EINVAL;
844                 goto err;
845         }
846
847         return 0;
848
849 err:
850         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
851
852         return ret;
853 }
854
855 static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
856 {
857         struct state *state = d_to_priv(d);
858
859         switch (state->af9033_config[0].tuner) {
860         case AF9033_TUNER_FC0011:
861                 return af9035_fc0011_tuner_callback(d, cmd, arg);
862         case AF9033_TUNER_TUA9001:
863                 return af9035_tua9001_tuner_callback(d, cmd, arg);
864         default:
865                 break;
866         }
867
868         return 0;
869 }
870
871 static int af9035_frontend_callback(void *adapter_priv, int component,
872                                     int cmd, int arg)
873 {
874         struct i2c_adapter *adap = adapter_priv;
875         struct dvb_usb_device *d = i2c_get_adapdata(adap);
876
877         dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
878                         __func__, component, cmd, arg);
879
880         switch (component) {
881         case DVB_FRONTEND_COMPONENT_TUNER:
882                 return af9035_tuner_callback(d, cmd, arg);
883         default:
884                 break;
885         }
886
887         return 0;
888 }
889
890 static int af9035_get_adapter_count(struct dvb_usb_device *d)
891 {
892         struct state *state = d_to_priv(d);
893
894         /* disable 2nd adapter as we don't have PID filters implemented */
895         if (d->udev->speed == USB_SPEED_FULL)
896                 return 1;
897         else
898                 return state->dual_mode + 1;
899 }
900
901 static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
902 {
903         struct state *state = adap_to_priv(adap);
904         struct dvb_usb_device *d = adap_to_d(adap);
905         int ret;
906         dev_dbg(&d->udev->dev, "%s:\n", __func__);
907
908         if (!state->af9033_config[adap->id].tuner) {
909                 /* unsupported tuner */
910                 ret = -ENODEV;
911                 goto err;
912         }
913
914         /* attach demodulator */
915         adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id],
916                         &d->i2c_adap);
917         if (adap->fe[0] == NULL) {
918                 ret = -ENODEV;
919                 goto err;
920         }
921
922         /* disable I2C-gate */
923         adap->fe[0]->ops.i2c_gate_ctrl = NULL;
924         adap->fe[0]->callback = af9035_frontend_callback;
925
926         return 0;
927
928 err:
929         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
930
931         return ret;
932 }
933
934 static struct tua9001_config af9035_tua9001_config = {
935         .i2c_addr = 0x60,
936 };
937
938 static const struct fc0011_config af9035_fc0011_config = {
939         .i2c_address = 0x60,
940 };
941
942 static struct mxl5007t_config af9035_mxl5007t_config[] = {
943         {
944                 .xtal_freq_hz = MxL_XTAL_24_MHZ,
945                 .if_freq_hz = MxL_IF_4_57_MHZ,
946                 .invert_if = 0,
947                 .loop_thru_enable = 0,
948                 .clk_out_enable = 0,
949                 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
950         }, {
951                 .xtal_freq_hz = MxL_XTAL_24_MHZ,
952                 .if_freq_hz = MxL_IF_4_57_MHZ,
953                 .invert_if = 0,
954                 .loop_thru_enable = 1,
955                 .clk_out_enable = 1,
956                 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
957         }
958 };
959
960 static struct tda18218_config af9035_tda18218_config = {
961         .i2c_address = 0x60,
962         .i2c_wr_max = 21,
963 };
964
965 static const struct fc2580_config af9035_fc2580_config = {
966         .i2c_addr = 0x56,
967         .clock = 16384000,
968 };
969
970 static const struct fc0012_config af9035_fc0012_config[] = {
971         {
972                 .i2c_address = 0x63,
973                 .xtal_freq = FC_XTAL_36_MHZ,
974                 .dual_master = true,
975                 .loop_through = true,
976                 .clock_out = true,
977         }, {
978                 .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
979                 .xtal_freq = FC_XTAL_36_MHZ,
980                 .dual_master = true,
981         }
982 };
983
984 static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
985 {
986         struct state *state = adap_to_priv(adap);
987         struct dvb_usb_device *d = adap_to_d(adap);
988         int ret;
989         struct dvb_frontend *fe;
990         struct i2c_msg msg[1];
991         u8 tuner_addr;
992         dev_dbg(&d->udev->dev, "%s:\n", __func__);
993
994         /*
995          * XXX: Hack used in that function: we abuse unused I2C address bit [7]
996          * to carry info about used I2C bus for dual tuner configuration.
997          */
998
999         switch (state->af9033_config[adap->id].tuner) {
1000         case AF9033_TUNER_TUA9001:
1001                 /* AF9035 gpiot3 = TUA9001 RESETN
1002                    AF9035 gpiot2 = TUA9001 RXEN */
1003
1004                 /* configure gpiot2 and gpiot2 as output */
1005                 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
1006                 if (ret < 0)
1007                         goto err;
1008
1009                 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
1010                 if (ret < 0)
1011                         goto err;
1012
1013                 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
1014                 if (ret < 0)
1015                         goto err;
1016
1017                 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
1018                 if (ret < 0)
1019                         goto err;
1020
1021                 /* attach tuner */
1022                 fe = dvb_attach(tua9001_attach, adap->fe[0],
1023                                 &d->i2c_adap, &af9035_tua9001_config);
1024                 break;
1025         case AF9033_TUNER_FC0011:
1026                 fe = dvb_attach(fc0011_attach, adap->fe[0],
1027                                 &d->i2c_adap, &af9035_fc0011_config);
1028                 break;
1029         case AF9033_TUNER_MXL5007T:
1030                 if (adap->id == 0) {
1031                         ret = af9035_wr_reg(d, 0x00d8e0, 1);
1032                         if (ret < 0)
1033                                 goto err;
1034
1035                         ret = af9035_wr_reg(d, 0x00d8e1, 1);
1036                         if (ret < 0)
1037                                 goto err;
1038
1039                         ret = af9035_wr_reg(d, 0x00d8df, 0);
1040                         if (ret < 0)
1041                                 goto err;
1042
1043                         msleep(30);
1044
1045                         ret = af9035_wr_reg(d, 0x00d8df, 1);
1046                         if (ret < 0)
1047                                 goto err;
1048
1049                         msleep(300);
1050
1051                         ret = af9035_wr_reg(d, 0x00d8c0, 1);
1052                         if (ret < 0)
1053                                 goto err;
1054
1055                         ret = af9035_wr_reg(d, 0x00d8c1, 1);
1056                         if (ret < 0)
1057                                 goto err;
1058
1059                         ret = af9035_wr_reg(d, 0x00d8bf, 0);
1060                         if (ret < 0)
1061                                 goto err;
1062
1063                         ret = af9035_wr_reg(d, 0x00d8b4, 1);
1064                         if (ret < 0)
1065                                 goto err;
1066
1067                         ret = af9035_wr_reg(d, 0x00d8b5, 1);
1068                         if (ret < 0)
1069                                 goto err;
1070
1071                         ret = af9035_wr_reg(d, 0x00d8b3, 1);
1072                         if (ret < 0)
1073                                 goto err;
1074
1075                         tuner_addr = 0x60;
1076                 } else {
1077                         tuner_addr = 0x60 | 0x80; /* I2C bus hack */
1078                 }
1079
1080                 /* attach tuner */
1081                 fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1082                                 tuner_addr, &af9035_mxl5007t_config[adap->id]);
1083                 break;
1084         case AF9033_TUNER_TDA18218:
1085                 /* attach tuner */
1086                 fe = dvb_attach(tda18218_attach, adap->fe[0],
1087                                 &d->i2c_adap, &af9035_tda18218_config);
1088                 break;
1089         case AF9033_TUNER_FC2580:
1090                 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on  */
1091                 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1092                 if (ret < 0)
1093                         goto err;
1094
1095                 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1096                 if (ret < 0)
1097                         goto err;
1098
1099                 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1100                 if (ret < 0)
1101                         goto err;
1102
1103                 usleep_range(10000, 50000);
1104                 /* attach tuner */
1105                 fe = dvb_attach(fc2580_attach, adap->fe[0],
1106                                 &d->i2c_adap, &af9035_fc2580_config);
1107                 break;
1108         case AF9033_TUNER_FC0012:
1109                 /*
1110                  * AF9035 gpiot2 = FC0012 enable
1111                  * XXX: there seems to be something on gpioh8 too, but on my
1112                  * my test I didn't find any difference.
1113                  */
1114
1115                 if (adap->id == 0) {
1116                         /* configure gpiot2 as output and high */
1117                         ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1118                         if (ret < 0)
1119                                 goto err;
1120
1121                         ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1122                         if (ret < 0)
1123                                 goto err;
1124
1125                         ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1126                         if (ret < 0)
1127                                 goto err;
1128                 } else {
1129                         /*
1130                          * FIXME: That belongs for the FC0012 driver.
1131                          * Write 02 to FC0012 master tuner register 0d directly
1132                          * in order to make slave tuner working.
1133                          */
1134                         msg[0].addr = 0x63;
1135                         msg[0].flags = 0;
1136                         msg[0].len = 2;
1137                         msg[0].buf = "\x0d\x02";
1138                         ret = i2c_transfer(&d->i2c_adap, msg, 1);
1139                         if (ret < 0)
1140                                 goto err;
1141                 }
1142
1143                 usleep_range(10000, 50000);
1144
1145                 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
1146                                 &af9035_fc0012_config[adap->id]);
1147                 break;
1148         case AF9033_TUNER_IT9135_38:
1149         case AF9033_TUNER_IT9135_51:
1150         case AF9033_TUNER_IT9135_52:
1151         case AF9033_TUNER_IT9135_60:
1152         case AF9033_TUNER_IT9135_61:
1153         case AF9033_TUNER_IT9135_62:
1154                 /* attach tuner */
1155                 fe = dvb_attach(it913x_attach, adap->fe[0], &d->i2c_adap,
1156                                 state->af9033_config[adap->id].i2c_addr,
1157                                 state->af9033_config[0].tuner);
1158                 break;
1159         default:
1160                 fe = NULL;
1161         }
1162
1163         if (fe == NULL) {
1164                 ret = -ENODEV;
1165                 goto err;
1166         }
1167
1168         return 0;
1169
1170 err:
1171         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1172
1173         return ret;
1174 }
1175
1176 static int af9035_init(struct dvb_usb_device *d)
1177 {
1178         struct state *state = d_to_priv(d);
1179         int ret, i;
1180         u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1181         u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1182         struct reg_val_mask tab[] = {
1183                 { 0x80f99d, 0x01, 0x01 },
1184                 { 0x80f9a4, 0x01, 0x01 },
1185                 { 0x00dd11, 0x00, 0x20 },
1186                 { 0x00dd11, 0x00, 0x40 },
1187                 { 0x00dd13, 0x00, 0x20 },
1188                 { 0x00dd13, 0x00, 0x40 },
1189                 { 0x00dd11, 0x20, 0x20 },
1190                 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1191                 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1192                 { 0x00dd0c, packet_size, 0xff},
1193                 { 0x00dd11, state->dual_mode << 6, 0x40 },
1194                 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1195                 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1196                 { 0x00dd0d, packet_size, 0xff },
1197                 { 0x80f9a3, state->dual_mode, 0x01 },
1198                 { 0x80f9cd, state->dual_mode, 0x01 },
1199                 { 0x80f99d, 0x00, 0x01 },
1200                 { 0x80f9a4, 0x00, 0x01 },
1201         };
1202
1203         dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \
1204                         "packet_size=%02x\n", __func__,
1205                         d->udev->speed, frame_size, packet_size);
1206
1207         /* init endpoints */
1208         for (i = 0; i < ARRAY_SIZE(tab); i++) {
1209                 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1210                                 tab[i].mask);
1211                 if (ret < 0)
1212                         goto err;
1213         }
1214
1215         return 0;
1216
1217 err:
1218         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1219
1220         return ret;
1221 }
1222
1223 #if IS_ENABLED(CONFIG_RC_CORE)
1224 static int af9035_rc_query(struct dvb_usb_device *d)
1225 {
1226         int ret;
1227         u32 key;
1228         u8 buf[4];
1229         struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
1230
1231         ret = af9035_ctrl_msg(d, &req);
1232         if (ret == 1)
1233                 return 0;
1234         else if (ret < 0)
1235                 goto err;
1236
1237         if ((buf[2] + buf[3]) == 0xff) {
1238                 if ((buf[0] + buf[1]) == 0xff) {
1239                         /* NEC standard 16bit */
1240                         key = buf[0] << 8 | buf[2];
1241                 } else {
1242                         /* NEC extended 24bit */
1243                         key = buf[0] << 16 | buf[1] << 8 | buf[2];
1244                 }
1245         } else {
1246                 /* NEC full code 32bit */
1247                 key = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1248         }
1249
1250         dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 4, buf);
1251
1252         rc_keydown(d->rc_dev, key, 0);
1253
1254         return 0;
1255
1256 err:
1257         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1258
1259         return ret;
1260 }
1261
1262 static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1263 {
1264         struct state *state = d_to_priv(d);
1265         int ret;
1266         u8 tmp;
1267
1268         ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_MODE, &tmp);
1269         if (ret < 0)
1270                 goto err;
1271
1272         dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
1273
1274         /* don't activate rc if in HID mode or if not available */
1275         if (tmp == 5) {
1276                 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_TYPE,
1277                                 &tmp);
1278                 if (ret < 0)
1279                         goto err;
1280
1281                 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
1282
1283                 switch (tmp) {
1284                 case 0: /* NEC */
1285                 default:
1286                         rc->allowed_protos = RC_BIT_NEC;
1287                         break;
1288                 case 1: /* RC6 */
1289                         rc->allowed_protos = RC_BIT_RC6_MCE;
1290                         break;
1291                 }
1292
1293                 rc->query = af9035_rc_query;
1294                 rc->interval = 500;
1295
1296                 /* load empty to enable rc */
1297                 if (!rc->map_name)
1298                         rc->map_name = RC_MAP_EMPTY;
1299         }
1300
1301         return 0;
1302
1303 err:
1304         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1305
1306         return ret;
1307 }
1308 #else
1309         #define af9035_get_rc_config NULL
1310 #endif
1311
1312 static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1313                 struct usb_data_stream_properties *stream)
1314 {
1315         struct dvb_usb_device *d = fe_to_d(fe);
1316         dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
1317
1318         if (d->udev->speed == USB_SPEED_FULL)
1319                 stream->u.bulk.buffersize = 5 * 188;
1320
1321         return 0;
1322 }
1323
1324 /*
1325  * FIXME: PID filter is property of demodulator and should be moved to the
1326  * correct driver. Also we support only adapter #0 PID filter and will
1327  * disable adapter #1 if USB1.1 is used.
1328  */
1329 static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1330 {
1331         struct dvb_usb_device *d = adap_to_d(adap);
1332         int ret;
1333
1334         dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1335
1336         ret = af9035_wr_reg_mask(d, 0x80f993, onoff, 0x01);
1337         if (ret < 0)
1338                 goto err;
1339
1340         return 0;
1341
1342 err:
1343         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1344
1345         return ret;
1346 }
1347
1348 static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1349                 int onoff)
1350 {
1351         struct dvb_usb_device *d = adap_to_d(adap);
1352         int ret;
1353         u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
1354
1355         dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
1356                         __func__, index, pid, onoff);
1357
1358         ret = af9035_wr_regs(d, 0x80f996, wbuf, 2);
1359         if (ret < 0)
1360                 goto err;
1361
1362         ret = af9035_wr_reg(d, 0x80f994, onoff);
1363         if (ret < 0)
1364                 goto err;
1365
1366         ret = af9035_wr_reg(d, 0x80f995, index);
1367         if (ret < 0)
1368                 goto err;
1369
1370         return 0;
1371
1372 err:
1373         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1374
1375         return ret;
1376 }
1377
1378 static int af9035_probe(struct usb_interface *intf,
1379                 const struct usb_device_id *id)
1380 {
1381         struct usb_device *udev = interface_to_usbdev(intf);
1382         char manufacturer[sizeof("Afatech")];
1383
1384         memset(manufacturer, 0, sizeof(manufacturer));
1385         usb_string(udev, udev->descriptor.iManufacturer,
1386                         manufacturer, sizeof(manufacturer));
1387         /*
1388          * There is two devices having same ID but different chipset. One uses
1389          * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1390          * is iManufacturer string.
1391          *
1392          * idVendor           0x0ccd TerraTec Electronic GmbH
1393          * idProduct          0x0099
1394          * bcdDevice            2.00
1395          * iManufacturer           1 Afatech
1396          * iProduct                2 DVB-T 2
1397          *
1398          * idVendor           0x0ccd TerraTec Electronic GmbH
1399          * idProduct          0x0099
1400          * bcdDevice            2.00
1401          * iManufacturer           1 ITE Technologies, Inc.
1402          * iProduct                2 DVB-T TV Stick
1403          */
1404         if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1405                         (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1406                 if (!strcmp("Afatech", manufacturer)) {
1407                         dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
1408                         return -ENODEV;
1409                 }
1410         }
1411
1412         return dvb_usbv2_probe(intf, id);
1413 }
1414
1415 /* interface 0 is used by DVB-T receiver and
1416    interface 1 is for remote controller (HID) */
1417 static const struct dvb_usb_device_properties af9035_props = {
1418         .driver_name = KBUILD_MODNAME,
1419         .owner = THIS_MODULE,
1420         .adapter_nr = adapter_nr,
1421         .size_of_priv = sizeof(struct state),
1422
1423         .generic_bulk_ctrl_endpoint = 0x02,
1424         .generic_bulk_ctrl_endpoint_response = 0x81,
1425
1426         .identify_state = af9035_identify_state,
1427         .download_firmware = af9035_download_firmware,
1428
1429         .i2c_algo = &af9035_i2c_algo,
1430         .read_config = af9035_read_config,
1431         .frontend_attach = af9035_frontend_attach,
1432         .tuner_attach = af9035_tuner_attach,
1433         .init = af9035_init,
1434         .get_rc_config = af9035_get_rc_config,
1435         .get_stream_config = af9035_get_stream_config,
1436
1437         .get_adapter_count = af9035_get_adapter_count,
1438         .adapter = {
1439                 {
1440                         .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1441                                 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1442
1443                         .pid_filter_count = 32,
1444                         .pid_filter_ctrl = af9035_pid_filter_ctrl,
1445                         .pid_filter = af9035_pid_filter,
1446
1447                         .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1448                 }, {
1449                         .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1450                 },
1451         },
1452 };
1453
1454 static const struct usb_device_id af9035_id_table[] = {
1455         /* AF9035 devices */
1456         { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1457                 &af9035_props, "Afatech AF9035 reference design", NULL) },
1458         { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1459                 &af9035_props, "Afatech AF9035 reference design", NULL) },
1460         { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1461                 &af9035_props, "Afatech AF9035 reference design", NULL) },
1462         { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1463                 &af9035_props, "Afatech AF9035 reference design", NULL) },
1464         { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1465                 &af9035_props, "Afatech AF9035 reference design", NULL) },
1466         { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1467                 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1468         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1469                 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1470         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1471                 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1472         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1473                 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1474         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1475                 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1476         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1477                 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
1478         { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1479                 &af9035_props, "Asus U3100Mini Plus", NULL) },
1480         { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
1481                 &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
1482         /* IT9135 devices */
1483 #if 0
1484         { DVB_USB_DEVICE(0x048d, 0x9135,
1485                 &af9035_props, "IT9135 reference design", NULL) },
1486         { DVB_USB_DEVICE(0x048d, 0x9006,
1487                 &af9035_props, "IT9135 reference design", NULL) },
1488 #endif
1489         /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
1490         { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
1491                 &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)", NULL) },
1492         { }
1493 };
1494 MODULE_DEVICE_TABLE(usb, af9035_id_table);
1495
1496 static struct usb_driver af9035_usb_driver = {
1497         .name = KBUILD_MODNAME,
1498         .id_table = af9035_id_table,
1499         .probe = af9035_probe,
1500         .disconnect = dvb_usbv2_disconnect,
1501         .suspend = dvb_usbv2_suspend,
1502         .resume = dvb_usbv2_resume,
1503         .reset_resume = dvb_usbv2_reset_resume,
1504         .no_dynamic_id = 1,
1505         .soft_unbind = 1,
1506 };
1507
1508 module_usb_driver(af9035_usb_driver);
1509
1510 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1511 MODULE_DESCRIPTION("Afatech AF9035 driver");
1512 MODULE_LICENSE("GPL");
1513 MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
1514 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
1515 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);