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
);
201 skb_push(rcb
->skb
, skb_headroom(rcb
->skb
));
202 skb_trim(rcb
->skb
, 0);
205 urb
->transfer_buffer
= skb_put(rcb
->skb
,
206 skb_tailroom(rcb
->skb
));
209 if (usb_submit_urb(urb
, GFP_ATOMIC
)) {
210 dev_dbg(&priv
->usb
->dev
, "Failed to re submit rx skb\n");
216 int vnt_submit_rx_urb(struct vnt_private
*priv
, struct vnt_rcb
*rcb
)
219 struct urb
*urb
= rcb
->urb
;
222 dev_dbg(&priv
->usb
->dev
, "rcb->skb is null\n");
226 usb_fill_bulk_urb(urb
,
228 usb_rcvbulkpipe(priv
->usb
, 2),
229 skb_put(rcb
->skb
, skb_tailroom(rcb
->skb
)),
230 MAX_TOTAL_SIZE_WITH_ALL_HEADERS
,
231 vnt_submit_rx_urb_complete
,
234 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
236 dev_dbg(&priv
->usb
->dev
, "Submit Rx URB failed %d\n", status
);
237 return STATUS_FAILURE
;
245 static void vnt_tx_context_complete(struct urb
*urb
)
247 struct vnt_usb_send_context
*context
= urb
->context
;
248 struct vnt_private
*priv
= context
->priv
;
250 switch (urb
->status
) {
252 dev_dbg(&priv
->usb
->dev
, "Write %d bytes\n", context
->buf_len
);
257 context
->in_use
= false;
261 dev_dbg(&priv
->usb
->dev
, "BULK Out failed %d\n", urb
->status
);
265 if (context
->type
== CONTEXT_DATA_PACKET
)
266 ieee80211_wake_queues(priv
->hw
);
268 if (urb
->status
|| context
->type
== CONTEXT_BEACON_PACKET
) {
270 ieee80211_free_txskb(priv
->hw
, context
->skb
);
272 context
->in_use
= false;
276 int vnt_tx_context(struct vnt_private
*priv
,
277 struct vnt_usb_send_context
*context
)
280 struct urb
*urb
= context
->urb
;
282 if (test_bit(DEVICE_FLAGS_DISCONNECTED
, &priv
->flags
)) {
283 context
->in_use
= false;
284 return STATUS_RESOURCES
;
287 usb_fill_bulk_urb(urb
,
289 usb_sndbulkpipe(priv
->usb
, 3),
292 vnt_tx_context_complete
,
295 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
297 dev_dbg(&priv
->usb
->dev
, "Submit Tx URB failed %d\n", status
);
299 context
->in_use
= false;
300 return STATUS_FAILURE
;
303 return STATUS_PENDING
;