2 * Abilis Systems Single DVB-T Receiver
3 * Copyright (C) 2008 Pierrick Hascoet <pierrick.hascoet@abilis.com>
4 * Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/ctype.h>
19 #include <linux/delay.h>
20 #include <linux/firmware.h>
22 #include "as102_drv.h"
25 static const char as102_st_fw1
[] = "as102_data1_st.hex";
26 static const char as102_st_fw2
[] = "as102_data2_st.hex";
27 static const char as102_dt_fw1
[] = "as102_data1_dt.hex";
28 static const char as102_dt_fw2
[] = "as102_data2_dt.hex";
30 static unsigned char atohx(unsigned char *dst
, char *src
)
32 unsigned char value
= 0;
34 char msb
= tolower(*src
) - '0';
35 char lsb
= tolower(*(src
+ 1)) - '0';
42 *dst
= value
= ((msb
& 0xF) << 4) | (lsb
& 0xF);
47 * Parse INTEL HEX firmware file to extract address and data.
49 static int parse_hex_line(unsigned char *fw_data
, unsigned char *addr
,
50 unsigned char *data
, int *dataLength
,
51 unsigned char *addr_has_changed
) {
54 unsigned char *src
, dst
;
56 if (*fw_data
++ != ':') {
57 pr_err("invalid firmware file\n");
61 /* locate end of line */
62 for (src
= fw_data
; *src
!= '\n'; src
+= 2) {
64 /* parse line to split addr / data */
76 /* check if data is an address */
78 *addr_has_changed
= 1;
80 *addr_has_changed
= 0;
84 if (*addr_has_changed
)
85 addr
[(count
- 4)] = dst
;
87 data
[(count
- 4)] = dst
;
90 data
[(count
- 4)] = dst
;
96 /* return read value + ':' + '\n' */
97 return (count
* 2) + 2;
100 static int as102_firmware_upload(struct as10x_bus_adapter_t
*bus_adap
,
102 const struct firmware
*firmware
) {
104 struct as10x_fw_pkt_t fw_pkt
;
105 int total_read_bytes
= 0, errno
= 0;
106 unsigned char addr_has_changed
= 0;
108 for (total_read_bytes
= 0; total_read_bytes
< firmware
->size
; ) {
109 int read_bytes
= 0, data_len
= 0;
111 /* parse intel hex line */
112 read_bytes
= parse_hex_line(
113 (u8
*) (firmware
->data
+ total_read_bytes
),
122 /* detect the end of file */
123 total_read_bytes
+= read_bytes
;
124 if (total_read_bytes
== firmware
->size
) {
125 fw_pkt
.u
.request
[0] = 0x00;
126 fw_pkt
.u
.request
[1] = 0x03;
128 /* send EOF command */
129 errno
= bus_adap
->ops
->upload_fw_pkt(bus_adap
,
135 if (!addr_has_changed
) {
136 /* prepare command to send */
137 fw_pkt
.u
.request
[0] = 0x00;
138 fw_pkt
.u
.request
[1] = 0x01;
140 data_len
+= sizeof(fw_pkt
.u
.request
);
141 data_len
+= sizeof(fw_pkt
.raw
.address
);
143 /* send cmd to device */
144 errno
= bus_adap
->ops
->upload_fw_pkt(bus_adap
,
155 return (errno
== 0) ? total_read_bytes
: errno
;
158 int as102_fw_upload(struct as10x_bus_adapter_t
*bus_adap
)
161 const struct firmware
*firmware
= NULL
;
162 unsigned char *cmd_buf
= NULL
;
163 const char *fw1
, *fw2
;
164 struct usb_device
*dev
= bus_adap
->usb_dev
;
166 /* select fw file to upload */
175 /* allocate buffer to store firmware upload command and data */
176 cmd_buf
= kzalloc(MAX_FW_PKT_SIZE
, GFP_KERNEL
);
177 if (cmd_buf
== NULL
) {
182 /* request kernel to locate firmware file: part1 */
183 errno
= request_firmware(&firmware
, fw1
, &dev
->dev
);
185 pr_err("%s: unable to locate firmware file: %s\n",
190 /* initiate firmware upload */
191 errno
= as102_firmware_upload(bus_adap
, cmd_buf
, firmware
);
193 pr_err("%s: error during firmware upload part1\n",
198 pr_info("%s: firmware: %s loaded with success\n",
200 release_firmware(firmware
);
202 /* wait for boot to complete */
205 /* request kernel to locate firmware file: part2 */
206 errno
= request_firmware(&firmware
, fw2
, &dev
->dev
);
208 pr_err("%s: unable to locate firmware file: %s\n",
213 /* initiate firmware upload */
214 errno
= as102_firmware_upload(bus_adap
, cmd_buf
, firmware
);
216 pr_err("%s: error during firmware upload part2\n",
221 pr_info("%s: firmware: %s loaded with success\n",
225 release_firmware(firmware
);