blob: db08b0a8888afc5763e935a53e712844f8a217aa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jan Engelhardt96de0e22007-10-19 23:21:04 +02002 * TerraTec Cinergy T²/qanu USB2 DVB-T adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/input.h>
30#include <linux/dvb/frontend.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020031#include <linux/mutex.h>
Al Virof23f6e02006-10-20 15:17:02 -040032#include <linux/mm.h>
Jean Delvare6473d162007-03-06 02:45:12 -080033#include <asm/io.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;
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300121 struct mutex wq_sem;
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700122 struct dvb_adapter adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct dvb_device *fedev;
124 struct dmxdev dmxdev;
125 struct dvb_net dvbnet;
126
127 int streaming;
128 int sleeping;
129
130 struct dvbt_set_parameters_msg param;
131 struct dvbt_get_status_msg status;
David Howellsc4028952006-11-22 14:57:56 +0000132 struct delayed_work query_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 wait_queue_head_t poll_wq;
135 int pending_fe_events;
Deti Fliegle4acba32006-01-09 15:25:23 -0200136 int disconnect_pending;
137 atomic_t inuse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 void *streambuf;
140 dma_addr_t streambuf_dmahandle;
141 struct urb *stream_urb [STREAM_URB_COUNT];
142
143#ifdef ENABLE_RC
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500144 struct input_dev *rc_input_dev;
145 char phys[64];
David Howellsc4028952006-11-22 14:57:56 +0000146 struct delayed_work rc_query_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int rc_input_event;
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700148 u32 rc_last_code;
149 unsigned long last_event_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#endif
151};
152
153enum {
154 CINERGYT2_RC_EVENT_TYPE_NONE = 0x00,
155 CINERGYT2_RC_EVENT_TYPE_NEC = 0x01,
156 CINERGYT2_RC_EVENT_TYPE_RC5 = 0x02
157};
158
159struct cinergyt2_rc_event {
160 char type;
161 uint32_t value;
162} __attribute__((packed));
163
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700164static const uint32_t rc_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfe01eb04, KEY_POWER,
166 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfd02eb04, KEY_1,
167 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfc03eb04, KEY_2,
168 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfb04eb04, KEY_3,
169 CINERGYT2_RC_EVENT_TYPE_NEC, 0xfa05eb04, KEY_4,
170 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf906eb04, KEY_5,
171 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf807eb04, KEY_6,
172 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf708eb04, KEY_7,
173 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf609eb04, KEY_8,
174 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf50aeb04, KEY_9,
175 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf30ceb04, KEY_0,
176 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf40beb04, KEY_VIDEO,
177 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf20deb04, KEY_REFRESH,
178 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf10eeb04, KEY_SELECT,
179 CINERGYT2_RC_EVENT_TYPE_NEC, 0xf00feb04, KEY_EPG,
180 CINERGYT2_RC_EVENT_TYPE_NEC, 0xef10eb04, KEY_UP,
181 CINERGYT2_RC_EVENT_TYPE_NEC, 0xeb14eb04, KEY_DOWN,
182 CINERGYT2_RC_EVENT_TYPE_NEC, 0xee11eb04, KEY_LEFT,
183 CINERGYT2_RC_EVENT_TYPE_NEC, 0xec13eb04, KEY_RIGHT,
184 CINERGYT2_RC_EVENT_TYPE_NEC, 0xed12eb04, KEY_OK,
185 CINERGYT2_RC_EVENT_TYPE_NEC, 0xea15eb04, KEY_TEXT,
186 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe916eb04, KEY_INFO,
187 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe817eb04, KEY_RED,
188 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe718eb04, KEY_GREEN,
189 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe619eb04, KEY_YELLOW,
190 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe51aeb04, KEY_BLUE,
191 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe31ceb04, KEY_VOLUMEUP,
192 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe11eeb04, KEY_VOLUMEDOWN,
193 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe21deb04, KEY_MUTE,
194 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe41beb04, KEY_CHANNELUP,
195 CINERGYT2_RC_EVENT_TYPE_NEC, 0xe01feb04, KEY_CHANNELDOWN,
196 CINERGYT2_RC_EVENT_TYPE_NEC, 0xbf40eb04, KEY_PAUSE,
197 CINERGYT2_RC_EVENT_TYPE_NEC, 0xb34ceb04, KEY_PLAY,
198 CINERGYT2_RC_EVENT_TYPE_NEC, 0xa758eb04, KEY_RECORD,
199 CINERGYT2_RC_EVENT_TYPE_NEC, 0xab54eb04, KEY_PREVIOUS,
200 CINERGYT2_RC_EVENT_TYPE_NEC, 0xb748eb04, KEY_STOP,
201 CINERGYT2_RC_EVENT_TYPE_NEC, 0xa35ceb04, KEY_NEXT
202};
203
204static int cinergyt2_command (struct cinergyt2 *cinergyt2,
205 char *send_buf, int send_buf_len,
206 char *recv_buf, int recv_buf_len)
207{
208 int actual_len;
209 char dummy;
210 int ret;
211
212 ret = usb_bulk_msg(cinergyt2->udev, usb_sndbulkpipe(cinergyt2->udev, 1),
213 send_buf, send_buf_len, &actual_len, 1000);
214
215 if (ret)
216 dprintk(1, "usb_bulk_msg (send) failed, err %i\n", ret);
217
218 if (!recv_buf)
219 recv_buf = &dummy;
220
221 ret = usb_bulk_msg(cinergyt2->udev, usb_rcvbulkpipe(cinergyt2->udev, 1),
222 recv_buf, recv_buf_len, &actual_len, 1000);
223
224 if (ret)
225 dprintk(1, "usb_bulk_msg (read) failed, err %i\n", ret);
226
227 return ret ? ret : actual_len;
228}
229
230static void cinergyt2_control_stream_transfer (struct cinergyt2 *cinergyt2, int enable)
231{
232 char buf [] = { CINERGYT2_EP1_CONTROL_STREAM_TRANSFER, enable ? 1 : 0 };
233 cinergyt2_command(cinergyt2, buf, sizeof(buf), NULL, 0);
234}
235
236static void cinergyt2_sleep (struct cinergyt2 *cinergyt2, int sleep)
237{
238 char buf [] = { CINERGYT2_EP1_SLEEP_MODE, sleep ? 1 : 0 };
239 cinergyt2_command(cinergyt2, buf, sizeof(buf), NULL, 0);
240 cinergyt2->sleeping = sleep;
241}
242
David Howells7d12e782006-10-05 14:55:46 +0100243static void cinergyt2_stream_irq (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245static int cinergyt2_submit_stream_urb (struct cinergyt2 *cinergyt2, struct urb *urb)
246{
247 int err;
248
249 usb_fill_bulk_urb(urb,
250 cinergyt2->udev,
251 usb_rcvbulkpipe(cinergyt2->udev, 0x2),
252 urb->transfer_buffer,
253 STREAM_BUF_SIZE,
254 cinergyt2_stream_irq,
255 cinergyt2);
256
257 if ((err = usb_submit_urb(urb, GFP_ATOMIC)))
258 dprintk(1, "urb submission failed (err = %i)!\n", err);
259
260 return err;
261}
262
David Howells7d12e782006-10-05 14:55:46 +0100263static void cinergyt2_stream_irq (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 struct cinergyt2 *cinergyt2 = urb->context;
266
267 if (urb->actual_length > 0)
268 dvb_dmx_swfilter(&cinergyt2->demux,
269 urb->transfer_buffer, urb->actual_length);
270
271 if (cinergyt2->streaming)
272 cinergyt2_submit_stream_urb(cinergyt2, urb);
273}
274
275static void cinergyt2_free_stream_urbs (struct cinergyt2 *cinergyt2)
276{
277 int i;
278
279 for (i=0; i<STREAM_URB_COUNT; i++)
Mariusz Kozlowski4064fe42006-11-08 15:34:22 +0100280 usb_free_urb(cinergyt2->stream_urb[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Guido Guenther574780d2005-11-16 00:08:44 -0800282 usb_buffer_free(cinergyt2->udev, STREAM_URB_COUNT*STREAM_BUF_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 cinergyt2->streambuf, cinergyt2->streambuf_dmahandle);
284}
285
286static int cinergyt2_alloc_stream_urbs (struct cinergyt2 *cinergyt2)
287{
288 int i;
289
Guido Guenther574780d2005-11-16 00:08:44 -0800290 cinergyt2->streambuf = usb_buffer_alloc(cinergyt2->udev, STREAM_URB_COUNT*STREAM_BUF_SIZE,
Christoph Lametere94b1762006-12-06 20:33:17 -0800291 GFP_KERNEL, &cinergyt2->streambuf_dmahandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (!cinergyt2->streambuf) {
293 dprintk(1, "failed to alloc consistent stream memory area, bailing out!\n");
294 return -ENOMEM;
295 }
296
297 memset(cinergyt2->streambuf, 0, STREAM_URB_COUNT*STREAM_BUF_SIZE);
298
299 for (i=0; i<STREAM_URB_COUNT; i++) {
300 struct urb *urb;
301
302 if (!(urb = usb_alloc_urb(0, GFP_ATOMIC))) {
303 dprintk(1, "failed to alloc consistent stream urbs, bailing out!\n");
304 cinergyt2_free_stream_urbs(cinergyt2);
305 return -ENOMEM;
306 }
307
308 urb->transfer_buffer = cinergyt2->streambuf + i * STREAM_BUF_SIZE;
309 urb->transfer_buffer_length = STREAM_BUF_SIZE;
310
311 cinergyt2->stream_urb[i] = urb;
312 }
313
314 return 0;
315}
316
317static void cinergyt2_stop_stream_xfer (struct cinergyt2 *cinergyt2)
318{
319 int i;
320
321 cinergyt2_control_stream_transfer(cinergyt2, 0);
322
323 for (i=0; i<STREAM_URB_COUNT; i++)
Mariusz Kozlowski4064fe42006-11-08 15:34:22 +0100324 usb_kill_urb(cinergyt2->stream_urb[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327static int cinergyt2_start_stream_xfer (struct cinergyt2 *cinergyt2)
328{
329 int i, err;
330
331 for (i=0; i<STREAM_URB_COUNT; i++) {
332 if ((err = cinergyt2_submit_stream_urb(cinergyt2, cinergyt2->stream_urb[i]))) {
333 cinergyt2_stop_stream_xfer(cinergyt2);
334 dprintk(1, "failed urb submission (%i: err = %i)!\n", i, err);
335 return err;
336 }
337 }
338
339 cinergyt2_control_stream_transfer(cinergyt2, 1);
340 return 0;
341}
342
343static int cinergyt2_start_feed(struct dvb_demux_feed *dvbdmxfeed)
344{
345 struct dvb_demux *demux = dvbdmxfeed->demux;
346 struct cinergyt2 *cinergyt2 = demux->priv;
347
Jiri Slaby475d5262007-10-15 13:07:30 -0300348 if (cinergyt2->disconnect_pending)
349 return -EAGAIN;
350 if (mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -ERESTARTSYS;
352
353 if (cinergyt2->streaming == 0)
354 cinergyt2_start_stream_xfer(cinergyt2);
355
356 cinergyt2->streaming++;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200357 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return 0;
359}
360
361static int cinergyt2_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
362{
363 struct dvb_demux *demux = dvbdmxfeed->demux;
364 struct cinergyt2 *cinergyt2 = demux->priv;
365
Jiri Slaby475d5262007-10-15 13:07:30 -0300366 if (cinergyt2->disconnect_pending)
367 return -EAGAIN;
368 if (mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return -ERESTARTSYS;
370
371 if (--cinergyt2->streaming == 0)
372 cinergyt2_stop_stream_xfer(cinergyt2);
373
Ingo Molnar3593cab2006-02-07 06:49:14 -0200374 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
376}
377
378/**
379 * convert linux-dvb frontend parameter set into TPS.
380 * See ETSI ETS-300744, section 4.6.2, table 9 for details.
381 *
382 * This function is probably reusable and may better get placed in a support
383 * library.
384 *
385 * We replace errornous fields by default TPS fields (the ones with value 0).
386 */
387static uint16_t compute_tps (struct dvb_frontend_parameters *p)
388{
389 struct dvb_ofdm_parameters *op = &p->u.ofdm;
390 uint16_t tps = 0;
391
392 switch (op->code_rate_HP) {
393 case FEC_2_3:
394 tps |= (1 << 7);
395 break;
396 case FEC_3_4:
397 tps |= (2 << 7);
398 break;
399 case FEC_5_6:
400 tps |= (3 << 7);
401 break;
402 case FEC_7_8:
403 tps |= (4 << 7);
404 break;
405 case FEC_1_2:
406 case FEC_AUTO:
407 default:
408 /* tps |= (0 << 7) */;
409 }
410
411 switch (op->code_rate_LP) {
412 case FEC_2_3:
413 tps |= (1 << 4);
414 break;
415 case FEC_3_4:
416 tps |= (2 << 4);
417 break;
418 case FEC_5_6:
419 tps |= (3 << 4);
420 break;
421 case FEC_7_8:
422 tps |= (4 << 4);
423 break;
424 case FEC_1_2:
425 case FEC_AUTO:
426 default:
427 /* tps |= (0 << 4) */;
428 }
429
430 switch (op->constellation) {
431 case QAM_16:
432 tps |= (1 << 13);
433 break;
434 case QAM_64:
435 tps |= (2 << 13);
436 break;
437 case QPSK:
438 default:
439 /* tps |= (0 << 13) */;
440 }
441
442 switch (op->transmission_mode) {
443 case TRANSMISSION_MODE_8K:
444 tps |= (1 << 0);
445 break;
446 case TRANSMISSION_MODE_2K:
447 default:
448 /* tps |= (0 << 0) */;
449 }
450
451 switch (op->guard_interval) {
452 case GUARD_INTERVAL_1_16:
453 tps |= (1 << 2);
454 break;
455 case GUARD_INTERVAL_1_8:
456 tps |= (2 << 2);
457 break;
458 case GUARD_INTERVAL_1_4:
459 tps |= (3 << 2);
460 break;
461 case GUARD_INTERVAL_1_32:
462 default:
463 /* tps |= (0 << 2) */;
464 }
465
466 switch (op->hierarchy_information) {
467 case HIERARCHY_1:
468 tps |= (1 << 10);
469 break;
470 case HIERARCHY_2:
471 tps |= (2 << 10);
472 break;
473 case HIERARCHY_4:
474 tps |= (3 << 10);
475 break;
476 case HIERARCHY_NONE:
477 default:
478 /* tps |= (0 << 10) */;
479 }
480
481 return tps;
482}
483
484static int cinergyt2_open (struct inode *inode, struct file *file)
485{
486 struct dvb_device *dvbdev = file->private_data;
487 struct cinergyt2 *cinergyt2 = dvbdev->priv;
Jiri Slaby475d5262007-10-15 13:07:30 -0300488 int err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Jiri Slaby475d5262007-10-15 13:07:30 -0300490 if (cinergyt2->disconnect_pending)
491 goto out;
492 err = mutex_lock_interruptible(&cinergyt2->wq_sem);
493 if (err)
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Jiri Slaby475d5262007-10-15 13:07:30 -0300496 err = mutex_lock_interruptible(&cinergyt2->sem);
497 if (err)
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300498 goto out_unlock1;
Deti Fliegle4acba32006-01-09 15:25:23 -0200499
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300500 if ((err = dvb_generic_open(inode, file)))
501 goto out_unlock2;
Deti Fliegle4acba32006-01-09 15:25:23 -0200502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
504 cinergyt2_sleep(cinergyt2, 0);
505 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
506 }
507
Deti Fliegle4acba32006-01-09 15:25:23 -0200508 atomic_inc(&cinergyt2->inuse);
509
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300510out_unlock2:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200511 mutex_unlock(&cinergyt2->sem);
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300512out_unlock1:
513 mutex_unlock(&cinergyt2->wq_sem);
514out:
515 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
Deti Fliegle4acba32006-01-09 15:25:23 -0200518static void cinergyt2_unregister(struct cinergyt2 *cinergyt2)
519{
Markus Rechberger77cc5312006-03-15 09:31:31 -0300520 dvb_net_release(&cinergyt2->dvbnet);
521 dvb_dmxdev_release(&cinergyt2->dmxdev);
522 dvb_dmx_release(&cinergyt2->demux);
Deti Fliegle4acba32006-01-09 15:25:23 -0200523 dvb_unregister_device(cinergyt2->fedev);
524 dvb_unregister_adapter(&cinergyt2->adapter);
525
526 cinergyt2_free_stream_urbs(cinergyt2);
527 kfree(cinergyt2);
528}
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530static int cinergyt2_release (struct inode *inode, struct file *file)
531{
532 struct dvb_device *dvbdev = file->private_data;
533 struct cinergyt2 *cinergyt2 = dvbdev->priv;
534
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300535 mutex_lock(&cinergyt2->wq_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Deti Fliegle4acba32006-01-09 15:25:23 -0200537 if (!cinergyt2->disconnect_pending && (file->f_flags & O_ACCMODE) != O_RDONLY) {
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300538 cancel_rearming_delayed_work(&cinergyt2->query_work);
539
540 mutex_lock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 cinergyt2_sleep(cinergyt2, 1);
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300542 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300545 mutex_unlock(&cinergyt2->wq_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Deti Fliegle4acba32006-01-09 15:25:23 -0200547 if (atomic_dec_and_test(&cinergyt2->inuse) && cinergyt2->disconnect_pending) {
548 warn("delayed unregister in release");
549 cinergyt2_unregister(cinergyt2);
550 }
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return dvb_generic_release(inode, file);
553}
554
555static unsigned int cinergyt2_poll (struct file *file, struct poll_table_struct *wait)
556{
557 struct dvb_device *dvbdev = file->private_data;
558 struct cinergyt2 *cinergyt2 = dvbdev->priv;
Michael Krufkyb930e1d2007-08-27 18:16:54 -0300559 unsigned int mask = 0;
Deti Fliegle4acba32006-01-09 15:25:23 -0200560
Jiri Slaby475d5262007-10-15 13:07:30 -0300561 if (cinergyt2->disconnect_pending)
562 return -EAGAIN;
563 if (mutex_lock_interruptible(&cinergyt2->sem))
Deti Fliegle4acba32006-01-09 15:25:23 -0200564 return -ERESTARTSYS;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 poll_wait(file, &cinergyt2->poll_wq, wait);
Deti Fliegle4acba32006-01-09 15:25:23 -0200567
Michael Krufkyb930e1d2007-08-27 18:16:54 -0300568 if (cinergyt2->pending_fe_events != 0)
Michael Krufkyfd174c62006-06-12 13:40:37 -0300569 mask |= (POLLIN | POLLRDNORM | POLLPRI);
Dyks, Axel (XL)774dd5d2006-06-11 17:14:35 -0300570
Ingo Molnar3593cab2006-02-07 06:49:14 -0200571 mutex_unlock(&cinergyt2->sem);
Deti Fliegle4acba32006-01-09 15:25:23 -0200572
Michael Krufkyb930e1d2007-08-27 18:16:54 -0300573 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576
577static int cinergyt2_ioctl (struct inode *inode, struct file *file,
578 unsigned cmd, unsigned long arg)
579{
580 struct dvb_device *dvbdev = file->private_data;
581 struct cinergyt2 *cinergyt2 = dvbdev->priv;
582 struct dvbt_get_status_msg *stat = &cinergyt2->status;
583 fe_status_t status = 0;
584
585 switch (cmd) {
586 case FE_GET_INFO:
587 return copy_to_user((void __user*) arg, &cinergyt2_fe_info,
588 sizeof(struct dvb_frontend_info));
589
590 case FE_READ_STATUS:
591 if (0xffff - le16_to_cpu(stat->gain) > 30)
592 status |= FE_HAS_SIGNAL;
593 if (stat->lock_bits & (1 << 6))
594 status |= FE_HAS_LOCK;
595 if (stat->lock_bits & (1 << 5))
596 status |= FE_HAS_SYNC;
597 if (stat->lock_bits & (1 << 4))
598 status |= FE_HAS_CARRIER;
599 if (stat->lock_bits & (1 << 1))
600 status |= FE_HAS_VITERBI;
601
602 return copy_to_user((void __user*) arg, &status, sizeof(status));
603
604 case FE_READ_BER:
605 return put_user(le32_to_cpu(stat->viterbi_error_rate),
606 (__u32 __user *) arg);
607
608 case FE_READ_SIGNAL_STRENGTH:
609 return put_user(0xffff - le16_to_cpu(stat->gain),
610 (__u16 __user *) arg);
611
612 case FE_READ_SNR:
613 return put_user((stat->snr << 8) | stat->snr,
614 (__u16 __user *) arg);
615
616 case FE_READ_UNCORRECTED_BLOCKS:
Stephen Williams2c3f11b2006-01-09 15:25:21 -0200617 {
618 uint32_t unc_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Stephen Williams2c3f11b2006-01-09 15:25:21 -0200620 unc_count = stat->uncorrected_block_count;
621 stat->uncorrected_block_count = 0;
622
623 /* UNC are already converted to host byte order... */
624 return put_user(unc_count,(__u32 __user *) arg);
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 case FE_SET_FRONTEND:
627 {
628 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
629 struct dvb_frontend_parameters p;
630 int err;
631
632 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
633 return -EPERM;
634
635 if (copy_from_user(&p, (void __user*) arg, sizeof(p)))
636 return -EFAULT;
637
Jiri Slaby475d5262007-10-15 13:07:30 -0300638 if (cinergyt2->disconnect_pending)
639 return -EAGAIN;
640 if (mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return -ERESTARTSYS;
642
643 param->cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
644 param->tps = cpu_to_le16(compute_tps(&p));
645 param->freq = cpu_to_le32(p.frequency / 1000);
646 param->bandwidth = 8 - p.u.ofdm.bandwidth - BANDWIDTH_8_MHZ;
647
648 stat->lock_bits = 0;
649 cinergyt2->pending_fe_events++;
650 wake_up_interruptible(&cinergyt2->poll_wq);
651
652 err = cinergyt2_command(cinergyt2,
653 (char *) param, sizeof(*param),
654 NULL, 0);
655
Ingo Molnar3593cab2006-02-07 06:49:14 -0200656 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 return (err < 0) ? err : 0;
659 }
660
661 case FE_GET_FRONTEND:
662 /**
663 * trivial to implement (see struct dvbt_get_status_msg).
664 * equivalent to FE_READ ioctls, but needs
665 * TPS -> linux-dvb parameter set conversion. Feel free
666 * to implement this and send us a patch if you need this
667 * functionality.
668 */
669 break;
670
671 case FE_GET_EVENT:
672 {
673 /**
674 * for now we only fill the status field. the parameters
675 * are trivial to fill as soon FE_GET_FRONTEND is done.
676 */
677 struct dvb_frontend_event __user *e = (void __user *) arg;
678 if (cinergyt2->pending_fe_events == 0) {
679 if (file->f_flags & O_NONBLOCK)
680 return -EWOULDBLOCK;
681 wait_event_interruptible(cinergyt2->poll_wq,
682 cinergyt2->pending_fe_events > 0);
683 }
684 cinergyt2->pending_fe_events = 0;
685 return cinergyt2_ioctl(inode, file, FE_READ_STATUS,
686 (unsigned long) &e->status);
687 }
688
689 default:
690 ;
691 }
692
693 return -EINVAL;
694}
695
696static int cinergyt2_mmap(struct file *file, struct vm_area_struct *vma)
697{
698 struct dvb_device *dvbdev = file->private_data;
699 struct cinergyt2 *cinergyt2 = dvbdev->priv;
700 int ret = 0;
701
702 lock_kernel();
703
704 if (vma->vm_flags & (VM_WRITE | VM_EXEC)) {
705 ret = -EPERM;
706 goto bailout;
707 }
708
709 if (vma->vm_end > vma->vm_start + STREAM_URB_COUNT * STREAM_BUF_SIZE) {
710 ret = -EINVAL;
711 goto bailout;
712 }
713
714 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
715 vma->vm_file = file;
716
717 ret = remap_pfn_range(vma, vma->vm_start,
718 virt_to_phys(cinergyt2->streambuf) >> PAGE_SHIFT,
719 vma->vm_end - vma->vm_start,
720 vma->vm_page_prot) ? -EAGAIN : 0;
721bailout:
722 unlock_kernel();
723 return ret;
724}
725
726static struct file_operations cinergyt2_fops = {
727 .owner = THIS_MODULE,
728 .ioctl = cinergyt2_ioctl,
729 .poll = cinergyt2_poll,
730 .open = cinergyt2_open,
731 .release = cinergyt2_release,
732 .mmap = cinergyt2_mmap
733};
734
735static struct dvb_device cinergyt2_fe_template = {
736 .users = ~0,
737 .writers = 1,
738 .readers = (~0)-1,
739 .fops = &cinergyt2_fops
740};
741
742#ifdef ENABLE_RC
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500743
David Howellsc4028952006-11-22 14:57:56 +0000744static void cinergyt2_query_rc (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
David Howellsc4028952006-11-22 14:57:56 +0000746 struct cinergyt2 *cinergyt2 =
747 container_of(work, struct cinergyt2, rc_query_work.work);
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700748 char buf[1] = { CINERGYT2_EP1_GET_RC_EVENTS };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct cinergyt2_rc_event rc_events[12];
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700750 int n, len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Ingo Molnar3593cab2006-02-07 06:49:14 -0200752 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return;
754
755 len = cinergyt2_command(cinergyt2, buf, sizeof(buf),
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700756 (char *) rc_events, sizeof(rc_events));
757 if (len < 0)
758 goto out;
759 if (len == 0) {
760 if (time_after(jiffies, cinergyt2->last_event_jiffies +
761 msecs_to_jiffies(150))) {
762 /* stop key repeat */
763 if (cinergyt2->rc_input_event != KEY_MAX) {
764 dprintk(1, "rc_input_event=%d Up\n", cinergyt2->rc_input_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500765 input_report_key(cinergyt2->rc_input_dev,
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700766 cinergyt2->rc_input_event, 0);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300767 input_sync(cinergyt2->rc_input_dev);
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700768 cinergyt2->rc_input_event = KEY_MAX;
769 }
770 cinergyt2->rc_last_code = ~0;
771 }
772 goto out;
773 }
774 cinergyt2->last_event_jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700776 for (n = 0; n < (len / sizeof(rc_events[0])); n++) {
777 dprintk(1, "rc_events[%d].value = %x, type=%x\n",
778 n, le32_to_cpu(rc_events[n].value), rc_events[n].type);
Martin Loschwitz38b32d62005-07-07 17:57:31 -0700779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (rc_events[n].type == CINERGYT2_RC_EVENT_TYPE_NEC &&
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700781 rc_events[n].value == ~0) {
782 /* keyrepeat bit -> just repeat last rc_input_event */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 } else {
784 cinergyt2->rc_input_event = KEY_MAX;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500785 for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3) {
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700786 if (rc_keys[i + 0] == rc_events[n].type &&
787 rc_keys[i + 1] == le32_to_cpu(rc_events[n].value)) {
788 cinergyt2->rc_input_event = rc_keys[i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 break;
790 }
791 }
792 }
793
794 if (cinergyt2->rc_input_event != KEY_MAX) {
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700795 if (rc_events[n].value == cinergyt2->rc_last_code &&
796 cinergyt2->rc_last_code != ~0) {
797 /* emit a key-up so the double event is recognized */
798 dprintk(1, "rc_input_event=%d UP\n", cinergyt2->rc_input_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500799 input_report_key(cinergyt2->rc_input_dev,
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700800 cinergyt2->rc_input_event, 0);
801 }
802 dprintk(1, "rc_input_event=%d\n", cinergyt2->rc_input_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500803 input_report_key(cinergyt2->rc_input_dev,
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700804 cinergyt2->rc_input_event, 1);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300805 input_sync(cinergyt2->rc_input_dev);
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700806 cinergyt2->rc_last_code = rc_events[n].value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808 }
809
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700810out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 schedule_delayed_work(&cinergyt2->rc_query_work,
812 msecs_to_jiffies(RC_QUERY_INTERVAL));
813
Ingo Molnar3593cab2006-02-07 06:49:14 -0200814 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500816
817static int cinergyt2_register_rc(struct cinergyt2 *cinergyt2)
818{
819 struct input_dev *input_dev;
820 int i;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300821 int err;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500822
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300823 input_dev = input_allocate_device();
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500824 if (!input_dev)
825 return -ENOMEM;
826
827 usb_make_path(cinergyt2->udev, cinergyt2->phys, sizeof(cinergyt2->phys));
828 strlcat(cinergyt2->phys, "/input0", sizeof(cinergyt2->phys));
829 cinergyt2->rc_input_event = KEY_MAX;
830 cinergyt2->rc_last_code = ~0;
David Howellsc4028952006-11-22 14:57:56 +0000831 INIT_DELAYED_WORK(&cinergyt2->rc_query_work, cinergyt2_query_rc);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500832
833 input_dev->name = DRIVER_NAME " remote control";
834 input_dev->phys = cinergyt2->phys;
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700835 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
Dmitry Torokhovdb93a822005-11-20 11:13:29 -0500836 for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3)
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500837 set_bit(rc_keys[i + 2], input_dev->keybit);
838 input_dev->keycodesize = 0;
839 input_dev->keycodemax = 0;
Tino Keitelef7b8b72007-01-22 18:33:07 -0300840 input_dev->id.bustype = BUS_USB;
841 input_dev->id.vendor = cinergyt2->udev->descriptor.idVendor;
842 input_dev->id.product = cinergyt2->udev->descriptor.idProduct;
843 input_dev->id.version = 1;
Dmitry Torokhov2c8a3a32007-07-16 09:28:15 -0300844 input_dev->dev.parent = &cinergyt2->udev->dev;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500845
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300846 err = input_register_device(input_dev);
847 if (err) {
848 input_free_device(input_dev);
849 return err;
850 }
851
852 cinergyt2->rc_input_dev = input_dev;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500853 schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
David S. Miller7b5603e2005-11-16 00:11:50 -0800854
855 return 0;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500856}
857
858static void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2)
859{
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300860 cancel_rearming_delayed_work(&cinergyt2->rc_query_work);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500861 input_unregister_device(cinergyt2->rc_input_dev);
862}
863
864static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2)
865{
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300866 cancel_rearming_delayed_work(&cinergyt2->rc_query_work);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500867}
868
869static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2)
870{
871 schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
872}
873
874#else
875
876static inline int cinergyt2_register_rc(struct cinergyt2 *cinergyt2) { return 0; }
877static inline void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2) { }
878static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2) { }
879static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2) { }
880
881#endif /* ENABLE_RC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
David Howellsc4028952006-11-22 14:57:56 +0000883static void cinergyt2_query (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
David Howellsc4028952006-11-22 14:57:56 +0000885 struct cinergyt2 *cinergyt2 =
886 container_of(work, struct cinergyt2, query_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 char cmd [] = { CINERGYT2_EP1_GET_TUNER_STATUS };
888 struct dvbt_get_status_msg *s = &cinergyt2->status;
889 uint8_t lock_bits;
890 uint32_t unc;
891
Ingo Molnar3593cab2006-02-07 06:49:14 -0200892 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return;
894
895 unc = s->uncorrected_block_count;
896 lock_bits = s->lock_bits;
897
898 cinergyt2_command(cinergyt2, cmd, sizeof(cmd), (char *) s, sizeof(*s));
899
900 unc += le32_to_cpu(s->uncorrected_block_count);
901 s->uncorrected_block_count = unc;
902
903 if (lock_bits != s->lock_bits) {
904 wake_up_interruptible(&cinergyt2->poll_wq);
905 cinergyt2->pending_fe_events++;
906 }
907
908 schedule_delayed_work(&cinergyt2->query_work,
909 msecs_to_jiffies(QUERY_INTERVAL));
910
Ingo Molnar3593cab2006-02-07 06:49:14 -0200911 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914static int cinergyt2_probe (struct usb_interface *intf,
915 const struct usb_device_id *id)
916{
917 struct cinergyt2 *cinergyt2;
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700918 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700920 if (!(cinergyt2 = kzalloc (sizeof(struct cinergyt2), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 dprintk(1, "out of memory?!?\n");
922 return -ENOMEM;
923 }
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 usb_set_intfdata (intf, (void *) cinergyt2);
926
Ingo Molnar3593cab2006-02-07 06:49:14 -0200927 mutex_init(&cinergyt2->sem);
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300928 mutex_init(&cinergyt2->wq_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 init_waitqueue_head (&cinergyt2->poll_wq);
David Howellsc4028952006-11-22 14:57:56 +0000930 INIT_DELAYED_WORK(&cinergyt2->query_work, cinergyt2_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 cinergyt2->udev = interface_to_usbdev(intf);
933 cinergyt2->param.cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
934
935 if (cinergyt2_alloc_stream_urbs (cinergyt2) < 0) {
936 dprintk(1, "unable to allocate stream urbs\n");
937 kfree(cinergyt2);
938 return -ENOMEM;
939 }
940
Andrew de Quinceyd09dbf92006-04-10 09:27:37 -0300941 if ((err = dvb_register_adapter(&cinergyt2->adapter, DRIVER_NAME, THIS_MODULE, &cinergyt2->udev->dev)) < 0) {
Andrew de Quinceya064fad2006-04-06 17:05:46 -0300942 kfree(cinergyt2);
943 return err;
944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 cinergyt2->demux.priv = cinergyt2;
947 cinergyt2->demux.filternum = 256;
948 cinergyt2->demux.feednum = 256;
949 cinergyt2->demux.start_feed = cinergyt2_start_feed;
950 cinergyt2->demux.stop_feed = cinergyt2_stop_feed;
951 cinergyt2->demux.dmx.capabilities = DMX_TS_FILTERING |
952 DMX_SECTION_FILTERING |
953 DMX_MEMORY_BASED_FILTERING;
954
955 if ((err = dvb_dmx_init(&cinergyt2->demux)) < 0) {
956 dprintk(1, "dvb_dmx_init() failed (err = %d)\n", err);
957 goto bailout;
958 }
959
960 cinergyt2->dmxdev.filternum = cinergyt2->demux.filternum;
961 cinergyt2->dmxdev.demux = &cinergyt2->demux.dmx;
962 cinergyt2->dmxdev.capabilities = 0;
963
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700964 if ((err = dvb_dmxdev_init(&cinergyt2->dmxdev, &cinergyt2->adapter)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 dprintk(1, "dvb_dmxdev_init() failed (err = %d)\n", err);
966 goto bailout;
967 }
968
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700969 if (dvb_net_init(&cinergyt2->adapter, &cinergyt2->dvbnet, &cinergyt2->demux.dmx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 dprintk(1, "dvb_net_init() failed!\n");
971
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700972 dvb_register_device(&cinergyt2->adapter, &cinergyt2->fedev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 &cinergyt2_fe_template, cinergyt2,
974 DVB_DEVICE_FRONTEND);
975
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500976 err = cinergyt2_register_rc(cinergyt2);
977 if (err)
978 goto bailout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return 0;
981
982bailout:
Markus Rechberger77cc5312006-03-15 09:31:31 -0300983 dvb_net_release(&cinergyt2->dvbnet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 dvb_dmxdev_release(&cinergyt2->dmxdev);
985 dvb_dmx_release(&cinergyt2->demux);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500986 dvb_unregister_adapter(&cinergyt2->adapter);
987 cinergyt2_free_stream_urbs(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 kfree(cinergyt2);
989 return -ENOMEM;
990}
991
992static void cinergyt2_disconnect (struct usb_interface *intf)
993{
994 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
995
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500996 cinergyt2_unregister_rc(cinergyt2);
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300997 cancel_rearming_delayed_work(&cinergyt2->query_work);
Deti Fliegle4acba32006-01-09 15:25:23 -0200998 wake_up_interruptible(&cinergyt2->poll_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Deti Fliegle4acba32006-01-09 15:25:23 -02001000 cinergyt2->demux.dmx.close(&cinergyt2->demux.dmx);
1001 cinergyt2->disconnect_pending = 1;
1002
1003 if (!atomic_read(&cinergyt2->inuse))
1004 cinergyt2_unregister(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
Pavel Macheka2910682005-04-16 15:25:27 -07001007static int cinergyt2_suspend (struct usb_interface *intf, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
1010
Jiri Slaby475d5262007-10-15 13:07:30 -03001011 if (cinergyt2->disconnect_pending)
1012 return -EAGAIN;
1013 if (mutex_lock_interruptible(&cinergyt2->wq_sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return -ERESTARTSYS;
1015
Mauro Carvalho Chehab9b7cc422007-07-02 15:48:40 -03001016 cinergyt2_suspend_rc(cinergyt2);
1017 cancel_rearming_delayed_work(&cinergyt2->query_work);
Oleg Nesterov1e4597e2007-07-02 12:26:20 -03001018
Mauro Carvalho Chehab9b7cc422007-07-02 15:48:40 -03001019 mutex_lock(&cinergyt2->sem);
1020 if (cinergyt2->streaming)
1021 cinergyt2_stop_stream_xfer(cinergyt2);
1022 cinergyt2_sleep(cinergyt2, 1);
1023 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Oleg Nesterov1597f1f2007-08-09 12:02:30 -03001025 mutex_unlock(&cinergyt2->wq_sem);
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return 0;
1028}
1029
1030static int cinergyt2_resume (struct usb_interface *intf)
1031{
1032 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
1033 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
Jiri Slaby475d5262007-10-15 13:07:30 -03001034 int err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Jiri Slaby475d5262007-10-15 13:07:30 -03001036 if (cinergyt2->disconnect_pending)
1037 goto out;
1038 err = mutex_lock_interruptible(&cinergyt2->wq_sem);
1039 if (err)
Oleg Nesterov1e4597e2007-07-02 12:26:20 -03001040 goto out;
1041
Jiri Slaby475d5262007-10-15 13:07:30 -03001042 err = mutex_lock_interruptible(&cinergyt2->sem);
1043 if (err)
Oleg Nesterov1e4597e2007-07-02 12:26:20 -03001044 goto out_unlock1;
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (!cinergyt2->sleeping) {
1047 cinergyt2_sleep(cinergyt2, 0);
1048 cinergyt2_command(cinergyt2, (char *) param, sizeof(*param), NULL, 0);
1049 if (cinergyt2->streaming)
1050 cinergyt2_start_stream_xfer(cinergyt2);
1051 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
1052 }
1053
Dmitry Torokhovb7df3912005-09-15 02:01:53 -05001054 cinergyt2_resume_rc(cinergyt2);
1055
Ingo Molnar3593cab2006-02-07 06:49:14 -02001056 mutex_unlock(&cinergyt2->sem);
Oleg Nesterov1e4597e2007-07-02 12:26:20 -03001057out_unlock1:
1058 mutex_unlock(&cinergyt2->wq_sem);
1059out:
1060 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
1063static const struct usb_device_id cinergyt2_table [] __devinitdata = {
1064 { USB_DEVICE(0x0ccd, 0x0038) },
1065 { 0 }
1066};
1067
1068MODULE_DEVICE_TABLE(usb, cinergyt2_table);
1069
1070static struct usb_driver cinergyt2_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 .name = "cinergyT2",
1072 .probe = cinergyt2_probe,
1073 .disconnect = cinergyt2_disconnect,
1074 .suspend = cinergyt2_suspend,
1075 .resume = cinergyt2_resume,
1076 .id_table = cinergyt2_table
1077};
1078
1079static int __init cinergyt2_init (void)
1080{
1081 int err;
1082
1083 if ((err = usb_register(&cinergyt2_driver)) < 0)
1084 dprintk(1, "usb_register() failed! (err %i)\n", err);
1085
1086 return err;
1087}
1088
1089static void __exit cinergyt2_exit (void)
1090{
1091 usb_deregister(&cinergyt2_driver);
1092}
1093
1094module_init (cinergyt2_init);
1095module_exit (cinergyt2_exit);
1096
1097MODULE_LICENSE("GPL");
1098MODULE_AUTHOR("Holger Waechtler, Daniel Mack");