more changes on original files
[linux-2.4.git] / drivers / usb / host / sl811.c
1 /*
2  * SL811 Host Controller Interface driver for USB.
3  *
4  * Copyright (c) 2003/06, Courage Co., Ltd.
5  *
6  * Based on:
7  *      1.uhci.c by Linus Torvalds, Johannes Erdfelt, Randy Dunlap,
8  *        Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber,
9  *        Adam Richter, Gregory P. Smith;
10  *      2.Original SL811 driver (hc_sl811.o) by Pei Liu <pbl@cypress.com>
11  *      3.Rewrited as sl811.o by Yin Aihua <yinah:couragetech.com.cn>
12  *
13  * It's now support isochornous mode and more effective than hc_sl811.o
14  * Support x86 architecture now.
15  *
16  * 19.09.2003 (05.06.2003) HNE
17  * sl811_alloc_hc: Set "bus->bus_name" at init.
18  * sl811_reg_test (hc_reset,regTest):
19  *   Stop output at first failed pattern.
20  * Down-Grade for Kernel 2.4.20 and from 2.4.22
21  * Splitt hardware depens into file sl811-x86.h and sl811-arm.h.
22  *
23  * 22.09.2003 HNE
24  * sl811_found_hc: First patterntest, than interrupt enable.
25  * Do nothing, if patterntest failed. Release io, if failed.
26  * Stop Interrupts first, than remove handle. (Old blocked Shred IRQ)
27  * Alternate IO-Base for second Controller (CF/USB1).
28  *
29  * 24.09.2003 HNE
30  * Remove all arm specific source (moved into include/asm/sl811-hw.h).
31  *
32  * 03.10.2003 HNE
33  * Low level only for port io into hardware-include.
34
35  *
36  * To do:
37  *      1.Modify the timeout part, it's some messy
38  *      2.Use usb-a and usb-b set in Ping-Pong mode
39  *      o Floppy do not work.
40  *      o driver crash, if io region can't register
41  *      o Only as module tested! Compiled in Version not tested!
42
43  * This program is free software; you can redistribute it and/or modify
44  * it under the terms of the GNU General Public License as published by
45  * the Free Software Foundation; either version 2 of the License, or
46  * (at your option) any later version.
47  *
48  */
49
50 #include <linux/config.h>
51 #include <linux/module.h>
52 #include <linux/kernel.h>
53 #include <linux/delay.h>
54 #include <linux/sched.h>
55 #include <linux/slab.h>
56 #include <linux/errno.h>
57 #include <linux/init.h>
58 #include <linux/smp_lock.h>
59 #include <linux/list.h>
60 #include <linux/ioport.h>
61 #include <asm/io.h>
62 #include <linux/irq.h>
63 #include <linux/usb.h>
64
65 #include "../hcd.h"
66 #include "../hub.h"
67 #include "sl811.h"
68
69 #define DRIVER_VERSION "v0.30"
70 #define MODNAME "SL811"
71 #define DRIVER_AUTHOR "Yin Aihua <yinah@couragetech.com.cn>, Henry Nestler <hne@ist1.de>"
72 #define DRIVER_DESC "Sl811 USB Host Controller Alternate Driver"
73
74 static LIST_HEAD(sl811_hcd_list);
75
76 /*
77  * 0, normal prompt and information
78  * 1, error should not occur in normal
79  * 2, error maybe occur in normal
80  * 3, useful and detail debug information
81  * 4, function level enter and level inforamtion
82  * 5, endless information will output because of timer function or interrupt
83  */
84 static int debug = 0;
85 MODULE_PARM(debug,"i");
86 MODULE_PARM_DESC(debug,"debug level");
87
88 #include <asm/sl811-hw.h>       /* Include hardware and board depens */
89
90 static void sl811_rh_int_timer_do(unsigned long ptr);
91 static void sl811_transfer_done(struct sl811_hc *hc, int sof);
92
93 /*
94  * Read a byte of data from the SL811H/SL11H
95  */
96 static __u8 inline sl811_read(struct sl811_hc *hc, __u8 offset)
97 {
98         sl811_write_index (hc, offset);
99         return (sl811_read_data (hc));
100 }
101
102 /*
103  * Write a byte of data to the SL811H/SL11H
104  */
105 static void inline sl811_write(struct sl811_hc *hc, __u8 offset, __u8 data)
106 {
107         sl811_write_index_data (hc, offset, data);
108 }
109
110 /*
111  * Read consecutive bytes of data from the SL811H/SL11H buffer
112  */
113 static void inline sl811_read_buf(struct sl811_hc *hc, __u8 offset, __u8 *buf, __u8 size)
114 {
115         sl811_write_index (hc, offset);
116         while (size--) {
117                 *buf++ = sl811_read_data(hc);
118         }
119 }
120
121 /*
122  * Write consecutive bytes of data to the SL811H/SL11H buffer
123  */
124 static void inline sl811_write_buf(struct sl811_hc *hc, __u8 offset, __u8 *buf, __u8 size)
125 {
126         sl811_write_index (hc, offset);
127         while (size--) {
128                 sl811_write_data (hc, *buf);
129                 buf++;
130         }
131 }
132
133 /*
134  * This routine test the Read/Write functionality of SL811HS registers
135  */
136 static int sl811_reg_test(struct sl811_hc *hc)
137 {
138         int i, data, result = 0;
139         __u8 buf[256];
140
141         for (i = 0x10; i < 256; i++) {
142                 /* save the original buffer */
143                 buf[i] = sl811_read(hc, i);
144
145                 /* Write the new data to the buffer */
146                 sl811_write(hc, i, i);
147         }
148
149         /* compare the written data */
150         for (i = 0x10; i < 256; i++) {
151                 data = sl811_read(hc, i);
152                 if (data != i) {
153                         PDEBUG(1, "Pattern test failed!! value = 0x%x, s/b 0x%x", data, i);
154                         result = -1;
155
156                         /* If no Debug, show only first failed Address */
157                         if (!debug)
158                             break;
159                 }
160         }
161
162         /* restore the data */
163         for (i = 0x10; i < 256; i++)
164                 sl811_write(hc, i, buf[i]);
165
166         return result;
167 }
168
169 /*
170  * Display all SL811HS register values
171  */
172 #if 0 /* unused (hne) */
173 static void sl811_reg_show(struct sl811_hc *hc)
174 {
175         int i;
176
177         for (i = 0; i < 256; i++)
178                 PDEBUG(4, "offset %d: 0x%x", i, sl811_read(hc, i));
179 }
180 #endif
181
182 /*
183  * This function enables SL811HS interrupts
184  */
185 static void sl811_enable_interrupt(struct sl811_hc *hc)
186 {
187         PDEBUG(4, "enter");
188         sl811_write(hc, SL811_INTR, SL811_INTR_DONE_A | SL811_INTR_SOF | SL811_INTR_INSRMV);
189 }
190
191 /*
192  * This function disables SL811HS interrupts
193  */
194 static void sl811_disable_interrupt(struct sl811_hc *hc)
195 {
196         PDEBUG(4, "enter");
197         // Disable all other interrupt except for insert/remove.
198         sl811_write(hc, SL811_INTR, SL811_INTR_INSRMV);
199 }
200
201 /*
202  * SL811 Virtual Root Hub
203  */
204
205 /* Device descriptor */
206 static __u8 sl811_rh_dev_des[] =
207 {
208         0x12,       /*  __u8  bLength; */
209         0x01,       /*  __u8  bDescriptorType; Device */
210         0x10,       /*  __u16 bcdUSB; v1.1 */
211         0x01,
212         0x09,       /*  __u8  bDeviceClass; HUB_CLASSCODE */
213         0x00,       /*  __u8  bDeviceSubClass; */
214         0x00,       /*  __u8  bDeviceProtocol; */
215         0x08,       /*  __u8  bMaxPacketSize0; 8 Bytes */
216         0x00,       /*  __u16 idVendor; */
217         0x00,
218         0x00,       /*  __u16 idProduct; */
219         0x00,
220         0x00,       /*  __u16 bcdDevice; */
221         0x00,
222         0x00,       /*  __u8  iManufacturer; */
223         0x02,       /*  __u8  iProduct; */
224         0x01,       /*  __u8  iSerialNumber; */
225         0x01        /*  __u8  bNumConfigurations; */
226 };
227
228 /* Configuration descriptor */
229 static __u8 sl811_rh_config_des[] =
230 {
231         0x09,       /*  __u8  bLength; */
232         0x02,       /*  __u8  bDescriptorType; Configuration */
233         0x19,       /*  __u16 wTotalLength; */
234         0x00,
235         0x01,       /*  __u8  bNumInterfaces; */
236         0x01,       /*  __u8  bConfigurationValue; */
237         0x00,       /*  __u8  iConfiguration; */
238         0x40,       /*  __u8  bmAttributes;
239                     Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup,
240                     4..0: resvd */
241         0x00,       /*  __u8  MaxPower; */
242
243         /* interface */
244         0x09,       /*  __u8  if_bLength; */
245         0x04,       /*  __u8  if_bDescriptorType; Interface */
246         0x00,       /*  __u8  if_bInterfaceNumber; */
247         0x00,       /*  __u8  if_bAlternateSetting; */
248         0x01,       /*  __u8  if_bNumEndpoints; */
249         0x09,       /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */
250         0x00,       /*  __u8  if_bInterfaceSubClass; */
251         0x00,       /*  __u8  if_bInterfaceProtocol; */
252         0x00,       /*  __u8  if_iInterface; */
253
254         /* endpoint */
255         0x07,       /*  __u8  ep_bLength; */
256         0x05,       /*  __u8  ep_bDescriptorType; Endpoint */
257         0x81,       /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */
258         0x03,       /*  __u8  ep_bmAttributes; Interrupt */
259         0x08,       /*  __u16 ep_wMaxPacketSize; */
260         0x00,
261         0xff        /*  __u8  ep_bInterval; 255 ms */
262 };
263
264 /* root hub class descriptor*/
265 static __u8 sl811_rh_hub_des[] =
266 {
267         0x09,                   /*  __u8  bLength; */
268         0x29,                   /*  __u8  bDescriptorType; Hub-descriptor */
269         0x01,                   /*  __u8  bNbrPorts; */
270         0x00,                   /* __u16  wHubCharacteristics; */
271         0x00,
272         0x50,                   /*  __u8  bPwrOn2pwrGood; 2ms */
273         0x00,                   /*  __u8  bHubContrCurrent; 0 mA */
274         0xfc,                   /*  __u8  DeviceRemovable; *** 7 Ports max *** */
275         0xff                    /*  __u8  PortPwrCtrlMask; *** 7 ports max *** */
276 };
277
278 /*
279  * This function examine the port change in the virtual root hub. HUB INTERRUPT ENDPOINT.
280  */
281 static int sl811_rh_send_irq(struct sl811_hc *hc, __u8 *rh_change, int rh_len)
282 {
283         __u8 data = 0;
284                 
285         PDEBUG(5, "enter");
286
287         /*
288          * Right now, It is assume the power is good and no changes and only one port.
289          */
290         if (hc->rh_status.wPortChange & (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE)) {  
291                 data = 1<<1;
292                 *(__u8 *)rh_change = data;
293                 return 1;
294         } else
295                 return 0;
296 }
297
298 /*
299  * This function creates a timer that act as interrupt pipe in the virtual hub.
300  *
301  * Note:  The virtual root hub's interrupt pipe are polled by the timer
302  *        every "interval" ms
303  */
304 static void sl811_rh_init_int_timer(struct urb * urb)
305 {
306          struct sl811_hc *hc = urb->dev->bus->hcpriv;
307          hc->rh.interval = urb->interval;
308
309          init_timer(&hc->rh.rh_int_timer);
310          hc->rh.rh_int_timer.function = sl811_rh_int_timer_do;
311          hc->rh.rh_int_timer.data = (unsigned long)urb;
312          hc->rh.rh_int_timer.expires = jiffies +
313                 (HZ * (urb->interval < 30? 30: urb->interval)) / 1000;
314          add_timer (&hc->rh.rh_int_timer);
315 }
316
317 /*
318  * This function is called when the timer expires.  It gets the the port
319  * change data and pass along to the upper protocol.
320  */
321 static void sl811_rh_int_timer_do(unsigned long ptr)
322 {
323         int len;
324         struct urb *urb = (struct urb *)ptr;
325         struct sl811_hc *hc = urb->dev->bus->hcpriv;
326         PDEBUG (5, "enter");
327
328         if(hc->rh.send) {
329                 len = sl811_rh_send_irq(hc, urb->transfer_buffer,
330                         urb->transfer_buffer_length);
331                 if (len > 0) {
332                         urb->actual_length = len;
333                         if (urb->complete)
334                                 urb->complete(urb);
335                 }
336         }
337
338 #ifdef SL811_TIMEOUT
339         
340 {
341         struct list_head *head, *tmp;
342         struct sl811_urb_priv *urbp;
343         struct urb *u;
344         int i;
345         static int timeout_count = 0;
346
347 // check time out every second
348         if (++timeout_count > 4) {
349                 int max_scan = hc->active_urbs;
350                 timeout_count = 0;
351                 for (i = 0; i < 6; ++i) {
352                         head = &hc->urb_list[i];
353                         tmp = head->next;
354                         while (tmp != head && max_scan--) {
355                                 u = list_entry(tmp, struct urb, urb_list);
356                                 urbp = (struct sl811_urb_priv *)u->hcpriv;
357                                 tmp = tmp->next;
358                                 // Check if the URB timed out
359                                 if (u->timeout && time_after_eq(jiffies, urbp->inserttime + u->timeout)) {
360                                         PDEBUG(3, "urb = %p time out, we kill it", urb);
361                                         u->transfer_flags |= USB_TIMEOUT_KILLED;
362                                 }
363                         }
364                 }
365         }
366 }
367
368 #endif
369         // re-activate the timer
370         sl811_rh_init_int_timer(urb);
371 }
372
373 /* helper macro */
374 #define OK(x)   len = (x); break
375
376 /*
377  * This function handles all USB request to the the virtual root hub
378  */
379 static int sl811_rh_submit_urb(struct urb *urb)
380 {
381         struct usb_device *usb_dev = urb->dev;
382         struct sl811_hc *hc = usb_dev->bus->hcpriv;
383         struct usb_ctrlrequest *cmd = (struct usb_ctrlrequest *)urb->setup_packet;
384         void *data = urb->transfer_buffer;
385         int buf_len = urb->transfer_buffer_length;
386         unsigned int pipe = urb->pipe;
387         __u8 data_buf[16];
388         __u8 *bufp = data_buf;
389         int len = 0;
390         int status = 0;
391         
392         __u16 bmRType_bReq;
393         __u16 wValue;
394         __u16 wIndex;
395         __u16 wLength;
396
397         if (usb_pipeint(pipe)) {
398                 hc->rh.urb =  urb;
399                 hc->rh.send = 1;
400                 hc->rh.interval = urb->interval;
401                 sl811_rh_init_int_timer(urb);
402                 urb->status = 0;
403
404                 return 0;
405         }
406
407         bmRType_bReq  = cmd->bRequestType | (cmd->bRequest << 8);
408         wValue        = le16_to_cpu (cmd->wValue);
409         wIndex        = le16_to_cpu (cmd->wIndex);
410         wLength       = le16_to_cpu (cmd->wLength);
411
412         PDEBUG(5, "submit rh urb, req = %d(%x) len=%d", bmRType_bReq, bmRType_bReq, wLength);
413
414         /* Request Destination:
415                    without flags: Device,
416                    USB_RECIP_INTERFACE: interface,
417                    USB_RECIP_ENDPOINT: endpoint,
418                    USB_TYPE_CLASS means HUB here,
419                    USB_RECIP_OTHER | USB_TYPE_CLASS  almost ever means HUB_PORT here
420         */
421         switch (bmRType_bReq) {
422         case RH_GET_STATUS:
423                 *(__u16 *)bufp = cpu_to_le16(1);
424                 OK(2);
425
426         case RH_GET_STATUS | USB_RECIP_INTERFACE:
427                 *(__u16 *)bufp = cpu_to_le16(0);
428                 OK(2);
429
430         case RH_GET_STATUS | USB_RECIP_ENDPOINT:
431                 *(__u16 *)bufp = cpu_to_le16(0);
432                 OK(2);
433
434         case RH_GET_STATUS | USB_TYPE_CLASS:
435                 *(__u32 *)bufp = cpu_to_le32(0);
436                 OK(4);
437
438         case RH_GET_STATUS | USB_RECIP_OTHER | USB_TYPE_CLASS:
439                 *(__u32 *)bufp = cpu_to_le32(hc->rh_status.wPortChange<<16 | hc->rh_status.wPortStatus);
440                 OK(4);
441
442         case RH_CLEAR_FEATURE | USB_RECIP_ENDPOINT:
443                 switch (wValue) {
444                 case 1: 
445                         OK(0);
446                 }
447                 break;
448
449         case RH_CLEAR_FEATURE | USB_TYPE_CLASS:
450                 switch (wValue) {
451                 case C_HUB_LOCAL_POWER:
452                         OK(0);
453
454                 case C_HUB_OVER_CURRENT:
455                         OK(0);
456                 }
457                 break;
458
459         case RH_CLEAR_FEATURE | USB_RECIP_OTHER | USB_TYPE_CLASS:
460                 switch (wValue) {
461                 case USB_PORT_FEAT_ENABLE:
462                         hc->rh_status.wPortStatus &= ~USB_PORT_STAT_ENABLE;
463                         OK(0);
464
465                 case USB_PORT_FEAT_SUSPEND:
466                         hc->rh_status.wPortStatus &= ~USB_PORT_STAT_SUSPEND;
467                         OK(0);
468
469                 case USB_PORT_FEAT_POWER:
470                         hc->rh_status.wPortStatus &= ~USB_PORT_STAT_POWER;
471                         OK(0);
472
473                 case USB_PORT_FEAT_C_CONNECTION:
474                         hc->rh_status.wPortChange &= ~USB_PORT_STAT_C_CONNECTION;
475                         OK(0);
476
477                 case USB_PORT_FEAT_C_ENABLE:
478                         hc->rh_status.wPortChange &= ~USB_PORT_STAT_C_ENABLE;
479                         OK(0);
480
481                 case USB_PORT_FEAT_C_SUSPEND:
482                         hc->rh_status.wPortChange &= ~USB_PORT_STAT_C_SUSPEND;
483                         OK(0);
484
485                 case USB_PORT_FEAT_C_OVER_CURRENT:
486                         hc->rh_status.wPortChange &= ~USB_PORT_STAT_C_OVERCURRENT;
487                         OK(0);
488
489                 case USB_PORT_FEAT_C_RESET:
490                         hc->rh_status.wPortChange &= ~USB_PORT_STAT_C_RESET;
491                         OK(0);
492                 }
493                 break;
494
495         case RH_SET_FEATURE | USB_RECIP_OTHER | USB_TYPE_CLASS:
496                 switch (wValue) {
497                 case USB_PORT_FEAT_SUSPEND:
498                         hc->rh_status.wPortStatus |= USB_PORT_STAT_SUSPEND;
499                         OK(0);
500
501                 case USB_PORT_FEAT_RESET:
502                         hc->rh_status.wPortStatus |= USB_PORT_STAT_RESET;
503                         hc->rh_status.wPortChange = 0;
504                         hc->rh_status.wPortChange |= USB_PORT_STAT_C_RESET;
505                         hc->rh_status.wPortStatus &= ~USB_PORT_STAT_RESET;
506                         hc->rh_status.wPortStatus |= USB_PORT_STAT_ENABLE;
507                         OK(0);
508
509                 case USB_PORT_FEAT_POWER:
510                         hc->rh_status.wPortStatus |= USB_PORT_STAT_POWER;
511                         OK(0);
512
513                 case USB_PORT_FEAT_ENABLE:
514                         hc->rh_status.wPortStatus |= USB_PORT_STAT_ENABLE;
515                         OK(0);
516                 }
517                 break;
518
519         case RH_SET_ADDRESS:
520                 hc->rh.devnum = wValue;
521                 OK(0);
522
523         case RH_GET_DESCRIPTOR:
524                 switch ((wValue & 0xff00) >> 8) {
525                 case USB_DT_DEVICE:
526                         len = sizeof(sl811_rh_dev_des);
527                         bufp = sl811_rh_dev_des;
528                         OK(len);
529
530                 case USB_DT_CONFIG: 
531                         len = sizeof(sl811_rh_config_des);
532                         bufp = sl811_rh_config_des;
533                         OK(len);
534
535                 case USB_DT_STRING:
536                         len = usb_root_hub_string(wValue & 0xff, (int)(long)0,  "SL811HS", data, wLength);
537                         if (len > 0) {
538                                 bufp = data;
539                                 OK(len);
540                         }
541                 
542                 default:
543                         status = -EPIPE;
544                 }
545                 break;
546
547         case RH_GET_DESCRIPTOR | USB_TYPE_CLASS:
548                 len = sizeof(sl811_rh_hub_des);
549                 bufp = sl811_rh_hub_des;
550                 OK(len);
551
552         case RH_GET_CONFIGURATION:
553                 bufp[0] = 0x01;
554                 OK(1);
555
556         case RH_SET_CONFIGURATION:
557                 OK(0);
558
559         default:
560                 PDEBUG(1, "unsupported root hub command");
561                 status = -EPIPE;
562         }
563
564         len = min(len, buf_len);
565         if (data != bufp)
566                 memcpy(data, bufp, len);
567         urb->actual_length = len;
568         urb->status = status;
569
570         PDEBUG(5, "len = %d, status = %d", len, status);
571         
572         urb->hcpriv = NULL;
573         urb->dev = NULL;
574         if (urb->complete)
575                 urb->complete(urb);
576
577         return 0;
578 }
579
580 /*
581  * This function unlinks the URB
582  */
583 static int sl811_rh_unlink_urb(struct urb *urb)
584 {
585         struct sl811_hc *hc = urb->dev->bus->hcpriv;
586
587         PDEBUG(5, "enter");
588         
589         if (hc->rh.urb == urb) {
590                 hc->rh.send = 0;
591                 del_timer(&hc->rh.rh_int_timer);
592                 hc->rh.urb = NULL;
593                 urb->hcpriv = NULL;
594                 usb_dec_dev_use(urb->dev);
595                 urb->dev = NULL;
596                 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
597                         urb->status = -ECONNRESET;
598                         if (urb->complete)
599                                 urb->complete(urb);
600                 } else
601                         urb->status = -ENOENT;
602         }
603
604         return 0;
605 }
606
607 /*
608  * This function connect the virtual root hub to the USB stack
609  */
610 static int sl811_connect_rh(struct sl811_hc * hc)
611 {
612         struct usb_device *usb_dev;
613
614         hc->rh.devnum = 0;
615         usb_dev = usb_alloc_dev(NULL, hc->bus);
616         if (!usb_dev)
617                 return -ENOMEM;
618
619         hc->bus->root_hub = usb_dev;
620         usb_connect(usb_dev);
621
622         if (usb_new_device(usb_dev)) {
623                 usb_free_dev(usb_dev);
624                 return -ENODEV;
625         }
626         
627         PDEBUG(5, "leave success");
628         
629         return 0;
630 }
631
632 /*
633  * This function allocates private data space for the usb device
634  */
635 static int sl811_alloc_dev_priv(struct usb_device *usb_dev)
636 {
637         return 0;
638 }
639
640 /*
641  * This function de-allocates private data space for the usb devic
642  */
643 static int sl811_free_dev_priv (struct usb_device *usb_dev)
644 {
645         return 0;
646 }
647
648 /*
649  * This function allocates private data space for the urb
650  */
651 static struct sl811_urb_priv* sl811_alloc_urb_priv(struct urb *urb)
652 {
653         struct sl811_urb_priv *urbp;
654         
655         urbp = kmalloc(sizeof(*urbp), GFP_ATOMIC);
656         if (!urbp)
657                 return NULL;
658         
659         memset(urbp, 0, sizeof(*urbp));
660         
661         INIT_LIST_HEAD(&urbp->td_list);
662         
663         urbp->urb = urb;
664         urb->hcpriv = urbp;
665         
666         return urbp;
667 }
668
669 /*
670  * This function free private data space for the urb
671  */
672 static void sl811_free_urb_priv(struct urb *urb)
673 {
674         struct sl811_urb_priv *urbp = urb->hcpriv;
675         struct sl811_td *td;
676         struct list_head *head, *tmp;
677         
678         if (!urbp)
679                 return ;
680         
681         head = &urbp->td_list;
682         tmp = head->next;
683         
684         while (tmp != head) {
685                 td = list_entry(tmp, struct sl811_td, td_list);
686                 tmp = tmp->next;
687                 kfree(td);
688         }
689         
690         kfree(urbp);
691         urb->hcpriv = NULL;
692         
693         return ;
694 }
695
696 /*
697  * This function calculate the bus time need by this td.
698  * Fix me! Can this use usb_calc_bus_time()?
699  */
700 static void sl811_calc_td_time(struct sl811_td *td)
701 {
702 #if 1
703         int time;
704         int len = td->len;
705         struct sl811_hc *hc = td->urb->dev->bus->hcpriv; 
706
707         if (hc->rh_status.wPortStatus & USB_PORT_STAT_LOW_SPEED)
708                 time = 8*8*len + 1024;
709         else {
710                 if (td->ctrl & SL811_USB_CTRL_PREAMBLE)
711                         time = 8*8*len + 2048;
712                 else
713                         time = 8*len + 256;
714         }
715
716         time += 2*10 * len;
717
718         td->bustime = time;
719         
720 #else
721
722         unsigned long tmp;
723         int time;
724         int low_speed = usb_pipeslow(td->urb->pipe);
725         int input_dir = usb_pipein(td->urb->pipe);
726         int bytecount = td->len;
727         int isoc = usb_pipeisoc(td->urb->pipe); 
728
729         if (low_speed) {        /* no isoc. here */
730                 if (input_dir) {
731                         tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
732                         time =  (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
733                 } else {
734                         tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
735                         time =  (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
736                 }
737         } else if (!isoc){      /* for full-speed: */
738                 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
739                 time = (9107L + BW_HOST_DELAY + tmp);
740         } else {                /* for isoc: */
741                 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
742                 time =  (((input_dir) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
743         }
744         
745         td->bustime = time / 84;
746
747 #endif           
748 }
749
750 /*
751  * This function calculate the remainder bus time in current frame.
752  */
753 static inline int sl811_calc_bus_remainder(struct sl811_hc *hc)
754 {
755         return (sl811_read(hc, SL811_SOFCNTDIV) * 64);
756 }
757
758 /*
759  * This function allocates td for the urb
760  */
761 static struct sl811_td* sl811_alloc_td(struct urb *urb)
762 {
763         struct sl811_urb_priv *urbp = urb->hcpriv;
764         struct sl811_td *td;
765         
766         td = kmalloc(sizeof (*td), GFP_ATOMIC);
767         if (!td)
768                 return NULL;
769         
770         memset(td, 0, sizeof(*td));
771         
772         INIT_LIST_HEAD(&td->td_list);
773         
774         td->urb = urb;
775         list_add_tail(&td->td_list, &urbp->td_list);
776         
777         return td;
778 }
779
780 /*
781  * Fill the td.
782  */
783 static inline void sl811_fill_td(struct sl811_td *td, __u8 ctrl, __u8 addr, __u8 len, __u8 pidep, __u8 dev, __u8 *buf)
784 {
785         td->ctrl = ctrl;
786         td->addr = addr;
787         td->len = len;
788         td->pidep = pidep;
789         td->dev = dev;
790         td->buf = buf;
791         td->left = len;
792         td->errcnt = 3;
793 }
794
795 /*
796  * Fill the td.
797  */
798 static inline void sl811_reset_td(struct sl811_td *td)
799 {
800         td->status = 0;
801         td->left = td->len;
802         td->done = 0;
803         td->errcnt = 3;
804         td->nakcnt = 0;
805         td->td_status = 0;
806 }
807
808 static void sl811_print_td(int level, struct sl811_td *td)
809 {
810          PDEBUG(level, "td = %p, ctrl = %x, addr = %x, len = %x, pidep = %x\n "
811                 "dev = %x, status = %x, left = %x, errcnt = %x, done = %x\n "
812                 "buf = %p, bustime = %d, td_status = %d\n", 
813                 td, td->ctrl, td->addr, td->len, td->pidep,
814                 td->dev, td->status, td->left, td->errcnt, td->done,
815                 td->buf, td->bustime, td->td_status);
816 }
817
818 /*
819  * Isochronous transfers
820  */
821 static int sl811_submit_isochronous(struct urb *urb)
822 {
823         __u8 dev = usb_pipedevice(urb->pipe);
824         __u8 pidep = PIDEP(usb_packetid(urb->pipe), usb_pipeendpoint(urb->pipe));
825         __u8 ctrl = 0;
826         struct sl811_urb_priv *urbp = urb->hcpriv;
827         struct sl811_td *td = NULL;
828         int i;
829         
830         PDEBUG(4, "enter, urb = %p, urbp = %p", urb, urbp);
831         
832         /* Can't have low speed bulk transfers */
833         if (usb_pipeslow(urb->pipe)) {
834                 PDEBUG(1, "error, urb = %p, low speed device", urb);
835                 return -EINVAL;
836         }
837         
838         if (usb_pipeout(urb->pipe))
839                 ctrl |= SL811_USB_CTRL_DIR_OUT;
840                 
841         ctrl |= SL811_USB_CTRL_ARM | SL811_USB_CTRL_ENABLE | SL811_USB_CTRL_ISO;
842         
843         for (i = 0; i < urb->number_of_packets; i++) {
844                 urb->iso_frame_desc[i].actual_length = 0;
845                 urb->iso_frame_desc[i].status = -EXDEV;
846                         
847                 td = sl811_alloc_td(urb);
848                 if (!td)
849                         return -ENOMEM;
850
851                 sl811_fill_td(td, ctrl, SL811_DATA_START, 
852                         urb->iso_frame_desc[i].length,
853                         pidep, dev,
854                         urb->transfer_buffer + urb->iso_frame_desc[i].offset);
855                 sl811_calc_td_time(td);
856                 if (urbp->cur_td == NULL)
857                         urbp->cur_td = urbp->first_td = td;     
858         }
859
860         urbp->last_td = td;     
861         
862         PDEBUG(4, "leave success");
863
864 /*      
865 // for debug
866         {
867                 struct list_head *head, *tmp;
868                 struct sl811_td *td;
869                 int i = 0;
870                 head = &urbp->td_list;
871                 tmp = head->next;
872         
873                 if (list_empty(&urbp->td_list)) {
874                         PDEBUG(1, "bug!!! td list is empty!");
875                         return -ENODEV;
876                 }
877                 
878                 while (tmp != head) {
879                         ++i;
880                         td = list_entry(tmp, struct sl811_td, td_list);
881                         PDEBUG(2, "td = %p, i = %d", td, i);
882                         tmp = tmp->next;
883                 }
884         }
885 */      
886         return 0;
887 }
888
889 /*
890  * Reset isochronous transfers
891  */
892 static void sl811_reset_isochronous(struct urb *urb)
893 {
894         struct sl811_urb_priv *urbp = urb->hcpriv;
895         struct sl811_td *td = NULL;
896         struct list_head *head, *tmp;
897         int i;
898
899         PDEBUG(4, "enter, urb = %p", urb);
900         
901         for (i = 0; i < urb->number_of_packets; i++) {
902                 urb->iso_frame_desc[i].actual_length = 0;
903                 urb->iso_frame_desc[i].status = -EXDEV;
904         }
905
906         head = &urbp->td_list;
907         tmp = head->next;
908         while (tmp != head) {
909                 td = list_entry(tmp, struct sl811_td, td_list);
910                 tmp = tmp->next;
911                 sl811_reset_td(td);
912         }
913         
914         urbp->cur_td = urbp->first_td;
915         
916         urb->status = -EINPROGRESS;
917         urb->actual_length = 0;
918         urb->error_count = 0;
919 }
920
921 /*
922  * Result the iso urb.
923  */
924 static void sl811_result_isochronous(struct urb *urb)
925 {
926         struct list_head *tmp, *head;
927         struct sl811_urb_priv *urbp = urb->hcpriv;
928         int status = 0;
929         struct sl811_td *td;
930         int i;
931
932         PDEBUG(4, "enter, urb = %p", urb);
933                 
934         urb->actual_length = 0;
935
936         i = 0;
937         head = &urbp->td_list;
938         tmp = head->next;
939         while (tmp != head) {
940                 td = list_entry(tmp, struct sl811_td, td_list);
941                 tmp = tmp->next;
942                 
943                 if (!td->done) {
944                         if (urbp->unlink)
945                                 urb->status = -ENOENT;
946                         else {
947                                 PDEBUG(1, "we should not get here!");
948                                 urb->status = -EXDEV;
949                         }
950                         return ;        
951                 }
952                 if (td->td_status) {
953                         status = td->td_status;
954                         urb->error_count++;
955                         PDEBUG(1, "error: td = %p, td status = %d", td, td->td_status);
956                 }
957
958                 urb->iso_frame_desc[i].actual_length = td->len - td->left;
959                 urb->actual_length += td->len - td->left;
960                 urb->iso_frame_desc[i].status = td->td_status;
961                 ++i;
962                 if (td->left)
963                         PDEBUG(3, "short packet, td = %p, len = %d, left = %d", td, td->len, td->left);
964         }
965
966         urb->status = status;
967 /*
968 // for debug
969         PDEBUG(2, "iso urb complete, len = %d, status =%d ", urb->actual_length, urb->status);          
970 */
971         PDEBUG(4, "leave success");
972 }
973
974 /*
975  * Interrupt transfers
976  */
977 static int sl811_submit_interrupt(struct urb *urb)
978 {
979         int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
980         int len = urb->transfer_buffer_length;
981         __u8 *data = urb->transfer_buffer;
982         __u8 dev = usb_pipedevice(urb->pipe);
983         __u8 pidep = PIDEP(usb_packetid(urb->pipe), usb_pipeendpoint(urb->pipe));
984         __u8 ctrl = 0;
985         struct sl811_hc *hc = urb->dev->bus->hcpriv;
986         struct sl811_urb_priv *urbp = urb->hcpriv;
987         struct sl811_td *td = NULL;
988         
989         PDEBUG(4, "enter, urb = %p", urb);
990         
991         if (len > maxsze) {
992                 PDEBUG(1, "length is big than max packet size, len = %d, max packet = %d", len, maxsze);
993                 return -EINVAL;
994         }
995         if (usb_pipeslow(urb->pipe) && !(hc->rh_status.wPortStatus & USB_PORT_STAT_LOW_SPEED))
996                 ctrl |= SL811_USB_CTRL_PREAMBLE;
997         
998         ctrl |= SL811_USB_CTRL_ARM | SL811_USB_CTRL_ENABLE;
999         if (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe)))
1000                 ctrl |= SL811_USB_CTRL_TOGGLE_1;
1001         usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));    
1002         td = sl811_alloc_td(urb);
1003         if (!td)
1004                 return -ENOMEM;
1005                 
1006         sl811_fill_td(td, ctrl, SL811_DATA_START, len, pidep, dev, data);
1007         sl811_calc_td_time(td);
1008         urbp->cur_td = urbp->first_td = urbp->last_td = td;
1009         urbp->interval = 0;
1010         
1011         PDEBUG(4, "leave success");
1012         
1013         return 0;
1014 }
1015
1016 /*
1017  * Reset interrupt transfers
1018  */
1019 static void sl811_reset_interrupt(struct urb *urb)
1020 {
1021         struct sl811_urb_priv *urbp = urb->hcpriv;
1022         struct sl811_td *td = urbp->cur_td;
1023         
1024         PDEBUG(4, "enter, interval = %d", urb->interval);
1025         
1026         td->ctrl &= ~SL811_USB_CTRL_TOGGLE_1;
1027         if (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe)))
1028                 td->ctrl |= SL811_USB_CTRL_TOGGLE_1;
1029         usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));    
1030         
1031         sl811_reset_td(td);
1032
1033         urbp->interval = urb->interval;
1034         
1035         urb->status = -EINPROGRESS;
1036         urb->actual_length = 0;
1037 }
1038
1039 /*
1040  * Result the interrupt urb.
1041  */
1042 static void sl811_result_interrupt(struct urb *urb)
1043 {
1044         struct list_head *tmp;
1045         struct sl811_urb_priv *urbp = urb->hcpriv;
1046         struct sl811_td *td;
1047         int toggle;
1048         
1049         PDEBUG(4, "enter, urb = %p", urb);
1050         
1051         urb->actual_length = 0;
1052
1053         tmp = &urbp->td_list;
1054         tmp = tmp->next;
1055         td = list_entry(tmp, struct sl811_td, td_list);
1056
1057         // success.
1058         if (td->done && td->td_status == 0) {
1059                 urb->actual_length += td->len - td->left;
1060                 urb->status = 0;
1061                 return ;
1062         }
1063         // tranfer is done but fail, reset the toggle.
1064         else if (td->done && td->td_status) {
1065                 urb->status = td->td_status;
1066 reset_toggle:
1067                 toggle = (td->ctrl & SL811_USB_CTRL_TOGGLE_1) ? 1 : 0;
1068                 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), toggle);
1069                 PDEBUG(3, "error: td = %p, td status = %d", td, td->td_status);
1070                 return ;
1071         }
1072         // unlink, and not do transfer yet
1073         else if (td->done == 0 && urbp->unlink && td->td_status == 0) {
1074                 urb->status = -ENOENT;
1075                 PDEBUG(3, "unlink and not transfer!");
1076                 return ;
1077         }
1078         // unlink, and transfer not complete yet.
1079         else if (td->done == 0 && urbp->unlink && td->td_status) {
1080                 urb->status = -ENOENT;
1081                 PDEBUG(3, "unlink and not complete!");
1082                 goto reset_toggle;
1083         }
1084         // must be bug!!!
1085         else {// (td->done == 0 && urbp->unlink == 0)
1086                 PDEBUG(1, "we should not get here!");
1087                 urb->status = -EPIPE;
1088                 return ;
1089         }
1090 }
1091
1092 /*
1093  * Control transfers
1094  */
1095 static int sl811_submit_control(struct urb *urb)
1096 {
1097         int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1098         int len = urb->transfer_buffer_length;
1099         __u8 *data = urb->transfer_buffer;
1100         __u8 dev = usb_pipedevice(urb->pipe);
1101         __u8 pidep = 0;
1102         __u8 ctrl = 0;
1103         struct sl811_hc *hc = urb->dev->bus->hcpriv;
1104         struct sl811_urb_priv *urbp = urb->hcpriv;
1105         struct sl811_td *td = NULL;
1106         
1107         PDEBUG(4, "enter, urb = %p", urb);
1108         
1109         if (usb_pipeslow(urb->pipe) && !(hc->rh_status.wPortStatus & USB_PORT_STAT_LOW_SPEED))
1110                 ctrl |= SL811_USB_CTRL_PREAMBLE;
1111         
1112         /* Build SETUP TD */
1113         pidep = PIDEP(USB_PID_SETUP, usb_pipeendpoint(urb->pipe));
1114         ctrl |= SL811_USB_CTRL_ARM | SL811_USB_CTRL_ENABLE | SL811_USB_CTRL_DIR_OUT;
1115         td = sl811_alloc_td(urb);
1116         if (!td)
1117                 return -ENOMEM;
1118                 
1119         sl811_fill_td(td, ctrl, SL811_DATA_START, 8, pidep, dev, urb->setup_packet);
1120         sl811_calc_td_time(td);
1121         
1122         urbp->cur_td = urbp->first_td = td;
1123         
1124         /*
1125          * If direction is "send", change the frame from SETUP (0x2D)
1126          * to OUT (0xE1). Else change it from SETUP to IN (0x69).
1127          */
1128         pidep = PIDEP(usb_packetid(urb->pipe), usb_pipeendpoint(urb->pipe));
1129         if (usb_pipeout(urb->pipe))
1130                 ctrl |= SL811_USB_CTRL_DIR_OUT;
1131         else
1132                 ctrl &= ~SL811_USB_CTRL_DIR_OUT;
1133
1134         /* Build the DATA TD's */
1135         while (len > 0) {
1136                 int pktsze = len;
1137
1138                 if (pktsze > maxsze)
1139                         pktsze = maxsze;
1140
1141                 /* Alternate Data0/1 (start with Data1) */
1142                 ctrl ^= SL811_USB_CTRL_TOGGLE_1;
1143         
1144                 td = sl811_alloc_td(urb);
1145                 if (!td)
1146                         return -ENOMEM;
1147
1148                 sl811_fill_td(td, ctrl, SL811_DATA_START, pktsze, pidep, dev, data);    
1149                 sl811_calc_td_time(td);
1150                 
1151                 data += pktsze;
1152                 len -= pktsze;
1153         }
1154
1155         /* Build the final TD for control status */
1156         td = sl811_alloc_td(urb);
1157         if (!td)
1158                 return -ENOMEM;
1159
1160         /* It's IN if the pipe is an output pipe or we're not expecting data back */
1161         if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length) {
1162                 pidep = PIDEP(USB_PID_IN, usb_pipeendpoint(urb->pipe));
1163                 ctrl &= ~SL811_USB_CTRL_DIR_OUT;        
1164         } else {
1165                 pidep = PIDEP(USB_PID_OUT, usb_pipeendpoint(urb->pipe));
1166                 ctrl |= SL811_USB_CTRL_DIR_OUT;
1167         }
1168                 
1169         /* End in Data1 */
1170         ctrl |= SL811_USB_CTRL_TOGGLE_1;
1171
1172         sl811_fill_td(td, ctrl, SL811_DATA_START, 0, pidep, dev, 0);
1173         sl811_calc_td_time(td);
1174         urbp->last_td = td;
1175 /*      
1176 // for debug
1177         {
1178                 struct list_head *head, *tmp;
1179                 struct sl811_td *td;
1180                 int i = 0;
1181                 head = &urbp->td_list;
1182                 tmp = head->next;
1183         
1184                 if (list_empty(&urbp->td_list)) {
1185                         PDEBUG(1, "bug!!! td list is empty!");
1186                         return -ENODEV;
1187                 }
1188                 
1189                 while (tmp != head) {
1190                         ++i;
1191                         td = list_entry(tmp, struct sl811_td, td_list);
1192                         PDEBUG(3, "td = %p, i = %d", td, i);
1193                         tmp = tmp->next;
1194                 }
1195         }
1196 */      
1197         PDEBUG(4, "leave success");
1198         
1199         return 0;
1200 }
1201
1202 /*
1203  * Result the control urb.
1204  */
1205 static void sl811_result_control(struct urb *urb)
1206 {
1207         struct list_head *tmp, *head;
1208         struct sl811_urb_priv *urbp = urb->hcpriv;
1209         struct sl811_td *td;
1210
1211         PDEBUG(4, "enter, urb = %p", urb);
1212         
1213         if (list_empty(&urbp->td_list)) {
1214                 PDEBUG(1, "td list is empty");
1215                 return ;
1216         }
1217
1218         head = &urbp->td_list;
1219
1220         tmp = head->next;
1221         td = list_entry(tmp, struct sl811_td, td_list);
1222
1223         /* The first TD is the SETUP phase, check the status, but skip the count */
1224         if (!td->done) {
1225                 PDEBUG(3, "setup phase error, td = %p, done = %d", td, td->done);
1226                 goto err_done;
1227         }
1228         if (td->td_status)  {
1229                 PDEBUG(3, "setup phase error, td = %p, td status = %d", td, td->td_status);
1230                 goto err_status;
1231         }
1232
1233         urb->actual_length = 0;
1234
1235         /* The rest of the TD's (but the last) are data */
1236         tmp = tmp->next;
1237         while (tmp != head && tmp->next != head) {
1238                 td = list_entry(tmp, struct sl811_td, td_list);
1239                 tmp = tmp->next;
1240                 if (!td->done) {
1241                         PDEBUG(3, "data phase error, td = %p, done = %d", td, td->done);
1242                         goto err_done;
1243                 }
1244                 if (td->td_status)  {
1245                         PDEBUG(3, "data phase error, td = %p, td status = %d", td, td->td_status);
1246                         goto err_status;
1247                 }
1248
1249                 urb->actual_length += td->len - td->left;
1250                 // short packet.
1251                 if (td->left) {
1252                         PDEBUG(3, "data phase short packet, td = %p, count = %d", td, td->len - td->left);
1253                         break;
1254                 }
1255         }
1256
1257         /* The last td is status phase */
1258         td = urbp->last_td;
1259         if (!td->done) {
1260                 PDEBUG(3, "status phase error, td = %p, done = %d", td, td->done);
1261                 goto err_done;
1262         }
1263         if (td->td_status)  {
1264                 PDEBUG(3, "status phase error, td = %p, td status = %d", td, td->td_status);
1265                 goto err_status;
1266         }
1267         
1268         PDEBUG(4, "leave success");
1269         
1270         urb->status = 0;
1271         return ;
1272
1273 err_done:
1274         if (urbp->unlink)
1275                 urb->status = -ENOENT;
1276         else {
1277                 PDEBUG(1, "we should not get here! td = %p", td);
1278                 urb->status = -EPIPE;
1279         }
1280         return ;        
1281
1282 err_status:
1283         urb->status = td->td_status;            
1284         return ;
1285 }
1286
1287 /*
1288  * Bulk transfers
1289  */
1290 static int sl811_submit_bulk(struct urb *urb)
1291 {
1292         int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1293         int len = urb->transfer_buffer_length;
1294         __u8 *data = urb->transfer_buffer;
1295         __u8 dev = usb_pipedevice(urb->pipe);
1296         __u8 pidep = PIDEP(usb_packetid(urb->pipe), usb_pipeendpoint(urb->pipe));
1297         __u8 ctrl = 0;
1298         struct sl811_urb_priv *urbp = urb->hcpriv;
1299         struct sl811_td *td = NULL;
1300
1301         PDEBUG(4, "enter, urb = %p", urb);
1302                 
1303         if (len < 0) {
1304                 PDEBUG(1, "error, urb = %p, len = %d", urb, len);
1305                 return -EINVAL;
1306         }
1307
1308         /* Can't have low speed bulk transfers */
1309         if (usb_pipeslow(urb->pipe)) {
1310                 PDEBUG(1, "error, urb = %p, low speed device", urb);
1311                 return -EINVAL;
1312         }
1313
1314         if (usb_pipeout(urb->pipe))
1315                 ctrl |= SL811_USB_CTRL_DIR_OUT;
1316                 
1317         ctrl |= SL811_USB_CTRL_ARM | SL811_USB_CTRL_ENABLE;
1318                         
1319         /* Build the DATA TD's */
1320         do {    /* Allow zero length packets */
1321                 int pktsze = len;
1322
1323                 if (pktsze > maxsze)
1324                         pktsze = maxsze;
1325
1326                 td = sl811_alloc_td(urb);
1327                 if (!td)
1328                         return -ENOMEM;
1329
1330                 /* Alternate Data0/1 (start with Data1) */
1331                 ctrl &= ~SL811_USB_CTRL_TOGGLE_1;
1332                 if (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe)))
1333                         ctrl |= SL811_USB_CTRL_TOGGLE_1;
1334                 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));            
1335                 
1336                 sl811_fill_td(td, ctrl, SL811_DATA_START, pktsze, pidep, dev, data);
1337                 sl811_calc_td_time(td);
1338                 
1339                 if (urbp->cur_td == NULL)
1340                         urbp->cur_td = urbp->first_td = td;
1341                         
1342                 data += pktsze;
1343                 len -= maxsze;
1344         } while (len > 0);
1345
1346         /*
1347          * USB_ZERO_PACKET means adding a 0-length packet, if
1348          * direction is OUT and the transfer_length was an
1349          * exact multiple of maxsze, hence
1350          * (len = transfer_length - N * maxsze) == 0
1351          * however, if transfer_length == 0, the zero packet
1352          * was already prepared above.
1353          */
1354         if (usb_pipeout(urb->pipe) && (urb->transfer_flags & USB_ZERO_PACKET) &&
1355            !len && urb->transfer_buffer_length) {
1356                 
1357                 td = sl811_alloc_td(urb);
1358                 if (!td)
1359                         return -ENOMEM;
1360
1361                 /* Alternate Data0/1 (start with Data1) */
1362                 ctrl &= ~SL811_USB_CTRL_TOGGLE_1;
1363                 if (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe)))
1364                         ctrl |= SL811_USB_CTRL_TOGGLE_1;
1365                 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
1366                         
1367                 sl811_fill_td(td, ctrl, SL811_DATA_START, 0, pidep, dev, 0);
1368                 sl811_calc_td_time(td);
1369         }
1370         
1371         urbp->last_td = td;
1372         
1373         PDEBUG(4, "leave success");
1374         
1375         return 0;
1376 }
1377
1378 /*
1379  * Reset bulk transfers
1380  */
1381 static int sl811_reset_bulk(struct urb *urb)
1382 {
1383         struct sl811_urb_priv *urbp = urb->hcpriv;
1384         struct sl811_td *td;
1385         struct list_head *head, *tmp;
1386
1387         PDEBUG(4, "enter, urb = %p", urb);
1388         
1389         
1390         head = &urbp->td_list;  
1391         tmp = head->next;
1392         
1393         while (tmp != head) {
1394                 td = list_entry(tmp, struct sl811_td, td_list);
1395
1396                 /* Alternate Data0/1 (start with Data1) */
1397                 td->ctrl &= ~SL811_USB_CTRL_TOGGLE_1;
1398                 if (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe)))
1399                         td->ctrl |= SL811_USB_CTRL_TOGGLE_1;
1400                 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
1401                 
1402                 sl811_reset_td(td);
1403         } 
1404
1405         urb->status = -EINPROGRESS;
1406         urb->actual_length = 0;
1407         urbp->cur_td = urbp->first_td;
1408
1409         PDEBUG(4, "leave success");
1410         
1411         return 0;
1412 }
1413
1414 /*
1415  * Result the bulk urb.
1416  */
1417 static void sl811_result_bulk(struct urb *urb)
1418 {
1419         struct list_head *tmp, *head;
1420         struct sl811_urb_priv *urbp = urb->hcpriv;
1421         struct sl811_td *td = NULL;
1422         int toggle;
1423
1424         PDEBUG(4, "enter, urb = %p", urb);
1425         
1426         urb->actual_length = 0;
1427
1428         head = &urbp->td_list;
1429         tmp = head->next;
1430         while (tmp != head) {
1431                 td = list_entry(tmp, struct sl811_td, td_list);
1432                 tmp = tmp->next;
1433
1434                 // success.
1435                 if (td->done && td->td_status == 0) {
1436                         urb->actual_length += td->len - td->left;
1437                         
1438                         // short packet
1439                         if (td->left) {
1440                                 urb->status = 0;
1441                                 PDEBUG(3, "short packet, td = %p, count = %d", td, td->len - td->left);
1442                                 goto reset_toggle;
1443                         }
1444                 }
1445                 // tranfer is done but fail, reset the toggle.
1446                 else if (td->done && td->td_status) {
1447                         urb->status = td->td_status;
1448                         PDEBUG(3, "error: td = %p, td status = %d", td, td->td_status);
1449                         goto reset_toggle;
1450                 }
1451                 // unlink, and not do transfer yet
1452                 else if (td->done == 0 && urbp->unlink && td->td_status == 0) {
1453                         urb->status = -ENOENT;
1454                         PDEBUG(3, "unlink and not transfer!");
1455                         return ;
1456                 }
1457                 // unlink, and transfer not complete yet.
1458                 else if (td->done == 0 && urbp->unlink && td->td_status) {
1459                         PDEBUG(3, "unlink and not complete!");
1460                         urb->status = -ENOENT;
1461                         goto reset_toggle;
1462                 }
1463                 // must be bug!!!
1464                 else {// (td->done == 0 && urbp->unlink == 0)
1465                         urb->status = -EPIPE;
1466                         PDEBUG(1, "we should not get here!");
1467                         return ;
1468                 }
1469         }
1470         
1471         PDEBUG(4, "leave success");             
1472         urb->status = 0;
1473         return ;
1474
1475 reset_toggle:
1476         toggle = (td->ctrl & SL811_USB_CTRL_TOGGLE_1) ? 1 : 0;
1477         usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), toggle);
1478 }
1479
1480 /*
1481  * Find the first urb have the same dev and endpoint.
1482  */
1483 static inline int sl811_find_same_urb(struct list_head *head, struct urb *urb)
1484 {
1485         struct list_head *tmp;
1486         struct urb *u;
1487         
1488         if (!head || !urb)
1489                 return 0;
1490                 
1491         tmp = head->next;
1492         
1493         while (tmp != head) {
1494                 u = list_entry(tmp, struct urb, urb_list);
1495                 if (u == urb)
1496                         return 1;
1497                 tmp = tmp->next;        
1498         }
1499         
1500         return 0;
1501 }
1502
1503 /*
1504  * Find the first urb have the same dev and endpoint.
1505  */
1506 static inline struct urb* sl811_find_same_devep(struct list_head *head, struct urb *urb)
1507 {
1508         struct list_head *tmp;
1509         struct urb *u;
1510         
1511         if (!head || !urb)
1512                 return NULL;
1513                 
1514         tmp = head->next;
1515         
1516         while (tmp != head) {
1517                 u = list_entry(tmp, struct urb, urb_list);
1518                 if ((usb_pipe_endpdev(u->pipe)) == (usb_pipe_endpdev(urb->pipe)))
1519                         return u;
1520                 tmp = tmp->next;        
1521         }
1522         
1523         return NULL;
1524 }
1525
1526 /*
1527  * This function is called by the USB core API when an URB is available to
1528  * process. 
1529  */
1530 static int sl811_submit_urb(struct urb *urb)
1531 {
1532         struct sl811_hc *hc = urb->dev->bus->hcpriv;
1533         unsigned int pipe = urb->pipe;
1534         struct list_head *head = NULL;
1535         unsigned long flags;
1536         int bustime;
1537         int ret = 0;
1538         
1539         if (!urb) {
1540                 PDEBUG(1, "urb is null");
1541                 return -EINVAL;
1542         }
1543         
1544         if (urb->hcpriv) {
1545                 PDEBUG(1, "urbp is not null, urb = %p, urbp = %p", urb, urb->hcpriv);
1546                 return -EINVAL;
1547         }
1548         
1549         if (!urb->dev || !urb->dev->bus || !hc)  {
1550                 PDEBUG(1, "dev or bus or hc is null");
1551                 return -ENODEV;
1552         }
1553         
1554         if (usb_endpoint_halted(urb->dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
1555                 PDEBUG(2, "sl811_submit_urb: endpoint_halted");
1556                 return -EPIPE;
1557         }
1558         
1559         if (usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)) > SL811_DATA_LIMIT) {
1560                 printk(KERN_ERR "Packet size is big for SL811, should < %d!\n", SL811_DATA_LIMIT);
1561                 return -EINVAL;
1562         }
1563         
1564         /* a request to the virtual root hub */ 
1565         if (usb_pipedevice(pipe) == hc->rh.devnum)
1566                 return sl811_rh_submit_urb(urb);
1567         
1568         spin_lock_irqsave(&hc->hc_lock, flags);
1569         spin_lock(&urb->lock);
1570         
1571         switch (usb_pipetype(urb->pipe)) {
1572         case PIPE_ISOCHRONOUS:
1573                 head = &hc->iso_list;
1574                 break;
1575         case PIPE_INTERRUPT:
1576                 head = &hc->intr_list;
1577                 break;
1578         case PIPE_CONTROL:
1579                 head = &hc->ctrl_list;
1580                 break;
1581         case PIPE_BULK:
1582                 head = &hc->bulk_list;
1583                 break;
1584         }
1585                 
1586         if (sl811_find_same_devep(head, urb)) {
1587                 list_add(&urb->urb_list, &hc->wait_list);
1588                 PDEBUG(4, "add to wait list");
1589                 goto out_unlock;
1590         }
1591         
1592         if (!sl811_alloc_urb_priv(urb)) {
1593                 ret = -ENOMEM;
1594                 goto out_unlock;
1595         }
1596         
1597         switch (usb_pipetype(urb->pipe)) {
1598         case PIPE_ISOCHRONOUS:
1599                 if (urb->number_of_packets <= 0) {
1600                         ret = -EINVAL;
1601                         break;
1602                 }
1603                 bustime = usb_check_bandwidth(urb->dev, urb);
1604                 if (bustime < 0) {
1605                         ret = bustime;
1606                         break;
1607                 }
1608                 if (!(ret = sl811_submit_isochronous(urb)))
1609                         usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1610                 break;
1611         case PIPE_INTERRUPT:
1612                 bustime = usb_check_bandwidth(urb->dev, urb);
1613                 if (bustime < 0)
1614                         ret = bustime;
1615                 else if (!(ret = sl811_submit_interrupt(urb)))
1616                         usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1617                 break;
1618         case PIPE_CONTROL:
1619                 ret = sl811_submit_control(urb);
1620                 break;
1621         case PIPE_BULK:
1622                 ret = sl811_submit_bulk(urb);
1623                 break;
1624         }
1625         
1626         if (!ret) {
1627                 ((struct sl811_urb_priv *)urb->hcpriv)->inserttime = jiffies;
1628                 list_add(&urb->urb_list, head);
1629                 PDEBUG(4, "add to type list");
1630                 urb->status = -EINPROGRESS;
1631                 if (++hc->active_urbs == 1)
1632                         sl811_enable_interrupt(hc);
1633                 goto out_unlock;        
1634         } else {
1635                 PDEBUG(2, "submit urb fail! error = %d", ret);
1636                 sl811_free_urb_priv(urb);
1637         }
1638         
1639 out_unlock:     
1640         spin_unlock(&urb->lock);
1641         spin_unlock_irqrestore(&hc->hc_lock, flags);
1642
1643         return ret;
1644 }
1645
1646 /*
1647  * Submit the urb the wait list.
1648  */
1649 static int sl811_submit_urb_with_lock(struct urb *urb)
1650 {
1651         struct sl811_hc *hc = urb->dev->bus->hcpriv;
1652         struct list_head *head = NULL;
1653         int bustime;
1654         int ret = 0;
1655         
1656         spin_lock(&urb->lock);
1657         
1658         switch (usb_pipetype(urb->pipe)) {
1659         case PIPE_ISOCHRONOUS:
1660                 head = &hc->iso_list;
1661                 break;
1662         case PIPE_INTERRUPT:
1663                 head = &hc->intr_list;
1664                 break;
1665         case PIPE_CONTROL:
1666                 head = &hc->ctrl_list;
1667                 break;
1668         case PIPE_BULK:
1669                 head = &hc->bulk_list;
1670                 break;
1671         }
1672                 
1673         if (!sl811_alloc_urb_priv(urb)) {
1674                 ret = -ENOMEM;
1675                 goto out_unlock;
1676         }
1677         
1678         switch (usb_pipetype(urb->pipe)) {
1679         case PIPE_ISOCHRONOUS:
1680                 if (urb->number_of_packets <= 0) {
1681                         ret = -EINVAL;
1682                         break;
1683                 }
1684                 bustime = usb_check_bandwidth(urb->dev, urb);
1685                 if (bustime < 0) {
1686                         ret = bustime;
1687                         break;
1688                 }
1689                 if (!(ret = sl811_submit_isochronous(urb)))
1690                         usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1691                 break;
1692         case PIPE_INTERRUPT:
1693                 bustime = usb_check_bandwidth(urb->dev, urb);
1694                 if (bustime < 0)
1695                         ret = bustime;
1696                 else if (!(ret = sl811_submit_interrupt(urb)))
1697                         usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1698                 break;
1699         case PIPE_CONTROL:
1700                 ret = sl811_submit_control(urb);
1701                 break;
1702         case PIPE_BULK:
1703                 ret = sl811_submit_bulk(urb);
1704                 break;
1705         }
1706         
1707         if (ret == 0) {
1708                 ((struct sl811_urb_priv *)urb->hcpriv)->inserttime = jiffies;
1709                 list_add(&urb->urb_list, head);
1710                 PDEBUG(4, "add to type list");
1711                 urb->status = -EINPROGRESS;
1712                 if (++hc->active_urbs == 1)
1713                         sl811_enable_interrupt(hc);
1714                 goto out_unlock;        
1715         } else {
1716                 PDEBUG(2, "submit urb fail! error = %d", ret);
1717                 sl811_free_urb_priv(urb);
1718         }
1719         
1720 out_unlock:     
1721         spin_unlock(&urb->lock);
1722
1723         return ret;
1724 }
1725
1726 /*
1727  * Reset the urb
1728  */
1729 static void sl811_reset_urb(struct urb *urb)
1730 {
1731         struct sl811_urb_priv *urbp = urb->hcpriv;
1732
1733         switch (usb_pipetype(urb->pipe)) {
1734         case PIPE_ISOCHRONOUS:
1735                 sl811_reset_isochronous(urb);
1736                 break;
1737         case PIPE_INTERRUPT:
1738                 sl811_reset_interrupt(urb);
1739                 break;
1740         case PIPE_CONTROL:
1741                 return;
1742         case PIPE_BULK:
1743                 sl811_reset_bulk(urb);
1744                 break;
1745         }
1746         urbp->inserttime = jiffies;
1747 }
1748
1749 /*
1750  * Return the result of a transfer
1751  */
1752 static void sl811_result_urb(struct urb *urb)
1753 {
1754         struct sl811_urb_priv *urbp = urb->hcpriv;
1755         struct sl811_hc *hc = urb->dev->bus->hcpriv;
1756         struct list_head *head = NULL;
1757         struct urb *u = NULL;
1758         int reset = 0;
1759         int ring = 0;
1760
1761         if (urb->status != -EINPROGRESS) {
1762                 PDEBUG(1, "urb status is not EINPROGRESS!");
1763                 return ;
1764         }
1765         
1766         spin_lock(&urb->lock);
1767         
1768         switch (usb_pipetype(urb->pipe)) {
1769         case PIPE_ISOCHRONOUS:
1770                 head = &hc->iso_list;
1771                 sl811_result_isochronous(urb);
1772                 
1773                 // if the urb is not unlink and is in a urb "ring", we reset it
1774                 if (!urbp->unlink && urb->next) 
1775                         ring = 1;
1776                 break;
1777         case PIPE_INTERRUPT:
1778                 head = &hc->intr_list;
1779                 sl811_result_interrupt(urb);
1780                 
1781                 // if the urb is not unlink and not "once" query, we reset.
1782                 if (!urbp->unlink && urb->interval)
1783                         reset = 1;
1784                 break;
1785         case PIPE_CONTROL:
1786                 head = &hc->ctrl_list;
1787                 sl811_result_control(urb);
1788                 break;
1789         case PIPE_BULK:
1790                 head = &hc->bulk_list;
1791                 sl811_result_bulk(urb);
1792                 
1793                 // if the urb is not unlink and is in a urb "ring", we reset it
1794                 if (!urbp->unlink && urb->next)
1795                         ring = 1;
1796                 break;
1797         }
1798         
1799         PDEBUG(4, "result urb status = %d", urb->status);
1800         
1801         if (ring && urb->next == urb)
1802                 reset = 1;
1803         
1804         if (!reset) {
1805                 switch (usb_pipetype(urb->pipe)) {
1806                 case PIPE_ISOCHRONOUS:
1807                         usb_release_bandwidth(urb->dev, urb, 1);
1808                         break;
1809                 case PIPE_INTERRUPT:
1810                         usb_release_bandwidth(urb->dev, urb, 0);
1811                         break;
1812                 }
1813                 sl811_free_urb_priv(urb);
1814         }
1815         
1816         spin_unlock(&urb->lock);
1817         
1818         if (urb->complete)
1819                 urb->complete(urb);
1820         
1821         if (reset) {
1822                 spin_lock(&urb->lock);
1823                 sl811_reset_urb(urb);
1824                 if (usb_pipeint(urb->pipe))
1825                         list_add(&urb->urb_list, &hc->idle_intr_list);
1826                 else
1827                         list_add(&urb->urb_list, head);
1828                 spin_unlock(&urb->lock);
1829         } else {
1830                 if (--hc->active_urbs <= 0) {
1831                         hc->active_urbs = 0;
1832                         sl811_disable_interrupt(hc);
1833                 }
1834                 
1835                 if (ring) 
1836                         u = urb->next;
1837                 else
1838                         u = sl811_find_same_devep(&hc->wait_list, urb);
1839                         
1840                 if (u) {
1841                         if (!list_empty(&u->urb_list))
1842                                 list_del(&u->urb_list);
1843                         if (sl811_submit_urb_with_lock(u))
1844                                 list_add(&u->urb_list, &hc->wait_list);
1845                 }
1846         }
1847 }
1848
1849
1850 #ifdef SL811_TIMEOUT
1851
1852 /*
1853  * Unlink the urb from the urb list
1854  */
1855 static int sl811_unlink_urb(struct urb *urb)
1856 {
1857         unsigned long flags;
1858         struct sl811_hc *hc;
1859         struct sl811_urb_priv *urbp;
1860         int call = 0;
1861         int schedule = 0;
1862         int count = 0;
1863         
1864         if (!urb) {
1865                 PDEBUG(1, "urb is null");
1866                 return -EINVAL;
1867         }
1868         
1869         if (!urb->dev || !urb->dev->bus) {
1870                 PDEBUG(1, "dev or bus is null");
1871                 return -ENODEV;
1872         }
1873         
1874         hc = urb->dev->bus->hcpriv; 
1875         urbp = urb->hcpriv;
1876         
1877         /* a request to the virtual root hub */
1878         if (usb_pipedevice(urb->pipe) == hc->rh.devnum)
1879                 return sl811_rh_unlink_urb(urb); 
1880         
1881         spin_lock_irqsave(&hc->hc_lock, flags);
1882         spin_lock(&urb->lock);
1883
1884         // in wait list
1885         if (sl811_find_same_urb(&hc->wait_list, urb)) { 
1886                 PDEBUG(4, "unlink urb in wait list");
1887                 list_del_init(&urb->urb_list);
1888                 urb->status = -ENOENT;
1889                 call = 1;
1890                 goto out;
1891         }
1892         
1893         // in intr idle list.
1894         if (sl811_find_same_urb(&hc->idle_intr_list, urb)) {    
1895                 PDEBUG(4, "unlink urb in idle intr list");
1896                 list_del_init(&urb->urb_list);
1897                 urb->status = -ENOENT;
1898                 sl811_free_urb_priv(urb);
1899                 usb_release_bandwidth(urb->dev, urb, 0);
1900                 if (--hc->active_urbs <= 0) {
1901                         hc->active_urbs = 0;
1902                         sl811_disable_interrupt(hc);
1903                 }
1904                 call = 1;
1905                 goto out;
1906         }
1907
1908         if (urb->status == -EINPROGRESS) {  
1909                 PDEBUG(3, "urb is still in progress");
1910                 urbp->unlink = 1;
1911
1912 re_unlink:
1913                 // Is it in progress?
1914                 urbp = urb->hcpriv;
1915                 if (urbp && hc->cur_td == urbp->cur_td) {
1916                         ++count;
1917                         if (sl811_read(hc, 0) & SL811_USB_CTRL_ARM) {
1918                                 PDEBUG(3, "unlink: cur td is still in progress! count = %d", count);
1919 re_schedule:                            
1920                                 schedule = 1;
1921                                 spin_unlock(&urb->lock);
1922                                 spin_unlock_irqrestore(&hc->hc_lock, flags);
1923                                 schedule_timeout(HZ/50);
1924                                 spin_lock_irqsave(&hc->hc_lock, flags);
1925                                 spin_lock(&urb->lock);
1926                         } else {
1927                                 PDEBUG(3, "unlink: lost of interrupt? do parse! count = %d", count);
1928                                 spin_unlock(&urb->lock);
1929                                 sl811_transfer_done(hc, 0);
1930                                 spin_lock(&urb->lock);
1931                         }
1932                         goto re_unlink;
1933                 }
1934                 
1935                 if (list_empty(&urb->urb_list)) {
1936                         PDEBUG(3, "unlink: list empty!");
1937                         goto out;
1938                 }
1939                         
1940                 if (urb->transfer_flags & USB_TIMEOUT_KILLED) { 
1941                         PDEBUG(3, "unlink: time out killed");
1942                         // it is timeout killed by us 
1943                         goto result;
1944                 } else if (urb->transfer_flags & USB_ASYNC_UNLINK) { 
1945                         // we do nothing, just let it be processing later
1946                         PDEBUG(3, "unlink async, do nothing");
1947                         goto out;
1948                 } else {
1949                         // synchron without callback
1950                         PDEBUG(3, "unlink synchron, we wait the urb complete or timeout");
1951                         if (schedule == 0) {
1952                                 PDEBUG(3, "goto re_schedule");
1953                                 goto re_schedule;
1954                         } else {
1955                                 PDEBUG(3, "already scheduled");
1956                                 goto result;
1957                         }
1958                 }
1959         } else if (!list_empty(&urb->urb_list)) {
1960                 PDEBUG(1, "urb = %p, status = %d is in a list, why?", urb, urb->status);
1961                 //list_del_init(&urb->urb_list);
1962                 //call = 1;
1963         }
1964
1965 out:
1966         spin_unlock(&urb->lock);
1967         spin_unlock_irqrestore(&hc->hc_lock, flags);
1968         
1969         if (call && urb->complete)
1970                 urb->complete(urb);
1971         
1972         return 0;
1973         
1974 result:
1975         spin_unlock(&urb->lock);
1976         
1977         list_del_init(&urb->urb_list);
1978         sl811_result_urb(urb);  
1979         
1980         spin_unlock_irqrestore(&hc->hc_lock, flags);
1981         
1982         return 0;
1983 }
1984
1985 #else
1986
1987 /*
1988  * Unlink the urb from the urb list
1989  */
1990 static int sl811_unlink_urb(struct urb *urb)
1991 {
1992         unsigned long flags;
1993         struct sl811_hc *hc;
1994         struct sl811_urb_priv *urbp;
1995         int call = 0;
1996         
1997         if (!urb) {
1998                 PDEBUG(1, "urb is null");
1999                 return -EINVAL;
2000         }
2001         
2002         if (!urb->dev || !urb->dev->bus) {
2003                 PDEBUG(1, "dev or bus is null");
2004                 return -ENODEV;
2005         }
2006         
2007         hc = urb->dev->bus->hcpriv; 
2008         urbp = urb->hcpriv;
2009         
2010         /* a request to the virtual root hub */
2011         if (usb_pipedevice(urb->pipe) == hc->rh.devnum)
2012                 return sl811_rh_unlink_urb(urb); 
2013         
2014         spin_lock_irqsave(&hc->hc_lock, flags);
2015         spin_lock(&urb->lock);
2016
2017         // in wait list
2018         if (sl811_find_same_urb(&hc->wait_list, urb)) { 
2019                 PDEBUG(2, "unlink urb in wait list");
2020                 list_del_init(&urb->urb_list);
2021                 urb->status = -ENOENT;
2022                 call = 1;
2023                 goto out;
2024         }
2025         
2026         if (urb->status == -EINPROGRESS) {  
2027                 PDEBUG(2, "urb is still in progress");
2028                 urbp->unlink = 1;
2029
2030                 // Is it in progress?
2031                 urbp = urb->hcpriv;
2032                 if (urbp && hc->cur_td == urbp->cur_td) {
2033                         // simple, let it out
2034                         PDEBUG(2, "unlink: cur td is still in progress!");
2035                         hc->cur_td = NULL;
2036                 }
2037                 
2038                 goto result;
2039         } else if (!list_empty(&urb->urb_list)) {
2040                 PDEBUG(1, "urb = %p, status = %d is in a list, why?", urb, urb->status);
2041                 list_del_init(&urb->urb_list);
2042                 if (urbp)
2043                         goto result;
2044                 else
2045                         call = 1;
2046         }
2047
2048 out:
2049         spin_unlock(&urb->lock);
2050         spin_unlock_irqrestore(&hc->hc_lock, flags);
2051         
2052         if (call && urb->complete)
2053                 urb->complete(urb);
2054                         
2055         return 0;
2056         
2057 result:
2058         spin_unlock(&urb->lock);
2059         
2060         list_del_init(&urb->urb_list);
2061         sl811_result_urb(urb);  
2062         
2063         spin_unlock_irqrestore(&hc->hc_lock, flags);
2064         
2065         return 0;
2066 }
2067
2068 #endif
2069
2070 static int sl811_get_current_frame_number(struct usb_device *usb_dev)
2071 {
2072         return ((struct sl811_hc *)(usb_dev->bus->hcpriv))->frame_number;
2073 }
2074
2075 static struct usb_operations sl811_device_operations = 
2076 {
2077         sl811_alloc_dev_priv,
2078         sl811_free_dev_priv,
2079         sl811_get_current_frame_number,
2080         sl811_submit_urb,
2081         sl811_unlink_urb
2082 };
2083
2084 /*
2085  * This functions transmit a td.
2086  */
2087 static inline void sl811_trans_cur_td(struct sl811_hc *hc, struct sl811_td *td)
2088 {
2089         sl811_print_td(4, td);
2090         sl811_write_buf(hc, SL811_ADDR_A,  &td->addr, 4);
2091         if (td->len && (td->ctrl & SL811_USB_CTRL_DIR_OUT))
2092                 sl811_write_buf(hc, td->addr,  td->buf, td->len);
2093
2094         sl811_write(hc, SL811_CTRL_A, td->ctrl);
2095 }
2096
2097                 
2098 /*
2099  * This function checks the status of the transmitted or received packet
2100  * and copy the data from the SL811HS register into a buffer.
2101  */
2102 static void sl811_parse_cur_td(struct sl811_hc *hc, struct sl811_td *td)
2103 {
2104         struct urb *urb = td->urb;
2105 #ifdef SL811_DEBUG
2106         int dev = usb_pipedevice(td->urb->pipe);
2107         int ep = usb_pipeendpoint(td->urb->pipe);
2108 #endif
2109
2110         sl811_read_buf(hc, SL811_STS_A, &td->status, 2);
2111         
2112         if (td->status & SL811_USB_STS_ACK) {
2113                 td->done = 1;
2114                 
2115 /*              if ((td->ctrl & SL811_USB_CTRL_TOGGLE_1) != (td->status & SL811_USB_STS_TOGGLE_1)) {
2116                         PDEBUG(2, "dev %d endpoint %d unexpect data toggle!", dev, ep);
2117                         td->td_status = -EILSEQ;
2118                 }
2119 */              
2120                 if (!(td->ctrl & SL811_USB_CTRL_DIR_OUT) && td->len > 0)
2121                         sl811_read_buf(hc, td->addr, td->buf, td->len - td->left);
2122                         
2123                 if (td->left && (urb->transfer_flags & USB_DISABLE_SPD)) {
2124                         PDEBUG(2, "dev %d endpoint %d unexpect short packet! td = %p", dev, ep, td);
2125                         td->td_status = -EREMOTEIO;
2126                 } else
2127                         td->td_status = 0;
2128         } else if (td->status & SL811_USB_STS_STALL) {
2129                 PDEBUG(2, "dev %d endpoint %d halt, td = %p", dev, ep, td);
2130                 td->td_status = -EPIPE;
2131                 if (urb->dev)
2132                         usb_endpoint_halt(td->urb->dev, usb_pipeendpoint(td->urb->pipe), usb_pipeout(td->urb->pipe));
2133                 td->done = 1;
2134         } else if (td->status & SL811_USB_STS_OVERFLOW) {
2135                 PDEBUG(1, "dev %d endpoint %d overflow, sl811 only support packet less than %d", dev, ep, SL811_DATA_LIMIT);    
2136                 td->td_status = -EOVERFLOW;
2137                 td->done = 1;
2138         } else if (td->status & SL811_USB_STS_TIMEOUT ) {
2139                 PDEBUG(2, "dev %d endpoint %d timeout, td = %p", dev, ep, td);  
2140                 td->td_status = -ETIMEDOUT;
2141                 if (--td->errcnt == 0)
2142                         td->done = 1;
2143         } else if (td->status & SL811_USB_STS_ERROR) {
2144                 PDEBUG(2, "dev %d endpoint %d error, td = %p", dev, ep, td);
2145                 td->td_status = -EILSEQ;
2146                 if (--td->errcnt == 0)
2147                         td->done = 1;
2148         } else if (td->status & SL811_USB_STS_NAK) {
2149                 ++td->nakcnt;
2150                 PDEBUG(3, "dev %d endpoint %d nak, td = %p, count = %d", dev, ep, td, td->nakcnt);
2151                 td->td_status = -EINPROGRESS;
2152                 if (!usb_pipeslow(td->urb->pipe) && td->nakcnt > 1024) {
2153                         PDEBUG(2, "too many naks, td = %p, count = %d", td, td->nakcnt);
2154                         td->td_status = -ETIMEDOUT;
2155                         td->done = 1;
2156                 } 
2157         } 
2158         
2159         sl811_print_td(4, td);
2160 }
2161
2162 /*
2163  * This function checks the status of current urb.
2164  */
2165 static int sl811_parse_cur_urb(struct urb *urb)
2166 {
2167         struct sl811_urb_priv *urbp = urb->hcpriv;
2168         struct sl811_td *td = urbp->cur_td;
2169         struct list_head *tmp;
2170         
2171         sl811_print_td(5, td);
2172         
2173         // this td not done yet.
2174         if (!td->done)
2175                 return 0;
2176         
2177         // the last ld, so the urb is done.
2178         if (td == urbp->last_td) {
2179                 PDEBUG(4, "urb = %p is done success", td->urb);
2180                 if (usb_pipeisoc(td->urb->pipe))
2181                         PDEBUG(4, "ISO URB DONE, td = %p", td);
2182                 return 1;
2183         }
2184         
2185         // iso transfer, we always advance to next td 
2186         if (usb_pipeisoc(td->urb->pipe)) {
2187                 tmp = &td->td_list;
2188                 tmp = tmp->next;
2189                 urbp->cur_td = list_entry(tmp, struct sl811_td, td_list);
2190                 PDEBUG(4, "ISO NEXT, td = %p", urbp->cur_td);   
2191                 return 0;
2192         }
2193                 
2194         // some error occur, so the urb is done.
2195         if (td->td_status) {
2196                 PDEBUG(3, "urb = %p is done error, td status is = %d", td->urb, td->td_status);
2197                 return 1;
2198         }
2199                 
2200         // short packet.
2201         if (td->left) {
2202                 if (usb_pipecontrol(td->urb->pipe)) {
2203                         // control packet, we advance to the last td
2204                         PDEBUG(3, "ctrl short packet, advance to last td");
2205                         urbp->cur_td = urbp->last_td;
2206                         return 0;
2207                 } else {
2208                         // interrut and bulk packet, urb is over.
2209                         PDEBUG(3, "bulk or intr short packet, urb is over");
2210                         return 1;
2211                 }
2212         }
2213
2214         // we advance to next td.       
2215         tmp = &td->td_list;
2216         tmp = tmp->next;
2217         urbp->cur_td = list_entry(tmp, struct sl811_td, td_list);
2218 #ifdef SL811_DEBUG
2219         PDEBUG(4, "advance to the next td, urb = %p, td = %p", urb, urbp->cur_td);
2220         sl811_print_td(5, urbp->cur_td);
2221         if (td == urbp->cur_td)
2222                 PDEBUG(1, "bug!!!");
2223 #endif          
2224         return 0;
2225 }
2226
2227 /*
2228  * Find the next td to transfer.
2229  */
2230 static inline struct sl811_td* sl811_schedule_next_td(struct urb *urb, struct sl811_td *cur_td)
2231 {
2232         struct sl811_urb_priv *urbp = urb->hcpriv;
2233         
2234         PDEBUG(4, "urb at %p, cur td at %p", urb, cur_td);
2235         
2236         // iso don't schedule the td in the same frame.
2237         if (usb_pipeisoc(cur_td->urb->pipe))
2238                 return NULL;
2239         
2240         // cur td is not complete
2241         if (!cur_td->done)
2242                 return NULL;    
2243         
2244         // here, urbp->cur_td is already the next td;
2245         return urbp->cur_td;
2246 }
2247
2248 /*
2249  * Scan the list to find a active urb
2250  */
2251 static inline struct urb* sl811_get_list_next_urb(struct sl811_hc *hc, struct list_head *next)
2252 {
2253         struct urb *urb;
2254         int i;
2255         
2256         if (list_empty(next))
2257                 return NULL;
2258         
2259         if (next == hc->cur_list)
2260                 return NULL;
2261                         
2262         for (i = 0; i < 4; ++i) 
2263                 if (next == &hc->urb_list[i])
2264                         return NULL;
2265                         
2266         urb = list_entry(next, struct urb, urb_list);
2267         PDEBUG(4, "next urb in list is at %p", urb);
2268                 
2269         return urb;
2270 }
2271
2272 /*
2273  * Find the next td to transfer.
2274  */
2275 static struct sl811_td* sl811_schedule_next_urb(struct sl811_hc *hc, struct list_head *next)
2276 {
2277         struct urb *urb = NULL;
2278         int back_loop = 1;
2279         struct list_head *old_list = hc->cur_list;
2280                 
2281         // try to get next urb in the same list.
2282         if (next) {
2283                 urb = sl811_get_list_next_urb(hc, next);
2284                 if (!urb)
2285                         ++hc->cur_list;
2286         }
2287
2288         // try other list.
2289         if (!urb) {                     
2290 re_loop:
2291                 // try all the list.
2292                 while (hc->cur_list < &hc->urb_list[4]) { 
2293                         if ((urb = sl811_get_list_next_urb(hc, hc->cur_list->next)))
2294                                 return ((struct sl811_urb_priv *)urb->hcpriv)->cur_td;
2295                         ++hc->cur_list;
2296                 }
2297                 // the last list is try 
2298                 if (back_loop && (old_list >= &hc->ctrl_list)) {
2299                         hc->cur_list = &hc->ctrl_list;
2300                         back_loop = 0;
2301                         goto re_loop;
2302                 }
2303         }
2304         
2305         if (hc->cur_list > &hc->urb_list[3])
2306                 hc->cur_list = &hc->ctrl_list;
2307                         
2308         return NULL;
2309 }
2310
2311 /*
2312  * This function process the transfer rusult.
2313  */
2314 static void sl811_transfer_done(struct sl811_hc *hc, int sof) 
2315 {
2316         struct sl811_td *cur_td = hc->cur_td, *next_td = NULL;
2317         struct urb *cur_urb = NULL;
2318         struct list_head *next = NULL;
2319         int done;
2320         
2321         PDEBUG(5, "enter");
2322         
2323         if (cur_td == NULL) {
2324                 PDEBUG(1, "in done interrupt, but td is null, be already parsed?");
2325                 return ;
2326         }
2327
2328         cur_urb = cur_td->urb;
2329         hc->cur_td = NULL;
2330         next = &cur_urb->urb_list;
2331         next = next->next;
2332         
2333         spin_lock(&cur_urb->lock);      
2334         sl811_parse_cur_td(hc, cur_td);
2335         done = sl811_parse_cur_urb(cur_urb);
2336         spin_unlock(&cur_urb->lock);
2337         
2338         if (done) {
2339                 list_del_init(&cur_urb->urb_list);
2340                 cur_td = NULL;
2341                 sl811_result_urb(cur_urb);      
2342         }
2343
2344         if (sof)
2345                 return ;
2346         
2347         if (!done) {
2348                 next_td = sl811_schedule_next_td(cur_urb, cur_td);
2349                 if (next_td && next_td != cur_td && (sl811_calc_bus_remainder(hc) > next_td->bustime)) {
2350                         hc->cur_td = next_td;
2351                         PDEBUG(5, "ADD TD");
2352                         sl811_trans_cur_td(hc, next_td);
2353                         return ;
2354                 }
2355         }
2356         
2357         while (1) {
2358                 next_td = sl811_schedule_next_urb(hc, next);
2359                 if (!next_td)
2360                         return;
2361                 if (next_td == cur_td)
2362                         return;
2363                 next = &next_td->urb->urb_list;
2364                 next = next->next;
2365                 if (sl811_calc_bus_remainder(hc) > next_td->bustime) {
2366                         hc->cur_td = next_td;
2367                         PDEBUG(5, "ADD TD");
2368                         sl811_trans_cur_td(hc, next_td);
2369                         return ;
2370                 }
2371         }
2372 }
2373
2374 /*
2375  *
2376  */
2377 static void inline sl811_dec_intr_interval(struct sl811_hc *hc)
2378 {
2379         struct list_head *head, *tmp;
2380         struct urb *urb;
2381         struct sl811_urb_priv *urbp;
2382         
2383         if (list_empty(&hc->idle_intr_list))
2384                 return ;
2385         
2386         head = &hc->idle_intr_list;
2387         tmp = head->next;
2388         
2389         while (tmp != head) {
2390                 urb = list_entry(tmp, struct urb, urb_list);
2391                 tmp = tmp->next;
2392                 spin_lock(&urb->lock);
2393                 urbp = urb->hcpriv;
2394                 if (--urbp->interval == 0) {
2395                         list_del(&urb->urb_list);
2396                         list_add(&urb->urb_list, &hc->intr_list);
2397                         PDEBUG(4, "intr urb active");
2398                 }
2399                 spin_unlock(&urb->lock);
2400         }
2401 }
2402
2403 /*
2404  * The sof interrupt is happen. 
2405  */
2406 static void sl811_start_sof(struct sl811_hc *hc)
2407 {
2408         struct sl811_td *next_td;
2409 #ifdef SL811_DEBUG
2410         static struct sl811_td *repeat_td = NULL;
2411         static int repeat_cnt = 1;
2412 #endif  
2413         if (++hc->frame_number > 1024)
2414                 hc->frame_number = 0;
2415         
2416         if (hc->active_urbs == 0)
2417                 return ;
2418         
2419         sl811_dec_intr_interval(hc);
2420         
2421         if (hc->cur_td) {
2422                 if (sl811_read(hc, 0) & SL811_USB_CTRL_ARM) {
2423 #ifdef SL811_DEBUG
2424                         if (repeat_td == hc->cur_td) 
2425                                 ++repeat_cnt;
2426                         else {
2427                                 if (repeat_cnt >= 2)
2428                                         PDEBUG(2, "cur td = %p repeat %d", hc->cur_td, repeat_cnt);
2429                                 repeat_cnt = 1;
2430                                 repeat_td = hc->cur_td;
2431                         }
2432 #endif
2433                         return ;
2434                 } else {
2435                         PDEBUG(2, "lost of interrupt in sof? do parse!");
2436                         sl811_transfer_done(hc, 1);
2437                         
2438                         // let this frame idle  
2439                         return;
2440                 }
2441         }
2442         
2443         hc->cur_list = &hc->iso_list;
2444         
2445         if (hc->active_urbs == 0)
2446                 return ;
2447         
2448         next_td = sl811_schedule_next_urb(hc, NULL);
2449         if (!next_td) {
2450 #ifdef SL811_DEBUG
2451                 if (list_empty(&hc->idle_intr_list))
2452                         PDEBUG(2, "not schedule a td, why? urbs = %d", hc->active_urbs);
2453 #endif
2454                 return; 
2455         }
2456         if (sl811_calc_bus_remainder(hc) > next_td->bustime) {
2457                 hc->cur_td = next_td;
2458                 sl811_trans_cur_td(hc, next_td);
2459         } else
2460                 PDEBUG(2, "bus time if not enough, why?");
2461 }
2462
2463 /*
2464  * This function resets SL811HS controller and detects the speed of
2465  * the connecting device
2466  *
2467  * Return: 0 = no device attached; 1 = USB device attached
2468  */
2469 static int sl811_hc_reset(struct sl811_hc *hc)
2470 {
2471         int status ;
2472
2473         sl811_write(hc, SL811_CTRL2, SL811_CTL2_HOST | SL811_12M_HI);
2474         sl811_write(hc, SL811_CTRL1, SL811_CTRL1_RESET);
2475
2476         mdelay(20);
2477         
2478         // Disable hardware SOF generation, clear all irq status.
2479         sl811_write(hc, SL811_CTRL1, 0);
2480         mdelay(2);
2481         sl811_write(hc, SL811_INTRSTS, 0xff); 
2482         status = sl811_read(hc, SL811_INTRSTS);
2483
2484         if (status & SL811_INTR_NOTPRESENT) {
2485                 // Device is not present
2486                 PDEBUG(0, "Device not present");
2487                 hc->rh_status.wPortStatus &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE);
2488                 hc->rh_status.wPortChange |= USB_PORT_STAT_C_CONNECTION;
2489                 sl811_write(hc, SL811_INTR, SL811_INTR_INSRMV);
2490                 return 0;
2491         }
2492
2493         // Send SOF to address 0, endpoint 0.
2494         sl811_write(hc, SL811_LEN_B, 0);
2495         sl811_write(hc, SL811_PIDEP_B, PIDEP(USB_PID_SOF, 0));
2496         sl811_write(hc, SL811_DEV_B, 0x00);
2497         sl811_write (hc, SL811_SOFLOW, SL811_12M_HI);
2498
2499         if (status & SL811_INTR_SPEED_FULL) {
2500                 /* full speed device connect directly to root hub */
2501                 PDEBUG (0, "Full speed Device attached");
2502                 
2503                 sl811_write(hc, SL811_CTRL1, SL811_CTRL1_RESET);
2504                 mdelay(20);
2505                 sl811_write(hc, SL811_CTRL2, SL811_CTL2_HOST | SL811_12M_HI);
2506                 sl811_write(hc, SL811_CTRL1, SL811_CTRL1_SOF);
2507
2508                 /* start the SOF or EOP */
2509                 sl811_write(hc, SL811_CTRL_B, SL811_USB_CTRL_ARM);
2510                 hc->rh_status.wPortStatus |= USB_PORT_STAT_CONNECTION;
2511                 hc->rh_status.wPortStatus &= ~USB_PORT_STAT_LOW_SPEED;
2512                 mdelay(2);
2513                 sl811_write (hc, SL811_INTRSTS, 0xff);
2514         } else {
2515                 /* slow speed device connect directly to root-hub */
2516                 PDEBUG(0, "Low speed Device attached");
2517                 
2518                 sl811_write(hc, SL811_CTRL1, SL811_CTRL1_RESET);
2519                 mdelay(20);
2520                 sl811_write(hc, SL811_CTRL2, SL811_CTL2_HOST | SL811_CTL2_DSWAP | SL811_12M_HI);
2521                 sl811_write(hc, SL811_CTRL1, SL811_CTRL1_SPEED_LOW | SL811_CTRL1_SOF);
2522
2523                 /* start the SOF or EOP */
2524                 sl811_write(hc, SL811_CTRL_B, SL811_USB_CTRL_ARM);
2525                 hc->rh_status.wPortStatus |= USB_PORT_STAT_CONNECTION | USB_PORT_STAT_LOW_SPEED;
2526                 mdelay(2);
2527                 sl811_write(hc, SL811_INTRSTS, 0xff);
2528         }
2529
2530         hc->rh_status.wPortChange |= USB_PORT_STAT_C_CONNECTION;
2531         sl811_write(hc, SL811_INTR, SL811_INTR_INSRMV);
2532         
2533         return 1;
2534 }
2535
2536 /*
2537  * Interrupt service routine.
2538  */
2539 static void sl811_interrupt(int irq, void *__hc, struct pt_regs * r)
2540 {
2541         __u8 status;
2542         struct sl811_hc *hc = __hc;
2543
2544         status = sl811_read(hc, SL811_INTRSTS);
2545         if (status == 0)
2546                 return ; /* Not me */
2547
2548         sl811_write(hc, SL811_INTRSTS, 0xff);
2549
2550         if (status & SL811_INTR_INSRMV) {
2551                 sl811_write(hc, SL811_INTR, 0);
2552                 sl811_write(hc, SL811_CTRL1, 0);
2553                 // wait for device stable
2554                 mdelay(100);                    
2555                 sl811_hc_reset(hc);
2556                 return ;
2557         }
2558
2559         spin_lock(&hc->hc_lock);
2560         
2561         if (status & SL811_INTR_DONE_A) {
2562                 if (status & SL811_INTR_SOF) {
2563                         sl811_transfer_done(hc, 1);
2564                         PDEBUG(4, "sof in done!");
2565                         sl811_start_sof(hc);
2566                 } else
2567                         sl811_transfer_done(hc, 0);
2568         } else if (status & SL811_INTR_SOF)
2569                 sl811_start_sof(hc);    
2570
2571         spin_unlock(&hc->hc_lock);      
2572
2573         return ;
2574 }
2575
2576 /*
2577  * This function allocates all data structure and store in the
2578  * private data structure.
2579  *
2580  * Return value  : data structure for the host controller
2581  */
2582 static struct sl811_hc* __devinit sl811_alloc_hc(void)
2583 {
2584         struct sl811_hc *hc;
2585         struct usb_bus *bus;
2586         int i;
2587
2588         PDEBUG(5, "enter");
2589
2590         hc = (struct sl811_hc *)kmalloc(sizeof(struct sl811_hc), GFP_KERNEL);
2591         if (!hc)
2592                 return NULL;
2593
2594         memset(hc, 0, sizeof(struct sl811_hc));
2595
2596         hc->rh_status.wPortStatus = USB_PORT_STAT_POWER;
2597         hc->rh_status.wPortChange = 0;
2598
2599         hc->active_urbs = 0;
2600         INIT_LIST_HEAD(&hc->hc_hcd_list);
2601         list_add(&hc->hc_hcd_list, &sl811_hcd_list);
2602         
2603         init_waitqueue_head(&hc->waitq);
2604         
2605         for (i = 0; i < 6; ++i)
2606                 INIT_LIST_HEAD(&hc->urb_list[i]);
2607         
2608         hc->cur_list = &hc->iso_list;
2609
2610         bus = usb_alloc_bus(&sl811_device_operations);
2611         if (!bus) {
2612                 kfree (hc);
2613                 return NULL;
2614         }
2615
2616         hc->bus = bus;
2617         bus->bus_name = MODNAME;
2618         bus->hcpriv = hc;
2619
2620         return hc;
2621 }
2622
2623 /*
2624  * This function De-allocate all resources
2625  */
2626 static void sl811_release_hc(struct sl811_hc *hc)
2627 {
2628         PDEBUG(5, "enter");
2629
2630         /* disconnect all devices */
2631         if (hc->bus->root_hub)
2632                 usb_disconnect(&hc->bus->root_hub);
2633
2634         // Stop interrupt handle
2635         if (hc->irq)
2636                 free_irq(hc->irq, hc);
2637         hc->irq = 0;
2638
2639         /* Stop interrupt for sharing, but only, if PatternTest ok */
2640         if (hc->addr_io) {
2641                 /* Disable Interrupts */
2642                 sl811_write(hc, SL811_INTR, 0);
2643
2644                 /* Remove all Interrupt events */
2645                 mdelay(2);
2646                 sl811_write(hc, SL811_INTRSTS, 0xff);
2647         }
2648
2649         /* free io regions */
2650         sl811_release_regions(hc);
2651
2652         usb_deregister_bus(hc->bus);
2653         usb_free_bus(hc->bus);
2654
2655         list_del(&hc->hc_hcd_list);
2656         INIT_LIST_HEAD(&hc->hc_hcd_list);
2657
2658         kfree (hc);
2659 }
2660
2661 /*
2662  * This function request IO memory regions, request IRQ, and
2663  * allocate all other resources.
2664  *
2665  * Input: addr_io = first IO address
2666  *        data_io = second IO address
2667  *        irq = interrupt number
2668  *
2669  * Return: 0 = success or error condition
2670  */
2671 static int __devinit sl811_found_hc(int addr_io, int data_io, int irq)
2672 {
2673         struct sl811_hc *hc;
2674
2675         PDEBUG(5, "enter");
2676
2677         hc = sl811_alloc_hc();
2678         if (!hc)
2679                 return -ENOMEM;
2680
2681         if (sl811_request_regions (hc, addr_io, data_io, MODNAME)) {
2682                 PDEBUG(1, "ioport %X,%X is in use!", addr_io, data_io);
2683                 sl811_release_hc(hc);
2684                 return -EBUSY;
2685         }
2686
2687         if (sl811_reg_test(hc)) {
2688                 PDEBUG(1, "SL811 register test failed!");
2689                 sl811_release_hc(hc);
2690                 return -ENODEV;
2691         }
2692         
2693 #ifdef SL811_DEBUG_VERBOSE
2694         {
2695             __u8 u = SL811Read (hci, SL11H_HWREVREG);
2696             
2697             // Show the hardware revision of chip
2698             PDEBUG(1, "SL811 HW: %02Xh", u);
2699             switch (u & 0xF0) {
2700             case 0x00: PDEBUG(1, "SL11H");              break;
2701             case 0x10: PDEBUG(1, "SL811HS rev1.2");     break;
2702             case 0x20: PDEBUG(1, "SL811HS rev1.5");     break;
2703             default:   PDEBUG(1, "Revision unknown!");
2704             }
2705         }
2706 #endif // SL811_DEBUG_VERBOSE
2707
2708         sl811_init_irq();
2709
2710         usb_register_bus(hc->bus);
2711
2712         if (request_irq(irq, sl811_interrupt, SA_SHIRQ, MODNAME, hc)) {
2713                 PDEBUG(1, "request interrupt %d failed", irq);
2714                 sl811_release_hc(hc);
2715                 return -EBUSY;
2716         }
2717         hc->irq = irq;
2718
2719         printk(KERN_INFO __FILE__ ": USB SL811 at %x,%x, IRQ %d\n",
2720                 addr_io, data_io, irq);
2721
2722         sl811_hc_reset(hc);
2723         sl811_connect_rh(hc);
2724         
2725         return 0;
2726 }
2727
2728 /*
2729  * This is an init function, and it is the first function being called
2730  *
2731  * Return: 0 = success or error condition
2732  */
2733 static int __init sl811_hcd_init(void)
2734 {
2735         int ret = -ENODEV;
2736         int count;
2737         
2738         PDEBUG(5, "enter");
2739
2740         info(DRIVER_VERSION " : " DRIVER_DESC);
2741
2742         // registering some instance
2743         for (count = 0; count < MAX_CONTROLERS; count++) {
2744                 if (io[count]) {
2745                         ret = sl811_found_hc(io[count], io[count]+OFFSET_DATA_REG, irq[count]);
2746                         if (ret)
2747                                 return (ret);
2748                 }
2749         }
2750
2751         return ret;
2752 }
2753
2754 /*
2755  * This is a cleanup function, and it is called when module is unloaded.
2756  */
2757 static void __exit sl811_hcd_cleanup(void)
2758 {
2759         struct list_head *list = sl811_hcd_list.next;
2760         struct sl811_hc *hc;
2761
2762         PDEBUG(5, "enter");
2763
2764         for (; list != &sl811_hcd_list; ) {
2765                 hc = list_entry(list, struct sl811_hc, hc_hcd_list);
2766                 list = list->next;
2767                 sl811_release_hc(hc);
2768         }
2769 }
2770
2771 module_init(sl811_hcd_init);
2772 module_exit(sl811_hcd_cleanup);
2773
2774 MODULE_AUTHOR(DRIVER_AUTHOR);
2775 MODULE_DESCRIPTION(DRIVER_DESC);