dvb-usb-firmware.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* dvb-usb-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 and 2 based devices.
  8. *
  9. * FIXME: This part does actually not belong to dvb-usb, but to the usb-subsystem.
  10. */
  11. #include "dvb-usb-common.h"
  12. #include <linux/usb.h>
  13. struct usb_cypress_controller {
  14. int id;
  15. const char *name; /* name of the usb controller */
  16. u16 cpu_cs_register; /* needs to be restarted, when the firmware has been downloaded. */
  17. };
  18. static struct usb_cypress_controller cypress[] = {
  19. { .id = DEVICE_SPECIFIC, .name = "Device specific", .cpu_cs_register = 0 },
  20. { .id = CYPRESS_AN2135, .name = "Cypress AN2135", .cpu_cs_register = 0x7f92 },
  21. { .id = CYPRESS_AN2235, .name = "Cypress AN2235", .cpu_cs_register = 0x7f92 },
  22. { .id = CYPRESS_FX2, .name = "Cypress FX2", .cpu_cs_register = 0xe600 },
  23. };
  24. /*
  25. * load a firmware packet to the device
  26. */
  27. static int usb_cypress_writemem(struct usb_device *udev,u16 addr,u8 *data, 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 usb_cypress_load_firmware(struct usb_device *udev, const struct firmware *fw, int type)
  33. {
  34. struct hexline *hx;
  35. u8 *buf;
  36. int ret, pos = 0;
  37. u16 cpu_cs_register = cypress[type].cpu_cs_register;
  38. buf = kmalloc(sizeof(*hx), GFP_KERNEL);
  39. if (!buf)
  40. return -ENOMEM;
  41. hx = (struct hexline *)buf;
  42. /* stop the CPU */
  43. buf[0] = 1;
  44. if (usb_cypress_writemem(udev, cpu_cs_register, buf, 1) != 1)
  45. err("could not stop the USB controller CPU.");
  46. while ((ret = dvb_usb_get_hexline(fw, hx, &pos)) > 0) {
  47. deb_fw("writing to address 0x%04x (buffer: 0x%02x %02x)\n", hx->addr, hx->len, hx->chk);
  48. ret = usb_cypress_writemem(udev, hx->addr, hx->data, hx->len);
  49. if (ret != hx->len) {
  50. err("error while transferring firmware (transferred size: %d, block size: %d)",
  51. ret, hx->len);
  52. ret = -EINVAL;
  53. break;
  54. }
  55. }
  56. if (ret < 0) {
  57. err("firmware download failed at %d with %d",pos,ret);
  58. kfree(buf);
  59. return ret;
  60. }
  61. if (ret == 0) {
  62. /* restart the CPU */
  63. buf[0] = 0;
  64. if (usb_cypress_writemem(udev, cpu_cs_register, buf, 1) != 1) {
  65. err("could not restart the USB controller CPU.");
  66. ret = -EINVAL;
  67. }
  68. } else
  69. ret = -EIO;
  70. kfree(buf);
  71. return ret;
  72. }
  73. EXPORT_SYMBOL(usb_cypress_load_firmware);
  74. int dvb_usb_download_firmware(struct usb_device *udev, struct dvb_usb_device_properties *props)
  75. {
  76. int ret;
  77. const struct firmware *fw = NULL;
  78. if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) {
  79. err("did not find the firmware file. (%s) Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)",
  80. props->firmware,ret);
  81. return ret;
  82. }
  83. info("downloading firmware from file '%s'",props->firmware);
  84. switch (props->usb_ctrl) {
  85. case CYPRESS_AN2135:
  86. case CYPRESS_AN2235:
  87. case CYPRESS_FX2:
  88. ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl);
  89. break;
  90. case DEVICE_SPECIFIC:
  91. if (props->download_firmware)
  92. ret = props->download_firmware(udev,fw);
  93. else {
  94. err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one.");
  95. ret = -EINVAL;
  96. }
  97. break;
  98. default:
  99. ret = -EINVAL;
  100. break;
  101. }
  102. release_firmware(fw);
  103. return ret;
  104. }
  105. int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx,
  106. int *pos)
  107. {
  108. u8 *b = (u8 *) &fw->data[*pos];
  109. int data_offs = 4;
  110. if (*pos >= fw->size)
  111. return 0;
  112. memset(hx,0,sizeof(struct hexline));
  113. hx->len = b[0];
  114. if ((*pos + hx->len + 4) >= fw->size)
  115. return -EINVAL;
  116. hx->addr = b[1] | (b[2] << 8);
  117. hx->type = b[3];
  118. if (hx->type == 0x04) {
  119. /* b[4] and b[5] are the Extended linear address record data field */
  120. hx->addr |= (b[4] << 24) | (b[5] << 16);
  121. /* hx->len -= 2;
  122. data_offs += 2; */
  123. }
  124. memcpy(hx->data,&b[data_offs],hx->len);
  125. hx->chk = b[hx->len + data_offs];
  126. *pos += hx->len + 5;
  127. return *pos;
  128. }
  129. EXPORT_SYMBOL(dvb_usb_get_hexline);