2 * EZ-USB specific functions used by some of the USB to Serial drivers.
4 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/usb.h>
15 #include <linux/firmware.h>
16 #include <linux/ihex.h>
17 #include <linux/usb/ezusb.h>
19 struct ezusb_fx_type
{
20 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
21 unsigned short cpucs_reg
;
22 unsigned short max_internal_adress
;
25 static const struct ezusb_fx_type ezusb_fx1
= {
27 .max_internal_adress
= 0x1B3F,
30 /* Commands for writing to memory */
31 #define WRITE_INT_RAM 0xA0
32 #define WRITE_EXT_RAM 0xA3
34 static int ezusb_writememory(struct usb_device
*dev
, int address
,
35 unsigned char *data
, int length
, __u8 request
)
38 unsigned char *transfer_buffer
;
43 transfer_buffer
= kmemdup(data
, length
, GFP_KERNEL
);
44 if (!transfer_buffer
) {
45 dev_err(&dev
->dev
, "%s - kmalloc(%d) failed.\n",
49 result
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), request
,
50 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
51 address
, 0, transfer_buffer
, length
, 3000);
53 kfree(transfer_buffer
);
57 static int ezusb_set_reset(struct usb_device
*dev
, unsigned short cpucs_reg
,
58 unsigned char reset_bit
)
60 int response
= ezusb_writememory(dev
, cpucs_reg
, &reset_bit
, 1, WRITE_INT_RAM
);
62 dev_err(&dev
->dev
, "%s-%d failed: %d\n",
63 __func__
, reset_bit
, response
);
67 int ezusb_fx1_set_reset(struct usb_device
*dev
, unsigned char reset_bit
)
69 return ezusb_set_reset(dev
, ezusb_fx1
.cpucs_reg
, reset_bit
);
71 EXPORT_SYMBOL_GPL(ezusb_fx1_set_reset
);
73 static int ezusb_ihex_firmware_download(struct usb_device
*dev
,
74 struct ezusb_fx_type fx
,
75 const char *firmware_path
)
78 const struct firmware
*firmware
= NULL
;
79 const struct ihex_binrec
*record
;
81 if (request_ihex_firmware(&firmware
, firmware_path
,
84 "%s - request \"%s\" failed\n",
85 __func__
, firmware_path
);
89 ret
= ezusb_set_reset(dev
, fx
.cpucs_reg
, 0);
93 record
= (const struct ihex_binrec
*)firmware
->data
;
94 for (; record
; record
= ihex_next_binrec(record
)) {
95 if (be32_to_cpu(record
->addr
) > fx
.max_internal_adress
) {
96 ret
= ezusb_writememory(dev
, be32_to_cpu(record
->addr
),
97 (unsigned char *)record
->data
,
98 be16_to_cpu(record
->len
), WRITE_EXT_RAM
);
100 dev_err(&dev
->dev
, "%s - ezusb_writememory "
101 "failed writing internal memory "
102 "(%d %04X %p %d)\n", __func__
, ret
,
103 be32_to_cpu(record
->addr
), record
->data
,
104 be16_to_cpu(record
->len
));
110 ret
= ezusb_set_reset(dev
, fx
.cpucs_reg
, 1);
113 record
= (const struct ihex_binrec
*)firmware
->data
;
114 for (; record
; record
= ihex_next_binrec(record
)) {
115 if (be32_to_cpu(record
->addr
) <= fx
.max_internal_adress
) {
116 ret
= ezusb_writememory(dev
, be32_to_cpu(record
->addr
),
117 (unsigned char *)record
->data
,
118 be16_to_cpu(record
->len
), WRITE_INT_RAM
);
120 dev_err(&dev
->dev
, "%s - ezusb_writememory "
121 "failed writing external memory "
122 "(%d %04X %p %d)\n", __func__
, ret
,
123 be32_to_cpu(record
->addr
), record
->data
,
124 be16_to_cpu(record
->len
));
129 ret
= ezusb_set_reset(dev
, fx
.cpucs_reg
, 0);
131 release_firmware(firmware
);
135 int ezusb_fx1_ihex_firmware_download(struct usb_device
*dev
,
136 const char *firmware_path
)
138 return ezusb_ihex_firmware_download(dev
, ezusb_fx1
, firmware_path
);
140 EXPORT_SYMBOL_GPL(ezusb_fx1_ihex_firmware_download
);
144 * Once someone one needs these fx2 functions, uncomment them
145 * and add them to ezusb.h and all should be good.
147 static struct ezusb_fx_type ezusb_fx2
= {
149 .max_internal_adress
= 0x3FFF,
152 int ezusb_fx2_set_reset(struct usb_device
*dev
, unsigned char reset_bit
)
154 return ezusb_set_reset(dev
, ezusb_fx2
.cpucs_reg
, reset_bit
);
156 EXPORT_SYMBOL_GPL(ezusb_fx2_set_reset
);
158 int ezusb_fx2_ihex_firmware_download(struct usb_device
*dev
,
159 const char *firmware_path
)
161 return ezusb_ihex_firmware_download(dev
, ezusb_fx2
, firmware_path
);
163 EXPORT_SYMBOL_GPL(ezusb_fx2_ihex_firmware_download
);
166 MODULE_LICENSE("GPL");