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