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