ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / host / hc_simple.c
1 /*-------------------------------------------------------------------------*/
2 /*-------------------------------------------------------------------------*
3  * simple generic USB HCD frontend Version 0.9.5 (10/28/2001)
4  * for embedded HCs (SL811HS)
5  * 
6  * USB URB handling, hci_ hcs_
7  * URB queueing, qu_
8  * Transfer scheduling, sh_
9  * 
10  *
11  *-------------------------------------------------------------------------*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  *-------------------------------------------------------------------------*/
27
28 /* main lock for urb access */
29 static spinlock_t usb_urb_lock = SPIN_LOCK_UNLOCKED;
30
31 /*-------------------------------------------------------------------------*/
32 /*-------------------------------------------------------------------------*/
33 /* URB HCD API function layer
34  * * * */
35
36 /***************************************************************************
37  * Function Name : hcs_urb_queue
38  *
39  * This function initializes the urb status and length before queueing the 
40  * urb. 
41  *
42  * Input:  hci = data structure for the host controller
43  *         urb = USB request block data structure 
44  *
45  * Return: 0 
46  **************************************************************************/
47 static inline int hcs_urb_queue (hci_t * hci, struct urb * urb)
48 {
49         int i;
50
51         DBGFUNC ("enter hcs_urb_queue\n");
52         if (usb_pipeisoc (urb->pipe)) {
53                 DBGVERBOSE ("hcs_urb_queue: isoc pipe\n");
54                 for (i = 0; i < urb->number_of_packets; i++) {
55                         urb->iso_frame_desc[i].actual_length = 0;
56                         urb->iso_frame_desc[i].status = -EXDEV;
57                 }
58
59                 /* urb->next hack : 1 .. resub, 0 .. single shot */
60                 /* urb->interval = urb->next ? 1 : 0; */
61         }
62
63         urb->status = -EINPROGRESS;
64         urb->actual_length = 0;
65         urb->error_count = 0;
66
67         if (usb_pipecontrol (urb->pipe))
68                 hc_flush_data_cache (hci, urb->setup_packet, 8);
69         if (usb_pipeout (urb->pipe))
70                 hc_flush_data_cache (hci, urb->transfer_buffer,
71                                      urb->transfer_buffer_length);
72
73         qu_queue_urb (hci, urb);
74
75         return 0;
76 }
77
78 /***************************************************************************
79  * Function Name : hcs_return_urb
80  *
81  * This function the return path of URB back to the USB core. It calls the
82  * the urb complete function if exist, and also handles the resubmition of
83  * interrupt URBs.
84  *
85  * Input:  hci = data structure for the host controller
86  *         urb = USB request block data structure 
87  *         resub_ok = resubmit flag: 1 = submit urb again, 0 = not submit 
88  *
89  * Return: 0 
90  **************************************************************************/
91 static int hcs_return_urb (hci_t * hci, struct urb * urb, int resub_ok)
92 {
93         struct usb_device *dev = urb->dev;
94         int resubmit = 0;
95
96         DBGFUNC ("enter hcs_return_urb, urb pointer = 0x%x, "
97                  "transferbuffer point = 0x%x, "
98                  " setup packet pointer = 0x%x, context pointer = 0x%x \n",
99                  (__u32 *) urb, (__u32 *) urb->transfer_buffer,
100                  (__u32 *) urb->setup_packet, (__u32 *) urb->context);
101         if (urb_debug)
102                 urb_print (urb, "RET", usb_pipeout (urb->pipe));
103
104         resubmit = urb->interval && resub_ok;
105
106         urb->dev = urb->hcpriv = NULL;
107
108         if (urb->complete) {
109                 urb->complete (urb, NULL);      /* call complete */
110         }
111
112         if (resubmit) {
113                 /* requeue the URB */
114                 urb->dev = dev;
115                 hcs_urb_queue (hci, urb);
116         } else {
117                 usb_put_urb(urb);
118         }
119
120         return 0;
121 }
122
123 /***************************************************************************
124  * Function Name : hci_submit_urb
125  *
126  * This function is called by the USB core API when an URB is available to
127  * process.  This function does the following
128  *
129  * 1) Check the validity of the URB
130  * 2) Parse the device number from the URB
131  * 3) Pass the URB to the root hub routine if its intended for the hub, else
132  *    queue the urb for the attached device. 
133  *
134  * Input: urb = USB request block data structure 
135  *
136  * Return: 0 if success or error code 
137  **************************************************************************/
138 static int hci_submit_urb (struct urb * urb, int mem_flags)
139 {
140         hci_t *hci;
141         unsigned int pipe = urb->pipe;
142         unsigned long flags;
143         int ret;
144
145         DBGFUNC ("enter hci_submit_urb, pipe = 0x%x\n", urb->pipe);
146         if (!urb->dev || !urb->dev->bus || urb->hcpriv)
147                 return -EINVAL;
148
149         if (usb_endpoint_halted
150             (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe))) {
151                 printk ("hci_submit_urb: endpoint_halted\n");
152                 return -EPIPE;
153         }
154         hci = (hci_t *) urb->dev->bus->hcpriv;
155
156         /* a request to the virtual root hub */
157         if (usb_pipedevice (pipe) == hci->rh.devnum) {
158                 if (urb_debug > 1)
159                         urb_print (urb, "SUB-RH", usb_pipein (pipe));
160
161                 return rh_submit_urb (urb);
162         }
163
164         /* increment urb's reference count, we now control it. */
165         urb = usb_get_urb (urb);
166
167         /* queue the URB to its endpoint-queue */
168         spin_lock_irqsave (&usb_urb_lock, flags);
169         ret = hcs_urb_queue (hci, urb);
170         if (ret != 0) {
171                 /* error on return */
172                 DBGERR ("hci_submit_urb: return err, ret = 0x%x, urb->status = 0x%x\n",
173                         ret, urb->status);
174                 usb_put_urb (urb);
175         }
176
177         spin_unlock_irqrestore (&usb_urb_lock, flags);
178
179         return ret;
180
181 }
182
183 /***************************************************************************
184  * Function Name : hci_unlink_urb
185  *
186  * This function mark the URB to unlink
187  *
188  * Input: urb = USB request block data structure 
189  *
190  * Return: 0 if success or error code 
191  **************************************************************************/
192 static int hci_unlink_urb (struct urb * urb)
193 {
194         unsigned long flags;
195         hci_t *hci;
196         DECLARE_WAITQUEUE (wait, current);
197         void *comp = NULL;
198
199         DBGFUNC ("enter hci_unlink_urb\n");
200
201         if (!urb)               /* just to be sure */
202                 return -EINVAL;
203
204         if (!urb->dev || !urb->dev->bus)
205                 return -ENODEV;
206
207         hci = (hci_t *) urb->dev->bus->hcpriv;
208
209         /* a request to the virtual root hub */
210         if (usb_pipedevice (urb->pipe) == hci->rh.devnum) {
211                 return rh_unlink_urb (urb);
212         }
213
214         if (urb_debug)
215                 urb_print (urb, "UNLINK", 1);
216
217         spin_lock_irqsave (&usb_urb_lock, flags);
218
219         if (!list_empty (&urb->urb_list) && urb->status == -EINPROGRESS) {
220                 /* URB active? */
221
222                 if (urb->transfer_flags & URB_ASYNC_UNLINK) {
223                         /* asynchronous with callback */
224                         /* relink the urb to the del list */
225                         list_move (&urb->urb_list, &hci->del_list);
226                         spin_unlock_irqrestore (&usb_urb_lock, flags);
227                 } else {
228                         /* synchronous without callback */
229
230                         add_wait_queue (&hci->waitq, &wait);
231
232                         set_current_state (TASK_UNINTERRUPTIBLE);
233                         comp = urb->complete;
234                         urb->complete = NULL;
235
236                         /* relink the urb to the del list */
237                         list_move(&urb->urb_list, &hci->del_list);
238
239                         spin_unlock_irqrestore (&usb_urb_lock, flags);
240
241                         schedule_timeout (HZ / 50);
242
243                         if (!list_empty (&urb->urb_list))
244                                 list_del (&urb->urb_list);
245
246                         urb->complete = comp;
247                         urb->hcpriv = NULL;
248                         remove_wait_queue (&hci->waitq, &wait);
249                 }
250         } else {
251                 /* hcd does not own URB but we keep the driver happy anyway */
252                 spin_unlock_irqrestore (&usb_urb_lock, flags);
253
254                 if (urb->complete && (urb->transfer_flags & URB_ASYNC_UNLINK)) {
255                         urb->status = -ENOENT;
256                         urb->actual_length = 0;
257                         urb->complete (urb, NULL);
258                         urb->status = 0;
259                 } else {
260                         urb->status = -ENOENT;
261                 }
262         }
263
264         return 0;
265 }
266
267 /***************************************************************************
268  * Function Name : hci_alloc_dev
269  *
270  * This function allocates private data space for the usb device and 
271  * initialize the endpoint descriptor heads.
272  *
273  * Input: usb_dev = pointer to the usb device 
274  *
275  * Return: 0 if success or error code 
276  **************************************************************************/
277 static int hci_alloc_dev (struct usb_device *usb_dev)
278 {
279         struct hci_device *dev;
280         int i;
281
282         DBGFUNC ("enter hci_alloc_dev\n");
283         dev = kmalloc (sizeof (*dev), GFP_KERNEL);
284         if (!dev)
285                 return -ENOMEM;
286
287         memset (dev, 0, sizeof (*dev));
288
289         for (i = 0; i < 32; i++) {
290                 INIT_LIST_HEAD (&(dev->ed[i].urb_queue));
291                 dev->ed[i].pipe_head = NULL;
292         }
293
294         usb_dev->hcpriv = dev;
295
296         DBGVERBOSE ("USB HC dev alloc %d bytes\n", sizeof (*dev));
297
298         return 0;
299
300 }
301
302 /***************************************************************************
303  * Function Name : hci_free_dev
304  *
305  * This function de-allocates private data space for the usb devic
306  *
307  * Input: usb_dev = pointer to the usb device 
308  *
309  * Return: 0  
310  **************************************************************************/
311 static int hci_free_dev (struct usb_device *usb_dev)
312 {
313         DBGFUNC ("enter hci_free_dev\n");
314
315         if (usb_dev->hcpriv)
316                 kfree (usb_dev->hcpriv);
317
318         usb_dev->hcpriv = NULL;
319
320         return 0;
321 }
322
323 /***************************************************************************
324  * Function Name : hci_get_current_frame_number
325  *
326  * This function get the current USB frame number
327  *
328  * Input: usb_dev = pointer to the usb device 
329  *
330  * Return: frame number  
331  **************************************************************************/
332 static int hci_get_current_frame_number (struct usb_device *usb_dev)
333 {
334         hci_t *hci = usb_dev->bus->hcpriv;
335         DBGFUNC ("enter hci_get_current_frame_number, frame = 0x%x \r\n",
336                  hci->frame_number);
337
338         return (hci->frame_number);
339 }
340
341 /***************************************************************************
342  * List of all io-functions 
343  **************************************************************************/
344
345 static struct usb_operations hci_device_operations = {
346         .allocate =             hci_alloc_dev,
347         .deallocate =           hci_free_dev,
348         .get_frame_number =     hci_get_current_frame_number,
349         .submit_urb =           hci_submit_urb,
350         .unlink_urb =           hci_unlink_urb,
351 };
352
353 /***************************************************************************
354  * URB queueing:
355  * 
356  * For each type of transfer (INTR, BULK, ISO, CTRL) there is a list of 
357  * active URBs.
358  * (hci->intr_list, hci->bulk_list, hci->iso_list, hci->ctrl_list)
359  * For every endpoint the head URB of the queued URBs is linked to one of 
360  * those lists.
361  * 
362  * The rest of the queued URBs of an endpoint are linked into a 
363  * private URB list for each endpoint. (hci_dev->ed [endpoint_io].urb_queue)
364  * hci_dev->ed [endpoint_io].pipe_head .. points to the head URB which is 
365  * in one of the active URB lists.
366  * 
367  * The index of an endpoint consists of its number and its direction.
368  * 
369  * The state of an intr and iso URB is 0. 
370  * For ctrl URBs the states are US_CTRL_SETUP, US_CTRL_DATA, US_CTRL_ACK
371  * Bulk URBs states are US_BULK and US_BULK0 (with 0-len packet)
372  * 
373  **************************************************************************/
374
375 /***************************************************************************
376  * Function Name : qu_urb_timeout
377  *
378  * This function is called when the URB timeout. The function unlinks the 
379  * URB. 
380  *
381  * Input: lurb: URB 
382  *
383  * Return: none  
384  **************************************************************************/
385 #ifdef HC_URB_TIMEOUT
386 static void qu_urb_timeout (unsigned long lurb)
387 {
388         struct urb *urb = (struct urb *) lurb;
389
390         DBGFUNC ("enter qu_urb_timeout\n");
391         hci_unlink_urb (urb);
392 }
393 #endif
394
395 /***************************************************************************
396  * Function Name : qu_pipeindex
397  *
398  * This function gets the index of the pipe.   
399  *
400  * Input: pipe: the urb pipe 
401  *
402  * Return: index  
403  **************************************************************************/
404 static inline int qu_pipeindex (__u32 pipe)
405 {
406         DBGFUNC ("enter qu_pipeindex\n");
407         return (usb_pipeendpoint (pipe) << 1) | (usb_pipecontrol (pipe) ? 0 : usb_pipeout (pipe));
408 }
409
410 /***************************************************************************
411  * Function Name : qu_seturbstate
412  *
413  * This function set the state of the URB.  
414  * 
415  * control pipe: 3 states -- Setup, data, status
416  * interrupt and bulk pipe: 1 state -- data    
417  *
418  * Input: urb = USB request block data structure 
419  *        state = the urb state
420  *
421  * Return: none  
422  **************************************************************************/
423 static inline void qu_seturbstate (struct urb * urb, int state)
424 {
425         DBGFUNC ("enter qu_seturbstate\n");
426         urb->pipe &= ~0x1f;
427         urb->pipe |= state & 0x1f;
428 }
429
430 /***************************************************************************
431  * Function Name : qu_urbstate
432  *
433  * This function get the current state of the URB.  
434  * 
435  * Input: urb = USB request block data structure 
436  *
437  * Return: none  
438  **************************************************************************/
439 static inline int qu_urbstate (struct urb * urb)
440 {
441
442         DBGFUNC ("enter qu_urbstate\n");
443
444         return urb->pipe & 0x1f;
445 }
446
447 /***************************************************************************
448  * Function Name : qu_queue_active_urb
449  *
450  * This function adds the urb to the appropriate active urb list and set
451  * the urb state.
452  * 
453  * There are four active lists: isochoronous list, interrupt list, 
454  * control list, and bulk list.
455  * 
456  * Input: hci = data structure for the host controller 
457  *        urb = USB request block data structure 
458  *        ed = endpoint descriptor
459  *
460  * Return: none  
461  **************************************************************************/
462 static inline void qu_queue_active_urb (hci_t * hci, struct urb * urb, epd_t * ed)
463 {
464         int urb_state = 0;
465         DBGFUNC ("enter qu_queue_active_urb\n");
466         switch (usb_pipetype (urb->pipe)) {
467         case PIPE_CONTROL:
468                 list_add (&urb->urb_list, &hci->ctrl_list);
469                 urb_state = US_CTRL_SETUP;
470                 break;
471
472         case PIPE_BULK:
473                 list_add (&urb->urb_list, &hci->bulk_list);
474                 if ((urb->transfer_flags & URB_ZERO_PACKET)
475                     && urb->transfer_buffer_length > 0
476                     &&
477                     ((urb->transfer_buffer_length %
478                       usb_maxpacket (urb->dev, urb->pipe,
479                                      usb_pipeout (urb->pipe))) == 0)) {
480                         urb_state = US_BULK0;
481                 }
482                 break;
483
484         case PIPE_INTERRUPT:
485                 urb->start_frame = hci->frame_number;
486                 list_add (&urb->urb_list, &hci->intr_list);
487                 break;
488
489         case PIPE_ISOCHRONOUS:
490                 list_add (&urb->urb_list, &hci->iso_list);
491                 break;
492         }
493
494 #ifdef HC_URB_TIMEOUT
495         if (urb->timeout) {
496                 ed->timeout.data = (unsigned long) urb;
497                 ed->timeout.expires = urb->timeout + jiffies;
498                 ed->timeout.function = qu_urb_timeout;
499                 add_timer (&ed->timeout);
500         }
501 #endif
502
503         qu_seturbstate (urb, urb_state);
504 }
505
506 /***************************************************************************
507  * Function Name : qu_queue_urb
508  *
509  * This function adds the urb to the endpoint descriptor list 
510  * 
511  * Input: hci = data structure for the host controller 
512  *        urb = USB request block data structure 
513  *
514  * Return: none  
515  **************************************************************************/
516 static int qu_queue_urb (hci_t * hci, struct urb * urb)
517 {
518         struct hci_device *hci_dev = usb_to_hci (urb->dev);
519         epd_t *ed = &hci_dev->ed[qu_pipeindex (urb->pipe)];
520
521         DBGFUNC ("Enter qu_queue_urb\n");
522
523         /* for ISOC transfers calculate start frame index */
524
525         if (usb_pipeisoc (urb->pipe) && urb->transfer_flags & URB_ISO_ASAP) {
526                 urb->start_frame = ((ed->pipe_head) ? (ed->last_iso + 1) : hci_get_current_frame_number (urb-> dev) + 1) & 0xffff;
527         }
528
529         if (ed->pipe_head) {
530                 __list_add (&urb->urb_list, ed->urb_queue.prev,
531                             &(ed->urb_queue));
532         } else {
533                 ed->pipe_head = urb;
534                 qu_queue_active_urb (hci, urb, ed);
535                 if (++hci->active_urbs == 1)
536                         hc_start_int (hci);
537         }
538
539         return 0;
540 }
541
542 /***************************************************************************
543  * Function Name : qu_next_urb
544  *
545  * This function removes the URB from the queue and add the next URB to 
546  * active list. 
547  * 
548  * Input: hci = data structure for the host controller 
549  *        urb = USB request block data structure 
550  *        resub_ok = resubmit flag
551  *
552  * Return: pointer to the next urb  
553  **************************************************************************/
554 static struct urb *qu_next_urb (hci_t * hci, struct urb * urb, int resub_ok)
555 {
556         struct hci_device *hci_dev = usb_to_hci (urb->dev);
557         epd_t *ed = &hci_dev->ed[qu_pipeindex (urb->pipe)];
558
559         DBGFUNC ("enter qu_next_urb\n");
560         list_del_init(&urb->urb_list);
561
562         if (ed->pipe_head == urb) {
563 #ifdef HC_URB_TIMEOUT
564                 if (urb->timeout)
565                         del_timer (&ed->timeout);
566 #endif
567
568                 if (!--hci->active_urbs)
569                         hc_stop_int (hci);
570
571                 if (!list_empty (&ed->urb_queue)) {
572                         urb = list_entry (ed->urb_queue.next, struct urb, urb_list);
573                         list_del_init (&urb->urb_list);
574                         ed->pipe_head = urb;
575                         qu_queue_active_urb (hci, urb, ed);
576                 } else {
577                         ed->pipe_head = NULL;
578                         urb = NULL;
579                 }
580         }
581         return urb;
582 }
583
584 /***************************************************************************
585  * Function Name : qu_return_urb
586  *
587  * This function is part of the return path.   
588  * 
589  * Input: hci = data structure for the host controller 
590  *        urb = USB request block data structure 
591  *        resub_ok = resubmit flag
592  *
593  * Return: pointer to the next urb  
594  **************************************************************************/
595 static struct urb *qu_return_urb (hci_t * hci, struct urb * urb, int resub_ok)
596 {
597         struct urb *next_urb;
598
599         DBGFUNC ("enter qu_return_rub\n");
600         next_urb = qu_next_urb (hci, urb, resub_ok);
601         hcs_return_urb (hci, urb, resub_ok);
602         return next_urb;
603 }
604
605 /***************************************************************************
606  * Function Name : sh_scan_iso_urb_list
607  *
608  * This function goes through the isochronous urb list and schedule the 
609  * the transfer.   
610  *
611  * Note: This function has not tested yet
612  * 
613  * Input: hci = data structure for the host controller 
614  *        list_lh = pointer to the isochronous list 
615  *        frame_number = the frame number 
616  *
617  * Return: 0 = unsuccessful; 1 = successful  
618  **************************************************************************/
619 static int sh_scan_iso_urb_list (hci_t * hci, struct list_head *list_lh,
620                                  int frame_number)
621 {
622         struct list_head *lh = list_lh->next;
623         struct urb *urb;
624
625         DBGFUNC ("enter sh_scan_iso_urb_list\n");
626         hci->td_array->len = 0;
627
628         while (lh != list_lh) {
629                 urb = list_entry (lh, struct urb, urb_list);
630                 lh = lh->next;
631                 if (((frame_number - urb->start_frame) & 0x7ff) <
632                     urb->number_of_packets) {
633                         if (!sh_add_packet (hci, urb)) {
634                                 return 0;
635                         } else {
636                                 if (((frame_number -
637                                       urb->start_frame) & 0x7ff) > 0x400) {
638                                         if (qu_urbstate (urb) > 0)
639                                                 urb = qu_return_urb (hci, urb, 1);
640                                         else
641                                                 urb = qu_next_urb (hci, urb, 1);
642
643                                         if (lh == list_lh && urb)
644                                                 lh = &urb->urb_list;
645                                 }
646                         }
647                 }
648         }
649         return 1;
650 }
651
652 /***************************************************************************
653  * Function Name : sh_scan_urb_list
654  *
655  * This function goes through the urb list and schedule the 
656  * the transaction.   
657  * 
658  * Input: hci = data structure for the host controller 
659  *        list_lh = pointer to the isochronous list 
660  *
661  * Return: 0 = unsuccessful; 1 = successful  
662  **************************************************************************/
663 static int sh_scan_urb_list (hci_t * hci, struct list_head *list_lh)
664 {
665         struct list_head *lh = NULL;
666         struct urb *urb;
667
668         if (list_lh == NULL) {
669                 DBGERR ("sh_scan_urb_list: error, list_lh == NULL\n");
670         }
671
672         DBGFUNC ("enter sh_scan_urb_list: frame# \n");
673
674         list_for_each (lh, list_lh) {
675                 urb = list_entry (lh, struct urb, urb_list);
676                 if (urb == NULL)
677                         return 1;
678                 if (!usb_pipeint (urb->pipe)
679                     || (((hci->frame_number - urb->start_frame)
680                          & 0x7ff) >= urb->interval)) {
681                         DBGVERBOSE ("sh_scan_urb_list !INT: %d fr_no: %d int: %d pint: %d\n",
682                                     urb->start_frame, hci->frame_number, urb->interval,
683                                     usb_pipeint (urb->pipe));
684                         if (!sh_add_packet (hci, urb)) {
685                                 return 0;
686                         } else {
687                                 DBGVERBOSE ("INT: start: %d fr_no: %d int: %d pint: %d\n",
688                                             urb->start_frame, hci->frame_number,
689                                             urb->interval, usb_pipeint (urb->pipe));
690                                 urb->start_frame = hci->frame_number;
691                                 return 0;
692
693                         }
694                 }
695         }
696         return 1;
697 }
698
699 /***************************************************************************
700  * Function Name : sh_shedule_trans
701  *
702  * This function schedule the USB transaction.
703  * This function will process the endpoint in the following order: 
704  * interrupt, control, and bulk.    
705  * 
706  * Input: hci = data structure for the host controller 
707  *        isSOF = flag indicate if Start Of Frame has occurred 
708  *
709  * Return: 0   
710  **************************************************************************/
711 static int sh_schedule_trans (hci_t * hci, int isSOF)
712 {
713         int units_left = 1;
714         struct list_head *lh;
715
716         if (hci == NULL) {
717                 DBGERR ("sh_schedule_trans: hci == NULL\n");
718                 return 0;
719         }
720         if (hci->td_array == NULL) {
721                 DBGERR ("sh_schedule_trans: hci->td_array == NULL\n");
722                 return 0;
723         }
724
725         if (hci->td_array->len != 0) {
726                 DBGERR ("ERROR: schedule, hci->td_array->len = 0x%x, s/b: 0\n",
727                         hci->td_array->len);
728         }
729
730         /* schedule the next available interrupt transfer or the next
731          * stage of the interrupt transfer */
732
733         if (hci->td_array->len == 0 && !list_empty (&hci->intr_list)) {
734                 units_left = sh_scan_urb_list (hci, &hci->intr_list);
735         }
736
737         /* schedule the next available control transfer or the next
738          * stage of the control transfer */
739
740         if (hci->td_array->len == 0 && !list_empty (&hci->ctrl_list) && units_left > 0) {
741                 units_left = sh_scan_urb_list (hci, &hci->ctrl_list);
742         }
743
744         /* schedule the next available bulk transfer or the next
745          * stage of the bulk transfer */
746
747         if (hci->td_array->len == 0 && !list_empty (&hci->bulk_list) && units_left > 0) {
748                 sh_scan_urb_list (hci, &hci->bulk_list);
749
750                 /* be fair to each BULK URB (move list head around) 
751                  * only when the new SOF happens */
752
753                 lh = hci->bulk_list.next;
754                 list_move (&hci->bulk_list, lh);
755         }
756         return 0;
757 }
758
759 /***************************************************************************
760  * Function Name : sh_add_packet
761  *
762  * This function forms the packet and transmit the packet. This function
763  * will handle all endpoint type: isochoronus, interrupt, control, and 
764  * bulk.
765  * 
766  * Input: hci = data structure for the host controller 
767  *        urb = USB request block data structure 
768  *
769  * Return: 0 = unsucessful; 1 = successful   
770  **************************************************************************/
771 static int sh_add_packet (hci_t * hci, struct urb * urb)
772 {
773         __u8 *data = NULL;
774         int len = 0;
775         int toggle = 0;
776         int maxps = usb_maxpacket (urb->dev, urb->pipe, usb_pipeout (urb->pipe));
777         int endpoint = usb_pipeendpoint (urb->pipe);
778         int address = usb_pipedevice (urb->pipe);
779         int slow = (((urb->pipe) >> 26) & 1);
780         int out = usb_pipeout (urb->pipe);
781         int pid = 0;
782         int ret;
783         int i = 0;
784         int iso = 0;
785
786         DBGFUNC ("enter sh_add_packet\n");
787         if (maxps == 0)
788                 maxps = 8;
789
790         /* calculate len, toggle bit and add the transaction */
791         switch (usb_pipetype (urb->pipe)) {
792         case PIPE_ISOCHRONOUS:
793                 pid = out ? PID_OUT : PID_IN;
794                 iso = 1;
795                 i = hci->frame_number - urb->start_frame;
796                 data = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
797                 len = urb->iso_frame_desc[i].length;
798                 break;
799
800         case PIPE_BULK: /* BULK and BULK0 */
801         case PIPE_INTERRUPT:
802                 pid = out ? PID_OUT : PID_IN;
803                 len = urb->transfer_buffer_length - urb->actual_length;
804                 data = urb->transfer_buffer + urb->actual_length;
805                 toggle = usb_gettoggle (urb->dev, endpoint, out);
806                 break;
807
808         case PIPE_CONTROL:
809                 switch (qu_urbstate (urb)) {
810                 case US_CTRL_SETUP:
811                         len = 8;
812                         pid = PID_SETUP;
813                         data = urb->setup_packet;
814                         toggle = 0;
815                         break;
816
817                 case US_CTRL_DATA:
818                         if (!hci->last_packet_nak) {
819                                 /* The last packet received is not a nak:
820                                  * reset the nak count
821                                  */
822
823                                 hci->nakCnt = 0;
824                         }
825                         if (urb->transfer_buffer_length != 0) {
826                                 pid = out ? PID_OUT : PID_IN;
827                                 len = urb->transfer_buffer_length - urb->actual_length;
828                                 data = urb->transfer_buffer + urb->actual_length;
829                                 toggle = (urb->actual_length & maxps) ? 0 : 1;
830                                 usb_settoggle (urb->dev,
831                                                usb_pipeendpoint (urb->pipe),
832                                                usb_pipeout (urb->pipe), toggle);
833                                 break;
834                         } else {
835                                 /* correct state and fall through */
836                                 qu_seturbstate (urb, US_CTRL_ACK);
837                         }
838
839                 case US_CTRL_ACK:
840                         len = 0;
841
842                         /* reply in opposite direction */
843                         pid = !out ? PID_OUT : PID_IN;
844                         toggle = 1;
845                         usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe),
846                                        usb_pipeout (urb->pipe), toggle);
847                         break;
848                 }
849         }
850
851         ret =
852             hc_add_trans (hci, len, data, toggle, maxps, slow, endpoint,
853                           address, pid, iso, qu_urbstate (urb));
854
855         DBGVERBOSE ("transfer_pa: addr:%d ep:%d pid:%x tog:%x iso:%x sl:%x "
856                     "max:%d\n len:%d ret:%d data:%p left:%d\n",
857                     address, endpoint, pid, toggle, iso, slow,
858                     maxps, len, ret, data, hci->hp.units_left);
859
860         if (ret >= 0) {
861                 hci->td_array->td[hci->td_array->len].urb = urb;
862                 hci->td_array->td[hci->td_array->len].len = ret;
863                 hci->td_array->td[hci->td_array->len].iso_index = i;
864                 hci->td_array->len++;
865                 hci->active_trans = 1;
866                 return 1;
867         }
868         return 0;
869 }
870
871 /***************************************************************************
872  * Function Name : cc_to_error
873  *
874  * This function maps the SL811HS hardware error code to the linux USB error
875  * code.
876  * 
877  * Input: cc = hardware error code 
878  *
879  * Return: USB error code   
880  **************************************************************************/
881 static int cc_to_error (int cc)
882 {
883         int errCode = 0;
884         if (cc & SL11H_STATMASK_ERROR) {
885                 errCode |= -EILSEQ;
886         } else if (cc & SL11H_STATMASK_OVF) {
887                 errCode |= -EOVERFLOW;
888         } else if (cc & SL11H_STATMASK_STALL) {
889                 errCode |= -EPIPE;
890         }
891         return errCode;
892 }
893
894 /***************************************************************************
895  * Function Name : sh_done_list
896  *
897  * This function process the packet when it has done finish transfer.
898  * 
899  * 1) It handles hardware error
900  * 2) It updates the URB state
901  * 3) If the USB transaction is complete, it start the return stack path.
902  * 
903  * Input: hci = data structure for the host controller 
904  *        isExcessNak = flag tells if there excess NAK condition occurred 
905  *
906  * Return:  urb_state or -1 if the transaction has complete   
907  **************************************************************************/
908 static int sh_done_list (hci_t * hci, int *isExcessNak)
909 {
910         int actbytes = 0;
911         int active = 0;
912         void *data = NULL;
913         int cc;
914         int maxps;
915         int toggle;
916         struct urb *urb;
917         int urb_state = 0;
918         int ret = 1;            /* -1 parse abbort, 1 parse ok, 0 last element */
919         int trans = 0;
920         int len;
921         int iso_index = 0;
922         int out;
923         int pid = 0;
924         int debugLen = 0;
925
926         *isExcessNak = 0;
927
928         DBGFUNC ("enter sh_done_list: td_array->len = 0x%x\n",
929                  hci->td_array->len);
930
931         debugLen = hci->td_array->len;
932         if (debugLen > 1)
933                 DBGERR ("sh_done_list: td_array->len = 0x%x > 1\n",
934                         hci->td_array->len);
935
936         for (trans = 0; ret && trans < hci->td_array->len && trans < MAX_TRANS;
937              trans++) {
938                 urb = hci->td_array->td[trans].urb;
939                 len = hci->td_array->td[trans].len;
940                 out = usb_pipeout (urb->pipe);
941
942                 if (usb_pipeisoc (urb->pipe)) {
943                         iso_index = hci->td_array->td[trans].iso_index;
944                         data = urb->transfer_buffer + urb->iso_frame_desc[iso_index].offset;
945                         toggle = 0;
946                 } else {
947                         data = urb->transfer_buffer + urb->actual_length;
948                         toggle = usb_gettoggle (urb->dev,
949                                                 usb_pipeendpoint (urb->pipe),
950                                                 usb_pipeout (urb->pipe));
951
952                 }
953                 urb_state = qu_urbstate (urb);
954                 pid = out ? PID_OUT : PID_IN;
955                 ret = hc_parse_trans (hci, &actbytes, data, &cc, &toggle, len,
956                                       pid, urb_state);
957                 maxps = usb_maxpacket (urb->dev, urb->pipe, usb_pipeout (urb->pipe));
958
959                 if (maxps == 0)
960                         maxps = 8;
961
962                 active = (urb_state != US_CTRL_SETUP) && (actbytes && !(actbytes & (maxps - 1)));
963
964                 /* If the transfer is not bulk in, then it is necessary to get all
965                  * data specify by the urb->transfer_len.
966                  */
967
968                 if (!(usb_pipebulk (urb->pipe) && usb_pipein (urb->pipe)))
969                         active = active && (urb->transfer_buffer_length != urb->actual_length + actbytes);
970
971                 if (urb->transfer_buffer_length == urb->actual_length + actbytes)
972                         active = 0;
973
974                 if ((cc &
975                      (SL11H_STATMASK_ERROR | SL11H_STATMASK_TMOUT |
976                       SL11H_STATMASK_OVF | SL11H_STATMASK_STALL))
977                     && !(cc & SL11H_STATMASK_NAK)) {
978                         if (++urb->error_count > 3) {
979                                 DBGERR ("done_list: excessive error: errcount = 0x%x, cc = 0x%x\n",
980                                         urb->error_count, cc);
981                                 urb_state = 0;
982                                 active = 0;
983                         } else {
984                                 DBGERR ("done_list: packet err, cc = 0x%x, "
985                                         " urb->length = 0x%x, actual_len = 0x%x,"
986                                         " urb_state =0x%x\n",
987                                         cc, urb->transfer_buffer_length,
988                                         urb->actual_length, urb_state);
989 //                      if (cc & SL11H_STATMASK_STALL) {
990                                 /* The USB function is STALLED on a control pipe (0), 
991                                  * then it needs to send the SETUP command again to 
992                                  * clear the STALL condition
993                                  */
994
995 //                              if (usb_pipeendpoint (urb->pipe) == 0) {
996 //                                      urb_state = 2;  
997 //                                      active = 0;
998 //                              }
999 //                      } else   
1000                                 active = 1;
1001                         }
1002                 } else {
1003                         if (cc & SL11H_STATMASK_NAK) {
1004                                 if (hci->nakCnt < 0x10000) {
1005                                         hci->nakCnt++;
1006                                         hci->last_packet_nak = 1;
1007                                         active = 1;
1008                                         *isExcessNak = 0;
1009                                 } else {
1010                                         DBGERR ("done_list: nak count exceed limit\n");
1011                                         active = 0;
1012                                         *isExcessNak = 1;
1013                                         hci->nakCnt = 0;
1014                                 }
1015                         } else {
1016                                 hci->nakCnt = 0;
1017                                 hci->last_packet_nak = 0;
1018                         }
1019
1020                         if (urb_state != US_CTRL_SETUP) {
1021                                 /* no error */
1022                                 urb->actual_length += actbytes;
1023                                 usb_settoggle (urb->dev,
1024                                                usb_pipeendpoint (urb->pipe),
1025                                                usb_pipeout (urb->pipe), toggle);
1026                         }
1027                         if (usb_pipeisoc (urb->pipe)) {
1028                                 urb->iso_frame_desc[iso_index].actual_length = actbytes;
1029                                 urb->iso_frame_desc[iso_index].status = cc_to_error (cc);
1030                                 active = (iso_index < urb->number_of_packets);
1031                         }
1032                 }
1033                 if (!active) {
1034                         if (!urb_state) {
1035                                 urb->status = cc_to_error (cc);
1036                                 if (urb->status) {
1037                                         DBGERR ("error on received packet: urb->status = 0x%x\n",
1038                                                 urb->status);
1039                                 }
1040                                 hci->td_array->len = 0;
1041                                 qu_return_urb (hci, urb, 1);
1042                                 return -1;
1043                         } else {
1044                                 /* We do not want to decrement the urb_state if exceeded nak,
1045                                  * because we need to finish the data stage of the control 
1046                                  * packet 
1047                                  */
1048
1049                                 if (!(*isExcessNak))
1050                                         urb_state--;
1051                                 qu_seturbstate (urb, urb_state);
1052                         }
1053                 }
1054         }
1055
1056         if (urb_state < 0)
1057                 DBGERR ("ERROR: done_list, urb_state = %d, suppose > 0\n",
1058                         urb_state);
1059         if (debugLen != hci->td_array->len) {
1060                 DBGERR ("ERROR: done_list, debugLen!= td_array->len,"
1061                         "debugLen = 0x%x, hci->td_array->len = 0x%x\n",
1062                         debugLen, hci->td_array->len);
1063         }
1064
1065         hci->td_array->len = 0;
1066
1067         return urb_state;
1068 }