Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / staging / vt6656 / usbpipe.c
blob273176386a51af01382330fa1463bc37e6c86d32
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
4 * All rights reserved.
6 * File: usbpipe.c
8 * Purpose: Handle USB control endpoint
10 * Author: Warren Hsu
12 * Date: Mar. 29, 2005
14 * Functions:
15 * vnt_control_out - Write variable length bytes to MEM/BB/MAC/EEPROM
16 * vnt_control_in - Read variable length bytes from MEM/BB/MAC/EEPROM
17 * vnt_control_out_u8 - Write one byte to MEM/BB/MAC/EEPROM
18 * vnt_control_in_u8 - Read one byte from MEM/BB/MAC/EEPROM
20 * Revision History:
21 * 04-05-2004 Jerry Chen: Initial release
22 * 11-24-2004 Warren Hsu: Add ControlvWriteByte,ControlvReadByte,
23 * ControlvMaskByte
27 #include "int.h"
28 #include "rxtx.h"
29 #include "dpc.h"
30 #include "desc.h"
31 #include "device.h"
32 #include "usbpipe.h"
34 #define USB_CTL_WAIT 500 /* ms */
36 int vnt_control_out(struct vnt_private *priv, u8 request, u16 value,
37 u16 index, u16 length, u8 *buffer)
39 int status = 0;
40 u8 *usb_buffer;
42 if (test_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags))
43 return STATUS_FAILURE;
45 mutex_lock(&priv->usb_lock);
47 usb_buffer = kmemdup(buffer, length, GFP_KERNEL);
48 if (!usb_buffer) {
49 mutex_unlock(&priv->usb_lock);
50 return -ENOMEM;
53 status = usb_control_msg(priv->usb,
54 usb_sndctrlpipe(priv->usb, 0),
55 request, 0x40, value,
56 index, usb_buffer, length, USB_CTL_WAIT);
58 kfree(usb_buffer);
60 mutex_unlock(&priv->usb_lock);
62 if (status < (int)length)
63 return STATUS_FAILURE;
65 return STATUS_SUCCESS;
68 void vnt_control_out_u8(struct vnt_private *priv, u8 reg, u8 reg_off, u8 data)
70 vnt_control_out(priv, MESSAGE_TYPE_WRITE,
71 reg_off, reg, sizeof(u8), &data);
74 int vnt_control_in(struct vnt_private *priv, u8 request, u16 value,
75 u16 index, u16 length, u8 *buffer)
77 int status;
78 u8 *usb_buffer;
80 if (test_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags))
81 return STATUS_FAILURE;
83 mutex_lock(&priv->usb_lock);
85 usb_buffer = kmalloc(length, GFP_KERNEL);
86 if (!usb_buffer) {
87 mutex_unlock(&priv->usb_lock);
88 return -ENOMEM;
91 status = usb_control_msg(priv->usb,
92 usb_rcvctrlpipe(priv->usb, 0),
93 request, 0xc0, value,
94 index, usb_buffer, length, USB_CTL_WAIT);
96 if (status == length)
97 memcpy(buffer, usb_buffer, length);
99 kfree(usb_buffer);
101 mutex_unlock(&priv->usb_lock);
103 if (status < (int)length)
104 return STATUS_FAILURE;
106 return STATUS_SUCCESS;
109 void vnt_control_in_u8(struct vnt_private *priv, u8 reg, u8 reg_off, u8 *data)
111 vnt_control_in(priv, MESSAGE_TYPE_READ,
112 reg_off, reg, sizeof(u8), data);
115 static void vnt_start_interrupt_urb_complete(struct urb *urb)
117 struct vnt_private *priv = urb->context;
118 int status = urb->status;
120 switch (status) {
121 case 0:
122 case -ETIMEDOUT:
123 break;
124 case -ECONNRESET:
125 case -ENOENT:
126 case -ESHUTDOWN:
127 priv->int_buf.in_use = false;
128 return;
129 default:
130 break;
133 if (status) {
134 priv->int_buf.in_use = false;
136 dev_dbg(&priv->usb->dev, "%s status = %d\n", __func__, status);
137 } else {
138 vnt_int_process_data(priv);
141 status = usb_submit_urb(priv->interrupt_urb, GFP_ATOMIC);
142 if (status)
143 dev_dbg(&priv->usb->dev, "Submit int URB failed %d\n", status);
144 else
145 priv->int_buf.in_use = true;
148 int vnt_start_interrupt_urb(struct vnt_private *priv)
150 int status = STATUS_FAILURE;
152 if (priv->int_buf.in_use)
153 return STATUS_FAILURE;
155 priv->int_buf.in_use = true;
157 usb_fill_int_urb(priv->interrupt_urb,
158 priv->usb,
159 usb_rcvintpipe(priv->usb, 1),
160 priv->int_buf.data_buf,
161 MAX_INTERRUPT_SIZE,
162 vnt_start_interrupt_urb_complete,
163 priv,
164 priv->int_interval);
166 status = usb_submit_urb(priv->interrupt_urb, GFP_ATOMIC);
167 if (status) {
168 dev_dbg(&priv->usb->dev, "Submit int URB failed %d\n", status);
169 priv->int_buf.in_use = false;
172 return status;
175 static void vnt_submit_rx_urb_complete(struct urb *urb)
177 struct vnt_rcb *rcb = urb->context;
178 struct vnt_private *priv = rcb->priv;
180 switch (urb->status) {
181 case 0:
182 break;
183 case -ECONNRESET:
184 case -ENOENT:
185 case -ESHUTDOWN:
186 return;
187 case -ETIMEDOUT:
188 default:
189 dev_dbg(&priv->usb->dev, "BULK In failed %d\n", urb->status);
190 break;
193 if (urb->actual_length) {
194 if (vnt_rx_data(priv, rcb, urb->actual_length)) {
195 rcb->skb = dev_alloc_skb(priv->rx_buf_sz);
196 if (!rcb->skb) {
197 dev_dbg(&priv->usb->dev,
198 "Failed to re-alloc rx skb\n");
200 rcb->in_use = false;
201 return;
203 } else {
204 skb_push(rcb->skb, skb_headroom(rcb->skb));
205 skb_trim(rcb->skb, 0);
208 urb->transfer_buffer = skb_put(rcb->skb,
209 skb_tailroom(rcb->skb));
212 if (usb_submit_urb(urb, GFP_ATOMIC)) {
213 dev_dbg(&priv->usb->dev, "Failed to re submit rx skb\n");
215 rcb->in_use = false;
219 int vnt_submit_rx_urb(struct vnt_private *priv, struct vnt_rcb *rcb)
221 int status = 0;
222 struct urb *urb = rcb->urb;
224 if (!rcb->skb) {
225 dev_dbg(&priv->usb->dev, "rcb->skb is null\n");
226 return status;
229 usb_fill_bulk_urb(urb,
230 priv->usb,
231 usb_rcvbulkpipe(priv->usb, 2),
232 skb_put(rcb->skb, skb_tailroom(rcb->skb)),
233 MAX_TOTAL_SIZE_WITH_ALL_HEADERS,
234 vnt_submit_rx_urb_complete,
235 rcb);
237 status = usb_submit_urb(urb, GFP_ATOMIC);
238 if (status) {
239 dev_dbg(&priv->usb->dev, "Submit Rx URB failed %d\n", status);
240 return STATUS_FAILURE;
243 rcb->in_use = true;
245 return status;
248 static void vnt_tx_context_complete(struct urb *urb)
250 struct vnt_usb_send_context *context = urb->context;
251 struct vnt_private *priv = context->priv;
253 switch (urb->status) {
254 case 0:
255 dev_dbg(&priv->usb->dev, "Write %d bytes\n", context->buf_len);
256 break;
257 case -ECONNRESET:
258 case -ENOENT:
259 case -ESHUTDOWN:
260 context->in_use = false;
261 return;
262 case -ETIMEDOUT:
263 default:
264 dev_dbg(&priv->usb->dev, "BULK Out failed %d\n", urb->status);
265 break;
268 if (context->type == CONTEXT_DATA_PACKET)
269 ieee80211_wake_queues(priv->hw);
271 if (urb->status || context->type == CONTEXT_BEACON_PACKET) {
272 if (context->skb)
273 ieee80211_free_txskb(priv->hw, context->skb);
275 context->in_use = false;
279 int vnt_tx_context(struct vnt_private *priv,
280 struct vnt_usb_send_context *context)
282 int status;
283 struct urb *urb = context->urb;
285 if (test_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags)) {
286 context->in_use = false;
287 return STATUS_RESOURCES;
290 usb_fill_bulk_urb(urb,
291 priv->usb,
292 usb_sndbulkpipe(priv->usb, 3),
293 context->data,
294 context->buf_len,
295 vnt_tx_context_complete,
296 context);
298 status = usb_submit_urb(urb, GFP_ATOMIC);
299 if (status) {
300 dev_dbg(&priv->usb->dev, "Submit Tx URB failed %d\n", status);
302 context->in_use = false;
303 return STATUS_FAILURE;
306 return STATUS_PENDING;