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