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
55 //static int msglevel =MSG_LEVEL_DEBUG;
56 static int msglevel
=MSG_LEVEL_INFO
;
59 #define USB_CTL_WAIT 500 //ms
61 #ifndef URB_ASYNC_UNLINK
62 #define URB_ASYNC_UNLINK 0
65 /*--------------------- Static Classes ----------------------------*/
67 /*--------------------- Static Variables --------------------------*/
69 /*--------------------- Static Functions --------------------------*/
70 static void s_nsInterruptUsbIoCompleteRead(struct urb
*urb
);
71 static void s_nsBulkInUsbIoCompleteRead(struct urb
*urb
);
72 static void s_nsBulkOutIoCompleteWrite(struct urb
*urb
);
73 static void s_nsControlInUsbIoCompleteRead(struct urb
*urb
);
74 static void s_nsControlInUsbIoCompleteWrite(struct urb
*urb
);
76 /*--------------------- Export Variables --------------------------*/
78 /*--------------------- Export Functions --------------------------*/
80 int PIPEnsControlOutAsyn(struct vnt_private
*pDevice
, u8 byRequest
,
81 u16 wValue
, u16 wIndex
, u16 wLength
, u8
*pbyBuffer
)
85 if (pDevice
->Flags
& fMP_DISCONNECTED
)
86 return STATUS_FAILURE
;
88 if (pDevice
->Flags
& fMP_CONTROL_WRITES
)
89 return STATUS_FAILURE
;
92 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"in_interrupt return ..byRequest %x\n", byRequest
);
93 return STATUS_FAILURE
;
96 ntStatus
= usb_control_msg(
98 usb_sndctrlpipe(pDevice
->usb
, 0),
108 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"usb_sndctrlpipe ntStatus= %d\n", ntStatus
);
111 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"usb_sndctrlpipe fail, ntStatus= %d\n", ntStatus
);
117 int PIPEnsControlOut(struct vnt_private
*pDevice
, u8 byRequest
, u16 wValue
,
118 u16 wIndex
, u16 wLength
, u8
*pbyBuffer
)
123 if (pDevice
->Flags
& fMP_DISCONNECTED
)
124 return STATUS_FAILURE
;
126 if (pDevice
->Flags
& fMP_CONTROL_WRITES
)
127 return STATUS_FAILURE
;
129 if (pDevice
->Flags
& fMP_CONTROL_READS
)
130 return STATUS_FAILURE
;
132 MP_SET_FLAG(pDevice
, fMP_CONTROL_WRITES
);
134 pDevice
->sUsbCtlRequest
.bRequestType
= 0x40;
135 pDevice
->sUsbCtlRequest
.bRequest
= byRequest
;
136 pDevice
->sUsbCtlRequest
.wValue
= cpu_to_le16p(&wValue
);
137 pDevice
->sUsbCtlRequest
.wIndex
= cpu_to_le16p(&wIndex
);
138 pDevice
->sUsbCtlRequest
.wLength
= cpu_to_le16p(&wLength
);
139 pDevice
->pControlURB
->transfer_flags
|= URB_ASYNC_UNLINK
;
140 pDevice
->pControlURB
->actual_length
= 0;
141 // Notice, pbyBuffer limited point to variable buffer, can't be constant.
142 usb_fill_control_urb(pDevice
->pControlURB
, pDevice
->usb
,
143 usb_sndctrlpipe(pDevice
->usb
, 0), (char *) &pDevice
->sUsbCtlRequest
,
144 pbyBuffer
, wLength
, s_nsControlInUsbIoCompleteWrite
, pDevice
);
146 ntStatus
= usb_submit_urb(pDevice
->pControlURB
, GFP_ATOMIC
);
148 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
149 "control send request submission failed: %d\n",
151 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_WRITES
);
152 return STATUS_FAILURE
;
155 spin_unlock_irq(&pDevice
->lock
);
156 for (ii
= 0; ii
<= USB_CTL_WAIT
; ii
++) {
158 if (pDevice
->Flags
& fMP_CONTROL_WRITES
)
163 if (ii
>= USB_CTL_WAIT
) {
164 DBG_PRT(MSG_LEVEL_DEBUG
,
165 KERN_INFO
"control send request submission timeout\n");
166 spin_lock_irq(&pDevice
->lock
);
167 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_WRITES
);
168 return STATUS_FAILURE
;
171 spin_lock_irq(&pDevice
->lock
);
173 return STATUS_SUCCESS
;
176 int PIPEnsControlIn(struct vnt_private
*pDevice
, u8 byRequest
, u16 wValue
,
177 u16 wIndex
, u16 wLength
, u8
*pbyBuffer
)
182 if (pDevice
->Flags
& fMP_DISCONNECTED
)
183 return STATUS_FAILURE
;
185 if (pDevice
->Flags
& fMP_CONTROL_READS
)
186 return STATUS_FAILURE
;
188 if (pDevice
->Flags
& fMP_CONTROL_WRITES
)
189 return STATUS_FAILURE
;
191 MP_SET_FLAG(pDevice
, fMP_CONTROL_READS
);
193 pDevice
->sUsbCtlRequest
.bRequestType
= 0xC0;
194 pDevice
->sUsbCtlRequest
.bRequest
= byRequest
;
195 pDevice
->sUsbCtlRequest
.wValue
= cpu_to_le16p(&wValue
);
196 pDevice
->sUsbCtlRequest
.wIndex
= cpu_to_le16p(&wIndex
);
197 pDevice
->sUsbCtlRequest
.wLength
= cpu_to_le16p(&wLength
);
198 pDevice
->pControlURB
->transfer_flags
|= URB_ASYNC_UNLINK
;
199 pDevice
->pControlURB
->actual_length
= 0;
200 usb_fill_control_urb(pDevice
->pControlURB
, pDevice
->usb
,
201 usb_rcvctrlpipe(pDevice
->usb
, 0), (char *) &pDevice
->sUsbCtlRequest
,
202 pbyBuffer
, wLength
, s_nsControlInUsbIoCompleteRead
, pDevice
);
204 ntStatus
= usb_submit_urb(pDevice
->pControlURB
, GFP_ATOMIC
);
206 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
207 "control request submission failed: %d\n", ntStatus
);
208 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_READS
);
209 return STATUS_FAILURE
;
212 spin_unlock_irq(&pDevice
->lock
);
213 for (ii
= 0; ii
<= USB_CTL_WAIT
; ii
++) {
215 if (pDevice
->Flags
& fMP_CONTROL_READS
)
220 if (ii
>= USB_CTL_WAIT
) {
221 DBG_PRT(MSG_LEVEL_DEBUG
,
222 KERN_INFO
"control rcv request submission timeout\n");
223 spin_lock_irq(&pDevice
->lock
);
224 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_READS
);
225 return STATUS_FAILURE
;
228 spin_lock_irq(&pDevice
->lock
);
233 static void s_nsControlInUsbIoCompleteWrite(struct urb
*urb
)
235 struct vnt_private
*pDevice
= (struct vnt_private
*)urb
->context
;
237 pDevice
= urb
->context
;
238 switch (urb
->status
) {
242 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl write urb status EINPROGRESS%d\n", urb
->status
);
245 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl write urb status ENOENT %d\n", urb
->status
);
248 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl write urb status %d\n", urb
->status
);
251 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_WRITES
);
258 * Complete function of usb Control callback
262 * pDevice - Pointer to the adapter
267 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
271 static void s_nsControlInUsbIoCompleteRead(struct urb
*urb
)
273 struct vnt_private
*pDevice
= (struct vnt_private
*)urb
->context
;
275 switch (urb
->status
) {
279 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl read urb status EINPROGRESS%d\n", urb
->status
);
282 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl read urb status = ENOENT %d\n", urb
->status
);
285 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl read urb status %d\n", urb
->status
);
288 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_READS
);
296 * Allocates an usb interrupt in irp and calls USBD.
300 * pDevice - Pointer to the adapter
304 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
308 int PIPEnsInterruptRead(struct vnt_private
*pDevice
)
310 int ntStatus
= STATUS_FAILURE
;
312 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsStartInterruptUsbRead()\n");
314 if(pDevice
->intBuf
.bInUse
== true){
315 return (STATUS_FAILURE
);
317 pDevice
->intBuf
.bInUse
= true;
318 // pDevice->bEventAvailable = false;
319 pDevice
->ulIntInPosted
++;
322 // Now that we have created the urb, we will send a
323 // request to the USB device object.
325 pDevice
->pInterruptURB
->interval
= pDevice
->int_interval
;
327 usb_fill_bulk_urb(pDevice
->pInterruptURB
,
329 usb_rcvbulkpipe(pDevice
->usb
, 1),
330 (void *) pDevice
->intBuf
.pDataBuf
,
332 s_nsInterruptUsbIoCompleteRead
,
335 ntStatus
= usb_submit_urb(pDevice
->pInterruptURB
, GFP_ATOMIC
);
337 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Submit int URB failed %d\n", ntStatus
);
340 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"<----s_nsStartInterruptUsbRead Return(%x)\n",ntStatus
);
347 * Complete function of usb interrupt in irp.
351 * pDevice - Pointer to the adapter
356 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
360 static void s_nsInterruptUsbIoCompleteRead(struct urb
*urb
)
362 struct vnt_private
*pDevice
= (struct vnt_private
*)urb
->context
;
365 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsInterruptUsbIoCompleteRead\n");
367 // The context given to IoSetCompletionRoutine is the receive buffer object
371 // We have a number of cases:
372 // 1) The USB read timed out and we received no data.
373 // 2) The USB read timed out and we received some data.
374 // 3) The USB read was successful and fully filled our irp buffer.
375 // 4) The irp was cancelled.
376 // 5) Some other failure from the USB device object.
378 ntStatus
= urb
->status
;
380 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"s_nsInterruptUsbIoCompleteRead Status %d\n", ntStatus
);
382 // if we were not successful, we need to free the int buffer for future use right here
383 // otherwise interrupt data handler will free int buffer after it handle it.
384 if (( ntStatus
!= STATUS_SUCCESS
)) {
385 pDevice
->ulBulkInError
++;
386 pDevice
->intBuf
.bInUse
= false;
388 // if (ntStatus == USBD_STATUS_CRC) {
389 // pDevice->ulIntInContCRCError++;
392 // if (ntStatus == STATUS_NOT_CONNECTED )
394 pDevice
->fKillEventPollingThread
= true;
396 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"IntUSBIoCompleteControl STATUS = %d\n", ntStatus
);
398 pDevice
->ulIntInBytesRead
+= (unsigned long) urb
->actual_length
;
399 pDevice
->ulIntInContCRCError
= 0;
400 pDevice
->bEventAvailable
= true;
401 INTnsProcessData(pDevice
);
404 STAvUpdateUSBCounter(&pDevice
->scStatistic
.USB_InterruptStat
, ntStatus
);
407 if (pDevice
->fKillEventPollingThread
!= true) {
408 usb_fill_bulk_urb(pDevice
->pInterruptURB
,
410 usb_rcvbulkpipe(pDevice
->usb
, 1),
411 (void *) pDevice
->intBuf
.pDataBuf
,
413 s_nsInterruptUsbIoCompleteRead
,
416 ntStatus
= usb_submit_urb(pDevice
->pInterruptURB
, GFP_ATOMIC
);
418 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Submit int URB failed %d\n", ntStatus
);
422 // We return STATUS_MORE_PROCESSING_REQUIRED so that the completion
423 // routine (IofCompleteRequest) will stop working on the irp.
430 * Allocates an usb BulkIn irp and calls USBD.
434 * pDevice - Pointer to the adapter
438 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
442 int PIPEnsBulkInUsbRead(struct vnt_private
*pDevice
, PRCB pRCB
)
448 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsStartBulkInUsbRead\n");
450 if (pDevice
->Flags
& fMP_DISCONNECTED
)
451 return STATUS_FAILURE
;
453 pDevice
->ulBulkInPosted
++;
458 // Now that we have created the urb, we will send a
459 // request to the USB device object.
461 if (pRCB
->skb
== NULL
) {
462 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"pRCB->skb is null \n");
466 usb_fill_bulk_urb(pUrb
,
468 usb_rcvbulkpipe(pDevice
->usb
, 2),
469 (void *) (pRCB
->skb
->data
),
470 MAX_TOTAL_SIZE_WITH_ALL_HEADERS
,
471 s_nsBulkInUsbIoCompleteRead
,
474 ntStatus
= usb_submit_urb(pUrb
, GFP_ATOMIC
);
476 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Submit Rx URB failed %d\n", ntStatus
);
477 return STATUS_FAILURE
;
480 pRCB
->bBoolInUse
= true;
490 * Complete function of usb BulkIn irp.
494 * pDevice - Pointer to the adapter
499 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
503 static void s_nsBulkInUsbIoCompleteRead(struct urb
*urb
)
505 PRCB pRCB
= (PRCB
)urb
->context
;
506 struct vnt_private
*pDevice
= pRCB
->pDevice
;
507 unsigned long bytesRead
;
508 int bIndicateReceive
= false;
509 int bReAllocSkb
= false;
512 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsBulkInUsbIoCompleteRead\n");
513 status
= urb
->status
;
514 bytesRead
= urb
->actual_length
;
517 pDevice
->ulBulkInError
++;
518 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"BULK In failed %d\n", status
);
520 pDevice
->scStatistic
.RxFcsErrCnt
++;
522 // if (status == USBD_STATUS_CRC) {
523 // pDevice->ulBulkInContCRCError++;
525 // if (status == STATUS_DEVICE_NOT_CONNECTED )
527 // MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
531 bIndicateReceive
= true;
532 pDevice
->ulBulkInContCRCError
= 0;
533 pDevice
->ulBulkInBytesRead
+= bytesRead
;
535 pDevice
->scStatistic
.RxOkCnt
++;
539 STAvUpdateUSBCounter(&pDevice
->scStatistic
.USB_BulkInStat
, status
);
541 if (bIndicateReceive
) {
542 spin_lock(&pDevice
->lock
);
543 if (RXbBulkInProcessData(pDevice
, pRCB
, bytesRead
) == true)
545 spin_unlock(&pDevice
->lock
);
550 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"RxvFreeNormal %d \n",pDevice
->NumRecvFreeList
);
551 spin_lock(&pDevice
->lock
);
552 RXvFreeRCB(pRCB
, bReAllocSkb
);
553 spin_unlock(&pDevice
->lock
);
562 * Allocates an usb BulkOut irp and calls USBD.
566 * pDevice - Pointer to the adapter
570 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
574 int PIPEnsSendBulkOut(struct vnt_private
*pDevice
, PUSB_SEND_CONTEXT pContext
)
581 pDevice
->bPWBitOn
= false;
584 if (pDevice->pPendingBulkOutContext != NULL) {
585 pDevice->NumContextsQueued++;
586 EnqueueContext(pDevice->FirstTxContextQueue, pDevice->LastTxContextQueue, pContext);
587 status = STATUS_PENDING;
588 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send pending!\n");
593 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"s_nsSendBulkOut\n");
595 if (MP_IS_READY(pDevice
) && (pDevice
->Flags
& fMP_POST_WRITES
)) {
597 pUrb
= pContext
->pUrb
;
598 pDevice
->ulBulkOutPosted
++;
599 // pDevice->pPendingBulkOutContext = pContext;
603 usb_sndbulkpipe(pDevice
->usb
, 3),
604 (void *) &(pContext
->Data
[0]),
606 s_nsBulkOutIoCompleteWrite
,
609 status
= usb_submit_urb(pUrb
, GFP_ATOMIC
);
612 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Submit Tx URB failed %d\n", status
);
613 pContext
->bBoolInUse
= false;
614 return STATUS_FAILURE
;
616 return STATUS_PENDING
;
619 pContext
->bBoolInUse
= false;
620 return STATUS_RESOURCES
;
625 * Description: s_nsBulkOutIoCompleteWrite
626 * 1a) Indicate to the protocol the status of the write.
627 * 1b) Return ownership of the packet to the protocol.
629 * 2) If any more packets are queue for sending, send another packet
631 * If the attempt to send the packet to the driver fails,
632 * return ownership of the packet to the protocol and
633 * try another packet (until one succeeds).
637 * pdoUsbDevObj - pointer to the USB device object which
639 * pIrp - the irp which was completed by the
641 * pContext - the context given to IoSetCompletionRoutine
642 * before calling IoCallDriver on the irp
643 * The pContext is a pointer to the USB device object.
647 * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
648 * (IofCompleteRequest) to stop working on the irp.
652 static void s_nsBulkOutIoCompleteWrite(struct urb
*urb
)
654 struct vnt_private
*pDevice
;
656 CONTEXT_TYPE ContextType
;
657 unsigned long ulBufLen
;
658 PUSB_SEND_CONTEXT pContext
;
661 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsBulkOutIoCompleteWrite\n");
663 // The context given to IoSetCompletionRoutine is an USB_CONTEXT struct
665 pContext
= (PUSB_SEND_CONTEXT
) urb
->context
;
666 ASSERT( NULL
!= pContext
);
668 pDevice
= pContext
->pDevice
;
669 ContextType
= pContext
->Type
;
670 ulBufLen
= pContext
->uBufLen
;
672 if (!netif_device_present(pDevice
->dev
))
676 // Perform various IRP, URB, and buffer 'sanity checks'
679 status
= urb
->status
;
680 //we should have failed, succeeded, or cancelled, but NOT be pending
681 STAvUpdateUSBCounter(&pDevice
->scStatistic
.USB_BulkOutStat
, status
);
683 if(status
== STATUS_SUCCESS
) {
684 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Write %d bytes\n",(int)ulBufLen
);
685 pDevice
->ulBulkOutBytesWrite
+= ulBufLen
;
686 pDevice
->ulBulkOutContCRCError
= 0;
687 pDevice
->nTxDataTimeCout
= 0;
690 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"BULK Out failed %d\n", status
);
691 pDevice
->ulBulkOutError
++;
694 // pDevice->ulCheckForHangCount = 0;
695 // pDevice->pPendingBulkOutContext = NULL;
697 if ( CONTEXT_DATA_PACKET
== ContextType
) {
698 // Indicate to the protocol the status of the sent packet and return
699 // ownership of the packet.
700 if (pContext
->pPacket
!= NULL
) {
701 dev_kfree_skb_irq(pContext
->pPacket
);
702 pContext
->pPacket
= NULL
;
703 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"tx %d bytes\n",(int)ulBufLen
);
706 pDevice
->dev
->trans_start
= jiffies
;
709 if (status
== STATUS_SUCCESS
) {
710 pDevice
->packetsSent
++;
713 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Send USB error! [%08xh]\n", status
);
714 pDevice
->packetsSentDropped
++;
718 if (pDevice
->bLinkPass
== true) {
719 if (netif_queue_stopped(pDevice
->dev
))
720 netif_wake_queue(pDevice
->dev
);
722 pContext
->bBoolInUse
= false;