Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Linux driver for TerraTec DMX 6Fire USB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Firmware loader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author:	Torsten Schenk <torsten.schenk@zoho.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Created:	Jan 01, 2011
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright:	(C) Torsten Schenk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/firmware.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/bitrev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "firmware.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "chip.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) MODULE_FIRMWARE("6fire/dmx6firel2.ihx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) MODULE_FIRMWARE("6fire/dmx6fireap.ihx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) MODULE_FIRMWARE("6fire/dmx6firecf.bin");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	FPGA_BUFSIZE = 512, FPGA_EP = 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * wMaxPacketSize of pcm endpoints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * keep synced with rates_in_packet_size and rates_out_packet_size in pcm.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * fpp: frames per isopacket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * CAUTION: keep sizeof <= buffer[] in usb6fire_fw_init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static const u8 ep_w_max_packet_size[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	0xe4, 0x00, 0xe4, 0x00, /* alt 1: 228 EP2 and EP6 (7 fpp) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	0xa4, 0x01, 0xa4, 0x01, /* alt 2: 420 EP2 and EP6 (13 fpp)*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	0x94, 0x01, 0x5c, 0x02  /* alt 3: 404 EP2 and 604 EP6 (25 fpp) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static const u8 known_fw_versions[][2] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	{ 0x03, 0x01 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) struct ihex_record {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	u16 address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	u8 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	u8 data[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	char error; /* true if an error occurred parsing this record */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	u8 max_len; /* maximum record length in whole ihex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	/* private */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	const char *txt_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	unsigned int txt_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	unsigned int txt_offset; /* current position in txt_data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static u8 usb6fire_fw_ihex_hex(const u8 *data, u8 *crc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	u8 val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	int hval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	hval = hex_to_bin(data[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (hval >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		val |= (hval << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	hval = hex_to_bin(data[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (hval >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		val |= hval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	*crc += val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * returns true if record is available, false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * iff an error occurred, false will be returned and record->error will be true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static bool usb6fire_fw_ihex_next_record(struct ihex_record *record)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	u8 crc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	u8 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	record->error = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/* find begin of record (marked by a colon) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	while (record->txt_offset < record->txt_length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			&& record->txt_data[record->txt_offset] != ':')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		record->txt_offset++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (record->txt_offset == record->txt_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	/* number of characters needed for len, addr and type entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	record->txt_offset++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (record->txt_offset + 8 > record->txt_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		record->error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	record->len = usb6fire_fw_ihex_hex(record->txt_data +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			record->txt_offset, &crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	record->txt_offset += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	record->address = usb6fire_fw_ihex_hex(record->txt_data +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			record->txt_offset, &crc) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	record->txt_offset += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	record->address |= usb6fire_fw_ihex_hex(record->txt_data +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			record->txt_offset, &crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	record->txt_offset += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	type = usb6fire_fw_ihex_hex(record->txt_data +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			record->txt_offset, &crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	record->txt_offset += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	/* number of characters needed for data and crc entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (record->txt_offset + 2 * (record->len + 1) > record->txt_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		record->error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	for (i = 0; i < record->len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		record->data[i] = usb6fire_fw_ihex_hex(record->txt_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 				+ record->txt_offset, &crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		record->txt_offset += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	usb6fire_fw_ihex_hex(record->txt_data + record->txt_offset, &crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (crc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		record->error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (type == 1 || !record->len) /* eof */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	else if (type == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		record->error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static int usb6fire_fw_ihex_init(const struct firmware *fw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		struct ihex_record *record)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	record->txt_data = fw->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	record->txt_length = fw->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	record->txt_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	record->max_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	/* read all records, if loop ends, record->error indicates,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	 * whether ihex is valid. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	while (usb6fire_fw_ihex_next_record(record))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		record->max_len = max(record->len, record->max_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (record->error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	record->txt_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static int usb6fire_fw_ezusb_write(struct usb_device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		int type, int value, char *data, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	return usb_control_msg_send(device, 0, type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				    value, 0, data, len, 1000, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int usb6fire_fw_ezusb_read(struct usb_device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		int type, int value, char *data, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	return usb_control_msg_recv(device, 0, type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 				    USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				    value, 0, data, len, 1000, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static int usb6fire_fw_fpga_write(struct usb_device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		char *data, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	int actual_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	ret = usb_bulk_msg(device, usb_sndbulkpipe(device, FPGA_EP), data, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			&actual_len, 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	else if (actual_len != len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int usb6fire_fw_ezusb_upload(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		struct usb_interface *intf, const char *fwname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		unsigned int postaddr, u8 *postdata, unsigned int postlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	u8 data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	struct usb_device *device = interface_to_usbdev(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	const struct firmware *fw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	struct ihex_record *rec = kmalloc(sizeof(struct ihex_record),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (!rec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	ret = request_firmware(&fw, fwname, &device->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		kfree(rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		dev_err(&intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			"error requesting ezusb firmware %s.\n", fwname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	ret = usb6fire_fw_ihex_init(fw, rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		kfree(rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		release_firmware(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		dev_err(&intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			"error validating ezusb firmware %s.\n", fwname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	/* upload firmware image */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	data = 0x01; /* stop ezusb cpu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		kfree(rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		release_firmware(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		dev_err(&intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			"unable to upload ezusb firmware %s: begin message.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			fwname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	while (usb6fire_fw_ihex_next_record(rec)) { /* write firmware */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		ret = usb6fire_fw_ezusb_write(device, 0xa0, rec->address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 				rec->data, rec->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			kfree(rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			release_firmware(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			dev_err(&intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 				"unable to upload ezusb firmware %s: data urb.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 				fwname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	release_firmware(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	kfree(rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (postdata) { /* write data after firmware has been uploaded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		ret = usb6fire_fw_ezusb_write(device, 0xa0, postaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 				postdata, postlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			dev_err(&intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 				"unable to upload ezusb firmware %s: post urb.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 				fwname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	data = 0x00; /* resume ezusb cpu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		dev_err(&intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			"unable to upload ezusb firmware %s: end message.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			fwname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static int usb6fire_fw_fpga_upload(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		struct usb_interface *intf, const char *fwname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	struct usb_device *device = interface_to_usbdev(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	u8 *buffer = kmalloc(FPGA_BUFSIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	const char *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	const char *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	const struct firmware *fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	ret = request_firmware(&fw, fwname, &device->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		dev_err(&intf->dev, "unable to get fpga firmware %s.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 				fwname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	c = fw->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	end = fw->data + fw->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	ret = usb6fire_fw_ezusb_write(device, 8, 0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		release_firmware(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		dev_err(&intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			"unable to upload fpga firmware: begin urb.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	while (c != end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		for (i = 0; c != end && i < FPGA_BUFSIZE; i++, c++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			buffer[i] = bitrev8((u8)*c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		ret = usb6fire_fw_fpga_write(device, buffer, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			release_firmware(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			dev_err(&intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 				"unable to upload fpga firmware: fw urb.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	release_firmware(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	ret = usb6fire_fw_ezusb_write(device, 9, 0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		dev_err(&intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			"unable to upload fpga firmware: end urb.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /* check, if the firmware version the devices has currently loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)  * is known by this driver. 'version' needs to have 4 bytes version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)  * info data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static int usb6fire_fw_check(struct usb_interface *intf, const u8 *version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	for (i = 0; i < ARRAY_SIZE(known_fw_versions); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		if (!memcmp(version, known_fw_versions + i, 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	dev_err(&intf->dev, "invalid firmware version in device: %4ph. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			"please reconnect to power. if this failure "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			"still happens, check your firmware installation.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) int usb6fire_fw_init(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	struct usb_device *device = interface_to_usbdev(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	/* buffer: 8 receiving bytes from device and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	 * sizeof(EP_W_MAX_PACKET_SIZE) bytes for non-const copy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	u8 buffer[12];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	ret = usb6fire_fw_ezusb_read(device, 1, 0, buffer, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		dev_err(&intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			"unable to receive device firmware state.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (buffer[0] != 0xeb || buffer[1] != 0xaa || buffer[2] != 0x55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		dev_err(&intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			"unknown device firmware state received from device:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		for (i = 0; i < 8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			printk(KERN_CONT "%02x ", buffer[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		printk(KERN_CONT "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	/* do we need fpga loader ezusb firmware? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	if (buffer[3] == 0x01) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		ret = usb6fire_fw_ezusb_upload(intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 				"6fire/dmx6firel2.ihx", 0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		return FW_NOT_READY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	/* do we need fpga firmware and application ezusb firmware? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	else if (buffer[3] == 0x02) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		ret = usb6fire_fw_check(intf, buffer + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		ret = usb6fire_fw_fpga_upload(intf, "6fire/dmx6firecf.bin");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		memcpy(buffer, ep_w_max_packet_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 				sizeof(ep_w_max_packet_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		ret = usb6fire_fw_ezusb_upload(intf, "6fire/dmx6fireap.ihx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 				0x0003,	buffer, sizeof(ep_w_max_packet_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		return FW_NOT_READY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	/* all fw loaded? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	else if (buffer[3] == 0x03)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		return usb6fire_fw_check(intf, buffer + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	/* unknown data? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		dev_err(&intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			"unknown device firmware state received from device: ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		for (i = 0; i < 8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			printk(KERN_CONT "%02x ", buffer[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		printk(KERN_CONT "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)