dvb-usb-firmware.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* dvb-usb-firmware.c is part of the DVB USB library.
  2. *
  3. * Copyright (C) 2004-5 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 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. static int usb_cypress_load_firmware(struct usb_device *udev, const struct firmware *fw, int type)
  32. {
  33. struct hexline hx;
  34. u8 reset;
  35. int ret,pos=0;
  36. /* stop the CPU */
  37. reset = 1;
  38. if ((ret = usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1)) != 1)
  39. err("could not stop the USB controller CPU.");
  40. while ((ret = dvb_usb_get_hexline(fw,&hx,&pos)) > 0) {
  41. deb_fw("writing to address 0x%04x (buffer: 0x%02x %02x)\n",hx.addr,hx.len,hx.chk);
  42. ret = usb_cypress_writemem(udev,hx.addr,hx.data,hx.len);
  43. if (ret != hx.len) {
  44. err("error while transferring firmware "
  45. "(transferred size: %d, block size: %d)",
  46. ret,hx.len);
  47. ret = -EINVAL;
  48. break;
  49. }
  50. }
  51. if (ret < 0) {
  52. err("firmware download failed at %d with %d",pos,ret);
  53. return ret;
  54. }
  55. if (ret == 0) {
  56. /* restart the CPU */
  57. reset = 0;
  58. if (ret || usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1) != 1) {
  59. err("could not restart the USB controller CPU.");
  60. ret = -EINVAL;
  61. }
  62. } else
  63. ret = -EIO;
  64. return ret;
  65. }
  66. /*
  67. * DViCO bluebird firmware needs the "warm" product ID to be patched into the
  68. * firmware file before download.
  69. */
  70. #define BLUEBIRD_01_ID_OFFSET 6638
  71. static int dvb_usb_patch_dvico_firmware(struct usb_device *udev, const struct firmware *fw)
  72. {
  73. if (fw->size < BLUEBIRD_01_ID_OFFSET + 4)
  74. return -EINVAL;
  75. if (fw->data[BLUEBIRD_01_ID_OFFSET] == (USB_VID_DVICO & 0xff) &&
  76. fw->data[BLUEBIRD_01_ID_OFFSET + 1] == USB_VID_DVICO >> 8) {
  77. fw->data[BLUEBIRD_01_ID_OFFSET + 2] = udev->descriptor.idProduct + 1;
  78. fw->data[BLUEBIRD_01_ID_OFFSET + 3] = udev->descriptor.idProduct >> 8;
  79. return 0;
  80. }
  81. return -EINVAL;
  82. }
  83. int dvb_usb_download_firmware(struct usb_device *udev, struct dvb_usb_properties *props)
  84. {
  85. int ret;
  86. const struct firmware *fw = NULL;
  87. if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) {
  88. err("did not find the firmware file. (%s) "
  89. "Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)",
  90. props->firmware,ret);
  91. return ret;
  92. }
  93. info("downloading firmware from file '%s'",props->firmware);
  94. if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_DVICO) {
  95. ret = dvb_usb_patch_dvico_firmware(udev, fw);
  96. if (ret != 0)
  97. warn("this firmware file not recognised");
  98. }
  99. switch (props->usb_ctrl) {
  100. case CYPRESS_AN2135:
  101. case CYPRESS_AN2235:
  102. case CYPRESS_FX2:
  103. ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl);
  104. break;
  105. case DEVICE_SPECIFIC:
  106. if (props->download_firmware)
  107. ret = props->download_firmware(udev,fw);
  108. else {
  109. err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one.");
  110. ret = -EINVAL;
  111. }
  112. break;
  113. default:
  114. ret = -EINVAL;
  115. break;
  116. }
  117. release_firmware(fw);
  118. return ret;
  119. }
  120. int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx, int *pos)
  121. {
  122. u8 *b = (u8 *) &fw->data[*pos];
  123. int data_offs = 4;
  124. if (*pos >= fw->size)
  125. return 0;
  126. memset(hx,0,sizeof(struct hexline));
  127. hx->len = b[0];
  128. if ((*pos + hx->len + 4) >= fw->size)
  129. return -EINVAL;
  130. hx->addr = le16_to_cpu( *((u16 *) &b[1]) );
  131. hx->type = b[3];
  132. if (hx->type == 0x04) {
  133. /* b[4] and b[5] are the Extended linear address record data field */
  134. hx->addr |= (b[4] << 24) | (b[5] << 16);
  135. /* hx->len -= 2;
  136. data_offs += 2; */
  137. }
  138. memcpy(hx->data,&b[data_offs],hx->len);
  139. hx->chk = b[hx->len + data_offs];
  140. *pos += hx->len + 5;
  141. return *pos;
  142. }
  143. EXPORT_SYMBOL(dvb_usb_get_hexline);