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"
23 /* Default timeout for ARPC CPort requests */
24 #define ES2_ARPC_CPORT_TIMEOUT 500
26 /* Fixed CPort numbers */
27 #define ES2_CPORT_CDSI0 16
28 #define ES2_CPORT_CDSI1 17
30 /* Memory sizes for the buffers sent to/from the ES2 controller */
31 #define ES2_GBUF_MSG_SIZE_MAX 2048
33 /* Memory sizes for the ARPC buffers */
34 #define ARPC_OUT_SIZE_MAX U16_MAX
35 #define ARPC_IN_SIZE_MAX 128
37 static const struct usb_device_id id_table[] = {
38 { USB_DEVICE(0x18d1, 0x1eaf) },
41 MODULE_DEVICE_TABLE(usb, id_table);
43 #define APB1_LOG_SIZE SZ_16K
45 /* Number of bulk in and bulk out couple */
48 /* Expected number of bulk out endpoints */
49 #define NUM_BULKS_OUT NUM_BULKS
51 /* Expected number of bulk in endpoints (including ARPC endpoint) */
52 #define NUM_BULKS_IN (NUM_BULKS + 1)
55 * Number of CPort IN urbs in flight at any point in time.
56 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
59 #define NUM_CPORT_IN_URB 4
61 /* Number of CPort OUT urbs in flight at any point in time.
62 * Adjust if we get messages saying we are out of urbs in the system log.
64 #define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
67 * Number of ARPC in urbs in flight at any point in time.
69 #define NUM_ARPC_IN_URB 2
72 * @endpoint: bulk in endpoint for CPort data
73 * @urb: array of urbs for the CPort in messages
74 * @buffer: array of buffers for the @cport_in_urb urbs
78 struct urb *urb[NUM_CPORT_IN_URB];
79 u8 *buffer[NUM_CPORT_IN_URB];
83 * @endpoint: bulk out endpoint for CPort data
85 struct es2_cport_out {
90 * es2_ap_dev - ES2 USB Bridge to AP structure
91 * @usb_dev: pointer to the USB device we are.
92 * @usb_intf: pointer to the USB interface we are bound to.
93 * @hd: pointer to our gb_host_device structure
95 * @cport_in: endpoint, urbs and buffer for cport in messages
96 * @cport_out: endpoint for for cport out messages
97 * @cport_out_urb: array of urbs for the CPort out messages
98 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
100 * @cport_out_urb_cancelled: array of flags indicating whether the
101 * corresponding @cport_out_urb is being cancelled
102 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
104 * @apb_log_task: task pointer for logging thread
105 * @apb_log_dentry: file system entry for the log file interface
106 * @apb_log_enable_dentry: file system entry for enabling logging
107 * @apb_log_fifo: kernel FIFO to carry logged data
108 * @arpc_urb: array of urbs for the ARPC in messages
109 * @arpc_buffer: array of buffers for the @arpc_urb urbs
110 * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC
111 * @arpc_id_cycle: gives an unique id to ARPC
112 * @arpc_lock: locks ARPC list
113 * @arpcs: list of in progress ARPCs
116 struct usb_device *usb_dev;
117 struct usb_interface *usb_intf;
118 struct gb_host_device *hd;
120 struct es2_cport_in cport_in[NUM_BULKS];
121 struct es2_cport_out cport_out[NUM_BULKS];
122 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
123 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
124 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
125 spinlock_t cport_out_urb_lock;
131 struct task_struct *apb_log_task;
132 struct dentry *apb_log_dentry;
133 struct dentry *apb_log_enable_dentry;
134 DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
136 __u8 arpc_endpoint_in;
137 struct urb *arpc_urb[NUM_ARPC_IN_URB];
138 u8 *arpc_buffer[NUM_ARPC_IN_URB];
141 spinlock_t arpc_lock;
142 struct list_head arpcs;
146 * cport_to_ep - information about cport to endpoints mapping
147 * @cport_id: the id of cport to map to endpoints
148 * @endpoint_in: the endpoint number to use for in transfer
149 * @endpoint_out: he endpoint number to use for out transfer
158 * timesync_enable_request - Enable timesync in an APBridge
159 * @count: number of TimeSync Pulses to expect
160 * @frame_time: the initial FrameTime at the first TimeSync Pulse
161 * @strobe_delay: the expected delay in microseconds between each TimeSync Pulse
162 * @refclk: The AP mandated reference clock to run FrameTime at
164 struct timesync_enable_request {
172 * timesync_authoritative_request - Transmit authoritative FrameTime to APBridge
173 * @frame_time: An array of authoritative FrameTimes provided by the SVC
174 * and relayed to the APBridge by the AP
176 struct timesync_authoritative_request {
177 __le64 frame_time[GB_TIMESYNC_MAX_STROBES];
181 struct list_head list;
182 struct arpc_request_message *req;
183 struct arpc_response_message *resp;
184 struct completion response_received;
188 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
190 return (struct es2_ap_dev *)&hd->hd_priv;
193 static void cport_out_callback(struct urb *urb);
194 static void usb_log_enable(struct es2_ap_dev *es2);
195 static void usb_log_disable(struct es2_ap_dev *es2);
196 static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
197 size_t size, int *result, unsigned int timeout);
199 /* Get the endpoints pair mapped to the cport */
200 static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id)
202 if (cport_id >= es2->hd->num_cports)
204 return es2->cport_to_ep[cport_id];
207 #define ES2_TIMEOUT 500 /* 500 ms for the SVC to do something */
209 /* Disable for now until we work all of this out to keep a warning-free build */
211 /* Test if the endpoints pair is already mapped to a cport */
212 static int ep_pair_in_use(struct es2_ap_dev *es2, int ep_pair)
216 for (i = 0; i < es2->hd->num_cports; i++) {
217 if (es2->cport_to_ep[i] == ep_pair)
223 /* Configure the endpoint mapping and send the request to APBridge */
224 static int map_cport_to_ep(struct es2_ap_dev *es2,
225 u16 cport_id, int ep_pair)
228 struct cport_to_ep *cport_to_ep;
230 if (ep_pair < 0 || ep_pair >= NUM_BULKS)
232 if (cport_id >= es2->hd->num_cports)
234 if (ep_pair && ep_pair_in_use(es2, ep_pair))
237 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
241 es2->cport_to_ep[cport_id] = ep_pair;
242 cport_to_ep->cport_id = cpu_to_le16(cport_id);
243 cport_to_ep->endpoint_in = es2->cport_in[ep_pair].endpoint;
244 cport_to_ep->endpoint_out = es2->cport_out[ep_pair].endpoint;
246 retval = usb_control_msg(es2->usb_dev,
247 usb_sndctrlpipe(es2->usb_dev, 0),
248 GB_APB_REQUEST_EP_MAPPING,
249 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
252 sizeof(*cport_to_ep),
254 if (retval == sizeof(*cport_to_ep))
261 /* Unmap a cport: use the muxed endpoints pair */
262 static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id)
264 return map_cport_to_ep(es2, cport_id, 0);
268 static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
270 struct usb_device *udev = es2->usb_dev;
274 data = kmalloc(size, GFP_KERNEL);
277 memcpy(data, req, size);
279 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
281 USB_DIR_OUT | USB_TYPE_VENDOR |
283 0, 0, data, size, ES2_TIMEOUT);
285 dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
293 static void ap_urb_complete(struct urb *urb)
295 struct usb_ctrlrequest *dr = urb->context;
301 static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
303 struct usb_device *udev = es2->usb_dev;
305 struct usb_ctrlrequest *dr;
309 urb = usb_alloc_urb(0, GFP_ATOMIC);
313 dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
319 buf = (u8 *)dr + sizeof(*dr);
320 memcpy(buf, req, size);
323 dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
326 dr->wLength = cpu_to_le16(size);
328 usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
329 (unsigned char *)dr, buf, size,
330 ap_urb_complete, dr);
331 retval = usb_submit_urb(urb, GFP_ATOMIC);
339 static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
342 struct es2_ap_dev *es2 = hd_to_es2(hd);
345 return output_async(es2, req, size, cmd);
347 return output_sync(es2, req, size, cmd);
350 static int es2_cport_in_enable(struct es2_ap_dev *es2,
351 struct es2_cport_in *cport_in)
357 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
358 urb = cport_in->urb[i];
360 ret = usb_submit_urb(urb, GFP_KERNEL);
362 dev_err(&es2->usb_dev->dev,
363 "failed to submit in-urb: %d\n", ret);
371 for (--i; i >= 0; --i) {
372 urb = cport_in->urb[i];
379 static void es2_cport_in_disable(struct es2_ap_dev *es2,
380 struct es2_cport_in *cport_in)
385 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
386 urb = cport_in->urb[i];
391 static int es2_arpc_in_enable(struct es2_ap_dev *es2)
397 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
398 urb = es2->arpc_urb[i];
400 ret = usb_submit_urb(urb, GFP_KERNEL);
402 dev_err(&es2->usb_dev->dev,
403 "failed to submit arpc in-urb: %d\n", ret);
411 for (--i; i >= 0; --i) {
412 urb = es2->arpc_urb[i];
419 static void es2_arpc_in_disable(struct es2_ap_dev *es2)
424 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
425 urb = es2->arpc_urb[i];
430 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
432 struct urb *urb = NULL;
436 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
438 /* Look in our pool of allocated urbs first, as that's the "fastest" */
439 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
440 if (es2->cport_out_urb_busy[i] == false &&
441 es2->cport_out_urb_cancelled[i] == false) {
442 es2->cport_out_urb_busy[i] = true;
443 urb = es2->cport_out_urb[i];
447 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
452 * Crap, pool is empty, complain to the syslog and go allocate one
453 * dynamically as we have to succeed.
455 dev_dbg(&es2->usb_dev->dev,
456 "No free CPort OUT urbs, having to dynamically allocate one!\n");
457 return usb_alloc_urb(0, gfp_mask);
460 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
465 * See if this was an urb in our pool, if so mark it "free", otherwise
466 * we need to free it ourselves.
468 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
469 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
470 if (urb == es2->cport_out_urb[i]) {
471 es2->cport_out_urb_busy[i] = false;
476 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
478 /* If urb is not NULL, then we need to free this urb */
483 * We (ab)use the operation-message header pad bytes to transfer the
484 * cport id in order to minimise overhead.
487 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
489 header->pad[0] = cport_id;
492 /* Clear the pad bytes used for the CPort id */
493 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
498 /* Extract the CPort id packed into the header, and clear it */
499 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
501 u16 cport_id = header->pad[0];
503 gb_message_cport_clear(header);
509 * Returns zero if the message was successfully queued, or a negative errno
512 static int message_send(struct gb_host_device *hd, u16 cport_id,
513 struct gb_message *message, gfp_t gfp_mask)
515 struct es2_ap_dev *es2 = hd_to_es2(hd);
516 struct usb_device *udev = es2->usb_dev;
524 * The data actually transferred will include an indication
525 * of where the data should be sent. Do one last check of
526 * the target CPort id before filling it in.
528 if (!cport_id_valid(hd, cport_id)) {
529 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
533 /* Find a free urb */
534 urb = next_free_urb(es2, gfp_mask);
538 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
539 message->hcpriv = urb;
540 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
542 /* Pack the cport id into the message header */
543 gb_message_cport_pack(message->header, cport_id);
545 buffer_size = sizeof(*message->header) + message->payload_size;
547 ep_pair = cport_to_ep_pair(es2, cport_id);
548 usb_fill_bulk_urb(urb, udev,
549 usb_sndbulkpipe(udev,
550 es2->cport_out[ep_pair].endpoint),
551 message->buffer, buffer_size,
552 cport_out_callback, message);
553 urb->transfer_flags |= URB_ZERO_PACKET;
555 trace_gb_message_submit(message);
557 retval = usb_submit_urb(urb, gfp_mask);
559 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
561 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
562 message->hcpriv = NULL;
563 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
566 gb_message_cport_clear(message->header);
575 * Can not be called in atomic context.
577 static void message_cancel(struct gb_message *message)
579 struct gb_host_device *hd = message->operation->connection->hd;
580 struct es2_ap_dev *es2 = hd_to_es2(hd);
586 spin_lock_irq(&es2->cport_out_urb_lock);
587 urb = message->hcpriv;
589 /* Prevent dynamically allocated urb from being deallocated. */
592 /* Prevent pre-allocated urb from being reused. */
593 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
594 if (urb == es2->cport_out_urb[i]) {
595 es2->cport_out_urb_cancelled[i] = true;
599 spin_unlock_irq(&es2->cport_out_urb_lock);
603 if (i < NUM_CPORT_OUT_URB) {
604 spin_lock_irq(&es2->cport_out_urb_lock);
605 es2->cport_out_urb_cancelled[i] = false;
606 spin_unlock_irq(&es2->cport_out_urb_lock);
612 static int cport_reset(struct gb_host_device *hd, u16 cport_id)
614 struct es2_ap_dev *es2 = hd_to_es2(hd);
615 struct usb_device *udev = es2->usb_dev;
616 struct arpc_cport_reset_req req;
621 case GB_SVC_CPORT_ID:
622 case ES2_CPORT_CDSI0:
623 case ES2_CPORT_CDSI1:
627 req.cport_id = cpu_to_le16(cport_id);
628 retval = arpc_sync(es2, ARPC_TYPE_CPORT_RESET, &req, sizeof(req),
629 &result, ES2_ARPC_CPORT_TIMEOUT);
630 if (retval == -EREMOTEIO) {
631 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
638 static int es2_cport_allocate(struct gb_host_device *hd, int cport_id,
641 struct es2_ap_dev *es2 = hd_to_es2(hd);
642 struct ida *id_map = &hd->cport_id_map;
643 int ida_start, ida_end;
646 case ES2_CPORT_CDSI0:
647 case ES2_CPORT_CDSI1:
648 dev_err(&hd->dev, "cport %d not available\n", cport_id);
652 if (flags & GB_CONNECTION_FLAG_OFFLOADED &&
653 flags & GB_CONNECTION_FLAG_CDSI1) {
654 if (es2->cdsi1_in_use) {
655 dev_err(&hd->dev, "CDSI1 already in use\n");
659 es2->cdsi1_in_use = true;
661 return ES2_CPORT_CDSI1;
666 ida_end = hd->num_cports;
667 } else if (cport_id < hd->num_cports) {
668 ida_start = cport_id;
669 ida_end = cport_id + 1;
671 dev_err(&hd->dev, "cport %d not available\n", cport_id);
675 return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
678 static void es2_cport_release(struct gb_host_device *hd, u16 cport_id)
680 struct es2_ap_dev *es2 = hd_to_es2(hd);
683 case ES2_CPORT_CDSI1:
684 es2->cdsi1_in_use = false;
688 ida_simple_remove(&hd->cport_id_map, cport_id);
691 static int cport_enable(struct gb_host_device *hd, u16 cport_id,
694 struct es2_ap_dev *es2 = hd_to_es2(hd);
695 struct usb_device *udev = es2->usb_dev;
696 struct gb_apb_request_cport_flags *req;
697 u32 connection_flags;
700 req = kzalloc(sizeof(*req), GFP_KERNEL);
704 connection_flags = 0;
705 if (flags & GB_CONNECTION_FLAG_CONTROL)
706 connection_flags |= GB_APB_CPORT_FLAG_CONTROL;
707 if (flags & GB_CONNECTION_FLAG_HIGH_PRIO)
708 connection_flags |= GB_APB_CPORT_FLAG_HIGH_PRIO;
710 req->flags = cpu_to_le32(connection_flags);
712 dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__,
713 cport_id, connection_flags);
715 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
716 GB_APB_REQUEST_CPORT_FLAGS,
717 USB_DIR_OUT | USB_TYPE_VENDOR |
718 USB_RECIP_INTERFACE, cport_id, 0,
719 req, sizeof(*req), ES2_TIMEOUT);
720 if (ret != sizeof(*req)) {
721 dev_err(&udev->dev, "failed to set cport flags for port %d\n",
736 static int cport_disable(struct gb_host_device *hd, u16 cport_id)
740 retval = cport_reset(hd, cport_id);
747 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
750 struct es2_ap_dev *es2 = hd_to_es2(hd);
751 struct usb_device *udev = es2->usb_dev;
753 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
754 GB_APB_REQUEST_LATENCY_TAG_EN,
755 USB_DIR_OUT | USB_TYPE_VENDOR |
756 USB_RECIP_INTERFACE, cport_id, 0, NULL,
760 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
765 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
768 struct es2_ap_dev *es2 = hd_to_es2(hd);
769 struct usb_device *udev = es2->usb_dev;
771 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
772 GB_APB_REQUEST_LATENCY_TAG_DIS,
773 USB_DIR_OUT | USB_TYPE_VENDOR |
774 USB_RECIP_INTERFACE, cport_id, 0, NULL,
778 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
783 static int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
786 struct es2_ap_dev *es2 = hd_to_es2(hd);
787 struct usb_device *udev = es2->usb_dev;
789 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
790 GB_APB_REQUEST_CPORT_FEAT_EN,
791 USB_DIR_OUT | USB_TYPE_VENDOR |
792 USB_RECIP_INTERFACE, cport_id, 0, NULL,
795 dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
800 static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
803 struct es2_ap_dev *es2 = hd_to_es2(hd);
804 struct usb_device *udev = es2->usb_dev;
806 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
807 GB_APB_REQUEST_CPORT_FEAT_DIS,
808 USB_DIR_OUT | USB_TYPE_VENDOR |
809 USB_RECIP_INTERFACE, cport_id, 0, NULL,
813 "Cannot disable CPort features for cport %u: %d\n",
818 static int timesync_enable(struct gb_host_device *hd, u8 count,
819 u64 frame_time, u32 strobe_delay, u32 refclk)
822 struct es2_ap_dev *es2 = hd_to_es2(hd);
823 struct usb_device *udev = es2->usb_dev;
824 struct gb_control_timesync_enable_request *request;
826 request = kzalloc(sizeof(*request), GFP_KERNEL);
830 request->count = count;
831 request->frame_time = cpu_to_le64(frame_time);
832 request->strobe_delay = cpu_to_le32(strobe_delay);
833 request->refclk = cpu_to_le32(refclk);
834 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
835 REQUEST_TIMESYNC_ENABLE,
836 USB_DIR_OUT | USB_TYPE_VENDOR |
837 USB_RECIP_INTERFACE, 0, 0, request,
838 sizeof(*request), ES2_TIMEOUT);
840 dev_err(&udev->dev, "Cannot enable timesync %d\n", retval);
846 static int timesync_disable(struct gb_host_device *hd)
849 struct es2_ap_dev *es2 = hd_to_es2(hd);
850 struct usb_device *udev = es2->usb_dev;
852 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
853 REQUEST_TIMESYNC_DISABLE,
854 USB_DIR_OUT | USB_TYPE_VENDOR |
855 USB_RECIP_INTERFACE, 0, 0, NULL,
858 dev_err(&udev->dev, "Cannot disable timesync %d\n", retval);
863 static int timesync_authoritative(struct gb_host_device *hd, u64 *frame_time)
866 struct es2_ap_dev *es2 = hd_to_es2(hd);
867 struct usb_device *udev = es2->usb_dev;
868 struct timesync_authoritative_request *request;
870 request = kzalloc(sizeof(*request), GFP_KERNEL);
874 for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
875 request->frame_time[i] = cpu_to_le64(frame_time[i]);
877 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
878 REQUEST_TIMESYNC_AUTHORITATIVE,
879 USB_DIR_OUT | USB_TYPE_VENDOR |
880 USB_RECIP_INTERFACE, 0, 0, request,
881 sizeof(*request), ES2_TIMEOUT);
883 dev_err(&udev->dev, "Cannot timesync authoritative out %d\n", retval);
889 static int timesync_get_last_event(struct gb_host_device *hd, u64 *frame_time)
892 struct es2_ap_dev *es2 = hd_to_es2(hd);
893 struct usb_device *udev = es2->usb_dev;
894 __le64 *response_frame_time;
896 response_frame_time = kzalloc(sizeof(*response_frame_time), GFP_KERNEL);
897 if (!response_frame_time)
900 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
901 REQUEST_TIMESYNC_GET_LAST_EVENT,
902 USB_DIR_IN | USB_TYPE_VENDOR |
903 USB_RECIP_INTERFACE, 0, 0, response_frame_time,
904 sizeof(*response_frame_time), ES2_TIMEOUT);
906 if (retval != sizeof(*response_frame_time)) {
907 dev_err(&udev->dev, "Cannot get last TimeSync event: %d\n",
915 *frame_time = le64_to_cpu(*response_frame_time);
918 kfree(response_frame_time);
922 static struct gb_hd_driver es2_driver = {
923 .hd_priv_size = sizeof(struct es2_ap_dev),
924 .message_send = message_send,
925 .message_cancel = message_cancel,
926 .cport_allocate = es2_cport_allocate,
927 .cport_release = es2_cport_release,
928 .cport_enable = cport_enable,
929 .cport_disable = cport_disable,
930 .latency_tag_enable = latency_tag_enable,
931 .latency_tag_disable = latency_tag_disable,
933 .cport_features_enable = cport_features_enable,
934 .cport_features_disable = cport_features_disable,
935 .timesync_enable = timesync_enable,
936 .timesync_disable = timesync_disable,
937 .timesync_authoritative = timesync_authoritative,
938 .timesync_get_last_event = timesync_get_last_event,
941 /* Common function to report consistent warnings based on URB status */
942 static int check_urb_status(struct urb *urb)
944 struct device *dev = &urb->dev->dev;
945 int status = urb->status;
952 dev_err(dev, "%s: overflow actual length is %d\n",
953 __func__, urb->actual_length);
959 /* device is gone, stop sending */
962 dev_err(dev, "%s: unknown status %d\n", __func__, status);
967 static void es2_destroy(struct es2_ap_dev *es2)
969 struct usb_device *udev;
973 debugfs_remove(es2->apb_log_enable_dentry);
974 usb_log_disable(es2);
976 /* Tear down everything! */
977 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
978 struct urb *urb = es2->cport_out_urb[i];
984 es2->cport_out_urb[i] = NULL;
985 es2->cport_out_urb_busy[i] = false; /* just to be anal */
988 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
989 struct urb *urb = es2->arpc_urb[i];
994 kfree(es2->arpc_buffer[i]);
995 es2->arpc_buffer[i] = NULL;
998 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
999 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1001 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1002 struct urb *urb = cport_in->urb[i];
1007 kfree(cport_in->buffer[i]);
1008 cport_in->buffer[i] = NULL;
1012 kfree(es2->cport_to_ep);
1014 /* release reserved CDSI0 and CDSI1 cports */
1015 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
1016 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);
1018 udev = es2->usb_dev;
1024 static void cport_in_callback(struct urb *urb)
1026 struct gb_host_device *hd = urb->context;
1027 struct device *dev = &urb->dev->dev;
1028 struct gb_operation_msg_hdr *header;
1029 int status = check_urb_status(urb);
1034 if ((status == -EAGAIN) || (status == -EPROTO))
1037 /* The urb is being unlinked */
1038 if (status == -ENOENT || status == -ESHUTDOWN)
1041 dev_err(dev, "urb cport in error %d (dropped)\n", status);
1045 if (urb->actual_length < sizeof(*header)) {
1046 dev_err(dev, "short message received\n");
1050 /* Extract the CPort id, which is packed in the message header */
1051 header = urb->transfer_buffer;
1052 cport_id = gb_message_cport_unpack(header);
1054 if (cport_id_valid(hd, cport_id)) {
1055 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
1056 urb->actual_length);
1058 dev_err(dev, "invalid cport id %u received\n", cport_id);
1061 /* put our urb back in the request pool */
1062 retval = usb_submit_urb(urb, GFP_ATOMIC);
1064 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
1067 static void cport_out_callback(struct urb *urb)
1069 struct gb_message *message = urb->context;
1070 struct gb_host_device *hd = message->operation->connection->hd;
1071 struct es2_ap_dev *es2 = hd_to_es2(hd);
1072 int status = check_urb_status(urb);
1073 unsigned long flags;
1075 gb_message_cport_clear(message->header);
1077 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
1078 message->hcpriv = NULL;
1079 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
1082 * Tell the submitter that the message send (attempt) is
1083 * complete, and report the status.
1085 greybus_message_sent(hd, message, status);
1090 static struct arpc *arpc_alloc(void *payload, u16 size, u8 type)
1094 if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX)
1097 rpc = kzalloc(sizeof(*rpc), GFP_KERNEL);
1101 INIT_LIST_HEAD(&rpc->list);
1102 rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL);
1106 rpc->resp = kzalloc(sizeof(*rpc->resp), GFP_KERNEL);
1110 rpc->req->type = type;
1111 rpc->req->size = cpu_to_le16(sizeof(rpc->req) + size);
1112 memcpy(rpc->req->data, payload, size);
1114 init_completion(&rpc->response_received);
1126 static void arpc_free(struct arpc *rpc)
1133 static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id)
1137 list_for_each_entry(rpc, &es2->arpcs, list) {
1138 if (rpc->req->id == id)
1145 static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc)
1148 rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++);
1149 list_add_tail(&rpc->list, &es2->arpcs);
1152 static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc)
1155 rpc->active = false;
1156 list_del(&rpc->list);
1160 static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout)
1162 struct usb_device *udev = es2->usb_dev;
1165 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1166 APBA_REQUEST_ARPC_RUN,
1167 USB_DIR_OUT | USB_TYPE_VENDOR |
1168 USB_RECIP_INTERFACE,
1170 rpc->req, le16_to_cpu(rpc->req->size),
1172 if (retval != le16_to_cpu(rpc->req->size)) {
1174 "failed to send ARPC request %d: %d\n",
1175 rpc->req->type, retval);
1184 static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
1185 size_t size, int *result, unsigned int timeout)
1188 unsigned long flags;
1194 rpc = arpc_alloc(payload, size, type);
1198 spin_lock_irqsave(&es2->arpc_lock, flags);
1200 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1202 retval = arpc_send(es2, rpc, timeout);
1206 retval = wait_for_completion_interruptible_timeout(
1207 &rpc->response_received,
1208 msecs_to_jiffies(timeout));
1211 retval = -ETIMEDOUT;
1215 if (rpc->resp->result) {
1216 retval = -EREMOTEIO;
1218 *result = rpc->resp->result;
1224 spin_lock_irqsave(&es2->arpc_lock, flags);
1226 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1229 if (retval < 0 && retval != -EREMOTEIO) {
1230 dev_err(&es2->usb_dev->dev,
1231 "failed to execute ARPC: %d\n", retval);
1237 static void arpc_in_callback(struct urb *urb)
1239 struct es2_ap_dev *es2 = urb->context;
1240 struct device *dev = &urb->dev->dev;
1241 int status = check_urb_status(urb);
1243 struct arpc_response_message *resp;
1244 unsigned long flags;
1248 if ((status == -EAGAIN) || (status == -EPROTO))
1251 /* The urb is being unlinked */
1252 if (status == -ENOENT || status == -ESHUTDOWN)
1255 dev_err(dev, "arpc in-urb error %d (dropped)\n", status);
1259 if (urb->actual_length < sizeof(*resp)) {
1260 dev_err(dev, "short aprc response received\n");
1264 resp = urb->transfer_buffer;
1265 spin_lock_irqsave(&es2->arpc_lock, flags);
1266 rpc = arpc_find(es2, resp->id);
1268 dev_err(dev, "invalid arpc response id received: %u\n",
1269 le16_to_cpu(resp->id));
1270 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1275 memcpy(rpc->resp, resp, sizeof(*resp));
1276 complete(&rpc->response_received);
1277 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1280 /* put our urb back in the request pool */
1281 retval = usb_submit_urb(urb, GFP_ATOMIC);
1283 dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval);
1286 #define APB1_LOG_MSG_SIZE 64
1287 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
1291 /* SVC messages go down our control pipe */
1293 retval = usb_control_msg(es2->usb_dev,
1294 usb_rcvctrlpipe(es2->usb_dev, 0),
1296 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1302 kfifo_in(&es2->apb_log_fifo, buf, retval);
1303 } while (retval > 0);
1306 static int apb_log_poll(void *data)
1308 struct es2_ap_dev *es2 = data;
1311 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
1315 while (!kthread_should_stop()) {
1317 apb_log_get(es2, buf);
1325 static ssize_t apb_log_read(struct file *f, char __user *buf,
1326 size_t count, loff_t *ppos)
1328 struct es2_ap_dev *es2 = f->f_inode->i_private;
1333 if (count > APB1_LOG_SIZE)
1334 count = APB1_LOG_SIZE;
1336 tmp_buf = kmalloc(count, GFP_KERNEL);
1340 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
1341 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
1348 static const struct file_operations apb_log_fops = {
1349 .read = apb_log_read,
1352 static void usb_log_enable(struct es2_ap_dev *es2)
1354 if (!IS_ERR_OR_NULL(es2->apb_log_task))
1357 /* get log from APB1 */
1358 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
1359 if (IS_ERR(es2->apb_log_task))
1361 /* XXX We will need to rename this per APB */
1362 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
1363 gb_debugfs_get(), es2,
1367 static void usb_log_disable(struct es2_ap_dev *es2)
1369 if (IS_ERR_OR_NULL(es2->apb_log_task))
1372 debugfs_remove(es2->apb_log_dentry);
1373 es2->apb_log_dentry = NULL;
1375 kthread_stop(es2->apb_log_task);
1376 es2->apb_log_task = NULL;
1379 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
1380 size_t count, loff_t *ppos)
1382 struct es2_ap_dev *es2 = f->f_inode->i_private;
1383 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
1386 sprintf(tmp_buf, "%d\n", enable);
1387 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
1390 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
1391 size_t count, loff_t *ppos)
1395 struct es2_ap_dev *es2 = f->f_inode->i_private;
1397 retval = kstrtoint_from_user(buf, count, 10, &enable);
1402 usb_log_enable(es2);
1404 usb_log_disable(es2);
1409 static const struct file_operations apb_log_enable_fops = {
1410 .read = apb_log_enable_read,
1411 .write = apb_log_enable_write,
1414 static int apb_get_cport_count(struct usb_device *udev)
1417 __le16 *cport_count;
1419 cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
1423 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1424 GB_APB_REQUEST_CPORT_COUNT,
1425 USB_DIR_IN | USB_TYPE_VENDOR |
1426 USB_RECIP_INTERFACE, 0, 0, cport_count,
1427 sizeof(*cport_count), ES2_TIMEOUT);
1428 if (retval != sizeof(*cport_count)) {
1429 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1438 retval = le16_to_cpu(*cport_count);
1440 /* We need to fit a CPort ID in one byte of a message header */
1441 if (retval > U8_MAX) {
1443 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
1452 * The ES2 USB Bridge device has 15 endpoints
1453 * 1 Control - usual USB stuff + AP -> APBridgeA messages
1454 * 7 Bulk IN - CPort data in
1455 * 7 Bulk OUT - CPort data out
1457 static int ap_probe(struct usb_interface *interface,
1458 const struct usb_device_id *id)
1460 struct es2_ap_dev *es2;
1461 struct gb_host_device *hd;
1462 struct usb_device *udev;
1463 struct usb_host_interface *iface_desc;
1464 struct usb_endpoint_descriptor *endpoint;
1471 udev = usb_get_dev(interface_to_usbdev(interface));
1473 num_cports = apb_get_cport_count(udev);
1474 if (num_cports < 0) {
1476 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1481 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
1488 es2 = hd_to_es2(hd);
1490 es2->usb_intf = interface;
1491 es2->usb_dev = udev;
1492 spin_lock_init(&es2->cport_out_urb_lock);
1493 INIT_KFIFO(es2->apb_log_fifo);
1494 usb_set_intfdata(interface, es2);
1497 * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
1500 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
1503 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
1507 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
1509 if (!es2->cport_to_ep) {
1514 /* find all bulk endpoints */
1515 iface_desc = interface->cur_altsetting;
1516 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1517 endpoint = &iface_desc->endpoint[i].desc;
1519 if (usb_endpoint_is_bulk_in(endpoint)) {
1520 if (bulk_in < NUM_BULKS)
1521 es2->cport_in[bulk_in].endpoint =
1522 endpoint->bEndpointAddress;
1524 es2->arpc_endpoint_in =
1525 endpoint->bEndpointAddress;
1527 } else if (usb_endpoint_is_bulk_out(endpoint)) {
1528 es2->cport_out[bulk_out++].endpoint =
1529 endpoint->bEndpointAddress;
1532 "Unknown endpoint type found, address 0x%02x\n",
1533 endpoint->bEndpointAddress);
1536 if (bulk_in != NUM_BULKS_IN || bulk_out != NUM_BULKS_OUT) {
1537 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1542 /* Allocate buffers for our cport in messages */
1543 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1544 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1546 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1550 urb = usb_alloc_urb(0, GFP_KERNEL);
1555 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1561 usb_fill_bulk_urb(urb, udev,
1562 usb_rcvbulkpipe(udev,
1563 cport_in->endpoint),
1564 buffer, ES2_GBUF_MSG_SIZE_MAX,
1565 cport_in_callback, hd);
1566 cport_in->urb[i] = urb;
1567 cport_in->buffer[i] = buffer;
1571 /* Allocate buffers for ARPC in messages */
1572 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
1576 urb = usb_alloc_urb(0, GFP_KERNEL);
1581 buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL);
1587 usb_fill_bulk_urb(urb, udev,
1588 usb_rcvbulkpipe(udev,
1589 es2->arpc_endpoint_in),
1590 buffer, ARPC_IN_SIZE_MAX,
1591 arpc_in_callback, es2);
1593 es2->arpc_urb[i] = urb;
1594 es2->arpc_buffer[i] = buffer;
1597 /* Allocate urbs for our CPort OUT messages */
1598 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1601 urb = usb_alloc_urb(0, GFP_KERNEL);
1607 es2->cport_out_urb[i] = urb;
1608 es2->cport_out_urb_busy[i] = false; /* just to be anal */
1611 /* XXX We will need to rename this per APB */
1612 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1613 (S_IWUSR | S_IRUGO),
1614 gb_debugfs_get(), es2,
1615 &apb_log_enable_fops);
1617 INIT_LIST_HEAD(&es2->arpcs);
1618 spin_lock_init(&es2->arpc_lock);
1620 if (es2_arpc_in_enable(es2))
1623 retval = gb_hd_add(hd);
1625 goto err_disable_arpc_in;
1627 for (i = 0; i < NUM_BULKS; ++i) {
1628 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1630 goto err_disable_cport_in;
1635 err_disable_cport_in:
1636 for (--i; i >= 0; --i)
1637 es2_cport_in_disable(es2, &es2->cport_in[i]);
1639 err_disable_arpc_in:
1640 es2_arpc_in_disable(es2);
1647 static void ap_disconnect(struct usb_interface *interface)
1649 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1654 for (i = 0; i < NUM_BULKS; ++i)
1655 es2_cport_in_disable(es2, &es2->cport_in[i]);
1656 es2_arpc_in_disable(es2);
1661 static struct usb_driver es2_ap_driver = {
1662 .name = "es2_ap_driver",
1664 .disconnect = ap_disconnect,
1665 .id_table = id_table,
1669 module_usb_driver(es2_ap_driver);
1671 MODULE_LICENSE("GPL v2");
1672 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");