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 **************************************************************************/
hcs_urb_queue(hci_t * hci,struct urb * urb)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 **************************************************************************/
hcs_return_urb(hci_t * hci,struct urb * urb,int resub_ok)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 = %p, "
97 "transferbuffer point = %p, "
98 " setup packet pointer = %p, context pointer = %p \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); /* call complete */
110 }
111
112 if (resubmit) {
113 /* requeue the URB */
114 urb->dev = dev;
115 hcs_urb_queue (hci, urb);
116 }
117
118 return 0;
119 }
120
121 /***************************************************************************
122 * Function Name : hci_submit_urb
123 *
124 * This function is called by the USB core API when an URB is available to
125 * process. This function does the following
126 *
127 * 1) Check the validity of the URB
128 * 2) Parse the device number from the URB
129 * 3) Pass the URB to the root hub routine if its intended for the hub, else
130 * queue the urb for the attached device.
131 *
132 * Input: urb = USB request block data structure
133 *
134 * Return: 0 if success or error code
135 **************************************************************************/
hci_submit_urb(struct urb * urb)136 static int hci_submit_urb (struct urb * urb)
137 {
138 hci_t *hci;
139 unsigned int pipe = urb->pipe;
140 unsigned long flags;
141 int ret;
142
143 DBGFUNC ("enter hci_submit_urb, pipe = 0x%x\n", urb->pipe);
144 if (!urb->dev || !urb->dev->bus || urb->hcpriv)
145 return -EINVAL;
146
147 if (usb_endpoint_halted
148 (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe))) {
149 printk ("hci_submit_urb: endpoint_halted\n");
150 return -EPIPE;
151 }
152 hci = (hci_t *) urb->dev->bus->hcpriv;
153
154 /* a request to the virtual root hub */
155
156 if (usb_pipedevice (pipe) == hci->rh.devnum) {
157 if (urb_debug > 1)
158 urb_print (urb, "SUB-RH", usb_pipein (pipe));
159
160 return rh_submit_urb (urb);
161 }
162
163 /* queue the URB to its endpoint-queue */
164
165 spin_lock_irqsave (&usb_urb_lock, flags);
166 ret = hcs_urb_queue (hci, urb);
167 if (ret != 0) {
168 /* error on return */
169 DBGERR
170 ("hci_submit_urb: return err, ret = 0x%x, urb->status = 0x%x\n",
171 ret, urb->status);
172 }
173
174 spin_unlock_irqrestore (&usb_urb_lock, flags);
175
176 return ret;
177
178 }
179
180 /***************************************************************************
181 * Function Name : hci_unlink_urb
182 *
183 * This function mark the URB to unlink
184 *
185 * Input: urb = USB request block data structure
186 *
187 * Return: 0 if success or error code
188 **************************************************************************/
hci_unlink_urb(struct urb * urb)189 static int hci_unlink_urb (struct urb * urb)
190 {
191 unsigned long flags;
192 hci_t *hci;
193 DECLARE_WAITQUEUE (wait, current);
194 void *comp = NULL;
195
196 DBGFUNC ("enter hci_unlink_urb\n");
197
198 if (!urb) /* just to be sure */
199 return -EINVAL;
200
201 if (!urb->dev || !urb->dev->bus)
202 return -ENODEV;
203
204 hci = (hci_t *) urb->dev->bus->hcpriv;
205
206 /* a request to the virtual root hub */
207 if (usb_pipedevice (urb->pipe) == hci->rh.devnum) {
208 return rh_unlink_urb (urb);
209 }
210
211 if (urb_debug)
212 urb_print (urb, "UNLINK", 1);
213
214 spin_lock_irqsave (&usb_urb_lock, flags);
215
216 if (!list_empty (&urb->urb_list) && urb->status == -EINPROGRESS) {
217 /* URB active? */
218
219 if (urb->
220 transfer_flags & (USB_ASYNC_UNLINK | USB_TIMEOUT_KILLED)) {
221 /* asynchron with callback */
222
223 list_del (&urb->urb_list); /* relink the urb to the del list */
224 list_add (&urb->urb_list, &hci->del_list);
225 spin_unlock_irqrestore (&usb_urb_lock, flags);
226
227 } else {
228 /* synchron 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 /* --> crash --> */ list_del (&urb->urb_list); /* relink the urb to the del list */
237 list_add (&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 & USB_ASYNC_UNLINK)) {
255 urb->status = -ENOENT;
256 urb->actual_length = 0;
257 urb->complete (urb);
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 **************************************************************************/
hci_alloc_dev(struct usb_device * usb_dev)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 **************************************************************************/
hci_free_dev(struct usb_device * usb_dev)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 **************************************************************************/
hci_get_current_frame_number(struct usb_device * usb_dev)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
qu_urb_timeout(unsigned long lurb)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 urb->transfer_flags |= USB_TIMEOUT_KILLED;
392 hci_unlink_urb (urb);
393 }
394 #endif
395
396 /***************************************************************************
397 * Function Name : qu_pipeindex
398 *
399 * This function gets the index of the pipe.
400 *
401 * Input: pipe: the urb pipe
402 *
403 * Return: index
404 **************************************************************************/
qu_pipeindex(__u32 pipe)405 static inline int qu_pipeindex (__u32 pipe)
406 {
407 DBGFUNC ("enter qu_pipeindex\n");
408 return (usb_pipeendpoint (pipe) << 1) | (usb_pipecontrol (pipe) ? 0 : usb_pipeout (pipe));
409 }
410
411 /***************************************************************************
412 * Function Name : qu_seturbstate
413 *
414 * This function set the state of the URB.
415 *
416 * control pipe: 3 states -- Setup, data, status
417 * interrupt and bulk pipe: 1 state -- data
418 *
419 * Input: urb = USB request block data structure
420 * state = the urb state
421 *
422 * Return: none
423 **************************************************************************/
qu_seturbstate(struct urb * urb,int state)424 static inline void qu_seturbstate (struct urb * urb, int state)
425 {
426 DBGFUNC ("enter qu_seturbstate\n");
427 urb->pipe &= ~0x1f;
428 urb->pipe |= state & 0x1f;
429 }
430
431 /***************************************************************************
432 * Function Name : qu_urbstate
433 *
434 * This function get the current state of the URB.
435 *
436 * Input: urb = USB request block data structure
437 *
438 * Return: none
439 **************************************************************************/
qu_urbstate(struct urb * urb)440 static inline int qu_urbstate (struct urb * urb)
441 {
442
443 DBGFUNC ("enter qu_urbstate\n");
444
445 return urb->pipe & 0x1f;
446 }
447
448 /***************************************************************************
449 * Function Name : qu_queue_active_urb
450 *
451 * This function adds the urb to the appropriate active urb list and set
452 * the urb state.
453 *
454 * There are four active lists: isochoronous list, interrupt list,
455 * control list, and bulk list.
456 *
457 * Input: hci = data structure for the host controller
458 * urb = USB request block data structure
459 * ed = endpoint descriptor
460 *
461 * Return: none
462 **************************************************************************/
qu_queue_active_urb(hci_t * hci,struct urb * urb,epd_t * ed)463 static inline void qu_queue_active_urb (hci_t * hci, struct urb * urb, epd_t * ed)
464 {
465 int urb_state = 0;
466 DBGFUNC ("enter qu_queue_active_urb\n");
467 switch (usb_pipetype (urb->pipe)) {
468 case PIPE_CONTROL:
469 list_add (&urb->urb_list, &hci->ctrl_list);
470 urb_state = US_CTRL_SETUP;
471 break;
472
473 case PIPE_BULK:
474 list_add (&urb->urb_list, &hci->bulk_list);
475 if ((urb->transfer_flags & USB_ZERO_PACKET)
476 && urb->transfer_buffer_length > 0
477 &&
478 ((urb->transfer_buffer_length %
479 usb_maxpacket (urb->dev, urb->pipe,
480 usb_pipeout (urb->pipe))) == 0)) {
481 urb_state = US_BULK0;
482 }
483 break;
484
485 case PIPE_INTERRUPT:
486 urb->start_frame = hci->frame_number;
487 list_add (&urb->urb_list, &hci->intr_list);
488 break;
489
490 case PIPE_ISOCHRONOUS:
491 list_add (&urb->urb_list, &hci->iso_list);
492 break;
493 }
494
495 #ifdef HC_URB_TIMEOUT
496 if (urb->timeout) {
497 ed->timeout.data = (unsigned long) urb;
498 ed->timeout.expires = urb->timeout + jiffies;
499 ed->timeout.function = qu_urb_timeout;
500 add_timer (&ed->timeout);
501 }
502 #endif
503
504 qu_seturbstate (urb, urb_state);
505 }
506
507 /***************************************************************************
508 * Function Name : qu_queue_urb
509 *
510 * This function adds the urb to the endpoint descriptor list
511 *
512 * Input: hci = data structure for the host controller
513 * urb = USB request block data structure
514 *
515 * Return: none
516 **************************************************************************/
qu_queue_urb(hci_t * hci,struct urb * urb)517 static int qu_queue_urb (hci_t * hci, struct urb * urb)
518 {
519 struct hci_device *hci_dev = usb_to_hci (urb->dev);
520 epd_t *ed = &hci_dev->ed[qu_pipeindex (urb->pipe)];
521
522 DBGFUNC ("Enter qu_queue_urb\n");
523
524 /* for ISOC transfers calculate start frame index */
525
526 if (usb_pipeisoc (urb->pipe) && urb->transfer_flags & USB_ISO_ASAP) {
527 urb->start_frame = ((ed->pipe_head) ? (ed->last_iso + 1) : hci_get_current_frame_number (urb-> dev) + 1) & 0xffff;
528 }
529
530 if (ed->pipe_head) {
531 __list_add (&urb->urb_list, ed->urb_queue.prev,
532 &(ed->urb_queue));
533 } else {
534 ed->pipe_head = urb;
535 qu_queue_active_urb (hci, urb, ed);
536 if (++hci->active_urbs == 1)
537 hc_start_int (hci);
538 }
539
540 return 0;
541 }
542
543 /***************************************************************************
544 * Function Name : qu_next_urb
545 *
546 * This function removes the URB from the queue and add the next URB to
547 * active list.
548 *
549 * Input: hci = data structure for the host controller
550 * urb = USB request block data structure
551 * resub_ok = resubmit flag
552 *
553 * Return: pointer to the next urb
554 **************************************************************************/
qu_next_urb(hci_t * hci,struct urb * urb,int resub_ok)555 static struct urb *qu_next_urb (hci_t * hci, struct urb * urb, int resub_ok)
556 {
557 struct hci_device *hci_dev = usb_to_hci (urb->dev);
558 epd_t *ed = &hci_dev->ed[qu_pipeindex (urb->pipe)];
559
560 DBGFUNC ("enter qu_next_urb\n");
561 list_del (&urb->urb_list);
562 INIT_LIST_HEAD (&urb->urb_list);
563 if (ed->pipe_head == urb) {
564
565 #ifdef HC_URB_TIMEOUT
566 if (urb->timeout)
567 del_timer (&ed->timeout);
568 #endif
569
570 if (!--hci->active_urbs)
571 hc_stop_int (hci);
572
573 if (!list_empty (&ed->urb_queue)) {
574 urb = list_entry (ed->urb_queue.next, struct urb, urb_list);
575 list_del (&urb->urb_list);
576 INIT_LIST_HEAD (&urb->urb_list);
577 ed->pipe_head = urb;
578 qu_queue_active_urb (hci, urb, ed);
579 } else {
580 ed->pipe_head = NULL;
581 urb = NULL;
582 }
583 }
584 return urb;
585 }
586
587 /***************************************************************************
588 * Function Name : qu_return_urb
589 *
590 * This function is part of the return path.
591 *
592 * Input: hci = data structure for the host controller
593 * urb = USB request block data structure
594 * resub_ok = resubmit flag
595 *
596 * Return: pointer to the next urb
597 **************************************************************************/
qu_return_urb(hci_t * hci,struct urb * urb,int resub_ok)598 static struct urb *qu_return_urb (hci_t * hci, struct urb * urb, int resub_ok)
599 {
600 struct urb *next_urb;
601
602 DBGFUNC ("enter qu_return_rub\n");
603 next_urb = qu_next_urb (hci, urb, resub_ok);
604 hcs_return_urb (hci, urb, resub_ok);
605 return next_urb;
606 }
607
608 #if 0 /* unused now (hne) */
609 /***************************************************************************
610 * Function Name : sh_scan_iso_urb_list
611 *
612 * This function goes throught the isochronous urb list and schedule the
613 * the transfer.
614 *
615 * Note: This function has not tested yet
616 *
617 * Input: hci = data structure for the host controller
618 * list_lh = pointer to the isochronous list
619 * frame_number = the frame number
620 *
621 * Return: 0 = unsuccessful; 1 = successful
622 **************************************************************************/
623 static int sh_scan_iso_urb_list (hci_t * hci, struct list_head *list_lh,
624 int frame_number)
625 {
626 struct list_head *lh = list_lh->next;
627 struct urb *urb;
628
629 DBGFUNC ("enter sh_scan_iso_urb_list\n");
630 hci->td_array->len = 0;
631
632 while (lh != list_lh) {
633 urb = list_entry (lh, struct urb, urb_list);
634 lh = lh->next;
635 if (((frame_number - urb->start_frame) & 0x7ff) <
636 urb->number_of_packets) {
637 if (!sh_add_packet (hci, urb)) {
638 return 0;
639 } else {
640 if (((frame_number -
641 urb->start_frame) & 0x7ff) > 0x400) {
642 if (qu_urbstate (urb) > 0)
643 urb = qu_return_urb (hci, urb, 1);
644 else
645 urb = qu_next_urb (hci, urb, 1);
646
647 if (lh == list_lh && urb)
648 lh = &urb->urb_list;
649 }
650 }
651 }
652 }
653 return 1;
654 }
655 #endif // if0
656
657 /***************************************************************************
658 * Function Name : sh_scan_urb_list
659 *
660 * This function goes through the urb list and schedule the
661 * the transaction.
662 *
663 * Input: hci = data structure for the host controller
664 * list_lh = pointer to the isochronous list
665 *
666 * Return: 0 = unsuccessful; 1 = successful
667 **************************************************************************/
sh_scan_urb_list(hci_t * hci,struct list_head * list_lh)668 static int sh_scan_urb_list (hci_t * hci, struct list_head *list_lh)
669 {
670 struct list_head *lh = NULL;
671 struct urb *urb;
672
673 if (list_lh == NULL) {
674 DBGERR ("sh_scan_urb_list: error, list_lh == NULL\n");
675 }
676
677 DBGFUNC ("enter sh_scan_urb_list: frame# \n");
678
679 list_for_each (lh, list_lh) {
680 urb = list_entry (lh, struct urb, urb_list);
681 if (urb == NULL)
682 return 1;
683 if (!usb_pipeint (urb->pipe)
684 || (((hci->frame_number - urb->start_frame)
685 & 0x7ff) >= urb->interval)) {
686 DBGVERBOSE ("sh_scan_urb_list !INT: %d fr_no: %d int: %d pint: %d\n",
687 urb->start_frame, hci->frame_number, urb->interval,
688 usb_pipeint (urb->pipe));
689 if (!sh_add_packet (hci, urb)) {
690 return 0;
691 } else {
692 DBGVERBOSE ("INT: start: %d fr_no: %d int: %d pint: %d\n",
693 urb->start_frame, hci->frame_number,
694 urb->interval, usb_pipeint (urb->pipe));
695 urb->start_frame = hci->frame_number;
696 return 0;
697
698 }
699 }
700 }
701 return 1;
702 }
703
704 /***************************************************************************
705 * Function Name : sh_shedule_trans
706 *
707 * This function schedule the USB transaction.
708 * This function will process the endpoint in the following order:
709 * interrupt, control, and bulk.
710 *
711 * Input: hci = data structure for the host controller
712 * isSOF = flag indicate if Start Of Frame has occurred
713 *
714 * Return: 0
715 **************************************************************************/
sh_schedule_trans(hci_t * hci,int isSOF)716 static int sh_schedule_trans (hci_t * hci, int isSOF)
717 {
718 int units_left = 1;
719 struct list_head *lh;
720
721 if (hci == NULL) {
722 DBGERR ("sh_schedule_trans: hci == NULL\n");
723 return 0;
724 }
725 if (hci->td_array == NULL) {
726 DBGERR ("sh_schedule_trans: hci->td_array == NULL\n");
727 return 0;
728 }
729
730 if (hci->td_array->len != 0) {
731 DBGERR ("ERROR: schedule, hci->td_array->len = 0x%x, s/b: 0\n",
732 hci->td_array->len);
733 }
734
735 /* schedule the next available interrupt transfer or the next
736 * stage of the interrupt transfer */
737
738 if (hci->td_array->len == 0 && !list_empty (&hci->intr_list)) {
739 units_left = sh_scan_urb_list (hci, &hci->intr_list);
740 }
741
742 /* schedule the next available control transfer or the next
743 * stage of the control transfer */
744
745 if (hci->td_array->len == 0 && !list_empty (&hci->ctrl_list) && units_left > 0) {
746 units_left = sh_scan_urb_list (hci, &hci->ctrl_list);
747 }
748
749 /* schedule the next available bulk transfer or the next
750 * stage of the bulk transfer */
751
752 if (hci->td_array->len == 0 && !list_empty (&hci->bulk_list) && units_left > 0) {
753 sh_scan_urb_list (hci, &hci->bulk_list);
754
755 /* be fair to each BULK URB (move list head around)
756 * only when the new SOF happens */
757
758 lh = hci->bulk_list.next;
759 list_del (&hci->bulk_list);
760 list_add (&hci->bulk_list, lh);
761 }
762 return 0;
763 }
764
765 /***************************************************************************
766 * Function Name : sh_add_packet
767 *
768 * This function forms the packet and transmit the packet. This function
769 * will handle all endpoint type: isochoronus, interrupt, control, and
770 * bulk.
771 *
772 * Input: hci = data structure for the host controller
773 * urb = USB request block data structure
774 *
775 * Return: 0 = unsucessful; 1 = successful
776 **************************************************************************/
sh_add_packet(hci_t * hci,struct urb * urb)777 static int sh_add_packet (hci_t * hci, struct urb * urb)
778 {
779 __u8 *data = NULL;
780 int len = 0;
781 int toggle = 0;
782 int maxps = usb_maxpacket (urb->dev, urb->pipe, usb_pipeout (urb->pipe));
783 int endpoint = usb_pipeendpoint (urb->pipe);
784 int address = usb_pipedevice (urb->pipe);
785 int slow = (((urb->pipe) >> 26) & 1);
786 int out = usb_pipeout (urb->pipe);
787 int pid = 0;
788 int ret;
789 int i = 0;
790 int iso = 0;
791
792 DBGFUNC ("enter sh_add_packet\n");
793 if (maxps == 0)
794 maxps = 8;
795
796 /* calculate len, toggle bit and add the transaction */
797 switch (usb_pipetype (urb->pipe)) {
798 case PIPE_ISOCHRONOUS:
799 pid = out ? PID_OUT : PID_IN;
800 iso = 1;
801 i = hci->frame_number - urb->start_frame;
802 data = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
803 len = urb->iso_frame_desc[i].length;
804 break;
805
806 case PIPE_BULK: /* BULK and BULK0 */
807 case PIPE_INTERRUPT:
808 pid = out ? PID_OUT : PID_IN;
809 len = urb->transfer_buffer_length - urb->actual_length;
810 data = urb->transfer_buffer + urb->actual_length;
811 toggle = usb_gettoggle (urb->dev, endpoint, out);
812 break;
813
814 case PIPE_CONTROL:
815 switch (qu_urbstate (urb)) {
816 case US_CTRL_SETUP:
817 len = 8;
818 pid = PID_SETUP;
819 data = urb->setup_packet;
820 toggle = 0;
821 break;
822
823 case US_CTRL_DATA:
824 if (!hci->last_packet_nak) {
825 /* The last packet received is not a nak:
826 * reset the nak count
827 */
828
829 hci->nakCnt = 0;
830 }
831 if (urb->transfer_buffer_length != 0) {
832 pid = out ? PID_OUT : PID_IN;
833 len = urb->transfer_buffer_length - urb->actual_length;
834 data = urb->transfer_buffer + urb->actual_length;
835 toggle = (urb->actual_length & maxps) ? 0 : 1;
836 usb_settoggle (urb->dev,
837 usb_pipeendpoint (urb->pipe),
838 usb_pipeout (urb->pipe), toggle);
839 break;
840 } else {
841 /* correct state and fall through */
842 qu_seturbstate (urb, US_CTRL_ACK);
843 }
844
845 case US_CTRL_ACK:
846 len = 0;
847
848 /* reply in opposite direction */
849 pid = !out ? PID_OUT : PID_IN;
850 toggle = 1;
851 usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe),
852 usb_pipeout (urb->pipe), toggle);
853 break;
854 }
855 }
856
857 ret =
858 hc_add_trans (hci, len, data, toggle, maxps, slow, endpoint,
859 address, pid, iso, qu_urbstate (urb));
860
861 DBGVERBOSE ("transfer_pa: addr:%d ep:%d pid:%x tog:%x iso:%x sl:%x "
862 "max:%d\n len:%d ret:%d data:%p left:%d\n",
863 address, endpoint, pid, toggle, iso, slow,
864 maxps, len, ret, data, hci->hp.units_left);
865
866 if (ret >= 0) {
867 hci->td_array->td[hci->td_array->len].urb = urb;
868 hci->td_array->td[hci->td_array->len].len = ret;
869 hci->td_array->td[hci->td_array->len].iso_index = i;
870 hci->td_array->len++;
871 hci->active_trans = 1;
872 return 1;
873 }
874 return 0;
875 }
876
877 /***************************************************************************
878 * Function Name : cc_to_error
879 *
880 * This function maps the SL811HS hardware error code to the linux USB error
881 * code.
882 *
883 * Input: cc = hardware error code
884 *
885 * Return: USB error code
886 **************************************************************************/
cc_to_error(int cc)887 static int cc_to_error (int cc)
888 {
889 int errCode = 0;
890 if (cc & SL11H_STATMASK_ERROR) {
891 errCode |= -EILSEQ;
892 } else if (cc & SL11H_STATMASK_OVF) {
893 errCode |= -EOVERFLOW;
894 } else if (cc & SL11H_STATMASK_STALL) {
895 errCode |= -EPIPE;
896 }
897 return errCode;
898 }
899
900 /***************************************************************************
901 * Function Name : sh_done_list
902 *
903 * This function process the packet when it has done finish transfer.
904 *
905 * 1) It handles hardware error
906 * 2) It updates the URB state
907 * 3) If the USB transaction is complete, it start the return stack path.
908 *
909 * Input: hci = data structure for the host controller
910 * isExcessNak = flag tells if there excess NAK condition occurred
911 *
912 * Return: urb_state or -1 if the transaction has complete
913 **************************************************************************/
sh_done_list(hci_t * hci,int * isExcessNak)914 static int sh_done_list (hci_t * hci, int *isExcessNak)
915 {
916 int actbytes = 0;
917 int active = 0;
918 void *data = NULL;
919 int cc;
920 int maxps;
921 int toggle;
922 struct urb *urb;
923 int urb_state = 0;
924 int ret = 1; /* -1 parse abbort, 1 parse ok, 0 last element */
925 int trans = 0;
926 int len;
927 int iso_index = 0;
928 int out;
929 int pid = 0;
930 int debugLen = 0;
931
932 *isExcessNak = 0;
933
934 DBGFUNC ("enter sh_done_list: td_array->len = 0x%x\n",
935 hci->td_array->len);
936
937 debugLen = hci->td_array->len;
938 if (debugLen > 1)
939 DBGERR ("sh_done_list: td_array->len = 0x%x > 1\n",
940 hci->td_array->len);
941
942 for (trans = 0; ret && trans < hci->td_array->len && trans < MAX_TRANS;
943 trans++) {
944 urb = hci->td_array->td[trans].urb;
945 /* FIXME: */
946 /* +++ I'm sorry, can't handle NULL-Pointers 21.11.2002 (hne) */
947 if (!urb) {
948 DBGERR ("sh_done_list: urb = NULL\n");
949 continue;
950 }
951 if (!urb->dev || !urb->pipe) {
952 if (!urb->dev) DBGERR ("sh_done_list: urb->dev = NULL\n");
953 if (!urb->pipe) DBGERR ("sh_done_list: urb->pipe = NULL\n");
954 continue;
955 }
956 /* --- 21.11.2002 (hne) */
957
958 len = hci->td_array->td[trans].len;
959 out = usb_pipeout (urb->pipe);
960
961 if (usb_pipeisoc (urb->pipe)) {
962 iso_index = hci->td_array->td[trans].iso_index;
963 data = urb->transfer_buffer + urb->iso_frame_desc[iso_index].offset;
964 toggle = 0;
965 } else {
966 data = urb->transfer_buffer + urb->actual_length;
967 /* +++ Crash (hne) urb->dev == NULL !!! */
968 toggle = usb_gettoggle (urb->dev,
969 usb_pipeendpoint (urb->pipe),
970 usb_pipeout (urb->pipe));
971 /* --- Crash (hne) urb->dev == NULL !!! */
972
973 }
974 urb_state = qu_urbstate (urb);
975 pid = out ? PID_OUT : PID_IN;
976 ret = hc_parse_trans (hci, &actbytes, data, &cc, &toggle, len,
977 pid, urb_state);
978 maxps = usb_maxpacket (urb->dev, urb->pipe, usb_pipeout (urb->pipe));
979
980 if (maxps == 0)
981 maxps = 8;
982
983 active = (urb_state != US_CTRL_SETUP) && (actbytes && !(actbytes & (maxps - 1)));
984
985 /* If the transfer is not bulk in, then it is necessary to get all
986 * data specify by the urb->transfer_len.
987 */
988
989 if (!(usb_pipebulk (urb->pipe) && usb_pipein (urb->pipe)))
990 active = active && (urb->transfer_buffer_length != urb->actual_length + actbytes);
991
992 if (urb->transfer_buffer_length == urb->actual_length + actbytes)
993 active = 0;
994
995 if ((cc &
996 (SL11H_STATMASK_ERROR | SL11H_STATMASK_TMOUT |
997 SL11H_STATMASK_OVF | SL11H_STATMASK_STALL))
998 && !(cc & SL11H_STATMASK_NAK)) {
999 if (++urb->error_count > 3) {
1000 DBGERR ("done_list: excessive error: errcount = 0x%x, cc = 0x%x\n",
1001 urb->error_count, cc);
1002 urb_state = 0;
1003 active = 0;
1004 } else {
1005 DBGERR ("done_list: packet err, cc=0x%x, "
1006 " urb->length=0x%x, actual_len=0x%x,"
1007 " urb_state=0x%x\n",
1008 cc, urb->transfer_buffer_length,
1009 urb->actual_length, urb_state);
1010 // if (cc & SL11H_STATMASK_STALL) {
1011 /* The USB function is STALLED on a control pipe (0),
1012 * then it needs to send the SETUP command again to
1013 * clear the STALL condition
1014 */
1015
1016 // if (usb_pipeendpoint (urb->pipe) == 0) {
1017 // urb_state = 2;
1018 // active = 0;
1019 // }
1020 // } else
1021 active = 1;
1022 }
1023 } else {
1024 if (cc & SL11H_STATMASK_NAK) {
1025 if (hci->nakCnt < 0x10000) {
1026 hci->nakCnt++;
1027 hci->last_packet_nak = 1;
1028 active = 1;
1029 *isExcessNak = 0;
1030 } else {
1031 DBGERR ("done_list: nak count exceed limit\n");
1032 active = 0;
1033 *isExcessNak = 1;
1034 hci->nakCnt = 0;
1035 }
1036 } else {
1037 hci->nakCnt = 0;
1038 hci->last_packet_nak = 0;
1039 }
1040
1041 if (urb_state != US_CTRL_SETUP) {
1042 /* no error */
1043 urb->actual_length += actbytes;
1044 usb_settoggle (urb->dev,
1045 usb_pipeendpoint (urb->pipe),
1046 usb_pipeout (urb->pipe), toggle);
1047 }
1048 if (usb_pipeisoc (urb->pipe)) {
1049 urb->iso_frame_desc[iso_index].actual_length = actbytes;
1050 urb->iso_frame_desc[iso_index].status = cc_to_error (cc);
1051 active = (iso_index < urb->number_of_packets);
1052 }
1053 }
1054 if (!active) {
1055 if (!urb_state) {
1056 urb->status = cc_to_error (cc);
1057 if (urb->status) {
1058 DBGERR ("error on received packet: urb->status = 0x%x\n",
1059 urb->status);
1060 }
1061 hci->td_array->len = 0;
1062 qu_return_urb (hci, urb, 1);
1063 return -1;
1064 } else {
1065 /* We do not want to decrement the urb_state if exceeded nak,
1066 * because we need to finish the data stage of the control
1067 * packet
1068 */
1069
1070 if (!(*isExcessNak))
1071 urb_state--;
1072 qu_seturbstate (urb, urb_state);
1073 }
1074 }
1075 }
1076
1077 if (urb_state < 0)
1078 DBGERR ("ERROR: done_list, urb_state = %d, suppose > 0\n",
1079 urb_state);
1080 if (debugLen != hci->td_array->len) {
1081 DBGERR ("ERROR: done_list, debugLen!= td_array->len,"
1082 "debugLen = 0x%x, hci->td_array->len = 0x%x\n",
1083 debugLen, hci->td_array->len);
1084 }
1085
1086 hci->td_array->len = 0;
1087
1088 return urb_state;
1089 }
1090