blob: a1607e7d6d6bfba74eddb9798d42de0b16f69aa8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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
34#include "dmxdev.h"
35#include "dvb_demux.h"
36#include "dvb_net.h"
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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)
Johannes Stezenbach6d789332005-09-09 13:03:05 -070050 #define RC_QUERY_INTERVAL (50) /* milliseconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 #define QUERY_INTERVAL (333) /* milliseconds */
52#endif
53
54#define DRIVER_NAME "TerraTec/qanu USB2.0 Highspeed DVB-T Receiver"
55
56static int debug;
57module_param_named(debug, debug, int, 0644);
58MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
59
60#define dprintk(level, args...) \
61do { \
62 if ((debug & level)) { \
63 printk("%s: %s(): ", __stringify(KBUILD_MODNAME), \
64 __FUNCTION__); \
65 printk(args); } \
66} while (0)
67
68enum 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
80struct 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
88struct 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
102static 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
116struct cinergyt2 {
117 struct dvb_demux demux;
118 struct usb_device *udev;
119 struct semaphore sem;
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700120 struct dvb_adapter adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 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
135 void *streambuf;
136 dma_addr_t streambuf_dmahandle;
137 struct urb *stream_urb [STREAM_URB_COUNT];
138
139#ifdef ENABLE_RC
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500140 struct input_dev *rc_input_dev;
141 char phys[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct work_struct rc_query_work;
143 int rc_input_event;
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700144 u32 rc_last_code;
145 unsigned long last_event_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#endif
147};
148
149enum {
150 CINERGYT2_RC_EVENT_TYPE_NONE = 0x00,
151 CINERGYT2_RC_EVENT_TYPE_NEC = 0x01,
152 CINERGYT2_RC_EVENT_TYPE_RC5 = 0x02
153};
154
155struct cinergyt2_rc_event {
156 char type;
157 uint32_t value;
158} __attribute__((packed));
159
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700160static const uint32_t rc_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfe01eb04, KEY_POWER,
162 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfd02eb04, KEY_1,
163 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfc03eb04, KEY_2,
164 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfb04eb04, KEY_3,
165 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfa05eb04, KEY_4,
166 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf906eb04, KEY_5,
167 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf807eb04, KEY_6,
168 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf708eb04, KEY_7,
169 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf609eb04, KEY_8,
170 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf50aeb04, KEY_9,
171 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf30ceb04, KEY_0,
172 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf40beb04, KEY_VIDEO,
173 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf20deb04, KEY_REFRESH,
174 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf10eeb04, KEY_SELECT,
175 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf00feb04, KEY_EPG,
176 CINERGYT2_RC_EVENT_TYPE_NEC, 0xef10eb04, KEY_UP,
177 CINERGYT2_RC_EVENT_TYPE_NEC, 0xeb14eb04, KEY_DOWN,
178 CINERGYT2_RC_EVENT_TYPE_NEC, 0xee11eb04, KEY_LEFT,
179 CINERGYT2_RC_EVENT_TYPE_NEC, 0xec13eb04, KEY_RIGHT,
180 CINERGYT2_RC_EVENT_TYPE_NEC, 0xed12eb04, KEY_OK,
181 CINERGYT2_RC_EVENT_TYPE_NEC, 0xea15eb04, KEY_TEXT,
182 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe916eb04, KEY_INFO,
183 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe817eb04, KEY_RED,
184 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe718eb04, KEY_GREEN,
185 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe619eb04, KEY_YELLOW,
186 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe51aeb04, KEY_BLUE,
187 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe31ceb04, KEY_VOLUMEUP,
188 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe11eeb04, KEY_VOLUMEDOWN,
189 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe21deb04, KEY_MUTE,
190 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe41beb04, KEY_CHANNELUP,
191 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe01feb04, KEY_CHANNELDOWN,
192 CINERGYT2_RC_EVENT_TYPE_NEC, 0xbf40eb04, KEY_PAUSE,
193 CINERGYT2_RC_EVENT_TYPE_NEC, 0xb34ceb04, KEY_PLAY,
194 CINERGYT2_RC_EVENT_TYPE_NEC, 0xa758eb04, KEY_RECORD,
195 CINERGYT2_RC_EVENT_TYPE_NEC, 0xab54eb04, KEY_PREVIOUS,
196 CINERGYT2_RC_EVENT_TYPE_NEC, 0xb748eb04, KEY_STOP,
197 CINERGYT2_RC_EVENT_TYPE_NEC, 0xa35ceb04, KEY_NEXT
198};
199
200static int cinergyt2_command (struct cinergyt2 *cinergyt2,
201 char *send_buf, int send_buf_len,
202 char *recv_buf, int recv_buf_len)
203{
204 int actual_len;
205 char dummy;
206 int ret;
207
208 ret = usb_bulk_msg(cinergyt2->udev, usb_sndbulkpipe(cinergyt2->udev, 1),
209 send_buf, send_buf_len, &actual_len, 1000);
210
211 if (ret)
212 dprintk(1, "usb_bulk_msg (send) failed, err %i\n", ret);
213
214 if (!recv_buf)
215 recv_buf = &dummy;
216
217 ret = usb_bulk_msg(cinergyt2->udev, usb_rcvbulkpipe(cinergyt2->udev, 1),
218 recv_buf, recv_buf_len, &actual_len, 1000);
219
220 if (ret)
221 dprintk(1, "usb_bulk_msg (read) failed, err %i\n", ret);
222
223 return ret ? ret : actual_len;
224}
225
226static void cinergyt2_control_stream_transfer (struct cinergyt2 *cinergyt2, int enable)
227{
228 char buf [] = { CINERGYT2_EP1_CONTROL_STREAM_TRANSFER, enable ? 1 : 0 };
229 cinergyt2_command(cinergyt2, buf, sizeof(buf), NULL, 0);
230}
231
232static void cinergyt2_sleep (struct cinergyt2 *cinergyt2, int sleep)
233{
234 char buf [] = { CINERGYT2_EP1_SLEEP_MODE, sleep ? 1 : 0 };
235 cinergyt2_command(cinergyt2, buf, sizeof(buf), NULL, 0);
236 cinergyt2->sleeping = sleep;
237}
238
239static void cinergyt2_stream_irq (struct urb *urb, struct pt_regs *regs);
240
241static int cinergyt2_submit_stream_urb (struct cinergyt2 *cinergyt2, struct urb *urb)
242{
243 int err;
244
245 usb_fill_bulk_urb(urb,
246 cinergyt2->udev,
247 usb_rcvbulkpipe(cinergyt2->udev, 0x2),
248 urb->transfer_buffer,
249 STREAM_BUF_SIZE,
250 cinergyt2_stream_irq,
251 cinergyt2);
252
253 if ((err = usb_submit_urb(urb, GFP_ATOMIC)))
254 dprintk(1, "urb submission failed (err = %i)!\n", err);
255
256 return err;
257}
258
259static void cinergyt2_stream_irq (struct urb *urb, struct pt_regs *regs)
260{
261 struct cinergyt2 *cinergyt2 = urb->context;
262
263 if (urb->actual_length > 0)
264 dvb_dmx_swfilter(&cinergyt2->demux,
265 urb->transfer_buffer, urb->actual_length);
266
267 if (cinergyt2->streaming)
268 cinergyt2_submit_stream_urb(cinergyt2, urb);
269}
270
271static void cinergyt2_free_stream_urbs (struct cinergyt2 *cinergyt2)
272{
273 int i;
274
275 for (i=0; i<STREAM_URB_COUNT; i++)
276 if (cinergyt2->stream_urb[i])
277 usb_free_urb(cinergyt2->stream_urb[i]);
278
279 pci_free_consistent(NULL, STREAM_URB_COUNT*STREAM_BUF_SIZE,
280 cinergyt2->streambuf, cinergyt2->streambuf_dmahandle);
281}
282
283static int cinergyt2_alloc_stream_urbs (struct cinergyt2 *cinergyt2)
284{
285 int i;
286
287 cinergyt2->streambuf = pci_alloc_consistent(NULL,
288 STREAM_URB_COUNT*STREAM_BUF_SIZE,
289 &cinergyt2->streambuf_dmahandle);
290 if (!cinergyt2->streambuf) {
291 dprintk(1, "failed to alloc consistent stream memory area, bailing out!\n");
292 return -ENOMEM;
293 }
294
295 memset(cinergyt2->streambuf, 0, STREAM_URB_COUNT*STREAM_BUF_SIZE);
296
297 for (i=0; i<STREAM_URB_COUNT; i++) {
298 struct urb *urb;
299
300 if (!(urb = usb_alloc_urb(0, GFP_ATOMIC))) {
301 dprintk(1, "failed to alloc consistent stream urbs, bailing out!\n");
302 cinergyt2_free_stream_urbs(cinergyt2);
303 return -ENOMEM;
304 }
305
306 urb->transfer_buffer = cinergyt2->streambuf + i * STREAM_BUF_SIZE;
307 urb->transfer_buffer_length = STREAM_BUF_SIZE;
308
309 cinergyt2->stream_urb[i] = urb;
310 }
311
312 return 0;
313}
314
315static void cinergyt2_stop_stream_xfer (struct cinergyt2 *cinergyt2)
316{
317 int i;
318
319 cinergyt2_control_stream_transfer(cinergyt2, 0);
320
321 for (i=0; i<STREAM_URB_COUNT; i++)
322 if (cinergyt2->stream_urb[i])
323 usb_kill_urb(cinergyt2->stream_urb[i]);
324}
325
326static int cinergyt2_start_stream_xfer (struct cinergyt2 *cinergyt2)
327{
328 int i, err;
329
330 for (i=0; i<STREAM_URB_COUNT; i++) {
331 if ((err = cinergyt2_submit_stream_urb(cinergyt2, cinergyt2->stream_urb[i]))) {
332 cinergyt2_stop_stream_xfer(cinergyt2);
333 dprintk(1, "failed urb submission (%i: err = %i)!\n", i, err);
334 return err;
335 }
336 }
337
338 cinergyt2_control_stream_transfer(cinergyt2, 1);
339 return 0;
340}
341
342static int cinergyt2_start_feed(struct dvb_demux_feed *dvbdmxfeed)
343{
344 struct dvb_demux *demux = dvbdmxfeed->demux;
345 struct cinergyt2 *cinergyt2 = demux->priv;
346
347 if (down_interruptible(&cinergyt2->sem))
348 return -ERESTARTSYS;
349
350 if (cinergyt2->streaming == 0)
351 cinergyt2_start_stream_xfer(cinergyt2);
352
353 cinergyt2->streaming++;
354 up(&cinergyt2->sem);
355 return 0;
356}
357
358static int cinergyt2_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
359{
360 struct dvb_demux *demux = dvbdmxfeed->demux;
361 struct cinergyt2 *cinergyt2 = demux->priv;
362
363 if (down_interruptible(&cinergyt2->sem))
364 return -ERESTARTSYS;
365
366 if (--cinergyt2->streaming == 0)
367 cinergyt2_stop_stream_xfer(cinergyt2);
368
369 up(&cinergyt2->sem);
370 return 0;
371}
372
373/**
374 * convert linux-dvb frontend parameter set into TPS.
375 * See ETSI ETS-300744, section 4.6.2, table 9 for details.
376 *
377 * This function is probably reusable and may better get placed in a support
378 * library.
379 *
380 * We replace errornous fields by default TPS fields (the ones with value 0).
381 */
382static uint16_t compute_tps (struct dvb_frontend_parameters *p)
383{
384 struct dvb_ofdm_parameters *op = &p->u.ofdm;
385 uint16_t tps = 0;
386
387 switch (op->code_rate_HP) {
388 case FEC_2_3:
389 tps |= (1 << 7);
390 break;
391 case FEC_3_4:
392 tps |= (2 << 7);
393 break;
394 case FEC_5_6:
395 tps |= (3 << 7);
396 break;
397 case FEC_7_8:
398 tps |= (4 << 7);
399 break;
400 case FEC_1_2:
401 case FEC_AUTO:
402 default:
403 /* tps |= (0 << 7) */;
404 }
405
406 switch (op->code_rate_LP) {
407 case FEC_2_3:
408 tps |= (1 << 4);
409 break;
410 case FEC_3_4:
411 tps |= (2 << 4);
412 break;
413 case FEC_5_6:
414 tps |= (3 << 4);
415 break;
416 case FEC_7_8:
417 tps |= (4 << 4);
418 break;
419 case FEC_1_2:
420 case FEC_AUTO:
421 default:
422 /* tps |= (0 << 4) */;
423 }
424
425 switch (op->constellation) {
426 case QAM_16:
427 tps |= (1 << 13);
428 break;
429 case QAM_64:
430 tps |= (2 << 13);
431 break;
432 case QPSK:
433 default:
434 /* tps |= (0 << 13) */;
435 }
436
437 switch (op->transmission_mode) {
438 case TRANSMISSION_MODE_8K:
439 tps |= (1 << 0);
440 break;
441 case TRANSMISSION_MODE_2K:
442 default:
443 /* tps |= (0 << 0) */;
444 }
445
446 switch (op->guard_interval) {
447 case GUARD_INTERVAL_1_16:
448 tps |= (1 << 2);
449 break;
450 case GUARD_INTERVAL_1_8:
451 tps |= (2 << 2);
452 break;
453 case GUARD_INTERVAL_1_4:
454 tps |= (3 << 2);
455 break;
456 case GUARD_INTERVAL_1_32:
457 default:
458 /* tps |= (0 << 2) */;
459 }
460
461 switch (op->hierarchy_information) {
462 case HIERARCHY_1:
463 tps |= (1 << 10);
464 break;
465 case HIERARCHY_2:
466 tps |= (2 << 10);
467 break;
468 case HIERARCHY_4:
469 tps |= (3 << 10);
470 break;
471 case HIERARCHY_NONE:
472 default:
473 /* tps |= (0 << 10) */;
474 }
475
476 return tps;
477}
478
479static int cinergyt2_open (struct inode *inode, struct file *file)
480{
481 struct dvb_device *dvbdev = file->private_data;
482 struct cinergyt2 *cinergyt2 = dvbdev->priv;
483 int err;
484
485 if ((err = dvb_generic_open(inode, file)))
486 return err;
487
488 if (down_interruptible(&cinergyt2->sem))
489 return -ERESTARTSYS;
490
491 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
492 cinergyt2_sleep(cinergyt2, 0);
493 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
494 }
495
496 up(&cinergyt2->sem);
497 return 0;
498}
499
500static int cinergyt2_release (struct inode *inode, struct file *file)
501{
502 struct dvb_device *dvbdev = file->private_data;
503 struct cinergyt2 *cinergyt2 = dvbdev->priv;
504
505 if (down_interruptible(&cinergyt2->sem))
506 return -ERESTARTSYS;
507
508 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
509 cancel_delayed_work(&cinergyt2->query_work);
510 flush_scheduled_work();
511 cinergyt2_sleep(cinergyt2, 1);
512 }
513
514 up(&cinergyt2->sem);
515
516 return dvb_generic_release(inode, file);
517}
518
519static unsigned int cinergyt2_poll (struct file *file, struct poll_table_struct *wait)
520{
521 struct dvb_device *dvbdev = file->private_data;
522 struct cinergyt2 *cinergyt2 = dvbdev->priv;
523 poll_wait(file, &cinergyt2->poll_wq, wait);
524 return (POLLIN | POLLRDNORM | POLLPRI);
525}
526
527
528static int cinergyt2_ioctl (struct inode *inode, struct file *file,
529 unsigned cmd, unsigned long arg)
530{
531 struct dvb_device *dvbdev = file->private_data;
532 struct cinergyt2 *cinergyt2 = dvbdev->priv;
533 struct dvbt_get_status_msg *stat = &cinergyt2->status;
534 fe_status_t status = 0;
535
536 switch (cmd) {
537 case FE_GET_INFO:
538 return copy_to_user((void __user*) arg, &cinergyt2_fe_info,
539 sizeof(struct dvb_frontend_info));
540
541 case FE_READ_STATUS:
542 if (0xffff - le16_to_cpu(stat->gain) > 30)
543 status |= FE_HAS_SIGNAL;
544 if (stat->lock_bits & (1 << 6))
545 status |= FE_HAS_LOCK;
546 if (stat->lock_bits & (1 << 5))
547 status |= FE_HAS_SYNC;
548 if (stat->lock_bits & (1 << 4))
549 status |= FE_HAS_CARRIER;
550 if (stat->lock_bits & (1 << 1))
551 status |= FE_HAS_VITERBI;
552
553 return copy_to_user((void __user*) arg, &status, sizeof(status));
554
555 case FE_READ_BER:
556 return put_user(le32_to_cpu(stat->viterbi_error_rate),
557 (__u32 __user *) arg);
558
559 case FE_READ_SIGNAL_STRENGTH:
560 return put_user(0xffff - le16_to_cpu(stat->gain),
561 (__u16 __user *) arg);
562
563 case FE_READ_SNR:
564 return put_user((stat->snr << 8) | stat->snr,
565 (__u16 __user *) arg);
566
567 case FE_READ_UNCORRECTED_BLOCKS:
568 /* UNC are already converted to host byte order... */
569 return put_user(stat->uncorrected_block_count,
570 (__u32 __user *) arg);
571
572 case FE_SET_FRONTEND:
573 {
574 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
575 struct dvb_frontend_parameters p;
576 int err;
577
578 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
579 return -EPERM;
580
581 if (copy_from_user(&p, (void __user*) arg, sizeof(p)))
582 return -EFAULT;
583
584 if (down_interruptible(&cinergyt2->sem))
585 return -ERESTARTSYS;
586
587 param->cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
588 param->tps = cpu_to_le16(compute_tps(&p));
589 param->freq = cpu_to_le32(p.frequency / 1000);
590 param->bandwidth = 8 - p.u.ofdm.bandwidth - BANDWIDTH_8_MHZ;
591
592 stat->lock_bits = 0;
593 cinergyt2->pending_fe_events++;
594 wake_up_interruptible(&cinergyt2->poll_wq);
595
596 err = cinergyt2_command(cinergyt2,
597 (char *) param, sizeof(*param),
598 NULL, 0);
599
600 up(&cinergyt2->sem);
601
602 return (err < 0) ? err : 0;
603 }
604
605 case FE_GET_FRONTEND:
606 /**
607 * trivial to implement (see struct dvbt_get_status_msg).
608 * equivalent to FE_READ ioctls, but needs
609 * TPS -> linux-dvb parameter set conversion. Feel free
610 * to implement this and send us a patch if you need this
611 * functionality.
612 */
613 break;
614
615 case FE_GET_EVENT:
616 {
617 /**
618 * for now we only fill the status field. the parameters
619 * are trivial to fill as soon FE_GET_FRONTEND is done.
620 */
621 struct dvb_frontend_event __user *e = (void __user *) arg;
622 if (cinergyt2->pending_fe_events == 0) {
623 if (file->f_flags & O_NONBLOCK)
624 return -EWOULDBLOCK;
625 wait_event_interruptible(cinergyt2->poll_wq,
626 cinergyt2->pending_fe_events > 0);
627 }
628 cinergyt2->pending_fe_events = 0;
629 return cinergyt2_ioctl(inode, file, FE_READ_STATUS,
630 (unsigned long) &e->status);
631 }
632
633 default:
634 ;
635 }
636
637 return -EINVAL;
638}
639
640static int cinergyt2_mmap(struct file *file, struct vm_area_struct *vma)
641{
642 struct dvb_device *dvbdev = file->private_data;
643 struct cinergyt2 *cinergyt2 = dvbdev->priv;
644 int ret = 0;
645
646 lock_kernel();
647
648 if (vma->vm_flags & (VM_WRITE | VM_EXEC)) {
649 ret = -EPERM;
650 goto bailout;
651 }
652
653 if (vma->vm_end > vma->vm_start + STREAM_URB_COUNT * STREAM_BUF_SIZE) {
654 ret = -EINVAL;
655 goto bailout;
656 }
657
658 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
659 vma->vm_file = file;
660
661 ret = remap_pfn_range(vma, vma->vm_start,
662 virt_to_phys(cinergyt2->streambuf) >> PAGE_SHIFT,
663 vma->vm_end - vma->vm_start,
664 vma->vm_page_prot) ? -EAGAIN : 0;
665bailout:
666 unlock_kernel();
667 return ret;
668}
669
670static struct file_operations cinergyt2_fops = {
671 .owner = THIS_MODULE,
672 .ioctl = cinergyt2_ioctl,
673 .poll = cinergyt2_poll,
674 .open = cinergyt2_open,
675 .release = cinergyt2_release,
676 .mmap = cinergyt2_mmap
677};
678
679static struct dvb_device cinergyt2_fe_template = {
680 .users = ~0,
681 .writers = 1,
682 .readers = (~0)-1,
683 .fops = &cinergyt2_fops
684};
685
686#ifdef ENABLE_RC
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688static void cinergyt2_query_rc (void *data)
689{
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700690 struct cinergyt2 *cinergyt2 = data;
691 char buf[1] = { CINERGYT2_EP1_GET_RC_EVENTS };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct cinergyt2_rc_event rc_events[12];
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700693 int n, len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (down_interruptible(&cinergyt2->sem))
696 return;
697
698 len = cinergyt2_command(cinergyt2, buf, sizeof(buf),
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700699 (char *) rc_events, sizeof(rc_events));
700 if (len < 0)
701 goto out;
702 if (len == 0) {
703 if (time_after(jiffies, cinergyt2->last_event_jiffies +
704 msecs_to_jiffies(150))) {
705 /* stop key repeat */
706 if (cinergyt2->rc_input_event != KEY_MAX) {
707 dprintk(1, "rc_input_event=%d Up\n", cinergyt2->rc_input_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500708 input_report_key(cinergyt2->rc_input_dev,
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700709 cinergyt2->rc_input_event, 0);
710 cinergyt2->rc_input_event = KEY_MAX;
711 }
712 cinergyt2->rc_last_code = ~0;
713 }
714 goto out;
715 }
716 cinergyt2->last_event_jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700718 for (n = 0; n < (len / sizeof(rc_events[0])); n++) {
719 dprintk(1, "rc_events[%d].value = %x, type=%x\n",
720 n, le32_to_cpu(rc_events[n].value), rc_events[n].type);
Martin Loschwitz38b32d62005-07-07 17:57:31 -0700721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (rc_events[n].type == CINERGYT2_RC_EVENT_TYPE_NEC &&
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700723 rc_events[n].value == ~0) {
724 /* keyrepeat bit -> just repeat last rc_input_event */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 } else {
726 cinergyt2->rc_input_event = KEY_MAX;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500727 for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3) {
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700728 if (rc_keys[i + 0] == rc_events[n].type &&
729 rc_keys[i + 1] == le32_to_cpu(rc_events[n].value)) {
730 cinergyt2->rc_input_event = rc_keys[i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 break;
732 }
733 }
734 }
735
736 if (cinergyt2->rc_input_event != KEY_MAX) {
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700737 if (rc_events[n].value == cinergyt2->rc_last_code &&
738 cinergyt2->rc_last_code != ~0) {
739 /* emit a key-up so the double event is recognized */
740 dprintk(1, "rc_input_event=%d UP\n", cinergyt2->rc_input_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500741 input_report_key(cinergyt2->rc_input_dev,
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700742 cinergyt2->rc_input_event, 0);
743 }
744 dprintk(1, "rc_input_event=%d\n", cinergyt2->rc_input_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500745 input_report_key(cinergyt2->rc_input_dev,
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700746 cinergyt2->rc_input_event, 1);
747 cinergyt2->rc_last_code = rc_events[n].value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749 }
750
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700751out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 schedule_delayed_work(&cinergyt2->rc_query_work,
753 msecs_to_jiffies(RC_QUERY_INTERVAL));
754
755 up(&cinergyt2->sem);
756}
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500757
758static int cinergyt2_register_rc(struct cinergyt2 *cinergyt2)
759{
760 struct input_dev *input_dev;
761 int i;
762
763 cinergyt2->rc_input_dev = input_dev = input_allocate_device();
764 if (!input_dev)
765 return -ENOMEM;
766
767 usb_make_path(cinergyt2->udev, cinergyt2->phys, sizeof(cinergyt2->phys));
768 strlcat(cinergyt2->phys, "/input0", sizeof(cinergyt2->phys));
769 cinergyt2->rc_input_event = KEY_MAX;
770 cinergyt2->rc_last_code = ~0;
771 INIT_WORK(&cinergyt2->rc_query_work, cinergyt2_query_rc, cinergyt2);
772
773 input_dev->name = DRIVER_NAME " remote control";
774 input_dev->phys = cinergyt2->phys;
775 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
776 for (i = 0; ARRAY_SIZE(rc_keys); i += 3)
777 set_bit(rc_keys[i + 2], input_dev->keybit);
778 input_dev->keycodesize = 0;
779 input_dev->keycodemax = 0;
780
781 input_register_device(cinergyt2->rc_input_dev);
782 schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
783}
784
785static void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2)
786{
787 cancel_delayed_work(&cinergyt2->rc_query_work);
788 flush_scheduled_work();
789 input_unregister_device(cinergyt2->rc_input_dev);
790}
791
792static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2)
793{
794 cancel_delayed_work(&cinergyt2->rc_query_work);
795}
796
797static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2)
798{
799 schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
800}
801
802#else
803
804static inline int cinergyt2_register_rc(struct cinergyt2 *cinergyt2) { return 0; }
805static inline void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2) { }
806static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2) { }
807static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2) { }
808
809#endif /* ENABLE_RC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811static void cinergyt2_query (void *data)
812{
813 struct cinergyt2 *cinergyt2 = (struct cinergyt2 *) data;
814 char cmd [] = { CINERGYT2_EP1_GET_TUNER_STATUS };
815 struct dvbt_get_status_msg *s = &cinergyt2->status;
816 uint8_t lock_bits;
817 uint32_t unc;
818
819 if (down_interruptible(&cinergyt2->sem))
820 return;
821
822 unc = s->uncorrected_block_count;
823 lock_bits = s->lock_bits;
824
825 cinergyt2_command(cinergyt2, cmd, sizeof(cmd), (char *) s, sizeof(*s));
826
827 unc += le32_to_cpu(s->uncorrected_block_count);
828 s->uncorrected_block_count = unc;
829
830 if (lock_bits != s->lock_bits) {
831 wake_up_interruptible(&cinergyt2->poll_wq);
832 cinergyt2->pending_fe_events++;
833 }
834
835 schedule_delayed_work(&cinergyt2->query_work,
836 msecs_to_jiffies(QUERY_INTERVAL));
837
838 up(&cinergyt2->sem);
839}
840
841static int cinergyt2_probe (struct usb_interface *intf,
842 const struct usb_device_id *id)
843{
844 struct cinergyt2 *cinergyt2;
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700845 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 if (!(cinergyt2 = kmalloc (sizeof(struct cinergyt2), GFP_KERNEL))) {
848 dprintk(1, "out of memory?!?\n");
849 return -ENOMEM;
850 }
851
852 memset (cinergyt2, 0, sizeof (struct cinergyt2));
853 usb_set_intfdata (intf, (void *) cinergyt2);
854
855 init_MUTEX(&cinergyt2->sem);
856 init_waitqueue_head (&cinergyt2->poll_wq);
857 INIT_WORK(&cinergyt2->query_work, cinergyt2_query, cinergyt2);
858
859 cinergyt2->udev = interface_to_usbdev(intf);
860 cinergyt2->param.cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
861
862 if (cinergyt2_alloc_stream_urbs (cinergyt2) < 0) {
863 dprintk(1, "unable to allocate stream urbs\n");
864 kfree(cinergyt2);
865 return -ENOMEM;
866 }
867
868 dvb_register_adapter(&cinergyt2->adapter, DRIVER_NAME, THIS_MODULE);
869
870 cinergyt2->demux.priv = cinergyt2;
871 cinergyt2->demux.filternum = 256;
872 cinergyt2->demux.feednum = 256;
873 cinergyt2->demux.start_feed = cinergyt2_start_feed;
874 cinergyt2->demux.stop_feed = cinergyt2_stop_feed;
875 cinergyt2->demux.dmx.capabilities = DMX_TS_FILTERING |
876 DMX_SECTION_FILTERING |
877 DMX_MEMORY_BASED_FILTERING;
878
879 if ((err = dvb_dmx_init(&cinergyt2->demux)) < 0) {
880 dprintk(1, "dvb_dmx_init() failed (err = %d)\n", err);
881 goto bailout;
882 }
883
884 cinergyt2->dmxdev.filternum = cinergyt2->demux.filternum;
885 cinergyt2->dmxdev.demux = &cinergyt2->demux.dmx;
886 cinergyt2->dmxdev.capabilities = 0;
887
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700888 if ((err = dvb_dmxdev_init(&cinergyt2->dmxdev, &cinergyt2->adapter)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 dprintk(1, "dvb_dmxdev_init() failed (err = %d)\n", err);
890 goto bailout;
891 }
892
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700893 if (dvb_net_init(&cinergyt2->adapter, &cinergyt2->dvbnet, &cinergyt2->demux.dmx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 dprintk(1, "dvb_net_init() failed!\n");
895
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700896 dvb_register_device(&cinergyt2->adapter, &cinergyt2->fedev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 &cinergyt2_fe_template, cinergyt2,
898 DVB_DEVICE_FRONTEND);
899
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500900 err = cinergyt2_register_rc(cinergyt2);
901 if (err)
902 goto bailout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return 0;
905
906bailout:
907 dvb_dmxdev_release(&cinergyt2->dmxdev);
908 dvb_dmx_release(&cinergyt2->demux);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500909 dvb_unregister_adapter(&cinergyt2->adapter);
910 cinergyt2_free_stream_urbs(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 kfree(cinergyt2);
912 return -ENOMEM;
913}
914
915static void cinergyt2_disconnect (struct usb_interface *intf)
916{
917 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
918
919 if (down_interruptible(&cinergyt2->sem))
920 return;
921
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500922 cinergyt2_unregister_rc(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 cinergyt2->demux.dmx.close(&cinergyt2->demux.dmx);
925 dvb_net_release(&cinergyt2->dvbnet);
926 dvb_dmxdev_release(&cinergyt2->dmxdev);
927 dvb_dmx_release(&cinergyt2->demux);
928 dvb_unregister_device(cinergyt2->fedev);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700929 dvb_unregister_adapter(&cinergyt2->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 cinergyt2_free_stream_urbs(cinergyt2);
932 up(&cinergyt2->sem);
933 kfree(cinergyt2);
934}
935
Pavel Macheka2910682005-04-16 15:25:27 -0700936static int cinergyt2_suspend (struct usb_interface *intf, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
939
940 if (down_interruptible(&cinergyt2->sem))
941 return -ERESTARTSYS;
942
Pavel Machekca078ba2005-09-03 15:56:57 -0700943 if (state.event > PM_EVENT_ON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500945
946 cinergyt2_suspend_rc(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 cancel_delayed_work(&cinergyt2->query_work);
948 if (cinergyt2->streaming)
949 cinergyt2_stop_stream_xfer(cinergyt2);
950 flush_scheduled_work();
951 cinergyt2_sleep(cinergyt2, 1);
952 }
953
954 up(&cinergyt2->sem);
955 return 0;
956}
957
958static int cinergyt2_resume (struct usb_interface *intf)
959{
960 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
961 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
962
963 if (down_interruptible(&cinergyt2->sem))
964 return -ERESTARTSYS;
965
966 if (!cinergyt2->sleeping) {
967 cinergyt2_sleep(cinergyt2, 0);
968 cinergyt2_command(cinergyt2, (char *) param, sizeof(*param), NULL, 0);
969 if (cinergyt2->streaming)
970 cinergyt2_start_stream_xfer(cinergyt2);
971 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
972 }
973
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500974 cinergyt2_resume_rc(cinergyt2);
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 up(&cinergyt2->sem);
977 return 0;
978}
979
980static const struct usb_device_id cinergyt2_table [] __devinitdata = {
981 { USB_DEVICE(0x0ccd, 0x0038) },
982 { 0 }
983};
984
985MODULE_DEVICE_TABLE(usb, cinergyt2_table);
986
987static struct usb_driver cinergyt2_driver = {
988 .owner = THIS_MODULE,
989 .name = "cinergyT2",
990 .probe = cinergyt2_probe,
991 .disconnect = cinergyt2_disconnect,
992 .suspend = cinergyt2_suspend,
993 .resume = cinergyt2_resume,
994 .id_table = cinergyt2_table
995};
996
997static int __init cinergyt2_init (void)
998{
999 int err;
1000
1001 if ((err = usb_register(&cinergyt2_driver)) < 0)
1002 dprintk(1, "usb_register() failed! (err %i)\n", err);
1003
1004 return err;
1005}
1006
1007static void __exit cinergyt2_exit (void)
1008{
1009 usb_deregister(&cinergyt2_driver);
1010}
1011
1012module_init (cinergyt2_init);
1013module_exit (cinergyt2_exit);
1014
1015MODULE_LICENSE("GPL");
1016MODULE_AUTHOR("Holger Waechtler, Daniel Mack");
1017