Merge git://1984.lsi.us.es/nf-next
[cascardo/linux.git] / drivers / staging / gdm72xx / usb_boot.c
1 /*
2  * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
3  *
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.
7  *
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.
12  */
13
14 #include <linux/uaccess.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/usb.h>
19 #include <linux/unistd.h>
20 #include <linux/slab.h>
21
22 #include <asm/byteorder.h>
23 #include "gdm_usb.h"
24 #include "usb_boot.h"
25
26 #define DN_KERNEL_MAGIC_NUMBER          0x10760001
27 #define DN_ROOTFS_MAGIC_NUMBER          0x10760002
28
29 #define DOWNLOAD_SIZE   1024
30
31 #define DH2B(x)         __cpu_to_be32(x)
32 #define DL2H(x)         __le32_to_cpu(x)
33
34 #define MIN(a, b)       ((a) > (b) ? (b) : (a))
35
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"
40
41 struct dn_header {
42         u32     magic_num;
43         u32     file_size;
44 };
45
46 struct img_header {
47         u32             magic_code;
48         u32             count;
49         u32             len;
50         u32             offset[MAX_IMG_CNT];
51         char    hostname[32];
52         char    date[32];
53 };
54
55 struct fw_info {
56         u32             id;
57         u32             len;
58         u32             kernel_len;
59         u32             rootfs_len;
60         u32             kernel_offset;
61         u32             rootfs_offset;
62         u32             fw_ver;
63         u32             mac_ver;
64         char    hostname[32];
65         char    userid[16];
66         char    date[32];
67         char    user_desc[128];
68 };
69
70 static void array_le32_to_cpu(u32 *arr, int num)
71 {
72         int i;
73         for (i = 0; i < num; i++, arr++)
74                 *arr = DL2H(*arr);
75 }
76
77 static u8 *tx_buf;
78
79 static int gdm_wibro_send(struct usb_device *usbdev, void *data, int len)
80 {
81         int ret;
82         int actual;
83
84         ret = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), data, len,
85                         &actual, 1000);
86
87         if (ret < 0) {
88                 printk(KERN_ERR "Error : usb_bulk_msg ( result = %d )\n", ret);
89                 return ret;
90         }
91         return 0;
92 }
93
94 static int gdm_wibro_recv(struct usb_device *usbdev, void *data, int len)
95 {
96         int ret;
97         int actual;
98
99         ret = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), data, len,
100                         &actual, 5000);
101
102         if (ret < 0) {
103                 printk(KERN_ERR "Error : usb_bulk_msg(recv) ( result = %d )\n",
104                         ret);
105                 return ret;
106         }
107         return 0;
108 }
109
110 static int download_image(struct usb_device *usbdev, struct file *filp,
111                                 loff_t *pos, u32 img_len, u32 magic_num)
112 {
113         struct dn_header h;
114         int ret = 0;
115         u32 size;
116         int len, readn;
117
118         size = (img_len + DOWNLOAD_SIZE - 1) & ~(DOWNLOAD_SIZE - 1);
119         h.magic_num = DH2B(magic_num);
120         h.file_size = DH2B(size);
121
122         ret = gdm_wibro_send(usbdev, &h, sizeof(h));
123         if (ret < 0)
124                 goto out;
125
126         readn = 0;
127         while ((len = filp->f_op->read(filp, tx_buf, DOWNLOAD_SIZE, pos))) {
128
129                 if (len < 0) {
130                         ret = -1;
131                         goto out;
132                 }
133                 readn += len;
134
135                 ret = gdm_wibro_send(usbdev, tx_buf, DOWNLOAD_SIZE);
136                 if (ret < 0)
137                         goto out;
138                 if (readn >= img_len)
139                         break;
140         }
141
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);
145                 ret = -EIO;
146         }
147 out:
148
149         return ret;
150 }
151
152 int usb_boot(struct usb_device *usbdev, u16 pid)
153 {
154         int i, ret = 0;
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;
160         loff_t pos = 0;
161         char *img_name = UIMG_PATH;
162         int len;
163
164         tx_buf = kmalloc(DOWNLOAD_SIZE, GFP_KERNEL);
165         if (tx_buf == NULL) {
166                 printk(KERN_ERR "Error: kmalloc\n");
167                 return -ENOMEM;
168         }
169
170         fs = get_fs();
171         set_fs(get_ds());
172
173         filp = filp_open(img_name, O_RDONLY | O_LARGEFILE, 0);
174         if (IS_ERR(filp)) {
175                 printk(KERN_ERR "Can't find %s.\n", img_name);
176                 ret = PTR_ERR(filp);
177                 goto restore_fs;
178         }
179
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);
183                 ret = -EINVAL;
184                 goto out;
185         }
186
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");
190                 ret = -EIO;
191                 goto out;
192         }
193
194         array_le32_to_cpu((u32 *)&hdr, 19);
195 #if 0
196         if (hdr.magic_code != 0x10767fff) {
197                 printk(KERN_ERR "gdmwm: Invalid magic code 0x%08x\n",
198                         hdr.magic_code);
199                 ret = -EINVAL;
200                 goto out;
201         }
202 #endif
203         if (hdr.count > MAX_IMG_CNT) {
204                 printk(KERN_ERR "gdmwm: Too many images. %d\n", hdr.count);
205                 ret = -EINVAL;
206                 goto out;
207         }
208
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);
215                         ret = -EINVAL;
216                         goto out;
217                 }
218
219                 pos = hdr.offset[i];
220                 len = filp->f_op->read(filp, (u8 *)&fw_info, sizeof(fw_info),
221                                         &pos);
222                 if (len != sizeof(fw_info)) {
223                         printk(KERN_ERR "gdmwm: Cannot read the FW info.\n");
224                         ret = -EIO;
225                         goto out;
226                 }
227
228                 array_le32_to_cpu((u32 *)&fw_info, 8);
229 #if 0
230                 if ((fw_info.id & 0xfffff000) != 0x10767000) {
231                         printk(KERN_ERR "gdmwm: Invalid FW id. 0x%08x\n",
232                                 fw_info.id);
233                         ret = -EIO;
234                         goto out;
235                 }
236 #endif
237
238                 if ((fw_info.id & 0xffff) != pid)
239                         continue;
240
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);
244                 if (ret < 0)
245                         goto out;
246                 printk(KERN_INFO "GCT: Kernel download success.\n");
247
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);
251                 if (ret < 0)
252                         goto out;
253                 printk(KERN_INFO "GCT: Filesystem download success.\n");
254
255                 break;
256         }
257
258         if (i == hdr.count) {
259                 printk(KERN_ERR "Firmware for gsk%x is not installed.\n", pid);
260                 ret = -EINVAL;
261         }
262 out:
263         filp_close(filp, NULL);
264
265 restore_fs:
266         set_fs(fs);
267         kfree(tx_buf);
268         return ret;
269 }
270
271 /*#define GDM7205_PADDING               256 */
272 #define DOWNLOAD_CHUCK                  2048
273 #define KERNEL_TYPE_STRING              "linux"
274 #define FS_TYPE_STRING                  "rootfs"
275
276 static int em_wait_ack(struct usb_device *usbdev, int send_zlp)
277 {
278         int ack;
279         int ret = -1;
280
281         if (send_zlp) {
282                 /*Send ZLP*/
283                 ret = gdm_wibro_send(usbdev, NULL, 0);
284                 if (ret < 0)
285                         goto out;
286         }
287
288         /*Wait for ACK*/
289         ret = gdm_wibro_recv(usbdev, &ack, sizeof(ack));
290         if (ret < 0)
291                 goto out;
292 out:
293         return ret;
294 }
295
296 static int em_download_image(struct usb_device *usbdev, char *path,
297                                 char *type_string)
298 {
299         struct file *filp;
300         struct inode *inode;
301         static mm_segment_t fs;
302         char *buf = NULL;
303         loff_t pos = 0;
304         int ret = 0;
305         int len, readn = 0;
306         #if defined(GDM7205_PADDING)
307         const int pad_size = GDM7205_PADDING;
308         #else
309         const int pad_size = 0;
310         #endif
311
312         fs = get_fs();
313         set_fs(get_ds());
314
315         filp = filp_open(path, O_RDONLY | O_LARGEFILE, 0);
316         if (IS_ERR(filp)) {
317                 printk(KERN_ERR "Can't find %s.\n", path);
318                 set_fs(fs);
319                 ret = -ENOENT;
320                 goto restore_fs;
321         }
322
323         inode = filp->f_dentry->d_inode;
324         if (!S_ISREG(inode->i_mode)) {
325                 printk(KERN_ERR "Invalid file type: %s\n", path);
326                 ret = -EINVAL;
327                 goto out;
328         }
329
330         buf = kmalloc(DOWNLOAD_CHUCK + pad_size, GFP_KERNEL);
331         if (buf == NULL) {
332                 printk(KERN_ERR "Error: kmalloc\n");
333                 return -ENOMEM;
334         }
335
336         strcpy(buf+pad_size, type_string);
337         ret = gdm_wibro_send(usbdev, buf, strlen(type_string)+pad_size);
338         if (ret < 0)
339                 goto out;
340
341         while ((len = filp->f_op->read(filp, buf+pad_size, DOWNLOAD_CHUCK,
342                                         &pos))) {
343                 if (len < 0) {
344                         ret = -1;
345                         goto out;
346                 }
347                 readn += len;
348
349                 ret = gdm_wibro_send(usbdev, buf, len+pad_size);
350                 if (ret < 0)
351                         goto out;
352
353                 ret = em_wait_ack(usbdev, ((len+pad_size) % 512 == 0));
354                 if (ret < 0)
355                         goto out;
356         }
357
358         ret = em_wait_ack(usbdev, 1);
359         if (ret < 0)
360                 goto out;
361
362 out:
363         filp_close(filp, NULL);
364
365 restore_fs:
366         set_fs(fs);
367
368         kfree(buf);
369
370         return ret;
371 }
372
373 static int em_fw_reset(struct usb_device *usbdev)
374 {
375         int ret;
376
377         /*Send ZLP*/
378         ret = gdm_wibro_send(usbdev, NULL, 0);
379         return ret;
380 }
381
382 int usb_emergency(struct usb_device *usbdev)
383 {
384         int ret;
385
386         ret = em_download_image(usbdev, KERN_PATH, KERNEL_TYPE_STRING);
387         if (ret < 0)
388                 goto out;
389         printk(KERN_INFO "GCT Emergency: Kernel download success.\n");
390
391         ret = em_download_image(usbdev, FS_PATH, FS_TYPE_STRING);
392         if (ret < 0)
393                 goto out;
394         printk(KERN_INFO "GCT Emergency: Filesystem download success.\n");
395
396         ret = em_fw_reset(usbdev);
397 out:
398         return ret;
399 }