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