linux/drivers/staging/rtl8712/usb_ops_linux.c
<<
>>
Prefs
   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
  41struct zero_bulkout_context {
  42        void *pbuf;
  43        void *purb;
  44        void *pirp;
  45        void *padapter;
  46};
  47
  48#define usb_write_cmd r8712_usb_write_mem
  49#define usb_write_cmd_complete usb_write_mem_complete
  50
  51uint r8712_usb_init_intf_priv(struct intf_priv *pintfpriv)
  52{
  53        pintfpriv->piorw_urb = usb_alloc_urb(0, GFP_ATOMIC);
  54        if (!pintfpriv->piorw_urb)
  55                return _FAIL;
  56        sema_init(&(pintfpriv->io_retevt), 0);
  57        return _SUCCESS;
  58}
  59
  60void r8712_usb_unload_intf_priv(struct intf_priv *pintfpriv)
  61{
  62        if (pintfpriv->piorw_urb) {
  63                usb_kill_urb(pintfpriv->piorw_urb);
  64                usb_free_urb(pintfpriv->piorw_urb);
  65        }
  66}
  67
  68static unsigned int ffaddr2pipehdl(struct dvobj_priv *pdvobj, u32 addr)
  69{
  70        unsigned int pipe = 0;
  71        struct usb_device *pusbd = pdvobj->pusbdev;
  72
  73        if (pdvobj->nr_endpoint == 11) {
  74                switch (addr) {
  75                case RTL8712_DMA_BKQ:
  76                        pipe = usb_sndbulkpipe(pusbd, 0x07);
  77                        break;
  78                case RTL8712_DMA_BEQ:
  79                        pipe = usb_sndbulkpipe(pusbd, 0x06);
  80                        break;
  81                case RTL8712_DMA_VIQ:
  82                        pipe = usb_sndbulkpipe(pusbd, 0x05);
  83                        break;
  84                case RTL8712_DMA_VOQ:
  85                        pipe = usb_sndbulkpipe(pusbd, 0x04);
  86                        break;
  87                case RTL8712_DMA_BCNQ:
  88                        pipe = usb_sndbulkpipe(pusbd, 0x0a);
  89                        break;
  90                case RTL8712_DMA_BMCQ:  /* HI Queue */
  91                        pipe = usb_sndbulkpipe(pusbd, 0x0b);
  92                        break;
  93                case RTL8712_DMA_MGTQ:
  94                        pipe = usb_sndbulkpipe(pusbd, 0x0c);
  95                        break;
  96                case RTL8712_DMA_RX0FF:
  97                        pipe = usb_rcvbulkpipe(pusbd, 0x03); /* in */
  98                        break;
  99                case RTL8712_DMA_C2HCMD:
 100                        pipe = usb_rcvbulkpipe(pusbd, 0x09); /* in */
 101                        break;
 102                case RTL8712_DMA_H2CCMD:
 103                        pipe = usb_sndbulkpipe(pusbd, 0x0d);
 104                        break;
 105                }
 106        } else if (pdvobj->nr_endpoint == 6) {
 107                switch (addr) {
 108                case RTL8712_DMA_BKQ:
 109                        pipe = usb_sndbulkpipe(pusbd, 0x07);
 110                        break;
 111                case RTL8712_DMA_BEQ:
 112                        pipe = usb_sndbulkpipe(pusbd, 0x06);
 113                        break;
 114                case RTL8712_DMA_VIQ:
 115                        pipe = usb_sndbulkpipe(pusbd, 0x05);
 116                        break;
 117                case RTL8712_DMA_VOQ:
 118                        pipe = usb_sndbulkpipe(pusbd, 0x04);
 119                        break;
 120                case RTL8712_DMA_RX0FF:
 121                case RTL8712_DMA_C2HCMD:
 122                        pipe = usb_rcvbulkpipe(pusbd, 0x03); /* in */
 123                        break;
 124                case RTL8712_DMA_H2CCMD:
 125                case RTL8712_DMA_BCNQ:
 126                case RTL8712_DMA_BMCQ:
 127                case RTL8712_DMA_MGTQ:
 128                        pipe = usb_sndbulkpipe(pusbd, 0x0d);
 129                        break;
 130                }
 131        } else if (pdvobj->nr_endpoint == 4) {
 132                switch (addr) {
 133                case RTL8712_DMA_BEQ:
 134                        pipe = usb_sndbulkpipe(pusbd, 0x06);
 135                        break;
 136                case RTL8712_DMA_VOQ:
 137                        pipe = usb_sndbulkpipe(pusbd, 0x04);
 138                        break;
 139                case RTL8712_DMA_RX0FF:
 140                case RTL8712_DMA_C2HCMD:
 141                        pipe = usb_rcvbulkpipe(pusbd, 0x03); /* in */
 142                        break;
 143                case RTL8712_DMA_H2CCMD:
 144                case RTL8712_DMA_BCNQ:
 145                case RTL8712_DMA_BMCQ:
 146                case RTL8712_DMA_MGTQ:
 147                        pipe = usb_sndbulkpipe(pusbd, 0x0d);
 148                        break;
 149                }
 150        } else
 151           pipe = 0;
 152        return pipe;
 153}
 154
 155static void usb_write_mem_complete(struct urb *purb)
 156{
 157        struct io_queue *pio_q = (struct io_queue *)purb->context;
 158        struct intf_hdl *pintf = &(pio_q->intf);
 159        struct intf_priv *pintfpriv = pintf->pintfpriv;
 160        struct _adapter *padapter = (struct _adapter *)pintf->adapter;
 161
 162        if (purb->status != 0) {
 163                if (purb->status == (-ESHUTDOWN))
 164                        padapter->bDriverStopped = true;
 165                else
 166                        padapter->bSurpriseRemoved = true;
 167        }
 168        up(&pintfpriv->io_retevt);
 169}
 170
 171void r8712_usb_write_mem(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *wmem)
 172{
 173        unsigned int pipe;
 174        int status;
 175        struct _adapter *padapter = (struct _adapter *)pintfhdl->adapter;
 176        struct intf_priv *pintfpriv = pintfhdl->pintfpriv;
 177        struct io_queue *pio_queue = (struct io_queue *)padapter->pio_queue;
 178        struct dvobj_priv *pdvobj = (struct dvobj_priv *)pintfpriv->intf_dev;
 179        struct usb_device *pusbd = pdvobj->pusbdev;
 180        struct urb *piorw_urb = pintfpriv->piorw_urb;
 181
 182        if ((padapter->bDriverStopped) || (padapter->bSurpriseRemoved) ||
 183            (padapter->pwrctrlpriv.pnp_bstop_trx))
 184                return;
 185        /* translate DMA FIFO addr to pipehandle */
 186        pipe = ffaddr2pipehdl(pdvobj, addr);
 187        if (pipe == 0)
 188                return;
 189        usb_fill_bulk_urb(piorw_urb, pusbd, pipe,
 190                          wmem, cnt, usb_write_mem_complete,
 191                          pio_queue);
 192        status = usb_submit_urb(piorw_urb, GFP_ATOMIC);
 193        _down_sema(&pintfpriv->io_retevt);
 194}
 195
 196static void r8712_usb_read_port_complete(struct urb *purb)
 197{
 198        uint isevt, *pbuf;
 199        struct recv_buf *precvbuf = (struct recv_buf *)purb->context;
 200        struct _adapter *padapter = (struct _adapter *)precvbuf->adapter;
 201        struct recv_priv *precvpriv = &padapter->recvpriv;
 202
 203        if (padapter->bSurpriseRemoved || padapter->bDriverStopped)
 204                return;
 205        if (purb->status == 0) { /* SUCCESS */
 206                if ((purb->actual_length > (MAX_RECVBUF_SZ)) ||
 207                    (purb->actual_length < RXDESC_SIZE)) {
 208                        precvbuf->reuse = true;
 209                        r8712_read_port(padapter, precvpriv->ff_hwaddr, 0,
 210                                  (unsigned char *)precvbuf);
 211                } else {
 212                        precvbuf->transfer_len = purb->actual_length;
 213                        pbuf = (uint *)precvbuf->pbuf;
 214                        isevt = le32_to_cpu(*(pbuf + 1)) & 0x1ff;
 215                        if ((isevt & 0x1ff) == 0x1ff) {
 216                                r8712_rxcmd_event_hdl(padapter, pbuf);
 217                                precvbuf->reuse = true;
 218                                r8712_read_port(padapter, precvpriv->ff_hwaddr,
 219                                                0, (unsigned char *)precvbuf);
 220                        } else {
 221                                _pkt *pskb = precvbuf->pskb;
 222                                skb_put(pskb, purb->actual_length);
 223                                skb_queue_tail(&precvpriv->rx_skb_queue, pskb);
 224                                tasklet_hi_schedule(&precvpriv->recv_tasklet);
 225                                precvbuf->pskb = NULL;
 226                                precvbuf->reuse = false;
 227                                r8712_read_port(padapter, precvpriv->ff_hwaddr,
 228                                                0, (unsigned char *)precvbuf);
 229                        }
 230                }
 231        } else {
 232                switch (purb->status) {
 233                case -EINVAL:
 234                case -EPIPE:
 235                case -ENODEV:
 236                case -ESHUTDOWN:
 237                case -ENOENT:
 238                        padapter->bDriverStopped = true;
 239                        break;
 240                case -EPROTO:
 241                        precvbuf->reuse = true;
 242                        r8712_read_port(padapter, precvpriv->ff_hwaddr, 0,
 243                                  (unsigned char *)precvbuf);
 244                        break;
 245                case -EINPROGRESS:
 246                        printk(KERN_ERR "r8712u: ERROR: URB IS IN"
 247                               " PROGRESS!/n");
 248                        break;
 249                default:
 250                        break;
 251                }
 252        }
 253}
 254
 255u32 r8712_usb_read_port(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *rmem)
 256{
 257        unsigned int pipe;
 258        int err;
 259        u32 tmpaddr = 0;
 260        int alignment = 0;
 261        u32 ret = _SUCCESS;
 262        struct urb *purb = NULL;
 263        struct recv_buf *precvbuf = (struct recv_buf *)rmem;
 264        struct intf_priv *pintfpriv = pintfhdl->pintfpriv;
 265        struct dvobj_priv *pdvobj = (struct dvobj_priv *)pintfpriv->intf_dev;
 266        struct _adapter *adapter = (struct _adapter *)pdvobj->padapter;
 267        struct recv_priv *precvpriv = &adapter->recvpriv;
 268        struct usb_device *pusbd = pdvobj->pusbdev;
 269
 270        if (adapter->bDriverStopped || adapter->bSurpriseRemoved ||
 271            adapter->pwrctrlpriv.pnp_bstop_trx)
 272                return _FAIL;
 273        if ((precvbuf->reuse == false) || (precvbuf->pskb == NULL)) {
 274                precvbuf->pskb = skb_dequeue(&precvpriv->free_recv_skb_queue);
 275                if (NULL != precvbuf->pskb)
 276                        precvbuf->reuse = true;
 277        }
 278        if (precvbuf != NULL) {
 279                r8712_init_recvbuf(adapter, precvbuf);
 280                /* re-assign for linux based on skb */
 281                if ((precvbuf->reuse == false) || (precvbuf->pskb == NULL)) {
 282                        precvbuf->pskb = netdev_alloc_skb(adapter->pnetdev,
 283                                         MAX_RECVBUF_SZ + RECVBUFF_ALIGN_SZ);
 284                        if (precvbuf->pskb == NULL)
 285                                return _FAIL;
 286                        tmpaddr = (addr_t)precvbuf->pskb->data;
 287                        alignment = tmpaddr & (RECVBUFF_ALIGN_SZ-1);
 288                        skb_reserve(precvbuf->pskb,
 289                                    (RECVBUFF_ALIGN_SZ - alignment));
 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                } else { /* reuse skb */
 296                        precvbuf->phead = precvbuf->pskb->head;
 297                        precvbuf->pdata = precvbuf->pskb->data;
 298                        precvbuf->ptail = skb_tail_pointer(precvbuf->pskb);
 299                        precvbuf->pend = skb_end_pointer(precvbuf->pskb);
 300                        precvbuf->pbuf = precvbuf->pskb->data;
 301                        precvbuf->reuse = false;
 302                }
 303                purb = precvbuf->purb;
 304                /* translate DMA FIFO addr to pipehandle */
 305                pipe = ffaddr2pipehdl(pdvobj, addr);
 306                usb_fill_bulk_urb(purb, pusbd, pipe,
 307                                  precvbuf->pbuf, MAX_RECVBUF_SZ,
 308                                  r8712_usb_read_port_complete,
 309                                  precvbuf);
 310                err = usb_submit_urb(purb, GFP_ATOMIC);
 311                if ((err) && (err != (-EPERM)))
 312                        ret = _FAIL;
 313        } else
 314                ret = _FAIL;
 315        return ret;
 316}
 317
 318void r8712_usb_read_port_cancel(struct _adapter *padapter)
 319{
 320        int i;
 321        struct recv_buf *precvbuf;
 322
 323        precvbuf = (struct recv_buf *)padapter->recvpriv.precv_buf;
 324        for (i = 0; i < NR_RECVBUFF; i++) {
 325                if (precvbuf->purb)
 326                        usb_kill_urb(precvbuf->purb);
 327                precvbuf++;
 328        }
 329}
 330
 331void r8712_xmit_bh(void *priv)
 332{
 333        int ret = false;
 334        struct _adapter *padapter = (struct _adapter *)priv;
 335        struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
 336
 337        if ((padapter->bDriverStopped == true) ||
 338            (padapter->bSurpriseRemoved == true)) {
 339                printk(KERN_ERR "r8712u: xmit_bh => bDriverStopped"
 340                       " or bSurpriseRemoved\n");
 341                return;
 342        }
 343        ret = r8712_xmitframe_complete(padapter, pxmitpriv, NULL);
 344        if (ret == false)
 345                return;
 346        tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
 347}
 348
 349static void usb_write_port_complete(struct urb *purb)
 350{
 351        int i;
 352        struct xmit_frame *pxmitframe = (struct xmit_frame *)purb->context;
 353        struct xmit_buf *pxmitbuf = pxmitframe->pxmitbuf;
 354        struct _adapter *padapter = pxmitframe->padapter;
 355        struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
 356        struct pkt_attrib *pattrib = &pxmitframe->attrib;
 357
 358        switch (pattrib->priority) {
 359        case 1:
 360        case 2:
 361                pxmitpriv->bkq_cnt--;
 362                break;
 363        case 4:
 364        case 5:
 365                pxmitpriv->viq_cnt--;
 366                break;
 367        case 6:
 368        case 7:
 369                pxmitpriv->voq_cnt--;
 370                break;
 371        case 0:
 372        case 3:
 373        default:
 374                pxmitpriv->beq_cnt--;
 375                break;
 376        }
 377        pxmitpriv->txirp_cnt--;
 378        for (i = 0; i < 8; i++) {
 379                if (purb == pxmitframe->pxmit_urb[i]) {
 380                        pxmitframe->bpending[i] = false;
 381                        break;
 382                }
 383        }
 384        if (padapter->bSurpriseRemoved)
 385                return;
 386        switch (purb->status) {
 387        case 0:
 388                break;
 389        default:
 390                printk(KERN_WARNING "r8712u: pipe error: (%d)\n", purb->status);
 391                break;
 392        }
 393        /* not to consider tx fragment */
 394        r8712_free_xmitframe_ex(pxmitpriv, pxmitframe);
 395        r8712_free_xmitbuf(pxmitpriv, pxmitbuf);
 396        tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
 397}
 398
 399u32 r8712_usb_write_port(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *wmem)
 400{
 401        unsigned long irqL;
 402        int i, status;
 403        unsigned int pipe;
 404        u32 ret, bwritezero;
 405        struct urb *purb = NULL;
 406        struct _adapter *padapter = (struct _adapter *)pintfhdl->adapter;
 407        struct dvobj_priv *pdvobj = (struct dvobj_priv   *)&padapter->dvobjpriv;
 408        struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
 409        struct xmit_frame *pxmitframe = (struct xmit_frame *)wmem;
 410        struct usb_device *pusbd = pdvobj->pusbdev;
 411        struct pkt_attrib *pattrib = &pxmitframe->attrib;
 412
 413        if ((padapter->bDriverStopped) || (padapter->bSurpriseRemoved) ||
 414            (padapter->pwrctrlpriv.pnp_bstop_trx))
 415                return _FAIL;
 416        for (i = 0; i < 8; i++) {
 417                if (pxmitframe->bpending[i] == false) {
 418                        spin_lock_irqsave(&pxmitpriv->lock, irqL);
 419                        pxmitpriv->txirp_cnt++;
 420                        pxmitframe->bpending[i]  = true;
 421                        switch (pattrib->priority) {
 422                        case 1:
 423                        case 2:
 424                                pxmitpriv->bkq_cnt++;
 425                                break;
 426                        case 4:
 427                        case 5:
 428                                pxmitpriv->viq_cnt++;
 429                                break;
 430                        case 6:
 431                        case 7:
 432                                pxmitpriv->voq_cnt++;
 433                                break;
 434                        case 0:
 435                        case 3:
 436                        default:
 437                                pxmitpriv->beq_cnt++;
 438                                break;
 439                        }
 440                        spin_unlock_irqrestore(&pxmitpriv->lock, irqL);
 441                        pxmitframe->sz[i] = (u16)cnt;
 442                        purb = pxmitframe->pxmit_urb[i];
 443                        break;
 444                }
 445        }
 446        bwritezero = false;
 447        if (pdvobj->ishighspeed) {
 448                if (cnt > 0 && cnt % 512 == 0)
 449                        bwritezero = true;
 450        } else {
 451                if (cnt > 0 && cnt % 64 == 0)
 452                        bwritezero = true;
 453        }
 454        /* translate DMA FIFO addr to pipehandle */
 455        pipe = ffaddr2pipehdl(pdvobj, addr);
 456        if (pxmitpriv->free_xmitbuf_cnt%NR_XMITBUFF == 0)
 457                purb->transfer_flags  &=  (~URB_NO_INTERRUPT);
 458        else
 459                purb->transfer_flags  |=  URB_NO_INTERRUPT;
 460        if (bwritezero)
 461                cnt += 8;
 462        usb_fill_bulk_urb(purb, pusbd, pipe,
 463                          pxmitframe->mem_addr,
 464                          cnt, usb_write_port_complete,
 465                          pxmitframe); /* context is xmit_frame */
 466        status = usb_submit_urb(purb, GFP_ATOMIC);
 467        if (!status)
 468                ret = _SUCCESS;
 469        else
 470                ret = _FAIL;
 471        return ret;
 472}
 473
 474void r8712_usb_write_port_cancel(struct _adapter *padapter)
 475{
 476        int i, j;
 477        struct xmit_buf *pxmitbuf = (struct xmit_buf *)
 478                                     padapter->xmitpriv.pxmitbuf;
 479
 480        for (i = 0; i < NR_XMITBUFF; i++) {
 481                for (j = 0; j < 8; j++) {
 482                        if (pxmitbuf->pxmit_urb[j])
 483                                usb_kill_urb(pxmitbuf->pxmit_urb[j]);
 484                }
 485                pxmitbuf++;
 486        }
 487}
 488
 489int r8712_usbctrl_vendorreq(struct intf_priv *pintfpriv, u8 request, u16 value,
 490                      u16 index, void *pdata, u16 len, u8 requesttype)
 491{
 492        unsigned int pipe;
 493        int status;
 494        u8 reqtype;
 495        struct dvobj_priv *pdvobjpriv = (struct dvobj_priv *)
 496                                         pintfpriv->intf_dev;
 497        struct usb_device *udev = pdvobjpriv->pusbdev;
 498        /* For mstar platform, mstar suggests the address for USB IO
 499         * should be 16 bytes alignment. Trying to fix it here.
 500         */
 501        u8 *palloc_buf, *pIo_buf;
 502
 503        palloc_buf = _malloc((u32) len + 16);
 504        if (palloc_buf == NULL) {
 505                printk(KERN_ERR "r8712u: [%s] Can't alloc memory for vendor"
 506                       " request\n", __func__);
 507                return -1;
 508        }
 509        pIo_buf = palloc_buf + 16 - ((addr_t)(palloc_buf) & 0x0f);
 510        if (requesttype == 0x01) {
 511                pipe = usb_rcvctrlpipe(udev, 0); /* read_in */
 512                reqtype =  RTL871X_VENQT_READ;
 513        } else {
 514                pipe = usb_sndctrlpipe(udev, 0); /* write_out */
 515                reqtype =  RTL871X_VENQT_WRITE;
 516                memcpy(pIo_buf, pdata, len);
 517        }
 518        status = usb_control_msg(udev, pipe, request, reqtype, value, index,
 519                                 pIo_buf, len, HZ / 2);
 520        if (status > 0) {  /* Success this control transfer. */
 521                if (requesttype == 0x01) {
 522                        /* For Control read transfer, we have to copy the read
 523                         * data from pIo_buf to pdata.
 524                         */
 525                        memcpy(pdata, pIo_buf,  status);
 526                }
 527        }
 528        kfree(palloc_buf);
 529        return status;
 530}
 531