Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-2.0+ |
79c02cb1 IK |
2 | /* |
3 | * Copyright (C) 2015 Karol Kosik <karo9@interia.eu> | |
4 | * Copyright (C) 2015-2016 Samsung Electronics | |
5 | * Igor Kotrasinski <i.kotrasinsk@samsung.com> | |
79c02cb1 IK |
6 | */ |
7 | ||
8 | #include <net/sock.h> | |
9 | #include <linux/list.h> | |
10 | #include <linux/kthread.h> | |
11 | ||
12 | #include "usbip_common.h" | |
13 | #include "vudc.h" | |
14 | ||
15 | static int alloc_urb_from_cmd(struct urb **urbp, | |
16 | struct usbip_header *pdu, u8 type) | |
17 | { | |
18 | struct urb *urb; | |
19 | ||
20 | if (type == USB_ENDPOINT_XFER_ISOC) | |
21 | urb = usb_alloc_urb(pdu->u.cmd_submit.number_of_packets, | |
22 | GFP_KERNEL); | |
23 | else | |
24 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
25 | ||
26 | if (!urb) | |
27 | goto err; | |
28 | ||
29 | usbip_pack_pdu(pdu, urb, USBIP_CMD_SUBMIT, 0); | |
30 | ||
31 | if (urb->transfer_buffer_length > 0) { | |
32 | urb->transfer_buffer = kzalloc(urb->transfer_buffer_length, | |
33 | GFP_KERNEL); | |
34 | if (!urb->transfer_buffer) | |
35 | goto free_urb; | |
36 | } | |
37 | ||
38 | urb->setup_packet = kmemdup(&pdu->u.cmd_submit.setup, 8, | |
39 | GFP_KERNEL); | |
40 | if (!urb->setup_packet) | |
41 | goto free_buffer; | |
42 | ||
43 | /* | |
44 | * FIXME - we only setup pipe enough for usbip functions | |
45 | * to behave nicely | |
46 | */ | |
47 | urb->pipe |= pdu->base.direction == USBIP_DIR_IN ? | |
48 | USB_DIR_IN : USB_DIR_OUT; | |
49 | ||
50 | *urbp = urb; | |
51 | return 0; | |
52 | ||
53 | free_buffer: | |
54 | kfree(urb->transfer_buffer); | |
55 | urb->transfer_buffer = NULL; | |
56 | free_urb: | |
57 | usb_free_urb(urb); | |
58 | err: | |
59 | return -ENOMEM; | |
60 | } | |
61 | ||
62 | static int v_recv_cmd_unlink(struct vudc *udc, | |
63 | struct usbip_header *pdu) | |
64 | { | |
65 | unsigned long flags; | |
66 | struct urbp *urb_p; | |
67 | ||
68 | spin_lock_irqsave(&udc->lock, flags); | |
69 | list_for_each_entry(urb_p, &udc->urb_queue, urb_entry) { | |
70 | if (urb_p->seqnum != pdu->u.cmd_unlink.seqnum) | |
71 | continue; | |
72 | urb_p->urb->unlinked = -ECONNRESET; | |
73 | urb_p->seqnum = pdu->base.seqnum; | |
74 | v_kick_timer(udc, jiffies); | |
75 | spin_unlock_irqrestore(&udc->lock, flags); | |
76 | return 0; | |
77 | } | |
78 | /* Not found, completed / not queued */ | |
79 | spin_lock(&udc->lock_tx); | |
80 | v_enqueue_ret_unlink(udc, pdu->base.seqnum, 0); | |
81 | wake_up(&udc->tx_waitq); | |
82 | spin_unlock(&udc->lock_tx); | |
83 | spin_unlock_irqrestore(&udc->lock, flags); | |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
88 | static int v_recv_cmd_submit(struct vudc *udc, | |
89 | struct usbip_header *pdu) | |
90 | { | |
91 | int ret = 0; | |
92 | struct urbp *urb_p; | |
93 | u8 address; | |
94 | unsigned long flags; | |
95 | ||
96 | urb_p = alloc_urbp(); | |
97 | if (!urb_p) { | |
98 | usbip_event_add(&udc->ud, VUDC_EVENT_ERROR_MALLOC); | |
99 | return -ENOMEM; | |
100 | } | |
101 | ||
102 | /* base.ep is pipeendpoint(pipe) */ | |
103 | address = pdu->base.ep; | |
104 | if (pdu->base.direction == USBIP_DIR_IN) | |
105 | address |= USB_DIR_IN; | |
106 | ||
107 | spin_lock_irq(&udc->lock); | |
0255cf9e | 108 | urb_p->ep = vudc_find_endpoint(udc, address); |
79c02cb1 IK |
109 | if (!urb_p->ep) { |
110 | /* we don't know the type, there may be isoc data! */ | |
111 | dev_err(&udc->pdev->dev, "request to nonexistent endpoint"); | |
112 | spin_unlock_irq(&udc->lock); | |
113 | usbip_event_add(&udc->ud, VUDC_EVENT_ERROR_TCP); | |
114 | ret = -EPIPE; | |
115 | goto free_urbp; | |
116 | } | |
117 | urb_p->type = urb_p->ep->type; | |
118 | spin_unlock_irq(&udc->lock); | |
119 | ||
120 | urb_p->new = 1; | |
121 | urb_p->seqnum = pdu->base.seqnum; | |
122 | ||
123 | ret = alloc_urb_from_cmd(&urb_p->urb, pdu, urb_p->ep->type); | |
124 | if (ret) { | |
125 | usbip_event_add(&udc->ud, VUDC_EVENT_ERROR_MALLOC); | |
126 | ret = -ENOMEM; | |
127 | goto free_urbp; | |
128 | } | |
129 | ||
130 | urb_p->urb->status = -EINPROGRESS; | |
131 | ||
132 | /* FIXME: more pipe setup to please usbip_common */ | |
238b7bd9 | 133 | urb_p->urb->pipe &= ~(3 << 30); |
79c02cb1 IK |
134 | switch (urb_p->ep->type) { |
135 | case USB_ENDPOINT_XFER_BULK: | |
136 | urb_p->urb->pipe |= (PIPE_BULK << 30); | |
137 | break; | |
138 | case USB_ENDPOINT_XFER_INT: | |
139 | urb_p->urb->pipe |= (PIPE_INTERRUPT << 30); | |
140 | break; | |
141 | case USB_ENDPOINT_XFER_CONTROL: | |
142 | urb_p->urb->pipe |= (PIPE_CONTROL << 30); | |
143 | break; | |
144 | case USB_ENDPOINT_XFER_ISOC: | |
145 | urb_p->urb->pipe |= (PIPE_ISOCHRONOUS << 30); | |
146 | break; | |
147 | } | |
148 | ret = usbip_recv_xbuff(&udc->ud, urb_p->urb); | |
149 | if (ret < 0) | |
150 | goto free_urbp; | |
151 | ||
152 | ret = usbip_recv_iso(&udc->ud, urb_p->urb); | |
153 | if (ret < 0) | |
154 | goto free_urbp; | |
155 | ||
156 | spin_lock_irqsave(&udc->lock, flags); | |
157 | v_kick_timer(udc, jiffies); | |
158 | list_add_tail(&urb_p->urb_entry, &udc->urb_queue); | |
159 | spin_unlock_irqrestore(&udc->lock, flags); | |
160 | ||
161 | return 0; | |
162 | ||
163 | free_urbp: | |
164 | free_urbp_and_urb(urb_p); | |
165 | return ret; | |
166 | } | |
167 | ||
168 | static int v_rx_pdu(struct usbip_device *ud) | |
169 | { | |
170 | int ret; | |
171 | struct usbip_header pdu; | |
172 | struct vudc *udc = container_of(ud, struct vudc, ud); | |
173 | ||
174 | memset(&pdu, 0, sizeof(pdu)); | |
175 | ret = usbip_recv(ud->tcp_socket, &pdu, sizeof(pdu)); | |
176 | if (ret != sizeof(pdu)) { | |
177 | usbip_event_add(ud, VUDC_EVENT_ERROR_TCP); | |
178 | if (ret >= 0) | |
179 | return -EPIPE; | |
180 | return ret; | |
181 | } | |
182 | usbip_header_correct_endian(&pdu, 0); | |
183 | ||
184 | spin_lock_irq(&ud->lock); | |
185 | ret = (ud->status == SDEV_ST_USED); | |
186 | spin_unlock_irq(&ud->lock); | |
187 | if (!ret) { | |
188 | usbip_event_add(ud, VUDC_EVENT_ERROR_TCP); | |
189 | return -EBUSY; | |
190 | } | |
191 | ||
192 | switch (pdu.base.command) { | |
193 | case USBIP_CMD_UNLINK: | |
194 | ret = v_recv_cmd_unlink(udc, &pdu); | |
195 | break; | |
196 | case USBIP_CMD_SUBMIT: | |
197 | ret = v_recv_cmd_submit(udc, &pdu); | |
198 | break; | |
199 | default: | |
200 | ret = -EPIPE; | |
201 | pr_err("rx: unknown command"); | |
202 | break; | |
203 | } | |
204 | return ret; | |
205 | } | |
206 | ||
207 | int v_rx_loop(void *data) | |
208 | { | |
209 | struct usbip_device *ud = data; | |
210 | int ret = 0; | |
211 | ||
212 | while (!kthread_should_stop()) { | |
213 | if (usbip_event_happened(ud)) | |
214 | break; | |
215 | ret = v_rx_pdu(ud); | |
216 | if (ret < 0) { | |
217 | pr_warn("v_rx exit with error %d", ret); | |
218 | break; | |
219 | } | |
220 | } | |
221 | return ret; | |
222 | } |