blob: 09e0f58f6bb761d9ff458c020ce13421a3bfd86e [file] [log] [blame]
Antti Palosaari80619de2008-09-15 17:18:09 -03001/*
2 * DVB USB Linux driver for Afatech AF9015 DVB-T USB2.0 receiver
3 *
4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
5 *
6 * Thanks to Afatech who kindly provided information.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 */
23
24#include "af9015.h"
Antti Palosaari80619de2008-09-15 17:18:09 -030025
Antti Palosaari349d0422008-11-05 16:31:24 -030026static int dvb_usb_af9015_remote;
Antti Palosaari80619de2008-09-15 17:18:09 -030027module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
28MODULE_PARM_DESC(remote, "select remote");
Antti Palosaari80619de2008-09-15 17:18:09 -030029DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
30
Antti Palosaaria3645e52012-06-07 20:36:35 -030031static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
Antti Palosaari80619de2008-09-15 17:18:09 -030032{
Antti Palosaari06565d72009-09-12 20:46:30 -030033#define REQ_HDR_LEN 8 /* send header size */
34#define ACK_HDR_LEN 2 /* rece header size */
Antti Palosaarie8089662012-06-16 18:13:06 -030035 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -030036 int ret, wlen, rlen;
Antti Palosaari80619de2008-09-15 17:18:09 -030037 u8 write = 1;
Antti Palosaari80619de2008-09-15 17:18:09 -030038
Antti Palosaariaff8c2d2013-02-26 13:25:19 -030039 mutex_lock(&d->usb_mutex);
40
41 state->buf[0] = req->cmd;
42 state->buf[1] = state->seq++;
43 state->buf[2] = req->i2c_addr;
44 state->buf[3] = req->addr >> 8;
45 state->buf[4] = req->addr & 0xff;
46 state->buf[5] = req->mbox;
47 state->buf[6] = req->addr_len;
48 state->buf[7] = req->data_len;
Antti Palosaari80619de2008-09-15 17:18:09 -030049
50 switch (req->cmd) {
51 case GET_CONFIG:
Antti Palosaari80619de2008-09-15 17:18:09 -030052 case READ_MEMORY:
53 case RECONNECT_USB:
Antti Palosaari80619de2008-09-15 17:18:09 -030054 write = 0;
55 break;
56 case READ_I2C:
57 write = 0;
Antti Palosaariaff8c2d2013-02-26 13:25:19 -030058 state->buf[2] |= 0x01; /* set I2C direction */
Antti Palosaari80619de2008-09-15 17:18:09 -030059 case WRITE_I2C:
Antti Palosaariaff8c2d2013-02-26 13:25:19 -030060 state->buf[0] = READ_WRITE_I2C;
Antti Palosaari80619de2008-09-15 17:18:09 -030061 break;
62 case WRITE_MEMORY:
63 if (((req->addr & 0xff00) == 0xff00) ||
Antti Palosaarif4e96de2009-09-12 21:25:59 -030064 ((req->addr & 0xff00) == 0xae00))
Antti Palosaariaff8c2d2013-02-26 13:25:19 -030065 state->buf[0] = WRITE_VIRTUAL_MEMORY;
Antti Palosaari80619de2008-09-15 17:18:09 -030066 case WRITE_VIRTUAL_MEMORY:
67 case COPY_FIRMWARE:
68 case DOWNLOAD_FIRMWARE:
Nils Kassubeba1bc642009-07-28 11:54:52 -030069 case BOOT:
Antti Palosaari80619de2008-09-15 17:18:09 -030070 break;
71 default:
Antti Palosaarif2247492012-09-12 20:23:50 -030072 dev_err(&d->udev->dev, "%s: unknown command=%d\n",
73 KBUILD_MODNAME, req->cmd);
Antti Palosaarie1e9e512012-09-12 20:23:52 -030074 ret = -EIO;
Antti Palosaaria3645e52012-06-07 20:36:35 -030075 goto error;
Antti Palosaari80619de2008-09-15 17:18:09 -030076 }
77
Antti Palosaari06565d72009-09-12 20:46:30 -030078 /* buffer overflow check */
79 if ((write && (req->data_len > BUF_LEN - REQ_HDR_LEN)) ||
Antti Palosaarif2247492012-09-12 20:23:50 -030080 (!write && (req->data_len > BUF_LEN - ACK_HDR_LEN))) {
81 dev_err(&d->udev->dev, "%s: too much data; cmd=%d len=%d\n",
82 KBUILD_MODNAME, req->cmd, req->data_len);
Antti Palosaari06565d72009-09-12 20:46:30 -030083 ret = -EINVAL;
Antti Palosaaria3645e52012-06-07 20:36:35 -030084 goto error;
Antti Palosaari06565d72009-09-12 20:46:30 -030085 }
86
Antti Palosaari06565d72009-09-12 20:46:30 -030087 /* write receives seq + status = 2 bytes
88 read receives seq + status + data = 2 + N bytes */
Antti Palosaaria3645e52012-06-07 20:36:35 -030089 wlen = REQ_HDR_LEN;
90 rlen = ACK_HDR_LEN;
91 if (write) {
92 wlen += req->data_len;
Antti Palosaariaff8c2d2013-02-26 13:25:19 -030093 memcpy(&state->buf[REQ_HDR_LEN], req->data, req->data_len);
Antti Palosaaria3645e52012-06-07 20:36:35 -030094 } else {
95 rlen += req->data_len;
Antti Palosaari80619de2008-09-15 17:18:09 -030096 }
97
Antti Palosaaria3645e52012-06-07 20:36:35 -030098 /* no ack for these packets */
99 if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
100 rlen = 0;
101
Antti Palosaariaff8c2d2013-02-26 13:25:19 -0300102 ret = dvb_usbv2_generic_rw_locked(d,
103 state->buf, wlen, state->buf, rlen);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300104 if (ret)
105 goto error;
Antti Palosaari80619de2008-09-15 17:18:09 -0300106
Antti Palosaari80619de2008-09-15 17:18:09 -0300107 /* check status */
Antti Palosaariaff8c2d2013-02-26 13:25:19 -0300108 if (rlen && state->buf[1]) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300109 dev_err(&d->udev->dev, "%s: command failed=%d\n",
Antti Palosaariaff8c2d2013-02-26 13:25:19 -0300110 KBUILD_MODNAME, state->buf[1]);
Antti Palosaarie1e9e512012-09-12 20:23:52 -0300111 ret = -EIO;
Antti Palosaaria3645e52012-06-07 20:36:35 -0300112 goto error;
Antti Palosaari80619de2008-09-15 17:18:09 -0300113 }
114
115 /* read request, copy returned data to return buf */
116 if (!write)
Antti Palosaariaff8c2d2013-02-26 13:25:19 -0300117 memcpy(req->data, &state->buf[ACK_HDR_LEN], req->data_len);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300118error:
Antti Palosaariaff8c2d2013-02-26 13:25:19 -0300119 mutex_unlock(&d->usb_mutex);
120
Antti Palosaari80619de2008-09-15 17:18:09 -0300121 return ret;
122}
123
Antti Palosaari80619de2008-09-15 17:18:09 -0300124static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
125 u8 len)
126{
127 struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
128 val};
129 return af9015_ctrl_msg(d, &req);
130}
131
Antti Palosaari74c8e3a2010-10-22 18:45:18 -0300132static int af9015_read_regs(struct dvb_usb_device *d, u16 addr, u8 *val, u8 len)
133{
134 struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
135 val};
136 return af9015_ctrl_msg(d, &req);
137}
138
Antti Palosaaria3645e52012-06-07 20:36:35 -0300139static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
140{
141 return af9015_write_regs(d, addr, &val, 1);
142}
143
Antti Palosaari80619de2008-09-15 17:18:09 -0300144static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
145{
Antti Palosaari74c8e3a2010-10-22 18:45:18 -0300146 return af9015_read_regs(d, addr, val, 1);
Antti Palosaari80619de2008-09-15 17:18:09 -0300147}
148
149static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
150 u8 val)
151{
Antti Palosaarie8089662012-06-16 18:13:06 -0300152 struct af9015_state *state = d_to_priv(d);
Antti Palosaari80619de2008-09-15 17:18:09 -0300153 struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
154
Antti Palosaaria3645e52012-06-07 20:36:35 -0300155 if (addr == state->af9013_config[0].i2c_addr ||
156 addr == state->af9013_config[1].i2c_addr)
Antti Palosaari80619de2008-09-15 17:18:09 -0300157 req.addr_len = 3;
158
159 return af9015_ctrl_msg(d, &req);
160}
161
162static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
163 u8 *val)
164{
Antti Palosaarie8089662012-06-16 18:13:06 -0300165 struct af9015_state *state = d_to_priv(d);
Antti Palosaari80619de2008-09-15 17:18:09 -0300166 struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
167
Antti Palosaaria3645e52012-06-07 20:36:35 -0300168 if (addr == state->af9013_config[0].i2c_addr ||
169 addr == state->af9013_config[1].i2c_addr)
Antti Palosaari80619de2008-09-15 17:18:09 -0300170 req.addr_len = 3;
171
172 return af9015_ctrl_msg(d, &req);
173}
174
Antti Palosaaria3645e52012-06-07 20:36:35 -0300175static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
176{
177 int ret;
178 u8 val, mask = 0x01;
179
180 ret = af9015_read_reg(d, addr, &val);
181 if (ret)
182 return ret;
183
184 mask <<= bit;
185 if (op) {
186 /* set bit */
187 val |= mask;
188 } else {
189 /* clear bit */
190 mask ^= 0xff;
191 val &= mask;
192 }
193
194 return af9015_write_reg(d, addr, val);
195}
196
197static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
198{
199 return af9015_do_reg_bit(d, addr, bit, 1);
200}
201
202static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
203{
204 return af9015_do_reg_bit(d, addr, bit, 0);
205}
206
Antti Palosaari80619de2008-09-15 17:18:09 -0300207static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
208 int num)
209{
210 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Antti Palosaarie8089662012-06-16 18:13:06 -0300211 struct af9015_state *state = d_to_priv(d);
Antti Palosaari80619de2008-09-15 17:18:09 -0300212 int ret = 0, i = 0;
213 u16 addr;
Antti Palosaari675375d2010-10-07 21:46:41 -0300214 u8 uninitialized_var(mbox), addr_len;
Antti Palosaari80619de2008-09-15 17:18:09 -0300215 struct req_t req;
216
Antti Palosaaribc050e62012-05-08 06:04:24 -0300217/*
Antti Palosaari80619de2008-09-15 17:18:09 -0300218The bus lock is needed because there is two tuners both using same I2C-address.
219Due to that the only way to select correct tuner is use demodulator I2C-gate.
220
221................................................
222. AF9015 includes integrated AF9013 demodulator.
223. ____________ ____________ . ____________
224.| uC | | demod | . | tuner |
225.|------------| |------------| . |------------|
226.| AF9015 | | AF9013/5 | . | MXL5003 |
227.| |--+----I2C-------|-----/ -----|-.-----I2C-------| |
228.| | | | addr 0x38 | . | addr 0xc6 |
229.|____________| | |____________| . |____________|
230.................|..............................
231 | ____________ ____________
232 | | demod | | tuner |
233 | |------------| |------------|
234 | | AF9013 | | MXL5003 |
235 +----I2C-------|-----/ -----|-------I2C-------| |
236 | addr 0x3a | | addr 0xc6 |
237 |____________| |____________|
238*/
239 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
240 return -EAGAIN;
241
242 while (i < num) {
Antti Palosaaria3645e52012-06-07 20:36:35 -0300243 if (msg[i].addr == state->af9013_config[0].i2c_addr ||
244 msg[i].addr == state->af9013_config[1].i2c_addr) {
Antti Palosaari80619de2008-09-15 17:18:09 -0300245 addr = msg[i].buf[0] << 8;
246 addr += msg[i].buf[1];
247 mbox = msg[i].buf[2];
248 addr_len = 3;
249 } else {
250 addr = msg[i].buf[0];
251 addr_len = 1;
Antti Palosaari675375d2010-10-07 21:46:41 -0300252 /* mbox is don't care in that case */
Antti Palosaari80619de2008-09-15 17:18:09 -0300253 }
254
255 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
Antti Palosaari709d9202011-06-17 21:16:38 -0300256 if (msg[i].len > 3 || msg[i+1].len > 61) {
257 ret = -EOPNOTSUPP;
258 goto error;
259 }
Antti Palosaaria3645e52012-06-07 20:36:35 -0300260 if (msg[i].addr == state->af9013_config[0].i2c_addr)
Antti Palosaari80619de2008-09-15 17:18:09 -0300261 req.cmd = READ_MEMORY;
262 else
263 req.cmd = READ_I2C;
264 req.i2c_addr = msg[i].addr;
265 req.addr = addr;
266 req.mbox = mbox;
267 req.addr_len = addr_len;
268 req.data_len = msg[i+1].len;
269 req.data = &msg[i+1].buf[0];
270 ret = af9015_ctrl_msg(d, &req);
271 i += 2;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300272 } else if (msg[i].flags & I2C_M_RD) {
Antti Palosaari709d9202011-06-17 21:16:38 -0300273 if (msg[i].len > 61) {
274 ret = -EOPNOTSUPP;
275 goto error;
276 }
Antti Palosaaria3645e52012-06-07 20:36:35 -0300277 if (msg[i].addr == state->af9013_config[0].i2c_addr) {
Antti Palosaari16b2dc22011-06-16 20:02:41 -0300278 ret = -EINVAL;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300279 goto error;
Antti Palosaari16b2dc22011-06-16 20:02:41 -0300280 }
281 req.cmd = READ_I2C;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300282 req.i2c_addr = msg[i].addr;
283 req.addr = addr;
284 req.mbox = mbox;
285 req.addr_len = addr_len;
286 req.data_len = msg[i].len;
287 req.data = &msg[i].buf[0];
288 ret = af9015_ctrl_msg(d, &req);
289 i += 1;
Antti Palosaari80619de2008-09-15 17:18:09 -0300290 } else {
Antti Palosaari709d9202011-06-17 21:16:38 -0300291 if (msg[i].len > 21) {
292 ret = -EOPNOTSUPP;
293 goto error;
294 }
Antti Palosaaria3645e52012-06-07 20:36:35 -0300295 if (msg[i].addr == state->af9013_config[0].i2c_addr)
Antti Palosaari80619de2008-09-15 17:18:09 -0300296 req.cmd = WRITE_MEMORY;
297 else
298 req.cmd = WRITE_I2C;
299 req.i2c_addr = msg[i].addr;
300 req.addr = addr;
301 req.mbox = mbox;
302 req.addr_len = addr_len;
303 req.data_len = msg[i].len-addr_len;
304 req.data = &msg[i].buf[addr_len];
305 ret = af9015_ctrl_msg(d, &req);
306 i += 1;
307 }
308 if (ret)
309 goto error;
310
311 }
312 ret = i;
313
314error:
315 mutex_unlock(&d->i2c_mutex);
316
317 return ret;
318}
319
320static u32 af9015_i2c_func(struct i2c_adapter *adapter)
321{
322 return I2C_FUNC_I2C;
323}
324
325static struct i2c_algorithm af9015_i2c_algo = {
326 .master_xfer = af9015_i2c_xfer,
327 .functionality = af9015_i2c_func,
328};
329
Antti Palosaaria0921af2012-06-18 23:42:53 -0300330static int af9015_identify_state(struct dvb_usb_device *d, const char **name)
Antti Palosaari80619de2008-09-15 17:18:09 -0300331{
332 int ret;
Antti Palosaaria3645e52012-06-07 20:36:35 -0300333 u8 reply;
334 struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
Antti Palosaari80619de2008-09-15 17:18:09 -0300335
Antti Palosaaria3645e52012-06-07 20:36:35 -0300336 ret = af9015_ctrl_msg(d, &req);
Antti Palosaari80619de2008-09-15 17:18:09 -0300337 if (ret)
338 return ret;
339
Antti Palosaarif2247492012-09-12 20:23:50 -0300340 dev_dbg(&d->udev->dev, "%s: reply=%02x\n", __func__, reply);
341
Antti Palosaaria3645e52012-06-07 20:36:35 -0300342 if (reply == 0x02)
343 ret = WARM;
Antti Palosaari80619de2008-09-15 17:18:09 -0300344 else
Antti Palosaaria3645e52012-06-07 20:36:35 -0300345 ret = COLD;
346
347 return ret;
348}
349
350static int af9015_download_firmware(struct dvb_usb_device *d,
351 const struct firmware *fw)
352{
Antti Palosaarie8089662012-06-16 18:13:06 -0300353 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300354 int i, len, remaining, ret;
355 struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
356 u16 checksum = 0;
Antti Palosaarif2247492012-09-12 20:23:50 -0300357 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300358
359 /* calc checksum */
360 for (i = 0; i < fw->size; i++)
361 checksum += fw->data[i];
362
363 state->firmware_size = fw->size;
364 state->firmware_checksum = checksum;
365
366 #define FW_ADDR 0x5100 /* firmware start address */
367 #define LEN_MAX 55 /* max packet size */
368 for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
369 len = remaining;
370 if (len > LEN_MAX)
371 len = LEN_MAX;
372
373 req.data_len = len;
374 req.data = (u8 *) &fw->data[fw->size - remaining];
375 req.addr = FW_ADDR + fw->size - remaining;
376
377 ret = af9015_ctrl_msg(d, &req);
378 if (ret) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300379 dev_err(&d->udev->dev,
380 "%s: firmware download failed=%d\n",
381 KBUILD_MODNAME, ret);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300382 goto error;
383 }
384 }
385
386 /* firmware loaded, request boot */
387 req.cmd = BOOT;
388 req.data_len = 0;
389 ret = af9015_ctrl_msg(d, &req);
390 if (ret) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300391 dev_err(&d->udev->dev, "%s: firmware boot failed=%d\n",
392 KBUILD_MODNAME, ret);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300393 goto error;
394 }
395
396error:
397 return ret;
398}
399
Mauro Carvalho Chehab65e2f1c2013-11-02 07:52:04 -0300400#define AF9015_EEPROM_SIZE 256
George Spelvinef703f42016-05-26 23:00:23 -0400401/* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
402#define GOLDEN_RATIO_PRIME_32 0x9e370001UL
Mauro Carvalho Chehab65e2f1c2013-11-02 07:52:04 -0300403
Antti Palosaaria3645e52012-06-07 20:36:35 -0300404/* hash (and dump) eeprom */
405static int af9015_eeprom_hash(struct dvb_usb_device *d)
406{
Antti Palosaarie8089662012-06-16 18:13:06 -0300407 struct af9015_state *state = d_to_priv(d);
Antti Palosaari2e35c662012-09-12 20:23:51 -0300408 int ret, i;
Antti Palosaari2e35c662012-09-12 20:23:51 -0300409 u8 buf[AF9015_EEPROM_SIZE];
410 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, NULL};
Antti Palosaaria3645e52012-06-07 20:36:35 -0300411
Antti Palosaari2e35c662012-09-12 20:23:51 -0300412 /* read eeprom */
413 for (i = 0; i < AF9015_EEPROM_SIZE; i++) {
414 req.addr = i;
415 req.data = &buf[i];
Antti Palosaaria3645e52012-06-07 20:36:35 -0300416 ret = af9015_ctrl_msg(d, &req);
Antti Palosaari2e35c662012-09-12 20:23:51 -0300417 if (ret < 0)
418 goto err;
Antti Palosaaria3645e52012-06-07 20:36:35 -0300419 }
420
Antti Palosaari2e35c662012-09-12 20:23:51 -0300421 /* calculate checksum */
422 for (i = 0; i < AF9015_EEPROM_SIZE / sizeof(u32); i++) {
Antti Palosaaria3645e52012-06-07 20:36:35 -0300423 state->eeprom_sum *= GOLDEN_RATIO_PRIME_32;
Hans Verkuil74426322014-08-20 16:34:27 -0300424 state->eeprom_sum += le32_to_cpu(((__le32 *)buf)[i]);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300425 }
426
Antti Palosaari2e35c662012-09-12 20:23:51 -0300427 for (i = 0; i < AF9015_EEPROM_SIZE; i += 16)
428 dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 16, buf + i);
429
Antti Palosaarif2247492012-09-12 20:23:50 -0300430 dev_dbg(&d->udev->dev, "%s: eeprom sum=%.8x\n",
431 __func__, state->eeprom_sum);
Antti Palosaari2e35c662012-09-12 20:23:51 -0300432 return 0;
433err:
434 dev_err(&d->udev->dev, "%s: eeprom failed=%d\n", KBUILD_MODNAME, ret);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300435 return ret;
436}
437
438static int af9015_read_config(struct dvb_usb_device *d)
439{
Antti Palosaarie8089662012-06-16 18:13:06 -0300440 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300441 int ret;
442 u8 val, i, offset = 0;
443 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
444
Antti Palosaarif2247492012-09-12 20:23:50 -0300445 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300446
447 /* IR remote controller */
448 req.addr = AF9015_EEPROM_IR_MODE;
449 /* first message will timeout often due to possible hw bug */
450 for (i = 0; i < 4; i++) {
451 ret = af9015_ctrl_msg(d, &req);
452 if (!ret)
453 break;
454 }
455 if (ret)
456 goto error;
457
458 ret = af9015_eeprom_hash(d);
459 if (ret)
460 goto error;
461
Antti Palosaaria3645e52012-06-07 20:36:35 -0300462 state->ir_mode = val;
Antti Palosaarif2247492012-09-12 20:23:50 -0300463 dev_dbg(&d->udev->dev, "%s: IR mode=%d\n", __func__, val);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300464
465 /* TS mode - one or two receivers */
466 req.addr = AF9015_EEPROM_TS_MODE;
467 ret = af9015_ctrl_msg(d, &req);
468 if (ret)
469 goto error;
470
471 state->dual_mode = val;
Antti Palosaarif2247492012-09-12 20:23:50 -0300472 dev_dbg(&d->udev->dev, "%s: TS mode=%d\n", __func__, state->dual_mode);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300473
474 /* disable 2nd adapter because we don't have PID-filters */
475 if (d->udev->speed == USB_SPEED_FULL)
476 state->dual_mode = 0;
477
478 if (state->dual_mode) {
479 /* read 2nd demodulator I2C address */
480 req.addr = AF9015_EEPROM_DEMOD2_I2C;
481 ret = af9015_ctrl_msg(d, &req);
482 if (ret)
483 goto error;
484
485 state->af9013_config[1].i2c_addr = val;
486 }
487
488 for (i = 0; i < state->dual_mode + 1; i++) {
489 if (i == 1)
490 offset = AF9015_EEPROM_OFFSET;
491 /* xtal */
492 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
493 ret = af9015_ctrl_msg(d, &req);
494 if (ret)
495 goto error;
496 switch (val) {
497 case 0:
498 state->af9013_config[i].clock = 28800000;
499 break;
500 case 1:
501 state->af9013_config[i].clock = 20480000;
502 break;
503 case 2:
504 state->af9013_config[i].clock = 28000000;
505 break;
506 case 3:
507 state->af9013_config[i].clock = 25000000;
508 break;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300509 }
Antti Palosaarif2247492012-09-12 20:23:50 -0300510 dev_dbg(&d->udev->dev, "%s: [%d] xtal=%d set clock=%d\n",
511 __func__, i, val,
512 state->af9013_config[i].clock);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300513
514 /* IF frequency */
515 req.addr = AF9015_EEPROM_IF1H + offset;
516 ret = af9015_ctrl_msg(d, &req);
517 if (ret)
518 goto error;
519
520 state->af9013_config[i].if_frequency = val << 8;
521
522 req.addr = AF9015_EEPROM_IF1L + offset;
523 ret = af9015_ctrl_msg(d, &req);
524 if (ret)
525 goto error;
526
527 state->af9013_config[i].if_frequency += val;
528 state->af9013_config[i].if_frequency *= 1000;
Antti Palosaarif2247492012-09-12 20:23:50 -0300529 dev_dbg(&d->udev->dev, "%s: [%d] IF frequency=%d\n", __func__,
530 i, state->af9013_config[i].if_frequency);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300531
532 /* MT2060 IF1 */
533 req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
534 ret = af9015_ctrl_msg(d, &req);
535 if (ret)
536 goto error;
537 state->mt2060_if1[i] = val << 8;
538 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
539 ret = af9015_ctrl_msg(d, &req);
540 if (ret)
541 goto error;
542 state->mt2060_if1[i] += val;
Antti Palosaarif2247492012-09-12 20:23:50 -0300543 dev_dbg(&d->udev->dev, "%s: [%d] MT2060 IF1=%d\n", __func__, i,
Antti Palosaaria3645e52012-06-07 20:36:35 -0300544 state->mt2060_if1[i]);
545
546 /* tuner */
547 req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
548 ret = af9015_ctrl_msg(d, &req);
549 if (ret)
550 goto error;
551 switch (val) {
552 case AF9013_TUNER_ENV77H11D5:
553 case AF9013_TUNER_MT2060:
554 case AF9013_TUNER_QT1010:
555 case AF9013_TUNER_UNKNOWN:
556 case AF9013_TUNER_MT2060_2:
557 case AF9013_TUNER_TDA18271:
558 case AF9013_TUNER_QT1010A:
559 case AF9013_TUNER_TDA18218:
560 state->af9013_config[i].spec_inv = 1;
561 break;
562 case AF9013_TUNER_MXL5003D:
563 case AF9013_TUNER_MXL5005D:
564 case AF9013_TUNER_MXL5005R:
565 case AF9013_TUNER_MXL5007T:
566 state->af9013_config[i].spec_inv = 0;
567 break;
568 case AF9013_TUNER_MC44S803:
569 state->af9013_config[i].gpio[1] = AF9013_GPIO_LO;
570 state->af9013_config[i].spec_inv = 1;
571 break;
572 default:
Antti Palosaarif2247492012-09-12 20:23:50 -0300573 dev_err(&d->udev->dev, "%s: tuner id=%d not " \
574 "supported, please report!\n",
575 KBUILD_MODNAME, val);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300576 return -ENODEV;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300577 }
Antti Palosaaria3645e52012-06-07 20:36:35 -0300578
579 state->af9013_config[i].tuner = val;
Antti Palosaarif2247492012-09-12 20:23:50 -0300580 dev_dbg(&d->udev->dev, "%s: [%d] tuner id=%d\n",
581 __func__, i, val);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300582 }
Antti Palosaari1e8750c2011-03-18 19:36:42 -0300583
Antti Palosaari80619de2008-09-15 17:18:09 -0300584error:
585 if (ret)
Antti Palosaarif2247492012-09-12 20:23:50 -0300586 dev_err(&d->udev->dev, "%s: eeprom read failed=%d\n",
587 KBUILD_MODNAME, ret);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300588
589 /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
590 content :-( Override some wrong values here. Ditto for the
591 AVerTV Red HD+ (A850T) device. */
592 if (le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
593 ((le16_to_cpu(d->udev->descriptor.idProduct) ==
594 USB_PID_AVERMEDIA_A850) ||
595 (le16_to_cpu(d->udev->descriptor.idProduct) ==
596 USB_PID_AVERMEDIA_A850T))) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300597 dev_dbg(&d->udev->dev,
598 "%s: AverMedia A850: overriding config\n",
599 __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300600 /* disable dual mode */
601 state->dual_mode = 0;
602
603 /* set correct IF */
604 state->af9013_config[0].if_frequency = 4570000;
605 }
606
607 return ret;
608}
609
Antti Palosaarib905a2a2012-06-18 22:54:16 -0300610static int af9015_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
Antti Palosaaria3645e52012-06-07 20:36:35 -0300611 struct usb_data_stream_properties *stream)
612{
Antti Palosaarif2247492012-09-12 20:23:50 -0300613 struct dvb_usb_device *d = fe_to_d(fe);
614 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300615
Antti Palosaarif2247492012-09-12 20:23:50 -0300616 if (d->udev->speed == USB_SPEED_FULL)
Antti Palosaaria3645e52012-06-07 20:36:35 -0300617 stream->u.bulk.buffersize = TS_USB11_FRAME_SIZE;
618
619 return 0;
620}
621
622static int af9015_get_adapter_count(struct dvb_usb_device *d)
623{
Antti Palosaarie8089662012-06-16 18:13:06 -0300624 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300625 return state->dual_mode + 1;
626}
627
628/* override demod callbacks for resource locking */
629static int af9015_af9013_set_frontend(struct dvb_frontend *fe)
630{
631 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300632 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300633
634 if (mutex_lock_interruptible(&state->fe_mutex))
635 return -EAGAIN;
636
Antti Palosaarie8089662012-06-16 18:13:06 -0300637 ret = state->set_frontend[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300638
639 mutex_unlock(&state->fe_mutex);
640
641 return ret;
642}
643
644/* override demod callbacks for resource locking */
645static int af9015_af9013_read_status(struct dvb_frontend *fe,
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300646 enum fe_status *status)
Antti Palosaaria3645e52012-06-07 20:36:35 -0300647{
648 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300649 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300650
651 if (mutex_lock_interruptible(&state->fe_mutex))
652 return -EAGAIN;
653
Antti Palosaarie8089662012-06-16 18:13:06 -0300654 ret = state->read_status[fe_to_adap(fe)->id](fe, status);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300655
656 mutex_unlock(&state->fe_mutex);
657
658 return ret;
659}
660
661/* override demod callbacks for resource locking */
662static int af9015_af9013_init(struct dvb_frontend *fe)
663{
664 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300665 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300666
667 if (mutex_lock_interruptible(&state->fe_mutex))
668 return -EAGAIN;
669
Antti Palosaarie8089662012-06-16 18:13:06 -0300670 ret = state->init[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300671
672 mutex_unlock(&state->fe_mutex);
673
674 return ret;
675}
676
677/* override demod callbacks for resource locking */
678static int af9015_af9013_sleep(struct dvb_frontend *fe)
679{
680 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300681 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300682
683 if (mutex_lock_interruptible(&state->fe_mutex))
684 return -EAGAIN;
685
Antti Palosaarie8089662012-06-16 18:13:06 -0300686 ret = state->sleep[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300687
688 mutex_unlock(&state->fe_mutex);
689
690 return ret;
691}
692
693/* override tuner callbacks for resource locking */
694static int af9015_tuner_init(struct dvb_frontend *fe)
695{
696 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300697 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300698
699 if (mutex_lock_interruptible(&state->fe_mutex))
700 return -EAGAIN;
701
Antti Palosaarie8089662012-06-16 18:13:06 -0300702 ret = state->tuner_init[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300703
704 mutex_unlock(&state->fe_mutex);
705
706 return ret;
707}
708
709/* override tuner callbacks for resource locking */
710static int af9015_tuner_sleep(struct dvb_frontend *fe)
711{
712 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300713 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300714
715 if (mutex_lock_interruptible(&state->fe_mutex))
716 return -EAGAIN;
717
Antti Palosaarie8089662012-06-16 18:13:06 -0300718 ret = state->tuner_sleep[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300719
720 mutex_unlock(&state->fe_mutex);
721
Antti Palosaari80619de2008-09-15 17:18:09 -0300722 return ret;
723}
724
725static int af9015_copy_firmware(struct dvb_usb_device *d)
726{
Antti Palosaarie8089662012-06-16 18:13:06 -0300727 struct af9015_state *state = d_to_priv(d);
Antti Palosaari80619de2008-09-15 17:18:09 -0300728 int ret;
729 u8 fw_params[4];
730 u8 val, i;
731 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
732 fw_params };
Antti Palosaarif2247492012-09-12 20:23:50 -0300733 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -0300734
Antti Palosaaria3645e52012-06-07 20:36:35 -0300735 fw_params[0] = state->firmware_size >> 8;
736 fw_params[1] = state->firmware_size & 0xff;
737 fw_params[2] = state->firmware_checksum >> 8;
738 fw_params[3] = state->firmware_checksum & 0xff;
Antti Palosaari80619de2008-09-15 17:18:09 -0300739
740 /* wait 2nd demodulator ready */
741 msleep(100);
742
Antti Palosaaria3645e52012-06-07 20:36:35 -0300743 ret = af9015_read_reg_i2c(d, state->af9013_config[1].i2c_addr,
744 0x98be, &val);
Antti Palosaari80619de2008-09-15 17:18:09 -0300745 if (ret)
746 goto error;
747 else
Antti Palosaarif2247492012-09-12 20:23:50 -0300748 dev_dbg(&d->udev->dev, "%s: firmware status=%02x\n",
749 __func__, val);
Antti Palosaari80619de2008-09-15 17:18:09 -0300750
751 if (val == 0x0c) /* fw is running, no need for download */
752 goto exit;
753
754 /* set I2C master clock to fast (to speed up firmware copy) */
755 ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
756 if (ret)
757 goto error;
758
759 msleep(50);
760
761 /* copy firmware */
762 ret = af9015_ctrl_msg(d, &req);
763 if (ret)
Antti Palosaarif2247492012-09-12 20:23:50 -0300764 dev_err(&d->udev->dev, "%s: firmware copy cmd failed=%d\n",
765 KBUILD_MODNAME, ret);
766
767 dev_dbg(&d->udev->dev, "%s: firmware copy done\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -0300768
769 /* set I2C master clock back to normal */
770 ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
771 if (ret)
772 goto error;
773
774 /* request boot firmware */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300775 ret = af9015_write_reg_i2c(d, state->af9013_config[1].i2c_addr,
776 0xe205, 1);
Antti Palosaarif2247492012-09-12 20:23:50 -0300777 dev_dbg(&d->udev->dev, "%s: firmware boot cmd status=%d\n",
778 __func__, ret);
Antti Palosaari80619de2008-09-15 17:18:09 -0300779 if (ret)
780 goto error;
781
782 for (i = 0; i < 15; i++) {
783 msleep(100);
784
785 /* check firmware status */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300786 ret = af9015_read_reg_i2c(d, state->af9013_config[1].i2c_addr,
787 0x98be, &val);
Antti Palosaarif2247492012-09-12 20:23:50 -0300788 dev_dbg(&d->udev->dev, "%s: firmware status cmd status=%d " \
789 "firmware status=%02x\n", __func__, ret, val);
Antti Palosaari80619de2008-09-15 17:18:09 -0300790 if (ret)
791 goto error;
792
793 if (val == 0x0c || val == 0x04) /* success or fail */
794 break;
795 }
796
797 if (val == 0x04) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300798 dev_err(&d->udev->dev, "%s: firmware did not run\n",
799 KBUILD_MODNAME);
Antti Palosaarie1e9e512012-09-12 20:23:52 -0300800 ret = -ETIMEDOUT;
Antti Palosaari80619de2008-09-15 17:18:09 -0300801 } else if (val != 0x0c) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300802 dev_err(&d->udev->dev, "%s: firmware boot timeout\n",
803 KBUILD_MODNAME);
Antti Palosaarie1e9e512012-09-12 20:23:52 -0300804 ret = -ETIMEDOUT;
Antti Palosaari80619de2008-09-15 17:18:09 -0300805 }
806
807error:
808exit:
809 return ret;
810}
811
Antti Palosaari80619de2008-09-15 17:18:09 -0300812static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
813{
814 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300815 struct af9015_state *state = adap_to_priv(adap);
Antti Palosaari80619de2008-09-15 17:18:09 -0300816
Antti Palosaaria3645e52012-06-07 20:36:35 -0300817 if (adap->id == 0) {
818 state->af9013_config[0].ts_mode = AF9013_TS_USB;
819 memcpy(state->af9013_config[0].api_version, "\x0\x1\x9\x0", 4);
820 state->af9013_config[0].gpio[0] = AF9013_GPIO_HI;
821 state->af9013_config[0].gpio[3] = AF9013_GPIO_TUNER_ON;
822 } else if (adap->id == 1) {
823 state->af9013_config[1].ts_mode = AF9013_TS_SERIAL;
824 memcpy(state->af9013_config[1].api_version, "\x0\x1\x9\x0", 4);
825 state->af9013_config[1].gpio[0] = AF9013_GPIO_TUNER_ON;
826 state->af9013_config[1].gpio[1] = AF9013_GPIO_LO;
827
Antti Palosaari80619de2008-09-15 17:18:09 -0300828 /* copy firmware to 2nd demodulator */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300829 if (state->dual_mode) {
Antti Palosaarie8089662012-06-16 18:13:06 -0300830 ret = af9015_copy_firmware(adap_to_d(adap));
Antti Palosaari80619de2008-09-15 17:18:09 -0300831 if (ret) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300832 dev_err(&adap_to_d(adap)->udev->dev,
833 "%s: firmware copy to 2nd " \
834 "frontend failed, will " \
835 "disable it\n", KBUILD_MODNAME);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300836 state->dual_mode = 0;
Antti Palosaari80619de2008-09-15 17:18:09 -0300837 return -ENODEV;
838 }
839 } else {
840 return -ENODEV;
841 }
842 }
843
844 /* attach demodulator */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300845 adap->fe[0] = dvb_attach(af9013_attach,
Antti Palosaarie8089662012-06-16 18:13:06 -0300846 &state->af9013_config[adap->id], &adap_to_d(adap)->i2c_adap);
Antti Palosaari80619de2008-09-15 17:18:09 -0300847
Antti Palosaarie90ab842011-11-12 22:33:30 -0300848 /*
849 * AF9015 firmware does not like if it gets interrupted by I2C adapter
850 * request on some critical phases. During normal operation I2C adapter
851 * is used only 2nd demodulator and tuner on dual tuner devices.
852 * Override demodulator callbacks and use mutex for limit access to
853 * those "critical" paths to keep AF9015 happy.
Antti Palosaarie90ab842011-11-12 22:33:30 -0300854 */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300855 if (adap->fe[0]) {
Antti Palosaarie90ab842011-11-12 22:33:30 -0300856 state->set_frontend[adap->id] =
Antti Palosaaria3645e52012-06-07 20:36:35 -0300857 adap->fe[0]->ops.set_frontend;
858 adap->fe[0]->ops.set_frontend =
Antti Palosaarie90ab842011-11-12 22:33:30 -0300859 af9015_af9013_set_frontend;
860
861 state->read_status[adap->id] =
Antti Palosaaria3645e52012-06-07 20:36:35 -0300862 adap->fe[0]->ops.read_status;
863 adap->fe[0]->ops.read_status =
Antti Palosaarie90ab842011-11-12 22:33:30 -0300864 af9015_af9013_read_status;
865
Antti Palosaaria3645e52012-06-07 20:36:35 -0300866 state->init[adap->id] = adap->fe[0]->ops.init;
867 adap->fe[0]->ops.init = af9015_af9013_init;
Antti Palosaarie90ab842011-11-12 22:33:30 -0300868
Antti Palosaaria3645e52012-06-07 20:36:35 -0300869 state->sleep[adap->id] = adap->fe[0]->ops.sleep;
870 adap->fe[0]->ops.sleep = af9015_af9013_sleep;
Antti Palosaarie90ab842011-11-12 22:33:30 -0300871 }
872
Antti Palosaaria3645e52012-06-07 20:36:35 -0300873 return adap->fe[0] == NULL ? -ENODEV : 0;
Antti Palosaari80619de2008-09-15 17:18:09 -0300874}
875
876static struct mt2060_config af9015_mt2060_config = {
877 .i2c_address = 0xc0,
878 .clock_out = 0,
879};
880
881static struct qt1010_config af9015_qt1010_config = {
882 .i2c_address = 0xc4,
883};
884
885static struct tda18271_config af9015_tda18271_config = {
886 .gate = TDA18271_GATE_DIGITAL,
Mauro Carvalho Chehab7655e592010-10-27 14:55:34 -0200887 .small_i2c = TDA18271_16_BYTE_CHUNK_INIT,
Antti Palosaari80619de2008-09-15 17:18:09 -0300888};
889
890static struct mxl5005s_config af9015_mxl5003_config = {
891 .i2c_address = 0xc6,
892 .if_freq = IF_FREQ_4570000HZ,
893 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
894 .agc_mode = MXL_SINGLE_AGC,
895 .tracking_filter = MXL_TF_DEFAULT,
Antti Palosaaria1310772008-09-22 13:59:25 -0300896 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -0300897 .cap_select = MXL_CAP_SEL_ENABLE,
898 .div_out = MXL_DIV_OUT_4,
899 .clock_out = MXL_CLOCK_OUT_DISABLE,
900 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
901 .top = MXL5005S_TOP_25P2,
902 .mod_mode = MXL_DIGITAL_MODE,
903 .if_mode = MXL_ZERO_IF,
904 .AgcMasterByte = 0x00,
905};
906
907static struct mxl5005s_config af9015_mxl5005_config = {
908 .i2c_address = 0xc6,
909 .if_freq = IF_FREQ_4570000HZ,
910 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
911 .agc_mode = MXL_SINGLE_AGC,
912 .tracking_filter = MXL_TF_OFF,
Antti Palosaaria1310772008-09-22 13:59:25 -0300913 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -0300914 .cap_select = MXL_CAP_SEL_ENABLE,
915 .div_out = MXL_DIV_OUT_4,
916 .clock_out = MXL_CLOCK_OUT_DISABLE,
917 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
918 .top = MXL5005S_TOP_25P2,
919 .mod_mode = MXL_DIGITAL_MODE,
920 .if_mode = MXL_ZERO_IF,
921 .AgcMasterByte = 0x00,
922};
923
Jochen Friedrichd5633992009-02-02 14:59:50 -0300924static struct mc44s803_config af9015_mc44s803_config = {
925 .i2c_address = 0xc0,
926 .dig_out = 1,
927};
928
Antti Palosaariee3d4402010-08-13 03:51:26 -0300929static struct tda18218_config af9015_tda18218_config = {
930 .i2c_address = 0xc0,
931 .i2c_wr_max = 21, /* max wr bytes AF9015 I2C adap can handle at once */
932};
933
Antti Palosaariab07fdd2010-09-09 14:59:10 -0300934static struct mxl5007t_config af9015_mxl5007t_config = {
935 .xtal_freq_hz = MxL_XTAL_24_MHZ,
936 .if_freq_hz = MxL_IF_4_57_MHZ,
937};
938
Antti Palosaari80619de2008-09-15 17:18:09 -0300939static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
940{
Antti Palosaarif2247492012-09-12 20:23:50 -0300941 struct dvb_usb_device *d = adap_to_d(adap);
942 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300943 int ret;
Antti Palosaarif2247492012-09-12 20:23:50 -0300944 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -0300945
Antti Palosaaria3645e52012-06-07 20:36:35 -0300946 switch (state->af9013_config[adap->id].tuner) {
Antti Palosaari80619de2008-09-15 17:18:09 -0300947 case AF9013_TUNER_MT2060:
948 case AF9013_TUNER_MT2060_2:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300949 ret = dvb_attach(mt2060_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300950 &adap_to_d(adap)->i2c_adap, &af9015_mt2060_config,
Antti Palosaaria3645e52012-06-07 20:36:35 -0300951 state->mt2060_if1[adap->id])
Antti Palosaari80619de2008-09-15 17:18:09 -0300952 == NULL ? -ENODEV : 0;
953 break;
954 case AF9013_TUNER_QT1010:
955 case AF9013_TUNER_QT1010A:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300956 ret = dvb_attach(qt1010_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300957 &adap_to_d(adap)->i2c_adap,
Antti Palosaari80619de2008-09-15 17:18:09 -0300958 &af9015_qt1010_config) == NULL ? -ENODEV : 0;
959 break;
960 case AF9013_TUNER_TDA18271:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300961 ret = dvb_attach(tda18271_attach, adap->fe[0], 0xc0,
Antti Palosaarie8089662012-06-16 18:13:06 -0300962 &adap_to_d(adap)->i2c_adap,
Antti Palosaari80619de2008-09-15 17:18:09 -0300963 &af9015_tda18271_config) == NULL ? -ENODEV : 0;
964 break;
Antti Palosaariee3d4402010-08-13 03:51:26 -0300965 case AF9013_TUNER_TDA18218:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300966 ret = dvb_attach(tda18218_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300967 &adap_to_d(adap)->i2c_adap,
Antti Palosaariee3d4402010-08-13 03:51:26 -0300968 &af9015_tda18218_config) == NULL ? -ENODEV : 0;
969 break;
Antti Palosaari80619de2008-09-15 17:18:09 -0300970 case AF9013_TUNER_MXL5003D:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300971 ret = dvb_attach(mxl5005s_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300972 &adap_to_d(adap)->i2c_adap,
Antti Palosaari80619de2008-09-15 17:18:09 -0300973 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
974 break;
975 case AF9013_TUNER_MXL5005D:
976 case AF9013_TUNER_MXL5005R:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300977 ret = dvb_attach(mxl5005s_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300978 &adap_to_d(adap)->i2c_adap,
Antti Palosaari80619de2008-09-15 17:18:09 -0300979 &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
980 break;
981 case AF9013_TUNER_ENV77H11D5:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300982 ret = dvb_attach(dvb_pll_attach, adap->fe[0], 0xc0,
Antti Palosaarie8089662012-06-16 18:13:06 -0300983 &adap_to_d(adap)->i2c_adap,
Antti Palosaari80619de2008-09-15 17:18:09 -0300984 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
985 break;
986 case AF9013_TUNER_MC44S803:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300987 ret = dvb_attach(mc44s803_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300988 &adap_to_d(adap)->i2c_adap,
Jochen Friedrichd5633992009-02-02 14:59:50 -0300989 &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
Antti Palosaari80619de2008-09-15 17:18:09 -0300990 break;
Antti Palosaariab07fdd2010-09-09 14:59:10 -0300991 case AF9013_TUNER_MXL5007T:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300992 ret = dvb_attach(mxl5007t_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300993 &adap_to_d(adap)->i2c_adap,
Antti Palosaariab07fdd2010-09-09 14:59:10 -0300994 0xc0, &af9015_mxl5007t_config) == NULL ? -ENODEV : 0;
995 break;
Antti Palosaari80619de2008-09-15 17:18:09 -0300996 case AF9013_TUNER_UNKNOWN:
997 default:
Antti Palosaarif2247492012-09-12 20:23:50 -0300998 dev_err(&d->udev->dev, "%s: unknown tuner id=%d\n",
999 KBUILD_MODNAME,
1000 state->af9013_config[adap->id].tuner);
Antti Palosaari80619de2008-09-15 17:18:09 -03001001 ret = -ENODEV;
Antti Palosaari80619de2008-09-15 17:18:09 -03001002 }
Gordon Heckerbe4a5e72012-03-14 10:27:30 -03001003
Antti Palosaaria3645e52012-06-07 20:36:35 -03001004 if (adap->fe[0]->ops.tuner_ops.init) {
Antti Palosaari6d535bd2012-03-14 10:27:31 -03001005 state->tuner_init[adap->id] =
Antti Palosaaria3645e52012-06-07 20:36:35 -03001006 adap->fe[0]->ops.tuner_ops.init;
1007 adap->fe[0]->ops.tuner_ops.init = af9015_tuner_init;
Antti Palosaari6d535bd2012-03-14 10:27:31 -03001008 }
Gordon Heckerbe4a5e72012-03-14 10:27:30 -03001009
Antti Palosaaria3645e52012-06-07 20:36:35 -03001010 if (adap->fe[0]->ops.tuner_ops.sleep) {
Antti Palosaari6d535bd2012-03-14 10:27:31 -03001011 state->tuner_sleep[adap->id] =
Antti Palosaaria3645e52012-06-07 20:36:35 -03001012 adap->fe[0]->ops.tuner_ops.sleep;
1013 adap->fe[0]->ops.tuner_ops.sleep = af9015_tuner_sleep;
Antti Palosaari6d535bd2012-03-14 10:27:31 -03001014 }
1015
Antti Palosaari80619de2008-09-15 17:18:09 -03001016 return ret;
1017}
1018
Antti Palosaaria3645e52012-06-07 20:36:35 -03001019static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1020{
Antti Palosaarif2247492012-09-12 20:23:50 -03001021 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001022 int ret;
Antti Palosaarif2247492012-09-12 20:23:50 -03001023 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001024
1025 if (onoff)
Antti Palosaarif2247492012-09-12 20:23:50 -03001026 ret = af9015_set_reg_bit(d, 0xd503, 0);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001027 else
Antti Palosaarif2247492012-09-12 20:23:50 -03001028 ret = af9015_clear_reg_bit(d, 0xd503, 0);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001029
1030 return ret;
1031}
1032
1033static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1034 int onoff)
1035{
Antti Palosaarif2247492012-09-12 20:23:50 -03001036 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001037 int ret;
1038 u8 idx;
Antti Palosaarif2247492012-09-12 20:23:50 -03001039 dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
1040 __func__, index, pid, onoff);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001041
Antti Palosaarif2247492012-09-12 20:23:50 -03001042 ret = af9015_write_reg(d, 0xd505, (pid & 0xff));
Antti Palosaaria3645e52012-06-07 20:36:35 -03001043 if (ret)
1044 goto error;
1045
Antti Palosaarif2247492012-09-12 20:23:50 -03001046 ret = af9015_write_reg(d, 0xd506, (pid >> 8));
Antti Palosaaria3645e52012-06-07 20:36:35 -03001047 if (ret)
1048 goto error;
1049
1050 idx = ((index & 0x1f) | (1 << 5));
Antti Palosaarif2247492012-09-12 20:23:50 -03001051 ret = af9015_write_reg(d, 0xd504, idx);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001052
1053error:
1054 return ret;
1055}
1056
1057static int af9015_init_endpoint(struct dvb_usb_device *d)
1058{
Antti Palosaarie8089662012-06-16 18:13:06 -03001059 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001060 int ret;
1061 u16 frame_size;
1062 u8 packet_size;
Antti Palosaarif2247492012-09-12 20:23:50 -03001063 dev_dbg(&d->udev->dev, "%s: USB speed=%d\n", __func__, d->udev->speed);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001064
1065 if (d->udev->speed == USB_SPEED_FULL) {
1066 frame_size = TS_USB11_FRAME_SIZE/4;
1067 packet_size = TS_USB11_MAX_PACKET_SIZE/4;
1068 } else {
1069 frame_size = TS_USB20_FRAME_SIZE/4;
1070 packet_size = TS_USB20_MAX_PACKET_SIZE/4;
1071 }
1072
1073 ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
1074 if (ret)
1075 goto error;
1076 ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
1077 if (ret)
1078 goto error;
1079 ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
1080 if (ret)
1081 goto error;
1082 ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
1083 if (ret)
1084 goto error;
1085 ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
1086 if (ret)
1087 goto error;
1088 if (state->dual_mode) {
1089 ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
1090 if (ret)
1091 goto error;
1092 }
1093 ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
1094 if (ret)
1095 goto error;
1096 if (state->dual_mode) {
1097 ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
1098 if (ret)
1099 goto error;
1100 }
1101 /* EP4 xfer length */
1102 ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
1103 if (ret)
1104 goto error;
1105 ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
1106 if (ret)
1107 goto error;
1108 /* EP5 xfer length */
1109 ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
1110 if (ret)
1111 goto error;
1112 ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
1113 if (ret)
1114 goto error;
1115 ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
1116 if (ret)
1117 goto error;
1118 ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
1119 if (ret)
1120 goto error;
1121 ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
1122 if (ret)
1123 goto error;
1124 if (state->dual_mode) {
1125 ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
1126 if (ret)
1127 goto error;
1128 }
1129
1130 /* enable / disable mp2if2 */
1131 if (state->dual_mode)
1132 ret = af9015_set_reg_bit(d, 0xd50b, 0);
1133 else
1134 ret = af9015_clear_reg_bit(d, 0xd50b, 0);
1135
1136error:
1137 if (ret)
Antti Palosaarif2247492012-09-12 20:23:50 -03001138 dev_err(&d->udev->dev, "%s: endpoint init failed=%d\n",
1139 KBUILD_MODNAME, ret);
1140
Antti Palosaaria3645e52012-06-07 20:36:35 -03001141 return ret;
1142}
1143
1144static int af9015_init(struct dvb_usb_device *d)
1145{
Antti Palosaarie8089662012-06-16 18:13:06 -03001146 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001147 int ret;
Antti Palosaarif2247492012-09-12 20:23:50 -03001148 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001149
1150 mutex_init(&state->fe_mutex);
1151
1152 /* init RC canary */
1153 ret = af9015_write_reg(d, 0x98e9, 0xff);
1154 if (ret)
1155 goto error;
1156
1157 ret = af9015_init_endpoint(d);
1158 if (ret)
1159 goto error;
1160
1161error:
1162 return ret;
1163}
1164
Antti Palosaari37b44a02013-01-04 15:21:26 -03001165#if IS_ENABLED(CONFIG_RC_CORE)
Antti Palosaaria3645e52012-06-07 20:36:35 -03001166struct af9015_rc_setup {
1167 unsigned int id;
1168 char *rc_codes;
Jonathan Niederd07b9012012-01-07 04:11:27 -03001169};
1170
Antti Palosaaria3645e52012-06-07 20:36:35 -03001171static char *af9015_rc_setup_match(unsigned int id,
1172 const struct af9015_rc_setup *table)
1173{
1174 for (; table->rc_codes; table++)
1175 if (table->id == id)
1176 return table->rc_codes;
1177 return NULL;
1178}
1179
1180static const struct af9015_rc_setup af9015_rc_setup_modparam[] = {
1181 { AF9015_REMOTE_A_LINK_DTU_M, RC_MAP_ALINK_DTU_M },
1182 { AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3, RC_MAP_MSI_DIGIVOX_II },
1183 { AF9015_REMOTE_MYGICTV_U718, RC_MAP_TOTAL_MEDIA_IN_HAND },
1184 { AF9015_REMOTE_DIGITTRADE_DVB_T, RC_MAP_DIGITTRADE },
1185 { AF9015_REMOTE_AVERMEDIA_KS, RC_MAP_AVERMEDIA_RM_KS },
Jonathan Niederd07b9012012-01-07 04:11:27 -03001186 { }
Antti Palosaari80619de2008-09-15 17:18:09 -03001187};
Antti Palosaari80619de2008-09-15 17:18:09 -03001188
Antti Palosaaria3645e52012-06-07 20:36:35 -03001189static const struct af9015_rc_setup af9015_rc_setup_hashes[] = {
1190 { 0xb8feb708, RC_MAP_MSI_DIGIVOX_II },
1191 { 0xa3703d00, RC_MAP_ALINK_DTU_M },
1192 { 0x9b7dc64e, RC_MAP_TOTAL_MEDIA_IN_HAND }, /* MYGICTV U718 */
1193 { 0x5d49e3db, RC_MAP_DIGITTRADE }, /* LC-Power LC-USB-DVBT */
1194 { }
1195};
Antti Palosaari80619de2008-09-15 17:18:09 -03001196
Antti Palosaaria3645e52012-06-07 20:36:35 -03001197static int af9015_rc_query(struct dvb_usb_device *d)
1198{
Antti Palosaarie8089662012-06-16 18:13:06 -03001199 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001200 int ret;
1201 u8 buf[17];
Antti Palosaari80619de2008-09-15 17:18:09 -03001202
Antti Palosaaria3645e52012-06-07 20:36:35 -03001203 /* read registers needed to detect remote controller code */
1204 ret = af9015_read_regs(d, 0x98d9, buf, sizeof(buf));
1205 if (ret)
1206 goto error;
Antti Palosaari80619de2008-09-15 17:18:09 -03001207
Antti Palosaaria3645e52012-06-07 20:36:35 -03001208 /* If any of these are non-zero, assume invalid data */
Antti Palosaarif2247492012-09-12 20:23:50 -03001209 if (buf[1] || buf[2] || buf[3]) {
1210 dev_dbg(&d->udev->dev, "%s: invalid data\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001211 return ret;
Antti Palosaarif2247492012-09-12 20:23:50 -03001212 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001213
Antti Palosaaria3645e52012-06-07 20:36:35 -03001214 /* Check for repeat of previous code */
1215 if ((state->rc_repeat != buf[6] || buf[0]) &&
1216 !memcmp(&buf[12], state->rc_last, 4)) {
Antti Palosaarif2247492012-09-12 20:23:50 -03001217 dev_dbg(&d->udev->dev, "%s: key repeated\n", __func__);
David Härdeman120703f2014-04-03 20:31:30 -03001218 rc_repeat(d->rc_dev);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001219 state->rc_repeat = buf[6];
1220 return ret;
1221 }
1222
1223 /* Only process key if canary killed */
1224 if (buf[16] != 0xff && buf[0] != 0x01) {
Antti Palosaarif2247492012-09-12 20:23:50 -03001225 dev_dbg(&d->udev->dev, "%s: key pressed %*ph\n",
1226 __func__, 4, buf + 12);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001227
1228 /* Reset the canary */
1229 ret = af9015_write_reg(d, 0x98e9, 0xff);
1230 if (ret)
1231 goto error;
1232
1233 /* Remember this key */
1234 memcpy(state->rc_last, &buf[12], 4);
1235 if (buf[14] == (u8) ~buf[15]) {
1236 if (buf[12] == (u8) ~buf[13]) {
1237 /* NEC */
David Härdeman120703f2014-04-03 20:31:30 -03001238 state->rc_keycode = RC_SCANCODE_NEC(buf[12],
1239 buf[14]);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001240 } else {
1241 /* NEC extended*/
David Härdeman120703f2014-04-03 20:31:30 -03001242 state->rc_keycode = RC_SCANCODE_NECX(buf[12] << 8 |
1243 buf[13],
1244 buf[14]);
Antti Palosaari80619de2008-09-15 17:18:09 -03001245 }
Antti Palosaaria3645e52012-06-07 20:36:35 -03001246 } else {
1247 /* 32 bit NEC */
David Härdeman120703f2014-04-03 20:31:30 -03001248 state->rc_keycode = RC_SCANCODE_NEC32(buf[12] << 24 |
1249 buf[13] << 16 |
1250 buf[14] << 8 |
1251 buf[15]);
Antti Palosaari80619de2008-09-15 17:18:09 -03001252 }
David Härdeman120703f2014-04-03 20:31:30 -03001253 rc_keydown(d->rc_dev, RC_TYPE_NEC, state->rc_keycode, 0);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001254 } else {
Antti Palosaarif2247492012-09-12 20:23:50 -03001255 dev_dbg(&d->udev->dev, "%s: no key press\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001256 /* Invalidate last keypress */
1257 /* Not really needed, but helps with debug */
1258 state->rc_last[2] = state->rc_last[3];
1259 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001260
Antti Palosaaria3645e52012-06-07 20:36:35 -03001261 state->rc_repeat = buf[6];
Antti Palosaarieb29fbe2012-07-24 21:21:04 -03001262 state->rc_failed = false;
Antti Palosaari80619de2008-09-15 17:18:09 -03001263
Antti Palosaaria3645e52012-06-07 20:36:35 -03001264error:
Antti Palosaarieb29fbe2012-07-24 21:21:04 -03001265 if (ret) {
Antti Palosaarif2247492012-09-12 20:23:50 -03001266 dev_warn(&d->udev->dev, "%s: rc query failed=%d\n",
1267 KBUILD_MODNAME, ret);
Antti Palosaari80619de2008-09-15 17:18:09 -03001268
Antti Palosaarieb29fbe2012-07-24 21:21:04 -03001269 /* allow random errors as dvb-usb will stop polling on error */
1270 if (!state->rc_failed)
1271 ret = 0;
1272
1273 state->rc_failed = true;
1274 }
1275
Antti Palosaaria3645e52012-06-07 20:36:35 -03001276 return ret;
1277}
Antti Palosaari80619de2008-09-15 17:18:09 -03001278
Antti Palosaaria3645e52012-06-07 20:36:35 -03001279static int af9015_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1280{
Antti Palosaarie8089662012-06-16 18:13:06 -03001281 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001282 u16 vid = le16_to_cpu(d->udev->descriptor.idVendor);
Antti Palosaari80619de2008-09-15 17:18:09 -03001283
Antti Palosaaria3645e52012-06-07 20:36:35 -03001284 if (state->ir_mode == AF9015_IR_MODE_DISABLED)
1285 return 0;
Antti Palosaari80619de2008-09-15 17:18:09 -03001286
Antti Palosaaria3645e52012-06-07 20:36:35 -03001287 /* try to load remote based module param */
Antti Palosaarieb29fbe2012-07-24 21:21:04 -03001288 if (!rc->map_name)
1289 rc->map_name = af9015_rc_setup_match(dvb_usb_af9015_remote,
1290 af9015_rc_setup_modparam);
Antti Palosaari80619de2008-09-15 17:18:09 -03001291
Antti Palosaaria3645e52012-06-07 20:36:35 -03001292 /* try to load remote based eeprom hash */
1293 if (!rc->map_name)
1294 rc->map_name = af9015_rc_setup_match(state->eeprom_sum,
1295 af9015_rc_setup_hashes);
Antti Palosaari80619de2008-09-15 17:18:09 -03001296
Antti Palosaaria3645e52012-06-07 20:36:35 -03001297 /* try to load remote based USB iManufacturer string */
1298 if (!rc->map_name && vid == USB_VID_AFATECH) {
1299 /* Check USB manufacturer and product strings and try
1300 to determine correct remote in case of chip vendor
1301 reference IDs are used.
1302 DO NOT ADD ANYTHING NEW HERE. Use hashes instead. */
1303 char manufacturer[10];
1304 memset(manufacturer, 0, sizeof(manufacturer));
1305 usb_string(d->udev, d->udev->descriptor.iManufacturer,
1306 manufacturer, sizeof(manufacturer));
1307 if (!strcmp("MSI", manufacturer)) {
1308 /* iManufacturer 1 MSI
1309 iProduct 2 MSI K-VOX */
1310 rc->map_name = af9015_rc_setup_match(
1311 AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
1312 af9015_rc_setup_modparam);
Antti Palosaari80619de2008-09-15 17:18:09 -03001313 }
Antti Palosaaria3645e52012-06-07 20:36:35 -03001314 }
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001315
Antti Palosaaride73bee2012-07-05 19:57:07 -03001316 /* load empty to enable rc */
1317 if (!rc->map_name)
1318 rc->map_name = RC_MAP_EMPTY;
1319
David Härdemanc003ab12012-10-11 19:11:54 -03001320 rc->allowed_protos = RC_BIT_NEC;
Antti Palosaaria3645e52012-06-07 20:36:35 -03001321 rc->query = af9015_rc_query;
1322 rc->interval = 500;
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001323
Antti Palosaaria3645e52012-06-07 20:36:35 -03001324 return 0;
1325}
Antti Palosaarib6215592012-12-09 20:15:47 -03001326#else
1327 #define af9015_get_rc_config NULL
1328#endif
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001329
Antti Palosaarie8c7aab2013-01-07 16:29:13 -03001330static int af9015_probe(struct usb_interface *intf,
1331 const struct usb_device_id *id)
1332{
1333 struct usb_device *udev = interface_to_usbdev(intf);
1334 char manufacturer[sizeof("ITE Technologies, Inc.")];
1335
1336 memset(manufacturer, 0, sizeof(manufacturer));
1337 usb_string(udev, udev->descriptor.iManufacturer,
1338 manufacturer, sizeof(manufacturer));
1339 /*
1340 * There is two devices having same ID but different chipset. One uses
1341 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1342 * is iManufacturer string.
1343 *
1344 * idVendor 0x0ccd TerraTec Electronic GmbH
1345 * idProduct 0x0099
1346 * bcdDevice 2.00
1347 * iManufacturer 1 Afatech
1348 * iProduct 2 DVB-T 2
1349 *
1350 * idVendor 0x0ccd TerraTec Electronic GmbH
1351 * idProduct 0x0099
1352 * bcdDevice 2.00
1353 * iManufacturer 1 ITE Technologies, Inc.
1354 * iProduct 2 DVB-T TV Stick
1355 */
1356 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1357 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1358 if (!strcmp("ITE Technologies, Inc.", manufacturer)) {
1359 dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
1360 return -ENODEV;
1361 }
1362 }
1363
1364 return dvb_usbv2_probe(intf, id);
1365}
1366
Antti Palosaaria3645e52012-06-07 20:36:35 -03001367/* interface 0 is used by DVB-T receiver and
1368 interface 1 is for remote controller (HID) */
1369static struct dvb_usb_device_properties af9015_props = {
1370 .driver_name = KBUILD_MODNAME,
1371 .owner = THIS_MODULE,
1372 .adapter_nr = adapter_nr,
1373 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001374
Antti Palosaaria3645e52012-06-07 20:36:35 -03001375 .generic_bulk_ctrl_endpoint = 0x02,
1376 .generic_bulk_ctrl_endpoint_response = 0x81,
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001377
Antti Palosaaria3645e52012-06-07 20:36:35 -03001378 .identify_state = af9015_identify_state,
Antti Palosaaribab9b4f2012-09-12 11:37:25 -03001379 .firmware = AF9015_FIRMWARE,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001380 .download_firmware = af9015_download_firmware,
1381
Antti Palosaaria3645e52012-06-07 20:36:35 -03001382 .i2c_algo = &af9015_i2c_algo,
Antti Palosaari2d2b37c72012-06-12 01:05:20 -03001383 .read_config = af9015_read_config,
1384 .frontend_attach = af9015_af9013_frontend_attach,
1385 .tuner_attach = af9015_tuner_attach,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001386 .init = af9015_init,
1387 .get_rc_config = af9015_get_rc_config,
Antti Palosaarib905a2a2012-06-18 22:54:16 -03001388 .get_stream_config = af9015_get_stream_config,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001389
1390 .get_adapter_count = af9015_get_adapter_count,
1391 .adapter = {
1392 {
1393 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1394 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1395 .pid_filter_count = 32,
1396 .pid_filter = af9015_pid_filter,
1397 .pid_filter_ctrl = af9015_pid_filter_ctrl,
Antti Palosaari1a590012012-06-16 16:25:22 -03001398
1399 .stream = DVB_USB_STREAM_BULK(0x84, 8, TS_USB20_FRAME_SIZE),
1400 }, {
1401 .stream = DVB_USB_STREAM_BULK(0x85, 8, TS_USB20_FRAME_SIZE),
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001402 },
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001403 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001404};
Antti Palosaari80619de2008-09-15 17:18:09 -03001405
Antti Palosaaria3645e52012-06-07 20:36:35 -03001406static const struct usb_device_id af9015_id_table[] = {
1407 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015,
1408 &af9015_props, "Afatech AF9015 reference design", NULL) },
1409 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016,
1410 &af9015_props, "Afatech AF9015 reference design", NULL) },
1411 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD,
1412 &af9015_props, "Leadtek WinFast DTV Dongle Gold", RC_MAP_LEADTEK_Y04G0051) },
1413 { DVB_USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E,
1414 &af9015_props, "Pinnacle PCTV 71e", NULL) },
1415 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U,
1416 &af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) },
1417 { DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TINYTWIN,
1418 &af9015_props, "DigitalNow TinyTwin", RC_MAP_AZUREWAVE_AD_TU700) },
1419 { DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_AZUREWAVE_AD_TU700,
1420 &af9015_props, "TwinHan AzureWave AD-TU700(704J)", RC_MAP_AZUREWAVE_AD_TU700) },
1421 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2,
1422 &af9015_props, "TerraTec Cinergy T USB XE", NULL) },
1423 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T,
1424 &af9015_props, "KWorld PlusTV Dual DVB-T PCI (DVB-T PC160-2T)", NULL) },
1425 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X,
1426 &af9015_props, "AVerMedia AVerTV DVB-T Volar X", RC_MAP_AVERMEDIA_M135A) },
1427 { DVB_USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380,
1428 &af9015_props, "Xtensions XD-380", NULL) },
1429 { DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO,
1430 &af9015_props, "MSI DIGIVOX Duo", RC_MAP_MSI_DIGIVOX_III) },
1431 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2,
1432 &af9015_props, "Fujitsu-Siemens Slim Mobile USB DVB-T", NULL) },
1433 { DVB_USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2,
1434 &af9015_props, "Telestar Starstick 2", NULL) },
1435 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309,
1436 &af9015_props, "AVerMedia A309", NULL) },
1437 { DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III,
1438 &af9015_props, "MSI Digi VOX mini III", RC_MAP_MSI_DIGIVOX_III) },
1439 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U,
1440 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1441 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2,
1442 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1443 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3,
1444 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1445 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT,
1446 &af9015_props, "TrekStor DVB-T USB Stick", RC_MAP_TREKSTOR) },
1447 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850,
1448 &af9015_props, "AverMedia AVerTV Volar Black HD (A850)", NULL) },
1449 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805,
1450 &af9015_props, "AverMedia AVerTV Volar GPS 805 (A805)", NULL) },
1451 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU,
1452 &af9015_props, "Conceptronic USB2.0 DVB-T CTVDIGRCU V3.0", NULL) },
1453 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810,
1454 &af9015_props, "KWorld Digial MC-810", NULL) },
1455 { DVB_USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03,
1456 &af9015_props, "Genius TVGo DVB-T03", NULL) },
1457 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2,
1458 &af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) },
1459 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T,
1460 &af9015_props, "KWorld PlusTV DVB-T PCI Pro Card (DVB-T PC160-T)", NULL) },
1461 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20,
1462 &af9015_props, "Sveon STV20 Tuner USB DVB-T HDTV", NULL) },
1463 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2,
1464 &af9015_props, "DigitalNow TinyTwin v2", RC_MAP_DIGITALNOW_TINYTWIN) },
1465 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS,
1466 &af9015_props, "Leadtek WinFast DTV2000DS", RC_MAP_LEADTEK_Y04G0051) },
1467 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T,
1468 &af9015_props, "KWorld USB DVB-T Stick Mobile (UB383-T)", NULL) },
1469 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4,
1470 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1471 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M,
1472 &af9015_props, "AverMedia AVerTV Volar M (A815Mac)", NULL) },
1473 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_RC,
1474 &af9015_props, "TerraTec Cinergy T Stick RC", RC_MAP_TERRATEC_SLIM_2) },
Antti Palosaarie8c7aab2013-01-07 16:29:13 -03001475 /* XXX: that same ID [0ccd:0099] is used by af9035 driver too */
Antti Palosaaria3645e52012-06-07 20:36:35 -03001476 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC,
1477 &af9015_props, "TerraTec Cinergy T Stick Dual RC", RC_MAP_TERRATEC_SLIM) },
1478 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850T,
1479 &af9015_props, "AverMedia AVerTV Red HD+ (A850T)", NULL) },
1480 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TINYTWIN_3,
1481 &af9015_props, "DigitalNow TinyTwin v3", RC_MAP_DIGITALNOW_TINYTWIN) },
1482 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22,
1483 &af9015_props, "Sveon STV22 Dual USB DVB-T Tuner HDTV", RC_MAP_MSI_DIGIVOX_III) },
1484 { }
1485};
1486MODULE_DEVICE_TABLE(usb, af9015_id_table);
Antti Palosaari80619de2008-09-15 17:18:09 -03001487
Antti Palosaari80619de2008-09-15 17:18:09 -03001488/* usb specific object needed to register this driver with the usb subsystem */
1489static struct usb_driver af9015_usb_driver = {
Antti Palosaaria3645e52012-06-07 20:36:35 -03001490 .name = KBUILD_MODNAME,
Antti Palosaari2d2b37c72012-06-12 01:05:20 -03001491 .id_table = af9015_id_table,
Antti Palosaarie8c7aab2013-01-07 16:29:13 -03001492 .probe = af9015_probe,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001493 .disconnect = dvb_usbv2_disconnect,
Antti Palosaari53dc1942012-06-12 02:20:01 -03001494 .suspend = dvb_usbv2_suspend,
1495 .resume = dvb_usbv2_resume,
Antti Palosaari04966aa2012-08-14 22:21:08 -03001496 .reset_resume = dvb_usbv2_reset_resume,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001497 .no_dynamic_id = 1,
Antti Palosaari77f54512012-06-09 21:22:06 -03001498 .soft_unbind = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001499};
1500
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08001501module_usb_driver(af9015_usb_driver);
Antti Palosaari80619de2008-09-15 17:18:09 -03001502
1503MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
Antti Palosaaribc050e62012-05-08 06:04:24 -03001504MODULE_DESCRIPTION("Afatech AF9015 driver");
Antti Palosaari80619de2008-09-15 17:18:09 -03001505MODULE_LICENSE("GPL");
Antti Palosaaribab9b4f2012-09-12 11:37:25 -03001506MODULE_FIRMWARE(AF9015_FIRMWARE);