23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
33 #include <linux/input.h>
45 static int output_pva;
57 #define dprintk if (debug) printk
59 #define DRIVER_NAME "TechnoTrend/Hauppauge DEC USB"
61 #define COMMAND_PIPE 0x03
62 #define RESULT_PIPE 0x04
67 #define COMMAND_PACKET_SIZE 0x3c
68 #define ARM_PACKET_SIZE 0x1000
69 #define IRQ_PACKET_SIZE 0x8
71 #define ISO_BUF_COUNT 0x04
72 #define FRAMES_PER_ISO_BUF 0x04
73 #define ISO_FRAME_SIZE 0x0380
75 #define MAX_PVA_LENGTH 6144
175 static u16 rc_keys[] = {
204 static void ttusb_dec_set_model(
struct ttusb_dec *dec,
207 static void ttusb_dec_handle_irq(
struct urb *
urb)
213 switch(urb->status) {
221 dprintk(
"%s:urb shutting down with status: %d\n",
222 __func__, urb->status);
225 dprintk(
"%s:nonzero status received: %d\n",
226 __func__,urb->status);
230 if( (buffer[0] == 0x1) && (buffer[2] == 0x15) ) {
240 dprintk(
"%s:rc signal:%d\n", __func__, buffer[4]);
241 input_report_key(dec->
rc_input_dev, rc_keys[buffer[4] - 1], 1);
243 input_report_key(dec->
rc_input_dev, rc_keys[buffer[4] - 1], 0);
249 printk(
"%s - usb_commit_urb failed with result: %d\n",
261 crc ^= (tmp ^ (tmp << 1)) << 4;
267 int param_length,
const u8 params[],
268 int *result_length,
u8 cmd_result[])
281 printk(
"%s: Failed to lock usb mutex.\n", __func__);
291 memcpy(&b[4], params, param_length);
294 printk(
"%s: command: ", __func__);
295 for (i = 0; i < param_length + 4; i++)
304 printk(
"%s: command bulk message failed: error %d\n",
315 printk(
"%s: result bulk message failed: error %d\n",
322 printk(
"%s: result: ", __func__);
323 for (i = 0; i < actual_len; i++)
329 *result_length = b[3];
330 if (cmd_result && b[3] > 0)
331 memcpy(cmd_result, &b[4], b[3]);
340 static int ttusb_dec_get_stb_state (
struct ttusb_dec *dec,
unsigned int *
mode,
350 result = ttusb_dec_send_command(dec, 0x08, 0,
NULL, &c_length, c);
354 if (c_length >= 0x0c) {
363 if (version !=
NULL) {
365 *version =
ntohl(tmp);
373 static int ttusb_dec_audio_pes2ts_cb(
void *
priv,
unsigned char *
data)
384 static int ttusb_dec_video_pes2ts_cb(
void *priv,
unsigned char *data)
395 static void ttusb_dec_set_pids(
struct ttusb_dec *dec)
397 u8 b[] = { 0x00, 0x00, 0x00, 0x00,
398 0x00, 0x00, 0xff, 0xff,
399 0xff, 0xff, 0xff, 0xff };
411 ttusb_dec_send_command(dec, 0x50,
sizeof(b), b,
NULL,
NULL);
414 ttusb_dec_audio_pes2ts_cb, dec);
416 ttusb_dec_video_pes2ts_cb, dec);
424 printk(
"%s: packet too short - discarding\n", __func__);
429 printk(
"%s: packet too long - discarding\n", __func__);
436 int prebytes = pva[5] & 0x03;
437 int postbytes = (pva[5] & 0x0c) >> 2;
438 __be16 v_pes_payload_length;
456 dec->
v_pes[7] = 0x80;
457 dec->
v_pes[8] = 0x05;
459 dec->
v_pes[9] = 0x21 | ((pva[8] & 0xc0) >> 5);
460 dec->
v_pes[10] = ((pva[8] & 0x3f) << 2) |
461 ((pva[9] & 0xc0) >> 6);
462 dec->
v_pes[11] = 0x01 |
463 ((pva[9] & 0x3f) << 2) |
464 ((pva[10] & 0x80) >> 6);
465 dec->
v_pes[12] = ((pva[10] & 0x7f) << 1) |
466 ((pva[11] & 0xc0) >> 7);
467 dec->
v_pes[13] = 0x01 | ((pva[11] & 0x7f) << 1);
470 length - 12 - prebytes);
473 dec->
v_pes[7] = 0x00;
474 dec->
v_pes[8] = 0x00;
485 dec->
v_pes[6] = 0x84;
487 dec->
v_pes[6] = 0x80;
512 printk(
"%s: unknown PVA type: %02x.\n", __func__,
540 filter->
feed->cb.sec(&packet[2], length - 2,
NULL, 0,
544 static void ttusb_dec_process_packet(
struct ttusb_dec *dec)
551 printk(
"%s: odd sized packet - discarding\n", __func__);
559 printk(
"%s: checksum failed - discarding\n", __func__);
567 printk(
"%s: warning: lost packets between %u and %u\n",
571 if (packet_id == 0xffff)
579 ttusb_dec_process_pva(dec, dec->
packet,
585 ttusb_dec_process_filter(dec, dec->
packet,
594 static void swap_bytes(
u8 *b,
int length)
598 length -= length % 2;
599 for (;
length; b += 2, length -= 2) {
606 static void ttusb_dec_process_urb_frame(
struct ttusb_dec *dec,
u8 *b,
609 swap_bytes(b, length);
629 }
else if (*b != 0xaa) {
641 if (dec->
packet[0] ==
'A' &&
646 }
else if (dec->
packet[0] ==
'S') {
650 }
else if (dec->
packet[0] == 0x00) {
656 printk(
"%s: unknown packet type: "
657 "%02x%02x\n", __func__,
680 ((dec->
packet[3] & 0x0f) << 8) +
691 if (length >= remainder) {
719 ttusb_dec_process_packet(dec);
728 printk(
"%s: illegal packet state encountered.\n",
735 static void ttusb_dec_process_urb_frame_list(
unsigned long data)
755 ttusb_dec_process_urb_frame(dec, frame->
data, frame->
length);
760 static void ttusb_dec_process_urb(
struct urb *urb)
768 struct usb_iso_packet_descriptor *
d;
773 d = &urb->iso_frame_desc[
i];
774 b = urb->transfer_buffer + d->offset;
775 length = d->actual_length;
796 if (urb->status != -
ENOENT)
797 dprintk(
"%s: urb error: %d\n", __func__,
805 static void ttusb_dec_setup_urbs(
struct ttusb_dec *dec)
807 int i,
j, buffer_offset = 0;
815 urb->dev = dec->
udev;
817 urb->complete = ttusb_dec_process_urb;
819 urb->transfer_flags = URB_ISO_ASAP;
824 urb->transfer_buffer = dec->
iso_buffer + buffer_offset;
835 static void ttusb_dec_stop_iso_xfer(
struct ttusb_dec *dec)
858 static int ttusb_dec_set_interface(
struct ttusb_dec *dec,
870 result = ttusb_dec_send_command(dec, 0x80,
sizeof(b),
890 static int ttusb_dec_start_iso_xfer(
struct ttusb_dec *dec)
900 ttusb_dec_setup_urbs(dec);
909 printk(
"%s: failed urb submission %d: "
910 "error %d\n", __func__, i, result);
930 static int ttusb_dec_start_ts_feed(
struct dvb_demux_feed *dvbdmxfeed)
955 dprintk(
" pes_type: DMX_TS_PES_VIDEO\n");
959 ttusb_dec_set_pids(dec);
963 dprintk(
" pes_type: DMX_TS_PES_AUDIO\n");
966 ttusb_dec_set_pids(dec);
971 dprintk(
" pes_type: DMX_TS_PES_TELETEXT(not supported)\n");
975 dprintk(
" pes_type: DMX_TS_PES_PCR\n");
977 ttusb_dec_set_pids(dec);
981 dprintk(
" pes_type: DMX_TS_PES_OTHER(not supported)\n");
990 result = ttusb_dec_send_command(dec, 0x80,
sizeof(b0), b0,
NULL,
NULL);
995 return ttusb_dec_start_iso_xfer(dec);
998 static int ttusb_dec_start_sec_feed(
struct dvb_demux_feed *dvbdmxfeed)
1001 u8 b0[] = { 0x00, 0x00, 0x00, 0x01,
1002 0x00, 0x00, 0x00, 0x00,
1003 0x00, 0x00, 0x00, 0x00,
1004 0x00, 0x00, 0x00, 0x00,
1005 0x00, 0xff, 0x00, 0x00,
1006 0x00, 0x00, 0x00, 0x00,
1007 0x00, 0x00, 0x00, 0x00,
1014 unsigned long flags;
1022 memcpy(&b0[5], &dvbdmxfeed->
filter->filter.filter_value[0], 1);
1024 result = ttusb_dec_send_command(dec, 0x60,
sizeof(b0), b0,
1028 if (c_length == 2) {
1042 dvbdmxfeed->
priv = finfo;
1045 return ttusb_dec_start_iso_xfer(dec);
1053 static int ttusb_dec_start_feed(
struct dvb_demux_feed *dvbdmxfeed)
1059 if (!dvbdmx->
dmx.frontend)
1064 switch (dvbdmxfeed->
type) {
1067 return ttusb_dec_start_ts_feed(dvbdmxfeed);
1071 return ttusb_dec_start_sec_feed(dvbdmxfeed);
1075 dprintk(
" type: unknown (%d)\n", dvbdmxfeed->
type);
1081 static int ttusb_dec_stop_ts_feed(
struct dvb_demux_feed *dvbdmxfeed)
1086 ttusb_dec_send_command(dec, 0x81,
sizeof(b0), b0,
NULL,
NULL);
1090 ttusb_dec_stop_iso_xfer(dec);
1095 static int ttusb_dec_stop_sec_feed(
struct dvb_demux_feed *dvbdmxfeed)
1098 u8 b0[] = { 0x00, 0x00 };
1100 unsigned long flags;
1107 ttusb_dec_send_command(dec, 0x62,
sizeof(b0), b0,
NULL,
NULL);
1111 ttusb_dec_stop_iso_xfer(dec);
1116 static int ttusb_dec_stop_feed(
struct dvb_demux_feed *dvbdmxfeed)
1120 switch (dvbdmxfeed->
type) {
1122 return ttusb_dec_stop_ts_feed(dvbdmxfeed);
1126 return ttusb_dec_stop_sec_feed(dvbdmxfeed);
1133 static void ttusb_dec_free_iso_urbs(
struct ttusb_dec *dec)
1148 static int ttusb_dec_alloc_iso_urbs(
struct ttusb_dec *dec)
1156 (FRAMES_PER_ISO_BUF *
1161 dprintk(
"%s: pci_alloc_consistent - not enough memory\n",
1173 ttusb_dec_free_iso_urbs(dec);
1180 ttusb_dec_setup_urbs(dec);
1185 static void ttusb_dec_init_tasklet(
struct ttusb_dec *dec)
1190 (
unsigned long)dec);
1193 static int ttusb_init_rc(
struct ttusb_dec *dec)
1195 struct input_dev *input_dev;
1196 u8 b[] = { 0x00, 0x01 };
1203 input_dev = input_allocate_device();
1207 input_dev->name =
"ttusb_dec remote control";
1208 input_dev->phys = dec->
rc_phys;
1210 input_dev->keycodesize =
sizeof(
u16);
1211 input_dev->keycodemax = 0x1a;
1212 input_dev->keycode = rc_keys;
1215 set_bit(rc_keys[i], input_dev->keybit);
1217 err = input_register_device(input_dev);
1219 input_free_device(input_dev);
1225 printk(
"%s: usb_submit_urb failed\n",__func__);
1227 ttusb_dec_send_command(dec,0xb0,
sizeof(b),b,
NULL,
NULL);
1232 static void ttusb_dec_init_v_pes(
struct ttusb_dec *dec)
1236 dec->
v_pes[0] = 0x00;
1237 dec->
v_pes[1] = 0x00;
1238 dec->
v_pes[2] = 0x01;
1239 dec->
v_pes[3] = 0xe0;
1242 static int ttusb_dec_init_usb(
struct ttusb_dec *dec)
1268 ttusb_dec_handle_irq, dec, 1);
1270 dec->
irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1273 return ttusb_dec_alloc_iso_urbs(dec);
1276 static int ttusb_dec_boot_dsp(
struct ttusb_dec *dec)
1279 u8 b0[] = { 0x00, 0x00, 0x00, 0x00,
1280 0x00, 0x00, 0x00, 0x00,
1286 size_t firmware_size = 0;
1287 u16 firmware_csum = 0;
1290 u32 crc32_csum, crc32_check;
1292 const struct firmware *fw_entry =
NULL;
1302 firmware = fw_entry->
data;
1303 firmware_size = fw_entry->
size;
1305 if (firmware_size < 60) {
1306 printk(
"%s: firmware size too small for DSP code (%zu < 60).\n",
1307 __func__, firmware_size);
1315 crc32_csum =
crc32(~0L, firmware, 56) ^ ~0
L;
1316 memcpy(&tmp, &firmware[56], 4);
1317 crc32_check =
ntohl(tmp);
1318 if (crc32_csum != crc32_check) {
1319 printk(
"%s: crc32 check of DSP code failed (calculated "
1320 "0x%08x != 0x%08x in file), file invalid.\n",
1321 __func__, crc32_csum, crc32_check);
1325 memcpy(idstring, &firmware[36], 20);
1326 idstring[20] =
'\0';
1329 firmware_size_nl =
htonl(firmware_size);
1330 memcpy(b0, &firmware_size_nl, 4);
1331 firmware_csum = crc16(~0, firmware, firmware_size) ^ ~0;
1332 firmware_csum_ns =
htons(firmware_csum);
1333 memcpy(&b0[6], &firmware_csum_ns, 2);
1335 result = ttusb_dec_send_command(dec, 0x41,
sizeof(b0), b0,
NULL,
NULL);
1352 size = firmware_size -
i;
1357 b[j + 1] = trans_count++;
1360 memcpy(&b[j + 4], &firmware[i], size);
1376 result = ttusb_dec_send_command(dec, 0x43,
sizeof(b1), b1,
NULL,
NULL);
1384 static int ttusb_dec_init_stb(
struct ttusb_dec *dec)
1387 unsigned int mode = 0, model = 0, version = 0;
1391 result = ttusb_dec_get_stb_state(dec, &mode, &model, &version);
1395 if (version == 0xABCDEFAB)
1397 "info in Firmware\n");
1401 version >> 24, (version >> 16) & 0xff,
1402 (version >> 8) & 0xff, version & 0xff);
1404 result = ttusb_dec_boot_dsp(dec);
1427 "by firmware (%08x) - please report\n",
1433 if (version >= 0x01770000)
1443 static int ttusb_dec_init_dvb(
struct ttusb_dec *dec)
1453 printk(
"%s: dvb_register_adapter failed: error %d\n",
1461 dec->
demux.priv = (
void *)dec;
1462 dec->
demux.filternum = 31;
1463 dec->
demux.feednum = 31;
1464 dec->
demux.start_feed = ttusb_dec_start_feed;
1465 dec->
demux.stop_feed = ttusb_dec_stop_feed;
1469 printk(
"%s: dvb_dmx_init failed: error %d\n", __func__,
1477 dec->
dmxdev.filternum = 32;
1479 dec->
dmxdev.capabilities = 0;
1482 printk(
"%s: dvb_dmxdev_init failed: error %d\n",
1493 if ((result = dec->
demux.dmx.add_frontend(&dec->
demux.dmx,
1495 printk(
"%s: dvb_dmx_init failed: error %d\n", __func__,
1505 if ((result = dec->
demux.dmx.connect_frontend(&dec->
demux.dmx,
1507 printk(
"%s: dvb_dmx_init failed: error %d\n", __func__,
1523 static void ttusb_dec_exit_dvb(
struct ttusb_dec *dec)
1534 if (dec->
fe->ops.release)
1535 dec->
fe->ops.release(dec->
fe);
1540 static void ttusb_dec_exit_rc(
struct ttusb_dec *dec)
1563 static void ttusb_dec_exit_usb(
struct ttusb_dec *dec)
1574 ttusb_dec_free_iso_urbs(dec);
1577 static void ttusb_dec_exit_tasklet(
struct ttusb_dec *dec)
1591 static void ttusb_dec_init_filters(
struct ttusb_dec *dec)
1597 static void ttusb_dec_exit_filters(
struct ttusb_dec *dec)
1609 static int fe_send_command(
struct dvb_frontend* fe,
const u8 command,
1610 int param_length,
const u8 params[],
1611 int *result_length,
u8 cmd_result[])
1614 return ttusb_dec_send_command(dec, command, param_length, params, result_length, cmd_result);
1618 .send_command = fe_send_command
1624 struct usb_device *
udev;
1629 udev = interface_to_usbdev(intf);
1632 printk(
"%s: couldn't allocate memory.\n", __func__);
1636 usb_set_intfdata(intf, (
void *)dec);
1654 if (ttusb_dec_init_usb(dec))
1656 if (ttusb_dec_init_stb(dec)) {
1657 ttusb_dec_exit_usb(dec);
1660 ttusb_dec_init_dvb(dec);
1675 printk(
"dvb-ttusb-dec: A frontend driver was not found for device [%04x:%04x]\n",
1680 printk(
"budget-ci: Frontend registration failed!\n");
1681 if (dec->
fe->ops.release)
1682 dec->
fe->ops.release(dec->
fe);
1687 ttusb_dec_init_v_pes(dec);
1688 ttusb_dec_init_filters(dec);
1689 ttusb_dec_init_tasklet(dec);
1701 static void ttusb_dec_disconnect(
struct usb_interface *intf)
1703 struct ttusb_dec *dec = usb_get_intfdata(intf);
1705 usb_set_intfdata(intf,
NULL);
1710 ttusb_dec_exit_tasklet(dec);
1711 ttusb_dec_exit_filters(dec);
1713 ttusb_dec_exit_rc(dec);
1714 ttusb_dec_exit_usb(dec);
1715 ttusb_dec_exit_dvb(dec);
1721 static void ttusb_dec_set_model(
struct ttusb_dec *dec,
1745 {USB_DEVICE(0x0b48, 0x1006)},
1747 {USB_DEVICE(0x0b48, 0x1008)},
1748 {USB_DEVICE(0x0b48, 0x1009)},
1752 static struct usb_driver ttusb_dec_driver = {
1753 .name =
"ttusb-dec",
1754 .probe = ttusb_dec_probe,
1755 .disconnect = ttusb_dec_disconnect,
1756 .id_table = ttusb_dec_table,