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