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