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