cypress_firmware.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* cypress_firmware.c is part of the DVB USB library.
  2. *
  3. * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
  4. * see dvb-usb-init.c for copyright information.
  5. *
  6. * This file contains functions for downloading the firmware to Cypress FX 1
  7. * and 2 based devices.
  8. *
  9. */
  10. #include "dvb_usb.h"
  11. #include "cypress_firmware.h"
  12. struct usb_cypress_controller {
  13. u8 id;
  14. const char *name; /* name of the usb controller */
  15. u16 cs_reg; /* needs to be restarted,
  16. * when the firmware has been downloaded */
  17. };
  18. static const struct usb_cypress_controller cypress[] = {
  19. { .id = CYPRESS_AN2135, .name = "Cypress AN2135", .cs_reg = 0x7f92 },
  20. { .id = CYPRESS_AN2235, .name = "Cypress AN2235", .cs_reg = 0x7f92 },
  21. { .id = CYPRESS_FX2, .name = "Cypress FX2", .cs_reg = 0xe600 },
  22. };
  23. /*
  24. * load a firmware packet to the device
  25. */
  26. static int usb_cypress_writemem(struct usb_device *udev, u16 addr, u8 *data,
  27. u8 len)
  28. {
  29. dvb_usb_dbg_usb_control_msg(udev,
  30. 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len);
  31. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  32. 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000);
  33. }
  34. int usbv2_cypress_load_firmware(struct usb_device *udev,
  35. const struct firmware *fw, int type)
  36. {
  37. struct hexline hx;
  38. u8 reset;
  39. int ret, pos = 0;
  40. /* stop the CPU */
  41. reset = 1;
  42. ret = usb_cypress_writemem(udev, cypress[type].cs_reg, &reset, 1);
  43. if (ret != 1)
  44. dev_err(&udev->dev,
  45. "%s: could not stop the USB controller CPU\n",
  46. KBUILD_MODNAME);
  47. while ((ret = dvb_usbv2_get_hexline(fw, &hx, &pos)) > 0) {
  48. ret = usb_cypress_writemem(udev, hx.addr, hx.data, hx.len);
  49. if (ret != hx.len) {
  50. dev_err(&udev->dev, "%s: error while transferring " \
  51. "firmware (transferred size=%d, " \
  52. "block size=%d)\n",
  53. KBUILD_MODNAME, ret, hx.len);
  54. ret = -EINVAL;
  55. break;
  56. }
  57. }
  58. if (ret < 0) {
  59. dev_err(&udev->dev,
  60. "%s: firmware download failed at %d with %d\n",
  61. KBUILD_MODNAME, pos, ret);
  62. return ret;
  63. }
  64. if (ret == 0) {
  65. /* restart the CPU */
  66. reset = 0;
  67. if (ret || usb_cypress_writemem(
  68. udev, cypress[type].cs_reg, &reset, 1) != 1) {
  69. dev_err(&udev->dev, "%s: could not restart the USB " \
  70. "controller CPU\n", KBUILD_MODNAME);
  71. ret = -EINVAL;
  72. }
  73. } else
  74. ret = -EIO;
  75. return ret;
  76. }
  77. EXPORT_SYMBOL(usbv2_cypress_load_firmware);
  78. int dvb_usbv2_get_hexline(const struct firmware *fw, struct hexline *hx,
  79. int *pos)
  80. {
  81. u8 *b = (u8 *) &fw->data[*pos];
  82. int data_offs = 4;
  83. if (*pos >= fw->size)
  84. return 0;
  85. memset(hx, 0, sizeof(struct hexline));
  86. hx->len = b[0];
  87. if ((*pos + hx->len + 4) >= fw->size)
  88. return -EINVAL;
  89. hx->addr = b[1] | (b[2] << 8);
  90. hx->type = b[3];
  91. if (hx->type == 0x04) {
  92. /* b[4] and b[5] are the Extended linear address record data
  93. * field */
  94. hx->addr |= (b[4] << 24) | (b[5] << 16);
  95. /*
  96. hx->len -= 2;
  97. data_offs += 2;
  98. */
  99. }
  100. memcpy(hx->data, &b[data_offs], hx->len);
  101. hx->chk = b[hx->len + data_offs];
  102. *pos += hx->len + 5;
  103. return *pos;
  104. }
  105. EXPORT_SYMBOL(dvb_usbv2_get_hexline);
  106. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  107. MODULE_DESCRIPTION("Cypress firmware download");
  108. MODULE_LICENSE("GPL");