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