blob: c429da7da95db94523cead666a2a30f1a8da3e4e [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 Palosaari80619de2008-09-15 17:18:09 -030074 ret = -1;
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 Palosaari80619de2008-09-15 17:18:09 -0300110 ret = -1;
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 Palosaaria3645e52012-06-07 20:36:35 -0300401 int ret;
402 static const unsigned int eeprom_size = 256;
403 unsigned int reg;
404 u8 val, *eeprom;
405 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
406
407 eeprom = kmalloc(eeprom_size, GFP_KERNEL);
408 if (eeprom == NULL)
409 return -ENOMEM;
410
411 for (reg = 0; reg < eeprom_size; reg++) {
412 req.addr = reg;
413 ret = af9015_ctrl_msg(d, &req);
414 if (ret)
415 goto free;
416
417 eeprom[reg] = val;
418 }
419
Antti Palosaarif2247492012-09-12 20:23:50 -0300420 for (reg = 0; reg < eeprom_size; reg += 16)
421 dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 16,
422 eeprom + reg);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300423
424 BUG_ON(eeprom_size % 4);
425
426 state->eeprom_sum = 0;
427 for (reg = 0; reg < eeprom_size / sizeof(u32); reg++) {
428 state->eeprom_sum *= GOLDEN_RATIO_PRIME_32;
429 state->eeprom_sum += le32_to_cpu(((u32 *)eeprom)[reg]);
430 }
431
Antti Palosaarif2247492012-09-12 20:23:50 -0300432 dev_dbg(&d->udev->dev, "%s: eeprom sum=%.8x\n",
433 __func__, state->eeprom_sum);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300434
435 ret = 0;
436free:
437 kfree(eeprom);
438 return ret;
439}
440
441static int af9015_read_config(struct dvb_usb_device *d)
442{
Antti Palosaarie8089662012-06-16 18:13:06 -0300443 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300444 int ret;
445 u8 val, i, offset = 0;
446 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
447
Antti Palosaarif2247492012-09-12 20:23:50 -0300448 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300449
450 /* IR remote controller */
451 req.addr = AF9015_EEPROM_IR_MODE;
452 /* first message will timeout often due to possible hw bug */
453 for (i = 0; i < 4; i++) {
454 ret = af9015_ctrl_msg(d, &req);
455 if (!ret)
456 break;
457 }
458 if (ret)
459 goto error;
460
461 ret = af9015_eeprom_hash(d);
462 if (ret)
463 goto error;
464
Antti Palosaaria3645e52012-06-07 20:36:35 -0300465 state->ir_mode = val;
Antti Palosaarif2247492012-09-12 20:23:50 -0300466 dev_dbg(&d->udev->dev, "%s: IR mode=%d\n", __func__, val);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300467
468 /* TS mode - one or two receivers */
469 req.addr = AF9015_EEPROM_TS_MODE;
470 ret = af9015_ctrl_msg(d, &req);
471 if (ret)
472 goto error;
473
474 state->dual_mode = val;
Antti Palosaarif2247492012-09-12 20:23:50 -0300475 dev_dbg(&d->udev->dev, "%s: TS mode=%d\n", __func__, state->dual_mode);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300476
477 /* disable 2nd adapter because we don't have PID-filters */
478 if (d->udev->speed == USB_SPEED_FULL)
479 state->dual_mode = 0;
480
481 if (state->dual_mode) {
482 /* read 2nd demodulator I2C address */
483 req.addr = AF9015_EEPROM_DEMOD2_I2C;
484 ret = af9015_ctrl_msg(d, &req);
485 if (ret)
486 goto error;
487
488 state->af9013_config[1].i2c_addr = val;
489 }
490
491 for (i = 0; i < state->dual_mode + 1; i++) {
492 if (i == 1)
493 offset = AF9015_EEPROM_OFFSET;
494 /* xtal */
495 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
496 ret = af9015_ctrl_msg(d, &req);
497 if (ret)
498 goto error;
499 switch (val) {
500 case 0:
501 state->af9013_config[i].clock = 28800000;
502 break;
503 case 1:
504 state->af9013_config[i].clock = 20480000;
505 break;
506 case 2:
507 state->af9013_config[i].clock = 28000000;
508 break;
509 case 3:
510 state->af9013_config[i].clock = 25000000;
511 break;
512 };
Antti Palosaarif2247492012-09-12 20:23:50 -0300513 dev_dbg(&d->udev->dev, "%s: [%d] xtal=%d set clock=%d\n",
514 __func__, i, val,
515 state->af9013_config[i].clock);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300516
517 /* IF frequency */
518 req.addr = AF9015_EEPROM_IF1H + offset;
519 ret = af9015_ctrl_msg(d, &req);
520 if (ret)
521 goto error;
522
523 state->af9013_config[i].if_frequency = val << 8;
524
525 req.addr = AF9015_EEPROM_IF1L + offset;
526 ret = af9015_ctrl_msg(d, &req);
527 if (ret)
528 goto error;
529
530 state->af9013_config[i].if_frequency += val;
531 state->af9013_config[i].if_frequency *= 1000;
Antti Palosaarif2247492012-09-12 20:23:50 -0300532 dev_dbg(&d->udev->dev, "%s: [%d] IF frequency=%d\n", __func__,
533 i, state->af9013_config[i].if_frequency);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300534
535 /* MT2060 IF1 */
536 req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
537 ret = af9015_ctrl_msg(d, &req);
538 if (ret)
539 goto error;
540 state->mt2060_if1[i] = val << 8;
541 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
542 ret = af9015_ctrl_msg(d, &req);
543 if (ret)
544 goto error;
545 state->mt2060_if1[i] += val;
Antti Palosaarif2247492012-09-12 20:23:50 -0300546 dev_dbg(&d->udev->dev, "%s: [%d] MT2060 IF1=%d\n", __func__, i,
Antti Palosaaria3645e52012-06-07 20:36:35 -0300547 state->mt2060_if1[i]);
548
549 /* tuner */
550 req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
551 ret = af9015_ctrl_msg(d, &req);
552 if (ret)
553 goto error;
554 switch (val) {
555 case AF9013_TUNER_ENV77H11D5:
556 case AF9013_TUNER_MT2060:
557 case AF9013_TUNER_QT1010:
558 case AF9013_TUNER_UNKNOWN:
559 case AF9013_TUNER_MT2060_2:
560 case AF9013_TUNER_TDA18271:
561 case AF9013_TUNER_QT1010A:
562 case AF9013_TUNER_TDA18218:
563 state->af9013_config[i].spec_inv = 1;
564 break;
565 case AF9013_TUNER_MXL5003D:
566 case AF9013_TUNER_MXL5005D:
567 case AF9013_TUNER_MXL5005R:
568 case AF9013_TUNER_MXL5007T:
569 state->af9013_config[i].spec_inv = 0;
570 break;
571 case AF9013_TUNER_MC44S803:
572 state->af9013_config[i].gpio[1] = AF9013_GPIO_LO;
573 state->af9013_config[i].spec_inv = 1;
574 break;
575 default:
Antti Palosaarif2247492012-09-12 20:23:50 -0300576 dev_err(&d->udev->dev, "%s: tuner id=%d not " \
577 "supported, please report!\n",
578 KBUILD_MODNAME, val);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300579 return -ENODEV;
580 };
581
582 state->af9013_config[i].tuner = val;
Antti Palosaarif2247492012-09-12 20:23:50 -0300583 dev_dbg(&d->udev->dev, "%s: [%d] tuner id=%d\n",
584 __func__, i, val);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300585 }
Antti Palosaari1e8750c2011-03-18 19:36:42 -0300586
Antti Palosaari80619de2008-09-15 17:18:09 -0300587error:
588 if (ret)
Antti Palosaarif2247492012-09-12 20:23:50 -0300589 dev_err(&d->udev->dev, "%s: eeprom read failed=%d\n",
590 KBUILD_MODNAME, ret);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300591
592 /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
593 content :-( Override some wrong values here. Ditto for the
594 AVerTV Red HD+ (A850T) device. */
595 if (le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
596 ((le16_to_cpu(d->udev->descriptor.idProduct) ==
597 USB_PID_AVERMEDIA_A850) ||
598 (le16_to_cpu(d->udev->descriptor.idProduct) ==
599 USB_PID_AVERMEDIA_A850T))) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300600 dev_dbg(&d->udev->dev,
601 "%s: AverMedia A850: overriding config\n",
602 __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300603 /* disable dual mode */
604 state->dual_mode = 0;
605
606 /* set correct IF */
607 state->af9013_config[0].if_frequency = 4570000;
608 }
609
610 return ret;
611}
612
Antti Palosaarib905a2a2012-06-18 22:54:16 -0300613static int af9015_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
Antti Palosaaria3645e52012-06-07 20:36:35 -0300614 struct usb_data_stream_properties *stream)
615{
Antti Palosaarif2247492012-09-12 20:23:50 -0300616 struct dvb_usb_device *d = fe_to_d(fe);
617 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300618
Antti Palosaarif2247492012-09-12 20:23:50 -0300619 if (d->udev->speed == USB_SPEED_FULL)
Antti Palosaaria3645e52012-06-07 20:36:35 -0300620 stream->u.bulk.buffersize = TS_USB11_FRAME_SIZE;
621
622 return 0;
623}
624
625static int af9015_get_adapter_count(struct dvb_usb_device *d)
626{
Antti Palosaarie8089662012-06-16 18:13:06 -0300627 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300628 return state->dual_mode + 1;
629}
630
631/* override demod callbacks for resource locking */
632static int af9015_af9013_set_frontend(struct dvb_frontend *fe)
633{
634 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300635 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300636
637 if (mutex_lock_interruptible(&state->fe_mutex))
638 return -EAGAIN;
639
Antti Palosaarie8089662012-06-16 18:13:06 -0300640 ret = state->set_frontend[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300641
642 mutex_unlock(&state->fe_mutex);
643
644 return ret;
645}
646
647/* override demod callbacks for resource locking */
648static int af9015_af9013_read_status(struct dvb_frontend *fe,
649 fe_status_t *status)
650{
651 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300652 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300653
654 if (mutex_lock_interruptible(&state->fe_mutex))
655 return -EAGAIN;
656
Antti Palosaarie8089662012-06-16 18:13:06 -0300657 ret = state->read_status[fe_to_adap(fe)->id](fe, status);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300658
659 mutex_unlock(&state->fe_mutex);
660
661 return ret;
662}
663
664/* override demod callbacks for resource locking */
665static int af9015_af9013_init(struct dvb_frontend *fe)
666{
667 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300668 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300669
670 if (mutex_lock_interruptible(&state->fe_mutex))
671 return -EAGAIN;
672
Antti Palosaarie8089662012-06-16 18:13:06 -0300673 ret = state->init[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300674
675 mutex_unlock(&state->fe_mutex);
676
677 return ret;
678}
679
680/* override demod callbacks for resource locking */
681static int af9015_af9013_sleep(struct dvb_frontend *fe)
682{
683 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300684 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300685
686 if (mutex_lock_interruptible(&state->fe_mutex))
687 return -EAGAIN;
688
Antti Palosaarie8089662012-06-16 18:13:06 -0300689 ret = state->sleep[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300690
691 mutex_unlock(&state->fe_mutex);
692
693 return ret;
694}
695
696/* override tuner callbacks for resource locking */
697static int af9015_tuner_init(struct dvb_frontend *fe)
698{
699 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300700 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300701
702 if (mutex_lock_interruptible(&state->fe_mutex))
703 return -EAGAIN;
704
Antti Palosaarie8089662012-06-16 18:13:06 -0300705 ret = state->tuner_init[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300706
707 mutex_unlock(&state->fe_mutex);
708
709 return ret;
710}
711
712/* override tuner callbacks for resource locking */
713static int af9015_tuner_sleep(struct dvb_frontend *fe)
714{
715 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300716 struct af9015_state *state = fe_to_priv(fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300717
718 if (mutex_lock_interruptible(&state->fe_mutex))
719 return -EAGAIN;
720
Antti Palosaarie8089662012-06-16 18:13:06 -0300721 ret = state->tuner_sleep[fe_to_adap(fe)->id](fe);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300722
723 mutex_unlock(&state->fe_mutex);
724
Antti Palosaari80619de2008-09-15 17:18:09 -0300725 return ret;
726}
727
728static int af9015_copy_firmware(struct dvb_usb_device *d)
729{
Antti Palosaarie8089662012-06-16 18:13:06 -0300730 struct af9015_state *state = d_to_priv(d);
Antti Palosaari80619de2008-09-15 17:18:09 -0300731 int ret;
732 u8 fw_params[4];
733 u8 val, i;
734 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
735 fw_params };
Antti Palosaarif2247492012-09-12 20:23:50 -0300736 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -0300737
Antti Palosaaria3645e52012-06-07 20:36:35 -0300738 fw_params[0] = state->firmware_size >> 8;
739 fw_params[1] = state->firmware_size & 0xff;
740 fw_params[2] = state->firmware_checksum >> 8;
741 fw_params[3] = state->firmware_checksum & 0xff;
Antti Palosaari80619de2008-09-15 17:18:09 -0300742
743 /* wait 2nd demodulator ready */
744 msleep(100);
745
Antti Palosaaria3645e52012-06-07 20:36:35 -0300746 ret = af9015_read_reg_i2c(d, state->af9013_config[1].i2c_addr,
747 0x98be, &val);
Antti Palosaari80619de2008-09-15 17:18:09 -0300748 if (ret)
749 goto error;
750 else
Antti Palosaarif2247492012-09-12 20:23:50 -0300751 dev_dbg(&d->udev->dev, "%s: firmware status=%02x\n",
752 __func__, val);
Antti Palosaari80619de2008-09-15 17:18:09 -0300753
754 if (val == 0x0c) /* fw is running, no need for download */
755 goto exit;
756
757 /* set I2C master clock to fast (to speed up firmware copy) */
758 ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
759 if (ret)
760 goto error;
761
762 msleep(50);
763
764 /* copy firmware */
765 ret = af9015_ctrl_msg(d, &req);
766 if (ret)
Antti Palosaarif2247492012-09-12 20:23:50 -0300767 dev_err(&d->udev->dev, "%s: firmware copy cmd failed=%d\n",
768 KBUILD_MODNAME, ret);
769
770 dev_dbg(&d->udev->dev, "%s: firmware copy done\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -0300771
772 /* set I2C master clock back to normal */
773 ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
774 if (ret)
775 goto error;
776
777 /* request boot firmware */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300778 ret = af9015_write_reg_i2c(d, state->af9013_config[1].i2c_addr,
779 0xe205, 1);
Antti Palosaarif2247492012-09-12 20:23:50 -0300780 dev_dbg(&d->udev->dev, "%s: firmware boot cmd status=%d\n",
781 __func__, ret);
Antti Palosaari80619de2008-09-15 17:18:09 -0300782 if (ret)
783 goto error;
784
785 for (i = 0; i < 15; i++) {
786 msleep(100);
787
788 /* check firmware status */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300789 ret = af9015_read_reg_i2c(d, state->af9013_config[1].i2c_addr,
790 0x98be, &val);
Antti Palosaarif2247492012-09-12 20:23:50 -0300791 dev_dbg(&d->udev->dev, "%s: firmware status cmd status=%d " \
792 "firmware status=%02x\n", __func__, ret, val);
Antti Palosaari80619de2008-09-15 17:18:09 -0300793 if (ret)
794 goto error;
795
796 if (val == 0x0c || val == 0x04) /* success or fail */
797 break;
798 }
799
800 if (val == 0x04) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300801 dev_err(&d->udev->dev, "%s: firmware did not run\n",
802 KBUILD_MODNAME);
Antti Palosaari80619de2008-09-15 17:18:09 -0300803 ret = -1;
804 } else if (val != 0x0c) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300805 dev_err(&d->udev->dev, "%s: firmware boot timeout\n",
806 KBUILD_MODNAME);
Antti Palosaari80619de2008-09-15 17:18:09 -0300807 ret = -1;
808 }
809
810error:
811exit:
812 return ret;
813}
814
Antti Palosaari80619de2008-09-15 17:18:09 -0300815static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
816{
817 int ret;
Antti Palosaarie8089662012-06-16 18:13:06 -0300818 struct af9015_state *state = adap_to_priv(adap);
Antti Palosaari80619de2008-09-15 17:18:09 -0300819
Antti Palosaaria3645e52012-06-07 20:36:35 -0300820 if (adap->id == 0) {
821 state->af9013_config[0].ts_mode = AF9013_TS_USB;
822 memcpy(state->af9013_config[0].api_version, "\x0\x1\x9\x0", 4);
823 state->af9013_config[0].gpio[0] = AF9013_GPIO_HI;
824 state->af9013_config[0].gpio[3] = AF9013_GPIO_TUNER_ON;
825 } else if (adap->id == 1) {
826 state->af9013_config[1].ts_mode = AF9013_TS_SERIAL;
827 memcpy(state->af9013_config[1].api_version, "\x0\x1\x9\x0", 4);
828 state->af9013_config[1].gpio[0] = AF9013_GPIO_TUNER_ON;
829 state->af9013_config[1].gpio[1] = AF9013_GPIO_LO;
830
Antti Palosaari80619de2008-09-15 17:18:09 -0300831 /* copy firmware to 2nd demodulator */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300832 if (state->dual_mode) {
Antti Palosaarie8089662012-06-16 18:13:06 -0300833 ret = af9015_copy_firmware(adap_to_d(adap));
Antti Palosaari80619de2008-09-15 17:18:09 -0300834 if (ret) {
Antti Palosaarif2247492012-09-12 20:23:50 -0300835 dev_err(&adap_to_d(adap)->udev->dev,
836 "%s: firmware copy to 2nd " \
837 "frontend failed, will " \
838 "disable it\n", KBUILD_MODNAME);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300839 state->dual_mode = 0;
Antti Palosaari80619de2008-09-15 17:18:09 -0300840 return -ENODEV;
841 }
842 } else {
843 return -ENODEV;
844 }
845 }
846
847 /* attach demodulator */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300848 adap->fe[0] = dvb_attach(af9013_attach,
Antti Palosaarie8089662012-06-16 18:13:06 -0300849 &state->af9013_config[adap->id], &adap_to_d(adap)->i2c_adap);
Antti Palosaari80619de2008-09-15 17:18:09 -0300850
Antti Palosaarie90ab842011-11-12 22:33:30 -0300851 /*
852 * AF9015 firmware does not like if it gets interrupted by I2C adapter
853 * request on some critical phases. During normal operation I2C adapter
854 * is used only 2nd demodulator and tuner on dual tuner devices.
855 * Override demodulator callbacks and use mutex for limit access to
856 * those "critical" paths to keep AF9015 happy.
Antti Palosaarie90ab842011-11-12 22:33:30 -0300857 */
Antti Palosaaria3645e52012-06-07 20:36:35 -0300858 if (adap->fe[0]) {
Antti Palosaarie90ab842011-11-12 22:33:30 -0300859 state->set_frontend[adap->id] =
Antti Palosaaria3645e52012-06-07 20:36:35 -0300860 adap->fe[0]->ops.set_frontend;
861 adap->fe[0]->ops.set_frontend =
Antti Palosaarie90ab842011-11-12 22:33:30 -0300862 af9015_af9013_set_frontend;
863
864 state->read_status[adap->id] =
Antti Palosaaria3645e52012-06-07 20:36:35 -0300865 adap->fe[0]->ops.read_status;
866 adap->fe[0]->ops.read_status =
Antti Palosaarie90ab842011-11-12 22:33:30 -0300867 af9015_af9013_read_status;
868
Antti Palosaaria3645e52012-06-07 20:36:35 -0300869 state->init[adap->id] = adap->fe[0]->ops.init;
870 adap->fe[0]->ops.init = af9015_af9013_init;
Antti Palosaarie90ab842011-11-12 22:33:30 -0300871
Antti Palosaaria3645e52012-06-07 20:36:35 -0300872 state->sleep[adap->id] = adap->fe[0]->ops.sleep;
873 adap->fe[0]->ops.sleep = af9015_af9013_sleep;
Antti Palosaarie90ab842011-11-12 22:33:30 -0300874 }
875
Antti Palosaaria3645e52012-06-07 20:36:35 -0300876 return adap->fe[0] == NULL ? -ENODEV : 0;
Antti Palosaari80619de2008-09-15 17:18:09 -0300877}
878
879static struct mt2060_config af9015_mt2060_config = {
880 .i2c_address = 0xc0,
881 .clock_out = 0,
882};
883
884static struct qt1010_config af9015_qt1010_config = {
885 .i2c_address = 0xc4,
886};
887
888static struct tda18271_config af9015_tda18271_config = {
889 .gate = TDA18271_GATE_DIGITAL,
Mauro Carvalho Chehab7655e592010-10-27 14:55:34 -0200890 .small_i2c = TDA18271_16_BYTE_CHUNK_INIT,
Antti Palosaari80619de2008-09-15 17:18:09 -0300891};
892
893static struct mxl5005s_config af9015_mxl5003_config = {
894 .i2c_address = 0xc6,
895 .if_freq = IF_FREQ_4570000HZ,
896 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
897 .agc_mode = MXL_SINGLE_AGC,
898 .tracking_filter = MXL_TF_DEFAULT,
Antti Palosaaria1310772008-09-22 13:59:25 -0300899 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -0300900 .cap_select = MXL_CAP_SEL_ENABLE,
901 .div_out = MXL_DIV_OUT_4,
902 .clock_out = MXL_CLOCK_OUT_DISABLE,
903 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
904 .top = MXL5005S_TOP_25P2,
905 .mod_mode = MXL_DIGITAL_MODE,
906 .if_mode = MXL_ZERO_IF,
907 .AgcMasterByte = 0x00,
908};
909
910static struct mxl5005s_config af9015_mxl5005_config = {
911 .i2c_address = 0xc6,
912 .if_freq = IF_FREQ_4570000HZ,
913 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
914 .agc_mode = MXL_SINGLE_AGC,
915 .tracking_filter = MXL_TF_OFF,
Antti Palosaaria1310772008-09-22 13:59:25 -0300916 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -0300917 .cap_select = MXL_CAP_SEL_ENABLE,
918 .div_out = MXL_DIV_OUT_4,
919 .clock_out = MXL_CLOCK_OUT_DISABLE,
920 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
921 .top = MXL5005S_TOP_25P2,
922 .mod_mode = MXL_DIGITAL_MODE,
923 .if_mode = MXL_ZERO_IF,
924 .AgcMasterByte = 0x00,
925};
926
Jochen Friedrichd5633992009-02-02 14:59:50 -0300927static struct mc44s803_config af9015_mc44s803_config = {
928 .i2c_address = 0xc0,
929 .dig_out = 1,
930};
931
Antti Palosaariee3d4402010-08-13 03:51:26 -0300932static struct tda18218_config af9015_tda18218_config = {
933 .i2c_address = 0xc0,
934 .i2c_wr_max = 21, /* max wr bytes AF9015 I2C adap can handle at once */
935};
936
Antti Palosaariab07fdd2010-09-09 14:59:10 -0300937static struct mxl5007t_config af9015_mxl5007t_config = {
938 .xtal_freq_hz = MxL_XTAL_24_MHZ,
939 .if_freq_hz = MxL_IF_4_57_MHZ,
940};
941
Antti Palosaari80619de2008-09-15 17:18:09 -0300942static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
943{
Antti Palosaarif2247492012-09-12 20:23:50 -0300944 struct dvb_usb_device *d = adap_to_d(adap);
945 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -0300946 int ret;
Antti Palosaarif2247492012-09-12 20:23:50 -0300947 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaari80619de2008-09-15 17:18:09 -0300948
Antti Palosaaria3645e52012-06-07 20:36:35 -0300949 switch (state->af9013_config[adap->id].tuner) {
Antti Palosaari80619de2008-09-15 17:18:09 -0300950 case AF9013_TUNER_MT2060:
951 case AF9013_TUNER_MT2060_2:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300952 ret = dvb_attach(mt2060_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300953 &adap_to_d(adap)->i2c_adap, &af9015_mt2060_config,
Antti Palosaaria3645e52012-06-07 20:36:35 -0300954 state->mt2060_if1[adap->id])
Antti Palosaari80619de2008-09-15 17:18:09 -0300955 == NULL ? -ENODEV : 0;
956 break;
957 case AF9013_TUNER_QT1010:
958 case AF9013_TUNER_QT1010A:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300959 ret = dvb_attach(qt1010_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300960 &adap_to_d(adap)->i2c_adap,
Antti Palosaari80619de2008-09-15 17:18:09 -0300961 &af9015_qt1010_config) == NULL ? -ENODEV : 0;
962 break;
963 case AF9013_TUNER_TDA18271:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300964 ret = dvb_attach(tda18271_attach, adap->fe[0], 0xc0,
Antti Palosaarie8089662012-06-16 18:13:06 -0300965 &adap_to_d(adap)->i2c_adap,
Antti Palosaari80619de2008-09-15 17:18:09 -0300966 &af9015_tda18271_config) == NULL ? -ENODEV : 0;
967 break;
Antti Palosaariee3d4402010-08-13 03:51:26 -0300968 case AF9013_TUNER_TDA18218:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300969 ret = dvb_attach(tda18218_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300970 &adap_to_d(adap)->i2c_adap,
Antti Palosaariee3d4402010-08-13 03:51:26 -0300971 &af9015_tda18218_config) == NULL ? -ENODEV : 0;
972 break;
Antti Palosaari80619de2008-09-15 17:18:09 -0300973 case AF9013_TUNER_MXL5003D:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300974 ret = dvb_attach(mxl5005s_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300975 &adap_to_d(adap)->i2c_adap,
Antti Palosaari80619de2008-09-15 17:18:09 -0300976 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
977 break;
978 case AF9013_TUNER_MXL5005D:
979 case AF9013_TUNER_MXL5005R:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300980 ret = dvb_attach(mxl5005s_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300981 &adap_to_d(adap)->i2c_adap,
Antti Palosaari80619de2008-09-15 17:18:09 -0300982 &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
983 break;
984 case AF9013_TUNER_ENV77H11D5:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300985 ret = dvb_attach(dvb_pll_attach, adap->fe[0], 0xc0,
Antti Palosaarie8089662012-06-16 18:13:06 -0300986 &adap_to_d(adap)->i2c_adap,
Antti Palosaari80619de2008-09-15 17:18:09 -0300987 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
988 break;
989 case AF9013_TUNER_MC44S803:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300990 ret = dvb_attach(mc44s803_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300991 &adap_to_d(adap)->i2c_adap,
Jochen Friedrichd5633992009-02-02 14:59:50 -0300992 &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
Antti Palosaari80619de2008-09-15 17:18:09 -0300993 break;
Antti Palosaariab07fdd2010-09-09 14:59:10 -0300994 case AF9013_TUNER_MXL5007T:
Antti Palosaaria3645e52012-06-07 20:36:35 -0300995 ret = dvb_attach(mxl5007t_attach, adap->fe[0],
Antti Palosaarie8089662012-06-16 18:13:06 -0300996 &adap_to_d(adap)->i2c_adap,
Antti Palosaariab07fdd2010-09-09 14:59:10 -0300997 0xc0, &af9015_mxl5007t_config) == NULL ? -ENODEV : 0;
998 break;
Antti Palosaari80619de2008-09-15 17:18:09 -0300999 case AF9013_TUNER_UNKNOWN:
1000 default:
Antti Palosaarif2247492012-09-12 20:23:50 -03001001 dev_err(&d->udev->dev, "%s: unknown tuner id=%d\n",
1002 KBUILD_MODNAME,
1003 state->af9013_config[adap->id].tuner);
Antti Palosaari80619de2008-09-15 17:18:09 -03001004 ret = -ENODEV;
Antti Palosaari80619de2008-09-15 17:18:09 -03001005 }
Gordon Heckerbe4a5e72012-03-14 10:27:30 -03001006
Antti Palosaaria3645e52012-06-07 20:36:35 -03001007 if (adap->fe[0]->ops.tuner_ops.init) {
Antti Palosaari6d535bd2012-03-14 10:27:31 -03001008 state->tuner_init[adap->id] =
Antti Palosaaria3645e52012-06-07 20:36:35 -03001009 adap->fe[0]->ops.tuner_ops.init;
1010 adap->fe[0]->ops.tuner_ops.init = af9015_tuner_init;
Antti Palosaari6d535bd2012-03-14 10:27:31 -03001011 }
Gordon Heckerbe4a5e72012-03-14 10:27:30 -03001012
Antti Palosaaria3645e52012-06-07 20:36:35 -03001013 if (adap->fe[0]->ops.tuner_ops.sleep) {
Antti Palosaari6d535bd2012-03-14 10:27:31 -03001014 state->tuner_sleep[adap->id] =
Antti Palosaaria3645e52012-06-07 20:36:35 -03001015 adap->fe[0]->ops.tuner_ops.sleep;
1016 adap->fe[0]->ops.tuner_ops.sleep = af9015_tuner_sleep;
Antti Palosaari6d535bd2012-03-14 10:27:31 -03001017 }
1018
Antti Palosaari80619de2008-09-15 17:18:09 -03001019 return ret;
1020}
1021
Antti Palosaaria3645e52012-06-07 20:36:35 -03001022static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1023{
Antti Palosaarif2247492012-09-12 20:23:50 -03001024 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001025 int ret;
Antti Palosaarif2247492012-09-12 20:23:50 -03001026 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001027
1028 if (onoff)
Antti Palosaarif2247492012-09-12 20:23:50 -03001029 ret = af9015_set_reg_bit(d, 0xd503, 0);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001030 else
Antti Palosaarif2247492012-09-12 20:23:50 -03001031 ret = af9015_clear_reg_bit(d, 0xd503, 0);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001032
1033 return ret;
1034}
1035
1036static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1037 int onoff)
1038{
Antti Palosaarif2247492012-09-12 20:23:50 -03001039 struct dvb_usb_device *d = adap_to_d(adap);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001040 int ret;
1041 u8 idx;
Antti Palosaarif2247492012-09-12 20:23:50 -03001042 dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
1043 __func__, index, pid, onoff);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001044
Antti Palosaarif2247492012-09-12 20:23:50 -03001045 ret = af9015_write_reg(d, 0xd505, (pid & 0xff));
Antti Palosaaria3645e52012-06-07 20:36:35 -03001046 if (ret)
1047 goto error;
1048
Antti Palosaarif2247492012-09-12 20:23:50 -03001049 ret = af9015_write_reg(d, 0xd506, (pid >> 8));
Antti Palosaaria3645e52012-06-07 20:36:35 -03001050 if (ret)
1051 goto error;
1052
1053 idx = ((index & 0x1f) | (1 << 5));
Antti Palosaarif2247492012-09-12 20:23:50 -03001054 ret = af9015_write_reg(d, 0xd504, idx);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001055
1056error:
1057 return ret;
1058}
1059
1060static int af9015_init_endpoint(struct dvb_usb_device *d)
1061{
Antti Palosaarie8089662012-06-16 18:13:06 -03001062 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001063 int ret;
1064 u16 frame_size;
1065 u8 packet_size;
Antti Palosaarif2247492012-09-12 20:23:50 -03001066 dev_dbg(&d->udev->dev, "%s: USB speed=%d\n", __func__, d->udev->speed);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001067
1068 if (d->udev->speed == USB_SPEED_FULL) {
1069 frame_size = TS_USB11_FRAME_SIZE/4;
1070 packet_size = TS_USB11_MAX_PACKET_SIZE/4;
1071 } else {
1072 frame_size = TS_USB20_FRAME_SIZE/4;
1073 packet_size = TS_USB20_MAX_PACKET_SIZE/4;
1074 }
1075
1076 ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
1077 if (ret)
1078 goto error;
1079 ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
1080 if (ret)
1081 goto error;
1082 ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
1083 if (ret)
1084 goto error;
1085 ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
1086 if (ret)
1087 goto error;
1088 ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
1089 if (ret)
1090 goto error;
1091 if (state->dual_mode) {
1092 ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
1093 if (ret)
1094 goto error;
1095 }
1096 ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
1097 if (ret)
1098 goto error;
1099 if (state->dual_mode) {
1100 ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
1101 if (ret)
1102 goto error;
1103 }
1104 /* EP4 xfer length */
1105 ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
1106 if (ret)
1107 goto error;
1108 ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
1109 if (ret)
1110 goto error;
1111 /* EP5 xfer length */
1112 ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
1113 if (ret)
1114 goto error;
1115 ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
1116 if (ret)
1117 goto error;
1118 ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
1119 if (ret)
1120 goto error;
1121 ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
1122 if (ret)
1123 goto error;
1124 ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
1125 if (ret)
1126 goto error;
1127 if (state->dual_mode) {
1128 ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
1129 if (ret)
1130 goto error;
1131 }
1132
1133 /* enable / disable mp2if2 */
1134 if (state->dual_mode)
1135 ret = af9015_set_reg_bit(d, 0xd50b, 0);
1136 else
1137 ret = af9015_clear_reg_bit(d, 0xd50b, 0);
1138
1139error:
1140 if (ret)
Antti Palosaarif2247492012-09-12 20:23:50 -03001141 dev_err(&d->udev->dev, "%s: endpoint init failed=%d\n",
1142 KBUILD_MODNAME, ret);
1143
Antti Palosaaria3645e52012-06-07 20:36:35 -03001144 return ret;
1145}
1146
1147static int af9015_init(struct dvb_usb_device *d)
1148{
Antti Palosaarie8089662012-06-16 18:13:06 -03001149 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001150 int ret;
Antti Palosaarif2247492012-09-12 20:23:50 -03001151 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001152
1153 mutex_init(&state->fe_mutex);
1154
1155 /* init RC canary */
1156 ret = af9015_write_reg(d, 0x98e9, 0xff);
1157 if (ret)
1158 goto error;
1159
1160 ret = af9015_init_endpoint(d);
1161 if (ret)
1162 goto error;
1163
1164error:
1165 return ret;
1166}
1167
1168struct af9015_rc_setup {
1169 unsigned int id;
1170 char *rc_codes;
Jonathan Niederd07b9012012-01-07 04:11:27 -03001171};
1172
Antti Palosaaria3645e52012-06-07 20:36:35 -03001173static char *af9015_rc_setup_match(unsigned int id,
1174 const struct af9015_rc_setup *table)
1175{
1176 for (; table->rc_codes; table++)
1177 if (table->id == id)
1178 return table->rc_codes;
1179 return NULL;
1180}
1181
1182static const struct af9015_rc_setup af9015_rc_setup_modparam[] = {
1183 { AF9015_REMOTE_A_LINK_DTU_M, RC_MAP_ALINK_DTU_M },
1184 { AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3, RC_MAP_MSI_DIGIVOX_II },
1185 { AF9015_REMOTE_MYGICTV_U718, RC_MAP_TOTAL_MEDIA_IN_HAND },
1186 { AF9015_REMOTE_DIGITTRADE_DVB_T, RC_MAP_DIGITTRADE },
1187 { AF9015_REMOTE_AVERMEDIA_KS, RC_MAP_AVERMEDIA_RM_KS },
Jonathan Niederd07b9012012-01-07 04:11:27 -03001188 { }
Antti Palosaari80619de2008-09-15 17:18:09 -03001189};
Antti Palosaari80619de2008-09-15 17:18:09 -03001190
Antti Palosaaria3645e52012-06-07 20:36:35 -03001191static const struct af9015_rc_setup af9015_rc_setup_hashes[] = {
1192 { 0xb8feb708, RC_MAP_MSI_DIGIVOX_II },
1193 { 0xa3703d00, RC_MAP_ALINK_DTU_M },
1194 { 0x9b7dc64e, RC_MAP_TOTAL_MEDIA_IN_HAND }, /* MYGICTV U718 */
1195 { 0x5d49e3db, RC_MAP_DIGITTRADE }, /* LC-Power LC-USB-DVBT */
1196 { }
1197};
Antti Palosaari80619de2008-09-15 17:18:09 -03001198
Antti Palosaaria3645e52012-06-07 20:36:35 -03001199static int af9015_rc_query(struct dvb_usb_device *d)
1200{
Antti Palosaarie8089662012-06-16 18:13:06 -03001201 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001202 int ret;
1203 u8 buf[17];
Antti Palosaari80619de2008-09-15 17:18:09 -03001204
Antti Palosaaria3645e52012-06-07 20:36:35 -03001205 /* read registers needed to detect remote controller code */
1206 ret = af9015_read_regs(d, 0x98d9, buf, sizeof(buf));
1207 if (ret)
1208 goto error;
Antti Palosaari80619de2008-09-15 17:18:09 -03001209
Antti Palosaaria3645e52012-06-07 20:36:35 -03001210 /* If any of these are non-zero, assume invalid data */
Antti Palosaarif2247492012-09-12 20:23:50 -03001211 if (buf[1] || buf[2] || buf[3]) {
1212 dev_dbg(&d->udev->dev, "%s: invalid data\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001213 return ret;
Antti Palosaarif2247492012-09-12 20:23:50 -03001214 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001215
Antti Palosaaria3645e52012-06-07 20:36:35 -03001216 /* Check for repeat of previous code */
1217 if ((state->rc_repeat != buf[6] || buf[0]) &&
1218 !memcmp(&buf[12], state->rc_last, 4)) {
Antti Palosaarif2247492012-09-12 20:23:50 -03001219 dev_dbg(&d->udev->dev, "%s: key repeated\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001220 rc_keydown(d->rc_dev, state->rc_keycode, 0);
1221 state->rc_repeat = buf[6];
1222 return ret;
1223 }
1224
1225 /* Only process key if canary killed */
1226 if (buf[16] != 0xff && buf[0] != 0x01) {
Antti Palosaarif2247492012-09-12 20:23:50 -03001227 dev_dbg(&d->udev->dev, "%s: key pressed %*ph\n",
1228 __func__, 4, buf + 12);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001229
1230 /* Reset the canary */
1231 ret = af9015_write_reg(d, 0x98e9, 0xff);
1232 if (ret)
1233 goto error;
1234
1235 /* Remember this key */
1236 memcpy(state->rc_last, &buf[12], 4);
1237 if (buf[14] == (u8) ~buf[15]) {
1238 if (buf[12] == (u8) ~buf[13]) {
1239 /* NEC */
1240 state->rc_keycode = buf[12] << 8 | buf[14];
1241 } else {
1242 /* NEC extended*/
1243 state->rc_keycode = buf[12] << 16 |
1244 buf[13] << 8 | buf[14];
Antti Palosaari80619de2008-09-15 17:18:09 -03001245 }
Antti Palosaaria3645e52012-06-07 20:36:35 -03001246 } else {
1247 /* 32 bit NEC */
1248 state->rc_keycode = buf[12] << 24 | buf[13] << 16 |
1249 buf[14] << 8 | buf[15];
Antti Palosaari80619de2008-09-15 17:18:09 -03001250 }
Antti Palosaaria3645e52012-06-07 20:36:35 -03001251 rc_keydown(d->rc_dev, state->rc_keycode, 0);
1252 } else {
Antti Palosaarif2247492012-09-12 20:23:50 -03001253 dev_dbg(&d->udev->dev, "%s: no key press\n", __func__);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001254 /* Invalidate last keypress */
1255 /* Not really needed, but helps with debug */
1256 state->rc_last[2] = state->rc_last[3];
1257 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001258
Antti Palosaaria3645e52012-06-07 20:36:35 -03001259 state->rc_repeat = buf[6];
Antti Palosaarieb29fbe2012-07-24 21:21:04 -03001260 state->rc_failed = false;
Antti Palosaari80619de2008-09-15 17:18:09 -03001261
Antti Palosaaria3645e52012-06-07 20:36:35 -03001262error:
Antti Palosaarieb29fbe2012-07-24 21:21:04 -03001263 if (ret) {
Antti Palosaarif2247492012-09-12 20:23:50 -03001264 dev_warn(&d->udev->dev, "%s: rc query failed=%d\n",
1265 KBUILD_MODNAME, ret);
Antti Palosaari80619de2008-09-15 17:18:09 -03001266
Antti Palosaarieb29fbe2012-07-24 21:21:04 -03001267 /* allow random errors as dvb-usb will stop polling on error */
1268 if (!state->rc_failed)
1269 ret = 0;
1270
1271 state->rc_failed = true;
1272 }
1273
Antti Palosaaria3645e52012-06-07 20:36:35 -03001274 return ret;
1275}
Antti Palosaari80619de2008-09-15 17:18:09 -03001276
Antti Palosaaria3645e52012-06-07 20:36:35 -03001277static int af9015_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1278{
Antti Palosaarie8089662012-06-16 18:13:06 -03001279 struct af9015_state *state = d_to_priv(d);
Antti Palosaaria3645e52012-06-07 20:36:35 -03001280 u16 vid = le16_to_cpu(d->udev->descriptor.idVendor);
Antti Palosaari80619de2008-09-15 17:18:09 -03001281
Antti Palosaaria3645e52012-06-07 20:36:35 -03001282 if (state->ir_mode == AF9015_IR_MODE_DISABLED)
1283 return 0;
Antti Palosaari80619de2008-09-15 17:18:09 -03001284
Antti Palosaaria3645e52012-06-07 20:36:35 -03001285 /* try to load remote based module param */
Antti Palosaarieb29fbe2012-07-24 21:21:04 -03001286 if (!rc->map_name)
1287 rc->map_name = af9015_rc_setup_match(dvb_usb_af9015_remote,
1288 af9015_rc_setup_modparam);
Antti Palosaari80619de2008-09-15 17:18:09 -03001289
Antti Palosaaria3645e52012-06-07 20:36:35 -03001290 /* try to load remote based eeprom hash */
1291 if (!rc->map_name)
1292 rc->map_name = af9015_rc_setup_match(state->eeprom_sum,
1293 af9015_rc_setup_hashes);
Antti Palosaari80619de2008-09-15 17:18:09 -03001294
Antti Palosaaria3645e52012-06-07 20:36:35 -03001295 /* try to load remote based USB iManufacturer string */
1296 if (!rc->map_name && vid == USB_VID_AFATECH) {
1297 /* Check USB manufacturer and product strings and try
1298 to determine correct remote in case of chip vendor
1299 reference IDs are used.
1300 DO NOT ADD ANYTHING NEW HERE. Use hashes instead. */
1301 char manufacturer[10];
1302 memset(manufacturer, 0, sizeof(manufacturer));
1303 usb_string(d->udev, d->udev->descriptor.iManufacturer,
1304 manufacturer, sizeof(manufacturer));
1305 if (!strcmp("MSI", manufacturer)) {
1306 /* iManufacturer 1 MSI
1307 iProduct 2 MSI K-VOX */
1308 rc->map_name = af9015_rc_setup_match(
1309 AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3,
1310 af9015_rc_setup_modparam);
Antti Palosaari80619de2008-09-15 17:18:09 -03001311 }
Antti Palosaaria3645e52012-06-07 20:36:35 -03001312 }
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001313
Antti Palosaaride73bee2012-07-05 19:57:07 -03001314 /* load empty to enable rc */
1315 if (!rc->map_name)
1316 rc->map_name = RC_MAP_EMPTY;
1317
Antti Palosaaria3645e52012-06-07 20:36:35 -03001318 rc->allowed_protos = RC_TYPE_NEC;
1319 rc->query = af9015_rc_query;
1320 rc->interval = 500;
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001321
Antti Palosaaria3645e52012-06-07 20:36:35 -03001322 return 0;
1323}
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001324
Antti Palosaaria3645e52012-06-07 20:36:35 -03001325/* interface 0 is used by DVB-T receiver and
1326 interface 1 is for remote controller (HID) */
1327static struct dvb_usb_device_properties af9015_props = {
1328 .driver_name = KBUILD_MODNAME,
1329 .owner = THIS_MODULE,
1330 .adapter_nr = adapter_nr,
1331 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001332
Antti Palosaaria3645e52012-06-07 20:36:35 -03001333 .generic_bulk_ctrl_endpoint = 0x02,
1334 .generic_bulk_ctrl_endpoint_response = 0x81,
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001335
Antti Palosaaria3645e52012-06-07 20:36:35 -03001336 .identify_state = af9015_identify_state,
Antti Palosaaribab9b4f2012-09-12 11:37:25 -03001337 .firmware = AF9015_FIRMWARE,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001338 .download_firmware = af9015_download_firmware,
1339
Antti Palosaaria3645e52012-06-07 20:36:35 -03001340 .i2c_algo = &af9015_i2c_algo,
Antti Palosaari2d2b37c72012-06-12 01:05:20 -03001341 .read_config = af9015_read_config,
1342 .frontend_attach = af9015_af9013_frontend_attach,
1343 .tuner_attach = af9015_tuner_attach,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001344 .init = af9015_init,
1345 .get_rc_config = af9015_get_rc_config,
Antti Palosaarib905a2a2012-06-18 22:54:16 -03001346 .get_stream_config = af9015_get_stream_config,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001347
1348 .get_adapter_count = af9015_get_adapter_count,
1349 .adapter = {
1350 {
1351 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1352 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1353 .pid_filter_count = 32,
1354 .pid_filter = af9015_pid_filter,
1355 .pid_filter_ctrl = af9015_pid_filter_ctrl,
Antti Palosaari1a590012012-06-16 16:25:22 -03001356
1357 .stream = DVB_USB_STREAM_BULK(0x84, 8, TS_USB20_FRAME_SIZE),
1358 }, {
1359 .stream = DVB_USB_STREAM_BULK(0x85, 8, TS_USB20_FRAME_SIZE),
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001360 },
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001361 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001362};
Antti Palosaari80619de2008-09-15 17:18:09 -03001363
Antti Palosaaria3645e52012-06-07 20:36:35 -03001364static const struct usb_device_id af9015_id_table[] = {
1365 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015,
1366 &af9015_props, "Afatech AF9015 reference design", NULL) },
1367 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016,
1368 &af9015_props, "Afatech AF9015 reference design", NULL) },
1369 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD,
1370 &af9015_props, "Leadtek WinFast DTV Dongle Gold", RC_MAP_LEADTEK_Y04G0051) },
1371 { DVB_USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E,
1372 &af9015_props, "Pinnacle PCTV 71e", NULL) },
1373 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U,
1374 &af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) },
1375 { DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TINYTWIN,
1376 &af9015_props, "DigitalNow TinyTwin", RC_MAP_AZUREWAVE_AD_TU700) },
1377 { DVB_USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_AZUREWAVE_AD_TU700,
1378 &af9015_props, "TwinHan AzureWave AD-TU700(704J)", RC_MAP_AZUREWAVE_AD_TU700) },
1379 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2,
1380 &af9015_props, "TerraTec Cinergy T USB XE", NULL) },
1381 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T,
1382 &af9015_props, "KWorld PlusTV Dual DVB-T PCI (DVB-T PC160-2T)", NULL) },
1383 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X,
1384 &af9015_props, "AVerMedia AVerTV DVB-T Volar X", RC_MAP_AVERMEDIA_M135A) },
1385 { DVB_USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380,
1386 &af9015_props, "Xtensions XD-380", NULL) },
1387 { DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO,
1388 &af9015_props, "MSI DIGIVOX Duo", RC_MAP_MSI_DIGIVOX_III) },
1389 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2,
1390 &af9015_props, "Fujitsu-Siemens Slim Mobile USB DVB-T", NULL) },
1391 { DVB_USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2,
1392 &af9015_props, "Telestar Starstick 2", NULL) },
1393 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309,
1394 &af9015_props, "AVerMedia A309", NULL) },
1395 { DVB_USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III,
1396 &af9015_props, "MSI Digi VOX mini III", RC_MAP_MSI_DIGIVOX_III) },
1397 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U,
1398 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1399 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2,
1400 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1401 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3,
1402 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1403 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT,
1404 &af9015_props, "TrekStor DVB-T USB Stick", RC_MAP_TREKSTOR) },
1405 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850,
1406 &af9015_props, "AverMedia AVerTV Volar Black HD (A850)", NULL) },
1407 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805,
1408 &af9015_props, "AverMedia AVerTV Volar GPS 805 (A805)", NULL) },
1409 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU,
1410 &af9015_props, "Conceptronic USB2.0 DVB-T CTVDIGRCU V3.0", NULL) },
1411 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810,
1412 &af9015_props, "KWorld Digial MC-810", NULL) },
1413 { DVB_USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03,
1414 &af9015_props, "Genius TVGo DVB-T03", NULL) },
1415 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2,
1416 &af9015_props, "KWorld PlusTV Dual DVB-T Stick (DVB-T 399U)", NULL) },
1417 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T,
1418 &af9015_props, "KWorld PlusTV DVB-T PCI Pro Card (DVB-T PC160-T)", NULL) },
1419 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20,
1420 &af9015_props, "Sveon STV20 Tuner USB DVB-T HDTV", NULL) },
1421 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TINYTWIN_2,
1422 &af9015_props, "DigitalNow TinyTwin v2", RC_MAP_DIGITALNOW_TINYTWIN) },
1423 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS,
1424 &af9015_props, "Leadtek WinFast DTV2000DS", RC_MAP_LEADTEK_Y04G0051) },
1425 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB383_T,
1426 &af9015_props, "KWorld USB DVB-T Stick Mobile (UB383-T)", NULL) },
1427 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_4,
1428 &af9015_props, "KWorld USB DVB-T TV Stick II (VS-DVB-T 395U)", NULL) },
1429 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A815M,
1430 &af9015_props, "AverMedia AVerTV Volar M (A815Mac)", NULL) },
1431 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_RC,
1432 &af9015_props, "TerraTec Cinergy T Stick RC", RC_MAP_TERRATEC_SLIM_2) },
1433 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_DUAL_RC,
1434 &af9015_props, "TerraTec Cinergy T Stick Dual RC", RC_MAP_TERRATEC_SLIM) },
1435 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850T,
1436 &af9015_props, "AverMedia AVerTV Red HD+ (A850T)", NULL) },
1437 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TINYTWIN_3,
1438 &af9015_props, "DigitalNow TinyTwin v3", RC_MAP_DIGITALNOW_TINYTWIN) },
1439 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22,
1440 &af9015_props, "Sveon STV22 Dual USB DVB-T Tuner HDTV", RC_MAP_MSI_DIGIVOX_III) },
1441 { }
1442};
1443MODULE_DEVICE_TABLE(usb, af9015_id_table);
Antti Palosaari80619de2008-09-15 17:18:09 -03001444
Antti Palosaari80619de2008-09-15 17:18:09 -03001445/* usb specific object needed to register this driver with the usb subsystem */
1446static struct usb_driver af9015_usb_driver = {
Antti Palosaaria3645e52012-06-07 20:36:35 -03001447 .name = KBUILD_MODNAME,
Antti Palosaari2d2b37c72012-06-12 01:05:20 -03001448 .id_table = af9015_id_table,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001449 .probe = dvb_usbv2_probe,
1450 .disconnect = dvb_usbv2_disconnect,
Antti Palosaari53dc1942012-06-12 02:20:01 -03001451 .suspend = dvb_usbv2_suspend,
1452 .resume = dvb_usbv2_resume,
Antti Palosaari04966aa2012-08-14 22:21:08 -03001453 .reset_resume = dvb_usbv2_reset_resume,
Antti Palosaaria3645e52012-06-07 20:36:35 -03001454 .no_dynamic_id = 1,
Antti Palosaari77f54512012-06-09 21:22:06 -03001455 .soft_unbind = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001456};
1457
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08001458module_usb_driver(af9015_usb_driver);
Antti Palosaari80619de2008-09-15 17:18:09 -03001459
1460MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
Antti Palosaaribc050e62012-05-08 06:04:24 -03001461MODULE_DESCRIPTION("Afatech AF9015 driver");
Antti Palosaari80619de2008-09-15 17:18:09 -03001462MODULE_LICENSE("GPL");
Antti Palosaaribab9b4f2012-09-12 11:37:25 -03001463MODULE_FIRMWARE(AF9015_FIRMWARE);