dvb-usb-firmware.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/firmware.h>
  12. #include <linux/usb.h>
  13. struct usb_cypress_controller {
  14. int id;
  15. const char *name; /* name of the usb controller */
  16. u16 cpu_cs_register; /* needs to be restarted, when the firmware has been downloaded. */
  17. };
  18. static struct usb_cypress_controller cypress[] = {
  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, 5*HZ);
  30. }
  31. int usb_cypress_load_firmware(struct usb_device *udev, const char *filename, int type)
  32. {
  33. const struct firmware *fw = NULL;
  34. u16 addr;
  35. u8 *b,*p;
  36. int ret = 0,i;
  37. if ((ret = request_firmware(&fw, filename, &udev->dev)) != 0) {
  38. err("did not find the firmware file. (%s) "
  39. "Please see linux/Documentation/dvb/ for more details on firmware-problems.",
  40. filename);
  41. return ret;
  42. }
  43. info("downloading firmware from file '%s' to the '%s'",filename,cypress[type].name);
  44. p = kmalloc(fw->size,GFP_KERNEL);
  45. if (p != NULL) {
  46. u8 reset;
  47. /*
  48. * you cannot use the fw->data as buffer for
  49. * usb_control_msg, a new buffer has to be
  50. * created
  51. */
  52. memcpy(p,fw->data,fw->size);
  53. /* stop the CPU */
  54. reset = 1;
  55. if ((ret = usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1)) != 1)
  56. err("could not stop the USB controller CPU.");
  57. for(i = 0; p[i+3] == 0 && i < fw->size; ) {
  58. b = (u8 *) &p[i];
  59. addr = cpu_to_le16( *((u16 *) &b[1]) );
  60. deb_fw("writing to address 0x%04x (buffer: 0x%02x%02x)\n",addr,b[1],b[2]);
  61. ret = usb_cypress_writemem(udev,addr,&b[4],b[0]);
  62. if (ret != b[0]) {
  63. err("error while transferring firmware "
  64. "(transferred size: %d, block size: %d)",
  65. ret,b[0]);
  66. ret = -EINVAL;
  67. break;
  68. }
  69. i += 5 + b[0];
  70. }
  71. /* length in ret */
  72. if (ret > 0)
  73. ret = 0;
  74. /* restart the CPU */
  75. reset = 0;
  76. if (ret || usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1) != 1) {
  77. err("could not restart the USB controller CPU.");
  78. ret = -EINVAL;
  79. }
  80. kfree(p);
  81. } else {
  82. ret = -ENOMEM;
  83. }
  84. release_firmware(fw);
  85. return ret;
  86. }