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 if ((ntStatus = usb_submit_urb(pDevice->pControlURB, GFP_ATOMIC)) != 0) {
197 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control send request submission failed: %d\n", ntStatus);
198 return STATUS_FAILURE;
201 MP_SET_FLAG(pDevice, fMP_CONTROL_WRITES);
203 spin_unlock_irq(&pDevice->lock);
204 for (ii = 0; ii <= USB_CTL_WAIT; ii ++) {
205 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_WRITES))
209 if (ii >= USB_CTL_WAIT) {
210 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control send request submission timeout \n");
211 spin_lock_irq(&pDevice->lock);
212 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
213 return STATUS_FAILURE;
216 spin_lock_irq(&pDevice->lock);
218 return STATUS_SUCCESS;
231 IN OUT PBYTE pbyBuffer
234 NTSTATUS ntStatus = 0;
237 if (MP_TEST_FLAG(pDevice, fMP_DISCONNECTED))
238 return STATUS_FAILURE;
240 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_READS)) {
241 return STATUS_FAILURE;
243 pDevice->sUsbCtlRequest.bRequestType = 0xC0;
244 pDevice->sUsbCtlRequest.bRequest = byRequest;
245 pDevice->sUsbCtlRequest.wValue = cpu_to_le16p(&wValue);
246 pDevice->sUsbCtlRequest.wIndex = cpu_to_le16p(&wIndex);
247 pDevice->sUsbCtlRequest.wLength = cpu_to_le16p(&wLength);
248 pDevice->pControlURB->transfer_flags |= URB_ASYNC_UNLINK;
249 pDevice->pControlURB->actual_length = 0;
250 usb_fill_control_urb(pDevice->pControlURB, pDevice->usb,
251 usb_rcvctrlpipe(pDevice->usb , 0), (char *) &pDevice->sUsbCtlRequest,
252 pbyBuffer, wLength, s_nsControlInUsbIoCompleteRead, pDevice);
254 if ((ntStatus = usb_submit_urb(pDevice->pControlURB, GFP_ATOMIC)) != 0) {
255 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control request submission failed: %d\n", ntStatus);
257 MP_SET_FLAG(pDevice, fMP_CONTROL_READS);
260 spin_unlock_irq(&pDevice->lock);
261 for (ii = 0; ii <= USB_CTL_WAIT; ii ++) {
262 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_READS))
267 if (ii >= USB_CTL_WAIT) {
268 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control rcv request submission timeout \n");
269 spin_lock_irq(&pDevice->lock);
270 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
271 return STATUS_FAILURE;
274 spin_lock_irq(&pDevice->lock);
281 s_nsControlInUsbIoCompleteWrite(
287 pDevice = urb->context;
288 switch (urb->status) {
292 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status EINPROGRESS%d\n", urb->status);
295 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status ENOENT %d\n", urb->status);
298 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status %d\n", urb->status);
301 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
308 * Complete function of usb Control callback
312 * pDevice - Pointer to the adapter
317 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
322 s_nsControlInUsbIoCompleteRead(
328 pDevice = urb->context;
329 switch (urb->status) {
333 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status EINPROGRESS%d\n", urb->status);
336 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status = ENOENT %d\n", urb->status);
339 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status %d\n", urb->status);
342 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
350 * Allocates an usb interrupt in irp and calls USBD.
354 * pDevice - Pointer to the adapter
358 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
366 NTSTATUS ntStatus = STATUS_FAILURE;
369 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartInterruptUsbRead()\n");
371 if(pDevice->intBuf.bInUse == TRUE){
372 return (STATUS_FAILURE);
374 pDevice->intBuf.bInUse = TRUE;
375 // pDevice->bEventAvailable = FALSE;
376 pDevice->ulIntInPosted++;
379 // Now that we have created the urb, we will send a
380 // request to the USB device object.
382 #if 0 //reserve int URB submit
383 usb_fill_int_urb(pDevice->pInterruptURB,
385 usb_rcvintpipe(pDevice->usb, 1),
386 (PVOID) pDevice->intBuf.pDataBuf,
388 s_nsInterruptUsbIoCompleteRead,
390 pDevice->int_interval
392 #else //replace int URB submit by bulk transfer
394 usb_fill_int_urb(pDevice->pInterruptURB,
396 usb_rcvintpipe(pDevice->usb, 1),
397 (PVOID) pDevice->intBuf.pDataBuf,
399 s_nsInterruptUsbIoCompleteRead,
401 pDevice->int_interval
405 pDevice->pInterruptURB->interval = pDevice->int_interval;
407 usb_fill_bulk_urb(pDevice->pInterruptURB,
409 usb_rcvbulkpipe(pDevice->usb, 1),
410 (PVOID) pDevice->intBuf.pDataBuf,
412 s_nsInterruptUsbIoCompleteRead,
417 if ((ntStatus = usb_submit_urb(pDevice->pInterruptURB, GFP_ATOMIC)) != 0) {
418 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
421 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----s_nsStartInterruptUsbRead Return(%x)\n",ntStatus);
428 * Complete function of usb interrupt in irp.
432 * pDevice - Pointer to the adapter
437 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
442 s_nsInterruptUsbIoCompleteRead(
451 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsInterruptUsbIoCompleteRead\n");
453 // The context given to IoSetCompletionRoutine is the receive buffer object
455 pDevice = (PSDevice)urb->context;
458 // We have a number of cases:
459 // 1) The USB read timed out and we received no data.
460 // 2) The USB read timed out and we received some data.
461 // 3) The USB read was successful and fully filled our irp buffer.
462 // 4) The irp was cancelled.
463 // 5) Some other failure from the USB device object.
465 ntStatus = urb->status;
467 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsInterruptUsbIoCompleteRead Status %d\n", ntStatus);
469 // if we were not successful, we need to free the int buffer for future use right here
470 // otherwise interrupt data handler will free int buffer after it handle it.
471 if (( ntStatus != STATUS_SUCCESS )) {
472 pDevice->ulBulkInError++;
473 pDevice->intBuf.bInUse = FALSE;
475 // if (ntStatus == USBD_STATUS_CRC) {
476 // pDevice->ulIntInContCRCError++;
479 // if (ntStatus == STATUS_NOT_CONNECTED )
481 pDevice->fKillEventPollingThread = TRUE;
483 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"IntUSBIoCompleteControl STATUS = %d\n", ntStatus );
486 pDevice->ulIntInBytesRead += (ULONG)urb->actual_length;
487 pDevice->ulIntInContCRCError = 0;
488 pDevice->bEventAvailable = TRUE;
489 INTnsProcessData(pDevice);
492 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_InterruptStat, ntStatus);
495 if (pDevice->fKillEventPollingThread != TRUE) {
496 #if 0 //reserve int URB submit
497 if ((ntStatus = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
498 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Re-Submit int URB failed %d\n", ntStatus);
500 #else //replace int URB submit by bulk transfer
502 usb_fill_bulk_urb(pDevice->pInterruptURB,
504 usb_rcvbulkpipe(pDevice->usb, 1),
505 (PVOID) pDevice->intBuf.pDataBuf,
507 s_nsInterruptUsbIoCompleteRead,
510 if ((ntStatus = usb_submit_urb(pDevice->pInterruptURB, GFP_ATOMIC)) != 0) {
511 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
515 tasklet_schedule(&pDevice->EventWorkItem);
520 // We return STATUS_MORE_PROCESSING_REQUIRED so that the completion
521 // routine (IofCompleteRequest) will stop working on the irp.
528 * Allocates an usb BulkIn irp and calls USBD.
532 * pDevice - Pointer to the adapter
536 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
545 NTSTATUS ntStatus= 0;
549 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartBulkInUsbRead\n");
551 if (MP_TEST_FLAG(pDevice, fMP_DISCONNECTED))
552 return STATUS_FAILURE;
554 pDevice->ulBulkInPosted++;
559 // Now that we have created the urb, we will send a
560 // request to the USB device object.
562 if (pRCB->skb == NULL) {
563 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pRCB->skb is null \n");
567 usb_fill_bulk_urb(pUrb,
569 usb_rcvbulkpipe(pDevice->usb, 2),
570 (PVOID) (pRCB->skb->data),
571 MAX_TOTAL_SIZE_WITH_ALL_HEADERS,
572 s_nsBulkInUsbIoCompleteRead,
575 if((ntStatus = usb_submit_urb(pUrb, GFP_ATOMIC)) != 0){
576 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Rx URB failed %d\n", ntStatus);
577 return STATUS_FAILURE ;
580 pRCB->bBoolInUse= TRUE;
590 * Complete function of usb BulkIn irp.
594 * pDevice - Pointer to the adapter
599 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
604 s_nsBulkInUsbIoCompleteRead(
609 PRCB pRCB = (PRCB)urb->context;
610 PSDevice pDevice = (PSDevice)pRCB->pDevice;
612 BOOL bIndicateReceive = FALSE;
613 BOOL bReAllocSkb = FALSE;
618 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkInUsbIoCompleteRead\n");
619 status = urb->status;
620 bytesRead = urb->actual_length;
623 pDevice->ulBulkInError++;
624 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK In failed %d\n", status);
626 #ifdef Calcu_LinkQual
627 pDevice->scStatistic.RxFcsErrCnt ++;
630 // if (status == USBD_STATUS_CRC) {
631 // pDevice->ulBulkInContCRCError++;
633 // if (status == STATUS_DEVICE_NOT_CONNECTED )
635 // MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
638 bIndicateReceive = TRUE;
639 pDevice->ulBulkInContCRCError = 0;
640 pDevice->ulBulkInBytesRead += bytesRead;
642 #ifdef Calcu_LinkQual
643 pDevice->scStatistic.RxOkCnt ++;
648 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkInStat, status);
650 if (bIndicateReceive) {
651 spin_lock(&pDevice->lock);
652 if (RXbBulkInProcessData(pDevice, pRCB, bytesRead) == TRUE)
654 spin_unlock(&pDevice->lock);
659 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"RxvFreeNormal %d \n",pDevice->NumRecvFreeList);
660 spin_lock(&pDevice->lock);
661 RXvFreeRCB(pRCB, bReAllocSkb);
662 spin_unlock(&pDevice->lock);
671 * Allocates an usb BulkOut irp and calls USBD.
675 * pDevice - Pointer to the adapter
679 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
685 IN PUSB_SEND_CONTEXT pContext
693 pDevice->bPWBitOn = FALSE;
696 if (pDevice->pPendingBulkOutContext != NULL) {
697 pDevice->NumContextsQueued++;
698 EnqueueContext(pDevice->FirstTxContextQueue, pDevice->LastTxContextQueue, pContext);
699 status = STATUS_PENDING;
700 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send pending!\n");
705 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsSendBulkOut\n");
707 if(MP_IS_READY(pDevice) && MP_TEST_FLAG(pDevice, fMP_POST_WRITES)) {
709 pUrb = pContext->pUrb;
710 pDevice->ulBulkOutPosted++;
711 // pDevice->pPendingBulkOutContext = pContext;
715 usb_sndbulkpipe(pDevice->usb, 3),
716 (PVOID) &(pContext->Data[0]),
718 s_nsBulkOutIoCompleteWrite,
721 if((status = usb_submit_urb(pUrb, GFP_ATOMIC))!=0)
723 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Tx URB failed %d\n", status);
724 return STATUS_FAILURE;
726 return STATUS_PENDING;
729 pContext->bBoolInUse = FALSE;
730 return STATUS_RESOURCES;
735 * Description: s_nsBulkOutIoCompleteWrite
736 * 1a) Indicate to the protocol the status of the write.
737 * 1b) Return ownership of the packet to the protocol.
739 * 2) If any more packets are queue for sending, send another packet
741 * If the attempt to send the packet to the driver fails,
742 * return ownership of the packet to the protocol and
743 * try another packet (until one succeeds).
747 * pdoUsbDevObj - pointer to the USB device object which
749 * pIrp - the irp which was completed by the
751 * pContext - the context given to IoSetCompletionRoutine
752 * before calling IoCallDriver on the irp
753 * The pContext is a pointer to the USB device object.
757 * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
758 * (IofCompleteRequest) to stop working on the irp.
763 s_nsBulkOutIoCompleteWrite(
769 CONTEXT_TYPE ContextType;
771 PUSB_SEND_CONTEXT pContext;
774 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkOutIoCompleteWrite\n");
776 // The context given to IoSetCompletionRoutine is an USB_CONTEXT struct
778 pContext = (PUSB_SEND_CONTEXT) urb->context;
779 ASSERT( NULL != pContext );
781 pDevice = pContext->pDevice;
782 ContextType = pContext->Type;
783 ulBufLen = pContext->uBufLen;
785 if (!netif_device_present(pDevice->dev))
789 // Perform various IRP, URB, and buffer 'sanity checks'
792 status = urb->status;
793 //we should have failed, succeeded, or cancelled, but NOT be pending
794 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkOutStat, status);
796 if(status == STATUS_SUCCESS) {
797 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Write %d bytes\n",(int)ulBufLen);
798 pDevice->ulBulkOutBytesWrite += ulBufLen;
799 pDevice->ulBulkOutContCRCError = 0;
800 //2007-0115-06<Add>by MikeLiu
802 pDevice->nTxDataTimeCout = 0;
806 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK Out failed %d\n", status);
807 pDevice->ulBulkOutError++;
810 // pDevice->ulCheckForHangCount = 0;
811 // pDevice->pPendingBulkOutContext = NULL;
813 if ( CONTEXT_DATA_PACKET == ContextType ) {
814 // Indicate to the protocol the status of the sent packet and return
815 // ownership of the packet.
816 if (pContext->pPacket != NULL) {
817 dev_kfree_skb_irq(pContext->pPacket);
818 pContext->pPacket = NULL;
819 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"tx %d bytes\n",(int)ulBufLen);
822 pDevice->dev->trans_start = jiffies;
825 if (status == STATUS_SUCCESS) {
826 pDevice->packetsSent++;
829 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send USB error! [%08xh]\n", status);
830 pDevice->packetsSentDropped++;
834 if (pDevice->bLinkPass == TRUE) {
835 if (netif_queue_stopped(pDevice->dev))
836 netif_wake_queue(pDevice->dev);
838 pContext->bBoolInUse = FALSE;