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