Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / media / dvb / ttusb-budget / dvb-ttusb-budget.c
blob7902ae1d9a181c80e205eac147fdb92d3f704ee4
1 /*
2 * TTUSB DVB driver
4 * Copyright (c) 2002 Holger Waechtler <holger@convergence.de>
5 * Copyright (c) 2003 Felix Domke <tmbinc@elitedvb.net>
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.
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/wait.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 #include <linux/delay.h>
18 #include <linux/time.h>
19 #include <linux/errno.h>
20 #include <linux/jiffies.h>
21 #include <linux/mutex.h>
23 #include "dvb_frontend.h"
24 #include "dmxdev.h"
25 #include "dvb_demux.h"
26 #include "dvb_net.h"
27 #include "ves1820.h"
28 #include "cx22700.h"
29 #include "tda1004x.h"
30 #include "stv0299.h"
31 #include "tda8083.h"
32 #include "stv0297.h"
33 #include "lnbp21.h"
35 #include <linux/dvb/frontend.h>
36 #include <linux/dvb/dmx.h>
37 #include <linux/pci.h>
40 TTUSB_HWSECTIONS:
41 the DSP supports filtering in hardware, however, since the "muxstream"
42 is a bit braindead (no matching channel masks or no matching filter mask),
43 we won't support this - yet. it doesn't event support negative filters,
44 so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
45 parse TS data. USB bandwidth will be a problem when having large
46 datastreams, especially for dvb-net, but hey, that's not my problem.
48 TTUSB_DISEQC, TTUSB_TONE:
49 let the STC do the diseqc/tone stuff. this isn't supported at least with
50 my TTUSB, so let it undef'd unless you want to implement another
51 frontend. never tested.
53 DEBUG:
54 define it to > 3 for really hardcore debugging. you probably don't want
55 this unless the device doesn't load at all. > 2 for bandwidth statistics.
58 static int debug;
60 module_param(debug, int, 0644);
61 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
63 #define dprintk(x...) do { if (debug) printk(KERN_DEBUG x); } while (0)
65 #define ISO_BUF_COUNT 4
66 #define FRAMES_PER_ISO_BUF 4
67 #define ISO_FRAME_SIZE 912
68 #define TTUSB_MAXCHANNEL 32
69 #ifdef TTUSB_HWSECTIONS
70 #define TTUSB_MAXFILTER 16 /* ??? */
71 #endif
73 #define TTUSB_REV_2_2 0x22
74 #define TTUSB_BUDGET_NAME "ttusb_stc_fw"
76 /**
77 * since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
78 * the dvb_demux field must be the first in struct!!
80 struct ttusb {
81 struct dvb_demux dvb_demux;
82 struct dmxdev dmxdev;
83 struct dvb_net dvbnet;
85 /* and one for USB access. */
86 struct mutex semi2c;
87 struct mutex semusb;
89 struct dvb_adapter adapter;
90 struct usb_device *dev;
92 struct i2c_adapter i2c_adap;
94 int disconnecting;
95 int iso_streaming;
97 unsigned int bulk_out_pipe;
98 unsigned int bulk_in_pipe;
99 unsigned int isoc_in_pipe;
101 void *iso_buffer;
102 dma_addr_t iso_dma_handle;
104 struct urb *iso_urb[ISO_BUF_COUNT];
106 int running_feed_count;
107 int last_channel;
108 int last_filter;
110 u8 c; /* transaction counter, wraps around... */
111 fe_sec_tone_mode_t tone;
112 fe_sec_voltage_t voltage;
114 int mux_state; // 0..2 - MuxSyncWord, 3 - nMuxPacks, 4 - muxpack
115 u8 mux_npacks;
116 u8 muxpack[256 + 8];
117 int muxpack_ptr, muxpack_len;
119 int insync;
121 int cc; /* MuxCounter - will increment on EVERY MUX PACKET */
122 /* (including stuffing. yes. really.) */
124 u8 last_result[32];
126 int revision;
128 struct dvb_frontend* fe;
131 /* ugly workaround ... don't know why it's necessary to read */
132 /* all result codes. */
134 #define DEBUG 0
135 static int ttusb_cmd(struct ttusb *ttusb,
136 const u8 * data, int len, int needresult)
138 int actual_len;
139 int err;
140 #if DEBUG >= 3
141 int i;
143 printk(">");
144 for (i = 0; i < len; ++i)
145 printk(" %02x", data[i]);
146 printk("\n");
147 #endif
149 if (mutex_lock_interruptible(&ttusb->semusb) < 0)
150 return -EAGAIN;
152 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_out_pipe,
153 (u8 *) data, len, &actual_len, 1000);
154 if (err != 0) {
155 dprintk("%s: usb_bulk_msg(send) failed, err == %i!\n",
156 __FUNCTION__, err);
157 mutex_unlock(&ttusb->semusb);
158 return err;
160 if (actual_len != len) {
161 dprintk("%s: only wrote %d of %d bytes\n", __FUNCTION__,
162 actual_len, len);
163 mutex_unlock(&ttusb->semusb);
164 return -1;
167 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_in_pipe,
168 ttusb->last_result, 32, &actual_len, 1000);
170 if (err != 0) {
171 printk("%s: failed, receive error %d\n", __FUNCTION__,
172 err);
173 mutex_unlock(&ttusb->semusb);
174 return err;
176 #if DEBUG >= 3
177 actual_len = ttusb->last_result[3] + 4;
178 printk("<");
179 for (i = 0; i < actual_len; ++i)
180 printk(" %02x", ttusb->last_result[i]);
181 printk("\n");
182 #endif
183 if (!needresult)
184 mutex_unlock(&ttusb->semusb);
185 return 0;
188 static int ttusb_result(struct ttusb *ttusb, u8 * data, int len)
190 memcpy(data, ttusb->last_result, len);
191 mutex_unlock(&ttusb->semusb);
192 return 0;
195 static int ttusb_i2c_msg(struct ttusb *ttusb,
196 u8 addr, u8 * snd_buf, u8 snd_len, u8 * rcv_buf,
197 u8 rcv_len)
199 u8 b[0x28];
200 u8 id = ++ttusb->c;
201 int i, err;
203 if (snd_len > 0x28 - 7 || rcv_len > 0x20 - 7)
204 return -EINVAL;
206 b[0] = 0xaa;
207 b[1] = id;
208 b[2] = 0x31;
209 b[3] = snd_len + 3;
210 b[4] = addr << 1;
211 b[5] = snd_len;
212 b[6] = rcv_len;
214 for (i = 0; i < snd_len; i++)
215 b[7 + i] = snd_buf[i];
217 err = ttusb_cmd(ttusb, b, snd_len + 7, 1);
219 if (err)
220 return -EREMOTEIO;
222 err = ttusb_result(ttusb, b, 0x20);
224 /* check if the i2c transaction was successful */
225 if ((snd_len != b[5]) || (rcv_len != b[6])) return -EREMOTEIO;
227 if (rcv_len > 0) {
229 if (err || b[0] != 0x55 || b[1] != id) {
230 dprintk
231 ("%s: usb_bulk_msg(recv) failed, err == %i, id == %02x, b == ",
232 __FUNCTION__, err, id);
233 return -EREMOTEIO;
236 for (i = 0; i < rcv_len; i++)
237 rcv_buf[i] = b[7 + i];
240 return rcv_len;
243 static int master_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
245 struct ttusb *ttusb = i2c_get_adapdata(adapter);
246 int i = 0;
247 int inc;
249 if (mutex_lock_interruptible(&ttusb->semi2c) < 0)
250 return -EAGAIN;
252 while (i < num) {
253 u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
254 int err;
256 if (num > i + 1 && (msg[i + 1].flags & I2C_M_RD)) {
257 addr = msg[i].addr;
258 snd_buf = msg[i].buf;
259 snd_len = msg[i].len;
260 rcv_buf = msg[i + 1].buf;
261 rcv_len = msg[i + 1].len;
262 inc = 2;
263 } else {
264 addr = msg[i].addr;
265 snd_buf = msg[i].buf;
266 snd_len = msg[i].len;
267 rcv_buf = NULL;
268 rcv_len = 0;
269 inc = 1;
272 err = ttusb_i2c_msg(ttusb, addr,
273 snd_buf, snd_len, rcv_buf, rcv_len);
275 if (err < rcv_len) {
276 dprintk("%s: i == %i\n", __FUNCTION__, i);
277 break;
280 i += inc;
283 mutex_unlock(&ttusb->semi2c);
284 return i;
287 #include "dvb-ttusb-dspbootcode.h"
289 static int ttusb_boot_dsp(struct ttusb *ttusb)
291 int i, err;
292 u8 b[40];
294 /* BootBlock */
295 b[0] = 0xaa;
296 b[2] = 0x13;
297 b[3] = 28;
299 /* upload dsp code in 32 byte steps (36 didn't work for me ...) */
300 /* 32 is max packet size, no messages should be splitted. */
301 for (i = 0; i < sizeof(dsp_bootcode); i += 28) {
302 memcpy(&b[4], &dsp_bootcode[i], 28);
304 b[1] = ++ttusb->c;
306 err = ttusb_cmd(ttusb, b, 32, 0);
307 if (err)
308 goto done;
311 /* last block ... */
312 b[1] = ++ttusb->c;
313 b[2] = 0x13;
314 b[3] = 0;
316 err = ttusb_cmd(ttusb, b, 4, 0);
317 if (err)
318 goto done;
320 /* BootEnd */
321 b[1] = ++ttusb->c;
322 b[2] = 0x14;
323 b[3] = 0;
325 err = ttusb_cmd(ttusb, b, 4, 0);
327 done:
328 if (err) {
329 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
330 __FUNCTION__, err);
333 return err;
336 static int ttusb_set_channel(struct ttusb *ttusb, int chan_id, int filter_type,
337 int pid)
339 int err;
340 /* SetChannel */
341 u8 b[] = { 0xaa, ++ttusb->c, 0x22, 4, chan_id, filter_type,
342 (pid >> 8) & 0xff, pid & 0xff
345 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
346 return err;
349 static int ttusb_del_channel(struct ttusb *ttusb, int channel_id)
351 int err;
352 /* DelChannel */
353 u8 b[] = { 0xaa, ++ttusb->c, 0x23, 1, channel_id };
355 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
356 return err;
359 #ifdef TTUSB_HWSECTIONS
360 static int ttusb_set_filter(struct ttusb *ttusb, int filter_id,
361 int associated_chan, u8 filter[8], u8 mask[8])
363 int err;
364 /* SetFilter */
365 u8 b[] = { 0xaa, 0, 0x24, 0x1a, filter_id, associated_chan,
366 filter[0], filter[1], filter[2], filter[3],
367 filter[4], filter[5], filter[6], filter[7],
368 filter[8], filter[9], filter[10], filter[11],
369 mask[0], mask[1], mask[2], mask[3],
370 mask[4], mask[5], mask[6], mask[7],
371 mask[8], mask[9], mask[10], mask[11]
374 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
375 return err;
378 static int ttusb_del_filter(struct ttusb *ttusb, int filter_id)
380 int err;
381 /* DelFilter */
382 u8 b[] = { 0xaa, ++ttusb->c, 0x25, 1, filter_id };
384 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
385 return err;
387 #endif
389 static int ttusb_init_controller(struct ttusb *ttusb)
391 u8 b0[] = { 0xaa, ++ttusb->c, 0x15, 1, 0 };
392 u8 b1[] = { 0xaa, ++ttusb->c, 0x15, 1, 1 };
393 u8 b2[] = { 0xaa, ++ttusb->c, 0x32, 1, 0 };
394 /* i2c write read: 5 bytes, addr 0x10, 0x02 bytes write, 1 bytes read. */
395 u8 b3[] =
396 { 0xaa, ++ttusb->c, 0x31, 5, 0x10, 0x02, 0x01, 0x00, 0x1e };
397 u8 b4[] =
398 { 0x55, ttusb->c, 0x31, 4, 0x10, 0x02, 0x01, 0x00, 0x1e };
400 u8 get_version[] = { 0xaa, ++ttusb->c, 0x17, 5, 0, 0, 0, 0, 0 };
401 u8 get_dsp_version[0x20] =
402 { 0xaa, ++ttusb->c, 0x26, 28, 0, 0, 0, 0, 0 };
403 int err;
405 /* reset board */
406 if ((err = ttusb_cmd(ttusb, b0, sizeof(b0), 0)))
407 return err;
409 /* reset board (again?) */
410 if ((err = ttusb_cmd(ttusb, b1, sizeof(b1), 0)))
411 return err;
413 ttusb_boot_dsp(ttusb);
415 /* set i2c bit rate */
416 if ((err = ttusb_cmd(ttusb, b2, sizeof(b2), 0)))
417 return err;
419 if ((err = ttusb_cmd(ttusb, b3, sizeof(b3), 1)))
420 return err;
422 err = ttusb_result(ttusb, b4, sizeof(b4));
424 if ((err = ttusb_cmd(ttusb, get_version, sizeof(get_version), 1)))
425 return err;
427 if ((err = ttusb_result(ttusb, get_version, sizeof(get_version))))
428 return err;
430 dprintk("%s: stc-version: %c%c%c%c%c\n", __FUNCTION__,
431 get_version[4], get_version[5], get_version[6],
432 get_version[7], get_version[8]);
434 if (memcmp(get_version + 4, "V 0.0", 5) &&
435 memcmp(get_version + 4, "V 1.1", 5) &&
436 memcmp(get_version + 4, "V 2.1", 5) &&
437 memcmp(get_version + 4, "V 2.2", 5)) {
438 printk
439 ("%s: unknown STC version %c%c%c%c%c, please report!\n",
440 __FUNCTION__, get_version[4], get_version[5],
441 get_version[6], get_version[7], get_version[8]);
444 ttusb->revision = ((get_version[6] - '0') << 4) |
445 (get_version[8] - '0');
447 err =
448 ttusb_cmd(ttusb, get_dsp_version, sizeof(get_dsp_version), 1);
449 if (err)
450 return err;
452 err =
453 ttusb_result(ttusb, get_dsp_version, sizeof(get_dsp_version));
454 if (err)
455 return err;
456 printk("%s: dsp-version: %c%c%c\n", __FUNCTION__,
457 get_dsp_version[4], get_dsp_version[5], get_dsp_version[6]);
458 return 0;
461 #ifdef TTUSB_DISEQC
462 static int ttusb_send_diseqc(struct dvb_frontend* fe,
463 const struct dvb_diseqc_master_cmd *cmd)
465 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
466 u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
468 int err;
470 b[3] = 4 + 2 + cmd->msg_len;
471 b[4] = 0xFF; /* send diseqc master, not burst */
472 b[5] = cmd->msg_len;
474 memcpy(b + 5, cmd->msg, cmd->msg_len);
476 /* Diseqc */
477 if ((err = ttusb_cmd(ttusb, b, 4 + b[3], 0))) {
478 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
479 __FUNCTION__, err);
482 return err;
484 #endif
486 static int ttusb_update_lnb(struct ttusb *ttusb)
488 u8 b[] = { 0xaa, ++ttusb->c, 0x16, 5, /*power: */ 1,
489 ttusb->voltage == SEC_VOLTAGE_18 ? 0 : 1,
490 ttusb->tone == SEC_TONE_ON ? 1 : 0, 1, 1
492 int err;
494 /* SetLNB */
495 if ((err = ttusb_cmd(ttusb, b, sizeof(b), 0))) {
496 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
497 __FUNCTION__, err);
500 return err;
503 static int ttusb_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
505 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
507 ttusb->voltage = voltage;
508 return ttusb_update_lnb(ttusb);
511 #ifdef TTUSB_TONE
512 static int ttusb_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
514 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
516 ttusb->tone = tone;
517 return ttusb_update_lnb(ttusb);
519 #endif
522 #if 0
523 static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
525 u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
526 int err, actual_len;
528 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
529 if (err) {
530 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
531 __FUNCTION__, err);
534 #endif
536 /*****************************************************************************/
538 #ifdef TTUSB_HWSECTIONS
539 static void ttusb_handle_ts_data(struct ttusb_channel *channel,
540 const u8 * data, int len);
541 static void ttusb_handle_sec_data(struct ttusb_channel *channel,
542 const u8 * data, int len);
543 #endif
545 static int numpkt = 0, numts, numstuff, numsec, numinvalid;
546 static unsigned long lastj;
548 static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
549 int len)
551 u16 csum = 0, cc;
552 int i;
553 for (i = 0; i < len; i += 2)
554 csum ^= le16_to_cpup((u16 *) (muxpack + i));
555 if (csum) {
556 printk("%s: muxpack with incorrect checksum, ignoring\n",
557 __FUNCTION__);
558 numinvalid++;
559 return;
562 cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
563 cc &= 0x7FFF;
564 if ((cc != ttusb->cc) && (ttusb->cc != -1))
565 printk("%s: cc discontinuity (%d frames missing)\n",
566 __FUNCTION__, (cc - ttusb->cc) & 0x7FFF);
567 ttusb->cc = (cc + 1) & 0x7FFF;
568 if (muxpack[0] & 0x80) {
569 #ifdef TTUSB_HWSECTIONS
570 /* section data */
571 int pusi = muxpack[0] & 0x40;
572 int channel = muxpack[0] & 0x1F;
573 int payload = muxpack[1];
574 const u8 *data = muxpack + 2;
575 /* check offset flag */
576 if (muxpack[0] & 0x20)
577 data++;
579 ttusb_handle_sec_data(ttusb->channel + channel, data,
580 payload);
581 data += payload;
583 if ((!!(ttusb->muxpack[0] & 0x20)) ^
584 !!(ttusb->muxpack[1] & 1))
585 data++;
586 #warning TODO: pusi
587 printk("cc: %04x\n", (data[0] << 8) | data[1]);
588 #endif
589 numsec++;
590 } else if (muxpack[0] == 0x47) {
591 #ifdef TTUSB_HWSECTIONS
592 /* we have TS data here! */
593 int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
594 int channel;
595 for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
596 if (ttusb->channel[channel].active
597 && (pid == ttusb->channel[channel].pid))
598 ttusb_handle_ts_data(ttusb->channel +
599 channel, muxpack,
600 188);
601 #endif
602 numts++;
603 dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
604 } else if (muxpack[0] != 0) {
605 numinvalid++;
606 printk("illegal muxpack type %02x\n", muxpack[0]);
607 } else
608 numstuff++;
611 static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
613 int maxwork = 1024;
614 while (len) {
615 if (!(maxwork--)) {
616 printk("%s: too much work\n", __FUNCTION__);
617 break;
620 switch (ttusb->mux_state) {
621 case 0:
622 case 1:
623 case 2:
624 len--;
625 if (*data++ == 0xAA)
626 ++ttusb->mux_state;
627 else {
628 ttusb->mux_state = 0;
629 #if DEBUG > 3
630 if (ttusb->insync)
631 printk("%02x ", data[-1]);
632 #else
633 if (ttusb->insync) {
634 printk("%s: lost sync.\n",
635 __FUNCTION__);
636 ttusb->insync = 0;
638 #endif
640 break;
641 case 3:
642 ttusb->insync = 1;
643 len--;
644 ttusb->mux_npacks = *data++;
645 ++ttusb->mux_state;
646 ttusb->muxpack_ptr = 0;
647 /* maximum bytes, until we know the length */
648 ttusb->muxpack_len = 2;
649 break;
650 case 4:
652 int avail;
653 avail = len;
654 if (avail >
655 (ttusb->muxpack_len -
656 ttusb->muxpack_ptr))
657 avail =
658 ttusb->muxpack_len -
659 ttusb->muxpack_ptr;
660 memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
661 data, avail);
662 ttusb->muxpack_ptr += avail;
663 BUG_ON(ttusb->muxpack_ptr > 264);
664 data += avail;
665 len -= avail;
666 /* determine length */
667 if (ttusb->muxpack_ptr == 2) {
668 if (ttusb->muxpack[0] & 0x80) {
669 ttusb->muxpack_len =
670 ttusb->muxpack[1] + 2;
671 if (ttusb->
672 muxpack[0] & 0x20)
673 ttusb->
674 muxpack_len++;
675 if ((!!
676 (ttusb->
677 muxpack[0] & 0x20)) ^
678 !!(ttusb->
679 muxpack[1] & 1))
680 ttusb->
681 muxpack_len++;
682 ttusb->muxpack_len += 4;
683 } else if (ttusb->muxpack[0] ==
684 0x47)
685 ttusb->muxpack_len =
686 188 + 4;
687 else if (ttusb->muxpack[0] == 0x00)
688 ttusb->muxpack_len =
689 ttusb->muxpack[1] + 2 +
691 else {
692 dprintk
693 ("%s: invalid state: first byte is %x\n",
694 __FUNCTION__,
695 ttusb->muxpack[0]);
696 ttusb->mux_state = 0;
701 * if length is valid and we reached the end:
702 * goto next muxpack
704 if ((ttusb->muxpack_ptr >= 2) &&
705 (ttusb->muxpack_ptr ==
706 ttusb->muxpack_len)) {
707 ttusb_process_muxpack(ttusb,
708 ttusb->
709 muxpack,
710 ttusb->
711 muxpack_ptr);
712 ttusb->muxpack_ptr = 0;
713 /* maximum bytes, until we know the length */
714 ttusb->muxpack_len = 2;
717 * no muxpacks left?
718 * return to search-sync state
720 if (!ttusb->mux_npacks--) {
721 ttusb->mux_state = 0;
722 break;
725 break;
727 default:
728 BUG();
729 break;
734 static void ttusb_iso_irq(struct urb *urb)
736 struct ttusb *ttusb = urb->context;
738 if (!ttusb->iso_streaming)
739 return;
741 #if 0
742 printk("%s: status %d, errcount == %d, length == %i\n",
743 __FUNCTION__,
744 urb->status, urb->error_count, urb->actual_length);
745 #endif
747 if (!urb->status) {
748 int i;
749 for (i = 0; i < urb->number_of_packets; ++i) {
750 struct usb_iso_packet_descriptor *d;
751 u8 *data;
752 int len;
753 numpkt++;
754 if (time_after_eq(jiffies, lastj + HZ)) {
755 #if DEBUG > 2
756 printk
757 ("frames/s: %d (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
758 numpkt * HZ / (jiffies - lastj),
759 numts, numstuff, numsec, numinvalid,
760 numts + numstuff + numsec +
761 numinvalid);
762 #endif
763 numts = numstuff = numsec = numinvalid = 0;
764 lastj = jiffies;
765 numpkt = 0;
767 d = &urb->iso_frame_desc[i];
768 data = urb->transfer_buffer + d->offset;
769 len = d->actual_length;
770 d->actual_length = 0;
771 d->status = 0;
772 ttusb_process_frame(ttusb, data, len);
775 usb_submit_urb(urb, GFP_ATOMIC);
778 static void ttusb_free_iso_urbs(struct ttusb *ttusb)
780 int i;
782 for (i = 0; i < ISO_BUF_COUNT; i++)
783 if (ttusb->iso_urb[i])
784 usb_free_urb(ttusb->iso_urb[i]);
786 pci_free_consistent(NULL,
787 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF *
788 ISO_BUF_COUNT, ttusb->iso_buffer,
789 ttusb->iso_dma_handle);
792 static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
794 int i;
796 ttusb->iso_buffer = pci_alloc_consistent(NULL,
797 ISO_FRAME_SIZE *
798 FRAMES_PER_ISO_BUF *
799 ISO_BUF_COUNT,
800 &ttusb->iso_dma_handle);
802 memset(ttusb->iso_buffer, 0,
803 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF * ISO_BUF_COUNT);
805 for (i = 0; i < ISO_BUF_COUNT; i++) {
806 struct urb *urb;
808 if (!
809 (urb =
810 usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
811 ttusb_free_iso_urbs(ttusb);
812 return -ENOMEM;
815 ttusb->iso_urb[i] = urb;
818 return 0;
821 static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
823 int i;
825 for (i = 0; i < ISO_BUF_COUNT; i++)
826 usb_kill_urb(ttusb->iso_urb[i]);
828 ttusb->iso_streaming = 0;
831 static int ttusb_start_iso_xfer(struct ttusb *ttusb)
833 int i, j, err, buffer_offset = 0;
835 if (ttusb->iso_streaming) {
836 printk("%s: iso xfer already running!\n", __FUNCTION__);
837 return 0;
840 ttusb->cc = -1;
841 ttusb->insync = 0;
842 ttusb->mux_state = 0;
844 for (i = 0; i < ISO_BUF_COUNT; i++) {
845 int frame_offset = 0;
846 struct urb *urb = ttusb->iso_urb[i];
848 urb->dev = ttusb->dev;
849 urb->context = ttusb;
850 urb->complete = ttusb_iso_irq;
851 urb->pipe = ttusb->isoc_in_pipe;
852 urb->transfer_flags = URB_ISO_ASAP;
853 urb->interval = 1;
854 urb->number_of_packets = FRAMES_PER_ISO_BUF;
855 urb->transfer_buffer_length =
856 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
857 urb->transfer_buffer = ttusb->iso_buffer + buffer_offset;
858 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
860 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
861 urb->iso_frame_desc[j].offset = frame_offset;
862 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
863 frame_offset += ISO_FRAME_SIZE;
867 for (i = 0; i < ISO_BUF_COUNT; i++) {
868 if ((err = usb_submit_urb(ttusb->iso_urb[i], GFP_ATOMIC))) {
869 ttusb_stop_iso_xfer(ttusb);
870 printk
871 ("%s: failed urb submission (%i: err = %i)!\n",
872 __FUNCTION__, i, err);
873 return err;
877 ttusb->iso_streaming = 1;
879 return 0;
882 #ifdef TTUSB_HWSECTIONS
883 static void ttusb_handle_ts_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
884 int len)
886 dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
889 static void ttusb_handle_sec_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
890 int len)
892 // struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
893 #error TODO: handle ugly stuff
894 // dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
896 #endif
898 static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
900 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
901 int feed_type = 1;
903 dprintk("ttusb_start_feed\n");
905 switch (dvbdmxfeed->type) {
906 case DMX_TYPE_TS:
907 break;
908 case DMX_TYPE_SEC:
909 break;
910 default:
911 return -EINVAL;
914 if (dvbdmxfeed->type == DMX_TYPE_TS) {
915 switch (dvbdmxfeed->pes_type) {
916 case DMX_TS_PES_VIDEO:
917 case DMX_TS_PES_AUDIO:
918 case DMX_TS_PES_TELETEXT:
919 case DMX_TS_PES_PCR:
920 case DMX_TS_PES_OTHER:
921 break;
922 default:
923 return -EINVAL;
927 #ifdef TTUSB_HWSECTIONS
928 #error TODO: allocate filters
929 if (dvbdmxfeed->type == DMX_TYPE_TS) {
930 feed_type = 1;
931 } else if (dvbdmxfeed->type == DMX_TYPE_SEC) {
932 feed_type = 2;
934 #endif
936 ttusb_set_channel(ttusb, dvbdmxfeed->index, feed_type, dvbdmxfeed->pid);
938 if (0 == ttusb->running_feed_count++)
939 ttusb_start_iso_xfer(ttusb);
941 return 0;
944 static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
946 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
948 ttusb_del_channel(ttusb, dvbdmxfeed->index);
950 if (--ttusb->running_feed_count == 0)
951 ttusb_stop_iso_xfer(ttusb);
953 return 0;
956 static int ttusb_setup_interfaces(struct ttusb *ttusb)
958 usb_set_interface(ttusb->dev, 1, 1);
960 ttusb->bulk_out_pipe = usb_sndbulkpipe(ttusb->dev, 1);
961 ttusb->bulk_in_pipe = usb_rcvbulkpipe(ttusb->dev, 1);
962 ttusb->isoc_in_pipe = usb_rcvisocpipe(ttusb->dev, 2);
964 return 0;
967 #if 0
968 static u8 stc_firmware[8192];
970 static int stc_open(struct inode *inode, struct file *file)
972 struct ttusb *ttusb = file->private_data;
973 int addr;
975 for (addr = 0; addr < 8192; addr += 16) {
976 u8 snd_buf[2] = { addr >> 8, addr & 0xFF };
977 ttusb_i2c_msg(ttusb, 0x50, snd_buf, 2, stc_firmware + addr,
978 16);
981 return 0;
984 static ssize_t stc_read(struct file *file, char *buf, size_t count,
985 loff_t * offset)
987 int tc = count;
989 if ((tc + *offset) > 8192)
990 tc = 8192 - *offset;
992 if (tc < 0)
993 return 0;
995 if (copy_to_user(buf, stc_firmware + *offset, tc))
996 return -EFAULT;
998 *offset += tc;
1000 return tc;
1003 static int stc_release(struct inode *inode, struct file *file)
1005 return 0;
1008 static struct file_operations stc_fops = {
1009 .owner = THIS_MODULE,
1010 .read = stc_read,
1011 .open = stc_open,
1012 .release = stc_release,
1014 #endif
1016 static u32 functionality(struct i2c_adapter *adapter)
1018 return I2C_FUNC_I2C;
1023 static int alps_tdmb7_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1025 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1026 u8 data[4];
1027 struct i2c_msg msg = {.addr=0x61, .flags=0, .buf=data, .len=sizeof(data) };
1028 u32 div;
1030 div = (params->frequency + 36166667) / 166667;
1032 data[0] = (div >> 8) & 0x7f;
1033 data[1] = div & 0xff;
1034 data[2] = ((div >> 10) & 0x60) | 0x85;
1035 data[3] = params->frequency < 592000000 ? 0x40 : 0x80;
1037 if (fe->ops.i2c_gate_ctrl)
1038 fe->ops.i2c_gate_ctrl(fe, 1);
1039 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1) return -EIO;
1040 return 0;
1043 static struct cx22700_config alps_tdmb7_config = {
1044 .demod_address = 0x43,
1051 static int philips_tdm1316l_tuner_init(struct dvb_frontend* fe)
1053 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1054 static u8 td1316_init[] = { 0x0b, 0xf5, 0x85, 0xab };
1055 static u8 disable_mc44BC374c[] = { 0x1d, 0x74, 0xa0, 0x68 };
1056 struct i2c_msg tuner_msg = { .addr=0x60, .flags=0, .buf=td1316_init, .len=sizeof(td1316_init) };
1058 // setup PLL configuration
1059 if (fe->ops.i2c_gate_ctrl)
1060 fe->ops.i2c_gate_ctrl(fe, 1);
1061 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) return -EIO;
1062 msleep(1);
1064 // disable the mc44BC374c (do not check for errors)
1065 tuner_msg.addr = 0x65;
1066 tuner_msg.buf = disable_mc44BC374c;
1067 tuner_msg.len = sizeof(disable_mc44BC374c);
1068 if (fe->ops.i2c_gate_ctrl)
1069 fe->ops.i2c_gate_ctrl(fe, 1);
1070 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1071 i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1);
1074 return 0;
1077 static int philips_tdm1316l_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1079 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1080 u8 tuner_buf[4];
1081 struct i2c_msg tuner_msg = {.addr=0x60, .flags=0, .buf=tuner_buf, .len=sizeof(tuner_buf) };
1082 int tuner_frequency = 0;
1083 u8 band, cp, filter;
1085 // determine charge pump
1086 tuner_frequency = params->frequency + 36130000;
1087 if (tuner_frequency < 87000000) return -EINVAL;
1088 else if (tuner_frequency < 130000000) cp = 3;
1089 else if (tuner_frequency < 160000000) cp = 5;
1090 else if (tuner_frequency < 200000000) cp = 6;
1091 else if (tuner_frequency < 290000000) cp = 3;
1092 else if (tuner_frequency < 420000000) cp = 5;
1093 else if (tuner_frequency < 480000000) cp = 6;
1094 else if (tuner_frequency < 620000000) cp = 3;
1095 else if (tuner_frequency < 830000000) cp = 5;
1096 else if (tuner_frequency < 895000000) cp = 7;
1097 else return -EINVAL;
1099 // determine band
1100 if (params->frequency < 49000000) return -EINVAL;
1101 else if (params->frequency < 159000000) band = 1;
1102 else if (params->frequency < 444000000) band = 2;
1103 else if (params->frequency < 861000000) band = 4;
1104 else return -EINVAL;
1106 // setup PLL filter
1107 switch (params->u.ofdm.bandwidth) {
1108 case BANDWIDTH_6_MHZ:
1109 tda1004x_writereg(fe, 0x0C, 0);
1110 filter = 0;
1111 break;
1113 case BANDWIDTH_7_MHZ:
1114 tda1004x_writereg(fe, 0x0C, 0);
1115 filter = 0;
1116 break;
1118 case BANDWIDTH_8_MHZ:
1119 tda1004x_writereg(fe, 0x0C, 0xFF);
1120 filter = 1;
1121 break;
1123 default:
1124 return -EINVAL;
1127 // calculate divisor
1128 // ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
1129 tuner_frequency = (((params->frequency / 1000) * 6) + 217280) / 1000;
1131 // setup tuner buffer
1132 tuner_buf[0] = tuner_frequency >> 8;
1133 tuner_buf[1] = tuner_frequency & 0xff;
1134 tuner_buf[2] = 0xca;
1135 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1137 if (fe->ops.i2c_gate_ctrl)
1138 fe->ops.i2c_gate_ctrl(fe, 1);
1139 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1)
1140 return -EIO;
1142 msleep(1);
1143 return 0;
1146 static int philips_tdm1316l_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1148 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1150 return request_firmware(fw, name, &ttusb->dev->dev);
1153 static struct tda1004x_config philips_tdm1316l_config = {
1155 .demod_address = 0x8,
1156 .invert = 1,
1157 .invert_oclk = 0,
1158 .request_firmware = philips_tdm1316l_request_firmware,
1161 static u8 alps_bsbe1_inittab[] = {
1162 0x01, 0x15,
1163 0x02, 0x30,
1164 0x03, 0x00,
1165 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1166 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1167 0x06, 0x40, /* DAC not used, set to high impendance mode */
1168 0x07, 0x00, /* DAC LSB */
1169 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1170 0x09, 0x00, /* FIFO */
1171 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1172 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1173 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1174 0x10, 0x3f, // AGC2 0x3d
1175 0x11, 0x84,
1176 0x12, 0xb9,
1177 0x15, 0xc9, // lock detector threshold
1178 0x16, 0x00,
1179 0x17, 0x00,
1180 0x18, 0x00,
1181 0x19, 0x00,
1182 0x1a, 0x00,
1183 0x1f, 0x50,
1184 0x20, 0x00,
1185 0x21, 0x00,
1186 0x22, 0x00,
1187 0x23, 0x00,
1188 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1189 0x29, 0x1e, // 1/2 threshold
1190 0x2a, 0x14, // 2/3 threshold
1191 0x2b, 0x0f, // 3/4 threshold
1192 0x2c, 0x09, // 5/6 threshold
1193 0x2d, 0x05, // 7/8 threshold
1194 0x2e, 0x01,
1195 0x31, 0x1f, // test all FECs
1196 0x32, 0x19, // viterbi and synchro search
1197 0x33, 0xfc, // rs control
1198 0x34, 0x93, // error control
1199 0x0f, 0x92,
1200 0xff, 0xff
1203 static u8 alps_bsru6_inittab[] = {
1204 0x01, 0x15,
1205 0x02, 0x30,
1206 0x03, 0x00,
1207 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1208 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1209 0x06, 0x40, /* DAC not used, set to high impendance mode */
1210 0x07, 0x00, /* DAC LSB */
1211 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1212 0x09, 0x00, /* FIFO */
1213 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1214 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1215 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1216 0x10, 0x3f, // AGC2 0x3d
1217 0x11, 0x84,
1218 0x12, 0xb9,
1219 0x15, 0xc9, // lock detector threshold
1220 0x16, 0x00,
1221 0x17, 0x00,
1222 0x18, 0x00,
1223 0x19, 0x00,
1224 0x1a, 0x00,
1225 0x1f, 0x50,
1226 0x20, 0x00,
1227 0x21, 0x00,
1228 0x22, 0x00,
1229 0x23, 0x00,
1230 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1231 0x29, 0x1e, // 1/2 threshold
1232 0x2a, 0x14, // 2/3 threshold
1233 0x2b, 0x0f, // 3/4 threshold
1234 0x2c, 0x09, // 5/6 threshold
1235 0x2d, 0x05, // 7/8 threshold
1236 0x2e, 0x01,
1237 0x31, 0x1f, // test all FECs
1238 0x32, 0x19, // viterbi and synchro search
1239 0x33, 0xfc, // rs control
1240 0x34, 0x93, // error control
1241 0x0f, 0x52,
1242 0xff, 0xff
1245 static int alps_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
1247 u8 aclk = 0;
1248 u8 bclk = 0;
1250 if (srate < 1500000) {
1251 aclk = 0xb7;
1252 bclk = 0x47;
1253 } else if (srate < 3000000) {
1254 aclk = 0xb7;
1255 bclk = 0x4b;
1256 } else if (srate < 7000000) {
1257 aclk = 0xb7;
1258 bclk = 0x4f;
1259 } else if (srate < 14000000) {
1260 aclk = 0xb7;
1261 bclk = 0x53;
1262 } else if (srate < 30000000) {
1263 aclk = 0xb6;
1264 bclk = 0x53;
1265 } else if (srate < 45000000) {
1266 aclk = 0xb4;
1267 bclk = 0x51;
1270 stv0299_writereg(fe, 0x13, aclk);
1271 stv0299_writereg(fe, 0x14, bclk);
1272 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
1273 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
1274 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
1276 return 0;
1279 static int philips_tsa5059_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1281 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1282 u8 buf[4];
1283 u32 div;
1284 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1286 if ((params->frequency < 950000) || (params->frequency > 2150000))
1287 return -EINVAL;
1289 div = (params->frequency + (125 - 1)) / 125; // round correctly
1290 buf[0] = (div >> 8) & 0x7f;
1291 buf[1] = div & 0xff;
1292 buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
1293 buf[3] = 0xC4;
1295 if (params->frequency > 1530000)
1296 buf[3] = 0xC0;
1298 /* BSBE1 wants XCE bit set */
1299 if (ttusb->revision == TTUSB_REV_2_2)
1300 buf[3] |= 0x20;
1302 if (fe->ops.i2c_gate_ctrl)
1303 fe->ops.i2c_gate_ctrl(fe, 1);
1304 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1305 return -EIO;
1307 return 0;
1310 static struct stv0299_config alps_stv0299_config = {
1311 .demod_address = 0x68,
1312 .inittab = alps_bsru6_inittab,
1313 .mclk = 88000000UL,
1314 .invert = 1,
1315 .skip_reinit = 0,
1316 .lock_output = STV0229_LOCKOUTPUT_1,
1317 .volt13_op0_op1 = STV0299_VOLT13_OP1,
1318 .min_delay_ms = 100,
1319 .set_symbol_rate = alps_stv0299_set_symbol_rate,
1322 static int ttusb_novas_grundig_29504_491_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1324 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1325 u8 buf[4];
1326 u32 div;
1327 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1329 div = params->frequency / 125;
1331 buf[0] = (div >> 8) & 0x7f;
1332 buf[1] = div & 0xff;
1333 buf[2] = 0x8e;
1334 buf[3] = 0x00;
1336 if (fe->ops.i2c_gate_ctrl)
1337 fe->ops.i2c_gate_ctrl(fe, 1);
1338 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1339 return -EIO;
1341 return 0;
1344 static struct tda8083_config ttusb_novas_grundig_29504_491_config = {
1346 .demod_address = 0x68,
1349 static int alps_tdbe2_tuner_set_params(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1351 struct ttusb* ttusb = fe->dvb->priv;
1352 u32 div;
1353 u8 data[4];
1354 struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
1356 div = (params->frequency + 35937500 + 31250) / 62500;
1358 data[0] = (div >> 8) & 0x7f;
1359 data[1] = div & 0xff;
1360 data[2] = 0x85 | ((div >> 10) & 0x60);
1361 data[3] = (params->frequency < 174000000 ? 0x88 : params->frequency < 470000000 ? 0x84 : 0x81);
1363 if (fe->ops.i2c_gate_ctrl)
1364 fe->ops.i2c_gate_ctrl(fe, 1);
1365 if (i2c_transfer (&ttusb->i2c_adap, &msg, 1) != 1)
1366 return -EIO;
1368 return 0;
1372 static struct ves1820_config alps_tdbe2_config = {
1373 .demod_address = 0x09,
1374 .xin = 57840000UL,
1375 .invert = 1,
1376 .selagc = VES1820_SELAGC_SIGNAMPERR,
1379 static u8 read_pwm(struct ttusb* ttusb)
1381 u8 b = 0xff;
1382 u8 pwm;
1383 struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
1384 { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
1386 if ((i2c_transfer(&ttusb->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
1387 pwm = 0x48;
1389 return pwm;
1393 static int dvbc_philips_tdm1316l_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1395 struct ttusb *ttusb = (struct ttusb *) fe->dvb->priv;
1396 u8 tuner_buf[5];
1397 struct i2c_msg tuner_msg = {.addr = 0x60,
1398 .flags = 0,
1399 .buf = tuner_buf,
1400 .len = sizeof(tuner_buf) };
1401 int tuner_frequency = 0;
1402 u8 band, cp, filter;
1404 // determine charge pump
1405 tuner_frequency = params->frequency;
1406 if (tuner_frequency < 87000000) {return -EINVAL;}
1407 else if (tuner_frequency < 130000000) {cp = 3; band = 1;}
1408 else if (tuner_frequency < 160000000) {cp = 5; band = 1;}
1409 else if (tuner_frequency < 200000000) {cp = 6; band = 1;}
1410 else if (tuner_frequency < 290000000) {cp = 3; band = 2;}
1411 else if (tuner_frequency < 420000000) {cp = 5; band = 2;}
1412 else if (tuner_frequency < 480000000) {cp = 6; band = 2;}
1413 else if (tuner_frequency < 620000000) {cp = 3; band = 4;}
1414 else if (tuner_frequency < 830000000) {cp = 5; band = 4;}
1415 else if (tuner_frequency < 895000000) {cp = 7; band = 4;}
1416 else {return -EINVAL;}
1418 // assume PLL filter should always be 8MHz for the moment.
1419 filter = 1;
1421 // calculate divisor
1422 // (Finput + Fif)/Fref; Fif = 36125000 Hz, Fref = 62500 Hz
1423 tuner_frequency = ((params->frequency + 36125000) / 62500);
1425 // setup tuner buffer
1426 tuner_buf[0] = tuner_frequency >> 8;
1427 tuner_buf[1] = tuner_frequency & 0xff;
1428 tuner_buf[2] = 0xc8;
1429 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1430 tuner_buf[4] = 0x80;
1432 if (fe->ops.i2c_gate_ctrl)
1433 fe->ops.i2c_gate_ctrl(fe, 1);
1434 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1435 printk("dvb-ttusb-budget: dvbc_philips_tdm1316l_pll_set Error 1\n");
1436 return -EIO;
1439 msleep(50);
1441 if (fe->ops.i2c_gate_ctrl)
1442 fe->ops.i2c_gate_ctrl(fe, 1);
1443 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1444 printk("dvb-ttusb-budget: dvbc_philips_tdm1316l_pll_set Error 2\n");
1445 return -EIO;
1448 msleep(1);
1450 return 0;
1453 static u8 dvbc_philips_tdm1316l_inittab[] = {
1454 0x80, 0x21,
1455 0x80, 0x20,
1456 0x81, 0x01,
1457 0x81, 0x00,
1458 0x00, 0x09,
1459 0x01, 0x69,
1460 0x03, 0x00,
1461 0x04, 0x00,
1462 0x07, 0x00,
1463 0x08, 0x00,
1464 0x20, 0x00,
1465 0x21, 0x40,
1466 0x22, 0x00,
1467 0x23, 0x00,
1468 0x24, 0x40,
1469 0x25, 0x88,
1470 0x30, 0xff,
1471 0x31, 0x00,
1472 0x32, 0xff,
1473 0x33, 0x00,
1474 0x34, 0x50,
1475 0x35, 0x7f,
1476 0x36, 0x00,
1477 0x37, 0x20,
1478 0x38, 0x00,
1479 0x40, 0x1c,
1480 0x41, 0xff,
1481 0x42, 0x29,
1482 0x43, 0x20,
1483 0x44, 0xff,
1484 0x45, 0x00,
1485 0x46, 0x00,
1486 0x49, 0x04,
1487 0x4a, 0xff,
1488 0x4b, 0x7f,
1489 0x52, 0x30,
1490 0x55, 0xae,
1491 0x56, 0x47,
1492 0x57, 0xe1,
1493 0x58, 0x3a,
1494 0x5a, 0x1e,
1495 0x5b, 0x34,
1496 0x60, 0x00,
1497 0x63, 0x00,
1498 0x64, 0x00,
1499 0x65, 0x00,
1500 0x66, 0x00,
1501 0x67, 0x00,
1502 0x68, 0x00,
1503 0x69, 0x00,
1504 0x6a, 0x02,
1505 0x6b, 0x00,
1506 0x70, 0xff,
1507 0x71, 0x00,
1508 0x72, 0x00,
1509 0x73, 0x00,
1510 0x74, 0x0c,
1511 0x80, 0x00,
1512 0x81, 0x00,
1513 0x82, 0x00,
1514 0x83, 0x00,
1515 0x84, 0x04,
1516 0x85, 0x80,
1517 0x86, 0x24,
1518 0x87, 0x78,
1519 0x88, 0x00,
1520 0x89, 0x00,
1521 0x90, 0x01,
1522 0x91, 0x01,
1523 0xa0, 0x00,
1524 0xa1, 0x00,
1525 0xa2, 0x00,
1526 0xb0, 0x91,
1527 0xb1, 0x0b,
1528 0xc0, 0x4b,
1529 0xc1, 0x00,
1530 0xc2, 0x00,
1531 0xd0, 0x00,
1532 0xd1, 0x00,
1533 0xd2, 0x00,
1534 0xd3, 0x00,
1535 0xd4, 0x00,
1536 0xd5, 0x00,
1537 0xde, 0x00,
1538 0xdf, 0x00,
1539 0x61, 0x38,
1540 0x62, 0x0a,
1541 0x53, 0x13,
1542 0x59, 0x08,
1543 0x55, 0x00,
1544 0x56, 0x40,
1545 0x57, 0x08,
1546 0x58, 0x3d,
1547 0x88, 0x10,
1548 0xa0, 0x00,
1549 0xa0, 0x00,
1550 0xa0, 0x00,
1551 0xa0, 0x04,
1552 0xff, 0xff,
1555 static struct stv0297_config dvbc_philips_tdm1316l_config = {
1556 .demod_address = 0x1c,
1557 .inittab = dvbc_philips_tdm1316l_inittab,
1558 .invert = 0,
1561 static void frontend_init(struct ttusb* ttusb)
1563 switch(le16_to_cpu(ttusb->dev->descriptor.idProduct)) {
1564 case 0x1003: // Hauppauge/TT Nova-USB-S budget (stv0299/ALPS BSRU6|BSBE1(tsa5059))
1565 // try the stv0299 based first
1566 ttusb->fe = dvb_attach(stv0299_attach, &alps_stv0299_config, &ttusb->i2c_adap);
1567 if (ttusb->fe != NULL) {
1568 ttusb->fe->ops.tuner_ops.set_params = philips_tsa5059_tuner_set_params;
1570 if(ttusb->revision == TTUSB_REV_2_2) { // ALPS BSBE1
1571 alps_stv0299_config.inittab = alps_bsbe1_inittab;
1572 dvb_attach(lnbp21_attach, ttusb->fe, &ttusb->i2c_adap, 0, 0);
1573 } else { // ALPS BSRU6
1574 ttusb->fe->ops.set_voltage = ttusb_set_voltage;
1576 break;
1579 // Grundig 29504-491
1580 ttusb->fe = dvb_attach(tda8083_attach, &ttusb_novas_grundig_29504_491_config, &ttusb->i2c_adap);
1581 if (ttusb->fe != NULL) {
1582 ttusb->fe->ops.tuner_ops.set_params = ttusb_novas_grundig_29504_491_tuner_set_params;
1583 ttusb->fe->ops.set_voltage = ttusb_set_voltage;
1584 break;
1586 break;
1588 case 0x1004: // Hauppauge/TT DVB-C budget (ves1820/ALPS TDBE2(sp5659))
1589 ttusb->fe = dvb_attach(ves1820_attach, &alps_tdbe2_config, &ttusb->i2c_adap, read_pwm(ttusb));
1590 if (ttusb->fe != NULL) {
1591 ttusb->fe->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
1592 break;
1595 ttusb->fe = dvb_attach(stv0297_attach, &dvbc_philips_tdm1316l_config, &ttusb->i2c_adap);
1596 if (ttusb->fe != NULL) {
1597 ttusb->fe->ops.tuner_ops.set_params = dvbc_philips_tdm1316l_tuner_set_params;
1598 break;
1600 break;
1602 case 0x1005: // Hauppauge/TT Nova-USB-t budget (tda10046/Philips td1316(tda6651tt) OR cx22700/ALPS TDMB7(??))
1603 // try the ALPS TDMB7 first
1604 ttusb->fe = dvb_attach(cx22700_attach, &alps_tdmb7_config, &ttusb->i2c_adap);
1605 if (ttusb->fe != NULL) {
1606 ttusb->fe->ops.tuner_ops.set_params = alps_tdmb7_tuner_set_params;
1607 break;
1610 // Philips td1316
1611 ttusb->fe = dvb_attach(tda10046_attach, &philips_tdm1316l_config, &ttusb->i2c_adap);
1612 if (ttusb->fe != NULL) {
1613 ttusb->fe->ops.tuner_ops.init = philips_tdm1316l_tuner_init;
1614 ttusb->fe->ops.tuner_ops.set_params = philips_tdm1316l_tuner_set_params;
1615 break;
1617 break;
1620 if (ttusb->fe == NULL) {
1621 printk("dvb-ttusb-budget: A frontend driver was not found for device %04x/%04x\n",
1622 le16_to_cpu(ttusb->dev->descriptor.idVendor),
1623 le16_to_cpu(ttusb->dev->descriptor.idProduct));
1624 } else {
1625 if (dvb_register_frontend(&ttusb->adapter, ttusb->fe)) {
1626 printk("dvb-ttusb-budget: Frontend registration failed!\n");
1627 dvb_frontend_detach(ttusb->fe);
1628 ttusb->fe = NULL;
1635 static struct i2c_algorithm ttusb_dec_algo = {
1636 .master_xfer = master_xfer,
1637 .functionality = functionality,
1640 static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1642 struct usb_device *udev;
1643 struct ttusb *ttusb;
1644 int result;
1646 dprintk("%s: TTUSB DVB connected\n", __FUNCTION__);
1648 udev = interface_to_usbdev(intf);
1650 if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
1652 if (!(ttusb = kzalloc(sizeof(struct ttusb), GFP_KERNEL)))
1653 return -ENOMEM;
1655 ttusb->dev = udev;
1656 ttusb->c = 0;
1657 ttusb->mux_state = 0;
1658 mutex_init(&ttusb->semi2c);
1660 mutex_lock(&ttusb->semi2c);
1662 mutex_init(&ttusb->semusb);
1664 ttusb_setup_interfaces(ttusb);
1666 ttusb_alloc_iso_urbs(ttusb);
1667 if (ttusb_init_controller(ttusb))
1668 printk("ttusb_init_controller: error\n");
1670 mutex_unlock(&ttusb->semi2c);
1672 if ((result = dvb_register_adapter(&ttusb->adapter, "Technotrend/Hauppauge Nova-USB", THIS_MODULE, &udev->dev)) < 0) {
1673 ttusb_free_iso_urbs(ttusb);
1674 kfree(ttusb);
1675 return result;
1677 ttusb->adapter.priv = ttusb;
1679 /* i2c */
1680 memset(&ttusb->i2c_adap, 0, sizeof(struct i2c_adapter));
1681 strcpy(ttusb->i2c_adap.name, "TTUSB DEC");
1683 i2c_set_adapdata(&ttusb->i2c_adap, ttusb);
1685 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
1686 ttusb->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL;
1687 #else
1688 ttusb->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
1689 #endif
1690 ttusb->i2c_adap.algo = &ttusb_dec_algo;
1691 ttusb->i2c_adap.algo_data = NULL;
1692 ttusb->i2c_adap.dev.parent = &udev->dev;
1694 result = i2c_add_adapter(&ttusb->i2c_adap);
1695 if (result) {
1696 dvb_unregister_adapter (&ttusb->adapter);
1697 return result;
1700 memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1702 ttusb->dvb_demux.dmx.capabilities =
1703 DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1704 ttusb->dvb_demux.priv = NULL;
1705 #ifdef TTUSB_HWSECTIONS
1706 ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1707 #else
1708 ttusb->dvb_demux.filternum = 32;
1709 #endif
1710 ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1711 ttusb->dvb_demux.start_feed = ttusb_start_feed;
1712 ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1713 ttusb->dvb_demux.write_to_decoder = NULL;
1715 if ((result = dvb_dmx_init(&ttusb->dvb_demux)) < 0) {
1716 printk("ttusb_dvb: dvb_dmx_init failed (errno = %d)\n", result);
1717 i2c_del_adapter(&ttusb->i2c_adap);
1718 dvb_unregister_adapter (&ttusb->adapter);
1719 return -ENODEV;
1721 //FIXME dmxdev (nur WAS?)
1722 ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1723 ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1724 ttusb->dmxdev.capabilities = 0;
1726 if ((result = dvb_dmxdev_init(&ttusb->dmxdev, &ttusb->adapter)) < 0) {
1727 printk("ttusb_dvb: dvb_dmxdev_init failed (errno = %d)\n",
1728 result);
1729 dvb_dmx_release(&ttusb->dvb_demux);
1730 i2c_del_adapter(&ttusb->i2c_adap);
1731 dvb_unregister_adapter (&ttusb->adapter);
1732 return -ENODEV;
1735 if (dvb_net_init(&ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
1736 printk("ttusb_dvb: dvb_net_init failed!\n");
1737 dvb_dmxdev_release(&ttusb->dmxdev);
1738 dvb_dmx_release(&ttusb->dvb_demux);
1739 i2c_del_adapter(&ttusb->i2c_adap);
1740 dvb_unregister_adapter (&ttusb->adapter);
1741 return -ENODEV;
1744 usb_set_intfdata(intf, (void *) ttusb);
1746 frontend_init(ttusb);
1748 return 0;
1751 static void ttusb_disconnect(struct usb_interface *intf)
1753 struct ttusb *ttusb = usb_get_intfdata(intf);
1755 usb_set_intfdata(intf, NULL);
1757 ttusb->disconnecting = 1;
1759 ttusb_stop_iso_xfer(ttusb);
1761 ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1762 dvb_net_release(&ttusb->dvbnet);
1763 dvb_dmxdev_release(&ttusb->dmxdev);
1764 dvb_dmx_release(&ttusb->dvb_demux);
1765 if (ttusb->fe != NULL) {
1766 dvb_unregister_frontend(ttusb->fe);
1767 dvb_frontend_detach(ttusb->fe);
1769 i2c_del_adapter(&ttusb->i2c_adap);
1770 dvb_unregister_adapter(&ttusb->adapter);
1772 ttusb_free_iso_urbs(ttusb);
1774 kfree(ttusb);
1776 dprintk("%s: TTUSB DVB disconnected\n", __FUNCTION__);
1779 static struct usb_device_id ttusb_table[] = {
1780 {USB_DEVICE(0xb48, 0x1003)},
1781 {USB_DEVICE(0xb48, 0x1004)},
1782 {USB_DEVICE(0xb48, 0x1005)},
1786 MODULE_DEVICE_TABLE(usb, ttusb_table);
1788 static struct usb_driver ttusb_driver = {
1789 .name = "ttusb",
1790 .probe = ttusb_probe,
1791 .disconnect = ttusb_disconnect,
1792 .id_table = ttusb_table,
1795 static int __init ttusb_init(void)
1797 int err;
1799 if ((err = usb_register(&ttusb_driver)) < 0) {
1800 printk("%s: usb_register failed! Error number %d",
1801 __FILE__, err);
1802 return err;
1805 return 0;
1808 static void __exit ttusb_exit(void)
1810 usb_deregister(&ttusb_driver);
1813 module_init(ttusb_init);
1814 module_exit(ttusb_exit);
1816 MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1817 MODULE_DESCRIPTION("TTUSB DVB Driver");
1818 MODULE_LICENSE("GPL");