• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <linux/kthread.h>
21 #include <linux/socket.h>
22 
23 #include "usbip_common.h"
24 #include "stub.h"
25 
stub_free_priv_and_urb(struct stub_priv * priv)26 static void stub_free_priv_and_urb(struct stub_priv *priv)
27 {
28 	struct urb *urb = priv->urb;
29 
30 	kfree(urb->setup_packet);
31 	kfree(urb->transfer_buffer);
32 	list_del(&priv->list);
33 	kmem_cache_free(stub_priv_cache, priv);
34 	usb_free_urb(urb);
35 }
36 
37 /* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
stub_enqueue_ret_unlink(struct stub_device * sdev,__u32 seqnum,__u32 status)38 void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
39 			     __u32 status)
40 {
41 	struct stub_unlink *unlink;
42 
43 	unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
44 	if (!unlink) {
45 		usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
46 		return;
47 	}
48 
49 	unlink->seqnum = seqnum;
50 	unlink->status = status;
51 
52 	list_add_tail(&unlink->list, &sdev->unlink_tx);
53 }
54 
55 /**
56  * stub_complete - completion handler of a usbip urb
57  * @urb: pointer to the urb completed
58  *
59  * When a urb has completed, the USB core driver calls this function mostly in
60  * the interrupt context. To return the result of a urb, the completed urb is
61  * linked to the pending list of returning.
62  *
63  */
stub_complete(struct urb * urb)64 void stub_complete(struct urb *urb)
65 {
66 	struct stub_priv *priv = (struct stub_priv *) urb->context;
67 	struct stub_device *sdev = priv->sdev;
68 	unsigned long flags;
69 
70 	usbip_dbg_stub_tx("complete! status %d\n", urb->status);
71 
72 	switch (urb->status) {
73 	case 0:
74 		/* OK */
75 		break;
76 	case -ENOENT:
77 		dev_info(&urb->dev->dev, "stopped by a call to usb_kill_urb() "
78 			 "because of cleaning up a virtual connection\n");
79 		return;
80 	case -ECONNRESET:
81 		dev_info(&urb->dev->dev, "unlinked by a call to "
82 			 "usb_unlink_urb()\n");
83 		break;
84 	case -EPIPE:
85 		dev_info(&urb->dev->dev, "endpoint %d is stalled\n",
86 			 usb_pipeendpoint(urb->pipe));
87 		break;
88 	case -ESHUTDOWN:
89 		dev_info(&urb->dev->dev, "device removed?\n");
90 		break;
91 	default:
92 		dev_info(&urb->dev->dev, "urb completion with non-zero status "
93 			 "%d\n", urb->status);
94 		break;
95 	}
96 
97 	/* link a urb to the queue of tx. */
98 	spin_lock_irqsave(&sdev->priv_lock, flags);
99 	if (priv->unlinking) {
100 		stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
101 		stub_free_priv_and_urb(priv);
102 	} else {
103 		list_move_tail(&priv->list, &sdev->priv_tx);
104 	}
105 	spin_unlock_irqrestore(&sdev->priv_lock, flags);
106 
107 	/* wake up tx_thread */
108 	wake_up(&sdev->tx_waitq);
109 }
110 
setup_base_pdu(struct usbip_header_basic * base,__u32 command,__u32 seqnum)111 static inline void setup_base_pdu(struct usbip_header_basic *base,
112 				  __u32 command, __u32 seqnum)
113 {
114 	base->command	= command;
115 	base->seqnum	= seqnum;
116 	base->devid	= 0;
117 	base->ep	= 0;
118 	base->direction = 0;
119 }
120 
setup_ret_submit_pdu(struct usbip_header * rpdu,struct urb * urb)121 static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
122 {
123 	struct stub_priv *priv = (struct stub_priv *) urb->context;
124 
125 	setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
126 	usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
127 }
128 
setup_ret_unlink_pdu(struct usbip_header * rpdu,struct stub_unlink * unlink)129 static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
130 				 struct stub_unlink *unlink)
131 {
132 	setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
133 	rpdu->u.ret_unlink.status = unlink->status;
134 }
135 
dequeue_from_priv_tx(struct stub_device * sdev)136 static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
137 {
138 	unsigned long flags;
139 	struct stub_priv *priv, *tmp;
140 
141 	spin_lock_irqsave(&sdev->priv_lock, flags);
142 
143 	list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
144 		list_move_tail(&priv->list, &sdev->priv_free);
145 		spin_unlock_irqrestore(&sdev->priv_lock, flags);
146 		return priv;
147 	}
148 
149 	spin_unlock_irqrestore(&sdev->priv_lock, flags);
150 
151 	return NULL;
152 }
153 
stub_send_ret_submit(struct stub_device * sdev)154 static int stub_send_ret_submit(struct stub_device *sdev)
155 {
156 	unsigned long flags;
157 	struct stub_priv *priv, *tmp;
158 
159 	struct msghdr msg;
160 	size_t txsize;
161 
162 	size_t total_size = 0;
163 
164 	while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
165 		int ret;
166 		struct urb *urb = priv->urb;
167 		struct usbip_header pdu_header;
168 		struct usbip_iso_packet_descriptor *iso_buffer = NULL;
169 		struct kvec *iov = NULL;
170 		int iovnum = 0;
171 
172 		txsize = 0;
173 		memset(&pdu_header, 0, sizeof(pdu_header));
174 		memset(&msg, 0, sizeof(msg));
175 
176 		if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
177 			iovnum = 2 + urb->number_of_packets;
178 		else
179 			iovnum = 2;
180 
181 		iov = kzalloc(iovnum * sizeof(struct kvec), GFP_KERNEL);
182 
183 		if (!iov) {
184 			usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_MALLOC);
185 			return -1;
186 		}
187 
188 		iovnum = 0;
189 
190 		/* 1. setup usbip_header */
191 		setup_ret_submit_pdu(&pdu_header, urb);
192 		usbip_dbg_stub_tx("setup txdata seqnum: %d urb: %p\n",
193 				  pdu_header.base.seqnum, urb);
194 		usbip_header_correct_endian(&pdu_header, 1);
195 
196 		iov[iovnum].iov_base = &pdu_header;
197 		iov[iovnum].iov_len  = sizeof(pdu_header);
198 		iovnum++;
199 		txsize += sizeof(pdu_header);
200 
201 		/* 2. setup transfer buffer */
202 		if (usb_pipein(urb->pipe) &&
203 		    usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS &&
204 		    urb->actual_length > 0) {
205 			iov[iovnum].iov_base = urb->transfer_buffer;
206 			iov[iovnum].iov_len  = urb->actual_length;
207 			iovnum++;
208 			txsize += urb->actual_length;
209 		} else if (usb_pipein(urb->pipe) &&
210 			   usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
211 			/*
212 			 * For isochronous packets: actual length is the sum of
213 			 * the actual length of the individual, packets, but as
214 			 * the packet offsets are not changed there will be
215 			 * padding between the packets. To optimally use the
216 			 * bandwidth the padding is not transmitted.
217 			 */
218 
219 			int i;
220 			for (i = 0; i < urb->number_of_packets; i++) {
221 				iov[iovnum].iov_base = urb->transfer_buffer +
222 					urb->iso_frame_desc[i].offset;
223 				iov[iovnum].iov_len =
224 					urb->iso_frame_desc[i].actual_length;
225 				iovnum++;
226 				txsize += urb->iso_frame_desc[i].actual_length;
227 			}
228 
229 			if (txsize != sizeof(pdu_header) + urb->actual_length) {
230 				dev_err(&sdev->interface->dev,
231 					"actual length of urb %d does not "
232 					"match iso packet sizes %zu\n",
233 					urb->actual_length,
234 					txsize-sizeof(pdu_header));
235 				kfree(iov);
236 				usbip_event_add(&sdev->ud,
237 						SDEV_EVENT_ERROR_TCP);
238 			   return -1;
239 			}
240 		}
241 
242 		/* 3. setup iso_packet_descriptor */
243 		if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
244 			ssize_t len = 0;
245 
246 			iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
247 			if (!iso_buffer) {
248 				usbip_event_add(&sdev->ud,
249 						SDEV_EVENT_ERROR_MALLOC);
250 				kfree(iov);
251 				return -1;
252 			}
253 
254 			iov[iovnum].iov_base = iso_buffer;
255 			iov[iovnum].iov_len  = len;
256 			txsize += len;
257 			iovnum++;
258 		}
259 
260 		ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg,
261 						iov,  iovnum, txsize);
262 		if (ret != txsize) {
263 			dev_err(&sdev->interface->dev,
264 				"sendmsg failed!, retval %d for %zd\n",
265 				ret, txsize);
266 			kfree(iov);
267 			kfree(iso_buffer);
268 			usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
269 			return -1;
270 		}
271 
272 		kfree(iov);
273 		kfree(iso_buffer);
274 
275 		total_size += txsize;
276 	}
277 
278 	spin_lock_irqsave(&sdev->priv_lock, flags);
279 	list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
280 		stub_free_priv_and_urb(priv);
281 	}
282 	spin_unlock_irqrestore(&sdev->priv_lock, flags);
283 
284 	return total_size;
285 }
286 
dequeue_from_unlink_tx(struct stub_device * sdev)287 static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
288 {
289 	unsigned long flags;
290 	struct stub_unlink *unlink, *tmp;
291 
292 	spin_lock_irqsave(&sdev->priv_lock, flags);
293 
294 	list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
295 		list_move_tail(&unlink->list, &sdev->unlink_free);
296 		spin_unlock_irqrestore(&sdev->priv_lock, flags);
297 		return unlink;
298 	}
299 
300 	spin_unlock_irqrestore(&sdev->priv_lock, flags);
301 
302 	return NULL;
303 }
304 
stub_send_ret_unlink(struct stub_device * sdev)305 static int stub_send_ret_unlink(struct stub_device *sdev)
306 {
307 	unsigned long flags;
308 	struct stub_unlink *unlink, *tmp;
309 
310 	struct msghdr msg;
311 	struct kvec iov[1];
312 	size_t txsize;
313 
314 	size_t total_size = 0;
315 
316 	while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
317 		int ret;
318 		struct usbip_header pdu_header;
319 
320 		txsize = 0;
321 		memset(&pdu_header, 0, sizeof(pdu_header));
322 		memset(&msg, 0, sizeof(msg));
323 		memset(&iov, 0, sizeof(iov));
324 
325 		usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
326 
327 		/* 1. setup usbip_header */
328 		setup_ret_unlink_pdu(&pdu_header, unlink);
329 		usbip_header_correct_endian(&pdu_header, 1);
330 
331 		iov[0].iov_base = &pdu_header;
332 		iov[0].iov_len  = sizeof(pdu_header);
333 		txsize += sizeof(pdu_header);
334 
335 		ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
336 				     1, txsize);
337 		if (ret != txsize) {
338 			dev_err(&sdev->interface->dev,
339 				"sendmsg failed!, retval %d for %zd\n",
340 				ret, txsize);
341 			usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
342 			return -1;
343 		}
344 
345 		usbip_dbg_stub_tx("send txdata\n");
346 		total_size += txsize;
347 	}
348 
349 	spin_lock_irqsave(&sdev->priv_lock, flags);
350 
351 	list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
352 		list_del(&unlink->list);
353 		kfree(unlink);
354 	}
355 
356 	spin_unlock_irqrestore(&sdev->priv_lock, flags);
357 
358 	return total_size;
359 }
360 
stub_tx_loop(void * data)361 int stub_tx_loop(void *data)
362 {
363 	struct usbip_device *ud = data;
364 	struct stub_device *sdev = container_of(ud, struct stub_device, ud);
365 
366 	while (!kthread_should_stop()) {
367 		if (usbip_event_happened(ud))
368 			break;
369 
370 		/*
371 		 * send_ret_submit comes earlier than send_ret_unlink.  stub_rx
372 		 * looks at only priv_init queue. If the completion of a URB is
373 		 * earlier than the receive of CMD_UNLINK, priv is moved to
374 		 * priv_tx queue and stub_rx does not find the target priv. In
375 		 * this case, vhci_rx receives the result of the submit request
376 		 * and then receives the result of the unlink request. The
377 		 * result of the submit is given back to the usbcore as the
378 		 * completion of the unlink request. The request of the
379 		 * unlink is ignored. This is ok because a driver who calls
380 		 * usb_unlink_urb() understands the unlink was too late by
381 		 * getting the status of the given-backed URB which has the
382 		 * status of usb_submit_urb().
383 		 */
384 		if (stub_send_ret_submit(sdev) < 0)
385 			break;
386 
387 		if (stub_send_ret_unlink(sdev) < 0)
388 			break;
389 
390 		wait_event_interruptible(sdev->tx_waitq,
391 					 (!list_empty(&sdev->priv_tx) ||
392 					  !list_empty(&sdev->unlink_tx) ||
393 					  kthread_should_stop()));
394 	}
395 
396 	return 0;
397 }
398