dvb-usb-firmware.c 4.0 KB

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