dvb-usb-firmware.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. ret = request_firmware(&fw, props->firmware, &udev->dev);
  79. if (ret)
  80. return ret;
  81. switch (props->usb_ctrl) {
  82. case CYPRESS_AN2135:
  83. case CYPRESS_AN2235:
  84. case CYPRESS_FX2:
  85. ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl);
  86. break;
  87. case DEVICE_SPECIFIC:
  88. if (props->download_firmware)
  89. ret = props->download_firmware(udev,fw);
  90. else {
  91. err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one.");
  92. ret = -EINVAL;
  93. }
  94. break;
  95. default:
  96. ret = -EINVAL;
  97. break;
  98. }
  99. release_firmware(fw);
  100. return ret;
  101. }
  102. int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx,
  103. int *pos)
  104. {
  105. u8 *b = (u8 *) &fw->data[*pos];
  106. int data_offs = 4;
  107. if (*pos >= fw->size)
  108. return 0;
  109. memset(hx,0,sizeof(struct hexline));
  110. hx->len = b[0];
  111. if ((*pos + hx->len + 4) >= fw->size)
  112. return -EINVAL;
  113. hx->addr = b[1] | (b[2] << 8);
  114. hx->type = b[3];
  115. if (hx->type == 0x04) {
  116. /* b[4] and b[5] are the Extended linear address record data field */
  117. hx->addr |= (b[4] << 24) | (b[5] << 16);
  118. /* hx->len -= 2;
  119. data_offs += 2; */
  120. }
  121. memcpy(hx->data,&b[data_offs],hx->len);
  122. hx->chk = b[hx->len + data_offs];
  123. *pos += hx->len + 5;
  124. return *pos;
  125. }
  126. EXPORT_SYMBOL(dvb_usb_get_hexline);