Automatic merge with /usr/src/ntfs-2.6.git.
[linux-2.6] / drivers / usb / host / isp116x-hcd.c
1 /*
2  * ISP116x HCD (Host Controller Driver) for USB.
3  *
4  * Derived from the SL811 HCD, rewritten for ISP116x.
5  * Copyright (C) 2005 Olav Kongas <ok@artecdesign.ee>
6  *
7  * Portions:
8  * Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
9  * Copyright (C) 2004 David Brownell
10  *
11  * Periodic scheduling is based on Roman's OHCI code
12  * Copyright (C) 1999 Roman Weissgaerber
13  *
14  */
15
16 /*
17  * The driver basically works. A number of people have used it with a range
18  * of devices.
19  *
20  * The driver passes all usbtests 1-14.
21  *
22  * Suspending/resuming of root hub via sysfs works. Remote wakeup works too.
23  * And suspending/resuming of platform device works too. Suspend/resume
24  * via HCD operations vector is not implemented.
25  *
26  * Iso transfer support is not implemented. Adding this would include
27  * implementing recovery from the failure to service the processed ITL
28  * fifo ram in time, which will involve chip reset.
29  *
30  * TODO:
31  + More testing of suspend/resume.
32 */
33
34 /*
35   ISP116x chips require certain delays between accesses to its
36   registers. The following timing options exist.
37
38   1. Configure your memory controller (the best)
39   2. Implement platform-specific delay function possibly
40   combined with configuring the memory controller; see
41   include/linux/usb-isp116x.h for more info. Some broken
42   memory controllers line LH7A400 SMC need this. Also,
43   uncomment for that to work the following
44   USE_PLATFORM_DELAY macro.
45   3. Use ndelay (easiest, poorest). For that, uncomment
46   the following USE_NDELAY macro.
47 */
48 #define USE_PLATFORM_DELAY
49 //#define USE_NDELAY
50
51 //#define DEBUG
52 //#define VERBOSE
53 /* Transfer descriptors. See dump_ptd() for printout format  */
54 //#define PTD_TRACE
55 /* enqueuing/finishing log of urbs */
56 //#define URB_TRACE
57
58 #include <linux/config.h>
59 #include <linux/module.h>
60 #include <linux/moduleparam.h>
61 #include <linux/kernel.h>
62 #include <linux/delay.h>
63 #include <linux/ioport.h>
64 #include <linux/sched.h>
65 #include <linux/slab.h>
66 #include <linux/smp_lock.h>
67 #include <linux/errno.h>
68 #include <linux/init.h>
69 #include <linux/list.h>
70 #include <linux/interrupt.h>
71 #include <linux/usb.h>
72 #include <linux/usb_isp116x.h>
73
74 #include <asm/io.h>
75 #include <asm/irq.h>
76 #include <asm/system.h>
77 #include <asm/byteorder.h>
78
79 #ifndef DEBUG
80 #       define  STUB_DEBUG_FILE
81 #endif
82
83 #include "../core/hcd.h"
84 #include "isp116x.h"
85
86 #define DRIVER_VERSION  "08 Apr 2005"
87 #define DRIVER_DESC     "ISP116x USB Host Controller Driver"
88
89 MODULE_DESCRIPTION(DRIVER_DESC);
90 MODULE_LICENSE("GPL");
91
92 static const char hcd_name[] = "isp116x-hcd";
93
94 /*-----------------------------------------------------------------*/
95
96 /*
97   Write len bytes to fifo, pad till 32-bit boundary
98  */
99 static void write_ptddata_to_fifo(struct isp116x *isp116x, void *buf, int len)
100 {
101         u8 *dp = (u8 *) buf;
102         u16 *dp2 = (u16 *) buf;
103         u16 w;
104         int quot = len % 4;
105
106         if ((unsigned long)dp2 & 1) {
107                 /* not aligned */
108                 for (; len > 1; len -= 2) {
109                         w = *dp++;
110                         w |= *dp++ << 8;
111                         isp116x_raw_write_data16(isp116x, w);
112                 }
113                 if (len)
114                         isp116x_write_data16(isp116x, (u16) * dp);
115         } else {
116                 /* aligned */
117                 for (; len > 1; len -= 2)
118                         isp116x_raw_write_data16(isp116x, *dp2++);
119                 if (len)
120                         isp116x_write_data16(isp116x, 0xff & *((u8 *) dp2));
121         }
122         if (quot == 1 || quot == 2)
123                 isp116x_raw_write_data16(isp116x, 0);
124 }
125
126 /*
127   Read len bytes from fifo and then read till 32-bit boundary.
128  */
129 static void read_ptddata_from_fifo(struct isp116x *isp116x, void *buf, int len)
130 {
131         u8 *dp = (u8 *) buf;
132         u16 *dp2 = (u16 *) buf;
133         u16 w;
134         int quot = len % 4;
135
136         if ((unsigned long)dp2 & 1) {
137                 /* not aligned */
138                 for (; len > 1; len -= 2) {
139                         w = isp116x_raw_read_data16(isp116x);
140                         *dp++ = w & 0xff;
141                         *dp++ = (w >> 8) & 0xff;
142                 }
143                 if (len)
144                         *dp = 0xff & isp116x_read_data16(isp116x);
145         } else {
146                 /* aligned */
147                 for (; len > 1; len -= 2)
148                         *dp2++ = isp116x_raw_read_data16(isp116x);
149                 if (len)
150                         *(u8 *) dp2 = 0xff & isp116x_read_data16(isp116x);
151         }
152         if (quot == 1 || quot == 2)
153                 isp116x_raw_read_data16(isp116x);
154 }
155
156 /*
157   Write ptd's and data for scheduled transfers into
158   the fifo ram. Fifo must be empty and ready.
159 */
160 static void pack_fifo(struct isp116x *isp116x)
161 {
162         struct isp116x_ep *ep;
163         struct ptd *ptd;
164         int buflen = isp116x->atl_last_dir == PTD_DIR_IN
165             ? isp116x->atl_bufshrt : isp116x->atl_buflen;
166         int ptd_count = 0;
167
168         isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
169         isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
170         isp116x_write_addr(isp116x, HCATLPORT | ISP116x_WRITE_OFFSET);
171         for (ep = isp116x->atl_active; ep; ep = ep->active) {
172                 ++ptd_count;
173                 ptd = &ep->ptd;
174                 dump_ptd(ptd);
175                 dump_ptd_out_data(ptd, ep->data);
176                 isp116x_write_data16(isp116x, ptd->count);
177                 isp116x_write_data16(isp116x, ptd->mps);
178                 isp116x_write_data16(isp116x, ptd->len);
179                 isp116x_write_data16(isp116x, ptd->faddr);
180                 buflen -= sizeof(struct ptd);
181                 /* Skip writing data for last IN PTD */
182                 if (ep->active || (isp116x->atl_last_dir != PTD_DIR_IN)) {
183                         write_ptddata_to_fifo(isp116x, ep->data, ep->length);
184                         buflen -= ALIGN(ep->length, 4);
185                 }
186         }
187         BUG_ON(buflen);
188 }
189
190 /*
191   Read the processed ptd's and data from fifo ram back to
192   URBs' buffers. Fifo must be full and done
193 */
194 static void unpack_fifo(struct isp116x *isp116x)
195 {
196         struct isp116x_ep *ep;
197         struct ptd *ptd;
198         int buflen = isp116x->atl_last_dir == PTD_DIR_IN
199             ? isp116x->atl_buflen : isp116x->atl_bufshrt;
200
201         isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
202         isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
203         isp116x_write_addr(isp116x, HCATLPORT);
204         for (ep = isp116x->atl_active; ep; ep = ep->active) {
205                 ptd = &ep->ptd;
206                 ptd->count = isp116x_read_data16(isp116x);
207                 ptd->mps = isp116x_read_data16(isp116x);
208                 ptd->len = isp116x_read_data16(isp116x);
209                 ptd->faddr = isp116x_read_data16(isp116x);
210                 buflen -= sizeof(struct ptd);
211                 /* Skip reading data for last Setup or Out PTD */
212                 if (ep->active || (isp116x->atl_last_dir == PTD_DIR_IN)) {
213                         read_ptddata_from_fifo(isp116x, ep->data, ep->length);
214                         buflen -= ALIGN(ep->length, 4);
215                 }
216                 dump_ptd(ptd);
217                 dump_ptd_in_data(ptd, ep->data);
218         }
219         BUG_ON(buflen);
220 }
221
222 /*---------------------------------------------------------------*/
223
224 /*
225   Set up PTD's.
226 */
227 static void preproc_atl_queue(struct isp116x *isp116x)
228 {
229         struct isp116x_ep *ep;
230         struct urb *urb;
231         struct ptd *ptd;
232         u16 toggle = 0, dir = PTD_DIR_SETUP, len;
233
234         for (ep = isp116x->atl_active; ep; ep = ep->active) {
235                 BUG_ON(list_empty(&ep->hep->urb_list));
236                 urb = container_of(ep->hep->urb_list.next,
237                                    struct urb, urb_list);
238                 ptd = &ep->ptd;
239                 len = ep->length;
240                 spin_lock(&urb->lock);
241                 ep->data = (unsigned char *)urb->transfer_buffer
242                     + urb->actual_length;
243
244                 switch (ep->nextpid) {
245                 case USB_PID_IN:
246                         toggle = usb_gettoggle(urb->dev, ep->epnum, 0);
247                         dir = PTD_DIR_IN;
248                         break;
249                 case USB_PID_OUT:
250                         toggle = usb_gettoggle(urb->dev, ep->epnum, 1);
251                         dir = PTD_DIR_OUT;
252                         break;
253                 case USB_PID_SETUP:
254                         len = sizeof(struct usb_ctrlrequest);
255                         ep->data = urb->setup_packet;
256                         break;
257                 case USB_PID_ACK:
258                         toggle = 1;
259                         len = 0;
260                         dir = (urb->transfer_buffer_length
261                                && usb_pipein(urb->pipe))
262                             ? PTD_DIR_OUT : PTD_DIR_IN;
263                         break;
264                 default:
265                         ERR("%s %d: ep->nextpid %d\n", __func__, __LINE__,
266                             ep->nextpid);
267                         BUG();
268                 }
269
270                 ptd->count = PTD_CC_MSK | PTD_ACTIVE_MSK | PTD_TOGGLE(toggle);
271                 ptd->mps = PTD_MPS(ep->maxpacket)
272                     | PTD_SPD(urb->dev->speed == USB_SPEED_LOW)
273                     | PTD_EP(ep->epnum);
274                 ptd->len = PTD_LEN(len) | PTD_DIR(dir);
275                 ptd->faddr = PTD_FA(usb_pipedevice(urb->pipe));
276                 spin_unlock(&urb->lock);
277                 if (!ep->active) {
278                         ptd->mps |= PTD_LAST_MSK;
279                         isp116x->atl_last_dir = dir;
280                 }
281                 isp116x->atl_bufshrt = sizeof(struct ptd) + isp116x->atl_buflen;
282                 isp116x->atl_buflen = isp116x->atl_bufshrt + ALIGN(len, 4);
283         }
284 }
285
286 /*
287   Analyze transfer results, handle partial transfers and errors
288 */
289 static void postproc_atl_queue(struct isp116x *isp116x)
290 {
291         struct isp116x_ep *ep;
292         struct urb *urb;
293         struct usb_device *udev;
294         struct ptd *ptd;
295         int short_not_ok;
296         u8 cc;
297
298         for (ep = isp116x->atl_active; ep; ep = ep->active) {
299                 BUG_ON(list_empty(&ep->hep->urb_list));
300                 urb =
301                     container_of(ep->hep->urb_list.next, struct urb, urb_list);
302                 udev = urb->dev;
303                 ptd = &ep->ptd;
304                 cc = PTD_GET_CC(ptd);
305
306                 spin_lock(&urb->lock);
307                 short_not_ok = 1;
308
309                 /* Data underrun is special. For allowed underrun
310                    we clear the error and continue as normal. For
311                    forbidden underrun we finish the DATA stage
312                    immediately while for control transfer,
313                    we do a STATUS stage. */
314                 if (cc == TD_DATAUNDERRUN) {
315                         if (!(urb->transfer_flags & URB_SHORT_NOT_OK)) {
316                                 DBG("Allowed data underrun\n");
317                                 cc = TD_CC_NOERROR;
318                                 short_not_ok = 0;
319                         } else {
320                                 ep->error_count = 1;
321                                 if (usb_pipecontrol(urb->pipe))
322                                         ep->nextpid = USB_PID_ACK;
323                                 else
324                                         usb_settoggle(udev, ep->epnum,
325                                                       ep->nextpid ==
326                                                       USB_PID_OUT,
327                                                       PTD_GET_TOGGLE(ptd) ^ 1);
328                                 urb->status = cc_to_error[TD_DATAUNDERRUN];
329                                 spin_unlock(&urb->lock);
330                                 continue;
331                         }
332                 }
333                 /* Keep underrun error through the STATUS stage */
334                 if (urb->status == cc_to_error[TD_DATAUNDERRUN])
335                         cc = TD_DATAUNDERRUN;
336
337                 if (cc != TD_CC_NOERROR && cc != TD_NOTACCESSED
338                     && (++ep->error_count >= 3 || cc == TD_CC_STALL
339                         || cc == TD_DATAOVERRUN)) {
340                         if (urb->status == -EINPROGRESS)
341                                 urb->status = cc_to_error[cc];
342                         if (ep->nextpid == USB_PID_ACK)
343                                 ep->nextpid = 0;
344                         spin_unlock(&urb->lock);
345                         continue;
346                 }
347                 /* According to usb spec, zero-length Int transfer signals
348                    finishing of the urb. Hey, does this apply only
349                    for IN endpoints? */
350                 if (usb_pipeint(urb->pipe) && !PTD_GET_LEN(ptd)) {
351                         if (urb->status == -EINPROGRESS)
352                                 urb->status = 0;
353                         spin_unlock(&urb->lock);
354                         continue;
355                 }
356
357                 /* Relax after previously failed, but later succeeded
358                    or correctly NAK'ed retransmission attempt */
359                 if (ep->error_count
360                     && (cc == TD_CC_NOERROR || cc == TD_NOTACCESSED))
361                         ep->error_count = 0;
362
363                 /* Take into account idiosyncracies of the isp116x chip
364                    regarding toggle bit for failed transfers */
365                 if (ep->nextpid == USB_PID_OUT)
366                         usb_settoggle(udev, ep->epnum, 1, PTD_GET_TOGGLE(ptd)
367                                       ^ (ep->error_count > 0));
368                 else if (ep->nextpid == USB_PID_IN)
369                         usb_settoggle(udev, ep->epnum, 0, PTD_GET_TOGGLE(ptd)
370                                       ^ (ep->error_count > 0));
371
372                 switch (ep->nextpid) {
373                 case USB_PID_IN:
374                 case USB_PID_OUT:
375                         urb->actual_length += PTD_GET_COUNT(ptd);
376                         if (PTD_GET_ACTIVE(ptd)
377                             || (cc != TD_CC_NOERROR && cc < 0x0E))
378                                 break;
379                         if (urb->transfer_buffer_length != urb->actual_length) {
380                                 if (short_not_ok)
381                                         break;
382                         } else {
383                                 if (urb->transfer_flags & URB_ZERO_PACKET
384                                     && ep->nextpid == USB_PID_OUT
385                                     && !(PTD_GET_COUNT(ptd) % ep->maxpacket)) {
386                                         DBG("Zero packet requested\n");
387                                         break;
388                                 }
389                         }
390                         /* All data for this URB is transferred, let's finish */
391                         if (usb_pipecontrol(urb->pipe))
392                                 ep->nextpid = USB_PID_ACK;
393                         else if (urb->status == -EINPROGRESS)
394                                 urb->status = 0;
395                         break;
396                 case USB_PID_SETUP:
397                         if (PTD_GET_ACTIVE(ptd)
398                             || (cc != TD_CC_NOERROR && cc < 0x0E))
399                                 break;
400                         if (urb->transfer_buffer_length == urb->actual_length)
401                                 ep->nextpid = USB_PID_ACK;
402                         else if (usb_pipeout(urb->pipe)) {
403                                 usb_settoggle(udev, 0, 1, 1);
404                                 ep->nextpid = USB_PID_OUT;
405                         } else {
406                                 usb_settoggle(udev, 0, 0, 1);
407                                 ep->nextpid = USB_PID_IN;
408                         }
409                         break;
410                 case USB_PID_ACK:
411                         if (PTD_GET_ACTIVE(ptd)
412                             || (cc != TD_CC_NOERROR && cc < 0x0E))
413                                 break;
414                         if (urb->status == -EINPROGRESS)
415                                 urb->status = 0;
416                         ep->nextpid = 0;
417                         break;
418                 default:
419                         BUG_ON(1);
420                 }
421                 spin_unlock(&urb->lock);
422         }
423 }
424
425 /*
426   Take done or failed requests out of schedule. Give back
427   processed urbs.
428 */
429 static void finish_request(struct isp116x *isp116x, struct isp116x_ep *ep,
430                            struct urb *urb, struct pt_regs *regs)
431 __releases(isp116x->lock) __acquires(isp116x->lock)
432 {
433         unsigned i;
434
435         urb->hcpriv = NULL;
436         ep->error_count = 0;
437
438         if (usb_pipecontrol(urb->pipe))
439                 ep->nextpid = USB_PID_SETUP;
440
441         urb_dbg(urb, "Finish");
442
443         spin_unlock(&isp116x->lock);
444         usb_hcd_giveback_urb(isp116x_to_hcd(isp116x), urb, regs);
445         spin_lock(&isp116x->lock);
446
447         /* take idle endpoints out of the schedule */
448         if (!list_empty(&ep->hep->urb_list))
449                 return;
450
451         /* async deschedule */
452         if (!list_empty(&ep->schedule)) {
453                 list_del_init(&ep->schedule);
454                 return;
455         }
456
457         /* periodic deschedule */
458         DBG("deschedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
459         for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
460                 struct isp116x_ep *temp;
461                 struct isp116x_ep **prev = &isp116x->periodic[i];
462
463                 while (*prev && ((temp = *prev) != ep))
464                         prev = &temp->next;
465                 if (*prev)
466                         *prev = ep->next;
467                 isp116x->load[i] -= ep->load;
468         }
469         ep->branch = PERIODIC_SIZE;
470         isp116x_to_hcd(isp116x)->self.bandwidth_allocated -=
471             ep->load / ep->period;
472
473         /* switch irq type? */
474         if (!--isp116x->periodic_count) {
475                 isp116x->irqenb &= ~HCuPINT_SOF;
476                 isp116x->irqenb |= HCuPINT_ATL;
477         }
478 }
479
480 /*
481   Scan transfer lists, schedule transfers, send data off
482   to chip.
483  */
484 static void start_atl_transfers(struct isp116x *isp116x)
485 {
486         struct isp116x_ep *last_ep = NULL, *ep;
487         struct urb *urb;
488         u16 load = 0;
489         int len, index, speed, byte_time;
490
491         if (atomic_read(&isp116x->atl_finishing))
492                 return;
493
494         if (!HC_IS_RUNNING(isp116x_to_hcd(isp116x)->state))
495                 return;
496
497         /* FIFO not empty? */
498         if (isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_FULL)
499                 return;
500
501         isp116x->atl_active = NULL;
502         isp116x->atl_buflen = isp116x->atl_bufshrt = 0;
503
504         /* Schedule int transfers */
505         if (isp116x->periodic_count) {
506                 isp116x->fmindex = index =
507                     (isp116x->fmindex + 1) & (PERIODIC_SIZE - 1);
508                 if ((load = isp116x->load[index])) {
509                         /* Bring all int transfers for this frame
510                            into the active queue */
511                         isp116x->atl_active = last_ep =
512                             isp116x->periodic[index];
513                         while (last_ep->next)
514                                 last_ep = (last_ep->active = last_ep->next);
515                         last_ep->active = NULL;
516                 }
517         }
518
519         /* Schedule control/bulk transfers */
520         list_for_each_entry(ep, &isp116x->async, schedule) {
521                 urb = container_of(ep->hep->urb_list.next,
522                                    struct urb, urb_list);
523                 speed = urb->dev->speed;
524                 byte_time = speed == USB_SPEED_LOW
525                     ? BYTE_TIME_LOWSPEED : BYTE_TIME_FULLSPEED;
526
527                 if (ep->nextpid == USB_PID_SETUP) {
528                         len = sizeof(struct usb_ctrlrequest);
529                 } else if (ep->nextpid == USB_PID_ACK) {
530                         len = 0;
531                 } else {
532                         /* Find current free length ... */
533                         len = (MAX_LOAD_LIMIT - load) / byte_time;
534
535                         /* ... then limit it to configured max size ... */
536                         len = min(len, speed == USB_SPEED_LOW ?
537                                   MAX_TRANSFER_SIZE_LOWSPEED :
538                                   MAX_TRANSFER_SIZE_FULLSPEED);
539
540                         /* ... and finally cut to the multiple of MaxPacketSize,
541                            or to the real length if there's enough room. */
542                         if (len <
543                             (urb->transfer_buffer_length -
544                              urb->actual_length)) {
545                                 len -= len % ep->maxpacket;
546                                 if (!len)
547                                         continue;
548                         } else
549                                 len = urb->transfer_buffer_length -
550                                     urb->actual_length;
551                         BUG_ON(len < 0);
552                 }
553
554                 load += len * byte_time;
555                 if (load > MAX_LOAD_LIMIT)
556                         break;
557
558                 ep->active = NULL;
559                 ep->length = len;
560                 if (last_ep)
561                         last_ep->active = ep;
562                 else
563                         isp116x->atl_active = ep;
564                 last_ep = ep;
565         }
566
567         /* Avoid starving of endpoints */
568         if ((&isp116x->async)->next != (&isp116x->async)->prev)
569                 list_move(&isp116x->async, (&isp116x->async)->next);
570
571         if (isp116x->atl_active) {
572                 preproc_atl_queue(isp116x);
573                 pack_fifo(isp116x);
574         }
575 }
576
577 /*
578   Finish the processed transfers
579 */
580 static void finish_atl_transfers(struct isp116x *isp116x, struct pt_regs *regs)
581 {
582         struct isp116x_ep *ep;
583         struct urb *urb;
584
585         if (!isp116x->atl_active)
586                 return;
587         /* Fifo not ready? */
588         if (!(isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_DONE))
589                 return;
590
591         atomic_inc(&isp116x->atl_finishing);
592         unpack_fifo(isp116x);
593         postproc_atl_queue(isp116x);
594         for (ep = isp116x->atl_active; ep; ep = ep->active) {
595                 urb =
596                     container_of(ep->hep->urb_list.next, struct urb, urb_list);
597                 /* USB_PID_ACK check here avoids finishing of
598                    control transfers, for which TD_DATAUNDERRUN
599                    occured, while URB_SHORT_NOT_OK was set */
600                 if (urb && urb->status != -EINPROGRESS
601                     && ep->nextpid != USB_PID_ACK)
602                         finish_request(isp116x, ep, urb, regs);
603         }
604         atomic_dec(&isp116x->atl_finishing);
605 }
606
607 static irqreturn_t isp116x_irq(struct usb_hcd *hcd, struct pt_regs *regs)
608 {
609         struct isp116x *isp116x = hcd_to_isp116x(hcd);
610         u16 irqstat;
611         irqreturn_t ret = IRQ_NONE;
612
613         spin_lock(&isp116x->lock);
614         isp116x_write_reg16(isp116x, HCuPINTENB, 0);
615         irqstat = isp116x_read_reg16(isp116x, HCuPINT);
616         isp116x_write_reg16(isp116x, HCuPINT, irqstat);
617
618         if (irqstat & (HCuPINT_ATL | HCuPINT_SOF)) {
619                 ret = IRQ_HANDLED;
620                 finish_atl_transfers(isp116x, regs);
621         }
622
623         if (irqstat & HCuPINT_OPR) {
624                 u32 intstat = isp116x_read_reg32(isp116x, HCINTSTAT);
625                 isp116x_write_reg32(isp116x, HCINTSTAT, intstat);
626                 if (intstat & HCINT_UE) {
627                         ERR("Unrecoverable error\n");
628                         /* What should we do here? Reset?  */
629                 }
630                 if (intstat & HCINT_RHSC) {
631                         isp116x->rhstatus =
632                             isp116x_read_reg32(isp116x, HCRHSTATUS);
633                         isp116x->rhport[0] =
634                             isp116x_read_reg32(isp116x, HCRHPORT1);
635                         isp116x->rhport[1] =
636                             isp116x_read_reg32(isp116x, HCRHPORT2);
637                 }
638                 if (intstat & HCINT_RD) {
639                         DBG("---- remote wakeup\n");
640                         schedule_work(&isp116x->rh_resume);
641                         ret = IRQ_HANDLED;
642                 }
643                 irqstat &= ~HCuPINT_OPR;
644                 ret = IRQ_HANDLED;
645         }
646
647         if (irqstat & (HCuPINT_ATL | HCuPINT_SOF)) {
648                 start_atl_transfers(isp116x);
649         }
650
651         isp116x_write_reg16(isp116x, HCuPINTENB, isp116x->irqenb);
652         spin_unlock(&isp116x->lock);
653         return ret;
654 }
655
656 /*-----------------------------------------------------------------*/
657
658 /* usb 1.1 says max 90% of a frame is available for periodic transfers.
659  * this driver doesn't promise that much since it's got to handle an
660  * IRQ per packet; irq handling latencies also use up that time.
661  */
662
663 /* out of 1000 us */
664 #define MAX_PERIODIC_LOAD       600
665 static int balance(struct isp116x *isp116x, u16 period, u16 load)
666 {
667         int i, branch = -ENOSPC;
668
669         /* search for the least loaded schedule branch of that period
670            which has enough bandwidth left unreserved. */
671         for (i = 0; i < period; i++) {
672                 if (branch < 0 || isp116x->load[branch] > isp116x->load[i]) {
673                         int j;
674
675                         for (j = i; j < PERIODIC_SIZE; j += period) {
676                                 if ((isp116x->load[j] + load)
677                                     > MAX_PERIODIC_LOAD)
678                                         break;
679                         }
680                         if (j < PERIODIC_SIZE)
681                                 continue;
682                         branch = i;
683                 }
684         }
685         return branch;
686 }
687
688 /* NB! ALL the code above this point runs with isp116x->lock
689    held, irqs off
690 */
691
692 /*-----------------------------------------------------------------*/
693
694 static int isp116x_urb_enqueue(struct usb_hcd *hcd,
695                                struct usb_host_endpoint *hep, struct urb *urb,
696                                unsigned mem_flags)
697 {
698         struct isp116x *isp116x = hcd_to_isp116x(hcd);
699         struct usb_device *udev = urb->dev;
700         unsigned int pipe = urb->pipe;
701         int is_out = !usb_pipein(pipe);
702         int type = usb_pipetype(pipe);
703         int epnum = usb_pipeendpoint(pipe);
704         struct isp116x_ep *ep = NULL;
705         unsigned long flags;
706         int i;
707         int ret = 0;
708
709         urb_dbg(urb, "Enqueue");
710
711         if (type == PIPE_ISOCHRONOUS) {
712                 ERR("Isochronous transfers not supported\n");
713                 urb_dbg(urb, "Refused to enqueue");
714                 return -ENXIO;
715         }
716         /* avoid all allocations within spinlocks: request or endpoint */
717         if (!hep->hcpriv) {
718                 ep = kcalloc(1, sizeof *ep, mem_flags);
719                 if (!ep)
720                         return -ENOMEM;
721         }
722
723         spin_lock_irqsave(&isp116x->lock, flags);
724         if (!HC_IS_RUNNING(hcd->state)) {
725                 ret = -ENODEV;
726                 goto fail;
727         }
728
729         if (hep->hcpriv)
730                 ep = hep->hcpriv;
731         else {
732                 INIT_LIST_HEAD(&ep->schedule);
733                 ep->udev = usb_get_dev(udev);
734                 ep->epnum = epnum;
735                 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
736                 usb_settoggle(udev, epnum, is_out, 0);
737
738                 if (type == PIPE_CONTROL) {
739                         ep->nextpid = USB_PID_SETUP;
740                 } else if (is_out) {
741                         ep->nextpid = USB_PID_OUT;
742                 } else {
743                         ep->nextpid = USB_PID_IN;
744                 }
745
746                 if (urb->interval) {
747                         /*
748                            With INT URBs submitted, the driver works with SOF
749                            interrupt enabled and ATL interrupt disabled. After
750                            the PTDs are written to fifo ram, the chip starts
751                            fifo processing and usb transfers after the next
752                            SOF and continues until the transfers are finished
753                            (succeeded or failed) or the frame ends. Therefore,
754                            the transfers occur only in every second frame,
755                            while fifo reading/writing and data processing
756                            occur in every other second frame. */
757                         if (urb->interval < 2)
758                                 urb->interval = 2;
759                         if (urb->interval > 2 * PERIODIC_SIZE)
760                                 urb->interval = 2 * PERIODIC_SIZE;
761                         ep->period = urb->interval >> 1;
762                         ep->branch = PERIODIC_SIZE;
763                         ep->load = usb_calc_bus_time(udev->speed,
764                                                      !is_out,
765                                                      (type == PIPE_ISOCHRONOUS),
766                                                      usb_maxpacket(udev, pipe,
767                                                                    is_out)) /
768                             1000;
769                 }
770                 hep->hcpriv = ep;
771                 ep->hep = hep;
772         }
773
774         /* maybe put endpoint into schedule */
775         switch (type) {
776         case PIPE_CONTROL:
777         case PIPE_BULK:
778                 if (list_empty(&ep->schedule))
779                         list_add_tail(&ep->schedule, &isp116x->async);
780                 break;
781         case PIPE_INTERRUPT:
782                 urb->interval = ep->period;
783                 ep->length = min((int)ep->maxpacket,
784                                  urb->transfer_buffer_length);
785
786                 /* urb submitted for already existing endpoint */
787                 if (ep->branch < PERIODIC_SIZE)
788                         break;
789
790                 ret = ep->branch = balance(isp116x, ep->period, ep->load);
791                 if (ret < 0)
792                         goto fail;
793                 ret = 0;
794
795                 urb->start_frame = (isp116x->fmindex & (PERIODIC_SIZE - 1))
796                     + ep->branch;
797
798                 /* sort each schedule branch by period (slow before fast)
799                    to share the faster parts of the tree without needing
800                    dummy/placeholder nodes */
801                 DBG("schedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
802                 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
803                         struct isp116x_ep **prev = &isp116x->periodic[i];
804                         struct isp116x_ep *here = *prev;
805
806                         while (here && ep != here) {
807                                 if (ep->period > here->period)
808                                         break;
809                                 prev = &here->next;
810                                 here = *prev;
811                         }
812                         if (ep != here) {
813                                 ep->next = here;
814                                 *prev = ep;
815                         }
816                         isp116x->load[i] += ep->load;
817                 }
818                 hcd->self.bandwidth_allocated += ep->load / ep->period;
819
820                 /* switch over to SOFint */
821                 if (!isp116x->periodic_count++) {
822                         isp116x->irqenb &= ~HCuPINT_ATL;
823                         isp116x->irqenb |= HCuPINT_SOF;
824                         isp116x_write_reg16(isp116x, HCuPINTENB,
825                                             isp116x->irqenb);
826                 }
827         }
828
829         /* in case of unlink-during-submit */
830         spin_lock(&urb->lock);
831         if (urb->status != -EINPROGRESS) {
832                 spin_unlock(&urb->lock);
833                 finish_request(isp116x, ep, urb, NULL);
834                 ret = 0;
835                 goto fail;
836         }
837         urb->hcpriv = hep;
838         spin_unlock(&urb->lock);
839         start_atl_transfers(isp116x);
840
841       fail:
842         spin_unlock_irqrestore(&isp116x->lock, flags);
843         return ret;
844 }
845
846 /*
847    Dequeue URBs.
848 */
849 static int isp116x_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
850 {
851         struct isp116x *isp116x = hcd_to_isp116x(hcd);
852         struct usb_host_endpoint *hep;
853         struct isp116x_ep *ep, *ep_act;
854         unsigned long flags;
855
856         spin_lock_irqsave(&isp116x->lock, flags);
857         hep = urb->hcpriv;
858         /* URB already unlinked (or never linked)? */
859         if (!hep) {
860                 spin_unlock_irqrestore(&isp116x->lock, flags);
861                 return 0;
862         }
863         ep = hep->hcpriv;
864         WARN_ON(hep != ep->hep);
865
866         /* In front of queue? */
867         if (ep->hep->urb_list.next == &urb->urb_list)
868                 /* active? */
869                 for (ep_act = isp116x->atl_active; ep_act;
870                      ep_act = ep_act->active)
871                         if (ep_act == ep) {
872                                 VDBG("dequeue, urb %p active; wait for irq\n",
873                                      urb);
874                                 urb = NULL;
875                                 break;
876                         }
877
878         if (urb)
879                 finish_request(isp116x, ep, urb, NULL);
880
881         spin_unlock_irqrestore(&isp116x->lock, flags);
882         return 0;
883 }
884
885 static void isp116x_endpoint_disable(struct usb_hcd *hcd,
886                                      struct usb_host_endpoint *hep)
887 {
888         int i;
889         struct isp116x_ep *ep = hep->hcpriv;;
890
891         if (!ep)
892                 return;
893
894         /* assume we'd just wait for the irq */
895         for (i = 0; i < 100 && !list_empty(&hep->urb_list); i++)
896                 msleep(3);
897         if (!list_empty(&hep->urb_list))
898                 WARN("ep %p not empty?\n", ep);
899
900         usb_put_dev(ep->udev);
901         kfree(ep);
902         hep->hcpriv = NULL;
903 }
904
905 static int isp116x_get_frame(struct usb_hcd *hcd)
906 {
907         struct isp116x *isp116x = hcd_to_isp116x(hcd);
908         u32 fmnum;
909         unsigned long flags;
910
911         spin_lock_irqsave(&isp116x->lock, flags);
912         fmnum = isp116x_read_reg32(isp116x, HCFMNUM);
913         spin_unlock_irqrestore(&isp116x->lock, flags);
914         return (int)fmnum;
915 }
916
917 /*----------------------------------------------------------------*/
918
919 /*
920   Adapted from ohci-hub.c. Currently we don't support autosuspend.
921 */
922 static int isp116x_hub_status_data(struct usb_hcd *hcd, char *buf)
923 {
924         struct isp116x *isp116x = hcd_to_isp116x(hcd);
925         int ports, i, changed = 0;
926
927         if (!HC_IS_RUNNING(hcd->state))
928                 return -ESHUTDOWN;
929
930         ports = isp116x->rhdesca & RH_A_NDP;
931
932         /* init status */
933         if (isp116x->rhstatus & (RH_HS_LPSC | RH_HS_OCIC))
934                 buf[0] = changed = 1;
935         else
936                 buf[0] = 0;
937
938         for (i = 0; i < ports; i++) {
939                 u32 status = isp116x->rhport[i];
940
941                 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
942                               | RH_PS_OCIC | RH_PS_PRSC)) {
943                         changed = 1;
944                         buf[0] |= 1 << (i + 1);
945                         continue;
946                 }
947         }
948         return changed;
949 }
950
951 static void isp116x_hub_descriptor(struct isp116x *isp116x,
952                                    struct usb_hub_descriptor *desc)
953 {
954         u32 reg = isp116x->rhdesca;
955
956         desc->bDescriptorType = 0x29;
957         desc->bDescLength = 9;
958         desc->bHubContrCurrent = 0;
959         desc->bNbrPorts = (u8) (reg & 0x3);
960         /* Power switching, device type, overcurrent. */
961         desc->wHubCharacteristics =
962             (__force __u16) cpu_to_le16((u16) ((reg >> 8) & 0x1f));
963         desc->bPwrOn2PwrGood = (u8) ((reg >> 24) & 0xff);
964         /* two bitmaps:  ports removable, and legacy PortPwrCtrlMask */
965         desc->bitmap[0] = desc->bNbrPorts == 1 ? 1 << 1 : 3 << 1;
966         desc->bitmap[1] = ~0;
967 }
968
969 /* Perform reset of a given port.
970    It would be great to just start the reset and let the
971    USB core to clear the reset in due time. However,
972    root hub ports should be reset for at least 50 ms, while
973    our chip stays in reset for about 10 ms. I.e., we must
974    repeatedly reset it ourself here.
975 */
976 static inline void root_port_reset(struct isp116x *isp116x, unsigned port)
977 {
978         u32 tmp;
979         unsigned long flags, t;
980
981         /* Root hub reset should be 50 ms, but some devices
982            want it even longer. */
983         t = jiffies + msecs_to_jiffies(100);
984
985         while (time_before(jiffies, t)) {
986                 spin_lock_irqsave(&isp116x->lock, flags);
987                 /* spin until any current reset finishes */
988                 for (;;) {
989                         tmp = isp116x_read_reg32(isp116x, port ?
990                                                  HCRHPORT2 : HCRHPORT1);
991                         if (!(tmp & RH_PS_PRS))
992                                 break;
993                         udelay(500);
994                 }
995                 /* Don't reset a disconnected port */
996                 if (!(tmp & RH_PS_CCS)) {
997                         spin_unlock_irqrestore(&isp116x->lock, flags);
998                         break;
999                 }
1000                 /* Reset lasts 10ms (claims datasheet) */
1001                 isp116x_write_reg32(isp116x, port ? HCRHPORT2 :
1002                                     HCRHPORT1, (RH_PS_PRS));
1003                 spin_unlock_irqrestore(&isp116x->lock, flags);
1004                 msleep(10);
1005         }
1006 }
1007
1008 /* Adapted from ohci-hub.c */
1009 static int isp116x_hub_control(struct usb_hcd *hcd,
1010                                u16 typeReq,
1011                                u16 wValue, u16 wIndex, char *buf, u16 wLength)
1012 {
1013         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1014         int ret = 0;
1015         unsigned long flags;
1016         int ports = isp116x->rhdesca & RH_A_NDP;
1017         u32 tmp = 0;
1018
1019         switch (typeReq) {
1020         case ClearHubFeature:
1021                 DBG("ClearHubFeature: ");
1022                 switch (wValue) {
1023                 case C_HUB_OVER_CURRENT:
1024                         DBG("C_HUB_OVER_CURRENT\n");
1025                         spin_lock_irqsave(&isp116x->lock, flags);
1026                         isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_OCIC);
1027                         spin_unlock_irqrestore(&isp116x->lock, flags);
1028                 case C_HUB_LOCAL_POWER:
1029                         DBG("C_HUB_LOCAL_POWER\n");
1030                         break;
1031                 default:
1032                         goto error;
1033                 }
1034                 break;
1035         case SetHubFeature:
1036                 DBG("SetHubFeature: ");
1037                 switch (wValue) {
1038                 case C_HUB_OVER_CURRENT:
1039                 case C_HUB_LOCAL_POWER:
1040                         DBG("C_HUB_OVER_CURRENT or C_HUB_LOCAL_POWER\n");
1041                         break;
1042                 default:
1043                         goto error;
1044                 }
1045                 break;
1046         case GetHubDescriptor:
1047                 DBG("GetHubDescriptor\n");
1048                 isp116x_hub_descriptor(isp116x,
1049                                        (struct usb_hub_descriptor *)buf);
1050                 break;
1051         case GetHubStatus:
1052                 DBG("GetHubStatus\n");
1053                 *(__le32 *) buf = 0;
1054                 break;
1055         case GetPortStatus:
1056                 DBG("GetPortStatus\n");
1057                 if (!wIndex || wIndex > ports)
1058                         goto error;
1059                 tmp = isp116x->rhport[--wIndex];
1060                 *(__le32 *) buf = cpu_to_le32(tmp);
1061                 DBG("GetPortStatus: port[%d]  %08x\n", wIndex + 1, tmp);
1062                 break;
1063         case ClearPortFeature:
1064                 DBG("ClearPortFeature: ");
1065                 if (!wIndex || wIndex > ports)
1066                         goto error;
1067                 wIndex--;
1068
1069                 switch (wValue) {
1070                 case USB_PORT_FEAT_ENABLE:
1071                         DBG("USB_PORT_FEAT_ENABLE\n");
1072                         tmp = RH_PS_CCS;
1073                         break;
1074                 case USB_PORT_FEAT_C_ENABLE:
1075                         DBG("USB_PORT_FEAT_C_ENABLE\n");
1076                         tmp = RH_PS_PESC;
1077                         break;
1078                 case USB_PORT_FEAT_SUSPEND:
1079                         DBG("USB_PORT_FEAT_SUSPEND\n");
1080                         tmp = RH_PS_POCI;
1081                         break;
1082                 case USB_PORT_FEAT_C_SUSPEND:
1083                         DBG("USB_PORT_FEAT_C_SUSPEND\n");
1084                         tmp = RH_PS_PSSC;
1085                         break;
1086                 case USB_PORT_FEAT_POWER:
1087                         DBG("USB_PORT_FEAT_POWER\n");
1088                         tmp = RH_PS_LSDA;
1089                         break;
1090                 case USB_PORT_FEAT_C_CONNECTION:
1091                         DBG("USB_PORT_FEAT_C_CONNECTION\n");
1092                         tmp = RH_PS_CSC;
1093                         break;
1094                 case USB_PORT_FEAT_C_OVER_CURRENT:
1095                         DBG("USB_PORT_FEAT_C_OVER_CURRENT\n");
1096                         tmp = RH_PS_OCIC;
1097                         break;
1098                 case USB_PORT_FEAT_C_RESET:
1099                         DBG("USB_PORT_FEAT_C_RESET\n");
1100                         tmp = RH_PS_PRSC;
1101                         break;
1102                 default:
1103                         goto error;
1104                 }
1105                 spin_lock_irqsave(&isp116x->lock, flags);
1106                 isp116x_write_reg32(isp116x, wIndex
1107                                     ? HCRHPORT2 : HCRHPORT1, tmp);
1108                 isp116x->rhport[wIndex] =
1109                     isp116x_read_reg32(isp116x, wIndex ? HCRHPORT2 : HCRHPORT1);
1110                 spin_unlock_irqrestore(&isp116x->lock, flags);
1111                 break;
1112         case SetPortFeature:
1113                 DBG("SetPortFeature: ");
1114                 if (!wIndex || wIndex > ports)
1115                         goto error;
1116                 wIndex--;
1117                 switch (wValue) {
1118                 case USB_PORT_FEAT_SUSPEND:
1119                         DBG("USB_PORT_FEAT_SUSPEND\n");
1120                         spin_lock_irqsave(&isp116x->lock, flags);
1121                         isp116x_write_reg32(isp116x, wIndex
1122                                             ? HCRHPORT2 : HCRHPORT1, RH_PS_PSS);
1123                         break;
1124                 case USB_PORT_FEAT_POWER:
1125                         DBG("USB_PORT_FEAT_POWER\n");
1126                         spin_lock_irqsave(&isp116x->lock, flags);
1127                         isp116x_write_reg32(isp116x, wIndex
1128                                             ? HCRHPORT2 : HCRHPORT1, RH_PS_PPS);
1129                         break;
1130                 case USB_PORT_FEAT_RESET:
1131                         DBG("USB_PORT_FEAT_RESET\n");
1132                         root_port_reset(isp116x, wIndex);
1133                         spin_lock_irqsave(&isp116x->lock, flags);
1134                         break;
1135                 default:
1136                         goto error;
1137                 }
1138                 isp116x->rhport[wIndex] =
1139                     isp116x_read_reg32(isp116x, wIndex ? HCRHPORT2 : HCRHPORT1);
1140                 spin_unlock_irqrestore(&isp116x->lock, flags);
1141                 break;
1142
1143         default:
1144               error:
1145                 /* "protocol stall" on error */
1146                 DBG("PROTOCOL STALL\n");
1147                 ret = -EPIPE;
1148         }
1149         return ret;
1150 }
1151
1152 #ifdef  CONFIG_PM
1153
1154 static int isp116x_hub_suspend(struct usb_hcd *hcd)
1155 {
1156         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1157         unsigned long flags;
1158         u32 val;
1159         int ret = 0;
1160
1161         spin_lock_irqsave(&isp116x->lock, flags);
1162
1163         val = isp116x_read_reg32(isp116x, HCCONTROL);
1164         switch (val & HCCONTROL_HCFS) {
1165         case HCCONTROL_USB_OPER:
1166                 hcd->state = HC_STATE_QUIESCING;
1167                 val &= (~HCCONTROL_HCFS & ~HCCONTROL_RWE);
1168                 val |= HCCONTROL_USB_SUSPEND;
1169                 if (hcd->remote_wakeup)
1170                         val |= HCCONTROL_RWE;
1171                 /* Wait for usb transfers to finish */
1172                 mdelay(2);
1173                 isp116x_write_reg32(isp116x, HCCONTROL, val);
1174                 hcd->state = HC_STATE_SUSPENDED;
1175                 /* Wait for devices to suspend */
1176                 mdelay(5);
1177         case HCCONTROL_USB_SUSPEND:
1178                 break;
1179         case HCCONTROL_USB_RESUME:
1180                 isp116x_write_reg32(isp116x, HCCONTROL,
1181                                     (val & ~HCCONTROL_HCFS) |
1182                                     HCCONTROL_USB_RESET);
1183         case HCCONTROL_USB_RESET:
1184                 ret = -EBUSY;
1185                 break;
1186         default:
1187                 ret = -EINVAL;
1188         }
1189
1190         spin_unlock_irqrestore(&isp116x->lock, flags);
1191         return ret;
1192 }
1193
1194 static int isp116x_hub_resume(struct usb_hcd *hcd)
1195 {
1196         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1197         u32 val;
1198         int ret = -EINPROGRESS;
1199
1200         msleep(5);
1201         spin_lock_irq(&isp116x->lock);
1202
1203         val = isp116x_read_reg32(isp116x, HCCONTROL);
1204         switch (val & HCCONTROL_HCFS) {
1205         case HCCONTROL_USB_SUSPEND:
1206                 val &= ~HCCONTROL_HCFS;
1207                 val |= HCCONTROL_USB_RESUME;
1208                 isp116x_write_reg32(isp116x, HCCONTROL, val);
1209         case HCCONTROL_USB_RESUME:
1210                 break;
1211         case HCCONTROL_USB_OPER:
1212                 /* Without setting power_state here the
1213                    SUSPENDED state won't be removed from
1214                    sysfs/usbN/power.state as a response to remote
1215                    wakeup. Maybe in the future. */
1216                 hcd->self.root_hub->dev.power.power_state = PMSG_ON;
1217                 ret = 0;
1218                 break;
1219         default:
1220                 ret = -EBUSY;
1221         }
1222
1223         if (ret != -EINPROGRESS) {
1224                 spin_unlock_irq(&isp116x->lock);
1225                 return ret;
1226         }
1227
1228         val = isp116x->rhdesca & RH_A_NDP;
1229         while (val--) {
1230                 u32 stat =
1231                     isp116x_read_reg32(isp116x, val ? HCRHPORT2 : HCRHPORT1);
1232                 /* force global, not selective, resume */
1233                 if (!(stat & RH_PS_PSS))
1234                         continue;
1235                 DBG("%s: Resuming port %d\n", __func__, val);
1236                 isp116x_write_reg32(isp116x, RH_PS_POCI, val
1237                                     ? HCRHPORT2 : HCRHPORT1);
1238         }
1239         spin_unlock_irq(&isp116x->lock);
1240
1241         hcd->state = HC_STATE_RESUMING;
1242         mdelay(20);
1243
1244         /* Go operational */
1245         spin_lock_irq(&isp116x->lock);
1246         val = isp116x_read_reg32(isp116x, HCCONTROL);
1247         isp116x_write_reg32(isp116x, HCCONTROL,
1248                             (val & ~HCCONTROL_HCFS) | HCCONTROL_USB_OPER);
1249         spin_unlock_irq(&isp116x->lock);
1250         /* see analogous comment above */
1251         hcd->self.root_hub->dev.power.power_state = PMSG_ON;
1252         hcd->state = HC_STATE_RUNNING;
1253
1254         return 0;
1255 }
1256
1257 static void isp116x_rh_resume(void *_hcd)
1258 {
1259         struct usb_hcd *hcd = _hcd;
1260
1261         usb_resume_device(hcd->self.root_hub);
1262 }
1263
1264 #else
1265
1266 #define isp116x_hub_suspend     NULL
1267 #define isp116x_hub_resume      NULL
1268
1269 static void isp116x_rh_resume(void *_hcd)
1270 {
1271 }
1272
1273 #endif
1274
1275 /*-----------------------------------------------------------------*/
1276
1277 #ifdef STUB_DEBUG_FILE
1278
1279 static inline void create_debug_file(struct isp116x *isp116x)
1280 {
1281 }
1282
1283 static inline void remove_debug_file(struct isp116x *isp116x)
1284 {
1285 }
1286
1287 #else
1288
1289 #include <linux/proc_fs.h>
1290 #include <linux/seq_file.h>
1291
1292 static void dump_irq(struct seq_file *s, char *label, u16 mask)
1293 {
1294         seq_printf(s, "%s %04x%s%s%s%s%s%s\n", label, mask,
1295                    mask & HCuPINT_CLKRDY ? " clkrdy" : "",
1296                    mask & HCuPINT_SUSP ? " susp" : "",
1297                    mask & HCuPINT_OPR ? " opr" : "",
1298                    mask & HCuPINT_AIIEOT ? " eot" : "",
1299                    mask & HCuPINT_ATL ? " atl" : "",
1300                    mask & HCuPINT_SOF ? " sof" : "");
1301 }
1302
1303 static void dump_int(struct seq_file *s, char *label, u32 mask)
1304 {
1305         seq_printf(s, "%s %08x%s%s%s%s%s%s%s\n", label, mask,
1306                    mask & HCINT_MIE ? " MIE" : "",
1307                    mask & HCINT_RHSC ? " rhsc" : "",
1308                    mask & HCINT_FNO ? " fno" : "",
1309                    mask & HCINT_UE ? " ue" : "",
1310                    mask & HCINT_RD ? " rd" : "",
1311                    mask & HCINT_SF ? " sof" : "", mask & HCINT_SO ? " so" : "");
1312 }
1313
1314 static int proc_isp116x_show(struct seq_file *s, void *unused)
1315 {
1316         struct isp116x *isp116x = s->private;
1317         struct isp116x_ep *ep;
1318         struct urb *urb;
1319         unsigned i;
1320         char *str;
1321
1322         seq_printf(s, "%s\n%s version %s\n",
1323                    isp116x_to_hcd(isp116x)->product_desc, hcd_name,
1324                    DRIVER_VERSION);
1325
1326         if (HC_IS_SUSPENDED(isp116x_to_hcd(isp116x)->state)) {
1327                 seq_printf(s, "HCD is suspended\n");
1328                 return 0;
1329         }
1330         if (!HC_IS_RUNNING(isp116x_to_hcd(isp116x)->state)) {
1331                 seq_printf(s, "HCD not running\n");
1332                 return 0;
1333         }
1334
1335         spin_lock_irq(&isp116x->lock);
1336
1337         dump_irq(s, "hc_irq_enable", isp116x_read_reg16(isp116x, HCuPINTENB));
1338         dump_irq(s, "hc_irq_status", isp116x_read_reg16(isp116x, HCuPINT));
1339         dump_int(s, "hc_int_enable", isp116x_read_reg32(isp116x, HCINTENB));
1340         dump_int(s, "hc_int_status", isp116x_read_reg32(isp116x, HCINTSTAT));
1341
1342         list_for_each_entry(ep, &isp116x->async, schedule) {
1343
1344                 switch (ep->nextpid) {
1345                 case USB_PID_IN:
1346                         str = "in";
1347                         break;
1348                 case USB_PID_OUT:
1349                         str = "out";
1350                         break;
1351                 case USB_PID_SETUP:
1352                         str = "setup";
1353                         break;
1354                 case USB_PID_ACK:
1355                         str = "status";
1356                         break;
1357                 default:
1358                         str = "?";
1359                         break;
1360                 };
1361                 seq_printf(s, "%p, ep%d%s, maxpacket %d:\n", ep,
1362                            ep->epnum, str, ep->maxpacket);
1363                 list_for_each_entry(urb, &ep->hep->urb_list, urb_list) {
1364                         seq_printf(s, "  urb%p, %d/%d\n", urb,
1365                                    urb->actual_length,
1366                                    urb->transfer_buffer_length);
1367                 }
1368         }
1369         if (!list_empty(&isp116x->async))
1370                 seq_printf(s, "\n");
1371
1372         seq_printf(s, "periodic size= %d\n", PERIODIC_SIZE);
1373
1374         for (i = 0; i < PERIODIC_SIZE; i++) {
1375                 ep = isp116x->periodic[i];
1376                 if (!ep)
1377                         continue;
1378                 seq_printf(s, "%2d [%3d]:\n", i, isp116x->load[i]);
1379
1380                 /* DUMB: prints shared entries multiple times */
1381                 do {
1382                         seq_printf(s, "   %d/%p (%sdev%d ep%d%s max %d)\n",
1383                                    ep->period, ep,
1384                                    (ep->udev->speed ==
1385                                     USB_SPEED_FULL) ? "" : "ls ",
1386                                    ep->udev->devnum, ep->epnum,
1387                                    (ep->epnum ==
1388                                     0) ? "" : ((ep->nextpid ==
1389                                                 USB_PID_IN) ? "in" : "out"),
1390                                    ep->maxpacket);
1391                         ep = ep->next;
1392                 } while (ep);
1393         }
1394         spin_unlock_irq(&isp116x->lock);
1395         seq_printf(s, "\n");
1396
1397         return 0;
1398 }
1399
1400 static int proc_isp116x_open(struct inode *inode, struct file *file)
1401 {
1402         return single_open(file, proc_isp116x_show, PDE(inode)->data);
1403 }
1404
1405 static struct file_operations proc_ops = {
1406         .open = proc_isp116x_open,
1407         .read = seq_read,
1408         .llseek = seq_lseek,
1409         .release = single_release,
1410 };
1411
1412 /* expect just one isp116x per system */
1413 static const char proc_filename[] = "driver/isp116x";
1414
1415 static void create_debug_file(struct isp116x *isp116x)
1416 {
1417         struct proc_dir_entry *pde;
1418
1419         pde = create_proc_entry(proc_filename, 0, NULL);
1420         if (pde == NULL)
1421                 return;
1422
1423         pde->proc_fops = &proc_ops;
1424         pde->data = isp116x;
1425         isp116x->pde = pde;
1426 }
1427
1428 static void remove_debug_file(struct isp116x *isp116x)
1429 {
1430         if (isp116x->pde)
1431                 remove_proc_entry(proc_filename, NULL);
1432 }
1433
1434 #endif
1435
1436 /*-----------------------------------------------------------------*/
1437
1438 /*
1439   Software reset - can be called from any contect.
1440 */
1441 static int isp116x_sw_reset(struct isp116x *isp116x)
1442 {
1443         int retries = 15;
1444         unsigned long flags;
1445         int ret = 0;
1446
1447         spin_lock_irqsave(&isp116x->lock, flags);
1448         isp116x_write_reg16(isp116x, HCSWRES, HCSWRES_MAGIC);
1449         isp116x_write_reg32(isp116x, HCCMDSTAT, HCCMDSTAT_HCR);
1450         while (--retries) {
1451                 /* It usually resets within 1 ms */
1452                 mdelay(1);
1453                 if (!(isp116x_read_reg32(isp116x, HCCMDSTAT) & HCCMDSTAT_HCR))
1454                         break;
1455         }
1456         if (!retries) {
1457                 ERR("Software reset timeout\n");
1458                 ret = -ETIME;
1459         }
1460         spin_unlock_irqrestore(&isp116x->lock, flags);
1461         return ret;
1462 }
1463
1464 /*
1465   Reset. Tries to perform platform-specific hardware
1466   reset first; falls back to software reset.
1467 */
1468 static int isp116x_reset(struct usb_hcd *hcd)
1469 {
1470         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1471         unsigned long t;
1472         u16 clkrdy = 0;
1473         int ret = 0, timeout = 15 /* ms */ ;
1474
1475         if (isp116x->board && isp116x->board->reset) {
1476                 /* Hardware reset */
1477                 isp116x->board->reset(hcd->self.controller, 1);
1478                 msleep(10);
1479                 if (isp116x->board->clock)
1480                         isp116x->board->clock(hcd->self.controller, 1);
1481                 msleep(1);
1482                 isp116x->board->reset(hcd->self.controller, 0);
1483         } else
1484                 ret = isp116x_sw_reset(isp116x);
1485
1486         if (ret)
1487                 return ret;
1488
1489         t = jiffies + msecs_to_jiffies(timeout);
1490         while (time_before_eq(jiffies, t)) {
1491                 msleep(4);
1492                 spin_lock_irq(&isp116x->lock);
1493                 clkrdy = isp116x_read_reg16(isp116x, HCuPINT) & HCuPINT_CLKRDY;
1494                 spin_unlock_irq(&isp116x->lock);
1495                 if (clkrdy)
1496                         break;
1497         }
1498         if (!clkrdy) {
1499                 ERR("Clock not ready after 20ms\n");
1500                 /* After sw_reset the clock won't report to be ready, if
1501                    H_WAKEUP pin is high. */
1502                 if (!isp116x->board || !isp116x->board->reset)
1503                         ERR("The driver does not support hardware wakeup.\n");
1504                         ERR("Please make sure that the H_WAKEUP pin "
1505                                 "is pulled low!\n");
1506                 ret = -ENODEV;
1507         }
1508         return ret;
1509 }
1510
1511 static void isp116x_stop(struct usb_hcd *hcd)
1512 {
1513         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1514         unsigned long flags;
1515         u32 val;
1516
1517         spin_lock_irqsave(&isp116x->lock, flags);
1518         isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1519
1520         /* Switch off ports' power, some devices don't come up
1521            after next 'insmod' without this */
1522         val = isp116x_read_reg32(isp116x, HCRHDESCA);
1523         val &= ~(RH_A_NPS | RH_A_PSM);
1524         isp116x_write_reg32(isp116x, HCRHDESCA, val);
1525         isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPS);
1526         spin_unlock_irqrestore(&isp116x->lock, flags);
1527
1528         /* Put the chip into reset state */
1529         if (isp116x->board && isp116x->board->reset)
1530                 isp116x->board->reset(hcd->self.controller, 0);
1531         else
1532                 isp116x_sw_reset(isp116x);
1533
1534         /* Stop the clock */
1535         if (isp116x->board && isp116x->board->clock)
1536                 isp116x->board->clock(hcd->self.controller, 0);
1537 }
1538
1539 /*
1540   Configure the chip. The chip must be successfully reset by now.
1541 */
1542 static int isp116x_start(struct usb_hcd *hcd)
1543 {
1544         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1545         struct isp116x_platform_data *board = isp116x->board;
1546         u32 val;
1547         unsigned long flags;
1548
1549         spin_lock_irqsave(&isp116x->lock, flags);
1550
1551         /* clear interrupt status and disable all interrupt sources */
1552         isp116x_write_reg16(isp116x, HCuPINT, 0xff);
1553         isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1554
1555         val = isp116x_read_reg16(isp116x, HCCHIPID);
1556         if ((val & HCCHIPID_MASK) != HCCHIPID_MAGIC) {
1557                 ERR("Invalid chip ID %04x\n", val);
1558                 spin_unlock_irqrestore(&isp116x->lock, flags);
1559                 return -ENODEV;
1560         }
1561
1562         isp116x_write_reg16(isp116x, HCITLBUFLEN, ISP116x_ITL_BUFSIZE);
1563         isp116x_write_reg16(isp116x, HCATLBUFLEN, ISP116x_ATL_BUFSIZE);
1564
1565         /* ----- HW conf */
1566         val = HCHWCFG_INT_ENABLE | HCHWCFG_DBWIDTH(1);
1567         if (board->sel15Kres)
1568                 val |= HCHWCFG_15KRSEL;
1569         /* Remote wakeup won't work without working clock */
1570         if (board->clknotstop || board->remote_wakeup_enable)
1571                 val |= HCHWCFG_CLKNOTSTOP;
1572         if (board->oc_enable)
1573                 val |= HCHWCFG_ANALOG_OC;
1574         if (board->int_act_high)
1575                 val |= HCHWCFG_INT_POL;
1576         if (board->int_edge_triggered)
1577                 val |= HCHWCFG_INT_TRIGGER;
1578         isp116x_write_reg16(isp116x, HCHWCFG, val);
1579
1580         /* ----- Root hub conf */
1581         val = 0;
1582         /* AN10003_1.pdf recommends NPS to be always 1 */
1583         if (board->no_power_switching)
1584                 val |= RH_A_NPS;
1585         if (board->power_switching_mode)
1586                 val |= RH_A_PSM;
1587         if (board->potpg)
1588                 val |= (board->potpg << 24) & RH_A_POTPGT;
1589         else
1590                 val |= (25 << 24) & RH_A_POTPGT;
1591         isp116x_write_reg32(isp116x, HCRHDESCA, val);
1592         isp116x->rhdesca = isp116x_read_reg32(isp116x, HCRHDESCA);
1593
1594         val = RH_B_PPCM;
1595         isp116x_write_reg32(isp116x, HCRHDESCB, val);
1596         isp116x->rhdescb = isp116x_read_reg32(isp116x, HCRHDESCB);
1597
1598         val = 0;
1599         if (board->remote_wakeup_enable) {
1600                 hcd->can_wakeup = 1;
1601                 val |= RH_HS_DRWE;
1602         }
1603         isp116x_write_reg32(isp116x, HCRHSTATUS, val);
1604         isp116x->rhstatus = isp116x_read_reg32(isp116x, HCRHSTATUS);
1605
1606         isp116x_write_reg32(isp116x, HCFMINTVL, 0x27782edf);
1607
1608         hcd->state = HC_STATE_RUNNING;
1609
1610         /* Set up interrupts */
1611         isp116x->intenb = HCINT_MIE | HCINT_RHSC | HCINT_UE;
1612         if (board->remote_wakeup_enable)
1613                 isp116x->intenb |= HCINT_RD;
1614         isp116x->irqenb = HCuPINT_ATL | HCuPINT_OPR;    /* | HCuPINT_SUSP; */
1615         isp116x_write_reg32(isp116x, HCINTENB, isp116x->intenb);
1616         isp116x_write_reg16(isp116x, HCuPINTENB, isp116x->irqenb);
1617
1618         /* Go operational */
1619         val = HCCONTROL_USB_OPER;
1620         /* Remote wakeup connected - NOT SUPPORTED */
1621         /*  if (board->remote_wakeup_connected)
1622            val |= HCCONTROL_RWC;  */
1623         if (board->remote_wakeup_enable)
1624                 val |= HCCONTROL_RWE;
1625         isp116x_write_reg32(isp116x, HCCONTROL, val);
1626
1627         /* Disable ports to avoid race in device enumeration */
1628         isp116x_write_reg32(isp116x, HCRHPORT1, RH_PS_CCS);
1629         isp116x_write_reg32(isp116x, HCRHPORT2, RH_PS_CCS);
1630
1631         isp116x_show_regs(isp116x);
1632         spin_unlock_irqrestore(&isp116x->lock, flags);
1633         return 0;
1634 }
1635
1636 /*-----------------------------------------------------------------*/
1637
1638 static struct hc_driver isp116x_hc_driver = {
1639         .description = hcd_name,
1640         .product_desc = "ISP116x Host Controller",
1641         .hcd_priv_size = sizeof(struct isp116x),
1642
1643         .irq = isp116x_irq,
1644         .flags = HCD_USB11,
1645
1646         .reset = isp116x_reset,
1647         .start = isp116x_start,
1648         .stop = isp116x_stop,
1649
1650         .urb_enqueue = isp116x_urb_enqueue,
1651         .urb_dequeue = isp116x_urb_dequeue,
1652         .endpoint_disable = isp116x_endpoint_disable,
1653
1654         .get_frame_number = isp116x_get_frame,
1655
1656         .hub_status_data = isp116x_hub_status_data,
1657         .hub_control = isp116x_hub_control,
1658         .hub_suspend = isp116x_hub_suspend,
1659         .hub_resume = isp116x_hub_resume,
1660 };
1661
1662 /*----------------------------------------------------------------*/
1663
1664 static int __init_or_module isp116x_remove(struct device *dev)
1665 {
1666         struct usb_hcd *hcd = dev_get_drvdata(dev);
1667         struct isp116x *isp116x;
1668         struct platform_device *pdev;
1669         struct resource *res;
1670
1671         if(!hcd)
1672                 return 0;
1673         isp116x = hcd_to_isp116x(hcd);
1674         pdev = container_of(dev, struct platform_device, dev);
1675         remove_debug_file(isp116x);
1676         usb_remove_hcd(hcd);
1677
1678         iounmap(isp116x->data_reg);
1679         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1680         release_mem_region(res->start, 2);
1681         iounmap(isp116x->addr_reg);
1682         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1683         release_mem_region(res->start, 2);
1684
1685         usb_put_hcd(hcd);
1686         return 0;
1687 }
1688
1689 #define resource_len(r) (((r)->end - (r)->start) + 1)
1690
1691 static int __init isp116x_probe(struct device *dev)
1692 {
1693         struct usb_hcd *hcd;
1694         struct isp116x *isp116x;
1695         struct platform_device *pdev;
1696         struct resource *addr, *data;
1697         void __iomem *addr_reg;
1698         void __iomem *data_reg;
1699         int irq;
1700         int ret = 0;
1701
1702         pdev = container_of(dev, struct platform_device, dev);
1703         if (pdev->num_resources < 3) {
1704                 ret = -ENODEV;
1705                 goto err1;
1706         }
1707
1708         data = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1709         addr = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1710         irq = platform_get_irq(pdev, 0);
1711         if (!addr || !data || irq < 0) {
1712                 ret = -ENODEV;
1713                 goto err1;
1714         }
1715
1716         if (dev->dma_mask) {
1717                 DBG("DMA not supported\n");
1718                 ret = -EINVAL;
1719                 goto err1;
1720         }
1721
1722         if (!request_mem_region(addr->start, 2, hcd_name)) {
1723                 ret = -EBUSY;
1724                 goto err1;
1725         }
1726         addr_reg = ioremap(addr->start, resource_len(addr));
1727         if (addr_reg == NULL) {
1728                 ret = -ENOMEM;
1729                 goto err2;
1730         }
1731         if (!request_mem_region(data->start, 2, hcd_name)) {
1732                 ret = -EBUSY;
1733                 goto err3;
1734         }
1735         data_reg = ioremap(data->start, resource_len(data));
1736         if (data_reg == NULL) {
1737                 ret = -ENOMEM;
1738                 goto err4;
1739         }
1740
1741         /* allocate and initialize hcd */
1742         hcd = usb_create_hcd(&isp116x_hc_driver, dev, dev->bus_id);
1743         if (!hcd) {
1744                 ret = -ENOMEM;
1745                 goto err5;
1746         }
1747         /* this rsrc_start is bogus */
1748         hcd->rsrc_start = addr->start;
1749         isp116x = hcd_to_isp116x(hcd);
1750         isp116x->data_reg = data_reg;
1751         isp116x->addr_reg = addr_reg;
1752         spin_lock_init(&isp116x->lock);
1753         INIT_LIST_HEAD(&isp116x->async);
1754         INIT_WORK(&isp116x->rh_resume, isp116x_rh_resume, hcd);
1755         isp116x->board = dev->platform_data;
1756
1757         if (!isp116x->board) {
1758                 ERR("Platform data structure not initialized\n");
1759                 ret = -ENODEV;
1760                 goto err6;
1761         }
1762         if (isp116x_check_platform_delay(isp116x)) {
1763                 ERR("USE_PLATFORM_DELAY defined, but delay function not "
1764                     "implemented.\n");
1765                 ERR("See comments in drivers/usb/host/isp116x-hcd.c\n");
1766                 ret = -ENODEV;
1767                 goto err6;
1768         }
1769
1770         ret = usb_add_hcd(hcd, irq, SA_INTERRUPT);
1771         if (ret != 0)
1772                 goto err6;
1773
1774         create_debug_file(isp116x);
1775         return 0;
1776
1777       err6:
1778         usb_put_hcd(hcd);
1779       err5:
1780         iounmap(data_reg);
1781       err4:
1782         release_mem_region(data->start, 2);
1783       err3:
1784         iounmap(addr_reg);
1785       err2:
1786         release_mem_region(addr->start, 2);
1787       err1:
1788         ERR("init error, %d\n", ret);
1789         return ret;
1790 }
1791
1792 #ifdef  CONFIG_PM
1793 /*
1794   Suspend of platform device
1795 */
1796 static int isp116x_suspend(struct device *dev, pm_message_t state, u32 phase)
1797 {
1798         int ret = 0;
1799         struct usb_hcd *hcd = dev_get_drvdata(dev);
1800
1801         VDBG("%s: state %x, phase %x\n", __func__, state, phase);
1802
1803         if (phase != SUSPEND_DISABLE && phase != SUSPEND_POWER_DOWN)
1804                 return 0;
1805
1806         ret = usb_suspend_device(hcd->self.root_hub, state);
1807         if (!ret) {
1808                 dev->power.power_state = state;
1809                 INFO("%s suspended\n", hcd_name);
1810         } else
1811                 ERR("%s suspend failed\n", hcd_name);
1812
1813         return ret;
1814 }
1815
1816 /*
1817   Resume platform device
1818 */
1819 static int isp116x_resume(struct device *dev, u32 phase)
1820 {
1821         int ret = 0;
1822         struct usb_hcd *hcd = dev_get_drvdata(dev);
1823
1824         VDBG("%s:  state %x, phase %x\n", __func__, dev->power.power_state,
1825              phase);
1826         if (phase != RESUME_POWER_ON)
1827                 return 0;
1828
1829         ret = usb_resume_device(hcd->self.root_hub);
1830         if (!ret) {
1831                 dev->power.power_state = PMSG_ON;
1832                 VDBG("%s resumed\n", (char *)hcd_name);
1833         }
1834         return ret;
1835 }
1836
1837 #else
1838
1839 #define isp116x_suspend    NULL
1840 #define isp116x_resume     NULL
1841
1842 #endif
1843
1844 static struct device_driver isp116x_driver = {
1845         .name = (char *)hcd_name,
1846         .bus = &platform_bus_type,
1847         .probe = isp116x_probe,
1848         .remove = isp116x_remove,
1849         .suspend = isp116x_suspend,
1850         .resume = isp116x_resume,
1851 };
1852
1853 /*-----------------------------------------------------------------*/
1854
1855 static int __init isp116x_init(void)
1856 {
1857         if (usb_disabled())
1858                 return -ENODEV;
1859
1860         INFO("driver %s, %s\n", hcd_name, DRIVER_VERSION);
1861         return driver_register(&isp116x_driver);
1862 }
1863
1864 module_init(isp116x_init);
1865
1866 static void __exit isp116x_cleanup(void)
1867 {
1868         driver_unregister(&isp116x_driver);
1869 }
1870
1871 module_exit(isp116x_cleanup);