0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #define DVB_USB_LOG_PREFIX "opera"
0011
0012 #include "dvb-usb.h"
0013 #include "stv0299.h"
0014
0015 #define OPERA_READ_MSG 0
0016 #define OPERA_WRITE_MSG 1
0017 #define OPERA_I2C_TUNER 0xd1
0018
0019 #define READ_FX2_REG_REQ 0xba
0020 #define READ_MAC_ADDR 0x08
0021 #define OPERA_WRITE_FX2 0xbb
0022 #define OPERA_TUNER_REQ 0xb1
0023 #define REG_1F_SYMBOLRATE_BYTE0 0x1f
0024 #define REG_20_SYMBOLRATE_BYTE1 0x20
0025 #define REG_21_SYMBOLRATE_BYTE2 0x21
0026
0027 #define ADDR_B600_VOLTAGE_13V (0x02)
0028 #define ADDR_B601_VOLTAGE_18V (0x03)
0029 #define ADDR_B1A6_STREAM_CTRL (0x04)
0030 #define ADDR_B880_READ_REMOTE (0x05)
0031
0032 struct opera1_state {
0033 u32 last_key_pressed;
0034 };
0035 struct rc_map_opera_table {
0036 u32 keycode;
0037 u32 event;
0038 };
0039
0040 static int dvb_usb_opera1_debug;
0041 module_param_named(debug, dvb_usb_opera1_debug, int, 0644);
0042 MODULE_PARM_DESC(debug,
0043 "set debugging level (1=info,xfer=2,pll=4,ts=8,err=16,rc=32,fw=64 (or-able))."
0044 DVB_USB_DEBUG_STATUS);
0045
0046 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
0047
0048
0049 static int opera1_xilinx_rw(struct usb_device *dev, u8 request, u16 value,
0050 u8 * data, u16 len, int flags)
0051 {
0052 int ret;
0053 u8 tmp;
0054 u8 *buf;
0055 unsigned int pipe = (flags == OPERA_READ_MSG) ?
0056 usb_rcvctrlpipe(dev,0) : usb_sndctrlpipe(dev, 0);
0057 u8 request_type = (flags == OPERA_READ_MSG) ? USB_DIR_IN : USB_DIR_OUT;
0058
0059 buf = kmalloc(len, GFP_KERNEL);
0060 if (!buf)
0061 return -ENOMEM;
0062
0063 if (flags == OPERA_WRITE_MSG)
0064 memcpy(buf, data, len);
0065 ret = usb_control_msg(dev, pipe, request,
0066 request_type | USB_TYPE_VENDOR, value, 0x0,
0067 buf, len, 2000);
0068
0069 if (request == OPERA_TUNER_REQ) {
0070 tmp = buf[0];
0071 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
0072 OPERA_TUNER_REQ, USB_DIR_IN | USB_TYPE_VENDOR,
0073 0x01, 0x0, buf, 1, 2000) < 1 || buf[0] != 0x08) {
0074 ret = 0;
0075 goto out;
0076 }
0077 buf[0] = tmp;
0078 }
0079 if (flags == OPERA_READ_MSG)
0080 memcpy(data, buf, len);
0081 out:
0082 kfree(buf);
0083 return ret;
0084 }
0085
0086
0087
0088 static int opera1_usb_i2c_msgxfer(struct dvb_usb_device *dev, u16 addr,
0089 u8 * buf, u16 len)
0090 {
0091 int ret = 0;
0092 u8 request;
0093 u16 value;
0094
0095 if (!dev) {
0096 info("no usb_device");
0097 return -EINVAL;
0098 }
0099 if (mutex_lock_interruptible(&dev->usb_mutex) < 0)
0100 return -EAGAIN;
0101
0102 switch (addr>>1){
0103 case ADDR_B600_VOLTAGE_13V:
0104 request=0xb6;
0105 value=0x00;
0106 break;
0107 case ADDR_B601_VOLTAGE_18V:
0108 request=0xb6;
0109 value=0x01;
0110 break;
0111 case ADDR_B1A6_STREAM_CTRL:
0112 request=0xb1;
0113 value=0xa6;
0114 break;
0115 case ADDR_B880_READ_REMOTE:
0116 request=0xb8;
0117 value=0x80;
0118 break;
0119 default:
0120 request=0xb1;
0121 value=addr;
0122 }
0123 ret = opera1_xilinx_rw(dev->udev, request,
0124 value, buf, len,
0125 addr&0x01?OPERA_READ_MSG:OPERA_WRITE_MSG);
0126
0127 mutex_unlock(&dev->usb_mutex);
0128 return ret;
0129 }
0130
0131 static int opera1_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
0132 int num)
0133 {
0134 struct dvb_usb_device *d = i2c_get_adapdata(adap);
0135 int i = 0, tmp = 0;
0136
0137 if (!d)
0138 return -ENODEV;
0139 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
0140 return -EAGAIN;
0141
0142 for (i = 0; i < num; i++) {
0143 if ((tmp = opera1_usb_i2c_msgxfer(d,
0144 (msg[i].addr<<1)|(msg[i].flags&I2C_M_RD?0x01:0),
0145 msg[i].buf,
0146 msg[i].len
0147 )) != msg[i].len) {
0148 break;
0149 }
0150 if (dvb_usb_opera1_debug & 0x10)
0151 info("sending i2c message %d %d", tmp, msg[i].len);
0152 }
0153 mutex_unlock(&d->i2c_mutex);
0154 return num;
0155 }
0156
0157 static u32 opera1_i2c_func(struct i2c_adapter *adapter)
0158 {
0159 return I2C_FUNC_I2C;
0160 }
0161
0162 static struct i2c_algorithm opera1_i2c_algo = {
0163 .master_xfer = opera1_i2c_xfer,
0164 .functionality = opera1_i2c_func,
0165 };
0166
0167 static int opera1_set_voltage(struct dvb_frontend *fe,
0168 enum fe_sec_voltage voltage)
0169 {
0170 static u8 command_13v[1]={0x00};
0171 static u8 command_18v[1]={0x01};
0172 struct i2c_msg msg[] = {
0173 {.addr = ADDR_B600_VOLTAGE_13V,.flags = 0,.buf = command_13v,.len = 1},
0174 };
0175 struct dvb_usb_adapter *udev_adap =
0176 (struct dvb_usb_adapter *)(fe->dvb->priv);
0177 if (voltage == SEC_VOLTAGE_18) {
0178 msg[0].addr = ADDR_B601_VOLTAGE_18V;
0179 msg[0].buf = command_18v;
0180 }
0181 i2c_transfer(&udev_adap->dev->i2c_adap, msg, 1);
0182 return 0;
0183 }
0184
0185 static int opera1_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate,
0186 u32 ratio)
0187 {
0188 stv0299_writereg(fe, 0x13, 0x98);
0189 stv0299_writereg(fe, 0x14, 0x95);
0190 stv0299_writereg(fe, REG_1F_SYMBOLRATE_BYTE0, (ratio >> 16) & 0xff);
0191 stv0299_writereg(fe, REG_20_SYMBOLRATE_BYTE1, (ratio >> 8) & 0xff);
0192 stv0299_writereg(fe, REG_21_SYMBOLRATE_BYTE2, (ratio) & 0xf0);
0193 return 0;
0194
0195 }
0196 static u8 opera1_inittab[] = {
0197 0x00, 0xa1,
0198 0x01, 0x15,
0199 0x02, 0x30,
0200 0x03, 0x00,
0201 0x04, 0x7d,
0202 0x05, 0x05,
0203 0x06, 0x02,
0204 0x07, 0x00,
0205 0x0b, 0x00,
0206 0x0c, 0x01,
0207 0x0d, 0x81,
0208 0x0e, 0x44,
0209 0x0f, 0x19,
0210 0x10, 0x3f,
0211 0x11, 0x84,
0212 0x12, 0xda,
0213 0x13, 0x98,
0214 0x14, 0x95,
0215 0x15, 0xc9,
0216 0x16, 0xeb,
0217 0x17, 0x00,
0218 0x18, 0x19,
0219 0x19, 0x8b,
0220 0x1a, 0x00,
0221 0x1b, 0x82,
0222 0x1c, 0x7f,
0223 0x1d, 0x00,
0224 0x1e, 0x00,
0225 REG_1F_SYMBOLRATE_BYTE0, 0x06,
0226 REG_20_SYMBOLRATE_BYTE1, 0x50,
0227 REG_21_SYMBOLRATE_BYTE2, 0x10,
0228 0x22, 0x00,
0229 0x23, 0x00,
0230 0x24, 0x37,
0231 0x25, 0xbc,
0232 0x26, 0x00,
0233 0x27, 0x00,
0234 0x28, 0x00,
0235 0x29, 0x1e,
0236 0x2a, 0x14,
0237 0x2b, 0x1f,
0238 0x2c, 0x09,
0239 0x2d, 0x0a,
0240 0x2e, 0x00,
0241 0x2f, 0x00,
0242 0x30, 0x00,
0243 0x31, 0x1f,
0244 0x32, 0x19,
0245 0x33, 0xfc,
0246 0x34, 0x13,
0247 0xff, 0xff,
0248 };
0249
0250 static struct stv0299_config opera1_stv0299_config = {
0251 .demod_address = 0xd0>>1,
0252 .min_delay_ms = 100,
0253 .mclk = 88000000UL,
0254 .invert = 1,
0255 .skip_reinit = 0,
0256 .lock_output = STV0299_LOCKOUTPUT_0,
0257 .volt13_op0_op1 = STV0299_VOLT13_OP0,
0258 .inittab = opera1_inittab,
0259 .set_symbol_rate = opera1_stv0299_set_symbol_rate,
0260 };
0261
0262 static int opera1_frontend_attach(struct dvb_usb_adapter *d)
0263 {
0264 d->fe_adap[0].fe = dvb_attach(stv0299_attach, &opera1_stv0299_config,
0265 &d->dev->i2c_adap);
0266 if ((d->fe_adap[0].fe) != NULL) {
0267 d->fe_adap[0].fe->ops.set_voltage = opera1_set_voltage;
0268 return 0;
0269 }
0270 info("not attached stv0299");
0271 return -EIO;
0272 }
0273
0274 static int opera1_tuner_attach(struct dvb_usb_adapter *adap)
0275 {
0276 dvb_attach(
0277 dvb_pll_attach, adap->fe_adap[0].fe, 0xc0>>1,
0278 &adap->dev->i2c_adap, DVB_PLL_OPERA1
0279 );
0280 return 0;
0281 }
0282
0283 static int opera1_power_ctrl(struct dvb_usb_device *d, int onoff)
0284 {
0285 u8 val = onoff ? 0x01 : 0x00;
0286
0287 if (dvb_usb_opera1_debug)
0288 info("power %s", onoff ? "on" : "off");
0289 return opera1_xilinx_rw(d->udev, 0xb7, val,
0290 &val, 1, OPERA_WRITE_MSG);
0291 }
0292
0293 static int opera1_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
0294 {
0295 static u8 buf_start[2] = { 0xff, 0x03 };
0296 static u8 buf_stop[2] = { 0xff, 0x00 };
0297 struct i2c_msg start_tuner[] = {
0298 {.addr = ADDR_B1A6_STREAM_CTRL,.buf = onoff ? buf_start : buf_stop,.len = 2},
0299 };
0300 if (dvb_usb_opera1_debug)
0301 info("streaming %s", onoff ? "on" : "off");
0302 i2c_transfer(&adap->dev->i2c_adap, start_tuner, 1);
0303 return 0;
0304 }
0305
0306 static int opera1_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
0307 int onoff)
0308 {
0309 u8 b_pid[3];
0310 struct i2c_msg msg[] = {
0311 {.addr = ADDR_B1A6_STREAM_CTRL,.buf = b_pid,.len = 3},
0312 };
0313 if (dvb_usb_opera1_debug)
0314 info("pidfilter index: %d pid: %d %s", index, pid,
0315 onoff ? "on" : "off");
0316 b_pid[0] = (2 * index) + 4;
0317 b_pid[1] = onoff ? (pid & 0xff) : (0x00);
0318 b_pid[2] = onoff ? ((pid >> 8) & 0xff) : (0x00);
0319 i2c_transfer(&adap->dev->i2c_adap, msg, 1);
0320 return 0;
0321 }
0322
0323 static int opera1_pid_filter_control(struct dvb_usb_adapter *adap, int onoff)
0324 {
0325 int u = 0x04;
0326 u8 b_pid[3];
0327 struct i2c_msg msg[] = {
0328 {.addr = ADDR_B1A6_STREAM_CTRL,.buf = b_pid,.len = 3},
0329 };
0330 if (dvb_usb_opera1_debug)
0331 info("%s hw-pidfilter", onoff ? "enable" : "disable");
0332 for (; u < 0x7e; u += 2) {
0333 b_pid[0] = u;
0334 b_pid[1] = 0;
0335 b_pid[2] = 0x80;
0336 i2c_transfer(&adap->dev->i2c_adap, msg, 1);
0337 }
0338 return 0;
0339 }
0340
0341 static struct rc_map_table rc_map_opera1_table[] = {
0342 {0x5fa0, KEY_1},
0343 {0x51af, KEY_2},
0344 {0x5da2, KEY_3},
0345 {0x41be, KEY_4},
0346 {0x0bf5, KEY_5},
0347 {0x43bd, KEY_6},
0348 {0x47b8, KEY_7},
0349 {0x49b6, KEY_8},
0350 {0x05fa, KEY_9},
0351 {0x45ba, KEY_0},
0352 {0x09f6, KEY_CHANNELUP},
0353 {0x1be5, KEY_CHANNELDOWN},
0354 {0x5da3, KEY_VOLUMEDOWN},
0355 {0x5fa1, KEY_VOLUMEUP},
0356 {0x07f8, KEY_SPACE},
0357 {0x1fe1, KEY_OK},
0358 {0x1be4, KEY_ZOOM},
0359 {0x59a6, KEY_MUTE},
0360 {0x5ba5, KEY_RADIO},
0361 {0x19e7, KEY_RECORD},
0362 {0x01fe, KEY_STOP},
0363 {0x03fd, KEY_PAUSE},
0364 {0x03fc, KEY_SCREEN},
0365 {0x07f9, KEY_CAMERA},
0366 {0x47b9, KEY_ESC},
0367 {0x43bc, KEY_POWER2},
0368 };
0369
0370 static int opera1_rc_query(struct dvb_usb_device *dev, u32 * event, int *state)
0371 {
0372 struct opera1_state *opst = dev->priv;
0373 u8 rcbuffer[32];
0374 const u16 startmarker1 = 0x10ed;
0375 const u16 startmarker2 = 0x11ec;
0376 struct i2c_msg read_remote[] = {
0377 {.addr = ADDR_B880_READ_REMOTE,.buf = rcbuffer,.flags = I2C_M_RD,.len = 32},
0378 };
0379 int i = 0;
0380 u32 send_key = 0;
0381
0382 if (i2c_transfer(&dev->i2c_adap, read_remote, 1) == 1) {
0383 for (i = 0; i < 32; i++) {
0384 if (rcbuffer[i])
0385 send_key |= 1;
0386 if (i < 31)
0387 send_key = send_key << 1;
0388 }
0389 if (send_key & 0x8000)
0390 send_key = (send_key << 1) | (send_key >> 15 & 0x01);
0391
0392 if (send_key == 0xffff && opst->last_key_pressed != 0) {
0393 *state = REMOTE_KEY_REPEAT;
0394 *event = opst->last_key_pressed;
0395 return 0;
0396 }
0397 for (; send_key != 0;) {
0398 if (send_key >> 16 == startmarker2) {
0399 break;
0400 } else if (send_key >> 16 == startmarker1) {
0401 send_key =
0402 (send_key & 0xfffeffff) | (startmarker1 << 16);
0403 break;
0404 } else
0405 send_key >>= 1;
0406 }
0407
0408 if (send_key == 0)
0409 return 0;
0410
0411 send_key = (send_key & 0xffff) | 0x0100;
0412
0413 for (i = 0; i < ARRAY_SIZE(rc_map_opera1_table); i++) {
0414 if (rc5_scan(&rc_map_opera1_table[i]) == (send_key & 0xffff)) {
0415 *state = REMOTE_KEY_PRESSED;
0416 *event = rc_map_opera1_table[i].keycode;
0417 opst->last_key_pressed =
0418 rc_map_opera1_table[i].keycode;
0419 break;
0420 }
0421 opst->last_key_pressed = 0;
0422 }
0423 } else
0424 *state = REMOTE_NO_KEY_PRESSED;
0425 return 0;
0426 }
0427
0428 enum {
0429 CYPRESS_OPERA1_COLD,
0430 OPERA1_WARM,
0431 };
0432
0433 static struct usb_device_id opera1_table[] = {
0434 DVB_USB_DEV(CYPRESS, CYPRESS_OPERA1_COLD),
0435 DVB_USB_DEV(OPERA1, OPERA1_WARM),
0436 { }
0437 };
0438
0439 MODULE_DEVICE_TABLE(usb, opera1_table);
0440
0441 static int opera1_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
0442 {
0443 u8 command[] = { READ_MAC_ADDR };
0444 opera1_xilinx_rw(d->udev, 0xb1, 0xa0, command, 1, OPERA_WRITE_MSG);
0445 opera1_xilinx_rw(d->udev, 0xb1, 0xa1, mac, 6, OPERA_READ_MSG);
0446 return 0;
0447 }
0448 static int opera1_xilinx_load_firmware(struct usb_device *dev,
0449 const char *filename)
0450 {
0451 const struct firmware *fw = NULL;
0452 u8 *b, *p;
0453 int ret = 0, i,fpgasize=40;
0454 u8 testval;
0455 info("start downloading fpga firmware %s",filename);
0456
0457 if ((ret = request_firmware(&fw, filename, &dev->dev)) != 0) {
0458 err("did not find the firmware file '%s'. You can use <kernel_dir>/scripts/get_dvb_firmware to get the firmware",
0459 filename);
0460 return ret;
0461 } else {
0462 p = kmalloc(fw->size, GFP_KERNEL);
0463 opera1_xilinx_rw(dev, 0xbc, 0x00, &testval, 1, OPERA_READ_MSG);
0464 if (p != NULL && testval != 0x67) {
0465
0466 u8 reset = 0, fpga_command = 0;
0467 memcpy(p, fw->data, fw->size);
0468
0469 opera1_xilinx_rw(dev, 0xbc, 0xaa, &fpga_command, 1,
0470 OPERA_WRITE_MSG);
0471 for (i = 0; i < fw->size;) {
0472 if ( (fw->size - i) <fpgasize){
0473 fpgasize=fw->size-i;
0474 }
0475 b = (u8 *) p + i;
0476 if (opera1_xilinx_rw
0477 (dev, OPERA_WRITE_FX2, 0x0, b , fpgasize,
0478 OPERA_WRITE_MSG) != fpgasize
0479 ) {
0480 err("error while transferring firmware");
0481 ret = -EINVAL;
0482 break;
0483 }
0484 i = i + fpgasize;
0485 }
0486
0487 if (ret || opera1_xilinx_rw
0488 (dev, 0xa0, 0xe600, &reset, 1,
0489 OPERA_WRITE_MSG) != 1) {
0490 err("could not restart the USB controller CPU.");
0491 ret = -EINVAL;
0492 }
0493 }
0494 }
0495 kfree(p);
0496 release_firmware(fw);
0497 return ret;
0498 }
0499
0500 static struct dvb_usb_device_properties opera1_properties = {
0501 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
0502 .usb_ctrl = CYPRESS_FX2,
0503 .firmware = "dvb-usb-opera-01.fw",
0504 .size_of_priv = sizeof(struct opera1_state),
0505
0506 .power_ctrl = opera1_power_ctrl,
0507 .i2c_algo = &opera1_i2c_algo,
0508
0509 .rc.legacy = {
0510 .rc_map_table = rc_map_opera1_table,
0511 .rc_map_size = ARRAY_SIZE(rc_map_opera1_table),
0512 .rc_interval = 200,
0513 .rc_query = opera1_rc_query,
0514 },
0515 .read_mac_address = opera1_read_mac_address,
0516 .generic_bulk_ctrl_endpoint = 0x00,
0517
0518 .num_adapters = 1,
0519 .adapter = {
0520 {
0521 .num_frontends = 1,
0522 .fe = {{
0523 .frontend_attach = opera1_frontend_attach,
0524 .streaming_ctrl = opera1_streaming_ctrl,
0525 .tuner_attach = opera1_tuner_attach,
0526 .caps =
0527 DVB_USB_ADAP_HAS_PID_FILTER |
0528 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
0529 .pid_filter = opera1_pid_filter,
0530 .pid_filter_ctrl = opera1_pid_filter_control,
0531 .pid_filter_count = 252,
0532 .stream = {
0533 .type = USB_BULK,
0534 .count = 10,
0535 .endpoint = 0x82,
0536 .u = {
0537 .bulk = {
0538 .buffersize = 4096,
0539 }
0540 }
0541 },
0542 }},
0543 }
0544 },
0545 .num_device_descs = 1,
0546 .devices = {
0547 {"Opera1 DVB-S USB2.0",
0548 {&opera1_table[CYPRESS_OPERA1_COLD], NULL},
0549 {&opera1_table[OPERA1_WARM], NULL},
0550 },
0551 }
0552 };
0553
0554 static int opera1_probe(struct usb_interface *intf,
0555 const struct usb_device_id *id)
0556 {
0557 struct usb_device *udev = interface_to_usbdev(intf);
0558
0559 if (le16_to_cpu(udev->descriptor.idProduct) == USB_PID_OPERA1_WARM &&
0560 le16_to_cpu(udev->descriptor.idVendor) == USB_VID_OPERA1 &&
0561 opera1_xilinx_load_firmware(udev, "dvb-usb-opera1-fpga-01.fw") != 0
0562 ) {
0563 return -EINVAL;
0564 }
0565
0566 if (0 != dvb_usb_device_init(intf, &opera1_properties,
0567 THIS_MODULE, NULL, adapter_nr))
0568 return -EINVAL;
0569 return 0;
0570 }
0571
0572 static struct usb_driver opera1_driver = {
0573 .name = "opera1",
0574 .probe = opera1_probe,
0575 .disconnect = dvb_usb_device_exit,
0576 .id_table = opera1_table,
0577 };
0578
0579 module_usb_driver(opera1_driver);
0580
0581 MODULE_AUTHOR("Mario Hlawitschka (c) dh1pa@amsat.org");
0582 MODULE_AUTHOR("Marco Gittler (c) g.marco@freenet.de");
0583 MODULE_DESCRIPTION("Driver for Opera1 DVB-S device");
0584 MODULE_VERSION("0.1");
0585 MODULE_LICENSE("GPL");