2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
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.
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.
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,
20 #include <asm/byteorder.h>
21 #include <linux/kthread.h>
22 #include <linux/usb.h>
23 #include <linux/usb/hcd.h>
25 #include "usbip_common.h"
28 static int is_clear_halt_cmd(struct urb *urb)
30 struct usb_ctrlrequest *req;
32 req = (struct usb_ctrlrequest *) urb->setup_packet;
34 return (req->bRequest == USB_REQ_CLEAR_FEATURE) &&
35 (req->bRequestType == USB_RECIP_ENDPOINT) &&
36 (req->wValue == USB_ENDPOINT_HALT);
39 static int is_set_interface_cmd(struct urb *urb)
41 struct usb_ctrlrequest *req;
43 req = (struct usb_ctrlrequest *) urb->setup_packet;
45 return (req->bRequest == USB_REQ_SET_INTERFACE) &&
46 (req->bRequestType == USB_RECIP_INTERFACE);
49 static int is_set_configuration_cmd(struct urb *urb)
51 struct usb_ctrlrequest *req;
53 req = (struct usb_ctrlrequest *) urb->setup_packet;
55 return (req->bRequest == USB_REQ_SET_CONFIGURATION) &&
56 (req->bRequestType == USB_RECIP_DEVICE);
59 static int is_reset_device_cmd(struct urb *urb)
61 struct usb_ctrlrequest *req;
65 req = (struct usb_ctrlrequest *) urb->setup_packet;
66 value = le16_to_cpu(req->wValue);
67 index = le16_to_cpu(req->wIndex);
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);
78 static int tweak_clear_halt_cmd(struct urb *urb)
80 struct usb_ctrlrequest *req;
86 req = (struct usb_ctrlrequest *) urb->setup_packet;
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
93 target_endp = le16_to_cpu(req->wIndex) & 0x000f;
95 /* the stalled endpoint direction is IN or OUT?. USB_DIR_IN is 0x80. */
96 target_dir = le16_to_cpu(req->wIndex) & 0x0080;
99 target_pipe = usb_rcvctrlpipe(urb->dev, target_endp);
101 target_pipe = usb_sndctrlpipe(urb->dev, target_endp);
103 ret = usb_clear_halt(urb->dev, target_pipe);
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);
109 dev_info(&urb->dev->dev,
110 "usb_clear_halt done: devnum %d endp %d\n",
111 urb->dev->devnum, target_endp);
116 static int tweak_set_interface_cmd(struct urb *urb)
118 struct usb_ctrlrequest *req;
123 req = (struct usb_ctrlrequest *) urb->setup_packet;
124 alternate = le16_to_cpu(req->wValue);
125 interface = le16_to_cpu(req->wIndex);
127 usbip_dbg_stub_rx("set_interface: inf %u alt %u\n",
128 interface, alternate);
130 ret = usb_set_interface(urb->dev, interface, alternate);
132 dev_err(&urb->dev->dev,
133 "usb_set_interface error: inf %u alt %u ret %d\n",
134 interface, alternate, ret);
136 dev_info(&urb->dev->dev,
137 "usb_set_interface done: inf %u alt %u\n",
138 interface, alternate);
143 static int tweak_set_configuration_cmd(struct urb *urb)
145 struct usb_ctrlrequest *req;
148 req = (struct usb_ctrlrequest *) urb->setup_packet;
149 config = le16_to_cpu(req->wValue);
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.
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
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.
167 dev_info(&urb->dev->dev, "usb_set_configuration %d to %s... skip!\n",
168 config, dev_name(&urb->dev->dev));
173 static int tweak_reset_device_cmd(struct urb *urb)
175 struct stub_priv *priv = (struct stub_priv *) urb->context;
176 struct stub_device *sdev = priv->sdev;
178 dev_info(&urb->dev->dev, "usb_queue_reset_device\n");
181 * With the implementation of pre_reset and post_reset the driver no
182 * longer unbinds. This allows the use of synchronous reset.
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");
189 usb_reset_device(sdev->udev);
190 usb_unlock_device(sdev->udev);
196 * clear_halt, set_interface, and set_configuration require special tricks.
198 static void tweak_special_requests(struct urb *urb)
200 if (!urb || !urb->setup_packet)
203 if (usb_pipetype(urb->pipe) != PIPE_CONTROL)
206 if (is_clear_halt_cmd(urb))
207 /* tweak clear_halt */
208 tweak_clear_halt_cmd(urb);
210 else if (is_set_interface_cmd(urb))
211 /* tweak set_interface */
212 tweak_set_interface_cmd(urb);
214 else if (is_set_configuration_cmd(urb))
215 /* tweak set_configuration */
216 tweak_set_configuration_cmd(urb);
218 else if (is_reset_device_cmd(urb))
219 tweak_reset_device_cmd(urb);
221 usbip_dbg_stub_rx("no need to tweak\n");
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.
230 * See also comments about unlinking strategy in vhci_hcd.c.
232 static int stub_recv_cmd_unlink(struct stub_device *sdev,
233 struct usbip_header *pdu)
237 struct stub_priv *priv;
239 spin_lock_irqsave(&sdev->priv_lock, flags);
241 list_for_each_entry(priv, &sdev->priv_init, list) {
242 if (priv->seqnum != pdu->u.cmd_unlink.seqnum)
245 dev_info(&priv->urb->dev->dev, "unlink urb %p\n",
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.
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.
264 priv->seqnum = pdu->base.seqnum;
266 spin_unlock_irqrestore(&sdev->priv_lock, flags);
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?
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 ?
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",
292 usbip_dbg_stub_rx("seqnum %d is not pending\n",
293 pdu->u.cmd_unlink.seqnum);
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.
301 stub_enqueue_ret_unlink(sdev, pdu->base.seqnum, 0);
303 spin_unlock_irqrestore(&sdev->priv_lock, flags);
308 static int valid_request(struct stub_device *sdev, struct usbip_header *pdu)
310 struct usbip_device *ud = &sdev->ud;
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. */
319 spin_unlock_irq(&ud->lock);
325 static struct stub_priv *stub_priv_alloc(struct stub_device *sdev,
326 struct usbip_header *pdu)
328 struct stub_priv *priv;
329 struct usbip_device *ud = &sdev->ud;
332 spin_lock_irqsave(&sdev->priv_lock, flags);
334 priv = kmem_cache_zalloc(stub_priv_cache, GFP_ATOMIC);
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);
342 priv->seqnum = pdu->base.seqnum;
346 * After a stub_priv is linked to a list_head,
347 * our error handler can free allocated data.
349 list_add_tail(&priv->list, &sdev->priv_init);
351 spin_unlock_irqrestore(&sdev->priv_lock, flags);
356 static int get_pipe(struct stub_device *sdev, int epnum, int dir)
358 struct usb_device *udev = sdev->udev;
359 struct usb_host_endpoint *ep;
360 struct usb_endpoint_descriptor *epd = NULL;
362 if (dir == USBIP_DIR_IN)
363 ep = udev->ep_in[epnum & 0x7f];
365 ep = udev->ep_out[epnum & 0x7f];
367 dev_err(&sdev->interface->dev, "no such endpoint?, %d\n",
373 if (usb_endpoint_xfer_control(epd)) {
374 if (dir == USBIP_DIR_OUT)
375 return usb_sndctrlpipe(udev, epnum);
377 return usb_rcvctrlpipe(udev, epnum);
380 if (usb_endpoint_xfer_bulk(epd)) {
381 if (dir == USBIP_DIR_OUT)
382 return usb_sndbulkpipe(udev, epnum);
384 return usb_rcvbulkpipe(udev, epnum);
387 if (usb_endpoint_xfer_int(epd)) {
388 if (dir == USBIP_DIR_OUT)
389 return usb_sndintpipe(udev, epnum);
391 return usb_rcvintpipe(udev, epnum);
394 if (usb_endpoint_xfer_isoc(epd)) {
395 if (dir == USBIP_DIR_OUT)
396 return usb_sndisocpipe(udev, epnum);
398 return usb_rcvisocpipe(udev, epnum);
402 dev_err(&sdev->interface->dev, "get pipe, epnum %d\n", epnum);
406 static void masking_bogus_flags(struct urb *urb)
409 struct usb_device *dev;
410 struct usb_host_endpoint *ep;
412 unsigned int allowed;
414 if (!urb || urb->hcpriv || !urb->complete)
417 if ((!dev) || (dev->state < USB_STATE_UNAUTHENTICATED))
420 ep = (usb_pipein(urb->pipe) ? dev->ep_in : dev->ep_out)
421 [usb_pipeendpoint(urb->pipe)];
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;
432 is_out = !(setup->bRequestType & USB_DIR_IN) ||
435 is_out = usb_endpoint_dir_out(&ep->desc);
438 /* enforce simple/standard policy */
439 allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT |
440 URB_DIR_MASK | URB_FREE_BUFFER);
442 case USB_ENDPOINT_XFER_BULK:
444 allowed |= URB_ZERO_PACKET;
446 case USB_ENDPOINT_XFER_CONTROL:
447 allowed |= URB_NO_FSBR; /* only affects UHCI */
449 default: /* all non-iso endpoints */
451 allowed |= URB_SHORT_NOT_OK;
453 case USB_ENDPOINT_XFER_ISOC:
454 allowed |= URB_ISO_ASAP;
457 urb->transfer_flags &= allowed;
460 static void stub_recv_cmd_submit(struct stub_device *sdev,
461 struct usbip_header *pdu)
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);
469 priv = stub_priv_alloc(sdev, pdu);
474 if (usb_pipeisoc(pipe))
475 priv->urb = usb_alloc_urb(pdu->u.cmd_submit.number_of_packets,
478 priv->urb = usb_alloc_urb(0, GFP_KERNEL);
481 dev_err(&sdev->interface->dev, "malloc urb\n");
482 usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
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,
491 if (!priv->urb->transfer_buffer) {
492 usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
497 /* copy urb setup packet */
498 priv->urb->setup_packet = kmemdup(&pdu->u.cmd_submit.setup, 8,
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);
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;
512 usbip_pack_pdu(pdu, priv->urb, USBIP_CMD_SUBMIT, 0);
515 if (usbip_recv_xbuff(ud, priv->urb) < 0)
518 if (usbip_recv_iso(ud, priv->urb) < 0)
521 /* no need to submit an intercepted request, but harmless? */
522 tweak_special_requests(priv->urb);
524 masking_bogus_flags(priv->urb);
525 /* urb is now ready to submit */
526 ret = usb_submit_urb(priv->urb, GFP_KERNEL);
529 usbip_dbg_stub_rx("submit urb ok, seqnum %u\n",
532 dev_err(&sdev->interface->dev, "submit_urb error, %d\n", ret);
533 usbip_dump_header(pdu);
534 usbip_dump_urb(priv->urb);
538 * This connection will be discarded.
540 usbip_event_add(ud, SDEV_EVENT_ERROR_SUBMIT);
543 usbip_dbg_stub_rx("Leave\n");
548 static void stub_rx_pdu(struct usbip_device *ud)
551 struct usbip_header pdu;
552 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
553 struct device *dev = &sdev->interface->dev;
555 usbip_dbg_stub_rx("Enter\n");
557 memset(&pdu, 0, sizeof(pdu));
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);
567 usbip_header_correct_endian(&pdu, 0);
569 if (usbip_dbg_flag_stub_rx)
570 usbip_dump_header(&pdu);
572 if (!valid_request(sdev, &pdu)) {
573 dev_err(dev, "recv invalid request\n");
574 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
578 switch (pdu.base.command) {
579 case USBIP_CMD_UNLINK:
580 stub_recv_cmd_unlink(sdev, &pdu);
583 case USBIP_CMD_SUBMIT:
584 stub_recv_cmd_submit(sdev, &pdu);
589 dev_err(dev, "unknown pdu\n");
590 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
595 int stub_rx_loop(void *data)
597 struct usbip_device *ud = data;
599 while (!kthread_should_stop()) {
600 if (usbip_event_happened(ud))