Merge git://git.kernel.org/pub/scm/linux/kernel/git/lethal/sh-2.6
[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 <linux/slab.h>
32 #include <net/iw_handler.h>
33
34 extern void zfLnxInitUsbTxQ(zdev_t *dev);
35 extern void zfLnxInitUsbRxQ(zdev_t *dev);
36 extern u32_t zfLnxSubmitRegInUrb(zdev_t *dev);
37 u32_t zfLnxUsbOut(zdev_t *dev, u8_t *hdr, u16_t hdrlen, u8_t *snap,
38                 u16_t snapLen, u8_t *tail, u16_t tailLen, zbuf_t *buf,
39                 u16_t offset);
40 u32_t zfLnxUsbWriteReg(zdev_t *dev, u32_t *cmd, u16_t cmdLen);
41
42 void zfwUsbRegisterCallBack(zdev_t *dev, struct zfCbUsbFuncTbl *zfUsbFunc)
43 {
44         struct usbdrv_private *macp = dev->ml_priv;
45
46         macp->usbCbFunctions.zfcbUsbRecv = zfUsbFunc->zfcbUsbRecv;
47         macp->usbCbFunctions.zfcbUsbRegIn = zfUsbFunc->zfcbUsbRegIn;
48         macp->usbCbFunctions.zfcbUsbOutComplete = zfUsbFunc->zfcbUsbOutComplete;
49
50         return;
51 }
52
53 u32_t zfwUsbGetFreeTxQSize(zdev_t *dev)
54 {
55         struct usbdrv_private *macp = dev->ml_priv;
56         u32_t           freeTxQSize;
57         unsigned long irqFlag;
58         /* zmw_declare_for_critical_section();  */
59
60         /* zmw_enter_critical_section(dev);     */
61         spin_lock_irqsave(&macp->cs_lock, irqFlag);
62
63         freeTxQSize = ZM_MAX_TX_BUF_NUM - macp->TxBufCnt;
64
65         /* zmw_leave_critical_section(dev);     */
66         spin_unlock_irqrestore(&macp->cs_lock, irqFlag);
67
68         return freeTxQSize;
69 }
70
71 u32_t zfwUsbGetMaxTxQSize(zdev_t *dev)
72 {
73         return ZM_MAX_TX_BUF_NUM;
74 }
75
76 u32_t zfwUsbEnableIntEpt(zdev_t *dev, u8_t endpt)
77 {
78         /* Initialize USB TxQ */
79         zfLnxInitUsbTxQ(dev);
80
81         /* Initialize USB RxQ */
82         zfLnxInitUsbRxQ(dev);
83
84         /* Initialize USB Register In URB */
85         /* zfwUsbSubmitRegIn(dev);      */
86         /* Initialize USB Register In URB */
87         zfLnxSubmitRegInUrb(dev);
88
89         return 0;
90 }
91
92 int zfwUsbEnableRxEpt(zdev_t *dev, u8_t endpt)
93 {
94         return 0;
95 }
96
97 u32_t zfwUsbSubmitControl(zdev_t *dev, u8_t req, u16_t value, u16_t index,
98                          void *data, u32_t size)
99 {
100         int result = 0;
101         u32_t ret = 0;
102         struct usbdrv_private *macp = dev->ml_priv;
103         u8_t *buf;
104
105         if (size > 0) {
106                 buf = kmalloc(size, GFP_KERNEL);
107                 if (buf == NULL) {
108                         pr_err("zfwUsbSubmitControl() failed, "
109                                   "kmalloc() returned NULL\n");
110                         return 1;
111                 }
112                 memcpy(buf, (u8_t *)data, size);
113         } else
114                 buf = NULL;
115
116 #if 0
117         printk(KERN_ERR "req = 0x%02x\n", req);
118         printk(KERN_ERR "value = 0x%04x\n", value);
119         printk(KERN_ERR "index = 0x%04x\n", index);
120         printk(KERN_ERR "data = 0x%lx\n", (u32_t) data);
121         printk(KERN_ERR "size = %ld\n", size);
122 #endif
123
124         result = usb_control_msg(macp->udev, usb_sndctrlpipe(macp->udev, 0),
125                         req, USB_DIR_OUT | 0x40, value, index, buf, size, HZ);
126
127         if (result < 0) {
128                 printk(KERN_ERR "zfwUsbSubmitControl() failed, result = 0x%x\n",
129                         result);
130                 ret = 1;
131         }
132         kfree(buf);
133
134         return ret;
135 }
136
137 void zfwUsbCmd(zdev_t *dev, u8_t endpt, u32_t *cmd, u16_t cmdLen)
138 {
139         struct usbdrv_private *macp = dev->ml_priv;
140         u32_t ret;
141
142         /* MPUsbCommand(dev, endpt, cmd, cmdLen);       */
143         ret = zfLnxUsbWriteReg(dev, cmd, cmdLen);
144
145         /*
146          * if zfLnxUsbWriteReg() return error, free and allocate urb,
147          * resend again
148          */
149         if (ret != 0) {
150                 usb_free_urb(macp->RegOutUrb);
151                 macp->RegOutUrb = usb_alloc_urb(0, GFP_ATOMIC);
152                 ret = zfLnxUsbWriteReg(dev, cmd, cmdLen);
153         }
154 }
155
156 u32_t zfwUsbSend(zdev_t *dev, u8_t endpt, u8_t *hdr, u16_t hdrlen, u8_t *snap,
157                         u16_t snapLen,  u8_t *tail, u16_t tailLen,
158                         zbuf_t *buf, u16_t offset)
159 {
160         u32_t status;
161
162 #ifdef ZM_CONFIG_BIG_ENDIAN
163         u32_t ii = 0;
164         u16_t *pc = NULL;
165
166         pc = (u16_t *)hdr;
167         for (ii = 0; ii < (hdrlen >> 1); ii++)
168                 pc[ii] = cpu_to_le16(pc[ii]);
169
170         pc = (u16_t *)snap;
171         for (ii = 0; ii < (snapLen >> 1); ii++)
172                 pc[ii] = cpu_to_le16(pc[ii]);
173
174         pc = (u16_t *)tail;
175         for (ii = 0; ii < (tailLen>>1); ii++)
176                 pc[ii] = cpu_to_le16(pc[ii]);
177 #endif
178
179         status = zfLnxUsbOut(dev, hdr, hdrlen, snap, snapLen, tail, tailLen,
180                                 buf, offset);
181         if (status == 0)
182                 return 0;
183         else
184                 return 1;
185 }
186
187 /* Leave an empty line below to remove warning message on some compiler */