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
50 //endpoint 1: interrupt
51 //endpoint 2: read bulk
52 //endpoint 3: write bulk
54 //static int msglevel =MSG_LEVEL_DEBUG;
55 static int msglevel =MSG_LEVEL_INFO;
58 #define USB_CTL_WAIT 500 //ms
60 #ifndef URB_ASYNC_UNLINK
61 #define URB_ASYNC_UNLINK 0
66 static void s_nsInterruptUsbIoCompleteRead(struct urb *urb);
67 static void s_nsBulkInUsbIoCompleteRead(struct urb *urb);
68 static void s_nsBulkOutIoCompleteWrite(struct urb *urb);
69 static void s_nsControlInUsbIoCompleteRead(struct urb *urb);
70 static void s_nsControlInUsbIoCompleteWrite(struct urb *urb);
74 int PIPEnsControlOutAsyn(struct vnt_private *pDevice, u8 byRequest,
75 u16 wValue, u16 wIndex, u16 wLength, u8 *pbyBuffer)
79 if (pDevice->Flags & fMP_DISCONNECTED)
80 return STATUS_FAILURE;
82 if (pDevice->Flags & fMP_CONTROL_WRITES)
83 return STATUS_FAILURE;
86 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"in_interrupt return ..byRequest %x\n", byRequest);
87 return STATUS_FAILURE;
90 ntStatus = usb_control_msg(
92 usb_sndctrlpipe(pDevice->usb , 0),
102 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"usb_sndctrlpipe ntStatus= %d\n", ntStatus);
105 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"usb_sndctrlpipe fail, ntStatus= %d\n", ntStatus);
111 int PIPEnsControlOut(struct vnt_private *pDevice, u8 byRequest, u16 wValue,
112 u16 wIndex, u16 wLength, u8 *pbyBuffer)
117 if (pDevice->Flags & fMP_DISCONNECTED)
118 return STATUS_FAILURE;
120 if (pDevice->Flags & fMP_CONTROL_WRITES)
121 return STATUS_FAILURE;
123 if (pDevice->Flags & fMP_CONTROL_READS)
124 return STATUS_FAILURE;
126 MP_SET_FLAG(pDevice, fMP_CONTROL_WRITES);
128 pDevice->sUsbCtlRequest.bRequestType = 0x40;
129 pDevice->sUsbCtlRequest.bRequest = byRequest;
130 pDevice->sUsbCtlRequest.wValue = cpu_to_le16p(&wValue);
131 pDevice->sUsbCtlRequest.wIndex = cpu_to_le16p(&wIndex);
132 pDevice->sUsbCtlRequest.wLength = cpu_to_le16p(&wLength);
133 pDevice->pControlURB->transfer_flags |= URB_ASYNC_UNLINK;
134 pDevice->pControlURB->actual_length = 0;
135 // Notice, pbyBuffer limited point to variable buffer, can't be constant.
136 usb_fill_control_urb(pDevice->pControlURB, pDevice->usb,
137 usb_sndctrlpipe(pDevice->usb , 0), (char *) &pDevice->sUsbCtlRequest,
138 pbyBuffer, wLength, s_nsControlInUsbIoCompleteWrite, pDevice);
140 ntStatus = usb_submit_urb(pDevice->pControlURB, GFP_ATOMIC);
142 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
143 "control send request submission failed: %d\n",
145 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
146 return STATUS_FAILURE;
149 spin_unlock_irq(&pDevice->lock);
150 for (ii = 0; ii <= USB_CTL_WAIT; ii ++) {
152 if (pDevice->Flags & fMP_CONTROL_WRITES)
157 if (ii >= USB_CTL_WAIT) {
158 DBG_PRT(MSG_LEVEL_DEBUG,
159 KERN_INFO "control send request submission timeout\n");
160 spin_lock_irq(&pDevice->lock);
161 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
162 return STATUS_FAILURE;
165 spin_lock_irq(&pDevice->lock);
167 return STATUS_SUCCESS;
170 int PIPEnsControlIn(struct vnt_private *pDevice, u8 byRequest, u16 wValue,
171 u16 wIndex, u16 wLength, u8 *pbyBuffer)
176 if (pDevice->Flags & fMP_DISCONNECTED)
177 return STATUS_FAILURE;
179 if (pDevice->Flags & fMP_CONTROL_READS)
180 return STATUS_FAILURE;
182 if (pDevice->Flags & fMP_CONTROL_WRITES)
183 return STATUS_FAILURE;
185 MP_SET_FLAG(pDevice, fMP_CONTROL_READS);
187 pDevice->sUsbCtlRequest.bRequestType = 0xC0;
188 pDevice->sUsbCtlRequest.bRequest = byRequest;
189 pDevice->sUsbCtlRequest.wValue = cpu_to_le16p(&wValue);
190 pDevice->sUsbCtlRequest.wIndex = cpu_to_le16p(&wIndex);
191 pDevice->sUsbCtlRequest.wLength = cpu_to_le16p(&wLength);
192 pDevice->pControlURB->transfer_flags |= URB_ASYNC_UNLINK;
193 pDevice->pControlURB->actual_length = 0;
194 usb_fill_control_urb(pDevice->pControlURB, pDevice->usb,
195 usb_rcvctrlpipe(pDevice->usb , 0), (char *) &pDevice->sUsbCtlRequest,
196 pbyBuffer, wLength, s_nsControlInUsbIoCompleteRead, pDevice);
198 ntStatus = usb_submit_urb(pDevice->pControlURB, GFP_ATOMIC);
200 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
201 "control request submission failed: %d\n", ntStatus);
202 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
203 return STATUS_FAILURE;
206 spin_unlock_irq(&pDevice->lock);
207 for (ii = 0; ii <= USB_CTL_WAIT; ii ++) {
209 if (pDevice->Flags & fMP_CONTROL_READS)
214 if (ii >= USB_CTL_WAIT) {
215 DBG_PRT(MSG_LEVEL_DEBUG,
216 KERN_INFO "control rcv request submission timeout\n");
217 spin_lock_irq(&pDevice->lock);
218 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
219 return STATUS_FAILURE;
222 spin_lock_irq(&pDevice->lock);
227 static void s_nsControlInUsbIoCompleteWrite(struct urb *urb)
229 struct vnt_private *pDevice = (struct vnt_private *)urb->context;
231 pDevice = urb->context;
232 switch (urb->status) {
236 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status EINPROGRESS%d\n", urb->status);
239 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status ENOENT %d\n", urb->status);
242 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl write urb status %d\n", urb->status);
245 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
252 * Complete function of usb Control callback
256 * pDevice - Pointer to the adapter
261 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
265 static void s_nsControlInUsbIoCompleteRead(struct urb *urb)
267 struct vnt_private *pDevice = (struct vnt_private *)urb->context;
269 switch (urb->status) {
273 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status EINPROGRESS%d\n", urb->status);
276 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status = ENOENT %d\n", urb->status);
279 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ctrl read urb status %d\n", urb->status);
282 MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
290 * Allocates an usb interrupt in irp and calls USBD.
294 * pDevice - Pointer to the adapter
298 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
302 int PIPEnsInterruptRead(struct vnt_private *pDevice)
304 int ntStatus = STATUS_FAILURE;
306 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartInterruptUsbRead()\n");
308 if(pDevice->intBuf.bInUse == true){
309 return (STATUS_FAILURE);
311 pDevice->intBuf.bInUse = true;
312 // pDevice->bEventAvailable = false;
313 pDevice->ulIntInPosted++;
316 // Now that we have created the urb, we will send a
317 // request to the USB device object.
319 pDevice->pInterruptURB->interval = pDevice->int_interval;
321 usb_fill_bulk_urb(pDevice->pInterruptURB,
323 usb_rcvbulkpipe(pDevice->usb, 1),
324 (void *) pDevice->intBuf.pDataBuf,
326 s_nsInterruptUsbIoCompleteRead,
329 ntStatus = usb_submit_urb(pDevice->pInterruptURB, GFP_ATOMIC);
331 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
334 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----s_nsStartInterruptUsbRead Return(%x)\n",ntStatus);
341 * Complete function of usb interrupt in irp.
345 * pDevice - Pointer to the adapter
350 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
354 static void s_nsInterruptUsbIoCompleteRead(struct urb *urb)
356 struct vnt_private *pDevice = (struct vnt_private *)urb->context;
359 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsInterruptUsbIoCompleteRead\n");
361 // The context given to IoSetCompletionRoutine is the receive buffer object
365 // We have a number of cases:
366 // 1) The USB read timed out and we received no data.
367 // 2) The USB read timed out and we received some data.
368 // 3) The USB read was successful and fully filled our irp buffer.
369 // 4) The irp was cancelled.
370 // 5) Some other failure from the USB device object.
372 ntStatus = urb->status;
374 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsInterruptUsbIoCompleteRead Status %d\n", ntStatus);
376 // if we were not successful, we need to free the int buffer for future use right here
377 // otherwise interrupt data handler will free int buffer after it handle it.
378 if (( ntStatus != STATUS_SUCCESS )) {
379 pDevice->ulBulkInError++;
380 pDevice->intBuf.bInUse = false;
382 // if (ntStatus == USBD_STATUS_CRC) {
383 // pDevice->ulIntInContCRCError++;
386 // if (ntStatus == STATUS_NOT_CONNECTED )
388 pDevice->fKillEventPollingThread = true;
390 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"IntUSBIoCompleteControl STATUS = %d\n", ntStatus );
392 pDevice->ulIntInBytesRead += (unsigned long) urb->actual_length;
393 pDevice->ulIntInContCRCError = 0;
394 pDevice->bEventAvailable = true;
395 INTnsProcessData(pDevice);
398 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_InterruptStat, ntStatus);
401 if (pDevice->fKillEventPollingThread != true) {
402 usb_fill_bulk_urb(pDevice->pInterruptURB,
404 usb_rcvbulkpipe(pDevice->usb, 1),
405 (void *) pDevice->intBuf.pDataBuf,
407 s_nsInterruptUsbIoCompleteRead,
410 ntStatus = usb_submit_urb(pDevice->pInterruptURB, GFP_ATOMIC);
412 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit int URB failed %d\n", ntStatus);
416 // We return STATUS_MORE_PROCESSING_REQUIRED so that the completion
417 // routine (IofCompleteRequest) will stop working on the irp.
424 * Allocates an usb BulkIn irp and calls USBD.
428 * pDevice - Pointer to the adapter
432 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
436 int PIPEnsBulkInUsbRead(struct vnt_private *pDevice, PRCB pRCB)
442 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsStartBulkInUsbRead\n");
444 if (pDevice->Flags & fMP_DISCONNECTED)
445 return STATUS_FAILURE;
447 pDevice->ulBulkInPosted++;
452 // Now that we have created the urb, we will send a
453 // request to the USB device object.
455 if (pRCB->skb == NULL) {
456 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pRCB->skb is null \n");
460 usb_fill_bulk_urb(pUrb,
462 usb_rcvbulkpipe(pDevice->usb, 2),
463 (void *) (pRCB->skb->data),
464 MAX_TOTAL_SIZE_WITH_ALL_HEADERS,
465 s_nsBulkInUsbIoCompleteRead,
468 ntStatus = usb_submit_urb(pUrb, GFP_ATOMIC);
470 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Rx URB failed %d\n", ntStatus);
471 return STATUS_FAILURE ;
474 pRCB->bBoolInUse= true;
484 * Complete function of usb BulkIn irp.
488 * pDevice - Pointer to the adapter
493 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
497 static void s_nsBulkInUsbIoCompleteRead(struct urb *urb)
499 PRCB pRCB = (PRCB)urb->context;
500 struct vnt_private *pDevice = pRCB->pDevice;
501 unsigned long bytesRead;
502 int bIndicateReceive = false;
503 int bReAllocSkb = false;
506 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkInUsbIoCompleteRead\n");
507 status = urb->status;
508 bytesRead = urb->actual_length;
511 pDevice->ulBulkInError++;
512 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK In failed %d\n", status);
514 pDevice->scStatistic.RxFcsErrCnt ++;
516 // if (status == USBD_STATUS_CRC) {
517 // pDevice->ulBulkInContCRCError++;
519 // if (status == STATUS_DEVICE_NOT_CONNECTED )
521 // MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
525 bIndicateReceive = true;
526 pDevice->ulBulkInContCRCError = 0;
527 pDevice->ulBulkInBytesRead += bytesRead;
529 pDevice->scStatistic.RxOkCnt ++;
533 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkInStat, status);
535 if (bIndicateReceive) {
536 spin_lock(&pDevice->lock);
537 if (RXbBulkInProcessData(pDevice, pRCB, bytesRead) == true)
539 spin_unlock(&pDevice->lock);
544 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"RxvFreeNormal %d \n",pDevice->NumRecvFreeList);
545 spin_lock(&pDevice->lock);
546 RXvFreeRCB(pRCB, bReAllocSkb);
547 spin_unlock(&pDevice->lock);
556 * Allocates an usb BulkOut irp and calls USBD.
560 * pDevice - Pointer to the adapter
564 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
568 int PIPEnsSendBulkOut(struct vnt_private *pDevice, PUSB_SEND_CONTEXT pContext)
575 pDevice->bPWBitOn = false;
578 if (pDevice->pPendingBulkOutContext != NULL) {
579 pDevice->NumContextsQueued++;
580 EnqueueContext(pDevice->FirstTxContextQueue, pDevice->LastTxContextQueue, pContext);
581 status = STATUS_PENDING;
582 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send pending!\n");
587 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_nsSendBulkOut\n");
589 if (MP_IS_READY(pDevice) && (pDevice->Flags & fMP_POST_WRITES)) {
591 pUrb = pContext->pUrb;
592 pDevice->ulBulkOutPosted++;
593 // pDevice->pPendingBulkOutContext = pContext;
597 usb_sndbulkpipe(pDevice->usb, 3),
598 (void *) &(pContext->Data[0]),
600 s_nsBulkOutIoCompleteWrite,
603 status = usb_submit_urb(pUrb, GFP_ATOMIC);
606 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Submit Tx URB failed %d\n", status);
607 pContext->bBoolInUse = false;
608 return STATUS_FAILURE;
610 return STATUS_PENDING;
613 pContext->bBoolInUse = false;
614 return STATUS_RESOURCES;
619 * Description: s_nsBulkOutIoCompleteWrite
620 * 1a) Indicate to the protocol the status of the write.
621 * 1b) Return ownership of the packet to the protocol.
623 * 2) If any more packets are queue for sending, send another packet
625 * If the attempt to send the packet to the driver fails,
626 * return ownership of the packet to the protocol and
627 * try another packet (until one succeeds).
631 * pdoUsbDevObj - pointer to the USB device object which
633 * pIrp - the irp which was completed by the
635 * pContext - the context given to IoSetCompletionRoutine
636 * before calling IoCallDriver on the irp
637 * The pContext is a pointer to the USB device object.
641 * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
642 * (IofCompleteRequest) to stop working on the irp.
646 static void s_nsBulkOutIoCompleteWrite(struct urb *urb)
648 struct vnt_private *pDevice;
650 CONTEXT_TYPE ContextType;
651 unsigned long ulBufLen;
652 PUSB_SEND_CONTEXT pContext;
655 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->s_nsBulkOutIoCompleteWrite\n");
657 // The context given to IoSetCompletionRoutine is an USB_CONTEXT struct
659 pContext = (PUSB_SEND_CONTEXT) urb->context;
660 ASSERT( NULL != pContext );
662 pDevice = pContext->pDevice;
663 ContextType = pContext->Type;
664 ulBufLen = pContext->uBufLen;
666 if (!netif_device_present(pDevice->dev))
670 // Perform various IRP, URB, and buffer 'sanity checks'
673 status = urb->status;
674 //we should have failed, succeeded, or cancelled, but NOT be pending
675 STAvUpdateUSBCounter(&pDevice->scStatistic.USB_BulkOutStat, status);
677 if(status == STATUS_SUCCESS) {
678 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Write %d bytes\n",(int)ulBufLen);
679 pDevice->ulBulkOutBytesWrite += ulBufLen;
680 pDevice->ulBulkOutContCRCError = 0;
681 pDevice->nTxDataTimeCout = 0;
684 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BULK Out failed %d\n", status);
685 pDevice->ulBulkOutError++;
688 // pDevice->ulCheckForHangCount = 0;
689 // pDevice->pPendingBulkOutContext = NULL;
691 if ( CONTEXT_DATA_PACKET == ContextType ) {
692 // Indicate to the protocol the status of the sent packet and return
693 // ownership of the packet.
694 if (pContext->pPacket != NULL) {
695 dev_kfree_skb_irq(pContext->pPacket);
696 pContext->pPacket = NULL;
697 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"tx %d bytes\n",(int)ulBufLen);
700 pDevice->dev->trans_start = jiffies;
703 if (status == STATUS_SUCCESS) {
704 pDevice->packetsSent++;
707 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send USB error! [%08xh]\n", status);
708 pDevice->packetsSentDropped++;
712 if (pDevice->bLinkPass == true) {
713 if (netif_queue_stopped(pDevice->dev))
714 netif_wake_queue(pDevice->dev);
716 pContext->bBoolInUse = false;