perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / usb / dvb-usb / af9005.c
blob16e946e01d2caab9eaf68546067b3fb92f18731d
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 * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
20 #include "af9005.h"
22 /* debug */
23 int dvb_usb_af9005_debug;
24 module_param_named(debug, dvb_usb_af9005_debug, int, 0644);
25 MODULE_PARM_DESC(debug,
26 "set debugging level (1=info,xfer=2,rc=4,reg=8,i2c=16,fw=32 (or-able))."
27 DVB_USB_DEBUG_STATUS);
28 /* enable obnoxious led */
29 bool dvb_usb_af9005_led = true;
30 module_param_named(led, dvb_usb_af9005_led, bool, 0644);
31 MODULE_PARM_DESC(led, "enable led (default: 1).");
33 /* eeprom dump */
34 static int dvb_usb_af9005_dump_eeprom;
35 module_param_named(dump_eeprom, dvb_usb_af9005_dump_eeprom, int, 0);
36 MODULE_PARM_DESC(dump_eeprom, "dump contents of the eeprom.");
38 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
40 /* remote control decoder */
41 static int (*rc_decode) (struct dvb_usb_device *d, u8 *data, int len,
42 u32 *event, int *state);
43 static void *rc_keys;
44 static int *rc_keys_size;
46 u8 regmask[8] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
48 struct af9005_device_state {
49 u8 sequence;
50 int led_state;
51 unsigned char data[256];
54 static int af9005_generic_read_write(struct dvb_usb_device *d, u16 reg,
55 int readwrite, int type, u8 * values, int len)
57 struct af9005_device_state *st = d->priv;
58 u8 command, seq;
59 int i, ret;
61 if (len < 1) {
62 err("generic read/write, less than 1 byte. Makes no sense.");
63 return -EINVAL;
65 if (len > 8) {
66 err("generic read/write, more than 8 bytes. Not supported.");
67 return -EINVAL;
70 mutex_lock(&d->data_mutex);
71 st->data[0] = 14; /* rest of buffer length low */
72 st->data[1] = 0; /* rest of buffer length high */
74 st->data[2] = AF9005_REGISTER_RW; /* register operation */
75 st->data[3] = 12; /* rest of buffer length */
77 st->data[4] = seq = st->sequence++; /* sequence number */
79 st->data[5] = (u8) (reg >> 8); /* register address */
80 st->data[6] = (u8) (reg & 0xff);
82 if (type == AF9005_OFDM_REG) {
83 command = AF9005_CMD_OFDM_REG;
84 } else {
85 command = AF9005_CMD_TUNER;
88 if (len > 1)
89 command |=
90 AF9005_CMD_BURST | AF9005_CMD_AUTOINC | (len - 1) << 3;
91 command |= readwrite;
92 if (readwrite == AF9005_CMD_WRITE)
93 for (i = 0; i < len; i++)
94 st->data[8 + i] = values[i];
95 else if (type == AF9005_TUNER_REG)
96 /* read command for tuner, the first byte contains the i2c address */
97 st->data[8] = values[0];
98 st->data[7] = command;
100 ret = dvb_usb_generic_rw(d, st->data, 16, st->data, 17, 0);
101 if (ret)
102 goto ret;
104 /* sanity check */
105 if (st->data[2] != AF9005_REGISTER_RW_ACK) {
106 err("generic read/write, wrong reply code.");
107 ret = -EIO;
108 goto ret;
110 if (st->data[3] != 0x0d) {
111 err("generic read/write, wrong length in reply.");
112 ret = -EIO;
113 goto ret;
115 if (st->data[4] != seq) {
116 err("generic read/write, wrong sequence in reply.");
117 ret = -EIO;
118 goto ret;
121 * In thesis, both input and output buffers should have
122 * identical values for st->data[5] to st->data[8].
123 * However, windows driver doesn't check these fields, in fact
124 * sometimes the register in the reply is different that what
125 * has been sent
127 if (st->data[16] != 0x01) {
128 err("generic read/write wrong status code in reply.");
129 ret = -EIO;
130 goto ret;
133 if (readwrite == AF9005_CMD_READ)
134 for (i = 0; i < len; i++)
135 values[i] = st->data[8 + i];
137 ret:
138 mutex_unlock(&d->data_mutex);
139 return ret;
143 int af9005_read_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 * value)
145 int ret;
146 deb_reg("read register %x ", reg);
147 ret = af9005_generic_read_write(d, reg,
148 AF9005_CMD_READ, AF9005_OFDM_REG,
149 value, 1);
150 if (ret)
151 deb_reg("failed\n");
152 else
153 deb_reg("value %x\n", *value);
154 return ret;
157 int af9005_read_ofdm_registers(struct dvb_usb_device *d, u16 reg,
158 u8 * values, int len)
160 int ret;
161 deb_reg("read %d registers %x ", len, reg);
162 ret = af9005_generic_read_write(d, reg,
163 AF9005_CMD_READ, AF9005_OFDM_REG,
164 values, len);
165 if (ret)
166 deb_reg("failed\n");
167 else
168 debug_dump(values, len, deb_reg);
169 return ret;
172 int af9005_write_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 value)
174 int ret;
175 u8 temp = value;
176 deb_reg("write register %x value %x ", reg, value);
177 ret = af9005_generic_read_write(d, reg,
178 AF9005_CMD_WRITE, AF9005_OFDM_REG,
179 &temp, 1);
180 if (ret)
181 deb_reg("failed\n");
182 else
183 deb_reg("ok\n");
184 return ret;
187 int af9005_write_ofdm_registers(struct dvb_usb_device *d, u16 reg,
188 u8 * values, int len)
190 int ret;
191 deb_reg("write %d registers %x values ", len, reg);
192 debug_dump(values, len, deb_reg);
194 ret = af9005_generic_read_write(d, reg,
195 AF9005_CMD_WRITE, AF9005_OFDM_REG,
196 values, len);
197 if (ret)
198 deb_reg("failed\n");
199 else
200 deb_reg("ok\n");
201 return ret;
204 int af9005_read_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos,
205 u8 len, u8 * value)
207 u8 temp;
208 int ret;
209 deb_reg("read bits %x %x %x", reg, pos, len);
210 ret = af9005_read_ofdm_register(d, reg, &temp);
211 if (ret) {
212 deb_reg(" failed\n");
213 return ret;
215 *value = (temp >> pos) & regmask[len - 1];
216 deb_reg(" value %x\n", *value);
217 return 0;
221 int af9005_write_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos,
222 u8 len, u8 value)
224 u8 temp, mask;
225 int ret;
226 deb_reg("write bits %x %x %x value %x\n", reg, pos, len, value);
227 if (pos == 0 && len == 8)
228 return af9005_write_ofdm_register(d, reg, value);
229 ret = af9005_read_ofdm_register(d, reg, &temp);
230 if (ret)
231 return ret;
232 mask = regmask[len - 1] << pos;
233 temp = (temp & ~mask) | ((value << pos) & mask);
234 return af9005_write_ofdm_register(d, reg, temp);
238 static int af9005_usb_read_tuner_registers(struct dvb_usb_device *d,
239 u16 reg, u8 * values, int len)
241 return af9005_generic_read_write(d, reg,
242 AF9005_CMD_READ, AF9005_TUNER_REG,
243 values, len);
246 static int af9005_usb_write_tuner_registers(struct dvb_usb_device *d,
247 u16 reg, u8 * values, int len)
249 return af9005_generic_read_write(d, reg,
250 AF9005_CMD_WRITE,
251 AF9005_TUNER_REG, values, len);
254 int af9005_write_tuner_registers(struct dvb_usb_device *d, u16 reg,
255 u8 * values, int len)
257 /* don't let the name of this function mislead you: it's just used
258 as an interface from the firmware to the i2c bus. The actual
259 i2c addresses are contained in the data */
260 int ret, i, done = 0, fail = 0;
261 u8 temp;
262 ret = af9005_usb_write_tuner_registers(d, reg, values, len);
263 if (ret)
264 return ret;
265 if (reg != 0xffff) {
266 /* check if write done (0xa40d bit 1) or fail (0xa40d bit 2) */
267 for (i = 0; i < 200; i++) {
268 ret =
269 af9005_read_ofdm_register(d,
270 xd_I2C_i2c_m_status_wdat_done,
271 &temp);
272 if (ret)
273 return ret;
274 done = temp & (regmask[i2c_m_status_wdat_done_len - 1]
275 << i2c_m_status_wdat_done_pos);
276 if (done)
277 break;
278 fail = temp & (regmask[i2c_m_status_wdat_fail_len - 1]
279 << i2c_m_status_wdat_fail_pos);
280 if (fail)
281 break;
282 msleep(50);
284 if (i == 200)
285 return -ETIMEDOUT;
286 if (fail) {
287 /* clear write fail bit */
288 af9005_write_register_bits(d,
289 xd_I2C_i2c_m_status_wdat_fail,
290 i2c_m_status_wdat_fail_pos,
291 i2c_m_status_wdat_fail_len,
293 return -EIO;
295 /* clear write done bit */
296 ret =
297 af9005_write_register_bits(d,
298 xd_I2C_i2c_m_status_wdat_fail,
299 i2c_m_status_wdat_done_pos,
300 i2c_m_status_wdat_done_len, 1);
301 if (ret)
302 return ret;
304 return 0;
307 int af9005_read_tuner_registers(struct dvb_usb_device *d, u16 reg, u8 addr,
308 u8 * values, int len)
310 /* don't let the name of this function mislead you: it's just used
311 as an interface from the firmware to the i2c bus. The actual
312 i2c addresses are contained in the data */
313 int ret, i;
314 u8 temp, buf[2];
316 buf[0] = addr; /* tuner i2c address */
317 buf[1] = values[0]; /* tuner register */
319 values[0] = addr + 0x01; /* i2c read address */
321 if (reg == APO_REG_I2C_RW_SILICON_TUNER) {
322 /* write tuner i2c address to tuner, 0c00c0 undocumented, found by sniffing */
323 ret = af9005_write_tuner_registers(d, 0x00c0, buf, 2);
324 if (ret)
325 return ret;
328 /* send read command to ofsm */
329 ret = af9005_usb_read_tuner_registers(d, reg, values, 1);
330 if (ret)
331 return ret;
333 /* check if read done */
334 for (i = 0; i < 200; i++) {
335 ret = af9005_read_ofdm_register(d, 0xa408, &temp);
336 if (ret)
337 return ret;
338 if (temp & 0x01)
339 break;
340 msleep(50);
342 if (i == 200)
343 return -ETIMEDOUT;
345 /* clear read done bit (by writing 1) */
346 ret = af9005_write_ofdm_register(d, xd_I2C_i2c_m_data8, 1);
347 if (ret)
348 return ret;
350 /* get read data (available from 0xa400) */
351 for (i = 0; i < len; i++) {
352 ret = af9005_read_ofdm_register(d, 0xa400 + i, &temp);
353 if (ret)
354 return ret;
355 values[i] = temp;
357 return 0;
360 static int af9005_i2c_write(struct dvb_usb_device *d, u8 i2caddr, u8 reg,
361 u8 * data, int len)
363 int ret, i;
364 u8 buf[3];
365 deb_i2c("i2c_write i2caddr %x, reg %x, len %d data ", i2caddr,
366 reg, len);
367 debug_dump(data, len, deb_i2c);
369 for (i = 0; i < len; i++) {
370 buf[0] = i2caddr;
371 buf[1] = reg + (u8) i;
372 buf[2] = data[i];
373 ret =
374 af9005_write_tuner_registers(d,
375 APO_REG_I2C_RW_SILICON_TUNER,
376 buf, 3);
377 if (ret) {
378 deb_i2c("i2c_write failed\n");
379 return ret;
382 deb_i2c("i2c_write ok\n");
383 return 0;
386 static int af9005_i2c_read(struct dvb_usb_device *d, u8 i2caddr, u8 reg,
387 u8 * data, int len)
389 int ret, i;
390 u8 temp;
391 deb_i2c("i2c_read i2caddr %x, reg %x, len %d\n ", i2caddr, reg, len);
392 for (i = 0; i < len; i++) {
393 temp = reg + i;
394 ret =
395 af9005_read_tuner_registers(d,
396 APO_REG_I2C_RW_SILICON_TUNER,
397 i2caddr, &temp, 1);
398 if (ret) {
399 deb_i2c("i2c_read failed\n");
400 return ret;
402 data[i] = temp;
404 deb_i2c("i2c data read: ");
405 debug_dump(data, len, deb_i2c);
406 return 0;
409 static int af9005_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
410 int num)
412 /* only implements what the mt2060 module does, don't know how
413 to make it really generic */
414 struct dvb_usb_device *d = i2c_get_adapdata(adap);
415 int ret;
416 u8 reg, addr;
417 u8 *value;
419 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
420 return -EAGAIN;
422 if (num > 2)
423 warn("more than 2 i2c messages at a time is not handled yet. TODO.");
425 if (num == 2) {
426 /* reads a single register */
427 reg = *msg[0].buf;
428 addr = msg[0].addr;
429 value = msg[1].buf;
430 ret = af9005_i2c_read(d, addr, reg, value, 1);
431 if (ret == 0)
432 ret = 2;
433 } else {
434 /* write one or more registers */
435 reg = msg[0].buf[0];
436 addr = msg[0].addr;
437 value = &msg[0].buf[1];
438 ret = af9005_i2c_write(d, addr, reg, value, msg[0].len - 1);
439 if (ret == 0)
440 ret = 1;
443 mutex_unlock(&d->i2c_mutex);
444 return ret;
447 static u32 af9005_i2c_func(struct i2c_adapter *adapter)
449 return I2C_FUNC_I2C;
452 static struct i2c_algorithm af9005_i2c_algo = {
453 .master_xfer = af9005_i2c_xfer,
454 .functionality = af9005_i2c_func,
457 int af9005_send_command(struct dvb_usb_device *d, u8 command, u8 * wbuf,
458 int wlen, u8 * rbuf, int rlen)
460 struct af9005_device_state *st = d->priv;
462 int ret, i, packet_len;
463 u8 seq;
465 if (wlen < 0) {
466 err("send command, wlen less than 0 bytes. Makes no sense.");
467 return -EINVAL;
469 if (wlen > 54) {
470 err("send command, wlen more than 54 bytes. Not supported.");
471 return -EINVAL;
473 if (rlen > 54) {
474 err("send command, rlen more than 54 bytes. Not supported.");
475 return -EINVAL;
477 packet_len = wlen + 5;
479 mutex_lock(&d->data_mutex);
481 st->data[0] = (u8) (packet_len & 0xff);
482 st->data[1] = (u8) ((packet_len & 0xff00) >> 8);
484 st->data[2] = 0x26; /* packet type */
485 st->data[3] = wlen + 3;
486 st->data[4] = seq = st->sequence++;
487 st->data[5] = command;
488 st->data[6] = wlen;
489 for (i = 0; i < wlen; i++)
490 st->data[7 + i] = wbuf[i];
491 ret = dvb_usb_generic_rw(d, st->data, wlen + 7, st->data, rlen + 7, 0);
492 if (st->data[2] != 0x27) {
493 err("send command, wrong reply code.");
494 ret = -EIO;
495 } else if (st->data[4] != seq) {
496 err("send command, wrong sequence in reply.");
497 ret = -EIO;
498 } else if (st->data[5] != 0x01) {
499 err("send command, wrong status code in reply.");
500 ret = -EIO;
501 } else if (st->data[6] != rlen) {
502 err("send command, invalid data length in reply.");
503 ret = -EIO;
505 if (!ret) {
506 for (i = 0; i < rlen; i++)
507 rbuf[i] = st->data[i + 7];
510 mutex_unlock(&d->data_mutex);
511 return ret;
514 int af9005_read_eeprom(struct dvb_usb_device *d, u8 address, u8 * values,
515 int len)
517 struct af9005_device_state *st = d->priv;
518 u8 seq;
519 int ret, i;
521 mutex_lock(&d->data_mutex);
523 memset(st->data, 0, sizeof(st->data));
525 st->data[0] = 14; /* length of rest of packet low */
526 st->data[1] = 0; /* length of rest of packer high */
528 st->data[2] = 0x2a; /* read/write eeprom */
530 st->data[3] = 12; /* size */
532 st->data[4] = seq = st->sequence++;
534 st->data[5] = 0; /* read */
536 st->data[6] = len;
537 st->data[7] = address;
538 ret = dvb_usb_generic_rw(d, st->data, 16, st->data, 14, 0);
539 if (st->data[2] != 0x2b) {
540 err("Read eeprom, invalid reply code");
541 ret = -EIO;
542 } else if (st->data[3] != 10) {
543 err("Read eeprom, invalid reply length");
544 ret = -EIO;
545 } else if (st->data[4] != seq) {
546 err("Read eeprom, wrong sequence in reply ");
547 ret = -EIO;
548 } else if (st->data[5] != 1) {
549 err("Read eeprom, wrong status in reply ");
550 ret = -EIO;
553 if (!ret) {
554 for (i = 0; i < len; i++)
555 values[i] = st->data[6 + i];
557 mutex_unlock(&d->data_mutex);
559 return ret;
562 static int af9005_boot_packet(struct usb_device *udev, int type, u8 *reply,
563 u8 *buf, int size)
565 u16 checksum;
566 int act_len, i, ret;
568 memset(buf, 0, size);
569 buf[0] = (u8) (FW_BULKOUT_SIZE & 0xff);
570 buf[1] = (u8) ((FW_BULKOUT_SIZE >> 8) & 0xff);
571 switch (type) {
572 case FW_CONFIG:
573 buf[2] = 0x11;
574 buf[3] = 0x04;
575 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
576 buf[5] = 0x03;
577 checksum = buf[4] + buf[5];
578 buf[6] = (u8) ((checksum >> 8) & 0xff);
579 buf[7] = (u8) (checksum & 0xff);
580 break;
581 case FW_CONFIRM:
582 buf[2] = 0x11;
583 buf[3] = 0x04;
584 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
585 buf[5] = 0x01;
586 checksum = buf[4] + buf[5];
587 buf[6] = (u8) ((checksum >> 8) & 0xff);
588 buf[7] = (u8) (checksum & 0xff);
589 break;
590 case FW_BOOT:
591 buf[2] = 0x10;
592 buf[3] = 0x08;
593 buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
594 buf[5] = 0x97;
595 buf[6] = 0xaa;
596 buf[7] = 0x55;
597 buf[8] = 0xa5;
598 buf[9] = 0x5a;
599 checksum = 0;
600 for (i = 4; i <= 9; i++)
601 checksum += buf[i];
602 buf[10] = (u8) ((checksum >> 8) & 0xff);
603 buf[11] = (u8) (checksum & 0xff);
604 break;
605 default:
606 err("boot packet invalid boot packet type");
607 return -EINVAL;
609 deb_fw(">>> ");
610 debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw);
612 ret = usb_bulk_msg(udev,
613 usb_sndbulkpipe(udev, 0x02),
614 buf, FW_BULKOUT_SIZE + 2, &act_len, 2000);
615 if (ret)
616 err("boot packet bulk message failed: %d (%d/%d)", ret,
617 FW_BULKOUT_SIZE + 2, act_len);
618 else
619 ret = act_len != FW_BULKOUT_SIZE + 2 ? -1 : 0;
620 if (ret)
621 return ret;
622 memset(buf, 0, 9);
623 ret = usb_bulk_msg(udev,
624 usb_rcvbulkpipe(udev, 0x01), buf, 9, &act_len, 2000);
625 if (ret) {
626 err("boot packet recv bulk message failed: %d", ret);
627 return ret;
629 deb_fw("<<< ");
630 debug_dump(buf, act_len, deb_fw);
631 checksum = 0;
632 switch (type) {
633 case FW_CONFIG:
634 if (buf[2] != 0x11) {
635 err("boot bad config header.");
636 return -EIO;
638 if (buf[3] != 0x05) {
639 err("boot bad config size.");
640 return -EIO;
642 if (buf[4] != 0x00) {
643 err("boot bad config sequence.");
644 return -EIO;
646 if (buf[5] != 0x04) {
647 err("boot bad config subtype.");
648 return -EIO;
650 for (i = 4; i <= 6; i++)
651 checksum += buf[i];
652 if (buf[7] * 256 + buf[8] != checksum) {
653 err("boot bad config checksum.");
654 return -EIO;
656 *reply = buf[6];
657 break;
658 case FW_CONFIRM:
659 if (buf[2] != 0x11) {
660 err("boot bad confirm header.");
661 return -EIO;
663 if (buf[3] != 0x05) {
664 err("boot bad confirm size.");
665 return -EIO;
667 if (buf[4] != 0x00) {
668 err("boot bad confirm sequence.");
669 return -EIO;
671 if (buf[5] != 0x02) {
672 err("boot bad confirm subtype.");
673 return -EIO;
675 for (i = 4; i <= 6; i++)
676 checksum += buf[i];
677 if (buf[7] * 256 + buf[8] != checksum) {
678 err("boot bad confirm checksum.");
679 return -EIO;
681 *reply = buf[6];
682 break;
683 case FW_BOOT:
684 if (buf[2] != 0x10) {
685 err("boot bad boot header.");
686 return -EIO;
688 if (buf[3] != 0x05) {
689 err("boot bad boot size.");
690 return -EIO;
692 if (buf[4] != 0x00) {
693 err("boot bad boot sequence.");
694 return -EIO;
696 if (buf[5] != 0x01) {
697 err("boot bad boot pattern 01.");
698 return -EIO;
700 if (buf[6] != 0x10) {
701 err("boot bad boot pattern 10.");
702 return -EIO;
704 for (i = 4; i <= 6; i++)
705 checksum += buf[i];
706 if (buf[7] * 256 + buf[8] != checksum) {
707 err("boot bad boot checksum.");
708 return -EIO;
710 break;
714 return 0;
717 static int af9005_download_firmware(struct usb_device *udev, const struct firmware *fw)
719 int i, packets, ret, act_len;
721 u8 *buf;
722 u8 reply;
724 buf = kmalloc(FW_BULKOUT_SIZE + 2, GFP_KERNEL);
725 if (!buf)
726 return -ENOMEM;
728 ret = af9005_boot_packet(udev, FW_CONFIG, &reply, buf,
729 FW_BULKOUT_SIZE + 2);
730 if (ret)
731 goto err;
732 if (reply != 0x01) {
733 err("before downloading firmware, FW_CONFIG expected 0x01, received 0x%x", reply);
734 ret = -EIO;
735 goto err;
737 packets = fw->size / FW_BULKOUT_SIZE;
738 buf[0] = (u8) (FW_BULKOUT_SIZE & 0xff);
739 buf[1] = (u8) ((FW_BULKOUT_SIZE >> 8) & 0xff);
740 for (i = 0; i < packets; i++) {
741 memcpy(&buf[2], fw->data + i * FW_BULKOUT_SIZE,
742 FW_BULKOUT_SIZE);
743 deb_fw(">>> ");
744 debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw);
745 ret = usb_bulk_msg(udev,
746 usb_sndbulkpipe(udev, 0x02),
747 buf, FW_BULKOUT_SIZE + 2, &act_len, 1000);
748 if (ret) {
749 err("firmware download failed at packet %d with code %d", i, ret);
750 goto err;
753 ret = af9005_boot_packet(udev, FW_CONFIRM, &reply,
754 buf, FW_BULKOUT_SIZE + 2);
755 if (ret)
756 goto err;
757 if (reply != (u8) (packets & 0xff)) {
758 err("after downloading firmware, FW_CONFIRM expected 0x%x, received 0x%x", packets & 0xff, reply);
759 ret = -EIO;
760 goto err;
762 ret = af9005_boot_packet(udev, FW_BOOT, &reply, buf,
763 FW_BULKOUT_SIZE + 2);
764 if (ret)
765 goto err;
766 ret = af9005_boot_packet(udev, FW_CONFIG, &reply, buf,
767 FW_BULKOUT_SIZE + 2);
768 if (ret)
769 goto err;
770 if (reply != 0x02) {
771 err("after downloading firmware, FW_CONFIG expected 0x02, received 0x%x", reply);
772 ret = -EIO;
773 goto err;
776 err:
777 kfree(buf);
778 return ret;
782 int af9005_led_control(struct dvb_usb_device *d, int onoff)
784 struct af9005_device_state *st = d->priv;
785 int temp, ret;
787 if (onoff && dvb_usb_af9005_led)
788 temp = 1;
789 else
790 temp = 0;
791 if (st->led_state != temp) {
792 ret =
793 af9005_write_register_bits(d, xd_p_reg_top_locken1,
794 reg_top_locken1_pos,
795 reg_top_locken1_len, temp);
796 if (ret)
797 return ret;
798 ret =
799 af9005_write_register_bits(d, xd_p_reg_top_lock1,
800 reg_top_lock1_pos,
801 reg_top_lock1_len, temp);
802 if (ret)
803 return ret;
804 st->led_state = temp;
806 return 0;
809 static int af9005_frontend_attach(struct dvb_usb_adapter *adap)
811 u8 buf[8];
812 int i;
814 /* without these calls the first commands after downloading
815 the firmware fail. I put these calls here to simulate
816 what it is done in dvb-usb-init.c.
818 struct usb_device *udev = adap->dev->udev;
819 usb_clear_halt(udev, usb_sndbulkpipe(udev, 2));
820 usb_clear_halt(udev, usb_rcvbulkpipe(udev, 1));
821 if (dvb_usb_af9005_dump_eeprom) {
822 printk("EEPROM DUMP\n");
823 for (i = 0; i < 255; i += 8) {
824 af9005_read_eeprom(adap->dev, i, buf, 8);
825 debug_dump(buf, 8, printk);
828 adap->fe_adap[0].fe = af9005_fe_attach(adap->dev);
829 return 0;
832 static int af9005_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
834 struct af9005_device_state *st = d->priv;
835 int ret, len;
836 u8 seq;
838 *state = REMOTE_NO_KEY_PRESSED;
839 if (rc_decode == NULL) {
840 /* it shouldn't never come here */
841 return 0;
844 mutex_lock(&d->data_mutex);
846 /* deb_info("rc_query\n"); */
847 st->data[0] = 3; /* rest of packet length low */
848 st->data[1] = 0; /* rest of packet lentgh high */
849 st->data[2] = 0x40; /* read remote */
850 st->data[3] = 1; /* rest of packet length */
851 st->data[4] = seq = st->sequence++; /* sequence number */
852 ret = dvb_usb_generic_rw(d, st->data, 5, st->data, 256, 0);
853 if (ret) {
854 err("rc query failed");
855 goto ret;
857 if (st->data[2] != 0x41) {
858 err("rc query bad header.");
859 ret = -EIO;
860 goto ret;
861 } else if (st->data[4] != seq) {
862 err("rc query bad sequence.");
863 ret = -EIO;
864 goto ret;
866 len = st->data[5];
867 if (len > 246) {
868 err("rc query invalid length");
869 ret = -EIO;
870 goto ret;
872 if (len > 0) {
873 deb_rc("rc data (%d) ", len);
874 debug_dump((st->data + 6), len, deb_rc);
875 ret = rc_decode(d, &st->data[6], len, event, state);
876 if (ret) {
877 err("rc_decode failed");
878 goto ret;
879 } else {
880 deb_rc("rc_decode state %x event %x\n", *state, *event);
881 if (*state == REMOTE_KEY_REPEAT)
882 *event = d->last_event;
886 ret:
887 mutex_unlock(&d->data_mutex);
888 return ret;
891 static int af9005_power_ctrl(struct dvb_usb_device *d, int onoff)
894 return 0;
897 static int af9005_pid_filter_control(struct dvb_usb_adapter *adap, int onoff)
899 int ret;
900 deb_info("pid filter control onoff %d\n", onoff);
901 if (onoff) {
902 ret =
903 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 1);
904 if (ret)
905 return ret;
906 ret =
907 af9005_write_register_bits(adap->dev,
908 XD_MP2IF_DMX_CTRL, 1, 1, 1);
909 if (ret)
910 return ret;
911 ret =
912 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 1);
913 } else
914 ret =
915 af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 0);
916 if (ret)
917 return ret;
918 deb_info("pid filter control ok\n");
919 return 0;
922 static int af9005_pid_filter(struct dvb_usb_adapter *adap, int index,
923 u16 pid, int onoff)
925 u8 cmd = index & 0x1f;
926 int ret;
927 deb_info("set pid filter, index %d, pid %x, onoff %d\n", index,
928 pid, onoff);
929 if (onoff) {
930 /* cannot use it as pid_filter_ctrl since it has to be done
931 before setting the first pid */
932 if (adap->feedcount == 1) {
933 deb_info("first pid set, enable pid table\n");
934 ret = af9005_pid_filter_control(adap, onoff);
935 if (ret)
936 return ret;
938 ret =
939 af9005_write_ofdm_register(adap->dev,
940 XD_MP2IF_PID_DATA_L,
941 (u8) (pid & 0xff));
942 if (ret)
943 return ret;
944 ret =
945 af9005_write_ofdm_register(adap->dev,
946 XD_MP2IF_PID_DATA_H,
947 (u8) (pid >> 8));
948 if (ret)
949 return ret;
950 cmd |= 0x20 | 0x40;
951 } else {
952 if (adap->feedcount == 0) {
953 deb_info("last pid unset, disable pid table\n");
954 ret = af9005_pid_filter_control(adap, onoff);
955 if (ret)
956 return ret;
959 ret = af9005_write_ofdm_register(adap->dev, XD_MP2IF_PID_IDX, cmd);
960 if (ret)
961 return ret;
962 deb_info("set pid ok\n");
963 return 0;
966 static int af9005_identify_state(struct usb_device *udev,
967 struct dvb_usb_device_properties *props,
968 struct dvb_usb_device_description **desc,
969 int *cold)
971 int ret;
972 u8 reply, *buf;
974 buf = kmalloc(FW_BULKOUT_SIZE + 2, GFP_KERNEL);
975 if (!buf)
976 return -ENOMEM;
978 ret = af9005_boot_packet(udev, FW_CONFIG, &reply,
979 buf, FW_BULKOUT_SIZE + 2);
980 if (ret)
981 goto err;
982 deb_info("result of FW_CONFIG in identify state %d\n", reply);
983 if (reply == 0x01)
984 *cold = 1;
985 else if (reply == 0x02)
986 *cold = 0;
987 else
988 return -EIO;
989 deb_info("Identify state cold = %d\n", *cold);
991 err:
992 kfree(buf);
993 return ret;
996 static struct dvb_usb_device_properties af9005_properties;
998 static int af9005_usb_probe(struct usb_interface *intf,
999 const struct usb_device_id *id)
1001 return dvb_usb_device_init(intf, &af9005_properties,
1002 THIS_MODULE, NULL, adapter_nr);
1005 enum af9005_usb_table_entry {
1006 AFATECH_AF9005,
1007 TERRATEC_AF9005,
1008 ANSONIC_AF9005,
1011 static struct usb_device_id af9005_usb_table[] = {
1012 [AFATECH_AF9005] = {USB_DEVICE(USB_VID_AFATECH,
1013 USB_PID_AFATECH_AF9005)},
1014 [TERRATEC_AF9005] = {USB_DEVICE(USB_VID_TERRATEC,
1015 USB_PID_TERRATEC_CINERGY_T_USB_XE)},
1016 [ANSONIC_AF9005] = {USB_DEVICE(USB_VID_ANSONIC,
1017 USB_PID_ANSONIC_DVBT_USB)},
1021 MODULE_DEVICE_TABLE(usb, af9005_usb_table);
1023 static struct dvb_usb_device_properties af9005_properties = {
1024 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1026 .usb_ctrl = DEVICE_SPECIFIC,
1027 .firmware = "af9005.fw",
1028 .download_firmware = af9005_download_firmware,
1029 .no_reconnect = 1,
1031 .size_of_priv = sizeof(struct af9005_device_state),
1033 .num_adapters = 1,
1034 .adapter = {
1036 .num_frontends = 1,
1037 .fe = {{
1038 .caps =
1039 DVB_USB_ADAP_HAS_PID_FILTER |
1040 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1041 .pid_filter_count = 32,
1042 .pid_filter = af9005_pid_filter,
1043 /* .pid_filter_ctrl = af9005_pid_filter_control, */
1044 .frontend_attach = af9005_frontend_attach,
1045 /* .tuner_attach = af9005_tuner_attach, */
1046 /* parameter for the MPEG2-data transfer */
1047 .stream = {
1048 .type = USB_BULK,
1049 .count = 10,
1050 .endpoint = 0x04,
1051 .u = {
1052 .bulk = {
1053 .buffersize = 4096, /* actual size seen is 3948 */
1060 .power_ctrl = af9005_power_ctrl,
1061 .identify_state = af9005_identify_state,
1063 .i2c_algo = &af9005_i2c_algo,
1065 .rc.legacy = {
1066 .rc_interval = 200,
1067 .rc_map_table = NULL,
1068 .rc_map_size = 0,
1069 .rc_query = af9005_rc_query,
1072 .generic_bulk_ctrl_endpoint = 2,
1073 .generic_bulk_ctrl_endpoint_response = 1,
1075 .num_device_descs = 3,
1076 .devices = {
1077 {.name = "Afatech DVB-T USB1.1 stick",
1078 .cold_ids = {&af9005_usb_table[AFATECH_AF9005], NULL},
1079 .warm_ids = {NULL},
1081 {.name = "TerraTec Cinergy T USB XE",
1082 .cold_ids = {&af9005_usb_table[TERRATEC_AF9005], NULL},
1083 .warm_ids = {NULL},
1085 {.name = "Ansonic DVB-T USB1.1 stick",
1086 .cold_ids = {&af9005_usb_table[ANSONIC_AF9005], NULL},
1087 .warm_ids = {NULL},
1089 {NULL},
1093 /* usb specific object needed to register this driver with the usb subsystem */
1094 static struct usb_driver af9005_usb_driver = {
1095 .name = "dvb_usb_af9005",
1096 .probe = af9005_usb_probe,
1097 .disconnect = dvb_usb_device_exit,
1098 .id_table = af9005_usb_table,
1101 /* module stuff */
1102 static int __init af9005_usb_module_init(void)
1104 int result;
1105 if ((result = usb_register(&af9005_usb_driver))) {
1106 err("usb_register failed. (%d)", result);
1107 return result;
1109 #if IS_MODULE(CONFIG_DVB_USB_AF9005) || defined(CONFIG_DVB_USB_AF9005_REMOTE)
1110 /* FIXME: convert to todays kernel IR infrastructure */
1111 rc_decode = symbol_request(af9005_rc_decode);
1112 rc_keys = symbol_request(rc_map_af9005_table);
1113 rc_keys_size = symbol_request(rc_map_af9005_table_size);
1114 #endif
1115 if (rc_decode == NULL || rc_keys == NULL || rc_keys_size == NULL) {
1116 err("af9005_rc_decode function not found, disabling remote");
1117 af9005_properties.rc.legacy.rc_query = NULL;
1118 } else {
1119 af9005_properties.rc.legacy.rc_map_table = rc_keys;
1120 af9005_properties.rc.legacy.rc_map_size = *rc_keys_size;
1123 return 0;
1126 static void __exit af9005_usb_module_exit(void)
1128 /* release rc decode symbols */
1129 if (rc_decode != NULL)
1130 symbol_put(af9005_rc_decode);
1131 if (rc_keys != NULL)
1132 symbol_put(rc_map_af9005_table);
1133 if (rc_keys_size != NULL)
1134 symbol_put(rc_map_af9005_table_size);
1135 /* deregister this driver from the USB subsystem */
1136 usb_deregister(&af9005_usb_driver);
1139 module_init(af9005_usb_module_init);
1140 module_exit(af9005_usb_module_exit);
1142 MODULE_AUTHOR("Luca Olivetti <luca@ventoso.org>");
1143 MODULE_DESCRIPTION("Driver for Afatech 9005 DVB-T USB1.1 stick");
1144 MODULE_VERSION("1.0");
1145 MODULE_LICENSE("GPL");