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