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 --------------------------*/
110 int PIPEnsControlOutAsyn(
121 if (MP_TEST_FLAG(pDevice, fMP_DISCONNECTED))
122 return STATUS_FAILURE;
125 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_WRITES)) {
126 return STATUS_FAILURE;
129 if (in_interrupt()) {
130 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"in_interrupt return ..byRequest %x\n", byRequest);
131 return STATUS_FAILURE;
134 ntStatus = usb_control_msg(
136 usb_sndctrlpipe(pDevice->usb , 0),
146 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"usb_sndctrlpipe ntStatus= %d\n", ntStatus);
149 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"usb_sndctrlpipe fail, ntStatus= %d\n", ntStatus);
155 int PIPEnsControlOut(
167 if (MP_TEST_FLAG(pDevice, fMP_DISCONNECTED))
168 return STATUS_FAILURE;
170 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_WRITES)) {
171 return STATUS_FAILURE;
174 pDevice->sUsbCtlRequest.bRequestType = 0x40;
175 pDevice->sUsbCtlRequest.bRequest = byRequest;
176 pDevice->sUsbCtlRequest.wValue = cpu_to_le16p(&wValue);
177 pDevice->sUsbCtlRequest.wIndex = cpu_to_le16p(&wIndex);
178 pDevice->sUsbCtlRequest.wLength = cpu_to_le16p(&wLength);
179 pDevice->pControlURB->transfer_flags |= URB_ASYNC_UNLINK;
180 pDevice->pControlURB->actual_length = 0;
181 // Notice, pbyBuffer limited point to variable buffer, can't be constant.
182 usb_fill_control_urb(pDevice->pControlURB, pDevice->usb,
183 usb_sndctrlpipe(pDevice->usb , 0), (char *) &pDevice->sUsbCtlRequest,
184 pbyBuffer, wLength, s_nsControlInUsbIoCompleteWrite, pDevice);
186 ntStatus = usb_submit_urb(pDevice->pControlURB, GFP_ATOMIC);
188 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control send request submission failed: %d\n", ntStatus);
189 return STATUS_FAILURE;
192 MP_SET_FLAG(pDevice, fMP_CONTROL_WRITES);
194 spin_unlock_irq(&pDevice->lock);
195 for (ii = 0; ii <= USB_CTL_WAIT; ii ++) {
196 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_WRITES))
200 if (ii >= USB_CTL_WAIT) {
201 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control send request submission timeout \n");
202 spin_lock_irq(&pDevice->lock);
203 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
204 return STATUS_FAILURE;
207 spin_lock_irq(&pDevice->lock);
209 return STATUS_SUCCESS;
224 if (MP_TEST_FLAG(pDevice, fMP_DISCONNECTED))
225 return STATUS_FAILURE;
227 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_READS)) {
228 return STATUS_FAILURE;
230 pDevice->sUsbCtlRequest.bRequestType = 0xC0;
231 pDevice->sUsbCtlRequest.bRequest = byRequest;
232 pDevice->sUsbCtlRequest.wValue = cpu_to_le16p(&wValue);
233 pDevice->sUsbCtlRequest.wIndex = cpu_to_le16p(&wIndex);
234 pDevice->sUsbCtlRequest.wLength = cpu_to_le16p(&wLength);
235 pDevice->pControlURB->transfer_flags |= URB_ASYNC_UNLINK;
236 pDevice->pControlURB->actual_length = 0;
237 usb_fill_control_urb(pDevice->pControlURB, pDevice->usb,
238 usb_rcvctrlpipe(pDevice->usb , 0), (char *) &pDevice->sUsbCtlRequest,
239 pbyBuffer, wLength, s_nsControlInUsbIoCompleteRead, pDevice);
241 ntStatus = usb_submit_urb(pDevice->pControlURB, GFP_ATOMIC);
243 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control request submission failed: %d\n", ntStatus);
245 MP_SET_FLAG(pDevice, fMP_CONTROL_READS);
248 spin_unlock_irq(&pDevice->lock);
249 for (ii = 0; ii <= USB_CTL_WAIT; ii ++) {
250 if (MP_TEST_FLAG(pDevice, fMP_CONTROL_READS))
255 if (ii >= USB_CTL_WAIT) {
256 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"control rcv request submission timeout \n");
257 spin_lock_irq(&pDevice->lock);
258 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
259 return STATUS_FAILURE;
262 spin_lock_irq(&pDevice->lock);
269 s_nsControlInUsbIoCompleteWrite(
275 pDevice = urb->context;
276 switch (urb->status) {
280 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status EINPROGRESS%d\n", urb->status);
283 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status ENOENT %d\n", urb->status);
286 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status %d\n", urb->status);
289 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
296 * Complete function of usb Control callback
300 * pDevice - Pointer to the adapter
305 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
310 s_nsControlInUsbIoCompleteRead(
316 pDevice = urb->context;
317 switch (urb->status) {
321 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status EINPROGRESS%d\n", urb->status);
324 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status = ENOENT %d\n", urb->status);
327 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status %d\n", urb->status);
330 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
338 * Allocates an usb interrupt in irp and calls USBD.
342 * pDevice - Pointer to the adapter
346 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
349 int PIPEnsInterruptRead(PSDevice pDevice)
351 int ntStatus = STATUS_FAILURE;
353 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartInterruptUsbRead()\n");
355 if(pDevice->intBuf.bInUse == TRUE){
356 return (STATUS_FAILURE);
358 pDevice->intBuf.bInUse = TRUE;
359 // pDevice->bEventAvailable = FALSE;
360 pDevice->ulIntInPosted++;
363 // Now that we have created the urb, we will send a
364 // request to the USB device object.
366 pDevice->pInterruptURB->interval = pDevice->int_interval;
368 usb_fill_bulk_urb(pDevice->pInterruptURB,
370 usb_rcvbulkpipe(pDevice->usb, 1),
371 (void *) pDevice->intBuf.pDataBuf,
373 s_nsInterruptUsbIoCompleteRead,
376 ntStatus = usb_submit_urb(pDevice->pInterruptURB, GFP_ATOMIC);
378 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
381 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----s_nsStartInterruptUsbRead Return(%x)\n",ntStatus);
388 * Complete function of usb interrupt in irp.
392 * pDevice - Pointer to the adapter
397 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
402 s_nsInterruptUsbIoCompleteRead(
410 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsInterruptUsbIoCompleteRead\n");
412 // The context given to IoSetCompletionRoutine is the receive buffer object
414 pDevice = (PSDevice)urb->context;
417 // We have a number of cases:
418 // 1) The USB read timed out and we received no data.
419 // 2) The USB read timed out and we received some data.
420 // 3) The USB read was successful and fully filled our irp buffer.
421 // 4) The irp was cancelled.
422 // 5) Some other failure from the USB device object.
424 ntStatus = urb->status;
426 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsInterruptUsbIoCompleteRead Status %d\n", ntStatus);
428 // if we were not successful, we need to free the int buffer for future use right here
429 // otherwise interrupt data handler will free int buffer after it handle it.
430 if (( ntStatus != STATUS_SUCCESS )) {
431 pDevice->ulBulkInError++;
432 pDevice->intBuf.bInUse = FALSE;
434 // if (ntStatus == USBD_STATUS_CRC) {
435 // pDevice->ulIntInContCRCError++;
438 // if (ntStatus == STATUS_NOT_CONNECTED )
440 pDevice->fKillEventPollingThread = TRUE;
442 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"IntUSBIoCompleteControl STATUS = %d\n", ntStatus );
444 pDevice->ulIntInBytesRead += (unsigned long) urb->actual_length;
445 pDevice->ulIntInContCRCError = 0;
446 pDevice->bEventAvailable = TRUE;
447 INTnsProcessData(pDevice);
450 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_InterruptStat, ntStatus);
453 if (pDevice->fKillEventPollingThread != TRUE) {
454 usb_fill_bulk_urb(pDevice->pInterruptURB,
456 usb_rcvbulkpipe(pDevice->usb, 1),
457 (void *) pDevice->intBuf.pDataBuf,
459 s_nsInterruptUsbIoCompleteRead,
462 ntStatus = usb_submit_urb(pDevice->pInterruptURB, GFP_ATOMIC);
464 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
468 // We return STATUS_MORE_PROCESSING_REQUIRED so that the completion
469 // routine (IofCompleteRequest) will stop working on the irp.
476 * Allocates an usb BulkIn irp and calls USBD.
480 * pDevice - Pointer to the adapter
484 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
487 int PIPEnsBulkInUsbRead(PSDevice pDevice, PRCB pRCB)
493 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartBulkInUsbRead\n");
495 if (MP_TEST_FLAG(pDevice, fMP_DISCONNECTED))
496 return STATUS_FAILURE;
498 pDevice->ulBulkInPosted++;
503 // Now that we have created the urb, we will send a
504 // request to the USB device object.
506 if (pRCB->skb == NULL) {
507 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pRCB->skb is null \n");
511 usb_fill_bulk_urb(pUrb,
513 usb_rcvbulkpipe(pDevice->usb, 2),
514 (void *) (pRCB->skb->data),
515 MAX_TOTAL_SIZE_WITH_ALL_HEADERS,
516 s_nsBulkInUsbIoCompleteRead,
519 ntStatus = usb_submit_urb(pUrb, GFP_ATOMIC);
521 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Rx URB failed %d\n", ntStatus);
522 return STATUS_FAILURE ;
525 pRCB->bBoolInUse= TRUE;
535 * Complete function of usb BulkIn irp.
539 * pDevice - Pointer to the adapter
544 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
549 s_nsBulkInUsbIoCompleteRead(
554 PRCB pRCB = (PRCB)urb->context;
555 PSDevice pDevice = (PSDevice)pRCB->pDevice;
556 unsigned long bytesRead;
557 BOOL bIndicateReceive = FALSE;
558 BOOL bReAllocSkb = FALSE;
561 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkInUsbIoCompleteRead\n");
562 status = urb->status;
563 bytesRead = urb->actual_length;
566 pDevice->ulBulkInError++;
567 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK In failed %d\n", status);
569 pDevice->scStatistic.RxFcsErrCnt ++;
571 // if (status == USBD_STATUS_CRC) {
572 // pDevice->ulBulkInContCRCError++;
574 // if (status == STATUS_DEVICE_NOT_CONNECTED )
576 // MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
579 bIndicateReceive = TRUE;
580 pDevice->ulBulkInContCRCError = 0;
581 pDevice->ulBulkInBytesRead += bytesRead;
583 pDevice->scStatistic.RxOkCnt ++;
587 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkInStat, status);
589 if (bIndicateReceive) {
590 spin_lock(&pDevice->lock);
591 if (RXbBulkInProcessData(pDevice, pRCB, bytesRead) == TRUE)
593 spin_unlock(&pDevice->lock);
598 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"RxvFreeNormal %d \n",pDevice->NumRecvFreeList);
599 spin_lock(&pDevice->lock);
600 RXvFreeRCB(pRCB, bReAllocSkb);
601 spin_unlock(&pDevice->lock);
610 * Allocates an usb BulkOut irp and calls USBD.
614 * pDevice - Pointer to the adapter
618 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
624 PUSB_SEND_CONTEXT pContext
632 pDevice->bPWBitOn = FALSE;
635 if (pDevice->pPendingBulkOutContext != NULL) {
636 pDevice->NumContextsQueued++;
637 EnqueueContext(pDevice->FirstTxContextQueue, pDevice->LastTxContextQueue, pContext);
638 status = STATUS_PENDING;
639 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send pending!\n");
644 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsSendBulkOut\n");
646 if(MP_IS_READY(pDevice) && MP_TEST_FLAG(pDevice, fMP_POST_WRITES)) {
648 pUrb = pContext->pUrb;
649 pDevice->ulBulkOutPosted++;
650 // pDevice->pPendingBulkOutContext = pContext;
654 usb_sndbulkpipe(pDevice->usb, 3),
655 (void *) &(pContext->Data[0]),
657 s_nsBulkOutIoCompleteWrite,
660 status = usb_submit_urb(pUrb, GFP_ATOMIC);
663 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Tx URB failed %d\n", status);
664 return STATUS_FAILURE;
666 return STATUS_PENDING;
669 pContext->bBoolInUse = FALSE;
670 return STATUS_RESOURCES;
675 * Description: s_nsBulkOutIoCompleteWrite
676 * 1a) Indicate to the protocol the status of the write.
677 * 1b) Return ownership of the packet to the protocol.
679 * 2) If any more packets are queue for sending, send another packet
681 * If the attempt to send the packet to the driver fails,
682 * return ownership of the packet to the protocol and
683 * try another packet (until one succeeds).
687 * pdoUsbDevObj - pointer to the USB device object which
689 * pIrp - the irp which was completed by the
691 * pContext - the context given to IoSetCompletionRoutine
692 * before calling IoCallDriver on the irp
693 * The pContext is a pointer to the USB device object.
697 * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
698 * (IofCompleteRequest) to stop working on the irp.
703 s_nsBulkOutIoCompleteWrite(
709 CONTEXT_TYPE ContextType;
710 unsigned long ulBufLen;
711 PUSB_SEND_CONTEXT pContext;
714 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkOutIoCompleteWrite\n");
716 // The context given to IoSetCompletionRoutine is an USB_CONTEXT struct
718 pContext = (PUSB_SEND_CONTEXT) urb->context;
719 ASSERT( NULL != pContext );
721 pDevice = pContext->pDevice;
722 ContextType = pContext->Type;
723 ulBufLen = pContext->uBufLen;
725 if (!netif_device_present(pDevice->dev))
729 // Perform various IRP, URB, and buffer 'sanity checks'
732 status = urb->status;
733 //we should have failed, succeeded, or cancelled, but NOT be pending
734 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkOutStat, status);
736 if(status == STATUS_SUCCESS) {
737 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Write %d bytes\n",(int)ulBufLen);
738 pDevice->ulBulkOutBytesWrite += ulBufLen;
739 pDevice->ulBulkOutContCRCError = 0;
740 pDevice->nTxDataTimeCout = 0;
743 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK Out failed %d\n", status);
744 pDevice->ulBulkOutError++;
747 // pDevice->ulCheckForHangCount = 0;
748 // pDevice->pPendingBulkOutContext = NULL;
750 if ( CONTEXT_DATA_PACKET == ContextType ) {
751 // Indicate to the protocol the status of the sent packet and return
752 // ownership of the packet.
753 if (pContext->pPacket != NULL) {
754 dev_kfree_skb_irq(pContext->pPacket);
755 pContext->pPacket = NULL;
756 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"tx %d bytes\n",(int)ulBufLen);
759 pDevice->dev->trans_start = jiffies;
762 if (status == STATUS_SUCCESS) {
763 pDevice->packetsSent++;
766 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send USB error! [%08xh]\n", status);
767 pDevice->packetsSentDropped++;
771 if (pDevice->bLinkPass == TRUE) {
772 if (netif_queue_stopped(pDevice->dev))
773 netif_wake_queue(pDevice->dev);
775 pContext->bBoolInUse = FALSE;