2 * Greybus "AP" USB driver for "ES2" controller chips
4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
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>
18 #include "greybus_trace.h"
19 #include "kernel_ver.h"
20 #include "connection.h"
22 /* Fixed CPort numbers */
23 #define ES2_CPORT_CDSI0 16
24 #define ES2_CPORT_CDSI1 17
26 /* Memory sizes for the buffers sent to/from the ES2 controller */
27 #define ES2_GBUF_MSG_SIZE_MAX 2048
29 /* Memory sizes for the ARPC buffers */
30 #define ARPC_OUT_SIZE_MAX U16_MAX
31 #define ARPC_IN_SIZE_MAX 128
33 static const struct usb_device_id id_table[] = {
34 { USB_DEVICE(0x18d1, 0x1eaf) },
37 MODULE_DEVICE_TABLE(usb, id_table);
39 #define APB1_LOG_SIZE SZ_16K
41 /* Number of bulk in and bulk out couple */
44 /* Expected number of bulk out endpoints */
45 #define NUM_BULKS_OUT NUM_BULKS
47 /* Expected number of bulk in endpoints (including ARPC endpoint) */
48 #define NUM_BULKS_IN (NUM_BULKS + 1)
51 * Number of CPort IN urbs in flight at any point in time.
52 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
55 #define NUM_CPORT_IN_URB 4
57 /* Number of CPort OUT urbs in flight at any point in time.
58 * Adjust if we get messages saying we are out of urbs in the system log.
60 #define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
63 * Number of ARPC in urbs in flight at any point in time.
65 #define NUM_ARPC_IN_URB 2
68 * @endpoint: bulk in endpoint for CPort data
69 * @urb: array of urbs for the CPort in messages
70 * @buffer: array of buffers for the @cport_in_urb urbs
74 struct urb *urb[NUM_CPORT_IN_URB];
75 u8 *buffer[NUM_CPORT_IN_URB];
79 * @endpoint: bulk out endpoint for CPort data
81 struct es2_cport_out {
86 * es2_ap_dev - ES2 USB Bridge to AP structure
87 * @usb_dev: pointer to the USB device we are.
88 * @usb_intf: pointer to the USB interface we are bound to.
89 * @hd: pointer to our gb_host_device structure
91 * @cport_in: endpoint, urbs and buffer for cport in messages
92 * @cport_out: endpoint for for cport out messages
93 * @cport_out_urb: array of urbs for the CPort out messages
94 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
96 * @cport_out_urb_cancelled: array of flags indicating whether the
97 * corresponding @cport_out_urb is being cancelled
98 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
100 * @apb_log_task: task pointer for logging thread
101 * @apb_log_dentry: file system entry for the log file interface
102 * @apb_log_enable_dentry: file system entry for enabling logging
103 * @apb_log_fifo: kernel FIFO to carry logged data
104 * @arpc_urb: array of urbs for the ARPC in messages
105 * @arpc_buffer: array of buffers for the @arpc_urb urbs
106 * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC
107 * @arpc_id_cycle: gives an unique id to ARPC
108 * @arpc_lock: locks ARPC list
109 * @arpcs: list of in progress ARPCs
112 struct usb_device *usb_dev;
113 struct usb_interface *usb_intf;
114 struct gb_host_device *hd;
116 struct es2_cport_in cport_in[NUM_BULKS];
117 struct es2_cport_out cport_out[NUM_BULKS];
118 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
119 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
120 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
121 spinlock_t cport_out_urb_lock;
127 struct task_struct *apb_log_task;
128 struct dentry *apb_log_dentry;
129 struct dentry *apb_log_enable_dentry;
130 DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
132 __u8 arpc_endpoint_in;
133 struct urb *arpc_urb[NUM_ARPC_IN_URB];
134 u8 *arpc_buffer[NUM_ARPC_IN_URB];
137 spinlock_t arpc_lock;
138 struct list_head arpcs;
142 * cport_to_ep - information about cport to endpoints mapping
143 * @cport_id: the id of cport to map to endpoints
144 * @endpoint_in: the endpoint number to use for in transfer
145 * @endpoint_out: he endpoint number to use for out transfer
154 * timesync_enable_request - Enable timesync in an APBridge
155 * @count: number of TimeSync Pulses to expect
156 * @frame_time: the initial FrameTime at the first TimeSync Pulse
157 * @strobe_delay: the expected delay in microseconds between each TimeSync Pulse
158 * @refclk: The AP mandated reference clock to run FrameTime at
160 struct timesync_enable_request {
168 * timesync_authoritative_request - Transmit authoritative FrameTime to APBridge
169 * @frame_time: An array of authoritative FrameTimes provided by the SVC
170 * and relayed to the APBridge by the AP
172 struct timesync_authoritative_request {
173 __le64 frame_time[GB_TIMESYNC_MAX_STROBES];
177 struct list_head list;
178 struct arpc_request_message *req;
179 struct arpc_response_message *resp;
180 struct completion response_received;
184 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
186 return (struct es2_ap_dev *)&hd->hd_priv;
189 static void cport_out_callback(struct urb *urb);
190 static void usb_log_enable(struct es2_ap_dev *es2);
191 static void usb_log_disable(struct es2_ap_dev *es2);
192 static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
193 size_t size, int *result, unsigned int timeout);
195 /* Get the endpoints pair mapped to the cport */
196 static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id)
198 if (cport_id >= es2->hd->num_cports)
200 return es2->cport_to_ep[cport_id];
203 #define ES2_TIMEOUT 500 /* 500 ms for the SVC to do something */
205 /* Disable for now until we work all of this out to keep a warning-free build */
207 /* Test if the endpoints pair is already mapped to a cport */
208 static int ep_pair_in_use(struct es2_ap_dev *es2, int ep_pair)
212 for (i = 0; i < es2->hd->num_cports; i++) {
213 if (es2->cport_to_ep[i] == ep_pair)
219 /* Configure the endpoint mapping and send the request to APBridge */
220 static int map_cport_to_ep(struct es2_ap_dev *es2,
221 u16 cport_id, int ep_pair)
224 struct cport_to_ep *cport_to_ep;
226 if (ep_pair < 0 || ep_pair >= NUM_BULKS)
228 if (cport_id >= es2->hd->num_cports)
230 if (ep_pair && ep_pair_in_use(es2, ep_pair))
233 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
237 es2->cport_to_ep[cport_id] = ep_pair;
238 cport_to_ep->cport_id = cpu_to_le16(cport_id);
239 cport_to_ep->endpoint_in = es2->cport_in[ep_pair].endpoint;
240 cport_to_ep->endpoint_out = es2->cport_out[ep_pair].endpoint;
242 retval = usb_control_msg(es2->usb_dev,
243 usb_sndctrlpipe(es2->usb_dev, 0),
244 GB_APB_REQUEST_EP_MAPPING,
245 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
248 sizeof(*cport_to_ep),
250 if (retval == sizeof(*cport_to_ep))
257 /* Unmap a cport: use the muxed endpoints pair */
258 static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id)
260 return map_cport_to_ep(es2, cport_id, 0);
264 static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
266 struct usb_device *udev = es2->usb_dev;
270 data = kmalloc(size, GFP_KERNEL);
273 memcpy(data, req, size);
275 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
277 USB_DIR_OUT | USB_TYPE_VENDOR |
279 0, 0, data, size, ES2_TIMEOUT);
281 dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
289 static void ap_urb_complete(struct urb *urb)
291 struct usb_ctrlrequest *dr = urb->context;
297 static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
299 struct usb_device *udev = es2->usb_dev;
301 struct usb_ctrlrequest *dr;
305 urb = usb_alloc_urb(0, GFP_ATOMIC);
309 dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
315 buf = (u8 *)dr + sizeof(*dr);
316 memcpy(buf, req, size);
319 dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
322 dr->wLength = cpu_to_le16(size);
324 usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
325 (unsigned char *)dr, buf, size,
326 ap_urb_complete, dr);
327 retval = usb_submit_urb(urb, GFP_ATOMIC);
335 static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
338 struct es2_ap_dev *es2 = hd_to_es2(hd);
341 return output_async(es2, req, size, cmd);
343 return output_sync(es2, req, size, cmd);
346 static int es2_cport_in_enable(struct es2_ap_dev *es2,
347 struct es2_cport_in *cport_in)
353 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
354 urb = cport_in->urb[i];
356 ret = usb_submit_urb(urb, GFP_KERNEL);
358 dev_err(&es2->usb_dev->dev,
359 "failed to submit in-urb: %d\n", ret);
367 for (--i; i >= 0; --i) {
368 urb = cport_in->urb[i];
375 static void es2_cport_in_disable(struct es2_ap_dev *es2,
376 struct es2_cport_in *cport_in)
381 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
382 urb = cport_in->urb[i];
387 static int es2_arpc_in_enable(struct es2_ap_dev *es2)
393 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
394 urb = es2->arpc_urb[i];
396 ret = usb_submit_urb(urb, GFP_KERNEL);
398 dev_err(&es2->usb_dev->dev,
399 "failed to submit arpc in-urb: %d\n", ret);
407 for (--i; i >= 0; --i) {
408 urb = es2->arpc_urb[i];
415 static void es2_arpc_in_disable(struct es2_ap_dev *es2)
420 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
421 urb = es2->arpc_urb[i];
426 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
428 struct urb *urb = NULL;
432 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
434 /* Look in our pool of allocated urbs first, as that's the "fastest" */
435 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
436 if (es2->cport_out_urb_busy[i] == false &&
437 es2->cport_out_urb_cancelled[i] == false) {
438 es2->cport_out_urb_busy[i] = true;
439 urb = es2->cport_out_urb[i];
443 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
448 * Crap, pool is empty, complain to the syslog and go allocate one
449 * dynamically as we have to succeed.
451 dev_dbg(&es2->usb_dev->dev,
452 "No free CPort OUT urbs, having to dynamically allocate one!\n");
453 return usb_alloc_urb(0, gfp_mask);
456 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
461 * See if this was an urb in our pool, if so mark it "free", otherwise
462 * we need to free it ourselves.
464 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
465 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
466 if (urb == es2->cport_out_urb[i]) {
467 es2->cport_out_urb_busy[i] = false;
472 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
474 /* If urb is not NULL, then we need to free this urb */
479 * We (ab)use the operation-message header pad bytes to transfer the
480 * cport id in order to minimise overhead.
483 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
485 header->pad[0] = cport_id;
488 /* Clear the pad bytes used for the CPort id */
489 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
494 /* Extract the CPort id packed into the header, and clear it */
495 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
497 u16 cport_id = header->pad[0];
499 gb_message_cport_clear(header);
505 * Returns zero if the message was successfully queued, or a negative errno
508 static int message_send(struct gb_host_device *hd, u16 cport_id,
509 struct gb_message *message, gfp_t gfp_mask)
511 struct es2_ap_dev *es2 = hd_to_es2(hd);
512 struct usb_device *udev = es2->usb_dev;
520 * The data actually transferred will include an indication
521 * of where the data should be sent. Do one last check of
522 * the target CPort id before filling it in.
524 if (!cport_id_valid(hd, cport_id)) {
525 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
529 /* Find a free urb */
530 urb = next_free_urb(es2, gfp_mask);
534 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
535 message->hcpriv = urb;
536 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
538 /* Pack the cport id into the message header */
539 gb_message_cport_pack(message->header, cport_id);
541 buffer_size = sizeof(*message->header) + message->payload_size;
543 ep_pair = cport_to_ep_pair(es2, cport_id);
544 usb_fill_bulk_urb(urb, udev,
545 usb_sndbulkpipe(udev,
546 es2->cport_out[ep_pair].endpoint),
547 message->buffer, buffer_size,
548 cport_out_callback, message);
549 urb->transfer_flags |= URB_ZERO_PACKET;
551 trace_gb_message_submit(message);
553 retval = usb_submit_urb(urb, gfp_mask);
555 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
557 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
558 message->hcpriv = NULL;
559 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
562 gb_message_cport_clear(message->header);
571 * Can not be called in atomic context.
573 static void message_cancel(struct gb_message *message)
575 struct gb_host_device *hd = message->operation->connection->hd;
576 struct es2_ap_dev *es2 = hd_to_es2(hd);
582 spin_lock_irq(&es2->cport_out_urb_lock);
583 urb = message->hcpriv;
585 /* Prevent dynamically allocated urb from being deallocated. */
588 /* Prevent pre-allocated urb from being reused. */
589 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
590 if (urb == es2->cport_out_urb[i]) {
591 es2->cport_out_urb_cancelled[i] = true;
595 spin_unlock_irq(&es2->cport_out_urb_lock);
599 if (i < NUM_CPORT_OUT_URB) {
600 spin_lock_irq(&es2->cport_out_urb_lock);
601 es2->cport_out_urb_cancelled[i] = false;
602 spin_unlock_irq(&es2->cport_out_urb_lock);
608 static int cport_reset(struct gb_host_device *hd, u16 cport_id)
610 struct es2_ap_dev *es2 = hd_to_es2(hd);
611 struct usb_device *udev = es2->usb_dev;
612 struct arpc_cport_reset req;
617 case GB_SVC_CPORT_ID:
618 case ES2_CPORT_CDSI0:
619 case ES2_CPORT_CDSI1:
623 req.cport_id = cpu_to_le16(cport_id);
624 retval = arpc_sync(es2, ARPC_CPORT_RESET, &req, sizeof(req),
625 &result, ES2_TIMEOUT);
626 if (retval == -EREMOTEIO) {
627 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
634 static int es2_cport_allocate(struct gb_host_device *hd, int cport_id,
637 struct es2_ap_dev *es2 = hd_to_es2(hd);
638 struct ida *id_map = &hd->cport_id_map;
639 int ida_start, ida_end;
642 case ES2_CPORT_CDSI0:
643 case ES2_CPORT_CDSI1:
644 dev_err(&hd->dev, "cport %d not available\n", cport_id);
648 if (flags & GB_CONNECTION_FLAG_OFFLOADED &&
649 flags & GB_CONNECTION_FLAG_CDSI1) {
650 if (es2->cdsi1_in_use) {
651 dev_err(&hd->dev, "CDSI1 already in use\n");
655 es2->cdsi1_in_use = true;
657 return ES2_CPORT_CDSI1;
662 ida_end = hd->num_cports;
663 } else if (cport_id < hd->num_cports) {
664 ida_start = cport_id;
665 ida_end = cport_id + 1;
667 dev_err(&hd->dev, "cport %d not available\n", cport_id);
671 return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
674 static void es2_cport_release(struct gb_host_device *hd, u16 cport_id)
676 struct es2_ap_dev *es2 = hd_to_es2(hd);
679 case ES2_CPORT_CDSI1:
680 es2->cdsi1_in_use = false;
684 ida_simple_remove(&hd->cport_id_map, cport_id);
687 static int cport_enable(struct gb_host_device *hd, u16 cport_id,
690 struct es2_ap_dev *es2 = hd_to_es2(hd);
691 struct usb_device *udev = es2->usb_dev;
692 struct gb_apb_request_cport_flags *req;
695 req = kzalloc(sizeof(*req), GFP_KERNEL);
699 if (flags & GB_CONNECTION_FLAG_CONTROL)
700 req->flags |= GB_APB_CPORT_FLAG_CONTROL;
701 if (flags & GB_CONNECTION_FLAG_HIGH_PRIO)
702 req->flags |= GB_APB_CPORT_FLAG_HIGH_PRIO;
704 dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__,
705 cport_id, req->flags);
707 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
708 GB_APB_REQUEST_CPORT_FLAGS,
709 USB_DIR_OUT | USB_TYPE_VENDOR |
710 USB_RECIP_INTERFACE, cport_id, 0,
711 req, sizeof(*req), ES2_TIMEOUT);
712 if (ret != sizeof(*req)) {
713 dev_err(&udev->dev, "failed to set cport flags for port %d\n",
728 static int cport_disable(struct gb_host_device *hd, u16 cport_id)
732 retval = cport_reset(hd, cport_id);
739 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
742 struct es2_ap_dev *es2 = hd_to_es2(hd);
743 struct usb_device *udev = es2->usb_dev;
745 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
746 GB_APB_REQUEST_LATENCY_TAG_EN,
747 USB_DIR_OUT | USB_TYPE_VENDOR |
748 USB_RECIP_INTERFACE, cport_id, 0, NULL,
752 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
757 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
760 struct es2_ap_dev *es2 = hd_to_es2(hd);
761 struct usb_device *udev = es2->usb_dev;
763 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
764 GB_APB_REQUEST_LATENCY_TAG_DIS,
765 USB_DIR_OUT | USB_TYPE_VENDOR |
766 USB_RECIP_INTERFACE, cport_id, 0, NULL,
770 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
775 static int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
778 struct es2_ap_dev *es2 = hd_to_es2(hd);
779 struct usb_device *udev = es2->usb_dev;
781 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
782 GB_APB_REQUEST_CPORT_FEAT_EN,
783 USB_DIR_OUT | USB_TYPE_VENDOR |
784 USB_RECIP_INTERFACE, cport_id, 0, NULL,
787 dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
792 static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
795 struct es2_ap_dev *es2 = hd_to_es2(hd);
796 struct usb_device *udev = es2->usb_dev;
798 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
799 GB_APB_REQUEST_CPORT_FEAT_DIS,
800 USB_DIR_OUT | USB_TYPE_VENDOR |
801 USB_RECIP_INTERFACE, cport_id, 0, NULL,
805 "Cannot disable CPort features for cport %u: %d\n",
810 static int timesync_enable(struct gb_host_device *hd, u8 count,
811 u64 frame_time, u32 strobe_delay, u32 refclk)
814 struct es2_ap_dev *es2 = hd_to_es2(hd);
815 struct usb_device *udev = es2->usb_dev;
816 struct gb_control_timesync_enable_request *request;
818 request = kzalloc(sizeof(*request), GFP_KERNEL);
822 request->count = count;
823 request->frame_time = cpu_to_le64(frame_time);
824 request->strobe_delay = cpu_to_le32(strobe_delay);
825 request->refclk = cpu_to_le32(refclk);
826 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
827 REQUEST_TIMESYNC_ENABLE,
828 USB_DIR_OUT | USB_TYPE_VENDOR |
829 USB_RECIP_INTERFACE, 0, 0, request,
830 sizeof(*request), ES2_TIMEOUT);
832 dev_err(&udev->dev, "Cannot enable timesync %d\n", retval);
838 static int timesync_disable(struct gb_host_device *hd)
841 struct es2_ap_dev *es2 = hd_to_es2(hd);
842 struct usb_device *udev = es2->usb_dev;
844 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
845 REQUEST_TIMESYNC_DISABLE,
846 USB_DIR_OUT | USB_TYPE_VENDOR |
847 USB_RECIP_INTERFACE, 0, 0, NULL,
850 dev_err(&udev->dev, "Cannot disable timesync %d\n", retval);
855 static int timesync_authoritative(struct gb_host_device *hd, u64 *frame_time)
858 struct es2_ap_dev *es2 = hd_to_es2(hd);
859 struct usb_device *udev = es2->usb_dev;
860 struct timesync_authoritative_request *request;
862 request = kzalloc(sizeof(*request), GFP_KERNEL);
866 for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
867 request->frame_time[i] = cpu_to_le64(frame_time[i]);
869 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
870 REQUEST_TIMESYNC_AUTHORITATIVE,
871 USB_DIR_OUT | USB_TYPE_VENDOR |
872 USB_RECIP_INTERFACE, 0, 0, request,
873 sizeof(*request), ES2_TIMEOUT);
875 dev_err(&udev->dev, "Cannot timesync authoritative out %d\n", retval);
881 static int timesync_get_last_event(struct gb_host_device *hd, u64 *frame_time)
884 struct es2_ap_dev *es2 = hd_to_es2(hd);
885 struct usb_device *udev = es2->usb_dev;
886 __le64 *response_frame_time;
888 response_frame_time = kzalloc(sizeof(*response_frame_time), GFP_KERNEL);
889 if (!response_frame_time)
892 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
893 REQUEST_TIMESYNC_GET_LAST_EVENT,
894 USB_DIR_IN | USB_TYPE_VENDOR |
895 USB_RECIP_INTERFACE, 0, 0, response_frame_time,
896 sizeof(*response_frame_time), ES2_TIMEOUT);
898 if (retval != sizeof(*response_frame_time)) {
899 dev_err(&udev->dev, "Cannot get last TimeSync event: %d\n",
907 *frame_time = le64_to_cpu(*response_frame_time);
910 kfree(response_frame_time);
914 static struct gb_hd_driver es2_driver = {
915 .hd_priv_size = sizeof(struct es2_ap_dev),
916 .message_send = message_send,
917 .message_cancel = message_cancel,
918 .cport_allocate = es2_cport_allocate,
919 .cport_release = es2_cport_release,
920 .cport_enable = cport_enable,
921 .cport_disable = cport_disable,
922 .latency_tag_enable = latency_tag_enable,
923 .latency_tag_disable = latency_tag_disable,
925 .cport_features_enable = cport_features_enable,
926 .cport_features_disable = cport_features_disable,
927 .timesync_enable = timesync_enable,
928 .timesync_disable = timesync_disable,
929 .timesync_authoritative = timesync_authoritative,
930 .timesync_get_last_event = timesync_get_last_event,
933 /* Common function to report consistent warnings based on URB status */
934 static int check_urb_status(struct urb *urb)
936 struct device *dev = &urb->dev->dev;
937 int status = urb->status;
944 dev_err(dev, "%s: overflow actual length is %d\n",
945 __func__, urb->actual_length);
951 /* device is gone, stop sending */
954 dev_err(dev, "%s: unknown status %d\n", __func__, status);
959 static void es2_destroy(struct es2_ap_dev *es2)
961 struct usb_device *udev;
965 debugfs_remove(es2->apb_log_enable_dentry);
966 usb_log_disable(es2);
968 /* Tear down everything! */
969 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
970 struct urb *urb = es2->cport_out_urb[i];
976 es2->cport_out_urb[i] = NULL;
977 es2->cport_out_urb_busy[i] = false; /* just to be anal */
980 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
981 struct urb *urb = es2->arpc_urb[i];
986 kfree(es2->arpc_buffer[i]);
987 es2->arpc_buffer[i] = NULL;
990 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
991 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
993 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
994 struct urb *urb = cport_in->urb[i];
999 kfree(cport_in->buffer[i]);
1000 cport_in->buffer[i] = NULL;
1004 kfree(es2->cport_to_ep);
1006 /* release reserved CDSI0 and CDSI1 cports */
1007 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
1008 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);
1010 udev = es2->usb_dev;
1016 static void cport_in_callback(struct urb *urb)
1018 struct gb_host_device *hd = urb->context;
1019 struct device *dev = &urb->dev->dev;
1020 struct gb_operation_msg_hdr *header;
1021 int status = check_urb_status(urb);
1026 if ((status == -EAGAIN) || (status == -EPROTO))
1029 /* The urb is being unlinked */
1030 if (status == -ENOENT || status == -ESHUTDOWN)
1033 dev_err(dev, "urb cport in error %d (dropped)\n", status);
1037 if (urb->actual_length < sizeof(*header)) {
1038 dev_err(dev, "short message received\n");
1042 /* Extract the CPort id, which is packed in the message header */
1043 header = urb->transfer_buffer;
1044 cport_id = gb_message_cport_unpack(header);
1046 if (cport_id_valid(hd, cport_id)) {
1047 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
1048 urb->actual_length);
1050 dev_err(dev, "invalid cport id %u received\n", cport_id);
1053 /* put our urb back in the request pool */
1054 retval = usb_submit_urb(urb, GFP_ATOMIC);
1056 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
1059 static void cport_out_callback(struct urb *urb)
1061 struct gb_message *message = urb->context;
1062 struct gb_host_device *hd = message->operation->connection->hd;
1063 struct es2_ap_dev *es2 = hd_to_es2(hd);
1064 int status = check_urb_status(urb);
1065 unsigned long flags;
1067 gb_message_cport_clear(message->header);
1069 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
1070 message->hcpriv = NULL;
1071 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
1074 * Tell the submitter that the message send (attempt) is
1075 * complete, and report the status.
1077 greybus_message_sent(hd, message, status);
1082 static struct arpc *arpc_alloc(void *payload, u16 size, u8 type)
1086 if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX)
1089 rpc = kzalloc(sizeof(*rpc), GFP_KERNEL);
1093 INIT_LIST_HEAD(&rpc->list);
1094 rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL);
1098 rpc->resp = kzalloc(sizeof(*rpc->resp), GFP_KERNEL);
1102 rpc->req->type = type;
1103 rpc->req->size = cpu_to_le16(sizeof(rpc->req) + size);
1104 memcpy(rpc->req->data, payload, size);
1106 init_completion(&rpc->response_received);
1118 static void arpc_free(struct arpc *rpc)
1125 static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id)
1129 list_for_each_entry(rpc, &es2->arpcs, list) {
1130 if (rpc->req->id == id)
1137 static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc)
1140 rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++);
1141 list_add_tail(&es2->arpcs, &rpc->list);
1144 static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc)
1147 rpc->active = false;
1148 list_del(&rpc->list);
1152 static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout)
1154 struct usb_device *udev = es2->usb_dev;
1157 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1158 APBA_REQUEST_ARPC_RUN,
1159 USB_DIR_OUT | USB_TYPE_VENDOR |
1160 USB_RECIP_INTERFACE,
1162 rpc->req, le16_to_cpu(rpc->req->size),
1164 if (retval != le16_to_cpu(rpc->req->size)) {
1166 "failed to send ARPC request %d: %d\n",
1167 rpc->req->type, retval);
1176 static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
1177 size_t size, int *result, unsigned int timeout)
1180 unsigned long flags;
1183 rpc = arpc_alloc(payload, size, type);
1187 spin_lock_irqsave(&es2->arpc_lock, flags);
1189 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1191 retval = arpc_send(es2, rpc, timeout);
1195 retval = wait_for_completion_interruptible_timeout(
1196 &rpc->response_received,
1197 msecs_to_jiffies(timeout));
1200 retval = -ETIMEDOUT;
1204 *result = rpc->resp->result;
1206 retval = -EREMOTEIO;
1211 spin_lock_irqsave(&es2->arpc_lock, flags);
1213 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1216 if (retval < 0 && retval != -EREMOTEIO) {
1217 dev_err(&es2->usb_dev->dev,
1218 "failed to execute ARPC: %d\n", retval);
1224 static void arpc_in_callback(struct urb *urb)
1226 struct es2_ap_dev *es2 = urb->context;
1227 struct device *dev = &urb->dev->dev;
1228 int status = check_urb_status(urb);
1230 struct arpc_response_message *resp;
1231 unsigned long flags;
1235 if ((status == -EAGAIN) || (status == -EPROTO))
1238 /* The urb is being unlinked */
1239 if (status == -ENOENT || status == -ESHUTDOWN)
1242 dev_err(dev, "arpc in-urb error %d (dropped)\n", status);
1246 if (urb->actual_length < sizeof(*resp)) {
1247 dev_err(dev, "short aprc response received\n");
1251 resp = urb->transfer_buffer;
1252 spin_lock_irqsave(&es2->arpc_lock, flags);
1253 rpc = arpc_find(es2, resp->id);
1255 dev_err(dev, "invalid arpc response id received: %d\n",
1257 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1262 memcpy(rpc->resp, resp, sizeof(*resp));
1263 complete(&rpc->response_received);
1264 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1267 /* put our urb back in the request pool */
1268 retval = usb_submit_urb(urb, GFP_ATOMIC);
1270 dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval);
1273 #define APB1_LOG_MSG_SIZE 64
1274 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
1278 /* SVC messages go down our control pipe */
1280 retval = usb_control_msg(es2->usb_dev,
1281 usb_rcvctrlpipe(es2->usb_dev, 0),
1283 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1289 kfifo_in(&es2->apb_log_fifo, buf, retval);
1290 } while (retval > 0);
1293 static int apb_log_poll(void *data)
1295 struct es2_ap_dev *es2 = data;
1298 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
1302 while (!kthread_should_stop()) {
1304 apb_log_get(es2, buf);
1312 static ssize_t apb_log_read(struct file *f, char __user *buf,
1313 size_t count, loff_t *ppos)
1315 struct es2_ap_dev *es2 = f->f_inode->i_private;
1320 if (count > APB1_LOG_SIZE)
1321 count = APB1_LOG_SIZE;
1323 tmp_buf = kmalloc(count, GFP_KERNEL);
1327 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
1328 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
1335 static const struct file_operations apb_log_fops = {
1336 .read = apb_log_read,
1339 static void usb_log_enable(struct es2_ap_dev *es2)
1341 if (!IS_ERR_OR_NULL(es2->apb_log_task))
1344 /* get log from APB1 */
1345 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
1346 if (IS_ERR(es2->apb_log_task))
1348 /* XXX We will need to rename this per APB */
1349 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
1350 gb_debugfs_get(), es2,
1354 static void usb_log_disable(struct es2_ap_dev *es2)
1356 if (IS_ERR_OR_NULL(es2->apb_log_task))
1359 debugfs_remove(es2->apb_log_dentry);
1360 es2->apb_log_dentry = NULL;
1362 kthread_stop(es2->apb_log_task);
1363 es2->apb_log_task = NULL;
1366 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
1367 size_t count, loff_t *ppos)
1369 struct es2_ap_dev *es2 = f->f_inode->i_private;
1370 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
1373 sprintf(tmp_buf, "%d\n", enable);
1374 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
1377 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
1378 size_t count, loff_t *ppos)
1382 struct es2_ap_dev *es2 = f->f_inode->i_private;
1384 retval = kstrtoint_from_user(buf, count, 10, &enable);
1389 usb_log_enable(es2);
1391 usb_log_disable(es2);
1396 static const struct file_operations apb_log_enable_fops = {
1397 .read = apb_log_enable_read,
1398 .write = apb_log_enable_write,
1401 static int apb_get_cport_count(struct usb_device *udev)
1404 __le16 *cport_count;
1406 cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
1410 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1411 GB_APB_REQUEST_CPORT_COUNT,
1412 USB_DIR_IN | USB_TYPE_VENDOR |
1413 USB_RECIP_INTERFACE, 0, 0, cport_count,
1414 sizeof(*cport_count), ES2_TIMEOUT);
1415 if (retval != sizeof(*cport_count)) {
1416 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1425 retval = le16_to_cpu(*cport_count);
1427 /* We need to fit a CPort ID in one byte of a message header */
1428 if (retval > U8_MAX) {
1430 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
1439 * The ES2 USB Bridge device has 15 endpoints
1440 * 1 Control - usual USB stuff + AP -> APBridgeA messages
1441 * 7 Bulk IN - CPort data in
1442 * 7 Bulk OUT - CPort data out
1444 static int ap_probe(struct usb_interface *interface,
1445 const struct usb_device_id *id)
1447 struct es2_ap_dev *es2;
1448 struct gb_host_device *hd;
1449 struct usb_device *udev;
1450 struct usb_host_interface *iface_desc;
1451 struct usb_endpoint_descriptor *endpoint;
1458 udev = usb_get_dev(interface_to_usbdev(interface));
1460 num_cports = apb_get_cport_count(udev);
1461 if (num_cports < 0) {
1463 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1468 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
1475 es2 = hd_to_es2(hd);
1477 es2->usb_intf = interface;
1478 es2->usb_dev = udev;
1479 spin_lock_init(&es2->cport_out_urb_lock);
1480 INIT_KFIFO(es2->apb_log_fifo);
1481 usb_set_intfdata(interface, es2);
1484 * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
1487 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
1490 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
1494 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
1496 if (!es2->cport_to_ep) {
1501 /* find all bulk endpoints */
1502 iface_desc = interface->cur_altsetting;
1503 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1504 endpoint = &iface_desc->endpoint[i].desc;
1506 if (usb_endpoint_is_bulk_in(endpoint)) {
1507 if (bulk_in < NUM_BULKS)
1508 es2->cport_in[bulk_in].endpoint =
1509 endpoint->bEndpointAddress;
1511 es2->arpc_endpoint_in =
1512 endpoint->bEndpointAddress;
1514 } else if (usb_endpoint_is_bulk_out(endpoint)) {
1515 es2->cport_out[bulk_out++].endpoint =
1516 endpoint->bEndpointAddress;
1519 "Unknown endpoint type found, address 0x%02x\n",
1520 endpoint->bEndpointAddress);
1523 if (bulk_in != NUM_BULKS_IN || bulk_out != NUM_BULKS_OUT) {
1524 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1529 /* Allocate buffers for our cport in messages */
1530 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1531 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1533 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1537 urb = usb_alloc_urb(0, GFP_KERNEL);
1542 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1548 usb_fill_bulk_urb(urb, udev,
1549 usb_rcvbulkpipe(udev,
1550 cport_in->endpoint),
1551 buffer, ES2_GBUF_MSG_SIZE_MAX,
1552 cport_in_callback, hd);
1553 cport_in->urb[i] = urb;
1554 cport_in->buffer[i] = buffer;
1558 /* Allocate buffers for ARPC in messages */
1559 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
1563 urb = usb_alloc_urb(0, GFP_KERNEL);
1568 buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL);
1574 usb_fill_bulk_urb(urb, udev,
1575 usb_rcvbulkpipe(udev,
1576 es2->arpc_endpoint_in),
1577 buffer, ARPC_IN_SIZE_MAX,
1578 arpc_in_callback, es2);
1580 es2->arpc_urb[i] = urb;
1581 es2->arpc_buffer[i] = buffer;
1584 /* Allocate urbs for our CPort OUT messages */
1585 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1588 urb = usb_alloc_urb(0, GFP_KERNEL);
1594 es2->cport_out_urb[i] = urb;
1595 es2->cport_out_urb_busy[i] = false; /* just to be anal */
1598 /* XXX We will need to rename this per APB */
1599 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1600 (S_IWUSR | S_IRUGO),
1601 gb_debugfs_get(), es2,
1602 &apb_log_enable_fops);
1604 INIT_LIST_HEAD(&es2->arpcs);
1605 spin_lock_init(&es2->arpc_lock);
1607 if (es2_arpc_in_enable(es2))
1610 retval = gb_hd_add(hd);
1612 goto err_disable_arpc_in;
1614 for (i = 0; i < NUM_BULKS; ++i) {
1615 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1617 goto err_disable_cport_in;
1622 err_disable_cport_in:
1623 for (--i; i >= 0; --i)
1624 es2_cport_in_disable(es2, &es2->cport_in[i]);
1626 err_disable_arpc_in:
1627 es2_arpc_in_disable(es2);
1634 static void ap_disconnect(struct usb_interface *interface)
1636 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1641 for (i = 0; i < NUM_BULKS; ++i)
1642 es2_cport_in_disable(es2, &es2->cport_in[i]);
1643 es2_arpc_in_disable(es2);
1648 static struct usb_driver es2_ap_driver = {
1649 .name = "es2_ap_driver",
1651 .disconnect = ap_disconnect,
1652 .id_table = id_table,
1656 module_usb_driver(es2_ap_driver);
1658 MODULE_LICENSE("GPL v2");
1659 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");