gp8psk.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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[] = "dvb-usb-gp8psk-02.fw";
  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 = request_firmware(&fw, bcm4500_firmware,
  113. &d->udev->dev)) != 0) {
  114. err("did not find the bcm4500 firmware file. (%s) Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)",
  115. bcm4500_firmware,ret);
  116. return ret;
  117. }
  118. ret = -EINVAL;
  119. if (gp8psk_usb_out_op(d, LOAD_BCM4500,1,0,NULL, 0))
  120. goto out_rel_fw;
  121. info("downloading bcm4500 firmware from file '%s'",bcm4500_firmware);
  122. ptr = fw->data;
  123. buf = kmalloc(64, GFP_KERNEL | GFP_DMA);
  124. if (!buf) {
  125. ret = -ENOMEM;
  126. goto out_rel_fw;
  127. }
  128. while (ptr[0] != 0xff) {
  129. u16 buflen = ptr[0] + 4;
  130. if (ptr + buflen >= fw->data + fw->size) {
  131. err("failed to load bcm4500 firmware.");
  132. goto out_free;
  133. }
  134. if (buflen > 64) {
  135. err("firmware chunk size bigger than 64 bytes.");
  136. goto out_free;
  137. }
  138. memcpy(buf, ptr, buflen);
  139. if (dvb_usb_generic_write(d, buf, buflen)) {
  140. err("failed to load bcm4500 firmware.");
  141. goto out_free;
  142. }
  143. ptr += buflen;
  144. }
  145. ret = 0;
  146. out_free:
  147. kfree(buf);
  148. out_rel_fw:
  149. release_firmware(fw);
  150. return ret;
  151. }
  152. static int gp8psk_power_ctrl(struct dvb_usb_device *d, int onoff)
  153. {
  154. u8 status = 0, buf;
  155. int gp_product_id = le16_to_cpu(d->udev->descriptor.idProduct);
  156. if (onoff) {
  157. gp8psk_usb_in_op(d, GET_8PSK_CONFIG,0,0,&status,1);
  158. if (! (status & bm8pskStarted)) { /* started */
  159. if(gp_product_id == USB_PID_GENPIX_SKYWALKER_CW3K)
  160. gp8psk_usb_out_op(d, CW3K_INIT, 1, 0, NULL, 0);
  161. if (gp8psk_usb_in_op(d, BOOT_8PSK, 1, 0, &buf, 1))
  162. return -EINVAL;
  163. gp8psk_info(d);
  164. }
  165. if (gp_product_id == USB_PID_GENPIX_8PSK_REV_1_WARM)
  166. if (! (status & bm8pskFW_Loaded)) /* BCM4500 firmware loaded */
  167. if(gp8psk_load_bcm4500fw(d))
  168. return -EINVAL;
  169. if (! (status & bmIntersilOn)) /* LNB Power */
  170. if (gp8psk_usb_in_op(d, START_INTERSIL, 1, 0,
  171. &buf, 1))
  172. return -EINVAL;
  173. /* Set DVB mode to 1 */
  174. if (gp_product_id == USB_PID_GENPIX_8PSK_REV_1_WARM)
  175. if (gp8psk_usb_out_op(d, SET_DVB_MODE, 1, 0, NULL, 0))
  176. return -EINVAL;
  177. /* Abort possible TS (if previous tune crashed) */
  178. if (gp8psk_usb_out_op(d, ARM_TRANSFER, 0, 0, NULL, 0))
  179. return -EINVAL;
  180. } else {
  181. /* Turn off LNB power */
  182. if (gp8psk_usb_in_op(d, START_INTERSIL, 0, 0, &buf, 1))
  183. return -EINVAL;
  184. /* Turn off 8psk power */
  185. if (gp8psk_usb_in_op(d, BOOT_8PSK, 0, 0, &buf, 1))
  186. return -EINVAL;
  187. if(gp_product_id == USB_PID_GENPIX_SKYWALKER_CW3K)
  188. gp8psk_usb_out_op(d, CW3K_INIT, 0, 0, NULL, 0);
  189. }
  190. return 0;
  191. }
  192. static int gp8psk_bcm4500_reload(struct dvb_usb_device *d)
  193. {
  194. u8 buf;
  195. int gp_product_id = le16_to_cpu(d->udev->descriptor.idProduct);
  196. deb_xfer("reloading firmware\n");
  197. /* Turn off 8psk power */
  198. if (gp8psk_usb_in_op(d, BOOT_8PSK, 0, 0, &buf, 1))
  199. return -EINVAL;
  200. /* Turn On 8psk power */
  201. if (gp8psk_usb_in_op(d, BOOT_8PSK, 1, 0, &buf, 1))
  202. return -EINVAL;
  203. /* load BCM4500 firmware */
  204. if (gp_product_id == USB_PID_GENPIX_8PSK_REV_1_WARM)
  205. if (gp8psk_load_bcm4500fw(d))
  206. return -EINVAL;
  207. return 0;
  208. }
  209. static int gp8psk_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  210. {
  211. return gp8psk_usb_out_op(adap->dev, ARM_TRANSFER, onoff, 0 , NULL, 0);
  212. }
  213. /* Callbacks for gp8psk-fe.c */
  214. static int gp8psk_fe_in(void *priv, u8 req, u16 value,
  215. u16 index, u8 *b, int blen)
  216. {
  217. struct dvb_usb_device *d = priv;
  218. return gp8psk_usb_in_op(d, req, value, index, b, blen);
  219. }
  220. static int gp8psk_fe_out(void *priv, u8 req, u16 value,
  221. u16 index, u8 *b, int blen)
  222. {
  223. struct dvb_usb_device *d = priv;
  224. return gp8psk_usb_out_op(d, req, value, index, b, blen);
  225. }
  226. static int gp8psk_fe_reload(void *priv)
  227. {
  228. struct dvb_usb_device *d = priv;
  229. return gp8psk_bcm4500_reload(d);
  230. }
  231. static const struct gp8psk_fe_ops gp8psk_fe_ops = {
  232. .in = gp8psk_fe_in,
  233. .out = gp8psk_fe_out,
  234. .reload = gp8psk_fe_reload,
  235. };
  236. static int gp8psk_frontend_attach(struct dvb_usb_adapter *adap)
  237. {
  238. struct dvb_usb_device *d = adap->dev;
  239. int id = le16_to_cpu(d->udev->descriptor.idProduct);
  240. int is_rev1;
  241. is_rev1 = (id == USB_PID_GENPIX_8PSK_REV_1_WARM) ? true : false;
  242. adap->fe_adap[0].fe = dvb_attach(gp8psk_fe_attach,
  243. &gp8psk_fe_ops, d, is_rev1);
  244. return 0;
  245. }
  246. static struct dvb_usb_device_properties gp8psk_properties;
  247. static int gp8psk_usb_probe(struct usb_interface *intf,
  248. const struct usb_device_id *id)
  249. {
  250. int ret;
  251. struct usb_device *udev = interface_to_usbdev(intf);
  252. ret = dvb_usb_device_init(intf, &gp8psk_properties,
  253. THIS_MODULE, NULL, adapter_nr);
  254. if (ret == 0) {
  255. info("found Genpix USB device pID = %x (hex)",
  256. le16_to_cpu(udev->descriptor.idProduct));
  257. }
  258. return ret;
  259. }
  260. static struct usb_device_id gp8psk_usb_table [] = {
  261. { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_8PSK_REV_1_COLD) },
  262. { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_8PSK_REV_1_WARM) },
  263. { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_8PSK_REV_2) },
  264. { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_SKYWALKER_1) },
  265. { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_SKYWALKER_2) },
  266. /* { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_SKYWALKER_CW3K) }, */
  267. { 0 },
  268. };
  269. MODULE_DEVICE_TABLE(usb, gp8psk_usb_table);
  270. static struct dvb_usb_device_properties gp8psk_properties = {
  271. .usb_ctrl = CYPRESS_FX2,
  272. .firmware = "dvb-usb-gp8psk-01.fw",
  273. .size_of_priv = sizeof(struct gp8psk_state),
  274. .num_adapters = 1,
  275. .adapter = {
  276. {
  277. .num_frontends = 1,
  278. .fe = {{
  279. .streaming_ctrl = gp8psk_streaming_ctrl,
  280. .frontend_attach = gp8psk_frontend_attach,
  281. /* parameter for the MPEG2-data transfer */
  282. .stream = {
  283. .type = USB_BULK,
  284. .count = 7,
  285. .endpoint = 0x82,
  286. .u = {
  287. .bulk = {
  288. .buffersize = 8192,
  289. }
  290. }
  291. },
  292. }},
  293. }
  294. },
  295. .power_ctrl = gp8psk_power_ctrl,
  296. .generic_bulk_ctrl_endpoint = 0x01,
  297. .num_device_descs = 4,
  298. .devices = {
  299. { .name = "Genpix 8PSK-to-USB2 Rev.1 DVB-S receiver",
  300. .cold_ids = { &gp8psk_usb_table[0], NULL },
  301. .warm_ids = { &gp8psk_usb_table[1], NULL },
  302. },
  303. { .name = "Genpix 8PSK-to-USB2 Rev.2 DVB-S receiver",
  304. .cold_ids = { NULL },
  305. .warm_ids = { &gp8psk_usb_table[2], NULL },
  306. },
  307. { .name = "Genpix SkyWalker-1 DVB-S receiver",
  308. .cold_ids = { NULL },
  309. .warm_ids = { &gp8psk_usb_table[3], NULL },
  310. },
  311. { .name = "Genpix SkyWalker-2 DVB-S receiver",
  312. .cold_ids = { NULL },
  313. .warm_ids = { &gp8psk_usb_table[4], NULL },
  314. },
  315. { NULL },
  316. }
  317. };
  318. /* usb specific object needed to register this driver with the usb subsystem */
  319. static struct usb_driver gp8psk_usb_driver = {
  320. .name = "dvb_usb_gp8psk",
  321. .probe = gp8psk_usb_probe,
  322. .disconnect = dvb_usb_device_exit,
  323. .id_table = gp8psk_usb_table,
  324. };
  325. module_usb_driver(gp8psk_usb_driver);
  326. MODULE_AUTHOR("Alan Nisota <alannisota@gamil.com>");
  327. MODULE_DESCRIPTION("Driver for Genpix DVB-S");
  328. MODULE_VERSION("1.1");
  329. MODULE_LICENSE("GPL");