blob: e83b39d3993cebc70177891f5608f34b41ec2cc4 [file] [log] [blame]
Antti Palosaari7f882c22012-03-30 09:10:08 -03001/*
2 * Afatech AF9035 DVB USB driver
3 *
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#include "af9035.h"
Antti Palosaari7f882c22012-03-30 09:10:08 -030023
24DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
25static DEFINE_MUTEX(af9035_usb_mutex);
Antti Palosaarif2b61d02012-04-05 20:28:51 -030026static struct dvb_usb_device_properties af9035_properties[2];
Antti Palosaari7f882c22012-03-30 09:10:08 -030027static int af9035_properties_count = ARRAY_SIZE(af9035_properties);
Antti Palosaari7f882c22012-03-30 09:10:08 -030028
Michael Büschb1a95992012-04-01 16:33:48 -030029static u16 af9035_checksum(const u8 *buf, size_t len)
30{
31 size_t i;
32 u16 checksum = 0;
33
34 for (i = 1; i < len; i++) {
35 if (i % 2)
36 checksum += buf[i] << 8;
37 else
38 checksum += buf[i];
39 }
40 checksum = ~checksum;
41
42 return checksum;
43}
44
Antti Palosaari7f882c22012-03-30 09:10:08 -030045static int af9035_ctrl_msg(struct usb_device *udev, struct usb_req *req)
46{
Antti Palosaari6fb39c52012-04-06 16:32:30 -030047#define BUF_LEN 64
Antti Palosaari7f882c22012-03-30 09:10:08 -030048#define REQ_HDR_LEN 4 /* send header size */
49#define ACK_HDR_LEN 3 /* rece header size */
50#define CHECKSUM_LEN 2
51#define USB_TIMEOUT 2000
52
Antti Palosaari6fb39c52012-04-06 16:32:30 -030053 int ret, msg_len, act_len;
Antti Palosaari7f882c22012-03-30 09:10:08 -030054 u8 buf[BUF_LEN];
Antti Palosaari7f882c22012-03-30 09:10:08 -030055 static u8 seq; /* packet sequence number */
Antti Palosaari6fb39c52012-04-06 16:32:30 -030056 u16 checksum, tmp_checksum;
Antti Palosaari7f882c22012-03-30 09:10:08 -030057
58 /* buffer overflow check */
59 if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
60 req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
61 pr_debug("%s: too much data wlen=%d rlen=%d\n", __func__,
62 req->wlen, req->rlen);
63 return -EINVAL;
64 }
65
66 if (mutex_lock_interruptible(&af9035_usb_mutex) < 0)
67 return -EAGAIN;
68
69 buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
70 buf[1] = req->mbox;
71 buf[2] = req->cmd;
72 buf[3] = seq++;
73 if (req->wlen)
74 memcpy(&buf[4], req->wbuf, req->wlen);
75
76 /* calc and add checksum */
Michael Büschb1a95992012-04-01 16:33:48 -030077 checksum = af9035_checksum(buf, buf[0] - 1);
Antti Palosaari6fb39c52012-04-06 16:32:30 -030078 buf[buf[0] - 1] = (checksum >> 8);
79 buf[buf[0] - 0] = (checksum & 0xff);
Antti Palosaari7f882c22012-03-30 09:10:08 -030080
81 msg_len = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN ;
82
83 /* send req */
84 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
Antti Palosaari6fb39c52012-04-06 16:32:30 -030085 &act_len, USB_TIMEOUT);
Antti Palosaari7f882c22012-03-30 09:10:08 -030086 if (ret < 0)
87 err("bulk message failed=%d (%d/%d)", ret, msg_len, act_len);
88 else
89 if (act_len != msg_len)
90 ret = -EIO; /* all data is not send */
91 if (ret < 0)
92 goto err_mutex_unlock;
93
94 /* no ack for those packets */
95 if (req->cmd == CMD_FW_DL)
96 goto exit_mutex_unlock;
97
98 /* receive ack and data if read req */
99 msg_len = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
100 ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300101 &act_len, USB_TIMEOUT);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300102 if (ret < 0) {
103 err("recv bulk message failed=%d", ret);
104 ret = -EIO;
105 goto err_mutex_unlock;
106 }
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300107
Michael Büschb1a95992012-04-01 16:33:48 -0300108 if (act_len != msg_len) {
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300109 err("recv bulk message truncated (%d != %d)", act_len, msg_len);
Michael Büschb1a95992012-04-01 16:33:48 -0300110 ret = -EIO;
111 goto err_mutex_unlock;
112 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300113
Michael Büschb1a95992012-04-01 16:33:48 -0300114 /* verify checksum */
115 checksum = af9035_checksum(buf, act_len - 2);
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300116 tmp_checksum = (buf[act_len - 2] << 8) | buf[act_len - 1];
117 if (tmp_checksum != checksum) {
118 err("%s: command=%02x checksum mismatch (%04x != %04x)",
119 __func__, req->cmd, tmp_checksum, checksum);
Michael Büschb1a95992012-04-01 16:33:48 -0300120 ret = -EIO;
121 goto err_mutex_unlock;
122 }
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300123
Antti Palosaari7f882c22012-03-30 09:10:08 -0300124 /* check status */
125 if (buf[2]) {
126 pr_debug("%s: command=%02x failed fw error=%d\n", __func__,
127 req->cmd, buf[2]);
128 ret = -EIO;
129 goto err_mutex_unlock;
130 }
131
132 /* read request, copy returned data to return buf */
133 if (req->rlen)
134 memcpy(req->rbuf, &buf[ACK_HDR_LEN], req->rlen);
135
136err_mutex_unlock:
137exit_mutex_unlock:
138 mutex_unlock(&af9035_usb_mutex);
139
140 return ret;
141}
142
143/* write multiple registers */
144static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
145{
146 u8 wbuf[6 + len];
147 u8 mbox = (reg >> 16) & 0xff;
148 struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };
149
150 wbuf[0] = len;
151 wbuf[1] = 2;
152 wbuf[2] = 0;
153 wbuf[3] = 0;
154 wbuf[4] = (reg >> 8) & 0xff;
155 wbuf[5] = (reg >> 0) & 0xff;
156 memcpy(&wbuf[6], val, len);
157
158 return af9035_ctrl_msg(d->udev, &req);
159}
160
161/* read multiple registers */
162static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
163{
164 u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
165 u8 mbox = (reg >> 16) & 0xff;
166 struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
167
168 return af9035_ctrl_msg(d->udev, &req);
169}
170
171/* write single register */
172static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
173{
174 return af9035_wr_regs(d, reg, &val, 1);
175}
176
177/* read single register */
178static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
179{
180 return af9035_rd_regs(d, reg, val, 1);
181}
182
183/* write single register with mask */
184static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
185 u8 mask)
186{
187 int ret;
188 u8 tmp;
189
190 /* no need for read if whole reg is written */
191 if (mask != 0xff) {
192 ret = af9035_rd_regs(d, reg, &tmp, 1);
193 if (ret)
194 return ret;
195
196 val &= mask;
197 tmp &= ~mask;
198 val |= tmp;
199 }
200
201 return af9035_wr_regs(d, reg, &val, 1);
202}
203
204static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
205 struct i2c_msg msg[], int num)
206{
207 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300208 struct state *state = d->priv;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300209 int ret;
210
211 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
212 return -EAGAIN;
213
Antti Palosaariad30e912012-04-02 20:18:59 -0300214 /*
215 * I2C sub header is 5 bytes long. Meaning of those bytes are:
216 * 0: data len
217 * 1: I2C addr << 1
218 * 2: reg addr len
219 * byte 3 and 4 can be used as reg addr
220 * 3: reg addr MSB
221 * used when reg addr len is set to 2
222 * 4: reg addr LSB
223 * used when reg addr len is set to 1 or 2
224 *
225 * For the simplify we do not use register addr at all.
226 * NOTE: As a firmware knows tuner type there is very small possibility
227 * there could be some tuner I2C hacks done by firmware and this may
228 * lead problems if firmware expects those bytes are used.
229 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300230 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
231 (msg[1].flags & I2C_M_RD)) {
232 if (msg[0].len > 40 || msg[1].len > 40) {
233 /* TODO: correct limits > 40 */
234 ret = -EOPNOTSUPP;
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300235 } else if (msg[0].addr == state->af9033_config[0].i2c_addr) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300236 /* integrated demod */
237 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
238 msg[0].buf[2];
239 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
240 msg[1].len);
241 } else {
242 /* I2C */
Antti Palosaariad30e912012-04-02 20:18:59 -0300243 u8 buf[5 + msg[0].len];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300244 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
245 buf, msg[1].len, msg[1].buf };
Hans-Frieder Vogt812fe6d2012-04-01 14:11:29 -0300246 buf[0] = msg[1].len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300247 buf[1] = msg[0].addr << 1;
Antti Palosaariad30e912012-04-02 20:18:59 -0300248 buf[2] = 0x00; /* reg addr len */
249 buf[3] = 0x00; /* reg addr MSB */
250 buf[4] = 0x00; /* reg addr LSB */
251 memcpy(&buf[5], msg[0].buf, msg[0].len);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300252 ret = af9035_ctrl_msg(d->udev, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300253 }
254 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
255 if (msg[0].len > 40) {
256 /* TODO: correct limits > 40 */
257 ret = -EOPNOTSUPP;
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300258 } else if (msg[0].addr == state->af9033_config[0].i2c_addr) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300259 /* integrated demod */
260 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
261 msg[0].buf[2];
262 ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
263 msg[0].len - 3);
264 } else {
265 /* I2C */
Antti Palosaariad30e912012-04-02 20:18:59 -0300266 u8 buf[5 + msg[0].len];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300267 struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
268 0, NULL };
269 buf[0] = msg[0].len;
270 buf[1] = msg[0].addr << 1;
Antti Palosaariad30e912012-04-02 20:18:59 -0300271 buf[2] = 0x00; /* reg addr len */
272 buf[3] = 0x00; /* reg addr MSB */
273 buf[4] = 0x00; /* reg addr LSB */
274 memcpy(&buf[5], msg[0].buf, msg[0].len);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300275 ret = af9035_ctrl_msg(d->udev, &req);
276 }
277 } else {
278 /*
279 * We support only two kind of I2C transactions:
280 * 1) 1 x read + 1 x write
281 * 2) 1 x write
282 */
283 ret = -EOPNOTSUPP;
284 }
285
286 mutex_unlock(&d->i2c_mutex);
287
288 if (ret < 0)
289 return ret;
290 else
291 return num;
292}
293
294static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
295{
296 return I2C_FUNC_I2C;
297}
298
299static struct i2c_algorithm af9035_i2c_algo = {
300 .master_xfer = af9035_i2c_master_xfer,
301 .functionality = af9035_i2c_functionality,
302};
303
Hans-Frieder Vogt3234bd22012-04-21 18:23:16 -0300304#define AF9035_POLL 250
305static int af9035_rc_query(struct dvb_usb_device *d)
306{
307 unsigned int key;
308 unsigned char b[4];
309 int ret;
310 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, b };
311
312 ret = af9035_ctrl_msg(d->udev, &req);
313 if (ret < 0)
314 goto err;
315
316 if ((b[2] + b[3]) == 0xff) {
317 if ((b[0] + b[1]) == 0xff) {
318 /* NEC */
319 key = b[0] << 8 | b[2];
320 } else {
321 /* ext. NEC */
322 key = b[0] << 16 | b[1] << 8 | b[2];
323 }
324 } else {
325 key = b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3];
326 }
327
328 rc_keydown(d->rc_dev, key, 0);
329
330err:
331 /* ignore errors */
332 return 0;
333}
334
Antti Palosaari7f882c22012-03-30 09:10:08 -0300335static int af9035_init(struct dvb_usb_device *d)
336{
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300337 struct state *state = d->priv;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300338 int ret, i;
339 u16 frame_size = 87 * 188 / 4;
340 u8 packet_size = 512 / 4;
341 struct reg_val_mask tab[] = {
342 { 0x80f99d, 0x01, 0x01 },
343 { 0x80f9a4, 0x01, 0x01 },
344 { 0x00dd11, 0x00, 0x20 },
345 { 0x00dd11, 0x00, 0x40 },
346 { 0x00dd13, 0x00, 0x20 },
347 { 0x00dd13, 0x00, 0x40 },
348 { 0x00dd11, 0x20, 0x20 },
349 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
350 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
351 { 0x00dd0c, packet_size, 0xff},
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300352 { 0x00dd11, state->dual_mode << 6, 0x40 },
Antti Palosaari7f882c22012-03-30 09:10:08 -0300353 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
354 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
355 { 0x00dd0d, packet_size, 0xff },
356 { 0x80f9a3, 0x00, 0x01 },
357 { 0x80f9cd, 0x00, 0x01 },
358 { 0x80f99d, 0x00, 0x01 },
359 { 0x80f9a4, 0x00, 0x01 },
360 };
361
362 pr_debug("%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
363 __func__, d->udev->speed, frame_size, packet_size);
364
365 /* init endpoints */
366 for (i = 0; i < ARRAY_SIZE(tab); i++) {
367 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
368 tab[i].mask);
369 if (ret < 0)
370 goto err;
371 }
372
373 return 0;
374
375err:
376 pr_debug("%s: failed=%d\n", __func__, ret);
377
378 return ret;
379}
380
381static int af9035_identify_state(struct usb_device *udev,
382 struct dvb_usb_device_properties *props,
383 struct dvb_usb_device_description **desc,
384 int *cold)
385{
386 int ret;
387 u8 wbuf[1] = { 1 };
388 u8 rbuf[4];
389 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
390 sizeof(rbuf), rbuf };
391
392 ret = af9035_ctrl_msg(udev, &req);
393 if (ret < 0)
394 goto err;
395
396 pr_debug("%s: reply=%02x %02x %02x %02x\n", __func__,
397 rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
398 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
399 *cold = 0;
400 else
401 *cold = 1;
402
403 return 0;
404
405err:
406 pr_debug("%s: failed=%d\n", __func__, ret);
407
408 return ret;
409}
410
411static int af9035_download_firmware(struct usb_device *udev,
412 const struct firmware *fw)
413{
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300414 int ret, i, j, len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300415 u8 wbuf[1];
416 u8 rbuf[4];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300417 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
418 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
419 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300420 u8 hdr_core;
421 u16 hdr_addr, hdr_data_len, hdr_checksum;
Antti Palosaari6fb39c52012-04-06 16:32:30 -0300422 #define MAX_DATA 58
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300423 #define HDR_SIZE 7
Antti Palosaari7f882c22012-03-30 09:10:08 -0300424
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300425 /*
426 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
427 *
428 * byte 0: MCS 51 core
429 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
430 * address spaces
431 * byte 1-2: Big endian destination address
432 * byte 3-4: Big endian number of data bytes following the header
433 * byte 5-6: Big endian header checksum, apparently ignored by the chip
434 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
435 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300436
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300437 for (i = fw->size; i > HDR_SIZE;) {
438 hdr_core = fw->data[fw->size - i + 0];
439 hdr_addr = fw->data[fw->size - i + 1] << 8;
440 hdr_addr |= fw->data[fw->size - i + 2] << 0;
441 hdr_data_len = fw->data[fw->size - i + 3] << 8;
442 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
443 hdr_checksum = fw->data[fw->size - i + 5] << 8;
444 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300445
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300446 pr_debug("%s: core=%d addr=%04x data_len=%d checksum=%04x\n",
447 __func__, hdr_core, hdr_addr, hdr_data_len,
448 hdr_checksum);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300449
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300450 if (((hdr_core != 1) && (hdr_core != 2)) ||
451 (hdr_data_len > i)) {
452 pr_debug("%s: bad firmware\n", __func__);
453 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300454 }
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300455
456 /* download begin packet */
457 req.cmd = CMD_FW_DL_BEGIN;
458 ret = af9035_ctrl_msg(udev, &req);
Antti Palosaari41d44a82012-04-01 11:06:23 -0300459 if (ret < 0)
460 goto err;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300461
462 /* download firmware packet(s) */
463 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
464 len = j;
465 if (len > MAX_DATA)
466 len = MAX_DATA;
467 req_fw_dl.wlen = len;
468 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
469 HDR_SIZE + hdr_data_len - j];
470 ret = af9035_ctrl_msg(udev, &req_fw_dl);
471 if (ret < 0)
472 goto err;
473 }
474
475 /* download end packet */
476 req.cmd = CMD_FW_DL_END;
477 ret = af9035_ctrl_msg(udev, &req);
478 if (ret < 0)
479 goto err;
480
481 i -= hdr_data_len + HDR_SIZE;
482
Gianluca Gennari6ec12982012-04-02 20:32:58 -0300483 pr_debug("%s: data uploaded=%zu\n", __func__, fw->size - i);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300484 }
485
486 /* firmware loaded, request boot */
487 req.cmd = CMD_FW_BOOT;
488 ret = af9035_ctrl_msg(udev, &req);
489 if (ret < 0)
490 goto err;
491
492 /* ensure firmware starts */
493 wbuf[0] = 1;
494 ret = af9035_ctrl_msg(udev, &req_fw_ver);
495 if (ret < 0)
496 goto err;
497
Antti Palosaari7f882c22012-03-30 09:10:08 -0300498 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300499 info("firmware did not run");
Antti Palosaari7f882c22012-03-30 09:10:08 -0300500 ret = -ENODEV;
501 goto err;
502 }
503
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300504 info("firmware version=%d.%d.%d.%d", rbuf[0], rbuf[1], rbuf[2],
505 rbuf[3]);
506
Antti Palosaari7f882c22012-03-30 09:10:08 -0300507 return 0;
508
509err:
510 pr_debug("%s: failed=%d\n", __func__, ret);
511
512 return ret;
513}
514
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300515static int af9035_download_firmware_it9135(struct usb_device *udev,
516 const struct firmware *fw)
517{
518 int ret, i, i_prev;
519 u8 wbuf[1];
520 u8 rbuf[4];
521 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
522 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
523 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
524 #define HDR_SIZE 7
525
526 /*
527 * There seems to be following firmware header. Meaning of bytes 0-3
528 * is unknown.
529 *
530 * 0: 3
531 * 1: 0, 1
532 * 2: 0
533 * 3: 1, 2, 3
534 * 4: addr MSB
535 * 5: addr LSB
536 * 6: count of data bytes ?
537 */
538
539 for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
540 if (i == fw->size ||
541 (fw->data[i + 0] == 0x03 &&
542 (fw->data[i + 1] == 0x00 ||
543 fw->data[i + 1] == 0x01) &&
544 fw->data[i + 2] == 0x00)) {
545 req_fw_dl.wlen = i - i_prev;
546 req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
547 i_prev = i;
548 ret = af9035_ctrl_msg(udev, &req_fw_dl);
549 if (ret < 0)
550 goto err;
551
552 pr_debug("%s: data uploaded=%d\n", __func__, i);
553 }
554 }
555
556 /* firmware loaded, request boot */
557 req.cmd = CMD_FW_BOOT;
558 ret = af9035_ctrl_msg(udev, &req);
559 if (ret < 0)
560 goto err;
561
562 /* ensure firmware starts */
563 wbuf[0] = 1;
564 ret = af9035_ctrl_msg(udev, &req_fw_ver);
565 if (ret < 0)
566 goto err;
567
568 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
569 info("firmware did not run");
570 ret = -ENODEV;
571 goto err;
572 }
573
574 info("firmware version=%d.%d.%d.%d", rbuf[0], rbuf[1], rbuf[2],
575 rbuf[3]);
576
577 return 0;
578
579err:
580 pr_debug("%s: failed=%d\n", __func__, ret);
581
582 return ret;
583}
584
Antti Palosaari7f882c22012-03-30 09:10:08 -0300585/* abuse that callback as there is no better one for reading eeprom */
586static int af9035_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
587{
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300588 struct state *state = d->priv;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300589 int ret, i, eeprom_shift = 0;
590 u8 tmp;
591 u16 tmp16;
592
593 /* check if there is dual tuners */
594 ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp);
595 if (ret < 0)
596 goto err;
597
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300598 state->dual_mode = tmp;
599 pr_debug("%s: dual mode=%d\n", __func__, state->dual_mode);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300600
601 for (i = 0; i < af9035_properties[0].num_adapters; i++) {
602 /* tuner */
603 ret = af9035_rd_reg(d, EEPROM_1_TUNER_ID + eeprom_shift, &tmp);
604 if (ret < 0)
605 goto err;
606
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300607 state->af9033_config[i].tuner = tmp;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300608 pr_debug("%s: [%d]tuner=%02x\n", __func__, i, tmp);
609
610 switch (tmp) {
611 case AF9033_TUNER_TUA9001:
Michael Büschffc501f2012-04-02 12:18:36 -0300612 case AF9033_TUNER_FC0011:
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300613 case AF9033_TUNER_MXL5007T:
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300614 case AF9033_TUNER_TDA18218:
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300615 state->af9033_config[i].spec_inv = 1;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300616 break;
617 default:
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300618 warn("tuner ID=%02x not supported, please report!",
Antti Palosaari7f882c22012-03-30 09:10:08 -0300619 tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300620 };
621
622 /* tuner IF frequency */
623 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp);
624 if (ret < 0)
625 goto err;
626
627 tmp16 = tmp;
628
629 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp);
630 if (ret < 0)
631 goto err;
632
633 tmp16 |= tmp << 8;
634
635 pr_debug("%s: [%d]IF=%d\n", __func__, i, tmp16);
636
637 eeprom_shift = 0x10; /* shift for the 2nd tuner params */
638 }
639
640 /* get demod clock */
641 ret = af9035_rd_reg(d, 0x00d800, &tmp);
642 if (ret < 0)
643 goto err;
644
645 tmp = (tmp >> 0) & 0x0f;
646
647 for (i = 0; i < af9035_properties[0].num_adapters; i++)
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300648 state->af9033_config[i].clock = clock_lut[tmp];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300649
Hans-Frieder Vogt3234bd22012-04-21 18:23:16 -0300650 ret = af9035_rd_reg(d, EEPROM_IR_MODE, &tmp);
651 if (ret < 0)
652 goto err;
653 pr_debug("%s: ir_mode=%02x\n", __func__, tmp);
654
655 /* don't activate rc if in HID mode or if not available */
656 if (tmp == 5) {
657 ret = af9035_rd_reg(d, EEPROM_IR_TYPE, &tmp);
658 if (ret < 0)
659 goto err;
660 pr_debug("%s: ir_type=%02x\n", __func__, tmp);
661
662 switch (tmp) {
663 case 0: /* NEC */
664 default:
665 d->props.rc.core.protocol = RC_TYPE_NEC;
666 d->props.rc.core.allowed_protos = RC_TYPE_NEC;
667 break;
668 case 1: /* RC6 */
669 d->props.rc.core.protocol = RC_TYPE_RC6;
670 d->props.rc.core.allowed_protos = RC_TYPE_RC6;
671 break;
672 }
673 d->props.rc.core.rc_query = af9035_rc_query;
674 }
675
Antti Palosaari7f882c22012-03-30 09:10:08 -0300676 return 0;
677
678err:
679 pr_debug("%s: failed=%d\n", __func__, ret);
680
681 return ret;
682}
683
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300684/* abuse that callback as there is no better one for reading eeprom */
685static int af9035_read_mac_address_it9135(struct dvb_usb_device *d, u8 mac[6])
686{
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300687 struct state *state = d->priv;
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300688 int ret, i;
689 u8 tmp;
690
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300691 state->dual_mode = false;
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300692
693 /* get demod clock */
694 ret = af9035_rd_reg(d, 0x00d800, &tmp);
695 if (ret < 0)
696 goto err;
697
698 tmp = (tmp >> 0) & 0x0f;
699
700 for (i = 0; i < af9035_properties[0].num_adapters; i++)
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300701 state->af9033_config[i].clock = clock_lut_it9135[tmp];
Antti Palosaarif2b61d02012-04-05 20:28:51 -0300702
703 return 0;
704
705err:
706 pr_debug("%s: failed=%d\n", __func__, ret);
707
708 return ret;
709}
710
Michael Büschffc501f2012-04-02 12:18:36 -0300711static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300712 int cmd, int arg)
Michael Büschffc501f2012-04-02 12:18:36 -0300713{
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300714 int ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300715
716 switch (cmd) {
717 case FC0011_FE_CALLBACK_POWER:
718 /* Tuner enable */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300719 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
720 if (ret < 0)
721 goto err;
722
723 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
724 if (ret < 0)
725 goto err;
726
727 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
728 if (ret < 0)
729 goto err;
730
Michael Büschffc501f2012-04-02 12:18:36 -0300731 /* LED */
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300732 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
733 if (ret < 0)
734 goto err;
735
736 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
737 if (ret < 0)
738 goto err;
739
Michael Büschde2bec52012-04-03 05:11:30 -0300740 usleep_range(10000, 50000);
Michael Büschffc501f2012-04-02 12:18:36 -0300741 break;
742 case FC0011_FE_CALLBACK_RESET:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300743 ret = af9035_wr_reg(d, 0xd8e9, 1);
744 if (ret < 0)
745 goto err;
746
747 ret = af9035_wr_reg(d, 0xd8e8, 1);
748 if (ret < 0)
749 goto err;
750
751 ret = af9035_wr_reg(d, 0xd8e7, 1);
752 if (ret < 0)
753 goto err;
754
Michael Büschde2bec52012-04-03 05:11:30 -0300755 usleep_range(10000, 20000);
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300756
757 ret = af9035_wr_reg(d, 0xd8e7, 0);
758 if (ret < 0)
759 goto err;
760
Michael Büschde2bec52012-04-03 05:11:30 -0300761 usleep_range(10000, 20000);
Michael Büschffc501f2012-04-02 12:18:36 -0300762 break;
763 default:
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300764 ret = -EINVAL;
765 goto err;
Michael Büschffc501f2012-04-02 12:18:36 -0300766 }
767
768 return 0;
Antti Palosaaridaacd5b2012-04-05 21:52:21 -0300769
770err:
771 pr_debug("%s: failed=%d\n", __func__, ret);
772
773 return ret;
Michael Büschffc501f2012-04-02 12:18:36 -0300774}
775
776static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
777{
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300778 struct state *state = d->priv;
779
780 switch (state->af9033_config[0].tuner) {
Michael Büschffc501f2012-04-02 12:18:36 -0300781 case AF9033_TUNER_FC0011:
782 return af9035_fc0011_tuner_callback(d, cmd, arg);
783 default:
784 break;
785 }
786
787 return -ENODEV;
788}
789
790static int af9035_frontend_callback(void *adapter_priv, int component,
791 int cmd, int arg)
792{
793 struct i2c_adapter *adap = adapter_priv;
794 struct dvb_usb_device *d = i2c_get_adapdata(adap);
795
796 switch (component) {
797 case DVB_FRONTEND_COMPONENT_TUNER:
798 return af9035_tuner_callback(d, cmd, arg);
799 default:
800 break;
801 }
802
803 return -EINVAL;
804}
805
Antti Palosaari7f882c22012-03-30 09:10:08 -0300806static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
807{
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300808 struct state *state = adap->dev->priv;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300809 int ret;
810
Antti Palosaari1cbabf92012-05-07 14:59:55 -0300811 if (!state->af9033_config[adap->id].tuner) {
812 /* unsupported tuner */
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300813 ret = -ENODEV;
814 goto err;
815 }
816
Antti Palosaari7f882c22012-03-30 09:10:08 -0300817 if (adap->id == 0) {
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300818 state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
819 state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
820
Antti Palosaari7f882c22012-03-30 09:10:08 -0300821 ret = af9035_wr_reg(adap->dev, 0x00417f,
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300822 state->af9033_config[1].i2c_addr);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300823 if (ret < 0)
824 goto err;
825
826 ret = af9035_wr_reg(adap->dev, 0x00d81a,
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300827 state->dual_mode);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300828 if (ret < 0)
829 goto err;
830 }
831
832 /* attach demodulator */
833 adap->fe_adap[0].fe = dvb_attach(af9033_attach,
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300834 &state->af9033_config[adap->id], &adap->dev->i2c_adap);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300835 if (adap->fe_adap[0].fe == NULL) {
836 ret = -ENODEV;
837 goto err;
838 }
Antti Palosaari852f0d92012-04-06 07:09:23 -0300839
840 /* disable I2C-gate */
841 adap->fe_adap[0].fe->ops.i2c_gate_ctrl = NULL;
Michael Büschffc501f2012-04-02 12:18:36 -0300842 adap->fe_adap[0].fe->callback = af9035_frontend_callback;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300843
844 return 0;
845
846err:
847 pr_debug("%s: failed=%d\n", __func__, ret);
848
849 return ret;
850}
851
852static struct tua9001_config af9035_tua9001_config = {
853 .i2c_addr = 0x60,
854};
855
Michael Büschffc501f2012-04-02 12:18:36 -0300856static const struct fc0011_config af9035_fc0011_config = {
857 .i2c_address = 0x60,
858};
859
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300860static struct mxl5007t_config af9035_mxl5007t_config = {
861 .xtal_freq_hz = MxL_XTAL_24_MHZ,
862 .if_freq_hz = MxL_IF_4_57_MHZ,
863 .invert_if = 0,
864 .loop_thru_enable = 0,
865 .clk_out_enable = 0,
866 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
867};
868
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300869static struct tda18218_config af9035_tda18218_config = {
870 .i2c_address = 0x60,
871 .i2c_wr_max = 21,
872};
873
Antti Palosaari7f882c22012-03-30 09:10:08 -0300874static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
875{
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300876 struct state *state = adap->dev->priv;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300877 int ret;
878 struct dvb_frontend *fe;
879
Antti Palosaari2a79eef2012-05-07 14:50:40 -0300880 switch (state->af9033_config[adap->id].tuner) {
Antti Palosaari7f882c22012-03-30 09:10:08 -0300881 case AF9033_TUNER_TUA9001:
882 /* AF9035 gpiot3 = TUA9001 RESETN
883 AF9035 gpiot2 = TUA9001 RXEN */
884
885 /* configure gpiot2 and gpiot2 as output */
886 ret = af9035_wr_reg_mask(adap->dev, 0x00d8ec, 0x01, 0x01);
887 if (ret < 0)
888 goto err;
889
890 ret = af9035_wr_reg_mask(adap->dev, 0x00d8ed, 0x01, 0x01);
891 if (ret < 0)
892 goto err;
893
894 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e8, 0x01, 0x01);
895 if (ret < 0)
896 goto err;
897
898 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e9, 0x01, 0x01);
899 if (ret < 0)
900 goto err;
901
902 /* reset tuner */
903 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e7, 0x00, 0x01);
904 if (ret < 0)
905 goto err;
906
907 usleep_range(2000, 20000);
908
909 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e7, 0x01, 0x01);
910 if (ret < 0)
911 goto err;
912
913 /* activate tuner RX */
914 /* TODO: use callback for TUA9001 RXEN */
915 ret = af9035_wr_reg_mask(adap->dev, 0x00d8eb, 0x01, 0x01);
916 if (ret < 0)
917 goto err;
918
919 /* attach tuner */
920 fe = dvb_attach(tua9001_attach, adap->fe_adap[0].fe,
921 &adap->dev->i2c_adap, &af9035_tua9001_config);
922 break;
Michael Büschffc501f2012-04-02 12:18:36 -0300923 case AF9033_TUNER_FC0011:
924 fe = dvb_attach(fc0011_attach, adap->fe_adap[0].fe,
925 &adap->dev->i2c_adap, &af9035_fc0011_config);
926 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300927 case AF9033_TUNER_MXL5007T:
928 ret = af9035_wr_reg(adap->dev, 0x00d8e0, 1);
929 if (ret < 0)
930 goto err;
931 ret = af9035_wr_reg(adap->dev, 0x00d8e1, 1);
932 if (ret < 0)
933 goto err;
934 ret = af9035_wr_reg(adap->dev, 0x00d8df, 0);
935 if (ret < 0)
936 goto err;
937
938 msleep(30);
939
940 ret = af9035_wr_reg(adap->dev, 0x00d8df, 1);
941 if (ret < 0)
942 goto err;
943
944 msleep(300);
945
946 ret = af9035_wr_reg(adap->dev, 0x00d8c0, 1);
947 if (ret < 0)
948 goto err;
949 ret = af9035_wr_reg(adap->dev, 0x00d8c1, 1);
950 if (ret < 0)
951 goto err;
952 ret = af9035_wr_reg(adap->dev, 0x00d8bf, 0);
953 if (ret < 0)
954 goto err;
955 ret = af9035_wr_reg(adap->dev, 0x00d8b4, 1);
956 if (ret < 0)
957 goto err;
958 ret = af9035_wr_reg(adap->dev, 0x00d8b5, 1);
959 if (ret < 0)
960 goto err;
961 ret = af9035_wr_reg(adap->dev, 0x00d8b3, 1);
962 if (ret < 0)
963 goto err;
964
965 /* attach tuner */
966 fe = dvb_attach(mxl5007t_attach, adap->fe_adap[0].fe,
967 &adap->dev->i2c_adap, 0x60, &af9035_mxl5007t_config);
968 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300969 case AF9033_TUNER_TDA18218:
970 /* attach tuner */
971 fe = dvb_attach(tda18218_attach, adap->fe_adap[0].fe,
972 &adap->dev->i2c_adap, &af9035_tda18218_config);
973 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300974 default:
975 fe = NULL;
976 }
977
978 if (fe == NULL) {
979 ret = -ENODEV;
980 goto err;
981 }
982
983 return 0;
984
985err:
986 pr_debug("%s: failed=%d\n", __func__, ret);
987
988 return ret;
989}
990
991enum af9035_id_entry {
Michael Büsch1083a0f2012-04-02 12:34:52 -0300992 AF9035_15A4_9035,
Antti Palosaarid281c1f2012-05-07 15:16:36 -0300993 AF9035_15A4_1000,
Michael Büsch1083a0f2012-04-02 12:34:52 -0300994 AF9035_15A4_1001,
Antti Palosaarid281c1f2012-05-07 15:16:36 -0300995 AF9035_15A4_1002,
996 AF9035_15A4_1003,
Antti Palosaarib55b4b72012-04-05 21:58:09 -0300997 AF9035_0CCD_0093,
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300998 AF9035_07CA_A835,
999 AF9035_07CA_B835,
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001000 AF9035_07CA_1867,
Gianluca Gennari728827b2012-04-02 17:25:13 -03001001 AF9035_07CA_A867,
Pierangelo Terzullidbac01f2012-04-05 21:26:18 -03001002 AF9035_07CA_0825,
Antti Palosaari7f882c22012-03-30 09:10:08 -03001003};
1004
1005static struct usb_device_id af9035_id[] = {
Michael Büsch1083a0f2012-04-02 12:34:52 -03001006 [AF9035_15A4_9035] = {
Antti Palosaarid281c1f2012-05-07 15:16:36 -03001007 USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035)},
1008 [AF9035_15A4_1000] = {
1009 USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000)},
Michael Büsch1083a0f2012-04-02 12:34:52 -03001010 [AF9035_15A4_1001] = {
Antti Palosaarid281c1f2012-05-07 15:16:36 -03001011 USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001)},
1012 [AF9035_15A4_1002] = {
1013 USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002)},
1014 [AF9035_15A4_1003] = {
1015 USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003)},
Antti Palosaarib55b4b72012-04-05 21:58:09 -03001016 [AF9035_0CCD_0093] = {
1017 USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK)},
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001018 [AF9035_07CA_A835] = {
1019 USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835)},
1020 [AF9035_07CA_B835] = {
1021 USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835)},
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001022 [AF9035_07CA_1867] = {
1023 USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867)},
Gianluca Gennari728827b2012-04-02 17:25:13 -03001024 [AF9035_07CA_A867] = {
1025 USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867)},
Pierangelo Terzullidbac01f2012-04-05 21:26:18 -03001026 [AF9035_07CA_0825] = {
1027 USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR)},
Antti Palosaari7f882c22012-03-30 09:10:08 -03001028 {},
1029};
1030
1031MODULE_DEVICE_TABLE(usb, af9035_id);
1032
1033static struct dvb_usb_device_properties af9035_properties[] = {
1034 {
1035 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1036
1037 .usb_ctrl = DEVICE_SPECIFIC,
1038 .download_firmware = af9035_download_firmware,
Antti Palosaari77c5ff22012-04-01 01:32:23 -03001039 .firmware = "dvb-usb-af9035-02.fw",
Antti Palosaari7f882c22012-03-30 09:10:08 -03001040 .no_reconnect = 1,
1041
Antti Palosaari2a79eef2012-05-07 14:50:40 -03001042 .size_of_priv = sizeof(struct state),
1043
Antti Palosaari7f882c22012-03-30 09:10:08 -03001044 .num_adapters = 1,
1045 .adapter = {
1046 {
1047 .num_frontends = 1,
1048 .fe = {
1049 {
1050 .frontend_attach = af9035_frontend_attach,
1051 .tuner_attach = af9035_tuner_attach,
1052 .stream = {
1053 .type = USB_BULK,
1054 .count = 6,
1055 .endpoint = 0x84,
1056 .u = {
1057 .bulk = {
1058 .buffersize = (87 * 188),
1059 }
1060 }
1061 }
1062 }
1063 }
1064 }
1065 },
1066
1067 .identify_state = af9035_identify_state,
1068 .read_mac_address = af9035_read_mac_address,
1069
1070 .i2c_algo = &af9035_i2c_algo,
1071
Hans-Frieder Vogt3234bd22012-04-21 18:23:16 -03001072 .rc.core = {
1073 .protocol = RC_TYPE_UNKNOWN,
1074 .module_name = "af9035",
1075 .rc_query = NULL,
1076 .rc_interval = AF9035_POLL,
1077 .allowed_protos = RC_TYPE_UNKNOWN,
1078 .rc_codes = RC_MAP_EMPTY,
1079 },
Pierangelo Terzullidbac01f2012-04-05 21:26:18 -03001080 .num_device_descs = 5,
Antti Palosaari7f882c22012-03-30 09:10:08 -03001081 .devices = {
1082 {
Antti Palosaarib55b4b72012-04-05 21:58:09 -03001083 .name = "Afatech AF9035 reference design",
Michael Büsch1083a0f2012-04-02 12:34:52 -03001084 .cold_ids = {
1085 &af9035_id[AF9035_15A4_9035],
Antti Palosaarid281c1f2012-05-07 15:16:36 -03001086 &af9035_id[AF9035_15A4_1000],
Michael Büsch1083a0f2012-04-02 12:34:52 -03001087 &af9035_id[AF9035_15A4_1001],
Antti Palosaarid281c1f2012-05-07 15:16:36 -03001088 &af9035_id[AF9035_15A4_1002],
1089 &af9035_id[AF9035_15A4_1003],
Michael Büsch1083a0f2012-04-02 12:34:52 -03001090 },
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001091 }, {
Antti Palosaarib55b4b72012-04-05 21:58:09 -03001092 .name = "TerraTec Cinergy T Stick",
1093 .cold_ids = {
1094 &af9035_id[AF9035_0CCD_0093],
1095 },
1096 }, {
Gianluca Gennarice1fe372012-04-02 17:25:14 -03001097 .name = "AVerMedia AVerTV Volar HD/PRO (A835)",
1098 .cold_ids = {
1099 &af9035_id[AF9035_07CA_A835],
1100 &af9035_id[AF9035_07CA_B835],
1101 },
1102 }, {
Gianluca Gennari728827b2012-04-02 17:25:13 -03001103 .name = "AVerMedia HD Volar (A867)",
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001104 .cold_ids = {
1105 &af9035_id[AF9035_07CA_1867],
Gianluca Gennari728827b2012-04-02 17:25:13 -03001106 &af9035_id[AF9035_07CA_A867],
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001107 },
Pierangelo Terzullidbac01f2012-04-05 21:26:18 -03001108 }, {
1109 .name = "AVerMedia Twinstar (A825)",
1110 .cold_ids = {
1111 &af9035_id[AF9035_07CA_0825],
1112 },
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -03001113 },
Antti Palosaari7f882c22012-03-30 09:10:08 -03001114 }
1115 },
Antti Palosaarif2b61d02012-04-05 20:28:51 -03001116 {
1117 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1118
1119 .usb_ctrl = DEVICE_SPECIFIC,
1120 .download_firmware = af9035_download_firmware_it9135,
1121 .firmware = "dvb-usb-it9135-01.fw",
1122 .no_reconnect = 1,
1123
Antti Palosaari2a79eef2012-05-07 14:50:40 -03001124 .size_of_priv = sizeof(struct state),
1125
Antti Palosaarif2b61d02012-04-05 20:28:51 -03001126 .num_adapters = 1,
1127 .adapter = {
1128 {
1129 .num_frontends = 1,
1130 .fe = {
1131 {
1132 .frontend_attach = af9035_frontend_attach,
1133 .tuner_attach = af9035_tuner_attach,
1134 .stream = {
1135 .type = USB_BULK,
1136 .count = 6,
1137 .endpoint = 0x84,
1138 .u = {
1139 .bulk = {
1140 .buffersize = (87 * 188),
1141 }
1142 }
1143 }
1144 }
1145 }
1146 }
1147 },
1148
1149 .identify_state = af9035_identify_state,
1150 .read_mac_address = af9035_read_mac_address_it9135,
1151
1152 .i2c_algo = &af9035_i2c_algo,
1153
1154 .num_device_descs = 0, /* disabled as no support for IT9135 */
1155 .devices = {
1156 {
1157 .name = "ITE Tech. IT9135 reference design",
1158 },
1159 }
1160 },
Antti Palosaari7f882c22012-03-30 09:10:08 -03001161};
1162
1163static int af9035_usb_probe(struct usb_interface *intf,
1164 const struct usb_device_id *id)
1165{
1166 int ret, i;
1167 struct dvb_usb_device *d = NULL;
1168 struct usb_device *udev;
1169 bool found;
1170
1171 pr_debug("%s: interface=%d\n", __func__,
1172 intf->cur_altsetting->desc.bInterfaceNumber);
1173
1174 /* interface 0 is used by DVB-T receiver and
1175 interface 1 is for remote controller (HID) */
1176 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
1177 return 0;
1178
1179 /* Dynamic USB ID support. Replaces first device ID with current one. */
1180 udev = interface_to_usbdev(intf);
1181
1182 for (i = 0, found = false; i < ARRAY_SIZE(af9035_id) - 1; i++) {
1183 if (af9035_id[i].idVendor ==
1184 le16_to_cpu(udev->descriptor.idVendor) &&
1185 af9035_id[i].idProduct ==
1186 le16_to_cpu(udev->descriptor.idProduct)) {
1187 found = true;
1188 break;
1189 }
1190 }
1191
1192 if (!found) {
1193 pr_debug("%s: using dynamic ID %04x:%04x\n", __func__,
1194 le16_to_cpu(udev->descriptor.idVendor),
1195 le16_to_cpu(udev->descriptor.idProduct));
1196 af9035_properties[0].devices[0].cold_ids[0]->idVendor =
1197 le16_to_cpu(udev->descriptor.idVendor);
1198 af9035_properties[0].devices[0].cold_ids[0]->idProduct =
1199 le16_to_cpu(udev->descriptor.idProduct);
1200 }
1201
1202
1203 for (i = 0; i < af9035_properties_count; i++) {
1204 ret = dvb_usb_device_init(intf, &af9035_properties[i],
1205 THIS_MODULE, &d, adapter_nr);
1206
1207 if (ret == -ENODEV)
1208 continue;
1209 else
1210 break;
1211 }
1212
1213 if (ret < 0)
1214 goto err;
1215
1216 if (d) {
1217 ret = af9035_init(d);
1218 if (ret < 0)
1219 goto err;
1220 }
1221
1222 return 0;
1223
1224err:
1225 pr_debug("%s: failed=%d\n", __func__, ret);
1226
1227 return ret;
1228}
1229
1230/* usb specific object needed to register this driver with the usb subsystem */
1231static struct usb_driver af9035_usb_driver = {
1232 .name = "dvb_usb_af9035",
1233 .probe = af9035_usb_probe,
1234 .disconnect = dvb_usb_device_exit,
1235 .id_table = af9035_id,
1236};
1237
Gianluca Gennari48bf7e12012-04-02 17:25:17 -03001238module_usb_driver(af9035_usb_driver);
Antti Palosaari7f882c22012-03-30 09:10:08 -03001239
1240MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1241MODULE_DESCRIPTION("Afatech AF9035 driver");
1242MODULE_LICENSE("GPL");