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