[S390] Remove error checking from copy_oldmem_page()
[linux/fpc-iii.git] / drivers / usb / gadget / fusb300_udc.c
blobe593f2849fa9499bb0efcdb748b4f610e1d3b22a
1 /*
2 * Fusb300 UDC (USB gadget)
4 * Copyright (C) 2010 Faraday Technology Corp.
6 * Author : Yuan-hsin Chen <yhchen@faraday-tech.com>
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; version 2 of the License.
12 #include <linux/dma-mapping.h>
13 #include <linux/err.h>
14 #include <linux/interrupt.h>
15 #include <linux/io.h>
16 #include <linux/platform_device.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/gadget.h>
20 #include "fusb300_udc.h"
22 MODULE_DESCRIPTION("FUSB300 USB gadget driver");
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Yuan Hsin Chen <yhchen@faraday-tech.com>");
25 MODULE_ALIAS("platform:fusb300_udc");
27 #define DRIVER_VERSION "20 October 2010"
29 static const char udc_name[] = "fusb300_udc";
30 static const char * const fusb300_ep_name[] = {
31 "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7", "ep8", "ep9",
32 "ep10", "ep11", "ep12", "ep13", "ep14", "ep15"
35 static void done(struct fusb300_ep *ep, struct fusb300_request *req,
36 int status);
38 static void fusb300_enable_bit(struct fusb300 *fusb300, u32 offset,
39 u32 value)
41 u32 reg = ioread32(fusb300->reg + offset);
43 reg |= value;
44 iowrite32(reg, fusb300->reg + offset);
47 static void fusb300_disable_bit(struct fusb300 *fusb300, u32 offset,
48 u32 value)
50 u32 reg = ioread32(fusb300->reg + offset);
52 reg &= ~value;
53 iowrite32(reg, fusb300->reg + offset);
57 static void fusb300_ep_setting(struct fusb300_ep *ep,
58 struct fusb300_ep_info info)
60 ep->epnum = info.epnum;
61 ep->type = info.type;
64 static int fusb300_ep_release(struct fusb300_ep *ep)
66 if (!ep->epnum)
67 return 0;
68 ep->epnum = 0;
69 ep->stall = 0;
70 ep->wedged = 0;
71 return 0;
74 static void fusb300_set_fifo_entry(struct fusb300 *fusb300,
75 u32 ep)
77 u32 val = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
79 val &= ~FUSB300_EPSET1_FIFOENTRY_MSK;
80 val |= FUSB300_EPSET1_FIFOENTRY(FUSB300_FIFO_ENTRY_NUM);
81 iowrite32(val, fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
84 static void fusb300_set_start_entry(struct fusb300 *fusb300,
85 u8 ep)
87 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
88 u32 start_entry = fusb300->fifo_entry_num * FUSB300_FIFO_ENTRY_NUM;
90 reg &= ~FUSB300_EPSET1_START_ENTRY_MSK ;
91 reg |= FUSB300_EPSET1_START_ENTRY(start_entry);
92 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
93 if (fusb300->fifo_entry_num == FUSB300_MAX_FIFO_ENTRY) {
94 fusb300->fifo_entry_num = 0;
95 fusb300->addrofs = 0;
96 pr_err("fifo entry is over the maximum number!\n");
97 } else
98 fusb300->fifo_entry_num++;
101 /* set fusb300_set_start_entry first before fusb300_set_epaddrofs */
102 static void fusb300_set_epaddrofs(struct fusb300 *fusb300,
103 struct fusb300_ep_info info)
105 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
107 reg &= ~FUSB300_EPSET2_ADDROFS_MSK;
108 reg |= FUSB300_EPSET2_ADDROFS(fusb300->addrofs);
109 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
110 fusb300->addrofs += (info.maxpacket + 7) / 8 * FUSB300_FIFO_ENTRY_NUM;
113 static void ep_fifo_setting(struct fusb300 *fusb300,
114 struct fusb300_ep_info info)
116 fusb300_set_fifo_entry(fusb300, info.epnum);
117 fusb300_set_start_entry(fusb300, info.epnum);
118 fusb300_set_epaddrofs(fusb300, info);
121 static void fusb300_set_eptype(struct fusb300 *fusb300,
122 struct fusb300_ep_info info)
124 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
126 reg &= ~FUSB300_EPSET1_TYPE_MSK;
127 reg |= FUSB300_EPSET1_TYPE(info.type);
128 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
131 static void fusb300_set_epdir(struct fusb300 *fusb300,
132 struct fusb300_ep_info info)
134 u32 reg;
136 if (!info.dir_in)
137 return;
138 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
139 reg &= ~FUSB300_EPSET1_DIR_MSK;
140 reg |= FUSB300_EPSET1_DIRIN;
141 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
144 static void fusb300_set_ep_active(struct fusb300 *fusb300,
145 u8 ep)
147 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
149 reg |= FUSB300_EPSET1_ACTEN;
150 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
153 static void fusb300_set_epmps(struct fusb300 *fusb300,
154 struct fusb300_ep_info info)
156 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
158 reg &= ~FUSB300_EPSET2_MPS_MSK;
159 reg |= FUSB300_EPSET2_MPS(info.maxpacket);
160 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
163 static void fusb300_set_interval(struct fusb300 *fusb300,
164 struct fusb300_ep_info info)
166 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
168 reg &= ~FUSB300_EPSET1_INTERVAL(0x7);
169 reg |= FUSB300_EPSET1_INTERVAL(info.interval);
170 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
173 static void fusb300_set_bwnum(struct fusb300 *fusb300,
174 struct fusb300_ep_info info)
176 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
178 reg &= ~FUSB300_EPSET1_BWNUM(0x3);
179 reg |= FUSB300_EPSET1_BWNUM(info.bw_num);
180 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
183 static void set_ep_reg(struct fusb300 *fusb300,
184 struct fusb300_ep_info info)
186 fusb300_set_eptype(fusb300, info);
187 fusb300_set_epdir(fusb300, info);
188 fusb300_set_epmps(fusb300, info);
190 if (info.interval)
191 fusb300_set_interval(fusb300, info);
193 if (info.bw_num)
194 fusb300_set_bwnum(fusb300, info);
196 fusb300_set_ep_active(fusb300, info.epnum);
199 static int config_ep(struct fusb300_ep *ep,
200 const struct usb_endpoint_descriptor *desc)
202 struct fusb300 *fusb300 = ep->fusb300;
203 struct fusb300_ep_info info;
205 ep->desc = desc;
207 info.interval = 0;
208 info.addrofs = 0;
209 info.bw_num = 0;
211 info.type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
212 info.dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
213 info.maxpacket = usb_endpoint_maxp(desc);
214 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
216 if ((info.type == USB_ENDPOINT_XFER_INT) ||
217 (info.type == USB_ENDPOINT_XFER_ISOC)) {
218 info.interval = desc->bInterval;
219 if (info.type == USB_ENDPOINT_XFER_ISOC)
220 info.bw_num = ((desc->wMaxPacketSize & 0x1800) >> 11);
223 ep_fifo_setting(fusb300, info);
225 set_ep_reg(fusb300, info);
227 fusb300_ep_setting(ep, info);
229 fusb300->ep[info.epnum] = ep;
231 return 0;
234 static int fusb300_enable(struct usb_ep *_ep,
235 const struct usb_endpoint_descriptor *desc)
237 struct fusb300_ep *ep;
239 ep = container_of(_ep, struct fusb300_ep, ep);
241 if (ep->fusb300->reenum) {
242 ep->fusb300->fifo_entry_num = 0;
243 ep->fusb300->addrofs = 0;
244 ep->fusb300->reenum = 0;
247 return config_ep(ep, desc);
250 static int fusb300_disable(struct usb_ep *_ep)
252 struct fusb300_ep *ep;
253 struct fusb300_request *req;
254 unsigned long flags;
256 ep = container_of(_ep, struct fusb300_ep, ep);
258 BUG_ON(!ep);
260 while (!list_empty(&ep->queue)) {
261 req = list_entry(ep->queue.next, struct fusb300_request, queue);
262 spin_lock_irqsave(&ep->fusb300->lock, flags);
263 done(ep, req, -ECONNRESET);
264 spin_unlock_irqrestore(&ep->fusb300->lock, flags);
267 return fusb300_ep_release(ep);
270 static struct usb_request *fusb300_alloc_request(struct usb_ep *_ep,
271 gfp_t gfp_flags)
273 struct fusb300_request *req;
275 req = kzalloc(sizeof(struct fusb300_request), gfp_flags);
276 if (!req)
277 return NULL;
278 INIT_LIST_HEAD(&req->queue);
280 return &req->req;
283 static void fusb300_free_request(struct usb_ep *_ep, struct usb_request *_req)
285 struct fusb300_request *req;
287 req = container_of(_req, struct fusb300_request, req);
288 kfree(req);
291 static int enable_fifo_int(struct fusb300_ep *ep)
293 struct fusb300 *fusb300 = ep->fusb300;
295 if (ep->epnum) {
296 fusb300_enable_bit(fusb300, FUSB300_OFFSET_IGER0,
297 FUSB300_IGER0_EEPn_FIFO_INT(ep->epnum));
298 } else {
299 pr_err("can't enable_fifo_int ep0\n");
300 return -EINVAL;
303 return 0;
306 static int disable_fifo_int(struct fusb300_ep *ep)
308 struct fusb300 *fusb300 = ep->fusb300;
310 if (ep->epnum) {
311 fusb300_disable_bit(fusb300, FUSB300_OFFSET_IGER0,
312 FUSB300_IGER0_EEPn_FIFO_INT(ep->epnum));
313 } else {
314 pr_err("can't disable_fifo_int ep0\n");
315 return -EINVAL;
318 return 0;
321 static void fusb300_set_cxlen(struct fusb300 *fusb300, u32 length)
323 u32 reg;
325 reg = ioread32(fusb300->reg + FUSB300_OFFSET_CSR);
326 reg &= ~FUSB300_CSR_LEN_MSK;
327 reg |= FUSB300_CSR_LEN(length);
328 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_CSR);
331 /* write data to cx fifo */
332 static void fusb300_wrcxf(struct fusb300_ep *ep,
333 struct fusb300_request *req)
335 int i = 0;
336 u8 *tmp;
337 u32 data;
338 struct fusb300 *fusb300 = ep->fusb300;
339 u32 length = req->req.length - req->req.actual;
341 tmp = req->req.buf + req->req.actual;
343 if (length > SS_CTL_MAX_PACKET_SIZE) {
344 fusb300_set_cxlen(fusb300, SS_CTL_MAX_PACKET_SIZE);
345 for (i = (SS_CTL_MAX_PACKET_SIZE >> 2); i > 0; i--) {
346 data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16 |
347 *(tmp + 3) << 24;
348 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
349 tmp += 4;
351 req->req.actual += SS_CTL_MAX_PACKET_SIZE;
352 } else { /* length is less than max packet size */
353 fusb300_set_cxlen(fusb300, length);
354 for (i = length >> 2; i > 0; i--) {
355 data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16 |
356 *(tmp + 3) << 24;
357 printk(KERN_DEBUG " 0x%x\n", data);
358 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
359 tmp = tmp + 4;
361 switch (length % 4) {
362 case 1:
363 data = *tmp;
364 printk(KERN_DEBUG " 0x%x\n", data);
365 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
366 break;
367 case 2:
368 data = *tmp | *(tmp + 1) << 8;
369 printk(KERN_DEBUG " 0x%x\n", data);
370 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
371 break;
372 case 3:
373 data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16;
374 printk(KERN_DEBUG " 0x%x\n", data);
375 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
376 break;
377 default:
378 break;
380 req->req.actual += length;
384 static void fusb300_set_epnstall(struct fusb300 *fusb300, u8 ep)
386 fusb300_enable_bit(fusb300, FUSB300_OFFSET_EPSET0(ep),
387 FUSB300_EPSET0_STL);
390 static void fusb300_clear_epnstall(struct fusb300 *fusb300, u8 ep)
392 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET0(ep));
394 if (reg & FUSB300_EPSET0_STL) {
395 printk(KERN_DEBUG "EP%d stall... Clear!!\n", ep);
396 reg &= ~FUSB300_EPSET0_STL;
397 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET0(ep));
401 static void ep0_queue(struct fusb300_ep *ep, struct fusb300_request *req)
403 if (ep->fusb300->ep0_dir) { /* if IN */
404 if (req->req.length) {
405 fusb300_wrcxf(ep, req);
406 } else
407 printk(KERN_DEBUG "%s : req->req.length = 0x%x\n",
408 __func__, req->req.length);
409 if ((req->req.length == req->req.actual) ||
410 (req->req.actual < ep->ep.maxpacket))
411 done(ep, req, 0);
412 } else { /* OUT */
413 if (!req->req.length)
414 done(ep, req, 0);
415 else
416 fusb300_enable_bit(ep->fusb300, FUSB300_OFFSET_IGER1,
417 FUSB300_IGER1_CX_OUT_INT);
421 static int fusb300_queue(struct usb_ep *_ep, struct usb_request *_req,
422 gfp_t gfp_flags)
424 struct fusb300_ep *ep;
425 struct fusb300_request *req;
426 unsigned long flags;
427 int request = 0;
429 ep = container_of(_ep, struct fusb300_ep, ep);
430 req = container_of(_req, struct fusb300_request, req);
432 if (ep->fusb300->gadget.speed == USB_SPEED_UNKNOWN)
433 return -ESHUTDOWN;
435 spin_lock_irqsave(&ep->fusb300->lock, flags);
437 if (list_empty(&ep->queue))
438 request = 1;
440 list_add_tail(&req->queue, &ep->queue);
442 req->req.actual = 0;
443 req->req.status = -EINPROGRESS;
445 if (ep->desc == NULL) /* ep0 */
446 ep0_queue(ep, req);
447 else if (request && !ep->stall)
448 enable_fifo_int(ep);
450 spin_unlock_irqrestore(&ep->fusb300->lock, flags);
452 return 0;
455 static int fusb300_dequeue(struct usb_ep *_ep, struct usb_request *_req)
457 struct fusb300_ep *ep;
458 struct fusb300_request *req;
459 unsigned long flags;
461 ep = container_of(_ep, struct fusb300_ep, ep);
462 req = container_of(_req, struct fusb300_request, req);
464 spin_lock_irqsave(&ep->fusb300->lock, flags);
465 if (!list_empty(&ep->queue))
466 done(ep, req, -ECONNRESET);
467 spin_unlock_irqrestore(&ep->fusb300->lock, flags);
469 return 0;
472 static int fusb300_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedge)
474 struct fusb300_ep *ep;
475 struct fusb300 *fusb300;
476 unsigned long flags;
477 int ret = 0;
479 ep = container_of(_ep, struct fusb300_ep, ep);
481 fusb300 = ep->fusb300;
483 spin_lock_irqsave(&ep->fusb300->lock, flags);
485 if (!list_empty(&ep->queue)) {
486 ret = -EAGAIN;
487 goto out;
490 if (value) {
491 fusb300_set_epnstall(fusb300, ep->epnum);
492 ep->stall = 1;
493 if (wedge)
494 ep->wedged = 1;
495 } else {
496 fusb300_clear_epnstall(fusb300, ep->epnum);
497 ep->stall = 0;
498 ep->wedged = 0;
501 out:
502 spin_unlock_irqrestore(&ep->fusb300->lock, flags);
503 return ret;
506 static int fusb300_set_halt(struct usb_ep *_ep, int value)
508 return fusb300_set_halt_and_wedge(_ep, value, 0);
511 static int fusb300_set_wedge(struct usb_ep *_ep)
513 return fusb300_set_halt_and_wedge(_ep, 1, 1);
516 static void fusb300_fifo_flush(struct usb_ep *_ep)
520 static struct usb_ep_ops fusb300_ep_ops = {
521 .enable = fusb300_enable,
522 .disable = fusb300_disable,
524 .alloc_request = fusb300_alloc_request,
525 .free_request = fusb300_free_request,
527 .queue = fusb300_queue,
528 .dequeue = fusb300_dequeue,
530 .set_halt = fusb300_set_halt,
531 .fifo_flush = fusb300_fifo_flush,
532 .set_wedge = fusb300_set_wedge,
535 /*****************************************************************************/
536 static void fusb300_clear_int(struct fusb300 *fusb300, u32 offset,
537 u32 value)
539 iowrite32(value, fusb300->reg + offset);
542 static void fusb300_reset(void)
546 static void fusb300_set_cxstall(struct fusb300 *fusb300)
548 fusb300_enable_bit(fusb300, FUSB300_OFFSET_CSR,
549 FUSB300_CSR_STL);
552 static void fusb300_set_cxdone(struct fusb300 *fusb300)
554 fusb300_enable_bit(fusb300, FUSB300_OFFSET_CSR,
555 FUSB300_CSR_DONE);
558 /* read data from cx fifo */
559 void fusb300_rdcxf(struct fusb300 *fusb300,
560 u8 *buffer, u32 length)
562 int i = 0;
563 u8 *tmp;
564 u32 data;
566 tmp = buffer;
568 for (i = (length >> 2); i > 0; i--) {
569 data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
570 printk(KERN_DEBUG " 0x%x\n", data);
571 *tmp = data & 0xFF;
572 *(tmp + 1) = (data >> 8) & 0xFF;
573 *(tmp + 2) = (data >> 16) & 0xFF;
574 *(tmp + 3) = (data >> 24) & 0xFF;
575 tmp = tmp + 4;
578 switch (length % 4) {
579 case 1:
580 data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
581 printk(KERN_DEBUG " 0x%x\n", data);
582 *tmp = data & 0xFF;
583 break;
584 case 2:
585 data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
586 printk(KERN_DEBUG " 0x%x\n", data);
587 *tmp = data & 0xFF;
588 *(tmp + 1) = (data >> 8) & 0xFF;
589 break;
590 case 3:
591 data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
592 printk(KERN_DEBUG " 0x%x\n", data);
593 *tmp = data & 0xFF;
594 *(tmp + 1) = (data >> 8) & 0xFF;
595 *(tmp + 2) = (data >> 16) & 0xFF;
596 break;
597 default:
598 break;
602 static void fusb300_rdfifo(struct fusb300_ep *ep,
603 struct fusb300_request *req,
604 u32 length)
606 int i = 0;
607 u8 *tmp;
608 u32 data, reg;
609 struct fusb300 *fusb300 = ep->fusb300;
611 tmp = req->req.buf + req->req.actual;
612 req->req.actual += length;
614 if (req->req.actual > req->req.length)
615 printk(KERN_DEBUG "req->req.actual > req->req.length\n");
617 for (i = (length >> 2); i > 0; i--) {
618 data = ioread32(fusb300->reg +
619 FUSB300_OFFSET_EPPORT(ep->epnum));
620 *tmp = data & 0xFF;
621 *(tmp + 1) = (data >> 8) & 0xFF;
622 *(tmp + 2) = (data >> 16) & 0xFF;
623 *(tmp + 3) = (data >> 24) & 0xFF;
624 tmp = tmp + 4;
627 switch (length % 4) {
628 case 1:
629 data = ioread32(fusb300->reg +
630 FUSB300_OFFSET_EPPORT(ep->epnum));
631 *tmp = data & 0xFF;
632 break;
633 case 2:
634 data = ioread32(fusb300->reg +
635 FUSB300_OFFSET_EPPORT(ep->epnum));
636 *tmp = data & 0xFF;
637 *(tmp + 1) = (data >> 8) & 0xFF;
638 break;
639 case 3:
640 data = ioread32(fusb300->reg +
641 FUSB300_OFFSET_EPPORT(ep->epnum));
642 *tmp = data & 0xFF;
643 *(tmp + 1) = (data >> 8) & 0xFF;
644 *(tmp + 2) = (data >> 16) & 0xFF;
645 break;
646 default:
647 break;
650 do {
651 reg = ioread32(fusb300->reg + FUSB300_OFFSET_IGR1);
652 reg &= FUSB300_IGR1_SYNF0_EMPTY_INT;
653 if (i)
654 printk(KERN_INFO "sync fifo is not empty!\n");
655 i++;
656 } while (!reg);
659 static u8 fusb300_get_epnstall(struct fusb300 *fusb300, u8 ep)
661 u8 value;
662 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET0(ep));
664 value = reg & FUSB300_EPSET0_STL;
666 return value;
669 static u8 fusb300_get_cxstall(struct fusb300 *fusb300)
671 u8 value;
672 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_CSR);
674 value = (reg & FUSB300_CSR_STL) >> 1;
676 return value;
679 static void request_error(struct fusb300 *fusb300)
681 fusb300_set_cxstall(fusb300);
682 printk(KERN_DEBUG "request error!!\n");
685 static void get_status(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
686 __releases(fusb300->lock)
687 __acquires(fusb300->lock)
689 u8 ep;
690 u16 status = 0;
691 u16 w_index = ctrl->wIndex;
693 switch (ctrl->bRequestType & USB_RECIP_MASK) {
694 case USB_RECIP_DEVICE:
695 status = 1 << USB_DEVICE_SELF_POWERED;
696 break;
697 case USB_RECIP_INTERFACE:
698 status = 0;
699 break;
700 case USB_RECIP_ENDPOINT:
701 ep = w_index & USB_ENDPOINT_NUMBER_MASK;
702 if (ep) {
703 if (fusb300_get_epnstall(fusb300, ep))
704 status = 1 << USB_ENDPOINT_HALT;
705 } else {
706 if (fusb300_get_cxstall(fusb300))
707 status = 0;
709 break;
711 default:
712 request_error(fusb300);
713 return; /* exit */
716 fusb300->ep0_data = cpu_to_le16(status);
717 fusb300->ep0_req->buf = &fusb300->ep0_data;
718 fusb300->ep0_req->length = 2;
720 spin_unlock(&fusb300->lock);
721 fusb300_queue(fusb300->gadget.ep0, fusb300->ep0_req, GFP_KERNEL);
722 spin_lock(&fusb300->lock);
725 static void set_feature(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
727 u8 ep;
729 switch (ctrl->bRequestType & USB_RECIP_MASK) {
730 case USB_RECIP_DEVICE:
731 fusb300_set_cxdone(fusb300);
732 break;
733 case USB_RECIP_INTERFACE:
734 fusb300_set_cxdone(fusb300);
735 break;
736 case USB_RECIP_ENDPOINT: {
737 u16 w_index = le16_to_cpu(ctrl->wIndex);
739 ep = w_index & USB_ENDPOINT_NUMBER_MASK;
740 if (ep)
741 fusb300_set_epnstall(fusb300, ep);
742 else
743 fusb300_set_cxstall(fusb300);
744 fusb300_set_cxdone(fusb300);
746 break;
747 default:
748 request_error(fusb300);
749 break;
753 static void fusb300_clear_seqnum(struct fusb300 *fusb300, u8 ep)
755 fusb300_enable_bit(fusb300, FUSB300_OFFSET_EPSET0(ep),
756 FUSB300_EPSET0_CLRSEQNUM);
759 static void clear_feature(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
761 struct fusb300_ep *ep =
762 fusb300->ep[ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK];
764 switch (ctrl->bRequestType & USB_RECIP_MASK) {
765 case USB_RECIP_DEVICE:
766 fusb300_set_cxdone(fusb300);
767 break;
768 case USB_RECIP_INTERFACE:
769 fusb300_set_cxdone(fusb300);
770 break;
771 case USB_RECIP_ENDPOINT:
772 if (ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK) {
773 if (ep->wedged) {
774 fusb300_set_cxdone(fusb300);
775 break;
777 if (ep->stall) {
778 ep->stall = 0;
779 fusb300_clear_seqnum(fusb300, ep->epnum);
780 fusb300_clear_epnstall(fusb300, ep->epnum);
781 if (!list_empty(&ep->queue))
782 enable_fifo_int(ep);
785 fusb300_set_cxdone(fusb300);
786 break;
787 default:
788 request_error(fusb300);
789 break;
793 static void fusb300_set_dev_addr(struct fusb300 *fusb300, u16 addr)
795 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_DAR);
797 reg &= ~FUSB300_DAR_DRVADDR_MSK;
798 reg |= FUSB300_DAR_DRVADDR(addr);
800 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_DAR);
803 static void set_address(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
805 if (ctrl->wValue >= 0x0100)
806 request_error(fusb300);
807 else {
808 fusb300_set_dev_addr(fusb300, ctrl->wValue);
809 fusb300_set_cxdone(fusb300);
813 #define UVC_COPY_DESCRIPTORS(mem, src) \
814 do { \
815 const struct usb_descriptor_header * const *__src; \
816 for (__src = src; *__src; ++__src) { \
817 memcpy(mem, *__src, (*__src)->bLength); \
818 mem += (*__src)->bLength; \
820 } while (0)
822 static int setup_packet(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
824 u8 *p = (u8 *)ctrl;
825 u8 ret = 0;
826 u8 i = 0;
828 fusb300_rdcxf(fusb300, p, 8);
829 fusb300->ep0_dir = ctrl->bRequestType & USB_DIR_IN;
830 fusb300->ep0_length = ctrl->wLength;
832 /* check request */
833 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
834 switch (ctrl->bRequest) {
835 case USB_REQ_GET_STATUS:
836 get_status(fusb300, ctrl);
837 break;
838 case USB_REQ_CLEAR_FEATURE:
839 clear_feature(fusb300, ctrl);
840 break;
841 case USB_REQ_SET_FEATURE:
842 set_feature(fusb300, ctrl);
843 break;
844 case USB_REQ_SET_ADDRESS:
845 set_address(fusb300, ctrl);
846 break;
847 case USB_REQ_SET_CONFIGURATION:
848 fusb300_enable_bit(fusb300, FUSB300_OFFSET_DAR,
849 FUSB300_DAR_SETCONFG);
850 /* clear sequence number */
851 for (i = 1; i <= FUSB300_MAX_NUM_EP; i++)
852 fusb300_clear_seqnum(fusb300, i);
853 fusb300->reenum = 1;
854 ret = 1;
855 break;
856 default:
857 ret = 1;
858 break;
860 } else
861 ret = 1;
863 return ret;
866 static void done(struct fusb300_ep *ep, struct fusb300_request *req,
867 int status)
869 list_del_init(&req->queue);
871 /* don't modify queue heads during completion callback */
872 if (ep->fusb300->gadget.speed == USB_SPEED_UNKNOWN)
873 req->req.status = -ESHUTDOWN;
874 else
875 req->req.status = status;
877 spin_unlock(&ep->fusb300->lock);
878 req->req.complete(&ep->ep, &req->req);
879 spin_lock(&ep->fusb300->lock);
881 if (ep->epnum) {
882 disable_fifo_int(ep);
883 if (!list_empty(&ep->queue))
884 enable_fifo_int(ep);
885 } else
886 fusb300_set_cxdone(ep->fusb300);
889 static void fusb300_fill_idma_prdtbl(struct fusb300_ep *ep, dma_addr_t d,
890 u32 len)
892 u32 value;
893 u32 reg;
895 /* wait SW owner */
896 do {
897 reg = ioread32(ep->fusb300->reg +
898 FUSB300_OFFSET_EPPRD_W0(ep->epnum));
899 reg &= FUSB300_EPPRD0_H;
900 } while (reg);
902 iowrite32(d, ep->fusb300->reg + FUSB300_OFFSET_EPPRD_W1(ep->epnum));
904 value = FUSB300_EPPRD0_BTC(len) | FUSB300_EPPRD0_H |
905 FUSB300_EPPRD0_F | FUSB300_EPPRD0_L | FUSB300_EPPRD0_I;
906 iowrite32(value, ep->fusb300->reg + FUSB300_OFFSET_EPPRD_W0(ep->epnum));
908 iowrite32(0x0, ep->fusb300->reg + FUSB300_OFFSET_EPPRD_W2(ep->epnum));
910 fusb300_enable_bit(ep->fusb300, FUSB300_OFFSET_EPPRDRDY,
911 FUSB300_EPPRDR_EP_PRD_RDY(ep->epnum));
914 static void fusb300_wait_idma_finished(struct fusb300_ep *ep)
916 u32 reg;
918 do {
919 reg = ioread32(ep->fusb300->reg + FUSB300_OFFSET_IGR1);
920 if ((reg & FUSB300_IGR1_VBUS_CHG_INT) ||
921 (reg & FUSB300_IGR1_WARM_RST_INT) ||
922 (reg & FUSB300_IGR1_HOT_RST_INT) ||
923 (reg & FUSB300_IGR1_USBRST_INT)
925 goto IDMA_RESET;
926 reg = ioread32(ep->fusb300->reg + FUSB300_OFFSET_IGR0);
927 reg &= FUSB300_IGR0_EPn_PRD_INT(ep->epnum);
928 } while (!reg);
930 fusb300_clear_int(ep->fusb300, FUSB300_OFFSET_IGR0,
931 FUSB300_IGR0_EPn_PRD_INT(ep->epnum));
932 IDMA_RESET:
933 fusb300_clear_int(ep->fusb300, FUSB300_OFFSET_IGER0,
934 FUSB300_IGER0_EEPn_PRD_INT(ep->epnum));
937 static void fusb300_set_idma(struct fusb300_ep *ep,
938 struct fusb300_request *req)
940 dma_addr_t d;
942 d = dma_map_single(NULL, req->req.buf, req->req.length, DMA_TO_DEVICE);
944 if (dma_mapping_error(NULL, d)) {
945 printk(KERN_DEBUG "dma_mapping_error\n");
946 return;
949 dma_sync_single_for_device(NULL, d, req->req.length, DMA_TO_DEVICE);
951 fusb300_enable_bit(ep->fusb300, FUSB300_OFFSET_IGER0,
952 FUSB300_IGER0_EEPn_PRD_INT(ep->epnum));
954 fusb300_fill_idma_prdtbl(ep, d, req->req.length);
955 /* check idma is done */
956 fusb300_wait_idma_finished(ep);
958 dma_unmap_single(NULL, d, req->req.length, DMA_TO_DEVICE);
961 static void in_ep_fifo_handler(struct fusb300_ep *ep)
963 struct fusb300_request *req = list_entry(ep->queue.next,
964 struct fusb300_request, queue);
966 if (req->req.length)
967 fusb300_set_idma(ep, req);
968 done(ep, req, 0);
971 static void out_ep_fifo_handler(struct fusb300_ep *ep)
973 struct fusb300 *fusb300 = ep->fusb300;
974 struct fusb300_request *req = list_entry(ep->queue.next,
975 struct fusb300_request, queue);
976 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPFFR(ep->epnum));
977 u32 length = reg & FUSB300_FFR_BYCNT;
979 fusb300_rdfifo(ep, req, length);
981 /* finish out transfer */
982 if ((req->req.length == req->req.actual) || (length < ep->ep.maxpacket))
983 done(ep, req, 0);
986 static void check_device_mode(struct fusb300 *fusb300)
988 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_GCR);
990 switch (reg & FUSB300_GCR_DEVEN_MSK) {
991 case FUSB300_GCR_DEVEN_SS:
992 fusb300->gadget.speed = USB_SPEED_SUPER;
993 break;
994 case FUSB300_GCR_DEVEN_HS:
995 fusb300->gadget.speed = USB_SPEED_HIGH;
996 break;
997 case FUSB300_GCR_DEVEN_FS:
998 fusb300->gadget.speed = USB_SPEED_FULL;
999 break;
1000 default:
1001 fusb300->gadget.speed = USB_SPEED_UNKNOWN;
1002 break;
1004 printk(KERN_INFO "dev_mode = %d\n", (reg & FUSB300_GCR_DEVEN_MSK));
1008 static void fusb300_ep0out(struct fusb300 *fusb300)
1010 struct fusb300_ep *ep = fusb300->ep[0];
1011 u32 reg;
1013 if (!list_empty(&ep->queue)) {
1014 struct fusb300_request *req;
1016 req = list_first_entry(&ep->queue,
1017 struct fusb300_request, queue);
1018 if (req->req.length)
1019 fusb300_rdcxf(ep->fusb300, req->req.buf,
1020 req->req.length);
1021 done(ep, req, 0);
1022 reg = ioread32(fusb300->reg + FUSB300_OFFSET_IGER1);
1023 reg &= ~FUSB300_IGER1_CX_OUT_INT;
1024 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_IGER1);
1025 } else
1026 pr_err("%s : empty queue\n", __func__);
1029 static void fusb300_ep0in(struct fusb300 *fusb300)
1031 struct fusb300_request *req;
1032 struct fusb300_ep *ep = fusb300->ep[0];
1034 if ((!list_empty(&ep->queue)) && (fusb300->ep0_dir)) {
1035 req = list_entry(ep->queue.next,
1036 struct fusb300_request, queue);
1037 if (req->req.length)
1038 fusb300_wrcxf(ep, req);
1039 if ((req->req.length - req->req.actual) < ep->ep.maxpacket)
1040 done(ep, req, 0);
1041 } else
1042 fusb300_set_cxdone(fusb300);
1045 static void fusb300_grp2_handler(void)
1049 static void fusb300_grp3_handler(void)
1053 static void fusb300_grp4_handler(void)
1057 static void fusb300_grp5_handler(void)
1061 static irqreturn_t fusb300_irq(int irq, void *_fusb300)
1063 struct fusb300 *fusb300 = _fusb300;
1064 u32 int_grp1 = ioread32(fusb300->reg + FUSB300_OFFSET_IGR1);
1065 u32 int_grp1_en = ioread32(fusb300->reg + FUSB300_OFFSET_IGER1);
1066 u32 int_grp0 = ioread32(fusb300->reg + FUSB300_OFFSET_IGR0);
1067 u32 int_grp0_en = ioread32(fusb300->reg + FUSB300_OFFSET_IGER0);
1068 struct usb_ctrlrequest ctrl;
1069 u8 in;
1070 u32 reg;
1071 int i;
1073 spin_lock(&fusb300->lock);
1075 int_grp1 &= int_grp1_en;
1076 int_grp0 &= int_grp0_en;
1078 if (int_grp1 & FUSB300_IGR1_WARM_RST_INT) {
1079 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1080 FUSB300_IGR1_WARM_RST_INT);
1081 printk(KERN_INFO"fusb300_warmreset\n");
1082 fusb300_reset();
1085 if (int_grp1 & FUSB300_IGR1_HOT_RST_INT) {
1086 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1087 FUSB300_IGR1_HOT_RST_INT);
1088 printk(KERN_INFO"fusb300_hotreset\n");
1089 fusb300_reset();
1092 if (int_grp1 & FUSB300_IGR1_USBRST_INT) {
1093 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1094 FUSB300_IGR1_USBRST_INT);
1095 fusb300_reset();
1097 /* COMABT_INT has a highest priority */
1099 if (int_grp1 & FUSB300_IGR1_CX_COMABT_INT) {
1100 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1101 FUSB300_IGR1_CX_COMABT_INT);
1102 printk(KERN_INFO"fusb300_ep0abt\n");
1105 if (int_grp1 & FUSB300_IGR1_VBUS_CHG_INT) {
1106 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1107 FUSB300_IGR1_VBUS_CHG_INT);
1108 printk(KERN_INFO"fusb300_vbus_change\n");
1111 if (int_grp1 & FUSB300_IGR1_U3_EXIT_FAIL_INT) {
1112 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1113 FUSB300_IGR1_U3_EXIT_FAIL_INT);
1116 if (int_grp1 & FUSB300_IGR1_U2_EXIT_FAIL_INT) {
1117 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1118 FUSB300_IGR1_U2_EXIT_FAIL_INT);
1121 if (int_grp1 & FUSB300_IGR1_U1_EXIT_FAIL_INT) {
1122 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1123 FUSB300_IGR1_U1_EXIT_FAIL_INT);
1126 if (int_grp1 & FUSB300_IGR1_U2_ENTRY_FAIL_INT) {
1127 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1128 FUSB300_IGR1_U2_ENTRY_FAIL_INT);
1131 if (int_grp1 & FUSB300_IGR1_U1_ENTRY_FAIL_INT) {
1132 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1133 FUSB300_IGR1_U1_ENTRY_FAIL_INT);
1136 if (int_grp1 & FUSB300_IGR1_U3_EXIT_INT) {
1137 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1138 FUSB300_IGR1_U3_EXIT_INT);
1139 printk(KERN_INFO "FUSB300_IGR1_U3_EXIT_INT\n");
1142 if (int_grp1 & FUSB300_IGR1_U2_EXIT_INT) {
1143 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1144 FUSB300_IGR1_U2_EXIT_INT);
1145 printk(KERN_INFO "FUSB300_IGR1_U2_EXIT_INT\n");
1148 if (int_grp1 & FUSB300_IGR1_U1_EXIT_INT) {
1149 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1150 FUSB300_IGR1_U1_EXIT_INT);
1151 printk(KERN_INFO "FUSB300_IGR1_U1_EXIT_INT\n");
1154 if (int_grp1 & FUSB300_IGR1_U3_ENTRY_INT) {
1155 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1156 FUSB300_IGR1_U3_ENTRY_INT);
1157 printk(KERN_INFO "FUSB300_IGR1_U3_ENTRY_INT\n");
1158 fusb300_enable_bit(fusb300, FUSB300_OFFSET_SSCR1,
1159 FUSB300_SSCR1_GO_U3_DONE);
1162 if (int_grp1 & FUSB300_IGR1_U2_ENTRY_INT) {
1163 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1164 FUSB300_IGR1_U2_ENTRY_INT);
1165 printk(KERN_INFO "FUSB300_IGR1_U2_ENTRY_INT\n");
1168 if (int_grp1 & FUSB300_IGR1_U1_ENTRY_INT) {
1169 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1170 FUSB300_IGR1_U1_ENTRY_INT);
1171 printk(KERN_INFO "FUSB300_IGR1_U1_ENTRY_INT\n");
1174 if (int_grp1 & FUSB300_IGR1_RESM_INT) {
1175 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1176 FUSB300_IGR1_RESM_INT);
1177 printk(KERN_INFO "fusb300_resume\n");
1180 if (int_grp1 & FUSB300_IGR1_SUSP_INT) {
1181 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1182 FUSB300_IGR1_SUSP_INT);
1183 printk(KERN_INFO "fusb300_suspend\n");
1186 if (int_grp1 & FUSB300_IGR1_HS_LPM_INT) {
1187 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1188 FUSB300_IGR1_HS_LPM_INT);
1189 printk(KERN_INFO "fusb300_HS_LPM_INT\n");
1192 if (int_grp1 & FUSB300_IGR1_DEV_MODE_CHG_INT) {
1193 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1194 FUSB300_IGR1_DEV_MODE_CHG_INT);
1195 check_device_mode(fusb300);
1198 if (int_grp1 & FUSB300_IGR1_CX_COMFAIL_INT) {
1199 fusb300_set_cxstall(fusb300);
1200 printk(KERN_INFO "fusb300_ep0fail\n");
1203 if (int_grp1 & FUSB300_IGR1_CX_SETUP_INT) {
1204 printk(KERN_INFO "fusb300_ep0setup\n");
1205 if (setup_packet(fusb300, &ctrl)) {
1206 spin_unlock(&fusb300->lock);
1207 if (fusb300->driver->setup(&fusb300->gadget, &ctrl) < 0)
1208 fusb300_set_cxstall(fusb300);
1209 spin_lock(&fusb300->lock);
1213 if (int_grp1 & FUSB300_IGR1_CX_CMDEND_INT)
1214 printk(KERN_INFO "fusb300_cmdend\n");
1217 if (int_grp1 & FUSB300_IGR1_CX_OUT_INT) {
1218 printk(KERN_INFO "fusb300_cxout\n");
1219 fusb300_ep0out(fusb300);
1222 if (int_grp1 & FUSB300_IGR1_CX_IN_INT) {
1223 printk(KERN_INFO "fusb300_cxin\n");
1224 fusb300_ep0in(fusb300);
1227 if (int_grp1 & FUSB300_IGR1_INTGRP5)
1228 fusb300_grp5_handler();
1230 if (int_grp1 & FUSB300_IGR1_INTGRP4)
1231 fusb300_grp4_handler();
1233 if (int_grp1 & FUSB300_IGR1_INTGRP3)
1234 fusb300_grp3_handler();
1236 if (int_grp1 & FUSB300_IGR1_INTGRP2)
1237 fusb300_grp2_handler();
1239 if (int_grp0) {
1240 for (i = 1; i < FUSB300_MAX_NUM_EP; i++) {
1241 if (int_grp0 & FUSB300_IGR0_EPn_FIFO_INT(i)) {
1242 reg = ioread32(fusb300->reg +
1243 FUSB300_OFFSET_EPSET1(i));
1244 in = (reg & FUSB300_EPSET1_DIRIN) ? 1 : 0;
1245 if (in)
1246 in_ep_fifo_handler(fusb300->ep[i]);
1247 else
1248 out_ep_fifo_handler(fusb300->ep[i]);
1253 spin_unlock(&fusb300->lock);
1255 return IRQ_HANDLED;
1258 static void fusb300_set_u2_timeout(struct fusb300 *fusb300,
1259 u32 time)
1261 u32 reg;
1263 reg = ioread32(fusb300->reg + FUSB300_OFFSET_TT);
1264 reg &= ~0xff;
1265 reg |= FUSB300_SSCR2_U2TIMEOUT(time);
1267 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_TT);
1270 static void fusb300_set_u1_timeout(struct fusb300 *fusb300,
1271 u32 time)
1273 u32 reg;
1275 reg = ioread32(fusb300->reg + FUSB300_OFFSET_TT);
1276 reg &= ~(0xff << 8);
1277 reg |= FUSB300_SSCR2_U1TIMEOUT(time);
1279 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_TT);
1282 static void init_controller(struct fusb300 *fusb300)
1284 u32 reg;
1285 u32 mask = 0;
1286 u32 val = 0;
1288 /* split on */
1289 mask = val = FUSB300_AHBBCR_S0_SPLIT_ON | FUSB300_AHBBCR_S1_SPLIT_ON;
1290 reg = ioread32(fusb300->reg + FUSB300_OFFSET_AHBCR);
1291 reg &= ~mask;
1292 reg |= val;
1293 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_AHBCR);
1295 /* enable high-speed LPM */
1296 mask = val = FUSB300_HSCR_HS_LPM_PERMIT;
1297 reg = ioread32(fusb300->reg + FUSB300_OFFSET_HSCR);
1298 reg &= ~mask;
1299 reg |= val;
1300 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_HSCR);
1302 /*set u1 u2 timmer*/
1303 fusb300_set_u2_timeout(fusb300, 0xff);
1304 fusb300_set_u1_timeout(fusb300, 0xff);
1306 /* enable all grp1 interrupt */
1307 iowrite32(0xcfffff9f, fusb300->reg + FUSB300_OFFSET_IGER1);
1309 /*------------------------------------------------------------------------*/
1310 static struct fusb300 *the_controller;
1312 static int fusb300_udc_start(struct usb_gadget_driver *driver,
1313 int (*bind)(struct usb_gadget *))
1315 struct fusb300 *fusb300 = the_controller;
1316 int retval;
1318 if (!driver
1319 || driver->speed < USB_SPEED_FULL
1320 || !bind
1321 || !driver->setup)
1322 return -EINVAL;
1324 if (!fusb300)
1325 return -ENODEV;
1327 if (fusb300->driver)
1328 return -EBUSY;
1330 /* hook up the driver */
1331 driver->driver.bus = NULL;
1332 fusb300->driver = driver;
1333 fusb300->gadget.dev.driver = &driver->driver;
1335 retval = device_add(&fusb300->gadget.dev);
1336 if (retval) {
1337 pr_err("device_add error (%d)\n", retval);
1338 goto error;
1341 retval = bind(&fusb300->gadget);
1342 if (retval) {
1343 pr_err("bind to driver error (%d)\n", retval);
1344 device_del(&fusb300->gadget.dev);
1345 goto error;
1348 return 0;
1350 error:
1351 fusb300->driver = NULL;
1352 fusb300->gadget.dev.driver = NULL;
1354 return retval;
1357 static int fusb300_udc_stop(struct usb_gadget_driver *driver)
1359 struct fusb300 *fusb300 = the_controller;
1361 if (driver != fusb300->driver || !driver->unbind)
1362 return -EINVAL;
1364 driver->unbind(&fusb300->gadget);
1365 fusb300->gadget.dev.driver = NULL;
1367 init_controller(fusb300);
1368 device_del(&fusb300->gadget.dev);
1369 fusb300->driver = NULL;
1371 return 0;
1373 /*--------------------------------------------------------------------------*/
1375 static int fusb300_udc_pullup(struct usb_gadget *_gadget, int is_active)
1377 return 0;
1380 static struct usb_gadget_ops fusb300_gadget_ops = {
1381 .pullup = fusb300_udc_pullup,
1382 .start = fusb300_udc_start,
1383 .stop = fusb300_udc_stop,
1386 static int __exit fusb300_remove(struct platform_device *pdev)
1388 struct fusb300 *fusb300 = dev_get_drvdata(&pdev->dev);
1390 usb_del_gadget_udc(&fusb300->gadget);
1391 iounmap(fusb300->reg);
1392 free_irq(platform_get_irq(pdev, 0), fusb300);
1394 fusb300_free_request(&fusb300->ep[0]->ep, fusb300->ep0_req);
1395 kfree(fusb300);
1397 return 0;
1400 static int __init fusb300_probe(struct platform_device *pdev)
1402 struct resource *res, *ires, *ires1;
1403 void __iomem *reg = NULL;
1404 struct fusb300 *fusb300 = NULL;
1405 struct fusb300_ep *_ep[FUSB300_MAX_NUM_EP];
1406 int ret = 0;
1407 int i;
1409 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1410 if (!res) {
1411 ret = -ENODEV;
1412 pr_err("platform_get_resource error.\n");
1413 goto clean_up;
1416 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1417 if (!ires) {
1418 ret = -ENODEV;
1419 dev_err(&pdev->dev,
1420 "platform_get_resource IORESOURCE_IRQ error.\n");
1421 goto clean_up;
1424 ires1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1425 if (!ires1) {
1426 ret = -ENODEV;
1427 dev_err(&pdev->dev,
1428 "platform_get_resource IORESOURCE_IRQ 1 error.\n");
1429 goto clean_up;
1432 reg = ioremap(res->start, resource_size(res));
1433 if (reg == NULL) {
1434 ret = -ENOMEM;
1435 pr_err("ioremap error.\n");
1436 goto clean_up;
1439 /* initialize udc */
1440 fusb300 = kzalloc(sizeof(struct fusb300), GFP_KERNEL);
1441 if (fusb300 == NULL) {
1442 pr_err("kzalloc error\n");
1443 goto clean_up;
1446 for (i = 0; i < FUSB300_MAX_NUM_EP; i++) {
1447 _ep[i] = kzalloc(sizeof(struct fusb300_ep), GFP_KERNEL);
1448 if (_ep[i] == NULL) {
1449 pr_err("_ep kzalloc error\n");
1450 goto clean_up;
1452 fusb300->ep[i] = _ep[i];
1455 spin_lock_init(&fusb300->lock);
1457 dev_set_drvdata(&pdev->dev, fusb300);
1459 fusb300->gadget.ops = &fusb300_gadget_ops;
1461 device_initialize(&fusb300->gadget.dev);
1463 dev_set_name(&fusb300->gadget.dev, "gadget");
1465 fusb300->gadget.is_dualspeed = 1;
1466 fusb300->gadget.dev.parent = &pdev->dev;
1467 fusb300->gadget.dev.dma_mask = pdev->dev.dma_mask;
1468 fusb300->gadget.dev.release = pdev->dev.release;
1469 fusb300->gadget.name = udc_name;
1470 fusb300->reg = reg;
1472 ret = request_irq(ires->start, fusb300_irq, IRQF_SHARED,
1473 udc_name, fusb300);
1474 if (ret < 0) {
1475 pr_err("request_irq error (%d)\n", ret);
1476 goto clean_up;
1479 ret = request_irq(ires1->start, fusb300_irq,
1480 IRQF_SHARED, udc_name, fusb300);
1481 if (ret < 0) {
1482 pr_err("request_irq1 error (%d)\n", ret);
1483 goto clean_up;
1486 INIT_LIST_HEAD(&fusb300->gadget.ep_list);
1488 for (i = 0; i < FUSB300_MAX_NUM_EP ; i++) {
1489 struct fusb300_ep *ep = fusb300->ep[i];
1491 if (i != 0) {
1492 INIT_LIST_HEAD(&fusb300->ep[i]->ep.ep_list);
1493 list_add_tail(&fusb300->ep[i]->ep.ep_list,
1494 &fusb300->gadget.ep_list);
1496 ep->fusb300 = fusb300;
1497 INIT_LIST_HEAD(&ep->queue);
1498 ep->ep.name = fusb300_ep_name[i];
1499 ep->ep.ops = &fusb300_ep_ops;
1500 ep->ep.maxpacket = HS_BULK_MAX_PACKET_SIZE;
1502 fusb300->ep[0]->ep.maxpacket = HS_CTL_MAX_PACKET_SIZE;
1503 fusb300->ep[0]->epnum = 0;
1504 fusb300->gadget.ep0 = &fusb300->ep[0]->ep;
1505 INIT_LIST_HEAD(&fusb300->gadget.ep0->ep_list);
1507 the_controller = fusb300;
1509 fusb300->ep0_req = fusb300_alloc_request(&fusb300->ep[0]->ep,
1510 GFP_KERNEL);
1511 if (fusb300->ep0_req == NULL)
1512 goto clean_up3;
1514 init_controller(fusb300);
1515 ret = usb_add_gadget_udc(&pdev->dev, &fusb300->gadget);
1516 if (ret)
1517 goto err_add_udc;
1519 dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
1521 return 0;
1522 err_add_udc:
1523 fusb300_free_request(&fusb300->ep[0]->ep, fusb300->ep0_req);
1525 clean_up3:
1526 free_irq(ires->start, fusb300);
1528 clean_up:
1529 if (fusb300) {
1530 if (fusb300->ep0_req)
1531 fusb300_free_request(&fusb300->ep[0]->ep,
1532 fusb300->ep0_req);
1533 kfree(fusb300);
1535 if (reg)
1536 iounmap(reg);
1538 return ret;
1541 static struct platform_driver fusb300_driver = {
1542 .remove = __exit_p(fusb300_remove),
1543 .driver = {
1544 .name = (char *) udc_name,
1545 .owner = THIS_MODULE,
1549 static int __init fusb300_udc_init(void)
1551 return platform_driver_probe(&fusb300_driver, fusb300_probe);
1554 module_init(fusb300_udc_init);
1556 static void __exit fusb300_udc_cleanup(void)
1558 platform_driver_unregister(&fusb300_driver);
1560 module_exit(fusb300_udc_cleanup);