opera1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. #include "opera1.h"
  13. #include "stv0299.h"
  14. #define OPERA_READ_MSG 0
  15. #define OPERA_WRITE_MSG 1
  16. #define OPERA_I2C_TUNER 0xd1
  17. #define READ_FX2_REG_REQ 0xba
  18. #define READ_MAC_ADDR 0x08
  19. #define OPERA_WRITE_FX2 0xbb
  20. #define OPERA_TUNER_REQ 0xb1
  21. #define REG_1F_SYMBOLRATE_BYTE0 0x1f
  22. #define REG_20_SYMBOLRATE_BYTE1 0x20
  23. #define REG_21_SYMBOLRATE_BYTE2 0x21
  24. struct opera1_state {
  25. u32 last_key_pressed;
  26. };
  27. struct opera_rc_keys {
  28. u32 keycode;
  29. u32 event;
  30. };
  31. int dvb_usb_opera1_debug;
  32. module_param_named(debug, dvb_usb_opera1_debug, int, 0644);
  33. MODULE_PARM_DESC(debug,
  34. "set debugging level (1=info,xfer=2,pll=4,ts=8,err=16,rc=32,fw=64 (or-able))."
  35. DVB_USB_DEBUG_STATUS);
  36. static int opera1_xilinx_rw(struct usb_device *dev, u8 request, u16 value,
  37. u8 * data, u16 len, int flags)
  38. {
  39. int ret;
  40. u8 r;
  41. u8 u8buf[len];
  42. unsigned int pipe = (flags == OPERA_READ_MSG) ?
  43. usb_rcvctrlpipe(dev,0) : usb_sndctrlpipe(dev, 0);
  44. u8 request_type = (flags == OPERA_READ_MSG) ? USB_DIR_IN : USB_DIR_OUT;
  45. if (flags == OPERA_WRITE_MSG)
  46. memcpy(u8buf, data, len);
  47. ret =
  48. usb_control_msg(dev, pipe, request, request_type | USB_TYPE_VENDOR,
  49. value, 0x0, u8buf, len, 2000);
  50. if (request == OPERA_TUNER_REQ) {
  51. if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  52. OPERA_TUNER_REQ, USB_DIR_IN | USB_TYPE_VENDOR,
  53. 0x01, 0x0, &r, 1, 2000)<1 || r!=0x08)
  54. return 0;
  55. }
  56. if (flags == OPERA_READ_MSG)
  57. memcpy(data, u8buf, len);
  58. return ret;
  59. }
  60. /* I2C */
  61. static int opera1_usb_i2c_msgxfer(struct dvb_usb_device *dev, u16 addr,
  62. u8 * buf, u16 len, int flag)
  63. {
  64. int ret = 0;
  65. u8 request;
  66. u16 value;
  67. if (!dev) {
  68. info("no usb_device");
  69. return -EINVAL;
  70. }
  71. if (mutex_lock_interruptible(&dev->usb_mutex) < 0)
  72. return -EAGAIN;
  73. request = (addr & 0xff00) >> 8;
  74. if (!request)
  75. request = 0xb1;
  76. value = (addr & 0xff);
  77. if (flag & OPERA_READ_MSG) {
  78. value |= 0x01;
  79. }
  80. if (request == 0xa0)
  81. value = 0xe600;
  82. ret = opera1_xilinx_rw(dev->udev, request, value, buf, len, flag);
  83. mutex_unlock(&dev->usb_mutex);
  84. return ret;
  85. }
  86. static int opera1_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  87. int num)
  88. {
  89. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  90. int i = 0, tmp = 0;
  91. if (!d)
  92. return -ENODEV;
  93. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  94. return -EAGAIN;
  95. for (i = 0; i < num; i++) {
  96. if ((tmp = opera1_usb_i2c_msgxfer(d,
  97. msg[i].addr,
  98. msg[i].buf,
  99. msg[i].len,
  100. (msg[i].flags ==
  101. I2C_M_RD ?
  102. OPERA_READ_MSG :
  103. OPERA_WRITE_MSG))!= msg[i].len)) {
  104. break;
  105. }
  106. if (dvb_usb_opera1_debug & 0x10)
  107. info("sending i2c mesage %d %d", tmp, msg[i].len);
  108. }
  109. mutex_unlock(&d->i2c_mutex);
  110. return num;
  111. }
  112. static u32 opera1_i2c_func(struct i2c_adapter *adapter)
  113. {
  114. return I2C_FUNC_I2C;
  115. }
  116. static struct i2c_algorithm opera1_i2c_algo = {
  117. .master_xfer = opera1_i2c_xfer,
  118. .functionality = opera1_i2c_func,
  119. };
  120. static int opera1_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
  121. {
  122. static u8 command_13v[1]={0x00};
  123. static u8 command_18v[1]={0x01};
  124. struct i2c_msg msg[] = {
  125. {.addr = 0xb600,.flags = 0,.buf = command_13v,.len = 1},
  126. };
  127. struct dvb_usb_adapter *udev_adap =
  128. (struct dvb_usb_adapter *)(fe->dvb->priv);
  129. if (voltage == SEC_VOLTAGE_18) {
  130. msg[0].addr = 0xb601;
  131. msg[0].buf = command_18v;
  132. }
  133. i2c_transfer(&udev_adap->dev->i2c_adap, msg, 1);
  134. return 0;
  135. }
  136. static int opera1_stv0299_set_symbol_rate(struct dvb_frontend *fe, u32 srate,
  137. u32 ratio)
  138. {
  139. stv0299_writereg(fe, 0x13, 0x98);
  140. stv0299_writereg(fe, 0x14, 0x95);
  141. stv0299_writereg(fe, REG_1F_SYMBOLRATE_BYTE0, (ratio >> 16) & 0xff);
  142. stv0299_writereg(fe, REG_20_SYMBOLRATE_BYTE1, (ratio >> 8) & 0xff);
  143. stv0299_writereg(fe, REG_21_SYMBOLRATE_BYTE2, (ratio) & 0xf0);
  144. return 0;
  145. }
  146. static u8 opera1_inittab[] = {
  147. 0x00, 0xa1,
  148. 0x01, 0x15,
  149. 0x02, 0x00,
  150. 0x03, 0x00,
  151. 0x04, 0x7d,
  152. 0x05, 0x05,
  153. 0x06, 0x02,
  154. 0x07, 0x00,
  155. 0x0b, 0x00,
  156. 0x0c, 0x01,
  157. 0x0d, 0x81,
  158. 0x0e, 0x44,
  159. 0x0f, 0x19,
  160. 0x10, 0x3f,
  161. 0x11, 0x84,
  162. 0x12, 0xda,
  163. 0x13, 0x98,
  164. 0x14, 0x95,
  165. 0x15, 0xc9,
  166. 0x16, 0xeb,
  167. 0x17, 0x00,
  168. 0x18, 0x19,
  169. 0x19, 0x8b,
  170. 0x1a, 0x00,
  171. 0x1b, 0x82,
  172. 0x1c, 0x7f,
  173. 0x1d, 0x00,
  174. 0x1e, 0x00,
  175. REG_1F_SYMBOLRATE_BYTE0, 0x06,
  176. REG_20_SYMBOLRATE_BYTE1, 0x50,
  177. REG_21_SYMBOLRATE_BYTE2, 0x10,
  178. 0x22, 0x00,
  179. 0x23, 0x00,
  180. 0x24, 0x37,
  181. 0x25, 0xbc,
  182. 0x26, 0x00,
  183. 0x27, 0x00,
  184. 0x28, 0x00,
  185. 0x29, 0x1e,
  186. 0x2a, 0x14,
  187. 0x2b, 0x1f,
  188. 0x2c, 0x09,
  189. 0x2d, 0x0a,
  190. 0x2e, 0x00,
  191. 0x2f, 0x00,
  192. 0x30, 0x00,
  193. 0x31, 0x1f,
  194. 0x32, 0x19,
  195. 0x33, 0xfc,
  196. 0x34, 0x13,
  197. 0xff, 0xff,
  198. };
  199. static struct stv0299_config opera1_stv0299_config = {
  200. .demod_address = 0xd0,
  201. .min_delay_ms = 100,
  202. .mclk = 88000000UL,
  203. .invert = 1,
  204. .skip_reinit = 0,
  205. .lock_output = STV0229_LOCKOUTPUT_0,
  206. .volt13_op0_op1 = STV0299_VOLT13_OP0,
  207. .inittab = opera1_inittab,
  208. .set_symbol_rate = opera1_stv0299_set_symbol_rate,
  209. };
  210. static int opera1_frontend_attach(struct dvb_usb_adapter *d)
  211. {
  212. if ((d->fe =
  213. dvb_attach(stv0299_attach, &opera1_stv0299_config,
  214. &d->dev->i2c_adap)) != NULL) {
  215. d->fe->ops.set_voltage = opera1_set_voltage;
  216. return 0;
  217. }
  218. info("not attached stv0299");
  219. return -EIO;
  220. }
  221. static int opera1_tuner_attach(struct dvb_usb_adapter *adap)
  222. {
  223. adap->pll_addr = 0xc0;
  224. adap->pll_desc = &dvb_pll_opera1;
  225. adap->fe->ops.tuner_ops.set_params = dvb_usb_tuner_set_params_i2c;
  226. return 0;
  227. }
  228. static int opera1_power_ctrl(struct dvb_usb_device *d, int onoff)
  229. {
  230. int addr = onoff ? 0xb701 : 0xb700;
  231. u8 val = onoff ? 0x01 : 0x00;
  232. if (dvb_usb_opera1_debug)
  233. info("power %s", onoff ? "on" : "off");
  234. return opera1_usb_i2c_msgxfer(d, addr, &val, 1, OPERA_WRITE_MSG);
  235. }
  236. static int opera1_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  237. {
  238. static u8 buf_start[2] = { 0xff, 0x03 };
  239. static u8 buf_stop[2] = { 0xff, 0x00 };
  240. struct i2c_msg start_tuner[] = {
  241. {.addr = 0xb1a6,.buf = onoff ? buf_start : buf_stop,.len = 2},
  242. };
  243. if (dvb_usb_opera1_debug)
  244. info("streaming %s", onoff ? "on" : "off");
  245. i2c_transfer(&adap->dev->i2c_adap, start_tuner, 1);
  246. return 0;
  247. }
  248. static int opera1_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
  249. int onoff)
  250. {
  251. u8 b_pid[3];
  252. struct i2c_msg msg[] = {
  253. {.addr = 0xb1a6,.buf = b_pid,.len = 3},
  254. };
  255. if (dvb_usb_opera1_debug)
  256. info("pidfilter index: %d pid: %d %s", index, pid,
  257. onoff ? "on" : "off");
  258. b_pid[0] = (2 * index) + 4;
  259. b_pid[1] = onoff ? (pid & 0xff) : (0x00);
  260. b_pid[2] = onoff ? ((pid >> 8) & 0xff) : (0x00);
  261. i2c_transfer(&adap->dev->i2c_adap, msg, 1);
  262. return 0;
  263. }
  264. static int opera1_pid_filter_control(struct dvb_usb_adapter *adap, int onoff)
  265. {
  266. int u = 0x04;
  267. u8 b_pid[3];
  268. struct i2c_msg msg[] = {
  269. {.addr = 0xb1a6,.buf = b_pid,.len = 3},
  270. };
  271. if (dvb_usb_opera1_debug)
  272. info("%s hw-pidfilter", onoff ? "enable" : "disable");
  273. for (; u < 0x7e; u += 2) {
  274. b_pid[0] = u;
  275. b_pid[1] = 0;
  276. b_pid[2] = 0x80;
  277. i2c_transfer(&adap->dev->i2c_adap, msg, 1);
  278. }
  279. return 0;
  280. }
  281. static struct dvb_usb_rc_key opera1_rc_keys[] = {
  282. {0x5f, 0xa0, KEY_1},
  283. {0x51, 0xaf, KEY_2},
  284. {0x5d, 0xa2, KEY_3},
  285. {0x41, 0xbe, KEY_4},
  286. {0x0b, 0xf5, KEY_5},
  287. {0x43, 0xbd, KEY_6},
  288. {0x47, 0xb8, KEY_7},
  289. {0x49, 0xb6, KEY_8},
  290. {0x05, 0xfa, KEY_9},
  291. {0x45, 0xba, KEY_0},
  292. {0x09, 0xf6, KEY_UP}, /*chanup */
  293. {0x1b, 0xe5, KEY_DOWN}, /*chandown */
  294. {0x5d, 0xa3, KEY_LEFT}, /*voldown */
  295. {0x5f, 0xa1, KEY_RIGHT}, /*volup */
  296. {0x07, 0xf8, KEY_SPACE}, /*tab */
  297. {0x1f, 0xe1, KEY_ENTER}, /*play ok */
  298. {0x1b, 0xe4, KEY_Z}, /*zoom */
  299. {0x59, 0xa6, KEY_M}, /*mute */
  300. {0x5b, 0xa5, KEY_F}, /*tv/f */
  301. {0x19, 0xe7, KEY_R}, /*rec */
  302. {0x01, 0xfe, KEY_S}, /*Stop */
  303. {0x03, 0xfd, KEY_P}, /*pause */
  304. {0x03, 0xfc, KEY_W}, /*<- -> */
  305. {0x07, 0xf9, KEY_C}, /*capture */
  306. {0x47, 0xb9, KEY_Q}, /*exit */
  307. {0x43, 0xbc, KEY_O}, /*power */
  308. };
  309. static int opera1_rc_query(struct dvb_usb_device *dev, u32 * event, int *state)
  310. {
  311. struct opera1_state *opst = dev->priv;
  312. u8 rcbuffer[32];
  313. const u16 startmarker1 = 0x10ed;
  314. const u16 startmarker2 = 0x11ec;
  315. struct i2c_msg read_remote[] = {
  316. {.addr = 0xb880,.buf = rcbuffer,.flags = I2C_M_RD,.len = 32},
  317. };
  318. int i = 0;
  319. u32 send_key = 0;
  320. if (i2c_transfer(&dev->i2c_adap, read_remote, 1) == 1) {
  321. for (i = 0; i < 32; i++) {
  322. if (rcbuffer[i])
  323. send_key |= 1;
  324. if (i < 31)
  325. send_key = send_key << 1;
  326. }
  327. if (send_key & 0x8000)
  328. send_key = (send_key << 1) | (send_key >> 15 & 0x01);
  329. if (send_key == 0xffff && opst->last_key_pressed != 0) {
  330. *state = REMOTE_KEY_REPEAT;
  331. *event = opst->last_key_pressed;
  332. return 0;
  333. }
  334. for (; send_key != 0;) {
  335. if (send_key >> 16 == startmarker2) {
  336. break;
  337. } else if (send_key >> 16 == startmarker1) {
  338. send_key =
  339. (send_key & 0xfffeffff) | (startmarker1 << 16);
  340. break;
  341. } else
  342. send_key >>= 1;
  343. }
  344. if (send_key == 0)
  345. return 0;
  346. send_key = (send_key & 0xffff) | 0x0100;
  347. for (i = 0; i < ARRAY_SIZE(opera1_rc_keys); i++) {
  348. if ((opera1_rc_keys[i].custom * 256 +
  349. opera1_rc_keys[i].data) == (send_key & 0xffff)) {
  350. *state = REMOTE_KEY_PRESSED;
  351. *event = opera1_rc_keys[i].event;
  352. opst->last_key_pressed =
  353. opera1_rc_keys[i].event;
  354. break;
  355. }
  356. opst->last_key_pressed = 0;
  357. }
  358. } else
  359. *state = REMOTE_NO_KEY_PRESSED;
  360. return 0;
  361. }
  362. static struct usb_device_id opera1_table[] = {
  363. {USB_DEVICE(USB_VID_CYPRESS, USB_PID_OPERA1_COLD)},
  364. {USB_DEVICE(USB_VID_OPERA1, USB_PID_OPERA1_WARM)},
  365. {}
  366. };
  367. MODULE_DEVICE_TABLE(usb, opera1_table);
  368. static int opera1_read_mac_address(struct dvb_usb_device *d, u8 mac[6])
  369. {
  370. u8 command[] = { READ_MAC_ADDR };
  371. opera1_xilinx_rw(d->udev, 0xb1, 0xa0, command, 1, OPERA_WRITE_MSG);
  372. opera1_xilinx_rw(d->udev, 0xb1, 0xa1, mac, 6, OPERA_READ_MSG);
  373. return 0;
  374. }
  375. static int opera1_xilinx_load_firmware(struct usb_device *dev,
  376. const char *filename)
  377. {
  378. const struct firmware *fw = NULL;
  379. u8 *b, *p;
  380. int ret = 0, i;
  381. u8 testval;
  382. info("start downloading fpga firmware");
  383. if ((ret = request_firmware(&fw, filename, &dev->dev)) != 0) {
  384. err("did not find the firmware file. (%s) "
  385. "Please see linux/Documentation/dvb/ for more details on firmware-problems.",
  386. filename);
  387. return ret;
  388. } else {
  389. p = kmalloc(fw->size, GFP_KERNEL);
  390. opera1_xilinx_rw(dev, 0xbc, 0x00, &testval, 1, OPERA_READ_MSG);
  391. if (p != NULL && testval != 0x67) {
  392. u8 reset = 0, fpga_command = 0;
  393. memcpy(p, fw->data, fw->size);
  394. /* clear fpga ? */
  395. opera1_xilinx_rw(dev, 0xbc, 0xaa, &fpga_command, 1,
  396. OPERA_WRITE_MSG);
  397. for (i = 0; p[i] != 0 && i < fw->size;) {
  398. b = (u8 *) p + i;
  399. if (opera1_xilinx_rw
  400. (dev, OPERA_WRITE_FX2, 0x0, b + 1, b[0],
  401. OPERA_WRITE_MSG) != b[0]
  402. ) {
  403. err("error while transferring firmware");
  404. ret = -EINVAL;
  405. break;
  406. }
  407. i = i + 1 + b[0];
  408. }
  409. /* restart the CPU */
  410. if (ret || opera1_xilinx_rw
  411. (dev, 0xa0, 0xe600, &reset, 1,
  412. OPERA_WRITE_MSG) != 1) {
  413. err("could not restart the USB controller CPU.");
  414. ret = -EINVAL;
  415. }
  416. kfree(p);
  417. }
  418. }
  419. if (fw) {
  420. release_firmware(fw);
  421. }
  422. return ret;
  423. }
  424. static struct dvb_usb_device_properties opera1_properties = {
  425. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  426. .usb_ctrl = CYPRESS_FX2,
  427. .firmware = "opera.fw",
  428. .size_of_priv = sizeof(struct opera1_state),
  429. .power_ctrl = opera1_power_ctrl,
  430. .i2c_algo = &opera1_i2c_algo,
  431. .rc_key_map = opera1_rc_keys,
  432. .rc_key_map_size = ARRAY_SIZE(opera1_rc_keys),
  433. .rc_interval = 200,
  434. .rc_query = opera1_rc_query,
  435. .read_mac_address = opera1_read_mac_address,
  436. .generic_bulk_ctrl_endpoint = 0x00,
  437. /* parameter for the MPEG2-data transfer */
  438. .num_adapters = 1,
  439. .adapter = {
  440. {
  441. .frontend_attach = opera1_frontend_attach,
  442. .streaming_ctrl = opera1_streaming_ctrl,
  443. .tuner_attach = opera1_tuner_attach,
  444. .caps =
  445. DVB_USB_ADAP_HAS_PID_FILTER |
  446. DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
  447. .pid_filter = opera1_pid_filter,
  448. .pid_filter_ctrl = opera1_pid_filter_control,
  449. .pid_filter_count = 252,
  450. .stream = {
  451. .type = USB_BULK,
  452. .count = 10,
  453. .endpoint = 0x82,
  454. .u = {
  455. .bulk = {
  456. .buffersize = 4096,
  457. }
  458. }
  459. },
  460. }
  461. },
  462. .num_device_descs = 1,
  463. .devices = {
  464. {"Opera1 DVB-S USB2.0",
  465. {&opera1_table[0], NULL},
  466. {&opera1_table[1], NULL},
  467. },
  468. }
  469. };
  470. static int opera1_probe(struct usb_interface *intf,
  471. const struct usb_device_id *id)
  472. {
  473. struct dvb_usb_device *d;
  474. struct usb_device *udev = interface_to_usbdev(intf);
  475. if (udev->descriptor.idProduct == USB_PID_OPERA1_WARM &&
  476. udev->descriptor.idVendor == USB_VID_OPERA1 &&
  477. (d == NULL
  478. || opera1_xilinx_load_firmware(udev, "opera1-fpga.fw") != 0)
  479. ) {
  480. return -EINVAL;
  481. }
  482. if (dvb_usb_device_init(intf, &opera1_properties, THIS_MODULE, &d) != 0)
  483. return -EINVAL;
  484. return 0;
  485. }
  486. static struct usb_driver opera1_driver = {
  487. .name = "opera1",
  488. .probe = opera1_probe,
  489. .disconnect = dvb_usb_device_exit,
  490. .id_table = opera1_table,
  491. };
  492. static int __init opera1_module_init(void)
  493. {
  494. int result = 0;
  495. if ((result = usb_register(&opera1_driver))) {
  496. err("usb_register failed. Error number %d", result);
  497. }
  498. return result;
  499. }
  500. static void __exit opera1_module_exit(void)
  501. {
  502. usb_deregister(&opera1_driver);
  503. }
  504. module_init(opera1_module_init);
  505. module_exit(opera1_module_exit);
  506. MODULE_AUTHOR("Mario Hlawitschka (c) dh1pa@amsat.org");
  507. MODULE_AUTHOR("Marco Gittler (c) g.marco@freenet.de");
  508. MODULE_DESCRIPTION("Driver for Opera1 DVB-S device");
  509. MODULE_VERSION("0.1");
  510. MODULE_LICENSE("GPL");