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 USB vendor requests. */
24 #define ES2_USB_CTRL_TIMEOUT 500
26 /* Default timeout for ARPC CPort requests */
27 #define ES2_ARPC_CPORT_TIMEOUT 500
29 /* Fixed CPort numbers */
30 #define ES2_CPORT_CDSI0 16
31 #define ES2_CPORT_CDSI1 17
33 /* Memory sizes for the buffers sent to/from the ES2 controller */
34 #define ES2_GBUF_MSG_SIZE_MAX 2048
36 /* Memory sizes for the ARPC buffers */
37 #define ARPC_OUT_SIZE_MAX U16_MAX
38 #define ARPC_IN_SIZE_MAX 128
40 static const struct usb_device_id id_table[] = {
41 { USB_DEVICE(0x18d1, 0x1eaf) },
44 MODULE_DEVICE_TABLE(usb, id_table);
46 #define APB1_LOG_SIZE SZ_16K
48 /* Number of bulk in and bulk out couple */
51 /* Expected number of bulk out endpoints */
52 #define NUM_BULKS_OUT NUM_BULKS
54 /* Expected number of bulk in endpoints (including ARPC endpoint) */
55 #define NUM_BULKS_IN (NUM_BULKS + 1)
58 * Number of CPort IN urbs in flight at any point in time.
59 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
62 #define NUM_CPORT_IN_URB 4
64 /* Number of CPort OUT urbs in flight at any point in time.
65 * Adjust if we get messages saying we are out of urbs in the system log.
67 #define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
70 * Number of ARPC in urbs in flight at any point in time.
72 #define NUM_ARPC_IN_URB 2
75 * @endpoint: bulk in endpoint for CPort data
76 * @urb: array of urbs for the CPort in messages
77 * @buffer: array of buffers for the @cport_in_urb urbs
81 struct urb *urb[NUM_CPORT_IN_URB];
82 u8 *buffer[NUM_CPORT_IN_URB];
86 * @endpoint: bulk out endpoint for CPort data
88 struct es2_cport_out {
93 * es2_ap_dev - ES2 USB Bridge to AP structure
94 * @usb_dev: pointer to the USB device we are.
95 * @usb_intf: pointer to the USB interface we are bound to.
96 * @hd: pointer to our gb_host_device structure
98 * @cport_in: endpoint, urbs and buffer for cport in messages
99 * @cport_out: endpoint for for cport out messages
100 * @cport_out_urb: array of urbs for the CPort out messages
101 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
103 * @cport_out_urb_cancelled: array of flags indicating whether the
104 * corresponding @cport_out_urb is being cancelled
105 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
107 * @apb_log_task: task pointer for logging thread
108 * @apb_log_dentry: file system entry for the log file interface
109 * @apb_log_enable_dentry: file system entry for enabling logging
110 * @apb_log_fifo: kernel FIFO to carry logged data
111 * @arpc_urb: array of urbs for the ARPC in messages
112 * @arpc_buffer: array of buffers for the @arpc_urb urbs
113 * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC
114 * @arpc_id_cycle: gives an unique id to ARPC
115 * @arpc_lock: locks ARPC list
116 * @arpcs: list of in progress ARPCs
119 struct usb_device *usb_dev;
120 struct usb_interface *usb_intf;
121 struct gb_host_device *hd;
123 struct es2_cport_in cport_in[NUM_BULKS];
124 struct es2_cport_out cport_out[NUM_BULKS];
125 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
126 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
127 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
128 spinlock_t cport_out_urb_lock;
134 struct task_struct *apb_log_task;
135 struct dentry *apb_log_dentry;
136 struct dentry *apb_log_enable_dentry;
137 DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
139 __u8 arpc_endpoint_in;
140 struct urb *arpc_urb[NUM_ARPC_IN_URB];
141 u8 *arpc_buffer[NUM_ARPC_IN_URB];
144 spinlock_t arpc_lock;
145 struct list_head arpcs;
149 * cport_to_ep - information about cport to endpoints mapping
150 * @cport_id: the id of cport to map to endpoints
151 * @endpoint_in: the endpoint number to use for in transfer
152 * @endpoint_out: he endpoint number to use for out transfer
161 * timesync_enable_request - Enable timesync in an APBridge
162 * @count: number of TimeSync Pulses to expect
163 * @frame_time: the initial FrameTime at the first TimeSync Pulse
164 * @strobe_delay: the expected delay in microseconds between each TimeSync Pulse
165 * @refclk: The AP mandated reference clock to run FrameTime at
167 struct timesync_enable_request {
175 * timesync_authoritative_request - Transmit authoritative FrameTime to APBridge
176 * @frame_time: An array of authoritative FrameTimes provided by the SVC
177 * and relayed to the APBridge by the AP
179 struct timesync_authoritative_request {
180 __le64 frame_time[GB_TIMESYNC_MAX_STROBES];
184 struct list_head list;
185 struct arpc_request_message *req;
186 struct arpc_response_message *resp;
187 struct completion response_received;
191 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
193 return (struct es2_ap_dev *)&hd->hd_priv;
196 static void cport_out_callback(struct urb *urb);
197 static void usb_log_enable(struct es2_ap_dev *es2);
198 static void usb_log_disable(struct es2_ap_dev *es2);
199 static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
200 size_t size, int *result, unsigned int timeout);
202 /* Get the endpoints pair mapped to the cport */
203 static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id)
205 if (cport_id >= es2->hd->num_cports)
207 return es2->cport_to_ep[cport_id];
210 /* Disable for now until we work all of this out to keep a warning-free build */
212 /* Test if the endpoints pair is already mapped to a cport */
213 static int ep_pair_in_use(struct es2_ap_dev *es2, int ep_pair)
217 for (i = 0; i < es2->hd->num_cports; i++) {
218 if (es2->cport_to_ep[i] == ep_pair)
224 /* Configure the endpoint mapping and send the request to APBridge */
225 static int map_cport_to_ep(struct es2_ap_dev *es2,
226 u16 cport_id, int ep_pair)
229 struct cport_to_ep *cport_to_ep;
231 if (ep_pair < 0 || ep_pair >= NUM_BULKS)
233 if (cport_id >= es2->hd->num_cports)
235 if (ep_pair && ep_pair_in_use(es2, ep_pair))
238 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
242 es2->cport_to_ep[cport_id] = ep_pair;
243 cport_to_ep->cport_id = cpu_to_le16(cport_id);
244 cport_to_ep->endpoint_in = es2->cport_in[ep_pair].endpoint;
245 cport_to_ep->endpoint_out = es2->cport_out[ep_pair].endpoint;
247 retval = usb_control_msg(es2->usb_dev,
248 usb_sndctrlpipe(es2->usb_dev, 0),
249 GB_APB_REQUEST_EP_MAPPING,
250 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
253 sizeof(*cport_to_ep),
254 ES2_USB_CTRL_TIMEOUT);
255 if (retval == sizeof(*cport_to_ep))
262 /* Unmap a cport: use the muxed endpoints pair */
263 static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id)
265 return map_cport_to_ep(es2, cport_id, 0);
269 static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
271 struct usb_device *udev = es2->usb_dev;
275 data = kmalloc(size, GFP_KERNEL);
278 memcpy(data, req, size);
280 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
282 USB_DIR_OUT | USB_TYPE_VENDOR |
284 0, 0, data, size, ES2_USB_CTRL_TIMEOUT);
286 dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
294 static void ap_urb_complete(struct urb *urb)
296 struct usb_ctrlrequest *dr = urb->context;
302 static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
304 struct usb_device *udev = es2->usb_dev;
306 struct usb_ctrlrequest *dr;
310 urb = usb_alloc_urb(0, GFP_ATOMIC);
314 dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
320 buf = (u8 *)dr + sizeof(*dr);
321 memcpy(buf, req, size);
324 dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
327 dr->wLength = cpu_to_le16(size);
329 usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
330 (unsigned char *)dr, buf, size,
331 ap_urb_complete, dr);
332 retval = usb_submit_urb(urb, GFP_ATOMIC);
340 static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
343 struct es2_ap_dev *es2 = hd_to_es2(hd);
346 return output_async(es2, req, size, cmd);
348 return output_sync(es2, req, size, cmd);
351 static int es2_cport_in_enable(struct es2_ap_dev *es2,
352 struct es2_cport_in *cport_in)
358 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
359 urb = cport_in->urb[i];
361 ret = usb_submit_urb(urb, GFP_KERNEL);
363 dev_err(&es2->usb_dev->dev,
364 "failed to submit in-urb: %d\n", ret);
372 for (--i; i >= 0; --i) {
373 urb = cport_in->urb[i];
380 static void es2_cport_in_disable(struct es2_ap_dev *es2,
381 struct es2_cport_in *cport_in)
386 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
387 urb = cport_in->urb[i];
392 static int es2_arpc_in_enable(struct es2_ap_dev *es2)
398 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
399 urb = es2->arpc_urb[i];
401 ret = usb_submit_urb(urb, GFP_KERNEL);
403 dev_err(&es2->usb_dev->dev,
404 "failed to submit arpc in-urb: %d\n", ret);
412 for (--i; i >= 0; --i) {
413 urb = es2->arpc_urb[i];
420 static void es2_arpc_in_disable(struct es2_ap_dev *es2)
425 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
426 urb = es2->arpc_urb[i];
431 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
433 struct urb *urb = NULL;
437 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
439 /* Look in our pool of allocated urbs first, as that's the "fastest" */
440 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
441 if (es2->cport_out_urb_busy[i] == false &&
442 es2->cport_out_urb_cancelled[i] == false) {
443 es2->cport_out_urb_busy[i] = true;
444 urb = es2->cport_out_urb[i];
448 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
453 * Crap, pool is empty, complain to the syslog and go allocate one
454 * dynamically as we have to succeed.
456 dev_dbg(&es2->usb_dev->dev,
457 "No free CPort OUT urbs, having to dynamically allocate one!\n");
458 return usb_alloc_urb(0, gfp_mask);
461 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
466 * See if this was an urb in our pool, if so mark it "free", otherwise
467 * we need to free it ourselves.
469 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
470 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
471 if (urb == es2->cport_out_urb[i]) {
472 es2->cport_out_urb_busy[i] = false;
477 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
479 /* If urb is not NULL, then we need to free this urb */
484 * We (ab)use the operation-message header pad bytes to transfer the
485 * cport id in order to minimise overhead.
488 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
490 header->pad[0] = cport_id;
493 /* Clear the pad bytes used for the CPort id */
494 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
499 /* Extract the CPort id packed into the header, and clear it */
500 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
502 u16 cport_id = header->pad[0];
504 gb_message_cport_clear(header);
510 * Returns zero if the message was successfully queued, or a negative errno
513 static int message_send(struct gb_host_device *hd, u16 cport_id,
514 struct gb_message *message, gfp_t gfp_mask)
516 struct es2_ap_dev *es2 = hd_to_es2(hd);
517 struct usb_device *udev = es2->usb_dev;
525 * The data actually transferred will include an indication
526 * of where the data should be sent. Do one last check of
527 * the target CPort id before filling it in.
529 if (!cport_id_valid(hd, cport_id)) {
530 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
534 /* Find a free urb */
535 urb = next_free_urb(es2, gfp_mask);
539 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
540 message->hcpriv = urb;
541 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
543 /* Pack the cport id into the message header */
544 gb_message_cport_pack(message->header, cport_id);
546 buffer_size = sizeof(*message->header) + message->payload_size;
548 ep_pair = cport_to_ep_pair(es2, cport_id);
549 usb_fill_bulk_urb(urb, udev,
550 usb_sndbulkpipe(udev,
551 es2->cport_out[ep_pair].endpoint),
552 message->buffer, buffer_size,
553 cport_out_callback, message);
554 urb->transfer_flags |= URB_ZERO_PACKET;
556 trace_gb_message_submit(message);
558 retval = usb_submit_urb(urb, gfp_mask);
560 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
562 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
563 message->hcpriv = NULL;
564 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
567 gb_message_cport_clear(message->header);
576 * Can not be called in atomic context.
578 static void message_cancel(struct gb_message *message)
580 struct gb_host_device *hd = message->operation->connection->hd;
581 struct es2_ap_dev *es2 = hd_to_es2(hd);
587 spin_lock_irq(&es2->cport_out_urb_lock);
588 urb = message->hcpriv;
590 /* Prevent dynamically allocated urb from being deallocated. */
593 /* Prevent pre-allocated urb from being reused. */
594 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
595 if (urb == es2->cport_out_urb[i]) {
596 es2->cport_out_urb_cancelled[i] = true;
600 spin_unlock_irq(&es2->cport_out_urb_lock);
604 if (i < NUM_CPORT_OUT_URB) {
605 spin_lock_irq(&es2->cport_out_urb_lock);
606 es2->cport_out_urb_cancelled[i] = false;
607 spin_unlock_irq(&es2->cport_out_urb_lock);
613 static int cport_reset(struct gb_host_device *hd, u16 cport_id)
615 struct es2_ap_dev *es2 = hd_to_es2(hd);
616 struct usb_device *udev = es2->usb_dev;
617 struct arpc_cport_reset_req req;
622 case GB_SVC_CPORT_ID:
623 case ES2_CPORT_CDSI0:
624 case ES2_CPORT_CDSI1:
628 req.cport_id = cpu_to_le16(cport_id);
629 retval = arpc_sync(es2, ARPC_TYPE_CPORT_RESET, &req, sizeof(req),
630 &result, ES2_ARPC_CPORT_TIMEOUT);
631 if (retval == -EREMOTEIO) {
632 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
639 static int es2_cport_allocate(struct gb_host_device *hd, int cport_id,
642 struct es2_ap_dev *es2 = hd_to_es2(hd);
643 struct ida *id_map = &hd->cport_id_map;
644 int ida_start, ida_end;
647 case ES2_CPORT_CDSI0:
648 case ES2_CPORT_CDSI1:
649 dev_err(&hd->dev, "cport %d not available\n", cport_id);
653 if (flags & GB_CONNECTION_FLAG_OFFLOADED &&
654 flags & GB_CONNECTION_FLAG_CDSI1) {
655 if (es2->cdsi1_in_use) {
656 dev_err(&hd->dev, "CDSI1 already in use\n");
660 es2->cdsi1_in_use = true;
662 return ES2_CPORT_CDSI1;
667 ida_end = hd->num_cports;
668 } else if (cport_id < hd->num_cports) {
669 ida_start = cport_id;
670 ida_end = cport_id + 1;
672 dev_err(&hd->dev, "cport %d not available\n", cport_id);
676 return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
679 static void es2_cport_release(struct gb_host_device *hd, u16 cport_id)
681 struct es2_ap_dev *es2 = hd_to_es2(hd);
684 case ES2_CPORT_CDSI1:
685 es2->cdsi1_in_use = false;
689 ida_simple_remove(&hd->cport_id_map, cport_id);
692 static int cport_enable(struct gb_host_device *hd, u16 cport_id,
695 struct es2_ap_dev *es2 = hd_to_es2(hd);
696 struct usb_device *udev = es2->usb_dev;
697 struct gb_apb_request_cport_flags *req;
698 u32 connection_flags;
701 req = kzalloc(sizeof(*req), GFP_KERNEL);
705 connection_flags = 0;
706 if (flags & GB_CONNECTION_FLAG_CONTROL)
707 connection_flags |= GB_APB_CPORT_FLAG_CONTROL;
708 if (flags & GB_CONNECTION_FLAG_HIGH_PRIO)
709 connection_flags |= GB_APB_CPORT_FLAG_HIGH_PRIO;
711 req->flags = cpu_to_le32(connection_flags);
713 dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__,
714 cport_id, connection_flags);
716 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
717 GB_APB_REQUEST_CPORT_FLAGS,
718 USB_DIR_OUT | USB_TYPE_VENDOR |
719 USB_RECIP_INTERFACE, cport_id, 0,
720 req, sizeof(*req), ES2_USB_CTRL_TIMEOUT);
721 if (ret != sizeof(*req)) {
722 dev_err(&udev->dev, "failed to set cport flags for port %d\n",
737 static int cport_disable(struct gb_host_device *hd, u16 cport_id)
741 retval = cport_reset(hd, cport_id);
748 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
751 struct es2_ap_dev *es2 = hd_to_es2(hd);
752 struct usb_device *udev = es2->usb_dev;
754 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
755 GB_APB_REQUEST_LATENCY_TAG_EN,
756 USB_DIR_OUT | USB_TYPE_VENDOR |
757 USB_RECIP_INTERFACE, cport_id, 0, NULL,
758 0, ES2_USB_CTRL_TIMEOUT);
761 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
766 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
769 struct es2_ap_dev *es2 = hd_to_es2(hd);
770 struct usb_device *udev = es2->usb_dev;
772 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
773 GB_APB_REQUEST_LATENCY_TAG_DIS,
774 USB_DIR_OUT | USB_TYPE_VENDOR |
775 USB_RECIP_INTERFACE, cport_id, 0, NULL,
776 0, ES2_USB_CTRL_TIMEOUT);
779 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
784 static int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
787 struct es2_ap_dev *es2 = hd_to_es2(hd);
788 struct usb_device *udev = es2->usb_dev;
790 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
791 GB_APB_REQUEST_CPORT_FEAT_EN,
792 USB_DIR_OUT | USB_TYPE_VENDOR |
793 USB_RECIP_INTERFACE, cport_id, 0, NULL,
794 0, ES2_USB_CTRL_TIMEOUT);
796 dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
801 static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
804 struct es2_ap_dev *es2 = hd_to_es2(hd);
805 struct usb_device *udev = es2->usb_dev;
807 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
808 GB_APB_REQUEST_CPORT_FEAT_DIS,
809 USB_DIR_OUT | USB_TYPE_VENDOR |
810 USB_RECIP_INTERFACE, cport_id, 0, NULL,
811 0, ES2_USB_CTRL_TIMEOUT);
814 "Cannot disable CPort features for cport %u: %d\n",
819 static int timesync_enable(struct gb_host_device *hd, u8 count,
820 u64 frame_time, u32 strobe_delay, u32 refclk)
823 struct es2_ap_dev *es2 = hd_to_es2(hd);
824 struct usb_device *udev = es2->usb_dev;
825 struct gb_control_timesync_enable_request *request;
827 request = kzalloc(sizeof(*request), GFP_KERNEL);
831 request->count = count;
832 request->frame_time = cpu_to_le64(frame_time);
833 request->strobe_delay = cpu_to_le32(strobe_delay);
834 request->refclk = cpu_to_le32(refclk);
835 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
836 REQUEST_TIMESYNC_ENABLE,
837 USB_DIR_OUT | USB_TYPE_VENDOR |
838 USB_RECIP_INTERFACE, 0, 0, request,
839 sizeof(*request), ES2_USB_CTRL_TIMEOUT);
841 dev_err(&udev->dev, "Cannot enable timesync %d\n", retval);
847 static int timesync_disable(struct gb_host_device *hd)
850 struct es2_ap_dev *es2 = hd_to_es2(hd);
851 struct usb_device *udev = es2->usb_dev;
853 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
854 REQUEST_TIMESYNC_DISABLE,
855 USB_DIR_OUT | USB_TYPE_VENDOR |
856 USB_RECIP_INTERFACE, 0, 0, NULL,
857 0, ES2_USB_CTRL_TIMEOUT);
859 dev_err(&udev->dev, "Cannot disable timesync %d\n", retval);
864 static int timesync_authoritative(struct gb_host_device *hd, u64 *frame_time)
867 struct es2_ap_dev *es2 = hd_to_es2(hd);
868 struct usb_device *udev = es2->usb_dev;
869 struct timesync_authoritative_request *request;
871 request = kzalloc(sizeof(*request), GFP_KERNEL);
875 for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
876 request->frame_time[i] = cpu_to_le64(frame_time[i]);
878 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
879 REQUEST_TIMESYNC_AUTHORITATIVE,
880 USB_DIR_OUT | USB_TYPE_VENDOR |
881 USB_RECIP_INTERFACE, 0, 0, request,
882 sizeof(*request), ES2_USB_CTRL_TIMEOUT);
884 dev_err(&udev->dev, "Cannot timesync authoritative out %d\n", retval);
890 static int timesync_get_last_event(struct gb_host_device *hd, u64 *frame_time)
893 struct es2_ap_dev *es2 = hd_to_es2(hd);
894 struct usb_device *udev = es2->usb_dev;
895 __le64 *response_frame_time;
897 response_frame_time = kzalloc(sizeof(*response_frame_time), GFP_KERNEL);
898 if (!response_frame_time)
901 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
902 REQUEST_TIMESYNC_GET_LAST_EVENT,
903 USB_DIR_IN | USB_TYPE_VENDOR |
904 USB_RECIP_INTERFACE, 0, 0, response_frame_time,
905 sizeof(*response_frame_time),
906 ES2_USB_CTRL_TIMEOUT);
908 if (retval != sizeof(*response_frame_time)) {
909 dev_err(&udev->dev, "Cannot get last TimeSync event: %d\n",
917 *frame_time = le64_to_cpu(*response_frame_time);
920 kfree(response_frame_time);
924 static struct gb_hd_driver es2_driver = {
925 .hd_priv_size = sizeof(struct es2_ap_dev),
926 .message_send = message_send,
927 .message_cancel = message_cancel,
928 .cport_allocate = es2_cport_allocate,
929 .cport_release = es2_cport_release,
930 .cport_enable = cport_enable,
931 .cport_disable = cport_disable,
932 .latency_tag_enable = latency_tag_enable,
933 .latency_tag_disable = latency_tag_disable,
935 .cport_features_enable = cport_features_enable,
936 .cport_features_disable = cport_features_disable,
937 .timesync_enable = timesync_enable,
938 .timesync_disable = timesync_disable,
939 .timesync_authoritative = timesync_authoritative,
940 .timesync_get_last_event = timesync_get_last_event,
943 /* Common function to report consistent warnings based on URB status */
944 static int check_urb_status(struct urb *urb)
946 struct device *dev = &urb->dev->dev;
947 int status = urb->status;
954 dev_err(dev, "%s: overflow actual length is %d\n",
955 __func__, urb->actual_length);
961 /* device is gone, stop sending */
964 dev_err(dev, "%s: unknown status %d\n", __func__, status);
969 static void es2_destroy(struct es2_ap_dev *es2)
971 struct usb_device *udev;
975 debugfs_remove(es2->apb_log_enable_dentry);
976 usb_log_disable(es2);
978 /* Tear down everything! */
979 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
980 struct urb *urb = es2->cport_out_urb[i];
986 es2->cport_out_urb[i] = NULL;
987 es2->cport_out_urb_busy[i] = false; /* just to be anal */
990 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
991 struct urb *urb = es2->arpc_urb[i];
996 kfree(es2->arpc_buffer[i]);
997 es2->arpc_buffer[i] = NULL;
1000 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1001 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1003 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1004 struct urb *urb = cport_in->urb[i];
1009 kfree(cport_in->buffer[i]);
1010 cport_in->buffer[i] = NULL;
1014 kfree(es2->cport_to_ep);
1016 /* release reserved CDSI0 and CDSI1 cports */
1017 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
1018 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);
1020 udev = es2->usb_dev;
1026 static void cport_in_callback(struct urb *urb)
1028 struct gb_host_device *hd = urb->context;
1029 struct device *dev = &urb->dev->dev;
1030 struct gb_operation_msg_hdr *header;
1031 int status = check_urb_status(urb);
1036 if ((status == -EAGAIN) || (status == -EPROTO))
1039 /* The urb is being unlinked */
1040 if (status == -ENOENT || status == -ESHUTDOWN)
1043 dev_err(dev, "urb cport in error %d (dropped)\n", status);
1047 if (urb->actual_length < sizeof(*header)) {
1048 dev_err(dev, "short message received\n");
1052 /* Extract the CPort id, which is packed in the message header */
1053 header = urb->transfer_buffer;
1054 cport_id = gb_message_cport_unpack(header);
1056 if (cport_id_valid(hd, cport_id)) {
1057 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
1058 urb->actual_length);
1060 dev_err(dev, "invalid cport id %u received\n", cport_id);
1063 /* put our urb back in the request pool */
1064 retval = usb_submit_urb(urb, GFP_ATOMIC);
1066 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
1069 static void cport_out_callback(struct urb *urb)
1071 struct gb_message *message = urb->context;
1072 struct gb_host_device *hd = message->operation->connection->hd;
1073 struct es2_ap_dev *es2 = hd_to_es2(hd);
1074 int status = check_urb_status(urb);
1075 unsigned long flags;
1077 gb_message_cport_clear(message->header);
1079 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
1080 message->hcpriv = NULL;
1081 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
1084 * Tell the submitter that the message send (attempt) is
1085 * complete, and report the status.
1087 greybus_message_sent(hd, message, status);
1092 static struct arpc *arpc_alloc(void *payload, u16 size, u8 type)
1096 if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX)
1099 rpc = kzalloc(sizeof(*rpc), GFP_KERNEL);
1103 INIT_LIST_HEAD(&rpc->list);
1104 rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL);
1108 rpc->resp = kzalloc(sizeof(*rpc->resp), GFP_KERNEL);
1112 rpc->req->type = type;
1113 rpc->req->size = cpu_to_le16(sizeof(rpc->req) + size);
1114 memcpy(rpc->req->data, payload, size);
1116 init_completion(&rpc->response_received);
1128 static void arpc_free(struct arpc *rpc)
1135 static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id)
1139 list_for_each_entry(rpc, &es2->arpcs, list) {
1140 if (rpc->req->id == id)
1147 static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc)
1150 rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++);
1151 list_add_tail(&rpc->list, &es2->arpcs);
1154 static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc)
1157 rpc->active = false;
1158 list_del(&rpc->list);
1162 static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout)
1164 struct usb_device *udev = es2->usb_dev;
1167 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1168 APBA_REQUEST_ARPC_RUN,
1169 USB_DIR_OUT | USB_TYPE_VENDOR |
1170 USB_RECIP_INTERFACE,
1172 rpc->req, le16_to_cpu(rpc->req->size),
1173 ES2_USB_CTRL_TIMEOUT);
1174 if (retval != le16_to_cpu(rpc->req->size)) {
1176 "failed to send ARPC request %d: %d\n",
1177 rpc->req->type, retval);
1186 static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
1187 size_t size, int *result, unsigned int timeout)
1190 unsigned long flags;
1196 rpc = arpc_alloc(payload, size, type);
1200 spin_lock_irqsave(&es2->arpc_lock, flags);
1202 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1204 retval = arpc_send(es2, rpc, timeout);
1208 retval = wait_for_completion_interruptible_timeout(
1209 &rpc->response_received,
1210 msecs_to_jiffies(timeout));
1213 retval = -ETIMEDOUT;
1217 if (rpc->resp->result) {
1218 retval = -EREMOTEIO;
1220 *result = rpc->resp->result;
1226 spin_lock_irqsave(&es2->arpc_lock, flags);
1228 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1231 if (retval < 0 && retval != -EREMOTEIO) {
1232 dev_err(&es2->usb_dev->dev,
1233 "failed to execute ARPC: %d\n", retval);
1239 static void arpc_in_callback(struct urb *urb)
1241 struct es2_ap_dev *es2 = urb->context;
1242 struct device *dev = &urb->dev->dev;
1243 int status = check_urb_status(urb);
1245 struct arpc_response_message *resp;
1246 unsigned long flags;
1250 if ((status == -EAGAIN) || (status == -EPROTO))
1253 /* The urb is being unlinked */
1254 if (status == -ENOENT || status == -ESHUTDOWN)
1257 dev_err(dev, "arpc in-urb error %d (dropped)\n", status);
1261 if (urb->actual_length < sizeof(*resp)) {
1262 dev_err(dev, "short aprc response received\n");
1266 resp = urb->transfer_buffer;
1267 spin_lock_irqsave(&es2->arpc_lock, flags);
1268 rpc = arpc_find(es2, resp->id);
1270 dev_err(dev, "invalid arpc response id received: %u\n",
1271 le16_to_cpu(resp->id));
1272 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1277 memcpy(rpc->resp, resp, sizeof(*resp));
1278 complete(&rpc->response_received);
1279 spin_unlock_irqrestore(&es2->arpc_lock, flags);
1282 /* put our urb back in the request pool */
1283 retval = usb_submit_urb(urb, GFP_ATOMIC);
1285 dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval);
1288 #define APB1_LOG_MSG_SIZE 64
1289 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
1294 retval = usb_control_msg(es2->usb_dev,
1295 usb_rcvctrlpipe(es2->usb_dev, 0),
1297 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1301 ES2_USB_CTRL_TIMEOUT);
1303 kfifo_in(&es2->apb_log_fifo, buf, retval);
1304 } while (retval > 0);
1307 static int apb_log_poll(void *data)
1309 struct es2_ap_dev *es2 = data;
1312 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
1316 while (!kthread_should_stop()) {
1318 apb_log_get(es2, buf);
1326 static ssize_t apb_log_read(struct file *f, char __user *buf,
1327 size_t count, loff_t *ppos)
1329 struct es2_ap_dev *es2 = f->f_inode->i_private;
1334 if (count > APB1_LOG_SIZE)
1335 count = APB1_LOG_SIZE;
1337 tmp_buf = kmalloc(count, GFP_KERNEL);
1341 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
1342 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
1349 static const struct file_operations apb_log_fops = {
1350 .read = apb_log_read,
1353 static void usb_log_enable(struct es2_ap_dev *es2)
1355 if (!IS_ERR_OR_NULL(es2->apb_log_task))
1358 /* get log from APB1 */
1359 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
1360 if (IS_ERR(es2->apb_log_task))
1362 /* XXX We will need to rename this per APB */
1363 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
1364 gb_debugfs_get(), es2,
1368 static void usb_log_disable(struct es2_ap_dev *es2)
1370 if (IS_ERR_OR_NULL(es2->apb_log_task))
1373 debugfs_remove(es2->apb_log_dentry);
1374 es2->apb_log_dentry = NULL;
1376 kthread_stop(es2->apb_log_task);
1377 es2->apb_log_task = NULL;
1380 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
1381 size_t count, loff_t *ppos)
1383 struct es2_ap_dev *es2 = f->f_inode->i_private;
1384 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
1387 sprintf(tmp_buf, "%d\n", enable);
1388 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
1391 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
1392 size_t count, loff_t *ppos)
1396 struct es2_ap_dev *es2 = f->f_inode->i_private;
1398 retval = kstrtoint_from_user(buf, count, 10, &enable);
1403 usb_log_enable(es2);
1405 usb_log_disable(es2);
1410 static const struct file_operations apb_log_enable_fops = {
1411 .read = apb_log_enable_read,
1412 .write = apb_log_enable_write,
1415 static int apb_get_cport_count(struct usb_device *udev)
1418 __le16 *cport_count;
1420 cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
1424 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1425 GB_APB_REQUEST_CPORT_COUNT,
1426 USB_DIR_IN | USB_TYPE_VENDOR |
1427 USB_RECIP_INTERFACE, 0, 0, cport_count,
1428 sizeof(*cport_count), ES2_USB_CTRL_TIMEOUT);
1429 if (retval != sizeof(*cport_count)) {
1430 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1439 retval = le16_to_cpu(*cport_count);
1441 /* We need to fit a CPort ID in one byte of a message header */
1442 if (retval > U8_MAX) {
1444 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
1453 * The ES2 USB Bridge device has 15 endpoints
1454 * 1 Control - usual USB stuff + AP -> APBridgeA messages
1455 * 7 Bulk IN - CPort data in
1456 * 7 Bulk OUT - CPort data out
1458 static int ap_probe(struct usb_interface *interface,
1459 const struct usb_device_id *id)
1461 struct es2_ap_dev *es2;
1462 struct gb_host_device *hd;
1463 struct usb_device *udev;
1464 struct usb_host_interface *iface_desc;
1465 struct usb_endpoint_descriptor *endpoint;
1472 udev = usb_get_dev(interface_to_usbdev(interface));
1474 num_cports = apb_get_cport_count(udev);
1475 if (num_cports < 0) {
1477 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1482 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
1489 es2 = hd_to_es2(hd);
1491 es2->usb_intf = interface;
1492 es2->usb_dev = udev;
1493 spin_lock_init(&es2->cport_out_urb_lock);
1494 INIT_KFIFO(es2->apb_log_fifo);
1495 usb_set_intfdata(interface, es2);
1498 * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
1501 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
1504 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
1508 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
1510 if (!es2->cport_to_ep) {
1515 /* find all bulk endpoints */
1516 iface_desc = interface->cur_altsetting;
1517 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1518 endpoint = &iface_desc->endpoint[i].desc;
1520 if (usb_endpoint_is_bulk_in(endpoint)) {
1521 if (bulk_in < NUM_BULKS)
1522 es2->cport_in[bulk_in].endpoint =
1523 endpoint->bEndpointAddress;
1525 es2->arpc_endpoint_in =
1526 endpoint->bEndpointAddress;
1528 } else if (usb_endpoint_is_bulk_out(endpoint)) {
1529 es2->cport_out[bulk_out++].endpoint =
1530 endpoint->bEndpointAddress;
1533 "Unknown endpoint type found, address 0x%02x\n",
1534 endpoint->bEndpointAddress);
1537 if (bulk_in != NUM_BULKS_IN || bulk_out != NUM_BULKS_OUT) {
1538 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1543 /* Allocate buffers for our cport in messages */
1544 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1545 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1547 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1551 urb = usb_alloc_urb(0, GFP_KERNEL);
1556 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1562 usb_fill_bulk_urb(urb, udev,
1563 usb_rcvbulkpipe(udev,
1564 cport_in->endpoint),
1565 buffer, ES2_GBUF_MSG_SIZE_MAX,
1566 cport_in_callback, hd);
1567 cport_in->urb[i] = urb;
1568 cport_in->buffer[i] = buffer;
1572 /* Allocate buffers for ARPC in messages */
1573 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
1577 urb = usb_alloc_urb(0, GFP_KERNEL);
1582 buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL);
1588 usb_fill_bulk_urb(urb, udev,
1589 usb_rcvbulkpipe(udev,
1590 es2->arpc_endpoint_in),
1591 buffer, ARPC_IN_SIZE_MAX,
1592 arpc_in_callback, es2);
1594 es2->arpc_urb[i] = urb;
1595 es2->arpc_buffer[i] = buffer;
1598 /* Allocate urbs for our CPort OUT messages */
1599 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1602 urb = usb_alloc_urb(0, GFP_KERNEL);
1608 es2->cport_out_urb[i] = urb;
1609 es2->cport_out_urb_busy[i] = false; /* just to be anal */
1612 /* XXX We will need to rename this per APB */
1613 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1614 (S_IWUSR | S_IRUGO),
1615 gb_debugfs_get(), es2,
1616 &apb_log_enable_fops);
1618 INIT_LIST_HEAD(&es2->arpcs);
1619 spin_lock_init(&es2->arpc_lock);
1621 if (es2_arpc_in_enable(es2))
1624 retval = gb_hd_add(hd);
1626 goto err_disable_arpc_in;
1628 for (i = 0; i < NUM_BULKS; ++i) {
1629 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1631 goto err_disable_cport_in;
1636 err_disable_cport_in:
1637 for (--i; i >= 0; --i)
1638 es2_cport_in_disable(es2, &es2->cport_in[i]);
1640 err_disable_arpc_in:
1641 es2_arpc_in_disable(es2);
1648 static void ap_disconnect(struct usb_interface *interface)
1650 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1655 for (i = 0; i < NUM_BULKS; ++i)
1656 es2_cport_in_disable(es2, &es2->cport_in[i]);
1657 es2_arpc_in_disable(es2);
1662 static struct usb_driver es2_ap_driver = {
1663 .name = "es2_ap_driver",
1665 .disconnect = ap_disconnect,
1666 .id_table = id_table,
1670 module_usb_driver(es2_ap_driver);
1672 MODULE_LICENSE("GPL v2");
1673 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");