1 // SPDX-License-Identifier: GPL-2.0
3 * R8A66597 HCD (Host Controller Driver)
5 * Copyright (C) 2006-2007 Renesas Solutions Corp.
6 * Portions Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
7 * Portions Copyright (C) 2004-2005 David Brownell
8 * Portions Copyright (C) 1999 Roman Weissgaerber
10 * Author : Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/errno.h>
17 #include <linux/timer.h>
18 #include <linux/delay.h>
19 #include <linux/list.h>
20 #include <linux/interrupt.h>
21 #include <linux/usb.h>
22 #include <linux/usb/hcd.h>
23 #include <linux/platform_device.h>
26 #include <linux/irq.h>
27 #include <linux/slab.h>
28 #include <asm/cacheflush.h>
32 MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Yoshihiro Shimoda");
35 MODULE_ALIAS("platform:r8a66597_hcd");
37 #define DRIVER_VERSION "2009-05-26"
39 static const char hcd_name
[] = "r8a66597_hcd";
41 static void packet_write(struct r8a66597
*r8a66597
, u16 pipenum
);
42 static int r8a66597_get_frame(struct usb_hcd
*hcd
);
44 /* this function must be called with interrupt disabled */
45 static void enable_pipe_irq(struct r8a66597
*r8a66597
, u16 pipenum
,
50 tmp
= r8a66597_read(r8a66597
, INTENB0
);
51 r8a66597_bclr(r8a66597
, BEMPE
| NRDYE
| BRDYE
, INTENB0
);
52 r8a66597_bset(r8a66597
, 1 << pipenum
, reg
);
53 r8a66597_write(r8a66597
, tmp
, INTENB0
);
56 /* this function must be called with interrupt disabled */
57 static void disable_pipe_irq(struct r8a66597
*r8a66597
, u16 pipenum
,
62 tmp
= r8a66597_read(r8a66597
, INTENB0
);
63 r8a66597_bclr(r8a66597
, BEMPE
| NRDYE
| BRDYE
, INTENB0
);
64 r8a66597_bclr(r8a66597
, 1 << pipenum
, reg
);
65 r8a66597_write(r8a66597
, tmp
, INTENB0
);
68 static void set_devadd_reg(struct r8a66597
*r8a66597
, u8 r8a66597_address
,
69 u16 usbspd
, u8 upphub
, u8 hubport
, int port
)
72 unsigned long devadd_reg
= get_devadd_addr(r8a66597_address
);
74 val
= (upphub
<< 11) | (hubport
<< 8) | (usbspd
<< 6) | (port
& 0x0001);
75 r8a66597_write(r8a66597
, val
, devadd_reg
);
78 static int r8a66597_clock_enable(struct r8a66597
*r8a66597
)
83 if (r8a66597
->pdata
->on_chip
) {
84 clk_prepare_enable(r8a66597
->clk
);
86 r8a66597_write(r8a66597
, SCKE
, SYSCFG0
);
87 tmp
= r8a66597_read(r8a66597
, SYSCFG0
);
89 printk(KERN_ERR
"r8a66597: reg access fail.\n");
92 } while ((tmp
& SCKE
) != SCKE
);
93 r8a66597_write(r8a66597
, 0x04, 0x02);
96 r8a66597_write(r8a66597
, USBE
, SYSCFG0
);
97 tmp
= r8a66597_read(r8a66597
, SYSCFG0
);
99 printk(KERN_ERR
"r8a66597: reg access fail.\n");
102 } while ((tmp
& USBE
) != USBE
);
103 r8a66597_bclr(r8a66597
, USBE
, SYSCFG0
);
104 r8a66597_mdfy(r8a66597
, get_xtal_from_pdata(r8a66597
->pdata
),
108 r8a66597_bset(r8a66597
, XCKE
, SYSCFG0
);
111 tmp
= r8a66597_read(r8a66597
, SYSCFG0
);
113 printk(KERN_ERR
"r8a66597: reg access fail.\n");
116 } while ((tmp
& SCKE
) != SCKE
);
122 static void r8a66597_clock_disable(struct r8a66597
*r8a66597
)
124 r8a66597_bclr(r8a66597
, SCKE
, SYSCFG0
);
127 if (r8a66597
->pdata
->on_chip
) {
128 clk_disable_unprepare(r8a66597
->clk
);
130 r8a66597_bclr(r8a66597
, PLLC
, SYSCFG0
);
131 r8a66597_bclr(r8a66597
, XCKE
, SYSCFG0
);
132 r8a66597_bclr(r8a66597
, USBE
, SYSCFG0
);
136 static void r8a66597_enable_port(struct r8a66597
*r8a66597
, int port
)
140 val
= port
? DRPD
: DCFM
| DRPD
;
141 r8a66597_bset(r8a66597
, val
, get_syscfg_reg(port
));
142 r8a66597_bset(r8a66597
, HSE
, get_syscfg_reg(port
));
144 r8a66597_write(r8a66597
, BURST
| CPU_ADR_RD_WR
, get_dmacfg_reg(port
));
145 r8a66597_bclr(r8a66597
, DTCHE
, get_intenb_reg(port
));
146 r8a66597_bset(r8a66597
, ATTCHE
, get_intenb_reg(port
));
149 static void r8a66597_disable_port(struct r8a66597
*r8a66597
, int port
)
153 r8a66597_write(r8a66597
, 0, get_intenb_reg(port
));
154 r8a66597_write(r8a66597
, 0, get_intsts_reg(port
));
156 r8a66597_port_power(r8a66597
, port
, 0);
159 tmp
= r8a66597_read(r8a66597
, SOFCFG
) & EDGESTS
;
161 } while (tmp
== EDGESTS
);
163 val
= port
? DRPD
: DCFM
| DRPD
;
164 r8a66597_bclr(r8a66597
, val
, get_syscfg_reg(port
));
165 r8a66597_bclr(r8a66597
, HSE
, get_syscfg_reg(port
));
168 static int enable_controller(struct r8a66597
*r8a66597
)
171 u16 vif
= r8a66597
->pdata
->vif
? LDRV
: 0;
172 u16 irq_sense
= r8a66597
->irq_sense_low
? INTL
: 0;
173 u16 endian
= r8a66597
->pdata
->endian
? BIGEND
: 0;
175 ret
= r8a66597_clock_enable(r8a66597
);
179 r8a66597_bset(r8a66597
, vif
& LDRV
, PINCFG
);
180 r8a66597_bset(r8a66597
, USBE
, SYSCFG0
);
182 r8a66597_bset(r8a66597
, BEMPE
| NRDYE
| BRDYE
, INTENB0
);
183 r8a66597_bset(r8a66597
, irq_sense
& INTL
, SOFCFG
);
184 r8a66597_bset(r8a66597
, BRDY0
, BRDYENB
);
185 r8a66597_bset(r8a66597
, BEMP0
, BEMPENB
);
187 r8a66597_bset(r8a66597
, endian
& BIGEND
, CFIFOSEL
);
188 r8a66597_bset(r8a66597
, endian
& BIGEND
, D0FIFOSEL
);
189 r8a66597_bset(r8a66597
, endian
& BIGEND
, D1FIFOSEL
);
190 r8a66597_bset(r8a66597
, TRNENSEL
, SOFCFG
);
192 r8a66597_bset(r8a66597
, SIGNE
| SACKE
, INTENB1
);
194 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++)
195 r8a66597_enable_port(r8a66597
, port
);
200 static void disable_controller(struct r8a66597
*r8a66597
)
204 /* disable interrupts */
205 r8a66597_write(r8a66597
, 0, INTENB0
);
206 r8a66597_write(r8a66597
, 0, INTENB1
);
207 r8a66597_write(r8a66597
, 0, BRDYENB
);
208 r8a66597_write(r8a66597
, 0, BEMPENB
);
209 r8a66597_write(r8a66597
, 0, NRDYENB
);
212 r8a66597_write(r8a66597
, 0, BRDYSTS
);
213 r8a66597_write(r8a66597
, 0, NRDYSTS
);
214 r8a66597_write(r8a66597
, 0, BEMPSTS
);
216 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++)
217 r8a66597_disable_port(r8a66597
, port
);
219 r8a66597_clock_disable(r8a66597
);
222 static int get_parent_r8a66597_address(struct r8a66597
*r8a66597
,
223 struct usb_device
*udev
)
225 struct r8a66597_device
*dev
;
227 if (udev
->parent
&& udev
->parent
->devnum
!= 1)
230 dev
= dev_get_drvdata(&udev
->dev
);
237 static int is_child_device(char *devpath
)
239 return (devpath
[2] ? 1 : 0);
242 static int is_hub_limit(char *devpath
)
244 return ((strlen(devpath
) >= 4) ? 1 : 0);
247 static void get_port_number(struct r8a66597
*r8a66597
,
248 char *devpath
, u16
*root_port
, u16
*hub_port
)
251 *root_port
= (devpath
[0] & 0x0F) - 1;
252 if (*root_port
>= r8a66597
->max_root_hub
)
253 printk(KERN_ERR
"r8a66597: Illegal root port number.\n");
256 *hub_port
= devpath
[2] & 0x0F;
259 static u16
get_r8a66597_usb_speed(enum usb_device_speed speed
)
274 printk(KERN_ERR
"r8a66597: unknown speed\n");
281 static void set_child_connect_map(struct r8a66597
*r8a66597
, int address
)
286 r8a66597
->child_connect_map
[idx
] |= 1 << (address
% 32);
289 static void put_child_connect_map(struct r8a66597
*r8a66597
, int address
)
294 r8a66597
->child_connect_map
[idx
] &= ~(1 << (address
% 32));
297 static void set_pipe_reg_addr(struct r8a66597_pipe
*pipe
, u8 dma_ch
)
299 u16 pipenum
= pipe
->info
.pipenum
;
300 const unsigned long fifoaddr
[] = {D0FIFO
, D1FIFO
, CFIFO
};
301 const unsigned long fifosel
[] = {D0FIFOSEL
, D1FIFOSEL
, CFIFOSEL
};
302 const unsigned long fifoctr
[] = {D0FIFOCTR
, D1FIFOCTR
, CFIFOCTR
};
304 if (dma_ch
> R8A66597_PIPE_NO_DMA
) /* dma fifo not use? */
305 dma_ch
= R8A66597_PIPE_NO_DMA
;
307 pipe
->fifoaddr
= fifoaddr
[dma_ch
];
308 pipe
->fifosel
= fifosel
[dma_ch
];
309 pipe
->fifoctr
= fifoctr
[dma_ch
];
312 pipe
->pipectr
= DCPCTR
;
314 pipe
->pipectr
= get_pipectr_addr(pipenum
);
316 if (check_bulk_or_isoc(pipenum
)) {
317 pipe
->pipetre
= get_pipetre_addr(pipenum
);
318 pipe
->pipetrn
= get_pipetrn_addr(pipenum
);
325 static struct r8a66597_device
*
326 get_urb_to_r8a66597_dev(struct r8a66597
*r8a66597
, struct urb
*urb
)
328 if (usb_pipedevice(urb
->pipe
) == 0)
329 return &r8a66597
->device0
;
331 return dev_get_drvdata(&urb
->dev
->dev
);
334 static int make_r8a66597_device(struct r8a66597
*r8a66597
,
335 struct urb
*urb
, u8 addr
)
337 struct r8a66597_device
*dev
;
338 int usb_address
= urb
->setup_packet
[2]; /* urb->pipe is address 0 */
340 dev
= kzalloc(sizeof(struct r8a66597_device
), GFP_ATOMIC
);
344 dev_set_drvdata(&urb
->dev
->dev
, dev
);
345 dev
->udev
= urb
->dev
;
347 dev
->usb_address
= usb_address
;
348 dev
->state
= USB_STATE_ADDRESS
;
349 dev
->ep_in_toggle
= 0;
350 dev
->ep_out_toggle
= 0;
351 INIT_LIST_HEAD(&dev
->device_list
);
352 list_add_tail(&dev
->device_list
, &r8a66597
->child_device
);
354 get_port_number(r8a66597
, urb
->dev
->devpath
,
355 &dev
->root_port
, &dev
->hub_port
);
356 if (!is_child_device(urb
->dev
->devpath
))
357 r8a66597
->root_hub
[dev
->root_port
].dev
= dev
;
359 set_devadd_reg(r8a66597
, dev
->address
,
360 get_r8a66597_usb_speed(urb
->dev
->speed
),
361 get_parent_r8a66597_address(r8a66597
, urb
->dev
),
362 dev
->hub_port
, dev
->root_port
);
367 /* this function must be called with interrupt disabled */
368 static u8
alloc_usb_address(struct r8a66597
*r8a66597
, struct urb
*urb
)
370 u8 addr
; /* R8A66597's address */
371 struct r8a66597_device
*dev
;
373 if (is_hub_limit(urb
->dev
->devpath
)) {
374 dev_err(&urb
->dev
->dev
, "External hub limit reached.\n");
378 dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
379 if (dev
&& dev
->state
>= USB_STATE_ADDRESS
)
382 for (addr
= 1; addr
<= R8A66597_MAX_DEVICE
; addr
++) {
383 if (r8a66597
->address_map
& (1 << addr
))
386 dev_dbg(&urb
->dev
->dev
, "alloc_address: r8a66597_addr=%d\n", addr
);
387 r8a66597
->address_map
|= 1 << addr
;
389 if (make_r8a66597_device(r8a66597
, urb
, addr
) < 0)
395 dev_err(&urb
->dev
->dev
,
396 "cannot communicate with a USB device more than 10.(%x)\n",
397 r8a66597
->address_map
);
402 /* this function must be called with interrupt disabled */
403 static void free_usb_address(struct r8a66597
*r8a66597
,
404 struct r8a66597_device
*dev
, int reset
)
411 dev_dbg(&dev
->udev
->dev
, "free_addr: addr=%d\n", dev
->address
);
413 dev
->state
= USB_STATE_DEFAULT
;
414 r8a66597
->address_map
&= ~(1 << dev
->address
);
417 * Only when resetting USB, it is necessary to erase drvdata. When
418 * a usb device with usb hub is disconnect, "dev->udev" is already
419 * freed on usb_desconnect(). So we cannot access the data.
422 dev_set_drvdata(&dev
->udev
->dev
, NULL
);
423 list_del(&dev
->device_list
);
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
;
434 static void r8a66597_reg_wait(struct r8a66597
*r8a66597
, unsigned long reg
,
441 tmp
= r8a66597_read(r8a66597
, reg
);
443 printk(KERN_ERR
"r8a66597: register%lx, loop %x "
444 "is timeout\n", reg
, loop
);
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
)
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
)
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
)
480 if (!pipe
|| pipe
->info
.pipenum
== 0)
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
)
496 r8a66597_bset(r8a66597
, SQSET
, pipe
->pipectr
);
498 r8a66597_bset(r8a66597
, SQCLR
, pipe
->pipectr
);
501 static inline unsigned short mbw_value(struct r8a66597
*r8a66597
)
503 if (r8a66597
->pdata
->on_chip
)
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
,
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)
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
,
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
);
572 *toggle
|= 1 << endpoint
;
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
,
582 if (r8a66597_read(r8a66597
, pipe
->pipectr
) & SQMON
)
583 pipe_toggle_set(r8a66597
, pipe
, urb
, 1);
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
,
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
);
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
)
609 if (info
->pipenum
== 0)
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
);
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
),
624 r8a66597_write(r8a66597
, make_devsel(info
->address
) | info
->maxpacket
,
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
))
669 case USB_ENDPOINT_XFER_INT
:
670 if (usb_endpoint_dir_in(ep
)) {
677 case USB_ENDPOINT_XFER_ISOC
:
678 if (usb_endpoint_dir_in(ep
))
684 printk(KERN_ERR
"r8a66597: Illegal type\n");
690 while (array
[i
] != 0) {
691 if (r8a66597
->pipe_cnt
[min
] > r8a66597
->pipe_cnt
[array
[i
]])
699 static u16
get_r8a66597_type(__u8 type
)
704 case USB_ENDPOINT_XFER_BULK
:
705 r8a66597_type
= R8A66597_BULK
;
707 case USB_ENDPOINT_XFER_INT
:
708 r8a66597_type
= R8A66597_INT
;
710 case USB_ENDPOINT_XFER_ISOC
:
711 r8a66597_type
= R8A66597_ISO
;
714 printk(KERN_ERR
"r8a66597: Illegal type\n");
715 r8a66597_type
= 0x0000;
719 return r8a66597_type
;
722 static u16
get_bufnum(u16 pipenum
)
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);
733 printk(KERN_ERR
"r8a66597: Illegal pipenum (%d)\n", pipenum
);
738 static u16
get_buf_bsize(u16 pipenum
)
744 else if (check_bulk_or_isoc(pipenum
))
745 buf_bsize
= R8A66597_BUF_BSIZE
- 1;
746 else if (check_interrupt(pipenum
))
749 printk(KERN_ERR
"r8a66597: Illegal pipenum (%d)\n", pipenum
);
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
,
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
)
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)
773 dev_info(&dev
->udev
->dev
,
774 "address %d, EndpointAddress 0x%02x use "
775 "DMA FIFO\n", usb_pipedevice(urb
->pipe
),
777 USB_ENDPOINT_DIR_MASK
+ 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
,
790 r8a66597_bset(r8a66597
, BCLR
, pipe
->fifoctr
);
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 dev_dbg(&dev
->udev
->dev
, "enable_pipe:\n");
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 static void r8a66597_urb_done(struct r8a66597
*r8a66597
, struct urb
*urb
,
816 __releases(r8a66597
->lock
)
817 __acquires(r8a66597
->lock
)
819 if (usb_pipein(urb
->pipe
) && usb_pipetype(urb
->pipe
) != PIPE_CONTROL
) {
822 for (ptr
= urb
->transfer_buffer
;
823 ptr
< urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
825 flush_dcache_page(virt_to_page(ptr
));
828 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597
), urb
);
829 spin_unlock(&r8a66597
->lock
);
830 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597
), urb
, status
);
831 spin_lock(&r8a66597
->lock
);
834 /* this function must be called with interrupt disabled */
835 static void force_dequeue(struct r8a66597
*r8a66597
, u16 pipenum
, u16 address
)
837 struct r8a66597_td
*td
, *next
;
839 struct list_head
*list
= &r8a66597
->pipe_queue
[pipenum
];
841 if (list_empty(list
))
844 list_for_each_entry_safe(td
, next
, list
, queue
) {
845 if (td
->address
!= address
)
849 list_del(&td
->queue
);
853 r8a66597_urb_done(r8a66597
, urb
, -ENODEV
);
859 /* this function must be called with interrupt disabled */
860 static void disable_r8a66597_pipe_all(struct r8a66597
*r8a66597
,
861 struct r8a66597_device
*dev
)
869 for (pipenum
= 1; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
870 if (!dev
->pipe_cnt
[pipenum
])
875 force_dequeue(r8a66597
, 0, dev
->address
);
878 r8a66597
->pipe_cnt
[pipenum
] -= dev
->pipe_cnt
[pipenum
];
879 dev
->pipe_cnt
[pipenum
] = 0;
880 force_dequeue(r8a66597
, pipenum
, dev
->address
);
883 dev_dbg(&dev
->udev
->dev
, "disable_pipe\n");
885 r8a66597
->dma_map
&= ~(dev
->dma_map
);
889 static u16
get_interval(struct urb
*urb
, __u8 interval
)
894 if (urb
->dev
->speed
== USB_SPEED_HIGH
) {
898 time
= interval
? interval
- 1 : 0;
900 if (interval
> 128) {
903 /* calculate the nearest value for PIPEPERI */
904 for (i
= 0; i
< 7; i
++) {
905 if ((1 << i
) < interval
&&
906 (1 << (i
+ 1) > interval
))
915 static unsigned long get_timer_interval(struct urb
*urb
, __u8 interval
)
918 unsigned long time
= 1;
920 if (usb_pipeisoc(urb
->pipe
))
923 if (get_r8a66597_usb_speed(urb
->dev
->speed
) == HSMODE
) {
924 for (i
= 0; i
< (interval
- 1); i
++)
926 time
= time
* 125 / 1000; /* uSOF -> msec */
934 /* this function must be called with interrupt disabled */
935 static void init_pipe_info(struct r8a66597
*r8a66597
, struct urb
*urb
,
936 struct usb_host_endpoint
*hep
,
937 struct usb_endpoint_descriptor
*ep
)
939 struct r8a66597_pipe_info info
;
941 info
.pipenum
= get_empty_pipenum(r8a66597
, ep
);
942 info
.address
= get_urb_to_r8a66597_addr(r8a66597
, urb
);
943 info
.epnum
= usb_endpoint_num(ep
);
944 info
.maxpacket
= usb_endpoint_maxp(ep
);
945 info
.type
= get_r8a66597_type(usb_endpoint_type(ep
));
946 info
.bufnum
= get_bufnum(info
.pipenum
);
947 info
.buf_bsize
= get_buf_bsize(info
.pipenum
);
948 if (info
.type
== R8A66597_BULK
) {
950 info
.timer_interval
= 0;
952 info
.interval
= get_interval(urb
, ep
->bInterval
);
953 info
.timer_interval
= get_timer_interval(urb
, ep
->bInterval
);
955 if (usb_endpoint_dir_in(ep
))
960 enable_r8a66597_pipe(r8a66597
, urb
, hep
, &info
);
963 static void init_pipe_config(struct r8a66597
*r8a66597
, struct urb
*urb
)
965 struct r8a66597_device
*dev
;
967 dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
968 dev
->state
= USB_STATE_CONFIGURED
;
971 static void pipe_irq_enable(struct r8a66597
*r8a66597
, struct urb
*urb
,
974 if (pipenum
== 0 && usb_pipeout(urb
->pipe
))
975 enable_irq_empty(r8a66597
, pipenum
);
977 enable_irq_ready(r8a66597
, pipenum
);
979 if (!usb_pipeisoc(urb
->pipe
))
980 enable_irq_nrdy(r8a66597
, pipenum
);
983 static void pipe_irq_disable(struct r8a66597
*r8a66597
, u16 pipenum
)
985 disable_irq_ready(r8a66597
, pipenum
);
986 disable_irq_nrdy(r8a66597
, pipenum
);
989 static void r8a66597_root_hub_start_polling(struct r8a66597
*r8a66597
)
991 mod_timer(&r8a66597
->rh_timer
,
992 jiffies
+ msecs_to_jiffies(R8A66597_RH_POLL_TIME
));
995 static void start_root_hub_sampling(struct r8a66597
*r8a66597
, int port
,
998 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
1000 rh
->old_syssts
= r8a66597_read(r8a66597
, get_syssts_reg(port
)) & LNST
;
1001 rh
->scount
= R8A66597_MAX_SAMPLING
;
1003 rh
->port
|= USB_PORT_STAT_CONNECTION
;
1005 rh
->port
&= ~USB_PORT_STAT_CONNECTION
;
1006 rh
->port
|= USB_PORT_STAT_C_CONNECTION
<< 16;
1008 r8a66597_root_hub_start_polling(r8a66597
);
1011 /* this function must be called with interrupt disabled */
1012 static void r8a66597_check_syssts(struct r8a66597
*r8a66597
, int port
,
1014 __releases(r8a66597
->lock
)
1015 __acquires(r8a66597
->lock
)
1017 if (syssts
== SE0
) {
1018 r8a66597_write(r8a66597
, ~ATTCH
, get_intsts_reg(port
));
1019 r8a66597_bset(r8a66597
, ATTCHE
, get_intenb_reg(port
));
1021 if (syssts
== FS_JSTS
)
1022 r8a66597_bset(r8a66597
, HSE
, get_syscfg_reg(port
));
1023 else if (syssts
== LS_JSTS
)
1024 r8a66597_bclr(r8a66597
, HSE
, get_syscfg_reg(port
));
1026 r8a66597_write(r8a66597
, ~DTCH
, get_intsts_reg(port
));
1027 r8a66597_bset(r8a66597
, DTCHE
, get_intenb_reg(port
));
1029 if (r8a66597
->bus_suspended
)
1030 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597
));
1033 spin_unlock(&r8a66597
->lock
);
1034 usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597
));
1035 spin_lock(&r8a66597
->lock
);
1038 /* this function must be called with interrupt disabled */
1039 static void r8a66597_usb_connect(struct r8a66597
*r8a66597
, int port
)
1041 u16 speed
= get_rh_usb_speed(r8a66597
, port
);
1042 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
1044 rh
->port
&= ~(USB_PORT_STAT_HIGH_SPEED
| USB_PORT_STAT_LOW_SPEED
);
1045 if (speed
== HSMODE
)
1046 rh
->port
|= USB_PORT_STAT_HIGH_SPEED
;
1047 else if (speed
== LSMODE
)
1048 rh
->port
|= USB_PORT_STAT_LOW_SPEED
;
1050 rh
->port
&= ~USB_PORT_STAT_RESET
;
1051 rh
->port
|= USB_PORT_STAT_ENABLE
;
1054 /* this function must be called with interrupt disabled */
1055 static void r8a66597_usb_disconnect(struct r8a66597
*r8a66597
, int port
)
1057 struct r8a66597_device
*dev
= r8a66597
->root_hub
[port
].dev
;
1059 disable_r8a66597_pipe_all(r8a66597
, dev
);
1060 free_usb_address(r8a66597
, dev
, 0);
1062 start_root_hub_sampling(r8a66597
, port
, 0);
1065 /* this function must be called with interrupt disabled */
1066 static void prepare_setup_packet(struct r8a66597
*r8a66597
,
1067 struct r8a66597_td
*td
)
1070 __le16
*p
= (__le16
*)td
->urb
->setup_packet
;
1071 unsigned long setup_addr
= USBREQ
;
1073 r8a66597_write(r8a66597
, make_devsel(td
->address
) | td
->maxpacket
,
1075 r8a66597_write(r8a66597
, ~(SIGN
| SACK
), INTSTS1
);
1077 for (i
= 0; i
< 4; i
++) {
1078 r8a66597_write(r8a66597
, le16_to_cpu(p
[i
]), setup_addr
);
1081 r8a66597_write(r8a66597
, SUREQ
, DCPCTR
);
1084 /* this function must be called with interrupt disabled */
1085 static void prepare_packet_read(struct r8a66597
*r8a66597
,
1086 struct r8a66597_td
*td
)
1088 struct urb
*urb
= td
->urb
;
1090 if (usb_pipecontrol(urb
->pipe
)) {
1091 r8a66597_bclr(r8a66597
, R8A66597_DIR
, DCPCFG
);
1092 r8a66597_mdfy(r8a66597
, 0, ISEL
| CURPIPE
, CFIFOSEL
);
1093 r8a66597_reg_wait(r8a66597
, CFIFOSEL
, CURPIPE
, 0);
1094 if (urb
->actual_length
== 0) {
1095 r8a66597_pipe_toggle(r8a66597
, td
->pipe
, 1);
1096 r8a66597_write(r8a66597
, BCLR
, CFIFOCTR
);
1098 pipe_irq_disable(r8a66597
, td
->pipenum
);
1099 pipe_start(r8a66597
, td
->pipe
);
1100 pipe_irq_enable(r8a66597
, urb
, td
->pipenum
);
1102 if (urb
->actual_length
== 0) {
1103 pipe_irq_disable(r8a66597
, td
->pipenum
);
1104 pipe_setting(r8a66597
, td
);
1105 pipe_stop(r8a66597
, td
->pipe
);
1106 r8a66597_write(r8a66597
, ~(1 << td
->pipenum
), BRDYSTS
);
1108 if (td
->pipe
->pipetre
) {
1109 r8a66597_write(r8a66597
, TRCLR
,
1111 r8a66597_write(r8a66597
,
1113 (urb
->transfer_buffer_length
,
1116 r8a66597_bset(r8a66597
, TRENB
,
1120 pipe_start(r8a66597
, td
->pipe
);
1121 pipe_irq_enable(r8a66597
, urb
, td
->pipenum
);
1126 /* this function must be called with interrupt disabled */
1127 static void prepare_packet_write(struct r8a66597
*r8a66597
,
1128 struct r8a66597_td
*td
)
1131 struct urb
*urb
= td
->urb
;
1133 if (usb_pipecontrol(urb
->pipe
)) {
1134 pipe_stop(r8a66597
, td
->pipe
);
1135 r8a66597_bset(r8a66597
, R8A66597_DIR
, DCPCFG
);
1136 r8a66597_mdfy(r8a66597
, ISEL
, ISEL
| CURPIPE
, CFIFOSEL
);
1137 r8a66597_reg_wait(r8a66597
, CFIFOSEL
, CURPIPE
, 0);
1138 if (urb
->actual_length
== 0) {
1139 r8a66597_pipe_toggle(r8a66597
, td
->pipe
, 1);
1140 r8a66597_write(r8a66597
, BCLR
, CFIFOCTR
);
1143 if (urb
->actual_length
== 0)
1144 pipe_setting(r8a66597
, td
);
1145 if (td
->pipe
->pipetre
)
1146 r8a66597_bclr(r8a66597
, TRENB
, td
->pipe
->pipetre
);
1148 r8a66597_write(r8a66597
, ~(1 << td
->pipenum
), BRDYSTS
);
1150 fifo_change_from_pipe(r8a66597
, td
->pipe
);
1151 tmp
= r8a66597_read(r8a66597
, td
->pipe
->fifoctr
);
1152 if (unlikely((tmp
& FRDY
) == 0))
1153 pipe_irq_enable(r8a66597
, urb
, td
->pipenum
);
1155 packet_write(r8a66597
, td
->pipenum
);
1156 pipe_start(r8a66597
, td
->pipe
);
1159 /* this function must be called with interrupt disabled */
1160 static void prepare_status_packet(struct r8a66597
*r8a66597
,
1161 struct r8a66597_td
*td
)
1163 struct urb
*urb
= td
->urb
;
1165 r8a66597_pipe_toggle(r8a66597
, td
->pipe
, 1);
1166 pipe_stop(r8a66597
, td
->pipe
);
1168 if (urb
->setup_packet
[0] & USB_ENDPOINT_DIR_MASK
) {
1169 r8a66597_bset(r8a66597
, R8A66597_DIR
, DCPCFG
);
1170 r8a66597_mdfy(r8a66597
, ISEL
, ISEL
| CURPIPE
, CFIFOSEL
);
1171 r8a66597_reg_wait(r8a66597
, CFIFOSEL
, CURPIPE
, 0);
1172 r8a66597_write(r8a66597
, ~BEMP0
, BEMPSTS
);
1173 r8a66597_write(r8a66597
, BCLR
| BVAL
, CFIFOCTR
);
1174 enable_irq_empty(r8a66597
, 0);
1176 r8a66597_bclr(r8a66597
, R8A66597_DIR
, DCPCFG
);
1177 r8a66597_mdfy(r8a66597
, 0, ISEL
| CURPIPE
, CFIFOSEL
);
1178 r8a66597_reg_wait(r8a66597
, CFIFOSEL
, CURPIPE
, 0);
1179 r8a66597_write(r8a66597
, BCLR
, CFIFOCTR
);
1180 enable_irq_ready(r8a66597
, 0);
1182 enable_irq_nrdy(r8a66597
, 0);
1183 pipe_start(r8a66597
, td
->pipe
);
1186 static int is_set_address(unsigned char *setup_packet
)
1188 if (((setup_packet
[0] & USB_TYPE_MASK
) == USB_TYPE_STANDARD
) &&
1189 setup_packet
[1] == USB_REQ_SET_ADDRESS
)
1195 /* this function must be called with interrupt disabled */
1196 static int start_transfer(struct r8a66597
*r8a66597
, struct r8a66597_td
*td
)
1202 if (is_set_address(td
->urb
->setup_packet
)) {
1203 td
->set_address
= 1;
1204 td
->urb
->setup_packet
[2] = alloc_usb_address(r8a66597
,
1206 if (td
->urb
->setup_packet
[2] == 0)
1209 prepare_setup_packet(r8a66597
, td
);
1212 prepare_packet_read(r8a66597
, td
);
1215 prepare_packet_write(r8a66597
, td
);
1218 prepare_status_packet(r8a66597
, td
);
1221 printk(KERN_ERR
"r8a66597: invalid type.\n");
1228 static int check_transfer_finish(struct r8a66597_td
*td
, struct urb
*urb
)
1230 if (usb_pipeisoc(urb
->pipe
)) {
1231 if (urb
->number_of_packets
== td
->iso_cnt
)
1235 /* control or bulk or interrupt */
1236 if ((urb
->transfer_buffer_length
<= urb
->actual_length
) ||
1237 (td
->short_packet
) || (td
->zero_packet
))
1243 /* this function must be called with interrupt disabled */
1244 static void set_td_timer(struct r8a66597
*r8a66597
, struct r8a66597_td
*td
)
1250 if (!list_empty(&r8a66597
->pipe_queue
[td
->pipenum
]) &&
1251 !usb_pipecontrol(td
->urb
->pipe
) && usb_pipein(td
->urb
->pipe
)) {
1252 r8a66597
->timeout_map
|= 1 << td
->pipenum
;
1253 switch (usb_pipetype(td
->urb
->pipe
)) {
1254 case PIPE_INTERRUPT
:
1255 case PIPE_ISOCHRONOUS
:
1263 mod_timer(&r8a66597
->timers
[td
->pipenum
].td
,
1264 jiffies
+ msecs_to_jiffies(time
));
1268 /* this function must be called with interrupt disabled */
1269 static void finish_request(struct r8a66597
*r8a66597
, struct r8a66597_td
*td
,
1270 u16 pipenum
, struct urb
*urb
, int status
)
1271 __releases(r8a66597
->lock
) __acquires(r8a66597
->lock
)
1274 struct usb_hcd
*hcd
= r8a66597_to_hcd(r8a66597
);
1276 r8a66597
->timeout_map
&= ~(1 << pipenum
);
1279 if (td
->set_address
&& (status
!= 0 || urb
->unlinked
))
1280 r8a66597
->address_map
&= ~(1 << urb
->setup_packet
[2]);
1282 pipe_toggle_save(r8a66597
, td
->pipe
, urb
);
1283 list_del(&td
->queue
);
1287 if (!list_empty(&r8a66597
->pipe_queue
[pipenum
]))
1291 if (usb_pipeisoc(urb
->pipe
))
1292 urb
->start_frame
= r8a66597_get_frame(hcd
);
1294 r8a66597_urb_done(r8a66597
, urb
, status
);
1298 td
= r8a66597_get_td(r8a66597
, pipenum
);
1302 start_transfer(r8a66597
, td
);
1303 set_td_timer(r8a66597
, td
);
1307 static void packet_read(struct r8a66597
*r8a66597
, u16 pipenum
)
1310 int rcv_len
, bufsize
, urb_len
, size
;
1312 struct r8a66597_td
*td
= r8a66597_get_td(r8a66597
, pipenum
);
1321 fifo_change_from_pipe(r8a66597
, td
->pipe
);
1322 tmp
= r8a66597_read(r8a66597
, td
->pipe
->fifoctr
);
1323 if (unlikely((tmp
& FRDY
) == 0)) {
1324 pipe_stop(r8a66597
, td
->pipe
);
1325 pipe_irq_disable(r8a66597
, pipenum
);
1326 printk(KERN_ERR
"r8a66597: in fifo not ready (%d)\n", pipenum
);
1327 finish_request(r8a66597
, td
, pipenum
, td
->urb
, -EPIPE
);
1331 /* prepare parameters */
1332 rcv_len
= tmp
& DTLN
;
1333 if (usb_pipeisoc(urb
->pipe
)) {
1334 buf
= (u16
*)(urb
->transfer_buffer
+
1335 urb
->iso_frame_desc
[td
->iso_cnt
].offset
);
1336 urb_len
= urb
->iso_frame_desc
[td
->iso_cnt
].length
;
1338 buf
= (void *)urb
->transfer_buffer
+ urb
->actual_length
;
1339 urb_len
= urb
->transfer_buffer_length
- urb
->actual_length
;
1341 bufsize
= min(urb_len
, (int) td
->maxpacket
);
1342 if (rcv_len
<= bufsize
) {
1346 status
= -EOVERFLOW
;
1350 /* update parameters */
1351 urb
->actual_length
+= size
;
1353 td
->zero_packet
= 1;
1354 if (rcv_len
< bufsize
) {
1355 td
->short_packet
= 1;
1357 if (usb_pipeisoc(urb
->pipe
)) {
1358 urb
->iso_frame_desc
[td
->iso_cnt
].actual_length
= size
;
1359 urb
->iso_frame_desc
[td
->iso_cnt
].status
= status
;
1364 /* check transfer finish */
1365 if (finish
|| check_transfer_finish(td
, urb
)) {
1366 pipe_stop(r8a66597
, td
->pipe
);
1367 pipe_irq_disable(r8a66597
, pipenum
);
1372 if (urb
->transfer_buffer
) {
1374 r8a66597_write(r8a66597
, BCLR
, td
->pipe
->fifoctr
);
1376 r8a66597_read_fifo(r8a66597
, td
->pipe
->fifoaddr
,
1380 if (finish
&& pipenum
!= 0)
1381 finish_request(r8a66597
, td
, pipenum
, urb
, status
);
1384 static void packet_write(struct r8a66597
*r8a66597
, u16 pipenum
)
1389 struct r8a66597_td
*td
= r8a66597_get_td(r8a66597
, pipenum
);
1396 fifo_change_from_pipe(r8a66597
, td
->pipe
);
1397 tmp
= r8a66597_read(r8a66597
, td
->pipe
->fifoctr
);
1398 if (unlikely((tmp
& FRDY
) == 0)) {
1399 pipe_stop(r8a66597
, td
->pipe
);
1400 pipe_irq_disable(r8a66597
, pipenum
);
1401 printk(KERN_ERR
"r8a66597: out fifo not ready (%d)\n", pipenum
);
1402 finish_request(r8a66597
, td
, pipenum
, urb
, -EPIPE
);
1406 /* prepare parameters */
1407 bufsize
= td
->maxpacket
;
1408 if (usb_pipeisoc(urb
->pipe
)) {
1409 buf
= (u16
*)(urb
->transfer_buffer
+
1410 urb
->iso_frame_desc
[td
->iso_cnt
].offset
);
1412 (int)urb
->iso_frame_desc
[td
->iso_cnt
].length
);
1414 buf
= (u16
*)(urb
->transfer_buffer
+ urb
->actual_length
);
1415 size
= min_t(u32
, bufsize
,
1416 urb
->transfer_buffer_length
- urb
->actual_length
);
1421 r8a66597_write(r8a66597
, ~(1 << pipenum
), BEMPSTS
);
1422 if (urb
->transfer_buffer
) {
1423 r8a66597_write_fifo(r8a66597
, td
->pipe
, buf
, size
);
1424 if (!usb_pipebulk(urb
->pipe
) || td
->maxpacket
!= size
)
1425 r8a66597_write(r8a66597
, BVAL
, td
->pipe
->fifoctr
);
1428 /* update parameters */
1429 urb
->actual_length
+= size
;
1430 if (usb_pipeisoc(urb
->pipe
)) {
1431 urb
->iso_frame_desc
[td
->iso_cnt
].actual_length
= size
;
1432 urb
->iso_frame_desc
[td
->iso_cnt
].status
= 0;
1436 /* check transfer finish */
1437 if (check_transfer_finish(td
, urb
)) {
1438 disable_irq_ready(r8a66597
, pipenum
);
1439 enable_irq_empty(r8a66597
, pipenum
);
1440 if (!usb_pipeisoc(urb
->pipe
))
1441 enable_irq_nrdy(r8a66597
, pipenum
);
1443 pipe_irq_enable(r8a66597
, urb
, pipenum
);
1447 static void check_next_phase(struct r8a66597
*r8a66597
, int status
)
1449 struct r8a66597_td
*td
= r8a66597_get_td(r8a66597
, 0);
1460 if (check_transfer_finish(td
, urb
))
1461 td
->type
= USB_PID_ACK
;
1464 if (urb
->transfer_buffer_length
== urb
->actual_length
)
1465 td
->type
= USB_PID_ACK
;
1466 else if (usb_pipeout(urb
->pipe
))
1467 td
->type
= USB_PID_OUT
;
1469 td
->type
= USB_PID_IN
;
1476 if (finish
|| status
!= 0 || urb
->unlinked
)
1477 finish_request(r8a66597
, td
, 0, urb
, status
);
1479 start_transfer(r8a66597
, td
);
1482 static int get_urb_error(struct r8a66597
*r8a66597
, u16 pipenum
)
1484 struct r8a66597_td
*td
= r8a66597_get_td(r8a66597
, pipenum
);
1487 u16 pid
= r8a66597_read(r8a66597
, td
->pipe
->pipectr
) & PID
;
1497 static void irq_pipe_ready(struct r8a66597
*r8a66597
)
1502 struct r8a66597_td
*td
;
1504 mask
= r8a66597_read(r8a66597
, BRDYSTS
)
1505 & r8a66597_read(r8a66597
, BRDYENB
);
1506 r8a66597_write(r8a66597
, ~mask
, BRDYSTS
);
1508 td
= r8a66597_get_td(r8a66597
, 0);
1509 if (td
&& td
->type
== USB_PID_IN
)
1510 packet_read(r8a66597
, 0);
1512 pipe_irq_disable(r8a66597
, 0);
1513 check_next_phase(r8a66597
, 0);
1516 for (pipenum
= 1; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1517 check
= 1 << pipenum
;
1519 td
= r8a66597_get_td(r8a66597
, pipenum
);
1523 if (td
->type
== USB_PID_IN
)
1524 packet_read(r8a66597
, pipenum
);
1525 else if (td
->type
== USB_PID_OUT
)
1526 packet_write(r8a66597
, pipenum
);
1531 static void irq_pipe_empty(struct r8a66597
*r8a66597
)
1537 struct r8a66597_td
*td
;
1539 mask
= r8a66597_read(r8a66597
, BEMPSTS
)
1540 & r8a66597_read(r8a66597
, BEMPENB
);
1541 r8a66597_write(r8a66597
, ~mask
, BEMPSTS
);
1543 cfifo_change(r8a66597
, 0);
1544 td
= r8a66597_get_td(r8a66597
, 0);
1545 if (td
&& td
->type
!= USB_PID_OUT
)
1546 disable_irq_empty(r8a66597
, 0);
1547 check_next_phase(r8a66597
, 0);
1550 for (pipenum
= 1; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1551 check
= 1 << pipenum
;
1553 struct r8a66597_td
*td
;
1554 td
= r8a66597_get_td(r8a66597
, pipenum
);
1558 tmp
= r8a66597_read(r8a66597
, td
->pipe
->pipectr
);
1559 if ((tmp
& INBUFM
) == 0) {
1560 disable_irq_empty(r8a66597
, pipenum
);
1561 pipe_irq_disable(r8a66597
, pipenum
);
1562 finish_request(r8a66597
, td
, pipenum
, td
->urb
,
1569 static void irq_pipe_nrdy(struct r8a66597
*r8a66597
)
1576 mask
= r8a66597_read(r8a66597
, NRDYSTS
)
1577 & r8a66597_read(r8a66597
, NRDYENB
);
1578 r8a66597_write(r8a66597
, ~mask
, NRDYSTS
);
1580 cfifo_change(r8a66597
, 0);
1581 status
= get_urb_error(r8a66597
, 0);
1582 pipe_irq_disable(r8a66597
, 0);
1583 check_next_phase(r8a66597
, status
);
1586 for (pipenum
= 1; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1587 check
= 1 << pipenum
;
1589 struct r8a66597_td
*td
;
1590 td
= r8a66597_get_td(r8a66597
, pipenum
);
1594 status
= get_urb_error(r8a66597
, pipenum
);
1595 pipe_irq_disable(r8a66597
, pipenum
);
1596 pipe_stop(r8a66597
, td
->pipe
);
1597 finish_request(r8a66597
, td
, pipenum
, td
->urb
, status
);
1602 static irqreturn_t
r8a66597_irq(struct usb_hcd
*hcd
)
1604 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1605 u16 intsts0
, intsts1
, intsts2
;
1606 u16 intenb0
, intenb1
, intenb2
;
1607 u16 mask0
, mask1
, mask2
;
1610 spin_lock(&r8a66597
->lock
);
1612 intsts0
= r8a66597_read(r8a66597
, INTSTS0
);
1613 intsts1
= r8a66597_read(r8a66597
, INTSTS1
);
1614 intsts2
= r8a66597_read(r8a66597
, INTSTS2
);
1615 intenb0
= r8a66597_read(r8a66597
, INTENB0
);
1616 intenb1
= r8a66597_read(r8a66597
, INTENB1
);
1617 intenb2
= r8a66597_read(r8a66597
, INTENB2
);
1619 mask2
= intsts2
& intenb2
;
1620 mask1
= intsts1
& intenb1
;
1621 mask0
= intsts0
& intenb0
& (BEMP
| NRDY
| BRDY
);
1623 if (mask2
& ATTCH
) {
1624 r8a66597_write(r8a66597
, ~ATTCH
, INTSTS2
);
1625 r8a66597_bclr(r8a66597
, ATTCHE
, INTENB2
);
1627 /* start usb bus sampling */
1628 start_root_hub_sampling(r8a66597
, 1, 1);
1631 r8a66597_write(r8a66597
, ~DTCH
, INTSTS2
);
1632 r8a66597_bclr(r8a66597
, DTCHE
, INTENB2
);
1633 r8a66597_usb_disconnect(r8a66597
, 1);
1636 r8a66597_write(r8a66597
, ~BCHG
, INTSTS2
);
1637 r8a66597_bclr(r8a66597
, BCHGE
, INTENB2
);
1638 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597
));
1643 if (mask1
& ATTCH
) {
1644 r8a66597_write(r8a66597
, ~ATTCH
, INTSTS1
);
1645 r8a66597_bclr(r8a66597
, ATTCHE
, INTENB1
);
1647 /* start usb bus sampling */
1648 start_root_hub_sampling(r8a66597
, 0, 1);
1651 r8a66597_write(r8a66597
, ~DTCH
, INTSTS1
);
1652 r8a66597_bclr(r8a66597
, DTCHE
, INTENB1
);
1653 r8a66597_usb_disconnect(r8a66597
, 0);
1656 r8a66597_write(r8a66597
, ~BCHG
, INTSTS1
);
1657 r8a66597_bclr(r8a66597
, BCHGE
, INTENB1
);
1658 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597
));
1662 r8a66597_write(r8a66597
, ~SIGN
, INTSTS1
);
1663 status
= get_urb_error(r8a66597
, 0);
1664 check_next_phase(r8a66597
, status
);
1667 r8a66597_write(r8a66597
, ~SACK
, INTSTS1
);
1668 check_next_phase(r8a66597
, 0);
1673 irq_pipe_ready(r8a66597
);
1675 irq_pipe_empty(r8a66597
);
1677 irq_pipe_nrdy(r8a66597
);
1680 spin_unlock(&r8a66597
->lock
);
1684 /* this function must be called with interrupt disabled */
1685 static void r8a66597_root_hub_control(struct r8a66597
*r8a66597
, int port
)
1688 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
1690 if (rh
->port
& USB_PORT_STAT_RESET
) {
1691 unsigned long dvstctr_reg
= get_dvstctr_reg(port
);
1693 tmp
= r8a66597_read(r8a66597
, dvstctr_reg
);
1694 if ((tmp
& USBRST
) == USBRST
) {
1695 r8a66597_mdfy(r8a66597
, UACT
, USBRST
| UACT
,
1697 r8a66597_root_hub_start_polling(r8a66597
);
1699 r8a66597_usb_connect(r8a66597
, port
);
1702 if (!(rh
->port
& USB_PORT_STAT_CONNECTION
)) {
1703 r8a66597_write(r8a66597
, ~ATTCH
, get_intsts_reg(port
));
1704 r8a66597_bset(r8a66597
, ATTCHE
, get_intenb_reg(port
));
1707 if (rh
->scount
> 0) {
1708 tmp
= r8a66597_read(r8a66597
, get_syssts_reg(port
)) & LNST
;
1709 if (tmp
== rh
->old_syssts
) {
1711 if (rh
->scount
== 0)
1712 r8a66597_check_syssts(r8a66597
, port
, tmp
);
1714 r8a66597_root_hub_start_polling(r8a66597
);
1716 rh
->scount
= R8A66597_MAX_SAMPLING
;
1717 rh
->old_syssts
= tmp
;
1718 r8a66597_root_hub_start_polling(r8a66597
);
1723 static void r8a66597_interval_timer(struct timer_list
*t
)
1725 struct r8a66597_timers
*timers
= from_timer(timers
, t
, interval
);
1726 struct r8a66597
*r8a66597
= timers
->r8a66597
;
1727 unsigned long flags
;
1729 struct r8a66597_td
*td
;
1731 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1733 for (pipenum
= 0; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1734 if (!(r8a66597
->interval_map
& (1 << pipenum
)))
1736 if (timer_pending(&r8a66597
->timers
[pipenum
].interval
))
1739 td
= r8a66597_get_td(r8a66597
, pipenum
);
1741 start_transfer(r8a66597
, td
);
1744 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1747 static void r8a66597_td_timer(struct timer_list
*t
)
1749 struct r8a66597_timers
*timers
= from_timer(timers
, t
, td
);
1750 struct r8a66597
*r8a66597
= timers
->r8a66597
;
1751 unsigned long flags
;
1753 struct r8a66597_td
*td
, *new_td
= NULL
;
1754 struct r8a66597_pipe
*pipe
;
1756 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1757 for (pipenum
= 0; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1758 if (!(r8a66597
->timeout_map
& (1 << pipenum
)))
1760 if (timer_pending(&r8a66597
->timers
[pipenum
].td
))
1763 td
= r8a66597_get_td(r8a66597
, pipenum
);
1765 r8a66597
->timeout_map
&= ~(1 << pipenum
);
1769 if (td
->urb
->actual_length
) {
1770 set_td_timer(r8a66597
, td
);
1775 pipe_stop(r8a66597
, pipe
);
1777 /* Select a different address or endpoint */
1780 list_move_tail(&new_td
->queue
,
1781 &r8a66597
->pipe_queue
[pipenum
]);
1782 new_td
= r8a66597_get_td(r8a66597
, pipenum
);
1787 } while (td
!= new_td
&& td
->address
== new_td
->address
&&
1788 td
->pipe
->info
.epnum
== new_td
->pipe
->info
.epnum
);
1790 start_transfer(r8a66597
, new_td
);
1793 r8a66597
->timeout_map
&= ~(1 << pipenum
);
1795 set_td_timer(r8a66597
, new_td
);
1798 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1801 static void r8a66597_timer(struct timer_list
*t
)
1803 struct r8a66597
*r8a66597
= from_timer(r8a66597
, t
, rh_timer
);
1804 unsigned long flags
;
1807 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1809 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++)
1810 r8a66597_root_hub_control(r8a66597
, port
);
1812 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1815 static int check_pipe_config(struct r8a66597
*r8a66597
, struct urb
*urb
)
1817 struct r8a66597_device
*dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
1819 if (dev
&& dev
->address
&& dev
->state
!= USB_STATE_CONFIGURED
&&
1820 (urb
->dev
->state
== USB_STATE_CONFIGURED
))
1826 static int r8a66597_start(struct usb_hcd
*hcd
)
1828 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1830 hcd
->state
= HC_STATE_RUNNING
;
1831 return enable_controller(r8a66597
);
1834 static void r8a66597_stop(struct usb_hcd
*hcd
)
1836 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1838 disable_controller(r8a66597
);
1841 static void set_address_zero(struct r8a66597
*r8a66597
, struct urb
*urb
)
1843 unsigned int usb_address
= usb_pipedevice(urb
->pipe
);
1844 u16 root_port
, hub_port
;
1846 if (usb_address
== 0) {
1847 get_port_number(r8a66597
, urb
->dev
->devpath
,
1848 &root_port
, &hub_port
);
1849 set_devadd_reg(r8a66597
, 0,
1850 get_r8a66597_usb_speed(urb
->dev
->speed
),
1851 get_parent_r8a66597_address(r8a66597
, urb
->dev
),
1852 hub_port
, root_port
);
1856 static struct r8a66597_td
*r8a66597_make_td(struct r8a66597
*r8a66597
,
1858 struct usb_host_endpoint
*hep
)
1860 struct r8a66597_td
*td
;
1863 td
= kzalloc(sizeof(struct r8a66597_td
), GFP_ATOMIC
);
1867 pipenum
= r8a66597_get_pipenum(urb
, hep
);
1868 td
->pipenum
= pipenum
;
1869 td
->pipe
= hep
->hcpriv
;
1871 td
->address
= get_urb_to_r8a66597_addr(r8a66597
, urb
);
1872 td
->maxpacket
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1873 !usb_pipein(urb
->pipe
));
1874 if (usb_pipecontrol(urb
->pipe
))
1875 td
->type
= USB_PID_SETUP
;
1876 else if (usb_pipein(urb
->pipe
))
1877 td
->type
= USB_PID_IN
;
1879 td
->type
= USB_PID_OUT
;
1880 INIT_LIST_HEAD(&td
->queue
);
1885 static int r8a66597_urb_enqueue(struct usb_hcd
*hcd
,
1889 struct usb_host_endpoint
*hep
= urb
->ep
;
1890 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1891 struct r8a66597_td
*td
= NULL
;
1892 int ret
, request
= 0;
1893 unsigned long flags
;
1895 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1896 if (!get_urb_to_r8a66597_dev(r8a66597
, urb
)) {
1898 goto error_not_linked
;
1901 ret
= usb_hcd_link_urb_to_ep(hcd
, urb
);
1903 goto error_not_linked
;
1906 hep
->hcpriv
= kzalloc(sizeof(struct r8a66597_pipe
),
1912 set_pipe_reg_addr(hep
->hcpriv
, R8A66597_PIPE_NO_DMA
);
1913 if (usb_pipeendpoint(urb
->pipe
))
1914 init_pipe_info(r8a66597
, urb
, hep
, &hep
->desc
);
1917 if (unlikely(check_pipe_config(r8a66597
, urb
)))
1918 init_pipe_config(r8a66597
, urb
);
1920 set_address_zero(r8a66597
, urb
);
1921 td
= r8a66597_make_td(r8a66597
, urb
, hep
);
1926 if (list_empty(&r8a66597
->pipe_queue
[td
->pipenum
]))
1928 list_add_tail(&td
->queue
, &r8a66597
->pipe_queue
[td
->pipenum
]);
1932 if (td
->pipe
->info
.timer_interval
) {
1933 r8a66597
->interval_map
|= 1 << td
->pipenum
;
1934 mod_timer(&r8a66597
->timers
[td
->pipenum
].interval
,
1935 jiffies
+ msecs_to_jiffies(
1936 td
->pipe
->info
.timer_interval
));
1938 ret
= start_transfer(r8a66597
, td
);
1940 list_del(&td
->queue
);
1945 set_td_timer(r8a66597
, td
);
1949 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
1951 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1955 static int r8a66597_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
,
1958 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1959 struct r8a66597_td
*td
;
1960 unsigned long flags
;
1963 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1964 rc
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
1970 pipe_stop(r8a66597
, td
->pipe
);
1971 pipe_irq_disable(r8a66597
, td
->pipenum
);
1972 disable_irq_empty(r8a66597
, td
->pipenum
);
1973 finish_request(r8a66597
, td
, td
->pipenum
, urb
, status
);
1976 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1980 static void r8a66597_endpoint_disable(struct usb_hcd
*hcd
,
1981 struct usb_host_endpoint
*hep
)
1982 __acquires(r8a66597
->lock
)
1983 __releases(r8a66597
->lock
)
1985 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1986 struct r8a66597_pipe
*pipe
= (struct r8a66597_pipe
*)hep
->hcpriv
;
1987 struct r8a66597_td
*td
;
1988 struct urb
*urb
= NULL
;
1990 unsigned long flags
;
1994 pipenum
= pipe
->info
.pipenum
;
1996 spin_lock_irqsave(&r8a66597
->lock
, flags
);
2000 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
2004 pipe_stop(r8a66597
, pipe
);
2005 pipe_irq_disable(r8a66597
, pipenum
);
2006 disable_irq_empty(r8a66597
, pipenum
);
2007 td
= r8a66597_get_td(r8a66597
, pipenum
);
2010 finish_request(r8a66597
, td
, pipenum
, urb
, -ESHUTDOWN
);
2013 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
2016 static int r8a66597_get_frame(struct usb_hcd
*hcd
)
2018 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2019 return r8a66597_read(r8a66597
, FRMNUM
) & 0x03FF;
2022 static void collect_usb_address_map(struct usb_device
*udev
, unsigned long *map
)
2025 struct usb_device
*childdev
;
2027 if (udev
->state
== USB_STATE_CONFIGURED
&&
2028 udev
->parent
&& udev
->parent
->devnum
> 1 &&
2029 udev
->parent
->descriptor
.bDeviceClass
== USB_CLASS_HUB
)
2030 map
[udev
->devnum
/32] |= (1 << (udev
->devnum
% 32));
2032 usb_hub_for_each_child(udev
, chix
, childdev
)
2033 collect_usb_address_map(childdev
, map
);
2036 /* this function must be called with interrupt disabled */
2037 static struct r8a66597_device
*get_r8a66597_device(struct r8a66597
*r8a66597
,
2040 struct r8a66597_device
*dev
;
2041 struct list_head
*list
= &r8a66597
->child_device
;
2043 list_for_each_entry(dev
, list
, device_list
) {
2044 if (dev
->usb_address
!= addr
)
2050 printk(KERN_ERR
"r8a66597: get_r8a66597_device fail.(%d)\n", addr
);
2054 static void update_usb_address_map(struct r8a66597
*r8a66597
,
2055 struct usb_device
*root_hub
,
2060 unsigned long flags
;
2062 for (i
= 0; i
< 4; i
++) {
2063 diff
= r8a66597
->child_connect_map
[i
] ^ map
[i
];
2067 for (j
= 0; j
< 32; j
++) {
2068 if (!(diff
& (1 << j
)))
2072 if (map
[i
] & (1 << j
))
2073 set_child_connect_map(r8a66597
, addr
);
2075 struct r8a66597_device
*dev
;
2077 spin_lock_irqsave(&r8a66597
->lock
, flags
);
2078 dev
= get_r8a66597_device(r8a66597
, addr
);
2079 disable_r8a66597_pipe_all(r8a66597
, dev
);
2080 free_usb_address(r8a66597
, dev
, 0);
2081 put_child_connect_map(r8a66597
, addr
);
2082 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
2088 static void r8a66597_check_detect_child(struct r8a66597
*r8a66597
,
2089 struct usb_hcd
*hcd
)
2091 struct usb_bus
*bus
;
2092 unsigned long now_map
[4];
2094 memset(now_map
, 0, sizeof(now_map
));
2096 mutex_lock(&usb_bus_idr_lock
);
2097 bus
= idr_find(&usb_bus_idr
, hcd
->self
.busnum
);
2098 if (bus
&& bus
->root_hub
) {
2099 collect_usb_address_map(bus
->root_hub
, now_map
);
2100 update_usb_address_map(r8a66597
, bus
->root_hub
, now_map
);
2102 mutex_unlock(&usb_bus_idr_lock
);
2105 static int r8a66597_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
2107 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2108 unsigned long flags
;
2111 r8a66597_check_detect_child(r8a66597
, hcd
);
2113 spin_lock_irqsave(&r8a66597
->lock
, flags
);
2115 *buf
= 0; /* initialize (no change) */
2117 for (i
= 0; i
< r8a66597
->max_root_hub
; i
++) {
2118 if (r8a66597
->root_hub
[i
].port
& 0xffff0000)
2119 *buf
|= 1 << (i
+ 1);
2122 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
2127 static void r8a66597_hub_descriptor(struct r8a66597
*r8a66597
,
2128 struct usb_hub_descriptor
*desc
)
2130 desc
->bDescriptorType
= USB_DT_HUB
;
2131 desc
->bHubContrCurrent
= 0;
2132 desc
->bNbrPorts
= r8a66597
->max_root_hub
;
2133 desc
->bDescLength
= 9;
2134 desc
->bPwrOn2PwrGood
= 0;
2135 desc
->wHubCharacteristics
=
2136 cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM
| HUB_CHAR_NO_OCPM
);
2137 desc
->u
.hs
.DeviceRemovable
[0] =
2138 ((1 << r8a66597
->max_root_hub
) - 1) << 1;
2139 desc
->u
.hs
.DeviceRemovable
[1] = ~0;
2142 static int r8a66597_hub_control(struct usb_hcd
*hcd
, u16 typeReq
, u16 wValue
,
2143 u16 wIndex
, char *buf
, u16 wLength
)
2145 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2147 int port
= (wIndex
& 0x00FF) - 1;
2148 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
2149 unsigned long flags
;
2153 spin_lock_irqsave(&r8a66597
->lock
, flags
);
2155 case ClearHubFeature
:
2158 case C_HUB_OVER_CURRENT
:
2159 case C_HUB_LOCAL_POWER
:
2165 case ClearPortFeature
:
2166 if (wIndex
> r8a66597
->max_root_hub
)
2172 case USB_PORT_FEAT_ENABLE
:
2173 rh
->port
&= ~USB_PORT_STAT_POWER
;
2175 case USB_PORT_FEAT_SUSPEND
:
2177 case USB_PORT_FEAT_POWER
:
2178 r8a66597_port_power(r8a66597
, port
, 0);
2180 case USB_PORT_FEAT_C_ENABLE
:
2181 case USB_PORT_FEAT_C_SUSPEND
:
2182 case USB_PORT_FEAT_C_CONNECTION
:
2183 case USB_PORT_FEAT_C_OVER_CURRENT
:
2184 case USB_PORT_FEAT_C_RESET
:
2189 rh
->port
&= ~(1 << wValue
);
2191 case GetHubDescriptor
:
2192 r8a66597_hub_descriptor(r8a66597
,
2193 (struct usb_hub_descriptor
*)buf
);
2199 if (wIndex
> r8a66597
->max_root_hub
)
2201 *(__le32
*)buf
= cpu_to_le32(rh
->port
);
2203 case SetPortFeature
:
2204 if (wIndex
> r8a66597
->max_root_hub
)
2210 case USB_PORT_FEAT_SUSPEND
:
2212 case USB_PORT_FEAT_POWER
:
2213 r8a66597_port_power(r8a66597
, port
, 1);
2214 rh
->port
|= USB_PORT_STAT_POWER
;
2216 case USB_PORT_FEAT_RESET
: {
2217 struct r8a66597_device
*dev
= rh
->dev
;
2219 rh
->port
|= USB_PORT_STAT_RESET
;
2221 disable_r8a66597_pipe_all(r8a66597
, dev
);
2222 free_usb_address(r8a66597
, dev
, 1);
2224 r8a66597_mdfy(r8a66597
, USBRST
, USBRST
| UACT
,
2225 get_dvstctr_reg(port
));
2226 mod_timer(&r8a66597
->rh_timer
,
2227 jiffies
+ msecs_to_jiffies(50));
2233 rh
->port
|= 1 << wValue
;
2241 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
2245 #if defined(CONFIG_PM)
2246 static int r8a66597_bus_suspend(struct usb_hcd
*hcd
)
2248 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2251 dev_dbg(&r8a66597
->device0
.udev
->dev
, "%s\n", __func__
);
2253 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++) {
2254 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
2255 unsigned long dvstctr_reg
= get_dvstctr_reg(port
);
2257 if (!(rh
->port
& USB_PORT_STAT_ENABLE
))
2260 dev_dbg(&rh
->dev
->udev
->dev
, "suspend port = %d\n", port
);
2261 r8a66597_bclr(r8a66597
, UACT
, dvstctr_reg
); /* suspend */
2262 rh
->port
|= USB_PORT_STAT_SUSPEND
;
2264 if (rh
->dev
->udev
->do_remote_wakeup
) {
2265 msleep(3); /* waiting last SOF */
2266 r8a66597_bset(r8a66597
, RWUPE
, dvstctr_reg
);
2267 r8a66597_write(r8a66597
, ~BCHG
, get_intsts_reg(port
));
2268 r8a66597_bset(r8a66597
, BCHGE
, get_intenb_reg(port
));
2272 r8a66597
->bus_suspended
= 1;
2277 static int r8a66597_bus_resume(struct usb_hcd
*hcd
)
2279 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2282 dev_dbg(&r8a66597
->device0
.udev
->dev
, "%s\n", __func__
);
2284 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++) {
2285 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
2286 unsigned long dvstctr_reg
= get_dvstctr_reg(port
);
2288 if (!(rh
->port
& USB_PORT_STAT_SUSPEND
))
2291 dev_dbg(&rh
->dev
->udev
->dev
, "resume port = %d\n", port
);
2292 rh
->port
&= ~USB_PORT_STAT_SUSPEND
;
2293 rh
->port
|= USB_PORT_STAT_C_SUSPEND
<< 16;
2294 r8a66597_mdfy(r8a66597
, RESUME
, RESUME
| UACT
, dvstctr_reg
);
2295 msleep(USB_RESUME_TIMEOUT
);
2296 r8a66597_mdfy(r8a66597
, UACT
, RESUME
| UACT
, dvstctr_reg
);
2303 #define r8a66597_bus_suspend NULL
2304 #define r8a66597_bus_resume NULL
2307 static const struct hc_driver r8a66597_hc_driver
= {
2308 .description
= hcd_name
,
2309 .hcd_priv_size
= sizeof(struct r8a66597
),
2310 .irq
= r8a66597_irq
,
2313 * generic hardware linkage
2317 .start
= r8a66597_start
,
2318 .stop
= r8a66597_stop
,
2321 * managing i/o requests and associated device resources
2323 .urb_enqueue
= r8a66597_urb_enqueue
,
2324 .urb_dequeue
= r8a66597_urb_dequeue
,
2325 .endpoint_disable
= r8a66597_endpoint_disable
,
2328 * periodic schedule support
2330 .get_frame_number
= r8a66597_get_frame
,
2335 .hub_status_data
= r8a66597_hub_status_data
,
2336 .hub_control
= r8a66597_hub_control
,
2337 .bus_suspend
= r8a66597_bus_suspend
,
2338 .bus_resume
= r8a66597_bus_resume
,
2341 #if defined(CONFIG_PM)
2342 static int r8a66597_suspend(struct device
*dev
)
2344 struct r8a66597
*r8a66597
= dev_get_drvdata(dev
);
2347 dev_dbg(dev
, "%s\n", __func__
);
2349 disable_controller(r8a66597
);
2351 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++) {
2352 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
2354 rh
->port
= 0x00000000;
2360 static int r8a66597_resume(struct device
*dev
)
2362 struct r8a66597
*r8a66597
= dev_get_drvdata(dev
);
2363 struct usb_hcd
*hcd
= r8a66597_to_hcd(r8a66597
);
2365 dev_dbg(dev
, "%s\n", __func__
);
2367 enable_controller(r8a66597
);
2368 usb_root_hub_lost_power(hcd
->self
.root_hub
);
2373 static const struct dev_pm_ops r8a66597_dev_pm_ops
= {
2374 .suspend
= r8a66597_suspend
,
2375 .resume
= r8a66597_resume
,
2376 .poweroff
= r8a66597_suspend
,
2377 .restore
= r8a66597_resume
,
2380 #define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
2381 #else /* if defined(CONFIG_PM) */
2382 #define R8A66597_DEV_PM_OPS NULL
2385 static int r8a66597_remove(struct platform_device
*pdev
)
2387 struct r8a66597
*r8a66597
= platform_get_drvdata(pdev
);
2388 struct usb_hcd
*hcd
= r8a66597_to_hcd(r8a66597
);
2390 del_timer_sync(&r8a66597
->rh_timer
);
2391 usb_remove_hcd(hcd
);
2392 iounmap(r8a66597
->reg
);
2393 if (r8a66597
->pdata
->on_chip
)
2394 clk_put(r8a66597
->clk
);
2399 static int r8a66597_probe(struct platform_device
*pdev
)
2402 struct resource
*res
= NULL
, *ires
;
2404 void __iomem
*reg
= NULL
;
2405 struct usb_hcd
*hcd
= NULL
;
2406 struct r8a66597
*r8a66597
;
2409 unsigned long irq_trigger
;
2414 if (pdev
->dev
.dma_mask
) {
2416 dev_err(&pdev
->dev
, "dma not supported\n");
2420 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2423 dev_err(&pdev
->dev
, "platform_get_resource error.\n");
2427 ires
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
2431 "platform_get_resource IORESOURCE_IRQ error.\n");
2436 irq_trigger
= ires
->flags
& IRQF_TRIGGER_MASK
;
2438 reg
= ioremap(res
->start
, resource_size(res
));
2441 dev_err(&pdev
->dev
, "ioremap error.\n");
2445 if (pdev
->dev
.platform_data
== NULL
) {
2446 dev_err(&pdev
->dev
, "no platform data\n");
2451 /* initialize hcd */
2452 hcd
= usb_create_hcd(&r8a66597_hc_driver
, &pdev
->dev
, (char *)hcd_name
);
2455 dev_err(&pdev
->dev
, "Failed to create hcd\n");
2458 r8a66597
= hcd_to_r8a66597(hcd
);
2459 memset(r8a66597
, 0, sizeof(struct r8a66597
));
2460 platform_set_drvdata(pdev
, r8a66597
);
2461 r8a66597
->pdata
= dev_get_platdata(&pdev
->dev
);
2462 r8a66597
->irq_sense_low
= irq_trigger
== IRQF_TRIGGER_LOW
;
2464 if (r8a66597
->pdata
->on_chip
) {
2465 snprintf(clk_name
, sizeof(clk_name
), "usb%d", pdev
->id
);
2466 r8a66597
->clk
= clk_get(&pdev
->dev
, clk_name
);
2467 if (IS_ERR(r8a66597
->clk
)) {
2468 dev_err(&pdev
->dev
, "cannot get clock \"%s\"\n",
2470 ret
= PTR_ERR(r8a66597
->clk
);
2473 r8a66597
->max_root_hub
= 1;
2475 r8a66597
->max_root_hub
= 2;
2477 spin_lock_init(&r8a66597
->lock
);
2478 timer_setup(&r8a66597
->rh_timer
, r8a66597_timer
, 0);
2479 r8a66597
->reg
= reg
;
2481 /* make sure no interrupts are pending */
2482 ret
= r8a66597_clock_enable(r8a66597
);
2485 disable_controller(r8a66597
);
2487 for (i
= 0; i
< R8A66597_MAX_NUM_PIPE
; i
++) {
2488 INIT_LIST_HEAD(&r8a66597
->pipe_queue
[i
]);
2489 r8a66597
->timers
[i
].r8a66597
= r8a66597
;
2490 timer_setup(&r8a66597
->timers
[i
].td
, r8a66597_td_timer
, 0);
2491 timer_setup(&r8a66597
->timers
[i
].interval
,
2492 r8a66597_interval_timer
, 0);
2494 INIT_LIST_HEAD(&r8a66597
->child_device
);
2496 hcd
->rsrc_start
= res
->start
;
2499 ret
= usb_add_hcd(hcd
, irq
, irq_trigger
);
2501 dev_err(&pdev
->dev
, "Failed to add hcd\n");
2504 device_wakeup_enable(hcd
->self
.controller
);
2509 if (r8a66597
->pdata
->on_chip
)
2510 clk_put(r8a66597
->clk
);
2521 static struct platform_driver r8a66597_driver
= {
2522 .probe
= r8a66597_probe
,
2523 .remove
= r8a66597_remove
,
2526 .pm
= R8A66597_DEV_PM_OPS
,
2530 module_platform_driver(r8a66597_driver
);