1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Abilis Systems Single DVB-T Receiver
4 * Copyright (C) 2008 Pierrick Hascoet <pierrick.hascoet@abilis.com>
5 * Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.com>
7 #include <linux/kernel.h>
8 #include <linux/errno.h>
9 #include <linux/ctype.h>
10 #include <linux/delay.h>
11 #include <linux/firmware.h>
13 #include "as102_drv.h"
16 static const char as102_st_fw1
[] = "as102_data1_st.hex";
17 static const char as102_st_fw2
[] = "as102_data2_st.hex";
18 static const char as102_dt_fw1
[] = "as102_data1_dt.hex";
19 static const char as102_dt_fw2
[] = "as102_data2_dt.hex";
21 static unsigned char atohx(unsigned char *dst
, char *src
)
23 unsigned char value
= 0;
25 char msb
= tolower(*src
) - '0';
26 char lsb
= tolower(*(src
+ 1)) - '0';
33 *dst
= value
= ((msb
& 0xF) << 4) | (lsb
& 0xF);
38 * Parse INTEL HEX firmware file to extract address and data.
40 static int parse_hex_line(unsigned char *fw_data
, unsigned char *addr
,
41 unsigned char *data
, int *dataLength
,
42 unsigned char *addr_has_changed
) {
45 unsigned char *src
, dst
;
47 if (*fw_data
++ != ':') {
48 pr_err("invalid firmware file\n");
52 /* locate end of line */
53 for (src
= fw_data
; *src
!= '\n'; src
+= 2) {
55 /* parse line to split addr / data */
67 /* check if data is an address */
69 *addr_has_changed
= 1;
71 *addr_has_changed
= 0;
75 if (*addr_has_changed
)
76 addr
[(count
- 4)] = dst
;
78 data
[(count
- 4)] = dst
;
81 data
[(count
- 4)] = dst
;
87 /* return read value + ':' + '\n' */
88 return (count
* 2) + 2;
91 static int as102_firmware_upload(struct as10x_bus_adapter_t
*bus_adap
,
93 const struct firmware
*firmware
) {
95 struct as10x_fw_pkt_t
*fw_pkt
;
96 int total_read_bytes
= 0, errno
= 0;
97 unsigned char addr_has_changed
= 0;
99 fw_pkt
= kmalloc(sizeof(*fw_pkt
), GFP_KERNEL
);
104 for (total_read_bytes
= 0; total_read_bytes
< firmware
->size
; ) {
105 int read_bytes
= 0, data_len
= 0;
107 /* parse intel hex line */
108 read_bytes
= parse_hex_line(
109 (u8
*) (firmware
->data
+ total_read_bytes
),
118 /* detect the end of file */
119 total_read_bytes
+= read_bytes
;
120 if (total_read_bytes
== firmware
->size
) {
121 fw_pkt
->u
.request
[0] = 0x00;
122 fw_pkt
->u
.request
[1] = 0x03;
124 /* send EOF command */
125 errno
= bus_adap
->ops
->upload_fw_pkt(bus_adap
,
131 if (!addr_has_changed
) {
132 /* prepare command to send */
133 fw_pkt
->u
.request
[0] = 0x00;
134 fw_pkt
->u
.request
[1] = 0x01;
136 data_len
+= sizeof(fw_pkt
->u
.request
);
137 data_len
+= sizeof(fw_pkt
->raw
.address
);
139 /* send cmd to device */
140 errno
= bus_adap
->ops
->upload_fw_pkt(bus_adap
,
152 return (errno
== 0) ? total_read_bytes
: errno
;
155 int as102_fw_upload(struct as10x_bus_adapter_t
*bus_adap
)
158 const struct firmware
*firmware
= NULL
;
159 unsigned char *cmd_buf
= NULL
;
160 const char *fw1
, *fw2
;
161 struct usb_device
*dev
= bus_adap
->usb_dev
;
163 /* select fw file to upload */
172 /* allocate buffer to store firmware upload command and data */
173 cmd_buf
= kzalloc(MAX_FW_PKT_SIZE
, GFP_KERNEL
);
174 if (cmd_buf
== NULL
) {
179 /* request kernel to locate firmware file: part1 */
180 errno
= request_firmware(&firmware
, fw1
, &dev
->dev
);
182 pr_err("%s: unable to locate firmware file: %s\n",
187 /* initiate firmware upload */
188 errno
= as102_firmware_upload(bus_adap
, cmd_buf
, firmware
);
190 pr_err("%s: error during firmware upload part1\n",
195 pr_info("%s: firmware: %s loaded with success\n",
197 release_firmware(firmware
);
200 /* wait for boot to complete */
203 /* request kernel to locate firmware file: part2 */
204 errno
= request_firmware(&firmware
, fw2
, &dev
->dev
);
206 pr_err("%s: unable to locate firmware file: %s\n",
211 /* initiate firmware upload */
212 errno
= as102_firmware_upload(bus_adap
, cmd_buf
, firmware
);
214 pr_err("%s: error during firmware upload part2\n",
219 pr_info("%s: firmware: %s loaded with success\n",
223 release_firmware(firmware
);