tizen 2.4 release
[kernel/u-boot-tm1.git] / drivers / usb / gadget / f_fastboot.c
1 /*
2  * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
3  *
4  * Copyright (C) 2003-2005,2008 David Brownell
5  * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6  * Copyright (C) 2008 Nokia Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 //#define DEBUG
23
24 #include <common.h>
25 #include <asm/errno.h>
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/gadget.h>
28 #include <linux/ctype.h>
29 #include <normal_mode.h>
30 #include "gadget_chips.h"
31
32
33 #define atomic_read
34 #define spin_lock(x)
35 #define spin_unlock(x)
36
37
38 unsigned packet_received, packet_sent;
39
40 #define DEV_CONFIG_CDC  1
41 #define GFP_ATOMIC ((gfp_t) 0)
42 #define GFP_KERNEL ((gfp_t) 0)
43
44
45 #define DRIVER_DESC             "fastboot Gadget"
46 /* Based on linux 2.6.27 version */
47 #define DRIVER_VERSION          "May Day 2005"
48
49 static const char shortname[] = "fastboot";
50 static const char driver_desc[] = DRIVER_DESC;
51
52 #define RX_EXTRA        20              /* guard against rx overflows */
53
54 /* CDC support the same host-chosen outgoing packet filters. */
55 #define DEFAULT_FILTER  (USB_CDC_PACKET_TYPE_BROADCAST \
56                         |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
57                         |USB_CDC_PACKET_TYPE_PROMISCUOUS \
58                         |USB_CDC_PACKET_TYPE_DIRECTED)
59
60 #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
61
62 /*-------------------------------------------------------------------------*/
63 struct fastboot_dev {
64         struct usb_gadget       *gadget;
65         struct usb_request      *req;           /* for control responses */
66         struct usb_request      *stat_req;      /* for cdc status */
67
68         u8                      config;
69         struct usb_ep           *in_ep, *out_ep;
70         const struct usb_endpoint_descriptor
71                                 *in, *out;
72
73         struct usb_request      *tx_req, *rx_req;
74
75         unsigned                suspended:1;
76         unsigned                network_started:1;
77 };
78
79 static struct fastboot_dev l_fbdev;
80 static struct usb_gadget_driver fastboot_driver;
81
82 /*-------------------------------------------------------------------------*/
83
84 #define DEFAULT_QLEN    2       /* double buffering by default */
85
86 /* peak bulk transfer bits-per-second */
87 #define HS_BPS          (13 * 512 * 8 * 1000 * 8)
88 #define FS_BPS          (19 *  64 * 1 * 1000 * 8)
89
90 #ifdef CONFIG_USB_GADGET_DUALSPEED
91 #define DEVSPEED        USB_SPEED_HIGH
92
93 /* for dual-speed hardware, use deeper queues at highspeed */
94 #define qlen(gadget) \
95         (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
96
97 static inline int BITRATE(struct usb_gadget *g)
98 {
99         return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
100 }
101
102 #else   /* full speed (low speed doesn't do bulk) */
103
104 #define qmult           1
105
106 #define DEVSPEED        USB_SPEED_FULL
107
108 #define qlen(gadget) DEFAULT_QLEN
109
110 static inline int BITRATE(struct usb_gadget *g)
111 {
112         return FS_BPS;
113 }
114 #endif
115
116
117 /*
118  * Thanks to NetChip Technologies for donating this product ID.
119  * It's for devices with only CDC Ethernet configurations.
120  */
121 #define FASTBOOT_VENDOR_NUM             0x18D1  /* Goolge VID */
122 #define FASTBOOT_PRODUCT_NUM            0x0FFF  /* Fastboot Product ID */
123 //#define FASTBOOT_PRODUCT_NUM          0x0C02  /* Linux-USB Ethernet Gadget */
124
125
126
127 /*
128  * Some systems will want different product identifers published in the
129  * device descriptor, either numbers or strings or both.  These string
130  * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
131  */
132
133 static ushort bcdDevice;
134
135 static char *iManufacturer = "Spreadtrum";
136
137 static char *iProduct;
138 static char *iSerialNumber = "20080823";
139
140 /*-------------------------------------------------------------------------*/
141
142 /*
143  * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
144  * ep0 implementation:  descriptors, config management, setup().
145  * also optional class-specific notification interrupt transfer.
146  */
147
148 /*
149  * DESCRIPTORS ... most are static, but strings and (full) configuration
150  * descriptors are built on demand.  For now we do either full CDC, or
151  * our simple subset.
152  */
153
154 #define STRING_MANUFACTURER             1
155 #define STRING_PRODUCT                  2
156 #define STRING_ETHADDR                  3
157 #define STRING_DATA                     4
158 #define STRING_CONTROL                  5
159 #define STRING_CDC                      7
160 #define STRING_SUBSET                   8
161 #define STRING_SERIALNUMBER             10
162
163 /* holds our biggest descriptor */
164 #define USB_BUFSIZ      256
165
166 /*
167  * This device advertises one configuration, eth_config,
168  * on hardware supporting at least two configs.
169  *
170  * FIXME define some higher-powered configurations to make it easier
171  * to recharge batteries ...
172  */
173
174 #define DEV_CONFIG_VALUE        1       /* cdc or subset */
175
176 static struct usb_device_descriptor
177 device_desc = {
178         .bLength =              sizeof device_desc,
179         .bDescriptorType =      USB_DT_DEVICE,
180
181         .bcdUSB =               __constant_cpu_to_le16(0x0200),
182
183         .bDeviceClass =         USB_CLASS_VENDOR_SPEC,
184         .bDeviceSubClass =      0,
185         .bDeviceProtocol =      0,
186
187         .idVendor =             __constant_cpu_to_le16(FASTBOOT_VENDOR_NUM),
188         .idProduct =            __constant_cpu_to_le16(FASTBOOT_PRODUCT_NUM),
189         .iManufacturer =        STRING_MANUFACTURER,
190         .iProduct =             STRING_PRODUCT,
191         .bNumConfigurations =   1,
192 };
193
194 static struct usb_config_descriptor
195 fastboot_config = {
196         .bLength =              sizeof fastboot_config,
197         .bDescriptorType =      USB_DT_CONFIG,
198
199         /* compute wTotalLength on the fly */
200         .bNumInterfaces =       1,
201         .bConfigurationValue =  DEV_CONFIG_VALUE,
202         .iConfiguration =       STRING_CDC,
203         .bmAttributes =         USB_CONFIG_ATT_ONE,// | USB_CONFIG_ATT_SELFPOWER,
204         .bMaxPower =            0xfa,
205 };
206
207
208
209 /* ... but the "real" data interface has two bulk endpoints */
210
211 static const struct usb_interface_descriptor
212 data_intf = {
213         .bLength =              sizeof data_intf,
214         .bDescriptorType =      USB_DT_INTERFACE,
215
216         .bInterfaceNumber =     0,
217         .bAlternateSetting =    0,
218         .bNumEndpoints =        2,
219         .bInterfaceClass =      USB_CLASS_VENDOR_SPEC,
220         .bInterfaceSubClass =   0x42,
221         .bInterfaceProtocol =   3,
222         .iInterface =           STRING_DATA,
223 };
224
225
226 static struct usb_endpoint_descriptor
227 fs_source_desc = {
228         .bLength =              USB_DT_ENDPOINT_SIZE,
229         .bDescriptorType =      USB_DT_ENDPOINT,
230
231         .bEndpointAddress =     USB_DIR_IN,
232         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
233 };
234
235 static struct usb_endpoint_descriptor
236 fs_sink_desc = {
237         .bLength =              USB_DT_ENDPOINT_SIZE,
238         .bDescriptorType =      USB_DT_ENDPOINT,
239
240         .bEndpointAddress =     USB_DIR_OUT,
241         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
242 };
243
244 static const struct usb_descriptor_header *fs_fastboot_function[] = {
245 //      (struct usb_descriptor_header *) &otg_descriptor,
246 #ifdef DEV_CONFIG_CDC
247         /* "cdc" mode descriptors */
248 //      (struct usb_descriptor_header *) &control_intf,
249 //      (struct usb_descriptor_header *) &header_desc,
250 //      (struct usb_descriptor_header *) &union_desc,
251 //      (struct usb_descriptor_header *) &ether_desc,
252         /* NOTE: status endpoint may need to be removed */
253 //      (struct usb_descriptor_header *) &fs_status_desc,
254         /* data interface, with altsetting */
255         (struct usb_descriptor_header *) &data_intf,
256         (struct usb_descriptor_header *) &fs_source_desc,
257         (struct usb_descriptor_header *) &fs_sink_desc,
258         NULL,
259 #endif /* DEV_CONFIG_CDC */
260 };
261
262
263 /*
264  * usb 2.0 devices need to expose both high speed and full speed
265  * descriptors, unless they only run at full speed.
266  */
267
268 static struct usb_endpoint_descriptor
269 hs_source_desc = {
270         .bLength =              USB_DT_ENDPOINT_SIZE,
271         .bDescriptorType =      USB_DT_ENDPOINT,
272
273         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
274         .wMaxPacketSize =       __constant_cpu_to_le16(512),
275 };
276
277 static struct usb_endpoint_descriptor
278 hs_sink_desc = {
279         .bLength =              USB_DT_ENDPOINT_SIZE,
280         .bDescriptorType =      USB_DT_ENDPOINT,
281
282         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
283         .wMaxPacketSize =       __constant_cpu_to_le16(512),
284 };
285
286 static struct usb_qualifier_descriptor
287 dev_qualifier = {
288         .bLength =              sizeof dev_qualifier,
289         .bDescriptorType =      USB_DT_DEVICE_QUALIFIER,
290
291         .bcdUSB =               __constant_cpu_to_le16(0x0200),
292         .bDeviceClass =         USB_CLASS_VENDOR_SPEC,
293
294         .bNumConfigurations =   1,
295 };
296
297 static const struct usb_descriptor_header *hs_fastboot_function[] = {
298 //      (struct usb_descriptor_header *) &otg_descriptor,
299 #ifdef DEV_CONFIG_CDC
300         /* "cdc" mode descriptors */
301 //      (struct usb_descriptor_header *) &control_intf,
302 //      (struct usb_descriptor_header *) &header_desc,
303 //      (struct usb_descriptor_header *) &union_desc,
304 //      (struct usb_descriptor_header *) &ether_desc,
305         /* NOTE: status endpoint may need to be removed */
306 //      (struct usb_descriptor_header *) &hs_status_desc,
307         /* data interface, with altsetting */
308 //      (struct usb_descriptor_header *) &data_nop_intf,
309         (struct usb_descriptor_header *) &data_intf,
310         (struct usb_descriptor_header *) &hs_source_desc,
311         (struct usb_descriptor_header *) &hs_sink_desc,
312         NULL,
313 #endif /* DEV_CONFIG_CDC */
314 };
315
316
317 /* maxpacket and other transfer characteristics vary by speed. */
318 static inline struct usb_endpoint_descriptor *
319 ep_choose(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
320                 struct usb_endpoint_descriptor *fs)
321 {
322         if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
323                 return hs;
324         return fs;
325 }
326
327 /*-------------------------------------------------------------------------*/
328
329 /* descriptors that are built on-demand */
330
331 static char manufacturer[50];
332 static char product_desc[40] = DRIVER_DESC;
333 static char serial_number[SP09_MAX_SN_LEN];
334
335
336 /* static strings, in UTF-8 */
337 static struct usb_string                strings[] = {
338         { STRING_MANUFACTURER,  manufacturer, },
339         { STRING_PRODUCT,       product_desc, },
340         { STRING_SERIALNUMBER,  serial_number, },
341         { STRING_DATA,          "fastboot Data", },
342 #ifdef  DEV_CONFIG_CDC
343         { STRING_CDC,           "fastboot Ethernet", },
344         { STRING_CONTROL,       "fastboot Communications Control", },
345 #endif
346         {  }            /* end of list */
347 };
348
349 static struct usb_gadget_strings        stringtab = {
350         .language       = 0x0409,       /* en-us */
351         .strings        = strings,
352 };
353
354 /*============================================================================*/
355 static u8 control_req[USB_BUFSIZ]  __attribute__ ((aligned(4)));
356
357 /**
358  * strlcpy - Copy a %NUL terminated string into a sized buffer
359  * @dest: Where to copy the string to
360  * @src: Where to copy the string from
361  * @size: size of destination buffer
362  *
363  * Compatible with *BSD: the result is always a valid
364  * NUL-terminated string that fits in the buffer (unless,
365  * of course, the buffer size is zero). It does not pad
366  * out the result like strncpy() does.
367  */
368 size_t strlcpy(char *dest, const char *src, size_t size)
369 {
370         size_t ret = strlen(src);
371
372         if (size) {
373                 size_t len = (ret >= size) ? size - 1 : ret;
374                 memcpy(dest, src, len);
375                 dest[len] = '\0';
376         }
377         return ret;
378 }
379
380 /*============================================================================*/
381
382 /*
383  * one config, two interfaces:  control, data.
384  * complications: class descriptors, and an altsetting.
385  */
386 static int
387 config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
388 {
389         int                                     len;
390         const struct usb_config_descriptor      *config;
391         const struct usb_descriptor_header      **function;
392         int                                     hs = 0;
393
394         if (gadget_is_dualspeed(g)) {
395                 hs = (g->speed == USB_SPEED_HIGH);
396                 if (type == USB_DT_OTHER_SPEED_CONFIG)
397                         hs = !hs;
398         }
399 #define which_fn(t)     (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
400
401         debug("%s , type :%d, index %d\n", __func__, type, index);
402         if (index >= device_desc.bNumConfigurations)
403                 return -EINVAL;
404
405         config = &fastboot_config;
406         function = which_fn(fastboot);
407
408         /* for now, don't advertise srp-only devices */
409 /*
410         if (!is_otg)
411                 function++;
412 */
413         len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
414         if (len < 0)
415                 return len;
416         ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
417         return len;
418 }
419
420 /*-------------------------------------------------------------------------*/
421
422 static int alloc_requests(struct fastboot_dev *dev, unsigned n, gfp_t gfp_flags);
423
424 static int
425 set_fastboot_config(struct fastboot_dev *dev, gfp_t gfp_flags)
426 {
427         int                                     result = 0;
428         struct usb_gadget                       *gadget = dev->gadget;
429
430
431         dev->in = ep_choose(gadget, &hs_source_desc, &fs_source_desc);
432         dev->in_ep->driver_data = dev;
433
434         dev->out = ep_choose(gadget, &hs_sink_desc, &fs_sink_desc);
435         dev->out_ep->driver_data = dev;
436
437
438         result = usb_ep_enable(dev->in_ep, dev->in);
439         if (result != 0) {
440                 debug("enable %s --> %d\n",
441                         dev->in_ep->name, result);
442                 goto done;
443         }
444
445         result = usb_ep_enable(dev->out_ep, dev->out);
446         if (result != 0) {
447                 debug("enable %s --> %d\n",
448                         dev->out_ep->name, result);
449                 goto done;
450         }
451         
452 done:
453         if (result == 0)
454                 result = alloc_requests(dev, 1, gfp_flags);
455
456         /* on error, disable any endpoints  */
457         if (result < 0) {
458                 (void) usb_ep_disable(dev->in_ep);
459                 (void) usb_ep_disable(dev->out_ep);
460                 dev->in = NULL;
461                 dev->out = NULL;
462         }
463
464         /* caller is responsible for cleanup on error */
465         return result;
466 }
467
468 static void fastboot_reset_config(struct fastboot_dev *dev)
469 {
470         if (dev->config == 0)
471                 return;
472
473         debug("%s\n", __func__);
474
475         /*
476          * disable endpoints, forcing (synchronous) completion of
477          * pending i/o.  then free the requests.
478          */
479
480         if (dev->in) {
481                 usb_ep_disable(dev->in_ep);
482                 if (dev->tx_req) {
483                         usb_ep_free_request(dev->in_ep, dev->tx_req);
484                         dev->tx_req = NULL;
485                 }
486         }
487         if (dev->out) {
488                 usb_ep_disable(dev->out_ep);
489                 if (dev->rx_req) {
490                         usb_ep_free_request(dev->out_ep, dev->rx_req);
491                         dev->rx_req = NULL;
492                 }
493         }
494         dev->config = 0;
495 }
496
497 /*
498  * change our operational config.  must agree with the code
499  * that returns config descriptors, and altsetting code.
500  */
501 static int fastboot_set_config(struct fastboot_dev *dev, unsigned number,
502                                 gfp_t gfp_flags)
503 {
504         int                     result = 0;
505         struct usb_gadget       *gadget = dev->gadget;
506
507         fastboot_reset_config(dev);
508
509         switch (number) {
510         case DEV_CONFIG_VALUE:
511                 result = set_fastboot_config(dev, gfp_flags);
512                 break;
513         default:
514                 result = -EINVAL;
515                 /* FALL THROUGH */
516         case 0:
517                 break;
518         }
519
520         if (result) {
521                 if (number)
522                         fastboot_reset_config(dev);
523                 usb_gadget_vbus_draw(dev->gadget,
524                                 gadget_is_otg(dev->gadget) ? 8 : 100);
525         } else {
526                 char *speed;
527                 unsigned power;
528
529                 power = 2 * fastboot_config.bMaxPower;
530                 usb_gadget_vbus_draw(dev->gadget, power);
531
532                 switch (gadget->speed) {
533                 case USB_SPEED_FULL:
534                         speed = "full"; break;
535 #ifdef CONFIG_USB_GADGET_DUALSPEED
536                 case USB_SPEED_HIGH:
537                         speed = "high"; break;
538 #endif
539                 default:
540                         speed = "?"; break;
541                 }
542
543                 dev->config = number;
544                 debug("%s speed config #%d: %d mA, %s\n",
545                                 speed, number, power, driver_desc);
546         }
547         return result;
548 }
549
550
551 /*-------------------------------------------------------------------------*/
552
553 static void fastboot_setup_complete(struct usb_ep *ep, struct usb_request *req)
554 {
555         if (req->status || req->actual != req->length)
556                 debug("setup complete --> %d, %d/%d\n",
557                                 req->status, req->actual, req->length);
558 }
559
560 /*
561  * The setup() callback implements all the ep0 functionality that's not
562  * handled lower down.  CDC has a number of less-common features:
563  *
564  *  - two interfaces:  control, and ethernet data
565  *  - Ethernet data interface has two altsettings:  default, and active
566  *  - class-specific descriptors for the control interface
567  *  - class-specific control requests
568  */
569 static int
570 fastboot_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
571 {
572         struct fastboot_dev             *dev = get_gadget_data(gadget);
573         struct usb_request      *req = dev->req;
574         int                     value = -EOPNOTSUPP;
575         u16                     wIndex = le16_to_cpu(ctrl->wIndex);
576         u16                     wValue = le16_to_cpu(ctrl->wValue);
577         u16                     wLength = le16_to_cpu(ctrl->wLength);
578
579         /*
580          * descriptors just go into the pre-allocated ep0 buffer,
581          * while config change events may enable network traffic.
582          */
583
584         debug("%s\n", __func__);
585
586         req->complete = fastboot_setup_complete;
587         switch (ctrl->bRequest) {
588
589         case USB_REQ_GET_DESCRIPTOR:
590                 if (ctrl->bRequestType != USB_DIR_IN)
591                         break;
592                 switch (wValue >> 8) {
593
594                 case USB_DT_DEVICE:
595                         value = min(wLength, (u16) sizeof device_desc);
596                         memcpy(req->buf, &device_desc, value);
597                         break;
598                 case USB_DT_DEVICE_QUALIFIER:
599                         if (!gadget_is_dualspeed(gadget))
600                                 break;
601                         value = min(wLength, (u16) sizeof dev_qualifier);
602                         memcpy(req->buf, &dev_qualifier, value);
603                         break;
604
605                 case USB_DT_OTHER_SPEED_CONFIG:
606                         if (!gadget_is_dualspeed(gadget))
607                                 break;
608                         /* FALLTHROUGH */
609                 case USB_DT_CONFIG:
610                         value = config_buf(gadget, req->buf,
611                                         wValue >> 8,
612                                         wValue & 0xff,
613                                         gadget_is_otg(gadget));
614                         if (value >= 0)
615                                 value = min(wLength, (u16) value);
616                         break;
617
618                 case USB_DT_STRING:
619                         value = usb_gadget_get_string(&stringtab,
620                                         wValue & 0xff, req->buf);
621
622                         if (value >= 0)
623                                 value = min(wLength, (u16) value);
624
625                         break;
626                 }
627                 break;
628
629         case USB_REQ_SET_CONFIGURATION:
630                 if (ctrl->bRequestType != 0)
631                         break;
632                 value = fastboot_set_config(dev, wValue, GFP_ATOMIC);
633                 l_fbdev.network_started = 1;
634                 break;
635         case USB_REQ_GET_CONFIGURATION:
636                 if (ctrl->bRequestType != USB_DIR_IN)
637                         break;
638                 *(u8 *)req->buf = dev->config;
639                 value = min(wLength, (u16) 1);
640                 break;
641
642         case USB_REQ_SET_INTERFACE:
643                 if (ctrl->bRequestType != USB_RECIP_INTERFACE
644                                 || !dev->config
645                                 || wIndex > 1)
646                         break;
647                 /*
648                  * FIXME this is wrong, as is the assumption that
649                  * all non-PXA hardware talks real CDC ...
650                  */
651                 debug("set_interface ignored!\n");
652
653
654 done_set_intf:
655                 break;
656         case USB_REQ_GET_INTERFACE:
657                 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
658                                 || !dev->config
659                                 || wIndex > 1)
660                         break;
661         
662
663                 /* for CDC, iff carrier is on, data interface is active. */
664                 if (wIndex != 1)
665                         *(u8 *)req->buf = 0;
666                 else {
667                         /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
668                         /* carrier always ok ...*/
669                         *(u8 *)req->buf = 1 ;
670                 }
671                 value = min(wLength, (u16) 1);
672                 break;
673         default:
674                 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
675                         ctrl->bRequestType, ctrl->bRequest,
676                         wValue, wIndex, wLength);
677         }
678
679         /* respond with data transfer before status phase? */
680         if (value >= 0) {
681                 debug("respond with data transfer before status phase\n");
682                 req->length = value;
683                 req->zero = value < wLength
684                                 && (value % gadget->ep0->maxpacket) == 0;
685                 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
686                 if (value < 0) {
687                         debug("ep_queue --> %d\n", value);
688                         req->status = 0;
689                         fastboot_setup_complete(gadget->ep0, req);
690                 }
691         }
692
693         /* host either stalls (value < 0) or reports success */
694         return value;
695 }
696
697 /*-------------------------------------------------------------------------*/
698 #if 0
699 static void rx_complete(struct usb_ep *ep, struct usb_request *req);
700
701 static int rx_submit(struct fastboot_dev *dev, struct usb_request *req,
702                                 gfp_t gfp_flags)
703 {
704         int                     retval = -ENOMEM;
705         size_t                  size;
706
707         /*
708          * Padding up to RX_EXTRA handles minor disagreements with host.
709          * Normally we use the USB "terminate on short read" convention;
710          * so allow up to (N*maxpacket), since that memory is normally
711          * already allocated.  Some hardware doesn't deal well with short
712          * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
713          * byte off the end (to force hardware errors on overflow).
714          */
715
716         debug("%s\n", __func__);
717
718         size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
719         size += dev->out_ep->maxpacket - 1;
720         size -= size % dev->out_ep->maxpacket;
721
722         /*
723          * Some platforms perform better when IP packets are aligned,
724          * but on at least one, checksumming fails otherwise.
725          */
726
727         req->buf = (u8 *) NetRxPackets[0];
728         req->length = size;
729         req->complete = rx_complete;
730
731         retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
732
733         if (retval)
734                 error("rx submit --> %d", retval);
735
736         return retval;
737 }
738
739 static void rx_complete(struct usb_ep *ep, struct usb_request *req)
740 {
741         struct fastboot_dev     *dev = ep->driver_data;
742
743         debug("%s: status %d\n", __func__, req->status);
744
745         packet_received = 1;
746 }
747
748 #endif
749 static int alloc_requests(struct fastboot_dev *dev, unsigned n, gfp_t gfp_flags)
750 {
751
752 #if 0
753         dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
754
755         if (!dev->tx_req)
756                 goto fail1;
757
758         dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
759
760         if (!dev->rx_req)
761                 goto fail2;
762
763         return 0;
764
765 fail2:
766         usb_ep_free_request(dev->in_ep, dev->tx_req);
767 fail1:
768         error("can't alloc requests");
769         return -1;
770 #endif
771        return 0;
772 }
773
774 static void tx_complete(struct usb_ep *ep, struct usb_request *req)
775 {
776         debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
777         packet_sent = 1;
778 }
779
780 static void fastboot_unbind(struct usb_gadget *gadget)
781 {
782         struct fastboot_dev *dev = get_gadget_data(gadget);
783
784         debug("%s...\n", __func__);
785
786         /* we've already been disconnected ... no i/o is active */
787         if (dev->req) {
788                 usb_ep_free_request(gadget->ep0, dev->req);
789                 dev->req = NULL;
790         }
791 #if 0
792         if (dev->tx_req) {
793                 usb_ep_free_request(dev->in_ep, dev->tx_req);
794                 dev->tx_req = NULL;
795         }
796
797         if (dev->rx_req) {
798                 usb_ep_free_request(dev->out_ep, dev->rx_req);
799                 dev->rx_req = NULL;
800         }
801 #endif
802         set_gadget_data(gadget, NULL);
803 }
804
805 static void fastboot_disconnect(struct usb_gadget *gadget)
806 {
807         fastboot_reset_config(get_gadget_data(gadget));
808 }
809
810 static void fastboot_suspend(struct usb_gadget *gadget)
811 {
812         /* Not used */
813 }
814
815 static void fastboot_resume(struct usb_gadget *gadget)
816 {
817         /* Not used */
818 }
819
820 /*-------------------------------------------------------------------------*/
821
822 static int fastboot_bind(struct usb_gadget *gadget)
823 {
824         struct fastboot_dev             *dev = &l_fbdev;
825         u8                      cdc = 1, zlp = 1;
826         struct usb_ep           *in_ep, *out_ep;
827         int                     gcnum;
828         u8                      tmp[7];
829
830         debug("%s controller :%s recognized\n", __func__, gadget->name);
831         gcnum = usb_gadget_controller_number(gadget);
832         if (gcnum >= 0)
833                 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
834         else {
835                 /*
836                  * can't assume CDC works.  don't want to default to
837                  * anything less functional on CDC-capable hardware,
838                  * so we fail in this case.
839                  */
840                 error("controller '%s' not recognized",
841                         gadget->name);
842                 return -ENODEV;
843         }
844
845         if (bcdDevice)
846                 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
847         if (iManufacturer)
848                 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
849         if (iProduct)
850                 strlcpy(product_desc, iProduct, sizeof product_desc);
851
852         iSerialNumber = get_product_sn();
853         device_desc.iSerialNumber = STRING_SERIALNUMBER,
854         strlcpy(serial_number, iSerialNumber, sizeof serial_number);
855
856         /* all we really need is bulk IN/OUT */
857         usb_ep_autoconfig_reset(gadget);
858         in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
859         if (!in_ep) {
860 autoconf_fail:
861                 error("can't autoconfigure on %s\n",
862                         gadget->name);
863                 return -ENODEV;
864         }
865         in_ep->driver_data = in_ep;     /* claim */
866
867         out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
868         if (!out_ep)
869                 goto autoconf_fail;
870         out_ep->driver_data = out_ep;   /* claim */
871
872
873         device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
874         usb_gadget_set_selfpowered(gadget);
875
876         if (gadget_is_dualspeed(gadget)) {
877                 
878                 /* assumes ep0 uses the same value for both speeds ... */
879                 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
880
881                 /* and that all endpoints are dual-speed */
882                 hs_source_desc.bEndpointAddress =
883                                 fs_source_desc.bEndpointAddress;
884                 hs_sink_desc.bEndpointAddress =
885                                 fs_sink_desc.bEndpointAddress;
886         }
887
888         dev->network_started = 0;
889         dev->in_ep = in_ep;
890         dev->out_ep = out_ep;
891
892         
893         
894         /* preallocate control message data and buffer */
895         dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
896         if (!dev->req)
897                 goto fail;
898         dev->req->buf = control_req;
899         dev->req->complete = fastboot_setup_complete;
900
901         /* ... and maybe likewise for status transfer */
902
903         /* finish hookup to lower layer ... */
904         dev->gadget = gadget;
905         set_gadget_data(gadget, dev);
906         gadget->ep0->driver_data = dev;
907         
908         debug("bind controller with the driver\n");
909         /*
910          * two kinds of host-initiated state changes:
911          *  - iff DATA transfer is active, carrier is "on"
912          *  - tx queueing enabled if open *and* carrier is "on"
913          */
914         return 0;
915
916 fail:
917         error("%s failed", __func__);
918         fastboot_unbind(gadget);
919         return -ENOMEM;
920 }
921
922 static int usb_fastboot_start(void)
923 {
924         struct fastboot_dev *dev = &l_fbdev;
925
926         while(!dev->network_started)
927                 usb_gadget_handle_interrupts();
928
929 /*
930         if (packet_received) {
931                 debug("%s: packet received\n", __func__);
932                 if (dev->rx_req) {
933                         NetReceive(NetRxPackets[0], dev->rx_req->length);
934                         packet_received = 0;
935
936                         rx_submit(dev, dev->rx_req, 0);
937                 } else
938                         error("dev->rx_req invalid");
939         }
940 */
941         return 0;
942 }
943
944 static struct usb_gadget_driver fastboot_driver = {
945         .speed          = DEVSPEED,
946
947         .bind           = fastboot_bind,
948         .unbind         = fastboot_unbind,
949
950         .setup          = fastboot_setup,
951         .disconnect     = fastboot_disconnect,
952
953         .suspend        = fastboot_suspend,
954         .resume         = fastboot_resume,
955 };
956
957 #define mdelay(n)       udelay((n)*1000)
958
959 #define BASE_ADDR       0x00000000
960 #define DEFAULT_CMDLINE "mem=128M console=null";
961
962 #define TAGS_ADDR       (BASE_ADDR + 0x00000100)
963 #define KERNEL_ADDR     (BASE_ADDR + 0x00800000)
964 #define RAMDISK_ADDR    (BASE_ADDR + 0x01000000)
965 //(BASE_ADDR + 0x02000000)
966
967 extern int fastboot_init(void *base, unsigned size, struct usb_ep * in, struct usb_ep *out);
968
969 int usb_fastboot_initialize(void)
970 {
971         int status = 0;
972
973 /*
974         fastboot_register("boot", cmd_boot);
975         fastboot_register("erase:", cmd_erase);
976         fastboot_register("flash:", cmd_flash);
977         fastboot_register("continue", cmd_continue);
978         fastboot_publish("product", "swordfish");
979         fastboot_publish("kernel", "lk");
980 */
981
982         status = usb_gadget_register_driver(&fastboot_driver);
983         mdelay(20);
984         usb_fastboot_start();
985
986         fastboot_init((void*) SCRATCH_ADDR, FB_DOWNLOAD_BUF_SIZE, l_fbdev.in_ep, l_fbdev.out_ep);
987         
988         if (status < 0)
989                 goto fail;
990
991         
992         return 0;
993
994 fail:
995         error("%s failed. error = %d", __func__, status);
996         return status;
997 }
998