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