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/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>
23 #include "dvb_frontend.h"
25 #include "dvb_demux.h"
32 #include <linux/dvb/frontend.h>
33 #include <linux/dvb/dmx.h>
34 #include <linux/pci.h>
39 the DSP supports filtering in hardware, however, since the "muxstream"
40 is a bit braindead (no matching channel masks or no matching filter mask),
41 we won't support this - yet. it doesn't event support negative filters,
42 so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
43 parse TS data. USB bandwith will be a problem when having large
44 datastreams, especially for dvb-net, but hey, that's not my problem.
46 TTUSB_DISEQC, TTUSB_TONE:
47 let the STC do the diseqc/tone stuff. this isn't supported at least with
48 my TTUSB, so let it undef'd unless you want to implement another
49 frontend. never tested.
52 define it to > 3 for really hardcore debugging. you probably don't want
53 this unless the device doesn't load at all. > 2 for bandwidth statistics.
58 module_param(debug, int, 0644);
59 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
61 #define dprintk(x...) do { if (debug) printk(KERN_DEBUG x); } while (0)
63 #define ISO_BUF_COUNT 4
64 #define FRAMES_PER_ISO_BUF 4
65 #define ISO_FRAME_SIZE 912
66 #define TTUSB_MAXCHANNEL 32
67 #ifdef TTUSB_HWSECTIONS
68 #define TTUSB_MAXFILTER 16 /* ??? */
71 #define TTUSB_REV_2_2 0x22
72 #define TTUSB_BUDGET_NAME "ttusb_stc_fw"
75 * since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
76 * the dvb_demux field must be the first in struct!!
79 struct dvb_demux dvb_demux;
81 struct dvb_net dvbnet;
83 /* and one for USB access. */
84 struct semaphore semi2c;
85 struct semaphore semusb;
87 struct dvb_adapter adapter;
88 struct usb_device *dev;
90 struct i2c_adapter i2c_adap;
95 unsigned int bulk_out_pipe;
96 unsigned int bulk_in_pipe;
97 unsigned int isoc_in_pipe;
100 dma_addr_t iso_dma_handle;
102 struct urb *iso_urb[ISO_BUF_COUNT];
104 int running_feed_count;
108 u8 c; /* transaction counter, wraps around... */
109 fe_sec_tone_mode_t tone;
110 fe_sec_voltage_t voltage;
112 int mux_state; // 0..2 - MuxSyncWord, 3 - nMuxPacks, 4 - muxpack
115 int muxpack_ptr, muxpack_len;
119 int cc; /* MuxCounter - will increment on EVERY MUX PACKET */
120 /* (including stuffing. yes. really.) */
127 devfs_handle_t stc_devfs_handle;
130 struct dvb_frontend* fe;
133 /* ugly workaround ... don't know why it's neccessary to read */
134 /* all result codes. */
137 static int ttusb_cmd(struct ttusb *ttusb,
138 const u8 * data, int len, int needresult)
146 for (i = 0; i < len; ++i)
147 printk(" %02x", data[i]);
151 if (down_interruptible(&ttusb->semusb) < 0)
154 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_out_pipe,
155 (u8 *) data, len, &actual_len, 1000);
157 dprintk("%s: usb_bulk_msg(send) failed, err == %i!\n",
162 if (actual_len != len) {
163 dprintk("%s: only wrote %d of %d bytes\n", __FUNCTION__,
169 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_in_pipe,
170 ttusb->last_result, 32, &actual_len, 1000);
173 printk("%s: failed, receive error %d\n", __FUNCTION__,
179 actual_len = ttusb->last_result[3] + 4;
181 for (i = 0; i < actual_len; ++i)
182 printk(" %02x", ttusb->last_result[i]);
190 static int ttusb_result(struct ttusb *ttusb, u8 * data, int len)
192 memcpy(data, ttusb->last_result, len);
197 static int ttusb_i2c_msg(struct ttusb *ttusb,
198 u8 addr, u8 * snd_buf, u8 snd_len, u8 * rcv_buf,
205 if (snd_len > 0x28 - 7 || rcv_len > 0x20 - 7)
216 for (i = 0; i < snd_len; i++)
217 b[7 + i] = snd_buf[i];
219 err = ttusb_cmd(ttusb, b, snd_len + 7, 1);
224 err = ttusb_result(ttusb, b, 0x20);
226 /* check if the i2c transaction was successful */
227 if ((snd_len != b[5]) || (rcv_len != b[6])) return -EREMOTEIO;
231 if (err || b[0] != 0x55 || b[1] != id) {
233 ("%s: usb_bulk_msg(recv) failed, err == %i, id == %02x, b == ",
234 __FUNCTION__, err, id);
238 for (i = 0; i < rcv_len; i++)
239 rcv_buf[i] = b[7 + i];
245 static int master_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
247 struct ttusb *ttusb = i2c_get_adapdata(adapter);
251 if (down_interruptible(&ttusb->semi2c) < 0)
255 u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
258 if (num > i + 1 && (msg[i + 1].flags & I2C_M_RD)) {
260 snd_buf = msg[i].buf;
261 snd_len = msg[i].len;
262 rcv_buf = msg[i + 1].buf;
263 rcv_len = msg[i + 1].len;
267 snd_buf = msg[i].buf;
268 snd_len = msg[i].len;
274 err = ttusb_i2c_msg(ttusb, addr,
275 snd_buf, snd_len, rcv_buf, rcv_len);
278 dprintk("%s: i == %i\n", __FUNCTION__, i);
289 #include "dvb-ttusb-dspbootcode.h"
291 static int ttusb_boot_dsp(struct ttusb *ttusb)
301 /* upload dsp code in 32 byte steps (36 didn't work for me ...) */
302 /* 32 is max packet size, no messages should be splitted. */
303 for (i = 0; i < sizeof(dsp_bootcode); i += 28) {
304 memcpy(&b[4], &dsp_bootcode[i], 28);
308 err = ttusb_cmd(ttusb, b, 32, 0);
318 err = ttusb_cmd(ttusb, b, 4, 0);
327 err = ttusb_cmd(ttusb, b, 4, 0);
331 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
338 static int ttusb_set_channel(struct ttusb *ttusb, int chan_id, int filter_type,
343 u8 b[] = { 0xaa, ++ttusb->c, 0x22, 4, chan_id, filter_type,
344 (pid >> 8) & 0xff, pid & 0xff
347 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
351 static int ttusb_del_channel(struct ttusb *ttusb, int channel_id)
355 u8 b[] = { 0xaa, ++ttusb->c, 0x23, 1, channel_id };
357 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
361 #ifdef TTUSB_HWSECTIONS
362 static int ttusb_set_filter(struct ttusb *ttusb, int filter_id,
363 int associated_chan, u8 filter[8], u8 mask[8])
367 u8 b[] = { 0xaa, 0, 0x24, 0x1a, filter_id, associated_chan,
368 filter[0], filter[1], filter[2], filter[3],
369 filter[4], filter[5], filter[6], filter[7],
370 filter[8], filter[9], filter[10], filter[11],
371 mask[0], mask[1], mask[2], mask[3],
372 mask[4], mask[5], mask[6], mask[7],
373 mask[8], mask[9], mask[10], mask[11]
376 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
380 static int ttusb_del_filter(struct ttusb *ttusb, int filter_id)
384 u8 b[] = { 0xaa, ++ttusb->c, 0x25, 1, filter_id };
386 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
391 static int ttusb_init_controller(struct ttusb *ttusb)
393 u8 b0[] = { 0xaa, ++ttusb->c, 0x15, 1, 0 };
394 u8 b1[] = { 0xaa, ++ttusb->c, 0x15, 1, 1 };
395 u8 b2[] = { 0xaa, ++ttusb->c, 0x32, 1, 0 };
396 /* i2c write read: 5 bytes, addr 0x10, 0x02 bytes write, 1 bytes read. */
398 { 0xaa, ++ttusb->c, 0x31, 5, 0x10, 0x02, 0x01, 0x00, 0x1e };
400 { 0x55, ttusb->c, 0x31, 4, 0x10, 0x02, 0x01, 0x00, 0x1e };
402 u8 get_version[] = { 0xaa, ++ttusb->c, 0x17, 5, 0, 0, 0, 0, 0 };
403 u8 get_dsp_version[0x20] =
404 { 0xaa, ++ttusb->c, 0x26, 28, 0, 0, 0, 0, 0 };
408 if ((err = ttusb_cmd(ttusb, b0, sizeof(b0), 0)))
411 /* reset board (again?) */
412 if ((err = ttusb_cmd(ttusb, b1, sizeof(b1), 0)))
415 ttusb_boot_dsp(ttusb);
417 /* set i2c bit rate */
418 if ((err = ttusb_cmd(ttusb, b2, sizeof(b2), 0)))
421 if ((err = ttusb_cmd(ttusb, b3, sizeof(b3), 1)))
424 err = ttusb_result(ttusb, b4, sizeof(b4));
426 if ((err = ttusb_cmd(ttusb, get_version, sizeof(get_version), 1)))
429 if ((err = ttusb_result(ttusb, get_version, sizeof(get_version))))
432 dprintk("%s: stc-version: %c%c%c%c%c\n", __FUNCTION__,
433 get_version[4], get_version[5], get_version[6],
434 get_version[7], get_version[8]);
436 if (memcmp(get_version + 4, "V 0.0", 5) &&
437 memcmp(get_version + 4, "V 1.1", 5) &&
438 memcmp(get_version + 4, "V 2.1", 5) &&
439 memcmp(get_version + 4, "V 2.2", 5)) {
441 ("%s: unknown STC version %c%c%c%c%c, please report!\n",
442 __FUNCTION__, get_version[4], get_version[5],
443 get_version[6], get_version[7], get_version[8]);
446 ttusb->revision = ((get_version[6] - '0') << 4) |
447 (get_version[8] - '0');
450 ttusb_cmd(ttusb, get_dsp_version, sizeof(get_dsp_version), 1);
455 ttusb_result(ttusb, get_dsp_version, sizeof(get_dsp_version));
458 printk("%s: dsp-version: %c%c%c\n", __FUNCTION__,
459 get_dsp_version[4], get_dsp_version[5], get_dsp_version[6]);
464 static int ttusb_send_diseqc(struct dvb_frontend* fe,
465 const struct dvb_diseqc_master_cmd *cmd)
467 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
468 u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
472 b[3] = 4 + 2 + cmd->msg_len;
473 b[4] = 0xFF; /* send diseqc master, not burst */
476 memcpy(b + 5, cmd->msg, cmd->msg_len);
479 if ((err = ttusb_cmd(ttusb, b, 4 + b[3], 0))) {
480 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
488 static int lnbp21_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
490 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
493 struct i2c_msg msg = { .addr = 0x08, .flags = 0, .buf = data, .len = sizeof(data) };
496 case SEC_VOLTAGE_OFF:
509 ret = i2c_transfer(&ttusb->i2c_adap, &msg, 1);
510 return (ret != 1) ? -EIO : 0;
513 static int ttusb_update_lnb(struct ttusb *ttusb)
515 u8 b[] = { 0xaa, ++ttusb->c, 0x16, 5, /*power: */ 1,
516 ttusb->voltage == SEC_VOLTAGE_18 ? 0 : 1,
517 ttusb->tone == SEC_TONE_ON ? 1 : 0, 1, 1
522 if ((err = ttusb_cmd(ttusb, b, sizeof(b), 0))) {
523 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
530 static int ttusb_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
532 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
534 ttusb->voltage = voltage;
535 return ttusb_update_lnb(ttusb);
539 static int ttusb_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
541 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
544 return ttusb_update_lnb(ttusb);
550 static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
552 u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
555 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
557 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
563 /*****************************************************************************/
565 #ifdef TTUSB_HWSECTIONS
566 static void ttusb_handle_ts_data(struct ttusb_channel *channel,
567 const u8 * data, int len);
568 static void ttusb_handle_sec_data(struct ttusb_channel *channel,
569 const u8 * data, int len);
572 static int numpkt = 0, lastj, numts, numstuff, numsec, numinvalid;
574 static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
579 for (i = 0; i < len; i += 2)
580 csum ^= le16_to_cpup((u16 *) (muxpack + i));
582 printk("%s: muxpack with incorrect checksum, ignoring\n",
588 cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
590 if ((cc != ttusb->cc) && (ttusb->cc != -1))
591 printk("%s: cc discontinuity (%d frames missing)\n",
592 __FUNCTION__, (cc - ttusb->cc) & 0x7FFF);
593 ttusb->cc = (cc + 1) & 0x7FFF;
594 if (muxpack[0] & 0x80) {
595 #ifdef TTUSB_HWSECTIONS
597 int pusi = muxpack[0] & 0x40;
598 int channel = muxpack[0] & 0x1F;
599 int payload = muxpack[1];
600 const u8 *data = muxpack + 2;
601 /* check offset flag */
602 if (muxpack[0] & 0x20)
605 ttusb_handle_sec_data(ttusb->channel + channel, data,
609 if ((!!(ttusb->muxpack[0] & 0x20)) ^
610 !!(ttusb->muxpack[1] & 1))
613 printk("cc: %04x\n", (data[0] << 8) | data[1]);
616 } else if (muxpack[0] == 0x47) {
617 #ifdef TTUSB_HWSECTIONS
618 /* we have TS data here! */
619 int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
621 for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
622 if (ttusb->channel[channel].active
623 && (pid == ttusb->channel[channel].pid))
624 ttusb_handle_ts_data(ttusb->channel +
629 dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
630 } else if (muxpack[0] != 0) {
632 printk("illegal muxpack type %02x\n", muxpack[0]);
637 static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
642 printk("%s: too much work\n", __FUNCTION__);
646 switch (ttusb->mux_state) {
654 ttusb->mux_state = 0;
657 printk("%02x ", data[-1]);
660 printk("%s: lost sync.\n",
670 ttusb->mux_npacks = *data++;
672 ttusb->muxpack_ptr = 0;
673 /* maximum bytes, until we know the length */
674 ttusb->muxpack_len = 2;
681 (ttusb->muxpack_len -
686 memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
688 ttusb->muxpack_ptr += avail;
689 if (ttusb->muxpack_ptr > 264)
693 /* determine length */
694 if (ttusb->muxpack_ptr == 2) {
695 if (ttusb->muxpack[0] & 0x80) {
697 ttusb->muxpack[1] + 2;
704 muxpack[0] & 0x20)) ^
709 ttusb->muxpack_len += 4;
710 } else if (ttusb->muxpack[0] ==
714 else if (ttusb->muxpack[0] == 0x00)
716 ttusb->muxpack[1] + 2 +
720 ("%s: invalid state: first byte is %x\n",
723 ttusb->mux_state = 0;
728 * if length is valid and we reached the end:
731 if ((ttusb->muxpack_ptr >= 2) &&
732 (ttusb->muxpack_ptr ==
733 ttusb->muxpack_len)) {
734 ttusb_process_muxpack(ttusb,
739 ttusb->muxpack_ptr = 0;
740 /* maximum bytes, until we know the length */
741 ttusb->muxpack_len = 2;
745 * return to search-sync state
747 if (!ttusb->mux_npacks--) {
748 ttusb->mux_state = 0;
761 static void ttusb_iso_irq(struct urb *urb, struct pt_regs *ptregs)
763 struct ttusb *ttusb = urb->context;
765 if (!ttusb->iso_streaming)
769 printk("%s: status %d, errcount == %d, length == %i\n",
771 urb->status, urb->error_count, urb->actual_length);
776 for (i = 0; i < urb->number_of_packets; ++i) {
777 struct usb_iso_packet_descriptor *d;
781 if ((jiffies - lastj) >= HZ) {
784 ("frames/s: %d (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
785 numpkt * HZ / (jiffies - lastj),
786 numts, numstuff, numsec, numinvalid,
787 numts + numstuff + numsec +
790 numts = numstuff = numsec = numinvalid = 0;
794 d = &urb->iso_frame_desc[i];
795 data = urb->transfer_buffer + d->offset;
796 len = d->actual_length;
797 d->actual_length = 0;
799 ttusb_process_frame(ttusb, data, len);
802 usb_submit_urb(urb, GFP_ATOMIC);
805 static void ttusb_free_iso_urbs(struct ttusb *ttusb)
809 for (i = 0; i < ISO_BUF_COUNT; i++)
810 if (ttusb->iso_urb[i])
811 usb_free_urb(ttusb->iso_urb[i]);
813 pci_free_consistent(NULL,
814 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF *
815 ISO_BUF_COUNT, ttusb->iso_buffer,
816 ttusb->iso_dma_handle);
819 static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
823 ttusb->iso_buffer = pci_alloc_consistent(NULL,
827 &ttusb->iso_dma_handle);
829 memset(ttusb->iso_buffer, 0,
830 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF * ISO_BUF_COUNT);
832 for (i = 0; i < ISO_BUF_COUNT; i++) {
837 usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
838 ttusb_free_iso_urbs(ttusb);
842 ttusb->iso_urb[i] = urb;
848 static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
852 for (i = 0; i < ISO_BUF_COUNT; i++)
853 usb_kill_urb(ttusb->iso_urb[i]);
855 ttusb->iso_streaming = 0;
858 static int ttusb_start_iso_xfer(struct ttusb *ttusb)
860 int i, j, err, buffer_offset = 0;
862 if (ttusb->iso_streaming) {
863 printk("%s: iso xfer already running!\n", __FUNCTION__);
869 ttusb->mux_state = 0;
871 for (i = 0; i < ISO_BUF_COUNT; i++) {
872 int frame_offset = 0;
873 struct urb *urb = ttusb->iso_urb[i];
875 urb->dev = ttusb->dev;
876 urb->context = ttusb;
877 urb->complete = ttusb_iso_irq;
878 urb->pipe = ttusb->isoc_in_pipe;
879 urb->transfer_flags = URB_ISO_ASAP;
881 urb->number_of_packets = FRAMES_PER_ISO_BUF;
882 urb->transfer_buffer_length =
883 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
884 urb->transfer_buffer = ttusb->iso_buffer + buffer_offset;
885 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
887 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
888 urb->iso_frame_desc[j].offset = frame_offset;
889 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
890 frame_offset += ISO_FRAME_SIZE;
894 for (i = 0; i < ISO_BUF_COUNT; i++) {
895 if ((err = usb_submit_urb(ttusb->iso_urb[i], GFP_ATOMIC))) {
896 ttusb_stop_iso_xfer(ttusb);
898 ("%s: failed urb submission (%i: err = %i)!\n",
899 __FUNCTION__, i, err);
904 ttusb->iso_streaming = 1;
909 #ifdef TTUSB_HWSECTIONS
910 static void ttusb_handle_ts_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
913 dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
916 static void ttusb_handle_sec_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
919 // struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
920 #error TODO: handle ugly stuff
921 // dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
925 static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
927 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
930 dprintk("ttusb_start_feed\n");
932 switch (dvbdmxfeed->type) {
941 if (dvbdmxfeed->type == DMX_TYPE_TS) {
942 switch (dvbdmxfeed->pes_type) {
943 case DMX_TS_PES_VIDEO:
944 case DMX_TS_PES_AUDIO:
945 case DMX_TS_PES_TELETEXT:
947 case DMX_TS_PES_OTHER:
954 #ifdef TTUSB_HWSECTIONS
955 #error TODO: allocate filters
956 if (dvbdmxfeed->type == DMX_TYPE_TS) {
958 } else if (dvbdmxfeed->type == DMX_TYPE_SEC) {
963 ttusb_set_channel(ttusb, dvbdmxfeed->index, feed_type, dvbdmxfeed->pid);
965 if (0 == ttusb->running_feed_count++)
966 ttusb_start_iso_xfer(ttusb);
971 static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
973 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
975 ttusb_del_channel(ttusb, dvbdmxfeed->index);
977 if (--ttusb->running_feed_count == 0)
978 ttusb_stop_iso_xfer(ttusb);
983 static int ttusb_setup_interfaces(struct ttusb *ttusb)
985 usb_set_interface(ttusb->dev, 1, 1);
987 ttusb->bulk_out_pipe = usb_sndbulkpipe(ttusb->dev, 1);
988 ttusb->bulk_in_pipe = usb_rcvbulkpipe(ttusb->dev, 1);
989 ttusb->isoc_in_pipe = usb_rcvisocpipe(ttusb->dev, 2);
995 static u8 stc_firmware[8192];
997 static int stc_open(struct inode *inode, struct file *file)
999 struct ttusb *ttusb = file->private_data;
1002 for (addr = 0; addr < 8192; addr += 16) {
1003 u8 snd_buf[2] = { addr >> 8, addr & 0xFF };
1004 ttusb_i2c_msg(ttusb, 0x50, snd_buf, 2, stc_firmware + addr,
1011 static ssize_t stc_read(struct file *file, char *buf, size_t count,
1016 if ((tc + *offset) > 8192)
1017 tc = 8192 - *offset;
1022 if (copy_to_user(buf, stc_firmware + *offset, tc))
1030 static int stc_release(struct inode *inode, struct file *file)
1035 static struct file_operations stc_fops = {
1036 .owner = THIS_MODULE,
1039 .release = stc_release,
1043 static u32 functionality(struct i2c_adapter *adapter)
1045 return I2C_FUNC_I2C;
1050 static int alps_tdmb7_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1052 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1054 struct i2c_msg msg = {.addr=0x61, .flags=0, .buf=data, .len=sizeof(data) };
1057 div = (params->frequency + 36166667) / 166667;
1059 data[0] = (div >> 8) & 0x7f;
1060 data[1] = div & 0xff;
1061 data[2] = ((div >> 10) & 0x60) | 0x85;
1062 data[3] = params->frequency < 592000000 ? 0x40 : 0x80;
1064 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1) return -EIO;
1068 static struct cx22700_config alps_tdmb7_config = {
1069 .demod_address = 0x43,
1070 .pll_set = alps_tdmb7_pll_set,
1077 static int philips_tdm1316l_pll_init(struct dvb_frontend* fe)
1079 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1080 static u8 td1316_init[] = { 0x0b, 0xf5, 0x85, 0xab };
1081 static u8 disable_mc44BC374c[] = { 0x1d, 0x74, 0xa0, 0x68 };
1082 struct i2c_msg tuner_msg = { .addr=0x60, .flags=0, .buf=td1316_init, .len=sizeof(td1316_init) };
1084 // setup PLL configuration
1085 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) return -EIO;
1088 // disable the mc44BC374c (do not check for errors)
1089 tuner_msg.addr = 0x65;
1090 tuner_msg.buf = disable_mc44BC374c;
1091 tuner_msg.len = sizeof(disable_mc44BC374c);
1092 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1093 i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1);
1099 static int philips_tdm1316l_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
1101 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1103 struct i2c_msg tuner_msg = {.addr=0x60, .flags=0, .buf=tuner_buf, .len=sizeof(tuner_buf) };
1104 int tuner_frequency = 0;
1105 u8 band, cp, filter;
1107 // determine charge pump
1108 tuner_frequency = params->frequency + 36130000;
1109 if (tuner_frequency < 87000000) return -EINVAL;
1110 else if (tuner_frequency < 130000000) cp = 3;
1111 else if (tuner_frequency < 160000000) cp = 5;
1112 else if (tuner_frequency < 200000000) cp = 6;
1113 else if (tuner_frequency < 290000000) cp = 3;
1114 else if (tuner_frequency < 420000000) cp = 5;
1115 else if (tuner_frequency < 480000000) cp = 6;
1116 else if (tuner_frequency < 620000000) cp = 3;
1117 else if (tuner_frequency < 830000000) cp = 5;
1118 else if (tuner_frequency < 895000000) cp = 7;
1119 else return -EINVAL;
1122 if (params->frequency < 49000000) return -EINVAL;
1123 else if (params->frequency < 159000000) band = 1;
1124 else if (params->frequency < 444000000) band = 2;
1125 else if (params->frequency < 861000000) band = 4;
1126 else return -EINVAL;
1129 switch (params->u.ofdm.bandwidth) {
1130 case BANDWIDTH_6_MHZ:
1131 tda1004x_write_byte(fe, 0x0C, 0);
1135 case BANDWIDTH_7_MHZ:
1136 tda1004x_write_byte(fe, 0x0C, 0);
1140 case BANDWIDTH_8_MHZ:
1141 tda1004x_write_byte(fe, 0x0C, 0xFF);
1149 // calculate divisor
1150 // ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
1151 tuner_frequency = (((params->frequency / 1000) * 6) + 217280) / 1000;
1153 // setup tuner buffer
1154 tuner_buf[0] = tuner_frequency >> 8;
1155 tuner_buf[1] = tuner_frequency & 0xff;
1156 tuner_buf[2] = 0xca;
1157 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1159 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1)
1166 static int philips_tdm1316l_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1168 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1170 return request_firmware(fw, name, &ttusb->dev->dev);
1173 static struct tda1004x_config philips_tdm1316l_config = {
1175 .demod_address = 0x8,
1178 .pll_init = philips_tdm1316l_pll_init,
1179 .pll_set = philips_tdm1316l_pll_set,
1180 .request_firmware = philips_tdm1316l_request_firmware,
1183 static u8 alps_bsbe1_inittab[] = {
1187 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1188 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1189 0x06, 0x40, /* DAC not used, set to high impendance mode */
1190 0x07, 0x00, /* DAC LSB */
1191 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1192 0x09, 0x00, /* FIFO */
1193 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1194 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1195 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1196 0x10, 0x3f, // AGC2 0x3d
1198 0x12, 0xb5, // Lock detect: -64 Carrier freq detect:on
1199 0x15, 0xc9, // lock detector threshold
1210 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1211 0x29, 0x1e, // 1/2 threshold
1212 0x2a, 0x14, // 2/3 threshold
1213 0x2b, 0x0f, // 3/4 threshold
1214 0x2c, 0x09, // 5/6 threshold
1215 0x2d, 0x05, // 7/8 threshold
1217 0x31, 0x1f, // test all FECs
1218 0x32, 0x19, // viterbi and synchro search
1219 0x33, 0xfc, // rs control
1220 0x34, 0x93, // error control
1225 static u8 alps_bsru6_inittab[] = {
1229 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1230 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1231 0x06, 0x40, /* DAC not used, set to high impendance mode */
1232 0x07, 0x00, /* DAC LSB */
1233 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1234 0x09, 0x00, /* FIFO */
1235 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1236 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1237 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1238 0x10, 0x3f, // AGC2 0x3d
1240 0x12, 0xb5, // Lock detect: -64 Carrier freq detect:on
1241 0x15, 0xc9, // lock detector threshold
1252 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1253 0x29, 0x1e, // 1/2 threshold
1254 0x2a, 0x14, // 2/3 threshold
1255 0x2b, 0x0f, // 3/4 threshold
1256 0x2c, 0x09, // 5/6 threshold
1257 0x2d, 0x05, // 7/8 threshold
1259 0x31, 0x1f, // test all FECs
1260 0x32, 0x19, // viterbi and synchro search
1261 0x33, 0xfc, // rs control
1262 0x34, 0x93, // error control
1267 static int alps_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
1272 if (srate < 1500000) {
1275 } else if (srate < 3000000) {
1278 } else if (srate < 7000000) {
1281 } else if (srate < 14000000) {
1284 } else if (srate < 30000000) {
1287 } else if (srate < 45000000) {
1292 stv0299_writereg(fe, 0x13, aclk);
1293 stv0299_writereg(fe, 0x14, bclk);
1294 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
1295 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
1296 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
1301 static int philips_tsa5059_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1303 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1306 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1308 if ((params->frequency < 950000) || (params->frequency > 2150000))
1311 div = (params->frequency + (125 - 1)) / 125; // round correctly
1312 buf[0] = (div >> 8) & 0x7f;
1313 buf[1] = div & 0xff;
1314 buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
1317 if (params->frequency > 1530000)
1320 /* BSBE1 wants XCE bit set */
1321 if (ttusb->revision == TTUSB_REV_2_2)
1324 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1330 static struct stv0299_config alps_stv0299_config = {
1331 .demod_address = 0x68,
1332 .inittab = alps_bsru6_inittab,
1335 .enhanced_tuning = 0,
1337 .lock_output = STV0229_LOCKOUTPUT_1,
1338 .volt13_op0_op1 = STV0299_VOLT13_OP1,
1339 .min_delay_ms = 100,
1340 .set_symbol_rate = alps_stv0299_set_symbol_rate,
1341 .pll_set = philips_tsa5059_pll_set,
1344 static int ttusb_novas_grundig_29504_491_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
1346 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1349 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1351 div = params->frequency / 125;
1353 buf[0] = (div >> 8) & 0x7f;
1354 buf[1] = div & 0xff;
1358 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1364 static struct tda8083_config ttusb_novas_grundig_29504_491_config = {
1366 .demod_address = 0x68,
1367 .pll_set = ttusb_novas_grundig_29504_491_pll_set,
1372 static void frontend_init(struct ttusb* ttusb)
1374 switch(le16_to_cpu(ttusb->dev->descriptor.idProduct)) {
1375 case 0x1003: // Hauppauge/TT Nova-USB-S budget (stv0299/ALPS BSRU6|BSBE1(tsa5059))
1376 // try the stv0299 based first
1377 ttusb->fe = stv0299_attach(&alps_stv0299_config, &ttusb->i2c_adap);
1378 if (ttusb->fe != NULL) {
1379 if(ttusb->revision == TTUSB_REV_2_2) { // ALPS BSBE1
1380 alps_stv0299_config.inittab = alps_bsbe1_inittab;
1381 ttusb->fe->ops->set_voltage = lnbp21_set_voltage;
1382 } else { // ALPS BSRU6
1383 ttusb->fe->ops->set_voltage = ttusb_set_voltage;
1388 // Grundig 29504-491
1389 ttusb->fe = tda8083_attach(&ttusb_novas_grundig_29504_491_config, &ttusb->i2c_adap);
1390 if (ttusb->fe != NULL) {
1391 ttusb->fe->ops->set_voltage = ttusb_set_voltage;
1397 case 0x1005: // Hauppauge/TT Nova-USB-t budget (tda10046/Philips td1316(tda6651tt) OR cx22700/ALPS TDMB7(??))
1398 // try the ALPS TDMB7 first
1399 ttusb->fe = cx22700_attach(&alps_tdmb7_config, &ttusb->i2c_adap);
1400 if (ttusb->fe != NULL)
1404 ttusb->fe = tda10046_attach(&philips_tdm1316l_config, &ttusb->i2c_adap);
1405 if (ttusb->fe != NULL)
1410 if (ttusb->fe == NULL) {
1411 printk("dvb-ttusb-budget: A frontend driver was not found for device %04x/%04x\n",
1412 le16_to_cpu(ttusb->dev->descriptor.idVendor),
1413 le16_to_cpu(ttusb->dev->descriptor.idProduct));
1415 if (dvb_register_frontend(&ttusb->adapter, ttusb->fe)) {
1416 printk("dvb-ttusb-budget: Frontend registration failed!\n");
1417 if (ttusb->fe->ops->release)
1418 ttusb->fe->ops->release(ttusb->fe);
1426 static struct i2c_algorithm ttusb_dec_algo = {
1427 .name = "ttusb dec i2c algorithm",
1429 .master_xfer = master_xfer,
1430 .functionality = functionality,
1433 static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1435 struct usb_device *udev;
1436 struct ttusb *ttusb;
1439 dprintk("%s: TTUSB DVB connected\n", __FUNCTION__);
1441 udev = interface_to_usbdev(intf);
1443 if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
1445 if (!(ttusb = kmalloc(sizeof(struct ttusb), GFP_KERNEL)))
1448 memset(ttusb, 0, sizeof(struct ttusb));
1452 ttusb->mux_state = 0;
1453 sema_init(&ttusb->semi2c, 0);
1454 sema_init(&ttusb->semusb, 1);
1456 ttusb_setup_interfaces(ttusb);
1458 ttusb_alloc_iso_urbs(ttusb);
1459 if (ttusb_init_controller(ttusb))
1460 printk("ttusb_init_controller: error\n");
1464 dvb_register_adapter(&ttusb->adapter, "Technotrend/Hauppauge Nova-USB", THIS_MODULE);
1465 ttusb->adapter.priv = ttusb;
1468 memset(&ttusb->i2c_adap, 0, sizeof(struct i2c_adapter));
1469 strcpy(ttusb->i2c_adap.name, "TTUSB DEC");
1471 i2c_set_adapdata(&ttusb->i2c_adap, ttusb);
1473 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
1474 ttusb->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL;
1476 ttusb->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
1478 ttusb->i2c_adap.algo = &ttusb_dec_algo;
1479 ttusb->i2c_adap.algo_data = NULL;
1480 ttusb->i2c_adap.id = I2C_ALGO_BIT;
1482 result = i2c_add_adapter(&ttusb->i2c_adap);
1484 dvb_unregister_adapter (&ttusb->adapter);
1488 memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1490 ttusb->dvb_demux.dmx.capabilities =
1491 DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1492 ttusb->dvb_demux.priv = NULL;
1493 #ifdef TTUSB_HWSECTIONS
1494 ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1496 ttusb->dvb_demux.filternum = 32;
1498 ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1499 ttusb->dvb_demux.start_feed = ttusb_start_feed;
1500 ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1501 ttusb->dvb_demux.write_to_decoder = NULL;
1503 if ((result = dvb_dmx_init(&ttusb->dvb_demux)) < 0) {
1504 printk("ttusb_dvb: dvb_dmx_init failed (errno = %d)\n", result);
1505 i2c_del_adapter(&ttusb->i2c_adap);
1506 dvb_unregister_adapter (&ttusb->adapter);
1509 //FIXME dmxdev (nur WAS?)
1510 ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1511 ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1512 ttusb->dmxdev.capabilities = 0;
1514 if ((result = dvb_dmxdev_init(&ttusb->dmxdev, &ttusb->adapter)) < 0) {
1515 printk("ttusb_dvb: dvb_dmxdev_init failed (errno = %d)\n",
1517 dvb_dmx_release(&ttusb->dvb_demux);
1518 i2c_del_adapter(&ttusb->i2c_adap);
1519 dvb_unregister_adapter (&ttusb->adapter);
1523 if (dvb_net_init(&ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
1524 printk("ttusb_dvb: dvb_net_init failed!\n");
1525 dvb_dmxdev_release(&ttusb->dmxdev);
1526 dvb_dmx_release(&ttusb->dvb_demux);
1527 i2c_del_adapter(&ttusb->i2c_adap);
1528 dvb_unregister_adapter (&ttusb->adapter);
1533 ttusb->stc_devfs_handle =
1534 devfs_register(ttusb->adapter->devfs_handle, TTUSB_BUDGET_NAME,
1535 DEVFS_FL_DEFAULT, 0, 192,
1536 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
1537 | S_IROTH | S_IWOTH, &stc_fops, ttusb);
1539 usb_set_intfdata(intf, (void *) ttusb);
1541 frontend_init(ttusb);
1546 static void ttusb_disconnect(struct usb_interface *intf)
1548 struct ttusb *ttusb = usb_get_intfdata(intf);
1550 usb_set_intfdata(intf, NULL);
1552 ttusb->disconnecting = 1;
1554 ttusb_stop_iso_xfer(ttusb);
1556 ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1557 dvb_net_release(&ttusb->dvbnet);
1558 dvb_dmxdev_release(&ttusb->dmxdev);
1559 dvb_dmx_release(&ttusb->dvb_demux);
1560 if (ttusb->fe != NULL) dvb_unregister_frontend(ttusb->fe);
1561 i2c_del_adapter(&ttusb->i2c_adap);
1562 dvb_unregister_adapter(&ttusb->adapter);
1564 ttusb_free_iso_urbs(ttusb);
1568 dprintk("%s: TTUSB DVB disconnected\n", __FUNCTION__);
1571 static struct usb_device_id ttusb_table[] = {
1572 {USB_DEVICE(0xb48, 0x1003)},
1573 /* {USB_DEVICE(0xb48, 0x1004)},UNDEFINED HARDWARE - mail linuxtv.org list*/ /* to be confirmed ???? */
1574 {USB_DEVICE(0xb48, 0x1005)},
1578 MODULE_DEVICE_TABLE(usb, ttusb_table);
1580 static struct usb_driver ttusb_driver = {
1581 .name = "Technotrend/Hauppauge USB-Nova",
1582 .probe = ttusb_probe,
1583 .disconnect = ttusb_disconnect,
1584 .id_table = ttusb_table,
1587 static int __init ttusb_init(void)
1591 if ((err = usb_register(&ttusb_driver)) < 0) {
1592 printk("%s: usb_register failed! Error number %d",
1600 static void __exit ttusb_exit(void)
1602 usb_deregister(&ttusb_driver);
1605 module_init(ttusb_init);
1606 module_exit(ttusb_exit);
1608 MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1609 MODULE_DESCRIPTION("TTUSB DVB Driver");
1610 MODULE_LICENSE("GPL");