usb: r8a66597-hcd: fix iinterval for Full/Low speed device
[linux-2.6/mini2440.git] / drivers / usb / host / r8a66597-hcd.c
blobd5f02dddb1203ec31c151401aab80208388d1235
1 /*
2 * R8A66597 HCD (Host Controller Driver)
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
9 * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
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.
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.
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
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>
41 #include "../core/hcd.h"
42 #include "r8a66597.h"
44 MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
45 MODULE_LICENSE("GPL");
46 MODULE_AUTHOR("Yoshihiro Shimoda");
47 MODULE_ALIAS("platform:r8a66597_hcd");
49 #define DRIVER_VERSION "10 Apr 2008"
51 static const char hcd_name[] = "r8a66597_hcd";
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
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)");
65 static unsigned short endian;
66 module_param(endian, ushort, 0644);
67 MODULE_PARM_DESC(endian, "data endian: big=256, little=0 (default=0)");
69 static unsigned short irq_sense = INTL;
70 module_param(irq_sense, ushort, 0644);
71 MODULE_PARM_DESC(irq_sense, "IRQ sense: low level=32, falling edge=0 "
72 "(default=32)");
74 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
75 static int r8a66597_get_frame(struct usb_hcd *hcd);
77 /* this function must be called with interrupt disabled */
78 static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
79 unsigned long reg)
81 u16 tmp;
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);
89 /* this function must be called with interrupt disabled */
90 static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
91 unsigned long reg)
93 u16 tmp;
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);
101 static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
102 u16 usbspd, u8 upphub, u8 hubport, int port)
104 u16 val;
105 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
107 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
108 r8a66597_write(r8a66597, val, devadd_reg);
111 static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
113 u16 tmp;
114 int i = 0;
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 err("register access fail.");
122 return -ENXIO;
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 err("register access fail.");
132 return -ENXIO;
134 } while ((tmp & USBE) != USBE);
135 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
136 r8a66597_mdfy(r8a66597, clock, XTAL, SYSCFG0);
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 err("register access fail.");
145 return -ENXIO;
147 } while ((tmp & SCKE) != SCKE);
148 #endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
150 return 0;
153 static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
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
164 static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
166 u16 val;
168 val = port ? DRPD : DCFM | DRPD;
169 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
170 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
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));
177 static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
179 u16 val, tmp;
181 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
182 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
184 r8a66597_port_power(r8a66597, port, 0);
186 do {
187 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
188 udelay(640);
189 } while (tmp == EDGESTS);
191 val = port ? DRPD : DCFM | DRPD;
192 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
193 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
196 static int enable_controller(struct r8a66597 *r8a66597)
198 int ret, port;
200 ret = r8a66597_clock_enable(r8a66597);
201 if (ret < 0)
202 return ret;
204 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
205 r8a66597_bset(r8a66597, USBE, SYSCFG0);
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);
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);
217 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
219 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
220 r8a66597_enable_port(r8a66597, port);
222 return 0;
225 static void disable_controller(struct r8a66597 *r8a66597)
227 int port;
229 r8a66597_write(r8a66597, 0, INTENB0);
230 r8a66597_write(r8a66597, 0, INTSTS0);
232 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
233 r8a66597_disable_port(r8a66597, port);
235 r8a66597_clock_disable(r8a66597);
238 static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
239 struct usb_device *udev)
241 struct r8a66597_device *dev;
243 if (udev->parent && udev->parent->devnum != 1)
244 udev = udev->parent;
246 dev = dev_get_drvdata(&udev->dev);
247 if (dev)
248 return dev->address;
249 else
250 return 0;
253 static int is_child_device(char *devpath)
255 return (devpath[2] ? 1 : 0);
258 static int is_hub_limit(char *devpath)
260 return ((strlen(devpath) >= 4) ? 1 : 0);
263 static void get_port_number(char *devpath, u16 *root_port, u16 *hub_port)
265 if (root_port) {
266 *root_port = (devpath[0] & 0x0F) - 1;
267 if (*root_port >= R8A66597_MAX_ROOT_HUB)
268 err("illegal root port number");
270 if (hub_port)
271 *hub_port = devpath[2] & 0x0F;
274 static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
276 u16 usbspd = 0;
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 err("unknown speed");
290 break;
293 return usbspd;
296 static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
298 int idx;
300 idx = address / 32;
301 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
304 static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
306 int idx;
308 idx = address / 32;
309 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
312 static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
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};
319 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
320 dma_ch = R8A66597_PIPE_NO_DMA;
322 pipe->fifoaddr = fifoaddr[dma_ch];
323 pipe->fifosel = fifosel[dma_ch];
324 pipe->fifoctr = fifoctr[dma_ch];
326 if (pipenum == 0)
327 pipe->pipectr = DCPCTR;
328 else
329 pipe->pipectr = get_pipectr_addr(pipenum);
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;
340 static struct r8a66597_device *
341 get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
343 if (usb_pipedevice(urb->pipe) == 0)
344 return &r8a66597->device0;
346 return dev_get_drvdata(&urb->dev->dev);
349 static int make_r8a66597_device(struct r8a66597 *r8a66597,
350 struct urb *urb, u8 addr)
352 struct r8a66597_device *dev;
353 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
355 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
356 if (dev == NULL)
357 return -ENOMEM;
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);
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;
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);
378 return 0;
381 /* this function must be called with interrupt disabled */
382 static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
384 u8 addr; /* R8A66597's address */
385 struct r8a66597_device *dev;
387 if (is_hub_limit(urb->dev->devpath)) {
388 err("Externel hub limit reached.");
389 return 0;
392 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
393 if (dev && dev->state >= USB_STATE_ADDRESS)
394 return dev->address;
396 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
397 if (r8a66597->address_map & (1 << addr))
398 continue;
400 dbg("alloc_address: r8a66597_addr=%d", addr);
401 r8a66597->address_map |= 1 << addr;
403 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
404 return 0;
406 return addr;
409 err("cannot communicate with a USB device more than 10.(%x)",
410 r8a66597->address_map);
412 return 0;
415 /* this function must be called with interrupt disabled */
416 static void free_usb_address(struct r8a66597 *r8a66597,
417 struct r8a66597_device *dev)
419 int port;
421 if (!dev)
422 return;
424 dbg("free_addr: addr=%d", dev->address);
426 dev->state = USB_STATE_DEFAULT;
427 r8a66597->address_map &= ~(1 << dev->address);
428 dev->address = 0;
429 dev_set_drvdata(&dev->udev->dev, NULL);
430 list_del(&dev->device_list);
431 kfree(dev);
433 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
434 if (r8a66597->root_hub[port].dev == dev) {
435 r8a66597->root_hub[port].dev = NULL;
436 break;
441 static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
442 u16 mask, u16 loop)
444 u16 tmp;
445 int i = 0;
447 do {
448 tmp = r8a66597_read(r8a66597, reg);
449 if (i++ > 1000000) {
450 err("register%lx, loop %x is timeout", reg, loop);
451 break;
453 ndelay(1);
454 } while ((tmp & mask) != loop);
457 /* this function must be called with interrupt disabled */
458 static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
460 u16 tmp;
462 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
463 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
464 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
465 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
468 /* this function must be called with interrupt disabled */
469 static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
471 u16 tmp;
473 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
474 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
475 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
476 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
477 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
480 /* this function must be called with interrupt disabled */
481 static void clear_all_buffer(struct r8a66597 *r8a66597,
482 struct r8a66597_pipe *pipe)
484 u16 tmp;
486 if (!pipe || pipe->info.pipenum == 0)
487 return;
489 pipe_stop(r8a66597, pipe);
490 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
491 tmp = r8a66597_read(r8a66597, pipe->pipectr);
492 tmp = r8a66597_read(r8a66597, pipe->pipectr);
493 tmp = r8a66597_read(r8a66597, pipe->pipectr);
494 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
497 /* this function must be called with interrupt disabled */
498 static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
499 struct r8a66597_pipe *pipe, int toggle)
501 if (toggle)
502 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
503 else
504 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
507 /* this function must be called with interrupt disabled */
508 static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
510 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
511 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
514 /* this function must be called with interrupt disabled */
515 static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
516 struct r8a66597_pipe *pipe)
518 cfifo_change(r8a66597, 0);
519 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D0FIFOSEL);
520 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D1FIFOSEL);
522 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum, MBW | CURPIPE,
523 pipe->fifosel);
524 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
527 static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
529 struct r8a66597_pipe *pipe = hep->hcpriv;
531 if (usb_pipeendpoint(urb->pipe) == 0)
532 return 0;
533 else
534 return pipe->info.pipenum;
537 static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
539 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
541 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
544 static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
545 int urb_pipe)
547 if (!dev)
548 return NULL;
550 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
553 /* this function must be called with interrupt disabled */
554 static void pipe_toggle_set(struct r8a66597 *r8a66597,
555 struct r8a66597_pipe *pipe,
556 struct urb *urb, int set)
558 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
559 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
560 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
562 if (!toggle)
563 return;
565 if (set)
566 *toggle |= 1 << endpoint;
567 else
568 *toggle &= ~(1 << endpoint);
571 /* this function must be called with interrupt disabled */
572 static void pipe_toggle_save(struct r8a66597 *r8a66597,
573 struct r8a66597_pipe *pipe,
574 struct urb *urb)
576 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
577 pipe_toggle_set(r8a66597, pipe, urb, 1);
578 else
579 pipe_toggle_set(r8a66597, pipe, urb, 0);
582 /* this function must be called with interrupt disabled */
583 static void pipe_toggle_restore(struct r8a66597 *r8a66597,
584 struct r8a66597_pipe *pipe,
585 struct urb *urb)
587 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
588 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
589 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
591 if (!toggle)
592 return;
594 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
597 /* this function must be called with interrupt disabled */
598 static void pipe_buffer_setting(struct r8a66597 *r8a66597,
599 struct r8a66597_pipe_info *info)
601 u16 val = 0;
603 if (info->pipenum == 0)
604 return;
606 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
607 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
608 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
609 if (!info->dir_in)
610 val |= R8A66597_DIR;
611 if (info->type == R8A66597_BULK && info->dir_in)
612 val |= R8A66597_DBLB | R8A66597_SHTNAK;
613 val |= info->type | info->epnum;
614 r8a66597_write(r8a66597, val, PIPECFG);
616 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
617 PIPEBUF);
618 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
619 PIPEMAXP);
620 r8a66597_write(r8a66597, info->interval, PIPEPERI);
623 /* this function must be called with interrupt disabled */
624 static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
626 struct r8a66597_pipe_info *info;
627 struct urb *urb = td->urb;
629 if (td->pipenum > 0) {
630 info = &td->pipe->info;
631 cfifo_change(r8a66597, 0);
632 pipe_buffer_setting(r8a66597, info);
634 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
635 usb_pipeout(urb->pipe)) &&
636 !usb_pipecontrol(urb->pipe)) {
637 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
638 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
639 clear_all_buffer(r8a66597, td->pipe);
640 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
641 usb_pipeout(urb->pipe), 1);
643 pipe_toggle_restore(r8a66597, td->pipe, urb);
647 /* this function must be called with interrupt disabled */
648 static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
649 struct usb_endpoint_descriptor *ep)
651 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
653 memset(array, 0, sizeof(array));
654 switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
655 case USB_ENDPOINT_XFER_BULK:
656 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
657 array[i++] = 4;
658 else {
659 array[i++] = 3;
660 array[i++] = 5;
662 break;
663 case USB_ENDPOINT_XFER_INT:
664 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
665 array[i++] = 6;
666 array[i++] = 7;
667 array[i++] = 8;
668 } else
669 array[i++] = 9;
670 break;
671 case USB_ENDPOINT_XFER_ISOC:
672 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
673 array[i++] = 2;
674 else
675 array[i++] = 1;
676 break;
677 default:
678 err("Illegal type");
679 return 0;
682 i = 1;
683 min = array[0];
684 while (array[i] != 0) {
685 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
686 min = array[i];
687 i++;
690 return min;
693 static u16 get_r8a66597_type(__u8 type)
695 u16 r8a66597_type;
697 switch (type) {
698 case USB_ENDPOINT_XFER_BULK:
699 r8a66597_type = R8A66597_BULK;
700 break;
701 case USB_ENDPOINT_XFER_INT:
702 r8a66597_type = R8A66597_INT;
703 break;
704 case USB_ENDPOINT_XFER_ISOC:
705 r8a66597_type = R8A66597_ISO;
706 break;
707 default:
708 err("Illegal type");
709 r8a66597_type = 0x0000;
710 break;
713 return r8a66597_type;
716 static u16 get_bufnum(u16 pipenum)
718 u16 bufnum = 0;
720 if (pipenum == 0)
721 bufnum = 0;
722 else if (check_bulk_or_isoc(pipenum))
723 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
724 else if (check_interrupt(pipenum))
725 bufnum = 4 + (pipenum - 6);
726 else
727 err("Illegal pipenum (%d)", pipenum);
729 return bufnum;
732 static u16 get_buf_bsize(u16 pipenum)
734 u16 buf_bsize = 0;
736 if (pipenum == 0)
737 buf_bsize = 3;
738 else if (check_bulk_or_isoc(pipenum))
739 buf_bsize = R8A66597_BUF_BSIZE - 1;
740 else if (check_interrupt(pipenum))
741 buf_bsize = 0;
742 else
743 err("Illegal pipenum (%d)", pipenum);
745 return buf_bsize;
748 /* this function must be called with interrupt disabled */
749 static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
750 struct r8a66597_device *dev,
751 struct r8a66597_pipe *pipe,
752 struct urb *urb)
754 #if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
755 int i;
756 struct r8a66597_pipe_info *info = &pipe->info;
758 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
759 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
760 if ((r8a66597->dma_map & (1 << i)) != 0)
761 continue;
763 info("address %d, EndpointAddress 0x%02x use DMA FIFO",
764 usb_pipedevice(urb->pipe),
765 info->dir_in ? USB_ENDPOINT_DIR_MASK + info->epnum
766 : info->epnum);
768 r8a66597->dma_map |= 1 << i;
769 dev->dma_map |= 1 << i;
770 set_pipe_reg_addr(pipe, i);
772 cfifo_change(r8a66597, 0);
773 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum,
774 MBW | CURPIPE, pipe->fifosel);
776 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
777 pipe->info.pipenum);
778 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
779 break;
782 #endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
785 /* this function must be called with interrupt disabled */
786 static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
787 struct usb_host_endpoint *hep,
788 struct r8a66597_pipe_info *info)
790 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
791 struct r8a66597_pipe *pipe = hep->hcpriv;
793 dbg("enable_pipe:");
795 pipe->info = *info;
796 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
797 r8a66597->pipe_cnt[pipe->info.pipenum]++;
798 dev->pipe_cnt[pipe->info.pipenum]++;
800 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
803 /* this function must be called with interrupt disabled */
804 static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
806 struct r8a66597_td *td, *next;
807 struct urb *urb;
808 struct list_head *list = &r8a66597->pipe_queue[pipenum];
810 if (list_empty(list))
811 return;
813 list_for_each_entry_safe(td, next, list, queue) {
814 if (!td)
815 continue;
816 if (td->address != address)
817 continue;
819 urb = td->urb;
820 list_del(&td->queue);
821 kfree(td);
823 if (urb) {
824 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
825 urb);
827 spin_unlock(&r8a66597->lock);
828 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
829 -ENODEV);
830 spin_lock(&r8a66597->lock);
832 break;
836 /* this function must be called with interrupt disabled */
837 static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
838 struct r8a66597_device *dev)
840 int check_ep0 = 0;
841 u16 pipenum;
843 if (!dev)
844 return;
846 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
847 if (!dev->pipe_cnt[pipenum])
848 continue;
850 if (!check_ep0) {
851 check_ep0 = 1;
852 force_dequeue(r8a66597, 0, dev->address);
855 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
856 dev->pipe_cnt[pipenum] = 0;
857 force_dequeue(r8a66597, pipenum, dev->address);
860 dbg("disable_pipe");
862 r8a66597->dma_map &= ~(dev->dma_map);
863 dev->dma_map = 0;
866 static u16 get_interval(struct urb *urb, __u8 interval)
868 u16 time = 1;
869 int i;
871 if (urb->dev->speed == USB_SPEED_HIGH) {
872 if (interval > IITV)
873 time = IITV;
874 else
875 time = interval ? interval - 1 : 0;
876 } else {
877 if (interval > 128) {
878 time = IITV;
879 } else {
880 /* calculate the nearest value for PIPEPERI */
881 for (i = 0; i < 7; i++) {
882 if ((1 << i) < interval &&
883 (1 << (i + 1) > interval))
884 time = 1 << i;
889 return time;
892 static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
894 __u8 i;
895 unsigned long time = 1;
897 if (usb_pipeisoc(urb->pipe))
898 return 0;
900 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
901 for (i = 0; i < (interval - 1); i++)
902 time *= 2;
903 time = time * 125 / 1000; /* uSOF -> msec */
904 } else {
905 time = interval;
908 return time;
911 /* this function must be called with interrupt disabled */
912 static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
913 struct usb_host_endpoint *hep,
914 struct usb_endpoint_descriptor *ep)
916 struct r8a66597_pipe_info info;
918 info.pipenum = get_empty_pipenum(r8a66597, ep);
919 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
920 info.epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
921 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
922 info.type = get_r8a66597_type(ep->bmAttributes
923 & USB_ENDPOINT_XFERTYPE_MASK);
924 info.bufnum = get_bufnum(info.pipenum);
925 info.buf_bsize = get_buf_bsize(info.pipenum);
926 if (info.type == R8A66597_BULK) {
927 info.interval = 0;
928 info.timer_interval = 0;
929 } else {
930 info.interval = get_interval(urb, ep->bInterval);
931 info.timer_interval = get_timer_interval(urb, ep->bInterval);
933 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
934 info.dir_in = 1;
935 else
936 info.dir_in = 0;
938 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
941 static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
943 struct r8a66597_device *dev;
945 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
946 dev->state = USB_STATE_CONFIGURED;
949 static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
950 u16 pipenum)
952 if (pipenum == 0 && usb_pipeout(urb->pipe))
953 enable_irq_empty(r8a66597, pipenum);
954 else
955 enable_irq_ready(r8a66597, pipenum);
957 if (!usb_pipeisoc(urb->pipe))
958 enable_irq_nrdy(r8a66597, pipenum);
961 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
963 disable_irq_ready(r8a66597, pipenum);
964 disable_irq_nrdy(r8a66597, pipenum);
967 /* this function must be called with interrupt disabled */
968 static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
969 u16 syssts)
971 if (syssts == SE0) {
972 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
973 return;
976 if (syssts == FS_JSTS)
977 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
978 else if (syssts == LS_JSTS)
979 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
981 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
982 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
985 /* this function must be called with interrupt disabled */
986 static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
988 u16 speed = get_rh_usb_speed(r8a66597, port);
989 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
991 if (speed == HSMODE)
992 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
993 else if (speed == LSMODE)
994 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
996 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
997 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1000 /* this function must be called with interrupt disabled */
1001 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1003 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1005 r8a66597->root_hub[port].port &= ~(1 << USB_PORT_FEAT_CONNECTION);
1006 r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_C_CONNECTION);
1008 disable_r8a66597_pipe_all(r8a66597, dev);
1009 free_usb_address(r8a66597, dev);
1011 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1014 /* this function must be called with interrupt disabled */
1015 static void prepare_setup_packet(struct r8a66597 *r8a66597,
1016 struct r8a66597_td *td)
1018 int i;
1019 __le16 *p = (__le16 *)td->urb->setup_packet;
1020 unsigned long setup_addr = USBREQ;
1022 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1023 DCPMAXP);
1024 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
1026 for (i = 0; i < 4; i++) {
1027 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
1028 setup_addr += 2;
1030 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1033 /* this function must be called with interrupt disabled */
1034 static void prepare_packet_read(struct r8a66597 *r8a66597,
1035 struct r8a66597_td *td)
1037 struct urb *urb = td->urb;
1039 if (usb_pipecontrol(urb->pipe)) {
1040 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1041 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1042 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1043 if (urb->actual_length == 0) {
1044 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1045 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1047 pipe_irq_disable(r8a66597, td->pipenum);
1048 pipe_start(r8a66597, td->pipe);
1049 pipe_irq_enable(r8a66597, urb, td->pipenum);
1050 } else {
1051 if (urb->actual_length == 0) {
1052 pipe_irq_disable(r8a66597, td->pipenum);
1053 pipe_setting(r8a66597, td);
1054 pipe_stop(r8a66597, td->pipe);
1055 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1057 if (td->pipe->pipetre) {
1058 r8a66597_write(r8a66597, TRCLR,
1059 td->pipe->pipetre);
1060 r8a66597_write(r8a66597,
1061 DIV_ROUND_UP
1062 (urb->transfer_buffer_length,
1063 td->maxpacket),
1064 td->pipe->pipetrn);
1065 r8a66597_bset(r8a66597, TRENB,
1066 td->pipe->pipetre);
1069 pipe_start(r8a66597, td->pipe);
1070 pipe_irq_enable(r8a66597, urb, td->pipenum);
1075 /* this function must be called with interrupt disabled */
1076 static void prepare_packet_write(struct r8a66597 *r8a66597,
1077 struct r8a66597_td *td)
1079 u16 tmp;
1080 struct urb *urb = td->urb;
1082 if (usb_pipecontrol(urb->pipe)) {
1083 pipe_stop(r8a66597, td->pipe);
1084 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1085 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1086 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1087 if (urb->actual_length == 0) {
1088 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1089 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1091 } else {
1092 if (urb->actual_length == 0)
1093 pipe_setting(r8a66597, td);
1094 if (td->pipe->pipetre)
1095 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1097 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1099 fifo_change_from_pipe(r8a66597, td->pipe);
1100 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1101 if (unlikely((tmp & FRDY) == 0))
1102 pipe_irq_enable(r8a66597, urb, td->pipenum);
1103 else
1104 packet_write(r8a66597, td->pipenum);
1105 pipe_start(r8a66597, td->pipe);
1108 /* this function must be called with interrupt disabled */
1109 static void prepare_status_packet(struct r8a66597 *r8a66597,
1110 struct r8a66597_td *td)
1112 struct urb *urb = td->urb;
1114 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1115 pipe_stop(r8a66597, td->pipe);
1117 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1118 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1119 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1120 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1121 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
1122 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
1123 enable_irq_empty(r8a66597, 0);
1124 } else {
1125 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1126 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1127 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1128 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1129 enable_irq_ready(r8a66597, 0);
1131 enable_irq_nrdy(r8a66597, 0);
1132 pipe_start(r8a66597, td->pipe);
1135 static int is_set_address(unsigned char *setup_packet)
1137 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1138 setup_packet[1] == USB_REQ_SET_ADDRESS)
1139 return 1;
1140 else
1141 return 0;
1144 /* this function must be called with interrupt disabled */
1145 static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1147 BUG_ON(!td);
1149 switch (td->type) {
1150 case USB_PID_SETUP:
1151 if (is_set_address(td->urb->setup_packet)) {
1152 td->set_address = 1;
1153 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1154 td->urb);
1155 if (td->urb->setup_packet[2] == 0)
1156 return -EPIPE;
1158 prepare_setup_packet(r8a66597, td);
1159 break;
1160 case USB_PID_IN:
1161 prepare_packet_read(r8a66597, td);
1162 break;
1163 case USB_PID_OUT:
1164 prepare_packet_write(r8a66597, td);
1165 break;
1166 case USB_PID_ACK:
1167 prepare_status_packet(r8a66597, td);
1168 break;
1169 default:
1170 err("invalid type.");
1171 break;
1174 return 0;
1177 static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1179 if (usb_pipeisoc(urb->pipe)) {
1180 if (urb->number_of_packets == td->iso_cnt)
1181 return 1;
1184 /* control or bulk or interrupt */
1185 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1186 (td->short_packet) || (td->zero_packet))
1187 return 1;
1189 return 0;
1192 /* this function must be called with interrupt disabled */
1193 static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1195 unsigned long time;
1197 BUG_ON(!td);
1199 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1200 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1201 r8a66597->timeout_map |= 1 << td->pipenum;
1202 switch (usb_pipetype(td->urb->pipe)) {
1203 case PIPE_INTERRUPT:
1204 case PIPE_ISOCHRONOUS:
1205 time = 30;
1206 break;
1207 default:
1208 time = 300;
1209 break;
1212 mod_timer(&r8a66597->td_timer[td->pipenum],
1213 jiffies + msecs_to_jiffies(time));
1217 /* this function must be called with interrupt disabled */
1218 static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1219 u16 pipenum, struct urb *urb, int status)
1220 __releases(r8a66597->lock) __acquires(r8a66597->lock)
1222 int restart = 0;
1223 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1225 r8a66597->timeout_map &= ~(1 << pipenum);
1227 if (likely(td)) {
1228 if (td->set_address && (status != 0 || urb->unlinked))
1229 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1231 pipe_toggle_save(r8a66597, td->pipe, urb);
1232 list_del(&td->queue);
1233 kfree(td);
1236 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1237 restart = 1;
1239 if (likely(urb)) {
1240 if (usb_pipeisoc(urb->pipe))
1241 urb->start_frame = r8a66597_get_frame(hcd);
1243 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
1244 spin_unlock(&r8a66597->lock);
1245 usb_hcd_giveback_urb(hcd, urb, status);
1246 spin_lock(&r8a66597->lock);
1249 if (restart) {
1250 td = r8a66597_get_td(r8a66597, pipenum);
1251 if (unlikely(!td))
1252 return;
1254 start_transfer(r8a66597, td);
1255 set_td_timer(r8a66597, td);
1259 static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1261 u16 tmp;
1262 int rcv_len, bufsize, urb_len, size;
1263 u16 *buf;
1264 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1265 struct urb *urb;
1266 int finish = 0;
1267 int status = 0;
1269 if (unlikely(!td))
1270 return;
1271 urb = td->urb;
1273 fifo_change_from_pipe(r8a66597, td->pipe);
1274 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1275 if (unlikely((tmp & FRDY) == 0)) {
1276 pipe_stop(r8a66597, td->pipe);
1277 pipe_irq_disable(r8a66597, pipenum);
1278 err("in fifo not ready (%d)", pipenum);
1279 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
1280 return;
1283 /* prepare parameters */
1284 rcv_len = tmp & DTLN;
1285 if (usb_pipeisoc(urb->pipe)) {
1286 buf = (u16 *)(urb->transfer_buffer +
1287 urb->iso_frame_desc[td->iso_cnt].offset);
1288 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1289 } else {
1290 buf = (void *)urb->transfer_buffer + urb->actual_length;
1291 urb_len = urb->transfer_buffer_length - urb->actual_length;
1293 bufsize = min(urb_len, (int) td->maxpacket);
1294 if (rcv_len <= bufsize) {
1295 size = rcv_len;
1296 } else {
1297 size = bufsize;
1298 status = -EOVERFLOW;
1299 finish = 1;
1302 /* update parameters */
1303 urb->actual_length += size;
1304 if (rcv_len == 0)
1305 td->zero_packet = 1;
1306 if (rcv_len < bufsize) {
1307 td->short_packet = 1;
1309 if (usb_pipeisoc(urb->pipe)) {
1310 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1311 urb->iso_frame_desc[td->iso_cnt].status = status;
1312 td->iso_cnt++;
1313 finish = 0;
1316 /* check transfer finish */
1317 if (finish || check_transfer_finish(td, urb)) {
1318 pipe_stop(r8a66597, td->pipe);
1319 pipe_irq_disable(r8a66597, pipenum);
1320 finish = 1;
1323 /* read fifo */
1324 if (urb->transfer_buffer) {
1325 if (size == 0)
1326 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1327 else
1328 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1329 buf, size);
1332 if (finish && pipenum != 0)
1333 finish_request(r8a66597, td, pipenum, urb, status);
1336 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1338 u16 tmp;
1339 int bufsize, size;
1340 u16 *buf;
1341 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1342 struct urb *urb;
1344 if (unlikely(!td))
1345 return;
1346 urb = td->urb;
1348 fifo_change_from_pipe(r8a66597, td->pipe);
1349 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1350 if (unlikely((tmp & FRDY) == 0)) {
1351 pipe_stop(r8a66597, td->pipe);
1352 pipe_irq_disable(r8a66597, pipenum);
1353 err("out write fifo not ready. (%d)", pipenum);
1354 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
1355 return;
1358 /* prepare parameters */
1359 bufsize = td->maxpacket;
1360 if (usb_pipeisoc(urb->pipe)) {
1361 buf = (u16 *)(urb->transfer_buffer +
1362 urb->iso_frame_desc[td->iso_cnt].offset);
1363 size = min(bufsize,
1364 (int)urb->iso_frame_desc[td->iso_cnt].length);
1365 } else {
1366 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1367 size = min((int)bufsize,
1368 urb->transfer_buffer_length - urb->actual_length);
1371 /* write fifo */
1372 if (pipenum > 0)
1373 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
1374 if (urb->transfer_buffer) {
1375 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1376 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1377 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1380 /* update parameters */
1381 urb->actual_length += size;
1382 if (usb_pipeisoc(urb->pipe)) {
1383 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1384 urb->iso_frame_desc[td->iso_cnt].status = 0;
1385 td->iso_cnt++;
1388 /* check transfer finish */
1389 if (check_transfer_finish(td, urb)) {
1390 disable_irq_ready(r8a66597, pipenum);
1391 enable_irq_empty(r8a66597, pipenum);
1392 if (!usb_pipeisoc(urb->pipe))
1393 enable_irq_nrdy(r8a66597, pipenum);
1394 } else
1395 pipe_irq_enable(r8a66597, urb, pipenum);
1399 static void check_next_phase(struct r8a66597 *r8a66597, int status)
1401 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1402 struct urb *urb;
1403 u8 finish = 0;
1405 if (unlikely(!td))
1406 return;
1407 urb = td->urb;
1409 switch (td->type) {
1410 case USB_PID_IN:
1411 case USB_PID_OUT:
1412 if (check_transfer_finish(td, urb))
1413 td->type = USB_PID_ACK;
1414 break;
1415 case USB_PID_SETUP:
1416 if (urb->transfer_buffer_length == urb->actual_length)
1417 td->type = USB_PID_ACK;
1418 else if (usb_pipeout(urb->pipe))
1419 td->type = USB_PID_OUT;
1420 else
1421 td->type = USB_PID_IN;
1422 break;
1423 case USB_PID_ACK:
1424 finish = 1;
1425 break;
1428 if (finish || status != 0 || urb->unlinked)
1429 finish_request(r8a66597, td, 0, urb, status);
1430 else
1431 start_transfer(r8a66597, td);
1434 static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
1436 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1438 if (td) {
1439 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1441 if (pid == PID_NAK)
1442 return -ECONNRESET;
1443 else
1444 return -EPIPE;
1446 return 0;
1449 static void irq_pipe_ready(struct r8a66597 *r8a66597)
1451 u16 check;
1452 u16 pipenum;
1453 u16 mask;
1454 struct r8a66597_td *td;
1456 mask = r8a66597_read(r8a66597, BRDYSTS)
1457 & r8a66597_read(r8a66597, BRDYENB);
1458 r8a66597_write(r8a66597, ~mask, BRDYSTS);
1459 if (mask & BRDY0) {
1460 td = r8a66597_get_td(r8a66597, 0);
1461 if (td && td->type == USB_PID_IN)
1462 packet_read(r8a66597, 0);
1463 else
1464 pipe_irq_disable(r8a66597, 0);
1465 check_next_phase(r8a66597, 0);
1468 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1469 check = 1 << pipenum;
1470 if (mask & check) {
1471 td = r8a66597_get_td(r8a66597, pipenum);
1472 if (unlikely(!td))
1473 continue;
1475 if (td->type == USB_PID_IN)
1476 packet_read(r8a66597, pipenum);
1477 else if (td->type == USB_PID_OUT)
1478 packet_write(r8a66597, pipenum);
1483 static void irq_pipe_empty(struct r8a66597 *r8a66597)
1485 u16 tmp;
1486 u16 check;
1487 u16 pipenum;
1488 u16 mask;
1489 struct r8a66597_td *td;
1491 mask = r8a66597_read(r8a66597, BEMPSTS)
1492 & r8a66597_read(r8a66597, BEMPENB);
1493 r8a66597_write(r8a66597, ~mask, BEMPSTS);
1494 if (mask & BEMP0) {
1495 cfifo_change(r8a66597, 0);
1496 td = r8a66597_get_td(r8a66597, 0);
1497 if (td && td->type != USB_PID_OUT)
1498 disable_irq_empty(r8a66597, 0);
1499 check_next_phase(r8a66597, 0);
1502 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1503 check = 1 << pipenum;
1504 if (mask & check) {
1505 struct r8a66597_td *td;
1506 td = r8a66597_get_td(r8a66597, pipenum);
1507 if (unlikely(!td))
1508 continue;
1510 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1511 if ((tmp & INBUFM) == 0) {
1512 disable_irq_empty(r8a66597, pipenum);
1513 pipe_irq_disable(r8a66597, pipenum);
1514 finish_request(r8a66597, td, pipenum, td->urb,
1521 static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1523 u16 check;
1524 u16 pipenum;
1525 u16 mask;
1526 int status;
1528 mask = r8a66597_read(r8a66597, NRDYSTS)
1529 & r8a66597_read(r8a66597, NRDYENB);
1530 r8a66597_write(r8a66597, ~mask, NRDYSTS);
1531 if (mask & NRDY0) {
1532 cfifo_change(r8a66597, 0);
1533 status = get_urb_error(r8a66597, 0);
1534 pipe_irq_disable(r8a66597, 0);
1535 check_next_phase(r8a66597, status);
1538 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1539 check = 1 << pipenum;
1540 if (mask & check) {
1541 struct r8a66597_td *td;
1542 td = r8a66597_get_td(r8a66597, pipenum);
1543 if (unlikely(!td))
1544 continue;
1546 status = get_urb_error(r8a66597, pipenum);
1547 pipe_irq_disable(r8a66597, pipenum);
1548 pipe_stop(r8a66597, td->pipe);
1549 finish_request(r8a66597, td, pipenum, td->urb, status);
1554 static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
1556 mod_timer(&r8a66597->rh_timer,
1557 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
1560 static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port)
1562 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1564 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1565 rh->scount = R8A66597_MAX_SAMPLING;
1566 r8a66597->root_hub[port].port |= (1 << USB_PORT_FEAT_CONNECTION)
1567 | (1 << USB_PORT_FEAT_C_CONNECTION);
1568 r8a66597_root_hub_start_polling(r8a66597);
1571 static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1573 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1574 u16 intsts0, intsts1, intsts2;
1575 u16 intenb0, intenb1, intenb2;
1576 u16 mask0, mask1, mask2;
1577 int status;
1579 spin_lock(&r8a66597->lock);
1581 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1582 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1583 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1584 intenb0 = r8a66597_read(r8a66597, INTENB0);
1585 intenb1 = r8a66597_read(r8a66597, INTENB1);
1586 intenb2 = r8a66597_read(r8a66597, INTENB2);
1588 mask2 = intsts2 & intenb2;
1589 mask1 = intsts1 & intenb1;
1590 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1591 if (mask2) {
1592 if (mask2 & ATTCH) {
1593 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
1594 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1596 /* start usb bus sampling */
1597 start_root_hub_sampling(r8a66597, 1);
1599 if (mask2 & DTCH) {
1600 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
1601 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1602 r8a66597_usb_disconnect(r8a66597, 1);
1606 if (mask1) {
1607 if (mask1 & ATTCH) {
1608 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
1609 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1611 /* start usb bus sampling */
1612 start_root_hub_sampling(r8a66597, 0);
1614 if (mask1 & DTCH) {
1615 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
1616 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1617 r8a66597_usb_disconnect(r8a66597, 0);
1619 if (mask1 & SIGN) {
1620 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
1621 status = get_urb_error(r8a66597, 0);
1622 check_next_phase(r8a66597, status);
1624 if (mask1 & SACK) {
1625 r8a66597_write(r8a66597, ~SACK, INTSTS1);
1626 check_next_phase(r8a66597, 0);
1629 if (mask0) {
1630 if (mask0 & BRDY)
1631 irq_pipe_ready(r8a66597);
1632 if (mask0 & BEMP)
1633 irq_pipe_empty(r8a66597);
1634 if (mask0 & NRDY)
1635 irq_pipe_nrdy(r8a66597);
1638 spin_unlock(&r8a66597->lock);
1639 return IRQ_HANDLED;
1642 /* this function must be called with interrupt disabled */
1643 static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1645 u16 tmp;
1646 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1648 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1649 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1651 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1652 if ((tmp & USBRST) == USBRST) {
1653 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1654 dvstctr_reg);
1655 r8a66597_root_hub_start_polling(r8a66597);
1656 } else
1657 r8a66597_usb_connect(r8a66597, port);
1660 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1661 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1662 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1665 if (rh->scount > 0) {
1666 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1667 if (tmp == rh->old_syssts) {
1668 rh->scount--;
1669 if (rh->scount == 0)
1670 r8a66597_check_syssts(r8a66597, port, tmp);
1671 else
1672 r8a66597_root_hub_start_polling(r8a66597);
1673 } else {
1674 rh->scount = R8A66597_MAX_SAMPLING;
1675 rh->old_syssts = tmp;
1676 r8a66597_root_hub_start_polling(r8a66597);
1681 static void r8a66597_interval_timer(unsigned long _r8a66597)
1683 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1684 unsigned long flags;
1685 u16 pipenum;
1686 struct r8a66597_td *td;
1688 spin_lock_irqsave(&r8a66597->lock, flags);
1690 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1691 if (!(r8a66597->interval_map & (1 << pipenum)))
1692 continue;
1693 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1694 continue;
1696 td = r8a66597_get_td(r8a66597, pipenum);
1697 if (td)
1698 start_transfer(r8a66597, td);
1701 spin_unlock_irqrestore(&r8a66597->lock, flags);
1704 static void r8a66597_td_timer(unsigned long _r8a66597)
1706 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1707 unsigned long flags;
1708 u16 pipenum;
1709 struct r8a66597_td *td, *new_td = NULL;
1710 struct r8a66597_pipe *pipe;
1712 spin_lock_irqsave(&r8a66597->lock, flags);
1713 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1714 if (!(r8a66597->timeout_map & (1 << pipenum)))
1715 continue;
1716 if (timer_pending(&r8a66597->td_timer[pipenum]))
1717 continue;
1719 td = r8a66597_get_td(r8a66597, pipenum);
1720 if (!td) {
1721 r8a66597->timeout_map &= ~(1 << pipenum);
1722 continue;
1725 if (td->urb->actual_length) {
1726 set_td_timer(r8a66597, td);
1727 break;
1730 pipe = td->pipe;
1731 pipe_stop(r8a66597, pipe);
1733 new_td = td;
1734 do {
1735 list_move_tail(&new_td->queue,
1736 &r8a66597->pipe_queue[pipenum]);
1737 new_td = r8a66597_get_td(r8a66597, pipenum);
1738 if (!new_td) {
1739 new_td = td;
1740 break;
1742 } while (td != new_td && td->address == new_td->address);
1744 start_transfer(r8a66597, new_td);
1746 if (td == new_td)
1747 r8a66597->timeout_map &= ~(1 << pipenum);
1748 else
1749 set_td_timer(r8a66597, new_td);
1750 break;
1752 spin_unlock_irqrestore(&r8a66597->lock, flags);
1755 static void r8a66597_timer(unsigned long _r8a66597)
1757 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1758 unsigned long flags;
1760 spin_lock_irqsave(&r8a66597->lock, flags);
1762 r8a66597_root_hub_control(r8a66597, 0);
1763 r8a66597_root_hub_control(r8a66597, 1);
1765 spin_unlock_irqrestore(&r8a66597->lock, flags);
1768 static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1770 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1772 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1773 (urb->dev->state == USB_STATE_CONFIGURED))
1774 return 1;
1775 else
1776 return 0;
1779 static int r8a66597_start(struct usb_hcd *hcd)
1781 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1783 hcd->state = HC_STATE_RUNNING;
1784 return enable_controller(r8a66597);
1787 static void r8a66597_stop(struct usb_hcd *hcd)
1789 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1791 disable_controller(r8a66597);
1794 static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1796 unsigned int usb_address = usb_pipedevice(urb->pipe);
1797 u16 root_port, hub_port;
1799 if (usb_address == 0) {
1800 get_port_number(urb->dev->devpath,
1801 &root_port, &hub_port);
1802 set_devadd_reg(r8a66597, 0,
1803 get_r8a66597_usb_speed(urb->dev->speed),
1804 get_parent_r8a66597_address(r8a66597, urb->dev),
1805 hub_port, root_port);
1809 static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1810 struct urb *urb,
1811 struct usb_host_endpoint *hep)
1813 struct r8a66597_td *td;
1814 u16 pipenum;
1816 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
1817 if (td == NULL)
1818 return NULL;
1820 pipenum = r8a66597_get_pipenum(urb, hep);
1821 td->pipenum = pipenum;
1822 td->pipe = hep->hcpriv;
1823 td->urb = urb;
1824 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1825 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1826 !usb_pipein(urb->pipe));
1827 if (usb_pipecontrol(urb->pipe))
1828 td->type = USB_PID_SETUP;
1829 else if (usb_pipein(urb->pipe))
1830 td->type = USB_PID_IN;
1831 else
1832 td->type = USB_PID_OUT;
1833 INIT_LIST_HEAD(&td->queue);
1835 return td;
1838 static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
1839 struct urb *urb,
1840 gfp_t mem_flags)
1842 struct usb_host_endpoint *hep = urb->ep;
1843 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1844 struct r8a66597_td *td = NULL;
1845 int ret, request = 0;
1846 unsigned long flags;
1848 spin_lock_irqsave(&r8a66597->lock, flags);
1849 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1850 ret = -ENODEV;
1851 goto error_not_linked;
1854 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1855 if (ret)
1856 goto error_not_linked;
1858 if (!hep->hcpriv) {
1859 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1860 GFP_ATOMIC);
1861 if (!hep->hcpriv) {
1862 ret = -ENOMEM;
1863 goto error;
1865 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1866 if (usb_pipeendpoint(urb->pipe))
1867 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1870 if (unlikely(check_pipe_config(r8a66597, urb)))
1871 init_pipe_config(r8a66597, urb);
1873 set_address_zero(r8a66597, urb);
1874 td = r8a66597_make_td(r8a66597, urb, hep);
1875 if (td == NULL) {
1876 ret = -ENOMEM;
1877 goto error;
1879 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1880 request = 1;
1881 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
1882 urb->hcpriv = td;
1884 if (request) {
1885 if (td->pipe->info.timer_interval) {
1886 r8a66597->interval_map |= 1 << td->pipenum;
1887 mod_timer(&r8a66597->interval_timer[td->pipenum],
1888 jiffies + msecs_to_jiffies(
1889 td->pipe->info.timer_interval));
1890 } else {
1891 ret = start_transfer(r8a66597, td);
1892 if (ret < 0) {
1893 list_del(&td->queue);
1894 kfree(td);
1897 } else
1898 set_td_timer(r8a66597, td);
1900 error:
1901 if (ret)
1902 usb_hcd_unlink_urb_from_ep(hcd, urb);
1903 error_not_linked:
1904 spin_unlock_irqrestore(&r8a66597->lock, flags);
1905 return ret;
1908 static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1909 int status)
1911 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1912 struct r8a66597_td *td;
1913 unsigned long flags;
1914 int rc;
1916 spin_lock_irqsave(&r8a66597->lock, flags);
1917 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1918 if (rc)
1919 goto done;
1921 if (urb->hcpriv) {
1922 td = urb->hcpriv;
1923 pipe_stop(r8a66597, td->pipe);
1924 pipe_irq_disable(r8a66597, td->pipenum);
1925 disable_irq_empty(r8a66597, td->pipenum);
1926 finish_request(r8a66597, td, td->pipenum, urb, status);
1928 done:
1929 spin_unlock_irqrestore(&r8a66597->lock, flags);
1930 return rc;
1933 static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1934 struct usb_host_endpoint *hep)
1936 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1937 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1938 struct r8a66597_td *td;
1939 struct urb *urb = NULL;
1940 u16 pipenum;
1941 unsigned long flags;
1943 if (pipe == NULL)
1944 return;
1945 pipenum = pipe->info.pipenum;
1947 if (pipenum == 0) {
1948 kfree(hep->hcpriv);
1949 hep->hcpriv = NULL;
1950 return;
1953 spin_lock_irqsave(&r8a66597->lock, flags);
1954 pipe_stop(r8a66597, pipe);
1955 pipe_irq_disable(r8a66597, pipenum);
1956 disable_irq_empty(r8a66597, pipenum);
1957 td = r8a66597_get_td(r8a66597, pipenum);
1958 if (td)
1959 urb = td->urb;
1960 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
1961 kfree(hep->hcpriv);
1962 hep->hcpriv = NULL;
1963 spin_unlock_irqrestore(&r8a66597->lock, flags);
1966 static int r8a66597_get_frame(struct usb_hcd *hcd)
1968 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1969 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1972 static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
1974 int chix;
1976 if (udev->state == USB_STATE_CONFIGURED &&
1977 udev->parent && udev->parent->devnum > 1 &&
1978 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
1979 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
1981 for (chix = 0; chix < udev->maxchild; chix++) {
1982 struct usb_device *childdev = udev->children[chix];
1984 if (childdev)
1985 collect_usb_address_map(childdev, map);
1989 /* this function must be called with interrupt disabled */
1990 static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
1991 int addr)
1993 struct r8a66597_device *dev;
1994 struct list_head *list = &r8a66597->child_device;
1996 list_for_each_entry(dev, list, device_list) {
1997 if (!dev)
1998 continue;
1999 if (dev->usb_address != addr)
2000 continue;
2002 return dev;
2005 err("get_r8a66597_device fail.(%d)\n", addr);
2006 return NULL;
2009 static void update_usb_address_map(struct r8a66597 *r8a66597,
2010 struct usb_device *root_hub,
2011 unsigned long *map)
2013 int i, j, addr;
2014 unsigned long diff;
2015 unsigned long flags;
2017 for (i = 0; i < 4; i++) {
2018 diff = r8a66597->child_connect_map[i] ^ map[i];
2019 if (!diff)
2020 continue;
2022 for (j = 0; j < 32; j++) {
2023 if (!(diff & (1 << j)))
2024 continue;
2026 addr = i * 32 + j;
2027 if (map[i] & (1 << j))
2028 set_child_connect_map(r8a66597, addr);
2029 else {
2030 struct r8a66597_device *dev;
2032 spin_lock_irqsave(&r8a66597->lock, flags);
2033 dev = get_r8a66597_device(r8a66597, addr);
2034 disable_r8a66597_pipe_all(r8a66597, dev);
2035 free_usb_address(r8a66597, dev);
2036 put_child_connect_map(r8a66597, addr);
2037 spin_unlock_irqrestore(&r8a66597->lock, flags);
2043 static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2044 struct usb_hcd *hcd)
2046 struct usb_bus *bus;
2047 unsigned long now_map[4];
2049 memset(now_map, 0, sizeof(now_map));
2051 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2052 if (!bus->root_hub)
2053 continue;
2055 if (bus->busnum != hcd->self.busnum)
2056 continue;
2058 collect_usb_address_map(bus->root_hub, now_map);
2059 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2063 static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2065 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2066 unsigned long flags;
2067 int i;
2069 r8a66597_check_detect_child(r8a66597, hcd);
2071 spin_lock_irqsave(&r8a66597->lock, flags);
2073 *buf = 0; /* initialize (no change) */
2075 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
2076 if (r8a66597->root_hub[i].port & 0xffff0000)
2077 *buf |= 1 << (i + 1);
2080 spin_unlock_irqrestore(&r8a66597->lock, flags);
2082 return (*buf != 0);
2085 static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2086 struct usb_hub_descriptor *desc)
2088 desc->bDescriptorType = 0x29;
2089 desc->bHubContrCurrent = 0;
2090 desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
2091 desc->bDescLength = 9;
2092 desc->bPwrOn2PwrGood = 0;
2093 desc->wHubCharacteristics = cpu_to_le16(0x0011);
2094 desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
2095 desc->bitmap[1] = ~0;
2098 static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2099 u16 wIndex, char *buf, u16 wLength)
2101 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2102 int ret;
2103 int port = (wIndex & 0x00FF) - 1;
2104 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2105 unsigned long flags;
2107 ret = 0;
2109 spin_lock_irqsave(&r8a66597->lock, flags);
2110 switch (typeReq) {
2111 case ClearHubFeature:
2112 case SetHubFeature:
2113 switch (wValue) {
2114 case C_HUB_OVER_CURRENT:
2115 case C_HUB_LOCAL_POWER:
2116 break;
2117 default:
2118 goto error;
2120 break;
2121 case ClearPortFeature:
2122 if (wIndex > R8A66597_MAX_ROOT_HUB)
2123 goto error;
2124 if (wLength != 0)
2125 goto error;
2127 switch (wValue) {
2128 case USB_PORT_FEAT_ENABLE:
2129 rh->port &= (1 << USB_PORT_FEAT_POWER);
2130 break;
2131 case USB_PORT_FEAT_SUSPEND:
2132 break;
2133 case USB_PORT_FEAT_POWER:
2134 r8a66597_port_power(r8a66597, port, 0);
2135 break;
2136 case USB_PORT_FEAT_C_ENABLE:
2137 case USB_PORT_FEAT_C_SUSPEND:
2138 case USB_PORT_FEAT_C_CONNECTION:
2139 case USB_PORT_FEAT_C_OVER_CURRENT:
2140 case USB_PORT_FEAT_C_RESET:
2141 break;
2142 default:
2143 goto error;
2145 rh->port &= ~(1 << wValue);
2146 break;
2147 case GetHubDescriptor:
2148 r8a66597_hub_descriptor(r8a66597,
2149 (struct usb_hub_descriptor *)buf);
2150 break;
2151 case GetHubStatus:
2152 *buf = 0x00;
2153 break;
2154 case GetPortStatus:
2155 if (wIndex > R8A66597_MAX_ROOT_HUB)
2156 goto error;
2157 *(__le32 *)buf = cpu_to_le32(rh->port);
2158 break;
2159 case SetPortFeature:
2160 if (wIndex > R8A66597_MAX_ROOT_HUB)
2161 goto error;
2162 if (wLength != 0)
2163 goto error;
2165 switch (wValue) {
2166 case USB_PORT_FEAT_SUSPEND:
2167 break;
2168 case USB_PORT_FEAT_POWER:
2169 r8a66597_port_power(r8a66597, port, 1);
2170 rh->port |= (1 << USB_PORT_FEAT_POWER);
2171 break;
2172 case USB_PORT_FEAT_RESET: {
2173 struct r8a66597_device *dev = rh->dev;
2175 rh->port |= (1 << USB_PORT_FEAT_RESET);
2177 disable_r8a66597_pipe_all(r8a66597, dev);
2178 free_usb_address(r8a66597, dev);
2180 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2181 get_dvstctr_reg(port));
2182 mod_timer(&r8a66597->rh_timer,
2183 jiffies + msecs_to_jiffies(50));
2185 break;
2186 default:
2187 goto error;
2189 rh->port |= 1 << wValue;
2190 break;
2191 default:
2192 error:
2193 ret = -EPIPE;
2194 break;
2197 spin_unlock_irqrestore(&r8a66597->lock, flags);
2198 return ret;
2201 static struct hc_driver r8a66597_hc_driver = {
2202 .description = hcd_name,
2203 .hcd_priv_size = sizeof(struct r8a66597),
2204 .irq = r8a66597_irq,
2207 * generic hardware linkage
2209 .flags = HCD_USB2,
2211 .start = r8a66597_start,
2212 .stop = r8a66597_stop,
2215 * managing i/o requests and associated device resources
2217 .urb_enqueue = r8a66597_urb_enqueue,
2218 .urb_dequeue = r8a66597_urb_dequeue,
2219 .endpoint_disable = r8a66597_endpoint_disable,
2222 * periodic schedule support
2224 .get_frame_number = r8a66597_get_frame,
2227 * root hub support
2229 .hub_status_data = r8a66597_hub_status_data,
2230 .hub_control = r8a66597_hub_control,
2233 #if defined(CONFIG_PM)
2234 static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
2236 return 0;
2239 static int r8a66597_resume(struct platform_device *pdev)
2241 return 0;
2243 #else /* if defined(CONFIG_PM) */
2244 #define r8a66597_suspend NULL
2245 #define r8a66597_resume NULL
2246 #endif
2248 static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2250 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2251 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2253 del_timer_sync(&r8a66597->rh_timer);
2254 usb_remove_hcd(hcd);
2255 iounmap((void *)r8a66597->reg);
2256 usb_put_hcd(hcd);
2257 return 0;
2260 #define resource_len(r) (((r)->end - (r)->start) + 1)
2261 static int __init r8a66597_probe(struct platform_device *pdev)
2263 struct resource *res = NULL;
2264 int irq = -1;
2265 void __iomem *reg = NULL;
2266 struct usb_hcd *hcd = NULL;
2267 struct r8a66597 *r8a66597;
2268 int ret = 0;
2269 int i;
2270 unsigned long irq_trigger;
2272 if (pdev->dev.dma_mask) {
2273 ret = -EINVAL;
2274 err("dma not support");
2275 goto clean_up;
2278 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2279 (char *)hcd_name);
2280 if (!res) {
2281 ret = -ENODEV;
2282 err("platform_get_resource_byname error.");
2283 goto clean_up;
2286 irq = platform_get_irq(pdev, 0);
2287 if (irq < 0) {
2288 ret = -ENODEV;
2289 err("platform_get_irq error.");
2290 goto clean_up;
2293 reg = ioremap(res->start, resource_len(res));
2294 if (reg == NULL) {
2295 ret = -ENOMEM;
2296 err("ioremap error.");
2297 goto clean_up;
2300 /* initialize hcd */
2301 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2302 if (!hcd) {
2303 ret = -ENOMEM;
2304 err("Failed to create hcd");
2305 goto clean_up;
2307 r8a66597 = hcd_to_r8a66597(hcd);
2308 memset(r8a66597, 0, sizeof(struct r8a66597));
2309 dev_set_drvdata(&pdev->dev, r8a66597);
2311 spin_lock_init(&r8a66597->lock);
2312 init_timer(&r8a66597->rh_timer);
2313 r8a66597->rh_timer.function = r8a66597_timer;
2314 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2315 r8a66597->reg = (unsigned long)reg;
2317 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2318 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2319 init_timer(&r8a66597->td_timer[i]);
2320 r8a66597->td_timer[i].function = r8a66597_td_timer;
2321 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
2322 setup_timer(&r8a66597->interval_timer[i],
2323 r8a66597_interval_timer,
2324 (unsigned long)r8a66597);
2326 INIT_LIST_HEAD(&r8a66597->child_device);
2328 hcd->rsrc_start = res->start;
2329 if (irq_sense == INTL)
2330 irq_trigger = IRQF_TRIGGER_LOW;
2331 else
2332 irq_trigger = IRQF_TRIGGER_FALLING;
2333 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
2334 if (ret != 0) {
2335 err("Failed to add hcd");
2336 goto clean_up;
2339 return 0;
2341 clean_up:
2342 if (reg)
2343 iounmap(reg);
2345 return ret;
2348 static struct platform_driver r8a66597_driver = {
2349 .probe = r8a66597_probe,
2350 .remove = r8a66597_remove,
2351 .suspend = r8a66597_suspend,
2352 .resume = r8a66597_resume,
2353 .driver = {
2354 .name = (char *) hcd_name,
2355 .owner = THIS_MODULE,
2359 static int __init r8a66597_init(void)
2361 if (usb_disabled())
2362 return -ENODEV;
2364 info("driver %s, %s", hcd_name, DRIVER_VERSION);
2365 return platform_driver_register(&r8a66597_driver);
2367 module_init(r8a66597_init);
2369 static void __exit r8a66597_cleanup(void)
2371 platform_driver_unregister(&r8a66597_driver);
2373 module_exit(r8a66597_cleanup);