blob: e33d36256350b8d0b3a4c23577d9f59a92ed1ef3 [file] [log] [blame]
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001/*
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>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090029#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>
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090037#include <linux/io.h>
38#include <linux/irq.h>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090039
40#include "../core/hcd.h"
41#include "r8a66597.h"
42
43MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
44MODULE_LICENSE("GPL");
45MODULE_AUTHOR("Yoshihiro Shimoda");
Kay Sieversf4fce612008-04-10 21:29:22 -070046MODULE_ALIAS("platform:r8a66597_hcd");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090047
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +090048#define DRIVER_VERSION "2009-05-26"
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090049
50static const char hcd_name[] = "r8a66597_hcd";
51
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090052static 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
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +090089static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090090{
91 u16 tmp;
92 int i = 0;
93
Magnus Damm719a72b2009-07-17 14:59:55 +000094 if (r8a66597->pdata->on_chip) {
95#ifdef CONFIG_HAVE_CLK
96 clk_enable(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +090097#endif
Magnus Damm719a72b2009-07-17 14:59:55 +000098 do {
99 r8a66597_write(r8a66597, SCKE, SYSCFG0);
100 tmp = r8a66597_read(r8a66597, SYSCFG0);
101 if (i++ > 1000) {
102 printk(KERN_ERR "r8a66597: reg access fail.\n");
103 return -ENXIO;
104 }
105 } while ((tmp & SCKE) != SCKE);
106 r8a66597_write(r8a66597, 0x04, 0x02);
107 } else {
108 do {
109 r8a66597_write(r8a66597, USBE, SYSCFG0);
110 tmp = r8a66597_read(r8a66597, SYSCFG0);
111 if (i++ > 1000) {
112 printk(KERN_ERR "r8a66597: reg access fail.\n");
113 return -ENXIO;
114 }
115 } while ((tmp & USBE) != USBE);
116 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
117 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
118 XTAL, SYSCFG0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900119
Magnus Damm719a72b2009-07-17 14:59:55 +0000120 i = 0;
121 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
122 do {
123 msleep(1);
124 tmp = r8a66597_read(r8a66597, SYSCFG0);
125 if (i++ > 500) {
126 printk(KERN_ERR "r8a66597: reg access fail.\n");
127 return -ENXIO;
128 }
129 } while ((tmp & SCKE) != SCKE);
130 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900131
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900132 return 0;
133}
134
135static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
136{
137 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
138 udelay(1);
Magnus Damm719a72b2009-07-17 14:59:55 +0000139
140 if (r8a66597->pdata->on_chip) {
141#ifdef CONFIG_HAVE_CLK
142 clk_disable(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +0900143#endif
Magnus Damm719a72b2009-07-17 14:59:55 +0000144 } else {
145 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
146 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
147 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
148 }
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900149}
150
151static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
152{
153 u16 val;
154
155 val = port ? DRPD : DCFM | DRPD;
156 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
157 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
158
159 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
160 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
161 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
162}
163
164static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
165{
166 u16 val, tmp;
167
168 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
169 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
170
171 r8a66597_port_power(r8a66597, port, 0);
172
173 do {
174 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
175 udelay(640);
176 } while (tmp == EDGESTS);
177
178 val = port ? DRPD : DCFM | DRPD;
179 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
180 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
181}
182
183static int enable_controller(struct r8a66597 *r8a66597)
184{
185 int ret, port;
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +0900186 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
187 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
188 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900189
190 ret = r8a66597_clock_enable(r8a66597);
191 if (ret < 0)
192 return ret;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900193
194 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900195 r8a66597_bset(r8a66597, USBE, SYSCFG0);
196
197 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
198 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
199 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
200 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
201
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900202 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
203 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
204 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900205 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
206
207 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900208
Magnus Damm719a72b2009-07-17 14:59:55 +0000209 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900210 r8a66597_enable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900211
212 return 0;
213}
214
215static void disable_controller(struct r8a66597 *r8a66597)
216{
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900217 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900218
219 r8a66597_write(r8a66597, 0, INTENB0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900220 r8a66597_write(r8a66597, 0, INTSTS0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900221
Magnus Damm719a72b2009-07-17 14:59:55 +0000222 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900223 r8a66597_disable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900224
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900225 r8a66597_clock_disable(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900226}
227
228static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
229 struct usb_device *udev)
230{
231 struct r8a66597_device *dev;
232
233 if (udev->parent && udev->parent->devnum != 1)
234 udev = udev->parent;
235
236 dev = dev_get_drvdata(&udev->dev);
237 if (dev)
238 return dev->address;
239 else
240 return 0;
241}
242
243static int is_child_device(char *devpath)
244{
245 return (devpath[2] ? 1 : 0);
246}
247
248static int is_hub_limit(char *devpath)
249{
250 return ((strlen(devpath) >= 4) ? 1 : 0);
251}
252
Magnus Damm719a72b2009-07-17 14:59:55 +0000253static void get_port_number(struct r8a66597 *r8a66597,
254 char *devpath, u16 *root_port, u16 *hub_port)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900255{
256 if (root_port) {
257 *root_port = (devpath[0] & 0x0F) - 1;
Magnus Damm719a72b2009-07-17 14:59:55 +0000258 if (*root_port >= r8a66597->max_root_hub)
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700259 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900260 }
261 if (hub_port)
262 *hub_port = devpath[2] & 0x0F;
263}
264
265static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
266{
267 u16 usbspd = 0;
268
269 switch (speed) {
270 case USB_SPEED_LOW:
271 usbspd = LSMODE;
272 break;
273 case USB_SPEED_FULL:
274 usbspd = FSMODE;
275 break;
276 case USB_SPEED_HIGH:
277 usbspd = HSMODE;
278 break;
279 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700280 printk(KERN_ERR "r8a66597: unknown speed\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900281 break;
282 }
283
284 return usbspd;
285}
286
287static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
288{
289 int idx;
290
291 idx = address / 32;
292 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
293}
294
295static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
296{
297 int idx;
298
299 idx = address / 32;
300 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
301}
302
303static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
304{
305 u16 pipenum = pipe->info.pipenum;
Ming Leife9b9032008-06-08 16:13:03 +0800306 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
307 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
308 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900309
310 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
311 dma_ch = R8A66597_PIPE_NO_DMA;
312
313 pipe->fifoaddr = fifoaddr[dma_ch];
314 pipe->fifosel = fifosel[dma_ch];
315 pipe->fifoctr = fifoctr[dma_ch];
316
317 if (pipenum == 0)
318 pipe->pipectr = DCPCTR;
319 else
320 pipe->pipectr = get_pipectr_addr(pipenum);
321
322 if (check_bulk_or_isoc(pipenum)) {
323 pipe->pipetre = get_pipetre_addr(pipenum);
324 pipe->pipetrn = get_pipetrn_addr(pipenum);
325 } else {
326 pipe->pipetre = 0;
327 pipe->pipetrn = 0;
328 }
329}
330
331static struct r8a66597_device *
332get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
333{
334 if (usb_pipedevice(urb->pipe) == 0)
335 return &r8a66597->device0;
336
337 return dev_get_drvdata(&urb->dev->dev);
338}
339
340static int make_r8a66597_device(struct r8a66597 *r8a66597,
341 struct urb *urb, u8 addr)
342{
343 struct r8a66597_device *dev;
344 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
345
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900346 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900347 if (dev == NULL)
348 return -ENOMEM;
349
350 dev_set_drvdata(&urb->dev->dev, dev);
351 dev->udev = urb->dev;
352 dev->address = addr;
353 dev->usb_address = usb_address;
354 dev->state = USB_STATE_ADDRESS;
355 dev->ep_in_toggle = 0;
356 dev->ep_out_toggle = 0;
357 INIT_LIST_HEAD(&dev->device_list);
358 list_add_tail(&dev->device_list, &r8a66597->child_device);
359
Magnus Damm719a72b2009-07-17 14:59:55 +0000360 get_port_number(r8a66597, urb->dev->devpath,
361 &dev->root_port, &dev->hub_port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900362 if (!is_child_device(urb->dev->devpath))
363 r8a66597->root_hub[dev->root_port].dev = dev;
364
365 set_devadd_reg(r8a66597, dev->address,
366 get_r8a66597_usb_speed(urb->dev->speed),
367 get_parent_r8a66597_address(r8a66597, urb->dev),
368 dev->hub_port, dev->root_port);
369
370 return 0;
371}
372
373/* this function must be called with interrupt disabled */
374static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
375{
376 u8 addr; /* R8A66597's address */
377 struct r8a66597_device *dev;
378
379 if (is_hub_limit(urb->dev->devpath)) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700380 dev_err(&urb->dev->dev, "External hub limit reached.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900381 return 0;
382 }
383
384 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
385 if (dev && dev->state >= USB_STATE_ADDRESS)
386 return dev->address;
387
388 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
389 if (r8a66597->address_map & (1 << addr))
390 continue;
391
392 dbg("alloc_address: r8a66597_addr=%d", addr);
393 r8a66597->address_map |= 1 << addr;
394
395 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
396 return 0;
397
398 return addr;
399 }
400
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700401 dev_err(&urb->dev->dev,
402 "cannot communicate with a USB device more than 10.(%x)\n",
403 r8a66597->address_map);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900404
405 return 0;
406}
407
408/* this function must be called with interrupt disabled */
409static void free_usb_address(struct r8a66597 *r8a66597,
410 struct r8a66597_device *dev)
411{
412 int port;
413
414 if (!dev)
415 return;
416
417 dbg("free_addr: addr=%d", dev->address);
418
419 dev->state = USB_STATE_DEFAULT;
420 r8a66597->address_map &= ~(1 << dev->address);
421 dev->address = 0;
422 dev_set_drvdata(&dev->udev->dev, NULL);
423 list_del(&dev->device_list);
424 kfree(dev);
425
Magnus Damm719a72b2009-07-17 14:59:55 +0000426 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900427 if (r8a66597->root_hub[port].dev == dev) {
428 r8a66597->root_hub[port].dev = NULL;
429 break;
430 }
431 }
432}
433
434static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
435 u16 mask, u16 loop)
436{
437 u16 tmp;
438 int i = 0;
439
440 do {
441 tmp = r8a66597_read(r8a66597, reg);
442 if (i++ > 1000000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700443 printk(KERN_ERR "r8a66597: register%lx, loop %x "
444 "is timeout\n", reg, loop);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900445 break;
446 }
447 ndelay(1);
448 } while ((tmp & mask) != loop);
449}
450
451/* this function must be called with interrupt disabled */
452static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
453{
454 u16 tmp;
455
456 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
457 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
458 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
459 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
460}
461
462/* this function must be called with interrupt disabled */
463static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
464{
465 u16 tmp;
466
467 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
468 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
469 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
470 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
471 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
472}
473
474/* this function must be called with interrupt disabled */
475static void clear_all_buffer(struct r8a66597 *r8a66597,
476 struct r8a66597_pipe *pipe)
477{
478 u16 tmp;
479
480 if (!pipe || pipe->info.pipenum == 0)
481 return;
482
483 pipe_stop(r8a66597, pipe);
484 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
485 tmp = r8a66597_read(r8a66597, pipe->pipectr);
486 tmp = r8a66597_read(r8a66597, pipe->pipectr);
487 tmp = r8a66597_read(r8a66597, pipe->pipectr);
488 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
489}
490
491/* this function must be called with interrupt disabled */
492static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
493 struct r8a66597_pipe *pipe, int toggle)
494{
495 if (toggle)
496 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
497 else
498 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
499}
500
Magnus Damm719a72b2009-07-17 14:59:55 +0000501static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
502{
503 if (r8a66597->pdata->on_chip)
504 return MBW_32;
505 else
506 return MBW_16;
507}
508
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900509/* this function must be called with interrupt disabled */
510static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
511{
Magnus Damm719a72b2009-07-17 14:59:55 +0000512 unsigned short mbw = mbw_value(r8a66597);
513
514 r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900515 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
516}
517
518/* this function must be called with interrupt disabled */
519static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
520 struct r8a66597_pipe *pipe)
521{
Magnus Damm719a72b2009-07-17 14:59:55 +0000522 unsigned short mbw = mbw_value(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900523
Magnus Damm719a72b2009-07-17 14:59:55 +0000524 cfifo_change(r8a66597, 0);
525 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
526 r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
527
528 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900529 pipe->fifosel);
530 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
531}
532
533static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
534{
535 struct r8a66597_pipe *pipe = hep->hcpriv;
536
537 if (usb_pipeendpoint(urb->pipe) == 0)
538 return 0;
539 else
540 return pipe->info.pipenum;
541}
542
543static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
544{
545 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
546
547 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
548}
549
550static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
551 int urb_pipe)
552{
553 if (!dev)
554 return NULL;
555
556 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
557}
558
559/* this function must be called with interrupt disabled */
560static void pipe_toggle_set(struct r8a66597 *r8a66597,
561 struct r8a66597_pipe *pipe,
562 struct urb *urb, int set)
563{
564 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
565 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
566 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
567
568 if (!toggle)
569 return;
570
571 if (set)
572 *toggle |= 1 << endpoint;
573 else
574 *toggle &= ~(1 << endpoint);
575}
576
577/* this function must be called with interrupt disabled */
578static void pipe_toggle_save(struct r8a66597 *r8a66597,
579 struct r8a66597_pipe *pipe,
580 struct urb *urb)
581{
582 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
583 pipe_toggle_set(r8a66597, pipe, urb, 1);
584 else
585 pipe_toggle_set(r8a66597, pipe, urb, 0);
586}
587
588/* this function must be called with interrupt disabled */
589static void pipe_toggle_restore(struct r8a66597 *r8a66597,
590 struct r8a66597_pipe *pipe,
591 struct urb *urb)
592{
593 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
594 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
595 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
596
Yoshihiro Shimodaf6ace2c2007-05-30 20:42:41 +0900597 if (!toggle)
598 return;
599
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900600 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
601}
602
603/* this function must be called with interrupt disabled */
604static void pipe_buffer_setting(struct r8a66597 *r8a66597,
605 struct r8a66597_pipe_info *info)
606{
607 u16 val = 0;
608
609 if (info->pipenum == 0)
610 return;
611
612 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
613 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
614 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
615 if (!info->dir_in)
616 val |= R8A66597_DIR;
617 if (info->type == R8A66597_BULK && info->dir_in)
618 val |= R8A66597_DBLB | R8A66597_SHTNAK;
619 val |= info->type | info->epnum;
620 r8a66597_write(r8a66597, val, PIPECFG);
621
622 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
623 PIPEBUF);
624 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
625 PIPEMAXP);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900626 r8a66597_write(r8a66597, info->interval, PIPEPERI);
627}
628
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900629/* this function must be called with interrupt disabled */
630static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
631{
632 struct r8a66597_pipe_info *info;
633 struct urb *urb = td->urb;
634
635 if (td->pipenum > 0) {
636 info = &td->pipe->info;
637 cfifo_change(r8a66597, 0);
638 pipe_buffer_setting(r8a66597, info);
639
640 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
641 usb_pipeout(urb->pipe)) &&
642 !usb_pipecontrol(urb->pipe)) {
643 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
644 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
645 clear_all_buffer(r8a66597, td->pipe);
646 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
647 usb_pipeout(urb->pipe), 1);
648 }
649 pipe_toggle_restore(r8a66597, td->pipe, urb);
650 }
651}
652
653/* this function must be called with interrupt disabled */
654static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
655 struct usb_endpoint_descriptor *ep)
656{
657 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
658
659 memset(array, 0, sizeof(array));
Julia Lawall2e0fe702008-12-29 11:22:14 +0100660 switch (usb_endpoint_type(ep)) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900661 case USB_ENDPOINT_XFER_BULK:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100662 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900663 array[i++] = 4;
664 else {
665 array[i++] = 3;
666 array[i++] = 5;
667 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900668 break;
669 case USB_ENDPOINT_XFER_INT:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100670 if (usb_endpoint_dir_in(ep)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900671 array[i++] = 6;
672 array[i++] = 7;
673 array[i++] = 8;
674 } else
675 array[i++] = 9;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900676 break;
677 case USB_ENDPOINT_XFER_ISOC:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100678 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900679 array[i++] = 2;
680 else
681 array[i++] = 1;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900682 break;
683 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700684 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900685 return 0;
686 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900687
688 i = 1;
689 min = array[0];
690 while (array[i] != 0) {
691 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
692 min = array[i];
693 i++;
694 }
695
696 return min;
697}
698
699static u16 get_r8a66597_type(__u8 type)
700{
701 u16 r8a66597_type;
702
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900703 switch (type) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900704 case USB_ENDPOINT_XFER_BULK:
705 r8a66597_type = R8A66597_BULK;
706 break;
707 case USB_ENDPOINT_XFER_INT:
708 r8a66597_type = R8A66597_INT;
709 break;
710 case USB_ENDPOINT_XFER_ISOC:
711 r8a66597_type = R8A66597_ISO;
712 break;
713 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700714 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900715 r8a66597_type = 0x0000;
716 break;
717 }
718
719 return r8a66597_type;
720}
721
722static u16 get_bufnum(u16 pipenum)
723{
724 u16 bufnum = 0;
725
726 if (pipenum == 0)
727 bufnum = 0;
728 else if (check_bulk_or_isoc(pipenum))
729 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
730 else if (check_interrupt(pipenum))
731 bufnum = 4 + (pipenum - 6);
732 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700733 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900734
735 return bufnum;
736}
737
738static u16 get_buf_bsize(u16 pipenum)
739{
740 u16 buf_bsize = 0;
741
742 if (pipenum == 0)
743 buf_bsize = 3;
744 else if (check_bulk_or_isoc(pipenum))
745 buf_bsize = R8A66597_BUF_BSIZE - 1;
746 else if (check_interrupt(pipenum))
747 buf_bsize = 0;
748 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700749 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900750
751 return buf_bsize;
752}
753
754/* this function must be called with interrupt disabled */
755static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
756 struct r8a66597_device *dev,
757 struct r8a66597_pipe *pipe,
758 struct urb *urb)
759{
760 int i;
761 struct r8a66597_pipe_info *info = &pipe->info;
Magnus Damm719a72b2009-07-17 14:59:55 +0000762 unsigned short mbw = mbw_value(r8a66597);
763
764 /* pipe dma is only for external controlles */
765 if (r8a66597->pdata->on_chip)
766 return;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900767
768 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
769 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
770 if ((r8a66597->dma_map & (1 << i)) != 0)
771 continue;
772
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -0700773 dev_info(&dev->udev->dev,
774 "address %d, EndpointAddress 0x%02x use "
775 "DMA FIFO\n", usb_pipedevice(urb->pipe),
776 info->dir_in ?
777 USB_ENDPOINT_DIR_MASK + info->epnum
778 : info->epnum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900779
780 r8a66597->dma_map |= 1 << i;
781 dev->dma_map |= 1 << i;
782 set_pipe_reg_addr(pipe, i);
783
784 cfifo_change(r8a66597, 0);
Magnus Damm719a72b2009-07-17 14:59:55 +0000785 r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
786 mbw | CURPIPE, pipe->fifosel);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900787
788 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
789 pipe->info.pipenum);
790 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
791 break;
792 }
793 }
794}
795
796/* this function must be called with interrupt disabled */
797static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
798 struct usb_host_endpoint *hep,
799 struct r8a66597_pipe_info *info)
800{
801 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
802 struct r8a66597_pipe *pipe = hep->hcpriv;
803
804 dbg("enable_pipe:");
805
806 pipe->info = *info;
807 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
808 r8a66597->pipe_cnt[pipe->info.pipenum]++;
809 dev->pipe_cnt[pipe->info.pipenum]++;
810
811 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
812}
813
814/* this function must be called with interrupt disabled */
815static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
816{
817 struct r8a66597_td *td, *next;
818 struct urb *urb;
819 struct list_head *list = &r8a66597->pipe_queue[pipenum];
820
821 if (list_empty(list))
822 return;
823
824 list_for_each_entry_safe(td, next, list, queue) {
825 if (!td)
826 continue;
827 if (td->address != address)
828 continue;
829
830 urb = td->urb;
831 list_del(&td->queue);
832 kfree(td);
833
834 if (urb) {
Alan Sterne9df41c2007-08-08 11:48:02 -0400835 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
836 urb);
837
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900838 spin_unlock(&r8a66597->lock);
Alan Stern4a000272007-08-24 15:42:24 -0400839 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
840 -ENODEV);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900841 spin_lock(&r8a66597->lock);
842 }
843 break;
844 }
845}
846
847/* this function must be called with interrupt disabled */
848static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
849 struct r8a66597_device *dev)
850{
851 int check_ep0 = 0;
852 u16 pipenum;
853
854 if (!dev)
855 return;
856
857 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
858 if (!dev->pipe_cnt[pipenum])
859 continue;
860
861 if (!check_ep0) {
862 check_ep0 = 1;
863 force_dequeue(r8a66597, 0, dev->address);
864 }
865
866 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
867 dev->pipe_cnt[pipenum] = 0;
868 force_dequeue(r8a66597, pipenum, dev->address);
869 }
870
871 dbg("disable_pipe");
872
873 r8a66597->dma_map &= ~(dev->dma_map);
874 dev->dma_map = 0;
875}
876
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900877static u16 get_interval(struct urb *urb, __u8 interval)
878{
879 u16 time = 1;
880 int i;
881
882 if (urb->dev->speed == USB_SPEED_HIGH) {
883 if (interval > IITV)
884 time = IITV;
885 else
886 time = interval ? interval - 1 : 0;
887 } else {
888 if (interval > 128) {
889 time = IITV;
890 } else {
891 /* calculate the nearest value for PIPEPERI */
892 for (i = 0; i < 7; i++) {
893 if ((1 << i) < interval &&
894 (1 << (i + 1) > interval))
895 time = 1 << i;
896 }
897 }
898 }
899
900 return time;
901}
902
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900903static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
904{
905 __u8 i;
906 unsigned long time = 1;
907
908 if (usb_pipeisoc(urb->pipe))
909 return 0;
910
911 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
912 for (i = 0; i < (interval - 1); i++)
913 time *= 2;
914 time = time * 125 / 1000; /* uSOF -> msec */
915 } else {
916 time = interval;
917 }
918
919 return time;
920}
921
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900922/* this function must be called with interrupt disabled */
923static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
924 struct usb_host_endpoint *hep,
925 struct usb_endpoint_descriptor *ep)
926{
927 struct r8a66597_pipe_info info;
928
929 info.pipenum = get_empty_pipenum(r8a66597, ep);
930 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100931 info.epnum = usb_endpoint_num(ep);
Yoshihiro Shimoda05eac912007-10-03 18:53:28 +0900932 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100933 info.type = get_r8a66597_type(usb_endpoint_type(ep));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900934 info.bufnum = get_bufnum(info.pipenum);
935 info.buf_bsize = get_buf_bsize(info.pipenum);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900936 if (info.type == R8A66597_BULK) {
937 info.interval = 0;
938 info.timer_interval = 0;
939 } else {
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900940 info.interval = get_interval(urb, ep->bInterval);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900941 info.timer_interval = get_timer_interval(urb, ep->bInterval);
942 }
Julia Lawall2e0fe702008-12-29 11:22:14 +0100943 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900944 info.dir_in = 1;
945 else
946 info.dir_in = 0;
947
948 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
949}
950
951static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
952{
953 struct r8a66597_device *dev;
954
955 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
956 dev->state = USB_STATE_CONFIGURED;
957}
958
959static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
960 u16 pipenum)
961{
962 if (pipenum == 0 && usb_pipeout(urb->pipe))
963 enable_irq_empty(r8a66597, pipenum);
964 else
965 enable_irq_ready(r8a66597, pipenum);
966
967 if (!usb_pipeisoc(urb->pipe))
968 enable_irq_nrdy(r8a66597, pipenum);
969}
970
971static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
972{
973 disable_irq_ready(r8a66597, pipenum);
974 disable_irq_nrdy(r8a66597, pipenum);
975}
976
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +0900977static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
978{
979 mod_timer(&r8a66597->rh_timer,
980 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
981}
982
983static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
984 int connect)
985{
986 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
987
988 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
989 rh->scount = R8A66597_MAX_SAMPLING;
990 if (connect)
991 rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
992 else
993 rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
994 rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
995
996 r8a66597_root_hub_start_polling(r8a66597);
997}
998
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900999/* this function must be called with interrupt disabled */
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001000static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
1001 u16 syssts)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001002{
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001003 if (syssts == SE0) {
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001004 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001005 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001006 } else {
1007 if (syssts == FS_JSTS)
1008 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1009 else if (syssts == LS_JSTS)
1010 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1011
1012 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
1013 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1014
1015 if (r8a66597->bus_suspended)
1016 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001017 }
1018
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001019 usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001020}
1021
1022/* this function must be called with interrupt disabled */
1023static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1024{
1025 u16 speed = get_rh_usb_speed(r8a66597, port);
1026 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1027
Yoshihiro Shimoda1e6159f2009-10-21 20:33:39 +09001028 rh->port &= ~((1 << USB_PORT_FEAT_HIGHSPEED) |
1029 (1 << USB_PORT_FEAT_LOWSPEED));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001030 if (speed == HSMODE)
1031 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
1032 else if (speed == LSMODE)
1033 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
1034
1035 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
1036 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1037}
1038
1039/* this function must be called with interrupt disabled */
1040static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1041{
1042 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1043
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001044 disable_r8a66597_pipe_all(r8a66597, dev);
1045 free_usb_address(r8a66597, dev);
1046
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001047 start_root_hub_sampling(r8a66597, port, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001048}
1049
1050/* this function must be called with interrupt disabled */
1051static void prepare_setup_packet(struct r8a66597 *r8a66597,
1052 struct r8a66597_td *td)
1053{
1054 int i;
Al Virofd05e722008-04-28 07:00:16 +01001055 __le16 *p = (__le16 *)td->urb->setup_packet;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001056 unsigned long setup_addr = USBREQ;
1057
1058 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1059 DCPMAXP);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001060 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001061
1062 for (i = 0; i < 4; i++) {
Al Virofd05e722008-04-28 07:00:16 +01001063 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001064 setup_addr += 2;
1065 }
1066 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1067}
1068
1069/* this function must be called with interrupt disabled */
1070static void prepare_packet_read(struct r8a66597 *r8a66597,
1071 struct r8a66597_td *td)
1072{
1073 struct urb *urb = td->urb;
1074
1075 if (usb_pipecontrol(urb->pipe)) {
1076 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1077 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1078 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1079 if (urb->actual_length == 0) {
1080 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1081 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1082 }
1083 pipe_irq_disable(r8a66597, td->pipenum);
1084 pipe_start(r8a66597, td->pipe);
1085 pipe_irq_enable(r8a66597, urb, td->pipenum);
1086 } else {
1087 if (urb->actual_length == 0) {
1088 pipe_irq_disable(r8a66597, td->pipenum);
1089 pipe_setting(r8a66597, td);
1090 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001091 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001092
1093 if (td->pipe->pipetre) {
1094 r8a66597_write(r8a66597, TRCLR,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001095 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001096 r8a66597_write(r8a66597,
Julia Lawalldfa5ec72008-03-04 15:25:11 -08001097 DIV_ROUND_UP
1098 (urb->transfer_buffer_length,
1099 td->maxpacket),
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001100 td->pipe->pipetrn);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001101 r8a66597_bset(r8a66597, TRENB,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001102 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001103 }
1104
1105 pipe_start(r8a66597, td->pipe);
1106 pipe_irq_enable(r8a66597, urb, td->pipenum);
1107 }
1108 }
1109}
1110
1111/* this function must be called with interrupt disabled */
1112static void prepare_packet_write(struct r8a66597 *r8a66597,
1113 struct r8a66597_td *td)
1114{
1115 u16 tmp;
1116 struct urb *urb = td->urb;
1117
1118 if (usb_pipecontrol(urb->pipe)) {
1119 pipe_stop(r8a66597, td->pipe);
1120 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1121 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1122 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1123 if (urb->actual_length == 0) {
1124 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1125 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1126 }
1127 } else {
1128 if (urb->actual_length == 0)
1129 pipe_setting(r8a66597, td);
1130 if (td->pipe->pipetre)
1131 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1132 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001133 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001134
1135 fifo_change_from_pipe(r8a66597, td->pipe);
1136 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1137 if (unlikely((tmp & FRDY) == 0))
1138 pipe_irq_enable(r8a66597, urb, td->pipenum);
1139 else
1140 packet_write(r8a66597, td->pipenum);
1141 pipe_start(r8a66597, td->pipe);
1142}
1143
1144/* this function must be called with interrupt disabled */
1145static void prepare_status_packet(struct r8a66597 *r8a66597,
1146 struct r8a66597_td *td)
1147{
1148 struct urb *urb = td->urb;
1149
1150 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001151 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001152
1153 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1154 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1155 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1156 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001157 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +09001158 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001159 enable_irq_empty(r8a66597, 0);
1160 } else {
1161 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1162 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1163 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1164 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001165 enable_irq_ready(r8a66597, 0);
1166 }
1167 enable_irq_nrdy(r8a66597, 0);
1168 pipe_start(r8a66597, td->pipe);
1169}
1170
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001171static int is_set_address(unsigned char *setup_packet)
1172{
1173 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1174 setup_packet[1] == USB_REQ_SET_ADDRESS)
1175 return 1;
1176 else
1177 return 0;
1178}
1179
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001180/* this function must be called with interrupt disabled */
1181static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1182{
1183 BUG_ON(!td);
1184
1185 switch (td->type) {
1186 case USB_PID_SETUP:
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001187 if (is_set_address(td->urb->setup_packet)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001188 td->set_address = 1;
1189 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1190 td->urb);
1191 if (td->urb->setup_packet[2] == 0)
1192 return -EPIPE;
1193 }
1194 prepare_setup_packet(r8a66597, td);
1195 break;
1196 case USB_PID_IN:
1197 prepare_packet_read(r8a66597, td);
1198 break;
1199 case USB_PID_OUT:
1200 prepare_packet_write(r8a66597, td);
1201 break;
1202 case USB_PID_ACK:
1203 prepare_status_packet(r8a66597, td);
1204 break;
1205 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001206 printk(KERN_ERR "r8a66597: invalid type.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001207 break;
1208 }
1209
1210 return 0;
1211}
1212
1213static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1214{
1215 if (usb_pipeisoc(urb->pipe)) {
1216 if (urb->number_of_packets == td->iso_cnt)
1217 return 1;
1218 }
1219
1220 /* control or bulk or interrupt */
1221 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1222 (td->short_packet) || (td->zero_packet))
1223 return 1;
1224
1225 return 0;
1226}
1227
1228/* this function must be called with interrupt disabled */
1229static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1230{
1231 unsigned long time;
1232
1233 BUG_ON(!td);
1234
1235 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1236 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1237 r8a66597->timeout_map |= 1 << td->pipenum;
1238 switch (usb_pipetype(td->urb->pipe)) {
1239 case PIPE_INTERRUPT:
1240 case PIPE_ISOCHRONOUS:
1241 time = 30;
1242 break;
1243 default:
1244 time = 300;
1245 break;
1246 }
1247
1248 mod_timer(&r8a66597->td_timer[td->pipenum],
1249 jiffies + msecs_to_jiffies(time));
1250 }
1251}
1252
1253/* this function must be called with interrupt disabled */
Alan Sterndfd1e532007-08-21 15:36:52 -04001254static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
Alan Stern888fda42007-08-24 15:41:18 -04001255 u16 pipenum, struct urb *urb, int status)
Alan Sterndfd1e532007-08-21 15:36:52 -04001256__releases(r8a66597->lock) __acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001257{
1258 int restart = 0;
1259 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1260
1261 r8a66597->timeout_map &= ~(1 << pipenum);
1262
1263 if (likely(td)) {
Alan Stern888fda42007-08-24 15:41:18 -04001264 if (td->set_address && (status != 0 || urb->unlinked))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001265 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1266
1267 pipe_toggle_save(r8a66597, td->pipe, urb);
1268 list_del(&td->queue);
1269 kfree(td);
1270 }
1271
1272 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1273 restart = 1;
1274
1275 if (likely(urb)) {
1276 if (usb_pipeisoc(urb->pipe))
1277 urb->start_frame = r8a66597_get_frame(hcd);
1278
Alan Sterne9df41c2007-08-08 11:48:02 -04001279 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001280 spin_unlock(&r8a66597->lock);
Alan Stern4a000272007-08-24 15:42:24 -04001281 usb_hcd_giveback_urb(hcd, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001282 spin_lock(&r8a66597->lock);
1283 }
1284
1285 if (restart) {
1286 td = r8a66597_get_td(r8a66597, pipenum);
1287 if (unlikely(!td))
1288 return;
1289
1290 start_transfer(r8a66597, td);
1291 set_td_timer(r8a66597, td);
1292 }
1293}
1294
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001295static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1296{
1297 u16 tmp;
1298 int rcv_len, bufsize, urb_len, size;
1299 u16 *buf;
1300 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1301 struct urb *urb;
1302 int finish = 0;
Alan Sterndfd1e532007-08-21 15:36:52 -04001303 int status = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001304
1305 if (unlikely(!td))
1306 return;
1307 urb = td->urb;
1308
1309 fifo_change_from_pipe(r8a66597, td->pipe);
1310 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1311 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001312 pipe_stop(r8a66597, td->pipe);
1313 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001314 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001315 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001316 return;
1317 }
1318
1319 /* prepare parameters */
1320 rcv_len = tmp & DTLN;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001321 if (usb_pipeisoc(urb->pipe)) {
1322 buf = (u16 *)(urb->transfer_buffer +
1323 urb->iso_frame_desc[td->iso_cnt].offset);
1324 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1325 } else {
1326 buf = (void *)urb->transfer_buffer + urb->actual_length;
1327 urb_len = urb->transfer_buffer_length - urb->actual_length;
1328 }
Alan Sterndfd1e532007-08-21 15:36:52 -04001329 bufsize = min(urb_len, (int) td->maxpacket);
1330 if (rcv_len <= bufsize) {
1331 size = rcv_len;
1332 } else {
1333 size = bufsize;
1334 status = -EOVERFLOW;
1335 finish = 1;
1336 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001337
1338 /* update parameters */
1339 urb->actual_length += size;
1340 if (rcv_len == 0)
1341 td->zero_packet = 1;
Alan Sterndfd1e532007-08-21 15:36:52 -04001342 if (rcv_len < bufsize) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001343 td->short_packet = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001344 }
1345 if (usb_pipeisoc(urb->pipe)) {
1346 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
Alan Sterndfd1e532007-08-21 15:36:52 -04001347 urb->iso_frame_desc[td->iso_cnt].status = status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001348 td->iso_cnt++;
Alan Sterndfd1e532007-08-21 15:36:52 -04001349 finish = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001350 }
1351
1352 /* check transfer finish */
Alan Sternb0d9efb2007-08-21 15:39:21 -04001353 if (finish || check_transfer_finish(td, urb)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001354 pipe_stop(r8a66597, td->pipe);
1355 pipe_irq_disable(r8a66597, pipenum);
1356 finish = 1;
1357 }
1358
1359 /* read fifo */
1360 if (urb->transfer_buffer) {
1361 if (size == 0)
1362 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1363 else
1364 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1365 buf, size);
1366 }
1367
Alan Stern888fda42007-08-24 15:41:18 -04001368 if (finish && pipenum != 0)
1369 finish_request(r8a66597, td, pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001370}
1371
1372static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1373{
1374 u16 tmp;
1375 int bufsize, size;
1376 u16 *buf;
1377 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1378 struct urb *urb;
1379
1380 if (unlikely(!td))
1381 return;
1382 urb = td->urb;
1383
1384 fifo_change_from_pipe(r8a66597, td->pipe);
1385 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1386 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001387 pipe_stop(r8a66597, td->pipe);
1388 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001389 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001390 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001391 return;
1392 }
1393
1394 /* prepare parameters */
1395 bufsize = td->maxpacket;
1396 if (usb_pipeisoc(urb->pipe)) {
1397 buf = (u16 *)(urb->transfer_buffer +
1398 urb->iso_frame_desc[td->iso_cnt].offset);
1399 size = min(bufsize,
1400 (int)urb->iso_frame_desc[td->iso_cnt].length);
1401 } else {
1402 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
Greg Kroah-Hartman16e2e5f2009-03-03 16:44:13 -08001403 size = min_t(u32, bufsize,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001404 urb->transfer_buffer_length - urb->actual_length);
1405 }
1406
1407 /* write fifo */
1408 if (pipenum > 0)
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001409 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001410 if (urb->transfer_buffer) {
1411 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1412 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1413 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1414 }
1415
1416 /* update parameters */
1417 urb->actual_length += size;
1418 if (usb_pipeisoc(urb->pipe)) {
1419 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1420 urb->iso_frame_desc[td->iso_cnt].status = 0;
1421 td->iso_cnt++;
1422 }
1423
1424 /* check transfer finish */
1425 if (check_transfer_finish(td, urb)) {
1426 disable_irq_ready(r8a66597, pipenum);
1427 enable_irq_empty(r8a66597, pipenum);
1428 if (!usb_pipeisoc(urb->pipe))
1429 enable_irq_nrdy(r8a66597, pipenum);
1430 } else
1431 pipe_irq_enable(r8a66597, urb, pipenum);
1432}
1433
1434
Alan Stern888fda42007-08-24 15:41:18 -04001435static void check_next_phase(struct r8a66597 *r8a66597, int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001436{
1437 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1438 struct urb *urb;
1439 u8 finish = 0;
1440
1441 if (unlikely(!td))
1442 return;
1443 urb = td->urb;
1444
1445 switch (td->type) {
1446 case USB_PID_IN:
1447 case USB_PID_OUT:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001448 if (check_transfer_finish(td, urb))
1449 td->type = USB_PID_ACK;
1450 break;
1451 case USB_PID_SETUP:
Alan Sterneb231052007-08-21 15:40:36 -04001452 if (urb->transfer_buffer_length == urb->actual_length)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001453 td->type = USB_PID_ACK;
Alan Sterneb231052007-08-21 15:40:36 -04001454 else if (usb_pipeout(urb->pipe))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001455 td->type = USB_PID_OUT;
1456 else
1457 td->type = USB_PID_IN;
1458 break;
1459 case USB_PID_ACK:
1460 finish = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001461 break;
1462 }
1463
Alan Stern888fda42007-08-24 15:41:18 -04001464 if (finish || status != 0 || urb->unlinked)
1465 finish_request(r8a66597, td, 0, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001466 else
1467 start_transfer(r8a66597, td);
1468}
1469
Alan Stern888fda42007-08-24 15:41:18 -04001470static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001471{
1472 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1473
Alan Stern888fda42007-08-24 15:41:18 -04001474 if (td) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001475 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1476
1477 if (pid == PID_NAK)
Alan Stern888fda42007-08-24 15:41:18 -04001478 return -ECONNRESET;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001479 else
Alan Stern888fda42007-08-24 15:41:18 -04001480 return -EPIPE;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001481 }
Alan Stern888fda42007-08-24 15:41:18 -04001482 return 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001483}
1484
1485static void irq_pipe_ready(struct r8a66597 *r8a66597)
1486{
1487 u16 check;
1488 u16 pipenum;
1489 u16 mask;
1490 struct r8a66597_td *td;
1491
1492 mask = r8a66597_read(r8a66597, BRDYSTS)
1493 & r8a66597_read(r8a66597, BRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001494 r8a66597_write(r8a66597, ~mask, BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001495 if (mask & BRDY0) {
1496 td = r8a66597_get_td(r8a66597, 0);
1497 if (td && td->type == USB_PID_IN)
1498 packet_read(r8a66597, 0);
1499 else
1500 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001501 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001502 }
1503
1504 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1505 check = 1 << pipenum;
1506 if (mask & check) {
1507 td = r8a66597_get_td(r8a66597, pipenum);
1508 if (unlikely(!td))
1509 continue;
1510
1511 if (td->type == USB_PID_IN)
1512 packet_read(r8a66597, pipenum);
1513 else if (td->type == USB_PID_OUT)
1514 packet_write(r8a66597, pipenum);
1515 }
1516 }
1517}
1518
1519static void irq_pipe_empty(struct r8a66597 *r8a66597)
1520{
1521 u16 tmp;
1522 u16 check;
1523 u16 pipenum;
1524 u16 mask;
1525 struct r8a66597_td *td;
1526
1527 mask = r8a66597_read(r8a66597, BEMPSTS)
1528 & r8a66597_read(r8a66597, BEMPENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001529 r8a66597_write(r8a66597, ~mask, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001530 if (mask & BEMP0) {
1531 cfifo_change(r8a66597, 0);
1532 td = r8a66597_get_td(r8a66597, 0);
1533 if (td && td->type != USB_PID_OUT)
1534 disable_irq_empty(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001535 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001536 }
1537
1538 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1539 check = 1 << pipenum;
1540 if (mask & check) {
1541 struct r8a66597_td *td;
1542 td = r8a66597_get_td(r8a66597, pipenum);
1543 if (unlikely(!td))
1544 continue;
1545
1546 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1547 if ((tmp & INBUFM) == 0) {
1548 disable_irq_empty(r8a66597, pipenum);
1549 pipe_irq_disable(r8a66597, pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001550 finish_request(r8a66597, td, pipenum, td->urb,
1551 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001552 }
1553 }
1554 }
1555}
1556
1557static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1558{
1559 u16 check;
1560 u16 pipenum;
1561 u16 mask;
Alan Stern888fda42007-08-24 15:41:18 -04001562 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001563
1564 mask = r8a66597_read(r8a66597, NRDYSTS)
1565 & r8a66597_read(r8a66597, NRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001566 r8a66597_write(r8a66597, ~mask, NRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001567 if (mask & NRDY0) {
1568 cfifo_change(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001569 status = get_urb_error(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001570 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001571 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001572 }
1573
1574 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1575 check = 1 << pipenum;
1576 if (mask & check) {
1577 struct r8a66597_td *td;
1578 td = r8a66597_get_td(r8a66597, pipenum);
1579 if (unlikely(!td))
1580 continue;
1581
Alan Stern888fda42007-08-24 15:41:18 -04001582 status = get_urb_error(r8a66597, pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001583 pipe_irq_disable(r8a66597, pipenum);
1584 pipe_stop(r8a66597, td->pipe);
Alan Stern888fda42007-08-24 15:41:18 -04001585 finish_request(r8a66597, td, pipenum, td->urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001586 }
1587 }
1588}
1589
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001590static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1591{
1592 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1593 u16 intsts0, intsts1, intsts2;
1594 u16 intenb0, intenb1, intenb2;
1595 u16 mask0, mask1, mask2;
Alan Stern888fda42007-08-24 15:41:18 -04001596 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001597
1598 spin_lock(&r8a66597->lock);
1599
1600 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1601 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1602 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1603 intenb0 = r8a66597_read(r8a66597, INTENB0);
1604 intenb1 = r8a66597_read(r8a66597, INTENB1);
1605 intenb2 = r8a66597_read(r8a66597, INTENB2);
1606
1607 mask2 = intsts2 & intenb2;
1608 mask1 = intsts1 & intenb1;
1609 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1610 if (mask2) {
1611 if (mask2 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001612 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001613 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1614
1615 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001616 start_root_hub_sampling(r8a66597, 1, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001617 }
1618 if (mask2 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001619 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001620 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1621 r8a66597_usb_disconnect(r8a66597, 1);
1622 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001623 if (mask2 & BCHG) {
1624 r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1625 r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1626 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1627 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001628 }
1629
1630 if (mask1) {
1631 if (mask1 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001632 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001633 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1634
1635 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001636 start_root_hub_sampling(r8a66597, 0, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001637 }
1638 if (mask1 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001639 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001640 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1641 r8a66597_usb_disconnect(r8a66597, 0);
1642 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001643 if (mask1 & BCHG) {
1644 r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1645 r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1646 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1647 }
1648
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001649 if (mask1 & SIGN) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001650 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001651 status = get_urb_error(r8a66597, 0);
1652 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001653 }
1654 if (mask1 & SACK) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001655 r8a66597_write(r8a66597, ~SACK, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001656 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001657 }
1658 }
1659 if (mask0) {
1660 if (mask0 & BRDY)
1661 irq_pipe_ready(r8a66597);
1662 if (mask0 & BEMP)
1663 irq_pipe_empty(r8a66597);
1664 if (mask0 & NRDY)
1665 irq_pipe_nrdy(r8a66597);
1666 }
1667
1668 spin_unlock(&r8a66597->lock);
1669 return IRQ_HANDLED;
1670}
1671
1672/* this function must be called with interrupt disabled */
1673static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1674{
1675 u16 tmp;
1676 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1677
1678 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1679 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1680
1681 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1682 if ((tmp & USBRST) == USBRST) {
1683 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1684 dvstctr_reg);
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001685 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001686 } else
1687 r8a66597_usb_connect(r8a66597, port);
1688 }
1689
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001690 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1691 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1692 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1693 }
1694
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001695 if (rh->scount > 0) {
1696 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1697 if (tmp == rh->old_syssts) {
1698 rh->scount--;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001699 if (rh->scount == 0)
1700 r8a66597_check_syssts(r8a66597, port, tmp);
1701 else
1702 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001703 } else {
1704 rh->scount = R8A66597_MAX_SAMPLING;
1705 rh->old_syssts = tmp;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001706 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001707 }
1708 }
1709}
1710
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001711static void r8a66597_interval_timer(unsigned long _r8a66597)
1712{
1713 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1714 unsigned long flags;
1715 u16 pipenum;
1716 struct r8a66597_td *td;
1717
1718 spin_lock_irqsave(&r8a66597->lock, flags);
1719
1720 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1721 if (!(r8a66597->interval_map & (1 << pipenum)))
1722 continue;
1723 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1724 continue;
1725
1726 td = r8a66597_get_td(r8a66597, pipenum);
1727 if (td)
1728 start_transfer(r8a66597, td);
1729 }
1730
1731 spin_unlock_irqrestore(&r8a66597->lock, flags);
1732}
1733
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001734static void r8a66597_td_timer(unsigned long _r8a66597)
1735{
1736 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1737 unsigned long flags;
1738 u16 pipenum;
1739 struct r8a66597_td *td, *new_td = NULL;
1740 struct r8a66597_pipe *pipe;
1741
1742 spin_lock_irqsave(&r8a66597->lock, flags);
1743 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1744 if (!(r8a66597->timeout_map & (1 << pipenum)))
1745 continue;
1746 if (timer_pending(&r8a66597->td_timer[pipenum]))
1747 continue;
1748
1749 td = r8a66597_get_td(r8a66597, pipenum);
1750 if (!td) {
1751 r8a66597->timeout_map &= ~(1 << pipenum);
1752 continue;
1753 }
1754
1755 if (td->urb->actual_length) {
1756 set_td_timer(r8a66597, td);
1757 break;
1758 }
1759
1760 pipe = td->pipe;
1761 pipe_stop(r8a66597, pipe);
1762
1763 new_td = td;
1764 do {
1765 list_move_tail(&new_td->queue,
1766 &r8a66597->pipe_queue[pipenum]);
1767 new_td = r8a66597_get_td(r8a66597, pipenum);
1768 if (!new_td) {
1769 new_td = td;
1770 break;
1771 }
1772 } while (td != new_td && td->address == new_td->address);
1773
1774 start_transfer(r8a66597, new_td);
1775
1776 if (td == new_td)
1777 r8a66597->timeout_map &= ~(1 << pipenum);
1778 else
1779 set_td_timer(r8a66597, new_td);
1780 break;
1781 }
1782 spin_unlock_irqrestore(&r8a66597->lock, flags);
1783}
1784
1785static void r8a66597_timer(unsigned long _r8a66597)
1786{
1787 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1788 unsigned long flags;
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001789 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001790
1791 spin_lock_irqsave(&r8a66597->lock, flags);
1792
Magnus Damm719a72b2009-07-17 14:59:55 +00001793 for (port = 0; port < r8a66597->max_root_hub; port++)
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001794 r8a66597_root_hub_control(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001795
1796 spin_unlock_irqrestore(&r8a66597->lock, flags);
1797}
1798
1799static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1800{
1801 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1802
1803 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1804 (urb->dev->state == USB_STATE_CONFIGURED))
1805 return 1;
1806 else
1807 return 0;
1808}
1809
1810static int r8a66597_start(struct usb_hcd *hcd)
1811{
1812 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001813
1814 hcd->state = HC_STATE_RUNNING;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001815 return enable_controller(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001816}
1817
1818static void r8a66597_stop(struct usb_hcd *hcd)
1819{
1820 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1821
1822 disable_controller(r8a66597);
1823}
1824
1825static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1826{
1827 unsigned int usb_address = usb_pipedevice(urb->pipe);
1828 u16 root_port, hub_port;
1829
1830 if (usb_address == 0) {
Magnus Damm719a72b2009-07-17 14:59:55 +00001831 get_port_number(r8a66597, urb->dev->devpath,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001832 &root_port, &hub_port);
1833 set_devadd_reg(r8a66597, 0,
1834 get_r8a66597_usb_speed(urb->dev->speed),
1835 get_parent_r8a66597_address(r8a66597, urb->dev),
1836 hub_port, root_port);
1837 }
1838}
1839
1840static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1841 struct urb *urb,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001842 struct usb_host_endpoint *hep)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001843{
1844 struct r8a66597_td *td;
1845 u16 pipenum;
1846
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001847 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001848 if (td == NULL)
1849 return NULL;
1850
1851 pipenum = r8a66597_get_pipenum(urb, hep);
1852 td->pipenum = pipenum;
1853 td->pipe = hep->hcpriv;
1854 td->urb = urb;
1855 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1856 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1857 !usb_pipein(urb->pipe));
1858 if (usb_pipecontrol(urb->pipe))
1859 td->type = USB_PID_SETUP;
1860 else if (usb_pipein(urb->pipe))
1861 td->type = USB_PID_IN;
1862 else
1863 td->type = USB_PID_OUT;
1864 INIT_LIST_HEAD(&td->queue);
1865
1866 return td;
1867}
1868
1869static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001870 struct urb *urb,
1871 gfp_t mem_flags)
1872{
Alan Sterne9df41c2007-08-08 11:48:02 -04001873 struct usb_host_endpoint *hep = urb->ep;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001874 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1875 struct r8a66597_td *td = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -04001876 int ret, request = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001877 unsigned long flags;
1878
1879 spin_lock_irqsave(&r8a66597->lock, flags);
1880 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1881 ret = -ENODEV;
Alan Sterne9df41c2007-08-08 11:48:02 -04001882 goto error_not_linked;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001883 }
1884
Alan Sterne9df41c2007-08-08 11:48:02 -04001885 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1886 if (ret)
1887 goto error_not_linked;
1888
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001889 if (!hep->hcpriv) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001890 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1891 GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001892 if (!hep->hcpriv) {
1893 ret = -ENOMEM;
1894 goto error;
1895 }
1896 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1897 if (usb_pipeendpoint(urb->pipe))
1898 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1899 }
1900
1901 if (unlikely(check_pipe_config(r8a66597, urb)))
1902 init_pipe_config(r8a66597, urb);
1903
1904 set_address_zero(r8a66597, urb);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001905 td = r8a66597_make_td(r8a66597, urb, hep);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001906 if (td == NULL) {
1907 ret = -ENOMEM;
1908 goto error;
1909 }
1910 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1911 request = 1;
1912 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001913 urb->hcpriv = td;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001914
1915 if (request) {
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001916 if (td->pipe->info.timer_interval) {
1917 r8a66597->interval_map |= 1 << td->pipenum;
1918 mod_timer(&r8a66597->interval_timer[td->pipenum],
1919 jiffies + msecs_to_jiffies(
1920 td->pipe->info.timer_interval));
1921 } else {
1922 ret = start_transfer(r8a66597, td);
1923 if (ret < 0) {
1924 list_del(&td->queue);
1925 kfree(td);
1926 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001927 }
1928 } else
1929 set_td_timer(r8a66597, td);
1930
1931error:
Alan Sterne9df41c2007-08-08 11:48:02 -04001932 if (ret)
1933 usb_hcd_unlink_urb_from_ep(hcd, urb);
1934error_not_linked:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001935 spin_unlock_irqrestore(&r8a66597->lock, flags);
1936 return ret;
1937}
1938
Alan Sterne9df41c2007-08-08 11:48:02 -04001939static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1940 int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001941{
1942 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1943 struct r8a66597_td *td;
1944 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001945 int rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001946
1947 spin_lock_irqsave(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001948 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1949 if (rc)
1950 goto done;
1951
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001952 if (urb->hcpriv) {
1953 td = urb->hcpriv;
1954 pipe_stop(r8a66597, td->pipe);
1955 pipe_irq_disable(r8a66597, td->pipenum);
1956 disable_irq_empty(r8a66597, td->pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001957 finish_request(r8a66597, td, td->pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001958 }
Alan Sterne9df41c2007-08-08 11:48:02 -04001959 done:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001960 spin_unlock_irqrestore(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001961 return rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001962}
1963
1964static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1965 struct usb_host_endpoint *hep)
1966{
1967 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1968 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1969 struct r8a66597_td *td;
1970 struct urb *urb = NULL;
1971 u16 pipenum;
1972 unsigned long flags;
1973
1974 if (pipe == NULL)
1975 return;
1976 pipenum = pipe->info.pipenum;
1977
1978 if (pipenum == 0) {
1979 kfree(hep->hcpriv);
1980 hep->hcpriv = NULL;
1981 return;
1982 }
1983
1984 spin_lock_irqsave(&r8a66597->lock, flags);
1985 pipe_stop(r8a66597, pipe);
1986 pipe_irq_disable(r8a66597, pipenum);
1987 disable_irq_empty(r8a66597, pipenum);
1988 td = r8a66597_get_td(r8a66597, pipenum);
1989 if (td)
1990 urb = td->urb;
Alan Stern888fda42007-08-24 15:41:18 -04001991 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001992 kfree(hep->hcpriv);
1993 hep->hcpriv = NULL;
1994 spin_unlock_irqrestore(&r8a66597->lock, flags);
1995}
1996
1997static int r8a66597_get_frame(struct usb_hcd *hcd)
1998{
1999 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2000 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
2001}
2002
2003static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
2004{
2005 int chix;
2006
2007 if (udev->state == USB_STATE_CONFIGURED &&
2008 udev->parent && udev->parent->devnum > 1 &&
2009 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
2010 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
2011
2012 for (chix = 0; chix < udev->maxchild; chix++) {
2013 struct usb_device *childdev = udev->children[chix];
2014
2015 if (childdev)
2016 collect_usb_address_map(childdev, map);
2017 }
2018}
2019
2020/* this function must be called with interrupt disabled */
2021static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2022 int addr)
2023{
2024 struct r8a66597_device *dev;
2025 struct list_head *list = &r8a66597->child_device;
2026
2027 list_for_each_entry(dev, list, device_list) {
2028 if (!dev)
2029 continue;
2030 if (dev->usb_address != addr)
2031 continue;
2032
2033 return dev;
2034 }
2035
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002036 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002037 return NULL;
2038}
2039
2040static void update_usb_address_map(struct r8a66597 *r8a66597,
2041 struct usb_device *root_hub,
2042 unsigned long *map)
2043{
2044 int i, j, addr;
2045 unsigned long diff;
2046 unsigned long flags;
2047
2048 for (i = 0; i < 4; i++) {
2049 diff = r8a66597->child_connect_map[i] ^ map[i];
2050 if (!diff)
2051 continue;
2052
2053 for (j = 0; j < 32; j++) {
2054 if (!(diff & (1 << j)))
2055 continue;
2056
2057 addr = i * 32 + j;
2058 if (map[i] & (1 << j))
2059 set_child_connect_map(r8a66597, addr);
2060 else {
2061 struct r8a66597_device *dev;
2062
2063 spin_lock_irqsave(&r8a66597->lock, flags);
2064 dev = get_r8a66597_device(r8a66597, addr);
2065 disable_r8a66597_pipe_all(r8a66597, dev);
2066 free_usb_address(r8a66597, dev);
2067 put_child_connect_map(r8a66597, addr);
2068 spin_unlock_irqrestore(&r8a66597->lock, flags);
2069 }
2070 }
2071 }
2072}
2073
2074static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2075 struct usb_hcd *hcd)
2076{
2077 struct usb_bus *bus;
2078 unsigned long now_map[4];
2079
2080 memset(now_map, 0, sizeof(now_map));
2081
2082 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2083 if (!bus->root_hub)
2084 continue;
2085
2086 if (bus->busnum != hcd->self.busnum)
2087 continue;
2088
2089 collect_usb_address_map(bus->root_hub, now_map);
2090 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2091 }
2092}
2093
2094static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2095{
2096 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2097 unsigned long flags;
2098 int i;
2099
2100 r8a66597_check_detect_child(r8a66597, hcd);
2101
2102 spin_lock_irqsave(&r8a66597->lock, flags);
2103
2104 *buf = 0; /* initialize (no change) */
2105
Magnus Damm719a72b2009-07-17 14:59:55 +00002106 for (i = 0; i < r8a66597->max_root_hub; i++) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002107 if (r8a66597->root_hub[i].port & 0xffff0000)
2108 *buf |= 1 << (i + 1);
2109 }
2110
2111 spin_unlock_irqrestore(&r8a66597->lock, flags);
2112
2113 return (*buf != 0);
2114}
2115
2116static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2117 struct usb_hub_descriptor *desc)
2118{
2119 desc->bDescriptorType = 0x29;
2120 desc->bHubContrCurrent = 0;
Magnus Damm719a72b2009-07-17 14:59:55 +00002121 desc->bNbrPorts = r8a66597->max_root_hub;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002122 desc->bDescLength = 9;
2123 desc->bPwrOn2PwrGood = 0;
2124 desc->wHubCharacteristics = cpu_to_le16(0x0011);
Magnus Damm719a72b2009-07-17 14:59:55 +00002125 desc->bitmap[0] = ((1 << r8a66597->max_root_hub) - 1) << 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002126 desc->bitmap[1] = ~0;
2127}
2128
2129static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2130 u16 wIndex, char *buf, u16 wLength)
2131{
2132 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2133 int ret;
2134 int port = (wIndex & 0x00FF) - 1;
2135 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2136 unsigned long flags;
2137
2138 ret = 0;
2139
2140 spin_lock_irqsave(&r8a66597->lock, flags);
2141 switch (typeReq) {
2142 case ClearHubFeature:
2143 case SetHubFeature:
2144 switch (wValue) {
2145 case C_HUB_OVER_CURRENT:
2146 case C_HUB_LOCAL_POWER:
2147 break;
2148 default:
2149 goto error;
2150 }
2151 break;
2152 case ClearPortFeature:
Magnus Damm719a72b2009-07-17 14:59:55 +00002153 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002154 goto error;
2155 if (wLength != 0)
2156 goto error;
2157
2158 switch (wValue) {
2159 case USB_PORT_FEAT_ENABLE:
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002160 rh->port &= ~(1 << USB_PORT_FEAT_POWER);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002161 break;
2162 case USB_PORT_FEAT_SUSPEND:
2163 break;
2164 case USB_PORT_FEAT_POWER:
2165 r8a66597_port_power(r8a66597, port, 0);
2166 break;
2167 case USB_PORT_FEAT_C_ENABLE:
2168 case USB_PORT_FEAT_C_SUSPEND:
2169 case USB_PORT_FEAT_C_CONNECTION:
2170 case USB_PORT_FEAT_C_OVER_CURRENT:
2171 case USB_PORT_FEAT_C_RESET:
2172 break;
2173 default:
2174 goto error;
2175 }
2176 rh->port &= ~(1 << wValue);
2177 break;
2178 case GetHubDescriptor:
2179 r8a66597_hub_descriptor(r8a66597,
2180 (struct usb_hub_descriptor *)buf);
2181 break;
2182 case GetHubStatus:
2183 *buf = 0x00;
2184 break;
2185 case GetPortStatus:
Magnus Damm719a72b2009-07-17 14:59:55 +00002186 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002187 goto error;
Al Virofd05e722008-04-28 07:00:16 +01002188 *(__le32 *)buf = cpu_to_le32(rh->port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002189 break;
2190 case SetPortFeature:
Magnus Damm719a72b2009-07-17 14:59:55 +00002191 if (wIndex > r8a66597->max_root_hub)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002192 goto error;
2193 if (wLength != 0)
2194 goto error;
2195
2196 switch (wValue) {
2197 case USB_PORT_FEAT_SUSPEND:
2198 break;
2199 case USB_PORT_FEAT_POWER:
2200 r8a66597_port_power(r8a66597, port, 1);
2201 rh->port |= (1 << USB_PORT_FEAT_POWER);
2202 break;
2203 case USB_PORT_FEAT_RESET: {
2204 struct r8a66597_device *dev = rh->dev;
2205
2206 rh->port |= (1 << USB_PORT_FEAT_RESET);
2207
2208 disable_r8a66597_pipe_all(r8a66597, dev);
2209 free_usb_address(r8a66597, dev);
2210
2211 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2212 get_dvstctr_reg(port));
2213 mod_timer(&r8a66597->rh_timer,
2214 jiffies + msecs_to_jiffies(50));
2215 }
2216 break;
2217 default:
2218 goto error;
2219 }
2220 rh->port |= 1 << wValue;
2221 break;
2222 default:
2223error:
2224 ret = -EPIPE;
2225 break;
2226 }
2227
2228 spin_unlock_irqrestore(&r8a66597->lock, flags);
2229 return ret;
2230}
2231
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002232#if defined(CONFIG_PM)
2233static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2234{
2235 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2236 int port;
2237
2238 dbg("%s", __func__);
2239
Magnus Damm719a72b2009-07-17 14:59:55 +00002240 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002241 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2242 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2243
2244 if (!(rh->port & (1 << USB_PORT_FEAT_ENABLE)))
2245 continue;
2246
2247 dbg("suspend port = %d", port);
2248 r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
2249 rh->port |= 1 << USB_PORT_FEAT_SUSPEND;
2250
2251 if (rh->dev->udev->do_remote_wakeup) {
2252 msleep(3); /* waiting last SOF */
2253 r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2254 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2255 r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2256 }
2257 }
2258
2259 r8a66597->bus_suspended = 1;
2260
2261 return 0;
2262}
2263
2264static int r8a66597_bus_resume(struct usb_hcd *hcd)
2265{
2266 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2267 int port;
2268
2269 dbg("%s", __func__);
2270
Magnus Damm719a72b2009-07-17 14:59:55 +00002271 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002272 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2273 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2274
2275 if (!(rh->port & (1 << USB_PORT_FEAT_SUSPEND)))
2276 continue;
2277
2278 dbg("resume port = %d", port);
2279 rh->port &= ~(1 << USB_PORT_FEAT_SUSPEND);
2280 rh->port |= 1 << USB_PORT_FEAT_C_SUSPEND;
2281 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2282 msleep(50);
2283 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2284 }
2285
2286 return 0;
2287
2288}
2289#else
2290#define r8a66597_bus_suspend NULL
2291#define r8a66597_bus_resume NULL
2292#endif
2293
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002294static struct hc_driver r8a66597_hc_driver = {
2295 .description = hcd_name,
2296 .hcd_priv_size = sizeof(struct r8a66597),
2297 .irq = r8a66597_irq,
2298
2299 /*
2300 * generic hardware linkage
2301 */
2302 .flags = HCD_USB2,
2303
2304 .start = r8a66597_start,
2305 .stop = r8a66597_stop,
2306
2307 /*
2308 * managing i/o requests and associated device resources
2309 */
2310 .urb_enqueue = r8a66597_urb_enqueue,
2311 .urb_dequeue = r8a66597_urb_dequeue,
2312 .endpoint_disable = r8a66597_endpoint_disable,
2313
2314 /*
2315 * periodic schedule support
2316 */
2317 .get_frame_number = r8a66597_get_frame,
2318
2319 /*
2320 * root hub support
2321 */
2322 .hub_status_data = r8a66597_hub_status_data,
2323 .hub_control = r8a66597_hub_control,
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002324 .bus_suspend = r8a66597_bus_suspend,
2325 .bus_resume = r8a66597_bus_resume,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002326};
2327
2328#if defined(CONFIG_PM)
Magnus Dammae1cef62009-07-17 14:52:05 +00002329static int r8a66597_suspend(struct device *dev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002330{
Magnus Dammae1cef62009-07-17 14:52:05 +00002331 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002332 int port;
2333
2334 dbg("%s", __func__);
2335
2336 disable_controller(r8a66597);
2337
Magnus Damm719a72b2009-07-17 14:59:55 +00002338 for (port = 0; port < r8a66597->max_root_hub; port++) {
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002339 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2340
2341 rh->port = 0x00000000;
2342 }
2343
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002344 return 0;
2345}
2346
Magnus Dammae1cef62009-07-17 14:52:05 +00002347static int r8a66597_resume(struct device *dev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002348{
Magnus Dammae1cef62009-07-17 14:52:05 +00002349 struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002350 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2351
2352 dbg("%s", __func__);
2353
2354 enable_controller(r8a66597);
2355 usb_root_hub_lost_power(hcd->self.root_hub);
2356
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002357 return 0;
2358}
Magnus Dammae1cef62009-07-17 14:52:05 +00002359
2360static struct dev_pm_ops r8a66597_dev_pm_ops = {
2361 .suspend = r8a66597_suspend,
2362 .resume = r8a66597_resume,
Yoshihiro Shimoda3725f282009-07-29 09:24:41 +00002363 .poweroff = r8a66597_suspend,
2364 .restore = r8a66597_resume,
Magnus Dammae1cef62009-07-17 14:52:05 +00002365};
2366
2367#define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002368#else /* if defined(CONFIG_PM) */
Magnus Dammae1cef62009-07-17 14:52:05 +00002369#define R8A66597_DEV_PM_OPS NULL
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002370#endif
2371
2372static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2373{
2374 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2375 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2376
2377 del_timer_sync(&r8a66597->rh_timer);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002378 usb_remove_hcd(hcd);
Yoshihiro Shimodaca0677a2007-10-05 15:53:12 +09002379 iounmap((void *)r8a66597->reg);
Magnus Damm719a72b2009-07-17 14:59:55 +00002380#ifdef CONFIG_HAVE_CLK
2381 if (r8a66597->pdata->on_chip)
2382 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002383#endif
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002384 usb_put_hcd(hcd);
2385 return 0;
2386}
2387
Uwe Kleine-König8864bd82009-03-28 00:27:00 +01002388static int __devinit r8a66597_probe(struct platform_device *pdev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002389{
Magnus Damm719a72b2009-07-17 14:59:55 +00002390#ifdef CONFIG_HAVE_CLK
Magnus Damm765786e2008-10-31 20:22:38 +09002391 char clk_name[8];
2392#endif
Marc Zyngier27140212008-08-18 13:08:42 +02002393 struct resource *res = NULL, *ires;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002394 int irq = -1;
2395 void __iomem *reg = NULL;
2396 struct usb_hcd *hcd = NULL;
2397 struct r8a66597 *r8a66597;
2398 int ret = 0;
2399 int i;
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002400 unsigned long irq_trigger;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002401
2402 if (pdev->dev.dma_mask) {
2403 ret = -EINVAL;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002404 dev_err(&pdev->dev, "dma not supported\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002405 goto clean_up;
2406 }
2407
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002408 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002409 if (!res) {
2410 ret = -ENODEV;
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002411 dev_err(&pdev->dev, "platform_get_resource error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002412 goto clean_up;
2413 }
2414
Marc Zyngier27140212008-08-18 13:08:42 +02002415 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2416 if (!ires) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002417 ret = -ENODEV;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002418 dev_err(&pdev->dev,
2419 "platform_get_resource IORESOURCE_IRQ error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002420 goto clean_up;
2421 }
2422
Marc Zyngier27140212008-08-18 13:08:42 +02002423 irq = ires->start;
2424 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2425
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002426 reg = ioremap(res->start, resource_size(res));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002427 if (reg == NULL) {
2428 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002429 dev_err(&pdev->dev, "ioremap error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002430 goto clean_up;
2431 }
2432
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002433 if (pdev->dev.platform_data == NULL) {
2434 dev_err(&pdev->dev, "no platform data\n");
2435 ret = -ENODEV;
2436 goto clean_up;
2437 }
2438
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002439 /* initialize hcd */
2440 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2441 if (!hcd) {
2442 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002443 dev_err(&pdev->dev, "Failed to create hcd\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002444 goto clean_up;
2445 }
2446 r8a66597 = hcd_to_r8a66597(hcd);
2447 memset(r8a66597, 0, sizeof(struct r8a66597));
2448 dev_set_drvdata(&pdev->dev, r8a66597);
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002449 r8a66597->pdata = pdev->dev.platform_data;
2450 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002451
Magnus Damm719a72b2009-07-17 14:59:55 +00002452 if (r8a66597->pdata->on_chip) {
2453#ifdef CONFIG_HAVE_CLK
2454 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2455 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2456 if (IS_ERR(r8a66597->clk)) {
2457 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
2458 clk_name);
2459 ret = PTR_ERR(r8a66597->clk);
2460 goto clean_up2;
2461 }
Magnus Damm765786e2008-10-31 20:22:38 +09002462#endif
Magnus Damm719a72b2009-07-17 14:59:55 +00002463 r8a66597->max_root_hub = 1;
2464 } else
2465 r8a66597->max_root_hub = 2;
Magnus Damm765786e2008-10-31 20:22:38 +09002466
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002467 spin_lock_init(&r8a66597->lock);
2468 init_timer(&r8a66597->rh_timer);
2469 r8a66597->rh_timer.function = r8a66597_timer;
2470 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2471 r8a66597->reg = (unsigned long)reg;
2472
2473 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2474 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2475 init_timer(&r8a66597->td_timer[i]);
2476 r8a66597->td_timer[i].function = r8a66597_td_timer;
2477 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09002478 setup_timer(&r8a66597->interval_timer[i],
2479 r8a66597_interval_timer,
2480 (unsigned long)r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002481 }
2482 INIT_LIST_HEAD(&r8a66597->child_device);
2483
2484 hcd->rsrc_start = res->start;
Marc Zyngier27140212008-08-18 13:08:42 +02002485
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002486 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002487 if (ret != 0) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002488 dev_err(&pdev->dev, "Failed to add hcd\n");
Magnus Damm765786e2008-10-31 20:22:38 +09002489 goto clean_up3;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002490 }
2491
2492 return 0;
2493
Magnus Damm765786e2008-10-31 20:22:38 +09002494clean_up3:
Magnus Damm719a72b2009-07-17 14:59:55 +00002495#ifdef CONFIG_HAVE_CLK
2496 if (r8a66597->pdata->on_chip)
2497 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002498clean_up2:
Paul Mundtd6435102008-11-18 12:40:39 +09002499#endif
Magnus Damm765786e2008-10-31 20:22:38 +09002500 usb_put_hcd(hcd);
2501
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002502clean_up:
2503 if (reg)
2504 iounmap(reg);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002505
2506 return ret;
2507}
2508
2509static struct platform_driver r8a66597_driver = {
2510 .probe = r8a66597_probe,
2511 .remove = r8a66597_remove,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002512 .driver = {
2513 .name = (char *) hcd_name,
Kay Sieversf4fce612008-04-10 21:29:22 -07002514 .owner = THIS_MODULE,
Magnus Dammae1cef62009-07-17 14:52:05 +00002515 .pm = R8A66597_DEV_PM_OPS,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002516 },
2517};
2518
2519static int __init r8a66597_init(void)
2520{
2521 if (usb_disabled())
2522 return -ENODEV;
2523
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -07002524 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2525 DRIVER_VERSION);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002526 return platform_driver_register(&r8a66597_driver);
2527}
2528module_init(r8a66597_init);
2529
2530static void __exit r8a66597_cleanup(void)
2531{
2532 platform_driver_unregister(&r8a66597_driver);
2533}
2534module_exit(r8a66597_cleanup);
2535