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>
19 #include "greybus_trace.h"
20 #include "kernel_ver.h"
21 #include "connection.h"
24 /* Default timeout for USB vendor requests. */
25 #define ES2_USB_CTRL_TIMEOUT 500
27 /* Default timeout for ARPC CPort requests */
28 #define ES2_ARPC_CPORT_TIMEOUT 500
30 /* Fixed CPort numbers */
31 #define ES2_CPORT_CDSI0 16
32 #define ES2_CPORT_CDSI1 17
34 /* Memory sizes for the buffers sent to/from the ES2 controller */
35 #define ES2_GBUF_MSG_SIZE_MAX 2048
37 /* Memory sizes for the ARPC buffers */
38 #define ARPC_OUT_SIZE_MAX U16_MAX
39 #define ARPC_IN_SIZE_MAX 128
41 static const struct usb_device_id id_table[] = {
42 { USB_DEVICE(0x18d1, 0x1eaf) },
45 MODULE_DEVICE_TABLE(usb, id_table);
47 #define APB1_LOG_SIZE SZ_16K
49 /* Number of bulk in and bulk out couple */
52 /* Expected number of bulk out endpoints */
53 #define NUM_BULKS_OUT NUM_BULKS
55 /* Expected number of bulk in endpoints (including ARPC endpoint) */
56 #define NUM_BULKS_IN (NUM_BULKS + 1)
59 * Number of CPort IN urbs in flight at any point in time.
60 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
63 #define NUM_CPORT_IN_URB 4
65 /* Number of CPort OUT urbs in flight at any point in time.
66 * Adjust if we get messages saying we are out of urbs in the system log.
68 #define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
71 * Number of ARPC in urbs in flight at any point in time.
73 #define NUM_ARPC_IN_URB 2
76 * @endpoint: bulk in endpoint for CPort data
77 * @urb: array of urbs for the CPort in messages
78 * @buffer: array of buffers for the @cport_in_urb urbs
82 struct urb *urb[NUM_CPORT_IN_URB];
83 u8 *buffer[NUM_CPORT_IN_URB];
87 * @endpoint: bulk out endpoint for CPort data
89 struct es2_cport_out {
94 * es2_ap_dev - ES2 USB Bridge to AP structure
95 * @usb_dev: pointer to the USB device we are.
96 * @usb_intf: pointer to the USB interface we are bound to.
97 * @hd: pointer to our gb_host_device structure
99 * @cport_in: endpoint, urbs and buffer for cport in messages
100 * @cport_out: endpoint for for cport out messages
101 * @cport_out_urb: array of urbs for the CPort out messages
102 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
104 * @cport_out_urb_cancelled: array of flags indicating whether the
105 * corresponding @cport_out_urb is being cancelled
106 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
108 * @apb_log_task: task pointer for logging thread
109 * @apb_log_dentry: file system entry for the log file interface
110 * @apb_log_enable_dentry: file system entry for enabling logging
111 * @apb_log_fifo: kernel FIFO to carry logged data
112 * @arpc_urb: array of urbs for the ARPC in messages
113 * @arpc_buffer: array of buffers for the @arpc_urb urbs
114 * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC
115 * @arpc_id_cycle: gives an unique id to ARPC
116 * @arpc_lock: locks ARPC list
117 * @arpcs: list of in progress ARPCs
120 struct usb_device *usb_dev;
121 struct usb_interface *usb_intf;
122 struct gb_host_device *hd;
124 struct es2_cport_in cport_in[NUM_BULKS];
125 struct es2_cport_out cport_out[NUM_BULKS];
126 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
127 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
128 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
129 spinlock_t cport_out_urb_lock;
135 struct task_struct *apb_log_task;
136 struct dentry *apb_log_dentry;
137 struct dentry *apb_log_enable_dentry;
138 DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
140 __u8 arpc_endpoint_in;
141 struct urb *arpc_urb[NUM_ARPC_IN_URB];
142 u8 *arpc_buffer[NUM_ARPC_IN_URB];
145 spinlock_t arpc_lock;
146 struct list_head arpcs;
150 * cport_to_ep - information about cport to endpoints mapping
151 * @cport_id: the id of cport to map to endpoints
152 * @endpoint_in: the endpoint number to use for in transfer
153 * @endpoint_out: he endpoint number to use for out transfer
162 * timesync_enable_request - Enable timesync in an APBridge
163 * @count: number of TimeSync Pulses to expect
164 * @frame_time: the initial FrameTime at the first TimeSync Pulse
165 * @strobe_delay: the expected delay in microseconds between each TimeSync Pulse
166 * @refclk: The AP mandated reference clock to run FrameTime at
168 struct timesync_enable_request {
176 * timesync_authoritative_request - Transmit authoritative FrameTime to APBridge
177 * @frame_time: An array of authoritative FrameTimes provided by the SVC
178 * and relayed to the APBridge by the AP
180 struct timesync_authoritative_request {
181 __le64 frame_time[GB_TIMESYNC_MAX_STROBES];
185 struct list_head list;
186 struct arpc_request_message *req;
187 struct arpc_response_message *resp;
188 struct completion response_received;
192 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
194 return (struct es2_ap_dev *)&hd->hd_priv;
197 static void cport_out_callback(struct urb *urb);
198 static void usb_log_enable(struct es2_ap_dev *es2);
199 static void usb_log_disable(struct es2_ap_dev *es2);
200 static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
201 size_t size, int *result, unsigned int timeout);
203 /* Get the endpoints pair mapped to the cport */
204 static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id)
206 if (cport_id >= es2->hd->num_cports)
208 return es2->cport_to_ep[cport_id];
211 /* Disable for now until we work all of this out to keep a warning-free build */
213 /* Test if the endpoints pair is already mapped to a cport */
214 static int ep_pair_in_use(struct es2_ap_dev *es2, int ep_pair)
218 for (i = 0; i < es2->hd->num_cports; i++) {
219 if (es2->cport_to_ep[i] == ep_pair)
225 /* Configure the endpoint mapping and send the request to APBridge */
226 static int map_cport_to_ep(struct es2_ap_dev *es2,
227 u16 cport_id, int ep_pair)
230 struct cport_to_ep *cport_to_ep;
232 if (ep_pair < 0 || ep_pair >= NUM_BULKS)
234 if (cport_id >= es2->hd->num_cports)
236 if (ep_pair && ep_pair_in_use(es2, ep_pair))
239 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
243 es2->cport_to_ep[cport_id] = ep_pair;
244 cport_to_ep->cport_id = cpu_to_le16(cport_id);
245 cport_to_ep->endpoint_in = es2->cport_in[ep_pair].endpoint;
246 cport_to_ep->endpoint_out = es2->cport_out[ep_pair].endpoint;
248 retval = usb_control_msg(es2->usb_dev,
249 usb_sndctrlpipe(es2->usb_dev, 0),
250 GB_APB_REQUEST_EP_MAPPING,
251 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
254 sizeof(*cport_to_ep),
255 ES2_USB_CTRL_TIMEOUT);
256 if (retval == sizeof(*cport_to_ep))
263 /* Unmap a cport: use the muxed endpoints pair */
264 static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id)
266 return map_cport_to_ep(es2, cport_id, 0);
270 static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
272 struct usb_device *udev = es2->usb_dev;
276 data = kmalloc(size, GFP_KERNEL);
279 memcpy(data, req, size);
281 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
283 USB_DIR_OUT | USB_TYPE_VENDOR |
285 0, 0, data, size, ES2_USB_CTRL_TIMEOUT);
287 dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
295 static void ap_urb_complete(struct urb *urb)
297 struct usb_ctrlrequest *dr = urb->context;
303 static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
305 struct usb_device *udev = es2->usb_dev;
307 struct usb_ctrlrequest *dr;
311 urb = usb_alloc_urb(0, GFP_ATOMIC);
315 dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
321 buf = (u8 *)dr + sizeof(*dr);
322 memcpy(buf, req, size);
325 dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
328 dr->wLength = cpu_to_le16(size);
330 usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
331 (unsigned char *)dr, buf, size,
332 ap_urb_complete, dr);
333 retval = usb_submit_urb(urb, GFP_ATOMIC);
341 static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
344 struct es2_ap_dev *es2 = hd_to_es2(hd);
347 return output_async(es2, req, size, cmd);
349 return output_sync(es2, req, size, cmd);
352 static int es2_cport_in_enable(struct es2_ap_dev *es2,
353 struct es2_cport_in *cport_in)
359 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
360 urb = cport_in->urb[i];
362 ret = usb_submit_urb(urb, GFP_KERNEL);
364 dev_err(&es2->usb_dev->dev,
365 "failed to submit in-urb: %d\n", ret);
373 for (--i; i >= 0; --i) {
374 urb = cport_in->urb[i];
381 static void es2_cport_in_disable(struct es2_ap_dev *es2,
382 struct es2_cport_in *cport_in)
387 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
388 urb = cport_in->urb[i];
393 static int es2_arpc_in_enable(struct es2_ap_dev *es2)
399 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
400 urb = es2->arpc_urb[i];
402 ret = usb_submit_urb(urb, GFP_KERNEL);
404 dev_err(&es2->usb_dev->dev,
405 "failed to submit arpc in-urb: %d\n", ret);
413 for (--i; i >= 0; --i) {
414 urb = es2->arpc_urb[i];
421 static void es2_arpc_in_disable(struct es2_ap_dev *es2)
426 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
427 urb = es2->arpc_urb[i];
432 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
434 struct urb *urb = NULL;
438 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
440 /* Look in our pool of allocated urbs first, as that's the "fastest" */
441 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
442 if (es2->cport_out_urb_busy[i] == false &&
443 es2->cport_out_urb_cancelled[i] == false) {
444 es2->cport_out_urb_busy[i] = true;
445 urb = es2->cport_out_urb[i];
449 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
454 * Crap, pool is empty, complain to the syslog and go allocate one
455 * dynamically as we have to succeed.
457 dev_dbg(&es2->usb_dev->dev,
458 "No free CPort OUT urbs, having to dynamically allocate one!\n");
459 return usb_alloc_urb(0, gfp_mask);
462 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
467 * See if this was an urb in our pool, if so mark it "free", otherwise
468 * we need to free it ourselves.
470 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
471 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
472 if (urb == es2->cport_out_urb[i]) {
473 es2->cport_out_urb_busy[i] = false;
478 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
480 /* If urb is not NULL, then we need to free this urb */
485 * We (ab)use the operation-message header pad bytes to transfer the
486 * cport id in order to minimise overhead.
489 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
491 header->pad[0] = cport_id;
494 /* Clear the pad bytes used for the CPort id */
495 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
500 /* Extract the CPort id packed into the header, and clear it */
501 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
503 u16 cport_id = header->pad[0];
505 gb_message_cport_clear(header);
511 * Returns zero if the message was successfully queued, or a negative errno
514 static int message_send(struct gb_host_device *hd, u16 cport_id,
515 struct gb_message *message, gfp_t gfp_mask)
517 struct es2_ap_dev *es2 = hd_to_es2(hd);
518 struct usb_device *udev = es2->usb_dev;
526 * The data actually transferred will include an indication
527 * of where the data should be sent. Do one last check of
528 * the target CPort id before filling it in.
530 if (!cport_id_valid(hd, cport_id)) {
531 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
535 /* Find a free urb */
536 urb = next_free_urb(es2, gfp_mask);
540 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
541 message->hcpriv = urb;
542 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
544 /* Pack the cport id into the message header */
545 gb_message_cport_pack(message->header, cport_id);
547 buffer_size = sizeof(*message->header) + message->payload_size;
549 ep_pair = cport_to_ep_pair(es2, cport_id);
550 usb_fill_bulk_urb(urb, udev,
551 usb_sndbulkpipe(udev,
552 es2->cport_out[ep_pair].endpoint),
553 message->buffer, buffer_size,
554 cport_out_callback, message);
555 urb->transfer_flags |= URB_ZERO_PACKET;
557 trace_gb_message_submit(message);
559 retval = usb_submit_urb(urb, gfp_mask);
561 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
563 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
564 message->hcpriv = NULL;
565 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
568 gb_message_cport_clear(message->header);
577 * Can not be called in atomic context.
579 static void message_cancel(struct gb_message *message)
581 struct gb_host_device *hd = message->operation->connection->hd;
582 struct es2_ap_dev *es2 = hd_to_es2(hd);
588 spin_lock_irq(&es2->cport_out_urb_lock);
589 urb = message->hcpriv;
591 /* Prevent dynamically allocated urb from being deallocated. */
594 /* Prevent pre-allocated urb from being reused. */
595 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
596 if (urb == es2->cport_out_urb[i]) {
597 es2->cport_out_urb_cancelled[i] = true;
601 spin_unlock_irq(&es2->cport_out_urb_lock);
605 if (i < NUM_CPORT_OUT_URB) {
606 spin_lock_irq(&es2->cport_out_urb_lock);
607 es2->cport_out_urb_cancelled[i] = false;
608 spin_unlock_irq(&es2->cport_out_urb_lock);
614 static int cport_reset(struct gb_host_device *hd, u16 cport_id)
616 struct es2_ap_dev *es2 = hd_to_es2(hd);
617 struct usb_device *udev = es2->usb_dev;
618 struct arpc_cport_reset_req req;
623 case GB_SVC_CPORT_ID:
624 case ES2_CPORT_CDSI0:
625 case ES2_CPORT_CDSI1:
629 req.cport_id = cpu_to_le16(cport_id);
630 retval = arpc_sync(es2, ARPC_TYPE_CPORT_RESET, &req, sizeof(req),
631 &result, ES2_ARPC_CPORT_TIMEOUT);
632 if (retval == -EREMOTEIO) {
633 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
640 static int es2_cport_allocate(struct gb_host_device *hd, int cport_id,
643 struct es2_ap_dev *es2 = hd_to_es2(hd);
644 struct ida *id_map = &hd->cport_id_map;
645 int ida_start, ida_end;
648 case ES2_CPORT_CDSI0:
649 case ES2_CPORT_CDSI1:
650 dev_err(&hd->dev, "cport %d not available\n", cport_id);
654 if (flags & GB_CONNECTION_FLAG_OFFLOADED &&
655 flags & GB_CONNECTION_FLAG_CDSI1) {
656 if (es2->cdsi1_in_use) {
657 dev_err(&hd->dev, "CDSI1 already in use\n");
661 es2->cdsi1_in_use = true;
663 return ES2_CPORT_CDSI1;
668 ida_end = hd->num_cports;
669 } else if (cport_id < hd->num_cports) {
670 ida_start = cport_id;
671 ida_end = cport_id + 1;
673 dev_err(&hd->dev, "cport %d not available\n", cport_id);
677 return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
680 static void es2_cport_release(struct gb_host_device *hd, u16 cport_id)
682 struct es2_ap_dev *es2 = hd_to_es2(hd);
685 case ES2_CPORT_CDSI1:
686 es2->cdsi1_in_use = false;
690 ida_simple_remove(&hd->cport_id_map, cport_id);
693 static int cport_enable(struct gb_host_device *hd, u16 cport_id,
696 struct es2_ap_dev *es2 = hd_to_es2(hd);
697 struct usb_device *udev = es2->usb_dev;
698 struct gb_apb_request_cport_flags *req;
699 u32 connection_flags;
702 req = kzalloc(sizeof(*req), GFP_KERNEL);
706 connection_flags = 0;
707 if (flags & GB_CONNECTION_FLAG_CONTROL)
708 connection_flags |= GB_APB_CPORT_FLAG_CONTROL;
709 if (flags & GB_CONNECTION_FLAG_HIGH_PRIO)
710 connection_flags |= GB_APB_CPORT_FLAG_HIGH_PRIO;
712 req->flags = cpu_to_le32(connection_flags);
714 dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__,
715 cport_id, connection_flags);
717 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
718 GB_APB_REQUEST_CPORT_FLAGS,
719 USB_DIR_OUT | USB_TYPE_VENDOR |
720 USB_RECIP_INTERFACE, cport_id, 0,
721 req, sizeof(*req), ES2_USB_CTRL_TIMEOUT);
722 if (ret != sizeof(*req)) {
723 dev_err(&udev->dev, "failed to set cport flags for port %d\n",
738 static int cport_disable(struct gb_host_device *hd, u16 cport_id)
742 retval = cport_reset(hd, cport_id);
749 static int es2_cport_connected(struct gb_host_device *hd, u16 cport_id)
751 struct es2_ap_dev *es2 = hd_to_es2(hd);
752 struct device *dev = &es2->usb_dev->dev;
753 struct arpc_cport_connected_req req;
756 req.cport_id = cpu_to_le16(cport_id);
757 ret = arpc_sync(es2, ARPC_TYPE_CPORT_CONNECTED, &req, sizeof(req),
758 NULL, ES2_ARPC_CPORT_TIMEOUT);
760 dev_err(dev, "failed to set connected state for cport %u: %d\n",
768 static int es2_cport_quiesce(struct gb_host_device *hd, u16 cport_id,
769 size_t peer_space, unsigned int timeout)
771 struct es2_ap_dev *es2 = hd_to_es2(hd);
772 struct device *dev = &es2->usb_dev->dev;
773 struct arpc_cport_quiesce_req req;
777 if (peer_space > U16_MAX)
780 if (timeout > U16_MAX)
783 req.cport_id = cpu_to_le16(cport_id);
784 req.peer_space = cpu_to_le16(peer_space);
785 req.timeout = cpu_to_le16(timeout);
786 ret = arpc_sync(es2, ARPC_TYPE_CPORT_QUIESCE, &req, sizeof(req),
787 &result, ES2_ARPC_CPORT_TIMEOUT + timeout);
789 dev_err(dev, "failed to quiesce cport %u: %d (%d)\n",
790 cport_id, ret, result);
797 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
800 struct es2_ap_dev *es2 = hd_to_es2(hd);
801 struct usb_device *udev = es2->usb_dev;
803 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
804 GB_APB_REQUEST_LATENCY_TAG_EN,
805 USB_DIR_OUT | USB_TYPE_VENDOR |
806 USB_RECIP_INTERFACE, cport_id, 0, NULL,
807 0, ES2_USB_CTRL_TIMEOUT);
810 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
815 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
818 struct es2_ap_dev *es2 = hd_to_es2(hd);
819 struct usb_device *udev = es2->usb_dev;
821 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
822 GB_APB_REQUEST_LATENCY_TAG_DIS,
823 USB_DIR_OUT | USB_TYPE_VENDOR |
824 USB_RECIP_INTERFACE, cport_id, 0, NULL,
825 0, ES2_USB_CTRL_TIMEOUT);
828 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
833 static int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
836 struct es2_ap_dev *es2 = hd_to_es2(hd);
837 struct usb_device *udev = es2->usb_dev;
839 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
840 GB_APB_REQUEST_CPORT_FEAT_EN,
841 USB_DIR_OUT | USB_TYPE_VENDOR |
842 USB_RECIP_INTERFACE, cport_id, 0, NULL,
843 0, ES2_USB_CTRL_TIMEOUT);
845 dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
850 static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
853 struct es2_ap_dev *es2 = hd_to_es2(hd);
854 struct usb_device *udev = es2->usb_dev;
856 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
857 GB_APB_REQUEST_CPORT_FEAT_DIS,
858 USB_DIR_OUT | USB_TYPE_VENDOR |
859 USB_RECIP_INTERFACE, cport_id, 0, NULL,
860 0, ES2_USB_CTRL_TIMEOUT);
863 "Cannot disable CPort features for cport %u: %d\n",
868 static int timesync_enable(struct gb_host_device *hd, u8 count,
869 u64 frame_time, u32 strobe_delay, u32 refclk)
872 struct es2_ap_dev *es2 = hd_to_es2(hd);
873 struct usb_device *udev = es2->usb_dev;
874 struct gb_control_timesync_enable_request *request;
876 request = kzalloc(sizeof(*request), GFP_KERNEL);
880 request->count = count;
881 request->frame_time = cpu_to_le64(frame_time);
882 request->strobe_delay = cpu_to_le32(strobe_delay);
883 request->refclk = cpu_to_le32(refclk);
884 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
885 GB_APB_REQUEST_TIMESYNC_ENABLE,
886 USB_DIR_OUT | USB_TYPE_VENDOR |
887 USB_RECIP_INTERFACE, 0, 0, request,
888 sizeof(*request), ES2_USB_CTRL_TIMEOUT);
890 dev_err(&udev->dev, "Cannot enable timesync %d\n", retval);
896 static int timesync_disable(struct gb_host_device *hd)
899 struct es2_ap_dev *es2 = hd_to_es2(hd);
900 struct usb_device *udev = es2->usb_dev;
902 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
903 GB_APB_REQUEST_TIMESYNC_DISABLE,
904 USB_DIR_OUT | USB_TYPE_VENDOR |
905 USB_RECIP_INTERFACE, 0, 0, NULL,
906 0, ES2_USB_CTRL_TIMEOUT);
908 dev_err(&udev->dev, "Cannot disable timesync %d\n", retval);
913 static int timesync_authoritative(struct gb_host_device *hd, u64 *frame_time)
916 struct es2_ap_dev *es2 = hd_to_es2(hd);
917 struct usb_device *udev = es2->usb_dev;
918 struct timesync_authoritative_request *request;
920 request = kzalloc(sizeof(*request), GFP_KERNEL);
924 for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
925 request->frame_time[i] = cpu_to_le64(frame_time[i]);
927 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
928 GB_APB_REQUEST_TIMESYNC_AUTHORITATIVE,
929 USB_DIR_OUT | USB_TYPE_VENDOR |
930 USB_RECIP_INTERFACE, 0, 0, request,
931 sizeof(*request), ES2_USB_CTRL_TIMEOUT);
933 dev_err(&udev->dev, "Cannot timesync authoritative out %d\n", retval);
939 static int timesync_get_last_event(struct gb_host_device *hd, u64 *frame_time)
942 struct es2_ap_dev *es2 = hd_to_es2(hd);
943 struct usb_device *udev = es2->usb_dev;
944 __le64 *response_frame_time;
946 response_frame_time = kzalloc(sizeof(*response_frame_time), GFP_KERNEL);
947 if (!response_frame_time)
950 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
951 GB_APB_REQUEST_TIMESYNC_GET_LAST_EVENT,
952 USB_DIR_IN | USB_TYPE_VENDOR |
953 USB_RECIP_INTERFACE, 0, 0, response_frame_time,
954 sizeof(*response_frame_time),
955 ES2_USB_CTRL_TIMEOUT);
957 if (retval != sizeof(*response_frame_time)) {
958 dev_err(&udev->dev, "Cannot get last TimeSync event: %d\n",
966 *frame_time = le64_to_cpu(*response_frame_time);
969 kfree(response_frame_time);
973 static struct gb_hd_driver es2_driver = {
974 .hd_priv_size = sizeof(struct es2_ap_dev),
975 .message_send = message_send,
976 .message_cancel = message_cancel,
977 .cport_allocate = es2_cport_allocate,
978 .cport_release = es2_cport_release,
979 .cport_enable = cport_enable,
980 .cport_disable = cport_disable,
981 .cport_connected = es2_cport_connected,
982 .cport_quiesce = es2_cport_quiesce,
983 .latency_tag_enable = latency_tag_enable,
984 .latency_tag_disable = latency_tag_disable,
986 .cport_features_enable = cport_features_enable,
987 .cport_features_disable = cport_features_disable,
988 .timesync_enable = timesync_enable,
989 .timesync_disable = timesync_disable,
990 .timesync_authoritative = timesync_authoritative,
991 .timesync_get_last_event = timesync_get_last_event,
994 /* Common function to report consistent warnings based on URB status */
995 static int check_urb_status(struct urb *urb)
997 struct device *dev = &urb->dev->dev;
998 int status = urb->status;
1005 dev_err(dev, "%s: overflow actual length is %d\n",
1006 __func__, urb->actual_length);
1012 /* device is gone, stop sending */
1015 dev_err(dev, "%s: unknown status %d\n", __func__, status);
1020 static void es2_destroy(struct es2_ap_dev *es2)
1022 struct usb_device *udev;
1026 debugfs_remove(es2->apb_log_enable_dentry);
1027 usb_log_disable(es2);
1029 /* Tear down everything! */
1030 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1031 struct urb *urb = es2->cport_out_urb[i];
1037 es2->cport_out_urb[i] = NULL;
1038 es2->cport_out_urb_busy[i] = false; /* just to be anal */
1041 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
1042 struct urb *urb = es2->arpc_urb[i];
1047 kfree(es2->arpc_buffer[i]);
1048 es2->arpc_buffer[i] = NULL;
1051 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1052 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1054 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1055 struct urb *urb = cport_in->urb[i];
1060 kfree(cport_in->buffer[i]);
1061 cport_in->buffer[i] = NULL;
1065 kfree(es2->cport_to_ep);
1067 /* release reserved CDSI0 and CDSI1 cports */
1068 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
1069 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);
1071 udev = es2->usb_dev;
1077 static void cport_in_callback(struct urb *urb)
1079 struct gb_host_device *hd = urb->context;
1080 struct device *dev = &urb->dev->dev;
1081 struct gb_operation_msg_hdr *header;
1082 int status = check_urb_status(urb);
1087 if ((status == -EAGAIN) || (status == -EPROTO))
1090 /* The urb is being unlinked */
1091 if (status == -ENOENT || status == -ESHUTDOWN)
1094 dev_err(dev, "urb cport in error %d (dropped)\n", status);
1098 if (urb->actual_length < sizeof(*header)) {
1099 dev_err(dev, "short message received\n");
1103 /* Extract the CPort id, which is packed in the message header */
1104 header = urb->transfer_buffer;
1105 cport_id = gb_message_cport_unpack(header);
1107 if (cport_id_valid(hd, cport_id)) {
1108 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
1109 urb->actual_length);
1111 dev_err(dev, "invalid cport id %u received\n", cport_id);
1114 /* put our urb back in the request pool */
1115 retval = usb_submit_urb(urb, GFP_ATOMIC);
1117 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
1120 static void cport_out_callback(struct urb *urb)
1122 struct gb_message *message = urb->context;
1123 struct gb_host_device *hd = message->operation->connection->hd;
1124 struct es2_ap_dev *es2 = hd_to_es2(hd);
1125 int status = check_urb_status(urb);
1126 unsigned long flags;
1128 gb_message_cport_clear(message->header);
1130 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
1131 message->hcpriv = NULL;
1132 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
1135 * Tell the submitter that the message send (attempt) is
1136 * complete, and report the status.
1138 greybus_message_sent(hd, message, status);
1143 static struct arpc *arpc_alloc(void *payload, u16 size, u8 type)
1147 if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX)
1150 rpc = kzalloc(sizeof(*rpc), GFP_KERNEL);
1154 INIT_LIST_HEAD(&rpc->list);
1155 rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL);
1159 rpc->resp = kzalloc(sizeof(*rpc->resp), GFP_KERNEL);
1163 rpc->req->type = type;
1164 rpc->req->size = cpu_to_le16(sizeof(rpc->req) + size);
1165 memcpy(rpc->req->data, payload, size);
1167 init_completion(&rpc->response_received);
1179 static void arpc_free(struct arpc *rpc)
1186 static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id)
1190 list_for_each_entry(rpc, &es2->arpcs, list) {
1191 if (rpc->req->id == id)
1198 static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc)
1201 rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++);
1202 list_add_tail(&rpc->list, &es2->arpcs);
1205 static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc)
1208 rpc->active = false;
1209 list_del(&rpc->list);
1213 static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout)
1215 struct usb_device *udev = es2->usb_dev;
1218 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1219 GB_APB_REQUEST_ARPC_RUN,
1220 USB_DIR_OUT | USB_TYPE_VENDOR |
1221 USB_RECIP_INTERFACE,
1223 rpc->req, le16_to_cpu(rpc->req->size),
1224 ES2_USB_CTRL_TIMEOUT);
1225 if (retval != le16_to_cpu(rpc->req->size)) {
1227 "failed to send ARPC request %d: %d\n",
1228 rpc->req->type, retval);
1237 static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
1238 size_t size, int *result, unsigned int timeout)
1241 unsigned long flags;
1247 rpc = arpc_alloc(payload, size, type);
1251 spin_lock_irqsave(&es2->arpc_lock, flags);
1253 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1255 retval = arpc_send(es2, rpc, timeout);
1259 retval = wait_for_completion_interruptible_timeout(
1260 &rpc->response_received,
1261 msecs_to_jiffies(timeout));
1264 retval = -ETIMEDOUT;
1268 if (rpc->resp->result) {
1269 retval = -EREMOTEIO;
1271 *result = rpc->resp->result;
1277 spin_lock_irqsave(&es2->arpc_lock, flags);
1279 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1282 if (retval < 0 && retval != -EREMOTEIO) {
1283 dev_err(&es2->usb_dev->dev,
1284 "failed to execute ARPC: %d\n", retval);
1290 static void arpc_in_callback(struct urb *urb)
1292 struct es2_ap_dev *es2 = urb->context;
1293 struct device *dev = &urb->dev->dev;
1294 int status = check_urb_status(urb);
1296 struct arpc_response_message *resp;
1297 unsigned long flags;
1301 if ((status == -EAGAIN) || (status == -EPROTO))
1304 /* The urb is being unlinked */
1305 if (status == -ENOENT || status == -ESHUTDOWN)
1308 dev_err(dev, "arpc in-urb error %d (dropped)\n", status);
1312 if (urb->actual_length < sizeof(*resp)) {
1313 dev_err(dev, "short aprc response received\n");
1317 resp = urb->transfer_buffer;
1318 spin_lock_irqsave(&es2->arpc_lock, flags);
1319 rpc = arpc_find(es2, resp->id);
1321 dev_err(dev, "invalid arpc response id received: %u\n",
1322 le16_to_cpu(resp->id));
1323 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1328 memcpy(rpc->resp, resp, sizeof(*resp));
1329 complete(&rpc->response_received);
1330 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1333 /* put our urb back in the request pool */
1334 retval = usb_submit_urb(urb, GFP_ATOMIC);
1336 dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval);
1339 #define APB1_LOG_MSG_SIZE 64
1340 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
1345 retval = usb_control_msg(es2->usb_dev,
1346 usb_rcvctrlpipe(es2->usb_dev, 0),
1348 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1352 ES2_USB_CTRL_TIMEOUT);
1354 kfifo_in(&es2->apb_log_fifo, buf, retval);
1355 } while (retval > 0);
1358 static int apb_log_poll(void *data)
1360 struct es2_ap_dev *es2 = data;
1363 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
1367 while (!kthread_should_stop()) {
1369 apb_log_get(es2, buf);
1377 static ssize_t apb_log_read(struct file *f, char __user *buf,
1378 size_t count, loff_t *ppos)
1380 struct es2_ap_dev *es2 = f->f_inode->i_private;
1385 if (count > APB1_LOG_SIZE)
1386 count = APB1_LOG_SIZE;
1388 tmp_buf = kmalloc(count, GFP_KERNEL);
1392 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
1393 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
1400 static const struct file_operations apb_log_fops = {
1401 .read = apb_log_read,
1404 static void usb_log_enable(struct es2_ap_dev *es2)
1406 if (!IS_ERR_OR_NULL(es2->apb_log_task))
1409 /* get log from APB1 */
1410 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
1411 if (IS_ERR(es2->apb_log_task))
1413 /* XXX We will need to rename this per APB */
1414 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
1415 gb_debugfs_get(), es2,
1419 static void usb_log_disable(struct es2_ap_dev *es2)
1421 if (IS_ERR_OR_NULL(es2->apb_log_task))
1424 debugfs_remove(es2->apb_log_dentry);
1425 es2->apb_log_dentry = NULL;
1427 kthread_stop(es2->apb_log_task);
1428 es2->apb_log_task = NULL;
1431 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
1432 size_t count, loff_t *ppos)
1434 struct es2_ap_dev *es2 = f->f_inode->i_private;
1435 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
1438 sprintf(tmp_buf, "%d\n", enable);
1439 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
1442 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
1443 size_t count, loff_t *ppos)
1447 struct es2_ap_dev *es2 = f->f_inode->i_private;
1449 retval = kstrtoint_from_user(buf, count, 10, &enable);
1454 usb_log_enable(es2);
1456 usb_log_disable(es2);
1461 static const struct file_operations apb_log_enable_fops = {
1462 .read = apb_log_enable_read,
1463 .write = apb_log_enable_write,
1466 static int apb_get_cport_count(struct usb_device *udev)
1469 __le16 *cport_count;
1471 cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
1475 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1476 GB_APB_REQUEST_CPORT_COUNT,
1477 USB_DIR_IN | USB_TYPE_VENDOR |
1478 USB_RECIP_INTERFACE, 0, 0, cport_count,
1479 sizeof(*cport_count), ES2_USB_CTRL_TIMEOUT);
1480 if (retval != sizeof(*cport_count)) {
1481 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1490 retval = le16_to_cpu(*cport_count);
1492 /* We need to fit a CPort ID in one byte of a message header */
1493 if (retval > U8_MAX) {
1495 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
1504 * The ES2 USB Bridge device has 15 endpoints
1505 * 1 Control - usual USB stuff + AP -> APBridgeA messages
1506 * 7 Bulk IN - CPort data in
1507 * 7 Bulk OUT - CPort data out
1509 static int ap_probe(struct usb_interface *interface,
1510 const struct usb_device_id *id)
1512 struct es2_ap_dev *es2;
1513 struct gb_host_device *hd;
1514 struct usb_device *udev;
1515 struct usb_host_interface *iface_desc;
1516 struct usb_endpoint_descriptor *endpoint;
1523 udev = usb_get_dev(interface_to_usbdev(interface));
1525 num_cports = apb_get_cport_count(udev);
1526 if (num_cports < 0) {
1528 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1533 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
1540 es2 = hd_to_es2(hd);
1542 es2->usb_intf = interface;
1543 es2->usb_dev = udev;
1544 spin_lock_init(&es2->cport_out_urb_lock);
1545 INIT_KFIFO(es2->apb_log_fifo);
1546 usb_set_intfdata(interface, es2);
1549 * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
1552 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
1555 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
1559 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
1561 if (!es2->cport_to_ep) {
1566 /* find all bulk endpoints */
1567 iface_desc = interface->cur_altsetting;
1568 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1569 endpoint = &iface_desc->endpoint[i].desc;
1571 if (usb_endpoint_is_bulk_in(endpoint)) {
1572 if (bulk_in < NUM_BULKS)
1573 es2->cport_in[bulk_in].endpoint =
1574 endpoint->bEndpointAddress;
1576 es2->arpc_endpoint_in =
1577 endpoint->bEndpointAddress;
1579 } else if (usb_endpoint_is_bulk_out(endpoint)) {
1580 es2->cport_out[bulk_out++].endpoint =
1581 endpoint->bEndpointAddress;
1584 "Unknown endpoint type found, address 0x%02x\n",
1585 endpoint->bEndpointAddress);
1588 if (bulk_in != NUM_BULKS_IN || bulk_out != NUM_BULKS_OUT) {
1589 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1594 /* Allocate buffers for our cport in messages */
1595 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1596 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1598 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1602 urb = usb_alloc_urb(0, GFP_KERNEL);
1607 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1613 usb_fill_bulk_urb(urb, udev,
1614 usb_rcvbulkpipe(udev,
1615 cport_in->endpoint),
1616 buffer, ES2_GBUF_MSG_SIZE_MAX,
1617 cport_in_callback, hd);
1618 cport_in->urb[i] = urb;
1619 cport_in->buffer[i] = buffer;
1623 /* Allocate buffers for ARPC in messages */
1624 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
1628 urb = usb_alloc_urb(0, GFP_KERNEL);
1633 buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL);
1639 usb_fill_bulk_urb(urb, udev,
1640 usb_rcvbulkpipe(udev,
1641 es2->arpc_endpoint_in),
1642 buffer, ARPC_IN_SIZE_MAX,
1643 arpc_in_callback, es2);
1645 es2->arpc_urb[i] = urb;
1646 es2->arpc_buffer[i] = buffer;
1649 /* Allocate urbs for our CPort OUT messages */
1650 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1653 urb = usb_alloc_urb(0, GFP_KERNEL);
1659 es2->cport_out_urb[i] = urb;
1660 es2->cport_out_urb_busy[i] = false; /* just to be anal */
1663 /* XXX We will need to rename this per APB */
1664 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1665 (S_IWUSR | S_IRUGO),
1666 gb_debugfs_get(), es2,
1667 &apb_log_enable_fops);
1669 INIT_LIST_HEAD(&es2->arpcs);
1670 spin_lock_init(&es2->arpc_lock);
1672 if (es2_arpc_in_enable(es2))
1675 retval = gb_hd_add(hd);
1677 goto err_disable_arpc_in;
1679 for (i = 0; i < NUM_BULKS; ++i) {
1680 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1682 goto err_disable_cport_in;
1687 err_disable_cport_in:
1688 for (--i; i >= 0; --i)
1689 es2_cport_in_disable(es2, &es2->cport_in[i]);
1691 err_disable_arpc_in:
1692 es2_arpc_in_disable(es2);
1699 static void ap_disconnect(struct usb_interface *interface)
1701 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1706 for (i = 0; i < NUM_BULKS; ++i)
1707 es2_cport_in_disable(es2, &es2->cport_in[i]);
1708 es2_arpc_in_disable(es2);
1713 static struct usb_driver es2_ap_driver = {
1714 .name = "es2_ap_driver",
1716 .disconnect = ap_disconnect,
1717 .id_table = id_table,
1721 module_usb_driver(es2_ap_driver);
1723 MODULE_LICENSE("GPL v2");
1724 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");