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 --------------------------*/
113 PIPEnsControlOutAsyn(
125 if (MP_TEST_FLAG(pDevice, fMP_DISCONNECTED))
126 return STATUS_FAILURE;
129 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_WRITES)) {
130 return STATUS_FAILURE;
133 if (in_interrupt()) {
134 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"in_interrupt return ..byRequest %x\n", byRequest);
135 return STATUS_FAILURE;
138 ntStatus = usb_control_msg(
140 usb_sndctrlpipe(pDevice->usb , 0),
150 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"usb_sndctrlpipe ntStatus= %d\n", ntStatus);
153 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"usb_sndctrlpipe fail, ntStatus= %d\n", ntStatus);
173 NTSTATUS ntStatus = 0;
177 if (MP_TEST_FLAG(pDevice, fMP_DISCONNECTED))
178 return STATUS_FAILURE;
180 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_WRITES)) {
181 return STATUS_FAILURE;
184 pDevice->sUsbCtlRequest.bRequestType = 0x40;
185 pDevice->sUsbCtlRequest.bRequest = byRequest;
186 pDevice->sUsbCtlRequest.wValue = cpu_to_le16p(&wValue);
187 pDevice->sUsbCtlRequest.wIndex = cpu_to_le16p(&wIndex);
188 pDevice->sUsbCtlRequest.wLength = cpu_to_le16p(&wLength);
189 pDevice->pControlURB->transfer_flags |= URB_ASYNC_UNLINK;
190 pDevice->pControlURB->actual_length = 0;
191 // Notice, pbyBuffer limited point to variable buffer, can't be constant.
192 usb_fill_control_urb(pDevice->pControlURB, pDevice->usb,
193 usb_sndctrlpipe(pDevice->usb , 0), (char *) &pDevice->sUsbCtlRequest,
194 pbyBuffer, wLength, s_nsControlInUsbIoCompleteWrite, pDevice);
196 ntStatus = usb_submit_urb(pDevice->pControlURB, GFP_ATOMIC);
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;
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 ntStatus = usb_submit_urb(pDevice->pControlURB, GFP_ATOMIC);
257 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control request submission failed: %d\n", ntStatus);
259 MP_SET_FLAG(pDevice, fMP_CONTROL_READS);
262 spin_unlock_irq(&pDevice->lock);
263 for (ii = 0; ii <= USB_CTL_WAIT; ii ++) {
264 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_READS))
269 if (ii >= USB_CTL_WAIT) {
270 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control rcv request submission timeout \n");
271 spin_lock_irq(&pDevice->lock);
272 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
273 return STATUS_FAILURE;
276 spin_lock_irq(&pDevice->lock);
283 s_nsControlInUsbIoCompleteWrite(
289 pDevice = urb->context;
290 switch (urb->status) {
294 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status EINPROGRESS%d\n", urb->status);
297 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status ENOENT %d\n", urb->status);
300 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status %d\n", urb->status);
303 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
310 * Complete function of usb Control callback
314 * pDevice - Pointer to the adapter
319 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
324 s_nsControlInUsbIoCompleteRead(
330 pDevice = urb->context;
331 switch (urb->status) {
335 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status EINPROGRESS%d\n", urb->status);
338 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status = ENOENT %d\n", urb->status);
341 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status %d\n", urb->status);
344 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
352 * Allocates an usb interrupt in irp and calls USBD.
356 * pDevice - Pointer to the adapter
360 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
368 NTSTATUS ntStatus = STATUS_FAILURE;
371 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartInterruptUsbRead()\n");
373 if(pDevice->intBuf.bInUse == TRUE){
374 return (STATUS_FAILURE);
376 pDevice->intBuf.bInUse = TRUE;
377 // pDevice->bEventAvailable = FALSE;
378 pDevice->ulIntInPosted++;
381 // Now that we have created the urb, we will send a
382 // request to the USB device object.
384 #if 0 //reserve int URB submit
385 usb_fill_int_urb(pDevice->pInterruptURB,
387 usb_rcvintpipe(pDevice->usb, 1),
388 (void *) pDevice->intBuf.pDataBuf,
390 s_nsInterruptUsbIoCompleteRead,
392 pDevice->int_interval
394 #else //replace int URB submit by bulk transfer
396 usb_fill_int_urb(pDevice->pInterruptURB,
398 usb_rcvintpipe(pDevice->usb, 1),
399 (void *) pDevice->intBuf.pDataBuf,
401 s_nsInterruptUsbIoCompleteRead,
403 pDevice->int_interval
407 pDevice->pInterruptURB->interval = pDevice->int_interval;
409 usb_fill_bulk_urb(pDevice->pInterruptURB,
411 usb_rcvbulkpipe(pDevice->usb, 1),
412 (void *) pDevice->intBuf.pDataBuf,
414 s_nsInterruptUsbIoCompleteRead,
419 ntStatus = usb_submit_urb(pDevice->pInterruptURB, GFP_ATOMIC);
421 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
424 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----s_nsStartInterruptUsbRead Return(%x)\n",ntStatus);
431 * Complete function of usb interrupt in irp.
435 * pDevice - Pointer to the adapter
440 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
445 s_nsInterruptUsbIoCompleteRead(
454 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsInterruptUsbIoCompleteRead\n");
456 // The context given to IoSetCompletionRoutine is the receive buffer object
458 pDevice = (PSDevice)urb->context;
461 // We have a number of cases:
462 // 1) The USB read timed out and we received no data.
463 // 2) The USB read timed out and we received some data.
464 // 3) The USB read was successful and fully filled our irp buffer.
465 // 4) The irp was cancelled.
466 // 5) Some other failure from the USB device object.
468 ntStatus = urb->status;
470 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsInterruptUsbIoCompleteRead Status %d\n", ntStatus);
472 // if we were not successful, we need to free the int buffer for future use right here
473 // otherwise interrupt data handler will free int buffer after it handle it.
474 if (( ntStatus != STATUS_SUCCESS )) {
475 pDevice->ulBulkInError++;
476 pDevice->intBuf.bInUse = FALSE;
478 // if (ntStatus == USBD_STATUS_CRC) {
479 // pDevice->ulIntInContCRCError++;
482 // if (ntStatus == STATUS_NOT_CONNECTED )
484 pDevice->fKillEventPollingThread = TRUE;
486 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"IntUSBIoCompleteControl STATUS = %d\n", ntStatus );
488 pDevice->ulIntInBytesRead += (unsigned long) urb->actual_length;
489 pDevice->ulIntInContCRCError = 0;
490 pDevice->bEventAvailable = TRUE;
491 INTnsProcessData(pDevice);
494 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_InterruptStat, ntStatus);
497 if (pDevice->fKillEventPollingThread != TRUE) {
498 #if 0 //reserve int URB submit
499 ntStatus = usb_submit_urb(urb, GFP_ATOMIC);
501 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Re-Submit int URB failed %d\n", ntStatus);
503 #else //replace int URB submit by bulk transfer
505 usb_fill_bulk_urb(pDevice->pInterruptURB,
507 usb_rcvbulkpipe(pDevice->usb, 1),
508 (void *) pDevice->intBuf.pDataBuf,
510 s_nsInterruptUsbIoCompleteRead,
513 ntStatus = usb_submit_urb(pDevice->pInterruptURB, GFP_ATOMIC);
515 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
519 tasklet_schedule(&pDevice->EventWorkItem);
524 // We return STATUS_MORE_PROCESSING_REQUIRED so that the completion
525 // routine (IofCompleteRequest) will stop working on the irp.
532 * Allocates an usb BulkIn irp and calls USBD.
536 * pDevice - Pointer to the adapter
540 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
549 NTSTATUS ntStatus= 0;
553 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartBulkInUsbRead\n");
555 if (MP_TEST_FLAG(pDevice, fMP_DISCONNECTED))
556 return STATUS_FAILURE;
558 pDevice->ulBulkInPosted++;
563 // Now that we have created the urb, we will send a
564 // request to the USB device object.
566 if (pRCB->skb == NULL) {
567 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pRCB->skb is null \n");
571 usb_fill_bulk_urb(pUrb,
573 usb_rcvbulkpipe(pDevice->usb, 2),
574 (void *) (pRCB->skb->data),
575 MAX_TOTAL_SIZE_WITH_ALL_HEADERS,
576 s_nsBulkInUsbIoCompleteRead,
579 ntStatus = usb_submit_urb(pUrb, GFP_ATOMIC);
581 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Rx URB failed %d\n", ntStatus);
582 return STATUS_FAILURE ;
585 pRCB->bBoolInUse= TRUE;
595 * Complete function of usb BulkIn irp.
599 * pDevice - Pointer to the adapter
604 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
609 s_nsBulkInUsbIoCompleteRead(
614 PRCB pRCB = (PRCB)urb->context;
615 PSDevice pDevice = (PSDevice)pRCB->pDevice;
616 unsigned long bytesRead;
617 BOOL bIndicateReceive = FALSE;
618 BOOL bReAllocSkb = FALSE;
623 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkInUsbIoCompleteRead\n");
624 status = urb->status;
625 bytesRead = urb->actual_length;
628 pDevice->ulBulkInError++;
629 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK In failed %d\n", status);
631 #ifdef Calcu_LinkQual
632 pDevice->scStatistic.RxFcsErrCnt ++;
635 // if (status == USBD_STATUS_CRC) {
636 // pDevice->ulBulkInContCRCError++;
638 // if (status == STATUS_DEVICE_NOT_CONNECTED )
640 // MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
643 bIndicateReceive = TRUE;
644 pDevice->ulBulkInContCRCError = 0;
645 pDevice->ulBulkInBytesRead += bytesRead;
647 #ifdef Calcu_LinkQual
648 pDevice->scStatistic.RxOkCnt ++;
653 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkInStat, status);
655 if (bIndicateReceive) {
656 spin_lock(&pDevice->lock);
657 if (RXbBulkInProcessData(pDevice, pRCB, bytesRead) == TRUE)
659 spin_unlock(&pDevice->lock);
664 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"RxvFreeNormal %d \n",pDevice->NumRecvFreeList);
665 spin_lock(&pDevice->lock);
666 RXvFreeRCB(pRCB, bReAllocSkb);
667 spin_unlock(&pDevice->lock);
676 * Allocates an usb BulkOut irp and calls USBD.
680 * pDevice - Pointer to the adapter
684 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
690 PUSB_SEND_CONTEXT pContext
698 pDevice->bPWBitOn = FALSE;
701 if (pDevice->pPendingBulkOutContext != NULL) {
702 pDevice->NumContextsQueued++;
703 EnqueueContext(pDevice->FirstTxContextQueue, pDevice->LastTxContextQueue, pContext);
704 status = STATUS_PENDING;
705 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send pending!\n");
710 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsSendBulkOut\n");
712 if(MP_IS_READY(pDevice) && MP_TEST_FLAG(pDevice, fMP_POST_WRITES)) {
714 pUrb = pContext->pUrb;
715 pDevice->ulBulkOutPosted++;
716 // pDevice->pPendingBulkOutContext = pContext;
720 usb_sndbulkpipe(pDevice->usb, 3),
721 (void *) &(pContext->Data[0]),
723 s_nsBulkOutIoCompleteWrite,
726 status = usb_submit_urb(pUrb, GFP_ATOMIC);
729 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Tx URB failed %d\n", status);
730 return STATUS_FAILURE;
732 return STATUS_PENDING;
735 pContext->bBoolInUse = FALSE;
736 return STATUS_RESOURCES;
741 * Description: s_nsBulkOutIoCompleteWrite
742 * 1a) Indicate to the protocol the status of the write.
743 * 1b) Return ownership of the packet to the protocol.
745 * 2) If any more packets are queue for sending, send another packet
747 * If the attempt to send the packet to the driver fails,
748 * return ownership of the packet to the protocol and
749 * try another packet (until one succeeds).
753 * pdoUsbDevObj - pointer to the USB device object which
755 * pIrp - the irp which was completed by the
757 * pContext - the context given to IoSetCompletionRoutine
758 * before calling IoCallDriver on the irp
759 * The pContext is a pointer to the USB device object.
763 * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
764 * (IofCompleteRequest) to stop working on the irp.
769 s_nsBulkOutIoCompleteWrite(
775 CONTEXT_TYPE ContextType;
776 unsigned long ulBufLen;
777 PUSB_SEND_CONTEXT pContext;
780 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkOutIoCompleteWrite\n");
782 // The context given to IoSetCompletionRoutine is an USB_CONTEXT struct
784 pContext = (PUSB_SEND_CONTEXT) urb->context;
785 ASSERT( NULL != pContext );
787 pDevice = pContext->pDevice;
788 ContextType = pContext->Type;
789 ulBufLen = pContext->uBufLen;
791 if (!netif_device_present(pDevice->dev))
795 // Perform various IRP, URB, and buffer 'sanity checks'
798 status = urb->status;
799 //we should have failed, succeeded, or cancelled, but NOT be pending
800 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkOutStat, status);
802 if(status == STATUS_SUCCESS) {
803 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Write %d bytes\n",(int)ulBufLen);
804 pDevice->ulBulkOutBytesWrite += ulBufLen;
805 pDevice->ulBulkOutContCRCError = 0;
806 //2007-0115-06<Add>by MikeLiu
808 pDevice->nTxDataTimeCout = 0;
812 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK Out failed %d\n", status);
813 pDevice->ulBulkOutError++;
816 // pDevice->ulCheckForHangCount = 0;
817 // pDevice->pPendingBulkOutContext = NULL;
819 if ( CONTEXT_DATA_PACKET == ContextType ) {
820 // Indicate to the protocol the status of the sent packet and return
821 // ownership of the packet.
822 if (pContext->pPacket != NULL) {
823 dev_kfree_skb_irq(pContext->pPacket);
824 pContext->pPacket = NULL;
825 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"tx %d bytes\n",(int)ulBufLen);
828 pDevice->dev->trans_start = jiffies;
831 if (status == STATUS_SUCCESS) {
832 pDevice->packetsSent++;
835 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send USB error! [%08xh]\n", status);
836 pDevice->packetsSentDropped++;
840 if (pDevice->bLinkPass == TRUE) {
841 if (netif_queue_stopped(pDevice->dev))
842 netif_wake_queue(pDevice->dev);
844 pContext->bBoolInUse = FALSE;