blob: 92d27aa808009f32868df644eed32aad5d6e7088 [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"
23#include "af9033.h"
24#include "tua9001.h"
25
26DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
27static DEFINE_MUTEX(af9035_usb_mutex);
28static struct config af9035_config;
29static struct dvb_usb_device_properties af9035_properties[1];
30static int af9035_properties_count = ARRAY_SIZE(af9035_properties);
31static struct af9033_config af9035_af9033_config[] = {
32 {
33 .ts_mode = AF9033_TS_MODE_USB,
34 }, {
35 .ts_mode = AF9033_TS_MODE_SERIAL,
36 }
37};
38
Michael Büschb1a95992012-04-01 16:33:48 -030039static u16 af9035_checksum(const u8 *buf, size_t len)
40{
41 size_t i;
42 u16 checksum = 0;
43
44 for (i = 1; i < len; i++) {
45 if (i % 2)
46 checksum += buf[i] << 8;
47 else
48 checksum += buf[i];
49 }
50 checksum = ~checksum;
51
52 return checksum;
53}
54
Antti Palosaari7f882c22012-03-30 09:10:08 -030055static int af9035_ctrl_msg(struct usb_device *udev, struct usb_req *req)
56{
57#define BUF_LEN 63
58#define REQ_HDR_LEN 4 /* send header size */
59#define ACK_HDR_LEN 3 /* rece header size */
60#define CHECKSUM_LEN 2
61#define USB_TIMEOUT 2000
62
Michael Büschb1a95992012-04-01 16:33:48 -030063 int ret, act_len;
Antti Palosaari7f882c22012-03-30 09:10:08 -030064 u8 buf[BUF_LEN];
65 u32 msg_len;
66 static u8 seq; /* packet sequence number */
Michael Büschb1a95992012-04-01 16:33:48 -030067 u16 checksum, tmpsum;
Antti Palosaari7f882c22012-03-30 09:10:08 -030068
69 /* buffer overflow check */
70 if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
71 req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
72 pr_debug("%s: too much data wlen=%d rlen=%d\n", __func__,
73 req->wlen, req->rlen);
74 return -EINVAL;
75 }
76
77 if (mutex_lock_interruptible(&af9035_usb_mutex) < 0)
78 return -EAGAIN;
79
80 buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
81 buf[1] = req->mbox;
82 buf[2] = req->cmd;
83 buf[3] = seq++;
84 if (req->wlen)
85 memcpy(&buf[4], req->wbuf, req->wlen);
86
87 /* calc and add checksum */
Michael Büschb1a95992012-04-01 16:33:48 -030088 checksum = af9035_checksum(buf, buf[0] - 1);
Antti Palosaari7f882c22012-03-30 09:10:08 -030089 buf[buf[0]-1] = (checksum >> 8);
90 buf[buf[0]-0] = (checksum & 0xff);
91
92 msg_len = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN ;
93
94 /* send req */
95 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
96 &act_len, USB_TIMEOUT);
97 if (ret < 0)
98 err("bulk message failed=%d (%d/%d)", ret, msg_len, act_len);
99 else
100 if (act_len != msg_len)
101 ret = -EIO; /* all data is not send */
102 if (ret < 0)
103 goto err_mutex_unlock;
104
105 /* no ack for those packets */
106 if (req->cmd == CMD_FW_DL)
107 goto exit_mutex_unlock;
108
109 /* receive ack and data if read req */
110 msg_len = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
111 ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
112 &act_len, USB_TIMEOUT);
113 if (ret < 0) {
114 err("recv bulk message failed=%d", ret);
115 ret = -EIO;
116 goto err_mutex_unlock;
117 }
Michael Büschb1a95992012-04-01 16:33:48 -0300118 if (act_len != msg_len) {
119 err("recv bulk message truncated (%d != %u)\n",
120 act_len, (unsigned int)msg_len);
121 ret = -EIO;
122 goto err_mutex_unlock;
123 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300124
Michael Büschb1a95992012-04-01 16:33:48 -0300125 /* verify checksum */
126 checksum = af9035_checksum(buf, act_len - 2);
127 tmpsum = (buf[act_len - 2] << 8) | buf[act_len - 1];
128 if (tmpsum != checksum) {
129 err("%s: command=%02X checksum mismatch (%04X != %04X)\n",
130 __func__, req->cmd,
131 (unsigned int)tmpsum, (unsigned int)checksum);
132 ret = -EIO;
133 goto err_mutex_unlock;
134 }
Antti Palosaari7f882c22012-03-30 09:10:08 -0300135 /* check status */
136 if (buf[2]) {
137 pr_debug("%s: command=%02x failed fw error=%d\n", __func__,
138 req->cmd, buf[2]);
139 ret = -EIO;
140 goto err_mutex_unlock;
141 }
142
143 /* read request, copy returned data to return buf */
144 if (req->rlen)
145 memcpy(req->rbuf, &buf[ACK_HDR_LEN], req->rlen);
146
147err_mutex_unlock:
148exit_mutex_unlock:
149 mutex_unlock(&af9035_usb_mutex);
150
151 return ret;
152}
153
154/* write multiple registers */
155static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
156{
157 u8 wbuf[6 + len];
158 u8 mbox = (reg >> 16) & 0xff;
159 struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };
160
161 wbuf[0] = len;
162 wbuf[1] = 2;
163 wbuf[2] = 0;
164 wbuf[3] = 0;
165 wbuf[4] = (reg >> 8) & 0xff;
166 wbuf[5] = (reg >> 0) & 0xff;
167 memcpy(&wbuf[6], val, len);
168
169 return af9035_ctrl_msg(d->udev, &req);
170}
171
172/* read multiple registers */
173static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
174{
175 u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
176 u8 mbox = (reg >> 16) & 0xff;
177 struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
178
179 return af9035_ctrl_msg(d->udev, &req);
180}
181
182/* write single register */
183static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
184{
185 return af9035_wr_regs(d, reg, &val, 1);
186}
187
188/* read single register */
189static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
190{
191 return af9035_rd_regs(d, reg, val, 1);
192}
193
194/* write single register with mask */
195static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
196 u8 mask)
197{
198 int ret;
199 u8 tmp;
200
201 /* no need for read if whole reg is written */
202 if (mask != 0xff) {
203 ret = af9035_rd_regs(d, reg, &tmp, 1);
204 if (ret)
205 return ret;
206
207 val &= mask;
208 tmp &= ~mask;
209 val |= tmp;
210 }
211
212 return af9035_wr_regs(d, reg, &val, 1);
213}
214
215static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
216 struct i2c_msg msg[], int num)
217{
218 struct dvb_usb_device *d = i2c_get_adapdata(adap);
219 int ret;
220
221 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
222 return -EAGAIN;
223
224 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
225 (msg[1].flags & I2C_M_RD)) {
226 if (msg[0].len > 40 || msg[1].len > 40) {
227 /* TODO: correct limits > 40 */
228 ret = -EOPNOTSUPP;
229 } else if (msg[0].addr == af9035_af9033_config[0].i2c_addr) {
230 /* integrated demod */
231 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
232 msg[0].buf[2];
233 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
234 msg[1].len);
235 } else {
236 /* I2C */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300237 u8 buf[4 + msg[0].len];
238 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
239 buf, msg[1].len, msg[1].buf };
Hans-Frieder Vogt812fe6d2012-04-01 14:11:29 -0300240 buf[0] = msg[1].len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300241 buf[1] = msg[0].addr << 1;
242 buf[2] = 0x01;
243 buf[3] = 0x00;
244 memcpy(&buf[4], msg[0].buf, msg[0].len);
245 ret = af9035_ctrl_msg(d->udev, &req);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300246 }
247 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
248 if (msg[0].len > 40) {
249 /* TODO: correct limits > 40 */
250 ret = -EOPNOTSUPP;
251 } else if (msg[0].addr == af9035_af9033_config[0].i2c_addr) {
252 /* integrated demod */
253 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
254 msg[0].buf[2];
255 ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
256 msg[0].len - 3);
257 } else {
258 /* I2C */
259 u8 buf[4 + msg[0].len];
260 struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
261 0, NULL };
262 buf[0] = msg[0].len;
263 buf[1] = msg[0].addr << 1;
264 buf[2] = 0x01;
265 buf[3] = 0x00;
266 memcpy(&buf[4], msg[0].buf, msg[0].len);
267 ret = af9035_ctrl_msg(d->udev, &req);
268 }
269 } else {
270 /*
271 * We support only two kind of I2C transactions:
272 * 1) 1 x read + 1 x write
273 * 2) 1 x write
274 */
275 ret = -EOPNOTSUPP;
276 }
277
278 mutex_unlock(&d->i2c_mutex);
279
280 if (ret < 0)
281 return ret;
282 else
283 return num;
284}
285
286static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
287{
288 return I2C_FUNC_I2C;
289}
290
291static struct i2c_algorithm af9035_i2c_algo = {
292 .master_xfer = af9035_i2c_master_xfer,
293 .functionality = af9035_i2c_functionality,
294};
295
296static int af9035_init(struct dvb_usb_device *d)
297{
298 int ret, i;
299 u16 frame_size = 87 * 188 / 4;
300 u8 packet_size = 512 / 4;
301 struct reg_val_mask tab[] = {
302 { 0x80f99d, 0x01, 0x01 },
303 { 0x80f9a4, 0x01, 0x01 },
304 { 0x00dd11, 0x00, 0x20 },
305 { 0x00dd11, 0x00, 0x40 },
306 { 0x00dd13, 0x00, 0x20 },
307 { 0x00dd13, 0x00, 0x40 },
308 { 0x00dd11, 0x20, 0x20 },
309 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
310 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
311 { 0x00dd0c, packet_size, 0xff},
312 { 0x00dd11, af9035_config.dual_mode << 6, 0x40 },
313 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
314 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
315 { 0x00dd0d, packet_size, 0xff },
316 { 0x80f9a3, 0x00, 0x01 },
317 { 0x80f9cd, 0x00, 0x01 },
318 { 0x80f99d, 0x00, 0x01 },
319 { 0x80f9a4, 0x00, 0x01 },
320 };
321
322 pr_debug("%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
323 __func__, d->udev->speed, frame_size, packet_size);
324
325 /* init endpoints */
326 for (i = 0; i < ARRAY_SIZE(tab); i++) {
327 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
328 tab[i].mask);
329 if (ret < 0)
330 goto err;
331 }
332
333 return 0;
334
335err:
336 pr_debug("%s: failed=%d\n", __func__, ret);
337
338 return ret;
339}
340
341static int af9035_identify_state(struct usb_device *udev,
342 struct dvb_usb_device_properties *props,
343 struct dvb_usb_device_description **desc,
344 int *cold)
345{
346 int ret;
347 u8 wbuf[1] = { 1 };
348 u8 rbuf[4];
349 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
350 sizeof(rbuf), rbuf };
351
352 ret = af9035_ctrl_msg(udev, &req);
353 if (ret < 0)
354 goto err;
355
356 pr_debug("%s: reply=%02x %02x %02x %02x\n", __func__,
357 rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
358 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
359 *cold = 0;
360 else
361 *cold = 1;
362
363 return 0;
364
365err:
366 pr_debug("%s: failed=%d\n", __func__, ret);
367
368 return ret;
369}
370
371static int af9035_download_firmware(struct usb_device *udev,
372 const struct firmware *fw)
373{
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300374 int ret, i, j, len;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300375 u8 wbuf[1];
376 u8 rbuf[4];
Antti Palosaari7f882c22012-03-30 09:10:08 -0300377 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
378 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
379 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300380 u8 hdr_core;
381 u16 hdr_addr, hdr_data_len, hdr_checksum;
382 #define MAX_DATA 57
383 #define HDR_SIZE 7
Antti Palosaari7f882c22012-03-30 09:10:08 -0300384
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300385 /*
386 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
387 *
388 * byte 0: MCS 51 core
389 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
390 * address spaces
391 * byte 1-2: Big endian destination address
392 * byte 3-4: Big endian number of data bytes following the header
393 * byte 5-6: Big endian header checksum, apparently ignored by the chip
394 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
395 */
Antti Palosaari7f882c22012-03-30 09:10:08 -0300396
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300397 for (i = fw->size; i > HDR_SIZE;) {
398 hdr_core = fw->data[fw->size - i + 0];
399 hdr_addr = fw->data[fw->size - i + 1] << 8;
400 hdr_addr |= fw->data[fw->size - i + 2] << 0;
401 hdr_data_len = fw->data[fw->size - i + 3] << 8;
402 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
403 hdr_checksum = fw->data[fw->size - i + 5] << 8;
404 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300405
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300406 pr_debug("%s: core=%d addr=%04x data_len=%d checksum=%04x\n",
407 __func__, hdr_core, hdr_addr, hdr_data_len,
408 hdr_checksum);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300409
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300410 if (((hdr_core != 1) && (hdr_core != 2)) ||
411 (hdr_data_len > i)) {
412 pr_debug("%s: bad firmware\n", __func__);
413 break;
Antti Palosaari7f882c22012-03-30 09:10:08 -0300414 }
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300415
416 /* download begin packet */
417 req.cmd = CMD_FW_DL_BEGIN;
418 ret = af9035_ctrl_msg(udev, &req);
Antti Palosaari41d44a82012-04-01 11:06:23 -0300419 if (ret < 0)
420 goto err;
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300421
422 /* download firmware packet(s) */
423 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
424 len = j;
425 if (len > MAX_DATA)
426 len = MAX_DATA;
427 req_fw_dl.wlen = len;
428 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
429 HDR_SIZE + hdr_data_len - j];
430 ret = af9035_ctrl_msg(udev, &req_fw_dl);
431 if (ret < 0)
432 goto err;
433 }
434
435 /* download end packet */
436 req.cmd = CMD_FW_DL_END;
437 ret = af9035_ctrl_msg(udev, &req);
438 if (ret < 0)
439 goto err;
440
441 i -= hdr_data_len + HDR_SIZE;
442
443 pr_debug("%s: data uploaded=%lu\n", __func__, fw->size - i);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300444 }
445
446 /* firmware loaded, request boot */
447 req.cmd = CMD_FW_BOOT;
448 ret = af9035_ctrl_msg(udev, &req);
449 if (ret < 0)
450 goto err;
451
452 /* ensure firmware starts */
453 wbuf[0] = 1;
454 ret = af9035_ctrl_msg(udev, &req_fw_ver);
455 if (ret < 0)
456 goto err;
457
Antti Palosaari7f882c22012-03-30 09:10:08 -0300458 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300459 info("firmware did not run");
Antti Palosaari7f882c22012-03-30 09:10:08 -0300460 ret = -ENODEV;
461 goto err;
462 }
463
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300464 info("firmware version=%d.%d.%d.%d", rbuf[0], rbuf[1], rbuf[2],
465 rbuf[3]);
466
Antti Palosaari7f882c22012-03-30 09:10:08 -0300467 return 0;
468
469err:
470 pr_debug("%s: failed=%d\n", __func__, ret);
471
472 return ret;
473}
474
475/* abuse that callback as there is no better one for reading eeprom */
476static int af9035_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
477{
478 int ret, i, eeprom_shift = 0;
479 u8 tmp;
480 u16 tmp16;
481
482 /* check if there is dual tuners */
483 ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp);
484 if (ret < 0)
485 goto err;
486
487 af9035_config.dual_mode = tmp;
488 pr_debug("%s: dual mode=%d\n", __func__, af9035_config.dual_mode);
489
490 for (i = 0; i < af9035_properties[0].num_adapters; i++) {
491 /* tuner */
492 ret = af9035_rd_reg(d, EEPROM_1_TUNER_ID + eeprom_shift, &tmp);
493 if (ret < 0)
494 goto err;
495
496 af9035_af9033_config[i].tuner = tmp;
497 pr_debug("%s: [%d]tuner=%02x\n", __func__, i, tmp);
498
499 switch (tmp) {
500 case AF9033_TUNER_TUA9001:
501 af9035_af9033_config[i].spec_inv = 1;
502 break;
503 default:
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300504 af9035_config.hw_not_supported = true;
505 warn("tuner ID=%02x not supported, please report!",
Antti Palosaari7f882c22012-03-30 09:10:08 -0300506 tmp);
Antti Palosaari7f882c22012-03-30 09:10:08 -0300507 };
508
509 /* tuner IF frequency */
510 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp);
511 if (ret < 0)
512 goto err;
513
514 tmp16 = tmp;
515
516 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp);
517 if (ret < 0)
518 goto err;
519
520 tmp16 |= tmp << 8;
521
522 pr_debug("%s: [%d]IF=%d\n", __func__, i, tmp16);
523
524 eeprom_shift = 0x10; /* shift for the 2nd tuner params */
525 }
526
527 /* get demod clock */
528 ret = af9035_rd_reg(d, 0x00d800, &tmp);
529 if (ret < 0)
530 goto err;
531
532 tmp = (tmp >> 0) & 0x0f;
533
534 for (i = 0; i < af9035_properties[0].num_adapters; i++)
535 af9035_af9033_config[i].clock = clock_lut[tmp];
536
537 return 0;
538
539err:
540 pr_debug("%s: failed=%d\n", __func__, ret);
541
542 return ret;
543}
544
545static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
546{
547 int ret;
548
Antti Palosaari5a9abae2012-03-30 17:15:16 -0300549 if (af9035_config.hw_not_supported) {
550 ret = -ENODEV;
551 goto err;
552 }
553
Antti Palosaari7f882c22012-03-30 09:10:08 -0300554 if (adap->id == 0) {
555 ret = af9035_wr_reg(adap->dev, 0x00417f,
556 af9035_af9033_config[1].i2c_addr);
557 if (ret < 0)
558 goto err;
559
560 ret = af9035_wr_reg(adap->dev, 0x00d81a,
561 af9035_config.dual_mode);
562 if (ret < 0)
563 goto err;
564 }
565
566 /* attach demodulator */
567 adap->fe_adap[0].fe = dvb_attach(af9033_attach,
568 &af9035_af9033_config[adap->id], &adap->dev->i2c_adap);
569 if (adap->fe_adap[0].fe == NULL) {
570 ret = -ENODEV;
571 goto err;
572 }
573
574 return 0;
575
576err:
577 pr_debug("%s: failed=%d\n", __func__, ret);
578
579 return ret;
580}
581
582static struct tua9001_config af9035_tua9001_config = {
583 .i2c_addr = 0x60,
584};
585
586static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
587{
588 int ret;
589 struct dvb_frontend *fe;
590
591 switch (af9035_af9033_config[adap->id].tuner) {
592 case AF9033_TUNER_TUA9001:
593 /* AF9035 gpiot3 = TUA9001 RESETN
594 AF9035 gpiot2 = TUA9001 RXEN */
595
596 /* configure gpiot2 and gpiot2 as output */
597 ret = af9035_wr_reg_mask(adap->dev, 0x00d8ec, 0x01, 0x01);
598 if (ret < 0)
599 goto err;
600
601 ret = af9035_wr_reg_mask(adap->dev, 0x00d8ed, 0x01, 0x01);
602 if (ret < 0)
603 goto err;
604
605 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e8, 0x01, 0x01);
606 if (ret < 0)
607 goto err;
608
609 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e9, 0x01, 0x01);
610 if (ret < 0)
611 goto err;
612
613 /* reset tuner */
614 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e7, 0x00, 0x01);
615 if (ret < 0)
616 goto err;
617
618 usleep_range(2000, 20000);
619
620 ret = af9035_wr_reg_mask(adap->dev, 0x00d8e7, 0x01, 0x01);
621 if (ret < 0)
622 goto err;
623
624 /* activate tuner RX */
625 /* TODO: use callback for TUA9001 RXEN */
626 ret = af9035_wr_reg_mask(adap->dev, 0x00d8eb, 0x01, 0x01);
627 if (ret < 0)
628 goto err;
629
630 /* attach tuner */
631 fe = dvb_attach(tua9001_attach, adap->fe_adap[0].fe,
632 &adap->dev->i2c_adap, &af9035_tua9001_config);
633 break;
634 default:
635 fe = NULL;
636 }
637
638 if (fe == NULL) {
639 ret = -ENODEV;
640 goto err;
641 }
642
643 return 0;
644
645err:
646 pr_debug("%s: failed=%d\n", __func__, ret);
647
648 return ret;
649}
650
651enum af9035_id_entry {
652 AF9035_0CCD_0093,
653};
654
655static struct usb_device_id af9035_id[] = {
656 [AF9035_0CCD_0093] = {
657 USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK)},
658 {},
659};
660
661MODULE_DEVICE_TABLE(usb, af9035_id);
662
663static struct dvb_usb_device_properties af9035_properties[] = {
664 {
665 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
666
667 .usb_ctrl = DEVICE_SPECIFIC,
668 .download_firmware = af9035_download_firmware,
Antti Palosaari77c5ff22012-04-01 01:32:23 -0300669 .firmware = "dvb-usb-af9035-02.fw",
Antti Palosaari7f882c22012-03-30 09:10:08 -0300670 .no_reconnect = 1,
671
672 .num_adapters = 1,
673 .adapter = {
674 {
675 .num_frontends = 1,
676 .fe = {
677 {
678 .frontend_attach = af9035_frontend_attach,
679 .tuner_attach = af9035_tuner_attach,
680 .stream = {
681 .type = USB_BULK,
682 .count = 6,
683 .endpoint = 0x84,
684 .u = {
685 .bulk = {
686 .buffersize = (87 * 188),
687 }
688 }
689 }
690 }
691 }
692 }
693 },
694
695 .identify_state = af9035_identify_state,
696 .read_mac_address = af9035_read_mac_address,
697
698 .i2c_algo = &af9035_i2c_algo,
699
700 .num_device_descs = 1,
701 .devices = {
702 {
703 .name = "TerraTec Cinergy T Stick",
704 .cold_ids = {
705 &af9035_id[AF9035_0CCD_0093],
706 },
707 },
708 }
709 },
710};
711
712static int af9035_usb_probe(struct usb_interface *intf,
713 const struct usb_device_id *id)
714{
715 int ret, i;
716 struct dvb_usb_device *d = NULL;
717 struct usb_device *udev;
718 bool found;
719
720 pr_debug("%s: interface=%d\n", __func__,
721 intf->cur_altsetting->desc.bInterfaceNumber);
722
723 /* interface 0 is used by DVB-T receiver and
724 interface 1 is for remote controller (HID) */
725 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
726 return 0;
727
728 /* Dynamic USB ID support. Replaces first device ID with current one. */
729 udev = interface_to_usbdev(intf);
730
731 for (i = 0, found = false; i < ARRAY_SIZE(af9035_id) - 1; i++) {
732 if (af9035_id[i].idVendor ==
733 le16_to_cpu(udev->descriptor.idVendor) &&
734 af9035_id[i].idProduct ==
735 le16_to_cpu(udev->descriptor.idProduct)) {
736 found = true;
737 break;
738 }
739 }
740
741 if (!found) {
742 pr_debug("%s: using dynamic ID %04x:%04x\n", __func__,
743 le16_to_cpu(udev->descriptor.idVendor),
744 le16_to_cpu(udev->descriptor.idProduct));
745 af9035_properties[0].devices[0].cold_ids[0]->idVendor =
746 le16_to_cpu(udev->descriptor.idVendor);
747 af9035_properties[0].devices[0].cold_ids[0]->idProduct =
748 le16_to_cpu(udev->descriptor.idProduct);
749 }
750
751
752 for (i = 0; i < af9035_properties_count; i++) {
753 ret = dvb_usb_device_init(intf, &af9035_properties[i],
754 THIS_MODULE, &d, adapter_nr);
755
756 if (ret == -ENODEV)
757 continue;
758 else
759 break;
760 }
761
762 if (ret < 0)
763 goto err;
764
765 if (d) {
766 ret = af9035_init(d);
767 if (ret < 0)
768 goto err;
769 }
770
771 return 0;
772
773err:
774 pr_debug("%s: failed=%d\n", __func__, ret);
775
776 return ret;
777}
778
779/* usb specific object needed to register this driver with the usb subsystem */
780static struct usb_driver af9035_usb_driver = {
781 .name = "dvb_usb_af9035",
782 .probe = af9035_usb_probe,
783 .disconnect = dvb_usb_device_exit,
784 .id_table = af9035_id,
785};
786
787/* module stuff */
788static int __init af9035_usb_module_init(void)
789{
790 int ret;
791
792 ret = usb_register(&af9035_usb_driver);
793 if (ret < 0)
794 goto err;
795
796 return 0;
797
798err:
799 pr_debug("%s: failed=%d\n", __func__, ret);
800
801 return ret;
802}
803
804static void __exit af9035_usb_module_exit(void)
805{
806 /* deregister this driver from the USB subsystem */
807 usb_deregister(&af9035_usb_driver);
808}
809
810module_init(af9035_usb_module_init);
811module_exit(af9035_usb_module_exit);
812
813MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
814MODULE_DESCRIPTION("Afatech AF9035 driver");
815MODULE_LICENSE("GPL");