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