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