1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (c) 2002 Holger Waechtler <holger@convergence.de>
6 * Copyright (c) 2003 Felix Domke <tmbinc@elitedvb.net>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #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>
22 #include <linux/firmware.h>
24 #include <media/dvb_frontend.h>
25 #include <media/dmxdev.h>
26 #include <media/dvb_demux.h>
27 #include <media/dvb_net.h>
36 #include <linux/dvb/frontend.h>
37 #include <linux/dvb/dmx.h>
38 #include <linux/pci.h>
42 the DSP supports filtering in hardware, however, since the "muxstream"
43 is a bit braindead (no matching channel masks or no matching filter mask),
44 we won't support this - yet. it doesn't event support negative filters,
45 so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
46 parse TS data. USB bandwidth will be a problem when having large
47 datastreams, especially for dvb-net, but hey, that's not my problem.
49 TTUSB_DISEQC, TTUSB_TONE:
50 let the STC do the diseqc/tone stuff. this isn't supported at least with
51 my TTUSB, so let it undef'd unless you want to implement another
52 frontend. never tested.
55 define it to > 3 for really hardcore debugging. you probably don't want
56 this unless the device doesn't load at all. > 2 for bandwidth statistics.
60 module_param(debug, int, 0644);
61 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
63 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
65 #define dprintk(fmt, arg...) do { \
67 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
72 #define ISO_BUF_COUNT 4
73 #define FRAMES_PER_ISO_BUF 4
74 #define ISO_FRAME_SIZE 912
75 #define TTUSB_MAXCHANNEL 32
76 #ifdef TTUSB_HWSECTIONS
77 #define TTUSB_MAXFILTER 16 /* ??? */
80 #define TTUSB_REV_2_2 0x22
81 #define TTUSB_BUDGET_NAME "ttusb_stc_fw"
87 * since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
88 * the dvb_demux field must be the first in struct!!
91 struct dvb_demux dvb_demux;
93 struct dvb_net dvbnet;
95 /* and one for USB access. */
99 struct dvb_adapter adapter;
100 struct usb_device *dev;
102 struct i2c_adapter i2c_adap;
107 unsigned int bulk_out_pipe;
108 unsigned int bulk_in_pipe;
109 unsigned int isoc_in_pipe;
113 struct urb *iso_urb[ISO_BUF_COUNT];
115 int running_feed_count;
119 u8 c; /* transaction counter, wraps around... */
120 enum fe_sec_tone_mode tone;
121 enum fe_sec_voltage voltage;
123 int mux_state; // 0..2 - MuxSyncWord, 3 - nMuxPacks, 4 - muxpack
126 int muxpack_ptr, muxpack_len;
130 int cc; /* MuxCounter - will increment on EVERY MUX PACKET */
131 /* (including stuffing. yes. really.) */
133 u8 send_buf[MAX_SEND];
134 u8 last_result[MAX_RCV];
138 struct dvb_frontend* fe;
141 static int ttusb_cmd(struct ttusb *ttusb, u8 *data, int len, int len_result)
146 if (mutex_lock_interruptible(&ttusb->semusb) < 0)
150 dprintk("> %*ph\n", len, data);
152 memcpy(data, ttusb->send_buf, len);
154 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_out_pipe,
155 ttusb->send_buf, len, &actual_len, 1000);
157 dprintk("usb_bulk_msg(send) failed, err == %i!\n", err);
160 if (actual_len != len) {
162 dprintk("only wrote %d of %d bytes\n",
167 err = usb_bulk_msg(ttusb->dev, ttusb->bulk_in_pipe,
168 ttusb->last_result, MAX_RCV, &actual_len, 1000);
171 pr_err("cmd xter failed, receive error %d\n", err);
176 actual_len = ttusb->last_result[3] + 4;
177 dprintk("< %*ph\n", actual_len, ttusb->last_result);
181 memcpy(ttusb->send_buf, ttusb->last_result, len_result);
184 mutex_unlock(&ttusb->semusb);
188 static int ttusb_i2c_msg(struct ttusb *ttusb,
189 u8 addr, u8 * snd_buf, u8 snd_len, u8 * rcv_buf,
196 if (snd_len > MAX_SEND - 7 || rcv_len > MAX_RCV - 7)
207 for (i = 0; i < snd_len; i++)
208 b[7 + i] = snd_buf[i];
210 err = ttusb_cmd(ttusb, b, snd_len + 7, MAX_RCV);
215 /* check if the i2c transaction was successful */
216 if ((snd_len != b[5]) || (rcv_len != b[6])) return -EREMOTEIO;
220 if (err || b[0] != 0x55 || b[1] != id) {
221 dprintk("usb_bulk_msg(recv) failed, err == %i, id == %02x, b == ",
226 for (i = 0; i < rcv_len; i++)
227 rcv_buf[i] = b[7 + i];
233 static int master_xfer(struct i2c_adapter* adapter, struct i2c_msg *msg, int num)
235 struct ttusb *ttusb = i2c_get_adapdata(adapter);
239 if (mutex_lock_interruptible(&ttusb->semi2c) < 0)
243 u8 addr, snd_len, rcv_len, *snd_buf, *rcv_buf;
246 if (num > i + 1 && (msg[i + 1].flags & I2C_M_RD)) {
248 snd_buf = msg[i].buf;
249 snd_len = msg[i].len;
250 rcv_buf = msg[i + 1].buf;
251 rcv_len = msg[i + 1].len;
255 snd_buf = msg[i].buf;
256 snd_len = msg[i].len;
262 err = ttusb_i2c_msg(ttusb, addr,
263 snd_buf, snd_len, rcv_buf, rcv_len);
266 dprintk("i == %i\n", i);
273 mutex_unlock(&ttusb->semi2c);
277 static int ttusb_boot_dsp(struct ttusb *ttusb)
279 const struct firmware *fw;
283 err = reject_firmware(&fw, "/*(DEBLOBBED)*/",
286 pr_err("failed to request firmware\n");
295 /* upload dsp code in 32 byte steps (36 didn't work for me ...) */
296 /* 32 is max packet size, no messages should be split. */
297 for (i = 0; i < fw->size; i += 28) {
298 memcpy(&b[4], &fw->data[i], 28);
302 err = ttusb_cmd(ttusb, b, 32, 0);
312 err = ttusb_cmd(ttusb, b, 4, 0);
321 err = ttusb_cmd(ttusb, b, 4, 0);
324 release_firmware(fw);
326 dprintk("usb_bulk_msg() failed, return value %i!\n", err);
332 static int ttusb_set_channel(struct ttusb *ttusb, int chan_id, int filter_type,
337 u8 b[] = { 0xaa, ++ttusb->c, 0x22, 4, chan_id, filter_type,
338 (pid >> 8) & 0xff, pid & 0xff
341 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
345 static int ttusb_del_channel(struct ttusb *ttusb, int channel_id)
349 u8 b[] = { 0xaa, ++ttusb->c, 0x23, 1, channel_id };
351 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
355 #ifdef TTUSB_HWSECTIONS
356 static int ttusb_set_filter(struct ttusb *ttusb, int filter_id,
357 int associated_chan, u8 filter[8], u8 mask[8])
361 u8 b[] = { 0xaa, 0, 0x24, 0x1a, filter_id, associated_chan,
362 filter[0], filter[1], filter[2], filter[3],
363 filter[4], filter[5], filter[6], filter[7],
364 filter[8], filter[9], filter[10], filter[11],
365 mask[0], mask[1], mask[2], mask[3],
366 mask[4], mask[5], mask[6], mask[7],
367 mask[8], mask[9], mask[10], mask[11]
370 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
374 static int ttusb_del_filter(struct ttusb *ttusb, int filter_id)
378 u8 b[] = { 0xaa, ++ttusb->c, 0x25, 1, filter_id };
380 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
385 static int ttusb_init_controller(struct ttusb *ttusb)
387 u8 b0[] = { 0xaa, ++ttusb->c, 0x15, 1, 0 };
388 u8 b1[] = { 0xaa, ++ttusb->c, 0x15, 1, 1 };
389 u8 b2[] = { 0xaa, ++ttusb->c, 0x32, 1, 0 };
390 /* i2c write read: 5 bytes, addr 0x10, 0x02 bytes write, 1 bytes read. */
392 { 0xaa, ++ttusb->c, 0x31, 5, 0x10, 0x02, 0x01, 0x00, 0x1e };
394 u8 get_version[] = { 0xaa, ++ttusb->c, 0x17, 5, 0, 0, 0, 0, 0 };
395 u8 get_dsp_version[0x20] =
396 { 0xaa, ++ttusb->c, 0x26, 28, 0, 0, 0, 0, 0 };
400 if ((err = ttusb_cmd(ttusb, b0, sizeof(b0), 0)))
403 /* reset board (again?) */
404 if ((err = ttusb_cmd(ttusb, b1, sizeof(b1), 0)))
407 ttusb_boot_dsp(ttusb);
409 /* set i2c bit rate */
410 if ((err = ttusb_cmd(ttusb, b2, sizeof(b2), 0)))
413 if ((err = ttusb_cmd(ttusb, b3, sizeof(b3), 0)))
416 if ((err = ttusb_cmd(ttusb, get_version,
417 sizeof(get_version), sizeof(get_version))))
420 dprintk("stc-version: %c%c%c%c%c\n", get_version[4], get_version[5],
421 get_version[6], get_version[7], get_version[8]);
423 if (memcmp(get_version + 4, "V 0.0", 5) &&
424 memcmp(get_version + 4, "V 1.1", 5) &&
425 memcmp(get_version + 4, "V 2.1", 5) &&
426 memcmp(get_version + 4, "V 2.2", 5)) {
427 pr_err("unknown STC version %c%c%c%c%c, please report!\n",
428 get_version[4], get_version[5],
429 get_version[6], get_version[7], get_version[8]);
432 ttusb->revision = ((get_version[6] - '0') << 4) |
433 (get_version[8] - '0');
436 ttusb_cmd(ttusb, get_dsp_version,
437 sizeof(get_dsp_version), sizeof(get_dsp_version));
441 pr_info("dsp-version: %c%c%c\n",
442 get_dsp_version[4], get_dsp_version[5], get_dsp_version[6]);
447 static int ttusb_send_diseqc(struct dvb_frontend* fe,
448 const struct dvb_diseqc_master_cmd *cmd)
450 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
451 u8 b[12] = { 0xaa, ++ttusb->c, 0x18 };
455 b[3] = 4 + 2 + cmd->msg_len;
456 b[4] = 0xFF; /* send diseqc master, not burst */
459 memcpy(b + 5, cmd->msg, cmd->msg_len);
462 if ((err = ttusb_cmd(ttusb, b, 4 + b[3], 0))) {
463 dprintk("usb_bulk_msg() failed, return value %i!\n", err);
470 static int ttusb_update_lnb(struct ttusb *ttusb)
472 u8 b[] = { 0xaa, ++ttusb->c, 0x16, 5, /*power: */ 1,
473 ttusb->voltage == SEC_VOLTAGE_18 ? 0 : 1,
474 ttusb->tone == SEC_TONE_ON ? 1 : 0, 1, 1
479 if ((err = ttusb_cmd(ttusb, b, sizeof(b), 0))) {
480 dprintk("usb_bulk_msg() failed, return value %i!\n", err);
486 static int ttusb_set_voltage(struct dvb_frontend *fe,
487 enum fe_sec_voltage voltage)
489 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
491 ttusb->voltage = voltage;
492 return ttusb_update_lnb(ttusb);
496 static int ttusb_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
498 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
501 return ttusb_update_lnb(ttusb);
507 static void ttusb_set_led_freq(struct ttusb *ttusb, u8 freq)
509 u8 b[] = { 0xaa, ++ttusb->c, 0x19, 1, freq };
512 err = ttusb_cmd(ttusb, b, sizeof(b), 0);
514 dprintk("usb_bulk_msg() failed, return value %i!\n", err);
519 /*****************************************************************************/
521 #ifdef TTUSB_HWSECTIONS
522 static void ttusb_handle_ts_data(struct ttusb_channel *channel,
523 const u8 * data, int len);
524 static void ttusb_handle_sec_data(struct ttusb_channel *channel,
525 const u8 * data, int len);
528 static int numpkt, numts, numstuff, numsec, numinvalid;
529 static unsigned long lastj;
531 static void ttusb_process_muxpack(struct ttusb *ttusb, const u8 * muxpack,
537 if (len < 4 || len & 0x1) {
538 pr_warn("muxpack has invalid len %d\n", len);
543 for (i = 0; i < len; i += 2)
544 csum ^= le16_to_cpup((__le16 *) (muxpack + i));
546 pr_warn("muxpack with incorrect checksum, ignoring\n");
551 cc = (muxpack[len - 4] << 8) | muxpack[len - 3];
553 if ((cc != ttusb->cc) && (ttusb->cc != -1))
554 pr_warn("cc discontinuity (%d frames missing)\n",
555 (cc - ttusb->cc) & 0x7FFF);
556 ttusb->cc = (cc + 1) & 0x7FFF;
557 if (muxpack[0] & 0x80) {
558 #ifdef TTUSB_HWSECTIONS
560 int pusi = muxpack[0] & 0x40;
561 int channel = muxpack[0] & 0x1F;
562 int payload = muxpack[1];
563 const u8 *data = muxpack + 2;
564 /* check offset flag */
565 if (muxpack[0] & 0x20)
568 ttusb_handle_sec_data(ttusb->channel + channel, data,
572 if ((!!(ttusb->muxpack[0] & 0x20)) ^
573 !!(ttusb->muxpack[1] & 1))
576 dprintk("cc: %04x\n", (data[0] << 8) | data[1]);
579 } else if (muxpack[0] == 0x47) {
580 #ifdef TTUSB_HWSECTIONS
581 /* we have TS data here! */
582 int pid = ((muxpack[1] & 0x0F) << 8) | muxpack[2];
584 for (channel = 0; channel < TTUSB_MAXCHANNEL; ++channel)
585 if (ttusb->channel[channel].active
586 && (pid == ttusb->channel[channel].pid))
587 ttusb_handle_ts_data(ttusb->channel +
592 dvb_dmx_swfilter_packets(&ttusb->dvb_demux, muxpack, 1);
593 } else if (muxpack[0] != 0) {
595 pr_err("illegal muxpack type %02x\n", muxpack[0]);
600 static void ttusb_process_frame(struct ttusb *ttusb, u8 * data, int len)
605 pr_err("too much work\n");
609 switch (ttusb->mux_state) {
617 ttusb->mux_state = 0;
619 pr_info("lost sync.\n");
627 ttusb->mux_npacks = *data++;
629 ttusb->muxpack_ptr = 0;
630 /* maximum bytes, until we know the length */
631 ttusb->muxpack_len = 2;
638 (ttusb->muxpack_len -
643 memcpy(ttusb->muxpack + ttusb->muxpack_ptr,
645 ttusb->muxpack_ptr += avail;
646 BUG_ON(ttusb->muxpack_ptr > 264);
649 /* determine length */
650 if (ttusb->muxpack_ptr == 2) {
651 if (ttusb->muxpack[0] & 0x80) {
653 ttusb->muxpack[1] + 2;
660 muxpack[0] & 0x20)) ^
665 ttusb->muxpack_len += 4;
666 } else if (ttusb->muxpack[0] ==
670 else if (ttusb->muxpack[0] == 0x00)
672 ttusb->muxpack[1] + 2 +
675 dprintk("invalid state: first byte is %x\n",
677 ttusb->mux_state = 0;
682 * if length is valid and we reached the end:
685 if ((ttusb->muxpack_ptr >= 2) &&
686 (ttusb->muxpack_ptr ==
687 ttusb->muxpack_len)) {
688 ttusb_process_muxpack(ttusb,
693 ttusb->muxpack_ptr = 0;
694 /* maximum bytes, until we know the length */
695 ttusb->muxpack_len = 2;
699 * return to search-sync state
701 if (!ttusb->mux_npacks--) {
702 ttusb->mux_state = 0;
715 static void ttusb_iso_irq(struct urb *urb)
717 struct ttusb *ttusb = urb->context;
718 struct usb_iso_packet_descriptor *d;
722 if (!ttusb->iso_streaming)
726 for (i = 0; i < urb->number_of_packets; ++i) {
728 if (time_after_eq(jiffies, lastj + HZ)) {
729 dprintk("frames/s: %lu (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
730 numpkt * HZ / (jiffies - lastj),
731 numts, numstuff, numsec, numinvalid,
732 numts + numstuff + numsec + numinvalid);
733 numts = numstuff = numsec = numinvalid = 0;
737 d = &urb->iso_frame_desc[i];
738 data = urb->transfer_buffer + d->offset;
739 len = d->actual_length;
740 d->actual_length = 0;
742 ttusb_process_frame(ttusb, data, len);
745 usb_submit_urb(urb, GFP_ATOMIC);
748 static void ttusb_free_iso_urbs(struct ttusb *ttusb)
752 for (i = 0; i < ISO_BUF_COUNT; i++)
753 usb_free_urb(ttusb->iso_urb[i]);
754 kfree(ttusb->iso_buffer);
757 static int ttusb_alloc_iso_urbs(struct ttusb *ttusb)
761 ttusb->iso_buffer = kcalloc(FRAMES_PER_ISO_BUF * ISO_BUF_COUNT,
762 ISO_FRAME_SIZE, GFP_KERNEL);
763 if (!ttusb->iso_buffer)
766 for (i = 0; i < ISO_BUF_COUNT; i++) {
771 usb_alloc_urb(FRAMES_PER_ISO_BUF, GFP_ATOMIC))) {
772 ttusb_free_iso_urbs(ttusb);
776 ttusb->iso_urb[i] = urb;
782 static void ttusb_stop_iso_xfer(struct ttusb *ttusb)
786 for (i = 0; i < ISO_BUF_COUNT; i++)
787 usb_kill_urb(ttusb->iso_urb[i]);
789 ttusb->iso_streaming = 0;
792 static int ttusb_start_iso_xfer(struct ttusb *ttusb)
794 int i, j, err, buffer_offset = 0;
796 if (ttusb->iso_streaming) {
797 pr_err("iso xfer already running!\n");
803 ttusb->mux_state = 0;
805 for (i = 0; i < ISO_BUF_COUNT; i++) {
806 int frame_offset = 0;
807 struct urb *urb = ttusb->iso_urb[i];
809 urb->dev = ttusb->dev;
810 urb->context = ttusb;
811 urb->complete = ttusb_iso_irq;
812 urb->pipe = ttusb->isoc_in_pipe;
813 urb->transfer_flags = URB_ISO_ASAP;
815 urb->number_of_packets = FRAMES_PER_ISO_BUF;
816 urb->transfer_buffer_length =
817 ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
818 urb->transfer_buffer = ttusb->iso_buffer + buffer_offset;
819 buffer_offset += ISO_FRAME_SIZE * FRAMES_PER_ISO_BUF;
821 for (j = 0; j < FRAMES_PER_ISO_BUF; j++) {
822 urb->iso_frame_desc[j].offset = frame_offset;
823 urb->iso_frame_desc[j].length = ISO_FRAME_SIZE;
824 frame_offset += ISO_FRAME_SIZE;
828 for (i = 0; i < ISO_BUF_COUNT; i++) {
829 if ((err = usb_submit_urb(ttusb->iso_urb[i], GFP_ATOMIC))) {
830 ttusb_stop_iso_xfer(ttusb);
831 pr_err("failed urb submission (%i: err = %i)!\n",
837 ttusb->iso_streaming = 1;
842 #ifdef TTUSB_HWSECTIONS
843 static void ttusb_handle_ts_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
846 dvbdmxfeed->cb.ts(data, len, 0, 0, &dvbdmxfeed->feed.ts, 0);
849 static void ttusb_handle_sec_data(struct dvb_demux_feed *dvbdmxfeed, const u8 * data,
852 // struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
853 #error TODO: handle ugly stuff
854 // dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
858 static int ttusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
860 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
863 dprintk("ttusb_start_feed\n");
865 switch (dvbdmxfeed->type) {
874 if (dvbdmxfeed->type == DMX_TYPE_TS) {
875 switch (dvbdmxfeed->pes_type) {
878 case DMX_PES_TELETEXT:
887 #ifdef TTUSB_HWSECTIONS
888 #error TODO: allocate filters
889 if (dvbdmxfeed->type == DMX_TYPE_TS) {
891 } else if (dvbdmxfeed->type == DMX_TYPE_SEC) {
896 ttusb_set_channel(ttusb, dvbdmxfeed->index, feed_type, dvbdmxfeed->pid);
898 if (0 == ttusb->running_feed_count++)
899 ttusb_start_iso_xfer(ttusb);
904 static int ttusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
906 struct ttusb *ttusb = (struct ttusb *) dvbdmxfeed->demux;
908 ttusb_del_channel(ttusb, dvbdmxfeed->index);
910 if (--ttusb->running_feed_count == 0)
911 ttusb_stop_iso_xfer(ttusb);
916 static int ttusb_setup_interfaces(struct ttusb *ttusb)
918 usb_set_interface(ttusb->dev, 1, 1);
920 ttusb->bulk_out_pipe = usb_sndbulkpipe(ttusb->dev, 1);
921 ttusb->bulk_in_pipe = usb_rcvbulkpipe(ttusb->dev, 1);
922 ttusb->isoc_in_pipe = usb_rcvisocpipe(ttusb->dev, 2);
928 static u8 stc_firmware[8192];
930 static int stc_open(struct inode *inode, struct file *file)
932 struct ttusb *ttusb = file->private_data;
935 for (addr = 0; addr < 8192; addr += 16) {
936 u8 snd_buf[2] = { addr >> 8, addr & 0xFF };
937 ttusb_i2c_msg(ttusb, 0x50, snd_buf, 2, stc_firmware + addr,
944 static ssize_t stc_read(struct file *file, char *buf, size_t count,
947 return simple_read_from_buffer(buf, count, offset, stc_firmware, 8192);
950 static int stc_release(struct inode *inode, struct file *file)
955 static const struct file_operations stc_fops = {
956 .owner = THIS_MODULE,
959 .release = stc_release,
963 static u32 functionality(struct i2c_adapter *adapter)
970 static int alps_tdmb7_tuner_set_params(struct dvb_frontend *fe)
972 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
973 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
975 struct i2c_msg msg = {.addr=0x61, .flags=0, .buf=data, .len=sizeof(data) };
978 div = (p->frequency + 36166667) / 166667;
980 data[0] = (div >> 8) & 0x7f;
981 data[1] = div & 0xff;
982 data[2] = ((div >> 10) & 0x60) | 0x85;
983 data[3] = p->frequency < 592000000 ? 0x40 : 0x80;
985 if (fe->ops.i2c_gate_ctrl)
986 fe->ops.i2c_gate_ctrl(fe, 1);
987 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1) return -EIO;
991 static struct cx22700_config alps_tdmb7_config = {
992 .demod_address = 0x43,
999 static int philips_tdm1316l_tuner_init(struct dvb_frontend* fe)
1001 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1002 static u8 td1316_init[] = { 0x0b, 0xf5, 0x85, 0xab };
1003 static u8 disable_mc44BC374c[] = { 0x1d, 0x74, 0xa0, 0x68 };
1004 struct i2c_msg tuner_msg = { .addr=0x60, .flags=0, .buf=td1316_init, .len=sizeof(td1316_init) };
1006 // setup PLL configuration
1007 if (fe->ops.i2c_gate_ctrl)
1008 fe->ops.i2c_gate_ctrl(fe, 1);
1009 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) return -EIO;
1012 // disable the mc44BC374c (do not check for errors)
1013 tuner_msg.addr = 0x65;
1014 tuner_msg.buf = disable_mc44BC374c;
1015 tuner_msg.len = sizeof(disable_mc44BC374c);
1016 if (fe->ops.i2c_gate_ctrl)
1017 fe->ops.i2c_gate_ctrl(fe, 1);
1018 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1019 i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1);
1025 static int philips_tdm1316l_tuner_set_params(struct dvb_frontend *fe)
1027 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1028 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1030 struct i2c_msg tuner_msg = {.addr=0x60, .flags=0, .buf=tuner_buf, .len=sizeof(tuner_buf) };
1031 int tuner_frequency = 0;
1032 u8 band, cp, filter;
1034 // determine charge pump
1035 tuner_frequency = p->frequency + 36130000;
1036 if (tuner_frequency < 87000000) return -EINVAL;
1037 else if (tuner_frequency < 130000000) cp = 3;
1038 else if (tuner_frequency < 160000000) cp = 5;
1039 else if (tuner_frequency < 200000000) cp = 6;
1040 else if (tuner_frequency < 290000000) cp = 3;
1041 else if (tuner_frequency < 420000000) cp = 5;
1042 else if (tuner_frequency < 480000000) cp = 6;
1043 else if (tuner_frequency < 620000000) cp = 3;
1044 else if (tuner_frequency < 830000000) cp = 5;
1045 else if (tuner_frequency < 895000000) cp = 7;
1046 else return -EINVAL;
1049 if (p->frequency < 49000000)
1051 else if (p->frequency < 159000000)
1053 else if (p->frequency < 444000000)
1055 else if (p->frequency < 861000000)
1057 else return -EINVAL;
1060 switch (p->bandwidth_hz) {
1062 tda1004x_writereg(fe, 0x0C, 0);
1067 tda1004x_writereg(fe, 0x0C, 0);
1072 tda1004x_writereg(fe, 0x0C, 0xFF);
1080 // calculate divisor
1081 // ((36130000+((1000000/6)/2)) + Finput)/(1000000/6)
1082 tuner_frequency = (((p->frequency / 1000) * 6) + 217280) / 1000;
1084 // setup tuner buffer
1085 tuner_buf[0] = tuner_frequency >> 8;
1086 tuner_buf[1] = tuner_frequency & 0xff;
1087 tuner_buf[2] = 0xca;
1088 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1090 if (fe->ops.i2c_gate_ctrl)
1091 fe->ops.i2c_gate_ctrl(fe, 1);
1092 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1)
1099 static int philips_tdm1316l_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1101 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1103 return reject_firmware(fw, name, &ttusb->dev->dev);
1106 static struct tda1004x_config philips_tdm1316l_config = {
1108 .demod_address = 0x8,
1111 .request_firmware = philips_tdm1316l_request_firmware,
1114 static u8 alps_bsbe1_inittab[] = {
1118 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1119 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1120 0x06, 0x40, /* DAC not used, set to high impendance mode */
1121 0x07, 0x00, /* DAC LSB */
1122 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1123 0x09, 0x00, /* FIFO */
1124 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1125 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1126 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1127 0x10, 0x3f, // AGC2 0x3d
1130 0x15, 0xc9, // lock detector threshold
1141 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1142 0x29, 0x1e, // 1/2 threshold
1143 0x2a, 0x14, // 2/3 threshold
1144 0x2b, 0x0f, // 3/4 threshold
1145 0x2c, 0x09, // 5/6 threshold
1146 0x2d, 0x05, // 7/8 threshold
1148 0x31, 0x1f, // test all FECs
1149 0x32, 0x19, // viterbi and synchro search
1150 0x33, 0xfc, // rs control
1151 0x34, 0x93, // error control
1156 static u8 alps_bsru6_inittab[] = {
1160 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
1161 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
1162 0x06, 0x40, /* DAC not used, set to high impendance mode */
1163 0x07, 0x00, /* DAC LSB */
1164 0x08, 0x40, /* DiSEqC off, LNB power on OP2/LOCK pin on */
1165 0x09, 0x00, /* FIFO */
1166 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
1167 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
1168 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
1169 0x10, 0x3f, // AGC2 0x3d
1172 0x15, 0xc9, // lock detector threshold
1183 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
1184 0x29, 0x1e, // 1/2 threshold
1185 0x2a, 0x14, // 2/3 threshold
1186 0x2b, 0x0f, // 3/4 threshold
1187 0x2c, 0x09, // 5/6 threshold
1188 0x2d, 0x05, // 7/8 threshold
1190 0x31, 0x1f, // test all FECs
1191 0x32, 0x19, // viterbi and synchro search
1192 0x33, 0xfc, // rs control
1193 0x34, 0x93, // error control
1198 static int alps_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
1203 if (srate < 1500000) {
1206 } else if (srate < 3000000) {
1209 } else if (srate < 7000000) {
1212 } else if (srate < 14000000) {
1215 } else if (srate < 30000000) {
1218 } else if (srate < 45000000) {
1223 stv0299_writereg(fe, 0x13, aclk);
1224 stv0299_writereg(fe, 0x14, bclk);
1225 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
1226 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
1227 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
1232 static int philips_tsa5059_tuner_set_params(struct dvb_frontend *fe)
1234 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1235 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1238 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1240 if ((p->frequency < 950000) || (p->frequency > 2150000))
1243 div = (p->frequency + (125 - 1)) / 125; /* round correctly */
1244 buf[0] = (div >> 8) & 0x7f;
1245 buf[1] = div & 0xff;
1246 buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
1249 if (p->frequency > 1530000)
1252 /* BSBE1 wants XCE bit set */
1253 if (ttusb->revision == TTUSB_REV_2_2)
1256 if (fe->ops.i2c_gate_ctrl)
1257 fe->ops.i2c_gate_ctrl(fe, 1);
1258 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1264 static struct stv0299_config alps_stv0299_config = {
1265 .demod_address = 0x68,
1266 .inittab = alps_bsru6_inittab,
1270 .lock_output = STV0299_LOCKOUTPUT_1,
1271 .volt13_op0_op1 = STV0299_VOLT13_OP1,
1272 .min_delay_ms = 100,
1273 .set_symbol_rate = alps_stv0299_set_symbol_rate,
1276 static int ttusb_novas_grundig_29504_491_tuner_set_params(struct dvb_frontend *fe)
1278 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1279 struct ttusb* ttusb = (struct ttusb*) fe->dvb->priv;
1282 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
1284 div = p->frequency / 125;
1286 buf[0] = (div >> 8) & 0x7f;
1287 buf[1] = div & 0xff;
1291 if (fe->ops.i2c_gate_ctrl)
1292 fe->ops.i2c_gate_ctrl(fe, 1);
1293 if (i2c_transfer(&ttusb->i2c_adap, &msg, 1) != 1)
1299 static struct tda8083_config ttusb_novas_grundig_29504_491_config = {
1301 .demod_address = 0x68,
1304 static int alps_tdbe2_tuner_set_params(struct dvb_frontend *fe)
1306 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1307 struct ttusb* ttusb = fe->dvb->priv;
1310 struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
1312 div = (p->frequency + 35937500 + 31250) / 62500;
1314 data[0] = (div >> 8) & 0x7f;
1315 data[1] = div & 0xff;
1316 data[2] = 0x85 | ((div >> 10) & 0x60);
1317 data[3] = (p->frequency < 174000000 ? 0x88 : p->frequency < 470000000 ? 0x84 : 0x81);
1319 if (fe->ops.i2c_gate_ctrl)
1320 fe->ops.i2c_gate_ctrl(fe, 1);
1321 if (i2c_transfer (&ttusb->i2c_adap, &msg, 1) != 1)
1328 static struct ves1820_config alps_tdbe2_config = {
1329 .demod_address = 0x09,
1332 .selagc = VES1820_SELAGC_SIGNAMPERR,
1335 static u8 read_pwm(struct ttusb* ttusb)
1339 struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
1340 { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
1342 if ((i2c_transfer(&ttusb->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
1349 static int dvbc_philips_tdm1316l_tuner_set_params(struct dvb_frontend *fe)
1351 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1352 struct ttusb *ttusb = (struct ttusb *) fe->dvb->priv;
1354 struct i2c_msg tuner_msg = {.addr = 0x60,
1357 .len = sizeof(tuner_buf) };
1358 int tuner_frequency = 0;
1359 u8 band, cp, filter;
1361 // determine charge pump
1362 tuner_frequency = p->frequency;
1363 if (tuner_frequency < 87000000) {return -EINVAL;}
1364 else if (tuner_frequency < 130000000) {cp = 3; band = 1;}
1365 else if (tuner_frequency < 160000000) {cp = 5; band = 1;}
1366 else if (tuner_frequency < 200000000) {cp = 6; band = 1;}
1367 else if (tuner_frequency < 290000000) {cp = 3; band = 2;}
1368 else if (tuner_frequency < 420000000) {cp = 5; band = 2;}
1369 else if (tuner_frequency < 480000000) {cp = 6; band = 2;}
1370 else if (tuner_frequency < 620000000) {cp = 3; band = 4;}
1371 else if (tuner_frequency < 830000000) {cp = 5; band = 4;}
1372 else if (tuner_frequency < 895000000) {cp = 7; band = 4;}
1373 else {return -EINVAL;}
1375 // assume PLL filter should always be 8MHz for the moment.
1378 // calculate divisor
1379 // (Finput + Fif)/Fref; Fif = 36125000 Hz, Fref = 62500 Hz
1380 tuner_frequency = ((p->frequency + 36125000) / 62500);
1382 // setup tuner buffer
1383 tuner_buf[0] = tuner_frequency >> 8;
1384 tuner_buf[1] = tuner_frequency & 0xff;
1385 tuner_buf[2] = 0xc8;
1386 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
1387 tuner_buf[4] = 0x80;
1389 if (fe->ops.i2c_gate_ctrl)
1390 fe->ops.i2c_gate_ctrl(fe, 1);
1391 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1392 pr_err("dvbc_philips_tdm1316l_pll_set Error 1\n");
1398 if (fe->ops.i2c_gate_ctrl)
1399 fe->ops.i2c_gate_ctrl(fe, 1);
1400 if (i2c_transfer(&ttusb->i2c_adap, &tuner_msg, 1) != 1) {
1401 pr_err("dvbc_philips_tdm1316l_pll_set Error 2\n");
1410 static u8 dvbc_philips_tdm1316l_inittab[] = {
1512 static struct stv0297_config dvbc_philips_tdm1316l_config = {
1513 .demod_address = 0x1c,
1514 .inittab = dvbc_philips_tdm1316l_inittab,
1518 static void frontend_init(struct ttusb* ttusb)
1520 switch(le16_to_cpu(ttusb->dev->descriptor.idProduct)) {
1521 case 0x1003: // Hauppauge/TT Nova-USB-S budget (stv0299/ALPS BSRU6|BSBE1(tsa5059))
1522 // try the stv0299 based first
1523 ttusb->fe = dvb_attach(stv0299_attach, &alps_stv0299_config, &ttusb->i2c_adap);
1524 if (ttusb->fe != NULL) {
1525 ttusb->fe->ops.tuner_ops.set_params = philips_tsa5059_tuner_set_params;
1527 if(ttusb->revision == TTUSB_REV_2_2) { // ALPS BSBE1
1528 alps_stv0299_config.inittab = alps_bsbe1_inittab;
1529 dvb_attach(lnbp21_attach, ttusb->fe, &ttusb->i2c_adap, 0, 0);
1530 } else { // ALPS BSRU6
1531 ttusb->fe->ops.set_voltage = ttusb_set_voltage;
1536 // Grundig 29504-491
1537 ttusb->fe = dvb_attach(tda8083_attach, &ttusb_novas_grundig_29504_491_config, &ttusb->i2c_adap);
1538 if (ttusb->fe != NULL) {
1539 ttusb->fe->ops.tuner_ops.set_params = ttusb_novas_grundig_29504_491_tuner_set_params;
1540 ttusb->fe->ops.set_voltage = ttusb_set_voltage;
1545 case 0x1004: // Hauppauge/TT DVB-C budget (ves1820/ALPS TDBE2(sp5659))
1546 ttusb->fe = dvb_attach(ves1820_attach, &alps_tdbe2_config, &ttusb->i2c_adap, read_pwm(ttusb));
1547 if (ttusb->fe != NULL) {
1548 ttusb->fe->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
1552 ttusb->fe = dvb_attach(stv0297_attach, &dvbc_philips_tdm1316l_config, &ttusb->i2c_adap);
1553 if (ttusb->fe != NULL) {
1554 ttusb->fe->ops.tuner_ops.set_params = dvbc_philips_tdm1316l_tuner_set_params;
1559 case 0x1005: // Hauppauge/TT Nova-USB-t budget (tda10046/Philips td1316(tda6651tt) OR cx22700/ALPS TDMB7(??))
1560 // try the ALPS TDMB7 first
1561 ttusb->fe = dvb_attach(cx22700_attach, &alps_tdmb7_config, &ttusb->i2c_adap);
1562 if (ttusb->fe != NULL) {
1563 ttusb->fe->ops.tuner_ops.set_params = alps_tdmb7_tuner_set_params;
1568 ttusb->fe = dvb_attach(tda10046_attach, &philips_tdm1316l_config, &ttusb->i2c_adap);
1569 if (ttusb->fe != NULL) {
1570 ttusb->fe->ops.tuner_ops.init = philips_tdm1316l_tuner_init;
1571 ttusb->fe->ops.tuner_ops.set_params = philips_tdm1316l_tuner_set_params;
1577 if (ttusb->fe == NULL) {
1578 pr_err("no frontend driver found for device [%04x:%04x]\n",
1579 le16_to_cpu(ttusb->dev->descriptor.idVendor),
1580 le16_to_cpu(ttusb->dev->descriptor.idProduct));
1582 if (dvb_register_frontend(&ttusb->adapter, ttusb->fe)) {
1583 pr_err("Frontend registration failed!\n");
1584 dvb_frontend_detach(ttusb->fe);
1592 static const struct i2c_algorithm ttusb_dec_algo = {
1593 .master_xfer = master_xfer,
1594 .functionality = functionality,
1597 static int ttusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1599 struct usb_device *udev;
1600 struct ttusb *ttusb;
1603 dprintk("TTUSB DVB connected\n");
1605 udev = interface_to_usbdev(intf);
1607 if (intf->altsetting->desc.bInterfaceNumber != 1) return -ENODEV;
1609 if (!(ttusb = kzalloc(sizeof(struct ttusb), GFP_KERNEL)))
1614 ttusb->mux_state = 0;
1615 mutex_init(&ttusb->semi2c);
1617 mutex_lock(&ttusb->semi2c);
1619 mutex_init(&ttusb->semusb);
1621 ttusb_setup_interfaces(ttusb);
1623 result = ttusb_alloc_iso_urbs(ttusb);
1625 dprintk("ttusb_alloc_iso_urbs - failed\n");
1626 mutex_unlock(&ttusb->semi2c);
1631 if (ttusb_init_controller(ttusb))
1632 pr_err("ttusb_init_controller: error\n");
1634 mutex_unlock(&ttusb->semi2c);
1636 result = dvb_register_adapter(&ttusb->adapter,
1637 "Technotrend/Hauppauge Nova-USB",
1638 THIS_MODULE, &udev->dev, adapter_nr);
1640 ttusb_free_iso_urbs(ttusb);
1644 ttusb->adapter.priv = ttusb;
1647 memset(&ttusb->i2c_adap, 0, sizeof(struct i2c_adapter));
1648 strscpy(ttusb->i2c_adap.name, "TTUSB DEC", sizeof(ttusb->i2c_adap.name));
1650 i2c_set_adapdata(&ttusb->i2c_adap, ttusb);
1652 ttusb->i2c_adap.algo = &ttusb_dec_algo;
1653 ttusb->i2c_adap.algo_data = NULL;
1654 ttusb->i2c_adap.dev.parent = &udev->dev;
1656 result = i2c_add_adapter(&ttusb->i2c_adap);
1658 goto err_unregister_adapter;
1660 memset(&ttusb->dvb_demux, 0, sizeof(ttusb->dvb_demux));
1662 ttusb->dvb_demux.dmx.capabilities =
1663 DMX_TS_FILTERING | DMX_SECTION_FILTERING;
1664 ttusb->dvb_demux.priv = NULL;
1665 #ifdef TTUSB_HWSECTIONS
1666 ttusb->dvb_demux.filternum = TTUSB_MAXFILTER;
1668 ttusb->dvb_demux.filternum = 32;
1670 ttusb->dvb_demux.feednum = TTUSB_MAXCHANNEL;
1671 ttusb->dvb_demux.start_feed = ttusb_start_feed;
1672 ttusb->dvb_demux.stop_feed = ttusb_stop_feed;
1673 ttusb->dvb_demux.write_to_decoder = NULL;
1675 result = dvb_dmx_init(&ttusb->dvb_demux);
1677 pr_err("dvb_dmx_init failed (errno = %d)\n", result);
1679 goto err_i2c_del_adapter;
1681 //FIXME dmxdev (nur WAS?)
1682 ttusb->dmxdev.filternum = ttusb->dvb_demux.filternum;
1683 ttusb->dmxdev.demux = &ttusb->dvb_demux.dmx;
1684 ttusb->dmxdev.capabilities = 0;
1686 result = dvb_dmxdev_init(&ttusb->dmxdev, &ttusb->adapter);
1688 pr_err("dvb_dmxdev_init failed (errno = %d)\n",
1691 goto err_release_dmx;
1694 if (dvb_net_init(&ttusb->adapter, &ttusb->dvbnet, &ttusb->dvb_demux.dmx)) {
1695 pr_err("dvb_net_init failed!\n");
1697 goto err_release_dmxdev;
1700 usb_set_intfdata(intf, (void *) ttusb);
1702 frontend_init(ttusb);
1707 dvb_dmxdev_release(&ttusb->dmxdev);
1709 dvb_dmx_release(&ttusb->dvb_demux);
1710 err_i2c_del_adapter:
1711 i2c_del_adapter(&ttusb->i2c_adap);
1712 err_unregister_adapter:
1713 dvb_unregister_adapter (&ttusb->adapter);
1714 ttusb_free_iso_urbs(ttusb);
1719 static void ttusb_disconnect(struct usb_interface *intf)
1721 struct ttusb *ttusb = usb_get_intfdata(intf);
1723 usb_set_intfdata(intf, NULL);
1725 ttusb->disconnecting = 1;
1727 ttusb_stop_iso_xfer(ttusb);
1729 ttusb->dvb_demux.dmx.close(&ttusb->dvb_demux.dmx);
1730 dvb_net_release(&ttusb->dvbnet);
1731 dvb_dmxdev_release(&ttusb->dmxdev);
1732 dvb_dmx_release(&ttusb->dvb_demux);
1733 if (ttusb->fe != NULL) {
1734 dvb_unregister_frontend(ttusb->fe);
1735 dvb_frontend_detach(ttusb->fe);
1737 i2c_del_adapter(&ttusb->i2c_adap);
1738 dvb_unregister_adapter(&ttusb->adapter);
1740 ttusb_free_iso_urbs(ttusb);
1744 dprintk("TTUSB DVB disconnected\n");
1747 static const struct usb_device_id ttusb_table[] = {
1748 {USB_DEVICE(0xb48, 0x1003)},
1749 {USB_DEVICE(0xb48, 0x1004)},
1750 {USB_DEVICE(0xb48, 0x1005)},
1754 MODULE_DEVICE_TABLE(usb, ttusb_table);
1756 static struct usb_driver ttusb_driver = {
1758 .probe = ttusb_probe,
1759 .disconnect = ttusb_disconnect,
1760 .id_table = ttusb_table,
1763 module_usb_driver(ttusb_driver);
1765 MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1766 MODULE_DESCRIPTION("TTUSB DVB Driver");
1767 MODULE_LICENSE("GPL");