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