linux/drivers/staging/usbip/stub_tx.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
   3 *
   4 * This is free software; you can redistribute it and/or modify
   5 * it under the terms of the GNU General Public License as published by
   6 * the Free Software Foundation; either version 2 of the License, or
   7 * (at your option) any later version.
   8 *
   9 * This is distributed in the hope that it will be useful,
  10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 * GNU General Public License for more details.
  13 *
  14 * You should have received a copy of the GNU General Public License
  15 * along with this program; if not, write to the Free Software
  16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17 * USA.
  18 */
  19
  20#include "usbip_common.h"
  21#include "stub.h"
  22
  23
  24static void stub_free_priv_and_urb(struct stub_priv *priv)
  25{
  26        struct urb *urb = priv->urb;
  27
  28        kfree(urb->setup_packet);
  29        kfree(urb->transfer_buffer);
  30        list_del(&priv->list);
  31        kmem_cache_free(stub_priv_cache, priv);
  32        usb_free_urb(urb);
  33}
  34
  35/* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
  36void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
  37                             __u32 status)
  38{
  39        struct stub_unlink *unlink;
  40
  41        unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
  42        if (!unlink) {
  43                dev_err(&sdev->interface->dev, "alloc stub_unlink\n");
  44                usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
  45                return;
  46        }
  47
  48        unlink->seqnum = seqnum;
  49        unlink->status = status;
  50
  51        list_add_tail(&unlink->list, &sdev->unlink_tx);
  52}
  53
  54/**
  55 * stub_complete - completion handler of a usbip urb
  56 * @urb: pointer to the urb completed
  57 *
  58 * When a urb has completed, the USB core driver calls this function mostly in
  59 * the interrupt context. To return the result of a urb, the completed urb is
  60 * linked to the pending list of returning.
  61 *
  62 */
  63void stub_complete(struct urb *urb)
  64{
  65        struct stub_priv *priv = (struct stub_priv *) urb->context;
  66        struct stub_device *sdev = priv->sdev;
  67        unsigned long flags;
  68
  69        usbip_dbg_stub_tx("complete! status %d\n", urb->status);
  70
  71
  72        switch (urb->status) {
  73        case 0:
  74                /* OK */
  75                break;
  76        case -ENOENT:
  77                usbip_uinfo("stopped by a call of usb_kill_urb() because of"
  78                                        "cleaning up a virtual connection\n");
  79                return;
  80        case -ECONNRESET:
  81                usbip_uinfo("unlinked by a call of usb_unlink_urb()\n");
  82                break;
  83        case -EPIPE:
  84                usbip_uinfo("endpoint %d is stalled\n",
  85                                                usb_pipeendpoint(urb->pipe));
  86                break;
  87        case -ESHUTDOWN:
  88                usbip_uinfo("device removed?\n");
  89                break;
  90        default:
  91                usbip_uinfo("urb completion with non-zero status %d\n",
  92                                                        urb->status);
  93        }
  94
  95        /* link a urb to the queue of tx. */
  96        spin_lock_irqsave(&sdev->priv_lock, flags);
  97
  98        if (priv->unlinking) {
  99                stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
 100                stub_free_priv_and_urb(priv);
 101        } else
 102                list_move_tail(&priv->list, &sdev->priv_tx);
 103
 104
 105        spin_unlock_irqrestore(&sdev->priv_lock, flags);
 106
 107        /* wake up tx_thread */
 108        wake_up(&sdev->tx_waitq);
 109}
 110
 111
 112/*-------------------------------------------------------------------------*/
 113/* fill PDU */
 114
 115static inline void setup_base_pdu(struct usbip_header_basic *base,
 116                __u32 command, __u32 seqnum)
 117{
 118        base->command = command;
 119        base->seqnum  = seqnum;
 120        base->devid   = 0;
 121        base->ep      = 0;
 122        base->direction   = 0;
 123}
 124
 125static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
 126{
 127        struct stub_priv *priv = (struct stub_priv *) urb->context;
 128
 129        setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
 130
 131        usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
 132}
 133
 134static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
 135                struct stub_unlink *unlink)
 136{
 137        setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
 138
 139        rpdu->u.ret_unlink.status = unlink->status;
 140}
 141
 142
 143/*-------------------------------------------------------------------------*/
 144/* send RET_SUBMIT */
 145
 146static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
 147{
 148        unsigned long flags;
 149        struct stub_priv *priv, *tmp;
 150
 151        spin_lock_irqsave(&sdev->priv_lock, flags);
 152
 153        list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
 154                list_move_tail(&priv->list, &sdev->priv_free);
 155                spin_unlock_irqrestore(&sdev->priv_lock, flags);
 156                return priv;
 157        }
 158
 159        spin_unlock_irqrestore(&sdev->priv_lock, flags);
 160
 161        return NULL;
 162}
 163
 164static int stub_send_ret_submit(struct stub_device *sdev)
 165{
 166        unsigned long flags;
 167        struct stub_priv *priv, *tmp;
 168
 169        struct msghdr msg;
 170        struct kvec iov[3];
 171        size_t txsize;
 172
 173        size_t total_size = 0;
 174
 175        while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
 176                int ret;
 177                struct urb *urb = priv->urb;
 178                struct usbip_header pdu_header;
 179                void *iso_buffer = NULL;
 180
 181                txsize = 0;
 182                memset(&pdu_header, 0, sizeof(pdu_header));
 183                memset(&msg, 0, sizeof(msg));
 184                memset(&iov, 0, sizeof(iov));
 185
 186                usbip_dbg_stub_tx("setup txdata urb %p\n", urb);
 187
 188
 189                /* 1. setup usbip_header */
 190                setup_ret_submit_pdu(&pdu_header, urb);
 191                usbip_header_correct_endian(&pdu_header, 1);
 192
 193                iov[0].iov_base = &pdu_header;
 194                iov[0].iov_len  = sizeof(pdu_header);
 195                txsize += sizeof(pdu_header);
 196
 197                /* 2. setup transfer buffer */
 198                if (usb_pipein(urb->pipe) && urb->actual_length > 0) {
 199                        iov[1].iov_base = urb->transfer_buffer;
 200                        iov[1].iov_len  = urb->actual_length;
 201                        txsize += urb->actual_length;
 202                }
 203
 204                /* 3. setup iso_packet_descriptor */
 205                if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
 206                        ssize_t len = 0;
 207
 208                        iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
 209                        if (!iso_buffer) {
 210                                usbip_event_add(&sdev->ud,
 211                                                SDEV_EVENT_ERROR_MALLOC);
 212                                return -1;
 213                        }
 214
 215                        iov[2].iov_base = iso_buffer;
 216                        iov[2].iov_len  = len;
 217                        txsize += len;
 218                }
 219
 220                ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
 221                                     3, txsize);
 222                if (ret != txsize) {
 223                        dev_err(&sdev->interface->dev,
 224                                "sendmsg failed!, retval %d for %zd\n",
 225                                ret, txsize);
 226                        kfree(iso_buffer);
 227                        usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
 228                        return -1;
 229                }
 230
 231                kfree(iso_buffer);
 232                usbip_dbg_stub_tx("send txdata\n");
 233
 234                total_size += txsize;
 235        }
 236
 237
 238        spin_lock_irqsave(&sdev->priv_lock, flags);
 239
 240        list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
 241                stub_free_priv_and_urb(priv);
 242        }
 243
 244        spin_unlock_irqrestore(&sdev->priv_lock, flags);
 245
 246        return total_size;
 247}
 248
 249
 250/*-------------------------------------------------------------------------*/
 251/* send RET_UNLINK */
 252
 253static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
 254{
 255        unsigned long flags;
 256        struct stub_unlink *unlink, *tmp;
 257
 258        spin_lock_irqsave(&sdev->priv_lock, flags);
 259
 260        list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
 261                list_move_tail(&unlink->list, &sdev->unlink_free);
 262                spin_unlock_irqrestore(&sdev->priv_lock, flags);
 263                return unlink;
 264        }
 265
 266        spin_unlock_irqrestore(&sdev->priv_lock, flags);
 267
 268        return NULL;
 269}
 270
 271
 272static int stub_send_ret_unlink(struct stub_device *sdev)
 273{
 274        unsigned long flags;
 275        struct stub_unlink *unlink, *tmp;
 276
 277        struct msghdr msg;
 278        struct kvec iov[1];
 279        size_t txsize;
 280
 281        size_t total_size = 0;
 282
 283        while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
 284                int ret;
 285                struct usbip_header pdu_header;
 286
 287                txsize = 0;
 288                memset(&pdu_header, 0, sizeof(pdu_header));
 289                memset(&msg, 0, sizeof(msg));
 290                memset(&iov, 0, sizeof(iov));
 291
 292                usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
 293
 294                /* 1. setup usbip_header */
 295                setup_ret_unlink_pdu(&pdu_header, unlink);
 296                usbip_header_correct_endian(&pdu_header, 1);
 297
 298                iov[0].iov_base = &pdu_header;
 299                iov[0].iov_len  = sizeof(pdu_header);
 300                txsize += sizeof(pdu_header);
 301
 302                ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
 303                                     1, txsize);
 304                if (ret != txsize) {
 305                        dev_err(&sdev->interface->dev,
 306                                "sendmsg failed!, retval %d for %zd\n",
 307                                ret, txsize);
 308                        usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
 309                        return -1;
 310                }
 311
 312
 313                usbip_dbg_stub_tx("send txdata\n");
 314
 315                total_size += txsize;
 316        }
 317
 318
 319        spin_lock_irqsave(&sdev->priv_lock, flags);
 320
 321        list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
 322                list_del(&unlink->list);
 323                kfree(unlink);
 324        }
 325
 326        spin_unlock_irqrestore(&sdev->priv_lock, flags);
 327
 328        return total_size;
 329}
 330
 331
 332/*-------------------------------------------------------------------------*/
 333
 334void stub_tx_loop(struct usbip_task *ut)
 335{
 336        struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx);
 337        struct stub_device *sdev = container_of(ud, struct stub_device, ud);
 338
 339        while (1) {
 340                if (signal_pending(current)) {
 341                        usbip_dbg_stub_tx("signal catched\n");
 342                        break;
 343                }
 344
 345                if (usbip_event_happened(ud))
 346                        break;
 347
 348                /*
 349                 * send_ret_submit comes earlier than send_ret_unlink.  stub_rx
 350                 * looks at only priv_init queue. If the completion of a URB is
 351                 * earlier than the receive of CMD_UNLINK, priv is moved to
 352                 * priv_tx queue and stub_rx does not find the target priv. In
 353                 * this case, vhci_rx receives the result of the submit request
 354                 * and then receives the result of the unlink request. The
 355                 * result of the submit is given back to the usbcore as the
 356                 * completion of the unlink request. The request of the
 357                 * unlink is ignored. This is ok because a driver who calls
 358                 * usb_unlink_urb() understands the unlink was too late by
 359                 * getting the status of the given-backed URB which has the
 360                 * status of usb_submit_urb().
 361                 */
 362                if (stub_send_ret_submit(sdev) < 0)
 363                        break;
 364
 365                if (stub_send_ret_unlink(sdev) < 0)
 366                        break;
 367
 368                wait_event_interruptible(sdev->tx_waitq,
 369                                (!list_empty(&sdev->priv_tx) ||
 370                                 !list_empty(&sdev->unlink_tx)));
 371        }
 372}
 373