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