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