blob: 943d93423705a0e1c5d404db966219ff528ebb15 [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 BUF_LEN 63
34#define REQ_HDR_LEN 8 /* send header size */
35#define ACK_HDR_LEN 2 /* rece header size */
Antti Palosaarie8089662012-06-16 18:13:06 -030036 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -030037 int ret, wlen, rlen;
Antti Palosaari06565d72009-09-12 20:46:30 -030038 u8 buf[BUF_LEN];
Antti Palosaari80619de2008-09-15 17:18:09 -030039 u8 write = 1;
Antti Palosaari80619de2008-09-15 17:18:09 -030040
41 buf[0] = req->cmd;
Antti Palosaaria3645e52012-06-07 20:36:35 -030042 buf[1] = state->seq++;
Antti Palosaari80619de2008-09-15 17:18:09 -030043 buf[2] = req->i2c_addr;
44 buf[3] = req->addr >> 8;
45 buf[4] = req->addr & 0xff;
46 buf[5] = req->mbox;
47 buf[6] = req->addr_len;
48 buf[7] = req->data_len;
49
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;
58 buf[2] |= 0x01; /* set I2C direction */
59 case WRITE_I2C:
60 buf[0] = READ_WRITE_I2C;
61 break;
62 case WRITE_MEMORY:
63 if (((req->addr & 0xff00) == 0xff00) ||
Antti Palosaarif4e96de2009-09-12 21:25:59 -030064 ((req->addr & 0xff00) == 0xae00))
Antti Palosaari80619de2008-09-15 17:18:09 -030065 buf[0] = WRITE_VIRTUAL_MEMORY;
66 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;
93 memcpy(&buf[REQ_HDR_LEN], req->data, req->data_len);
94 } 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 Palosaari1162c7b2012-06-20 20:27:42 -0300102 ret = dvb_usbv2_generic_rw(d, buf, wlen, buf, rlen);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300103 if (ret)
104 goto error;
Antti Palosaari80619de2008-09-15 17:18:09 -0300105
Antti Palosaari80619de2008-09-15 17:18:09 -0300106 /* check status */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300107 if (rlen && buf[1]) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300108 dev_err(&d->udev->dev, "%s: command failed=%d\n",
109 KBUILD_MODNAME, buf[1]);
Antti Palosaarie1e9e512012-09-12 20:23:52 -0300110 ret = -EIO;
Antti Palosaaria3645e52012-06-07 20:36:35 -0300111 goto error;
Antti Palosaari80619de2008-09-15 17:18:09 -0300112 }
113
114 /* read request, copy returned data to return buf */
115 if (!write)
Antti Palosaari06565d72009-09-12 20:46:30 -0300116 memcpy(req->data, &buf[ACK_HDR_LEN], req->data_len);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300117error:
Antti Palosaari80619de2008-09-15 17:18:09 -0300118 return ret;
119}
120
Antti Palosaari80619de2008-09-15 17:18:09 -0300121static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
122 u8 len)
123{
124 struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
125 val};
126 return af9015_ctrl_msg(d, &req);
127}
128
Antti Palosaari74c8e3a2010-10-22 18:45:18 -0300129static int af9015_read_regs(struct dvb_usb_device *d, u16 addr, u8 *val, u8 len)
130{
131 struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
132 val};
133 return af9015_ctrl_msg(d, &req);
134}
135
Antti Palosaaria3645e52012-06-07 20:36:35 -0300136static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
137{
138 return af9015_write_regs(d, addr, &val, 1);
139}
140
Antti Palosaari80619de2008-09-15 17:18:09 -0300141static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
142{
Antti Palosaari74c8e3a2010-10-22 18:45:18 -0300143 return af9015_read_regs(d, addr, val, 1);
Antti Palosaari80619de2008-09-15 17:18:09 -0300144}
145
146static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
147 u8 val)
148{
Antti Palosaarie8089662012-06-16 18:13:06 -0300149 struct af9015_state *state = d_to_priv(d);
Antti Palosaari80619de2008-09-15 17:18:09 -0300150 struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
151
Antti Palosaaria3645e52012-06-07 20:36:35 -0300152 if (addr == state->af9013_config[0].i2c_addr ||
153 addr == state->af9013_config[1].i2c_addr)
Antti Palosaari80619de2008-09-15 17:18:09 -0300154 req.addr_len = 3;
155
156 return af9015_ctrl_msg(d, &req);
157}
158
159static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
160 u8 *val)
161{
Antti Palosaarie8089662012-06-16 18:13:06 -0300162 struct af9015_state *state = d_to_priv(d);
Antti Palosaari80619de2008-09-15 17:18:09 -0300163 struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
164
Antti Palosaaria3645e52012-06-07 20:36:35 -0300165 if (addr == state->af9013_config[0].i2c_addr ||
166 addr == state->af9013_config[1].i2c_addr)
Antti Palosaari80619de2008-09-15 17:18:09 -0300167 req.addr_len = 3;
168
169 return af9015_ctrl_msg(d, &req);
170}
171
Antti Palosaaria3645e52012-06-07 20:36:35 -0300172static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
173{
174 int ret;
175 u8 val, mask = 0x01;
176
177 ret = af9015_read_reg(d, addr, &val);
178 if (ret)
179 return ret;
180
181 mask <<= bit;
182 if (op) {
183 /* set bit */
184 val |= mask;
185 } else {
186 /* clear bit */
187 mask ^= 0xff;
188 val &= mask;
189 }
190
191 return af9015_write_reg(d, addr, val);
192}
193
194static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
195{
196 return af9015_do_reg_bit(d, addr, bit, 1);
197}
198
199static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
200{
201 return af9015_do_reg_bit(d, addr, bit, 0);
202}
203
Antti Palosaari80619de2008-09-15 17:18:09 -0300204static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
205 int num)
206{
207 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Antti Palosaarie8089662012-06-16 18:13:06 -0300208 struct af9015_state *state = d_to_priv(d);
Antti Palosaari80619de2008-09-15 17:18:09 -0300209 int ret = 0, i = 0;
210 u16 addr;
Antti Palosaari675375d2010-10-07 21:46:41 -0300211 u8 uninitialized_var(mbox), addr_len;
Antti Palosaari80619de2008-09-15 17:18:09 -0300212 struct req_t req;
213
Antti Palosaaribc050e62012-05-08 06:04:24 -0300214/*
Antti Palosaari80619de2008-09-15 17:18:09 -0300215The bus lock is needed because there is two tuners both using same I2C-address.
216Due to that the only way to select correct tuner is use demodulator I2C-gate.
217
218................................................
219. AF9015 includes integrated AF9013 demodulator.
220. ____________ ____________ . ____________
221.| uC | | demod | . | tuner |
222.|------------| |------------| . |------------|
223.| AF9015 | | AF9013/5 | . | MXL5003 |
224.| |--+----I2C-------|-----/ -----|-.-----I2C-------| |
225.| | | | addr 0x38 | . | addr 0xc6 |
226.|____________| | |____________| . |____________|
227.................|..............................
228 | ____________ ____________
229 | | demod | | tuner |
230 | |------------| |------------|
231 | | AF9013 | | MXL5003 |
232 +----I2C-------|-----/ -----|-------I2C-------| |
233 | addr 0x3a | | addr 0xc6 |
234 |____________| |____________|
235*/
236 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
237 return -EAGAIN;
238
239 while (i < num) {
Antti Palosaaria3645e52012-06-07 20:36:35 -0300240 if (msg[i].addr == state->af9013_config[0].i2c_addr ||
241 msg[i].addr == state->af9013_config[1].i2c_addr) {
Antti Palosaari80619de2008-09-15 17:18:09 -0300242 addr = msg[i].buf[0] << 8;
243 addr += msg[i].buf[1];
244 mbox = msg[i].buf[2];
245 addr_len = 3;
246 } else {
247 addr = msg[i].buf[0];
248 addr_len = 1;
Antti Palosaari675375d2010-10-07 21:46:41 -0300249 /* mbox is don't care in that case */
Antti Palosaari80619de2008-09-15 17:18:09 -0300250 }
251
252 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
Antti Palosaari709d9202011-06-17 21:16:38 -0300253 if (msg[i].len > 3 || msg[i+1].len > 61) {
254 ret = -EOPNOTSUPP;
255 goto error;
256 }
Antti Palosaaria3645e52012-06-07 20:36:35 -0300257 if (msg[i].addr == state->af9013_config[0].i2c_addr)
Antti Palosaari80619de2008-09-15 17:18:09 -0300258 req.cmd = READ_MEMORY;
259 else
260 req.cmd = READ_I2C;
261 req.i2c_addr = msg[i].addr;
262 req.addr = addr;
263 req.mbox = mbox;
264 req.addr_len = addr_len;
265 req.data_len = msg[i+1].len;
266 req.data = &msg[i+1].buf[0];
267 ret = af9015_ctrl_msg(d, &req);
268 i += 2;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300269 } else if (msg[i].flags & I2C_M_RD) {
Antti Palosaari709d9202011-06-17 21:16:38 -0300270 if (msg[i].len > 61) {
271 ret = -EOPNOTSUPP;
272 goto error;
273 }
Antti Palosaaria3645e52012-06-07 20:36:35 -0300274 if (msg[i].addr == state->af9013_config[0].i2c_addr) {
Antti Palosaari16b2dc22011-06-16 20:02:41 -0300275 ret = -EINVAL;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300276 goto error;
Antti Palosaari16b2dc22011-06-16 20:02:41 -0300277 }
278 req.cmd = READ_I2C;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300279 req.i2c_addr = msg[i].addr;
280 req.addr = addr;
281 req.mbox = mbox;
282 req.addr_len = addr_len;
283 req.data_len = msg[i].len;
284 req.data = &msg[i].buf[0];
285 ret = af9015_ctrl_msg(d, &req);
286 i += 1;
Antti Palosaari80619de2008-09-15 17:18:09 -0300287 } else {
Antti Palosaari709d9202011-06-17 21:16:38 -0300288 if (msg[i].len > 21) {
289 ret = -EOPNOTSUPP;
290 goto error;
291 }
Antti Palosaaria3645e52012-06-07 20:36:35 -0300292 if (msg[i].addr == state->af9013_config[0].i2c_addr)
Antti Palosaari80619de2008-09-15 17:18:09 -0300293 req.cmd = WRITE_MEMORY;
294 else
295 req.cmd = WRITE_I2C;
296 req.i2c_addr = msg[i].addr;
297 req.addr = addr;
298 req.mbox = mbox;
299 req.addr_len = addr_len;
300 req.data_len = msg[i].len-addr_len;
301 req.data = &msg[i].buf[addr_len];
302 ret = af9015_ctrl_msg(d, &req);
303 i += 1;
304 }
305 if (ret)
306 goto error;
307
308 }
309 ret = i;
310
311error:
312 mutex_unlock(&d->i2c_mutex);
313
314 return ret;
315}
316
317static u32 af9015_i2c_func(struct i2c_adapter *adapter)
318{
319 return I2C_FUNC_I2C;
320}
321
322static struct i2c_algorithm af9015_i2c_algo = {
323 .master_xfer = af9015_i2c_xfer,
324 .functionality = af9015_i2c_func,
325};
326
Antti Palosaaria0921af2012-06-18 23:42:53 -0300327static int af9015_identify_state(struct dvb_usb_device *d, const char **name)
Antti Palosaari80619de2008-09-15 17:18:09 -0300328{
329 int ret;
Antti Palosaaria3645e52012-06-07 20:36:35 -0300330 u8 reply;
331 struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
Antti Palosaari80619de2008-09-15 17:18:09 -0300332
Antti Palosaaria3645e52012-06-07 20:36:35 -0300333 ret = af9015_ctrl_msg(d, &req);
Antti Palosaari80619de2008-09-15 17:18:09 -0300334 if (ret)
335 return ret;
336
Antti Palosaarif2247492012-09-12 20:23:50 -0300337 dev_dbg(&d->udev->dev, "%s: reply=%02x\n", __func__, reply);
338
Antti Palosaaria3645e52012-06-07 20:36:35 -0300339 if (reply == 0x02)
340 ret = WARM;
Antti Palosaari80619de2008-09-15 17:18:09 -0300341 else
Antti Palosaaria3645e52012-06-07 20:36:35 -0300342 ret = COLD;
343
344 return ret;
345}
346
347static int af9015_download_firmware(struct dvb_usb_device *d,
348 const struct firmware *fw)
349{
Antti Palosaarie8089662012-06-16 18:13:06 -0300350 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300351 int i, len, remaining, ret;
352 struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
353 u16 checksum = 0;
Antti Palosaarif2247492012-09-12 20:23:50 -0300354 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300355
356 /* calc checksum */
357 for (i = 0; i < fw->size; i++)
358 checksum += fw->data[i];
359
360 state->firmware_size = fw->size;
361 state->firmware_checksum = checksum;
362
363 #define FW_ADDR 0x5100 /* firmware start address */
364 #define LEN_MAX 55 /* max packet size */
365 for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
366 len = remaining;
367 if (len > LEN_MAX)
368 len = LEN_MAX;
369
370 req.data_len = len;
371 req.data = (u8 *) &fw->data[fw->size - remaining];
372 req.addr = FW_ADDR + fw->size - remaining;
373
374 ret = af9015_ctrl_msg(d, &req);
375 if (ret) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300376 dev_err(&d->udev->dev,
377 "%s: firmware download failed=%d\n",
378 KBUILD_MODNAME, ret);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300379 goto error;
380 }
381 }
382
383 /* firmware loaded, request boot */
384 req.cmd = BOOT;
385 req.data_len = 0;
386 ret = af9015_ctrl_msg(d, &req);
387 if (ret) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300388 dev_err(&d->udev->dev, "%s: firmware boot failed=%d\n",
389 KBUILD_MODNAME, ret);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300390 goto error;
391 }
392
393error:
394 return ret;
395}
396
397/* hash (and dump) eeprom */
398static int af9015_eeprom_hash(struct dvb_usb_device *d)
399{
Antti Palosaarie8089662012-06-16 18:13:06 -0300400 struct af9015_state *state = d_to_priv(d);
Antti Palosaari2e35c662012-09-12 20:23:51 -0300401 int ret, i;
402 static const unsigned int AF9015_EEPROM_SIZE = 256;
403 u8 buf[AF9015_EEPROM_SIZE];
404 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, NULL};
Antti Palosaaria3645e52012-06-07 20:36:35 -0300405
Antti Palosaari2e35c662012-09-12 20:23:51 -0300406 /* read eeprom */
407 for (i = 0; i < AF9015_EEPROM_SIZE; i++) {
408 req.addr = i;
409 req.data = &buf[i];
Antti Palosaaria3645e52012-06-07 20:36:35 -0300410 ret = af9015_ctrl_msg(d, &req);
Antti Palosaari2e35c662012-09-12 20:23:51 -0300411 if (ret < 0)
412 goto err;
Antti Palosaaria3645e52012-06-07 20:36:35 -0300413 }
414
Antti Palosaari2e35c662012-09-12 20:23:51 -0300415 /* calculate checksum */
416 for (i = 0; i < AF9015_EEPROM_SIZE / sizeof(u32); i++) {
Antti Palosaaria3645e52012-06-07 20:36:35 -0300417 state->eeprom_sum *= GOLDEN_RATIO_PRIME_32;
Antti Palosaari2e35c662012-09-12 20:23:51 -0300418 state->eeprom_sum += le32_to_cpu(((u32 *)buf)[i]);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300419 }
420
Antti Palosaari2e35c662012-09-12 20:23:51 -0300421 for (i = 0; i < AF9015_EEPROM_SIZE; i += 16)
422 dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 16, buf + i);
423
Antti Palosaarif2247492012-09-12 20:23:50 -0300424 dev_dbg(&d->udev->dev, "%s: eeprom sum=%.8x\n",
425 __func__, state->eeprom_sum);
Antti Palosaari2e35c662012-09-12 20:23:51 -0300426 return 0;
427err:
428 dev_err(&d->udev->dev, "%s: eeprom failed=%d\n", KBUILD_MODNAME, ret);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300429 return ret;
430}
431
432static int af9015_read_config(struct dvb_usb_device *d)
433{
Antti Palosaarie8089662012-06-16 18:13:06 -0300434 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300435 int ret;
436 u8 val, i, offset = 0;
437 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
438
Antti Palosaarif2247492012-09-12 20:23:50 -0300439 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300440
441 /* IR remote controller */
442 req.addr = AF9015_EEPROM_IR_MODE;
443 /* first message will timeout often due to possible hw bug */
444 for (i = 0; i < 4; i++) {
445 ret = af9015_ctrl_msg(d, &req);
446 if (!ret)
447 break;
448 }
449 if (ret)
450 goto error;
451
452 ret = af9015_eeprom_hash(d);
453 if (ret)
454 goto error;
455
Antti Palosaaria3645e52012-06-07 20:36:35 -0300456 state->ir_mode = val;
Antti Palosaarif2247492012-09-12 20:23:50 -0300457 dev_dbg(&d->udev->dev, "%s: IR mode=%d\n", __func__, val);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300458
459 /* TS mode - one or two receivers */
460 req.addr = AF9015_EEPROM_TS_MODE;
461 ret = af9015_ctrl_msg(d, &req);
462 if (ret)
463 goto error;
464
465 state->dual_mode = val;
Antti Palosaarif2247492012-09-12 20:23:50 -0300466 dev_dbg(&d->udev->dev, "%s: TS mode=%d\n", __func__, state->dual_mode);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300467
468 /* disable 2nd adapter because we don't have PID-filters */
469 if (d->udev->speed == USB_SPEED_FULL)
470 state->dual_mode = 0;
471
472 if (state->dual_mode) {
473 /* read 2nd demodulator I2C address */
474 req.addr = AF9015_EEPROM_DEMOD2_I2C;
475 ret = af9015_ctrl_msg(d, &req);
476 if (ret)
477 goto error;
478
479 state->af9013_config[1].i2c_addr = val;
480 }
481
482 for (i = 0; i < state->dual_mode + 1; i++) {
483 if (i == 1)
484 offset = AF9015_EEPROM_OFFSET;
485 /* xtal */
486 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
487 ret = af9015_ctrl_msg(d, &req);
488 if (ret)
489 goto error;
490 switch (val) {
491 case 0:
492 state->af9013_config[i].clock = 28800000;
493 break;
494 case 1:
495 state->af9013_config[i].clock = 20480000;
496 break;
497 case 2:
498 state->af9013_config[i].clock = 28000000;
499 break;
500 case 3:
501 state->af9013_config[i].clock = 25000000;
502 break;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300503 }
Antti Palosaarif2247492012-09-12 20:23:50 -0300504 dev_dbg(&d->udev->dev, "%s: [%d] xtal=%d set clock=%d\n",
505 __func__, i, val,
506 state->af9013_config[i].clock);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300507
508 /* IF frequency */
509 req.addr = AF9015_EEPROM_IF1H + offset;
510 ret = af9015_ctrl_msg(d, &req);
511 if (ret)
512 goto error;
513
514 state->af9013_config[i].if_frequency = val << 8;
515
516 req.addr = AF9015_EEPROM_IF1L + offset;
517 ret = af9015_ctrl_msg(d, &req);
518 if (ret)
519 goto error;
520
521 state->af9013_config[i].if_frequency += val;
522 state->af9013_config[i].if_frequency *= 1000;
Antti Palosaarif2247492012-09-12 20:23:50 -0300523 dev_dbg(&d->udev->dev, "%s: [%d] IF frequency=%d\n", __func__,
524 i, state->af9013_config[i].if_frequency);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300525
526 /* MT2060 IF1 */
527 req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
528 ret = af9015_ctrl_msg(d, &req);
529 if (ret)
530 goto error;
531 state->mt2060_if1[i] = val << 8;
532 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
533 ret = af9015_ctrl_msg(d, &req);
534 if (ret)
535 goto error;
536 state->mt2060_if1[i] += val;
Antti Palosaarif2247492012-09-12 20:23:50 -0300537 dev_dbg(&d->udev->dev, "%s: [%d] MT2060 IF1=%d\n", __func__, i,
Antti Palosaaria3645e52012-06-07 20:36:35 -0300538 state->mt2060_if1[i]);
539
540 /* tuner */
541 req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
542 ret = af9015_ctrl_msg(d, &req);
543 if (ret)
544 goto error;
545 switch (val) {
546 case AF9013_TUNER_ENV77H11D5:
547 case AF9013_TUNER_MT2060:
548 case AF9013_TUNER_QT1010:
549 case AF9013_TUNER_UNKNOWN:
550 case AF9013_TUNER_MT2060_2:
551 case AF9013_TUNER_TDA18271:
552 case AF9013_TUNER_QT1010A:
553 case AF9013_TUNER_TDA18218:
554 state->af9013_config[i].spec_inv = 1;
555 break;
556 case AF9013_TUNER_MXL5003D:
557 case AF9013_TUNER_MXL5005D:
558 case AF9013_TUNER_MXL5005R:
559 case AF9013_TUNER_MXL5007T:
560 state->af9013_config[i].spec_inv = 0;
561 break;
562 case AF9013_TUNER_MC44S803:
563 state->af9013_config[i].gpio[1] = AF9013_GPIO_LO;
564 state->af9013_config[i].spec_inv = 1;
565 break;
566 default:
Antti Palosaarif2247492012-09-12 20:23:50 -0300567 dev_err(&d->udev->dev, "%s: tuner id=%d not " \
568 "supported, please report!\n",
569 KBUILD_MODNAME, val);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300570 return -ENODEV;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -0300571 }
Antti Palosaaria3645e52012-06-07 20:36:35 -0300572
573 state->af9013_config[i].tuner = val;
Antti Palosaarif2247492012-09-12 20:23:50 -0300574 dev_dbg(&d->udev->dev, "%s: [%d] tuner id=%d\n",
575 __func__, i, val);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300576 }
Antti Palosaari1e8750c2011-03-18 19:36:42 -0300577
Antti Palosaari80619de2008-09-15 17:18:09 -0300578error:
579 if (ret)
Antti Palosaarif2247492012-09-12 20:23:50 -0300580 dev_err(&d->udev->dev, "%s: eeprom read failed=%d\n",
581 KBUILD_MODNAME, ret);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300582
583 /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
584 content :-( Override some wrong values here. Ditto for the
585 AVerTV Red HD+ (A850T) device. */
586 if (le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
587 ((le16_to_cpu(d->udev->descriptor.idProduct) ==
588 USB_PID_AVERMEDIA_A850) ||
589 (le16_to_cpu(d->udev->descriptor.idProduct) ==
590 USB_PID_AVERMEDIA_A850T))) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300591 dev_dbg(&d->udev->dev,
592 "%s: AverMedia A850: overriding config\n",
593 __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300594 /* disable dual mode */
595 state->dual_mode = 0;
596
597 /* set correct IF */
598 state->af9013_config[0].if_frequency = 4570000;
599 }
600
601 return ret;
602}
603
Antti Palosaarib905a2a2012-06-18 22:54:16 -0300604static int af9015_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
Antti Palosaaria3645e52012-06-07 20:36:35 -0300605 struct usb_data_stream_properties *stream)
606{
Antti Palosaarif2247492012-09-12 20:23:50 -0300607 struct dvb_usb_device *d = fe_to_d(fe);
608 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300609
Antti Palosaarif2247492012-09-12 20:23:50 -0300610 if (d->udev->speed == USB_SPEED_FULL)
Antti Palosaaria3645e52012-06-07 20:36:35 -0300611 stream->u.bulk.buffersize = TS_USB11_FRAME_SIZE;
612
613 return 0;
614}
615
616static int af9015_get_adapter_count(struct dvb_usb_device *d)
617{
Antti Palosaarie8089662012-06-16 18:13:06 -0300618 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300619 return state->dual_mode + 1;
620}
621
622/* override demod callbacks for resource locking */
623static int af9015_af9013_set_frontend(struct dvb_frontend *fe)
624{
625 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300626 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300627
628 if (mutex_lock_interruptible(&state->fe_mutex))
629 return -EAGAIN;
630
Antti Palosaarie8089662012-06-16 18:13:06 -0300631 ret = state->set_frontend[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300632
633 mutex_unlock(&state->fe_mutex);
634
635 return ret;
636}
637
638/* override demod callbacks for resource locking */
639static int af9015_af9013_read_status(struct dvb_frontend *fe,
640 fe_status_t *status)
641{
642 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300643 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300644
645 if (mutex_lock_interruptible(&state->fe_mutex))
646 return -EAGAIN;
647
Antti Palosaarie8089662012-06-16 18:13:06 -0300648 ret = state->read_status[fe_to_adap(fe)->id](fe, status);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300649
650 mutex_unlock(&state->fe_mutex);
651
652 return ret;
653}
654
655/* override demod callbacks for resource locking */
656static int af9015_af9013_init(struct dvb_frontend *fe)
657{
658 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300659 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300660
661 if (mutex_lock_interruptible(&state->fe_mutex))
662 return -EAGAIN;
663
Antti Palosaarie8089662012-06-16 18:13:06 -0300664 ret = state->init[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300665
666 mutex_unlock(&state->fe_mutex);
667
668 return ret;
669}
670
671/* override demod callbacks for resource locking */
672static int af9015_af9013_sleep(struct dvb_frontend *fe)
673{
674 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300675 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300676
677 if (mutex_lock_interruptible(&state->fe_mutex))
678 return -EAGAIN;
679
Antti Palosaarie8089662012-06-16 18:13:06 -0300680 ret = state->sleep[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300681
682 mutex_unlock(&state->fe_mutex);
683
684 return ret;
685}
686
687/* override tuner callbacks for resource locking */
688static int af9015_tuner_init(struct dvb_frontend *fe)
689{
690 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300691 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300692
693 if (mutex_lock_interruptible(&state->fe_mutex))
694 return -EAGAIN;
695
Antti Palosaarie8089662012-06-16 18:13:06 -0300696 ret = state->tuner_init[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300697
698 mutex_unlock(&state->fe_mutex);
699
700 return ret;
701}
702
703/* override tuner callbacks for resource locking */
704static int af9015_tuner_sleep(struct dvb_frontend *fe)
705{
706 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300707 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300708
709 if (mutex_lock_interruptible(&state->fe_mutex))
710 return -EAGAIN;
711
Antti Palosaarie8089662012-06-16 18:13:06 -0300712 ret = state->tuner_sleep[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300713
714 mutex_unlock(&state->fe_mutex);
715
Antti Palosaari80619de2008-09-15 17:18:09 -0300716 return ret;
717}
718
719static int af9015_copy_firmware(struct dvb_usb_device *d)
720{
Antti Palosaarie8089662012-06-16 18:13:06 -0300721 struct af9015_state *state = d_to_priv(d);
Antti Palosaari80619de2008-09-15 17:18:09 -0300722 int ret;
723 u8 fw_params[4];
724 u8 val, i;
725 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
726 fw_params };
Antti Palosaarif2247492012-09-12 20:23:50 -0300727 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -0300728
Antti Palosaaria3645e52012-06-07 20:36:35 -0300729 fw_params[0] = state->firmware_size >> 8;
730 fw_params[1] = state->firmware_size & 0xff;
731 fw_params[2] = state->firmware_checksum >> 8;
732 fw_params[3] = state->firmware_checksum & 0xff;
Antti Palosaari80619de2008-09-15 17:18:09 -0300733
734 /* wait 2nd demodulator ready */
735 msleep(100);
736
Antti Palosaaria3645e52012-06-07 20:36:35 -0300737 ret = af9015_read_reg_i2c(d, state->af9013_config[1].i2c_addr,
738 0x98be, &val);
Antti Palosaari80619de2008-09-15 17:18:09 -0300739 if (ret)
740 goto error;
741 else
Antti Palosaarif2247492012-09-12 20:23:50 -0300742 dev_dbg(&d->udev->dev, "%s: firmware status=%02x\n",
743 __func__, val);
Antti Palosaari80619de2008-09-15 17:18:09 -0300744
745 if (val == 0x0c) /* fw is running, no need for download */
746 goto exit;
747
748 /* set I2C master clock to fast (to speed up firmware copy) */
749 ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
750 if (ret)
751 goto error;
752
753 msleep(50);
754
755 /* copy firmware */
756 ret = af9015_ctrl_msg(d, &req);
757 if (ret)
Antti Palosaarif2247492012-09-12 20:23:50 -0300758 dev_err(&d->udev->dev, "%s: firmware copy cmd failed=%d\n",
759 KBUILD_MODNAME, ret);
760
761 dev_dbg(&d->udev->dev, "%s: firmware copy done\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -0300762
763 /* set I2C master clock back to normal */
764 ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
765 if (ret)
766 goto error;
767
768 /* request boot firmware */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300769 ret = af9015_write_reg_i2c(d, state->af9013_config[1].i2c_addr,
770 0xe205, 1);
Antti Palosaarif2247492012-09-12 20:23:50 -0300771 dev_dbg(&d->udev->dev, "%s: firmware boot cmd status=%d\n",
772 __func__, ret);
Antti Palosaari80619de2008-09-15 17:18:09 -0300773 if (ret)
774 goto error;
775
776 for (i = 0; i < 15; i++) {
777 msleep(100);
778
779 /* check firmware status */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300780 ret = af9015_read_reg_i2c(d, state->af9013_config[1].i2c_addr,
781 0x98be, &val);
Antti Palosaarif2247492012-09-12 20:23:50 -0300782 dev_dbg(&d->udev->dev, "%s: firmware status cmd status=%d " \
783 "firmware status=%02x\n", __func__, ret, val);
Antti Palosaari80619de2008-09-15 17:18:09 -0300784 if (ret)
785 goto error;
786
787 if (val == 0x0c || val == 0x04) /* success or fail */
788 break;
789 }
790
791 if (val == 0x04) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300792 dev_err(&d->udev->dev, "%s: firmware did not run\n",
793 KBUILD_MODNAME);
Antti Palosaarie1e9e512012-09-12 20:23:52 -0300794 ret = -ETIMEDOUT;
Antti Palosaari80619de2008-09-15 17:18:09 -0300795 } else if (val != 0x0c) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300796 dev_err(&d->udev->dev, "%s: firmware boot timeout\n",
797 KBUILD_MODNAME);
Antti Palosaarie1e9e512012-09-12 20:23:52 -0300798 ret = -ETIMEDOUT;
Antti Palosaari80619de2008-09-15 17:18:09 -0300799 }
800
801error:
802exit:
803 return ret;
804}
805
Antti Palosaari80619de2008-09-15 17:18:09 -0300806static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
807{
808 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300809 struct af9015_state *state = adap_to_priv(adap);
Antti Palosaari80619de2008-09-15 17:18:09 -0300810
Antti Palosaaria3645e52012-06-07 20:36:35 -0300811 if (adap->id == 0) {
812 state->af9013_config[0].ts_mode = AF9013_TS_USB;
813 memcpy(state->af9013_config[0].api_version, "\x0\x1\x9\x0", 4);
814 state->af9013_config[0].gpio[0] = AF9013_GPIO_HI;
815 state->af9013_config[0].gpio[3] = AF9013_GPIO_TUNER_ON;
816 } else if (adap->id == 1) {
817 state->af9013_config[1].ts_mode = AF9013_TS_SERIAL;
818 memcpy(state->af9013_config[1].api_version, "\x0\x1\x9\x0", 4);
819 state->af9013_config[1].gpio[0] = AF9013_GPIO_TUNER_ON;
820 state->af9013_config[1].gpio[1] = AF9013_GPIO_LO;
821
Antti Palosaari80619de2008-09-15 17:18:09 -0300822 /* copy firmware to 2nd demodulator */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300823 if (state->dual_mode) {
Antti Palosaarie8089662012-06-16 18:13:06 -0300824 ret = af9015_copy_firmware(adap_to_d(adap));
Antti Palosaari80619de2008-09-15 17:18:09 -0300825 if (ret) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300826 dev_err(&adap_to_d(adap)->udev->dev,
827 "%s: firmware copy to 2nd " \
828 "frontend failed, will " \
829 "disable it\n", KBUILD_MODNAME);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300830 state->dual_mode = 0;
Antti Palosaari80619de2008-09-15 17:18:09 -0300831 return -ENODEV;
832 }
833 } else {
834 return -ENODEV;
835 }
836 }
837
838 /* attach demodulator */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300839 adap->fe[0] = dvb_attach(af9013_attach,
Antti Palosaarie8089662012-06-16 18:13:06 -0300840 &state->af9013_config[adap->id], &adap_to_d(adap)->i2c_adap);
Antti Palosaari80619de2008-09-15 17:18:09 -0300841
Antti Palosaarie90ab842011-11-12 22:33:30 -0300842 /*
843 * AF9015 firmware does not like if it gets interrupted by I2C adapter
844 * request on some critical phases. During normal operation I2C adapter
845 * is used only 2nd demodulator and tuner on dual tuner devices.
846 * Override demodulator callbacks and use mutex for limit access to
847 * those "critical" paths to keep AF9015 happy.
Antti Palosaarie90ab842011-11-12 22:33:30 -0300848 */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300849 if (adap->fe[0]) {
Antti Palosaarie90ab842011-11-12 22:33:30 -0300850 state->set_frontend[adap->id] =
Antti Palosaaria3645e52012-06-07 20:36:35 -0300851 adap->fe[0]->ops.set_frontend;
852 adap->fe[0]->ops.set_frontend =
Antti Palosaarie90ab842011-11-12 22:33:30 -0300853 af9015_af9013_set_frontend;
854
855 state->read_status[adap->id] =
Antti Palosaaria3645e52012-06-07 20:36:35 -0300856 adap->fe[0]->ops.read_status;
857 adap->fe[0]->ops.read_status =
Antti Palosaarie90ab842011-11-12 22:33:30 -0300858 af9015_af9013_read_status;
859
Antti Palosaaria3645e52012-06-07 20:36:35 -0300860 state->init[adap->id] = adap->fe[0]->ops.init;
861 adap->fe[0]->ops.init = af9015_af9013_init;
Antti Palosaarie90ab842011-11-12 22:33:30 -0300862
Antti Palosaaria3645e52012-06-07 20:36:35 -0300863 state->sleep[adap->id] = adap->fe[0]->ops.sleep;
864 adap->fe[0]->ops.sleep = af9015_af9013_sleep;
Antti Palosaarie90ab842011-11-12 22:33:30 -0300865 }
866
Antti Palosaaria3645e52012-06-07 20:36:35 -0300867 return adap->fe[0] == NULL ? -ENODEV : 0;
Antti Palosaari80619de2008-09-15 17:18:09 -0300868}
869
870static struct mt2060_config af9015_mt2060_config = {
871 .i2c_address = 0xc0,
872 .clock_out = 0,
873};
874
875static struct qt1010_config af9015_qt1010_config = {
876 .i2c_address = 0xc4,
877};
878
879static struct tda18271_config af9015_tda18271_config = {
880 .gate = TDA18271_GATE_DIGITAL,
Mauro Carvalho Chehab7655e592010-10-27 14:55:34 -0200881 .small_i2c = TDA18271_16_BYTE_CHUNK_INIT,
Antti Palosaari80619de2008-09-15 17:18:09 -0300882};
883
884static struct mxl5005s_config af9015_mxl5003_config = {
885 .i2c_address = 0xc6,
886 .if_freq = IF_FREQ_4570000HZ,
887 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
888 .agc_mode = MXL_SINGLE_AGC,
889 .tracking_filter = MXL_TF_DEFAULT,
Antti Palosaaria1310772008-09-22 13:59:25 -0300890 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -0300891 .cap_select = MXL_CAP_SEL_ENABLE,
892 .div_out = MXL_DIV_OUT_4,
893 .clock_out = MXL_CLOCK_OUT_DISABLE,
894 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
895 .top = MXL5005S_TOP_25P2,
896 .mod_mode = MXL_DIGITAL_MODE,
897 .if_mode = MXL_ZERO_IF,
898 .AgcMasterByte = 0x00,
899};
900
901static struct mxl5005s_config af9015_mxl5005_config = {
902 .i2c_address = 0xc6,
903 .if_freq = IF_FREQ_4570000HZ,
904 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
905 .agc_mode = MXL_SINGLE_AGC,
906 .tracking_filter = MXL_TF_OFF,
Antti Palosaaria1310772008-09-22 13:59:25 -0300907 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -0300908 .cap_select = MXL_CAP_SEL_ENABLE,
909 .div_out = MXL_DIV_OUT_4,
910 .clock_out = MXL_CLOCK_OUT_DISABLE,
911 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
912 .top = MXL5005S_TOP_25P2,
913 .mod_mode = MXL_DIGITAL_MODE,
914 .if_mode = MXL_ZERO_IF,
915 .AgcMasterByte = 0x00,
916};
917
Jochen Friedrichd5633992009-02-02 14:59:50 -0300918static struct mc44s803_config af9015_mc44s803_config = {
919 .i2c_address = 0xc0,
920 .dig_out = 1,
921};
922
Antti Palosaariee3d4402010-08-13 03:51:26 -0300923static struct tda18218_config af9015_tda18218_config = {
924 .i2c_address = 0xc0,
925 .i2c_wr_max = 21, /* max wr bytes AF9015 I2C adap can handle at once */
926};
927
Antti Palosaariab07fdd2010-09-09 14:59:10 -0300928static struct mxl5007t_config af9015_mxl5007t_config = {
929 .xtal_freq_hz = MxL_XTAL_24_MHZ,
930 .if_freq_hz = MxL_IF_4_57_MHZ,
931};
932
Antti Palosaari80619de2008-09-15 17:18:09 -0300933static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
934{
Antti Palosaarif2247492012-09-12 20:23:50 -0300935 struct dvb_usb_device *d = adap_to_d(adap);
936 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300937 int ret;
Antti Palosaarif2247492012-09-12 20:23:50 -0300938 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -0300939
Antti Palosaaria3645e52012-06-07 20:36:35 -0300940 switch (state->af9013_config[adap->id].tuner) {
Antti Palosaari80619de2008-09-15 17:18:09 -0300941 case AF9013_TUNER_MT2060:
942 case AF9013_TUNER_MT2060_2:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300943 ret = dvb_attach(mt2060_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300944 &adap_to_d(adap)->i2c_adap, &af9015_mt2060_config,
Antti Palosaaria3645e52012-06-07 20:36:35 -0300945 state->mt2060_if1[adap->id])
Antti Palosaari80619de2008-09-15 17:18:09 -0300946 == NULL ? -ENODEV : 0;
947 break;
948 case AF9013_TUNER_QT1010:
949 case AF9013_TUNER_QT1010A:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300950 ret = dvb_attach(qt1010_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300951 &adap_to_d(adap)->i2c_adap,
Antti Palosaari80619de2008-09-15 17:18:09 -0300952 &af9015_qt1010_config) == NULL ? -ENODEV : 0;
953 break;
954 case AF9013_TUNER_TDA18271:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300955 ret = dvb_attach(tda18271_attach, adap->fe[0], 0xc0,
Antti Palosaarie8089662012-06-16 18:13:06 -0300956 &adap_to_d(adap)->i2c_adap,
Antti Palosaari80619de2008-09-15 17:18:09 -0300957 &af9015_tda18271_config) == NULL ? -ENODEV : 0;
958 break;
Antti Palosaariee3d4402010-08-13 03:51:26 -0300959 case AF9013_TUNER_TDA18218:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300960 ret = dvb_attach(tda18218_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300961 &adap_to_d(adap)->i2c_adap,
Antti Palosaariee3d4402010-08-13 03:51:26 -0300962 &af9015_tda18218_config) == NULL ? -ENODEV : 0;
963 break;
Antti Palosaari80619de2008-09-15 17:18:09 -0300964 case AF9013_TUNER_MXL5003D:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300965 ret = dvb_attach(mxl5005s_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300966 &adap_to_d(adap)->i2c_adap,
Antti Palosaari80619de2008-09-15 17:18:09 -0300967 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
968 break;
969 case AF9013_TUNER_MXL5005D:
970 case AF9013_TUNER_MXL5005R:
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_mxl5005_config) == NULL ? -ENODEV : 0;
974 break;
975 case AF9013_TUNER_ENV77H11D5:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300976 ret = dvb_attach(dvb_pll_attach, adap->fe[0], 0xc0,
Antti Palosaarie8089662012-06-16 18:13:06 -0300977 &adap_to_d(adap)->i2c_adap,
Antti Palosaari80619de2008-09-15 17:18:09 -0300978 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
979 break;
980 case AF9013_TUNER_MC44S803:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300981 ret = dvb_attach(mc44s803_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300982 &adap_to_d(adap)->i2c_adap,
Jochen Friedrichd5633992009-02-02 14:59:50 -0300983 &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
Antti Palosaari80619de2008-09-15 17:18:09 -0300984 break;
Antti Palosaariab07fdd2010-09-09 14:59:10 -0300985 case AF9013_TUNER_MXL5007T:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300986 ret = dvb_attach(mxl5007t_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300987 &adap_to_d(adap)->i2c_adap,
Antti Palosaariab07fdd2010-09-09 14:59:10 -0300988 0xc0, &af9015_mxl5007t_config) == NULL ? -ENODEV : 0;
989 break;
Antti Palosaari80619de2008-09-15 17:18:09 -0300990 case AF9013_TUNER_UNKNOWN:
991 default:
Antti Palosaarif2247492012-09-12 20:23:50 -0300992 dev_err(&d->udev->dev, "%s: unknown tuner id=%d\n",
993 KBUILD_MODNAME,
994 state->af9013_config[adap->id].tuner);
Antti Palosaari80619de2008-09-15 17:18:09 -0300995 ret = -ENODEV;
Antti Palosaari80619de2008-09-15 17:18:09 -0300996 }
Gordon Heckerbe4a5e72012-03-14 10:27:30 -0300997
Antti Palosaaria3645e52012-06-07 20:36:35 -0300998 if (adap->fe[0]->ops.tuner_ops.init) {
Antti Palosaari6d535bd2012-03-14 10:27:31 -0300999 state->tuner_init[adap->id] =
Antti Palosaaria3645e52012-06-07 20:36:35 -03001000 adap->fe[0]->ops.tuner_ops.init;
1001 adap->fe[0]->ops.tuner_ops.init = af9015_tuner_init;
Antti Palosaari6d535bd2012-03-14 10:27:31 -03001002 }
Gordon Heckerbe4a5e72012-03-14 10:27:30 -03001003
Antti Palosaaria3645e52012-06-07 20:36:35 -03001004 if (adap->fe[0]->ops.tuner_ops.sleep) {
Antti Palosaari6d535bd2012-03-14 10:27:31 -03001005 state->tuner_sleep[adap->id] =
Antti Palosaaria3645e52012-06-07 20:36:35 -03001006 adap->fe[0]->ops.tuner_ops.sleep;
1007 adap->fe[0]->ops.tuner_ops.sleep = af9015_tuner_sleep;
Antti Palosaari6d535bd2012-03-14 10:27:31 -03001008 }
1009
Antti Palosaari80619de2008-09-15 17:18:09 -03001010 return ret;
1011}
1012
Antti Palosaaria3645e52012-06-07 20:36:35 -03001013static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1014{
Antti Palosaarif2247492012-09-12 20:23:50 -03001015 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001016 int ret;
Antti Palosaarif2247492012-09-12 20:23:50 -03001017 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001018
1019 if (onoff)
Antti Palosaarif2247492012-09-12 20:23:50 -03001020 ret = af9015_set_reg_bit(d, 0xd503, 0);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001021 else
Antti Palosaarif2247492012-09-12 20:23:50 -03001022 ret = af9015_clear_reg_bit(d, 0xd503, 0);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001023
1024 return ret;
1025}
1026
1027static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1028 int onoff)
1029{
Antti Palosaarif2247492012-09-12 20:23:50 -03001030 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001031 int ret;
1032 u8 idx;
Antti Palosaarif2247492012-09-12 20:23:50 -03001033 dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
1034 __func__, index, pid, onoff);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001035
Antti Palosaarif2247492012-09-12 20:23:50 -03001036 ret = af9015_write_reg(d, 0xd505, (pid & 0xff));
Antti Palosaaria3645e52012-06-07 20:36:35 -03001037 if (ret)
1038 goto error;
1039
Antti Palosaarif2247492012-09-12 20:23:50 -03001040 ret = af9015_write_reg(d, 0xd506, (pid >> 8));
Antti Palosaaria3645e52012-06-07 20:36:35 -03001041 if (ret)
1042 goto error;
1043
1044 idx = ((index & 0x1f) | (1 << 5));
Antti Palosaarif2247492012-09-12 20:23:50 -03001045 ret = af9015_write_reg(d, 0xd504, idx);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001046
1047error:
1048 return ret;
1049}
1050
1051static int af9015_init_endpoint(struct dvb_usb_device *d)
1052{
Antti Palosaarie8089662012-06-16 18:13:06 -03001053 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001054 int ret;
1055 u16 frame_size;
1056 u8 packet_size;
Antti Palosaarif2247492012-09-12 20:23:50 -03001057 dev_dbg(&d->udev->dev, "%s: USB speed=%d\n", __func__, d->udev->speed);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001058
1059 if (d->udev->speed == USB_SPEED_FULL) {
1060 frame_size = TS_USB11_FRAME_SIZE/4;
1061 packet_size = TS_USB11_MAX_PACKET_SIZE/4;
1062 } else {
1063 frame_size = TS_USB20_FRAME_SIZE/4;
1064 packet_size = TS_USB20_MAX_PACKET_SIZE/4;
1065 }
1066
1067 ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
1068 if (ret)
1069 goto error;
1070 ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
1071 if (ret)
1072 goto error;
1073 ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
1074 if (ret)
1075 goto error;
1076 ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
1077 if (ret)
1078 goto error;
1079 ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
1080 if (ret)
1081 goto error;
1082 if (state->dual_mode) {
1083 ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
1084 if (ret)
1085 goto error;
1086 }
1087 ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
1088 if (ret)
1089 goto error;
1090 if (state->dual_mode) {
1091 ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
1092 if (ret)
1093 goto error;
1094 }
1095 /* EP4 xfer length */
1096 ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
1097 if (ret)
1098 goto error;
1099 ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
1100 if (ret)
1101 goto error;
1102 /* EP5 xfer length */
1103 ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
1104 if (ret)
1105 goto error;
1106 ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
1107 if (ret)
1108 goto error;
1109 ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
1110 if (ret)
1111 goto error;
1112 ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
1113 if (ret)
1114 goto error;
1115 ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
1116 if (ret)
1117 goto error;
1118 if (state->dual_mode) {
1119 ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
1120 if (ret)
1121 goto error;
1122 }
1123
1124 /* enable / disable mp2if2 */
1125 if (state->dual_mode)
1126 ret = af9015_set_reg_bit(d, 0xd50b, 0);
1127 else
1128 ret = af9015_clear_reg_bit(d, 0xd50b, 0);
1129
1130error:
1131 if (ret)
Antti Palosaarif2247492012-09-12 20:23:50 -03001132 dev_err(&d->udev->dev, "%s: endpoint init failed=%d\n",
1133 KBUILD_MODNAME, ret);
1134
Antti Palosaaria3645e52012-06-07 20:36:35 -03001135 return ret;
1136}
1137
1138static int af9015_init(struct dvb_usb_device *d)
1139{
Antti Palosaarie8089662012-06-16 18:13:06 -03001140 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001141 int ret;
Antti Palosaarif2247492012-09-12 20:23:50 -03001142 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001143
1144 mutex_init(&state->fe_mutex);
1145
1146 /* init RC canary */
1147 ret = af9015_write_reg(d, 0x98e9, 0xff);
1148 if (ret)
1149 goto error;
1150
1151 ret = af9015_init_endpoint(d);
1152 if (ret)
1153 goto error;
1154
1155error:
1156 return ret;
1157}
1158
1159struct af9015_rc_setup {
1160 unsigned int id;
1161 char *rc_codes;
Jonathan Niederd07b9012012-01-07 04:11:27 -03001162};
1163
Antti Palosaaria3645e52012-06-07 20:36:35 -03001164static char *af9015_rc_setup_match(unsigned int id,
1165 const struct af9015_rc_setup *table)
1166{
1167 for (; table->rc_codes; table++)
1168 if (table->id == id)
1169 return table->rc_codes;
1170 return NULL;
1171}
1172
1173static const struct af9015_rc_setup af9015_rc_setup_modparam[] = {
1174 { AF9015_REMOTE_A_LINK_DTU_M, RC_MAP_ALINK_DTU_M },
1175 { AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3, RC_MAP_MSI_DIGIVOX_II },
1176 { AF9015_REMOTE_MYGICTV_U718, RC_MAP_TOTAL_MEDIA_IN_HAND },
1177 { AF9015_REMOTE_DIGITTRADE_DVB_T, RC_MAP_DIGITTRADE },
1178 { AF9015_REMOTE_AVERMEDIA_KS, RC_MAP_AVERMEDIA_RM_KS },
Jonathan Niederd07b9012012-01-07 04:11:27 -03001179 { }
Antti Palosaari80619de2008-09-15 17:18:09 -03001180};
Antti Palosaari80619de2008-09-15 17:18:09 -03001181
Antti Palosaaria3645e52012-06-07 20:36:35 -03001182static const struct af9015_rc_setup af9015_rc_setup_hashes[] = {
1183 { 0xb8feb708, RC_MAP_MSI_DIGIVOX_II },
1184 { 0xa3703d00, RC_MAP_ALINK_DTU_M },
1185 { 0x9b7dc64e, RC_MAP_TOTAL_MEDIA_IN_HAND }, /* MYGICTV U718 */
1186 { 0x5d49e3db, RC_MAP_DIGITTRADE }, /* LC-Power LC-USB-DVBT */
1187 { }
1188};
Antti Palosaari80619de2008-09-15 17:18:09 -03001189
Antti Palosaaria3645e52012-06-07 20:36:35 -03001190static int af9015_rc_query(struct dvb_usb_device *d)
1191{
Antti Palosaarie8089662012-06-16 18:13:06 -03001192 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001193 int ret;
1194 u8 buf[17];
Antti Palosaari80619de2008-09-15 17:18:09 -03001195
Antti Palosaaria3645e52012-06-07 20:36:35 -03001196 /* read registers needed to detect remote controller code */
1197 ret = af9015_read_regs(d, 0x98d9, buf, sizeof(buf));
1198 if (ret)
1199 goto error;
Antti Palosaari80619de2008-09-15 17:18:09 -03001200
Antti Palosaaria3645e52012-06-07 20:36:35 -03001201 /* If any of these are non-zero, assume invalid data */
Antti Palosaarif2247492012-09-12 20:23:50 -03001202 if (buf[1] || buf[2] || buf[3]) {
1203 dev_dbg(&d->udev->dev, "%s: invalid data\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001204 return ret;
Antti Palosaarif2247492012-09-12 20:23:50 -03001205 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001206
Antti Palosaaria3645e52012-06-07 20:36:35 -03001207 /* Check for repeat of previous code */
1208 if ((state->rc_repeat != buf[6] || buf[0]) &&
1209 !memcmp(&buf[12], state->rc_last, 4)) {
Antti Palosaarif2247492012-09-12 20:23:50 -03001210 dev_dbg(&d->udev->dev, "%s: key repeated\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001211 rc_keydown(d->rc_dev, state->rc_keycode, 0);
1212 state->rc_repeat = buf[6];
1213 return ret;
1214 }
1215
1216 /* Only process key if canary killed */
1217 if (buf[16] != 0xff && buf[0] != 0x01) {
Antti Palosaarif2247492012-09-12 20:23:50 -03001218 dev_dbg(&d->udev->dev, "%s: key pressed %*ph\n",
1219 __func__, 4, buf + 12);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001220
1221 /* Reset the canary */
1222 ret = af9015_write_reg(d, 0x98e9, 0xff);
1223 if (ret)
1224 goto error;
1225
1226 /* Remember this key */
1227 memcpy(state->rc_last, &buf[12], 4);
1228 if (buf[14] == (u8) ~buf[15]) {
1229 if (buf[12] == (u8) ~buf[13]) {
1230 /* NEC */
1231 state->rc_keycode = buf[12] << 8 | buf[14];
1232 } else {
1233 /* NEC extended*/
1234 state->rc_keycode = buf[12] << 16 |
1235 buf[13] << 8 | buf[14];
Antti Palosaari80619de2008-09-15 17:18:09 -03001236 }
Antti Palosaaria3645e52012-06-07 20:36:35 -03001237 } else {
1238 /* 32 bit NEC */
1239 state->rc_keycode = buf[12] << 24 | buf[13] << 16 |
1240 buf[14] << 8 | buf[15];
Antti Palosaari80619de2008-09-15 17:18:09 -03001241 }
Antti Palosaaria3645e52012-06-07 20:36:35 -03001242 rc_keydown(d->rc_dev, state->rc_keycode, 0);
1243 } else {
Antti Palosaarif2247492012-09-12 20:23:50 -03001244 dev_dbg(&d->udev->dev, "%s: no key press\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001245 /* Invalidate last keypress */
1246 /* Not really needed, but helps with debug */
1247 state->rc_last[2] = state->rc_last[3];
1248 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001249
Antti Palosaaria3645e52012-06-07 20:36:35 -03001250 state->rc_repeat = buf[6];
Antti Palosaarieb29fbe2012-07-24 21:21:04 -03001251 state->rc_failed = false;
Antti Palosaari80619de2008-09-15 17:18:09 -03001252
Antti Palosaaria3645e52012-06-07 20:36:35 -03001253error:
Antti Palosaarieb29fbe2012-07-24 21:21:04 -03001254 if (ret) {
Antti Palosaarif2247492012-09-12 20:23:50 -03001255 dev_warn(&d->udev->dev, "%s: rc query failed=%d\n",
1256 KBUILD_MODNAME, ret);
Antti Palosaari80619de2008-09-15 17:18:09 -03001257
Antti Palosaarieb29fbe2012-07-24 21:21:04 -03001258 /* allow random errors as dvb-usb will stop polling on error */
1259 if (!state->rc_failed)
1260 ret = 0;
1261
1262 state->rc_failed = true;
1263 }
1264
Antti Palosaaria3645e52012-06-07 20:36:35 -03001265 return ret;
1266}
Antti Palosaari80619de2008-09-15 17:18:09 -03001267
Antti Palosaaria3645e52012-06-07 20:36:35 -03001268static int af9015_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1269{
Antti Palosaarie8089662012-06-16 18:13:06 -03001270 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001271 u16 vid = le16_to_cpu(d->udev->descriptor.idVendor);
Antti Palosaari80619de2008-09-15 17:18:09 -03001272
Antti Palosaaria3645e52012-06-07 20:36:35 -03001273 if (state->ir_mode == AF9015_IR_MODE_DISABLED)
1274 return 0;
Antti Palosaari80619de2008-09-15 17:18:09 -03001275
Antti Palosaaria3645e52012-06-07 20:36:35 -03001276 /* try to load remote based module param */
Antti Palosaarieb29fbe2012-07-24 21:21:04 -03001277 if (!rc->map_name)
1278 rc->map_name = af9015_rc_setup_match(dvb_usb_af9015_remote,
1279 af9015_rc_setup_modparam);
Antti Palosaari80619de2008-09-15 17:18:09 -03001280
Antti Palosaaria3645e52012-06-07 20:36:35 -03001281 /* try to load remote based eeprom hash */
1282 if (!rc->map_name)
1283 rc->map_name = af9015_rc_setup_match(state->eeprom_sum,
1284 af9015_rc_setup_hashes);
Antti Palosaari80619de2008-09-15 17:18:09 -03001285
Antti Palosaaria3645e52012-06-07 20:36:35 -03001286 /* try to load remote based USB iManufacturer string */
1287 if (!rc->map_name && vid == USB_VID_AFATECH) {
1288 /* Check USB manufacturer and product strings and try
1289 to determine correct remote in case of chip vendor
1290 reference IDs are used.
1291 DO NOT ADD ANYTHING NEW HERE. Use hashes instead. */
1292 char manufacturer[10];
1293 memset(manufacturer, 0, sizeof(manufacturer));
1294 usb_string(d->udev, d->udev->descriptor.iManufacturer,
1295 manufacturer, sizeof(manufacturer));
1296 if (!strcmp("MSI", manufacturer)) {
1297 /* iManufacturer 1 MSI
1298 iProduct 2 MSI K-VOX */
1299 rc->map_name = af9015_rc_setup_match(
1300 AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
1301 af9015_rc_setup_modparam);
Antti Palosaari80619de2008-09-15 17:18:09 -03001302 }
Antti Palosaaria3645e52012-06-07 20:36:35 -03001303 }
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001304
Antti Palosaaride73bee2012-07-05 19:57:07 -03001305 /* load empty to enable rc */
1306 if (!rc->map_name)
1307 rc->map_name = RC_MAP_EMPTY;
1308
David Härdemanc003ab12012-10-11 19:11:54 -03001309 rc->allowed_protos = RC_BIT_NEC;
Antti Palosaaria3645e52012-06-07 20:36:35 -03001310 rc->query = af9015_rc_query;
1311 rc->interval = 500;
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001312
Antti Palosaaria3645e52012-06-07 20:36:35 -03001313 return 0;
1314}
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001315
Antti Palosaaria3645e52012-06-07 20:36:35 -03001316/* interface 0 is used by DVB-T receiver and
1317 interface 1 is for remote controller (HID) */
1318static struct dvb_usb_device_properties af9015_props = {
1319 .driver_name = KBUILD_MODNAME,
1320 .owner = THIS_MODULE,
1321 .adapter_nr = adapter_nr,
1322 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001323
Antti Palosaaria3645e52012-06-07 20:36:35 -03001324 .generic_bulk_ctrl_endpoint = 0x02,
1325 .generic_bulk_ctrl_endpoint_response = 0x81,
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001326
Antti Palosaaria3645e52012-06-07 20:36:35 -03001327 .identify_state = af9015_identify_state,
Antti Palosaaribab9b4f2012-09-12 11:37:25 -03001328 .firmware = AF9015_FIRMWARE,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001329 .download_firmware = af9015_download_firmware,
1330
Antti Palosaaria3645e52012-06-07 20:36:35 -03001331 .i2c_algo = &af9015_i2c_algo,
Antti Palosaari2d2b37c72012-06-12 01:05:20 -03001332 .read_config = af9015_read_config,
1333 .frontend_attach = af9015_af9013_frontend_attach,
1334 .tuner_attach = af9015_tuner_attach,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001335 .init = af9015_init,
1336 .get_rc_config = af9015_get_rc_config,
Antti Palosaarib905a2a2012-06-18 22:54:16 -03001337 .get_stream_config = af9015_get_stream_config,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001338
1339 .get_adapter_count = af9015_get_adapter_count,
1340 .adapter = {
1341 {
1342 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1343 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1344 .pid_filter_count = 32,
1345 .pid_filter = af9015_pid_filter,
1346 .pid_filter_ctrl = af9015_pid_filter_ctrl,
Antti Palosaari1a590012012-06-16 16:25:22 -03001347
1348 .stream = DVB_USB_STREAM_BULK(0x84, 8, TS_USB20_FRAME_SIZE),
1349 }, {
1350 .stream = DVB_USB_STREAM_BULK(0x85, 8, TS_USB20_FRAME_SIZE),
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001351 },
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001352 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001353};
Antti Palosaari80619de2008-09-15 17:18:09 -03001354
Antti Palosaaria3645e52012-06-07 20:36:35 -03001355static const struct usb_device_id af9015_id_table[] = {
1356 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015,
1357 &af9015_props, "Afatech AF9015 reference design", NULL) },
1358 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016,
1359 &af9015_props, "Afatech AF9015 reference design", NULL) },
1360 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD,
1361 &af9015_props, "Leadtek WinFast DTV Dongle Gold", RC_MAP_LEADTEK_Y04G0051) },
1362 { DVB_USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E,
1363 &af9015_props, "Pinnacle PCTV 71e", NULL) },
1364 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U,
1365 &af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) },
1366 { DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TINYTWIN,
1367 &af9015_props, "DigitalNow TinyTwin", RC_MAP_AZUREWAVE_AD_TU700) },
1368 { DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_AZUREWAVE_AD_TU700,
1369 &af9015_props, "TwinHan AzureWave AD-TU700(704J)", RC_MAP_AZUREWAVE_AD_TU700) },
1370 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2,
1371 &af9015_props, "TerraTec Cinergy T USB XE", NULL) },
1372 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T,
1373 &af9015_props, "KWorld PlusTV Dual DVB-T PCI (DVB-T PC160-2T)", NULL) },
1374 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X,
1375 &af9015_props, "AVerMedia AVerTV DVB-T Volar X", RC_MAP_AVERMEDIA_M135A) },
1376 { DVB_USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380,
1377 &af9015_props, "Xtensions XD-380", NULL) },
1378 { DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO,
1379 &af9015_props, "MSI DIGIVOX Duo", RC_MAP_MSI_DIGIVOX_III) },
1380 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2,
1381 &af9015_props, "Fujitsu-Siemens Slim Mobile USB DVB-T", NULL) },
1382 { DVB_USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2,
1383 &af9015_props, "Telestar Starstick 2", NULL) },
1384 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309,
1385 &af9015_props, "AVerMedia A309", NULL) },
1386 { DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III,
1387 &af9015_props, "MSI Digi VOX mini III", RC_MAP_MSI_DIGIVOX_III) },
1388 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U,
1389 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1390 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2,
1391 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1392 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3,
1393 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1394 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT,
1395 &af9015_props, "TrekStor DVB-T USB Stick", RC_MAP_TREKSTOR) },
1396 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850,
1397 &af9015_props, "AverMedia AVerTV Volar Black HD (A850)", NULL) },
1398 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805,
1399 &af9015_props, "AverMedia AVerTV Volar GPS 805 (A805)", NULL) },
1400 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU,
1401 &af9015_props, "Conceptronic USB2.0 DVB-T CTVDIGRCU V3.0", NULL) },
1402 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810,
1403 &af9015_props, "KWorld Digial MC-810", NULL) },
1404 { DVB_USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03,
1405 &af9015_props, "Genius TVGo DVB-T03", NULL) },
1406 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2,
1407 &af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) },
1408 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T,
1409 &af9015_props, "KWorld PlusTV DVB-T PCI Pro Card (DVB-T PC160-T)", NULL) },
1410 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20,
1411 &af9015_props, "Sveon STV20 Tuner USB DVB-T HDTV", NULL) },
1412 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2,
1413 &af9015_props, "DigitalNow TinyTwin v2", RC_MAP_DIGITALNOW_TINYTWIN) },
1414 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS,
1415 &af9015_props, "Leadtek WinFast DTV2000DS", RC_MAP_LEADTEK_Y04G0051) },
1416 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T,
1417 &af9015_props, "KWorld USB DVB-T Stick Mobile (UB383-T)", NULL) },
1418 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4,
1419 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1420 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M,
1421 &af9015_props, "AverMedia AVerTV Volar M (A815Mac)", NULL) },
1422 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_RC,
1423 &af9015_props, "TerraTec Cinergy T Stick RC", RC_MAP_TERRATEC_SLIM_2) },
1424 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC,
1425 &af9015_props, "TerraTec Cinergy T Stick Dual RC", RC_MAP_TERRATEC_SLIM) },
1426 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850T,
1427 &af9015_props, "AverMedia AVerTV Red HD+ (A850T)", NULL) },
1428 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TINYTWIN_3,
1429 &af9015_props, "DigitalNow TinyTwin v3", RC_MAP_DIGITALNOW_TINYTWIN) },
1430 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22,
1431 &af9015_props, "Sveon STV22 Dual USB DVB-T Tuner HDTV", RC_MAP_MSI_DIGIVOX_III) },
1432 { }
1433};
1434MODULE_DEVICE_TABLE(usb, af9015_id_table);
Antti Palosaari80619de2008-09-15 17:18:09 -03001435
Antti Palosaari80619de2008-09-15 17:18:09 -03001436/* usb specific object needed to register this driver with the usb subsystem */
1437static struct usb_driver af9015_usb_driver = {
Antti Palosaaria3645e52012-06-07 20:36:35 -03001438 .name = KBUILD_MODNAME,
Antti Palosaari2d2b37c72012-06-12 01:05:20 -03001439 .id_table = af9015_id_table,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001440 .probe = dvb_usbv2_probe,
1441 .disconnect = dvb_usbv2_disconnect,
Antti Palosaari53dc1942012-06-12 02:20:01 -03001442 .suspend = dvb_usbv2_suspend,
1443 .resume = dvb_usbv2_resume,
Antti Palosaari04966aa2012-08-14 22:21:08 -03001444 .reset_resume = dvb_usbv2_reset_resume,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001445 .no_dynamic_id = 1,
Antti Palosaari77f54512012-06-09 21:22:06 -03001446 .soft_unbind = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001447};
1448
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08001449module_usb_driver(af9015_usb_driver);
Antti Palosaari80619de2008-09-15 17:18:09 -03001450
1451MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
Antti Palosaaribc050e62012-05-08 06:04:24 -03001452MODULE_DESCRIPTION("Afatech AF9015 driver");
Antti Palosaari80619de2008-09-15 17:18:09 -03001453MODULE_LICENSE("GPL");
Antti Palosaaribab9b4f2012-09-12 11:37:25 -03001454MODULE_FIRMWARE(AF9015_FIRMWARE);