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 <linux/kthread.h>
21 #include <linux/socket.h>
23 #include "usbip_common.h"
26 static void stub_free_priv_and_urb(struct stub_priv *priv)
28 struct urb *urb = priv->urb;
30 kfree(urb->setup_packet);
31 urb->setup_packet = NULL;
33 kfree(urb->transfer_buffer);
34 urb->transfer_buffer = NULL;
36 list_del(&priv->list);
37 kmem_cache_free(stub_priv_cache, priv);
41 /* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
42 void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
45 struct stub_unlink *unlink;
47 unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
49 dev_err(&sdev->interface->dev, "alloc stub_unlink\n");
50 usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
54 unlink->seqnum = seqnum;
55 unlink->status = status;
57 list_add_tail(&unlink->list, &sdev->unlink_tx);
61 * stub_complete - completion handler of a usbip urb
62 * @urb: pointer to the urb completed
64 * When a urb has completed, the USB core driver calls this function mostly in
65 * the interrupt context. To return the result of a urb, the completed urb is
66 * linked to the pending list of returning.
69 void stub_complete(struct urb *urb)
71 struct stub_priv *priv = (struct stub_priv *) urb->context;
72 struct stub_device *sdev = priv->sdev;
75 usbip_dbg_stub_tx("complete! status %d\n", urb->status);
77 switch (urb->status) {
82 dev_info(&urb->dev->dev, "stopped by a call to usb_kill_urb() "
83 "because of cleaning up a virtual connection\n");
86 dev_info(&urb->dev->dev, "unlinked by a call to "
87 "usb_unlink_urb()\n");
90 dev_info(&urb->dev->dev, "endpoint %d is stalled\n",
91 usb_pipeendpoint(urb->pipe));
94 dev_info(&urb->dev->dev, "device removed?\n");
97 dev_info(&urb->dev->dev, "urb completion with non-zero status "
102 /* link a urb to the queue of tx. */
103 spin_lock_irqsave(&sdev->priv_lock, flags);
104 if (priv->unlinking) {
105 stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
106 stub_free_priv_and_urb(priv);
108 list_move_tail(&priv->list, &sdev->priv_tx);
110 spin_unlock_irqrestore(&sdev->priv_lock, flags);
112 /* wake up tx_thread */
113 wake_up(&sdev->tx_waitq);
116 static inline void setup_base_pdu(struct usbip_header_basic *base,
117 __u32 command, __u32 seqnum)
119 base->command = command;
120 base->seqnum = seqnum;
126 static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
128 struct stub_priv *priv = (struct stub_priv *) urb->context;
130 setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
131 usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
134 static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
135 struct stub_unlink *unlink)
137 setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
138 rpdu->u.ret_unlink.status = unlink->status;
141 static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
144 struct stub_priv *priv, *tmp;
146 spin_lock_irqsave(&sdev->priv_lock, flags);
148 list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
149 list_move_tail(&priv->list, &sdev->priv_free);
150 spin_unlock_irqrestore(&sdev->priv_lock, flags);
154 spin_unlock_irqrestore(&sdev->priv_lock, flags);
159 static int stub_send_ret_submit(struct stub_device *sdev)
162 struct stub_priv *priv, *tmp;
167 size_t total_size = 0;
169 while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
171 struct urb *urb = priv->urb;
172 struct usbip_header pdu_header;
173 void *iso_buffer = NULL;
174 struct kvec *iov = NULL;
178 memset(&pdu_header, 0, sizeof(pdu_header));
179 memset(&msg, 0, sizeof(msg));
181 if (urb->actual_length > 0 && !urb->transfer_buffer) {
182 dev_err(&sdev->interface->dev,
183 "urb: actual_length %d transfer_buffer null\n",
188 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
189 iovnum = 2 + urb->number_of_packets;
193 iov = kzalloc(iovnum * sizeof(struct kvec), GFP_KERNEL);
196 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_MALLOC);
202 /* 1. setup usbip_header */
203 setup_ret_submit_pdu(&pdu_header, urb);
204 usbip_dbg_stub_tx("setup txdata seqnum: %d\n",
205 pdu_header.base.seqnum);
206 /*usbip_dump_header(pdu_header);*/
207 usbip_header_correct_endian(&pdu_header, 1);
209 iov[iovnum].iov_base = &pdu_header;
210 iov[iovnum].iov_len = sizeof(pdu_header);
212 txsize += sizeof(pdu_header);
214 /* 2. setup transfer buffer */
215 if (usb_pipein(urb->pipe) &&
216 usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS &&
217 urb->actual_length > 0) {
218 iov[iovnum].iov_base = urb->transfer_buffer;
219 iov[iovnum].iov_len = urb->actual_length;
221 txsize += urb->actual_length;
222 } else if (usb_pipein(urb->pipe) &&
223 usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
225 * For isochronous packets: actual length is the sum of
226 * the actual length of the individual, packets, but as
227 * the packet offsets are not changed there will be
228 * padding between the packets. To optimally use the
229 * bandwidth the padding is not transmitted.
233 for (i = 0; i < urb->number_of_packets; i++) {
234 iov[iovnum].iov_base = urb->transfer_buffer +
235 urb->iso_frame_desc[i].offset;
236 iov[iovnum].iov_len =
237 urb->iso_frame_desc[i].actual_length;
239 txsize += urb->iso_frame_desc[i].actual_length;
242 if (txsize != sizeof(pdu_header) + urb->actual_length) {
243 dev_err(&sdev->interface->dev,
244 "actual length of urb %d does not "
245 "match iso packet sizes %zu\n",
247 txsize-sizeof(pdu_header));
249 usbip_event_add(&sdev->ud,
250 SDEV_EVENT_ERROR_TCP);
255 /* 3. setup iso_packet_descriptor */
256 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
259 iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
261 usbip_event_add(&sdev->ud,
262 SDEV_EVENT_ERROR_MALLOC);
267 iov[iovnum].iov_base = iso_buffer;
268 iov[iovnum].iov_len = len;
273 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg,
274 iov, iovnum, txsize);
276 dev_err(&sdev->interface->dev,
277 "sendmsg failed!, retval %d for %zd\n",
281 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
288 total_size += txsize;
291 spin_lock_irqsave(&sdev->priv_lock, flags);
292 list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
293 stub_free_priv_and_urb(priv);
295 spin_unlock_irqrestore(&sdev->priv_lock, flags);
300 static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
303 struct stub_unlink *unlink, *tmp;
305 spin_lock_irqsave(&sdev->priv_lock, flags);
307 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
308 list_move_tail(&unlink->list, &sdev->unlink_free);
309 spin_unlock_irqrestore(&sdev->priv_lock, flags);
313 spin_unlock_irqrestore(&sdev->priv_lock, flags);
318 static int stub_send_ret_unlink(struct stub_device *sdev)
321 struct stub_unlink *unlink, *tmp;
327 size_t total_size = 0;
329 while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
331 struct usbip_header pdu_header;
334 memset(&pdu_header, 0, sizeof(pdu_header));
335 memset(&msg, 0, sizeof(msg));
336 memset(&iov, 0, sizeof(iov));
338 usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
340 /* 1. setup usbip_header */
341 setup_ret_unlink_pdu(&pdu_header, unlink);
342 usbip_header_correct_endian(&pdu_header, 1);
344 iov[0].iov_base = &pdu_header;
345 iov[0].iov_len = sizeof(pdu_header);
346 txsize += sizeof(pdu_header);
348 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
351 dev_err(&sdev->interface->dev,
352 "sendmsg failed!, retval %d for %zd\n",
354 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
358 usbip_dbg_stub_tx("send txdata\n");
359 total_size += txsize;
362 spin_lock_irqsave(&sdev->priv_lock, flags);
364 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
365 list_del(&unlink->list);
369 spin_unlock_irqrestore(&sdev->priv_lock, flags);
374 int stub_tx_loop(void *data)
376 struct usbip_device *ud = data;
377 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
379 while (!kthread_should_stop()) {
380 if (usbip_event_happened(ud))
384 * send_ret_submit comes earlier than send_ret_unlink. stub_rx
385 * looks at only priv_init queue. If the completion of a URB is
386 * earlier than the receive of CMD_UNLINK, priv is moved to
387 * priv_tx queue and stub_rx does not find the target priv. In
388 * this case, vhci_rx receives the result of the submit request
389 * and then receives the result of the unlink request. The
390 * result of the submit is given back to the usbcore as the
391 * completion of the unlink request. The request of the
392 * unlink is ignored. This is ok because a driver who calls
393 * usb_unlink_urb() understands the unlink was too late by
394 * getting the status of the given-backed URB which has the
395 * status of usb_submit_urb().
397 if (stub_send_ret_submit(sdev) < 0)
400 if (stub_send_ret_unlink(sdev) < 0)
403 wait_event_interruptible(sdev->tx_waitq,
404 (!list_empty(&sdev->priv_tx) ||
405 !list_empty(&sdev->unlink_tx) ||
406 kthread_should_stop()));