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 <asm/semaphore.h>
22 #include "dvb_frontend.h"
24 #include "dvb_demux.h"
27 #include <linux/dvb/frontend.h>
28 #include <linux/dvb/dmx.h>
29 #include <linux/pci.h>
31 #include "dvb_functions.h"
35 the DSP supports filtering in hardware, however, since the "muxstream"
36 is a bit braindead (no matching channel masks or no matching filter mask),
37 we won't support this - yet. it doesn't event support negative filters,
38 so the best way is maybe to keep TTUSB_HWSECTIONS undef'd and just
39 parse TS data. USB bandwith will be a problem when having large
40 datastreams, especially for dvb-net, but hey, that's not my problem.
42 TTUSB_DISEQC, TTUSB_TONE:
43 let the STC do the diseqc/tone stuff. this isn't supported at least with
44 my TTUSB, so let it undef'd unless you want to implement another
45 frontend. never tested.
48 define it to > 3 for really hardcore debugging. you probably don't want
49 this unless the device doesn't load at all. > 2 for bandwidth statistics.
54 #define dprintk(x...) do { if (debug) printk(KERN_DEBUG x); } while (0)
56 #define ISO_BUF_COUNT 4
57 #define FRAMES_PER_ISO_BUF 4
58 #define ISO_FRAME_SIZE 912
59 #define TTUSB_MAXCHANNEL 32
60 #ifdef TTUSB_HWSECTIONS
61 #define TTUSB_MAXFILTER 16 /* ??? */
64 #define TTUSB_BUDGET_NAME "ttusb_stc_fw"
67 * since we're casting (struct ttusb*) <-> (struct dvb_demux*) around
68 * the dvb_demux field must be the first in struct!!
71 struct dvb_demux dvb_demux
;
73 struct dvb_net dvbnet
;
75 /* our semaphore, for channel allocation/deallocation */
77 /* and one for USB access. */
78 struct semaphore semusb
;
80 struct dvb_adapter
*adapter
;
81 struct usb_device
*dev
;
86 unsigned int bulk_out_pipe
;
87 unsigned int bulk_in_pipe
;
88 unsigned int isoc_in_pipe
;
91 dma_addr_t iso_dma_handle
;
93 struct urb
*iso_urb
[ISO_BUF_COUNT
];
95 int running_feed_count
;
99 u8 c
; /* transaction counter, wraps around... */
100 fe_sec_tone_mode_t tone
;
101 fe_sec_voltage_t voltage
;
103 int mux_state
; // 0..2 - MuxSyncWord, 3 - nMuxPacks, 4 - muxpack
106 int muxpack_ptr
, muxpack_len
;
110 int cc
; /* MuxCounter - will increment on EVERY MUX PACKET */
111 /* (including stuffing. yes. really.) */
116 struct ttusb_channel
{
118 struct dvb_demux_feed
*dvbdmxfeed
;
123 int type
; /* 1 - TS, 2 - Filter */
124 #ifdef TTUSB_HWSECTIONS
125 int filterstate
[TTUSB_MAXFILTER
]; /* 0: not busy, 1: busy */
127 } channel
[TTUSB_MAXCHANNEL
];
129 devfs_handle_t stc_devfs_handle
;
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
, HZ
);
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
, HZ
);
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 ttusb_i2c_xfer(struct dvb_i2c_bus
*i2c
, const struct i2c_msg msg
[],
248 struct ttusb
*ttusb
= i2c
->data
;
252 if (down_interruptible(&ttusb
->sem
) < 0)
256 u8 addr
, snd_len
, rcv_len
, *snd_buf
, *rcv_buf
;
259 if (num
> i
+ 1 && (msg
[i
+ 1].flags
& I2C_M_RD
)) {
261 snd_buf
= msg
[i
].buf
;
262 snd_len
= msg
[i
].len
;
263 rcv_buf
= msg
[i
+ 1].buf
;
264 rcv_len
= msg
[i
+ 1].len
;
268 snd_buf
= msg
[i
].buf
;
269 snd_len
= msg
[i
].len
;
275 err
= ttusb_i2c_msg(ttusb
, addr
,
276 snd_buf
, snd_len
, rcv_buf
, rcv_len
);
279 dprintk("%s: i == %i\n", __FUNCTION__
, i
);
290 #include "dvb-ttusb-dspbootcode.h"
292 static int ttusb_boot_dsp(struct ttusb
*ttusb
)
302 /* upload dsp code in 32 byte steps (36 didn't work for me ...) */
303 /* 32 is max packet size, no messages should be splitted. */
304 for (i
= 0; i
< sizeof(dsp_bootcode
); i
+= 28) {
305 memcpy(&b
[4], &dsp_bootcode
[i
], 28);
309 err
= ttusb_cmd(ttusb
, b
, 32, 0);
319 err
= ttusb_cmd(ttusb
, b
, 4, 0);
328 err
= ttusb_cmd(ttusb
, b
, 4, 0);
332 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
339 static int ttusb_set_channel(struct ttusb
*ttusb
, int chan_id
, int filter_type
,
344 u8 b
[] = { 0xaa, ++ttusb
->c
, 0x22, 4, chan_id
, filter_type
,
345 (pid
>> 8) & 0xff, pid
& 0xff
348 err
= ttusb_cmd(ttusb
, b
, sizeof(b
), 0);
352 static int ttusb_del_channel(struct ttusb
*ttusb
, int channel_id
)
356 u8 b
[] = { 0xaa, ++ttusb
->c
, 0x23, 1, channel_id
};
358 err
= ttusb_cmd(ttusb
, b
, sizeof(b
), 0);
362 #ifdef TTUSB_HWSECTIONS
363 static int ttusb_set_filter(struct ttusb
*ttusb
, int filter_id
,
364 int associated_chan
, u8 filter
[8], u8 mask
[8])
368 u8 b
[] = { 0xaa, 0, 0x24, 0x1a, filter_id
, associated_chan
,
369 filter
[0], filter
[1], filter
[2], filter
[3],
370 filter
[4], filter
[5], filter
[6], filter
[7],
371 filter
[8], filter
[9], filter
[10], filter
[11],
372 mask
[0], mask
[1], mask
[2], mask
[3],
373 mask
[4], mask
[5], mask
[6], mask
[7],
374 mask
[8], mask
[9], mask
[10], mask
[11]
377 err
= ttusb_cmd(ttusb
, b
, sizeof(b
), 0);
381 static int ttusb_del_filter(struct ttusb
*ttusb
, int filter_id
)
385 u8 b
[] = { 0xaa, ++ttusb
->c
, 0x25, 1, filter_id
};
387 err
= ttusb_cmd(ttusb
, b
, sizeof(b
), 0);
392 static int ttusb_init_controller(struct ttusb
*ttusb
)
394 u8 b0
[] = { 0xaa, ++ttusb
->c
, 0x15, 1, 0 };
395 u8 b1
[] = { 0xaa, ++ttusb
->c
, 0x15, 1, 1 };
396 u8 b2
[] = { 0xaa, ++ttusb
->c
, 0x32, 1, 0 };
397 /* i2c write read: 5 bytes, addr 0x10, 0x02 bytes write, 1 bytes read. */
399 { 0xaa, ++ttusb
->c
, 0x31, 5, 0x10, 0x02, 0x01, 0x00, 0x1e };
401 { 0x55, ttusb
->c
, 0x31, 4, 0x10, 0x02, 0x01, 0x00, 0x1e };
403 u8 get_version
[] = { 0xaa, ++ttusb
->c
, 0x17, 5, 0, 0, 0, 0, 0 };
404 u8 get_dsp_version
[0x20] =
405 { 0xaa, ++ttusb
->c
, 0x26, 28, 0, 0, 0, 0, 0 };
409 if ((err
= ttusb_cmd(ttusb
, b0
, sizeof(b0
), 0)))
412 /* reset board (again?) */
413 if ((err
= ttusb_cmd(ttusb
, b1
, sizeof(b1
), 0)))
416 ttusb_boot_dsp(ttusb
);
418 /* set i2c bit rate */
419 if ((err
= ttusb_cmd(ttusb
, b2
, sizeof(b2
), 0)))
422 if ((err
= ttusb_cmd(ttusb
, b3
, sizeof(b3
), 1)))
425 err
= ttusb_result(ttusb
, b4
, sizeof(b4
));
427 if ((err
= ttusb_cmd(ttusb
, get_version
, sizeof(get_version
), 1)))
430 if ((err
= ttusb_result(ttusb
, get_version
, sizeof(get_version
))))
433 dprintk("%s: stc-version: %c%c%c%c%c\n", __FUNCTION__
,
434 get_version
[4], get_version
[5], get_version
[6],
435 get_version
[7], get_version
[8]);
437 if (memcmp(get_version
+ 4, "V 0.0", 5) &&
438 memcmp(get_version
+ 4, "V 1.1", 5) &&
439 memcmp(get_version
+ 4, "V 2.1", 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]);
447 ttusb_cmd(ttusb
, get_dsp_version
, sizeof(get_dsp_version
), 1);
452 ttusb_result(ttusb
, get_dsp_version
, sizeof(get_dsp_version
));
455 printk("%s: dsp-version: %c%c%c\n", __FUNCTION__
,
456 get_dsp_version
[4], get_dsp_version
[5], get_dsp_version
[6]);
461 static int ttusb_send_diseqc(struct ttusb
*ttusb
,
462 const struct dvb_diseqc_master_cmd
*cmd
)
464 u8 b
[12] = { 0xaa, ++ttusb
->c
, 0x18 };
468 b
[3] = 4 + 2 + cmd
->msg_len
;
469 b
[4] = 0xFF; /* send diseqc master, not burst */
472 memcpy(b
+ 5, cmd
->msg
, cmd
->msg_len
);
475 if ((err
= ttusb_cmd(ttusb
, b
, 4 + b
[3], 0))) {
476 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
484 static int ttusb_update_lnb(struct ttusb
*ttusb
)
486 u8 b
[] = { 0xaa, ++ttusb
->c
, 0x16, 5, /*power: */ 1,
487 ttusb
->voltage
== SEC_VOLTAGE_18
? 0 : 1,
488 ttusb
->tone
== SEC_TONE_ON
? 1 : 0, 1, 1
493 if ((err
= ttusb_cmd(ttusb
, b
, sizeof(b
), 0))) {
494 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
501 static int ttusb_set_voltage(struct ttusb
*ttusb
, fe_sec_voltage_t voltage
)
503 ttusb
->voltage
= voltage
;
504 return ttusb_update_lnb(ttusb
);
508 static int ttusb_set_tone(struct ttusb
*ttusb
, fe_sec_tone_mode_t tone
)
511 return ttusb_update_lnb(ttusb
);
515 static int ttusb_lnb_ioctl(struct dvb_frontend
*fe
, unsigned int cmd
, void *arg
)
517 struct ttusb
*ttusb
= fe
->i2c
->data
;
521 return ttusb_set_voltage(ttusb
, (fe_sec_voltage_t
) arg
);
524 return ttusb_set_tone(ttusb
, (fe_sec_tone_mode_t
) arg
);
527 case FE_DISEQC_SEND_MASTER_CMD
:
528 return ttusb_send_diseqc(ttusb
,
529 (struct dvb_diseqc_master_cmd
*)
538 static void ttusb_set_led_freq(struct ttusb
*ttusb
, u8 freq
)
540 u8 b
[] = { 0xaa, ++ttusb
->c
, 0x19, 1, freq
};
543 err
= ttusb_cmd(ttusb
, b
, sizeof(b
), 0);
545 dprintk("%s: usb_bulk_msg() failed, return value %i!\n",
551 /*****************************************************************************/
553 #ifdef TTUSB_HWSECTIONS
554 static void ttusb_handle_ts_data(struct ttusb_channel
*channel
,
555 const u8
* data
, int len
);
556 static void ttusb_handle_sec_data(struct ttusb_channel
*channel
,
557 const u8
* data
, int len
);
560 int numpkt
= 0, lastj
, numts
, numstuff
, numsec
, numinvalid
;
562 static void ttusb_process_muxpack(struct ttusb
*ttusb
, const u8
* muxpack
,
567 for (i
= 0; i
< len
; i
+= 2)
568 csum
^= le16_to_cpup((u16
*) (muxpack
+ i
));
570 printk("%s: muxpack with incorrect checksum, ignoring\n",
576 cc
= (muxpack
[len
- 4] << 8) | muxpack
[len
- 3];
578 if ((cc
!= ttusb
->cc
) && (ttusb
->cc
!= -1))
579 printk("%s: cc discontinuity (%d frames missing)\n",
580 __FUNCTION__
, (cc
- ttusb
->cc
) & 0x7FFF);
581 ttusb
->cc
= (cc
+ 1) & 0x7FFF;
582 if (muxpack
[0] & 0x80) {
583 #ifdef TTUSB_HWSECTIONS
585 int pusi
= muxpack
[0] & 0x40;
586 int channel
= muxpack
[0] & 0x1F;
587 int payload
= muxpack
[1];
588 const u8
*data
= muxpack
+ 2;
589 /* check offset flag */
590 if (muxpack
[0] & 0x20)
593 ttusb_handle_sec_data(ttusb
->channel
+ channel
, data
,
597 if ((!!(ttusb
->muxpack
[0] & 0x20)) ^
598 !!(ttusb
->muxpack
[1] & 1))
601 printk("cc: %04x\n", (data
[0] << 8) | data
[1]);
604 } else if (muxpack
[0] == 0x47) {
605 #ifdef TTUSB_HWSECTIONS
606 /* we have TS data here! */
607 int pid
= ((muxpack
[1] & 0x0F) << 8) | muxpack
[2];
609 for (channel
= 0; channel
< TTUSB_MAXCHANNEL
; ++channel
)
610 if (ttusb
->channel
[channel
].active
611 && (pid
== ttusb
->channel
[channel
].pid
))
612 ttusb_handle_ts_data(ttusb
->channel
+
617 dvb_dmx_swfilter_packets(&ttusb
->dvb_demux
, muxpack
, 1);
618 } else if (muxpack
[0] != 0) {
620 printk("illegal muxpack type %02x\n", muxpack
[0]);
625 static void ttusb_process_frame(struct ttusb
*ttusb
, u8
* data
, int len
)
630 printk("%s: too much work\n", __FUNCTION__
);
634 switch (ttusb
->mux_state
) {
642 ttusb
->mux_state
= 0;
645 printk("%02x ", data
[-1]);
648 printk("%s: lost sync.\n",
658 ttusb
->mux_npacks
= *data
++;
660 ttusb
->muxpack_ptr
= 0;
661 /* maximum bytes, until we know the length */
662 ttusb
->muxpack_len
= 2;
669 (ttusb
->muxpack_len
-
674 memcpy(ttusb
->muxpack
+ ttusb
->muxpack_ptr
,
676 ttusb
->muxpack_ptr
+= avail
;
677 if (ttusb
->muxpack_ptr
> 264)
681 /* determine length */
682 if (ttusb
->muxpack_ptr
== 2) {
683 if (ttusb
->muxpack
[0] & 0x80) {
685 ttusb
->muxpack
[1] + 2;
692 muxpack
[0] & 0x20)) ^
697 ttusb
->muxpack_len
+= 4;
698 } else if (ttusb
->muxpack
[0] ==
702 else if (ttusb
->muxpack
[0] == 0x00)
704 ttusb
->muxpack
[1] + 2 +
708 ("%s: invalid state: first byte is %x\n",
711 ttusb
->mux_state
= 0;
716 * if length is valid and we reached the end:
719 if ((ttusb
->muxpack_ptr
>= 2) &&
720 (ttusb
->muxpack_ptr
==
721 ttusb
->muxpack_len
)) {
722 ttusb_process_muxpack(ttusb
,
727 ttusb
->muxpack_ptr
= 0;
728 /* maximum bytes, until we know the length */
729 ttusb
->muxpack_len
= 2;
733 * return to search-sync state
735 if (!ttusb
->mux_npacks
--) {
736 ttusb
->mux_state
= 0;
749 static void ttusb_iso_irq(struct urb
*urb
, struct pt_regs
*ptregs
)
751 struct ttusb
*ttusb
= urb
->context
;
753 if (!ttusb
->iso_streaming
)
757 printk("%s: status %d, errcount == %d, length == %i\n",
759 urb
->status
, urb
->error_count
, urb
->actual_length
);
764 for (i
= 0; i
< urb
->number_of_packets
; ++i
) {
765 struct usb_iso_packet_descriptor
*d
;
769 if ((jiffies
- lastj
) >= HZ
) {
772 ("frames/s: %d (ts: %d, stuff %d, sec: %d, invalid: %d, all: %d)\n",
773 numpkt
* HZ
/ (jiffies
- lastj
),
774 numts
, numstuff
, numsec
, numinvalid
,
775 numts
+ numstuff
+ numsec
+
778 numts
= numstuff
= numsec
= numinvalid
= 0;
782 d
= &urb
->iso_frame_desc
[i
];
783 data
= urb
->transfer_buffer
+ d
->offset
;
784 len
= d
->actual_length
;
785 d
->actual_length
= 0;
787 ttusb_process_frame(ttusb
, data
, len
);
790 usb_submit_urb(urb
, GFP_KERNEL
);
793 static void ttusb_free_iso_urbs(struct ttusb
*ttusb
)
797 for (i
= 0; i
< ISO_BUF_COUNT
; i
++)
798 if (ttusb
->iso_urb
[i
])
799 usb_free_urb(ttusb
->iso_urb
[i
]);
801 pci_free_consistent(NULL
,
802 ISO_FRAME_SIZE
* FRAMES_PER_ISO_BUF
*
803 ISO_BUF_COUNT
, ttusb
->iso_buffer
,
804 ttusb
->iso_dma_handle
);
807 static int ttusb_alloc_iso_urbs(struct ttusb
*ttusb
)
811 ttusb
->iso_buffer
= pci_alloc_consistent(NULL
,
815 &ttusb
->iso_dma_handle
);
817 memset(ttusb
->iso_buffer
, 0,
818 ISO_FRAME_SIZE
* FRAMES_PER_ISO_BUF
* ISO_BUF_COUNT
);
820 for (i
= 0; i
< ISO_BUF_COUNT
; i
++) {
825 usb_alloc_urb(FRAMES_PER_ISO_BUF
, GFP_KERNEL
))) {
826 ttusb_free_iso_urbs(ttusb
);
830 ttusb
->iso_urb
[i
] = urb
;
836 static void ttusb_stop_iso_xfer(struct ttusb
*ttusb
)
840 for (i
= 0; i
< ISO_BUF_COUNT
; i
++)
841 usb_unlink_urb(ttusb
->iso_urb
[i
]);
843 ttusb
->iso_streaming
= 0;
846 static int ttusb_start_iso_xfer(struct ttusb
*ttusb
)
848 int i
, j
, err
, buffer_offset
= 0;
850 if (ttusb
->iso_streaming
) {
851 printk("%s: iso xfer already running!\n", __FUNCTION__
);
857 ttusb
->mux_state
= 0;
859 for (i
= 0; i
< ISO_BUF_COUNT
; i
++) {
860 int frame_offset
= 0;
861 struct urb
*urb
= ttusb
->iso_urb
[i
];
863 urb
->dev
= ttusb
->dev
;
864 urb
->context
= ttusb
;
865 urb
->complete
= ttusb_iso_irq
;
866 urb
->pipe
= ttusb
->isoc_in_pipe
;
867 urb
->transfer_flags
= URB_ISO_ASAP
;
869 urb
->number_of_packets
= FRAMES_PER_ISO_BUF
;
870 urb
->transfer_buffer_length
=
871 ISO_FRAME_SIZE
* FRAMES_PER_ISO_BUF
;
872 urb
->transfer_buffer
= ttusb
->iso_buffer
+ buffer_offset
;
873 buffer_offset
+= ISO_FRAME_SIZE
* FRAMES_PER_ISO_BUF
;
875 for (j
= 0; j
< FRAMES_PER_ISO_BUF
; j
++) {
876 urb
->iso_frame_desc
[j
].offset
= frame_offset
;
877 urb
->iso_frame_desc
[j
].length
= ISO_FRAME_SIZE
;
878 frame_offset
+= ISO_FRAME_SIZE
;
882 for (i
= 0; i
< ISO_BUF_COUNT
; i
++) {
883 if ((err
= usb_submit_urb(ttusb
->iso_urb
[i
], GFP_KERNEL
))) {
884 ttusb_stop_iso_xfer(ttusb
);
886 ("%s: failed urb submission (%i: err = %i)!\n",
887 __FUNCTION__
, i
, err
);
892 ttusb
->iso_streaming
= 1;
897 #ifdef TTUSB_HWSECTIONS
898 static void ttusb_handle_ts_data(struct ttusb_channel
*channel
, const u8
* data
,
901 struct dvb_demux_feed
*dvbdmxfeed
= channel
->dvbdmxfeed
;
903 dvbdmxfeed
->cb
.ts(data
, len
, 0, 0, &dvbdmxfeed
->feed
.ts
, 0);
906 static void ttusb_handle_sec_data(struct ttusb_channel
*channel
, const u8
* data
,
909 // struct dvb_demux_feed *dvbdmxfeed = channel->dvbdmxfeed;
910 #error TODO: handle ugly stuff
911 // dvbdmxfeed->cb.sec(data, len, 0, 0, &dvbdmxfeed->feed.sec, 0);
915 static struct ttusb_channel
*ttusb_channel_allocate(struct ttusb
*ttusb
)
919 if (down_interruptible(&ttusb
->sem
))
923 for (i
= 0; i
< TTUSB_MAXCHANNEL
; ++i
) {
924 if (!ttusb
->channel
[i
].active
) {
925 ttusb
->channel
[i
].active
= 1;
927 return ttusb
->channel
+ i
;
936 static int ttusb_start_feed(struct dvb_demux_feed
*dvbdmxfeed
)
938 struct ttusb
*ttusb
= (struct ttusb
*) dvbdmxfeed
->demux
;
939 struct ttusb_channel
*channel
;
941 dprintk("ttusb_start_feed\n");
943 switch (dvbdmxfeed
->type
) {
952 if (dvbdmxfeed
->type
== DMX_TYPE_TS
) {
953 switch (dvbdmxfeed
->pes_type
) {
954 case DMX_TS_PES_VIDEO
:
955 case DMX_TS_PES_AUDIO
:
956 case DMX_TS_PES_TELETEXT
:
958 case DMX_TS_PES_OTHER
:
959 channel
= ttusb_channel_allocate(ttusb
);
965 channel
= ttusb_channel_allocate(ttusb
);
971 dvbdmxfeed
->priv
= channel
;
972 channel
->dvbdmxfeed
= dvbdmxfeed
;
974 channel
->pid
= dvbdmxfeed
->pid
;
976 #ifdef TTUSB_HWSECTIONS
977 if (dvbdmxfeed
->type
== DMX_TYPE_TS
) {
979 } else if (dvbdmxfeed
->type
== DMX_TYPE_SEC
) {
981 #error TODO: allocate filters
987 ttusb_set_channel(ttusb
, channel
->id
, channel
->type
, channel
->pid
);
989 if (0 == ttusb
->running_feed_count
++)
990 ttusb_start_iso_xfer(ttusb
);
995 static int ttusb_stop_feed(struct dvb_demux_feed
*dvbdmxfeed
)
997 struct ttusb_channel
*channel
=
998 (struct ttusb_channel
*) dvbdmxfeed
->priv
;
999 struct ttusb
*ttusb
= (struct ttusb
*) dvbdmxfeed
->demux
;
1001 ttusb_del_channel(channel
->ttusb
, channel
->id
);
1003 if (--ttusb
->running_feed_count
== 0)
1004 ttusb_stop_iso_xfer(ttusb
);
1006 channel
->active
= 0;
1011 static int ttusb_setup_interfaces(struct ttusb
*ttusb
)
1013 usb_set_interface(ttusb
->dev
, 1, 1);
1015 ttusb
->bulk_out_pipe
= usb_sndbulkpipe(ttusb
->dev
, 1);
1016 ttusb
->bulk_in_pipe
= usb_rcvbulkpipe(ttusb
->dev
, 1);
1017 ttusb
->isoc_in_pipe
= usb_rcvisocpipe(ttusb
->dev
, 2);
1023 static u8 stc_firmware
[8192];
1025 static int stc_open(struct inode
*inode
, struct file
*file
)
1027 struct ttusb
*ttusb
= file
->private_data
;
1030 for (addr
= 0; addr
< 8192; addr
+= 16) {
1031 u8 snd_buf
[2] = { addr
>> 8, addr
& 0xFF };
1032 ttusb_i2c_msg(ttusb
, 0x50, snd_buf
, 2, stc_firmware
+ addr
,
1039 static ssize_t
stc_read(struct file
*file
, char *buf
, size_t count
,
1044 if ((tc
+ *offset
) > 8192)
1045 tc
= 8192 - *offset
;
1050 if (copy_to_user(buf
, stc_firmware
+ *offset
, tc
))
1058 static int stc_release(struct inode
*inode
, struct file
*file
)
1063 static struct file_operations stc_fops
= {
1064 .owner
= THIS_MODULE
,
1067 .release
= stc_release
,
1071 static int ttusb_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1073 struct usb_device
*udev
;
1074 struct ttusb
*ttusb
;
1075 int result
, channel
;
1077 dprintk("%s: TTUSB DVB connected\n", __FUNCTION__
);
1079 udev
= interface_to_usbdev(intf
);
1081 if (intf
->altsetting
->desc
.bInterfaceNumber
!= 1) return -ENODEV
;
1083 if (!(ttusb
= kmalloc(sizeof(struct ttusb
), GFP_KERNEL
)))
1086 memset(ttusb
, 0, sizeof(struct ttusb
));
1088 for (channel
= 0; channel
< TTUSB_MAXCHANNEL
; ++channel
) {
1089 ttusb
->channel
[channel
].id
= channel
;
1090 ttusb
->channel
[channel
].ttusb
= ttusb
;
1095 ttusb
->mux_state
= 0;
1096 sema_init(&ttusb
->sem
, 0);
1097 sema_init(&ttusb
->semusb
, 1);
1099 ttusb_setup_interfaces(ttusb
);
1101 ttusb_alloc_iso_urbs(ttusb
);
1102 if (ttusb_init_controller(ttusb
))
1103 printk("ttusb_init_controller: error\n");
1107 dvb_register_adapter(&ttusb
->adapter
, "Technotrend/Hauppauge Nova-USB", THIS_MODULE
);
1109 dvb_register_i2c_bus(ttusb_i2c_xfer
, ttusb
, ttusb
->adapter
, 0);
1110 dvb_add_frontend_ioctls(ttusb
->adapter
, ttusb_lnb_ioctl
, NULL
,
1113 memset(&ttusb
->dvb_demux
, 0, sizeof(ttusb
->dvb_demux
));
1115 ttusb
->dvb_demux
.dmx
.capabilities
=
1116 DMX_TS_FILTERING
| DMX_SECTION_FILTERING
;
1117 ttusb
->dvb_demux
.priv
= 0;
1118 #ifdef TTUSB_HWSECTIONS
1119 ttusb
->dvb_demux
.filternum
= TTUSB_MAXFILTER
;
1121 ttusb
->dvb_demux
.filternum
= 32;
1123 ttusb
->dvb_demux
.feednum
= TTUSB_MAXCHANNEL
;
1124 ttusb
->dvb_demux
.start_feed
= ttusb_start_feed
;
1125 ttusb
->dvb_demux
.stop_feed
= ttusb_stop_feed
;
1126 ttusb
->dvb_demux
.write_to_decoder
= 0;
1128 if ((result
= dvb_dmx_init(&ttusb
->dvb_demux
)) < 0) {
1129 printk("ttusb_dvb: dvb_dmx_init failed (errno = %d)\n",
1133 //FIXME dmxdev (nur WAS?)
1134 ttusb
->dmxdev
.filternum
= ttusb
->dvb_demux
.filternum
;
1135 ttusb
->dmxdev
.demux
= &ttusb
->dvb_demux
.dmx
;
1136 ttusb
->dmxdev
.capabilities
= 0;
1138 if ((result
= dvb_dmxdev_init(&ttusb
->dmxdev
, ttusb
->adapter
)) < 0) {
1139 printk("ttusb_dvb: dvb_dmxdev_init failed (errno = %d)\n",
1141 dvb_dmx_release(&ttusb
->dvb_demux
);
1146 (ttusb
->adapter
, &ttusb
->dvbnet
, &ttusb
->dvb_demux
.dmx
)) {
1147 printk("ttusb_dvb: dvb_net_init failed!\n");
1152 ttusb
->stc_devfs_handle
=
1153 devfs_register(ttusb
->adapter
->devfs_handle
, TTUSB_BUDGET_NAME
,
1154 DEVFS_FL_DEFAULT
, 0, 192,
1155 S_IFCHR
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
1156 | S_IROTH
| S_IWOTH
, &stc_fops
, ttusb
);
1159 usb_set_intfdata(intf
, (void *) ttusb
);
1164 static void ttusb_disconnect(struct usb_interface
*intf
)
1166 struct ttusb
*ttusb
= usb_get_intfdata(intf
);
1168 usb_set_intfdata(intf
, NULL
);
1170 ttusb
->disconnecting
= 1;
1172 ttusb_stop_iso_xfer(ttusb
);
1174 ttusb
->dvb_demux
.dmx
.close(&ttusb
->dvb_demux
.dmx
);
1175 dvb_net_release(&ttusb
->dvbnet
);
1176 dvb_dmxdev_release(&ttusb
->dmxdev
);
1177 dvb_dmx_release(&ttusb
->dvb_demux
);
1179 dvb_unregister_i2c_bus(ttusb_i2c_xfer
, ttusb
->adapter
, 0);
1180 dvb_unregister_adapter(ttusb
->adapter
);
1182 ttusb_free_iso_urbs(ttusb
);
1186 dprintk("%s: TTUSB DVB disconnected\n", __FUNCTION__
);
1189 static struct usb_device_id ttusb_table
[] = {
1190 {USB_DEVICE(0xb48, 0x1003)},
1191 {USB_DEVICE(0xb48, 0x1004)}, /* to be confirmed ???? */
1192 {USB_DEVICE(0xb48, 0x1005)},
1196 MODULE_DEVICE_TABLE(usb
, ttusb_table
);
1198 static struct usb_driver ttusb_driver
= {
1199 .name
= "Technotrend/Hauppauge USB-Nova",
1200 .probe
= ttusb_probe
,
1201 .disconnect
= ttusb_disconnect
,
1202 .id_table
= ttusb_table
,
1205 static int __init
ttusb_init(void)
1209 if ((err
= usb_register(&ttusb_driver
)) < 0) {
1210 printk("%s: usb_register failed! Error number %d",
1218 static void __exit
ttusb_exit(void)
1220 usb_deregister(&ttusb_driver
);
1223 module_init(ttusb_init
);
1224 module_exit(ttusb_exit
);
1226 MODULE_PARM(debug
, "i");
1227 MODULE_PARM_DESC(debug
, "Debug or not");
1229 MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
1230 MODULE_DESCRIPTION("TTUSB DVB Driver");
1231 MODULE_LICENSE("GPL");