1 /* DVB USB framework compliant Linux driver for the Opera1 DVB-S Card
3 * Copyright (C) 2006 Mario Hlawitschka (dh1pa@amsat.org)
4 * Copyright (C) 2006 Marco Gittler (g.marco@freenet.de)
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation, version 2.
10 * see Documentation/dvb/README.dvb-usb for more information
16 #define OPERA_READ_MSG 0
17 #define OPERA_WRITE_MSG 1
18 #define OPERA_I2C_TUNER 0xd1
20 #define READ_FX2_REG_REQ 0xba
21 #define READ_MAC_ADDR 0x08
22 #define OPERA_WRITE_FX2 0xbb
23 #define OPERA_TUNER_REQ 0xb1
24 #define REG_1F_SYMBOLRATE_BYTE0 0x1f
25 #define REG_20_SYMBOLRATE_BYTE1 0x20
26 #define REG_21_SYMBOLRATE_BYTE2 0x21
28 #define ADDR_B600_VOLTAGE_13V (0x02)
29 #define ADDR_B601_VOLTAGE_18V (0x03)
30 #define ADDR_B1A6_STREAM_CTRL (0x04)
31 #define ADDR_B880_READ_REMOTE (0x05)
36 struct opera_rc_keys
{
41 int dvb_usb_opera1_debug
;
42 module_param_named(debug
, dvb_usb_opera1_debug
, int, 0644);
43 MODULE_PARM_DESC(debug
,
44 "set debugging level (1=info,xfer=2,pll=4,ts=8,err=16,rc=32,fw=64 (or-able))."
45 DVB_USB_DEBUG_STATUS
);
47 static int opera1_xilinx_rw(struct usb_device
*dev
, u8 request
, u16 value
,
48 u8
* data
, u16 len
, int flags
)
54 unsigned int pipe
= (flags
== OPERA_READ_MSG
) ?
55 usb_rcvctrlpipe(dev
,0) : usb_sndctrlpipe(dev
, 0);
56 u8 request_type
= (flags
== OPERA_READ_MSG
) ? USB_DIR_IN
: USB_DIR_OUT
;
58 if (flags
== OPERA_WRITE_MSG
)
59 memcpy(u8buf
, data
, len
);
61 usb_control_msg(dev
, pipe
, request
, request_type
| USB_TYPE_VENDOR
,
62 value
, 0x0, u8buf
, len
, 2000);
64 if (request
== OPERA_TUNER_REQ
) {
65 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
66 OPERA_TUNER_REQ
, USB_DIR_IN
| USB_TYPE_VENDOR
,
67 0x01, 0x0, &r
, 1, 2000)<1 || r
!=0x08)
70 if (flags
== OPERA_READ_MSG
)
71 memcpy(data
, u8buf
, len
);
77 static int opera1_usb_i2c_msgxfer(struct dvb_usb_device
*dev
, u16 addr
,
85 info("no usb_device");
88 if (mutex_lock_interruptible(&dev
->usb_mutex
) < 0)
92 case ADDR_B600_VOLTAGE_13V
:
96 case ADDR_B601_VOLTAGE_18V
:
100 case ADDR_B1A6_STREAM_CTRL
:
104 case ADDR_B880_READ_REMOTE
:
112 ret
= opera1_xilinx_rw(dev
->udev
, request
,
114 addr
&0x01?OPERA_READ_MSG
:OPERA_WRITE_MSG
);
116 mutex_unlock(&dev
->usb_mutex
);
120 static int opera1_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg msg
[],
123 struct dvb_usb_device
*d
= i2c_get_adapdata(adap
);
128 if (mutex_lock_interruptible(&d
->i2c_mutex
) < 0)
131 for (i
= 0; i
< num
; i
++) {
132 if ((tmp
= opera1_usb_i2c_msgxfer(d
,
133 (msg
[i
].addr
<<1)|(msg
[i
].flags
&I2C_M_RD
?0x01:0),
139 if (dvb_usb_opera1_debug
& 0x10)
140 info("sending i2c mesage %d %d", tmp
, msg
[i
].len
);
142 mutex_unlock(&d
->i2c_mutex
);
146 static u32
opera1_i2c_func(struct i2c_adapter
*adapter
)
151 static struct i2c_algorithm opera1_i2c_algo
= {
152 .master_xfer
= opera1_i2c_xfer
,
153 .functionality
= opera1_i2c_func
,
156 static int opera1_set_voltage(struct dvb_frontend
*fe
, fe_sec_voltage_t voltage
)
158 static u8 command_13v
[1]={0x00};
159 static u8 command_18v
[1]={0x01};
160 struct i2c_msg msg
[] = {
161 {.addr
= ADDR_B600_VOLTAGE_13V
,.flags
= 0,.buf
= command_13v
,.len
= 1},
163 struct dvb_usb_adapter
*udev_adap
=
164 (struct dvb_usb_adapter
*)(fe
->dvb
->priv
);
165 if (voltage
== SEC_VOLTAGE_18
) {
166 msg
[0].addr
= ADDR_B601_VOLTAGE_18V
;
167 msg
[0].buf
= command_18v
;
169 i2c_transfer(&udev_adap
->dev
->i2c_adap
, msg
, 1);
173 static int opera1_stv0299_set_symbol_rate(struct dvb_frontend
*fe
, u32 srate
,
176 stv0299_writereg(fe
, 0x13, 0x98);
177 stv0299_writereg(fe
, 0x14, 0x95);
178 stv0299_writereg(fe
, REG_1F_SYMBOLRATE_BYTE0
, (ratio
>> 16) & 0xff);
179 stv0299_writereg(fe
, REG_20_SYMBOLRATE_BYTE1
, (ratio
>> 8) & 0xff);
180 stv0299_writereg(fe
, REG_21_SYMBOLRATE_BYTE2
, (ratio
) & 0xf0);
184 static u8 opera1_inittab
[] = {
213 REG_1F_SYMBOLRATE_BYTE0
, 0x06,
214 REG_20_SYMBOLRATE_BYTE1
, 0x50,
215 REG_21_SYMBOLRATE_BYTE2
, 0x10,
238 static struct stv0299_config opera1_stv0299_config
= {
239 .demod_address
= 0xd0>>1,
244 .lock_output
= STV0229_LOCKOUTPUT_0
,
245 .volt13_op0_op1
= STV0299_VOLT13_OP0
,
246 .inittab
= opera1_inittab
,
247 .set_symbol_rate
= opera1_stv0299_set_symbol_rate
,
250 static int opera1_frontend_attach(struct dvb_usb_adapter
*d
)
253 dvb_attach(stv0299_attach
, &opera1_stv0299_config
,
254 &d
->dev
->i2c_adap
)) != NULL
) {
255 d
->fe
->ops
.set_voltage
= opera1_set_voltage
;
258 info("not attached stv0299");
262 static int opera1_tuner_attach(struct dvb_usb_adapter
*adap
)
265 dvb_pll_attach
, adap
->fe
, 0xc0>>1,
266 &adap
->dev
->i2c_adap
, &dvb_pll_opera1
271 static int opera1_power_ctrl(struct dvb_usb_device
*d
, int onoff
)
273 u8 val
= onoff
? 0x01 : 0x00;
275 if (dvb_usb_opera1_debug
)
276 info("power %s", onoff
? "on" : "off");
277 return opera1_xilinx_rw(d
->udev
, 0xb7, val
,
278 &val
, 1, OPERA_WRITE_MSG
);
281 static int opera1_streaming_ctrl(struct dvb_usb_adapter
*adap
, int onoff
)
283 static u8 buf_start
[2] = { 0xff, 0x03 };
284 static u8 buf_stop
[2] = { 0xff, 0x00 };
285 struct i2c_msg start_tuner
[] = {
286 {.addr
= ADDR_B1A6_STREAM_CTRL
,.buf
= onoff
? buf_start
: buf_stop
,.len
= 2},
288 if (dvb_usb_opera1_debug
)
289 info("streaming %s", onoff
? "on" : "off");
290 i2c_transfer(&adap
->dev
->i2c_adap
, start_tuner
, 1);
294 static int opera1_pid_filter(struct dvb_usb_adapter
*adap
, int index
, u16 pid
,
298 struct i2c_msg msg
[] = {
299 {.addr
= ADDR_B1A6_STREAM_CTRL
,.buf
= b_pid
,.len
= 3},
301 if (dvb_usb_opera1_debug
)
302 info("pidfilter index: %d pid: %d %s", index
, pid
,
303 onoff
? "on" : "off");
304 b_pid
[0] = (2 * index
) + 4;
305 b_pid
[1] = onoff
? (pid
& 0xff) : (0x00);
306 b_pid
[2] = onoff
? ((pid
>> 8) & 0xff) : (0x00);
307 i2c_transfer(&adap
->dev
->i2c_adap
, msg
, 1);
311 static int opera1_pid_filter_control(struct dvb_usb_adapter
*adap
, int onoff
)
315 struct i2c_msg msg
[] = {
316 {.addr
= ADDR_B1A6_STREAM_CTRL
,.buf
= b_pid
,.len
= 3},
318 if (dvb_usb_opera1_debug
)
319 info("%s hw-pidfilter", onoff
? "enable" : "disable");
320 for (; u
< 0x7e; u
+= 2) {
324 i2c_transfer(&adap
->dev
->i2c_adap
, msg
, 1);
329 static struct dvb_usb_rc_key opera1_rc_keys
[] = {
340 {0x09, 0xf6, KEY_UP
}, /*chanup */
341 {0x1b, 0xe5, KEY_DOWN
}, /*chandown */
342 {0x5d, 0xa3, KEY_LEFT
}, /*voldown */
343 {0x5f, 0xa1, KEY_RIGHT
}, /*volup */
344 {0x07, 0xf8, KEY_SPACE
}, /*tab */
345 {0x1f, 0xe1, KEY_ENTER
}, /*play ok */
346 {0x1b, 0xe4, KEY_Z
}, /*zoom */
347 {0x59, 0xa6, KEY_M
}, /*mute */
348 {0x5b, 0xa5, KEY_F
}, /*tv/f */
349 {0x19, 0xe7, KEY_R
}, /*rec */
350 {0x01, 0xfe, KEY_S
}, /*Stop */
351 {0x03, 0xfd, KEY_P
}, /*pause */
352 {0x03, 0xfc, KEY_W
}, /*<- -> */
353 {0x07, 0xf9, KEY_C
}, /*capture */
354 {0x47, 0xb9, KEY_Q
}, /*exit */
355 {0x43, 0xbc, KEY_O
}, /*power */
359 static int opera1_rc_query(struct dvb_usb_device
*dev
, u32
* event
, int *state
)
361 struct opera1_state
*opst
= dev
->priv
;
363 const u16 startmarker1
= 0x10ed;
364 const u16 startmarker2
= 0x11ec;
365 struct i2c_msg read_remote
[] = {
366 {.addr
= ADDR_B880_READ_REMOTE
,.buf
= rcbuffer
,.flags
= I2C_M_RD
,.len
= 32},
371 if (i2c_transfer(&dev
->i2c_adap
, read_remote
, 1) == 1) {
372 for (i
= 0; i
< 32; i
++) {
376 send_key
= send_key
<< 1;
378 if (send_key
& 0x8000)
379 send_key
= (send_key
<< 1) | (send_key
>> 15 & 0x01);
381 if (send_key
== 0xffff && opst
->last_key_pressed
!= 0) {
382 *state
= REMOTE_KEY_REPEAT
;
383 *event
= opst
->last_key_pressed
;
386 for (; send_key
!= 0;) {
387 if (send_key
>> 16 == startmarker2
) {
389 } else if (send_key
>> 16 == startmarker1
) {
391 (send_key
& 0xfffeffff) | (startmarker1
<< 16);
400 send_key
= (send_key
& 0xffff) | 0x0100;
402 for (i
= 0; i
< ARRAY_SIZE(opera1_rc_keys
); i
++) {
403 if ((opera1_rc_keys
[i
].custom
* 256 +
404 opera1_rc_keys
[i
].data
) == (send_key
& 0xffff)) {
405 *state
= REMOTE_KEY_PRESSED
;
406 *event
= opera1_rc_keys
[i
].event
;
407 opst
->last_key_pressed
=
408 opera1_rc_keys
[i
].event
;
411 opst
->last_key_pressed
= 0;
414 *state
= REMOTE_NO_KEY_PRESSED
;
418 static struct usb_device_id opera1_table
[] = {
419 {USB_DEVICE(USB_VID_CYPRESS
, USB_PID_OPERA1_COLD
)},
420 {USB_DEVICE(USB_VID_OPERA1
, USB_PID_OPERA1_WARM
)},
424 MODULE_DEVICE_TABLE(usb
, opera1_table
);
426 static int opera1_read_mac_address(struct dvb_usb_device
*d
, u8 mac
[6])
428 u8 command
[] = { READ_MAC_ADDR
};
429 opera1_xilinx_rw(d
->udev
, 0xb1, 0xa0, command
, 1, OPERA_WRITE_MSG
);
430 opera1_xilinx_rw(d
->udev
, 0xb1, 0xa1, mac
, 6, OPERA_READ_MSG
);
433 static int opera1_xilinx_load_firmware(struct usb_device
*dev
,
434 const char *filename
)
436 const struct firmware
*fw
= NULL
;
440 info("start downloading fpga firmware");
442 if ((ret
= request_firmware(&fw
, filename
, &dev
->dev
)) != 0) {
443 err("did not find the firmware file. (%s) "
444 "Please see linux/Documentation/dvb/ for more details on firmware-problems.",
448 p
= kmalloc(fw
->size
, GFP_KERNEL
);
449 opera1_xilinx_rw(dev
, 0xbc, 0x00, &testval
, 1, OPERA_READ_MSG
);
450 if (p
!= NULL
&& testval
!= 0x67) {
452 u8 reset
= 0, fpga_command
= 0;
453 memcpy(p
, fw
->data
, fw
->size
);
455 opera1_xilinx_rw(dev
, 0xbc, 0xaa, &fpga_command
, 1,
457 for (i
= 0; p
[i
] != 0 && i
< fw
->size
;) {
460 (dev
, OPERA_WRITE_FX2
, 0x0, b
+ 1, b
[0],
461 OPERA_WRITE_MSG
) != b
[0]
463 err("error while transferring firmware");
469 /* restart the CPU */
470 if (ret
|| opera1_xilinx_rw
471 (dev
, 0xa0, 0xe600, &reset
, 1,
472 OPERA_WRITE_MSG
) != 1) {
473 err("could not restart the USB controller CPU.");
480 release_firmware(fw
);
485 static struct dvb_usb_device_properties opera1_properties
= {
486 .caps
= DVB_USB_IS_AN_I2C_ADAPTER
,
487 .usb_ctrl
= CYPRESS_FX2
,
488 .firmware
= "dvb-usb-opera-01.fw",
489 .size_of_priv
= sizeof(struct opera1_state
),
491 .power_ctrl
= opera1_power_ctrl
,
492 .i2c_algo
= &opera1_i2c_algo
,
494 .rc_key_map
= opera1_rc_keys
,
495 .rc_key_map_size
= ARRAY_SIZE(opera1_rc_keys
),
497 .rc_query
= opera1_rc_query
,
498 .read_mac_address
= opera1_read_mac_address
,
499 .generic_bulk_ctrl_endpoint
= 0x00,
500 /* parameter for the MPEG2-data transfer */
504 .frontend_attach
= opera1_frontend_attach
,
505 .streaming_ctrl
= opera1_streaming_ctrl
,
506 .tuner_attach
= opera1_tuner_attach
,
508 DVB_USB_ADAP_HAS_PID_FILTER
|
509 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF
,
510 .pid_filter
= opera1_pid_filter
,
511 .pid_filter_ctrl
= opera1_pid_filter_control
,
512 .pid_filter_count
= 252,
525 .num_device_descs
= 1,
527 {"Opera1 DVB-S USB2.0",
528 {&opera1_table
[0], NULL
},
529 {&opera1_table
[1], NULL
},
534 static int opera1_probe(struct usb_interface
*intf
,
535 const struct usb_device_id
*id
)
537 struct dvb_usb_device
*d
;
538 struct usb_device
*udev
= interface_to_usbdev(intf
);
540 if (udev
->descriptor
.idProduct
== USB_PID_OPERA1_WARM
&&
541 udev
->descriptor
.idVendor
== USB_VID_OPERA1
&&
543 || opera1_xilinx_load_firmware(udev
, "dvb-usb-opera1-fpga.fw") != 0)
548 if (dvb_usb_device_init(intf
, &opera1_properties
, THIS_MODULE
, &d
) != 0)
553 static struct usb_driver opera1_driver
= {
555 .probe
= opera1_probe
,
556 .disconnect
= dvb_usb_device_exit
,
557 .id_table
= opera1_table
,
560 static int __init
opera1_module_init(void)
563 if ((result
= usb_register(&opera1_driver
))) {
564 err("usb_register failed. Error number %d", result
);
569 static void __exit
opera1_module_exit(void)
571 usb_deregister(&opera1_driver
);
574 module_init(opera1_module_init
);
575 module_exit(opera1_module_exit
);
577 MODULE_AUTHOR("Mario Hlawitschka (c) dh1pa@amsat.org");
578 MODULE_AUTHOR("Marco Gittler (c) g.marco@freenet.de");
579 MODULE_DESCRIPTION("Driver for Opera1 DVB-S device");
580 MODULE_VERSION("0.1");
581 MODULE_LICENSE("GPL");