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 char as102_st_fw1[] = "as102_data1_st.hex";
30 char as102_st_fw2[] = "as102_data2_st.hex";
31 char as102_dt_fw1[] = "as102_data1_dt.hex";
32 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;
114 for (total_read_bytes = 0; total_read_bytes < firmware->size; ) {
115 int read_bytes = 0, data_len = 0;
117 /* parse intel hex line */
118 read_bytes = parse_hex_line(
119 (u8 *) (firmware->data + total_read_bytes),
128 /* detect the end of file */
129 total_read_bytes += read_bytes;
130 if (total_read_bytes == firmware->size) {
131 fw_pkt.u.request[0] = 0x00;
132 fw_pkt.u.request[1] = 0x03;
134 /* send EOF command */
135 errno = bus_adap->ops->upload_fw_pkt(bus_adap,
141 if (!addr_has_changed) {
142 /* prepare command to send */
143 fw_pkt.u.request[0] = 0x00;
144 fw_pkt.u.request[1] = 0x01;
146 data_len += sizeof(fw_pkt.u.request);
147 data_len += sizeof(fw_pkt.raw.address);
149 /* send cmd to device */
150 errno = bus_adap->ops->upload_fw_pkt(bus_adap,
162 return (errno == 0) ? total_read_bytes : errno;
165 int as102_fw_upload(struct as10x_bus_adapter_t *bus_adap)
168 const struct firmware *firmware = NULL;
169 unsigned char *cmd_buf = NULL;
171 struct usb_device *dev = bus_adap->usb_dev;
175 /* select fw file to upload */
184 /* allocate buffer to store firmware upload command and data */
185 cmd_buf = kzalloc(MAX_FW_PKT_SIZE, GFP_KERNEL);
186 if (cmd_buf == NULL) {
191 /* request kernel to locate firmware file: part1 */
192 errno = request_firmware(&firmware, fw1, &dev->dev);
194 pr_err("%s: unable to locate firmware file: %s\n",
199 /* initiate firmware upload */
200 errno = as102_firmware_upload(bus_adap, cmd_buf, firmware);
202 pr_err("%s: error during firmware upload part1\n",
207 pr_info("%s: firmware: %s loaded with success\n",
209 release_firmware(firmware);
211 /* wait for boot to complete */
214 /* request kernel to locate firmware file: part2 */
215 errno = request_firmware(&firmware, fw2, &dev->dev);
217 pr_err("%s: unable to locate firmware file: %s\n",
222 /* initiate firmware upload */
223 errno = as102_firmware_upload(bus_adap, cmd_buf, firmware);
225 pr_err("%s: error during firmware upload part2\n",
230 pr_info("%s: firmware: %s loaded with success\n",
233 /* free data buffer */
235 /* release firmware if needed */
236 if (firmware != NULL)
237 release_firmware(firmware);