Merge tag 'for-linus-20160324' of git://git.infradead.org/linux-mtd
[deliverable/linux.git] / drivers / media / usb / dvb-usb / opera1.c
CommitLineData
941491f3
MG
1/* DVB USB framework compliant Linux driver for the Opera1 DVB-S Card
2*
3* Copyright (C) 2006 Mario Hlawitschka (dh1pa@amsat.org)
4* Copyright (C) 2006 Marco Gittler (g.marco@freenet.de)
5*
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.
9*
10* see Documentation/dvb/README.dvb-usb for more information
11*/
12
53133afb
AB
13#define DVB_USB_LOG_PREFIX "opera"
14
15#include "dvb-usb.h"
941491f3
MG
16#include "stv0299.h"
17
18#define OPERA_READ_MSG 0
19#define OPERA_WRITE_MSG 1
20#define OPERA_I2C_TUNER 0xd1
21
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
29
86534e5e
MG
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)
34
941491f3
MG
35struct opera1_state {
36 u32 last_key_pressed;
37};
2f4f58d6 38struct rc_map_opera_table {
941491f3
MG
39 u32 keycode;
40 u32 event;
41};
42
53133afb 43static int dvb_usb_opera1_debug;
941491f3
MG
44module_param_named(debug, dvb_usb_opera1_debug, int, 0644);
45MODULE_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);
48
78e92006
JG
49DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
50
51
941491f3
MG
52static int opera1_xilinx_rw(struct usb_device *dev, u8 request, u16 value,
53 u8 * data, u16 len, int flags)
54{
55 int ret;
12fe2a61
FM
56 u8 tmp;
57 u8 *buf;
941491f3
MG
58 unsigned int pipe = (flags == OPERA_READ_MSG) ?
59 usb_rcvctrlpipe(dev,0) : usb_sndctrlpipe(dev, 0);
60 u8 request_type = (flags == OPERA_READ_MSG) ? USB_DIR_IN : USB_DIR_OUT;
61
12fe2a61
FM
62 buf = kmalloc(len, GFP_KERNEL);
63 if (!buf)
64 return -ENOMEM;
65
941491f3 66 if (flags == OPERA_WRITE_MSG)
12fe2a61
FM
67 memcpy(buf, data, len);
68 ret = usb_control_msg(dev, pipe, request,
69 request_type | USB_TYPE_VENDOR, value, 0x0,
70 buf, len, 2000);
941491f3
MG
71
72 if (request == OPERA_TUNER_REQ) {
12fe2a61 73 tmp = buf[0];
941491f3 74 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
12fe2a61
FM
75 OPERA_TUNER_REQ, USB_DIR_IN | USB_TYPE_VENDOR,
76 0x01, 0x0, buf, 1, 2000) < 1 || buf[0] != 0x08) {
77 ret = 0;
78 goto out;
79 }
80 buf[0] = tmp;
941491f3
MG
81 }
82 if (flags == OPERA_READ_MSG)
12fe2a61
FM
83 memcpy(data, buf, len);
84out:
85 kfree(buf);
941491f3
MG
86 return ret;
87}
88
89/* I2C */
90
91static int opera1_usb_i2c_msgxfer(struct dvb_usb_device *dev, u16 addr,
86534e5e 92 u8 * buf, u16 len)
941491f3
MG
93{
94 int ret = 0;
95 u8 request;
96 u16 value;
97
98 if (!dev) {
99 info("no usb_device");
100 return -EINVAL;
101 }
102 if (mutex_lock_interruptible(&dev->usb_mutex) < 0)
103 return -EAGAIN;
104
86534e5e
MG
105 switch (addr>>1){
106 case ADDR_B600_VOLTAGE_13V:
107 request=0xb6;
108 value=0x00;
109 break;
110 case ADDR_B601_VOLTAGE_18V:
111 request=0xb6;
112 value=0x01;
113 break;
114 case ADDR_B1A6_STREAM_CTRL:
115 request=0xb1;
116 value=0xa6;
117 break;
118 case ADDR_B880_READ_REMOTE:
119 request=0xb8;
120 value=0x80;
121 break;
122 default:
123 request=0xb1;
124 value=addr;
941491f3 125 }
86534e5e
MG
126 ret = opera1_xilinx_rw(dev->udev, request,
127 value, buf, len,
128 addr&0x01?OPERA_READ_MSG:OPERA_WRITE_MSG);
941491f3
MG
129
130 mutex_unlock(&dev->usb_mutex);
131 return ret;
132}
133
134static int opera1_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
135 int num)
136{
137 struct dvb_usb_device *d = i2c_get_adapdata(adap);
138 int i = 0, tmp = 0;
139
140 if (!d)
141 return -ENODEV;
142 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
143 return -EAGAIN;
144
145 for (i = 0; i < num; i++) {
146 if ((tmp = opera1_usb_i2c_msgxfer(d,
86534e5e 147 (msg[i].addr<<1)|(msg[i].flags&I2C_M_RD?0x01:0),
941491f3 148 msg[i].buf,
86534e5e 149 msg[i].len
1f39b583 150 )) != msg[i].len) {
941491f3
MG
151 break;
152 }
153 if (dvb_usb_opera1_debug & 0x10)
07f42258 154 info("sending i2c message %d %d", tmp, msg[i].len);
941491f3
MG
155 }
156 mutex_unlock(&d->i2c_mutex);
157 return num;
158}
159
160static u32 opera1_i2c_func(struct i2c_adapter *adapter)
161{
162 return I2C_FUNC_I2C;
163}
164
165static struct i2c_algorithm opera1_i2c_algo = {
166 .master_xfer = opera1_i2c_xfer,
167 .functionality = opera1_i2c_func,
168};
169
0df289a2
MCC
170static int opera1_set_voltage(struct dvb_frontend *fe,
171 enum fe_sec_voltage voltage)
941491f3
MG
172{
173 static u8 command_13v[1]={0x00};
174 static u8 command_18v[1]={0x01};
175 struct i2c_msg msg[] = {
86534e5e 176 {.addr = ADDR_B600_VOLTAGE_13V,.flags = 0,.buf = command_13v,.len = 1},
941491f3
MG
177 };
178 struct dvb_usb_adapter *udev_adap =
179 (struct dvb_usb_adapter *)(fe->dvb->priv);
180 if (voltage == SEC_VOLTAGE_18) {
86534e5e 181 msg[0].addr = ADDR_B601_VOLTAGE_18V;
941491f3
MG
182 msg[0].buf = command_18v;
183 }
184 i2c_transfer(&udev_adap->dev->i2c_adap, msg, 1);
185 return 0;
186}
187
188static int opera1_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate,
189 u32 ratio)
190{
191 stv0299_writereg(fe, 0x13, 0x98);
192 stv0299_writereg(fe, 0x14, 0x95);
193 stv0299_writereg(fe, REG_1F_SYMBOLRATE_BYTE0, (ratio >> 16) & 0xff);
194 stv0299_writereg(fe, REG_20_SYMBOLRATE_BYTE1, (ratio >> 8) & 0xff);
195 stv0299_writereg(fe, REG_21_SYMBOLRATE_BYTE2, (ratio) & 0xf0);
196 return 0;
197
198}
199static u8 opera1_inittab[] = {
200 0x00, 0xa1,
201 0x01, 0x15,
9d8e1b54 202 0x02, 0x30,
941491f3
MG
203 0x03, 0x00,
204 0x04, 0x7d,
205 0x05, 0x05,
206 0x06, 0x02,
207 0x07, 0x00,
208 0x0b, 0x00,
209 0x0c, 0x01,
210 0x0d, 0x81,
211 0x0e, 0x44,
212 0x0f, 0x19,
213 0x10, 0x3f,
214 0x11, 0x84,
215 0x12, 0xda,
216 0x13, 0x98,
217 0x14, 0x95,
218 0x15, 0xc9,
219 0x16, 0xeb,
220 0x17, 0x00,
221 0x18, 0x19,
222 0x19, 0x8b,
223 0x1a, 0x00,
224 0x1b, 0x82,
225 0x1c, 0x7f,
226 0x1d, 0x00,
227 0x1e, 0x00,
228 REG_1F_SYMBOLRATE_BYTE0, 0x06,
229 REG_20_SYMBOLRATE_BYTE1, 0x50,
230 REG_21_SYMBOLRATE_BYTE2, 0x10,
231 0x22, 0x00,
232 0x23, 0x00,
233 0x24, 0x37,
234 0x25, 0xbc,
235 0x26, 0x00,
236 0x27, 0x00,
237 0x28, 0x00,
238 0x29, 0x1e,
239 0x2a, 0x14,
240 0x2b, 0x1f,
241 0x2c, 0x09,
242 0x2d, 0x0a,
243 0x2e, 0x00,
244 0x2f, 0x00,
245 0x30, 0x00,
246 0x31, 0x1f,
247 0x32, 0x19,
248 0x33, 0xfc,
249 0x34, 0x13,
250 0xff, 0xff,
251};
252
253static struct stv0299_config opera1_stv0299_config = {
86534e5e 254 .demod_address = 0xd0>>1,
941491f3
MG
255 .min_delay_ms = 100,
256 .mclk = 88000000UL,
257 .invert = 1,
258 .skip_reinit = 0,
da2c7f66 259 .lock_output = STV0299_LOCKOUTPUT_0,
941491f3
MG
260 .volt13_op0_op1 = STV0299_VOLT13_OP0,
261 .inittab = opera1_inittab,
262 .set_symbol_rate = opera1_stv0299_set_symbol_rate,
263};
264
265static int opera1_frontend_attach(struct dvb_usb_adapter *d)
266{
ee03a679
MK
267 d->fe_adap[0].fe = dvb_attach(stv0299_attach, &opera1_stv0299_config,
268 &d->dev->i2c_adap);
269 if ((d->fe_adap[0].fe) != NULL) {
77eed219 270 d->fe_adap[0].fe->ops.set_voltage = opera1_set_voltage;
941491f3
MG
271 return 0;
272 }
273 info("not attached stv0299");
274 return -EIO;
275}
276
277static int opera1_tuner_attach(struct dvb_usb_adapter *adap)
278{
86534e5e 279 dvb_attach(
77eed219 280 dvb_pll_attach, adap->fe_adap[0].fe, 0xc0>>1,
47a9991e 281 &adap->dev->i2c_adap, DVB_PLL_OPERA1
86534e5e 282 );
941491f3
MG
283 return 0;
284}
285
286static int opera1_power_ctrl(struct dvb_usb_device *d, int onoff)
287{
941491f3 288 u8 val = onoff ? 0x01 : 0x00;
86534e5e 289
941491f3
MG
290 if (dvb_usb_opera1_debug)
291 info("power %s", onoff ? "on" : "off");
86534e5e
MG
292 return opera1_xilinx_rw(d->udev, 0xb7, val,
293 &val, 1, OPERA_WRITE_MSG);
941491f3
MG
294}
295
296static int opera1_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
297{
298 static u8 buf_start[2] = { 0xff, 0x03 };
299 static u8 buf_stop[2] = { 0xff, 0x00 };
300 struct i2c_msg start_tuner[] = {
86534e5e 301 {.addr = ADDR_B1A6_STREAM_CTRL,.buf = onoff ? buf_start : buf_stop,.len = 2},
941491f3
MG
302 };
303 if (dvb_usb_opera1_debug)
304 info("streaming %s", onoff ? "on" : "off");
305 i2c_transfer(&adap->dev->i2c_adap, start_tuner, 1);
306 return 0;
307}
308
309static int opera1_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
310 int onoff)
311{
312 u8 b_pid[3];
313 struct i2c_msg msg[] = {
86534e5e 314 {.addr = ADDR_B1A6_STREAM_CTRL,.buf = b_pid,.len = 3},
941491f3
MG
315 };
316 if (dvb_usb_opera1_debug)
317 info("pidfilter index: %d pid: %d %s", index, pid,
318 onoff ? "on" : "off");
319 b_pid[0] = (2 * index) + 4;
320 b_pid[1] = onoff ? (pid & 0xff) : (0x00);
321 b_pid[2] = onoff ? ((pid >> 8) & 0xff) : (0x00);
322 i2c_transfer(&adap->dev->i2c_adap, msg, 1);
323 return 0;
324}
325
326static int opera1_pid_filter_control(struct dvb_usb_adapter *adap, int onoff)
327{
328 int u = 0x04;
329 u8 b_pid[3];
330 struct i2c_msg msg[] = {
86534e5e 331 {.addr = ADDR_B1A6_STREAM_CTRL,.buf = b_pid,.len = 3},
941491f3
MG
332 };
333 if (dvb_usb_opera1_debug)
334 info("%s hw-pidfilter", onoff ? "enable" : "disable");
335 for (; u < 0x7e; u += 2) {
336 b_pid[0] = u;
337 b_pid[1] = 0;
338 b_pid[2] = 0x80;
339 i2c_transfer(&adap->dev->i2c_adap, msg, 1);
340 }
341 return 0;
342}
343
2f4f58d6 344static struct rc_map_table rc_map_opera1_table[] = {
2e365883
MCC
345 {0x5fa0, KEY_1},
346 {0x51af, KEY_2},
347 {0x5da2, KEY_3},
348 {0x41be, KEY_4},
349 {0x0bf5, KEY_5},
350 {0x43bd, KEY_6},
351 {0x47b8, KEY_7},
352 {0x49b6, KEY_8},
353 {0x05fa, KEY_9},
354 {0x45ba, KEY_0},
76fd93b6
MCC
355 {0x09f6, KEY_CHANNELUP}, /*chanup */
356 {0x1be5, KEY_CHANNELDOWN}, /*chandown */
357 {0x5da3, KEY_VOLUMEDOWN}, /*voldown */
358 {0x5fa1, KEY_VOLUMEUP}, /*volup */
359 {0x07f8, KEY_SPACE}, /*tab */
360 {0x1fe1, KEY_OK}, /*play ok */
361 {0x1be4, KEY_ZOOM}, /*zoom */
362 {0x59a6, KEY_MUTE}, /*mute */
363 {0x5ba5, KEY_RADIO}, /*tv/f */
364 {0x19e7, KEY_RECORD}, /*rec */
365 {0x01fe, KEY_STOP}, /*Stop */
366 {0x03fd, KEY_PAUSE}, /*pause */
367 {0x03fc, KEY_SCREEN}, /*<- -> */
368 {0x07f9, KEY_CAMERA}, /*capture */
369 {0x47b9, KEY_ESC}, /*exit */
370 {0x43bc, KEY_POWER2}, /*power */
941491f3
MG
371};
372
373static int opera1_rc_query(struct dvb_usb_device *dev, u32 * event, int *state)
374{
375 struct opera1_state *opst = dev->priv;
376 u8 rcbuffer[32];
377 const u16 startmarker1 = 0x10ed;
378 const u16 startmarker2 = 0x11ec;
379 struct i2c_msg read_remote[] = {
86534e5e 380 {.addr = ADDR_B880_READ_REMOTE,.buf = rcbuffer,.flags = I2C_M_RD,.len = 32},
941491f3
MG
381 };
382 int i = 0;
383 u32 send_key = 0;
384
385 if (i2c_transfer(&dev->i2c_adap, read_remote, 1) == 1) {
386 for (i = 0; i < 32; i++) {
387 if (rcbuffer[i])
388 send_key |= 1;
389 if (i < 31)
390 send_key = send_key << 1;
391 }
392 if (send_key & 0x8000)
393 send_key = (send_key << 1) | (send_key >> 15 & 0x01);
394
395 if (send_key == 0xffff && opst->last_key_pressed != 0) {
396 *state = REMOTE_KEY_REPEAT;
397 *event = opst->last_key_pressed;
398 return 0;
399 }
400 for (; send_key != 0;) {
401 if (send_key >> 16 == startmarker2) {
402 break;
403 } else if (send_key >> 16 == startmarker1) {
404 send_key =
405 (send_key & 0xfffeffff) | (startmarker1 << 16);
406 break;
407 } else
408 send_key >>= 1;
409 }
410
411 if (send_key == 0)
412 return 0;
413
414 send_key = (send_key & 0xffff) | 0x0100;
415
2f4f58d6
MCC
416 for (i = 0; i < ARRAY_SIZE(rc_map_opera1_table); i++) {
417 if (rc5_scan(&rc_map_opera1_table[i]) == (send_key & 0xffff)) {
941491f3 418 *state = REMOTE_KEY_PRESSED;
2f4f58d6 419 *event = rc_map_opera1_table[i].keycode;
941491f3 420 opst->last_key_pressed =
2f4f58d6 421 rc_map_opera1_table[i].keycode;
941491f3
MG
422 break;
423 }
424 opst->last_key_pressed = 0;
425 }
426 } else
427 *state = REMOTE_NO_KEY_PRESSED;
428 return 0;
429}
430
431static struct usb_device_id opera1_table[] = {
432 {USB_DEVICE(USB_VID_CYPRESS, USB_PID_OPERA1_COLD)},
433 {USB_DEVICE(USB_VID_OPERA1, USB_PID_OPERA1_WARM)},
434 {}
435};
436
437MODULE_DEVICE_TABLE(usb, opera1_table);
438
439static int opera1_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
440{
441 u8 command[] = { READ_MAC_ADDR };
442 opera1_xilinx_rw(d->udev, 0xb1, 0xa0, command, 1, OPERA_WRITE_MSG);
443 opera1_xilinx_rw(d->udev, 0xb1, 0xa1, mac, 6, OPERA_READ_MSG);
444 return 0;
445}
446static int opera1_xilinx_load_firmware(struct usb_device *dev,
447 const char *filename)
448{
449 const struct firmware *fw = NULL;
450 u8 *b, *p;
59800555 451 int ret = 0, i,fpgasize=40;
941491f3 452 u8 testval;
59800555 453 info("start downloading fpga firmware %s",filename);
941491f3
MG
454
455 if ((ret = request_firmware(&fw, filename, &dev->dev)) != 0) {
456 err("did not find the firmware file. (%s) "
457 "Please see linux/Documentation/dvb/ for more details on firmware-problems.",
458 filename);
459 return ret;
460 } else {
461 p = kmalloc(fw->size, GFP_KERNEL);
462 opera1_xilinx_rw(dev, 0xbc, 0x00, &testval, 1, OPERA_READ_MSG);
463 if (p != NULL && testval != 0x67) {
464
465 u8 reset = 0, fpga_command = 0;
466 memcpy(p, fw->data, fw->size);
467 /* clear fpga ? */
468 opera1_xilinx_rw(dev, 0xbc, 0xaa, &fpga_command, 1,
469 OPERA_WRITE_MSG);
59800555
MG
470 for (i = 0; i < fw->size;) {
471 if ( (fw->size - i) <fpgasize){
472 fpgasize=fw->size-i;
473 }
941491f3
MG
474 b = (u8 *) p + i;
475 if (opera1_xilinx_rw
59800555
MG
476 (dev, OPERA_WRITE_FX2, 0x0, b , fpgasize,
477 OPERA_WRITE_MSG) != fpgasize
941491f3
MG
478 ) {
479 err("error while transferring firmware");
480 ret = -EINVAL;
481 break;
482 }
59800555 483 i = i + fpgasize;
941491f3
MG
484 }
485 /* restart the CPU */
486 if (ret || opera1_xilinx_rw
487 (dev, 0xa0, 0xe600, &reset, 1,
488 OPERA_WRITE_MSG) != 1) {
489 err("could not restart the USB controller CPU.");
490 ret = -EINVAL;
491 }
941491f3
MG
492 }
493 }
77596058 494 kfree(p);
a8e07124 495 release_firmware(fw);
941491f3
MG
496 return ret;
497}
498
499static struct dvb_usb_device_properties opera1_properties = {
500 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
501 .usb_ctrl = CYPRESS_FX2,
86534e5e 502 .firmware = "dvb-usb-opera-01.fw",
941491f3
MG
503 .size_of_priv = sizeof(struct opera1_state),
504
505 .power_ctrl = opera1_power_ctrl,
506 .i2c_algo = &opera1_i2c_algo,
507
f72a27b8 508 .rc.legacy = {
2f4f58d6
MCC
509 .rc_map_table = rc_map_opera1_table,
510 .rc_map_size = ARRAY_SIZE(rc_map_opera1_table),
f72a27b8
MCC
511 .rc_interval = 200,
512 .rc_query = opera1_rc_query,
513 },
941491f3
MG
514 .read_mac_address = opera1_read_mac_address,
515 .generic_bulk_ctrl_endpoint = 0x00,
516 /* parameter for the MPEG2-data transfer */
517 .num_adapters = 1,
518 .adapter = {
519 {
77eed219
MK
520 .num_frontends = 1,
521 .fe = {{
941491f3
MG
522 .frontend_attach = opera1_frontend_attach,
523 .streaming_ctrl = opera1_streaming_ctrl,
524 .tuner_attach = opera1_tuner_attach,
525 .caps =
526 DVB_USB_ADAP_HAS_PID_FILTER |
527 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
528 .pid_filter = opera1_pid_filter,
529 .pid_filter_ctrl = opera1_pid_filter_control,
530 .pid_filter_count = 252,
531 .stream = {
532 .type = USB_BULK,
533 .count = 10,
534 .endpoint = 0x82,
535 .u = {
536 .bulk = {
537 .buffersize = 4096,
538 }
539 }
540 },
77eed219 541 }},
941491f3
MG
542 }
543 },
544 .num_device_descs = 1,
545 .devices = {
546 {"Opera1 DVB-S USB2.0",
547 {&opera1_table[0], NULL},
548 {&opera1_table[1], NULL},
549 },
550 }
551};
552
553static int opera1_probe(struct usb_interface *intf,
554 const struct usb_device_id *id)
555{
941491f3
MG
556 struct usb_device *udev = interface_to_usbdev(intf);
557
18d6a28a
HV
558 if (le16_to_cpu(udev->descriptor.idProduct) == USB_PID_OPERA1_WARM &&
559 le16_to_cpu(udev->descriptor.idVendor) == USB_VID_OPERA1 &&
59800555 560 opera1_xilinx_load_firmware(udev, "dvb-usb-opera1-fpga-01.fw") != 0
d06cdbe5 561 ) {
941491f3
MG
562 return -EINVAL;
563 }
564
78e92006
JG
565 if (0 != dvb_usb_device_init(intf, &opera1_properties,
566 THIS_MODULE, NULL, adapter_nr))
941491f3
MG
567 return -EINVAL;
568 return 0;
569}
570
571static struct usb_driver opera1_driver = {
572 .name = "opera1",
573 .probe = opera1_probe,
574 .disconnect = dvb_usb_device_exit,
575 .id_table = opera1_table,
576};
577
ecb3b2b3 578module_usb_driver(opera1_driver);
941491f3
MG
579
580MODULE_AUTHOR("Mario Hlawitschka (c) dh1pa@amsat.org");
581MODULE_AUTHOR("Marco Gittler (c) g.marco@freenet.de");
582MODULE_DESCRIPTION("Driver for Opera1 DVB-S device");
583MODULE_VERSION("0.1");
584MODULE_LICENSE("GPL");
This page took 0.778705 seconds and 5 git commands to generate.