2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
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.
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.
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.
22 * Purpose: Handle USB control endpoint
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
36 * 04-05-2004 Jerry Chen: Initial release
37 * 11-24-2004 Warren Hsu: Add ControlvWriteByte,ControlvReadByte,ControlvMaskByte
48 /*--------------------- Static Definitions -------------------------*/
51 //endpoint 1: interrupt
52 //endpoint 2: read bulk
53 //endpoint 3: write bulk
56 //#define REQUEST_OUT (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE) // 0x40
57 //#define REQUEST_IN (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE ) //0xc0
58 //static int msglevel =MSG_LEVEL_DEBUG;
59 static int msglevel
=MSG_LEVEL_INFO
;
62 #define USB_CTL_WAIT 500 //ms
64 #ifndef URB_ASYNC_UNLINK
65 #define URB_ASYNC_UNLINK 0
68 /*--------------------- Static Classes ----------------------------*/
70 /*--------------------- Static Variables --------------------------*/
72 /*--------------------- Static Functions --------------------------*/
75 s_nsInterruptUsbIoCompleteRead(
82 s_nsBulkInUsbIoCompleteRead(
89 s_nsBulkOutIoCompleteWrite(
96 s_nsControlInUsbIoCompleteRead(
102 s_nsControlInUsbIoCompleteWrite(
106 /*--------------------- Export Variables --------------------------*/
108 /*--------------------- Export Functions --------------------------*/
110 int PIPEnsControlOutAsyn(
121 if (pDevice
->Flags
& fMP_DISCONNECTED
)
122 return STATUS_FAILURE
;
124 if (pDevice
->Flags
& fMP_CONTROL_WRITES
)
125 return STATUS_FAILURE
;
127 if (in_interrupt()) {
128 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"in_interrupt return ..byRequest %x\n", byRequest
);
129 return STATUS_FAILURE
;
132 ntStatus
= usb_control_msg(
134 usb_sndctrlpipe(pDevice
->usb
, 0),
144 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"usb_sndctrlpipe ntStatus= %d\n", ntStatus
);
147 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"usb_sndctrlpipe fail, ntStatus= %d\n", ntStatus
);
153 int PIPEnsControlOut(
165 if (pDevice
->Flags
& fMP_DISCONNECTED
)
166 return STATUS_FAILURE
;
168 if (pDevice
->Flags
& fMP_CONTROL_WRITES
)
169 return STATUS_FAILURE
;
171 pDevice
->sUsbCtlRequest
.bRequestType
= 0x40;
172 pDevice
->sUsbCtlRequest
.bRequest
= byRequest
;
173 pDevice
->sUsbCtlRequest
.wValue
= cpu_to_le16p(&wValue
);
174 pDevice
->sUsbCtlRequest
.wIndex
= cpu_to_le16p(&wIndex
);
175 pDevice
->sUsbCtlRequest
.wLength
= cpu_to_le16p(&wLength
);
176 pDevice
->pControlURB
->transfer_flags
|= URB_ASYNC_UNLINK
;
177 pDevice
->pControlURB
->actual_length
= 0;
178 // Notice, pbyBuffer limited point to variable buffer, can't be constant.
179 usb_fill_control_urb(pDevice
->pControlURB
, pDevice
->usb
,
180 usb_sndctrlpipe(pDevice
->usb
, 0), (char *) &pDevice
->sUsbCtlRequest
,
181 pbyBuffer
, wLength
, s_nsControlInUsbIoCompleteWrite
, pDevice
);
183 ntStatus
= usb_submit_urb(pDevice
->pControlURB
, GFP_ATOMIC
);
185 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"control send request submission failed: %d\n", ntStatus
);
186 return STATUS_FAILURE
;
189 MP_SET_FLAG(pDevice
, fMP_CONTROL_WRITES
);
191 spin_unlock_irq(&pDevice
->lock
);
192 for (ii
= 0; ii
<= USB_CTL_WAIT
; ii
++) {
194 if (pDevice
->Flags
& fMP_CONTROL_WRITES
)
199 if (ii
>= USB_CTL_WAIT
) {
200 DBG_PRT(MSG_LEVEL_DEBUG
,
201 KERN_INFO
"control send request submission timeout\n");
202 spin_lock_irq(&pDevice
->lock
);
203 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_WRITES
);
204 return STATUS_FAILURE
;
207 spin_lock_irq(&pDevice
->lock
);
209 return STATUS_SUCCESS
;
224 if (pDevice
->Flags
& fMP_DISCONNECTED
)
225 return STATUS_FAILURE
;
227 if (pDevice
->Flags
& fMP_CONTROL_READS
)
228 return STATUS_FAILURE
;
230 pDevice
->sUsbCtlRequest
.bRequestType
= 0xC0;
231 pDevice
->sUsbCtlRequest
.bRequest
= byRequest
;
232 pDevice
->sUsbCtlRequest
.wValue
= cpu_to_le16p(&wValue
);
233 pDevice
->sUsbCtlRequest
.wIndex
= cpu_to_le16p(&wIndex
);
234 pDevice
->sUsbCtlRequest
.wLength
= cpu_to_le16p(&wLength
);
235 pDevice
->pControlURB
->transfer_flags
|= URB_ASYNC_UNLINK
;
236 pDevice
->pControlURB
->actual_length
= 0;
237 usb_fill_control_urb(pDevice
->pControlURB
, pDevice
->usb
,
238 usb_rcvctrlpipe(pDevice
->usb
, 0), (char *) &pDevice
->sUsbCtlRequest
,
239 pbyBuffer
, wLength
, s_nsControlInUsbIoCompleteRead
, pDevice
);
241 ntStatus
= usb_submit_urb(pDevice
->pControlURB
, GFP_ATOMIC
);
243 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"control request submission failed: %d\n", ntStatus
);
245 MP_SET_FLAG(pDevice
, fMP_CONTROL_READS
);
248 spin_unlock_irq(&pDevice
->lock
);
249 for (ii
= 0; ii
<= USB_CTL_WAIT
; ii
++) {
251 if (pDevice
->Flags
& fMP_CONTROL_READS
)
256 if (ii
>= USB_CTL_WAIT
) {
257 DBG_PRT(MSG_LEVEL_DEBUG
,
258 KERN_INFO
"control rcv request submission timeout\n");
259 spin_lock_irq(&pDevice
->lock
);
260 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_READS
);
261 return STATUS_FAILURE
;
264 spin_lock_irq(&pDevice
->lock
);
271 s_nsControlInUsbIoCompleteWrite(
277 pDevice
= urb
->context
;
278 switch (urb
->status
) {
282 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl write urb status EINPROGRESS%d\n", urb
->status
);
285 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl write urb status ENOENT %d\n", urb
->status
);
288 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl write urb status %d\n", urb
->status
);
291 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_WRITES
);
298 * Complete function of usb Control callback
302 * pDevice - Pointer to the adapter
307 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
312 s_nsControlInUsbIoCompleteRead(
318 pDevice
= urb
->context
;
319 switch (urb
->status
) {
323 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl read urb status EINPROGRESS%d\n", urb
->status
);
326 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl read urb status = ENOENT %d\n", urb
->status
);
329 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl read urb status %d\n", urb
->status
);
332 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_READS
);
340 * Allocates an usb interrupt in irp and calls USBD.
344 * pDevice - Pointer to the adapter
348 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
351 int PIPEnsInterruptRead(PSDevice pDevice
)
353 int ntStatus
= STATUS_FAILURE
;
355 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsStartInterruptUsbRead()\n");
357 if(pDevice
->intBuf
.bInUse
== TRUE
){
358 return (STATUS_FAILURE
);
360 pDevice
->intBuf
.bInUse
= TRUE
;
361 // pDevice->bEventAvailable = FALSE;
362 pDevice
->ulIntInPosted
++;
365 // Now that we have created the urb, we will send a
366 // request to the USB device object.
368 pDevice
->pInterruptURB
->interval
= pDevice
->int_interval
;
370 usb_fill_bulk_urb(pDevice
->pInterruptURB
,
372 usb_rcvbulkpipe(pDevice
->usb
, 1),
373 (void *) pDevice
->intBuf
.pDataBuf
,
375 s_nsInterruptUsbIoCompleteRead
,
378 ntStatus
= usb_submit_urb(pDevice
->pInterruptURB
, GFP_ATOMIC
);
380 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Submit int URB failed %d\n", ntStatus
);
383 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"<----s_nsStartInterruptUsbRead Return(%x)\n",ntStatus
);
390 * Complete function of usb interrupt in irp.
394 * pDevice - Pointer to the adapter
399 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
404 s_nsInterruptUsbIoCompleteRead(
412 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsInterruptUsbIoCompleteRead\n");
414 // The context given to IoSetCompletionRoutine is the receive buffer object
416 pDevice
= (PSDevice
)urb
->context
;
419 // We have a number of cases:
420 // 1) The USB read timed out and we received no data.
421 // 2) The USB read timed out and we received some data.
422 // 3) The USB read was successful and fully filled our irp buffer.
423 // 4) The irp was cancelled.
424 // 5) Some other failure from the USB device object.
426 ntStatus
= urb
->status
;
428 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"s_nsInterruptUsbIoCompleteRead Status %d\n", ntStatus
);
430 // if we were not successful, we need to free the int buffer for future use right here
431 // otherwise interrupt data handler will free int buffer after it handle it.
432 if (( ntStatus
!= STATUS_SUCCESS
)) {
433 pDevice
->ulBulkInError
++;
434 pDevice
->intBuf
.bInUse
= FALSE
;
436 // if (ntStatus == USBD_STATUS_CRC) {
437 // pDevice->ulIntInContCRCError++;
440 // if (ntStatus == STATUS_NOT_CONNECTED )
442 pDevice
->fKillEventPollingThread
= TRUE
;
444 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"IntUSBIoCompleteControl STATUS = %d\n", ntStatus
);
446 pDevice
->ulIntInBytesRead
+= (unsigned long) urb
->actual_length
;
447 pDevice
->ulIntInContCRCError
= 0;
448 pDevice
->bEventAvailable
= TRUE
;
449 INTnsProcessData(pDevice
);
452 STAvUpdateUSBCounter(&pDevice
->scStatistic
.USB_InterruptStat
, ntStatus
);
455 if (pDevice
->fKillEventPollingThread
!= TRUE
) {
456 usb_fill_bulk_urb(pDevice
->pInterruptURB
,
458 usb_rcvbulkpipe(pDevice
->usb
, 1),
459 (void *) pDevice
->intBuf
.pDataBuf
,
461 s_nsInterruptUsbIoCompleteRead
,
464 ntStatus
= usb_submit_urb(pDevice
->pInterruptURB
, GFP_ATOMIC
);
466 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Submit int URB failed %d\n", ntStatus
);
470 // We return STATUS_MORE_PROCESSING_REQUIRED so that the completion
471 // routine (IofCompleteRequest) will stop working on the irp.
478 * Allocates an usb BulkIn irp and calls USBD.
482 * pDevice - Pointer to the adapter
486 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
489 int PIPEnsBulkInUsbRead(PSDevice pDevice
, PRCB pRCB
)
495 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsStartBulkInUsbRead\n");
497 if (pDevice
->Flags
& fMP_DISCONNECTED
)
498 return STATUS_FAILURE
;
500 pDevice
->ulBulkInPosted
++;
505 // Now that we have created the urb, we will send a
506 // request to the USB device object.
508 if (pRCB
->skb
== NULL
) {
509 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"pRCB->skb is null \n");
513 usb_fill_bulk_urb(pUrb
,
515 usb_rcvbulkpipe(pDevice
->usb
, 2),
516 (void *) (pRCB
->skb
->data
),
517 MAX_TOTAL_SIZE_WITH_ALL_HEADERS
,
518 s_nsBulkInUsbIoCompleteRead
,
521 ntStatus
= usb_submit_urb(pUrb
, GFP_ATOMIC
);
523 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Submit Rx URB failed %d\n", ntStatus
);
524 return STATUS_FAILURE
;
527 pRCB
->bBoolInUse
= TRUE
;
537 * Complete function of usb BulkIn irp.
541 * pDevice - Pointer to the adapter
546 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
551 s_nsBulkInUsbIoCompleteRead(
556 PRCB pRCB
= (PRCB
)urb
->context
;
557 PSDevice pDevice
= (PSDevice
)pRCB
->pDevice
;
558 unsigned long bytesRead
;
559 BOOL bIndicateReceive
= FALSE
;
560 BOOL bReAllocSkb
= FALSE
;
563 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsBulkInUsbIoCompleteRead\n");
564 status
= urb
->status
;
565 bytesRead
= urb
->actual_length
;
568 pDevice
->ulBulkInError
++;
569 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"BULK In failed %d\n", status
);
571 pDevice
->scStatistic
.RxFcsErrCnt
++;
573 // if (status == USBD_STATUS_CRC) {
574 // pDevice->ulBulkInContCRCError++;
576 // if (status == STATUS_DEVICE_NOT_CONNECTED )
578 // MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
581 bIndicateReceive
= TRUE
;
582 pDevice
->ulBulkInContCRCError
= 0;
583 pDevice
->ulBulkInBytesRead
+= bytesRead
;
585 pDevice
->scStatistic
.RxOkCnt
++;
589 STAvUpdateUSBCounter(&pDevice
->scStatistic
.USB_BulkInStat
, status
);
591 if (bIndicateReceive
) {
592 spin_lock(&pDevice
->lock
);
593 if (RXbBulkInProcessData(pDevice
, pRCB
, bytesRead
) == TRUE
)
595 spin_unlock(&pDevice
->lock
);
600 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"RxvFreeNormal %d \n",pDevice
->NumRecvFreeList
);
601 spin_lock(&pDevice
->lock
);
602 RXvFreeRCB(pRCB
, bReAllocSkb
);
603 spin_unlock(&pDevice
->lock
);
612 * Allocates an usb BulkOut irp and calls USBD.
616 * pDevice - Pointer to the adapter
620 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
626 PUSB_SEND_CONTEXT pContext
634 pDevice
->bPWBitOn
= FALSE
;
637 if (pDevice->pPendingBulkOutContext != NULL) {
638 pDevice->NumContextsQueued++;
639 EnqueueContext(pDevice->FirstTxContextQueue, pDevice->LastTxContextQueue, pContext);
640 status = STATUS_PENDING;
641 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send pending!\n");
646 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"s_nsSendBulkOut\n");
648 if (MP_IS_READY(pDevice
) && (pDevice
->Flags
& fMP_POST_WRITES
)) {
650 pUrb
= pContext
->pUrb
;
651 pDevice
->ulBulkOutPosted
++;
652 // pDevice->pPendingBulkOutContext = pContext;
656 usb_sndbulkpipe(pDevice
->usb
, 3),
657 (void *) &(pContext
->Data
[0]),
659 s_nsBulkOutIoCompleteWrite
,
662 status
= usb_submit_urb(pUrb
, GFP_ATOMIC
);
665 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Submit Tx URB failed %d\n", status
);
666 return STATUS_FAILURE
;
668 return STATUS_PENDING
;
671 pContext
->bBoolInUse
= FALSE
;
672 return STATUS_RESOURCES
;
677 * Description: s_nsBulkOutIoCompleteWrite
678 * 1a) Indicate to the protocol the status of the write.
679 * 1b) Return ownership of the packet to the protocol.
681 * 2) If any more packets are queue for sending, send another packet
683 * If the attempt to send the packet to the driver fails,
684 * return ownership of the packet to the protocol and
685 * try another packet (until one succeeds).
689 * pdoUsbDevObj - pointer to the USB device object which
691 * pIrp - the irp which was completed by the
693 * pContext - the context given to IoSetCompletionRoutine
694 * before calling IoCallDriver on the irp
695 * The pContext is a pointer to the USB device object.
699 * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
700 * (IofCompleteRequest) to stop working on the irp.
705 s_nsBulkOutIoCompleteWrite(
711 CONTEXT_TYPE ContextType
;
712 unsigned long ulBufLen
;
713 PUSB_SEND_CONTEXT pContext
;
716 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsBulkOutIoCompleteWrite\n");
718 // The context given to IoSetCompletionRoutine is an USB_CONTEXT struct
720 pContext
= (PUSB_SEND_CONTEXT
) urb
->context
;
721 ASSERT( NULL
!= pContext
);
723 pDevice
= pContext
->pDevice
;
724 ContextType
= pContext
->Type
;
725 ulBufLen
= pContext
->uBufLen
;
727 if (!netif_device_present(pDevice
->dev
))
731 // Perform various IRP, URB, and buffer 'sanity checks'
734 status
= urb
->status
;
735 //we should have failed, succeeded, or cancelled, but NOT be pending
736 STAvUpdateUSBCounter(&pDevice
->scStatistic
.USB_BulkOutStat
, status
);
738 if(status
== STATUS_SUCCESS
) {
739 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Write %d bytes\n",(int)ulBufLen
);
740 pDevice
->ulBulkOutBytesWrite
+= ulBufLen
;
741 pDevice
->ulBulkOutContCRCError
= 0;
742 pDevice
->nTxDataTimeCout
= 0;
745 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"BULK Out failed %d\n", status
);
746 pDevice
->ulBulkOutError
++;
749 // pDevice->ulCheckForHangCount = 0;
750 // pDevice->pPendingBulkOutContext = NULL;
752 if ( CONTEXT_DATA_PACKET
== ContextType
) {
753 // Indicate to the protocol the status of the sent packet and return
754 // ownership of the packet.
755 if (pContext
->pPacket
!= NULL
) {
756 dev_kfree_skb_irq(pContext
->pPacket
);
757 pContext
->pPacket
= NULL
;
758 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"tx %d bytes\n",(int)ulBufLen
);
761 pDevice
->dev
->trans_start
= jiffies
;
764 if (status
== STATUS_SUCCESS
) {
765 pDevice
->packetsSent
++;
768 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Send USB error! [%08xh]\n", status
);
769 pDevice
->packetsSentDropped
++;
773 if (pDevice
->bLinkPass
== TRUE
) {
774 if (netif_queue_stopped(pDevice
->dev
))
775 netif_wake_queue(pDevice
->dev
);
777 pContext
->bBoolInUse
= FALSE
;