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 * vnt_control_in - Read variable length bytes from MEM/BB/MAC/EEPROM
31 * vnt_control_out_u8 - Write one byte to MEM/BB/MAC/EEPROM
32 * vnt_control_in_u8 - 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 void vnt_control_out_u8(struct vnt_private *priv, u8 reg, u8 reg_off, u8 data)
88 vnt_control_out(priv, MESSAGE_TYPE_WRITE,
89 reg_off, reg, sizeof(u8), &data);
92 int vnt_control_in(struct vnt_private *priv, u8 request, u16 value,
93 u16 index, u16 length, u8 *buffer)
97 if (priv->Flags & fMP_DISCONNECTED)
98 return STATUS_FAILURE;
100 mutex_lock(&priv->usb_lock);
102 status = usb_control_msg(priv->usb,
103 usb_rcvctrlpipe(priv->usb, 0), request, 0xc0, value,
104 index, buffer, length, USB_CTL_WAIT);
106 mutex_unlock(&priv->usb_lock);
108 if (status < (int)length)
109 return STATUS_FAILURE;
111 return STATUS_SUCCESS;
114 void vnt_control_in_u8(struct vnt_private *priv, u8 reg, u8 reg_off, u8 *data)
116 vnt_control_in(priv, MESSAGE_TYPE_READ,
117 reg_off, reg, sizeof(u8), data);
122 * Allocates an usb interrupt in irp and calls USBD.
126 * pDevice - Pointer to the adapter
130 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
134 int PIPEnsInterruptRead(struct vnt_private *priv)
136 int status = STATUS_FAILURE;
138 if (priv->int_buf.in_use == true)
139 return STATUS_FAILURE;
141 priv->int_buf.in_use = true;
143 usb_fill_int_urb(priv->pInterruptURB,
145 usb_rcvintpipe(priv->usb, 1),
146 priv->int_buf.data_buf,
148 s_nsInterruptUsbIoCompleteRead,
152 status = usb_submit_urb(priv->pInterruptURB, GFP_ATOMIC);
154 dev_dbg(&priv->usb->dev, "Submit int URB failed %d\n", status);
155 priv->int_buf.in_use = false;
163 * Complete function of usb interrupt in irp.
167 * pDevice - Pointer to the adapter
172 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
176 static void s_nsInterruptUsbIoCompleteRead(struct urb *urb)
178 struct vnt_private *priv = urb->context;
181 switch (urb->status) {
188 priv->int_buf.in_use = false;
194 status = urb->status;
196 if (status != STATUS_SUCCESS) {
197 priv->int_buf.in_use = false;
199 dev_dbg(&priv->usb->dev, "%s status = %d\n", __func__, status);
201 INTnsProcessData(priv);
204 status = usb_submit_urb(priv->pInterruptURB, GFP_ATOMIC);
206 dev_dbg(&priv->usb->dev, "Submit int URB failed %d\n", status);
208 priv->int_buf.in_use = true;
216 * Allocates an usb BulkIn irp and calls USBD.
220 * pDevice - Pointer to the adapter
224 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
228 int PIPEnsBulkInUsbRead(struct vnt_private *priv, struct vnt_rcb *rcb)
234 if (rcb->skb == NULL) {
235 dev_dbg(&priv->usb->dev, "rcb->skb is null\n");
239 usb_fill_bulk_urb(urb,
241 usb_rcvbulkpipe(priv->usb, 2),
242 skb_put(rcb->skb, skb_tailroom(rcb->skb)),
243 MAX_TOTAL_SIZE_WITH_ALL_HEADERS,
244 s_nsBulkInUsbIoCompleteRead,
247 status = usb_submit_urb(urb, GFP_ATOMIC);
249 dev_dbg(&priv->usb->dev, "Submit Rx URB failed %d\n", status);
250 return STATUS_FAILURE ;
254 rcb->bBoolInUse = true;
261 * Complete function of usb BulkIn irp.
265 * pDevice - Pointer to the adapter
270 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
274 static void s_nsBulkInUsbIoCompleteRead(struct urb *urb)
276 struct vnt_rcb *rcb = urb->context;
277 struct vnt_private *priv = rcb->pDevice;
279 int re_alloc_skb = false;
281 switch (urb->status) {
290 dev_dbg(&priv->usb->dev, "BULK In failed %d\n", urb->status);
294 if (urb->actual_length) {
295 spin_lock_irqsave(&priv->lock, flags);
297 if (vnt_rx_data(priv, rcb, urb->actual_length))
300 spin_unlock_irqrestore(&priv->lock, flags);
304 rcb->skb = dev_alloc_skb(priv->rx_buf_sz);
306 dev_dbg(&priv->usb->dev, "Failed to re-alloc rx skb\n");
308 rcb->bBoolInUse = false;
313 urb->transfer_buffer = skb_put(rcb->skb,
314 skb_tailroom(rcb->skb));
317 if (usb_submit_urb(urb, GFP_ATOMIC)) {
318 dev_dbg(&priv->usb->dev, "Failed to re submit rx skb\n");
320 rcb->bBoolInUse = false;
328 * Allocates an usb BulkOut irp and calls USBD.
332 * pDevice - Pointer to the adapter
336 * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
340 int PIPEnsSendBulkOut(struct vnt_private *priv,
341 struct vnt_usb_send_context *context)
346 if (!(MP_IS_READY(priv) && priv->Flags & fMP_POST_WRITES)) {
347 context->in_use = false;
348 return STATUS_RESOURCES;
353 usb_fill_bulk_urb(urb,
355 usb_sndbulkpipe(priv->usb, 3),
358 s_nsBulkOutIoCompleteWrite,
361 status = usb_submit_urb(urb, GFP_ATOMIC);
363 dev_dbg(&priv->usb->dev, "Submit Tx URB failed %d\n", status);
365 context->in_use = false;
366 return STATUS_FAILURE;
369 return STATUS_PENDING;
373 * Description: s_nsBulkOutIoCompleteWrite
374 * 1a) Indicate to the protocol the status of the write.
375 * 1b) Return ownership of the packet to the protocol.
377 * 2) If any more packets are queue for sending, send another packet
379 * If the attempt to send the packet to the driver fails,
380 * return ownership of the packet to the protocol and
381 * try another packet (until one succeeds).
385 * pdoUsbDevObj - pointer to the USB device object which
387 * pIrp - the irp which was completed by the
389 * pContext - the context given to IoSetCompletionRoutine
390 * before calling IoCallDriver on the irp
391 * The pContext is a pointer to the USB device object.
395 * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
396 * (IofCompleteRequest) to stop working on the irp.
400 static void s_nsBulkOutIoCompleteWrite(struct urb *urb)
402 struct vnt_usb_send_context *context = urb->context;
403 struct vnt_private *priv = context->priv;
404 struct ieee80211_tx_info *info;
406 switch (urb->status) {
408 dev_dbg(&priv->usb->dev, "Write %d bytes\n", context->buf_len);
413 context->in_use = false;
417 dev_dbg(&priv->usb->dev, "BULK Out failed %d\n", urb->status);
424 info = IEEE80211_SKB_CB(context->skb);
426 idx = info->control.rates[0].idx;
428 ieee80211_tx_info_clear_status(info);
429 info->status.rates[0].idx = idx;
430 info->status.rates[0].count = 0;
432 info->flags |= IEEE80211_TX_STAT_ACK;
433 ieee80211_tx_status_irqsafe(priv->hw, context->skb);
436 if (context->type == CONTEXT_DATA_PACKET)
437 ieee80211_wake_queues(priv->hw);
439 context->in_use = false;