ext4: Fix discard of inode prealloc space with delayed allocation.
[linux-2.6/mini2440.git] / drivers / usb / host / r8a66597-hcd.c
blob319041205b57c878b38b7b60b9ad1a9f3bf47dab
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 = 0xff;
70 module_param(irq_sense, ushort, 0644);
71 MODULE_PARM_DESC(irq_sense, "IRQ sense: low level=32, falling edge=0 "
72 "(default=32)");
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 #if defined(CONFIG_HAVE_CLK)
118 clk_enable(r8a66597->clk);
119 #endif
120 do {
121 r8a66597_write(r8a66597, SCKE, SYSCFG0);
122 tmp = r8a66597_read(r8a66597, SYSCFG0);
123 if (i++ > 1000) {
124 printk(KERN_ERR "r8a66597: register access fail.\n");
125 return -ENXIO;
127 } while ((tmp & SCKE) != SCKE);
128 r8a66597_write(r8a66597, 0x04, 0x02);
129 #else
130 do {
131 r8a66597_write(r8a66597, USBE, SYSCFG0);
132 tmp = r8a66597_read(r8a66597, SYSCFG0);
133 if (i++ > 1000) {
134 printk(KERN_ERR "r8a66597: register access fail.\n");
135 return -ENXIO;
137 } while ((tmp & USBE) != USBE);
138 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
139 r8a66597_mdfy(r8a66597, clock, XTAL, SYSCFG0);
141 i = 0;
142 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
143 do {
144 msleep(1);
145 tmp = r8a66597_read(r8a66597, SYSCFG0);
146 if (i++ > 500) {
147 printk(KERN_ERR "r8a66597: register access fail.\n");
148 return -ENXIO;
150 } while ((tmp & SCKE) != SCKE);
151 #endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
153 return 0;
156 static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
158 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
159 udelay(1);
160 #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
161 #if defined(CONFIG_HAVE_CLK)
162 clk_disable(r8a66597->clk);
163 #endif
164 #else
165 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
166 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
167 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
168 #endif
171 static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
173 u16 val;
175 val = port ? DRPD : DCFM | DRPD;
176 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
177 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
179 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
180 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
181 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
184 static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
186 u16 val, tmp;
188 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
189 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
191 r8a66597_port_power(r8a66597, port, 0);
193 do {
194 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
195 udelay(640);
196 } while (tmp == EDGESTS);
198 val = port ? DRPD : DCFM | DRPD;
199 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
200 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
203 static int enable_controller(struct r8a66597 *r8a66597)
205 int ret, port;
207 ret = r8a66597_clock_enable(r8a66597);
208 if (ret < 0)
209 return ret;
211 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
212 r8a66597_bset(r8a66597, USBE, SYSCFG0);
214 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
215 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
216 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
217 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
219 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
220 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
221 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
222 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
224 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
226 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
227 r8a66597_enable_port(r8a66597, port);
229 return 0;
232 static void disable_controller(struct r8a66597 *r8a66597)
234 int port;
236 r8a66597_write(r8a66597, 0, INTENB0);
237 r8a66597_write(r8a66597, 0, INTSTS0);
239 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
240 r8a66597_disable_port(r8a66597, port);
242 r8a66597_clock_disable(r8a66597);
245 static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
246 struct usb_device *udev)
248 struct r8a66597_device *dev;
250 if (udev->parent && udev->parent->devnum != 1)
251 udev = udev->parent;
253 dev = dev_get_drvdata(&udev->dev);
254 if (dev)
255 return dev->address;
256 else
257 return 0;
260 static int is_child_device(char *devpath)
262 return (devpath[2] ? 1 : 0);
265 static int is_hub_limit(char *devpath)
267 return ((strlen(devpath) >= 4) ? 1 : 0);
270 static void get_port_number(char *devpath, u16 *root_port, u16 *hub_port)
272 if (root_port) {
273 *root_port = (devpath[0] & 0x0F) - 1;
274 if (*root_port >= R8A66597_MAX_ROOT_HUB)
275 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
277 if (hub_port)
278 *hub_port = devpath[2] & 0x0F;
281 static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
283 u16 usbspd = 0;
285 switch (speed) {
286 case USB_SPEED_LOW:
287 usbspd = LSMODE;
288 break;
289 case USB_SPEED_FULL:
290 usbspd = FSMODE;
291 break;
292 case USB_SPEED_HIGH:
293 usbspd = HSMODE;
294 break;
295 default:
296 printk(KERN_ERR "r8a66597: unknown speed\n");
297 break;
300 return usbspd;
303 static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
305 int idx;
307 idx = address / 32;
308 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
311 static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
313 int idx;
315 idx = address / 32;
316 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
319 static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
321 u16 pipenum = pipe->info.pipenum;
322 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
323 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
324 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
326 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
327 dma_ch = R8A66597_PIPE_NO_DMA;
329 pipe->fifoaddr = fifoaddr[dma_ch];
330 pipe->fifosel = fifosel[dma_ch];
331 pipe->fifoctr = fifoctr[dma_ch];
333 if (pipenum == 0)
334 pipe->pipectr = DCPCTR;
335 else
336 pipe->pipectr = get_pipectr_addr(pipenum);
338 if (check_bulk_or_isoc(pipenum)) {
339 pipe->pipetre = get_pipetre_addr(pipenum);
340 pipe->pipetrn = get_pipetrn_addr(pipenum);
341 } else {
342 pipe->pipetre = 0;
343 pipe->pipetrn = 0;
347 static struct r8a66597_device *
348 get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
350 if (usb_pipedevice(urb->pipe) == 0)
351 return &r8a66597->device0;
353 return dev_get_drvdata(&urb->dev->dev);
356 static int make_r8a66597_device(struct r8a66597 *r8a66597,
357 struct urb *urb, u8 addr)
359 struct r8a66597_device *dev;
360 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
362 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
363 if (dev == NULL)
364 return -ENOMEM;
366 dev_set_drvdata(&urb->dev->dev, dev);
367 dev->udev = urb->dev;
368 dev->address = addr;
369 dev->usb_address = usb_address;
370 dev->state = USB_STATE_ADDRESS;
371 dev->ep_in_toggle = 0;
372 dev->ep_out_toggle = 0;
373 INIT_LIST_HEAD(&dev->device_list);
374 list_add_tail(&dev->device_list, &r8a66597->child_device);
376 get_port_number(urb->dev->devpath, &dev->root_port, &dev->hub_port);
377 if (!is_child_device(urb->dev->devpath))
378 r8a66597->root_hub[dev->root_port].dev = dev;
380 set_devadd_reg(r8a66597, dev->address,
381 get_r8a66597_usb_speed(urb->dev->speed),
382 get_parent_r8a66597_address(r8a66597, urb->dev),
383 dev->hub_port, dev->root_port);
385 return 0;
388 /* this function must be called with interrupt disabled */
389 static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
391 u8 addr; /* R8A66597's address */
392 struct r8a66597_device *dev;
394 if (is_hub_limit(urb->dev->devpath)) {
395 dev_err(&urb->dev->dev, "External hub limit reached.\n");
396 return 0;
399 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
400 if (dev && dev->state >= USB_STATE_ADDRESS)
401 return dev->address;
403 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
404 if (r8a66597->address_map & (1 << addr))
405 continue;
407 dbg("alloc_address: r8a66597_addr=%d", addr);
408 r8a66597->address_map |= 1 << addr;
410 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
411 return 0;
413 return addr;
416 dev_err(&urb->dev->dev,
417 "cannot communicate with a USB device more than 10.(%x)\n",
418 r8a66597->address_map);
420 return 0;
423 /* this function must be called with interrupt disabled */
424 static void free_usb_address(struct r8a66597 *r8a66597,
425 struct r8a66597_device *dev)
427 int port;
429 if (!dev)
430 return;
432 dbg("free_addr: addr=%d", dev->address);
434 dev->state = USB_STATE_DEFAULT;
435 r8a66597->address_map &= ~(1 << dev->address);
436 dev->address = 0;
437 dev_set_drvdata(&dev->udev->dev, NULL);
438 list_del(&dev->device_list);
439 kfree(dev);
441 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
442 if (r8a66597->root_hub[port].dev == dev) {
443 r8a66597->root_hub[port].dev = NULL;
444 break;
449 static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
450 u16 mask, u16 loop)
452 u16 tmp;
453 int i = 0;
455 do {
456 tmp = r8a66597_read(r8a66597, reg);
457 if (i++ > 1000000) {
458 printk(KERN_ERR "r8a66597: register%lx, loop %x "
459 "is timeout\n", reg, loop);
460 break;
462 ndelay(1);
463 } while ((tmp & mask) != loop);
466 /* this function must be called with interrupt disabled */
467 static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
469 u16 tmp;
471 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
472 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
473 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
474 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
477 /* this function must be called with interrupt disabled */
478 static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
480 u16 tmp;
482 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
483 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
484 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
485 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
486 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
489 /* this function must be called with interrupt disabled */
490 static void clear_all_buffer(struct r8a66597 *r8a66597,
491 struct r8a66597_pipe *pipe)
493 u16 tmp;
495 if (!pipe || pipe->info.pipenum == 0)
496 return;
498 pipe_stop(r8a66597, pipe);
499 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
500 tmp = r8a66597_read(r8a66597, pipe->pipectr);
501 tmp = r8a66597_read(r8a66597, pipe->pipectr);
502 tmp = r8a66597_read(r8a66597, pipe->pipectr);
503 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
506 /* this function must be called with interrupt disabled */
507 static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
508 struct r8a66597_pipe *pipe, int toggle)
510 if (toggle)
511 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
512 else
513 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
516 /* this function must be called with interrupt disabled */
517 static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
519 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
520 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
523 /* this function must be called with interrupt disabled */
524 static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
525 struct r8a66597_pipe *pipe)
527 cfifo_change(r8a66597, 0);
528 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D0FIFOSEL);
529 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D1FIFOSEL);
531 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum, MBW | CURPIPE,
532 pipe->fifosel);
533 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
536 static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
538 struct r8a66597_pipe *pipe = hep->hcpriv;
540 if (usb_pipeendpoint(urb->pipe) == 0)
541 return 0;
542 else
543 return pipe->info.pipenum;
546 static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
548 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
550 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
553 static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
554 int urb_pipe)
556 if (!dev)
557 return NULL;
559 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
562 /* this function must be called with interrupt disabled */
563 static void pipe_toggle_set(struct r8a66597 *r8a66597,
564 struct r8a66597_pipe *pipe,
565 struct urb *urb, int set)
567 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
568 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
569 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
571 if (!toggle)
572 return;
574 if (set)
575 *toggle |= 1 << endpoint;
576 else
577 *toggle &= ~(1 << endpoint);
580 /* this function must be called with interrupt disabled */
581 static void pipe_toggle_save(struct r8a66597 *r8a66597,
582 struct r8a66597_pipe *pipe,
583 struct urb *urb)
585 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
586 pipe_toggle_set(r8a66597, pipe, urb, 1);
587 else
588 pipe_toggle_set(r8a66597, pipe, urb, 0);
591 /* this function must be called with interrupt disabled */
592 static void pipe_toggle_restore(struct r8a66597 *r8a66597,
593 struct r8a66597_pipe *pipe,
594 struct urb *urb)
596 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
597 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
598 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
600 if (!toggle)
601 return;
603 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
606 /* this function must be called with interrupt disabled */
607 static void pipe_buffer_setting(struct r8a66597 *r8a66597,
608 struct r8a66597_pipe_info *info)
610 u16 val = 0;
612 if (info->pipenum == 0)
613 return;
615 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
616 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
617 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
618 if (!info->dir_in)
619 val |= R8A66597_DIR;
620 if (info->type == R8A66597_BULK && info->dir_in)
621 val |= R8A66597_DBLB | R8A66597_SHTNAK;
622 val |= info->type | info->epnum;
623 r8a66597_write(r8a66597, val, PIPECFG);
625 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
626 PIPEBUF);
627 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
628 PIPEMAXP);
629 r8a66597_write(r8a66597, info->interval, PIPEPERI);
632 /* this function must be called with interrupt disabled */
633 static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
635 struct r8a66597_pipe_info *info;
636 struct urb *urb = td->urb;
638 if (td->pipenum > 0) {
639 info = &td->pipe->info;
640 cfifo_change(r8a66597, 0);
641 pipe_buffer_setting(r8a66597, info);
643 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
644 usb_pipeout(urb->pipe)) &&
645 !usb_pipecontrol(urb->pipe)) {
646 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
647 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
648 clear_all_buffer(r8a66597, td->pipe);
649 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
650 usb_pipeout(urb->pipe), 1);
652 pipe_toggle_restore(r8a66597, td->pipe, urb);
656 /* this function must be called with interrupt disabled */
657 static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
658 struct usb_endpoint_descriptor *ep)
660 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
662 memset(array, 0, sizeof(array));
663 switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
664 case USB_ENDPOINT_XFER_BULK:
665 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
666 array[i++] = 4;
667 else {
668 array[i++] = 3;
669 array[i++] = 5;
671 break;
672 case USB_ENDPOINT_XFER_INT:
673 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
674 array[i++] = 6;
675 array[i++] = 7;
676 array[i++] = 8;
677 } else
678 array[i++] = 9;
679 break;
680 case USB_ENDPOINT_XFER_ISOC:
681 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
682 array[i++] = 2;
683 else
684 array[i++] = 1;
685 break;
686 default:
687 printk(KERN_ERR "r8a66597: Illegal type\n");
688 return 0;
691 i = 1;
692 min = array[0];
693 while (array[i] != 0) {
694 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
695 min = array[i];
696 i++;
699 return min;
702 static u16 get_r8a66597_type(__u8 type)
704 u16 r8a66597_type;
706 switch (type) {
707 case USB_ENDPOINT_XFER_BULK:
708 r8a66597_type = R8A66597_BULK;
709 break;
710 case USB_ENDPOINT_XFER_INT:
711 r8a66597_type = R8A66597_INT;
712 break;
713 case USB_ENDPOINT_XFER_ISOC:
714 r8a66597_type = R8A66597_ISO;
715 break;
716 default:
717 printk(KERN_ERR "r8a66597: Illegal type\n");
718 r8a66597_type = 0x0000;
719 break;
722 return r8a66597_type;
725 static u16 get_bufnum(u16 pipenum)
727 u16 bufnum = 0;
729 if (pipenum == 0)
730 bufnum = 0;
731 else if (check_bulk_or_isoc(pipenum))
732 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
733 else if (check_interrupt(pipenum))
734 bufnum = 4 + (pipenum - 6);
735 else
736 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
738 return bufnum;
741 static u16 get_buf_bsize(u16 pipenum)
743 u16 buf_bsize = 0;
745 if (pipenum == 0)
746 buf_bsize = 3;
747 else if (check_bulk_or_isoc(pipenum))
748 buf_bsize = R8A66597_BUF_BSIZE - 1;
749 else if (check_interrupt(pipenum))
750 buf_bsize = 0;
751 else
752 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
754 return buf_bsize;
757 /* this function must be called with interrupt disabled */
758 static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
759 struct r8a66597_device *dev,
760 struct r8a66597_pipe *pipe,
761 struct urb *urb)
763 #if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
764 int i;
765 struct r8a66597_pipe_info *info = &pipe->info;
767 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
768 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
769 if ((r8a66597->dma_map & (1 << i)) != 0)
770 continue;
772 dev_info(&dev->udev->dev,
773 "address %d, EndpointAddress 0x%02x use "
774 "DMA FIFO\n", usb_pipedevice(urb->pipe),
775 info->dir_in ?
776 USB_ENDPOINT_DIR_MASK + info->epnum
777 : info->epnum);
779 r8a66597->dma_map |= 1 << i;
780 dev->dma_map |= 1 << i;
781 set_pipe_reg_addr(pipe, i);
783 cfifo_change(r8a66597, 0);
784 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum,
785 MBW | CURPIPE, pipe->fifosel);
787 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
788 pipe->info.pipenum);
789 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
790 break;
793 #endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
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)
826 continue;
827 if (td->address != address)
828 continue;
830 urb = td->urb;
831 list_del(&td->queue);
832 kfree(td);
834 if (urb) {
835 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
836 urb);
838 spin_unlock(&r8a66597->lock);
839 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
840 -ENODEV);
841 spin_lock(&r8a66597->lock);
843 break;
847 /* this function must be called with interrupt disabled */
848 static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
849 struct r8a66597_device *dev)
851 int check_ep0 = 0;
852 u16 pipenum;
854 if (!dev)
855 return;
857 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
858 if (!dev->pipe_cnt[pipenum])
859 continue;
861 if (!check_ep0) {
862 check_ep0 = 1;
863 force_dequeue(r8a66597, 0, dev->address);
866 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
867 dev->pipe_cnt[pipenum] = 0;
868 force_dequeue(r8a66597, pipenum, dev->address);
871 dbg("disable_pipe");
873 r8a66597->dma_map &= ~(dev->dma_map);
874 dev->dma_map = 0;
877 static u16 get_interval(struct urb *urb, __u8 interval)
879 u16 time = 1;
880 int i;
882 if (urb->dev->speed == USB_SPEED_HIGH) {
883 if (interval > IITV)
884 time = IITV;
885 else
886 time = interval ? interval - 1 : 0;
887 } else {
888 if (interval > 128) {
889 time = IITV;
890 } else {
891 /* calculate the nearest value for PIPEPERI */
892 for (i = 0; i < 7; i++) {
893 if ((1 << i) < interval &&
894 (1 << (i + 1) > interval))
895 time = 1 << i;
900 return time;
903 static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
905 __u8 i;
906 unsigned long time = 1;
908 if (usb_pipeisoc(urb->pipe))
909 return 0;
911 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
912 for (i = 0; i < (interval - 1); i++)
913 time *= 2;
914 time = time * 125 / 1000; /* uSOF -> msec */
915 } else {
916 time = interval;
919 return time;
922 /* this function must be called with interrupt disabled */
923 static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
924 struct usb_host_endpoint *hep,
925 struct usb_endpoint_descriptor *ep)
927 struct r8a66597_pipe_info info;
929 info.pipenum = get_empty_pipenum(r8a66597, ep);
930 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
931 info.epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
932 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
933 info.type = get_r8a66597_type(ep->bmAttributes
934 & USB_ENDPOINT_XFERTYPE_MASK);
935 info.bufnum = get_bufnum(info.pipenum);
936 info.buf_bsize = get_buf_bsize(info.pipenum);
937 if (info.type == R8A66597_BULK) {
938 info.interval = 0;
939 info.timer_interval = 0;
940 } else {
941 info.interval = get_interval(urb, ep->bInterval);
942 info.timer_interval = get_timer_interval(urb, ep->bInterval);
944 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
945 info.dir_in = 1;
946 else
947 info.dir_in = 0;
949 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
952 static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
954 struct r8a66597_device *dev;
956 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
957 dev->state = USB_STATE_CONFIGURED;
960 static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
961 u16 pipenum)
963 if (pipenum == 0 && usb_pipeout(urb->pipe))
964 enable_irq_empty(r8a66597, pipenum);
965 else
966 enable_irq_ready(r8a66597, pipenum);
968 if (!usb_pipeisoc(urb->pipe))
969 enable_irq_nrdy(r8a66597, pipenum);
972 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
974 disable_irq_ready(r8a66597, pipenum);
975 disable_irq_nrdy(r8a66597, pipenum);
978 static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
980 mod_timer(&r8a66597->rh_timer,
981 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
984 static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
985 int connect)
987 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
989 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
990 rh->scount = R8A66597_MAX_SAMPLING;
991 if (connect)
992 rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
993 else
994 rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
995 rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
997 r8a66597_root_hub_start_polling(r8a66597);
1000 /* this function must be called with interrupt disabled */
1001 static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
1002 u16 syssts)
1004 if (syssts == SE0) {
1005 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1006 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1007 return;
1010 if (syssts == FS_JSTS)
1011 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1012 else if (syssts == LS_JSTS)
1013 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1015 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
1016 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1019 /* this function must be called with interrupt disabled */
1020 static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1022 u16 speed = get_rh_usb_speed(r8a66597, port);
1023 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1025 if (speed == HSMODE)
1026 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
1027 else if (speed == LSMODE)
1028 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
1030 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
1031 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1034 /* this function must be called with interrupt disabled */
1035 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1037 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1039 disable_r8a66597_pipe_all(r8a66597, dev);
1040 free_usb_address(r8a66597, dev);
1042 start_root_hub_sampling(r8a66597, port, 0);
1045 /* this function must be called with interrupt disabled */
1046 static void prepare_setup_packet(struct r8a66597 *r8a66597,
1047 struct r8a66597_td *td)
1049 int i;
1050 __le16 *p = (__le16 *)td->urb->setup_packet;
1051 unsigned long setup_addr = USBREQ;
1053 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1054 DCPMAXP);
1055 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
1057 for (i = 0; i < 4; i++) {
1058 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
1059 setup_addr += 2;
1061 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1064 /* this function must be called with interrupt disabled */
1065 static void prepare_packet_read(struct r8a66597 *r8a66597,
1066 struct r8a66597_td *td)
1068 struct urb *urb = td->urb;
1070 if (usb_pipecontrol(urb->pipe)) {
1071 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1072 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1073 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1074 if (urb->actual_length == 0) {
1075 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1076 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1078 pipe_irq_disable(r8a66597, td->pipenum);
1079 pipe_start(r8a66597, td->pipe);
1080 pipe_irq_enable(r8a66597, urb, td->pipenum);
1081 } else {
1082 if (urb->actual_length == 0) {
1083 pipe_irq_disable(r8a66597, td->pipenum);
1084 pipe_setting(r8a66597, td);
1085 pipe_stop(r8a66597, td->pipe);
1086 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1088 if (td->pipe->pipetre) {
1089 r8a66597_write(r8a66597, TRCLR,
1090 td->pipe->pipetre);
1091 r8a66597_write(r8a66597,
1092 DIV_ROUND_UP
1093 (urb->transfer_buffer_length,
1094 td->maxpacket),
1095 td->pipe->pipetrn);
1096 r8a66597_bset(r8a66597, TRENB,
1097 td->pipe->pipetre);
1100 pipe_start(r8a66597, td->pipe);
1101 pipe_irq_enable(r8a66597, urb, td->pipenum);
1106 /* this function must be called with interrupt disabled */
1107 static void prepare_packet_write(struct r8a66597 *r8a66597,
1108 struct r8a66597_td *td)
1110 u16 tmp;
1111 struct urb *urb = td->urb;
1113 if (usb_pipecontrol(urb->pipe)) {
1114 pipe_stop(r8a66597, td->pipe);
1115 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1116 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1117 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1118 if (urb->actual_length == 0) {
1119 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1120 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1122 } else {
1123 if (urb->actual_length == 0)
1124 pipe_setting(r8a66597, td);
1125 if (td->pipe->pipetre)
1126 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1128 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
1130 fifo_change_from_pipe(r8a66597, td->pipe);
1131 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1132 if (unlikely((tmp & FRDY) == 0))
1133 pipe_irq_enable(r8a66597, urb, td->pipenum);
1134 else
1135 packet_write(r8a66597, td->pipenum);
1136 pipe_start(r8a66597, td->pipe);
1139 /* this function must be called with interrupt disabled */
1140 static void prepare_status_packet(struct r8a66597 *r8a66597,
1141 struct r8a66597_td *td)
1143 struct urb *urb = td->urb;
1145 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1146 pipe_stop(r8a66597, td->pipe);
1148 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1149 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1150 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1151 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1152 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
1153 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
1154 enable_irq_empty(r8a66597, 0);
1155 } else {
1156 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1157 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1158 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1159 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1160 enable_irq_ready(r8a66597, 0);
1162 enable_irq_nrdy(r8a66597, 0);
1163 pipe_start(r8a66597, td->pipe);
1166 static int is_set_address(unsigned char *setup_packet)
1168 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1169 setup_packet[1] == USB_REQ_SET_ADDRESS)
1170 return 1;
1171 else
1172 return 0;
1175 /* this function must be called with interrupt disabled */
1176 static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1178 BUG_ON(!td);
1180 switch (td->type) {
1181 case USB_PID_SETUP:
1182 if (is_set_address(td->urb->setup_packet)) {
1183 td->set_address = 1;
1184 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1185 td->urb);
1186 if (td->urb->setup_packet[2] == 0)
1187 return -EPIPE;
1189 prepare_setup_packet(r8a66597, td);
1190 break;
1191 case USB_PID_IN:
1192 prepare_packet_read(r8a66597, td);
1193 break;
1194 case USB_PID_OUT:
1195 prepare_packet_write(r8a66597, td);
1196 break;
1197 case USB_PID_ACK:
1198 prepare_status_packet(r8a66597, td);
1199 break;
1200 default:
1201 printk(KERN_ERR "r8a66597: invalid type.\n");
1202 break;
1205 return 0;
1208 static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1210 if (usb_pipeisoc(urb->pipe)) {
1211 if (urb->number_of_packets == td->iso_cnt)
1212 return 1;
1215 /* control or bulk or interrupt */
1216 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1217 (td->short_packet) || (td->zero_packet))
1218 return 1;
1220 return 0;
1223 /* this function must be called with interrupt disabled */
1224 static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1226 unsigned long time;
1228 BUG_ON(!td);
1230 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1231 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1232 r8a66597->timeout_map |= 1 << td->pipenum;
1233 switch (usb_pipetype(td->urb->pipe)) {
1234 case PIPE_INTERRUPT:
1235 case PIPE_ISOCHRONOUS:
1236 time = 30;
1237 break;
1238 default:
1239 time = 300;
1240 break;
1243 mod_timer(&r8a66597->td_timer[td->pipenum],
1244 jiffies + msecs_to_jiffies(time));
1248 /* this function must be called with interrupt disabled */
1249 static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
1250 u16 pipenum, struct urb *urb, int status)
1251 __releases(r8a66597->lock) __acquires(r8a66597->lock)
1253 int restart = 0;
1254 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1256 r8a66597->timeout_map &= ~(1 << pipenum);
1258 if (likely(td)) {
1259 if (td->set_address && (status != 0 || urb->unlinked))
1260 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1262 pipe_toggle_save(r8a66597, td->pipe, urb);
1263 list_del(&td->queue);
1264 kfree(td);
1267 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1268 restart = 1;
1270 if (likely(urb)) {
1271 if (usb_pipeisoc(urb->pipe))
1272 urb->start_frame = r8a66597_get_frame(hcd);
1274 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
1275 spin_unlock(&r8a66597->lock);
1276 usb_hcd_giveback_urb(hcd, urb, status);
1277 spin_lock(&r8a66597->lock);
1280 if (restart) {
1281 td = r8a66597_get_td(r8a66597, pipenum);
1282 if (unlikely(!td))
1283 return;
1285 start_transfer(r8a66597, td);
1286 set_td_timer(r8a66597, td);
1290 static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1292 u16 tmp;
1293 int rcv_len, bufsize, urb_len, size;
1294 u16 *buf;
1295 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1296 struct urb *urb;
1297 int finish = 0;
1298 int status = 0;
1300 if (unlikely(!td))
1301 return;
1302 urb = td->urb;
1304 fifo_change_from_pipe(r8a66597, td->pipe);
1305 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1306 if (unlikely((tmp & FRDY) == 0)) {
1307 pipe_stop(r8a66597, td->pipe);
1308 pipe_irq_disable(r8a66597, pipenum);
1309 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
1310 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
1311 return;
1314 /* prepare parameters */
1315 rcv_len = tmp & DTLN;
1316 if (usb_pipeisoc(urb->pipe)) {
1317 buf = (u16 *)(urb->transfer_buffer +
1318 urb->iso_frame_desc[td->iso_cnt].offset);
1319 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1320 } else {
1321 buf = (void *)urb->transfer_buffer + urb->actual_length;
1322 urb_len = urb->transfer_buffer_length - urb->actual_length;
1324 bufsize = min(urb_len, (int) td->maxpacket);
1325 if (rcv_len <= bufsize) {
1326 size = rcv_len;
1327 } else {
1328 size = bufsize;
1329 status = -EOVERFLOW;
1330 finish = 1;
1333 /* update parameters */
1334 urb->actual_length += size;
1335 if (rcv_len == 0)
1336 td->zero_packet = 1;
1337 if (rcv_len < bufsize) {
1338 td->short_packet = 1;
1340 if (usb_pipeisoc(urb->pipe)) {
1341 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1342 urb->iso_frame_desc[td->iso_cnt].status = status;
1343 td->iso_cnt++;
1344 finish = 0;
1347 /* check transfer finish */
1348 if (finish || check_transfer_finish(td, urb)) {
1349 pipe_stop(r8a66597, td->pipe);
1350 pipe_irq_disable(r8a66597, pipenum);
1351 finish = 1;
1354 /* read fifo */
1355 if (urb->transfer_buffer) {
1356 if (size == 0)
1357 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1358 else
1359 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1360 buf, size);
1363 if (finish && pipenum != 0)
1364 finish_request(r8a66597, td, pipenum, urb, status);
1367 static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1369 u16 tmp;
1370 int bufsize, size;
1371 u16 *buf;
1372 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1373 struct urb *urb;
1375 if (unlikely(!td))
1376 return;
1377 urb = td->urb;
1379 fifo_change_from_pipe(r8a66597, td->pipe);
1380 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1381 if (unlikely((tmp & FRDY) == 0)) {
1382 pipe_stop(r8a66597, td->pipe);
1383 pipe_irq_disable(r8a66597, pipenum);
1384 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
1385 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
1386 return;
1389 /* prepare parameters */
1390 bufsize = td->maxpacket;
1391 if (usb_pipeisoc(urb->pipe)) {
1392 buf = (u16 *)(urb->transfer_buffer +
1393 urb->iso_frame_desc[td->iso_cnt].offset);
1394 size = min(bufsize,
1395 (int)urb->iso_frame_desc[td->iso_cnt].length);
1396 } else {
1397 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1398 size = min((int)bufsize,
1399 urb->transfer_buffer_length - urb->actual_length);
1402 /* write fifo */
1403 if (pipenum > 0)
1404 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
1405 if (urb->transfer_buffer) {
1406 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1407 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1408 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1411 /* update parameters */
1412 urb->actual_length += size;
1413 if (usb_pipeisoc(urb->pipe)) {
1414 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1415 urb->iso_frame_desc[td->iso_cnt].status = 0;
1416 td->iso_cnt++;
1419 /* check transfer finish */
1420 if (check_transfer_finish(td, urb)) {
1421 disable_irq_ready(r8a66597, pipenum);
1422 enable_irq_empty(r8a66597, pipenum);
1423 if (!usb_pipeisoc(urb->pipe))
1424 enable_irq_nrdy(r8a66597, pipenum);
1425 } else
1426 pipe_irq_enable(r8a66597, urb, pipenum);
1430 static void check_next_phase(struct r8a66597 *r8a66597, int status)
1432 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1433 struct urb *urb;
1434 u8 finish = 0;
1436 if (unlikely(!td))
1437 return;
1438 urb = td->urb;
1440 switch (td->type) {
1441 case USB_PID_IN:
1442 case USB_PID_OUT:
1443 if (check_transfer_finish(td, urb))
1444 td->type = USB_PID_ACK;
1445 break;
1446 case USB_PID_SETUP:
1447 if (urb->transfer_buffer_length == urb->actual_length)
1448 td->type = USB_PID_ACK;
1449 else if (usb_pipeout(urb->pipe))
1450 td->type = USB_PID_OUT;
1451 else
1452 td->type = USB_PID_IN;
1453 break;
1454 case USB_PID_ACK:
1455 finish = 1;
1456 break;
1459 if (finish || status != 0 || urb->unlinked)
1460 finish_request(r8a66597, td, 0, urb, status);
1461 else
1462 start_transfer(r8a66597, td);
1465 static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
1467 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1469 if (td) {
1470 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1472 if (pid == PID_NAK)
1473 return -ECONNRESET;
1474 else
1475 return -EPIPE;
1477 return 0;
1480 static void irq_pipe_ready(struct r8a66597 *r8a66597)
1482 u16 check;
1483 u16 pipenum;
1484 u16 mask;
1485 struct r8a66597_td *td;
1487 mask = r8a66597_read(r8a66597, BRDYSTS)
1488 & r8a66597_read(r8a66597, BRDYENB);
1489 r8a66597_write(r8a66597, ~mask, BRDYSTS);
1490 if (mask & BRDY0) {
1491 td = r8a66597_get_td(r8a66597, 0);
1492 if (td && td->type == USB_PID_IN)
1493 packet_read(r8a66597, 0);
1494 else
1495 pipe_irq_disable(r8a66597, 0);
1496 check_next_phase(r8a66597, 0);
1499 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1500 check = 1 << pipenum;
1501 if (mask & check) {
1502 td = r8a66597_get_td(r8a66597, pipenum);
1503 if (unlikely(!td))
1504 continue;
1506 if (td->type == USB_PID_IN)
1507 packet_read(r8a66597, pipenum);
1508 else if (td->type == USB_PID_OUT)
1509 packet_write(r8a66597, pipenum);
1514 static void irq_pipe_empty(struct r8a66597 *r8a66597)
1516 u16 tmp;
1517 u16 check;
1518 u16 pipenum;
1519 u16 mask;
1520 struct r8a66597_td *td;
1522 mask = r8a66597_read(r8a66597, BEMPSTS)
1523 & r8a66597_read(r8a66597, BEMPENB);
1524 r8a66597_write(r8a66597, ~mask, BEMPSTS);
1525 if (mask & BEMP0) {
1526 cfifo_change(r8a66597, 0);
1527 td = r8a66597_get_td(r8a66597, 0);
1528 if (td && td->type != USB_PID_OUT)
1529 disable_irq_empty(r8a66597, 0);
1530 check_next_phase(r8a66597, 0);
1533 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1534 check = 1 << pipenum;
1535 if (mask & check) {
1536 struct r8a66597_td *td;
1537 td = r8a66597_get_td(r8a66597, pipenum);
1538 if (unlikely(!td))
1539 continue;
1541 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1542 if ((tmp & INBUFM) == 0) {
1543 disable_irq_empty(r8a66597, pipenum);
1544 pipe_irq_disable(r8a66597, pipenum);
1545 finish_request(r8a66597, td, pipenum, td->urb,
1552 static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1554 u16 check;
1555 u16 pipenum;
1556 u16 mask;
1557 int status;
1559 mask = r8a66597_read(r8a66597, NRDYSTS)
1560 & r8a66597_read(r8a66597, NRDYENB);
1561 r8a66597_write(r8a66597, ~mask, NRDYSTS);
1562 if (mask & NRDY0) {
1563 cfifo_change(r8a66597, 0);
1564 status = get_urb_error(r8a66597, 0);
1565 pipe_irq_disable(r8a66597, 0);
1566 check_next_phase(r8a66597, status);
1569 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1570 check = 1 << pipenum;
1571 if (mask & check) {
1572 struct r8a66597_td *td;
1573 td = r8a66597_get_td(r8a66597, pipenum);
1574 if (unlikely(!td))
1575 continue;
1577 status = get_urb_error(r8a66597, pipenum);
1578 pipe_irq_disable(r8a66597, pipenum);
1579 pipe_stop(r8a66597, td->pipe);
1580 finish_request(r8a66597, td, pipenum, td->urb, status);
1585 static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1587 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1588 u16 intsts0, intsts1, intsts2;
1589 u16 intenb0, intenb1, intenb2;
1590 u16 mask0, mask1, mask2;
1591 int status;
1593 spin_lock(&r8a66597->lock);
1595 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1596 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1597 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1598 intenb0 = r8a66597_read(r8a66597, INTENB0);
1599 intenb1 = r8a66597_read(r8a66597, INTENB1);
1600 intenb2 = r8a66597_read(r8a66597, INTENB2);
1602 mask2 = intsts2 & intenb2;
1603 mask1 = intsts1 & intenb1;
1604 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1605 if (mask2) {
1606 if (mask2 & ATTCH) {
1607 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
1608 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1610 /* start usb bus sampling */
1611 start_root_hub_sampling(r8a66597, 1, 1);
1613 if (mask2 & DTCH) {
1614 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
1615 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1616 r8a66597_usb_disconnect(r8a66597, 1);
1620 if (mask1) {
1621 if (mask1 & ATTCH) {
1622 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
1623 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1625 /* start usb bus sampling */
1626 start_root_hub_sampling(r8a66597, 0, 1);
1628 if (mask1 & DTCH) {
1629 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
1630 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1631 r8a66597_usb_disconnect(r8a66597, 0);
1633 if (mask1 & SIGN) {
1634 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
1635 status = get_urb_error(r8a66597, 0);
1636 check_next_phase(r8a66597, status);
1638 if (mask1 & SACK) {
1639 r8a66597_write(r8a66597, ~SACK, INTSTS1);
1640 check_next_phase(r8a66597, 0);
1643 if (mask0) {
1644 if (mask0 & BRDY)
1645 irq_pipe_ready(r8a66597);
1646 if (mask0 & BEMP)
1647 irq_pipe_empty(r8a66597);
1648 if (mask0 & NRDY)
1649 irq_pipe_nrdy(r8a66597);
1652 spin_unlock(&r8a66597->lock);
1653 return IRQ_HANDLED;
1656 /* this function must be called with interrupt disabled */
1657 static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1659 u16 tmp;
1660 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1662 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1663 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1665 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1666 if ((tmp & USBRST) == USBRST) {
1667 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1668 dvstctr_reg);
1669 r8a66597_root_hub_start_polling(r8a66597);
1670 } else
1671 r8a66597_usb_connect(r8a66597, port);
1674 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1675 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1676 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1679 if (rh->scount > 0) {
1680 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1681 if (tmp == rh->old_syssts) {
1682 rh->scount--;
1683 if (rh->scount == 0)
1684 r8a66597_check_syssts(r8a66597, port, tmp);
1685 else
1686 r8a66597_root_hub_start_polling(r8a66597);
1687 } else {
1688 rh->scount = R8A66597_MAX_SAMPLING;
1689 rh->old_syssts = tmp;
1690 r8a66597_root_hub_start_polling(r8a66597);
1695 static void r8a66597_interval_timer(unsigned long _r8a66597)
1697 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1698 unsigned long flags;
1699 u16 pipenum;
1700 struct r8a66597_td *td;
1702 spin_lock_irqsave(&r8a66597->lock, flags);
1704 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1705 if (!(r8a66597->interval_map & (1 << pipenum)))
1706 continue;
1707 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1708 continue;
1710 td = r8a66597_get_td(r8a66597, pipenum);
1711 if (td)
1712 start_transfer(r8a66597, td);
1715 spin_unlock_irqrestore(&r8a66597->lock, flags);
1718 static void r8a66597_td_timer(unsigned long _r8a66597)
1720 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1721 unsigned long flags;
1722 u16 pipenum;
1723 struct r8a66597_td *td, *new_td = NULL;
1724 struct r8a66597_pipe *pipe;
1726 spin_lock_irqsave(&r8a66597->lock, flags);
1727 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1728 if (!(r8a66597->timeout_map & (1 << pipenum)))
1729 continue;
1730 if (timer_pending(&r8a66597->td_timer[pipenum]))
1731 continue;
1733 td = r8a66597_get_td(r8a66597, pipenum);
1734 if (!td) {
1735 r8a66597->timeout_map &= ~(1 << pipenum);
1736 continue;
1739 if (td->urb->actual_length) {
1740 set_td_timer(r8a66597, td);
1741 break;
1744 pipe = td->pipe;
1745 pipe_stop(r8a66597, pipe);
1747 new_td = td;
1748 do {
1749 list_move_tail(&new_td->queue,
1750 &r8a66597->pipe_queue[pipenum]);
1751 new_td = r8a66597_get_td(r8a66597, pipenum);
1752 if (!new_td) {
1753 new_td = td;
1754 break;
1756 } while (td != new_td && td->address == new_td->address);
1758 start_transfer(r8a66597, new_td);
1760 if (td == new_td)
1761 r8a66597->timeout_map &= ~(1 << pipenum);
1762 else
1763 set_td_timer(r8a66597, new_td);
1764 break;
1766 spin_unlock_irqrestore(&r8a66597->lock, flags);
1769 static void r8a66597_timer(unsigned long _r8a66597)
1771 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1772 unsigned long flags;
1773 int port;
1775 spin_lock_irqsave(&r8a66597->lock, flags);
1777 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
1778 r8a66597_root_hub_control(r8a66597, port);
1780 spin_unlock_irqrestore(&r8a66597->lock, flags);
1783 static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1785 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1787 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1788 (urb->dev->state == USB_STATE_CONFIGURED))
1789 return 1;
1790 else
1791 return 0;
1794 static int r8a66597_start(struct usb_hcd *hcd)
1796 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1798 hcd->state = HC_STATE_RUNNING;
1799 return enable_controller(r8a66597);
1802 static void r8a66597_stop(struct usb_hcd *hcd)
1804 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1806 disable_controller(r8a66597);
1809 static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1811 unsigned int usb_address = usb_pipedevice(urb->pipe);
1812 u16 root_port, hub_port;
1814 if (usb_address == 0) {
1815 get_port_number(urb->dev->devpath,
1816 &root_port, &hub_port);
1817 set_devadd_reg(r8a66597, 0,
1818 get_r8a66597_usb_speed(urb->dev->speed),
1819 get_parent_r8a66597_address(r8a66597, urb->dev),
1820 hub_port, root_port);
1824 static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1825 struct urb *urb,
1826 struct usb_host_endpoint *hep)
1828 struct r8a66597_td *td;
1829 u16 pipenum;
1831 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
1832 if (td == NULL)
1833 return NULL;
1835 pipenum = r8a66597_get_pipenum(urb, hep);
1836 td->pipenum = pipenum;
1837 td->pipe = hep->hcpriv;
1838 td->urb = urb;
1839 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1840 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1841 !usb_pipein(urb->pipe));
1842 if (usb_pipecontrol(urb->pipe))
1843 td->type = USB_PID_SETUP;
1844 else if (usb_pipein(urb->pipe))
1845 td->type = USB_PID_IN;
1846 else
1847 td->type = USB_PID_OUT;
1848 INIT_LIST_HEAD(&td->queue);
1850 return td;
1853 static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
1854 struct urb *urb,
1855 gfp_t mem_flags)
1857 struct usb_host_endpoint *hep = urb->ep;
1858 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1859 struct r8a66597_td *td = NULL;
1860 int ret, request = 0;
1861 unsigned long flags;
1863 spin_lock_irqsave(&r8a66597->lock, flags);
1864 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1865 ret = -ENODEV;
1866 goto error_not_linked;
1869 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1870 if (ret)
1871 goto error_not_linked;
1873 if (!hep->hcpriv) {
1874 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1875 GFP_ATOMIC);
1876 if (!hep->hcpriv) {
1877 ret = -ENOMEM;
1878 goto error;
1880 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1881 if (usb_pipeendpoint(urb->pipe))
1882 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1885 if (unlikely(check_pipe_config(r8a66597, urb)))
1886 init_pipe_config(r8a66597, urb);
1888 set_address_zero(r8a66597, urb);
1889 td = r8a66597_make_td(r8a66597, urb, hep);
1890 if (td == NULL) {
1891 ret = -ENOMEM;
1892 goto error;
1894 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1895 request = 1;
1896 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
1897 urb->hcpriv = td;
1899 if (request) {
1900 if (td->pipe->info.timer_interval) {
1901 r8a66597->interval_map |= 1 << td->pipenum;
1902 mod_timer(&r8a66597->interval_timer[td->pipenum],
1903 jiffies + msecs_to_jiffies(
1904 td->pipe->info.timer_interval));
1905 } else {
1906 ret = start_transfer(r8a66597, td);
1907 if (ret < 0) {
1908 list_del(&td->queue);
1909 kfree(td);
1912 } else
1913 set_td_timer(r8a66597, td);
1915 error:
1916 if (ret)
1917 usb_hcd_unlink_urb_from_ep(hcd, urb);
1918 error_not_linked:
1919 spin_unlock_irqrestore(&r8a66597->lock, flags);
1920 return ret;
1923 static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1924 int status)
1926 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1927 struct r8a66597_td *td;
1928 unsigned long flags;
1929 int rc;
1931 spin_lock_irqsave(&r8a66597->lock, flags);
1932 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1933 if (rc)
1934 goto done;
1936 if (urb->hcpriv) {
1937 td = urb->hcpriv;
1938 pipe_stop(r8a66597, td->pipe);
1939 pipe_irq_disable(r8a66597, td->pipenum);
1940 disable_irq_empty(r8a66597, td->pipenum);
1941 finish_request(r8a66597, td, td->pipenum, urb, status);
1943 done:
1944 spin_unlock_irqrestore(&r8a66597->lock, flags);
1945 return rc;
1948 static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1949 struct usb_host_endpoint *hep)
1951 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1952 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1953 struct r8a66597_td *td;
1954 struct urb *urb = NULL;
1955 u16 pipenum;
1956 unsigned long flags;
1958 if (pipe == NULL)
1959 return;
1960 pipenum = pipe->info.pipenum;
1962 if (pipenum == 0) {
1963 kfree(hep->hcpriv);
1964 hep->hcpriv = NULL;
1965 return;
1968 spin_lock_irqsave(&r8a66597->lock, flags);
1969 pipe_stop(r8a66597, pipe);
1970 pipe_irq_disable(r8a66597, pipenum);
1971 disable_irq_empty(r8a66597, pipenum);
1972 td = r8a66597_get_td(r8a66597, pipenum);
1973 if (td)
1974 urb = td->urb;
1975 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
1976 kfree(hep->hcpriv);
1977 hep->hcpriv = NULL;
1978 spin_unlock_irqrestore(&r8a66597->lock, flags);
1981 static int r8a66597_get_frame(struct usb_hcd *hcd)
1983 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1984 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1987 static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
1989 int chix;
1991 if (udev->state == USB_STATE_CONFIGURED &&
1992 udev->parent && udev->parent->devnum > 1 &&
1993 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
1994 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
1996 for (chix = 0; chix < udev->maxchild; chix++) {
1997 struct usb_device *childdev = udev->children[chix];
1999 if (childdev)
2000 collect_usb_address_map(childdev, map);
2004 /* this function must be called with interrupt disabled */
2005 static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2006 int addr)
2008 struct r8a66597_device *dev;
2009 struct list_head *list = &r8a66597->child_device;
2011 list_for_each_entry(dev, list, device_list) {
2012 if (!dev)
2013 continue;
2014 if (dev->usb_address != addr)
2015 continue;
2017 return dev;
2020 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
2021 return NULL;
2024 static void update_usb_address_map(struct r8a66597 *r8a66597,
2025 struct usb_device *root_hub,
2026 unsigned long *map)
2028 int i, j, addr;
2029 unsigned long diff;
2030 unsigned long flags;
2032 for (i = 0; i < 4; i++) {
2033 diff = r8a66597->child_connect_map[i] ^ map[i];
2034 if (!diff)
2035 continue;
2037 for (j = 0; j < 32; j++) {
2038 if (!(diff & (1 << j)))
2039 continue;
2041 addr = i * 32 + j;
2042 if (map[i] & (1 << j))
2043 set_child_connect_map(r8a66597, addr);
2044 else {
2045 struct r8a66597_device *dev;
2047 spin_lock_irqsave(&r8a66597->lock, flags);
2048 dev = get_r8a66597_device(r8a66597, addr);
2049 disable_r8a66597_pipe_all(r8a66597, dev);
2050 free_usb_address(r8a66597, dev);
2051 put_child_connect_map(r8a66597, addr);
2052 spin_unlock_irqrestore(&r8a66597->lock, flags);
2058 static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2059 struct usb_hcd *hcd)
2061 struct usb_bus *bus;
2062 unsigned long now_map[4];
2064 memset(now_map, 0, sizeof(now_map));
2066 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2067 if (!bus->root_hub)
2068 continue;
2070 if (bus->busnum != hcd->self.busnum)
2071 continue;
2073 collect_usb_address_map(bus->root_hub, now_map);
2074 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2078 static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2080 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2081 unsigned long flags;
2082 int i;
2084 r8a66597_check_detect_child(r8a66597, hcd);
2086 spin_lock_irqsave(&r8a66597->lock, flags);
2088 *buf = 0; /* initialize (no change) */
2090 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
2091 if (r8a66597->root_hub[i].port & 0xffff0000)
2092 *buf |= 1 << (i + 1);
2095 spin_unlock_irqrestore(&r8a66597->lock, flags);
2097 return (*buf != 0);
2100 static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2101 struct usb_hub_descriptor *desc)
2103 desc->bDescriptorType = 0x29;
2104 desc->bHubContrCurrent = 0;
2105 desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
2106 desc->bDescLength = 9;
2107 desc->bPwrOn2PwrGood = 0;
2108 desc->wHubCharacteristics = cpu_to_le16(0x0011);
2109 desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
2110 desc->bitmap[1] = ~0;
2113 static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2114 u16 wIndex, char *buf, u16 wLength)
2116 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2117 int ret;
2118 int port = (wIndex & 0x00FF) - 1;
2119 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2120 unsigned long flags;
2122 ret = 0;
2124 spin_lock_irqsave(&r8a66597->lock, flags);
2125 switch (typeReq) {
2126 case ClearHubFeature:
2127 case SetHubFeature:
2128 switch (wValue) {
2129 case C_HUB_OVER_CURRENT:
2130 case C_HUB_LOCAL_POWER:
2131 break;
2132 default:
2133 goto error;
2135 break;
2136 case ClearPortFeature:
2137 if (wIndex > R8A66597_MAX_ROOT_HUB)
2138 goto error;
2139 if (wLength != 0)
2140 goto error;
2142 switch (wValue) {
2143 case USB_PORT_FEAT_ENABLE:
2144 rh->port &= (1 << USB_PORT_FEAT_POWER);
2145 break;
2146 case USB_PORT_FEAT_SUSPEND:
2147 break;
2148 case USB_PORT_FEAT_POWER:
2149 r8a66597_port_power(r8a66597, port, 0);
2150 break;
2151 case USB_PORT_FEAT_C_ENABLE:
2152 case USB_PORT_FEAT_C_SUSPEND:
2153 case USB_PORT_FEAT_C_CONNECTION:
2154 case USB_PORT_FEAT_C_OVER_CURRENT:
2155 case USB_PORT_FEAT_C_RESET:
2156 break;
2157 default:
2158 goto error;
2160 rh->port &= ~(1 << wValue);
2161 break;
2162 case GetHubDescriptor:
2163 r8a66597_hub_descriptor(r8a66597,
2164 (struct usb_hub_descriptor *)buf);
2165 break;
2166 case GetHubStatus:
2167 *buf = 0x00;
2168 break;
2169 case GetPortStatus:
2170 if (wIndex > R8A66597_MAX_ROOT_HUB)
2171 goto error;
2172 *(__le32 *)buf = cpu_to_le32(rh->port);
2173 break;
2174 case SetPortFeature:
2175 if (wIndex > R8A66597_MAX_ROOT_HUB)
2176 goto error;
2177 if (wLength != 0)
2178 goto error;
2180 switch (wValue) {
2181 case USB_PORT_FEAT_SUSPEND:
2182 break;
2183 case USB_PORT_FEAT_POWER:
2184 r8a66597_port_power(r8a66597, port, 1);
2185 rh->port |= (1 << USB_PORT_FEAT_POWER);
2186 break;
2187 case USB_PORT_FEAT_RESET: {
2188 struct r8a66597_device *dev = rh->dev;
2190 rh->port |= (1 << USB_PORT_FEAT_RESET);
2192 disable_r8a66597_pipe_all(r8a66597, dev);
2193 free_usb_address(r8a66597, dev);
2195 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2196 get_dvstctr_reg(port));
2197 mod_timer(&r8a66597->rh_timer,
2198 jiffies + msecs_to_jiffies(50));
2200 break;
2201 default:
2202 goto error;
2204 rh->port |= 1 << wValue;
2205 break;
2206 default:
2207 error:
2208 ret = -EPIPE;
2209 break;
2212 spin_unlock_irqrestore(&r8a66597->lock, flags);
2213 return ret;
2216 static struct hc_driver r8a66597_hc_driver = {
2217 .description = hcd_name,
2218 .hcd_priv_size = sizeof(struct r8a66597),
2219 .irq = r8a66597_irq,
2222 * generic hardware linkage
2224 .flags = HCD_USB2,
2226 .start = r8a66597_start,
2227 .stop = r8a66597_stop,
2230 * managing i/o requests and associated device resources
2232 .urb_enqueue = r8a66597_urb_enqueue,
2233 .urb_dequeue = r8a66597_urb_dequeue,
2234 .endpoint_disable = r8a66597_endpoint_disable,
2237 * periodic schedule support
2239 .get_frame_number = r8a66597_get_frame,
2242 * root hub support
2244 .hub_status_data = r8a66597_hub_status_data,
2245 .hub_control = r8a66597_hub_control,
2248 #if defined(CONFIG_PM)
2249 static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
2251 return 0;
2254 static int r8a66597_resume(struct platform_device *pdev)
2256 return 0;
2258 #else /* if defined(CONFIG_PM) */
2259 #define r8a66597_suspend NULL
2260 #define r8a66597_resume NULL
2261 #endif
2263 static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2265 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2266 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2268 del_timer_sync(&r8a66597->rh_timer);
2269 usb_remove_hcd(hcd);
2270 iounmap((void *)r8a66597->reg);
2271 #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2272 clk_put(r8a66597->clk);
2273 #endif
2274 usb_put_hcd(hcd);
2275 return 0;
2278 static int __init r8a66597_probe(struct platform_device *pdev)
2280 #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2281 char clk_name[8];
2282 #endif
2283 struct resource *res = NULL, *ires;
2284 int irq = -1;
2285 void __iomem *reg = NULL;
2286 struct usb_hcd *hcd = NULL;
2287 struct r8a66597 *r8a66597;
2288 int ret = 0;
2289 int i;
2290 unsigned long irq_trigger;
2292 if (pdev->dev.dma_mask) {
2293 ret = -EINVAL;
2294 dev_err(&pdev->dev, "dma not supported\n");
2295 goto clean_up;
2298 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2299 if (!res) {
2300 ret = -ENODEV;
2301 dev_err(&pdev->dev, "platform_get_resource error.\n");
2302 goto clean_up;
2305 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2306 if (!ires) {
2307 ret = -ENODEV;
2308 dev_err(&pdev->dev,
2309 "platform_get_resource IORESOURCE_IRQ error.\n");
2310 goto clean_up;
2313 irq = ires->start;
2314 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2316 reg = ioremap(res->start, resource_size(res));
2317 if (reg == NULL) {
2318 ret = -ENOMEM;
2319 dev_err(&pdev->dev, "ioremap error.\n");
2320 goto clean_up;
2323 /* initialize hcd */
2324 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2325 if (!hcd) {
2326 ret = -ENOMEM;
2327 dev_err(&pdev->dev, "Failed to create hcd\n");
2328 goto clean_up;
2330 r8a66597 = hcd_to_r8a66597(hcd);
2331 memset(r8a66597, 0, sizeof(struct r8a66597));
2332 dev_set_drvdata(&pdev->dev, r8a66597);
2334 #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2335 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2336 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2337 if (IS_ERR(r8a66597->clk)) {
2338 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
2339 ret = PTR_ERR(r8a66597->clk);
2340 goto clean_up2;
2342 #endif
2344 spin_lock_init(&r8a66597->lock);
2345 init_timer(&r8a66597->rh_timer);
2346 r8a66597->rh_timer.function = r8a66597_timer;
2347 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2348 r8a66597->reg = (unsigned long)reg;
2350 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2351 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2352 init_timer(&r8a66597->td_timer[i]);
2353 r8a66597->td_timer[i].function = r8a66597_td_timer;
2354 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
2355 setup_timer(&r8a66597->interval_timer[i],
2356 r8a66597_interval_timer,
2357 (unsigned long)r8a66597);
2359 INIT_LIST_HEAD(&r8a66597->child_device);
2361 hcd->rsrc_start = res->start;
2363 /* irq_sense setting on cmdline takes precedence over resource
2364 * settings, so the introduction of irqflags in IRQ resourse
2365 * won't disturb existing setups */
2366 switch (irq_sense) {
2367 case INTL:
2368 irq_trigger = IRQF_TRIGGER_LOW;
2369 break;
2370 case 0:
2371 irq_trigger = IRQF_TRIGGER_FALLING;
2372 break;
2373 case 0xff:
2374 if (irq_trigger)
2375 irq_sense = (irq_trigger & IRQF_TRIGGER_LOW) ?
2376 INTL : 0;
2377 else {
2378 irq_sense = INTL;
2379 irq_trigger = IRQF_TRIGGER_LOW;
2381 break;
2382 default:
2383 dev_err(&pdev->dev, "Unknown irq_sense value.\n");
2386 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
2387 if (ret != 0) {
2388 dev_err(&pdev->dev, "Failed to add hcd\n");
2389 goto clean_up3;
2392 return 0;
2394 clean_up3:
2395 #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2396 clk_put(r8a66597->clk);
2397 clean_up2:
2398 #endif
2399 usb_put_hcd(hcd);
2401 clean_up:
2402 if (reg)
2403 iounmap(reg);
2405 return ret;
2408 static struct platform_driver r8a66597_driver = {
2409 .probe = r8a66597_probe,
2410 .remove = r8a66597_remove,
2411 .suspend = r8a66597_suspend,
2412 .resume = r8a66597_resume,
2413 .driver = {
2414 .name = (char *) hcd_name,
2415 .owner = THIS_MODULE,
2419 static int __init r8a66597_init(void)
2421 if (usb_disabled())
2422 return -ENODEV;
2424 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2425 DRIVER_VERSION);
2426 return platform_driver_register(&r8a66597_driver);
2428 module_init(r8a66597_init);
2430 static void __exit r8a66597_cleanup(void)
2432 platform_driver_unregister(&r8a66597_driver);
2434 module_exit(r8a66597_cleanup);