dvb-usb-firmware.c 4.0 KB

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