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