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