blob: 9a799d2e8792bf0bf3a2c7f5e83a6c6ada6eaa23 [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
Jiri Slaby6c614042010-01-22 12:10:52 -030024#include <linux/hash.h>
25
Antti Palosaari80619de2008-09-15 17:18:09 -030026#include "af9015.h"
27#include "af9013.h"
28#include "mt2060.h"
29#include "qt1010.h"
30#include "tda18271.h"
31#include "mxl5005s.h"
Jochen Friedrichd5633992009-02-02 14:59:50 -030032#include "mc44s803.h"
Antti Palosaari80619de2008-09-15 17:18:09 -030033
Antti Palosaari349d0422008-11-05 16:31:24 -030034static int dvb_usb_af9015_debug;
Antti Palosaari80619de2008-09-15 17:18:09 -030035module_param_named(debug, dvb_usb_af9015_debug, int, 0644);
36MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
Antti Palosaari349d0422008-11-05 16:31:24 -030037static int dvb_usb_af9015_remote;
Antti Palosaari80619de2008-09-15 17:18:09 -030038module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
39MODULE_PARM_DESC(remote, "select remote");
Antti Palosaari80619de2008-09-15 17:18:09 -030040DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
41
42static DEFINE_MUTEX(af9015_usb_mutex);
43
44static struct af9015_config af9015_config;
Antti Palosaari85d7d7c2009-04-09 09:16:12 -030045static struct dvb_usb_device_properties af9015_properties[3];
Antti Palosaari349d0422008-11-05 16:31:24 -030046static int af9015_properties_count = ARRAY_SIZE(af9015_properties);
Antti Palosaari80619de2008-09-15 17:18:09 -030047
48static struct af9013_config af9015_af9013_config[] = {
49 {
50 .demod_address = AF9015_I2C_DEMOD,
51 .output_mode = AF9013_OUTPUT_MODE_USB,
52 .api_version = { 0, 1, 9, 0 },
53 .gpio[0] = AF9013_GPIO_HI,
Antti Palosaari80619de2008-09-15 17:18:09 -030054 .gpio[3] = AF9013_GPIO_TUNER_ON,
55
56 }, {
57 .output_mode = AF9013_OUTPUT_MODE_SERIAL,
58 .api_version = { 0, 1, 9, 0 },
59 .gpio[0] = AF9013_GPIO_TUNER_ON,
60 .gpio[1] = AF9013_GPIO_LO,
61 }
62};
63
64static int af9015_rw_udev(struct usb_device *udev, struct req_t *req)
65{
Antti Palosaari06565d72009-09-12 20:46:30 -030066#define BUF_LEN 63
67#define REQ_HDR_LEN 8 /* send header size */
68#define ACK_HDR_LEN 2 /* rece header size */
Antti Palosaari80619de2008-09-15 17:18:09 -030069 int act_len, ret;
Antti Palosaari06565d72009-09-12 20:46:30 -030070 u8 buf[BUF_LEN];
Antti Palosaari80619de2008-09-15 17:18:09 -030071 u8 write = 1;
Antti Palosaari06565d72009-09-12 20:46:30 -030072 u8 msg_len = REQ_HDR_LEN;
Antti Palosaari80619de2008-09-15 17:18:09 -030073 static u8 seq; /* packet sequence number */
74
75 if (mutex_lock_interruptible(&af9015_usb_mutex) < 0)
76 return -EAGAIN;
77
78 buf[0] = req->cmd;
79 buf[1] = seq++;
80 buf[2] = req->i2c_addr;
81 buf[3] = req->addr >> 8;
82 buf[4] = req->addr & 0xff;
83 buf[5] = req->mbox;
84 buf[6] = req->addr_len;
85 buf[7] = req->data_len;
86
87 switch (req->cmd) {
88 case GET_CONFIG:
Antti Palosaari80619de2008-09-15 17:18:09 -030089 case READ_MEMORY:
90 case RECONNECT_USB:
91 case GET_IR_CODE:
92 write = 0;
93 break;
94 case READ_I2C:
95 write = 0;
96 buf[2] |= 0x01; /* set I2C direction */
97 case WRITE_I2C:
98 buf[0] = READ_WRITE_I2C;
99 break;
100 case WRITE_MEMORY:
101 if (((req->addr & 0xff00) == 0xff00) ||
Antti Palosaarif4e96de2009-09-12 21:25:59 -0300102 ((req->addr & 0xff00) == 0xae00))
Antti Palosaari80619de2008-09-15 17:18:09 -0300103 buf[0] = WRITE_VIRTUAL_MEMORY;
104 case WRITE_VIRTUAL_MEMORY:
105 case COPY_FIRMWARE:
106 case DOWNLOAD_FIRMWARE:
Nils Kassubeba1bc642009-07-28 11:54:52 -0300107 case BOOT:
Antti Palosaari80619de2008-09-15 17:18:09 -0300108 break;
109 default:
110 err("unknown command:%d", req->cmd);
111 ret = -1;
112 goto error_unlock;
113 }
114
Antti Palosaari06565d72009-09-12 20:46:30 -0300115 /* buffer overflow check */
116 if ((write && (req->data_len > BUF_LEN - REQ_HDR_LEN)) ||
117 (!write && (req->data_len > BUF_LEN - ACK_HDR_LEN))) {
118 err("too much data; cmd:%d len:%d", req->cmd, req->data_len);
119 ret = -EINVAL;
120 goto error_unlock;
121 }
122
Antti Palosaari80619de2008-09-15 17:18:09 -0300123 /* write requested */
124 if (write) {
Antti Palosaari06565d72009-09-12 20:46:30 -0300125 memcpy(&buf[REQ_HDR_LEN], req->data, req->data_len);
Antti Palosaari80619de2008-09-15 17:18:09 -0300126 msg_len += req->data_len;
127 }
Antti Palosaari06565d72009-09-12 20:46:30 -0300128
Antti Palosaari80619de2008-09-15 17:18:09 -0300129 deb_xfer(">>> ");
130 debug_dump(buf, msg_len, deb_xfer);
131
132 /* send req */
133 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
Antti Palosaari06565d72009-09-12 20:46:30 -0300134 &act_len, AF9015_USB_TIMEOUT);
Antti Palosaari80619de2008-09-15 17:18:09 -0300135 if (ret)
136 err("bulk message failed:%d (%d/%d)", ret, msg_len, act_len);
137 else
138 if (act_len != msg_len)
139 ret = -1; /* all data is not send */
140 if (ret)
141 goto error_unlock;
142
143 /* no ack for those packets */
144 if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
145 goto exit_unlock;
146
Antti Palosaari06565d72009-09-12 20:46:30 -0300147 /* write receives seq + status = 2 bytes
148 read receives seq + status + data = 2 + N bytes */
149 msg_len = ACK_HDR_LEN;
150 if (!write)
151 msg_len += req->data_len;
152
Antti Palosaari80619de2008-09-15 17:18:09 -0300153 ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
Antti Palosaari06565d72009-09-12 20:46:30 -0300154 &act_len, AF9015_USB_TIMEOUT);
Antti Palosaari80619de2008-09-15 17:18:09 -0300155 if (ret) {
156 err("recv bulk message failed:%d", ret);
157 ret = -1;
158 goto error_unlock;
159 }
160
161 deb_xfer("<<< ");
162 debug_dump(buf, act_len, deb_xfer);
163
164 /* remote controller query status is 1 if remote code is not received */
165 if (req->cmd == GET_IR_CODE && buf[1] == 1) {
166 buf[1] = 0; /* clear command "error" status */
167 memset(&buf[2], 0, req->data_len);
168 buf[3] = 1; /* no remote code received mark */
169 }
170
171 /* check status */
172 if (buf[1]) {
173 err("command failed:%d", buf[1]);
174 ret = -1;
175 goto error_unlock;
176 }
177
178 /* read request, copy returned data to return buf */
179 if (!write)
Antti Palosaari06565d72009-09-12 20:46:30 -0300180 memcpy(req->data, &buf[ACK_HDR_LEN], req->data_len);
Antti Palosaari80619de2008-09-15 17:18:09 -0300181
182error_unlock:
183exit_unlock:
184 mutex_unlock(&af9015_usb_mutex);
185
186 return ret;
187}
188
189static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
190{
191 return af9015_rw_udev(d->udev, req);
192}
193
194static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
195 u8 len)
196{
197 struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
198 val};
199 return af9015_ctrl_msg(d, &req);
200}
201
202static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
203{
204 return af9015_write_regs(d, addr, &val, 1);
205}
206
207static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
208{
209 struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, 1, val};
210 return af9015_ctrl_msg(d, &req);
211}
212
213static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
214 u8 val)
215{
216 struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
217
218 if (addr == af9015_af9013_config[0].demod_address ||
219 addr == af9015_af9013_config[1].demod_address)
220 req.addr_len = 3;
221
222 return af9015_ctrl_msg(d, &req);
223}
224
225static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
226 u8 *val)
227{
228 struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
229
230 if (addr == af9015_af9013_config[0].demod_address ||
231 addr == af9015_af9013_config[1].demod_address)
232 req.addr_len = 3;
233
234 return af9015_ctrl_msg(d, &req);
235}
236
237static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
238 int num)
239{
240 struct dvb_usb_device *d = i2c_get_adapdata(adap);
241 int ret = 0, i = 0;
242 u16 addr;
243 u8 mbox, addr_len;
244 struct req_t req;
245
246/* TODO: implement bus lock
247
248The bus lock is needed because there is two tuners both using same I2C-address.
249Due to that the only way to select correct tuner is use demodulator I2C-gate.
250
251................................................
252. AF9015 includes integrated AF9013 demodulator.
253. ____________ ____________ . ____________
254.| uC | | demod | . | tuner |
255.|------------| |------------| . |------------|
256.| AF9015 | | AF9013/5 | . | MXL5003 |
257.| |--+----I2C-------|-----/ -----|-.-----I2C-------| |
258.| | | | addr 0x38 | . | addr 0xc6 |
259.|____________| | |____________| . |____________|
260.................|..............................
261 | ____________ ____________
262 | | demod | | tuner |
263 | |------------| |------------|
264 | | AF9013 | | MXL5003 |
265 +----I2C-------|-----/ -----|-------I2C-------| |
266 | addr 0x3a | | addr 0xc6 |
267 |____________| |____________|
268*/
269 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
270 return -EAGAIN;
271
272 while (i < num) {
273 if (msg[i].addr == af9015_af9013_config[0].demod_address ||
274 msg[i].addr == af9015_af9013_config[1].demod_address) {
275 addr = msg[i].buf[0] << 8;
276 addr += msg[i].buf[1];
277 mbox = msg[i].buf[2];
278 addr_len = 3;
279 } else {
280 addr = msg[i].buf[0];
281 addr_len = 1;
282 mbox = 0;
283 }
284
285 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
286 if (msg[i].addr ==
287 af9015_af9013_config[0].demod_address)
288 req.cmd = READ_MEMORY;
289 else
290 req.cmd = READ_I2C;
291 req.i2c_addr = msg[i].addr;
292 req.addr = addr;
293 req.mbox = mbox;
294 req.addr_len = addr_len;
295 req.data_len = msg[i+1].len;
296 req.data = &msg[i+1].buf[0];
297 ret = af9015_ctrl_msg(d, &req);
298 i += 2;
Jochen Friedrichd5633992009-02-02 14:59:50 -0300299 } else if (msg[i].flags & I2C_M_RD) {
300 ret = -EINVAL;
301 if (msg[i].addr ==
302 af9015_af9013_config[0].demod_address)
303 goto error;
304 else
305 req.cmd = READ_I2C;
306 req.i2c_addr = msg[i].addr;
307 req.addr = addr;
308 req.mbox = mbox;
309 req.addr_len = addr_len;
310 req.data_len = msg[i].len;
311 req.data = &msg[i].buf[0];
312 ret = af9015_ctrl_msg(d, &req);
313 i += 1;
Antti Palosaari80619de2008-09-15 17:18:09 -0300314 } else {
315 if (msg[i].addr ==
316 af9015_af9013_config[0].demod_address)
317 req.cmd = WRITE_MEMORY;
318 else
319 req.cmd = WRITE_I2C;
320 req.i2c_addr = msg[i].addr;
321 req.addr = addr;
322 req.mbox = mbox;
323 req.addr_len = addr_len;
324 req.data_len = msg[i].len-addr_len;
325 req.data = &msg[i].buf[addr_len];
326 ret = af9015_ctrl_msg(d, &req);
327 i += 1;
328 }
329 if (ret)
330 goto error;
331
332 }
333 ret = i;
334
335error:
336 mutex_unlock(&d->i2c_mutex);
337
338 return ret;
339}
340
341static u32 af9015_i2c_func(struct i2c_adapter *adapter)
342{
343 return I2C_FUNC_I2C;
344}
345
346static struct i2c_algorithm af9015_i2c_algo = {
347 .master_xfer = af9015_i2c_xfer,
348 .functionality = af9015_i2c_func,
349};
350
351static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
352{
353 int ret;
354 u8 val, mask = 0x01;
355
356 ret = af9015_read_reg(d, addr, &val);
357 if (ret)
358 return ret;
359
360 mask <<= bit;
361 if (op) {
362 /* set bit */
363 val |= mask;
364 } else {
365 /* clear bit */
366 mask ^= 0xff;
367 val &= mask;
368 }
369
370 return af9015_write_reg(d, addr, val);
371}
372
373static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
374{
375 return af9015_do_reg_bit(d, addr, bit, 1);
376}
377
378static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
379{
380 return af9015_do_reg_bit(d, addr, bit, 0);
381}
382
383static int af9015_init_endpoint(struct dvb_usb_device *d)
384{
385 int ret;
386 u16 frame_size;
387 u8 packet_size;
388 deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);
389
Antti Palosaari9c863272009-09-12 13:35:29 -0300390 /* Windows driver uses packet count 21 for USB1.1 and 348 for USB2.0.
391 We use smaller - about 1/4 from the original, 5 and 87. */
Antti Palosaari80619de2008-09-15 17:18:09 -0300392#define TS_PACKET_SIZE 188
393
Antti Palosaari9c863272009-09-12 13:35:29 -0300394#define TS_USB20_PACKET_COUNT 87
Antti Palosaari80619de2008-09-15 17:18:09 -0300395#define TS_USB20_FRAME_SIZE (TS_PACKET_SIZE*TS_USB20_PACKET_COUNT)
396
Antti Palosaari9c863272009-09-12 13:35:29 -0300397#define TS_USB11_PACKET_COUNT 5
Antti Palosaari80619de2008-09-15 17:18:09 -0300398#define TS_USB11_FRAME_SIZE (TS_PACKET_SIZE*TS_USB11_PACKET_COUNT)
399
400#define TS_USB20_MAX_PACKET_SIZE 512
401#define TS_USB11_MAX_PACKET_SIZE 64
402
403 if (d->udev->speed == USB_SPEED_FULL) {
404 frame_size = TS_USB11_FRAME_SIZE/4;
405 packet_size = TS_USB11_MAX_PACKET_SIZE/4;
406 } else {
407 frame_size = TS_USB20_FRAME_SIZE/4;
408 packet_size = TS_USB20_MAX_PACKET_SIZE/4;
409 }
410
411 ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
412 if (ret)
413 goto error;
414 ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
415 if (ret)
416 goto error;
417 ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
418 if (ret)
419 goto error;
420 ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
421 if (ret)
422 goto error;
423 ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
424 if (ret)
425 goto error;
426 if (af9015_config.dual_mode) {
427 ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
428 if (ret)
429 goto error;
430 }
431 ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
432 if (ret)
433 goto error;
434 if (af9015_config.dual_mode) {
435 ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
436 if (ret)
437 goto error;
438 }
439 /* EP4 xfer length */
440 ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
441 if (ret)
442 goto error;
443 ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
444 if (ret)
445 goto error;
446 /* EP5 xfer length */
447 ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
448 if (ret)
449 goto error;
450 ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
451 if (ret)
452 goto error;
453 ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
454 if (ret)
455 goto error;
456 ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
457 if (ret)
458 goto error;
459 ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
460 if (ret)
461 goto error;
462 if (af9015_config.dual_mode) {
463 ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
464 if (ret)
465 goto error;
466 }
467
468 /* enable / disable mp2if2 */
469 if (af9015_config.dual_mode)
470 ret = af9015_set_reg_bit(d, 0xd50b, 0);
471 else
472 ret = af9015_clear_reg_bit(d, 0xd50b, 0);
473error:
474 if (ret)
475 err("endpoint init failed:%d", ret);
476 return ret;
477}
478
479static int af9015_copy_firmware(struct dvb_usb_device *d)
480{
481 int ret;
482 u8 fw_params[4];
483 u8 val, i;
484 struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
485 fw_params };
486 deb_info("%s:\n", __func__);
487
488 fw_params[0] = af9015_config.firmware_size >> 8;
489 fw_params[1] = af9015_config.firmware_size & 0xff;
490 fw_params[2] = af9015_config.firmware_checksum >> 8;
491 fw_params[3] = af9015_config.firmware_checksum & 0xff;
492
493 /* wait 2nd demodulator ready */
494 msleep(100);
495
496 ret = af9015_read_reg_i2c(d, 0x3a, 0x98be, &val);
497 if (ret)
498 goto error;
499 else
500 deb_info("%s: firmware status:%02x\n", __func__, val);
501
502 if (val == 0x0c) /* fw is running, no need for download */
503 goto exit;
504
505 /* set I2C master clock to fast (to speed up firmware copy) */
506 ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
507 if (ret)
508 goto error;
509
510 msleep(50);
511
512 /* copy firmware */
513 ret = af9015_ctrl_msg(d, &req);
514 if (ret)
515 err("firmware copy cmd failed:%d", ret);
516 deb_info("%s: firmware copy done\n", __func__);
517
518 /* set I2C master clock back to normal */
519 ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
520 if (ret)
521 goto error;
522
523 /* request boot firmware */
524 ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].demod_address,
525 0xe205, 1);
526 deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
527 if (ret)
528 goto error;
529
530 for (i = 0; i < 15; i++) {
531 msleep(100);
532
533 /* check firmware status */
534 ret = af9015_read_reg_i2c(d,
535 af9015_af9013_config[1].demod_address, 0x98be, &val);
536 deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
537 __func__, ret, val);
538 if (ret)
539 goto error;
540
541 if (val == 0x0c || val == 0x04) /* success or fail */
542 break;
543 }
544
545 if (val == 0x04) {
546 err("firmware did not run");
547 ret = -1;
548 } else if (val != 0x0c) {
549 err("firmware boot timeout");
550 ret = -1;
551 }
552
553error:
554exit:
555 return ret;
556}
557
Jiri Slaby6c614042010-01-22 12:10:52 -0300558/* hash (and dump) eeprom */
559static int af9015_eeprom_hash(struct usb_device *udev)
Antti Palosaari80619de2008-09-15 17:18:09 -0300560{
Jiri Slaby6c614042010-01-22 12:10:52 -0300561 static const unsigned int eeprom_size = 256;
562 unsigned int reg;
563 int ret;
564 u8 val, *eeprom;
565 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
Antti Palosaari80619de2008-09-15 17:18:09 -0300566
Jiri Slaby6c614042010-01-22 12:10:52 -0300567 eeprom = kmalloc(eeprom_size, GFP_KERNEL);
568 if (eeprom == NULL)
569 return -ENOMEM;
570
571 for (reg = 0; reg < eeprom_size; reg++) {
572 req.addr = reg;
573 ret = af9015_rw_udev(udev, &req);
574 if (ret)
575 goto free;
576 eeprom[reg] = val;
Antti Palosaari80619de2008-09-15 17:18:09 -0300577 }
Jiri Slaby6c614042010-01-22 12:10:52 -0300578
579 if (dvb_usb_af9015_debug & 0x01)
580 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, eeprom,
581 eeprom_size);
582
583 BUG_ON(eeprom_size % 4);
584
585 af9015_config.eeprom_sum = 0;
586 for (reg = 0; reg < eeprom_size / sizeof(u32); reg++) {
587 af9015_config.eeprom_sum *= GOLDEN_RATIO_PRIME_32;
588 af9015_config.eeprom_sum += le32_to_cpu(((u32 *)eeprom)[reg]);
589 }
590
591 deb_info("%s: eeprom sum=%.8x\n", __func__, af9015_config.eeprom_sum);
592
593 ret = 0;
594free:
595 kfree(eeprom);
596 return ret;
Antti Palosaari80619de2008-09-15 17:18:09 -0300597}
598
Antti Palosaari349d0422008-11-05 16:31:24 -0300599static int af9015_download_ir_table(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -0300600{
601 int i, packets = 0, ret;
602 u16 addr = 0x9a56; /* ir-table start address */
603 struct req_t req = {WRITE_MEMORY, 0, 0, 0, 0, 1, NULL};
604 u8 *data = NULL;
605 deb_info("%s:\n", __func__);
606
607 data = af9015_config.ir_table;
608 packets = af9015_config.ir_table_size;
609
610 /* no remote */
611 if (!packets)
612 goto exit;
613
614 /* load remote ir-table */
615 for (i = 0; i < packets; i++) {
616 req.addr = addr + i;
617 req.data = &data[i];
618 ret = af9015_ctrl_msg(d, &req);
619 if (ret) {
620 err("ir-table download failed at packet %d with " \
621 "code %d", i, ret);
622 return ret;
623 }
624 }
625
626exit:
627 return 0;
628}
629
630static int af9015_init(struct dvb_usb_device *d)
631{
632 int ret;
633 deb_info("%s:\n", __func__);
634
635 ret = af9015_init_endpoint(d);
636 if (ret)
637 goto error;
638
639 ret = af9015_download_ir_table(d);
640 if (ret)
641 goto error;
642
643error:
644 return ret;
645}
646
647static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
648{
649 int ret;
650 deb_info("%s: onoff:%d\n", __func__, onoff);
651
652 if (onoff)
653 ret = af9015_set_reg_bit(adap->dev, 0xd503, 0);
654 else
655 ret = af9015_clear_reg_bit(adap->dev, 0xd503, 0);
656
657 return ret;
658}
659
660static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
661 int onoff)
662{
663 int ret;
664 u8 idx;
665
666 deb_info("%s: set pid filter, index %d, pid %x, onoff %d\n",
667 __func__, index, pid, onoff);
668
669 ret = af9015_write_reg(adap->dev, 0xd505, (pid & 0xff));
670 if (ret)
671 goto error;
672
673 ret = af9015_write_reg(adap->dev, 0xd506, (pid >> 8));
674 if (ret)
675 goto error;
676
677 idx = ((index & 0x1f) | (1 << 5));
678 ret = af9015_write_reg(adap->dev, 0xd504, idx);
679
680error:
681 return ret;
682}
683
684static int af9015_download_firmware(struct usb_device *udev,
685 const struct firmware *fw)
686{
687 int i, len, packets, remainder, ret;
688 struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
689 u16 addr = 0x5100; /* firmware start address */
690 u16 checksum = 0;
691
692 deb_info("%s:\n", __func__);
693
694 /* calc checksum */
695 for (i = 0; i < fw->size; i++)
696 checksum += fw->data[i];
697
698 af9015_config.firmware_size = fw->size;
699 af9015_config.firmware_checksum = checksum;
700
701 #define FW_PACKET_MAX_DATA 55
702
703 packets = fw->size / FW_PACKET_MAX_DATA;
704 remainder = fw->size % FW_PACKET_MAX_DATA;
705 len = FW_PACKET_MAX_DATA;
706 for (i = 0; i <= packets; i++) {
707 if (i == packets) /* set size of the last packet */
708 len = remainder;
709
710 req.data_len = len;
Antti Palosaari541dfa82008-10-06 13:57:45 -0300711 req.data = (u8 *)(fw->data + i * FW_PACKET_MAX_DATA);
Antti Palosaari80619de2008-09-15 17:18:09 -0300712 req.addr = addr;
713 addr += FW_PACKET_MAX_DATA;
714
715 ret = af9015_rw_udev(udev, &req);
716 if (ret) {
717 err("firmware download failed at packet %d with " \
718 "code %d", i, ret);
719 goto error;
720 }
721 }
Antti Palosaari80619de2008-09-15 17:18:09 -0300722
723 /* firmware loaded, request boot */
724 req.cmd = BOOT;
725 ret = af9015_rw_udev(udev, &req);
726 if (ret) {
727 err("firmware boot failed:%d", ret);
728 goto error;
729 }
730
Antti Palosaari80619de2008-09-15 17:18:09 -0300731error:
732 return ret;
733}
734
Jiri Slaby634d2d72010-01-22 12:10:53 -0300735static void af9015_set_remote_config(struct usb_device *udev,
736 struct dvb_usb_device_properties *props)
737{
738 if (dvb_usb_af9015_remote) {
739 /* load remote defined as module param */
740 switch (dvb_usb_af9015_remote) {
741 case AF9015_REMOTE_A_LINK_DTU_M:
742 props->rc_key_map =
743 af9015_rc_keys_a_link;
744 props->rc_key_map_size =
745 ARRAY_SIZE(af9015_rc_keys_a_link);
746 af9015_config.ir_table = af9015_ir_table_a_link;
747 af9015_config.ir_table_size =
748 ARRAY_SIZE(af9015_ir_table_a_link);
749 break;
750 case AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3:
751 props->rc_key_map =
752 af9015_rc_keys_msi;
753 props->rc_key_map_size =
754 ARRAY_SIZE(af9015_rc_keys_msi);
755 af9015_config.ir_table = af9015_ir_table_msi;
756 af9015_config.ir_table_size =
757 ARRAY_SIZE(af9015_ir_table_msi);
758 break;
759 case AF9015_REMOTE_MYGICTV_U718:
760 props->rc_key_map =
761 af9015_rc_keys_mygictv;
762 props->rc_key_map_size =
763 ARRAY_SIZE(af9015_rc_keys_mygictv);
764 af9015_config.ir_table =
765 af9015_ir_table_mygictv;
766 af9015_config.ir_table_size =
767 ARRAY_SIZE(af9015_ir_table_mygictv);
768 break;
769 case AF9015_REMOTE_DIGITTRADE_DVB_T:
770 props->rc_key_map =
771 af9015_rc_keys_digittrade;
772 props->rc_key_map_size =
773 ARRAY_SIZE(af9015_rc_keys_digittrade);
774 af9015_config.ir_table =
775 af9015_ir_table_digittrade;
776 af9015_config.ir_table_size =
777 ARRAY_SIZE(af9015_ir_table_digittrade);
778 break;
779 case AF9015_REMOTE_AVERMEDIA_KS:
780 props->rc_key_map =
781 af9015_rc_keys_avermedia;
782 props->rc_key_map_size =
783 ARRAY_SIZE(af9015_rc_keys_avermedia);
784 af9015_config.ir_table =
785 af9015_ir_table_avermedia_ks;
786 af9015_config.ir_table_size =
787 ARRAY_SIZE(af9015_ir_table_avermedia_ks);
788 break;
789 }
790 } else {
791 switch (le16_to_cpu(udev->descriptor.idVendor)) {
792 case USB_VID_LEADTEK:
793 props->rc_key_map =
794 af9015_rc_keys_leadtek;
795 props->rc_key_map_size =
796 ARRAY_SIZE(af9015_rc_keys_leadtek);
797 af9015_config.ir_table =
798 af9015_ir_table_leadtek;
799 af9015_config.ir_table_size =
800 ARRAY_SIZE(af9015_ir_table_leadtek);
801 break;
802 case USB_VID_VISIONPLUS:
803 props->rc_key_map =
804 af9015_rc_keys_twinhan;
805 props->rc_key_map_size =
806 ARRAY_SIZE(af9015_rc_keys_twinhan);
807 af9015_config.ir_table =
808 af9015_ir_table_twinhan;
809 af9015_config.ir_table_size =
810 ARRAY_SIZE(af9015_ir_table_twinhan);
811 break;
812 case USB_VID_KWORLD_2:
813 /* TODO: use correct rc keys */
814 props->rc_key_map =
815 af9015_rc_keys_twinhan;
816 props->rc_key_map_size =
817 ARRAY_SIZE(af9015_rc_keys_twinhan);
818 af9015_config.ir_table = af9015_ir_table_kworld;
819 af9015_config.ir_table_size =
820 ARRAY_SIZE(af9015_ir_table_kworld);
821 break;
822 /* Check USB manufacturer and product strings and try
823 to determine correct remote in case of chip vendor
824 reference IDs are used. */
825 case USB_VID_AFATECH:
826 {
827 char manufacturer[10];
828 memset(manufacturer, 0, sizeof(manufacturer));
829 usb_string(udev, udev->descriptor.iManufacturer,
830 manufacturer, sizeof(manufacturer));
831 if (!strcmp("Geniatech", manufacturer)) {
832 /* iManufacturer 1 Geniatech
833 iProduct 2 AF9015 */
834 props->rc_key_map =
835 af9015_rc_keys_mygictv;
836 props->rc_key_map_size =
837 ARRAY_SIZE(af9015_rc_keys_mygictv);
838 af9015_config.ir_table =
839 af9015_ir_table_mygictv;
840 af9015_config.ir_table_size =
841 ARRAY_SIZE(af9015_ir_table_mygictv);
842 } else if (!strcmp("MSI", manufacturer)) {
843 /* iManufacturer 1 MSI
844 iProduct 2 MSI K-VOX */
845 props->rc_key_map =
846 af9015_rc_keys_msi;
847 props->rc_key_map_size =
848 ARRAY_SIZE(af9015_rc_keys_msi);
849 af9015_config.ir_table =
850 af9015_ir_table_msi;
851 af9015_config.ir_table_size =
852 ARRAY_SIZE(af9015_ir_table_msi);
853 } else if (udev->descriptor.idProduct ==
854 cpu_to_le16(USB_PID_TREKSTOR_DVBT)) {
855 props->rc_key_map =
856 af9015_rc_keys_trekstor;
857 props->rc_key_map_size =
858 ARRAY_SIZE(af9015_rc_keys_trekstor);
859 af9015_config.ir_table =
860 af9015_ir_table_trekstor;
861 af9015_config.ir_table_size =
862 ARRAY_SIZE(af9015_ir_table_trekstor);
863 }
864 break;
865 }
866 case USB_VID_AVERMEDIA:
867 props->rc_key_map =
868 af9015_rc_keys_avermedia;
869 props->rc_key_map_size =
870 ARRAY_SIZE(af9015_rc_keys_avermedia);
871 af9015_config.ir_table =
872 af9015_ir_table_avermedia;
873 af9015_config.ir_table_size =
874 ARRAY_SIZE(af9015_ir_table_avermedia);
875 break;
876 case USB_VID_MSI_2:
877 props->rc_key_map =
878 af9015_rc_keys_msi_digivox_iii;
879 props->rc_key_map_size =
880 ARRAY_SIZE(af9015_rc_keys_msi_digivox_iii);
881 af9015_config.ir_table =
882 af9015_ir_table_msi_digivox_iii;
883 af9015_config.ir_table_size =
884 ARRAY_SIZE(af9015_ir_table_msi_digivox_iii);
885 break;
886 }
887 }
888}
889
Antti Palosaari80619de2008-09-15 17:18:09 -0300890static int af9015_read_config(struct usb_device *udev)
891{
892 int ret;
893 u8 val, i, offset = 0;
894 struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
Antti Palosaari80619de2008-09-15 17:18:09 -0300895
896 /* IR remote controller */
897 req.addr = AF9015_EEPROM_IR_MODE;
Antti Palosaarid1a470f2009-01-20 14:56:20 -0300898 /* first message will timeout often due to possible hw bug */
899 for (i = 0; i < 4; i++) {
900 ret = af9015_rw_udev(udev, &req);
901 if (!ret)
902 break;
903 }
Antti Palosaari80619de2008-09-15 17:18:09 -0300904 if (ret)
905 goto error;
Jiri Slaby6c614042010-01-22 12:10:52 -0300906
907 ret = af9015_eeprom_hash(udev);
908 if (ret)
909 goto error;
910
Antti Palosaari80619de2008-09-15 17:18:09 -0300911 deb_info("%s: IR mode:%d\n", __func__, val);
912 for (i = 0; i < af9015_properties_count; i++) {
Antti Palosaari0f017212009-09-21 21:26:37 -0300913 if (val == AF9015_IR_MODE_DISABLED) {
Antti Palosaari80619de2008-09-15 17:18:09 -0300914 af9015_properties[i].rc_key_map = NULL;
915 af9015_properties[i].rc_key_map_size = 0;
Jiri Slaby634d2d72010-01-22 12:10:53 -0300916 } else
917 af9015_set_remote_config(udev, &af9015_properties[i]);
Antti Palosaari80619de2008-09-15 17:18:09 -0300918 }
919
920 /* TS mode - one or two receivers */
921 req.addr = AF9015_EEPROM_TS_MODE;
922 ret = af9015_rw_udev(udev, &req);
923 if (ret)
924 goto error;
925 af9015_config.dual_mode = val;
926 deb_info("%s: TS mode:%d\n", __func__, af9015_config.dual_mode);
Antti Palosaari80619de2008-09-15 17:18:09 -0300927
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300928 /* Set adapter0 buffer size according to USB port speed, adapter1 buffer
929 size can be static because it is enabled only USB2.0 */
Antti Palosaari80619de2008-09-15 17:18:09 -0300930 for (i = 0; i < af9015_properties_count; i++) {
931 /* USB1.1 set smaller buffersize and disable 2nd adapter */
932 if (udev->speed == USB_SPEED_FULL) {
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300933 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
Antti Palosaari9c863272009-09-12 13:35:29 -0300934 = TS_USB11_FRAME_SIZE;
Antti Palosaari80619de2008-09-15 17:18:09 -0300935 /* disable 2nd adapter because we don't have
936 PID-filters */
937 af9015_config.dual_mode = 0;
938 } else {
Antti Palosaarif0830eb2009-01-13 13:08:29 -0300939 af9015_properties[i].adapter[0].stream.u.bulk.buffersize
Jose Alberto Reguero353330c2009-09-12 09:51:36 -0300940 = TS_USB20_FRAME_SIZE;
Antti Palosaari80619de2008-09-15 17:18:09 -0300941 }
942 }
943
944 if (af9015_config.dual_mode) {
945 /* read 2nd demodulator I2C address */
946 req.addr = AF9015_EEPROM_DEMOD2_I2C;
947 ret = af9015_rw_udev(udev, &req);
948 if (ret)
949 goto error;
950 af9015_af9013_config[1].demod_address = val;
951
952 /* enable 2nd adapter */
953 for (i = 0; i < af9015_properties_count; i++)
954 af9015_properties[i].num_adapters = 2;
955
956 } else {
957 /* disable 2nd adapter */
958 for (i = 0; i < af9015_properties_count; i++)
959 af9015_properties[i].num_adapters = 1;
960 }
961
962 for (i = 0; i < af9015_properties[0].num_adapters; i++) {
963 if (i == 1)
964 offset = AF9015_EEPROM_OFFSET;
965 /* xtal */
966 req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
967 ret = af9015_rw_udev(udev, &req);
968 if (ret)
969 goto error;
970 switch (val) {
971 case 0:
972 af9015_af9013_config[i].adc_clock = 28800;
973 break;
974 case 1:
975 af9015_af9013_config[i].adc_clock = 20480;
976 break;
977 case 2:
978 af9015_af9013_config[i].adc_clock = 28000;
979 break;
980 case 3:
981 af9015_af9013_config[i].adc_clock = 25000;
982 break;
983 };
984 deb_info("%s: [%d] xtal:%d set adc_clock:%d\n", __func__, i,
985 val, af9015_af9013_config[i].adc_clock);
986
987 /* tuner IF */
988 req.addr = AF9015_EEPROM_IF1H + offset;
989 ret = af9015_rw_udev(udev, &req);
990 if (ret)
991 goto error;
992 af9015_af9013_config[i].tuner_if = val << 8;
993 req.addr = AF9015_EEPROM_IF1L + offset;
994 ret = af9015_rw_udev(udev, &req);
995 if (ret)
996 goto error;
997 af9015_af9013_config[i].tuner_if += val;
998 deb_info("%s: [%d] IF1:%d\n", __func__, i,
999 af9015_af9013_config[0].tuner_if);
1000
1001 /* MT2060 IF1 */
1002 req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
1003 ret = af9015_rw_udev(udev, &req);
1004 if (ret)
1005 goto error;
1006 af9015_config.mt2060_if1[i] = val << 8;
1007 req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
1008 ret = af9015_rw_udev(udev, &req);
1009 if (ret)
1010 goto error;
1011 af9015_config.mt2060_if1[i] += val;
1012 deb_info("%s: [%d] MT2060 IF1:%d\n", __func__, i,
1013 af9015_config.mt2060_if1[i]);
1014
1015 /* tuner */
1016 req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
1017 ret = af9015_rw_udev(udev, &req);
1018 if (ret)
1019 goto error;
1020 switch (val) {
1021 case AF9013_TUNER_ENV77H11D5:
1022 case AF9013_TUNER_MT2060:
Antti Palosaari80619de2008-09-15 17:18:09 -03001023 case AF9013_TUNER_QT1010:
1024 case AF9013_TUNER_UNKNOWN:
1025 case AF9013_TUNER_MT2060_2:
1026 case AF9013_TUNER_TDA18271:
1027 case AF9013_TUNER_QT1010A:
1028 af9015_af9013_config[i].rf_spec_inv = 1;
1029 break;
1030 case AF9013_TUNER_MXL5003D:
1031 case AF9013_TUNER_MXL5005D:
1032 case AF9013_TUNER_MXL5005R:
1033 af9015_af9013_config[i].rf_spec_inv = 0;
1034 break;
Jochen Friedrichd5633992009-02-02 14:59:50 -03001035 case AF9013_TUNER_MC44S803:
1036 af9015_af9013_config[i].gpio[1] = AF9013_GPIO_LO;
1037 af9015_af9013_config[i].rf_spec_inv = 1;
1038 break;
Antti Palosaari80619de2008-09-15 17:18:09 -03001039 default:
1040 warn("tuner id:%d not supported, please report!", val);
1041 return -ENODEV;
1042 };
1043
1044 af9015_af9013_config[i].tuner = val;
1045 deb_info("%s: [%d] tuner id:%d\n", __func__, i, val);
1046 }
1047
1048error:
1049 if (ret)
1050 err("eeprom read failed:%d", ret);
1051
Antti Palosaari3956fef2009-03-31 17:01:02 -03001052 /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
1053 content :-( Override some wrong values here. */
1054 if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
1055 le16_to_cpu(udev->descriptor.idProduct) == USB_PID_AVERMEDIA_A850) {
1056 deb_info("%s: AverMedia A850: overriding config\n", __func__);
1057 /* disable dual mode */
1058 af9015_config.dual_mode = 0;
1059 /* disable 2nd adapter */
1060 for (i = 0; i < af9015_properties_count; i++)
1061 af9015_properties[i].num_adapters = 1;
1062
1063 /* set correct IF */
1064 af9015_af9013_config[0].tuner_if = 4570;
1065 }
1066
Antti Palosaari80619de2008-09-15 17:18:09 -03001067 return ret;
1068}
1069
1070static int af9015_identify_state(struct usb_device *udev,
1071 struct dvb_usb_device_properties *props,
1072 struct dvb_usb_device_description **desc,
1073 int *cold)
1074{
1075 int ret;
1076 u8 reply;
1077 struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
1078
1079 ret = af9015_rw_udev(udev, &req);
1080 if (ret)
1081 return ret;
1082
1083 deb_info("%s: reply:%02x\n", __func__, reply);
1084 if (reply == 0x02)
1085 *cold = 0;
1086 else
1087 *cold = 1;
1088
1089 return ret;
1090}
1091
1092static int af9015_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
1093{
1094 u8 buf[8];
1095 struct req_t req = {GET_IR_CODE, 0, 0, 0, 0, sizeof(buf), buf};
1096 struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
1097 int i, ret;
1098
1099 memset(buf, 0, sizeof(buf));
1100
1101 ret = af9015_ctrl_msg(d, &req);
1102 if (ret)
1103 return ret;
1104
1105 *event = 0;
1106 *state = REMOTE_NO_KEY_PRESSED;
1107
1108 for (i = 0; i < d->props.rc_key_map_size; i++) {
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -03001109 if (!buf[1] && rc5_custom(&keymap[i]) == buf[0] &&
1110 rc5_data(&keymap[i]) == buf[2]) {
Antti Palosaari80619de2008-09-15 17:18:09 -03001111 *event = keymap[i].event;
1112 *state = REMOTE_KEY_PRESSED;
1113 break;
1114 }
1115 }
1116 if (!buf[1])
1117 deb_rc("%s: %02x %02x %02x %02x %02x %02x %02x %02x\n",
1118 __func__, buf[0], buf[1], buf[2], buf[3], buf[4],
1119 buf[5], buf[6], buf[7]);
1120
1121 return 0;
1122}
1123
1124/* init 2nd I2C adapter */
Antti Palosaari349d0422008-11-05 16:31:24 -03001125static int af9015_i2c_init(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -03001126{
1127 int ret;
1128 struct af9015_state *state = d->priv;
1129 deb_info("%s:\n", __func__);
1130
1131 strncpy(state->i2c_adap.name, d->desc->name,
1132 sizeof(state->i2c_adap.name));
1133#ifdef I2C_ADAP_CLASS_TV_DIGITAL
1134 state->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL,
1135#else
1136 state->i2c_adap.class = I2C_CLASS_TV_DIGITAL,
1137#endif
1138 state->i2c_adap.algo = d->props.i2c_algo;
1139 state->i2c_adap.algo_data = NULL;
1140 state->i2c_adap.dev.parent = &d->udev->dev;
1141
1142 i2c_set_adapdata(&state->i2c_adap, d);
1143
1144 ret = i2c_add_adapter(&state->i2c_adap);
1145 if (ret < 0)
1146 err("could not add i2c adapter");
1147
1148 return ret;
1149}
1150
1151static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
1152{
1153 int ret;
1154 struct af9015_state *state = adap->dev->priv;
1155 struct i2c_adapter *i2c_adap;
1156
1157 if (adap->id == 0) {
1158 /* select I2C adapter */
1159 i2c_adap = &adap->dev->i2c_adap;
1160
1161 deb_info("%s: init I2C\n", __func__);
1162 ret = af9015_i2c_init(adap->dev);
Antti Palosaari80619de2008-09-15 17:18:09 -03001163 } else {
1164 /* select I2C adapter */
1165 i2c_adap = &state->i2c_adap;
1166
1167 /* copy firmware to 2nd demodulator */
1168 if (af9015_config.dual_mode) {
1169 ret = af9015_copy_firmware(adap->dev);
1170 if (ret) {
1171 err("firmware copy to 2nd frontend " \
1172 "failed, will disable it");
1173 af9015_config.dual_mode = 0;
1174 return -ENODEV;
1175 }
1176 } else {
1177 return -ENODEV;
1178 }
1179 }
1180
1181 /* attach demodulator */
1182 adap->fe = dvb_attach(af9013_attach, &af9015_af9013_config[adap->id],
1183 i2c_adap);
1184
1185 return adap->fe == NULL ? -ENODEV : 0;
1186}
1187
1188static struct mt2060_config af9015_mt2060_config = {
1189 .i2c_address = 0xc0,
1190 .clock_out = 0,
1191};
1192
1193static struct qt1010_config af9015_qt1010_config = {
1194 .i2c_address = 0xc4,
1195};
1196
1197static struct tda18271_config af9015_tda18271_config = {
1198 .gate = TDA18271_GATE_DIGITAL,
1199 .small_i2c = 1,
1200};
1201
1202static struct mxl5005s_config af9015_mxl5003_config = {
1203 .i2c_address = 0xc6,
1204 .if_freq = IF_FREQ_4570000HZ,
1205 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1206 .agc_mode = MXL_SINGLE_AGC,
1207 .tracking_filter = MXL_TF_DEFAULT,
Antti Palosaaria1310772008-09-22 13:59:25 -03001208 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -03001209 .cap_select = MXL_CAP_SEL_ENABLE,
1210 .div_out = MXL_DIV_OUT_4,
1211 .clock_out = MXL_CLOCK_OUT_DISABLE,
1212 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1213 .top = MXL5005S_TOP_25P2,
1214 .mod_mode = MXL_DIGITAL_MODE,
1215 .if_mode = MXL_ZERO_IF,
1216 .AgcMasterByte = 0x00,
1217};
1218
1219static struct mxl5005s_config af9015_mxl5005_config = {
1220 .i2c_address = 0xc6,
1221 .if_freq = IF_FREQ_4570000HZ,
1222 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1223 .agc_mode = MXL_SINGLE_AGC,
1224 .tracking_filter = MXL_TF_OFF,
Antti Palosaaria1310772008-09-22 13:59:25 -03001225 .rssi_enable = MXL_RSSI_ENABLE,
Antti Palosaari80619de2008-09-15 17:18:09 -03001226 .cap_select = MXL_CAP_SEL_ENABLE,
1227 .div_out = MXL_DIV_OUT_4,
1228 .clock_out = MXL_CLOCK_OUT_DISABLE,
1229 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1230 .top = MXL5005S_TOP_25P2,
1231 .mod_mode = MXL_DIGITAL_MODE,
1232 .if_mode = MXL_ZERO_IF,
1233 .AgcMasterByte = 0x00,
1234};
1235
Jochen Friedrichd5633992009-02-02 14:59:50 -03001236static struct mc44s803_config af9015_mc44s803_config = {
1237 .i2c_address = 0xc0,
1238 .dig_out = 1,
1239};
1240
Antti Palosaari80619de2008-09-15 17:18:09 -03001241static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
1242{
1243 struct af9015_state *state = adap->dev->priv;
1244 struct i2c_adapter *i2c_adap;
1245 int ret;
1246 deb_info("%s: \n", __func__);
1247
1248 /* select I2C adapter */
1249 if (adap->id == 0)
1250 i2c_adap = &adap->dev->i2c_adap;
1251 else
1252 i2c_adap = &state->i2c_adap;
1253
1254 switch (af9015_af9013_config[adap->id].tuner) {
1255 case AF9013_TUNER_MT2060:
1256 case AF9013_TUNER_MT2060_2:
1257 ret = dvb_attach(mt2060_attach, adap->fe, i2c_adap,
1258 &af9015_mt2060_config,
1259 af9015_config.mt2060_if1[adap->id])
1260 == NULL ? -ENODEV : 0;
1261 break;
1262 case AF9013_TUNER_QT1010:
1263 case AF9013_TUNER_QT1010A:
1264 ret = dvb_attach(qt1010_attach, adap->fe, i2c_adap,
1265 &af9015_qt1010_config) == NULL ? -ENODEV : 0;
1266 break;
1267 case AF9013_TUNER_TDA18271:
1268 ret = dvb_attach(tda18271_attach, adap->fe, 0xc0, i2c_adap,
1269 &af9015_tda18271_config) == NULL ? -ENODEV : 0;
1270 break;
1271 case AF9013_TUNER_MXL5003D:
1272 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1273 &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
1274 break;
1275 case AF9013_TUNER_MXL5005D:
1276 case AF9013_TUNER_MXL5005R:
1277 ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
1278 &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
1279 break;
1280 case AF9013_TUNER_ENV77H11D5:
1281 ret = dvb_attach(dvb_pll_attach, adap->fe, 0xc0, i2c_adap,
1282 DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
1283 break;
1284 case AF9013_TUNER_MC44S803:
Jochen Friedrichd5633992009-02-02 14:59:50 -03001285 ret = dvb_attach(mc44s803_attach, adap->fe, i2c_adap,
1286 &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
Antti Palosaari80619de2008-09-15 17:18:09 -03001287 break;
1288 case AF9013_TUNER_UNKNOWN:
1289 default:
1290 ret = -ENODEV;
1291 err("Unknown tuner id:%d",
1292 af9015_af9013_config[adap->id].tuner);
1293 }
1294 return ret;
1295}
1296
1297static struct usb_device_id af9015_usb_table[] = {
1298/* 0 */{USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015)},
1299 {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016)},
1300 {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD)},
1301 {USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E)},
1302 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U)},
1303/* 5 */{USB_DEVICE(USB_VID_VISIONPLUS,
1304 USB_PID_TINYTWIN)},
1305 {USB_DEVICE(USB_VID_VISIONPLUS,
1306 USB_PID_AZUREWAVE_AD_TU700)},
1307 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2)},
1308 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T)},
1309 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X)},
1310/* 10 */{USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380)},
1311 {USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO)},
1312 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2)},
Antti Palosaaria3765882008-09-19 18:34:06 -03001313 {USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2)},
Antti Palosaari05c1cab2008-09-22 12:32:37 -03001314 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309)},
Herbert Graeber641015a2008-10-07 10:06:36 -03001315/* 15 */{USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III)},
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001316 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U)},
Antti Palosaari71bf2e02009-01-13 12:47:28 -03001317 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2)},
Antti Palosaari58fe1592009-03-26 20:41:05 -03001318 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3)},
Marc Schneider26144842009-03-26 21:07:18 -03001319 {USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT)},
Antti Palosaari1ed5fad2009-04-09 15:14:18 -03001320/* 20 */{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850)},
1321 {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805)},
Marcel Jueling734dd232009-04-09 17:16:41 -03001322 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU)},
Wen-chien Jesse Sung6e9c1a22009-04-28 01:11:22 -03001323 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810)},
Antti Palosaari22d46452009-05-31 17:07:01 -03001324 {USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03)},
Mart Raudseppc92f0562009-07-24 13:45:41 -03001325/* 25 */{USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U_2)},
Antti Palosaari486ba122009-09-18 13:37:57 -03001326 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_T)},
Ignacio de Miguel Diaz52322632009-11-13 23:13:34 -03001327 {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20)},
Antti Palosaari80619de2008-09-15 17:18:09 -03001328 {0},
1329};
1330MODULE_DEVICE_TABLE(usb, af9015_usb_table);
1331
1332static struct dvb_usb_device_properties af9015_properties[] = {
1333 {
1334 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1335
1336 .usb_ctrl = DEVICE_SPECIFIC,
1337 .download_firmware = af9015_download_firmware,
1338 .firmware = "dvb-usb-af9015.fw",
Jose Alberto Reguerocce25712008-11-13 14:14:18 -03001339 .no_reconnect = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001340
Antti Palosaari02542942009-09-16 20:33:03 -03001341 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari80619de2008-09-15 17:18:09 -03001342
1343 .num_adapters = 2,
1344 .adapter = {
1345 {
1346 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1347 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1348
1349 .pid_filter_count = 32,
1350 .pid_filter = af9015_pid_filter,
1351 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1352
1353 .frontend_attach =
1354 af9015_af9013_frontend_attach,
1355 .tuner_attach = af9015_tuner_attach,
1356 .stream = {
1357 .type = USB_BULK,
1358 .count = 6,
1359 .endpoint = 0x84,
1360 },
1361 },
1362 {
1363 .frontend_attach =
1364 af9015_af9013_frontend_attach,
1365 .tuner_attach = af9015_tuner_attach,
1366 .stream = {
1367 .type = USB_BULK,
1368 .count = 6,
1369 .endpoint = 0x85,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001370 .u = {
1371 .bulk = {
1372 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001373 TS_USB20_FRAME_SIZE,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001374 }
1375 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001376 },
1377 }
1378 },
1379
1380 .identify_state = af9015_identify_state,
1381
1382 .rc_query = af9015_rc_query,
1383 .rc_interval = 150,
1384
1385 .i2c_algo = &af9015_i2c_algo,
1386
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001387 .num_device_descs = 9, /* max 9 */
Antti Palosaari80619de2008-09-15 17:18:09 -03001388 .devices = {
1389 {
1390 .name = "Afatech AF9015 DVB-T USB2.0 stick",
1391 .cold_ids = {&af9015_usb_table[0],
1392 &af9015_usb_table[1], NULL},
1393 .warm_ids = {NULL},
1394 },
1395 {
1396 .name = "Leadtek WinFast DTV Dongle Gold",
1397 .cold_ids = {&af9015_usb_table[2], NULL},
1398 .warm_ids = {NULL},
1399 },
1400 {
1401 .name = "Pinnacle PCTV 71e",
1402 .cold_ids = {&af9015_usb_table[3], NULL},
1403 .warm_ids = {NULL},
1404 },
1405 {
1406 .name = "KWorld PlusTV Dual DVB-T Stick " \
1407 "(DVB-T 399U)",
Mart Raudseppc92f0562009-07-24 13:45:41 -03001408 .cold_ids = {&af9015_usb_table[4],
1409 &af9015_usb_table[25], NULL},
Antti Palosaari80619de2008-09-15 17:18:09 -03001410 .warm_ids = {NULL},
1411 },
1412 {
1413 .name = "DigitalNow TinyTwin DVB-T Receiver",
1414 .cold_ids = {&af9015_usb_table[5], NULL},
1415 .warm_ids = {NULL},
1416 },
1417 {
1418 .name = "TwinHan AzureWave AD-TU700(704J)",
1419 .cold_ids = {&af9015_usb_table[6], NULL},
1420 .warm_ids = {NULL},
1421 },
1422 {
1423 .name = "TerraTec Cinergy T USB XE",
1424 .cold_ids = {&af9015_usb_table[7], NULL},
1425 .warm_ids = {NULL},
1426 },
1427 {
1428 .name = "KWorld PlusTV Dual DVB-T PCI " \
1429 "(DVB-T PC160-2T)",
1430 .cold_ids = {&af9015_usb_table[8], NULL},
1431 .warm_ids = {NULL},
1432 },
1433 {
1434 .name = "AVerMedia AVerTV DVB-T Volar X",
1435 .cold_ids = {&af9015_usb_table[9], NULL},
1436 .warm_ids = {NULL},
1437 },
1438 }
1439 }, {
1440 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1441
1442 .usb_ctrl = DEVICE_SPECIFIC,
1443 .download_firmware = af9015_download_firmware,
1444 .firmware = "dvb-usb-af9015.fw",
Jose Alberto Reguerocce25712008-11-13 14:14:18 -03001445 .no_reconnect = 1,
Antti Palosaari80619de2008-09-15 17:18:09 -03001446
Antti Palosaari02542942009-09-16 20:33:03 -03001447 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari80619de2008-09-15 17:18:09 -03001448
1449 .num_adapters = 2,
1450 .adapter = {
1451 {
1452 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1453 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1454
1455 .pid_filter_count = 32,
1456 .pid_filter = af9015_pid_filter,
1457 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1458
1459 .frontend_attach =
1460 af9015_af9013_frontend_attach,
1461 .tuner_attach = af9015_tuner_attach,
1462 .stream = {
1463 .type = USB_BULK,
1464 .count = 6,
1465 .endpoint = 0x84,
1466 },
1467 },
1468 {
1469 .frontend_attach =
1470 af9015_af9013_frontend_attach,
1471 .tuner_attach = af9015_tuner_attach,
1472 .stream = {
1473 .type = USB_BULK,
1474 .count = 6,
1475 .endpoint = 0x85,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001476 .u = {
1477 .bulk = {
1478 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001479 TS_USB20_FRAME_SIZE,
Antti Palosaarif0830eb2009-01-13 13:08:29 -03001480 }
1481 }
Antti Palosaari80619de2008-09-15 17:18:09 -03001482 },
1483 }
1484 },
1485
1486 .identify_state = af9015_identify_state,
1487
1488 .rc_query = af9015_rc_query,
1489 .rc_interval = 150,
1490
1491 .i2c_algo = &af9015_i2c_algo,
1492
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001493 .num_device_descs = 9, /* max 9 */
Antti Palosaari80619de2008-09-15 17:18:09 -03001494 .devices = {
1495 {
1496 .name = "Xtensions XD-380",
1497 .cold_ids = {&af9015_usb_table[10], NULL},
1498 .warm_ids = {NULL},
1499 },
1500 {
1501 .name = "MSI DIGIVOX Duo",
1502 .cold_ids = {&af9015_usb_table[11], NULL},
1503 .warm_ids = {NULL},
1504 },
1505 {
1506 .name = "Fujitsu-Siemens Slim Mobile USB DVB-T",
1507 .cold_ids = {&af9015_usb_table[12], NULL},
1508 .warm_ids = {NULL},
1509 },
Mikko Ohtamaa111f9ec2008-09-19 18:26:05 -03001510 {
Antti Palosaaria3765882008-09-19 18:34:06 -03001511 .name = "Telestar Starstick 2",
Mikko Ohtamaa111f9ec2008-09-19 18:26:05 -03001512 .cold_ids = {&af9015_usb_table[13], NULL},
1513 .warm_ids = {NULL},
1514 },
Antti Palosaari05c1cab2008-09-22 12:32:37 -03001515 {
1516 .name = "AVerMedia A309",
1517 .cold_ids = {&af9015_usb_table[14], NULL},
1518 .warm_ids = {NULL},
1519 },
Herbert Graeber641015a2008-10-07 10:06:36 -03001520 {
1521 .name = "MSI Digi VOX mini III",
1522 .cold_ids = {&af9015_usb_table[15], NULL},
1523 .warm_ids = {NULL},
1524 },
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001525 {
1526 .name = "KWorld USB DVB-T TV Stick II " \
1527 "(VS-DVB-T 395U)",
Antti Palosaari71bf2e02009-01-13 12:47:28 -03001528 .cold_ids = {&af9015_usb_table[16],
Antti Palosaari58fe1592009-03-26 20:41:05 -03001529 &af9015_usb_table[17],
1530 &af9015_usb_table[18], NULL},
Antti Palosaari163e9cd2008-11-04 12:57:47 -03001531 .warm_ids = {NULL},
1532 },
Marc Schneider26144842009-03-26 21:07:18 -03001533 {
1534 .name = "TrekStor DVB-T USB Stick",
1535 .cold_ids = {&af9015_usb_table[19], NULL},
1536 .warm_ids = {NULL},
1537 },
Antti Palosaari3956fef2009-03-31 17:01:02 -03001538 {
1539 .name = "AverMedia AVerTV Volar Black HD " \
1540 "(A850)",
1541 .cold_ids = {&af9015_usb_table[20], NULL},
1542 .warm_ids = {NULL},
1543 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001544 }
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001545 }, {
1546 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1547
1548 .usb_ctrl = DEVICE_SPECIFIC,
1549 .download_firmware = af9015_download_firmware,
1550 .firmware = "dvb-usb-af9015.fw",
1551 .no_reconnect = 1,
1552
Antti Palosaari02542942009-09-16 20:33:03 -03001553 .size_of_priv = sizeof(struct af9015_state),
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001554
1555 .num_adapters = 2,
1556 .adapter = {
1557 {
1558 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1559 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1560
1561 .pid_filter_count = 32,
1562 .pid_filter = af9015_pid_filter,
1563 .pid_filter_ctrl = af9015_pid_filter_ctrl,
1564
1565 .frontend_attach =
1566 af9015_af9013_frontend_attach,
1567 .tuner_attach = af9015_tuner_attach,
1568 .stream = {
1569 .type = USB_BULK,
1570 .count = 6,
1571 .endpoint = 0x84,
1572 },
1573 },
1574 {
1575 .frontend_attach =
1576 af9015_af9013_frontend_attach,
1577 .tuner_attach = af9015_tuner_attach,
1578 .stream = {
1579 .type = USB_BULK,
1580 .count = 6,
1581 .endpoint = 0x85,
1582 .u = {
1583 .bulk = {
1584 .buffersize =
Jose Alberto Reguero353330c2009-09-12 09:51:36 -03001585 TS_USB20_FRAME_SIZE,
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001586 }
1587 }
1588 },
1589 }
1590 },
1591
1592 .identify_state = af9015_identify_state,
1593
1594 .rc_query = af9015_rc_query,
1595 .rc_interval = 150,
1596
1597 .i2c_algo = &af9015_i2c_algo,
1598
Ignacio de Miguel Diaz52322632009-11-13 23:13:34 -03001599 .num_device_descs = 6, /* max 9 */
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001600 .devices = {
Antti Palosaari1ed5fad2009-04-09 15:14:18 -03001601 {
1602 .name = "AverMedia AVerTV Volar GPS 805 (A805)",
1603 .cold_ids = {&af9015_usb_table[21], NULL},
1604 .warm_ids = {NULL},
1605 },
Marcel Jueling734dd232009-04-09 17:16:41 -03001606 {
1607 .name = "Conceptronic USB2.0 DVB-T CTVDIGRCU " \
1608 "V3.0",
1609 .cold_ids = {&af9015_usb_table[22], NULL},
1610 .warm_ids = {NULL},
1611 },
Wen-chien Jesse Sung6e9c1a22009-04-28 01:11:22 -03001612 {
1613 .name = "KWorld Digial MC-810",
1614 .cold_ids = {&af9015_usb_table[23], NULL},
1615 .warm_ids = {NULL},
1616 },
Antti Palosaari22d46452009-05-31 17:07:01 -03001617 {
1618 .name = "Genius TVGo DVB-T03",
1619 .cold_ids = {&af9015_usb_table[24], NULL},
1620 .warm_ids = {NULL},
1621 },
Antti Palosaari486ba122009-09-18 13:37:57 -03001622 {
1623 .name = "KWorld PlusTV DVB-T PCI Pro Card " \
1624 "(DVB-T PC160-T)",
1625 .cold_ids = {&af9015_usb_table[26], NULL},
1626 .warm_ids = {NULL},
1627 },
Ignacio de Miguel Diaz52322632009-11-13 23:13:34 -03001628 {
1629 .name = "Sveon STV20 Tuner USB DVB-T HDTV",
1630 .cold_ids = {&af9015_usb_table[27], NULL},
1631 .warm_ids = {NULL},
1632 },
Antti Palosaari85d7d7c2009-04-09 09:16:12 -03001633 }
1634 },
Antti Palosaari80619de2008-09-15 17:18:09 -03001635};
Antti Palosaari80619de2008-09-15 17:18:09 -03001636
1637static int af9015_usb_probe(struct usb_interface *intf,
1638 const struct usb_device_id *id)
1639{
1640 int ret = 0;
1641 struct dvb_usb_device *d = NULL;
1642 struct usb_device *udev = interface_to_usbdev(intf);
1643 u8 i;
1644
1645 deb_info("%s: interface:%d\n", __func__,
1646 intf->cur_altsetting->desc.bInterfaceNumber);
1647
1648 /* interface 0 is used by DVB-T receiver and
1649 interface 1 is for remote controller (HID) */
1650 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
1651 ret = af9015_read_config(udev);
1652 if (ret)
1653 return ret;
1654
1655 for (i = 0; i < af9015_properties_count; i++) {
1656 ret = dvb_usb_device_init(intf, &af9015_properties[i],
1657 THIS_MODULE, &d, adapter_nr);
1658 if (!ret)
1659 break;
1660 if (ret != -ENODEV)
1661 return ret;
1662 }
1663 if (ret)
1664 return ret;
1665
1666 if (d)
1667 ret = af9015_init(d);
1668 }
1669
1670 return ret;
1671}
1672
Antti Palosaari349d0422008-11-05 16:31:24 -03001673static void af9015_i2c_exit(struct dvb_usb_device *d)
Antti Palosaari80619de2008-09-15 17:18:09 -03001674{
1675 struct af9015_state *state = d->priv;
1676 deb_info("%s: \n", __func__);
1677
1678 /* remove 2nd I2C adapter */
1679 if (d->state & DVB_USB_STATE_I2C)
1680 i2c_del_adapter(&state->i2c_adap);
1681}
1682
1683static void af9015_usb_device_exit(struct usb_interface *intf)
1684{
1685 struct dvb_usb_device *d = usb_get_intfdata(intf);
1686 deb_info("%s: \n", __func__);
1687
1688 /* remove 2nd I2C adapter */
1689 if (d != NULL && d->desc != NULL)
1690 af9015_i2c_exit(d);
1691
1692 dvb_usb_device_exit(intf);
1693}
1694
1695/* usb specific object needed to register this driver with the usb subsystem */
1696static struct usb_driver af9015_usb_driver = {
1697 .name = "dvb_usb_af9015",
1698 .probe = af9015_usb_probe,
1699 .disconnect = af9015_usb_device_exit,
1700 .id_table = af9015_usb_table,
1701};
1702
1703/* module stuff */
1704static int __init af9015_usb_module_init(void)
1705{
1706 int ret;
1707 ret = usb_register(&af9015_usb_driver);
1708 if (ret)
1709 err("module init failed:%d", ret);
1710
1711 return ret;
1712}
1713
1714static void __exit af9015_usb_module_exit(void)
1715{
1716 /* deregister this driver from the USB subsystem */
1717 usb_deregister(&af9015_usb_driver);
1718}
1719
1720module_init(af9015_usb_module_init);
1721module_exit(af9015_usb_module_exit);
1722
1723MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1724MODULE_DESCRIPTION("Driver for Afatech AF9015 DVB-T");
1725MODULE_LICENSE("GPL");