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