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/kernel.h>
18 #include <linux/usb.h>
19 #include <linux/unistd.h>
20 #include <linux/slab.h>
22 #include <asm/byteorder.h>
26 #define DN_KERNEL_MAGIC_NUMBER 0x10760001
27 #define DN_ROOTFS_MAGIC_NUMBER 0x10760002
29 #define DOWNLOAD_SIZE 1024
31 #define DH2B(x) __cpu_to_be32(x)
32 #define DL2H(x) __le32_to_cpu(x)
34 #define MIN(a, b) ((a) > (b) ? (b) : (a))
36 #define MAX_IMG_CNT 16
37 #define UIMG_PATH "/lib/firmware/gdm72xx/gdmuimg.bin"
38 #define KERN_PATH "/lib/firmware/gdm72xx/zImage"
39 #define FS_PATH "/lib/firmware/gdm72xx/ramdisk.jffs2"
50 u32 offset[MAX_IMG_CNT];
70 static void array_le32_to_cpu(u32 *arr, int num)
73 for (i = 0; i < num; i++, arr++)
79 static int gdm_wibro_send(struct usb_device *usbdev, void *data, int len)
84 ret = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), data, len,
88 printk(KERN_ERR "Error : usb_bulk_msg ( result = %d )\n", ret);
94 static int gdm_wibro_recv(struct usb_device *usbdev, void *data, int len)
99 ret = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), data, len,
103 printk(KERN_ERR "Error : usb_bulk_msg(recv) ( result = %d )\n",
110 static int download_image(struct usb_device *usbdev, struct file *filp,
111 loff_t *pos, u32 img_len, u32 magic_num)
118 size = (img_len + DOWNLOAD_SIZE - 1) & ~(DOWNLOAD_SIZE - 1);
119 h.magic_num = DH2B(magic_num);
120 h.file_size = DH2B(size);
122 ret = gdm_wibro_send(usbdev, &h, sizeof(h));
127 while ((len = filp->f_op->read(filp, tx_buf, DOWNLOAD_SIZE, pos))) {
135 ret = gdm_wibro_send(usbdev, tx_buf, DOWNLOAD_SIZE);
138 if (readn >= img_len)
142 if (readn < img_len) {
143 printk(KERN_ERR "gdmwm: Cannot read to the requested size. "
144 "Read = %d Requested = %d\n", readn, img_len);
152 int usb_boot(struct usb_device *usbdev, u16 pid)
155 struct file *filp = NULL;
156 struct inode *inode = NULL;
157 static mm_segment_t fs;
158 struct img_header hdr;
159 struct fw_info fw_info;
161 char *img_name = UIMG_PATH;
164 tx_buf = kmalloc(DOWNLOAD_SIZE, GFP_KERNEL);
165 if (tx_buf == NULL) {
166 printk(KERN_ERR "Error: kmalloc\n");
173 filp = filp_open(img_name, O_RDONLY | O_LARGEFILE, 0);
175 printk(KERN_ERR "Can't find %s.\n", img_name);
180 inode = filp->f_dentry->d_inode;
181 if (!S_ISREG(inode->i_mode)) {
182 printk(KERN_ERR "Invalid file type: %s\n", img_name);
187 len = filp->f_op->read(filp, (u8 *)&hdr, sizeof(hdr), &pos);
188 if (len != sizeof(hdr)) {
189 printk(KERN_ERR "gdmwm: Cannot read the image info.\n");
194 array_le32_to_cpu((u32 *)&hdr, 19);
196 if (hdr.magic_code != 0x10767fff) {
197 printk(KERN_ERR "gdmwm: Invalid magic code 0x%08x\n",
203 if (hdr.count > MAX_IMG_CNT) {
204 printk(KERN_ERR "gdmwm: Too many images. %d\n", hdr.count);
209 for (i = 0; i < hdr.count; i++) {
210 if (hdr.offset[i] > hdr.len) {
211 printk(KERN_ERR "gdmwm: Invalid offset. "
212 "Entry = %d Offset = 0x%08x "
213 "Image length = 0x%08x\n",
214 i, hdr.offset[i], hdr.len);
220 len = filp->f_op->read(filp, (u8 *)&fw_info, sizeof(fw_info),
222 if (len != sizeof(fw_info)) {
223 printk(KERN_ERR "gdmwm: Cannot read the FW info.\n");
228 array_le32_to_cpu((u32 *)&fw_info, 8);
230 if ((fw_info.id & 0xfffff000) != 0x10767000) {
231 printk(KERN_ERR "gdmwm: Invalid FW id. 0x%08x\n",
238 if ((fw_info.id & 0xffff) != pid)
241 pos = hdr.offset[i] + fw_info.kernel_offset;
242 ret = download_image(usbdev, filp, &pos, fw_info.kernel_len,
243 DN_KERNEL_MAGIC_NUMBER);
246 printk(KERN_INFO "GCT: Kernel download success.\n");
248 pos = hdr.offset[i] + fw_info.rootfs_offset;
249 ret = download_image(usbdev, filp, &pos, fw_info.rootfs_len,
250 DN_ROOTFS_MAGIC_NUMBER);
253 printk(KERN_INFO "GCT: Filesystem download success.\n");
258 if (i == hdr.count) {
259 printk(KERN_ERR "Firmware for gsk%x is not installed.\n", pid);
263 filp_close(filp, NULL);
271 /*#define GDM7205_PADDING 256 */
272 #define DOWNLOAD_CHUCK 2048
273 #define KERNEL_TYPE_STRING "linux"
274 #define FS_TYPE_STRING "rootfs"
276 static int em_wait_ack(struct usb_device *usbdev, int send_zlp)
283 ret = gdm_wibro_send(usbdev, NULL, 0);
289 ret = gdm_wibro_recv(usbdev, &ack, sizeof(ack));
296 static int em_download_image(struct usb_device *usbdev, char *path,
301 static mm_segment_t fs;
306 #if defined(GDM7205_PADDING)
307 const int pad_size = GDM7205_PADDING;
309 const int pad_size = 0;
315 filp = filp_open(path, O_RDONLY | O_LARGEFILE, 0);
317 printk(KERN_ERR "Can't find %s.\n", path);
323 inode = filp->f_dentry->d_inode;
324 if (!S_ISREG(inode->i_mode)) {
325 printk(KERN_ERR "Invalid file type: %s\n", path);
330 buf = kmalloc(DOWNLOAD_CHUCK + pad_size, GFP_KERNEL);
332 printk(KERN_ERR "Error: kmalloc\n");
336 strcpy(buf+pad_size, type_string);
337 ret = gdm_wibro_send(usbdev, buf, strlen(type_string)+pad_size);
341 while ((len = filp->f_op->read(filp, buf+pad_size, DOWNLOAD_CHUCK,
349 ret = gdm_wibro_send(usbdev, buf, len+pad_size);
353 ret = em_wait_ack(usbdev, ((len+pad_size) % 512 == 0));
358 ret = em_wait_ack(usbdev, 1);
363 filp_close(filp, NULL);
373 static int em_fw_reset(struct usb_device *usbdev)
378 ret = gdm_wibro_send(usbdev, NULL, 0);
382 int usb_emergency(struct usb_device *usbdev)
386 ret = em_download_image(usbdev, KERN_PATH, KERNEL_TYPE_STRING);
389 printk(KERN_INFO "GCT Emergency: Kernel download success.\n");
391 ret = em_download_image(usbdev, FS_PATH, FS_TYPE_STRING);
394 printk(KERN_INFO "GCT Emergency: Filesystem download success.\n");
396 ret = em_fw_reset(usbdev);