]>
Commit | Line | Data |
---|---|---|
f587027e GKH |
1 | /* |
2 | * Greybus "AP" USB driver for "ES2" controller chips | |
3 | * | |
142f8ddf AE |
4 | * Copyright 2014-2015 Google Inc. |
5 | * Copyright 2014-2015 Linaro Ltd. | |
f587027e GKH |
6 | * |
7 | * Released under the GPLv2 only. | |
8 | */ | |
ca3ec299 | 9 | #include <linux/kthread.h> |
f587027e GKH |
10 | #include <linux/sizes.h> |
11 | #include <linux/usb.h> | |
ca3ec299 GKH |
12 | #include <linux/kfifo.h> |
13 | #include <linux/debugfs.h> | |
c14118a8 | 14 | #include <linux/list.h> |
491e60d6 | 15 | #include <asm/unaligned.h> |
f587027e | 16 | |
7d0e76d6 | 17 | #include "arpc.h" |
f587027e | 18 | #include "greybus.h" |
495787a7 | 19 | #include "greybus_trace.h" |
3f2a809e | 20 | #include "connection.h" |
05061507 | 21 | |
54f34e1e | 22 | |
60793c9b JH |
23 | /* Default timeout for USB vendor requests. */ |
24 | #define ES2_USB_CTRL_TIMEOUT 500 | |
25 | ||
54f34e1e JH |
26 | /* Default timeout for ARPC CPort requests */ |
27 | #define ES2_ARPC_CPORT_TIMEOUT 500 | |
28 | ||
05061507 JH |
29 | /* Fixed CPort numbers */ |
30 | #define ES2_CPORT_CDSI0 16 | |
31 | #define ES2_CPORT_CDSI1 17 | |
32 | ||
4b1d8204 AE |
33 | /* Memory sizes for the buffers sent to/from the ES2 controller */ |
34 | #define ES2_GBUF_MSG_SIZE_MAX 2048 | |
f587027e | 35 | |
9d9d3777 | 36 | /* Memory sizes for the ARPC buffers */ |
c14118a8 | 37 | #define ARPC_OUT_SIZE_MAX U16_MAX |
9d9d3777 AB |
38 | #define ARPC_IN_SIZE_MAX 128 |
39 | ||
f587027e | 40 | static const struct usb_device_id id_table[] = { |
8f0a654f | 41 | { USB_DEVICE(0x18d1, 0x1eaf) }, |
f587027e GKH |
42 | { }, |
43 | }; | |
44 | MODULE_DEVICE_TABLE(usb, id_table); | |
45 | ||
ca3ec299 | 46 | #define APB1_LOG_SIZE SZ_16K |
ca3ec299 | 47 | |
f587027e GKH |
48 | /* |
49 | * Number of CPort IN urbs in flight at any point in time. | |
50 | * Adjust if we are having stalls in the USB buffer due to not enough urbs in | |
51 | * flight. | |
52 | */ | |
53 | #define NUM_CPORT_IN_URB 4 | |
54 | ||
55 | /* Number of CPort OUT urbs in flight at any point in time. | |
56 | * Adjust if we get messages saying we are out of urbs in the system log. | |
57 | */ | |
fc994f0f | 58 | #define NUM_CPORT_OUT_URB 8 |
f587027e | 59 | |
9d9d3777 AB |
60 | /* |
61 | * Number of ARPC in urbs in flight at any point in time. | |
62 | */ | |
63 | #define NUM_ARPC_IN_URB 2 | |
64 | ||
ddc09acd AB |
65 | /* |
66 | * @endpoint: bulk in endpoint for CPort data | |
67 | * @urb: array of urbs for the CPort in messages | |
68 | * @buffer: array of buffers for the @cport_in_urb urbs | |
69 | */ | |
4b1d8204 | 70 | struct es2_cport_in { |
ddc09acd AB |
71 | __u8 endpoint; |
72 | struct urb *urb[NUM_CPORT_IN_URB]; | |
73 | u8 *buffer[NUM_CPORT_IN_URB]; | |
74 | }; | |
75 | ||
f587027e | 76 | /** |
4b1d8204 | 77 | * es2_ap_dev - ES2 USB Bridge to AP structure |
f587027e GKH |
78 | * @usb_dev: pointer to the USB device we are. |
79 | * @usb_intf: pointer to the USB interface we are bound to. | |
2537636a | 80 | * @hd: pointer to our gb_host_device structure |
ddc09acd | 81 | |
ddc09acd | 82 | * @cport_in: endpoint, urbs and buffer for cport in messages |
403074b5 | 83 | * @cport_out_endpoint: endpoint for for cport out messages |
f587027e GKH |
84 | * @cport_out_urb: array of urbs for the CPort out messages |
85 | * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or | |
86 | * not. | |
3e136cc9 JH |
87 | * @cport_out_urb_cancelled: array of flags indicating whether the |
88 | * corresponding @cport_out_urb is being cancelled | |
f587027e | 89 | * @cport_out_urb_lock: locks the @cport_out_urb_busy "list" |
1482b3e1 | 90 | * |
3be0e17d AE |
91 | * @apb_log_task: task pointer for logging thread |
92 | * @apb_log_dentry: file system entry for the log file interface | |
93 | * @apb_log_enable_dentry: file system entry for enabling logging | |
94 | * @apb_log_fifo: kernel FIFO to carry logged data | |
9d9d3777 AB |
95 | * @arpc_urb: array of urbs for the ARPC in messages |
96 | * @arpc_buffer: array of buffers for the @arpc_urb urbs | |
97 | * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC | |
c14118a8 AB |
98 | * @arpc_id_cycle: gives an unique id to ARPC |
99 | * @arpc_lock: locks ARPC list | |
100 | * @arpcs: list of in progress ARPCs | |
f587027e | 101 | */ |
4b1d8204 | 102 | struct es2_ap_dev { |
f587027e GKH |
103 | struct usb_device *usb_dev; |
104 | struct usb_interface *usb_intf; | |
2537636a | 105 | struct gb_host_device *hd; |
f587027e | 106 | |
7330c48e | 107 | struct es2_cport_in cport_in; |
403074b5 | 108 | __u8 cport_out_endpoint; |
f587027e GKH |
109 | struct urb *cport_out_urb[NUM_CPORT_OUT_URB]; |
110 | bool cport_out_urb_busy[NUM_CPORT_OUT_URB]; | |
3e136cc9 | 111 | bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB]; |
f587027e | 112 | spinlock_t cport_out_urb_lock; |
fc1a536e | 113 | |
045d3561 JH |
114 | bool cdsi1_in_use; |
115 | ||
3be0e17d AE |
116 | struct task_struct *apb_log_task; |
117 | struct dentry *apb_log_dentry; | |
118 | struct dentry *apb_log_enable_dentry; | |
119 | DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE); | |
9d9d3777 AB |
120 | |
121 | __u8 arpc_endpoint_in; | |
122 | struct urb *arpc_urb[NUM_ARPC_IN_URB]; | |
123 | u8 *arpc_buffer[NUM_ARPC_IN_URB]; | |
c14118a8 AB |
124 | |
125 | int arpc_id_cycle; | |
126 | spinlock_t arpc_lock; | |
127 | struct list_head arpcs; | |
fc1a536e AB |
128 | }; |
129 | ||
c14118a8 AB |
130 | struct arpc { |
131 | struct list_head list; | |
132 | struct arpc_request_message *req; | |
133 | struct arpc_response_message *resp; | |
134 | struct completion response_received; | |
135 | bool active; | |
136 | }; | |
137 | ||
2537636a | 138 | static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd) |
f587027e | 139 | { |
4b1d8204 | 140 | return (struct es2_ap_dev *)&hd->hd_priv; |
f587027e GKH |
141 | } |
142 | ||
143 | static void cport_out_callback(struct urb *urb); | |
4b1d8204 AE |
144 | static void usb_log_enable(struct es2_ap_dev *es2); |
145 | static void usb_log_disable(struct es2_ap_dev *es2); | |
c14118a8 AB |
146 | static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload, |
147 | size_t size, int *result, unsigned int timeout); | |
f587027e | 148 | |
ed4596e9 | 149 | static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd) |
8e2b7daa | 150 | { |
8e2b7daa | 151 | struct usb_device *udev = es2->usb_dev; |
ed4596e9 | 152 | u8 *data; |
8e2b7daa LP |
153 | int retval; |
154 | ||
9edae492 | 155 | data = kmemdup(req, size, GFP_KERNEL); |
ed4596e9 | 156 | if (!data) |
ccb58035 | 157 | return -ENOMEM; |
8e2b7daa LP |
158 | |
159 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
ed4596e9 | 160 | cmd, |
8e2b7daa | 161 | USB_DIR_OUT | USB_TYPE_VENDOR | |
ed4596e9 | 162 | USB_RECIP_INTERFACE, |
60793c9b | 163 | 0, 0, data, size, ES2_USB_CTRL_TIMEOUT); |
ccb58035 | 164 | if (retval < 0) |
ed4596e9 GKH |
165 | dev_err(&udev->dev, "%s: return error %d\n", __func__, retval); |
166 | else | |
167 | retval = 0; | |
8e2b7daa | 168 | |
ed4596e9 | 169 | kfree(data); |
ccb58035 | 170 | return retval; |
8e2b7daa | 171 | } |
ed4596e9 GKH |
172 | |
173 | static void ap_urb_complete(struct urb *urb) | |
174 | { | |
175 | struct usb_ctrlrequest *dr = urb->context; | |
176 | ||
177 | kfree(dr); | |
178 | usb_free_urb(urb); | |
179 | } | |
180 | ||
181 | static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd) | |
182 | { | |
183 | struct usb_device *udev = es2->usb_dev; | |
184 | struct urb *urb; | |
185 | struct usb_ctrlrequest *dr; | |
186 | u8 *buf; | |
187 | int retval; | |
188 | ||
189 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
190 | if (!urb) | |
191 | return -ENOMEM; | |
192 | ||
193 | dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC); | |
194 | if (!dr) { | |
195 | usb_free_urb(urb); | |
196 | return -ENOMEM; | |
197 | } | |
198 | ||
199 | buf = (u8 *)dr + sizeof(*dr); | |
200 | memcpy(buf, req, size); | |
201 | ||
202 | dr->bRequest = cmd; | |
203 | dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE; | |
204 | dr->wValue = 0; | |
205 | dr->wIndex = 0; | |
206 | dr->wLength = cpu_to_le16(size); | |
207 | ||
208 | usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0), | |
209 | (unsigned char *)dr, buf, size, | |
210 | ap_urb_complete, dr); | |
211 | retval = usb_submit_urb(urb, GFP_ATOMIC); | |
212 | if (retval) { | |
213 | usb_free_urb(urb); | |
214 | kfree(dr); | |
215 | } | |
216 | return retval; | |
217 | } | |
218 | ||
219 | static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd, | |
220 | bool async) | |
221 | { | |
222 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
223 | ||
224 | if (async) | |
225 | return output_async(es2, req, size, cmd); | |
226 | ||
227 | return output_sync(es2, req, size, cmd); | |
228 | } | |
8e2b7daa | 229 | |
0ce68ce4 JH |
230 | static int es2_cport_in_enable(struct es2_ap_dev *es2, |
231 | struct es2_cport_in *cport_in) | |
232 | { | |
233 | struct urb *urb; | |
234 | int ret; | |
235 | int i; | |
236 | ||
237 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { | |
238 | urb = cport_in->urb[i]; | |
239 | ||
240 | ret = usb_submit_urb(urb, GFP_KERNEL); | |
241 | if (ret) { | |
242 | dev_err(&es2->usb_dev->dev, | |
243 | "failed to submit in-urb: %d\n", ret); | |
244 | goto err_kill_urbs; | |
245 | } | |
246 | } | |
247 | ||
248 | return 0; | |
249 | ||
250 | err_kill_urbs: | |
251 | for (--i; i >= 0; --i) { | |
252 | urb = cport_in->urb[i]; | |
253 | usb_kill_urb(urb); | |
254 | } | |
255 | ||
256 | return ret; | |
257 | } | |
258 | ||
f6624ca7 JH |
259 | static void es2_cport_in_disable(struct es2_ap_dev *es2, |
260 | struct es2_cport_in *cport_in) | |
261 | { | |
262 | struct urb *urb; | |
263 | int i; | |
264 | ||
265 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { | |
266 | urb = cport_in->urb[i]; | |
267 | usb_kill_urb(urb); | |
268 | } | |
269 | } | |
270 | ||
9d9d3777 AB |
271 | static int es2_arpc_in_enable(struct es2_ap_dev *es2) |
272 | { | |
273 | struct urb *urb; | |
274 | int ret; | |
275 | int i; | |
276 | ||
277 | for (i = 0; i < NUM_ARPC_IN_URB; ++i) { | |
278 | urb = es2->arpc_urb[i]; | |
279 | ||
280 | ret = usb_submit_urb(urb, GFP_KERNEL); | |
281 | if (ret) { | |
282 | dev_err(&es2->usb_dev->dev, | |
283 | "failed to submit arpc in-urb: %d\n", ret); | |
284 | goto err_kill_urbs; | |
285 | } | |
286 | } | |
287 | ||
288 | return 0; | |
289 | ||
290 | err_kill_urbs: | |
291 | for (--i; i >= 0; --i) { | |
292 | urb = es2->arpc_urb[i]; | |
293 | usb_kill_urb(urb); | |
294 | } | |
295 | ||
296 | return ret; | |
297 | } | |
298 | ||
299 | static void es2_arpc_in_disable(struct es2_ap_dev *es2) | |
300 | { | |
301 | struct urb *urb; | |
302 | int i; | |
303 | ||
304 | for (i = 0; i < NUM_ARPC_IN_URB; ++i) { | |
305 | urb = es2->arpc_urb[i]; | |
306 | usb_kill_urb(urb); | |
307 | } | |
308 | } | |
309 | ||
4b1d8204 | 310 | static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask) |
f587027e GKH |
311 | { |
312 | struct urb *urb = NULL; | |
313 | unsigned long flags; | |
314 | int i; | |
315 | ||
4b1d8204 | 316 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
f587027e GKH |
317 | |
318 | /* Look in our pool of allocated urbs first, as that's the "fastest" */ | |
319 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { | |
4b1d8204 AE |
320 | if (es2->cport_out_urb_busy[i] == false && |
321 | es2->cport_out_urb_cancelled[i] == false) { | |
322 | es2->cport_out_urb_busy[i] = true; | |
323 | urb = es2->cport_out_urb[i]; | |
f587027e GKH |
324 | break; |
325 | } | |
326 | } | |
4b1d8204 | 327 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
f587027e GKH |
328 | if (urb) |
329 | return urb; | |
330 | ||
331 | /* | |
332 | * Crap, pool is empty, complain to the syslog and go allocate one | |
333 | * dynamically as we have to succeed. | |
334 | */ | |
ed972e3a | 335 | dev_dbg(&es2->usb_dev->dev, |
f587027e GKH |
336 | "No free CPort OUT urbs, having to dynamically allocate one!\n"); |
337 | return usb_alloc_urb(0, gfp_mask); | |
338 | } | |
339 | ||
4b1d8204 | 340 | static void free_urb(struct es2_ap_dev *es2, struct urb *urb) |
f587027e GKH |
341 | { |
342 | unsigned long flags; | |
343 | int i; | |
344 | /* | |
345 | * See if this was an urb in our pool, if so mark it "free", otherwise | |
346 | * we need to free it ourselves. | |
347 | */ | |
4b1d8204 | 348 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
f587027e | 349 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
4b1d8204 AE |
350 | if (urb == es2->cport_out_urb[i]) { |
351 | es2->cport_out_urb_busy[i] = false; | |
f587027e GKH |
352 | urb = NULL; |
353 | break; | |
354 | } | |
355 | } | |
4b1d8204 | 356 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
f587027e GKH |
357 | |
358 | /* If urb is not NULL, then we need to free this urb */ | |
359 | usb_free_urb(urb); | |
360 | } | |
361 | ||
d29b3d63 AE |
362 | /* |
363 | * We (ab)use the operation-message header pad bytes to transfer the | |
364 | * cport id in order to minimise overhead. | |
365 | */ | |
366 | static void | |
367 | gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id) | |
368 | { | |
4bc1389d | 369 | header->pad[0] = cport_id; |
d29b3d63 AE |
370 | } |
371 | ||
372 | /* Clear the pad bytes used for the CPort id */ | |
373 | static void gb_message_cport_clear(struct gb_operation_msg_hdr *header) | |
374 | { | |
4bc1389d | 375 | header->pad[0] = 0; |
d29b3d63 AE |
376 | } |
377 | ||
378 | /* Extract the CPort id packed into the header, and clear it */ | |
379 | static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header) | |
380 | { | |
4bc1389d | 381 | u16 cport_id = header->pad[0]; |
d29b3d63 AE |
382 | |
383 | gb_message_cport_clear(header); | |
384 | ||
385 | return cport_id; | |
386 | } | |
387 | ||
f587027e | 388 | /* |
3e136cc9 JH |
389 | * Returns zero if the message was successfully queued, or a negative errno |
390 | * otherwise. | |
f587027e | 391 | */ |
2537636a | 392 | static int message_send(struct gb_host_device *hd, u16 cport_id, |
7cf7bca9 | 393 | struct gb_message *message, gfp_t gfp_mask) |
f587027e | 394 | { |
4b1d8204 AE |
395 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
396 | struct usb_device *udev = es2->usb_dev; | |
7cf7bca9 | 397 | size_t buffer_size; |
f587027e GKH |
398 | int retval; |
399 | struct urb *urb; | |
3e136cc9 | 400 | unsigned long flags; |
f587027e | 401 | |
f587027e GKH |
402 | /* |
403 | * The data actually transferred will include an indication | |
404 | * of where the data should be sent. Do one last check of | |
405 | * the target CPort id before filling it in. | |
406 | */ | |
144670c2 | 407 | if (!cport_id_valid(hd, cport_id)) { |
100e9000 | 408 | dev_err(&udev->dev, "invalid cport %u\n", cport_id); |
3e136cc9 | 409 | return -EINVAL; |
f587027e | 410 | } |
f587027e GKH |
411 | |
412 | /* Find a free urb */ | |
4b1d8204 | 413 | urb = next_free_urb(es2, gfp_mask); |
f587027e | 414 | if (!urb) |
3e136cc9 JH |
415 | return -ENOMEM; |
416 | ||
4b1d8204 | 417 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
3e136cc9 | 418 | message->hcpriv = urb; |
4b1d8204 | 419 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
f587027e | 420 | |
d29b3d63 AE |
421 | /* Pack the cport id into the message header */ |
422 | gb_message_cport_pack(message->header, cport_id); | |
491e60d6 | 423 | |
821c620a | 424 | buffer_size = sizeof(*message->header) + message->payload_size; |
491e60d6 | 425 | |
f587027e | 426 | usb_fill_bulk_urb(urb, udev, |
606addd2 | 427 | usb_sndbulkpipe(udev, |
403074b5 | 428 | es2->cport_out_endpoint), |
821c620a | 429 | message->buffer, buffer_size, |
7cf7bca9 | 430 | cport_out_callback, message); |
977e209a | 431 | urb->transfer_flags |= URB_ZERO_PACKET; |
495787a7 AE |
432 | |
433 | trace_gb_message_submit(message); | |
434 | ||
f587027e GKH |
435 | retval = usb_submit_urb(urb, gfp_mask); |
436 | if (retval) { | |
05e30955 | 437 | dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval); |
3e136cc9 | 438 | |
4b1d8204 | 439 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
3e136cc9 | 440 | message->hcpriv = NULL; |
4b1d8204 | 441 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
3e136cc9 | 442 | |
4b1d8204 | 443 | free_urb(es2, urb); |
d29b3d63 | 444 | gb_message_cport_clear(message->header); |
3e136cc9 JH |
445 | |
446 | return retval; | |
f587027e GKH |
447 | } |
448 | ||
3e136cc9 | 449 | return 0; |
f587027e GKH |
450 | } |
451 | ||
452 | /* | |
3e136cc9 | 453 | * Can not be called in atomic context. |
f587027e | 454 | */ |
3e136cc9 | 455 | static void message_cancel(struct gb_message *message) |
f587027e | 456 | { |
2537636a | 457 | struct gb_host_device *hd = message->operation->connection->hd; |
4b1d8204 | 458 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
3e136cc9 JH |
459 | struct urb *urb; |
460 | int i; | |
f587027e | 461 | |
3e136cc9 JH |
462 | might_sleep(); |
463 | ||
07af9340 | 464 | spin_lock_irq(&es2->cport_out_urb_lock); |
3e136cc9 JH |
465 | urb = message->hcpriv; |
466 | ||
467 | /* Prevent dynamically allocated urb from being deallocated. */ | |
468 | usb_get_urb(urb); | |
469 | ||
470 | /* Prevent pre-allocated urb from being reused. */ | |
471 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { | |
4b1d8204 AE |
472 | if (urb == es2->cport_out_urb[i]) { |
473 | es2->cport_out_urb_cancelled[i] = true; | |
3e136cc9 JH |
474 | break; |
475 | } | |
476 | } | |
07af9340 | 477 | spin_unlock_irq(&es2->cport_out_urb_lock); |
3e136cc9 JH |
478 | |
479 | usb_kill_urb(urb); | |
480 | ||
481 | if (i < NUM_CPORT_OUT_URB) { | |
07af9340 | 482 | spin_lock_irq(&es2->cport_out_urb_lock); |
4b1d8204 | 483 | es2->cport_out_urb_cancelled[i] = false; |
07af9340 | 484 | spin_unlock_irq(&es2->cport_out_urb_lock); |
3e136cc9 JH |
485 | } |
486 | ||
487 | usb_free_urb(urb); | |
f587027e GKH |
488 | } |
489 | ||
045d3561 JH |
490 | static int es2_cport_allocate(struct gb_host_device *hd, int cport_id, |
491 | unsigned long flags) | |
492 | { | |
493 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
494 | struct ida *id_map = &hd->cport_id_map; | |
495 | int ida_start, ida_end; | |
496 | ||
497 | switch (cport_id) { | |
498 | case ES2_CPORT_CDSI0: | |
499 | case ES2_CPORT_CDSI1: | |
500 | dev_err(&hd->dev, "cport %d not available\n", cport_id); | |
501 | return -EBUSY; | |
502 | } | |
503 | ||
504 | if (flags & GB_CONNECTION_FLAG_OFFLOADED && | |
505 | flags & GB_CONNECTION_FLAG_CDSI1) { | |
506 | if (es2->cdsi1_in_use) { | |
507 | dev_err(&hd->dev, "CDSI1 already in use\n"); | |
508 | return -EBUSY; | |
509 | } | |
510 | ||
511 | es2->cdsi1_in_use = true; | |
512 | ||
513 | return ES2_CPORT_CDSI1; | |
514 | } | |
515 | ||
516 | if (cport_id < 0) { | |
517 | ida_start = 0; | |
518 | ida_end = hd->num_cports; | |
519 | } else if (cport_id < hd->num_cports) { | |
520 | ida_start = cport_id; | |
521 | ida_end = cport_id + 1; | |
522 | } else { | |
523 | dev_err(&hd->dev, "cport %d not available\n", cport_id); | |
524 | return -EINVAL; | |
525 | } | |
526 | ||
527 | return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL); | |
528 | } | |
529 | ||
530 | static void es2_cport_release(struct gb_host_device *hd, u16 cport_id) | |
531 | { | |
532 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
533 | ||
534 | switch (cport_id) { | |
535 | case ES2_CPORT_CDSI1: | |
536 | es2->cdsi1_in_use = false; | |
537 | return; | |
538 | } | |
539 | ||
540 | ida_simple_remove(&hd->cport_id_map, cport_id); | |
541 | } | |
542 | ||
74ec7598 JH |
543 | static int cport_enable(struct gb_host_device *hd, u16 cport_id, |
544 | unsigned long flags) | |
545 | { | |
546 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
547 | struct usb_device *udev = es2->usb_dev; | |
548 | struct gb_apb_request_cport_flags *req; | |
6a1d2959 | 549 | u32 connection_flags; |
74ec7598 JH |
550 | int ret; |
551 | ||
552 | req = kzalloc(sizeof(*req), GFP_KERNEL); | |
553 | if (!req) | |
554 | return -ENOMEM; | |
555 | ||
6a1d2959 | 556 | connection_flags = 0; |
74ec7598 | 557 | if (flags & GB_CONNECTION_FLAG_CONTROL) |
6a1d2959 | 558 | connection_flags |= GB_APB_CPORT_FLAG_CONTROL; |
74ec7598 | 559 | if (flags & GB_CONNECTION_FLAG_HIGH_PRIO) |
6a1d2959 BD |
560 | connection_flags |= GB_APB_CPORT_FLAG_HIGH_PRIO; |
561 | ||
562 | req->flags = cpu_to_le32(connection_flags); | |
74ec7598 JH |
563 | |
564 | dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__, | |
6a1d2959 | 565 | cport_id, connection_flags); |
74ec7598 JH |
566 | |
567 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
568 | GB_APB_REQUEST_CPORT_FLAGS, | |
569 | USB_DIR_OUT | USB_TYPE_VENDOR | | |
570 | USB_RECIP_INTERFACE, cport_id, 0, | |
60793c9b | 571 | req, sizeof(*req), ES2_USB_CTRL_TIMEOUT); |
74ec7598 JH |
572 | if (ret != sizeof(*req)) { |
573 | dev_err(&udev->dev, "failed to set cport flags for port %d\n", | |
574 | cport_id); | |
575 | if (ret >= 0) | |
576 | ret = -EIO; | |
577 | ||
578 | goto out; | |
579 | } | |
580 | ||
581 | ret = 0; | |
582 | out: | |
583 | kfree(req); | |
584 | ||
585 | return ret; | |
586 | } | |
587 | ||
a9dc1cf5 JH |
588 | static int es2_cport_connected(struct gb_host_device *hd, u16 cport_id) |
589 | { | |
590 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
591 | struct device *dev = &es2->usb_dev->dev; | |
592 | struct arpc_cport_connected_req req; | |
593 | int ret; | |
594 | ||
595 | req.cport_id = cpu_to_le16(cport_id); | |
596 | ret = arpc_sync(es2, ARPC_TYPE_CPORT_CONNECTED, &req, sizeof(req), | |
597 | NULL, ES2_ARPC_CPORT_TIMEOUT); | |
598 | if (ret) { | |
599 | dev_err(dev, "failed to set connected state for cport %u: %d\n", | |
600 | cport_id, ret); | |
601 | return ret; | |
602 | } | |
603 | ||
604 | return 0; | |
605 | } | |
606 | ||
d2dee94b JH |
607 | static int es2_cport_flush(struct gb_host_device *hd, u16 cport_id) |
608 | { | |
609 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
610 | struct device *dev = &es2->usb_dev->dev; | |
611 | struct arpc_cport_flush_req req; | |
612 | int ret; | |
613 | ||
614 | req.cport_id = cpu_to_le16(cport_id); | |
615 | ret = arpc_sync(es2, ARPC_TYPE_CPORT_FLUSH, &req, sizeof(req), | |
616 | NULL, ES2_ARPC_CPORT_TIMEOUT); | |
617 | if (ret) { | |
618 | dev_err(dev, "failed to flush cport %u: %d\n", cport_id, ret); | |
619 | return ret; | |
620 | } | |
621 | ||
622 | return 0; | |
623 | } | |
624 | ||
77e52b3b JH |
625 | static int es2_cport_shutdown(struct gb_host_device *hd, u16 cport_id, |
626 | u8 phase, unsigned int timeout) | |
627 | { | |
628 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
629 | struct device *dev = &es2->usb_dev->dev; | |
630 | struct arpc_cport_shutdown_req req; | |
631 | int result; | |
632 | int ret; | |
633 | ||
634 | if (timeout > U16_MAX) | |
635 | return -EINVAL; | |
636 | ||
637 | req.cport_id = cpu_to_le16(cport_id); | |
638 | req.timeout = cpu_to_le16(timeout); | |
639 | req.phase = phase; | |
640 | ret = arpc_sync(es2, ARPC_TYPE_CPORT_SHUTDOWN, &req, sizeof(req), | |
641 | &result, ES2_ARPC_CPORT_TIMEOUT + timeout); | |
642 | if (ret) { | |
643 | dev_err(dev, "failed to send shutdown over cport %u: %d (%d)\n", | |
644 | cport_id, ret, result); | |
645 | return ret; | |
646 | } | |
647 | ||
648 | return 0; | |
649 | } | |
650 | ||
0e7cd0d2 JH |
651 | static int es2_cport_quiesce(struct gb_host_device *hd, u16 cport_id, |
652 | size_t peer_space, unsigned int timeout) | |
653 | { | |
654 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
655 | struct device *dev = &es2->usb_dev->dev; | |
656 | struct arpc_cport_quiesce_req req; | |
657 | int result; | |
658 | int ret; | |
659 | ||
660 | if (peer_space > U16_MAX) | |
661 | return -EINVAL; | |
662 | ||
663 | if (timeout > U16_MAX) | |
664 | return -EINVAL; | |
665 | ||
666 | req.cport_id = cpu_to_le16(cport_id); | |
667 | req.peer_space = cpu_to_le16(peer_space); | |
668 | req.timeout = cpu_to_le16(timeout); | |
669 | ret = arpc_sync(es2, ARPC_TYPE_CPORT_QUIESCE, &req, sizeof(req), | |
670 | &result, ES2_ARPC_CPORT_TIMEOUT + timeout); | |
671 | if (ret) { | |
672 | dev_err(dev, "failed to quiesce cport %u: %d (%d)\n", | |
673 | cport_id, ret, result); | |
674 | return ret; | |
675 | } | |
676 | ||
677 | return 0; | |
678 | } | |
679 | ||
6471c003 JH |
680 | static int es2_cport_clear(struct gb_host_device *hd, u16 cport_id) |
681 | { | |
682 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
683 | struct device *dev = &es2->usb_dev->dev; | |
684 | struct arpc_cport_clear_req req; | |
685 | int ret; | |
686 | ||
687 | req.cport_id = cpu_to_le16(cport_id); | |
688 | ret = arpc_sync(es2, ARPC_TYPE_CPORT_CLEAR, &req, sizeof(req), | |
689 | NULL, ES2_ARPC_CPORT_TIMEOUT); | |
690 | if (ret) { | |
691 | dev_err(dev, "failed to clear cport %u: %d\n", cport_id, ret); | |
692 | return ret; | |
693 | } | |
694 | ||
695 | return 0; | |
696 | } | |
697 | ||
2537636a | 698 | static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id) |
608ab2fe BD |
699 | { |
700 | int retval; | |
4b1d8204 AE |
701 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
702 | struct usb_device *udev = es2->usb_dev; | |
608ab2fe | 703 | |
608ab2fe | 704 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
e5273381 | 705 | GB_APB_REQUEST_LATENCY_TAG_EN, |
608ab2fe BD |
706 | USB_DIR_OUT | USB_TYPE_VENDOR | |
707 | USB_RECIP_INTERFACE, cport_id, 0, NULL, | |
60793c9b | 708 | 0, ES2_USB_CTRL_TIMEOUT); |
608ab2fe BD |
709 | |
710 | if (retval < 0) | |
711 | dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n", | |
712 | cport_id); | |
713 | return retval; | |
714 | } | |
715 | ||
2537636a | 716 | static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id) |
608ab2fe BD |
717 | { |
718 | int retval; | |
4b1d8204 AE |
719 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
720 | struct usb_device *udev = es2->usb_dev; | |
608ab2fe | 721 | |
608ab2fe | 722 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
e5273381 | 723 | GB_APB_REQUEST_LATENCY_TAG_DIS, |
608ab2fe BD |
724 | USB_DIR_OUT | USB_TYPE_VENDOR | |
725 | USB_RECIP_INTERFACE, cport_id, 0, NULL, | |
60793c9b | 726 | 0, ES2_USB_CTRL_TIMEOUT); |
608ab2fe BD |
727 | |
728 | if (retval < 0) | |
729 | dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n", | |
730 | cport_id); | |
731 | return retval; | |
732 | } | |
733 | ||
a8cc020f | 734 | static struct gb_hd_driver es2_driver = { |
c8a657ba BD |
735 | .hd_priv_size = sizeof(struct es2_ap_dev), |
736 | .message_send = message_send, | |
737 | .message_cancel = message_cancel, | |
738 | .cport_allocate = es2_cport_allocate, | |
739 | .cport_release = es2_cport_release, | |
74ec7598 | 740 | .cport_enable = cport_enable, |
a9dc1cf5 | 741 | .cport_connected = es2_cport_connected, |
d2dee94b | 742 | .cport_flush = es2_cport_flush, |
77e52b3b | 743 | .cport_shutdown = es2_cport_shutdown, |
0e7cd0d2 | 744 | .cport_quiesce = es2_cport_quiesce, |
6471c003 | 745 | .cport_clear = es2_cport_clear, |
c8a657ba BD |
746 | .latency_tag_enable = latency_tag_enable, |
747 | .latency_tag_disable = latency_tag_disable, | |
748 | .output = output, | |
f587027e GKH |
749 | }; |
750 | ||
751 | /* Common function to report consistent warnings based on URB status */ | |
752 | static int check_urb_status(struct urb *urb) | |
753 | { | |
754 | struct device *dev = &urb->dev->dev; | |
755 | int status = urb->status; | |
756 | ||
757 | switch (status) { | |
758 | case 0: | |
759 | return 0; | |
760 | ||
761 | case -EOVERFLOW: | |
762 | dev_err(dev, "%s: overflow actual length is %d\n", | |
763 | __func__, urb->actual_length); | |
764 | case -ECONNRESET: | |
765 | case -ENOENT: | |
766 | case -ESHUTDOWN: | |
767 | case -EILSEQ: | |
768 | case -EPROTO: | |
769 | /* device is gone, stop sending */ | |
770 | return status; | |
771 | } | |
772 | dev_err(dev, "%s: unknown status %d\n", __func__, status); | |
773 | ||
774 | return -EAGAIN; | |
775 | } | |
776 | ||
57bc17ff | 777 | static void es2_destroy(struct es2_ap_dev *es2) |
f587027e | 778 | { |
f587027e | 779 | struct usb_device *udev; |
1521eb6b | 780 | struct urb *urb; |
f587027e GKH |
781 | int i; |
782 | ||
74cd6503 | 783 | debugfs_remove(es2->apb_log_enable_dentry); |
4b1d8204 | 784 | usb_log_disable(es2); |
ca3ec299 | 785 | |
f587027e GKH |
786 | /* Tear down everything! */ |
787 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { | |
1521eb6b | 788 | urb = es2->cport_out_urb[i]; |
f587027e GKH |
789 | usb_kill_urb(urb); |
790 | usb_free_urb(urb); | |
4b1d8204 AE |
791 | es2->cport_out_urb[i] = NULL; |
792 | es2->cport_out_urb_busy[i] = false; /* just to be anal */ | |
f587027e GKH |
793 | } |
794 | ||
9d9d3777 | 795 | for (i = 0; i < NUM_ARPC_IN_URB; ++i) { |
1521eb6b | 796 | usb_free_urb(es2->arpc_urb[i]); |
9d9d3777 AB |
797 | kfree(es2->arpc_buffer[i]); |
798 | es2->arpc_buffer[i] = NULL; | |
799 | } | |
800 | ||
7330c48e GKH |
801 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { |
802 | usb_free_urb(es2->cport_in.urb[i]); | |
803 | kfree(es2->cport_in.buffer[i]); | |
804 | es2->cport_in.buffer[i] = NULL; | |
f587027e GKH |
805 | } |
806 | ||
52033fde VA |
807 | /* release reserved CDSI0 and CDSI1 cports */ |
808 | gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1); | |
809 | gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0); | |
810 | ||
4b1d8204 | 811 | udev = es2->usb_dev; |
c1700479 | 812 | gb_hd_put(es2->hd); |
f587027e GKH |
813 | |
814 | usb_put_dev(udev); | |
815 | } | |
816 | ||
f587027e GKH |
817 | static void cport_in_callback(struct urb *urb) |
818 | { | |
2537636a | 819 | struct gb_host_device *hd = urb->context; |
f587027e | 820 | struct device *dev = &urb->dev->dev; |
491e60d6 | 821 | struct gb_operation_msg_hdr *header; |
f587027e GKH |
822 | int status = check_urb_status(urb); |
823 | int retval; | |
824 | u16 cport_id; | |
f587027e GKH |
825 | |
826 | if (status) { | |
827 | if ((status == -EAGAIN) || (status == -EPROTO)) | |
828 | goto exit; | |
62de6e0a VK |
829 | |
830 | /* The urb is being unlinked */ | |
831 | if (status == -ENOENT || status == -ESHUTDOWN) | |
832 | return; | |
833 | ||
f587027e GKH |
834 | dev_err(dev, "urb cport in error %d (dropped)\n", status); |
835 | return; | |
836 | } | |
837 | ||
491e60d6 | 838 | if (urb->actual_length < sizeof(*header)) { |
305a031e | 839 | dev_err(dev, "short message received\n"); |
f587027e GKH |
840 | goto exit; |
841 | } | |
842 | ||
d29b3d63 | 843 | /* Extract the CPort id, which is packed in the message header */ |
491e60d6 | 844 | header = urb->transfer_buffer; |
d29b3d63 | 845 | cport_id = gb_message_cport_unpack(header); |
f587027e | 846 | |
6872c461 | 847 | if (cport_id_valid(hd, cport_id)) { |
821c620a | 848 | greybus_data_rcvd(hd, cport_id, urb->transfer_buffer, |
491e60d6 | 849 | urb->actual_length); |
6872c461 | 850 | } else { |
100e9000 | 851 | dev_err(dev, "invalid cport id %u received\n", cport_id); |
6872c461 | 852 | } |
f587027e GKH |
853 | exit: |
854 | /* put our urb back in the request pool */ | |
855 | retval = usb_submit_urb(urb, GFP_ATOMIC); | |
856 | if (retval) | |
305a031e | 857 | dev_err(dev, "failed to resubmit in-urb: %d\n", retval); |
f587027e GKH |
858 | } |
859 | ||
860 | static void cport_out_callback(struct urb *urb) | |
861 | { | |
7cf7bca9 | 862 | struct gb_message *message = urb->context; |
2537636a | 863 | struct gb_host_device *hd = message->operation->connection->hd; |
4b1d8204 | 864 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
f587027e | 865 | int status = check_urb_status(urb); |
3e136cc9 | 866 | unsigned long flags; |
f587027e | 867 | |
d29b3d63 | 868 | gb_message_cport_clear(message->header); |
491e60d6 | 869 | |
4b1d8204 | 870 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
58c85123 | 871 | message->hcpriv = NULL; |
4b1d8204 | 872 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
58c85123 | 873 | |
f587027e | 874 | /* |
7cf7bca9 JH |
875 | * Tell the submitter that the message send (attempt) is |
876 | * complete, and report the status. | |
f587027e | 877 | */ |
7cf7bca9 JH |
878 | greybus_message_sent(hd, message, status); |
879 | ||
4b1d8204 | 880 | free_urb(es2, urb); |
f587027e GKH |
881 | } |
882 | ||
c14118a8 AB |
883 | static struct arpc *arpc_alloc(void *payload, u16 size, u8 type) |
884 | { | |
885 | struct arpc *rpc; | |
886 | ||
887 | if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX) | |
888 | return NULL; | |
889 | ||
890 | rpc = kzalloc(sizeof(*rpc), GFP_KERNEL); | |
891 | if (!rpc) | |
892 | return NULL; | |
893 | ||
894 | INIT_LIST_HEAD(&rpc->list); | |
895 | rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL); | |
896 | if (!rpc->req) | |
897 | goto err_free_rpc; | |
898 | ||
899 | rpc->resp = kzalloc(sizeof(*rpc->resp), GFP_KERNEL); | |
fcba5d04 | 900 | if (!rpc->resp) |
c14118a8 AB |
901 | goto err_free_req; |
902 | ||
903 | rpc->req->type = type; | |
7fec2bc9 | 904 | rpc->req->size = cpu_to_le16(sizeof(*rpc->req) + size); |
c14118a8 AB |
905 | memcpy(rpc->req->data, payload, size); |
906 | ||
907 | init_completion(&rpc->response_received); | |
908 | ||
909 | return rpc; | |
910 | ||
911 | err_free_req: | |
912 | kfree(rpc->req); | |
913 | err_free_rpc: | |
914 | kfree(rpc); | |
915 | ||
916 | return NULL; | |
917 | } | |
918 | ||
919 | static void arpc_free(struct arpc *rpc) | |
920 | { | |
921 | kfree(rpc->req); | |
922 | kfree(rpc->resp); | |
923 | kfree(rpc); | |
924 | } | |
925 | ||
165a74ab | 926 | static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id) |
c14118a8 AB |
927 | { |
928 | struct arpc *rpc; | |
929 | ||
930 | list_for_each_entry(rpc, &es2->arpcs, list) { | |
165a74ab | 931 | if (rpc->req->id == id) |
c14118a8 AB |
932 | return rpc; |
933 | } | |
934 | ||
935 | return NULL; | |
936 | } | |
937 | ||
938 | static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc) | |
939 | { | |
940 | rpc->active = true; | |
e818027c | 941 | rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++); |
c0219cbf | 942 | list_add_tail(&rpc->list, &es2->arpcs); |
c14118a8 AB |
943 | } |
944 | ||
945 | static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc) | |
946 | { | |
947 | if (rpc->active) { | |
948 | rpc->active = false; | |
949 | list_del(&rpc->list); | |
950 | } | |
951 | } | |
952 | ||
953 | static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout) | |
954 | { | |
955 | struct usb_device *udev = es2->usb_dev; | |
956 | int retval; | |
957 | ||
958 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
83d474f7 | 959 | GB_APB_REQUEST_ARPC_RUN, |
c14118a8 AB |
960 | USB_DIR_OUT | USB_TYPE_VENDOR | |
961 | USB_RECIP_INTERFACE, | |
962 | 0, 0, | |
e818027c | 963 | rpc->req, le16_to_cpu(rpc->req->size), |
60793c9b | 964 | ES2_USB_CTRL_TIMEOUT); |
e818027c | 965 | if (retval != le16_to_cpu(rpc->req->size)) { |
c14118a8 AB |
966 | dev_err(&udev->dev, |
967 | "failed to send ARPC request %d: %d\n", | |
968 | rpc->req->type, retval); | |
969 | if (retval > 0) | |
970 | retval = -EIO; | |
971 | return retval; | |
972 | } | |
973 | ||
974 | return 0; | |
975 | } | |
976 | ||
977 | static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload, | |
978 | size_t size, int *result, unsigned int timeout) | |
979 | { | |
980 | struct arpc *rpc; | |
981 | unsigned long flags; | |
982 | int retval; | |
983 | ||
121bae2b JH |
984 | if (result) |
985 | *result = 0; | |
4ae2d962 | 986 | |
c14118a8 AB |
987 | rpc = arpc_alloc(payload, size, type); |
988 | if (!rpc) | |
989 | return -ENOMEM; | |
990 | ||
991 | spin_lock_irqsave(&es2->arpc_lock, flags); | |
992 | arpc_add(es2, rpc); | |
993 | spin_unlock_irqrestore(&es2->arpc_lock, flags); | |
994 | ||
995 | retval = arpc_send(es2, rpc, timeout); | |
996 | if (retval) | |
997 | goto out_arpc_del; | |
998 | ||
999 | retval = wait_for_completion_interruptible_timeout( | |
1000 | &rpc->response_received, | |
1001 | msecs_to_jiffies(timeout)); | |
1002 | if (retval <= 0) { | |
1003 | if (!retval) | |
1004 | retval = -ETIMEDOUT; | |
1005 | goto out_arpc_del; | |
1006 | } | |
1007 | ||
4ae2d962 | 1008 | if (rpc->resp->result) { |
c14118a8 | 1009 | retval = -EREMOTEIO; |
121bae2b JH |
1010 | if (result) |
1011 | *result = rpc->resp->result; | |
4ae2d962 | 1012 | } else { |
34873949 | 1013 | retval = 0; |
4ae2d962 | 1014 | } |
c14118a8 AB |
1015 | |
1016 | out_arpc_del: | |
1017 | spin_lock_irqsave(&es2->arpc_lock, flags); | |
1018 | arpc_del(es2, rpc); | |
1019 | spin_unlock_irqrestore(&es2->arpc_lock, flags); | |
1020 | arpc_free(rpc); | |
1021 | ||
1022 | if (retval < 0 && retval != -EREMOTEIO) { | |
1023 | dev_err(&es2->usb_dev->dev, | |
1024 | "failed to execute ARPC: %d\n", retval); | |
1025 | } | |
1026 | ||
1027 | return retval; | |
1028 | } | |
1029 | ||
9d9d3777 AB |
1030 | static void arpc_in_callback(struct urb *urb) |
1031 | { | |
c14118a8 | 1032 | struct es2_ap_dev *es2 = urb->context; |
9d9d3777 AB |
1033 | struct device *dev = &urb->dev->dev; |
1034 | int status = check_urb_status(urb); | |
c14118a8 AB |
1035 | struct arpc *rpc; |
1036 | struct arpc_response_message *resp; | |
1037 | unsigned long flags; | |
9d9d3777 AB |
1038 | int retval; |
1039 | ||
1040 | if (status) { | |
1041 | if ((status == -EAGAIN) || (status == -EPROTO)) | |
1042 | goto exit; | |
1043 | ||
1044 | /* The urb is being unlinked */ | |
1045 | if (status == -ENOENT || status == -ESHUTDOWN) | |
1046 | return; | |
1047 | ||
1048 | dev_err(dev, "arpc in-urb error %d (dropped)\n", status); | |
1049 | return; | |
1050 | } | |
1051 | ||
c14118a8 AB |
1052 | if (urb->actual_length < sizeof(*resp)) { |
1053 | dev_err(dev, "short aprc response received\n"); | |
1054 | goto exit; | |
1055 | } | |
1056 | ||
1057 | resp = urb->transfer_buffer; | |
1058 | spin_lock_irqsave(&es2->arpc_lock, flags); | |
165a74ab | 1059 | rpc = arpc_find(es2, resp->id); |
c14118a8 | 1060 | if (!rpc) { |
178457e1 JH |
1061 | dev_err(dev, "invalid arpc response id received: %u\n", |
1062 | le16_to_cpu(resp->id)); | |
c14118a8 AB |
1063 | spin_unlock_irqrestore(&es2->arpc_lock, flags); |
1064 | goto exit; | |
1065 | } | |
1066 | ||
1067 | arpc_del(es2, rpc); | |
1068 | memcpy(rpc->resp, resp, sizeof(*resp)); | |
1069 | complete(&rpc->response_received); | |
1070 | spin_unlock_irqrestore(&es2->arpc_lock, flags); | |
1071 | ||
9d9d3777 AB |
1072 | exit: |
1073 | /* put our urb back in the request pool */ | |
1074 | retval = usb_submit_urb(urb, GFP_ATOMIC); | |
1075 | if (retval) | |
1076 | dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval); | |
1077 | } | |
1078 | ||
c15ccabe | 1079 | #define APB1_LOG_MSG_SIZE 64 |
3be0e17d | 1080 | static void apb_log_get(struct es2_ap_dev *es2, char *buf) |
ca3ec299 | 1081 | { |
ca3ec299 GKH |
1082 | int retval; |
1083 | ||
ca3ec299 | 1084 | do { |
4b1d8204 AE |
1085 | retval = usb_control_msg(es2->usb_dev, |
1086 | usb_rcvctrlpipe(es2->usb_dev, 0), | |
e5273381 | 1087 | GB_APB_REQUEST_LOG, |
ca3ec299 GKH |
1088 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
1089 | 0x00, 0x00, | |
1090 | buf, | |
c15ccabe | 1091 | APB1_LOG_MSG_SIZE, |
60793c9b | 1092 | ES2_USB_CTRL_TIMEOUT); |
ca3ec299 | 1093 | if (retval > 0) |
3be0e17d | 1094 | kfifo_in(&es2->apb_log_fifo, buf, retval); |
ca3ec299 GKH |
1095 | } while (retval > 0); |
1096 | } | |
1097 | ||
3be0e17d | 1098 | static int apb_log_poll(void *data) |
ca3ec299 | 1099 | { |
4b1d8204 | 1100 | struct es2_ap_dev *es2 = data; |
c15ccabe JH |
1101 | char *buf; |
1102 | ||
1103 | buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL); | |
1104 | if (!buf) | |
1105 | return -ENOMEM; | |
1106 | ||
ca3ec299 GKH |
1107 | while (!kthread_should_stop()) { |
1108 | msleep(1000); | |
3be0e17d | 1109 | apb_log_get(es2, buf); |
ca3ec299 | 1110 | } |
c15ccabe JH |
1111 | |
1112 | kfree(buf); | |
1113 | ||
ca3ec299 GKH |
1114 | return 0; |
1115 | } | |
1116 | ||
3be0e17d | 1117 | static ssize_t apb_log_read(struct file *f, char __user *buf, |
ca3ec299 GKH |
1118 | size_t count, loff_t *ppos) |
1119 | { | |
45063097 | 1120 | struct es2_ap_dev *es2 = file_inode(f)->i_private; |
ca3ec299 GKH |
1121 | ssize_t ret; |
1122 | size_t copied; | |
1123 | char *tmp_buf; | |
1124 | ||
1125 | if (count > APB1_LOG_SIZE) | |
1126 | count = APB1_LOG_SIZE; | |
1127 | ||
1128 | tmp_buf = kmalloc(count, GFP_KERNEL); | |
1129 | if (!tmp_buf) | |
1130 | return -ENOMEM; | |
1131 | ||
3be0e17d | 1132 | copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count); |
ca3ec299 GKH |
1133 | ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied); |
1134 | ||
1135 | kfree(tmp_buf); | |
1136 | ||
1137 | return ret; | |
1138 | } | |
1139 | ||
3be0e17d AE |
1140 | static const struct file_operations apb_log_fops = { |
1141 | .read = apb_log_read, | |
ca3ec299 GKH |
1142 | }; |
1143 | ||
4b1d8204 | 1144 | static void usb_log_enable(struct es2_ap_dev *es2) |
ca3ec299 | 1145 | { |
3be0e17d | 1146 | if (!IS_ERR_OR_NULL(es2->apb_log_task)) |
ca3ec299 GKH |
1147 | return; |
1148 | ||
1149 | /* get log from APB1 */ | |
3be0e17d AE |
1150 | es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log"); |
1151 | if (IS_ERR(es2->apb_log_task)) | |
ca3ec299 | 1152 | return; |
3be0e17d AE |
1153 | /* XXX We will need to rename this per APB */ |
1154 | es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO, | |
05a84919 | 1155 | gb_debugfs_get(), es2, |
3be0e17d | 1156 | &apb_log_fops); |
ca3ec299 GKH |
1157 | } |
1158 | ||
4b1d8204 | 1159 | static void usb_log_disable(struct es2_ap_dev *es2) |
ca3ec299 | 1160 | { |
3be0e17d | 1161 | if (IS_ERR_OR_NULL(es2->apb_log_task)) |
ca3ec299 GKH |
1162 | return; |
1163 | ||
3be0e17d AE |
1164 | debugfs_remove(es2->apb_log_dentry); |
1165 | es2->apb_log_dentry = NULL; | |
ca3ec299 | 1166 | |
3be0e17d AE |
1167 | kthread_stop(es2->apb_log_task); |
1168 | es2->apb_log_task = NULL; | |
ca3ec299 GKH |
1169 | } |
1170 | ||
3be0e17d | 1171 | static ssize_t apb_log_enable_read(struct file *f, char __user *buf, |
ca3ec299 GKH |
1172 | size_t count, loff_t *ppos) |
1173 | { | |
45063097 | 1174 | struct es2_ap_dev *es2 = file_inode(f)->i_private; |
3be0e17d | 1175 | int enable = !IS_ERR_OR_NULL(es2->apb_log_task); |
4b1d8204 | 1176 | char tmp_buf[3]; |
ca3ec299 GKH |
1177 | |
1178 | sprintf(tmp_buf, "%d\n", enable); | |
1179 | return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3); | |
1180 | } | |
1181 | ||
3be0e17d | 1182 | static ssize_t apb_log_enable_write(struct file *f, const char __user *buf, |
ca3ec299 GKH |
1183 | size_t count, loff_t *ppos) |
1184 | { | |
1185 | int enable; | |
1186 | ssize_t retval; | |
45063097 | 1187 | struct es2_ap_dev *es2 = file_inode(f)->i_private; |
ca3ec299 GKH |
1188 | |
1189 | retval = kstrtoint_from_user(buf, count, 10, &enable); | |
1190 | if (retval) | |
1191 | return retval; | |
1192 | ||
1193 | if (enable) | |
4b1d8204 | 1194 | usb_log_enable(es2); |
ca3ec299 | 1195 | else |
4b1d8204 | 1196 | usb_log_disable(es2); |
ca3ec299 GKH |
1197 | |
1198 | return count; | |
1199 | } | |
1200 | ||
3be0e17d AE |
1201 | static const struct file_operations apb_log_enable_fops = { |
1202 | .read = apb_log_enable_read, | |
1203 | .write = apb_log_enable_write, | |
ca3ec299 GKH |
1204 | }; |
1205 | ||
3be0e17d | 1206 | static int apb_get_cport_count(struct usb_device *udev) |
24a6112f FP |
1207 | { |
1208 | int retval; | |
1209 | __le16 *cport_count; | |
1210 | ||
478ce720 | 1211 | cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL); |
24a6112f FP |
1212 | if (!cport_count) |
1213 | return -ENOMEM; | |
1214 | ||
1215 | retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), | |
e5273381 | 1216 | GB_APB_REQUEST_CPORT_COUNT, |
24a6112f FP |
1217 | USB_DIR_IN | USB_TYPE_VENDOR | |
1218 | USB_RECIP_INTERFACE, 0, 0, cport_count, | |
60793c9b | 1219 | sizeof(*cport_count), ES2_USB_CTRL_TIMEOUT); |
478ce720 | 1220 | if (retval != sizeof(*cport_count)) { |
24a6112f FP |
1221 | dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n", |
1222 | retval); | |
478ce720 JH |
1223 | |
1224 | if (retval >= 0) | |
1225 | retval = -EIO; | |
1226 | ||
24a6112f FP |
1227 | goto out; |
1228 | } | |
1229 | ||
1230 | retval = le16_to_cpu(*cport_count); | |
1231 | ||
1232 | /* We need to fit a CPort ID in one byte of a message header */ | |
1233 | if (retval > U8_MAX) { | |
1234 | retval = U8_MAX; | |
1235 | dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n"); | |
1236 | } | |
1237 | ||
1238 | out: | |
1239 | kfree(cport_count); | |
1240 | return retval; | |
1241 | } | |
1242 | ||
f587027e | 1243 | /* |
4d5c446b JH |
1244 | * The ES2 USB Bridge device has 15 endpoints |
1245 | * 1 Control - usual USB stuff + AP -> APBridgeA messages | |
1246 | * 7 Bulk IN - CPort data in | |
1247 | * 7 Bulk OUT - CPort data out | |
f587027e GKH |
1248 | */ |
1249 | static int ap_probe(struct usb_interface *interface, | |
1250 | const struct usb_device_id *id) | |
1251 | { | |
4b1d8204 | 1252 | struct es2_ap_dev *es2; |
2537636a | 1253 | struct gb_host_device *hd; |
f587027e GKH |
1254 | struct usb_device *udev; |
1255 | struct usb_host_interface *iface_desc; | |
1256 | struct usb_endpoint_descriptor *endpoint; | |
27229100 | 1257 | __u8 ep_addr; |
be5d01f1 | 1258 | int retval; |
f587027e | 1259 | int i; |
24a6112f | 1260 | int num_cports; |
403074b5 | 1261 | bool bulk_out_found = false; |
7330c48e GKH |
1262 | bool bulk_in_found = false; |
1263 | bool arpc_in_found = false; | |
4bc1389d | 1264 | |
f587027e GKH |
1265 | udev = usb_get_dev(interface_to_usbdev(interface)); |
1266 | ||
3be0e17d | 1267 | num_cports = apb_get_cport_count(udev); |
24a6112f FP |
1268 | if (num_cports < 0) { |
1269 | usb_put_dev(udev); | |
1270 | dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n", | |
1271 | num_cports); | |
1272 | return num_cports; | |
1273 | } | |
1274 | ||
d6e139bc JH |
1275 | hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX, |
1276 | num_cports); | |
8ea70fe0 | 1277 | if (IS_ERR(hd)) { |
f587027e | 1278 | usb_put_dev(udev); |
8ea70fe0 | 1279 | return PTR_ERR(hd); |
f587027e GKH |
1280 | } |
1281 | ||
4b1d8204 AE |
1282 | es2 = hd_to_es2(hd); |
1283 | es2->hd = hd; | |
1284 | es2->usb_intf = interface; | |
1285 | es2->usb_dev = udev; | |
1286 | spin_lock_init(&es2->cport_out_urb_lock); | |
3be0e17d | 1287 | INIT_KFIFO(es2->apb_log_fifo); |
4b1d8204 | 1288 | usb_set_intfdata(interface, es2); |
f587027e | 1289 | |
05061507 JH |
1290 | /* |
1291 | * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated | |
1292 | * dynamically. | |
1293 | */ | |
1294 | retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0); | |
1295 | if (retval) | |
1296 | goto error; | |
1297 | retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1); | |
1298 | if (retval) | |
1299 | goto error; | |
1300 | ||
4d5c446b | 1301 | /* find all bulk endpoints */ |
f587027e GKH |
1302 | iface_desc = interface->cur_altsetting; |
1303 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { | |
1304 | endpoint = &iface_desc->endpoint[i].desc; | |
27229100 | 1305 | ep_addr = endpoint->bEndpointAddress; |
f587027e | 1306 | |
b767ee40 | 1307 | if (usb_endpoint_is_bulk_in(endpoint)) { |
7330c48e | 1308 | if (!bulk_in_found) { |
27229100 | 1309 | es2->cport_in.endpoint = ep_addr; |
7330c48e GKH |
1310 | bulk_in_found = true; |
1311 | } else if (!arpc_in_found) { | |
27229100 | 1312 | es2->arpc_endpoint_in = ep_addr; |
7330c48e | 1313 | arpc_in_found = true; |
27229100 GKH |
1314 | } else { |
1315 | dev_warn(&udev->dev, | |
1316 | "Unused bulk IN endpoint found: 0x%02x\n", | |
1317 | ep_addr); | |
7330c48e | 1318 | } |
27229100 | 1319 | continue; |
f587027e | 1320 | } |
27229100 GKH |
1321 | if (usb_endpoint_is_bulk_out(endpoint)) { |
1322 | if (!bulk_out_found) { | |
1323 | es2->cport_out_endpoint = ep_addr; | |
1324 | bulk_out_found = true; | |
1325 | } else { | |
1326 | dev_warn(&udev->dev, | |
1327 | "Unused bulk OUT endpoint found: 0x%02x\n", | |
1328 | ep_addr); | |
1329 | } | |
1330 | continue; | |
1331 | } | |
1332 | dev_warn(&udev->dev, | |
1333 | "Unknown endpoint type found, address 0x%02x\n", | |
1334 | ep_addr); | |
f587027e | 1335 | } |
7330c48e | 1336 | if (!bulk_in_found || !arpc_in_found || !bulk_out_found) { |
f587027e | 1337 | dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n"); |
be5d01f1 | 1338 | retval = -ENODEV; |
f587027e GKH |
1339 | goto error; |
1340 | } | |
1341 | ||
0ce68ce4 | 1342 | /* Allocate buffers for our cport in messages */ |
7330c48e GKH |
1343 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { |
1344 | struct urb *urb; | |
1345 | u8 *buffer; | |
606addd2 | 1346 | |
7330c48e GKH |
1347 | urb = usb_alloc_urb(0, GFP_KERNEL); |
1348 | if (!urb) { | |
1349 | retval = -ENOMEM; | |
1350 | goto error; | |
1351 | } | |
1352 | es2->cport_in.urb[i] = urb; | |
5f62eab0 | 1353 | |
7330c48e GKH |
1354 | buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL); |
1355 | if (!buffer) { | |
1356 | retval = -ENOMEM; | |
1357 | goto error; | |
1358 | } | |
606addd2 | 1359 | |
7330c48e GKH |
1360 | usb_fill_bulk_urb(urb, udev, |
1361 | usb_rcvbulkpipe(udev, es2->cport_in.endpoint), | |
1362 | buffer, ES2_GBUF_MSG_SIZE_MAX, | |
1363 | cport_in_callback, hd); | |
5f62eab0 | 1364 | |
7330c48e | 1365 | es2->cport_in.buffer[i] = buffer; |
f587027e GKH |
1366 | } |
1367 | ||
9d9d3777 AB |
1368 | /* Allocate buffers for ARPC in messages */ |
1369 | for (i = 0; i < NUM_ARPC_IN_URB; ++i) { | |
1370 | struct urb *urb; | |
1371 | u8 *buffer; | |
1372 | ||
1373 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
1374 | if (!urb) { | |
1375 | retval = -ENOMEM; | |
1376 | goto error; | |
1377 | } | |
5f62eab0 JH |
1378 | es2->arpc_urb[i] = urb; |
1379 | ||
9d9d3777 AB |
1380 | buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL); |
1381 | if (!buffer) { | |
1382 | retval = -ENOMEM; | |
1383 | goto error; | |
1384 | } | |
1385 | ||
1386 | usb_fill_bulk_urb(urb, udev, | |
1387 | usb_rcvbulkpipe(udev, | |
1388 | es2->arpc_endpoint_in), | |
1389 | buffer, ARPC_IN_SIZE_MAX, | |
1390 | arpc_in_callback, es2); | |
1391 | ||
9d9d3777 AB |
1392 | es2->arpc_buffer[i] = buffer; |
1393 | } | |
1394 | ||
f587027e GKH |
1395 | /* Allocate urbs for our CPort OUT messages */ |
1396 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { | |
1397 | struct urb *urb; | |
1398 | ||
1399 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
be5d01f1 JH |
1400 | if (!urb) { |
1401 | retval = -ENOMEM; | |
f587027e | 1402 | goto error; |
be5d01f1 | 1403 | } |
f587027e | 1404 | |
4b1d8204 AE |
1405 | es2->cport_out_urb[i] = urb; |
1406 | es2->cport_out_urb_busy[i] = false; /* just to be anal */ | |
f587027e GKH |
1407 | } |
1408 | ||
3be0e17d AE |
1409 | /* XXX We will need to rename this per APB */ |
1410 | es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable", | |
86f918ee | 1411 | (S_IWUSR | S_IRUGO), |
4b1d8204 | 1412 | gb_debugfs_get(), es2, |
3be0e17d | 1413 | &apb_log_enable_fops); |
0ce68ce4 | 1414 | |
c14118a8 AB |
1415 | INIT_LIST_HEAD(&es2->arpcs); |
1416 | spin_lock_init(&es2->arpc_lock); | |
1417 | ||
1305f2b2 WY |
1418 | retval = es2_arpc_in_enable(es2); |
1419 | if (retval) | |
9d9d3777 AB |
1420 | goto error; |
1421 | ||
c1700479 JH |
1422 | retval = gb_hd_add(hd); |
1423 | if (retval) | |
9d9d3777 | 1424 | goto err_disable_arpc_in; |
c1700479 | 1425 | |
7330c48e GKH |
1426 | retval = es2_cport_in_enable(es2, &es2->cport_in); |
1427 | if (retval) | |
1428 | goto err_hd_del; | |
0ce68ce4 | 1429 | |
f587027e | 1430 | return 0; |
57bc17ff | 1431 | |
7330c48e | 1432 | err_hd_del: |
c1700479 | 1433 | gb_hd_del(hd); |
9d9d3777 AB |
1434 | err_disable_arpc_in: |
1435 | es2_arpc_in_disable(es2); | |
f587027e | 1436 | error: |
57bc17ff | 1437 | es2_destroy(es2); |
f587027e GKH |
1438 | |
1439 | return retval; | |
1440 | } | |
1441 | ||
2437f1c6 VK |
1442 | static void ap_disconnect(struct usb_interface *interface) |
1443 | { | |
1444 | struct es2_ap_dev *es2 = usb_get_intfdata(interface); | |
2437f1c6 VK |
1445 | |
1446 | gb_hd_del(es2->hd); | |
1447 | ||
7330c48e | 1448 | es2_cport_in_disable(es2, &es2->cport_in); |
9d9d3777 | 1449 | es2_arpc_in_disable(es2); |
2437f1c6 VK |
1450 | |
1451 | es2_destroy(es2); | |
1452 | } | |
1453 | ||
4b1d8204 | 1454 | static struct usb_driver es2_ap_driver = { |
c13c8bf0 | 1455 | .name = "es2_ap_driver", |
f587027e GKH |
1456 | .probe = ap_probe, |
1457 | .disconnect = ap_disconnect, | |
1458 | .id_table = id_table, | |
a0f997bd | 1459 | .soft_unbind = 1, |
f587027e GKH |
1460 | }; |
1461 | ||
4b1d8204 | 1462 | module_usb_driver(es2_ap_driver); |
f587027e | 1463 | |
6cf42a44 | 1464 | MODULE_LICENSE("GPL v2"); |
f587027e | 1465 | MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>"); |