headers: smp_lock.h redux
[linux-block.git] / drivers / usb / host / r8a66597-hcd.c
CommitLineData
5d304358
YS
1/*
2 * R8A66597 HCD (Host Controller Driver)
3 *
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
8 *
9 * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
10 *
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.
14 *
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.
19 *
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
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/sched.h>
5d304358
YS
29#include <linux/errno.h>
30#include <linux/init.h>
31#include <linux/timer.h>
32#include <linux/delay.h>
33#include <linux/list.h>
34#include <linux/interrupt.h>
35#include <linux/usb.h>
36#include <linux/platform_device.h>
e294531d
YS
37#include <linux/io.h>
38#include <linux/irq.h>
5d304358
YS
39
40#include "../core/hcd.h"
41#include "r8a66597.h"
42
43MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
44MODULE_LICENSE("GPL");
45MODULE_AUTHOR("Yoshihiro Shimoda");
f4fce61d 46MODULE_ALIAS("platform:r8a66597_hcd");
5d304358 47
5effabbe 48#define DRIVER_VERSION "2009-05-26"
5d304358
YS
49
50static const char hcd_name[] = "r8a66597_hcd";
51
5d304358
YS
52static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
53static int r8a66597_get_frame(struct usb_hcd *hcd);
54
55/* this function must be called with interrupt disabled */
56static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
57 unsigned long reg)
58{
59 u16 tmp;
60
61 tmp = r8a66597_read(r8a66597, INTENB0);
62 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
63 r8a66597_bset(r8a66597, 1 << pipenum, reg);
64 r8a66597_write(r8a66597, tmp, INTENB0);
65}
66
67/* this function must be called with interrupt disabled */
68static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
69 unsigned long reg)
70{
71 u16 tmp;
72
73 tmp = r8a66597_read(r8a66597, INTENB0);
74 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
75 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
76 r8a66597_write(r8a66597, tmp, INTENB0);
77}
78
79static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
80 u16 usbspd, u8 upphub, u8 hubport, int port)
81{
82 u16 val;
83 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
84
85 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
86 r8a66597_write(r8a66597, val, devadd_reg);
87}
88
9424ea29 89static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
5d304358
YS
90{
91 u16 tmp;
92 int i = 0;
93
9424ea29 94#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
765786e0
MD
95#if defined(CONFIG_HAVE_CLK)
96 clk_enable(r8a66597->clk);
97#endif
9424ea29
YS
98 do {
99 r8a66597_write(r8a66597, SCKE, SYSCFG0);
100 tmp = r8a66597_read(r8a66597, SYSCFG0);
101 if (i++ > 1000) {
802f389a 102 printk(KERN_ERR "r8a66597: register access fail.\n");
9424ea29
YS
103 return -ENXIO;
104 }
105 } while ((tmp & SCKE) != SCKE);
106 r8a66597_write(r8a66597, 0x04, 0x02);
107#else
5d304358
YS
108 do {
109 r8a66597_write(r8a66597, USBE, SYSCFG0);
110 tmp = r8a66597_read(r8a66597, SYSCFG0);
111 if (i++ > 1000) {
802f389a 112 printk(KERN_ERR "r8a66597: register access fail.\n");
5d304358
YS
113 return -ENXIO;
114 }
115 } while ((tmp & USBE) != USBE);
116 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
5effabbe
YS
117 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata), XTAL,
118 SYSCFG0);
5d304358
YS
119
120 i = 0;
121 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
122 do {
123 msleep(1);
124 tmp = r8a66597_read(r8a66597, SYSCFG0);
125 if (i++ > 500) {
802f389a 126 printk(KERN_ERR "r8a66597: register access fail.\n");
5d304358
YS
127 return -ENXIO;
128 }
129 } while ((tmp & SCKE) != SCKE);
9424ea29
YS
130#endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
131
132 return 0;
133}
134
135static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
136{
137 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
138 udelay(1);
765786e0
MD
139#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
140#if defined(CONFIG_HAVE_CLK)
141 clk_disable(r8a66597->clk);
142#endif
143#else
9424ea29
YS
144 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
145 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
146 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
147#endif
148}
149
150static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
151{
152 u16 val;
153
154 val = port ? DRPD : DCFM | DRPD;
155 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
156 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
157
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));
161}
162
163static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
164{
165 u16 val, tmp;
166
167 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
168 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
5d304358 169
9424ea29
YS
170 r8a66597_port_power(r8a66597, port, 0);
171
172 do {
173 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
174 udelay(640);
175 } while (tmp == EDGESTS);
176
177 val = port ? DRPD : DCFM | DRPD;
178 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
179 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
180}
181
182static int enable_controller(struct r8a66597 *r8a66597)
183{
184 int ret, port;
5effabbe
YS
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;
9424ea29
YS
188
189 ret = r8a66597_clock_enable(r8a66597);
190 if (ret < 0)
191 return ret;
5d304358
YS
192
193 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
5d304358
YS
194 r8a66597_bset(r8a66597, USBE, SYSCFG0);
195
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);
200
5d304358
YS
201 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
202 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
203 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
5d304358
YS
204 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
205
206 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
9424ea29
YS
207
208 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
209 r8a66597_enable_port(r8a66597, port);
5d304358
YS
210
211 return 0;
212}
213
214static void disable_controller(struct r8a66597 *r8a66597)
215{
9424ea29 216 int port;
5d304358
YS
217
218 r8a66597_write(r8a66597, 0, INTENB0);
5d304358 219 r8a66597_write(r8a66597, 0, INTSTS0);
5d304358 220
9424ea29
YS
221 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
222 r8a66597_disable_port(r8a66597, port);
5d304358 223
9424ea29 224 r8a66597_clock_disable(r8a66597);
5d304358
YS
225}
226
227static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
228 struct usb_device *udev)
229{
230 struct r8a66597_device *dev;
231
232 if (udev->parent && udev->parent->devnum != 1)
233 udev = udev->parent;
234
235 dev = dev_get_drvdata(&udev->dev);
236 if (dev)
237 return dev->address;
238 else
239 return 0;
240}
241
242static int is_child_device(char *devpath)
243{
244 return (devpath[2] ? 1 : 0);
245}
246
247static int is_hub_limit(char *devpath)
248{
249 return ((strlen(devpath) >= 4) ? 1 : 0);
250}
251
252static void get_port_number(char *devpath, u16 *root_port, u16 *hub_port)
253{
254 if (root_port) {
255 *root_port = (devpath[0] & 0x0F) - 1;
256 if (*root_port >= R8A66597_MAX_ROOT_HUB)
802f389a 257 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
5d304358
YS
258 }
259 if (hub_port)
260 *hub_port = devpath[2] & 0x0F;
261}
262
263static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
264{
265 u16 usbspd = 0;
266
267 switch (speed) {
268 case USB_SPEED_LOW:
269 usbspd = LSMODE;
270 break;
271 case USB_SPEED_FULL:
272 usbspd = FSMODE;
273 break;
274 case USB_SPEED_HIGH:
275 usbspd = HSMODE;
276 break;
277 default:
802f389a 278 printk(KERN_ERR "r8a66597: unknown speed\n");
5d304358
YS
279 break;
280 }
281
282 return usbspd;
283}
284
285static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
286{
287 int idx;
288
289 idx = address / 32;
290 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
291}
292
293static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
294{
295 int idx;
296
297 idx = address / 32;
298 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
299}
300
301static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
302{
303 u16 pipenum = pipe->info.pipenum;
fe9b9034
ML
304 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
305 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
306 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
5d304358
YS
307
308 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
309 dma_ch = R8A66597_PIPE_NO_DMA;
310
311 pipe->fifoaddr = fifoaddr[dma_ch];
312 pipe->fifosel = fifosel[dma_ch];
313 pipe->fifoctr = fifoctr[dma_ch];
314
315 if (pipenum == 0)
316 pipe->pipectr = DCPCTR;
317 else
318 pipe->pipectr = get_pipectr_addr(pipenum);
319
320 if (check_bulk_or_isoc(pipenum)) {
321 pipe->pipetre = get_pipetre_addr(pipenum);
322 pipe->pipetrn = get_pipetrn_addr(pipenum);
323 } else {
324 pipe->pipetre = 0;
325 pipe->pipetrn = 0;
326 }
327}
328
329static struct r8a66597_device *
330get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
331{
332 if (usb_pipedevice(urb->pipe) == 0)
333 return &r8a66597->device0;
334
335 return dev_get_drvdata(&urb->dev->dev);
336}
337
338static int make_r8a66597_device(struct r8a66597 *r8a66597,
339 struct urb *urb, u8 addr)
340{
341 struct r8a66597_device *dev;
342 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
343
e294531d 344 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
5d304358
YS
345 if (dev == NULL)
346 return -ENOMEM;
347
348 dev_set_drvdata(&urb->dev->dev, dev);
349 dev->udev = urb->dev;
350 dev->address = addr;
351 dev->usb_address = usb_address;
352 dev->state = USB_STATE_ADDRESS;
353 dev->ep_in_toggle = 0;
354 dev->ep_out_toggle = 0;
355 INIT_LIST_HEAD(&dev->device_list);
356 list_add_tail(&dev->device_list, &r8a66597->child_device);
357
358 get_port_number(urb->dev->devpath, &dev->root_port, &dev->hub_port);
359 if (!is_child_device(urb->dev->devpath))
360 r8a66597->root_hub[dev->root_port].dev = dev;
361
362 set_devadd_reg(r8a66597, dev->address,
363 get_r8a66597_usb_speed(urb->dev->speed),
364 get_parent_r8a66597_address(r8a66597, urb->dev),
365 dev->hub_port, dev->root_port);
366
367 return 0;
368}
369
370/* this function must be called with interrupt disabled */
371static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
372{
373 u8 addr; /* R8A66597's address */
374 struct r8a66597_device *dev;
375
376 if (is_hub_limit(urb->dev->devpath)) {
802f389a 377 dev_err(&urb->dev->dev, "External hub limit reached.\n");
5d304358
YS
378 return 0;
379 }
380
381 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
382 if (dev && dev->state >= USB_STATE_ADDRESS)
383 return dev->address;
384
385 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
386 if (r8a66597->address_map & (1 << addr))
387 continue;
388
389 dbg("alloc_address: r8a66597_addr=%d", addr);
390 r8a66597->address_map |= 1 << addr;
391
392 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
393 return 0;
394
395 return addr;
396 }
397
802f389a
GKH
398 dev_err(&urb->dev->dev,
399 "cannot communicate with a USB device more than 10.(%x)\n",
400 r8a66597->address_map);
5d304358
YS
401
402 return 0;
403}
404
405/* this function must be called with interrupt disabled */
406static void free_usb_address(struct r8a66597 *r8a66597,
407 struct r8a66597_device *dev)
408{
409 int port;
410
411 if (!dev)
412 return;
413
414 dbg("free_addr: addr=%d", dev->address);
415
416 dev->state = USB_STATE_DEFAULT;
417 r8a66597->address_map &= ~(1 << dev->address);
418 dev->address = 0;
419 dev_set_drvdata(&dev->udev->dev, NULL);
420 list_del(&dev->device_list);
421 kfree(dev);
422
423 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
424 if (r8a66597->root_hub[port].dev == dev) {
425 r8a66597->root_hub[port].dev = NULL;
426 break;
427 }
428 }
429}
430
431static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
432 u16 mask, u16 loop)
433{
434 u16 tmp;
435 int i = 0;
436
437 do {
438 tmp = r8a66597_read(r8a66597, reg);
439 if (i++ > 1000000) {
802f389a
GKH
440 printk(KERN_ERR "r8a66597: register%lx, loop %x "
441 "is timeout\n", reg, loop);
5d304358
YS
442 break;
443 }
444 ndelay(1);
445 } while ((tmp & mask) != loop);
446}
447
448/* this function must be called with interrupt disabled */
449static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
450{
451 u16 tmp;
452
453 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
454 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
455 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
456 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
457}
458
459/* this function must be called with interrupt disabled */
460static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
461{
462 u16 tmp;
463
464 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
465 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
466 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
467 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
468 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
469}
470
471/* this function must be called with interrupt disabled */
472static void clear_all_buffer(struct r8a66597 *r8a66597,
473 struct r8a66597_pipe *pipe)
474{
475 u16 tmp;
476
477 if (!pipe || pipe->info.pipenum == 0)
478 return;
479
480 pipe_stop(r8a66597, pipe);
481 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
482 tmp = r8a66597_read(r8a66597, pipe->pipectr);
483 tmp = r8a66597_read(r8a66597, pipe->pipectr);
484 tmp = r8a66597_read(r8a66597, pipe->pipectr);
485 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
486}
487
488/* this function must be called with interrupt disabled */
489static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
490 struct r8a66597_pipe *pipe, int toggle)
491{
492 if (toggle)
493 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
494 else
495 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
496}
497
498/* this function must be called with interrupt disabled */
499static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
500{
501 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
502 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
503}
504
505/* this function must be called with interrupt disabled */
506static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
507 struct r8a66597_pipe *pipe)
508{
509 cfifo_change(r8a66597, 0);
510 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D0FIFOSEL);
511 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D1FIFOSEL);
512
513 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum, MBW | CURPIPE,
514 pipe->fifosel);
515 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
516}
517
518static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
519{
520 struct r8a66597_pipe *pipe = hep->hcpriv;
521
522 if (usb_pipeendpoint(urb->pipe) == 0)
523 return 0;
524 else
525 return pipe->info.pipenum;
526}
527
528static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
529{
530 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
531
532 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
533}
534
535static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
536 int urb_pipe)
537{
538 if (!dev)
539 return NULL;
540
541 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
542}
543
544/* this function must be called with interrupt disabled */
545static void pipe_toggle_set(struct r8a66597 *r8a66597,
546 struct r8a66597_pipe *pipe,
547 struct urb *urb, int set)
548{
549 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
550 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
551 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
552
553 if (!toggle)
554 return;
555
556 if (set)
557 *toggle |= 1 << endpoint;
558 else
559 *toggle &= ~(1 << endpoint);
560}
561
562/* this function must be called with interrupt disabled */
563static void pipe_toggle_save(struct r8a66597 *r8a66597,
564 struct r8a66597_pipe *pipe,
565 struct urb *urb)
566{
567 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
568 pipe_toggle_set(r8a66597, pipe, urb, 1);
569 else
570 pipe_toggle_set(r8a66597, pipe, urb, 0);
571}
572
573/* this function must be called with interrupt disabled */
574static void pipe_toggle_restore(struct r8a66597 *r8a66597,
575 struct r8a66597_pipe *pipe,
576 struct urb *urb)
577{
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);
581
f6ace2c9
YS
582 if (!toggle)
583 return;
584
5d304358
YS
585 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
586}
587
588/* this function must be called with interrupt disabled */
589static void pipe_buffer_setting(struct r8a66597 *r8a66597,
590 struct r8a66597_pipe_info *info)
591{
592 u16 val = 0;
593
594 if (info->pipenum == 0)
595 return;
596
597 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
598 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
599 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
600 if (!info->dir_in)
601 val |= R8A66597_DIR;
602 if (info->type == R8A66597_BULK && info->dir_in)
603 val |= R8A66597_DBLB | R8A66597_SHTNAK;
604 val |= info->type | info->epnum;
605 r8a66597_write(r8a66597, val, PIPECFG);
606
607 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
608 PIPEBUF);
609 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
610 PIPEMAXP);
5d304358
YS
611 r8a66597_write(r8a66597, info->interval, PIPEPERI);
612}
613
5d304358
YS
614/* this function must be called with interrupt disabled */
615static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
616{
617 struct r8a66597_pipe_info *info;
618 struct urb *urb = td->urb;
619
620 if (td->pipenum > 0) {
621 info = &td->pipe->info;
622 cfifo_change(r8a66597, 0);
623 pipe_buffer_setting(r8a66597, info);
624
625 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
626 usb_pipeout(urb->pipe)) &&
627 !usb_pipecontrol(urb->pipe)) {
628 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
629 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
630 clear_all_buffer(r8a66597, td->pipe);
631 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
632 usb_pipeout(urb->pipe), 1);
633 }
634 pipe_toggle_restore(r8a66597, td->pipe, urb);
635 }
636}
637
638/* this function must be called with interrupt disabled */
639static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
640 struct usb_endpoint_descriptor *ep)
641{
642 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
643
644 memset(array, 0, sizeof(array));
2e0fe709 645 switch (usb_endpoint_type(ep)) {
e294531d 646 case USB_ENDPOINT_XFER_BULK:
2e0fe709 647 if (usb_endpoint_dir_in(ep))
5d304358
YS
648 array[i++] = 4;
649 else {
650 array[i++] = 3;
651 array[i++] = 5;
652 }
e294531d
YS
653 break;
654 case USB_ENDPOINT_XFER_INT:
2e0fe709 655 if (usb_endpoint_dir_in(ep)) {
5d304358
YS
656 array[i++] = 6;
657 array[i++] = 7;
658 array[i++] = 8;
659 } else
660 array[i++] = 9;
e294531d
YS
661 break;
662 case USB_ENDPOINT_XFER_ISOC:
2e0fe709 663 if (usb_endpoint_dir_in(ep))
5d304358
YS
664 array[i++] = 2;
665 else
666 array[i++] = 1;
e294531d
YS
667 break;
668 default:
802f389a 669 printk(KERN_ERR "r8a66597: Illegal type\n");
e294531d
YS
670 return 0;
671 }
5d304358
YS
672
673 i = 1;
674 min = array[0];
675 while (array[i] != 0) {
676 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
677 min = array[i];
678 i++;
679 }
680
681 return min;
682}
683
684static u16 get_r8a66597_type(__u8 type)
685{
686 u16 r8a66597_type;
687
e294531d 688 switch (type) {
5d304358
YS
689 case USB_ENDPOINT_XFER_BULK:
690 r8a66597_type = R8A66597_BULK;
691 break;
692 case USB_ENDPOINT_XFER_INT:
693 r8a66597_type = R8A66597_INT;
694 break;
695 case USB_ENDPOINT_XFER_ISOC:
696 r8a66597_type = R8A66597_ISO;
697 break;
698 default:
802f389a 699 printk(KERN_ERR "r8a66597: Illegal type\n");
5d304358
YS
700 r8a66597_type = 0x0000;
701 break;
702 }
703
704 return r8a66597_type;
705}
706
707static u16 get_bufnum(u16 pipenum)
708{
709 u16 bufnum = 0;
710
711 if (pipenum == 0)
712 bufnum = 0;
713 else if (check_bulk_or_isoc(pipenum))
714 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
715 else if (check_interrupt(pipenum))
716 bufnum = 4 + (pipenum - 6);
717 else
802f389a 718 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
5d304358
YS
719
720 return bufnum;
721}
722
723static u16 get_buf_bsize(u16 pipenum)
724{
725 u16 buf_bsize = 0;
726
727 if (pipenum == 0)
728 buf_bsize = 3;
729 else if (check_bulk_or_isoc(pipenum))
730 buf_bsize = R8A66597_BUF_BSIZE - 1;
731 else if (check_interrupt(pipenum))
732 buf_bsize = 0;
733 else
802f389a 734 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
5d304358
YS
735
736 return buf_bsize;
737}
738
739/* this function must be called with interrupt disabled */
740static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
741 struct r8a66597_device *dev,
742 struct r8a66597_pipe *pipe,
743 struct urb *urb)
744{
9424ea29 745#if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
5d304358
YS
746 int i;
747 struct r8a66597_pipe_info *info = &pipe->info;
748
749 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
750 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
751 if ((r8a66597->dma_map & (1 << i)) != 0)
752 continue;
753
5909f6ea
GKH
754 dev_info(&dev->udev->dev,
755 "address %d, EndpointAddress 0x%02x use "
756 "DMA FIFO\n", usb_pipedevice(urb->pipe),
757 info->dir_in ?
758 USB_ENDPOINT_DIR_MASK + info->epnum
759 : info->epnum);
5d304358
YS
760
761 r8a66597->dma_map |= 1 << i;
762 dev->dma_map |= 1 << i;
763 set_pipe_reg_addr(pipe, i);
764
765 cfifo_change(r8a66597, 0);
766 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum,
767 MBW | CURPIPE, pipe->fifosel);
768
769 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
770 pipe->info.pipenum);
771 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
772 break;
773 }
774 }
9424ea29 775#endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
5d304358
YS
776}
777
778/* this function must be called with interrupt disabled */
779static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
780 struct usb_host_endpoint *hep,
781 struct r8a66597_pipe_info *info)
782{
783 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
784 struct r8a66597_pipe *pipe = hep->hcpriv;
785
786 dbg("enable_pipe:");
787
788 pipe->info = *info;
789 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
790 r8a66597->pipe_cnt[pipe->info.pipenum]++;
791 dev->pipe_cnt[pipe->info.pipenum]++;
792
793 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
794}
795
796/* this function must be called with interrupt disabled */
797static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
798{
799 struct r8a66597_td *td, *next;
800 struct urb *urb;
801 struct list_head *list = &r8a66597->pipe_queue[pipenum];
802
803 if (list_empty(list))
804 return;
805
806 list_for_each_entry_safe(td, next, list, queue) {
807 if (!td)
808 continue;
809 if (td->address != address)
810 continue;
811
812 urb = td->urb;
813 list_del(&td->queue);
814 kfree(td);
815
816 if (urb) {
e9df41c5
AS
817 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
818 urb);
819
5d304358 820 spin_unlock(&r8a66597->lock);
4a00027d
AS
821 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
822 -ENODEV);
5d304358
YS
823 spin_lock(&r8a66597->lock);
824 }
825 break;
826 }
827}
828
829/* this function must be called with interrupt disabled */
830static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
831 struct r8a66597_device *dev)
832{
833 int check_ep0 = 0;
834 u16 pipenum;
835
836 if (!dev)
837 return;
838
839 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
840 if (!dev->pipe_cnt[pipenum])
841 continue;
842
843 if (!check_ep0) {
844 check_ep0 = 1;
845 force_dequeue(r8a66597, 0, dev->address);
846 }
847
848 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
849 dev->pipe_cnt[pipenum] = 0;
850 force_dequeue(r8a66597, pipenum, dev->address);
851 }
852
853 dbg("disable_pipe");
854
855 r8a66597->dma_map &= ~(dev->dma_map);
856 dev->dma_map = 0;
857}
858
397f519a
YS
859static u16 get_interval(struct urb *urb, __u8 interval)
860{
861 u16 time = 1;
862 int i;
863
864 if (urb->dev->speed == USB_SPEED_HIGH) {
865 if (interval > IITV)
866 time = IITV;
867 else
868 time = interval ? interval - 1 : 0;
869 } else {
870 if (interval > 128) {
871 time = IITV;
872 } else {
873 /* calculate the nearest value for PIPEPERI */
874 for (i = 0; i < 7; i++) {
875 if ((1 << i) < interval &&
876 (1 << (i + 1) > interval))
877 time = 1 << i;
878 }
879 }
880 }
881
882 return time;
883}
884
6d879107
YS
885static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
886{
887 __u8 i;
888 unsigned long time = 1;
889
890 if (usb_pipeisoc(urb->pipe))
891 return 0;
892
893 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
894 for (i = 0; i < (interval - 1); i++)
895 time *= 2;
896 time = time * 125 / 1000; /* uSOF -> msec */
897 } else {
898 time = interval;
899 }
900
901 return time;
902}
903
5d304358
YS
904/* this function must be called with interrupt disabled */
905static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
906 struct usb_host_endpoint *hep,
907 struct usb_endpoint_descriptor *ep)
908{
909 struct r8a66597_pipe_info info;
910
911 info.pipenum = get_empty_pipenum(r8a66597, ep);
912 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
2e0fe709 913 info.epnum = usb_endpoint_num(ep);
05eac910 914 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
2e0fe709 915 info.type = get_r8a66597_type(usb_endpoint_type(ep));
5d304358
YS
916 info.bufnum = get_bufnum(info.pipenum);
917 info.buf_bsize = get_buf_bsize(info.pipenum);
6d879107
YS
918 if (info.type == R8A66597_BULK) {
919 info.interval = 0;
920 info.timer_interval = 0;
921 } else {
397f519a 922 info.interval = get_interval(urb, ep->bInterval);
6d879107
YS
923 info.timer_interval = get_timer_interval(urb, ep->bInterval);
924 }
2e0fe709 925 if (usb_endpoint_dir_in(ep))
5d304358
YS
926 info.dir_in = 1;
927 else
928 info.dir_in = 0;
929
930 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
931}
932
933static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
934{
935 struct r8a66597_device *dev;
936
937 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
938 dev->state = USB_STATE_CONFIGURED;
939}
940
941static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
942 u16 pipenum)
943{
944 if (pipenum == 0 && usb_pipeout(urb->pipe))
945 enable_irq_empty(r8a66597, pipenum);
946 else
947 enable_irq_ready(r8a66597, pipenum);
948
949 if (!usb_pipeisoc(urb->pipe))
950 enable_irq_nrdy(r8a66597, pipenum);
951}
952
953static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
954{
955 disable_irq_ready(r8a66597, pipenum);
956 disable_irq_nrdy(r8a66597, pipenum);
957}
958
54d0be9e
YS
959static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
960{
961 mod_timer(&r8a66597->rh_timer,
962 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
963}
964
965static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
966 int connect)
967{
968 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
969
970 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
971 rh->scount = R8A66597_MAX_SAMPLING;
972 if (connect)
973 rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
974 else
975 rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
976 rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
977
978 r8a66597_root_hub_start_polling(r8a66597);
979}
980
5d304358 981/* this function must be called with interrupt disabled */
29fab0cd
YS
982static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
983 u16 syssts)
5d304358 984{
29fab0cd 985 if (syssts == SE0) {
54d0be9e 986 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
29fab0cd
YS
987 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
988 return;
989 }
990
991 if (syssts == FS_JSTS)
992 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
993 else if (syssts == LS_JSTS)
994 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
995
e294531d 996 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
5d304358 997 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
e1e609be
YS
998
999 if (r8a66597->bus_suspended)
1000 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
5d304358
YS
1001}
1002
1003/* this function must be called with interrupt disabled */
1004static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1005{
1006 u16 speed = get_rh_usb_speed(r8a66597, port);
1007 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1008
1009 if (speed == HSMODE)
1010 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
1011 else if (speed == LSMODE)
1012 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
1013
1014 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
1015 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1016}
1017
1018/* this function must be called with interrupt disabled */
1019static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1020{
1021 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1022
5d304358
YS
1023 disable_r8a66597_pipe_all(r8a66597, dev);
1024 free_usb_address(r8a66597, dev);
1025
54d0be9e 1026 start_root_hub_sampling(r8a66597, port, 0);
5d304358
YS
1027}
1028
1029/* this function must be called with interrupt disabled */
1030static void prepare_setup_packet(struct r8a66597 *r8a66597,
1031 struct r8a66597_td *td)
1032{
1033 int i;
fd05e720 1034 __le16 *p = (__le16 *)td->urb->setup_packet;
5d304358
YS
1035 unsigned long setup_addr = USBREQ;
1036
1037 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1038 DCPMAXP);
e294531d 1039 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
5d304358
YS
1040
1041 for (i = 0; i < 4; i++) {
fd05e720 1042 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
5d304358
YS
1043 setup_addr += 2;
1044 }
1045 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1046}
1047
1048/* this function must be called with interrupt disabled */
1049static void prepare_packet_read(struct r8a66597 *r8a66597,
1050 struct r8a66597_td *td)
1051{
1052 struct urb *urb = td->urb;
1053
1054 if (usb_pipecontrol(urb->pipe)) {
1055 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1056 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1057 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1058 if (urb->actual_length == 0) {
1059 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1060 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1061 }
1062 pipe_irq_disable(r8a66597, td->pipenum);
1063 pipe_start(r8a66597, td->pipe);
1064 pipe_irq_enable(r8a66597, urb, td->pipenum);
1065 } else {
1066 if (urb->actual_length == 0) {
1067 pipe_irq_disable(r8a66597, td->pipenum);
1068 pipe_setting(r8a66597, td);
1069 pipe_stop(r8a66597, td->pipe);
e294531d 1070 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
5d304358
YS
1071
1072 if (td->pipe->pipetre) {
1073 r8a66597_write(r8a66597, TRCLR,
e294531d 1074 td->pipe->pipetre);
5d304358 1075 r8a66597_write(r8a66597,
dfa5ec79
JL
1076 DIV_ROUND_UP
1077 (urb->transfer_buffer_length,
1078 td->maxpacket),
e294531d 1079 td->pipe->pipetrn);
5d304358 1080 r8a66597_bset(r8a66597, TRENB,
e294531d 1081 td->pipe->pipetre);
5d304358
YS
1082 }
1083
1084 pipe_start(r8a66597, td->pipe);
1085 pipe_irq_enable(r8a66597, urb, td->pipenum);
1086 }
1087 }
1088}
1089
1090/* this function must be called with interrupt disabled */
1091static void prepare_packet_write(struct r8a66597 *r8a66597,
1092 struct r8a66597_td *td)
1093{
1094 u16 tmp;
1095 struct urb *urb = td->urb;
1096
1097 if (usb_pipecontrol(urb->pipe)) {
1098 pipe_stop(r8a66597, td->pipe);
1099 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1100 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1101 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1102 if (urb->actual_length == 0) {
1103 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1104 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1105 }
1106 } else {
1107 if (urb->actual_length == 0)
1108 pipe_setting(r8a66597, td);
1109 if (td->pipe->pipetre)
1110 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1111 }
e294531d 1112 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
5d304358
YS
1113
1114 fifo_change_from_pipe(r8a66597, td->pipe);
1115 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1116 if (unlikely((tmp & FRDY) == 0))
1117 pipe_irq_enable(r8a66597, urb, td->pipenum);
1118 else
1119 packet_write(r8a66597, td->pipenum);
1120 pipe_start(r8a66597, td->pipe);
1121}
1122
1123/* this function must be called with interrupt disabled */
1124static void prepare_status_packet(struct r8a66597 *r8a66597,
1125 struct r8a66597_td *td)
1126{
1127 struct urb *urb = td->urb;
1128
1129 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
e294531d 1130 pipe_stop(r8a66597, td->pipe);
5d304358
YS
1131
1132 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1133 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1134 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1135 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
e294531d 1136 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
9424ea29 1137 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
5d304358
YS
1138 enable_irq_empty(r8a66597, 0);
1139 } else {
1140 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1141 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1142 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1143 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
5d304358
YS
1144 enable_irq_ready(r8a66597, 0);
1145 }
1146 enable_irq_nrdy(r8a66597, 0);
1147 pipe_start(r8a66597, td->pipe);
1148}
1149
e3a09051
YS
1150static int is_set_address(unsigned char *setup_packet)
1151{
1152 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1153 setup_packet[1] == USB_REQ_SET_ADDRESS)
1154 return 1;
1155 else
1156 return 0;
1157}
1158
5d304358
YS
1159/* this function must be called with interrupt disabled */
1160static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1161{
1162 BUG_ON(!td);
1163
1164 switch (td->type) {
1165 case USB_PID_SETUP:
e3a09051 1166 if (is_set_address(td->urb->setup_packet)) {
5d304358
YS
1167 td->set_address = 1;
1168 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1169 td->urb);
1170 if (td->urb->setup_packet[2] == 0)
1171 return -EPIPE;
1172 }
1173 prepare_setup_packet(r8a66597, td);
1174 break;
1175 case USB_PID_IN:
1176 prepare_packet_read(r8a66597, td);
1177 break;
1178 case USB_PID_OUT:
1179 prepare_packet_write(r8a66597, td);
1180 break;
1181 case USB_PID_ACK:
1182 prepare_status_packet(r8a66597, td);
1183 break;
1184 default:
802f389a 1185 printk(KERN_ERR "r8a66597: invalid type.\n");
5d304358
YS
1186 break;
1187 }
1188
1189 return 0;
1190}
1191
1192static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1193{
1194 if (usb_pipeisoc(urb->pipe)) {
1195 if (urb->number_of_packets == td->iso_cnt)
1196 return 1;
1197 }
1198
1199 /* control or bulk or interrupt */
1200 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1201 (td->short_packet) || (td->zero_packet))
1202 return 1;
1203
1204 return 0;
1205}
1206
1207/* this function must be called with interrupt disabled */
1208static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1209{
1210 unsigned long time;
1211
1212 BUG_ON(!td);
1213
1214 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1215 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1216 r8a66597->timeout_map |= 1 << td->pipenum;
1217 switch (usb_pipetype(td->urb->pipe)) {
1218 case PIPE_INTERRUPT:
1219 case PIPE_ISOCHRONOUS:
1220 time = 30;
1221 break;
1222 default:
1223 time = 300;
1224 break;
1225 }
1226
1227 mod_timer(&r8a66597->td_timer[td->pipenum],
1228 jiffies + msecs_to_jiffies(time));
1229 }
1230}
1231
1232/* this function must be called with interrupt disabled */
dfd1e537 1233static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
888fda47 1234 u16 pipenum, struct urb *urb, int status)
dfd1e537 1235__releases(r8a66597->lock) __acquires(r8a66597->lock)
5d304358
YS
1236{
1237 int restart = 0;
1238 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1239
1240 r8a66597->timeout_map &= ~(1 << pipenum);
1241
1242 if (likely(td)) {
888fda47 1243 if (td->set_address && (status != 0 || urb->unlinked))
5d304358
YS
1244 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1245
1246 pipe_toggle_save(r8a66597, td->pipe, urb);
1247 list_del(&td->queue);
1248 kfree(td);
1249 }
1250
1251 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1252 restart = 1;
1253
1254 if (likely(urb)) {
1255 if (usb_pipeisoc(urb->pipe))
1256 urb->start_frame = r8a66597_get_frame(hcd);
1257
e9df41c5 1258 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
5d304358 1259 spin_unlock(&r8a66597->lock);
4a00027d 1260 usb_hcd_giveback_urb(hcd, urb, status);
5d304358
YS
1261 spin_lock(&r8a66597->lock);
1262 }
1263
1264 if (restart) {
1265 td = r8a66597_get_td(r8a66597, pipenum);
1266 if (unlikely(!td))
1267 return;
1268
1269 start_transfer(r8a66597, td);
1270 set_td_timer(r8a66597, td);
1271 }
1272}
1273
5d304358
YS
1274static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1275{
1276 u16 tmp;
1277 int rcv_len, bufsize, urb_len, size;
1278 u16 *buf;
1279 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1280 struct urb *urb;
1281 int finish = 0;
dfd1e537 1282 int status = 0;
5d304358
YS
1283
1284 if (unlikely(!td))
1285 return;
1286 urb = td->urb;
1287
1288 fifo_change_from_pipe(r8a66597, td->pipe);
1289 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1290 if (unlikely((tmp & FRDY) == 0)) {
5d304358
YS
1291 pipe_stop(r8a66597, td->pipe);
1292 pipe_irq_disable(r8a66597, pipenum);
802f389a 1293 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
888fda47 1294 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
5d304358
YS
1295 return;
1296 }
1297
1298 /* prepare parameters */
1299 rcv_len = tmp & DTLN;
5d304358
YS
1300 if (usb_pipeisoc(urb->pipe)) {
1301 buf = (u16 *)(urb->transfer_buffer +
1302 urb->iso_frame_desc[td->iso_cnt].offset);
1303 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1304 } else {
1305 buf = (void *)urb->transfer_buffer + urb->actual_length;
1306 urb_len = urb->transfer_buffer_length - urb->actual_length;
1307 }
dfd1e537
AS
1308 bufsize = min(urb_len, (int) td->maxpacket);
1309 if (rcv_len <= bufsize) {
1310 size = rcv_len;
1311 } else {
1312 size = bufsize;
1313 status = -EOVERFLOW;
1314 finish = 1;
1315 }
5d304358
YS
1316
1317 /* update parameters */
1318 urb->actual_length += size;
1319 if (rcv_len == 0)
1320 td->zero_packet = 1;
dfd1e537 1321 if (rcv_len < bufsize) {
5d304358 1322 td->short_packet = 1;
5d304358
YS
1323 }
1324 if (usb_pipeisoc(urb->pipe)) {
1325 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
dfd1e537 1326 urb->iso_frame_desc[td->iso_cnt].status = status;
5d304358 1327 td->iso_cnt++;
dfd1e537 1328 finish = 0;
5d304358
YS
1329 }
1330
1331 /* check transfer finish */
b0d9efba 1332 if (finish || check_transfer_finish(td, urb)) {
5d304358
YS
1333 pipe_stop(r8a66597, td->pipe);
1334 pipe_irq_disable(r8a66597, pipenum);
1335 finish = 1;
1336 }
1337
1338 /* read fifo */
1339 if (urb->transfer_buffer) {
1340 if (size == 0)
1341 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1342 else
1343 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1344 buf, size);
1345 }
1346
888fda47
AS
1347 if (finish && pipenum != 0)
1348 finish_request(r8a66597, td, pipenum, urb, status);
5d304358
YS
1349}
1350
1351static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1352{
1353 u16 tmp;
1354 int bufsize, size;
1355 u16 *buf;
1356 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1357 struct urb *urb;
1358
1359 if (unlikely(!td))
1360 return;
1361 urb = td->urb;
1362
1363 fifo_change_from_pipe(r8a66597, td->pipe);
1364 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1365 if (unlikely((tmp & FRDY) == 0)) {
5d304358
YS
1366 pipe_stop(r8a66597, td->pipe);
1367 pipe_irq_disable(r8a66597, pipenum);
802f389a 1368 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
888fda47 1369 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
5d304358
YS
1370 return;
1371 }
1372
1373 /* prepare parameters */
1374 bufsize = td->maxpacket;
1375 if (usb_pipeisoc(urb->pipe)) {
1376 buf = (u16 *)(urb->transfer_buffer +
1377 urb->iso_frame_desc[td->iso_cnt].offset);
1378 size = min(bufsize,
1379 (int)urb->iso_frame_desc[td->iso_cnt].length);
1380 } else {
1381 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
16e2e5f6 1382 size = min_t(u32, bufsize,
5d304358
YS
1383 urb->transfer_buffer_length - urb->actual_length);
1384 }
1385
1386 /* write fifo */
1387 if (pipenum > 0)
e294531d 1388 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
5d304358
YS
1389 if (urb->transfer_buffer) {
1390 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1391 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1392 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1393 }
1394
1395 /* update parameters */
1396 urb->actual_length += size;
1397 if (usb_pipeisoc(urb->pipe)) {
1398 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1399 urb->iso_frame_desc[td->iso_cnt].status = 0;
1400 td->iso_cnt++;
1401 }
1402
1403 /* check transfer finish */
1404 if (check_transfer_finish(td, urb)) {
1405 disable_irq_ready(r8a66597, pipenum);
1406 enable_irq_empty(r8a66597, pipenum);
1407 if (!usb_pipeisoc(urb->pipe))
1408 enable_irq_nrdy(r8a66597, pipenum);
1409 } else
1410 pipe_irq_enable(r8a66597, urb, pipenum);
1411}
1412
1413
888fda47 1414static void check_next_phase(struct r8a66597 *r8a66597, int status)
5d304358
YS
1415{
1416 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1417 struct urb *urb;
1418 u8 finish = 0;
1419
1420 if (unlikely(!td))
1421 return;
1422 urb = td->urb;
1423
1424 switch (td->type) {
1425 case USB_PID_IN:
1426 case USB_PID_OUT:
5d304358
YS
1427 if (check_transfer_finish(td, urb))
1428 td->type = USB_PID_ACK;
1429 break;
1430 case USB_PID_SETUP:
eb231054 1431 if (urb->transfer_buffer_length == urb->actual_length)
5d304358 1432 td->type = USB_PID_ACK;
eb231054 1433 else if (usb_pipeout(urb->pipe))
5d304358
YS
1434 td->type = USB_PID_OUT;
1435 else
1436 td->type = USB_PID_IN;
1437 break;
1438 case USB_PID_ACK:
1439 finish = 1;
5d304358
YS
1440 break;
1441 }
1442
888fda47
AS
1443 if (finish || status != 0 || urb->unlinked)
1444 finish_request(r8a66597, td, 0, urb, status);
5d304358
YS
1445 else
1446 start_transfer(r8a66597, td);
1447}
1448
888fda47 1449static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
5d304358
YS
1450{
1451 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1452
888fda47 1453 if (td) {
5d304358
YS
1454 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1455
1456 if (pid == PID_NAK)
888fda47 1457 return -ECONNRESET;
5d304358 1458 else
888fda47 1459 return -EPIPE;
5d304358 1460 }
888fda47 1461 return 0;
5d304358
YS
1462}
1463
1464static void irq_pipe_ready(struct r8a66597 *r8a66597)
1465{
1466 u16 check;
1467 u16 pipenum;
1468 u16 mask;
1469 struct r8a66597_td *td;
1470
1471 mask = r8a66597_read(r8a66597, BRDYSTS)
1472 & r8a66597_read(r8a66597, BRDYENB);
e294531d 1473 r8a66597_write(r8a66597, ~mask, BRDYSTS);
5d304358
YS
1474 if (mask & BRDY0) {
1475 td = r8a66597_get_td(r8a66597, 0);
1476 if (td && td->type == USB_PID_IN)
1477 packet_read(r8a66597, 0);
1478 else
1479 pipe_irq_disable(r8a66597, 0);
888fda47 1480 check_next_phase(r8a66597, 0);
5d304358
YS
1481 }
1482
1483 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1484 check = 1 << pipenum;
1485 if (mask & check) {
1486 td = r8a66597_get_td(r8a66597, pipenum);
1487 if (unlikely(!td))
1488 continue;
1489
1490 if (td->type == USB_PID_IN)
1491 packet_read(r8a66597, pipenum);
1492 else if (td->type == USB_PID_OUT)
1493 packet_write(r8a66597, pipenum);
1494 }
1495 }
1496}
1497
1498static void irq_pipe_empty(struct r8a66597 *r8a66597)
1499{
1500 u16 tmp;
1501 u16 check;
1502 u16 pipenum;
1503 u16 mask;
1504 struct r8a66597_td *td;
1505
1506 mask = r8a66597_read(r8a66597, BEMPSTS)
1507 & r8a66597_read(r8a66597, BEMPENB);
e294531d 1508 r8a66597_write(r8a66597, ~mask, BEMPSTS);
5d304358
YS
1509 if (mask & BEMP0) {
1510 cfifo_change(r8a66597, 0);
1511 td = r8a66597_get_td(r8a66597, 0);
1512 if (td && td->type != USB_PID_OUT)
1513 disable_irq_empty(r8a66597, 0);
888fda47 1514 check_next_phase(r8a66597, 0);
5d304358
YS
1515 }
1516
1517 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1518 check = 1 << pipenum;
1519 if (mask & check) {
1520 struct r8a66597_td *td;
1521 td = r8a66597_get_td(r8a66597, pipenum);
1522 if (unlikely(!td))
1523 continue;
1524
1525 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1526 if ((tmp & INBUFM) == 0) {
1527 disable_irq_empty(r8a66597, pipenum);
1528 pipe_irq_disable(r8a66597, pipenum);
888fda47
AS
1529 finish_request(r8a66597, td, pipenum, td->urb,
1530 0);
5d304358
YS
1531 }
1532 }
1533 }
1534}
1535
1536static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1537{
1538 u16 check;
1539 u16 pipenum;
1540 u16 mask;
888fda47 1541 int status;
5d304358
YS
1542
1543 mask = r8a66597_read(r8a66597, NRDYSTS)
1544 & r8a66597_read(r8a66597, NRDYENB);
e294531d 1545 r8a66597_write(r8a66597, ~mask, NRDYSTS);
5d304358
YS
1546 if (mask & NRDY0) {
1547 cfifo_change(r8a66597, 0);
888fda47 1548 status = get_urb_error(r8a66597, 0);
5d304358 1549 pipe_irq_disable(r8a66597, 0);
888fda47 1550 check_next_phase(r8a66597, status);
5d304358
YS
1551 }
1552
1553 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1554 check = 1 << pipenum;
1555 if (mask & check) {
1556 struct r8a66597_td *td;
1557 td = r8a66597_get_td(r8a66597, pipenum);
1558 if (unlikely(!td))
1559 continue;
1560
888fda47 1561 status = get_urb_error(r8a66597, pipenum);
5d304358
YS
1562 pipe_irq_disable(r8a66597, pipenum);
1563 pipe_stop(r8a66597, td->pipe);
888fda47 1564 finish_request(r8a66597, td, pipenum, td->urb, status);
5d304358
YS
1565 }
1566 }
1567}
1568
5d304358
YS
1569static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1570{
1571 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1572 u16 intsts0, intsts1, intsts2;
1573 u16 intenb0, intenb1, intenb2;
1574 u16 mask0, mask1, mask2;
888fda47 1575 int status;
5d304358
YS
1576
1577 spin_lock(&r8a66597->lock);
1578
1579 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1580 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1581 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1582 intenb0 = r8a66597_read(r8a66597, INTENB0);
1583 intenb1 = r8a66597_read(r8a66597, INTENB1);
1584 intenb2 = r8a66597_read(r8a66597, INTENB2);
1585
1586 mask2 = intsts2 & intenb2;
1587 mask1 = intsts1 & intenb1;
1588 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1589 if (mask2) {
1590 if (mask2 & ATTCH) {
e294531d 1591 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
5d304358
YS
1592 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1593
1594 /* start usb bus sampling */
54d0be9e 1595 start_root_hub_sampling(r8a66597, 1, 1);
5d304358
YS
1596 }
1597 if (mask2 & DTCH) {
e294531d 1598 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
5d304358
YS
1599 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1600 r8a66597_usb_disconnect(r8a66597, 1);
1601 }
e1e609be
YS
1602 if (mask2 & BCHG) {
1603 r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1604 r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1605 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1606 }
5d304358
YS
1607 }
1608
1609 if (mask1) {
1610 if (mask1 & ATTCH) {
e294531d 1611 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
5d304358
YS
1612 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1613
1614 /* start usb bus sampling */
54d0be9e 1615 start_root_hub_sampling(r8a66597, 0, 1);
5d304358
YS
1616 }
1617 if (mask1 & DTCH) {
e294531d 1618 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
5d304358
YS
1619 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1620 r8a66597_usb_disconnect(r8a66597, 0);
1621 }
e1e609be
YS
1622 if (mask1 & BCHG) {
1623 r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1624 r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1625 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1626 }
1627
5d304358 1628 if (mask1 & SIGN) {
e294531d 1629 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
888fda47
AS
1630 status = get_urb_error(r8a66597, 0);
1631 check_next_phase(r8a66597, status);
5d304358
YS
1632 }
1633 if (mask1 & SACK) {
e294531d 1634 r8a66597_write(r8a66597, ~SACK, INTSTS1);
888fda47 1635 check_next_phase(r8a66597, 0);
5d304358
YS
1636 }
1637 }
1638 if (mask0) {
1639 if (mask0 & BRDY)
1640 irq_pipe_ready(r8a66597);
1641 if (mask0 & BEMP)
1642 irq_pipe_empty(r8a66597);
1643 if (mask0 & NRDY)
1644 irq_pipe_nrdy(r8a66597);
1645 }
1646
1647 spin_unlock(&r8a66597->lock);
1648 return IRQ_HANDLED;
1649}
1650
1651/* this function must be called with interrupt disabled */
1652static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1653{
1654 u16 tmp;
1655 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1656
1657 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1658 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1659
1660 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1661 if ((tmp & USBRST) == USBRST) {
1662 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1663 dvstctr_reg);
29fab0cd 1664 r8a66597_root_hub_start_polling(r8a66597);
5d304358
YS
1665 } else
1666 r8a66597_usb_connect(r8a66597, port);
1667 }
1668
29fab0cd
YS
1669 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1670 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1671 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1672 }
1673
5d304358
YS
1674 if (rh->scount > 0) {
1675 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1676 if (tmp == rh->old_syssts) {
1677 rh->scount--;
29fab0cd
YS
1678 if (rh->scount == 0)
1679 r8a66597_check_syssts(r8a66597, port, tmp);
1680 else
1681 r8a66597_root_hub_start_polling(r8a66597);
5d304358
YS
1682 } else {
1683 rh->scount = R8A66597_MAX_SAMPLING;
1684 rh->old_syssts = tmp;
29fab0cd 1685 r8a66597_root_hub_start_polling(r8a66597);
5d304358
YS
1686 }
1687 }
1688}
1689
6d879107
YS
1690static void r8a66597_interval_timer(unsigned long _r8a66597)
1691{
1692 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1693 unsigned long flags;
1694 u16 pipenum;
1695 struct r8a66597_td *td;
1696
1697 spin_lock_irqsave(&r8a66597->lock, flags);
1698
1699 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1700 if (!(r8a66597->interval_map & (1 << pipenum)))
1701 continue;
1702 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1703 continue;
1704
1705 td = r8a66597_get_td(r8a66597, pipenum);
1706 if (td)
1707 start_transfer(r8a66597, td);
1708 }
1709
1710 spin_unlock_irqrestore(&r8a66597->lock, flags);
1711}
1712
5d304358
YS
1713static void r8a66597_td_timer(unsigned long _r8a66597)
1714{
1715 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1716 unsigned long flags;
1717 u16 pipenum;
1718 struct r8a66597_td *td, *new_td = NULL;
1719 struct r8a66597_pipe *pipe;
1720
1721 spin_lock_irqsave(&r8a66597->lock, flags);
1722 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1723 if (!(r8a66597->timeout_map & (1 << pipenum)))
1724 continue;
1725 if (timer_pending(&r8a66597->td_timer[pipenum]))
1726 continue;
1727
1728 td = r8a66597_get_td(r8a66597, pipenum);
1729 if (!td) {
1730 r8a66597->timeout_map &= ~(1 << pipenum);
1731 continue;
1732 }
1733
1734 if (td->urb->actual_length) {
1735 set_td_timer(r8a66597, td);
1736 break;
1737 }
1738
1739 pipe = td->pipe;
1740 pipe_stop(r8a66597, pipe);
1741
1742 new_td = td;
1743 do {
1744 list_move_tail(&new_td->queue,
1745 &r8a66597->pipe_queue[pipenum]);
1746 new_td = r8a66597_get_td(r8a66597, pipenum);
1747 if (!new_td) {
1748 new_td = td;
1749 break;
1750 }
1751 } while (td != new_td && td->address == new_td->address);
1752
1753 start_transfer(r8a66597, new_td);
1754
1755 if (td == new_td)
1756 r8a66597->timeout_map &= ~(1 << pipenum);
1757 else
1758 set_td_timer(r8a66597, new_td);
1759 break;
1760 }
1761 spin_unlock_irqrestore(&r8a66597->lock, flags);
1762}
1763
1764static void r8a66597_timer(unsigned long _r8a66597)
1765{
1766 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1767 unsigned long flags;
58639646 1768 int port;
5d304358
YS
1769
1770 spin_lock_irqsave(&r8a66597->lock, flags);
1771
58639646
YS
1772 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
1773 r8a66597_root_hub_control(r8a66597, port);
5d304358
YS
1774
1775 spin_unlock_irqrestore(&r8a66597->lock, flags);
1776}
1777
1778static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1779{
1780 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1781
1782 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1783 (urb->dev->state == USB_STATE_CONFIGURED))
1784 return 1;
1785 else
1786 return 0;
1787}
1788
1789static int r8a66597_start(struct usb_hcd *hcd)
1790{
1791 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
5d304358
YS
1792
1793 hcd->state = HC_STATE_RUNNING;
e294531d 1794 return enable_controller(r8a66597);
5d304358
YS
1795}
1796
1797static void r8a66597_stop(struct usb_hcd *hcd)
1798{
1799 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1800
1801 disable_controller(r8a66597);
1802}
1803
1804static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1805{
1806 unsigned int usb_address = usb_pipedevice(urb->pipe);
1807 u16 root_port, hub_port;
1808
1809 if (usb_address == 0) {
1810 get_port_number(urb->dev->devpath,
1811 &root_port, &hub_port);
1812 set_devadd_reg(r8a66597, 0,
1813 get_r8a66597_usb_speed(urb->dev->speed),
1814 get_parent_r8a66597_address(r8a66597, urb->dev),
1815 hub_port, root_port);
1816 }
1817}
1818
1819static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1820 struct urb *urb,
e294531d 1821 struct usb_host_endpoint *hep)
5d304358
YS
1822{
1823 struct r8a66597_td *td;
1824 u16 pipenum;
1825
e294531d 1826 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
5d304358
YS
1827 if (td == NULL)
1828 return NULL;
1829
1830 pipenum = r8a66597_get_pipenum(urb, hep);
1831 td->pipenum = pipenum;
1832 td->pipe = hep->hcpriv;
1833 td->urb = urb;
1834 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1835 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1836 !usb_pipein(urb->pipe));
1837 if (usb_pipecontrol(urb->pipe))
1838 td->type = USB_PID_SETUP;
1839 else if (usb_pipein(urb->pipe))
1840 td->type = USB_PID_IN;
1841 else
1842 td->type = USB_PID_OUT;
1843 INIT_LIST_HEAD(&td->queue);
1844
1845 return td;
1846}
1847
1848static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
5d304358
YS
1849 struct urb *urb,
1850 gfp_t mem_flags)
1851{
e9df41c5 1852 struct usb_host_endpoint *hep = urb->ep;
5d304358
YS
1853 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1854 struct r8a66597_td *td = NULL;
e9df41c5 1855 int ret, request = 0;
5d304358
YS
1856 unsigned long flags;
1857
1858 spin_lock_irqsave(&r8a66597->lock, flags);
1859 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1860 ret = -ENODEV;
e9df41c5 1861 goto error_not_linked;
5d304358
YS
1862 }
1863
e9df41c5
AS
1864 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1865 if (ret)
1866 goto error_not_linked;
1867
5d304358 1868 if (!hep->hcpriv) {
e294531d
YS
1869 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1870 GFP_ATOMIC);
5d304358
YS
1871 if (!hep->hcpriv) {
1872 ret = -ENOMEM;
1873 goto error;
1874 }
1875 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1876 if (usb_pipeendpoint(urb->pipe))
1877 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1878 }
1879
1880 if (unlikely(check_pipe_config(r8a66597, urb)))
1881 init_pipe_config(r8a66597, urb);
1882
1883 set_address_zero(r8a66597, urb);
e294531d 1884 td = r8a66597_make_td(r8a66597, urb, hep);
5d304358
YS
1885 if (td == NULL) {
1886 ret = -ENOMEM;
1887 goto error;
1888 }
1889 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1890 request = 1;
1891 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
5d304358 1892 urb->hcpriv = td;
5d304358
YS
1893
1894 if (request) {
6d879107
YS
1895 if (td->pipe->info.timer_interval) {
1896 r8a66597->interval_map |= 1 << td->pipenum;
1897 mod_timer(&r8a66597->interval_timer[td->pipenum],
1898 jiffies + msecs_to_jiffies(
1899 td->pipe->info.timer_interval));
1900 } else {
1901 ret = start_transfer(r8a66597, td);
1902 if (ret < 0) {
1903 list_del(&td->queue);
1904 kfree(td);
1905 }
5d304358
YS
1906 }
1907 } else
1908 set_td_timer(r8a66597, td);
1909
1910error:
e9df41c5
AS
1911 if (ret)
1912 usb_hcd_unlink_urb_from_ep(hcd, urb);
1913error_not_linked:
5d304358
YS
1914 spin_unlock_irqrestore(&r8a66597->lock, flags);
1915 return ret;
1916}
1917
e9df41c5
AS
1918static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1919 int status)
5d304358
YS
1920{
1921 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1922 struct r8a66597_td *td;
1923 unsigned long flags;
e9df41c5 1924 int rc;
5d304358
YS
1925
1926 spin_lock_irqsave(&r8a66597->lock, flags);
e9df41c5
AS
1927 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1928 if (rc)
1929 goto done;
1930
5d304358
YS
1931 if (urb->hcpriv) {
1932 td = urb->hcpriv;
1933 pipe_stop(r8a66597, td->pipe);
1934 pipe_irq_disable(r8a66597, td->pipenum);
1935 disable_irq_empty(r8a66597, td->pipenum);
888fda47 1936 finish_request(r8a66597, td, td->pipenum, urb, status);
5d304358 1937 }
e9df41c5 1938 done:
5d304358 1939 spin_unlock_irqrestore(&r8a66597->lock, flags);
e9df41c5 1940 return rc;
5d304358
YS
1941}
1942
1943static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1944 struct usb_host_endpoint *hep)
1945{
1946 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1947 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1948 struct r8a66597_td *td;
1949 struct urb *urb = NULL;
1950 u16 pipenum;
1951 unsigned long flags;
1952
1953 if (pipe == NULL)
1954 return;
1955 pipenum = pipe->info.pipenum;
1956
1957 if (pipenum == 0) {
1958 kfree(hep->hcpriv);
1959 hep->hcpriv = NULL;
1960 return;
1961 }
1962
1963 spin_lock_irqsave(&r8a66597->lock, flags);
1964 pipe_stop(r8a66597, pipe);
1965 pipe_irq_disable(r8a66597, pipenum);
1966 disable_irq_empty(r8a66597, pipenum);
1967 td = r8a66597_get_td(r8a66597, pipenum);
1968 if (td)
1969 urb = td->urb;
888fda47 1970 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
5d304358
YS
1971 kfree(hep->hcpriv);
1972 hep->hcpriv = NULL;
1973 spin_unlock_irqrestore(&r8a66597->lock, flags);
1974}
1975
1976static int r8a66597_get_frame(struct usb_hcd *hcd)
1977{
1978 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1979 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1980}
1981
1982static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
1983{
1984 int chix;
1985
1986 if (udev->state == USB_STATE_CONFIGURED &&
1987 udev->parent && udev->parent->devnum > 1 &&
1988 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
1989 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
1990
1991 for (chix = 0; chix < udev->maxchild; chix++) {
1992 struct usb_device *childdev = udev->children[chix];
1993
1994 if (childdev)
1995 collect_usb_address_map(childdev, map);
1996 }
1997}
1998
1999/* this function must be called with interrupt disabled */
2000static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2001 int addr)
2002{
2003 struct r8a66597_device *dev;
2004 struct list_head *list = &r8a66597->child_device;
2005
2006 list_for_each_entry(dev, list, device_list) {
2007 if (!dev)
2008 continue;
2009 if (dev->usb_address != addr)
2010 continue;
2011
2012 return dev;
2013 }
2014
802f389a 2015 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
5d304358
YS
2016 return NULL;
2017}
2018
2019static void update_usb_address_map(struct r8a66597 *r8a66597,
2020 struct usb_device *root_hub,
2021 unsigned long *map)
2022{
2023 int i, j, addr;
2024 unsigned long diff;
2025 unsigned long flags;
2026
2027 for (i = 0; i < 4; i++) {
2028 diff = r8a66597->child_connect_map[i] ^ map[i];
2029 if (!diff)
2030 continue;
2031
2032 for (j = 0; j < 32; j++) {
2033 if (!(diff & (1 << j)))
2034 continue;
2035
2036 addr = i * 32 + j;
2037 if (map[i] & (1 << j))
2038 set_child_connect_map(r8a66597, addr);
2039 else {
2040 struct r8a66597_device *dev;
2041
2042 spin_lock_irqsave(&r8a66597->lock, flags);
2043 dev = get_r8a66597_device(r8a66597, addr);
2044 disable_r8a66597_pipe_all(r8a66597, dev);
2045 free_usb_address(r8a66597, dev);
2046 put_child_connect_map(r8a66597, addr);
2047 spin_unlock_irqrestore(&r8a66597->lock, flags);
2048 }
2049 }
2050 }
2051}
2052
2053static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2054 struct usb_hcd *hcd)
2055{
2056 struct usb_bus *bus;
2057 unsigned long now_map[4];
2058
2059 memset(now_map, 0, sizeof(now_map));
2060
2061 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2062 if (!bus->root_hub)
2063 continue;
2064
2065 if (bus->busnum != hcd->self.busnum)
2066 continue;
2067
2068 collect_usb_address_map(bus->root_hub, now_map);
2069 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2070 }
2071}
2072
2073static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2074{
2075 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2076 unsigned long flags;
2077 int i;
2078
2079 r8a66597_check_detect_child(r8a66597, hcd);
2080
2081 spin_lock_irqsave(&r8a66597->lock, flags);
2082
2083 *buf = 0; /* initialize (no change) */
2084
2085 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
2086 if (r8a66597->root_hub[i].port & 0xffff0000)
2087 *buf |= 1 << (i + 1);
2088 }
2089
2090 spin_unlock_irqrestore(&r8a66597->lock, flags);
2091
2092 return (*buf != 0);
2093}
2094
2095static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2096 struct usb_hub_descriptor *desc)
2097{
2098 desc->bDescriptorType = 0x29;
2099 desc->bHubContrCurrent = 0;
2100 desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
2101 desc->bDescLength = 9;
2102 desc->bPwrOn2PwrGood = 0;
2103 desc->wHubCharacteristics = cpu_to_le16(0x0011);
2104 desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
2105 desc->bitmap[1] = ~0;
2106}
2107
2108static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2109 u16 wIndex, char *buf, u16 wLength)
2110{
2111 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2112 int ret;
2113 int port = (wIndex & 0x00FF) - 1;
2114 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2115 unsigned long flags;
2116
2117 ret = 0;
2118
2119 spin_lock_irqsave(&r8a66597->lock, flags);
2120 switch (typeReq) {
2121 case ClearHubFeature:
2122 case SetHubFeature:
2123 switch (wValue) {
2124 case C_HUB_OVER_CURRENT:
2125 case C_HUB_LOCAL_POWER:
2126 break;
2127 default:
2128 goto error;
2129 }
2130 break;
2131 case ClearPortFeature:
2132 if (wIndex > R8A66597_MAX_ROOT_HUB)
2133 goto error;
2134 if (wLength != 0)
2135 goto error;
2136
2137 switch (wValue) {
2138 case USB_PORT_FEAT_ENABLE:
e1e609be 2139 rh->port &= ~(1 << USB_PORT_FEAT_POWER);
5d304358
YS
2140 break;
2141 case USB_PORT_FEAT_SUSPEND:
2142 break;
2143 case USB_PORT_FEAT_POWER:
2144 r8a66597_port_power(r8a66597, port, 0);
2145 break;
2146 case USB_PORT_FEAT_C_ENABLE:
2147 case USB_PORT_FEAT_C_SUSPEND:
2148 case USB_PORT_FEAT_C_CONNECTION:
2149 case USB_PORT_FEAT_C_OVER_CURRENT:
2150 case USB_PORT_FEAT_C_RESET:
2151 break;
2152 default:
2153 goto error;
2154 }
2155 rh->port &= ~(1 << wValue);
2156 break;
2157 case GetHubDescriptor:
2158 r8a66597_hub_descriptor(r8a66597,
2159 (struct usb_hub_descriptor *)buf);
2160 break;
2161 case GetHubStatus:
2162 *buf = 0x00;
2163 break;
2164 case GetPortStatus:
2165 if (wIndex > R8A66597_MAX_ROOT_HUB)
2166 goto error;
fd05e720 2167 *(__le32 *)buf = cpu_to_le32(rh->port);
5d304358
YS
2168 break;
2169 case SetPortFeature:
2170 if (wIndex > R8A66597_MAX_ROOT_HUB)
2171 goto error;
2172 if (wLength != 0)
2173 goto error;
2174
2175 switch (wValue) {
2176 case USB_PORT_FEAT_SUSPEND:
2177 break;
2178 case USB_PORT_FEAT_POWER:
2179 r8a66597_port_power(r8a66597, port, 1);
2180 rh->port |= (1 << USB_PORT_FEAT_POWER);
2181 break;
2182 case USB_PORT_FEAT_RESET: {
2183 struct r8a66597_device *dev = rh->dev;
2184
2185 rh->port |= (1 << USB_PORT_FEAT_RESET);
2186
2187 disable_r8a66597_pipe_all(r8a66597, dev);
2188 free_usb_address(r8a66597, dev);
2189
2190 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2191 get_dvstctr_reg(port));
2192 mod_timer(&r8a66597->rh_timer,
2193 jiffies + msecs_to_jiffies(50));
2194 }
2195 break;
2196 default:
2197 goto error;
2198 }
2199 rh->port |= 1 << wValue;
2200 break;
2201 default:
2202error:
2203 ret = -EPIPE;
2204 break;
2205 }
2206
2207 spin_unlock_irqrestore(&r8a66597->lock, flags);
2208 return ret;
2209}
2210
e1e609be
YS
2211#if defined(CONFIG_PM)
2212static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2213{
2214 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2215 int port;
2216
2217 dbg("%s", __func__);
2218
2219 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
2220 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2221 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2222
2223 if (!(rh->port & (1 << USB_PORT_FEAT_ENABLE)))
2224 continue;
2225
2226 dbg("suspend port = %d", port);
2227 r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
2228 rh->port |= 1 << USB_PORT_FEAT_SUSPEND;
2229
2230 if (rh->dev->udev->do_remote_wakeup) {
2231 msleep(3); /* waiting last SOF */
2232 r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2233 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2234 r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2235 }
2236 }
2237
2238 r8a66597->bus_suspended = 1;
2239
2240 return 0;
2241}
2242
2243static int r8a66597_bus_resume(struct usb_hcd *hcd)
2244{
2245 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2246 int port;
2247
2248 dbg("%s", __func__);
2249
2250 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
2251 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2252 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2253
2254 if (!(rh->port & (1 << USB_PORT_FEAT_SUSPEND)))
2255 continue;
2256
2257 dbg("resume port = %d", port);
2258 rh->port &= ~(1 << USB_PORT_FEAT_SUSPEND);
2259 rh->port |= 1 << USB_PORT_FEAT_C_SUSPEND;
2260 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2261 msleep(50);
2262 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2263 }
2264
2265 return 0;
2266
2267}
2268#else
2269#define r8a66597_bus_suspend NULL
2270#define r8a66597_bus_resume NULL
2271#endif
2272
5d304358
YS
2273static struct hc_driver r8a66597_hc_driver = {
2274 .description = hcd_name,
2275 .hcd_priv_size = sizeof(struct r8a66597),
2276 .irq = r8a66597_irq,
2277
2278 /*
2279 * generic hardware linkage
2280 */
2281 .flags = HCD_USB2,
2282
2283 .start = r8a66597_start,
2284 .stop = r8a66597_stop,
2285
2286 /*
2287 * managing i/o requests and associated device resources
2288 */
2289 .urb_enqueue = r8a66597_urb_enqueue,
2290 .urb_dequeue = r8a66597_urb_dequeue,
2291 .endpoint_disable = r8a66597_endpoint_disable,
2292
2293 /*
2294 * periodic schedule support
2295 */
2296 .get_frame_number = r8a66597_get_frame,
2297
2298 /*
2299 * root hub support
2300 */
2301 .hub_status_data = r8a66597_hub_status_data,
2302 .hub_control = r8a66597_hub_control,
e1e609be
YS
2303 .bus_suspend = r8a66597_bus_suspend,
2304 .bus_resume = r8a66597_bus_resume,
5d304358
YS
2305};
2306
2307#if defined(CONFIG_PM)
2308static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
2309{
e1e609be
YS
2310 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2311 int port;
2312
2313 dbg("%s", __func__);
2314
2315 disable_controller(r8a66597);
2316
2317 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
2318 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2319
2320 rh->port = 0x00000000;
2321 }
2322
5d304358
YS
2323 return 0;
2324}
2325
2326static int r8a66597_resume(struct platform_device *pdev)
2327{
e1e609be
YS
2328 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2329 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2330
2331 dbg("%s", __func__);
2332
2333 enable_controller(r8a66597);
2334 usb_root_hub_lost_power(hcd->self.root_hub);
2335
5d304358
YS
2336 return 0;
2337}
2338#else /* if defined(CONFIG_PM) */
2339#define r8a66597_suspend NULL
2340#define r8a66597_resume NULL
2341#endif
2342
2343static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2344{
2345 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2346 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2347
2348 del_timer_sync(&r8a66597->rh_timer);
5d304358 2349 usb_remove_hcd(hcd);
ca0677a2 2350 iounmap((void *)r8a66597->reg);
765786e0
MD
2351#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2352 clk_put(r8a66597->clk);
2353#endif
5d304358
YS
2354 usb_put_hcd(hcd);
2355 return 0;
2356}
2357
8864bd86 2358static int __devinit r8a66597_probe(struct platform_device *pdev)
5d304358 2359{
765786e0
MD
2360#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2361 char clk_name[8];
2362#endif
27140219 2363 struct resource *res = NULL, *ires;
5d304358
YS
2364 int irq = -1;
2365 void __iomem *reg = NULL;
2366 struct usb_hcd *hcd = NULL;
2367 struct r8a66597 *r8a66597;
2368 int ret = 0;
2369 int i;
0bf32b80 2370 unsigned long irq_trigger;
5d304358
YS
2371
2372 if (pdev->dev.dma_mask) {
2373 ret = -EINVAL;
802f389a 2374 dev_err(&pdev->dev, "dma not supported\n");
5d304358
YS
2375 goto clean_up;
2376 }
2377
0a2e5b9b 2378 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5d304358
YS
2379 if (!res) {
2380 ret = -ENODEV;
0a2e5b9b 2381 dev_err(&pdev->dev, "platform_get_resource error.\n");
5d304358
YS
2382 goto clean_up;
2383 }
2384
27140219
MZ
2385 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2386 if (!ires) {
5d304358 2387 ret = -ENODEV;
802f389a
GKH
2388 dev_err(&pdev->dev,
2389 "platform_get_resource IORESOURCE_IRQ error.\n");
5d304358
YS
2390 goto clean_up;
2391 }
2392
27140219
MZ
2393 irq = ires->start;
2394 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2395
0a2e5b9b 2396 reg = ioremap(res->start, resource_size(res));
5d304358
YS
2397 if (reg == NULL) {
2398 ret = -ENOMEM;
802f389a 2399 dev_err(&pdev->dev, "ioremap error.\n");
5d304358
YS
2400 goto clean_up;
2401 }
2402
5effabbe
YS
2403 if (pdev->dev.platform_data == NULL) {
2404 dev_err(&pdev->dev, "no platform data\n");
2405 ret = -ENODEV;
2406 goto clean_up;
2407 }
2408
5d304358
YS
2409 /* initialize hcd */
2410 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2411 if (!hcd) {
2412 ret = -ENOMEM;
802f389a 2413 dev_err(&pdev->dev, "Failed to create hcd\n");
5d304358
YS
2414 goto clean_up;
2415 }
2416 r8a66597 = hcd_to_r8a66597(hcd);
2417 memset(r8a66597, 0, sizeof(struct r8a66597));
2418 dev_set_drvdata(&pdev->dev, r8a66597);
5effabbe
YS
2419 r8a66597->pdata = pdev->dev.platform_data;
2420 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
5d304358 2421
765786e0
MD
2422#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2423 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2424 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2425 if (IS_ERR(r8a66597->clk)) {
2426 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
2427 ret = PTR_ERR(r8a66597->clk);
2428 goto clean_up2;
2429 }
2430#endif
2431
5d304358
YS
2432 spin_lock_init(&r8a66597->lock);
2433 init_timer(&r8a66597->rh_timer);
2434 r8a66597->rh_timer.function = r8a66597_timer;
2435 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2436 r8a66597->reg = (unsigned long)reg;
2437
2438 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2439 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2440 init_timer(&r8a66597->td_timer[i]);
2441 r8a66597->td_timer[i].function = r8a66597_td_timer;
2442 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
6d879107
YS
2443 setup_timer(&r8a66597->interval_timer[i],
2444 r8a66597_interval_timer,
2445 (unsigned long)r8a66597);
5d304358
YS
2446 }
2447 INIT_LIST_HEAD(&r8a66597->child_device);
2448
2449 hcd->rsrc_start = res->start;
27140219 2450
0bf32b80 2451 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
5d304358 2452 if (ret != 0) {
802f389a 2453 dev_err(&pdev->dev, "Failed to add hcd\n");
765786e0 2454 goto clean_up3;
5d304358
YS
2455 }
2456
2457 return 0;
2458
765786e0
MD
2459clean_up3:
2460#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2461 clk_put(r8a66597->clk);
765786e0 2462clean_up2:
d6435102 2463#endif
765786e0
MD
2464 usb_put_hcd(hcd);
2465
5d304358
YS
2466clean_up:
2467 if (reg)
2468 iounmap(reg);
5d304358
YS
2469
2470 return ret;
2471}
2472
2473static struct platform_driver r8a66597_driver = {
2474 .probe = r8a66597_probe,
2475 .remove = r8a66597_remove,
2476 .suspend = r8a66597_suspend,
2477 .resume = r8a66597_resume,
2478 .driver = {
2479 .name = (char *) hcd_name,
f4fce61d 2480 .owner = THIS_MODULE,
5d304358
YS
2481 },
2482};
2483
2484static int __init r8a66597_init(void)
2485{
2486 if (usb_disabled())
2487 return -ENODEV;
2488
5909f6ea
GKH
2489 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2490 DRIVER_VERSION);
5d304358
YS
2491 return platform_driver_register(&r8a66597_driver);
2492}
2493module_init(r8a66597_init);
2494
2495static void __exit r8a66597_cleanup(void)
2496{
2497 platform_driver_unregister(&r8a66597_driver);
2498}
2499module_exit(r8a66597_cleanup);
2500