Version:  2.0.40 2.2.26 2.4.37 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18

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

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