2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/uaccess.h>
15 #include <linux/module.h>
16 #include <linux/version.h>
17 #include <linux/kernel.h>
19 #include <linux/usb.h>
20 #include <linux/unistd.h>
21 #include <linux/slab.h>
23 #include <asm/byteorder.h>
27 #define DN_KERNEL_MAGIC_NUMBER 0x10760001
28 #define DN_ROOTFS_MAGIC_NUMBER 0x10760002
30 #define DOWNLOAD_SIZE 1024
32 #define DH2B(x) __cpu_to_be32(x)
33 #define DL2H(x) __le32_to_cpu(x)
35 #define MIN(a, b) ((a) > (b) ? (b) : (a))
37 #define MAX_IMG_CNT 16
38 #define UIMG_PATH "/lib/firmware/gdm72xx/gdmuimg.bin"
39 #define KERN_PATH "/lib/firmware/gdm72xx/zImage"
40 #define FS_PATH "/lib/firmware/gdm72xx/ramdisk.jffs2"
51 u32 offset[MAX_IMG_CNT];
71 static void array_le32_to_cpu(u32 *arr, int num)
74 for (i = 0; i < num; i++, arr++)
80 static int gdm_wibro_send(struct usb_device *usbdev, void *data, int len)
85 ret = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), data, len,
89 printk(KERN_ERR "Error : usb_bulk_msg ( result = %d )\n", ret);
95 static int gdm_wibro_recv(struct usb_device *usbdev, void *data, int len)
100 ret = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), data, len,
104 printk(KERN_ERR "Error : usb_bulk_msg(recv) ( result = %d )\n",
111 static int download_image(struct usb_device *usbdev, struct file *filp,
112 loff_t *pos, u32 img_len, u32 magic_num)
119 size = (img_len + DOWNLOAD_SIZE - 1) & ~(DOWNLOAD_SIZE - 1);
120 h.magic_num = DH2B(magic_num);
121 h.file_size = DH2B(size);
123 ret = gdm_wibro_send(usbdev, &h, sizeof(h));
128 while ((len = filp->f_op->read(filp, tx_buf, DOWNLOAD_SIZE, pos))) {
136 ret = gdm_wibro_send(usbdev, tx_buf, DOWNLOAD_SIZE);
139 if (readn >= img_len)
143 if (readn < img_len) {
144 printk(KERN_ERR "gdmwm: Cannot read to the requested size. "
145 "Read = %d Requested = %d\n", readn, img_len);
153 int usb_boot(struct usb_device *usbdev, u16 pid)
156 struct file *filp = NULL;
157 struct inode *inode = NULL;
158 static mm_segment_t fs;
159 struct img_header hdr;
160 struct fw_info fw_info;
162 char *img_name = UIMG_PATH;
165 tx_buf = kmalloc(DOWNLOAD_SIZE, GFP_KERNEL);
166 if (tx_buf == NULL) {
167 printk(KERN_ERR "Error: kmalloc\n");
174 filp = filp_open(img_name, O_RDONLY | O_LARGEFILE, 0);
176 printk(KERN_ERR "Can't find %s.\n", img_name);
181 inode = filp->f_dentry->d_inode;
182 if (!S_ISREG(inode->i_mode)) {
183 printk(KERN_ERR "Invalid file type: %s\n", img_name);
188 len = filp->f_op->read(filp, (u8 *)&hdr, sizeof(hdr), &pos);
189 if (len != sizeof(hdr)) {
190 printk(KERN_ERR "gdmwm: Cannot read the image info.\n");
195 array_le32_to_cpu((u32 *)&hdr, 19);
197 if (hdr.magic_code != 0x10767fff) {
198 printk(KERN_ERR "gdmwm: Invalid magic code 0x%08x\n",
204 if (hdr.count > MAX_IMG_CNT) {
205 printk(KERN_ERR "gdmwm: Too many images. %d\n", hdr.count);
210 for (i = 0; i < hdr.count; i++) {
211 if (hdr.offset[i] > hdr.len) {
212 printk(KERN_ERR "gdmwm: Invalid offset. "
213 "Entry = %d Offset = 0x%08x "
214 "Image length = 0x%08x\n",
215 i, hdr.offset[i], hdr.len);
221 len = filp->f_op->read(filp, (u8 *)&fw_info, sizeof(fw_info),
223 if (len != sizeof(fw_info)) {
224 printk(KERN_ERR "gdmwm: Cannot read the FW info.\n");
229 array_le32_to_cpu((u32 *)&fw_info, 8);
231 if ((fw_info.id & 0xfffff000) != 0x10767000) {
232 printk(KERN_ERR "gdmwm: Invalid FW id. 0x%08x\n",
239 if ((fw_info.id & 0xffff) != pid)
242 pos = hdr.offset[i] + fw_info.kernel_offset;
243 ret = download_image(usbdev, filp, &pos, fw_info.kernel_len,
244 DN_KERNEL_MAGIC_NUMBER);
247 printk(KERN_INFO "GCT: Kernel download success.\n");
249 pos = hdr.offset[i] + fw_info.rootfs_offset;
250 ret = download_image(usbdev, filp, &pos, fw_info.rootfs_len,
251 DN_ROOTFS_MAGIC_NUMBER);
254 printk(KERN_INFO "GCT: Filesystem download success.\n");
259 if (i == hdr.count) {
260 printk(KERN_ERR "Firmware for gsk%x is not installed.\n", pid);
264 filp_close(filp, NULL);
272 /*#define GDM7205_PADDING 256 */
273 #define DOWNLOAD_CHUCK 2048
274 #define KERNEL_TYPE_STRING "linux"
275 #define FS_TYPE_STRING "rootfs"
277 static int em_wait_ack(struct usb_device *usbdev, int send_zlp)
284 ret = gdm_wibro_send(usbdev, NULL, 0);
290 ret = gdm_wibro_recv(usbdev, &ack, sizeof(ack));
297 static int em_download_image(struct usb_device *usbdev, char *path,
302 static mm_segment_t fs;
307 #if defined(GDM7205_PADDING)
308 const int pad_size = GDM7205_PADDING;
310 const int pad_size = 0;
316 filp = filp_open(path, O_RDONLY | O_LARGEFILE, 0);
318 printk(KERN_ERR "Can't find %s.\n", path);
324 inode = filp->f_dentry->d_inode;
325 if (!S_ISREG(inode->i_mode)) {
326 printk(KERN_ERR "Invalid file type: %s\n", path);
331 buf = kmalloc(DOWNLOAD_CHUCK + pad_size, GFP_KERNEL);
333 printk(KERN_ERR "Error: kmalloc\n");
337 strcpy(buf+pad_size, type_string);
338 ret = gdm_wibro_send(usbdev, buf, strlen(type_string)+pad_size);
342 while ((len = filp->f_op->read(filp, buf+pad_size, DOWNLOAD_CHUCK,
350 ret = gdm_wibro_send(usbdev, buf, len+pad_size);
354 ret = em_wait_ack(usbdev, ((len+pad_size) % 512 == 0));
359 ret = em_wait_ack(usbdev, 1);
364 filp_close(filp, NULL);
374 static int em_fw_reset(struct usb_device *usbdev)
379 ret = gdm_wibro_send(usbdev, NULL, 0);
383 int usb_emergency(struct usb_device *usbdev)
387 ret = em_download_image(usbdev, KERN_PATH, KERNEL_TYPE_STRING);
390 printk(KERN_INFO "GCT Emergency: Kernel download success.\n");
392 ret = em_download_image(usbdev, FS_PATH, FS_TYPE_STRING);
395 printk(KERN_INFO "GCT Emergency: Filesystem download success.\n");
397 ret = em_fw_reset(usbdev);