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 * vnt_control_out - 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 #define USB_CTL_WAIT 500 //ms
56 #ifndef URB_ASYNC_UNLINK
57 #define URB_ASYNC_UNLINK 0
60 static void s_nsInterruptUsbIoCompleteRead(struct urb *urb);
61 static void s_nsBulkInUsbIoCompleteRead(struct urb *urb);
62 static void s_nsBulkOutIoCompleteWrite(struct urb *urb);
64 int vnt_control_out(struct vnt_private *priv, u8 request, u16 value,
65 u16 index, u16 length, u8 *buffer)
69 if (priv->Flags & fMP_DISCONNECTED)
70 return STATUS_FAILURE;
72 mutex_lock(&priv->usb_lock);
74 status = usb_control_msg(priv->usb,
75 usb_sndctrlpipe(priv->usb, 0), request, 0x40, value,
76 index, buffer, length, USB_CTL_WAIT);
78 mutex_unlock(&priv->usb_lock);
80 if (status < (int)length)
81 return STATUS_FAILURE;
83 return STATUS_SUCCESS;
86 int PIPEnsControlIn(struct vnt_private *priv, u8 request, u16 value,
87 u16 index, u16 length, u8 *buffer)
91 if (priv->Flags & fMP_DISCONNECTED)
92 return STATUS_FAILURE;
94 mutex_lock(&priv->usb_lock);
96 status = usb_control_msg(priv->usb,
97 usb_rcvctrlpipe(priv->usb, 0), request, 0xc0, value,
98 index, buffer, length, USB_CTL_WAIT);
100 mutex_unlock(&priv->usb_lock);
102 if (status < (int)length)
103 return STATUS_FAILURE;
105 return STATUS_SUCCESS;
110 * Allocates an usb interrupt in irp and calls USBD.
114 * pDevice - Pointer to the adapter
118 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
122 int PIPEnsInterruptRead(struct vnt_private *priv)
124 int status = STATUS_FAILURE;
126 if (priv->int_buf.in_use == true)
127 return STATUS_FAILURE;
129 priv->int_buf.in_use = true;
131 usb_fill_int_urb(priv->pInterruptURB,
133 usb_rcvintpipe(priv->usb, 1),
134 priv->int_buf.data_buf,
136 s_nsInterruptUsbIoCompleteRead,
140 status = usb_submit_urb(priv->pInterruptURB, GFP_ATOMIC);
142 dev_dbg(&priv->usb->dev, "Submit int URB failed %d\n", status);
143 priv->int_buf.in_use = false;
151 * Complete function of usb interrupt in irp.
155 * pDevice - Pointer to the adapter
160 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
164 static void s_nsInterruptUsbIoCompleteRead(struct urb *urb)
166 struct vnt_private *priv = urb->context;
169 switch (urb->status) {
176 priv->int_buf.in_use = false;
182 status = urb->status;
184 if (status != STATUS_SUCCESS) {
185 priv->int_buf.in_use = false;
187 dev_dbg(&priv->usb->dev, "%s status = %d\n", __func__, status);
189 INTnsProcessData(priv);
192 status = usb_submit_urb(priv->pInterruptURB, GFP_ATOMIC);
194 dev_dbg(&priv->usb->dev, "Submit int URB failed %d\n", status);
196 priv->int_buf.in_use = true;
204 * Allocates an usb BulkIn irp and calls USBD.
208 * pDevice - Pointer to the adapter
212 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
216 int PIPEnsBulkInUsbRead(struct vnt_private *priv, struct vnt_rcb *rcb)
221 if (priv->Flags & fMP_DISCONNECTED)
222 return STATUS_FAILURE;
225 if (rcb->skb == NULL) {
226 dev_dbg(&priv->usb->dev, "rcb->skb is null\n");
230 usb_fill_bulk_urb(urb,
232 usb_rcvbulkpipe(priv->usb, 2),
233 (void *) (rcb->skb->data),
234 MAX_TOTAL_SIZE_WITH_ALL_HEADERS,
235 s_nsBulkInUsbIoCompleteRead,
238 status = usb_submit_urb(urb, GFP_ATOMIC);
240 dev_dbg(&priv->usb->dev, "Submit Rx URB failed %d\n", status);
241 return STATUS_FAILURE ;
245 rcb->bBoolInUse = true;
252 * Complete function of usb BulkIn irp.
256 * pDevice - Pointer to the adapter
261 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
265 static void s_nsBulkInUsbIoCompleteRead(struct urb *urb)
267 struct vnt_rcb *rcb = urb->context;
268 struct vnt_private *priv = rcb->pDevice;
270 int re_alloc_skb = false;
272 switch (urb->status) {
281 dev_dbg(&priv->usb->dev, "BULK In failed %d\n", urb->status);
285 if (urb->actual_length) {
286 spin_lock_irqsave(&priv->lock, flags);
288 if (RXbBulkInProcessData(priv, rcb, urb->actual_length) == true)
291 spin_unlock_irqrestore(&priv->lock, flags);
296 dev_dbg(&priv->usb->dev,
297 "RxvFreeNormal %d\n", priv->NumRecvFreeList);
299 spin_lock_irqsave(&priv->lock, flags);
301 RXvFreeRCB(rcb, re_alloc_skb);
303 spin_unlock_irqrestore(&priv->lock, flags);
311 * Allocates an usb BulkOut irp and calls USBD.
315 * pDevice - Pointer to the adapter
319 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
323 int PIPEnsSendBulkOut(struct vnt_private *priv,
324 struct vnt_usb_send_context *context)
329 priv->bPWBitOn = false;
331 if (!(MP_IS_READY(priv) && priv->Flags & fMP_POST_WRITES)) {
332 context->in_use = false;
333 return STATUS_RESOURCES;
338 usb_fill_bulk_urb(urb,
340 usb_sndbulkpipe(priv->usb, 3),
343 s_nsBulkOutIoCompleteWrite,
346 status = usb_submit_urb(urb, GFP_ATOMIC);
348 dev_dbg(&priv->usb->dev, "Submit Tx URB failed %d\n", status);
350 context->in_use = false;
351 return STATUS_FAILURE;
354 return STATUS_PENDING;
358 * Description: s_nsBulkOutIoCompleteWrite
359 * 1a) Indicate to the protocol the status of the write.
360 * 1b) Return ownership of the packet to the protocol.
362 * 2) If any more packets are queue for sending, send another packet
364 * If the attempt to send the packet to the driver fails,
365 * return ownership of the packet to the protocol and
366 * try another packet (until one succeeds).
370 * pdoUsbDevObj - pointer to the USB device object which
372 * pIrp - the irp which was completed by the
374 * pContext - the context given to IoSetCompletionRoutine
375 * before calling IoCallDriver on the irp
376 * The pContext is a pointer to the USB device object.
380 * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
381 * (IofCompleteRequest) to stop working on the irp.
385 static void s_nsBulkOutIoCompleteWrite(struct urb *urb)
387 struct vnt_usb_send_context *context = urb->context;
388 struct vnt_private *priv = context->priv;
389 u8 context_type = context->type;
391 switch (urb->status) {
393 dev_dbg(&priv->usb->dev, "Write %d bytes\n", context->buf_len);
398 context->in_use = false;
402 dev_dbg(&priv->usb->dev, "BULK Out failed %d\n", urb->status);
406 if (!netif_device_present(priv->dev))
409 if (CONTEXT_DATA_PACKET == context_type) {
410 if (context->skb != NULL) {
411 dev_kfree_skb_irq(context->skb);
413 dev_dbg(&priv->usb->dev,
414 "tx %d bytes\n", context->buf_len);
417 priv->dev->trans_start = jiffies;
420 if (priv->bLinkPass == true) {
421 if (netif_queue_stopped(priv->dev))
422 netif_wake_queue(priv->dev);
425 context->in_use = false;