Merge branch 'for-linus' of git://git.linaro.org/people/rmk/linux-arm
[firefly-linux-kernel-4.4.55.git] / drivers / staging / vt6656 / usbpipe.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  *
20  * File: usbpipe.c
21  *
22  * Purpose: Handle USB control endpoint
23  *
24  * Author: Warren Hsu
25  *
26  * Date: Mar. 29, 2005
27  *
28  * Functions:
29  *      CONTROLnsRequestOut - Write variable length bytes to MEM/BB/MAC/EEPROM
30  *      CONTROLnsRequestIn - Read variable length bytes from MEM/BB/MAC/EEPROM
31  *      ControlvWriteByte - Write one byte to MEM/BB/MAC/EEPROM
32  *      ControlvReadByte - Read one byte from MEM/BB/MAC/EEPROM
33  *      ControlvMaskByte - Read one byte from MEM/BB/MAC/EEPROM and clear/set some bits in the same address
34  *
35  * Revision History:
36  *      04-05-2004 Jerry Chen:  Initial release
37  *      11-24-2004 Warren Hsu: Add ControlvWriteByte,ControlvReadByte,ControlvMaskByte
38  *
39  */
40
41 #include "int.h"
42 #include "rxtx.h"
43 #include "dpc.h"
44 #include "control.h"
45 #include "desc.h"
46 #include "device.h"
47
48 /*---------------------  Static Definitions -------------------------*/
49 //endpoint def
50 //endpoint 0: control
51 //endpoint 1: interrupt
52 //endpoint 2: read bulk
53 //endpoint 3: write bulk
54
55 //static int          msglevel                =MSG_LEVEL_DEBUG;
56 static int          msglevel                =MSG_LEVEL_INFO;
57
58
59 #define USB_CTL_WAIT   500 //ms
60
61 #ifndef URB_ASYNC_UNLINK
62 #define URB_ASYNC_UNLINK    0
63 #endif
64
65 /*---------------------  Static Classes  ----------------------------*/
66
67 /*---------------------  Static Variables  --------------------------*/
68
69 /*---------------------  Static Functions  --------------------------*/
70 static
71 void
72 s_nsInterruptUsbIoCompleteRead(
73      struct urb *urb
74     );
75
76
77 static
78 void
79 s_nsBulkInUsbIoCompleteRead(
80      struct urb *urb
81     );
82
83
84 static
85 void
86 s_nsBulkOutIoCompleteWrite(
87      struct urb *urb
88     );
89
90
91 static
92 void
93 s_nsControlInUsbIoCompleteRead(
94      struct urb *urb
95     );
96
97 static
98 void
99 s_nsControlInUsbIoCompleteWrite(
100      struct urb *urb
101     );
102
103 /*---------------------  Export Variables  --------------------------*/
104
105 /*---------------------  Export Functions  --------------------------*/
106
107 int PIPEnsControlOutAsyn(
108      PSDevice     pDevice,
109      BYTE         byRequest,
110      WORD         wValue,
111      WORD         wIndex,
112      WORD         wLength,
113      PBYTE        pbyBuffer
114     )
115 {
116         int ntStatus;
117
118     if (pDevice->Flags & fMP_DISCONNECTED)
119         return STATUS_FAILURE;
120
121     if (pDevice->Flags & fMP_CONTROL_WRITES)
122         return STATUS_FAILURE;
123
124     if (in_interrupt()) {
125         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"in_interrupt return ..byRequest %x\n", byRequest);
126         return STATUS_FAILURE;
127     }
128
129     ntStatus = usb_control_msg(
130                             pDevice->usb,
131                             usb_sndctrlpipe(pDevice->usb , 0),
132                             byRequest,
133                             0x40, // RequestType
134                             wValue,
135                             wIndex,
136                             (void *) pbyBuffer,
137                             wLength,
138                             HZ
139                           );
140     if (ntStatus >= 0) {
141         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"usb_sndctrlpipe ntStatus= %d\n", ntStatus);
142         ntStatus = 0;
143     } else {
144         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"usb_sndctrlpipe fail, ntStatus= %d\n", ntStatus);
145     }
146
147     return ntStatus;
148 }
149
150 int PIPEnsControlOut(
151      PSDevice     pDevice,
152      BYTE         byRequest,
153      WORD         wValue,
154      WORD         wIndex,
155      WORD         wLength,
156      PBYTE        pbyBuffer
157     )
158 {
159         int ntStatus = 0;
160     int ii;
161
162     if (pDevice->Flags & fMP_DISCONNECTED)
163         return STATUS_FAILURE;
164
165     if (pDevice->Flags & fMP_CONTROL_WRITES)
166         return STATUS_FAILURE;
167
168         pDevice->sUsbCtlRequest.bRequestType = 0x40;
169         pDevice->sUsbCtlRequest.bRequest = byRequest;
170         pDevice->sUsbCtlRequest.wValue = cpu_to_le16p(&wValue);
171         pDevice->sUsbCtlRequest.wIndex = cpu_to_le16p(&wIndex);
172         pDevice->sUsbCtlRequest.wLength = cpu_to_le16p(&wLength);
173         pDevice->pControlURB->transfer_flags |= URB_ASYNC_UNLINK;
174     pDevice->pControlURB->actual_length = 0;
175     // Notice, pbyBuffer limited point to variable buffer, can't be constant.
176         usb_fill_control_urb(pDevice->pControlURB, pDevice->usb,
177                          usb_sndctrlpipe(pDevice->usb , 0), (char *) &pDevice->sUsbCtlRequest,
178                          pbyBuffer, wLength, s_nsControlInUsbIoCompleteWrite, pDevice);
179
180         ntStatus = usb_submit_urb(pDevice->pControlURB, GFP_ATOMIC);
181         if (ntStatus != 0) {
182                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control send request submission failed: %d\n", ntStatus);
183                 return STATUS_FAILURE;
184         }
185         else {
186             MP_SET_FLAG(pDevice, fMP_CONTROL_WRITES);
187         }
188         spin_unlock_irq(&pDevice->lock);
189     for (ii = 0; ii <= USB_CTL_WAIT; ii ++) {
190
191         if (pDevice->Flags & fMP_CONTROL_WRITES)
192                 mdelay(1);
193         else
194                 break;
195
196         if (ii >= USB_CTL_WAIT) {
197                 DBG_PRT(MSG_LEVEL_DEBUG,
198                         KERN_INFO "control send request submission timeout\n");
199             spin_lock_irq(&pDevice->lock);
200             MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
201             return STATUS_FAILURE;
202         }
203     }
204         spin_lock_irq(&pDevice->lock);
205
206     return STATUS_SUCCESS;
207 }
208
209 int PIPEnsControlIn(
210      PSDevice     pDevice,
211      BYTE         byRequest,
212      WORD         wValue,
213      WORD         wIndex,
214      WORD         wLength,
215        PBYTE   pbyBuffer
216     )
217 {
218         int ntStatus = 0;
219     int ii;
220
221     if (pDevice->Flags & fMP_DISCONNECTED)
222         return STATUS_FAILURE;
223
224     if (pDevice->Flags & fMP_CONTROL_READS)
225         return STATUS_FAILURE;
226
227         pDevice->sUsbCtlRequest.bRequestType = 0xC0;
228         pDevice->sUsbCtlRequest.bRequest = byRequest;
229         pDevice->sUsbCtlRequest.wValue = cpu_to_le16p(&wValue);
230         pDevice->sUsbCtlRequest.wIndex = cpu_to_le16p(&wIndex);
231         pDevice->sUsbCtlRequest.wLength = cpu_to_le16p(&wLength);
232         pDevice->pControlURB->transfer_flags |= URB_ASYNC_UNLINK;
233     pDevice->pControlURB->actual_length = 0;
234         usb_fill_control_urb(pDevice->pControlURB, pDevice->usb,
235                          usb_rcvctrlpipe(pDevice->usb , 0), (char *) &pDevice->sUsbCtlRequest,
236                          pbyBuffer, wLength, s_nsControlInUsbIoCompleteRead, pDevice);
237
238         ntStatus = usb_submit_urb(pDevice->pControlURB, GFP_ATOMIC);
239         if (ntStatus != 0) {
240                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control request submission failed: %d\n", ntStatus);
241         }else {
242                 MP_SET_FLAG(pDevice, fMP_CONTROL_READS);
243     }
244
245         spin_unlock_irq(&pDevice->lock);
246     for (ii = 0; ii <= USB_CTL_WAIT; ii ++) {
247
248         if (pDevice->Flags & fMP_CONTROL_READS)
249                 mdelay(1);
250         else
251                 break;
252
253         if (ii >= USB_CTL_WAIT) {
254                 DBG_PRT(MSG_LEVEL_DEBUG,
255                         KERN_INFO "control rcv request submission timeout\n");
256             spin_lock_irq(&pDevice->lock);
257             MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
258             return STATUS_FAILURE;
259         }
260     }
261         spin_lock_irq(&pDevice->lock);
262
263     return ntStatus;
264 }
265
266 static
267 void
268 s_nsControlInUsbIoCompleteWrite(
269      struct urb *urb
270     )
271 {
272     PSDevice        pDevice;
273
274         pDevice = urb->context;
275         switch (urb->status) {
276         case 0:
277                 break;
278         case -EINPROGRESS:
279                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status EINPROGRESS%d\n", urb->status);
280                 break;
281         case -ENOENT:
282                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status ENOENT %d\n", urb->status);
283                 break;
284         default:
285                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status %d\n", urb->status);
286         }
287
288     MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
289 }
290
291
292
293 /*
294  * Description:
295  *      Complete function of usb Control callback
296  *
297  * Parameters:
298  *  In:
299  *      pDevice     - Pointer to the adapter
300  *
301  *  Out:
302  *      none
303  *
304  * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
305  *
306  */
307 static
308 void
309 s_nsControlInUsbIoCompleteRead(
310      struct urb *urb
311     )
312 {
313     PSDevice        pDevice;
314
315         pDevice = urb->context;
316         switch (urb->status) {
317         case 0:
318                 break;
319         case -EINPROGRESS:
320                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status EINPROGRESS%d\n", urb->status);
321                 break;
322         case -ENOENT:
323                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status = ENOENT %d\n", urb->status);
324                 break;
325         default:
326                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status %d\n", urb->status);
327         }
328
329     MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
330 }
331
332
333
334
335 /*
336  * Description:
337  *      Allocates an usb interrupt in irp and calls USBD.
338  *
339  * Parameters:
340  *  In:
341  *      pDevice     - Pointer to the adapter
342  *  Out:
343  *      none
344  *
345  * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
346  *
347  */
348 int PIPEnsInterruptRead(PSDevice pDevice)
349 {
350     int ntStatus = STATUS_FAILURE;
351
352     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartInterruptUsbRead()\n");
353
354     if(pDevice->intBuf.bInUse == TRUE){
355         return (STATUS_FAILURE);
356     }
357     pDevice->intBuf.bInUse = TRUE;
358 //    pDevice->bEventAvailable = FALSE;
359     pDevice->ulIntInPosted++;
360
361     //
362     // Now that we have created the urb, we will send a
363     // request to the USB device object.
364     //
365     pDevice->pInterruptURB->interval = pDevice->int_interval;
366
367 usb_fill_bulk_urb(pDevice->pInterruptURB,
368                 pDevice->usb,
369                 usb_rcvbulkpipe(pDevice->usb, 1),
370                 (void *) pDevice->intBuf.pDataBuf,
371                 MAX_INTERRUPT_SIZE,
372                 s_nsInterruptUsbIoCompleteRead,
373                 pDevice);
374
375         ntStatus = usb_submit_urb(pDevice->pInterruptURB, GFP_ATOMIC);
376         if (ntStatus != 0) {
377             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
378     }
379
380     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----s_nsStartInterruptUsbRead Return(%x)\n",ntStatus);
381     return ntStatus;
382 }
383
384
385 /*
386  * Description:
387  *      Complete function of usb interrupt in irp.
388  *
389  * Parameters:
390  *  In:
391  *      pDevice     - Pointer to the adapter
392  *
393  *  Out:
394  *      none
395  *
396  * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
397  *
398  */
399 static
400 void
401 s_nsInterruptUsbIoCompleteRead(
402      struct urb *urb
403     )
404
405 {
406     PSDevice        pDevice;
407     int ntStatus;
408
409     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsInterruptUsbIoCompleteRead\n");
410     //
411     // The context given to IoSetCompletionRoutine is the receive buffer object
412     //
413     pDevice = (PSDevice)urb->context;
414
415     //
416     // We have a number of cases:
417     //      1) The USB read timed out and we received no data.
418     //      2) The USB read timed out and we received some data.
419     //      3) The USB read was successful and fully filled our irp buffer.
420     //      4) The irp was cancelled.
421     //      5) Some other failure from the USB device object.
422     //
423     ntStatus = urb->status;
424
425     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsInterruptUsbIoCompleteRead Status %d\n", ntStatus);
426
427     // if we were not successful, we need to free the int buffer for future use right here
428     // otherwise interrupt data handler will free int buffer after it handle it.
429     if (( ntStatus != STATUS_SUCCESS )) {
430         pDevice->ulBulkInError++;
431         pDevice->intBuf.bInUse = FALSE;
432
433 //        if (ntStatus == USBD_STATUS_CRC) {
434 //            pDevice->ulIntInContCRCError++;
435 //        }
436
437 //        if (ntStatus == STATUS_NOT_CONNECTED )
438 //        {
439             pDevice->fKillEventPollingThread = TRUE;
440 //        }
441         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"IntUSBIoCompleteControl STATUS = %d\n", ntStatus );
442     } else {
443             pDevice->ulIntInBytesRead += (unsigned long) urb->actual_length;
444             pDevice->ulIntInContCRCError = 0;
445             pDevice->bEventAvailable = TRUE;
446             INTnsProcessData(pDevice);
447     }
448
449     STAvUpdateUSBCounter(&pDevice->scStatistic.USB_InterruptStat, ntStatus);
450
451
452     if (pDevice->fKillEventPollingThread != TRUE) {
453        usb_fill_bulk_urb(pDevice->pInterruptURB,
454                       pDevice->usb,
455                       usb_rcvbulkpipe(pDevice->usb, 1),
456                      (void *) pDevice->intBuf.pDataBuf,
457                      MAX_INTERRUPT_SIZE,
458                      s_nsInterruptUsbIoCompleteRead,
459                      pDevice);
460
461         ntStatus = usb_submit_urb(pDevice->pInterruptURB, GFP_ATOMIC);
462         if (ntStatus != 0) {
463             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
464            }
465     }
466     //
467     // We return STATUS_MORE_PROCESSING_REQUIRED so that the completion
468     // routine (IofCompleteRequest) will stop working on the irp.
469     //
470     return ;
471 }
472
473 /*
474  * Description:
475  *      Allocates an usb BulkIn  irp and calls USBD.
476  *
477  * Parameters:
478  *  In:
479  *      pDevice     - Pointer to the adapter
480  *  Out:
481  *      none
482  *
483  * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
484  *
485  */
486 int PIPEnsBulkInUsbRead(PSDevice pDevice, PRCB pRCB)
487 {
488         int ntStatus = 0;
489     struct urb          *pUrb;
490
491
492     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartBulkInUsbRead\n");
493
494     if (pDevice->Flags & fMP_DISCONNECTED)
495         return STATUS_FAILURE;
496
497     pDevice->ulBulkInPosted++;
498
499
500         pUrb = pRCB->pUrb;
501     //
502     // Now that we have created the urb, we will send a
503     // request to the USB device object.
504     //
505     if (pRCB->skb == NULL) {
506         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pRCB->skb is null \n");
507         return ntStatus;
508     }
509
510         usb_fill_bulk_urb(pUrb,
511                 pDevice->usb,
512                 usb_rcvbulkpipe(pDevice->usb, 2),
513                 (void *) (pRCB->skb->data),
514                 MAX_TOTAL_SIZE_WITH_ALL_HEADERS,
515                 s_nsBulkInUsbIoCompleteRead,
516                 pRCB);
517
518         ntStatus = usb_submit_urb(pUrb, GFP_ATOMIC);
519         if (ntStatus != 0) {
520                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Rx URB failed %d\n", ntStatus);
521                 return STATUS_FAILURE ;
522         }
523     pRCB->Ref = 1;
524     pRCB->bBoolInUse= TRUE;
525
526     return ntStatus;
527 }
528
529
530
531
532 /*
533  * Description:
534  *      Complete function of usb BulkIn irp.
535  *
536  * Parameters:
537  *  In:
538  *      pDevice     - Pointer to the adapter
539  *
540  *  Out:
541  *      none
542  *
543  * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
544  *
545  */
546 static
547 void
548 s_nsBulkInUsbIoCompleteRead(
549      struct urb *urb
550     )
551
552 {
553     PRCB    pRCB = (PRCB)urb->context;
554     PSDevice pDevice = (PSDevice)pRCB->pDevice;
555     unsigned long   bytesRead;
556     BOOL    bIndicateReceive = FALSE;
557     BOOL    bReAllocSkb = FALSE;
558     int status;
559
560     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkInUsbIoCompleteRead\n");
561     status = urb->status;
562     bytesRead = urb->actual_length;
563
564     if (status) {
565         pDevice->ulBulkInError++;
566         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK In failed %d\n", status);
567
568            pDevice->scStatistic.RxFcsErrCnt ++;
569 //todo...xxxxxx
570 //        if (status == USBD_STATUS_CRC) {
571 //            pDevice->ulBulkInContCRCError++;
572 //        }
573 //        if (status == STATUS_DEVICE_NOT_CONNECTED )
574 //        {
575 //            MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
576 //        }
577     } else {
578         if (bytesRead)
579                 bIndicateReceive = TRUE;
580         pDevice->ulBulkInContCRCError = 0;
581         pDevice->ulBulkInBytesRead += bytesRead;
582
583            pDevice->scStatistic.RxOkCnt ++;
584     }
585
586
587     STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkInStat, status);
588
589     if (bIndicateReceive) {
590         spin_lock(&pDevice->lock);
591         if (RXbBulkInProcessData(pDevice, pRCB, bytesRead) == TRUE)
592             bReAllocSkb = TRUE;
593         spin_unlock(&pDevice->lock);
594     }
595     pRCB->Ref--;
596     if (pRCB->Ref == 0)
597     {
598         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"RxvFreeNormal %d \n",pDevice->NumRecvFreeList);
599         spin_lock(&pDevice->lock);
600         RXvFreeRCB(pRCB, bReAllocSkb);
601         spin_unlock(&pDevice->lock);
602     }
603
604
605     return;
606 }
607
608 /*
609  * Description:
610  *      Allocates an usb BulkOut  irp and calls USBD.
611  *
612  * Parameters:
613  *  In:
614  *      pDevice     - Pointer to the adapter
615  *  Out:
616  *      none
617  *
618  * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
619  *
620  */
621 int
622 PIPEnsSendBulkOut(
623       PSDevice pDevice,
624       PUSB_SEND_CONTEXT pContext
625     )
626 {
627     int status;
628     struct urb          *pUrb;
629
630
631
632     pDevice->bPWBitOn = FALSE;
633
634 /*
635     if (pDevice->pPendingBulkOutContext != NULL) {
636         pDevice->NumContextsQueued++;
637         EnqueueContext(pDevice->FirstTxContextQueue, pDevice->LastTxContextQueue, pContext);
638         status = STATUS_PENDING;
639         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send pending!\n");
640         return status;
641     }
642 */
643
644     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsSendBulkOut\n");
645
646     if (MP_IS_READY(pDevice) && (pDevice->Flags & fMP_POST_WRITES)) {
647
648         pUrb = pContext->pUrb;
649         pDevice->ulBulkOutPosted++;
650 //        pDevice->pPendingBulkOutContext = pContext;
651         usb_fill_bulk_urb(
652                     pUrb,
653                         pDevice->usb,
654                     usb_sndbulkpipe(pDevice->usb, 3),
655                     (void *) &(pContext->Data[0]),
656                         pContext->uBufLen,
657                         s_nsBulkOutIoCompleteWrite,
658                         pContext);
659
660         status = usb_submit_urb(pUrb, GFP_ATOMIC);
661         if (status != 0)
662         {
663                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Tx URB failed %d\n", status);
664                 pContext->bBoolInUse = FALSE;
665                 return STATUS_FAILURE;
666         }
667         return STATUS_PENDING;
668     }
669     else {
670         pContext->bBoolInUse = FALSE;
671         return STATUS_RESOURCES;
672     }
673 }
674
675 /*
676  * Description: s_nsBulkOutIoCompleteWrite
677  *     1a) Indicate to the protocol the status of the write.
678  *     1b) Return ownership of the packet to the protocol.
679  *
680  *     2)  If any more packets are queue for sending, send another packet
681  *         to USBD.
682  *         If the attempt to send the packet to the driver fails,
683  *         return ownership of the packet to the protocol and
684  *         try another packet (until one succeeds).
685  *
686  * Parameters:
687  *  In:
688  *      pdoUsbDevObj  - pointer to the USB device object which
689  *                      completed the irp
690  *      pIrp          - the irp which was completed by the
691  *                      device object
692  *      pContext      - the context given to IoSetCompletionRoutine
693  *                      before calling IoCallDriver on the irp
694  *                      The pContext is a pointer to the USB device object.
695  *  Out:
696  *      none
697  *
698  * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
699  *               (IofCompleteRequest) to stop working on the irp.
700  *
701  */
702 static
703 void
704 s_nsBulkOutIoCompleteWrite(
705      struct urb *urb
706     )
707 {
708     PSDevice            pDevice;
709     int status;
710     CONTEXT_TYPE        ContextType;
711     unsigned long               ulBufLen;
712     PUSB_SEND_CONTEXT   pContext;
713
714
715     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkOutIoCompleteWrite\n");
716     //
717     // The context given to IoSetCompletionRoutine is an USB_CONTEXT struct
718     //
719     pContext = (PUSB_SEND_CONTEXT) urb->context;
720     ASSERT( NULL != pContext );
721
722     pDevice = pContext->pDevice;
723     ContextType = pContext->Type;
724     ulBufLen = pContext->uBufLen;
725
726     if (!netif_device_present(pDevice->dev))
727             return;
728
729    //
730     // Perform various IRP, URB, and buffer 'sanity checks'
731     //
732
733     status = urb->status;
734     //we should have failed, succeeded, or cancelled, but NOT be pending
735     STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkOutStat, status);
736
737     if(status == STATUS_SUCCESS) {
738         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Write %d bytes\n",(int)ulBufLen);
739         pDevice->ulBulkOutBytesWrite += ulBufLen;
740         pDevice->ulBulkOutContCRCError = 0;
741         pDevice->nTxDataTimeCout = 0;
742
743     } else {
744         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK Out failed %d\n", status);
745         pDevice->ulBulkOutError++;
746     }
747
748 //    pDevice->ulCheckForHangCount = 0;
749 //    pDevice->pPendingBulkOutContext = NULL;
750
751     if ( CONTEXT_DATA_PACKET == ContextType ) {
752         // Indicate to the protocol the status of the sent packet and return
753         // ownership of the packet.
754             if (pContext->pPacket != NULL) {
755                 dev_kfree_skb_irq(pContext->pPacket);
756                 pContext->pPacket = NULL;
757             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"tx  %d bytes\n",(int)ulBufLen);
758             }
759
760         pDevice->dev->trans_start = jiffies;
761
762
763         if (status == STATUS_SUCCESS) {
764             pDevice->packetsSent++;
765         }
766         else {
767             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send USB error! [%08xh]\n", status);
768             pDevice->packetsSentDropped++;
769         }
770
771     }
772     if (pDevice->bLinkPass == TRUE) {
773         if (netif_queue_stopped(pDevice->dev))
774             netif_wake_queue(pDevice->dev);
775     }
776     pContext->bBoolInUse = FALSE;
777
778     return;
779 }