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;
693 u32 connection_flags;
696 req = kzalloc(sizeof(*req), GFP_KERNEL);
700 connection_flags = 0;
701 if (flags & GB_CONNECTION_FLAG_CONTROL)
702 connection_flags |= GB_APB_CPORT_FLAG_CONTROL;
703 if (flags & GB_CONNECTION_FLAG_HIGH_PRIO)
704 connection_flags |= GB_APB_CPORT_FLAG_HIGH_PRIO;
706 req->flags = cpu_to_le32(connection_flags);
708 dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__,
709 cport_id, connection_flags);
711 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
712 GB_APB_REQUEST_CPORT_FLAGS,
713 USB_DIR_OUT | USB_TYPE_VENDOR |
714 USB_RECIP_INTERFACE, cport_id, 0,
715 req, sizeof(*req), ES2_TIMEOUT);
716 if (ret != sizeof(*req)) {
717 dev_err(&udev->dev, "failed to set cport flags for port %d\n",
732 static int cport_disable(struct gb_host_device *hd, u16 cport_id)
736 retval = cport_reset(hd, cport_id);
743 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
746 struct es2_ap_dev *es2 = hd_to_es2(hd);
747 struct usb_device *udev = es2->usb_dev;
749 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
750 GB_APB_REQUEST_LATENCY_TAG_EN,
751 USB_DIR_OUT | USB_TYPE_VENDOR |
752 USB_RECIP_INTERFACE, cport_id, 0, NULL,
756 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
761 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
764 struct es2_ap_dev *es2 = hd_to_es2(hd);
765 struct usb_device *udev = es2->usb_dev;
767 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
768 GB_APB_REQUEST_LATENCY_TAG_DIS,
769 USB_DIR_OUT | USB_TYPE_VENDOR |
770 USB_RECIP_INTERFACE, cport_id, 0, NULL,
774 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
779 static int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
782 struct es2_ap_dev *es2 = hd_to_es2(hd);
783 struct usb_device *udev = es2->usb_dev;
785 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
786 GB_APB_REQUEST_CPORT_FEAT_EN,
787 USB_DIR_OUT | USB_TYPE_VENDOR |
788 USB_RECIP_INTERFACE, cport_id, 0, NULL,
791 dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
796 static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
799 struct es2_ap_dev *es2 = hd_to_es2(hd);
800 struct usb_device *udev = es2->usb_dev;
802 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
803 GB_APB_REQUEST_CPORT_FEAT_DIS,
804 USB_DIR_OUT | USB_TYPE_VENDOR |
805 USB_RECIP_INTERFACE, cport_id, 0, NULL,
809 "Cannot disable CPort features for cport %u: %d\n",
814 static int timesync_enable(struct gb_host_device *hd, u8 count,
815 u64 frame_time, u32 strobe_delay, u32 refclk)
818 struct es2_ap_dev *es2 = hd_to_es2(hd);
819 struct usb_device *udev = es2->usb_dev;
820 struct gb_control_timesync_enable_request *request;
822 request = kzalloc(sizeof(*request), GFP_KERNEL);
826 request->count = count;
827 request->frame_time = cpu_to_le64(frame_time);
828 request->strobe_delay = cpu_to_le32(strobe_delay);
829 request->refclk = cpu_to_le32(refclk);
830 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
831 REQUEST_TIMESYNC_ENABLE,
832 USB_DIR_OUT | USB_TYPE_VENDOR |
833 USB_RECIP_INTERFACE, 0, 0, request,
834 sizeof(*request), ES2_TIMEOUT);
836 dev_err(&udev->dev, "Cannot enable timesync %d\n", retval);
842 static int timesync_disable(struct gb_host_device *hd)
845 struct es2_ap_dev *es2 = hd_to_es2(hd);
846 struct usb_device *udev = es2->usb_dev;
848 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
849 REQUEST_TIMESYNC_DISABLE,
850 USB_DIR_OUT | USB_TYPE_VENDOR |
851 USB_RECIP_INTERFACE, 0, 0, NULL,
854 dev_err(&udev->dev, "Cannot disable timesync %d\n", retval);
859 static int timesync_authoritative(struct gb_host_device *hd, u64 *frame_time)
862 struct es2_ap_dev *es2 = hd_to_es2(hd);
863 struct usb_device *udev = es2->usb_dev;
864 struct timesync_authoritative_request *request;
866 request = kzalloc(sizeof(*request), GFP_KERNEL);
870 for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
871 request->frame_time[i] = cpu_to_le64(frame_time[i]);
873 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
874 REQUEST_TIMESYNC_AUTHORITATIVE,
875 USB_DIR_OUT | USB_TYPE_VENDOR |
876 USB_RECIP_INTERFACE, 0, 0, request,
877 sizeof(*request), ES2_TIMEOUT);
879 dev_err(&udev->dev, "Cannot timesync authoritative out %d\n", retval);
885 static int timesync_get_last_event(struct gb_host_device *hd, u64 *frame_time)
888 struct es2_ap_dev *es2 = hd_to_es2(hd);
889 struct usb_device *udev = es2->usb_dev;
890 __le64 *response_frame_time;
892 response_frame_time = kzalloc(sizeof(*response_frame_time), GFP_KERNEL);
893 if (!response_frame_time)
896 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
897 REQUEST_TIMESYNC_GET_LAST_EVENT,
898 USB_DIR_IN | USB_TYPE_VENDOR |
899 USB_RECIP_INTERFACE, 0, 0, response_frame_time,
900 sizeof(*response_frame_time), ES2_TIMEOUT);
902 if (retval != sizeof(*response_frame_time)) {
903 dev_err(&udev->dev, "Cannot get last TimeSync event: %d\n",
911 *frame_time = le64_to_cpu(*response_frame_time);
914 kfree(response_frame_time);
918 static struct gb_hd_driver es2_driver = {
919 .hd_priv_size = sizeof(struct es2_ap_dev),
920 .message_send = message_send,
921 .message_cancel = message_cancel,
922 .cport_allocate = es2_cport_allocate,
923 .cport_release = es2_cport_release,
924 .cport_enable = cport_enable,
925 .cport_disable = cport_disable,
926 .latency_tag_enable = latency_tag_enable,
927 .latency_tag_disable = latency_tag_disable,
929 .cport_features_enable = cport_features_enable,
930 .cport_features_disable = cport_features_disable,
931 .timesync_enable = timesync_enable,
932 .timesync_disable = timesync_disable,
933 .timesync_authoritative = timesync_authoritative,
934 .timesync_get_last_event = timesync_get_last_event,
937 /* Common function to report consistent warnings based on URB status */
938 static int check_urb_status(struct urb *urb)
940 struct device *dev = &urb->dev->dev;
941 int status = urb->status;
948 dev_err(dev, "%s: overflow actual length is %d\n",
949 __func__, urb->actual_length);
955 /* device is gone, stop sending */
958 dev_err(dev, "%s: unknown status %d\n", __func__, status);
963 static void es2_destroy(struct es2_ap_dev *es2)
965 struct usb_device *udev;
969 debugfs_remove(es2->apb_log_enable_dentry);
970 usb_log_disable(es2);
972 /* Tear down everything! */
973 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
974 struct urb *urb = es2->cport_out_urb[i];
980 es2->cport_out_urb[i] = NULL;
981 es2->cport_out_urb_busy[i] = false; /* just to be anal */
984 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
985 struct urb *urb = es2->arpc_urb[i];
990 kfree(es2->arpc_buffer[i]);
991 es2->arpc_buffer[i] = NULL;
994 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
995 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
997 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
998 struct urb *urb = cport_in->urb[i];
1003 kfree(cport_in->buffer[i]);
1004 cport_in->buffer[i] = NULL;
1008 kfree(es2->cport_to_ep);
1010 /* release reserved CDSI0 and CDSI1 cports */
1011 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
1012 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);
1014 udev = es2->usb_dev;
1020 static void cport_in_callback(struct urb *urb)
1022 struct gb_host_device *hd = urb->context;
1023 struct device *dev = &urb->dev->dev;
1024 struct gb_operation_msg_hdr *header;
1025 int status = check_urb_status(urb);
1030 if ((status == -EAGAIN) || (status == -EPROTO))
1033 /* The urb is being unlinked */
1034 if (status == -ENOENT || status == -ESHUTDOWN)
1037 dev_err(dev, "urb cport in error %d (dropped)\n", status);
1041 if (urb->actual_length < sizeof(*header)) {
1042 dev_err(dev, "short message received\n");
1046 /* Extract the CPort id, which is packed in the message header */
1047 header = urb->transfer_buffer;
1048 cport_id = gb_message_cport_unpack(header);
1050 if (cport_id_valid(hd, cport_id)) {
1051 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
1052 urb->actual_length);
1054 dev_err(dev, "invalid cport id %u received\n", cport_id);
1057 /* put our urb back in the request pool */
1058 retval = usb_submit_urb(urb, GFP_ATOMIC);
1060 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
1063 static void cport_out_callback(struct urb *urb)
1065 struct gb_message *message = urb->context;
1066 struct gb_host_device *hd = message->operation->connection->hd;
1067 struct es2_ap_dev *es2 = hd_to_es2(hd);
1068 int status = check_urb_status(urb);
1069 unsigned long flags;
1071 gb_message_cport_clear(message->header);
1073 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
1074 message->hcpriv = NULL;
1075 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
1078 * Tell the submitter that the message send (attempt) is
1079 * complete, and report the status.
1081 greybus_message_sent(hd, message, status);
1086 static struct arpc *arpc_alloc(void *payload, u16 size, u8 type)
1090 if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX)
1093 rpc = kzalloc(sizeof(*rpc), GFP_KERNEL);
1097 INIT_LIST_HEAD(&rpc->list);
1098 rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL);
1102 rpc->resp = kzalloc(sizeof(*rpc->resp), GFP_KERNEL);
1106 rpc->req->type = type;
1107 rpc->req->size = cpu_to_le16(sizeof(rpc->req) + size);
1108 memcpy(rpc->req->data, payload, size);
1110 init_completion(&rpc->response_received);
1122 static void arpc_free(struct arpc *rpc)
1129 static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id)
1133 list_for_each_entry(rpc, &es2->arpcs, list) {
1134 if (rpc->req->id == id)
1141 static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc)
1144 rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++);
1145 list_add_tail(&es2->arpcs, &rpc->list);
1148 static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc)
1151 rpc->active = false;
1152 list_del(&rpc->list);
1156 static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout)
1158 struct usb_device *udev = es2->usb_dev;
1161 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1162 APBA_REQUEST_ARPC_RUN,
1163 USB_DIR_OUT | USB_TYPE_VENDOR |
1164 USB_RECIP_INTERFACE,
1166 rpc->req, le16_to_cpu(rpc->req->size),
1168 if (retval != le16_to_cpu(rpc->req->size)) {
1170 "failed to send ARPC request %d: %d\n",
1171 rpc->req->type, retval);
1180 static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
1181 size_t size, int *result, unsigned int timeout)
1184 unsigned long flags;
1187 rpc = arpc_alloc(payload, size, type);
1191 spin_lock_irqsave(&es2->arpc_lock, flags);
1193 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1195 retval = arpc_send(es2, rpc, timeout);
1199 retval = wait_for_completion_interruptible_timeout(
1200 &rpc->response_received,
1201 msecs_to_jiffies(timeout));
1204 retval = -ETIMEDOUT;
1208 *result = rpc->resp->result;
1210 retval = -EREMOTEIO;
1215 spin_lock_irqsave(&es2->arpc_lock, flags);
1217 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1220 if (retval < 0 && retval != -EREMOTEIO) {
1221 dev_err(&es2->usb_dev->dev,
1222 "failed to execute ARPC: %d\n", retval);
1228 static void arpc_in_callback(struct urb *urb)
1230 struct es2_ap_dev *es2 = urb->context;
1231 struct device *dev = &urb->dev->dev;
1232 int status = check_urb_status(urb);
1234 struct arpc_response_message *resp;
1235 unsigned long flags;
1239 if ((status == -EAGAIN) || (status == -EPROTO))
1242 /* The urb is being unlinked */
1243 if (status == -ENOENT || status == -ESHUTDOWN)
1246 dev_err(dev, "arpc in-urb error %d (dropped)\n", status);
1250 if (urb->actual_length < sizeof(*resp)) {
1251 dev_err(dev, "short aprc response received\n");
1255 resp = urb->transfer_buffer;
1256 spin_lock_irqsave(&es2->arpc_lock, flags);
1257 rpc = arpc_find(es2, resp->id);
1259 dev_err(dev, "invalid arpc response id received: %d\n",
1261 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1266 memcpy(rpc->resp, resp, sizeof(*resp));
1267 complete(&rpc->response_received);
1268 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1271 /* put our urb back in the request pool */
1272 retval = usb_submit_urb(urb, GFP_ATOMIC);
1274 dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval);
1277 #define APB1_LOG_MSG_SIZE 64
1278 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
1282 /* SVC messages go down our control pipe */
1284 retval = usb_control_msg(es2->usb_dev,
1285 usb_rcvctrlpipe(es2->usb_dev, 0),
1287 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1293 kfifo_in(&es2->apb_log_fifo, buf, retval);
1294 } while (retval > 0);
1297 static int apb_log_poll(void *data)
1299 struct es2_ap_dev *es2 = data;
1302 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
1306 while (!kthread_should_stop()) {
1308 apb_log_get(es2, buf);
1316 static ssize_t apb_log_read(struct file *f, char __user *buf,
1317 size_t count, loff_t *ppos)
1319 struct es2_ap_dev *es2 = f->f_inode->i_private;
1324 if (count > APB1_LOG_SIZE)
1325 count = APB1_LOG_SIZE;
1327 tmp_buf = kmalloc(count, GFP_KERNEL);
1331 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
1332 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
1339 static const struct file_operations apb_log_fops = {
1340 .read = apb_log_read,
1343 static void usb_log_enable(struct es2_ap_dev *es2)
1345 if (!IS_ERR_OR_NULL(es2->apb_log_task))
1348 /* get log from APB1 */
1349 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
1350 if (IS_ERR(es2->apb_log_task))
1352 /* XXX We will need to rename this per APB */
1353 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
1354 gb_debugfs_get(), es2,
1358 static void usb_log_disable(struct es2_ap_dev *es2)
1360 if (IS_ERR_OR_NULL(es2->apb_log_task))
1363 debugfs_remove(es2->apb_log_dentry);
1364 es2->apb_log_dentry = NULL;
1366 kthread_stop(es2->apb_log_task);
1367 es2->apb_log_task = NULL;
1370 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
1371 size_t count, loff_t *ppos)
1373 struct es2_ap_dev *es2 = f->f_inode->i_private;
1374 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
1377 sprintf(tmp_buf, "%d\n", enable);
1378 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
1381 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
1382 size_t count, loff_t *ppos)
1386 struct es2_ap_dev *es2 = f->f_inode->i_private;
1388 retval = kstrtoint_from_user(buf, count, 10, &enable);
1393 usb_log_enable(es2);
1395 usb_log_disable(es2);
1400 static const struct file_operations apb_log_enable_fops = {
1401 .read = apb_log_enable_read,
1402 .write = apb_log_enable_write,
1405 static int apb_get_cport_count(struct usb_device *udev)
1408 __le16 *cport_count;
1410 cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
1414 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1415 GB_APB_REQUEST_CPORT_COUNT,
1416 USB_DIR_IN | USB_TYPE_VENDOR |
1417 USB_RECIP_INTERFACE, 0, 0, cport_count,
1418 sizeof(*cport_count), ES2_TIMEOUT);
1419 if (retval != sizeof(*cport_count)) {
1420 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1429 retval = le16_to_cpu(*cport_count);
1431 /* We need to fit a CPort ID in one byte of a message header */
1432 if (retval > U8_MAX) {
1434 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
1443 * The ES2 USB Bridge device has 15 endpoints
1444 * 1 Control - usual USB stuff + AP -> APBridgeA messages
1445 * 7 Bulk IN - CPort data in
1446 * 7 Bulk OUT - CPort data out
1448 static int ap_probe(struct usb_interface *interface,
1449 const struct usb_device_id *id)
1451 struct es2_ap_dev *es2;
1452 struct gb_host_device *hd;
1453 struct usb_device *udev;
1454 struct usb_host_interface *iface_desc;
1455 struct usb_endpoint_descriptor *endpoint;
1462 udev = usb_get_dev(interface_to_usbdev(interface));
1464 num_cports = apb_get_cport_count(udev);
1465 if (num_cports < 0) {
1467 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1472 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
1479 es2 = hd_to_es2(hd);
1481 es2->usb_intf = interface;
1482 es2->usb_dev = udev;
1483 spin_lock_init(&es2->cport_out_urb_lock);
1484 INIT_KFIFO(es2->apb_log_fifo);
1485 usb_set_intfdata(interface, es2);
1488 * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
1491 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
1494 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
1498 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
1500 if (!es2->cport_to_ep) {
1505 /* find all bulk endpoints */
1506 iface_desc = interface->cur_altsetting;
1507 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1508 endpoint = &iface_desc->endpoint[i].desc;
1510 if (usb_endpoint_is_bulk_in(endpoint)) {
1511 if (bulk_in < NUM_BULKS)
1512 es2->cport_in[bulk_in].endpoint =
1513 endpoint->bEndpointAddress;
1515 es2->arpc_endpoint_in =
1516 endpoint->bEndpointAddress;
1518 } else if (usb_endpoint_is_bulk_out(endpoint)) {
1519 es2->cport_out[bulk_out++].endpoint =
1520 endpoint->bEndpointAddress;
1523 "Unknown endpoint type found, address 0x%02x\n",
1524 endpoint->bEndpointAddress);
1527 if (bulk_in != NUM_BULKS_IN || bulk_out != NUM_BULKS_OUT) {
1528 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1533 /* Allocate buffers for our cport in messages */
1534 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1535 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1537 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1541 urb = usb_alloc_urb(0, GFP_KERNEL);
1546 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1552 usb_fill_bulk_urb(urb, udev,
1553 usb_rcvbulkpipe(udev,
1554 cport_in->endpoint),
1555 buffer, ES2_GBUF_MSG_SIZE_MAX,
1556 cport_in_callback, hd);
1557 cport_in->urb[i] = urb;
1558 cport_in->buffer[i] = buffer;
1562 /* Allocate buffers for ARPC in messages */
1563 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
1567 urb = usb_alloc_urb(0, GFP_KERNEL);
1572 buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL);
1578 usb_fill_bulk_urb(urb, udev,
1579 usb_rcvbulkpipe(udev,
1580 es2->arpc_endpoint_in),
1581 buffer, ARPC_IN_SIZE_MAX,
1582 arpc_in_callback, es2);
1584 es2->arpc_urb[i] = urb;
1585 es2->arpc_buffer[i] = buffer;
1588 /* Allocate urbs for our CPort OUT messages */
1589 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1592 urb = usb_alloc_urb(0, GFP_KERNEL);
1598 es2->cport_out_urb[i] = urb;
1599 es2->cport_out_urb_busy[i] = false; /* just to be anal */
1602 /* XXX We will need to rename this per APB */
1603 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1604 (S_IWUSR | S_IRUGO),
1605 gb_debugfs_get(), es2,
1606 &apb_log_enable_fops);
1608 INIT_LIST_HEAD(&es2->arpcs);
1609 spin_lock_init(&es2->arpc_lock);
1611 if (es2_arpc_in_enable(es2))
1614 retval = gb_hd_add(hd);
1616 goto err_disable_arpc_in;
1618 for (i = 0; i < NUM_BULKS; ++i) {
1619 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1621 goto err_disable_cport_in;
1626 err_disable_cport_in:
1627 for (--i; i >= 0; --i)
1628 es2_cport_in_disable(es2, &es2->cport_in[i]);
1630 err_disable_arpc_in:
1631 es2_arpc_in_disable(es2);
1638 static void ap_disconnect(struct usb_interface *interface)
1640 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1645 for (i = 0; i < NUM_BULKS; ++i)
1646 es2_cport_in_disable(es2, &es2->cport_in[i]);
1647 es2_arpc_in_disable(es2);
1652 static struct usb_driver es2_ap_driver = {
1653 .name = "es2_ap_driver",
1655 .disconnect = ap_disconnect,
1656 .id_table = id_table,
1660 module_usb_driver(es2_ap_driver);
1662 MODULE_LICENSE("GPL v2");
1663 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");