2 * TerraTec Cinergy T²/qanu USB2 DVB-T adapter.
4 * Copyright (C) 2004 Daniel Mack <daniel@qanu.de> and
5 * Holger Waechtler <holger@qanu.de>
7 * Protocol Spec published on http://qanu.de/specs/terratec_cinergyT2.pdf
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/config.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/usb.h>
30 #include <linux/pci.h>
31 #include <linux/input.h>
32 #include <linux/dvb/frontend.h>
33 #include <linux/mutex.h>
36 #include "dvb_demux.h"
39 #ifdef CONFIG_DVB_CINERGYT2_TUNING
40 #define STREAM_URB_COUNT (CONFIG_DVB_CINERGYT2_STREAM_URB_COUNT)
41 #define STREAM_BUF_SIZE (CONFIG_DVB_CINERGYT2_STREAM_BUF_SIZE)
42 #define QUERY_INTERVAL (CONFIG_DVB_CINERGYT2_QUERY_INTERVAL)
43 #ifdef CONFIG_DVB_CINERGYT2_ENABLE_RC_INPUT_DEVICE
44 #define RC_QUERY_INTERVAL (CONFIG_DVB_CINERGYT2_RC_QUERY_INTERVAL)
48 #define STREAM_URB_COUNT (32)
49 #define STREAM_BUF_SIZE (512) /* bytes */
51 #define RC_QUERY_INTERVAL (50) /* milliseconds */
52 #define QUERY_INTERVAL (333) /* milliseconds */
55 #define DRIVER_NAME "TerraTec/qanu USB2.0 Highspeed DVB-T Receiver"
58 module_param_named(debug, debug, int, 0644);
59 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
61 #define dprintk(level, args...) \
63 if ((debug & level)) { \
64 printk("%s: %s(): ", KBUILD_MODNAME, \
69 enum cinergyt2_ep1_cmd {
70 CINERGYT2_EP1_PID_TABLE_RESET = 0x01,
71 CINERGYT2_EP1_PID_SETUP = 0x02,
72 CINERGYT2_EP1_CONTROL_STREAM_TRANSFER = 0x03,
73 CINERGYT2_EP1_SET_TUNER_PARAMETERS = 0x04,
74 CINERGYT2_EP1_GET_TUNER_STATUS = 0x05,
75 CINERGYT2_EP1_START_SCAN = 0x06,
76 CINERGYT2_EP1_CONTINUE_SCAN = 0x07,
77 CINERGYT2_EP1_GET_RC_EVENTS = 0x08,
78 CINERGYT2_EP1_SLEEP_MODE = 0x09
81 struct dvbt_set_parameters_msg {
87 } __attribute__((packed));
89 struct dvbt_get_status_msg {
96 uint32_t viterbi_error_rate;
97 uint32_t rs_error_rate;
98 uint32_t uncorrected_block_count;
100 uint8_t prev_lock_bits;
101 } __attribute__((packed));
103 static struct dvb_frontend_info cinergyt2_fe_info = {
106 .frequency_min = 174000000,
107 .frequency_max = 862000000,
108 .frequency_stepsize = 166667,
109 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
110 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
112 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
113 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
114 FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER | FE_CAN_MUTE_TS
118 struct dvb_demux demux;
119 struct usb_device *udev;
121 struct dvb_adapter adapter;
122 struct dvb_device *fedev;
123 struct dmxdev dmxdev;
124 struct dvb_net dvbnet;
129 struct dvbt_set_parameters_msg param;
130 struct dvbt_get_status_msg status;
131 struct work_struct query_work;
133 wait_queue_head_t poll_wq;
134 int pending_fe_events;
135 int disconnect_pending;
139 dma_addr_t streambuf_dmahandle;
140 struct urb *stream_urb [STREAM_URB_COUNT];
143 struct input_dev *rc_input_dev;
145 struct work_struct rc_query_work;
148 unsigned long last_event_jiffies;
153 CINERGYT2_RC_EVENT_TYPE_NONE = 0x00,
154 CINERGYT2_RC_EVENT_TYPE_NEC = 0x01,
155 CINERGYT2_RC_EVENT_TYPE_RC5 = 0x02
158 struct cinergyt2_rc_event {
161 } __attribute__((packed));
163 static const uint32_t rc_keys[] = {
164 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfe01eb04, KEY_POWER,
165 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfd02eb04, KEY_1,
166 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfc03eb04, KEY_2,
167 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfb04eb04, KEY_3,
168 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfa05eb04, KEY_4,
169 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf906eb04, KEY_5,
170 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf807eb04, KEY_6,
171 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf708eb04, KEY_7,
172 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf609eb04, KEY_8,
173 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf50aeb04, KEY_9,
174 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf30ceb04, KEY_0,
175 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf40beb04, KEY_VIDEO,
176 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf20deb04, KEY_REFRESH,
177 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf10eeb04, KEY_SELECT,
178 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf00feb04, KEY_EPG,
179 CINERGYT2_RC_EVENT_TYPE_NEC, 0xef10eb04, KEY_UP,
180 CINERGYT2_RC_EVENT_TYPE_NEC, 0xeb14eb04, KEY_DOWN,
181 CINERGYT2_RC_EVENT_TYPE_NEC, 0xee11eb04, KEY_LEFT,
182 CINERGYT2_RC_EVENT_TYPE_NEC, 0xec13eb04, KEY_RIGHT,
183 CINERGYT2_RC_EVENT_TYPE_NEC, 0xed12eb04, KEY_OK,
184 CINERGYT2_RC_EVENT_TYPE_NEC, 0xea15eb04, KEY_TEXT,
185 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe916eb04, KEY_INFO,
186 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe817eb04, KEY_RED,
187 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe718eb04, KEY_GREEN,
188 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe619eb04, KEY_YELLOW,
189 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe51aeb04, KEY_BLUE,
190 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe31ceb04, KEY_VOLUMEUP,
191 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe11eeb04, KEY_VOLUMEDOWN,
192 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe21deb04, KEY_MUTE,
193 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe41beb04, KEY_CHANNELUP,
194 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe01feb04, KEY_CHANNELDOWN,
195 CINERGYT2_RC_EVENT_TYPE_NEC, 0xbf40eb04, KEY_PAUSE,
196 CINERGYT2_RC_EVENT_TYPE_NEC, 0xb34ceb04, KEY_PLAY,
197 CINERGYT2_RC_EVENT_TYPE_NEC, 0xa758eb04, KEY_RECORD,
198 CINERGYT2_RC_EVENT_TYPE_NEC, 0xab54eb04, KEY_PREVIOUS,
199 CINERGYT2_RC_EVENT_TYPE_NEC, 0xb748eb04, KEY_STOP,
200 CINERGYT2_RC_EVENT_TYPE_NEC, 0xa35ceb04, KEY_NEXT
203 static int cinergyt2_command (struct cinergyt2 *cinergyt2,
204 char *send_buf, int send_buf_len,
205 char *recv_buf, int recv_buf_len)
211 ret = usb_bulk_msg(cinergyt2->udev, usb_sndbulkpipe(cinergyt2->udev, 1),
212 send_buf, send_buf_len, &actual_len, 1000);
215 dprintk(1, "usb_bulk_msg (send) failed, err %i\n", ret);
220 ret = usb_bulk_msg(cinergyt2->udev, usb_rcvbulkpipe(cinergyt2->udev, 1),
221 recv_buf, recv_buf_len, &actual_len, 1000);
224 dprintk(1, "usb_bulk_msg (read) failed, err %i\n", ret);
226 return ret ? ret : actual_len;
229 static void cinergyt2_control_stream_transfer (struct cinergyt2 *cinergyt2, int enable)
231 char buf [] = { CINERGYT2_EP1_CONTROL_STREAM_TRANSFER, enable ? 1 : 0 };
232 cinergyt2_command(cinergyt2, buf, sizeof(buf), NULL, 0);
235 static void cinergyt2_sleep (struct cinergyt2 *cinergyt2, int sleep)
237 char buf [] = { CINERGYT2_EP1_SLEEP_MODE, sleep ? 1 : 0 };
238 cinergyt2_command(cinergyt2, buf, sizeof(buf), NULL, 0);
239 cinergyt2->sleeping = sleep;
242 static void cinergyt2_stream_irq (struct urb *urb, struct pt_regs *regs);
244 static int cinergyt2_submit_stream_urb (struct cinergyt2 *cinergyt2, struct urb *urb)
248 usb_fill_bulk_urb(urb,
250 usb_rcvbulkpipe(cinergyt2->udev, 0x2),
251 urb->transfer_buffer,
253 cinergyt2_stream_irq,
256 if ((err = usb_submit_urb(urb, GFP_ATOMIC)))
257 dprintk(1, "urb submission failed (err = %i)!\n", err);
262 static void cinergyt2_stream_irq (struct urb *urb, struct pt_regs *regs)
264 struct cinergyt2 *cinergyt2 = urb->context;
266 if (urb->actual_length > 0)
267 dvb_dmx_swfilter(&cinergyt2->demux,
268 urb->transfer_buffer, urb->actual_length);
270 if (cinergyt2->streaming)
271 cinergyt2_submit_stream_urb(cinergyt2, urb);
274 static void cinergyt2_free_stream_urbs (struct cinergyt2 *cinergyt2)
278 for (i=0; i<STREAM_URB_COUNT; i++)
279 if (cinergyt2->stream_urb[i])
280 usb_free_urb(cinergyt2->stream_urb[i]);
282 usb_buffer_free(cinergyt2->udev, STREAM_URB_COUNT*STREAM_BUF_SIZE,
283 cinergyt2->streambuf, cinergyt2->streambuf_dmahandle);
286 static int cinergyt2_alloc_stream_urbs (struct cinergyt2 *cinergyt2)
290 cinergyt2->streambuf = usb_buffer_alloc(cinergyt2->udev, STREAM_URB_COUNT*STREAM_BUF_SIZE,
291 SLAB_KERNEL, &cinergyt2->streambuf_dmahandle);
292 if (!cinergyt2->streambuf) {
293 dprintk(1, "failed to alloc consistent stream memory area, bailing out!\n");
297 memset(cinergyt2->streambuf, 0, STREAM_URB_COUNT*STREAM_BUF_SIZE);
299 for (i=0; i<STREAM_URB_COUNT; i++) {
302 if (!(urb = usb_alloc_urb(0, GFP_ATOMIC))) {
303 dprintk(1, "failed to alloc consistent stream urbs, bailing out!\n");
304 cinergyt2_free_stream_urbs(cinergyt2);
308 urb->transfer_buffer = cinergyt2->streambuf + i * STREAM_BUF_SIZE;
309 urb->transfer_buffer_length = STREAM_BUF_SIZE;
311 cinergyt2->stream_urb[i] = urb;
317 static void cinergyt2_stop_stream_xfer (struct cinergyt2 *cinergyt2)
321 cinergyt2_control_stream_transfer(cinergyt2, 0);
323 for (i=0; i<STREAM_URB_COUNT; i++)
324 if (cinergyt2->stream_urb[i])
325 usb_kill_urb(cinergyt2->stream_urb[i]);
328 static int cinergyt2_start_stream_xfer (struct cinergyt2 *cinergyt2)
332 for (i=0; i<STREAM_URB_COUNT; i++) {
333 if ((err = cinergyt2_submit_stream_urb(cinergyt2, cinergyt2->stream_urb[i]))) {
334 cinergyt2_stop_stream_xfer(cinergyt2);
335 dprintk(1, "failed urb submission (%i: err = %i)!\n", i, err);
340 cinergyt2_control_stream_transfer(cinergyt2, 1);
344 static int cinergyt2_start_feed(struct dvb_demux_feed *dvbdmxfeed)
346 struct dvb_demux *demux = dvbdmxfeed->demux;
347 struct cinergyt2 *cinergyt2 = demux->priv;
349 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
352 if (cinergyt2->streaming == 0)
353 cinergyt2_start_stream_xfer(cinergyt2);
355 cinergyt2->streaming++;
356 mutex_unlock(&cinergyt2->sem);
360 static int cinergyt2_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
362 struct dvb_demux *demux = dvbdmxfeed->demux;
363 struct cinergyt2 *cinergyt2 = demux->priv;
365 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
368 if (--cinergyt2->streaming == 0)
369 cinergyt2_stop_stream_xfer(cinergyt2);
371 mutex_unlock(&cinergyt2->sem);
376 * convert linux-dvb frontend parameter set into TPS.
377 * See ETSI ETS-300744, section 4.6.2, table 9 for details.
379 * This function is probably reusable and may better get placed in a support
382 * We replace errornous fields by default TPS fields (the ones with value 0).
384 static uint16_t compute_tps (struct dvb_frontend_parameters *p)
386 struct dvb_ofdm_parameters *op = &p->u.ofdm;
389 switch (op->code_rate_HP) {
405 /* tps |= (0 << 7) */;
408 switch (op->code_rate_LP) {
424 /* tps |= (0 << 4) */;
427 switch (op->constellation) {
436 /* tps |= (0 << 13) */;
439 switch (op->transmission_mode) {
440 case TRANSMISSION_MODE_8K:
443 case TRANSMISSION_MODE_2K:
445 /* tps |= (0 << 0) */;
448 switch (op->guard_interval) {
449 case GUARD_INTERVAL_1_16:
452 case GUARD_INTERVAL_1_8:
455 case GUARD_INTERVAL_1_4:
458 case GUARD_INTERVAL_1_32:
460 /* tps |= (0 << 2) */;
463 switch (op->hierarchy_information) {
475 /* tps |= (0 << 10) */;
481 static int cinergyt2_open (struct inode *inode, struct file *file)
483 struct dvb_device *dvbdev = file->private_data;
484 struct cinergyt2 *cinergyt2 = dvbdev->priv;
485 int err = -ERESTARTSYS;
487 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
490 if ((err = dvb_generic_open(inode, file))) {
491 mutex_unlock(&cinergyt2->sem);
496 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
497 cinergyt2_sleep(cinergyt2, 0);
498 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
501 atomic_inc(&cinergyt2->inuse);
503 mutex_unlock(&cinergyt2->sem);
507 static void cinergyt2_unregister(struct cinergyt2 *cinergyt2)
509 dvb_unregister_device(cinergyt2->fedev);
510 dvb_unregister_adapter(&cinergyt2->adapter);
512 cinergyt2_free_stream_urbs(cinergyt2);
516 static int cinergyt2_release (struct inode *inode, struct file *file)
518 struct dvb_device *dvbdev = file->private_data;
519 struct cinergyt2 *cinergyt2 = dvbdev->priv;
521 if (mutex_lock_interruptible(&cinergyt2->sem))
524 if (!cinergyt2->disconnect_pending && (file->f_flags & O_ACCMODE) != O_RDONLY) {
525 cancel_delayed_work(&cinergyt2->query_work);
526 flush_scheduled_work();
527 cinergyt2_sleep(cinergyt2, 1);
530 mutex_unlock(&cinergyt2->sem);
532 if (atomic_dec_and_test(&cinergyt2->inuse) && cinergyt2->disconnect_pending) {
533 warn("delayed unregister in release");
534 cinergyt2_unregister(cinergyt2);
537 return dvb_generic_release(inode, file);
540 static unsigned int cinergyt2_poll (struct file *file, struct poll_table_struct *wait)
542 struct dvb_device *dvbdev = file->private_data;
543 struct cinergyt2 *cinergyt2 = dvbdev->priv;
545 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
548 poll_wait(file, &cinergyt2->poll_wq, wait);
550 mutex_unlock(&cinergyt2->sem);
552 return (POLLIN | POLLRDNORM | POLLPRI);
556 static int cinergyt2_ioctl (struct inode *inode, struct file *file,
557 unsigned cmd, unsigned long arg)
559 struct dvb_device *dvbdev = file->private_data;
560 struct cinergyt2 *cinergyt2 = dvbdev->priv;
561 struct dvbt_get_status_msg *stat = &cinergyt2->status;
562 fe_status_t status = 0;
566 return copy_to_user((void __user*) arg, &cinergyt2_fe_info,
567 sizeof(struct dvb_frontend_info));
570 if (0xffff - le16_to_cpu(stat->gain) > 30)
571 status |= FE_HAS_SIGNAL;
572 if (stat->lock_bits & (1 << 6))
573 status |= FE_HAS_LOCK;
574 if (stat->lock_bits & (1 << 5))
575 status |= FE_HAS_SYNC;
576 if (stat->lock_bits & (1 << 4))
577 status |= FE_HAS_CARRIER;
578 if (stat->lock_bits & (1 << 1))
579 status |= FE_HAS_VITERBI;
581 return copy_to_user((void __user*) arg, &status, sizeof(status));
584 return put_user(le32_to_cpu(stat->viterbi_error_rate),
585 (__u32 __user *) arg);
587 case FE_READ_SIGNAL_STRENGTH:
588 return put_user(0xffff - le16_to_cpu(stat->gain),
589 (__u16 __user *) arg);
592 return put_user((stat->snr << 8) | stat->snr,
593 (__u16 __user *) arg);
595 case FE_READ_UNCORRECTED_BLOCKS:
599 unc_count = stat->uncorrected_block_count;
600 stat->uncorrected_block_count = 0;
602 /* UNC are already converted to host byte order... */
603 return put_user(unc_count,(__u32 __user *) arg);
605 case FE_SET_FRONTEND:
607 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
608 struct dvb_frontend_parameters p;
611 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
614 if (copy_from_user(&p, (void __user*) arg, sizeof(p)))
617 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
620 param->cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
621 param->tps = cpu_to_le16(compute_tps(&p));
622 param->freq = cpu_to_le32(p.frequency / 1000);
623 param->bandwidth = 8 - p.u.ofdm.bandwidth - BANDWIDTH_8_MHZ;
626 cinergyt2->pending_fe_events++;
627 wake_up_interruptible(&cinergyt2->poll_wq);
629 err = cinergyt2_command(cinergyt2,
630 (char *) param, sizeof(*param),
633 mutex_unlock(&cinergyt2->sem);
635 return (err < 0) ? err : 0;
638 case FE_GET_FRONTEND:
640 * trivial to implement (see struct dvbt_get_status_msg).
641 * equivalent to FE_READ ioctls, but needs
642 * TPS -> linux-dvb parameter set conversion. Feel free
643 * to implement this and send us a patch if you need this
651 * for now we only fill the status field. the parameters
652 * are trivial to fill as soon FE_GET_FRONTEND is done.
654 struct dvb_frontend_event __user *e = (void __user *) arg;
655 if (cinergyt2->pending_fe_events == 0) {
656 if (file->f_flags & O_NONBLOCK)
658 wait_event_interruptible(cinergyt2->poll_wq,
659 cinergyt2->pending_fe_events > 0);
661 cinergyt2->pending_fe_events = 0;
662 return cinergyt2_ioctl(inode, file, FE_READ_STATUS,
663 (unsigned long) &e->status);
673 static int cinergyt2_mmap(struct file *file, struct vm_area_struct *vma)
675 struct dvb_device *dvbdev = file->private_data;
676 struct cinergyt2 *cinergyt2 = dvbdev->priv;
681 if (vma->vm_flags & (VM_WRITE | VM_EXEC)) {
686 if (vma->vm_end > vma->vm_start + STREAM_URB_COUNT * STREAM_BUF_SIZE) {
691 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
694 ret = remap_pfn_range(vma, vma->vm_start,
695 virt_to_phys(cinergyt2->streambuf) >> PAGE_SHIFT,
696 vma->vm_end - vma->vm_start,
697 vma->vm_page_prot) ? -EAGAIN : 0;
703 static struct file_operations cinergyt2_fops = {
704 .owner = THIS_MODULE,
705 .ioctl = cinergyt2_ioctl,
706 .poll = cinergyt2_poll,
707 .open = cinergyt2_open,
708 .release = cinergyt2_release,
709 .mmap = cinergyt2_mmap
712 static struct dvb_device cinergyt2_fe_template = {
716 .fops = &cinergyt2_fops
721 static void cinergyt2_query_rc (void *data)
723 struct cinergyt2 *cinergyt2 = data;
724 char buf[1] = { CINERGYT2_EP1_GET_RC_EVENTS };
725 struct cinergyt2_rc_event rc_events[12];
728 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
731 len = cinergyt2_command(cinergyt2, buf, sizeof(buf),
732 (char *) rc_events, sizeof(rc_events));
736 if (time_after(jiffies, cinergyt2->last_event_jiffies +
737 msecs_to_jiffies(150))) {
738 /* stop key repeat */
739 if (cinergyt2->rc_input_event != KEY_MAX) {
740 dprintk(1, "rc_input_event=%d Up\n", cinergyt2->rc_input_event);
741 input_report_key(cinergyt2->rc_input_dev,
742 cinergyt2->rc_input_event, 0);
743 cinergyt2->rc_input_event = KEY_MAX;
745 cinergyt2->rc_last_code = ~0;
749 cinergyt2->last_event_jiffies = jiffies;
751 for (n = 0; n < (len / sizeof(rc_events[0])); n++) {
752 dprintk(1, "rc_events[%d].value = %x, type=%x\n",
753 n, le32_to_cpu(rc_events[n].value), rc_events[n].type);
755 if (rc_events[n].type == CINERGYT2_RC_EVENT_TYPE_NEC &&
756 rc_events[n].value == ~0) {
757 /* keyrepeat bit -> just repeat last rc_input_event */
759 cinergyt2->rc_input_event = KEY_MAX;
760 for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3) {
761 if (rc_keys[i + 0] == rc_events[n].type &&
762 rc_keys[i + 1] == le32_to_cpu(rc_events[n].value)) {
763 cinergyt2->rc_input_event = rc_keys[i + 2];
769 if (cinergyt2->rc_input_event != KEY_MAX) {
770 if (rc_events[n].value == cinergyt2->rc_last_code &&
771 cinergyt2->rc_last_code != ~0) {
772 /* emit a key-up so the double event is recognized */
773 dprintk(1, "rc_input_event=%d UP\n", cinergyt2->rc_input_event);
774 input_report_key(cinergyt2->rc_input_dev,
775 cinergyt2->rc_input_event, 0);
777 dprintk(1, "rc_input_event=%d\n", cinergyt2->rc_input_event);
778 input_report_key(cinergyt2->rc_input_dev,
779 cinergyt2->rc_input_event, 1);
780 cinergyt2->rc_last_code = rc_events[n].value;
785 schedule_delayed_work(&cinergyt2->rc_query_work,
786 msecs_to_jiffies(RC_QUERY_INTERVAL));
788 mutex_unlock(&cinergyt2->sem);
791 static int cinergyt2_register_rc(struct cinergyt2 *cinergyt2)
793 struct input_dev *input_dev;
796 cinergyt2->rc_input_dev = input_dev = input_allocate_device();
800 usb_make_path(cinergyt2->udev, cinergyt2->phys, sizeof(cinergyt2->phys));
801 strlcat(cinergyt2->phys, "/input0", sizeof(cinergyt2->phys));
802 cinergyt2->rc_input_event = KEY_MAX;
803 cinergyt2->rc_last_code = ~0;
804 INIT_WORK(&cinergyt2->rc_query_work, cinergyt2_query_rc, cinergyt2);
806 input_dev->name = DRIVER_NAME " remote control";
807 input_dev->phys = cinergyt2->phys;
808 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
809 for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3)
810 set_bit(rc_keys[i + 2], input_dev->keybit);
811 input_dev->keycodesize = 0;
812 input_dev->keycodemax = 0;
814 input_register_device(cinergyt2->rc_input_dev);
815 schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
820 static void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2)
822 cancel_delayed_work(&cinergyt2->rc_query_work);
823 input_unregister_device(cinergyt2->rc_input_dev);
826 static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2)
828 cancel_delayed_work(&cinergyt2->rc_query_work);
831 static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2)
833 schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
838 static inline int cinergyt2_register_rc(struct cinergyt2 *cinergyt2) { return 0; }
839 static inline void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2) { }
840 static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2) { }
841 static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2) { }
843 #endif /* ENABLE_RC */
845 static void cinergyt2_query (void *data)
847 struct cinergyt2 *cinergyt2 = (struct cinergyt2 *) data;
848 char cmd [] = { CINERGYT2_EP1_GET_TUNER_STATUS };
849 struct dvbt_get_status_msg *s = &cinergyt2->status;
853 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
856 unc = s->uncorrected_block_count;
857 lock_bits = s->lock_bits;
859 cinergyt2_command(cinergyt2, cmd, sizeof(cmd), (char *) s, sizeof(*s));
861 unc += le32_to_cpu(s->uncorrected_block_count);
862 s->uncorrected_block_count = unc;
864 if (lock_bits != s->lock_bits) {
865 wake_up_interruptible(&cinergyt2->poll_wq);
866 cinergyt2->pending_fe_events++;
869 schedule_delayed_work(&cinergyt2->query_work,
870 msecs_to_jiffies(QUERY_INTERVAL));
872 mutex_unlock(&cinergyt2->sem);
875 static int cinergyt2_probe (struct usb_interface *intf,
876 const struct usb_device_id *id)
878 struct cinergyt2 *cinergyt2;
881 if (!(cinergyt2 = kmalloc (sizeof(struct cinergyt2), GFP_KERNEL))) {
882 dprintk(1, "out of memory?!?\n");
886 memset (cinergyt2, 0, sizeof (struct cinergyt2));
887 usb_set_intfdata (intf, (void *) cinergyt2);
889 mutex_init(&cinergyt2->sem);
890 init_waitqueue_head (&cinergyt2->poll_wq);
891 INIT_WORK(&cinergyt2->query_work, cinergyt2_query, cinergyt2);
893 cinergyt2->udev = interface_to_usbdev(intf);
894 cinergyt2->param.cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
896 if (cinergyt2_alloc_stream_urbs (cinergyt2) < 0) {
897 dprintk(1, "unable to allocate stream urbs\n");
902 dvb_register_adapter(&cinergyt2->adapter, DRIVER_NAME, THIS_MODULE);
904 cinergyt2->demux.priv = cinergyt2;
905 cinergyt2->demux.filternum = 256;
906 cinergyt2->demux.feednum = 256;
907 cinergyt2->demux.start_feed = cinergyt2_start_feed;
908 cinergyt2->demux.stop_feed = cinergyt2_stop_feed;
909 cinergyt2->demux.dmx.capabilities = DMX_TS_FILTERING |
910 DMX_SECTION_FILTERING |
911 DMX_MEMORY_BASED_FILTERING;
913 if ((err = dvb_dmx_init(&cinergyt2->demux)) < 0) {
914 dprintk(1, "dvb_dmx_init() failed (err = %d)\n", err);
918 cinergyt2->dmxdev.filternum = cinergyt2->demux.filternum;
919 cinergyt2->dmxdev.demux = &cinergyt2->demux.dmx;
920 cinergyt2->dmxdev.capabilities = 0;
922 if ((err = dvb_dmxdev_init(&cinergyt2->dmxdev, &cinergyt2->adapter)) < 0) {
923 dprintk(1, "dvb_dmxdev_init() failed (err = %d)\n", err);
927 if (dvb_net_init(&cinergyt2->adapter, &cinergyt2->dvbnet, &cinergyt2->demux.dmx))
928 dprintk(1, "dvb_net_init() failed!\n");
930 dvb_register_device(&cinergyt2->adapter, &cinergyt2->fedev,
931 &cinergyt2_fe_template, cinergyt2,
932 DVB_DEVICE_FRONTEND);
934 err = cinergyt2_register_rc(cinergyt2);
941 dvb_dmxdev_release(&cinergyt2->dmxdev);
942 dvb_dmx_release(&cinergyt2->demux);
943 dvb_unregister_adapter(&cinergyt2->adapter);
944 cinergyt2_free_stream_urbs(cinergyt2);
949 static void cinergyt2_disconnect (struct usb_interface *intf)
951 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
953 flush_scheduled_work();
955 cinergyt2_unregister_rc(cinergyt2);
957 cancel_delayed_work(&cinergyt2->query_work);
958 wake_up_interruptible(&cinergyt2->poll_wq);
960 cinergyt2->demux.dmx.close(&cinergyt2->demux.dmx);
961 cinergyt2->disconnect_pending = 1;
963 if (!atomic_read(&cinergyt2->inuse))
964 cinergyt2_unregister(cinergyt2);
967 static int cinergyt2_suspend (struct usb_interface *intf, pm_message_t state)
969 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
971 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
974 if (state.event > PM_EVENT_ON) {
975 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
977 cinergyt2_suspend_rc(cinergyt2);
978 cancel_delayed_work(&cinergyt2->query_work);
979 if (cinergyt2->streaming)
980 cinergyt2_stop_stream_xfer(cinergyt2);
981 flush_scheduled_work();
982 cinergyt2_sleep(cinergyt2, 1);
985 mutex_unlock(&cinergyt2->sem);
989 static int cinergyt2_resume (struct usb_interface *intf)
991 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
992 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
994 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
997 if (!cinergyt2->sleeping) {
998 cinergyt2_sleep(cinergyt2, 0);
999 cinergyt2_command(cinergyt2, (char *) param, sizeof(*param), NULL, 0);
1000 if (cinergyt2->streaming)
1001 cinergyt2_start_stream_xfer(cinergyt2);
1002 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
1005 cinergyt2_resume_rc(cinergyt2);
1007 mutex_unlock(&cinergyt2->sem);
1011 static const struct usb_device_id cinergyt2_table [] __devinitdata = {
1012 { USB_DEVICE(0x0ccd, 0x0038) },
1016 MODULE_DEVICE_TABLE(usb, cinergyt2_table);
1018 static struct usb_driver cinergyt2_driver = {
1019 .name = "cinergyT2",
1020 .probe = cinergyt2_probe,
1021 .disconnect = cinergyt2_disconnect,
1022 .suspend = cinergyt2_suspend,
1023 .resume = cinergyt2_resume,
1024 .id_table = cinergyt2_table
1027 static int __init cinergyt2_init (void)
1031 if ((err = usb_register(&cinergyt2_driver)) < 0)
1032 dprintk(1, "usb_register() failed! (err %i)\n", err);
1037 static void __exit cinergyt2_exit (void)
1039 usb_deregister(&cinergyt2_driver);
1042 module_init (cinergyt2_init);
1043 module_exit (cinergyt2_exit);
1045 MODULE_LICENSE("GPL");
1046 MODULE_AUTHOR("Holger Waechtler, Daniel Mack");