V4L/DVB (8495): usb/anysee.c: make struct anysee_usb_mutex static
[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 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
43
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;
49
50 u8 regmask[8] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
51
52 struct af9005_device_state {
53         u8 sequence;
54         int led_state;
55 };
56
57 int af9005_usb_generic_rw(struct dvb_usb_device *d, u8 * wbuf, u16 wlen,
58                           u8 * rbuf, u16 rlen, int delay_ms)
59 {
60         int actlen, ret = -ENOMEM;
61
62         if (wbuf == NULL || wlen == 0)
63                 return -EINVAL;
64
65         if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
66                 return ret;
67
68         deb_xfer(">>> ");
69         debug_dump(wbuf, wlen, deb_xfer);
70
71         ret = usb_bulk_msg(d->udev, usb_sndbulkpipe(d->udev,
72                                                     2), wbuf, wlen,
73                            &actlen, 2000);
74
75         if (ret)
76                 err("bulk message failed: %d (%d/%d)", ret, wlen, actlen);
77         else
78                 ret = actlen != wlen ? -1 : 0;
79
80         /* an answer is expected, and no error before */
81         if (!ret && rbuf && rlen) {
82                 if (delay_ms)
83                         msleep(delay_ms);
84
85                 ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev,
86                                                             0x01), rbuf,
87                                    rlen, &actlen, 2000);
88
89                 if (ret)
90                         err("recv bulk message failed: %d", ret);
91                 else {
92                         deb_xfer("<<< ");
93                         debug_dump(rbuf, actlen, deb_xfer);
94                 }
95         }
96
97         mutex_unlock(&d->usb_mutex);
98         return ret;
99 }
100
101 int af9005_usb_generic_write(struct dvb_usb_device *d, u8 * buf, u16 len)
102 {
103         return af9005_usb_generic_rw(d, buf, len, NULL, 0, 0);
104 }
105
106 int af9005_generic_read_write(struct dvb_usb_device *d, u16 reg,
107                               int readwrite, int type, u8 * values, int len)
108 {
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;
115
116         if (len < 1) {
117                 err("generic read/write, less than 1 byte. Makes no sense.");
118                 return -EINVAL;
119         }
120         if (len > 8) {
121                 err("generic read/write, more than 8 bytes. Not supported.");
122                 return -EINVAL;
123         }
124
125         obuf[0] = 14;           /* rest of buffer length low */
126         obuf[1] = 0;            /* rest of buffer length high */
127
128         obuf[2] = AF9005_REGISTER_RW;   /* register operation */
129         obuf[3] = 12;           /* rest of buffer length */
130
131         obuf[4] = st->sequence++;       /* sequence number */
132
133         obuf[5] = (u8) (reg >> 8);      /* register address */
134         obuf[6] = (u8) (reg & 0xff);
135
136         if (type == AF9005_OFDM_REG) {
137                 command = AF9005_CMD_OFDM_REG;
138         } else {
139                 command = AF9005_CMD_TUNER;
140         }
141
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;
153
154         ret = af9005_usb_generic_rw(d, obuf, 16, ibuf, 17, 0);
155         if (ret)
156                 return ret;
157
158         /* sanity check */
159         if (ibuf[2] != AF9005_REGISTER_RW_ACK) {
160                 err("generic read/write, wrong reply code.");
161                 return -EIO;
162         }
163         if (ibuf[3] != 0x0d) {
164                 err("generic read/write, wrong length in reply.");
165                 return -EIO;
166         }
167         if (ibuf[4] != obuf[4]) {
168                 err("generic read/write, wrong sequence in reply.");
169                 return -EIO;
170         }
171         /*
172            Windows driver doesn't check these fields, in fact sometimes
173            the register in the reply is different that what has been sent
174
175            if (ibuf[5] != obuf[5] || ibuf[6] != obuf[6]) {
176            err("generic read/write, wrong register in reply.");
177            return -EIO;
178            }
179            if (ibuf[7] != command) {
180            err("generic read/write wrong command in reply.");
181            return -EIO;
182            }
183          */
184         if (ibuf[16] != 0x01) {
185                 err("generic read/write wrong status code in reply.");
186                 return -EIO;
187         }
188         if (readwrite == AF9005_CMD_READ)
189                 for (i = 0; i < len; i++)
190                         values[i] = ibuf[8 + i];
191
192         return 0;
193
194 }
195
196 int af9005_read_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 * value)
197 {
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;
208 }
209
210 int af9005_read_ofdm_registers(struct dvb_usb_device *d, u16 reg,
211                                u8 * values, int len)
212 {
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;
223 }
224
225 int af9005_write_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 value)
226 {
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;
238 }
239
240 int af9005_write_ofdm_registers(struct dvb_usb_device *d, u16 reg,
241                                 u8 * values, int len)
242 {
243         int ret;
244         deb_reg("write %d registers %x values ", len, reg);
245         debug_dump(values, len, deb_reg);
246
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;
255 }
256
257 int af9005_read_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos,
258                               u8 len, u8 * value)
259 {
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;
267         }
268         *value = (temp >> pos) & regmask[len - 1];
269         deb_reg(" value %x\n", *value);
270         return 0;
271
272 }
273
274 int af9005_write_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos,
275                                u8 len, u8 value)
276 {
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);
288
289 }
290
291 static int af9005_usb_read_tuner_registers(struct dvb_usb_device *d,
292                                            u16 reg, u8 * values, int len)
293 {
294         return af9005_generic_read_write(d, reg,
295                                          AF9005_CMD_READ, AF9005_TUNER_REG,
296                                          values, len);
297 }
298
299 static int af9005_usb_write_tuner_registers(struct dvb_usb_device *d,
300                                             u16 reg, u8 * values, int len)
301 {
302         return af9005_generic_read_write(d, reg,
303                                          AF9005_CMD_WRITE,
304                                          AF9005_TUNER_REG, values, len);
305 }
306
307 int af9005_write_tuner_registers(struct dvb_usb_device *d, u16 reg,
308                                  u8 * values, int len)
309 {
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);
336                 }
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,
345                                                    1);
346                         return -EIO;
347                 }
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;
356         }
357         return 0;
358 }
359
360 int af9005_read_tuner_registers(struct dvb_usb_device *d, u16 reg, u8 addr,
361                                 u8 * values, int len)
362 {
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];
368
369         buf[0] = addr;          /* tuner i2c address */
370         buf[1] = values[0];     /* tuner register */
371
372         values[0] = addr + 0x01;        /* i2c read address */
373
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;
379         }
380
381         /* send read command to ofsm */
382         ret = af9005_usb_read_tuner_registers(d, reg, values, 1);
383         if (ret)
384                 return ret;
385
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);
394         }
395         if (i == 200)
396                 return -ETIMEDOUT;
397
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;
402
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;
409         }
410         return 0;
411 }
412
413 static int af9005_i2c_write(struct dvb_usb_device *d, u8 i2caddr, u8 reg,
414                             u8 * data, int len)
415 {
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);
421
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;
433                 }
434         }
435         deb_i2c("i2c_write ok\n");
436         return 0;
437 }
438
439 static int af9005_i2c_read(struct dvb_usb_device *d, u8 i2caddr, u8 reg,
440                            u8 * data, int len)
441 {
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;
454                 }
455                 data[i] = temp;
456         }
457         deb_i2c("i2c data read: ");
458         debug_dump(data, len, deb_i2c);
459         return 0;
460 }
461
462 static int af9005_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
463                            int num)
464 {
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;
471
472         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
473                 return -EAGAIN;
474
475         if (num > 2)
476                 warn("more than 2 i2c messages at a time is not handled yet. TODO.");
477
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;
494         }
495
496         mutex_unlock(&d->i2c_mutex);
497         return ret;
498 }
499
500 static u32 af9005_i2c_func(struct i2c_adapter *adapter)
501 {
502         return I2C_FUNC_I2C;
503 }
504
505 static struct i2c_algorithm af9005_i2c_algo = {
506         .master_xfer = af9005_i2c_xfer,
507         .functionality = af9005_i2c_func,
508 };
509
510 int af9005_send_command(struct dvb_usb_device *d, u8 command, u8 * wbuf,
511                         int wlen, u8 * rbuf, int rlen)
512 {
513         struct af9005_device_state *st = d->priv;
514
515         int ret, i, packet_len;
516         u8 buf[64];
517         u8 ibuf[64];
518
519         if (wlen < 0) {
520                 err("send command, wlen less than 0 bytes. Makes no sense.");
521                 return -EINVAL;
522         }
523         if (wlen > 54) {
524                 err("send command, wlen more than 54 bytes. Not supported.");
525                 return -EINVAL;
526         }
527         if (rlen > 54) {
528                 err("send command, rlen more than 54 bytes. Not supported.");
529                 return -EINVAL;
530         }
531         packet_len = wlen + 5;
532         buf[0] = (u8) (packet_len & 0xff);
533         buf[1] = (u8) ((packet_len & 0xff00) >> 8);
534
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;
548         }
549         if (ibuf[4] != buf[4]) {
550                 err("send command, wrong sequence in reply.");
551                 return -EIO;
552         }
553         if (ibuf[5] != 0x01) {
554                 err("send command, wrong status code in reply.");
555                 return -EIO;
556         }
557         if (ibuf[6] != rlen) {
558                 err("send command, invalid data length in reply.");
559                 return -EIO;
560         }
561         for (i = 0; i < rlen; i++)
562                 rbuf[i] = ibuf[i + 7];
563         return 0;
564 }
565
566 int af9005_read_eeprom(struct dvb_usb_device *d, u8 address, u8 * values,
567                        int len)
568 {
569         struct af9005_device_state *st = d->priv;
570         u8 obuf[16], ibuf[14];
571         int ret, i;
572
573         memset(obuf, 0, sizeof(obuf));
574         memset(ibuf, 0, sizeof(ibuf));
575
576         obuf[0] = 14;           /* length of rest of packet low */
577         obuf[1] = 0;            /* length of rest of packer high */
578
579         obuf[2] = 0x2a;         /* read/write eeprom */
580
581         obuf[3] = 12;           /* size */
582
583         obuf[4] = st->sequence++;
584
585         obuf[5] = 0;            /* read */
586
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;
595         }
596         if (ibuf[3] != 10) {
597                 err("Read eeprom, invalid reply length");
598                 return -EIO;
599         }
600         if (ibuf[4] != obuf[4]) {
601                 err("Read eeprom, wrong sequence in reply ");
602                 return -EIO;
603         }
604         if (ibuf[5] != 1) {
605                 err("Read eeprom, wrong status in reply ");
606                 return -EIO;
607         }
608         for (i = 0; i < len; i++) {
609                 values[i] = ibuf[6 + i];
610         }
611         return 0;
612 }
613
614 static int af9005_boot_packet(struct usb_device *udev, int type, u8 * reply)
615 {
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;
659         }
660         deb_fw(">>> ");
661         debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw);
662
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;
679         }
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;
688                 }
689                 if (buf[3] != 0x05) {
690                         err("boot bad config size.");
691                         return -EIO;
692                 }
693                 if (buf[4] != 0x00) {
694                         err("boot bad config sequence.");
695                         return -EIO;
696                 }
697                 if (buf[5] != 0x04) {
698                         err("boot bad config subtype.");
699                         return -EIO;
700                 }
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;
706                 }
707                 *reply = buf[6];
708                 break;
709         case FW_CONFIRM:
710                 if (buf[2] != 0x11) {
711                         err("boot bad confirm header.");
712                         return -EIO;
713                 }
714                 if (buf[3] != 0x05) {
715                         err("boot bad confirm size.");
716                         return -EIO;
717                 }
718                 if (buf[4] != 0x00) {
719                         err("boot bad confirm sequence.");
720                         return -EIO;
721                 }
722                 if (buf[5] != 0x02) {
723                         err("boot bad confirm subtype.");
724                         return -EIO;
725                 }
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;
731                 }
732                 *reply = buf[6];
733                 break;
734         case FW_BOOT:
735                 if (buf[2] != 0x10) {
736                         err("boot bad boot header.");
737                         return -EIO;
738                 }
739                 if (buf[3] != 0x05) {
740                         err("boot bad boot size.");
741                         return -EIO;
742                 }
743                 if (buf[4] != 0x00) {
744                         err("boot bad boot sequence.");
745                         return -EIO;
746                 }
747                 if (buf[5] != 0x01) {
748                         err("boot bad boot pattern 01.");
749                         return -EIO;
750                 }
751                 if (buf[6] != 0x10) {
752                         err("boot bad boot pattern 10.");
753                         return -EIO;
754                 }
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;
760                 }
761                 break;
762
763         }
764
765         return 0;
766 }
767
768 int af9005_download_firmware(struct usb_device *udev, const struct firmware *fw)
769 {
770         int i, packets, ret, act_len;
771
772         u8 buf[FW_BULKOUT_SIZE + 2];
773         u8 reply;
774
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;
781         }
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;
796                 }
797         }
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;
804         }
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;
814         }
815
816         return 0;
817
818 }
819
820 int af9005_led_control(struct dvb_usb_device *d, int onoff)
821 {
822         struct af9005_device_state *st = d->priv;
823         int temp, ret;
824
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;
843         }
844         return 0;
845 }
846
847 static int af9005_frontend_attach(struct dvb_usb_adapter *adap)
848 {
849         u8 buf[8];
850         int i;
851
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.
855          */
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);
865                 }
866         }
867         adap->fe = af9005_fe_attach(adap->dev);
868         return 0;
869 }
870
871 static int af9005_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
872 {
873         struct af9005_device_state *st = d->priv;
874         int ret, len;
875
876         u8 obuf[5];
877         u8 ibuf[256];
878
879         *state = REMOTE_NO_KEY_PRESSED;
880         if (rc_decode == NULL) {
881                 /* it shouldn't never come here */
882                 return 0;
883         }
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;
894         }
895         if (ibuf[2] != 0x41) {
896                 err("rc query bad header.");
897                 return -EIO;
898         }
899         if (ibuf[4] != obuf[4]) {
900                 err("rc query bad sequence.");
901                 return -EIO;
902         }
903         len = ibuf[5];
904         if (len > 246) {
905                 err("rc query invalid length");
906                 return -EIO;
907         }
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;
919                 }
920         }
921         return 0;
922 }
923
924 static int af9005_power_ctrl(struct dvb_usb_device *d, int onoff)
925 {
926
927         return 0;
928 }
929
930 static int af9005_pid_filter_control(struct dvb_usb_adapter *adap, int onoff)
931 {
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;
953 }
954
955 static int af9005_pid_filter(struct dvb_usb_adapter *adap, int index,
956                              u16 pid, int onoff)
957 {
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;
970                 }
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;
990                 }
991         }
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;
997 }
998
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)
1003 {
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;
1018 }
1019
1020 static struct dvb_usb_device_properties af9005_properties;
1021
1022 static int af9005_usb_probe(struct usb_interface *intf,
1023                             const struct usb_device_id *id)
1024 {
1025         return dvb_usb_device_init(intf, &af9005_properties,
1026                                    THIS_MODULE, NULL, adapter_nr);
1027 }
1028
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},
1034 };
1035
1036 MODULE_DEVICE_TABLE(usb, af9005_usb_table);
1037
1038 static struct dvb_usb_device_properties af9005_properties = {
1039         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1040
1041         .usb_ctrl = DEVICE_SPECIFIC,
1042         .firmware = "af9005.fw",
1043         .download_firmware = af9005_download_firmware,
1044         .no_reconnect = 1,
1045
1046         .size_of_priv = sizeof(struct af9005_device_state),
1047
1048         .num_adapters = 1,
1049         .adapter = {
1050                     {
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 */
1067                                                }
1068                                       }
1069                                 },
1070                      }
1071                     },
1072         .power_ctrl = af9005_power_ctrl,
1073         .identify_state = af9005_identify_state,
1074
1075         .i2c_algo = &af9005_i2c_algo,
1076
1077         .rc_interval = 200,
1078         .rc_key_map = NULL,
1079         .rc_key_map_size = 0,
1080         .rc_query = af9005_rc_query,
1081
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},
1087                      },
1088                     {.name = "TerraTec Cinergy T USB XE",
1089                      .cold_ids = {&af9005_usb_table[1], NULL},
1090                      .warm_ids = {NULL},
1091                      },
1092                     {.name = "Ansonic DVB-T USB1.1 stick",
1093                      .cold_ids = {&af9005_usb_table[2], NULL},
1094                      .warm_ids = {NULL},
1095                      },
1096                     {NULL},
1097                     }
1098 };
1099
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,
1106 };
1107
1108 /* module stuff */
1109 static int __init af9005_usb_module_init(void)
1110 {
1111         int result;
1112         if ((result = usb_register(&af9005_usb_driver))) {
1113                 err("usb_register failed. (%d)", result);
1114                 return result;
1115         }
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;
1125         }
1126
1127         return 0;
1128 }
1129
1130 static void __exit af9005_usb_module_exit(void)
1131 {
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);
1141 }
1142
1143 module_init(af9005_usb_module_init);
1144 module_exit(af9005_usb_module_exit);
1145
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");