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 "usbip_common.h"
22 #include "../../usb/core/hcd.h"
25 static int is_clear_halt_cmd(struct urb *urb)
27 struct usb_ctrlrequest *req;
29 req = (struct usb_ctrlrequest *) urb->setup_packet;
31 return (req->bRequest == USB_REQ_CLEAR_FEATURE) &&
32 (req->bRequestType == USB_RECIP_ENDPOINT) &&
33 (req->wValue == USB_ENDPOINT_HALT);
36 static int is_set_interface_cmd(struct urb *urb)
38 struct usb_ctrlrequest *req;
40 req = (struct usb_ctrlrequest *) urb->setup_packet;
42 return (req->bRequest == USB_REQ_SET_INTERFACE) &&
43 (req->bRequestType == USB_RECIP_INTERFACE);
46 static int is_set_configuration_cmd(struct urb *urb)
48 struct usb_ctrlrequest *req;
50 req = (struct usb_ctrlrequest *) urb->setup_packet;
52 return (req->bRequest == USB_REQ_SET_CONFIGURATION) &&
53 (req->bRequestType == USB_RECIP_DEVICE);
56 static int is_reset_device_cmd(struct urb *urb)
58 struct usb_ctrlrequest *req;
62 req = (struct usb_ctrlrequest *) urb->setup_packet;
63 value = le16_to_cpu(req->wValue);
64 index = le16_to_cpu(req->wIndex);
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);
75 static int tweak_clear_halt_cmd(struct urb *urb)
77 struct usb_ctrlrequest *req;
83 req = (struct usb_ctrlrequest *) urb->setup_packet;
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
90 target_endp = le16_to_cpu(req->wIndex) & 0x000f;
92 /* the stalled endpoint direction is IN or OUT?. USB_DIR_IN is 0x80. */
93 target_dir = le16_to_cpu(req->wIndex) & 0x0080;
96 target_pipe = usb_rcvctrlpipe(urb->dev, target_endp);
98 target_pipe = usb_sndctrlpipe(urb->dev, target_endp);
100 ret = usb_clear_halt(urb->dev, target_pipe);
102 uinfo("clear_halt error: devnum %d endp %d, %d\n",
103 urb->dev->devnum, target_endp, ret);
105 uinfo("clear_halt done: devnum %d endp %d\n",
106 urb->dev->devnum, target_endp);
111 static int tweak_set_interface_cmd(struct urb *urb)
113 struct usb_ctrlrequest *req;
118 req = (struct usb_ctrlrequest *) urb->setup_packet;
119 alternate = le16_to_cpu(req->wValue);
120 interface = le16_to_cpu(req->wIndex);
122 dbg_stub_rx("set_interface: inf %u alt %u\n", interface, alternate);
124 ret = usb_set_interface(urb->dev, interface, alternate);
126 uinfo("set_interface error: inf %u alt %u, %d\n",
127 interface, alternate, ret);
129 uinfo("set_interface done: inf %u alt %u\n",
136 static int tweak_set_configuration_cmd(struct urb *urb)
138 struct usb_ctrlrequest *req;
141 req = (struct usb_ctrlrequest *) urb->setup_packet;
142 config = le16_to_cpu(req->wValue);
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.
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
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.
160 uinfo("set_configuration (%d) to %s\n", config, dev_name(&urb->dev->dev));
161 uinfo("but, skip!\n");
164 /* return usb_driver_set_configuration(urb->dev, config); */
167 static int tweak_reset_device_cmd(struct urb *urb)
169 struct usb_ctrlrequest *req;
174 req = (struct usb_ctrlrequest *) urb->setup_packet;
175 value = le16_to_cpu(req->wValue);
176 index = le16_to_cpu(req->wIndex);
178 uinfo("reset_device (port %d) to %s\n", index, dev_name(&urb->dev->dev));
180 /* all interfaces should be owned by usbip driver, so just reset it. */
181 ret = usb_lock_device_for_reset(urb->dev, NULL);
183 dev_err(&urb->dev->dev, "lock for reset\n");
187 /* try to reset the device */
188 ret = usb_reset_device(urb->dev);
190 dev_err(&urb->dev->dev, "device reset\n");
192 usb_unlock_device(urb->dev);
198 * clear_halt, set_interface, and set_configuration require special tricks.
200 static void tweak_special_requests(struct urb *urb)
202 if (!urb || !urb->setup_packet)
205 if (usb_pipetype(urb->pipe) != PIPE_CONTROL)
208 if (is_clear_halt_cmd(urb))
209 /* tweak clear_halt */
210 tweak_clear_halt_cmd(urb);
212 else if (is_set_interface_cmd(urb))
213 /* tweak set_interface */
214 tweak_set_interface_cmd(urb);
216 else if (is_set_configuration_cmd(urb))
217 /* tweak set_configuration */
218 tweak_set_configuration_cmd(urb);
220 else if (is_reset_device_cmd(urb))
221 tweak_reset_device_cmd(urb);
223 dbg_stub_rx("no need to tweak\n");
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.
232 * See also comments about unlinking strategy in vhci_hcd.c.
234 static int stub_recv_cmd_unlink(struct stub_device *sdev,
235 struct usbip_header *pdu)
239 struct stub_priv *priv;
242 spin_lock_irqsave(&sdev->priv_lock, flags);
244 list_for_each_entry(priv, &sdev->priv_init, list) {
245 if (priv->seqnum == pdu->u.cmd_unlink.seqnum) {
248 dev_info(&priv->urb->dev->dev, "unlink urb %p\n",
252 * This matched urb is not completed yet (i.e., be in
253 * flight in usb hcd hardware/driver). Now we are
254 * cancelling it. The unlinking flag means that we are
255 * now not going to return the normal result pdu of a
256 * submission request, but going to return a result pdu
257 * of the unlink request.
262 * In the case that unlinking flag is on, prev->seqnum
263 * is changed from the seqnum of the cancelling urb to
264 * the seqnum of the unlink request. This will be used
265 * to make the result pdu of the unlink request.
267 priv->seqnum = pdu->base.seqnum;
269 spin_unlock_irqrestore(&sdev->priv_lock, flags);
272 * usb_unlink_urb() is now out of spinlocking to avoid
273 * spinlock recursion since stub_complete() is
274 * sometimes called in this context but not in the
275 * interrupt context. If stub_complete() is executed
276 * before we call usb_unlink_urb(), usb_unlink_urb()
277 * will return an error value. In this case, stub_tx
278 * will return the result pdu of this unlink request
279 * though submission is completed and actual unlinking
280 * is not executed. OK?
282 /* In the above case, urb->status is not -ECONNRESET,
283 * so a driver in a client host will know the failure
284 * of the unlink request ?
286 ret = usb_unlink_urb(priv->urb);
287 if (ret != -EINPROGRESS)
288 dev_err(&priv->urb->dev->dev,
289 "failed to unlink a urb %p, ret %d\n",
295 dbg_stub_rx("seqnum %d is not pending\n", pdu->u.cmd_unlink.seqnum);
298 * The urb of the unlink target is not found in priv_init queue. It was
299 * already completed and its results is/was going to be sent by a
300 * CMD_RET pdu. In this case, usb_unlink_urb() is not needed. We only
301 * return the completeness of this unlink request to vhci_hcd.
303 stub_enqueue_ret_unlink(sdev, pdu->base.seqnum, 0);
305 spin_unlock_irqrestore(&sdev->priv_lock, flags);
311 static int valid_request(struct stub_device *sdev, struct usbip_header *pdu)
313 struct usbip_device *ud = &sdev->ud;
315 if (pdu->base.devid == sdev->devid) {
316 spin_lock(&ud->lock);
317 if (ud->status == SDEV_ST_USED) {
318 /* A request is valid. */
319 spin_unlock(&ud->lock);
322 spin_unlock(&ud->lock);
328 static struct stub_priv *stub_priv_alloc(struct stub_device *sdev,
329 struct usbip_header *pdu)
331 struct stub_priv *priv;
332 struct usbip_device *ud = &sdev->ud;
335 spin_lock_irqsave(&sdev->priv_lock, flags);
337 priv = kmem_cache_zalloc(stub_priv_cache, GFP_ATOMIC);
339 dev_err(&sdev->interface->dev, "alloc stub_priv\n");
340 spin_unlock_irqrestore(&sdev->priv_lock, flags);
341 usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
345 priv->seqnum = pdu->base.seqnum;
349 * After a stub_priv is linked to a list_head,
350 * our error handler can free allocated data.
352 list_add_tail(&priv->list, &sdev->priv_init);
354 spin_unlock_irqrestore(&sdev->priv_lock, flags);
360 static struct usb_host_endpoint *get_ep_from_epnum(struct usb_device *udev,
363 struct usb_host_config *config;
365 struct usb_host_endpoint *ep = NULL;
372 config = udev->actconfig;
376 for (i = 0; i < config->desc.bNumInterfaces; i++) {
377 struct usb_host_interface *setting;
379 setting = config->interface[i]->cur_altsetting;
381 for (j = 0; j < setting->desc.bNumEndpoints; j++) {
382 ep = &setting->endpoint[j];
383 epnum = (ep->desc.bEndpointAddress & 0x7f);
385 if (epnum == epnum0) {
386 /* uinfo("found epnum %d\n", epnum0); */
400 static int get_pipe(struct stub_device *sdev, int epnum, int dir)
402 struct usb_device *udev = interface_to_usbdev(sdev->interface);
403 struct usb_host_endpoint *ep;
404 struct usb_endpoint_descriptor *epd = NULL;
406 ep = get_ep_from_epnum(udev, epnum);
408 dev_err(&sdev->interface->dev, "no such endpoint?, %d\n",
417 /* epnum 0 is always control */
419 if (dir == USBIP_DIR_OUT)
420 return usb_sndctrlpipe(udev, 0);
422 return usb_rcvctrlpipe(udev, 0);
426 if (usb_endpoint_xfer_control(epd)) {
427 if (dir == USBIP_DIR_OUT)
428 return usb_sndctrlpipe(udev, epnum);
430 return usb_rcvctrlpipe(udev, epnum);
433 if (usb_endpoint_xfer_bulk(epd)) {
434 if (dir == USBIP_DIR_OUT)
435 return usb_sndbulkpipe(udev, epnum);
437 return usb_rcvbulkpipe(udev, epnum);
440 if (usb_endpoint_xfer_int(epd)) {
441 if (dir == USBIP_DIR_OUT)
442 return usb_sndintpipe(udev, epnum);
444 return usb_rcvintpipe(udev, epnum);
447 if (usb_endpoint_xfer_isoc(epd)) {
448 if (dir == USBIP_DIR_OUT)
449 return usb_sndisocpipe(udev, epnum);
451 return usb_rcvisocpipe(udev, epnum);
455 dev_err(&sdev->interface->dev, "get pipe, epnum %d\n", epnum);
459 static void stub_recv_cmd_submit(struct stub_device *sdev,
460 struct usbip_header *pdu)
463 struct stub_priv *priv;
464 struct usbip_device *ud = &sdev->ud;
465 struct usb_device *udev = interface_to_usbdev(sdev->interface);
466 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 /* set priv->urb->transfer_buffer */
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 dev_err(&sdev->interface->dev, "malloc x_buff\n");
493 usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
498 /* set priv->urb->setup_packet */
499 priv->urb->setup_packet = kzalloc(8, 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);
505 memcpy(priv->urb->setup_packet, &pdu->u.cmd_submit.setup, 8);
507 /* set other members from the base header of pdu */
508 priv->urb->context = (void *) priv;
509 priv->urb->dev = udev;
510 priv->urb->pipe = pipe;
511 priv->urb->complete = stub_complete;
513 usbip_pack_pdu(pdu, priv->urb, USBIP_CMD_SUBMIT, 0);
516 if (usbip_recv_xbuff(ud, priv->urb) < 0)
519 if (usbip_recv_iso(ud, priv->urb) < 0)
522 /* no need to submit an intercepted request, but harmless? */
523 tweak_special_requests(priv->urb);
525 /* urb is now ready to submit */
526 ret = usb_submit_urb(priv->urb, GFP_KERNEL);
529 dbg_stub_rx("submit urb ok, seqnum %u\n", pdu->base.seqnum);
531 dev_err(&sdev->interface->dev, "submit_urb error, %d\n", ret);
532 usbip_dump_header(pdu);
533 usbip_dump_urb(priv->urb);
537 * This connection will be discarded.
539 usbip_event_add(ud, SDEV_EVENT_ERROR_SUBMIT);
542 dbg_stub_rx("Leave\n");
547 static void stub_rx_pdu(struct usbip_device *ud)
550 struct usbip_header pdu;
551 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
552 struct device *dev = &sdev->interface->dev;
554 dbg_stub_rx("Enter\n");
556 memset(&pdu, 0, sizeof(pdu));
558 /* 1. receive a pdu header */
559 ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
560 if (ret != sizeof(pdu)) {
561 dev_err(dev, "recv a header, %d\n", ret);
562 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
566 usbip_header_correct_endian(&pdu, 0);
568 if (dbg_flag_stub_rx)
569 usbip_dump_header(&pdu);
571 if (!valid_request(sdev, &pdu)) {
572 dev_err(dev, "recv invalid request\n");
573 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
577 switch (pdu.base.command) {
578 case USBIP_CMD_UNLINK:
579 stub_recv_cmd_unlink(sdev, &pdu);
582 case USBIP_CMD_SUBMIT:
583 stub_recv_cmd_submit(sdev, &pdu);
588 dev_err(dev, "unknown pdu\n");
589 usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
595 void stub_rx_loop(struct usbip_task *ut)
597 struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx);
600 if (signal_pending(current)) {
601 dbg_stub_rx("signal caught!\n");
605 if (usbip_event_happend(ud))