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