Merge tag 'iio-fixes-for-3.16a' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.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         if (priv->Flags & fMP_DISCONNECTED)
234                 return STATUS_FAILURE;
235
236         urb = rcb->pUrb;
237         if (rcb->skb == NULL) {
238                 dev_dbg(&priv->usb->dev, "rcb->skb is null\n");
239                 return status;
240         }
241
242         usb_fill_bulk_urb(urb,
243                 priv->usb,
244                 usb_rcvbulkpipe(priv->usb, 2),
245                 (void *) (rcb->skb->data),
246                 MAX_TOTAL_SIZE_WITH_ALL_HEADERS,
247                 s_nsBulkInUsbIoCompleteRead,
248                 rcb);
249
250         status = usb_submit_urb(urb, GFP_ATOMIC);
251         if (status != 0) {
252                 dev_dbg(&priv->usb->dev, "Submit Rx URB failed %d\n", status);
253                 return STATUS_FAILURE ;
254         }
255
256         rcb->Ref = 1;
257         rcb->bBoolInUse = true;
258
259         return status;
260 }
261
262 /*
263  * Description:
264  *      Complete function of usb BulkIn irp.
265  *
266  * Parameters:
267  *  In:
268  *      pDevice     - Pointer to the adapter
269  *
270  *  Out:
271  *      none
272  *
273  * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
274  *
275  */
276
277 static void s_nsBulkInUsbIoCompleteRead(struct urb *urb)
278 {
279         struct vnt_rcb *rcb = urb->context;
280         struct vnt_private *priv = rcb->pDevice;
281         unsigned long flags;
282         int re_alloc_skb = false;
283
284         switch (urb->status) {
285         case 0:
286                 break;
287         case -ECONNRESET:
288         case -ENOENT:
289         case -ESHUTDOWN:
290                 return;
291         case -ETIMEDOUT:
292         default:
293                 dev_dbg(&priv->usb->dev, "BULK In failed %d\n", urb->status);
294                 break;
295         }
296
297         if (urb->actual_length) {
298                 spin_lock_irqsave(&priv->lock, flags);
299
300                 if (RXbBulkInProcessData(priv, rcb, urb->actual_length) == true)
301                         re_alloc_skb = true;
302
303                 spin_unlock_irqrestore(&priv->lock, flags);
304         }
305
306         rcb->Ref--;
307         if (rcb->Ref == 0) {
308                 dev_dbg(&priv->usb->dev,
309                                 "RxvFreeNormal %d\n", priv->NumRecvFreeList);
310
311                 spin_lock_irqsave(&priv->lock, flags);
312
313                 RXvFreeRCB(rcb, re_alloc_skb);
314
315                 spin_unlock_irqrestore(&priv->lock, flags);
316         }
317
318         return;
319 }
320
321 /*
322  * Description:
323  *      Allocates an usb BulkOut  irp and calls USBD.
324  *
325  * Parameters:
326  *  In:
327  *      pDevice     - Pointer to the adapter
328  *  Out:
329  *      none
330  *
331  * Return Value: STATUS_INSUFFICIENT_RESOURCES or result of IoCallDriver
332  *
333  */
334
335 int PIPEnsSendBulkOut(struct vnt_private *priv,
336                                 struct vnt_usb_send_context *context)
337 {
338         int status;
339         struct urb *urb;
340
341         priv->bPWBitOn = false;
342
343         if (!(MP_IS_READY(priv) && priv->Flags & fMP_POST_WRITES)) {
344                 context->in_use = false;
345                 return STATUS_RESOURCES;
346         }
347
348         urb = context->urb;
349
350         usb_fill_bulk_urb(urb,
351                         priv->usb,
352                         usb_sndbulkpipe(priv->usb, 3),
353                         context->data,
354                         context->buf_len,
355                         s_nsBulkOutIoCompleteWrite,
356                         context);
357
358         status = usb_submit_urb(urb, GFP_ATOMIC);
359         if (status != 0) {
360                 dev_dbg(&priv->usb->dev, "Submit Tx URB failed %d\n", status);
361
362                 context->in_use = false;
363                 return STATUS_FAILURE;
364         }
365
366         return STATUS_PENDING;
367 }
368
369 /*
370  * Description: s_nsBulkOutIoCompleteWrite
371  *     1a) Indicate to the protocol the status of the write.
372  *     1b) Return ownership of the packet to the protocol.
373  *
374  *     2)  If any more packets are queue for sending, send another packet
375  *         to USBD.
376  *         If the attempt to send the packet to the driver fails,
377  *         return ownership of the packet to the protocol and
378  *         try another packet (until one succeeds).
379  *
380  * Parameters:
381  *  In:
382  *      pdoUsbDevObj  - pointer to the USB device object which
383  *                      completed the irp
384  *      pIrp          - the irp which was completed by the
385  *                      device object
386  *      pContext      - the context given to IoSetCompletionRoutine
387  *                      before calling IoCallDriver on the irp
388  *                      The pContext is a pointer to the USB device object.
389  *  Out:
390  *      none
391  *
392  * Return Value: STATUS_MORE_PROCESSING_REQUIRED - allows the completion routine
393  *               (IofCompleteRequest) to stop working on the irp.
394  *
395  */
396
397 static void s_nsBulkOutIoCompleteWrite(struct urb *urb)
398 {
399         struct vnt_usb_send_context *context = urb->context;
400         struct vnt_private *priv = context->priv;
401         u8 context_type = context->type;
402
403         switch (urb->status) {
404         case 0:
405                 dev_dbg(&priv->usb->dev, "Write %d bytes\n", context->buf_len);
406                 break;
407         case -ECONNRESET:
408         case -ENOENT:
409         case -ESHUTDOWN:
410                 context->in_use = false;
411                 return;
412         case -ETIMEDOUT:
413         default:
414                 dev_dbg(&priv->usb->dev, "BULK Out failed %d\n", urb->status);
415                 break;
416         }
417
418         if (!netif_device_present(priv->dev))
419                 return;
420
421         if (CONTEXT_DATA_PACKET == context_type) {
422                 if (context->skb != NULL) {
423                         dev_kfree_skb_irq(context->skb);
424                         context->skb = NULL;
425                         dev_dbg(&priv->usb->dev,
426                                         "tx  %d bytes\n", context->buf_len);
427                 }
428
429                 priv->dev->trans_start = jiffies;
430         }
431
432         if (priv->bLinkPass == true) {
433                 if (netif_queue_stopped(priv->dev))
434                         netif_wake_queue(priv->dev);
435         }
436
437         context->in_use = false;
438
439         return;
440 }