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;
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 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 (PVOID) 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 (PVOID) 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 (PVOID) 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 );
489 pDevice->ulIntInBytesRead += (ULONG)urb->actual_length;
490 pDevice->ulIntInContCRCError = 0;
491 pDevice->bEventAvailable = TRUE;
492 INTnsProcessData(pDevice);
495 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_InterruptStat, ntStatus);
498 if (pDevice->fKillEventPollingThread != TRUE) {
499 #if 0 //reserve int URB submit
500 ntStatus = usb_submit_urb(urb, GFP_ATOMIC);
502 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Re-Submit int URB failed %d\n", ntStatus);
504 #else //replace int URB submit by bulk transfer
506 usb_fill_bulk_urb(pDevice->pInterruptURB,
508 usb_rcvbulkpipe(pDevice->usb, 1),
509 (PVOID) pDevice->intBuf.pDataBuf,
511 s_nsInterruptUsbIoCompleteRead,
514 ntStatus = usb_submit_urb(pDevice->pInterruptURB, GFP_ATOMIC);
516 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
520 tasklet_schedule(&pDevice->EventWorkItem);
525 // We return STATUS_MORE_PROCESSING_REQUIRED so that the completion
526 // routine (IofCompleteRequest) will stop working on the irp.
533 * Allocates an usb BulkIn irp and calls USBD.
537 * pDevice - Pointer to the adapter
541 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
550 NTSTATUS ntStatus= 0;
554 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartBulkInUsbRead\n");
556 if (MP_TEST_FLAG(pDevice, fMP_DISCONNECTED))
557 return STATUS_FAILURE;
559 pDevice->ulBulkInPosted++;
564 // Now that we have created the urb, we will send a
565 // request to the USB device object.
567 if (pRCB->skb == NULL) {
568 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pRCB->skb is null \n");
572 usb_fill_bulk_urb(pUrb,
574 usb_rcvbulkpipe(pDevice->usb, 2),
575 (PVOID) (pRCB->skb->data),
576 MAX_TOTAL_SIZE_WITH_ALL_HEADERS,
577 s_nsBulkInUsbIoCompleteRead,
580 ntStatus = usb_submit_urb(pUrb, GFP_ATOMIC);
582 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Rx URB failed %d\n", ntStatus);
583 return STATUS_FAILURE ;
586 pRCB->bBoolInUse= TRUE;
596 * Complete function of usb BulkIn irp.
600 * pDevice - Pointer to the adapter
605 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
610 s_nsBulkInUsbIoCompleteRead(
615 PRCB pRCB = (PRCB)urb->context;
616 PSDevice pDevice = (PSDevice)pRCB->pDevice;
618 BOOL bIndicateReceive = FALSE;
619 BOOL bReAllocSkb = FALSE;
624 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkInUsbIoCompleteRead\n");
625 status = urb->status;
626 bytesRead = urb->actual_length;
629 pDevice->ulBulkInError++;
630 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK In failed %d\n", status);
632 #ifdef Calcu_LinkQual
633 pDevice->scStatistic.RxFcsErrCnt ++;
636 // if (status == USBD_STATUS_CRC) {
637 // pDevice->ulBulkInContCRCError++;
639 // if (status == STATUS_DEVICE_NOT_CONNECTED )
641 // MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
644 bIndicateReceive = TRUE;
645 pDevice->ulBulkInContCRCError = 0;
646 pDevice->ulBulkInBytesRead += bytesRead;
648 #ifdef Calcu_LinkQual
649 pDevice->scStatistic.RxOkCnt ++;
654 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkInStat, status);
656 if (bIndicateReceive) {
657 spin_lock(&pDevice->lock);
658 if (RXbBulkInProcessData(pDevice, pRCB, bytesRead) == TRUE)
660 spin_unlock(&pDevice->lock);
665 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"RxvFreeNormal %d \n",pDevice->NumRecvFreeList);
666 spin_lock(&pDevice->lock);
667 RXvFreeRCB(pRCB, bReAllocSkb);
668 spin_unlock(&pDevice->lock);
677 * Allocates an usb BulkOut irp and calls USBD.
681 * pDevice - Pointer to the adapter
685 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
691 IN PUSB_SEND_CONTEXT pContext
699 pDevice->bPWBitOn = FALSE;
702 if (pDevice->pPendingBulkOutContext != NULL) {
703 pDevice->NumContextsQueued++;
704 EnqueueContext(pDevice->FirstTxContextQueue, pDevice->LastTxContextQueue, pContext);
705 status = STATUS_PENDING;
706 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send pending!\n");
711 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsSendBulkOut\n");
713 if(MP_IS_READY(pDevice) && MP_TEST_FLAG(pDevice, fMP_POST_WRITES)) {
715 pUrb = pContext->pUrb;
716 pDevice->ulBulkOutPosted++;
717 // pDevice->pPendingBulkOutContext = pContext;
721 usb_sndbulkpipe(pDevice->usb, 3),
722 (PVOID) &(pContext->Data[0]),
724 s_nsBulkOutIoCompleteWrite,
727 status = usb_submit_urb(pUrb, GFP_ATOMIC);
730 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Tx URB failed %d\n", status);
731 return STATUS_FAILURE;
733 return STATUS_PENDING;
736 pContext->bBoolInUse = FALSE;
737 return STATUS_RESOURCES;
742 * Description: s_nsBulkOutIoCompleteWrite
743 * 1a) Indicate to the protocol the status of the write.
744 * 1b) Return ownership of the packet to the protocol.
746 * 2) If any more packets are queue for sending, send another packet
748 * If the attempt to send the packet to the driver fails,
749 * return ownership of the packet to the protocol and
750 * try another packet (until one succeeds).
754 * pdoUsbDevObj - pointer to the USB device object which
756 * pIrp - the irp which was completed by the
758 * pContext - the context given to IoSetCompletionRoutine
759 * before calling IoCallDriver on the irp
760 * The pContext is a pointer to the USB device object.
764 * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
765 * (IofCompleteRequest) to stop working on the irp.
770 s_nsBulkOutIoCompleteWrite(
776 CONTEXT_TYPE ContextType;
778 PUSB_SEND_CONTEXT pContext;
781 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkOutIoCompleteWrite\n");
783 // The context given to IoSetCompletionRoutine is an USB_CONTEXT struct
785 pContext = (PUSB_SEND_CONTEXT) urb->context;
786 ASSERT( NULL != pContext );
788 pDevice = pContext->pDevice;
789 ContextType = pContext->Type;
790 ulBufLen = pContext->uBufLen;
792 if (!netif_device_present(pDevice->dev))
796 // Perform various IRP, URB, and buffer 'sanity checks'
799 status = urb->status;
800 //we should have failed, succeeded, or cancelled, but NOT be pending
801 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkOutStat, status);
803 if(status == STATUS_SUCCESS) {
804 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Write %d bytes\n",(int)ulBufLen);
805 pDevice->ulBulkOutBytesWrite += ulBufLen;
806 pDevice->ulBulkOutContCRCError = 0;
807 //2007-0115-06<Add>by MikeLiu
809 pDevice->nTxDataTimeCout = 0;
813 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK Out failed %d\n", status);
814 pDevice->ulBulkOutError++;
817 // pDevice->ulCheckForHangCount = 0;
818 // pDevice->pPendingBulkOutContext = NULL;
820 if ( CONTEXT_DATA_PACKET == ContextType ) {
821 // Indicate to the protocol the status of the sent packet and return
822 // ownership of the packet.
823 if (pContext->pPacket != NULL) {
824 dev_kfree_skb_irq(pContext->pPacket);
825 pContext->pPacket = NULL;
826 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"tx %d bytes\n",(int)ulBufLen);
829 pDevice->dev->trans_start = jiffies;
832 if (status == STATUS_SUCCESS) {
833 pDevice->packetsSent++;
836 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send USB error! [%08xh]\n", status);
837 pDevice->packetsSentDropped++;
841 if (pDevice->bLinkPass == TRUE) {
842 if (netif_queue_stopped(pDevice->dev))
843 netif_wake_queue(pDevice->dev);
845 pContext->bBoolInUse = FALSE;