dvb-usb-firmware.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* dvb-usb-firmware.c is part of the DVB USB library.
  2. *
  3. * Copyright (C) 2004-6 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. 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. EXPORT_SYMBOL(usb_cypress_load_firmware);
  67. int dvb_usb_download_firmware(struct usb_device *udev, struct dvb_usb_device_properties *props)
  68. {
  69. int ret;
  70. const struct firmware *fw = NULL;
  71. if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) {
  72. err("did not find the firmware file. (%s) "
  73. "Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)",
  74. props->firmware,ret);
  75. return ret;
  76. }
  77. info("downloading firmware from file '%s'",props->firmware);
  78. switch (props->usb_ctrl) {
  79. case CYPRESS_AN2135:
  80. case CYPRESS_AN2235:
  81. case CYPRESS_FX2:
  82. ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl);
  83. break;
  84. case DEVICE_SPECIFIC:
  85. if (props->download_firmware)
  86. ret = props->download_firmware(udev,fw);
  87. else {
  88. err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one.");
  89. ret = -EINVAL;
  90. }
  91. break;
  92. default:
  93. ret = -EINVAL;
  94. break;
  95. }
  96. release_firmware(fw);
  97. return ret;
  98. }
  99. int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx,
  100. int *pos)
  101. {
  102. u8 *b = (u8 *) &fw->data[*pos];
  103. int data_offs = 4;
  104. if (*pos >= fw->size)
  105. return 0;
  106. memset(hx,0,sizeof(struct hexline));
  107. hx->len = b[0];
  108. if ((*pos + hx->len + 4) >= fw->size)
  109. return -EINVAL;
  110. hx->addr = b[1] | (b[2] << 8);
  111. hx->type = b[3];
  112. if (hx->type == 0x04) {
  113. /* b[4] and b[5] are the Extended linear address record data field */
  114. hx->addr |= (b[4] << 24) | (b[5] << 16);
  115. /* hx->len -= 2;
  116. data_offs += 2; */
  117. }
  118. memcpy(hx->data,&b[data_offs],hx->len);
  119. hx->chk = b[hx->len + data_offs];
  120. *pos += hx->len + 5;
  121. return *pos;
  122. }
  123. EXPORT_SYMBOL(dvb_usb_get_hexline);