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
50 //endpoint 1: interrupt
51 //endpoint 2: read bulk
52 //endpoint 3: write bulk
54 //static int msglevel =MSG_LEVEL_DEBUG;
55 static int msglevel
=MSG_LEVEL_INFO
;
57 #define USB_CTL_WAIT 500 //ms
59 #ifndef URB_ASYNC_UNLINK
60 #define URB_ASYNC_UNLINK 0
63 static void s_nsInterruptUsbIoCompleteRead(struct urb
*urb
);
64 static void s_nsBulkInUsbIoCompleteRead(struct urb
*urb
);
65 static void s_nsBulkOutIoCompleteWrite(struct urb
*urb
);
66 static void s_nsControlInUsbIoCompleteRead(struct urb
*urb
);
67 static void s_nsControlInUsbIoCompleteWrite(struct urb
*urb
);
69 int PIPEnsControlOutAsyn(struct vnt_private
*pDevice
, u8 byRequest
,
70 u16 wValue
, u16 wIndex
, u16 wLength
, u8
*pbyBuffer
)
74 if (pDevice
->Flags
& fMP_DISCONNECTED
)
75 return STATUS_FAILURE
;
77 if (pDevice
->Flags
& fMP_CONTROL_WRITES
)
78 return STATUS_FAILURE
;
81 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"in_interrupt return ..byRequest %x\n", byRequest
);
82 return STATUS_FAILURE
;
85 ntStatus
= usb_control_msg(
87 usb_sndctrlpipe(pDevice
->usb
, 0),
97 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"usb_sndctrlpipe ntStatus= %d\n", ntStatus
);
100 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"usb_sndctrlpipe fail, ntStatus= %d\n", ntStatus
);
106 int PIPEnsControlOut(struct vnt_private
*pDevice
, u8 byRequest
, u16 wValue
,
107 u16 wIndex
, u16 wLength
, u8
*pbyBuffer
)
112 if (pDevice
->Flags
& fMP_DISCONNECTED
)
113 return STATUS_FAILURE
;
115 if (pDevice
->Flags
& fMP_CONTROL_WRITES
)
116 return STATUS_FAILURE
;
118 if (pDevice
->Flags
& fMP_CONTROL_READS
)
119 return STATUS_FAILURE
;
121 MP_SET_FLAG(pDevice
, fMP_CONTROL_WRITES
);
123 pDevice
->sUsbCtlRequest
.bRequestType
= 0x40;
124 pDevice
->sUsbCtlRequest
.bRequest
= byRequest
;
125 pDevice
->sUsbCtlRequest
.wValue
= cpu_to_le16p(&wValue
);
126 pDevice
->sUsbCtlRequest
.wIndex
= cpu_to_le16p(&wIndex
);
127 pDevice
->sUsbCtlRequest
.wLength
= cpu_to_le16p(&wLength
);
128 pDevice
->pControlURB
->transfer_flags
|= URB_ASYNC_UNLINK
;
129 pDevice
->pControlURB
->actual_length
= 0;
130 // Notice, pbyBuffer limited point to variable buffer, can't be constant.
131 usb_fill_control_urb(pDevice
->pControlURB
, pDevice
->usb
,
132 usb_sndctrlpipe(pDevice
->usb
, 0), (char *) &pDevice
->sUsbCtlRequest
,
133 pbyBuffer
, wLength
, s_nsControlInUsbIoCompleteWrite
, pDevice
);
135 ntStatus
= usb_submit_urb(pDevice
->pControlURB
, GFP_ATOMIC
);
137 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
138 "control send request submission failed: %d\n",
140 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_WRITES
);
141 return STATUS_FAILURE
;
144 spin_unlock_irq(&pDevice
->lock
);
145 for (ii
= 0; ii
<= USB_CTL_WAIT
; ii
++) {
147 if (pDevice
->Flags
& fMP_CONTROL_WRITES
)
152 if (ii
>= USB_CTL_WAIT
) {
153 DBG_PRT(MSG_LEVEL_DEBUG
,
154 KERN_INFO
"control send request submission timeout\n");
155 spin_lock_irq(&pDevice
->lock
);
156 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_WRITES
);
157 return STATUS_FAILURE
;
160 spin_lock_irq(&pDevice
->lock
);
162 return STATUS_SUCCESS
;
165 int PIPEnsControlIn(struct vnt_private
*pDevice
, u8 byRequest
, u16 wValue
,
166 u16 wIndex
, u16 wLength
, u8
*pbyBuffer
)
171 if (pDevice
->Flags
& fMP_DISCONNECTED
)
172 return STATUS_FAILURE
;
174 if (pDevice
->Flags
& fMP_CONTROL_READS
)
175 return STATUS_FAILURE
;
177 if (pDevice
->Flags
& fMP_CONTROL_WRITES
)
178 return STATUS_FAILURE
;
180 MP_SET_FLAG(pDevice
, fMP_CONTROL_READS
);
182 pDevice
->sUsbCtlRequest
.bRequestType
= 0xC0;
183 pDevice
->sUsbCtlRequest
.bRequest
= byRequest
;
184 pDevice
->sUsbCtlRequest
.wValue
= cpu_to_le16p(&wValue
);
185 pDevice
->sUsbCtlRequest
.wIndex
= cpu_to_le16p(&wIndex
);
186 pDevice
->sUsbCtlRequest
.wLength
= cpu_to_le16p(&wLength
);
187 pDevice
->pControlURB
->transfer_flags
|= URB_ASYNC_UNLINK
;
188 pDevice
->pControlURB
->actual_length
= 0;
189 usb_fill_control_urb(pDevice
->pControlURB
, pDevice
->usb
,
190 usb_rcvctrlpipe(pDevice
->usb
, 0), (char *) &pDevice
->sUsbCtlRequest
,
191 pbyBuffer
, wLength
, s_nsControlInUsbIoCompleteRead
, pDevice
);
193 ntStatus
= usb_submit_urb(pDevice
->pControlURB
, GFP_ATOMIC
);
195 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
196 "control request submission failed: %d\n", ntStatus
);
197 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_READS
);
198 return STATUS_FAILURE
;
201 spin_unlock_irq(&pDevice
->lock
);
202 for (ii
= 0; ii
<= USB_CTL_WAIT
; ii
++) {
204 if (pDevice
->Flags
& fMP_CONTROL_READS
)
209 if (ii
>= USB_CTL_WAIT
) {
210 DBG_PRT(MSG_LEVEL_DEBUG
,
211 KERN_INFO
"control rcv request submission timeout\n");
212 spin_lock_irq(&pDevice
->lock
);
213 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_READS
);
214 return STATUS_FAILURE
;
217 spin_lock_irq(&pDevice
->lock
);
222 static void s_nsControlInUsbIoCompleteWrite(struct urb
*urb
)
224 struct vnt_private
*pDevice
= (struct vnt_private
*)urb
->context
;
226 pDevice
= urb
->context
;
227 switch (urb
->status
) {
231 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl write urb status EINPROGRESS%d\n", urb
->status
);
234 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl write urb status ENOENT %d\n", urb
->status
);
237 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl write urb status %d\n", urb
->status
);
240 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_WRITES
);
245 * Complete function of usb Control callback
249 * pDevice - Pointer to the adapter
254 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
258 static void s_nsControlInUsbIoCompleteRead(struct urb
*urb
)
260 struct vnt_private
*pDevice
= (struct vnt_private
*)urb
->context
;
262 switch (urb
->status
) {
266 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl read urb status EINPROGRESS%d\n", urb
->status
);
269 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl read urb status = ENOENT %d\n", urb
->status
);
272 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl read urb status %d\n", urb
->status
);
275 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_READS
);
280 * Allocates an usb interrupt in irp and calls USBD.
284 * pDevice - Pointer to the adapter
288 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
292 int PIPEnsInterruptRead(struct vnt_private
*pDevice
)
294 int ntStatus
= STATUS_FAILURE
;
296 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsStartInterruptUsbRead()\n");
298 if(pDevice
->intBuf
.bInUse
== true){
299 return (STATUS_FAILURE
);
301 pDevice
->intBuf
.bInUse
= true;
302 // pDevice->bEventAvailable = false;
303 pDevice
->ulIntInPosted
++;
306 // Now that we have created the urb, we will send a
307 // request to the USB device object.
309 pDevice
->pInterruptURB
->interval
= pDevice
->int_interval
;
311 usb_fill_bulk_urb(pDevice
->pInterruptURB
,
313 usb_rcvbulkpipe(pDevice
->usb
, 1),
314 (void *) pDevice
->intBuf
.pDataBuf
,
316 s_nsInterruptUsbIoCompleteRead
,
319 ntStatus
= usb_submit_urb(pDevice
->pInterruptURB
, GFP_ATOMIC
);
321 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Submit int URB failed %d\n", ntStatus
);
324 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"<----s_nsStartInterruptUsbRead Return(%x)\n",ntStatus
);
330 * Complete function of usb interrupt in irp.
334 * pDevice - Pointer to the adapter
339 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
343 static void s_nsInterruptUsbIoCompleteRead(struct urb
*urb
)
345 struct vnt_private
*pDevice
= (struct vnt_private
*)urb
->context
;
348 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsInterruptUsbIoCompleteRead\n");
350 // The context given to IoSetCompletionRoutine is the receive buffer object
354 // We have a number of cases:
355 // 1) The USB read timed out and we received no data.
356 // 2) The USB read timed out and we received some data.
357 // 3) The USB read was successful and fully filled our irp buffer.
358 // 4) The irp was cancelled.
359 // 5) Some other failure from the USB device object.
361 ntStatus
= urb
->status
;
363 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"s_nsInterruptUsbIoCompleteRead Status %d\n", ntStatus
);
365 // if we were not successful, we need to free the int buffer for future use right here
366 // otherwise interrupt data handler will free int buffer after it handle it.
367 if (( ntStatus
!= STATUS_SUCCESS
)) {
368 pDevice
->ulBulkInError
++;
369 pDevice
->intBuf
.bInUse
= false;
371 // if (ntStatus == USBD_STATUS_CRC) {
372 // pDevice->ulIntInContCRCError++;
375 // if (ntStatus == STATUS_NOT_CONNECTED )
377 pDevice
->fKillEventPollingThread
= true;
379 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"IntUSBIoCompleteControl STATUS = %d\n", ntStatus
);
381 pDevice
->ulIntInBytesRead
+= (unsigned long) urb
->actual_length
;
382 pDevice
->ulIntInContCRCError
= 0;
383 pDevice
->bEventAvailable
= true;
384 INTnsProcessData(pDevice
);
387 STAvUpdateUSBCounter(&pDevice
->scStatistic
.USB_InterruptStat
, ntStatus
);
389 if (pDevice
->fKillEventPollingThread
!= true) {
390 usb_fill_bulk_urb(pDevice
->pInterruptURB
,
392 usb_rcvbulkpipe(pDevice
->usb
, 1),
393 (void *) pDevice
->intBuf
.pDataBuf
,
395 s_nsInterruptUsbIoCompleteRead
,
398 ntStatus
= usb_submit_urb(pDevice
->pInterruptURB
, GFP_ATOMIC
);
400 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Submit int URB failed %d\n", ntStatus
);
404 // We return STATUS_MORE_PROCESSING_REQUIRED so that the completion
405 // routine (IofCompleteRequest) will stop working on the irp.
412 * Allocates an usb BulkIn irp and calls USBD.
416 * pDevice - Pointer to the adapter
420 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
424 int PIPEnsBulkInUsbRead(struct vnt_private
*pDevice
, PRCB pRCB
)
429 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsStartBulkInUsbRead\n");
431 if (pDevice
->Flags
& fMP_DISCONNECTED
)
432 return STATUS_FAILURE
;
434 pDevice
->ulBulkInPosted
++;
438 // Now that we have created the urb, we will send a
439 // request to the USB device object.
441 if (pRCB
->skb
== NULL
) {
442 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"pRCB->skb is null \n");
446 usb_fill_bulk_urb(pUrb
,
448 usb_rcvbulkpipe(pDevice
->usb
, 2),
449 (void *) (pRCB
->skb
->data
),
450 MAX_TOTAL_SIZE_WITH_ALL_HEADERS
,
451 s_nsBulkInUsbIoCompleteRead
,
454 ntStatus
= usb_submit_urb(pUrb
, GFP_ATOMIC
);
456 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Submit Rx URB failed %d\n", ntStatus
);
457 return STATUS_FAILURE
;
460 pRCB
->bBoolInUse
= true;
467 * Complete function of usb BulkIn irp.
471 * pDevice - Pointer to the adapter
476 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
480 static void s_nsBulkInUsbIoCompleteRead(struct urb
*urb
)
482 PRCB pRCB
= (PRCB
)urb
->context
;
483 struct vnt_private
*pDevice
= pRCB
->pDevice
;
484 unsigned long bytesRead
;
485 int bIndicateReceive
= false;
486 int bReAllocSkb
= false;
489 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsBulkInUsbIoCompleteRead\n");
490 status
= urb
->status
;
491 bytesRead
= urb
->actual_length
;
494 pDevice
->ulBulkInError
++;
495 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"BULK In failed %d\n", status
);
497 pDevice
->scStatistic
.RxFcsErrCnt
++;
499 // if (status == USBD_STATUS_CRC) {
500 // pDevice->ulBulkInContCRCError++;
502 // if (status == STATUS_DEVICE_NOT_CONNECTED )
504 // MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
508 bIndicateReceive
= true;
509 pDevice
->ulBulkInContCRCError
= 0;
510 pDevice
->ulBulkInBytesRead
+= bytesRead
;
512 pDevice
->scStatistic
.RxOkCnt
++;
515 STAvUpdateUSBCounter(&pDevice
->scStatistic
.USB_BulkInStat
, status
);
517 if (bIndicateReceive
) {
518 spin_lock(&pDevice
->lock
);
519 if (RXbBulkInProcessData(pDevice
, pRCB
, bytesRead
) == true)
521 spin_unlock(&pDevice
->lock
);
526 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"RxvFreeNormal %d \n",pDevice
->NumRecvFreeList
);
527 spin_lock(&pDevice
->lock
);
528 RXvFreeRCB(pRCB
, bReAllocSkb
);
529 spin_unlock(&pDevice
->lock
);
537 * Allocates an usb BulkOut irp and calls USBD.
541 * pDevice - Pointer to the adapter
545 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
549 int PIPEnsSendBulkOut(struct vnt_private
*pDevice
, PUSB_SEND_CONTEXT pContext
)
554 pDevice
->bPWBitOn
= false;
557 if (pDevice->pPendingBulkOutContext != NULL) {
558 pDevice->NumContextsQueued++;
559 EnqueueContext(pDevice->FirstTxContextQueue, pDevice->LastTxContextQueue, pContext);
560 status = STATUS_PENDING;
561 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send pending!\n");
566 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"s_nsSendBulkOut\n");
568 if (MP_IS_READY(pDevice
) && (pDevice
->Flags
& fMP_POST_WRITES
)) {
570 pUrb
= pContext
->pUrb
;
571 pDevice
->ulBulkOutPosted
++;
572 // pDevice->pPendingBulkOutContext = pContext;
576 usb_sndbulkpipe(pDevice
->usb
, 3),
577 (void *) &(pContext
->Data
[0]),
579 s_nsBulkOutIoCompleteWrite
,
582 status
= usb_submit_urb(pUrb
, GFP_ATOMIC
);
585 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Submit Tx URB failed %d\n", status
);
586 pContext
->bBoolInUse
= false;
587 return STATUS_FAILURE
;
589 return STATUS_PENDING
;
592 pContext
->bBoolInUse
= false;
593 return STATUS_RESOURCES
;
598 * Description: s_nsBulkOutIoCompleteWrite
599 * 1a) Indicate to the protocol the status of the write.
600 * 1b) Return ownership of the packet to the protocol.
602 * 2) If any more packets are queue for sending, send another packet
604 * If the attempt to send the packet to the driver fails,
605 * return ownership of the packet to the protocol and
606 * try another packet (until one succeeds).
610 * pdoUsbDevObj - pointer to the USB device object which
612 * pIrp - the irp which was completed by the
614 * pContext - the context given to IoSetCompletionRoutine
615 * before calling IoCallDriver on the irp
616 * The pContext is a pointer to the USB device object.
620 * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
621 * (IofCompleteRequest) to stop working on the irp.
625 static void s_nsBulkOutIoCompleteWrite(struct urb
*urb
)
627 struct vnt_private
*pDevice
;
629 CONTEXT_TYPE ContextType
;
630 unsigned long ulBufLen
;
631 PUSB_SEND_CONTEXT pContext
;
633 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsBulkOutIoCompleteWrite\n");
635 // The context given to IoSetCompletionRoutine is an USB_CONTEXT struct
637 pContext
= (PUSB_SEND_CONTEXT
) urb
->context
;
638 ASSERT( NULL
!= pContext
);
640 pDevice
= pContext
->pDevice
;
641 ContextType
= pContext
->Type
;
642 ulBufLen
= pContext
->uBufLen
;
644 if (!netif_device_present(pDevice
->dev
))
648 // Perform various IRP, URB, and buffer 'sanity checks'
651 status
= urb
->status
;
652 //we should have failed, succeeded, or cancelled, but NOT be pending
653 STAvUpdateUSBCounter(&pDevice
->scStatistic
.USB_BulkOutStat
, status
);
655 if(status
== STATUS_SUCCESS
) {
656 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Write %d bytes\n",(int)ulBufLen
);
657 pDevice
->ulBulkOutBytesWrite
+= ulBufLen
;
658 pDevice
->ulBulkOutContCRCError
= 0;
659 pDevice
->nTxDataTimeCout
= 0;
662 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"BULK Out failed %d\n", status
);
663 pDevice
->ulBulkOutError
++;
666 // pDevice->ulCheckForHangCount = 0;
667 // pDevice->pPendingBulkOutContext = NULL;
669 if ( CONTEXT_DATA_PACKET
== ContextType
) {
670 // Indicate to the protocol the status of the sent packet and return
671 // ownership of the packet.
672 if (pContext
->pPacket
!= NULL
) {
673 dev_kfree_skb_irq(pContext
->pPacket
);
674 pContext
->pPacket
= NULL
;
675 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"tx %d bytes\n",(int)ulBufLen
);
678 pDevice
->dev
->trans_start
= jiffies
;
680 if (status
== STATUS_SUCCESS
) {
681 pDevice
->packetsSent
++;
684 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Send USB error! [%08xh]\n", status
);
685 pDevice
->packetsSentDropped
++;
689 if (pDevice
->bLinkPass
== true) {
690 if (netif_queue_stopped(pDevice
->dev
))
691 netif_wake_queue(pDevice
->dev
);
693 pContext
->bBoolInUse
= false;