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