[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / media / dvb / dvb-usb / af9005.c
blobca5a0a4d2a47c9ba9bc21ce4ce2173665eac66b4
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 static int dvb_usb_af9005_dump_eeprom;
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 static int (*rc_decode) (struct dvb_usb_device *d, u8 *data, int len,
46 u32 *event, int *state);
47 static void *rc_keys;
48 static 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 static 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 static int af9005_generic_read_write(struct dvb_usb_device *d, u16 reg,
102 int readwrite, int type, u8 * values, int len)
104 struct af9005_device_state *st = d->priv;
105 u8 obuf[16] = { 0 };
106 u8 ibuf[17] = { 0 };
107 u8 command;
108 int i;
109 int ret;
111 if (len < 1) {
112 err("generic read/write, less than 1 byte. Makes no sense.");
113 return -EINVAL;
115 if (len > 8) {
116 err("generic read/write, more than 8 bytes. Not supported.");
117 return -EINVAL;
120 obuf[0] = 14; /* rest of buffer length low */
121 obuf[1] = 0; /* rest of buffer length high */
123 obuf[2] = AF9005_REGISTER_RW; /* register operation */
124 obuf[3] = 12; /* rest of buffer length */
126 obuf[4] = st->sequence++; /* sequence number */
128 obuf[5] = (u8) (reg >> 8); /* register address */
129 obuf[6] = (u8) (reg & 0xff);
131 if (type == AF9005_OFDM_REG) {
132 command = AF9005_CMD_OFDM_REG;
133 } else {
134 command = AF9005_CMD_TUNER;
137 if (len > 1)
138 command |=
139 AF9005_CMD_BURST | AF9005_CMD_AUTOINC | (len - 1) << 3;
140 command |= readwrite;
141 if (readwrite == AF9005_CMD_WRITE)
142 for (i = 0; i < len; i++)
143 obuf[8 + i] = values[i];
144 else if (type == AF9005_TUNER_REG)
145 /* read command for tuner, the first byte contains the i2c address */
146 obuf[8] = values[0];
147 obuf[7] = command;
149 ret = af9005_usb_generic_rw(d, obuf, 16, ibuf, 17, 0);
150 if (ret)
151 return ret;
153 /* sanity check */
154 if (ibuf[2] != AF9005_REGISTER_RW_ACK) {
155 err("generic read/write, wrong reply code.");
156 return -EIO;
158 if (ibuf[3] != 0x0d) {
159 err("generic read/write, wrong length in reply.");
160 return -EIO;
162 if (ibuf[4] != obuf[4]) {
163 err("generic read/write, wrong sequence in reply.");
164 return -EIO;
167 Windows driver doesn't check these fields, in fact sometimes
168 the register in the reply is different that what has been sent
170 if (ibuf[5] != obuf[5] || ibuf[6] != obuf[6]) {
171 err("generic read/write, wrong register in reply.");
172 return -EIO;
174 if (ibuf[7] != command) {
175 err("generic read/write wrong command in reply.");
176 return -EIO;
179 if (ibuf[16] != 0x01) {
180 err("generic read/write wrong status code in reply.");
181 return -EIO;
183 if (readwrite == AF9005_CMD_READ)
184 for (i = 0; i < len; i++)
185 values[i] = ibuf[8 + i];
187 return 0;
191 int af9005_read_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 * value)
193 int ret;
194 deb_reg("read register %x ", reg);
195 ret = af9005_generic_read_write(d, reg,
196 AF9005_CMD_READ, AF9005_OFDM_REG,
197 value, 1);
198 if (ret)
199 deb_reg("failed\n");
200 else
201 deb_reg("value %x\n", *value);
202 return ret;
205 int af9005_read_ofdm_registers(struct dvb_usb_device *d, u16 reg,
206 u8 * values, int len)
208 int ret;
209 deb_reg("read %d registers %x ", len, reg);
210 ret = af9005_generic_read_write(d, reg,
211 AF9005_CMD_READ, AF9005_OFDM_REG,
212 values, len);
213 if (ret)
214 deb_reg("failed\n");
215 else
216 debug_dump(values, len, deb_reg);
217 return ret;
220 int af9005_write_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 value)
222 int ret;
223 u8 temp = value;
224 deb_reg("write register %x value %x ", reg, value);
225 ret = af9005_generic_read_write(d, reg,
226 AF9005_CMD_WRITE, AF9005_OFDM_REG,
227 &temp, 1);
228 if (ret)
229 deb_reg("failed\n");
230 else
231 deb_reg("ok\n");
232 return ret;
235 int af9005_write_ofdm_registers(struct dvb_usb_device *d, u16 reg,
236 u8 * values, int len)
238 int ret;
239 deb_reg("write %d registers %x values ", len, reg);
240 debug_dump(values, len, deb_reg);
242 ret = af9005_generic_read_write(d, reg,
243 AF9005_CMD_WRITE, AF9005_OFDM_REG,
244 values, len);
245 if (ret)
246 deb_reg("failed\n");
247 else
248 deb_reg("ok\n");
249 return ret;
252 int af9005_read_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos,
253 u8 len, u8 * value)
255 u8 temp;
256 int ret;
257 deb_reg("read bits %x %x %x", reg, pos, len);
258 ret = af9005_read_ofdm_register(d, reg, &temp);
259 if (ret) {
260 deb_reg(" failed\n");
261 return ret;
263 *value = (temp >> pos) & regmask[len - 1];
264 deb_reg(" value %x\n", *value);
265 return 0;
269 int af9005_write_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos,
270 u8 len, u8 value)
272 u8 temp, mask;
273 int ret;
274 deb_reg("write bits %x %x %x value %x\n", reg, pos, len, value);
275 if (pos == 0 && len == 8)
276 return af9005_write_ofdm_register(d, reg, value);
277 ret = af9005_read_ofdm_register(d, reg, &temp);
278 if (ret)
279 return ret;
280 mask = regmask[len - 1] << pos;
281 temp = (temp & ~mask) | ((value << pos) & mask);
282 return af9005_write_ofdm_register(d, reg, temp);
286 static int af9005_usb_read_tuner_registers(struct dvb_usb_device *d,
287 u16 reg, u8 * values, int len)
289 return af9005_generic_read_write(d, reg,
290 AF9005_CMD_READ, AF9005_TUNER_REG,
291 values, len);
294 static int af9005_usb_write_tuner_registers(struct dvb_usb_device *d,
295 u16 reg, u8 * values, int len)
297 return af9005_generic_read_write(d, reg,
298 AF9005_CMD_WRITE,
299 AF9005_TUNER_REG, values, len);
302 int af9005_write_tuner_registers(struct dvb_usb_device *d, u16 reg,
303 u8 * values, int len)
305 /* don't let the name of this function mislead you: it's just used
306 as an interface from the firmware to the i2c bus. The actual
307 i2c addresses are contained in the data */
308 int ret, i, done = 0, fail = 0;
309 u8 temp;
310 ret = af9005_usb_write_tuner_registers(d, reg, values, len);
311 if (ret)
312 return ret;
313 if (reg != 0xffff) {
314 /* check if write done (0xa40d bit 1) or fail (0xa40d bit 2) */
315 for (i = 0; i < 200; i++) {
316 ret =
317 af9005_read_ofdm_register(d,
318 xd_I2C_i2c_m_status_wdat_done,
319 &temp);
320 if (ret)
321 return ret;
322 done = temp & (regmask[i2c_m_status_wdat_done_len - 1]
323 << i2c_m_status_wdat_done_pos);
324 if (done)
325 break;
326 fail = temp & (regmask[i2c_m_status_wdat_fail_len - 1]
327 << i2c_m_status_wdat_fail_pos);
328 if (fail)
329 break;
330 msleep(50);
332 if (i == 200)
333 return -ETIMEDOUT;
334 if (fail) {
335 /* clear write fail bit */
336 af9005_write_register_bits(d,
337 xd_I2C_i2c_m_status_wdat_fail,
338 i2c_m_status_wdat_fail_pos,
339 i2c_m_status_wdat_fail_len,
341 return -EIO;
343 /* clear write done bit */
344 ret =
345 af9005_write_register_bits(d,
346 xd_I2C_i2c_m_status_wdat_fail,
347 i2c_m_status_wdat_done_pos,
348 i2c_m_status_wdat_done_len, 1);
349 if (ret)
350 return ret;
352 return 0;
355 int af9005_read_tuner_registers(struct dvb_usb_device *d, u16 reg, u8 addr,
356 u8 * values, int len)
358 /* don't let the name of this function mislead you: it's just used
359 as an interface from the firmware to the i2c bus. The actual
360 i2c addresses are contained in the data */
361 int ret, i;
362 u8 temp, buf[2];
364 buf[0] = addr; /* tuner i2c address */
365 buf[1] = values[0]; /* tuner register */
367 values[0] = addr + 0x01; /* i2c read address */
369 if (reg == APO_REG_I2C_RW_SILICON_TUNER) {
370 /* write tuner i2c address to tuner, 0c00c0 undocumented, found by sniffing */
371 ret = af9005_write_tuner_registers(d, 0x00c0, buf, 2);
372 if (ret)
373 return ret;
376 /* send read command to ofsm */
377 ret = af9005_usb_read_tuner_registers(d, reg, values, 1);
378 if (ret)
379 return ret;
381 /* check if read done */
382 for (i = 0; i < 200; i++) {
383 ret = af9005_read_ofdm_register(d, 0xa408, &temp);
384 if (ret)
385 return ret;
386 if (temp & 0x01)
387 break;
388 msleep(50);
390 if (i == 200)
391 return -ETIMEDOUT;
393 /* clear read done bit (by writing 1) */
394 ret = af9005_write_ofdm_register(d, xd_I2C_i2c_m_data8, 1);
395 if (ret)
396 return ret;
398 /* get read data (available from 0xa400) */
399 for (i = 0; i < len; i++) {
400 ret = af9005_read_ofdm_register(d, 0xa400 + i, &temp);
401 if (ret)
402 return ret;
403 values[i] = temp;
405 return 0;
408 static int af9005_i2c_write(struct dvb_usb_device *d, u8 i2caddr, u8 reg,
409 u8 * data, int len)
411 int ret, i;
412 u8 buf[3];
413 deb_i2c("i2c_write i2caddr %x, reg %x, len %d data ", i2caddr,
414 reg, len);
415 debug_dump(data, len, deb_i2c);
417 for (i = 0; i < len; i++) {
418 buf[0] = i2caddr;
419 buf[1] = reg + (u8) i;
420 buf[2] = data[i];
421 ret =
422 af9005_write_tuner_registers(d,
423 APO_REG_I2C_RW_SILICON_TUNER,
424 buf, 3);
425 if (ret) {
426 deb_i2c("i2c_write failed\n");
427 return ret;
430 deb_i2c("i2c_write ok\n");
431 return 0;
434 static int af9005_i2c_read(struct dvb_usb_device *d, u8 i2caddr, u8 reg,
435 u8 * data, int len)
437 int ret, i;
438 u8 temp;
439 deb_i2c("i2c_read i2caddr %x, reg %x, len %d\n ", i2caddr, reg, len);
440 for (i = 0; i < len; i++) {
441 temp = reg + i;
442 ret =
443 af9005_read_tuner_registers(d,
444 APO_REG_I2C_RW_SILICON_TUNER,
445 i2caddr, &temp, 1);
446 if (ret) {
447 deb_i2c("i2c_read failed\n");
448 return ret;
450 data[i] = temp;
452 deb_i2c("i2c data read: ");
453 debug_dump(data, len, deb_i2c);
454 return 0;
457 static int af9005_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
458 int num)
460 /* only implements what the mt2060 module does, don't know how
461 to make it really generic */
462 struct dvb_usb_device *d = i2c_get_adapdata(adap);
463 int ret;
464 u8 reg, addr;
465 u8 *value;
467 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
468 return -EAGAIN;
470 if (num > 2)
471 warn("more than 2 i2c messages at a time is not handled yet. TODO.");
473 if (num == 2) {
474 /* reads a single register */
475 reg = *msg[0].buf;
476 addr = msg[0].addr;
477 value = msg[1].buf;
478 ret = af9005_i2c_read(d, addr, reg, value, 1);
479 if (ret == 0)
480 ret = 2;
481 } else {
482 /* write one or more registers */
483 reg = msg[0].buf[0];
484 addr = msg[0].addr;
485 value = &msg[0].buf[1];
486 ret = af9005_i2c_write(d, addr, reg, value, msg[0].len - 1);
487 if (ret == 0)
488 ret = 1;
491 mutex_unlock(&d->i2c_mutex);
492 return ret;
495 static u32 af9005_i2c_func(struct i2c_adapter *adapter)
497 return I2C_FUNC_I2C;
500 static struct i2c_algorithm af9005_i2c_algo = {
501 .master_xfer = af9005_i2c_xfer,
502 .functionality = af9005_i2c_func,
505 int af9005_send_command(struct dvb_usb_device *d, u8 command, u8 * wbuf,
506 int wlen, u8 * rbuf, int rlen)
508 struct af9005_device_state *st = d->priv;
510 int ret, i, packet_len;
511 u8 buf[64];
512 u8 ibuf[64];
514 if (wlen < 0) {
515 err("send command, wlen less than 0 bytes. Makes no sense.");
516 return -EINVAL;
518 if (wlen > 54) {
519 err("send command, wlen more than 54 bytes. Not supported.");
520 return -EINVAL;
522 if (rlen > 54) {
523 err("send command, rlen more than 54 bytes. Not supported.");
524 return -EINVAL;
526 packet_len = wlen + 5;
527 buf[0] = (u8) (packet_len & 0xff);
528 buf[1] = (u8) ((packet_len & 0xff00) >> 8);
530 buf[2] = 0x26; /* packet type */
531 buf[3] = wlen + 3;
532 buf[4] = st->sequence++;
533 buf[5] = command;
534 buf[6] = wlen;
535 for (i = 0; i < wlen; i++)
536 buf[7 + i] = wbuf[i];
537 ret = af9005_usb_generic_rw(d, buf, wlen + 7, ibuf, rlen + 7, 0);
538 if (ret)
539 return ret;
540 if (ibuf[2] != 0x27) {
541 err("send command, wrong reply code.");
542 return -EIO;
544 if (ibuf[4] != buf[4]) {
545 err("send command, wrong sequence in reply.");
546 return -EIO;
548 if (ibuf[5] != 0x01) {
549 err("send command, wrong status code in reply.");
550 return -EIO;
552 if (ibuf[6] != rlen) {
553 err("send command, invalid data length in reply.");
554 return -EIO;
556 for (i = 0; i < rlen; i++)
557 rbuf[i] = ibuf[i + 7];
558 return 0;
561 int af9005_read_eeprom(struct dvb_usb_device *d, u8 address, u8 * values,
562 int len)
564 struct af9005_device_state *st = d->priv;
565 u8 obuf[16], ibuf[14];
566 int ret, i;
568 memset(obuf, 0, sizeof(obuf));
569 memset(ibuf, 0, sizeof(ibuf));
571 obuf[0] = 14; /* length of rest of packet low */
572 obuf[1] = 0; /* length of rest of packer high */
574 obuf[2] = 0x2a; /* read/write eeprom */
576 obuf[3] = 12; /* size */
578 obuf[4] = st->sequence++;
580 obuf[5] = 0; /* read */
582 obuf[6] = len;
583 obuf[7] = address;
584 ret = af9005_usb_generic_rw(d, obuf, 16, ibuf, 14, 0);
585 if (ret)
586 return ret;
587 if (ibuf[2] != 0x2b) {
588 err("Read eeprom, invalid reply code");
589 return -EIO;
591 if (ibuf[3] != 10) {
592 err("Read eeprom, invalid reply length");
593 return -EIO;
595 if (ibuf[4] != obuf[4]) {
596 err("Read eeprom, wrong sequence in reply ");
597 return -EIO;
599 if (ibuf[5] != 1) {
600 err("Read eeprom, wrong status in reply ");
601 return -EIO;
603 for (i = 0; i < len; i++) {
604 values[i] = ibuf[6 + i];
606 return 0;
609 static int af9005_boot_packet(struct usb_device *udev, int type, u8 * reply)
611 u8 buf[FW_BULKOUT_SIZE + 2];
612 u16 checksum;
613 int act_len, i, ret;
614 memset(buf, 0, sizeof(buf));
615 buf[0] = (u8) (FW_BULKOUT_SIZE & 0xff);
616 buf[1] = (u8) ((FW_BULKOUT_SIZE >> 8) & 0xff);
617 switch (type) {
618 case FW_CONFIG:
619 buf[2] = 0x11;
620 buf[3] = 0x04;
621 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
622 buf[5] = 0x03;
623 checksum = buf[4] + buf[5];
624 buf[6] = (u8) ((checksum >> 8) & 0xff);
625 buf[7] = (u8) (checksum & 0xff);
626 break;
627 case FW_CONFIRM:
628 buf[2] = 0x11;
629 buf[3] = 0x04;
630 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
631 buf[5] = 0x01;
632 checksum = buf[4] + buf[5];
633 buf[6] = (u8) ((checksum >> 8) & 0xff);
634 buf[7] = (u8) (checksum & 0xff);
635 break;
636 case FW_BOOT:
637 buf[2] = 0x10;
638 buf[3] = 0x08;
639 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
640 buf[5] = 0x97;
641 buf[6] = 0xaa;
642 buf[7] = 0x55;
643 buf[8] = 0xa5;
644 buf[9] = 0x5a;
645 checksum = 0;
646 for (i = 4; i <= 9; i++)
647 checksum += buf[i];
648 buf[10] = (u8) ((checksum >> 8) & 0xff);
649 buf[11] = (u8) (checksum & 0xff);
650 break;
651 default:
652 err("boot packet invalid boot packet type");
653 return -EINVAL;
655 deb_fw(">>> ");
656 debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw);
658 ret = usb_bulk_msg(udev,
659 usb_sndbulkpipe(udev, 0x02),
660 buf, FW_BULKOUT_SIZE + 2, &act_len, 2000);
661 if (ret)
662 err("boot packet bulk message failed: %d (%d/%d)", ret,
663 FW_BULKOUT_SIZE + 2, act_len);
664 else
665 ret = act_len != FW_BULKOUT_SIZE + 2 ? -1 : 0;
666 if (ret)
667 return ret;
668 memset(buf, 0, 9);
669 ret = usb_bulk_msg(udev,
670 usb_rcvbulkpipe(udev, 0x01), buf, 9, &act_len, 2000);
671 if (ret) {
672 err("boot packet recv bulk message failed: %d", ret);
673 return ret;
675 deb_fw("<<< ");
676 debug_dump(buf, act_len, deb_fw);
677 checksum = 0;
678 switch (type) {
679 case FW_CONFIG:
680 if (buf[2] != 0x11) {
681 err("boot bad config header.");
682 return -EIO;
684 if (buf[3] != 0x05) {
685 err("boot bad config size.");
686 return -EIO;
688 if (buf[4] != 0x00) {
689 err("boot bad config sequence.");
690 return -EIO;
692 if (buf[5] != 0x04) {
693 err("boot bad config subtype.");
694 return -EIO;
696 for (i = 4; i <= 6; i++)
697 checksum += buf[i];
698 if (buf[7] * 256 + buf[8] != checksum) {
699 err("boot bad config checksum.");
700 return -EIO;
702 *reply = buf[6];
703 break;
704 case FW_CONFIRM:
705 if (buf[2] != 0x11) {
706 err("boot bad confirm header.");
707 return -EIO;
709 if (buf[3] != 0x05) {
710 err("boot bad confirm size.");
711 return -EIO;
713 if (buf[4] != 0x00) {
714 err("boot bad confirm sequence.");
715 return -EIO;
717 if (buf[5] != 0x02) {
718 err("boot bad confirm subtype.");
719 return -EIO;
721 for (i = 4; i <= 6; i++)
722 checksum += buf[i];
723 if (buf[7] * 256 + buf[8] != checksum) {
724 err("boot bad confirm checksum.");
725 return -EIO;
727 *reply = buf[6];
728 break;
729 case FW_BOOT:
730 if (buf[2] != 0x10) {
731 err("boot bad boot header.");
732 return -EIO;
734 if (buf[3] != 0x05) {
735 err("boot bad boot size.");
736 return -EIO;
738 if (buf[4] != 0x00) {
739 err("boot bad boot sequence.");
740 return -EIO;
742 if (buf[5] != 0x01) {
743 err("boot bad boot pattern 01.");
744 return -EIO;
746 if (buf[6] != 0x10) {
747 err("boot bad boot pattern 10.");
748 return -EIO;
750 for (i = 4; i <= 6; i++)
751 checksum += buf[i];
752 if (buf[7] * 256 + buf[8] != checksum) {
753 err("boot bad boot checksum.");
754 return -EIO;
756 break;
760 return 0;
763 static int af9005_download_firmware(struct usb_device *udev, const struct firmware *fw)
765 int i, packets, ret, act_len;
767 u8 buf[FW_BULKOUT_SIZE + 2];
768 u8 reply;
770 ret = af9005_boot_packet(udev, FW_CONFIG, &reply);
771 if (ret)
772 return ret;
773 if (reply != 0x01) {
774 err("before downloading firmware, FW_CONFIG expected 0x01, received 0x%x", reply);
775 return -EIO;
777 packets = fw->size / FW_BULKOUT_SIZE;
778 buf[0] = (u8) (FW_BULKOUT_SIZE & 0xff);
779 buf[1] = (u8) ((FW_BULKOUT_SIZE >> 8) & 0xff);
780 for (i = 0; i < packets; i++) {
781 memcpy(&buf[2], fw->data + i * FW_BULKOUT_SIZE,
782 FW_BULKOUT_SIZE);
783 deb_fw(">>> ");
784 debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw);
785 ret = usb_bulk_msg(udev,
786 usb_sndbulkpipe(udev, 0x02),
787 buf, FW_BULKOUT_SIZE + 2, &act_len, 1000);
788 if (ret) {
789 err("firmware download failed at packet %d with code %d", i, ret);
790 return ret;
793 ret = af9005_boot_packet(udev, FW_CONFIRM, &reply);
794 if (ret)
795 return ret;
796 if (reply != (u8) (packets & 0xff)) {
797 err("after downloading firmware, FW_CONFIRM expected 0x%x, received 0x%x", packets & 0xff, reply);
798 return -EIO;
800 ret = af9005_boot_packet(udev, FW_BOOT, &reply);
801 if (ret)
802 return ret;
803 ret = af9005_boot_packet(udev, FW_CONFIG, &reply);
804 if (ret)
805 return ret;
806 if (reply != 0x02) {
807 err("after downloading firmware, FW_CONFIG expected 0x02, received 0x%x", reply);
808 return -EIO;
811 return 0;
815 int af9005_led_control(struct dvb_usb_device *d, int onoff)
817 struct af9005_device_state *st = d->priv;
818 int temp, ret;
820 if (onoff && dvb_usb_af9005_led)
821 temp = 1;
822 else
823 temp = 0;
824 if (st->led_state != temp) {
825 ret =
826 af9005_write_register_bits(d, xd_p_reg_top_locken1,
827 reg_top_locken1_pos,
828 reg_top_locken1_len, temp);
829 if (ret)
830 return ret;
831 ret =
832 af9005_write_register_bits(d, xd_p_reg_top_lock1,
833 reg_top_lock1_pos,
834 reg_top_lock1_len, temp);
835 if (ret)
836 return ret;
837 st->led_state = temp;
839 return 0;
842 static int af9005_frontend_attach(struct dvb_usb_adapter *adap)
844 u8 buf[8];
845 int i;
847 /* without these calls the first commands after downloading
848 the firmware fail. I put these calls here to simulate
849 what it is done in dvb-usb-init.c.
851 struct usb_device *udev = adap->dev->udev;
852 usb_clear_halt(udev, usb_sndbulkpipe(udev, 2));
853 usb_clear_halt(udev, usb_rcvbulkpipe(udev, 1));
854 if (dvb_usb_af9005_dump_eeprom) {
855 printk("EEPROM DUMP\n");
856 for (i = 0; i < 255; i += 8) {
857 af9005_read_eeprom(adap->dev, i, buf, 8);
858 printk("ADDR %x ", i);
859 debug_dump(buf, 8, printk);
862 adap->fe = af9005_fe_attach(adap->dev);
863 return 0;
866 static int af9005_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
868 struct af9005_device_state *st = d->priv;
869 int ret, len;
871 u8 obuf[5];
872 u8 ibuf[256];
874 *state = REMOTE_NO_KEY_PRESSED;
875 if (rc_decode == NULL) {
876 /* it shouldn't never come here */
877 return 0;
879 /* deb_info("rc_query\n"); */
880 obuf[0] = 3; /* rest of packet length low */
881 obuf[1] = 0; /* rest of packet lentgh high */
882 obuf[2] = 0x40; /* read remote */
883 obuf[3] = 1; /* rest of packet length */
884 obuf[4] = st->sequence++; /* sequence number */
885 ret = af9005_usb_generic_rw(d, obuf, 5, ibuf, 256, 0);
886 if (ret) {
887 err("rc query failed");
888 return ret;
890 if (ibuf[2] != 0x41) {
891 err("rc query bad header.");
892 return -EIO;
894 if (ibuf[4] != obuf[4]) {
895 err("rc query bad sequence.");
896 return -EIO;
898 len = ibuf[5];
899 if (len > 246) {
900 err("rc query invalid length");
901 return -EIO;
903 if (len > 0) {
904 deb_rc("rc data (%d) ", len);
905 debug_dump((ibuf + 6), len, deb_rc);
906 ret = rc_decode(d, &ibuf[6], len, event, state);
907 if (ret) {
908 err("rc_decode failed");
909 return ret;
910 } else {
911 deb_rc("rc_decode state %x event %x\n", *state, *event);
912 if (*state == REMOTE_KEY_REPEAT)
913 *event = d->last_event;
916 return 0;
919 static int af9005_power_ctrl(struct dvb_usb_device *d, int onoff)
922 return 0;
925 static int af9005_pid_filter_control(struct dvb_usb_adapter *adap, int onoff)
927 int ret;
928 deb_info("pid filter control onoff %d\n", onoff);
929 if (onoff) {
930 ret =
931 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 1);
932 if (ret)
933 return ret;
934 ret =
935 af9005_write_register_bits(adap->dev,
936 XD_MP2IF_DMX_CTRL, 1, 1, 1);
937 if (ret)
938 return ret;
939 ret =
940 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 1);
941 } else
942 ret =
943 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 0);
944 if (ret)
945 return ret;
946 deb_info("pid filter control ok\n");
947 return 0;
950 static int af9005_pid_filter(struct dvb_usb_adapter *adap, int index,
951 u16 pid, int onoff)
953 u8 cmd = index & 0x1f;
954 int ret;
955 deb_info("set pid filter, index %d, pid %x, onoff %d\n", index,
956 pid, onoff);
957 if (onoff) {
958 /* cannot use it as pid_filter_ctrl since it has to be done
959 before setting the first pid */
960 if (adap->feedcount == 1) {
961 deb_info("first pid set, enable pid table\n");
962 ret = af9005_pid_filter_control(adap, onoff);
963 if (ret)
964 return ret;
966 ret =
967 af9005_write_ofdm_register(adap->dev,
968 XD_MP2IF_PID_DATA_L,
969 (u8) (pid & 0xff));
970 if (ret)
971 return ret;
972 ret =
973 af9005_write_ofdm_register(adap->dev,
974 XD_MP2IF_PID_DATA_H,
975 (u8) (pid >> 8));
976 if (ret)
977 return ret;
978 cmd |= 0x20 | 0x40;
979 } else {
980 if (adap->feedcount == 0) {
981 deb_info("last pid unset, disable pid table\n");
982 ret = af9005_pid_filter_control(adap, onoff);
983 if (ret)
984 return ret;
987 ret = af9005_write_ofdm_register(adap->dev, XD_MP2IF_PID_IDX, cmd);
988 if (ret)
989 return ret;
990 deb_info("set pid ok\n");
991 return 0;
994 static int af9005_identify_state(struct usb_device *udev,
995 struct dvb_usb_device_properties *props,
996 struct dvb_usb_device_description **desc,
997 int *cold)
999 int ret;
1000 u8 reply;
1001 ret = af9005_boot_packet(udev, FW_CONFIG, &reply);
1002 if (ret)
1003 return ret;
1004 deb_info("result of FW_CONFIG in identify state %d\n", reply);
1005 if (reply == 0x01)
1006 *cold = 1;
1007 else if (reply == 0x02)
1008 *cold = 0;
1009 else
1010 return -EIO;
1011 deb_info("Identify state cold = %d\n", *cold);
1012 return 0;
1015 static struct dvb_usb_device_properties af9005_properties;
1017 static int af9005_usb_probe(struct usb_interface *intf,
1018 const struct usb_device_id *id)
1020 return dvb_usb_device_init(intf, &af9005_properties,
1021 THIS_MODULE, NULL, adapter_nr);
1024 static struct usb_device_id af9005_usb_table[] = {
1025 {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9005)},
1026 {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE)},
1027 {USB_DEVICE(USB_VID_ANSONIC, USB_PID_ANSONIC_DVBT_USB)},
1028 {0},
1031 MODULE_DEVICE_TABLE(usb, af9005_usb_table);
1033 static struct dvb_usb_device_properties af9005_properties = {
1034 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1036 .usb_ctrl = DEVICE_SPECIFIC,
1037 .firmware = "af9005.fw",
1038 .download_firmware = af9005_download_firmware,
1039 .no_reconnect = 1,
1041 .size_of_priv = sizeof(struct af9005_device_state),
1043 .num_adapters = 1,
1044 .adapter = {
1046 .caps =
1047 DVB_USB_ADAP_HAS_PID_FILTER |
1048 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1049 .pid_filter_count = 32,
1050 .pid_filter = af9005_pid_filter,
1051 /* .pid_filter_ctrl = af9005_pid_filter_control, */
1052 .frontend_attach = af9005_frontend_attach,
1053 /* .tuner_attach = af9005_tuner_attach, */
1054 /* parameter for the MPEG2-data transfer */
1055 .stream = {
1056 .type = USB_BULK,
1057 .count = 10,
1058 .endpoint = 0x04,
1059 .u = {
1060 .bulk = {
1061 .buffersize = 4096, /* actual size seen is 3948 */
1067 .power_ctrl = af9005_power_ctrl,
1068 .identify_state = af9005_identify_state,
1070 .i2c_algo = &af9005_i2c_algo,
1072 .rc_interval = 200,
1073 .rc_key_map = NULL,
1074 .rc_key_map_size = 0,
1075 .rc_query = af9005_rc_query,
1077 .num_device_descs = 3,
1078 .devices = {
1079 {.name = "Afatech DVB-T USB1.1 stick",
1080 .cold_ids = {&af9005_usb_table[0], NULL},
1081 .warm_ids = {NULL},
1083 {.name = "TerraTec Cinergy T USB XE",
1084 .cold_ids = {&af9005_usb_table[1], NULL},
1085 .warm_ids = {NULL},
1087 {.name = "Ansonic DVB-T USB1.1 stick",
1088 .cold_ids = {&af9005_usb_table[2], NULL},
1089 .warm_ids = {NULL},
1091 {NULL},
1095 /* usb specific object needed to register this driver with the usb subsystem */
1096 static struct usb_driver af9005_usb_driver = {
1097 .name = "dvb_usb_af9005",
1098 .probe = af9005_usb_probe,
1099 .disconnect = dvb_usb_device_exit,
1100 .id_table = af9005_usb_table,
1103 /* module stuff */
1104 static int __init af9005_usb_module_init(void)
1106 int result;
1107 if ((result = usb_register(&af9005_usb_driver))) {
1108 err("usb_register failed. (%d)", result);
1109 return result;
1111 rc_decode = symbol_request(af9005_rc_decode);
1112 rc_keys = symbol_request(af9005_rc_keys);
1113 rc_keys_size = symbol_request(af9005_rc_keys_size);
1114 if (rc_decode == NULL || rc_keys == NULL || rc_keys_size == NULL) {
1115 err("af9005_rc_decode function not found, disabling remote");
1116 af9005_properties.rc_query = NULL;
1117 } else {
1118 af9005_properties.rc_key_map = rc_keys;
1119 af9005_properties.rc_key_map_size = *rc_keys_size;
1122 return 0;
1125 static void __exit af9005_usb_module_exit(void)
1127 /* release rc decode symbols */
1128 if (rc_decode != NULL)
1129 symbol_put(af9005_rc_decode);
1130 if (rc_keys != NULL)
1131 symbol_put(af9005_rc_keys);
1132 if (rc_keys_size != NULL)
1133 symbol_put(af9005_rc_keys_size);
1134 /* deregister this driver from the USB subsystem */
1135 usb_deregister(&af9005_usb_driver);
1138 module_init(af9005_usb_module_init);
1139 module_exit(af9005_usb_module_exit);
1141 MODULE_AUTHOR("Luca Olivetti <luca@ventoso.org>");
1142 MODULE_DESCRIPTION("Driver for Afatech 9005 DVB-T USB1.1 stick");
1143 MODULE_VERSION("1.0");
1144 MODULE_LICENSE("GPL");