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 <yoshihiro.shimoda.uh@renesas.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/timer.h>
32 #include <linux/delay.h>
33 #include <linux/list.h>
34 #include <linux/interrupt.h>
35 #include <linux/usb.h>
36 #include <linux/usb/hcd.h>
37 #include <linux/platform_device.h>
40 #include <linux/irq.h>
41 #include <linux/slab.h>
42 #include <asm/cacheflush.h>
46 MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
47 MODULE_LICENSE("GPL");
48 MODULE_AUTHOR("Yoshihiro Shimoda");
49 MODULE_ALIAS("platform:r8a66597_hcd");
51 #define DRIVER_VERSION "2009-05-26"
53 static const char hcd_name
[] = "r8a66597_hcd";
55 static void packet_write(struct r8a66597
*r8a66597
, u16 pipenum
);
56 static int r8a66597_get_frame(struct usb_hcd
*hcd
);
58 /* this function must be called with interrupt disabled */
59 static void enable_pipe_irq(struct r8a66597
*r8a66597
, u16 pipenum
,
64 tmp
= r8a66597_read(r8a66597
, INTENB0
);
65 r8a66597_bclr(r8a66597
, BEMPE
| NRDYE
| BRDYE
, INTENB0
);
66 r8a66597_bset(r8a66597
, 1 << pipenum
, reg
);
67 r8a66597_write(r8a66597
, tmp
, INTENB0
);
70 /* this function must be called with interrupt disabled */
71 static void disable_pipe_irq(struct r8a66597
*r8a66597
, u16 pipenum
,
76 tmp
= r8a66597_read(r8a66597
, INTENB0
);
77 r8a66597_bclr(r8a66597
, BEMPE
| NRDYE
| BRDYE
, INTENB0
);
78 r8a66597_bclr(r8a66597
, 1 << pipenum
, reg
);
79 r8a66597_write(r8a66597
, tmp
, INTENB0
);
82 static void set_devadd_reg(struct r8a66597
*r8a66597
, u8 r8a66597_address
,
83 u16 usbspd
, u8 upphub
, u8 hubport
, int port
)
86 unsigned long devadd_reg
= get_devadd_addr(r8a66597_address
);
88 val
= (upphub
<< 11) | (hubport
<< 8) | (usbspd
<< 6) | (port
& 0x0001);
89 r8a66597_write(r8a66597
, val
, devadd_reg
);
92 static int r8a66597_clock_enable(struct r8a66597
*r8a66597
)
97 if (r8a66597
->pdata
->on_chip
) {
98 clk_enable(r8a66597
->clk
);
100 r8a66597_write(r8a66597
, SCKE
, SYSCFG0
);
101 tmp
= r8a66597_read(r8a66597
, SYSCFG0
);
103 printk(KERN_ERR
"r8a66597: reg access fail.\n");
106 } while ((tmp
& SCKE
) != SCKE
);
107 r8a66597_write(r8a66597
, 0x04, 0x02);
110 r8a66597_write(r8a66597
, USBE
, SYSCFG0
);
111 tmp
= r8a66597_read(r8a66597
, SYSCFG0
);
113 printk(KERN_ERR
"r8a66597: reg access fail.\n");
116 } while ((tmp
& USBE
) != USBE
);
117 r8a66597_bclr(r8a66597
, USBE
, SYSCFG0
);
118 r8a66597_mdfy(r8a66597
, get_xtal_from_pdata(r8a66597
->pdata
),
122 r8a66597_bset(r8a66597
, XCKE
, SYSCFG0
);
125 tmp
= r8a66597_read(r8a66597
, SYSCFG0
);
127 printk(KERN_ERR
"r8a66597: reg access fail.\n");
130 } while ((tmp
& SCKE
) != SCKE
);
136 static void r8a66597_clock_disable(struct r8a66597
*r8a66597
)
138 r8a66597_bclr(r8a66597
, SCKE
, SYSCFG0
);
141 if (r8a66597
->pdata
->on_chip
) {
142 clk_disable(r8a66597
->clk
);
144 r8a66597_bclr(r8a66597
, PLLC
, SYSCFG0
);
145 r8a66597_bclr(r8a66597
, XCKE
, SYSCFG0
);
146 r8a66597_bclr(r8a66597
, USBE
, SYSCFG0
);
150 static void r8a66597_enable_port(struct r8a66597
*r8a66597
, int port
)
154 val
= port
? DRPD
: DCFM
| DRPD
;
155 r8a66597_bset(r8a66597
, val
, get_syscfg_reg(port
));
156 r8a66597_bset(r8a66597
, HSE
, get_syscfg_reg(port
));
158 r8a66597_write(r8a66597
, BURST
| CPU_ADR_RD_WR
, get_dmacfg_reg(port
));
159 r8a66597_bclr(r8a66597
, DTCHE
, get_intenb_reg(port
));
160 r8a66597_bset(r8a66597
, ATTCHE
, get_intenb_reg(port
));
163 static void r8a66597_disable_port(struct r8a66597
*r8a66597
, int port
)
167 r8a66597_write(r8a66597
, 0, get_intenb_reg(port
));
168 r8a66597_write(r8a66597
, 0, get_intsts_reg(port
));
170 r8a66597_port_power(r8a66597
, port
, 0);
173 tmp
= r8a66597_read(r8a66597
, SOFCFG
) & EDGESTS
;
175 } while (tmp
== EDGESTS
);
177 val
= port
? DRPD
: DCFM
| DRPD
;
178 r8a66597_bclr(r8a66597
, val
, get_syscfg_reg(port
));
179 r8a66597_bclr(r8a66597
, HSE
, get_syscfg_reg(port
));
182 static int enable_controller(struct r8a66597
*r8a66597
)
185 u16 vif
= r8a66597
->pdata
->vif
? LDRV
: 0;
186 u16 irq_sense
= r8a66597
->irq_sense_low
? INTL
: 0;
187 u16 endian
= r8a66597
->pdata
->endian
? BIGEND
: 0;
189 ret
= r8a66597_clock_enable(r8a66597
);
193 r8a66597_bset(r8a66597
, vif
& LDRV
, PINCFG
);
194 r8a66597_bset(r8a66597
, USBE
, SYSCFG0
);
196 r8a66597_bset(r8a66597
, BEMPE
| NRDYE
| BRDYE
, INTENB0
);
197 r8a66597_bset(r8a66597
, irq_sense
& INTL
, SOFCFG
);
198 r8a66597_bset(r8a66597
, BRDY0
, BRDYENB
);
199 r8a66597_bset(r8a66597
, BEMP0
, BEMPENB
);
201 r8a66597_bset(r8a66597
, endian
& BIGEND
, CFIFOSEL
);
202 r8a66597_bset(r8a66597
, endian
& BIGEND
, D0FIFOSEL
);
203 r8a66597_bset(r8a66597
, endian
& BIGEND
, D1FIFOSEL
);
204 r8a66597_bset(r8a66597
, TRNENSEL
, SOFCFG
);
206 r8a66597_bset(r8a66597
, SIGNE
| SACKE
, INTENB1
);
208 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++)
209 r8a66597_enable_port(r8a66597
, port
);
214 static void disable_controller(struct r8a66597
*r8a66597
)
218 /* disable interrupts */
219 r8a66597_write(r8a66597
, 0, INTENB0
);
220 r8a66597_write(r8a66597
, 0, INTENB1
);
221 r8a66597_write(r8a66597
, 0, BRDYENB
);
222 r8a66597_write(r8a66597
, 0, BEMPENB
);
223 r8a66597_write(r8a66597
, 0, NRDYENB
);
226 r8a66597_write(r8a66597
, 0, BRDYSTS
);
227 r8a66597_write(r8a66597
, 0, NRDYSTS
);
228 r8a66597_write(r8a66597
, 0, BEMPSTS
);
230 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++)
231 r8a66597_disable_port(r8a66597
, port
);
233 r8a66597_clock_disable(r8a66597
);
236 static int get_parent_r8a66597_address(struct r8a66597
*r8a66597
,
237 struct usb_device
*udev
)
239 struct r8a66597_device
*dev
;
241 if (udev
->parent
&& udev
->parent
->devnum
!= 1)
244 dev
= dev_get_drvdata(&udev
->dev
);
251 static int is_child_device(char *devpath
)
253 return (devpath
[2] ? 1 : 0);
256 static int is_hub_limit(char *devpath
)
258 return ((strlen(devpath
) >= 4) ? 1 : 0);
261 static void get_port_number(struct r8a66597
*r8a66597
,
262 char *devpath
, u16
*root_port
, u16
*hub_port
)
265 *root_port
= (devpath
[0] & 0x0F) - 1;
266 if (*root_port
>= r8a66597
->max_root_hub
)
267 printk(KERN_ERR
"r8a66597: Illegal root port number.\n");
270 *hub_port
= devpath
[2] & 0x0F;
273 static u16
get_r8a66597_usb_speed(enum usb_device_speed speed
)
288 printk(KERN_ERR
"r8a66597: unknown speed\n");
295 static void set_child_connect_map(struct r8a66597
*r8a66597
, int address
)
300 r8a66597
->child_connect_map
[idx
] |= 1 << (address
% 32);
303 static void put_child_connect_map(struct r8a66597
*r8a66597
, int address
)
308 r8a66597
->child_connect_map
[idx
] &= ~(1 << (address
% 32));
311 static void set_pipe_reg_addr(struct r8a66597_pipe
*pipe
, u8 dma_ch
)
313 u16 pipenum
= pipe
->info
.pipenum
;
314 const unsigned long fifoaddr
[] = {D0FIFO
, D1FIFO
, CFIFO
};
315 const unsigned long fifosel
[] = {D0FIFOSEL
, D1FIFOSEL
, CFIFOSEL
};
316 const unsigned long fifoctr
[] = {D0FIFOCTR
, D1FIFOCTR
, CFIFOCTR
};
318 if (dma_ch
> R8A66597_PIPE_NO_DMA
) /* dma fifo not use? */
319 dma_ch
= R8A66597_PIPE_NO_DMA
;
321 pipe
->fifoaddr
= fifoaddr
[dma_ch
];
322 pipe
->fifosel
= fifosel
[dma_ch
];
323 pipe
->fifoctr
= fifoctr
[dma_ch
];
326 pipe
->pipectr
= DCPCTR
;
328 pipe
->pipectr
= get_pipectr_addr(pipenum
);
330 if (check_bulk_or_isoc(pipenum
)) {
331 pipe
->pipetre
= get_pipetre_addr(pipenum
);
332 pipe
->pipetrn
= get_pipetrn_addr(pipenum
);
339 static struct r8a66597_device
*
340 get_urb_to_r8a66597_dev(struct r8a66597
*r8a66597
, struct urb
*urb
)
342 if (usb_pipedevice(urb
->pipe
) == 0)
343 return &r8a66597
->device0
;
345 return dev_get_drvdata(&urb
->dev
->dev
);
348 static int make_r8a66597_device(struct r8a66597
*r8a66597
,
349 struct urb
*urb
, u8 addr
)
351 struct r8a66597_device
*dev
;
352 int usb_address
= urb
->setup_packet
[2]; /* urb->pipe is address 0 */
354 dev
= kzalloc(sizeof(struct r8a66597_device
), GFP_ATOMIC
);
358 dev_set_drvdata(&urb
->dev
->dev
, dev
);
359 dev
->udev
= urb
->dev
;
361 dev
->usb_address
= usb_address
;
362 dev
->state
= USB_STATE_ADDRESS
;
363 dev
->ep_in_toggle
= 0;
364 dev
->ep_out_toggle
= 0;
365 INIT_LIST_HEAD(&dev
->device_list
);
366 list_add_tail(&dev
->device_list
, &r8a66597
->child_device
);
368 get_port_number(r8a66597
, urb
->dev
->devpath
,
369 &dev
->root_port
, &dev
->hub_port
);
370 if (!is_child_device(urb
->dev
->devpath
))
371 r8a66597
->root_hub
[dev
->root_port
].dev
= dev
;
373 set_devadd_reg(r8a66597
, dev
->address
,
374 get_r8a66597_usb_speed(urb
->dev
->speed
),
375 get_parent_r8a66597_address(r8a66597
, urb
->dev
),
376 dev
->hub_port
, dev
->root_port
);
381 /* this function must be called with interrupt disabled */
382 static u8
alloc_usb_address(struct r8a66597
*r8a66597
, struct urb
*urb
)
384 u8 addr
; /* R8A66597's address */
385 struct r8a66597_device
*dev
;
387 if (is_hub_limit(urb
->dev
->devpath
)) {
388 dev_err(&urb
->dev
->dev
, "External hub limit reached.\n");
392 dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
393 if (dev
&& dev
->state
>= USB_STATE_ADDRESS
)
396 for (addr
= 1; addr
<= R8A66597_MAX_DEVICE
; addr
++) {
397 if (r8a66597
->address_map
& (1 << addr
))
400 dev_dbg(&urb
->dev
->dev
, "alloc_address: r8a66597_addr=%d\n", addr
);
401 r8a66597
->address_map
|= 1 << addr
;
403 if (make_r8a66597_device(r8a66597
, urb
, addr
) < 0)
409 dev_err(&urb
->dev
->dev
,
410 "cannot communicate with a USB device more than 10.(%x)\n",
411 r8a66597
->address_map
);
416 /* this function must be called with interrupt disabled */
417 static void free_usb_address(struct r8a66597
*r8a66597
,
418 struct r8a66597_device
*dev
, int reset
)
425 dev_dbg(&dev
->udev
->dev
, "free_addr: addr=%d\n", dev
->address
);
427 dev
->state
= USB_STATE_DEFAULT
;
428 r8a66597
->address_map
&= ~(1 << dev
->address
);
431 * Only when resetting USB, it is necessary to erase drvdata. When
432 * a usb device with usb hub is disconnect, "dev->udev" is already
433 * freed on usb_desconnect(). So we cannot access the data.
436 dev_set_drvdata(&dev
->udev
->dev
, NULL
);
437 list_del(&dev
->device_list
);
440 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++) {
441 if (r8a66597
->root_hub
[port
].dev
== dev
) {
442 r8a66597
->root_hub
[port
].dev
= NULL
;
448 static void r8a66597_reg_wait(struct r8a66597
*r8a66597
, unsigned long reg
,
455 tmp
= r8a66597_read(r8a66597
, reg
);
457 printk(KERN_ERR
"r8a66597: register%lx, loop %x "
458 "is timeout\n", reg
, loop
);
462 } while ((tmp
& mask
) != loop
);
465 /* this function must be called with interrupt disabled */
466 static void pipe_start(struct r8a66597
*r8a66597
, struct r8a66597_pipe
*pipe
)
470 tmp
= r8a66597_read(r8a66597
, pipe
->pipectr
) & PID
;
471 if ((pipe
->info
.pipenum
!= 0) & ((tmp
& PID_STALL
) != 0)) /* stall? */
472 r8a66597_mdfy(r8a66597
, PID_NAK
, PID
, pipe
->pipectr
);
473 r8a66597_mdfy(r8a66597
, PID_BUF
, PID
, pipe
->pipectr
);
476 /* this function must be called with interrupt disabled */
477 static void pipe_stop(struct r8a66597
*r8a66597
, struct r8a66597_pipe
*pipe
)
481 tmp
= r8a66597_read(r8a66597
, pipe
->pipectr
) & PID
;
482 if ((tmp
& PID_STALL11
) != PID_STALL11
) /* force stall? */
483 r8a66597_mdfy(r8a66597
, PID_STALL
, PID
, pipe
->pipectr
);
484 r8a66597_mdfy(r8a66597
, PID_NAK
, PID
, pipe
->pipectr
);
485 r8a66597_reg_wait(r8a66597
, pipe
->pipectr
, PBUSY
, 0);
488 /* this function must be called with interrupt disabled */
489 static void clear_all_buffer(struct r8a66597
*r8a66597
,
490 struct r8a66597_pipe
*pipe
)
494 if (!pipe
|| pipe
->info
.pipenum
== 0)
497 pipe_stop(r8a66597
, pipe
);
498 r8a66597_bset(r8a66597
, ACLRM
, pipe
->pipectr
);
499 tmp
= r8a66597_read(r8a66597
, pipe
->pipectr
);
500 tmp
= r8a66597_read(r8a66597
, pipe
->pipectr
);
501 tmp
= r8a66597_read(r8a66597
, pipe
->pipectr
);
502 r8a66597_bclr(r8a66597
, ACLRM
, pipe
->pipectr
);
505 /* this function must be called with interrupt disabled */
506 static void r8a66597_pipe_toggle(struct r8a66597
*r8a66597
,
507 struct r8a66597_pipe
*pipe
, int toggle
)
510 r8a66597_bset(r8a66597
, SQSET
, pipe
->pipectr
);
512 r8a66597_bset(r8a66597
, SQCLR
, pipe
->pipectr
);
515 static inline unsigned short mbw_value(struct r8a66597
*r8a66597
)
517 if (r8a66597
->pdata
->on_chip
)
523 /* this function must be called with interrupt disabled */
524 static inline void cfifo_change(struct r8a66597
*r8a66597
, u16 pipenum
)
526 unsigned short mbw
= mbw_value(r8a66597
);
528 r8a66597_mdfy(r8a66597
, mbw
| pipenum
, mbw
| CURPIPE
, CFIFOSEL
);
529 r8a66597_reg_wait(r8a66597
, CFIFOSEL
, CURPIPE
, pipenum
);
532 /* this function must be called with interrupt disabled */
533 static inline void fifo_change_from_pipe(struct r8a66597
*r8a66597
,
534 struct r8a66597_pipe
*pipe
)
536 unsigned short mbw
= mbw_value(r8a66597
);
538 cfifo_change(r8a66597
, 0);
539 r8a66597_mdfy(r8a66597
, mbw
| 0, mbw
| CURPIPE
, D0FIFOSEL
);
540 r8a66597_mdfy(r8a66597
, mbw
| 0, mbw
| CURPIPE
, D1FIFOSEL
);
542 r8a66597_mdfy(r8a66597
, mbw
| pipe
->info
.pipenum
, mbw
| CURPIPE
,
544 r8a66597_reg_wait(r8a66597
, pipe
->fifosel
, CURPIPE
, pipe
->info
.pipenum
);
547 static u16
r8a66597_get_pipenum(struct urb
*urb
, struct usb_host_endpoint
*hep
)
549 struct r8a66597_pipe
*pipe
= hep
->hcpriv
;
551 if (usb_pipeendpoint(urb
->pipe
) == 0)
554 return pipe
->info
.pipenum
;
557 static u16
get_urb_to_r8a66597_addr(struct r8a66597
*r8a66597
, struct urb
*urb
)
559 struct r8a66597_device
*dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
561 return (usb_pipedevice(urb
->pipe
) == 0) ? 0 : dev
->address
;
564 static unsigned short *get_toggle_pointer(struct r8a66597_device
*dev
,
570 return usb_pipein(urb_pipe
) ? &dev
->ep_in_toggle
: &dev
->ep_out_toggle
;
573 /* this function must be called with interrupt disabled */
574 static void pipe_toggle_set(struct r8a66597
*r8a66597
,
575 struct r8a66597_pipe
*pipe
,
576 struct urb
*urb
, int set
)
578 struct r8a66597_device
*dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
579 unsigned char endpoint
= usb_pipeendpoint(urb
->pipe
);
580 unsigned short *toggle
= get_toggle_pointer(dev
, urb
->pipe
);
586 *toggle
|= 1 << endpoint
;
588 *toggle
&= ~(1 << endpoint
);
591 /* this function must be called with interrupt disabled */
592 static void pipe_toggle_save(struct r8a66597
*r8a66597
,
593 struct r8a66597_pipe
*pipe
,
596 if (r8a66597_read(r8a66597
, pipe
->pipectr
) & SQMON
)
597 pipe_toggle_set(r8a66597
, pipe
, urb
, 1);
599 pipe_toggle_set(r8a66597
, pipe
, urb
, 0);
602 /* this function must be called with interrupt disabled */
603 static void pipe_toggle_restore(struct r8a66597
*r8a66597
,
604 struct r8a66597_pipe
*pipe
,
607 struct r8a66597_device
*dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
608 unsigned char endpoint
= usb_pipeendpoint(urb
->pipe
);
609 unsigned short *toggle
= get_toggle_pointer(dev
, urb
->pipe
);
614 r8a66597_pipe_toggle(r8a66597
, pipe
, *toggle
& (1 << endpoint
));
617 /* this function must be called with interrupt disabled */
618 static void pipe_buffer_setting(struct r8a66597
*r8a66597
,
619 struct r8a66597_pipe_info
*info
)
623 if (info
->pipenum
== 0)
626 r8a66597_bset(r8a66597
, ACLRM
, get_pipectr_addr(info
->pipenum
));
627 r8a66597_bclr(r8a66597
, ACLRM
, get_pipectr_addr(info
->pipenum
));
628 r8a66597_write(r8a66597
, info
->pipenum
, PIPESEL
);
631 if (info
->type
== R8A66597_BULK
&& info
->dir_in
)
632 val
|= R8A66597_DBLB
| R8A66597_SHTNAK
;
633 val
|= info
->type
| info
->epnum
;
634 r8a66597_write(r8a66597
, val
, PIPECFG
);
636 r8a66597_write(r8a66597
, (info
->buf_bsize
<< 10) | (info
->bufnum
),
638 r8a66597_write(r8a66597
, make_devsel(info
->address
) | info
->maxpacket
,
640 r8a66597_write(r8a66597
, info
->interval
, PIPEPERI
);
643 /* this function must be called with interrupt disabled */
644 static void pipe_setting(struct r8a66597
*r8a66597
, struct r8a66597_td
*td
)
646 struct r8a66597_pipe_info
*info
;
647 struct urb
*urb
= td
->urb
;
649 if (td
->pipenum
> 0) {
650 info
= &td
->pipe
->info
;
651 cfifo_change(r8a66597
, 0);
652 pipe_buffer_setting(r8a66597
, info
);
654 if (!usb_gettoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
),
655 usb_pipeout(urb
->pipe
)) &&
656 !usb_pipecontrol(urb
->pipe
)) {
657 r8a66597_pipe_toggle(r8a66597
, td
->pipe
, 0);
658 pipe_toggle_set(r8a66597
, td
->pipe
, urb
, 0);
659 clear_all_buffer(r8a66597
, td
->pipe
);
660 usb_settoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
),
661 usb_pipeout(urb
->pipe
), 1);
663 pipe_toggle_restore(r8a66597
, td
->pipe
, urb
);
667 /* this function must be called with interrupt disabled */
668 static u16
get_empty_pipenum(struct r8a66597
*r8a66597
,
669 struct usb_endpoint_descriptor
*ep
)
671 u16 array
[R8A66597_MAX_NUM_PIPE
], i
= 0, min
;
673 memset(array
, 0, sizeof(array
));
674 switch (usb_endpoint_type(ep
)) {
675 case USB_ENDPOINT_XFER_BULK
:
676 if (usb_endpoint_dir_in(ep
))
683 case USB_ENDPOINT_XFER_INT
:
684 if (usb_endpoint_dir_in(ep
)) {
691 case USB_ENDPOINT_XFER_ISOC
:
692 if (usb_endpoint_dir_in(ep
))
698 printk(KERN_ERR
"r8a66597: Illegal type\n");
704 while (array
[i
] != 0) {
705 if (r8a66597
->pipe_cnt
[min
] > r8a66597
->pipe_cnt
[array
[i
]])
713 static u16
get_r8a66597_type(__u8 type
)
718 case USB_ENDPOINT_XFER_BULK
:
719 r8a66597_type
= R8A66597_BULK
;
721 case USB_ENDPOINT_XFER_INT
:
722 r8a66597_type
= R8A66597_INT
;
724 case USB_ENDPOINT_XFER_ISOC
:
725 r8a66597_type
= R8A66597_ISO
;
728 printk(KERN_ERR
"r8a66597: Illegal type\n");
729 r8a66597_type
= 0x0000;
733 return r8a66597_type
;
736 static u16
get_bufnum(u16 pipenum
)
742 else if (check_bulk_or_isoc(pipenum
))
743 bufnum
= 8 + (pipenum
- 1) * R8A66597_BUF_BSIZE
*2;
744 else if (check_interrupt(pipenum
))
745 bufnum
= 4 + (pipenum
- 6);
747 printk(KERN_ERR
"r8a66597: Illegal pipenum (%d)\n", pipenum
);
752 static u16
get_buf_bsize(u16 pipenum
)
758 else if (check_bulk_or_isoc(pipenum
))
759 buf_bsize
= R8A66597_BUF_BSIZE
- 1;
760 else if (check_interrupt(pipenum
))
763 printk(KERN_ERR
"r8a66597: Illegal pipenum (%d)\n", pipenum
);
768 /* this function must be called with interrupt disabled */
769 static void enable_r8a66597_pipe_dma(struct r8a66597
*r8a66597
,
770 struct r8a66597_device
*dev
,
771 struct r8a66597_pipe
*pipe
,
775 struct r8a66597_pipe_info
*info
= &pipe
->info
;
776 unsigned short mbw
= mbw_value(r8a66597
);
778 /* pipe dma is only for external controlles */
779 if (r8a66597
->pdata
->on_chip
)
782 if ((pipe
->info
.pipenum
!= 0) && (info
->type
!= R8A66597_INT
)) {
783 for (i
= 0; i
< R8A66597_MAX_DMA_CHANNEL
; i
++) {
784 if ((r8a66597
->dma_map
& (1 << i
)) != 0)
787 dev_info(&dev
->udev
->dev
,
788 "address %d, EndpointAddress 0x%02x use "
789 "DMA FIFO\n", usb_pipedevice(urb
->pipe
),
791 USB_ENDPOINT_DIR_MASK
+ info
->epnum
794 r8a66597
->dma_map
|= 1 << i
;
795 dev
->dma_map
|= 1 << i
;
796 set_pipe_reg_addr(pipe
, i
);
798 cfifo_change(r8a66597
, 0);
799 r8a66597_mdfy(r8a66597
, mbw
| pipe
->info
.pipenum
,
800 mbw
| CURPIPE
, pipe
->fifosel
);
802 r8a66597_reg_wait(r8a66597
, pipe
->fifosel
, CURPIPE
,
804 r8a66597_bset(r8a66597
, BCLR
, pipe
->fifoctr
);
810 /* this function must be called with interrupt disabled */
811 static void enable_r8a66597_pipe(struct r8a66597
*r8a66597
, struct urb
*urb
,
812 struct usb_host_endpoint
*hep
,
813 struct r8a66597_pipe_info
*info
)
815 struct r8a66597_device
*dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
816 struct r8a66597_pipe
*pipe
= hep
->hcpriv
;
818 dev_dbg(&dev
->udev
->dev
, "enable_pipe:\n");
821 set_pipe_reg_addr(pipe
, R8A66597_PIPE_NO_DMA
);
822 r8a66597
->pipe_cnt
[pipe
->info
.pipenum
]++;
823 dev
->pipe_cnt
[pipe
->info
.pipenum
]++;
825 enable_r8a66597_pipe_dma(r8a66597
, dev
, pipe
, urb
);
828 static void r8a66597_urb_done(struct r8a66597
*r8a66597
, struct urb
*urb
,
830 __releases(r8a66597
->lock
)
831 __acquires(r8a66597
->lock
)
833 if (usb_pipein(urb
->pipe
) && usb_pipetype(urb
->pipe
) != PIPE_CONTROL
) {
836 for (ptr
= urb
->transfer_buffer
;
837 ptr
< urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
839 flush_dcache_page(virt_to_page(ptr
));
842 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597
), urb
);
843 spin_unlock(&r8a66597
->lock
);
844 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597
), urb
, status
);
845 spin_lock(&r8a66597
->lock
);
848 /* this function must be called with interrupt disabled */
849 static void force_dequeue(struct r8a66597
*r8a66597
, u16 pipenum
, u16 address
)
851 struct r8a66597_td
*td
, *next
;
853 struct list_head
*list
= &r8a66597
->pipe_queue
[pipenum
];
855 if (list_empty(list
))
858 list_for_each_entry_safe(td
, next
, list
, queue
) {
859 if (td
->address
!= address
)
863 list_del(&td
->queue
);
867 r8a66597_urb_done(r8a66597
, urb
, -ENODEV
);
873 /* this function must be called with interrupt disabled */
874 static void disable_r8a66597_pipe_all(struct r8a66597
*r8a66597
,
875 struct r8a66597_device
*dev
)
883 for (pipenum
= 1; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
884 if (!dev
->pipe_cnt
[pipenum
])
889 force_dequeue(r8a66597
, 0, dev
->address
);
892 r8a66597
->pipe_cnt
[pipenum
] -= dev
->pipe_cnt
[pipenum
];
893 dev
->pipe_cnt
[pipenum
] = 0;
894 force_dequeue(r8a66597
, pipenum
, dev
->address
);
897 dev_dbg(&dev
->udev
->dev
, "disable_pipe\n");
899 r8a66597
->dma_map
&= ~(dev
->dma_map
);
903 static u16
get_interval(struct urb
*urb
, __u8 interval
)
908 if (urb
->dev
->speed
== USB_SPEED_HIGH
) {
912 time
= interval
? interval
- 1 : 0;
914 if (interval
> 128) {
917 /* calculate the nearest value for PIPEPERI */
918 for (i
= 0; i
< 7; i
++) {
919 if ((1 << i
) < interval
&&
920 (1 << (i
+ 1) > interval
))
929 static unsigned long get_timer_interval(struct urb
*urb
, __u8 interval
)
932 unsigned long time
= 1;
934 if (usb_pipeisoc(urb
->pipe
))
937 if (get_r8a66597_usb_speed(urb
->dev
->speed
) == HSMODE
) {
938 for (i
= 0; i
< (interval
- 1); i
++)
940 time
= time
* 125 / 1000; /* uSOF -> msec */
948 /* this function must be called with interrupt disabled */
949 static void init_pipe_info(struct r8a66597
*r8a66597
, struct urb
*urb
,
950 struct usb_host_endpoint
*hep
,
951 struct usb_endpoint_descriptor
*ep
)
953 struct r8a66597_pipe_info info
;
955 info
.pipenum
= get_empty_pipenum(r8a66597
, ep
);
956 info
.address
= get_urb_to_r8a66597_addr(r8a66597
, urb
);
957 info
.epnum
= usb_endpoint_num(ep
);
958 info
.maxpacket
= usb_endpoint_maxp(ep
);
959 info
.type
= get_r8a66597_type(usb_endpoint_type(ep
));
960 info
.bufnum
= get_bufnum(info
.pipenum
);
961 info
.buf_bsize
= get_buf_bsize(info
.pipenum
);
962 if (info
.type
== R8A66597_BULK
) {
964 info
.timer_interval
= 0;
966 info
.interval
= get_interval(urb
, ep
->bInterval
);
967 info
.timer_interval
= get_timer_interval(urb
, ep
->bInterval
);
969 if (usb_endpoint_dir_in(ep
))
974 enable_r8a66597_pipe(r8a66597
, urb
, hep
, &info
);
977 static void init_pipe_config(struct r8a66597
*r8a66597
, struct urb
*urb
)
979 struct r8a66597_device
*dev
;
981 dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
982 dev
->state
= USB_STATE_CONFIGURED
;
985 static void pipe_irq_enable(struct r8a66597
*r8a66597
, struct urb
*urb
,
988 if (pipenum
== 0 && usb_pipeout(urb
->pipe
))
989 enable_irq_empty(r8a66597
, pipenum
);
991 enable_irq_ready(r8a66597
, pipenum
);
993 if (!usb_pipeisoc(urb
->pipe
))
994 enable_irq_nrdy(r8a66597
, pipenum
);
997 static void pipe_irq_disable(struct r8a66597
*r8a66597
, u16 pipenum
)
999 disable_irq_ready(r8a66597
, pipenum
);
1000 disable_irq_nrdy(r8a66597
, pipenum
);
1003 static void r8a66597_root_hub_start_polling(struct r8a66597
*r8a66597
)
1005 mod_timer(&r8a66597
->rh_timer
,
1006 jiffies
+ msecs_to_jiffies(R8A66597_RH_POLL_TIME
));
1009 static void start_root_hub_sampling(struct r8a66597
*r8a66597
, int port
,
1012 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
1014 rh
->old_syssts
= r8a66597_read(r8a66597
, get_syssts_reg(port
)) & LNST
;
1015 rh
->scount
= R8A66597_MAX_SAMPLING
;
1017 rh
->port
|= USB_PORT_STAT_CONNECTION
;
1019 rh
->port
&= ~USB_PORT_STAT_CONNECTION
;
1020 rh
->port
|= USB_PORT_STAT_C_CONNECTION
<< 16;
1022 r8a66597_root_hub_start_polling(r8a66597
);
1025 /* this function must be called with interrupt disabled */
1026 static void r8a66597_check_syssts(struct r8a66597
*r8a66597
, int port
,
1028 __releases(r8a66597
->lock
)
1029 __acquires(r8a66597
->lock
)
1031 if (syssts
== SE0
) {
1032 r8a66597_write(r8a66597
, ~ATTCH
, get_intsts_reg(port
));
1033 r8a66597_bset(r8a66597
, ATTCHE
, get_intenb_reg(port
));
1035 if (syssts
== FS_JSTS
)
1036 r8a66597_bset(r8a66597
, HSE
, get_syscfg_reg(port
));
1037 else if (syssts
== LS_JSTS
)
1038 r8a66597_bclr(r8a66597
, HSE
, get_syscfg_reg(port
));
1040 r8a66597_write(r8a66597
, ~DTCH
, get_intsts_reg(port
));
1041 r8a66597_bset(r8a66597
, DTCHE
, get_intenb_reg(port
));
1043 if (r8a66597
->bus_suspended
)
1044 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597
));
1047 spin_unlock(&r8a66597
->lock
);
1048 usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597
));
1049 spin_lock(&r8a66597
->lock
);
1052 /* this function must be called with interrupt disabled */
1053 static void r8a66597_usb_connect(struct r8a66597
*r8a66597
, int port
)
1055 u16 speed
= get_rh_usb_speed(r8a66597
, port
);
1056 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
1058 rh
->port
&= ~(USB_PORT_STAT_HIGH_SPEED
| USB_PORT_STAT_LOW_SPEED
);
1059 if (speed
== HSMODE
)
1060 rh
->port
|= USB_PORT_STAT_HIGH_SPEED
;
1061 else if (speed
== LSMODE
)
1062 rh
->port
|= USB_PORT_STAT_LOW_SPEED
;
1064 rh
->port
&= ~USB_PORT_STAT_RESET
;
1065 rh
->port
|= USB_PORT_STAT_ENABLE
;
1068 /* this function must be called with interrupt disabled */
1069 static void r8a66597_usb_disconnect(struct r8a66597
*r8a66597
, int port
)
1071 struct r8a66597_device
*dev
= r8a66597
->root_hub
[port
].dev
;
1073 disable_r8a66597_pipe_all(r8a66597
, dev
);
1074 free_usb_address(r8a66597
, dev
, 0);
1076 start_root_hub_sampling(r8a66597
, port
, 0);
1079 /* this function must be called with interrupt disabled */
1080 static void prepare_setup_packet(struct r8a66597
*r8a66597
,
1081 struct r8a66597_td
*td
)
1084 __le16
*p
= (__le16
*)td
->urb
->setup_packet
;
1085 unsigned long setup_addr
= USBREQ
;
1087 r8a66597_write(r8a66597
, make_devsel(td
->address
) | td
->maxpacket
,
1089 r8a66597_write(r8a66597
, ~(SIGN
| SACK
), INTSTS1
);
1091 for (i
= 0; i
< 4; i
++) {
1092 r8a66597_write(r8a66597
, le16_to_cpu(p
[i
]), setup_addr
);
1095 r8a66597_write(r8a66597
, SUREQ
, DCPCTR
);
1098 /* this function must be called with interrupt disabled */
1099 static void prepare_packet_read(struct r8a66597
*r8a66597
,
1100 struct r8a66597_td
*td
)
1102 struct urb
*urb
= td
->urb
;
1104 if (usb_pipecontrol(urb
->pipe
)) {
1105 r8a66597_bclr(r8a66597
, R8A66597_DIR
, DCPCFG
);
1106 r8a66597_mdfy(r8a66597
, 0, ISEL
| CURPIPE
, CFIFOSEL
);
1107 r8a66597_reg_wait(r8a66597
, CFIFOSEL
, CURPIPE
, 0);
1108 if (urb
->actual_length
== 0) {
1109 r8a66597_pipe_toggle(r8a66597
, td
->pipe
, 1);
1110 r8a66597_write(r8a66597
, BCLR
, CFIFOCTR
);
1112 pipe_irq_disable(r8a66597
, td
->pipenum
);
1113 pipe_start(r8a66597
, td
->pipe
);
1114 pipe_irq_enable(r8a66597
, urb
, td
->pipenum
);
1116 if (urb
->actual_length
== 0) {
1117 pipe_irq_disable(r8a66597
, td
->pipenum
);
1118 pipe_setting(r8a66597
, td
);
1119 pipe_stop(r8a66597
, td
->pipe
);
1120 r8a66597_write(r8a66597
, ~(1 << td
->pipenum
), BRDYSTS
);
1122 if (td
->pipe
->pipetre
) {
1123 r8a66597_write(r8a66597
, TRCLR
,
1125 r8a66597_write(r8a66597
,
1127 (urb
->transfer_buffer_length
,
1130 r8a66597_bset(r8a66597
, TRENB
,
1134 pipe_start(r8a66597
, td
->pipe
);
1135 pipe_irq_enable(r8a66597
, urb
, td
->pipenum
);
1140 /* this function must be called with interrupt disabled */
1141 static void prepare_packet_write(struct r8a66597
*r8a66597
,
1142 struct r8a66597_td
*td
)
1145 struct urb
*urb
= td
->urb
;
1147 if (usb_pipecontrol(urb
->pipe
)) {
1148 pipe_stop(r8a66597
, td
->pipe
);
1149 r8a66597_bset(r8a66597
, R8A66597_DIR
, DCPCFG
);
1150 r8a66597_mdfy(r8a66597
, ISEL
, ISEL
| CURPIPE
, CFIFOSEL
);
1151 r8a66597_reg_wait(r8a66597
, CFIFOSEL
, CURPIPE
, 0);
1152 if (urb
->actual_length
== 0) {
1153 r8a66597_pipe_toggle(r8a66597
, td
->pipe
, 1);
1154 r8a66597_write(r8a66597
, BCLR
, CFIFOCTR
);
1157 if (urb
->actual_length
== 0)
1158 pipe_setting(r8a66597
, td
);
1159 if (td
->pipe
->pipetre
)
1160 r8a66597_bclr(r8a66597
, TRENB
, td
->pipe
->pipetre
);
1162 r8a66597_write(r8a66597
, ~(1 << td
->pipenum
), BRDYSTS
);
1164 fifo_change_from_pipe(r8a66597
, td
->pipe
);
1165 tmp
= r8a66597_read(r8a66597
, td
->pipe
->fifoctr
);
1166 if (unlikely((tmp
& FRDY
) == 0))
1167 pipe_irq_enable(r8a66597
, urb
, td
->pipenum
);
1169 packet_write(r8a66597
, td
->pipenum
);
1170 pipe_start(r8a66597
, td
->pipe
);
1173 /* this function must be called with interrupt disabled */
1174 static void prepare_status_packet(struct r8a66597
*r8a66597
,
1175 struct r8a66597_td
*td
)
1177 struct urb
*urb
= td
->urb
;
1179 r8a66597_pipe_toggle(r8a66597
, td
->pipe
, 1);
1180 pipe_stop(r8a66597
, td
->pipe
);
1182 if (urb
->setup_packet
[0] & USB_ENDPOINT_DIR_MASK
) {
1183 r8a66597_bset(r8a66597
, R8A66597_DIR
, DCPCFG
);
1184 r8a66597_mdfy(r8a66597
, ISEL
, ISEL
| CURPIPE
, CFIFOSEL
);
1185 r8a66597_reg_wait(r8a66597
, CFIFOSEL
, CURPIPE
, 0);
1186 r8a66597_write(r8a66597
, ~BEMP0
, BEMPSTS
);
1187 r8a66597_write(r8a66597
, BCLR
| BVAL
, CFIFOCTR
);
1188 enable_irq_empty(r8a66597
, 0);
1190 r8a66597_bclr(r8a66597
, R8A66597_DIR
, DCPCFG
);
1191 r8a66597_mdfy(r8a66597
, 0, ISEL
| CURPIPE
, CFIFOSEL
);
1192 r8a66597_reg_wait(r8a66597
, CFIFOSEL
, CURPIPE
, 0);
1193 r8a66597_write(r8a66597
, BCLR
, CFIFOCTR
);
1194 enable_irq_ready(r8a66597
, 0);
1196 enable_irq_nrdy(r8a66597
, 0);
1197 pipe_start(r8a66597
, td
->pipe
);
1200 static int is_set_address(unsigned char *setup_packet
)
1202 if (((setup_packet
[0] & USB_TYPE_MASK
) == USB_TYPE_STANDARD
) &&
1203 setup_packet
[1] == USB_REQ_SET_ADDRESS
)
1209 /* this function must be called with interrupt disabled */
1210 static int start_transfer(struct r8a66597
*r8a66597
, struct r8a66597_td
*td
)
1216 if (is_set_address(td
->urb
->setup_packet
)) {
1217 td
->set_address
= 1;
1218 td
->urb
->setup_packet
[2] = alloc_usb_address(r8a66597
,
1220 if (td
->urb
->setup_packet
[2] == 0)
1223 prepare_setup_packet(r8a66597
, td
);
1226 prepare_packet_read(r8a66597
, td
);
1229 prepare_packet_write(r8a66597
, td
);
1232 prepare_status_packet(r8a66597
, td
);
1235 printk(KERN_ERR
"r8a66597: invalid type.\n");
1242 static int check_transfer_finish(struct r8a66597_td
*td
, struct urb
*urb
)
1244 if (usb_pipeisoc(urb
->pipe
)) {
1245 if (urb
->number_of_packets
== td
->iso_cnt
)
1249 /* control or bulk or interrupt */
1250 if ((urb
->transfer_buffer_length
<= urb
->actual_length
) ||
1251 (td
->short_packet
) || (td
->zero_packet
))
1257 /* this function must be called with interrupt disabled */
1258 static void set_td_timer(struct r8a66597
*r8a66597
, struct r8a66597_td
*td
)
1264 if (!list_empty(&r8a66597
->pipe_queue
[td
->pipenum
]) &&
1265 !usb_pipecontrol(td
->urb
->pipe
) && usb_pipein(td
->urb
->pipe
)) {
1266 r8a66597
->timeout_map
|= 1 << td
->pipenum
;
1267 switch (usb_pipetype(td
->urb
->pipe
)) {
1268 case PIPE_INTERRUPT
:
1269 case PIPE_ISOCHRONOUS
:
1277 mod_timer(&r8a66597
->td_timer
[td
->pipenum
],
1278 jiffies
+ msecs_to_jiffies(time
));
1282 /* this function must be called with interrupt disabled */
1283 static void finish_request(struct r8a66597
*r8a66597
, struct r8a66597_td
*td
,
1284 u16 pipenum
, struct urb
*urb
, int status
)
1285 __releases(r8a66597
->lock
) __acquires(r8a66597
->lock
)
1288 struct usb_hcd
*hcd
= r8a66597_to_hcd(r8a66597
);
1290 r8a66597
->timeout_map
&= ~(1 << pipenum
);
1293 if (td
->set_address
&& (status
!= 0 || urb
->unlinked
))
1294 r8a66597
->address_map
&= ~(1 << urb
->setup_packet
[2]);
1296 pipe_toggle_save(r8a66597
, td
->pipe
, urb
);
1297 list_del(&td
->queue
);
1301 if (!list_empty(&r8a66597
->pipe_queue
[pipenum
]))
1305 if (usb_pipeisoc(urb
->pipe
))
1306 urb
->start_frame
= r8a66597_get_frame(hcd
);
1308 r8a66597_urb_done(r8a66597
, urb
, status
);
1312 td
= r8a66597_get_td(r8a66597
, pipenum
);
1316 start_transfer(r8a66597
, td
);
1317 set_td_timer(r8a66597
, td
);
1321 static void packet_read(struct r8a66597
*r8a66597
, u16 pipenum
)
1324 int rcv_len
, bufsize
, urb_len
, size
;
1326 struct r8a66597_td
*td
= r8a66597_get_td(r8a66597
, pipenum
);
1335 fifo_change_from_pipe(r8a66597
, td
->pipe
);
1336 tmp
= r8a66597_read(r8a66597
, td
->pipe
->fifoctr
);
1337 if (unlikely((tmp
& FRDY
) == 0)) {
1338 pipe_stop(r8a66597
, td
->pipe
);
1339 pipe_irq_disable(r8a66597
, pipenum
);
1340 printk(KERN_ERR
"r8a66597: in fifo not ready (%d)\n", pipenum
);
1341 finish_request(r8a66597
, td
, pipenum
, td
->urb
, -EPIPE
);
1345 /* prepare parameters */
1346 rcv_len
= tmp
& DTLN
;
1347 if (usb_pipeisoc(urb
->pipe
)) {
1348 buf
= (u16
*)(urb
->transfer_buffer
+
1349 urb
->iso_frame_desc
[td
->iso_cnt
].offset
);
1350 urb_len
= urb
->iso_frame_desc
[td
->iso_cnt
].length
;
1352 buf
= (void *)urb
->transfer_buffer
+ urb
->actual_length
;
1353 urb_len
= urb
->transfer_buffer_length
- urb
->actual_length
;
1355 bufsize
= min(urb_len
, (int) td
->maxpacket
);
1356 if (rcv_len
<= bufsize
) {
1360 status
= -EOVERFLOW
;
1364 /* update parameters */
1365 urb
->actual_length
+= size
;
1367 td
->zero_packet
= 1;
1368 if (rcv_len
< bufsize
) {
1369 td
->short_packet
= 1;
1371 if (usb_pipeisoc(urb
->pipe
)) {
1372 urb
->iso_frame_desc
[td
->iso_cnt
].actual_length
= size
;
1373 urb
->iso_frame_desc
[td
->iso_cnt
].status
= status
;
1378 /* check transfer finish */
1379 if (finish
|| check_transfer_finish(td
, urb
)) {
1380 pipe_stop(r8a66597
, td
->pipe
);
1381 pipe_irq_disable(r8a66597
, pipenum
);
1386 if (urb
->transfer_buffer
) {
1388 r8a66597_write(r8a66597
, BCLR
, td
->pipe
->fifoctr
);
1390 r8a66597_read_fifo(r8a66597
, td
->pipe
->fifoaddr
,
1394 if (finish
&& pipenum
!= 0)
1395 finish_request(r8a66597
, td
, pipenum
, urb
, status
);
1398 static void packet_write(struct r8a66597
*r8a66597
, u16 pipenum
)
1403 struct r8a66597_td
*td
= r8a66597_get_td(r8a66597
, pipenum
);
1410 fifo_change_from_pipe(r8a66597
, td
->pipe
);
1411 tmp
= r8a66597_read(r8a66597
, td
->pipe
->fifoctr
);
1412 if (unlikely((tmp
& FRDY
) == 0)) {
1413 pipe_stop(r8a66597
, td
->pipe
);
1414 pipe_irq_disable(r8a66597
, pipenum
);
1415 printk(KERN_ERR
"r8a66597: out fifo not ready (%d)\n", pipenum
);
1416 finish_request(r8a66597
, td
, pipenum
, urb
, -EPIPE
);
1420 /* prepare parameters */
1421 bufsize
= td
->maxpacket
;
1422 if (usb_pipeisoc(urb
->pipe
)) {
1423 buf
= (u16
*)(urb
->transfer_buffer
+
1424 urb
->iso_frame_desc
[td
->iso_cnt
].offset
);
1426 (int)urb
->iso_frame_desc
[td
->iso_cnt
].length
);
1428 buf
= (u16
*)(urb
->transfer_buffer
+ urb
->actual_length
);
1429 size
= min_t(u32
, bufsize
,
1430 urb
->transfer_buffer_length
- urb
->actual_length
);
1435 r8a66597_write(r8a66597
, ~(1 << pipenum
), BEMPSTS
);
1436 if (urb
->transfer_buffer
) {
1437 r8a66597_write_fifo(r8a66597
, td
->pipe
, buf
, size
);
1438 if (!usb_pipebulk(urb
->pipe
) || td
->maxpacket
!= size
)
1439 r8a66597_write(r8a66597
, BVAL
, td
->pipe
->fifoctr
);
1442 /* update parameters */
1443 urb
->actual_length
+= size
;
1444 if (usb_pipeisoc(urb
->pipe
)) {
1445 urb
->iso_frame_desc
[td
->iso_cnt
].actual_length
= size
;
1446 urb
->iso_frame_desc
[td
->iso_cnt
].status
= 0;
1450 /* check transfer finish */
1451 if (check_transfer_finish(td
, urb
)) {
1452 disable_irq_ready(r8a66597
, pipenum
);
1453 enable_irq_empty(r8a66597
, pipenum
);
1454 if (!usb_pipeisoc(urb
->pipe
))
1455 enable_irq_nrdy(r8a66597
, pipenum
);
1457 pipe_irq_enable(r8a66597
, urb
, pipenum
);
1461 static void check_next_phase(struct r8a66597
*r8a66597
, int status
)
1463 struct r8a66597_td
*td
= r8a66597_get_td(r8a66597
, 0);
1474 if (check_transfer_finish(td
, urb
))
1475 td
->type
= USB_PID_ACK
;
1478 if (urb
->transfer_buffer_length
== urb
->actual_length
)
1479 td
->type
= USB_PID_ACK
;
1480 else if (usb_pipeout(urb
->pipe
))
1481 td
->type
= USB_PID_OUT
;
1483 td
->type
= USB_PID_IN
;
1490 if (finish
|| status
!= 0 || urb
->unlinked
)
1491 finish_request(r8a66597
, td
, 0, urb
, status
);
1493 start_transfer(r8a66597
, td
);
1496 static int get_urb_error(struct r8a66597
*r8a66597
, u16 pipenum
)
1498 struct r8a66597_td
*td
= r8a66597_get_td(r8a66597
, pipenum
);
1501 u16 pid
= r8a66597_read(r8a66597
, td
->pipe
->pipectr
) & PID
;
1511 static void irq_pipe_ready(struct r8a66597
*r8a66597
)
1516 struct r8a66597_td
*td
;
1518 mask
= r8a66597_read(r8a66597
, BRDYSTS
)
1519 & r8a66597_read(r8a66597
, BRDYENB
);
1520 r8a66597_write(r8a66597
, ~mask
, BRDYSTS
);
1522 td
= r8a66597_get_td(r8a66597
, 0);
1523 if (td
&& td
->type
== USB_PID_IN
)
1524 packet_read(r8a66597
, 0);
1526 pipe_irq_disable(r8a66597
, 0);
1527 check_next_phase(r8a66597
, 0);
1530 for (pipenum
= 1; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1531 check
= 1 << pipenum
;
1533 td
= r8a66597_get_td(r8a66597
, pipenum
);
1537 if (td
->type
== USB_PID_IN
)
1538 packet_read(r8a66597
, pipenum
);
1539 else if (td
->type
== USB_PID_OUT
)
1540 packet_write(r8a66597
, pipenum
);
1545 static void irq_pipe_empty(struct r8a66597
*r8a66597
)
1551 struct r8a66597_td
*td
;
1553 mask
= r8a66597_read(r8a66597
, BEMPSTS
)
1554 & r8a66597_read(r8a66597
, BEMPENB
);
1555 r8a66597_write(r8a66597
, ~mask
, BEMPSTS
);
1557 cfifo_change(r8a66597
, 0);
1558 td
= r8a66597_get_td(r8a66597
, 0);
1559 if (td
&& td
->type
!= USB_PID_OUT
)
1560 disable_irq_empty(r8a66597
, 0);
1561 check_next_phase(r8a66597
, 0);
1564 for (pipenum
= 1; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1565 check
= 1 << pipenum
;
1567 struct r8a66597_td
*td
;
1568 td
= r8a66597_get_td(r8a66597
, pipenum
);
1572 tmp
= r8a66597_read(r8a66597
, td
->pipe
->pipectr
);
1573 if ((tmp
& INBUFM
) == 0) {
1574 disable_irq_empty(r8a66597
, pipenum
);
1575 pipe_irq_disable(r8a66597
, pipenum
);
1576 finish_request(r8a66597
, td
, pipenum
, td
->urb
,
1583 static void irq_pipe_nrdy(struct r8a66597
*r8a66597
)
1590 mask
= r8a66597_read(r8a66597
, NRDYSTS
)
1591 & r8a66597_read(r8a66597
, NRDYENB
);
1592 r8a66597_write(r8a66597
, ~mask
, NRDYSTS
);
1594 cfifo_change(r8a66597
, 0);
1595 status
= get_urb_error(r8a66597
, 0);
1596 pipe_irq_disable(r8a66597
, 0);
1597 check_next_phase(r8a66597
, status
);
1600 for (pipenum
= 1; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1601 check
= 1 << pipenum
;
1603 struct r8a66597_td
*td
;
1604 td
= r8a66597_get_td(r8a66597
, pipenum
);
1608 status
= get_urb_error(r8a66597
, pipenum
);
1609 pipe_irq_disable(r8a66597
, pipenum
);
1610 pipe_stop(r8a66597
, td
->pipe
);
1611 finish_request(r8a66597
, td
, pipenum
, td
->urb
, status
);
1616 static irqreturn_t
r8a66597_irq(struct usb_hcd
*hcd
)
1618 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1619 u16 intsts0
, intsts1
, intsts2
;
1620 u16 intenb0
, intenb1
, intenb2
;
1621 u16 mask0
, mask1
, mask2
;
1624 spin_lock(&r8a66597
->lock
);
1626 intsts0
= r8a66597_read(r8a66597
, INTSTS0
);
1627 intsts1
= r8a66597_read(r8a66597
, INTSTS1
);
1628 intsts2
= r8a66597_read(r8a66597
, INTSTS2
);
1629 intenb0
= r8a66597_read(r8a66597
, INTENB0
);
1630 intenb1
= r8a66597_read(r8a66597
, INTENB1
);
1631 intenb2
= r8a66597_read(r8a66597
, INTENB2
);
1633 mask2
= intsts2
& intenb2
;
1634 mask1
= intsts1
& intenb1
;
1635 mask0
= intsts0
& intenb0
& (BEMP
| NRDY
| BRDY
);
1637 if (mask2
& ATTCH
) {
1638 r8a66597_write(r8a66597
, ~ATTCH
, INTSTS2
);
1639 r8a66597_bclr(r8a66597
, ATTCHE
, INTENB2
);
1641 /* start usb bus sampling */
1642 start_root_hub_sampling(r8a66597
, 1, 1);
1645 r8a66597_write(r8a66597
, ~DTCH
, INTSTS2
);
1646 r8a66597_bclr(r8a66597
, DTCHE
, INTENB2
);
1647 r8a66597_usb_disconnect(r8a66597
, 1);
1650 r8a66597_write(r8a66597
, ~BCHG
, INTSTS2
);
1651 r8a66597_bclr(r8a66597
, BCHGE
, INTENB2
);
1652 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597
));
1657 if (mask1
& ATTCH
) {
1658 r8a66597_write(r8a66597
, ~ATTCH
, INTSTS1
);
1659 r8a66597_bclr(r8a66597
, ATTCHE
, INTENB1
);
1661 /* start usb bus sampling */
1662 start_root_hub_sampling(r8a66597
, 0, 1);
1665 r8a66597_write(r8a66597
, ~DTCH
, INTSTS1
);
1666 r8a66597_bclr(r8a66597
, DTCHE
, INTENB1
);
1667 r8a66597_usb_disconnect(r8a66597
, 0);
1670 r8a66597_write(r8a66597
, ~BCHG
, INTSTS1
);
1671 r8a66597_bclr(r8a66597
, BCHGE
, INTENB1
);
1672 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597
));
1676 r8a66597_write(r8a66597
, ~SIGN
, INTSTS1
);
1677 status
= get_urb_error(r8a66597
, 0);
1678 check_next_phase(r8a66597
, status
);
1681 r8a66597_write(r8a66597
, ~SACK
, INTSTS1
);
1682 check_next_phase(r8a66597
, 0);
1687 irq_pipe_ready(r8a66597
);
1689 irq_pipe_empty(r8a66597
);
1691 irq_pipe_nrdy(r8a66597
);
1694 spin_unlock(&r8a66597
->lock
);
1698 /* this function must be called with interrupt disabled */
1699 static void r8a66597_root_hub_control(struct r8a66597
*r8a66597
, int port
)
1702 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
1704 if (rh
->port
& USB_PORT_STAT_RESET
) {
1705 unsigned long dvstctr_reg
= get_dvstctr_reg(port
);
1707 tmp
= r8a66597_read(r8a66597
, dvstctr_reg
);
1708 if ((tmp
& USBRST
) == USBRST
) {
1709 r8a66597_mdfy(r8a66597
, UACT
, USBRST
| UACT
,
1711 r8a66597_root_hub_start_polling(r8a66597
);
1713 r8a66597_usb_connect(r8a66597
, port
);
1716 if (!(rh
->port
& USB_PORT_STAT_CONNECTION
)) {
1717 r8a66597_write(r8a66597
, ~ATTCH
, get_intsts_reg(port
));
1718 r8a66597_bset(r8a66597
, ATTCHE
, get_intenb_reg(port
));
1721 if (rh
->scount
> 0) {
1722 tmp
= r8a66597_read(r8a66597
, get_syssts_reg(port
)) & LNST
;
1723 if (tmp
== rh
->old_syssts
) {
1725 if (rh
->scount
== 0)
1726 r8a66597_check_syssts(r8a66597
, port
, tmp
);
1728 r8a66597_root_hub_start_polling(r8a66597
);
1730 rh
->scount
= R8A66597_MAX_SAMPLING
;
1731 rh
->old_syssts
= tmp
;
1732 r8a66597_root_hub_start_polling(r8a66597
);
1737 static void r8a66597_interval_timer(unsigned long _r8a66597
)
1739 struct r8a66597
*r8a66597
= (struct r8a66597
*)_r8a66597
;
1740 unsigned long flags
;
1742 struct r8a66597_td
*td
;
1744 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1746 for (pipenum
= 0; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1747 if (!(r8a66597
->interval_map
& (1 << pipenum
)))
1749 if (timer_pending(&r8a66597
->interval_timer
[pipenum
]))
1752 td
= r8a66597_get_td(r8a66597
, pipenum
);
1754 start_transfer(r8a66597
, td
);
1757 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1760 static void r8a66597_td_timer(unsigned long _r8a66597
)
1762 struct r8a66597
*r8a66597
= (struct r8a66597
*)_r8a66597
;
1763 unsigned long flags
;
1765 struct r8a66597_td
*td
, *new_td
= NULL
;
1766 struct r8a66597_pipe
*pipe
;
1768 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1769 for (pipenum
= 0; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1770 if (!(r8a66597
->timeout_map
& (1 << pipenum
)))
1772 if (timer_pending(&r8a66597
->td_timer
[pipenum
]))
1775 td
= r8a66597_get_td(r8a66597
, pipenum
);
1777 r8a66597
->timeout_map
&= ~(1 << pipenum
);
1781 if (td
->urb
->actual_length
) {
1782 set_td_timer(r8a66597
, td
);
1787 pipe_stop(r8a66597
, pipe
);
1791 list_move_tail(&new_td
->queue
,
1792 &r8a66597
->pipe_queue
[pipenum
]);
1793 new_td
= r8a66597_get_td(r8a66597
, pipenum
);
1798 } while (td
!= new_td
&& td
->address
== new_td
->address
);
1800 start_transfer(r8a66597
, new_td
);
1803 r8a66597
->timeout_map
&= ~(1 << pipenum
);
1805 set_td_timer(r8a66597
, new_td
);
1808 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1811 static void r8a66597_timer(unsigned long _r8a66597
)
1813 struct r8a66597
*r8a66597
= (struct r8a66597
*)_r8a66597
;
1814 unsigned long flags
;
1817 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1819 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++)
1820 r8a66597_root_hub_control(r8a66597
, port
);
1822 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1825 static int check_pipe_config(struct r8a66597
*r8a66597
, struct urb
*urb
)
1827 struct r8a66597_device
*dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
1829 if (dev
&& dev
->address
&& dev
->state
!= USB_STATE_CONFIGURED
&&
1830 (urb
->dev
->state
== USB_STATE_CONFIGURED
))
1836 static int r8a66597_start(struct usb_hcd
*hcd
)
1838 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1840 hcd
->state
= HC_STATE_RUNNING
;
1841 return enable_controller(r8a66597
);
1844 static void r8a66597_stop(struct usb_hcd
*hcd
)
1846 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1848 disable_controller(r8a66597
);
1851 static void set_address_zero(struct r8a66597
*r8a66597
, struct urb
*urb
)
1853 unsigned int usb_address
= usb_pipedevice(urb
->pipe
);
1854 u16 root_port
, hub_port
;
1856 if (usb_address
== 0) {
1857 get_port_number(r8a66597
, urb
->dev
->devpath
,
1858 &root_port
, &hub_port
);
1859 set_devadd_reg(r8a66597
, 0,
1860 get_r8a66597_usb_speed(urb
->dev
->speed
),
1861 get_parent_r8a66597_address(r8a66597
, urb
->dev
),
1862 hub_port
, root_port
);
1866 static struct r8a66597_td
*r8a66597_make_td(struct r8a66597
*r8a66597
,
1868 struct usb_host_endpoint
*hep
)
1870 struct r8a66597_td
*td
;
1873 td
= kzalloc(sizeof(struct r8a66597_td
), GFP_ATOMIC
);
1877 pipenum
= r8a66597_get_pipenum(urb
, hep
);
1878 td
->pipenum
= pipenum
;
1879 td
->pipe
= hep
->hcpriv
;
1881 td
->address
= get_urb_to_r8a66597_addr(r8a66597
, urb
);
1882 td
->maxpacket
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1883 !usb_pipein(urb
->pipe
));
1884 if (usb_pipecontrol(urb
->pipe
))
1885 td
->type
= USB_PID_SETUP
;
1886 else if (usb_pipein(urb
->pipe
))
1887 td
->type
= USB_PID_IN
;
1889 td
->type
= USB_PID_OUT
;
1890 INIT_LIST_HEAD(&td
->queue
);
1895 static int r8a66597_urb_enqueue(struct usb_hcd
*hcd
,
1899 struct usb_host_endpoint
*hep
= urb
->ep
;
1900 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1901 struct r8a66597_td
*td
= NULL
;
1902 int ret
, request
= 0;
1903 unsigned long flags
;
1905 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1906 if (!get_urb_to_r8a66597_dev(r8a66597
, urb
)) {
1908 goto error_not_linked
;
1911 ret
= usb_hcd_link_urb_to_ep(hcd
, urb
);
1913 goto error_not_linked
;
1916 hep
->hcpriv
= kzalloc(sizeof(struct r8a66597_pipe
),
1922 set_pipe_reg_addr(hep
->hcpriv
, R8A66597_PIPE_NO_DMA
);
1923 if (usb_pipeendpoint(urb
->pipe
))
1924 init_pipe_info(r8a66597
, urb
, hep
, &hep
->desc
);
1927 if (unlikely(check_pipe_config(r8a66597
, urb
)))
1928 init_pipe_config(r8a66597
, urb
);
1930 set_address_zero(r8a66597
, urb
);
1931 td
= r8a66597_make_td(r8a66597
, urb
, hep
);
1936 if (list_empty(&r8a66597
->pipe_queue
[td
->pipenum
]))
1938 list_add_tail(&td
->queue
, &r8a66597
->pipe_queue
[td
->pipenum
]);
1942 if (td
->pipe
->info
.timer_interval
) {
1943 r8a66597
->interval_map
|= 1 << td
->pipenum
;
1944 mod_timer(&r8a66597
->interval_timer
[td
->pipenum
],
1945 jiffies
+ msecs_to_jiffies(
1946 td
->pipe
->info
.timer_interval
));
1948 ret
= start_transfer(r8a66597
, td
);
1950 list_del(&td
->queue
);
1955 set_td_timer(r8a66597
, td
);
1959 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
1961 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1965 static int r8a66597_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
,
1968 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1969 struct r8a66597_td
*td
;
1970 unsigned long flags
;
1973 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1974 rc
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
1980 pipe_stop(r8a66597
, td
->pipe
);
1981 pipe_irq_disable(r8a66597
, td
->pipenum
);
1982 disable_irq_empty(r8a66597
, td
->pipenum
);
1983 finish_request(r8a66597
, td
, td
->pipenum
, urb
, status
);
1986 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1990 static void r8a66597_endpoint_disable(struct usb_hcd
*hcd
,
1991 struct usb_host_endpoint
*hep
)
1993 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1994 struct r8a66597_pipe
*pipe
= (struct r8a66597_pipe
*)hep
->hcpriv
;
1995 struct r8a66597_td
*td
;
1996 struct urb
*urb
= NULL
;
1998 unsigned long flags
;
2002 pipenum
= pipe
->info
.pipenum
;
2010 spin_lock_irqsave(&r8a66597
->lock
, flags
);
2011 pipe_stop(r8a66597
, pipe
);
2012 pipe_irq_disable(r8a66597
, pipenum
);
2013 disable_irq_empty(r8a66597
, pipenum
);
2014 td
= r8a66597_get_td(r8a66597
, pipenum
);
2017 finish_request(r8a66597
, td
, pipenum
, urb
, -ESHUTDOWN
);
2020 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
2023 static int r8a66597_get_frame(struct usb_hcd
*hcd
)
2025 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2026 return r8a66597_read(r8a66597
, FRMNUM
) & 0x03FF;
2029 static void collect_usb_address_map(struct usb_device
*udev
, unsigned long *map
)
2033 if (udev
->state
== USB_STATE_CONFIGURED
&&
2034 udev
->parent
&& udev
->parent
->devnum
> 1 &&
2035 udev
->parent
->descriptor
.bDeviceClass
== USB_CLASS_HUB
)
2036 map
[udev
->devnum
/32] |= (1 << (udev
->devnum
% 32));
2038 for (chix
= 0; chix
< udev
->maxchild
; chix
++) {
2039 struct usb_device
*childdev
= udev
->children
[chix
];
2042 collect_usb_address_map(childdev
, map
);
2046 /* this function must be called with interrupt disabled */
2047 static struct r8a66597_device
*get_r8a66597_device(struct r8a66597
*r8a66597
,
2050 struct r8a66597_device
*dev
;
2051 struct list_head
*list
= &r8a66597
->child_device
;
2053 list_for_each_entry(dev
, list
, device_list
) {
2054 if (dev
->usb_address
!= addr
)
2060 printk(KERN_ERR
"r8a66597: get_r8a66597_device fail.(%d)\n", addr
);
2064 static void update_usb_address_map(struct r8a66597
*r8a66597
,
2065 struct usb_device
*root_hub
,
2070 unsigned long flags
;
2072 for (i
= 0; i
< 4; i
++) {
2073 diff
= r8a66597
->child_connect_map
[i
] ^ map
[i
];
2077 for (j
= 0; j
< 32; j
++) {
2078 if (!(diff
& (1 << j
)))
2082 if (map
[i
] & (1 << j
))
2083 set_child_connect_map(r8a66597
, addr
);
2085 struct r8a66597_device
*dev
;
2087 spin_lock_irqsave(&r8a66597
->lock
, flags
);
2088 dev
= get_r8a66597_device(r8a66597
, addr
);
2089 disable_r8a66597_pipe_all(r8a66597
, dev
);
2090 free_usb_address(r8a66597
, dev
, 0);
2091 put_child_connect_map(r8a66597
, addr
);
2092 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
2098 static void r8a66597_check_detect_child(struct r8a66597
*r8a66597
,
2099 struct usb_hcd
*hcd
)
2101 struct usb_bus
*bus
;
2102 unsigned long now_map
[4];
2104 memset(now_map
, 0, sizeof(now_map
));
2106 list_for_each_entry(bus
, &usb_bus_list
, bus_list
) {
2110 if (bus
->busnum
!= hcd
->self
.busnum
)
2113 collect_usb_address_map(bus
->root_hub
, now_map
);
2114 update_usb_address_map(r8a66597
, bus
->root_hub
, now_map
);
2118 static int r8a66597_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
2120 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2121 unsigned long flags
;
2124 r8a66597_check_detect_child(r8a66597
, hcd
);
2126 spin_lock_irqsave(&r8a66597
->lock
, flags
);
2128 *buf
= 0; /* initialize (no change) */
2130 for (i
= 0; i
< r8a66597
->max_root_hub
; i
++) {
2131 if (r8a66597
->root_hub
[i
].port
& 0xffff0000)
2132 *buf
|= 1 << (i
+ 1);
2135 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
2140 static void r8a66597_hub_descriptor(struct r8a66597
*r8a66597
,
2141 struct usb_hub_descriptor
*desc
)
2143 desc
->bDescriptorType
= 0x29;
2144 desc
->bHubContrCurrent
= 0;
2145 desc
->bNbrPorts
= r8a66597
->max_root_hub
;
2146 desc
->bDescLength
= 9;
2147 desc
->bPwrOn2PwrGood
= 0;
2148 desc
->wHubCharacteristics
= cpu_to_le16(0x0011);
2149 desc
->u
.hs
.DeviceRemovable
[0] =
2150 ((1 << r8a66597
->max_root_hub
) - 1) << 1;
2151 desc
->u
.hs
.DeviceRemovable
[1] = ~0;
2154 static int r8a66597_hub_control(struct usb_hcd
*hcd
, u16 typeReq
, u16 wValue
,
2155 u16 wIndex
, char *buf
, u16 wLength
)
2157 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2159 int port
= (wIndex
& 0x00FF) - 1;
2160 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
2161 unsigned long flags
;
2165 spin_lock_irqsave(&r8a66597
->lock
, flags
);
2167 case ClearHubFeature
:
2170 case C_HUB_OVER_CURRENT
:
2171 case C_HUB_LOCAL_POWER
:
2177 case ClearPortFeature
:
2178 if (wIndex
> r8a66597
->max_root_hub
)
2184 case USB_PORT_FEAT_ENABLE
:
2185 rh
->port
&= ~USB_PORT_STAT_POWER
;
2187 case USB_PORT_FEAT_SUSPEND
:
2189 case USB_PORT_FEAT_POWER
:
2190 r8a66597_port_power(r8a66597
, port
, 0);
2192 case USB_PORT_FEAT_C_ENABLE
:
2193 case USB_PORT_FEAT_C_SUSPEND
:
2194 case USB_PORT_FEAT_C_CONNECTION
:
2195 case USB_PORT_FEAT_C_OVER_CURRENT
:
2196 case USB_PORT_FEAT_C_RESET
:
2201 rh
->port
&= ~(1 << wValue
);
2203 case GetHubDescriptor
:
2204 r8a66597_hub_descriptor(r8a66597
,
2205 (struct usb_hub_descriptor
*)buf
);
2211 if (wIndex
> r8a66597
->max_root_hub
)
2213 *(__le32
*)buf
= cpu_to_le32(rh
->port
);
2215 case SetPortFeature
:
2216 if (wIndex
> r8a66597
->max_root_hub
)
2222 case USB_PORT_FEAT_SUSPEND
:
2224 case USB_PORT_FEAT_POWER
:
2225 r8a66597_port_power(r8a66597
, port
, 1);
2226 rh
->port
|= USB_PORT_STAT_POWER
;
2228 case USB_PORT_FEAT_RESET
: {
2229 struct r8a66597_device
*dev
= rh
->dev
;
2231 rh
->port
|= USB_PORT_STAT_RESET
;
2233 disable_r8a66597_pipe_all(r8a66597
, dev
);
2234 free_usb_address(r8a66597
, dev
, 1);
2236 r8a66597_mdfy(r8a66597
, USBRST
, USBRST
| UACT
,
2237 get_dvstctr_reg(port
));
2238 mod_timer(&r8a66597
->rh_timer
,
2239 jiffies
+ msecs_to_jiffies(50));
2245 rh
->port
|= 1 << wValue
;
2253 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
2257 #if defined(CONFIG_PM)
2258 static int r8a66597_bus_suspend(struct usb_hcd
*hcd
)
2260 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2263 dev_dbg(&r8a66597
->device0
.udev
->dev
, "%s\n", __func__
);
2265 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++) {
2266 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
2267 unsigned long dvstctr_reg
= get_dvstctr_reg(port
);
2269 if (!(rh
->port
& USB_PORT_STAT_ENABLE
))
2272 dev_dbg(&rh
->dev
->udev
->dev
, "suspend port = %d\n", port
);
2273 r8a66597_bclr(r8a66597
, UACT
, dvstctr_reg
); /* suspend */
2274 rh
->port
|= USB_PORT_STAT_SUSPEND
;
2276 if (rh
->dev
->udev
->do_remote_wakeup
) {
2277 msleep(3); /* waiting last SOF */
2278 r8a66597_bset(r8a66597
, RWUPE
, dvstctr_reg
);
2279 r8a66597_write(r8a66597
, ~BCHG
, get_intsts_reg(port
));
2280 r8a66597_bset(r8a66597
, BCHGE
, get_intenb_reg(port
));
2284 r8a66597
->bus_suspended
= 1;
2289 static int r8a66597_bus_resume(struct usb_hcd
*hcd
)
2291 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2294 dev_dbg(&r8a66597
->device0
.udev
->dev
, "%s\n", __func__
);
2296 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++) {
2297 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
2298 unsigned long dvstctr_reg
= get_dvstctr_reg(port
);
2300 if (!(rh
->port
& USB_PORT_STAT_SUSPEND
))
2303 dev_dbg(&rh
->dev
->udev
->dev
, "resume port = %d\n", port
);
2304 rh
->port
&= ~USB_PORT_STAT_SUSPEND
;
2305 rh
->port
|= USB_PORT_STAT_C_SUSPEND
<< 16;
2306 r8a66597_mdfy(r8a66597
, RESUME
, RESUME
| UACT
, dvstctr_reg
);
2308 r8a66597_mdfy(r8a66597
, UACT
, RESUME
| UACT
, dvstctr_reg
);
2315 #define r8a66597_bus_suspend NULL
2316 #define r8a66597_bus_resume NULL
2319 static struct hc_driver r8a66597_hc_driver
= {
2320 .description
= hcd_name
,
2321 .hcd_priv_size
= sizeof(struct r8a66597
),
2322 .irq
= r8a66597_irq
,
2325 * generic hardware linkage
2329 .start
= r8a66597_start
,
2330 .stop
= r8a66597_stop
,
2333 * managing i/o requests and associated device resources
2335 .urb_enqueue
= r8a66597_urb_enqueue
,
2336 .urb_dequeue
= r8a66597_urb_dequeue
,
2337 .endpoint_disable
= r8a66597_endpoint_disable
,
2340 * periodic schedule support
2342 .get_frame_number
= r8a66597_get_frame
,
2347 .hub_status_data
= r8a66597_hub_status_data
,
2348 .hub_control
= r8a66597_hub_control
,
2349 .bus_suspend
= r8a66597_bus_suspend
,
2350 .bus_resume
= r8a66597_bus_resume
,
2353 #if defined(CONFIG_PM)
2354 static int r8a66597_suspend(struct device
*dev
)
2356 struct r8a66597
*r8a66597
= dev_get_drvdata(dev
);
2359 dev_dbg(dev
, "%s\n", __func__
);
2361 disable_controller(r8a66597
);
2363 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++) {
2364 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
2366 rh
->port
= 0x00000000;
2372 static int r8a66597_resume(struct device
*dev
)
2374 struct r8a66597
*r8a66597
= dev_get_drvdata(dev
);
2375 struct usb_hcd
*hcd
= r8a66597_to_hcd(r8a66597
);
2377 dev_dbg(dev
, "%s\n", __func__
);
2379 enable_controller(r8a66597
);
2380 usb_root_hub_lost_power(hcd
->self
.root_hub
);
2385 static const struct dev_pm_ops r8a66597_dev_pm_ops
= {
2386 .suspend
= r8a66597_suspend
,
2387 .resume
= r8a66597_resume
,
2388 .poweroff
= r8a66597_suspend
,
2389 .restore
= r8a66597_resume
,
2392 #define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
2393 #else /* if defined(CONFIG_PM) */
2394 #define R8A66597_DEV_PM_OPS NULL
2397 static int __devexit
r8a66597_remove(struct platform_device
*pdev
)
2399 struct r8a66597
*r8a66597
= dev_get_drvdata(&pdev
->dev
);
2400 struct usb_hcd
*hcd
= r8a66597_to_hcd(r8a66597
);
2402 del_timer_sync(&r8a66597
->rh_timer
);
2403 usb_remove_hcd(hcd
);
2404 iounmap(r8a66597
->reg
);
2405 if (r8a66597
->pdata
->on_chip
)
2406 clk_put(r8a66597
->clk
);
2411 static int __devinit
r8a66597_probe(struct platform_device
*pdev
)
2414 struct resource
*res
= NULL
, *ires
;
2416 void __iomem
*reg
= NULL
;
2417 struct usb_hcd
*hcd
= NULL
;
2418 struct r8a66597
*r8a66597
;
2421 unsigned long irq_trigger
;
2426 if (pdev
->dev
.dma_mask
) {
2428 dev_err(&pdev
->dev
, "dma not supported\n");
2432 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2435 dev_err(&pdev
->dev
, "platform_get_resource error.\n");
2439 ires
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
2443 "platform_get_resource IORESOURCE_IRQ error.\n");
2448 irq_trigger
= ires
->flags
& IRQF_TRIGGER_MASK
;
2450 reg
= ioremap(res
->start
, resource_size(res
));
2453 dev_err(&pdev
->dev
, "ioremap error.\n");
2457 if (pdev
->dev
.platform_data
== NULL
) {
2458 dev_err(&pdev
->dev
, "no platform data\n");
2463 /* initialize hcd */
2464 hcd
= usb_create_hcd(&r8a66597_hc_driver
, &pdev
->dev
, (char *)hcd_name
);
2467 dev_err(&pdev
->dev
, "Failed to create hcd\n");
2470 r8a66597
= hcd_to_r8a66597(hcd
);
2471 memset(r8a66597
, 0, sizeof(struct r8a66597
));
2472 dev_set_drvdata(&pdev
->dev
, r8a66597
);
2473 r8a66597
->pdata
= pdev
->dev
.platform_data
;
2474 r8a66597
->irq_sense_low
= irq_trigger
== IRQF_TRIGGER_LOW
;
2476 if (r8a66597
->pdata
->on_chip
) {
2477 snprintf(clk_name
, sizeof(clk_name
), "usb%d", pdev
->id
);
2478 r8a66597
->clk
= clk_get(&pdev
->dev
, clk_name
);
2479 if (IS_ERR(r8a66597
->clk
)) {
2480 dev_err(&pdev
->dev
, "cannot get clock \"%s\"\n",
2482 ret
= PTR_ERR(r8a66597
->clk
);
2485 r8a66597
->max_root_hub
= 1;
2487 r8a66597
->max_root_hub
= 2;
2489 spin_lock_init(&r8a66597
->lock
);
2490 init_timer(&r8a66597
->rh_timer
);
2491 r8a66597
->rh_timer
.function
= r8a66597_timer
;
2492 r8a66597
->rh_timer
.data
= (unsigned long)r8a66597
;
2493 r8a66597
->reg
= reg
;
2495 /* make sure no interrupts are pending */
2496 ret
= r8a66597_clock_enable(r8a66597
);
2499 disable_controller(r8a66597
);
2501 for (i
= 0; i
< R8A66597_MAX_NUM_PIPE
; i
++) {
2502 INIT_LIST_HEAD(&r8a66597
->pipe_queue
[i
]);
2503 init_timer(&r8a66597
->td_timer
[i
]);
2504 r8a66597
->td_timer
[i
].function
= r8a66597_td_timer
;
2505 r8a66597
->td_timer
[i
].data
= (unsigned long)r8a66597
;
2506 setup_timer(&r8a66597
->interval_timer
[i
],
2507 r8a66597_interval_timer
,
2508 (unsigned long)r8a66597
);
2510 INIT_LIST_HEAD(&r8a66597
->child_device
);
2512 hcd
->rsrc_start
= res
->start
;
2515 ret
= usb_add_hcd(hcd
, irq
, irq_trigger
);
2517 dev_err(&pdev
->dev
, "Failed to add hcd\n");
2524 if (r8a66597
->pdata
->on_chip
)
2525 clk_put(r8a66597
->clk
);
2536 static struct platform_driver r8a66597_driver
= {
2537 .probe
= r8a66597_probe
,
2538 .remove
= __devexit_p(r8a66597_remove
),
2540 .name
= (char *) hcd_name
,
2541 .owner
= THIS_MODULE
,
2542 .pm
= R8A66597_DEV_PM_OPS
,
2546 module_platform_driver(r8a66597_driver
);