blob: 28929b618e20bb07cfb4f271b5a9da9c207ebcda [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>
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
Ingo Molnar3593cab2006-02-07 06:49:14 -0200348 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return -ERESTARTSYS;
350
351 if (cinergyt2->streaming == 0)
352 cinergyt2_start_stream_xfer(cinergyt2);
353
354 cinergyt2->streaming++;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200355 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357}
358
359static int cinergyt2_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
360{
361 struct dvb_demux *demux = dvbdmxfeed->demux;
362 struct cinergyt2 *cinergyt2 = demux->priv;
363
Ingo Molnar3593cab2006-02-07 06:49:14 -0200364 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return -ERESTARTSYS;
366
367 if (--cinergyt2->streaming == 0)
368 cinergyt2_stop_stream_xfer(cinergyt2);
369
Ingo Molnar3593cab2006-02-07 06:49:14 -0200370 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
372}
373
374/**
375 * convert linux-dvb frontend parameter set into TPS.
376 * See ETSI ETS-300744, section 4.6.2, table 9 for details.
377 *
378 * This function is probably reusable and may better get placed in a support
379 * library.
380 *
381 * We replace errornous fields by default TPS fields (the ones with value 0).
382 */
383static uint16_t compute_tps (struct dvb_frontend_parameters *p)
384{
385 struct dvb_ofdm_parameters *op = &p->u.ofdm;
386 uint16_t tps = 0;
387
388 switch (op->code_rate_HP) {
389 case FEC_2_3:
390 tps |= (1 << 7);
391 break;
392 case FEC_3_4:
393 tps |= (2 << 7);
394 break;
395 case FEC_5_6:
396 tps |= (3 << 7);
397 break;
398 case FEC_7_8:
399 tps |= (4 << 7);
400 break;
401 case FEC_1_2:
402 case FEC_AUTO:
403 default:
404 /* tps |= (0 << 7) */;
405 }
406
407 switch (op->code_rate_LP) {
408 case FEC_2_3:
409 tps |= (1 << 4);
410 break;
411 case FEC_3_4:
412 tps |= (2 << 4);
413 break;
414 case FEC_5_6:
415 tps |= (3 << 4);
416 break;
417 case FEC_7_8:
418 tps |= (4 << 4);
419 break;
420 case FEC_1_2:
421 case FEC_AUTO:
422 default:
423 /* tps |= (0 << 4) */;
424 }
425
426 switch (op->constellation) {
427 case QAM_16:
428 tps |= (1 << 13);
429 break;
430 case QAM_64:
431 tps |= (2 << 13);
432 break;
433 case QPSK:
434 default:
435 /* tps |= (0 << 13) */;
436 }
437
438 switch (op->transmission_mode) {
439 case TRANSMISSION_MODE_8K:
440 tps |= (1 << 0);
441 break;
442 case TRANSMISSION_MODE_2K:
443 default:
444 /* tps |= (0 << 0) */;
445 }
446
447 switch (op->guard_interval) {
448 case GUARD_INTERVAL_1_16:
449 tps |= (1 << 2);
450 break;
451 case GUARD_INTERVAL_1_8:
452 tps |= (2 << 2);
453 break;
454 case GUARD_INTERVAL_1_4:
455 tps |= (3 << 2);
456 break;
457 case GUARD_INTERVAL_1_32:
458 default:
459 /* tps |= (0 << 2) */;
460 }
461
462 switch (op->hierarchy_information) {
463 case HIERARCHY_1:
464 tps |= (1 << 10);
465 break;
466 case HIERARCHY_2:
467 tps |= (2 << 10);
468 break;
469 case HIERARCHY_4:
470 tps |= (3 << 10);
471 break;
472 case HIERARCHY_NONE:
473 default:
474 /* tps |= (0 << 10) */;
475 }
476
477 return tps;
478}
479
480static int cinergyt2_open (struct inode *inode, struct file *file)
481{
482 struct dvb_device *dvbdev = file->private_data;
483 struct cinergyt2 *cinergyt2 = dvbdev->priv;
Deti Fliegle4acba32006-01-09 15:25:23 -0200484 int err = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300486 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->wq_sem))
487 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300489 if (mutex_lock_interruptible(&cinergyt2->sem))
490 goto out_unlock1;
Deti Fliegle4acba32006-01-09 15:25:23 -0200491
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300492 if ((err = dvb_generic_open(inode, file)))
493 goto out_unlock2;
Deti Fliegle4acba32006-01-09 15:25:23 -0200494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
496 cinergyt2_sleep(cinergyt2, 0);
497 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
498 }
499
Deti Fliegle4acba32006-01-09 15:25:23 -0200500 atomic_inc(&cinergyt2->inuse);
501
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300502out_unlock2:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200503 mutex_unlock(&cinergyt2->sem);
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300504out_unlock1:
505 mutex_unlock(&cinergyt2->wq_sem);
506out:
507 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Deti Fliegle4acba32006-01-09 15:25:23 -0200510static void cinergyt2_unregister(struct cinergyt2 *cinergyt2)
511{
Markus Rechberger77cc5312006-03-15 09:31:31 -0300512 dvb_net_release(&cinergyt2->dvbnet);
513 dvb_dmxdev_release(&cinergyt2->dmxdev);
514 dvb_dmx_release(&cinergyt2->demux);
Deti Fliegle4acba32006-01-09 15:25:23 -0200515 dvb_unregister_device(cinergyt2->fedev);
516 dvb_unregister_adapter(&cinergyt2->adapter);
517
518 cinergyt2_free_stream_urbs(cinergyt2);
519 kfree(cinergyt2);
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522static int cinergyt2_release (struct inode *inode, struct file *file)
523{
524 struct dvb_device *dvbdev = file->private_data;
525 struct cinergyt2 *cinergyt2 = dvbdev->priv;
526
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300527 mutex_lock(&cinergyt2->wq_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Deti Fliegle4acba32006-01-09 15:25:23 -0200529 if (!cinergyt2->disconnect_pending && (file->f_flags & O_ACCMODE) != O_RDONLY) {
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300530 cancel_rearming_delayed_work(&cinergyt2->query_work);
531
532 mutex_lock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 cinergyt2_sleep(cinergyt2, 1);
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300534 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300537 mutex_unlock(&cinergyt2->wq_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Deti Fliegle4acba32006-01-09 15:25:23 -0200539 if (atomic_dec_and_test(&cinergyt2->inuse) && cinergyt2->disconnect_pending) {
540 warn("delayed unregister in release");
541 cinergyt2_unregister(cinergyt2);
542 }
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return dvb_generic_release(inode, file);
545}
546
547static unsigned int cinergyt2_poll (struct file *file, struct poll_table_struct *wait)
548{
549 struct dvb_device *dvbdev = file->private_data;
550 struct cinergyt2 *cinergyt2 = dvbdev->priv;
Dyks, Axel (XL)774dd5d2006-06-11 17:14:35 -0300551 unsigned int mask = 0;
Deti Fliegle4acba32006-01-09 15:25:23 -0200552
Ingo Molnar3593cab2006-02-07 06:49:14 -0200553 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Deti Fliegle4acba32006-01-09 15:25:23 -0200554 return -ERESTARTSYS;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 poll_wait(file, &cinergyt2->poll_wq, wait);
Deti Fliegle4acba32006-01-09 15:25:23 -0200557
Dyks, Axel (XL)774dd5d2006-06-11 17:14:35 -0300558 if (cinergyt2->pending_fe_events != 0)
Michael Krufkyfd174c62006-06-12 13:40:37 -0300559 mask |= (POLLIN | POLLRDNORM | POLLPRI);
Dyks, Axel (XL)774dd5d2006-06-11 17:14:35 -0300560
Ingo Molnar3593cab2006-02-07 06:49:14 -0200561 mutex_unlock(&cinergyt2->sem);
Deti Fliegle4acba32006-01-09 15:25:23 -0200562
Dyks, Axel (XL)774dd5d2006-06-11 17:14:35 -0300563 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566
567static int cinergyt2_ioctl (struct inode *inode, struct file *file,
568 unsigned cmd, unsigned long arg)
569{
570 struct dvb_device *dvbdev = file->private_data;
571 struct cinergyt2 *cinergyt2 = dvbdev->priv;
572 struct dvbt_get_status_msg *stat = &cinergyt2->status;
573 fe_status_t status = 0;
574
575 switch (cmd) {
576 case FE_GET_INFO:
577 return copy_to_user((void __user*) arg, &cinergyt2_fe_info,
578 sizeof(struct dvb_frontend_info));
579
580 case FE_READ_STATUS:
581 if (0xffff - le16_to_cpu(stat->gain) > 30)
582 status |= FE_HAS_SIGNAL;
583 if (stat->lock_bits & (1 << 6))
584 status |= FE_HAS_LOCK;
585 if (stat->lock_bits & (1 << 5))
586 status |= FE_HAS_SYNC;
587 if (stat->lock_bits & (1 << 4))
588 status |= FE_HAS_CARRIER;
589 if (stat->lock_bits & (1 << 1))
590 status |= FE_HAS_VITERBI;
591
592 return copy_to_user((void __user*) arg, &status, sizeof(status));
593
594 case FE_READ_BER:
595 return put_user(le32_to_cpu(stat->viterbi_error_rate),
596 (__u32 __user *) arg);
597
598 case FE_READ_SIGNAL_STRENGTH:
599 return put_user(0xffff - le16_to_cpu(stat->gain),
600 (__u16 __user *) arg);
601
602 case FE_READ_SNR:
603 return put_user((stat->snr << 8) | stat->snr,
604 (__u16 __user *) arg);
605
606 case FE_READ_UNCORRECTED_BLOCKS:
Stephen Williams2c3f11b2006-01-09 15:25:21 -0200607 {
608 uint32_t unc_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Stephen Williams2c3f11b2006-01-09 15:25:21 -0200610 unc_count = stat->uncorrected_block_count;
611 stat->uncorrected_block_count = 0;
612
613 /* UNC are already converted to host byte order... */
614 return put_user(unc_count,(__u32 __user *) arg);
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 case FE_SET_FRONTEND:
617 {
618 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
619 struct dvb_frontend_parameters p;
620 int err;
621
622 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
623 return -EPERM;
624
625 if (copy_from_user(&p, (void __user*) arg, sizeof(p)))
626 return -EFAULT;
627
Ingo Molnar3593cab2006-02-07 06:49:14 -0200628 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -ERESTARTSYS;
630
631 param->cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
632 param->tps = cpu_to_le16(compute_tps(&p));
633 param->freq = cpu_to_le32(p.frequency / 1000);
634 param->bandwidth = 8 - p.u.ofdm.bandwidth - BANDWIDTH_8_MHZ;
635
636 stat->lock_bits = 0;
637 cinergyt2->pending_fe_events++;
638 wake_up_interruptible(&cinergyt2->poll_wq);
639
640 err = cinergyt2_command(cinergyt2,
641 (char *) param, sizeof(*param),
642 NULL, 0);
643
Ingo Molnar3593cab2006-02-07 06:49:14 -0200644 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 return (err < 0) ? err : 0;
647 }
648
649 case FE_GET_FRONTEND:
650 /**
651 * trivial to implement (see struct dvbt_get_status_msg).
652 * equivalent to FE_READ ioctls, but needs
653 * TPS -> linux-dvb parameter set conversion. Feel free
654 * to implement this and send us a patch if you need this
655 * functionality.
656 */
657 break;
658
659 case FE_GET_EVENT:
660 {
661 /**
662 * for now we only fill the status field. the parameters
663 * are trivial to fill as soon FE_GET_FRONTEND is done.
664 */
665 struct dvb_frontend_event __user *e = (void __user *) arg;
666 if (cinergyt2->pending_fe_events == 0) {
667 if (file->f_flags & O_NONBLOCK)
668 return -EWOULDBLOCK;
669 wait_event_interruptible(cinergyt2->poll_wq,
670 cinergyt2->pending_fe_events > 0);
671 }
672 cinergyt2->pending_fe_events = 0;
673 return cinergyt2_ioctl(inode, file, FE_READ_STATUS,
674 (unsigned long) &e->status);
675 }
676
677 default:
678 ;
679 }
680
681 return -EINVAL;
682}
683
684static int cinergyt2_mmap(struct file *file, struct vm_area_struct *vma)
685{
686 struct dvb_device *dvbdev = file->private_data;
687 struct cinergyt2 *cinergyt2 = dvbdev->priv;
688 int ret = 0;
689
690 lock_kernel();
691
692 if (vma->vm_flags & (VM_WRITE | VM_EXEC)) {
693 ret = -EPERM;
694 goto bailout;
695 }
696
697 if (vma->vm_end > vma->vm_start + STREAM_URB_COUNT * STREAM_BUF_SIZE) {
698 ret = -EINVAL;
699 goto bailout;
700 }
701
702 vma->vm_flags |= (VM_IO | VM_DONTCOPY);
703 vma->vm_file = file;
704
705 ret = remap_pfn_range(vma, vma->vm_start,
706 virt_to_phys(cinergyt2->streambuf) >> PAGE_SHIFT,
707 vma->vm_end - vma->vm_start,
708 vma->vm_page_prot) ? -EAGAIN : 0;
709bailout:
710 unlock_kernel();
711 return ret;
712}
713
714static struct file_operations cinergyt2_fops = {
715 .owner = THIS_MODULE,
716 .ioctl = cinergyt2_ioctl,
717 .poll = cinergyt2_poll,
718 .open = cinergyt2_open,
719 .release = cinergyt2_release,
720 .mmap = cinergyt2_mmap
721};
722
723static struct dvb_device cinergyt2_fe_template = {
724 .users = ~0,
725 .writers = 1,
726 .readers = (~0)-1,
727 .fops = &cinergyt2_fops
728};
729
730#ifdef ENABLE_RC
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500731
David Howellsc4028952006-11-22 14:57:56 +0000732static void cinergyt2_query_rc (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
David Howellsc4028952006-11-22 14:57:56 +0000734 struct cinergyt2 *cinergyt2 =
735 container_of(work, struct cinergyt2, rc_query_work.work);
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700736 char buf[1] = { CINERGYT2_EP1_GET_RC_EVENTS };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 struct cinergyt2_rc_event rc_events[12];
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700738 int n, len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Ingo Molnar3593cab2006-02-07 06:49:14 -0200740 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return;
742
743 len = cinergyt2_command(cinergyt2, buf, sizeof(buf),
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700744 (char *) rc_events, sizeof(rc_events));
745 if (len < 0)
746 goto out;
747 if (len == 0) {
748 if (time_after(jiffies, cinergyt2->last_event_jiffies +
749 msecs_to_jiffies(150))) {
750 /* stop key repeat */
751 if (cinergyt2->rc_input_event != KEY_MAX) {
752 dprintk(1, "rc_input_event=%d Up\n", cinergyt2->rc_input_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500753 input_report_key(cinergyt2->rc_input_dev,
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700754 cinergyt2->rc_input_event, 0);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300755 input_sync(cinergyt2->rc_input_dev);
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700756 cinergyt2->rc_input_event = KEY_MAX;
757 }
758 cinergyt2->rc_last_code = ~0;
759 }
760 goto out;
761 }
762 cinergyt2->last_event_jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700764 for (n = 0; n < (len / sizeof(rc_events[0])); n++) {
765 dprintk(1, "rc_events[%d].value = %x, type=%x\n",
766 n, le32_to_cpu(rc_events[n].value), rc_events[n].type);
Martin Loschwitz38b32d62005-07-07 17:57:31 -0700767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (rc_events[n].type == CINERGYT2_RC_EVENT_TYPE_NEC &&
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700769 rc_events[n].value == ~0) {
770 /* keyrepeat bit -> just repeat last rc_input_event */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 } else {
772 cinergyt2->rc_input_event = KEY_MAX;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500773 for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3) {
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700774 if (rc_keys[i + 0] == rc_events[n].type &&
775 rc_keys[i + 1] == le32_to_cpu(rc_events[n].value)) {
776 cinergyt2->rc_input_event = rc_keys[i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 break;
778 }
779 }
780 }
781
782 if (cinergyt2->rc_input_event != KEY_MAX) {
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700783 if (rc_events[n].value == cinergyt2->rc_last_code &&
784 cinergyt2->rc_last_code != ~0) {
785 /* emit a key-up so the double event is recognized */
786 dprintk(1, "rc_input_event=%d UP\n", cinergyt2->rc_input_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500787 input_report_key(cinergyt2->rc_input_dev,
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700788 cinergyt2->rc_input_event, 0);
789 }
790 dprintk(1, "rc_input_event=%d\n", cinergyt2->rc_input_event);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500791 input_report_key(cinergyt2->rc_input_dev,
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700792 cinergyt2->rc_input_event, 1);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300793 input_sync(cinergyt2->rc_input_dev);
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700794 cinergyt2->rc_last_code = rc_events[n].value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796 }
797
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700798out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 schedule_delayed_work(&cinergyt2->rc_query_work,
800 msecs_to_jiffies(RC_QUERY_INTERVAL));
801
Ingo Molnar3593cab2006-02-07 06:49:14 -0200802 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500804
805static int cinergyt2_register_rc(struct cinergyt2 *cinergyt2)
806{
807 struct input_dev *input_dev;
808 int i;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300809 int err;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500810
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300811 input_dev = input_allocate_device();
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500812 if (!input_dev)
813 return -ENOMEM;
814
815 usb_make_path(cinergyt2->udev, cinergyt2->phys, sizeof(cinergyt2->phys));
816 strlcat(cinergyt2->phys, "/input0", sizeof(cinergyt2->phys));
817 cinergyt2->rc_input_event = KEY_MAX;
818 cinergyt2->rc_last_code = ~0;
David Howellsc4028952006-11-22 14:57:56 +0000819 INIT_DELAYED_WORK(&cinergyt2->rc_query_work, cinergyt2_query_rc);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500820
821 input_dev->name = DRIVER_NAME " remote control";
822 input_dev->phys = cinergyt2->phys;
823 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
Dmitry Torokhovdb93a822005-11-20 11:13:29 -0500824 for (i = 0; i < ARRAY_SIZE(rc_keys); i += 3)
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500825 set_bit(rc_keys[i + 2], input_dev->keybit);
826 input_dev->keycodesize = 0;
827 input_dev->keycodemax = 0;
Tino Keitelef7b8b72007-01-22 18:33:07 -0300828 input_dev->id.bustype = BUS_USB;
829 input_dev->id.vendor = cinergyt2->udev->descriptor.idVendor;
830 input_dev->id.product = cinergyt2->udev->descriptor.idProduct;
831 input_dev->id.version = 1;
Dmitry Torokhov2c8a3a32007-07-16 09:28:15 -0300832 input_dev->dev.parent = &cinergyt2->udev->dev;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500833
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300834 err = input_register_device(input_dev);
835 if (err) {
836 input_free_device(input_dev);
837 return err;
838 }
839
840 cinergyt2->rc_input_dev = input_dev;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500841 schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
David S. Miller7b5603e2005-11-16 00:11:50 -0800842
843 return 0;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500844}
845
846static void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2)
847{
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300848 cancel_rearming_delayed_work(&cinergyt2->rc_query_work);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500849 input_unregister_device(cinergyt2->rc_input_dev);
850}
851
852static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2)
853{
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300854 cancel_rearming_delayed_work(&cinergyt2->rc_query_work);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500855}
856
857static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2)
858{
859 schedule_delayed_work(&cinergyt2->rc_query_work, HZ/2);
860}
861
862#else
863
864static inline int cinergyt2_register_rc(struct cinergyt2 *cinergyt2) { return 0; }
865static inline void cinergyt2_unregister_rc(struct cinergyt2 *cinergyt2) { }
866static inline void cinergyt2_suspend_rc(struct cinergyt2 *cinergyt2) { }
867static inline void cinergyt2_resume_rc(struct cinergyt2 *cinergyt2) { }
868
869#endif /* ENABLE_RC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
David Howellsc4028952006-11-22 14:57:56 +0000871static void cinergyt2_query (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
David Howellsc4028952006-11-22 14:57:56 +0000873 struct cinergyt2 *cinergyt2 =
874 container_of(work, struct cinergyt2, query_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 char cmd [] = { CINERGYT2_EP1_GET_TUNER_STATUS };
876 struct dvbt_get_status_msg *s = &cinergyt2->status;
877 uint8_t lock_bits;
878 uint32_t unc;
879
Ingo Molnar3593cab2006-02-07 06:49:14 -0200880 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return;
882
883 unc = s->uncorrected_block_count;
884 lock_bits = s->lock_bits;
885
886 cinergyt2_command(cinergyt2, cmd, sizeof(cmd), (char *) s, sizeof(*s));
887
888 unc += le32_to_cpu(s->uncorrected_block_count);
889 s->uncorrected_block_count = unc;
890
891 if (lock_bits != s->lock_bits) {
892 wake_up_interruptible(&cinergyt2->poll_wq);
893 cinergyt2->pending_fe_events++;
894 }
895
896 schedule_delayed_work(&cinergyt2->query_work,
897 msecs_to_jiffies(QUERY_INTERVAL));
898
Ingo Molnar3593cab2006-02-07 06:49:14 -0200899 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902static int cinergyt2_probe (struct usb_interface *intf,
903 const struct usb_device_id *id)
904{
905 struct cinergyt2 *cinergyt2;
Johannes Stezenbach6d789332005-09-09 13:03:05 -0700906 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700908 if (!(cinergyt2 = kzalloc (sizeof(struct cinergyt2), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 dprintk(1, "out of memory?!?\n");
910 return -ENOMEM;
911 }
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 usb_set_intfdata (intf, (void *) cinergyt2);
914
Ingo Molnar3593cab2006-02-07 06:49:14 -0200915 mutex_init(&cinergyt2->sem);
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300916 mutex_init(&cinergyt2->wq_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 init_waitqueue_head (&cinergyt2->poll_wq);
David Howellsc4028952006-11-22 14:57:56 +0000918 INIT_DELAYED_WORK(&cinergyt2->query_work, cinergyt2_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 cinergyt2->udev = interface_to_usbdev(intf);
921 cinergyt2->param.cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
922
923 if (cinergyt2_alloc_stream_urbs (cinergyt2) < 0) {
924 dprintk(1, "unable to allocate stream urbs\n");
925 kfree(cinergyt2);
926 return -ENOMEM;
927 }
928
Andrew de Quinceyd09dbf92006-04-10 09:27:37 -0300929 if ((err = dvb_register_adapter(&cinergyt2->adapter, DRIVER_NAME, THIS_MODULE, &cinergyt2->udev->dev)) < 0) {
Andrew de Quinceya064fad2006-04-06 17:05:46 -0300930 kfree(cinergyt2);
931 return err;
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 cinergyt2->demux.priv = cinergyt2;
935 cinergyt2->demux.filternum = 256;
936 cinergyt2->demux.feednum = 256;
937 cinergyt2->demux.start_feed = cinergyt2_start_feed;
938 cinergyt2->demux.stop_feed = cinergyt2_stop_feed;
939 cinergyt2->demux.dmx.capabilities = DMX_TS_FILTERING |
940 DMX_SECTION_FILTERING |
941 DMX_MEMORY_BASED_FILTERING;
942
943 if ((err = dvb_dmx_init(&cinergyt2->demux)) < 0) {
944 dprintk(1, "dvb_dmx_init() failed (err = %d)\n", err);
945 goto bailout;
946 }
947
948 cinergyt2->dmxdev.filternum = cinergyt2->demux.filternum;
949 cinergyt2->dmxdev.demux = &cinergyt2->demux.dmx;
950 cinergyt2->dmxdev.capabilities = 0;
951
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700952 if ((err = dvb_dmxdev_init(&cinergyt2->dmxdev, &cinergyt2->adapter)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 dprintk(1, "dvb_dmxdev_init() failed (err = %d)\n", err);
954 goto bailout;
955 }
956
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700957 if (dvb_net_init(&cinergyt2->adapter, &cinergyt2->dvbnet, &cinergyt2->demux.dmx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 dprintk(1, "dvb_net_init() failed!\n");
959
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700960 dvb_register_device(&cinergyt2->adapter, &cinergyt2->fedev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 &cinergyt2_fe_template, cinergyt2,
962 DVB_DEVICE_FRONTEND);
963
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500964 err = cinergyt2_register_rc(cinergyt2);
965 if (err)
966 goto bailout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return 0;
969
970bailout:
Markus Rechberger77cc5312006-03-15 09:31:31 -0300971 dvb_net_release(&cinergyt2->dvbnet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 dvb_dmxdev_release(&cinergyt2->dmxdev);
973 dvb_dmx_release(&cinergyt2->demux);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500974 dvb_unregister_adapter(&cinergyt2->adapter);
975 cinergyt2_free_stream_urbs(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 kfree(cinergyt2);
977 return -ENOMEM;
978}
979
980static void cinergyt2_disconnect (struct usb_interface *intf)
981{
982 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
983
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500984 cinergyt2_unregister_rc(cinergyt2);
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300985 cancel_rearming_delayed_work(&cinergyt2->query_work);
Deti Fliegle4acba32006-01-09 15:25:23 -0200986 wake_up_interruptible(&cinergyt2->poll_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Deti Fliegle4acba32006-01-09 15:25:23 -0200988 cinergyt2->demux.dmx.close(&cinergyt2->demux.dmx);
989 cinergyt2->disconnect_pending = 1;
990
991 if (!atomic_read(&cinergyt2->inuse))
992 cinergyt2_unregister(cinergyt2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Pavel Macheka2910682005-04-16 15:25:27 -0700995static int cinergyt2_suspend (struct usb_interface *intf, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
998
Oleg Nesterov1e4597e2007-07-02 12:26:20 -0300999 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->wq_sem))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return -ERESTARTSYS;
1001
Mauro Carvalho Chehab9b7cc422007-07-02 15:48:40 -03001002 cinergyt2_suspend_rc(cinergyt2);
1003 cancel_rearming_delayed_work(&cinergyt2->query_work);
Oleg Nesterov1e4597e2007-07-02 12:26:20 -03001004
Mauro Carvalho Chehab9b7cc422007-07-02 15:48:40 -03001005 mutex_lock(&cinergyt2->sem);
1006 if (cinergyt2->streaming)
1007 cinergyt2_stop_stream_xfer(cinergyt2);
1008 cinergyt2_sleep(cinergyt2, 1);
1009 mutex_unlock(&cinergyt2->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return 0;
1012}
1013
1014static int cinergyt2_resume (struct usb_interface *intf)
1015{
1016 struct cinergyt2 *cinergyt2 = usb_get_intfdata (intf);
1017 struct dvbt_set_parameters_msg *param = &cinergyt2->param;
Oleg Nesterov1e4597e2007-07-02 12:26:20 -03001018 int err = -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Oleg Nesterov1e4597e2007-07-02 12:26:20 -03001020 if (cinergyt2->disconnect_pending || mutex_lock_interruptible(&cinergyt2->wq_sem))
1021 goto out;
1022
1023 if (mutex_lock_interruptible(&cinergyt2->sem))
1024 goto out_unlock1;
1025
1026 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 if (!cinergyt2->sleeping) {
1029 cinergyt2_sleep(cinergyt2, 0);
1030 cinergyt2_command(cinergyt2, (char *) param, sizeof(*param), NULL, 0);
1031 if (cinergyt2->streaming)
1032 cinergyt2_start_stream_xfer(cinergyt2);
1033 schedule_delayed_work(&cinergyt2->query_work, HZ/2);
1034 }
1035
Dmitry Torokhovb7df3912005-09-15 02:01:53 -05001036 cinergyt2_resume_rc(cinergyt2);
1037
Ingo Molnar3593cab2006-02-07 06:49:14 -02001038 mutex_unlock(&cinergyt2->sem);
Oleg Nesterov1e4597e2007-07-02 12:26:20 -03001039out_unlock1:
1040 mutex_unlock(&cinergyt2->wq_sem);
1041out:
1042 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045static const struct usb_device_id cinergyt2_table [] __devinitdata = {
1046 { USB_DEVICE(0x0ccd, 0x0038) },
1047 { 0 }
1048};
1049
1050MODULE_DEVICE_TABLE(usb, cinergyt2_table);
1051
1052static struct usb_driver cinergyt2_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 .name = "cinergyT2",
1054 .probe = cinergyt2_probe,
1055 .disconnect = cinergyt2_disconnect,
1056 .suspend = cinergyt2_suspend,
1057 .resume = cinergyt2_resume,
1058 .id_table = cinergyt2_table
1059};
1060
1061static int __init cinergyt2_init (void)
1062{
1063 int err;
1064
1065 if ((err = usb_register(&cinergyt2_driver)) < 0)
1066 dprintk(1, "usb_register() failed! (err %i)\n", err);
1067
1068 return err;
1069}
1070
1071static void __exit cinergyt2_exit (void)
1072{
1073 usb_deregister(&cinergyt2_driver);
1074}
1075
1076module_init (cinergyt2_init);
1077module_exit (cinergyt2_exit);
1078
1079MODULE_LICENSE("GPL");
1080MODULE_AUTHOR("Holger Waechtler, Daniel Mack");