[PATCH] USB: S3C2410 OHCI - add driver owner field
[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                         usb_hcd_resume_root_hub(hcd);
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                                gfp_t 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_bus_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_bus_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
1267 #else
1268
1269 #define isp116x_bus_suspend     NULL
1270 #define isp116x_bus_resume      NULL
1271
1272 #endif
1273
1274 /*-----------------------------------------------------------------*/
1275
1276 #ifdef STUB_DEBUG_FILE
1277
1278 static inline void create_debug_file(struct isp116x *isp116x)
1279 {
1280 }
1281
1282 static inline void remove_debug_file(struct isp116x *isp116x)
1283 {
1284 }
1285
1286 #else
1287
1288 #include <linux/proc_fs.h>
1289 #include <linux/seq_file.h>
1290
1291 static void dump_irq(struct seq_file *s, char *label, u16 mask)
1292 {
1293         seq_printf(s, "%s %04x%s%s%s%s%s%s\n", label, mask,
1294                    mask & HCuPINT_CLKRDY ? " clkrdy" : "",
1295                    mask & HCuPINT_SUSP ? " susp" : "",
1296                    mask & HCuPINT_OPR ? " opr" : "",
1297                    mask & HCuPINT_AIIEOT ? " eot" : "",
1298                    mask & HCuPINT_ATL ? " atl" : "",
1299                    mask & HCuPINT_SOF ? " sof" : "");
1300 }
1301
1302 static void dump_int(struct seq_file *s, char *label, u32 mask)
1303 {
1304         seq_printf(s, "%s %08x%s%s%s%s%s%s%s\n", label, mask,
1305                    mask & HCINT_MIE ? " MIE" : "",
1306                    mask & HCINT_RHSC ? " rhsc" : "",
1307                    mask & HCINT_FNO ? " fno" : "",
1308                    mask & HCINT_UE ? " ue" : "",
1309                    mask & HCINT_RD ? " rd" : "",
1310                    mask & HCINT_SF ? " sof" : "", mask & HCINT_SO ? " so" : "");
1311 }
1312
1313 static int proc_isp116x_show(struct seq_file *s, void *unused)
1314 {
1315         struct isp116x *isp116x = s->private;
1316         struct isp116x_ep *ep;
1317         struct urb *urb;
1318         unsigned i;
1319         char *str;
1320
1321         seq_printf(s, "%s\n%s version %s\n",
1322                    isp116x_to_hcd(isp116x)->product_desc, hcd_name,
1323                    DRIVER_VERSION);
1324
1325         if (HC_IS_SUSPENDED(isp116x_to_hcd(isp116x)->state)) {
1326                 seq_printf(s, "HCD is suspended\n");
1327                 return 0;
1328         }
1329         if (!HC_IS_RUNNING(isp116x_to_hcd(isp116x)->state)) {
1330                 seq_printf(s, "HCD not running\n");
1331                 return 0;
1332         }
1333
1334         spin_lock_irq(&isp116x->lock);
1335
1336         dump_irq(s, "hc_irq_enable", isp116x_read_reg16(isp116x, HCuPINTENB));
1337         dump_irq(s, "hc_irq_status", isp116x_read_reg16(isp116x, HCuPINT));
1338         dump_int(s, "hc_int_enable", isp116x_read_reg32(isp116x, HCINTENB));
1339         dump_int(s, "hc_int_status", isp116x_read_reg32(isp116x, HCINTSTAT));
1340
1341         list_for_each_entry(ep, &isp116x->async, schedule) {
1342
1343                 switch (ep->nextpid) {
1344                 case USB_PID_IN:
1345                         str = "in";
1346                         break;
1347                 case USB_PID_OUT:
1348                         str = "out";
1349                         break;
1350                 case USB_PID_SETUP:
1351                         str = "setup";
1352                         break;
1353                 case USB_PID_ACK:
1354                         str = "status";
1355                         break;
1356                 default:
1357                         str = "?";
1358                         break;
1359                 };
1360                 seq_printf(s, "%p, ep%d%s, maxpacket %d:\n", ep,
1361                            ep->epnum, str, ep->maxpacket);
1362                 list_for_each_entry(urb, &ep->hep->urb_list, urb_list) {
1363                         seq_printf(s, "  urb%p, %d/%d\n", urb,
1364                                    urb->actual_length,
1365                                    urb->transfer_buffer_length);
1366                 }
1367         }
1368         if (!list_empty(&isp116x->async))
1369                 seq_printf(s, "\n");
1370
1371         seq_printf(s, "periodic size= %d\n", PERIODIC_SIZE);
1372
1373         for (i = 0; i < PERIODIC_SIZE; i++) {
1374                 ep = isp116x->periodic[i];
1375                 if (!ep)
1376                         continue;
1377                 seq_printf(s, "%2d [%3d]:\n", i, isp116x->load[i]);
1378
1379                 /* DUMB: prints shared entries multiple times */
1380                 do {
1381                         seq_printf(s, "   %d/%p (%sdev%d ep%d%s max %d)\n",
1382                                    ep->period, ep,
1383                                    (ep->udev->speed ==
1384                                     USB_SPEED_FULL) ? "" : "ls ",
1385                                    ep->udev->devnum, ep->epnum,
1386                                    (ep->epnum ==
1387                                     0) ? "" : ((ep->nextpid ==
1388                                                 USB_PID_IN) ? "in" : "out"),
1389                                    ep->maxpacket);
1390                         ep = ep->next;
1391                 } while (ep);
1392         }
1393         spin_unlock_irq(&isp116x->lock);
1394         seq_printf(s, "\n");
1395
1396         return 0;
1397 }
1398
1399 static int proc_isp116x_open(struct inode *inode, struct file *file)
1400 {
1401         return single_open(file, proc_isp116x_show, PDE(inode)->data);
1402 }
1403
1404 static struct file_operations proc_ops = {
1405         .open = proc_isp116x_open,
1406         .read = seq_read,
1407         .llseek = seq_lseek,
1408         .release = single_release,
1409 };
1410
1411 /* expect just one isp116x per system */
1412 static const char proc_filename[] = "driver/isp116x";
1413
1414 static void create_debug_file(struct isp116x *isp116x)
1415 {
1416         struct proc_dir_entry *pde;
1417
1418         pde = create_proc_entry(proc_filename, 0, NULL);
1419         if (pde == NULL)
1420                 return;
1421
1422         pde->proc_fops = &proc_ops;
1423         pde->data = isp116x;
1424         isp116x->pde = pde;
1425 }
1426
1427 static void remove_debug_file(struct isp116x *isp116x)
1428 {
1429         if (isp116x->pde)
1430                 remove_proc_entry(proc_filename, NULL);
1431 }
1432
1433 #endif
1434
1435 /*-----------------------------------------------------------------*/
1436
1437 /*
1438   Software reset - can be called from any contect.
1439 */
1440 static int isp116x_sw_reset(struct isp116x *isp116x)
1441 {
1442         int retries = 15;
1443         unsigned long flags;
1444         int ret = 0;
1445
1446         spin_lock_irqsave(&isp116x->lock, flags);
1447         isp116x_write_reg16(isp116x, HCSWRES, HCSWRES_MAGIC);
1448         isp116x_write_reg32(isp116x, HCCMDSTAT, HCCMDSTAT_HCR);
1449         while (--retries) {
1450                 /* It usually resets within 1 ms */
1451                 mdelay(1);
1452                 if (!(isp116x_read_reg32(isp116x, HCCMDSTAT) & HCCMDSTAT_HCR))
1453                         break;
1454         }
1455         if (!retries) {
1456                 ERR("Software reset timeout\n");
1457                 ret = -ETIME;
1458         }
1459         spin_unlock_irqrestore(&isp116x->lock, flags);
1460         return ret;
1461 }
1462
1463 static int isp116x_reset(struct usb_hcd *hcd)
1464 {
1465         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1466         unsigned long t;
1467         u16 clkrdy = 0;
1468         int ret = 0, timeout = 15 /* ms */ ;
1469
1470         ret = isp116x_sw_reset(isp116x);
1471         if (ret)
1472                 return ret;
1473
1474         t = jiffies + msecs_to_jiffies(timeout);
1475         while (time_before_eq(jiffies, t)) {
1476                 msleep(4);
1477                 spin_lock_irq(&isp116x->lock);
1478                 clkrdy = isp116x_read_reg16(isp116x, HCuPINT) & HCuPINT_CLKRDY;
1479                 spin_unlock_irq(&isp116x->lock);
1480                 if (clkrdy)
1481                         break;
1482         }
1483         if (!clkrdy) {
1484                 ERR("Clock not ready after 20ms\n");
1485                 /* After sw_reset the clock won't report to be ready, if
1486                    H_WAKEUP pin is high. */
1487                 ERR("Please make sure that the H_WAKEUP pin is pulled low!\n");
1488                 ret = -ENODEV;
1489         }
1490         return ret;
1491 }
1492
1493 static void isp116x_stop(struct usb_hcd *hcd)
1494 {
1495         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1496         unsigned long flags;
1497         u32 val;
1498
1499         spin_lock_irqsave(&isp116x->lock, flags);
1500         isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1501
1502         /* Switch off ports' power, some devices don't come up
1503            after next 'insmod' without this */
1504         val = isp116x_read_reg32(isp116x, HCRHDESCA);
1505         val &= ~(RH_A_NPS | RH_A_PSM);
1506         isp116x_write_reg32(isp116x, HCRHDESCA, val);
1507         isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPS);
1508         spin_unlock_irqrestore(&isp116x->lock, flags);
1509
1510         isp116x_sw_reset(isp116x);
1511 }
1512
1513 /*
1514   Configure the chip. The chip must be successfully reset by now.
1515 */
1516 static int isp116x_start(struct usb_hcd *hcd)
1517 {
1518         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1519         struct isp116x_platform_data *board = isp116x->board;
1520         u32 val;
1521         unsigned long flags;
1522
1523         spin_lock_irqsave(&isp116x->lock, flags);
1524
1525         /* clear interrupt status and disable all interrupt sources */
1526         isp116x_write_reg16(isp116x, HCuPINT, 0xff);
1527         isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1528
1529         val = isp116x_read_reg16(isp116x, HCCHIPID);
1530         if ((val & HCCHIPID_MASK) != HCCHIPID_MAGIC) {
1531                 ERR("Invalid chip ID %04x\n", val);
1532                 spin_unlock_irqrestore(&isp116x->lock, flags);
1533                 return -ENODEV;
1534         }
1535
1536         /* To be removed in future */
1537         hcd->uses_new_polling = 1;
1538
1539         isp116x_write_reg16(isp116x, HCITLBUFLEN, ISP116x_ITL_BUFSIZE);
1540         isp116x_write_reg16(isp116x, HCATLBUFLEN, ISP116x_ATL_BUFSIZE);
1541
1542         /* ----- HW conf */
1543         val = HCHWCFG_INT_ENABLE | HCHWCFG_DBWIDTH(1);
1544         if (board->sel15Kres)
1545                 val |= HCHWCFG_15KRSEL;
1546         /* Remote wakeup won't work without working clock */
1547         if (board->remote_wakeup_enable)
1548                 val |= HCHWCFG_CLKNOTSTOP;
1549         if (board->oc_enable)
1550                 val |= HCHWCFG_ANALOG_OC;
1551         if (board->int_act_high)
1552                 val |= HCHWCFG_INT_POL;
1553         if (board->int_edge_triggered)
1554                 val |= HCHWCFG_INT_TRIGGER;
1555         isp116x_write_reg16(isp116x, HCHWCFG, val);
1556
1557         /* ----- Root hub conf */
1558         val = (25 << 24) & RH_A_POTPGT;
1559         /* AN10003_1.pdf recommends RH_A_NPS (no power switching) to
1560            be always set. Yet, instead, we request individual port
1561            power switching. */
1562         val |= RH_A_PSM;
1563         /* Report overcurrent per port */
1564         val |= RH_A_OCPM;
1565         isp116x_write_reg32(isp116x, HCRHDESCA, val);
1566         isp116x->rhdesca = isp116x_read_reg32(isp116x, HCRHDESCA);
1567
1568         val = RH_B_PPCM;
1569         isp116x_write_reg32(isp116x, HCRHDESCB, val);
1570         isp116x->rhdescb = isp116x_read_reg32(isp116x, HCRHDESCB);
1571
1572         val = 0;
1573         if (board->remote_wakeup_enable) {
1574                 hcd->can_wakeup = 1;
1575                 val |= RH_HS_DRWE;
1576         }
1577         isp116x_write_reg32(isp116x, HCRHSTATUS, val);
1578         isp116x->rhstatus = isp116x_read_reg32(isp116x, HCRHSTATUS);
1579
1580         isp116x_write_reg32(isp116x, HCFMINTVL, 0x27782edf);
1581
1582         hcd->state = HC_STATE_RUNNING;
1583
1584         /* Set up interrupts */
1585         isp116x->intenb = HCINT_MIE | HCINT_RHSC | HCINT_UE;
1586         if (board->remote_wakeup_enable)
1587                 isp116x->intenb |= HCINT_RD;
1588         isp116x->irqenb = HCuPINT_ATL | HCuPINT_OPR;    /* | HCuPINT_SUSP; */
1589         isp116x_write_reg32(isp116x, HCINTENB, isp116x->intenb);
1590         isp116x_write_reg16(isp116x, HCuPINTENB, isp116x->irqenb);
1591
1592         /* Go operational */
1593         val = HCCONTROL_USB_OPER;
1594         if (board->remote_wakeup_enable)
1595                 val |= HCCONTROL_RWE;
1596         isp116x_write_reg32(isp116x, HCCONTROL, val);
1597
1598         /* Disable ports to avoid race in device enumeration */
1599         isp116x_write_reg32(isp116x, HCRHPORT1, RH_PS_CCS);
1600         isp116x_write_reg32(isp116x, HCRHPORT2, RH_PS_CCS);
1601
1602         isp116x_show_regs(isp116x);
1603         spin_unlock_irqrestore(&isp116x->lock, flags);
1604         return 0;
1605 }
1606
1607 /*-----------------------------------------------------------------*/
1608
1609 static struct hc_driver isp116x_hc_driver = {
1610         .description = hcd_name,
1611         .product_desc = "ISP116x Host Controller",
1612         .hcd_priv_size = sizeof(struct isp116x),
1613
1614         .irq = isp116x_irq,
1615         .flags = HCD_USB11,
1616
1617         .reset = isp116x_reset,
1618         .start = isp116x_start,
1619         .stop = isp116x_stop,
1620
1621         .urb_enqueue = isp116x_urb_enqueue,
1622         .urb_dequeue = isp116x_urb_dequeue,
1623         .endpoint_disable = isp116x_endpoint_disable,
1624
1625         .get_frame_number = isp116x_get_frame,
1626
1627         .hub_status_data = isp116x_hub_status_data,
1628         .hub_control = isp116x_hub_control,
1629         .bus_suspend = isp116x_bus_suspend,
1630         .bus_resume = isp116x_bus_resume,
1631 };
1632
1633 /*----------------------------------------------------------------*/
1634
1635 static int __init_or_module isp116x_remove(struct device *dev)
1636 {
1637         struct usb_hcd *hcd = dev_get_drvdata(dev);
1638         struct isp116x *isp116x;
1639         struct platform_device *pdev;
1640         struct resource *res;
1641
1642         if (!hcd)
1643                 return 0;
1644         isp116x = hcd_to_isp116x(hcd);
1645         pdev = container_of(dev, struct platform_device, dev);
1646         remove_debug_file(isp116x);
1647         usb_remove_hcd(hcd);
1648
1649         iounmap(isp116x->data_reg);
1650         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1651         release_mem_region(res->start, 2);
1652         iounmap(isp116x->addr_reg);
1653         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1654         release_mem_region(res->start, 2);
1655
1656         usb_put_hcd(hcd);
1657         return 0;
1658 }
1659
1660 #define resource_len(r) (((r)->end - (r)->start) + 1)
1661
1662 static int __init isp116x_probe(struct device *dev)
1663 {
1664         struct usb_hcd *hcd;
1665         struct isp116x *isp116x;
1666         struct platform_device *pdev;
1667         struct resource *addr, *data;
1668         void __iomem *addr_reg;
1669         void __iomem *data_reg;
1670         int irq;
1671         int ret = 0;
1672
1673         pdev = container_of(dev, struct platform_device, dev);
1674         if (pdev->num_resources < 3) {
1675                 ret = -ENODEV;
1676                 goto err1;
1677         }
1678
1679         data = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1680         addr = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1681         irq = platform_get_irq(pdev, 0);
1682         if (!addr || !data || irq < 0) {
1683                 ret = -ENODEV;
1684                 goto err1;
1685         }
1686
1687         if (dev->dma_mask) {
1688                 DBG("DMA not supported\n");
1689                 ret = -EINVAL;
1690                 goto err1;
1691         }
1692
1693         if (!request_mem_region(addr->start, 2, hcd_name)) {
1694                 ret = -EBUSY;
1695                 goto err1;
1696         }
1697         addr_reg = ioremap(addr->start, resource_len(addr));
1698         if (addr_reg == NULL) {
1699                 ret = -ENOMEM;
1700                 goto err2;
1701         }
1702         if (!request_mem_region(data->start, 2, hcd_name)) {
1703                 ret = -EBUSY;
1704                 goto err3;
1705         }
1706         data_reg = ioremap(data->start, resource_len(data));
1707         if (data_reg == NULL) {
1708                 ret = -ENOMEM;
1709                 goto err4;
1710         }
1711
1712         /* allocate and initialize hcd */
1713         hcd = usb_create_hcd(&isp116x_hc_driver, dev, dev->bus_id);
1714         if (!hcd) {
1715                 ret = -ENOMEM;
1716                 goto err5;
1717         }
1718         /* this rsrc_start is bogus */
1719         hcd->rsrc_start = addr->start;
1720         isp116x = hcd_to_isp116x(hcd);
1721         isp116x->data_reg = data_reg;
1722         isp116x->addr_reg = addr_reg;
1723         spin_lock_init(&isp116x->lock);
1724         INIT_LIST_HEAD(&isp116x->async);
1725         isp116x->board = dev->platform_data;
1726
1727         if (!isp116x->board) {
1728                 ERR("Platform data structure not initialized\n");
1729                 ret = -ENODEV;
1730                 goto err6;
1731         }
1732         if (isp116x_check_platform_delay(isp116x)) {
1733                 ERR("USE_PLATFORM_DELAY defined, but delay function not "
1734                     "implemented.\n");
1735                 ERR("See comments in drivers/usb/host/isp116x-hcd.c\n");
1736                 ret = -ENODEV;
1737                 goto err6;
1738         }
1739
1740         ret = usb_add_hcd(hcd, irq, SA_INTERRUPT);
1741         if (ret != 0)
1742                 goto err6;
1743
1744         create_debug_file(isp116x);
1745         return 0;
1746
1747       err6:
1748         usb_put_hcd(hcd);
1749       err5:
1750         iounmap(data_reg);
1751       err4:
1752         release_mem_region(data->start, 2);
1753       err3:
1754         iounmap(addr_reg);
1755       err2:
1756         release_mem_region(addr->start, 2);
1757       err1:
1758         ERR("init error, %d\n", ret);
1759         return ret;
1760 }
1761
1762 #ifdef  CONFIG_PM
1763 /*
1764   Suspend of platform device
1765 */
1766 static int isp116x_suspend(struct device *dev, pm_message_t state)
1767 {
1768         int ret = 0;
1769
1770         VDBG("%s: state %x\n", __func__, state);
1771
1772         dev->power.power_state = state;
1773
1774         return ret;
1775 }
1776
1777 /*
1778   Resume platform device
1779 */
1780 static int isp116x_resume(struct device *dev)
1781 {
1782         int ret = 0;
1783
1784         VDBG("%s:  state %x\n", __func__, dev->power.power_state);
1785
1786         dev->power.power_state = PMSG_ON;
1787
1788         return ret;
1789 }
1790
1791 #else
1792
1793 #define isp116x_suspend    NULL
1794 #define isp116x_resume     NULL
1795
1796 #endif
1797
1798 static struct device_driver isp116x_driver = {
1799         .name = (char *)hcd_name,
1800         .bus = &platform_bus_type,
1801         .probe = isp116x_probe,
1802         .remove = isp116x_remove,
1803         .suspend = isp116x_suspend,
1804         .resume = isp116x_resume,
1805 };
1806
1807 /*-----------------------------------------------------------------*/
1808
1809 static int __init isp116x_init(void)
1810 {
1811         if (usb_disabled())
1812                 return -ENODEV;
1813
1814         INFO("driver %s, %s\n", hcd_name, DRIVER_VERSION);
1815         return driver_register(&isp116x_driver);
1816 }
1817
1818 module_init(isp116x_init);
1819
1820 static void __exit isp116x_cleanup(void)
1821 {
1822         driver_unregister(&isp116x_driver);
1823 }
1824
1825 module_exit(isp116x_cleanup);