cypress_firmware.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. int ret, pos = 0;
  39. hx = kmalloc(sizeof(struct hexline), GFP_KERNEL);
  40. if (!hx) {
  41. dev_err(&udev->dev, "%s: kmalloc() failed\n", KBUILD_MODNAME);
  42. return -ENOMEM;
  43. }
  44. /* stop the CPU */
  45. hx->data[0] = 1;
  46. ret = usb_cypress_writemem(udev, cypress[type].cs_reg, hx->data, 1);
  47. if (ret != 1) {
  48. dev_err(&udev->dev, "%s: CPU stop failed=%d\n",
  49. KBUILD_MODNAME, ret);
  50. ret = -EIO;
  51. goto err_kfree;
  52. }
  53. /* write firmware to memory */
  54. for (;;) {
  55. ret = dvb_usbv2_get_hexline(fw, hx, &pos);
  56. if (ret < 0)
  57. goto err_kfree;
  58. else if (ret == 0)
  59. break;
  60. ret = usb_cypress_writemem(udev, hx->addr, hx->data, hx->len);
  61. if (ret < 0) {
  62. goto err_kfree;
  63. } else if (ret != hx->len) {
  64. dev_err(&udev->dev, "%s: error while transferring " \
  65. "firmware (transferred size=%d, " \
  66. "block size=%d)\n",
  67. KBUILD_MODNAME, ret, hx->len);
  68. ret = -EIO;
  69. goto err_kfree;
  70. }
  71. }
  72. /* start the CPU */
  73. hx->data[0] = 0;
  74. ret = usb_cypress_writemem(udev, cypress[type].cs_reg, hx->data, 1);
  75. if (ret != 1) {
  76. dev_err(&udev->dev, "%s: CPU start failed=%d\n",
  77. KBUILD_MODNAME, ret);
  78. ret = -EIO;
  79. goto err_kfree;
  80. }
  81. ret = 0;
  82. err_kfree:
  83. kfree(hx);
  84. return ret;
  85. }
  86. EXPORT_SYMBOL(usbv2_cypress_load_firmware);
  87. int dvb_usbv2_get_hexline(const struct firmware *fw, struct hexline *hx,
  88. int *pos)
  89. {
  90. u8 *b = (u8 *) &fw->data[*pos];
  91. int data_offs = 4;
  92. if (*pos >= fw->size)
  93. return 0;
  94. memset(hx, 0, sizeof(struct hexline));
  95. hx->len = b[0];
  96. if ((*pos + hx->len + 4) >= fw->size)
  97. return -EINVAL;
  98. hx->addr = b[1] | (b[2] << 8);
  99. hx->type = b[3];
  100. if (hx->type == 0x04) {
  101. /* b[4] and b[5] are the Extended linear address record data
  102. * field */
  103. hx->addr |= (b[4] << 24) | (b[5] << 16);
  104. }
  105. memcpy(hx->data, &b[data_offs], hx->len);
  106. hx->chk = b[hx->len + data_offs];
  107. *pos += hx->len + 5;
  108. return *pos;
  109. }
  110. EXPORT_SYMBOL(dvb_usbv2_get_hexline);
  111. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  112. MODULE_DESCRIPTION("Cypress firmware download");
  113. MODULE_LICENSE("GPL");