Revert "genirq: Add IRQF_RESUME_EARLY and resume such IRQs earlier"
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / usbip / stub_tx.c
blob523d7ff37cda17bea69fef1f1074aa04aea09147
1 /*
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,
17 * USA.
20 #include "usbip_common.h"
21 #include "stub.h"
24 static void stub_free_priv_and_urb(struct stub_priv *priv)
26 struct urb *urb = priv->urb;
28 kfree(urb->setup_packet);
29 kfree(urb->transfer_buffer);
30 list_del(&priv->list);
31 kmem_cache_free(stub_priv_cache, priv);
32 usb_free_urb(urb);
35 /* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
36 void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
37 __u32 status)
39 struct stub_unlink *unlink;
41 unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
42 if (!unlink) {
43 dev_err(&sdev->interface->dev, "alloc stub_unlink\n");
44 usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
45 return;
48 unlink->seqnum = seqnum;
49 unlink->status = status;
51 list_add_tail(&unlink->list, &sdev->unlink_tx);
54 /**
55 * stub_complete - completion handler of a usbip urb
56 * @urb: pointer to the urb completed
58 * When a urb has completed, the USB core driver calls this function mostly in
59 * the interrupt context. To return the result of a urb, the completed urb is
60 * linked to the pending list of returning.
63 void stub_complete(struct urb *urb)
65 struct stub_priv *priv = (struct stub_priv *) urb->context;
66 struct stub_device *sdev = priv->sdev;
67 unsigned long flags;
69 usbip_dbg_stub_tx("complete! status %d\n", urb->status);
72 switch (urb->status) {
73 case 0:
74 /* OK */
75 break;
76 case -ENOENT:
77 usbip_uinfo("stopped by a call of usb_kill_urb() because of"
78 "cleaning up a virtual connection\n");
79 return;
80 case -ECONNRESET:
81 usbip_uinfo("unlinked by a call of usb_unlink_urb()\n");
82 break;
83 case -EPIPE:
84 usbip_uinfo("endpoint %d is stalled\n",
85 usb_pipeendpoint(urb->pipe));
86 break;
87 case -ESHUTDOWN:
88 usbip_uinfo("device removed?\n");
89 break;
90 default:
91 usbip_uinfo("urb completion with non-zero status %d\n",
92 urb->status);
95 /* link a urb to the queue of tx. */
96 spin_lock_irqsave(&sdev->priv_lock, flags);
98 if (priv->unlinking) {
99 stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
100 stub_free_priv_and_urb(priv);
101 } else
102 list_move_tail(&priv->list, &sdev->priv_tx);
105 spin_unlock_irqrestore(&sdev->priv_lock, flags);
107 /* wake up tx_thread */
108 wake_up(&sdev->tx_waitq);
112 /*-------------------------------------------------------------------------*/
113 /* fill PDU */
115 static inline void setup_base_pdu(struct usbip_header_basic *base,
116 __u32 command, __u32 seqnum)
118 base->command = command;
119 base->seqnum = seqnum;
120 base->devid = 0;
121 base->ep = 0;
122 base->direction = 0;
125 static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
127 struct stub_priv *priv = (struct stub_priv *) urb->context;
129 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);
139 rpdu->u.ret_unlink.status = unlink->status;
143 /*-------------------------------------------------------------------------*/
144 /* send RET_SUBMIT */
146 static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
148 unsigned long flags;
149 struct stub_priv *priv, *tmp;
151 spin_lock_irqsave(&sdev->priv_lock, flags);
153 list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
154 list_move_tail(&priv->list, &sdev->priv_free);
155 spin_unlock_irqrestore(&sdev->priv_lock, flags);
156 return priv;
159 spin_unlock_irqrestore(&sdev->priv_lock, flags);
161 return NULL;
164 static int stub_send_ret_submit(struct stub_device *sdev)
166 unsigned long flags;
167 struct stub_priv *priv, *tmp;
169 struct msghdr msg;
170 size_t txsize;
172 size_t total_size = 0;
174 while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
175 int ret;
176 struct urb *urb = priv->urb;
177 struct usbip_header pdu_header;
178 void *iso_buffer = NULL;
179 struct kvec *iov = NULL;
180 int iovnum = 0;
182 txsize = 0;
183 memset(&pdu_header, 0, sizeof(pdu_header));
184 memset(&msg, 0, sizeof(msg));
186 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
187 iovnum = 2 + urb->number_of_packets;
188 else
189 iovnum = 2;
191 iov = kzalloc(iovnum * sizeof(struct kvec), GFP_KERNEL);
193 if (!iov) {
194 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_MALLOC);
195 return -1;
198 iovnum = 0;
200 /* 1. setup usbip_header */
201 setup_ret_submit_pdu(&pdu_header, urb);
202 usbip_dbg_stub_tx("setup txdata seqnum: %d urb: %p\n",
203 pdu_header.base.seqnum, urb);
204 /*usbip_dump_header(pdu_header);*/
205 usbip_header_correct_endian(&pdu_header, 1);
207 iov[iovnum].iov_base = &pdu_header;
208 iov[iovnum].iov_len = sizeof(pdu_header);
209 iovnum++;
210 txsize += sizeof(pdu_header);
212 /* 2. setup transfer buffer */
213 if (usb_pipein(urb->pipe) &&
214 usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS &&
215 urb->actual_length > 0) {
216 iov[iovnum].iov_base = urb->transfer_buffer;
217 iov[iovnum].iov_len = urb->actual_length;
218 iovnum++;
219 txsize += urb->actual_length;
220 } else if (usb_pipein(urb->pipe) &&
221 usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
223 * For isochronous packets: actual length is the sum of
224 * the actual length of the individual, packets, but as
225 * the packet offsets are not changed there will be
226 * padding between the packets. To optimally use the
227 * bandwidth the padding is not transmitted.
230 int i;
231 for (i = 0; i < urb->number_of_packets; i++) {
232 iov[iovnum].iov_base = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
233 iov[iovnum].iov_len = urb->iso_frame_desc[i].actual_length;
234 iovnum++;
235 txsize += urb->iso_frame_desc[i].actual_length;
238 if (txsize != sizeof(pdu_header) + urb->actual_length) {
239 dev_err(&sdev->interface->dev,
240 "actual length of urb (%d) does not match iso packet sizes (%d)\n",
241 urb->actual_length, txsize-sizeof(pdu_header));
242 kfree(iov);
243 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
244 return -1;
248 /* 3. setup iso_packet_descriptor */
249 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
250 ssize_t len = 0;
252 iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
253 if (!iso_buffer) {
254 usbip_event_add(&sdev->ud,
255 SDEV_EVENT_ERROR_MALLOC);
256 kfree(iov);
257 return -1;
260 iov[iovnum].iov_base = iso_buffer;
261 iov[iovnum].iov_len = len;
262 txsize += len;
263 iovnum++;
266 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg,
267 iov, iovnum, txsize);
268 if (ret != txsize) {
269 dev_err(&sdev->interface->dev,
270 "sendmsg failed!, retval %d for %zd\n",
271 ret, txsize);
272 kfree(iov);
273 kfree(iso_buffer);
274 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
275 return -1;
278 kfree(iov);
279 kfree(iso_buffer);
281 total_size += txsize;
284 spin_lock_irqsave(&sdev->priv_lock, flags);
286 list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
287 stub_free_priv_and_urb(priv);
290 spin_unlock_irqrestore(&sdev->priv_lock, flags);
292 return total_size;
296 /*-------------------------------------------------------------------------*/
297 /* send RET_UNLINK */
299 static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
301 unsigned long flags;
302 struct stub_unlink *unlink, *tmp;
304 spin_lock_irqsave(&sdev->priv_lock, flags);
306 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
307 list_move_tail(&unlink->list, &sdev->unlink_free);
308 spin_unlock_irqrestore(&sdev->priv_lock, flags);
309 return unlink;
312 spin_unlock_irqrestore(&sdev->priv_lock, flags);
314 return NULL;
318 static int stub_send_ret_unlink(struct stub_device *sdev)
320 unsigned long flags;
321 struct stub_unlink *unlink, *tmp;
323 struct msghdr msg;
324 struct kvec iov[1];
325 size_t txsize;
327 size_t total_size = 0;
329 while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
330 int ret;
331 struct usbip_header pdu_header;
333 txsize = 0;
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,
349 1, txsize);
350 if (ret != txsize) {
351 dev_err(&sdev->interface->dev,
352 "sendmsg failed!, retval %d for %zd\n",
353 ret, txsize);
354 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
355 return -1;
359 usbip_dbg_stub_tx("send txdata\n");
361 total_size += txsize;
365 spin_lock_irqsave(&sdev->priv_lock, flags);
367 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
368 list_del(&unlink->list);
369 kfree(unlink);
372 spin_unlock_irqrestore(&sdev->priv_lock, flags);
374 return total_size;
378 /*-------------------------------------------------------------------------*/
380 void stub_tx_loop(struct usbip_task *ut)
382 struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx);
383 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
385 while (1) {
386 if (signal_pending(current)) {
387 usbip_dbg_stub_tx("signal catched\n");
388 break;
391 if (usbip_event_happened(ud))
392 break;
395 * send_ret_submit comes earlier than send_ret_unlink. stub_rx
396 * looks at only priv_init queue. If the completion of a URB is
397 * earlier than the receive of CMD_UNLINK, priv is moved to
398 * priv_tx queue and stub_rx does not find the target priv. In
399 * this case, vhci_rx receives the result of the submit request
400 * and then receives the result of the unlink request. The
401 * result of the submit is given back to the usbcore as the
402 * completion of the unlink request. The request of the
403 * unlink is ignored. This is ok because a driver who calls
404 * usb_unlink_urb() understands the unlink was too late by
405 * getting the status of the given-backed URB which has the
406 * status of usb_submit_urb().
408 if (stub_send_ret_submit(sdev) < 0)
409 break;
411 if (stub_send_ret_unlink(sdev) < 0)
412 break;
414 wait_event_interruptible(sdev->tx_waitq,
415 (!list_empty(&sdev->priv_tx) ||
416 !list_empty(&sdev->unlink_tx)));