Merge branch 'drm-radeon-testing' of /ssd/git/drm-radeon-next into drm-next-stage
[pandora-kernel.git] / drivers / staging / otus / wrap_usb.c
1 /*
2  * Copyright (c) 2007-2008 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 /*                                                                      */
17 /*  Module Name : wrap_usb.c                                            */
18 /*                                                                      */
19 /*  Abstract                                                            */
20 /*     This module contains wrapper functions for USB management        */
21 /*                                                                      */
22 /*  NOTES                                                               */
23 /*     Platform dependent.                                              */
24 /*                                                                      */
25 /************************************************************************/
26
27 #include "oal_dt.h"
28 #include "usbdrv.h"
29
30 #include <linux/netlink.h>
31 #include <net/iw_handler.h>
32
33 extern void zfLnxInitUsbTxQ(zdev_t *dev);
34 extern void zfLnxInitUsbRxQ(zdev_t *dev);
35 extern u32_t zfLnxSubmitRegInUrb(zdev_t *dev);
36 u32_t zfLnxUsbOut(zdev_t *dev, u8_t *hdr, u16_t hdrlen, u8_t *snap,
37                 u16_t snapLen, u8_t *tail, u16_t tailLen, zbuf_t *buf,
38                 u16_t offset);
39 u32_t zfLnxUsbWriteReg(zdev_t *dev, u32_t *cmd, u16_t cmdLen);
40
41 void zfwUsbRegisterCallBack(zdev_t *dev, struct zfCbUsbFuncTbl *zfUsbFunc)
42 {
43         struct usbdrv_private *macp = dev->ml_priv;
44
45         macp->usbCbFunctions.zfcbUsbRecv = zfUsbFunc->zfcbUsbRecv;
46         macp->usbCbFunctions.zfcbUsbRegIn = zfUsbFunc->zfcbUsbRegIn;
47         macp->usbCbFunctions.zfcbUsbOutComplete = zfUsbFunc->zfcbUsbOutComplete;
48
49         return;
50 }
51
52 u32_t zfwUsbGetFreeTxQSize(zdev_t *dev)
53 {
54         struct usbdrv_private *macp = dev->ml_priv;
55         u32_t           freeTxQSize;
56         unsigned long irqFlag;
57         /* zmw_declare_for_critical_section();  */
58
59         /* zmw_enter_critical_section(dev);     */
60         spin_lock_irqsave(&macp->cs_lock, irqFlag);
61
62         freeTxQSize = ZM_MAX_TX_BUF_NUM - macp->TxBufCnt;
63
64         /* zmw_leave_critical_section(dev);     */
65         spin_unlock_irqrestore(&macp->cs_lock, irqFlag);
66
67         return freeTxQSize;
68 }
69
70 u32_t zfwUsbGetMaxTxQSize(zdev_t *dev)
71 {
72         return ZM_MAX_TX_BUF_NUM;
73 }
74
75 u32_t zfwUsbEnableIntEpt(zdev_t *dev, u8_t endpt)
76 {
77         /* Initialize USB TxQ */
78         zfLnxInitUsbTxQ(dev);
79
80         /* Initialize USB RxQ */
81         zfLnxInitUsbRxQ(dev);
82
83         /* Initialize USB Register In URB */
84         /* zfwUsbSubmitRegIn(dev);      */
85         /* Initialize USB Register In URB */
86         zfLnxSubmitRegInUrb(dev);
87
88         return 0;
89 }
90
91 int zfwUsbEnableRxEpt(zdev_t *dev, u8_t endpt)
92 {
93         return 0;
94 }
95
96 u32_t zfwUsbSubmitControl(zdev_t *dev, u8_t req, u16_t value, u16_t index,
97                          void *data, u32_t size)
98 {
99         int result = 0;
100         u32_t ret = 0;
101         struct usbdrv_private *macp = dev->ml_priv;
102         u8_t *buf;
103
104         if (size > 0) {
105                 buf = kmalloc(size, GFP_KERNEL);
106                 memcpy(buf, (u8_t *)data, size);
107         } else
108                 buf = NULL;
109
110 #if 0
111         printk(KERN_ERR "req = 0x%02x\n", req);
112         printk(KERN_ERR "value = 0x%04x\n", value);
113         printk(KERN_ERR "index = 0x%04x\n", index);
114         printk(KERN_ERR "data = 0x%lx\n", (u32_t) data);
115         printk(KERN_ERR "size = %ld\n", size);
116 #endif
117
118         result = usb_control_msg(macp->udev, usb_sndctrlpipe(macp->udev, 0),
119                         req, USB_DIR_OUT | 0x40, value, index, buf, size, HZ);
120
121         if (result < 0) {
122                 printk(KERN_ERR "zfwUsbSubmitControl() failed, result = 0x%x\n",
123                         result);
124                 ret = 1;
125         }
126         kfree(buf);
127
128         return ret;
129 }
130
131 void zfwUsbCmd(zdev_t *dev, u8_t endpt, u32_t *cmd, u16_t cmdLen)
132 {
133         struct usbdrv_private *macp = dev->ml_priv;
134         u32_t ret;
135
136         /* MPUsbCommand(dev, endpt, cmd, cmdLen);       */
137         ret = zfLnxUsbWriteReg(dev, cmd, cmdLen);
138
139         /*
140          * if zfLnxUsbWriteReg() return error, free and allocate urb,
141          * resend again
142          */
143         if (ret != 0) {
144                 usb_free_urb(macp->RegOutUrb);
145                 macp->RegOutUrb = usb_alloc_urb(0, GFP_ATOMIC);
146                 ret = zfLnxUsbWriteReg(dev, cmd, cmdLen);
147         }
148 }
149
150 u32_t zfwUsbSend(zdev_t *dev, u8_t endpt, u8_t *hdr, u16_t hdrlen, u8_t *snap,
151                         u16_t snapLen,  u8_t *tail, u16_t tailLen,
152                         zbuf_t *buf, u16_t offset)
153 {
154         u32_t status;
155
156 #ifdef ZM_CONFIG_BIG_ENDIAN
157         u32_t ii = 0;
158         u16_t *pc = NULL;
159
160         pc = (u16_t *)hdr;
161         for (ii = 0; ii < (hdrlen >> 1); ii++)
162                 pc[ii] = cpu_to_le16(pc[ii]);
163
164         pc = (u16_t *)snap;
165         for (ii = 0; ii < (snapLen >> 1); ii++)
166                 pc[ii] = cpu_to_le16(pc[ii]);
167
168         pc = (u16_t *)tail;
169         for (ii = 0; ii < (tailLen>>1); ii++)
170                 pc[ii] = cpu_to_le16(pc[ii]);
171 #endif
172
173         status = zfLnxUsbOut(dev, hdr, hdrlen, snap, snapLen, tail, tailLen,
174                                 buf, offset);
175         if (status == 0)
176                 return 0;
177         else
178                 return 1;
179 }
180
181 /* Leave an empty line below to remove warning message on some compiler */