hamradio/scc: add missing block braces to multi-statement if
[linux/fpc-iii.git] / drivers / media / dvb / dvb-usb / af9005.c
blobcfe71feefcad27a8e64598316a366bda99ae8985
1 /* DVB USB compliant Linux driver for the Afatech 9005
2 * USB1.1 DVB-T receiver.
4 * Copyright (C) 2007 Luca Olivetti (luca@ventoso.org)
6 * Thanks to Afatech who kindly provided information.
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.
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.
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.
22 * see Documentation/dvb/REDME.dvb-usb for more information
24 #include "af9005.h"
26 /* debug */
27 int dvb_usb_af9005_debug;
28 module_param_named(debug, dvb_usb_af9005_debug, int, 0644);
29 MODULE_PARM_DESC(debug,
30 "set debugging level (1=info,xfer=2,rc=4,reg=8,i2c=16,fw=32 (or-able))."
31 DVB_USB_DEBUG_STATUS);
32 /* enable obnoxious led */
33 int dvb_usb_af9005_led = 1;
34 module_param_named(led, dvb_usb_af9005_led, bool, 0644);
35 MODULE_PARM_DESC(led, "enable led (default: 1).");
37 /* eeprom dump */
38 int dvb_usb_af9005_dump_eeprom = 0;
39 module_param_named(dump_eeprom, dvb_usb_af9005_dump_eeprom, int, 0);
40 MODULE_PARM_DESC(dump_eeprom, "dump contents of the eeprom.");
42 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
44 /* remote control decoder */
45 int (*rc_decode) (struct dvb_usb_device * d, u8 * data, int len, u32 * event,
46 int *state);
47 void *rc_keys;
48 int *rc_keys_size;
50 u8 regmask[8] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
52 struct af9005_device_state {
53 u8 sequence;
54 int led_state;
57 int af9005_usb_generic_rw(struct dvb_usb_device *d, u8 * wbuf, u16 wlen,
58 u8 * rbuf, u16 rlen, int delay_ms)
60 int actlen, ret = -ENOMEM;
62 if (wbuf == NULL || wlen == 0)
63 return -EINVAL;
65 if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
66 return ret;
68 deb_xfer(">>> ");
69 debug_dump(wbuf, wlen, deb_xfer);
71 ret = usb_bulk_msg(d->udev, usb_sndbulkpipe(d->udev,
72 2), wbuf, wlen,
73 &actlen, 2000);
75 if (ret)
76 err("bulk message failed: %d (%d/%d)", ret, wlen, actlen);
77 else
78 ret = actlen != wlen ? -1 : 0;
80 /* an answer is expected, and no error before */
81 if (!ret && rbuf && rlen) {
82 if (delay_ms)
83 msleep(delay_ms);
85 ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev,
86 0x01), rbuf,
87 rlen, &actlen, 2000);
89 if (ret)
90 err("recv bulk message failed: %d", ret);
91 else {
92 deb_xfer("<<< ");
93 debug_dump(rbuf, actlen, deb_xfer);
97 mutex_unlock(&d->usb_mutex);
98 return ret;
101 int af9005_usb_generic_write(struct dvb_usb_device *d, u8 * buf, u16 len)
103 return af9005_usb_generic_rw(d, buf, len, NULL, 0, 0);
106 int af9005_generic_read_write(struct dvb_usb_device *d, u16 reg,
107 int readwrite, int type, u8 * values, int len)
109 struct af9005_device_state *st = d->priv;
110 u8 obuf[16] = { 0 };
111 u8 ibuf[17] = { 0 };
112 u8 command;
113 int i;
114 int ret;
116 if (len < 1) {
117 err("generic read/write, less than 1 byte. Makes no sense.");
118 return -EINVAL;
120 if (len > 8) {
121 err("generic read/write, more than 8 bytes. Not supported.");
122 return -EINVAL;
125 obuf[0] = 14; /* rest of buffer length low */
126 obuf[1] = 0; /* rest of buffer length high */
128 obuf[2] = AF9005_REGISTER_RW; /* register operation */
129 obuf[3] = 12; /* rest of buffer length */
131 obuf[4] = st->sequence++; /* sequence number */
133 obuf[5] = (u8) (reg >> 8); /* register address */
134 obuf[6] = (u8) (reg & 0xff);
136 if (type == AF9005_OFDM_REG) {
137 command = AF9005_CMD_OFDM_REG;
138 } else {
139 command = AF9005_CMD_TUNER;
142 if (len > 1)
143 command |=
144 AF9005_CMD_BURST | AF9005_CMD_AUTOINC | (len - 1) << 3;
145 command |= readwrite;
146 if (readwrite == AF9005_CMD_WRITE)
147 for (i = 0; i < len; i++)
148 obuf[8 + i] = values[i];
149 else if (type == AF9005_TUNER_REG)
150 /* read command for tuner, the first byte contains the i2c address */
151 obuf[8] = values[0];
152 obuf[7] = command;
154 ret = af9005_usb_generic_rw(d, obuf, 16, ibuf, 17, 0);
155 if (ret)
156 return ret;
158 /* sanity check */
159 if (ibuf[2] != AF9005_REGISTER_RW_ACK) {
160 err("generic read/write, wrong reply code.");
161 return -EIO;
163 if (ibuf[3] != 0x0d) {
164 err("generic read/write, wrong length in reply.");
165 return -EIO;
167 if (ibuf[4] != obuf[4]) {
168 err("generic read/write, wrong sequence in reply.");
169 return -EIO;
172 Windows driver doesn't check these fields, in fact sometimes
173 the register in the reply is different that what has been sent
175 if (ibuf[5] != obuf[5] || ibuf[6] != obuf[6]) {
176 err("generic read/write, wrong register in reply.");
177 return -EIO;
179 if (ibuf[7] != command) {
180 err("generic read/write wrong command in reply.");
181 return -EIO;
184 if (ibuf[16] != 0x01) {
185 err("generic read/write wrong status code in reply.");
186 return -EIO;
188 if (readwrite == AF9005_CMD_READ)
189 for (i = 0; i < len; i++)
190 values[i] = ibuf[8 + i];
192 return 0;
196 int af9005_read_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 * value)
198 int ret;
199 deb_reg("read register %x ", reg);
200 ret = af9005_generic_read_write(d, reg,
201 AF9005_CMD_READ, AF9005_OFDM_REG,
202 value, 1);
203 if (ret)
204 deb_reg("failed\n");
205 else
206 deb_reg("value %x\n", *value);
207 return ret;
210 int af9005_read_ofdm_registers(struct dvb_usb_device *d, u16 reg,
211 u8 * values, int len)
213 int ret;
214 deb_reg("read %d registers %x ", len, reg);
215 ret = af9005_generic_read_write(d, reg,
216 AF9005_CMD_READ, AF9005_OFDM_REG,
217 values, len);
218 if (ret)
219 deb_reg("failed\n");
220 else
221 debug_dump(values, len, deb_reg);
222 return ret;
225 int af9005_write_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 value)
227 int ret;
228 u8 temp = value;
229 deb_reg("write register %x value %x ", reg, value);
230 ret = af9005_generic_read_write(d, reg,
231 AF9005_CMD_WRITE, AF9005_OFDM_REG,
232 &temp, 1);
233 if (ret)
234 deb_reg("failed\n");
235 else
236 deb_reg("ok\n");
237 return ret;
240 int af9005_write_ofdm_registers(struct dvb_usb_device *d, u16 reg,
241 u8 * values, int len)
243 int ret;
244 deb_reg("write %d registers %x values ", len, reg);
245 debug_dump(values, len, deb_reg);
247 ret = af9005_generic_read_write(d, reg,
248 AF9005_CMD_WRITE, AF9005_OFDM_REG,
249 values, len);
250 if (ret)
251 deb_reg("failed\n");
252 else
253 deb_reg("ok\n");
254 return ret;
257 int af9005_read_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos,
258 u8 len, u8 * value)
260 u8 temp;
261 int ret;
262 deb_reg("read bits %x %x %x", reg, pos, len);
263 ret = af9005_read_ofdm_register(d, reg, &temp);
264 if (ret) {
265 deb_reg(" failed\n");
266 return ret;
268 *value = (temp >> pos) & regmask[len - 1];
269 deb_reg(" value %x\n", *value);
270 return 0;
274 int af9005_write_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos,
275 u8 len, u8 value)
277 u8 temp, mask;
278 int ret;
279 deb_reg("write bits %x %x %x value %x\n", reg, pos, len, value);
280 if (pos == 0 && len == 8)
281 return af9005_write_ofdm_register(d, reg, value);
282 ret = af9005_read_ofdm_register(d, reg, &temp);
283 if (ret)
284 return ret;
285 mask = regmask[len - 1] << pos;
286 temp = (temp & ~mask) | ((value << pos) & mask);
287 return af9005_write_ofdm_register(d, reg, temp);
291 static int af9005_usb_read_tuner_registers(struct dvb_usb_device *d,
292 u16 reg, u8 * values, int len)
294 return af9005_generic_read_write(d, reg,
295 AF9005_CMD_READ, AF9005_TUNER_REG,
296 values, len);
299 static int af9005_usb_write_tuner_registers(struct dvb_usb_device *d,
300 u16 reg, u8 * values, int len)
302 return af9005_generic_read_write(d, reg,
303 AF9005_CMD_WRITE,
304 AF9005_TUNER_REG, values, len);
307 int af9005_write_tuner_registers(struct dvb_usb_device *d, u16 reg,
308 u8 * values, int len)
310 /* don't let the name of this function mislead you: it's just used
311 as an interface from the firmware to the i2c bus. The actual
312 i2c addresses are contained in the data */
313 int ret, i, done = 0, fail = 0;
314 u8 temp;
315 ret = af9005_usb_write_tuner_registers(d, reg, values, len);
316 if (ret)
317 return ret;
318 if (reg != 0xffff) {
319 /* check if write done (0xa40d bit 1) or fail (0xa40d bit 2) */
320 for (i = 0; i < 200; i++) {
321 ret =
322 af9005_read_ofdm_register(d,
323 xd_I2C_i2c_m_status_wdat_done,
324 &temp);
325 if (ret)
326 return ret;
327 done = temp & (regmask[i2c_m_status_wdat_done_len - 1]
328 << i2c_m_status_wdat_done_pos);
329 if (done)
330 break;
331 fail = temp & (regmask[i2c_m_status_wdat_fail_len - 1]
332 << i2c_m_status_wdat_fail_pos);
333 if (fail)
334 break;
335 msleep(50);
337 if (i == 200)
338 return -ETIMEDOUT;
339 if (fail) {
340 /* clear write fail bit */
341 af9005_write_register_bits(d,
342 xd_I2C_i2c_m_status_wdat_fail,
343 i2c_m_status_wdat_fail_pos,
344 i2c_m_status_wdat_fail_len,
346 return -EIO;
348 /* clear write done bit */
349 ret =
350 af9005_write_register_bits(d,
351 xd_I2C_i2c_m_status_wdat_fail,
352 i2c_m_status_wdat_done_pos,
353 i2c_m_status_wdat_done_len, 1);
354 if (ret)
355 return ret;
357 return 0;
360 int af9005_read_tuner_registers(struct dvb_usb_device *d, u16 reg, u8 addr,
361 u8 * values, int len)
363 /* don't let the name of this function mislead you: it's just used
364 as an interface from the firmware to the i2c bus. The actual
365 i2c addresses are contained in the data */
366 int ret, i;
367 u8 temp, buf[2];
369 buf[0] = addr; /* tuner i2c address */
370 buf[1] = values[0]; /* tuner register */
372 values[0] = addr + 0x01; /* i2c read address */
374 if (reg == APO_REG_I2C_RW_SILICON_TUNER) {
375 /* write tuner i2c address to tuner, 0c00c0 undocumented, found by sniffing */
376 ret = af9005_write_tuner_registers(d, 0x00c0, buf, 2);
377 if (ret)
378 return ret;
381 /* send read command to ofsm */
382 ret = af9005_usb_read_tuner_registers(d, reg, values, 1);
383 if (ret)
384 return ret;
386 /* check if read done */
387 for (i = 0; i < 200; i++) {
388 ret = af9005_read_ofdm_register(d, 0xa408, &temp);
389 if (ret)
390 return ret;
391 if (temp & 0x01)
392 break;
393 msleep(50);
395 if (i == 200)
396 return -ETIMEDOUT;
398 /* clear read done bit (by writing 1) */
399 ret = af9005_write_ofdm_register(d, xd_I2C_i2c_m_data8, 1);
400 if (ret)
401 return ret;
403 /* get read data (available from 0xa400) */
404 for (i = 0; i < len; i++) {
405 ret = af9005_read_ofdm_register(d, 0xa400 + i, &temp);
406 if (ret)
407 return ret;
408 values[i] = temp;
410 return 0;
413 static int af9005_i2c_write(struct dvb_usb_device *d, u8 i2caddr, u8 reg,
414 u8 * data, int len)
416 int ret, i;
417 u8 buf[3];
418 deb_i2c("i2c_write i2caddr %x, reg %x, len %d data ", i2caddr,
419 reg, len);
420 debug_dump(data, len, deb_i2c);
422 for (i = 0; i < len; i++) {
423 buf[0] = i2caddr;
424 buf[1] = reg + (u8) i;
425 buf[2] = data[i];
426 ret =
427 af9005_write_tuner_registers(d,
428 APO_REG_I2C_RW_SILICON_TUNER,
429 buf, 3);
430 if (ret) {
431 deb_i2c("i2c_write failed\n");
432 return ret;
435 deb_i2c("i2c_write ok\n");
436 return 0;
439 static int af9005_i2c_read(struct dvb_usb_device *d, u8 i2caddr, u8 reg,
440 u8 * data, int len)
442 int ret, i;
443 u8 temp;
444 deb_i2c("i2c_read i2caddr %x, reg %x, len %d\n ", i2caddr, reg, len);
445 for (i = 0; i < len; i++) {
446 temp = reg + i;
447 ret =
448 af9005_read_tuner_registers(d,
449 APO_REG_I2C_RW_SILICON_TUNER,
450 i2caddr, &temp, 1);
451 if (ret) {
452 deb_i2c("i2c_read failed\n");
453 return ret;
455 data[i] = temp;
457 deb_i2c("i2c data read: ");
458 debug_dump(data, len, deb_i2c);
459 return 0;
462 static int af9005_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
463 int num)
465 /* only implements what the mt2060 module does, don't know how
466 to make it really generic */
467 struct dvb_usb_device *d = i2c_get_adapdata(adap);
468 int ret;
469 u8 reg, addr;
470 u8 *value;
472 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
473 return -EAGAIN;
475 if (num > 2)
476 warn("more than 2 i2c messages at a time is not handled yet. TODO.");
478 if (num == 2) {
479 /* reads a single register */
480 reg = *msg[0].buf;
481 addr = msg[0].addr;
482 value = msg[1].buf;
483 ret = af9005_i2c_read(d, addr, reg, value, 1);
484 if (ret == 0)
485 ret = 2;
486 } else {
487 /* write one or more registers */
488 reg = msg[0].buf[0];
489 addr = msg[0].addr;
490 value = &msg[0].buf[1];
491 ret = af9005_i2c_write(d, addr, reg, value, msg[0].len - 1);
492 if (ret == 0)
493 ret = 1;
496 mutex_unlock(&d->i2c_mutex);
497 return ret;
500 static u32 af9005_i2c_func(struct i2c_adapter *adapter)
502 return I2C_FUNC_I2C;
505 static struct i2c_algorithm af9005_i2c_algo = {
506 .master_xfer = af9005_i2c_xfer,
507 .functionality = af9005_i2c_func,
510 int af9005_send_command(struct dvb_usb_device *d, u8 command, u8 * wbuf,
511 int wlen, u8 * rbuf, int rlen)
513 struct af9005_device_state *st = d->priv;
515 int ret, i, packet_len;
516 u8 buf[64];
517 u8 ibuf[64];
519 if (wlen < 0) {
520 err("send command, wlen less than 0 bytes. Makes no sense.");
521 return -EINVAL;
523 if (wlen > 54) {
524 err("send command, wlen more than 54 bytes. Not supported.");
525 return -EINVAL;
527 if (rlen > 54) {
528 err("send command, rlen more than 54 bytes. Not supported.");
529 return -EINVAL;
531 packet_len = wlen + 5;
532 buf[0] = (u8) (packet_len & 0xff);
533 buf[1] = (u8) ((packet_len & 0xff00) >> 8);
535 buf[2] = 0x26; /* packet type */
536 buf[3] = wlen + 3;
537 buf[4] = st->sequence++;
538 buf[5] = command;
539 buf[6] = wlen;
540 for (i = 0; i < wlen; i++)
541 buf[7 + i] = wbuf[i];
542 ret = af9005_usb_generic_rw(d, buf, wlen + 7, ibuf, rlen + 7, 0);
543 if (ret)
544 return ret;
545 if (ibuf[2] != 0x27) {
546 err("send command, wrong reply code.");
547 return -EIO;
549 if (ibuf[4] != buf[4]) {
550 err("send command, wrong sequence in reply.");
551 return -EIO;
553 if (ibuf[5] != 0x01) {
554 err("send command, wrong status code in reply.");
555 return -EIO;
557 if (ibuf[6] != rlen) {
558 err("send command, invalid data length in reply.");
559 return -EIO;
561 for (i = 0; i < rlen; i++)
562 rbuf[i] = ibuf[i + 7];
563 return 0;
566 int af9005_read_eeprom(struct dvb_usb_device *d, u8 address, u8 * values,
567 int len)
569 struct af9005_device_state *st = d->priv;
570 u8 obuf[16], ibuf[14];
571 int ret, i;
573 memset(obuf, 0, sizeof(obuf));
574 memset(ibuf, 0, sizeof(ibuf));
576 obuf[0] = 14; /* length of rest of packet low */
577 obuf[1] = 0; /* length of rest of packer high */
579 obuf[2] = 0x2a; /* read/write eeprom */
581 obuf[3] = 12; /* size */
583 obuf[4] = st->sequence++;
585 obuf[5] = 0; /* read */
587 obuf[6] = len;
588 obuf[7] = address;
589 ret = af9005_usb_generic_rw(d, obuf, 16, ibuf, 14, 0);
590 if (ret)
591 return ret;
592 if (ibuf[2] != 0x2b) {
593 err("Read eeprom, invalid reply code");
594 return -EIO;
596 if (ibuf[3] != 10) {
597 err("Read eeprom, invalid reply length");
598 return -EIO;
600 if (ibuf[4] != obuf[4]) {
601 err("Read eeprom, wrong sequence in reply ");
602 return -EIO;
604 if (ibuf[5] != 1) {
605 err("Read eeprom, wrong status in reply ");
606 return -EIO;
608 for (i = 0; i < len; i++) {
609 values[i] = ibuf[6 + i];
611 return 0;
614 static int af9005_boot_packet(struct usb_device *udev, int type, u8 * reply)
616 u8 buf[FW_BULKOUT_SIZE + 2];
617 u16 checksum;
618 int act_len, i, ret;
619 memset(buf, 0, sizeof(buf));
620 buf[0] = (u8) (FW_BULKOUT_SIZE & 0xff);
621 buf[1] = (u8) ((FW_BULKOUT_SIZE >> 8) & 0xff);
622 switch (type) {
623 case FW_CONFIG:
624 buf[2] = 0x11;
625 buf[3] = 0x04;
626 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
627 buf[5] = 0x03;
628 checksum = buf[4] + buf[5];
629 buf[6] = (u8) ((checksum >> 8) & 0xff);
630 buf[7] = (u8) (checksum & 0xff);
631 break;
632 case FW_CONFIRM:
633 buf[2] = 0x11;
634 buf[3] = 0x04;
635 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
636 buf[5] = 0x01;
637 checksum = buf[4] + buf[5];
638 buf[6] = (u8) ((checksum >> 8) & 0xff);
639 buf[7] = (u8) (checksum & 0xff);
640 break;
641 case FW_BOOT:
642 buf[2] = 0x10;
643 buf[3] = 0x08;
644 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
645 buf[5] = 0x97;
646 buf[6] = 0xaa;
647 buf[7] = 0x55;
648 buf[8] = 0xa5;
649 buf[9] = 0x5a;
650 checksum = 0;
651 for (i = 4; i <= 9; i++)
652 checksum += buf[i];
653 buf[10] = (u8) ((checksum >> 8) & 0xff);
654 buf[11] = (u8) (checksum & 0xff);
655 break;
656 default:
657 err("boot packet invalid boot packet type");
658 return -EINVAL;
660 deb_fw(">>> ");
661 debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw);
663 ret = usb_bulk_msg(udev,
664 usb_sndbulkpipe(udev, 0x02),
665 buf, FW_BULKOUT_SIZE + 2, &act_len, 2000);
666 if (ret)
667 err("boot packet bulk message failed: %d (%d/%d)", ret,
668 FW_BULKOUT_SIZE + 2, act_len);
669 else
670 ret = act_len != FW_BULKOUT_SIZE + 2 ? -1 : 0;
671 if (ret)
672 return ret;
673 memset(buf, 0, 9);
674 ret = usb_bulk_msg(udev,
675 usb_rcvbulkpipe(udev, 0x01), buf, 9, &act_len, 2000);
676 if (ret) {
677 err("boot packet recv bulk message failed: %d", ret);
678 return ret;
680 deb_fw("<<< ");
681 debug_dump(buf, act_len, deb_fw);
682 checksum = 0;
683 switch (type) {
684 case FW_CONFIG:
685 if (buf[2] != 0x11) {
686 err("boot bad config header.");
687 return -EIO;
689 if (buf[3] != 0x05) {
690 err("boot bad config size.");
691 return -EIO;
693 if (buf[4] != 0x00) {
694 err("boot bad config sequence.");
695 return -EIO;
697 if (buf[5] != 0x04) {
698 err("boot bad config subtype.");
699 return -EIO;
701 for (i = 4; i <= 6; i++)
702 checksum += buf[i];
703 if (buf[7] * 256 + buf[8] != checksum) {
704 err("boot bad config checksum.");
705 return -EIO;
707 *reply = buf[6];
708 break;
709 case FW_CONFIRM:
710 if (buf[2] != 0x11) {
711 err("boot bad confirm header.");
712 return -EIO;
714 if (buf[3] != 0x05) {
715 err("boot bad confirm size.");
716 return -EIO;
718 if (buf[4] != 0x00) {
719 err("boot bad confirm sequence.");
720 return -EIO;
722 if (buf[5] != 0x02) {
723 err("boot bad confirm subtype.");
724 return -EIO;
726 for (i = 4; i <= 6; i++)
727 checksum += buf[i];
728 if (buf[7] * 256 + buf[8] != checksum) {
729 err("boot bad confirm checksum.");
730 return -EIO;
732 *reply = buf[6];
733 break;
734 case FW_BOOT:
735 if (buf[2] != 0x10) {
736 err("boot bad boot header.");
737 return -EIO;
739 if (buf[3] != 0x05) {
740 err("boot bad boot size.");
741 return -EIO;
743 if (buf[4] != 0x00) {
744 err("boot bad boot sequence.");
745 return -EIO;
747 if (buf[5] != 0x01) {
748 err("boot bad boot pattern 01.");
749 return -EIO;
751 if (buf[6] != 0x10) {
752 err("boot bad boot pattern 10.");
753 return -EIO;
755 for (i = 4; i <= 6; i++)
756 checksum += buf[i];
757 if (buf[7] * 256 + buf[8] != checksum) {
758 err("boot bad boot checksum.");
759 return -EIO;
761 break;
765 return 0;
768 int af9005_download_firmware(struct usb_device *udev, const struct firmware *fw)
770 int i, packets, ret, act_len;
772 u8 buf[FW_BULKOUT_SIZE + 2];
773 u8 reply;
775 ret = af9005_boot_packet(udev, FW_CONFIG, &reply);
776 if (ret)
777 return ret;
778 if (reply != 0x01) {
779 err("before downloading firmware, FW_CONFIG expected 0x01, received 0x%x", reply);
780 return -EIO;
782 packets = fw->size / FW_BULKOUT_SIZE;
783 buf[0] = (u8) (FW_BULKOUT_SIZE & 0xff);
784 buf[1] = (u8) ((FW_BULKOUT_SIZE >> 8) & 0xff);
785 for (i = 0; i < packets; i++) {
786 memcpy(&buf[2], fw->data + i * FW_BULKOUT_SIZE,
787 FW_BULKOUT_SIZE);
788 deb_fw(">>> ");
789 debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw);
790 ret = usb_bulk_msg(udev,
791 usb_sndbulkpipe(udev, 0x02),
792 buf, FW_BULKOUT_SIZE + 2, &act_len, 1000);
793 if (ret) {
794 err("firmware download failed at packet %d with code %d", i, ret);
795 return ret;
798 ret = af9005_boot_packet(udev, FW_CONFIRM, &reply);
799 if (ret)
800 return ret;
801 if (reply != (u8) (packets & 0xff)) {
802 err("after downloading firmware, FW_CONFIRM expected 0x%x, received 0x%x", packets & 0xff, reply);
803 return -EIO;
805 ret = af9005_boot_packet(udev, FW_BOOT, &reply);
806 if (ret)
807 return ret;
808 ret = af9005_boot_packet(udev, FW_CONFIG, &reply);
809 if (ret)
810 return ret;
811 if (reply != 0x02) {
812 err("after downloading firmware, FW_CONFIG expected 0x02, received 0x%x", reply);
813 return -EIO;
816 return 0;
820 int af9005_led_control(struct dvb_usb_device *d, int onoff)
822 struct af9005_device_state *st = d->priv;
823 int temp, ret;
825 if (onoff && dvb_usb_af9005_led)
826 temp = 1;
827 else
828 temp = 0;
829 if (st->led_state != temp) {
830 ret =
831 af9005_write_register_bits(d, xd_p_reg_top_locken1,
832 reg_top_locken1_pos,
833 reg_top_locken1_len, temp);
834 if (ret)
835 return ret;
836 ret =
837 af9005_write_register_bits(d, xd_p_reg_top_lock1,
838 reg_top_lock1_pos,
839 reg_top_lock1_len, temp);
840 if (ret)
841 return ret;
842 st->led_state = temp;
844 return 0;
847 static int af9005_frontend_attach(struct dvb_usb_adapter *adap)
849 u8 buf[8];
850 int i;
852 /* without these calls the first commands after downloading
853 the firmware fail. I put these calls here to simulate
854 what it is done in dvb-usb-init.c.
856 struct usb_device *udev = adap->dev->udev;
857 usb_clear_halt(udev, usb_sndbulkpipe(udev, 2));
858 usb_clear_halt(udev, usb_rcvbulkpipe(udev, 1));
859 if (dvb_usb_af9005_dump_eeprom) {
860 printk("EEPROM DUMP\n");
861 for (i = 0; i < 255; i += 8) {
862 af9005_read_eeprom(adap->dev, i, buf, 8);
863 printk("ADDR %x ", i);
864 debug_dump(buf, 8, printk);
867 adap->fe = af9005_fe_attach(adap->dev);
868 return 0;
871 static int af9005_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
873 struct af9005_device_state *st = d->priv;
874 int ret, len;
876 u8 obuf[5];
877 u8 ibuf[256];
879 *state = REMOTE_NO_KEY_PRESSED;
880 if (rc_decode == NULL) {
881 /* it shouldn't never come here */
882 return 0;
884 /* deb_info("rc_query\n"); */
885 obuf[0] = 3; /* rest of packet length low */
886 obuf[1] = 0; /* rest of packet lentgh high */
887 obuf[2] = 0x40; /* read remote */
888 obuf[3] = 1; /* rest of packet length */
889 obuf[4] = st->sequence++; /* sequence number */
890 ret = af9005_usb_generic_rw(d, obuf, 5, ibuf, 256, 0);
891 if (ret) {
892 err("rc query failed");
893 return ret;
895 if (ibuf[2] != 0x41) {
896 err("rc query bad header.");
897 return -EIO;
899 if (ibuf[4] != obuf[4]) {
900 err("rc query bad sequence.");
901 return -EIO;
903 len = ibuf[5];
904 if (len > 246) {
905 err("rc query invalid length");
906 return -EIO;
908 if (len > 0) {
909 deb_rc("rc data (%d) ", len);
910 debug_dump((ibuf + 6), len, deb_rc);
911 ret = rc_decode(d, &ibuf[6], len, event, state);
912 if (ret) {
913 err("rc_decode failed");
914 return ret;
915 } else {
916 deb_rc("rc_decode state %x event %x\n", *state, *event);
917 if (*state == REMOTE_KEY_REPEAT)
918 *event = d->last_event;
921 return 0;
924 static int af9005_power_ctrl(struct dvb_usb_device *d, int onoff)
927 return 0;
930 static int af9005_pid_filter_control(struct dvb_usb_adapter *adap, int onoff)
932 int ret;
933 deb_info("pid filter control onoff %d\n", onoff);
934 if (onoff) {
935 ret =
936 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 1);
937 if (ret)
938 return ret;
939 ret =
940 af9005_write_register_bits(adap->dev,
941 XD_MP2IF_DMX_CTRL, 1, 1, 1);
942 if (ret)
943 return ret;
944 ret =
945 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 1);
946 } else
947 ret =
948 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 0);
949 if (ret)
950 return ret;
951 deb_info("pid filter control ok\n");
952 return 0;
955 static int af9005_pid_filter(struct dvb_usb_adapter *adap, int index,
956 u16 pid, int onoff)
958 u8 cmd = index & 0x1f;
959 int ret;
960 deb_info("set pid filter, index %d, pid %x, onoff %d\n", index,
961 pid, onoff);
962 if (onoff) {
963 /* cannot use it as pid_filter_ctrl since it has to be done
964 before setting the first pid */
965 if (adap->feedcount == 1) {
966 deb_info("first pid set, enable pid table\n");
967 ret = af9005_pid_filter_control(adap, onoff);
968 if (ret)
969 return ret;
971 ret =
972 af9005_write_ofdm_register(adap->dev,
973 XD_MP2IF_PID_DATA_L,
974 (u8) (pid & 0xff));
975 if (ret)
976 return ret;
977 ret =
978 af9005_write_ofdm_register(adap->dev,
979 XD_MP2IF_PID_DATA_H,
980 (u8) (pid >> 8));
981 if (ret)
982 return ret;
983 cmd |= 0x20 | 0x40;
984 } else {
985 if (adap->feedcount == 0) {
986 deb_info("last pid unset, disable pid table\n");
987 ret = af9005_pid_filter_control(adap, onoff);
988 if (ret)
989 return ret;
992 ret = af9005_write_ofdm_register(adap->dev, XD_MP2IF_PID_IDX, cmd);
993 if (ret)
994 return ret;
995 deb_info("set pid ok\n");
996 return 0;
999 static int af9005_identify_state(struct usb_device *udev,
1000 struct dvb_usb_device_properties *props,
1001 struct dvb_usb_device_description **desc,
1002 int *cold)
1004 int ret;
1005 u8 reply;
1006 ret = af9005_boot_packet(udev, FW_CONFIG, &reply);
1007 if (ret)
1008 return ret;
1009 deb_info("result of FW_CONFIG in identify state %d\n", reply);
1010 if (reply == 0x01)
1011 *cold = 1;
1012 else if (reply == 0x02)
1013 *cold = 0;
1014 else
1015 return -EIO;
1016 deb_info("Identify state cold = %d\n", *cold);
1017 return 0;
1020 static struct dvb_usb_device_properties af9005_properties;
1022 static int af9005_usb_probe(struct usb_interface *intf,
1023 const struct usb_device_id *id)
1025 return dvb_usb_device_init(intf, &af9005_properties,
1026 THIS_MODULE, NULL, adapter_nr);
1029 static struct usb_device_id af9005_usb_table[] = {
1030 {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9005)},
1031 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE)},
1032 {USB_DEVICE(USB_VID_ANSONIC, USB_PID_ANSONIC_DVBT_USB)},
1033 {0},
1036 MODULE_DEVICE_TABLE(usb, af9005_usb_table);
1038 static struct dvb_usb_device_properties af9005_properties = {
1039 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1041 .usb_ctrl = DEVICE_SPECIFIC,
1042 .firmware = "af9005.fw",
1043 .download_firmware = af9005_download_firmware,
1044 .no_reconnect = 1,
1046 .size_of_priv = sizeof(struct af9005_device_state),
1048 .num_adapters = 1,
1049 .adapter = {
1051 .caps =
1052 DVB_USB_ADAP_HAS_PID_FILTER |
1053 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1054 .pid_filter_count = 32,
1055 .pid_filter = af9005_pid_filter,
1056 /* .pid_filter_ctrl = af9005_pid_filter_control, */
1057 .frontend_attach = af9005_frontend_attach,
1058 /* .tuner_attach = af9005_tuner_attach, */
1059 /* parameter for the MPEG2-data transfer */
1060 .stream = {
1061 .type = USB_BULK,
1062 .count = 10,
1063 .endpoint = 0x04,
1064 .u = {
1065 .bulk = {
1066 .buffersize = 4096, /* actual size seen is 3948 */
1072 .power_ctrl = af9005_power_ctrl,
1073 .identify_state = af9005_identify_state,
1075 .i2c_algo = &af9005_i2c_algo,
1077 .rc_interval = 200,
1078 .rc_key_map = NULL,
1079 .rc_key_map_size = 0,
1080 .rc_query = af9005_rc_query,
1082 .num_device_descs = 3,
1083 .devices = {
1084 {.name = "Afatech DVB-T USB1.1 stick",
1085 .cold_ids = {&af9005_usb_table[0], NULL},
1086 .warm_ids = {NULL},
1088 {.name = "TerraTec Cinergy T USB XE",
1089 .cold_ids = {&af9005_usb_table[1], NULL},
1090 .warm_ids = {NULL},
1092 {.name = "Ansonic DVB-T USB1.1 stick",
1093 .cold_ids = {&af9005_usb_table[2], NULL},
1094 .warm_ids = {NULL},
1096 {NULL},
1100 /* usb specific object needed to register this driver with the usb subsystem */
1101 static struct usb_driver af9005_usb_driver = {
1102 .name = "dvb_usb_af9005",
1103 .probe = af9005_usb_probe,
1104 .disconnect = dvb_usb_device_exit,
1105 .id_table = af9005_usb_table,
1108 /* module stuff */
1109 static int __init af9005_usb_module_init(void)
1111 int result;
1112 if ((result = usb_register(&af9005_usb_driver))) {
1113 err("usb_register failed. (%d)", result);
1114 return result;
1116 rc_decode = symbol_request(af9005_rc_decode);
1117 rc_keys = symbol_request(af9005_rc_keys);
1118 rc_keys_size = symbol_request(af9005_rc_keys_size);
1119 if (rc_decode == NULL || rc_keys == NULL || rc_keys_size == NULL) {
1120 err("af9005_rc_decode function not found, disabling remote");
1121 af9005_properties.rc_query = NULL;
1122 } else {
1123 af9005_properties.rc_key_map = rc_keys;
1124 af9005_properties.rc_key_map_size = *rc_keys_size;
1127 return 0;
1130 static void __exit af9005_usb_module_exit(void)
1132 /* release rc decode symbols */
1133 if (rc_decode != NULL)
1134 symbol_put(af9005_rc_decode);
1135 if (rc_keys != NULL)
1136 symbol_put(af9005_rc_keys);
1137 if (rc_keys_size != NULL)
1138 symbol_put(af9005_rc_keys_size);
1139 /* deregister this driver from the USB subsystem */
1140 usb_deregister(&af9005_usb_driver);
1143 module_init(af9005_usb_module_init);
1144 module_exit(af9005_usb_module_exit);
1146 MODULE_AUTHOR("Luca Olivetti <luca@ventoso.org>");
1147 MODULE_DESCRIPTION("Driver for Afatech 9005 DVB-T USB1.1 stick");
1148 MODULE_VERSION("1.0");
1149 MODULE_LICENSE("GPL");