blob: e18f74946e6824a427bd5b3b0185ee79ffebcd35 [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
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +090094#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
Magnus Damm765786e2008-10-31 20:22:38 +090095#if defined(CONFIG_HAVE_CLK)
96 clk_enable(r8a66597->clk);
97#endif
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +090098 do {
99 r8a66597_write(r8a66597, SCKE, SYSCFG0);
100 tmp = r8a66597_read(r8a66597, SYSCFG0);
101 if (i++ > 1000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700102 printk(KERN_ERR "r8a66597: register access fail.\n");
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900103 return -ENXIO;
104 }
105 } while ((tmp & SCKE) != SCKE);
106 r8a66597_write(r8a66597, 0x04, 0x02);
107#else
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900108 do {
109 r8a66597_write(r8a66597, USBE, SYSCFG0);
110 tmp = r8a66597_read(r8a66597, SYSCFG0);
111 if (i++ > 1000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700112 printk(KERN_ERR "r8a66597: register access fail.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900113 return -ENXIO;
114 }
115 } while ((tmp & USBE) != USBE);
116 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +0900117 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata), XTAL,
118 SYSCFG0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900119
120 i = 0;
121 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
122 do {
123 msleep(1);
124 tmp = r8a66597_read(r8a66597, SYSCFG0);
125 if (i++ > 500) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700126 printk(KERN_ERR "r8a66597: register access fail.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900127 return -ENXIO;
128 }
129 } while ((tmp & SCKE) != SCKE);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900130#endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
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 Damm765786e2008-10-31 20:22:38 +0900139#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
140#if defined(CONFIG_HAVE_CLK)
141 clk_disable(r8a66597->clk);
142#endif
143#else
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900144 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
145 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
146 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
147#endif
148}
149
150static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
151{
152 u16 val;
153
154 val = port ? DRPD : DCFM | DRPD;
155 r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
156 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
157
158 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
159 r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
160 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
161}
162
163static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
164{
165 u16 val, tmp;
166
167 r8a66597_write(r8a66597, 0, get_intenb_reg(port));
168 r8a66597_write(r8a66597, 0, get_intsts_reg(port));
169
170 r8a66597_port_power(r8a66597, port, 0);
171
172 do {
173 tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
174 udelay(640);
175 } while (tmp == EDGESTS);
176
177 val = port ? DRPD : DCFM | DRPD;
178 r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
179 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
180}
181
182static int enable_controller(struct r8a66597 *r8a66597)
183{
184 int ret, port;
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +0900185 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
186 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
187 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900188
189 ret = r8a66597_clock_enable(r8a66597);
190 if (ret < 0)
191 return ret;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900192
193 r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900194 r8a66597_bset(r8a66597, USBE, SYSCFG0);
195
196 r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
197 r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
198 r8a66597_bset(r8a66597, BRDY0, BRDYENB);
199 r8a66597_bset(r8a66597, BEMP0, BEMPENB);
200
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900201 r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
202 r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
203 r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900204 r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
205
206 r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900207
208 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
209 r8a66597_enable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900210
211 return 0;
212}
213
214static void disable_controller(struct r8a66597 *r8a66597)
215{
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900216 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900217
218 r8a66597_write(r8a66597, 0, INTENB0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900219 r8a66597_write(r8a66597, 0, INTSTS0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900220
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900221 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
222 r8a66597_disable_port(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900223
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900224 r8a66597_clock_disable(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900225}
226
227static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
228 struct usb_device *udev)
229{
230 struct r8a66597_device *dev;
231
232 if (udev->parent && udev->parent->devnum != 1)
233 udev = udev->parent;
234
235 dev = dev_get_drvdata(&udev->dev);
236 if (dev)
237 return dev->address;
238 else
239 return 0;
240}
241
242static int is_child_device(char *devpath)
243{
244 return (devpath[2] ? 1 : 0);
245}
246
247static int is_hub_limit(char *devpath)
248{
249 return ((strlen(devpath) >= 4) ? 1 : 0);
250}
251
252static void get_port_number(char *devpath, u16 *root_port, u16 *hub_port)
253{
254 if (root_port) {
255 *root_port = (devpath[0] & 0x0F) - 1;
256 if (*root_port >= R8A66597_MAX_ROOT_HUB)
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700257 printk(KERN_ERR "r8a66597: Illegal root port number.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900258 }
259 if (hub_port)
260 *hub_port = devpath[2] & 0x0F;
261}
262
263static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
264{
265 u16 usbspd = 0;
266
267 switch (speed) {
268 case USB_SPEED_LOW:
269 usbspd = LSMODE;
270 break;
271 case USB_SPEED_FULL:
272 usbspd = FSMODE;
273 break;
274 case USB_SPEED_HIGH:
275 usbspd = HSMODE;
276 break;
277 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700278 printk(KERN_ERR "r8a66597: unknown speed\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900279 break;
280 }
281
282 return usbspd;
283}
284
285static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
286{
287 int idx;
288
289 idx = address / 32;
290 r8a66597->child_connect_map[idx] |= 1 << (address % 32);
291}
292
293static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
294{
295 int idx;
296
297 idx = address / 32;
298 r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
299}
300
301static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
302{
303 u16 pipenum = pipe->info.pipenum;
Ming Leife9b9032008-06-08 16:13:03 +0800304 const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
305 const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
306 const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900307
308 if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
309 dma_ch = R8A66597_PIPE_NO_DMA;
310
311 pipe->fifoaddr = fifoaddr[dma_ch];
312 pipe->fifosel = fifosel[dma_ch];
313 pipe->fifoctr = fifoctr[dma_ch];
314
315 if (pipenum == 0)
316 pipe->pipectr = DCPCTR;
317 else
318 pipe->pipectr = get_pipectr_addr(pipenum);
319
320 if (check_bulk_or_isoc(pipenum)) {
321 pipe->pipetre = get_pipetre_addr(pipenum);
322 pipe->pipetrn = get_pipetrn_addr(pipenum);
323 } else {
324 pipe->pipetre = 0;
325 pipe->pipetrn = 0;
326 }
327}
328
329static struct r8a66597_device *
330get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
331{
332 if (usb_pipedevice(urb->pipe) == 0)
333 return &r8a66597->device0;
334
335 return dev_get_drvdata(&urb->dev->dev);
336}
337
338static int make_r8a66597_device(struct r8a66597 *r8a66597,
339 struct urb *urb, u8 addr)
340{
341 struct r8a66597_device *dev;
342 int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
343
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900344 dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900345 if (dev == NULL)
346 return -ENOMEM;
347
348 dev_set_drvdata(&urb->dev->dev, dev);
349 dev->udev = urb->dev;
350 dev->address = addr;
351 dev->usb_address = usb_address;
352 dev->state = USB_STATE_ADDRESS;
353 dev->ep_in_toggle = 0;
354 dev->ep_out_toggle = 0;
355 INIT_LIST_HEAD(&dev->device_list);
356 list_add_tail(&dev->device_list, &r8a66597->child_device);
357
358 get_port_number(urb->dev->devpath, &dev->root_port, &dev->hub_port);
359 if (!is_child_device(urb->dev->devpath))
360 r8a66597->root_hub[dev->root_port].dev = dev;
361
362 set_devadd_reg(r8a66597, dev->address,
363 get_r8a66597_usb_speed(urb->dev->speed),
364 get_parent_r8a66597_address(r8a66597, urb->dev),
365 dev->hub_port, dev->root_port);
366
367 return 0;
368}
369
370/* this function must be called with interrupt disabled */
371static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
372{
373 u8 addr; /* R8A66597's address */
374 struct r8a66597_device *dev;
375
376 if (is_hub_limit(urb->dev->devpath)) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700377 dev_err(&urb->dev->dev, "External hub limit reached.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900378 return 0;
379 }
380
381 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
382 if (dev && dev->state >= USB_STATE_ADDRESS)
383 return dev->address;
384
385 for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
386 if (r8a66597->address_map & (1 << addr))
387 continue;
388
389 dbg("alloc_address: r8a66597_addr=%d", addr);
390 r8a66597->address_map |= 1 << addr;
391
392 if (make_r8a66597_device(r8a66597, urb, addr) < 0)
393 return 0;
394
395 return addr;
396 }
397
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700398 dev_err(&urb->dev->dev,
399 "cannot communicate with a USB device more than 10.(%x)\n",
400 r8a66597->address_map);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900401
402 return 0;
403}
404
405/* this function must be called with interrupt disabled */
406static void free_usb_address(struct r8a66597 *r8a66597,
407 struct r8a66597_device *dev)
408{
409 int port;
410
411 if (!dev)
412 return;
413
414 dbg("free_addr: addr=%d", dev->address);
415
416 dev->state = USB_STATE_DEFAULT;
417 r8a66597->address_map &= ~(1 << dev->address);
418 dev->address = 0;
419 dev_set_drvdata(&dev->udev->dev, NULL);
420 list_del(&dev->device_list);
421 kfree(dev);
422
423 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
424 if (r8a66597->root_hub[port].dev == dev) {
425 r8a66597->root_hub[port].dev = NULL;
426 break;
427 }
428 }
429}
430
431static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
432 u16 mask, u16 loop)
433{
434 u16 tmp;
435 int i = 0;
436
437 do {
438 tmp = r8a66597_read(r8a66597, reg);
439 if (i++ > 1000000) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700440 printk(KERN_ERR "r8a66597: register%lx, loop %x "
441 "is timeout\n", reg, loop);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900442 break;
443 }
444 ndelay(1);
445 } while ((tmp & mask) != loop);
446}
447
448/* this function must be called with interrupt disabled */
449static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
450{
451 u16 tmp;
452
453 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
454 if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
455 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
456 r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
457}
458
459/* this function must be called with interrupt disabled */
460static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
461{
462 u16 tmp;
463
464 tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
465 if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
466 r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
467 r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
468 r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
469}
470
471/* this function must be called with interrupt disabled */
472static void clear_all_buffer(struct r8a66597 *r8a66597,
473 struct r8a66597_pipe *pipe)
474{
475 u16 tmp;
476
477 if (!pipe || pipe->info.pipenum == 0)
478 return;
479
480 pipe_stop(r8a66597, pipe);
481 r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
482 tmp = r8a66597_read(r8a66597, pipe->pipectr);
483 tmp = r8a66597_read(r8a66597, pipe->pipectr);
484 tmp = r8a66597_read(r8a66597, pipe->pipectr);
485 r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
486}
487
488/* this function must be called with interrupt disabled */
489static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
490 struct r8a66597_pipe *pipe, int toggle)
491{
492 if (toggle)
493 r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
494 else
495 r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
496}
497
498/* this function must be called with interrupt disabled */
499static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
500{
501 r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL);
502 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
503}
504
505/* this function must be called with interrupt disabled */
506static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
507 struct r8a66597_pipe *pipe)
508{
509 cfifo_change(r8a66597, 0);
510 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D0FIFOSEL);
511 r8a66597_mdfy(r8a66597, MBW | 0, MBW | CURPIPE, D1FIFOSEL);
512
513 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum, MBW | CURPIPE,
514 pipe->fifosel);
515 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
516}
517
518static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
519{
520 struct r8a66597_pipe *pipe = hep->hcpriv;
521
522 if (usb_pipeendpoint(urb->pipe) == 0)
523 return 0;
524 else
525 return pipe->info.pipenum;
526}
527
528static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
529{
530 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
531
532 return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
533}
534
535static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
536 int urb_pipe)
537{
538 if (!dev)
539 return NULL;
540
541 return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
542}
543
544/* this function must be called with interrupt disabled */
545static void pipe_toggle_set(struct r8a66597 *r8a66597,
546 struct r8a66597_pipe *pipe,
547 struct urb *urb, int set)
548{
549 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
550 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
551 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
552
553 if (!toggle)
554 return;
555
556 if (set)
557 *toggle |= 1 << endpoint;
558 else
559 *toggle &= ~(1 << endpoint);
560}
561
562/* this function must be called with interrupt disabled */
563static void pipe_toggle_save(struct r8a66597 *r8a66597,
564 struct r8a66597_pipe *pipe,
565 struct urb *urb)
566{
567 if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
568 pipe_toggle_set(r8a66597, pipe, urb, 1);
569 else
570 pipe_toggle_set(r8a66597, pipe, urb, 0);
571}
572
573/* this function must be called with interrupt disabled */
574static void pipe_toggle_restore(struct r8a66597 *r8a66597,
575 struct r8a66597_pipe *pipe,
576 struct urb *urb)
577{
578 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
579 unsigned char endpoint = usb_pipeendpoint(urb->pipe);
580 unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
581
Yoshihiro Shimodaf6ace2c2007-05-30 20:42:41 +0900582 if (!toggle)
583 return;
584
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900585 r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
586}
587
588/* this function must be called with interrupt disabled */
589static void pipe_buffer_setting(struct r8a66597 *r8a66597,
590 struct r8a66597_pipe_info *info)
591{
592 u16 val = 0;
593
594 if (info->pipenum == 0)
595 return;
596
597 r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
598 r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
599 r8a66597_write(r8a66597, info->pipenum, PIPESEL);
600 if (!info->dir_in)
601 val |= R8A66597_DIR;
602 if (info->type == R8A66597_BULK && info->dir_in)
603 val |= R8A66597_DBLB | R8A66597_SHTNAK;
604 val |= info->type | info->epnum;
605 r8a66597_write(r8a66597, val, PIPECFG);
606
607 r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
608 PIPEBUF);
609 r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
610 PIPEMAXP);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900611 r8a66597_write(r8a66597, info->interval, PIPEPERI);
612}
613
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900614/* this function must be called with interrupt disabled */
615static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
616{
617 struct r8a66597_pipe_info *info;
618 struct urb *urb = td->urb;
619
620 if (td->pipenum > 0) {
621 info = &td->pipe->info;
622 cfifo_change(r8a66597, 0);
623 pipe_buffer_setting(r8a66597, info);
624
625 if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
626 usb_pipeout(urb->pipe)) &&
627 !usb_pipecontrol(urb->pipe)) {
628 r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
629 pipe_toggle_set(r8a66597, td->pipe, urb, 0);
630 clear_all_buffer(r8a66597, td->pipe);
631 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
632 usb_pipeout(urb->pipe), 1);
633 }
634 pipe_toggle_restore(r8a66597, td->pipe, urb);
635 }
636}
637
638/* this function must be called with interrupt disabled */
639static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
640 struct usb_endpoint_descriptor *ep)
641{
642 u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
643
644 memset(array, 0, sizeof(array));
Julia Lawall2e0fe702008-12-29 11:22:14 +0100645 switch (usb_endpoint_type(ep)) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900646 case USB_ENDPOINT_XFER_BULK:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100647 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900648 array[i++] = 4;
649 else {
650 array[i++] = 3;
651 array[i++] = 5;
652 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900653 break;
654 case USB_ENDPOINT_XFER_INT:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100655 if (usb_endpoint_dir_in(ep)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900656 array[i++] = 6;
657 array[i++] = 7;
658 array[i++] = 8;
659 } else
660 array[i++] = 9;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900661 break;
662 case USB_ENDPOINT_XFER_ISOC:
Julia Lawall2e0fe702008-12-29 11:22:14 +0100663 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900664 array[i++] = 2;
665 else
666 array[i++] = 1;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900667 break;
668 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700669 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900670 return 0;
671 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900672
673 i = 1;
674 min = array[0];
675 while (array[i] != 0) {
676 if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
677 min = array[i];
678 i++;
679 }
680
681 return min;
682}
683
684static u16 get_r8a66597_type(__u8 type)
685{
686 u16 r8a66597_type;
687
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900688 switch (type) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900689 case USB_ENDPOINT_XFER_BULK:
690 r8a66597_type = R8A66597_BULK;
691 break;
692 case USB_ENDPOINT_XFER_INT:
693 r8a66597_type = R8A66597_INT;
694 break;
695 case USB_ENDPOINT_XFER_ISOC:
696 r8a66597_type = R8A66597_ISO;
697 break;
698 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700699 printk(KERN_ERR "r8a66597: Illegal type\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900700 r8a66597_type = 0x0000;
701 break;
702 }
703
704 return r8a66597_type;
705}
706
707static u16 get_bufnum(u16 pipenum)
708{
709 u16 bufnum = 0;
710
711 if (pipenum == 0)
712 bufnum = 0;
713 else if (check_bulk_or_isoc(pipenum))
714 bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
715 else if (check_interrupt(pipenum))
716 bufnum = 4 + (pipenum - 6);
717 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700718 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900719
720 return bufnum;
721}
722
723static u16 get_buf_bsize(u16 pipenum)
724{
725 u16 buf_bsize = 0;
726
727 if (pipenum == 0)
728 buf_bsize = 3;
729 else if (check_bulk_or_isoc(pipenum))
730 buf_bsize = R8A66597_BUF_BSIZE - 1;
731 else if (check_interrupt(pipenum))
732 buf_bsize = 0;
733 else
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700734 printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900735
736 return buf_bsize;
737}
738
739/* this function must be called with interrupt disabled */
740static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
741 struct r8a66597_device *dev,
742 struct r8a66597_pipe *pipe,
743 struct urb *urb)
744{
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900745#if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900746 int i;
747 struct r8a66597_pipe_info *info = &pipe->info;
748
749 if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
750 for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
751 if ((r8a66597->dma_map & (1 << i)) != 0)
752 continue;
753
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -0700754 dev_info(&dev->udev->dev,
755 "address %d, EndpointAddress 0x%02x use "
756 "DMA FIFO\n", usb_pipedevice(urb->pipe),
757 info->dir_in ?
758 USB_ENDPOINT_DIR_MASK + info->epnum
759 : info->epnum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900760
761 r8a66597->dma_map |= 1 << i;
762 dev->dma_map |= 1 << i;
763 set_pipe_reg_addr(pipe, i);
764
765 cfifo_change(r8a66597, 0);
766 r8a66597_mdfy(r8a66597, MBW | pipe->info.pipenum,
767 MBW | CURPIPE, pipe->fifosel);
768
769 r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
770 pipe->info.pipenum);
771 r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
772 break;
773 }
774 }
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +0900775#endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900776}
777
778/* this function must be called with interrupt disabled */
779static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
780 struct usb_host_endpoint *hep,
781 struct r8a66597_pipe_info *info)
782{
783 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
784 struct r8a66597_pipe *pipe = hep->hcpriv;
785
786 dbg("enable_pipe:");
787
788 pipe->info = *info;
789 set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
790 r8a66597->pipe_cnt[pipe->info.pipenum]++;
791 dev->pipe_cnt[pipe->info.pipenum]++;
792
793 enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
794}
795
796/* this function must be called with interrupt disabled */
797static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
798{
799 struct r8a66597_td *td, *next;
800 struct urb *urb;
801 struct list_head *list = &r8a66597->pipe_queue[pipenum];
802
803 if (list_empty(list))
804 return;
805
806 list_for_each_entry_safe(td, next, list, queue) {
807 if (!td)
808 continue;
809 if (td->address != address)
810 continue;
811
812 urb = td->urb;
813 list_del(&td->queue);
814 kfree(td);
815
816 if (urb) {
Alan Sterne9df41c2007-08-08 11:48:02 -0400817 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597),
818 urb);
819
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900820 spin_unlock(&r8a66597->lock);
Alan Stern4a000272007-08-24 15:42:24 -0400821 usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb,
822 -ENODEV);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900823 spin_lock(&r8a66597->lock);
824 }
825 break;
826 }
827}
828
829/* this function must be called with interrupt disabled */
830static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
831 struct r8a66597_device *dev)
832{
833 int check_ep0 = 0;
834 u16 pipenum;
835
836 if (!dev)
837 return;
838
839 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
840 if (!dev->pipe_cnt[pipenum])
841 continue;
842
843 if (!check_ep0) {
844 check_ep0 = 1;
845 force_dequeue(r8a66597, 0, dev->address);
846 }
847
848 r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
849 dev->pipe_cnt[pipenum] = 0;
850 force_dequeue(r8a66597, pipenum, dev->address);
851 }
852
853 dbg("disable_pipe");
854
855 r8a66597->dma_map &= ~(dev->dma_map);
856 dev->dma_map = 0;
857}
858
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900859static u16 get_interval(struct urb *urb, __u8 interval)
860{
861 u16 time = 1;
862 int i;
863
864 if (urb->dev->speed == USB_SPEED_HIGH) {
865 if (interval > IITV)
866 time = IITV;
867 else
868 time = interval ? interval - 1 : 0;
869 } else {
870 if (interval > 128) {
871 time = IITV;
872 } else {
873 /* calculate the nearest value for PIPEPERI */
874 for (i = 0; i < 7; i++) {
875 if ((1 << i) < interval &&
876 (1 << (i + 1) > interval))
877 time = 1 << i;
878 }
879 }
880 }
881
882 return time;
883}
884
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900885static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
886{
887 __u8 i;
888 unsigned long time = 1;
889
890 if (usb_pipeisoc(urb->pipe))
891 return 0;
892
893 if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
894 for (i = 0; i < (interval - 1); i++)
895 time *= 2;
896 time = time * 125 / 1000; /* uSOF -> msec */
897 } else {
898 time = interval;
899 }
900
901 return time;
902}
903
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900904/* this function must be called with interrupt disabled */
905static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
906 struct usb_host_endpoint *hep,
907 struct usb_endpoint_descriptor *ep)
908{
909 struct r8a66597_pipe_info info;
910
911 info.pipenum = get_empty_pipenum(r8a66597, ep);
912 info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100913 info.epnum = usb_endpoint_num(ep);
Yoshihiro Shimoda05eac912007-10-03 18:53:28 +0900914 info.maxpacket = le16_to_cpu(ep->wMaxPacketSize);
Julia Lawall2e0fe702008-12-29 11:22:14 +0100915 info.type = get_r8a66597_type(usb_endpoint_type(ep));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900916 info.bufnum = get_bufnum(info.pipenum);
917 info.buf_bsize = get_buf_bsize(info.pipenum);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900918 if (info.type == R8A66597_BULK) {
919 info.interval = 0;
920 info.timer_interval = 0;
921 } else {
Yoshihiro Shimoda397f5192008-06-27 19:09:58 +0900922 info.interval = get_interval(urb, ep->bInterval);
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +0900923 info.timer_interval = get_timer_interval(urb, ep->bInterval);
924 }
Julia Lawall2e0fe702008-12-29 11:22:14 +0100925 if (usb_endpoint_dir_in(ep))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900926 info.dir_in = 1;
927 else
928 info.dir_in = 0;
929
930 enable_r8a66597_pipe(r8a66597, urb, hep, &info);
931}
932
933static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
934{
935 struct r8a66597_device *dev;
936
937 dev = get_urb_to_r8a66597_dev(r8a66597, urb);
938 dev->state = USB_STATE_CONFIGURED;
939}
940
941static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
942 u16 pipenum)
943{
944 if (pipenum == 0 && usb_pipeout(urb->pipe))
945 enable_irq_empty(r8a66597, pipenum);
946 else
947 enable_irq_ready(r8a66597, pipenum);
948
949 if (!usb_pipeisoc(urb->pipe))
950 enable_irq_nrdy(r8a66597, pipenum);
951}
952
953static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
954{
955 disable_irq_ready(r8a66597, pipenum);
956 disable_irq_nrdy(r8a66597, pipenum);
957}
958
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +0900959static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
960{
961 mod_timer(&r8a66597->rh_timer,
962 jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
963}
964
965static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
966 int connect)
967{
968 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
969
970 rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
971 rh->scount = R8A66597_MAX_SAMPLING;
972 if (connect)
973 rh->port |= 1 << USB_PORT_FEAT_CONNECTION;
974 else
975 rh->port &= ~(1 << USB_PORT_FEAT_CONNECTION);
976 rh->port |= 1 << USB_PORT_FEAT_C_CONNECTION;
977
978 r8a66597_root_hub_start_polling(r8a66597);
979}
980
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900981/* this function must be called with interrupt disabled */
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +0900982static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
983 u16 syssts)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900984{
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +0900985 if (syssts == SE0) {
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +0900986 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +0900987 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
988 return;
989 }
990
991 if (syssts == FS_JSTS)
992 r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
993 else if (syssts == LS_JSTS)
994 r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
995
Yoshihiro Shimodae2945312007-07-18 23:10:34 +0900996 r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +0900997 r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +0900998
999 if (r8a66597->bus_suspended)
1000 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001001}
1002
1003/* this function must be called with interrupt disabled */
1004static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
1005{
1006 u16 speed = get_rh_usb_speed(r8a66597, port);
1007 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1008
1009 if (speed == HSMODE)
1010 rh->port |= (1 << USB_PORT_FEAT_HIGHSPEED);
1011 else if (speed == LSMODE)
1012 rh->port |= (1 << USB_PORT_FEAT_LOWSPEED);
1013
1014 rh->port &= ~(1 << USB_PORT_FEAT_RESET);
1015 rh->port |= 1 << USB_PORT_FEAT_ENABLE;
1016}
1017
1018/* this function must be called with interrupt disabled */
1019static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
1020{
1021 struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
1022
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001023 disable_r8a66597_pipe_all(r8a66597, dev);
1024 free_usb_address(r8a66597, dev);
1025
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001026 start_root_hub_sampling(r8a66597, port, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001027}
1028
1029/* this function must be called with interrupt disabled */
1030static void prepare_setup_packet(struct r8a66597 *r8a66597,
1031 struct r8a66597_td *td)
1032{
1033 int i;
Al Virofd05e722008-04-28 07:00:16 +01001034 __le16 *p = (__le16 *)td->urb->setup_packet;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001035 unsigned long setup_addr = USBREQ;
1036
1037 r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
1038 DCPMAXP);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001039 r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001040
1041 for (i = 0; i < 4; i++) {
Al Virofd05e722008-04-28 07:00:16 +01001042 r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001043 setup_addr += 2;
1044 }
1045 r8a66597_write(r8a66597, SUREQ, DCPCTR);
1046}
1047
1048/* this function must be called with interrupt disabled */
1049static void prepare_packet_read(struct r8a66597 *r8a66597,
1050 struct r8a66597_td *td)
1051{
1052 struct urb *urb = td->urb;
1053
1054 if (usb_pipecontrol(urb->pipe)) {
1055 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1056 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1057 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1058 if (urb->actual_length == 0) {
1059 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1060 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1061 }
1062 pipe_irq_disable(r8a66597, td->pipenum);
1063 pipe_start(r8a66597, td->pipe);
1064 pipe_irq_enable(r8a66597, urb, td->pipenum);
1065 } else {
1066 if (urb->actual_length == 0) {
1067 pipe_irq_disable(r8a66597, td->pipenum);
1068 pipe_setting(r8a66597, td);
1069 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001070 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001071
1072 if (td->pipe->pipetre) {
1073 r8a66597_write(r8a66597, TRCLR,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001074 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001075 r8a66597_write(r8a66597,
Julia Lawalldfa5ec72008-03-04 15:25:11 -08001076 DIV_ROUND_UP
1077 (urb->transfer_buffer_length,
1078 td->maxpacket),
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001079 td->pipe->pipetrn);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001080 r8a66597_bset(r8a66597, TRENB,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001081 td->pipe->pipetre);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001082 }
1083
1084 pipe_start(r8a66597, td->pipe);
1085 pipe_irq_enable(r8a66597, urb, td->pipenum);
1086 }
1087 }
1088}
1089
1090/* this function must be called with interrupt disabled */
1091static void prepare_packet_write(struct r8a66597 *r8a66597,
1092 struct r8a66597_td *td)
1093{
1094 u16 tmp;
1095 struct urb *urb = td->urb;
1096
1097 if (usb_pipecontrol(urb->pipe)) {
1098 pipe_stop(r8a66597, td->pipe);
1099 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1100 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1101 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1102 if (urb->actual_length == 0) {
1103 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
1104 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
1105 }
1106 } else {
1107 if (urb->actual_length == 0)
1108 pipe_setting(r8a66597, td);
1109 if (td->pipe->pipetre)
1110 r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
1111 }
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001112 r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001113
1114 fifo_change_from_pipe(r8a66597, td->pipe);
1115 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1116 if (unlikely((tmp & FRDY) == 0))
1117 pipe_irq_enable(r8a66597, urb, td->pipenum);
1118 else
1119 packet_write(r8a66597, td->pipenum);
1120 pipe_start(r8a66597, td->pipe);
1121}
1122
1123/* this function must be called with interrupt disabled */
1124static void prepare_status_packet(struct r8a66597 *r8a66597,
1125 struct r8a66597_td *td)
1126{
1127 struct urb *urb = td->urb;
1128
1129 r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001130 pipe_stop(r8a66597, td->pipe);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001131
1132 if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
1133 r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
1134 r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
1135 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001136 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
Yoshihiro Shimoda9424ea22008-04-10 21:05:58 +09001137 r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001138 enable_irq_empty(r8a66597, 0);
1139 } else {
1140 r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
1141 r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
1142 r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
1143 r8a66597_write(r8a66597, BCLR, CFIFOCTR);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001144 enable_irq_ready(r8a66597, 0);
1145 }
1146 enable_irq_nrdy(r8a66597, 0);
1147 pipe_start(r8a66597, td->pipe);
1148}
1149
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001150static int is_set_address(unsigned char *setup_packet)
1151{
1152 if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
1153 setup_packet[1] == USB_REQ_SET_ADDRESS)
1154 return 1;
1155 else
1156 return 0;
1157}
1158
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001159/* this function must be called with interrupt disabled */
1160static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1161{
1162 BUG_ON(!td);
1163
1164 switch (td->type) {
1165 case USB_PID_SETUP:
Yoshihiro Shimodae3a09052007-10-03 18:53:13 +09001166 if (is_set_address(td->urb->setup_packet)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001167 td->set_address = 1;
1168 td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
1169 td->urb);
1170 if (td->urb->setup_packet[2] == 0)
1171 return -EPIPE;
1172 }
1173 prepare_setup_packet(r8a66597, td);
1174 break;
1175 case USB_PID_IN:
1176 prepare_packet_read(r8a66597, td);
1177 break;
1178 case USB_PID_OUT:
1179 prepare_packet_write(r8a66597, td);
1180 break;
1181 case USB_PID_ACK:
1182 prepare_status_packet(r8a66597, td);
1183 break;
1184 default:
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001185 printk(KERN_ERR "r8a66597: invalid type.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001186 break;
1187 }
1188
1189 return 0;
1190}
1191
1192static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
1193{
1194 if (usb_pipeisoc(urb->pipe)) {
1195 if (urb->number_of_packets == td->iso_cnt)
1196 return 1;
1197 }
1198
1199 /* control or bulk or interrupt */
1200 if ((urb->transfer_buffer_length <= urb->actual_length) ||
1201 (td->short_packet) || (td->zero_packet))
1202 return 1;
1203
1204 return 0;
1205}
1206
1207/* this function must be called with interrupt disabled */
1208static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
1209{
1210 unsigned long time;
1211
1212 BUG_ON(!td);
1213
1214 if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
1215 !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
1216 r8a66597->timeout_map |= 1 << td->pipenum;
1217 switch (usb_pipetype(td->urb->pipe)) {
1218 case PIPE_INTERRUPT:
1219 case PIPE_ISOCHRONOUS:
1220 time = 30;
1221 break;
1222 default:
1223 time = 300;
1224 break;
1225 }
1226
1227 mod_timer(&r8a66597->td_timer[td->pipenum],
1228 jiffies + msecs_to_jiffies(time));
1229 }
1230}
1231
1232/* this function must be called with interrupt disabled */
Alan Sterndfd1e532007-08-21 15:36:52 -04001233static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
Alan Stern888fda42007-08-24 15:41:18 -04001234 u16 pipenum, struct urb *urb, int status)
Alan Sterndfd1e532007-08-21 15:36:52 -04001235__releases(r8a66597->lock) __acquires(r8a66597->lock)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001236{
1237 int restart = 0;
1238 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
1239
1240 r8a66597->timeout_map &= ~(1 << pipenum);
1241
1242 if (likely(td)) {
Alan Stern888fda42007-08-24 15:41:18 -04001243 if (td->set_address && (status != 0 || urb->unlinked))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001244 r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
1245
1246 pipe_toggle_save(r8a66597, td->pipe, urb);
1247 list_del(&td->queue);
1248 kfree(td);
1249 }
1250
1251 if (!list_empty(&r8a66597->pipe_queue[pipenum]))
1252 restart = 1;
1253
1254 if (likely(urb)) {
1255 if (usb_pipeisoc(urb->pipe))
1256 urb->start_frame = r8a66597_get_frame(hcd);
1257
Alan Sterne9df41c2007-08-08 11:48:02 -04001258 usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001259 spin_unlock(&r8a66597->lock);
Alan Stern4a000272007-08-24 15:42:24 -04001260 usb_hcd_giveback_urb(hcd, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001261 spin_lock(&r8a66597->lock);
1262 }
1263
1264 if (restart) {
1265 td = r8a66597_get_td(r8a66597, pipenum);
1266 if (unlikely(!td))
1267 return;
1268
1269 start_transfer(r8a66597, td);
1270 set_td_timer(r8a66597, td);
1271 }
1272}
1273
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001274static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
1275{
1276 u16 tmp;
1277 int rcv_len, bufsize, urb_len, size;
1278 u16 *buf;
1279 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1280 struct urb *urb;
1281 int finish = 0;
Alan Sterndfd1e532007-08-21 15:36:52 -04001282 int status = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001283
1284 if (unlikely(!td))
1285 return;
1286 urb = td->urb;
1287
1288 fifo_change_from_pipe(r8a66597, td->pipe);
1289 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1290 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001291 pipe_stop(r8a66597, td->pipe);
1292 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001293 printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001294 finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001295 return;
1296 }
1297
1298 /* prepare parameters */
1299 rcv_len = tmp & DTLN;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001300 if (usb_pipeisoc(urb->pipe)) {
1301 buf = (u16 *)(urb->transfer_buffer +
1302 urb->iso_frame_desc[td->iso_cnt].offset);
1303 urb_len = urb->iso_frame_desc[td->iso_cnt].length;
1304 } else {
1305 buf = (void *)urb->transfer_buffer + urb->actual_length;
1306 urb_len = urb->transfer_buffer_length - urb->actual_length;
1307 }
Alan Sterndfd1e532007-08-21 15:36:52 -04001308 bufsize = min(urb_len, (int) td->maxpacket);
1309 if (rcv_len <= bufsize) {
1310 size = rcv_len;
1311 } else {
1312 size = bufsize;
1313 status = -EOVERFLOW;
1314 finish = 1;
1315 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001316
1317 /* update parameters */
1318 urb->actual_length += size;
1319 if (rcv_len == 0)
1320 td->zero_packet = 1;
Alan Sterndfd1e532007-08-21 15:36:52 -04001321 if (rcv_len < bufsize) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001322 td->short_packet = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001323 }
1324 if (usb_pipeisoc(urb->pipe)) {
1325 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
Alan Sterndfd1e532007-08-21 15:36:52 -04001326 urb->iso_frame_desc[td->iso_cnt].status = status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001327 td->iso_cnt++;
Alan Sterndfd1e532007-08-21 15:36:52 -04001328 finish = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001329 }
1330
1331 /* check transfer finish */
Alan Sternb0d9efb2007-08-21 15:39:21 -04001332 if (finish || check_transfer_finish(td, urb)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001333 pipe_stop(r8a66597, td->pipe);
1334 pipe_irq_disable(r8a66597, pipenum);
1335 finish = 1;
1336 }
1337
1338 /* read fifo */
1339 if (urb->transfer_buffer) {
1340 if (size == 0)
1341 r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
1342 else
1343 r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
1344 buf, size);
1345 }
1346
Alan Stern888fda42007-08-24 15:41:18 -04001347 if (finish && pipenum != 0)
1348 finish_request(r8a66597, td, pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001349}
1350
1351static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
1352{
1353 u16 tmp;
1354 int bufsize, size;
1355 u16 *buf;
1356 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1357 struct urb *urb;
1358
1359 if (unlikely(!td))
1360 return;
1361 urb = td->urb;
1362
1363 fifo_change_from_pipe(r8a66597, td->pipe);
1364 tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
1365 if (unlikely((tmp & FRDY) == 0)) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001366 pipe_stop(r8a66597, td->pipe);
1367 pipe_irq_disable(r8a66597, pipenum);
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07001368 printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001369 finish_request(r8a66597, td, pipenum, urb, -EPIPE);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001370 return;
1371 }
1372
1373 /* prepare parameters */
1374 bufsize = td->maxpacket;
1375 if (usb_pipeisoc(urb->pipe)) {
1376 buf = (u16 *)(urb->transfer_buffer +
1377 urb->iso_frame_desc[td->iso_cnt].offset);
1378 size = min(bufsize,
1379 (int)urb->iso_frame_desc[td->iso_cnt].length);
1380 } else {
1381 buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
Greg Kroah-Hartman16e2e5f2009-03-03 16:44:13 -08001382 size = min_t(u32, bufsize,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001383 urb->transfer_buffer_length - urb->actual_length);
1384 }
1385
1386 /* write fifo */
1387 if (pipenum > 0)
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001388 r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001389 if (urb->transfer_buffer) {
1390 r8a66597_write_fifo(r8a66597, td->pipe->fifoaddr, buf, size);
1391 if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
1392 r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
1393 }
1394
1395 /* update parameters */
1396 urb->actual_length += size;
1397 if (usb_pipeisoc(urb->pipe)) {
1398 urb->iso_frame_desc[td->iso_cnt].actual_length = size;
1399 urb->iso_frame_desc[td->iso_cnt].status = 0;
1400 td->iso_cnt++;
1401 }
1402
1403 /* check transfer finish */
1404 if (check_transfer_finish(td, urb)) {
1405 disable_irq_ready(r8a66597, pipenum);
1406 enable_irq_empty(r8a66597, pipenum);
1407 if (!usb_pipeisoc(urb->pipe))
1408 enable_irq_nrdy(r8a66597, pipenum);
1409 } else
1410 pipe_irq_enable(r8a66597, urb, pipenum);
1411}
1412
1413
Alan Stern888fda42007-08-24 15:41:18 -04001414static void check_next_phase(struct r8a66597 *r8a66597, int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001415{
1416 struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
1417 struct urb *urb;
1418 u8 finish = 0;
1419
1420 if (unlikely(!td))
1421 return;
1422 urb = td->urb;
1423
1424 switch (td->type) {
1425 case USB_PID_IN:
1426 case USB_PID_OUT:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001427 if (check_transfer_finish(td, urb))
1428 td->type = USB_PID_ACK;
1429 break;
1430 case USB_PID_SETUP:
Alan Sterneb231052007-08-21 15:40:36 -04001431 if (urb->transfer_buffer_length == urb->actual_length)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001432 td->type = USB_PID_ACK;
Alan Sterneb231052007-08-21 15:40:36 -04001433 else if (usb_pipeout(urb->pipe))
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001434 td->type = USB_PID_OUT;
1435 else
1436 td->type = USB_PID_IN;
1437 break;
1438 case USB_PID_ACK:
1439 finish = 1;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001440 break;
1441 }
1442
Alan Stern888fda42007-08-24 15:41:18 -04001443 if (finish || status != 0 || urb->unlinked)
1444 finish_request(r8a66597, td, 0, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001445 else
1446 start_transfer(r8a66597, td);
1447}
1448
Alan Stern888fda42007-08-24 15:41:18 -04001449static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001450{
1451 struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
1452
Alan Stern888fda42007-08-24 15:41:18 -04001453 if (td) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001454 u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
1455
1456 if (pid == PID_NAK)
Alan Stern888fda42007-08-24 15:41:18 -04001457 return -ECONNRESET;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001458 else
Alan Stern888fda42007-08-24 15:41:18 -04001459 return -EPIPE;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001460 }
Alan Stern888fda42007-08-24 15:41:18 -04001461 return 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001462}
1463
1464static void irq_pipe_ready(struct r8a66597 *r8a66597)
1465{
1466 u16 check;
1467 u16 pipenum;
1468 u16 mask;
1469 struct r8a66597_td *td;
1470
1471 mask = r8a66597_read(r8a66597, BRDYSTS)
1472 & r8a66597_read(r8a66597, BRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001473 r8a66597_write(r8a66597, ~mask, BRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001474 if (mask & BRDY0) {
1475 td = r8a66597_get_td(r8a66597, 0);
1476 if (td && td->type == USB_PID_IN)
1477 packet_read(r8a66597, 0);
1478 else
1479 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001480 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001481 }
1482
1483 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1484 check = 1 << pipenum;
1485 if (mask & check) {
1486 td = r8a66597_get_td(r8a66597, pipenum);
1487 if (unlikely(!td))
1488 continue;
1489
1490 if (td->type == USB_PID_IN)
1491 packet_read(r8a66597, pipenum);
1492 else if (td->type == USB_PID_OUT)
1493 packet_write(r8a66597, pipenum);
1494 }
1495 }
1496}
1497
1498static void irq_pipe_empty(struct r8a66597 *r8a66597)
1499{
1500 u16 tmp;
1501 u16 check;
1502 u16 pipenum;
1503 u16 mask;
1504 struct r8a66597_td *td;
1505
1506 mask = r8a66597_read(r8a66597, BEMPSTS)
1507 & r8a66597_read(r8a66597, BEMPENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001508 r8a66597_write(r8a66597, ~mask, BEMPSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001509 if (mask & BEMP0) {
1510 cfifo_change(r8a66597, 0);
1511 td = r8a66597_get_td(r8a66597, 0);
1512 if (td && td->type != USB_PID_OUT)
1513 disable_irq_empty(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001514 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001515 }
1516
1517 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1518 check = 1 << pipenum;
1519 if (mask & check) {
1520 struct r8a66597_td *td;
1521 td = r8a66597_get_td(r8a66597, pipenum);
1522 if (unlikely(!td))
1523 continue;
1524
1525 tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
1526 if ((tmp & INBUFM) == 0) {
1527 disable_irq_empty(r8a66597, pipenum);
1528 pipe_irq_disable(r8a66597, pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001529 finish_request(r8a66597, td, pipenum, td->urb,
1530 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001531 }
1532 }
1533 }
1534}
1535
1536static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
1537{
1538 u16 check;
1539 u16 pipenum;
1540 u16 mask;
Alan Stern888fda42007-08-24 15:41:18 -04001541 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001542
1543 mask = r8a66597_read(r8a66597, NRDYSTS)
1544 & r8a66597_read(r8a66597, NRDYENB);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001545 r8a66597_write(r8a66597, ~mask, NRDYSTS);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001546 if (mask & NRDY0) {
1547 cfifo_change(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001548 status = get_urb_error(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001549 pipe_irq_disable(r8a66597, 0);
Alan Stern888fda42007-08-24 15:41:18 -04001550 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001551 }
1552
1553 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1554 check = 1 << pipenum;
1555 if (mask & check) {
1556 struct r8a66597_td *td;
1557 td = r8a66597_get_td(r8a66597, pipenum);
1558 if (unlikely(!td))
1559 continue;
1560
Alan Stern888fda42007-08-24 15:41:18 -04001561 status = get_urb_error(r8a66597, pipenum);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001562 pipe_irq_disable(r8a66597, pipenum);
1563 pipe_stop(r8a66597, td->pipe);
Alan Stern888fda42007-08-24 15:41:18 -04001564 finish_request(r8a66597, td, pipenum, td->urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001565 }
1566 }
1567}
1568
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001569static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
1570{
1571 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1572 u16 intsts0, intsts1, intsts2;
1573 u16 intenb0, intenb1, intenb2;
1574 u16 mask0, mask1, mask2;
Alan Stern888fda42007-08-24 15:41:18 -04001575 int status;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001576
1577 spin_lock(&r8a66597->lock);
1578
1579 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1580 intsts1 = r8a66597_read(r8a66597, INTSTS1);
1581 intsts2 = r8a66597_read(r8a66597, INTSTS2);
1582 intenb0 = r8a66597_read(r8a66597, INTENB0);
1583 intenb1 = r8a66597_read(r8a66597, INTENB1);
1584 intenb2 = r8a66597_read(r8a66597, INTENB2);
1585
1586 mask2 = intsts2 & intenb2;
1587 mask1 = intsts1 & intenb1;
1588 mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
1589 if (mask2) {
1590 if (mask2 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001591 r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001592 r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
1593
1594 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001595 start_root_hub_sampling(r8a66597, 1, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001596 }
1597 if (mask2 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001598 r8a66597_write(r8a66597, ~DTCH, INTSTS2);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001599 r8a66597_bclr(r8a66597, DTCHE, INTENB2);
1600 r8a66597_usb_disconnect(r8a66597, 1);
1601 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001602 if (mask2 & BCHG) {
1603 r8a66597_write(r8a66597, ~BCHG, INTSTS2);
1604 r8a66597_bclr(r8a66597, BCHGE, INTENB2);
1605 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1606 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001607 }
1608
1609 if (mask1) {
1610 if (mask1 & ATTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001611 r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001612 r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
1613
1614 /* start usb bus sampling */
Yoshihiro Shimoda54d0be92008-07-11 18:53:45 +09001615 start_root_hub_sampling(r8a66597, 0, 1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001616 }
1617 if (mask1 & DTCH) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001618 r8a66597_write(r8a66597, ~DTCH, INTSTS1);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001619 r8a66597_bclr(r8a66597, DTCHE, INTENB1);
1620 r8a66597_usb_disconnect(r8a66597, 0);
1621 }
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09001622 if (mask1 & BCHG) {
1623 r8a66597_write(r8a66597, ~BCHG, INTSTS1);
1624 r8a66597_bclr(r8a66597, BCHGE, INTENB1);
1625 usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
1626 }
1627
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001628 if (mask1 & SIGN) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001629 r8a66597_write(r8a66597, ~SIGN, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001630 status = get_urb_error(r8a66597, 0);
1631 check_next_phase(r8a66597, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001632 }
1633 if (mask1 & SACK) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001634 r8a66597_write(r8a66597, ~SACK, INTSTS1);
Alan Stern888fda42007-08-24 15:41:18 -04001635 check_next_phase(r8a66597, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001636 }
1637 }
1638 if (mask0) {
1639 if (mask0 & BRDY)
1640 irq_pipe_ready(r8a66597);
1641 if (mask0 & BEMP)
1642 irq_pipe_empty(r8a66597);
1643 if (mask0 & NRDY)
1644 irq_pipe_nrdy(r8a66597);
1645 }
1646
1647 spin_unlock(&r8a66597->lock);
1648 return IRQ_HANDLED;
1649}
1650
1651/* this function must be called with interrupt disabled */
1652static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
1653{
1654 u16 tmp;
1655 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
1656
1657 if (rh->port & (1 << USB_PORT_FEAT_RESET)) {
1658 unsigned long dvstctr_reg = get_dvstctr_reg(port);
1659
1660 tmp = r8a66597_read(r8a66597, dvstctr_reg);
1661 if ((tmp & USBRST) == USBRST) {
1662 r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
1663 dvstctr_reg);
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001664 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001665 } else
1666 r8a66597_usb_connect(r8a66597, port);
1667 }
1668
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001669 if (!(rh->port & (1 << USB_PORT_FEAT_CONNECTION))) {
1670 r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
1671 r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
1672 }
1673
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001674 if (rh->scount > 0) {
1675 tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
1676 if (tmp == rh->old_syssts) {
1677 rh->scount--;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001678 if (rh->scount == 0)
1679 r8a66597_check_syssts(r8a66597, port, tmp);
1680 else
1681 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001682 } else {
1683 rh->scount = R8A66597_MAX_SAMPLING;
1684 rh->old_syssts = tmp;
Yoshihiro Shimoda29fab0c2008-04-10 21:05:55 +09001685 r8a66597_root_hub_start_polling(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001686 }
1687 }
1688}
1689
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001690static void r8a66597_interval_timer(unsigned long _r8a66597)
1691{
1692 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1693 unsigned long flags;
1694 u16 pipenum;
1695 struct r8a66597_td *td;
1696
1697 spin_lock_irqsave(&r8a66597->lock, flags);
1698
1699 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1700 if (!(r8a66597->interval_map & (1 << pipenum)))
1701 continue;
1702 if (timer_pending(&r8a66597->interval_timer[pipenum]))
1703 continue;
1704
1705 td = r8a66597_get_td(r8a66597, pipenum);
1706 if (td)
1707 start_transfer(r8a66597, td);
1708 }
1709
1710 spin_unlock_irqrestore(&r8a66597->lock, flags);
1711}
1712
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001713static void r8a66597_td_timer(unsigned long _r8a66597)
1714{
1715 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1716 unsigned long flags;
1717 u16 pipenum;
1718 struct r8a66597_td *td, *new_td = NULL;
1719 struct r8a66597_pipe *pipe;
1720
1721 spin_lock_irqsave(&r8a66597->lock, flags);
1722 for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
1723 if (!(r8a66597->timeout_map & (1 << pipenum)))
1724 continue;
1725 if (timer_pending(&r8a66597->td_timer[pipenum]))
1726 continue;
1727
1728 td = r8a66597_get_td(r8a66597, pipenum);
1729 if (!td) {
1730 r8a66597->timeout_map &= ~(1 << pipenum);
1731 continue;
1732 }
1733
1734 if (td->urb->actual_length) {
1735 set_td_timer(r8a66597, td);
1736 break;
1737 }
1738
1739 pipe = td->pipe;
1740 pipe_stop(r8a66597, pipe);
1741
1742 new_td = td;
1743 do {
1744 list_move_tail(&new_td->queue,
1745 &r8a66597->pipe_queue[pipenum]);
1746 new_td = r8a66597_get_td(r8a66597, pipenum);
1747 if (!new_td) {
1748 new_td = td;
1749 break;
1750 }
1751 } while (td != new_td && td->address == new_td->address);
1752
1753 start_transfer(r8a66597, new_td);
1754
1755 if (td == new_td)
1756 r8a66597->timeout_map &= ~(1 << pipenum);
1757 else
1758 set_td_timer(r8a66597, new_td);
1759 break;
1760 }
1761 spin_unlock_irqrestore(&r8a66597->lock, flags);
1762}
1763
1764static void r8a66597_timer(unsigned long _r8a66597)
1765{
1766 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1767 unsigned long flags;
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001768 int port;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001769
1770 spin_lock_irqsave(&r8a66597->lock, flags);
1771
Yoshihiro Shimoda58639642008-11-11 16:47:21 +09001772 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++)
1773 r8a66597_root_hub_control(r8a66597, port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001774
1775 spin_unlock_irqrestore(&r8a66597->lock, flags);
1776}
1777
1778static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
1779{
1780 struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
1781
1782 if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
1783 (urb->dev->state == USB_STATE_CONFIGURED))
1784 return 1;
1785 else
1786 return 0;
1787}
1788
1789static int r8a66597_start(struct usb_hcd *hcd)
1790{
1791 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001792
1793 hcd->state = HC_STATE_RUNNING;
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001794 return enable_controller(r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001795}
1796
1797static void r8a66597_stop(struct usb_hcd *hcd)
1798{
1799 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1800
1801 disable_controller(r8a66597);
1802}
1803
1804static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
1805{
1806 unsigned int usb_address = usb_pipedevice(urb->pipe);
1807 u16 root_port, hub_port;
1808
1809 if (usb_address == 0) {
1810 get_port_number(urb->dev->devpath,
1811 &root_port, &hub_port);
1812 set_devadd_reg(r8a66597, 0,
1813 get_r8a66597_usb_speed(urb->dev->speed),
1814 get_parent_r8a66597_address(r8a66597, urb->dev),
1815 hub_port, root_port);
1816 }
1817}
1818
1819static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
1820 struct urb *urb,
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001821 struct usb_host_endpoint *hep)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001822{
1823 struct r8a66597_td *td;
1824 u16 pipenum;
1825
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001826 td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001827 if (td == NULL)
1828 return NULL;
1829
1830 pipenum = r8a66597_get_pipenum(urb, hep);
1831 td->pipenum = pipenum;
1832 td->pipe = hep->hcpriv;
1833 td->urb = urb;
1834 td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
1835 td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
1836 !usb_pipein(urb->pipe));
1837 if (usb_pipecontrol(urb->pipe))
1838 td->type = USB_PID_SETUP;
1839 else if (usb_pipein(urb->pipe))
1840 td->type = USB_PID_IN;
1841 else
1842 td->type = USB_PID_OUT;
1843 INIT_LIST_HEAD(&td->queue);
1844
1845 return td;
1846}
1847
1848static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001849 struct urb *urb,
1850 gfp_t mem_flags)
1851{
Alan Sterne9df41c2007-08-08 11:48:02 -04001852 struct usb_host_endpoint *hep = urb->ep;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001853 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1854 struct r8a66597_td *td = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -04001855 int ret, request = 0;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001856 unsigned long flags;
1857
1858 spin_lock_irqsave(&r8a66597->lock, flags);
1859 if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
1860 ret = -ENODEV;
Alan Sterne9df41c2007-08-08 11:48:02 -04001861 goto error_not_linked;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001862 }
1863
Alan Sterne9df41c2007-08-08 11:48:02 -04001864 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1865 if (ret)
1866 goto error_not_linked;
1867
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001868 if (!hep->hcpriv) {
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001869 hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
1870 GFP_ATOMIC);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001871 if (!hep->hcpriv) {
1872 ret = -ENOMEM;
1873 goto error;
1874 }
1875 set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
1876 if (usb_pipeendpoint(urb->pipe))
1877 init_pipe_info(r8a66597, urb, hep, &hep->desc);
1878 }
1879
1880 if (unlikely(check_pipe_config(r8a66597, urb)))
1881 init_pipe_config(r8a66597, urb);
1882
1883 set_address_zero(r8a66597, urb);
Yoshihiro Shimodae2945312007-07-18 23:10:34 +09001884 td = r8a66597_make_td(r8a66597, urb, hep);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001885 if (td == NULL) {
1886 ret = -ENOMEM;
1887 goto error;
1888 }
1889 if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
1890 request = 1;
1891 list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001892 urb->hcpriv = td;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001893
1894 if (request) {
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09001895 if (td->pipe->info.timer_interval) {
1896 r8a66597->interval_map |= 1 << td->pipenum;
1897 mod_timer(&r8a66597->interval_timer[td->pipenum],
1898 jiffies + msecs_to_jiffies(
1899 td->pipe->info.timer_interval));
1900 } else {
1901 ret = start_transfer(r8a66597, td);
1902 if (ret < 0) {
1903 list_del(&td->queue);
1904 kfree(td);
1905 }
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001906 }
1907 } else
1908 set_td_timer(r8a66597, td);
1909
1910error:
Alan Sterne9df41c2007-08-08 11:48:02 -04001911 if (ret)
1912 usb_hcd_unlink_urb_from_ep(hcd, urb);
1913error_not_linked:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001914 spin_unlock_irqrestore(&r8a66597->lock, flags);
1915 return ret;
1916}
1917
Alan Sterne9df41c2007-08-08 11:48:02 -04001918static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
1919 int status)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001920{
1921 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1922 struct r8a66597_td *td;
1923 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001924 int rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001925
1926 spin_lock_irqsave(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001927 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1928 if (rc)
1929 goto done;
1930
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001931 if (urb->hcpriv) {
1932 td = urb->hcpriv;
1933 pipe_stop(r8a66597, td->pipe);
1934 pipe_irq_disable(r8a66597, td->pipenum);
1935 disable_irq_empty(r8a66597, td->pipenum);
Alan Stern888fda42007-08-24 15:41:18 -04001936 finish_request(r8a66597, td, td->pipenum, urb, status);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001937 }
Alan Sterne9df41c2007-08-08 11:48:02 -04001938 done:
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001939 spin_unlock_irqrestore(&r8a66597->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001940 return rc;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001941}
1942
1943static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
1944 struct usb_host_endpoint *hep)
1945{
1946 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1947 struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
1948 struct r8a66597_td *td;
1949 struct urb *urb = NULL;
1950 u16 pipenum;
1951 unsigned long flags;
1952
1953 if (pipe == NULL)
1954 return;
1955 pipenum = pipe->info.pipenum;
1956
1957 if (pipenum == 0) {
1958 kfree(hep->hcpriv);
1959 hep->hcpriv = NULL;
1960 return;
1961 }
1962
1963 spin_lock_irqsave(&r8a66597->lock, flags);
1964 pipe_stop(r8a66597, pipe);
1965 pipe_irq_disable(r8a66597, pipenum);
1966 disable_irq_empty(r8a66597, pipenum);
1967 td = r8a66597_get_td(r8a66597, pipenum);
1968 if (td)
1969 urb = td->urb;
Alan Stern888fda42007-08-24 15:41:18 -04001970 finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09001971 kfree(hep->hcpriv);
1972 hep->hcpriv = NULL;
1973 spin_unlock_irqrestore(&r8a66597->lock, flags);
1974}
1975
1976static int r8a66597_get_frame(struct usb_hcd *hcd)
1977{
1978 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
1979 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1980}
1981
1982static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
1983{
1984 int chix;
1985
1986 if (udev->state == USB_STATE_CONFIGURED &&
1987 udev->parent && udev->parent->devnum > 1 &&
1988 udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
1989 map[udev->devnum/32] |= (1 << (udev->devnum % 32));
1990
1991 for (chix = 0; chix < udev->maxchild; chix++) {
1992 struct usb_device *childdev = udev->children[chix];
1993
1994 if (childdev)
1995 collect_usb_address_map(childdev, map);
1996 }
1997}
1998
1999/* this function must be called with interrupt disabled */
2000static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
2001 int addr)
2002{
2003 struct r8a66597_device *dev;
2004 struct list_head *list = &r8a66597->child_device;
2005
2006 list_for_each_entry(dev, list, device_list) {
2007 if (!dev)
2008 continue;
2009 if (dev->usb_address != addr)
2010 continue;
2011
2012 return dev;
2013 }
2014
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002015 printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002016 return NULL;
2017}
2018
2019static void update_usb_address_map(struct r8a66597 *r8a66597,
2020 struct usb_device *root_hub,
2021 unsigned long *map)
2022{
2023 int i, j, addr;
2024 unsigned long diff;
2025 unsigned long flags;
2026
2027 for (i = 0; i < 4; i++) {
2028 diff = r8a66597->child_connect_map[i] ^ map[i];
2029 if (!diff)
2030 continue;
2031
2032 for (j = 0; j < 32; j++) {
2033 if (!(diff & (1 << j)))
2034 continue;
2035
2036 addr = i * 32 + j;
2037 if (map[i] & (1 << j))
2038 set_child_connect_map(r8a66597, addr);
2039 else {
2040 struct r8a66597_device *dev;
2041
2042 spin_lock_irqsave(&r8a66597->lock, flags);
2043 dev = get_r8a66597_device(r8a66597, addr);
2044 disable_r8a66597_pipe_all(r8a66597, dev);
2045 free_usb_address(r8a66597, dev);
2046 put_child_connect_map(r8a66597, addr);
2047 spin_unlock_irqrestore(&r8a66597->lock, flags);
2048 }
2049 }
2050 }
2051}
2052
2053static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
2054 struct usb_hcd *hcd)
2055{
2056 struct usb_bus *bus;
2057 unsigned long now_map[4];
2058
2059 memset(now_map, 0, sizeof(now_map));
2060
2061 list_for_each_entry(bus, &usb_bus_list, bus_list) {
2062 if (!bus->root_hub)
2063 continue;
2064
2065 if (bus->busnum != hcd->self.busnum)
2066 continue;
2067
2068 collect_usb_address_map(bus->root_hub, now_map);
2069 update_usb_address_map(r8a66597, bus->root_hub, now_map);
2070 }
2071}
2072
2073static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
2074{
2075 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2076 unsigned long flags;
2077 int i;
2078
2079 r8a66597_check_detect_child(r8a66597, hcd);
2080
2081 spin_lock_irqsave(&r8a66597->lock, flags);
2082
2083 *buf = 0; /* initialize (no change) */
2084
2085 for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) {
2086 if (r8a66597->root_hub[i].port & 0xffff0000)
2087 *buf |= 1 << (i + 1);
2088 }
2089
2090 spin_unlock_irqrestore(&r8a66597->lock, flags);
2091
2092 return (*buf != 0);
2093}
2094
2095static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
2096 struct usb_hub_descriptor *desc)
2097{
2098 desc->bDescriptorType = 0x29;
2099 desc->bHubContrCurrent = 0;
2100 desc->bNbrPorts = R8A66597_MAX_ROOT_HUB;
2101 desc->bDescLength = 9;
2102 desc->bPwrOn2PwrGood = 0;
2103 desc->wHubCharacteristics = cpu_to_le16(0x0011);
2104 desc->bitmap[0] = ((1 << R8A66597_MAX_ROOT_HUB) - 1) << 1;
2105 desc->bitmap[1] = ~0;
2106}
2107
2108static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
2109 u16 wIndex, char *buf, u16 wLength)
2110{
2111 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2112 int ret;
2113 int port = (wIndex & 0x00FF) - 1;
2114 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2115 unsigned long flags;
2116
2117 ret = 0;
2118
2119 spin_lock_irqsave(&r8a66597->lock, flags);
2120 switch (typeReq) {
2121 case ClearHubFeature:
2122 case SetHubFeature:
2123 switch (wValue) {
2124 case C_HUB_OVER_CURRENT:
2125 case C_HUB_LOCAL_POWER:
2126 break;
2127 default:
2128 goto error;
2129 }
2130 break;
2131 case ClearPortFeature:
2132 if (wIndex > R8A66597_MAX_ROOT_HUB)
2133 goto error;
2134 if (wLength != 0)
2135 goto error;
2136
2137 switch (wValue) {
2138 case USB_PORT_FEAT_ENABLE:
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002139 rh->port &= ~(1 << USB_PORT_FEAT_POWER);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002140 break;
2141 case USB_PORT_FEAT_SUSPEND:
2142 break;
2143 case USB_PORT_FEAT_POWER:
2144 r8a66597_port_power(r8a66597, port, 0);
2145 break;
2146 case USB_PORT_FEAT_C_ENABLE:
2147 case USB_PORT_FEAT_C_SUSPEND:
2148 case USB_PORT_FEAT_C_CONNECTION:
2149 case USB_PORT_FEAT_C_OVER_CURRENT:
2150 case USB_PORT_FEAT_C_RESET:
2151 break;
2152 default:
2153 goto error;
2154 }
2155 rh->port &= ~(1 << wValue);
2156 break;
2157 case GetHubDescriptor:
2158 r8a66597_hub_descriptor(r8a66597,
2159 (struct usb_hub_descriptor *)buf);
2160 break;
2161 case GetHubStatus:
2162 *buf = 0x00;
2163 break;
2164 case GetPortStatus:
2165 if (wIndex > R8A66597_MAX_ROOT_HUB)
2166 goto error;
Al Virofd05e722008-04-28 07:00:16 +01002167 *(__le32 *)buf = cpu_to_le32(rh->port);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002168 break;
2169 case SetPortFeature:
2170 if (wIndex > R8A66597_MAX_ROOT_HUB)
2171 goto error;
2172 if (wLength != 0)
2173 goto error;
2174
2175 switch (wValue) {
2176 case USB_PORT_FEAT_SUSPEND:
2177 break;
2178 case USB_PORT_FEAT_POWER:
2179 r8a66597_port_power(r8a66597, port, 1);
2180 rh->port |= (1 << USB_PORT_FEAT_POWER);
2181 break;
2182 case USB_PORT_FEAT_RESET: {
2183 struct r8a66597_device *dev = rh->dev;
2184
2185 rh->port |= (1 << USB_PORT_FEAT_RESET);
2186
2187 disable_r8a66597_pipe_all(r8a66597, dev);
2188 free_usb_address(r8a66597, dev);
2189
2190 r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
2191 get_dvstctr_reg(port));
2192 mod_timer(&r8a66597->rh_timer,
2193 jiffies + msecs_to_jiffies(50));
2194 }
2195 break;
2196 default:
2197 goto error;
2198 }
2199 rh->port |= 1 << wValue;
2200 break;
2201 default:
2202error:
2203 ret = -EPIPE;
2204 break;
2205 }
2206
2207 spin_unlock_irqrestore(&r8a66597->lock, flags);
2208 return ret;
2209}
2210
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002211#if defined(CONFIG_PM)
2212static int r8a66597_bus_suspend(struct usb_hcd *hcd)
2213{
2214 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2215 int port;
2216
2217 dbg("%s", __func__);
2218
2219 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
2220 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2221 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2222
2223 if (!(rh->port & (1 << USB_PORT_FEAT_ENABLE)))
2224 continue;
2225
2226 dbg("suspend port = %d", port);
2227 r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
2228 rh->port |= 1 << USB_PORT_FEAT_SUSPEND;
2229
2230 if (rh->dev->udev->do_remote_wakeup) {
2231 msleep(3); /* waiting last SOF */
2232 r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
2233 r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
2234 r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
2235 }
2236 }
2237
2238 r8a66597->bus_suspended = 1;
2239
2240 return 0;
2241}
2242
2243static int r8a66597_bus_resume(struct usb_hcd *hcd)
2244{
2245 struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
2246 int port;
2247
2248 dbg("%s", __func__);
2249
2250 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
2251 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2252 unsigned long dvstctr_reg = get_dvstctr_reg(port);
2253
2254 if (!(rh->port & (1 << USB_PORT_FEAT_SUSPEND)))
2255 continue;
2256
2257 dbg("resume port = %d", port);
2258 rh->port &= ~(1 << USB_PORT_FEAT_SUSPEND);
2259 rh->port |= 1 << USB_PORT_FEAT_C_SUSPEND;
2260 r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
2261 msleep(50);
2262 r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
2263 }
2264
2265 return 0;
2266
2267}
2268#else
2269#define r8a66597_bus_suspend NULL
2270#define r8a66597_bus_resume NULL
2271#endif
2272
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002273static struct hc_driver r8a66597_hc_driver = {
2274 .description = hcd_name,
2275 .hcd_priv_size = sizeof(struct r8a66597),
2276 .irq = r8a66597_irq,
2277
2278 /*
2279 * generic hardware linkage
2280 */
2281 .flags = HCD_USB2,
2282
2283 .start = r8a66597_start,
2284 .stop = r8a66597_stop,
2285
2286 /*
2287 * managing i/o requests and associated device resources
2288 */
2289 .urb_enqueue = r8a66597_urb_enqueue,
2290 .urb_dequeue = r8a66597_urb_dequeue,
2291 .endpoint_disable = r8a66597_endpoint_disable,
2292
2293 /*
2294 * periodic schedule support
2295 */
2296 .get_frame_number = r8a66597_get_frame,
2297
2298 /*
2299 * root hub support
2300 */
2301 .hub_status_data = r8a66597_hub_status_data,
2302 .hub_control = r8a66597_hub_control,
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002303 .bus_suspend = r8a66597_bus_suspend,
2304 .bus_resume = r8a66597_bus_resume,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002305};
2306
2307#if defined(CONFIG_PM)
2308static int r8a66597_suspend(struct platform_device *pdev, pm_message_t state)
2309{
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002310 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2311 int port;
2312
2313 dbg("%s", __func__);
2314
2315 disable_controller(r8a66597);
2316
2317 for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) {
2318 struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
2319
2320 rh->port = 0x00000000;
2321 }
2322
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002323 return 0;
2324}
2325
2326static int r8a66597_resume(struct platform_device *pdev)
2327{
Yoshihiro Shimodae1e609b2009-03-19 14:18:15 +09002328 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2329 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2330
2331 dbg("%s", __func__);
2332
2333 enable_controller(r8a66597);
2334 usb_root_hub_lost_power(hcd->self.root_hub);
2335
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002336 return 0;
2337}
2338#else /* if defined(CONFIG_PM) */
2339#define r8a66597_suspend NULL
2340#define r8a66597_resume NULL
2341#endif
2342
2343static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2344{
2345 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
2346 struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
2347
2348 del_timer_sync(&r8a66597->rh_timer);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002349 usb_remove_hcd(hcd);
Yoshihiro Shimodaca0677a2007-10-05 15:53:12 +09002350 iounmap((void *)r8a66597->reg);
Magnus Damm765786e2008-10-31 20:22:38 +09002351#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2352 clk_put(r8a66597->clk);
2353#endif
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002354 usb_put_hcd(hcd);
2355 return 0;
2356}
2357
Uwe Kleine-König8864bd82009-03-28 00:27:00 +01002358static int __devinit r8a66597_probe(struct platform_device *pdev)
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002359{
Magnus Damm765786e2008-10-31 20:22:38 +09002360#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2361 char clk_name[8];
2362#endif
Marc Zyngier27140212008-08-18 13:08:42 +02002363 struct resource *res = NULL, *ires;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002364 int irq = -1;
2365 void __iomem *reg = NULL;
2366 struct usb_hcd *hcd = NULL;
2367 struct r8a66597 *r8a66597;
2368 int ret = 0;
2369 int i;
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002370 unsigned long irq_trigger;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002371
2372 if (pdev->dev.dma_mask) {
2373 ret = -EINVAL;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002374 dev_err(&pdev->dev, "dma not supported\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002375 goto clean_up;
2376 }
2377
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002378 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002379 if (!res) {
2380 ret = -ENODEV;
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002381 dev_err(&pdev->dev, "platform_get_resource error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002382 goto clean_up;
2383 }
2384
Marc Zyngier27140212008-08-18 13:08:42 +02002385 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2386 if (!ires) {
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002387 ret = -ENODEV;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002388 dev_err(&pdev->dev,
2389 "platform_get_resource IORESOURCE_IRQ error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002390 goto clean_up;
2391 }
2392
Marc Zyngier27140212008-08-18 13:08:42 +02002393 irq = ires->start;
2394 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
2395
Magnus Damm0a2e5b92008-11-13 15:46:37 +09002396 reg = ioremap(res->start, resource_size(res));
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002397 if (reg == NULL) {
2398 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002399 dev_err(&pdev->dev, "ioremap error.\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002400 goto clean_up;
2401 }
2402
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002403 if (pdev->dev.platform_data == NULL) {
2404 dev_err(&pdev->dev, "no platform data\n");
2405 ret = -ENODEV;
2406 goto clean_up;
2407 }
2408
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002409 /* initialize hcd */
2410 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2411 if (!hcd) {
2412 ret = -ENOMEM;
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002413 dev_err(&pdev->dev, "Failed to create hcd\n");
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002414 goto clean_up;
2415 }
2416 r8a66597 = hcd_to_r8a66597(hcd);
2417 memset(r8a66597, 0, sizeof(struct r8a66597));
2418 dev_set_drvdata(&pdev->dev, r8a66597);
Yoshihiro Shimoda5effabb2009-05-26 18:24:34 +09002419 r8a66597->pdata = pdev->dev.platform_data;
2420 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002421
Magnus Damm765786e2008-10-31 20:22:38 +09002422#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2423 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
2424 r8a66597->clk = clk_get(&pdev->dev, clk_name);
2425 if (IS_ERR(r8a66597->clk)) {
2426 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
2427 ret = PTR_ERR(r8a66597->clk);
2428 goto clean_up2;
2429 }
2430#endif
2431
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002432 spin_lock_init(&r8a66597->lock);
2433 init_timer(&r8a66597->rh_timer);
2434 r8a66597->rh_timer.function = r8a66597_timer;
2435 r8a66597->rh_timer.data = (unsigned long)r8a66597;
2436 r8a66597->reg = (unsigned long)reg;
2437
2438 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
2439 INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
2440 init_timer(&r8a66597->td_timer[i]);
2441 r8a66597->td_timer[i].function = r8a66597_td_timer;
2442 r8a66597->td_timer[i].data = (unsigned long)r8a66597;
Yoshihiro Shimoda6d879102008-04-10 21:05:47 +09002443 setup_timer(&r8a66597->interval_timer[i],
2444 r8a66597_interval_timer,
2445 (unsigned long)r8a66597);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002446 }
2447 INIT_LIST_HEAD(&r8a66597->child_device);
2448
2449 hcd->rsrc_start = res->start;
Marc Zyngier27140212008-08-18 13:08:42 +02002450
Yoshihiro Shimoda0bf32b82008-06-27 19:09:55 +09002451 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002452 if (ret != 0) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -07002453 dev_err(&pdev->dev, "Failed to add hcd\n");
Magnus Damm765786e2008-10-31 20:22:38 +09002454 goto clean_up3;
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002455 }
2456
2457 return 0;
2458
Magnus Damm765786e2008-10-31 20:22:38 +09002459clean_up3:
2460#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2461 clk_put(r8a66597->clk);
Magnus Damm765786e2008-10-31 20:22:38 +09002462clean_up2:
Paul Mundtd6435102008-11-18 12:40:39 +09002463#endif
Magnus Damm765786e2008-10-31 20:22:38 +09002464 usb_put_hcd(hcd);
2465
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002466clean_up:
2467 if (reg)
2468 iounmap(reg);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002469
2470 return ret;
2471}
2472
2473static struct platform_driver r8a66597_driver = {
2474 .probe = r8a66597_probe,
2475 .remove = r8a66597_remove,
2476 .suspend = r8a66597_suspend,
2477 .resume = r8a66597_resume,
2478 .driver = {
2479 .name = (char *) hcd_name,
Kay Sieversf4fce612008-04-10 21:29:22 -07002480 .owner = THIS_MODULE,
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002481 },
2482};
2483
2484static int __init r8a66597_init(void)
2485{
2486 if (usb_disabled())
2487 return -ENODEV;
2488
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -07002489 printk(KERN_INFO KBUILD_MODNAME ": driver %s, %s\n", hcd_name,
2490 DRIVER_VERSION);
Yoshihiro Shimoda5d304352007-05-10 13:18:19 +09002491 return platform_driver_register(&r8a66597_driver);
2492}
2493module_init(r8a66597_init);
2494
2495static void __exit r8a66597_cleanup(void)
2496{
2497 platform_driver_unregister(&r8a66597_driver);
2498}
2499module_exit(r8a66597_cleanup);
2500