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
13 #define DVB_USB_LOG_PREFIX "opera"
18 #define OPERA_READ_MSG 0
19 #define OPERA_WRITE_MSG 1
20 #define OPERA_I2C_TUNER 0xd1
22 #define READ_FX2_REG_REQ 0xba
23 #define READ_MAC_ADDR 0x08
24 #define OPERA_WRITE_FX2 0xbb
25 #define OPERA_TUNER_REQ 0xb1
26 #define REG_1F_SYMBOLRATE_BYTE0 0x1f
27 #define REG_20_SYMBOLRATE_BYTE1 0x20
28 #define REG_21_SYMBOLRATE_BYTE2 0x21
30 #define ADDR_B600_VOLTAGE_13V (0x02)
31 #define ADDR_B601_VOLTAGE_18V (0x03)
32 #define ADDR_B1A6_STREAM_CTRL (0x04)
33 #define ADDR_B880_READ_REMOTE (0x05)
38 struct rc_map_opera_table
{
43 static int dvb_usb_opera1_debug
;
44 module_param_named(debug
, dvb_usb_opera1_debug
, int, 0644);
45 MODULE_PARM_DESC(debug
,
46 "set debugging level (1=info,xfer=2,pll=4,ts=8,err=16,rc=32,fw=64 (or-able))."
47 DVB_USB_DEBUG_STATUS
);
49 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
52 static int opera1_xilinx_rw(struct usb_device
*dev
, u8 request
, u16 value
,
53 u8
* data
, u16 len
, int flags
)
59 unsigned int pipe
= (flags
== OPERA_READ_MSG
) ?
60 usb_rcvctrlpipe(dev
,0) : usb_sndctrlpipe(dev
, 0);
61 u8 request_type
= (flags
== OPERA_READ_MSG
) ? USB_DIR_IN
: USB_DIR_OUT
;
63 if (flags
== OPERA_WRITE_MSG
)
64 memcpy(u8buf
, data
, len
);
66 usb_control_msg(dev
, pipe
, request
, request_type
| USB_TYPE_VENDOR
,
67 value
, 0x0, u8buf
, len
, 2000);
69 if (request
== OPERA_TUNER_REQ
) {
70 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
71 OPERA_TUNER_REQ
, USB_DIR_IN
| USB_TYPE_VENDOR
,
72 0x01, 0x0, &r
, 1, 2000)<1 || r
!=0x08)
75 if (flags
== OPERA_READ_MSG
)
76 memcpy(data
, u8buf
, len
);
82 static int opera1_usb_i2c_msgxfer(struct dvb_usb_device
*dev
, u16 addr
,
90 info("no usb_device");
93 if (mutex_lock_interruptible(&dev
->usb_mutex
) < 0)
97 case ADDR_B600_VOLTAGE_13V
:
101 case ADDR_B601_VOLTAGE_18V
:
105 case ADDR_B1A6_STREAM_CTRL
:
109 case ADDR_B880_READ_REMOTE
:
117 ret
= opera1_xilinx_rw(dev
->udev
, request
,
119 addr
&0x01?OPERA_READ_MSG
:OPERA_WRITE_MSG
);
121 mutex_unlock(&dev
->usb_mutex
);
125 static int opera1_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg msg
[],
128 struct dvb_usb_device
*d
= i2c_get_adapdata(adap
);
133 if (mutex_lock_interruptible(&d
->i2c_mutex
) < 0)
136 for (i
= 0; i
< num
; i
++) {
137 if ((tmp
= opera1_usb_i2c_msgxfer(d
,
138 (msg
[i
].addr
<<1)|(msg
[i
].flags
&I2C_M_RD
?0x01:0),
144 if (dvb_usb_opera1_debug
& 0x10)
145 info("sending i2c mesage %d %d", tmp
, msg
[i
].len
);
147 mutex_unlock(&d
->i2c_mutex
);
151 static u32
opera1_i2c_func(struct i2c_adapter
*adapter
)
156 static struct i2c_algorithm opera1_i2c_algo
= {
157 .master_xfer
= opera1_i2c_xfer
,
158 .functionality
= opera1_i2c_func
,
161 static int opera1_set_voltage(struct dvb_frontend
*fe
, fe_sec_voltage_t voltage
)
163 static u8 command_13v
[1]={0x00};
164 static u8 command_18v
[1]={0x01};
165 struct i2c_msg msg
[] = {
166 {.addr
= ADDR_B600_VOLTAGE_13V
,.flags
= 0,.buf
= command_13v
,.len
= 1},
168 struct dvb_usb_adapter
*udev_adap
=
169 (struct dvb_usb_adapter
*)(fe
->dvb
->priv
);
170 if (voltage
== SEC_VOLTAGE_18
) {
171 msg
[0].addr
= ADDR_B601_VOLTAGE_18V
;
172 msg
[0].buf
= command_18v
;
174 i2c_transfer(&udev_adap
->dev
->i2c_adap
, msg
, 1);
178 static int opera1_stv0299_set_symbol_rate(struct dvb_frontend
*fe
, u32 srate
,
181 stv0299_writereg(fe
, 0x13, 0x98);
182 stv0299_writereg(fe
, 0x14, 0x95);
183 stv0299_writereg(fe
, REG_1F_SYMBOLRATE_BYTE0
, (ratio
>> 16) & 0xff);
184 stv0299_writereg(fe
, REG_20_SYMBOLRATE_BYTE1
, (ratio
>> 8) & 0xff);
185 stv0299_writereg(fe
, REG_21_SYMBOLRATE_BYTE2
, (ratio
) & 0xf0);
189 static u8 opera1_inittab
[] = {
218 REG_1F_SYMBOLRATE_BYTE0
, 0x06,
219 REG_20_SYMBOLRATE_BYTE1
, 0x50,
220 REG_21_SYMBOLRATE_BYTE2
, 0x10,
243 static struct stv0299_config opera1_stv0299_config
= {
244 .demod_address
= 0xd0>>1,
249 .lock_output
= STV0299_LOCKOUTPUT_0
,
250 .volt13_op0_op1
= STV0299_VOLT13_OP0
,
251 .inittab
= opera1_inittab
,
252 .set_symbol_rate
= opera1_stv0299_set_symbol_rate
,
255 static int opera1_frontend_attach(struct dvb_usb_adapter
*d
)
258 dvb_attach(stv0299_attach
, &opera1_stv0299_config
,
259 &d
->dev
->i2c_adap
)) != NULL
) {
260 d
->fe
->ops
.set_voltage
= opera1_set_voltage
;
263 info("not attached stv0299");
267 static int opera1_tuner_attach(struct dvb_usb_adapter
*adap
)
270 dvb_pll_attach
, adap
->fe
, 0xc0>>1,
271 &adap
->dev
->i2c_adap
, DVB_PLL_OPERA1
276 static int opera1_power_ctrl(struct dvb_usb_device
*d
, int onoff
)
278 u8 val
= onoff
? 0x01 : 0x00;
280 if (dvb_usb_opera1_debug
)
281 info("power %s", onoff
? "on" : "off");
282 return opera1_xilinx_rw(d
->udev
, 0xb7, val
,
283 &val
, 1, OPERA_WRITE_MSG
);
286 static int opera1_streaming_ctrl(struct dvb_usb_adapter
*adap
, int onoff
)
288 static u8 buf_start
[2] = { 0xff, 0x03 };
289 static u8 buf_stop
[2] = { 0xff, 0x00 };
290 struct i2c_msg start_tuner
[] = {
291 {.addr
= ADDR_B1A6_STREAM_CTRL
,.buf
= onoff
? buf_start
: buf_stop
,.len
= 2},
293 if (dvb_usb_opera1_debug
)
294 info("streaming %s", onoff
? "on" : "off");
295 i2c_transfer(&adap
->dev
->i2c_adap
, start_tuner
, 1);
299 static int opera1_pid_filter(struct dvb_usb_adapter
*adap
, int index
, u16 pid
,
303 struct i2c_msg msg
[] = {
304 {.addr
= ADDR_B1A6_STREAM_CTRL
,.buf
= b_pid
,.len
= 3},
306 if (dvb_usb_opera1_debug
)
307 info("pidfilter index: %d pid: %d %s", index
, pid
,
308 onoff
? "on" : "off");
309 b_pid
[0] = (2 * index
) + 4;
310 b_pid
[1] = onoff
? (pid
& 0xff) : (0x00);
311 b_pid
[2] = onoff
? ((pid
>> 8) & 0xff) : (0x00);
312 i2c_transfer(&adap
->dev
->i2c_adap
, msg
, 1);
316 static int opera1_pid_filter_control(struct dvb_usb_adapter
*adap
, int onoff
)
320 struct i2c_msg msg
[] = {
321 {.addr
= ADDR_B1A6_STREAM_CTRL
,.buf
= b_pid
,.len
= 3},
323 if (dvb_usb_opera1_debug
)
324 info("%s hw-pidfilter", onoff
? "enable" : "disable");
325 for (; u
< 0x7e; u
+= 2) {
329 i2c_transfer(&adap
->dev
->i2c_adap
, msg
, 1);
334 static struct rc_map_table rc_map_opera1_table
[] = {
345 {0x09f6, KEY_CHANNELUP
}, /*chanup */
346 {0x1be5, KEY_CHANNELDOWN
}, /*chandown */
347 {0x5da3, KEY_VOLUMEDOWN
}, /*voldown */
348 {0x5fa1, KEY_VOLUMEUP
}, /*volup */
349 {0x07f8, KEY_SPACE
}, /*tab */
350 {0x1fe1, KEY_OK
}, /*play ok */
351 {0x1be4, KEY_ZOOM
}, /*zoom */
352 {0x59a6, KEY_MUTE
}, /*mute */
353 {0x5ba5, KEY_RADIO
}, /*tv/f */
354 {0x19e7, KEY_RECORD
}, /*rec */
355 {0x01fe, KEY_STOP
}, /*Stop */
356 {0x03fd, KEY_PAUSE
}, /*pause */
357 {0x03fc, KEY_SCREEN
}, /*<- -> */
358 {0x07f9, KEY_CAMERA
}, /*capture */
359 {0x47b9, KEY_ESC
}, /*exit */
360 {0x43bc, KEY_POWER2
}, /*power */
363 static int opera1_rc_query(struct dvb_usb_device
*dev
, u32
* event
, int *state
)
365 struct opera1_state
*opst
= dev
->priv
;
367 const u16 startmarker1
= 0x10ed;
368 const u16 startmarker2
= 0x11ec;
369 struct i2c_msg read_remote
[] = {
370 {.addr
= ADDR_B880_READ_REMOTE
,.buf
= rcbuffer
,.flags
= I2C_M_RD
,.len
= 32},
375 if (i2c_transfer(&dev
->i2c_adap
, read_remote
, 1) == 1) {
376 for (i
= 0; i
< 32; i
++) {
380 send_key
= send_key
<< 1;
382 if (send_key
& 0x8000)
383 send_key
= (send_key
<< 1) | (send_key
>> 15 & 0x01);
385 if (send_key
== 0xffff && opst
->last_key_pressed
!= 0) {
386 *state
= REMOTE_KEY_REPEAT
;
387 *event
= opst
->last_key_pressed
;
390 for (; send_key
!= 0;) {
391 if (send_key
>> 16 == startmarker2
) {
393 } else if (send_key
>> 16 == startmarker1
) {
395 (send_key
& 0xfffeffff) | (startmarker1
<< 16);
404 send_key
= (send_key
& 0xffff) | 0x0100;
406 for (i
= 0; i
< ARRAY_SIZE(rc_map_opera1_table
); i
++) {
407 if (rc5_scan(&rc_map_opera1_table
[i
]) == (send_key
& 0xffff)) {
408 *state
= REMOTE_KEY_PRESSED
;
409 *event
= rc_map_opera1_table
[i
].keycode
;
410 opst
->last_key_pressed
=
411 rc_map_opera1_table
[i
].keycode
;
414 opst
->last_key_pressed
= 0;
417 *state
= REMOTE_NO_KEY_PRESSED
;
421 static struct usb_device_id opera1_table
[] = {
422 {USB_DEVICE(USB_VID_CYPRESS
, USB_PID_OPERA1_COLD
)},
423 {USB_DEVICE(USB_VID_OPERA1
, USB_PID_OPERA1_WARM
)},
427 MODULE_DEVICE_TABLE(usb
, opera1_table
);
429 static int opera1_read_mac_address(struct dvb_usb_device
*d
, u8 mac
[6])
431 u8 command
[] = { READ_MAC_ADDR
};
432 opera1_xilinx_rw(d
->udev
, 0xb1, 0xa0, command
, 1, OPERA_WRITE_MSG
);
433 opera1_xilinx_rw(d
->udev
, 0xb1, 0xa1, mac
, 6, OPERA_READ_MSG
);
436 static int opera1_xilinx_load_firmware(struct usb_device
*dev
,
437 const char *filename
)
439 const struct firmware
*fw
= NULL
;
441 int ret
= 0, i
,fpgasize
=40;
443 info("start downloading fpga firmware %s",filename
);
445 if ((ret
= request_firmware(&fw
, filename
, &dev
->dev
)) != 0) {
446 err("did not find the firmware file. (%s) "
447 "Please see linux/Documentation/dvb/ for more details on firmware-problems.",
451 p
= kmalloc(fw
->size
, GFP_KERNEL
);
452 opera1_xilinx_rw(dev
, 0xbc, 0x00, &testval
, 1, OPERA_READ_MSG
);
453 if (p
!= NULL
&& testval
!= 0x67) {
455 u8 reset
= 0, fpga_command
= 0;
456 memcpy(p
, fw
->data
, fw
->size
);
458 opera1_xilinx_rw(dev
, 0xbc, 0xaa, &fpga_command
, 1,
460 for (i
= 0; i
< fw
->size
;) {
461 if ( (fw
->size
- i
) <fpgasize
){
466 (dev
, OPERA_WRITE_FX2
, 0x0, b
, fpgasize
,
467 OPERA_WRITE_MSG
) != fpgasize
469 err("error while transferring firmware");
475 /* restart the CPU */
476 if (ret
|| opera1_xilinx_rw
477 (dev
, 0xa0, 0xe600, &reset
, 1,
478 OPERA_WRITE_MSG
) != 1) {
479 err("could not restart the USB controller CPU.");
485 release_firmware(fw
);
489 static struct dvb_usb_device_properties opera1_properties
= {
490 .caps
= DVB_USB_IS_AN_I2C_ADAPTER
,
491 .usb_ctrl
= CYPRESS_FX2
,
492 .firmware
= "dvb-usb-opera-01.fw",
493 .size_of_priv
= sizeof(struct opera1_state
),
495 .power_ctrl
= opera1_power_ctrl
,
496 .i2c_algo
= &opera1_i2c_algo
,
499 .rc_map_table
= rc_map_opera1_table
,
500 .rc_map_size
= ARRAY_SIZE(rc_map_opera1_table
),
502 .rc_query
= opera1_rc_query
,
504 .read_mac_address
= opera1_read_mac_address
,
505 .generic_bulk_ctrl_endpoint
= 0x00,
506 /* parameter for the MPEG2-data transfer */
510 .frontend_attach
= opera1_frontend_attach
,
511 .streaming_ctrl
= opera1_streaming_ctrl
,
512 .tuner_attach
= opera1_tuner_attach
,
514 DVB_USB_ADAP_HAS_PID_FILTER
|
515 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF
,
516 .pid_filter
= opera1_pid_filter
,
517 .pid_filter_ctrl
= opera1_pid_filter_control
,
518 .pid_filter_count
= 252,
531 .num_device_descs
= 1,
533 {"Opera1 DVB-S USB2.0",
534 {&opera1_table
[0], NULL
},
535 {&opera1_table
[1], NULL
},
540 static int opera1_probe(struct usb_interface
*intf
,
541 const struct usb_device_id
*id
)
543 struct usb_device
*udev
= interface_to_usbdev(intf
);
545 if (udev
->descriptor
.idProduct
== USB_PID_OPERA1_WARM
&&
546 udev
->descriptor
.idVendor
== USB_VID_OPERA1
&&
547 opera1_xilinx_load_firmware(udev
, "dvb-usb-opera1-fpga-01.fw") != 0
552 if (0 != dvb_usb_device_init(intf
, &opera1_properties
,
553 THIS_MODULE
, NULL
, adapter_nr
))
558 static struct usb_driver opera1_driver
= {
560 .probe
= opera1_probe
,
561 .disconnect
= dvb_usb_device_exit
,
562 .id_table
= opera1_table
,
565 static int __init
opera1_module_init(void)
568 if ((result
= usb_register(&opera1_driver
))) {
569 err("usb_register failed. Error number %d", result
);
574 static void __exit
opera1_module_exit(void)
576 usb_deregister(&opera1_driver
);
579 module_init(opera1_module_init
);
580 module_exit(opera1_module_exit
);
582 MODULE_AUTHOR("Mario Hlawitschka (c) dh1pa@amsat.org");
583 MODULE_AUTHOR("Marco Gittler (c) g.marco@freenet.de");
584 MODULE_DESCRIPTION("Driver for Opera1 DVB-S device");
585 MODULE_VERSION("0.1");
586 MODULE_LICENSE("GPL");