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
49 /*--------------------- Static Definitions -------------------------*/
52 //endpoint 1: interrupt
53 //endpoint 2: read bulk
54 //endpoint 3: write bulk
57 //#define REQUEST_OUT (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE) // 0x40
58 //#define REQUEST_IN (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE ) //0xc0
59 //static int msglevel =MSG_LEVEL_DEBUG;
60 static int msglevel =MSG_LEVEL_INFO;
63 #define USB_CTL_WAIT 500 //ms
65 #ifndef URB_ASYNC_UNLINK
66 #define URB_ASYNC_UNLINK 0
69 /*--------------------- Static Classes ----------------------------*/
71 /*--------------------- Static Variables --------------------------*/
73 /*--------------------- Static Functions --------------------------*/
76 s_nsInterruptUsbIoCompleteRead(
83 s_nsBulkInUsbIoCompleteRead(
90 s_nsBulkOutIoCompleteWrite(
97 s_nsControlInUsbIoCompleteRead(
103 s_nsControlInUsbIoCompleteWrite(
107 /*--------------------- Export Variables --------------------------*/
109 /*--------------------- Export Functions --------------------------*/
114 PIPEnsControlOutAsyn(
126 if (MP_TEST_FLAG(pDevice, fMP_DISCONNECTED))
127 return STATUS_FAILURE;
130 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_WRITES)) {
131 return STATUS_FAILURE;
134 if (in_interrupt()) {
135 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"in_interrupt return ..byRequest %x\n", byRequest);
136 return STATUS_FAILURE;
139 ntStatus = usb_control_msg(
141 usb_sndctrlpipe(pDevice->usb , 0),
151 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"usb_sndctrlpipe ntStatus= %d\n", ntStatus);
154 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"usb_sndctrlpipe fail, ntStatus= %d\n", ntStatus);
174 NTSTATUS ntStatus = 0;
178 if (MP_TEST_FLAG(pDevice, fMP_DISCONNECTED))
179 return STATUS_FAILURE;
181 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_WRITES)) {
182 return STATUS_FAILURE;
185 pDevice->sUsbCtlRequest.bRequestType = 0x40;
186 pDevice->sUsbCtlRequest.bRequest = byRequest;
187 pDevice->sUsbCtlRequest.wValue = cpu_to_le16p(&wValue);
188 pDevice->sUsbCtlRequest.wIndex = cpu_to_le16p(&wIndex);
189 pDevice->sUsbCtlRequest.wLength = cpu_to_le16p(&wLength);
190 pDevice->pControlURB->transfer_flags |= URB_ASYNC_UNLINK;
191 pDevice->pControlURB->actual_length = 0;
192 // Notice, pbyBuffer limited point to variable buffer, can't be constant.
193 usb_fill_control_urb(pDevice->pControlURB, pDevice->usb,
194 usb_sndctrlpipe(pDevice->usb , 0), (char *) &pDevice->sUsbCtlRequest,
195 pbyBuffer, wLength, s_nsControlInUsbIoCompleteWrite, pDevice);
197 if ((ntStatus = vntwusb_submit_urb(pDevice->pControlURB) != 0)) {
198 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control send request submission failed: %d\n", ntStatus);
199 return STATUS_FAILURE;
202 MP_SET_FLAG(pDevice, fMP_CONTROL_WRITES);
204 spin_unlock_irq(&pDevice->lock);
205 for (ii = 0; ii <= USB_CTL_WAIT; ii ++) {
206 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_WRITES))
210 if (ii >= USB_CTL_WAIT) {
211 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control send request submission timeout \n");
212 spin_lock_irq(&pDevice->lock);
213 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
214 return STATUS_FAILURE;
217 spin_lock_irq(&pDevice->lock);
219 return STATUS_SUCCESS;
232 IN OUT PBYTE pbyBuffer
235 NTSTATUS ntStatus = 0;
238 if (MP_TEST_FLAG(pDevice, fMP_DISCONNECTED))
239 return STATUS_FAILURE;
241 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_READS)) {
242 return STATUS_FAILURE;
244 pDevice->sUsbCtlRequest.bRequestType = 0xC0;
245 pDevice->sUsbCtlRequest.bRequest = byRequest;
246 pDevice->sUsbCtlRequest.wValue = cpu_to_le16p(&wValue);
247 pDevice->sUsbCtlRequest.wIndex = cpu_to_le16p(&wIndex);
248 pDevice->sUsbCtlRequest.wLength = cpu_to_le16p(&wLength);
249 pDevice->pControlURB->transfer_flags |= URB_ASYNC_UNLINK;
250 pDevice->pControlURB->actual_length = 0;
251 usb_fill_control_urb(pDevice->pControlURB, pDevice->usb,
252 usb_rcvctrlpipe(pDevice->usb , 0), (char *) &pDevice->sUsbCtlRequest,
253 pbyBuffer, wLength, s_nsControlInUsbIoCompleteRead, pDevice);
255 if ((ntStatus = vntwusb_submit_urb(pDevice->pControlURB) != 0)) {
256 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control request submission failed: %d\n", ntStatus);
258 MP_SET_FLAG(pDevice, fMP_CONTROL_READS);
261 spin_unlock_irq(&pDevice->lock);
262 for (ii = 0; ii <= USB_CTL_WAIT; ii ++) {
263 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_READS))
268 if (ii >= USB_CTL_WAIT) {
269 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control rcv request submission timeout \n");
270 spin_lock_irq(&pDevice->lock);
271 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
272 return STATUS_FAILURE;
275 spin_lock_irq(&pDevice->lock);
282 s_nsControlInUsbIoCompleteWrite(
288 pDevice = urb->context;
289 switch (urb->status) {
293 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status EINPROGRESS%d\n", urb->status);
296 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status ENOENT %d\n", urb->status);
299 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status %d\n", urb->status);
302 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
309 * Complete function of usb Control callback
313 * pDevice - Pointer to the adapter
318 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
323 s_nsControlInUsbIoCompleteRead(
329 pDevice = urb->context;
330 switch (urb->status) {
334 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status EINPROGRESS%d\n", urb->status);
337 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status = ENOENT %d\n", urb->status);
340 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status %d\n", urb->status);
343 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
351 * Allocates an usb interrupt in irp and calls USBD.
355 * pDevice - Pointer to the adapter
359 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
367 NTSTATUS ntStatus = STATUS_FAILURE;
370 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartInterruptUsbRead()\n");
372 if(pDevice->intBuf.bInUse == TRUE){
373 return (STATUS_FAILURE);
375 pDevice->intBuf.bInUse = TRUE;
376 // pDevice->bEventAvailable = FALSE;
377 pDevice->ulIntInPosted++;
380 // Now that we have created the urb, we will send a
381 // request to the USB device object.
383 #if 0 //reserve int URB submit
384 usb_fill_int_urb(pDevice->pInterruptURB,
386 usb_rcvintpipe(pDevice->usb, 1),
387 (PVOID) pDevice->intBuf.pDataBuf,
389 s_nsInterruptUsbIoCompleteRead,
391 pDevice->int_interval
393 #else //replace int URB submit by bulk transfer
395 usb_fill_int_urb(pDevice->pInterruptURB,
397 usb_rcvintpipe(pDevice->usb, 1),
398 (PVOID) pDevice->intBuf.pDataBuf,
400 s_nsInterruptUsbIoCompleteRead,
402 pDevice->int_interval
406 pDevice->pInterruptURB->interval = pDevice->int_interval;
408 usb_fill_bulk_urb(pDevice->pInterruptURB,
410 usb_rcvbulkpipe(pDevice->usb, 1),
411 (PVOID) pDevice->intBuf.pDataBuf,
413 s_nsInterruptUsbIoCompleteRead,
418 if ((ntStatus = vntwusb_submit_urb(pDevice->pInterruptURB)) != 0) {
419 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
422 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----s_nsStartInterruptUsbRead Return(%x)\n",ntStatus);
429 * Complete function of usb interrupt in irp.
433 * pDevice - Pointer to the adapter
438 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
443 s_nsInterruptUsbIoCompleteRead(
452 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsInterruptUsbIoCompleteRead\n");
454 // The context given to IoSetCompletionRoutine is the receive buffer object
456 pDevice = (PSDevice)urb->context;
459 // We have a number of cases:
460 // 1) The USB read timed out and we received no data.
461 // 2) The USB read timed out and we received some data.
462 // 3) The USB read was successful and fully filled our irp buffer.
463 // 4) The irp was cancelled.
464 // 5) Some other failure from the USB device object.
466 ntStatus = urb->status;
468 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsInterruptUsbIoCompleteRead Status %d\n", ntStatus);
470 // if we were not successful, we need to free the int buffer for future use right here
471 // otherwise interrupt data handler will free int buffer after it handle it.
472 if (( ntStatus != STATUS_SUCCESS )) {
473 pDevice->ulBulkInError++;
474 pDevice->intBuf.bInUse = FALSE;
476 // if (ntStatus == USBD_STATUS_CRC) {
477 // pDevice->ulIntInContCRCError++;
480 // if (ntStatus == STATUS_NOT_CONNECTED )
482 pDevice->fKillEventPollingThread = TRUE;
484 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"IntUSBIoCompleteControl STATUS = %d\n", ntStatus );
487 pDevice->ulIntInBytesRead += (ULONG)urb->actual_length;
488 pDevice->ulIntInContCRCError = 0;
489 pDevice->bEventAvailable = TRUE;
490 INTnsProcessData(pDevice);
493 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_InterruptStat, ntStatus);
496 if (pDevice->fKillEventPollingThread != TRUE) {
497 #if 0 //reserve int URB submit
498 if ((ntStatus = vntwusb_submit_urb(urb)) != 0) {
499 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Re-Submit int URB failed %d\n", ntStatus);
501 #else //replace int URB submit by bulk transfer
503 usb_fill_bulk_urb(pDevice->pInterruptURB,
505 usb_rcvbulkpipe(pDevice->usb, 1),
506 (PVOID) pDevice->intBuf.pDataBuf,
508 s_nsInterruptUsbIoCompleteRead,
511 if ((ntStatus = vntwusb_submit_urb(pDevice->pInterruptURB)) != 0) {
512 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
516 tasklet_schedule(&pDevice->EventWorkItem);
521 // We return STATUS_MORE_PROCESSING_REQUIRED so that the completion
522 // routine (IofCompleteRequest) will stop working on the irp.
529 * Allocates an usb BulkIn irp and calls USBD.
533 * pDevice - Pointer to the adapter
537 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
546 NTSTATUS ntStatus= 0;
550 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartBulkInUsbRead\n");
552 if (MP_TEST_FLAG(pDevice, fMP_DISCONNECTED))
553 return STATUS_FAILURE;
555 pDevice->ulBulkInPosted++;
560 // Now that we have created the urb, we will send a
561 // request to the USB device object.
563 if (pRCB->skb == NULL) {
564 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pRCB->skb is null \n");
568 usb_fill_bulk_urb(pUrb,
570 usb_rcvbulkpipe(pDevice->usb, 2),
571 (PVOID) (pRCB->skb->data),
572 MAX_TOTAL_SIZE_WITH_ALL_HEADERS,
573 s_nsBulkInUsbIoCompleteRead,
576 if((ntStatus = vntwusb_submit_urb(pUrb)!=0)){
577 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Rx URB failed %d\n", ntStatus);
578 return STATUS_FAILURE ;
581 pRCB->bBoolInUse= TRUE;
591 * Complete function of usb BulkIn irp.
595 * pDevice - Pointer to the adapter
600 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
605 s_nsBulkInUsbIoCompleteRead(
610 PRCB pRCB = (PRCB)urb->context;
611 PSDevice pDevice = (PSDevice)pRCB->pDevice;
613 BOOL bIndicateReceive = FALSE;
614 BOOL bReAllocSkb = FALSE;
619 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkInUsbIoCompleteRead\n");
620 status = urb->status;
621 bytesRead = urb->actual_length;
624 pDevice->ulBulkInError++;
625 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK In failed %d\n", status);
627 #ifdef Calcu_LinkQual
628 pDevice->scStatistic.RxFcsErrCnt ++;
631 // if (status == USBD_STATUS_CRC) {
632 // pDevice->ulBulkInContCRCError++;
634 // if (status == STATUS_DEVICE_NOT_CONNECTED )
636 // MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
639 bIndicateReceive = TRUE;
640 pDevice->ulBulkInContCRCError = 0;
641 pDevice->ulBulkInBytesRead += bytesRead;
643 #ifdef Calcu_LinkQual
644 pDevice->scStatistic.RxOkCnt ++;
649 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkInStat, status);
651 if (bIndicateReceive) {
652 spin_lock(&pDevice->lock);
653 if (RXbBulkInProcessData(pDevice, pRCB, bytesRead) == TRUE)
655 spin_unlock(&pDevice->lock);
660 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"RxvFreeNormal %d \n",pDevice->NumRecvFreeList);
661 spin_lock(&pDevice->lock);
662 RXvFreeRCB(pRCB, bReAllocSkb);
663 spin_unlock(&pDevice->lock);
672 * Allocates an usb BulkOut irp and calls USBD.
676 * pDevice - Pointer to the adapter
680 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
686 IN PUSB_SEND_CONTEXT pContext
694 pDevice->bPWBitOn = FALSE;
697 if (pDevice->pPendingBulkOutContext != NULL) {
698 pDevice->NumContextsQueued++;
699 EnqueueContext(pDevice->FirstTxContextQueue, pDevice->LastTxContextQueue, pContext);
700 status = STATUS_PENDING;
701 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send pending!\n");
706 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsSendBulkOut\n");
708 if(MP_IS_READY(pDevice) && MP_TEST_FLAG(pDevice, fMP_POST_WRITES)) {
710 pUrb = pContext->pUrb;
711 pDevice->ulBulkOutPosted++;
712 // pDevice->pPendingBulkOutContext = pContext;
716 usb_sndbulkpipe(pDevice->usb, 3),
717 (PVOID) &(pContext->Data[0]),
719 s_nsBulkOutIoCompleteWrite,
722 if((status = vntwusb_submit_urb(pUrb))!=0)
724 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Tx URB failed %d\n", status);
725 return STATUS_FAILURE;
727 return STATUS_PENDING;
730 pContext->bBoolInUse = FALSE;
731 return STATUS_RESOURCES;
736 * Description: s_nsBulkOutIoCompleteWrite
737 * 1a) Indicate to the protocol the status of the write.
738 * 1b) Return ownership of the packet to the protocol.
740 * 2) If any more packets are queue for sending, send another packet
742 * If the attempt to send the packet to the driver fails,
743 * return ownership of the packet to the protocol and
744 * try another packet (until one succeeds).
748 * pdoUsbDevObj - pointer to the USB device object which
750 * pIrp - the irp which was completed by the
752 * pContext - the context given to IoSetCompletionRoutine
753 * before calling IoCallDriver on the irp
754 * The pContext is a pointer to the USB device object.
758 * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
759 * (IofCompleteRequest) to stop working on the irp.
764 s_nsBulkOutIoCompleteWrite(
770 CONTEXT_TYPE ContextType;
772 PUSB_SEND_CONTEXT pContext;
775 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkOutIoCompleteWrite\n");
777 // The context given to IoSetCompletionRoutine is an USB_CONTEXT struct
779 pContext = (PUSB_SEND_CONTEXT) urb->context;
780 ASSERT( NULL != pContext );
782 pDevice = pContext->pDevice;
783 ContextType = pContext->Type;
784 ulBufLen = pContext->uBufLen;
786 if (!netif_device_present(pDevice->dev))
790 // Perform various IRP, URB, and buffer 'sanity checks'
793 status = urb->status;
794 //we should have failed, succeeded, or cancelled, but NOT be pending
795 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkOutStat, status);
797 if(status == STATUS_SUCCESS) {
798 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Write %d bytes\n",(int)ulBufLen);
799 pDevice->ulBulkOutBytesWrite += ulBufLen;
800 pDevice->ulBulkOutContCRCError = 0;
801 //2007-0115-06<Add>by MikeLiu
803 pDevice->nTxDataTimeCout = 0;
807 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK Out failed %d\n", status);
808 pDevice->ulBulkOutError++;
811 // pDevice->ulCheckForHangCount = 0;
812 // pDevice->pPendingBulkOutContext = NULL;
814 if ( CONTEXT_DATA_PACKET == ContextType ) {
815 // Indicate to the protocol the status of the sent packet and return
816 // ownership of the packet.
817 if (pContext->pPacket != NULL) {
818 dev_kfree_skb_irq(pContext->pPacket);
819 pContext->pPacket = NULL;
820 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"tx %d bytes\n",(int)ulBufLen);
823 pDevice->dev->trans_start = jiffies;
826 if (status == STATUS_SUCCESS) {
827 pDevice->packetsSent++;
830 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send USB error! [%08xh]\n", status);
831 pDevice->packetsSentDropped++;
835 if (pDevice->bLinkPass == TRUE) {
836 if (netif_queue_stopped(pDevice->dev))
837 netif_wake_queue(pDevice->dev);
839 pContext->bBoolInUse = FALSE;