blob: 206c13e47a06c28fef0f03ac6ad8a237fd64d878 [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>
Al Virof23f6e02006-10-20 15:17:02 -040033#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include "dmxdev.h"
36#include "dvb_demux.h"
37#include "dvb_net.h"
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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)
Johannes Stezenbach6d789332005-09-09 13:03:05 -070051 #define RC_QUERY_INTERVAL (50) /* milliseconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 #define QUERY_INTERVAL (333) /* milliseconds */
53#endif
54
55#define DRIVER_NAME "TerraTec/qanu USB2.0 Highspeed DVB-T Receiver"
56
57static int debug;
58module_param_named(debug, debug, int, 0644);
59MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
60
61#define dprintk(level, args...) \
62do { \
63 if ((debug & level)) { \
Sam Ravnborg367cb702006-01-06 21:17:50 +010064 printk("%s: %s(): ", KBUILD_MODNAME, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 __FUNCTION__); \
66 printk(args); } \
67} while (0)
68
69enum 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
81struct 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
89struct 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
103static 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
117struct cinergyt2 {
118 struct dvb_demux demux;
119 struct usb_device *udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200120 struct mutex sem;
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700121 struct dvb_adapter adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 struct dvb_device *fedev;
123 struct dmxdev dmxdev;
124 struct dvb_net dvbnet;
125
126 int streaming;
127 int sleeping;
128
129 struct dvbt_set_parameters_msg param;
130 struct dvbt_get_status_msg status;
David Howellsc4028952006-11-22 14:57:56 +0000131 struct delayed_work query_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 wait_queue_head_t poll_wq;
134 int pending_fe_events;
Deti Fliegle4acba32006-01-09 15:25:23 -0200135 int disconnect_pending;
136 atomic_t inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 void *streambuf;
139 dma_addr_t streambuf_dmahandle;
140 struct urb *stream_urb [STREAM_URB_COUNT];
141
142#ifdef ENABLE_RC
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500143 struct input_dev *rc_input_dev;
144 char phys[64];
David Howellsc4028952006-11-22 14:57:56 +0000145 struct delayed_work rc_query_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 int rc_input_event;
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700147 u32 rc_last_code;
148 unsigned long last_event_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#endif
150};
151
152enum {
153 CINERGYT2_RC_EVENT_TYPE_NONE = 0x00,
154 CINERGYT2_RC_EVENT_TYPE_NEC = 0x01,
155 CINERGYT2_RC_EVENT_TYPE_RC5 = 0x02
156};
157
158struct cinergyt2_rc_event {
159 char type;
160 uint32_t value;
161} __attribute__((packed));
162
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700163static const uint32_t rc_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfe01eb04, KEY_POWER,
165 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfd02eb04, KEY_1,
166 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfc03eb04, KEY_2,
167 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfb04eb04, KEY_3,
168 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfa05eb04, KEY_4,
169 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf906eb04, KEY_5,
170 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf807eb04, KEY_6,
171 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf708eb04, KEY_7,
172 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf609eb04, KEY_8,
173 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf50aeb04, KEY_9,
174 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf30ceb04, KEY_0,
175 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf40beb04, KEY_VIDEO,
176 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf20deb04, KEY_REFRESH,
177 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf10eeb04, KEY_SELECT,
178 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf00feb04, KEY_EPG,
179 CINERGYT2_RC_EVENT_TYPE_NEC, 0xef10eb04, KEY_UP,
180 CINERGYT2_RC_EVENT_TYPE_NEC, 0xeb14eb04, KEY_DOWN,
181 CINERGYT2_RC_EVENT_TYPE_NEC, 0xee11eb04, KEY_LEFT,
182 CINERGYT2_RC_EVENT_TYPE_NEC, 0xec13eb04, KEY_RIGHT,
183 CINERGYT2_RC_EVENT_TYPE_NEC, 0xed12eb04, KEY_OK,
184 CINERGYT2_RC_EVENT_TYPE_NEC, 0xea15eb04, KEY_TEXT,
185 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe916eb04, KEY_INFO,
186 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe817eb04, KEY_RED,
187 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe718eb04, KEY_GREEN,
188 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe619eb04, KEY_YELLOW,
189 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe51aeb04, KEY_BLUE,
190 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe31ceb04, KEY_VOLUMEUP,
191 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe11eeb04, KEY_VOLUMEDOWN,
192 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe21deb04, KEY_MUTE,
193 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe41beb04, KEY_CHANNELUP,
194 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe01feb04, KEY_CHANNELDOWN,
195 CINERGYT2_RC_EVENT_TYPE_NEC, 0xbf40eb04, KEY_PAUSE,
196 CINERGYT2_RC_EVENT_TYPE_NEC, 0xb34ceb04, KEY_PLAY,
197 CINERGYT2_RC_EVENT_TYPE_NEC, 0xa758eb04, KEY_RECORD,
198 CINERGYT2_RC_EVENT_TYPE_NEC, 0xab54eb04, KEY_PREVIOUS,
199 CINERGYT2_RC_EVENT_TYPE_NEC, 0xb748eb04, KEY_STOP,
200 CINERGYT2_RC_EVENT_TYPE_NEC, 0xa35ceb04, KEY_NEXT
201};
202
203static int cinergyt2_command (struct cinergyt2 *cinergyt2,
204 char *send_buf, int send_buf_len,
205 char *recv_buf, int recv_buf_len)
206{
207 int actual_len;
208 char dummy;
209 int ret;
210
211 ret = usb_bulk_msg(cinergyt2->udev, usb_sndbulkpipe(cinergyt2->udev, 1),
212 send_buf, send_buf_len, &actual_len, 1000);
213
214 if (ret)
215 dprintk(1, "usb_bulk_msg (send) failed, err %i\n", ret);
216
217 if (!recv_buf)
218 recv_buf = &dummy;
219
220 ret = usb_bulk_msg(cinergyt2->udev, usb_rcvbulkpipe(cinergyt2->udev, 1),
221 recv_buf, recv_buf_len, &actual_len, 1000);
222
223 if (ret)
224 dprintk(1, "usb_bulk_msg (read) failed, err %i\n", ret);
225
226 return ret ? ret : actual_len;
227}
228
229static void cinergyt2_control_stream_transfer (struct cinergyt2 *cinergyt2, int enable)
230{
231 char buf [] = { CINERGYT2_EP1_CONTROL_STREAM_TRANSFER, enable ? 1 : 0 };
232 cinergyt2_command(cinergyt2, buf, sizeof(buf), NULL, 0);
233}
234
235static void cinergyt2_sleep (struct cinergyt2 *cinergyt2, int sleep)
236{
237 char buf [] = { CINERGYT2_EP1_SLEEP_MODE, sleep ? 1 : 0 };
238 cinergyt2_command(cinergyt2, buf, sizeof(buf), NULL, 0);
239 cinergyt2->sleeping = sleep;
240}
241
David Howells7d12e782006-10-05 14:55:46 +0100242static void cinergyt2_stream_irq (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244static int cinergyt2_submit_stream_urb (struct cinergyt2 *cinergyt2, struct urb *urb)
245{
246 int err;
247
248 usb_fill_bulk_urb(urb,
249 cinergyt2->udev,
250 usb_rcvbulkpipe(cinergyt2->udev, 0x2),
251 urb->transfer_buffer,
252 STREAM_BUF_SIZE,
253 cinergyt2_stream_irq,
254 cinergyt2);
255
256 if ((err = usb_submit_urb(urb, GFP_ATOMIC)))
257 dprintk(1, "urb submission failed (err = %i)!\n", err);
258
259 return err;
260}
261
David Howells7d12e782006-10-05 14:55:46 +0100262static void cinergyt2_stream_irq (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 struct cinergyt2 *cinergyt2 = urb->context;
265
266 if (urb->actual_length > 0)
267 dvb_dmx_swfilter(&cinergyt2->demux,
268 urb->transfer_buffer, urb->actual_length);
269
270 if (cinergyt2->streaming)
271 cinergyt2_submit_stream_urb(cinergyt2, urb);
272}
273
274static void cinergyt2_free_stream_urbs (struct cinergyt2 *cinergyt2)
275{
276 int i;
277
278 for (i=0; i<STREAM_URB_COUNT; i++)
Mariusz Kozlowski4064fe42006-11-08 15:34:22 +0100279 usb_free_urb(cinergyt2->stream_urb[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Guido Guenther574780d2005-11-16 00:08:44 -0800281 usb_buffer_free(cinergyt2->udev, STREAM_URB_COUNT*STREAM_BUF_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 cinergyt2->streambuf, cinergyt2->streambuf_dmahandle);
283}
284
285static int cinergyt2_alloc_stream_urbs (struct cinergyt2 *cinergyt2)
286{
287 int i;
288
Guido Guenther574780d2005-11-16 00:08:44 -0800289 cinergyt2->streambuf = usb_buffer_alloc(cinergyt2->udev, STREAM_URB_COUNT*STREAM_BUF_SIZE,
290 SLAB_KERNEL, &cinergyt2->streambuf_dmahandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (!cinergyt2->streambuf) {
292 dprintk(1, "failed to alloc consistent stream memory area, bailing out!\n");
293 return -ENOMEM;
294 }
295
296 memset(cinergyt2->streambuf, 0, STREAM_URB_COUNT*STREAM_BUF_SIZE);
297
298 for (i=0; i<STREAM_URB_COUNT; i++) {
299 struct urb *urb;
300
301 if (!(urb = usb_alloc_urb(0, GFP_ATOMIC))) {
302 dprintk(1, "failed to alloc consistent stream urbs, bailing out!\n");
303 cinergyt2_free_stream_urbs(cinergyt2);
304 return -ENOMEM;
305 }
306
307 urb->transfer_buffer = cinergyt2->streambuf + i * STREAM_BUF_SIZE;
308 urb->transfer_buffer_length = STREAM_BUF_SIZE;
309
310 cinergyt2->stream_urb[i] = urb;
311 }
312
313 return 0;
314}
315
316static void cinergyt2_stop_stream_xfer (struct cinergyt2 *cinergyt2)
317{
318 int i;
319
320 cinergyt2_control_stream_transfer(cinergyt2, 0);
321
322 for (i=0; i<STREAM_URB_COUNT; i++)
Mariusz Kozlowski4064fe42006-11-08 15:34:22 +0100323 usb_kill_urb(cinergyt2->stream_urb[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
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
Ingo Molnar3593cab2006-02-07 06:49:14 -0200347 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return -ERESTARTSYS;
349
350 if (cinergyt2->streaming == 0)
351 cinergyt2_start_stream_xfer(cinergyt2);
352
353 cinergyt2->streaming++;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200354 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 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
Ingo Molnar3593cab2006-02-07 06:49:14 -0200363 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return -ERESTARTSYS;
365
366 if (--cinergyt2->streaming == 0)
367 cinergyt2_stop_stream_xfer(cinergyt2);
368
Ingo Molnar3593cab2006-02-07 06:49:14 -0200369 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 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;
Deti Fliegle4acba32006-01-09 15:25:23 -0200483 int err = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Ingo Molnar3593cab2006-02-07 06:49:14 -0200485 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -ERESTARTSYS;
487
Deti Fliegle4acba32006-01-09 15:25:23 -0200488 if ((err = dvb_generic_open(inode, file))) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200489 mutex_unlock(&cinergyt2->sem);
Deti Fliegle4acba32006-01-09 15:25:23 -0200490 return err;
491 }
492
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
495 cinergyt2_sleep(cinergyt2, 0);
496 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
497 }
498
Deti Fliegle4acba32006-01-09 15:25:23 -0200499 atomic_inc(&cinergyt2->inuse);
500
Ingo Molnar3593cab2006-02-07 06:49:14 -0200501 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return 0;
503}
504
Deti Fliegle4acba32006-01-09 15:25:23 -0200505static void cinergyt2_unregister(struct cinergyt2 *cinergyt2)
506{
Markus Rechberger77cc5312006-03-15 09:31:31 -0300507 dvb_net_release(&cinergyt2->dvbnet);
508 dvb_dmxdev_release(&cinergyt2->dmxdev);
509 dvb_dmx_release(&cinergyt2->demux);
Deti Fliegle4acba32006-01-09 15:25:23 -0200510 dvb_unregister_device(cinergyt2->fedev);
511 dvb_unregister_adapter(&cinergyt2->adapter);
512
513 cinergyt2_free_stream_urbs(cinergyt2);
514 kfree(cinergyt2);
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517static int cinergyt2_release (struct inode *inode, struct file *file)
518{
519 struct dvb_device *dvbdev = file->private_data;
520 struct cinergyt2 *cinergyt2 = dvbdev->priv;
521
Ingo Molnar3593cab2006-02-07 06:49:14 -0200522 if (mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return -ERESTARTSYS;
524
Deti Fliegle4acba32006-01-09 15:25:23 -0200525 if (!cinergyt2->disconnect_pending && (file->f_flags & O_ACCMODE) != O_RDONLY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 cancel_delayed_work(&cinergyt2->query_work);
527 flush_scheduled_work();
528 cinergyt2_sleep(cinergyt2, 1);
529 }
530
Ingo Molnar3593cab2006-02-07 06:49:14 -0200531 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Deti Fliegle4acba32006-01-09 15:25:23 -0200533 if (atomic_dec_and_test(&cinergyt2->inuse) && cinergyt2->disconnect_pending) {
534 warn("delayed unregister in release");
535 cinergyt2_unregister(cinergyt2);
536 }
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return dvb_generic_release(inode, file);
539}
540
541static unsigned int cinergyt2_poll (struct file *file, struct poll_table_struct *wait)
542{
543 struct dvb_device *dvbdev = file->private_data;
544 struct cinergyt2 *cinergyt2 = dvbdev->priv;
Dyks, Axel (XL)774dd5d2006-06-11 17:14:35 -0300545 unsigned int mask = 0;
Deti Fliegle4acba32006-01-09 15:25:23 -0200546
Ingo Molnar3593cab2006-02-07 06:49:14 -0200547 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Deti Fliegle4acba32006-01-09 15:25:23 -0200548 return -ERESTARTSYS;
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 poll_wait(file, &cinergyt2->poll_wq, wait);
Deti Fliegle4acba32006-01-09 15:25:23 -0200551
Dyks, Axel (XL)774dd5d2006-06-11 17:14:35 -0300552 if (cinergyt2->pending_fe_events != 0)
Michael Krufkyfd174c62006-06-12 13:40:37 -0300553 mask |= (POLLIN | POLLRDNORM | POLLPRI);
Dyks, Axel (XL)774dd5d2006-06-11 17:14:35 -0300554
Ingo Molnar3593cab2006-02-07 06:49:14 -0200555 mutex_unlock(&cinergyt2->sem);
Deti Fliegle4acba32006-01-09 15:25:23 -0200556
Dyks, Axel (XL)774dd5d2006-06-11 17:14:35 -0300557 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560
561static int cinergyt2_ioctl (struct inode *inode, struct file *file,
562 unsigned cmd, unsigned long arg)
563{
564 struct dvb_device *dvbdev = file->private_data;
565 struct cinergyt2 *cinergyt2 = dvbdev->priv;
566 struct dvbt_get_status_msg *stat = &cinergyt2->status;
567 fe_status_t status = 0;
568
569 switch (cmd) {
570 case FE_GET_INFO:
571 return copy_to_user((void __user*) arg, &cinergyt2_fe_info,
572 sizeof(struct dvb_frontend_info));
573
574 case FE_READ_STATUS:
575 if (0xffff - le16_to_cpu(stat->gain) > 30)
576 status |= FE_HAS_SIGNAL;
577 if (stat->lock_bits & (1 << 6))
578 status |= FE_HAS_LOCK;
579 if (stat->lock_bits & (1 << 5))
580 status |= FE_HAS_SYNC;
581 if (stat->lock_bits & (1 << 4))
582 status |= FE_HAS_CARRIER;
583 if (stat->lock_bits & (1 << 1))
584 status |= FE_HAS_VITERBI;
585
586 return copy_to_user((void __user*) arg, &status, sizeof(status));
587
588 case FE_READ_BER:
589 return put_user(le32_to_cpu(stat->viterbi_error_rate),
590 (__u32 __user *) arg);
591
592 case FE_READ_SIGNAL_STRENGTH:
593 return put_user(0xffff - le16_to_cpu(stat->gain),
594 (__u16 __user *) arg);
595
596 case FE_READ_SNR:
597 return put_user((stat->snr << 8) | stat->snr,
598 (__u16 __user *) arg);
599
600 case FE_READ_UNCORRECTED_BLOCKS:
Stephen Williams2c3f11b2006-01-09 15:25:21 -0200601 {
602 uint32_t unc_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Stephen Williams2c3f11b2006-01-09 15:25:21 -0200604 unc_count = stat->uncorrected_block_count;
605 stat->uncorrected_block_count = 0;
606
607 /* UNC are already converted to host byte order... */
608 return put_user(unc_count,(__u32 __user *) arg);
609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 case FE_SET_FRONTEND:
611 {
612 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
613 struct dvb_frontend_parameters p;
614 int err;
615
616 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
617 return -EPERM;
618
619 if (copy_from_user(&p, (void __user*) arg, sizeof(p)))
620 return -EFAULT;
621
Ingo Molnar3593cab2006-02-07 06:49:14 -0200622 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -ERESTARTSYS;
624
625 param->cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
626 param->tps = cpu_to_le16(compute_tps(&p));
627 param->freq = cpu_to_le32(p.frequency / 1000);
628 param->bandwidth = 8 - p.u.ofdm.bandwidth - BANDWIDTH_8_MHZ;
629
630 stat->lock_bits = 0;
631 cinergyt2->pending_fe_events++;
632 wake_up_interruptible(&cinergyt2->poll_wq);
633
634 err = cinergyt2_command(cinergyt2,
635 (char *) param, sizeof(*param),
636 NULL, 0);
637
Ingo Molnar3593cab2006-02-07 06:49:14 -0200638 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 return (err < 0) ? err : 0;
641 }
642
643 case FE_GET_FRONTEND:
644 /**
645 * trivial to implement (see struct dvbt_get_status_msg).
646 * equivalent to FE_READ ioctls, but needs
647 * TPS -> linux-dvb parameter set conversion. Feel free
648 * to implement this and send us a patch if you need this
649 * functionality.
650 */
651 break;
652
653 case FE_GET_EVENT:
654 {
655 /**
656 * for now we only fill the status field. the parameters
657 * are trivial to fill as soon FE_GET_FRONTEND is done.
658 */
659 struct dvb_frontend_event __user *e = (void __user *) arg;
660 if (cinergyt2->pending_fe_events == 0) {
661 if (file->f_flags & O_NONBLOCK)
662 return -EWOULDBLOCK;
663 wait_event_interruptible(cinergyt2->poll_wq,
664 cinergyt2->pending_fe_events > 0);
665 }
666 cinergyt2->pending_fe_events = 0;
667 return cinergyt2_ioctl(inode, file, FE_READ_STATUS,
668 (unsigned long) &e->status);
669 }
670
671 default:
672 ;
673 }
674
675 return -EINVAL;
676}
677
678static int cinergyt2_mmap(struct file *file, struct vm_area_struct *vma)
679{
680 struct dvb_device *dvbdev = file->private_data;
681 struct cinergyt2 *cinergyt2 = dvbdev->priv;
682 int ret = 0;
683
684 lock_kernel();
685
686 if (vma->vm_flags & (VM_WRITE | VM_EXEC)) {
687 ret = -EPERM;
688 goto bailout;
689 }
690
691 if (vma->vm_end > vma->vm_start + STREAM_URB_COUNT * STREAM_BUF_SIZE) {
692 ret = -EINVAL;
693 goto bailout;
694 }
695
696 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
697 vma->vm_file = file;
698
699 ret = remap_pfn_range(vma, vma->vm_start,
700 virt_to_phys(cinergyt2->streambuf) >> PAGE_SHIFT,
701 vma->vm_end - vma->vm_start,
702 vma->vm_page_prot) ? -EAGAIN : 0;
703bailout:
704 unlock_kernel();
705 return ret;
706}
707
708static struct file_operations cinergyt2_fops = {
709 .owner = THIS_MODULE,
710 .ioctl = cinergyt2_ioctl,
711 .poll = cinergyt2_poll,
712 .open = cinergyt2_open,
713 .release = cinergyt2_release,
714 .mmap = cinergyt2_mmap
715};
716
717static struct dvb_device cinergyt2_fe_template = {
718 .users = ~0,
719 .writers = 1,
720 .readers = (~0)-1,
721 .fops = &cinergyt2_fops
722};
723
724#ifdef ENABLE_RC
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500725
David Howellsc4028952006-11-22 14:57:56 +0000726static void cinergyt2_query_rc (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
David Howellsc4028952006-11-22 14:57:56 +0000728 struct cinergyt2 *cinergyt2 =
729 container_of(work, struct cinergyt2, rc_query_work.work);
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700730 char buf[1] = { CINERGYT2_EP1_GET_RC_EVENTS };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct cinergyt2_rc_event rc_events[12];
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700732 int n, len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Ingo Molnar3593cab2006-02-07 06:49:14 -0200734 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return;
736
737 len = cinergyt2_command(cinergyt2, buf, sizeof(buf),
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700738 (char *) rc_events, sizeof(rc_events));
739 if (len < 0)
740 goto out;
741 if (len == 0) {
742 if (time_after(jiffies, cinergyt2->last_event_jiffies +
743 msecs_to_jiffies(150))) {
744 /* stop key repeat */
745 if (cinergyt2->rc_input_event != KEY_MAX) {
746 dprintk(1, "rc_input_event=%d Up\n", cinergyt2->rc_input_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500747 input_report_key(cinergyt2->rc_input_dev,
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700748 cinergyt2->rc_input_event, 0);
749 cinergyt2->rc_input_event = KEY_MAX;
750 }
751 cinergyt2->rc_last_code = ~0;
752 }
753 goto out;
754 }
755 cinergyt2->last_event_jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700757 for (n = 0; n < (len / sizeof(rc_events[0])); n++) {
758 dprintk(1, "rc_events[%d].value = %x, type=%x\n",
759 n, le32_to_cpu(rc_events[n].value), rc_events[n].type);
Martin Loschwitz38b32d62005-07-07 17:57:31 -0700760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (rc_events[n].type == CINERGYT2_RC_EVENT_TYPE_NEC &&
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700762 rc_events[n].value == ~0) {
763 /* keyrepeat bit -> just repeat last rc_input_event */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 } else {
765 cinergyt2->rc_input_event = KEY_MAX;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500766 for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3) {
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700767 if (rc_keys[i + 0] == rc_events[n].type &&
768 rc_keys[i + 1] == le32_to_cpu(rc_events[n].value)) {
769 cinergyt2->rc_input_event = rc_keys[i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 break;
771 }
772 }
773 }
774
775 if (cinergyt2->rc_input_event != KEY_MAX) {
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700776 if (rc_events[n].value == cinergyt2->rc_last_code &&
777 cinergyt2->rc_last_code != ~0) {
778 /* emit a key-up so the double event is recognized */
779 dprintk(1, "rc_input_event=%d UP\n", cinergyt2->rc_input_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500780 input_report_key(cinergyt2->rc_input_dev,
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700781 cinergyt2->rc_input_event, 0);
782 }
783 dprintk(1, "rc_input_event=%d\n", cinergyt2->rc_input_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500784 input_report_key(cinergyt2->rc_input_dev,
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700785 cinergyt2->rc_input_event, 1);
786 cinergyt2->rc_last_code = rc_events[n].value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788 }
789
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700790out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 schedule_delayed_work(&cinergyt2->rc_query_work,
792 msecs_to_jiffies(RC_QUERY_INTERVAL));
793
Ingo Molnar3593cab2006-02-07 06:49:14 -0200794 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500796
797static int cinergyt2_register_rc(struct cinergyt2 *cinergyt2)
798{
799 struct input_dev *input_dev;
800 int i;
801
802 cinergyt2->rc_input_dev = input_dev = input_allocate_device();
803 if (!input_dev)
804 return -ENOMEM;
805
806 usb_make_path(cinergyt2->udev, cinergyt2->phys, sizeof(cinergyt2->phys));
807 strlcat(cinergyt2->phys, "/input0", sizeof(cinergyt2->phys));
808 cinergyt2->rc_input_event = KEY_MAX;
809 cinergyt2->rc_last_code = ~0;
David Howellsc4028952006-11-22 14:57:56 +0000810 INIT_DELAYED_WORK(&cinergyt2->rc_query_work, cinergyt2_query_rc);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500811
812 input_dev->name = DRIVER_NAME " remote control";
813 input_dev->phys = cinergyt2->phys;
814 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
Dmitry Torokhovdb93a822005-11-20 11:13:29 -0500815 for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3)
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500816 set_bit(rc_keys[i + 2], input_dev->keybit);
817 input_dev->keycodesize = 0;
818 input_dev->keycodemax = 0;
819
820 input_register_device(cinergyt2->rc_input_dev);
821 schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
David S. Miller7b5603e2005-11-16 00:11:50 -0800822
823 return 0;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500824}
825
826static void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2)
827{
828 cancel_delayed_work(&cinergyt2->rc_query_work);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500829 input_unregister_device(cinergyt2->rc_input_dev);
830}
831
832static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2)
833{
834 cancel_delayed_work(&cinergyt2->rc_query_work);
835}
836
837static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2)
838{
839 schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
840}
841
842#else
843
844static inline int cinergyt2_register_rc(struct cinergyt2 *cinergyt2) { return 0; }
845static inline void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2) { }
846static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2) { }
847static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2) { }
848
849#endif /* ENABLE_RC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
David Howellsc4028952006-11-22 14:57:56 +0000851static void cinergyt2_query (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
David Howellsc4028952006-11-22 14:57:56 +0000853 struct cinergyt2 *cinergyt2 =
854 container_of(work, struct cinergyt2, query_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 char cmd [] = { CINERGYT2_EP1_GET_TUNER_STATUS };
856 struct dvbt_get_status_msg *s = &cinergyt2->status;
857 uint8_t lock_bits;
858 uint32_t unc;
859
Ingo Molnar3593cab2006-02-07 06:49:14 -0200860 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return;
862
863 unc = s->uncorrected_block_count;
864 lock_bits = s->lock_bits;
865
866 cinergyt2_command(cinergyt2, cmd, sizeof(cmd), (char *) s, sizeof(*s));
867
868 unc += le32_to_cpu(s->uncorrected_block_count);
869 s->uncorrected_block_count = unc;
870
871 if (lock_bits != s->lock_bits) {
872 wake_up_interruptible(&cinergyt2->poll_wq);
873 cinergyt2->pending_fe_events++;
874 }
875
876 schedule_delayed_work(&cinergyt2->query_work,
877 msecs_to_jiffies(QUERY_INTERVAL));
878
Ingo Molnar3593cab2006-02-07 06:49:14 -0200879 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
882static int cinergyt2_probe (struct usb_interface *intf,
883 const struct usb_device_id *id)
884{
885 struct cinergyt2 *cinergyt2;
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700886 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 if (!(cinergyt2 = kmalloc (sizeof(struct cinergyt2), GFP_KERNEL))) {
889 dprintk(1, "out of memory?!?\n");
890 return -ENOMEM;
891 }
892
893 memset (cinergyt2, 0, sizeof (struct cinergyt2));
894 usb_set_intfdata (intf, (void *) cinergyt2);
895
Ingo Molnar3593cab2006-02-07 06:49:14 -0200896 mutex_init(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 init_waitqueue_head (&cinergyt2->poll_wq);
David Howellsc4028952006-11-22 14:57:56 +0000898 INIT_DELAYED_WORK(&cinergyt2->query_work, cinergyt2_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 cinergyt2->udev = interface_to_usbdev(intf);
901 cinergyt2->param.cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
902
903 if (cinergyt2_alloc_stream_urbs (cinergyt2) < 0) {
904 dprintk(1, "unable to allocate stream urbs\n");
905 kfree(cinergyt2);
906 return -ENOMEM;
907 }
908
Andrew de Quinceyd09dbf92006-04-10 09:27:37 -0300909 if ((err = dvb_register_adapter(&cinergyt2->adapter, DRIVER_NAME, THIS_MODULE, &cinergyt2->udev->dev)) < 0) {
Andrew de Quinceya064fad2006-04-06 17:05:46 -0300910 kfree(cinergyt2);
911 return err;
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 cinergyt2->demux.priv = cinergyt2;
915 cinergyt2->demux.filternum = 256;
916 cinergyt2->demux.feednum = 256;
917 cinergyt2->demux.start_feed = cinergyt2_start_feed;
918 cinergyt2->demux.stop_feed = cinergyt2_stop_feed;
919 cinergyt2->demux.dmx.capabilities = DMX_TS_FILTERING |
920 DMX_SECTION_FILTERING |
921 DMX_MEMORY_BASED_FILTERING;
922
923 if ((err = dvb_dmx_init(&cinergyt2->demux)) < 0) {
924 dprintk(1, "dvb_dmx_init() failed (err = %d)\n", err);
925 goto bailout;
926 }
927
928 cinergyt2->dmxdev.filternum = cinergyt2->demux.filternum;
929 cinergyt2->dmxdev.demux = &cinergyt2->demux.dmx;
930 cinergyt2->dmxdev.capabilities = 0;
931
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700932 if ((err = dvb_dmxdev_init(&cinergyt2->dmxdev, &cinergyt2->adapter)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 dprintk(1, "dvb_dmxdev_init() failed (err = %d)\n", err);
934 goto bailout;
935 }
936
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700937 if (dvb_net_init(&cinergyt2->adapter, &cinergyt2->dvbnet, &cinergyt2->demux.dmx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 dprintk(1, "dvb_net_init() failed!\n");
939
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700940 dvb_register_device(&cinergyt2->adapter, &cinergyt2->fedev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 &cinergyt2_fe_template, cinergyt2,
942 DVB_DEVICE_FRONTEND);
943
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500944 err = cinergyt2_register_rc(cinergyt2);
945 if (err)
946 goto bailout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return 0;
949
950bailout:
Markus Rechberger77cc5312006-03-15 09:31:31 -0300951 dvb_net_release(&cinergyt2->dvbnet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 dvb_dmxdev_release(&cinergyt2->dmxdev);
953 dvb_dmx_release(&cinergyt2->demux);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500954 dvb_unregister_adapter(&cinergyt2->adapter);
955 cinergyt2_free_stream_urbs(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 kfree(cinergyt2);
957 return -ENOMEM;
958}
959
960static void cinergyt2_disconnect (struct usb_interface *intf)
961{
962 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
963
Deti Fliegle4acba32006-01-09 15:25:23 -0200964 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500966 cinergyt2_unregister_rc(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Deti Fliegle4acba32006-01-09 15:25:23 -0200968 cancel_delayed_work(&cinergyt2->query_work);
969 wake_up_interruptible(&cinergyt2->poll_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Deti Fliegle4acba32006-01-09 15:25:23 -0200971 cinergyt2->demux.dmx.close(&cinergyt2->demux.dmx);
972 cinergyt2->disconnect_pending = 1;
973
974 if (!atomic_read(&cinergyt2->inuse))
975 cinergyt2_unregister(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Pavel Macheka2910682005-04-16 15:25:27 -0700978static int cinergyt2_suspend (struct usb_interface *intf, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
981
Ingo Molnar3593cab2006-02-07 06:49:14 -0200982 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return -ERESTARTSYS;
984
David Brownell8b4b8a22006-08-14 23:11:03 -0700985 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500987
988 cinergyt2_suspend_rc(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 cancel_delayed_work(&cinergyt2->query_work);
990 if (cinergyt2->streaming)
991 cinergyt2_stop_stream_xfer(cinergyt2);
992 flush_scheduled_work();
993 cinergyt2_sleep(cinergyt2, 1);
994 }
995
Ingo Molnar3593cab2006-02-07 06:49:14 -0200996 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return 0;
998}
999
1000static int cinergyt2_resume (struct usb_interface *intf)
1001{
1002 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
1003 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
1004
Ingo Molnar3593cab2006-02-07 06:49:14 -02001005 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return -ERESTARTSYS;
1007
1008 if (!cinergyt2->sleeping) {
1009 cinergyt2_sleep(cinergyt2, 0);
1010 cinergyt2_command(cinergyt2, (char *) param, sizeof(*param), NULL, 0);
1011 if (cinergyt2->streaming)
1012 cinergyt2_start_stream_xfer(cinergyt2);
1013 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
1014 }
1015
Dmitry Torokhovb7df3912005-09-15 02:01:53 -05001016 cinergyt2_resume_rc(cinergyt2);
1017
Ingo Molnar3593cab2006-02-07 06:49:14 -02001018 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020}
1021
1022static const struct usb_device_id cinergyt2_table [] __devinitdata = {
1023 { USB_DEVICE(0x0ccd, 0x0038) },
1024 { 0 }
1025};
1026
1027MODULE_DEVICE_TABLE(usb, cinergyt2_table);
1028
1029static struct usb_driver cinergyt2_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 .name = "cinergyT2",
1031 .probe = cinergyt2_probe,
1032 .disconnect = cinergyt2_disconnect,
1033 .suspend = cinergyt2_suspend,
1034 .resume = cinergyt2_resume,
1035 .id_table = cinergyt2_table
1036};
1037
1038static int __init cinergyt2_init (void)
1039{
1040 int err;
1041
1042 if ((err = usb_register(&cinergyt2_driver)) < 0)
1043 dprintk(1, "usb_register() failed! (err %i)\n", err);
1044
1045 return err;
1046}
1047
1048static void __exit cinergyt2_exit (void)
1049{
1050 usb_deregister(&cinergyt2_driver);
1051}
1052
1053module_init (cinergyt2_init);
1054module_exit (cinergyt2_exit);
1055
1056MODULE_LICENSE("GPL");
1057MODULE_AUTHOR("Holger Waechtler, Daniel Mack");