Merge kernel.org:/pub/scm/linux/kernel/git/mchehab/v4l-dvb.git with fixups
[linux-2.6] / drivers / media / dvb / cinergyT2 / cinergyT2.c
1 /*
2  * TerraTec Cinergy T²/qanu USB2 DVB-T adapter.
3  *
4  * Copyright (C) 2004 Daniel Mack <daniel@qanu.de> and
5  *                  Holger Waechtler <holger@qanu.de>
6  *
7  *  Protocol Spec published on http://qanu.de/specs/terratec_cinergyT2.pdf
8  *
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.
13  *
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.
18  *
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.
22  *
23  */
24
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>
34
35 #include "dmxdev.h"
36 #include "dvb_demux.h"
37 #include "dvb_net.h"
38
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)
45                 #define ENABLE_RC (1)
46         #endif
47 #else
48         #define STREAM_URB_COUNT (32)
49         #define STREAM_BUF_SIZE (512)   /* bytes */
50         #define ENABLE_RC (1)
51         #define RC_QUERY_INTERVAL (50)  /* milliseconds */
52         #define QUERY_INTERVAL (333)    /* milliseconds */
53 #endif
54
55 #define DRIVER_NAME "TerraTec/qanu USB2.0 Highspeed DVB-T Receiver"
56
57 static int debug;
58 module_param_named(debug, debug, int, 0644);
59 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
60
61 #define dprintk(level, args...)                                         \
62 do {                                                                    \
63         if ((debug & level)) {                                          \
64                 printk("%s: %s(): ", KBUILD_MODNAME,                    \
65                        __FUNCTION__);                                   \
66                 printk(args); }                                         \
67 } while (0)
68
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
79 };
80
81 struct dvbt_set_parameters_msg {
82         uint8_t cmd;
83         uint32_t freq;
84         uint8_t bandwidth;
85         uint16_t tps;
86         uint8_t flags;
87 } __attribute__((packed));
88
89 struct dvbt_get_status_msg {
90         uint32_t freq;
91         uint8_t bandwidth;
92         uint16_t tps;
93         uint8_t flags;
94         uint16_t gain;
95         uint8_t snr;
96         uint32_t viterbi_error_rate;
97         uint32_t rs_error_rate;
98         uint32_t uncorrected_block_count;
99         uint8_t lock_bits;
100         uint8_t prev_lock_bits;
101 } __attribute__((packed));
102
103 static struct dvb_frontend_info cinergyt2_fe_info = {
104         .name = DRIVER_NAME,
105         .type = FE_OFDM,
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 |
111                 FE_CAN_FEC_AUTO |
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
115 };
116
117 struct cinergyt2 {
118         struct dvb_demux demux;
119         struct usb_device *udev;
120         struct mutex sem;
121         struct dvb_adapter adapter;
122         struct dvb_device *fedev;
123         struct dmxdev dmxdev;
124         struct dvb_net dvbnet;
125
126         int streaming;
127         int sleeping;
128
129         struct dvbt_set_parameters_msg param;
130         struct dvbt_get_status_msg status;
131         struct work_struct query_work;
132
133         wait_queue_head_t poll_wq;
134         int pending_fe_events;
135         int disconnect_pending;
136         atomic_t inuse;
137
138         void *streambuf;
139         dma_addr_t streambuf_dmahandle;
140         struct urb *stream_urb [STREAM_URB_COUNT];
141
142 #ifdef ENABLE_RC
143         struct input_dev *rc_input_dev;
144         char phys[64];
145         struct work_struct rc_query_work;
146         int rc_input_event;
147         u32 rc_last_code;
148         unsigned long last_event_jiffies;
149 #endif
150 };
151
152 enum {
153         CINERGYT2_RC_EVENT_TYPE_NONE = 0x00,
154         CINERGYT2_RC_EVENT_TYPE_NEC  = 0x01,
155         CINERGYT2_RC_EVENT_TYPE_RC5  = 0x02
156 };
157
158 struct cinergyt2_rc_event {
159         char type;
160         uint32_t value;
161 } __attribute__((packed));
162
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
201 };
202
203 static int cinergyt2_command (struct cinergyt2 *cinergyt2,
204                               char *send_buf, int send_buf_len,
205                               char *recv_buf, int recv_buf_len)
206 {
207         int actual_len;
208         char dummy;
209         int ret;
210
211         ret = usb_bulk_msg(cinergyt2->udev, usb_sndbulkpipe(cinergyt2->udev, 1),
212                            send_buf, send_buf_len, &actual_len, 1000);
213
214         if (ret)
215                 dprintk(1, "usb_bulk_msg (send) failed, err %i\n", ret);
216
217         if (!recv_buf)
218                 recv_buf = &dummy;
219
220         ret = usb_bulk_msg(cinergyt2->udev, usb_rcvbulkpipe(cinergyt2->udev, 1),
221                            recv_buf, recv_buf_len, &actual_len, 1000);
222
223         if (ret)
224                 dprintk(1, "usb_bulk_msg (read) failed, err %i\n", ret);
225
226         return ret ? ret : actual_len;
227 }
228
229 static void cinergyt2_control_stream_transfer (struct cinergyt2 *cinergyt2, int enable)
230 {
231         char buf [] = { CINERGYT2_EP1_CONTROL_STREAM_TRANSFER, enable ? 1 : 0 };
232         cinergyt2_command(cinergyt2, buf, sizeof(buf), NULL, 0);
233 }
234
235 static void cinergyt2_sleep (struct cinergyt2 *cinergyt2, int sleep)
236 {
237         char buf [] = { CINERGYT2_EP1_SLEEP_MODE, sleep ? 1 : 0 };
238         cinergyt2_command(cinergyt2, buf, sizeof(buf), NULL, 0);
239         cinergyt2->sleeping = sleep;
240 }
241
242 static void cinergyt2_stream_irq (struct urb *urb, struct pt_regs *regs);
243
244 static int cinergyt2_submit_stream_urb (struct cinergyt2 *cinergyt2, struct urb *urb)
245 {
246         int err;
247
248         usb_fill_bulk_urb(urb,
249                           cinergyt2->udev,
250                           usb_rcvbulkpipe(cinergyt2->udev, 0x2),
251                           urb->transfer_buffer,
252                           STREAM_BUF_SIZE,
253                           cinergyt2_stream_irq,
254                           cinergyt2);
255
256         if ((err = usb_submit_urb(urb, GFP_ATOMIC)))
257                 dprintk(1, "urb submission failed (err = %i)!\n", err);
258
259         return err;
260 }
261
262 static void cinergyt2_stream_irq (struct urb *urb, struct pt_regs *regs)
263 {
264         struct cinergyt2 *cinergyt2 = urb->context;
265
266         if (urb->actual_length > 0)
267                 dvb_dmx_swfilter(&cinergyt2->demux,
268                                  urb->transfer_buffer, urb->actual_length);
269
270         if (cinergyt2->streaming)
271                 cinergyt2_submit_stream_urb(cinergyt2, urb);
272 }
273
274 static void cinergyt2_free_stream_urbs (struct cinergyt2 *cinergyt2)
275 {
276         int i;
277
278         for (i=0; i<STREAM_URB_COUNT; i++)
279                 if (cinergyt2->stream_urb[i])
280                         usb_free_urb(cinergyt2->stream_urb[i]);
281
282         usb_buffer_free(cinergyt2->udev, STREAM_URB_COUNT*STREAM_BUF_SIZE,
283                             cinergyt2->streambuf, cinergyt2->streambuf_dmahandle);
284 }
285
286 static int cinergyt2_alloc_stream_urbs (struct cinergyt2 *cinergyt2)
287 {
288         int i;
289
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");
294                 return -ENOMEM;
295         }
296
297         memset(cinergyt2->streambuf, 0, STREAM_URB_COUNT*STREAM_BUF_SIZE);
298
299         for (i=0; i<STREAM_URB_COUNT; i++) {
300                 struct urb *urb;
301
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);
305                         return -ENOMEM;
306                 }
307
308                 urb->transfer_buffer = cinergyt2->streambuf + i * STREAM_BUF_SIZE;
309                 urb->transfer_buffer_length = STREAM_BUF_SIZE;
310
311                 cinergyt2->stream_urb[i] = urb;
312         }
313
314         return 0;
315 }
316
317 static void cinergyt2_stop_stream_xfer (struct cinergyt2 *cinergyt2)
318 {
319         int i;
320
321         cinergyt2_control_stream_transfer(cinergyt2, 0);
322
323         for (i=0; i<STREAM_URB_COUNT; i++)
324                 if (cinergyt2->stream_urb[i])
325                         usb_kill_urb(cinergyt2->stream_urb[i]);
326 }
327
328 static int cinergyt2_start_stream_xfer (struct cinergyt2 *cinergyt2)
329 {
330         int i, err;
331
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);
336                         return err;
337                 }
338         }
339
340         cinergyt2_control_stream_transfer(cinergyt2, 1);
341         return 0;
342 }
343
344 static int cinergyt2_start_feed(struct dvb_demux_feed *dvbdmxfeed)
345 {
346         struct dvb_demux *demux = dvbdmxfeed->demux;
347         struct cinergyt2 *cinergyt2 = demux->priv;
348
349         if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
350                 return -ERESTARTSYS;
351
352         if (cinergyt2->streaming == 0)
353                 cinergyt2_start_stream_xfer(cinergyt2);
354
355         cinergyt2->streaming++;
356         mutex_unlock(&cinergyt2->sem);
357         return 0;
358 }
359
360 static int cinergyt2_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
361 {
362         struct dvb_demux *demux = dvbdmxfeed->demux;
363         struct cinergyt2 *cinergyt2 = demux->priv;
364
365         if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
366                 return -ERESTARTSYS;
367
368         if (--cinergyt2->streaming == 0)
369                 cinergyt2_stop_stream_xfer(cinergyt2);
370
371         mutex_unlock(&cinergyt2->sem);
372         return 0;
373 }
374
375 /**
376  *  convert linux-dvb frontend parameter set into TPS.
377  *  See ETSI ETS-300744, section 4.6.2, table 9 for details.
378  *
379  *  This function is probably reusable and may better get placed in a support
380  *  library.
381  *
382  *  We replace errornous fields by default TPS fields (the ones with value 0).
383  */
384 static uint16_t compute_tps (struct dvb_frontend_parameters *p)
385 {
386         struct dvb_ofdm_parameters *op = &p->u.ofdm;
387         uint16_t tps = 0;
388
389         switch (op->code_rate_HP) {
390                 case FEC_2_3:
391                         tps |= (1 << 7);
392                         break;
393                 case FEC_3_4:
394                         tps |= (2 << 7);
395                         break;
396                 case FEC_5_6:
397                         tps |= (3 << 7);
398                         break;
399                 case FEC_7_8:
400                         tps |= (4 << 7);
401                         break;
402                 case FEC_1_2:
403                 case FEC_AUTO:
404                 default:
405                         /* tps |= (0 << 7) */;
406         }
407
408         switch (op->code_rate_LP) {
409                 case FEC_2_3:
410                         tps |= (1 << 4);
411                         break;
412                 case FEC_3_4:
413                         tps |= (2 << 4);
414                         break;
415                 case FEC_5_6:
416                         tps |= (3 << 4);
417                         break;
418                 case FEC_7_8:
419                         tps |= (4 << 4);
420                         break;
421                 case FEC_1_2:
422                 case FEC_AUTO:
423                 default:
424                         /* tps |= (0 << 4) */;
425         }
426
427         switch (op->constellation) {
428                 case QAM_16:
429                         tps |= (1 << 13);
430                         break;
431                 case QAM_64:
432                         tps |= (2 << 13);
433                         break;
434                 case QPSK:
435                 default:
436                         /* tps |= (0 << 13) */;
437         }
438
439         switch (op->transmission_mode) {
440                 case TRANSMISSION_MODE_8K:
441                         tps |= (1 << 0);
442                         break;
443                 case TRANSMISSION_MODE_2K:
444                 default:
445                         /* tps |= (0 << 0) */;
446         }
447
448         switch (op->guard_interval) {
449                 case GUARD_INTERVAL_1_16:
450                         tps |= (1 << 2);
451                         break;
452                 case GUARD_INTERVAL_1_8:
453                         tps |= (2 << 2);
454                         break;
455                 case GUARD_INTERVAL_1_4:
456                         tps |= (3 << 2);
457                         break;
458                 case GUARD_INTERVAL_1_32:
459                 default:
460                         /* tps |= (0 << 2) */;
461         }
462
463         switch (op->hierarchy_information) {
464                 case HIERARCHY_1:
465                         tps |= (1 << 10);
466                         break;
467                 case HIERARCHY_2:
468                         tps |= (2 << 10);
469                         break;
470                 case HIERARCHY_4:
471                         tps |= (3 << 10);
472                         break;
473                 case HIERARCHY_NONE:
474                 default:
475                         /* tps |= (0 << 10) */;
476         }
477
478         return tps;
479 }
480
481 static int cinergyt2_open (struct inode *inode, struct file *file)
482 {
483         struct dvb_device *dvbdev = file->private_data;
484         struct cinergyt2 *cinergyt2 = dvbdev->priv;
485         int err = -ERESTARTSYS;
486
487         if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
488                 return -ERESTARTSYS;
489
490         if ((err = dvb_generic_open(inode, file))) {
491                 mutex_unlock(&cinergyt2->sem);
492                 return err;
493         }
494
495
496         if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
497                 cinergyt2_sleep(cinergyt2, 0);
498                 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
499         }
500
501         atomic_inc(&cinergyt2->inuse);
502
503         mutex_unlock(&cinergyt2->sem);
504         return 0;
505 }
506
507 static void cinergyt2_unregister(struct cinergyt2 *cinergyt2)
508 {
509         dvb_net_release(&cinergyt2->dvbnet);
510         dvb_dmxdev_release(&cinergyt2->dmxdev);
511         dvb_dmx_release(&cinergyt2->demux);
512         dvb_unregister_device(cinergyt2->fedev);
513         dvb_unregister_adapter(&cinergyt2->adapter);
514
515         cinergyt2_free_stream_urbs(cinergyt2);
516         kfree(cinergyt2);
517 }
518
519 static int cinergyt2_release (struct inode *inode, struct file *file)
520 {
521         struct dvb_device *dvbdev = file->private_data;
522         struct cinergyt2 *cinergyt2 = dvbdev->priv;
523
524         if (mutex_lock_interruptible(&cinergyt2->sem))
525                 return -ERESTARTSYS;
526
527         if (!cinergyt2->disconnect_pending && (file->f_flags & O_ACCMODE) != O_RDONLY) {
528                 cancel_delayed_work(&cinergyt2->query_work);
529                 flush_scheduled_work();
530                 cinergyt2_sleep(cinergyt2, 1);
531         }
532
533         mutex_unlock(&cinergyt2->sem);
534
535         if (atomic_dec_and_test(&cinergyt2->inuse) && cinergyt2->disconnect_pending) {
536                 warn("delayed unregister in release");
537                 cinergyt2_unregister(cinergyt2);
538         }
539
540         return dvb_generic_release(inode, file);
541 }
542
543 static unsigned int cinergyt2_poll (struct file *file, struct poll_table_struct *wait)
544 {
545         struct dvb_device *dvbdev = file->private_data;
546         struct cinergyt2 *cinergyt2 = dvbdev->priv;
547
548         if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
549                 return -ERESTARTSYS;
550
551         poll_wait(file, &cinergyt2->poll_wq, wait);
552
553         mutex_unlock(&cinergyt2->sem);
554
555         return (POLLIN | POLLRDNORM | POLLPRI);
556 }
557
558
559 static int cinergyt2_ioctl (struct inode *inode, struct file *file,
560                      unsigned cmd, unsigned long arg)
561 {
562         struct dvb_device *dvbdev = file->private_data;
563         struct cinergyt2 *cinergyt2 = dvbdev->priv;
564         struct dvbt_get_status_msg *stat = &cinergyt2->status;
565         fe_status_t status = 0;
566
567         switch (cmd) {
568         case FE_GET_INFO:
569                 return copy_to_user((void __user*) arg, &cinergyt2_fe_info,
570                                     sizeof(struct dvb_frontend_info));
571
572         case FE_READ_STATUS:
573                 if (0xffff - le16_to_cpu(stat->gain) > 30)
574                         status |= FE_HAS_SIGNAL;
575                 if (stat->lock_bits & (1 << 6))
576                         status |= FE_HAS_LOCK;
577                 if (stat->lock_bits & (1 << 5))
578                         status |= FE_HAS_SYNC;
579                 if (stat->lock_bits & (1 << 4))
580                         status |= FE_HAS_CARRIER;
581                 if (stat->lock_bits & (1 << 1))
582                         status |= FE_HAS_VITERBI;
583
584                 return copy_to_user((void  __user*) arg, &status, sizeof(status));
585
586         case FE_READ_BER:
587                 return put_user(le32_to_cpu(stat->viterbi_error_rate),
588                                 (__u32 __user *) arg);
589
590         case FE_READ_SIGNAL_STRENGTH:
591                 return put_user(0xffff - le16_to_cpu(stat->gain),
592                                 (__u16 __user *) arg);
593
594         case FE_READ_SNR:
595                 return put_user((stat->snr << 8) | stat->snr,
596                                 (__u16 __user *) arg);
597
598         case FE_READ_UNCORRECTED_BLOCKS:
599         {
600                 uint32_t unc_count;
601
602                 unc_count = stat->uncorrected_block_count;
603                 stat->uncorrected_block_count = 0;
604
605                 /* UNC are already converted to host byte order... */
606                 return put_user(unc_count,(__u32 __user *) arg);
607         }
608         case FE_SET_FRONTEND:
609         {
610                 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
611                 struct dvb_frontend_parameters p;
612                 int err;
613
614                 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
615                         return -EPERM;
616
617                 if (copy_from_user(&p, (void  __user*) arg, sizeof(p)))
618                         return -EFAULT;
619
620                 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
621                         return -ERESTARTSYS;
622
623                 param->cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
624                 param->tps = cpu_to_le16(compute_tps(&p));
625                 param->freq = cpu_to_le32(p.frequency / 1000);
626                 param->bandwidth = 8 - p.u.ofdm.bandwidth - BANDWIDTH_8_MHZ;
627
628                 stat->lock_bits = 0;
629                 cinergyt2->pending_fe_events++;
630                 wake_up_interruptible(&cinergyt2->poll_wq);
631
632                 err = cinergyt2_command(cinergyt2,
633                                         (char *) param, sizeof(*param),
634                                         NULL, 0);
635
636                 mutex_unlock(&cinergyt2->sem);
637
638                 return (err < 0) ? err : 0;
639         }
640
641         case FE_GET_FRONTEND:
642                 /**
643                  *  trivial to implement (see struct dvbt_get_status_msg).
644                  *  equivalent to FE_READ ioctls, but needs
645                  *  TPS -> linux-dvb parameter set conversion. Feel free
646                  *  to implement this and send us a patch if you need this
647                  *  functionality.
648                  */
649                 break;
650
651         case FE_GET_EVENT:
652         {
653                 /**
654                  *  for now we only fill the status field. the parameters
655                  *  are trivial to fill as soon FE_GET_FRONTEND is done.
656                  */
657                 struct dvb_frontend_event __user *e = (void __user *) arg;
658                 if (cinergyt2->pending_fe_events == 0) {
659                         if (file->f_flags & O_NONBLOCK)
660                                 return -EWOULDBLOCK;
661                         wait_event_interruptible(cinergyt2->poll_wq,
662                                                  cinergyt2->pending_fe_events > 0);
663                 }
664                 cinergyt2->pending_fe_events = 0;
665                 return cinergyt2_ioctl(inode, file, FE_READ_STATUS,
666                                         (unsigned long) &e->status);
667         }
668
669         default:
670                 ;
671         }
672
673         return -EINVAL;
674 }
675
676 static int cinergyt2_mmap(struct file *file, struct vm_area_struct *vma)
677 {
678         struct dvb_device *dvbdev = file->private_data;
679         struct cinergyt2 *cinergyt2 = dvbdev->priv;
680         int ret = 0;
681
682         lock_kernel();
683
684         if (vma->vm_flags & (VM_WRITE | VM_EXEC)) {
685                 ret = -EPERM;
686                 goto bailout;
687         }
688
689         if (vma->vm_end > vma->vm_start + STREAM_URB_COUNT * STREAM_BUF_SIZE) {
690                 ret = -EINVAL;
691                 goto bailout;
692         }
693
694         vma->vm_flags |= (VM_IO | VM_DONTCOPY);
695         vma->vm_file = file;
696
697         ret = remap_pfn_range(vma, vma->vm_start,
698                               virt_to_phys(cinergyt2->streambuf) >> PAGE_SHIFT,
699                               vma->vm_end - vma->vm_start,
700                               vma->vm_page_prot) ? -EAGAIN : 0;
701 bailout:
702         unlock_kernel();
703         return ret;
704 }
705
706 static struct file_operations cinergyt2_fops = {
707         .owner          = THIS_MODULE,
708         .ioctl          = cinergyt2_ioctl,
709         .poll           = cinergyt2_poll,
710         .open           = cinergyt2_open,
711         .release        = cinergyt2_release,
712         .mmap           = cinergyt2_mmap
713 };
714
715 static struct dvb_device cinergyt2_fe_template = {
716         .users = ~0,
717         .writers = 1,
718         .readers = (~0)-1,
719         .fops = &cinergyt2_fops
720 };
721
722 #ifdef ENABLE_RC
723
724 static void cinergyt2_query_rc (void *data)
725 {
726         struct cinergyt2 *cinergyt2 = data;
727         char buf[1] = { CINERGYT2_EP1_GET_RC_EVENTS };
728         struct cinergyt2_rc_event rc_events[12];
729         int n, len, i;
730
731         if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
732                 return;
733
734         len = cinergyt2_command(cinergyt2, buf, sizeof(buf),
735                                 (char *) rc_events, sizeof(rc_events));
736         if (len < 0)
737                 goto out;
738         if (len == 0) {
739                 if (time_after(jiffies, cinergyt2->last_event_jiffies +
740                                msecs_to_jiffies(150))) {
741                         /* stop key repeat */
742                         if (cinergyt2->rc_input_event != KEY_MAX) {
743                                 dprintk(1, "rc_input_event=%d Up\n", cinergyt2->rc_input_event);
744                                 input_report_key(cinergyt2->rc_input_dev,
745                                                  cinergyt2->rc_input_event, 0);
746                                 cinergyt2->rc_input_event = KEY_MAX;
747                         }
748                         cinergyt2->rc_last_code = ~0;
749                 }
750                 goto out;
751         }
752         cinergyt2->last_event_jiffies = jiffies;
753
754         for (n = 0; n < (len / sizeof(rc_events[0])); n++) {
755                 dprintk(1, "rc_events[%d].value = %x, type=%x\n",
756                         n, le32_to_cpu(rc_events[n].value), rc_events[n].type);
757
758                 if (rc_events[n].type == CINERGYT2_RC_EVENT_TYPE_NEC &&
759                     rc_events[n].value == ~0) {
760                         /* keyrepeat bit -> just repeat last rc_input_event */
761                 } else {
762                         cinergyt2->rc_input_event = KEY_MAX;
763                         for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3) {
764                                 if (rc_keys[i + 0] == rc_events[n].type &&
765                                     rc_keys[i + 1] == le32_to_cpu(rc_events[n].value)) {
766                                         cinergyt2->rc_input_event = rc_keys[i + 2];
767                                         break;
768                                 }
769                         }
770                 }
771
772                 if (cinergyt2->rc_input_event != KEY_MAX) {
773                         if (rc_events[n].value == cinergyt2->rc_last_code &&
774                             cinergyt2->rc_last_code != ~0) {
775                                 /* emit a key-up so the double event is recognized */
776                                 dprintk(1, "rc_input_event=%d UP\n", cinergyt2->rc_input_event);
777                                 input_report_key(cinergyt2->rc_input_dev,
778                                                  cinergyt2->rc_input_event, 0);
779                         }
780                         dprintk(1, "rc_input_event=%d\n", cinergyt2->rc_input_event);
781                         input_report_key(cinergyt2->rc_input_dev,
782                                          cinergyt2->rc_input_event, 1);
783                         cinergyt2->rc_last_code = rc_events[n].value;
784                 }
785         }
786
787 out:
788         schedule_delayed_work(&cinergyt2->rc_query_work,
789                               msecs_to_jiffies(RC_QUERY_INTERVAL));
790
791         mutex_unlock(&cinergyt2->sem);
792 }
793
794 static int cinergyt2_register_rc(struct cinergyt2 *cinergyt2)
795 {
796         struct input_dev *input_dev;
797         int i;
798
799         cinergyt2->rc_input_dev = input_dev = input_allocate_device();
800         if (!input_dev)
801                 return -ENOMEM;
802
803         usb_make_path(cinergyt2->udev, cinergyt2->phys, sizeof(cinergyt2->phys));
804         strlcat(cinergyt2->phys, "/input0", sizeof(cinergyt2->phys));
805         cinergyt2->rc_input_event = KEY_MAX;
806         cinergyt2->rc_last_code = ~0;
807         INIT_WORK(&cinergyt2->rc_query_work, cinergyt2_query_rc, cinergyt2);
808
809         input_dev->name = DRIVER_NAME " remote control";
810         input_dev->phys = cinergyt2->phys;
811         input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
812         for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3)
813                 set_bit(rc_keys[i + 2], input_dev->keybit);
814         input_dev->keycodesize = 0;
815         input_dev->keycodemax = 0;
816
817         input_register_device(cinergyt2->rc_input_dev);
818         schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
819
820         return 0;
821 }
822
823 static void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2)
824 {
825         cancel_delayed_work(&cinergyt2->rc_query_work);
826         input_unregister_device(cinergyt2->rc_input_dev);
827 }
828
829 static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2)
830 {
831         cancel_delayed_work(&cinergyt2->rc_query_work);
832 }
833
834 static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2)
835 {
836         schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
837 }
838
839 #else
840
841 static inline int cinergyt2_register_rc(struct cinergyt2 *cinergyt2) { return 0; }
842 static inline void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2) { }
843 static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2) { }
844 static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2) { }
845
846 #endif /* ENABLE_RC */
847
848 static void cinergyt2_query (void *data)
849 {
850         struct cinergyt2 *cinergyt2 = (struct cinergyt2 *) data;
851         char cmd [] = { CINERGYT2_EP1_GET_TUNER_STATUS };
852         struct dvbt_get_status_msg *s = &cinergyt2->status;
853         uint8_t lock_bits;
854         uint32_t unc;
855
856         if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
857                 return;
858
859         unc = s->uncorrected_block_count;
860         lock_bits = s->lock_bits;
861
862         cinergyt2_command(cinergyt2, cmd, sizeof(cmd), (char *) s, sizeof(*s));
863
864         unc += le32_to_cpu(s->uncorrected_block_count);
865         s->uncorrected_block_count = unc;
866
867         if (lock_bits != s->lock_bits) {
868                 wake_up_interruptible(&cinergyt2->poll_wq);
869                 cinergyt2->pending_fe_events++;
870         }
871
872         schedule_delayed_work(&cinergyt2->query_work,
873                               msecs_to_jiffies(QUERY_INTERVAL));
874
875         mutex_unlock(&cinergyt2->sem);
876 }
877
878 static int cinergyt2_probe (struct usb_interface *intf,
879                   const struct usb_device_id *id)
880 {
881         struct cinergyt2 *cinergyt2;
882         int err;
883
884         if (!(cinergyt2 = kmalloc (sizeof(struct cinergyt2), GFP_KERNEL))) {
885                 dprintk(1, "out of memory?!?\n");
886                 return -ENOMEM;
887         }
888
889         memset (cinergyt2, 0, sizeof (struct cinergyt2));
890         usb_set_intfdata (intf, (void *) cinergyt2);
891
892         mutex_init(&cinergyt2->sem);
893         init_waitqueue_head (&cinergyt2->poll_wq);
894         INIT_WORK(&cinergyt2->query_work, cinergyt2_query, cinergyt2);
895
896         cinergyt2->udev = interface_to_usbdev(intf);
897         cinergyt2->param.cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
898
899         if (cinergyt2_alloc_stream_urbs (cinergyt2) < 0) {
900                 dprintk(1, "unable to allocate stream urbs\n");
901                 kfree(cinergyt2);
902                 return -ENOMEM;
903         }
904
905         dvb_register_adapter(&cinergyt2->adapter, DRIVER_NAME, THIS_MODULE);
906
907         cinergyt2->demux.priv = cinergyt2;
908         cinergyt2->demux.filternum = 256;
909         cinergyt2->demux.feednum = 256;
910         cinergyt2->demux.start_feed = cinergyt2_start_feed;
911         cinergyt2->demux.stop_feed = cinergyt2_stop_feed;
912         cinergyt2->demux.dmx.capabilities = DMX_TS_FILTERING |
913                                             DMX_SECTION_FILTERING |
914                                             DMX_MEMORY_BASED_FILTERING;
915
916         if ((err = dvb_dmx_init(&cinergyt2->demux)) < 0) {
917                 dprintk(1, "dvb_dmx_init() failed (err = %d)\n", err);
918                 goto bailout;
919         }
920
921         cinergyt2->dmxdev.filternum = cinergyt2->demux.filternum;
922         cinergyt2->dmxdev.demux = &cinergyt2->demux.dmx;
923         cinergyt2->dmxdev.capabilities = 0;
924
925         if ((err = dvb_dmxdev_init(&cinergyt2->dmxdev, &cinergyt2->adapter)) < 0) {
926                 dprintk(1, "dvb_dmxdev_init() failed (err = %d)\n", err);
927                 goto bailout;
928         }
929
930         if (dvb_net_init(&cinergyt2->adapter, &cinergyt2->dvbnet, &cinergyt2->demux.dmx))
931                 dprintk(1, "dvb_net_init() failed!\n");
932
933         dvb_register_device(&cinergyt2->adapter, &cinergyt2->fedev,
934                             &cinergyt2_fe_template, cinergyt2,
935                             DVB_DEVICE_FRONTEND);
936
937         err = cinergyt2_register_rc(cinergyt2);
938         if (err)
939                 goto bailout;
940
941         return 0;
942
943 bailout:
944         dvb_net_release(&cinergyt2->dvbnet);
945         dvb_dmxdev_release(&cinergyt2->dmxdev);
946         dvb_dmx_release(&cinergyt2->demux);
947         dvb_unregister_adapter(&cinergyt2->adapter);
948         cinergyt2_free_stream_urbs(cinergyt2);
949         kfree(cinergyt2);
950         return -ENOMEM;
951 }
952
953 static void cinergyt2_disconnect (struct usb_interface *intf)
954 {
955         struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
956
957         flush_scheduled_work();
958
959         cinergyt2_unregister_rc(cinergyt2);
960
961         cancel_delayed_work(&cinergyt2->query_work);
962         wake_up_interruptible(&cinergyt2->poll_wq);
963
964         cinergyt2->demux.dmx.close(&cinergyt2->demux.dmx);
965         cinergyt2->disconnect_pending = 1;
966
967         if (!atomic_read(&cinergyt2->inuse))
968                 cinergyt2_unregister(cinergyt2);
969 }
970
971 static int cinergyt2_suspend (struct usb_interface *intf, pm_message_t state)
972 {
973         struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
974
975         if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
976                 return -ERESTARTSYS;
977
978         if (state.event > PM_EVENT_ON) {
979                 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
980
981                 cinergyt2_suspend_rc(cinergyt2);
982                 cancel_delayed_work(&cinergyt2->query_work);
983                 if (cinergyt2->streaming)
984                         cinergyt2_stop_stream_xfer(cinergyt2);
985                 flush_scheduled_work();
986                 cinergyt2_sleep(cinergyt2, 1);
987         }
988
989         mutex_unlock(&cinergyt2->sem);
990         return 0;
991 }
992
993 static int cinergyt2_resume (struct usb_interface *intf)
994 {
995         struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
996         struct dvbt_set_parameters_msg *param = &cinergyt2->param;
997
998         if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
999                 return -ERESTARTSYS;
1000
1001         if (!cinergyt2->sleeping) {
1002                 cinergyt2_sleep(cinergyt2, 0);
1003                 cinergyt2_command(cinergyt2, (char *) param, sizeof(*param), NULL, 0);
1004                 if (cinergyt2->streaming)
1005                         cinergyt2_start_stream_xfer(cinergyt2);
1006                 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
1007         }
1008
1009         cinergyt2_resume_rc(cinergyt2);
1010
1011         mutex_unlock(&cinergyt2->sem);
1012         return 0;
1013 }
1014
1015 static const struct usb_device_id cinergyt2_table [] __devinitdata = {
1016         { USB_DEVICE(0x0ccd, 0x0038) },
1017         { 0 }
1018 };
1019
1020 MODULE_DEVICE_TABLE(usb, cinergyt2_table);
1021
1022 static struct usb_driver cinergyt2_driver = {
1023         .name   = "cinergyT2",
1024         .probe  = cinergyt2_probe,
1025         .disconnect     = cinergyt2_disconnect,
1026         .suspend        = cinergyt2_suspend,
1027         .resume         = cinergyt2_resume,
1028         .id_table       = cinergyt2_table
1029 };
1030
1031 static int __init cinergyt2_init (void)
1032 {
1033         int err;
1034
1035         if ((err = usb_register(&cinergyt2_driver)) < 0)
1036                 dprintk(1, "usb_register() failed! (err %i)\n", err);
1037
1038         return err;
1039 }
1040
1041 static void __exit cinergyt2_exit (void)
1042 {
1043         usb_deregister(&cinergyt2_driver);
1044 }
1045
1046 module_init (cinergyt2_init);
1047 module_exit (cinergyt2_exit);
1048
1049 MODULE_LICENSE("GPL");
1050 MODULE_AUTHOR("Holger Waechtler, Daniel Mack");