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