]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/host/r8a66597-hcd.c
USB: r8a66597-hcd: fix usb device connection timing
[linux-2.6-omap-h63xx.git] / drivers / usb / host / r8a66597-hcd.c
1 /*
2  * R8A66597 HCD (Host Controller Driver)
3  *
4  * Copyright (C) 2006-2007 Renesas Solutions Corp.
5  * Portions Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
6  * Portions Copyright (C) 2004-2005 David Brownell
7  * Portions Copyright (C) 1999 Roman Weissgaerber
8  *
9  * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; version 2 of the License.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23  *
24  */
25
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/smp_lock.h>
30 #include <linux/errno.h>
31 #include <linux/init.h>
32 #include <linux/timer.h>
33 #include <linux/delay.h>
34 #include <linux/list.h>
35 #include <linux/interrupt.h>
36 #include <linux/usb.h>
37 #include <linux/platform_device.h>
38 #include <linux/io.h>
39 #include <linux/irq.h>
40
41 #include "../core/hcd.h"
42 #include "r8a66597.h"
43
44 MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
45 MODULE_LICENSE("GPL");
46 MODULE_AUTHOR("Yoshihiro Shimoda");
47 MODULE_ALIAS("platform:r8a66597_hcd");
48
49 #define DRIVER_VERSION  "10 Apr 2008"
50
51 static const char hcd_name[] = "r8a66597_hcd";
52
53 /* module parameters */
54 static unsigned short clock = XTAL12;
55 module_param(clock, ushort, 0644);
56 MODULE_PARM_DESC(clock, "input clock: 48MHz=32768, 24MHz=16384, 12MHz=0 "
57                 "(default=0)");
58
59 static unsigned short vif = LDRV;
60 module_param(vif, ushort, 0644);
61 MODULE_PARM_DESC(vif, "input VIF: 3.3V=32768, 1.5V=0(default=32768)");
62
63 static unsigned short endian;
64 module_param(endian, ushort, 0644);
65 MODULE_PARM_DESC(endian, "data endian: big=256, little=0 (default=0)");
66
67 static unsigned short irq_sense = INTL;
68 module_param(irq_sense, ushort, 0644);
69 MODULE_PARM_DESC(irq_sense, "IRQ sense: low level=32, falling edge=0 "
70                 "(default=32)");
71
72 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
73 static int r8a66597_get_frame(struct usb_hcd *hcd);
74
75 /* this function must be called with interrupt disabled */
76 static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
77                             unsigned long reg)
78 {
79         u16 tmp;
80
81         tmp = r8a66597_read(r8a66597, INTENB0);
82         r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
83         r8a66597_bset(r8a66597, 1 << pipenum, reg);
84         r8a66597_write(r8a66597, tmp, INTENB0);
85 }
86
87 /* this function must be called with interrupt disabled */
88 static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
89                              unsigned long reg)
90 {
91         u16 tmp;
92
93         tmp = r8a66597_read(r8a66597, INTENB0);
94         r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
95         r8a66597_bclr(r8a66597, 1 << pipenum, reg);
96         r8a66597_write(r8a66597, tmp, INTENB0);
97 }
98
99 static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
100                            u16 usbspd, u8 upphub, u8 hubport, int port)
101 {
102         u16 val;
103         unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
104
105         val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
106         r8a66597_write(r8a66597, val, devadd_reg);
107 }
108
109 static int enable_controller(struct r8a66597 *r8a66597)
110 {
111         u16 tmp;
112         int i = 0;
113
114         do {
115                 r8a66597_write(r8a66597, USBE, SYSCFG0);
116                 tmp = r8a66597_read(r8a66597, SYSCFG0);
117                 if (i++ > 1000) {
118                         err("register access fail.");
119                         return -ENXIO;
120                 }
121         } while ((tmp & USBE) != USBE);
122         r8a66597_bclr(r8a66597, USBE, SYSCFG0);
123         r8a66597_mdfy(r8a66597, clock, XTAL, SYSCFG0);
124
125         i = 0;
126         r8a66597_bset(r8a66597, XCKE, SYSCFG0);
127         do {
128                 msleep(1);
129                 tmp = r8a66597_read(r8a66597, SYSCFG0);
130                 if (i++ > 500) {
131                         err("register access fail.");
132                         return -ENXIO;
133                 }
134         } while ((tmp & SCKE) != SCKE);
135
136         r8a66597_bset(r8a66597, DCFM | DRPD, SYSCFG0);
137         r8a66597_bset(r8a66597, DRPD, SYSCFG1);
138
139         r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
140         r8a66597_bset(r8a66597, HSE, SYSCFG0);
141         r8a66597_bset(r8a66597, HSE, SYSCFG1);
142         r8a66597_bset(r8a66597, USBE, SYSCFG0);
143
144         r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
145         r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
146         r8a66597_bset(r8a66597, BRDY0, BRDYENB);
147         r8a66597_bset(r8a66597, BEMP0, BEMPENB);
148
149         r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, DMA0CFG);
150         r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, DMA1CFG);
151
152         r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
153         r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
154         r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
155
156         r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
157
158         r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
159         r8a66597_bclr(r8a66597, DTCHE, INTENB1);
160         r8a66597_bset(r8a66597, ATTCHE, INTENB1);
161         r8a66597_bclr(r8a66597, DTCHE, INTENB2);
162         r8a66597_bset(r8a66597, ATTCHE, INTENB2);
163
164         return 0;
165 }
166
167 static void disable_controller(struct r8a66597 *r8a66597)
168 {
169         u16 tmp;
170
171         r8a66597_write(r8a66597, 0, INTENB0);
172         r8a66597_write(r8a66597, 0, INTENB1);
173         r8a66597_write(r8a66597, 0, INTENB2);
174         r8a66597_write(r8a66597, 0, INTSTS0);
175         r8a66597_write(r8a66597, 0, INTSTS1);
176         r8a66597_write(r8a66597, 0, INTSTS2);
177
178         r8a66597_port_power(r8a66597, 0, 0);
179         r8a66597_port_power(r8a66597, 1, 0);
180
181         do {
182                 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
183                 udelay(640);
184         } while (tmp == EDGESTS);
185
186         r8a66597_bclr(r8a66597, DCFM | DRPD, SYSCFG0);
187         r8a66597_bclr(r8a66597, DRPD, SYSCFG1);
188         r8a66597_bclr(r8a66597, HSE, SYSCFG0);
189         r8a66597_bclr(r8a66597, HSE, SYSCFG1);
190
191         r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
192         udelay(1);
193         r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
194         r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
195         r8a66597_bclr(r8a66597, USBE, SYSCFG0);
196 }
197
198 static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
199                                        struct usb_device *udev)
200 {
201         struct r8a66597_device *dev;
202
203         if (udev->parent && udev->parent->devnum != 1)
204                 udev = udev->parent;
205
206         dev = dev_get_drvdata(&udev->dev);
207         if (dev)
208                 return dev->address;
209         else
210                 return 0;
211 }
212
213 static int is_child_device(char *devpath)
214 {
215         return (devpath[2] ? 1 : 0);
216 }
217
218 static int is_hub_limit(char *devpath)
219 {
220         return ((strlen(devpath) >= 4) ? 1 : 0);
221 }
222
223 static void get_port_number(char *devpath, u16 *root_port, u16 *hub_port)
224 {
225         if (root_port) {
226                 *root_port = (devpath[0] & 0x0F) - 1;
227                 if (*root_port >= R8A66597_MAX_ROOT_HUB)
228                         err("illegal root port number");
229         }
230         if (hub_port)
231                 *hub_port = devpath[2] & 0x0F;
232 }
233
234 static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
235 {
236         u16 usbspd = 0;
237
238         switch (speed) {
239         case USB_SPEED_LOW:
240                 usbspd = LSMODE;
241                 break;
242         case USB_SPEED_FULL:
243                 usbspd = FSMODE;
244                 break;
245         case USB_SPEED_HIGH:
246                 usbspd = HSMODE;
247                 break;
248         default:
249                 err("unknown speed");
250                 break;
251         }
252
253         return usbspd;
254 }
255
256 static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
257 {
258         int idx;
259
260         idx = address / 32;
261         r8a66597->child_connect_map[idx] |= 1 << (address % 32);
262 }
263
264 static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
265 {
266         int idx;
267
268         idx = address / 32;
269         r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
270 }
271
272 static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
273 {
274         u16 pipenum = pipe->info.pipenum;
275         unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
276         unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
277         unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
278
279         if (dma_ch > R8A66597_PIPE_NO_DMA)      /* dma fifo not use? */
280                 dma_ch = R8A66597_PIPE_NO_DMA;
281
282         pipe->fifoaddr = fifoaddr[dma_ch];
283         pipe->fifosel = fifosel[dma_ch];
284         pipe->fifoctr = fifoctr[dma_ch];
285
286         if (pipenum == 0)
287                 pipe->pipectr = DCPCTR;
288         else
289                 pipe->pipectr = get_pipectr_addr(pipenum);
290
291         if (check_bulk_or_isoc(pipenum)) {
292                 pipe->pipetre = get_pipetre_addr(pipenum);
293                 pipe->pipetrn = get_pipetrn_addr(pipenum);
294         } else {
295                 pipe->pipetre = 0;
296                 pipe->pipetrn = 0;
297         }
298 }
299
300 static struct r8a66597_device *
301 get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
302 {
303         if (usb_pipedevice(urb->pipe) == 0)
304                 return &r8a66597->device0;
305
306         return dev_get_drvdata(&urb->dev->dev);
307 }
308
309 static int make_r8a66597_device(struct r8a66597 *r8a66597,
310                                 struct urb *urb, u8 addr)
311 {
312         struct r8a66597_device *dev;
313         int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
314
315         dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
316         if (dev == NULL)
317                 return -ENOMEM;
318
319         dev_set_drvdata(&urb->dev->dev, dev);
320         dev->udev = urb->dev;
321         dev->address = addr;
322         dev->usb_address = usb_address;
323         dev->state = USB_STATE_ADDRESS;
324         dev->ep_in_toggle = 0;
325         dev->ep_out_toggle = 0;
326         INIT_LIST_HEAD(&dev->device_list);
327         list_add_tail(&dev->device_list, &r8a66597->child_device);
328
329         get_port_number(urb->dev->devpath, &dev->root_port, &dev->hub_port);
330         if (!is_child_device(urb->dev->devpath))
331                 r8a66597->root_hub[dev->root_port].dev = dev;
332
333         set_devadd_reg(r8a66597, dev->address,
334                        get_r8a66597_usb_speed(urb->dev->speed),
335                        get_parent_r8a66597_address(r8a66597, urb->dev),
336                        dev->hub_port, dev->root_port);
337
338         return 0;
339 }
340
341 /* this function must be called with interrupt disabled */
342 static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
343 {
344         u8 addr;        /* R8A66597's address */
345         struct r8a66597_device *dev;
346
347         if (is_hub_limit(urb->dev->devpath)) {
348                 err("Externel hub limit reached.");
349                 return 0;
350         }
351
352         dev = get_urb_to_r8a66597_dev(r8a66597, urb);
353         if (dev && dev->state >= USB_STATE_ADDRESS)
354                 return dev->address;
355
356         for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
357                 if (r8a66597->address_map & (1 << addr))
358                         continue;
359
360                 dbg("alloc_address: r8a66597_addr=%d", addr);
361                 r8a66597->address_map |= 1 << addr;
362
363                 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
364                         return 0;
365
366                 return addr;
367         }
368
369         err("cannot communicate with a USB device more than 10.(%x)",
370             r8a66597->address_map);
371
372         return 0;
373 }
374
375 /* this function must be called with interrupt disabled */
376 static void free_usb_address(struct r8a66597 *r8a66597,
377                              struct r8a66597_device *dev)
378 {
379         int port;
380
381         if (!dev)
382                 return;
383
384         dbg("free_addr: addr=%d", dev->address);
385
386         dev->state = USB_STATE_DEFAULT;
387         r8a66597->address_map &= ~(1 << dev->address);
388         dev->address = 0;
389         dev_set_drvdata(&dev->udev->dev, NULL);
390         list_del(&dev->device_list);
391         kfree(dev);
392
393         for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
394                 if (r8a66597->root_hub[port].dev == dev) {
395                         r8a66597->root_hub[port].dev = NULL;
396                         break;
397                 }
398         }
399 }
400
401 static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
402                               u16 mask, u16 loop)
403 {
404         u16 tmp;
405         int i = 0;
406
407         do {
408                 tmp = r8a66597_read(r8a66597, reg);
409                 if (i++ > 1000000) {
410                         err("register%lx, loop %x is timeout", reg, loop);
411                         break;
412                 }
413                 ndelay(1);
414         } while ((tmp & mask) != loop);
415 }
416
417 /* this function must be called with interrupt disabled */
418 static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
419 {
420         u16 tmp;
421
422         tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
423         if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
424                 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
425         r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
426 }
427
428 /* this function must be called with interrupt disabled */
429 static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
430 {
431         u16 tmp;
432
433         tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
434         if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
435                 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
436         r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
437         r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
438 }
439
440 /* this function must be called with interrupt disabled */
441 static void clear_all_buffer(struct r8a66597 *r8a66597,
442                              struct r8a66597_pipe *pipe)
443 {
444         u16 tmp;
445
446         if (!pipe || pipe->info.pipenum == 0)
447                 return;
448
449         pipe_stop(r8a66597, pipe);
450         r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
451         tmp = r8a66597_read(r8a66597, pipe->pipectr);
452         tmp = r8a66597_read(r8a66597, pipe->pipectr);
453         tmp = r8a66597_read(r8a66597, pipe->pipectr);
454         r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
455 }
456
457 /* this function must be called with interrupt disabled */
458 static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
459                                  struct r8a66597_pipe *pipe, int toggle)
460 {
461         if (toggle)
462                 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
463         else
464                 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
465 }
466
467 /* this function must be called with interrupt disabled */
468 static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
469 {
470         r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
471         r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
472 }
473
474 /* this function must be called with interrupt disabled */
475 static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
476                                          struct r8a66597_pipe *pipe)
477 {
478         cfifo_change(r8a66597, 0);
479         r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D0FIFOSEL);
480         r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D1FIFOSEL);
481
482         r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum, MBW | CURPIPE,
483                       pipe->fifosel);
484         r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
485 }
486
487 static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
488 {
489         struct r8a66597_pipe *pipe = hep->hcpriv;
490
491         if (usb_pipeendpoint(urb->pipe) == 0)
492                 return 0;
493         else
494                 return pipe->info.pipenum;
495 }
496
497 static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
498 {
499         struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
500
501         return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
502 }
503
504 static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
505                                           int urb_pipe)
506 {
507         if (!dev)
508                 return NULL;
509
510         return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
511 }
512
513 /* this function must be called with interrupt disabled */
514 static void pipe_toggle_set(struct r8a66597 *r8a66597,
515                             struct r8a66597_pipe *pipe,
516                             struct urb *urb, int set)
517 {
518         struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
519         unsigned char endpoint = usb_pipeendpoint(urb->pipe);
520         unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
521
522         if (!toggle)
523                 return;
524
525         if (set)
526                 *toggle |= 1 << endpoint;
527         else
528                 *toggle &= ~(1 << endpoint);
529 }
530
531 /* this function must be called with interrupt disabled */
532 static void pipe_toggle_save(struct r8a66597 *r8a66597,
533                              struct r8a66597_pipe *pipe,
534                              struct urb *urb)
535 {
536         if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
537                 pipe_toggle_set(r8a66597, pipe, urb, 1);
538         else
539                 pipe_toggle_set(r8a66597, pipe, urb, 0);
540 }
541
542 /* this function must be called with interrupt disabled */
543 static void pipe_toggle_restore(struct r8a66597 *r8a66597,
544                                 struct r8a66597_pipe *pipe,
545                                 struct urb *urb)
546 {
547         struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
548         unsigned char endpoint = usb_pipeendpoint(urb->pipe);
549         unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
550
551         if (!toggle)
552                 return;
553
554         r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
555 }
556
557 /* this function must be called with interrupt disabled */
558 static void pipe_buffer_setting(struct r8a66597 *r8a66597,
559                                 struct r8a66597_pipe_info *info)
560 {
561         u16 val = 0;
562
563         if (info->pipenum == 0)
564                 return;
565
566         r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
567         r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
568         r8a66597_write(r8a66597, info->pipenum, PIPESEL);
569         if (!info->dir_in)
570                 val |= R8A66597_DIR;
571         if (info->type == R8A66597_BULK && info->dir_in)
572                 val |= R8A66597_DBLB | R8A66597_SHTNAK;
573         val |= info->type | info->epnum;
574         r8a66597_write(r8a66597, val, PIPECFG);
575
576         r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
577                        PIPEBUF);
578         r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
579                        PIPEMAXP);
580         r8a66597_write(r8a66597, info->interval, PIPEPERI);
581 }
582
583 /* this function must be called with interrupt disabled */
584 static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
585 {
586         struct r8a66597_pipe_info *info;
587         struct urb *urb = td->urb;
588
589         if (td->pipenum > 0) {
590                 info = &td->pipe->info;
591                 cfifo_change(r8a66597, 0);
592                 pipe_buffer_setting(r8a66597, info);
593
594                 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
595                                    usb_pipeout(urb->pipe)) &&
596                     !usb_pipecontrol(urb->pipe)) {
597                         r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
598                         pipe_toggle_set(r8a66597, td->pipe, urb, 0);
599                         clear_all_buffer(r8a66597, td->pipe);
600                         usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
601                                       usb_pipeout(urb->pipe), 1);
602                 }
603                 pipe_toggle_restore(r8a66597, td->pipe, urb);
604         }
605 }
606
607 /* this function must be called with interrupt disabled */
608 static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
609                              struct usb_endpoint_descriptor *ep)
610 {
611         u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
612
613         memset(array, 0, sizeof(array));
614         switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
615         case USB_ENDPOINT_XFER_BULK:
616                 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
617                         array[i++] = 4;
618                 else {
619                         array[i++] = 3;
620                         array[i++] = 5;
621                 }
622                 break;
623         case USB_ENDPOINT_XFER_INT:
624                 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
625                         array[i++] = 6;
626                         array[i++] = 7;
627                         array[i++] = 8;
628                 } else
629                         array[i++] = 9;
630                 break;
631         case USB_ENDPOINT_XFER_ISOC:
632                 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
633                         array[i++] = 2;
634                 else
635                         array[i++] = 1;
636                 break;
637         default:
638                 err("Illegal type");
639                 return 0;
640         }
641
642         i = 1;
643         min = array[0];
644         while (array[i] != 0) {
645                 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
646                         min = array[i];
647                 i++;
648         }
649
650         return min;
651 }
652
653 static u16 get_r8a66597_type(__u8 type)
654 {
655         u16 r8a66597_type;
656
657         switch (type) {
658         case USB_ENDPOINT_XFER_BULK:
659                 r8a66597_type = R8A66597_BULK;
660                 break;
661         case USB_ENDPOINT_XFER_INT:
662                 r8a66597_type = R8A66597_INT;
663                 break;
664         case USB_ENDPOINT_XFER_ISOC:
665                 r8a66597_type = R8A66597_ISO;
666                 break;
667         default:
668                 err("Illegal type");
669                 r8a66597_type = 0x0000;
670                 break;
671         }
672
673         return r8a66597_type;
674 }
675
676 static u16 get_bufnum(u16 pipenum)
677 {
678         u16 bufnum = 0;
679
680         if (pipenum == 0)
681                 bufnum = 0;
682         else if (check_bulk_or_isoc(pipenum))
683                 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
684         else if (check_interrupt(pipenum))
685                 bufnum = 4 + (pipenum - 6);
686         else
687                 err("Illegal pipenum (%d)", pipenum);
688
689         return bufnum;
690 }
691
692 static u16 get_buf_bsize(u16 pipenum)
693 {
694         u16 buf_bsize = 0;
695
696         if (pipenum == 0)
697                 buf_bsize = 3;
698         else if (check_bulk_or_isoc(pipenum))
699                 buf_bsize = R8A66597_BUF_BSIZE - 1;
700         else if (check_interrupt(pipenum))
701                 buf_bsize = 0;
702         else
703                 err("Illegal pipenum (%d)", pipenum);
704
705         return buf_bsize;
706 }
707
708 /* this function must be called with interrupt disabled */
709 static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
710                                      struct r8a66597_device *dev,
711                                      struct r8a66597_pipe *pipe,
712                                      struct urb *urb)
713 {
714         int i;
715         struct r8a66597_pipe_info *info = &pipe->info;
716
717         if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
718                 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
719                         if ((r8a66597->dma_map & (1 << i)) != 0)
720                                 continue;
721
722                         info("address %d, EndpointAddress 0x%02x use DMA FIFO",
723                              usb_pipedevice(urb->pipe),
724                              info->dir_in ? USB_ENDPOINT_DIR_MASK + info->epnum
725                                             : info->epnum);
726
727                         r8a66597->dma_map |= 1 << i;
728                         dev->dma_map |= 1 << i;
729                         set_pipe_reg_addr(pipe, i);
730
731                         cfifo_change(r8a66597, 0);
732                         r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum,
733                                       MBW | CURPIPE, pipe->fifosel);
734
735                         r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
736                                           pipe->info.pipenum);
737                         r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
738                         break;
739                 }
740         }
741 }
742
743 /* this function must be called with interrupt disabled */
744 static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
745                                  struct usb_host_endpoint *hep,
746                                  struct r8a66597_pipe_info *info)
747 {
748         struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
749         struct r8a66597_pipe *pipe = hep->hcpriv;
750
751         dbg("enable_pipe:");
752
753         pipe->info = *info;
754         set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
755         r8a66597->pipe_cnt[pipe->info.pipenum]++;
756         dev->pipe_cnt[pipe->info.pipenum]++;
757
758         enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
759 }
760
761 /* this function must be called with interrupt disabled */
762 static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
763 {
764         struct r8a66597_td *td, *next;
765         struct urb *urb;
766         struct list_head *list = &r8a66597->pipe_queue[pipenum];
767
768         if (list_empty(list))
769                 return;
770
771         list_for_each_entry_safe(td, next, list, queue) {
772                 if (!td)
773                         continue;
774                 if (td->address != address)
775                         continue;
776
777                 urb = td->urb;
778                 list_del(&td->queue);
779                 kfree(td);
780
781                 if (urb) {
782                         usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
783                                         urb);
784
785                         spin_unlock(&r8a66597->lock);
786                         usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
787                                         -ENODEV);
788                         spin_lock(&r8a66597->lock);
789                 }
790                 break;
791         }
792 }
793
794 /* this function must be called with interrupt disabled */
795 static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
796                                       struct r8a66597_device *dev)
797 {
798         int check_ep0 = 0;
799         u16 pipenum;
800
801         if (!dev)
802                 return;
803
804         for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
805                 if (!dev->pipe_cnt[pipenum])
806                         continue;
807
808                 if (!check_ep0) {
809                         check_ep0 = 1;
810                         force_dequeue(r8a66597, 0, dev->address);
811                 }
812
813                 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
814                 dev->pipe_cnt[pipenum] = 0;
815                 force_dequeue(r8a66597, pipenum, dev->address);
816         }
817
818         dbg("disable_pipe");
819
820         r8a66597->dma_map &= ~(dev->dma_map);
821         dev->dma_map = 0;
822 }
823
824 static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
825 {
826         __u8 i;
827         unsigned long time = 1;
828
829         if (usb_pipeisoc(urb->pipe))
830                 return 0;
831
832         if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
833                 for (i = 0; i < (interval - 1); i++)
834                         time *= 2;
835                 time = time * 125 / 1000;       /* uSOF -> msec */
836         } else {
837                 time = interval;
838         }
839
840         return time;
841 }
842
843 /* this function must be called with interrupt disabled */
844 static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
845                            struct usb_host_endpoint *hep,
846                            struct usb_endpoint_descriptor *ep)
847 {
848         struct r8a66597_pipe_info info;
849
850         info.pipenum = get_empty_pipenum(r8a66597, ep);
851         info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
852         info.epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
853         info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
854         info.type = get_r8a66597_type(ep->bmAttributes
855                                       & USB_ENDPOINT_XFERTYPE_MASK);
856         info.bufnum = get_bufnum(info.pipenum);
857         info.buf_bsize = get_buf_bsize(info.pipenum);
858         if (info.type == R8A66597_BULK) {
859                 info.interval = 0;
860                 info.timer_interval = 0;
861         } else {
862                 if (ep->bInterval > IITV)
863                         info.interval = IITV;
864                 else
865                         info.interval = ep->bInterval ? ep->bInterval - 1 : 0;
866                 info.timer_interval = get_timer_interval(urb, ep->bInterval);
867         }
868         if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
869                 info.dir_in = 1;
870         else
871                 info.dir_in = 0;
872
873         enable_r8a66597_pipe(r8a66597, urb, hep, &info);
874 }
875
876 static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
877 {
878         struct r8a66597_device *dev;
879
880         dev = get_urb_to_r8a66597_dev(r8a66597, urb);
881         dev->state = USB_STATE_CONFIGURED;
882 }
883
884 static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
885                             u16 pipenum)
886 {
887         if (pipenum == 0 && usb_pipeout(urb->pipe))
888                 enable_irq_empty(r8a66597, pipenum);
889         else
890                 enable_irq_ready(r8a66597, pipenum);
891
892         if (!usb_pipeisoc(urb->pipe))
893                 enable_irq_nrdy(r8a66597, pipenum);
894 }
895
896 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
897 {
898         disable_irq_ready(r8a66597, pipenum);
899         disable_irq_nrdy(r8a66597, pipenum);
900 }
901
902 /* this function must be called with interrupt disabled */
903 static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
904                                         u16 syssts)
905 {
906         if (syssts == SE0) {
907                 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
908                 return;
909         }
910
911         if (syssts == FS_JSTS)
912                 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
913         else if (syssts == LS_JSTS)
914                 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
915
916         r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
917         r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
918 }
919
920 /* this function must be called with interrupt disabled */
921 static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
922 {
923         u16 speed = get_rh_usb_speed(r8a66597, port);
924         struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
925
926         if (speed == HSMODE)
927                 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
928         else if (speed == LSMODE)
929                 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
930
931         rh->port &= ~(1 << USB_PORT_FEAT_RESET);
932         rh->port |= 1 << USB_PORT_FEAT_ENABLE;
933 }
934
935 /* this function must be called with interrupt disabled */
936 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
937 {
938         struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
939
940         r8a66597->root_hub[port].port &= ~(1 << USB_PORT_FEAT_CONNECTION);
941         r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_C_CONNECTION);
942
943         disable_r8a66597_pipe_all(r8a66597, dev);
944         free_usb_address(r8a66597, dev);
945
946         r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
947 }
948
949 /* this function must be called with interrupt disabled */
950 static void prepare_setup_packet(struct r8a66597 *r8a66597,
951                                  struct r8a66597_td *td)
952 {
953         int i;
954         u16 *p = (u16 *)td->urb->setup_packet;
955         unsigned long setup_addr = USBREQ;
956
957         r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
958                        DCPMAXP);
959         r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
960
961         for (i = 0; i < 4; i++) {
962                 r8a66597_write(r8a66597, cpu_to_le16(p[i]), setup_addr);
963                 setup_addr += 2;
964         }
965         r8a66597_write(r8a66597, SUREQ, DCPCTR);
966 }
967
968 /* this function must be called with interrupt disabled */
969 static void prepare_packet_read(struct r8a66597 *r8a66597,
970                                 struct r8a66597_td *td)
971 {
972         struct urb *urb = td->urb;
973
974         if (usb_pipecontrol(urb->pipe)) {
975                 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
976                 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
977                 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
978                 if (urb->actual_length == 0) {
979                         r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
980                         r8a66597_write(r8a66597, BCLR, CFIFOCTR);
981                 }
982                 pipe_irq_disable(r8a66597, td->pipenum);
983                 pipe_start(r8a66597, td->pipe);
984                 pipe_irq_enable(r8a66597, urb, td->pipenum);
985         } else {
986                 if (urb->actual_length == 0) {
987                         pipe_irq_disable(r8a66597, td->pipenum);
988                         pipe_setting(r8a66597, td);
989                         pipe_stop(r8a66597, td->pipe);
990                         r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
991
992                         if (td->pipe->pipetre) {
993                                 r8a66597_write(r8a66597, TRCLR,
994                                                 td->pipe->pipetre);
995                                 r8a66597_write(r8a66597,
996                                                 DIV_ROUND_UP
997                                                   (urb->transfer_buffer_length,
998                                                    td->maxpacket),
999                                                 td->pipe->pipetrn);
1000                                 r8a66597_bset(r8a66597, TRENB,
1001                                                 td->pipe->pipetre);
1002                         }
1003
1004                         pipe_start(r8a66597, td->pipe);
1005                         pipe_irq_enable(r8a66597, urb, td->pipenum);
1006                 }
1007         }
1008 }
1009
1010 /* this function must be called with interrupt disabled */
1011 static void prepare_packet_write(struct r8a66597 *r8a66597,
1012                                  struct r8a66597_td *td)
1013 {
1014         u16 tmp;
1015         struct urb *urb = td->urb;
1016
1017         if (usb_pipecontrol(urb->pipe)) {
1018                 pipe_stop(r8a66597, td->pipe);
1019                 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1020                 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1021                 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1022                 if (urb->actual_length == 0) {
1023                         r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1024                         r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1025                 }
1026         } else {
1027                 if (urb->actual_length == 0)
1028                         pipe_setting(r8a66597, td);
1029                 if (td->pipe->pipetre)
1030                         r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1031         }
1032         r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1033
1034         fifo_change_from_pipe(r8a66597, td->pipe);
1035         tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1036         if (unlikely((tmp & FRDY) == 0))
1037                 pipe_irq_enable(r8a66597, urb, td->pipenum);
1038         else
1039                 packet_write(r8a66597, td->pipenum);
1040         pipe_start(r8a66597, td->pipe);
1041 }
1042
1043 /* this function must be called with interrupt disabled */
1044 static void prepare_status_packet(struct r8a66597 *r8a66597,
1045                                   struct r8a66597_td *td)
1046 {
1047         struct urb *urb = td->urb;
1048
1049         r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1050         pipe_stop(r8a66597, td->pipe);
1051
1052         if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1053                 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1054                 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1055                 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1056                 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
1057                 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1058                 r8a66597_write(r8a66597, BVAL, CFIFOCTR);
1059                 enable_irq_empty(r8a66597, 0);
1060         } else {
1061                 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1062                 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1063                 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1064                 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1065                 enable_irq_ready(r8a66597, 0);
1066         }
1067         enable_irq_nrdy(r8a66597, 0);
1068         pipe_start(r8a66597, td->pipe);
1069 }
1070
1071 static int is_set_address(unsigned char *setup_packet)
1072 {
1073         if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1074                         setup_packet[1] == USB_REQ_SET_ADDRESS)
1075                 return 1;
1076         else
1077                 return 0;
1078 }
1079
1080 /* this function must be called with interrupt disabled */
1081 static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1082 {
1083         BUG_ON(!td);
1084
1085         switch (td->type) {
1086         case USB_PID_SETUP:
1087                 if (is_set_address(td->urb->setup_packet)) {
1088                         td->set_address = 1;
1089                         td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1090                                                                      td->urb);
1091                         if (td->urb->setup_packet[2] == 0)
1092                                 return -EPIPE;
1093                 }
1094                 prepare_setup_packet(r8a66597, td);
1095                 break;
1096         case USB_PID_IN:
1097                 prepare_packet_read(r8a66597, td);
1098                 break;
1099         case USB_PID_OUT:
1100                 prepare_packet_write(r8a66597, td);
1101                 break;
1102         case USB_PID_ACK:
1103                 prepare_status_packet(r8a66597, td);
1104                 break;
1105         default:
1106                 err("invalid type.");
1107                 break;
1108         }
1109
1110         return 0;
1111 }
1112
1113 static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1114 {
1115         if (usb_pipeisoc(urb->pipe)) {
1116                 if (urb->number_of_packets == td->iso_cnt)
1117                         return 1;
1118         }
1119
1120         /* control or bulk or interrupt */
1121         if ((urb->transfer_buffer_length <= urb->actual_length) ||
1122             (td->short_packet) || (td->zero_packet))
1123                 return 1;
1124
1125         return 0;
1126 }
1127
1128 /* this function must be called with interrupt disabled */
1129 static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1130 {
1131         unsigned long time;
1132
1133         BUG_ON(!td);
1134
1135         if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1136             !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1137                 r8a66597->timeout_map |= 1 << td->pipenum;
1138                 switch (usb_pipetype(td->urb->pipe)) {
1139                 case PIPE_INTERRUPT:
1140                 case PIPE_ISOCHRONOUS:
1141                         time = 30;
1142                         break;
1143                 default:
1144                         time = 300;
1145                         break;
1146                 }
1147
1148                 mod_timer(&r8a66597->td_timer[td->pipenum],
1149                           jiffies + msecs_to_jiffies(time));
1150         }
1151 }
1152
1153 /* this function must be called with interrupt disabled */
1154 static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1155                 u16 pipenum, struct urb *urb, int status)
1156 __releases(r8a66597->lock) __acquires(r8a66597->lock)
1157 {
1158         int restart = 0;
1159         struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1160
1161         r8a66597->timeout_map &= ~(1 << pipenum);
1162
1163         if (likely(td)) {
1164                 if (td->set_address && (status != 0 || urb->unlinked))
1165                         r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1166
1167                 pipe_toggle_save(r8a66597, td->pipe, urb);
1168                 list_del(&td->queue);
1169                 kfree(td);
1170         }
1171
1172         if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1173                 restart = 1;
1174
1175         if (likely(urb)) {
1176                 if (usb_pipeisoc(urb->pipe))
1177                         urb->start_frame = r8a66597_get_frame(hcd);
1178
1179                 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
1180                 spin_unlock(&r8a66597->lock);
1181                 usb_hcd_giveback_urb(hcd, urb, status);
1182                 spin_lock(&r8a66597->lock);
1183         }
1184
1185         if (restart) {
1186                 td = r8a66597_get_td(r8a66597, pipenum);
1187                 if (unlikely(!td))
1188                         return;
1189
1190                 start_transfer(r8a66597, td);
1191                 set_td_timer(r8a66597, td);
1192         }
1193 }
1194
1195 static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1196 {
1197         u16 tmp;
1198         int rcv_len, bufsize, urb_len, size;
1199         u16 *buf;
1200         struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1201         struct urb *urb;
1202         int finish = 0;
1203         int status = 0;
1204
1205         if (unlikely(!td))
1206                 return;
1207         urb = td->urb;
1208
1209         fifo_change_from_pipe(r8a66597, td->pipe);
1210         tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1211         if (unlikely((tmp & FRDY) == 0)) {
1212                 pipe_stop(r8a66597, td->pipe);
1213                 pipe_irq_disable(r8a66597, pipenum);
1214                 err("in fifo not ready (%d)", pipenum);
1215                 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
1216                 return;
1217         }
1218
1219         /* prepare parameters */
1220         rcv_len = tmp & DTLN;
1221         if (usb_pipeisoc(urb->pipe)) {
1222                 buf = (u16 *)(urb->transfer_buffer +
1223                                 urb->iso_frame_desc[td->iso_cnt].offset);
1224                 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1225         } else {
1226                 buf = (void *)urb->transfer_buffer + urb->actual_length;
1227                 urb_len = urb->transfer_buffer_length - urb->actual_length;
1228         }
1229         bufsize = min(urb_len, (int) td->maxpacket);
1230         if (rcv_len <= bufsize) {
1231                 size = rcv_len;
1232         } else {
1233                 size = bufsize;
1234                 status = -EOVERFLOW;
1235                 finish = 1;
1236         }
1237
1238         /* update parameters */
1239         urb->actual_length += size;
1240         if (rcv_len == 0)
1241                 td->zero_packet = 1;
1242         if (rcv_len < bufsize) {
1243                 td->short_packet = 1;
1244         }
1245         if (usb_pipeisoc(urb->pipe)) {
1246                 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1247                 urb->iso_frame_desc[td->iso_cnt].status = status;
1248                 td->iso_cnt++;
1249                 finish = 0;
1250         }
1251
1252         /* check transfer finish */
1253         if (finish || check_transfer_finish(td, urb)) {
1254                 pipe_stop(r8a66597, td->pipe);
1255                 pipe_irq_disable(r8a66597, pipenum);
1256                 finish = 1;
1257         }
1258
1259         /* read fifo */
1260         if (urb->transfer_buffer) {
1261                 if (size == 0)
1262                         r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1263                 else
1264                         r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1265                                            buf, size);
1266         }
1267
1268         if (finish && pipenum != 0)
1269                 finish_request(r8a66597, td, pipenum, urb, status);
1270 }
1271
1272 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1273 {
1274         u16 tmp;
1275         int bufsize, size;
1276         u16 *buf;
1277         struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1278         struct urb *urb;
1279
1280         if (unlikely(!td))
1281                 return;
1282         urb = td->urb;
1283
1284         fifo_change_from_pipe(r8a66597, td->pipe);
1285         tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1286         if (unlikely((tmp & FRDY) == 0)) {
1287                 pipe_stop(r8a66597, td->pipe);
1288                 pipe_irq_disable(r8a66597, pipenum);
1289                 err("out write fifo not ready. (%d)", pipenum);
1290                 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
1291                 return;
1292         }
1293
1294         /* prepare parameters */
1295         bufsize = td->maxpacket;
1296         if (usb_pipeisoc(urb->pipe)) {
1297                 buf = (u16 *)(urb->transfer_buffer +
1298                                 urb->iso_frame_desc[td->iso_cnt].offset);
1299                 size = min(bufsize,
1300                            (int)urb->iso_frame_desc[td->iso_cnt].length);
1301         } else {
1302                 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1303                 size = min((int)bufsize,
1304                            urb->transfer_buffer_length - urb->actual_length);
1305         }
1306
1307         /* write fifo */
1308         if (pipenum > 0)
1309                 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
1310         if (urb->transfer_buffer) {
1311                 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1312                 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1313                         r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1314         }
1315
1316         /* update parameters */
1317         urb->actual_length += size;
1318         if (usb_pipeisoc(urb->pipe)) {
1319                 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1320                 urb->iso_frame_desc[td->iso_cnt].status = 0;
1321                 td->iso_cnt++;
1322         }
1323
1324         /* check transfer finish */
1325         if (check_transfer_finish(td, urb)) {
1326                 disable_irq_ready(r8a66597, pipenum);
1327                 enable_irq_empty(r8a66597, pipenum);
1328                 if (!usb_pipeisoc(urb->pipe))
1329                         enable_irq_nrdy(r8a66597, pipenum);
1330         } else
1331                 pipe_irq_enable(r8a66597, urb, pipenum);
1332 }
1333
1334
1335 static void check_next_phase(struct r8a66597 *r8a66597, int status)
1336 {
1337         struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1338         struct urb *urb;
1339         u8 finish = 0;
1340
1341         if (unlikely(!td))
1342                 return;
1343         urb = td->urb;
1344
1345         switch (td->type) {
1346         case USB_PID_IN:
1347         case USB_PID_OUT:
1348                 if (check_transfer_finish(td, urb))
1349                         td->type = USB_PID_ACK;
1350                 break;
1351         case USB_PID_SETUP:
1352                 if (urb->transfer_buffer_length == urb->actual_length)
1353                         td->type = USB_PID_ACK;
1354                 else if (usb_pipeout(urb->pipe))
1355                         td->type = USB_PID_OUT;
1356                 else
1357                         td->type = USB_PID_IN;
1358                 break;
1359         case USB_PID_ACK:
1360                 finish = 1;
1361                 break;
1362         }
1363
1364         if (finish || status != 0 || urb->unlinked)
1365                 finish_request(r8a66597, td, 0, urb, status);
1366         else
1367                 start_transfer(r8a66597, td);
1368 }
1369
1370 static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
1371 {
1372         struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1373
1374         if (td) {
1375                 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1376
1377                 if (pid == PID_NAK)
1378                         return -ECONNRESET;
1379                 else
1380                         return -EPIPE;
1381         }
1382         return 0;
1383 }
1384
1385 static void irq_pipe_ready(struct r8a66597 *r8a66597)
1386 {
1387         u16 check;
1388         u16 pipenum;
1389         u16 mask;
1390         struct r8a66597_td *td;
1391
1392         mask = r8a66597_read(r8a66597, BRDYSTS)
1393                & r8a66597_read(r8a66597, BRDYENB);
1394         r8a66597_write(r8a66597, ~mask, BRDYSTS);
1395         if (mask & BRDY0) {
1396                 td = r8a66597_get_td(r8a66597, 0);
1397                 if (td && td->type == USB_PID_IN)
1398                         packet_read(r8a66597, 0);
1399                 else
1400                         pipe_irq_disable(r8a66597, 0);
1401                 check_next_phase(r8a66597, 0);
1402         }
1403
1404         for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1405                 check = 1 << pipenum;
1406                 if (mask & check) {
1407                         td = r8a66597_get_td(r8a66597, pipenum);
1408                         if (unlikely(!td))
1409                                 continue;
1410
1411                         if (td->type == USB_PID_IN)
1412                                 packet_read(r8a66597, pipenum);
1413                         else if (td->type == USB_PID_OUT)
1414                                 packet_write(r8a66597, pipenum);
1415                 }
1416         }
1417 }
1418
1419 static void irq_pipe_empty(struct r8a66597 *r8a66597)
1420 {
1421         u16 tmp;
1422         u16 check;
1423         u16 pipenum;
1424         u16 mask;
1425         struct r8a66597_td *td;
1426
1427         mask = r8a66597_read(r8a66597, BEMPSTS)
1428                & r8a66597_read(r8a66597, BEMPENB);
1429         r8a66597_write(r8a66597, ~mask, BEMPSTS);
1430         if (mask & BEMP0) {
1431                 cfifo_change(r8a66597, 0);
1432                 td = r8a66597_get_td(r8a66597, 0);
1433                 if (td && td->type != USB_PID_OUT)
1434                         disable_irq_empty(r8a66597, 0);
1435                 check_next_phase(r8a66597, 0);
1436         }
1437
1438         for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1439                 check = 1 << pipenum;
1440                 if (mask &  check) {
1441                         struct r8a66597_td *td;
1442                         td = r8a66597_get_td(r8a66597, pipenum);
1443                         if (unlikely(!td))
1444                                 continue;
1445
1446                         tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1447                         if ((tmp & INBUFM) == 0) {
1448                                 disable_irq_empty(r8a66597, pipenum);
1449                                 pipe_irq_disable(r8a66597, pipenum);
1450                                 finish_request(r8a66597, td, pipenum, td->urb,
1451                                                 0);
1452                         }
1453                 }
1454         }
1455 }
1456
1457 static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1458 {
1459         u16 check;
1460         u16 pipenum;
1461         u16 mask;
1462         int status;
1463
1464         mask = r8a66597_read(r8a66597, NRDYSTS)
1465                & r8a66597_read(r8a66597, NRDYENB);
1466         r8a66597_write(r8a66597, ~mask, NRDYSTS);
1467         if (mask & NRDY0) {
1468                 cfifo_change(r8a66597, 0);
1469                 status = get_urb_error(r8a66597, 0);
1470                 pipe_irq_disable(r8a66597, 0);
1471                 check_next_phase(r8a66597, status);
1472         }
1473
1474         for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1475                 check = 1 << pipenum;
1476                 if (mask & check) {
1477                         struct r8a66597_td *td;
1478                         td = r8a66597_get_td(r8a66597, pipenum);
1479                         if (unlikely(!td))
1480                                 continue;
1481
1482                         status = get_urb_error(r8a66597, pipenum);
1483                         pipe_irq_disable(r8a66597, pipenum);
1484                         pipe_stop(r8a66597, td->pipe);
1485                         finish_request(r8a66597, td, pipenum, td->urb, status);
1486                 }
1487         }
1488 }
1489
1490 static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
1491 {
1492         mod_timer(&r8a66597->rh_timer,
1493                         jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
1494 }
1495
1496 static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port)
1497 {
1498         struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1499
1500         rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1501         rh->scount = R8A66597_MAX_SAMPLING;
1502         r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_CONNECTION)
1503                                          | (1 << USB_PORT_FEAT_C_CONNECTION);
1504         r8a66597_root_hub_start_polling(r8a66597);
1505 }
1506
1507 static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1508 {
1509         struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1510         u16 intsts0, intsts1, intsts2;
1511         u16 intenb0, intenb1, intenb2;
1512         u16 mask0, mask1, mask2;
1513         int status;
1514
1515         spin_lock(&r8a66597->lock);
1516
1517         intsts0 = r8a66597_read(r8a66597, INTSTS0);
1518         intsts1 = r8a66597_read(r8a66597, INTSTS1);
1519         intsts2 = r8a66597_read(r8a66597, INTSTS2);
1520         intenb0 = r8a66597_read(r8a66597, INTENB0);
1521         intenb1 = r8a66597_read(r8a66597, INTENB1);
1522         intenb2 = r8a66597_read(r8a66597, INTENB2);
1523
1524         mask2 = intsts2 & intenb2;
1525         mask1 = intsts1 & intenb1;
1526         mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1527         if (mask2) {
1528                 if (mask2 & ATTCH) {
1529                         r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
1530                         r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1531
1532                         /* start usb bus sampling */
1533                         start_root_hub_sampling(r8a66597, 1);
1534                 }
1535                 if (mask2 & DTCH) {
1536                         r8a66597_write(r8a66597, ~DTCH, INTSTS2);
1537                         r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1538                         r8a66597_usb_disconnect(r8a66597, 1);
1539                 }
1540         }
1541
1542         if (mask1) {
1543                 if (mask1 & ATTCH) {
1544                         r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
1545                         r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1546
1547                         /* start usb bus sampling */
1548                         start_root_hub_sampling(r8a66597, 0);
1549                 }
1550                 if (mask1 & DTCH) {
1551                         r8a66597_write(r8a66597, ~DTCH, INTSTS1);
1552                         r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1553                         r8a66597_usb_disconnect(r8a66597, 0);
1554                 }
1555                 if (mask1 & SIGN) {
1556                         r8a66597_write(r8a66597, ~SIGN, INTSTS1);
1557                         status = get_urb_error(r8a66597, 0);
1558                         check_next_phase(r8a66597, status);
1559                 }
1560                 if (mask1 & SACK) {
1561                         r8a66597_write(r8a66597, ~SACK, INTSTS1);
1562                         check_next_phase(r8a66597, 0);
1563                 }
1564         }
1565         if (mask0) {
1566                 if (mask0 & BRDY)
1567                         irq_pipe_ready(r8a66597);
1568                 if (mask0 & BEMP)
1569                         irq_pipe_empty(r8a66597);
1570                 if (mask0 & NRDY)
1571                         irq_pipe_nrdy(r8a66597);
1572         }
1573
1574         spin_unlock(&r8a66597->lock);
1575         return IRQ_HANDLED;
1576 }
1577
1578 /* this function must be called with interrupt disabled */
1579 static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1580 {
1581         u16 tmp;
1582         struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1583
1584         if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1585                 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1586
1587                 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1588                 if ((tmp & USBRST) == USBRST) {
1589                         r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1590                                       dvstctr_reg);
1591                         r8a66597_root_hub_start_polling(r8a66597);
1592                 } else
1593                         r8a66597_usb_connect(r8a66597, port);
1594         }
1595
1596         if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1597                 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1598                 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1599         }
1600
1601         if (rh->scount > 0) {
1602                 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1603                 if (tmp == rh->old_syssts) {
1604                         rh->scount--;
1605                         if (rh->scount == 0)
1606                                 r8a66597_check_syssts(r8a66597, port, tmp);
1607                         else
1608                                 r8a66597_root_hub_start_polling(r8a66597);
1609                 } else {
1610                         rh->scount = R8A66597_MAX_SAMPLING;
1611                         rh->old_syssts = tmp;
1612                         r8a66597_root_hub_start_polling(r8a66597);
1613                 }
1614         }
1615 }
1616
1617 static void r8a66597_interval_timer(unsigned long _r8a66597)
1618 {
1619         struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1620         unsigned long flags;
1621         u16 pipenum;
1622         struct r8a66597_td *td;
1623
1624         spin_lock_irqsave(&r8a66597->lock, flags);
1625
1626         for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1627                 if (!(r8a66597->interval_map & (1 << pipenum)))
1628                         continue;
1629                 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1630                         continue;
1631
1632                 td = r8a66597_get_td(r8a66597, pipenum);
1633                 if (td)
1634                         start_transfer(r8a66597, td);
1635         }
1636
1637         spin_unlock_irqrestore(&r8a66597->lock, flags);
1638 }
1639
1640 static void r8a66597_td_timer(unsigned long _r8a66597)
1641 {
1642         struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1643         unsigned long flags;
1644         u16 pipenum;
1645         struct r8a66597_td *td, *new_td = NULL;
1646         struct r8a66597_pipe *pipe;
1647
1648         spin_lock_irqsave(&r8a66597->lock, flags);
1649         for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1650                 if (!(r8a66597->timeout_map & (1 << pipenum)))
1651                         continue;
1652                 if (timer_pending(&r8a66597->td_timer[pipenum]))
1653                         continue;
1654
1655                 td = r8a66597_get_td(r8a66597, pipenum);
1656                 if (!td) {
1657                         r8a66597->timeout_map &= ~(1 << pipenum);
1658                         continue;
1659                 }
1660
1661                 if (td->urb->actual_length) {
1662                         set_td_timer(r8a66597, td);
1663                         break;
1664                 }
1665
1666                 pipe = td->pipe;
1667                 pipe_stop(r8a66597, pipe);
1668
1669                 new_td = td;
1670                 do {
1671                         list_move_tail(&new_td->queue,
1672                                        &r8a66597->pipe_queue[pipenum]);
1673                         new_td = r8a66597_get_td(r8a66597, pipenum);
1674                         if (!new_td) {
1675                                 new_td = td;
1676                                 break;
1677                         }
1678                 } while (td != new_td && td->address == new_td->address);
1679
1680                 start_transfer(r8a66597, new_td);
1681
1682                 if (td == new_td)
1683                         r8a66597->timeout_map &= ~(1 << pipenum);
1684                 else
1685                         set_td_timer(r8a66597, new_td);
1686                 break;
1687         }
1688         spin_unlock_irqrestore(&r8a66597->lock, flags);
1689 }
1690
1691 static void r8a66597_timer(unsigned long _r8a66597)
1692 {
1693         struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1694         unsigned long flags;
1695
1696         spin_lock_irqsave(&r8a66597->lock, flags);
1697
1698         r8a66597_root_hub_control(r8a66597, 0);
1699         r8a66597_root_hub_control(r8a66597, 1);
1700
1701         spin_unlock_irqrestore(&r8a66597->lock, flags);
1702 }
1703
1704 static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1705 {
1706         struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1707
1708         if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1709             (urb->dev->state == USB_STATE_CONFIGURED))
1710                 return 1;
1711         else
1712                 return 0;
1713 }
1714
1715 static int r8a66597_start(struct usb_hcd *hcd)
1716 {
1717         struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1718
1719         hcd->state = HC_STATE_RUNNING;
1720         return enable_controller(r8a66597);
1721 }
1722
1723 static void r8a66597_stop(struct usb_hcd *hcd)
1724 {
1725         struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1726
1727         disable_controller(r8a66597);
1728 }
1729
1730 static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1731 {
1732         unsigned int usb_address = usb_pipedevice(urb->pipe);
1733         u16 root_port, hub_port;
1734
1735         if (usb_address == 0) {
1736                 get_port_number(urb->dev->devpath,
1737                                 &root_port, &hub_port);
1738                 set_devadd_reg(r8a66597, 0,
1739                                get_r8a66597_usb_speed(urb->dev->speed),
1740                                get_parent_r8a66597_address(r8a66597, urb->dev),
1741                                hub_port, root_port);
1742         }
1743 }
1744
1745 static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1746                                             struct urb *urb,
1747                                             struct usb_host_endpoint *hep)
1748 {
1749         struct r8a66597_td *td;
1750         u16 pipenum;
1751
1752         td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
1753         if (td == NULL)
1754                 return NULL;
1755
1756         pipenum = r8a66597_get_pipenum(urb, hep);
1757         td->pipenum = pipenum;
1758         td->pipe = hep->hcpriv;
1759         td->urb = urb;
1760         td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1761         td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1762                                       !usb_pipein(urb->pipe));
1763         if (usb_pipecontrol(urb->pipe))
1764                 td->type = USB_PID_SETUP;
1765         else if (usb_pipein(urb->pipe))
1766                 td->type = USB_PID_IN;
1767         else
1768                 td->type = USB_PID_OUT;
1769         INIT_LIST_HEAD(&td->queue);
1770
1771         return td;
1772 }
1773
1774 static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
1775                                 struct urb *urb,
1776                                 gfp_t mem_flags)
1777 {
1778         struct usb_host_endpoint *hep = urb->ep;
1779         struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1780         struct r8a66597_td *td = NULL;
1781         int ret, request = 0;
1782         unsigned long flags;
1783
1784         spin_lock_irqsave(&r8a66597->lock, flags);
1785         if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1786                 ret = -ENODEV;
1787                 goto error_not_linked;
1788         }
1789
1790         ret = usb_hcd_link_urb_to_ep(hcd, urb);
1791         if (ret)
1792                 goto error_not_linked;
1793
1794         if (!hep->hcpriv) {
1795                 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1796                                 GFP_ATOMIC);
1797                 if (!hep->hcpriv) {
1798                         ret = -ENOMEM;
1799                         goto error;
1800                 }
1801                 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1802                 if (usb_pipeendpoint(urb->pipe))
1803                         init_pipe_info(r8a66597, urb, hep, &hep->desc);
1804         }
1805
1806         if (unlikely(check_pipe_config(r8a66597, urb)))
1807                 init_pipe_config(r8a66597, urb);
1808
1809         set_address_zero(r8a66597, urb);
1810         td = r8a66597_make_td(r8a66597, urb, hep);
1811         if (td == NULL) {
1812                 ret = -ENOMEM;
1813                 goto error;
1814         }
1815         if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1816                 request = 1;
1817         list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
1818         urb->hcpriv = td;
1819
1820         if (request) {
1821                 if (td->pipe->info.timer_interval) {
1822                         r8a66597->interval_map |= 1 << td->pipenum;
1823                         mod_timer(&r8a66597->interval_timer[td->pipenum],
1824                                   jiffies + msecs_to_jiffies(
1825                                         td->pipe->info.timer_interval));
1826                 } else {
1827                         ret = start_transfer(r8a66597, td);
1828                         if (ret < 0) {
1829                                 list_del(&td->queue);
1830                                 kfree(td);
1831                         }
1832                 }
1833         } else
1834                 set_td_timer(r8a66597, td);
1835
1836 error:
1837         if (ret)
1838                 usb_hcd_unlink_urb_from_ep(hcd, urb);
1839 error_not_linked:
1840         spin_unlock_irqrestore(&r8a66597->lock, flags);
1841         return ret;
1842 }
1843
1844 static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1845                 int status)
1846 {
1847         struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1848         struct r8a66597_td *td;
1849         unsigned long flags;
1850         int rc;
1851
1852         spin_lock_irqsave(&r8a66597->lock, flags);
1853         rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1854         if (rc)
1855                 goto done;
1856
1857         if (urb->hcpriv) {
1858                 td = urb->hcpriv;
1859                 pipe_stop(r8a66597, td->pipe);
1860                 pipe_irq_disable(r8a66597, td->pipenum);
1861                 disable_irq_empty(r8a66597, td->pipenum);
1862                 finish_request(r8a66597, td, td->pipenum, urb, status);
1863         }
1864  done:
1865         spin_unlock_irqrestore(&r8a66597->lock, flags);
1866         return rc;
1867 }
1868
1869 static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1870                                       struct usb_host_endpoint *hep)
1871 {
1872         struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1873         struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1874         struct r8a66597_td *td;
1875         struct urb *urb = NULL;
1876         u16 pipenum;
1877         unsigned long flags;
1878
1879         if (pipe == NULL)
1880                 return;
1881         pipenum = pipe->info.pipenum;
1882
1883         if (pipenum == 0) {
1884                 kfree(hep->hcpriv);
1885                 hep->hcpriv = NULL;
1886                 return;
1887         }
1888
1889         spin_lock_irqsave(&r8a66597->lock, flags);
1890         pipe_stop(r8a66597, pipe);
1891         pipe_irq_disable(r8a66597, pipenum);
1892         disable_irq_empty(r8a66597, pipenum);
1893         td = r8a66597_get_td(r8a66597, pipenum);
1894         if (td)
1895                 urb = td->urb;
1896         finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
1897         kfree(hep->hcpriv);
1898         hep->hcpriv = NULL;
1899         spin_unlock_irqrestore(&r8a66597->lock, flags);
1900 }
1901
1902 static int r8a66597_get_frame(struct usb_hcd *hcd)
1903 {
1904         struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1905         return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1906 }
1907
1908 static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
1909 {
1910         int chix;
1911
1912         if (udev->state == USB_STATE_CONFIGURED &&
1913             udev->parent && udev->parent->devnum > 1 &&
1914             udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
1915                 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
1916
1917         for (chix = 0; chix < udev->maxchild; chix++) {
1918                 struct usb_device *childdev = udev->children[chix];
1919
1920                 if (childdev)
1921                         collect_usb_address_map(childdev, map);
1922         }
1923 }
1924
1925 /* this function must be called with interrupt disabled */
1926 static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
1927                                                    int addr)
1928 {
1929         struct r8a66597_device *dev;
1930         struct list_head *list = &r8a66597->child_device;
1931
1932         list_for_each_entry(dev, list, device_list) {
1933                 if (!dev)
1934                         continue;
1935                 if (dev->usb_address != addr)
1936                         continue;
1937
1938                 return dev;
1939         }
1940
1941         err("get_r8a66597_device fail.(%d)\n", addr);
1942         return NULL;
1943 }
1944
1945 static void update_usb_address_map(struct r8a66597 *r8a66597,
1946                                    struct usb_device *root_hub,
1947                                    unsigned long *map)
1948 {
1949         int i, j, addr;
1950         unsigned long diff;
1951         unsigned long flags;
1952
1953         for (i = 0; i < 4; i++) {
1954                 diff = r8a66597->child_connect_map[i] ^ map[i];
1955                 if (!diff)
1956                         continue;
1957
1958                 for (j = 0; j < 32; j++) {
1959                         if (!(diff & (1 << j)))
1960                                 continue;
1961
1962                         addr = i * 32 + j;
1963                         if (map[i] & (1 << j))
1964                                 set_child_connect_map(r8a66597, addr);
1965                         else {
1966                                 struct r8a66597_device *dev;
1967
1968                                 spin_lock_irqsave(&r8a66597->lock, flags);
1969                                 dev = get_r8a66597_device(r8a66597, addr);
1970                                 disable_r8a66597_pipe_all(r8a66597, dev);
1971                                 free_usb_address(r8a66597, dev);
1972                                 put_child_connect_map(r8a66597, addr);
1973                                 spin_unlock_irqrestore(&r8a66597->lock, flags);
1974                         }
1975                 }
1976         }
1977 }
1978
1979 static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
1980                                         struct usb_hcd *hcd)
1981 {
1982         struct usb_bus *bus;
1983         unsigned long now_map[4];
1984
1985         memset(now_map, 0, sizeof(now_map));
1986
1987         list_for_each_entry(bus, &usb_bus_list, bus_list) {
1988                 if (!bus->root_hub)
1989                         continue;
1990
1991                 if (bus->busnum != hcd->self.busnum)
1992                         continue;
1993
1994                 collect_usb_address_map(bus->root_hub, now_map);
1995                 update_usb_address_map(r8a66597, bus->root_hub, now_map);
1996         }
1997 }
1998
1999 static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2000 {
2001         struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2002         unsigned long flags;
2003         int i;
2004
2005         r8a66597_check_detect_child(r8a66597, hcd);
2006
2007         spin_lock_irqsave(&r8a66597->lock, flags);
2008
2009         *buf = 0;       /* initialize (no change) */
2010
2011         for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
2012                 if (r8a66597->root_hub[i].port & 0xffff0000)
2013                         *buf |= 1 << (i + 1);
2014         }
2015
2016         spin_unlock_irqrestore(&r8a66597->lock, flags);
2017
2018         return (*buf != 0);
2019 }
2020
2021 static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2022                                     struct usb_hub_descriptor *desc)
2023 {
2024         desc->bDescriptorType = 0x29;
2025         desc->bHubContrCurrent = 0;
2026         desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
2027         desc->bDescLength = 9;
2028         desc->bPwrOn2PwrGood = 0;
2029         desc->wHubCharacteristics = cpu_to_le16(0x0011);
2030         desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
2031         desc->bitmap[1] = ~0;
2032 }
2033
2034 static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2035                                 u16 wIndex, char *buf, u16 wLength)
2036 {
2037         struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2038         int ret;
2039         int port = (wIndex & 0x00FF) - 1;
2040         struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2041         unsigned long flags;
2042
2043         ret = 0;
2044
2045         spin_lock_irqsave(&r8a66597->lock, flags);
2046         switch (typeReq) {
2047         case ClearHubFeature:
2048         case SetHubFeature:
2049                 switch (wValue) {
2050                 case C_HUB_OVER_CURRENT:
2051                 case C_HUB_LOCAL_POWER:
2052                         break;
2053                 default:
2054                         goto error;
2055                 }
2056                 break;
2057         case ClearPortFeature:
2058                 if (wIndex > R8A66597_MAX_ROOT_HUB)
2059                         goto error;
2060                 if (wLength != 0)
2061                         goto error;
2062
2063                 switch (wValue) {
2064                 case USB_PORT_FEAT_ENABLE:
2065                         rh->port &= (1 << USB_PORT_FEAT_POWER);
2066                         break;
2067                 case USB_PORT_FEAT_SUSPEND:
2068                         break;
2069                 case USB_PORT_FEAT_POWER:
2070                         r8a66597_port_power(r8a66597, port, 0);
2071                         break;
2072                 case USB_PORT_FEAT_C_ENABLE:
2073                 case USB_PORT_FEAT_C_SUSPEND:
2074                 case USB_PORT_FEAT_C_CONNECTION:
2075                 case USB_PORT_FEAT_C_OVER_CURRENT:
2076                 case USB_PORT_FEAT_C_RESET:
2077                         break;
2078                 default:
2079                         goto error;
2080                 }
2081                 rh->port &= ~(1 << wValue);
2082                 break;
2083         case GetHubDescriptor:
2084                 r8a66597_hub_descriptor(r8a66597,
2085                                         (struct usb_hub_descriptor *)buf);
2086                 break;
2087         case GetHubStatus:
2088                 *buf = 0x00;
2089                 break;
2090         case GetPortStatus:
2091                 if (wIndex > R8A66597_MAX_ROOT_HUB)
2092                         goto error;
2093                 *(u32 *)buf = cpu_to_le32(rh->port);
2094                 break;
2095         case SetPortFeature:
2096                 if (wIndex > R8A66597_MAX_ROOT_HUB)
2097                         goto error;
2098                 if (wLength != 0)
2099                         goto error;
2100
2101                 switch (wValue) {
2102                 case USB_PORT_FEAT_SUSPEND:
2103                         break;
2104                 case USB_PORT_FEAT_POWER:
2105                         r8a66597_port_power(r8a66597, port, 1);
2106                         rh->port |= (1 << USB_PORT_FEAT_POWER);
2107                         break;
2108                 case USB_PORT_FEAT_RESET: {
2109                         struct r8a66597_device *dev = rh->dev;
2110
2111                         rh->port |= (1 << USB_PORT_FEAT_RESET);
2112
2113                         disable_r8a66597_pipe_all(r8a66597, dev);
2114                         free_usb_address(r8a66597, dev);
2115
2116                         r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2117                                       get_dvstctr_reg(port));
2118                         mod_timer(&r8a66597->rh_timer,
2119                                   jiffies + msecs_to_jiffies(50));
2120                         }
2121                         break;
2122                 default:
2123                         goto error;
2124                 }
2125                 rh->port |= 1 << wValue;
2126                 break;
2127         default:
2128 error:
2129                 ret = -EPIPE;
2130                 break;
2131         }
2132
2133         spin_unlock_irqrestore(&r8a66597->lock, flags);
2134         return ret;
2135 }
2136
2137 static struct hc_driver r8a66597_hc_driver = {
2138         .description =          hcd_name,
2139         .hcd_priv_size =        sizeof(struct r8a66597),
2140         .irq =                  r8a66597_irq,
2141
2142         /*
2143          * generic hardware linkage
2144          */
2145         .flags =                HCD_USB2,
2146
2147         .start =                r8a66597_start,
2148         .stop =                 r8a66597_stop,
2149
2150         /*
2151          * managing i/o requests and associated device resources
2152          */
2153         .urb_enqueue =          r8a66597_urb_enqueue,
2154         .urb_dequeue =          r8a66597_urb_dequeue,
2155         .endpoint_disable =     r8a66597_endpoint_disable,
2156
2157         /*
2158          * periodic schedule support
2159          */
2160         .get_frame_number =     r8a66597_get_frame,
2161
2162         /*
2163          * root hub support
2164          */
2165         .hub_status_data =      r8a66597_hub_status_data,
2166         .hub_control =          r8a66597_hub_control,
2167 };
2168
2169 #if defined(CONFIG_PM)
2170 static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
2171 {
2172         return 0;
2173 }
2174
2175 static int r8a66597_resume(struct platform_device *pdev)
2176 {
2177         return 0;
2178 }
2179 #else   /* if defined(CONFIG_PM) */
2180 #define r8a66597_suspend        NULL
2181 #define r8a66597_resume         NULL
2182 #endif
2183
2184 static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2185 {
2186         struct r8a66597         *r8a66597 = dev_get_drvdata(&pdev->dev);
2187         struct usb_hcd          *hcd = r8a66597_to_hcd(r8a66597);
2188
2189         del_timer_sync(&r8a66597->rh_timer);
2190         usb_remove_hcd(hcd);
2191         iounmap((void *)r8a66597->reg);
2192         usb_put_hcd(hcd);
2193         return 0;
2194 }
2195
2196 #define resource_len(r) (((r)->end - (r)->start) + 1)
2197 static int __init r8a66597_probe(struct platform_device *pdev)
2198 {
2199         struct resource *res = NULL;
2200         int irq = -1;
2201         void __iomem *reg = NULL;
2202         struct usb_hcd *hcd = NULL;
2203         struct r8a66597 *r8a66597;
2204         int ret = 0;
2205         int i;
2206
2207         if (pdev->dev.dma_mask) {
2208                 ret = -EINVAL;
2209                 err("dma not support");
2210                 goto clean_up;
2211         }
2212
2213         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2214                                            (char *)hcd_name);
2215         if (!res) {
2216                 ret = -ENODEV;
2217                 err("platform_get_resource_byname error.");
2218                 goto clean_up;
2219         }
2220
2221         irq = platform_get_irq(pdev, 0);
2222         if (irq < 0) {
2223                 ret = -ENODEV;
2224                 err("platform_get_irq error.");
2225                 goto clean_up;
2226         }
2227
2228         reg = ioremap(res->start, resource_len(res));
2229         if (reg == NULL) {
2230                 ret = -ENOMEM;
2231                 err("ioremap error.");
2232                 goto clean_up;
2233         }
2234
2235         /* initialize hcd */
2236         hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2237         if (!hcd) {
2238                 ret = -ENOMEM;
2239                 err("Failed to create hcd");
2240                 goto clean_up;
2241         }
2242         r8a66597 = hcd_to_r8a66597(hcd);
2243         memset(r8a66597, 0, sizeof(struct r8a66597));
2244         dev_set_drvdata(&pdev->dev, r8a66597);
2245
2246         spin_lock_init(&r8a66597->lock);
2247         init_timer(&r8a66597->rh_timer);
2248         r8a66597->rh_timer.function = r8a66597_timer;
2249         r8a66597->rh_timer.data = (unsigned long)r8a66597;
2250         r8a66597->reg = (unsigned long)reg;
2251
2252         for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2253                 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2254                 init_timer(&r8a66597->td_timer[i]);
2255                 r8a66597->td_timer[i].function = r8a66597_td_timer;
2256                 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
2257                 setup_timer(&r8a66597->interval_timer[i],
2258                                 r8a66597_interval_timer,
2259                                 (unsigned long)r8a66597);
2260         }
2261         INIT_LIST_HEAD(&r8a66597->child_device);
2262
2263         hcd->rsrc_start = res->start;
2264         ret = usb_add_hcd(hcd, irq, IRQF_DISABLED);
2265         if (ret != 0) {
2266                 err("Failed to add hcd");
2267                 goto clean_up;
2268         }
2269
2270         return 0;
2271
2272 clean_up:
2273         if (reg)
2274                 iounmap(reg);
2275
2276         return ret;
2277 }
2278
2279 static struct platform_driver r8a66597_driver = {
2280         .probe =        r8a66597_probe,
2281         .remove =       r8a66597_remove,
2282         .suspend =      r8a66597_suspend,
2283         .resume =       r8a66597_resume,
2284         .driver         = {
2285                 .name = (char *) hcd_name,
2286                 .owner  = THIS_MODULE,
2287         },
2288 };
2289
2290 static int __init r8a66597_init(void)
2291 {
2292         if (usb_disabled())
2293                 return -ENODEV;
2294
2295         info("driver %s, %s", hcd_name, DRIVER_VERSION);
2296         return platform_driver_register(&r8a66597_driver);
2297 }
2298 module_init(r8a66597_init);
2299
2300 static void __exit r8a66597_cleanup(void)
2301 {
2302         platform_driver_unregister(&r8a66597_driver);
2303 }
2304 module_exit(r8a66597_cleanup);
2305