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
55 //static int msglevel =MSG_LEVEL_DEBUG;
56 static int msglevel =MSG_LEVEL_INFO;
59 #define USB_CTL_WAIT 500 //ms
61 #ifndef URB_ASYNC_UNLINK
62 #define URB_ASYNC_UNLINK 0
65 /*--------------------- Static Classes ----------------------------*/
67 /*--------------------- Static Variables --------------------------*/
69 /*--------------------- Static Functions --------------------------*/
72 s_nsInterruptUsbIoCompleteRead(
79 s_nsBulkInUsbIoCompleteRead(
86 s_nsBulkOutIoCompleteWrite(
93 s_nsControlInUsbIoCompleteRead(
99 s_nsControlInUsbIoCompleteWrite(
103 /*--------------------- Export Variables --------------------------*/
105 /*--------------------- Export Functions --------------------------*/
107 int PIPEnsControlOutAsyn(
118 if (pDevice->Flags & fMP_DISCONNECTED)
119 return STATUS_FAILURE;
121 if (pDevice->Flags & fMP_CONTROL_WRITES)
122 return STATUS_FAILURE;
124 if (in_interrupt()) {
125 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"in_interrupt return ..byRequest %x\n", byRequest);
126 return STATUS_FAILURE;
129 ntStatus = usb_control_msg(
131 usb_sndctrlpipe(pDevice->usb , 0),
141 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"usb_sndctrlpipe ntStatus= %d\n", ntStatus);
144 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"usb_sndctrlpipe fail, ntStatus= %d\n", ntStatus);
150 int PIPEnsControlOut(
162 if (pDevice->Flags & fMP_DISCONNECTED)
163 return STATUS_FAILURE;
165 if (pDevice->Flags & fMP_CONTROL_WRITES)
166 return STATUS_FAILURE;
168 pDevice->sUsbCtlRequest.bRequestType = 0x40;
169 pDevice->sUsbCtlRequest.bRequest = byRequest;
170 pDevice->sUsbCtlRequest.wValue = cpu_to_le16p(&wValue);
171 pDevice->sUsbCtlRequest.wIndex = cpu_to_le16p(&wIndex);
172 pDevice->sUsbCtlRequest.wLength = cpu_to_le16p(&wLength);
173 pDevice->pControlURB->transfer_flags |= URB_ASYNC_UNLINK;
174 pDevice->pControlURB->actual_length = 0;
175 // Notice, pbyBuffer limited point to variable buffer, can't be constant.
176 usb_fill_control_urb(pDevice->pControlURB, pDevice->usb,
177 usb_sndctrlpipe(pDevice->usb , 0), (char *) &pDevice->sUsbCtlRequest,
178 pbyBuffer, wLength, s_nsControlInUsbIoCompleteWrite, pDevice);
180 ntStatus = usb_submit_urb(pDevice->pControlURB, GFP_ATOMIC);
182 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control send request submission failed: %d\n", ntStatus);
183 return STATUS_FAILURE;
186 MP_SET_FLAG(pDevice, fMP_CONTROL_WRITES);
188 spin_unlock_irq(&pDevice->lock);
189 for (ii = 0; ii <= USB_CTL_WAIT; ii ++) {
191 if (pDevice->Flags & fMP_CONTROL_WRITES)
196 if (ii >= USB_CTL_WAIT) {
197 DBG_PRT(MSG_LEVEL_DEBUG,
198 KERN_INFO "control send request submission timeout\n");
199 spin_lock_irq(&pDevice->lock);
200 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
201 return STATUS_FAILURE;
204 spin_lock_irq(&pDevice->lock);
206 return STATUS_SUCCESS;
221 if (pDevice->Flags & fMP_DISCONNECTED)
222 return STATUS_FAILURE;
224 if (pDevice->Flags & fMP_CONTROL_READS)
225 return STATUS_FAILURE;
227 pDevice->sUsbCtlRequest.bRequestType = 0xC0;
228 pDevice->sUsbCtlRequest.bRequest = byRequest;
229 pDevice->sUsbCtlRequest.wValue = cpu_to_le16p(&wValue);
230 pDevice->sUsbCtlRequest.wIndex = cpu_to_le16p(&wIndex);
231 pDevice->sUsbCtlRequest.wLength = cpu_to_le16p(&wLength);
232 pDevice->pControlURB->transfer_flags |= URB_ASYNC_UNLINK;
233 pDevice->pControlURB->actual_length = 0;
234 usb_fill_control_urb(pDevice->pControlURB, pDevice->usb,
235 usb_rcvctrlpipe(pDevice->usb , 0), (char *) &pDevice->sUsbCtlRequest,
236 pbyBuffer, wLength, s_nsControlInUsbIoCompleteRead, pDevice);
238 ntStatus = usb_submit_urb(pDevice->pControlURB, GFP_ATOMIC);
240 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control request submission failed: %d\n", ntStatus);
242 MP_SET_FLAG(pDevice, fMP_CONTROL_READS);
245 spin_unlock_irq(&pDevice->lock);
246 for (ii = 0; ii <= USB_CTL_WAIT; ii ++) {
248 if (pDevice->Flags & fMP_CONTROL_READS)
253 if (ii >= USB_CTL_WAIT) {
254 DBG_PRT(MSG_LEVEL_DEBUG,
255 KERN_INFO "control rcv request submission timeout\n");
256 spin_lock_irq(&pDevice->lock);
257 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
258 return STATUS_FAILURE;
261 spin_lock_irq(&pDevice->lock);
268 s_nsControlInUsbIoCompleteWrite(
274 pDevice = urb->context;
275 switch (urb->status) {
279 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status EINPROGRESS%d\n", urb->status);
282 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status ENOENT %d\n", urb->status);
285 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status %d\n", urb->status);
288 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
295 * Complete function of usb Control callback
299 * pDevice - Pointer to the adapter
304 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
309 s_nsControlInUsbIoCompleteRead(
315 pDevice = urb->context;
316 switch (urb->status) {
320 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status EINPROGRESS%d\n", urb->status);
323 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status = ENOENT %d\n", urb->status);
326 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status %d\n", urb->status);
329 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
337 * Allocates an usb interrupt in irp and calls USBD.
341 * pDevice - Pointer to the adapter
345 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
348 int PIPEnsInterruptRead(PSDevice pDevice)
350 int ntStatus = STATUS_FAILURE;
352 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartInterruptUsbRead()\n");
354 if(pDevice->intBuf.bInUse == TRUE){
355 return (STATUS_FAILURE);
357 pDevice->intBuf.bInUse = TRUE;
358 // pDevice->bEventAvailable = FALSE;
359 pDevice->ulIntInPosted++;
362 // Now that we have created the urb, we will send a
363 // request to the USB device object.
365 pDevice->pInterruptURB->interval = pDevice->int_interval;
367 usb_fill_bulk_urb(pDevice->pInterruptURB,
369 usb_rcvbulkpipe(pDevice->usb, 1),
370 (void *) pDevice->intBuf.pDataBuf,
372 s_nsInterruptUsbIoCompleteRead,
375 ntStatus = usb_submit_urb(pDevice->pInterruptURB, GFP_ATOMIC);
377 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
380 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----s_nsStartInterruptUsbRead Return(%x)\n",ntStatus);
387 * Complete function of usb interrupt in irp.
391 * pDevice - Pointer to the adapter
396 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
401 s_nsInterruptUsbIoCompleteRead(
409 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsInterruptUsbIoCompleteRead\n");
411 // The context given to IoSetCompletionRoutine is the receive buffer object
413 pDevice = (PSDevice)urb->context;
416 // We have a number of cases:
417 // 1) The USB read timed out and we received no data.
418 // 2) The USB read timed out and we received some data.
419 // 3) The USB read was successful and fully filled our irp buffer.
420 // 4) The irp was cancelled.
421 // 5) Some other failure from the USB device object.
423 ntStatus = urb->status;
425 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsInterruptUsbIoCompleteRead Status %d\n", ntStatus);
427 // if we were not successful, we need to free the int buffer for future use right here
428 // otherwise interrupt data handler will free int buffer after it handle it.
429 if (( ntStatus != STATUS_SUCCESS )) {
430 pDevice->ulBulkInError++;
431 pDevice->intBuf.bInUse = FALSE;
433 // if (ntStatus == USBD_STATUS_CRC) {
434 // pDevice->ulIntInContCRCError++;
437 // if (ntStatus == STATUS_NOT_CONNECTED )
439 pDevice->fKillEventPollingThread = TRUE;
441 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"IntUSBIoCompleteControl STATUS = %d\n", ntStatus );
443 pDevice->ulIntInBytesRead += (unsigned long) urb->actual_length;
444 pDevice->ulIntInContCRCError = 0;
445 pDevice->bEventAvailable = TRUE;
446 INTnsProcessData(pDevice);
449 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_InterruptStat, ntStatus);
452 if (pDevice->fKillEventPollingThread != TRUE) {
453 usb_fill_bulk_urb(pDevice->pInterruptURB,
455 usb_rcvbulkpipe(pDevice->usb, 1),
456 (void *) pDevice->intBuf.pDataBuf,
458 s_nsInterruptUsbIoCompleteRead,
461 ntStatus = usb_submit_urb(pDevice->pInterruptURB, GFP_ATOMIC);
463 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
467 // We return STATUS_MORE_PROCESSING_REQUIRED so that the completion
468 // routine (IofCompleteRequest) will stop working on the irp.
475 * Allocates an usb BulkIn irp and calls USBD.
479 * pDevice - Pointer to the adapter
483 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
486 int PIPEnsBulkInUsbRead(PSDevice pDevice, PRCB pRCB)
492 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartBulkInUsbRead\n");
494 if (pDevice->Flags & fMP_DISCONNECTED)
495 return STATUS_FAILURE;
497 pDevice->ulBulkInPosted++;
502 // Now that we have created the urb, we will send a
503 // request to the USB device object.
505 if (pRCB->skb == NULL) {
506 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pRCB->skb is null \n");
510 usb_fill_bulk_urb(pUrb,
512 usb_rcvbulkpipe(pDevice->usb, 2),
513 (void *) (pRCB->skb->data),
514 MAX_TOTAL_SIZE_WITH_ALL_HEADERS,
515 s_nsBulkInUsbIoCompleteRead,
518 ntStatus = usb_submit_urb(pUrb, GFP_ATOMIC);
520 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Rx URB failed %d\n", ntStatus);
521 return STATUS_FAILURE ;
524 pRCB->bBoolInUse= TRUE;
534 * Complete function of usb BulkIn irp.
538 * pDevice - Pointer to the adapter
543 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
548 s_nsBulkInUsbIoCompleteRead(
553 PRCB pRCB = (PRCB)urb->context;
554 PSDevice pDevice = (PSDevice)pRCB->pDevice;
555 unsigned long bytesRead;
556 BOOL bIndicateReceive = FALSE;
557 BOOL bReAllocSkb = FALSE;
560 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkInUsbIoCompleteRead\n");
561 status = urb->status;
562 bytesRead = urb->actual_length;
565 pDevice->ulBulkInError++;
566 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK In failed %d\n", status);
568 pDevice->scStatistic.RxFcsErrCnt ++;
570 // if (status == USBD_STATUS_CRC) {
571 // pDevice->ulBulkInContCRCError++;
573 // if (status == STATUS_DEVICE_NOT_CONNECTED )
575 // MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
578 bIndicateReceive = TRUE;
579 pDevice->ulBulkInContCRCError = 0;
580 pDevice->ulBulkInBytesRead += bytesRead;
582 pDevice->scStatistic.RxOkCnt ++;
586 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkInStat, status);
588 if (bIndicateReceive) {
589 spin_lock(&pDevice->lock);
590 if (RXbBulkInProcessData(pDevice, pRCB, bytesRead) == TRUE)
592 spin_unlock(&pDevice->lock);
597 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"RxvFreeNormal %d \n",pDevice->NumRecvFreeList);
598 spin_lock(&pDevice->lock);
599 RXvFreeRCB(pRCB, bReAllocSkb);
600 spin_unlock(&pDevice->lock);
609 * Allocates an usb BulkOut irp and calls USBD.
613 * pDevice - Pointer to the adapter
617 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
623 PUSB_SEND_CONTEXT pContext
631 pDevice->bPWBitOn = FALSE;
634 if (pDevice->pPendingBulkOutContext != NULL) {
635 pDevice->NumContextsQueued++;
636 EnqueueContext(pDevice->FirstTxContextQueue, pDevice->LastTxContextQueue, pContext);
637 status = STATUS_PENDING;
638 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send pending!\n");
643 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsSendBulkOut\n");
645 if (MP_IS_READY(pDevice) && (pDevice->Flags & fMP_POST_WRITES)) {
647 pUrb = pContext->pUrb;
648 pDevice->ulBulkOutPosted++;
649 // pDevice->pPendingBulkOutContext = pContext;
653 usb_sndbulkpipe(pDevice->usb, 3),
654 (void *) &(pContext->Data[0]),
656 s_nsBulkOutIoCompleteWrite,
659 status = usb_submit_urb(pUrb, GFP_ATOMIC);
662 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Tx URB failed %d\n", status);
663 return STATUS_FAILURE;
665 return STATUS_PENDING;
668 pContext->bBoolInUse = FALSE;
669 return STATUS_RESOURCES;
674 * Description: s_nsBulkOutIoCompleteWrite
675 * 1a) Indicate to the protocol the status of the write.
676 * 1b) Return ownership of the packet to the protocol.
678 * 2) If any more packets are queue for sending, send another packet
680 * If the attempt to send the packet to the driver fails,
681 * return ownership of the packet to the protocol and
682 * try another packet (until one succeeds).
686 * pdoUsbDevObj - pointer to the USB device object which
688 * pIrp - the irp which was completed by the
690 * pContext - the context given to IoSetCompletionRoutine
691 * before calling IoCallDriver on the irp
692 * The pContext is a pointer to the USB device object.
696 * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
697 * (IofCompleteRequest) to stop working on the irp.
702 s_nsBulkOutIoCompleteWrite(
708 CONTEXT_TYPE ContextType;
709 unsigned long ulBufLen;
710 PUSB_SEND_CONTEXT pContext;
713 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkOutIoCompleteWrite\n");
715 // The context given to IoSetCompletionRoutine is an USB_CONTEXT struct
717 pContext = (PUSB_SEND_CONTEXT) urb->context;
718 ASSERT( NULL != pContext );
720 pDevice = pContext->pDevice;
721 ContextType = pContext->Type;
722 ulBufLen = pContext->uBufLen;
724 if (!netif_device_present(pDevice->dev))
728 // Perform various IRP, URB, and buffer 'sanity checks'
731 status = urb->status;
732 //we should have failed, succeeded, or cancelled, but NOT be pending
733 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkOutStat, status);
735 if(status == STATUS_SUCCESS) {
736 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Write %d bytes\n",(int)ulBufLen);
737 pDevice->ulBulkOutBytesWrite += ulBufLen;
738 pDevice->ulBulkOutContCRCError = 0;
739 pDevice->nTxDataTimeCout = 0;
742 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK Out failed %d\n", status);
743 pDevice->ulBulkOutError++;
746 // pDevice->ulCheckForHangCount = 0;
747 // pDevice->pPendingBulkOutContext = NULL;
749 if ( CONTEXT_DATA_PACKET == ContextType ) {
750 // Indicate to the protocol the status of the sent packet and return
751 // ownership of the packet.
752 if (pContext->pPacket != NULL) {
753 dev_kfree_skb_irq(pContext->pPacket);
754 pContext->pPacket = NULL;
755 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"tx %d bytes\n",(int)ulBufLen);
758 pDevice->dev->trans_start = jiffies;
761 if (status == STATUS_SUCCESS) {
762 pDevice->packetsSent++;
765 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send USB error! [%08xh]\n", status);
766 pDevice->packetsSentDropped++;
770 if (pDevice->bLinkPass == TRUE) {
771 if (netif_queue_stopped(pDevice->dev))
772 netif_wake_queue(pDevice->dev);
774 pContext->bBoolInUse = FALSE;