dvb-usb-firmware.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. static int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx,
  24. int *pos);
  25. /*
  26. * load a firmware packet to the device
  27. */
  28. static int usb_cypress_writemem(struct usb_device *udev,u16 addr,u8 *data, u8 len)
  29. {
  30. return usb_control_msg(udev, usb_sndctrlpipe(udev,0),
  31. 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000);
  32. }
  33. int usb_cypress_load_firmware(struct usb_device *udev, const struct firmware *fw, int type)
  34. {
  35. struct hexline hx;
  36. u8 reset;
  37. int ret,pos=0;
  38. /* stop the CPU */
  39. reset = 1;
  40. if ((ret = usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1)) != 1)
  41. err("could not stop the USB controller CPU.");
  42. while ((ret = dvb_usb_get_hexline(fw,&hx,&pos)) > 0) {
  43. deb_fw("writing to address 0x%04x (buffer: 0x%02x %02x)\n",hx.addr,hx.len,hx.chk);
  44. ret = usb_cypress_writemem(udev,hx.addr,hx.data,hx.len);
  45. if (ret != hx.len) {
  46. err("error while transferring firmware "
  47. "(transferred size: %d, block size: %d)",
  48. ret,hx.len);
  49. ret = -EINVAL;
  50. break;
  51. }
  52. }
  53. if (ret < 0) {
  54. err("firmware download failed at %d with %d",pos,ret);
  55. return ret;
  56. }
  57. if (ret == 0) {
  58. /* restart the CPU */
  59. reset = 0;
  60. if (ret || usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1) != 1) {
  61. err("could not restart the USB controller CPU.");
  62. ret = -EINVAL;
  63. }
  64. } else
  65. ret = -EIO;
  66. return ret;
  67. }
  68. EXPORT_SYMBOL(usb_cypress_load_firmware);
  69. int dvb_usb_download_firmware(struct usb_device *udev, struct dvb_usb_properties *props)
  70. {
  71. int ret;
  72. const struct firmware *fw = NULL;
  73. if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) {
  74. err("did not find the firmware file. (%s) "
  75. "Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)",
  76. props->firmware,ret);
  77. return ret;
  78. }
  79. info("downloading firmware from file '%s'",props->firmware);
  80. switch (props->usb_ctrl) {
  81. case CYPRESS_AN2135:
  82. case CYPRESS_AN2235:
  83. case CYPRESS_FX2:
  84. ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl);
  85. break;
  86. case DEVICE_SPECIFIC:
  87. if (props->download_firmware)
  88. ret = props->download_firmware(udev,fw);
  89. else {
  90. err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one.");
  91. ret = -EINVAL;
  92. }
  93. break;
  94. default:
  95. ret = -EINVAL;
  96. break;
  97. }
  98. release_firmware(fw);
  99. return ret;
  100. }
  101. static int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx,
  102. int *pos)
  103. {
  104. u8 *b = (u8 *) &fw->data[*pos];
  105. int data_offs = 4;
  106. if (*pos >= fw->size)
  107. return 0;
  108. memset(hx,0,sizeof(struct hexline));
  109. hx->len = b[0];
  110. if ((*pos + hx->len + 4) >= fw->size)
  111. return -EINVAL;
  112. hx->addr = le16_to_cpu( *((u16 *) &b[1]) );
  113. hx->type = b[3];
  114. if (hx->type == 0x04) {
  115. /* b[4] and b[5] are the Extended linear address record data field */
  116. hx->addr |= (b[4] << 24) | (b[5] << 16);
  117. /* hx->len -= 2;
  118. data_offs += 2; */
  119. }
  120. memcpy(hx->data,&b[data_offs],hx->len);
  121. hx->chk = b[hx->len + data_offs];
  122. *pos += hx->len + 5;
  123. return *pos;
  124. }