| Linux kernel & device driver programming |
| [ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] |
1 /* 1
2 * at91_udc -- driver for at91-series USB peri
3 *
4 * Copyright (C) 2004 by Thomas Rathbone
5 * Copyright (C) 2005 by HP Labs
6 * Copyright (C) 2005 by David Brownell
7 *
8 * This program is free software; you can redi
9 * it under the terms of the GNU General Publi
10 * the Free Software Foundation; either versio
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope tha
14 * but WITHOUT ANY WARRANTY; without even the
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * GNU General Public License for more details
17 *
18 * You should have received a copy of the GNU
19 * along with this program; if not, write to t
20 * Free Software Foundation, Inc., 59 Temple P
21 * Boston, MA 02111-1307, USA.
22 */
23
24 #undef VERBOSE_DEBUG
25 #undef PACKET_TRACE
26
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/delay.h>
31 #include <linux/ioport.h>
32 #include <linux/slab.h>
33 #include <linux/errno.h>
34 #include <linux/init.h>
35 #include <linux/list.h>
36 #include <linux/interrupt.h>
37 #include <linux/proc_fs.h>
38 #include <linux/clk.h>
39 #include <linux/usb/ch9.h>
40 #include <linux/usb/gadget.h>
41
42 #include <asm/byteorder.h>
43 #include <asm/hardware.h>
44 #include <asm/io.h>
45 #include <asm/irq.h>
46 #include <asm/system.h>
47 #include <asm/mach-types.h>
48 #include <asm/gpio.h>
49
50 #include <asm/arch/board.h>
51 #include <asm/arch/cpu.h>
52 #include <asm/arch/at91sam9261_matrix.h>
53
54 #include "at91_udc.h"
55
56
57 /*
58 * This controller is simple and PIO-only. It
59 * full speed USB controllers, including the a
60 * at91sam926x (arm926ejs, with MMU), and seve
61 *
62 * This driver expects the board has been wire
63 * a VBUS sensing IRQ, and a D+ pullup. (They
64 * testing hasn't covered such cases.)
65 *
66 * The pullup is most important (so it's integ
67 * provides software control over whether the
68 *
69 * The VBUS sensing helps during enumeration,
70 * (and the transceiver) to stay gated off unt
71 * power. During USB suspend, the 48 MHz cloc
72 * it may also be gated off by software during
73 */
74
75 #define DRIVER_VERSION "3 May 2006"
76
77 static const char driver_name [] = "at91_udc";
78 static const char ep0name[] = "ep0";
79
80
81 #define at91_udp_read(dev, reg) \
82 __raw_readl((dev)->udp_baseaddr + (reg
83 #define at91_udp_write(dev, reg, val) \
84 __raw_writel((val), (dev)->udp_baseadd
85
86 /*--------------------------------------------
87
88 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
89
90 #include <linux/seq_file.h>
91
92 static const char debug_filename[] = "driver/u
93
94 #define FOURBITS "%s%s%s%s"
95 #define EIGHTBITS FOURBITS FOURBITS
96
97 static void proc_ep_show(struct seq_file *s, s
98 {
99 static char *types[] = {
100 "control", "out-iso", "out-bul
101 "BOGUS", "in-iso", "in-bulk
102
103 u32 csr;
104 struct at91_request *req;
105 unsigned long flags;
106
107 local_irq_save(flags);
108
109 csr = __raw_readl(ep->creg);
110
111 /* NOTE: not collecting per-endpoint
112
113 seq_printf(s, "\n");
114 seq_printf(s, "%s, maxpacket %d %s%s %
115 ep->ep.name, ep->ep.ma
116 ep->is_in ? "in" : "ou
117 ep->is_iso ? " iso" :
118 ep->is_pingpong
119 ? (ep->fifo_ba
120 : "",
121 ep->stopped ? " stoppe
122 seq_printf(s, "csr %08x rxbytes=%d %s
123 csr,
124 (csr & 0x07ff0000) >> 16,
125 (csr & (1 << 15)) ? "enabled"
126 (csr & (1 << 11)) ? "DATA1" :
127 types[(csr & 0x700) >> 8],
128
129 /* iff type is control then pr
130 (!(csr & 0x700))
131 ? ((csr & (1 << 7)) ?
132 : "",
133 (csr & (1 << 6)) ? " rxdatabk1
134 (csr & (1 << 5)) ? " forcestal
135 (csr & (1 << 4)) ? " txpktrdy"
136
137 (csr & (1 << 3)) ? " stallsent
138 (csr & (1 << 2)) ? " rxsetup"
139 (csr & (1 << 1)) ? " rxdatabk0
140 (csr & (1 << 0)) ? " txcomp" :
141 if (list_empty (&ep->queue))
142 seq_printf(s, "\t(queue empty)
143
144 else list_for_each_entry (req, &ep->qu
145 unsigned length = req->
146
147 seq_printf(s, "\treq %p len %d
148 &req->req, len
149 req->req.lengt
150 }
151 local_irq_restore(flags);
152 }
153
154 static void proc_irq_show(struct seq_file *s,
155 {
156 int i;
157
158 seq_printf(s, "%s %04x:%s%s" FOURBITS,
159 (mask & (1 << 13)) ? " wakeup"
160 (mask & (1 << 12)) ? " endbusr
161
162 (mask & (1 << 11)) ? " sofint"
163 (mask & (1 << 10)) ? " extrsm"
164 (mask & (1 << 9)) ? " rxrsm" :
165 (mask & (1 << 8)) ? " rxsusp"
166 for (i = 0; i < 8; i++) {
167 if (mask & (1 << i))
168 seq_printf(s, " ep%d",
169 }
170 seq_printf(s, "\n");
171 }
172
173 static int proc_udc_show(struct seq_file *s, v
174 {
175 struct at91_udc *udc = s->private;
176 struct at91_ep *ep;
177 u32 tmp;
178
179 seq_printf(s, "%s: version %s\n", driv
180
181 seq_printf(s, "vbus %s, pullup %s, %s
182 udc->vbus ? "present" : "off",
183 udc->enabled
184 ? (udc->vbus ? "active
185 : "disabled",
186 udc->selfpowered ? "self" : "V
187 udc->suspended ? ", suspended"
188 udc->driver ? udc->driver->dri
189
190 /* don't access registers when interfa
191 if (!udc->clocked) {
192 seq_printf(s, "(not clocked)\n
193 return 0;
194 }
195
196 tmp = at91_udp_read(udc, AT91_UDP_FRM_
197 seq_printf(s, "frame %05x:%s%s frame=%
198 (tmp & AT91_UDP_FRM_OK) ? " ok
199 (tmp & AT91_UDP_FRM_ERR) ? " e
200 (tmp & AT91_UDP_NUM));
201
202 tmp = at91_udp_read(udc, AT91_UDP_GLB_
203 seq_printf(s, "glbstate %02x:%s" FOURB
204 (tmp & AT91_UDP_RMWUPE) ? " rm
205 (tmp & AT91_UDP_RSMINPR) ? " r
206 (tmp & AT91_UDP_ESR) ? " esr"
207 (tmp & AT91_UDP_CONFG) ? " con
208 (tmp & AT91_UDP_FADDEN) ? " fa
209
210 tmp = at91_udp_read(udc, AT91_UDP_FADD
211 seq_printf(s, "faddr %03x:%s fadd=%d
212 (tmp & AT91_UDP_FEN) ? " fen"
213 (tmp & AT91_UDP_FADD));
214
215 proc_irq_show(s, "imr ", at91_udp_re
216 proc_irq_show(s, "isr ", at91_udp_re
217
218 if (udc->enabled && udc->vbus) {
219 proc_ep_show(s, &udc->ep[0]);
220 list_for_each_entry (ep, &udc-
221 if (ep->desc)
222 proc_ep_show(s
223 }
224 }
225 return 0;
226 }
227
228 static int proc_udc_open(struct inode *inode,
229 {
230 return single_open(file, proc_udc_show
231 }
232
233 static const struct file_operations proc_ops =
234 .open = proc_udc_open,
235 .read = seq_read,
236 .llseek = seq_lseek,
237 .release = single_release,
238 };
239
240 static void create_debug_file(struct at91_udc
241 {
242 struct proc_dir_entry *pde;
243
244 pde = create_proc_entry (debug_filenam
245 udc->pde = pde;
246 if (pde == NULL)
247 return;
248
249 pde->proc_fops = &proc_ops;
250 pde->data = udc;
251 }
252
253 static void remove_debug_file(struct at91_udc
254 {
255 if (udc->pde)
256 remove_proc_entry(debug_filena
257 }
258
259 #else
260
261 static inline void create_debug_file(struct at
262 static inline void remove_debug_file(struct at
263
264 #endif
265
266
267 /*--------------------------------------------
268
269 static void done(struct at91_ep *ep, struct at
270 {
271 unsigned stopped = ep->stopped;
272 struct at91_udc *udc = ep->udc;
273
274 list_del_init(&req->queue);
275 if (req->req.status == -EINPROGRESS)
276 req->req.status = status;
277 else
278 status = req->req.status;
279 if (status && status != -ESHUTDOWN)
280 VDBG("%s done %p, status %d\n"
281
282 ep->stopped = 1;
283 req->req.complete(&ep->ep, &req->req);
284 ep->stopped = stopped;
285
286 /* ep0 is always ready; other endpoint
287 if (list_empty(&ep->queue) && ep->int_
288 at91_udp_write(udc, AT91_UDP_I
289 }
290
291 /*--------------------------------------------
292
293 /* bits indicating OUT fifo has data ready */
294 #define RX_DATA_READY (AT91_UDP_RX_DATA_BK0
295
296 /*
297 * Endpoint FIFO CSR bits have a mix of bits,
298 * back most of the value you just read (becau
299 * bits that may change after reading and befo
300 *
301 * Except when changing a specific bit, always
302 * - clear SET_FX bits (setting them could ch
303 * - set CLR_FX bits (clearing them could cha
304 *
305 * There are also state bits like FORCESTALL,
306 * that shouldn't normally be changed.
307 *
308 * NOTE at91sam9260 docs mention synch between
309 * implying a need to wait for one write to co
310 * before starting the next write. This shoul
311 * infrequently we write, except maybe for wri
312 */
313 #define SET_FX (AT91_UDP_TXPKTRDY)
314 #define CLR_FX (RX_DATA_READY | AT91_UDP_RXSE
315 | AT91_UDP_STALLSENT | AT91_UD
316
317 /* pull OUT packet data from the endpoint's fi
318 static int read_fifo (struct at91_ep *ep, stru
319 {
320 u32 __iomem *creg = ep->creg;
321 u8 __iomem *dreg = ep->creg + (AT
322 u32 csr;
323 u8 *buf;
324 unsigned int count, bufferspace, is
325
326 buf = req->req.buf + req->req.actual;
327 bufferspace = req->req.length - req->r
328
329 /*
330 * there might be nothing to read if e
331 * or if we already emptied both pingp
332 */
333 rescan:
334 csr = __raw_readl(creg);
335 if ((csr & RX_DATA_READY) == 0)
336 return 0;
337
338 count = (csr & AT91_UDP_RXBYTECNT) >>
339 if (count > ep->ep.maxpacket)
340 count = ep->ep.maxpacket;
341 if (count > bufferspace) {
342 DBG("%s buffer overflow\n", ep
343 req->req.status = -EOVERFLOW;
344 count = bufferspace;
345 }
346 __raw_readsb(dreg, buf, count);
347
348 /* release and swap pingpong mem bank
349 csr |= CLR_FX;
350 if (ep->is_pingpong) {
351 if (ep->fifo_bank == 0) {
352 csr &= ~(SET_FX | AT91
353 ep->fifo_bank = 1;
354 } else {
355 csr &= ~(SET_FX | AT91
356 ep->fifo_bank = 0;
357 }
358 } else
359 csr &= ~(SET_FX | AT91_UDP_RX_
360 __raw_writel(csr, creg);
361
362 req->req.actual += count;
363 is_done = (count < ep->ep.maxpacket);
364 if (count == bufferspace)
365 is_done = 1;
366
367 PACKET("%s %p out/%d%s\n", ep->ep.name
368 is_done ? " (done)" :
369
370 /*
371 * avoid extra trips through IRQ logic
372 * the fifo ... maybe preventing an ex
373 */
374 if (is_done)
375 done(ep, req, 0);
376 else if (ep->is_pingpong) {
377 bufferspace -= count;
378 buf += count;
379 goto rescan;
380 }
381
382 return is_done;
383 }
384
385 /* load fifo for an IN packet */
386 static int write_fifo(struct at91_ep *ep, stru
387 {
388 u32 __iomem *creg = ep->creg;
389 u32 csr = __raw_readl(creg
390 u8 __iomem *dreg = ep->creg + (AT
391 unsigned total, count, is_last;
392
393 /*
394 * TODO: allow for writing two packets
395 * reduce the amount of IN-NAKing, but
396 * throughput much. (Unlike preventin
397 */
398
399 /*
400 * If ep_queue() calls us, the queue i
401 * odd states like TXCOMP not yet clea
402 * least one IRQ) or the fifo not yet
403 * issues normally (IRQ handler fast p
404 */
405 if (unlikely(csr & (AT91_UDP_TXCOMP |
406 if (csr & AT91_UDP_TXCOMP) {
407 csr |= CLR_FX;
408 csr &= ~(SET_FX | AT91
409 __raw_writel(csr, creg
410 csr = __raw_readl(creg
411 }
412 if (csr & AT91_UDP_TXPKTRDY)
413 return 0;
414 }
415
416 total = req->req.length - req->req.act
417 if (ep->ep.maxpacket < total) {
418 count = ep->ep.maxpacket;
419 is_last = 0;
420 } else {
421 count = total;
422 is_last = (count < ep->ep.maxp
423 }
424
425 /*
426 * Write the packet, maybe it's a ZLP.
427 *
428 * NOTE: incrementing req->actual bef
429 * gadget driver IN bytecounts can be
430 * fixable with PIO drivers like this
431 * do the increment later on TX irq),
432 *
433 * So all gadget drivers must accept t
434 * hardware supports precise fifo stat
435 * recover when the actual bytecount m
436 * and Measurement Class devices).
437 */
438 __raw_writesb(dreg, req->req.buf + req
439 csr &= ~SET_FX;
440 csr |= CLR_FX | AT91_UDP_TXPKTRDY;
441 __raw_writel(csr, creg);
442 req->req.actual += count;
443
444 PACKET("%s %p in/%d%s\n", ep->ep.name,
445 is_last ? " (done)" :
446 if (is_last)
447 done(ep, req, 0);
448 return is_last;
449 }
450
451 static void nuke(struct at91_ep *ep, int statu
452 {
453 struct at91_request *req;
454
455 // terminer chaque requete dans la que
456 ep->stopped = 1;
457 if (list_empty(&ep->queue))
458 return;
459
460 VDBG("%s %s\n", __FUNCTION__, ep->ep.n
461 while (!list_empty(&ep->queue)) {
462 req = list_entry(ep->queue.nex
463 done(ep, req, status);
464 }
465 }
466
467 /*--------------------------------------------
468
469 static int at91_ep_enable(struct usb_ep *_ep,
470 const struct u
471 {
472 struct at91_ep *ep = container_of(_ep
473 struct at91_udc *dev = ep->udc;
474 u16 maxpacket;
475 u32 tmp;
476 unsigned long flags;
477
478 if (!_ep || !ep
479 || !desc || ep->desc
480 || _ep->name == ep0nam
481 || desc->bDescriptorTy
482 || (maxpacket = le16_t
483 || maxpacket > ep->max
484 DBG("bad ep or descriptor\n");
485 return -EINVAL;
486 }
487
488 if (!dev->driver || dev->gadget.speed
489 DBG("bogus device state\n");
490 return -ESHUTDOWN;
491 }
492
493 tmp = desc->bmAttributes & USB_ENDPOIN
494 switch (tmp) {
495 case USB_ENDPOINT_XFER_CONTROL:
496 DBG("only one control endpoint
497 return -EINVAL;
498 case USB_ENDPOINT_XFER_INT:
499 if (maxpacket > 64)
500 goto bogus_max;
501 break;
502 case USB_ENDPOINT_XFER_BULK:
503 switch (maxpacket) {
504 case 8:
505 case 16:
506 case 32:
507 case 64:
508 goto ok;
509 }
510 bogus_max:
511 DBG("bogus maxpacket %d\n", ma
512 return -EINVAL;
513 case USB_ENDPOINT_XFER_ISOC:
514 if (!ep->is_pingpong) {
515 DBG("iso requires doub
516 return -EINVAL;
517 }
518 break;
519 }
520
521 ok:
522 local_irq_save(flags);
523
524 /* initialize endpoint to match this d
525 ep->is_in = (desc->bEndpointAddress &
526 ep->is_iso = (tmp == USB_ENDPOINT_XFER
527 ep->stopped = 0;
528 if (ep->is_in)
529 tmp |= 0x04;
530 tmp <<= 8;
531 tmp |= AT91_UDP_EPEDS;
532 __raw_writel(tmp, ep->creg);
533
534 ep->desc = desc;
535 ep->ep.maxpacket = maxpacket;
536
537 /*
538 * reset/init endpoint fifo. NOTE: l
539 * since endpoint resets don't reset h
540 */
541 at91_udp_write(dev, AT91_UDP_RST_EP, e
542 at91_udp_write(dev, AT91_UDP_RST_EP, 0
543
544 local_irq_restore(flags);
545 return 0;
546 }
547
548 static int at91_ep_disable (struct usb_ep * _e
549 {
550 struct at91_ep *ep = container_of(_ep
551 struct at91_udc *udc = ep->udc;
552 unsigned long flags;
553
554 if (ep == &ep->udc->ep[0])
555 return -EINVAL;
556
557 local_irq_save(flags);
558
559 nuke(ep, -ESHUTDOWN);
560
561 /* restore the endpoint's pristine con
562 ep->desc = NULL;
563 ep->ep.maxpacket = ep->maxpacket;
564
565 /* reset fifos and endpoint */
566 if (ep->udc->clocked) {
567 at91_udp_write(udc, AT91_UDP_R
568 at91_udp_write(udc, AT91_UDP_R
569 __raw_writel(0, ep->creg);
570 }
571
572 local_irq_restore(flags);
573 return 0;
574 }
575
576 /*
577 * this is a PIO-only driver, so there's nothi
578 * interesting for request or buffer allocatio
579 */
580
581 static struct usb_request *
582 at91_ep_alloc_request(struct usb_ep *_ep, gfp_
583 {
584 struct at91_request *req;
585
586 req = kzalloc(sizeof (struct at91_requ
587 if (!req)
588 return NULL;
589
590 INIT_LIST_HEAD(&req->queue);
591 return &req->req;
592 }
593
594 static void at91_ep_free_request(struct usb_ep
595 {
596 struct at91_request *req;
597
598 req = container_of(_req, struct at91_r
599 BUG_ON(!list_empty(&req->queue));
600 kfree(req);
601 }
602
603 static int at91_ep_queue(struct usb_ep *_ep,
604 struct usb_request *_r
605 {
606 struct at91_request *req;
607 struct at91_ep *ep;
608 struct at91_udc *dev;
609 int status;
610 unsigned long flags;
611
612 req = container_of(_req, struct at91_r
613 ep = container_of(_ep, struct at91_ep,
614
615 if (!_req || !_req->complete
616 || !_req->buf || !list
617 DBG("invalid request\n");
618 return -EINVAL;
619 }
620
621 if (!_ep || (!ep->desc && ep->ep.name
622 DBG("invalid ep\n");
623 return -EINVAL;
624 }
625
626 dev = ep->udc;
627
628 if (!dev || !dev->driver || dev->gadge
629 DBG("invalid device\n");
630 return -EINVAL;
631 }
632
633 _req->status = -EINPROGRESS;
634 _req->actual = 0;
635
636 local_irq_save(flags);
637
638 /* try to kickstart any empty and idle
639 if (list_empty(&ep->queue) && !ep->sto
640 int is_ep0;
641
642 /*
643 * If this control request has
644 * will start that stage. It
645 * request (until the status s
646 *
647 * If the data stage is empty,
648 * IN/STATUS stage. (Unsucces
649 */
650 is_ep0 = (ep->ep.name == ep0na
651 if (is_ep0) {
652 u32 tmp;
653
654 if (!dev->req_pending)
655 status = -EINV
656 goto done;
657 }
658
659 /*
660 * defer changing CONF
661 * reconfigures the en
662 */
663 if (dev->wait_for_conf
664 tmp = at91_udp
665 tmp ^= AT91_UD
666 VDBG("toggle c
667 at91_udp_write
668 }
669 if (req->req.length ==
670 ep0_in_status:
671 PACKET("ep0 in
672 status = 0;
673 tmp = __raw_re
674 tmp &= ~SET_FX
675 tmp |= CLR_FX
676 __raw_writel(t
677 dev->req_pendi
678 goto done;
679 }
680 }
681
682 if (ep->is_in)
683 status = write_fifo(ep
684 else {
685 status = read_fifo(ep,
686
687 /* IN/STATUS stage is
688 if (status && is_ep0)
689 goto ep0_in_st
690 }
691 } else
692 status = 0;
693
694 if (req && !status) {
695 list_add_tail (&req->queue, &e
696 at91_udp_write(dev, AT91_UDP_I
697 }
698 done:
699 local_irq_restore(flags);
700 return (status < 0) ? status : 0;
701 }
702
703 static int at91_ep_dequeue(struct usb_ep *_ep,
704 {
705 struct at91_ep *ep;
706 struct at91_request *req;
707
708 ep = container_of(_ep, struct at91_ep,
709 if (!_ep || ep->ep.name == ep0name)
710 return -EINVAL;
711
712 /* make sure it's actually queued on t
713 list_for_each_entry (req, &ep->queue,
714 if (&req->req == _req)
715 break;
716 }
717 if (&req->req != _req)
718 return -EINVAL;
719
720 done(ep, req, -ECONNRESET);
721 return 0;
722 }
723
724 static int at91_ep_set_halt(struct usb_ep *_ep
725 {
726 struct at91_ep *ep = container_of(_ep
727 struct at91_udc *udc = ep->udc;
728 u32 __iomem *creg;
729 u32 csr;
730 unsigned long flags;
731 int status = 0;
732
733 if (!_ep || ep->is_iso || !ep->udc->cl
734 return -EINVAL;
735
736 creg = ep->creg;
737 local_irq_save(flags);
738
739 csr = __raw_readl(creg);
740
741 /*
742 * fail with still-busy IN endpoints,
743 * of data tx then stall. note that t
744 * completely accurate as a tx bytecou
745 */
746 if (ep->is_in && (!list_empty(&ep->que
747 status = -EAGAIN;
748 else {
749 csr |= CLR_FX;
750 csr &= ~SET_FX;
751 if (value) {
752 csr |= AT91_UDP_FORCES
753 VDBG("halt %s\n", ep->
754 } else {
755 at91_udp_write(udc, AT
756 at91_udp_write(udc, AT
757 csr &= ~AT91_UDP_FORCE
758 }
759 __raw_writel(csr, creg);
760 }
761
762 local_irq_restore(flags);
763 return status;
764 }
765
766 static const struct usb_ep_ops at91_ep_ops = {
767 .enable = at91_ep_enable,
768 .disable = at91_ep_disable,
769 .alloc_request = at91_ep_alloc_reques
770 .free_request = at91_ep_free_request
771 .queue = at91_ep_queue,
772 .dequeue = at91_ep_dequeue,
773 .set_halt = at91_ep_set_halt,
774 // there's only imprecise fifo status
775 };
776
777 /*--------------------------------------------
778
779 static int at91_get_frame(struct usb_gadget *g
780 {
781 struct at91_udc *udc = to_udc(gadget);
782
783 if (!to_udc(gadget)->clocked)
784 return -EINVAL;
785 return at91_udp_read(udc, AT91_UDP_FRM
786 }
787
788 static int at91_wakeup(struct usb_gadget *gadg
789 {
790 struct at91_udc *udc = to_udc(gadget);
791 u32 glbstate;
792 int status = -EINVAL;
793 unsigned long flags;
794
795 DBG("%s\n", __FUNCTION__ );
796 local_irq_save(flags);
797
798 if (!udc->clocked || !udc->suspended)
799 goto done;
800
801 /* NOTE: some "early versions" handle
802
803 glbstate = at91_udp_read(udc, AT91_UDP
804 if (!(glbstate & AT91_UDP_ESR))
805 goto done;
806 glbstate |= AT91_UDP_ESR;
807 at91_udp_write(udc, AT91_UDP_GLB_STAT,
808
809 done:
810 local_irq_restore(flags);
811 return status;
812 }
813
814 /* reinit == restore inital software state */
815 static void udc_reinit(struct at91_udc *udc)
816 {
817 u32 i;
818
819 INIT_LIST_HEAD(&udc->gadget.ep_list);
820 INIT_LIST_HEAD(&udc->gadget.ep0->ep_li
821
822 for (i = 0; i < NUM_ENDPOINTS; i++) {
823 struct at91_ep *ep = &udc->ep[
824
825 if (i != 0)
826 list_add_tail(&ep->ep.
827 ep->desc = NULL;
828 ep->stopped = 0;
829 ep->fifo_bank = 0;
830 ep->ep.maxpacket = ep->maxpack
831 ep->creg = (void __iomem *) ud
832 // initialiser une queue par e
833 INIT_LIST_HEAD(&ep->queue);
834 }
835 }
836
837 static void stop_activity(struct at91_udc *udc
838 {
839 struct usb_gadget_driver *driver = udc
840 int i;
841
842 if (udc->gadget.speed == USB_SPEED_UNK
843 driver = NULL;
844 udc->gadget.speed = USB_SPEED_UNKNOWN;
845 udc->suspended = 0;
846
847 for (i = 0; i < NUM_ENDPOINTS; i++) {
848 struct at91_ep *ep = &udc->ep[
849 ep->stopped = 1;
850 nuke(ep, -ESHUTDOWN);
851 }
852 if (driver)
853 driver->disconnect(&udc->gadge
854
855 udc_reinit(udc);
856 }
857
858 static void clk_on(struct at91_udc *udc)
859 {
860 if (udc->clocked)
861 return;
862 udc->clocked = 1;
863 clk_enable(udc->iclk);
864 clk_enable(udc->fclk);
865 }
866
867 static void clk_off(struct at91_udc *udc)
868 {
869 if (!udc->clocked)
870 return;
871 udc->clocked = 0;
872 udc->gadget.speed = USB_SPEED_UNKNOWN;
873 clk_disable(udc->fclk);
874 clk_disable(udc->iclk);
875 }
876
877 /*
878 * activate/deactivate link with host; minimiz
879 * inactive links by cutting clocks and transc
880 */
881 static void pullup(struct at91_udc *udc, int i
882 {
883 int active = !udc->board.pullup_ac
884
885 if (!udc->enabled || !udc->vbus)
886 is_on = 0;
887 DBG("%sactive\n", is_on ? "" : "in");
888
889 if (is_on) {
890 clk_on(udc);
891 at91_udp_write(udc, AT91_UDP_I
892 at91_udp_write(udc, AT91_UDP_T
893 if (cpu_is_at91rm9200())
894 gpio_set_value(udc->bo
895 else if (cpu_is_at91sam9260()
896 u32 txvc = at91_ud
897
898 txvc |= AT91_UDP_TXVC_
899 at91_udp_write(udc, AT
900 } else if (cpu_is_at91sam9261(
901 u32 usbpucr;
902
903 usbpucr = at91_sys_rea
904 usbpucr |= AT91_MATRIX
905 at91_sys_write(AT91_MA
906 }
907 } else {
908 stop_activity(udc);
909 at91_udp_write(udc, AT91_UDP_I
910 at91_udp_write(udc, AT91_UDP_T
911 if (cpu_is_at91rm9200())
912 gpio_set_value(udc->bo
913 else if (cpu_is_at91sam9260()
914 u32 txvc = at91_ud
915
916 txvc &= ~AT91_UDP_TXVC
917 at91_udp_write(udc, AT
918 } else if (cpu_is_at91sam9261(
919 u32 usbpucr;
920
921 usbpucr = at91_sys_rea
922 usbpucr &= ~AT91_MATRI
923 at91_sys_write(AT91_MA
924 }
925 clk_off(udc);
926 }
927 }
928
929 /* vbus is here! turn everything on that's re
930 static int at91_vbus_session(struct usb_gadget
931 {
932 struct at91_udc *udc = to_udc(gadget);
933 unsigned long flags;
934
935 // VDBG("vbus %s\n", is_active ? "on"
936 local_irq_save(flags);
937 udc->vbus = (is_active != 0);
938 if (udc->driver)
939 pullup(udc, is_active);
940 else
941 pullup(udc, 0);
942 local_irq_restore(flags);
943 return 0;
944 }
945
946 static int at91_pullup(struct usb_gadget *gadg
947 {
948 struct at91_udc *udc = to_udc(gadget);
949 unsigned long flags;
950
951 local_irq_save(flags);
952 udc->enabled = is_on = !!is_on;
953 pullup(udc, is_on);
954 local_irq_restore(flags);
955 return 0;
956 }
957
958 static int at91_set_selfpowered(struct usb_gad
959 {
960 struct at91_udc *udc = to_udc(gadget);
961 unsigned long flags;
962
963 local_irq_save(flags);
964 udc->selfpowered = (is_on != 0);
965 local_irq_restore(flags);
966 return 0;
967 }
968
969 static const struct usb_gadget_ops at91_udc_op
970 .get_frame = at91_get_fra
971 .wakeup = at91_wakeup,
972 .set_selfpowered = at91_set_sel
973 .vbus_session = at91_vbus_se
974 .pullup = at91_pullup,
975
976 /*
977 * VBUS-powered devices may also also
978 * power budgets after an appropriate
979 */
980 // .vbus_power = at91_vbus_po
981 };
982
983 /*--------------------------------------------
984
985 static int handle_ep(struct at91_ep *ep)
986 {
987 struct at91_request *req;
988 u32 __iomem *creg = ep->cr
989 u32 csr = __raw_re
990
991 if (!list_empty(&ep->queue))
992 req = list_entry(ep->queue.nex
993 struct at91_request, q
994 else
995 req = NULL;
996
997 if (ep->is_in) {
998 if (csr & (AT91_UDP_STALLSENT
999 csr |= CLR_FX;
1000 csr &= ~(SET_FX | AT9
1001 __raw_writel(csr, cre
1002 }
1003 if (req)
1004 return write_fifo(ep,
1005
1006 } else {
1007 if (csr & AT91_UDP_STALLSENT)
1008 /* STALLSENT bit == I
1009 if (ep->is_iso && req
1010 req->req.stat
1011 csr |= CLR_FX;
1012 csr &= ~(SET_FX | AT9
1013 __raw_writel(csr, cre
1014 csr = __raw_readl(cre
1015 }
1016 if (req && (csr & RX_DATA_REA
1017 return read_fifo(ep,
1018 }
1019 return 0;
1020 }
1021
1022 union setup {
1023 u8 raw[8];
1024 struct usb_ctrlrequest r;
1025 };
1026
1027 static void handle_setup(struct at91_udc *udc
1028 {
1029 u32 __iomem *creg = ep->creg;
1030 u8 __iomem *dreg = ep->creg + (A
1031 unsigned rxcount, i = 0;
1032 u32 tmp;
1033 union setup pkt;
1034 int status = 0;
1035
1036 /* read and ack SETUP; hard-fail for
1037 rxcount = (csr & AT91_UDP_RXBYTECNT)
1038 if (likely(rxcount == 8)) {
1039 while (rxcount--)
1040 pkt.raw[i++] = __raw_
1041 if (pkt.r.bRequestType & USB_
1042 csr |= AT91_UDP_DIR;
1043 ep->is_in = 1;
1044 } else {
1045 csr &= ~AT91_UDP_DIR;
1046 ep->is_in = 0;
1047 }
1048 } else {
1049 // REVISIT this happens somet
1050 ERR("SETUP len %d, csr %08x\n
1051 status = -EINVAL;
1052 }
1053 csr |= CLR_FX;
1054 csr &= ~(SET_FX | AT91_UDP_RXSETUP);
1055 __raw_writel(csr, creg);
1056 udc->wait_for_addr_ack = 0;
1057 udc->wait_for_config_ack = 0;
1058 ep->stopped = 0;
1059 if (unlikely(status != 0))
1060 goto stall;
1061
1062 #define w_index le16_to_cpu(pkt.r.wIn
1063 #define w_value le16_to_cpu(pkt.r.wVa
1064 #define w_length le16_to_cpu(pkt.r.wLe
1065
1066 VDBG("SETUP %02x.%02x v%04x i%04x l%0
1067 pkt.r.bRequestType, p
1068 w_value, w_index, w_l
1069
1070 /*
1071 * A few standard requests get handle
1072 * hardware ... notably for device an
1073 */
1074 udc->req_pending = 1;
1075 csr = __raw_readl(creg);
1076 csr |= CLR_FX;
1077 csr &= ~SET_FX;
1078 switch ((pkt.r.bRequestType << 8) | p
1079
1080 case ((USB_TYPE_STANDARD|USB_RECIP_DE
1081 | USB_REQ_SET_ADDRESS
1082 __raw_writel(csr | AT91_UDP_T
1083 udc->addr = w_value;
1084 udc->wait_for_addr_ack = 1;
1085 udc->req_pending = 0;
1086 /* FADDR is set later, when w
1087 return;
1088
1089 case ((USB_TYPE_STANDARD|USB_RECIP_DE
1090 | USB_REQ_SET_CONFIGU
1091 tmp = at91_udp_read(udc, AT91
1092 if (pkt.r.wValue)
1093 udc->wait_for_config_
1094 else
1095 udc->wait_for_config_
1096 if (udc->wait_for_config_ack)
1097 VDBG("wait for config
1098 /* CONFG is toggled later, if
1099 break;
1100
1101 /*
1102 * Hosts may set or clear remote wake
1103 * devices may report they're VBUS po
1104 */
1105 case ((USB_DIR_IN|USB_TYPE_STANDARD|U
1106 | USB_REQ_GET_STATUS:
1107 tmp = (udc->selfpowered << US
1108 if (at91_udp_read(udc, AT91_U
1109 tmp |= (1 << USB_DEVI
1110 PACKET("get device status\n")
1111 __raw_writeb(tmp, dreg);
1112 __raw_writeb(0, dreg);
1113 goto write_in;
1114 /* then STATUS starts later,
1115 case ((USB_TYPE_STANDARD|USB_RECIP_DE
1116 | USB_REQ_SET_FEATURE
1117 if (w_value != USB_DEVICE_REM
1118 goto stall;
1119 tmp = at91_udp_read(udc, AT91
1120 tmp |= AT91_UDP_ESR;
1121 at91_udp_write(udc, AT91_UDP_
1122 goto succeed;
1123 case ((USB_TYPE_STANDARD|USB_RECIP_DE
1124 | USB_REQ_CLEAR_FEATU
1125 if (w_value != USB_DEVICE_REM
1126 goto stall;
1127 tmp = at91_udp_read(udc, AT91
1128 tmp &= ~AT91_UDP_ESR;
1129 at91_udp_write(udc, AT91_UDP_
1130 goto succeed;
1131
1132 /*
1133 * Interfaces have no feature setting
1134 * we won't even insist the interface
1135 */
1136 case ((USB_DIR_IN|USB_TYPE_STANDARD|U
1137 | USB_REQ_GET_STATUS:
1138 PACKET("get interface status\
1139 __raw_writeb(0, dreg);
1140 __raw_writeb(0, dreg);
1141 goto write_in;
1142 /* then STATUS starts later,
1143 case ((USB_TYPE_STANDARD|USB_RECIP_IN
1144 | USB_REQ_SET_FEATURE
1145 case ((USB_TYPE_STANDARD|USB_RECIP_IN
1146 | USB_REQ_CLEAR_FEATU
1147 goto stall;
1148
1149 /*
1150 * Hosts may clear bulk/intr endpoint
1151 * driver sets it (not widely used);
1152 */
1153 case ((USB_DIR_IN|USB_TYPE_STANDARD|U
1154 | USB_REQ_GET_STATUS:
1155 tmp = w_index & USB_ENDPOINT_
1156 ep = &udc->ep[tmp];
1157 if (tmp >= NUM_ENDPOINTS || (
1158 goto stall;
1159
1160 if (tmp) {
1161 if ((w_index & USB_DI
1162 if (!ep->is_i
1163 goto
1164 } else if (ep->is_in)
1165 goto stall;
1166 }
1167 PACKET("get %s status\n", ep-
1168 if (__raw_readl(ep->creg) & A
1169 tmp = (1 << USB_ENDPO
1170 else
1171 tmp = 0;
1172 __raw_writeb(tmp, dreg);
1173 __raw_writeb(0, dreg);
1174 goto write_in;
1175 /* then STATUS starts later,
1176 case ((USB_TYPE_STANDARD|USB_RECIP_EN
1177 | USB_REQ_SET_FEATURE
1178 tmp = w_index & USB_ENDPOINT_
1179 ep = &udc->ep[tmp];
1180 if (w_value != USB_ENDPOINT_H
1181 goto stall;
1182 if (!ep->desc || ep->is_iso)
1183 goto stall;
1184 if ((w_index & USB_DIR_IN)) {
1185 if (!ep->is_in)
1186 goto stall;
1187 } else if (ep->is_in)
1188 goto stall;
1189
1190 tmp = __raw_readl(ep->creg);
1191 tmp &= ~SET_FX;
1192 tmp |= CLR_FX | AT91_UDP_FORC
1193 __raw_writel(tmp, ep->creg);
1194 goto succeed;
1195 case ((USB_TYPE_STANDARD|USB_RECIP_EN
1196 | USB_REQ_CLEAR_FEATU
1197 tmp = w_index & USB_ENDPOINT_
1198 ep = &udc->ep[tmp];
1199 if (w_value != USB_ENDPOINT_H
1200 goto stall;
1201 if (tmp == 0)
1202 goto succeed;
1203 if (!ep->desc || ep->is_iso)
1204 goto stall;
1205 if ((w_index & USB_DIR_IN)) {
1206 if (!ep->is_in)
1207 goto stall;
1208 } else if (ep->is_in)
1209 goto stall;
1210
1211 at91_udp_write(udc, AT91_UDP_
1212 at91_udp_write(udc, AT91_UDP_
1213 tmp = __raw_readl(ep->creg);
1214 tmp |= CLR_FX;
1215 tmp &= ~(SET_FX | AT91_UDP_FO
1216 __raw_writel(tmp, ep->creg);
1217 if (!list_empty(&ep->queue))
1218 handle_ep(ep);
1219 goto succeed;
1220 }
1221
1222 #undef w_value
1223 #undef w_index
1224 #undef w_length
1225
1226 /* pass request up to the gadget driv
1227 if (udc->driver)
1228 status = udc->driver->setup(&
1229 else
1230 status = -ENODEV;
1231 if (status < 0) {
1232 stall:
1233 VDBG("req %02x.%02x protocol
1234 pkt.r.bReques
1235 csr |= AT91_UDP_FORCESTALL;
1236 __raw_writel(csr, creg);
1237 udc->req_pending = 0;
1238 }
1239 return;
1240
1241 succeed:
1242 /* immediate successful (IN) STATUS a
1243 PACKET("ep0 in/status\n");
1244 write_in:
1245 csr |= AT91_UDP_TXPKTRDY;
1246 __raw_writel(csr, creg);
1247 udc->req_pending = 0;
1248 return;
1249 }
1250
1251 static void handle_ep0(struct at91_udc *udc)
1252 {
1253 struct at91_ep *ep0 = &udc->
1254 u32 __iomem *creg = ep0->
1255 u32 csr = __raw_r
1256 struct at91_request *req;
1257
1258 if (unlikely(csr & AT91_UDP_STALLSENT
1259 nuke(ep0, -EPROTO);
1260 udc->req_pending = 0;
1261 csr |= CLR_FX;
1262 csr &= ~(SET_FX | AT91_UDP_ST
1263 __raw_writel(csr, creg);
1264 VDBG("ep0 stalled\n");
1265 csr = __raw_readl(creg);
1266 }
1267 if (csr & AT91_UDP_RXSETUP) {
1268 nuke(ep0, 0);
1269 udc->req_pending = 0;
1270 handle_setup(udc, ep0, csr);
1271 return;
1272 }
1273
1274 if (list_empty(&ep0->queue))
1275 req = NULL;
1276 else
1277 req = list_entry(ep0->queue.n
1278
1279 /* host ACKed an IN packet that we se
1280 if (csr & AT91_UDP_TXCOMP) {
1281 csr |= CLR_FX;
1282 csr &= ~(SET_FX | AT91_UDP_TX
1283
1284 /* write more IN DATA? */
1285 if (req && ep0->is_in) {
1286 if (handle_ep(ep0))
1287 udc->req_pend
1288
1289 /*
1290 * Ack after:
1291 * - last IN DATA packet (in
1292 * - IN/STATUS for OUT DATA
1293 * - IN/STATUS for any zero-
1294 * except for the IN DATA cas
1295 * an OUT status later, which
1296 */
1297 } else {
1298 udc->req_pending = 0;
1299 __raw_writel(csr, cre
1300
1301 /*
1302 * SET_ADDRESS takes
1303 * (to the original a
1304 */
1305 if (udc->wait_for_add
1306 u32 tmp;
1307
1308 at91_udp_writ
1309
1310 tmp = at91_ud
1311 tmp &= ~AT91_
1312 if (udc->addr
1313 tmp |
1314 at91_udp_writ
1315
1316 udc->wait_for
1317 VDBG("address
1318 }
1319 }
1320 }
1321
1322 /* OUT packet arrived ... */
1323 else if (csr & AT91_UDP_RX_DATA_BK0)
1324 csr |= CLR_FX;
1325 csr &= ~(SET_FX | AT91_UDP_RX
1326
1327 /* OUT DATA stage */
1328 if (!ep0->is_in) {
1329 if (req) {
1330 if (handle_ep
1331 /* se
1332 PACKE
1333 csr =
1334 csr &
1335 csr |
1336 __raw
1337 udc->
1338 }
1339 } else if (udc->req_p
1340 /*
1341 * AT91 hardw
1342 * "deferred
1343 * transfers.
1344 *
1345 * The normal
1346 * fifo until
1347 * We couldn'
1348 * the IRQ th
1349 * e.g. when
1350 *
1351 * Working ar
1352 * would almo
1353 * except tha
1354 * stalling o
1355 * that gadge
1356 */
1357 DBG("no contr
1358 __raw_writel(
1359 udc->req_pend
1360 }
1361
1362 /* STATUS stage for control-I
1363 } else {
1364 PACKET("ep0 out/statu
1365 __raw_writel(csr, cre
1366
1367 /* "early" status sta
1368 if (req)
1369 done(ep0, req
1370 }
1371 }
1372 }
1373
1374 static irqreturn_t at91_udc_irq (int irq, voi
1375 {
1376 struct at91_udc *udc = _udc;
1377 u32 rescans = 5;
1378
1379 while (rescans--) {
1380 u32 status;
1381
1382 status = at91_udp_read(udc, A
1383 & at91_udp_read(udc,
1384 if (!status)
1385 break;
1386
1387 /* USB reset irq: not maskab
1388 if (status & AT91_UDP_ENDBUSR
1389 at91_udp_write(udc, A
1390 at91_udp_write(udc, A
1391 /* Atmel code clears
1392 at91_udp_write(udc, A
1393 at91_udp_write(udc, A
1394 VDBG("end bus reset\n
1395 udc->addr = 0;
1396 stop_activity(udc);
1397
1398 /* enable ep0 */
1399 at91_udp_write(udc, A
1400 AT91_
1401 udc->gadget.speed = U
1402 udc->suspended = 0;
1403 at91_udp_write(udc, A
1404
1405 /*
1406 * NOTE: this driver
1407 * USB host is presen
1408 * boards that don't
1409 * clocks need to be
1410 */
1411
1412 /* host initiated suspend (3+
1413 } else if (status & AT91_UDP_
1414 at91_udp_write(udc, A
1415 at91_udp_write(udc, A
1416 at91_udp_write(udc, A
1417 // VDBG("bus suspend\
1418 if (udc->suspended)
1419 continue;
1420 udc->suspended = 1;
1421
1422 /*
1423 * NOTE: when suspen
1424 * gadget driver shou
1425 * and then into stan
1426 * 500uA power (2500u
1427 */
1428 if (udc->driver && ud
1429 udc->driver->
1430
1431 /* host initiated resume */
1432 } else if (status & AT91_UDP_
1433 at91_udp_write(udc, A
1434 at91_udp_write(udc, A
1435 at91_udp_write(udc, A
1436 // VDBG("bus resume\n
1437 if (!udc->suspended)
1438 continue;
1439 udc->suspended = 0;
1440
1441 /*
1442 * NOTE: for a VBUS-
1443 * would normally wan
1444 * mode into normal m
1445 */
1446 if (udc->driver && ud
1447 udc->driver->
1448
1449 /* endpoint IRQs are cleared
1450 } else {
1451 int i;
1452 unsigned mask
1453 struct at91_ep *ep =
1454
1455 if (status & mask)
1456 handle_ep0(ud
1457 for (i = 1; i < NUM_E
1458 mask <<= 1;
1459 if (status &
1460 handl
1461 ep++;
1462 }
1463 }
1464 }
1465
1466 return IRQ_HANDLED;
1467 }
1468
1469 /*-------------------------------------------
1470
1471 static void nop_release(struct device *dev)
1472 {
1473 /* nothing to free */
1474 }
1475
1476 static struct at91_udc controller = {
1477 .gadget = {
1478 .ops = &at91_udc_ops,
1479 .ep0 = &controller.ep[0].e
1480 .name = driver_name,
1481 .dev = {
1482 .bus_id = "gadget",
1483 .release = nop_releas
1484 }
1485 },
1486 .ep[0] = {
1487 .ep = {
1488 .name = ep0name,
1489 .ops = &at91_ep_op
1490 },
1491 .udc = &controller
1492 .maxpacket = 8,
1493 .int_mask = 1 << 0,
1494 },
1495 .ep[1] = {
1496 .ep = {
1497 .name = "ep1",
1498 .ops = &at91_ep_op
1499 },
1500 .udc = &controller
1501 .is_pingpong = 1,
1502 .maxpacket = 64,
1503 .int_mask = 1 << 1,
1504 },
1505 .ep[2] = {
1506 .ep = {
1507 .name = "ep2",
1508 .ops = &at91_ep_op
1509 },
1510 .udc = &controller
1511 .is_pingpong = 1,
1512 .maxpacket = 64,
1513 .int_mask = 1 << 2,
1514 },
1515 .ep[3] = {
1516 .ep = {
1517 /* could actually do
1518 .name = "ep3-int",
1519 .ops = &at91_ep_op
1520 },
1521 .udc = &controller
1522 .maxpacket = 8,
1523 .int_mask = 1 << 3,
1524 },
1525 .ep[4] = {
1526 .ep = {
1527 .name = "ep4",
1528 .ops = &at91_ep_op
1529 },
1530 .udc = &controller
1531 .is_pingpong = 1,
1532 .maxpacket = 256,
1533 .int_mask = 1 << 4,
1534 },
1535 .ep[5] = {
1536 .ep = {
1537 .name = "ep5",
1538 .ops = &at91_ep_op
1539 },
1540 .udc = &controller
1541 .is_pingpong = 1,
1542 .maxpacket = 256,
1543 .int_mask = 1 << 5,
1544 },
1545 /* ep6 and ep7 are also reserved (cus
1546 };
1547
1548 static irqreturn_t at91_vbus_irq(int irq, voi
1549 {
1550 struct at91_udc *udc = _udc;
1551 unsigned value;
1552
1553 /* vbus needs at least brief debounci
1554 udelay(10);
1555 value = gpio_get_value(udc->board.vbu
1556 if (value != udc->vbus)
1557 at91_vbus_session(&udc->gadge
1558
1559 return IRQ_HANDLED;
1560 }
1561
1562 int usb_gadget_register_driver (struct usb_ga
1563 {
1564 struct at91_udc *udc = &controller;
1565 int retval;
1566
1567 if (!driver
1568 || driver->speed < US
1569 || !driver->bind
1570 || !driver->setup) {
1571 DBG("bad parameter.\n");
1572 return -EINVAL;
1573 }
1574
1575 if (udc->driver) {
1576 DBG("UDC already has a gadget
1577 return -EBUSY;
1578 }
1579
1580 udc->driver = driver;
1581 udc->gadget.dev.driver = &driver->dri
1582 udc->gadget.dev.driver_data = &driver
1583 udc->enabled = 1;
1584 udc->selfpowered = 1;
1585
1586 retval = driver->bind(&udc->gadget);
1587 if (retval) {
1588 DBG("driver->bind() returned
1589 udc->driver = NULL;
1590 udc->gadget.dev.driver = NULL
1591 udc->gadget.dev.driver_data =
1592 udc->enabled = 0;
1593 udc->selfpowered = 0;
1594 return retval;
1595 }
1596
1597 local_irq_disable();
1598 pullup(udc, 1);
1599 local_irq_enable();
1600
1601 DBG("bound to %s\n", driver->driver.n
1602 return 0;
1603 }
1604 EXPORT_SYMBOL (usb_gadget_register_driver);
1605
1606 int usb_gadget_unregister_driver (struct usb_
1607 {
1608 struct at91_udc *udc = &controller;
1609
1610 if (!driver || driver != udc->driver
1611 return -EINVAL;
1612
1613 local_irq_disable();
1614 udc->enabled = 0;
1615 at91_udp_write(udc, AT91_UDP_IDR, ~0)
1616 pullup(udc, 0);
1617 local_irq_enable();
1618
1619 driver->unbind(&udc->gadget);
1620 udc->gadget.dev.driver = NULL;
1621 udc->gadget.dev.driver_data = NULL;
1622 udc->driver = NULL;
1623
1624 DBG("unbound from %s\n", driver->driv
1625 return 0;
1626 }
1627 EXPORT_SYMBOL (usb_gadget_unregister_driver);
1628
1629 /*-------------------------------------------
1630
1631 static void at91udc_shutdown(struct platform_
1632 {
1633 /* force disconnect on reboot */
1634 pullup(platform_get_drvdata(dev), 0);
1635 }
1636
1637 static int __init at91udc_probe(struct platfo
1638 {
1639 struct device *dev = &pdev->dev;
1640 struct at91_udc *udc;
1641 int retval;
1642 struct resource *res;
1643
1644 if (!dev->platform_data) {
1645 /* small (so we copy it) but
1646 DBG("missing platform_data\n"
1647 return -ENODEV;
1648 }
1649
1650 if (pdev->num_resources != 2) {
1651 DBG("invalid num_resources\n"
1652 return -ENODEV;
1653 }
1654 if ((pdev->resource[0].flags != IORES
1655 || (pdev->resource[1]
1656 DBG("invalid resource type\n"
1657 return -ENODEV;
1658 }
1659
1660 res = platform_get_resource(pdev, IOR
1661 if (!res)
1662 return -ENXIO;
1663
1664 if (!request_mem_region(res->start,
1665 res->end - res->start
1666 driver_name)) {
1667 DBG("someone's using UDC memo
1668 return -EBUSY;
1669 }
1670
1671 /* init software state */
1672 udc = &controller;
1673 udc->gadget.dev.parent = dev;
1674 udc->board = *(struct at91_udc_data *
1675 udc->pdev = pdev;
1676 udc->enabled = 0;
1677
1678 /* rm9200 needs manual D+ pullup; off
1679 if (cpu_is_at91rm9200()) {
1680 if (udc->board.pullup_pin <=
1681 DBG("no D+ pullup?\n"
1682 retval = -ENODEV;
1683 goto fail0;
1684 }
1685 retval = gpio_request(udc->bo
1686 if (retval) {
1687 DBG("D+ pullup is bus
1688 goto fail0;
1689 }
1690 gpio_direction_output(udc->bo
1691 udc->board.pu
1692 }
1693
1694 udc->udp_baseaddr = ioremap(res->star
1695 if (!udc->udp_baseaddr) {
1696 retval = -ENOMEM;
1697 goto fail0a;
1698 }
1699
1700 udc_reinit(udc);
1701
1702 /* get interface and function clocks
1703 udc->iclk = clk_get(dev, "udc_clk");
1704 udc->fclk = clk_get(dev, "udpck");
1705 if (IS_ERR(udc->iclk) || IS_ERR(udc->
1706 DBG("clocks missing\n");
1707 retval = -ENODEV;
1708 /* NOTE: we "know" here that
1709 goto fail0b;
1710 }
1711
1712 retval = device_register(&udc->gadget
1713 if (retval < 0)
1714 goto fail0b;
1715
1716 /* don't do anything until we have bo
1717 clk_enable(udc->iclk);
1718 at91_udp_write(udc, AT91_UDP_TXVC, AT
1719 at91_udp_write(udc, AT91_UDP_IDR, 0xf
1720 /* Clear all pending interrupts - UDP
1721 at91_udp_write(udc, AT91_UDP_ICR, 0xf
1722 clk_disable(udc->iclk);
1723
1724 /* request UDC and maybe VBUS irqs */
1725 udc->udp_irq = platform_get_irq(pdev,
1726 retval = request_irq(udc->udp_irq, at
1727 IRQF_DISABLED, driver
1728 if (retval < 0) {
1729 DBG("request irq %d failed\n"
1730 goto fail1;
1731 }
1732 if (udc->board.vbus_pin > 0) {
1733 retval = gpio_request(udc->bo
1734 if (retval < 0) {
1735 DBG("request vbus pin
1736 goto fail2;
1737 }
1738 gpio_direction_input(udc->boa
1739
1740 /*
1741 * Get the initial state of V
1742 * a pending interrupt.
1743 */
1744 udc->vbus = gpio_get_value(ud
1745 if (request_irq(udc->board.vb
1746 IRQF_DISABLED
1747 DBG("request vbus irq
1748 udc->
1749 free_irq(udc->udp_irq
1750 retval = -EBUSY;
1751 goto fail3;
1752 }
1753 } else {
1754 DBG("no VBUS detection, assum
1755 udc->vbus = 1;
1756 }
1757 dev_set_drvdata(dev, udc);
1758 device_init_wakeup(dev, 1);
1759 create_debug_file(udc);
1760
1761 INFO("%s version %s\n", driver_name,
1762 return 0;
1763
1764 fail3:
1765 if (udc->board.vbus_pin > 0)
1766 gpio_free(udc->board.vbus_pin
1767 fail2:
1768 free_irq(udc->udp_irq, udc);
1769 fail1:
1770 device_unregister(&udc->gadget.dev);
1771 fail0b:
1772 iounmap(udc->udp_baseaddr);
1773 fail0a:
1774 if (cpu_is_at91rm9200())
1775 gpio_free(udc->board.pullup_p
1776 fail0:
1777 release_mem_region(res->start, res->e
1778 DBG("%s probe failed, %d\n", driver_n
1779 return retval;
1780 }
1781
1782 static int __exit at91udc_remove(struct platf
1783 {
1784 struct at91_udc *udc = platform_get_d
1785 struct resource *res;
1786
1787 DBG("remove\n");
1788
1789 if (udc->driver)
1790 return -EBUSY;
1791
1792 pullup(udc, 0);
1793
1794 device_init_wakeup(&pdev->dev, 0);
1795 remove_debug_file(udc);
1796 if (udc->board.vbus_pin > 0) {
1797 free_irq(udc->board.vbus_pin,
1798 gpio_free(udc->board.vbus_pin
1799 }
1800 free_irq(udc->udp_irq, udc);
1801 device_unregister(&udc->gadget.dev);
1802
1803 iounmap(udc->udp_baseaddr);
1804
1805 if (cpu_is_at91rm9200())
1806 gpio_free(udc->board.pullup_p
1807
1808 res = platform_get_resource(pdev, IOR
1809 release_mem_region(res->start, res->e
1810
1811 clk_put(udc->iclk);
1812 clk_put(udc->fclk);
1813
1814 return 0;
1815 }
1816
1817 #ifdef CONFIG_PM
1818 static int at91udc_suspend(struct platform_de
1819 {
1820 struct at91_udc *udc = platform_get_d
1821 int wake = udc->driver &&
1822
1823 /* Unless we can act normally to the
1824 * whenever it has work for us) force
1825 * PLLB for USB events (signaling for
1826 * tokens) and VBUS irqs (on systems
1827 */
1828 if ((!udc->suspended && udc->addr)
1829 || !wake
1830 || at91_suspend_enter
1831 pullup(udc, 0);
1832 wake = 0;
1833 } else
1834 enable_irq_wake(udc->udp_irq)
1835
1836 udc->active_suspend = wake;
1837 if (udc->board.vbus_pin > 0 && wake)
1838 enable_irq_wake(udc->board.vb
1839 return 0;
1840 }
1841
1842 static int at91udc_resume(struct platform_dev
1843 {
1844 struct at91_udc *udc = platform_get_d
1845
1846 if (udc->board.vbus_pin > 0 && udc->a
1847 disable_irq_wake(udc->board.v
1848
1849 /* maybe reconnect to host; if so, cl
1850 if (udc->active_suspend)
1851 disable_irq_wake(udc->udp_irq
1852 else
1853 pullup(udc, 1);
1854 return 0;
1855 }
1856 #else
1857 #define at91udc_suspend NULL
1858 #define at91udc_resume NULL
1859 #endif
1860
1861 static struct platform_driver at91_udc_driver
1862 .remove = __exit_p(at91udc_re
1863 .shutdown = at91udc_shutdown,
1864 .suspend = at91udc_suspend,
1865 .resume = at91udc_resume,
1866 .driver = {
1867 .name = (char *) driver_nam
1868 .owner = THIS_MODULE,
1869 },
1870 };
1871
1872 static int __init udc_init_module(void)
1873 {
1874 return platform_driver_probe(&at91_ud
1875 }
1876 module_init(udc_init_module);
1877
1878 static void __exit udc_exit_module(void)
1879 {
1880 platform_driver_unregister(&at91_udc_
1881 }
1882 module_exit(udc_exit_module);
1883
1884 MODULE_DESCRIPTION("AT91 udc driver");
1885 MODULE_AUTHOR("Thomas Rathbone, David Brownel
1886 MODULE_LICENSE("GPL");
1887 MODULE_ALIAS("platform:at91_udc");
1888
| This page was automatically generated by the LXR engine. |