const: constify remaining dev_pm_ops
[linux-2.6/linux-2.6-openrd.git] / drivers / usb / host / r8a66597-hcd.c
blobb7a661c02bcdb5d3edf0a84b39a50a41f2bf411c
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/errno.h>
30 #include <linux/init.h>
31 #include <linux/timer.h>
32 #include <linux/delay.h>
33 #include <linux/list.h>
34 #include <linux/interrupt.h>
35 #include <linux/usb.h>
36 #include <linux/platform_device.h>
37 #include <linux/io.h>
38 #include <linux/irq.h>
40 #include "../core/hcd.h"
41 #include "r8a66597.h"
43 MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Yoshihiro Shimoda");
46 MODULE_ALIAS("platform:r8a66597_hcd");
48 #define DRIVER_VERSION "2009-05-26"
50 static const char hcd_name[] = "r8a66597_hcd";
52 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
53 static int r8a66597_get_frame(struct usb_hcd *hcd);
55 /* this function must be called with interrupt disabled */
56 static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
57 unsigned long reg)
59 u16 tmp;
61 tmp = r8a66597_read(r8a66597, INTENB0);
62 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
63 r8a66597_bset(r8a66597, 1 << pipenum, reg);
64 r8a66597_write(r8a66597, tmp, INTENB0);
67 /* this function must be called with interrupt disabled */
68 static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
69 unsigned long reg)
71 u16 tmp;
73 tmp = r8a66597_read(r8a66597, INTENB0);
74 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
75 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
76 r8a66597_write(r8a66597, tmp, INTENB0);
79 static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
80 u16 usbspd, u8 upphub, u8 hubport, int port)
82 u16 val;
83 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
85 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
86 r8a66597_write(r8a66597, val, devadd_reg);
89 static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
91 u16 tmp;
92 int i = 0;
94 if (r8a66597->pdata->on_chip) {
95 #ifdef CONFIG_HAVE_CLK
96 clk_enable(r8a66597->clk);
97 #endif
98 do {
99 r8a66597_write(r8a66597, SCKE, SYSCFG0);
100 tmp = r8a66597_read(r8a66597, SYSCFG0);
101 if (i++ > 1000) {
102 printk(KERN_ERR "r8a66597: reg access fail.\n");
103 return -ENXIO;
105 } while ((tmp & SCKE) != SCKE);
106 r8a66597_write(r8a66597, 0x04, 0x02);
107 } else {
108 do {
109 r8a66597_write(r8a66597, USBE, SYSCFG0);
110 tmp = r8a66597_read(r8a66597, SYSCFG0);
111 if (i++ > 1000) {
112 printk(KERN_ERR "r8a66597: reg access fail.\n");
113 return -ENXIO;
115 } while ((tmp & USBE) != USBE);
116 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
117 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
118 XTAL, SYSCFG0);
120 i = 0;
121 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
122 do {
123 msleep(1);
124 tmp = r8a66597_read(r8a66597, SYSCFG0);
125 if (i++ > 500) {
126 printk(KERN_ERR "r8a66597: reg access fail.\n");
127 return -ENXIO;
129 } while ((tmp & SCKE) != SCKE);
132 return 0;
135 static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
137 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
138 udelay(1);
140 if (r8a66597->pdata->on_chip) {
141 #ifdef CONFIG_HAVE_CLK
142 clk_disable(r8a66597->clk);
143 #endif
144 } else {
145 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
146 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
147 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
151 static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
153 u16 val;
155 val = port ? DRPD : DCFM | DRPD;
156 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
157 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
159 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
160 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
161 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
164 static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
166 u16 val, tmp;
168 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
169 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
171 r8a66597_port_power(r8a66597, port, 0);
173 do {
174 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
175 udelay(640);
176 } while (tmp == EDGESTS);
178 val = port ? DRPD : DCFM | DRPD;
179 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
180 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
183 static int enable_controller(struct r8a66597 *r8a66597)
185 int ret, port;
186 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
187 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
188 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
190 ret = r8a66597_clock_enable(r8a66597);
191 if (ret < 0)
192 return ret;
194 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
195 r8a66597_bset(r8a66597, USBE, SYSCFG0);
197 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
198 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
199 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
200 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
202 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
203 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
204 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
205 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
207 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
209 for (port = 0; port < r8a66597->max_root_hub; port++)
210 r8a66597_enable_port(r8a66597, port);
212 return 0;
215 static void disable_controller(struct r8a66597 *r8a66597)
217 int port;
219 r8a66597_write(r8a66597, 0, INTENB0);
220 r8a66597_write(r8a66597, 0, INTSTS0);
222 for (port = 0; port < r8a66597->max_root_hub; port++)
223 r8a66597_disable_port(r8a66597, port);
225 r8a66597_clock_disable(r8a66597);
228 static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
229 struct usb_device *udev)
231 struct r8a66597_device *dev;
233 if (udev->parent && udev->parent->devnum != 1)
234 udev = udev->parent;
236 dev = dev_get_drvdata(&udev->dev);
237 if (dev)
238 return dev->address;
239 else
240 return 0;
243 static int is_child_device(char *devpath)
245 return (devpath[2] ? 1 : 0);
248 static int is_hub_limit(char *devpath)
250 return ((strlen(devpath) >= 4) ? 1 : 0);
253 static void get_port_number(struct r8a66597 *r8a66597,
254 char *devpath, u16 *root_port, u16 *hub_port)
256 if (root_port) {
257 *root_port = (devpath[0] & 0x0F) - 1;
258 if (*root_port >= r8a66597->max_root_hub)
259 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
261 if (hub_port)
262 *hub_port = devpath[2] & 0x0F;
265 static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
267 u16 usbspd = 0;
269 switch (speed) {
270 case USB_SPEED_LOW:
271 usbspd = LSMODE;
272 break;
273 case USB_SPEED_FULL:
274 usbspd = FSMODE;
275 break;
276 case USB_SPEED_HIGH:
277 usbspd = HSMODE;
278 break;
279 default:
280 printk(KERN_ERR "r8a66597: unknown speed\n");
281 break;
284 return usbspd;
287 static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
289 int idx;
291 idx = address / 32;
292 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
295 static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
297 int idx;
299 idx = address / 32;
300 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
303 static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
305 u16 pipenum = pipe->info.pipenum;
306 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
307 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
308 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
310 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
311 dma_ch = R8A66597_PIPE_NO_DMA;
313 pipe->fifoaddr = fifoaddr[dma_ch];
314 pipe->fifosel = fifosel[dma_ch];
315 pipe->fifoctr = fifoctr[dma_ch];
317 if (pipenum == 0)
318 pipe->pipectr = DCPCTR;
319 else
320 pipe->pipectr = get_pipectr_addr(pipenum);
322 if (check_bulk_or_isoc(pipenum)) {
323 pipe->pipetre = get_pipetre_addr(pipenum);
324 pipe->pipetrn = get_pipetrn_addr(pipenum);
325 } else {
326 pipe->pipetre = 0;
327 pipe->pipetrn = 0;
331 static struct r8a66597_device *
332 get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
334 if (usb_pipedevice(urb->pipe) == 0)
335 return &r8a66597->device0;
337 return dev_get_drvdata(&urb->dev->dev);
340 static int make_r8a66597_device(struct r8a66597 *r8a66597,
341 struct urb *urb, u8 addr)
343 struct r8a66597_device *dev;
344 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
346 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
347 if (dev == NULL)
348 return -ENOMEM;
350 dev_set_drvdata(&urb->dev->dev, dev);
351 dev->udev = urb->dev;
352 dev->address = addr;
353 dev->usb_address = usb_address;
354 dev->state = USB_STATE_ADDRESS;
355 dev->ep_in_toggle = 0;
356 dev->ep_out_toggle = 0;
357 INIT_LIST_HEAD(&dev->device_list);
358 list_add_tail(&dev->device_list, &r8a66597->child_device);
360 get_port_number(r8a66597, urb->dev->devpath,
361 &dev->root_port, &dev->hub_port);
362 if (!is_child_device(urb->dev->devpath))
363 r8a66597->root_hub[dev->root_port].dev = dev;
365 set_devadd_reg(r8a66597, dev->address,
366 get_r8a66597_usb_speed(urb->dev->speed),
367 get_parent_r8a66597_address(r8a66597, urb->dev),
368 dev->hub_port, dev->root_port);
370 return 0;
373 /* this function must be called with interrupt disabled */
374 static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
376 u8 addr; /* R8A66597's address */
377 struct r8a66597_device *dev;
379 if (is_hub_limit(urb->dev->devpath)) {
380 dev_err(&urb->dev->dev, "External hub limit reached.\n");
381 return 0;
384 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
385 if (dev && dev->state >= USB_STATE_ADDRESS)
386 return dev->address;
388 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
389 if (r8a66597->address_map & (1 << addr))
390 continue;
392 dbg("alloc_address: r8a66597_addr=%d", addr);
393 r8a66597->address_map |= 1 << addr;
395 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
396 return 0;
398 return addr;
401 dev_err(&urb->dev->dev,
402 "cannot communicate with a USB device more than 10.(%x)\n",
403 r8a66597->address_map);
405 return 0;
408 /* this function must be called with interrupt disabled */
409 static void free_usb_address(struct r8a66597 *r8a66597,
410 struct r8a66597_device *dev)
412 int port;
414 if (!dev)
415 return;
417 dbg("free_addr: addr=%d", dev->address);
419 dev->state = USB_STATE_DEFAULT;
420 r8a66597->address_map &= ~(1 << dev->address);
421 dev->address = 0;
422 dev_set_drvdata(&dev->udev->dev, NULL);
423 list_del(&dev->device_list);
424 kfree(dev);
426 for (port = 0; port < r8a66597->max_root_hub; port++) {
427 if (r8a66597->root_hub[port].dev == dev) {
428 r8a66597->root_hub[port].dev = NULL;
429 break;
434 static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
435 u16 mask, u16 loop)
437 u16 tmp;
438 int i = 0;
440 do {
441 tmp = r8a66597_read(r8a66597, reg);
442 if (i++ > 1000000) {
443 printk(KERN_ERR "r8a66597: register%lx, loop %x "
444 "is timeout\n", reg, loop);
445 break;
447 ndelay(1);
448 } while ((tmp & mask) != loop);
451 /* this function must be called with interrupt disabled */
452 static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
454 u16 tmp;
456 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
457 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
458 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
459 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
462 /* this function must be called with interrupt disabled */
463 static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
465 u16 tmp;
467 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
468 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
469 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
470 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
471 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
474 /* this function must be called with interrupt disabled */
475 static void clear_all_buffer(struct r8a66597 *r8a66597,
476 struct r8a66597_pipe *pipe)
478 u16 tmp;
480 if (!pipe || pipe->info.pipenum == 0)
481 return;
483 pipe_stop(r8a66597, pipe);
484 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
485 tmp = r8a66597_read(r8a66597, pipe->pipectr);
486 tmp = r8a66597_read(r8a66597, pipe->pipectr);
487 tmp = r8a66597_read(r8a66597, pipe->pipectr);
488 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
491 /* this function must be called with interrupt disabled */
492 static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
493 struct r8a66597_pipe *pipe, int toggle)
495 if (toggle)
496 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
497 else
498 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
501 static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
503 if (r8a66597->pdata->on_chip)
504 return MBW_32;
505 else
506 return MBW_16;
509 /* this function must be called with interrupt disabled */
510 static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
512 unsigned short mbw = mbw_value(r8a66597);
514 r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
515 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
518 /* this function must be called with interrupt disabled */
519 static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
520 struct r8a66597_pipe *pipe)
522 unsigned short mbw = mbw_value(r8a66597);
524 cfifo_change(r8a66597, 0);
525 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
526 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
528 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
529 pipe->fifosel);
530 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
533 static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
535 struct r8a66597_pipe *pipe = hep->hcpriv;
537 if (usb_pipeendpoint(urb->pipe) == 0)
538 return 0;
539 else
540 return pipe->info.pipenum;
543 static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
545 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
547 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
550 static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
551 int urb_pipe)
553 if (!dev)
554 return NULL;
556 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
559 /* this function must be called with interrupt disabled */
560 static void pipe_toggle_set(struct r8a66597 *r8a66597,
561 struct r8a66597_pipe *pipe,
562 struct urb *urb, int set)
564 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
565 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
566 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
568 if (!toggle)
569 return;
571 if (set)
572 *toggle |= 1 << endpoint;
573 else
574 *toggle &= ~(1 << endpoint);
577 /* this function must be called with interrupt disabled */
578 static void pipe_toggle_save(struct r8a66597 *r8a66597,
579 struct r8a66597_pipe *pipe,
580 struct urb *urb)
582 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
583 pipe_toggle_set(r8a66597, pipe, urb, 1);
584 else
585 pipe_toggle_set(r8a66597, pipe, urb, 0);
588 /* this function must be called with interrupt disabled */
589 static void pipe_toggle_restore(struct r8a66597 *r8a66597,
590 struct r8a66597_pipe *pipe,
591 struct urb *urb)
593 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
594 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
595 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
597 if (!toggle)
598 return;
600 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
603 /* this function must be called with interrupt disabled */
604 static void pipe_buffer_setting(struct r8a66597 *r8a66597,
605 struct r8a66597_pipe_info *info)
607 u16 val = 0;
609 if (info->pipenum == 0)
610 return;
612 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
613 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
614 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
615 if (!info->dir_in)
616 val |= R8A66597_DIR;
617 if (info->type == R8A66597_BULK && info->dir_in)
618 val |= R8A66597_DBLB | R8A66597_SHTNAK;
619 val |= info->type | info->epnum;
620 r8a66597_write(r8a66597, val, PIPECFG);
622 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
623 PIPEBUF);
624 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
625 PIPEMAXP);
626 r8a66597_write(r8a66597, info->interval, PIPEPERI);
629 /* this function must be called with interrupt disabled */
630 static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
632 struct r8a66597_pipe_info *info;
633 struct urb *urb = td->urb;
635 if (td->pipenum > 0) {
636 info = &td->pipe->info;
637 cfifo_change(r8a66597, 0);
638 pipe_buffer_setting(r8a66597, info);
640 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
641 usb_pipeout(urb->pipe)) &&
642 !usb_pipecontrol(urb->pipe)) {
643 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
644 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
645 clear_all_buffer(r8a66597, td->pipe);
646 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
647 usb_pipeout(urb->pipe), 1);
649 pipe_toggle_restore(r8a66597, td->pipe, urb);
653 /* this function must be called with interrupt disabled */
654 static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
655 struct usb_endpoint_descriptor *ep)
657 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
659 memset(array, 0, sizeof(array));
660 switch (usb_endpoint_type(ep)) {
661 case USB_ENDPOINT_XFER_BULK:
662 if (usb_endpoint_dir_in(ep))
663 array[i++] = 4;
664 else {
665 array[i++] = 3;
666 array[i++] = 5;
668 break;
669 case USB_ENDPOINT_XFER_INT:
670 if (usb_endpoint_dir_in(ep)) {
671 array[i++] = 6;
672 array[i++] = 7;
673 array[i++] = 8;
674 } else
675 array[i++] = 9;
676 break;
677 case USB_ENDPOINT_XFER_ISOC:
678 if (usb_endpoint_dir_in(ep))
679 array[i++] = 2;
680 else
681 array[i++] = 1;
682 break;
683 default:
684 printk(KERN_ERR "r8a66597: Illegal type\n");
685 return 0;
688 i = 1;
689 min = array[0];
690 while (array[i] != 0) {
691 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
692 min = array[i];
693 i++;
696 return min;
699 static u16 get_r8a66597_type(__u8 type)
701 u16 r8a66597_type;
703 switch (type) {
704 case USB_ENDPOINT_XFER_BULK:
705 r8a66597_type = R8A66597_BULK;
706 break;
707 case USB_ENDPOINT_XFER_INT:
708 r8a66597_type = R8A66597_INT;
709 break;
710 case USB_ENDPOINT_XFER_ISOC:
711 r8a66597_type = R8A66597_ISO;
712 break;
713 default:
714 printk(KERN_ERR "r8a66597: Illegal type\n");
715 r8a66597_type = 0x0000;
716 break;
719 return r8a66597_type;
722 static u16 get_bufnum(u16 pipenum)
724 u16 bufnum = 0;
726 if (pipenum == 0)
727 bufnum = 0;
728 else if (check_bulk_or_isoc(pipenum))
729 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
730 else if (check_interrupt(pipenum))
731 bufnum = 4 + (pipenum - 6);
732 else
733 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
735 return bufnum;
738 static u16 get_buf_bsize(u16 pipenum)
740 u16 buf_bsize = 0;
742 if (pipenum == 0)
743 buf_bsize = 3;
744 else if (check_bulk_or_isoc(pipenum))
745 buf_bsize = R8A66597_BUF_BSIZE - 1;
746 else if (check_interrupt(pipenum))
747 buf_bsize = 0;
748 else
749 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
751 return buf_bsize;
754 /* this function must be called with interrupt disabled */
755 static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
756 struct r8a66597_device *dev,
757 struct r8a66597_pipe *pipe,
758 struct urb *urb)
760 int i;
761 struct r8a66597_pipe_info *info = &pipe->info;
762 unsigned short mbw = mbw_value(r8a66597);
764 /* pipe dma is only for external controlles */
765 if (r8a66597->pdata->on_chip)
766 return;
768 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
769 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
770 if ((r8a66597->dma_map & (1 << i)) != 0)
771 continue;
773 dev_info(&dev->udev->dev,
774 "address %d, EndpointAddress 0x%02x use "
775 "DMA FIFO\n", usb_pipedevice(urb->pipe),
776 info->dir_in ?
777 USB_ENDPOINT_DIR_MASK + info->epnum
778 : info->epnum);
780 r8a66597->dma_map |= 1 << i;
781 dev->dma_map |= 1 << i;
782 set_pipe_reg_addr(pipe, i);
784 cfifo_change(r8a66597, 0);
785 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
786 mbw | CURPIPE, pipe->fifosel);
788 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
789 pipe->info.pipenum);
790 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
791 break;
796 /* this function must be called with interrupt disabled */
797 static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
798 struct usb_host_endpoint *hep,
799 struct r8a66597_pipe_info *info)
801 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
802 struct r8a66597_pipe *pipe = hep->hcpriv;
804 dbg("enable_pipe:");
806 pipe->info = *info;
807 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
808 r8a66597->pipe_cnt[pipe->info.pipenum]++;
809 dev->pipe_cnt[pipe->info.pipenum]++;
811 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
814 /* this function must be called with interrupt disabled */
815 static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
817 struct r8a66597_td *td, *next;
818 struct urb *urb;
819 struct list_head *list = &r8a66597->pipe_queue[pipenum];
821 if (list_empty(list))
822 return;
824 list_for_each_entry_safe(td, next, list, queue) {
825 if (td->address != address)
826 continue;
828 urb = td->urb;
829 list_del(&td->queue);
830 kfree(td);
832 if (urb) {
833 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
834 urb);
836 spin_unlock(&r8a66597->lock);
837 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
838 -ENODEV);
839 spin_lock(&r8a66597->lock);
841 break;
845 /* this function must be called with interrupt disabled */
846 static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
847 struct r8a66597_device *dev)
849 int check_ep0 = 0;
850 u16 pipenum;
852 if (!dev)
853 return;
855 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
856 if (!dev->pipe_cnt[pipenum])
857 continue;
859 if (!check_ep0) {
860 check_ep0 = 1;
861 force_dequeue(r8a66597, 0, dev->address);
864 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
865 dev->pipe_cnt[pipenum] = 0;
866 force_dequeue(r8a66597, pipenum, dev->address);
869 dbg("disable_pipe");
871 r8a66597->dma_map &= ~(dev->dma_map);
872 dev->dma_map = 0;
875 static u16 get_interval(struct urb *urb, __u8 interval)
877 u16 time = 1;
878 int i;
880 if (urb->dev->speed == USB_SPEED_HIGH) {
881 if (interval > IITV)
882 time = IITV;
883 else
884 time = interval ? interval - 1 : 0;
885 } else {
886 if (interval > 128) {
887 time = IITV;
888 } else {
889 /* calculate the nearest value for PIPEPERI */
890 for (i = 0; i < 7; i++) {
891 if ((1 << i) < interval &&
892 (1 << (i + 1) > interval))
893 time = 1 << i;
898 return time;
901 static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
903 __u8 i;
904 unsigned long time = 1;
906 if (usb_pipeisoc(urb->pipe))
907 return 0;
909 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
910 for (i = 0; i < (interval - 1); i++)
911 time *= 2;
912 time = time * 125 / 1000; /* uSOF -> msec */
913 } else {
914 time = interval;
917 return time;
920 /* this function must be called with interrupt disabled */
921 static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
922 struct usb_host_endpoint *hep,
923 struct usb_endpoint_descriptor *ep)
925 struct r8a66597_pipe_info info;
927 info.pipenum = get_empty_pipenum(r8a66597, ep);
928 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
929 info.epnum = usb_endpoint_num(ep);
930 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
931 info.type = get_r8a66597_type(usb_endpoint_type(ep));
932 info.bufnum = get_bufnum(info.pipenum);
933 info.buf_bsize = get_buf_bsize(info.pipenum);
934 if (info.type == R8A66597_BULK) {
935 info.interval = 0;
936 info.timer_interval = 0;
937 } else {
938 info.interval = get_interval(urb, ep->bInterval);
939 info.timer_interval = get_timer_interval(urb, ep->bInterval);
941 if (usb_endpoint_dir_in(ep))
942 info.dir_in = 1;
943 else
944 info.dir_in = 0;
946 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
949 static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
951 struct r8a66597_device *dev;
953 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
954 dev->state = USB_STATE_CONFIGURED;
957 static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
958 u16 pipenum)
960 if (pipenum == 0 && usb_pipeout(urb->pipe))
961 enable_irq_empty(r8a66597, pipenum);
962 else
963 enable_irq_ready(r8a66597, pipenum);
965 if (!usb_pipeisoc(urb->pipe))
966 enable_irq_nrdy(r8a66597, pipenum);
969 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
971 disable_irq_ready(r8a66597, pipenum);
972 disable_irq_nrdy(r8a66597, pipenum);
975 static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
977 mod_timer(&r8a66597->rh_timer,
978 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
981 static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
982 int connect)
984 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
986 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
987 rh->scount = R8A66597_MAX_SAMPLING;
988 if (connect)
989 rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
990 else
991 rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
992 rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
994 r8a66597_root_hub_start_polling(r8a66597);
997 /* this function must be called with interrupt disabled */
998 static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
999 u16 syssts)
1001 if (syssts == SE0) {
1002 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1003 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1004 } else {
1005 if (syssts == FS_JSTS)
1006 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1007 else if (syssts == LS_JSTS)
1008 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1010 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
1011 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1013 if (r8a66597->bus_suspended)
1014 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1017 usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
1020 /* this function must be called with interrupt disabled */
1021 static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1023 u16 speed = get_rh_usb_speed(r8a66597, port);
1024 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1026 rh->port &= ~((1 << USB_PORT_FEAT_HIGHSPEED) |
1027 (1 << USB_PORT_FEAT_LOWSPEED));
1028 if (speed == HSMODE)
1029 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
1030 else if (speed == LSMODE)
1031 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
1033 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
1034 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1037 /* this function must be called with interrupt disabled */
1038 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1040 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1042 disable_r8a66597_pipe_all(r8a66597, dev);
1043 free_usb_address(r8a66597, dev);
1045 start_root_hub_sampling(r8a66597, port, 0);
1048 /* this function must be called with interrupt disabled */
1049 static void prepare_setup_packet(struct r8a66597 *r8a66597,
1050 struct r8a66597_td *td)
1052 int i;
1053 __le16 *p = (__le16 *)td->urb->setup_packet;
1054 unsigned long setup_addr = USBREQ;
1056 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1057 DCPMAXP);
1058 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
1060 for (i = 0; i < 4; i++) {
1061 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
1062 setup_addr += 2;
1064 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1067 /* this function must be called with interrupt disabled */
1068 static void prepare_packet_read(struct r8a66597 *r8a66597,
1069 struct r8a66597_td *td)
1071 struct urb *urb = td->urb;
1073 if (usb_pipecontrol(urb->pipe)) {
1074 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1075 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1076 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1077 if (urb->actual_length == 0) {
1078 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1079 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1081 pipe_irq_disable(r8a66597, td->pipenum);
1082 pipe_start(r8a66597, td->pipe);
1083 pipe_irq_enable(r8a66597, urb, td->pipenum);
1084 } else {
1085 if (urb->actual_length == 0) {
1086 pipe_irq_disable(r8a66597, td->pipenum);
1087 pipe_setting(r8a66597, td);
1088 pipe_stop(r8a66597, td->pipe);
1089 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1091 if (td->pipe->pipetre) {
1092 r8a66597_write(r8a66597, TRCLR,
1093 td->pipe->pipetre);
1094 r8a66597_write(r8a66597,
1095 DIV_ROUND_UP
1096 (urb->transfer_buffer_length,
1097 td->maxpacket),
1098 td->pipe->pipetrn);
1099 r8a66597_bset(r8a66597, TRENB,
1100 td->pipe->pipetre);
1103 pipe_start(r8a66597, td->pipe);
1104 pipe_irq_enable(r8a66597, urb, td->pipenum);
1109 /* this function must be called with interrupt disabled */
1110 static void prepare_packet_write(struct r8a66597 *r8a66597,
1111 struct r8a66597_td *td)
1113 u16 tmp;
1114 struct urb *urb = td->urb;
1116 if (usb_pipecontrol(urb->pipe)) {
1117 pipe_stop(r8a66597, td->pipe);
1118 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1119 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1120 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1121 if (urb->actual_length == 0) {
1122 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1123 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1125 } else {
1126 if (urb->actual_length == 0)
1127 pipe_setting(r8a66597, td);
1128 if (td->pipe->pipetre)
1129 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1131 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1133 fifo_change_from_pipe(r8a66597, td->pipe);
1134 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1135 if (unlikely((tmp & FRDY) == 0))
1136 pipe_irq_enable(r8a66597, urb, td->pipenum);
1137 else
1138 packet_write(r8a66597, td->pipenum);
1139 pipe_start(r8a66597, td->pipe);
1142 /* this function must be called with interrupt disabled */
1143 static void prepare_status_packet(struct r8a66597 *r8a66597,
1144 struct r8a66597_td *td)
1146 struct urb *urb = td->urb;
1148 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1149 pipe_stop(r8a66597, td->pipe);
1151 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1152 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1153 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1154 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1155 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
1156 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
1157 enable_irq_empty(r8a66597, 0);
1158 } else {
1159 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1160 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1161 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1162 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1163 enable_irq_ready(r8a66597, 0);
1165 enable_irq_nrdy(r8a66597, 0);
1166 pipe_start(r8a66597, td->pipe);
1169 static int is_set_address(unsigned char *setup_packet)
1171 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1172 setup_packet[1] == USB_REQ_SET_ADDRESS)
1173 return 1;
1174 else
1175 return 0;
1178 /* this function must be called with interrupt disabled */
1179 static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1181 BUG_ON(!td);
1183 switch (td->type) {
1184 case USB_PID_SETUP:
1185 if (is_set_address(td->urb->setup_packet)) {
1186 td->set_address = 1;
1187 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1188 td->urb);
1189 if (td->urb->setup_packet[2] == 0)
1190 return -EPIPE;
1192 prepare_setup_packet(r8a66597, td);
1193 break;
1194 case USB_PID_IN:
1195 prepare_packet_read(r8a66597, td);
1196 break;
1197 case USB_PID_OUT:
1198 prepare_packet_write(r8a66597, td);
1199 break;
1200 case USB_PID_ACK:
1201 prepare_status_packet(r8a66597, td);
1202 break;
1203 default:
1204 printk(KERN_ERR "r8a66597: invalid type.\n");
1205 break;
1208 return 0;
1211 static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1213 if (usb_pipeisoc(urb->pipe)) {
1214 if (urb->number_of_packets == td->iso_cnt)
1215 return 1;
1218 /* control or bulk or interrupt */
1219 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1220 (td->short_packet) || (td->zero_packet))
1221 return 1;
1223 return 0;
1226 /* this function must be called with interrupt disabled */
1227 static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1229 unsigned long time;
1231 BUG_ON(!td);
1233 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1234 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1235 r8a66597->timeout_map |= 1 << td->pipenum;
1236 switch (usb_pipetype(td->urb->pipe)) {
1237 case PIPE_INTERRUPT:
1238 case PIPE_ISOCHRONOUS:
1239 time = 30;
1240 break;
1241 default:
1242 time = 300;
1243 break;
1246 mod_timer(&r8a66597->td_timer[td->pipenum],
1247 jiffies + msecs_to_jiffies(time));
1251 /* this function must be called with interrupt disabled */
1252 static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1253 u16 pipenum, struct urb *urb, int status)
1254 __releases(r8a66597->lock) __acquires(r8a66597->lock)
1256 int restart = 0;
1257 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1259 r8a66597->timeout_map &= ~(1 << pipenum);
1261 if (likely(td)) {
1262 if (td->set_address && (status != 0 || urb->unlinked))
1263 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1265 pipe_toggle_save(r8a66597, td->pipe, urb);
1266 list_del(&td->queue);
1267 kfree(td);
1270 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1271 restart = 1;
1273 if (likely(urb)) {
1274 if (usb_pipeisoc(urb->pipe))
1275 urb->start_frame = r8a66597_get_frame(hcd);
1277 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
1278 spin_unlock(&r8a66597->lock);
1279 usb_hcd_giveback_urb(hcd, urb, status);
1280 spin_lock(&r8a66597->lock);
1283 if (restart) {
1284 td = r8a66597_get_td(r8a66597, pipenum);
1285 if (unlikely(!td))
1286 return;
1288 start_transfer(r8a66597, td);
1289 set_td_timer(r8a66597, td);
1293 static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1295 u16 tmp;
1296 int rcv_len, bufsize, urb_len, size;
1297 u16 *buf;
1298 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1299 struct urb *urb;
1300 int finish = 0;
1301 int status = 0;
1303 if (unlikely(!td))
1304 return;
1305 urb = td->urb;
1307 fifo_change_from_pipe(r8a66597, td->pipe);
1308 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1309 if (unlikely((tmp & FRDY) == 0)) {
1310 pipe_stop(r8a66597, td->pipe);
1311 pipe_irq_disable(r8a66597, pipenum);
1312 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
1313 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
1314 return;
1317 /* prepare parameters */
1318 rcv_len = tmp & DTLN;
1319 if (usb_pipeisoc(urb->pipe)) {
1320 buf = (u16 *)(urb->transfer_buffer +
1321 urb->iso_frame_desc[td->iso_cnt].offset);
1322 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1323 } else {
1324 buf = (void *)urb->transfer_buffer + urb->actual_length;
1325 urb_len = urb->transfer_buffer_length - urb->actual_length;
1327 bufsize = min(urb_len, (int) td->maxpacket);
1328 if (rcv_len <= bufsize) {
1329 size = rcv_len;
1330 } else {
1331 size = bufsize;
1332 status = -EOVERFLOW;
1333 finish = 1;
1336 /* update parameters */
1337 urb->actual_length += size;
1338 if (rcv_len == 0)
1339 td->zero_packet = 1;
1340 if (rcv_len < bufsize) {
1341 td->short_packet = 1;
1343 if (usb_pipeisoc(urb->pipe)) {
1344 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1345 urb->iso_frame_desc[td->iso_cnt].status = status;
1346 td->iso_cnt++;
1347 finish = 0;
1350 /* check transfer finish */
1351 if (finish || check_transfer_finish(td, urb)) {
1352 pipe_stop(r8a66597, td->pipe);
1353 pipe_irq_disable(r8a66597, pipenum);
1354 finish = 1;
1357 /* read fifo */
1358 if (urb->transfer_buffer) {
1359 if (size == 0)
1360 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1361 else
1362 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1363 buf, size);
1366 if (finish && pipenum != 0)
1367 finish_request(r8a66597, td, pipenum, urb, status);
1370 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1372 u16 tmp;
1373 int bufsize, size;
1374 u16 *buf;
1375 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1376 struct urb *urb;
1378 if (unlikely(!td))
1379 return;
1380 urb = td->urb;
1382 fifo_change_from_pipe(r8a66597, td->pipe);
1383 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1384 if (unlikely((tmp & FRDY) == 0)) {
1385 pipe_stop(r8a66597, td->pipe);
1386 pipe_irq_disable(r8a66597, pipenum);
1387 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
1388 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
1389 return;
1392 /* prepare parameters */
1393 bufsize = td->maxpacket;
1394 if (usb_pipeisoc(urb->pipe)) {
1395 buf = (u16 *)(urb->transfer_buffer +
1396 urb->iso_frame_desc[td->iso_cnt].offset);
1397 size = min(bufsize,
1398 (int)urb->iso_frame_desc[td->iso_cnt].length);
1399 } else {
1400 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1401 size = min_t(u32, bufsize,
1402 urb->transfer_buffer_length - urb->actual_length);
1405 /* write fifo */
1406 if (pipenum > 0)
1407 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
1408 if (urb->transfer_buffer) {
1409 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1410 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1411 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1414 /* update parameters */
1415 urb->actual_length += size;
1416 if (usb_pipeisoc(urb->pipe)) {
1417 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1418 urb->iso_frame_desc[td->iso_cnt].status = 0;
1419 td->iso_cnt++;
1422 /* check transfer finish */
1423 if (check_transfer_finish(td, urb)) {
1424 disable_irq_ready(r8a66597, pipenum);
1425 enable_irq_empty(r8a66597, pipenum);
1426 if (!usb_pipeisoc(urb->pipe))
1427 enable_irq_nrdy(r8a66597, pipenum);
1428 } else
1429 pipe_irq_enable(r8a66597, urb, pipenum);
1433 static void check_next_phase(struct r8a66597 *r8a66597, int status)
1435 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1436 struct urb *urb;
1437 u8 finish = 0;
1439 if (unlikely(!td))
1440 return;
1441 urb = td->urb;
1443 switch (td->type) {
1444 case USB_PID_IN:
1445 case USB_PID_OUT:
1446 if (check_transfer_finish(td, urb))
1447 td->type = USB_PID_ACK;
1448 break;
1449 case USB_PID_SETUP:
1450 if (urb->transfer_buffer_length == urb->actual_length)
1451 td->type = USB_PID_ACK;
1452 else if (usb_pipeout(urb->pipe))
1453 td->type = USB_PID_OUT;
1454 else
1455 td->type = USB_PID_IN;
1456 break;
1457 case USB_PID_ACK:
1458 finish = 1;
1459 break;
1462 if (finish || status != 0 || urb->unlinked)
1463 finish_request(r8a66597, td, 0, urb, status);
1464 else
1465 start_transfer(r8a66597, td);
1468 static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
1470 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1472 if (td) {
1473 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1475 if (pid == PID_NAK)
1476 return -ECONNRESET;
1477 else
1478 return -EPIPE;
1480 return 0;
1483 static void irq_pipe_ready(struct r8a66597 *r8a66597)
1485 u16 check;
1486 u16 pipenum;
1487 u16 mask;
1488 struct r8a66597_td *td;
1490 mask = r8a66597_read(r8a66597, BRDYSTS)
1491 & r8a66597_read(r8a66597, BRDYENB);
1492 r8a66597_write(r8a66597, ~mask, BRDYSTS);
1493 if (mask & BRDY0) {
1494 td = r8a66597_get_td(r8a66597, 0);
1495 if (td && td->type == USB_PID_IN)
1496 packet_read(r8a66597, 0);
1497 else
1498 pipe_irq_disable(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 td = r8a66597_get_td(r8a66597, pipenum);
1506 if (unlikely(!td))
1507 continue;
1509 if (td->type == USB_PID_IN)
1510 packet_read(r8a66597, pipenum);
1511 else if (td->type == USB_PID_OUT)
1512 packet_write(r8a66597, pipenum);
1517 static void irq_pipe_empty(struct r8a66597 *r8a66597)
1519 u16 tmp;
1520 u16 check;
1521 u16 pipenum;
1522 u16 mask;
1523 struct r8a66597_td *td;
1525 mask = r8a66597_read(r8a66597, BEMPSTS)
1526 & r8a66597_read(r8a66597, BEMPENB);
1527 r8a66597_write(r8a66597, ~mask, BEMPSTS);
1528 if (mask & BEMP0) {
1529 cfifo_change(r8a66597, 0);
1530 td = r8a66597_get_td(r8a66597, 0);
1531 if (td && td->type != USB_PID_OUT)
1532 disable_irq_empty(r8a66597, 0);
1533 check_next_phase(r8a66597, 0);
1536 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1537 check = 1 << pipenum;
1538 if (mask & check) {
1539 struct r8a66597_td *td;
1540 td = r8a66597_get_td(r8a66597, pipenum);
1541 if (unlikely(!td))
1542 continue;
1544 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1545 if ((tmp & INBUFM) == 0) {
1546 disable_irq_empty(r8a66597, pipenum);
1547 pipe_irq_disable(r8a66597, pipenum);
1548 finish_request(r8a66597, td, pipenum, td->urb,
1555 static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1557 u16 check;
1558 u16 pipenum;
1559 u16 mask;
1560 int status;
1562 mask = r8a66597_read(r8a66597, NRDYSTS)
1563 & r8a66597_read(r8a66597, NRDYENB);
1564 r8a66597_write(r8a66597, ~mask, NRDYSTS);
1565 if (mask & NRDY0) {
1566 cfifo_change(r8a66597, 0);
1567 status = get_urb_error(r8a66597, 0);
1568 pipe_irq_disable(r8a66597, 0);
1569 check_next_phase(r8a66597, status);
1572 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1573 check = 1 << pipenum;
1574 if (mask & check) {
1575 struct r8a66597_td *td;
1576 td = r8a66597_get_td(r8a66597, pipenum);
1577 if (unlikely(!td))
1578 continue;
1580 status = get_urb_error(r8a66597, pipenum);
1581 pipe_irq_disable(r8a66597, pipenum);
1582 pipe_stop(r8a66597, td->pipe);
1583 finish_request(r8a66597, td, pipenum, td->urb, status);
1588 static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1590 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1591 u16 intsts0, intsts1, intsts2;
1592 u16 intenb0, intenb1, intenb2;
1593 u16 mask0, mask1, mask2;
1594 int status;
1596 spin_lock(&r8a66597->lock);
1598 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1599 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1600 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1601 intenb0 = r8a66597_read(r8a66597, INTENB0);
1602 intenb1 = r8a66597_read(r8a66597, INTENB1);
1603 intenb2 = r8a66597_read(r8a66597, INTENB2);
1605 mask2 = intsts2 & intenb2;
1606 mask1 = intsts1 & intenb1;
1607 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1608 if (mask2) {
1609 if (mask2 & ATTCH) {
1610 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
1611 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1613 /* start usb bus sampling */
1614 start_root_hub_sampling(r8a66597, 1, 1);
1616 if (mask2 & DTCH) {
1617 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
1618 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1619 r8a66597_usb_disconnect(r8a66597, 1);
1621 if (mask2 & BCHG) {
1622 r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1623 r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1624 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1628 if (mask1) {
1629 if (mask1 & ATTCH) {
1630 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
1631 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1633 /* start usb bus sampling */
1634 start_root_hub_sampling(r8a66597, 0, 1);
1636 if (mask1 & DTCH) {
1637 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
1638 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1639 r8a66597_usb_disconnect(r8a66597, 0);
1641 if (mask1 & BCHG) {
1642 r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1643 r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1644 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1647 if (mask1 & SIGN) {
1648 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
1649 status = get_urb_error(r8a66597, 0);
1650 check_next_phase(r8a66597, status);
1652 if (mask1 & SACK) {
1653 r8a66597_write(r8a66597, ~SACK, INTSTS1);
1654 check_next_phase(r8a66597, 0);
1657 if (mask0) {
1658 if (mask0 & BRDY)
1659 irq_pipe_ready(r8a66597);
1660 if (mask0 & BEMP)
1661 irq_pipe_empty(r8a66597);
1662 if (mask0 & NRDY)
1663 irq_pipe_nrdy(r8a66597);
1666 spin_unlock(&r8a66597->lock);
1667 return IRQ_HANDLED;
1670 /* this function must be called with interrupt disabled */
1671 static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1673 u16 tmp;
1674 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1676 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1677 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1679 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1680 if ((tmp & USBRST) == USBRST) {
1681 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1682 dvstctr_reg);
1683 r8a66597_root_hub_start_polling(r8a66597);
1684 } else
1685 r8a66597_usb_connect(r8a66597, port);
1688 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1689 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1690 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1693 if (rh->scount > 0) {
1694 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1695 if (tmp == rh->old_syssts) {
1696 rh->scount--;
1697 if (rh->scount == 0)
1698 r8a66597_check_syssts(r8a66597, port, tmp);
1699 else
1700 r8a66597_root_hub_start_polling(r8a66597);
1701 } else {
1702 rh->scount = R8A66597_MAX_SAMPLING;
1703 rh->old_syssts = tmp;
1704 r8a66597_root_hub_start_polling(r8a66597);
1709 static void r8a66597_interval_timer(unsigned long _r8a66597)
1711 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1712 unsigned long flags;
1713 u16 pipenum;
1714 struct r8a66597_td *td;
1716 spin_lock_irqsave(&r8a66597->lock, flags);
1718 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1719 if (!(r8a66597->interval_map & (1 << pipenum)))
1720 continue;
1721 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1722 continue;
1724 td = r8a66597_get_td(r8a66597, pipenum);
1725 if (td)
1726 start_transfer(r8a66597, td);
1729 spin_unlock_irqrestore(&r8a66597->lock, flags);
1732 static void r8a66597_td_timer(unsigned long _r8a66597)
1734 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1735 unsigned long flags;
1736 u16 pipenum;
1737 struct r8a66597_td *td, *new_td = NULL;
1738 struct r8a66597_pipe *pipe;
1740 spin_lock_irqsave(&r8a66597->lock, flags);
1741 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1742 if (!(r8a66597->timeout_map & (1 << pipenum)))
1743 continue;
1744 if (timer_pending(&r8a66597->td_timer[pipenum]))
1745 continue;
1747 td = r8a66597_get_td(r8a66597, pipenum);
1748 if (!td) {
1749 r8a66597->timeout_map &= ~(1 << pipenum);
1750 continue;
1753 if (td->urb->actual_length) {
1754 set_td_timer(r8a66597, td);
1755 break;
1758 pipe = td->pipe;
1759 pipe_stop(r8a66597, pipe);
1761 new_td = td;
1762 do {
1763 list_move_tail(&new_td->queue,
1764 &r8a66597->pipe_queue[pipenum]);
1765 new_td = r8a66597_get_td(r8a66597, pipenum);
1766 if (!new_td) {
1767 new_td = td;
1768 break;
1770 } while (td != new_td && td->address == new_td->address);
1772 start_transfer(r8a66597, new_td);
1774 if (td == new_td)
1775 r8a66597->timeout_map &= ~(1 << pipenum);
1776 else
1777 set_td_timer(r8a66597, new_td);
1778 break;
1780 spin_unlock_irqrestore(&r8a66597->lock, flags);
1783 static void r8a66597_timer(unsigned long _r8a66597)
1785 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1786 unsigned long flags;
1787 int port;
1789 spin_lock_irqsave(&r8a66597->lock, flags);
1791 for (port = 0; port < r8a66597->max_root_hub; port++)
1792 r8a66597_root_hub_control(r8a66597, port);
1794 spin_unlock_irqrestore(&r8a66597->lock, flags);
1797 static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1799 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1801 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1802 (urb->dev->state == USB_STATE_CONFIGURED))
1803 return 1;
1804 else
1805 return 0;
1808 static int r8a66597_start(struct usb_hcd *hcd)
1810 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1812 hcd->state = HC_STATE_RUNNING;
1813 return enable_controller(r8a66597);
1816 static void r8a66597_stop(struct usb_hcd *hcd)
1818 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1820 disable_controller(r8a66597);
1823 static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1825 unsigned int usb_address = usb_pipedevice(urb->pipe);
1826 u16 root_port, hub_port;
1828 if (usb_address == 0) {
1829 get_port_number(r8a66597, urb->dev->devpath,
1830 &root_port, &hub_port);
1831 set_devadd_reg(r8a66597, 0,
1832 get_r8a66597_usb_speed(urb->dev->speed),
1833 get_parent_r8a66597_address(r8a66597, urb->dev),
1834 hub_port, root_port);
1838 static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1839 struct urb *urb,
1840 struct usb_host_endpoint *hep)
1842 struct r8a66597_td *td;
1843 u16 pipenum;
1845 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
1846 if (td == NULL)
1847 return NULL;
1849 pipenum = r8a66597_get_pipenum(urb, hep);
1850 td->pipenum = pipenum;
1851 td->pipe = hep->hcpriv;
1852 td->urb = urb;
1853 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1854 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1855 !usb_pipein(urb->pipe));
1856 if (usb_pipecontrol(urb->pipe))
1857 td->type = USB_PID_SETUP;
1858 else if (usb_pipein(urb->pipe))
1859 td->type = USB_PID_IN;
1860 else
1861 td->type = USB_PID_OUT;
1862 INIT_LIST_HEAD(&td->queue);
1864 return td;
1867 static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
1868 struct urb *urb,
1869 gfp_t mem_flags)
1871 struct usb_host_endpoint *hep = urb->ep;
1872 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1873 struct r8a66597_td *td = NULL;
1874 int ret, request = 0;
1875 unsigned long flags;
1877 spin_lock_irqsave(&r8a66597->lock, flags);
1878 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1879 ret = -ENODEV;
1880 goto error_not_linked;
1883 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1884 if (ret)
1885 goto error_not_linked;
1887 if (!hep->hcpriv) {
1888 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1889 GFP_ATOMIC);
1890 if (!hep->hcpriv) {
1891 ret = -ENOMEM;
1892 goto error;
1894 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1895 if (usb_pipeendpoint(urb->pipe))
1896 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1899 if (unlikely(check_pipe_config(r8a66597, urb)))
1900 init_pipe_config(r8a66597, urb);
1902 set_address_zero(r8a66597, urb);
1903 td = r8a66597_make_td(r8a66597, urb, hep);
1904 if (td == NULL) {
1905 ret = -ENOMEM;
1906 goto error;
1908 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1909 request = 1;
1910 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
1911 urb->hcpriv = td;
1913 if (request) {
1914 if (td->pipe->info.timer_interval) {
1915 r8a66597->interval_map |= 1 << td->pipenum;
1916 mod_timer(&r8a66597->interval_timer[td->pipenum],
1917 jiffies + msecs_to_jiffies(
1918 td->pipe->info.timer_interval));
1919 } else {
1920 ret = start_transfer(r8a66597, td);
1921 if (ret < 0) {
1922 list_del(&td->queue);
1923 kfree(td);
1926 } else
1927 set_td_timer(r8a66597, td);
1929 error:
1930 if (ret)
1931 usb_hcd_unlink_urb_from_ep(hcd, urb);
1932 error_not_linked:
1933 spin_unlock_irqrestore(&r8a66597->lock, flags);
1934 return ret;
1937 static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1938 int status)
1940 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1941 struct r8a66597_td *td;
1942 unsigned long flags;
1943 int rc;
1945 spin_lock_irqsave(&r8a66597->lock, flags);
1946 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1947 if (rc)
1948 goto done;
1950 if (urb->hcpriv) {
1951 td = urb->hcpriv;
1952 pipe_stop(r8a66597, td->pipe);
1953 pipe_irq_disable(r8a66597, td->pipenum);
1954 disable_irq_empty(r8a66597, td->pipenum);
1955 finish_request(r8a66597, td, td->pipenum, urb, status);
1957 done:
1958 spin_unlock_irqrestore(&r8a66597->lock, flags);
1959 return rc;
1962 static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1963 struct usb_host_endpoint *hep)
1965 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1966 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1967 struct r8a66597_td *td;
1968 struct urb *urb = NULL;
1969 u16 pipenum;
1970 unsigned long flags;
1972 if (pipe == NULL)
1973 return;
1974 pipenum = pipe->info.pipenum;
1976 if (pipenum == 0) {
1977 kfree(hep->hcpriv);
1978 hep->hcpriv = NULL;
1979 return;
1982 spin_lock_irqsave(&r8a66597->lock, flags);
1983 pipe_stop(r8a66597, pipe);
1984 pipe_irq_disable(r8a66597, pipenum);
1985 disable_irq_empty(r8a66597, pipenum);
1986 td = r8a66597_get_td(r8a66597, pipenum);
1987 if (td)
1988 urb = td->urb;
1989 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
1990 kfree(hep->hcpriv);
1991 hep->hcpriv = NULL;
1992 spin_unlock_irqrestore(&r8a66597->lock, flags);
1995 static int r8a66597_get_frame(struct usb_hcd *hcd)
1997 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1998 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
2001 static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
2003 int chix;
2005 if (udev->state == USB_STATE_CONFIGURED &&
2006 udev->parent && udev->parent->devnum > 1 &&
2007 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
2008 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
2010 for (chix = 0; chix < udev->maxchild; chix++) {
2011 struct usb_device *childdev = udev->children[chix];
2013 if (childdev)
2014 collect_usb_address_map(childdev, map);
2018 /* this function must be called with interrupt disabled */
2019 static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2020 int addr)
2022 struct r8a66597_device *dev;
2023 struct list_head *list = &r8a66597->child_device;
2025 list_for_each_entry(dev, list, device_list) {
2026 if (dev->usb_address != addr)
2027 continue;
2029 return dev;
2032 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
2033 return NULL;
2036 static void update_usb_address_map(struct r8a66597 *r8a66597,
2037 struct usb_device *root_hub,
2038 unsigned long *map)
2040 int i, j, addr;
2041 unsigned long diff;
2042 unsigned long flags;
2044 for (i = 0; i < 4; i++) {
2045 diff = r8a66597->child_connect_map[i] ^ map[i];
2046 if (!diff)
2047 continue;
2049 for (j = 0; j < 32; j++) {
2050 if (!(diff & (1 << j)))
2051 continue;
2053 addr = i * 32 + j;
2054 if (map[i] & (1 << j))
2055 set_child_connect_map(r8a66597, addr);
2056 else {
2057 struct r8a66597_device *dev;
2059 spin_lock_irqsave(&r8a66597->lock, flags);
2060 dev = get_r8a66597_device(r8a66597, addr);
2061 disable_r8a66597_pipe_all(r8a66597, dev);
2062 free_usb_address(r8a66597, dev);
2063 put_child_connect_map(r8a66597, addr);
2064 spin_unlock_irqrestore(&r8a66597->lock, flags);
2070 static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2071 struct usb_hcd *hcd)
2073 struct usb_bus *bus;
2074 unsigned long now_map[4];
2076 memset(now_map, 0, sizeof(now_map));
2078 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2079 if (!bus->root_hub)
2080 continue;
2082 if (bus->busnum != hcd->self.busnum)
2083 continue;
2085 collect_usb_address_map(bus->root_hub, now_map);
2086 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2090 static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2092 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2093 unsigned long flags;
2094 int i;
2096 r8a66597_check_detect_child(r8a66597, hcd);
2098 spin_lock_irqsave(&r8a66597->lock, flags);
2100 *buf = 0; /* initialize (no change) */
2102 for (i = 0; i < r8a66597->max_root_hub; i++) {
2103 if (r8a66597->root_hub[i].port & 0xffff0000)
2104 *buf |= 1 << (i + 1);
2107 spin_unlock_irqrestore(&r8a66597->lock, flags);
2109 return (*buf != 0);
2112 static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2113 struct usb_hub_descriptor *desc)
2115 desc->bDescriptorType = 0x29;
2116 desc->bHubContrCurrent = 0;
2117 desc->bNbrPorts = r8a66597->max_root_hub;
2118 desc->bDescLength = 9;
2119 desc->bPwrOn2PwrGood = 0;
2120 desc->wHubCharacteristics = cpu_to_le16(0x0011);
2121 desc->bitmap[0] = ((1 << r8a66597->max_root_hub) - 1) << 1;
2122 desc->bitmap[1] = ~0;
2125 static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2126 u16 wIndex, char *buf, u16 wLength)
2128 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2129 int ret;
2130 int port = (wIndex & 0x00FF) - 1;
2131 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2132 unsigned long flags;
2134 ret = 0;
2136 spin_lock_irqsave(&r8a66597->lock, flags);
2137 switch (typeReq) {
2138 case ClearHubFeature:
2139 case SetHubFeature:
2140 switch (wValue) {
2141 case C_HUB_OVER_CURRENT:
2142 case C_HUB_LOCAL_POWER:
2143 break;
2144 default:
2145 goto error;
2147 break;
2148 case ClearPortFeature:
2149 if (wIndex > r8a66597->max_root_hub)
2150 goto error;
2151 if (wLength != 0)
2152 goto error;
2154 switch (wValue) {
2155 case USB_PORT_FEAT_ENABLE:
2156 rh->port &= ~(1 << USB_PORT_FEAT_POWER);
2157 break;
2158 case USB_PORT_FEAT_SUSPEND:
2159 break;
2160 case USB_PORT_FEAT_POWER:
2161 r8a66597_port_power(r8a66597, port, 0);
2162 break;
2163 case USB_PORT_FEAT_C_ENABLE:
2164 case USB_PORT_FEAT_C_SUSPEND:
2165 case USB_PORT_FEAT_C_CONNECTION:
2166 case USB_PORT_FEAT_C_OVER_CURRENT:
2167 case USB_PORT_FEAT_C_RESET:
2168 break;
2169 default:
2170 goto error;
2172 rh->port &= ~(1 << wValue);
2173 break;
2174 case GetHubDescriptor:
2175 r8a66597_hub_descriptor(r8a66597,
2176 (struct usb_hub_descriptor *)buf);
2177 break;
2178 case GetHubStatus:
2179 *buf = 0x00;
2180 break;
2181 case GetPortStatus:
2182 if (wIndex > r8a66597->max_root_hub)
2183 goto error;
2184 *(__le32 *)buf = cpu_to_le32(rh->port);
2185 break;
2186 case SetPortFeature:
2187 if (wIndex > r8a66597->max_root_hub)
2188 goto error;
2189 if (wLength != 0)
2190 goto error;
2192 switch (wValue) {
2193 case USB_PORT_FEAT_SUSPEND:
2194 break;
2195 case USB_PORT_FEAT_POWER:
2196 r8a66597_port_power(r8a66597, port, 1);
2197 rh->port |= (1 << USB_PORT_FEAT_POWER);
2198 break;
2199 case USB_PORT_FEAT_RESET: {
2200 struct r8a66597_device *dev = rh->dev;
2202 rh->port |= (1 << USB_PORT_FEAT_RESET);
2204 disable_r8a66597_pipe_all(r8a66597, dev);
2205 free_usb_address(r8a66597, dev);
2207 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2208 get_dvstctr_reg(port));
2209 mod_timer(&r8a66597->rh_timer,
2210 jiffies + msecs_to_jiffies(50));
2212 break;
2213 default:
2214 goto error;
2216 rh->port |= 1 << wValue;
2217 break;
2218 default:
2219 error:
2220 ret = -EPIPE;
2221 break;
2224 spin_unlock_irqrestore(&r8a66597->lock, flags);
2225 return ret;
2228 #if defined(CONFIG_PM)
2229 static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2231 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2232 int port;
2234 dbg("%s", __func__);
2236 for (port = 0; port < r8a66597->max_root_hub; port++) {
2237 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2238 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2240 if (!(rh->port & (1 << USB_PORT_FEAT_ENABLE)))
2241 continue;
2243 dbg("suspend port = %d", port);
2244 r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
2245 rh->port |= 1 << USB_PORT_FEAT_SUSPEND;
2247 if (rh->dev->udev->do_remote_wakeup) {
2248 msleep(3); /* waiting last SOF */
2249 r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2250 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2251 r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2255 r8a66597->bus_suspended = 1;
2257 return 0;
2260 static int r8a66597_bus_resume(struct usb_hcd *hcd)
2262 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2263 int port;
2265 dbg("%s", __func__);
2267 for (port = 0; port < r8a66597->max_root_hub; port++) {
2268 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2269 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2271 if (!(rh->port & (1 << USB_PORT_FEAT_SUSPEND)))
2272 continue;
2274 dbg("resume port = %d", port);
2275 rh->port &= ~(1 << USB_PORT_FEAT_SUSPEND);
2276 rh->port |= 1 << USB_PORT_FEAT_C_SUSPEND;
2277 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2278 msleep(50);
2279 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2282 return 0;
2285 #else
2286 #define r8a66597_bus_suspend NULL
2287 #define r8a66597_bus_resume NULL
2288 #endif
2290 static struct hc_driver r8a66597_hc_driver = {
2291 .description = hcd_name,
2292 .hcd_priv_size = sizeof(struct r8a66597),
2293 .irq = r8a66597_irq,
2296 * generic hardware linkage
2298 .flags = HCD_USB2,
2300 .start = r8a66597_start,
2301 .stop = r8a66597_stop,
2304 * managing i/o requests and associated device resources
2306 .urb_enqueue = r8a66597_urb_enqueue,
2307 .urb_dequeue = r8a66597_urb_dequeue,
2308 .endpoint_disable = r8a66597_endpoint_disable,
2311 * periodic schedule support
2313 .get_frame_number = r8a66597_get_frame,
2316 * root hub support
2318 .hub_status_data = r8a66597_hub_status_data,
2319 .hub_control = r8a66597_hub_control,
2320 .bus_suspend = r8a66597_bus_suspend,
2321 .bus_resume = r8a66597_bus_resume,
2324 #if defined(CONFIG_PM)
2325 static int r8a66597_suspend(struct device *dev)
2327 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
2328 int port;
2330 dbg("%s", __func__);
2332 disable_controller(r8a66597);
2334 for (port = 0; port < r8a66597->max_root_hub; port++) {
2335 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2337 rh->port = 0x00000000;
2340 return 0;
2343 static int r8a66597_resume(struct device *dev)
2345 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
2346 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2348 dbg("%s", __func__);
2350 enable_controller(r8a66597);
2351 usb_root_hub_lost_power(hcd->self.root_hub);
2353 return 0;
2356 static const struct dev_pm_ops r8a66597_dev_pm_ops = {
2357 .suspend = r8a66597_suspend,
2358 .resume = r8a66597_resume,
2359 .poweroff = r8a66597_suspend,
2360 .restore = r8a66597_resume,
2363 #define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
2364 #else /* if defined(CONFIG_PM) */
2365 #define R8A66597_DEV_PM_OPS NULL
2366 #endif
2368 static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2370 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2371 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2373 del_timer_sync(&r8a66597->rh_timer);
2374 usb_remove_hcd(hcd);
2375 iounmap((void *)r8a66597->reg);
2376 #ifdef CONFIG_HAVE_CLK
2377 if (r8a66597->pdata->on_chip)
2378 clk_put(r8a66597->clk);
2379 #endif
2380 usb_put_hcd(hcd);
2381 return 0;
2384 static int __devinit r8a66597_probe(struct platform_device *pdev)
2386 #ifdef CONFIG_HAVE_CLK
2387 char clk_name[8];
2388 #endif
2389 struct resource *res = NULL, *ires;
2390 int irq = -1;
2391 void __iomem *reg = NULL;
2392 struct usb_hcd *hcd = NULL;
2393 struct r8a66597 *r8a66597;
2394 int ret = 0;
2395 int i;
2396 unsigned long irq_trigger;
2398 if (pdev->dev.dma_mask) {
2399 ret = -EINVAL;
2400 dev_err(&pdev->dev, "dma not supported\n");
2401 goto clean_up;
2404 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2405 if (!res) {
2406 ret = -ENODEV;
2407 dev_err(&pdev->dev, "platform_get_resource error.\n");
2408 goto clean_up;
2411 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2412 if (!ires) {
2413 ret = -ENODEV;
2414 dev_err(&pdev->dev,
2415 "platform_get_resource IORESOURCE_IRQ error.\n");
2416 goto clean_up;
2419 irq = ires->start;
2420 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2422 reg = ioremap(res->start, resource_size(res));
2423 if (reg == NULL) {
2424 ret = -ENOMEM;
2425 dev_err(&pdev->dev, "ioremap error.\n");
2426 goto clean_up;
2429 if (pdev->dev.platform_data == NULL) {
2430 dev_err(&pdev->dev, "no platform data\n");
2431 ret = -ENODEV;
2432 goto clean_up;
2435 /* initialize hcd */
2436 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2437 if (!hcd) {
2438 ret = -ENOMEM;
2439 dev_err(&pdev->dev, "Failed to create hcd\n");
2440 goto clean_up;
2442 r8a66597 = hcd_to_r8a66597(hcd);
2443 memset(r8a66597, 0, sizeof(struct r8a66597));
2444 dev_set_drvdata(&pdev->dev, r8a66597);
2445 r8a66597->pdata = pdev->dev.platform_data;
2446 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
2448 if (r8a66597->pdata->on_chip) {
2449 #ifdef CONFIG_HAVE_CLK
2450 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2451 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2452 if (IS_ERR(r8a66597->clk)) {
2453 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
2454 clk_name);
2455 ret = PTR_ERR(r8a66597->clk);
2456 goto clean_up2;
2458 #endif
2459 r8a66597->max_root_hub = 1;
2460 } else
2461 r8a66597->max_root_hub = 2;
2463 spin_lock_init(&r8a66597->lock);
2464 init_timer(&r8a66597->rh_timer);
2465 r8a66597->rh_timer.function = r8a66597_timer;
2466 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2467 r8a66597->reg = (unsigned long)reg;
2469 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2470 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2471 init_timer(&r8a66597->td_timer[i]);
2472 r8a66597->td_timer[i].function = r8a66597_td_timer;
2473 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
2474 setup_timer(&r8a66597->interval_timer[i],
2475 r8a66597_interval_timer,
2476 (unsigned long)r8a66597);
2478 INIT_LIST_HEAD(&r8a66597->child_device);
2480 hcd->rsrc_start = res->start;
2482 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
2483 if (ret != 0) {
2484 dev_err(&pdev->dev, "Failed to add hcd\n");
2485 goto clean_up3;
2488 return 0;
2490 clean_up3:
2491 #ifdef CONFIG_HAVE_CLK
2492 if (r8a66597->pdata->on_chip)
2493 clk_put(r8a66597->clk);
2494 clean_up2:
2495 #endif
2496 usb_put_hcd(hcd);
2498 clean_up:
2499 if (reg)
2500 iounmap(reg);
2502 return ret;
2505 static struct platform_driver r8a66597_driver = {
2506 .probe = r8a66597_probe,
2507 .remove = r8a66597_remove,
2508 .driver = {
2509 .name = (char *) hcd_name,
2510 .owner = THIS_MODULE,
2511 .pm = R8A66597_DEV_PM_OPS,
2515 static int __init r8a66597_init(void)
2517 if (usb_disabled())
2518 return -ENODEV;
2520 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2521 DRIVER_VERSION);
2522 return platform_driver_register(&r8a66597_driver);
2524 module_init(r8a66597_init);
2526 static void __exit r8a66597_cleanup(void)
2528 platform_driver_unregister(&r8a66597_driver);
2530 module_exit(r8a66597_cleanup);