cypress_firmware.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* cypress_firmware.c is part of the DVB USB library.
  2. *
  3. * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
  4. * see dvb-usb-init.c for copyright information.
  5. *
  6. * This file contains functions for downloading the firmware to Cypress FX 1
  7. * and 2 based devices.
  8. *
  9. */
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/usb.h>
  13. #include <linux/firmware.h>
  14. #include "cypress_firmware.h"
  15. struct usb_cypress_controller {
  16. u8 id;
  17. const char *name; /* name of the usb controller */
  18. u16 cs_reg; /* needs to be restarted,
  19. * when the firmware has been downloaded */
  20. };
  21. static const struct usb_cypress_controller cypress[] = {
  22. { .id = CYPRESS_AN2135, .name = "Cypress AN2135", .cs_reg = 0x7f92 },
  23. { .id = CYPRESS_AN2235, .name = "Cypress AN2235", .cs_reg = 0x7f92 },
  24. { .id = CYPRESS_FX2, .name = "Cypress FX2", .cs_reg = 0xe600 },
  25. };
  26. /*
  27. * load a firmware packet to the device
  28. */
  29. static int usb_cypress_writemem(struct usb_device *udev, u16 addr, u8 *data,
  30. u8 len)
  31. {
  32. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  33. 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000);
  34. }
  35. static int cypress_get_hexline(const struct firmware *fw,
  36. struct hexline *hx, int *pos)
  37. {
  38. u8 *b = (u8 *) &fw->data[*pos];
  39. int data_offs = 4;
  40. if (*pos >= fw->size)
  41. return 0;
  42. memset(hx, 0, sizeof(struct hexline));
  43. hx->len = b[0];
  44. if ((*pos + hx->len + 4) >= fw->size)
  45. return -EINVAL;
  46. hx->addr = b[1] | (b[2] << 8);
  47. hx->type = b[3];
  48. if (hx->type == 0x04) {
  49. /* b[4] and b[5] are the Extended linear address record data
  50. * field */
  51. hx->addr |= (b[4] << 24) | (b[5] << 16);
  52. }
  53. memcpy(hx->data, &b[data_offs], hx->len);
  54. hx->chk = b[hx->len + data_offs];
  55. *pos += hx->len + 5;
  56. return *pos;
  57. }
  58. int cypress_load_firmware(struct usb_device *udev,
  59. const struct firmware *fw, int type)
  60. {
  61. struct hexline *hx;
  62. int ret, pos = 0;
  63. hx = kmalloc(sizeof(*hx), GFP_KERNEL);
  64. if (!hx)
  65. return -ENOMEM;
  66. /* stop the CPU */
  67. hx->data[0] = 1;
  68. ret = usb_cypress_writemem(udev, cypress[type].cs_reg, hx->data, 1);
  69. if (ret != 1) {
  70. dev_err(&udev->dev, "%s: CPU stop failed=%d\n",
  71. KBUILD_MODNAME, ret);
  72. ret = -EIO;
  73. goto err_kfree;
  74. }
  75. /* write firmware to memory */
  76. for (;;) {
  77. ret = cypress_get_hexline(fw, hx, &pos);
  78. if (ret < 0)
  79. goto err_kfree;
  80. else if (ret == 0)
  81. break;
  82. ret = usb_cypress_writemem(udev, hx->addr, hx->data, hx->len);
  83. if (ret < 0) {
  84. goto err_kfree;
  85. } else if (ret != hx->len) {
  86. dev_err(&udev->dev,
  87. "%s: error while transferring firmware (transferred size=%d, block size=%d)\n",
  88. KBUILD_MODNAME, ret, hx->len);
  89. ret = -EIO;
  90. goto err_kfree;
  91. }
  92. }
  93. /* start the CPU */
  94. hx->data[0] = 0;
  95. ret = usb_cypress_writemem(udev, cypress[type].cs_reg, hx->data, 1);
  96. if (ret != 1) {
  97. dev_err(&udev->dev, "%s: CPU start failed=%d\n",
  98. KBUILD_MODNAME, ret);
  99. ret = -EIO;
  100. goto err_kfree;
  101. }
  102. ret = 0;
  103. err_kfree:
  104. kfree(hx);
  105. return ret;
  106. }
  107. EXPORT_SYMBOL(cypress_load_firmware);
  108. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  109. MODULE_DESCRIPTION("Cypress firmware download");
  110. MODULE_LICENSE("GPL");