Merge git://git.kernel.org/pub/scm/linux/kernel/git/sam/kbuild
[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/input.h>
30 #include <linux/dvb/frontend.h>
31 #include <linux/mutex.h>
32 #include <linux/mm.h>
33 #include <asm/io.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 mutex wq_sem;
122         struct dvb_adapter adapter;
123         struct dvb_device *fedev;
124         struct dmxdev dmxdev;
125         struct dvb_net dvbnet;
126
127         int streaming;
128         int sleeping;
129
130         struct dvbt_set_parameters_msg param;
131         struct dvbt_get_status_msg status;
132         struct delayed_work query_work;
133
134         wait_queue_head_t poll_wq;
135         int pending_fe_events;
136         int disconnect_pending;
137         atomic_t inuse;
138
139         void *streambuf;
140         dma_addr_t streambuf_dmahandle;
141         struct urb *stream_urb [STREAM_URB_COUNT];
142
143 #ifdef ENABLE_RC
144         struct input_dev *rc_input_dev;
145         char phys[64];
146         struct delayed_work rc_query_work;
147         int rc_input_event;
148         u32 rc_last_code;
149         unsigned long last_event_jiffies;
150 #endif
151 };
152
153 enum {
154         CINERGYT2_RC_EVENT_TYPE_NONE = 0x00,
155         CINERGYT2_RC_EVENT_TYPE_NEC  = 0x01,
156         CINERGYT2_RC_EVENT_TYPE_RC5  = 0x02
157 };
158
159 struct cinergyt2_rc_event {
160         char type;
161         uint32_t value;
162 } __attribute__((packed));
163
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
202 };
203
204 static int cinergyt2_command (struct cinergyt2 *cinergyt2,
205                               char *send_buf, int send_buf_len,
206                               char *recv_buf, int recv_buf_len)
207 {
208         int actual_len;
209         char dummy;
210         int ret;
211
212         ret = usb_bulk_msg(cinergyt2->udev, usb_sndbulkpipe(cinergyt2->udev, 1),
213                            send_buf, send_buf_len, &actual_len, 1000);
214
215         if (ret)
216                 dprintk(1, "usb_bulk_msg (send) failed, err %i\n", ret);
217
218         if (!recv_buf)
219                 recv_buf = &dummy;
220
221         ret = usb_bulk_msg(cinergyt2->udev, usb_rcvbulkpipe(cinergyt2->udev, 1),
222                            recv_buf, recv_buf_len, &actual_len, 1000);
223
224         if (ret)
225                 dprintk(1, "usb_bulk_msg (read) failed, err %i\n", ret);
226
227         return ret ? ret : actual_len;
228 }
229
230 static void cinergyt2_control_stream_transfer (struct cinergyt2 *cinergyt2, int enable)
231 {
232         char buf [] = { CINERGYT2_EP1_CONTROL_STREAM_TRANSFER, enable ? 1 : 0 };
233         cinergyt2_command(cinergyt2, buf, sizeof(buf), NULL, 0);
234 }
235
236 static void cinergyt2_sleep (struct cinergyt2 *cinergyt2, int sleep)
237 {
238         char buf [] = { CINERGYT2_EP1_SLEEP_MODE, sleep ? 1 : 0 };
239         cinergyt2_command(cinergyt2, buf, sizeof(buf), NULL, 0);
240         cinergyt2->sleeping = sleep;
241 }
242
243 static void cinergyt2_stream_irq (struct urb *urb);
244
245 static int cinergyt2_submit_stream_urb (struct cinergyt2 *cinergyt2, struct urb *urb)
246 {
247         int err;
248
249         usb_fill_bulk_urb(urb,
250                           cinergyt2->udev,
251                           usb_rcvbulkpipe(cinergyt2->udev, 0x2),
252                           urb->transfer_buffer,
253                           STREAM_BUF_SIZE,
254                           cinergyt2_stream_irq,
255                           cinergyt2);
256
257         if ((err = usb_submit_urb(urb, GFP_ATOMIC)))
258                 dprintk(1, "urb submission failed (err = %i)!\n", err);
259
260         return err;
261 }
262
263 static void cinergyt2_stream_irq (struct urb *urb)
264 {
265         struct cinergyt2 *cinergyt2 = urb->context;
266
267         if (urb->actual_length > 0)
268                 dvb_dmx_swfilter(&cinergyt2->demux,
269                                  urb->transfer_buffer, urb->actual_length);
270
271         if (cinergyt2->streaming)
272                 cinergyt2_submit_stream_urb(cinergyt2, urb);
273 }
274
275 static void cinergyt2_free_stream_urbs (struct cinergyt2 *cinergyt2)
276 {
277         int i;
278
279         for (i=0; i<STREAM_URB_COUNT; 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                                               GFP_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                 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->wq_sem))
487                 goto out;
488
489         if (mutex_lock_interruptible(&cinergyt2->sem))
490                 goto out_unlock1;
491
492         if ((err = dvb_generic_open(inode, file)))
493                 goto out_unlock2;
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 out_unlock2:
503         mutex_unlock(&cinergyt2->sem);
504 out_unlock1:
505         mutex_unlock(&cinergyt2->wq_sem);
506 out:
507         return err;
508 }
509
510 static void cinergyt2_unregister(struct cinergyt2 *cinergyt2)
511 {
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);
517
518         cinergyt2_free_stream_urbs(cinergyt2);
519         kfree(cinergyt2);
520 }
521
522 static int cinergyt2_release (struct inode *inode, struct file *file)
523 {
524         struct dvb_device *dvbdev = file->private_data;
525         struct cinergyt2 *cinergyt2 = dvbdev->priv;
526
527         mutex_lock(&cinergyt2->wq_sem);
528
529         if (!cinergyt2->disconnect_pending && (file->f_flags & O_ACCMODE) != O_RDONLY) {
530                 cancel_rearming_delayed_work(&cinergyt2->query_work);
531
532                 mutex_lock(&cinergyt2->sem);
533                 cinergyt2_sleep(cinergyt2, 1);
534                 mutex_unlock(&cinergyt2->sem);
535         }
536
537         mutex_unlock(&cinergyt2->wq_sem);
538
539         if (atomic_dec_and_test(&cinergyt2->inuse) && cinergyt2->disconnect_pending) {
540                 warn("delayed unregister in release");
541                 cinergyt2_unregister(cinergyt2);
542         }
543
544         return dvb_generic_release(inode, file);
545 }
546
547 static unsigned int cinergyt2_poll (struct file *file, struct poll_table_struct *wait)
548 {
549         struct dvb_device *dvbdev = file->private_data;
550         struct cinergyt2 *cinergyt2 = dvbdev->priv;
551        unsigned int mask = 0;
552
553         if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
554                 return -ERESTARTSYS;
555
556         poll_wait(file, &cinergyt2->poll_wq, wait);
557
558        if (cinergyt2->pending_fe_events != 0)
559                 mask |= (POLLIN | POLLRDNORM | POLLPRI);
560
561         mutex_unlock(&cinergyt2->sem);
562
563        return mask;
564 }
565
566
567 static int cinergyt2_ioctl (struct inode *inode, struct file *file,
568                      unsigned cmd, unsigned long arg)
569 {
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;
574
575         switch (cmd) {
576         case FE_GET_INFO:
577                 return copy_to_user((void __user*) arg, &cinergyt2_fe_info,
578                                     sizeof(struct dvb_frontend_info));
579
580         case FE_READ_STATUS:
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;
591
592                 return copy_to_user((void  __user*) arg, &status, sizeof(status));
593
594         case FE_READ_BER:
595                 return put_user(le32_to_cpu(stat->viterbi_error_rate),
596                                 (__u32 __user *) arg);
597
598         case FE_READ_SIGNAL_STRENGTH:
599                 return put_user(0xffff - le16_to_cpu(stat->gain),
600                                 (__u16 __user *) arg);
601
602         case FE_READ_SNR:
603                 return put_user((stat->snr << 8) | stat->snr,
604                                 (__u16 __user *) arg);
605
606         case FE_READ_UNCORRECTED_BLOCKS:
607         {
608                 uint32_t unc_count;
609
610                 unc_count = stat->uncorrected_block_count;
611                 stat->uncorrected_block_count = 0;
612
613                 /* UNC are already converted to host byte order... */
614                 return put_user(unc_count,(__u32 __user *) arg);
615         }
616         case FE_SET_FRONTEND:
617         {
618                 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
619                 struct dvb_frontend_parameters p;
620                 int err;
621
622                 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
623                         return -EPERM;
624
625                 if (copy_from_user(&p, (void  __user*) arg, sizeof(p)))
626                         return -EFAULT;
627
628                 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
629                         return -ERESTARTSYS;
630
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;
635
636                 stat->lock_bits = 0;
637                 cinergyt2->pending_fe_events++;
638                 wake_up_interruptible(&cinergyt2->poll_wq);
639
640                 err = cinergyt2_command(cinergyt2,
641                                         (char *) param, sizeof(*param),
642                                         NULL, 0);
643
644                 mutex_unlock(&cinergyt2->sem);
645
646                 return (err < 0) ? err : 0;
647         }
648
649         case FE_GET_FRONTEND:
650                 /**
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
655                  *  functionality.
656                  */
657                 break;
658
659         case FE_GET_EVENT:
660         {
661                 /**
662                  *  for now we only fill the status field. the parameters
663                  *  are trivial to fill as soon FE_GET_FRONTEND is done.
664                  */
665                 struct dvb_frontend_event __user *e = (void __user *) arg;
666                 if (cinergyt2->pending_fe_events == 0) {
667                         if (file->f_flags & O_NONBLOCK)
668                                 return -EWOULDBLOCK;
669                         wait_event_interruptible(cinergyt2->poll_wq,
670                                                  cinergyt2->pending_fe_events > 0);
671                 }
672                 cinergyt2->pending_fe_events = 0;
673                 return cinergyt2_ioctl(inode, file, FE_READ_STATUS,
674                                         (unsigned long) &e->status);
675         }
676
677         default:
678                 ;
679         }
680
681         return -EINVAL;
682 }
683
684 static int cinergyt2_mmap(struct file *file, struct vm_area_struct *vma)
685 {
686         struct dvb_device *dvbdev = file->private_data;
687         struct cinergyt2 *cinergyt2 = dvbdev->priv;
688         int ret = 0;
689
690         lock_kernel();
691
692         if (vma->vm_flags & (VM_WRITE | VM_EXEC)) {
693                 ret = -EPERM;
694                 goto bailout;
695         }
696
697         if (vma->vm_end > vma->vm_start + STREAM_URB_COUNT * STREAM_BUF_SIZE) {
698                 ret = -EINVAL;
699                 goto bailout;
700         }
701
702         vma->vm_flags |= (VM_IO | VM_DONTCOPY);
703         vma->vm_file = file;
704
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;
709 bailout:
710         unlock_kernel();
711         return ret;
712 }
713
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
721 };
722
723 static struct dvb_device cinergyt2_fe_template = {
724         .users = ~0,
725         .writers = 1,
726         .readers = (~0)-1,
727         .fops = &cinergyt2_fops
728 };
729
730 #ifdef ENABLE_RC
731
732 static void cinergyt2_query_rc (struct work_struct *work)
733 {
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];
738         int n, len, i;
739
740         if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
741                 return;
742
743         len = cinergyt2_command(cinergyt2, buf, sizeof(buf),
744                                 (char *) rc_events, sizeof(rc_events));
745         if (len < 0)
746                 goto out;
747         if (len == 0) {
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;
757                         }
758                         cinergyt2->rc_last_code = ~0;
759                 }
760                 goto out;
761         }
762         cinergyt2->last_event_jiffies = jiffies;
763
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);
767
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 */
771                 } else {
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];
777                                         break;
778                                 }
779                         }
780                 }
781
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);
789                         }
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;
795                 }
796         }
797
798 out:
799         schedule_delayed_work(&cinergyt2->rc_query_work,
800                               msecs_to_jiffies(RC_QUERY_INTERVAL));
801
802         mutex_unlock(&cinergyt2->sem);
803 }
804
805 static int cinergyt2_register_rc(struct cinergyt2 *cinergyt2)
806 {
807         struct input_dev *input_dev;
808         int i;
809         int err;
810
811         input_dev = input_allocate_device();
812         if (!input_dev)
813                 return -ENOMEM;
814
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);
820
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;
833
834         err = input_register_device(input_dev);
835         if (err) {
836                 input_free_device(input_dev);
837                 return err;
838         }
839
840         cinergyt2->rc_input_dev = input_dev;
841         schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
842
843         return 0;
844 }
845
846 static void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2)
847 {
848         cancel_rearming_delayed_work(&cinergyt2->rc_query_work);
849         input_unregister_device(cinergyt2->rc_input_dev);
850 }
851
852 static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2)
853 {
854         cancel_rearming_delayed_work(&cinergyt2->rc_query_work);
855 }
856
857 static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2)
858 {
859         schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
860 }
861
862 #else
863
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) { }
868
869 #endif /* ENABLE_RC */
870
871 static void cinergyt2_query (struct work_struct *work)
872 {
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;
877         uint8_t lock_bits;
878         uint32_t unc;
879
880         if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
881                 return;
882
883         unc = s->uncorrected_block_count;
884         lock_bits = s->lock_bits;
885
886         cinergyt2_command(cinergyt2, cmd, sizeof(cmd), (char *) s, sizeof(*s));
887
888         unc += le32_to_cpu(s->uncorrected_block_count);
889         s->uncorrected_block_count = unc;
890
891         if (lock_bits != s->lock_bits) {
892                 wake_up_interruptible(&cinergyt2->poll_wq);
893                 cinergyt2->pending_fe_events++;
894         }
895
896         schedule_delayed_work(&cinergyt2->query_work,
897                               msecs_to_jiffies(QUERY_INTERVAL));
898
899         mutex_unlock(&cinergyt2->sem);
900 }
901
902 static int cinergyt2_probe (struct usb_interface *intf,
903                   const struct usb_device_id *id)
904 {
905         struct cinergyt2 *cinergyt2;
906         int err;
907
908         if (!(cinergyt2 = kzalloc (sizeof(struct cinergyt2), GFP_KERNEL))) {
909                 dprintk(1, "out of memory?!?\n");
910                 return -ENOMEM;
911         }
912
913         usb_set_intfdata (intf, (void *) cinergyt2);
914
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);
919
920         cinergyt2->udev = interface_to_usbdev(intf);
921         cinergyt2->param.cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
922
923         if (cinergyt2_alloc_stream_urbs (cinergyt2) < 0) {
924                 dprintk(1, "unable to allocate stream urbs\n");
925                 kfree(cinergyt2);
926                 return -ENOMEM;
927         }
928
929         if ((err = dvb_register_adapter(&cinergyt2->adapter, DRIVER_NAME, THIS_MODULE, &cinergyt2->udev->dev)) < 0) {
930                 kfree(cinergyt2);
931                 return err;
932         }
933
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;
942
943         if ((err = dvb_dmx_init(&cinergyt2->demux)) < 0) {
944                 dprintk(1, "dvb_dmx_init() failed (err = %d)\n", err);
945                 goto bailout;
946         }
947
948         cinergyt2->dmxdev.filternum = cinergyt2->demux.filternum;
949         cinergyt2->dmxdev.demux = &cinergyt2->demux.dmx;
950         cinergyt2->dmxdev.capabilities = 0;
951
952         if ((err = dvb_dmxdev_init(&cinergyt2->dmxdev, &cinergyt2->adapter)) < 0) {
953                 dprintk(1, "dvb_dmxdev_init() failed (err = %d)\n", err);
954                 goto bailout;
955         }
956
957         if (dvb_net_init(&cinergyt2->adapter, &cinergyt2->dvbnet, &cinergyt2->demux.dmx))
958                 dprintk(1, "dvb_net_init() failed!\n");
959
960         dvb_register_device(&cinergyt2->adapter, &cinergyt2->fedev,
961                             &cinergyt2_fe_template, cinergyt2,
962                             DVB_DEVICE_FRONTEND);
963
964         err = cinergyt2_register_rc(cinergyt2);
965         if (err)
966                 goto bailout;
967
968         return 0;
969
970 bailout:
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);
976         kfree(cinergyt2);
977         return -ENOMEM;
978 }
979
980 static void cinergyt2_disconnect (struct usb_interface *intf)
981 {
982         struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
983
984         cinergyt2_unregister_rc(cinergyt2);
985         cancel_rearming_delayed_work(&cinergyt2->query_work);
986         wake_up_interruptible(&cinergyt2->poll_wq);
987
988         cinergyt2->demux.dmx.close(&cinergyt2->demux.dmx);
989         cinergyt2->disconnect_pending = 1;
990
991         if (!atomic_read(&cinergyt2->inuse))
992                 cinergyt2_unregister(cinergyt2);
993 }
994
995 static int cinergyt2_suspend (struct usb_interface *intf, pm_message_t state)
996 {
997         struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
998
999         if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->wq_sem))
1000                 return -ERESTARTSYS;
1001
1002         cinergyt2_suspend_rc(cinergyt2);
1003         cancel_rearming_delayed_work(&cinergyt2->query_work);
1004
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);
1010
1011         return 0;
1012 }
1013
1014 static int cinergyt2_resume (struct usb_interface *intf)
1015 {
1016         struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
1017         struct dvbt_set_parameters_msg *param = &cinergyt2->param;
1018         int err = -ERESTARTSYS;
1019
1020         if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->wq_sem))
1021                 goto out;
1022
1023         if (mutex_lock_interruptible(&cinergyt2->sem))
1024                 goto out_unlock1;
1025
1026         err = 0;
1027
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);
1034         }
1035
1036         cinergyt2_resume_rc(cinergyt2);
1037
1038         mutex_unlock(&cinergyt2->sem);
1039 out_unlock1:
1040         mutex_unlock(&cinergyt2->wq_sem);
1041 out:
1042         return err;
1043 }
1044
1045 static const struct usb_device_id cinergyt2_table [] __devinitdata = {
1046         { USB_DEVICE(0x0ccd, 0x0038) },
1047         { 0 }
1048 };
1049
1050 MODULE_DEVICE_TABLE(usb, cinergyt2_table);
1051
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
1059 };
1060
1061 static int __init cinergyt2_init (void)
1062 {
1063         int err;
1064
1065         if ((err = usb_register(&cinergyt2_driver)) < 0)
1066                 dprintk(1, "usb_register() failed! (err %i)\n", err);
1067
1068         return err;
1069 }
1070
1071 static void __exit cinergyt2_exit (void)
1072 {
1073         usb_deregister(&cinergyt2_driver);
1074 }
1075
1076 module_init (cinergyt2_init);
1077 module_exit (cinergyt2_exit);
1078
1079 MODULE_LICENSE("GPL");
1080 MODULE_AUTHOR("Holger Waechtler, Daniel Mack");