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