0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include <linux/usb.h>
0015 #include <linux/firmware.h>
0016 #include "cypress_firmware.h"
0017
0018 struct usb_cypress_controller {
0019 u8 id;
0020 const char *name;
0021 u16 cs_reg;
0022
0023 };
0024
0025 static const struct usb_cypress_controller cypress[] = {
0026 { .id = CYPRESS_AN2135, .name = "Cypress AN2135", .cs_reg = 0x7f92 },
0027 { .id = CYPRESS_AN2235, .name = "Cypress AN2235", .cs_reg = 0x7f92 },
0028 { .id = CYPRESS_FX2, .name = "Cypress FX2", .cs_reg = 0xe600 },
0029 };
0030
0031
0032
0033
0034 static int usb_cypress_writemem(struct usb_device *udev, u16 addr, u8 *data,
0035 u8 len)
0036 {
0037 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
0038 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000);
0039 }
0040
0041 static int cypress_get_hexline(const struct firmware *fw,
0042 struct hexline *hx, int *pos)
0043 {
0044 u8 *b = (u8 *) &fw->data[*pos];
0045 int data_offs = 4;
0046
0047 if (*pos >= fw->size)
0048 return 0;
0049
0050 memset(hx, 0, sizeof(struct hexline));
0051 hx->len = b[0];
0052
0053 if ((*pos + hx->len + 4) >= fw->size)
0054 return -EINVAL;
0055
0056 hx->addr = b[1] | (b[2] << 8);
0057 hx->type = b[3];
0058
0059 if (hx->type == 0x04) {
0060
0061
0062 hx->addr |= (b[4] << 24) | (b[5] << 16);
0063 }
0064
0065 memcpy(hx->data, &b[data_offs], hx->len);
0066 hx->chk = b[hx->len + data_offs];
0067 *pos += hx->len + 5;
0068
0069 return *pos;
0070 }
0071
0072 int cypress_load_firmware(struct usb_device *udev,
0073 const struct firmware *fw, int type)
0074 {
0075 struct hexline *hx;
0076 int ret, pos = 0;
0077
0078 hx = kmalloc(sizeof(*hx), GFP_KERNEL);
0079 if (!hx)
0080 return -ENOMEM;
0081
0082
0083 hx->data[0] = 1;
0084 ret = usb_cypress_writemem(udev, cypress[type].cs_reg, hx->data, 1);
0085 if (ret != 1) {
0086 dev_err(&udev->dev, "%s: CPU stop failed=%d\n",
0087 KBUILD_MODNAME, ret);
0088 ret = -EIO;
0089 goto err_kfree;
0090 }
0091
0092
0093 for (;;) {
0094 ret = cypress_get_hexline(fw, hx, &pos);
0095 if (ret < 0)
0096 goto err_kfree;
0097 else if (ret == 0)
0098 break;
0099
0100 ret = usb_cypress_writemem(udev, hx->addr, hx->data, hx->len);
0101 if (ret < 0) {
0102 goto err_kfree;
0103 } else if (ret != hx->len) {
0104 dev_err(&udev->dev,
0105 "%s: error while transferring firmware (transferred size=%d, block size=%d)\n",
0106 KBUILD_MODNAME, ret, hx->len);
0107 ret = -EIO;
0108 goto err_kfree;
0109 }
0110 }
0111
0112
0113 hx->data[0] = 0;
0114 ret = usb_cypress_writemem(udev, cypress[type].cs_reg, hx->data, 1);
0115 if (ret != 1) {
0116 dev_err(&udev->dev, "%s: CPU start failed=%d\n",
0117 KBUILD_MODNAME, ret);
0118 ret = -EIO;
0119 goto err_kfree;
0120 }
0121
0122 ret = 0;
0123 err_kfree:
0124 kfree(hx);
0125 return ret;
0126 }
0127 EXPORT_SYMBOL(cypress_load_firmware);
0128
0129 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
0130 MODULE_DESCRIPTION("Cypress firmware download");
0131 MODULE_LICENSE("GPL");