Merge branch 'opw-next' into staging-next
[linux-2.6-microblaze.git] / drivers / staging / usbip / stub_rx.c
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 <asm/byteorder.h>
21 #include <linux/kthread.h>
22 #include <linux/usb.h>
23 #include <linux/usb/hcd.h>
24
25 #include "usbip_common.h"
26 #include "stub.h"
27
28 static int is_clear_halt_cmd(struct urb *urb)
29 {
30         struct usb_ctrlrequest *req;
31
32         req = (struct usb_ctrlrequest *) urb->setup_packet;
33
34          return (req->bRequest == USB_REQ_CLEAR_FEATURE) &&
35                  (req->bRequestType == USB_RECIP_ENDPOINT) &&
36                  (req->wValue == USB_ENDPOINT_HALT);
37 }
38
39 static int is_set_interface_cmd(struct urb *urb)
40 {
41         struct usb_ctrlrequest *req;
42
43         req = (struct usb_ctrlrequest *) urb->setup_packet;
44
45         return (req->bRequest == USB_REQ_SET_INTERFACE) &&
46                 (req->bRequestType == USB_RECIP_INTERFACE);
47 }
48
49 static int is_set_configuration_cmd(struct urb *urb)
50 {
51         struct usb_ctrlrequest *req;
52
53         req = (struct usb_ctrlrequest *) urb->setup_packet;
54
55         return (req->bRequest == USB_REQ_SET_CONFIGURATION) &&
56                 (req->bRequestType == USB_RECIP_DEVICE);
57 }
58
59 static int is_reset_device_cmd(struct urb *urb)
60 {
61         struct usb_ctrlrequest *req;
62         __u16 value;
63         __u16 index;
64
65         req = (struct usb_ctrlrequest *) urb->setup_packet;
66         value = le16_to_cpu(req->wValue);
67         index = le16_to_cpu(req->wIndex);
68
69         if ((req->bRequest == USB_REQ_SET_FEATURE) &&
70             (req->bRequestType == USB_RT_PORT) &&
71             (value == USB_PORT_FEAT_RESET)) {
72                 usbip_dbg_stub_rx("reset_device_cmd, port %u\n", index);
73                 return 1;
74         } else
75                 return 0;
76 }
77
78 static int tweak_clear_halt_cmd(struct urb *urb)
79 {
80         struct usb_ctrlrequest *req;
81         int target_endp;
82         int target_dir;
83         int target_pipe;
84         int ret;
85
86         req = (struct usb_ctrlrequest *) urb->setup_packet;
87
88         /*
89          * The stalled endpoint is specified in the wIndex value. The endpoint
90          * of the urb is the target of this clear_halt request (i.e., control
91          * endpoint).
92          */
93         target_endp = le16_to_cpu(req->wIndex) & 0x000f;
94
95         /* the stalled endpoint direction is IN or OUT?. USB_DIR_IN is 0x80.  */
96         target_dir = le16_to_cpu(req->wIndex) & 0x0080;
97
98         if (target_dir)
99                 target_pipe = usb_rcvctrlpipe(urb->dev, target_endp);
100         else
101                 target_pipe = usb_sndctrlpipe(urb->dev, target_endp);
102
103         ret = usb_clear_halt(urb->dev, target_pipe);
104         if (ret < 0)
105                 dev_err(&urb->dev->dev,
106                         "usb_clear_halt error: devnum %d endp %d ret %d\n",
107                         urb->dev->devnum, target_endp, ret);
108         else
109                 dev_info(&urb->dev->dev,
110                          "usb_clear_halt done: devnum %d endp %d\n",
111                          urb->dev->devnum, target_endp);
112
113         return ret;
114 }
115
116 static int tweak_set_interface_cmd(struct urb *urb)
117 {
118         struct usb_ctrlrequest *req;
119         __u16 alternate;
120         __u16 interface;
121         int ret;
122
123         req = (struct usb_ctrlrequest *) urb->setup_packet;
124         alternate = le16_to_cpu(req->wValue);
125         interface = le16_to_cpu(req->wIndex);
126
127         usbip_dbg_stub_rx("set_interface: inf %u alt %u\n",
128                           interface, alternate);
129
130         ret = usb_set_interface(urb->dev, interface, alternate);
131         if (ret < 0)
132                 dev_err(&urb->dev->dev,
133                         "usb_set_interface error: inf %u alt %u ret %d\n",
134                         interface, alternate, ret);
135         else
136                 dev_info(&urb->dev->dev,
137                         "usb_set_interface done: inf %u alt %u\n",
138                         interface, alternate);
139
140         return ret;
141 }
142
143 static int tweak_set_configuration_cmd(struct urb *urb)
144 {
145         struct usb_ctrlrequest *req;
146         __u16 config;
147
148         req = (struct usb_ctrlrequest *) urb->setup_packet;
149         config = le16_to_cpu(req->wValue);
150
151         /*
152          * I have never seen a multi-config device. Very rare.
153          * For most devices, this will be called to choose a default
154          * configuration only once in an initialization phase.
155          *
156          * set_configuration may change a device configuration and its device
157          * drivers will be unbound and assigned for a new device configuration.
158          * This means this usbip driver will be also unbound when called, then
159          * eventually reassigned to the device as far as driver matching
160          * condition is kept.
161          *
162          * Unfortunately, an existing usbip connection will be dropped
163          * due to this driver unbinding. So, skip here.
164          * A user may need to set a special configuration value before
165          * exporting the device.
166          */
167         dev_info(&urb->dev->dev, "usb_set_configuration %d to %s... skip!\n",
168                  config, dev_name(&urb->dev->dev));
169
170         return 0;
171 }
172
173 static int tweak_reset_device_cmd(struct urb *urb)
174 {
175         struct stub_priv *priv = (struct stub_priv *) urb->context;
176         struct stub_device *sdev = priv->sdev;
177
178         dev_info(&urb->dev->dev, "usb_queue_reset_device\n");
179
180         /*
181          * With the implementation of pre_reset and post_reset the driver no
182          * longer unbinds. This allows the use of synchronous reset.
183          */
184
185         if (usb_lock_device_for_reset(sdev->udev, sdev->interface) < 0) {
186                 dev_err(&urb->dev->dev, "could not obtain lock to reset device\n");
187                 return 0;
188         }
189         usb_reset_device(sdev->udev);
190         usb_unlock_device(sdev->udev);
191
192         return 0;
193 }
194
195 /*
196  * clear_halt, set_interface, and set_configuration require special tricks.
197  */
198 static void tweak_special_requests(struct urb *urb)
199 {
200         if (!urb || !urb->setup_packet)
201                 return;
202
203         if (usb_pipetype(urb->pipe) != PIPE_CONTROL)
204                 return;
205
206         if (is_clear_halt_cmd(urb))
207                 /* tweak clear_halt */
208                  tweak_clear_halt_cmd(urb);
209
210         else if (is_set_interface_cmd(urb))
211                 /* tweak set_interface */
212                 tweak_set_interface_cmd(urb);
213
214         else if (is_set_configuration_cmd(urb))
215                 /* tweak set_configuration */
216                 tweak_set_configuration_cmd(urb);
217
218         else if (is_reset_device_cmd(urb))
219                 tweak_reset_device_cmd(urb);
220         else
221                 usbip_dbg_stub_rx("no need to tweak\n");
222 }
223
224 /*
225  * stub_recv_unlink() unlinks the URB by a call to usb_unlink_urb().
226  * By unlinking the urb asynchronously, stub_rx can continuously
227  * process coming urbs.  Even if the urb is unlinked, its completion
228  * handler will be called and stub_tx will send a return pdu.
229  *
230  * See also comments about unlinking strategy in vhci_hcd.c.
231  */
232 static int stub_recv_cmd_unlink(struct stub_device *sdev,
233                                 struct usbip_header *pdu)
234 {
235         int ret;
236         unsigned long flags;
237         struct stub_priv *priv;
238
239         spin_lock_irqsave(&sdev->priv_lock, flags);
240
241         list_for_each_entry(priv, &sdev->priv_init, list) {
242                 if (priv->seqnum != pdu->u.cmd_unlink.seqnum)
243                         continue;
244
245                 dev_info(&priv->urb->dev->dev, "unlink urb %p\n",
246                          priv->urb);
247
248                 /*
249                  * This matched urb is not completed yet (i.e., be in
250                  * flight in usb hcd hardware/driver). Now we are
251                  * cancelling it. The unlinking flag means that we are
252                  * now not going to return the normal result pdu of a
253                  * submission request, but going to return a result pdu
254                  * of the unlink request.
255                  */
256                 priv->unlinking = 1;
257
258                 /*
259                  * In the case that unlinking flag is on, prev->seqnum
260                  * is changed from the seqnum of the cancelling urb to
261                  * the seqnum of the unlink request. This will be used
262                  * to make the result pdu of the unlink request.
263                  */
264                 priv->seqnum = pdu->base.seqnum;
265
266                 spin_unlock_irqrestore(&sdev->priv_lock, flags);
267
268                 /*
269                  * usb_unlink_urb() is now out of spinlocking to avoid
270                  * spinlock recursion since stub_complete() is
271                  * sometimes called in this context but not in the
272                  * interrupt context.  If stub_complete() is executed
273                  * before we call usb_unlink_urb(), usb_unlink_urb()
274                  * will return an error value. In this case, stub_tx
275                  * will return the result pdu of this unlink request
276                  * though submission is completed and actual unlinking
277                  * is not executed. OK?
278                  */
279                 /* In the above case, urb->status is not -ECONNRESET,
280                  * so a driver in a client host will know the failure
281                  * of the unlink request ?
282                  */
283                 ret = usb_unlink_urb(priv->urb);
284                 if (ret != -EINPROGRESS)
285                         dev_err(&priv->urb->dev->dev,
286                                 "failed to unlink a urb %p, ret %d\n",
287                                 priv->urb, ret);
288
289                 return 0;
290         }
291
292         usbip_dbg_stub_rx("seqnum %d is not pending\n",
293                           pdu->u.cmd_unlink.seqnum);
294
295         /*
296          * The urb of the unlink target is not found in priv_init queue. It was
297          * already completed and its results is/was going to be sent by a
298          * CMD_RET pdu. In this case, usb_unlink_urb() is not needed. We only
299          * return the completeness of this unlink request to vhci_hcd.
300          */
301         stub_enqueue_ret_unlink(sdev, pdu->base.seqnum, 0);
302
303         spin_unlock_irqrestore(&sdev->priv_lock, flags);
304
305         return 0;
306 }
307
308 static int valid_request(struct stub_device *sdev, struct usbip_header *pdu)
309 {
310         struct usbip_device *ud = &sdev->ud;
311         int valid = 0;
312
313         if (pdu->base.devid == sdev->devid) {
314                 spin_lock_irq(&ud->lock);
315                 if (ud->status == SDEV_ST_USED) {
316                         /* A request is valid. */
317                         valid = 1;
318                 }
319                 spin_unlock_irq(&ud->lock);
320         }
321
322         return valid;
323 }
324
325 static struct stub_priv *stub_priv_alloc(struct stub_device *sdev,
326                                          struct usbip_header *pdu)
327 {
328         struct stub_priv *priv;
329         struct usbip_device *ud = &sdev->ud;
330         unsigned long flags;
331
332         spin_lock_irqsave(&sdev->priv_lock, flags);
333
334         priv = kmem_cache_zalloc(stub_priv_cache, GFP_ATOMIC);
335         if (!priv) {
336                 dev_err(&sdev->interface->dev, "alloc stub_priv\n");
337                 spin_unlock_irqrestore(&sdev->priv_lock, flags);
338                 usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
339                 return NULL;
340         }
341
342         priv->seqnum = pdu->base.seqnum;
343         priv->sdev = sdev;
344
345         /*
346          * After a stub_priv is linked to a list_head,
347          * our error handler can free allocated data.
348          */
349         list_add_tail(&priv->list, &sdev->priv_init);
350
351         spin_unlock_irqrestore(&sdev->priv_lock, flags);
352
353         return priv;
354 }
355
356 static int get_pipe(struct stub_device *sdev, int epnum, int dir)
357 {
358         struct usb_device *udev = sdev->udev;
359         struct usb_host_endpoint *ep;
360         struct usb_endpoint_descriptor *epd = NULL;
361
362         if (dir == USBIP_DIR_IN)
363                 ep = udev->ep_in[epnum & 0x7f];
364         else
365                 ep = udev->ep_out[epnum & 0x7f];
366         if (!ep) {
367                 dev_err(&sdev->interface->dev, "no such endpoint?, %d\n",
368                         epnum);
369                 BUG();
370         }
371
372         epd = &ep->desc;
373         if (usb_endpoint_xfer_control(epd)) {
374                 if (dir == USBIP_DIR_OUT)
375                         return usb_sndctrlpipe(udev, epnum);
376                 else
377                         return usb_rcvctrlpipe(udev, epnum);
378         }
379
380         if (usb_endpoint_xfer_bulk(epd)) {
381                 if (dir == USBIP_DIR_OUT)
382                         return usb_sndbulkpipe(udev, epnum);
383                 else
384                         return usb_rcvbulkpipe(udev, epnum);
385         }
386
387         if (usb_endpoint_xfer_int(epd)) {
388                 if (dir == USBIP_DIR_OUT)
389                         return usb_sndintpipe(udev, epnum);
390                 else
391                         return usb_rcvintpipe(udev, epnum);
392         }
393
394         if (usb_endpoint_xfer_isoc(epd)) {
395                 if (dir == USBIP_DIR_OUT)
396                         return usb_sndisocpipe(udev, epnum);
397                 else
398                         return usb_rcvisocpipe(udev, epnum);
399         }
400
401         /* NOT REACHED */
402         dev_err(&sdev->interface->dev, "get pipe, epnum %d\n", epnum);
403         return 0;
404 }
405
406 static void masking_bogus_flags(struct urb *urb)
407 {
408         int                             xfertype;
409         struct usb_device               *dev;
410         struct usb_host_endpoint        *ep;
411         int                             is_out;
412         unsigned int    allowed;
413
414         if (!urb || urb->hcpriv || !urb->complete)
415                 return;
416         dev = urb->dev;
417         if ((!dev) || (dev->state < USB_STATE_UNAUTHENTICATED))
418                 return;
419
420         ep = (usb_pipein(urb->pipe) ? dev->ep_in : dev->ep_out)
421                 [usb_pipeendpoint(urb->pipe)];
422         if (!ep)
423                 return;
424
425         xfertype = usb_endpoint_type(&ep->desc);
426         if (xfertype == USB_ENDPOINT_XFER_CONTROL) {
427                 struct usb_ctrlrequest *setup =
428                         (struct usb_ctrlrequest *) urb->setup_packet;
429
430                 if (!setup)
431                         return;
432                 is_out = !(setup->bRequestType & USB_DIR_IN) ||
433                         !setup->wLength;
434         } else {
435                 is_out = usb_endpoint_dir_out(&ep->desc);
436         }
437
438         /* enforce simple/standard policy */
439         allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT |
440                    URB_DIR_MASK | URB_FREE_BUFFER);
441         switch (xfertype) {
442         case USB_ENDPOINT_XFER_BULK:
443                 if (is_out)
444                         allowed |= URB_ZERO_PACKET;
445                 /* FALLTHROUGH */
446         case USB_ENDPOINT_XFER_CONTROL:
447                 allowed |= URB_NO_FSBR; /* only affects UHCI */
448                 /* FALLTHROUGH */
449         default:                        /* all non-iso endpoints */
450                 if (!is_out)
451                         allowed |= URB_SHORT_NOT_OK;
452                 break;
453         case USB_ENDPOINT_XFER_ISOC:
454                 allowed |= URB_ISO_ASAP;
455                 break;
456         }
457         urb->transfer_flags &= allowed;
458 }
459
460 static void stub_recv_cmd_submit(struct stub_device *sdev,
461                                  struct usbip_header *pdu)
462 {
463         int ret;
464         struct stub_priv *priv;
465         struct usbip_device *ud = &sdev->ud;
466         struct usb_device *udev = sdev->udev;
467         int pipe = get_pipe(sdev, pdu->base.ep, pdu->base.direction);
468
469         priv = stub_priv_alloc(sdev, pdu);
470         if (!priv)
471                 return;
472
473         /* setup a urb */
474         if (usb_pipeisoc(pipe))
475                 priv->urb = usb_alloc_urb(pdu->u.cmd_submit.number_of_packets,
476                                           GFP_KERNEL);
477         else
478                 priv->urb = usb_alloc_urb(0, GFP_KERNEL);
479
480         if (!priv->urb) {
481                 dev_err(&sdev->interface->dev, "malloc urb\n");
482                 usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
483                 return;
484         }
485
486         /* allocate urb transfer buffer, if needed */
487         if (pdu->u.cmd_submit.transfer_buffer_length > 0) {
488                 priv->urb->transfer_buffer =
489                         kzalloc(pdu->u.cmd_submit.transfer_buffer_length,
490                                 GFP_KERNEL);
491                 if (!priv->urb->transfer_buffer) {
492                         usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
493                         return;
494                 }
495         }
496
497         /* copy urb setup packet */
498         priv->urb->setup_packet = kmemdup(&pdu->u.cmd_submit.setup, 8,
499                                           GFP_KERNEL);
500         if (!priv->urb->setup_packet) {
501                 dev_err(&sdev->interface->dev, "allocate setup_packet\n");
502                 usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
503                 return;
504         }
505
506         /* set other members from the base header of pdu */
507         priv->urb->context                = (void *) priv;
508         priv->urb->dev                    = udev;
509         priv->urb->pipe                   = pipe;
510         priv->urb->complete               = stub_complete;
511
512         usbip_pack_pdu(pdu, priv->urb, USBIP_CMD_SUBMIT, 0);
513
514
515         if (usbip_recv_xbuff(ud, priv->urb) < 0)
516                 return;
517
518         if (usbip_recv_iso(ud, priv->urb) < 0)
519                 return;
520
521         /* no need to submit an intercepted request, but harmless? */
522         tweak_special_requests(priv->urb);
523
524         masking_bogus_flags(priv->urb);
525         /* urb is now ready to submit */
526         ret = usb_submit_urb(priv->urb, GFP_KERNEL);
527
528         if (ret == 0)
529                 usbip_dbg_stub_rx("submit urb ok, seqnum %u\n",
530                                   pdu->base.seqnum);
531         else {
532                 dev_err(&sdev->interface->dev, "submit_urb error, %d\n", ret);
533                 usbip_dump_header(pdu);
534                 usbip_dump_urb(priv->urb);
535
536                 /*
537                  * Pessimistic.
538                  * This connection will be discarded.
539                  */
540                 usbip_event_add(ud, SDEV_EVENT_ERROR_SUBMIT);
541         }
542
543         usbip_dbg_stub_rx("Leave\n");
544         return;
545 }
546
547 /* recv a pdu */
548 static void stub_rx_pdu(struct usbip_device *ud)
549 {
550         int ret;
551         struct usbip_header pdu;
552         struct stub_device *sdev = container_of(ud, struct stub_device, ud);
553         struct device *dev = &sdev->interface->dev;
554
555         usbip_dbg_stub_rx("Enter\n");
556
557         memset(&pdu, 0, sizeof(pdu));
558
559         /* receive a pdu header */
560         ret = usbip_recv(ud->tcp_socket, &pdu, sizeof(pdu));
561         if (ret != sizeof(pdu)) {
562                 dev_err(dev, "recv a header, %d\n", ret);
563                 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
564                 return;
565         }
566
567         usbip_header_correct_endian(&pdu, 0);
568
569         if (usbip_dbg_flag_stub_rx)
570                 usbip_dump_header(&pdu);
571
572         if (!valid_request(sdev, &pdu)) {
573                 dev_err(dev, "recv invalid request\n");
574                 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
575                 return;
576         }
577
578         switch (pdu.base.command) {
579         case USBIP_CMD_UNLINK:
580                 stub_recv_cmd_unlink(sdev, &pdu);
581                 break;
582
583         case USBIP_CMD_SUBMIT:
584                 stub_recv_cmd_submit(sdev, &pdu);
585                 break;
586
587         default:
588                 /* NOTREACHED */
589                 dev_err(dev, "unknown pdu\n");
590                 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
591                 break;
592         }
593 }
594
595 int stub_rx_loop(void *data)
596 {
597         struct usbip_device *ud = data;
598
599         while (!kthread_should_stop()) {
600                 if (usbip_event_happened(ud))
601                         break;
602
603                 stub_rx_pdu(ud);
604         }
605
606         return 0;
607 }