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/init.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/usb.h>
29 #include <linux/input.h>
30 #include <linux/dvb/frontend.h>
31 #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;
122 struct dvb_adapter adapter;
123 struct dvb_device *fedev;
124 struct dmxdev dmxdev;
125 struct dvb_net dvbnet;
130 struct dvbt_set_parameters_msg param;
131 struct dvbt_get_status_msg status;
132 struct delayed_work query_work;
134 wait_queue_head_t poll_wq;
135 int pending_fe_events;
136 int disconnect_pending;
140 dma_addr_t streambuf_dmahandle;
141 struct urb *stream_urb [STREAM_URB_COUNT];
144 struct input_dev *rc_input_dev;
146 struct delayed_work rc_query_work;
149 unsigned long last_event_jiffies;
154 CINERGYT2_RC_EVENT_TYPE_NONE = 0x00,
155 CINERGYT2_RC_EVENT_TYPE_NEC = 0x01,
156 CINERGYT2_RC_EVENT_TYPE_RC5 = 0x02
159 struct cinergyt2_rc_event {
162 } __attribute__((packed));
164 static const uint32_t rc_keys[] = {
165 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfe01eb04, KEY_POWER,
166 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfd02eb04, KEY_1,
167 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfc03eb04, KEY_2,
168 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfb04eb04, KEY_3,
169 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfa05eb04, KEY_4,
170 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf906eb04, KEY_5,
171 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf807eb04, KEY_6,
172 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf708eb04, KEY_7,
173 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf609eb04, KEY_8,
174 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf50aeb04, KEY_9,
175 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf30ceb04, KEY_0,
176 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf40beb04, KEY_VIDEO,
177 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf20deb04, KEY_REFRESH,
178 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf10eeb04, KEY_SELECT,
179 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf00feb04, KEY_EPG,
180 CINERGYT2_RC_EVENT_TYPE_NEC, 0xef10eb04, KEY_UP,
181 CINERGYT2_RC_EVENT_TYPE_NEC, 0xeb14eb04, KEY_DOWN,
182 CINERGYT2_RC_EVENT_TYPE_NEC, 0xee11eb04, KEY_LEFT,
183 CINERGYT2_RC_EVENT_TYPE_NEC, 0xec13eb04, KEY_RIGHT,
184 CINERGYT2_RC_EVENT_TYPE_NEC, 0xed12eb04, KEY_OK,
185 CINERGYT2_RC_EVENT_TYPE_NEC, 0xea15eb04, KEY_TEXT,
186 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe916eb04, KEY_INFO,
187 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe817eb04, KEY_RED,
188 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe718eb04, KEY_GREEN,
189 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe619eb04, KEY_YELLOW,
190 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe51aeb04, KEY_BLUE,
191 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe31ceb04, KEY_VOLUMEUP,
192 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe11eeb04, KEY_VOLUMEDOWN,
193 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe21deb04, KEY_MUTE,
194 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe41beb04, KEY_CHANNELUP,
195 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe01feb04, KEY_CHANNELDOWN,
196 CINERGYT2_RC_EVENT_TYPE_NEC, 0xbf40eb04, KEY_PAUSE,
197 CINERGYT2_RC_EVENT_TYPE_NEC, 0xb34ceb04, KEY_PLAY,
198 CINERGYT2_RC_EVENT_TYPE_NEC, 0xa758eb04, KEY_RECORD,
199 CINERGYT2_RC_EVENT_TYPE_NEC, 0xab54eb04, KEY_PREVIOUS,
200 CINERGYT2_RC_EVENT_TYPE_NEC, 0xb748eb04, KEY_STOP,
201 CINERGYT2_RC_EVENT_TYPE_NEC, 0xa35ceb04, KEY_NEXT
204 static int cinergyt2_command (struct cinergyt2 *cinergyt2,
205 char *send_buf, int send_buf_len,
206 char *recv_buf, int recv_buf_len)
212 ret = usb_bulk_msg(cinergyt2->udev, usb_sndbulkpipe(cinergyt2->udev, 1),
213 send_buf, send_buf_len, &actual_len, 1000);
216 dprintk(1, "usb_bulk_msg (send) failed, err %i\n", ret);
221 ret = usb_bulk_msg(cinergyt2->udev, usb_rcvbulkpipe(cinergyt2->udev, 1),
222 recv_buf, recv_buf_len, &actual_len, 1000);
225 dprintk(1, "usb_bulk_msg (read) failed, err %i\n", ret);
227 return ret ? ret : actual_len;
230 static void cinergyt2_control_stream_transfer (struct cinergyt2 *cinergyt2, int enable)
232 char buf [] = { CINERGYT2_EP1_CONTROL_STREAM_TRANSFER, enable ? 1 : 0 };
233 cinergyt2_command(cinergyt2, buf, sizeof(buf), NULL, 0);
236 static void cinergyt2_sleep (struct cinergyt2 *cinergyt2, int sleep)
238 char buf [] = { CINERGYT2_EP1_SLEEP_MODE, sleep ? 1 : 0 };
239 cinergyt2_command(cinergyt2, buf, sizeof(buf), NULL, 0);
240 cinergyt2->sleeping = sleep;
243 static void cinergyt2_stream_irq (struct urb *urb);
245 static int cinergyt2_submit_stream_urb (struct cinergyt2 *cinergyt2, struct urb *urb)
249 usb_fill_bulk_urb(urb,
251 usb_rcvbulkpipe(cinergyt2->udev, 0x2),
252 urb->transfer_buffer,
254 cinergyt2_stream_irq,
257 if ((err = usb_submit_urb(urb, GFP_ATOMIC)))
258 dprintk(1, "urb submission failed (err = %i)!\n", err);
263 static void cinergyt2_stream_irq (struct urb *urb)
265 struct cinergyt2 *cinergyt2 = urb->context;
267 if (urb->actual_length > 0)
268 dvb_dmx_swfilter(&cinergyt2->demux,
269 urb->transfer_buffer, urb->actual_length);
271 if (cinergyt2->streaming)
272 cinergyt2_submit_stream_urb(cinergyt2, urb);
275 static void cinergyt2_free_stream_urbs (struct cinergyt2 *cinergyt2)
279 for (i=0; i<STREAM_URB_COUNT; 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 GFP_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 usb_kill_urb(cinergyt2->stream_urb[i]);
327 static int cinergyt2_start_stream_xfer (struct cinergyt2 *cinergyt2)
331 for (i=0; i<STREAM_URB_COUNT; i++) {
332 if ((err = cinergyt2_submit_stream_urb(cinergyt2, cinergyt2->stream_urb[i]))) {
333 cinergyt2_stop_stream_xfer(cinergyt2);
334 dprintk(1, "failed urb submission (%i: err = %i)!\n", i, err);
339 cinergyt2_control_stream_transfer(cinergyt2, 1);
343 static int cinergyt2_start_feed(struct dvb_demux_feed *dvbdmxfeed)
345 struct dvb_demux *demux = dvbdmxfeed->demux;
346 struct cinergyt2 *cinergyt2 = demux->priv;
348 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
351 if (cinergyt2->streaming == 0)
352 cinergyt2_start_stream_xfer(cinergyt2);
354 cinergyt2->streaming++;
355 mutex_unlock(&cinergyt2->sem);
359 static int cinergyt2_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
361 struct dvb_demux *demux = dvbdmxfeed->demux;
362 struct cinergyt2 *cinergyt2 = demux->priv;
364 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
367 if (--cinergyt2->streaming == 0)
368 cinergyt2_stop_stream_xfer(cinergyt2);
370 mutex_unlock(&cinergyt2->sem);
375 * convert linux-dvb frontend parameter set into TPS.
376 * See ETSI ETS-300744, section 4.6.2, table 9 for details.
378 * This function is probably reusable and may better get placed in a support
381 * We replace errornous fields by default TPS fields (the ones with value 0).
383 static uint16_t compute_tps (struct dvb_frontend_parameters *p)
385 struct dvb_ofdm_parameters *op = &p->u.ofdm;
388 switch (op->code_rate_HP) {
404 /* tps |= (0 << 7) */;
407 switch (op->code_rate_LP) {
423 /* tps |= (0 << 4) */;
426 switch (op->constellation) {
435 /* tps |= (0 << 13) */;
438 switch (op->transmission_mode) {
439 case TRANSMISSION_MODE_8K:
442 case TRANSMISSION_MODE_2K:
444 /* tps |= (0 << 0) */;
447 switch (op->guard_interval) {
448 case GUARD_INTERVAL_1_16:
451 case GUARD_INTERVAL_1_8:
454 case GUARD_INTERVAL_1_4:
457 case GUARD_INTERVAL_1_32:
459 /* tps |= (0 << 2) */;
462 switch (op->hierarchy_information) {
474 /* tps |= (0 << 10) */;
480 static int cinergyt2_open (struct inode *inode, struct file *file)
482 struct dvb_device *dvbdev = file->private_data;
483 struct cinergyt2 *cinergyt2 = dvbdev->priv;
484 int err = -ERESTARTSYS;
486 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->wq_sem))
489 if (mutex_lock_interruptible(&cinergyt2->sem))
492 if ((err = dvb_generic_open(inode, file)))
495 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
496 cinergyt2_sleep(cinergyt2, 0);
497 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
500 atomic_inc(&cinergyt2->inuse);
503 mutex_unlock(&cinergyt2->sem);
505 mutex_unlock(&cinergyt2->wq_sem);
510 static void cinergyt2_unregister(struct cinergyt2 *cinergyt2)
512 dvb_net_release(&cinergyt2->dvbnet);
513 dvb_dmxdev_release(&cinergyt2->dmxdev);
514 dvb_dmx_release(&cinergyt2->demux);
515 dvb_unregister_device(cinergyt2->fedev);
516 dvb_unregister_adapter(&cinergyt2->adapter);
518 cinergyt2_free_stream_urbs(cinergyt2);
522 static int cinergyt2_release (struct inode *inode, struct file *file)
524 struct dvb_device *dvbdev = file->private_data;
525 struct cinergyt2 *cinergyt2 = dvbdev->priv;
527 mutex_lock(&cinergyt2->wq_sem);
529 if (!cinergyt2->disconnect_pending && (file->f_flags & O_ACCMODE) != O_RDONLY) {
530 cancel_rearming_delayed_work(&cinergyt2->query_work);
532 mutex_lock(&cinergyt2->sem);
533 cinergyt2_sleep(cinergyt2, 1);
534 mutex_unlock(&cinergyt2->sem);
537 mutex_unlock(&cinergyt2->wq_sem);
539 if (atomic_dec_and_test(&cinergyt2->inuse) && cinergyt2->disconnect_pending) {
540 warn("delayed unregister in release");
541 cinergyt2_unregister(cinergyt2);
544 return dvb_generic_release(inode, file);
547 static unsigned int cinergyt2_poll (struct file *file, struct poll_table_struct *wait)
549 struct dvb_device *dvbdev = file->private_data;
550 struct cinergyt2 *cinergyt2 = dvbdev->priv;
551 unsigned int mask = 0;
553 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
556 poll_wait(file, &cinergyt2->poll_wq, wait);
558 if (cinergyt2->pending_fe_events != 0)
559 mask |= (POLLIN | POLLRDNORM | POLLPRI);
561 mutex_unlock(&cinergyt2->sem);
567 static int cinergyt2_ioctl (struct inode *inode, struct file *file,
568 unsigned cmd, unsigned long arg)
570 struct dvb_device *dvbdev = file->private_data;
571 struct cinergyt2 *cinergyt2 = dvbdev->priv;
572 struct dvbt_get_status_msg *stat = &cinergyt2->status;
573 fe_status_t status = 0;
577 return copy_to_user((void __user*) arg, &cinergyt2_fe_info,
578 sizeof(struct dvb_frontend_info));
581 if (0xffff - le16_to_cpu(stat->gain) > 30)
582 status |= FE_HAS_SIGNAL;
583 if (stat->lock_bits & (1 << 6))
584 status |= FE_HAS_LOCK;
585 if (stat->lock_bits & (1 << 5))
586 status |= FE_HAS_SYNC;
587 if (stat->lock_bits & (1 << 4))
588 status |= FE_HAS_CARRIER;
589 if (stat->lock_bits & (1 << 1))
590 status |= FE_HAS_VITERBI;
592 return copy_to_user((void __user*) arg, &status, sizeof(status));
595 return put_user(le32_to_cpu(stat->viterbi_error_rate),
596 (__u32 __user *) arg);
598 case FE_READ_SIGNAL_STRENGTH:
599 return put_user(0xffff - le16_to_cpu(stat->gain),
600 (__u16 __user *) arg);
603 return put_user((stat->snr << 8) | stat->snr,
604 (__u16 __user *) arg);
606 case FE_READ_UNCORRECTED_BLOCKS:
610 unc_count = stat->uncorrected_block_count;
611 stat->uncorrected_block_count = 0;
613 /* UNC are already converted to host byte order... */
614 return put_user(unc_count,(__u32 __user *) arg);
616 case FE_SET_FRONTEND:
618 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
619 struct dvb_frontend_parameters p;
622 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
625 if (copy_from_user(&p, (void __user*) arg, sizeof(p)))
628 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
631 param->cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
632 param->tps = cpu_to_le16(compute_tps(&p));
633 param->freq = cpu_to_le32(p.frequency / 1000);
634 param->bandwidth = 8 - p.u.ofdm.bandwidth - BANDWIDTH_8_MHZ;
637 cinergyt2->pending_fe_events++;
638 wake_up_interruptible(&cinergyt2->poll_wq);
640 err = cinergyt2_command(cinergyt2,
641 (char *) param, sizeof(*param),
644 mutex_unlock(&cinergyt2->sem);
646 return (err < 0) ? err : 0;
649 case FE_GET_FRONTEND:
651 * trivial to implement (see struct dvbt_get_status_msg).
652 * equivalent to FE_READ ioctls, but needs
653 * TPS -> linux-dvb parameter set conversion. Feel free
654 * to implement this and send us a patch if you need this
662 * for now we only fill the status field. the parameters
663 * are trivial to fill as soon FE_GET_FRONTEND is done.
665 struct dvb_frontend_event __user *e = (void __user *) arg;
666 if (cinergyt2->pending_fe_events == 0) {
667 if (file->f_flags & O_NONBLOCK)
669 wait_event_interruptible(cinergyt2->poll_wq,
670 cinergyt2->pending_fe_events > 0);
672 cinergyt2->pending_fe_events = 0;
673 return cinergyt2_ioctl(inode, file, FE_READ_STATUS,
674 (unsigned long) &e->status);
684 static int cinergyt2_mmap(struct file *file, struct vm_area_struct *vma)
686 struct dvb_device *dvbdev = file->private_data;
687 struct cinergyt2 *cinergyt2 = dvbdev->priv;
692 if (vma->vm_flags & (VM_WRITE | VM_EXEC)) {
697 if (vma->vm_end > vma->vm_start + STREAM_URB_COUNT * STREAM_BUF_SIZE) {
702 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
705 ret = remap_pfn_range(vma, vma->vm_start,
706 virt_to_phys(cinergyt2->streambuf) >> PAGE_SHIFT,
707 vma->vm_end - vma->vm_start,
708 vma->vm_page_prot) ? -EAGAIN : 0;
714 static struct file_operations cinergyt2_fops = {
715 .owner = THIS_MODULE,
716 .ioctl = cinergyt2_ioctl,
717 .poll = cinergyt2_poll,
718 .open = cinergyt2_open,
719 .release = cinergyt2_release,
720 .mmap = cinergyt2_mmap
723 static struct dvb_device cinergyt2_fe_template = {
727 .fops = &cinergyt2_fops
732 static void cinergyt2_query_rc (struct work_struct *work)
734 struct cinergyt2 *cinergyt2 =
735 container_of(work, struct cinergyt2, rc_query_work.work);
736 char buf[1] = { CINERGYT2_EP1_GET_RC_EVENTS };
737 struct cinergyt2_rc_event rc_events[12];
740 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
743 len = cinergyt2_command(cinergyt2, buf, sizeof(buf),
744 (char *) rc_events, sizeof(rc_events));
748 if (time_after(jiffies, cinergyt2->last_event_jiffies +
749 msecs_to_jiffies(150))) {
750 /* stop key repeat */
751 if (cinergyt2->rc_input_event != KEY_MAX) {
752 dprintk(1, "rc_input_event=%d Up\n", cinergyt2->rc_input_event);
753 input_report_key(cinergyt2->rc_input_dev,
754 cinergyt2->rc_input_event, 0);
755 input_sync(cinergyt2->rc_input_dev);
756 cinergyt2->rc_input_event = KEY_MAX;
758 cinergyt2->rc_last_code = ~0;
762 cinergyt2->last_event_jiffies = jiffies;
764 for (n = 0; n < (len / sizeof(rc_events[0])); n++) {
765 dprintk(1, "rc_events[%d].value = %x, type=%x\n",
766 n, le32_to_cpu(rc_events[n].value), rc_events[n].type);
768 if (rc_events[n].type == CINERGYT2_RC_EVENT_TYPE_NEC &&
769 rc_events[n].value == ~0) {
770 /* keyrepeat bit -> just repeat last rc_input_event */
772 cinergyt2->rc_input_event = KEY_MAX;
773 for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3) {
774 if (rc_keys[i + 0] == rc_events[n].type &&
775 rc_keys[i + 1] == le32_to_cpu(rc_events[n].value)) {
776 cinergyt2->rc_input_event = rc_keys[i + 2];
782 if (cinergyt2->rc_input_event != KEY_MAX) {
783 if (rc_events[n].value == cinergyt2->rc_last_code &&
784 cinergyt2->rc_last_code != ~0) {
785 /* emit a key-up so the double event is recognized */
786 dprintk(1, "rc_input_event=%d UP\n", cinergyt2->rc_input_event);
787 input_report_key(cinergyt2->rc_input_dev,
788 cinergyt2->rc_input_event, 0);
790 dprintk(1, "rc_input_event=%d\n", cinergyt2->rc_input_event);
791 input_report_key(cinergyt2->rc_input_dev,
792 cinergyt2->rc_input_event, 1);
793 input_sync(cinergyt2->rc_input_dev);
794 cinergyt2->rc_last_code = rc_events[n].value;
799 schedule_delayed_work(&cinergyt2->rc_query_work,
800 msecs_to_jiffies(RC_QUERY_INTERVAL));
802 mutex_unlock(&cinergyt2->sem);
805 static int cinergyt2_register_rc(struct cinergyt2 *cinergyt2)
807 struct input_dev *input_dev;
811 input_dev = input_allocate_device();
815 usb_make_path(cinergyt2->udev, cinergyt2->phys, sizeof(cinergyt2->phys));
816 strlcat(cinergyt2->phys, "/input0", sizeof(cinergyt2->phys));
817 cinergyt2->rc_input_event = KEY_MAX;
818 cinergyt2->rc_last_code = ~0;
819 INIT_DELAYED_WORK(&cinergyt2->rc_query_work, cinergyt2_query_rc);
821 input_dev->name = DRIVER_NAME " remote control";
822 input_dev->phys = cinergyt2->phys;
823 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
824 for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3)
825 set_bit(rc_keys[i + 2], input_dev->keybit);
826 input_dev->keycodesize = 0;
827 input_dev->keycodemax = 0;
828 input_dev->id.bustype = BUS_USB;
829 input_dev->id.vendor = cinergyt2->udev->descriptor.idVendor;
830 input_dev->id.product = cinergyt2->udev->descriptor.idProduct;
831 input_dev->id.version = 1;
832 input_dev->dev.parent = &cinergyt2->udev->dev;
834 err = input_register_device(input_dev);
836 input_free_device(input_dev);
840 cinergyt2->rc_input_dev = input_dev;
841 schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
846 static void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2)
848 cancel_rearming_delayed_work(&cinergyt2->rc_query_work);
849 input_unregister_device(cinergyt2->rc_input_dev);
852 static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2)
854 cancel_rearming_delayed_work(&cinergyt2->rc_query_work);
857 static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2)
859 schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
864 static inline int cinergyt2_register_rc(struct cinergyt2 *cinergyt2) { return 0; }
865 static inline void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2) { }
866 static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2) { }
867 static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2) { }
869 #endif /* ENABLE_RC */
871 static void cinergyt2_query (struct work_struct *work)
873 struct cinergyt2 *cinergyt2 =
874 container_of(work, struct cinergyt2, query_work.work);
875 char cmd [] = { CINERGYT2_EP1_GET_TUNER_STATUS };
876 struct dvbt_get_status_msg *s = &cinergyt2->status;
880 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
883 unc = s->uncorrected_block_count;
884 lock_bits = s->lock_bits;
886 cinergyt2_command(cinergyt2, cmd, sizeof(cmd), (char *) s, sizeof(*s));
888 unc += le32_to_cpu(s->uncorrected_block_count);
889 s->uncorrected_block_count = unc;
891 if (lock_bits != s->lock_bits) {
892 wake_up_interruptible(&cinergyt2->poll_wq);
893 cinergyt2->pending_fe_events++;
896 schedule_delayed_work(&cinergyt2->query_work,
897 msecs_to_jiffies(QUERY_INTERVAL));
899 mutex_unlock(&cinergyt2->sem);
902 static int cinergyt2_probe (struct usb_interface *intf,
903 const struct usb_device_id *id)
905 struct cinergyt2 *cinergyt2;
908 if (!(cinergyt2 = kzalloc (sizeof(struct cinergyt2), GFP_KERNEL))) {
909 dprintk(1, "out of memory?!?\n");
913 usb_set_intfdata (intf, (void *) cinergyt2);
915 mutex_init(&cinergyt2->sem);
916 mutex_init(&cinergyt2->wq_sem);
917 init_waitqueue_head (&cinergyt2->poll_wq);
918 INIT_DELAYED_WORK(&cinergyt2->query_work, cinergyt2_query);
920 cinergyt2->udev = interface_to_usbdev(intf);
921 cinergyt2->param.cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
923 if (cinergyt2_alloc_stream_urbs (cinergyt2) < 0) {
924 dprintk(1, "unable to allocate stream urbs\n");
929 if ((err = dvb_register_adapter(&cinergyt2->adapter, DRIVER_NAME, THIS_MODULE, &cinergyt2->udev->dev)) < 0) {
934 cinergyt2->demux.priv = cinergyt2;
935 cinergyt2->demux.filternum = 256;
936 cinergyt2->demux.feednum = 256;
937 cinergyt2->demux.start_feed = cinergyt2_start_feed;
938 cinergyt2->demux.stop_feed = cinergyt2_stop_feed;
939 cinergyt2->demux.dmx.capabilities = DMX_TS_FILTERING |
940 DMX_SECTION_FILTERING |
941 DMX_MEMORY_BASED_FILTERING;
943 if ((err = dvb_dmx_init(&cinergyt2->demux)) < 0) {
944 dprintk(1, "dvb_dmx_init() failed (err = %d)\n", err);
948 cinergyt2->dmxdev.filternum = cinergyt2->demux.filternum;
949 cinergyt2->dmxdev.demux = &cinergyt2->demux.dmx;
950 cinergyt2->dmxdev.capabilities = 0;
952 if ((err = dvb_dmxdev_init(&cinergyt2->dmxdev, &cinergyt2->adapter)) < 0) {
953 dprintk(1, "dvb_dmxdev_init() failed (err = %d)\n", err);
957 if (dvb_net_init(&cinergyt2->adapter, &cinergyt2->dvbnet, &cinergyt2->demux.dmx))
958 dprintk(1, "dvb_net_init() failed!\n");
960 dvb_register_device(&cinergyt2->adapter, &cinergyt2->fedev,
961 &cinergyt2_fe_template, cinergyt2,
962 DVB_DEVICE_FRONTEND);
964 err = cinergyt2_register_rc(cinergyt2);
971 dvb_net_release(&cinergyt2->dvbnet);
972 dvb_dmxdev_release(&cinergyt2->dmxdev);
973 dvb_dmx_release(&cinergyt2->demux);
974 dvb_unregister_adapter(&cinergyt2->adapter);
975 cinergyt2_free_stream_urbs(cinergyt2);
980 static void cinergyt2_disconnect (struct usb_interface *intf)
982 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
984 cinergyt2_unregister_rc(cinergyt2);
985 cancel_rearming_delayed_work(&cinergyt2->query_work);
986 wake_up_interruptible(&cinergyt2->poll_wq);
988 cinergyt2->demux.dmx.close(&cinergyt2->demux.dmx);
989 cinergyt2->disconnect_pending = 1;
991 if (!atomic_read(&cinergyt2->inuse))
992 cinergyt2_unregister(cinergyt2);
995 static int cinergyt2_suspend (struct usb_interface *intf, pm_message_t state)
997 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
999 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->wq_sem))
1000 return -ERESTARTSYS;
1002 cinergyt2_suspend_rc(cinergyt2);
1003 cancel_rearming_delayed_work(&cinergyt2->query_work);
1005 mutex_lock(&cinergyt2->sem);
1006 if (cinergyt2->streaming)
1007 cinergyt2_stop_stream_xfer(cinergyt2);
1008 cinergyt2_sleep(cinergyt2, 1);
1009 mutex_unlock(&cinergyt2->sem);
1014 static int cinergyt2_resume (struct usb_interface *intf)
1016 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
1017 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
1018 int err = -ERESTARTSYS;
1020 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->wq_sem))
1023 if (mutex_lock_interruptible(&cinergyt2->sem))
1028 if (!cinergyt2->sleeping) {
1029 cinergyt2_sleep(cinergyt2, 0);
1030 cinergyt2_command(cinergyt2, (char *) param, sizeof(*param), NULL, 0);
1031 if (cinergyt2->streaming)
1032 cinergyt2_start_stream_xfer(cinergyt2);
1033 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
1036 cinergyt2_resume_rc(cinergyt2);
1038 mutex_unlock(&cinergyt2->sem);
1040 mutex_unlock(&cinergyt2->wq_sem);
1045 static const struct usb_device_id cinergyt2_table [] __devinitdata = {
1046 { USB_DEVICE(0x0ccd, 0x0038) },
1050 MODULE_DEVICE_TABLE(usb, cinergyt2_table);
1052 static struct usb_driver cinergyt2_driver = {
1053 .name = "cinergyT2",
1054 .probe = cinergyt2_probe,
1055 .disconnect = cinergyt2_disconnect,
1056 .suspend = cinergyt2_suspend,
1057 .resume = cinergyt2_resume,
1058 .id_table = cinergyt2_table
1061 static int __init cinergyt2_init (void)
1065 if ((err = usb_register(&cinergyt2_driver)) < 0)
1066 dprintk(1, "usb_register() failed! (err %i)\n", err);
1071 static void __exit cinergyt2_exit (void)
1073 usb_deregister(&cinergyt2_driver);
1076 module_init (cinergyt2_init);
1077 module_exit (cinergyt2_exit);
1079 MODULE_LICENSE("GPL");
1080 MODULE_AUTHOR("Holger Waechtler, Daniel Mack");