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