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