gp8psk.c 9.9 KB

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