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
;
57 #define USB_CTL_WAIT 500 //ms
59 #ifndef URB_ASYNC_UNLINK
60 #define URB_ASYNC_UNLINK 0
63 static void s_nsInterruptUsbIoCompleteRead(struct urb
*urb
);
64 static void s_nsBulkInUsbIoCompleteRead(struct urb
*urb
);
65 static void s_nsBulkOutIoCompleteWrite(struct urb
*urb
);
66 static void s_nsControlInUsbIoCompleteRead(struct urb
*urb
);
67 static void s_nsControlInUsbIoCompleteWrite(struct urb
*urb
);
69 int PIPEnsControlOutAsyn(struct vnt_private
*pDevice
, u8 byRequest
,
70 u16 wValue
, u16 wIndex
, u16 wLength
, u8
*pbyBuffer
)
74 if (pDevice
->Flags
& fMP_DISCONNECTED
)
75 return STATUS_FAILURE
;
77 if (pDevice
->Flags
& fMP_CONTROL_WRITES
)
78 return STATUS_FAILURE
;
81 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"in_interrupt return ..byRequest %x\n", byRequest
);
82 return STATUS_FAILURE
;
85 ntStatus
= usb_control_msg(
87 usb_sndctrlpipe(pDevice
->usb
, 0),
97 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"usb_sndctrlpipe ntStatus= %d\n", ntStatus
);
100 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"usb_sndctrlpipe fail, ntStatus= %d\n", ntStatus
);
106 int PIPEnsControlOut(struct vnt_private
*pDevice
, u8 byRequest
, u16 wValue
,
107 u16 wIndex
, u16 wLength
, u8
*pbyBuffer
)
108 __releases(&pDevice
->lock
)
109 __acquires(&pDevice
->lock
)
114 if (pDevice
->Flags
& fMP_DISCONNECTED
)
115 return STATUS_FAILURE
;
117 if (pDevice
->Flags
& fMP_CONTROL_WRITES
)
118 return STATUS_FAILURE
;
120 if (pDevice
->Flags
& fMP_CONTROL_READS
)
121 return STATUS_FAILURE
;
123 if (pDevice
->pControlURB
->hcpriv
)
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
)
172 __releases(&pDevice
->lock
)
173 __acquires(&pDevice
->lock
)
178 if (pDevice
->Flags
& fMP_DISCONNECTED
)
179 return STATUS_FAILURE
;
181 if (pDevice
->Flags
& fMP_CONTROL_READS
)
182 return STATUS_FAILURE
;
184 if (pDevice
->Flags
& fMP_CONTROL_WRITES
)
185 return STATUS_FAILURE
;
187 if (pDevice
->pControlURB
->hcpriv
)
188 return STATUS_FAILURE
;
190 MP_SET_FLAG(pDevice
, fMP_CONTROL_READS
);
192 pDevice
->sUsbCtlRequest
.bRequestType
= 0xC0;
193 pDevice
->sUsbCtlRequest
.bRequest
= byRequest
;
194 pDevice
->sUsbCtlRequest
.wValue
= cpu_to_le16p(&wValue
);
195 pDevice
->sUsbCtlRequest
.wIndex
= cpu_to_le16p(&wIndex
);
196 pDevice
->sUsbCtlRequest
.wLength
= cpu_to_le16p(&wLength
);
197 pDevice
->pControlURB
->transfer_flags
|= URB_ASYNC_UNLINK
;
198 pDevice
->pControlURB
->actual_length
= 0;
199 usb_fill_control_urb(pDevice
->pControlURB
, pDevice
->usb
,
200 usb_rcvctrlpipe(pDevice
->usb
, 0), (char *) &pDevice
->sUsbCtlRequest
,
201 pbyBuffer
, wLength
, s_nsControlInUsbIoCompleteRead
, pDevice
);
203 ntStatus
= usb_submit_urb(pDevice
->pControlURB
, GFP_ATOMIC
);
205 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
206 "control request submission failed: %d\n", ntStatus
);
207 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_READS
);
208 return STATUS_FAILURE
;
211 spin_unlock_irq(&pDevice
->lock
);
212 for (ii
= 0; ii
<= USB_CTL_WAIT
; ii
++) {
214 if (pDevice
->Flags
& fMP_CONTROL_READS
)
219 if (ii
>= USB_CTL_WAIT
) {
220 DBG_PRT(MSG_LEVEL_DEBUG
,
221 KERN_INFO
"control rcv request submission timeout\n");
222 spin_lock_irq(&pDevice
->lock
);
223 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_READS
);
224 return STATUS_FAILURE
;
227 spin_lock_irq(&pDevice
->lock
);
232 static void s_nsControlInUsbIoCompleteWrite(struct urb
*urb
)
234 struct vnt_private
*pDevice
= (struct vnt_private
*)urb
->context
;
236 pDevice
= urb
->context
;
237 switch (urb
->status
) {
241 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl write urb status EINPROGRESS%d\n", urb
->status
);
244 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl write urb status ENOENT %d\n", urb
->status
);
247 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl write urb status %d\n", urb
->status
);
250 MP_CLEAR_FLAG(pDevice
, fMP_CONTROL_WRITES
);
255 * Complete function of usb Control callback
259 * pDevice - Pointer to the adapter
264 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
268 static void s_nsControlInUsbIoCompleteRead(struct urb
*urb
)
270 struct vnt_private
*pDevice
= (struct vnt_private
*)urb
->context
;
272 switch (urb
->status
) {
276 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl read urb status EINPROGRESS%d\n", urb
->status
);
279 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl read urb status = ENOENT %d\n", urb
->status
);
282 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"ctrl read urb status %d\n", urb
->status
);
285 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
*priv
)
304 int status
= STATUS_FAILURE
;
306 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
307 "---->s_nsStartInterruptUsbRead()\n");
309 if (priv
->int_buf
.in_use
== true)
310 return STATUS_FAILURE
;
312 priv
->int_buf
.in_use
= true;
314 usb_fill_int_urb(priv
->pInterruptURB
,
316 usb_rcvintpipe(priv
->usb
, 1),
317 priv
->int_buf
.data_buf
,
319 s_nsInterruptUsbIoCompleteRead
,
323 status
= usb_submit_urb(priv
->pInterruptURB
, GFP_ATOMIC
);
325 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
326 "Submit int URB failed %d\n", status
);
327 priv
->int_buf
.in_use
= false;
330 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
331 "<----s_nsStartInterruptUsbRead Return(%x)\n", status
);
338 * Complete function of usb interrupt in irp.
342 * pDevice - Pointer to the adapter
347 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
351 static void s_nsInterruptUsbIoCompleteRead(struct urb
*urb
)
353 struct vnt_private
*priv
= urb
->context
;
356 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
357 "---->s_nsInterruptUsbIoCompleteRead\n");
359 switch (urb
->status
) {
366 priv
->int_buf
.in_use
= false;
372 status
= urb
->status
;
374 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
375 "s_nsInterruptUsbIoCompleteRead Status %d\n", status
);
377 if (status
!= STATUS_SUCCESS
) {
378 priv
->int_buf
.in_use
= false;
380 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
381 "IntUSBIoCompleteControl STATUS = %d\n", status
);
383 INTnsProcessData(priv
);
386 status
= usb_submit_urb(priv
->pInterruptURB
, GFP_ATOMIC
);
388 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
389 "Submit int URB failed %d\n", status
);
391 priv
->int_buf
.in_use
= true;
399 * Allocates an usb BulkIn irp and calls USBD.
403 * pDevice - Pointer to the adapter
407 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
411 int PIPEnsBulkInUsbRead(struct vnt_private
*priv
, struct vnt_rcb
*rcb
)
416 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsStartBulkInUsbRead\n");
418 if (priv
->Flags
& fMP_DISCONNECTED
)
419 return STATUS_FAILURE
;
422 if (rcb
->skb
== NULL
) {
423 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"rcb->skb is null\n");
427 usb_fill_bulk_urb(urb
,
429 usb_rcvbulkpipe(priv
->usb
, 2),
430 (void *) (rcb
->skb
->data
),
431 MAX_TOTAL_SIZE_WITH_ALL_HEADERS
,
432 s_nsBulkInUsbIoCompleteRead
,
435 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
437 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
438 "Submit Rx URB failed %d\n", status
);
439 return STATUS_FAILURE
;
443 rcb
->bBoolInUse
= true;
450 * Complete function of usb BulkIn irp.
454 * pDevice - Pointer to the adapter
459 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
463 static void s_nsBulkInUsbIoCompleteRead(struct urb
*urb
)
465 struct vnt_rcb
*rcb
= urb
->context
;
466 struct vnt_private
*priv
= rcb
->pDevice
;
467 int re_alloc_skb
= false;
469 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsBulkInUsbIoCompleteRead\n");
471 switch (urb
->status
) {
480 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
481 "BULK In failed %d\n", urb
->status
);
485 if (urb
->actual_length
) {
486 spin_lock(&priv
->lock
);
488 if (RXbBulkInProcessData(priv
, rcb
, urb
->actual_length
) == true)
491 spin_unlock(&priv
->lock
);
496 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"RxvFreeNormal %d\n",
497 priv
->NumRecvFreeList
);
498 spin_lock(&priv
->lock
);
500 RXvFreeRCB(rcb
, re_alloc_skb
);
502 spin_unlock(&priv
->lock
);
510 * Allocates an usb BulkOut irp and calls USBD.
514 * pDevice - Pointer to the adapter
518 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
522 int PIPEnsSendBulkOut(struct vnt_private
*priv
,
523 struct vnt_usb_send_context
*context
)
528 priv
->bPWBitOn
= false;
530 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"s_nsSendBulkOut\n");
532 if (!(MP_IS_READY(priv
) && priv
->Flags
& fMP_POST_WRITES
)) {
533 context
->bBoolInUse
= false;
534 return STATUS_RESOURCES
;
539 usb_fill_bulk_urb(urb
,
541 usb_sndbulkpipe(priv
->usb
, 3),
544 s_nsBulkOutIoCompleteWrite
,
547 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
549 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
550 "Submit Tx URB failed %d\n", status
);
551 context
->bBoolInUse
= false;
552 return STATUS_FAILURE
;
555 return STATUS_PENDING
;
559 * Description: s_nsBulkOutIoCompleteWrite
560 * 1a) Indicate to the protocol the status of the write.
561 * 1b) Return ownership of the packet to the protocol.
563 * 2) If any more packets are queue for sending, send another packet
565 * If the attempt to send the packet to the driver fails,
566 * return ownership of the packet to the protocol and
567 * try another packet (until one succeeds).
571 * pdoUsbDevObj - pointer to the USB device object which
573 * pIrp - the irp which was completed by the
575 * pContext - the context given to IoSetCompletionRoutine
576 * before calling IoCallDriver on the irp
577 * The pContext is a pointer to the USB device object.
581 * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
582 * (IofCompleteRequest) to stop working on the irp.
586 static void s_nsBulkOutIoCompleteWrite(struct urb
*urb
)
588 struct vnt_usb_send_context
*context
= urb
->context
;
589 struct vnt_private
*priv
= context
->pDevice
;
590 u8 context_type
= context
->type
;
592 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"---->s_nsBulkOutIoCompleteWrite\n");
594 switch (urb
->status
) {
596 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
597 "Write %d bytes\n", context
->uBufLen
);
602 context
->bBoolInUse
= false;
606 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
607 "BULK Out failed %d\n", urb
->status
);
611 if (!netif_device_present(priv
->dev
))
614 if (CONTEXT_DATA_PACKET
== context_type
) {
615 if (context
->pPacket
!= NULL
) {
616 dev_kfree_skb_irq(context
->pPacket
);
617 context
->pPacket
= NULL
;
618 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
619 "tx %d bytes\n", context
->uBufLen
);
622 priv
->dev
->trans_start
= jiffies
;
625 if (priv
->bLinkPass
== true) {
626 if (netif_queue_stopped(priv
->dev
))
627 netif_wake_queue(priv
->dev
);
630 context
->bBoolInUse
= false;