blob: c18d8790c4107c032eb0e34b94be345683828d8d [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>
29#include <linux/smp_lock.h>
30#include <linux/errno.h>
31#include <linux/init.h>
32#include <linux/timer.h>
33#include <linux/delay.h>
34#include <linux/list.h>
35#include <linux/interrupt.h>
36#include <linux/usb.h>
37#include <linux/platform_device.h>
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090038#include <linux/io.h>
39#include <linux/irq.h>
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090040
41#include "../core/hcd.h"
42#include "r8a66597.h"
43
44MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
45MODULE_LICENSE("GPL");
46MODULE_AUTHOR("Yoshihiro Shimoda");
Kay Sieversf4fce612008-04-10 21:29:22 -070047MODULE_ALIAS("platform:r8a66597_hcd");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090048
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +090049#define DRIVER_VERSION "10 Apr 2008"
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090050
51static const char hcd_name[] = "r8a66597_hcd";
52
53/* module parameters */
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +090054#if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090055static unsigned short clock = XTAL12;
56module_param(clock, ushort, 0644);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090057MODULE_PARM_DESC(clock, "input clock: 48MHz=32768, 24MHz=16384, 12MHz=0 "
58 "(default=0)");
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +090059#endif
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090060
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090061static unsigned short vif = LDRV;
62module_param(vif, ushort, 0644);
63MODULE_PARM_DESC(vif, "input VIF: 3.3V=32768, 1.5V=0(default=32768)");
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090064
65static unsigned short endian;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090066module_param(endian, ushort, 0644);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090067MODULE_PARM_DESC(endian, "data endian: big=256, little=0 (default=0)");
68
Marc Zyngier27140212008-08-18 13:08:42 +020069static unsigned short irq_sense = 0xff;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090070module_param(irq_sense, ushort, 0644);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +090071MODULE_PARM_DESC(irq_sense, "IRQ sense: low level=32, falling edge=0 "
72 "(default=32)");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +090073
74static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
75static int r8a66597_get_frame(struct usb_hcd *hcd);
76
77/* this function must be called with interrupt disabled */
78static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
79 unsigned long reg)
80{
81 u16 tmp;
82
83 tmp = r8a66597_read(r8a66597, INTENB0);
84 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
85 r8a66597_bset(r8a66597, 1 << pipenum, reg);
86 r8a66597_write(r8a66597, tmp, INTENB0);
87}
88
89/* this function must be called with interrupt disabled */
90static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
91 unsigned long reg)
92{
93 u16 tmp;
94
95 tmp = r8a66597_read(r8a66597, INTENB0);
96 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
97 r8a66597_bclr(r8a66597, 1 << pipenum, reg);
98 r8a66597_write(r8a66597, tmp, INTENB0);
99}
100
101static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
102 u16 usbspd, u8 upphub, u8 hubport, int port)
103{
104 u16 val;
105 unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
106
107 val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
108 r8a66597_write(r8a66597, val, devadd_reg);
109}
110
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900111static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900112{
113 u16 tmp;
114 int i = 0;
115
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900116#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
117 do {
118 r8a66597_write(r8a66597, SCKE, SYSCFG0);
119 tmp = r8a66597_read(r8a66597, SYSCFG0);
120 if (i++ > 1000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700121 printk(KERN_ERR "r8a66597: register access fail.\n");
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900122 return -ENXIO;
123 }
124 } while ((tmp & SCKE) != SCKE);
125 r8a66597_write(r8a66597, 0x04, 0x02);
126#else
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900127 do {
128 r8a66597_write(r8a66597, USBE, SYSCFG0);
129 tmp = r8a66597_read(r8a66597, SYSCFG0);
130 if (i++ > 1000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700131 printk(KERN_ERR "r8a66597: register access fail.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900132 return -ENXIO;
133 }
134 } while ((tmp & USBE) != USBE);
135 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
136 r8a66597_mdfy(r8a66597, clock, XTAL, SYSCFG0);
137
138 i = 0;
139 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
140 do {
141 msleep(1);
142 tmp = r8a66597_read(r8a66597, SYSCFG0);
143 if (i++ > 500) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700144 printk(KERN_ERR "r8a66597: register access fail.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900145 return -ENXIO;
146 }
147 } while ((tmp & SCKE) != SCKE);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900148#endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900149
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900150 return 0;
151}
152
153static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
154{
155 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
156 udelay(1);
157#if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
158 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
159 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
160 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
161#endif
162}
163
164static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
165{
166 u16 val;
167
168 val = port ? DRPD : DCFM | DRPD;
169 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
170 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
171
172 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
173 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
174 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
175}
176
177static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
178{
179 u16 val, tmp;
180
181 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
182 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
183
184 r8a66597_port_power(r8a66597, port, 0);
185
186 do {
187 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
188 udelay(640);
189 } while (tmp == EDGESTS);
190
191 val = port ? DRPD : DCFM | DRPD;
192 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
193 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
194}
195
196static int enable_controller(struct r8a66597 *r8a66597)
197{
198 int ret, port;
199
200 ret = r8a66597_clock_enable(r8a66597);
201 if (ret < 0)
202 return ret;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900203
204 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900205 r8a66597_bset(r8a66597, USBE, SYSCFG0);
206
207 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
208 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
209 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
210 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
211
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900212 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
213 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
214 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900215 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
216
217 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900218
219 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
220 r8a66597_enable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900221
222 return 0;
223}
224
225static void disable_controller(struct r8a66597 *r8a66597)
226{
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900227 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900228
229 r8a66597_write(r8a66597, 0, INTENB0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900230 r8a66597_write(r8a66597, 0, INTSTS0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900231
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900232 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
233 r8a66597_disable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900234
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900235 r8a66597_clock_disable(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900236}
237
238static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
239 struct usb_device *udev)
240{
241 struct r8a66597_device *dev;
242
243 if (udev->parent && udev->parent->devnum != 1)
244 udev = udev->parent;
245
246 dev = dev_get_drvdata(&udev->dev);
247 if (dev)
248 return dev->address;
249 else
250 return 0;
251}
252
253static int is_child_device(char *devpath)
254{
255 return (devpath[2] ? 1 : 0);
256}
257
258static int is_hub_limit(char *devpath)
259{
260 return ((strlen(devpath) >= 4) ? 1 : 0);
261}
262
263static void get_port_number(char *devpath, u16 *root_port, u16 *hub_port)
264{
265 if (root_port) {
266 *root_port = (devpath[0] & 0x0F) - 1;
267 if (*root_port >= R8A66597_MAX_ROOT_HUB)
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700268 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900269 }
270 if (hub_port)
271 *hub_port = devpath[2] & 0x0F;
272}
273
274static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
275{
276 u16 usbspd = 0;
277
278 switch (speed) {
279 case USB_SPEED_LOW:
280 usbspd = LSMODE;
281 break;
282 case USB_SPEED_FULL:
283 usbspd = FSMODE;
284 break;
285 case USB_SPEED_HIGH:
286 usbspd = HSMODE;
287 break;
288 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700289 printk(KERN_ERR "r8a66597: unknown speed\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900290 break;
291 }
292
293 return usbspd;
294}
295
296static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
297{
298 int idx;
299
300 idx = address / 32;
301 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
302}
303
304static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
305{
306 int idx;
307
308 idx = address / 32;
309 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
310}
311
312static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
313{
314 u16 pipenum = pipe->info.pipenum;
Ming Leife9b9032008-06-08 16:13:03 +0800315 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
316 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
317 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900318
319 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
320 dma_ch = R8A66597_PIPE_NO_DMA;
321
322 pipe->fifoaddr = fifoaddr[dma_ch];
323 pipe->fifosel = fifosel[dma_ch];
324 pipe->fifoctr = fifoctr[dma_ch];
325
326 if (pipenum == 0)
327 pipe->pipectr = DCPCTR;
328 else
329 pipe->pipectr = get_pipectr_addr(pipenum);
330
331 if (check_bulk_or_isoc(pipenum)) {
332 pipe->pipetre = get_pipetre_addr(pipenum);
333 pipe->pipetrn = get_pipetrn_addr(pipenum);
334 } else {
335 pipe->pipetre = 0;
336 pipe->pipetrn = 0;
337 }
338}
339
340static struct r8a66597_device *
341get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
342{
343 if (usb_pipedevice(urb->pipe) == 0)
344 return &r8a66597->device0;
345
346 return dev_get_drvdata(&urb->dev->dev);
347}
348
349static int make_r8a66597_device(struct r8a66597 *r8a66597,
350 struct urb *urb, u8 addr)
351{
352 struct r8a66597_device *dev;
353 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
354
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900355 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900356 if (dev == NULL)
357 return -ENOMEM;
358
359 dev_set_drvdata(&urb->dev->dev, dev);
360 dev->udev = urb->dev;
361 dev->address = addr;
362 dev->usb_address = usb_address;
363 dev->state = USB_STATE_ADDRESS;
364 dev->ep_in_toggle = 0;
365 dev->ep_out_toggle = 0;
366 INIT_LIST_HEAD(&dev->device_list);
367 list_add_tail(&dev->device_list, &r8a66597->child_device);
368
369 get_port_number(urb->dev->devpath, &dev->root_port, &dev->hub_port);
370 if (!is_child_device(urb->dev->devpath))
371 r8a66597->root_hub[dev->root_port].dev = dev;
372
373 set_devadd_reg(r8a66597, dev->address,
374 get_r8a66597_usb_speed(urb->dev->speed),
375 get_parent_r8a66597_address(r8a66597, urb->dev),
376 dev->hub_port, dev->root_port);
377
378 return 0;
379}
380
381/* this function must be called with interrupt disabled */
382static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
383{
384 u8 addr; /* R8A66597's address */
385 struct r8a66597_device *dev;
386
387 if (is_hub_limit(urb->dev->devpath)) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700388 dev_err(&urb->dev->dev, "External hub limit reached.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900389 return 0;
390 }
391
392 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
393 if (dev && dev->state >= USB_STATE_ADDRESS)
394 return dev->address;
395
396 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
397 if (r8a66597->address_map & (1 << addr))
398 continue;
399
400 dbg("alloc_address: r8a66597_addr=%d", addr);
401 r8a66597->address_map |= 1 << addr;
402
403 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
404 return 0;
405
406 return addr;
407 }
408
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700409 dev_err(&urb->dev->dev,
410 "cannot communicate with a USB device more than 10.(%x)\n",
411 r8a66597->address_map);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900412
413 return 0;
414}
415
416/* this function must be called with interrupt disabled */
417static void free_usb_address(struct r8a66597 *r8a66597,
418 struct r8a66597_device *dev)
419{
420 int port;
421
422 if (!dev)
423 return;
424
425 dbg("free_addr: addr=%d", dev->address);
426
427 dev->state = USB_STATE_DEFAULT;
428 r8a66597->address_map &= ~(1 << dev->address);
429 dev->address = 0;
430 dev_set_drvdata(&dev->udev->dev, NULL);
431 list_del(&dev->device_list);
432 kfree(dev);
433
434 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
435 if (r8a66597->root_hub[port].dev == dev) {
436 r8a66597->root_hub[port].dev = NULL;
437 break;
438 }
439 }
440}
441
442static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
443 u16 mask, u16 loop)
444{
445 u16 tmp;
446 int i = 0;
447
448 do {
449 tmp = r8a66597_read(r8a66597, reg);
450 if (i++ > 1000000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700451 printk(KERN_ERR "r8a66597: register%lx, loop %x "
452 "is timeout\n", reg, loop);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900453 break;
454 }
455 ndelay(1);
456 } while ((tmp & mask) != loop);
457}
458
459/* this function must be called with interrupt disabled */
460static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
461{
462 u16 tmp;
463
464 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
465 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
466 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
467 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
468}
469
470/* this function must be called with interrupt disabled */
471static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
472{
473 u16 tmp;
474
475 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
476 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
477 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
478 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
479 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
480}
481
482/* this function must be called with interrupt disabled */
483static void clear_all_buffer(struct r8a66597 *r8a66597,
484 struct r8a66597_pipe *pipe)
485{
486 u16 tmp;
487
488 if (!pipe || pipe->info.pipenum == 0)
489 return;
490
491 pipe_stop(r8a66597, pipe);
492 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
493 tmp = r8a66597_read(r8a66597, pipe->pipectr);
494 tmp = r8a66597_read(r8a66597, pipe->pipectr);
495 tmp = r8a66597_read(r8a66597, pipe->pipectr);
496 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
497}
498
499/* this function must be called with interrupt disabled */
500static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
501 struct r8a66597_pipe *pipe, int toggle)
502{
503 if (toggle)
504 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
505 else
506 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
507}
508
509/* this function must be called with interrupt disabled */
510static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
511{
512 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
513 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
514}
515
516/* this function must be called with interrupt disabled */
517static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
518 struct r8a66597_pipe *pipe)
519{
520 cfifo_change(r8a66597, 0);
521 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D0FIFOSEL);
522 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D1FIFOSEL);
523
524 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum, MBW | CURPIPE,
525 pipe->fifosel);
526 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
527}
528
529static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
530{
531 struct r8a66597_pipe *pipe = hep->hcpriv;
532
533 if (usb_pipeendpoint(urb->pipe) == 0)
534 return 0;
535 else
536 return pipe->info.pipenum;
537}
538
539static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
540{
541 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
542
543 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
544}
545
546static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
547 int urb_pipe)
548{
549 if (!dev)
550 return NULL;
551
552 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
553}
554
555/* this function must be called with interrupt disabled */
556static void pipe_toggle_set(struct r8a66597 *r8a66597,
557 struct r8a66597_pipe *pipe,
558 struct urb *urb, int set)
559{
560 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
561 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
562 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
563
564 if (!toggle)
565 return;
566
567 if (set)
568 *toggle |= 1 << endpoint;
569 else
570 *toggle &= ~(1 << endpoint);
571}
572
573/* this function must be called with interrupt disabled */
574static void pipe_toggle_save(struct r8a66597 *r8a66597,
575 struct r8a66597_pipe *pipe,
576 struct urb *urb)
577{
578 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
579 pipe_toggle_set(r8a66597, pipe, urb, 1);
580 else
581 pipe_toggle_set(r8a66597, pipe, urb, 0);
582}
583
584/* this function must be called with interrupt disabled */
585static void pipe_toggle_restore(struct r8a66597 *r8a66597,
586 struct r8a66597_pipe *pipe,
587 struct urb *urb)
588{
589 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
590 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
591 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
592
Yoshihiro Shimodaf6ace2c2007-05-30 20:42:41 +0900593 if (!toggle)
594 return;
595
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900596 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
597}
598
599/* this function must be called with interrupt disabled */
600static void pipe_buffer_setting(struct r8a66597 *r8a66597,
601 struct r8a66597_pipe_info *info)
602{
603 u16 val = 0;
604
605 if (info->pipenum == 0)
606 return;
607
608 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
609 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
610 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
611 if (!info->dir_in)
612 val |= R8A66597_DIR;
613 if (info->type == R8A66597_BULK && info->dir_in)
614 val |= R8A66597_DBLB | R8A66597_SHTNAK;
615 val |= info->type | info->epnum;
616 r8a66597_write(r8a66597, val, PIPECFG);
617
618 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
619 PIPEBUF);
620 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
621 PIPEMAXP);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900622 r8a66597_write(r8a66597, info->interval, PIPEPERI);
623}
624
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900625/* this function must be called with interrupt disabled */
626static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
627{
628 struct r8a66597_pipe_info *info;
629 struct urb *urb = td->urb;
630
631 if (td->pipenum > 0) {
632 info = &td->pipe->info;
633 cfifo_change(r8a66597, 0);
634 pipe_buffer_setting(r8a66597, info);
635
636 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
637 usb_pipeout(urb->pipe)) &&
638 !usb_pipecontrol(urb->pipe)) {
639 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
640 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
641 clear_all_buffer(r8a66597, td->pipe);
642 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
643 usb_pipeout(urb->pipe), 1);
644 }
645 pipe_toggle_restore(r8a66597, td->pipe, urb);
646 }
647}
648
649/* this function must be called with interrupt disabled */
650static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
651 struct usb_endpoint_descriptor *ep)
652{
653 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
654
655 memset(array, 0, sizeof(array));
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900656 switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
657 case USB_ENDPOINT_XFER_BULK:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900658 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
659 array[i++] = 4;
660 else {
661 array[i++] = 3;
662 array[i++] = 5;
663 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900664 break;
665 case USB_ENDPOINT_XFER_INT:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900666 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
667 array[i++] = 6;
668 array[i++] = 7;
669 array[i++] = 8;
670 } else
671 array[i++] = 9;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900672 break;
673 case USB_ENDPOINT_XFER_ISOC:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900674 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
675 array[i++] = 2;
676 else
677 array[i++] = 1;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900678 break;
679 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700680 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900681 return 0;
682 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900683
684 i = 1;
685 min = array[0];
686 while (array[i] != 0) {
687 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
688 min = array[i];
689 i++;
690 }
691
692 return min;
693}
694
695static u16 get_r8a66597_type(__u8 type)
696{
697 u16 r8a66597_type;
698
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900699 switch (type) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900700 case USB_ENDPOINT_XFER_BULK:
701 r8a66597_type = R8A66597_BULK;
702 break;
703 case USB_ENDPOINT_XFER_INT:
704 r8a66597_type = R8A66597_INT;
705 break;
706 case USB_ENDPOINT_XFER_ISOC:
707 r8a66597_type = R8A66597_ISO;
708 break;
709 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700710 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900711 r8a66597_type = 0x0000;
712 break;
713 }
714
715 return r8a66597_type;
716}
717
718static u16 get_bufnum(u16 pipenum)
719{
720 u16 bufnum = 0;
721
722 if (pipenum == 0)
723 bufnum = 0;
724 else if (check_bulk_or_isoc(pipenum))
725 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
726 else if (check_interrupt(pipenum))
727 bufnum = 4 + (pipenum - 6);
728 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700729 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900730
731 return bufnum;
732}
733
734static u16 get_buf_bsize(u16 pipenum)
735{
736 u16 buf_bsize = 0;
737
738 if (pipenum == 0)
739 buf_bsize = 3;
740 else if (check_bulk_or_isoc(pipenum))
741 buf_bsize = R8A66597_BUF_BSIZE - 1;
742 else if (check_interrupt(pipenum))
743 buf_bsize = 0;
744 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700745 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900746
747 return buf_bsize;
748}
749
750/* this function must be called with interrupt disabled */
751static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
752 struct r8a66597_device *dev,
753 struct r8a66597_pipe *pipe,
754 struct urb *urb)
755{
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900756#if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900757 int i;
758 struct r8a66597_pipe_info *info = &pipe->info;
759
760 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
761 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
762 if ((r8a66597->dma_map & (1 << i)) != 0)
763 continue;
764
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -0700765 dev_info(&dev->udev->dev,
766 "address %d, EndpointAddress 0x%02x use "
767 "DMA FIFO\n", usb_pipedevice(urb->pipe),
768 info->dir_in ?
769 USB_ENDPOINT_DIR_MASK + info->epnum
770 : info->epnum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900771
772 r8a66597->dma_map |= 1 << i;
773 dev->dma_map |= 1 << i;
774 set_pipe_reg_addr(pipe, i);
775
776 cfifo_change(r8a66597, 0);
777 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum,
778 MBW | CURPIPE, pipe->fifosel);
779
780 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
781 pipe->info.pipenum);
782 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
783 break;
784 }
785 }
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900786#endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900787}
788
789/* this function must be called with interrupt disabled */
790static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
791 struct usb_host_endpoint *hep,
792 struct r8a66597_pipe_info *info)
793{
794 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
795 struct r8a66597_pipe *pipe = hep->hcpriv;
796
797 dbg("enable_pipe:");
798
799 pipe->info = *info;
800 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
801 r8a66597->pipe_cnt[pipe->info.pipenum]++;
802 dev->pipe_cnt[pipe->info.pipenum]++;
803
804 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
805}
806
807/* this function must be called with interrupt disabled */
808static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
809{
810 struct r8a66597_td *td, *next;
811 struct urb *urb;
812 struct list_head *list = &r8a66597->pipe_queue[pipenum];
813
814 if (list_empty(list))
815 return;
816
817 list_for_each_entry_safe(td, next, list, queue) {
818 if (!td)
819 continue;
820 if (td->address != address)
821 continue;
822
823 urb = td->urb;
824 list_del(&td->queue);
825 kfree(td);
826
827 if (urb) {
Alan Sterne9df41c2007-08-08 11:48:02 -0400828 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
829 urb);
830
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900831 spin_unlock(&r8a66597->lock);
Alan Stern4a000272007-08-24 15:42:24 -0400832 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
833 -ENODEV);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900834 spin_lock(&r8a66597->lock);
835 }
836 break;
837 }
838}
839
840/* this function must be called with interrupt disabled */
841static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
842 struct r8a66597_device *dev)
843{
844 int check_ep0 = 0;
845 u16 pipenum;
846
847 if (!dev)
848 return;
849
850 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
851 if (!dev->pipe_cnt[pipenum])
852 continue;
853
854 if (!check_ep0) {
855 check_ep0 = 1;
856 force_dequeue(r8a66597, 0, dev->address);
857 }
858
859 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
860 dev->pipe_cnt[pipenum] = 0;
861 force_dequeue(r8a66597, pipenum, dev->address);
862 }
863
864 dbg("disable_pipe");
865
866 r8a66597->dma_map &= ~(dev->dma_map);
867 dev->dma_map = 0;
868}
869
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900870static u16 get_interval(struct urb *urb, __u8 interval)
871{
872 u16 time = 1;
873 int i;
874
875 if (urb->dev->speed == USB_SPEED_HIGH) {
876 if (interval > IITV)
877 time = IITV;
878 else
879 time = interval ? interval - 1 : 0;
880 } else {
881 if (interval > 128) {
882 time = IITV;
883 } else {
884 /* calculate the nearest value for PIPEPERI */
885 for (i = 0; i < 7; i++) {
886 if ((1 << i) < interval &&
887 (1 << (i + 1) > interval))
888 time = 1 << i;
889 }
890 }
891 }
892
893 return time;
894}
895
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900896static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
897{
898 __u8 i;
899 unsigned long time = 1;
900
901 if (usb_pipeisoc(urb->pipe))
902 return 0;
903
904 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
905 for (i = 0; i < (interval - 1); i++)
906 time *= 2;
907 time = time * 125 / 1000; /* uSOF -> msec */
908 } else {
909 time = interval;
910 }
911
912 return time;
913}
914
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900915/* this function must be called with interrupt disabled */
916static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
917 struct usb_host_endpoint *hep,
918 struct usb_endpoint_descriptor *ep)
919{
920 struct r8a66597_pipe_info info;
921
922 info.pipenum = get_empty_pipenum(r8a66597, ep);
923 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
924 info.epnum = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
Yoshihiro Shimoda05eac912007-10-03 18:53:28 +0900925 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900926 info.type = get_r8a66597_type(ep->bmAttributes
927 & USB_ENDPOINT_XFERTYPE_MASK);
928 info.bufnum = get_bufnum(info.pipenum);
929 info.buf_bsize = get_buf_bsize(info.pipenum);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900930 if (info.type == R8A66597_BULK) {
931 info.interval = 0;
932 info.timer_interval = 0;
933 } else {
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900934 info.interval = get_interval(urb, ep->bInterval);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900935 info.timer_interval = get_timer_interval(urb, ep->bInterval);
936 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900937 if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
938 info.dir_in = 1;
939 else
940 info.dir_in = 0;
941
942 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
943}
944
945static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
946{
947 struct r8a66597_device *dev;
948
949 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
950 dev->state = USB_STATE_CONFIGURED;
951}
952
953static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
954 u16 pipenum)
955{
956 if (pipenum == 0 && usb_pipeout(urb->pipe))
957 enable_irq_empty(r8a66597, pipenum);
958 else
959 enable_irq_ready(r8a66597, pipenum);
960
961 if (!usb_pipeisoc(urb->pipe))
962 enable_irq_nrdy(r8a66597, pipenum);
963}
964
965static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
966{
967 disable_irq_ready(r8a66597, pipenum);
968 disable_irq_nrdy(r8a66597, pipenum);
969}
970
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +0900971static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
972{
973 mod_timer(&r8a66597->rh_timer,
974 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
975}
976
977static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
978 int connect)
979{
980 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
981
982 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
983 rh->scount = R8A66597_MAX_SAMPLING;
984 if (connect)
985 rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
986 else
987 rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
988 rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
989
990 r8a66597_root_hub_start_polling(r8a66597);
991}
992
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900993/* this function must be called with interrupt disabled */
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +0900994static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
995 u16 syssts)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900996{
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +0900997 if (syssts == SE0) {
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +0900998 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +0900999 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1000 return;
1001 }
1002
1003 if (syssts == FS_JSTS)
1004 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
1005 else if (syssts == LS_JSTS)
1006 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
1007
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001008 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001009 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
1010}
1011
1012/* this function must be called with interrupt disabled */
1013static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1014{
1015 u16 speed = get_rh_usb_speed(r8a66597, port);
1016 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1017
1018 if (speed == HSMODE)
1019 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
1020 else if (speed == LSMODE)
1021 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
1022
1023 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
1024 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1025}
1026
1027/* this function must be called with interrupt disabled */
1028static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1029{
1030 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1031
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001032 disable_r8a66597_pipe_all(r8a66597, dev);
1033 free_usb_address(r8a66597, dev);
1034
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001035 start_root_hub_sampling(r8a66597, port, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001036}
1037
1038/* this function must be called with interrupt disabled */
1039static void prepare_setup_packet(struct r8a66597 *r8a66597,
1040 struct r8a66597_td *td)
1041{
1042 int i;
Al Virofd05e722008-04-28 07:00:16 +01001043 __le16 *p = (__le16 *)td->urb->setup_packet;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001044 unsigned long setup_addr = USBREQ;
1045
1046 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1047 DCPMAXP);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001048 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001049
1050 for (i = 0; i < 4; i++) {
Al Virofd05e722008-04-28 07:00:16 +01001051 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001052 setup_addr += 2;
1053 }
1054 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1055}
1056
1057/* this function must be called with interrupt disabled */
1058static void prepare_packet_read(struct r8a66597 *r8a66597,
1059 struct r8a66597_td *td)
1060{
1061 struct urb *urb = td->urb;
1062
1063 if (usb_pipecontrol(urb->pipe)) {
1064 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1065 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1066 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1067 if (urb->actual_length == 0) {
1068 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1069 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1070 }
1071 pipe_irq_disable(r8a66597, td->pipenum);
1072 pipe_start(r8a66597, td->pipe);
1073 pipe_irq_enable(r8a66597, urb, td->pipenum);
1074 } else {
1075 if (urb->actual_length == 0) {
1076 pipe_irq_disable(r8a66597, td->pipenum);
1077 pipe_setting(r8a66597, td);
1078 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001079 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001080
1081 if (td->pipe->pipetre) {
1082 r8a66597_write(r8a66597, TRCLR,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001083 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001084 r8a66597_write(r8a66597,
Julia Lawalldfa5ec72008-03-04 15:25:11 -08001085 DIV_ROUND_UP
1086 (urb->transfer_buffer_length,
1087 td->maxpacket),
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001088 td->pipe->pipetrn);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001089 r8a66597_bset(r8a66597, TRENB,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001090 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001091 }
1092
1093 pipe_start(r8a66597, td->pipe);
1094 pipe_irq_enable(r8a66597, urb, td->pipenum);
1095 }
1096 }
1097}
1098
1099/* this function must be called with interrupt disabled */
1100static void prepare_packet_write(struct r8a66597 *r8a66597,
1101 struct r8a66597_td *td)
1102{
1103 u16 tmp;
1104 struct urb *urb = td->urb;
1105
1106 if (usb_pipecontrol(urb->pipe)) {
1107 pipe_stop(r8a66597, td->pipe);
1108 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1109 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1110 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1111 if (urb->actual_length == 0) {
1112 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1113 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1114 }
1115 } else {
1116 if (urb->actual_length == 0)
1117 pipe_setting(r8a66597, td);
1118 if (td->pipe->pipetre)
1119 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1120 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001121 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001122
1123 fifo_change_from_pipe(r8a66597, td->pipe);
1124 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1125 if (unlikely((tmp & FRDY) == 0))
1126 pipe_irq_enable(r8a66597, urb, td->pipenum);
1127 else
1128 packet_write(r8a66597, td->pipenum);
1129 pipe_start(r8a66597, td->pipe);
1130}
1131
1132/* this function must be called with interrupt disabled */
1133static void prepare_status_packet(struct r8a66597 *r8a66597,
1134 struct r8a66597_td *td)
1135{
1136 struct urb *urb = td->urb;
1137
1138 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001139 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001140
1141 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1142 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1143 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1144 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001145 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +09001146 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001147 enable_irq_empty(r8a66597, 0);
1148 } else {
1149 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1150 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1151 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1152 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001153 enable_irq_ready(r8a66597, 0);
1154 }
1155 enable_irq_nrdy(r8a66597, 0);
1156 pipe_start(r8a66597, td->pipe);
1157}
1158
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001159static int is_set_address(unsigned char *setup_packet)
1160{
1161 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1162 setup_packet[1] == USB_REQ_SET_ADDRESS)
1163 return 1;
1164 else
1165 return 0;
1166}
1167
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001168/* this function must be called with interrupt disabled */
1169static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1170{
1171 BUG_ON(!td);
1172
1173 switch (td->type) {
1174 case USB_PID_SETUP:
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001175 if (is_set_address(td->urb->setup_packet)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001176 td->set_address = 1;
1177 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1178 td->urb);
1179 if (td->urb->setup_packet[2] == 0)
1180 return -EPIPE;
1181 }
1182 prepare_setup_packet(r8a66597, td);
1183 break;
1184 case USB_PID_IN:
1185 prepare_packet_read(r8a66597, td);
1186 break;
1187 case USB_PID_OUT:
1188 prepare_packet_write(r8a66597, td);
1189 break;
1190 case USB_PID_ACK:
1191 prepare_status_packet(r8a66597, td);
1192 break;
1193 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001194 printk(KERN_ERR "r8a66597: invalid type.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001195 break;
1196 }
1197
1198 return 0;
1199}
1200
1201static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1202{
1203 if (usb_pipeisoc(urb->pipe)) {
1204 if (urb->number_of_packets == td->iso_cnt)
1205 return 1;
1206 }
1207
1208 /* control or bulk or interrupt */
1209 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1210 (td->short_packet) || (td->zero_packet))
1211 return 1;
1212
1213 return 0;
1214}
1215
1216/* this function must be called with interrupt disabled */
1217static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1218{
1219 unsigned long time;
1220
1221 BUG_ON(!td);
1222
1223 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1224 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1225 r8a66597->timeout_map |= 1 << td->pipenum;
1226 switch (usb_pipetype(td->urb->pipe)) {
1227 case PIPE_INTERRUPT:
1228 case PIPE_ISOCHRONOUS:
1229 time = 30;
1230 break;
1231 default:
1232 time = 300;
1233 break;
1234 }
1235
1236 mod_timer(&r8a66597->td_timer[td->pipenum],
1237 jiffies + msecs_to_jiffies(time));
1238 }
1239}
1240
1241/* this function must be called with interrupt disabled */
Alan Sterndfd1e532007-08-21 15:36:52 -04001242static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
Alan Stern888fda42007-08-24 15:41:18 -04001243 u16 pipenum, struct urb *urb, int status)
Alan Sterndfd1e532007-08-21 15:36:52 -04001244__releases(r8a66597->lock) __acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001245{
1246 int restart = 0;
1247 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1248
1249 r8a66597->timeout_map &= ~(1 << pipenum);
1250
1251 if (likely(td)) {
Alan Stern888fda42007-08-24 15:41:18 -04001252 if (td->set_address && (status != 0 || urb->unlinked))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001253 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1254
1255 pipe_toggle_save(r8a66597, td->pipe, urb);
1256 list_del(&td->queue);
1257 kfree(td);
1258 }
1259
1260 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1261 restart = 1;
1262
1263 if (likely(urb)) {
1264 if (usb_pipeisoc(urb->pipe))
1265 urb->start_frame = r8a66597_get_frame(hcd);
1266
Alan Sterne9df41c2007-08-08 11:48:02 -04001267 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001268 spin_unlock(&r8a66597->lock);
Alan Stern4a000272007-08-24 15:42:24 -04001269 usb_hcd_giveback_urb(hcd, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001270 spin_lock(&r8a66597->lock);
1271 }
1272
1273 if (restart) {
1274 td = r8a66597_get_td(r8a66597, pipenum);
1275 if (unlikely(!td))
1276 return;
1277
1278 start_transfer(r8a66597, td);
1279 set_td_timer(r8a66597, td);
1280 }
1281}
1282
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001283static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1284{
1285 u16 tmp;
1286 int rcv_len, bufsize, urb_len, size;
1287 u16 *buf;
1288 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1289 struct urb *urb;
1290 int finish = 0;
Alan Sterndfd1e532007-08-21 15:36:52 -04001291 int status = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001292
1293 if (unlikely(!td))
1294 return;
1295 urb = td->urb;
1296
1297 fifo_change_from_pipe(r8a66597, td->pipe);
1298 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1299 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001300 pipe_stop(r8a66597, td->pipe);
1301 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001302 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001303 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001304 return;
1305 }
1306
1307 /* prepare parameters */
1308 rcv_len = tmp & DTLN;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001309 if (usb_pipeisoc(urb->pipe)) {
1310 buf = (u16 *)(urb->transfer_buffer +
1311 urb->iso_frame_desc[td->iso_cnt].offset);
1312 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1313 } else {
1314 buf = (void *)urb->transfer_buffer + urb->actual_length;
1315 urb_len = urb->transfer_buffer_length - urb->actual_length;
1316 }
Alan Sterndfd1e532007-08-21 15:36:52 -04001317 bufsize = min(urb_len, (int) td->maxpacket);
1318 if (rcv_len <= bufsize) {
1319 size = rcv_len;
1320 } else {
1321 size = bufsize;
1322 status = -EOVERFLOW;
1323 finish = 1;
1324 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001325
1326 /* update parameters */
1327 urb->actual_length += size;
1328 if (rcv_len == 0)
1329 td->zero_packet = 1;
Alan Sterndfd1e532007-08-21 15:36:52 -04001330 if (rcv_len < bufsize) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001331 td->short_packet = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001332 }
1333 if (usb_pipeisoc(urb->pipe)) {
1334 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
Alan Sterndfd1e532007-08-21 15:36:52 -04001335 urb->iso_frame_desc[td->iso_cnt].status = status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001336 td->iso_cnt++;
Alan Sterndfd1e532007-08-21 15:36:52 -04001337 finish = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001338 }
1339
1340 /* check transfer finish */
Alan Sternb0d9efb2007-08-21 15:39:21 -04001341 if (finish || check_transfer_finish(td, urb)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001342 pipe_stop(r8a66597, td->pipe);
1343 pipe_irq_disable(r8a66597, pipenum);
1344 finish = 1;
1345 }
1346
1347 /* read fifo */
1348 if (urb->transfer_buffer) {
1349 if (size == 0)
1350 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1351 else
1352 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1353 buf, size);
1354 }
1355
Alan Stern888fda42007-08-24 15:41:18 -04001356 if (finish && pipenum != 0)
1357 finish_request(r8a66597, td, pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001358}
1359
1360static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1361{
1362 u16 tmp;
1363 int bufsize, size;
1364 u16 *buf;
1365 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1366 struct urb *urb;
1367
1368 if (unlikely(!td))
1369 return;
1370 urb = td->urb;
1371
1372 fifo_change_from_pipe(r8a66597, td->pipe);
1373 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1374 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001375 pipe_stop(r8a66597, td->pipe);
1376 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001377 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001378 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001379 return;
1380 }
1381
1382 /* prepare parameters */
1383 bufsize = td->maxpacket;
1384 if (usb_pipeisoc(urb->pipe)) {
1385 buf = (u16 *)(urb->transfer_buffer +
1386 urb->iso_frame_desc[td->iso_cnt].offset);
1387 size = min(bufsize,
1388 (int)urb->iso_frame_desc[td->iso_cnt].length);
1389 } else {
1390 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
1391 size = min((int)bufsize,
1392 urb->transfer_buffer_length - urb->actual_length);
1393 }
1394
1395 /* write fifo */
1396 if (pipenum > 0)
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001397 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001398 if (urb->transfer_buffer) {
1399 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1400 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1401 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1402 }
1403
1404 /* update parameters */
1405 urb->actual_length += size;
1406 if (usb_pipeisoc(urb->pipe)) {
1407 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1408 urb->iso_frame_desc[td->iso_cnt].status = 0;
1409 td->iso_cnt++;
1410 }
1411
1412 /* check transfer finish */
1413 if (check_transfer_finish(td, urb)) {
1414 disable_irq_ready(r8a66597, pipenum);
1415 enable_irq_empty(r8a66597, pipenum);
1416 if (!usb_pipeisoc(urb->pipe))
1417 enable_irq_nrdy(r8a66597, pipenum);
1418 } else
1419 pipe_irq_enable(r8a66597, urb, pipenum);
1420}
1421
1422
Alan Stern888fda42007-08-24 15:41:18 -04001423static void check_next_phase(struct r8a66597 *r8a66597, int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001424{
1425 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1426 struct urb *urb;
1427 u8 finish = 0;
1428
1429 if (unlikely(!td))
1430 return;
1431 urb = td->urb;
1432
1433 switch (td->type) {
1434 case USB_PID_IN:
1435 case USB_PID_OUT:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001436 if (check_transfer_finish(td, urb))
1437 td->type = USB_PID_ACK;
1438 break;
1439 case USB_PID_SETUP:
Alan Sterneb231052007-08-21 15:40:36 -04001440 if (urb->transfer_buffer_length == urb->actual_length)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001441 td->type = USB_PID_ACK;
Alan Sterneb231052007-08-21 15:40:36 -04001442 else if (usb_pipeout(urb->pipe))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001443 td->type = USB_PID_OUT;
1444 else
1445 td->type = USB_PID_IN;
1446 break;
1447 case USB_PID_ACK:
1448 finish = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001449 break;
1450 }
1451
Alan Stern888fda42007-08-24 15:41:18 -04001452 if (finish || status != 0 || urb->unlinked)
1453 finish_request(r8a66597, td, 0, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001454 else
1455 start_transfer(r8a66597, td);
1456}
1457
Alan Stern888fda42007-08-24 15:41:18 -04001458static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001459{
1460 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1461
Alan Stern888fda42007-08-24 15:41:18 -04001462 if (td) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001463 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1464
1465 if (pid == PID_NAK)
Alan Stern888fda42007-08-24 15:41:18 -04001466 return -ECONNRESET;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001467 else
Alan Stern888fda42007-08-24 15:41:18 -04001468 return -EPIPE;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001469 }
Alan Stern888fda42007-08-24 15:41:18 -04001470 return 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001471}
1472
1473static void irq_pipe_ready(struct r8a66597 *r8a66597)
1474{
1475 u16 check;
1476 u16 pipenum;
1477 u16 mask;
1478 struct r8a66597_td *td;
1479
1480 mask = r8a66597_read(r8a66597, BRDYSTS)
1481 & r8a66597_read(r8a66597, BRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001482 r8a66597_write(r8a66597, ~mask, BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001483 if (mask & BRDY0) {
1484 td = r8a66597_get_td(r8a66597, 0);
1485 if (td && td->type == USB_PID_IN)
1486 packet_read(r8a66597, 0);
1487 else
1488 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001489 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001490 }
1491
1492 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1493 check = 1 << pipenum;
1494 if (mask & check) {
1495 td = r8a66597_get_td(r8a66597, pipenum);
1496 if (unlikely(!td))
1497 continue;
1498
1499 if (td->type == USB_PID_IN)
1500 packet_read(r8a66597, pipenum);
1501 else if (td->type == USB_PID_OUT)
1502 packet_write(r8a66597, pipenum);
1503 }
1504 }
1505}
1506
1507static void irq_pipe_empty(struct r8a66597 *r8a66597)
1508{
1509 u16 tmp;
1510 u16 check;
1511 u16 pipenum;
1512 u16 mask;
1513 struct r8a66597_td *td;
1514
1515 mask = r8a66597_read(r8a66597, BEMPSTS)
1516 & r8a66597_read(r8a66597, BEMPENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001517 r8a66597_write(r8a66597, ~mask, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001518 if (mask & BEMP0) {
1519 cfifo_change(r8a66597, 0);
1520 td = r8a66597_get_td(r8a66597, 0);
1521 if (td && td->type != USB_PID_OUT)
1522 disable_irq_empty(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001523 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001524 }
1525
1526 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1527 check = 1 << pipenum;
1528 if (mask & check) {
1529 struct r8a66597_td *td;
1530 td = r8a66597_get_td(r8a66597, pipenum);
1531 if (unlikely(!td))
1532 continue;
1533
1534 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1535 if ((tmp & INBUFM) == 0) {
1536 disable_irq_empty(r8a66597, pipenum);
1537 pipe_irq_disable(r8a66597, pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001538 finish_request(r8a66597, td, pipenum, td->urb,
1539 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001540 }
1541 }
1542 }
1543}
1544
1545static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1546{
1547 u16 check;
1548 u16 pipenum;
1549 u16 mask;
Alan Stern888fda42007-08-24 15:41:18 -04001550 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001551
1552 mask = r8a66597_read(r8a66597, NRDYSTS)
1553 & r8a66597_read(r8a66597, NRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001554 r8a66597_write(r8a66597, ~mask, NRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001555 if (mask & NRDY0) {
1556 cfifo_change(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001557 status = get_urb_error(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001558 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001559 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001560 }
1561
1562 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1563 check = 1 << pipenum;
1564 if (mask & check) {
1565 struct r8a66597_td *td;
1566 td = r8a66597_get_td(r8a66597, pipenum);
1567 if (unlikely(!td))
1568 continue;
1569
Alan Stern888fda42007-08-24 15:41:18 -04001570 status = get_urb_error(r8a66597, pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001571 pipe_irq_disable(r8a66597, pipenum);
1572 pipe_stop(r8a66597, td->pipe);
Alan Stern888fda42007-08-24 15:41:18 -04001573 finish_request(r8a66597, td, pipenum, td->urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001574 }
1575 }
1576}
1577
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001578static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1579{
1580 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1581 u16 intsts0, intsts1, intsts2;
1582 u16 intenb0, intenb1, intenb2;
1583 u16 mask0, mask1, mask2;
Alan Stern888fda42007-08-24 15:41:18 -04001584 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001585
1586 spin_lock(&r8a66597->lock);
1587
1588 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1589 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1590 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1591 intenb0 = r8a66597_read(r8a66597, INTENB0);
1592 intenb1 = r8a66597_read(r8a66597, INTENB1);
1593 intenb2 = r8a66597_read(r8a66597, INTENB2);
1594
1595 mask2 = intsts2 & intenb2;
1596 mask1 = intsts1 & intenb1;
1597 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1598 if (mask2) {
1599 if (mask2 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001600 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001601 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1602
1603 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001604 start_root_hub_sampling(r8a66597, 1, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001605 }
1606 if (mask2 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001607 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001608 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1609 r8a66597_usb_disconnect(r8a66597, 1);
1610 }
1611 }
1612
1613 if (mask1) {
1614 if (mask1 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001615 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001616 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1617
1618 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001619 start_root_hub_sampling(r8a66597, 0, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001620 }
1621 if (mask1 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001622 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001623 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1624 r8a66597_usb_disconnect(r8a66597, 0);
1625 }
1626 if (mask1 & SIGN) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001627 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001628 status = get_urb_error(r8a66597, 0);
1629 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001630 }
1631 if (mask1 & SACK) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001632 r8a66597_write(r8a66597, ~SACK, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001633 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001634 }
1635 }
1636 if (mask0) {
1637 if (mask0 & BRDY)
1638 irq_pipe_ready(r8a66597);
1639 if (mask0 & BEMP)
1640 irq_pipe_empty(r8a66597);
1641 if (mask0 & NRDY)
1642 irq_pipe_nrdy(r8a66597);
1643 }
1644
1645 spin_unlock(&r8a66597->lock);
1646 return IRQ_HANDLED;
1647}
1648
1649/* this function must be called with interrupt disabled */
1650static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1651{
1652 u16 tmp;
1653 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1654
1655 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1656 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1657
1658 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1659 if ((tmp & USBRST) == USBRST) {
1660 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1661 dvstctr_reg);
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001662 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001663 } else
1664 r8a66597_usb_connect(r8a66597, port);
1665 }
1666
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001667 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1668 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1669 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1670 }
1671
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001672 if (rh->scount > 0) {
1673 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1674 if (tmp == rh->old_syssts) {
1675 rh->scount--;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001676 if (rh->scount == 0)
1677 r8a66597_check_syssts(r8a66597, port, tmp);
1678 else
1679 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001680 } else {
1681 rh->scount = R8A66597_MAX_SAMPLING;
1682 rh->old_syssts = tmp;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001683 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001684 }
1685 }
1686}
1687
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001688static void r8a66597_interval_timer(unsigned long _r8a66597)
1689{
1690 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1691 unsigned long flags;
1692 u16 pipenum;
1693 struct r8a66597_td *td;
1694
1695 spin_lock_irqsave(&r8a66597->lock, flags);
1696
1697 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1698 if (!(r8a66597->interval_map & (1 << pipenum)))
1699 continue;
1700 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1701 continue;
1702
1703 td = r8a66597_get_td(r8a66597, pipenum);
1704 if (td)
1705 start_transfer(r8a66597, td);
1706 }
1707
1708 spin_unlock_irqrestore(&r8a66597->lock, flags);
1709}
1710
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001711static void r8a66597_td_timer(unsigned long _r8a66597)
1712{
1713 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1714 unsigned long flags;
1715 u16 pipenum;
1716 struct r8a66597_td *td, *new_td = NULL;
1717 struct r8a66597_pipe *pipe;
1718
1719 spin_lock_irqsave(&r8a66597->lock, flags);
1720 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1721 if (!(r8a66597->timeout_map & (1 << pipenum)))
1722 continue;
1723 if (timer_pending(&r8a66597->td_timer[pipenum]))
1724 continue;
1725
1726 td = r8a66597_get_td(r8a66597, pipenum);
1727 if (!td) {
1728 r8a66597->timeout_map &= ~(1 << pipenum);
1729 continue;
1730 }
1731
1732 if (td->urb->actual_length) {
1733 set_td_timer(r8a66597, td);
1734 break;
1735 }
1736
1737 pipe = td->pipe;
1738 pipe_stop(r8a66597, pipe);
1739
1740 new_td = td;
1741 do {
1742 list_move_tail(&new_td->queue,
1743 &r8a66597->pipe_queue[pipenum]);
1744 new_td = r8a66597_get_td(r8a66597, pipenum);
1745 if (!new_td) {
1746 new_td = td;
1747 break;
1748 }
1749 } while (td != new_td && td->address == new_td->address);
1750
1751 start_transfer(r8a66597, new_td);
1752
1753 if (td == new_td)
1754 r8a66597->timeout_map &= ~(1 << pipenum);
1755 else
1756 set_td_timer(r8a66597, new_td);
1757 break;
1758 }
1759 spin_unlock_irqrestore(&r8a66597->lock, flags);
1760}
1761
1762static void r8a66597_timer(unsigned long _r8a66597)
1763{
1764 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1765 unsigned long flags;
1766
1767 spin_lock_irqsave(&r8a66597->lock, flags);
1768
1769 r8a66597_root_hub_control(r8a66597, 0);
1770 r8a66597_root_hub_control(r8a66597, 1);
1771
1772 spin_unlock_irqrestore(&r8a66597->lock, flags);
1773}
1774
1775static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1776{
1777 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1778
1779 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1780 (urb->dev->state == USB_STATE_CONFIGURED))
1781 return 1;
1782 else
1783 return 0;
1784}
1785
1786static int r8a66597_start(struct usb_hcd *hcd)
1787{
1788 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001789
1790 hcd->state = HC_STATE_RUNNING;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001791 return enable_controller(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001792}
1793
1794static void r8a66597_stop(struct usb_hcd *hcd)
1795{
1796 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1797
1798 disable_controller(r8a66597);
1799}
1800
1801static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1802{
1803 unsigned int usb_address = usb_pipedevice(urb->pipe);
1804 u16 root_port, hub_port;
1805
1806 if (usb_address == 0) {
1807 get_port_number(urb->dev->devpath,
1808 &root_port, &hub_port);
1809 set_devadd_reg(r8a66597, 0,
1810 get_r8a66597_usb_speed(urb->dev->speed),
1811 get_parent_r8a66597_address(r8a66597, urb->dev),
1812 hub_port, root_port);
1813 }
1814}
1815
1816static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1817 struct urb *urb,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001818 struct usb_host_endpoint *hep)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001819{
1820 struct r8a66597_td *td;
1821 u16 pipenum;
1822
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001823 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001824 if (td == NULL)
1825 return NULL;
1826
1827 pipenum = r8a66597_get_pipenum(urb, hep);
1828 td->pipenum = pipenum;
1829 td->pipe = hep->hcpriv;
1830 td->urb = urb;
1831 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1832 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1833 !usb_pipein(urb->pipe));
1834 if (usb_pipecontrol(urb->pipe))
1835 td->type = USB_PID_SETUP;
1836 else if (usb_pipein(urb->pipe))
1837 td->type = USB_PID_IN;
1838 else
1839 td->type = USB_PID_OUT;
1840 INIT_LIST_HEAD(&td->queue);
1841
1842 return td;
1843}
1844
1845static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001846 struct urb *urb,
1847 gfp_t mem_flags)
1848{
Alan Sterne9df41c2007-08-08 11:48:02 -04001849 struct usb_host_endpoint *hep = urb->ep;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001850 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1851 struct r8a66597_td *td = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -04001852 int ret, request = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001853 unsigned long flags;
1854
1855 spin_lock_irqsave(&r8a66597->lock, flags);
1856 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1857 ret = -ENODEV;
Alan Sterne9df41c2007-08-08 11:48:02 -04001858 goto error_not_linked;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001859 }
1860
Alan Sterne9df41c2007-08-08 11:48:02 -04001861 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1862 if (ret)
1863 goto error_not_linked;
1864
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001865 if (!hep->hcpriv) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001866 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1867 GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001868 if (!hep->hcpriv) {
1869 ret = -ENOMEM;
1870 goto error;
1871 }
1872 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1873 if (usb_pipeendpoint(urb->pipe))
1874 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1875 }
1876
1877 if (unlikely(check_pipe_config(r8a66597, urb)))
1878 init_pipe_config(r8a66597, urb);
1879
1880 set_address_zero(r8a66597, urb);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001881 td = r8a66597_make_td(r8a66597, urb, hep);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001882 if (td == NULL) {
1883 ret = -ENOMEM;
1884 goto error;
1885 }
1886 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1887 request = 1;
1888 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001889 urb->hcpriv = td;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001890
1891 if (request) {
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001892 if (td->pipe->info.timer_interval) {
1893 r8a66597->interval_map |= 1 << td->pipenum;
1894 mod_timer(&r8a66597->interval_timer[td->pipenum],
1895 jiffies + msecs_to_jiffies(
1896 td->pipe->info.timer_interval));
1897 } else {
1898 ret = start_transfer(r8a66597, td);
1899 if (ret < 0) {
1900 list_del(&td->queue);
1901 kfree(td);
1902 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001903 }
1904 } else
1905 set_td_timer(r8a66597, td);
1906
1907error:
Alan Sterne9df41c2007-08-08 11:48:02 -04001908 if (ret)
1909 usb_hcd_unlink_urb_from_ep(hcd, urb);
1910error_not_linked:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001911 spin_unlock_irqrestore(&r8a66597->lock, flags);
1912 return ret;
1913}
1914
Alan Sterne9df41c2007-08-08 11:48:02 -04001915static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1916 int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001917{
1918 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1919 struct r8a66597_td *td;
1920 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001921 int rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001922
1923 spin_lock_irqsave(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001924 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1925 if (rc)
1926 goto done;
1927
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001928 if (urb->hcpriv) {
1929 td = urb->hcpriv;
1930 pipe_stop(r8a66597, td->pipe);
1931 pipe_irq_disable(r8a66597, td->pipenum);
1932 disable_irq_empty(r8a66597, td->pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001933 finish_request(r8a66597, td, td->pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001934 }
Alan Sterne9df41c2007-08-08 11:48:02 -04001935 done:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001936 spin_unlock_irqrestore(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001937 return rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001938}
1939
1940static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1941 struct usb_host_endpoint *hep)
1942{
1943 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1944 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1945 struct r8a66597_td *td;
1946 struct urb *urb = NULL;
1947 u16 pipenum;
1948 unsigned long flags;
1949
1950 if (pipe == NULL)
1951 return;
1952 pipenum = pipe->info.pipenum;
1953
1954 if (pipenum == 0) {
1955 kfree(hep->hcpriv);
1956 hep->hcpriv = NULL;
1957 return;
1958 }
1959
1960 spin_lock_irqsave(&r8a66597->lock, flags);
1961 pipe_stop(r8a66597, pipe);
1962 pipe_irq_disable(r8a66597, pipenum);
1963 disable_irq_empty(r8a66597, pipenum);
1964 td = r8a66597_get_td(r8a66597, pipenum);
1965 if (td)
1966 urb = td->urb;
Alan Stern888fda42007-08-24 15:41:18 -04001967 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001968 kfree(hep->hcpriv);
1969 hep->hcpriv = NULL;
1970 spin_unlock_irqrestore(&r8a66597->lock, flags);
1971}
1972
1973static int r8a66597_get_frame(struct usb_hcd *hcd)
1974{
1975 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1976 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1977}
1978
1979static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
1980{
1981 int chix;
1982
1983 if (udev->state == USB_STATE_CONFIGURED &&
1984 udev->parent && udev->parent->devnum > 1 &&
1985 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
1986 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
1987
1988 for (chix = 0; chix < udev->maxchild; chix++) {
1989 struct usb_device *childdev = udev->children[chix];
1990
1991 if (childdev)
1992 collect_usb_address_map(childdev, map);
1993 }
1994}
1995
1996/* this function must be called with interrupt disabled */
1997static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
1998 int addr)
1999{
2000 struct r8a66597_device *dev;
2001 struct list_head *list = &r8a66597->child_device;
2002
2003 list_for_each_entry(dev, list, device_list) {
2004 if (!dev)
2005 continue;
2006 if (dev->usb_address != addr)
2007 continue;
2008
2009 return dev;
2010 }
2011
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002012 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002013 return NULL;
2014}
2015
2016static void update_usb_address_map(struct r8a66597 *r8a66597,
2017 struct usb_device *root_hub,
2018 unsigned long *map)
2019{
2020 int i, j, addr;
2021 unsigned long diff;
2022 unsigned long flags;
2023
2024 for (i = 0; i < 4; i++) {
2025 diff = r8a66597->child_connect_map[i] ^ map[i];
2026 if (!diff)
2027 continue;
2028
2029 for (j = 0; j < 32; j++) {
2030 if (!(diff & (1 << j)))
2031 continue;
2032
2033 addr = i * 32 + j;
2034 if (map[i] & (1 << j))
2035 set_child_connect_map(r8a66597, addr);
2036 else {
2037 struct r8a66597_device *dev;
2038
2039 spin_lock_irqsave(&r8a66597->lock, flags);
2040 dev = get_r8a66597_device(r8a66597, addr);
2041 disable_r8a66597_pipe_all(r8a66597, dev);
2042 free_usb_address(r8a66597, dev);
2043 put_child_connect_map(r8a66597, addr);
2044 spin_unlock_irqrestore(&r8a66597->lock, flags);
2045 }
2046 }
2047 }
2048}
2049
2050static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2051 struct usb_hcd *hcd)
2052{
2053 struct usb_bus *bus;
2054 unsigned long now_map[4];
2055
2056 memset(now_map, 0, sizeof(now_map));
2057
2058 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2059 if (!bus->root_hub)
2060 continue;
2061
2062 if (bus->busnum != hcd->self.busnum)
2063 continue;
2064
2065 collect_usb_address_map(bus->root_hub, now_map);
2066 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2067 }
2068}
2069
2070static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2071{
2072 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2073 unsigned long flags;
2074 int i;
2075
2076 r8a66597_check_detect_child(r8a66597, hcd);
2077
2078 spin_lock_irqsave(&r8a66597->lock, flags);
2079
2080 *buf = 0; /* initialize (no change) */
2081
2082 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
2083 if (r8a66597->root_hub[i].port & 0xffff0000)
2084 *buf |= 1 << (i + 1);
2085 }
2086
2087 spin_unlock_irqrestore(&r8a66597->lock, flags);
2088
2089 return (*buf != 0);
2090}
2091
2092static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2093 struct usb_hub_descriptor *desc)
2094{
2095 desc->bDescriptorType = 0x29;
2096 desc->bHubContrCurrent = 0;
2097 desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
2098 desc->bDescLength = 9;
2099 desc->bPwrOn2PwrGood = 0;
2100 desc->wHubCharacteristics = cpu_to_le16(0x0011);
2101 desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
2102 desc->bitmap[1] = ~0;
2103}
2104
2105static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2106 u16 wIndex, char *buf, u16 wLength)
2107{
2108 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2109 int ret;
2110 int port = (wIndex & 0x00FF) - 1;
2111 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2112 unsigned long flags;
2113
2114 ret = 0;
2115
2116 spin_lock_irqsave(&r8a66597->lock, flags);
2117 switch (typeReq) {
2118 case ClearHubFeature:
2119 case SetHubFeature:
2120 switch (wValue) {
2121 case C_HUB_OVER_CURRENT:
2122 case C_HUB_LOCAL_POWER:
2123 break;
2124 default:
2125 goto error;
2126 }
2127 break;
2128 case ClearPortFeature:
2129 if (wIndex > R8A66597_MAX_ROOT_HUB)
2130 goto error;
2131 if (wLength != 0)
2132 goto error;
2133
2134 switch (wValue) {
2135 case USB_PORT_FEAT_ENABLE:
2136 rh->port &= (1 << USB_PORT_FEAT_POWER);
2137 break;
2138 case USB_PORT_FEAT_SUSPEND:
2139 break;
2140 case USB_PORT_FEAT_POWER:
2141 r8a66597_port_power(r8a66597, port, 0);
2142 break;
2143 case USB_PORT_FEAT_C_ENABLE:
2144 case USB_PORT_FEAT_C_SUSPEND:
2145 case USB_PORT_FEAT_C_CONNECTION:
2146 case USB_PORT_FEAT_C_OVER_CURRENT:
2147 case USB_PORT_FEAT_C_RESET:
2148 break;
2149 default:
2150 goto error;
2151 }
2152 rh->port &= ~(1 << wValue);
2153 break;
2154 case GetHubDescriptor:
2155 r8a66597_hub_descriptor(r8a66597,
2156 (struct usb_hub_descriptor *)buf);
2157 break;
2158 case GetHubStatus:
2159 *buf = 0x00;
2160 break;
2161 case GetPortStatus:
2162 if (wIndex > R8A66597_MAX_ROOT_HUB)
2163 goto error;
Al Virofd05e722008-04-28 07:00:16 +01002164 *(__le32 *)buf = cpu_to_le32(rh->port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002165 break;
2166 case SetPortFeature:
2167 if (wIndex > R8A66597_MAX_ROOT_HUB)
2168 goto error;
2169 if (wLength != 0)
2170 goto error;
2171
2172 switch (wValue) {
2173 case USB_PORT_FEAT_SUSPEND:
2174 break;
2175 case USB_PORT_FEAT_POWER:
2176 r8a66597_port_power(r8a66597, port, 1);
2177 rh->port |= (1 << USB_PORT_FEAT_POWER);
2178 break;
2179 case USB_PORT_FEAT_RESET: {
2180 struct r8a66597_device *dev = rh->dev;
2181
2182 rh->port |= (1 << USB_PORT_FEAT_RESET);
2183
2184 disable_r8a66597_pipe_all(r8a66597, dev);
2185 free_usb_address(r8a66597, dev);
2186
2187 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2188 get_dvstctr_reg(port));
2189 mod_timer(&r8a66597->rh_timer,
2190 jiffies + msecs_to_jiffies(50));
2191 }
2192 break;
2193 default:
2194 goto error;
2195 }
2196 rh->port |= 1 << wValue;
2197 break;
2198 default:
2199error:
2200 ret = -EPIPE;
2201 break;
2202 }
2203
2204 spin_unlock_irqrestore(&r8a66597->lock, flags);
2205 return ret;
2206}
2207
2208static struct hc_driver r8a66597_hc_driver = {
2209 .description = hcd_name,
2210 .hcd_priv_size = sizeof(struct r8a66597),
2211 .irq = r8a66597_irq,
2212
2213 /*
2214 * generic hardware linkage
2215 */
2216 .flags = HCD_USB2,
2217
2218 .start = r8a66597_start,
2219 .stop = r8a66597_stop,
2220
2221 /*
2222 * managing i/o requests and associated device resources
2223 */
2224 .urb_enqueue = r8a66597_urb_enqueue,
2225 .urb_dequeue = r8a66597_urb_dequeue,
2226 .endpoint_disable = r8a66597_endpoint_disable,
2227
2228 /*
2229 * periodic schedule support
2230 */
2231 .get_frame_number = r8a66597_get_frame,
2232
2233 /*
2234 * root hub support
2235 */
2236 .hub_status_data = r8a66597_hub_status_data,
2237 .hub_control = r8a66597_hub_control,
2238};
2239
2240#if defined(CONFIG_PM)
2241static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
2242{
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002243 return 0;
2244}
2245
2246static int r8a66597_resume(struct platform_device *pdev)
2247{
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002248 return 0;
2249}
2250#else /* if defined(CONFIG_PM) */
2251#define r8a66597_suspend NULL
2252#define r8a66597_resume NULL
2253#endif
2254
2255static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2256{
2257 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2258 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2259
2260 del_timer_sync(&r8a66597->rh_timer);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002261 usb_remove_hcd(hcd);
Yoshihiro Shimodaca0677a2007-10-05 15:53:12 +09002262 iounmap((void *)r8a66597->reg);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002263 usb_put_hcd(hcd);
2264 return 0;
2265}
2266
2267#define resource_len(r) (((r)->end - (r)->start) + 1)
2268static int __init r8a66597_probe(struct platform_device *pdev)
2269{
Marc Zyngier27140212008-08-18 13:08:42 +02002270 struct resource *res = NULL, *ires;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002271 int irq = -1;
2272 void __iomem *reg = NULL;
2273 struct usb_hcd *hcd = NULL;
2274 struct r8a66597 *r8a66597;
2275 int ret = 0;
2276 int i;
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002277 unsigned long irq_trigger;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002278
2279 if (pdev->dev.dma_mask) {
2280 ret = -EINVAL;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002281 dev_err(&pdev->dev, "dma not supported\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002282 goto clean_up;
2283 }
2284
2285 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2286 (char *)hcd_name);
2287 if (!res) {
2288 ret = -ENODEV;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002289 dev_err(&pdev->dev, "platform_get_resource_byname error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002290 goto clean_up;
2291 }
2292
Marc Zyngier27140212008-08-18 13:08:42 +02002293 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2294 if (!ires) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002295 ret = -ENODEV;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002296 dev_err(&pdev->dev,
2297 "platform_get_resource IORESOURCE_IRQ error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002298 goto clean_up;
2299 }
2300
Marc Zyngier27140212008-08-18 13:08:42 +02002301 irq = ires->start;
2302 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2303
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002304 reg = ioremap(res->start, resource_len(res));
2305 if (reg == NULL) {
2306 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002307 dev_err(&pdev->dev, "ioremap error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002308 goto clean_up;
2309 }
2310
2311 /* initialize hcd */
2312 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2313 if (!hcd) {
2314 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002315 dev_err(&pdev->dev, "Failed to create hcd\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002316 goto clean_up;
2317 }
2318 r8a66597 = hcd_to_r8a66597(hcd);
2319 memset(r8a66597, 0, sizeof(struct r8a66597));
2320 dev_set_drvdata(&pdev->dev, r8a66597);
2321
2322 spin_lock_init(&r8a66597->lock);
2323 init_timer(&r8a66597->rh_timer);
2324 r8a66597->rh_timer.function = r8a66597_timer;
2325 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2326 r8a66597->reg = (unsigned long)reg;
2327
2328 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2329 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2330 init_timer(&r8a66597->td_timer[i]);
2331 r8a66597->td_timer[i].function = r8a66597_td_timer;
2332 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09002333 setup_timer(&r8a66597->interval_timer[i],
2334 r8a66597_interval_timer,
2335 (unsigned long)r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002336 }
2337 INIT_LIST_HEAD(&r8a66597->child_device);
2338
2339 hcd->rsrc_start = res->start;
Marc Zyngier27140212008-08-18 13:08:42 +02002340
2341 /* irq_sense setting on cmdline takes precedence over resource
2342 * settings, so the introduction of irqflags in IRQ resourse
2343 * won't disturb existing setups */
2344 switch (irq_sense) {
2345 case INTL:
2346 irq_trigger = IRQF_TRIGGER_LOW;
2347 break;
2348 case 0:
2349 irq_trigger = IRQF_TRIGGER_FALLING;
2350 break;
2351 case 0xff:
2352 if (irq_trigger)
2353 irq_sense = (irq_trigger & IRQF_TRIGGER_LOW) ?
2354 INTL : 0;
2355 else {
2356 irq_sense = INTL;
2357 irq_trigger = IRQF_TRIGGER_LOW;
2358 }
2359 break;
2360 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002361 dev_err(&pdev->dev, "Unknown irq_sense value.\n");
Marc Zyngier27140212008-08-18 13:08:42 +02002362 }
2363
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002364 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002365 if (ret != 0) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002366 dev_err(&pdev->dev, "Failed to add hcd\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002367 goto clean_up;
2368 }
2369
2370 return 0;
2371
2372clean_up:
2373 if (reg)
2374 iounmap(reg);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002375
2376 return ret;
2377}
2378
2379static struct platform_driver r8a66597_driver = {
2380 .probe = r8a66597_probe,
2381 .remove = r8a66597_remove,
2382 .suspend = r8a66597_suspend,
2383 .resume = r8a66597_resume,
2384 .driver = {
2385 .name = (char *) hcd_name,
Kay Sieversf4fce612008-04-10 21:29:22 -07002386 .owner = THIS_MODULE,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002387 },
2388};
2389
2390static int __init r8a66597_init(void)
2391{
2392 if (usb_disabled())
2393 return -ENODEV;
2394
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -07002395 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2396 DRIVER_VERSION);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002397 return platform_driver_register(&r8a66597_driver);
2398}
2399module_init(r8a66597_init);
2400
2401static void __exit r8a66597_cleanup(void)
2402{
2403 platform_driver_unregister(&r8a66597_driver);
2404}
2405module_exit(r8a66597_cleanup);
2406