treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / usb / misc / ezusb.c
blobf058d80297616889a38813d05d4ed478a5e060ef
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * EZ-USB specific functions used by some of the USB to Serial drivers.
5 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
6 */
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 #include <linux/firmware.h>
13 #include <linux/ihex.h>
14 #include <linux/usb/ezusb.h>
16 struct ezusb_fx_type {
17 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
18 unsigned short cpucs_reg;
19 unsigned short max_internal_adress;
22 static const struct ezusb_fx_type ezusb_fx1 = {
23 .cpucs_reg = 0x7F92,
24 .max_internal_adress = 0x1B3F,
27 /* Commands for writing to memory */
28 #define WRITE_INT_RAM 0xA0
29 #define WRITE_EXT_RAM 0xA3
31 static int ezusb_writememory(struct usb_device *dev, int address,
32 unsigned char *data, int length, __u8 request)
34 int result;
35 unsigned char *transfer_buffer;
37 if (!dev)
38 return -ENODEV;
40 transfer_buffer = kmemdup(data, length, GFP_KERNEL);
41 if (!transfer_buffer) {
42 dev_err(&dev->dev, "%s - kmalloc(%d) failed.\n",
43 __func__, length);
44 return -ENOMEM;
46 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
47 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
48 address, 0, transfer_buffer, length, 3000);
50 kfree(transfer_buffer);
51 return result;
54 static int ezusb_set_reset(struct usb_device *dev, unsigned short cpucs_reg,
55 unsigned char reset_bit)
57 int response = ezusb_writememory(dev, cpucs_reg, &reset_bit, 1, WRITE_INT_RAM);
58 if (response < 0)
59 dev_err(&dev->dev, "%s-%d failed: %d\n",
60 __func__, reset_bit, response);
61 return response;
64 int ezusb_fx1_set_reset(struct usb_device *dev, unsigned char reset_bit)
66 return ezusb_set_reset(dev, ezusb_fx1.cpucs_reg, reset_bit);
68 EXPORT_SYMBOL_GPL(ezusb_fx1_set_reset);
70 static int ezusb_ihex_firmware_download(struct usb_device *dev,
71 struct ezusb_fx_type fx,
72 const char *firmware_path)
74 int ret = -ENOENT;
75 const struct firmware *firmware = NULL;
76 const struct ihex_binrec *record;
78 if (request_ihex_firmware(&firmware, firmware_path,
79 &dev->dev)) {
80 dev_err(&dev->dev,
81 "%s - request \"%s\" failed\n",
82 __func__, firmware_path);
83 goto out;
86 ret = ezusb_set_reset(dev, fx.cpucs_reg, 0);
87 if (ret < 0)
88 goto out;
90 record = (const struct ihex_binrec *)firmware->data;
91 for (; record; record = ihex_next_binrec(record)) {
92 if (be32_to_cpu(record->addr) > fx.max_internal_adress) {
93 ret = ezusb_writememory(dev, be32_to_cpu(record->addr),
94 (unsigned char *)record->data,
95 be16_to_cpu(record->len), WRITE_EXT_RAM);
96 if (ret < 0) {
97 dev_err(&dev->dev, "%s - ezusb_writememory "
98 "failed writing internal memory "
99 "(%d %04X %p %d)\n", __func__, ret,
100 be32_to_cpu(record->addr), record->data,
101 be16_to_cpu(record->len));
102 goto out;
107 ret = ezusb_set_reset(dev, fx.cpucs_reg, 1);
108 if (ret < 0)
109 goto out;
110 record = (const struct ihex_binrec *)firmware->data;
111 for (; record; record = ihex_next_binrec(record)) {
112 if (be32_to_cpu(record->addr) <= fx.max_internal_adress) {
113 ret = ezusb_writememory(dev, be32_to_cpu(record->addr),
114 (unsigned char *)record->data,
115 be16_to_cpu(record->len), WRITE_INT_RAM);
116 if (ret < 0) {
117 dev_err(&dev->dev, "%s - ezusb_writememory "
118 "failed writing external memory "
119 "(%d %04X %p %d)\n", __func__, ret,
120 be32_to_cpu(record->addr), record->data,
121 be16_to_cpu(record->len));
122 goto out;
126 ret = ezusb_set_reset(dev, fx.cpucs_reg, 0);
127 out:
128 release_firmware(firmware);
129 return ret;
132 int ezusb_fx1_ihex_firmware_download(struct usb_device *dev,
133 const char *firmware_path)
135 return ezusb_ihex_firmware_download(dev, ezusb_fx1, firmware_path);
137 EXPORT_SYMBOL_GPL(ezusb_fx1_ihex_firmware_download);
139 #if 0
141 * Once someone one needs these fx2 functions, uncomment them
142 * and add them to ezusb.h and all should be good.
144 static struct ezusb_fx_type ezusb_fx2 = {
145 .cpucs_reg = 0xE600,
146 .max_internal_adress = 0x3FFF,
149 int ezusb_fx2_set_reset(struct usb_device *dev, unsigned char reset_bit)
151 return ezusb_set_reset(dev, ezusb_fx2.cpucs_reg, reset_bit);
153 EXPORT_SYMBOL_GPL(ezusb_fx2_set_reset);
155 int ezusb_fx2_ihex_firmware_download(struct usb_device *dev,
156 const char *firmware_path)
158 return ezusb_ihex_firmware_download(dev, ezusb_fx2, firmware_path);
160 EXPORT_SYMBOL_GPL(ezusb_fx2_ihex_firmware_download);
161 #endif
163 MODULE_LICENSE("GPL");