PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / media / usb / dvb-usb / af9005.c
blobaf176b6ce738e78d4dc366ae0d56597f833f4e59
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/README.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 bool 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_generic_read_write(struct dvb_usb_device *d, u16 reg,
58 int readwrite, int type, u8 * values, int len)
60 struct af9005_device_state *st = d->priv;
61 u8 obuf[16] = { 0 };
62 u8 ibuf[17] = { 0 };
63 u8 command;
64 int i;
65 int ret;
67 if (len < 1) {
68 err("generic read/write, less than 1 byte. Makes no sense.");
69 return -EINVAL;
71 if (len > 8) {
72 err("generic read/write, more than 8 bytes. Not supported.");
73 return -EINVAL;
76 obuf[0] = 14; /* rest of buffer length low */
77 obuf[1] = 0; /* rest of buffer length high */
79 obuf[2] = AF9005_REGISTER_RW; /* register operation */
80 obuf[3] = 12; /* rest of buffer length */
82 obuf[4] = st->sequence++; /* sequence number */
84 obuf[5] = (u8) (reg >> 8); /* register address */
85 obuf[6] = (u8) (reg & 0xff);
87 if (type == AF9005_OFDM_REG) {
88 command = AF9005_CMD_OFDM_REG;
89 } else {
90 command = AF9005_CMD_TUNER;
93 if (len > 1)
94 command |=
95 AF9005_CMD_BURST | AF9005_CMD_AUTOINC | (len - 1) << 3;
96 command |= readwrite;
97 if (readwrite == AF9005_CMD_WRITE)
98 for (i = 0; i < len; i++)
99 obuf[8 + i] = values[i];
100 else if (type == AF9005_TUNER_REG)
101 /* read command for tuner, the first byte contains the i2c address */
102 obuf[8] = values[0];
103 obuf[7] = command;
105 ret = dvb_usb_generic_rw(d, obuf, 16, ibuf, 17, 0);
106 if (ret)
107 return ret;
109 /* sanity check */
110 if (ibuf[2] != AF9005_REGISTER_RW_ACK) {
111 err("generic read/write, wrong reply code.");
112 return -EIO;
114 if (ibuf[3] != 0x0d) {
115 err("generic read/write, wrong length in reply.");
116 return -EIO;
118 if (ibuf[4] != obuf[4]) {
119 err("generic read/write, wrong sequence in reply.");
120 return -EIO;
123 Windows driver doesn't check these fields, in fact sometimes
124 the register in the reply is different that what has been sent
126 if (ibuf[5] != obuf[5] || ibuf[6] != obuf[6]) {
127 err("generic read/write, wrong register in reply.");
128 return -EIO;
130 if (ibuf[7] != command) {
131 err("generic read/write wrong command in reply.");
132 return -EIO;
135 if (ibuf[16] != 0x01) {
136 err("generic read/write wrong status code in reply.");
137 return -EIO;
139 if (readwrite == AF9005_CMD_READ)
140 for (i = 0; i < len; i++)
141 values[i] = ibuf[8 + i];
143 return 0;
147 int af9005_read_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 * value)
149 int ret;
150 deb_reg("read register %x ", reg);
151 ret = af9005_generic_read_write(d, reg,
152 AF9005_CMD_READ, AF9005_OFDM_REG,
153 value, 1);
154 if (ret)
155 deb_reg("failed\n");
156 else
157 deb_reg("value %x\n", *value);
158 return ret;
161 int af9005_read_ofdm_registers(struct dvb_usb_device *d, u16 reg,
162 u8 * values, int len)
164 int ret;
165 deb_reg("read %d registers %x ", len, reg);
166 ret = af9005_generic_read_write(d, reg,
167 AF9005_CMD_READ, AF9005_OFDM_REG,
168 values, len);
169 if (ret)
170 deb_reg("failed\n");
171 else
172 debug_dump(values, len, deb_reg);
173 return ret;
176 int af9005_write_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 value)
178 int ret;
179 u8 temp = value;
180 deb_reg("write register %x value %x ", reg, value);
181 ret = af9005_generic_read_write(d, reg,
182 AF9005_CMD_WRITE, AF9005_OFDM_REG,
183 &temp, 1);
184 if (ret)
185 deb_reg("failed\n");
186 else
187 deb_reg("ok\n");
188 return ret;
191 int af9005_write_ofdm_registers(struct dvb_usb_device *d, u16 reg,
192 u8 * values, int len)
194 int ret;
195 deb_reg("write %d registers %x values ", len, reg);
196 debug_dump(values, len, deb_reg);
198 ret = af9005_generic_read_write(d, reg,
199 AF9005_CMD_WRITE, AF9005_OFDM_REG,
200 values, len);
201 if (ret)
202 deb_reg("failed\n");
203 else
204 deb_reg("ok\n");
205 return ret;
208 int af9005_read_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos,
209 u8 len, u8 * value)
211 u8 temp;
212 int ret;
213 deb_reg("read bits %x %x %x", reg, pos, len);
214 ret = af9005_read_ofdm_register(d, reg, &temp);
215 if (ret) {
216 deb_reg(" failed\n");
217 return ret;
219 *value = (temp >> pos) & regmask[len - 1];
220 deb_reg(" value %x\n", *value);
221 return 0;
225 int af9005_write_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos,
226 u8 len, u8 value)
228 u8 temp, mask;
229 int ret;
230 deb_reg("write bits %x %x %x value %x\n", reg, pos, len, value);
231 if (pos == 0 && len == 8)
232 return af9005_write_ofdm_register(d, reg, value);
233 ret = af9005_read_ofdm_register(d, reg, &temp);
234 if (ret)
235 return ret;
236 mask = regmask[len - 1] << pos;
237 temp = (temp & ~mask) | ((value << pos) & mask);
238 return af9005_write_ofdm_register(d, reg, temp);
242 static int af9005_usb_read_tuner_registers(struct dvb_usb_device *d,
243 u16 reg, u8 * values, int len)
245 return af9005_generic_read_write(d, reg,
246 AF9005_CMD_READ, AF9005_TUNER_REG,
247 values, len);
250 static int af9005_usb_write_tuner_registers(struct dvb_usb_device *d,
251 u16 reg, u8 * values, int len)
253 return af9005_generic_read_write(d, reg,
254 AF9005_CMD_WRITE,
255 AF9005_TUNER_REG, values, len);
258 int af9005_write_tuner_registers(struct dvb_usb_device *d, u16 reg,
259 u8 * values, int len)
261 /* don't let the name of this function mislead you: it's just used
262 as an interface from the firmware to the i2c bus. The actual
263 i2c addresses are contained in the data */
264 int ret, i, done = 0, fail = 0;
265 u8 temp;
266 ret = af9005_usb_write_tuner_registers(d, reg, values, len);
267 if (ret)
268 return ret;
269 if (reg != 0xffff) {
270 /* check if write done (0xa40d bit 1) or fail (0xa40d bit 2) */
271 for (i = 0; i < 200; i++) {
272 ret =
273 af9005_read_ofdm_register(d,
274 xd_I2C_i2c_m_status_wdat_done,
275 &temp);
276 if (ret)
277 return ret;
278 done = temp & (regmask[i2c_m_status_wdat_done_len - 1]
279 << i2c_m_status_wdat_done_pos);
280 if (done)
281 break;
282 fail = temp & (regmask[i2c_m_status_wdat_fail_len - 1]
283 << i2c_m_status_wdat_fail_pos);
284 if (fail)
285 break;
286 msleep(50);
288 if (i == 200)
289 return -ETIMEDOUT;
290 if (fail) {
291 /* clear write fail bit */
292 af9005_write_register_bits(d,
293 xd_I2C_i2c_m_status_wdat_fail,
294 i2c_m_status_wdat_fail_pos,
295 i2c_m_status_wdat_fail_len,
297 return -EIO;
299 /* clear write done bit */
300 ret =
301 af9005_write_register_bits(d,
302 xd_I2C_i2c_m_status_wdat_fail,
303 i2c_m_status_wdat_done_pos,
304 i2c_m_status_wdat_done_len, 1);
305 if (ret)
306 return ret;
308 return 0;
311 int af9005_read_tuner_registers(struct dvb_usb_device *d, u16 reg, u8 addr,
312 u8 * values, int len)
314 /* don't let the name of this function mislead you: it's just used
315 as an interface from the firmware to the i2c bus. The actual
316 i2c addresses are contained in the data */
317 int ret, i;
318 u8 temp, buf[2];
320 buf[0] = addr; /* tuner i2c address */
321 buf[1] = values[0]; /* tuner register */
323 values[0] = addr + 0x01; /* i2c read address */
325 if (reg == APO_REG_I2C_RW_SILICON_TUNER) {
326 /* write tuner i2c address to tuner, 0c00c0 undocumented, found by sniffing */
327 ret = af9005_write_tuner_registers(d, 0x00c0, buf, 2);
328 if (ret)
329 return ret;
332 /* send read command to ofsm */
333 ret = af9005_usb_read_tuner_registers(d, reg, values, 1);
334 if (ret)
335 return ret;
337 /* check if read done */
338 for (i = 0; i < 200; i++) {
339 ret = af9005_read_ofdm_register(d, 0xa408, &temp);
340 if (ret)
341 return ret;
342 if (temp & 0x01)
343 break;
344 msleep(50);
346 if (i == 200)
347 return -ETIMEDOUT;
349 /* clear read done bit (by writing 1) */
350 ret = af9005_write_ofdm_register(d, xd_I2C_i2c_m_data8, 1);
351 if (ret)
352 return ret;
354 /* get read data (available from 0xa400) */
355 for (i = 0; i < len; i++) {
356 ret = af9005_read_ofdm_register(d, 0xa400 + i, &temp);
357 if (ret)
358 return ret;
359 values[i] = temp;
361 return 0;
364 static int af9005_i2c_write(struct dvb_usb_device *d, u8 i2caddr, u8 reg,
365 u8 * data, int len)
367 int ret, i;
368 u8 buf[3];
369 deb_i2c("i2c_write i2caddr %x, reg %x, len %d data ", i2caddr,
370 reg, len);
371 debug_dump(data, len, deb_i2c);
373 for (i = 0; i < len; i++) {
374 buf[0] = i2caddr;
375 buf[1] = reg + (u8) i;
376 buf[2] = data[i];
377 ret =
378 af9005_write_tuner_registers(d,
379 APO_REG_I2C_RW_SILICON_TUNER,
380 buf, 3);
381 if (ret) {
382 deb_i2c("i2c_write failed\n");
383 return ret;
386 deb_i2c("i2c_write ok\n");
387 return 0;
390 static int af9005_i2c_read(struct dvb_usb_device *d, u8 i2caddr, u8 reg,
391 u8 * data, int len)
393 int ret, i;
394 u8 temp;
395 deb_i2c("i2c_read i2caddr %x, reg %x, len %d\n ", i2caddr, reg, len);
396 for (i = 0; i < len; i++) {
397 temp = reg + i;
398 ret =
399 af9005_read_tuner_registers(d,
400 APO_REG_I2C_RW_SILICON_TUNER,
401 i2caddr, &temp, 1);
402 if (ret) {
403 deb_i2c("i2c_read failed\n");
404 return ret;
406 data[i] = temp;
408 deb_i2c("i2c data read: ");
409 debug_dump(data, len, deb_i2c);
410 return 0;
413 static int af9005_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
414 int num)
416 /* only implements what the mt2060 module does, don't know how
417 to make it really generic */
418 struct dvb_usb_device *d = i2c_get_adapdata(adap);
419 int ret;
420 u8 reg, addr;
421 u8 *value;
423 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
424 return -EAGAIN;
426 if (num > 2)
427 warn("more than 2 i2c messages at a time is not handled yet. TODO.");
429 if (num == 2) {
430 /* reads a single register */
431 reg = *msg[0].buf;
432 addr = msg[0].addr;
433 value = msg[1].buf;
434 ret = af9005_i2c_read(d, addr, reg, value, 1);
435 if (ret == 0)
436 ret = 2;
437 } else {
438 /* write one or more registers */
439 reg = msg[0].buf[0];
440 addr = msg[0].addr;
441 value = &msg[0].buf[1];
442 ret = af9005_i2c_write(d, addr, reg, value, msg[0].len - 1);
443 if (ret == 0)
444 ret = 1;
447 mutex_unlock(&d->i2c_mutex);
448 return ret;
451 static u32 af9005_i2c_func(struct i2c_adapter *adapter)
453 return I2C_FUNC_I2C;
456 static struct i2c_algorithm af9005_i2c_algo = {
457 .master_xfer = af9005_i2c_xfer,
458 .functionality = af9005_i2c_func,
461 int af9005_send_command(struct dvb_usb_device *d, u8 command, u8 * wbuf,
462 int wlen, u8 * rbuf, int rlen)
464 struct af9005_device_state *st = d->priv;
466 int ret, i, packet_len;
467 u8 buf[64];
468 u8 ibuf[64];
470 if (wlen < 0) {
471 err("send command, wlen less than 0 bytes. Makes no sense.");
472 return -EINVAL;
474 if (wlen > 54) {
475 err("send command, wlen more than 54 bytes. Not supported.");
476 return -EINVAL;
478 if (rlen > 54) {
479 err("send command, rlen more than 54 bytes. Not supported.");
480 return -EINVAL;
482 packet_len = wlen + 5;
483 buf[0] = (u8) (packet_len & 0xff);
484 buf[1] = (u8) ((packet_len & 0xff00) >> 8);
486 buf[2] = 0x26; /* packet type */
487 buf[3] = wlen + 3;
488 buf[4] = st->sequence++;
489 buf[5] = command;
490 buf[6] = wlen;
491 for (i = 0; i < wlen; i++)
492 buf[7 + i] = wbuf[i];
493 ret = dvb_usb_generic_rw(d, buf, wlen + 7, ibuf, rlen + 7, 0);
494 if (ret)
495 return ret;
496 if (ibuf[2] != 0x27) {
497 err("send command, wrong reply code.");
498 return -EIO;
500 if (ibuf[4] != buf[4]) {
501 err("send command, wrong sequence in reply.");
502 return -EIO;
504 if (ibuf[5] != 0x01) {
505 err("send command, wrong status code in reply.");
506 return -EIO;
508 if (ibuf[6] != rlen) {
509 err("send command, invalid data length in reply.");
510 return -EIO;
512 for (i = 0; i < rlen; i++)
513 rbuf[i] = ibuf[i + 7];
514 return 0;
517 int af9005_read_eeprom(struct dvb_usb_device *d, u8 address, u8 * values,
518 int len)
520 struct af9005_device_state *st = d->priv;
521 u8 obuf[16], ibuf[14];
522 int ret, i;
524 memset(obuf, 0, sizeof(obuf));
525 memset(ibuf, 0, sizeof(ibuf));
527 obuf[0] = 14; /* length of rest of packet low */
528 obuf[1] = 0; /* length of rest of packer high */
530 obuf[2] = 0x2a; /* read/write eeprom */
532 obuf[3] = 12; /* size */
534 obuf[4] = st->sequence++;
536 obuf[5] = 0; /* read */
538 obuf[6] = len;
539 obuf[7] = address;
540 ret = dvb_usb_generic_rw(d, obuf, 16, ibuf, 14, 0);
541 if (ret)
542 return ret;
543 if (ibuf[2] != 0x2b) {
544 err("Read eeprom, invalid reply code");
545 return -EIO;
547 if (ibuf[3] != 10) {
548 err("Read eeprom, invalid reply length");
549 return -EIO;
551 if (ibuf[4] != obuf[4]) {
552 err("Read eeprom, wrong sequence in reply ");
553 return -EIO;
555 if (ibuf[5] != 1) {
556 err("Read eeprom, wrong status in reply ");
557 return -EIO;
559 for (i = 0; i < len; i++) {
560 values[i] = ibuf[6 + i];
562 return 0;
565 static int af9005_boot_packet(struct usb_device *udev, int type, u8 * reply)
567 u8 buf[FW_BULKOUT_SIZE + 2];
568 u16 checksum;
569 int act_len, i, ret;
570 memset(buf, 0, sizeof(buf));
571 buf[0] = (u8) (FW_BULKOUT_SIZE & 0xff);
572 buf[1] = (u8) ((FW_BULKOUT_SIZE >> 8) & 0xff);
573 switch (type) {
574 case FW_CONFIG:
575 buf[2] = 0x11;
576 buf[3] = 0x04;
577 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
578 buf[5] = 0x03;
579 checksum = buf[4] + buf[5];
580 buf[6] = (u8) ((checksum >> 8) & 0xff);
581 buf[7] = (u8) (checksum & 0xff);
582 break;
583 case FW_CONFIRM:
584 buf[2] = 0x11;
585 buf[3] = 0x04;
586 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
587 buf[5] = 0x01;
588 checksum = buf[4] + buf[5];
589 buf[6] = (u8) ((checksum >> 8) & 0xff);
590 buf[7] = (u8) (checksum & 0xff);
591 break;
592 case FW_BOOT:
593 buf[2] = 0x10;
594 buf[3] = 0x08;
595 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
596 buf[5] = 0x97;
597 buf[6] = 0xaa;
598 buf[7] = 0x55;
599 buf[8] = 0xa5;
600 buf[9] = 0x5a;
601 checksum = 0;
602 for (i = 4; i <= 9; i++)
603 checksum += buf[i];
604 buf[10] = (u8) ((checksum >> 8) & 0xff);
605 buf[11] = (u8) (checksum & 0xff);
606 break;
607 default:
608 err("boot packet invalid boot packet type");
609 return -EINVAL;
611 deb_fw(">>> ");
612 debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw);
614 ret = usb_bulk_msg(udev,
615 usb_sndbulkpipe(udev, 0x02),
616 buf, FW_BULKOUT_SIZE + 2, &act_len, 2000);
617 if (ret)
618 err("boot packet bulk message failed: %d (%d/%d)", ret,
619 FW_BULKOUT_SIZE + 2, act_len);
620 else
621 ret = act_len != FW_BULKOUT_SIZE + 2 ? -1 : 0;
622 if (ret)
623 return ret;
624 memset(buf, 0, 9);
625 ret = usb_bulk_msg(udev,
626 usb_rcvbulkpipe(udev, 0x01), buf, 9, &act_len, 2000);
627 if (ret) {
628 err("boot packet recv bulk message failed: %d", ret);
629 return ret;
631 deb_fw("<<< ");
632 debug_dump(buf, act_len, deb_fw);
633 checksum = 0;
634 switch (type) {
635 case FW_CONFIG:
636 if (buf[2] != 0x11) {
637 err("boot bad config header.");
638 return -EIO;
640 if (buf[3] != 0x05) {
641 err("boot bad config size.");
642 return -EIO;
644 if (buf[4] != 0x00) {
645 err("boot bad config sequence.");
646 return -EIO;
648 if (buf[5] != 0x04) {
649 err("boot bad config subtype.");
650 return -EIO;
652 for (i = 4; i <= 6; i++)
653 checksum += buf[i];
654 if (buf[7] * 256 + buf[8] != checksum) {
655 err("boot bad config checksum.");
656 return -EIO;
658 *reply = buf[6];
659 break;
660 case FW_CONFIRM:
661 if (buf[2] != 0x11) {
662 err("boot bad confirm header.");
663 return -EIO;
665 if (buf[3] != 0x05) {
666 err("boot bad confirm size.");
667 return -EIO;
669 if (buf[4] != 0x00) {
670 err("boot bad confirm sequence.");
671 return -EIO;
673 if (buf[5] != 0x02) {
674 err("boot bad confirm subtype.");
675 return -EIO;
677 for (i = 4; i <= 6; i++)
678 checksum += buf[i];
679 if (buf[7] * 256 + buf[8] != checksum) {
680 err("boot bad confirm checksum.");
681 return -EIO;
683 *reply = buf[6];
684 break;
685 case FW_BOOT:
686 if (buf[2] != 0x10) {
687 err("boot bad boot header.");
688 return -EIO;
690 if (buf[3] != 0x05) {
691 err("boot bad boot size.");
692 return -EIO;
694 if (buf[4] != 0x00) {
695 err("boot bad boot sequence.");
696 return -EIO;
698 if (buf[5] != 0x01) {
699 err("boot bad boot pattern 01.");
700 return -EIO;
702 if (buf[6] != 0x10) {
703 err("boot bad boot pattern 10.");
704 return -EIO;
706 for (i = 4; i <= 6; i++)
707 checksum += buf[i];
708 if (buf[7] * 256 + buf[8] != checksum) {
709 err("boot bad boot checksum.");
710 return -EIO;
712 break;
716 return 0;
719 static int af9005_download_firmware(struct usb_device *udev, const struct firmware *fw)
721 int i, packets, ret, act_len;
723 u8 buf[FW_BULKOUT_SIZE + 2];
724 u8 reply;
726 ret = af9005_boot_packet(udev, FW_CONFIG, &reply);
727 if (ret)
728 return ret;
729 if (reply != 0x01) {
730 err("before downloading firmware, FW_CONFIG expected 0x01, received 0x%x", reply);
731 return -EIO;
733 packets = fw->size / FW_BULKOUT_SIZE;
734 buf[0] = (u8) (FW_BULKOUT_SIZE & 0xff);
735 buf[1] = (u8) ((FW_BULKOUT_SIZE >> 8) & 0xff);
736 for (i = 0; i < packets; i++) {
737 memcpy(&buf[2], fw->data + i * FW_BULKOUT_SIZE,
738 FW_BULKOUT_SIZE);
739 deb_fw(">>> ");
740 debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw);
741 ret = usb_bulk_msg(udev,
742 usb_sndbulkpipe(udev, 0x02),
743 buf, FW_BULKOUT_SIZE + 2, &act_len, 1000);
744 if (ret) {
745 err("firmware download failed at packet %d with code %d", i, ret);
746 return ret;
749 ret = af9005_boot_packet(udev, FW_CONFIRM, &reply);
750 if (ret)
751 return ret;
752 if (reply != (u8) (packets & 0xff)) {
753 err("after downloading firmware, FW_CONFIRM expected 0x%x, received 0x%x", packets & 0xff, reply);
754 return -EIO;
756 ret = af9005_boot_packet(udev, FW_BOOT, &reply);
757 if (ret)
758 return ret;
759 ret = af9005_boot_packet(udev, FW_CONFIG, &reply);
760 if (ret)
761 return ret;
762 if (reply != 0x02) {
763 err("after downloading firmware, FW_CONFIG expected 0x02, received 0x%x", reply);
764 return -EIO;
767 return 0;
771 int af9005_led_control(struct dvb_usb_device *d, int onoff)
773 struct af9005_device_state *st = d->priv;
774 int temp, ret;
776 if (onoff && dvb_usb_af9005_led)
777 temp = 1;
778 else
779 temp = 0;
780 if (st->led_state != temp) {
781 ret =
782 af9005_write_register_bits(d, xd_p_reg_top_locken1,
783 reg_top_locken1_pos,
784 reg_top_locken1_len, temp);
785 if (ret)
786 return ret;
787 ret =
788 af9005_write_register_bits(d, xd_p_reg_top_lock1,
789 reg_top_lock1_pos,
790 reg_top_lock1_len, temp);
791 if (ret)
792 return ret;
793 st->led_state = temp;
795 return 0;
798 static int af9005_frontend_attach(struct dvb_usb_adapter *adap)
800 u8 buf[8];
801 int i;
803 /* without these calls the first commands after downloading
804 the firmware fail. I put these calls here to simulate
805 what it is done in dvb-usb-init.c.
807 struct usb_device *udev = adap->dev->udev;
808 usb_clear_halt(udev, usb_sndbulkpipe(udev, 2));
809 usb_clear_halt(udev, usb_rcvbulkpipe(udev, 1));
810 if (dvb_usb_af9005_dump_eeprom) {
811 printk("EEPROM DUMP\n");
812 for (i = 0; i < 255; i += 8) {
813 af9005_read_eeprom(adap->dev, i, buf, 8);
814 printk("ADDR %x ", i);
815 debug_dump(buf, 8, printk);
818 adap->fe_adap[0].fe = af9005_fe_attach(adap->dev);
819 return 0;
822 static int af9005_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
824 struct af9005_device_state *st = d->priv;
825 int ret, len;
827 u8 obuf[5];
828 u8 ibuf[256];
830 *state = REMOTE_NO_KEY_PRESSED;
831 if (rc_decode == NULL) {
832 /* it shouldn't never come here */
833 return 0;
835 /* deb_info("rc_query\n"); */
836 obuf[0] = 3; /* rest of packet length low */
837 obuf[1] = 0; /* rest of packet lentgh high */
838 obuf[2] = 0x40; /* read remote */
839 obuf[3] = 1; /* rest of packet length */
840 obuf[4] = st->sequence++; /* sequence number */
841 ret = dvb_usb_generic_rw(d, obuf, 5, ibuf, 256, 0);
842 if (ret) {
843 err("rc query failed");
844 return ret;
846 if (ibuf[2] != 0x41) {
847 err("rc query bad header.");
848 return -EIO;
850 if (ibuf[4] != obuf[4]) {
851 err("rc query bad sequence.");
852 return -EIO;
854 len = ibuf[5];
855 if (len > 246) {
856 err("rc query invalid length");
857 return -EIO;
859 if (len > 0) {
860 deb_rc("rc data (%d) ", len);
861 debug_dump((ibuf + 6), len, deb_rc);
862 ret = rc_decode(d, &ibuf[6], len, event, state);
863 if (ret) {
864 err("rc_decode failed");
865 return ret;
866 } else {
867 deb_rc("rc_decode state %x event %x\n", *state, *event);
868 if (*state == REMOTE_KEY_REPEAT)
869 *event = d->last_event;
872 return 0;
875 static int af9005_power_ctrl(struct dvb_usb_device *d, int onoff)
878 return 0;
881 static int af9005_pid_filter_control(struct dvb_usb_adapter *adap, int onoff)
883 int ret;
884 deb_info("pid filter control onoff %d\n", onoff);
885 if (onoff) {
886 ret =
887 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 1);
888 if (ret)
889 return ret;
890 ret =
891 af9005_write_register_bits(adap->dev,
892 XD_MP2IF_DMX_CTRL, 1, 1, 1);
893 if (ret)
894 return ret;
895 ret =
896 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 1);
897 } else
898 ret =
899 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 0);
900 if (ret)
901 return ret;
902 deb_info("pid filter control ok\n");
903 return 0;
906 static int af9005_pid_filter(struct dvb_usb_adapter *adap, int index,
907 u16 pid, int onoff)
909 u8 cmd = index & 0x1f;
910 int ret;
911 deb_info("set pid filter, index %d, pid %x, onoff %d\n", index,
912 pid, onoff);
913 if (onoff) {
914 /* cannot use it as pid_filter_ctrl since it has to be done
915 before setting the first pid */
916 if (adap->feedcount == 1) {
917 deb_info("first pid set, enable pid table\n");
918 ret = af9005_pid_filter_control(adap, onoff);
919 if (ret)
920 return ret;
922 ret =
923 af9005_write_ofdm_register(adap->dev,
924 XD_MP2IF_PID_DATA_L,
925 (u8) (pid & 0xff));
926 if (ret)
927 return ret;
928 ret =
929 af9005_write_ofdm_register(adap->dev,
930 XD_MP2IF_PID_DATA_H,
931 (u8) (pid >> 8));
932 if (ret)
933 return ret;
934 cmd |= 0x20 | 0x40;
935 } else {
936 if (adap->feedcount == 0) {
937 deb_info("last pid unset, disable pid table\n");
938 ret = af9005_pid_filter_control(adap, onoff);
939 if (ret)
940 return ret;
943 ret = af9005_write_ofdm_register(adap->dev, XD_MP2IF_PID_IDX, cmd);
944 if (ret)
945 return ret;
946 deb_info("set pid ok\n");
947 return 0;
950 static int af9005_identify_state(struct usb_device *udev,
951 struct dvb_usb_device_properties *props,
952 struct dvb_usb_device_description **desc,
953 int *cold)
955 int ret;
956 u8 reply;
957 ret = af9005_boot_packet(udev, FW_CONFIG, &reply);
958 if (ret)
959 return ret;
960 deb_info("result of FW_CONFIG in identify state %d\n", reply);
961 if (reply == 0x01)
962 *cold = 1;
963 else if (reply == 0x02)
964 *cold = 0;
965 else
966 return -EIO;
967 deb_info("Identify state cold = %d\n", *cold);
968 return 0;
971 static struct dvb_usb_device_properties af9005_properties;
973 static int af9005_usb_probe(struct usb_interface *intf,
974 const struct usb_device_id *id)
976 return dvb_usb_device_init(intf, &af9005_properties,
977 THIS_MODULE, NULL, adapter_nr);
980 enum af9005_usb_table_entry {
981 AFATECH_AF9005,
982 TERRATEC_AF9005,
983 ANSONIC_AF9005,
986 static struct usb_device_id af9005_usb_table[] = {
987 [AFATECH_AF9005] = {USB_DEVICE(USB_VID_AFATECH,
988 USB_PID_AFATECH_AF9005)},
989 [TERRATEC_AF9005] = {USB_DEVICE(USB_VID_TERRATEC,
990 USB_PID_TERRATEC_CINERGY_T_USB_XE)},
991 [ANSONIC_AF9005] = {USB_DEVICE(USB_VID_ANSONIC,
992 USB_PID_ANSONIC_DVBT_USB)},
996 MODULE_DEVICE_TABLE(usb, af9005_usb_table);
998 static struct dvb_usb_device_properties af9005_properties = {
999 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1001 .usb_ctrl = DEVICE_SPECIFIC,
1002 .firmware = "af9005.fw",
1003 .download_firmware = af9005_download_firmware,
1004 .no_reconnect = 1,
1006 .size_of_priv = sizeof(struct af9005_device_state),
1008 .num_adapters = 1,
1009 .adapter = {
1011 .num_frontends = 1,
1012 .fe = {{
1013 .caps =
1014 DVB_USB_ADAP_HAS_PID_FILTER |
1015 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1016 .pid_filter_count = 32,
1017 .pid_filter = af9005_pid_filter,
1018 /* .pid_filter_ctrl = af9005_pid_filter_control, */
1019 .frontend_attach = af9005_frontend_attach,
1020 /* .tuner_attach = af9005_tuner_attach, */
1021 /* parameter for the MPEG2-data transfer */
1022 .stream = {
1023 .type = USB_BULK,
1024 .count = 10,
1025 .endpoint = 0x04,
1026 .u = {
1027 .bulk = {
1028 .buffersize = 4096, /* actual size seen is 3948 */
1035 .power_ctrl = af9005_power_ctrl,
1036 .identify_state = af9005_identify_state,
1038 .i2c_algo = &af9005_i2c_algo,
1040 .rc.legacy = {
1041 .rc_interval = 200,
1042 .rc_map_table = NULL,
1043 .rc_map_size = 0,
1044 .rc_query = af9005_rc_query,
1047 .generic_bulk_ctrl_endpoint = 2,
1048 .generic_bulk_ctrl_endpoint_response = 1,
1050 .num_device_descs = 3,
1051 .devices = {
1052 {.name = "Afatech DVB-T USB1.1 stick",
1053 .cold_ids = {&af9005_usb_table[AFATECH_AF9005], NULL},
1054 .warm_ids = {NULL},
1056 {.name = "TerraTec Cinergy T USB XE",
1057 .cold_ids = {&af9005_usb_table[TERRATEC_AF9005], NULL},
1058 .warm_ids = {NULL},
1060 {.name = "Ansonic DVB-T USB1.1 stick",
1061 .cold_ids = {&af9005_usb_table[ANSONIC_AF9005], NULL},
1062 .warm_ids = {NULL},
1064 {NULL},
1068 /* usb specific object needed to register this driver with the usb subsystem */
1069 static struct usb_driver af9005_usb_driver = {
1070 .name = "dvb_usb_af9005",
1071 .probe = af9005_usb_probe,
1072 .disconnect = dvb_usb_device_exit,
1073 .id_table = af9005_usb_table,
1076 /* module stuff */
1077 static int __init af9005_usb_module_init(void)
1079 int result;
1080 if ((result = usb_register(&af9005_usb_driver))) {
1081 err("usb_register failed. (%d)", result);
1082 return result;
1084 rc_decode = symbol_request(af9005_rc_decode);
1085 rc_keys = symbol_request(rc_map_af9005_table);
1086 rc_keys_size = symbol_request(rc_map_af9005_table_size);
1087 if (rc_decode == NULL || rc_keys == NULL || rc_keys_size == NULL) {
1088 err("af9005_rc_decode function not found, disabling remote");
1089 af9005_properties.rc.legacy.rc_query = NULL;
1090 } else {
1091 af9005_properties.rc.legacy.rc_map_table = rc_keys;
1092 af9005_properties.rc.legacy.rc_map_size = *rc_keys_size;
1095 return 0;
1098 static void __exit af9005_usb_module_exit(void)
1100 /* release rc decode symbols */
1101 if (rc_decode != NULL)
1102 symbol_put(af9005_rc_decode);
1103 if (rc_keys != NULL)
1104 symbol_put(rc_map_af9005_table);
1105 if (rc_keys_size != NULL)
1106 symbol_put(rc_map_af9005_table_size);
1107 /* deregister this driver from the USB subsystem */
1108 usb_deregister(&af9005_usb_driver);
1111 module_init(af9005_usb_module_init);
1112 module_exit(af9005_usb_module_exit);
1114 MODULE_AUTHOR("Luca Olivetti <luca@ventoso.org>");
1115 MODULE_DESCRIPTION("Driver for Afatech 9005 DVB-T USB1.1 stick");
1116 MODULE_VERSION("1.0");
1117 MODULE_LICENSE("GPL");