Version:  2.0.40 2.2.26 2.4.37 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us