Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-2.6] / drivers / media / dvb / ttusb-dec / ttusb_dec.c
1 /*
2  * TTUSB DEC Driver
3  *
4  * Copyright (C) 2003-2004 Alex Woods <linux-dvb@giblets.org>
5  * IR support by Peter Beutner <p.beutner@gmx.net>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <linux/mutex.h>
24
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/pci.h>
29 #include <linux/slab.h>
30 #include <linux/spinlock.h>
31 #include <linux/usb.h>
32 #include <linux/interrupt.h>
33 #include <linux/firmware.h>
34 #include <linux/crc32.h>
35 #include <linux/init.h>
36 #include <linux/input.h>
37
38 #include "dmxdev.h"
39 #include "dvb_demux.h"
40 #include "dvb_filter.h"
41 #include "dvb_frontend.h"
42 #include "dvb_net.h"
43 #include "ttusbdecfe.h"
44
45 static int debug;
46 static int output_pva;
47 static int enable_rc;
48
49 module_param(debug, int, 0644);
50 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
51 module_param(output_pva, int, 0444);
52 MODULE_PARM_DESC(output_pva, "Output PVA from dvr device (default:off)");
53 module_param(enable_rc, int, 0644);
54 MODULE_PARM_DESC(enable_rc, "Turn on/off IR remote control(default: off)");
55
56 #define dprintk if (debug) printk
57
58 #define DRIVER_NAME             "TechnoTrend/Hauppauge DEC USB"
59
60 #define COMMAND_PIPE            0x03
61 #define RESULT_PIPE             0x04
62 #define IN_PIPE                 0x08
63 #define OUT_PIPE                0x07
64 #define IRQ_PIPE                0x0A
65
66 #define COMMAND_PACKET_SIZE     0x3c
67 #define ARM_PACKET_SIZE         0x1000
68 #define IRQ_PACKET_SIZE         0x8
69
70 #define ISO_BUF_COUNT           0x04
71 #define FRAMES_PER_ISO_BUF      0x04
72 #define ISO_FRAME_SIZE          0x0380
73
74 #define MAX_PVA_LENGTH          6144
75
76 enum ttusb_dec_model {
77         TTUSB_DEC2000T,
78         TTUSB_DEC2540T,
79         TTUSB_DEC3000S
80 };
81
82 enum ttusb_dec_packet_type {
83         TTUSB_DEC_PACKET_PVA,
84         TTUSB_DEC_PACKET_SECTION,
85         TTUSB_DEC_PACKET_EMPTY
86 };
87
88 enum ttusb_dec_interface {
89         TTUSB_DEC_INTERFACE_INITIAL,
90         TTUSB_DEC_INTERFACE_IN,
91         TTUSB_DEC_INTERFACE_OUT
92 };
93
94 struct ttusb_dec {
95         enum ttusb_dec_model            model;
96         char                            *model_name;
97         char                            *firmware_name;
98         int                             can_playback;
99
100         /* DVB bits */
101         struct dvb_adapter              adapter;
102         struct dmxdev                   dmxdev;
103         struct dvb_demux                demux;
104         struct dmx_frontend             frontend;
105         struct dvb_net                  dvb_net;
106         struct dvb_frontend*            fe;
107
108         u16                     pid[DMX_PES_OTHER];
109
110         /* USB bits */
111         struct usb_device               *udev;
112         u8                              trans_count;
113         unsigned int                    command_pipe;
114         unsigned int                    result_pipe;
115         unsigned int                    in_pipe;
116         unsigned int                    out_pipe;
117         unsigned int                    irq_pipe;
118         enum ttusb_dec_interface        interface;
119         struct mutex                    usb_mutex;
120
121         void                    *irq_buffer;
122         struct urb              *irq_urb;
123         dma_addr_t              irq_dma_handle;
124         void                    *iso_buffer;
125         dma_addr_t              iso_dma_handle;
126         struct urb              *iso_urb[ISO_BUF_COUNT];
127         int                     iso_stream_count;
128         struct mutex            iso_mutex;
129
130         u8                              packet[MAX_PVA_LENGTH + 4];
131         enum ttusb_dec_packet_type      packet_type;
132         int                             packet_state;
133         int                             packet_length;
134         int                             packet_payload_length;
135         u16                             next_packet_id;
136
137         int                             pva_stream_count;
138         int                             filter_stream_count;
139
140         struct dvb_filter_pes2ts        a_pes2ts;
141         struct dvb_filter_pes2ts        v_pes2ts;
142
143         u8                      v_pes[16 + MAX_PVA_LENGTH];
144         int                     v_pes_length;
145         int                     v_pes_postbytes;
146
147         struct list_head        urb_frame_list;
148         struct tasklet_struct   urb_tasklet;
149         spinlock_t              urb_frame_list_lock;
150
151         struct dvb_demux_filter *audio_filter;
152         struct dvb_demux_filter *video_filter;
153         struct list_head        filter_info_list;
154         spinlock_t              filter_info_list_lock;
155
156         struct input_dev        *rc_input_dev;
157         char                    rc_phys[64];
158
159         int                     active; /* Loaded successfully */
160 };
161
162 struct urb_frame {
163         u8                      data[ISO_FRAME_SIZE];
164         int                     length;
165         struct list_head        urb_frame_list;
166 };
167
168 struct filter_info {
169         u8                      stream_id;
170         struct dvb_demux_filter *filter;
171         struct list_head        filter_info_list;
172 };
173
174 static u16 rc_keys[] = {
175         KEY_POWER,
176         KEY_MUTE,
177         KEY_1,
178         KEY_2,
179         KEY_3,
180         KEY_4,
181         KEY_5,
182         KEY_6,
183         KEY_7,
184         KEY_8,
185         KEY_9,
186         KEY_0,
187         KEY_CHANNELUP,
188         KEY_VOLUMEDOWN,
189         KEY_OK,
190         KEY_VOLUMEUP,
191         KEY_CHANNELDOWN,
192         KEY_PREVIOUS,
193         KEY_ESC,
194         KEY_RED,
195         KEY_GREEN,
196         KEY_YELLOW,
197         KEY_BLUE,
198         KEY_OPTION,
199         KEY_M,
200         KEY_RADIO
201 };
202
203 static void ttusb_dec_set_model(struct ttusb_dec *dec,
204                                 enum ttusb_dec_model model);
205
206 static void ttusb_dec_handle_irq( struct urb *urb)
207 {
208         struct ttusb_dec * dec = urb->context;
209         char *buffer = dec->irq_buffer;
210         int retval;
211
212         switch(urb->status) {
213                 case 0: /*success*/
214                         break;
215                 case -ECONNRESET:
216                 case -ENOENT:
217                 case -ESHUTDOWN:
218                 case -ETIME:
219                         /* this urb is dead, cleanup */
220                         dprintk("%s:urb shutting down with status: %d\n",
221                                         __FUNCTION__, urb->status);
222                         return;
223                 default:
224                         dprintk("%s:nonzero status received: %d\n",
225                                         __FUNCTION__,urb->status);
226                         goto exit;
227         }
228
229         if( (buffer[0] == 0x1) && (buffer[2] == 0x15) )  {
230                 /* IR - Event */
231                 /* this is an fact a bit too simple implementation;
232                  * the box also reports a keyrepeat signal
233                  * (with buffer[3] == 0x40) in an intervall of ~100ms.
234                  * But to handle this correctly we had to imlemenent some
235                  * kind of timer which signals a 'key up' event if no
236                  * keyrepeat signal is recieved for lets say 200ms.
237                  * this should/could be added later ...
238                  * for now lets report each signal as a key down and up*/
239                 dprintk("%s:rc signal:%d\n", __FUNCTION__, buffer[4]);
240                 input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 1);
241                 input_report_key(dec->rc_input_dev, rc_keys[buffer[4] - 1], 0);
242                 input_sync(dec->rc_input_dev);
243         }
244
245 exit:   retval = usb_submit_urb(urb, GFP_ATOMIC);
246         if(retval)
247                 printk("%s - usb_commit_urb failed with result: %d\n",
248                         __FUNCTION__, retval);
249 }
250
251 static u16 crc16(u16 crc, const u8 *buf, size_t len)
252 {
253         u16 tmp;
254
255         while (len--) {
256                 crc ^= *buf++;
257                 crc ^= (u8)crc >> 4;
258                 tmp = (u8)crc;
259                 crc ^= (tmp ^ (tmp << 1)) << 4;
260         }
261         return crc;
262 }
263
264 static int ttusb_dec_send_command(struct ttusb_dec *dec, const u8 command,
265                                   int param_length, const u8 params[],
266                                   int *result_length, u8 cmd_result[])
267 {
268         int result, actual_len, i;
269         u8 *b;
270
271         dprintk("%s\n", __FUNCTION__);
272
273         b = kmalloc(COMMAND_PACKET_SIZE + 4, GFP_KERNEL);
274         if (!b)
275                 return -ENOMEM;
276
277         if ((result = mutex_lock_interruptible(&dec->usb_mutex))) {
278                 kfree(b);
279                 printk("%s: Failed to lock usb mutex.\n", __FUNCTION__);
280                 return result;
281         }
282
283         b[0] = 0xaa;
284         b[1] = ++dec->trans_count;
285         b[2] = command;
286         b[3] = param_length;
287
288         if (params)
289                 memcpy(&b[4], params, param_length);
290
291         if (debug) {
292                 printk("%s: command: ", __FUNCTION__);
293                 for (i = 0; i < param_length + 4; i++)
294                         printk("0x%02X ", b[i]);
295                 printk("\n");
296         }
297
298         result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
299                               COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
300
301         if (result) {
302                 printk("%s: command bulk message failed: error %d\n",
303                        __FUNCTION__, result);
304                 mutex_unlock(&dec->usb_mutex);
305                 kfree(b);
306                 return result;
307         }
308
309         result = usb_bulk_msg(dec->udev, dec->result_pipe, b,
310                               COMMAND_PACKET_SIZE + 4, &actual_len, 1000);
311
312         if (result) {
313                 printk("%s: result bulk message failed: error %d\n",
314                        __FUNCTION__, result);
315                 mutex_unlock(&dec->usb_mutex);
316                 kfree(b);
317                 return result;
318         } else {
319                 if (debug) {
320                         printk("%s: result: ", __FUNCTION__);
321                         for (i = 0; i < actual_len; i++)
322                                 printk("0x%02X ", b[i]);
323                         printk("\n");
324                 }
325
326                 if (result_length)
327                         *result_length = b[3];
328                 if (cmd_result && b[3] > 0)
329                         memcpy(cmd_result, &b[4], b[3]);
330
331                 mutex_unlock(&dec->usb_mutex);
332
333                 kfree(b);
334                 return 0;
335         }
336 }
337
338 static int ttusb_dec_get_stb_state (struct ttusb_dec *dec, unsigned int *mode,
339                                     unsigned int *model, unsigned int *version)
340 {
341         u8 c[COMMAND_PACKET_SIZE];
342         int c_length;
343         int result;
344         unsigned int tmp;
345
346         dprintk("%s\n", __FUNCTION__);
347
348         result = ttusb_dec_send_command(dec, 0x08, 0, NULL, &c_length, c);
349         if (result)
350                 return result;
351
352         if (c_length >= 0x0c) {
353                 if (mode != NULL) {
354                         memcpy(&tmp, c, 4);
355                         *mode = ntohl(tmp);
356                 }
357                 if (model != NULL) {
358                         memcpy(&tmp, &c[4], 4);
359                         *model = ntohl(tmp);
360                 }
361                 if (version != NULL) {
362                         memcpy(&tmp, &c[8], 4);
363                         *version = ntohl(tmp);
364                 }
365                 return 0;
366         } else {
367                 return -1;
368         }
369 }
370
371 static int ttusb_dec_audio_pes2ts_cb(void *priv, unsigned char *data)
372 {
373         struct ttusb_dec *dec = priv;
374
375         dec->audio_filter->feed->cb.ts(data, 188, NULL, 0,
376                                        &dec->audio_filter->feed->feed.ts,
377                                        DMX_OK);
378
379         return 0;
380 }
381
382 static int ttusb_dec_video_pes2ts_cb(void *priv, unsigned char *data)
383 {
384         struct ttusb_dec *dec = priv;
385
386         dec->video_filter->feed->cb.ts(data, 188, NULL, 0,
387                                        &dec->video_filter->feed->feed.ts,
388                                        DMX_OK);
389
390         return 0;
391 }
392
393 static void ttusb_dec_set_pids(struct ttusb_dec *dec)
394 {
395         u8 b[] = { 0x00, 0x00, 0x00, 0x00,
396                    0x00, 0x00, 0xff, 0xff,
397                    0xff, 0xff, 0xff, 0xff };
398
399         u16 pcr = htons(dec->pid[DMX_PES_PCR]);
400         u16 audio = htons(dec->pid[DMX_PES_AUDIO]);
401         u16 video = htons(dec->pid[DMX_PES_VIDEO]);
402
403         dprintk("%s\n", __FUNCTION__);
404
405         memcpy(&b[0], &pcr, 2);
406         memcpy(&b[2], &audio, 2);
407         memcpy(&b[4], &video, 2);
408
409         ttusb_dec_send_command(dec, 0x50, sizeof(b), b, NULL, NULL);
410
411         dvb_filter_pes2ts_init(&dec->a_pes2ts, dec->pid[DMX_PES_AUDIO],
412                                ttusb_dec_audio_pes2ts_cb, dec);
413         dvb_filter_pes2ts_init(&dec->v_pes2ts, dec->pid[DMX_PES_VIDEO],
414                                ttusb_dec_video_pes2ts_cb, dec);
415         dec->v_pes_length = 0;
416         dec->v_pes_postbytes = 0;
417 }
418
419 static void ttusb_dec_process_pva(struct ttusb_dec *dec, u8 *pva, int length)
420 {
421         if (length < 8) {
422                 printk("%s: packet too short - discarding\n", __FUNCTION__);
423                 return;
424         }
425
426         if (length > 8 + MAX_PVA_LENGTH) {
427                 printk("%s: packet too long - discarding\n", __FUNCTION__);
428                 return;
429         }
430
431         switch (pva[2]) {
432
433         case 0x01: {            /* VideoStream */
434                 int prebytes = pva[5] & 0x03;
435                 int postbytes = (pva[5] & 0x0c) >> 2;
436                 u16 v_pes_payload_length;
437
438                 if (output_pva) {
439                         dec->video_filter->feed->cb.ts(pva, length, NULL, 0,
440                                 &dec->video_filter->feed->feed.ts, DMX_OK);
441                         return;
442                 }
443
444                 if (dec->v_pes_postbytes > 0 &&
445                     dec->v_pes_postbytes == prebytes) {
446                         memcpy(&dec->v_pes[dec->v_pes_length],
447                                &pva[12], prebytes);
448
449                         dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
450                                           dec->v_pes_length + prebytes, 1);
451                 }
452
453                 if (pva[5] & 0x10) {
454                         dec->v_pes[7] = 0x80;
455                         dec->v_pes[8] = 0x05;
456
457                         dec->v_pes[9] = 0x21 | ((pva[8] & 0xc0) >> 5);
458                         dec->v_pes[10] = ((pva[8] & 0x3f) << 2) |
459                                          ((pva[9] & 0xc0) >> 6);
460                         dec->v_pes[11] = 0x01 |
461                                          ((pva[9] & 0x3f) << 2) |
462                                          ((pva[10] & 0x80) >> 6);
463                         dec->v_pes[12] = ((pva[10] & 0x7f) << 1) |
464                                          ((pva[11] & 0xc0) >> 7);
465                         dec->v_pes[13] = 0x01 | ((pva[11] & 0x7f) << 1);
466
467                         memcpy(&dec->v_pes[14], &pva[12 + prebytes],
468                                length - 12 - prebytes);
469                         dec->v_pes_length = 14 + length - 12 - prebytes;
470                 } else {
471                         dec->v_pes[7] = 0x00;
472                         dec->v_pes[8] = 0x00;
473
474                         memcpy(&dec->v_pes[9], &pva[8], length - 8);
475                         dec->v_pes_length = 9 + length - 8;
476                 }
477
478                 dec->v_pes_postbytes = postbytes;
479
480                 if (dec->v_pes[9 + dec->v_pes[8]] == 0x00 &&
481                     dec->v_pes[10 + dec->v_pes[8]] == 0x00 &&
482                     dec->v_pes[11 + dec->v_pes[8]] == 0x01)
483                         dec->v_pes[6] = 0x84;
484                 else
485                         dec->v_pes[6] = 0x80;
486
487                 v_pes_payload_length = htons(dec->v_pes_length - 6 +
488                                              postbytes);
489                 memcpy(&dec->v_pes[4], &v_pes_payload_length, 2);
490
491                 if (postbytes == 0)
492                         dvb_filter_pes2ts(&dec->v_pes2ts, dec->v_pes,
493                                           dec->v_pes_length, 1);
494
495                 break;
496         }
497
498         case 0x02:              /* MainAudioStream */
499                 if (output_pva) {
500                         dec->audio_filter->feed->cb.ts(pva, length, NULL, 0,
501                                 &dec->audio_filter->feed->feed.ts, DMX_OK);
502                         return;
503                 }
504
505                 dvb_filter_pes2ts(&dec->a_pes2ts, &pva[8], length - 8,
506                                   pva[5] & 0x10);
507                 break;
508
509         default:
510                 printk("%s: unknown PVA type: %02x.\n", __FUNCTION__,
511                        pva[2]);
512                 break;
513         }
514 }
515
516 static void ttusb_dec_process_filter(struct ttusb_dec *dec, u8 *packet,
517                                      int length)
518 {
519         struct list_head *item;
520         struct filter_info *finfo;
521         struct dvb_demux_filter *filter = NULL;
522         unsigned long flags;
523         u8 sid;
524
525         sid = packet[1];
526         spin_lock_irqsave(&dec->filter_info_list_lock, flags);
527         for (item = dec->filter_info_list.next; item != &dec->filter_info_list;
528              item = item->next) {
529                 finfo = list_entry(item, struct filter_info, filter_info_list);
530                 if (finfo->stream_id == sid) {
531                         filter = finfo->filter;
532                         break;
533                 }
534         }
535         spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
536
537         if (filter)
538                 filter->feed->cb.sec(&packet[2], length - 2, NULL, 0,
539                                      &filter->filter, DMX_OK);
540 }
541
542 static void ttusb_dec_process_packet(struct ttusb_dec *dec)
543 {
544         int i;
545         u16 csum = 0;
546         u16 packet_id;
547
548         if (dec->packet_length % 2) {
549                 printk("%s: odd sized packet - discarding\n", __FUNCTION__);
550                 return;
551         }
552
553         for (i = 0; i < dec->packet_length; i += 2)
554                 csum ^= ((dec->packet[i] << 8) + dec->packet[i + 1]);
555
556         if (csum) {
557                 printk("%s: checksum failed - discarding\n", __FUNCTION__);
558                 return;
559         }
560
561         packet_id = dec->packet[dec->packet_length - 4] << 8;
562         packet_id += dec->packet[dec->packet_length - 3];
563
564         if ((packet_id != dec->next_packet_id) && dec->next_packet_id) {
565                 printk("%s: warning: lost packets between %u and %u\n",
566                        __FUNCTION__, dec->next_packet_id - 1, packet_id);
567         }
568
569         if (packet_id == 0xffff)
570                 dec->next_packet_id = 0x8000;
571         else
572                 dec->next_packet_id = packet_id + 1;
573
574         switch (dec->packet_type) {
575         case TTUSB_DEC_PACKET_PVA:
576                 if (dec->pva_stream_count)
577                         ttusb_dec_process_pva(dec, dec->packet,
578                                               dec->packet_payload_length);
579                 break;
580
581         case TTUSB_DEC_PACKET_SECTION:
582                 if (dec->filter_stream_count)
583                         ttusb_dec_process_filter(dec, dec->packet,
584                                                  dec->packet_payload_length);
585                 break;
586
587         case TTUSB_DEC_PACKET_EMPTY:
588                 break;
589         }
590 }
591
592 static void swap_bytes(u8 *b, int length)
593 {
594         u8 c;
595
596         length -= length % 2;
597         for (; length; b += 2, length -= 2) {
598                 c = *b;
599                 *b = *(b + 1);
600                 *(b + 1) = c;
601         }
602 }
603
604 static void ttusb_dec_process_urb_frame(struct ttusb_dec *dec, u8 *b,
605                                         int length)
606 {
607         swap_bytes(b, length);
608
609         while (length) {
610                 switch (dec->packet_state) {
611
612                 case 0:
613                 case 1:
614                 case 2:
615                         if (*b++ == 0xaa)
616                                 dec->packet_state++;
617                         else
618                                 dec->packet_state = 0;
619
620                         length--;
621                         break;
622
623                 case 3:
624                         if (*b == 0x00) {
625                                 dec->packet_state++;
626                                 dec->packet_length = 0;
627                         } else if (*b != 0xaa) {
628                                 dec->packet_state = 0;
629                         }
630
631                         b++;
632                         length--;
633                         break;
634
635                 case 4:
636                         dec->packet[dec->packet_length++] = *b++;
637
638                         if (dec->packet_length == 2) {
639                                 if (dec->packet[0] == 'A' &&
640                                     dec->packet[1] == 'V') {
641                                         dec->packet_type =
642                                                 TTUSB_DEC_PACKET_PVA;
643                                         dec->packet_state++;
644                                 } else if (dec->packet[0] == 'S') {
645                                         dec->packet_type =
646                                                 TTUSB_DEC_PACKET_SECTION;
647                                         dec->packet_state++;
648                                 } else if (dec->packet[0] == 0x00) {
649                                         dec->packet_type =
650                                                 TTUSB_DEC_PACKET_EMPTY;
651                                         dec->packet_payload_length = 2;
652                                         dec->packet_state = 7;
653                                 } else {
654                                         printk("%s: unknown packet type: "
655                                                "%02x%02x\n", __FUNCTION__,
656                                                dec->packet[0], dec->packet[1]);
657                                         dec->packet_state = 0;
658                                 }
659                         }
660
661                         length--;
662                         break;
663
664                 case 5:
665                         dec->packet[dec->packet_length++] = *b++;
666
667                         if (dec->packet_type == TTUSB_DEC_PACKET_PVA &&
668                             dec->packet_length == 8) {
669                                 dec->packet_state++;
670                                 dec->packet_payload_length = 8 +
671                                         (dec->packet[6] << 8) +
672                                         dec->packet[7];
673                         } else if (dec->packet_type ==
674                                         TTUSB_DEC_PACKET_SECTION &&
675                                    dec->packet_length == 5) {
676                                 dec->packet_state++;
677                                 dec->packet_payload_length = 5 +
678                                         ((dec->packet[3] & 0x0f) << 8) +
679                                         dec->packet[4];
680                         }
681
682                         length--;
683                         break;
684
685                 case 6: {
686                         int remainder = dec->packet_payload_length -
687                                         dec->packet_length;
688
689                         if (length >= remainder) {
690                                 memcpy(dec->packet + dec->packet_length,
691                                        b, remainder);
692                                 dec->packet_length += remainder;
693                                 b += remainder;
694                                 length -= remainder;
695                                 dec->packet_state++;
696                         } else {
697                                 memcpy(&dec->packet[dec->packet_length],
698                                        b, length);
699                                 dec->packet_length += length;
700                                 length = 0;
701                         }
702
703                         break;
704                 }
705
706                 case 7: {
707                         int tail = 4;
708
709                         dec->packet[dec->packet_length++] = *b++;
710
711                         if (dec->packet_type == TTUSB_DEC_PACKET_SECTION &&
712                             dec->packet_payload_length % 2)
713                                 tail++;
714
715                         if (dec->packet_length ==
716                             dec->packet_payload_length + tail) {
717                                 ttusb_dec_process_packet(dec);
718                                 dec->packet_state = 0;
719                         }
720
721                         length--;
722                         break;
723                 }
724
725                 default:
726                         printk("%s: illegal packet state encountered.\n",
727                                __FUNCTION__);
728                         dec->packet_state = 0;
729                 }
730         }
731 }
732
733 static void ttusb_dec_process_urb_frame_list(unsigned long data)
734 {
735         struct ttusb_dec *dec = (struct ttusb_dec *)data;
736         struct list_head *item;
737         struct urb_frame *frame;
738         unsigned long flags;
739
740         while (1) {
741                 spin_lock_irqsave(&dec->urb_frame_list_lock, flags);
742                 if ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
743                         frame = list_entry(item, struct urb_frame,
744                                            urb_frame_list);
745                         list_del(&frame->urb_frame_list);
746                 } else {
747                         spin_unlock_irqrestore(&dec->urb_frame_list_lock,
748                                                flags);
749                         return;
750                 }
751                 spin_unlock_irqrestore(&dec->urb_frame_list_lock, flags);
752
753                 ttusb_dec_process_urb_frame(dec, frame->data, frame->length);
754                 kfree(frame);
755         }
756 }
757
758 static void ttusb_dec_process_urb(struct urb *urb)
759 {
760         struct ttusb_dec *dec = urb->context;
761
762         if (!urb->status) {
763                 int i;
764
765                 for (i = 0; i < FRAMES_PER_ISO_BUF; i++) {
766                         struct usb_iso_packet_descriptor *d;
767                         u8 *b;
768                         int length;
769                         struct urb_frame *frame;
770
771                         d = &urb->iso_frame_desc[i];
772                         b = urb->transfer_buffer + d->offset;
773                         length = d->actual_length;
774
775                         if ((frame = kmalloc(sizeof(struct urb_frame),
776                                              GFP_ATOMIC))) {
777                                 unsigned long flags;
778
779                                 memcpy(frame->data, b, length);
780                                 frame->length = length;
781
782                                 spin_lock_irqsave(&dec->urb_frame_list_lock,
783                                                      flags);
784                                 list_add_tail(&frame->urb_frame_list,
785                                               &dec->urb_frame_list);
786                                 spin_unlock_irqrestore(&dec->urb_frame_list_lock,
787                                                        flags);
788
789                                 tasklet_schedule(&dec->urb_tasklet);
790                         }
791                 }
792         } else {
793                  /* -ENOENT is expected when unlinking urbs */
794                 if (urb->status != -ENOENT)
795                         dprintk("%s: urb error: %d\n", __FUNCTION__,
796                                 urb->status);
797         }
798
799         if (dec->iso_stream_count)
800                 usb_submit_urb(urb, GFP_ATOMIC);
801 }
802
803 static void ttusb_dec_setup_urbs(struct ttusb_dec *dec)
804 {
805         int i, j, buffer_offset = 0;
806
807         dprintk("%s\n", __FUNCTION__);
808
809         for (i = 0; i < ISO_BUF_COUNT; i++) {
810                 int frame_offset = 0;
811                 struct urb *urb = dec->iso_urb[i];
812
813                 urb->dev = dec->udev;
814                 urb->context = dec;
815                 urb->complete = ttusb_dec_process_urb;
816                 urb->pipe = dec->in_pipe;
817                 urb->transfer_flags = URB_ISO_ASAP;
818                 urb->interval = 1;
819                 urb->number_of_packets = FRAMES_PER_ISO_BUF;
820                 urb->transfer_buffer_length = ISO_FRAME_SIZE *
821                                               FRAMES_PER_ISO_BUF;
822                 urb->transfer_buffer = dec->iso_buffer + buffer_offset;
823                 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
824
825                 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
826                         urb->iso_frame_desc[j].offset = frame_offset;
827                         urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
828                         frame_offset += ISO_FRAME_SIZE;
829                 }
830         }
831 }
832
833 static void ttusb_dec_stop_iso_xfer(struct ttusb_dec *dec)
834 {
835         int i;
836
837         dprintk("%s\n", __FUNCTION__);
838
839         if (mutex_lock_interruptible(&dec->iso_mutex))
840                 return;
841
842         dec->iso_stream_count--;
843
844         if (!dec->iso_stream_count) {
845                 for (i = 0; i < ISO_BUF_COUNT; i++)
846                         usb_kill_urb(dec->iso_urb[i]);
847         }
848
849         mutex_unlock(&dec->iso_mutex);
850 }
851
852 /* Setting the interface of the DEC tends to take down the USB communications
853  * for a short period, so it's important not to call this function just before
854  * trying to talk to it.
855  */
856 static int ttusb_dec_set_interface(struct ttusb_dec *dec,
857                                    enum ttusb_dec_interface interface)
858 {
859         int result = 0;
860         u8 b[] = { 0x05 };
861
862         if (interface != dec->interface) {
863                 switch (interface) {
864                 case TTUSB_DEC_INTERFACE_INITIAL:
865                         result = usb_set_interface(dec->udev, 0, 0);
866                         break;
867                 case TTUSB_DEC_INTERFACE_IN:
868                         result = ttusb_dec_send_command(dec, 0x80, sizeof(b),
869                                                         b, NULL, NULL);
870                         if (result)
871                                 return result;
872                         result = usb_set_interface(dec->udev, 0, 8);
873                         break;
874                 case TTUSB_DEC_INTERFACE_OUT:
875                         result = usb_set_interface(dec->udev, 0, 1);
876                         break;
877                 }
878
879                 if (result)
880                         return result;
881
882                 dec->interface = interface;
883         }
884
885         return 0;
886 }
887
888 static int ttusb_dec_start_iso_xfer(struct ttusb_dec *dec)
889 {
890         int i, result;
891
892         dprintk("%s\n", __FUNCTION__);
893
894         if (mutex_lock_interruptible(&dec->iso_mutex))
895                 return -EAGAIN;
896
897         if (!dec->iso_stream_count) {
898                 ttusb_dec_setup_urbs(dec);
899
900                 dec->packet_state = 0;
901                 dec->v_pes_postbytes = 0;
902                 dec->next_packet_id = 0;
903
904                 for (i = 0; i < ISO_BUF_COUNT; i++) {
905                         if ((result = usb_submit_urb(dec->iso_urb[i],
906                                                      GFP_ATOMIC))) {
907                                 printk("%s: failed urb submission %d: "
908                                        "error %d\n", __FUNCTION__, i, result);
909
910                                 while (i) {
911                                         usb_kill_urb(dec->iso_urb[i - 1]);
912                                         i--;
913                                 }
914
915                                 mutex_unlock(&dec->iso_mutex);
916                                 return result;
917                         }
918                 }
919         }
920
921         dec->iso_stream_count++;
922
923         mutex_unlock(&dec->iso_mutex);
924
925         return 0;
926 }
927
928 static int ttusb_dec_start_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
929 {
930         struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
931         struct ttusb_dec *dec = dvbdmx->priv;
932         u8 b0[] = { 0x05 };
933         int result = 0;
934
935         dprintk("%s\n", __FUNCTION__);
936
937         dprintk("  ts_type:");
938
939         if (dvbdmxfeed->ts_type & TS_DECODER)
940                 dprintk(" TS_DECODER");
941
942         if (dvbdmxfeed->ts_type & TS_PACKET)
943                 dprintk(" TS_PACKET");
944
945         if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
946                 dprintk(" TS_PAYLOAD_ONLY");
947
948         dprintk("\n");
949
950         switch (dvbdmxfeed->pes_type) {
951
952         case DMX_TS_PES_VIDEO:
953                 dprintk("  pes_type: DMX_TS_PES_VIDEO\n");
954                 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
955                 dec->pid[DMX_PES_VIDEO] = dvbdmxfeed->pid;
956                 dec->video_filter = dvbdmxfeed->filter;
957                 ttusb_dec_set_pids(dec);
958                 break;
959
960         case DMX_TS_PES_AUDIO:
961                 dprintk("  pes_type: DMX_TS_PES_AUDIO\n");
962                 dec->pid[DMX_PES_AUDIO] = dvbdmxfeed->pid;
963                 dec->audio_filter = dvbdmxfeed->filter;
964                 ttusb_dec_set_pids(dec);
965                 break;
966
967         case DMX_TS_PES_TELETEXT:
968                 dec->pid[DMX_PES_TELETEXT] = dvbdmxfeed->pid;
969                 dprintk("  pes_type: DMX_TS_PES_TELETEXT(not supported)\n");
970                 return -ENOSYS;
971
972         case DMX_TS_PES_PCR:
973                 dprintk("  pes_type: DMX_TS_PES_PCR\n");
974                 dec->pid[DMX_PES_PCR] = dvbdmxfeed->pid;
975                 ttusb_dec_set_pids(dec);
976                 break;
977
978         case DMX_TS_PES_OTHER:
979                 dprintk("  pes_type: DMX_TS_PES_OTHER(not supported)\n");
980                 return -ENOSYS;
981
982         default:
983                 dprintk("  pes_type: unknown (%d)\n", dvbdmxfeed->pes_type);
984                 return -EINVAL;
985
986         }
987
988         result = ttusb_dec_send_command(dec, 0x80, sizeof(b0), b0, NULL, NULL);
989         if (result)
990                 return result;
991
992         dec->pva_stream_count++;
993         return ttusb_dec_start_iso_xfer(dec);
994 }
995
996 static int ttusb_dec_start_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
997 {
998         struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
999         u8 b0[] = { 0x00, 0x00, 0x00, 0x01,
1000                     0x00, 0x00, 0x00, 0x00,
1001                     0x00, 0x00, 0x00, 0x00,
1002                     0x00, 0x00, 0x00, 0x00,
1003                     0x00, 0xff, 0x00, 0x00,
1004                     0x00, 0x00, 0x00, 0x00,
1005                     0x00, 0x00, 0x00, 0x00,
1006                     0x00 };
1007         u16 pid;
1008         u8 c[COMMAND_PACKET_SIZE];
1009         int c_length;
1010         int result;
1011         struct filter_info *finfo;
1012         unsigned long flags;
1013         u8 x = 1;
1014
1015         dprintk("%s\n", __FUNCTION__);
1016
1017         pid = htons(dvbdmxfeed->pid);
1018         memcpy(&b0[0], &pid, 2);
1019         memcpy(&b0[4], &x, 1);
1020         memcpy(&b0[5], &dvbdmxfeed->filter->filter.filter_value[0], 1);
1021
1022         result = ttusb_dec_send_command(dec, 0x60, sizeof(b0), b0,
1023                                         &c_length, c);
1024
1025         if (!result) {
1026                 if (c_length == 2) {
1027                         if (!(finfo = kmalloc(sizeof(struct filter_info),
1028                                               GFP_ATOMIC)))
1029                                 return -ENOMEM;
1030
1031                         finfo->stream_id = c[1];
1032                         finfo->filter = dvbdmxfeed->filter;
1033
1034                         spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1035                         list_add_tail(&finfo->filter_info_list,
1036                                       &dec->filter_info_list);
1037                         spin_unlock_irqrestore(&dec->filter_info_list_lock,
1038                                                flags);
1039
1040                         dvbdmxfeed->priv = finfo;
1041
1042                         dec->filter_stream_count++;
1043                         return ttusb_dec_start_iso_xfer(dec);
1044                 }
1045
1046                 return -EAGAIN;
1047         } else
1048                 return result;
1049 }
1050
1051 static int ttusb_dec_start_feed(struct dvb_demux_feed *dvbdmxfeed)
1052 {
1053         struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1054
1055         dprintk("%s\n", __FUNCTION__);
1056
1057         if (!dvbdmx->dmx.frontend)
1058                 return -EINVAL;
1059
1060         dprintk("  pid: 0x%04X\n", dvbdmxfeed->pid);
1061
1062         switch (dvbdmxfeed->type) {
1063
1064         case DMX_TYPE_TS:
1065                 return ttusb_dec_start_ts_feed(dvbdmxfeed);
1066                 break;
1067
1068         case DMX_TYPE_SEC:
1069                 return ttusb_dec_start_sec_feed(dvbdmxfeed);
1070                 break;
1071
1072         default:
1073                 dprintk("  type: unknown (%d)\n", dvbdmxfeed->type);
1074                 return -EINVAL;
1075
1076         }
1077 }
1078
1079 static int ttusb_dec_stop_ts_feed(struct dvb_demux_feed *dvbdmxfeed)
1080 {
1081         struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1082         u8 b0[] = { 0x00 };
1083
1084         ttusb_dec_send_command(dec, 0x81, sizeof(b0), b0, NULL, NULL);
1085
1086         dec->pva_stream_count--;
1087
1088         ttusb_dec_stop_iso_xfer(dec);
1089
1090         return 0;
1091 }
1092
1093 static int ttusb_dec_stop_sec_feed(struct dvb_demux_feed *dvbdmxfeed)
1094 {
1095         struct ttusb_dec *dec = dvbdmxfeed->demux->priv;
1096         u8 b0[] = { 0x00, 0x00 };
1097         struct filter_info *finfo = (struct filter_info *)dvbdmxfeed->priv;
1098         unsigned long flags;
1099
1100         b0[1] = finfo->stream_id;
1101         spin_lock_irqsave(&dec->filter_info_list_lock, flags);
1102         list_del(&finfo->filter_info_list);
1103         spin_unlock_irqrestore(&dec->filter_info_list_lock, flags);
1104         kfree(finfo);
1105         ttusb_dec_send_command(dec, 0x62, sizeof(b0), b0, NULL, NULL);
1106
1107         dec->filter_stream_count--;
1108
1109         ttusb_dec_stop_iso_xfer(dec);
1110
1111         return 0;
1112 }
1113
1114 static int ttusb_dec_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
1115 {
1116         dprintk("%s\n", __FUNCTION__);
1117
1118         switch (dvbdmxfeed->type) {
1119         case DMX_TYPE_TS:
1120                 return ttusb_dec_stop_ts_feed(dvbdmxfeed);
1121                 break;
1122
1123         case DMX_TYPE_SEC:
1124                 return ttusb_dec_stop_sec_feed(dvbdmxfeed);
1125                 break;
1126         }
1127
1128         return 0;
1129 }
1130
1131 static void ttusb_dec_free_iso_urbs(struct ttusb_dec *dec)
1132 {
1133         int i;
1134
1135         dprintk("%s\n", __FUNCTION__);
1136
1137         for (i = 0; i < ISO_BUF_COUNT; i++)
1138                 usb_free_urb(dec->iso_urb[i]);
1139
1140         pci_free_consistent(NULL,
1141                             ISO_FRAME_SIZE * (FRAMES_PER_ISO_BUF *
1142                                               ISO_BUF_COUNT),
1143                             dec->iso_buffer, dec->iso_dma_handle);
1144 }
1145
1146 static int ttusb_dec_alloc_iso_urbs(struct ttusb_dec *dec)
1147 {
1148         int i;
1149
1150         dprintk("%s\n", __FUNCTION__);
1151
1152         dec->iso_buffer = pci_alloc_consistent(NULL,
1153                                                ISO_FRAME_SIZE *
1154                                                (FRAMES_PER_ISO_BUF *
1155                                                 ISO_BUF_COUNT),
1156                                                &dec->iso_dma_handle);
1157
1158         memset(dec->iso_buffer, 0,
1159                ISO_FRAME_SIZE * (FRAMES_PER_ISO_BUF * ISO_BUF_COUNT));
1160
1161         for (i = 0; i < ISO_BUF_COUNT; i++) {
1162                 struct urb *urb;
1163
1164                 if (!(urb = usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
1165                         ttusb_dec_free_iso_urbs(dec);
1166                         return -ENOMEM;
1167                 }
1168
1169                 dec->iso_urb[i] = urb;
1170         }
1171
1172         ttusb_dec_setup_urbs(dec);
1173
1174         return 0;
1175 }
1176
1177 static void ttusb_dec_init_tasklet(struct ttusb_dec *dec)
1178 {
1179         spin_lock_init(&dec->urb_frame_list_lock);
1180         INIT_LIST_HEAD(&dec->urb_frame_list);
1181         tasklet_init(&dec->urb_tasklet, ttusb_dec_process_urb_frame_list,
1182                      (unsigned long)dec);
1183 }
1184
1185 static int ttusb_init_rc( struct ttusb_dec *dec)
1186 {
1187         struct input_dev *input_dev;
1188         u8 b[] = { 0x00, 0x01 };
1189         int i;
1190
1191         usb_make_path(dec->udev, dec->rc_phys, sizeof(dec->rc_phys));
1192         strlcpy(dec->rc_phys, "/input0", sizeof(dec->rc_phys));
1193
1194         dec->rc_input_dev = input_dev = input_allocate_device();
1195         if (!input_dev)
1196                 return -ENOMEM;
1197
1198         input_dev->name = "ttusb_dec remote control";
1199         input_dev->phys = dec->rc_phys;
1200         input_dev->evbit[0] = BIT(EV_KEY);
1201         input_dev->keycodesize = sizeof(u16);
1202         input_dev->keycodemax = 0x1a;
1203         input_dev->keycode = rc_keys;
1204
1205         for (i = 0; i < ARRAY_SIZE(rc_keys); i++)
1206                   set_bit(rc_keys[i], input_dev->keybit);
1207
1208         input_register_device(input_dev);
1209
1210         if (usb_submit_urb(dec->irq_urb, GFP_KERNEL))
1211                 printk("%s: usb_submit_urb failed\n",__FUNCTION__);
1212         /* enable irq pipe */
1213         ttusb_dec_send_command(dec,0xb0,sizeof(b),b,NULL,NULL);
1214
1215         return 0;
1216 }
1217
1218 static void ttusb_dec_init_v_pes(struct ttusb_dec *dec)
1219 {
1220         dprintk("%s\n", __FUNCTION__);
1221
1222         dec->v_pes[0] = 0x00;
1223         dec->v_pes[1] = 0x00;
1224         dec->v_pes[2] = 0x01;
1225         dec->v_pes[3] = 0xe0;
1226 }
1227
1228 static int ttusb_dec_init_usb(struct ttusb_dec *dec)
1229 {
1230         dprintk("%s\n", __FUNCTION__);
1231
1232         mutex_init(&dec->usb_mutex);
1233         mutex_init(&dec->iso_mutex);
1234
1235         dec->command_pipe = usb_sndbulkpipe(dec->udev, COMMAND_PIPE);
1236         dec->result_pipe = usb_rcvbulkpipe(dec->udev, RESULT_PIPE);
1237         dec->in_pipe = usb_rcvisocpipe(dec->udev, IN_PIPE);
1238         dec->out_pipe = usb_sndisocpipe(dec->udev, OUT_PIPE);
1239         dec->irq_pipe = usb_rcvintpipe(dec->udev, IRQ_PIPE);
1240
1241         if(enable_rc) {
1242                 dec->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
1243                 if(!dec->irq_urb) {
1244                         return -ENOMEM;
1245                 }
1246                 dec->irq_buffer = usb_buffer_alloc(dec->udev,IRQ_PACKET_SIZE,
1247                                         SLAB_ATOMIC, &dec->irq_dma_handle);
1248                 if(!dec->irq_buffer) {
1249                         return -ENOMEM;
1250                 }
1251                 usb_fill_int_urb(dec->irq_urb, dec->udev,dec->irq_pipe,
1252                                  dec->irq_buffer, IRQ_PACKET_SIZE,
1253                                  ttusb_dec_handle_irq, dec, 1);
1254                 dec->irq_urb->transfer_dma = dec->irq_dma_handle;
1255                 dec->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1256         }
1257
1258         return ttusb_dec_alloc_iso_urbs(dec);
1259 }
1260
1261 static int ttusb_dec_boot_dsp(struct ttusb_dec *dec)
1262 {
1263         int i, j, actual_len, result, size, trans_count;
1264         u8 b0[] = { 0x00, 0x00, 0x00, 0x00,
1265                     0x00, 0x00, 0x00, 0x00,
1266                     0x61, 0x00 };
1267         u8 b1[] = { 0x61 };
1268         u8 *b;
1269         char idstring[21];
1270         u8 *firmware = NULL;
1271         size_t firmware_size = 0;
1272         u16 firmware_csum = 0;
1273         u16 firmware_csum_ns;
1274         u32 firmware_size_nl;
1275         u32 crc32_csum, crc32_check, tmp;
1276         const struct firmware *fw_entry = NULL;
1277
1278         dprintk("%s\n", __FUNCTION__);
1279
1280         if (request_firmware(&fw_entry, dec->firmware_name, &dec->udev->dev)) {
1281                 printk(KERN_ERR "%s: Firmware (%s) unavailable.\n",
1282                        __FUNCTION__, dec->firmware_name);
1283                 return 1;
1284         }
1285
1286         firmware = fw_entry->data;
1287         firmware_size = fw_entry->size;
1288
1289         if (firmware_size < 60) {
1290                 printk("%s: firmware size too small for DSP code (%zu < 60).\n",
1291                         __FUNCTION__, firmware_size);
1292                 release_firmware(fw_entry);
1293                 return -1;
1294         }
1295
1296         /* a 32 bit checksum over the first 56 bytes of the DSP Code is stored
1297            at offset 56 of file, so use it to check if the firmware file is
1298            valid. */
1299         crc32_csum = crc32(~0L, firmware, 56) ^ ~0L;
1300         memcpy(&tmp, &firmware[56], 4);
1301         crc32_check = htonl(tmp);
1302         if (crc32_csum != crc32_check) {
1303                 printk("%s: crc32 check of DSP code failed (calculated "
1304                        "0x%08x != 0x%08x in file), file invalid.\n",
1305                         __FUNCTION__, crc32_csum, crc32_check);
1306                 release_firmware(fw_entry);
1307                 return -1;
1308         }
1309         memcpy(idstring, &firmware[36], 20);
1310         idstring[20] = '\0';
1311         printk(KERN_INFO "ttusb_dec: found DSP code \"%s\".\n", idstring);
1312
1313         firmware_size_nl = htonl(firmware_size);
1314         memcpy(b0, &firmware_size_nl, 4);
1315         firmware_csum = crc16(~0, firmware, firmware_size) ^ ~0;
1316         firmware_csum_ns = htons(firmware_csum);
1317         memcpy(&b0[6], &firmware_csum_ns, 2);
1318
1319         result = ttusb_dec_send_command(dec, 0x41, sizeof(b0), b0, NULL, NULL);
1320
1321         if (result) {
1322                 release_firmware(fw_entry);
1323                 return result;
1324         }
1325
1326         trans_count = 0;
1327         j = 0;
1328
1329         b = kmalloc(ARM_PACKET_SIZE, GFP_KERNEL);
1330         if (b == NULL) {
1331                 release_firmware(fw_entry);
1332                 return -ENOMEM;
1333         }
1334
1335         for (i = 0; i < firmware_size; i += COMMAND_PACKET_SIZE) {
1336                 size = firmware_size - i;
1337                 if (size > COMMAND_PACKET_SIZE)
1338                         size = COMMAND_PACKET_SIZE;
1339
1340                 b[j + 0] = 0xaa;
1341                 b[j + 1] = trans_count++;
1342                 b[j + 2] = 0xf0;
1343                 b[j + 3] = size;
1344                 memcpy(&b[j + 4], &firmware[i], size);
1345
1346                 j += COMMAND_PACKET_SIZE + 4;
1347
1348                 if (j >= ARM_PACKET_SIZE) {
1349                         result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1350                                               ARM_PACKET_SIZE, &actual_len,
1351                                               100);
1352                         j = 0;
1353                 } else if (size < COMMAND_PACKET_SIZE) {
1354                         result = usb_bulk_msg(dec->udev, dec->command_pipe, b,
1355                                               j - COMMAND_PACKET_SIZE + size,
1356                                               &actual_len, 100);
1357                 }
1358         }
1359
1360         result = ttusb_dec_send_command(dec, 0x43, sizeof(b1), b1, NULL, NULL);
1361
1362         release_firmware(fw_entry);
1363         kfree(b);
1364
1365         return result;
1366 }
1367
1368 static int ttusb_dec_init_stb(struct ttusb_dec *dec)
1369 {
1370         int result;
1371         unsigned int mode, model, version;
1372
1373         dprintk("%s\n", __FUNCTION__);
1374
1375         result = ttusb_dec_get_stb_state(dec, &mode, &model, &version);
1376
1377         if (!result) {
1378                 if (!mode) {
1379                         if (version == 0xABCDEFAB)
1380                                 printk(KERN_INFO "ttusb_dec: no version "
1381                                        "info in Firmware\n");
1382                         else
1383                                 printk(KERN_INFO "ttusb_dec: Firmware "
1384                                        "%x.%02x%c%c\n",
1385                                        version >> 24, (version >> 16) & 0xff,
1386                                        (version >> 8) & 0xff, version & 0xff);
1387
1388                         result = ttusb_dec_boot_dsp(dec);
1389                         if (result)
1390                                 return result;
1391                         else
1392                                 return 1;
1393                 } else {
1394                         /* We can't trust the USB IDs that some firmwares
1395                            give the box */
1396                         switch (model) {
1397                         case 0x00070001:
1398                         case 0x00070008:
1399                         case 0x0007000c:
1400                                 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1401                                 break;
1402                         case 0x00070009:
1403                         case 0x00070013:
1404                                 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1405                                 break;
1406                         case 0x00070011:
1407                                 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1408                                 break;
1409                         default:
1410                                 printk(KERN_ERR "%s: unknown model returned "
1411                                        "by firmware (%08x) - please report\n",
1412                                        __FUNCTION__, model);
1413                                 return -1;
1414                                 break;
1415                         }
1416
1417                         if (version >= 0x01770000)
1418                                 dec->can_playback = 1;
1419
1420                         return 0;
1421                 }
1422         }
1423         else
1424                 return result;
1425 }
1426
1427 static int ttusb_dec_init_dvb(struct ttusb_dec *dec)
1428 {
1429         int result;
1430
1431         dprintk("%s\n", __FUNCTION__);
1432
1433         if ((result = dvb_register_adapter(&dec->adapter,
1434                                            dec->model_name, THIS_MODULE, &dec->udev->dev)) < 0) {
1435                 printk("%s: dvb_register_adapter failed: error %d\n",
1436                        __FUNCTION__, result);
1437
1438                 return result;
1439         }
1440
1441         dec->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1442
1443         dec->demux.priv = (void *)dec;
1444         dec->demux.filternum = 31;
1445         dec->demux.feednum = 31;
1446         dec->demux.start_feed = ttusb_dec_start_feed;
1447         dec->demux.stop_feed = ttusb_dec_stop_feed;
1448         dec->demux.write_to_decoder = NULL;
1449
1450         if ((result = dvb_dmx_init(&dec->demux)) < 0) {
1451                 printk("%s: dvb_dmx_init failed: error %d\n", __FUNCTION__,
1452                        result);
1453
1454                 dvb_unregister_adapter(&dec->adapter);
1455
1456                 return result;
1457         }
1458
1459         dec->dmxdev.filternum = 32;
1460         dec->dmxdev.demux = &dec->demux.dmx;
1461         dec->dmxdev.capabilities = 0;
1462
1463         if ((result = dvb_dmxdev_init(&dec->dmxdev, &dec->adapter)) < 0) {
1464                 printk("%s: dvb_dmxdev_init failed: error %d\n",
1465                        __FUNCTION__, result);
1466
1467                 dvb_dmx_release(&dec->demux);
1468                 dvb_unregister_adapter(&dec->adapter);
1469
1470                 return result;
1471         }
1472
1473         dec->frontend.source = DMX_FRONTEND_0;
1474
1475         if ((result = dec->demux.dmx.add_frontend(&dec->demux.dmx,
1476                                                   &dec->frontend)) < 0) {
1477                 printk("%s: dvb_dmx_init failed: error %d\n", __FUNCTION__,
1478                        result);
1479
1480                 dvb_dmxdev_release(&dec->dmxdev);
1481                 dvb_dmx_release(&dec->demux);
1482                 dvb_unregister_adapter(&dec->adapter);
1483
1484                 return result;
1485         }
1486
1487         if ((result = dec->demux.dmx.connect_frontend(&dec->demux.dmx,
1488                                                       &dec->frontend)) < 0) {
1489                 printk("%s: dvb_dmx_init failed: error %d\n", __FUNCTION__,
1490                        result);
1491
1492                 dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1493                 dvb_dmxdev_release(&dec->dmxdev);
1494                 dvb_dmx_release(&dec->demux);
1495                 dvb_unregister_adapter(&dec->adapter);
1496
1497                 return result;
1498         }
1499
1500         dvb_net_init(&dec->adapter, &dec->dvb_net, &dec->demux.dmx);
1501
1502         return 0;
1503 }
1504
1505 static void ttusb_dec_exit_dvb(struct ttusb_dec *dec)
1506 {
1507         dprintk("%s\n", __FUNCTION__);
1508
1509         dvb_net_release(&dec->dvb_net);
1510         dec->demux.dmx.close(&dec->demux.dmx);
1511         dec->demux.dmx.remove_frontend(&dec->demux.dmx, &dec->frontend);
1512         dvb_dmxdev_release(&dec->dmxdev);
1513         dvb_dmx_release(&dec->demux);
1514         if (dec->fe) {
1515                 dvb_unregister_frontend(dec->fe);
1516                 if (dec->fe->ops.release)
1517                         dec->fe->ops.release(dec->fe);
1518         }
1519         dvb_unregister_adapter(&dec->adapter);
1520 }
1521
1522 static void ttusb_dec_exit_rc(struct ttusb_dec *dec)
1523 {
1524
1525         dprintk("%s\n", __FUNCTION__);
1526         /* we have to check whether the irq URB is already submitted.
1527           * As the irq is submitted after the interface is changed,
1528           * this is the best method i figured out.
1529           * Any others?*/
1530         if (dec->interface == TTUSB_DEC_INTERFACE_IN)
1531                 usb_kill_urb(dec->irq_urb);
1532
1533         usb_free_urb(dec->irq_urb);
1534
1535         usb_buffer_free(dec->udev,IRQ_PACKET_SIZE,
1536                            dec->irq_buffer, dec->irq_dma_handle);
1537
1538         if (dec->rc_input_dev) {
1539                 input_unregister_device(dec->rc_input_dev);
1540                 dec->rc_input_dev = NULL;
1541         }
1542 }
1543
1544
1545 static void ttusb_dec_exit_usb(struct ttusb_dec *dec)
1546 {
1547         int i;
1548
1549         dprintk("%s\n", __FUNCTION__);
1550
1551         dec->iso_stream_count = 0;
1552
1553         for (i = 0; i < ISO_BUF_COUNT; i++)
1554                 usb_kill_urb(dec->iso_urb[i]);
1555
1556         ttusb_dec_free_iso_urbs(dec);
1557 }
1558
1559 static void ttusb_dec_exit_tasklet(struct ttusb_dec *dec)
1560 {
1561         struct list_head *item;
1562         struct urb_frame *frame;
1563
1564         tasklet_kill(&dec->urb_tasklet);
1565
1566         while ((item = dec->urb_frame_list.next) != &dec->urb_frame_list) {
1567                 frame = list_entry(item, struct urb_frame, urb_frame_list);
1568                 list_del(&frame->urb_frame_list);
1569                 kfree(frame);
1570         }
1571 }
1572
1573 static void ttusb_dec_init_filters(struct ttusb_dec *dec)
1574 {
1575         INIT_LIST_HEAD(&dec->filter_info_list);
1576         spin_lock_init(&dec->filter_info_list_lock);
1577 }
1578
1579 static void ttusb_dec_exit_filters(struct ttusb_dec *dec)
1580 {
1581         struct list_head *item;
1582         struct filter_info *finfo;
1583
1584         while ((item = dec->filter_info_list.next) != &dec->filter_info_list) {
1585                 finfo = list_entry(item, struct filter_info, filter_info_list);
1586                 list_del(&finfo->filter_info_list);
1587                 kfree(finfo);
1588         }
1589 }
1590
1591 static int fe_send_command(struct dvb_frontend* fe, const u8 command,
1592                            int param_length, const u8 params[],
1593                            int *result_length, u8 cmd_result[])
1594 {
1595         struct ttusb_dec* dec = fe->dvb->priv;
1596         return ttusb_dec_send_command(dec, command, param_length, params, result_length, cmd_result);
1597 }
1598
1599 static struct ttusbdecfe_config fe_config = {
1600         .send_command = fe_send_command
1601 };
1602
1603 static int ttusb_dec_probe(struct usb_interface *intf,
1604                            const struct usb_device_id *id)
1605 {
1606         struct usb_device *udev;
1607         struct ttusb_dec *dec;
1608
1609         dprintk("%s\n", __FUNCTION__);
1610
1611         udev = interface_to_usbdev(intf);
1612
1613         if (!(dec = kzalloc(sizeof(struct ttusb_dec), GFP_KERNEL))) {
1614                 printk("%s: couldn't allocate memory.\n", __FUNCTION__);
1615                 return -ENOMEM;
1616         }
1617
1618         usb_set_intfdata(intf, (void *)dec);
1619
1620         switch (le16_to_cpu(id->idProduct)) {
1621         case 0x1006:
1622                 ttusb_dec_set_model(dec, TTUSB_DEC3000S);
1623                 break;
1624
1625         case 0x1008:
1626                 ttusb_dec_set_model(dec, TTUSB_DEC2000T);
1627                 break;
1628
1629         case 0x1009:
1630                 ttusb_dec_set_model(dec, TTUSB_DEC2540T);
1631                 break;
1632         }
1633
1634         dec->udev = udev;
1635
1636         if (ttusb_dec_init_usb(dec))
1637                 return 0;
1638         if (ttusb_dec_init_stb(dec)) {
1639                 ttusb_dec_exit_usb(dec);
1640                 return 0;
1641         }
1642         ttusb_dec_init_dvb(dec);
1643
1644         dec->adapter.priv = dec;
1645         switch (le16_to_cpu(id->idProduct)) {
1646         case 0x1006:
1647                 dec->fe = ttusbdecfe_dvbs_attach(&fe_config);
1648                 break;
1649
1650         case 0x1008:
1651         case 0x1009:
1652                 dec->fe = ttusbdecfe_dvbt_attach(&fe_config);
1653                 break;
1654         }
1655
1656         if (dec->fe == NULL) {
1657                 printk("dvb-ttusb-dec: A frontend driver was not found for device %04x/%04x\n",
1658                        le16_to_cpu(dec->udev->descriptor.idVendor),
1659                        le16_to_cpu(dec->udev->descriptor.idProduct));
1660         } else {
1661                 if (dvb_register_frontend(&dec->adapter, dec->fe)) {
1662                         printk("budget-ci: Frontend registration failed!\n");
1663                         if (dec->fe->ops.release)
1664                                 dec->fe->ops.release(dec->fe);
1665                         dec->fe = NULL;
1666                 }
1667         }
1668
1669         ttusb_dec_init_v_pes(dec);
1670         ttusb_dec_init_filters(dec);
1671         ttusb_dec_init_tasklet(dec);
1672
1673         dec->active = 1;
1674
1675         ttusb_dec_set_interface(dec, TTUSB_DEC_INTERFACE_IN);
1676
1677         if (enable_rc)
1678                 ttusb_init_rc(dec);
1679
1680         return 0;
1681 }
1682
1683 static void ttusb_dec_disconnect(struct usb_interface *intf)
1684 {
1685         struct ttusb_dec *dec = usb_get_intfdata(intf);
1686
1687         usb_set_intfdata(intf, NULL);
1688
1689         dprintk("%s\n", __FUNCTION__);
1690
1691         if (dec->active) {
1692                 ttusb_dec_exit_tasklet(dec);
1693                 ttusb_dec_exit_filters(dec);
1694                 if(enable_rc)
1695                         ttusb_dec_exit_rc(dec);
1696                 ttusb_dec_exit_usb(dec);
1697                 ttusb_dec_exit_dvb(dec);
1698         }
1699
1700         kfree(dec);
1701 }
1702
1703 static void ttusb_dec_set_model(struct ttusb_dec *dec,
1704                                 enum ttusb_dec_model model)
1705 {
1706         dec->model = model;
1707
1708         switch (model) {
1709         case TTUSB_DEC2000T:
1710                 dec->model_name = "DEC2000-t";
1711                 dec->firmware_name = "dvb-ttusb-dec-2000t.fw";
1712                 break;
1713
1714         case TTUSB_DEC2540T:
1715                 dec->model_name = "DEC2540-t";
1716                 dec->firmware_name = "dvb-ttusb-dec-2540t.fw";
1717                 break;
1718
1719         case TTUSB_DEC3000S:
1720                 dec->model_name = "DEC3000-s";
1721                 dec->firmware_name = "dvb-ttusb-dec-3000s.fw";
1722                 break;
1723         }
1724 }
1725
1726 static struct usb_device_id ttusb_dec_table[] = {
1727         {USB_DEVICE(0x0b48, 0x1006)},   /* DEC3000-s */
1728         /*{USB_DEVICE(0x0b48, 0x1007)},    Unconfirmed */
1729         {USB_DEVICE(0x0b48, 0x1008)},   /* DEC2000-t */
1730         {USB_DEVICE(0x0b48, 0x1009)},   /* DEC2540-t */
1731         {}
1732 };
1733
1734 static struct usb_driver ttusb_dec_driver = {
1735         .name           = "ttusb-dec",
1736         .probe          = ttusb_dec_probe,
1737         .disconnect     = ttusb_dec_disconnect,
1738         .id_table       = ttusb_dec_table,
1739 };
1740
1741 static int __init ttusb_dec_init(void)
1742 {
1743         int result;
1744
1745         if ((result = usb_register(&ttusb_dec_driver)) < 0) {
1746                 printk("%s: initialisation failed: error %d.\n", __FUNCTION__,
1747                        result);
1748                 return result;
1749         }
1750
1751         return 0;
1752 }
1753
1754 static void __exit ttusb_dec_exit(void)
1755 {
1756         usb_deregister(&ttusb_dec_driver);
1757 }
1758
1759 module_init(ttusb_dec_init);
1760 module_exit(ttusb_dec_exit);
1761
1762 MODULE_AUTHOR("Alex Woods <linux-dvb@giblets.org>");
1763 MODULE_DESCRIPTION(DRIVER_NAME);
1764 MODULE_LICENSE("GPL");
1765 MODULE_DEVICE_TABLE(usb, ttusb_dec_table);