2 * R8A66597 HCD (Host Controller Driver)
4 * Copyright (C) 2006-2007 Renesas Solutions Corp.
5 * Portions Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
6 * Portions Copyright (C) 2004-2005 David Brownell
7 * Portions Copyright (C) 1999 Roman Weissgaerber
9 * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/timer.h>
32 #include <linux/delay.h>
33 #include <linux/list.h>
34 #include <linux/interrupt.h>
35 #include <linux/usb.h>
36 #include <linux/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 #ifdef CONFIG_HAVE_CLK
99 clk_enable(r8a66597
->clk
);
102 r8a66597_write(r8a66597
, SCKE
, SYSCFG0
);
103 tmp
= r8a66597_read(r8a66597
, SYSCFG0
);
105 printk(KERN_ERR
"r8a66597: reg access fail.\n");
108 } while ((tmp
& SCKE
) != SCKE
);
109 r8a66597_write(r8a66597
, 0x04, 0x02);
112 r8a66597_write(r8a66597
, USBE
, SYSCFG0
);
113 tmp
= r8a66597_read(r8a66597
, SYSCFG0
);
115 printk(KERN_ERR
"r8a66597: reg access fail.\n");
118 } while ((tmp
& USBE
) != USBE
);
119 r8a66597_bclr(r8a66597
, USBE
, SYSCFG0
);
120 r8a66597_mdfy(r8a66597
, get_xtal_from_pdata(r8a66597
->pdata
),
124 r8a66597_bset(r8a66597
, XCKE
, SYSCFG0
);
127 tmp
= r8a66597_read(r8a66597
, SYSCFG0
);
129 printk(KERN_ERR
"r8a66597: reg access fail.\n");
132 } while ((tmp
& SCKE
) != SCKE
);
138 static void r8a66597_clock_disable(struct r8a66597
*r8a66597
)
140 r8a66597_bclr(r8a66597
, SCKE
, SYSCFG0
);
143 if (r8a66597
->pdata
->on_chip
) {
144 #ifdef CONFIG_HAVE_CLK
145 clk_disable(r8a66597
->clk
);
148 r8a66597_bclr(r8a66597
, PLLC
, SYSCFG0
);
149 r8a66597_bclr(r8a66597
, XCKE
, SYSCFG0
);
150 r8a66597_bclr(r8a66597
, USBE
, SYSCFG0
);
154 static void r8a66597_enable_port(struct r8a66597
*r8a66597
, int port
)
158 val
= port
? DRPD
: DCFM
| DRPD
;
159 r8a66597_bset(r8a66597
, val
, get_syscfg_reg(port
));
160 r8a66597_bset(r8a66597
, HSE
, get_syscfg_reg(port
));
162 r8a66597_write(r8a66597
, BURST
| CPU_ADR_RD_WR
, get_dmacfg_reg(port
));
163 r8a66597_bclr(r8a66597
, DTCHE
, get_intenb_reg(port
));
164 r8a66597_bset(r8a66597
, ATTCHE
, get_intenb_reg(port
));
167 static void r8a66597_disable_port(struct r8a66597
*r8a66597
, int port
)
171 r8a66597_write(r8a66597
, 0, get_intenb_reg(port
));
172 r8a66597_write(r8a66597
, 0, get_intsts_reg(port
));
174 r8a66597_port_power(r8a66597
, port
, 0);
177 tmp
= r8a66597_read(r8a66597
, SOFCFG
) & EDGESTS
;
179 } while (tmp
== EDGESTS
);
181 val
= port
? DRPD
: DCFM
| DRPD
;
182 r8a66597_bclr(r8a66597
, val
, get_syscfg_reg(port
));
183 r8a66597_bclr(r8a66597
, HSE
, get_syscfg_reg(port
));
186 static int enable_controller(struct r8a66597
*r8a66597
)
189 u16 vif
= r8a66597
->pdata
->vif
? LDRV
: 0;
190 u16 irq_sense
= r8a66597
->irq_sense_low
? INTL
: 0;
191 u16 endian
= r8a66597
->pdata
->endian
? BIGEND
: 0;
193 ret
= r8a66597_clock_enable(r8a66597
);
197 r8a66597_bset(r8a66597
, vif
& LDRV
, PINCFG
);
198 r8a66597_bset(r8a66597
, USBE
, SYSCFG0
);
200 r8a66597_bset(r8a66597
, BEMPE
| NRDYE
| BRDYE
, INTENB0
);
201 r8a66597_bset(r8a66597
, irq_sense
& INTL
, SOFCFG
);
202 r8a66597_bset(r8a66597
, BRDY0
, BRDYENB
);
203 r8a66597_bset(r8a66597
, BEMP0
, BEMPENB
);
205 r8a66597_bset(r8a66597
, endian
& BIGEND
, CFIFOSEL
);
206 r8a66597_bset(r8a66597
, endian
& BIGEND
, D0FIFOSEL
);
207 r8a66597_bset(r8a66597
, endian
& BIGEND
, D1FIFOSEL
);
208 r8a66597_bset(r8a66597
, TRNENSEL
, SOFCFG
);
210 r8a66597_bset(r8a66597
, SIGNE
| SACKE
, INTENB1
);
212 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++)
213 r8a66597_enable_port(r8a66597
, port
);
218 static void disable_controller(struct r8a66597
*r8a66597
)
222 /* disable interrupts */
223 r8a66597_write(r8a66597
, 0, INTENB0
);
224 r8a66597_write(r8a66597
, 0, INTENB1
);
225 r8a66597_write(r8a66597
, 0, BRDYENB
);
226 r8a66597_write(r8a66597
, 0, BEMPENB
);
227 r8a66597_write(r8a66597
, 0, NRDYENB
);
230 r8a66597_write(r8a66597
, 0, BRDYSTS
);
231 r8a66597_write(r8a66597
, 0, NRDYSTS
);
232 r8a66597_write(r8a66597
, 0, BEMPSTS
);
234 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++)
235 r8a66597_disable_port(r8a66597
, port
);
237 r8a66597_clock_disable(r8a66597
);
240 static int get_parent_r8a66597_address(struct r8a66597
*r8a66597
,
241 struct usb_device
*udev
)
243 struct r8a66597_device
*dev
;
245 if (udev
->parent
&& udev
->parent
->devnum
!= 1)
248 dev
= dev_get_drvdata(&udev
->dev
);
255 static int is_child_device(char *devpath
)
257 return (devpath
[2] ? 1 : 0);
260 static int is_hub_limit(char *devpath
)
262 return ((strlen(devpath
) >= 4) ? 1 : 0);
265 static void get_port_number(struct r8a66597
*r8a66597
,
266 char *devpath
, u16
*root_port
, u16
*hub_port
)
269 *root_port
= (devpath
[0] & 0x0F) - 1;
270 if (*root_port
>= r8a66597
->max_root_hub
)
271 printk(KERN_ERR
"r8a66597: Illegal root port number.\n");
274 *hub_port
= devpath
[2] & 0x0F;
277 static u16
get_r8a66597_usb_speed(enum usb_device_speed speed
)
292 printk(KERN_ERR
"r8a66597: unknown speed\n");
299 static void set_child_connect_map(struct r8a66597
*r8a66597
, int address
)
304 r8a66597
->child_connect_map
[idx
] |= 1 << (address
% 32);
307 static void put_child_connect_map(struct r8a66597
*r8a66597
, int address
)
312 r8a66597
->child_connect_map
[idx
] &= ~(1 << (address
% 32));
315 static void set_pipe_reg_addr(struct r8a66597_pipe
*pipe
, u8 dma_ch
)
317 u16 pipenum
= pipe
->info
.pipenum
;
318 const unsigned long fifoaddr
[] = {D0FIFO
, D1FIFO
, CFIFO
};
319 const unsigned long fifosel
[] = {D0FIFOSEL
, D1FIFOSEL
, CFIFOSEL
};
320 const unsigned long fifoctr
[] = {D0FIFOCTR
, D1FIFOCTR
, CFIFOCTR
};
322 if (dma_ch
> R8A66597_PIPE_NO_DMA
) /* dma fifo not use? */
323 dma_ch
= R8A66597_PIPE_NO_DMA
;
325 pipe
->fifoaddr
= fifoaddr
[dma_ch
];
326 pipe
->fifosel
= fifosel
[dma_ch
];
327 pipe
->fifoctr
= fifoctr
[dma_ch
];
330 pipe
->pipectr
= DCPCTR
;
332 pipe
->pipectr
= get_pipectr_addr(pipenum
);
334 if (check_bulk_or_isoc(pipenum
)) {
335 pipe
->pipetre
= get_pipetre_addr(pipenum
);
336 pipe
->pipetrn
= get_pipetrn_addr(pipenum
);
343 static struct r8a66597_device
*
344 get_urb_to_r8a66597_dev(struct r8a66597
*r8a66597
, struct urb
*urb
)
346 if (usb_pipedevice(urb
->pipe
) == 0)
347 return &r8a66597
->device0
;
349 return dev_get_drvdata(&urb
->dev
->dev
);
352 static int make_r8a66597_device(struct r8a66597
*r8a66597
,
353 struct urb
*urb
, u8 addr
)
355 struct r8a66597_device
*dev
;
356 int usb_address
= urb
->setup_packet
[2]; /* urb->pipe is address 0 */
358 dev
= kzalloc(sizeof(struct r8a66597_device
), GFP_ATOMIC
);
362 dev_set_drvdata(&urb
->dev
->dev
, dev
);
363 dev
->udev
= urb
->dev
;
365 dev
->usb_address
= usb_address
;
366 dev
->state
= USB_STATE_ADDRESS
;
367 dev
->ep_in_toggle
= 0;
368 dev
->ep_out_toggle
= 0;
369 INIT_LIST_HEAD(&dev
->device_list
);
370 list_add_tail(&dev
->device_list
, &r8a66597
->child_device
);
372 get_port_number(r8a66597
, urb
->dev
->devpath
,
373 &dev
->root_port
, &dev
->hub_port
);
374 if (!is_child_device(urb
->dev
->devpath
))
375 r8a66597
->root_hub
[dev
->root_port
].dev
= dev
;
377 set_devadd_reg(r8a66597
, dev
->address
,
378 get_r8a66597_usb_speed(urb
->dev
->speed
),
379 get_parent_r8a66597_address(r8a66597
, urb
->dev
),
380 dev
->hub_port
, dev
->root_port
);
385 /* this function must be called with interrupt disabled */
386 static u8
alloc_usb_address(struct r8a66597
*r8a66597
, struct urb
*urb
)
388 u8 addr
; /* R8A66597's address */
389 struct r8a66597_device
*dev
;
391 if (is_hub_limit(urb
->dev
->devpath
)) {
392 dev_err(&urb
->dev
->dev
, "External hub limit reached.\n");
396 dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
397 if (dev
&& dev
->state
>= USB_STATE_ADDRESS
)
400 for (addr
= 1; addr
<= R8A66597_MAX_DEVICE
; addr
++) {
401 if (r8a66597
->address_map
& (1 << addr
))
404 dbg("alloc_address: r8a66597_addr=%d", addr
);
405 r8a66597
->address_map
|= 1 << addr
;
407 if (make_r8a66597_device(r8a66597
, urb
, addr
) < 0)
413 dev_err(&urb
->dev
->dev
,
414 "cannot communicate with a USB device more than 10.(%x)\n",
415 r8a66597
->address_map
);
420 /* this function must be called with interrupt disabled */
421 static void free_usb_address(struct r8a66597
*r8a66597
,
422 struct r8a66597_device
*dev
, int reset
)
429 dbg("free_addr: addr=%d", dev
->address
);
431 dev
->state
= USB_STATE_DEFAULT
;
432 r8a66597
->address_map
&= ~(1 << dev
->address
);
435 * Only when resetting USB, it is necessary to erase drvdata. When
436 * a usb device with usb hub is disconnect, "dev->udev" is already
437 * freed on usb_desconnect(). So we cannot access the data.
440 dev_set_drvdata(&dev
->udev
->dev
, NULL
);
441 list_del(&dev
->device_list
);
444 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++) {
445 if (r8a66597
->root_hub
[port
].dev
== dev
) {
446 r8a66597
->root_hub
[port
].dev
= NULL
;
452 static void r8a66597_reg_wait(struct r8a66597
*r8a66597
, unsigned long reg
,
459 tmp
= r8a66597_read(r8a66597
, reg
);
461 printk(KERN_ERR
"r8a66597: register%lx, loop %x "
462 "is timeout\n", reg
, loop
);
466 } while ((tmp
& mask
) != loop
);
469 /* this function must be called with interrupt disabled */
470 static void pipe_start(struct r8a66597
*r8a66597
, struct r8a66597_pipe
*pipe
)
474 tmp
= r8a66597_read(r8a66597
, pipe
->pipectr
) & PID
;
475 if ((pipe
->info
.pipenum
!= 0) & ((tmp
& PID_STALL
) != 0)) /* stall? */
476 r8a66597_mdfy(r8a66597
, PID_NAK
, PID
, pipe
->pipectr
);
477 r8a66597_mdfy(r8a66597
, PID_BUF
, PID
, pipe
->pipectr
);
480 /* this function must be called with interrupt disabled */
481 static void pipe_stop(struct r8a66597
*r8a66597
, struct r8a66597_pipe
*pipe
)
485 tmp
= r8a66597_read(r8a66597
, pipe
->pipectr
) & PID
;
486 if ((tmp
& PID_STALL11
) != PID_STALL11
) /* force stall? */
487 r8a66597_mdfy(r8a66597
, PID_STALL
, PID
, pipe
->pipectr
);
488 r8a66597_mdfy(r8a66597
, PID_NAK
, PID
, pipe
->pipectr
);
489 r8a66597_reg_wait(r8a66597
, pipe
->pipectr
, PBUSY
, 0);
492 /* this function must be called with interrupt disabled */
493 static void clear_all_buffer(struct r8a66597
*r8a66597
,
494 struct r8a66597_pipe
*pipe
)
498 if (!pipe
|| pipe
->info
.pipenum
== 0)
501 pipe_stop(r8a66597
, pipe
);
502 r8a66597_bset(r8a66597
, ACLRM
, pipe
->pipectr
);
503 tmp
= r8a66597_read(r8a66597
, pipe
->pipectr
);
504 tmp
= r8a66597_read(r8a66597
, pipe
->pipectr
);
505 tmp
= r8a66597_read(r8a66597
, pipe
->pipectr
);
506 r8a66597_bclr(r8a66597
, ACLRM
, pipe
->pipectr
);
509 /* this function must be called with interrupt disabled */
510 static void r8a66597_pipe_toggle(struct r8a66597
*r8a66597
,
511 struct r8a66597_pipe
*pipe
, int toggle
)
514 r8a66597_bset(r8a66597
, SQSET
, pipe
->pipectr
);
516 r8a66597_bset(r8a66597
, SQCLR
, pipe
->pipectr
);
519 static inline unsigned short mbw_value(struct r8a66597
*r8a66597
)
521 if (r8a66597
->pdata
->on_chip
)
527 /* this function must be called with interrupt disabled */
528 static inline void cfifo_change(struct r8a66597
*r8a66597
, u16 pipenum
)
530 unsigned short mbw
= mbw_value(r8a66597
);
532 r8a66597_mdfy(r8a66597
, mbw
| pipenum
, mbw
| CURPIPE
, CFIFOSEL
);
533 r8a66597_reg_wait(r8a66597
, CFIFOSEL
, CURPIPE
, pipenum
);
536 /* this function must be called with interrupt disabled */
537 static inline void fifo_change_from_pipe(struct r8a66597
*r8a66597
,
538 struct r8a66597_pipe
*pipe
)
540 unsigned short mbw
= mbw_value(r8a66597
);
542 cfifo_change(r8a66597
, 0);
543 r8a66597_mdfy(r8a66597
, mbw
| 0, mbw
| CURPIPE
, D0FIFOSEL
);
544 r8a66597_mdfy(r8a66597
, mbw
| 0, mbw
| CURPIPE
, D1FIFOSEL
);
546 r8a66597_mdfy(r8a66597
, mbw
| pipe
->info
.pipenum
, mbw
| CURPIPE
,
548 r8a66597_reg_wait(r8a66597
, pipe
->fifosel
, CURPIPE
, pipe
->info
.pipenum
);
551 static u16
r8a66597_get_pipenum(struct urb
*urb
, struct usb_host_endpoint
*hep
)
553 struct r8a66597_pipe
*pipe
= hep
->hcpriv
;
555 if (usb_pipeendpoint(urb
->pipe
) == 0)
558 return pipe
->info
.pipenum
;
561 static u16
get_urb_to_r8a66597_addr(struct r8a66597
*r8a66597
, struct urb
*urb
)
563 struct r8a66597_device
*dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
565 return (usb_pipedevice(urb
->pipe
) == 0) ? 0 : dev
->address
;
568 static unsigned short *get_toggle_pointer(struct r8a66597_device
*dev
,
574 return usb_pipein(urb_pipe
) ? &dev
->ep_in_toggle
: &dev
->ep_out_toggle
;
577 /* this function must be called with interrupt disabled */
578 static void pipe_toggle_set(struct r8a66597
*r8a66597
,
579 struct r8a66597_pipe
*pipe
,
580 struct urb
*urb
, int set
)
582 struct r8a66597_device
*dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
583 unsigned char endpoint
= usb_pipeendpoint(urb
->pipe
);
584 unsigned short *toggle
= get_toggle_pointer(dev
, urb
->pipe
);
590 *toggle
|= 1 << endpoint
;
592 *toggle
&= ~(1 << endpoint
);
595 /* this function must be called with interrupt disabled */
596 static void pipe_toggle_save(struct r8a66597
*r8a66597
,
597 struct r8a66597_pipe
*pipe
,
600 if (r8a66597_read(r8a66597
, pipe
->pipectr
) & SQMON
)
601 pipe_toggle_set(r8a66597
, pipe
, urb
, 1);
603 pipe_toggle_set(r8a66597
, pipe
, urb
, 0);
606 /* this function must be called with interrupt disabled */
607 static void pipe_toggle_restore(struct r8a66597
*r8a66597
,
608 struct r8a66597_pipe
*pipe
,
611 struct r8a66597_device
*dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
612 unsigned char endpoint
= usb_pipeendpoint(urb
->pipe
);
613 unsigned short *toggle
= get_toggle_pointer(dev
, urb
->pipe
);
618 r8a66597_pipe_toggle(r8a66597
, pipe
, *toggle
& (1 << endpoint
));
621 /* this function must be called with interrupt disabled */
622 static void pipe_buffer_setting(struct r8a66597
*r8a66597
,
623 struct r8a66597_pipe_info
*info
)
627 if (info
->pipenum
== 0)
630 r8a66597_bset(r8a66597
, ACLRM
, get_pipectr_addr(info
->pipenum
));
631 r8a66597_bclr(r8a66597
, ACLRM
, get_pipectr_addr(info
->pipenum
));
632 r8a66597_write(r8a66597
, info
->pipenum
, PIPESEL
);
635 if (info
->type
== R8A66597_BULK
&& info
->dir_in
)
636 val
|= R8A66597_DBLB
| R8A66597_SHTNAK
;
637 val
|= info
->type
| info
->epnum
;
638 r8a66597_write(r8a66597
, val
, PIPECFG
);
640 r8a66597_write(r8a66597
, (info
->buf_bsize
<< 10) | (info
->bufnum
),
642 r8a66597_write(r8a66597
, make_devsel(info
->address
) | info
->maxpacket
,
644 r8a66597_write(r8a66597
, info
->interval
, PIPEPERI
);
647 /* this function must be called with interrupt disabled */
648 static void pipe_setting(struct r8a66597
*r8a66597
, struct r8a66597_td
*td
)
650 struct r8a66597_pipe_info
*info
;
651 struct urb
*urb
= td
->urb
;
653 if (td
->pipenum
> 0) {
654 info
= &td
->pipe
->info
;
655 cfifo_change(r8a66597
, 0);
656 pipe_buffer_setting(r8a66597
, info
);
658 if (!usb_gettoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
),
659 usb_pipeout(urb
->pipe
)) &&
660 !usb_pipecontrol(urb
->pipe
)) {
661 r8a66597_pipe_toggle(r8a66597
, td
->pipe
, 0);
662 pipe_toggle_set(r8a66597
, td
->pipe
, urb
, 0);
663 clear_all_buffer(r8a66597
, td
->pipe
);
664 usb_settoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
),
665 usb_pipeout(urb
->pipe
), 1);
667 pipe_toggle_restore(r8a66597
, td
->pipe
, urb
);
671 /* this function must be called with interrupt disabled */
672 static u16
get_empty_pipenum(struct r8a66597
*r8a66597
,
673 struct usb_endpoint_descriptor
*ep
)
675 u16 array
[R8A66597_MAX_NUM_PIPE
], i
= 0, min
;
677 memset(array
, 0, sizeof(array
));
678 switch (usb_endpoint_type(ep
)) {
679 case USB_ENDPOINT_XFER_BULK
:
680 if (usb_endpoint_dir_in(ep
))
687 case USB_ENDPOINT_XFER_INT
:
688 if (usb_endpoint_dir_in(ep
)) {
695 case USB_ENDPOINT_XFER_ISOC
:
696 if (usb_endpoint_dir_in(ep
))
702 printk(KERN_ERR
"r8a66597: Illegal type\n");
708 while (array
[i
] != 0) {
709 if (r8a66597
->pipe_cnt
[min
] > r8a66597
->pipe_cnt
[array
[i
]])
717 static u16
get_r8a66597_type(__u8 type
)
722 case USB_ENDPOINT_XFER_BULK
:
723 r8a66597_type
= R8A66597_BULK
;
725 case USB_ENDPOINT_XFER_INT
:
726 r8a66597_type
= R8A66597_INT
;
728 case USB_ENDPOINT_XFER_ISOC
:
729 r8a66597_type
= R8A66597_ISO
;
732 printk(KERN_ERR
"r8a66597: Illegal type\n");
733 r8a66597_type
= 0x0000;
737 return r8a66597_type
;
740 static u16
get_bufnum(u16 pipenum
)
746 else if (check_bulk_or_isoc(pipenum
))
747 bufnum
= 8 + (pipenum
- 1) * R8A66597_BUF_BSIZE
*2;
748 else if (check_interrupt(pipenum
))
749 bufnum
= 4 + (pipenum
- 6);
751 printk(KERN_ERR
"r8a66597: Illegal pipenum (%d)\n", pipenum
);
756 static u16
get_buf_bsize(u16 pipenum
)
762 else if (check_bulk_or_isoc(pipenum
))
763 buf_bsize
= R8A66597_BUF_BSIZE
- 1;
764 else if (check_interrupt(pipenum
))
767 printk(KERN_ERR
"r8a66597: Illegal pipenum (%d)\n", pipenum
);
772 /* this function must be called with interrupt disabled */
773 static void enable_r8a66597_pipe_dma(struct r8a66597
*r8a66597
,
774 struct r8a66597_device
*dev
,
775 struct r8a66597_pipe
*pipe
,
779 struct r8a66597_pipe_info
*info
= &pipe
->info
;
780 unsigned short mbw
= mbw_value(r8a66597
);
782 /* pipe dma is only for external controlles */
783 if (r8a66597
->pdata
->on_chip
)
786 if ((pipe
->info
.pipenum
!= 0) && (info
->type
!= R8A66597_INT
)) {
787 for (i
= 0; i
< R8A66597_MAX_DMA_CHANNEL
; i
++) {
788 if ((r8a66597
->dma_map
& (1 << i
)) != 0)
791 dev_info(&dev
->udev
->dev
,
792 "address %d, EndpointAddress 0x%02x use "
793 "DMA FIFO\n", usb_pipedevice(urb
->pipe
),
795 USB_ENDPOINT_DIR_MASK
+ info
->epnum
798 r8a66597
->dma_map
|= 1 << i
;
799 dev
->dma_map
|= 1 << i
;
800 set_pipe_reg_addr(pipe
, i
);
802 cfifo_change(r8a66597
, 0);
803 r8a66597_mdfy(r8a66597
, mbw
| pipe
->info
.pipenum
,
804 mbw
| CURPIPE
, pipe
->fifosel
);
806 r8a66597_reg_wait(r8a66597
, pipe
->fifosel
, CURPIPE
,
808 r8a66597_bset(r8a66597
, BCLR
, pipe
->fifoctr
);
814 /* this function must be called with interrupt disabled */
815 static void enable_r8a66597_pipe(struct r8a66597
*r8a66597
, struct urb
*urb
,
816 struct usb_host_endpoint
*hep
,
817 struct r8a66597_pipe_info
*info
)
819 struct r8a66597_device
*dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
820 struct r8a66597_pipe
*pipe
= hep
->hcpriv
;
825 set_pipe_reg_addr(pipe
, R8A66597_PIPE_NO_DMA
);
826 r8a66597
->pipe_cnt
[pipe
->info
.pipenum
]++;
827 dev
->pipe_cnt
[pipe
->info
.pipenum
]++;
829 enable_r8a66597_pipe_dma(r8a66597
, dev
, pipe
, urb
);
832 static void r8a66597_urb_done(struct r8a66597
*r8a66597
, struct urb
*urb
,
834 __releases(r8a66597
->lock
)
835 __acquires(r8a66597
->lock
)
837 if (usb_pipein(urb
->pipe
) && usb_pipetype(urb
->pipe
) != PIPE_CONTROL
) {
840 for (ptr
= urb
->transfer_buffer
;
841 ptr
< urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
843 flush_dcache_page(virt_to_page(ptr
));
846 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597
), urb
);
847 spin_unlock(&r8a66597
->lock
);
848 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597
), urb
, status
);
849 spin_lock(&r8a66597
->lock
);
852 /* this function must be called with interrupt disabled */
853 static void force_dequeue(struct r8a66597
*r8a66597
, u16 pipenum
, u16 address
)
855 struct r8a66597_td
*td
, *next
;
857 struct list_head
*list
= &r8a66597
->pipe_queue
[pipenum
];
859 if (list_empty(list
))
862 list_for_each_entry_safe(td
, next
, list
, queue
) {
863 if (td
->address
!= address
)
867 list_del(&td
->queue
);
871 r8a66597_urb_done(r8a66597
, urb
, -ENODEV
);
877 /* this function must be called with interrupt disabled */
878 static void disable_r8a66597_pipe_all(struct r8a66597
*r8a66597
,
879 struct r8a66597_device
*dev
)
887 for (pipenum
= 1; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
888 if (!dev
->pipe_cnt
[pipenum
])
893 force_dequeue(r8a66597
, 0, dev
->address
);
896 r8a66597
->pipe_cnt
[pipenum
] -= dev
->pipe_cnt
[pipenum
];
897 dev
->pipe_cnt
[pipenum
] = 0;
898 force_dequeue(r8a66597
, pipenum
, dev
->address
);
903 r8a66597
->dma_map
&= ~(dev
->dma_map
);
907 static u16
get_interval(struct urb
*urb
, __u8 interval
)
912 if (urb
->dev
->speed
== USB_SPEED_HIGH
) {
916 time
= interval
? interval
- 1 : 0;
918 if (interval
> 128) {
921 /* calculate the nearest value for PIPEPERI */
922 for (i
= 0; i
< 7; i
++) {
923 if ((1 << i
) < interval
&&
924 (1 << (i
+ 1) > interval
))
933 static unsigned long get_timer_interval(struct urb
*urb
, __u8 interval
)
936 unsigned long time
= 1;
938 if (usb_pipeisoc(urb
->pipe
))
941 if (get_r8a66597_usb_speed(urb
->dev
->speed
) == HSMODE
) {
942 for (i
= 0; i
< (interval
- 1); i
++)
944 time
= time
* 125 / 1000; /* uSOF -> msec */
952 /* this function must be called with interrupt disabled */
953 static void init_pipe_info(struct r8a66597
*r8a66597
, struct urb
*urb
,
954 struct usb_host_endpoint
*hep
,
955 struct usb_endpoint_descriptor
*ep
)
957 struct r8a66597_pipe_info info
;
959 info
.pipenum
= get_empty_pipenum(r8a66597
, ep
);
960 info
.address
= get_urb_to_r8a66597_addr(r8a66597
, urb
);
961 info
.epnum
= usb_endpoint_num(ep
);
962 info
.maxpacket
= le16_to_cpu(ep
->wMaxPacketSize
);
963 info
.type
= get_r8a66597_type(usb_endpoint_type(ep
));
964 info
.bufnum
= get_bufnum(info
.pipenum
);
965 info
.buf_bsize
= get_buf_bsize(info
.pipenum
);
966 if (info
.type
== R8A66597_BULK
) {
968 info
.timer_interval
= 0;
970 info
.interval
= get_interval(urb
, ep
->bInterval
);
971 info
.timer_interval
= get_timer_interval(urb
, ep
->bInterval
);
973 if (usb_endpoint_dir_in(ep
))
978 enable_r8a66597_pipe(r8a66597
, urb
, hep
, &info
);
981 static void init_pipe_config(struct r8a66597
*r8a66597
, struct urb
*urb
)
983 struct r8a66597_device
*dev
;
985 dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
986 dev
->state
= USB_STATE_CONFIGURED
;
989 static void pipe_irq_enable(struct r8a66597
*r8a66597
, struct urb
*urb
,
992 if (pipenum
== 0 && usb_pipeout(urb
->pipe
))
993 enable_irq_empty(r8a66597
, pipenum
);
995 enable_irq_ready(r8a66597
, pipenum
);
997 if (!usb_pipeisoc(urb
->pipe
))
998 enable_irq_nrdy(r8a66597
, pipenum
);
1001 static void pipe_irq_disable(struct r8a66597
*r8a66597
, u16 pipenum
)
1003 disable_irq_ready(r8a66597
, pipenum
);
1004 disable_irq_nrdy(r8a66597
, pipenum
);
1007 static void r8a66597_root_hub_start_polling(struct r8a66597
*r8a66597
)
1009 mod_timer(&r8a66597
->rh_timer
,
1010 jiffies
+ msecs_to_jiffies(R8A66597_RH_POLL_TIME
));
1013 static void start_root_hub_sampling(struct r8a66597
*r8a66597
, int port
,
1016 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
1018 rh
->old_syssts
= r8a66597_read(r8a66597
, get_syssts_reg(port
)) & LNST
;
1019 rh
->scount
= R8A66597_MAX_SAMPLING
;
1021 rh
->port
|= USB_PORT_STAT_CONNECTION
;
1023 rh
->port
&= ~USB_PORT_STAT_CONNECTION
;
1024 rh
->port
|= USB_PORT_STAT_C_CONNECTION
<< 16;
1026 r8a66597_root_hub_start_polling(r8a66597
);
1029 /* this function must be called with interrupt disabled */
1030 static void r8a66597_check_syssts(struct r8a66597
*r8a66597
, int port
,
1032 __releases(r8a66597
->lock
)
1033 __acquires(r8a66597
->lock
)
1035 if (syssts
== SE0
) {
1036 r8a66597_write(r8a66597
, ~ATTCH
, get_intsts_reg(port
));
1037 r8a66597_bset(r8a66597
, ATTCHE
, get_intenb_reg(port
));
1039 if (syssts
== FS_JSTS
)
1040 r8a66597_bset(r8a66597
, HSE
, get_syscfg_reg(port
));
1041 else if (syssts
== LS_JSTS
)
1042 r8a66597_bclr(r8a66597
, HSE
, get_syscfg_reg(port
));
1044 r8a66597_write(r8a66597
, ~DTCH
, get_intsts_reg(port
));
1045 r8a66597_bset(r8a66597
, DTCHE
, get_intenb_reg(port
));
1047 if (r8a66597
->bus_suspended
)
1048 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597
));
1051 spin_unlock(&r8a66597
->lock
);
1052 usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597
));
1053 spin_lock(&r8a66597
->lock
);
1056 /* this function must be called with interrupt disabled */
1057 static void r8a66597_usb_connect(struct r8a66597
*r8a66597
, int port
)
1059 u16 speed
= get_rh_usb_speed(r8a66597
, port
);
1060 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
1062 rh
->port
&= ~(USB_PORT_STAT_HIGH_SPEED
| USB_PORT_STAT_LOW_SPEED
);
1063 if (speed
== HSMODE
)
1064 rh
->port
|= USB_PORT_STAT_HIGH_SPEED
;
1065 else if (speed
== LSMODE
)
1066 rh
->port
|= USB_PORT_STAT_LOW_SPEED
;
1068 rh
->port
&= ~USB_PORT_STAT_RESET
;
1069 rh
->port
|= USB_PORT_STAT_ENABLE
;
1072 /* this function must be called with interrupt disabled */
1073 static void r8a66597_usb_disconnect(struct r8a66597
*r8a66597
, int port
)
1075 struct r8a66597_device
*dev
= r8a66597
->root_hub
[port
].dev
;
1077 disable_r8a66597_pipe_all(r8a66597
, dev
);
1078 free_usb_address(r8a66597
, dev
, 0);
1080 start_root_hub_sampling(r8a66597
, port
, 0);
1083 /* this function must be called with interrupt disabled */
1084 static void prepare_setup_packet(struct r8a66597
*r8a66597
,
1085 struct r8a66597_td
*td
)
1088 __le16
*p
= (__le16
*)td
->urb
->setup_packet
;
1089 unsigned long setup_addr
= USBREQ
;
1091 r8a66597_write(r8a66597
, make_devsel(td
->address
) | td
->maxpacket
,
1093 r8a66597_write(r8a66597
, ~(SIGN
| SACK
), INTSTS1
);
1095 for (i
= 0; i
< 4; i
++) {
1096 r8a66597_write(r8a66597
, le16_to_cpu(p
[i
]), setup_addr
);
1099 r8a66597_write(r8a66597
, SUREQ
, DCPCTR
);
1102 /* this function must be called with interrupt disabled */
1103 static void prepare_packet_read(struct r8a66597
*r8a66597
,
1104 struct r8a66597_td
*td
)
1106 struct urb
*urb
= td
->urb
;
1108 if (usb_pipecontrol(urb
->pipe
)) {
1109 r8a66597_bclr(r8a66597
, R8A66597_DIR
, DCPCFG
);
1110 r8a66597_mdfy(r8a66597
, 0, ISEL
| CURPIPE
, CFIFOSEL
);
1111 r8a66597_reg_wait(r8a66597
, CFIFOSEL
, CURPIPE
, 0);
1112 if (urb
->actual_length
== 0) {
1113 r8a66597_pipe_toggle(r8a66597
, td
->pipe
, 1);
1114 r8a66597_write(r8a66597
, BCLR
, CFIFOCTR
);
1116 pipe_irq_disable(r8a66597
, td
->pipenum
);
1117 pipe_start(r8a66597
, td
->pipe
);
1118 pipe_irq_enable(r8a66597
, urb
, td
->pipenum
);
1120 if (urb
->actual_length
== 0) {
1121 pipe_irq_disable(r8a66597
, td
->pipenum
);
1122 pipe_setting(r8a66597
, td
);
1123 pipe_stop(r8a66597
, td
->pipe
);
1124 r8a66597_write(r8a66597
, ~(1 << td
->pipenum
), BRDYSTS
);
1126 if (td
->pipe
->pipetre
) {
1127 r8a66597_write(r8a66597
, TRCLR
,
1129 r8a66597_write(r8a66597
,
1131 (urb
->transfer_buffer_length
,
1134 r8a66597_bset(r8a66597
, TRENB
,
1138 pipe_start(r8a66597
, td
->pipe
);
1139 pipe_irq_enable(r8a66597
, urb
, td
->pipenum
);
1144 /* this function must be called with interrupt disabled */
1145 static void prepare_packet_write(struct r8a66597
*r8a66597
,
1146 struct r8a66597_td
*td
)
1149 struct urb
*urb
= td
->urb
;
1151 if (usb_pipecontrol(urb
->pipe
)) {
1152 pipe_stop(r8a66597
, td
->pipe
);
1153 r8a66597_bset(r8a66597
, R8A66597_DIR
, DCPCFG
);
1154 r8a66597_mdfy(r8a66597
, ISEL
, ISEL
| CURPIPE
, CFIFOSEL
);
1155 r8a66597_reg_wait(r8a66597
, CFIFOSEL
, CURPIPE
, 0);
1156 if (urb
->actual_length
== 0) {
1157 r8a66597_pipe_toggle(r8a66597
, td
->pipe
, 1);
1158 r8a66597_write(r8a66597
, BCLR
, CFIFOCTR
);
1161 if (urb
->actual_length
== 0)
1162 pipe_setting(r8a66597
, td
);
1163 if (td
->pipe
->pipetre
)
1164 r8a66597_bclr(r8a66597
, TRENB
, td
->pipe
->pipetre
);
1166 r8a66597_write(r8a66597
, ~(1 << td
->pipenum
), BRDYSTS
);
1168 fifo_change_from_pipe(r8a66597
, td
->pipe
);
1169 tmp
= r8a66597_read(r8a66597
, td
->pipe
->fifoctr
);
1170 if (unlikely((tmp
& FRDY
) == 0))
1171 pipe_irq_enable(r8a66597
, urb
, td
->pipenum
);
1173 packet_write(r8a66597
, td
->pipenum
);
1174 pipe_start(r8a66597
, td
->pipe
);
1177 /* this function must be called with interrupt disabled */
1178 static void prepare_status_packet(struct r8a66597
*r8a66597
,
1179 struct r8a66597_td
*td
)
1181 struct urb
*urb
= td
->urb
;
1183 r8a66597_pipe_toggle(r8a66597
, td
->pipe
, 1);
1184 pipe_stop(r8a66597
, td
->pipe
);
1186 if (urb
->setup_packet
[0] & USB_ENDPOINT_DIR_MASK
) {
1187 r8a66597_bset(r8a66597
, R8A66597_DIR
, DCPCFG
);
1188 r8a66597_mdfy(r8a66597
, ISEL
, ISEL
| CURPIPE
, CFIFOSEL
);
1189 r8a66597_reg_wait(r8a66597
, CFIFOSEL
, CURPIPE
, 0);
1190 r8a66597_write(r8a66597
, ~BEMP0
, BEMPSTS
);
1191 r8a66597_write(r8a66597
, BCLR
| BVAL
, CFIFOCTR
);
1192 enable_irq_empty(r8a66597
, 0);
1194 r8a66597_bclr(r8a66597
, R8A66597_DIR
, DCPCFG
);
1195 r8a66597_mdfy(r8a66597
, 0, ISEL
| CURPIPE
, CFIFOSEL
);
1196 r8a66597_reg_wait(r8a66597
, CFIFOSEL
, CURPIPE
, 0);
1197 r8a66597_write(r8a66597
, BCLR
, CFIFOCTR
);
1198 enable_irq_ready(r8a66597
, 0);
1200 enable_irq_nrdy(r8a66597
, 0);
1201 pipe_start(r8a66597
, td
->pipe
);
1204 static int is_set_address(unsigned char *setup_packet
)
1206 if (((setup_packet
[0] & USB_TYPE_MASK
) == USB_TYPE_STANDARD
) &&
1207 setup_packet
[1] == USB_REQ_SET_ADDRESS
)
1213 /* this function must be called with interrupt disabled */
1214 static int start_transfer(struct r8a66597
*r8a66597
, struct r8a66597_td
*td
)
1220 if (is_set_address(td
->urb
->setup_packet
)) {
1221 td
->set_address
= 1;
1222 td
->urb
->setup_packet
[2] = alloc_usb_address(r8a66597
,
1224 if (td
->urb
->setup_packet
[2] == 0)
1227 prepare_setup_packet(r8a66597
, td
);
1230 prepare_packet_read(r8a66597
, td
);
1233 prepare_packet_write(r8a66597
, td
);
1236 prepare_status_packet(r8a66597
, td
);
1239 printk(KERN_ERR
"r8a66597: invalid type.\n");
1246 static int check_transfer_finish(struct r8a66597_td
*td
, struct urb
*urb
)
1248 if (usb_pipeisoc(urb
->pipe
)) {
1249 if (urb
->number_of_packets
== td
->iso_cnt
)
1253 /* control or bulk or interrupt */
1254 if ((urb
->transfer_buffer_length
<= urb
->actual_length
) ||
1255 (td
->short_packet
) || (td
->zero_packet
))
1261 /* this function must be called with interrupt disabled */
1262 static void set_td_timer(struct r8a66597
*r8a66597
, struct r8a66597_td
*td
)
1268 if (!list_empty(&r8a66597
->pipe_queue
[td
->pipenum
]) &&
1269 !usb_pipecontrol(td
->urb
->pipe
) && usb_pipein(td
->urb
->pipe
)) {
1270 r8a66597
->timeout_map
|= 1 << td
->pipenum
;
1271 switch (usb_pipetype(td
->urb
->pipe
)) {
1272 case PIPE_INTERRUPT
:
1273 case PIPE_ISOCHRONOUS
:
1281 mod_timer(&r8a66597
->td_timer
[td
->pipenum
],
1282 jiffies
+ msecs_to_jiffies(time
));
1286 /* this function must be called with interrupt disabled */
1287 static void finish_request(struct r8a66597
*r8a66597
, struct r8a66597_td
*td
,
1288 u16 pipenum
, struct urb
*urb
, int status
)
1289 __releases(r8a66597
->lock
) __acquires(r8a66597
->lock
)
1292 struct usb_hcd
*hcd
= r8a66597_to_hcd(r8a66597
);
1294 r8a66597
->timeout_map
&= ~(1 << pipenum
);
1297 if (td
->set_address
&& (status
!= 0 || urb
->unlinked
))
1298 r8a66597
->address_map
&= ~(1 << urb
->setup_packet
[2]);
1300 pipe_toggle_save(r8a66597
, td
->pipe
, urb
);
1301 list_del(&td
->queue
);
1305 if (!list_empty(&r8a66597
->pipe_queue
[pipenum
]))
1309 if (usb_pipeisoc(urb
->pipe
))
1310 urb
->start_frame
= r8a66597_get_frame(hcd
);
1312 r8a66597_urb_done(r8a66597
, urb
, status
);
1316 td
= r8a66597_get_td(r8a66597
, pipenum
);
1320 start_transfer(r8a66597
, td
);
1321 set_td_timer(r8a66597
, td
);
1325 static void packet_read(struct r8a66597
*r8a66597
, u16 pipenum
)
1328 int rcv_len
, bufsize
, urb_len
, size
;
1330 struct r8a66597_td
*td
= r8a66597_get_td(r8a66597
, pipenum
);
1339 fifo_change_from_pipe(r8a66597
, td
->pipe
);
1340 tmp
= r8a66597_read(r8a66597
, td
->pipe
->fifoctr
);
1341 if (unlikely((tmp
& FRDY
) == 0)) {
1342 pipe_stop(r8a66597
, td
->pipe
);
1343 pipe_irq_disable(r8a66597
, pipenum
);
1344 printk(KERN_ERR
"r8a66597: in fifo not ready (%d)\n", pipenum
);
1345 finish_request(r8a66597
, td
, pipenum
, td
->urb
, -EPIPE
);
1349 /* prepare parameters */
1350 rcv_len
= tmp
& DTLN
;
1351 if (usb_pipeisoc(urb
->pipe
)) {
1352 buf
= (u16
*)(urb
->transfer_buffer
+
1353 urb
->iso_frame_desc
[td
->iso_cnt
].offset
);
1354 urb_len
= urb
->iso_frame_desc
[td
->iso_cnt
].length
;
1356 buf
= (void *)urb
->transfer_buffer
+ urb
->actual_length
;
1357 urb_len
= urb
->transfer_buffer_length
- urb
->actual_length
;
1359 bufsize
= min(urb_len
, (int) td
->maxpacket
);
1360 if (rcv_len
<= bufsize
) {
1364 status
= -EOVERFLOW
;
1368 /* update parameters */
1369 urb
->actual_length
+= size
;
1371 td
->zero_packet
= 1;
1372 if (rcv_len
< bufsize
) {
1373 td
->short_packet
= 1;
1375 if (usb_pipeisoc(urb
->pipe
)) {
1376 urb
->iso_frame_desc
[td
->iso_cnt
].actual_length
= size
;
1377 urb
->iso_frame_desc
[td
->iso_cnt
].status
= status
;
1382 /* check transfer finish */
1383 if (finish
|| check_transfer_finish(td
, urb
)) {
1384 pipe_stop(r8a66597
, td
->pipe
);
1385 pipe_irq_disable(r8a66597
, pipenum
);
1390 if (urb
->transfer_buffer
) {
1392 r8a66597_write(r8a66597
, BCLR
, td
->pipe
->fifoctr
);
1394 r8a66597_read_fifo(r8a66597
, td
->pipe
->fifoaddr
,
1398 if (finish
&& pipenum
!= 0)
1399 finish_request(r8a66597
, td
, pipenum
, urb
, status
);
1402 static void packet_write(struct r8a66597
*r8a66597
, u16 pipenum
)
1407 struct r8a66597_td
*td
= r8a66597_get_td(r8a66597
, pipenum
);
1414 fifo_change_from_pipe(r8a66597
, td
->pipe
);
1415 tmp
= r8a66597_read(r8a66597
, td
->pipe
->fifoctr
);
1416 if (unlikely((tmp
& FRDY
) == 0)) {
1417 pipe_stop(r8a66597
, td
->pipe
);
1418 pipe_irq_disable(r8a66597
, pipenum
);
1419 printk(KERN_ERR
"r8a66597: out fifo not ready (%d)\n", pipenum
);
1420 finish_request(r8a66597
, td
, pipenum
, urb
, -EPIPE
);
1424 /* prepare parameters */
1425 bufsize
= td
->maxpacket
;
1426 if (usb_pipeisoc(urb
->pipe
)) {
1427 buf
= (u16
*)(urb
->transfer_buffer
+
1428 urb
->iso_frame_desc
[td
->iso_cnt
].offset
);
1430 (int)urb
->iso_frame_desc
[td
->iso_cnt
].length
);
1432 buf
= (u16
*)(urb
->transfer_buffer
+ urb
->actual_length
);
1433 size
= min_t(u32
, bufsize
,
1434 urb
->transfer_buffer_length
- urb
->actual_length
);
1439 r8a66597_write(r8a66597
, ~(1 << pipenum
), BEMPSTS
);
1440 if (urb
->transfer_buffer
) {
1441 r8a66597_write_fifo(r8a66597
, td
->pipe
->fifoaddr
, buf
, size
);
1442 if (!usb_pipebulk(urb
->pipe
) || td
->maxpacket
!= size
)
1443 r8a66597_write(r8a66597
, BVAL
, td
->pipe
->fifoctr
);
1446 /* update parameters */
1447 urb
->actual_length
+= size
;
1448 if (usb_pipeisoc(urb
->pipe
)) {
1449 urb
->iso_frame_desc
[td
->iso_cnt
].actual_length
= size
;
1450 urb
->iso_frame_desc
[td
->iso_cnt
].status
= 0;
1454 /* check transfer finish */
1455 if (check_transfer_finish(td
, urb
)) {
1456 disable_irq_ready(r8a66597
, pipenum
);
1457 enable_irq_empty(r8a66597
, pipenum
);
1458 if (!usb_pipeisoc(urb
->pipe
))
1459 enable_irq_nrdy(r8a66597
, pipenum
);
1461 pipe_irq_enable(r8a66597
, urb
, pipenum
);
1465 static void check_next_phase(struct r8a66597
*r8a66597
, int status
)
1467 struct r8a66597_td
*td
= r8a66597_get_td(r8a66597
, 0);
1478 if (check_transfer_finish(td
, urb
))
1479 td
->type
= USB_PID_ACK
;
1482 if (urb
->transfer_buffer_length
== urb
->actual_length
)
1483 td
->type
= USB_PID_ACK
;
1484 else if (usb_pipeout(urb
->pipe
))
1485 td
->type
= USB_PID_OUT
;
1487 td
->type
= USB_PID_IN
;
1494 if (finish
|| status
!= 0 || urb
->unlinked
)
1495 finish_request(r8a66597
, td
, 0, urb
, status
);
1497 start_transfer(r8a66597
, td
);
1500 static int get_urb_error(struct r8a66597
*r8a66597
, u16 pipenum
)
1502 struct r8a66597_td
*td
= r8a66597_get_td(r8a66597
, pipenum
);
1505 u16 pid
= r8a66597_read(r8a66597
, td
->pipe
->pipectr
) & PID
;
1515 static void irq_pipe_ready(struct r8a66597
*r8a66597
)
1520 struct r8a66597_td
*td
;
1522 mask
= r8a66597_read(r8a66597
, BRDYSTS
)
1523 & r8a66597_read(r8a66597
, BRDYENB
);
1524 r8a66597_write(r8a66597
, ~mask
, BRDYSTS
);
1526 td
= r8a66597_get_td(r8a66597
, 0);
1527 if (td
&& td
->type
== USB_PID_IN
)
1528 packet_read(r8a66597
, 0);
1530 pipe_irq_disable(r8a66597
, 0);
1531 check_next_phase(r8a66597
, 0);
1534 for (pipenum
= 1; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1535 check
= 1 << pipenum
;
1537 td
= r8a66597_get_td(r8a66597
, pipenum
);
1541 if (td
->type
== USB_PID_IN
)
1542 packet_read(r8a66597
, pipenum
);
1543 else if (td
->type
== USB_PID_OUT
)
1544 packet_write(r8a66597
, pipenum
);
1549 static void irq_pipe_empty(struct r8a66597
*r8a66597
)
1555 struct r8a66597_td
*td
;
1557 mask
= r8a66597_read(r8a66597
, BEMPSTS
)
1558 & r8a66597_read(r8a66597
, BEMPENB
);
1559 r8a66597_write(r8a66597
, ~mask
, BEMPSTS
);
1561 cfifo_change(r8a66597
, 0);
1562 td
= r8a66597_get_td(r8a66597
, 0);
1563 if (td
&& td
->type
!= USB_PID_OUT
)
1564 disable_irq_empty(r8a66597
, 0);
1565 check_next_phase(r8a66597
, 0);
1568 for (pipenum
= 1; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1569 check
= 1 << pipenum
;
1571 struct r8a66597_td
*td
;
1572 td
= r8a66597_get_td(r8a66597
, pipenum
);
1576 tmp
= r8a66597_read(r8a66597
, td
->pipe
->pipectr
);
1577 if ((tmp
& INBUFM
) == 0) {
1578 disable_irq_empty(r8a66597
, pipenum
);
1579 pipe_irq_disable(r8a66597
, pipenum
);
1580 finish_request(r8a66597
, td
, pipenum
, td
->urb
,
1587 static void irq_pipe_nrdy(struct r8a66597
*r8a66597
)
1594 mask
= r8a66597_read(r8a66597
, NRDYSTS
)
1595 & r8a66597_read(r8a66597
, NRDYENB
);
1596 r8a66597_write(r8a66597
, ~mask
, NRDYSTS
);
1598 cfifo_change(r8a66597
, 0);
1599 status
= get_urb_error(r8a66597
, 0);
1600 pipe_irq_disable(r8a66597
, 0);
1601 check_next_phase(r8a66597
, status
);
1604 for (pipenum
= 1; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1605 check
= 1 << pipenum
;
1607 struct r8a66597_td
*td
;
1608 td
= r8a66597_get_td(r8a66597
, pipenum
);
1612 status
= get_urb_error(r8a66597
, pipenum
);
1613 pipe_irq_disable(r8a66597
, pipenum
);
1614 pipe_stop(r8a66597
, td
->pipe
);
1615 finish_request(r8a66597
, td
, pipenum
, td
->urb
, status
);
1620 static irqreturn_t
r8a66597_irq(struct usb_hcd
*hcd
)
1622 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1623 u16 intsts0
, intsts1
, intsts2
;
1624 u16 intenb0
, intenb1
, intenb2
;
1625 u16 mask0
, mask1
, mask2
;
1628 spin_lock(&r8a66597
->lock
);
1630 intsts0
= r8a66597_read(r8a66597
, INTSTS0
);
1631 intsts1
= r8a66597_read(r8a66597
, INTSTS1
);
1632 intsts2
= r8a66597_read(r8a66597
, INTSTS2
);
1633 intenb0
= r8a66597_read(r8a66597
, INTENB0
);
1634 intenb1
= r8a66597_read(r8a66597
, INTENB1
);
1635 intenb2
= r8a66597_read(r8a66597
, INTENB2
);
1637 mask2
= intsts2
& intenb2
;
1638 mask1
= intsts1
& intenb1
;
1639 mask0
= intsts0
& intenb0
& (BEMP
| NRDY
| BRDY
);
1641 if (mask2
& ATTCH
) {
1642 r8a66597_write(r8a66597
, ~ATTCH
, INTSTS2
);
1643 r8a66597_bclr(r8a66597
, ATTCHE
, INTENB2
);
1645 /* start usb bus sampling */
1646 start_root_hub_sampling(r8a66597
, 1, 1);
1649 r8a66597_write(r8a66597
, ~DTCH
, INTSTS2
);
1650 r8a66597_bclr(r8a66597
, DTCHE
, INTENB2
);
1651 r8a66597_usb_disconnect(r8a66597
, 1);
1654 r8a66597_write(r8a66597
, ~BCHG
, INTSTS2
);
1655 r8a66597_bclr(r8a66597
, BCHGE
, INTENB2
);
1656 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597
));
1661 if (mask1
& ATTCH
) {
1662 r8a66597_write(r8a66597
, ~ATTCH
, INTSTS1
);
1663 r8a66597_bclr(r8a66597
, ATTCHE
, INTENB1
);
1665 /* start usb bus sampling */
1666 start_root_hub_sampling(r8a66597
, 0, 1);
1669 r8a66597_write(r8a66597
, ~DTCH
, INTSTS1
);
1670 r8a66597_bclr(r8a66597
, DTCHE
, INTENB1
);
1671 r8a66597_usb_disconnect(r8a66597
, 0);
1674 r8a66597_write(r8a66597
, ~BCHG
, INTSTS1
);
1675 r8a66597_bclr(r8a66597
, BCHGE
, INTENB1
);
1676 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597
));
1680 r8a66597_write(r8a66597
, ~SIGN
, INTSTS1
);
1681 status
= get_urb_error(r8a66597
, 0);
1682 check_next_phase(r8a66597
, status
);
1685 r8a66597_write(r8a66597
, ~SACK
, INTSTS1
);
1686 check_next_phase(r8a66597
, 0);
1691 irq_pipe_ready(r8a66597
);
1693 irq_pipe_empty(r8a66597
);
1695 irq_pipe_nrdy(r8a66597
);
1698 spin_unlock(&r8a66597
->lock
);
1702 /* this function must be called with interrupt disabled */
1703 static void r8a66597_root_hub_control(struct r8a66597
*r8a66597
, int port
)
1706 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
1708 if (rh
->port
& USB_PORT_STAT_RESET
) {
1709 unsigned long dvstctr_reg
= get_dvstctr_reg(port
);
1711 tmp
= r8a66597_read(r8a66597
, dvstctr_reg
);
1712 if ((tmp
& USBRST
) == USBRST
) {
1713 r8a66597_mdfy(r8a66597
, UACT
, USBRST
| UACT
,
1715 r8a66597_root_hub_start_polling(r8a66597
);
1717 r8a66597_usb_connect(r8a66597
, port
);
1720 if (!(rh
->port
& USB_PORT_STAT_CONNECTION
)) {
1721 r8a66597_write(r8a66597
, ~ATTCH
, get_intsts_reg(port
));
1722 r8a66597_bset(r8a66597
, ATTCHE
, get_intenb_reg(port
));
1725 if (rh
->scount
> 0) {
1726 tmp
= r8a66597_read(r8a66597
, get_syssts_reg(port
)) & LNST
;
1727 if (tmp
== rh
->old_syssts
) {
1729 if (rh
->scount
== 0)
1730 r8a66597_check_syssts(r8a66597
, port
, tmp
);
1732 r8a66597_root_hub_start_polling(r8a66597
);
1734 rh
->scount
= R8A66597_MAX_SAMPLING
;
1735 rh
->old_syssts
= tmp
;
1736 r8a66597_root_hub_start_polling(r8a66597
);
1741 static void r8a66597_interval_timer(unsigned long _r8a66597
)
1743 struct r8a66597
*r8a66597
= (struct r8a66597
*)_r8a66597
;
1744 unsigned long flags
;
1746 struct r8a66597_td
*td
;
1748 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1750 for (pipenum
= 0; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1751 if (!(r8a66597
->interval_map
& (1 << pipenum
)))
1753 if (timer_pending(&r8a66597
->interval_timer
[pipenum
]))
1756 td
= r8a66597_get_td(r8a66597
, pipenum
);
1758 start_transfer(r8a66597
, td
);
1761 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1764 static void r8a66597_td_timer(unsigned long _r8a66597
)
1766 struct r8a66597
*r8a66597
= (struct r8a66597
*)_r8a66597
;
1767 unsigned long flags
;
1769 struct r8a66597_td
*td
, *new_td
= NULL
;
1770 struct r8a66597_pipe
*pipe
;
1772 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1773 for (pipenum
= 0; pipenum
< R8A66597_MAX_NUM_PIPE
; pipenum
++) {
1774 if (!(r8a66597
->timeout_map
& (1 << pipenum
)))
1776 if (timer_pending(&r8a66597
->td_timer
[pipenum
]))
1779 td
= r8a66597_get_td(r8a66597
, pipenum
);
1781 r8a66597
->timeout_map
&= ~(1 << pipenum
);
1785 if (td
->urb
->actual_length
) {
1786 set_td_timer(r8a66597
, td
);
1791 pipe_stop(r8a66597
, pipe
);
1795 list_move_tail(&new_td
->queue
,
1796 &r8a66597
->pipe_queue
[pipenum
]);
1797 new_td
= r8a66597_get_td(r8a66597
, pipenum
);
1802 } while (td
!= new_td
&& td
->address
== new_td
->address
);
1804 start_transfer(r8a66597
, new_td
);
1807 r8a66597
->timeout_map
&= ~(1 << pipenum
);
1809 set_td_timer(r8a66597
, new_td
);
1812 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1815 static void r8a66597_timer(unsigned long _r8a66597
)
1817 struct r8a66597
*r8a66597
= (struct r8a66597
*)_r8a66597
;
1818 unsigned long flags
;
1821 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1823 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++)
1824 r8a66597_root_hub_control(r8a66597
, port
);
1826 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1829 static int check_pipe_config(struct r8a66597
*r8a66597
, struct urb
*urb
)
1831 struct r8a66597_device
*dev
= get_urb_to_r8a66597_dev(r8a66597
, urb
);
1833 if (dev
&& dev
->address
&& dev
->state
!= USB_STATE_CONFIGURED
&&
1834 (urb
->dev
->state
== USB_STATE_CONFIGURED
))
1840 static int r8a66597_start(struct usb_hcd
*hcd
)
1842 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1844 hcd
->state
= HC_STATE_RUNNING
;
1845 return enable_controller(r8a66597
);
1848 static void r8a66597_stop(struct usb_hcd
*hcd
)
1850 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1852 disable_controller(r8a66597
);
1855 static void set_address_zero(struct r8a66597
*r8a66597
, struct urb
*urb
)
1857 unsigned int usb_address
= usb_pipedevice(urb
->pipe
);
1858 u16 root_port
, hub_port
;
1860 if (usb_address
== 0) {
1861 get_port_number(r8a66597
, urb
->dev
->devpath
,
1862 &root_port
, &hub_port
);
1863 set_devadd_reg(r8a66597
, 0,
1864 get_r8a66597_usb_speed(urb
->dev
->speed
),
1865 get_parent_r8a66597_address(r8a66597
, urb
->dev
),
1866 hub_port
, root_port
);
1870 static struct r8a66597_td
*r8a66597_make_td(struct r8a66597
*r8a66597
,
1872 struct usb_host_endpoint
*hep
)
1874 struct r8a66597_td
*td
;
1877 td
= kzalloc(sizeof(struct r8a66597_td
), GFP_ATOMIC
);
1881 pipenum
= r8a66597_get_pipenum(urb
, hep
);
1882 td
->pipenum
= pipenum
;
1883 td
->pipe
= hep
->hcpriv
;
1885 td
->address
= get_urb_to_r8a66597_addr(r8a66597
, urb
);
1886 td
->maxpacket
= usb_maxpacket(urb
->dev
, urb
->pipe
,
1887 !usb_pipein(urb
->pipe
));
1888 if (usb_pipecontrol(urb
->pipe
))
1889 td
->type
= USB_PID_SETUP
;
1890 else if (usb_pipein(urb
->pipe
))
1891 td
->type
= USB_PID_IN
;
1893 td
->type
= USB_PID_OUT
;
1894 INIT_LIST_HEAD(&td
->queue
);
1899 static int r8a66597_urb_enqueue(struct usb_hcd
*hcd
,
1903 struct usb_host_endpoint
*hep
= urb
->ep
;
1904 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1905 struct r8a66597_td
*td
= NULL
;
1906 int ret
, request
= 0;
1907 unsigned long flags
;
1909 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1910 if (!get_urb_to_r8a66597_dev(r8a66597
, urb
)) {
1912 goto error_not_linked
;
1915 ret
= usb_hcd_link_urb_to_ep(hcd
, urb
);
1917 goto error_not_linked
;
1920 hep
->hcpriv
= kzalloc(sizeof(struct r8a66597_pipe
),
1926 set_pipe_reg_addr(hep
->hcpriv
, R8A66597_PIPE_NO_DMA
);
1927 if (usb_pipeendpoint(urb
->pipe
))
1928 init_pipe_info(r8a66597
, urb
, hep
, &hep
->desc
);
1931 if (unlikely(check_pipe_config(r8a66597
, urb
)))
1932 init_pipe_config(r8a66597
, urb
);
1934 set_address_zero(r8a66597
, urb
);
1935 td
= r8a66597_make_td(r8a66597
, urb
, hep
);
1940 if (list_empty(&r8a66597
->pipe_queue
[td
->pipenum
]))
1942 list_add_tail(&td
->queue
, &r8a66597
->pipe_queue
[td
->pipenum
]);
1946 if (td
->pipe
->info
.timer_interval
) {
1947 r8a66597
->interval_map
|= 1 << td
->pipenum
;
1948 mod_timer(&r8a66597
->interval_timer
[td
->pipenum
],
1949 jiffies
+ msecs_to_jiffies(
1950 td
->pipe
->info
.timer_interval
));
1952 ret
= start_transfer(r8a66597
, td
);
1954 list_del(&td
->queue
);
1959 set_td_timer(r8a66597
, td
);
1963 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
1965 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1969 static int r8a66597_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
,
1972 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1973 struct r8a66597_td
*td
;
1974 unsigned long flags
;
1977 spin_lock_irqsave(&r8a66597
->lock
, flags
);
1978 rc
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
1984 pipe_stop(r8a66597
, td
->pipe
);
1985 pipe_irq_disable(r8a66597
, td
->pipenum
);
1986 disable_irq_empty(r8a66597
, td
->pipenum
);
1987 finish_request(r8a66597
, td
, td
->pipenum
, urb
, status
);
1990 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
1994 static void r8a66597_endpoint_disable(struct usb_hcd
*hcd
,
1995 struct usb_host_endpoint
*hep
)
1997 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
1998 struct r8a66597_pipe
*pipe
= (struct r8a66597_pipe
*)hep
->hcpriv
;
1999 struct r8a66597_td
*td
;
2000 struct urb
*urb
= NULL
;
2002 unsigned long flags
;
2006 pipenum
= pipe
->info
.pipenum
;
2014 spin_lock_irqsave(&r8a66597
->lock
, flags
);
2015 pipe_stop(r8a66597
, pipe
);
2016 pipe_irq_disable(r8a66597
, pipenum
);
2017 disable_irq_empty(r8a66597
, pipenum
);
2018 td
= r8a66597_get_td(r8a66597
, pipenum
);
2021 finish_request(r8a66597
, td
, pipenum
, urb
, -ESHUTDOWN
);
2024 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
2027 static int r8a66597_get_frame(struct usb_hcd
*hcd
)
2029 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2030 return r8a66597_read(r8a66597
, FRMNUM
) & 0x03FF;
2033 static void collect_usb_address_map(struct usb_device
*udev
, unsigned long *map
)
2037 if (udev
->state
== USB_STATE_CONFIGURED
&&
2038 udev
->parent
&& udev
->parent
->devnum
> 1 &&
2039 udev
->parent
->descriptor
.bDeviceClass
== USB_CLASS_HUB
)
2040 map
[udev
->devnum
/32] |= (1 << (udev
->devnum
% 32));
2042 for (chix
= 0; chix
< udev
->maxchild
; chix
++) {
2043 struct usb_device
*childdev
= udev
->children
[chix
];
2046 collect_usb_address_map(childdev
, map
);
2050 /* this function must be called with interrupt disabled */
2051 static struct r8a66597_device
*get_r8a66597_device(struct r8a66597
*r8a66597
,
2054 struct r8a66597_device
*dev
;
2055 struct list_head
*list
= &r8a66597
->child_device
;
2057 list_for_each_entry(dev
, list
, device_list
) {
2058 if (dev
->usb_address
!= addr
)
2064 printk(KERN_ERR
"r8a66597: get_r8a66597_device fail.(%d)\n", addr
);
2068 static void update_usb_address_map(struct r8a66597
*r8a66597
,
2069 struct usb_device
*root_hub
,
2074 unsigned long flags
;
2076 for (i
= 0; i
< 4; i
++) {
2077 diff
= r8a66597
->child_connect_map
[i
] ^ map
[i
];
2081 for (j
= 0; j
< 32; j
++) {
2082 if (!(diff
& (1 << j
)))
2086 if (map
[i
] & (1 << j
))
2087 set_child_connect_map(r8a66597
, addr
);
2089 struct r8a66597_device
*dev
;
2091 spin_lock_irqsave(&r8a66597
->lock
, flags
);
2092 dev
= get_r8a66597_device(r8a66597
, addr
);
2093 disable_r8a66597_pipe_all(r8a66597
, dev
);
2094 free_usb_address(r8a66597
, dev
, 0);
2095 put_child_connect_map(r8a66597
, addr
);
2096 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
2102 static void r8a66597_check_detect_child(struct r8a66597
*r8a66597
,
2103 struct usb_hcd
*hcd
)
2105 struct usb_bus
*bus
;
2106 unsigned long now_map
[4];
2108 memset(now_map
, 0, sizeof(now_map
));
2110 list_for_each_entry(bus
, &usb_bus_list
, bus_list
) {
2114 if (bus
->busnum
!= hcd
->self
.busnum
)
2117 collect_usb_address_map(bus
->root_hub
, now_map
);
2118 update_usb_address_map(r8a66597
, bus
->root_hub
, now_map
);
2122 static int r8a66597_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
2124 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2125 unsigned long flags
;
2128 r8a66597_check_detect_child(r8a66597
, hcd
);
2130 spin_lock_irqsave(&r8a66597
->lock
, flags
);
2132 *buf
= 0; /* initialize (no change) */
2134 for (i
= 0; i
< r8a66597
->max_root_hub
; i
++) {
2135 if (r8a66597
->root_hub
[i
].port
& 0xffff0000)
2136 *buf
|= 1 << (i
+ 1);
2139 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
2144 static void r8a66597_hub_descriptor(struct r8a66597
*r8a66597
,
2145 struct usb_hub_descriptor
*desc
)
2147 desc
->bDescriptorType
= 0x29;
2148 desc
->bHubContrCurrent
= 0;
2149 desc
->bNbrPorts
= r8a66597
->max_root_hub
;
2150 desc
->bDescLength
= 9;
2151 desc
->bPwrOn2PwrGood
= 0;
2152 desc
->wHubCharacteristics
= cpu_to_le16(0x0011);
2153 desc
->bitmap
[0] = ((1 << r8a66597
->max_root_hub
) - 1) << 1;
2154 desc
->bitmap
[1] = ~0;
2157 static int r8a66597_hub_control(struct usb_hcd
*hcd
, u16 typeReq
, u16 wValue
,
2158 u16 wIndex
, char *buf
, u16 wLength
)
2160 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2162 int port
= (wIndex
& 0x00FF) - 1;
2163 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
2164 unsigned long flags
;
2168 spin_lock_irqsave(&r8a66597
->lock
, flags
);
2170 case ClearHubFeature
:
2173 case C_HUB_OVER_CURRENT
:
2174 case C_HUB_LOCAL_POWER
:
2180 case ClearPortFeature
:
2181 if (wIndex
> r8a66597
->max_root_hub
)
2187 case USB_PORT_FEAT_ENABLE
:
2188 rh
->port
&= ~USB_PORT_STAT_POWER
;
2190 case USB_PORT_FEAT_SUSPEND
:
2192 case USB_PORT_FEAT_POWER
:
2193 r8a66597_port_power(r8a66597
, port
, 0);
2195 case USB_PORT_FEAT_C_ENABLE
:
2196 case USB_PORT_FEAT_C_SUSPEND
:
2197 case USB_PORT_FEAT_C_CONNECTION
:
2198 case USB_PORT_FEAT_C_OVER_CURRENT
:
2199 case USB_PORT_FEAT_C_RESET
:
2204 rh
->port
&= ~(1 << wValue
);
2206 case GetHubDescriptor
:
2207 r8a66597_hub_descriptor(r8a66597
,
2208 (struct usb_hub_descriptor
*)buf
);
2214 if (wIndex
> r8a66597
->max_root_hub
)
2216 *(__le32
*)buf
= cpu_to_le32(rh
->port
);
2218 case SetPortFeature
:
2219 if (wIndex
> r8a66597
->max_root_hub
)
2225 case USB_PORT_FEAT_SUSPEND
:
2227 case USB_PORT_FEAT_POWER
:
2228 r8a66597_port_power(r8a66597
, port
, 1);
2229 rh
->port
|= USB_PORT_STAT_POWER
;
2231 case USB_PORT_FEAT_RESET
: {
2232 struct r8a66597_device
*dev
= rh
->dev
;
2234 rh
->port
|= USB_PORT_STAT_RESET
;
2236 disable_r8a66597_pipe_all(r8a66597
, dev
);
2237 free_usb_address(r8a66597
, dev
, 1);
2239 r8a66597_mdfy(r8a66597
, USBRST
, USBRST
| UACT
,
2240 get_dvstctr_reg(port
));
2241 mod_timer(&r8a66597
->rh_timer
,
2242 jiffies
+ msecs_to_jiffies(50));
2248 rh
->port
|= 1 << wValue
;
2256 spin_unlock_irqrestore(&r8a66597
->lock
, flags
);
2260 #if defined(CONFIG_PM)
2261 static int r8a66597_bus_suspend(struct usb_hcd
*hcd
)
2263 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2266 dbg("%s", __func__
);
2268 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++) {
2269 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
2270 unsigned long dvstctr_reg
= get_dvstctr_reg(port
);
2272 if (!(rh
->port
& USB_PORT_STAT_ENABLE
))
2275 dbg("suspend port = %d", port
);
2276 r8a66597_bclr(r8a66597
, UACT
, dvstctr_reg
); /* suspend */
2277 rh
->port
|= USB_PORT_STAT_SUSPEND
;
2279 if (rh
->dev
->udev
->do_remote_wakeup
) {
2280 msleep(3); /* waiting last SOF */
2281 r8a66597_bset(r8a66597
, RWUPE
, dvstctr_reg
);
2282 r8a66597_write(r8a66597
, ~BCHG
, get_intsts_reg(port
));
2283 r8a66597_bset(r8a66597
, BCHGE
, get_intenb_reg(port
));
2287 r8a66597
->bus_suspended
= 1;
2292 static int r8a66597_bus_resume(struct usb_hcd
*hcd
)
2294 struct r8a66597
*r8a66597
= hcd_to_r8a66597(hcd
);
2297 dbg("%s", __func__
);
2299 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++) {
2300 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
2301 unsigned long dvstctr_reg
= get_dvstctr_reg(port
);
2303 if (!(rh
->port
& USB_PORT_STAT_SUSPEND
))
2306 dbg("resume port = %d", port
);
2307 rh
->port
&= ~USB_PORT_STAT_SUSPEND
;
2308 rh
->port
|= USB_PORT_STAT_C_SUSPEND
< 16;
2309 r8a66597_mdfy(r8a66597
, RESUME
, RESUME
| UACT
, dvstctr_reg
);
2311 r8a66597_mdfy(r8a66597
, UACT
, RESUME
| UACT
, dvstctr_reg
);
2318 #define r8a66597_bus_suspend NULL
2319 #define r8a66597_bus_resume NULL
2322 static struct hc_driver r8a66597_hc_driver
= {
2323 .description
= hcd_name
,
2324 .hcd_priv_size
= sizeof(struct r8a66597
),
2325 .irq
= r8a66597_irq
,
2328 * generic hardware linkage
2332 .start
= r8a66597_start
,
2333 .stop
= r8a66597_stop
,
2336 * managing i/o requests and associated device resources
2338 .urb_enqueue
= r8a66597_urb_enqueue
,
2339 .urb_dequeue
= r8a66597_urb_dequeue
,
2340 .endpoint_disable
= r8a66597_endpoint_disable
,
2343 * periodic schedule support
2345 .get_frame_number
= r8a66597_get_frame
,
2350 .hub_status_data
= r8a66597_hub_status_data
,
2351 .hub_control
= r8a66597_hub_control
,
2352 .bus_suspend
= r8a66597_bus_suspend
,
2353 .bus_resume
= r8a66597_bus_resume
,
2356 #if defined(CONFIG_PM)
2357 static int r8a66597_suspend(struct device
*dev
)
2359 struct r8a66597
*r8a66597
= dev_get_drvdata(dev
);
2362 dbg("%s", __func__
);
2364 disable_controller(r8a66597
);
2366 for (port
= 0; port
< r8a66597
->max_root_hub
; port
++) {
2367 struct r8a66597_root_hub
*rh
= &r8a66597
->root_hub
[port
];
2369 rh
->port
= 0x00000000;
2375 static int r8a66597_resume(struct device
*dev
)
2377 struct r8a66597
*r8a66597
= dev_get_drvdata(dev
);
2378 struct usb_hcd
*hcd
= r8a66597_to_hcd(r8a66597
);
2380 dbg("%s", __func__
);
2382 enable_controller(r8a66597
);
2383 usb_root_hub_lost_power(hcd
->self
.root_hub
);
2388 static const struct dev_pm_ops r8a66597_dev_pm_ops
= {
2389 .suspend
= r8a66597_suspend
,
2390 .resume
= r8a66597_resume
,
2391 .poweroff
= r8a66597_suspend
,
2392 .restore
= r8a66597_resume
,
2395 #define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
2396 #else /* if defined(CONFIG_PM) */
2397 #define R8A66597_DEV_PM_OPS NULL
2400 static int __devexit
r8a66597_remove(struct platform_device
*pdev
)
2402 struct r8a66597
*r8a66597
= dev_get_drvdata(&pdev
->dev
);
2403 struct usb_hcd
*hcd
= r8a66597_to_hcd(r8a66597
);
2405 del_timer_sync(&r8a66597
->rh_timer
);
2406 usb_remove_hcd(hcd
);
2407 iounmap(r8a66597
->reg
);
2408 #ifdef CONFIG_HAVE_CLK
2409 if (r8a66597
->pdata
->on_chip
)
2410 clk_put(r8a66597
->clk
);
2416 static int __devinit
r8a66597_probe(struct platform_device
*pdev
)
2418 #ifdef CONFIG_HAVE_CLK
2421 struct resource
*res
= NULL
, *ires
;
2423 void __iomem
*reg
= NULL
;
2424 struct usb_hcd
*hcd
= NULL
;
2425 struct r8a66597
*r8a66597
;
2428 unsigned long irq_trigger
;
2430 if (pdev
->dev
.dma_mask
) {
2432 dev_err(&pdev
->dev
, "dma not supported\n");
2436 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2439 dev_err(&pdev
->dev
, "platform_get_resource error.\n");
2443 ires
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
2447 "platform_get_resource IORESOURCE_IRQ error.\n");
2452 irq_trigger
= ires
->flags
& IRQF_TRIGGER_MASK
;
2454 reg
= ioremap(res
->start
, resource_size(res
));
2457 dev_err(&pdev
->dev
, "ioremap error.\n");
2461 if (pdev
->dev
.platform_data
== NULL
) {
2462 dev_err(&pdev
->dev
, "no platform data\n");
2467 /* initialize hcd */
2468 hcd
= usb_create_hcd(&r8a66597_hc_driver
, &pdev
->dev
, (char *)hcd_name
);
2471 dev_err(&pdev
->dev
, "Failed to create hcd\n");
2474 r8a66597
= hcd_to_r8a66597(hcd
);
2475 memset(r8a66597
, 0, sizeof(struct r8a66597
));
2476 dev_set_drvdata(&pdev
->dev
, r8a66597
);
2477 r8a66597
->pdata
= pdev
->dev
.platform_data
;
2478 r8a66597
->irq_sense_low
= irq_trigger
== IRQF_TRIGGER_LOW
;
2480 if (r8a66597
->pdata
->on_chip
) {
2481 #ifdef CONFIG_HAVE_CLK
2482 snprintf(clk_name
, sizeof(clk_name
), "usb%d", pdev
->id
);
2483 r8a66597
->clk
= clk_get(&pdev
->dev
, clk_name
);
2484 if (IS_ERR(r8a66597
->clk
)) {
2485 dev_err(&pdev
->dev
, "cannot get clock \"%s\"\n",
2487 ret
= PTR_ERR(r8a66597
->clk
);
2491 r8a66597
->max_root_hub
= 1;
2493 r8a66597
->max_root_hub
= 2;
2495 spin_lock_init(&r8a66597
->lock
);
2496 init_timer(&r8a66597
->rh_timer
);
2497 r8a66597
->rh_timer
.function
= r8a66597_timer
;
2498 r8a66597
->rh_timer
.data
= (unsigned long)r8a66597
;
2499 r8a66597
->reg
= reg
;
2501 /* make sure no interrupts are pending */
2502 ret
= r8a66597_clock_enable(r8a66597
);
2505 disable_controller(r8a66597
);
2507 for (i
= 0; i
< R8A66597_MAX_NUM_PIPE
; i
++) {
2508 INIT_LIST_HEAD(&r8a66597
->pipe_queue
[i
]);
2509 init_timer(&r8a66597
->td_timer
[i
]);
2510 r8a66597
->td_timer
[i
].function
= r8a66597_td_timer
;
2511 r8a66597
->td_timer
[i
].data
= (unsigned long)r8a66597
;
2512 setup_timer(&r8a66597
->interval_timer
[i
],
2513 r8a66597_interval_timer
,
2514 (unsigned long)r8a66597
);
2516 INIT_LIST_HEAD(&r8a66597
->child_device
);
2518 hcd
->rsrc_start
= res
->start
;
2520 ret
= usb_add_hcd(hcd
, irq
, IRQF_DISABLED
| irq_trigger
);
2522 dev_err(&pdev
->dev
, "Failed to add hcd\n");
2529 #ifdef CONFIG_HAVE_CLK
2530 if (r8a66597
->pdata
->on_chip
)
2531 clk_put(r8a66597
->clk
);
2543 static struct platform_driver r8a66597_driver
= {
2544 .probe
= r8a66597_probe
,
2545 .remove
= __devexit_p(r8a66597_remove
),
2547 .name
= (char *) hcd_name
,
2548 .owner
= THIS_MODULE
,
2549 .pm
= R8A66597_DEV_PM_OPS
,
2553 static int __init
r8a66597_init(void)
2558 printk(KERN_INFO KBUILD_MODNAME
": driver %s, %s\n", hcd_name
,
2560 return platform_driver_register(&r8a66597_driver
);
2562 module_init(r8a66597_init
);
2564 static void __exit
r8a66597_cleanup(void)
2566 platform_driver_unregister(&r8a66597_driver
);
2568 module_exit(r8a66597_cleanup
);