]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/vt6656/usbpipe.c
staging: vt6656: s_nsBulkOutIoCompleteWrite fix bug of 5GHZ a rates idx
[karo-tx-linux.git] / drivers / staging / vt6656 / usbpipe.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
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.
9  *
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.
14  *
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.
18  *
19  *
20  * File: usbpipe.c
21  *
22  * Purpose: Handle USB control endpoint
23  *
24  * Author: Warren Hsu
25  *
26  * Date: Mar. 29, 2005
27  *
28  * Functions:
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
34  *
35  * Revision History:
36  *      04-05-2004 Jerry Chen:  Initial release
37  *      11-24-2004 Warren Hsu: Add ControlvWriteByte,ControlvReadByte,ControlvMaskByte
38  *
39  */
40
41 #include "int.h"
42 #include "rxtx.h"
43 #include "dpc.h"
44 #include "desc.h"
45 #include "device.h"
46 #include "usbpipe.h"
47
48 //endpoint def
49 //endpoint 0: control
50 //endpoint 1: interrupt
51 //endpoint 2: read bulk
52 //endpoint 3: write bulk
53
54 #define USB_CTL_WAIT   500 //ms
55
56 #ifndef URB_ASYNC_UNLINK
57 #define URB_ASYNC_UNLINK    0
58 #endif
59
60 static void s_nsInterruptUsbIoCompleteRead(struct urb *urb);
61 static void s_nsBulkInUsbIoCompleteRead(struct urb *urb);
62 static void s_nsBulkOutIoCompleteWrite(struct urb *urb);
63
64 int vnt_control_out(struct vnt_private *priv, u8 request, u16 value,
65                 u16 index, u16 length, u8 *buffer)
66 {
67         int status = 0;
68
69         if (priv->Flags & fMP_DISCONNECTED)
70                 return STATUS_FAILURE;
71
72         mutex_lock(&priv->usb_lock);
73
74         status = usb_control_msg(priv->usb,
75                 usb_sndctrlpipe(priv->usb, 0), request, 0x40, value,
76                         index, buffer, length, USB_CTL_WAIT);
77
78         mutex_unlock(&priv->usb_lock);
79
80         if (status < (int)length)
81                 return STATUS_FAILURE;
82
83         return STATUS_SUCCESS;
84 }
85
86 void vnt_control_out_u8(struct vnt_private *priv, u8 reg, u8 reg_off, u8 data)
87 {
88         vnt_control_out(priv, MESSAGE_TYPE_WRITE,
89                                         reg_off, reg, sizeof(u8), &data);
90 }
91
92 int vnt_control_in(struct vnt_private *priv, u8 request, u16 value,
93                 u16 index, u16 length, u8 *buffer)
94 {
95         int status;
96
97         if (priv->Flags & fMP_DISCONNECTED)
98                 return STATUS_FAILURE;
99
100         mutex_lock(&priv->usb_lock);
101
102         status = usb_control_msg(priv->usb,
103                 usb_rcvctrlpipe(priv->usb, 0), request, 0xc0, value,
104                         index, buffer, length, USB_CTL_WAIT);
105
106         mutex_unlock(&priv->usb_lock);
107
108         if (status < (int)length)
109                 return STATUS_FAILURE;
110
111         return STATUS_SUCCESS;
112 }
113
114 void vnt_control_in_u8(struct vnt_private *priv, u8 reg, u8 reg_off, u8 *data)
115 {
116         vnt_control_in(priv, MESSAGE_TYPE_READ,
117                         reg_off, reg, sizeof(u8), data);
118 }
119
120 /*
121  * Description:
122  *      Allocates an usb interrupt in irp and calls USBD.
123  *
124  * Parameters:
125  *  In:
126  *      pDevice     - Pointer to the adapter
127  *  Out:
128  *      none
129  *
130  * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
131  *
132  */
133
134 int PIPEnsInterruptRead(struct vnt_private *priv)
135 {
136         int status = STATUS_FAILURE;
137
138         if (priv->int_buf.in_use == true)
139                 return STATUS_FAILURE;
140
141         priv->int_buf.in_use = true;
142
143         usb_fill_int_urb(priv->pInterruptURB,
144                 priv->usb,
145                 usb_rcvintpipe(priv->usb, 1),
146                 priv->int_buf.data_buf,
147                 MAX_INTERRUPT_SIZE,
148                 s_nsInterruptUsbIoCompleteRead,
149                 priv,
150                 priv->int_interval);
151
152         status = usb_submit_urb(priv->pInterruptURB, GFP_ATOMIC);
153         if (status) {
154                 dev_dbg(&priv->usb->dev, "Submit int URB failed %d\n", status);
155                 priv->int_buf.in_use = false;
156         }
157
158         return status;
159 }
160
161 /*
162  * Description:
163  *      Complete function of usb interrupt in irp.
164  *
165  * Parameters:
166  *  In:
167  *      pDevice     - Pointer to the adapter
168  *
169  *  Out:
170  *      none
171  *
172  * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
173  *
174  */
175
176 static void s_nsInterruptUsbIoCompleteRead(struct urb *urb)
177 {
178         struct vnt_private *priv = urb->context;
179         int status;
180
181         switch (urb->status) {
182         case 0:
183         case -ETIMEDOUT:
184                 break;
185         case -ECONNRESET:
186         case -ENOENT:
187         case -ESHUTDOWN:
188                 priv->int_buf.in_use = false;
189                 return;
190         default:
191                 break;
192         }
193
194         status = urb->status;
195
196         if (status != STATUS_SUCCESS) {
197                 priv->int_buf.in_use = false;
198
199                 dev_dbg(&priv->usb->dev, "%s status = %d\n", __func__, status);
200         } else {
201                 INTnsProcessData(priv);
202         }
203
204         status = usb_submit_urb(priv->pInterruptURB, GFP_ATOMIC);
205         if (status) {
206                 dev_dbg(&priv->usb->dev, "Submit int URB failed %d\n", status);
207         } else {
208                 priv->int_buf.in_use = true;
209         }
210
211         return;
212 }
213
214 /*
215  * Description:
216  *      Allocates an usb BulkIn  irp and calls USBD.
217  *
218  * Parameters:
219  *  In:
220  *      pDevice     - Pointer to the adapter
221  *  Out:
222  *      none
223  *
224  * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
225  *
226  */
227
228 int PIPEnsBulkInUsbRead(struct vnt_private *priv, struct vnt_rcb *rcb)
229 {
230         int status = 0;
231         struct urb *urb;
232
233         urb = rcb->pUrb;
234         if (rcb->skb == NULL) {
235                 dev_dbg(&priv->usb->dev, "rcb->skb is null\n");
236                 return status;
237         }
238
239         usb_fill_bulk_urb(urb,
240                 priv->usb,
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,
245                 rcb);
246
247         status = usb_submit_urb(urb, GFP_ATOMIC);
248         if (status != 0) {
249                 dev_dbg(&priv->usb->dev, "Submit Rx URB failed %d\n", status);
250                 return STATUS_FAILURE ;
251         }
252
253         rcb->Ref = 1;
254         rcb->bBoolInUse = true;
255
256         return status;
257 }
258
259 /*
260  * Description:
261  *      Complete function of usb BulkIn irp.
262  *
263  * Parameters:
264  *  In:
265  *      pDevice     - Pointer to the adapter
266  *
267  *  Out:
268  *      none
269  *
270  * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
271  *
272  */
273
274 static void s_nsBulkInUsbIoCompleteRead(struct urb *urb)
275 {
276         struct vnt_rcb *rcb = urb->context;
277         struct vnt_private *priv = rcb->pDevice;
278         unsigned long flags;
279         int re_alloc_skb = false;
280
281         switch (urb->status) {
282         case 0:
283                 break;
284         case -ECONNRESET:
285         case -ENOENT:
286         case -ESHUTDOWN:
287                 return;
288         case -ETIMEDOUT:
289         default:
290                 dev_dbg(&priv->usb->dev, "BULK In failed %d\n", urb->status);
291                 break;
292         }
293
294         if (urb->actual_length) {
295                 spin_lock_irqsave(&priv->lock, flags);
296
297                 if (vnt_rx_data(priv, rcb, urb->actual_length))
298                         re_alloc_skb = true;
299
300                 spin_unlock_irqrestore(&priv->lock, flags);
301         }
302
303         if (re_alloc_skb) {
304                 rcb->skb = dev_alloc_skb(priv->rx_buf_sz);
305                 if (!rcb->skb) {
306                         dev_dbg(&priv->usb->dev, "Failed to re-alloc rx skb\n");
307
308                         rcb->bBoolInUse = false;
309
310                         return;
311                 }
312
313                 urb->transfer_buffer = skb_put(rcb->skb,
314                                                 skb_tailroom(rcb->skb));
315         }
316
317         if (usb_submit_urb(urb, GFP_ATOMIC)) {
318                 dev_dbg(&priv->usb->dev, "Failed to re submit rx skb\n");
319
320                 rcb->bBoolInUse = false;
321         }
322
323         return;
324 }
325
326 /*
327  * Description:
328  *      Allocates an usb BulkOut  irp and calls USBD.
329  *
330  * Parameters:
331  *  In:
332  *      pDevice     - Pointer to the adapter
333  *  Out:
334  *      none
335  *
336  * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
337  *
338  */
339
340 int PIPEnsSendBulkOut(struct vnt_private *priv,
341                                 struct vnt_usb_send_context *context)
342 {
343         int status;
344         struct urb *urb;
345
346         if (!(MP_IS_READY(priv) && priv->Flags & fMP_POST_WRITES)) {
347                 context->in_use = false;
348                 return STATUS_RESOURCES;
349         }
350
351         urb = context->urb;
352
353         usb_fill_bulk_urb(urb,
354                         priv->usb,
355                         usb_sndbulkpipe(priv->usb, 3),
356                         context->data,
357                         context->buf_len,
358                         s_nsBulkOutIoCompleteWrite,
359                         context);
360
361         status = usb_submit_urb(urb, GFP_ATOMIC);
362         if (status != 0) {
363                 dev_dbg(&priv->usb->dev, "Submit Tx URB failed %d\n", status);
364
365                 context->in_use = false;
366                 return STATUS_FAILURE;
367         }
368
369         return STATUS_PENDING;
370 }
371
372 /*
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.
376  *
377  *     2)  If any more packets are queue for sending, send another packet
378  *         to USBD.
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).
382  *
383  * Parameters:
384  *  In:
385  *      pdoUsbDevObj  - pointer to the USB device object which
386  *                      completed the irp
387  *      pIrp          - the irp which was completed by the
388  *                      device object
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.
392  *  Out:
393  *      none
394  *
395  * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
396  *               (IofCompleteRequest) to stop working on the irp.
397  *
398  */
399
400 static void s_nsBulkOutIoCompleteWrite(struct urb *urb)
401 {
402         struct vnt_usb_send_context *context = urb->context;
403         struct vnt_private *priv = context->priv;
404         struct ieee80211_tx_info *info;
405
406         switch (urb->status) {
407         case 0:
408                 dev_dbg(&priv->usb->dev, "Write %d bytes\n", context->buf_len);
409                 break;
410         case -ECONNRESET:
411         case -ENOENT:
412         case -ESHUTDOWN:
413                 context->in_use = false;
414                 return;
415         case -ETIMEDOUT:
416         default:
417                 dev_dbg(&priv->usb->dev, "BULK Out failed %d\n", urb->status);
418                 break;
419         }
420
421         if (context->skb) {
422                 s8 idx;
423
424                 info = IEEE80211_SKB_CB(context->skb);
425
426                 idx = info->control.rates[0].idx;
427
428                 ieee80211_tx_info_clear_status(info);
429                 info->status.rates[0].idx = idx;
430                 info->status.rates[0].count = 0;
431                 if (!urb->status)
432                         info->flags |= IEEE80211_TX_STAT_ACK;
433                 ieee80211_tx_status_irqsafe(priv->hw, context->skb);
434         }
435
436         if (context->type == CONTEXT_DATA_PACKET)
437                 ieee80211_wake_queues(priv->hw);
438
439         context->in_use = false;
440
441         return;
442 }