gp8psk.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* DVB USB compliant Linux driver for the
  2. * - GENPIX 8pks/qpsk/DCII USB2.0 DVB-S module
  3. *
  4. * Copyright (C) 2006,2007 Alan Nisota (alannisota@gmail.com)
  5. * Copyright (C) 2006,2007 Genpix Electronics (genpix@genpix-electronics.com)
  6. *
  7. * Thanks to GENPIX for the sample code used to implement this module.
  8. *
  9. * This module is based off the vp7045 and vp702x modules
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation, version 2.
  14. *
  15. * see Documentation/dvb/README.dvb-usb for more information
  16. */
  17. #include "gp8psk.h"
  18. #include "gp8psk-fe.h"
  19. /* debug */
  20. static char bcm4500_firmware[] = "/*(DEBLOBBED)*/";
  21. int dvb_usb_gp8psk_debug;
  22. module_param_named(debug,dvb_usb_gp8psk_debug, int, 0644);
  23. MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS);
  24. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  25. struct gp8psk_state {
  26. unsigned char data[80];
  27. };
  28. static int gp8psk_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value,
  29. u16 index, u8 *b, int blen)
  30. {
  31. struct gp8psk_state *st = d->priv;
  32. int ret = 0,try = 0;
  33. if (blen > sizeof(st->data))
  34. return -EIO;
  35. if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
  36. return ret;
  37. while (ret >= 0 && ret != blen && try < 3) {
  38. ret = usb_control_msg(d->udev,
  39. usb_rcvctrlpipe(d->udev,0),
  40. req,
  41. USB_TYPE_VENDOR | USB_DIR_IN,
  42. value, index, st->data, blen,
  43. 2000);
  44. deb_info("reading number %d (ret: %d)\n",try,ret);
  45. try++;
  46. }
  47. if (ret < 0 || ret != blen) {
  48. warn("usb in %d operation failed.", req);
  49. ret = -EIO;
  50. } else {
  51. ret = 0;
  52. memcpy(b, st->data, blen);
  53. }
  54. deb_xfer("in: req. %x, val: %x, ind: %x, buffer: ",req,value,index);
  55. debug_dump(b,blen,deb_xfer);
  56. mutex_unlock(&d->usb_mutex);
  57. return ret;
  58. }
  59. static int gp8psk_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
  60. u16 index, u8 *b, int blen)
  61. {
  62. struct gp8psk_state *st = d->priv;
  63. int ret;
  64. deb_xfer("out: req. %x, val: %x, ind: %x, buffer: ",req,value,index);
  65. debug_dump(b,blen,deb_xfer);
  66. if (blen > sizeof(st->data))
  67. return -EIO;
  68. if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
  69. return ret;
  70. memcpy(st->data, b, blen);
  71. if (usb_control_msg(d->udev,
  72. usb_sndctrlpipe(d->udev,0),
  73. req,
  74. USB_TYPE_VENDOR | USB_DIR_OUT,
  75. value, index, st->data, blen,
  76. 2000) != blen) {
  77. warn("usb out operation failed.");
  78. ret = -EIO;
  79. } else
  80. ret = 0;
  81. mutex_unlock(&d->usb_mutex);
  82. return ret;
  83. }
  84. static int gp8psk_get_fw_version(struct dvb_usb_device *d, u8 *fw_vers)
  85. {
  86. return gp8psk_usb_in_op(d, GET_FW_VERS, 0, 0, fw_vers, 6);
  87. }
  88. static int gp8psk_get_fpga_version(struct dvb_usb_device *d, u8 *fpga_vers)
  89. {
  90. return gp8psk_usb_in_op(d, GET_FPGA_VERS, 0, 0, fpga_vers, 1);
  91. }
  92. static void gp8psk_info(struct dvb_usb_device *d)
  93. {
  94. u8 fpga_vers, fw_vers[6];
  95. if (!gp8psk_get_fw_version(d, fw_vers))
  96. info("FW Version = %i.%02i.%i (0x%x) Build %4i/%02i/%02i",
  97. fw_vers[2], fw_vers[1], fw_vers[0], GP8PSK_FW_VERS(fw_vers),
  98. 2000 + fw_vers[5], fw_vers[4], fw_vers[3]);
  99. else
  100. info("failed to get FW version");
  101. if (!gp8psk_get_fpga_version(d, &fpga_vers))
  102. info("FPGA Version = %i", fpga_vers);
  103. else
  104. info("failed to get FPGA version");
  105. }
  106. static int gp8psk_load_bcm4500fw(struct dvb_usb_device *d)
  107. {
  108. int ret;
  109. const struct firmware *fw = NULL;
  110. const u8 *ptr;
  111. u8 *buf;
  112. if ((ret = reject_firmware(&fw, bcm4500_firmware,
  113. &d->udev->dev)) != 0) {
  114. err("did not find the bcm4500 firmware file. (%s) "
  115. "Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)",
  116. bcm4500_firmware,ret);
  117. return ret;
  118. }
  119. ret = -EINVAL;
  120. if (gp8psk_usb_out_op(d, LOAD_BCM4500,1,0,NULL, 0))
  121. goto out_rel_fw;
  122. info("downloading bcm4500 firmware from file '%s'",bcm4500_firmware);
  123. ptr = fw->data;
  124. buf = kmalloc(64, GFP_KERNEL | GFP_DMA);
  125. if (!buf) {
  126. ret = -ENOMEM;
  127. goto out_rel_fw;
  128. }
  129. while (ptr[0] != 0xff) {
  130. u16 buflen = ptr[0] + 4;
  131. if (ptr + buflen >= fw->data + fw->size) {
  132. err("failed to load bcm4500 firmware.");
  133. goto out_free;
  134. }
  135. if (buflen > 64) {
  136. err("firmare chunk size bigger than 64 bytes.");
  137. goto out_free;
  138. }
  139. memcpy(buf, ptr, buflen);
  140. if (dvb_usb_generic_write(d, buf, buflen)) {
  141. err("failed to load bcm4500 firmware.");
  142. goto out_free;
  143. }
  144. ptr += buflen;
  145. }
  146. ret = 0;
  147. out_free:
  148. kfree(buf);
  149. out_rel_fw:
  150. release_firmware(fw);
  151. return ret;
  152. }
  153. static int gp8psk_power_ctrl(struct dvb_usb_device *d, int onoff)
  154. {
  155. u8 status, buf;
  156. int gp_product_id = le16_to_cpu(d->udev->descriptor.idProduct);
  157. if (onoff) {
  158. gp8psk_usb_in_op(d, GET_8PSK_CONFIG,0,0,&status,1);
  159. if (! (status & bm8pskStarted)) { /* started */
  160. if(gp_product_id == USB_PID_GENPIX_SKYWALKER_CW3K)
  161. gp8psk_usb_out_op(d, CW3K_INIT, 1, 0, NULL, 0);
  162. if (gp8psk_usb_in_op(d, BOOT_8PSK, 1, 0, &buf, 1))
  163. return -EINVAL;
  164. gp8psk_info(d);
  165. }
  166. if (gp_product_id == USB_PID_GENPIX_8PSK_REV_1_WARM)
  167. if (! (status & bm8pskFW_Loaded)) /* BCM4500 firmware loaded */
  168. if(gp8psk_load_bcm4500fw(d))
  169. return -EINVAL;
  170. if (! (status & bmIntersilOn)) /* LNB Power */
  171. if (gp8psk_usb_in_op(d, START_INTERSIL, 1, 0,
  172. &buf, 1))
  173. return -EINVAL;
  174. /* Set DVB mode to 1 */
  175. if (gp_product_id == USB_PID_GENPIX_8PSK_REV_1_WARM)
  176. if (gp8psk_usb_out_op(d, SET_DVB_MODE, 1, 0, NULL, 0))
  177. return -EINVAL;
  178. /* Abort possible TS (if previous tune crashed) */
  179. if (gp8psk_usb_out_op(d, ARM_TRANSFER, 0, 0, NULL, 0))
  180. return -EINVAL;
  181. } else {
  182. /* Turn off LNB power */
  183. if (gp8psk_usb_in_op(d, START_INTERSIL, 0, 0, &buf, 1))
  184. return -EINVAL;
  185. /* Turn off 8psk power */
  186. if (gp8psk_usb_in_op(d, BOOT_8PSK, 0, 0, &buf, 1))
  187. return -EINVAL;
  188. if(gp_product_id == USB_PID_GENPIX_SKYWALKER_CW3K)
  189. gp8psk_usb_out_op(d, CW3K_INIT, 0, 0, NULL, 0);
  190. }
  191. return 0;
  192. }
  193. static int gp8psk_bcm4500_reload(struct dvb_usb_device *d)
  194. {
  195. u8 buf;
  196. int gp_product_id = le16_to_cpu(d->udev->descriptor.idProduct);
  197. deb_xfer("reloading firmware\n");
  198. /* Turn off 8psk power */
  199. if (gp8psk_usb_in_op(d, BOOT_8PSK, 0, 0, &buf, 1))
  200. return -EINVAL;
  201. /* Turn On 8psk power */
  202. if (gp8psk_usb_in_op(d, BOOT_8PSK, 1, 0, &buf, 1))
  203. return -EINVAL;
  204. /* load BCM4500 firmware */
  205. if (gp_product_id == USB_PID_GENPIX_8PSK_REV_1_WARM)
  206. if (gp8psk_load_bcm4500fw(d))
  207. return -EINVAL;
  208. return 0;
  209. }
  210. static int gp8psk_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  211. {
  212. return gp8psk_usb_out_op(adap->dev, ARM_TRANSFER, onoff, 0 , NULL, 0);
  213. }
  214. /* Callbacks for gp8psk-fe.c */
  215. static int gp8psk_fe_in(void *priv, u8 req, u16 value,
  216. u16 index, u8 *b, int blen)
  217. {
  218. struct dvb_usb_device *d = priv;
  219. return gp8psk_usb_in_op(d, req, value, index, b, blen);
  220. }
  221. static int gp8psk_fe_out(void *priv, u8 req, u16 value,
  222. u16 index, u8 *b, int blen)
  223. {
  224. struct dvb_usb_device *d = priv;
  225. return gp8psk_usb_out_op(d, req, value, index, b, blen);
  226. }
  227. static int gp8psk_fe_reload(void *priv)
  228. {
  229. struct dvb_usb_device *d = priv;
  230. return gp8psk_bcm4500_reload(d);
  231. }
  232. const struct gp8psk_fe_ops gp8psk_fe_ops = {
  233. .in = gp8psk_fe_in,
  234. .out = gp8psk_fe_out,
  235. .reload = gp8psk_fe_reload,
  236. };
  237. static int gp8psk_frontend_attach(struct dvb_usb_adapter *adap)
  238. {
  239. struct dvb_usb_device *d = adap->dev;
  240. int id = le16_to_cpu(d->udev->descriptor.idProduct);
  241. int is_rev1;
  242. is_rev1 = (id == USB_PID_GENPIX_8PSK_REV_1_WARM) ? true : false;
  243. adap->fe_adap[0].fe = dvb_attach(gp8psk_fe_attach,
  244. &gp8psk_fe_ops, d, is_rev1);
  245. return 0;
  246. }
  247. static struct dvb_usb_device_properties gp8psk_properties;
  248. static int gp8psk_usb_probe(struct usb_interface *intf,
  249. const struct usb_device_id *id)
  250. {
  251. int ret;
  252. struct usb_device *udev = interface_to_usbdev(intf);
  253. ret = dvb_usb_device_init(intf, &gp8psk_properties,
  254. THIS_MODULE, NULL, adapter_nr);
  255. if (ret == 0) {
  256. info("found Genpix USB device pID = %x (hex)",
  257. le16_to_cpu(udev->descriptor.idProduct));
  258. }
  259. return ret;
  260. }
  261. static struct usb_device_id gp8psk_usb_table [] = {
  262. { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_8PSK_REV_1_COLD) },
  263. { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_8PSK_REV_1_WARM) },
  264. { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_8PSK_REV_2) },
  265. { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_SKYWALKER_1) },
  266. { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_SKYWALKER_2) },
  267. /* { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_SKYWALKER_CW3K) }, */
  268. { 0 },
  269. };
  270. MODULE_DEVICE_TABLE(usb, gp8psk_usb_table);
  271. static struct dvb_usb_device_properties gp8psk_properties = {
  272. .usb_ctrl = CYPRESS_FX2,
  273. .firmware = "/*(DEBLOBBED)*/",
  274. .size_of_priv = sizeof(struct gp8psk_state),
  275. .num_adapters = 1,
  276. .adapter = {
  277. {
  278. .num_frontends = 1,
  279. .fe = {{
  280. .streaming_ctrl = gp8psk_streaming_ctrl,
  281. .frontend_attach = gp8psk_frontend_attach,
  282. /* parameter for the MPEG2-data transfer */
  283. .stream = {
  284. .type = USB_BULK,
  285. .count = 7,
  286. .endpoint = 0x82,
  287. .u = {
  288. .bulk = {
  289. .buffersize = 8192,
  290. }
  291. }
  292. },
  293. }},
  294. }
  295. },
  296. .power_ctrl = gp8psk_power_ctrl,
  297. .generic_bulk_ctrl_endpoint = 0x01,
  298. .num_device_descs = 4,
  299. .devices = {
  300. { .name = "Genpix 8PSK-to-USB2 Rev.1 DVB-S receiver",
  301. .cold_ids = { &gp8psk_usb_table[0], NULL },
  302. .warm_ids = { &gp8psk_usb_table[1], NULL },
  303. },
  304. { .name = "Genpix 8PSK-to-USB2 Rev.2 DVB-S receiver",
  305. .cold_ids = { NULL },
  306. .warm_ids = { &gp8psk_usb_table[2], NULL },
  307. },
  308. { .name = "Genpix SkyWalker-1 DVB-S receiver",
  309. .cold_ids = { NULL },
  310. .warm_ids = { &gp8psk_usb_table[3], NULL },
  311. },
  312. { .name = "Genpix SkyWalker-2 DVB-S receiver",
  313. .cold_ids = { NULL },
  314. .warm_ids = { &gp8psk_usb_table[4], NULL },
  315. },
  316. { NULL },
  317. }
  318. };
  319. /* usb specific object needed to register this driver with the usb subsystem */
  320. static struct usb_driver gp8psk_usb_driver = {
  321. .name = "dvb_usb_gp8psk",
  322. .probe = gp8psk_usb_probe,
  323. .disconnect = dvb_usb_device_exit,
  324. .id_table = gp8psk_usb_table,
  325. };
  326. module_usb_driver(gp8psk_usb_driver);
  327. MODULE_AUTHOR("Alan Nisota <alannisota@gamil.com>");
  328. MODULE_DESCRIPTION("Driver for Genpix DVB-S");
  329. MODULE_VERSION("1.1");
  330. MODULE_LICENSE("GPL");