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);
183 inode = filp->f_dentry->d_inode;
184 if (!inode || !S_ISREG(inode->i_mode)) {
185 printk(KERN_ERR "Invalid file type: %s\n", img_name);
190 len = filp->f_op->read(filp, (u8 *)&hdr, sizeof(hdr), &pos);
191 if (len != sizeof(hdr)) {
192 printk(KERN_ERR "gdmwm: Cannot read the image info.\n");
197 array_le32_to_cpu((u32 *)&hdr, 19);
199 if (hdr.magic_code != 0x10767fff) {
200 printk(KERN_ERR "gdmwm: Invalid magic code 0x%08x\n",
206 if (hdr.count > MAX_IMG_CNT) {
207 printk(KERN_ERR "gdmwm: Too many images. %d\n", hdr.count);
212 for (i = 0; i < hdr.count; i++) {
213 if (hdr.offset[i] > hdr.len) {
214 printk(KERN_ERR "gdmwm: Invalid offset. "
215 "Entry = %d Offset = 0x%08x "
216 "Image length = 0x%08x\n",
217 i, hdr.offset[i], hdr.len);
223 len = filp->f_op->read(filp, (u8 *)&fw_info, sizeof(fw_info),
225 if (len != sizeof(fw_info)) {
226 printk(KERN_ERR "gdmwm: Cannot read the FW info.\n");
231 array_le32_to_cpu((u32 *)&fw_info, 8);
233 if ((fw_info.id & 0xfffff000) != 0x10767000) {
234 printk(KERN_ERR "gdmwm: Invalid FW id. 0x%08x\n",
241 if ((fw_info.id & 0xffff) != pid)
244 pos = hdr.offset[i] + fw_info.kernel_offset;
245 ret = download_image(usbdev, filp, &pos, fw_info.kernel_len,
246 DN_KERNEL_MAGIC_NUMBER);
249 printk(KERN_INFO "GCT: Kernel download success.\n");
251 pos = hdr.offset[i] + fw_info.rootfs_offset;
252 ret = download_image(usbdev, filp, &pos, fw_info.rootfs_len,
253 DN_ROOTFS_MAGIC_NUMBER);
256 printk(KERN_INFO "GCT: Filesystem download success.\n");
261 if (i == hdr.count) {
262 printk(KERN_ERR "Firmware for gsk%x is not installed.\n", pid);
266 filp_close(filp, current->files);
274 /*#define GDM7205_PADDING 256 */
275 #define DOWNLOAD_CHUCK 2048
276 #define KERNEL_TYPE_STRING "linux"
277 #define FS_TYPE_STRING "rootfs"
279 static int em_wait_ack(struct usb_device *usbdev, int send_zlp)
286 ret = gdm_wibro_send(usbdev, NULL, 0);
292 ret = gdm_wibro_recv(usbdev, &ack, sizeof(ack));
299 static int em_download_image(struct usb_device *usbdev, char *path,
304 static mm_segment_t fs;
309 #if defined(GDM7205_PADDING)
310 const int pad_size = GDM7205_PADDING;
312 const int pad_size = 0;
318 filp = filp_open(path, O_RDONLY | O_LARGEFILE, 0);
320 printk(KERN_ERR "Can't find %s.\n", path);
326 if (filp->f_dentry) {
327 inode = filp->f_dentry->d_inode;
328 if (!inode || !S_ISREG(inode->i_mode)) {
329 printk(KERN_ERR "Invalid file type: %s\n", path);
335 buf = kmalloc(DOWNLOAD_CHUCK + pad_size, GFP_KERNEL);
337 printk(KERN_ERR "Error: kmalloc\n");
341 strcpy(buf+pad_size, type_string);
342 ret = gdm_wibro_send(usbdev, buf, strlen(type_string)+pad_size);
346 while ((len = filp->f_op->read(filp, buf+pad_size, DOWNLOAD_CHUCK,
354 ret = gdm_wibro_send(usbdev, buf, len+pad_size);
358 ret = em_wait_ack(usbdev, ((len+pad_size) % 512 == 0));
363 ret = em_wait_ack(usbdev, 1);
368 filp_close(filp, current->files);
378 static int em_fw_reset(struct usb_device *usbdev)
383 ret = gdm_wibro_send(usbdev, NULL, 0);
387 int usb_emergency(struct usb_device *usbdev)
391 ret = em_download_image(usbdev, KERN_PATH, KERNEL_TYPE_STRING);
394 printk(KERN_INFO "GCT Emergency: Kernel download success.\n");
396 ret = em_download_image(usbdev, FS_PATH, FS_TYPE_STRING);
399 printk(KERN_INFO "GCT Emergency: Filesystem download success.\n");
401 ret = em_fw_reset(usbdev);