cypress_firmware.c 3.1 KB

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