]>
Commit | Line | Data |
---|---|---|
04679b34 TH |
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 | ||
9720b4bc | 20 | #include <linux/kthread.h> |
7aaacb43 | 21 | #include <linux/slab.h> |
5a0e3ad6 | 22 | |
04679b34 TH |
23 | #include "usbip_common.h" |
24 | #include "vhci.h" | |
25 | ||
b92a5e23 | 26 | /* get URB from transmitted urb queue. caller must hold vdev->priv_lock */ |
5ef6aceb | 27 | struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum) |
04679b34 TH |
28 | { |
29 | struct vhci_priv *priv, *tmp; | |
30 | struct urb *urb = NULL; | |
31 | int status; | |
32 | ||
04679b34 TH |
33 | list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) { |
34 | if (priv->seqnum == seqnum) { | |
35 | urb = priv->urb; | |
36 | status = urb->status; | |
37 | ||
b8868e45 | 38 | usbip_dbg_vhci_rx("find urb %p vurb %p seqnum %u\n", |
5ef6aceb | 39 | urb, priv, seqnum); |
04679b34 TH |
40 | |
41 | /* TODO: fix logic here to improve indent situtation */ | |
42 | if (status != -EINPROGRESS) { | |
43 | if (status == -ENOENT || | |
5ef6aceb | 44 | status == -ECONNRESET) |
04679b34 TH |
45 | dev_info(&urb->dev->dev, |
46 | "urb %p was unlinked " | |
47 | "%ssynchronuously.\n", urb, | |
48 | status == -ENOENT ? "" : "a"); | |
49 | else | |
50 | dev_info(&urb->dev->dev, | |
51 | "urb %p may be in a error, " | |
52 | "status %d\n", urb, status); | |
53 | } | |
54 | ||
55 | list_del(&priv->list); | |
56 | kfree(priv); | |
57 | urb->hcpriv = NULL; | |
58 | ||
59 | break; | |
60 | } | |
61 | } | |
62 | ||
04679b34 TH |
63 | return urb; |
64 | } | |
65 | ||
66 | static void vhci_recv_ret_submit(struct vhci_device *vdev, | |
5ef6aceb | 67 | struct usbip_header *pdu) |
04679b34 TH |
68 | { |
69 | struct usbip_device *ud = &vdev->ud; | |
70 | struct urb *urb; | |
71 | ||
b92a5e23 | 72 | spin_lock(&vdev->priv_lock); |
04679b34 | 73 | urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum); |
b92a5e23 | 74 | spin_unlock(&vdev->priv_lock); |
04679b34 TH |
75 | |
76 | if (!urb) { | |
b8868e45 | 77 | usbip_uerr("cannot find a urb of seqnum %u\n", |
5ef6aceb | 78 | pdu->base.seqnum); |
b8868e45 | 79 | usbip_uinfo("max seqnum %d\n", |
5ef6aceb | 80 | atomic_read(&the_controller->seqnum)); |
04679b34 TH |
81 | usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); |
82 | return; | |
83 | } | |
84 | ||
04679b34 TH |
85 | /* unpack the pdu to a urb */ |
86 | usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0); | |
87 | ||
04679b34 TH |
88 | /* recv transfer buffer */ |
89 | if (usbip_recv_xbuff(ud, urb) < 0) | |
90 | return; | |
91 | ||
04679b34 TH |
92 | /* recv iso_packet_descriptor */ |
93 | if (usbip_recv_iso(ud, urb) < 0) | |
94 | return; | |
95 | ||
28276a28 AM |
96 | /* restore the padding in iso packets */ |
97 | if (usbip_pad_iso(ud, urb) < 0) | |
98 | return; | |
04679b34 | 99 | |
b8868e45 | 100 | if (usbip_dbg_flag_vhci_rx) |
04679b34 TH |
101 | usbip_dump_urb(urb); |
102 | ||
b8868e45 | 103 | usbip_dbg_vhci_rx("now giveback urb %p\n", urb); |
04679b34 TH |
104 | |
105 | spin_lock(&the_controller->lock); | |
106 | usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb); | |
107 | spin_unlock(&the_controller->lock); | |
108 | ||
109 | usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status); | |
110 | ||
b8868e45 | 111 | usbip_dbg_vhci_rx("Leave\n"); |
04679b34 TH |
112 | |
113 | return; | |
114 | } | |
115 | ||
04679b34 | 116 | static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev, |
5ef6aceb | 117 | struct usbip_header *pdu) |
04679b34 TH |
118 | { |
119 | struct vhci_unlink *unlink, *tmp; | |
120 | ||
121 | spin_lock(&vdev->priv_lock); | |
122 | ||
123 | list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) { | |
b8868e45 | 124 | usbip_uinfo("unlink->seqnum %lu\n", unlink->seqnum); |
04679b34 | 125 | if (unlink->seqnum == pdu->base.seqnum) { |
b8868e45 | 126 | usbip_dbg_vhci_rx("found pending unlink, %lu\n", |
5ef6aceb | 127 | unlink->seqnum); |
04679b34 TH |
128 | list_del(&unlink->list); |
129 | ||
130 | spin_unlock(&vdev->priv_lock); | |
131 | return unlink; | |
132 | } | |
133 | } | |
134 | ||
135 | spin_unlock(&vdev->priv_lock); | |
136 | ||
137 | return NULL; | |
138 | } | |
139 | ||
04679b34 | 140 | static void vhci_recv_ret_unlink(struct vhci_device *vdev, |
5ef6aceb | 141 | struct usbip_header *pdu) |
04679b34 TH |
142 | { |
143 | struct vhci_unlink *unlink; | |
144 | struct urb *urb; | |
145 | ||
146 | usbip_dump_header(pdu); | |
147 | ||
148 | unlink = dequeue_pending_unlink(vdev, pdu); | |
149 | if (!unlink) { | |
b8868e45 | 150 | usbip_uinfo("cannot find the pending unlink %u\n", |
5ef6aceb | 151 | pdu->base.seqnum); |
04679b34 TH |
152 | return; |
153 | } | |
154 | ||
b92a5e23 | 155 | spin_lock(&vdev->priv_lock); |
04679b34 | 156 | urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum); |
b92a5e23 MV |
157 | spin_unlock(&vdev->priv_lock); |
158 | ||
04679b34 TH |
159 | if (!urb) { |
160 | /* | |
161 | * I get the result of a unlink request. But, it seems that I | |
162 | * already received the result of its submit result and gave | |
163 | * back the URB. | |
164 | */ | |
b8868e45 | 165 | usbip_uinfo("the urb (seqnum %d) was already given backed\n", |
5ef6aceb | 166 | pdu->base.seqnum); |
04679b34 | 167 | } else { |
b8868e45 | 168 | usbip_dbg_vhci_rx("now giveback urb %p\n", urb); |
04679b34 TH |
169 | |
170 | /* If unlink is succeed, status is -ECONNRESET */ | |
171 | urb->status = pdu->u.ret_unlink.status; | |
b8868e45 | 172 | usbip_uinfo("%d\n", urb->status); |
04679b34 TH |
173 | |
174 | spin_lock(&the_controller->lock); | |
175 | usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb); | |
176 | spin_unlock(&the_controller->lock); | |
177 | ||
178 | usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, | |
5ef6aceb | 179 | urb->status); |
04679b34 TH |
180 | } |
181 | ||
182 | kfree(unlink); | |
183 | ||
184 | return; | |
185 | } | |
186 | ||
bd65f623 MV |
187 | static int vhci_priv_tx_empty(struct vhci_device *vdev) |
188 | { | |
189 | int empty = 0; | |
190 | ||
191 | spin_lock(&vdev->priv_lock); | |
bd65f623 | 192 | empty = list_empty(&vdev->priv_rx); |
bd65f623 MV |
193 | spin_unlock(&vdev->priv_lock); |
194 | ||
195 | return empty; | |
196 | } | |
197 | ||
04679b34 TH |
198 | /* recv a pdu */ |
199 | static void vhci_rx_pdu(struct usbip_device *ud) | |
200 | { | |
201 | int ret; | |
202 | struct usbip_header pdu; | |
203 | struct vhci_device *vdev = container_of(ud, struct vhci_device, ud); | |
204 | ||
b8868e45 | 205 | usbip_dbg_vhci_rx("Enter\n"); |
04679b34 TH |
206 | |
207 | memset(&pdu, 0, sizeof(pdu)); | |
208 | ||
04679b34 TH |
209 | /* 1. receive a pdu header */ |
210 | ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0); | |
7e249c8b MV |
211 | if (ret < 0) { |
212 | if (ret == -ECONNRESET) | |
213 | usbip_uinfo("connection reset by peer\n"); | |
bd65f623 MV |
214 | else if (ret == -EAGAIN) { |
215 | /* ignore if connection was idle */ | |
216 | if (vhci_priv_tx_empty(vdev)) | |
217 | return; | |
218 | usbip_uinfo("connection timed out with pending urbs\n"); | |
219 | } else if (ret != -ERESTARTSYS) | |
7e249c8b | 220 | usbip_uinfo("xmit failed %d\n", ret); |
bd65f623 | 221 | |
7e249c8b MV |
222 | usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); |
223 | return; | |
224 | } | |
225 | if (ret == 0) { | |
226 | usbip_uinfo("connection closed"); | |
227 | usbip_event_add(ud, VDEV_EVENT_DOWN); | |
228 | return; | |
229 | } | |
04679b34 | 230 | if (ret != sizeof(pdu)) { |
7e249c8b | 231 | usbip_uerr("received pdu size is %d, should be %d\n", |
5ef6aceb | 232 | ret, (unsigned int)sizeof(pdu)); |
04679b34 TH |
233 | usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); |
234 | return; | |
235 | } | |
236 | ||
237 | usbip_header_correct_endian(&pdu, 0); | |
238 | ||
b8868e45 | 239 | if (usbip_dbg_flag_vhci_rx) |
04679b34 TH |
240 | usbip_dump_header(&pdu); |
241 | ||
242 | switch (pdu.base.command) { | |
243 | case USBIP_RET_SUBMIT: | |
244 | vhci_recv_ret_submit(vdev, &pdu); | |
245 | break; | |
246 | case USBIP_RET_UNLINK: | |
247 | vhci_recv_ret_unlink(vdev, &pdu); | |
248 | break; | |
249 | default: | |
49aecefc | 250 | /* NOT REACHED */ |
b8868e45 | 251 | usbip_uerr("unknown pdu %u\n", pdu.base.command); |
04679b34 TH |
252 | usbip_dump_header(&pdu); |
253 | usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); | |
49aecefc | 254 | break; |
04679b34 TH |
255 | } |
256 | } | |
257 | ||
9720b4bc | 258 | int vhci_rx_loop(void *data) |
04679b34 | 259 | { |
9720b4bc | 260 | struct usbip_device *ud = data; |
04679b34 | 261 | |
9720b4bc | 262 | while (!kthread_should_stop()) { |
b8868e45 | 263 | if (usbip_event_happened(ud)) |
04679b34 TH |
264 | break; |
265 | ||
266 | vhci_rx_pdu(ud); | |
267 | } | |
04679b34 | 268 | |
9720b4bc AB |
269 | return 0; |
270 | } |