ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / dvb / ttusb-budget / dvb-ttusb-budget.c
1 /*
2  * TTUSB DVB driver
3  *
4  * Copyright (c) 2002 Holger Waechtler <holger@convergence.de>
5  * Copyright (c) 2003 Felix Domke <tmbinc@elitedvb.net>
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License as
9  *      published by the Free Software Foundation; either version 2 of
10  *      the License, or (at your option) any later version.
11  */
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/wait.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 #include <linux/delay.h>
18 #include <linux/time.h>
19 #include <linux/errno.h>
20 #include <asm/semaphore.h>
21
22 #include "dvb_frontend.h"
23 #include "dmxdev.h"
24 #include "dvb_demux.h"
25 #include "dvb_net.h"
26
27 #include <linux/dvb/frontend.h>
28 #include <linux/dvb/dmx.h>
29 #include <linux/pci.h>
30
31 #include "dvb_functions.h"
32
33 /*
34   TTUSB_HWSECTIONS:
35     the DSP supports filtering in hardware, however, since the "muxstream"
36     is a bit braindead (no matching channel masks or no matching filter mask),
37     we won't support this - yet. it doesn't event support negative filters,
38     so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
39     parse TS data. USB bandwith will be a problem when having large
40     datastreams, especially for dvb-net, but hey, that's not my problem.
41         
42   TTUSB_DISEQC, TTUSB_TONE:
43     let the STC do the diseqc/tone stuff. this isn't supported at least with
44     my TTUSB, so let it undef'd unless you want to implement another
45     frontend. never tested.
46                 
47   DEBUG:
48     define it to > 3 for really hardcore debugging. you probably don't want
49     this unless the device doesn't load at all. > 2 for bandwidth statistics.
50 */
51
52 static int debug = 0;
53
54 #define dprintk(x...) do { if (debug) printk(KERN_DEBUG x); } while (0)
55
56 #define ISO_BUF_COUNT      4
57 #define FRAMES_PER_ISO_BUF 4
58 #define ISO_FRAME_SIZE     912
59 #define TTUSB_MAXCHANNEL   32
60 #ifdef TTUSB_HWSECTIONS
61 #define TTUSB_MAXFILTER    16   /* ??? */
62 #endif
63
64 #define TTUSB_BUDGET_NAME "ttusb_stc_fw"
65
66 /**
67  *  since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
68  *  the dvb_demux field must be the first in struct!!
69  */
70 struct ttusb {
71         struct dvb_demux dvb_demux;
72         struct dmxdev dmxdev;
73         struct dvb_net dvbnet;
74
75         /* our semaphore, for channel allocation/deallocation */
76         struct semaphore sem;
77         /* and one for USB access. */
78         struct semaphore semusb;
79
80         struct dvb_adapter *adapter;
81         struct usb_device *dev;
82
83         int disconnecting;
84         int iso_streaming;
85
86         unsigned int bulk_out_pipe;
87         unsigned int bulk_in_pipe;
88         unsigned int isoc_in_pipe;
89
90         void *iso_buffer;
91         dma_addr_t iso_dma_handle;
92
93         struct urb *iso_urb[ISO_BUF_COUNT];
94
95         int running_feed_count;
96         int last_channel;
97         int last_filter;
98
99         u8 c;                   /* transaction counter, wraps around...  */
100         fe_sec_tone_mode_t tone;
101         fe_sec_voltage_t voltage;
102
103         int mux_state;          // 0..2 - MuxSyncWord, 3 - nMuxPacks,    4 - muxpack
104         u8 mux_npacks;
105         u8 muxpack[256 + 8];
106         int muxpack_ptr, muxpack_len;
107
108         int insync;
109
110         int cc;                 /* MuxCounter - will increment on EVERY MUX PACKET */
111         /* (including stuffing. yes. really.) */
112
113
114         u8 last_result[32];
115
116         struct ttusb_channel {
117                 struct ttusb *ttusb;
118                 struct dvb_demux_feed *dvbdmxfeed;
119
120                 int active;
121                 int id;
122                 int pid;
123                 int type;       /* 1 - TS, 2 - Filter */
124 #ifdef TTUSB_HWSECTIONS
125                 int filterstate[TTUSB_MAXFILTER];       /* 0: not busy, 1: busy */
126 #endif
127         } channel[TTUSB_MAXCHANNEL];
128 #if 0
129         devfs_handle_t stc_devfs_handle;
130 #endif
131 };
132
133 /* ugly workaround ... don't know why it's neccessary to read */
134 /* all result codes. */
135
136 #define DEBUG 0
137 static int ttusb_cmd(struct ttusb *ttusb,
138               const u8 * data, int len, int needresult)
139 {
140         int actual_len;
141         int err;
142 #if DEBUG >= 3
143         int i;
144
145         printk(">");
146         for (i = 0; i < len; ++i)
147                 printk(" %02x", data[i]);
148         printk("\n");
149 #endif
150
151         if (down_interruptible(&ttusb->semusb) < 0)
152                 return -EAGAIN;
153
154         err = usb_bulk_msg(ttusb->dev, ttusb->bulk_out_pipe,
155                            (u8 *) data, len, &actual_len, HZ);
156         if (err != 0) {
157                 dprintk("%s: usb_bulk_msg(send) failed, err == %i!\n",
158                         __FUNCTION__, err);
159                 up(&ttusb->semusb);
160                 return err;
161         }
162         if (actual_len != len) {
163                 dprintk("%s: only wrote %d of %d bytes\n", __FUNCTION__,
164                         actual_len, len);
165                 up(&ttusb->semusb);
166                 return -1;
167         }
168
169         err = usb_bulk_msg(ttusb->dev, ttusb->bulk_in_pipe,
170                            ttusb->last_result, 32, &actual_len, HZ);
171
172         if (err != 0) {
173                 printk("%s: failed, receive error %d\n", __FUNCTION__,
174                        err);
175                 up(&ttusb->semusb);
176                 return err;
177         }
178 #if DEBUG >= 3
179         actual_len = ttusb->last_result[3] + 4;
180         printk("<");
181         for (i = 0; i < actual_len; ++i)
182                 printk(" %02x", ttusb->last_result[i]);
183         printk("\n");
184 #endif
185         if (!needresult)
186                 up(&ttusb->semusb);
187         return 0;
188 }
189
190 static int ttusb_result(struct ttusb *ttusb, u8 * data, int len)
191 {
192         memcpy(data, ttusb->last_result, len);
193         up(&ttusb->semusb);
194         return 0;
195 }
196
197 static int ttusb_i2c_msg(struct ttusb *ttusb,
198                   u8 addr, u8 * snd_buf, u8 snd_len, u8 * rcv_buf,
199                   u8 rcv_len)
200 {
201         u8 b[0x28];
202         u8 id = ++ttusb->c;
203         int i, err;
204
205         if (snd_len > 0x28 - 7 || rcv_len > 0x20 - 7)
206                 return -EINVAL;
207
208         b[0] = 0xaa;
209         b[1] = id;
210         b[2] = 0x31;
211         b[3] = snd_len + 3;
212         b[4] = addr << 1;
213         b[5] = snd_len;
214         b[6] = rcv_len;
215
216         for (i = 0; i < snd_len; i++)
217                 b[7 + i] = snd_buf[i];
218
219         err = ttusb_cmd(ttusb, b, snd_len + 7, 1);
220
221         if (err)
222                 return -EREMOTEIO;
223
224         err = ttusb_result(ttusb, b, 0x20);
225
226         /* check if the i2c transaction was successful */
227         if ((snd_len != b[5]) || (rcv_len != b[6])) return -EREMOTEIO;
228
229         if (rcv_len > 0) {
230
231                 if (err || b[0] != 0x55 || b[1] != id) {
232                         dprintk
233                             ("%s: usb_bulk_msg(recv) failed, err == %i, id == %02x, b == ",
234                              __FUNCTION__, err, id);
235                         return -EREMOTEIO;
236                 }
237
238                 for (i = 0; i < rcv_len; i++)
239                         rcv_buf[i] = b[7 + i];
240         }
241
242         return rcv_len;
243 }
244
245 static int ttusb_i2c_xfer(struct dvb_i2c_bus *i2c, const struct i2c_msg msg[],
246                    int num)
247 {
248         struct ttusb *ttusb = i2c->data;
249         int i = 0;
250         int inc;
251
252         if (down_interruptible(&ttusb->sem) < 0)
253                 return -EAGAIN;
254
255         while (i < num) {
256                 u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
257                 int err;
258
259                 if (num > i + 1 && (msg[i + 1].flags & I2C_M_RD)) {
260                         addr = msg[i].addr;
261                         snd_buf = msg[i].buf;
262                         snd_len = msg[i].len;
263                         rcv_buf = msg[i + 1].buf;
264                         rcv_len = msg[i + 1].len;
265                         inc = 2;
266                 } else {
267                         addr = msg[i].addr;
268                         snd_buf = msg[i].buf;
269                         snd_len = msg[i].len;
270                         rcv_buf = NULL;
271                         rcv_len = 0;
272                         inc = 1;
273                 }
274
275                 err = ttusb_i2c_msg(ttusb, addr,
276                                     snd_buf, snd_len, rcv_buf, rcv_len);
277
278                 if (err < rcv_len) {
279                         dprintk("%s: i == %i\n", __FUNCTION__, i);
280                         break;
281                 }
282
283                 i += inc;
284         }
285
286         up(&ttusb->sem);
287         return i;
288 }
289
290 #include "dvb-ttusb-dspbootcode.h"
291
292 static int ttusb_boot_dsp(struct ttusb *ttusb)
293 {
294         int i, err;
295         u8 b[40];
296
297         /* BootBlock */
298         b[0] = 0xaa;
299         b[2] = 0x13;
300         b[3] = 28;
301
302         /* upload dsp code in 32 byte steps (36 didn't work for me ...) */
303         /* 32 is max packet size, no messages should be splitted. */
304         for (i = 0; i < sizeof(dsp_bootcode); i += 28) {
305                 memcpy(&b[4], &dsp_bootcode[i], 28);
306
307                 b[1] = ++ttusb->c;
308
309                 err = ttusb_cmd(ttusb, b, 32, 0);
310                 if (err)
311                         goto done;
312         }
313
314         /* last block ... */
315         b[1] = ++ttusb->c;
316         b[2] = 0x13;
317         b[3] = 0;
318
319         err = ttusb_cmd(ttusb, b, 4, 0);
320         if (err)
321                 goto done;
322
323         /* BootEnd */
324         b[1] = ++ttusb->c;
325         b[2] = 0x14;
326         b[3] = 0;
327
328         err = ttusb_cmd(ttusb, b, 4, 0);
329
330       done:
331         if (err) {
332                 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
333                         __FUNCTION__, err);
334         }
335
336         return err;
337 }
338
339 static int ttusb_set_channel(struct ttusb *ttusb, int chan_id, int filter_type,
340                       int pid)
341 {
342         int err;
343         /* SetChannel */
344         u8 b[] = { 0xaa, ++ttusb->c, 0x22, 4, chan_id, filter_type,
345                 (pid >> 8) & 0xff, pid & 0xff
346         };
347
348         err = ttusb_cmd(ttusb, b, sizeof(b), 0);
349         return err;
350 }
351
352 static int ttusb_del_channel(struct ttusb *ttusb, int channel_id)
353 {
354         int err;
355         /* DelChannel */
356         u8 b[] = { 0xaa, ++ttusb->c, 0x23, 1, channel_id };
357
358         err = ttusb_cmd(ttusb, b, sizeof(b), 0);
359         return err;
360 }
361
362 #ifdef TTUSB_HWSECTIONS
363 static int ttusb_set_filter(struct ttusb *ttusb, int filter_id,
364                      int associated_chan, u8 filter[8], u8 mask[8])
365 {
366         int err;
367         /* SetFilter */
368         u8 b[] = { 0xaa, 0, 0x24, 0x1a, filter_id, associated_chan,
369                 filter[0], filter[1], filter[2], filter[3],
370                 filter[4], filter[5], filter[6], filter[7],
371                 filter[8], filter[9], filter[10], filter[11],
372                 mask[0], mask[1], mask[2], mask[3],
373                 mask[4], mask[5], mask[6], mask[7],
374                 mask[8], mask[9], mask[10], mask[11]
375         };
376
377         err = ttusb_cmd(ttusb, b, sizeof(b), 0);
378         return err;
379 }
380
381 static int ttusb_del_filter(struct ttusb *ttusb, int filter_id)
382 {
383         int err;
384         /* DelFilter */
385         u8 b[] = { 0xaa, ++ttusb->c, 0x25, 1, filter_id };
386
387         err = ttusb_cmd(ttusb, b, sizeof(b), 0);
388         return err;
389 }
390 #endif
391
392 static int ttusb_init_controller(struct ttusb *ttusb)
393 {
394         u8 b0[] = { 0xaa, ++ttusb->c, 0x15, 1, 0 };
395         u8 b1[] = { 0xaa, ++ttusb->c, 0x15, 1, 1 };
396         u8 b2[] = { 0xaa, ++ttusb->c, 0x32, 1, 0 };
397         /* i2c write read: 5 bytes, addr 0x10, 0x02 bytes write, 1 bytes read. */
398         u8 b3[] =
399             { 0xaa, ++ttusb->c, 0x31, 5, 0x10, 0x02, 0x01, 0x00, 0x1e };
400         u8 b4[] =
401             { 0x55, ttusb->c, 0x31, 4, 0x10, 0x02, 0x01, 0x00, 0x1e };
402
403         u8 get_version[] = { 0xaa, ++ttusb->c, 0x17, 5, 0, 0, 0, 0, 0 };
404         u8 get_dsp_version[0x20] =
405             { 0xaa, ++ttusb->c, 0x26, 28, 0, 0, 0, 0, 0 };
406         int err;
407
408         /* reset board */
409         if ((err = ttusb_cmd(ttusb, b0, sizeof(b0), 0)))
410                 return err;
411
412         /* reset board (again?) */
413         if ((err = ttusb_cmd(ttusb, b1, sizeof(b1), 0)))
414                 return err;
415
416         ttusb_boot_dsp(ttusb);
417
418         /* set i2c bit rate */
419         if ((err = ttusb_cmd(ttusb, b2, sizeof(b2), 0)))
420                 return err;
421
422         if ((err = ttusb_cmd(ttusb, b3, sizeof(b3), 1)))
423                 return err;
424
425         err = ttusb_result(ttusb, b4, sizeof(b4));
426
427         if ((err = ttusb_cmd(ttusb, get_version, sizeof(get_version), 1)))
428                 return err;
429
430         if ((err = ttusb_result(ttusb, get_version, sizeof(get_version))))
431                 return err;
432
433         dprintk("%s: stc-version: %c%c%c%c%c\n", __FUNCTION__,
434                 get_version[4], get_version[5], get_version[6],
435                 get_version[7], get_version[8]);
436
437         if (memcmp(get_version + 4, "V 0.0", 5) &&
438             memcmp(get_version + 4, "V 1.1", 5) &&
439             memcmp(get_version + 4, "V 2.1", 5)) {
440                 printk
441                     ("%s: unknown STC version %c%c%c%c%c, please report!\n",
442                      __FUNCTION__, get_version[4], get_version[5],
443                      get_version[6], get_version[7], get_version[8]);
444         }
445
446         err =
447             ttusb_cmd(ttusb, get_dsp_version, sizeof(get_dsp_version), 1);
448         if (err)
449                 return err;
450
451         err =
452             ttusb_result(ttusb, get_dsp_version, sizeof(get_dsp_version));
453         if (err)
454                 return err;
455         printk("%s: dsp-version: %c%c%c\n", __FUNCTION__,
456                get_dsp_version[4], get_dsp_version[5], get_dsp_version[6]);
457         return 0;
458 }
459
460 #ifdef TTUSB_DISEQC
461 static int ttusb_send_diseqc(struct ttusb *ttusb,
462                       const struct dvb_diseqc_master_cmd *cmd)
463 {
464         u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
465
466         int err;
467
468         b[3] = 4 + 2 + cmd->msg_len;
469         b[4] = 0xFF;            /* send diseqc master, not burst */
470         b[5] = cmd->msg_len;
471
472         memcpy(b + 5, cmd->msg, cmd->msg_len);
473
474         /* Diseqc */
475         if ((err = ttusb_cmd(ttusb, b, 4 + b[3], 0))) {
476                 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
477                         __FUNCTION__, err);
478         }
479
480         return err;
481 }
482 #endif
483
484 static int ttusb_update_lnb(struct ttusb *ttusb)
485 {
486         u8 b[] = { 0xaa, ++ttusb->c, 0x16, 5, /*power: */ 1,
487                 ttusb->voltage == SEC_VOLTAGE_18 ? 0 : 1,
488                 ttusb->tone == SEC_TONE_ON ? 1 : 0, 1, 1
489         };
490         int err;
491
492         /* SetLNB */
493         if ((err = ttusb_cmd(ttusb, b, sizeof(b), 0))) {
494                 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
495                         __FUNCTION__, err);
496         }
497
498         return err;
499 }
500
501 static int ttusb_set_voltage(struct ttusb *ttusb, fe_sec_voltage_t voltage)
502 {
503         ttusb->voltage = voltage;
504         return ttusb_update_lnb(ttusb);
505 }
506
507 #ifdef TTUSB_TONE
508 static int ttusb_set_tone(struct ttusb *ttusb, fe_sec_tone_mode_t tone)
509 {
510         ttusb->tone = tone;
511         return ttusb_update_lnb(ttusb);
512 }
513 #endif
514
515 static int ttusb_lnb_ioctl(struct dvb_frontend *fe, unsigned int cmd, void *arg)
516 {
517         struct ttusb *ttusb = fe->i2c->data;
518
519         switch (cmd) {
520         case FE_SET_VOLTAGE:
521                 return ttusb_set_voltage(ttusb, (fe_sec_voltage_t) arg);
522 #ifdef TTUSB_TONE
523         case FE_SET_TONE:
524                 return ttusb_set_tone(ttusb, (fe_sec_tone_mode_t) arg);
525 #endif
526 #ifdef TTUSB_DISEQC
527         case FE_DISEQC_SEND_MASTER_CMD:
528                 return ttusb_send_diseqc(ttusb,
529                                          (struct dvb_diseqc_master_cmd *)
530                                          arg);
531 #endif
532         default:
533                 return -EOPNOTSUPP;
534         };
535 }
536
537 #if 0
538 static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
539 {
540         u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
541         int err, actual_len;
542
543         err = ttusb_cmd(ttusb, b, sizeof(b), 0);
544         if (err) {
545                 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
546                         __FUNCTION__, err);
547         }
548 }
549 #endif
550
551 /*****************************************************************************/
552
553 #ifdef TTUSB_HWSECTIONS
554 static void ttusb_handle_ts_data(struct ttusb_channel *channel,
555                                  const u8 * data, int len);
556 static void ttusb_handle_sec_data(struct ttusb_channel *channel,
557                                   const u8 * data, int len);
558 #endif
559
560 int numpkt = 0, lastj, numts, numstuff, numsec, numinvalid;
561
562 static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
563                            int len)
564 {
565         u16 csum = 0, cc;
566         int i;
567         for (i = 0; i < len; i += 2)
568                 csum ^= le16_to_cpup((u16 *) (muxpack + i));
569         if (csum) {
570                 printk("%s: muxpack with incorrect checksum, ignoring\n",
571                        __FUNCTION__);
572                 numinvalid++;
573                 return;
574         }
575
576         cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
577         cc &= 0x7FFF;
578         if ((cc != ttusb->cc) && (ttusb->cc != -1))
579                 printk("%s: cc discontinuity (%d frames missing)\n",
580                        __FUNCTION__, (cc - ttusb->cc) & 0x7FFF);
581         ttusb->cc = (cc + 1) & 0x7FFF;
582         if (muxpack[0] & 0x80) {
583 #ifdef TTUSB_HWSECTIONS
584                 /* section data */
585                 int pusi = muxpack[0] & 0x40;
586                 int channel = muxpack[0] & 0x1F;
587                 int payload = muxpack[1];
588                 const u8 *data = muxpack + 2;
589                 /* check offset flag */
590                 if (muxpack[0] & 0x20)
591                         data++;
592
593                 ttusb_handle_sec_data(ttusb->channel + channel, data,
594                                       payload);
595                 data += payload;
596
597                 if ((!!(ttusb->muxpack[0] & 0x20)) ^
598                     !!(ttusb->muxpack[1] & 1))
599                         data++;
600 #warning TODO: pusi
601                 printk("cc: %04x\n", (data[0] << 8) | data[1]);
602 #endif
603                 numsec++;
604         } else if (muxpack[0] == 0x47) {
605 #ifdef TTUSB_HWSECTIONS
606                 /* we have TS data here! */
607                 int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
608                 int channel;
609                 for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
610                         if (ttusb->channel[channel].active
611                             && (pid == ttusb->channel[channel].pid))
612                                 ttusb_handle_ts_data(ttusb->channel +
613                                                      channel, muxpack,
614                                                      188);
615 #endif
616                 numts++;
617                 dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
618         } else if (muxpack[0] != 0) {
619                 numinvalid++;
620                 printk("illegal muxpack type %02x\n", muxpack[0]);
621         } else
622                 numstuff++;
623 }
624
625 static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
626 {
627         int maxwork = 1024;
628         while (len) {
629                 if (!(maxwork--)) {
630                         printk("%s: too much work\n", __FUNCTION__);
631                         break;
632                 }
633
634                 switch (ttusb->mux_state) {
635                 case 0:
636                 case 1:
637                 case 2:
638                         len--;
639                         if (*data++ == 0xAA)
640                                 ++ttusb->mux_state;
641                         else {
642                                 ttusb->mux_state = 0;
643 #if DEBUG > 3
644                                 if (ttusb->insync)
645                                         printk("%02x ", data[-1]);
646 #else
647                                 if (ttusb->insync) {
648                                         printk("%s: lost sync.\n",
649                                                __FUNCTION__);
650                                         ttusb->insync = 0;
651                                 }
652 #endif
653                         }
654                         break;
655                 case 3:
656                         ttusb->insync = 1;
657                         len--;
658                         ttusb->mux_npacks = *data++;
659                         ++ttusb->mux_state;
660                         ttusb->muxpack_ptr = 0;
661                         /* maximum bytes, until we know the length */
662                         ttusb->muxpack_len = 2;
663                         break;
664                 case 4:
665                         {
666                                 int avail;
667                                 avail = len;
668                                 if (avail >
669                                     (ttusb->muxpack_len -
670                                      ttusb->muxpack_ptr))
671                                         avail =
672                                             ttusb->muxpack_len -
673                                             ttusb->muxpack_ptr;
674                                 memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
675                                        data, avail);
676                                 ttusb->muxpack_ptr += avail;
677                                 if (ttusb->muxpack_ptr > 264)
678                                         BUG();
679                                 data += avail;
680                                 len -= avail;
681                                 /* determine length */
682                                 if (ttusb->muxpack_ptr == 2) {
683                                         if (ttusb->muxpack[0] & 0x80) {
684                                                 ttusb->muxpack_len =
685                                                     ttusb->muxpack[1] + 2;
686                                                 if (ttusb->
687                                                     muxpack[0] & 0x20)
688                                                         ttusb->
689                                                             muxpack_len++;
690                                                 if ((!!
691                                                      (ttusb->
692                                                       muxpack[0] & 0x20)) ^
693                                                     !!(ttusb->
694                                                        muxpack[1] & 1))
695                                                         ttusb->
696                                                             muxpack_len++;
697                                                 ttusb->muxpack_len += 4;
698                                         } else if (ttusb->muxpack[0] ==
699                                                    0x47)
700                                                 ttusb->muxpack_len =
701                                                     188 + 4;
702                                         else if (ttusb->muxpack[0] == 0x00)
703                                                 ttusb->muxpack_len =
704                                                     ttusb->muxpack[1] + 2 +
705                                                     4;
706                                         else {
707                                                 dprintk
708                                                     ("%s: invalid state: first byte is %x\n",
709                                                      __FUNCTION__,
710                                                      ttusb->muxpack[0]);
711                                                 ttusb->mux_state = 0;
712                                         }
713                                 }
714
715                         /**
716                          * if length is valid and we reached the end:
717                          * goto next muxpack
718                          */
719                                 if ((ttusb->muxpack_ptr >= 2) &&
720                                     (ttusb->muxpack_ptr ==
721                                      ttusb->muxpack_len)) {
722                                         ttusb_process_muxpack(ttusb,
723                                                               ttusb->
724                                                               muxpack,
725                                                               ttusb->
726                                                               muxpack_ptr);
727                                         ttusb->muxpack_ptr = 0;
728                                         /* maximum bytes, until we know the length */
729                                         ttusb->muxpack_len = 2;
730
731                                 /**
732                                  * no muxpacks left?
733                                  * return to search-sync state
734                                  */
735                                         if (!ttusb->mux_npacks--) {
736                                                 ttusb->mux_state = 0;
737                                                 break;
738                                         }
739                                 }
740                                 break;
741                         }
742                 default:
743                         BUG();
744                         break;
745                 }
746         }
747 }
748
749 static void ttusb_iso_irq(struct urb *urb, struct pt_regs *ptregs)
750 {
751         struct ttusb *ttusb = urb->context;
752
753         if (!ttusb->iso_streaming)
754                 return;
755
756 #if 0
757         printk("%s: status %d, errcount == %d, length == %i\n",
758                __FUNCTION__,
759                urb->status, urb->error_count, urb->actual_length);
760 #endif
761
762         if (!urb->status) {
763                 int i;
764                 for (i = 0; i < urb->number_of_packets; ++i) {
765                         struct usb_iso_packet_descriptor *d;
766                         u8 *data;
767                         int len;
768                         numpkt++;
769                         if ((jiffies - lastj) >= HZ) {
770 #if DEBUG > 2
771                                 printk
772                                     ("frames/s: %d (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
773                                      numpkt * HZ / (jiffies - lastj),
774                                      numts, numstuff, numsec, numinvalid,
775                                      numts + numstuff + numsec +
776                                      numinvalid);
777 #endif
778                                 numts = numstuff = numsec = numinvalid = 0;
779                                 lastj = jiffies;
780                                 numpkt = 0;
781                         }
782                         d = &urb->iso_frame_desc[i];
783                         data = urb->transfer_buffer + d->offset;
784                         len = d->actual_length;
785                         d->actual_length = 0;
786                         d->status = 0;
787                         ttusb_process_frame(ttusb, data, len);
788                 }
789         }
790         usb_submit_urb(urb, GFP_KERNEL);
791 }
792
793 static void ttusb_free_iso_urbs(struct ttusb *ttusb)
794 {
795         int i;
796
797         for (i = 0; i < ISO_BUF_COUNT; i++)
798                 if (ttusb->iso_urb[i])
799                         usb_free_urb(ttusb->iso_urb[i]);
800
801         pci_free_consistent(NULL,
802                             ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF *
803                             ISO_BUF_COUNT, ttusb->iso_buffer,
804                             ttusb->iso_dma_handle);
805 }
806
807 static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
808 {
809         int i;
810
811         ttusb->iso_buffer = pci_alloc_consistent(NULL,
812                                                  ISO_FRAME_SIZE *
813                                                  FRAMES_PER_ISO_BUF *
814                                                  ISO_BUF_COUNT,
815                                                  &ttusb->iso_dma_handle);
816
817         memset(ttusb->iso_buffer, 0,
818                ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF * ISO_BUF_COUNT);
819
820         for (i = 0; i < ISO_BUF_COUNT; i++) {
821                 struct urb *urb;
822
823                 if (!
824                     (urb =
825                      usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_KERNEL))) {
826                         ttusb_free_iso_urbs(ttusb);
827                         return -ENOMEM;
828                 }
829
830                 ttusb->iso_urb[i] = urb;
831         }
832
833         return 0;
834 }
835
836 static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
837 {
838         int i;
839
840         for (i = 0; i < ISO_BUF_COUNT; i++)
841                 usb_unlink_urb(ttusb->iso_urb[i]);
842
843         ttusb->iso_streaming = 0;
844 }
845
846 static int ttusb_start_iso_xfer(struct ttusb *ttusb)
847 {
848         int i, j, err, buffer_offset = 0;
849
850         if (ttusb->iso_streaming) {
851                 printk("%s: iso xfer already running!\n", __FUNCTION__);
852                 return 0;
853         }
854
855         ttusb->cc = -1;
856         ttusb->insync = 0;
857         ttusb->mux_state = 0;
858
859         for (i = 0; i < ISO_BUF_COUNT; i++) {
860                 int frame_offset = 0;
861                 struct urb *urb = ttusb->iso_urb[i];
862
863                 urb->dev = ttusb->dev;
864                 urb->context = ttusb;
865                 urb->complete = ttusb_iso_irq;
866                 urb->pipe = ttusb->isoc_in_pipe;
867                 urb->transfer_flags = URB_ISO_ASAP;
868                 urb->interval = 1;
869                 urb->number_of_packets = FRAMES_PER_ISO_BUF;
870                 urb->transfer_buffer_length =
871                     ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
872                 urb->transfer_buffer = ttusb->iso_buffer + buffer_offset;
873                 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
874
875                 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
876                         urb->iso_frame_desc[j].offset = frame_offset;
877                         urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
878                         frame_offset += ISO_FRAME_SIZE;
879                 }
880         }
881
882         for (i = 0; i < ISO_BUF_COUNT; i++) {
883                 if ((err = usb_submit_urb(ttusb->iso_urb[i], GFP_KERNEL))) {
884                         ttusb_stop_iso_xfer(ttusb);
885                         printk
886                             ("%s: failed urb submission (%i: err = %i)!\n",
887                              __FUNCTION__, i, err);
888                         return err;
889                 }
890         }
891
892         ttusb->iso_streaming = 1;
893
894         return 0;
895 }
896
897 #ifdef TTUSB_HWSECTIONS
898 static void ttusb_handle_ts_data(struct ttusb_channel *channel, const u8 * data,
899                           int len)
900 {
901         struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
902
903         dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
904 }
905
906 static void ttusb_handle_sec_data(struct ttusb_channel *channel, const u8 * data,
907                            int len)
908 {
909 //      struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
910 #error TODO: handle ugly stuff
911 //      dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
912 }
913 #endif
914
915 static struct ttusb_channel *ttusb_channel_allocate(struct ttusb *ttusb)
916 {
917         int i;
918
919         if (down_interruptible(&ttusb->sem))
920                 return NULL;
921
922         /* lock! */
923         for (i = 0; i < TTUSB_MAXCHANNEL; ++i) {
924                 if (!ttusb->channel[i].active) {
925                         ttusb->channel[i].active = 1;
926                         up(&ttusb->sem);
927                         return ttusb->channel + i;
928                 }
929         }
930
931         up(&ttusb->sem);
932
933         return NULL;
934 }
935
936 static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
937 {
938         struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
939         struct ttusb_channel *channel;
940
941         dprintk("ttusb_start_feed\n");
942
943         switch (dvbdmxfeed->type) {
944         case DMX_TYPE_TS:
945                 break;
946         case DMX_TYPE_SEC:
947                 break;
948         default:
949                 return -EINVAL;
950         }
951
952         if (dvbdmxfeed->type == DMX_TYPE_TS) {
953                 switch (dvbdmxfeed->pes_type) {
954                 case DMX_TS_PES_VIDEO:
955                 case DMX_TS_PES_AUDIO:
956                 case DMX_TS_PES_TELETEXT:
957                 case DMX_TS_PES_PCR:
958                 case DMX_TS_PES_OTHER:
959                         channel = ttusb_channel_allocate(ttusb);
960                         break;
961                 default:
962                         return -EINVAL;
963                 }
964         } else {
965                 channel = ttusb_channel_allocate(ttusb);
966         }
967
968         if (!channel)
969                 return -EBUSY;
970
971         dvbdmxfeed->priv = channel;
972         channel->dvbdmxfeed = dvbdmxfeed;
973
974         channel->pid = dvbdmxfeed->pid;
975
976 #ifdef TTUSB_HWSECTIONS
977         if (dvbdmxfeed->type == DMX_TYPE_TS) {
978                 channel->type = 1;
979         } else if (dvbdmxfeed->type == DMX_TYPE_SEC) {
980                 channel->type = 2;
981 #error TODO: allocate filters
982         }
983 #else
984         channel->type = 1;
985 #endif
986
987         ttusb_set_channel(ttusb, channel->id, channel->type, channel->pid);
988
989         if (0 == ttusb->running_feed_count++)
990                 ttusb_start_iso_xfer(ttusb);
991
992         return 0;
993 }
994
995 static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
996 {
997         struct ttusb_channel *channel =
998             (struct ttusb_channel *) dvbdmxfeed->priv;
999         struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
1000
1001         ttusb_del_channel(channel->ttusb, channel->id);
1002
1003         if (--ttusb->running_feed_count == 0)
1004                 ttusb_stop_iso_xfer(ttusb);
1005
1006         channel->active = 0;
1007
1008         return 0;
1009 }
1010
1011 static int ttusb_setup_interfaces(struct ttusb *ttusb)
1012 {
1013         usb_set_interface(ttusb->dev, 1, 1);
1014
1015         ttusb->bulk_out_pipe = usb_sndbulkpipe(ttusb->dev, 1);
1016         ttusb->bulk_in_pipe = usb_rcvbulkpipe(ttusb->dev, 1);
1017         ttusb->isoc_in_pipe = usb_rcvisocpipe(ttusb->dev, 2);
1018
1019         return 0;
1020 }
1021
1022 #if 0
1023 static u8 stc_firmware[8192];
1024
1025 static int stc_open(struct inode *inode, struct file *file)
1026 {
1027         struct ttusb *ttusb = file->private_data;
1028         int addr;
1029
1030         for (addr = 0; addr < 8192; addr += 16) {
1031                 u8 snd_buf[2] = { addr >> 8, addr & 0xFF };
1032                 ttusb_i2c_msg(ttusb, 0x50, snd_buf, 2, stc_firmware + addr,
1033                               16);
1034         }
1035
1036         return 0;
1037 }
1038
1039 static ssize_t stc_read(struct file *file, char *buf, size_t count,
1040                  loff_t * offset)
1041 {
1042         int tc = count;
1043
1044         if ((tc + *offset) > 8192)
1045                 tc = 8192 - *offset;
1046
1047         if (tc < 0)
1048                 return 0;
1049
1050         if (copy_to_user(buf, stc_firmware + *offset, tc))
1051                 return -EFAULT;
1052
1053         *offset += tc;
1054
1055         return tc;
1056 }
1057
1058 static int stc_release(struct inode *inode, struct file *file)
1059 {
1060         return 0;
1061 }
1062
1063 static struct file_operations stc_fops = {
1064         .owner = THIS_MODULE,
1065         .read = stc_read,
1066         .open = stc_open,
1067         .release = stc_release,
1068 };
1069 #endif
1070
1071 static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1072 {
1073         struct usb_device *udev;
1074         struct ttusb *ttusb;
1075         int result, channel;
1076
1077         dprintk("%s: TTUSB DVB connected\n", __FUNCTION__);
1078
1079         udev = interface_to_usbdev(intf);
1080
1081         /* Device has already been reset; its configuration was chosen.
1082          * If this fault happens, use a hotplug script to choose the
1083          * right configuration (write bConfigurationValue in sysfs).
1084          */
1085         if (udev->actconfig->desc.bConfigurationValue != 1) {
1086                 dev_err(&intf->dev, "device config is #%d, need #1\n",
1087                         udev->actconfig->desc.bConfigurationValue);
1088                 return -ENODEV;
1089         }
1090
1091
1092         if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
1093
1094         if (!(ttusb = kmalloc(sizeof(struct ttusb), GFP_KERNEL)))
1095                 return -ENOMEM;
1096
1097         memset(ttusb, 0, sizeof(struct ttusb));
1098
1099         for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel) {
1100                 ttusb->channel[channel].id = channel;
1101                 ttusb->channel[channel].ttusb = ttusb;
1102         }
1103
1104         ttusb->dev = udev;
1105         ttusb->c = 0;
1106         ttusb->mux_state = 0;
1107         sema_init(&ttusb->sem, 0);
1108         sema_init(&ttusb->semusb, 1);
1109
1110         ttusb_setup_interfaces(ttusb);
1111
1112         ttusb_alloc_iso_urbs(ttusb);
1113         if (ttusb_init_controller(ttusb))
1114                 printk("ttusb_init_controller: error\n");
1115
1116         up(&ttusb->sem);
1117
1118         dvb_register_adapter(&ttusb->adapter, "Technotrend/Hauppauge Nova-USB", THIS_MODULE);
1119
1120         dvb_register_i2c_bus(ttusb_i2c_xfer, ttusb, ttusb->adapter, 0);
1121         dvb_add_frontend_ioctls(ttusb->adapter, ttusb_lnb_ioctl, NULL,
1122                                 ttusb);
1123
1124         memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1125
1126         ttusb->dvb_demux.dmx.capabilities =
1127             DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1128         ttusb->dvb_demux.priv = 0;
1129 #ifdef TTUSB_HWSECTIONS
1130         ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1131 #else
1132         ttusb->dvb_demux.filternum = 32;
1133 #endif
1134         ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1135         ttusb->dvb_demux.start_feed = ttusb_start_feed;
1136         ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1137         ttusb->dvb_demux.write_to_decoder = 0;
1138
1139         if ((result = dvb_dmx_init(&ttusb->dvb_demux)) < 0) {
1140                 printk("ttusb_dvb: dvb_dmx_init failed (errno = %d)\n",
1141                        result);
1142                 goto err;
1143         }
1144 //FIXME dmxdev (nur WAS?)
1145         ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1146         ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1147         ttusb->dmxdev.capabilities = 0;
1148
1149         if ((result = dvb_dmxdev_init(&ttusb->dmxdev, ttusb->adapter)) < 0) {
1150                 printk("ttusb_dvb: dvb_dmxdev_init failed (errno = %d)\n",
1151                        result);
1152                 dvb_dmx_release(&ttusb->dvb_demux);
1153                 goto err;
1154         }
1155
1156         if (dvb_net_init
1157             (ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
1158                 printk("ttusb_dvb: dvb_net_init failed!\n");
1159         }
1160
1161       err:
1162 #if 0
1163         ttusb->stc_devfs_handle =
1164             devfs_register(ttusb->adapter->devfs_handle, TTUSB_BUDGET_NAME,
1165                            DEVFS_FL_DEFAULT, 0, 192,
1166                            S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
1167                            | S_IROTH | S_IWOTH, &stc_fops, ttusb);
1168 #endif
1169
1170         usb_set_intfdata(intf, (void *) ttusb);
1171
1172         return 0;
1173 }
1174
1175 static void ttusb_disconnect(struct usb_interface *intf)
1176 {
1177         struct ttusb *ttusb = usb_get_intfdata(intf);
1178
1179         usb_set_intfdata(intf, NULL);
1180
1181         ttusb->disconnecting = 1;
1182
1183         ttusb_stop_iso_xfer(ttusb);
1184
1185         ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1186         dvb_net_release(&ttusb->dvbnet);
1187         dvb_dmxdev_release(&ttusb->dmxdev);
1188         dvb_dmx_release(&ttusb->dvb_demux);
1189
1190         dvb_unregister_i2c_bus(ttusb_i2c_xfer, ttusb->adapter, 0);
1191         dvb_unregister_adapter(ttusb->adapter);
1192
1193         ttusb_free_iso_urbs(ttusb);
1194
1195         kfree(ttusb);
1196
1197         dprintk("%s: TTUSB DVB disconnected\n", __FUNCTION__);
1198 }
1199
1200 static struct usb_device_id ttusb_table[] = {
1201         {USB_DEVICE(0xb48, 0x1003)},
1202         {USB_DEVICE(0xb48, 0x1004)},    /* to be confirmed ????  */
1203         {USB_DEVICE(0xb48, 0x1005)},
1204         {}
1205 };
1206
1207 MODULE_DEVICE_TABLE(usb, ttusb_table);
1208
1209 static struct usb_driver ttusb_driver = {
1210       .name             = "Technotrend/Hauppauge USB-Nova",
1211       .probe            = ttusb_probe,
1212       .disconnect       = ttusb_disconnect,
1213       .id_table         = ttusb_table,
1214 };
1215
1216 static int __init ttusb_init(void)
1217 {
1218         int err;
1219
1220         if ((err = usb_register(&ttusb_driver)) < 0) {
1221                 printk("%s: usb_register failed! Error number %d",
1222                        __FILE__, err);
1223                 return err;
1224         }
1225
1226         return 0;
1227 }
1228
1229 static void __exit ttusb_exit(void)
1230 {
1231         usb_deregister(&ttusb_driver);
1232 }
1233
1234 module_init(ttusb_init);
1235 module_exit(ttusb_exit);
1236
1237 MODULE_PARM(debug, "i");
1238 MODULE_PARM_DESC(debug, "Debug or not");
1239
1240 MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1241 MODULE_DESCRIPTION("TTUSB DVB Driver");
1242 MODULE_LICENSE("GPL");