vserver 2.0 rc7
[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/moduleparam.h>
17 #include <linux/usb.h>
18 #include <linux/delay.h>
19 #include <linux/time.h>
20 #include <linux/errno.h>
21 #include <asm/semaphore.h>
22
23 #include "dvb_frontend.h"
24 #include "dmxdev.h"
25 #include "dvb_demux.h"
26 #include "dvb_net.h"
27 #include "cx22700.h"
28 #include "tda1004x.h"
29 #include "stv0299.h"
30 #include "tda8083.h"
31
32 #include <linux/dvb/frontend.h>
33 #include <linux/dvb/dmx.h>
34 #include <linux/pci.h>
35
36
37 /*
38   TTUSB_HWSECTIONS:
39     the DSP supports filtering in hardware, however, since the "muxstream"
40     is a bit braindead (no matching channel masks or no matching filter mask),
41     we won't support this - yet. it doesn't event support negative filters,
42     so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
43     parse TS data. USB bandwith will be a problem when having large
44     datastreams, especially for dvb-net, but hey, that's not my problem.
45
46   TTUSB_DISEQC, TTUSB_TONE:
47     let the STC do the diseqc/tone stuff. this isn't supported at least with
48     my TTUSB, so let it undef'd unless you want to implement another
49     frontend. never tested.
50
51   DEBUG:
52     define it to > 3 for really hardcore debugging. you probably don't want
53     this unless the device doesn't load at all. > 2 for bandwidth statistics.
54 */
55
56 static int debug;
57
58 module_param(debug, int, 0644);
59 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
60
61 #define dprintk(x...) do { if (debug) printk(KERN_DEBUG x); } while (0)
62
63 #define ISO_BUF_COUNT      4
64 #define FRAMES_PER_ISO_BUF 4
65 #define ISO_FRAME_SIZE     912
66 #define TTUSB_MAXCHANNEL   32
67 #ifdef TTUSB_HWSECTIONS
68 #define TTUSB_MAXFILTER    16   /* ??? */
69 #endif
70
71 #define TTUSB_REV_2_2   0x22
72 #define TTUSB_BUDGET_NAME "ttusb_stc_fw"
73
74 /**
75  *  since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
76  *  the dvb_demux field must be the first in struct!!
77  */
78 struct ttusb {
79         struct dvb_demux dvb_demux;
80         struct dmxdev dmxdev;
81         struct dvb_net dvbnet;
82
83         /* and one for USB access. */
84         struct semaphore semi2c;
85         struct semaphore semusb;
86
87         struct dvb_adapter adapter;
88         struct usb_device *dev;
89
90         struct i2c_adapter i2c_adap;
91
92         int disconnecting;
93         int iso_streaming;
94
95         unsigned int bulk_out_pipe;
96         unsigned int bulk_in_pipe;
97         unsigned int isoc_in_pipe;
98
99         void *iso_buffer;
100         dma_addr_t iso_dma_handle;
101
102         struct urb *iso_urb[ISO_BUF_COUNT];
103
104         int running_feed_count;
105         int last_channel;
106         int last_filter;
107
108         u8 c;                   /* transaction counter, wraps around...  */
109         fe_sec_tone_mode_t tone;
110         fe_sec_voltage_t voltage;
111
112         int mux_state;          // 0..2 - MuxSyncWord, 3 - nMuxPacks,    4 - muxpack
113         u8 mux_npacks;
114         u8 muxpack[256 + 8];
115         int muxpack_ptr, muxpack_len;
116
117         int insync;
118
119         int cc;                 /* MuxCounter - will increment on EVERY MUX PACKET */
120         /* (including stuffing. yes. really.) */
121
122         u8 last_result[32];
123
124         int revision;
125
126 #if 0
127         devfs_handle_t stc_devfs_handle;
128 #endif
129
130         struct dvb_frontend* fe;
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, 1000);
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, 1000);
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 master_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
246 {
247         struct ttusb *ttusb = i2c_get_adapdata(adapter);
248         int i = 0;
249         int inc;
250
251         if (down_interruptible(&ttusb->semi2c) < 0)
252                 return -EAGAIN;
253
254         while (i < num) {
255                 u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
256                 int err;
257
258                 if (num > i + 1 && (msg[i + 1].flags & I2C_M_RD)) {
259                         addr = msg[i].addr;
260                         snd_buf = msg[i].buf;
261                         snd_len = msg[i].len;
262                         rcv_buf = msg[i + 1].buf;
263                         rcv_len = msg[i + 1].len;
264                         inc = 2;
265                 } else {
266                         addr = msg[i].addr;
267                         snd_buf = msg[i].buf;
268                         snd_len = msg[i].len;
269                         rcv_buf = NULL;
270                         rcv_len = 0;
271                         inc = 1;
272                 }
273
274                 err = ttusb_i2c_msg(ttusb, addr,
275                                     snd_buf, snd_len, rcv_buf, rcv_len);
276
277                 if (err < rcv_len) {
278                         dprintk("%s: i == %i\n", __FUNCTION__, i);
279                         break;
280                 }
281
282                 i += inc;
283         }
284
285         up(&ttusb->semi2c);
286         return i;
287 }
288
289 #include "dvb-ttusb-dspbootcode.h"
290
291 static int ttusb_boot_dsp(struct ttusb *ttusb)
292 {
293         int i, err;
294         u8 b[40];
295
296         /* BootBlock */
297         b[0] = 0xaa;
298         b[2] = 0x13;
299         b[3] = 28;
300
301         /* upload dsp code in 32 byte steps (36 didn't work for me ...) */
302         /* 32 is max packet size, no messages should be splitted. */
303         for (i = 0; i < sizeof(dsp_bootcode); i += 28) {
304                 memcpy(&b[4], &dsp_bootcode[i], 28);
305
306                 b[1] = ++ttusb->c;
307
308                 err = ttusb_cmd(ttusb, b, 32, 0);
309                 if (err)
310                         goto done;
311         }
312
313         /* last block ... */
314         b[1] = ++ttusb->c;
315         b[2] = 0x13;
316         b[3] = 0;
317
318         err = ttusb_cmd(ttusb, b, 4, 0);
319         if (err)
320                 goto done;
321
322         /* BootEnd */
323         b[1] = ++ttusb->c;
324         b[2] = 0x14;
325         b[3] = 0;
326
327         err = ttusb_cmd(ttusb, b, 4, 0);
328
329       done:
330         if (err) {
331                 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
332                         __FUNCTION__, err);
333         }
334
335         return err;
336 }
337
338 static int ttusb_set_channel(struct ttusb *ttusb, int chan_id, int filter_type,
339                       int pid)
340 {
341         int err;
342         /* SetChannel */
343         u8 b[] = { 0xaa, ++ttusb->c, 0x22, 4, chan_id, filter_type,
344                 (pid >> 8) & 0xff, pid & 0xff
345         };
346
347         err = ttusb_cmd(ttusb, b, sizeof(b), 0);
348         return err;
349 }
350
351 static int ttusb_del_channel(struct ttusb *ttusb, int channel_id)
352 {
353         int err;
354         /* DelChannel */
355         u8 b[] = { 0xaa, ++ttusb->c, 0x23, 1, channel_id };
356
357         err = ttusb_cmd(ttusb, b, sizeof(b), 0);
358         return err;
359 }
360
361 #ifdef TTUSB_HWSECTIONS
362 static int ttusb_set_filter(struct ttusb *ttusb, int filter_id,
363                      int associated_chan, u8 filter[8], u8 mask[8])
364 {
365         int err;
366         /* SetFilter */
367         u8 b[] = { 0xaa, 0, 0x24, 0x1a, filter_id, associated_chan,
368                 filter[0], filter[1], filter[2], filter[3],
369                 filter[4], filter[5], filter[6], filter[7],
370                 filter[8], filter[9], filter[10], filter[11],
371                 mask[0], mask[1], mask[2], mask[3],
372                 mask[4], mask[5], mask[6], mask[7],
373                 mask[8], mask[9], mask[10], mask[11]
374         };
375
376         err = ttusb_cmd(ttusb, b, sizeof(b), 0);
377         return err;
378 }
379
380 static int ttusb_del_filter(struct ttusb *ttusb, int filter_id)
381 {
382         int err;
383         /* DelFilter */
384         u8 b[] = { 0xaa, ++ttusb->c, 0x25, 1, filter_id };
385
386         err = ttusb_cmd(ttusb, b, sizeof(b), 0);
387         return err;
388 }
389 #endif
390
391 static int ttusb_init_controller(struct ttusb *ttusb)
392 {
393         u8 b0[] = { 0xaa, ++ttusb->c, 0x15, 1, 0 };
394         u8 b1[] = { 0xaa, ++ttusb->c, 0x15, 1, 1 };
395         u8 b2[] = { 0xaa, ++ttusb->c, 0x32, 1, 0 };
396         /* i2c write read: 5 bytes, addr 0x10, 0x02 bytes write, 1 bytes read. */
397         u8 b3[] =
398             { 0xaa, ++ttusb->c, 0x31, 5, 0x10, 0x02, 0x01, 0x00, 0x1e };
399         u8 b4[] =
400             { 0x55, ttusb->c, 0x31, 4, 0x10, 0x02, 0x01, 0x00, 0x1e };
401
402         u8 get_version[] = { 0xaa, ++ttusb->c, 0x17, 5, 0, 0, 0, 0, 0 };
403         u8 get_dsp_version[0x20] =
404             { 0xaa, ++ttusb->c, 0x26, 28, 0, 0, 0, 0, 0 };
405         int err;
406
407         /* reset board */
408         if ((err = ttusb_cmd(ttusb, b0, sizeof(b0), 0)))
409                 return err;
410
411         /* reset board (again?) */
412         if ((err = ttusb_cmd(ttusb, b1, sizeof(b1), 0)))
413                 return err;
414
415         ttusb_boot_dsp(ttusb);
416
417         /* set i2c bit rate */
418         if ((err = ttusb_cmd(ttusb, b2, sizeof(b2), 0)))
419                 return err;
420
421         if ((err = ttusb_cmd(ttusb, b3, sizeof(b3), 1)))
422                 return err;
423
424         err = ttusb_result(ttusb, b4, sizeof(b4));
425
426         if ((err = ttusb_cmd(ttusb, get_version, sizeof(get_version), 1)))
427                 return err;
428
429         if ((err = ttusb_result(ttusb, get_version, sizeof(get_version))))
430                 return err;
431
432         dprintk("%s: stc-version: %c%c%c%c%c\n", __FUNCTION__,
433                 get_version[4], get_version[5], get_version[6],
434                 get_version[7], get_version[8]);
435
436         if (memcmp(get_version + 4, "V 0.0", 5) &&
437             memcmp(get_version + 4, "V 1.1", 5) &&
438             memcmp(get_version + 4, "V 2.1", 5) &&
439             memcmp(get_version + 4, "V 2.2", 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         ttusb->revision = ((get_version[6] - '0') << 4) |
447                            (get_version[8] - '0');
448
449         err =
450             ttusb_cmd(ttusb, get_dsp_version, sizeof(get_dsp_version), 1);
451         if (err)
452                 return err;
453
454         err =
455             ttusb_result(ttusb, get_dsp_version, sizeof(get_dsp_version));
456         if (err)
457                 return err;
458         printk("%s: dsp-version: %c%c%c\n", __FUNCTION__,
459                get_dsp_version[4], get_dsp_version[5], get_dsp_version[6]);
460         return 0;
461 }
462
463 #ifdef TTUSB_DISEQC
464 static int ttusb_send_diseqc(struct dvb_frontend* fe,
465                              const struct dvb_diseqc_master_cmd *cmd)
466 {
467         struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
468         u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
469
470         int err;
471
472         b[3] = 4 + 2 + cmd->msg_len;
473         b[4] = 0xFF;            /* send diseqc master, not burst */
474         b[5] = cmd->msg_len;
475
476         memcpy(b + 5, cmd->msg, cmd->msg_len);
477
478         /* Diseqc */
479         if ((err = ttusb_cmd(ttusb, b, 4 + b[3], 0))) {
480                 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
481                         __FUNCTION__, err);
482         }
483
484         return err;
485 }
486 #endif
487
488 static int lnbp21_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
489 {
490         struct  ttusb* ttusb = (struct ttusb*)  fe->dvb->priv;
491         int ret;
492         u8 data[1];
493         struct i2c_msg msg = { .addr = 0x08, .flags = 0, .buf = data, .len = sizeof(data) };
494
495         switch(voltage) {
496         case SEC_VOLTAGE_OFF:
497                 data[0] = 0x00;
498                 break;
499         case SEC_VOLTAGE_13:
500                 data[0] = 0x44;
501                 break;
502         case SEC_VOLTAGE_18:
503                 data[0] = 0x4c;
504                 break;
505         default:
506                 return -EINVAL;
507         };
508
509         ret = i2c_transfer(&ttusb->i2c_adap, &msg, 1);
510         return (ret != 1) ? -EIO : 0;
511 }
512
513 static int ttusb_update_lnb(struct ttusb *ttusb)
514 {
515         u8 b[] = { 0xaa, ++ttusb->c, 0x16, 5, /*power: */ 1,
516                 ttusb->voltage == SEC_VOLTAGE_18 ? 0 : 1,
517                 ttusb->tone == SEC_TONE_ON ? 1 : 0, 1, 1
518         };
519         int err;
520
521         /* SetLNB */
522         if ((err = ttusb_cmd(ttusb, b, sizeof(b), 0))) {
523                 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
524                         __FUNCTION__, err);
525         }
526
527         return err;
528 }
529
530 static int ttusb_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
531 {
532         struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
533
534         ttusb->voltage = voltage;
535         return ttusb_update_lnb(ttusb);
536 }
537
538 #ifdef TTUSB_TONE
539 static int ttusb_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
540 {
541         struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
542
543         ttusb->tone = tone;
544         return ttusb_update_lnb(ttusb);
545 }
546 #endif
547
548
549 #if 0
550 static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
551 {
552         u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
553         int err, actual_len;
554
555         err = ttusb_cmd(ttusb, b, sizeof(b), 0);
556         if (err) {
557                 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
558                         __FUNCTION__, err);
559         }
560 }
561 #endif
562
563 /*****************************************************************************/
564
565 #ifdef TTUSB_HWSECTIONS
566 static void ttusb_handle_ts_data(struct ttusb_channel *channel,
567                                  const u8 * data, int len);
568 static void ttusb_handle_sec_data(struct ttusb_channel *channel,
569                                   const u8 * data, int len);
570 #endif
571
572 static int numpkt = 0, lastj, numts, numstuff, numsec, numinvalid;
573
574 static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
575                            int len)
576 {
577         u16 csum = 0, cc;
578         int i;
579         for (i = 0; i < len; i += 2)
580                 csum ^= le16_to_cpup((u16 *) (muxpack + i));
581         if (csum) {
582                 printk("%s: muxpack with incorrect checksum, ignoring\n",
583                        __FUNCTION__);
584                 numinvalid++;
585                 return;
586         }
587
588         cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
589         cc &= 0x7FFF;
590         if ((cc != ttusb->cc) && (ttusb->cc != -1))
591                 printk("%s: cc discontinuity (%d frames missing)\n",
592                        __FUNCTION__, (cc - ttusb->cc) & 0x7FFF);
593         ttusb->cc = (cc + 1) & 0x7FFF;
594         if (muxpack[0] & 0x80) {
595 #ifdef TTUSB_HWSECTIONS
596                 /* section data */
597                 int pusi = muxpack[0] & 0x40;
598                 int channel = muxpack[0] & 0x1F;
599                 int payload = muxpack[1];
600                 const u8 *data = muxpack + 2;
601                 /* check offset flag */
602                 if (muxpack[0] & 0x20)
603                         data++;
604
605                 ttusb_handle_sec_data(ttusb->channel + channel, data,
606                                       payload);
607                 data += payload;
608
609                 if ((!!(ttusb->muxpack[0] & 0x20)) ^
610                     !!(ttusb->muxpack[1] & 1))
611                         data++;
612 #warning TODO: pusi
613                 printk("cc: %04x\n", (data[0] << 8) | data[1]);
614 #endif
615                 numsec++;
616         } else if (muxpack[0] == 0x47) {
617 #ifdef TTUSB_HWSECTIONS
618                 /* we have TS data here! */
619                 int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
620                 int channel;
621                 for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
622                         if (ttusb->channel[channel].active
623                             && (pid == ttusb->channel[channel].pid))
624                                 ttusb_handle_ts_data(ttusb->channel +
625                                                      channel, muxpack,
626                                                      188);
627 #endif
628                 numts++;
629                 dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
630         } else if (muxpack[0] != 0) {
631                 numinvalid++;
632                 printk("illegal muxpack type %02x\n", muxpack[0]);
633         } else
634                 numstuff++;
635 }
636
637 static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
638 {
639         int maxwork = 1024;
640         while (len) {
641                 if (!(maxwork--)) {
642                         printk("%s: too much work\n", __FUNCTION__);
643                         break;
644                 }
645
646                 switch (ttusb->mux_state) {
647                 case 0:
648                 case 1:
649                 case 2:
650                         len--;
651                         if (*data++ == 0xAA)
652                                 ++ttusb->mux_state;
653                         else {
654                                 ttusb->mux_state = 0;
655 #if DEBUG > 3
656                                 if (ttusb->insync)
657                                         printk("%02x ", data[-1]);
658 #else
659                                 if (ttusb->insync) {
660                                         printk("%s: lost sync.\n",
661                                                __FUNCTION__);
662                                         ttusb->insync = 0;
663                                 }
664 #endif
665                         }
666                         break;
667                 case 3:
668                         ttusb->insync = 1;
669                         len--;
670                         ttusb->mux_npacks = *data++;
671                         ++ttusb->mux_state;
672                         ttusb->muxpack_ptr = 0;
673                         /* maximum bytes, until we know the length */
674                         ttusb->muxpack_len = 2;
675                         break;
676                 case 4:
677                         {
678                                 int avail;
679                                 avail = len;
680                                 if (avail >
681                                     (ttusb->muxpack_len -
682                                      ttusb->muxpack_ptr))
683                                         avail =
684                                             ttusb->muxpack_len -
685                                             ttusb->muxpack_ptr;
686                                 memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
687                                        data, avail);
688                                 ttusb->muxpack_ptr += avail;
689                                 if (ttusb->muxpack_ptr > 264)
690                                         BUG();
691                                 data += avail;
692                                 len -= avail;
693                                 /* determine length */
694                                 if (ttusb->muxpack_ptr == 2) {
695                                         if (ttusb->muxpack[0] & 0x80) {
696                                                 ttusb->muxpack_len =
697                                                     ttusb->muxpack[1] + 2;
698                                                 if (ttusb->
699                                                     muxpack[0] & 0x20)
700                                                         ttusb->
701                                                             muxpack_len++;
702                                                 if ((!!
703                                                      (ttusb->
704                                                       muxpack[0] & 0x20)) ^
705                                                     !!(ttusb->
706                                                        muxpack[1] & 1))
707                                                         ttusb->
708                                                             muxpack_len++;
709                                                 ttusb->muxpack_len += 4;
710                                         } else if (ttusb->muxpack[0] ==
711                                                    0x47)
712                                                 ttusb->muxpack_len =
713                                                     188 + 4;
714                                         else if (ttusb->muxpack[0] == 0x00)
715                                                 ttusb->muxpack_len =
716                                                     ttusb->muxpack[1] + 2 +
717                                                     4;
718                                         else {
719                                                 dprintk
720                                                     ("%s: invalid state: first byte is %x\n",
721                                                      __FUNCTION__,
722                                                      ttusb->muxpack[0]);
723                                                 ttusb->mux_state = 0;
724                                         }
725                                 }
726
727                         /**
728                          * if length is valid and we reached the end:
729                          * goto next muxpack
730                          */
731                                 if ((ttusb->muxpack_ptr >= 2) &&
732                                     (ttusb->muxpack_ptr ==
733                                      ttusb->muxpack_len)) {
734                                         ttusb_process_muxpack(ttusb,
735                                                               ttusb->
736                                                               muxpack,
737                                                               ttusb->
738                                                               muxpack_ptr);
739                                         ttusb->muxpack_ptr = 0;
740                                         /* maximum bytes, until we know the length */
741                                         ttusb->muxpack_len = 2;
742
743                                 /**
744                                  * no muxpacks left?
745                                  * return to search-sync state
746                                  */
747                                         if (!ttusb->mux_npacks--) {
748                                                 ttusb->mux_state = 0;
749                                                 break;
750                                         }
751                                 }
752                                 break;
753                         }
754                 default:
755                         BUG();
756                         break;
757                 }
758         }
759 }
760
761 static void ttusb_iso_irq(struct urb *urb, struct pt_regs *ptregs)
762 {
763         struct ttusb *ttusb = urb->context;
764
765         if (!ttusb->iso_streaming)
766                 return;
767
768 #if 0
769         printk("%s: status %d, errcount == %d, length == %i\n",
770                __FUNCTION__,
771                urb->status, urb->error_count, urb->actual_length);
772 #endif
773
774         if (!urb->status) {
775                 int i;
776                 for (i = 0; i < urb->number_of_packets; ++i) {
777                         struct usb_iso_packet_descriptor *d;
778                         u8 *data;
779                         int len;
780                         numpkt++;
781                         if ((jiffies - lastj) >= HZ) {
782 #if DEBUG > 2
783                                 printk
784                                     ("frames/s: %d (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
785                                      numpkt * HZ / (jiffies - lastj),
786                                      numts, numstuff, numsec, numinvalid,
787                                      numts + numstuff + numsec +
788                                      numinvalid);
789 #endif
790                                 numts = numstuff = numsec = numinvalid = 0;
791                                 lastj = jiffies;
792                                 numpkt = 0;
793                         }
794                         d = &urb->iso_frame_desc[i];
795                         data = urb->transfer_buffer + d->offset;
796                         len = d->actual_length;
797                         d->actual_length = 0;
798                         d->status = 0;
799                         ttusb_process_frame(ttusb, data, len);
800                 }
801         }
802         usb_submit_urb(urb, GFP_ATOMIC);
803 }
804
805 static void ttusb_free_iso_urbs(struct ttusb *ttusb)
806 {
807         int i;
808
809         for (i = 0; i < ISO_BUF_COUNT; i++)
810                 if (ttusb->iso_urb[i])
811                         usb_free_urb(ttusb->iso_urb[i]);
812
813         pci_free_consistent(NULL,
814                             ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF *
815                             ISO_BUF_COUNT, ttusb->iso_buffer,
816                             ttusb->iso_dma_handle);
817 }
818
819 static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
820 {
821         int i;
822
823         ttusb->iso_buffer = pci_alloc_consistent(NULL,
824                                                  ISO_FRAME_SIZE *
825                                                  FRAMES_PER_ISO_BUF *
826                                                  ISO_BUF_COUNT,
827                                                  &ttusb->iso_dma_handle);
828
829         memset(ttusb->iso_buffer, 0,
830                ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF * ISO_BUF_COUNT);
831
832         for (i = 0; i < ISO_BUF_COUNT; i++) {
833                 struct urb *urb;
834
835                 if (!
836                     (urb =
837                      usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
838                         ttusb_free_iso_urbs(ttusb);
839                         return -ENOMEM;
840                 }
841
842                 ttusb->iso_urb[i] = urb;
843         }
844
845         return 0;
846 }
847
848 static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
849 {
850         int i;
851
852         for (i = 0; i < ISO_BUF_COUNT; i++)
853                 usb_kill_urb(ttusb->iso_urb[i]);
854
855         ttusb->iso_streaming = 0;
856 }
857
858 static int ttusb_start_iso_xfer(struct ttusb *ttusb)
859 {
860         int i, j, err, buffer_offset = 0;
861
862         if (ttusb->iso_streaming) {
863                 printk("%s: iso xfer already running!\n", __FUNCTION__);
864                 return 0;
865         }
866
867         ttusb->cc = -1;
868         ttusb->insync = 0;
869         ttusb->mux_state = 0;
870
871         for (i = 0; i < ISO_BUF_COUNT; i++) {
872                 int frame_offset = 0;
873                 struct urb *urb = ttusb->iso_urb[i];
874
875                 urb->dev = ttusb->dev;
876                 urb->context = ttusb;
877                 urb->complete = ttusb_iso_irq;
878                 urb->pipe = ttusb->isoc_in_pipe;
879                 urb->transfer_flags = URB_ISO_ASAP;
880                 urb->interval = 1;
881                 urb->number_of_packets = FRAMES_PER_ISO_BUF;
882                 urb->transfer_buffer_length =
883                     ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
884                 urb->transfer_buffer = ttusb->iso_buffer + buffer_offset;
885                 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
886
887                 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
888                         urb->iso_frame_desc[j].offset = frame_offset;
889                         urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
890                         frame_offset += ISO_FRAME_SIZE;
891                 }
892         }
893
894         for (i = 0; i < ISO_BUF_COUNT; i++) {
895                 if ((err = usb_submit_urb(ttusb->iso_urb[i], GFP_ATOMIC))) {
896                         ttusb_stop_iso_xfer(ttusb);
897                         printk
898                             ("%s: failed urb submission (%i: err = %i)!\n",
899                              __FUNCTION__, i, err);
900                         return err;
901                 }
902         }
903
904         ttusb->iso_streaming = 1;
905
906         return 0;
907 }
908
909 #ifdef TTUSB_HWSECTIONS
910 static void ttusb_handle_ts_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
911                           int len)
912 {
913         dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
914 }
915
916 static void ttusb_handle_sec_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
917                            int len)
918 {
919 //      struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
920 #error TODO: handle ugly stuff
921 //      dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
922 }
923 #endif
924
925 static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
926 {
927         struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
928         int feed_type = 1;
929
930         dprintk("ttusb_start_feed\n");
931
932         switch (dvbdmxfeed->type) {
933         case DMX_TYPE_TS:
934                 break;
935         case DMX_TYPE_SEC:
936                 break;
937         default:
938                 return -EINVAL;
939         }
940
941         if (dvbdmxfeed->type == DMX_TYPE_TS) {
942                 switch (dvbdmxfeed->pes_type) {
943                 case DMX_TS_PES_VIDEO:
944                 case DMX_TS_PES_AUDIO:
945                 case DMX_TS_PES_TELETEXT:
946                 case DMX_TS_PES_PCR:
947                 case DMX_TS_PES_OTHER:
948                         break;
949                 default:
950                         return -EINVAL;
951                 }
952         }
953
954 #ifdef TTUSB_HWSECTIONS
955 #error TODO: allocate filters
956         if (dvbdmxfeed->type == DMX_TYPE_TS) {
957                 feed_type = 1;
958         } else if (dvbdmxfeed->type == DMX_TYPE_SEC) {
959                 feed_type = 2;
960         }
961 #endif
962
963         ttusb_set_channel(ttusb, dvbdmxfeed->index, feed_type, dvbdmxfeed->pid);
964
965         if (0 == ttusb->running_feed_count++)
966                 ttusb_start_iso_xfer(ttusb);
967
968         return 0;
969 }
970
971 static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
972 {
973         struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
974
975         ttusb_del_channel(ttusb, dvbdmxfeed->index);
976
977         if (--ttusb->running_feed_count == 0)
978                 ttusb_stop_iso_xfer(ttusb);
979
980         return 0;
981 }
982
983 static int ttusb_setup_interfaces(struct ttusb *ttusb)
984 {
985         usb_set_interface(ttusb->dev, 1, 1);
986
987         ttusb->bulk_out_pipe = usb_sndbulkpipe(ttusb->dev, 1);
988         ttusb->bulk_in_pipe = usb_rcvbulkpipe(ttusb->dev, 1);
989         ttusb->isoc_in_pipe = usb_rcvisocpipe(ttusb->dev, 2);
990
991         return 0;
992 }
993
994 #if 0
995 static u8 stc_firmware[8192];
996
997 static int stc_open(struct inode *inode, struct file *file)
998 {
999         struct ttusb *ttusb = file->private_data;
1000         int addr;
1001
1002         for (addr = 0; addr < 8192; addr += 16) {
1003                 u8 snd_buf[2] = { addr >> 8, addr & 0xFF };
1004                 ttusb_i2c_msg(ttusb, 0x50, snd_buf, 2, stc_firmware + addr,
1005                               16);
1006         }
1007
1008         return 0;
1009 }
1010
1011 static ssize_t stc_read(struct file *file, char *buf, size_t count,
1012                  loff_t * offset)
1013 {
1014         int tc = count;
1015
1016         if ((tc + *offset) > 8192)
1017                 tc = 8192 - *offset;
1018
1019         if (tc < 0)
1020                 return 0;
1021
1022         if (copy_to_user(buf, stc_firmware + *offset, tc))
1023                 return -EFAULT;
1024
1025         *offset += tc;
1026
1027         return tc;
1028 }
1029
1030 static int stc_release(struct inode *inode, struct file *file)
1031 {
1032         return 0;
1033 }
1034
1035 static struct file_operations stc_fops = {
1036         .owner = THIS_MODULE,
1037         .read = stc_read,
1038         .open = stc_open,
1039         .release = stc_release,
1040 };
1041 #endif
1042
1043 static u32 functionality(struct i2c_adapter *adapter)
1044 {
1045         return I2C_FUNC_I2C;
1046 }
1047
1048
1049
1050 static int alps_tdmb7_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1051 {
1052         struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1053         u8 data[4];
1054         struct i2c_msg msg = {.addr=0x61, .flags=0, .buf=data, .len=sizeof(data) };
1055         u32 div;
1056
1057         div = (params->frequency + 36166667) / 166667;
1058
1059         data[0] = (div >> 8) & 0x7f;
1060         data[1] = div & 0xff;
1061         data[2] = ((div >> 10) & 0x60) | 0x85;
1062         data[3] = params->frequency < 592000000 ? 0x40 : 0x80;
1063
1064         if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1) return -EIO;
1065         return 0;
1066 }
1067
1068 static struct cx22700_config alps_tdmb7_config = {
1069         .demod_address = 0x43,
1070         .pll_set = alps_tdmb7_pll_set,
1071 };
1072
1073
1074
1075
1076
1077 static int philips_tdm1316l_pll_init(struct dvb_frontend* fe)
1078 {
1079         struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1080         static u8 td1316_init[] = { 0x0b, 0xf5, 0x85, 0xab };
1081         static u8 disable_mc44BC374c[] = { 0x1d, 0x74, 0xa0, 0x68 };
1082         struct i2c_msg tuner_msg = { .addr=0x60, .flags=0, .buf=td1316_init, .len=sizeof(td1316_init) };
1083
1084         // setup PLL configuration
1085         if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) return -EIO;
1086         msleep(1);
1087
1088         // disable the mc44BC374c (do not check for errors)
1089         tuner_msg.addr = 0x65;
1090         tuner_msg.buf = disable_mc44BC374c;
1091         tuner_msg.len = sizeof(disable_mc44BC374c);
1092         if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1093                 i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1);
1094         }
1095
1096         return 0;
1097 }
1098
1099 static int philips_tdm1316l_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1100 {
1101         struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1102         u8 tuner_buf[4];
1103         struct i2c_msg tuner_msg = {.addr=0x60, .flags=0, .buf=tuner_buf, .len=sizeof(tuner_buf) };
1104         int tuner_frequency = 0;
1105         u8 band, cp, filter;
1106
1107         // determine charge pump
1108         tuner_frequency = params->frequency + 36130000;
1109         if (tuner_frequency < 87000000) return -EINVAL;
1110         else if (tuner_frequency < 130000000) cp = 3;
1111         else if (tuner_frequency < 160000000) cp = 5;
1112         else if (tuner_frequency < 200000000) cp = 6;
1113         else if (tuner_frequency < 290000000) cp = 3;
1114         else if (tuner_frequency < 420000000) cp = 5;
1115         else if (tuner_frequency < 480000000) cp = 6;
1116         else if (tuner_frequency < 620000000) cp = 3;
1117         else if (tuner_frequency < 830000000) cp = 5;
1118         else if (tuner_frequency < 895000000) cp = 7;
1119         else return -EINVAL;
1120
1121         // determine band
1122         if (params->frequency < 49000000) return -EINVAL;
1123         else if (params->frequency < 159000000) band = 1;
1124         else if (params->frequency < 444000000) band = 2;
1125         else if (params->frequency < 861000000) band = 4;
1126         else return -EINVAL;
1127
1128         // setup PLL filter
1129         switch (params->u.ofdm.bandwidth) {
1130         case BANDWIDTH_6_MHZ:
1131                 tda1004x_write_byte(fe, 0x0C, 0);
1132                 filter = 0;
1133                 break;
1134
1135         case BANDWIDTH_7_MHZ:
1136                 tda1004x_write_byte(fe, 0x0C, 0);
1137                 filter = 0;
1138                 break;
1139
1140         case BANDWIDTH_8_MHZ:
1141                 tda1004x_write_byte(fe, 0x0C, 0xFF);
1142                 filter = 1;
1143                 break;
1144
1145         default:
1146                 return -EINVAL;
1147         }
1148
1149         // calculate divisor
1150         // ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
1151         tuner_frequency = (((params->frequency / 1000) * 6) + 217280) / 1000;
1152
1153         // setup tuner buffer
1154         tuner_buf[0] = tuner_frequency >> 8;
1155         tuner_buf[1] = tuner_frequency & 0xff;
1156         tuner_buf[2] = 0xca;
1157         tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1158
1159         if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1)
1160                 return -EIO;
1161
1162         msleep(1);
1163         return 0;
1164 }
1165
1166 static int philips_tdm1316l_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1167 {
1168         struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1169
1170         return request_firmware(fw, name, &ttusb->dev->dev);
1171 }
1172
1173 static struct tda1004x_config philips_tdm1316l_config = {
1174
1175         .demod_address = 0x8,
1176         .invert = 1,
1177         .invert_oclk = 0,
1178         .pll_init = philips_tdm1316l_pll_init,
1179         .pll_set = philips_tdm1316l_pll_set,
1180         .request_firmware = philips_tdm1316l_request_firmware,
1181 };
1182
1183 static u8 alps_bsbe1_inittab[] = {
1184         0x01, 0x15,
1185         0x02, 0x30,
1186         0x03, 0x00,
1187         0x04, 0x7d,             /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1188         0x05, 0x35,             /* I2CT = 0, SCLT = 1, SDAT = 1 */
1189         0x06, 0x40,             /* DAC not used, set to high impendance mode */
1190         0x07, 0x00,             /* DAC LSB */
1191         0x08, 0x40,             /* DiSEqC off, LNB power on OP2/LOCK pin on */
1192         0x09, 0x00,             /* FIFO */
1193         0x0c, 0x51,             /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1194         0x0d, 0x82,             /* DC offset compensation = ON, beta_agc1 = 2 */
1195         0x0e, 0x23,             /* alpha_tmg = 2, beta_tmg = 3 */
1196         0x10, 0x3f,             // AGC2  0x3d
1197         0x11, 0x84,
1198         0x12, 0xb5,             // Lock detect: -64  Carrier freq detect:on
1199         0x15, 0xc9,             // lock detector threshold
1200         0x16, 0x00,
1201         0x17, 0x00,
1202         0x18, 0x00,
1203         0x19, 0x00,
1204         0x1a, 0x00,
1205         0x1f, 0x50,
1206         0x20, 0x00,
1207         0x21, 0x00,
1208         0x22, 0x00,
1209         0x23, 0x00,
1210         0x28, 0x00,             // out imp: normal  out type: parallel FEC mode:0
1211         0x29, 0x1e,             // 1/2 threshold
1212         0x2a, 0x14,             // 2/3 threshold
1213         0x2b, 0x0f,             // 3/4 threshold
1214         0x2c, 0x09,             // 5/6 threshold
1215         0x2d, 0x05,             // 7/8 threshold
1216         0x2e, 0x01,
1217         0x31, 0x1f,             // test all FECs
1218         0x32, 0x19,             // viterbi and synchro search
1219         0x33, 0xfc,             // rs control
1220         0x34, 0x93,             // error control
1221         0x0f, 0x92,
1222         0xff, 0xff
1223 };
1224
1225 static u8 alps_bsru6_inittab[] = {
1226         0x01, 0x15,
1227         0x02, 0x30,
1228         0x03, 0x00,
1229         0x04, 0x7d,             /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1230         0x05, 0x35,             /* I2CT = 0, SCLT = 1, SDAT = 1 */
1231         0x06, 0x40,             /* DAC not used, set to high impendance mode */
1232         0x07, 0x00,             /* DAC LSB */
1233         0x08, 0x40,             /* DiSEqC off, LNB power on OP2/LOCK pin on */
1234         0x09, 0x00,             /* FIFO */
1235         0x0c, 0x51,             /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1236         0x0d, 0x82,             /* DC offset compensation = ON, beta_agc1 = 2 */
1237         0x0e, 0x23,             /* alpha_tmg = 2, beta_tmg = 3 */
1238         0x10, 0x3f,             // AGC2  0x3d
1239         0x11, 0x84,
1240         0x12, 0xb5,             // Lock detect: -64  Carrier freq detect:on
1241         0x15, 0xc9,             // lock detector threshold
1242         0x16, 0x00,
1243         0x17, 0x00,
1244         0x18, 0x00,
1245         0x19, 0x00,
1246         0x1a, 0x00,
1247         0x1f, 0x50,
1248         0x20, 0x00,
1249         0x21, 0x00,
1250         0x22, 0x00,
1251         0x23, 0x00,
1252         0x28, 0x00,             // out imp: normal  out type: parallel FEC mode:0
1253         0x29, 0x1e,             // 1/2 threshold
1254         0x2a, 0x14,             // 2/3 threshold
1255         0x2b, 0x0f,             // 3/4 threshold
1256         0x2c, 0x09,             // 5/6 threshold
1257         0x2d, 0x05,             // 7/8 threshold
1258         0x2e, 0x01,
1259         0x31, 0x1f,             // test all FECs
1260         0x32, 0x19,             // viterbi and synchro search
1261         0x33, 0xfc,             // rs control
1262         0x34, 0x93,             // error control
1263         0x0f, 0x52,
1264         0xff, 0xff
1265 };
1266
1267 static int alps_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
1268 {
1269         u8 aclk = 0;
1270         u8 bclk = 0;
1271
1272         if (srate < 1500000) {
1273                 aclk = 0xb7;
1274                 bclk = 0x47;
1275         } else if (srate < 3000000) {
1276                 aclk = 0xb7;
1277                 bclk = 0x4b;
1278         } else if (srate < 7000000) {
1279                 aclk = 0xb7;
1280                 bclk = 0x4f;
1281         } else if (srate < 14000000) {
1282                 aclk = 0xb7;
1283                 bclk = 0x53;
1284         } else if (srate < 30000000) {
1285                 aclk = 0xb6;
1286                 bclk = 0x53;
1287         } else if (srate < 45000000) {
1288                 aclk = 0xb4;
1289                 bclk = 0x51;
1290         }
1291
1292         stv0299_writereg(fe, 0x13, aclk);
1293         stv0299_writereg(fe, 0x14, bclk);
1294         stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
1295         stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
1296         stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
1297
1298         return 0;
1299 }
1300
1301 static int philips_tsa5059_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1302 {
1303         struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1304         u8 buf[4];
1305         u32 div;
1306         struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1307
1308         if ((params->frequency < 950000) || (params->frequency > 2150000))
1309                 return -EINVAL;
1310
1311         div = (params->frequency + (125 - 1)) / 125;    // round correctly
1312         buf[0] = (div >> 8) & 0x7f;
1313         buf[1] = div & 0xff;
1314         buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
1315         buf[3] = 0xC4;
1316
1317         if (params->frequency > 1530000)
1318                 buf[3] = 0xC0;
1319
1320         /* BSBE1 wants XCE bit set */
1321         if (ttusb->revision == TTUSB_REV_2_2)
1322                 buf[3] |= 0x20;
1323
1324         if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1325                 return -EIO;
1326
1327         return 0;
1328 }
1329
1330 static struct stv0299_config alps_stv0299_config = {
1331         .demod_address = 0x68,
1332         .inittab = alps_bsru6_inittab,
1333         .mclk = 88000000UL,
1334         .invert = 1,
1335         .enhanced_tuning = 0,
1336         .skip_reinit = 0,
1337         .lock_output = STV0229_LOCKOUTPUT_1,
1338         .volt13_op0_op1 = STV0299_VOLT13_OP1,
1339         .min_delay_ms = 100,
1340         .set_symbol_rate = alps_stv0299_set_symbol_rate,
1341         .pll_set = philips_tsa5059_pll_set,
1342 };
1343
1344 static int ttusb_novas_grundig_29504_491_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1345 {
1346         struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1347         u8 buf[4];
1348         u32 div;
1349         struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1350
1351         div = params->frequency / 125;
1352
1353         buf[0] = (div >> 8) & 0x7f;
1354         buf[1] = div & 0xff;
1355         buf[2] = 0x8e;
1356         buf[3] = 0x00;
1357
1358         if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1359                 return -EIO;
1360
1361         return 0;
1362 }
1363
1364 static struct tda8083_config ttusb_novas_grundig_29504_491_config = {
1365
1366         .demod_address = 0x68,
1367         .pll_set = ttusb_novas_grundig_29504_491_pll_set,
1368 };
1369
1370
1371
1372 static void frontend_init(struct ttusb* ttusb)
1373 {
1374         switch(le16_to_cpu(ttusb->dev->descriptor.idProduct)) {
1375         case 0x1003: // Hauppauge/TT Nova-USB-S budget (stv0299/ALPS BSRU6|BSBE1(tsa5059))
1376                 // try the stv0299 based first
1377                 ttusb->fe = stv0299_attach(&alps_stv0299_config, &ttusb->i2c_adap);
1378                 if (ttusb->fe != NULL) {
1379                         if(ttusb->revision == TTUSB_REV_2_2) { // ALPS BSBE1
1380                                 alps_stv0299_config.inittab = alps_bsbe1_inittab;
1381                                 ttusb->fe->ops->set_voltage = lnbp21_set_voltage;
1382                         } else { // ALPS BSRU6
1383                                 ttusb->fe->ops->set_voltage = ttusb_set_voltage;
1384                         }
1385                         break;
1386                 }
1387
1388                 // Grundig 29504-491
1389                 ttusb->fe = tda8083_attach(&ttusb_novas_grundig_29504_491_config, &ttusb->i2c_adap);
1390                 if (ttusb->fe != NULL) {
1391                         ttusb->fe->ops->set_voltage = ttusb_set_voltage;
1392                         break;
1393                 }
1394
1395                 break;
1396
1397         case 0x1005: // Hauppauge/TT Nova-USB-t budget (tda10046/Philips td1316(tda6651tt) OR cx22700/ALPS TDMB7(??))
1398                 // try the ALPS TDMB7 first
1399                 ttusb->fe = cx22700_attach(&alps_tdmb7_config, &ttusb->i2c_adap);
1400                 if (ttusb->fe != NULL)
1401                         break;
1402
1403                 // Philips td1316
1404                 ttusb->fe = tda10046_attach(&philips_tdm1316l_config, &ttusb->i2c_adap);
1405                 if (ttusb->fe != NULL)
1406                         break;
1407                 break;
1408         }
1409
1410         if (ttusb->fe == NULL) {
1411                 printk("dvb-ttusb-budget: A frontend driver was not found for device %04x/%04x\n",
1412                        le16_to_cpu(ttusb->dev->descriptor.idVendor),
1413                        le16_to_cpu(ttusb->dev->descriptor.idProduct));
1414         } else {
1415                 if (dvb_register_frontend(&ttusb->adapter, ttusb->fe)) {
1416                         printk("dvb-ttusb-budget: Frontend registration failed!\n");
1417                         if (ttusb->fe->ops->release)
1418                                 ttusb->fe->ops->release(ttusb->fe);
1419                         ttusb->fe = NULL;
1420                 }
1421         }
1422 }
1423
1424
1425
1426 static struct i2c_algorithm ttusb_dec_algo = {
1427         .name           = "ttusb dec i2c algorithm",
1428         .id             = I2C_ALGO_BIT,
1429         .master_xfer    = master_xfer,
1430         .functionality  = functionality,
1431 };
1432
1433 static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1434 {
1435         struct usb_device *udev;
1436         struct ttusb *ttusb;
1437         int result;
1438
1439         dprintk("%s: TTUSB DVB connected\n", __FUNCTION__);
1440
1441         udev = interface_to_usbdev(intf);
1442
1443         if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
1444
1445         if (!(ttusb = kmalloc(sizeof(struct ttusb), GFP_KERNEL)))
1446                 return -ENOMEM;
1447
1448         memset(ttusb, 0, sizeof(struct ttusb));
1449
1450         ttusb->dev = udev;
1451         ttusb->c = 0;
1452         ttusb->mux_state = 0;
1453         sema_init(&ttusb->semi2c, 0);
1454         sema_init(&ttusb->semusb, 1);
1455
1456         ttusb_setup_interfaces(ttusb);
1457
1458         ttusb_alloc_iso_urbs(ttusb);
1459         if (ttusb_init_controller(ttusb))
1460                 printk("ttusb_init_controller: error\n");
1461
1462         up(&ttusb->semi2c);
1463
1464         dvb_register_adapter(&ttusb->adapter, "Technotrend/Hauppauge Nova-USB", THIS_MODULE);
1465         ttusb->adapter.priv = ttusb;
1466
1467         /* i2c */
1468         memset(&ttusb->i2c_adap, 0, sizeof(struct i2c_adapter));
1469         strcpy(ttusb->i2c_adap.name, "TTUSB DEC");
1470
1471         i2c_set_adapdata(&ttusb->i2c_adap, ttusb);
1472
1473 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
1474         ttusb->i2c_adap.class             = I2C_ADAP_CLASS_TV_DIGITAL;
1475 #else
1476         ttusb->i2c_adap.class             = I2C_CLASS_TV_DIGITAL;
1477 #endif
1478         ttusb->i2c_adap.algo              = &ttusb_dec_algo;
1479         ttusb->i2c_adap.algo_data         = NULL;
1480         ttusb->i2c_adap.id                = I2C_ALGO_BIT;
1481
1482         result = i2c_add_adapter(&ttusb->i2c_adap);
1483         if (result) {
1484                 dvb_unregister_adapter (&ttusb->adapter);
1485                 return result;
1486         }
1487
1488         memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1489
1490         ttusb->dvb_demux.dmx.capabilities =
1491             DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1492         ttusb->dvb_demux.priv = NULL;
1493 #ifdef TTUSB_HWSECTIONS
1494         ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1495 #else
1496         ttusb->dvb_demux.filternum = 32;
1497 #endif
1498         ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1499         ttusb->dvb_demux.start_feed = ttusb_start_feed;
1500         ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1501         ttusb->dvb_demux.write_to_decoder = NULL;
1502
1503         if ((result = dvb_dmx_init(&ttusb->dvb_demux)) < 0) {
1504                 printk("ttusb_dvb: dvb_dmx_init failed (errno = %d)\n", result);
1505                 i2c_del_adapter(&ttusb->i2c_adap);
1506                 dvb_unregister_adapter (&ttusb->adapter);
1507                 return -ENODEV;
1508         }
1509 //FIXME dmxdev (nur WAS?)
1510         ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1511         ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1512         ttusb->dmxdev.capabilities = 0;
1513
1514         if ((result = dvb_dmxdev_init(&ttusb->dmxdev, &ttusb->adapter)) < 0) {
1515                 printk("ttusb_dvb: dvb_dmxdev_init failed (errno = %d)\n",
1516                        result);
1517                 dvb_dmx_release(&ttusb->dvb_demux);
1518                 i2c_del_adapter(&ttusb->i2c_adap);
1519                 dvb_unregister_adapter (&ttusb->adapter);
1520                 return -ENODEV;
1521         }
1522
1523         if (dvb_net_init(&ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
1524                 printk("ttusb_dvb: dvb_net_init failed!\n");
1525                 dvb_dmxdev_release(&ttusb->dmxdev);
1526                 dvb_dmx_release(&ttusb->dvb_demux);
1527                 i2c_del_adapter(&ttusb->i2c_adap);
1528                 dvb_unregister_adapter (&ttusb->adapter);
1529                 return -ENODEV;
1530         }
1531
1532 #if 0
1533         ttusb->stc_devfs_handle =
1534             devfs_register(ttusb->adapter->devfs_handle, TTUSB_BUDGET_NAME,
1535                            DEVFS_FL_DEFAULT, 0, 192,
1536                            S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
1537                            | S_IROTH | S_IWOTH, &stc_fops, ttusb);
1538 #endif
1539         usb_set_intfdata(intf, (void *) ttusb);
1540
1541         frontend_init(ttusb);
1542
1543         return 0;
1544 }
1545
1546 static void ttusb_disconnect(struct usb_interface *intf)
1547 {
1548         struct ttusb *ttusb = usb_get_intfdata(intf);
1549
1550         usb_set_intfdata(intf, NULL);
1551
1552         ttusb->disconnecting = 1;
1553
1554         ttusb_stop_iso_xfer(ttusb);
1555
1556         ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1557         dvb_net_release(&ttusb->dvbnet);
1558         dvb_dmxdev_release(&ttusb->dmxdev);
1559         dvb_dmx_release(&ttusb->dvb_demux);
1560         if (ttusb->fe != NULL) dvb_unregister_frontend(ttusb->fe);
1561         i2c_del_adapter(&ttusb->i2c_adap);
1562         dvb_unregister_adapter(&ttusb->adapter);
1563
1564         ttusb_free_iso_urbs(ttusb);
1565
1566         kfree(ttusb);
1567
1568         dprintk("%s: TTUSB DVB disconnected\n", __FUNCTION__);
1569 }
1570
1571 static struct usb_device_id ttusb_table[] = {
1572         {USB_DEVICE(0xb48, 0x1003)},
1573 /*      {USB_DEVICE(0xb48, 0x1004)},UNDEFINED HARDWARE - mail linuxtv.org list*/        /* to be confirmed ????  */
1574         {USB_DEVICE(0xb48, 0x1005)},
1575         {}
1576 };
1577
1578 MODULE_DEVICE_TABLE(usb, ttusb_table);
1579
1580 static struct usb_driver ttusb_driver = {
1581       .name             = "Technotrend/Hauppauge USB-Nova",
1582       .probe            = ttusb_probe,
1583       .disconnect       = ttusb_disconnect,
1584       .id_table         = ttusb_table,
1585 };
1586
1587 static int __init ttusb_init(void)
1588 {
1589         int err;
1590
1591         if ((err = usb_register(&ttusb_driver)) < 0) {
1592                 printk("%s: usb_register failed! Error number %d",
1593                        __FILE__, err);
1594                 return err;
1595         }
1596
1597         return 0;
1598 }
1599
1600 static void __exit ttusb_exit(void)
1601 {
1602         usb_deregister(&ttusb_driver);
1603 }
1604
1605 module_init(ttusb_init);
1606 module_exit(ttusb_exit);
1607
1608 MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1609 MODULE_DESCRIPTION("TTUSB DVB Driver");
1610 MODULE_LICENSE("GPL");