0ea23519956e824bb705978e46b883f24b4c3b28
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rtl8723bu / hal / hal_hci / hal_usb.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *                                        
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _HAL_USB_C_
21
22 #include <drv_types.h>
23 #include <hal_data.h>
24
25
26 #ifdef CONFIG_USB_SUPPORT_ASYNC_VDN_REQ
27 int usb_write_async(struct usb_device *udev, u32 addr, void *pdata, u16 len)
28 {
29         u8 request;
30         u8 requesttype;
31         u16 wvalue;
32         u16 index;
33         int ret;
34
35         requesttype = VENDOR_WRITE;//write_out
36         request = REALTEK_USB_VENQT_CMD_REQ;
37         index = REALTEK_USB_VENQT_CMD_IDX;//n/a
38
39         wvalue = (u16)(addr&0x0000ffff);
40
41         ret = _usbctrl_vendorreq_async_write(udev, request, wvalue, index, pdata, len, requesttype);
42
43         return ret;
44 }
45
46 int usb_async_write8(struct intf_hdl *pintfhdl, u32 addr, u8 val)
47 {
48         u8 data;
49         int ret;
50         struct dvobj_priv  *pdvobjpriv = (struct dvobj_priv  *)pintfhdl->pintf_dev;
51         struct usb_device *udev=pdvobjpriv->pusbdev;
52
53         _func_enter_;
54         data = val;
55         ret = usb_write_async(udev, addr, &data, 1);
56         _func_exit_;
57
58         return ret;
59 }
60
61 int usb_async_write16(struct intf_hdl *pintfhdl, u32 addr, u16 val)
62 {
63         u16 data;
64         int ret;
65         struct dvobj_priv  *pdvobjpriv = (struct dvobj_priv  *)pintfhdl->pintf_dev;
66         struct usb_device *udev=pdvobjpriv->pusbdev;
67
68         _func_enter_;
69         data = val;
70         ret = usb_write_async(udev, addr, &data, 2);
71         _func_exit_;
72
73         return ret;
74 }
75
76 int usb_async_write32(struct intf_hdl *pintfhdl, u32 addr, u32 val)
77 {
78         u32 data;
79         int ret;
80         struct dvobj_priv  *pdvobjpriv = (struct dvobj_priv  *)pintfhdl->pintf_dev;
81         struct usb_device *udev=pdvobjpriv->pusbdev;
82
83         _func_enter_;
84         data = val;
85         ret = usb_write_async(udev, addr, &data, 4);
86         _func_exit_;
87         
88         return ret;
89 }
90 #endif /* CONFIG_USB_SUPPORT_ASYNC_VDN_REQ */
91
92
93
94 #ifdef CONFIG_RTL8192D
95 /*      This function only works in 92DU chip.          */
96 void usb_read_reg_rf_byfw(struct intf_hdl *pintfhdl, 
97                                 u16 byteCount, 
98                                 u32 registerIndex, 
99                                 PVOID buffer)
100 {
101         u16     wPage = 0x0000, offset;
102         u32     BufferLengthRead;
103         PADAPTER        Adapter = pintfhdl->padapter;
104         HAL_DATA_TYPE   *pHalData = GET_HAL_DATA(Adapter);
105         u8      RFPath=0,nPHY=0;
106
107         RFPath =(u8) ((registerIndex&0xff0000)>>16);
108         
109         if (pHalData->interfaceIndex!=0)
110         { 
111                 nPHY = 1; //MAC1
112                 if(registerIndex&MAC1_ACCESS_PHY0)// MAC1 need to access PHY0
113                         nPHY = 0;
114         }
115         else
116         {
117                 if(registerIndex&MAC0_ACCESS_PHY1)
118                         nPHY = 1;
119         }
120         registerIndex &= 0xFF;
121         wPage = ((nPHY<<7)|(RFPath<<5)|8)<<8;
122         offset = (u16)registerIndex;
123         
124         //
125         // IN a vendor request to read back MAC register.
126         //
127         usbctrl_vendorreq(pintfhdl, 0x05, offset, wPage, buffer, byteCount, 0x01);
128
129 }
130 #endif
131
132 /*
133         92DU chip needs to remask "value" parameter,  this function only works in 92DU chip.
134 */
135 static inline void usb_value_remask(struct intf_hdl *pintfhdl, u16 *value)
136 {
137 #ifdef CONFIG_RTL8192D
138         _adapter        *padapter = pintfhdl->padapter;
139         HAL_DATA_TYPE   *pHalData = GET_HAL_DATA(padapter);
140
141         if ((IS_HARDWARE_TYPE_8192DU(padapter)) && (pHalData->interfaceIndex!=0))
142         {
143                 if(*value<0x1000)
144                         *value|=0x4000;
145                 else if ((*value&MAC1_ACCESS_PHY0) && !(*value&0x8000))   // MAC1 need to access PHY0
146                         *value &= 0xFFF;
147         }
148 #endif
149 }
150
151 u8 usb_read8(struct intf_hdl *pintfhdl, u32 addr)
152 {
153         u8 request;
154         u8 requesttype;
155         u16 wvalue;
156         u16 index;
157         u16 len;
158         u8 data=0;
159         
160         _func_enter_;
161
162         request = 0x05;
163         requesttype = 0x01;//read_in
164         index = 0;//n/a
165
166         wvalue = (u16)(addr&0x0000ffff);
167         len = 1;        
168         usb_value_remask(pintfhdl, &wvalue);
169         usbctrl_vendorreq(pintfhdl, request, wvalue, index,
170                                         &data, len, requesttype);
171
172         _func_exit_;
173
174         return data;    
175 }
176
177 u16 usb_read16(struct intf_hdl *pintfhdl, u32 addr)
178 {       
179         u8 request;
180         u8 requesttype;
181         u16 wvalue;
182         u16 index;
183         u16 len;
184         u16 data=0;
185         
186         _func_enter_;
187
188         request = 0x05;
189         requesttype = 0x01;//read_in
190         index = 0;//n/a
191
192         wvalue = (u16)(addr&0x0000ffff);
193         len = 2;        
194         usb_value_remask(pintfhdl, &wvalue);
195         usbctrl_vendorreq(pintfhdl, request, wvalue, index,
196                                         &data, len, requesttype);
197
198         _func_exit_;
199
200         return data;
201         
202 }
203
204 u32 usb_read32(struct intf_hdl *pintfhdl, u32 addr)
205 {
206         u8 request;
207         u8 requesttype;
208         u16 wvalue;
209         u16 index;
210         u16 len;
211         u32 data=0;
212         
213         _func_enter_;
214
215         request = 0x05;
216         requesttype = 0x01;//read_in
217         index = 0;//n/a
218
219         wvalue = (u16)(addr&0x0000ffff);
220         len = 4;
221 #ifdef CONFIG_RTL8192D
222         if ((IS_HARDWARE_TYPE_8192DU(pintfhdl->padapter)) && ((addr&0xff000000)>>24 == 0x66)) {
223                 usb_read_reg_rf_byfw(pintfhdl, len, addr, &data);
224         } else 
225 #endif
226         {
227                 usb_value_remask(pintfhdl, &wvalue);
228                 usbctrl_vendorreq(pintfhdl, request, wvalue, index,
229                                                 &data, len, requesttype);
230         }
231
232         _func_exit_;
233
234         return data;
235 }
236
237 int usb_write8(struct intf_hdl *pintfhdl, u32 addr, u8 val)
238 {
239         u8 request;
240         u8 requesttype;
241         u16 wvalue;
242         u16 index;
243         u16 len;
244         u8 data;
245         int ret;
246         
247         _func_enter_;
248
249         request = 0x05;
250         requesttype = 0x00;//write_out
251         index = 0;//n/a
252
253         wvalue = (u16)(addr&0x0000ffff);
254         len = 1;
255         
256         data = val;     
257         usb_value_remask(pintfhdl, &wvalue);
258         ret = usbctrl_vendorreq(pintfhdl, request, wvalue, index,
259                                                 &data, len, requesttype);
260         
261         _func_exit_;
262         
263         return ret;
264 }
265
266 int usb_write16(struct intf_hdl *pintfhdl, u32 addr, u16 val)
267 {       
268         u8 request;
269         u8 requesttype;
270         u16 wvalue;
271         u16 index;
272         u16 len;
273         u16 data;
274         int ret;
275         
276         _func_enter_;
277
278         request = 0x05;
279         requesttype = 0x00;//write_out
280         index = 0;//n/a
281
282         wvalue = (u16)(addr&0x0000ffff);
283         len = 2;
284         
285         data = val;
286         usb_value_remask(pintfhdl, &wvalue);
287         ret = usbctrl_vendorreq(pintfhdl, request, wvalue, index,
288                                                 &data, len, requesttype);
289         
290         _func_exit_;
291         
292         return ret;
293         
294 }
295
296 int usb_write32(struct intf_hdl *pintfhdl, u32 addr, u32 val)
297 {
298         u8 request;
299         u8 requesttype;
300         u16 wvalue;
301         u16 index;
302         u16 len;
303         u32 data;
304         int ret;
305         
306         _func_enter_;
307
308         request = 0x05;
309         requesttype = 0x00;//write_out
310         index = 0;//n/a
311
312         wvalue = (u16)(addr&0x0000ffff);
313         len = 4;
314         data =val;              
315         usb_value_remask(pintfhdl, &wvalue);
316         ret = usbctrl_vendorreq(pintfhdl, request, wvalue, index,
317                                                 &data, len, requesttype);
318
319         _func_exit_;
320         
321         return ret;
322         
323 }
324
325 int usb_writeN(struct intf_hdl *pintfhdl, u32 addr, u32 length, u8 *pdata)
326 {
327         u8 request;
328         u8 requesttype;
329         u16 wvalue;
330         u16 index;
331         u16 len;
332         u8 buf[VENDOR_CMD_MAX_DATA_LEN]={0};
333         int ret;
334         
335         _func_enter_;
336
337         request = 0x05;
338         requesttype = 0x00;//write_out
339         index = 0;//n/a
340
341         wvalue = (u16)(addr&0x0000ffff);
342         len = length;
343          _rtw_memcpy(buf, pdata, len );
344         usb_value_remask(pintfhdl, &wvalue);
345         ret = usbctrl_vendorreq(pintfhdl, request, wvalue, index,
346                                                 buf, len, requesttype);
347         
348         _func_exit_;
349         
350         return ret;
351         
352 }