Version:  2.0.40 2.2.26 2.4.37 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8

Linux/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 /* Max transfer size done by I2C transfer functions */
 25 #define MAX_XFER_SIZE  64
 26 
 27 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
 28 
 29 static u16 af9035_checksum(const u8 *buf, size_t len)
 30 {
 31         size_t i;
 32         u16 checksum = 0;
 33 
 34         for (i = 1; i < len; i++) {
 35                 if (i % 2)
 36                         checksum += buf[i] << 8;
 37                 else
 38                         checksum += buf[i];
 39         }
 40         checksum = ~checksum;
 41 
 42         return checksum;
 43 }
 44 
 45 static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
 46 {
 47 #define REQ_HDR_LEN 4 /* send header size */
 48 #define ACK_HDR_LEN 3 /* rece header size */
 49 #define CHECKSUM_LEN 2
 50 #define USB_TIMEOUT 2000
 51         struct state *state = d_to_priv(d);
 52         struct usb_interface *intf = d->intf;
 53         int ret, wlen, rlen;
 54         u16 checksum, tmp_checksum;
 55 
 56         mutex_lock(&d->usb_mutex);
 57 
 58         /* buffer overflow check */
 59         if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
 60                         req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
 61                 dev_err(&intf->dev, "too much data wlen=%d rlen=%d\n",
 62                         req->wlen, req->rlen);
 63                 ret = -EINVAL;
 64                 goto exit;
 65         }
 66 
 67         state->buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
 68         state->buf[1] = req->mbox;
 69         state->buf[2] = req->cmd;
 70         state->buf[3] = state->seq++;
 71         memcpy(&state->buf[REQ_HDR_LEN], req->wbuf, req->wlen);
 72 
 73         wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
 74         rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
 75 
 76         /* calc and add checksum */
 77         checksum = af9035_checksum(state->buf, state->buf[0] - 1);
 78         state->buf[state->buf[0] - 1] = (checksum >> 8);
 79         state->buf[state->buf[0] - 0] = (checksum & 0xff);
 80 
 81         /* no ack for these packets */
 82         if (req->cmd == CMD_FW_DL)
 83                 rlen = 0;
 84 
 85         ret = dvb_usbv2_generic_rw_locked(d,
 86                         state->buf, wlen, state->buf, rlen);
 87         if (ret)
 88                 goto exit;
 89 
 90         /* no ack for those packets */
 91         if (req->cmd == CMD_FW_DL)
 92                 goto exit;
 93 
 94         /* verify checksum */
 95         checksum = af9035_checksum(state->buf, rlen - 2);
 96         tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
 97         if (tmp_checksum != checksum) {
 98                 dev_err(&intf->dev, "command=%02x checksum mismatch (%04x != %04x)\n",
 99                         req->cmd, tmp_checksum, checksum);
100                 ret = -EIO;
101                 goto exit;
102         }
103 
104         /* check status */
105         if (state->buf[2]) {
106                 /* fw returns status 1 when IR code was not received */
107                 if (req->cmd == CMD_IR_GET || state->buf[2] == 1) {
108                         ret = 1;
109                         goto exit;
110                 }
111 
112                 dev_dbg(&intf->dev, "command=%02x failed fw error=%d\n",
113                         req->cmd, state->buf[2]);
114                 ret = -EIO;
115                 goto exit;
116         }
117 
118         /* read request, copy returned data to return buf */
119         if (req->rlen)
120                 memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
121 exit:
122         mutex_unlock(&d->usb_mutex);
123         if (ret < 0)
124                 dev_dbg(&intf->dev, "failed=%d\n", ret);
125         return ret;
126 }
127 
128 /* write multiple registers */
129 static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
130 {
131         struct usb_interface *intf = d->intf;
132         u8 wbuf[MAX_XFER_SIZE];
133         u8 mbox = (reg >> 16) & 0xff;
134         struct usb_req req = { CMD_MEM_WR, mbox, 6 + len, wbuf, 0, NULL };
135 
136         if (6 + len > sizeof(wbuf)) {
137                 dev_warn(&intf->dev, "i2c wr: len=%d is too big!\n", len);
138                 return -EOPNOTSUPP;
139         }
140 
141         wbuf[0] = len;
142         wbuf[1] = 2;
143         wbuf[2] = 0;
144         wbuf[3] = 0;
145         wbuf[4] = (reg >> 8) & 0xff;
146         wbuf[5] = (reg >> 0) & 0xff;
147         memcpy(&wbuf[6], val, len);
148 
149         return af9035_ctrl_msg(d, &req);
150 }
151 
152 /* read multiple registers */
153 static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
154 {
155         u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
156         u8 mbox = (reg >> 16) & 0xff;
157         struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
158 
159         return af9035_ctrl_msg(d, &req);
160 }
161 
162 /* write single register */
163 static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
164 {
165         return af9035_wr_regs(d, reg, &val, 1);
166 }
167 
168 /* read single register */
169 static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
170 {
171         return af9035_rd_regs(d, reg, val, 1);
172 }
173 
174 /* write single register with mask */
175 static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
176                 u8 mask)
177 {
178         int ret;
179         u8 tmp;
180 
181         /* no need for read if whole reg is written */
182         if (mask != 0xff) {
183                 ret = af9035_rd_regs(d, reg, &tmp, 1);
184                 if (ret)
185                         return ret;
186 
187                 val &= mask;
188                 tmp &= ~mask;
189                 val |= tmp;
190         }
191 
192         return af9035_wr_regs(d, reg, &val, 1);
193 }
194 
195 static int af9035_add_i2c_dev(struct dvb_usb_device *d, const char *type,
196                 u8 addr, void *platform_data, struct i2c_adapter *adapter)
197 {
198         int ret, num;
199         struct state *state = d_to_priv(d);
200         struct usb_interface *intf = d->intf;
201         struct i2c_client *client;
202         struct i2c_board_info board_info = {
203                 .addr = addr,
204                 .platform_data = platform_data,
205         };
206 
207         strlcpy(board_info.type, type, I2C_NAME_SIZE);
208 
209         /* find first free client */
210         for (num = 0; num < AF9035_I2C_CLIENT_MAX; num++) {
211                 if (state->i2c_client[num] == NULL)
212                         break;
213         }
214 
215         dev_dbg(&intf->dev, "num=%d\n", num);
216 
217         if (num == AF9035_I2C_CLIENT_MAX) {
218                 dev_err(&intf->dev, "I2C client out of index\n");
219                 ret = -ENODEV;
220                 goto err;
221         }
222 
223         request_module("%s", board_info.type);
224 
225         /* register I2C device */
226         client = i2c_new_device(adapter, &board_info);
227         if (client == NULL || client->dev.driver == NULL) {
228                 ret = -ENODEV;
229                 goto err;
230         }
231 
232         /* increase I2C driver usage count */
233         if (!try_module_get(client->dev.driver->owner)) {
234                 i2c_unregister_device(client);
235                 ret = -ENODEV;
236                 goto err;
237         }
238 
239         state->i2c_client[num] = client;
240         return 0;
241 err:
242         dev_dbg(&intf->dev, "failed=%d\n", ret);
243         return ret;
244 }
245 
246 static void af9035_del_i2c_dev(struct dvb_usb_device *d)
247 {
248         int num;
249         struct state *state = d_to_priv(d);
250         struct usb_interface *intf = d->intf;
251         struct i2c_client *client;
252 
253         /* find last used client */
254         num = AF9035_I2C_CLIENT_MAX;
255         while (num--) {
256                 if (state->i2c_client[num] != NULL)
257                         break;
258         }
259 
260         dev_dbg(&intf->dev, "num=%d\n", num);
261 
262         if (num == -1) {
263                 dev_err(&intf->dev, "I2C client out of index\n");
264                 goto err;
265         }
266 
267         client = state->i2c_client[num];
268 
269         /* decrease I2C driver usage count */
270         module_put(client->dev.driver->owner);
271 
272         /* unregister I2C device */
273         i2c_unregister_device(client);
274 
275         state->i2c_client[num] = NULL;
276         return;
277 err:
278         dev_dbg(&intf->dev, "failed\n");
279 }
280 
281 static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
282                 struct i2c_msg msg[], int num)
283 {
284         struct dvb_usb_device *d = i2c_get_adapdata(adap);
285         struct state *state = d_to_priv(d);
286         int ret;
287 
288         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
289                 return -EAGAIN;
290 
291         /*
292          * AF9035 I2C sub header is 5 bytes long. Meaning of those bytes are:
293          * 0: data len
294          * 1: I2C addr << 1
295          * 2: reg addr len
296          *    byte 3 and 4 can be used as reg addr
297          * 3: reg addr MSB
298          *    used when reg addr len is set to 2
299          * 4: reg addr LSB
300          *    used when reg addr len is set to 1 or 2
301          *
302          * For the simplify we do not use register addr at all.
303          * NOTE: As a firmware knows tuner type there is very small possibility
304          * there could be some tuner I2C hacks done by firmware and this may
305          * lead problems if firmware expects those bytes are used.
306          *
307          * TODO: Here is few hacks. AF9035 chip integrates AF9033 demodulator.
308          * IT9135 chip integrates AF9033 demodulator and RF tuner. For dual
309          * tuner devices, there is also external AF9033 demodulator connected
310          * via external I2C bus. All AF9033 demod I2C traffic, both single and
311          * dual tuner configuration, is covered by firmware - actual USB IO
312          * looks just like a memory access.
313          * In case of IT913x chip, there is own tuner driver. It is implemented
314          * currently as a I2C driver, even tuner IP block is likely build
315          * directly into the demodulator memory space and there is no own I2C
316          * bus. I2C subsystem does not allow register multiple devices to same
317          * bus, having same slave address. Due to that we reuse demod address,
318          * shifted by one bit, on that case.
319          *
320          * For IT930x we use a different command and the sub header is
321          * different as well:
322          * 0: data len
323          * 1: I2C bus (0x03 seems to be only value used)
324          * 2: I2C addr << 1
325          */
326 #define AF9035_IS_I2C_XFER_WRITE_READ(_msg, _num) \
327         (_num == 2 && !(_msg[0].flags & I2C_M_RD) && (_msg[1].flags & I2C_M_RD))
328 #define AF9035_IS_I2C_XFER_WRITE(_msg, _num) \
329         (_num == 1 && !(_msg[0].flags & I2C_M_RD))
330 #define AF9035_IS_I2C_XFER_READ(_msg, _num) \
331         (_num == 1 && (_msg[0].flags & I2C_M_RD))
332 
333         if (AF9035_IS_I2C_XFER_WRITE_READ(msg, num)) {
334                 if (msg[0].len > 40 || msg[1].len > 40) {
335                         /* TODO: correct limits > 40 */
336                         ret = -EOPNOTSUPP;
337                 } else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
338                            (msg[0].addr == state->af9033_i2c_addr[1]) ||
339                            (state->chip_type == 0x9135)) {
340                         /* demod access via firmware interface */
341                         u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
342                                         msg[0].buf[2];
343 
344                         if (msg[0].addr == state->af9033_i2c_addr[1] ||
345                             msg[0].addr == (state->af9033_i2c_addr[1] >> 1))
346                                 reg |= 0x100000;
347 
348                         ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
349                                         msg[1].len);
350                 } else if (state->no_read) {
351                         memset(msg[1].buf, 0, msg[1].len);
352                         ret = 0;
353                 } else {
354                         /* I2C write + read */
355                         u8 buf[MAX_XFER_SIZE];
356                         struct usb_req req = { CMD_I2C_RD, 0, 5 + msg[0].len,
357                                         buf, msg[1].len, msg[1].buf };
358 
359                         if (state->chip_type == 0x9306) {
360                                 req.cmd = CMD_GENERIC_I2C_RD;
361                                 req.wlen = 3 + msg[0].len;
362                         }
363                         req.mbox |= ((msg[0].addr & 0x80)  >>  3);
364 
365                         buf[0] = msg[1].len;
366                         if (state->chip_type == 0x9306) {
367                                 buf[1] = 0x03; /* I2C bus */
368                                 buf[2] = msg[0].addr << 1;
369                                 memcpy(&buf[3], msg[0].buf, msg[0].len);
370                         } else {
371                                 buf[1] = msg[0].addr << 1;
372                                 buf[3] = 0x00; /* reg addr MSB */
373                                 buf[4] = 0x00; /* reg addr LSB */
374 
375                                 /* Keep prev behavior for write req len > 2*/
376                                 if (msg[0].len > 2) {
377                                         buf[2] = 0x00; /* reg addr len */
378                                         memcpy(&buf[5], msg[0].buf, msg[0].len);
379 
380                                 /* Use reg addr fields if write req len <= 2 */
381                                 } else {
382                                         req.wlen = 5;
383                                         buf[2] = msg[0].len;
384                                         if (msg[0].len == 2) {
385                                                 buf[3] = msg[0].buf[0];
386                                                 buf[4] = msg[0].buf[1];
387                                         } else if (msg[0].len == 1) {
388                                                 buf[4] = msg[0].buf[0];
389                                         }
390                                 }
391                         }
392                         ret = af9035_ctrl_msg(d, &req);
393                 }
394         } else if (AF9035_IS_I2C_XFER_WRITE(msg, num)) {
395                 if (msg[0].len > 40) {
396                         /* TODO: correct limits > 40 */
397                         ret = -EOPNOTSUPP;
398                 } else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
399                            (msg[0].addr == state->af9033_i2c_addr[1]) ||
400                            (state->chip_type == 0x9135)) {
401                         /* demod access via firmware interface */
402                         u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
403                                         msg[0].buf[2];
404 
405                         if (msg[0].addr == state->af9033_i2c_addr[1] ||
406                             msg[0].addr == (state->af9033_i2c_addr[1] >> 1))
407                                 reg |= 0x100000;
408 
409                         ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
410                                         msg[0].len - 3);
411                 } else {
412                         /* I2C write */
413                         u8 buf[MAX_XFER_SIZE];
414                         struct usb_req req = { CMD_I2C_WR, 0, 5 + msg[0].len,
415                                         buf, 0, NULL };
416 
417                         if (state->chip_type == 0x9306) {
418                                 req.cmd = CMD_GENERIC_I2C_WR;
419                                 req.wlen = 3 + msg[0].len;
420                         }
421 
422                         req.mbox |= ((msg[0].addr & 0x80)  >>  3);
423                         buf[0] = msg[0].len;
424                         if (state->chip_type == 0x9306) {
425                                 buf[1] = 0x03; /* I2C bus */
426                                 buf[2] = msg[0].addr << 1;
427                                 memcpy(&buf[3], msg[0].buf, msg[0].len);
428                         } else {
429                                 buf[1] = msg[0].addr << 1;
430                                 buf[2] = 0x00; /* reg addr len */
431                                 buf[3] = 0x00; /* reg addr MSB */
432                                 buf[4] = 0x00; /* reg addr LSB */
433                                 memcpy(&buf[5], msg[0].buf, msg[0].len);
434                         }
435                         ret = af9035_ctrl_msg(d, &req);
436                 }
437         } else if (AF9035_IS_I2C_XFER_READ(msg, num)) {
438                 if (msg[0].len > 40) {
439                         /* TODO: correct limits > 40 */
440                         ret = -EOPNOTSUPP;
441                 } else if (state->no_read) {
442                         memset(msg[0].buf, 0, msg[0].len);
443                         ret = 0;
444                 } else {
445                         /* I2C read */
446                         u8 buf[5];
447                         struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
448                                                 buf, msg[0].len, msg[0].buf };
449 
450                         if (state->chip_type == 0x9306) {
451                                 req.cmd = CMD_GENERIC_I2C_RD;
452                                 req.wlen = 3;
453                         }
454                         req.mbox |= ((msg[0].addr & 0x80)  >>  3);
455                         buf[0] = msg[0].len;
456                         if (state->chip_type == 0x9306) {
457                                 buf[1] = 0x03; /* I2C bus */
458                                 buf[2] = msg[0].addr << 1;
459                         } else {
460                                 buf[1] = msg[0].addr << 1;
461                                 buf[2] = 0x00; /* reg addr len */
462                                 buf[3] = 0x00; /* reg addr MSB */
463                                 buf[4] = 0x00; /* reg addr LSB */
464                         }
465                         ret = af9035_ctrl_msg(d, &req);
466                 }
467         } else {
468                 /*
469                  * We support only three kind of I2C transactions:
470                  * 1) 1 x write + 1 x read (repeated start)
471                  * 2) 1 x write
472                  * 3) 1 x read
473                  */
474                 ret = -EOPNOTSUPP;
475         }
476 
477         mutex_unlock(&d->i2c_mutex);
478 
479         if (ret < 0)
480                 return ret;
481         else
482                 return num;
483 }
484 
485 static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
486 {
487         return I2C_FUNC_I2C;
488 }
489 
490 static struct i2c_algorithm af9035_i2c_algo = {
491         .master_xfer = af9035_i2c_master_xfer,
492         .functionality = af9035_i2c_functionality,
493 };
494 
495 static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
496 {
497         struct state *state = d_to_priv(d);
498         struct usb_interface *intf = d->intf;
499         int ret, ts_mode_invalid;
500         u8 tmp;
501         u8 wbuf[1] = { 1 };
502         u8 rbuf[4];
503         struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
504                         sizeof(rbuf), rbuf };
505 
506         ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
507         if (ret < 0)
508                 goto err;
509 
510         state->chip_version = rbuf[0];
511         state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
512 
513         ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
514         if (ret < 0)
515                 goto err;
516 
517         dev_info(&intf->dev, "prechip_version=%02x chip_version=%02x chip_type=%04x\n",
518                  state->prechip_version, state->chip_version, state->chip_type);
519 
520         if (state->chip_type == 0x9135) {
521                 if (state->chip_version == 0x02)
522                         *name = AF9035_FIRMWARE_IT9135_V2;
523                 else
524                         *name = AF9035_FIRMWARE_IT9135_V1;
525                 state->eeprom_addr = EEPROM_BASE_IT9135;
526         } else if (state->chip_type == 0x9306) {
527                 *name = AF9035_FIRMWARE_IT9303;
528                 state->eeprom_addr = EEPROM_BASE_IT9135;
529         } else {
530                 *name = AF9035_FIRMWARE_AF9035;
531                 state->eeprom_addr = EEPROM_BASE_AF9035;
532         }
533 
534 
535         /* check for dual tuner mode */
536         ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp);
537         if (ret < 0)
538                 goto err;
539 
540         ts_mode_invalid = 0;
541         switch (tmp) {
542         case 0:
543                 break;
544         case 1:
545         case 3:
546                 state->dual_mode = true;
547                 break;
548         case 5:
549                 if (state->chip_type != 0x9135 && state->chip_type != 0x9306)
550                         state->dual_mode = true;        /* AF9035 */
551                 else
552                         ts_mode_invalid = 1;
553                 break;
554         default:
555                 ts_mode_invalid = 1;
556         }
557 
558         dev_dbg(&intf->dev, "ts mode=%d dual mode=%d\n", tmp, state->dual_mode);
559 
560         if (ts_mode_invalid)
561                 dev_info(&intf->dev, "ts mode=%d not supported, defaulting to single tuner mode!", tmp);
562 
563 
564         ret = af9035_ctrl_msg(d, &req);
565         if (ret < 0)
566                 goto err;
567 
568         dev_dbg(&intf->dev, "reply=%*ph\n", 4, rbuf);
569         if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
570                 ret = WARM;
571         else
572                 ret = COLD;
573 
574         return ret;
575 
576 err:
577         dev_dbg(&intf->dev, "failed=%d\n", ret);
578 
579         return ret;
580 }
581 
582 static int af9035_download_firmware_old(struct dvb_usb_device *d,
583                 const struct firmware *fw)
584 {
585         struct usb_interface *intf = d->intf;
586         int ret, i, j, len;
587         u8 wbuf[1];
588         struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
589         struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
590         u8 hdr_core;
591         u16 hdr_addr, hdr_data_len, hdr_checksum;
592         #define MAX_DATA 58
593         #define HDR_SIZE 7
594 
595         /*
596          * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
597          *
598          * byte 0: MCS 51 core
599          *  There are two inside the AF9035 (1=Link and 2=OFDM) with separate
600          *  address spaces
601          * byte 1-2: Big endian destination address
602          * byte 3-4: Big endian number of data bytes following the header
603          * byte 5-6: Big endian header checksum, apparently ignored by the chip
604          *  Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
605          */
606 
607         for (i = fw->size; i > HDR_SIZE;) {
608                 hdr_core = fw->data[fw->size - i + 0];
609                 hdr_addr = fw->data[fw->size - i + 1] << 8;
610                 hdr_addr |= fw->data[fw->size - i + 2] << 0;
611                 hdr_data_len = fw->data[fw->size - i + 3] << 8;
612                 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
613                 hdr_checksum = fw->data[fw->size - i + 5] << 8;
614                 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
615 
616                 dev_dbg(&intf->dev, "core=%d addr=%04x data_len=%d checksum=%04x\n",
617                         hdr_core, hdr_addr, hdr_data_len, hdr_checksum);
618 
619                 if (((hdr_core != 1) && (hdr_core != 2)) ||
620                                 (hdr_data_len > i)) {
621                         dev_dbg(&intf->dev, "bad firmware\n");
622                         break;
623                 }
624 
625                 /* download begin packet */
626                 req.cmd = CMD_FW_DL_BEGIN;
627                 ret = af9035_ctrl_msg(d, &req);
628                 if (ret < 0)
629                         goto err;
630 
631                 /* download firmware packet(s) */
632                 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
633                         len = j;
634                         if (len > MAX_DATA)
635                                 len = MAX_DATA;
636                         req_fw_dl.wlen = len;
637                         req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
638                                         HDR_SIZE + hdr_data_len - j];
639                         ret = af9035_ctrl_msg(d, &req_fw_dl);
640                         if (ret < 0)
641                                 goto err;
642                 }
643 
644                 /* download end packet */
645                 req.cmd = CMD_FW_DL_END;
646                 ret = af9035_ctrl_msg(d, &req);
647                 if (ret < 0)
648                         goto err;
649 
650                 i -= hdr_data_len + HDR_SIZE;
651 
652                 dev_dbg(&intf->dev, "data uploaded=%zu\n", fw->size - i);
653         }
654 
655         /* print warn if firmware is bad, continue and see what happens */
656         if (i)
657                 dev_warn(&intf->dev, "bad firmware\n");
658 
659         return 0;
660 
661 err:
662         dev_dbg(&intf->dev, "failed=%d\n", ret);
663 
664         return ret;
665 }
666 
667 static int af9035_download_firmware_new(struct dvb_usb_device *d,
668                 const struct firmware *fw)
669 {
670         struct usb_interface *intf = d->intf;
671         int ret, i, i_prev;
672         struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
673         #define HDR_SIZE 7
674 
675         /*
676          * There seems to be following firmware header. Meaning of bytes 0-3
677          * is unknown.
678          *
679          * 0: 3
680          * 1: 0, 1
681          * 2: 0
682          * 3: 1, 2, 3
683          * 4: addr MSB
684          * 5: addr LSB
685          * 6: count of data bytes ?
686          */
687         for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
688                 if (i == fw->size ||
689                                 (fw->data[i + 0] == 0x03 &&
690                                 (fw->data[i + 1] == 0x00 ||
691                                 fw->data[i + 1] == 0x01) &&
692                                 fw->data[i + 2] == 0x00)) {
693                         req_fw_dl.wlen = i - i_prev;
694                         req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
695                         i_prev = i;
696                         ret = af9035_ctrl_msg(d, &req_fw_dl);
697                         if (ret < 0)
698                                 goto err;
699 
700                         dev_dbg(&intf->dev, "data uploaded=%d\n", i);
701                 }
702         }
703 
704         return 0;
705 
706 err:
707         dev_dbg(&intf->dev, "failed=%d\n", ret);
708 
709         return ret;
710 }
711 
712 static int af9035_download_firmware(struct dvb_usb_device *d,
713                 const struct firmware *fw)
714 {
715         struct usb_interface *intf = d->intf;
716         struct state *state = d_to_priv(d);
717         int ret;
718         u8 wbuf[1];
719         u8 rbuf[4];
720         u8 tmp;
721         struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
722         struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf };
723 
724         dev_dbg(&intf->dev, "\n");
725 
726         /*
727          * In case of dual tuner configuration we need to do some extra
728          * initialization in order to download firmware to slave demod too,
729          * which is done by master demod.
730          * Master feeds also clock and controls power via GPIO.
731          */
732         if (state->dual_mode) {
733                 /* configure gpioh1, reset & power slave demod */
734                 ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
735                 if (ret < 0)
736                         goto err;
737 
738                 ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
739                 if (ret < 0)
740                         goto err;
741 
742                 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
743                 if (ret < 0)
744                         goto err;
745 
746                 usleep_range(10000, 50000);
747 
748                 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
749                 if (ret < 0)
750                         goto err;
751 
752                 /* tell the slave I2C address */
753                 ret = af9035_rd_reg(d,
754                                 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
755                                 &tmp);
756                 if (ret < 0)
757                         goto err;
758 
759                 /* use default I2C address if eeprom has no address set */
760                 if (!tmp)
761                         tmp = 0x3a;
762 
763                 if ((state->chip_type == 0x9135) ||
764                                 (state->chip_type == 0x9306)) {
765                         ret = af9035_wr_reg(d, 0x004bfb, tmp);
766                         if (ret < 0)
767                                 goto err;
768                 } else {
769                         ret = af9035_wr_reg(d, 0x00417f, tmp);
770                         if (ret < 0)
771                                 goto err;
772 
773                         /* enable clock out */
774                         ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
775                         if (ret < 0)
776                                 goto err;
777                 }
778         }
779 
780         if (fw->data[0] == 0x01)
781                 ret = af9035_download_firmware_old(d, fw);
782         else
783                 ret = af9035_download_firmware_new(d, fw);
784         if (ret < 0)
785                 goto err;
786 
787         /* firmware loaded, request boot */
788         req.cmd = CMD_FW_BOOT;
789         ret = af9035_ctrl_msg(d, &req);
790         if (ret < 0)
791                 goto err;
792 
793         /* ensure firmware starts */
794         wbuf[0] = 1;
795         ret = af9035_ctrl_msg(d, &req_fw_ver);
796         if (ret < 0)
797                 goto err;
798 
799         if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
800                 dev_err(&intf->dev, "firmware did not run\n");
801                 ret = -ENODEV;
802                 goto err;
803         }
804 
805         dev_info(&intf->dev, "firmware version=%d.%d.%d.%d",
806                  rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
807 
808         return 0;
809 
810 err:
811         dev_dbg(&intf->dev, "failed=%d\n", ret);
812 
813         return ret;
814 }
815 
816 static int af9035_read_config(struct dvb_usb_device *d)
817 {
818         struct usb_interface *intf = d->intf;
819         struct state *state = d_to_priv(d);
820         int ret, i;
821         u8 tmp;
822         u16 tmp16, addr;
823 
824         /* demod I2C "address" */
825         state->af9033_i2c_addr[0] = 0x38;
826         state->af9033_i2c_addr[1] = 0x3a;
827         state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
828         state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
829         state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
830         state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
831 
832         if (state->chip_type == 0x9135) {
833                 /* feed clock for integrated RF tuner */
834                 state->af9033_config[0].dyn0_clk = true;
835                 state->af9033_config[1].dyn0_clk = true;
836 
837                 if (state->chip_version == 0x02) {
838                         state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
839                         state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
840                         tmp16 = 0x00461d; /* eeprom memory mapped location */
841                 } else {
842                         state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
843                         state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
844                         tmp16 = 0x00461b; /* eeprom memory mapped location */
845                 }
846 
847                 /* check if eeprom exists */
848                 ret = af9035_rd_reg(d, tmp16, &tmp);
849                 if (ret < 0)
850                         goto err;
851 
852                 if (tmp == 0x00) {
853                         dev_dbg(&intf->dev, "no eeprom\n");
854                         goto skip_eeprom;
855                 }
856         } else if (state->chip_type == 0x9306) {
857                 /*
858                  * IT930x is an USB bridge, only single demod-single tuner
859                  * configurations seen so far.
860                  */
861                 return 0;
862         }
863 
864 
865         if (state->dual_mode) {
866                 /* read 2nd demodulator I2C address */
867                 ret = af9035_rd_reg(d,
868                                 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
869                                 &tmp);
870                 if (ret < 0)
871                         goto err;
872 
873                 if (tmp)
874                         state->af9033_i2c_addr[1] = tmp;
875 
876                 dev_dbg(&intf->dev, "2nd demod I2C addr=%02x\n", tmp);
877         }
878 
879         addr = state->eeprom_addr;
880 
881         for (i = 0; i < state->dual_mode + 1; i++) {
882                 /* tuner */
883                 ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
884                 if (ret < 0)
885                         goto err;
886 
887                 dev_dbg(&intf->dev, "[%d]tuner=%02x\n", i, tmp);
888 
889                 /* tuner sanity check */
890                 if (state->chip_type == 0x9135) {
891                         if (state->chip_version == 0x02) {
892                                 /* IT9135 BX (v2) */
893                                 switch (tmp) {
894                                 case AF9033_TUNER_IT9135_60:
895                                 case AF9033_TUNER_IT9135_61:
896                                 case AF9033_TUNER_IT9135_62:
897                                         state->af9033_config[i].tuner = tmp;
898                                         break;
899                                 }
900                         } else {
901                                 /* IT9135 AX (v1) */
902                                 switch (tmp) {
903                                 case AF9033_TUNER_IT9135_38:
904                                 case AF9033_TUNER_IT9135_51:
905                                 case AF9033_TUNER_IT9135_52:
906                                         state->af9033_config[i].tuner = tmp;
907                                         break;
908                                 }
909                         }
910                 } else {
911                         /* AF9035 */
912                         state->af9033_config[i].tuner = tmp;
913                 }
914 
915                 if (state->af9033_config[i].tuner != tmp) {
916                         dev_info(&intf->dev, "[%d] overriding tuner from %02x to %02x\n",
917                                  i, tmp, state->af9033_config[i].tuner);
918                 }
919 
920                 switch (state->af9033_config[i].tuner) {
921                 case AF9033_TUNER_TUA9001:
922                 case AF9033_TUNER_FC0011:
923                 case AF9033_TUNER_MXL5007T:
924                 case AF9033_TUNER_TDA18218:
925                 case AF9033_TUNER_FC2580:
926                 case AF9033_TUNER_FC0012:
927                         state->af9033_config[i].spec_inv = 1;
928                         break;
929                 case AF9033_TUNER_IT9135_38:
930                 case AF9033_TUNER_IT9135_51:
931                 case AF9033_TUNER_IT9135_52:
932                 case AF9033_TUNER_IT9135_60:
933                 case AF9033_TUNER_IT9135_61:
934                 case AF9033_TUNER_IT9135_62:
935                         break;
936                 default:
937                         dev_warn(&intf->dev, "tuner id=%02x not supported, please report!",
938                                  tmp);
939                 }
940 
941                 /* disable dual mode if driver does not support it */
942                 if (i == 1)
943                         switch (state->af9033_config[i].tuner) {
944                         case AF9033_TUNER_FC0012:
945                         case AF9033_TUNER_IT9135_38:
946                         case AF9033_TUNER_IT9135_51:
947                         case AF9033_TUNER_IT9135_52:
948                         case AF9033_TUNER_IT9135_60:
949                         case AF9033_TUNER_IT9135_61:
950                         case AF9033_TUNER_IT9135_62:
951                         case AF9033_TUNER_MXL5007T:
952                                 break;
953                         default:
954                                 state->dual_mode = false;
955                                 dev_info(&intf->dev, "driver does not support 2nd tuner and will disable it");
956                 }
957 
958                 /* tuner IF frequency */
959                 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
960                 if (ret < 0)
961                         goto err;
962 
963                 tmp16 = tmp;
964 
965                 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
966                 if (ret < 0)
967                         goto err;
968 
969                 tmp16 |= tmp << 8;
970 
971                 dev_dbg(&intf->dev, "[%d]IF=%d\n", i, tmp16);
972 
973                 addr += 0x10; /* shift for the 2nd tuner params */
974         }
975 
976 skip_eeprom:
977         /* get demod clock */
978         ret = af9035_rd_reg(d, 0x00d800, &tmp);
979         if (ret < 0)
980                 goto err;
981 
982         tmp = (tmp >> 0) & 0x0f;
983 
984         for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
985                 if (state->chip_type == 0x9135)
986                         state->af9033_config[i].clock = clock_lut_it9135[tmp];
987                 else
988                         state->af9033_config[i].clock = clock_lut_af9035[tmp];
989         }
990 
991         state->no_read = false;
992         /* Some MXL5007T devices cannot properly handle tuner I2C read ops. */
993         if (state->af9033_config[0].tuner == AF9033_TUNER_MXL5007T &&
994                 le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA)
995 
996                 switch (le16_to_cpu(d->udev->descriptor.idProduct)) {
997                 case USB_PID_AVERMEDIA_A867:
998                 case USB_PID_AVERMEDIA_TWINSTAR:
999                         dev_info(&intf->dev,
1000                                  "Device may have issues with I2C read operations. Enabling fix.\n");
1001                         state->no_read = true;
1002                         break;
1003                 }
1004 
1005         return 0;
1006 
1007 err:
1008         dev_dbg(&intf->dev, "failed=%d\n", ret);
1009 
1010         return ret;
1011 }
1012 
1013 static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
1014                 int cmd, int arg)
1015 {
1016         struct usb_interface *intf = d->intf;
1017         int ret;
1018         u8 val;
1019 
1020         dev_dbg(&intf->dev, "cmd=%d arg=%d\n", cmd, arg);
1021 
1022         /*
1023          * CEN     always enabled by hardware wiring
1024          * RESETN  GPIOT3
1025          * RXEN    GPIOT2
1026          */
1027 
1028         switch (cmd) {
1029         case TUA9001_CMD_RESETN:
1030                 if (arg)
1031                         val = 0x00;
1032                 else
1033                         val = 0x01;
1034 
1035                 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
1036                 if (ret < 0)
1037                         goto err;
1038                 break;
1039         case TUA9001_CMD_RXEN:
1040                 if (arg)
1041                         val = 0x01;
1042                 else
1043                         val = 0x00;
1044 
1045                 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
1046                 if (ret < 0)
1047                         goto err;
1048                 break;
1049         }
1050 
1051         return 0;
1052 
1053 err:
1054         dev_dbg(&intf->dev, "failed=%d\n", ret);
1055 
1056         return ret;
1057 }
1058 
1059 
1060 static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
1061                 int cmd, int arg)
1062 {
1063         struct usb_interface *intf = d->intf;
1064         int ret;
1065 
1066         switch (cmd) {
1067         case FC0011_FE_CALLBACK_POWER:
1068                 /* Tuner enable */
1069                 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
1070                 if (ret < 0)
1071                         goto err;
1072 
1073                 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
1074                 if (ret < 0)
1075                         goto err;
1076 
1077                 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
1078                 if (ret < 0)
1079                         goto err;
1080 
1081                 /* LED */
1082                 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
1083                 if (ret < 0)
1084                         goto err;
1085 
1086                 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
1087                 if (ret < 0)
1088                         goto err;
1089 
1090                 usleep_range(10000, 50000);
1091                 break;
1092         case FC0011_FE_CALLBACK_RESET:
1093                 ret = af9035_wr_reg(d, 0xd8e9, 1);
1094                 if (ret < 0)
1095                         goto err;
1096 
1097                 ret = af9035_wr_reg(d, 0xd8e8, 1);
1098                 if (ret < 0)
1099                         goto err;
1100 
1101                 ret = af9035_wr_reg(d, 0xd8e7, 1);
1102                 if (ret < 0)
1103                         goto err;
1104 
1105                 usleep_range(10000, 20000);
1106 
1107                 ret = af9035_wr_reg(d, 0xd8e7, 0);
1108                 if (ret < 0)
1109                         goto err;
1110 
1111                 usleep_range(10000, 20000);
1112                 break;
1113         default:
1114                 ret = -EINVAL;
1115                 goto err;
1116         }
1117 
1118         return 0;
1119 
1120 err:
1121         dev_dbg(&intf->dev, "failed=%d\n", ret);
1122 
1123         return ret;
1124 }
1125 
1126 static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
1127 {
1128         struct state *state = d_to_priv(d);
1129 
1130         switch (state->af9033_config[0].tuner) {
1131         case AF9033_TUNER_FC0011:
1132                 return af9035_fc0011_tuner_callback(d, cmd, arg);
1133         case AF9033_TUNER_TUA9001:
1134                 return af9035_tua9001_tuner_callback(d, cmd, arg);
1135         default:
1136                 break;
1137         }
1138 
1139         return 0;
1140 }
1141 
1142 static int af9035_frontend_callback(void *adapter_priv, int component,
1143                                     int cmd, int arg)
1144 {
1145         struct i2c_adapter *adap = adapter_priv;
1146         struct dvb_usb_device *d = i2c_get_adapdata(adap);
1147         struct usb_interface *intf = d->intf;
1148 
1149         dev_dbg(&intf->dev, "component=%d cmd=%d arg=%d\n",
1150                 component, cmd, arg);
1151 
1152         switch (component) {
1153         case DVB_FRONTEND_COMPONENT_TUNER:
1154                 return af9035_tuner_callback(d, cmd, arg);
1155         default:
1156                 break;
1157         }
1158 
1159         return 0;
1160 }
1161 
1162 static int af9035_get_adapter_count(struct dvb_usb_device *d)
1163 {
1164         struct state *state = d_to_priv(d);
1165 
1166         return state->dual_mode + 1;
1167 }
1168 
1169 static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
1170 {
1171         struct state *state = adap_to_priv(adap);
1172         struct dvb_usb_device *d = adap_to_d(adap);
1173         struct usb_interface *intf = d->intf;
1174         int ret;
1175 
1176         dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1177 
1178         if (!state->af9033_config[adap->id].tuner) {
1179                 /* unsupported tuner */
1180                 ret = -ENODEV;
1181                 goto err;
1182         }
1183 
1184         state->af9033_config[adap->id].fe = &adap->fe[0];
1185         state->af9033_config[adap->id].ops = &state->ops;
1186         ret = af9035_add_i2c_dev(d, "af9033", state->af9033_i2c_addr[adap->id],
1187                         &state->af9033_config[adap->id], &d->i2c_adap);
1188         if (ret)
1189                 goto err;
1190 
1191         if (adap->fe[0] == NULL) {
1192                 ret = -ENODEV;
1193                 goto err;
1194         }
1195 
1196         /* disable I2C-gate */
1197         adap->fe[0]->ops.i2c_gate_ctrl = NULL;
1198         adap->fe[0]->callback = af9035_frontend_callback;
1199 
1200         return 0;
1201 
1202 err:
1203         dev_dbg(&intf->dev, "failed=%d\n", ret);
1204 
1205         return ret;
1206 }
1207 
1208 static int it930x_frontend_attach(struct dvb_usb_adapter *adap)
1209 {
1210         struct state *state = adap_to_priv(adap);
1211         struct dvb_usb_device *d = adap_to_d(adap);
1212         struct usb_interface *intf = d->intf;
1213         int ret;
1214         struct si2168_config si2168_config;
1215         struct i2c_adapter *adapter;
1216 
1217         dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1218 
1219         memset(&si2168_config, 0, sizeof(si2168_config));
1220         si2168_config.i2c_adapter = &adapter;
1221         si2168_config.fe = &adap->fe[0];
1222         si2168_config.ts_mode = SI2168_TS_SERIAL;
1223 
1224         state->af9033_config[adap->id].fe = &adap->fe[0];
1225         state->af9033_config[adap->id].ops = &state->ops;
1226         ret = af9035_add_i2c_dev(d, "si2168", 0x67, &si2168_config,
1227                                 &d->i2c_adap);
1228         if (ret)
1229                 goto err;
1230 
1231         if (adap->fe[0] == NULL) {
1232                 ret = -ENODEV;
1233                 goto err;
1234         }
1235         state->i2c_adapter_demod = adapter;
1236 
1237         return 0;
1238 
1239 err:
1240         dev_dbg(&intf->dev, "failed=%d\n", ret);
1241 
1242         return ret;
1243 }
1244 
1245 static int af9035_frontend_detach(struct dvb_usb_adapter *adap)
1246 {
1247         struct state *state = adap_to_priv(adap);
1248         struct dvb_usb_device *d = adap_to_d(adap);
1249         struct usb_interface *intf = d->intf;
1250         int demod2;
1251 
1252         dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1253 
1254         /*
1255          * For dual tuner devices we have to resolve 2nd demod client, as there
1256          * is two different kind of tuner drivers; one is using I2C binding
1257          * and the other is using DVB attach/detach binding.
1258          */
1259         switch (state->af9033_config[adap->id].tuner) {
1260         case AF9033_TUNER_IT9135_38:
1261         case AF9033_TUNER_IT9135_51:
1262         case AF9033_TUNER_IT9135_52:
1263         case AF9033_TUNER_IT9135_60:
1264         case AF9033_TUNER_IT9135_61:
1265         case AF9033_TUNER_IT9135_62:
1266                 demod2 = 2;
1267                 break;
1268         default:
1269                 demod2 = 1;
1270         }
1271 
1272         if (adap->id == 1) {
1273                 if (state->i2c_client[demod2])
1274                         af9035_del_i2c_dev(d);
1275         } else if (adap->id == 0) {
1276                 if (state->i2c_client[0])
1277                         af9035_del_i2c_dev(d);
1278         }
1279 
1280         return 0;
1281 }
1282 
1283 static const struct fc0011_config af9035_fc0011_config = {
1284         .i2c_address = 0x60,
1285 };
1286 
1287 static struct mxl5007t_config af9035_mxl5007t_config[] = {
1288         {
1289                 .xtal_freq_hz = MxL_XTAL_24_MHZ,
1290                 .if_freq_hz = MxL_IF_4_57_MHZ,
1291                 .invert_if = 0,
1292                 .loop_thru_enable = 0,
1293                 .clk_out_enable = 0,
1294                 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1295         }, {
1296                 .xtal_freq_hz = MxL_XTAL_24_MHZ,
1297                 .if_freq_hz = MxL_IF_4_57_MHZ,
1298                 .invert_if = 0,
1299                 .loop_thru_enable = 1,
1300                 .clk_out_enable = 1,
1301                 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1302         }
1303 };
1304 
1305 static struct tda18218_config af9035_tda18218_config = {
1306         .i2c_address = 0x60,
1307         .i2c_wr_max = 21,
1308 };
1309 
1310 static const struct fc0012_config af9035_fc0012_config[] = {
1311         {
1312                 .i2c_address = 0x63,
1313                 .xtal_freq = FC_XTAL_36_MHZ,
1314                 .dual_master = true,
1315                 .loop_through = true,
1316                 .clock_out = true,
1317         }, {
1318                 .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
1319                 .xtal_freq = FC_XTAL_36_MHZ,
1320                 .dual_master = true,
1321         }
1322 };
1323 
1324 static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1325 {
1326         struct state *state = adap_to_priv(adap);
1327         struct dvb_usb_device *d = adap_to_d(adap);
1328         struct usb_interface *intf = d->intf;
1329         int ret;
1330         struct dvb_frontend *fe;
1331         struct i2c_msg msg[1];
1332         u8 tuner_addr;
1333 
1334         dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1335 
1336         /*
1337          * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1338          * to carry info about used I2C bus for dual tuner configuration.
1339          */
1340 
1341         switch (state->af9033_config[adap->id].tuner) {
1342         case AF9033_TUNER_TUA9001: {
1343                 struct tua9001_platform_data tua9001_pdata = {
1344                         .dvb_frontend = adap->fe[0],
1345                 };
1346 
1347                 /*
1348                  * AF9035 gpiot3 = TUA9001 RESETN
1349                  * AF9035 gpiot2 = TUA9001 RXEN
1350                  */
1351 
1352                 /* configure gpiot2 and gpiot2 as output */
1353                 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
1354                 if (ret < 0)
1355                         goto err;
1356 
1357                 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
1358                 if (ret < 0)
1359                         goto err;
1360 
1361                 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
1362                 if (ret < 0)
1363                         goto err;
1364 
1365                 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
1366                 if (ret < 0)
1367                         goto err;
1368 
1369                 /* attach tuner */
1370                 ret = af9035_add_i2c_dev(d, "tua9001", 0x60, &tua9001_pdata,
1371                                          &d->i2c_adap);
1372                 if (ret)
1373                         goto err;
1374 
1375                 fe = adap->fe[0];
1376                 break;
1377         }
1378         case AF9033_TUNER_FC0011:
1379                 fe = dvb_attach(fc0011_attach, adap->fe[0],
1380                                 &d->i2c_adap, &af9035_fc0011_config);
1381                 break;
1382         case AF9033_TUNER_MXL5007T:
1383                 if (adap->id == 0) {
1384                         ret = af9035_wr_reg(d, 0x00d8e0, 1);
1385                         if (ret < 0)
1386                                 goto err;
1387 
1388                         ret = af9035_wr_reg(d, 0x00d8e1, 1);
1389                         if (ret < 0)
1390                                 goto err;
1391 
1392                         ret = af9035_wr_reg(d, 0x00d8df, 0);
1393                         if (ret < 0)
1394                                 goto err;
1395 
1396                         msleep(30);
1397 
1398                         ret = af9035_wr_reg(d, 0x00d8df, 1);
1399                         if (ret < 0)
1400                                 goto err;
1401 
1402                         msleep(300);
1403 
1404                         ret = af9035_wr_reg(d, 0x00d8c0, 1);
1405                         if (ret < 0)
1406                                 goto err;
1407 
1408                         ret = af9035_wr_reg(d, 0x00d8c1, 1);
1409                         if (ret < 0)
1410                                 goto err;
1411 
1412                         ret = af9035_wr_reg(d, 0x00d8bf, 0);
1413                         if (ret < 0)
1414                                 goto err;
1415 
1416                         ret = af9035_wr_reg(d, 0x00d8b4, 1);
1417                         if (ret < 0)
1418                                 goto err;
1419 
1420                         ret = af9035_wr_reg(d, 0x00d8b5, 1);
1421                         if (ret < 0)
1422                                 goto err;
1423 
1424                         ret = af9035_wr_reg(d, 0x00d8b3, 1);
1425                         if (ret < 0)
1426                                 goto err;
1427 
1428                         tuner_addr = 0x60;
1429                 } else {
1430                         tuner_addr = 0x60 | 0x80; /* I2C bus hack */
1431                 }
1432 
1433                 /* attach tuner */
1434                 fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1435                                 tuner_addr, &af9035_mxl5007t_config[adap->id]);
1436                 break;
1437         case AF9033_TUNER_TDA18218:
1438                 /* attach tuner */
1439                 fe = dvb_attach(tda18218_attach, adap->fe[0],
1440                                 &d->i2c_adap, &af9035_tda18218_config);
1441                 break;
1442         case AF9033_TUNER_FC2580: {
1443                 struct fc2580_platform_data fc2580_pdata = {
1444                         .dvb_frontend = adap->fe[0],
1445                 };
1446 
1447                 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on  */
1448                 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1449                 if (ret < 0)
1450                         goto err;
1451 
1452                 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1453                 if (ret < 0)
1454                         goto err;
1455 
1456                 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1457                 if (ret < 0)
1458                         goto err;
1459 
1460                 usleep_range(10000, 50000);
1461                 /* attach tuner */
1462                 ret = af9035_add_i2c_dev(d, "fc2580", 0x56, &fc2580_pdata,
1463                                          &d->i2c_adap);
1464                 if (ret)
1465                         goto err;
1466 
1467                 fe = adap->fe[0];
1468                 break;
1469         }
1470         case AF9033_TUNER_FC0012:
1471                 /*
1472                  * AF9035 gpiot2 = FC0012 enable
1473                  * XXX: there seems to be something on gpioh8 too, but on my
1474                  * my test I didn't find any difference.
1475                  */
1476 
1477                 if (adap->id == 0) {
1478                         /* configure gpiot2 as output and high */
1479                         ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1480                         if (ret < 0)
1481                                 goto err;
1482 
1483                         ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1484                         if (ret < 0)
1485                                 goto err;
1486 
1487                         ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1488                         if (ret < 0)
1489                                 goto err;
1490                 } else {
1491                         /*
1492                          * FIXME: That belongs for the FC0012 driver.
1493                          * Write 02 to FC0012 master tuner register 0d directly
1494                          * in order to make slave tuner working.
1495                          */
1496                         msg[0].addr = 0x63;
1497                         msg[0].flags = 0;
1498                         msg[0].len = 2;
1499                         msg[0].buf = "\x0d\x02";
1500                         ret = i2c_transfer(&d->i2c_adap, msg, 1);
1501                         if (ret < 0)
1502                                 goto err;
1503                 }
1504 
1505                 usleep_range(10000, 50000);
1506 
1507                 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
1508                                 &af9035_fc0012_config[adap->id]);
1509                 break;
1510         case AF9033_TUNER_IT9135_38:
1511         case AF9033_TUNER_IT9135_51:
1512         case AF9033_TUNER_IT9135_52:
1513         {
1514                 struct it913x_config it913x_config = {
1515                         .fe = adap->fe[0],
1516                         .chip_ver = 1,
1517                 };
1518 
1519                 if (state->dual_mode) {
1520                         if (adap->id == 0)
1521                                 it913x_config.role = IT913X_ROLE_DUAL_MASTER;
1522                         else
1523                                 it913x_config.role = IT913X_ROLE_DUAL_SLAVE;
1524                 }
1525 
1526                 ret = af9035_add_i2c_dev(d, "it913x",
1527                                 state->af9033_i2c_addr[adap->id] >> 1,
1528                                 &it913x_config, &d->i2c_adap);
1529                 if (ret)
1530                         goto err;
1531 
1532                 fe = adap->fe[0];
1533                 break;
1534         }
1535         case AF9033_TUNER_IT9135_60:
1536         case AF9033_TUNER_IT9135_61:
1537         case AF9033_TUNER_IT9135_62:
1538         {
1539                 struct it913x_config it913x_config = {
1540                         .fe = adap->fe[0],
1541                         .chip_ver = 2,
1542                 };
1543 
1544                 if (state->dual_mode) {
1545                         if (adap->id == 0)
1546                                 it913x_config.role = IT913X_ROLE_DUAL_MASTER;
1547                         else
1548                                 it913x_config.role = IT913X_ROLE_DUAL_SLAVE;
1549                 }
1550 
1551                 ret = af9035_add_i2c_dev(d, "it913x",
1552                                 state->af9033_i2c_addr[adap->id] >> 1,
1553                                 &it913x_config, &d->i2c_adap);
1554                 if (ret)
1555                         goto err;
1556 
1557                 fe = adap->fe[0];
1558                 break;
1559         }
1560         default:
1561                 fe = NULL;
1562         }
1563 
1564         if (fe == NULL) {
1565                 ret = -ENODEV;
1566                 goto err;
1567         }
1568 
1569         return 0;
1570 
1571 err:
1572         dev_dbg(&intf->dev, "failed=%d\n", ret);
1573 
1574         return ret;
1575 }
1576 
1577 static int it930x_tuner_attach(struct dvb_usb_adapter *adap)
1578 {
1579         struct state *state = adap_to_priv(adap);
1580         struct dvb_usb_device *d = adap_to_d(adap);
1581         struct usb_interface *intf = d->intf;
1582         int ret;
1583         struct si2157_config si2157_config;
1584 
1585         dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1586 
1587         /* I2C master bus 2 clock speed 300k */
1588         ret = af9035_wr_reg(d, 0x00f6a7, 0x07);
1589         if (ret < 0)
1590                 goto err;
1591 
1592         /* I2C master bus 1,3 clock speed 300k */
1593         ret = af9035_wr_reg(d, 0x00f103, 0x07);
1594         if (ret < 0)
1595                 goto err;
1596 
1597         /* set gpio11 low */
1598         ret = af9035_wr_reg_mask(d, 0xd8d4, 0x01, 0x01);
1599         if (ret < 0)
1600                 goto err;
1601 
1602         ret = af9035_wr_reg_mask(d, 0xd8d5, 0x01, 0x01);
1603         if (ret < 0)
1604                 goto err;
1605 
1606         ret = af9035_wr_reg_mask(d, 0xd8d3, 0x01, 0x01);
1607         if (ret < 0)
1608                 goto err;
1609 
1610         /* Tuner enable using gpiot2_en, gpiot2_on and gpiot2_o (reset) */
1611         ret = af9035_wr_reg_mask(d, 0xd8b8, 0x01, 0x01);
1612         if (ret < 0)
1613                 goto err;
1614 
1615         ret = af9035_wr_reg_mask(d, 0xd8b9, 0x01, 0x01);
1616         if (ret < 0)
1617                 goto err;
1618 
1619         ret = af9035_wr_reg_mask(d, 0xd8b7, 0x00, 0x01);
1620         if (ret < 0)
1621                 goto err;
1622 
1623         msleep(200);
1624 
1625         ret = af9035_wr_reg_mask(d, 0xd8b7, 0x01, 0x01);
1626         if (ret < 0)
1627                 goto err;
1628 
1629         memset(&si2157_config, 0, sizeof(si2157_config));
1630         si2157_config.fe = adap->fe[0];
1631         si2157_config.if_port = 1;
1632         ret = af9035_add_i2c_dev(d, "si2157", 0x63,
1633                         &si2157_config, state->i2c_adapter_demod);
1634 
1635         if (ret)
1636                 goto err;
1637 
1638         return 0;
1639 
1640 err:
1641         dev_dbg(&intf->dev, "failed=%d\n", ret);
1642 
1643         return ret;
1644 }
1645 
1646 
1647 static int it930x_tuner_detach(struct dvb_usb_adapter *adap)
1648 {
1649         struct state *state = adap_to_priv(adap);
1650         struct dvb_usb_device *d = adap_to_d(adap);
1651         struct usb_interface *intf = d->intf;
1652 
1653         dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1654 
1655         if (adap->id == 1) {
1656                 if (state->i2c_client[3])
1657                         af9035_del_i2c_dev(d);
1658         } else if (adap->id == 0) {
1659                 if (state->i2c_client[1])
1660                         af9035_del_i2c_dev(d);
1661         }
1662 
1663         return 0;
1664 }
1665 
1666 
1667 static int af9035_tuner_detach(struct dvb_usb_adapter *adap)
1668 {
1669         struct state *state = adap_to_priv(adap);
1670         struct dvb_usb_device *d = adap_to_d(adap);
1671         struct usb_interface *intf = d->intf;
1672 
1673         dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1674 
1675         switch (state->af9033_config[adap->id].tuner) {
1676         case AF9033_TUNER_TUA9001:
1677         case AF9033_TUNER_FC2580:
1678         case AF9033_TUNER_IT9135_38:
1679         case AF9033_TUNER_IT9135_51:
1680         case AF9033_TUNER_IT9135_52:
1681         case AF9033_TUNER_IT9135_60:
1682         case AF9033_TUNER_IT9135_61:
1683         case AF9033_TUNER_IT9135_62:
1684                 if (adap->id == 1) {
1685                         if (state->i2c_client[3])
1686                                 af9035_del_i2c_dev(d);
1687                 } else if (adap->id == 0) {
1688                         if (state->i2c_client[1])
1689                                 af9035_del_i2c_dev(d);
1690                 }
1691         }
1692 
1693         return 0;
1694 }
1695 
1696 static int af9035_init(struct dvb_usb_device *d)
1697 {
1698         struct state *state = d_to_priv(d);
1699         struct usb_interface *intf = d->intf;
1700         int ret, i;
1701         u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1702         u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1703         struct reg_val_mask tab[] = {
1704                 { 0x80f99d, 0x01, 0x01 },
1705                 { 0x80f9a4, 0x01, 0x01 },
1706                 { 0x00dd11, 0x00, 0x20 },
1707                 { 0x00dd11, 0x00, 0x40 },
1708                 { 0x00dd13, 0x00, 0x20 },
1709                 { 0x00dd13, 0x00, 0x40 },
1710                 { 0x00dd11, 0x20, 0x20 },
1711                 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1712                 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1713                 { 0x00dd0c, packet_size, 0xff},
1714                 { 0x00dd11, state->dual_mode << 6, 0x40 },
1715                 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1716                 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1717                 { 0x00dd0d, packet_size, 0xff },
1718                 { 0x80f9a3, state->dual_mode, 0x01 },
1719                 { 0x80f9cd, state->dual_mode, 0x01 },
1720                 { 0x80f99d, 0x00, 0x01 },
1721                 { 0x80f9a4, 0x00, 0x01 },
1722         };
1723 
1724         dev_dbg(&intf->dev, "USB speed=%d frame_size=%04x packet_size=%02x\n",
1725                 d->udev->speed, frame_size, packet_size);
1726 
1727         /* init endpoints */
1728         for (i = 0; i < ARRAY_SIZE(tab); i++) {
1729                 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1730                                 tab[i].mask);
1731                 if (ret < 0)
1732                         goto err;
1733         }
1734 
1735         return 0;
1736 
1737 err:
1738         dev_dbg(&intf->dev, "failed=%d\n", ret);
1739 
1740         return ret;
1741 }
1742 
1743 static int it930x_init(struct dvb_usb_device *d)
1744 {
1745         struct state *state = d_to_priv(d);
1746         struct usb_interface *intf = d->intf;
1747         int ret, i;
1748         u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 816) * 188 / 4;
1749         u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1750         struct reg_val_mask tab[] = {
1751                 { 0x00da1a, 0x00, 0x01 }, /* ignore_sync_byte */
1752                 { 0x00f41f, 0x04, 0x04 }, /* dvbt_inten */
1753                 { 0x00da10, 0x00, 0x01 }, /* mpeg_full_speed */
1754                 { 0x00f41a, 0x01, 0x01 }, /* dvbt_en */
1755                 { 0x00da1d, 0x01, 0x01 }, /* mp2_sw_rst, reset EP4 */
1756                 { 0x00dd11, 0x00, 0x20 }, /* ep4_tx_en, disable EP4 */
1757                 { 0x00dd13, 0x00, 0x20 }, /* ep4_tx_nak, disable EP4 NAK */
1758                 { 0x00dd11, 0x20, 0x20 }, /* ep4_tx_en, enable EP4 */
1759                 { 0x00dd11, 0x00, 0x40 }, /* ep5_tx_en, disable EP5 */
1760                 { 0x00dd13, 0x00, 0x40 }, /* ep5_tx_nak, disable EP5 NAK */
1761                 { 0x00dd11, state->dual_mode << 6, 0x40 }, /* enable EP5 */
1762                 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1763                 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1764                 { 0x00dd0c, packet_size, 0xff},
1765                 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1766                 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1767                 { 0x00dd0d, packet_size, 0xff },
1768                 { 0x00da1d, 0x00, 0x01 }, /* mp2_sw_rst, disable */
1769                 { 0x00d833, 0x01, 0xff }, /* slew rate ctrl: slew rate boosts */
1770                 { 0x00d830, 0x00, 0xff }, /* Bit 0 of output driving control */
1771                 { 0x00d831, 0x01, 0xff }, /* Bit 1 of output driving control */
1772                 { 0x00d832, 0x00, 0xff }, /* Bit 2 of output driving control */
1773 
1774                 /* suspend gpio1 for TS-C */
1775                 { 0x00d8b0, 0x01, 0xff }, /* gpio1 */
1776                 { 0x00d8b1, 0x01, 0xff }, /* gpio1 */
1777                 { 0x00d8af, 0x00, 0xff }, /* gpio1 */
1778 
1779                 /* suspend gpio7 for TS-D */
1780                 { 0x00d8c4, 0x01, 0xff }, /* gpio7 */
1781                 { 0x00d8c5, 0x01, 0xff }, /* gpio7 */
1782                 { 0x00d8c3, 0x00, 0xff }, /* gpio7 */
1783 
1784                 /* suspend gpio13 for TS-B */
1785                 { 0x00d8dc, 0x01, 0xff }, /* gpio13 */
1786                 { 0x00d8dd, 0x01, 0xff }, /* gpio13 */
1787                 { 0x00d8db, 0x00, 0xff }, /* gpio13 */
1788 
1789                 /* suspend gpio14 for TS-E */
1790                 { 0x00d8e4, 0x01, 0xff }, /* gpio14 */
1791                 { 0x00d8e5, 0x01, 0xff }, /* gpio14 */
1792                 { 0x00d8e3, 0x00, 0xff }, /* gpio14 */
1793 
1794                 /* suspend gpio15 for TS-A */
1795                 { 0x00d8e8, 0x01, 0xff }, /* gpio15 */
1796                 { 0x00d8e9, 0x01, 0xff }, /* gpio15 */
1797                 { 0x00d8e7, 0x00, 0xff }, /* gpio15 */
1798 
1799                 { 0x00da58, 0x00, 0x01 }, /* ts_in_src, serial */
1800                 { 0x00da73, 0x01, 0xff }, /* ts0_aggre_mode */
1801                 { 0x00da78, 0x47, 0xff }, /* ts0_sync_byte */
1802                 { 0x00da4c, 0x01, 0xff }, /* ts0_en */
1803                 { 0x00da5a, 0x1f, 0xff }, /* ts_fail_ignore */
1804         };
1805 
1806         dev_dbg(&intf->dev, "USB speed=%d frame_size=%04x packet_size=%02x\n",
1807                 d->udev->speed, frame_size, packet_size);
1808 
1809         /* init endpoints */
1810         for (i = 0; i < ARRAY_SIZE(tab); i++) {
1811                 ret = af9035_wr_reg_mask(d, tab[i].reg,
1812                                 tab[i].val, tab[i].mask);
1813 
1814                 if (ret < 0)
1815                         goto err;
1816         }
1817 
1818         return 0;
1819 err:
1820         dev_dbg(&intf->dev, "failed=%d\n", ret);
1821 
1822         return ret;
1823 }
1824 
1825 
1826 #if IS_ENABLED(CONFIG_RC_CORE)
1827 static int af9035_rc_query(struct dvb_usb_device *d)
1828 {
1829         struct usb_interface *intf = d->intf;
1830         int ret;
1831         u32 key;
1832         u8 buf[4];
1833         struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
1834 
1835         ret = af9035_ctrl_msg(d, &req);
1836         if (ret == 1)
1837                 return 0;
1838         else if (ret < 0)
1839                 goto err;
1840 
1841         if ((buf[2] + buf[3]) == 0xff) {
1842                 if ((buf[0] + buf[1]) == 0xff) {
1843                         /* NEC standard 16bit */
1844                         key = RC_SCANCODE_NEC(buf[0], buf[2]);
1845                 } else {
1846                         /* NEC extended 24bit */
1847                         key = RC_SCANCODE_NECX(buf[0] << 8 | buf[1], buf[2]);
1848                 }
1849         } else {
1850                 /* NEC full code 32bit */
1851                 key = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
1852                                         buf[2] << 8  | buf[3]);
1853         }
1854 
1855         dev_dbg(&intf->dev, "%*ph\n", 4, buf);
1856 
1857         rc_keydown(d->rc_dev, RC_TYPE_NEC, key, 0);
1858 
1859         return 0;
1860 
1861 err:
1862         dev_dbg(&intf->dev, "failed=%d\n", ret);
1863 
1864         return ret;
1865 }
1866 
1867 static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1868 {
1869         struct state *state = d_to_priv(d);
1870         struct usb_interface *intf = d->intf;
1871         int ret;
1872         u8 tmp;
1873 
1874         ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_MODE, &tmp);
1875         if (ret < 0)
1876                 goto err;
1877 
1878         dev_dbg(&intf->dev, "ir_mode=%02x\n", tmp);
1879 
1880         /* don't activate rc if in HID mode or if not available */
1881         if (tmp == 5) {
1882                 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_TYPE,
1883                                 &tmp);
1884                 if (ret < 0)
1885                         goto err;
1886 
1887                 dev_dbg(&intf->dev, "ir_type=%02x\n", tmp);
1888 
1889                 switch (tmp) {
1890                 case 0: /* NEC */
1891                 default:
1892                         rc->allowed_protos = RC_BIT_NEC;
1893                         break;
1894                 case 1: /* RC6 */
1895                         rc->allowed_protos = RC_BIT_RC6_MCE;
1896                         break;
1897                 }
1898 
1899                 rc->query = af9035_rc_query;
1900                 rc->interval = 500;
1901 
1902                 /* load empty to enable rc */
1903                 if (!rc->map_name)
1904                         rc->map_name = RC_MAP_EMPTY;
1905         }
1906 
1907         return 0;
1908 
1909 err:
1910         dev_dbg(&intf->dev, "failed=%d\n", ret);
1911 
1912         return ret;
1913 }
1914 #else
1915         #define af9035_get_rc_config NULL
1916 #endif
1917 
1918 static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1919                 struct usb_data_stream_properties *stream)
1920 {
1921         struct dvb_usb_device *d = fe_to_d(fe);
1922         struct usb_interface *intf = d->intf;
1923 
1924         dev_dbg(&intf->dev, "adap=%d\n", fe_to_adap(fe)->id);
1925 
1926         if (d->udev->speed == USB_SPEED_FULL)
1927                 stream->u.bulk.buffersize = 5 * 188;
1928 
1929         return 0;
1930 }
1931 
1932 static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1933 {
1934         struct state *state = adap_to_priv(adap);
1935 
1936         return state->ops.pid_filter_ctrl(adap->fe[0], onoff);
1937 }
1938 
1939 static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1940                 int onoff)
1941 {
1942         struct state *state = adap_to_priv(adap);
1943 
1944         return state->ops.pid_filter(adap->fe[0], index, pid, onoff);
1945 }
1946 
1947 static int af9035_probe(struct usb_interface *intf,
1948                 const struct usb_device_id *id)
1949 {
1950         struct usb_device *udev = interface_to_usbdev(intf);
1951         char manufacturer[sizeof("Afatech")];
1952 
1953         memset(manufacturer, 0, sizeof(manufacturer));
1954         usb_string(udev, udev->descriptor.iManufacturer,
1955                         manufacturer, sizeof(manufacturer));
1956         /*
1957          * There is two devices having same ID but different chipset. One uses
1958          * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1959          * is iManufacturer string.
1960          *
1961          * idVendor           0x0ccd TerraTec Electronic GmbH
1962          * idProduct          0x0099
1963          * bcdDevice            2.00
1964          * iManufacturer           1 Afatech
1965          * iProduct                2 DVB-T 2
1966          *
1967          * idVendor           0x0ccd TerraTec Electronic GmbH
1968          * idProduct          0x0099
1969          * bcdDevice            2.00
1970          * iManufacturer           1 ITE Technologies, Inc.
1971          * iProduct                2 DVB-T TV Stick
1972          */
1973         if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1974                         (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1975                 if (!strcmp("Afatech", manufacturer)) {
1976                         dev_dbg(&udev->dev, "rejecting device\n");
1977                         return -ENODEV;
1978                 }
1979         }
1980 
1981         return dvb_usbv2_probe(intf, id);
1982 }
1983 
1984 /* interface 0 is used by DVB-T receiver and
1985    interface 1 is for remote controller (HID) */
1986 static const struct dvb_usb_device_properties af9035_props = {
1987         .driver_name = KBUILD_MODNAME,
1988         .owner = THIS_MODULE,
1989         .adapter_nr = adapter_nr,
1990         .size_of_priv = sizeof(struct state),
1991 
1992         .generic_bulk_ctrl_endpoint = 0x02,
1993         .generic_bulk_ctrl_endpoint_response = 0x81,
1994 
1995         .identify_state = af9035_identify_state,
1996         .download_firmware = af9035_download_firmware,
1997 
1998         .i2c_algo = &af9035_i2c_algo,
1999         .read_config = af9035_read_config,
2000         .frontend_attach = af9035_frontend_attach,
2001         .frontend_detach = af9035_frontend_detach,
2002         .tuner_attach = af9035_tuner_attach,
2003         .tuner_detach = af9035_tuner_detach,
2004         .init = af9035_init,
2005         .get_rc_config = af9035_get_rc_config,
2006         .get_stream_config = af9035_get_stream_config,
2007 
2008         .get_adapter_count = af9035_get_adapter_count,
2009         .adapter = {
2010                 {
2011                         .caps = DVB_USB_ADAP_HAS_PID_FILTER |
2012                                 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
2013 
2014                         .pid_filter_count = 32,
2015                         .pid_filter_ctrl = af9035_pid_filter_ctrl,
2016                         .pid_filter = af9035_pid_filter,
2017 
2018                         .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
2019                 }, {
2020                         .caps = DVB_USB_ADAP_HAS_PID_FILTER |
2021                                 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
2022 
2023                         .pid_filter_count = 32,
2024                         .pid_filter_ctrl = af9035_pid_filter_ctrl,
2025                         .pid_filter = af9035_pid_filter,
2026 
2027                         .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
2028                 },
2029         },
2030 };
2031 
2032 static const struct dvb_usb_device_properties it930x_props = {
2033         .driver_name = KBUILD_MODNAME,
2034         .owner = THIS_MODULE,
2035         .adapter_nr = adapter_nr,
2036         .size_of_priv = sizeof(struct state),
2037 
2038         .generic_bulk_ctrl_endpoint = 0x02,
2039         .generic_bulk_ctrl_endpoint_response = 0x81,
2040 
2041         .identify_state = af9035_identify_state,
2042         .download_firmware = af9035_download_firmware,
2043 
2044         .i2c_algo = &af9035_i2c_algo,
2045         .read_config = af9035_read_config,
2046         .frontend_attach = it930x_frontend_attach,
2047         .frontend_detach = af9035_frontend_detach,
2048         .tuner_attach = it930x_tuner_attach,
2049         .tuner_detach = it930x_tuner_detach,
2050         .init = it930x_init,
2051         .get_stream_config = af9035_get_stream_config,
2052 
2053         .get_adapter_count = af9035_get_adapter_count,
2054         .adapter = {
2055                 {
2056                         .stream = DVB_USB_STREAM_BULK(0x84, 4, 816 * 188),
2057                 }, {
2058                         .stream = DVB_USB_STREAM_BULK(0x85, 4, 816 * 188),
2059                 },
2060         },
2061 };
2062 
2063 static const struct usb_device_id af9035_id_table[] = {
2064         /* AF9035 devices */
2065         { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
2066                 &af9035_props, "Afatech AF9035 reference design", NULL) },
2067         { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
2068                 &af9035_props, "Afatech AF9035 reference design", NULL) },
2069         { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
2070                 &af9035_props, "Afatech AF9035 reference design", NULL) },
2071         { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
2072                 &af9035_props, "Afatech AF9035 reference design", NULL) },
2073         { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
2074                 &af9035_props, "Afatech AF9035 reference design", NULL) },
2075         { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
2076                 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
2077         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
2078                 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
2079         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
2080                 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
2081         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
2082                 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2083         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
2084                 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2085         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
2086                 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
2087         { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
2088                 &af9035_props, "Asus U3100Mini Plus", NULL) },
2089         { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
2090                 &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
2091         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, 0x0337,
2092                 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2093 
2094         /* IT9135 devices */
2095         { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135,
2096                 &af9035_props, "ITE 9135 Generic", RC_MAP_IT913X_V1) },
2097         { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005,
2098                 &af9035_props, "ITE 9135(9005) Generic", RC_MAP_IT913X_V2) },
2099         { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006,
2100                 &af9035_props, "ITE 9135(9006) Generic", RC_MAP_IT913X_V1) },
2101         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_1835,
2102                 &af9035_props, "Avermedia A835B(1835)", RC_MAP_IT913X_V2) },
2103         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_2835,
2104                 &af9035_props, "Avermedia A835B(2835)", RC_MAP_IT913X_V2) },
2105         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_3835,
2106                 &af9035_props, "Avermedia A835B(3835)", RC_MAP_IT913X_V2) },
2107         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_4835,
2108                 &af9035_props, "Avermedia A835B(4835)", RC_MAP_IT913X_V2) },
2109         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TD110,
2110                 &af9035_props, "Avermedia AverTV Volar HD 2 (TD110)", RC_MAP_AVERMEDIA_RM_KS) },
2111         { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_H335,
2112                 &af9035_props, "Avermedia H335", RC_MAP_IT913X_V2) },
2113         { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09,
2114                 &af9035_props, "Kworld UB499-2T T09", RC_MAP_IT913X_V1) },
2115         { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137,
2116                 &af9035_props, "Sveon STV22 Dual DVB-T HDTV",
2117                                                         RC_MAP_IT913X_V1) },
2118         { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CTVDIGDUAL_V2,
2119                 &af9035_props, "Digital Dual TV Receiver CTVDIGDUAL_V2",
2120                                                         RC_MAP_IT913X_V1) },
2121         /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
2122         { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
2123                 &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)",
2124                 NULL) },
2125         { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a05,
2126                 &af9035_props, "Leadtek WinFast DTV Dongle Dual", NULL) },
2127         { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xf900,
2128                 &af9035_props, "Hauppauge WinTV-MiniStick 2", NULL) },
2129         { DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_78E,
2130                 &af9035_props, "PCTV AndroiDTV (78e)", RC_MAP_IT913X_V1) },
2131         { DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_79E,
2132                 &af9035_props, "PCTV microStick (79e)", RC_MAP_IT913X_V2) },
2133 
2134         /* IT930x devices */
2135         { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9303,
2136                 &it930x_props, "ITE 9303 Generic", NULL) },
2137         { }
2138 };
2139 MODULE_DEVICE_TABLE(usb, af9035_id_table);
2140 
2141 static struct usb_driver af9035_usb_driver = {
2142         .name = KBUILD_MODNAME,
2143         .id_table = af9035_id_table,
2144         .probe = af9035_probe,
2145         .disconnect = dvb_usbv2_disconnect,
2146         .suspend = dvb_usbv2_suspend,
2147         .resume = dvb_usbv2_resume,
2148         .reset_resume = dvb_usbv2_reset_resume,
2149         .no_dynamic_id = 1,
2150         .soft_unbind = 1,
2151 };
2152 
2153 module_usb_driver(af9035_usb_driver);
2154 
2155 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
2156 MODULE_DESCRIPTION("Afatech AF9035 driver");
2157 MODULE_LICENSE("GPL");
2158 MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
2159 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
2160 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);
2161 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9303);
2162 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us