blob: 55bc891768c29e0a8ba0a07d12b58ab91667f646 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.h>
26#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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>
Ingo Molnar3593cab2006-02-07 06:49:14 -020032#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
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)) { \
Sam Ravnborg367cb702006-01-06 21:17:50 +010063 printk("%s: %s(): ", KBUILD_MODNAME, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 __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;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200119 struct mutex 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;
Deti Fliegle4acba32006-01-09 15:25:23 -0200134 int disconnect_pending;
135 atomic_t inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 void *streambuf;
138 dma_addr_t streambuf_dmahandle;
139 struct urb *stream_urb [STREAM_URB_COUNT];
140
141#ifdef ENABLE_RC
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500142 struct input_dev *rc_input_dev;
143 char phys[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct work_struct rc_query_work;
145 int rc_input_event;
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700146 u32 rc_last_code;
147 unsigned long last_event_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#endif
149};
150
151enum {
152 CINERGYT2_RC_EVENT_TYPE_NONE = 0x00,
153 CINERGYT2_RC_EVENT_TYPE_NEC = 0x01,
154 CINERGYT2_RC_EVENT_TYPE_RC5 = 0x02
155};
156
157struct cinergyt2_rc_event {
158 char type;
159 uint32_t value;
160} __attribute__((packed));
161
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700162static const uint32_t rc_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 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
202static 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
228static 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
234static 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
David Howells7d12e782006-10-05 14:55:46 +0100241static void cinergyt2_stream_irq (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243static 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
David Howells7d12e782006-10-05 14:55:46 +0100261static void cinergyt2_stream_irq (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
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
273static void cinergyt2_free_stream_urbs (struct cinergyt2 *cinergyt2)
274{
275 int i;
276
277 for (i=0; i<STREAM_URB_COUNT; i++)
Mariusz Kozlowski4064fe42006-11-08 15:34:22 +0100278 usb_free_urb(cinergyt2->stream_urb[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Guido Guenther574780d2005-11-16 00:08:44 -0800280 usb_buffer_free(cinergyt2->udev, STREAM_URB_COUNT*STREAM_BUF_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 cinergyt2->streambuf, cinergyt2->streambuf_dmahandle);
282}
283
284static int cinergyt2_alloc_stream_urbs (struct cinergyt2 *cinergyt2)
285{
286 int i;
287
Guido Guenther574780d2005-11-16 00:08:44 -0800288 cinergyt2->streambuf = usb_buffer_alloc(cinergyt2->udev, STREAM_URB_COUNT*STREAM_BUF_SIZE,
289 SLAB_KERNEL, &cinergyt2->streambuf_dmahandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 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++)
Mariusz Kozlowski4064fe42006-11-08 15:34:22 +0100322 usb_kill_urb(cinergyt2->stream_urb[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325static int cinergyt2_start_stream_xfer (struct cinergyt2 *cinergyt2)
326{
327 int i, err;
328
329 for (i=0; i<STREAM_URB_COUNT; i++) {
330 if ((err = cinergyt2_submit_stream_urb(cinergyt2, cinergyt2->stream_urb[i]))) {
331 cinergyt2_stop_stream_xfer(cinergyt2);
332 dprintk(1, "failed urb submission (%i: err = %i)!\n", i, err);
333 return err;
334 }
335 }
336
337 cinergyt2_control_stream_transfer(cinergyt2, 1);
338 return 0;
339}
340
341static int cinergyt2_start_feed(struct dvb_demux_feed *dvbdmxfeed)
342{
343 struct dvb_demux *demux = dvbdmxfeed->demux;
344 struct cinergyt2 *cinergyt2 = demux->priv;
345
Ingo Molnar3593cab2006-02-07 06:49:14 -0200346 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return -ERESTARTSYS;
348
349 if (cinergyt2->streaming == 0)
350 cinergyt2_start_stream_xfer(cinergyt2);
351
352 cinergyt2->streaming++;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200353 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355}
356
357static int cinergyt2_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
358{
359 struct dvb_demux *demux = dvbdmxfeed->demux;
360 struct cinergyt2 *cinergyt2 = demux->priv;
361
Ingo Molnar3593cab2006-02-07 06:49:14 -0200362 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return -ERESTARTSYS;
364
365 if (--cinergyt2->streaming == 0)
366 cinergyt2_stop_stream_xfer(cinergyt2);
367
Ingo Molnar3593cab2006-02-07 06:49:14 -0200368 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370}
371
372/**
373 * convert linux-dvb frontend parameter set into TPS.
374 * See ETSI ETS-300744, section 4.6.2, table 9 for details.
375 *
376 * This function is probably reusable and may better get placed in a support
377 * library.
378 *
379 * We replace errornous fields by default TPS fields (the ones with value 0).
380 */
381static uint16_t compute_tps (struct dvb_frontend_parameters *p)
382{
383 struct dvb_ofdm_parameters *op = &p->u.ofdm;
384 uint16_t tps = 0;
385
386 switch (op->code_rate_HP) {
387 case FEC_2_3:
388 tps |= (1 << 7);
389 break;
390 case FEC_3_4:
391 tps |= (2 << 7);
392 break;
393 case FEC_5_6:
394 tps |= (3 << 7);
395 break;
396 case FEC_7_8:
397 tps |= (4 << 7);
398 break;
399 case FEC_1_2:
400 case FEC_AUTO:
401 default:
402 /* tps |= (0 << 7) */;
403 }
404
405 switch (op->code_rate_LP) {
406 case FEC_2_3:
407 tps |= (1 << 4);
408 break;
409 case FEC_3_4:
410 tps |= (2 << 4);
411 break;
412 case FEC_5_6:
413 tps |= (3 << 4);
414 break;
415 case FEC_7_8:
416 tps |= (4 << 4);
417 break;
418 case FEC_1_2:
419 case FEC_AUTO:
420 default:
421 /* tps |= (0 << 4) */;
422 }
423
424 switch (op->constellation) {
425 case QAM_16:
426 tps |= (1 << 13);
427 break;
428 case QAM_64:
429 tps |= (2 << 13);
430 break;
431 case QPSK:
432 default:
433 /* tps |= (0 << 13) */;
434 }
435
436 switch (op->transmission_mode) {
437 case TRANSMISSION_MODE_8K:
438 tps |= (1 << 0);
439 break;
440 case TRANSMISSION_MODE_2K:
441 default:
442 /* tps |= (0 << 0) */;
443 }
444
445 switch (op->guard_interval) {
446 case GUARD_INTERVAL_1_16:
447 tps |= (1 << 2);
448 break;
449 case GUARD_INTERVAL_1_8:
450 tps |= (2 << 2);
451 break;
452 case GUARD_INTERVAL_1_4:
453 tps |= (3 << 2);
454 break;
455 case GUARD_INTERVAL_1_32:
456 default:
457 /* tps |= (0 << 2) */;
458 }
459
460 switch (op->hierarchy_information) {
461 case HIERARCHY_1:
462 tps |= (1 << 10);
463 break;
464 case HIERARCHY_2:
465 tps |= (2 << 10);
466 break;
467 case HIERARCHY_4:
468 tps |= (3 << 10);
469 break;
470 case HIERARCHY_NONE:
471 default:
472 /* tps |= (0 << 10) */;
473 }
474
475 return tps;
476}
477
478static int cinergyt2_open (struct inode *inode, struct file *file)
479{
480 struct dvb_device *dvbdev = file->private_data;
481 struct cinergyt2 *cinergyt2 = dvbdev->priv;
Deti Fliegle4acba32006-01-09 15:25:23 -0200482 int err = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Ingo Molnar3593cab2006-02-07 06:49:14 -0200484 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return -ERESTARTSYS;
486
Deti Fliegle4acba32006-01-09 15:25:23 -0200487 if ((err = dvb_generic_open(inode, file))) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200488 mutex_unlock(&cinergyt2->sem);
Deti Fliegle4acba32006-01-09 15:25:23 -0200489 return err;
490 }
491
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
494 cinergyt2_sleep(cinergyt2, 0);
495 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
496 }
497
Deti Fliegle4acba32006-01-09 15:25:23 -0200498 atomic_inc(&cinergyt2->inuse);
499
Ingo Molnar3593cab2006-02-07 06:49:14 -0200500 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return 0;
502}
503
Deti Fliegle4acba32006-01-09 15:25:23 -0200504static void cinergyt2_unregister(struct cinergyt2 *cinergyt2)
505{
Markus Rechberger77cc5312006-03-15 09:31:31 -0300506 dvb_net_release(&cinergyt2->dvbnet);
507 dvb_dmxdev_release(&cinergyt2->dmxdev);
508 dvb_dmx_release(&cinergyt2->demux);
Deti Fliegle4acba32006-01-09 15:25:23 -0200509 dvb_unregister_device(cinergyt2->fedev);
510 dvb_unregister_adapter(&cinergyt2->adapter);
511
512 cinergyt2_free_stream_urbs(cinergyt2);
513 kfree(cinergyt2);
514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516static int cinergyt2_release (struct inode *inode, struct file *file)
517{
518 struct dvb_device *dvbdev = file->private_data;
519 struct cinergyt2 *cinergyt2 = dvbdev->priv;
520
Ingo Molnar3593cab2006-02-07 06:49:14 -0200521 if (mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return -ERESTARTSYS;
523
Deti Fliegle4acba32006-01-09 15:25:23 -0200524 if (!cinergyt2->disconnect_pending && (file->f_flags & O_ACCMODE) != O_RDONLY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 cancel_delayed_work(&cinergyt2->query_work);
526 flush_scheduled_work();
527 cinergyt2_sleep(cinergyt2, 1);
528 }
529
Ingo Molnar3593cab2006-02-07 06:49:14 -0200530 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Deti Fliegle4acba32006-01-09 15:25:23 -0200532 if (atomic_dec_and_test(&cinergyt2->inuse) && cinergyt2->disconnect_pending) {
533 warn("delayed unregister in release");
534 cinergyt2_unregister(cinergyt2);
535 }
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return dvb_generic_release(inode, file);
538}
539
540static unsigned int cinergyt2_poll (struct file *file, struct poll_table_struct *wait)
541{
542 struct dvb_device *dvbdev = file->private_data;
543 struct cinergyt2 *cinergyt2 = dvbdev->priv;
Dyks, Axel (XL)774dd5d2006-06-11 17:14:35 -0300544 unsigned int mask = 0;
Deti Fliegle4acba32006-01-09 15:25:23 -0200545
Ingo Molnar3593cab2006-02-07 06:49:14 -0200546 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Deti Fliegle4acba32006-01-09 15:25:23 -0200547 return -ERESTARTSYS;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 poll_wait(file, &cinergyt2->poll_wq, wait);
Deti Fliegle4acba32006-01-09 15:25:23 -0200550
Dyks, Axel (XL)774dd5d2006-06-11 17:14:35 -0300551 if (cinergyt2->pending_fe_events != 0)
Michael Krufkyfd174c62006-06-12 13:40:37 -0300552 mask |= (POLLIN | POLLRDNORM | POLLPRI);
Dyks, Axel (XL)774dd5d2006-06-11 17:14:35 -0300553
Ingo Molnar3593cab2006-02-07 06:49:14 -0200554 mutex_unlock(&cinergyt2->sem);
Deti Fliegle4acba32006-01-09 15:25:23 -0200555
Dyks, Axel (XL)774dd5d2006-06-11 17:14:35 -0300556 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559
560static int cinergyt2_ioctl (struct inode *inode, struct file *file,
561 unsigned cmd, unsigned long arg)
562{
563 struct dvb_device *dvbdev = file->private_data;
564 struct cinergyt2 *cinergyt2 = dvbdev->priv;
565 struct dvbt_get_status_msg *stat = &cinergyt2->status;
566 fe_status_t status = 0;
567
568 switch (cmd) {
569 case FE_GET_INFO:
570 return copy_to_user((void __user*) arg, &cinergyt2_fe_info,
571 sizeof(struct dvb_frontend_info));
572
573 case FE_READ_STATUS:
574 if (0xffff - le16_to_cpu(stat->gain) > 30)
575 status |= FE_HAS_SIGNAL;
576 if (stat->lock_bits & (1 << 6))
577 status |= FE_HAS_LOCK;
578 if (stat->lock_bits & (1 << 5))
579 status |= FE_HAS_SYNC;
580 if (stat->lock_bits & (1 << 4))
581 status |= FE_HAS_CARRIER;
582 if (stat->lock_bits & (1 << 1))
583 status |= FE_HAS_VITERBI;
584
585 return copy_to_user((void __user*) arg, &status, sizeof(status));
586
587 case FE_READ_BER:
588 return put_user(le32_to_cpu(stat->viterbi_error_rate),
589 (__u32 __user *) arg);
590
591 case FE_READ_SIGNAL_STRENGTH:
592 return put_user(0xffff - le16_to_cpu(stat->gain),
593 (__u16 __user *) arg);
594
595 case FE_READ_SNR:
596 return put_user((stat->snr << 8) | stat->snr,
597 (__u16 __user *) arg);
598
599 case FE_READ_UNCORRECTED_BLOCKS:
Stephen Williams2c3f11b2006-01-09 15:25:21 -0200600 {
601 uint32_t unc_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Stephen Williams2c3f11b2006-01-09 15:25:21 -0200603 unc_count = stat->uncorrected_block_count;
604 stat->uncorrected_block_count = 0;
605
606 /* UNC are already converted to host byte order... */
607 return put_user(unc_count,(__u32 __user *) arg);
608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 case FE_SET_FRONTEND:
610 {
611 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
612 struct dvb_frontend_parameters p;
613 int err;
614
615 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
616 return -EPERM;
617
618 if (copy_from_user(&p, (void __user*) arg, sizeof(p)))
619 return -EFAULT;
620
Ingo Molnar3593cab2006-02-07 06:49:14 -0200621 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return -ERESTARTSYS;
623
624 param->cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
625 param->tps = cpu_to_le16(compute_tps(&p));
626 param->freq = cpu_to_le32(p.frequency / 1000);
627 param->bandwidth = 8 - p.u.ofdm.bandwidth - BANDWIDTH_8_MHZ;
628
629 stat->lock_bits = 0;
630 cinergyt2->pending_fe_events++;
631 wake_up_interruptible(&cinergyt2->poll_wq);
632
633 err = cinergyt2_command(cinergyt2,
634 (char *) param, sizeof(*param),
635 NULL, 0);
636
Ingo Molnar3593cab2006-02-07 06:49:14 -0200637 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 return (err < 0) ? err : 0;
640 }
641
642 case FE_GET_FRONTEND:
643 /**
644 * trivial to implement (see struct dvbt_get_status_msg).
645 * equivalent to FE_READ ioctls, but needs
646 * TPS -> linux-dvb parameter set conversion. Feel free
647 * to implement this and send us a patch if you need this
648 * functionality.
649 */
650 break;
651
652 case FE_GET_EVENT:
653 {
654 /**
655 * for now we only fill the status field. the parameters
656 * are trivial to fill as soon FE_GET_FRONTEND is done.
657 */
658 struct dvb_frontend_event __user *e = (void __user *) arg;
659 if (cinergyt2->pending_fe_events == 0) {
660 if (file->f_flags & O_NONBLOCK)
661 return -EWOULDBLOCK;
662 wait_event_interruptible(cinergyt2->poll_wq,
663 cinergyt2->pending_fe_events > 0);
664 }
665 cinergyt2->pending_fe_events = 0;
666 return cinergyt2_ioctl(inode, file, FE_READ_STATUS,
667 (unsigned long) &e->status);
668 }
669
670 default:
671 ;
672 }
673
674 return -EINVAL;
675}
676
677static int cinergyt2_mmap(struct file *file, struct vm_area_struct *vma)
678{
679 struct dvb_device *dvbdev = file->private_data;
680 struct cinergyt2 *cinergyt2 = dvbdev->priv;
681 int ret = 0;
682
683 lock_kernel();
684
685 if (vma->vm_flags & (VM_WRITE | VM_EXEC)) {
686 ret = -EPERM;
687 goto bailout;
688 }
689
690 if (vma->vm_end > vma->vm_start + STREAM_URB_COUNT * STREAM_BUF_SIZE) {
691 ret = -EINVAL;
692 goto bailout;
693 }
694
695 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
696 vma->vm_file = file;
697
698 ret = remap_pfn_range(vma, vma->vm_start,
699 virt_to_phys(cinergyt2->streambuf) >> PAGE_SHIFT,
700 vma->vm_end - vma->vm_start,
701 vma->vm_page_prot) ? -EAGAIN : 0;
702bailout:
703 unlock_kernel();
704 return ret;
705}
706
707static struct file_operations cinergyt2_fops = {
708 .owner = THIS_MODULE,
709 .ioctl = cinergyt2_ioctl,
710 .poll = cinergyt2_poll,
711 .open = cinergyt2_open,
712 .release = cinergyt2_release,
713 .mmap = cinergyt2_mmap
714};
715
716static struct dvb_device cinergyt2_fe_template = {
717 .users = ~0,
718 .writers = 1,
719 .readers = (~0)-1,
720 .fops = &cinergyt2_fops
721};
722
723#ifdef ENABLE_RC
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725static void cinergyt2_query_rc (void *data)
726{
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700727 struct cinergyt2 *cinergyt2 = data;
728 char buf[1] = { CINERGYT2_EP1_GET_RC_EVENTS };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 struct cinergyt2_rc_event rc_events[12];
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700730 int n, len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Ingo Molnar3593cab2006-02-07 06:49:14 -0200732 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return;
734
735 len = cinergyt2_command(cinergyt2, buf, sizeof(buf),
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700736 (char *) rc_events, sizeof(rc_events));
737 if (len < 0)
738 goto out;
739 if (len == 0) {
740 if (time_after(jiffies, cinergyt2->last_event_jiffies +
741 msecs_to_jiffies(150))) {
742 /* stop key repeat */
743 if (cinergyt2->rc_input_event != KEY_MAX) {
744 dprintk(1, "rc_input_event=%d Up\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, 0);
747 cinergyt2->rc_input_event = KEY_MAX;
748 }
749 cinergyt2->rc_last_code = ~0;
750 }
751 goto out;
752 }
753 cinergyt2->last_event_jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700755 for (n = 0; n < (len / sizeof(rc_events[0])); n++) {
756 dprintk(1, "rc_events[%d].value = %x, type=%x\n",
757 n, le32_to_cpu(rc_events[n].value), rc_events[n].type);
Martin Loschwitz38b32d62005-07-07 17:57:31 -0700758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (rc_events[n].type == CINERGYT2_RC_EVENT_TYPE_NEC &&
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700760 rc_events[n].value == ~0) {
761 /* keyrepeat bit -> just repeat last rc_input_event */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 } else {
763 cinergyt2->rc_input_event = KEY_MAX;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500764 for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3) {
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700765 if (rc_keys[i + 0] == rc_events[n].type &&
766 rc_keys[i + 1] == le32_to_cpu(rc_events[n].value)) {
767 cinergyt2->rc_input_event = rc_keys[i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 break;
769 }
770 }
771 }
772
773 if (cinergyt2->rc_input_event != KEY_MAX) {
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700774 if (rc_events[n].value == cinergyt2->rc_last_code &&
775 cinergyt2->rc_last_code != ~0) {
776 /* emit a key-up so the double event is recognized */
777 dprintk(1, "rc_input_event=%d UP\n", cinergyt2->rc_input_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500778 input_report_key(cinergyt2->rc_input_dev,
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700779 cinergyt2->rc_input_event, 0);
780 }
781 dprintk(1, "rc_input_event=%d\n", cinergyt2->rc_input_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500782 input_report_key(cinergyt2->rc_input_dev,
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700783 cinergyt2->rc_input_event, 1);
784 cinergyt2->rc_last_code = rc_events[n].value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786 }
787
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700788out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 schedule_delayed_work(&cinergyt2->rc_query_work,
790 msecs_to_jiffies(RC_QUERY_INTERVAL));
791
Ingo Molnar3593cab2006-02-07 06:49:14 -0200792 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500794
795static int cinergyt2_register_rc(struct cinergyt2 *cinergyt2)
796{
797 struct input_dev *input_dev;
798 int i;
799
800 cinergyt2->rc_input_dev = input_dev = input_allocate_device();
801 if (!input_dev)
802 return -ENOMEM;
803
804 usb_make_path(cinergyt2->udev, cinergyt2->phys, sizeof(cinergyt2->phys));
805 strlcat(cinergyt2->phys, "/input0", sizeof(cinergyt2->phys));
806 cinergyt2->rc_input_event = KEY_MAX;
807 cinergyt2->rc_last_code = ~0;
808 INIT_WORK(&cinergyt2->rc_query_work, cinergyt2_query_rc, cinergyt2);
809
810 input_dev->name = DRIVER_NAME " remote control";
811 input_dev->phys = cinergyt2->phys;
812 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
Dmitry Torokhovdb93a822005-11-20 11:13:29 -0500813 for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3)
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500814 set_bit(rc_keys[i + 2], input_dev->keybit);
815 input_dev->keycodesize = 0;
816 input_dev->keycodemax = 0;
817
818 input_register_device(cinergyt2->rc_input_dev);
819 schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
David S. Miller7b5603e2005-11-16 00:11:50 -0800820
821 return 0;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500822}
823
824static void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2)
825{
826 cancel_delayed_work(&cinergyt2->rc_query_work);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500827 input_unregister_device(cinergyt2->rc_input_dev);
828}
829
830static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2)
831{
832 cancel_delayed_work(&cinergyt2->rc_query_work);
833}
834
835static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2)
836{
837 schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
838}
839
840#else
841
842static inline int cinergyt2_register_rc(struct cinergyt2 *cinergyt2) { return 0; }
843static inline void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2) { }
844static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2) { }
845static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2) { }
846
847#endif /* ENABLE_RC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849static void cinergyt2_query (void *data)
850{
851 struct cinergyt2 *cinergyt2 = (struct cinergyt2 *) data;
852 char cmd [] = { CINERGYT2_EP1_GET_TUNER_STATUS };
853 struct dvbt_get_status_msg *s = &cinergyt2->status;
854 uint8_t lock_bits;
855 uint32_t unc;
856
Ingo Molnar3593cab2006-02-07 06:49:14 -0200857 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return;
859
860 unc = s->uncorrected_block_count;
861 lock_bits = s->lock_bits;
862
863 cinergyt2_command(cinergyt2, cmd, sizeof(cmd), (char *) s, sizeof(*s));
864
865 unc += le32_to_cpu(s->uncorrected_block_count);
866 s->uncorrected_block_count = unc;
867
868 if (lock_bits != s->lock_bits) {
869 wake_up_interruptible(&cinergyt2->poll_wq);
870 cinergyt2->pending_fe_events++;
871 }
872
873 schedule_delayed_work(&cinergyt2->query_work,
874 msecs_to_jiffies(QUERY_INTERVAL));
875
Ingo Molnar3593cab2006-02-07 06:49:14 -0200876 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
879static int cinergyt2_probe (struct usb_interface *intf,
880 const struct usb_device_id *id)
881{
882 struct cinergyt2 *cinergyt2;
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700883 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (!(cinergyt2 = kmalloc (sizeof(struct cinergyt2), GFP_KERNEL))) {
886 dprintk(1, "out of memory?!?\n");
887 return -ENOMEM;
888 }
889
890 memset (cinergyt2, 0, sizeof (struct cinergyt2));
891 usb_set_intfdata (intf, (void *) cinergyt2);
892
Ingo Molnar3593cab2006-02-07 06:49:14 -0200893 mutex_init(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 init_waitqueue_head (&cinergyt2->poll_wq);
895 INIT_WORK(&cinergyt2->query_work, cinergyt2_query, cinergyt2);
896
897 cinergyt2->udev = interface_to_usbdev(intf);
898 cinergyt2->param.cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
899
900 if (cinergyt2_alloc_stream_urbs (cinergyt2) < 0) {
901 dprintk(1, "unable to allocate stream urbs\n");
902 kfree(cinergyt2);
903 return -ENOMEM;
904 }
905
Andrew de Quinceyd09dbf92006-04-10 09:27:37 -0300906 if ((err = dvb_register_adapter(&cinergyt2->adapter, DRIVER_NAME, THIS_MODULE, &cinergyt2->udev->dev)) < 0) {
Andrew de Quinceya064fad2006-04-06 17:05:46 -0300907 kfree(cinergyt2);
908 return err;
909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 cinergyt2->demux.priv = cinergyt2;
912 cinergyt2->demux.filternum = 256;
913 cinergyt2->demux.feednum = 256;
914 cinergyt2->demux.start_feed = cinergyt2_start_feed;
915 cinergyt2->demux.stop_feed = cinergyt2_stop_feed;
916 cinergyt2->demux.dmx.capabilities = DMX_TS_FILTERING |
917 DMX_SECTION_FILTERING |
918 DMX_MEMORY_BASED_FILTERING;
919
920 if ((err = dvb_dmx_init(&cinergyt2->demux)) < 0) {
921 dprintk(1, "dvb_dmx_init() failed (err = %d)\n", err);
922 goto bailout;
923 }
924
925 cinergyt2->dmxdev.filternum = cinergyt2->demux.filternum;
926 cinergyt2->dmxdev.demux = &cinergyt2->demux.dmx;
927 cinergyt2->dmxdev.capabilities = 0;
928
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700929 if ((err = dvb_dmxdev_init(&cinergyt2->dmxdev, &cinergyt2->adapter)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 dprintk(1, "dvb_dmxdev_init() failed (err = %d)\n", err);
931 goto bailout;
932 }
933
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700934 if (dvb_net_init(&cinergyt2->adapter, &cinergyt2->dvbnet, &cinergyt2->demux.dmx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 dprintk(1, "dvb_net_init() failed!\n");
936
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700937 dvb_register_device(&cinergyt2->adapter, &cinergyt2->fedev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 &cinergyt2_fe_template, cinergyt2,
939 DVB_DEVICE_FRONTEND);
940
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500941 err = cinergyt2_register_rc(cinergyt2);
942 if (err)
943 goto bailout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return 0;
946
947bailout:
Markus Rechberger77cc5312006-03-15 09:31:31 -0300948 dvb_net_release(&cinergyt2->dvbnet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 dvb_dmxdev_release(&cinergyt2->dmxdev);
950 dvb_dmx_release(&cinergyt2->demux);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500951 dvb_unregister_adapter(&cinergyt2->adapter);
952 cinergyt2_free_stream_urbs(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 kfree(cinergyt2);
954 return -ENOMEM;
955}
956
957static void cinergyt2_disconnect (struct usb_interface *intf)
958{
959 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
960
Deti Fliegle4acba32006-01-09 15:25:23 -0200961 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500963 cinergyt2_unregister_rc(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Deti Fliegle4acba32006-01-09 15:25:23 -0200965 cancel_delayed_work(&cinergyt2->query_work);
966 wake_up_interruptible(&cinergyt2->poll_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Deti Fliegle4acba32006-01-09 15:25:23 -0200968 cinergyt2->demux.dmx.close(&cinergyt2->demux.dmx);
969 cinergyt2->disconnect_pending = 1;
970
971 if (!atomic_read(&cinergyt2->inuse))
972 cinergyt2_unregister(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Pavel Macheka2910682005-04-16 15:25:27 -0700975static int cinergyt2_suspend (struct usb_interface *intf, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
978
Ingo Molnar3593cab2006-02-07 06:49:14 -0200979 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return -ERESTARTSYS;
981
David Brownell8b4b8a22006-08-14 23:11:03 -0700982 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500984
985 cinergyt2_suspend_rc(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 cancel_delayed_work(&cinergyt2->query_work);
987 if (cinergyt2->streaming)
988 cinergyt2_stop_stream_xfer(cinergyt2);
989 flush_scheduled_work();
990 cinergyt2_sleep(cinergyt2, 1);
991 }
992
Ingo Molnar3593cab2006-02-07 06:49:14 -0200993 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return 0;
995}
996
997static int cinergyt2_resume (struct usb_interface *intf)
998{
999 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
1000 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
1001
Ingo Molnar3593cab2006-02-07 06:49:14 -02001002 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return -ERESTARTSYS;
1004
1005 if (!cinergyt2->sleeping) {
1006 cinergyt2_sleep(cinergyt2, 0);
1007 cinergyt2_command(cinergyt2, (char *) param, sizeof(*param), NULL, 0);
1008 if (cinergyt2->streaming)
1009 cinergyt2_start_stream_xfer(cinergyt2);
1010 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
1011 }
1012
Dmitry Torokhovb7df3912005-09-15 02:01:53 -05001013 cinergyt2_resume_rc(cinergyt2);
1014
Ingo Molnar3593cab2006-02-07 06:49:14 -02001015 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return 0;
1017}
1018
1019static const struct usb_device_id cinergyt2_table [] __devinitdata = {
1020 { USB_DEVICE(0x0ccd, 0x0038) },
1021 { 0 }
1022};
1023
1024MODULE_DEVICE_TABLE(usb, cinergyt2_table);
1025
1026static struct usb_driver cinergyt2_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 .name = "cinergyT2",
1028 .probe = cinergyt2_probe,
1029 .disconnect = cinergyt2_disconnect,
1030 .suspend = cinergyt2_suspend,
1031 .resume = cinergyt2_resume,
1032 .id_table = cinergyt2_table
1033};
1034
1035static int __init cinergyt2_init (void)
1036{
1037 int err;
1038
1039 if ((err = usb_register(&cinergyt2_driver)) < 0)
1040 dprintk(1, "usb_register() failed! (err %i)\n", err);
1041
1042 return err;
1043}
1044
1045static void __exit cinergyt2_exit (void)
1046{
1047 usb_deregister(&cinergyt2_driver);
1048}
1049
1050module_init (cinergyt2_init);
1051module_exit (cinergyt2_exit);
1052
1053MODULE_LICENSE("GPL");
1054MODULE_AUTHOR("Holger Waechtler, Daniel Mack");