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 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/ctype.h>
23 #include <linux/delay.h>
24 #include <linux/firmware.h>
26 #include "as102_drv.h"
29 static const char as102_st_fw1[] = "as102_data1_st.hex";
30 static const char as102_st_fw2[] = "as102_data2_st.hex";
31 static const char as102_dt_fw1[] = "as102_data1_dt.hex";
32 static const char as102_dt_fw2[] = "as102_data2_dt.hex";
34 static unsigned char atohx(unsigned char *dst, char *src)
36 unsigned char value = 0;
38 char msb = tolower(*src) - '0';
39 char lsb = tolower(*(src + 1)) - '0';
46 *dst = value = ((msb & 0xF) << 4) | (lsb & 0xF);
51 * Parse INTEL HEX firmware file to extract address and data.
53 static int parse_hex_line(unsigned char *fw_data, unsigned char *addr,
54 unsigned char *data, int *dataLength,
55 unsigned char *addr_has_changed) {
58 unsigned char *src, dst;
60 if (*fw_data++ != ':') {
61 pr_err("invalid firmware file\n");
65 /* locate end of line */
66 for (src = fw_data; *src != '\n'; src += 2) {
68 /* parse line to split addr / data */
80 /* check if data is an address */
82 *addr_has_changed = 1;
84 *addr_has_changed = 0;
88 if (*addr_has_changed)
89 addr[(count - 4)] = dst;
91 data[(count - 4)] = dst;
94 data[(count - 4)] = dst;
100 /* return read value + ':' + '\n' */
101 return (count * 2) + 2;
104 static int as102_firmware_upload(struct as10x_bus_adapter_t *bus_adap,
106 const struct firmware *firmware) {
108 struct as10x_fw_pkt_t fw_pkt;
109 int total_read_bytes = 0, errno = 0;
110 unsigned char addr_has_changed = 0;
112 for (total_read_bytes = 0; total_read_bytes < firmware->size; ) {
113 int read_bytes = 0, data_len = 0;
115 /* parse intel hex line */
116 read_bytes = parse_hex_line(
117 (u8 *) (firmware->data + total_read_bytes),
126 /* detect the end of file */
127 total_read_bytes += read_bytes;
128 if (total_read_bytes == firmware->size) {
129 fw_pkt.u.request[0] = 0x00;
130 fw_pkt.u.request[1] = 0x03;
132 /* send EOF command */
133 errno = bus_adap->ops->upload_fw_pkt(bus_adap,
139 if (!addr_has_changed) {
140 /* prepare command to send */
141 fw_pkt.u.request[0] = 0x00;
142 fw_pkt.u.request[1] = 0x01;
144 data_len += sizeof(fw_pkt.u.request);
145 data_len += sizeof(fw_pkt.raw.address);
147 /* send cmd to device */
148 errno = bus_adap->ops->upload_fw_pkt(bus_adap,
159 return (errno == 0) ? total_read_bytes : errno;
162 int as102_fw_upload(struct as10x_bus_adapter_t *bus_adap)
165 const struct firmware *firmware = NULL;
166 unsigned char *cmd_buf = NULL;
167 const char *fw1, *fw2;
168 struct usb_device *dev = bus_adap->usb_dev;
170 /* select fw file to upload */
179 /* allocate buffer to store firmware upload command and data */
180 cmd_buf = kzalloc(MAX_FW_PKT_SIZE, GFP_KERNEL);
181 if (cmd_buf == NULL) {
186 /* request kernel to locate firmware file: part1 */
187 errno = request_firmware(&firmware, fw1, &dev->dev);
189 pr_err("%s: unable to locate firmware file: %s\n",
194 /* initiate firmware upload */
195 errno = as102_firmware_upload(bus_adap, cmd_buf, firmware);
197 pr_err("%s: error during firmware upload part1\n",
202 pr_info("%s: firmware: %s loaded with success\n",
204 release_firmware(firmware);
206 /* wait for boot to complete */
209 /* request kernel to locate firmware file: part2 */
210 errno = request_firmware(&firmware, fw2, &dev->dev);
212 pr_err("%s: unable to locate firmware file: %s\n",
217 /* initiate firmware upload */
218 errno = as102_firmware_upload(bus_adap, cmd_buf, firmware);
220 pr_err("%s: error during firmware upload part2\n",
225 pr_info("%s: firmware: %s loaded with success\n",
229 release_firmware(firmware);