• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2021 pureLiFi
4  */
5 
6 #include <linux/firmware.h>
7 #include <linux/bitrev.h>
8 
9 #include "mac.h"
10 #include "usb.h"
11 
send_vendor_request(struct usb_device * udev,int request,unsigned char * buffer,int buffer_size)12 static int send_vendor_request(struct usb_device *udev, int request,
13 			       unsigned char *buffer, int buffer_size)
14 {
15 	return usb_control_msg(udev,
16 			       usb_rcvctrlpipe(udev, 0),
17 			       request, 0xC0, 0, 0,
18 			       buffer, buffer_size, PLF_USB_TIMEOUT);
19 }
20 
send_vendor_command(struct usb_device * udev,int request,unsigned char * buffer,int buffer_size)21 static int send_vendor_command(struct usb_device *udev, int request,
22 			       unsigned char *buffer, int buffer_size)
23 {
24 	return usb_control_msg(udev,
25 			       usb_sndctrlpipe(udev, 0),
26 			       request, USB_TYPE_VENDOR /*0x40*/, 0, 0,
27 			       buffer, buffer_size, PLF_USB_TIMEOUT);
28 }
29 
plfxlc_download_fpga(struct usb_interface * intf)30 int plfxlc_download_fpga(struct usb_interface *intf)
31 {
32 	struct usb_device *udev = interface_to_usbdev(intf);
33 	unsigned char *fpga_dmabuff = NULL;
34 	const struct firmware *fw = NULL;
35 	int blk_tran_len = PLF_BULK_TLEN;
36 	unsigned char *fw_data;
37 	const char *fw_name;
38 	int r, actual_length;
39 	int fw_data_i = 0;
40 
41 	if ((le16_to_cpu(udev->descriptor.idVendor) ==
42 				PURELIFI_X_VENDOR_ID_0) &&
43 	    (le16_to_cpu(udev->descriptor.idProduct) ==
44 				PURELIFI_X_PRODUCT_ID_0)) {
45 		fw_name = "plfxlc/lifi-x.bin";
46 		dev_dbg(&intf->dev, "bin file for X selected\n");
47 
48 	} else if ((le16_to_cpu(udev->descriptor.idVendor)) ==
49 					PURELIFI_XC_VENDOR_ID_0 &&
50 		   (le16_to_cpu(udev->descriptor.idProduct) ==
51 					PURELIFI_XC_PRODUCT_ID_0)) {
52 		fw_name = "plfxlc/lifi-xc.bin";
53 		dev_dbg(&intf->dev, "bin file for XC selected\n");
54 
55 	} else {
56 		r = -EINVAL;
57 		goto error;
58 	}
59 
60 	r = request_firmware(&fw, fw_name, &intf->dev);
61 	if (r) {
62 		dev_err(&intf->dev, "request_firmware failed (%d)\n", r);
63 		goto error;
64 	}
65 	fpga_dmabuff = kmalloc(PLF_FPGA_STATUS_LEN, GFP_KERNEL);
66 
67 	if (!fpga_dmabuff) {
68 		r = -ENOMEM;
69 		goto error_free_fw;
70 	}
71 	send_vendor_request(udev, PLF_VNDR_FPGA_SET_REQ,
72 			    fpga_dmabuff, PLF_FPGA_STATUS_LEN);
73 
74 	send_vendor_command(udev, PLF_VNDR_FPGA_SET_CMD, NULL, 0);
75 
76 	if (fpga_dmabuff[0] != PLF_FPGA_MG) {
77 		dev_err(&intf->dev, "fpga_dmabuff[0] is wrong\n");
78 		r = -EINVAL;
79 		goto error_free_fw;
80 	}
81 
82 	for (fw_data_i = 0; fw_data_i < fw->size;) {
83 		int tbuf_idx;
84 
85 		if ((fw->size - fw_data_i) < blk_tran_len)
86 			blk_tran_len = fw->size - fw_data_i;
87 
88 		fw_data = kmemdup(&fw->data[fw_data_i], blk_tran_len,
89 				  GFP_KERNEL);
90 		if (!fw_data) {
91 			r = -ENOMEM;
92 			goto error_free_fw;
93 		}
94 
95 		for (tbuf_idx = 0; tbuf_idx < blk_tran_len; tbuf_idx++) {
96 			/* u8 bit reverse */
97 			fw_data[tbuf_idx] = bitrev8(fw_data[tbuf_idx]);
98 		}
99 		r = usb_bulk_msg(udev,
100 				 usb_sndbulkpipe(interface_to_usbdev(intf),
101 						 fpga_dmabuff[0] & 0xff),
102 				 fw_data,
103 				 blk_tran_len,
104 				 &actual_length,
105 				 2 * PLF_USB_TIMEOUT);
106 
107 		if (r)
108 			dev_err(&intf->dev, "Bulk msg failed (%d)\n", r);
109 
110 		kfree(fw_data);
111 		fw_data_i += blk_tran_len;
112 	}
113 
114 	kfree(fpga_dmabuff);
115 	fpga_dmabuff = kmalloc(PLF_FPGA_STATE_LEN, GFP_KERNEL);
116 	if (!fpga_dmabuff) {
117 		r = -ENOMEM;
118 		goto error_free_fw;
119 	}
120 	memset(fpga_dmabuff, 0xff, PLF_FPGA_STATE_LEN);
121 
122 	send_vendor_request(udev, PLF_VNDR_FPGA_STATE_REQ, fpga_dmabuff,
123 			    PLF_FPGA_STATE_LEN);
124 
125 	dev_dbg(&intf->dev, "%*ph\n", 8, fpga_dmabuff);
126 
127 	if (fpga_dmabuff[0] != 0) {
128 		r = -EINVAL;
129 		goto error_free_fw;
130 	}
131 
132 	send_vendor_command(udev, PLF_VNDR_FPGA_STATE_CMD, NULL, 0);
133 
134 	msleep(PLF_MSLEEP_TIME);
135 
136 error_free_fw:
137 	kfree(fpga_dmabuff);
138 	release_firmware(fw);
139 error:
140 	return r;
141 }
142 
plfxlc_download_xl_firmware(struct usb_interface * intf)143 int plfxlc_download_xl_firmware(struct usb_interface *intf)
144 {
145 	struct usb_device *udev = interface_to_usbdev(intf);
146 	const struct firmware *fwp = NULL;
147 	struct plfxlc_firmware_file file = {0};
148 	const char *fw_pack;
149 	int s, r;
150 	u8 *buf;
151 	u32 i;
152 
153 	r = send_vendor_command(udev, PLF_VNDR_XL_FW_CMD, NULL, 0);
154 	msleep(PLF_MSLEEP_TIME);
155 
156 	if (r) {
157 		dev_err(&intf->dev, "vendor command failed (%d)\n", r);
158 		return -EINVAL;
159 	}
160 	/* Code for single pack file download */
161 
162 	fw_pack = "plfxlc/lifi-xl.bin";
163 
164 	r = request_firmware(&fwp, fw_pack, &intf->dev);
165 	if (r) {
166 		dev_err(&intf->dev, "Request_firmware failed (%d)\n", r);
167 		return -EINVAL;
168 	}
169 	file.total_files = get_unaligned_le32(&fwp->data[0]);
170 	file.total_size = get_unaligned_le32(&fwp->size);
171 
172 	dev_dbg(&intf->dev, "XL Firmware (%d, %d)\n",
173 		file.total_files, file.total_size);
174 
175 	buf = kzalloc(PLF_XL_BUF_LEN, GFP_KERNEL);
176 	if (!buf) {
177 		release_firmware(fwp);
178 		return -ENOMEM;
179 	}
180 
181 	if (file.total_files > 10) {
182 		dev_err(&intf->dev, "Too many files (%d)\n", file.total_files);
183 		release_firmware(fwp);
184 		kfree(buf);
185 		return -EINVAL;
186 	}
187 
188 	/* Download firmware files in multiple steps */
189 	for (s = 0; s < file.total_files; s++) {
190 		buf[0] = s;
191 		r = send_vendor_command(udev, PLF_VNDR_XL_FILE_CMD, buf,
192 					PLF_XL_BUF_LEN);
193 
194 		if (s < file.total_files - 1)
195 			file.size = get_unaligned_le32(&fwp->data[4 + ((s + 1) * 4)])
196 				    - get_unaligned_le32(&fwp->data[4 + (s) * 4]);
197 		else
198 			file.size = file.total_size -
199 				    get_unaligned_le32(&fwp->data[4 + (s) * 4]);
200 
201 		if (file.size > file.total_size || file.size > 60000) {
202 			dev_err(&intf->dev, "File size is too large (%d)\n", file.size);
203 			break;
204 		}
205 
206 		file.start_addr = get_unaligned_le32(&fwp->data[4 + (s * 4)]);
207 
208 		if (file.size % PLF_XL_BUF_LEN && s < 2)
209 			file.size += PLF_XL_BUF_LEN - file.size % PLF_XL_BUF_LEN;
210 
211 		file.control_packets = file.size / PLF_XL_BUF_LEN;
212 
213 		for (i = 0; i < file.control_packets; i++) {
214 			memcpy(buf,
215 			       &fwp->data[file.start_addr + (i * PLF_XL_BUF_LEN)],
216 			       PLF_XL_BUF_LEN);
217 			r = send_vendor_command(udev, PLF_VNDR_XL_DATA_CMD, buf,
218 						PLF_XL_BUF_LEN);
219 		}
220 		dev_dbg(&intf->dev, "fw-dw step=%d,r=%d size=%d\n", s, r,
221 			file.size);
222 	}
223 	release_firmware(fwp);
224 	kfree(buf);
225 
226 	/* Code for single pack file download ends fw download finish */
227 
228 	r = send_vendor_command(udev, PLF_VNDR_XL_EX_CMD, NULL, 0);
229 	dev_dbg(&intf->dev, "Download fpga (4) (%d)\n", r);
230 
231 	return 0;
232 }
233 
plfxlc_upload_mac_and_serial(struct usb_interface * intf,unsigned char * hw_address,unsigned char * serial_number)234 int plfxlc_upload_mac_and_serial(struct usb_interface *intf,
235 				 unsigned char *hw_address,
236 				 unsigned char *serial_number)
237 {
238 	struct usb_device *udev = interface_to_usbdev(intf);
239 	unsigned long long firmware_version;
240 	unsigned char *dma_buffer = NULL;
241 
242 	dma_buffer = kmalloc(PLF_SERIAL_LEN, GFP_KERNEL);
243 	if (!dma_buffer)
244 		return -ENOMEM;
245 
246 	BUILD_BUG_ON(ETH_ALEN > PLF_SERIAL_LEN);
247 	BUILD_BUG_ON(PLF_FW_VER_LEN > PLF_SERIAL_LEN);
248 
249 	send_vendor_request(udev, PLF_MAC_VENDOR_REQUEST, dma_buffer,
250 			    ETH_ALEN);
251 
252 	memcpy(hw_address, dma_buffer, ETH_ALEN);
253 
254 	send_vendor_request(udev, PLF_SERIAL_NUMBER_VENDOR_REQUEST,
255 			    dma_buffer, PLF_SERIAL_LEN);
256 
257 	send_vendor_request(udev, PLF_SERIAL_NUMBER_VENDOR_REQUEST,
258 			    dma_buffer, PLF_SERIAL_LEN);
259 
260 	memcpy(serial_number, dma_buffer, PLF_SERIAL_LEN);
261 
262 	memset(dma_buffer, 0x00, PLF_SERIAL_LEN);
263 
264 	send_vendor_request(udev, PLF_FIRMWARE_VERSION_VENDOR_REQUEST,
265 			    (unsigned char *)dma_buffer, PLF_FW_VER_LEN);
266 
267 	memcpy(&firmware_version, dma_buffer, PLF_FW_VER_LEN);
268 
269 	dev_info(&intf->dev, "Firmware Version: %llu\n", firmware_version);
270 	kfree(dma_buffer);
271 
272 	dev_dbg(&intf->dev, "Mac: %pM\n", hw_address);
273 
274 	return 0;
275 }
276 
277