1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
8 * Purpose: Handle USB control endpoint
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
21 * 04-05-2004 Jerry Chen: Initial release
22 * 11-24-2004 Warren Hsu: Add ControlvWriteByte,ControlvReadByte,
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
)
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
);
49 mutex_unlock(&priv
->usb_lock
);
53 status
= usb_control_msg(priv
->usb
,
54 usb_sndctrlpipe(priv
->usb
, 0),
56 index
, usb_buffer
, length
, USB_CTL_WAIT
);
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
)
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
);
87 mutex_unlock(&priv
->usb_lock
);
91 status
= usb_control_msg(priv
->usb
,
92 usb_rcvctrlpipe(priv
->usb
, 0),
94 index
, usb_buffer
, length
, USB_CTL_WAIT
);
97 memcpy(buffer
, usb_buffer
, length
);
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
;
127 priv
->int_buf
.in_use
= false;
134 priv
->int_buf
.in_use
= false;
136 dev_dbg(&priv
->usb
->dev
, "%s status = %d\n", __func__
, status
);
138 vnt_int_process_data(priv
);
141 status
= usb_submit_urb(priv
->interrupt_urb
, GFP_ATOMIC
);
143 dev_dbg(&priv
->usb
->dev
, "Submit int URB failed %d\n", status
);
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
,
159 usb_rcvintpipe(priv
->usb
, 1),
160 priv
->int_buf
.data_buf
,
162 vnt_start_interrupt_urb_complete
,
166 status
= usb_submit_urb(priv
->interrupt_urb
, GFP_ATOMIC
);
168 dev_dbg(&priv
->usb
->dev
, "Submit int URB failed %d\n", status
);
169 priv
->int_buf
.in_use
= false;
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
) {
189 dev_dbg(&priv
->usb
->dev
, "BULK In failed %d\n", urb
->status
);
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
);
197 dev_dbg(&priv
->usb
->dev
,
198 "Failed to re-alloc rx skb\n");
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");
219 int vnt_submit_rx_urb(struct vnt_private
*priv
, struct vnt_rcb
*rcb
)
222 struct urb
*urb
= rcb
->urb
;
225 dev_dbg(&priv
->usb
->dev
, "rcb->skb is null\n");
229 usb_fill_bulk_urb(urb
,
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
,
237 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
239 dev_dbg(&priv
->usb
->dev
, "Submit Rx URB failed %d\n", status
);
240 return STATUS_FAILURE
;
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
) {
255 dev_dbg(&priv
->usb
->dev
, "Write %d bytes\n", context
->buf_len
);
260 context
->in_use
= false;
264 dev_dbg(&priv
->usb
->dev
, "BULK Out failed %d\n", urb
->status
);
268 if (context
->type
== CONTEXT_DATA_PACKET
)
269 ieee80211_wake_queues(priv
->hw
);
271 if (urb
->status
|| context
->type
== CONTEXT_BEACON_PACKET
) {
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
)
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
,
292 usb_sndbulkpipe(priv
->usb
, 3),
295 vnt_tx_context_complete
,
298 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
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
;