cypress_firmware.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  30. 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000);
  31. }
  32. int usbv2_cypress_load_firmware(struct usb_device *udev,
  33. const struct firmware *fw, int type)
  34. {
  35. struct hexline hx;
  36. u8 reset;
  37. int ret, pos = 0;
  38. /* stop the CPU */
  39. reset = 1;
  40. ret = usb_cypress_writemem(udev, cypress[type].cs_reg, &reset, 1);
  41. if (ret != 1)
  42. pr_err("%s: could not stop the USB controller CPU",
  43. KBUILD_MODNAME);
  44. while ((ret = dvb_usbv2_get_hexline(fw, &hx, &pos)) > 0) {
  45. pr_debug("%s: writing to address %04x (buffer: %02x %02x)\n",
  46. __func__, hx.addr, hx.len, hx.chk);
  47. ret = usb_cypress_writemem(udev, hx.addr, hx.data, hx.len);
  48. if (ret != hx.len) {
  49. pr_err("%s: error while transferring firmware " \
  50. "(transferred size=%d, block size=%d)",
  51. KBUILD_MODNAME, ret, hx.len);
  52. ret = -EINVAL;
  53. break;
  54. }
  55. }
  56. if (ret < 0) {
  57. pr_err("%s: firmware download failed at %d with %d",
  58. KBUILD_MODNAME, pos, ret);
  59. return ret;
  60. }
  61. if (ret == 0) {
  62. /* restart the CPU */
  63. reset = 0;
  64. if (ret || usb_cypress_writemem(
  65. udev, cypress[type].cs_reg, &reset, 1) != 1) {
  66. pr_err("%s: could not restart the USB controller CPU",
  67. KBUILD_MODNAME);
  68. ret = -EINVAL;
  69. }
  70. } else
  71. ret = -EIO;
  72. return ret;
  73. }
  74. EXPORT_SYMBOL(usbv2_cypress_load_firmware);
  75. int dvb_usbv2_get_hexline(const struct firmware *fw, struct hexline *hx,
  76. int *pos)
  77. {
  78. u8 *b = (u8 *) &fw->data[*pos];
  79. int data_offs = 4;
  80. if (*pos >= fw->size)
  81. return 0;
  82. memset(hx, 0, sizeof(struct hexline));
  83. hx->len = b[0];
  84. if ((*pos + hx->len + 4) >= fw->size)
  85. return -EINVAL;
  86. hx->addr = b[1] | (b[2] << 8);
  87. hx->type = b[3];
  88. if (hx->type == 0x04) {
  89. /* b[4] and b[5] are the Extended linear address record data
  90. * field */
  91. hx->addr |= (b[4] << 24) | (b[5] << 16);
  92. /*
  93. hx->len -= 2;
  94. data_offs += 2;
  95. */
  96. }
  97. memcpy(hx->data, &b[data_offs], hx->len);
  98. hx->chk = b[hx->len + data_offs];
  99. *pos += hx->len + 5;
  100. return *pos;
  101. }
  102. EXPORT_SYMBOL(dvb_usbv2_get_hexline);
  103. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  104. MODULE_DESCRIPTION("Cypress firmware download");
  105. MODULE_LICENSE("GPL");