• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  * usb_ops_linux.c
3  *
4  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5  * Linux device driver for RTL8192SU
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  *
20  * Modifications for inclusion into the Linux staging tree are
21  * Copyright(c) 2010 Larry Finger. All rights reserved.
22  *
23  * Contact information:
24  * WLAN FAE <wlanfae@realtek.com>
25  * Larry Finger <Larry.Finger@lwfinger.net>
26  *
27  ******************************************************************************/
28 
29 #define _HCI_OPS_OS_C_
30 
31 #include <linux/usb.h>
32 
33 #include "osdep_service.h"
34 #include "drv_types.h"
35 #include "osdep_intf.h"
36 #include "usb_ops.h"
37 
38 #define	RTL871X_VENQT_READ	0xc0
39 #define	RTL871X_VENQT_WRITE	0x40
40 
41 struct zero_bulkout_context {
42 	void *pbuf;
43 	void *purb;
44 	void *pirp;
45 	void *padapter;
46 };
47 
r8712_usb_init_intf_priv(struct intf_priv * pintfpriv)48 uint r8712_usb_init_intf_priv(struct intf_priv *pintfpriv)
49 {
50 	pintfpriv->piorw_urb = usb_alloc_urb(0, GFP_ATOMIC);
51 	if (!pintfpriv->piorw_urb)
52 		return _FAIL;
53 	init_completion(&pintfpriv->io_retevt_comp);
54 	return _SUCCESS;
55 }
56 
r8712_usb_unload_intf_priv(struct intf_priv * pintfpriv)57 void r8712_usb_unload_intf_priv(struct intf_priv *pintfpriv)
58 {
59 	if (pintfpriv->piorw_urb) {
60 		usb_kill_urb(pintfpriv->piorw_urb);
61 		usb_free_urb(pintfpriv->piorw_urb);
62 	}
63 }
64 
ffaddr2pipehdl(struct dvobj_priv * pdvobj,u32 addr)65 static unsigned int ffaddr2pipehdl(struct dvobj_priv *pdvobj, u32 addr)
66 {
67 	unsigned int pipe = 0;
68 	struct usb_device *pusbd = pdvobj->pusbdev;
69 
70 	if (pdvobj->nr_endpoint == 11) {
71 		switch (addr) {
72 		case RTL8712_DMA_BKQ:
73 			pipe = usb_sndbulkpipe(pusbd, 0x07);
74 			break;
75 		case RTL8712_DMA_BEQ:
76 			pipe = usb_sndbulkpipe(pusbd, 0x06);
77 			break;
78 		case RTL8712_DMA_VIQ:
79 			pipe = usb_sndbulkpipe(pusbd, 0x05);
80 			break;
81 		case RTL8712_DMA_VOQ:
82 			pipe = usb_sndbulkpipe(pusbd, 0x04);
83 			break;
84 		case RTL8712_DMA_BCNQ:
85 			pipe = usb_sndbulkpipe(pusbd, 0x0a);
86 			break;
87 		case RTL8712_DMA_BMCQ:	/* HI Queue */
88 			pipe = usb_sndbulkpipe(pusbd, 0x0b);
89 			break;
90 		case RTL8712_DMA_MGTQ:
91 			pipe = usb_sndbulkpipe(pusbd, 0x0c);
92 			break;
93 		case RTL8712_DMA_RX0FF:
94 			pipe = usb_rcvbulkpipe(pusbd, 0x03); /* in */
95 			break;
96 		case RTL8712_DMA_C2HCMD:
97 			pipe = usb_rcvbulkpipe(pusbd, 0x09); /* in */
98 			break;
99 		case RTL8712_DMA_H2CCMD:
100 			pipe = usb_sndbulkpipe(pusbd, 0x0d);
101 			break;
102 		}
103 	} else if (pdvobj->nr_endpoint == 6) {
104 		switch (addr) {
105 		case RTL8712_DMA_BKQ:
106 			pipe = usb_sndbulkpipe(pusbd, 0x07);
107 			break;
108 		case RTL8712_DMA_BEQ:
109 			pipe = usb_sndbulkpipe(pusbd, 0x06);
110 			break;
111 		case RTL8712_DMA_VIQ:
112 			pipe = usb_sndbulkpipe(pusbd, 0x05);
113 			break;
114 		case RTL8712_DMA_VOQ:
115 			pipe = usb_sndbulkpipe(pusbd, 0x04);
116 			break;
117 		case RTL8712_DMA_RX0FF:
118 		case RTL8712_DMA_C2HCMD:
119 			pipe = usb_rcvbulkpipe(pusbd, 0x03); /* in */
120 			break;
121 		case RTL8712_DMA_H2CCMD:
122 		case RTL8712_DMA_BCNQ:
123 		case RTL8712_DMA_BMCQ:
124 		case RTL8712_DMA_MGTQ:
125 			pipe = usb_sndbulkpipe(pusbd, 0x0d);
126 			break;
127 		}
128 	} else if (pdvobj->nr_endpoint == 4) {
129 		switch (addr) {
130 		case RTL8712_DMA_BEQ:
131 			pipe = usb_sndbulkpipe(pusbd, 0x06);
132 			break;
133 		case RTL8712_DMA_VOQ:
134 			pipe = usb_sndbulkpipe(pusbd, 0x04);
135 			break;
136 		case RTL8712_DMA_RX0FF:
137 		case RTL8712_DMA_C2HCMD:
138 			pipe = usb_rcvbulkpipe(pusbd, 0x03); /* in */
139 			break;
140 		case RTL8712_DMA_H2CCMD:
141 		case RTL8712_DMA_BCNQ:
142 		case RTL8712_DMA_BMCQ:
143 		case RTL8712_DMA_MGTQ:
144 			pipe = usb_sndbulkpipe(pusbd, 0x0d);
145 			break;
146 		}
147 	} else {
148 	   pipe = 0;
149 	}
150 	return pipe;
151 }
152 
usb_write_mem_complete(struct urb * purb)153 static void usb_write_mem_complete(struct urb *purb)
154 {
155 	struct io_queue *pio_q = (struct io_queue *)purb->context;
156 	struct intf_hdl *pintf = &(pio_q->intf);
157 	struct intf_priv *pintfpriv = pintf->pintfpriv;
158 	struct _adapter *padapter = (struct _adapter *)pintf->adapter;
159 
160 	if (purb->status != 0) {
161 		if (purb->status == (-ESHUTDOWN))
162 			padapter->bDriverStopped = true;
163 		else
164 			padapter->bSurpriseRemoved = true;
165 	}
166 	complete(&pintfpriv->io_retevt_comp);
167 }
168 
r8712_usb_write_mem(struct intf_hdl * pintfhdl,u32 addr,u32 cnt,u8 * wmem)169 void r8712_usb_write_mem(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *wmem)
170 {
171 	unsigned int pipe;
172 	struct _adapter *padapter = (struct _adapter *)pintfhdl->adapter;
173 	struct intf_priv *pintfpriv = pintfhdl->pintfpriv;
174 	struct io_queue *pio_queue = padapter->pio_queue;
175 	struct dvobj_priv *pdvobj = (struct dvobj_priv *)pintfpriv->intf_dev;
176 	struct usb_device *pusbd = pdvobj->pusbdev;
177 	struct urb *piorw_urb = pintfpriv->piorw_urb;
178 
179 	if ((padapter->bDriverStopped) || (padapter->bSurpriseRemoved) ||
180 	    (padapter->pwrctrlpriv.pnp_bstop_trx))
181 		return;
182 	/* translate DMA FIFO addr to pipehandle */
183 	pipe = ffaddr2pipehdl(pdvobj, addr);
184 	if (pipe == 0)
185 		return;
186 	usb_fill_bulk_urb(piorw_urb, pusbd, pipe,
187 			  wmem, cnt, usb_write_mem_complete,
188 			  pio_queue);
189 	usb_submit_urb(piorw_urb, GFP_ATOMIC);
190 	wait_for_completion_interruptible(&pintfpriv->io_retevt_comp);
191 }
192 
r8712_usb_read_port_complete(struct urb * purb)193 static void r8712_usb_read_port_complete(struct urb *purb)
194 {
195 	uint isevt, *pbuf;
196 	struct recv_buf	*precvbuf = (struct recv_buf *)purb->context;
197 	struct _adapter *padapter = (struct _adapter *)precvbuf->adapter;
198 	struct recv_priv *precvpriv = &padapter->recvpriv;
199 
200 	if (padapter->bSurpriseRemoved || padapter->bDriverStopped)
201 		return;
202 	if (purb->status == 0) { /* SUCCESS */
203 		if ((purb->actual_length > (MAX_RECVBUF_SZ)) ||
204 		    (purb->actual_length < RXDESC_SIZE)) {
205 			r8712_read_port(padapter, precvpriv->ff_hwaddr, 0,
206 				  (unsigned char *)precvbuf);
207 		} else {
208 			_pkt *pskb = precvbuf->pskb;
209 
210 			precvbuf->transfer_len = purb->actual_length;
211 			pbuf = (uint *)precvbuf->pbuf;
212 			isevt = le32_to_cpu(*(pbuf + 1)) & 0x1ff;
213 			if ((isevt & 0x1ff) == 0x1ff) {
214 				r8712_rxcmd_event_hdl(padapter, pbuf);
215 				skb_queue_tail(&precvpriv->rx_skb_queue, pskb);
216 				r8712_read_port(padapter, precvpriv->ff_hwaddr,
217 						0, (unsigned char *)precvbuf);
218 			} else {
219 				skb_put(pskb, purb->actual_length);
220 				skb_queue_tail(&precvpriv->rx_skb_queue, pskb);
221 				tasklet_hi_schedule(&precvpriv->recv_tasklet);
222 				r8712_read_port(padapter, precvpriv->ff_hwaddr,
223 						0, (unsigned char *)precvbuf);
224 			}
225 		}
226 	} else {
227 		switch (purb->status) {
228 		case -EINVAL:
229 		case -EPIPE:
230 		case -ENODEV:
231 		case -ESHUTDOWN:
232 			padapter->bDriverStopped = true;
233 			break;
234 		case -ENOENT:
235 			if (!padapter->bSuspended) {
236 				padapter->bDriverStopped = true;
237 				break;
238 			}
239 			/* Fall through. */
240 		case -EPROTO:
241 			r8712_read_port(padapter, precvpriv->ff_hwaddr, 0,
242 				  (unsigned char *)precvbuf);
243 			break;
244 		case -EINPROGRESS:
245 			netdev_err(padapter->pnetdev, "ERROR: URB IS IN PROGRESS!\n");
246 			break;
247 		default:
248 			break;
249 		}
250 	}
251 }
252 
r8712_usb_read_port(struct intf_hdl * pintfhdl,u32 addr,u32 cnt,u8 * rmem)253 u32 r8712_usb_read_port(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *rmem)
254 {
255 	unsigned int pipe;
256 	int err;
257 	u32 tmpaddr = 0;
258 	int alignment = 0;
259 	u32 ret = _SUCCESS;
260 	struct urb *purb = NULL;
261 	struct recv_buf	*precvbuf = (struct recv_buf *)rmem;
262 	struct intf_priv *pintfpriv = pintfhdl->pintfpriv;
263 	struct dvobj_priv *pdvobj = (struct dvobj_priv *)pintfpriv->intf_dev;
264 	struct _adapter *adapter = pdvobj->padapter;
265 	struct recv_priv *precvpriv = &adapter->recvpriv;
266 	struct usb_device *pusbd = pdvobj->pusbdev;
267 
268 	if (adapter->bDriverStopped || adapter->bSurpriseRemoved ||
269 	    adapter->pwrctrlpriv.pnp_bstop_trx || !precvbuf)
270 		return _FAIL;
271 	r8712_init_recvbuf(adapter, precvbuf);
272 	/* Try to use skb from the free queue */
273 	precvbuf->pskb = skb_dequeue(&precvpriv->free_recv_skb_queue);
274 
275 	if (!precvbuf->pskb) {
276 		precvbuf->pskb = netdev_alloc_skb(adapter->pnetdev,
277 				 MAX_RECVBUF_SZ + RECVBUFF_ALIGN_SZ);
278 		if (!precvbuf->pskb)
279 			return _FAIL;
280 		tmpaddr = (addr_t)precvbuf->pskb->data;
281 		alignment = tmpaddr & (RECVBUFF_ALIGN_SZ - 1);
282 		skb_reserve(precvbuf->pskb,
283 			    (RECVBUFF_ALIGN_SZ - alignment));
284 		precvbuf->phead = precvbuf->pskb->head;
285 		precvbuf->pdata = precvbuf->pskb->data;
286 		precvbuf->ptail = skb_tail_pointer(precvbuf->pskb);
287 		precvbuf->pend = skb_end_pointer(precvbuf->pskb);
288 		precvbuf->pbuf = precvbuf->pskb->data;
289 	} else { /* skb is reused */
290 		precvbuf->phead = precvbuf->pskb->head;
291 		precvbuf->pdata = precvbuf->pskb->data;
292 		precvbuf->ptail = skb_tail_pointer(precvbuf->pskb);
293 		precvbuf->pend = skb_end_pointer(precvbuf->pskb);
294 		precvbuf->pbuf = precvbuf->pskb->data;
295 	}
296 	purb = precvbuf->purb;
297 	/* translate DMA FIFO addr to pipehandle */
298 	pipe = ffaddr2pipehdl(pdvobj, addr);
299 	usb_fill_bulk_urb(purb, pusbd, pipe,
300 			  precvbuf->pbuf, MAX_RECVBUF_SZ,
301 			  r8712_usb_read_port_complete,
302 			  precvbuf);
303 	err = usb_submit_urb(purb, GFP_ATOMIC);
304 	if ((err) && (err != (-EPERM)))
305 		ret = _FAIL;
306 	return ret;
307 }
308 
r8712_usb_read_port_cancel(struct _adapter * padapter)309 void r8712_usb_read_port_cancel(struct _adapter *padapter)
310 {
311 	int i;
312 	struct recv_buf *precvbuf;
313 
314 	precvbuf = (struct recv_buf *)padapter->recvpriv.precv_buf;
315 	for (i = 0; i < NR_RECVBUFF; i++) {
316 		if (precvbuf->purb)
317 			usb_kill_urb(precvbuf->purb);
318 		precvbuf++;
319 	}
320 }
321 
r8712_xmit_bh(void * priv)322 void r8712_xmit_bh(void *priv)
323 {
324 	int ret = false;
325 	struct _adapter *padapter = priv;
326 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
327 
328 	if (padapter->bDriverStopped ||
329 	    padapter->bSurpriseRemoved) {
330 		netdev_err(padapter->pnetdev, "xmit_bh => bDriverStopped or bSurpriseRemoved\n");
331 		return;
332 	}
333 	ret = r8712_xmitframe_complete(padapter, pxmitpriv, NULL);
334 	if (!ret)
335 		return;
336 	tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
337 }
338 
usb_write_port_complete(struct urb * purb)339 static void usb_write_port_complete(struct urb *purb)
340 {
341 	int i;
342 	struct xmit_frame *pxmitframe = (struct xmit_frame *)purb->context;
343 	struct xmit_buf *pxmitbuf = pxmitframe->pxmitbuf;
344 	struct _adapter *padapter = pxmitframe->padapter;
345 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
346 	struct pkt_attrib *pattrib = &pxmitframe->attrib;
347 
348 	switch (pattrib->priority) {
349 	case 1:
350 	case 2:
351 		pxmitpriv->bkq_cnt--;
352 		break;
353 	case 4:
354 	case 5:
355 		pxmitpriv->viq_cnt--;
356 		break;
357 	case 6:
358 	case 7:
359 		pxmitpriv->voq_cnt--;
360 		break;
361 	case 0:
362 	case 3:
363 	default:
364 		pxmitpriv->beq_cnt--;
365 		break;
366 	}
367 	pxmitpriv->txirp_cnt--;
368 	for (i = 0; i < 8; i++) {
369 		if (purb == pxmitframe->pxmit_urb[i]) {
370 			pxmitframe->bpending[i] = false;
371 			break;
372 		}
373 	}
374 	if (padapter->bSurpriseRemoved)
375 		return;
376 	switch (purb->status) {
377 	case 0:
378 		break;
379 	default:
380 		netdev_warn(padapter->pnetdev,
381 				"r8712u: pipe error: (%d)\n", purb->status);
382 		break;
383 	}
384 	/* not to consider tx fragment */
385 	r8712_free_xmitframe_ex(pxmitpriv, pxmitframe);
386 	r8712_free_xmitbuf(pxmitpriv, pxmitbuf);
387 	tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
388 }
389 
r8712_usb_write_port(struct intf_hdl * pintfhdl,u32 addr,u32 cnt,u8 * wmem)390 u32 r8712_usb_write_port(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *wmem)
391 {
392 	unsigned long irqL;
393 	int i, status;
394 	unsigned int pipe;
395 	u32 ret, bwritezero;
396 	struct urb *purb = NULL;
397 	struct _adapter *padapter = (struct _adapter *)pintfhdl->adapter;
398 	struct dvobj_priv *pdvobj = &padapter->dvobjpriv;
399 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
400 	struct xmit_frame *pxmitframe = (struct xmit_frame *)wmem;
401 	struct usb_device *pusbd = pdvobj->pusbdev;
402 	struct pkt_attrib *pattrib = &pxmitframe->attrib;
403 
404 	if ((padapter->bDriverStopped) || (padapter->bSurpriseRemoved) ||
405 	    (padapter->pwrctrlpriv.pnp_bstop_trx))
406 		return _FAIL;
407 	for (i = 0; i < 8; i++) {
408 		if (!pxmitframe->bpending[i]) {
409 			spin_lock_irqsave(&pxmitpriv->lock, irqL);
410 			pxmitpriv->txirp_cnt++;
411 			pxmitframe->bpending[i]  = true;
412 			switch (pattrib->priority) {
413 			case 1:
414 			case 2:
415 				pxmitpriv->bkq_cnt++;
416 				break;
417 			case 4:
418 			case 5:
419 				pxmitpriv->viq_cnt++;
420 				break;
421 			case 6:
422 			case 7:
423 				pxmitpriv->voq_cnt++;
424 				break;
425 			case 0:
426 			case 3:
427 			default:
428 				pxmitpriv->beq_cnt++;
429 				break;
430 			}
431 			spin_unlock_irqrestore(&pxmitpriv->lock, irqL);
432 			pxmitframe->sz[i] = (u16)cnt;
433 			purb = pxmitframe->pxmit_urb[i];
434 			break;
435 		}
436 	}
437 	bwritezero = false;
438 	if (pdvobj->ishighspeed) {
439 		if (cnt > 0 && cnt % 512 == 0)
440 			bwritezero = true;
441 	} else {
442 		if (cnt > 0 && cnt % 64 == 0)
443 			bwritezero = true;
444 	}
445 	/* translate DMA FIFO addr to pipehandle */
446 	pipe = ffaddr2pipehdl(pdvobj, addr);
447 	if (pxmitpriv->free_xmitbuf_cnt % NR_XMITBUFF == 0)
448 		purb->transfer_flags  &=  (~URB_NO_INTERRUPT);
449 	else
450 		purb->transfer_flags  |=  URB_NO_INTERRUPT;
451 	if (bwritezero)
452 		cnt += 8;
453 	usb_fill_bulk_urb(purb, pusbd, pipe,
454 			  pxmitframe->mem_addr,
455 			  cnt, usb_write_port_complete,
456 			  pxmitframe); /* context is xmit_frame */
457 	status = usb_submit_urb(purb, GFP_ATOMIC);
458 	if (!status)
459 		ret = _SUCCESS;
460 	else
461 		ret = _FAIL;
462 	return ret;
463 }
464 
r8712_usb_write_port_cancel(struct _adapter * padapter)465 void r8712_usb_write_port_cancel(struct _adapter *padapter)
466 {
467 	int i, j;
468 	struct xmit_buf	*pxmitbuf = (struct xmit_buf *)
469 				     padapter->xmitpriv.pxmitbuf;
470 
471 	for (i = 0; i < NR_XMITBUFF; i++) {
472 		for (j = 0; j < 8; j++) {
473 			if (pxmitbuf->pxmit_urb[j])
474 				usb_kill_urb(pxmitbuf->pxmit_urb[j]);
475 		}
476 		pxmitbuf++;
477 	}
478 }
479 
r8712_usbctrl_vendorreq(struct intf_priv * pintfpriv,u8 request,u16 value,u16 index,void * pdata,u16 len,u8 requesttype)480 int r8712_usbctrl_vendorreq(struct intf_priv *pintfpriv, u8 request, u16 value,
481 		      u16 index, void *pdata, u16 len, u8 requesttype)
482 {
483 	unsigned int pipe;
484 	int status;
485 	u8 reqtype;
486 	struct dvobj_priv *pdvobjpriv = (struct dvobj_priv *)
487 					 pintfpriv->intf_dev;
488 	struct usb_device *udev = pdvobjpriv->pusbdev;
489 	/* For mstar platform, mstar suggests the address for USB IO
490 	 * should be 16 bytes alignment. Trying to fix it here.
491 	 */
492 	u8 *palloc_buf, *pIo_buf;
493 
494 	palloc_buf = kmalloc((u32)len + 16, GFP_ATOMIC);
495 	if (!palloc_buf)
496 		return -ENOMEM;
497 	pIo_buf = palloc_buf + 16 - ((addr_t)(palloc_buf) & 0x0f);
498 	if (requesttype == 0x01) {
499 		pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
500 		reqtype =  RTL871X_VENQT_READ;
501 	} else {
502 		pipe = usb_sndctrlpipe(udev, 0); /* write_out */
503 		reqtype =  RTL871X_VENQT_WRITE;
504 		memcpy(pIo_buf, pdata, len);
505 	}
506 	status = usb_control_msg(udev, pipe, request, reqtype, value, index,
507 				 pIo_buf, len, HZ / 2);
508 	if (status > 0) {  /* Success this control transfer. */
509 		if (requesttype == 0x01) {
510 			/* For Control read transfer, we have to copy the read
511 			 * data from pIo_buf to pdata.
512 			 */
513 			memcpy(pdata, pIo_buf,  status);
514 		}
515 	}
516 	kfree(palloc_buf);
517 	return status;
518 }
519