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 <asm/unaligned.h>
17 #include "greybus_trace.h"
18 #include "kernel_ver.h"
19 #include "connection.h"
21 /* Fixed CPort numbers */
22 #define ES2_CPORT_CDSI0 16
23 #define ES2_CPORT_CDSI1 17
25 /* Memory sizes for the buffers sent to/from the ES2 controller */
26 #define ES2_GBUF_MSG_SIZE_MAX 2048
28 /* Memory sizes for the ARPC buffers */
29 #define ARPC_IN_SIZE_MAX 128
31 static const struct usb_device_id id_table[] = {
32 { USB_DEVICE(0x18d1, 0x1eaf) },
35 MODULE_DEVICE_TABLE(usb, id_table);
37 #define APB1_LOG_SIZE SZ_16K
39 /* Number of bulk in and bulk out couple */
42 /* Expected number of bulk out endpoints */
43 #define NUM_BULKS_OUT NUM_BULKS
45 /* Expected number of bulk in endpoints (including ARPC endpoint) */
46 #define NUM_BULKS_IN (NUM_BULKS + 1)
49 * Number of CPort IN urbs in flight at any point in time.
50 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
53 #define NUM_CPORT_IN_URB 4
55 /* Number of CPort OUT urbs in flight at any point in time.
56 * Adjust if we get messages saying we are out of urbs in the system log.
58 #define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
61 * Number of ARPC in urbs in flight at any point in time.
63 #define NUM_ARPC_IN_URB 2
66 * @endpoint: bulk in endpoint for CPort data
67 * @urb: array of urbs for the CPort in messages
68 * @buffer: array of buffers for the @cport_in_urb urbs
72 struct urb *urb[NUM_CPORT_IN_URB];
73 u8 *buffer[NUM_CPORT_IN_URB];
77 * @endpoint: bulk out endpoint for CPort data
79 struct es2_cport_out {
84 * es2_ap_dev - ES2 USB Bridge to AP structure
85 * @usb_dev: pointer to the USB device we are.
86 * @usb_intf: pointer to the USB interface we are bound to.
87 * @hd: pointer to our gb_host_device structure
89 * @cport_in: endpoint, urbs and buffer for cport in messages
90 * @cport_out: endpoint for for cport out messages
91 * @cport_out_urb: array of urbs for the CPort out messages
92 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
94 * @cport_out_urb_cancelled: array of flags indicating whether the
95 * corresponding @cport_out_urb is being cancelled
96 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
98 * @apb_log_task: task pointer for logging thread
99 * @apb_log_dentry: file system entry for the log file interface
100 * @apb_log_enable_dentry: file system entry for enabling logging
101 * @apb_log_fifo: kernel FIFO to carry logged data
102 * @arpc_urb: array of urbs for the ARPC in messages
103 * @arpc_buffer: array of buffers for the @arpc_urb urbs
104 * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC
107 struct usb_device *usb_dev;
108 struct usb_interface *usb_intf;
109 struct gb_host_device *hd;
111 struct es2_cport_in cport_in[NUM_BULKS];
112 struct es2_cport_out cport_out[NUM_BULKS];
113 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
114 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
115 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
116 spinlock_t cport_out_urb_lock;
122 struct task_struct *apb_log_task;
123 struct dentry *apb_log_dentry;
124 struct dentry *apb_log_enable_dentry;
125 DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
127 __u8 arpc_endpoint_in;
128 struct urb *arpc_urb[NUM_ARPC_IN_URB];
129 u8 *arpc_buffer[NUM_ARPC_IN_URB];
133 * cport_to_ep - information about cport to endpoints mapping
134 * @cport_id: the id of cport to map to endpoints
135 * @endpoint_in: the endpoint number to use for in transfer
136 * @endpoint_out: he endpoint number to use for out transfer
145 * timesync_enable_request - Enable timesync in an APBridge
146 * @count: number of TimeSync Pulses to expect
147 * @frame_time: the initial FrameTime at the first TimeSync Pulse
148 * @strobe_delay: the expected delay in microseconds between each TimeSync Pulse
149 * @refclk: The AP mandated reference clock to run FrameTime at
151 struct timesync_enable_request {
159 * timesync_authoritative_request - Transmit authoritative FrameTime to APBridge
160 * @frame_time: An array of authoritative FrameTimes provided by the SVC
161 * and relayed to the APBridge by the AP
163 struct timesync_authoritative_request {
164 __le64 frame_time[GB_TIMESYNC_MAX_STROBES];
167 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
169 return (struct es2_ap_dev *)&hd->hd_priv;
172 static void cport_out_callback(struct urb *urb);
173 static void usb_log_enable(struct es2_ap_dev *es2);
174 static void usb_log_disable(struct es2_ap_dev *es2);
176 /* Get the endpoints pair mapped to the cport */
177 static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id)
179 if (cport_id >= es2->hd->num_cports)
181 return es2->cport_to_ep[cport_id];
184 #define ES2_TIMEOUT 500 /* 500 ms for the SVC to do something */
186 /* Disable for now until we work all of this out to keep a warning-free build */
188 /* Test if the endpoints pair is already mapped to a cport */
189 static int ep_pair_in_use(struct es2_ap_dev *es2, int ep_pair)
193 for (i = 0; i < es2->hd->num_cports; i++) {
194 if (es2->cport_to_ep[i] == ep_pair)
200 /* Configure the endpoint mapping and send the request to APBridge */
201 static int map_cport_to_ep(struct es2_ap_dev *es2,
202 u16 cport_id, int ep_pair)
205 struct cport_to_ep *cport_to_ep;
207 if (ep_pair < 0 || ep_pair >= NUM_BULKS)
209 if (cport_id >= es2->hd->num_cports)
211 if (ep_pair && ep_pair_in_use(es2, ep_pair))
214 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
218 es2->cport_to_ep[cport_id] = ep_pair;
219 cport_to_ep->cport_id = cpu_to_le16(cport_id);
220 cport_to_ep->endpoint_in = es2->cport_in[ep_pair].endpoint;
221 cport_to_ep->endpoint_out = es2->cport_out[ep_pair].endpoint;
223 retval = usb_control_msg(es2->usb_dev,
224 usb_sndctrlpipe(es2->usb_dev, 0),
225 GB_APB_REQUEST_EP_MAPPING,
226 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
229 sizeof(*cport_to_ep),
231 if (retval == sizeof(*cport_to_ep))
238 /* Unmap a cport: use the muxed endpoints pair */
239 static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id)
241 return map_cport_to_ep(es2, cport_id, 0);
245 static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
247 struct usb_device *udev = es2->usb_dev;
251 data = kmalloc(size, GFP_KERNEL);
254 memcpy(data, req, size);
256 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
258 USB_DIR_OUT | USB_TYPE_VENDOR |
260 0, 0, data, size, ES2_TIMEOUT);
262 dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
270 static void ap_urb_complete(struct urb *urb)
272 struct usb_ctrlrequest *dr = urb->context;
278 static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
280 struct usb_device *udev = es2->usb_dev;
282 struct usb_ctrlrequest *dr;
286 urb = usb_alloc_urb(0, GFP_ATOMIC);
290 dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
296 buf = (u8 *)dr + sizeof(*dr);
297 memcpy(buf, req, size);
300 dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
303 dr->wLength = cpu_to_le16(size);
305 usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
306 (unsigned char *)dr, buf, size,
307 ap_urb_complete, dr);
308 retval = usb_submit_urb(urb, GFP_ATOMIC);
316 static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
319 struct es2_ap_dev *es2 = hd_to_es2(hd);
322 return output_async(es2, req, size, cmd);
324 return output_sync(es2, req, size, cmd);
327 static int es2_cport_in_enable(struct es2_ap_dev *es2,
328 struct es2_cport_in *cport_in)
334 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
335 urb = cport_in->urb[i];
337 ret = usb_submit_urb(urb, GFP_KERNEL);
339 dev_err(&es2->usb_dev->dev,
340 "failed to submit in-urb: %d\n", ret);
348 for (--i; i >= 0; --i) {
349 urb = cport_in->urb[i];
356 static void es2_cport_in_disable(struct es2_ap_dev *es2,
357 struct es2_cport_in *cport_in)
362 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
363 urb = cport_in->urb[i];
368 static int es2_arpc_in_enable(struct es2_ap_dev *es2)
374 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
375 urb = es2->arpc_urb[i];
377 ret = usb_submit_urb(urb, GFP_KERNEL);
379 dev_err(&es2->usb_dev->dev,
380 "failed to submit arpc in-urb: %d\n", ret);
388 for (--i; i >= 0; --i) {
389 urb = es2->arpc_urb[i];
396 static void es2_arpc_in_disable(struct es2_ap_dev *es2)
401 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
402 urb = es2->arpc_urb[i];
407 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
409 struct urb *urb = NULL;
413 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
415 /* Look in our pool of allocated urbs first, as that's the "fastest" */
416 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
417 if (es2->cport_out_urb_busy[i] == false &&
418 es2->cport_out_urb_cancelled[i] == false) {
419 es2->cport_out_urb_busy[i] = true;
420 urb = es2->cport_out_urb[i];
424 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
429 * Crap, pool is empty, complain to the syslog and go allocate one
430 * dynamically as we have to succeed.
432 dev_dbg(&es2->usb_dev->dev,
433 "No free CPort OUT urbs, having to dynamically allocate one!\n");
434 return usb_alloc_urb(0, gfp_mask);
437 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
442 * See if this was an urb in our pool, if so mark it "free", otherwise
443 * we need to free it ourselves.
445 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
446 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
447 if (urb == es2->cport_out_urb[i]) {
448 es2->cport_out_urb_busy[i] = false;
453 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
455 /* If urb is not NULL, then we need to free this urb */
460 * We (ab)use the operation-message header pad bytes to transfer the
461 * cport id in order to minimise overhead.
464 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
466 header->pad[0] = cport_id;
469 /* Clear the pad bytes used for the CPort id */
470 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
475 /* Extract the CPort id packed into the header, and clear it */
476 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
478 u16 cport_id = header->pad[0];
480 gb_message_cport_clear(header);
486 * Returns zero if the message was successfully queued, or a negative errno
489 static int message_send(struct gb_host_device *hd, u16 cport_id,
490 struct gb_message *message, gfp_t gfp_mask)
492 struct es2_ap_dev *es2 = hd_to_es2(hd);
493 struct usb_device *udev = es2->usb_dev;
501 * The data actually transferred will include an indication
502 * of where the data should be sent. Do one last check of
503 * the target CPort id before filling it in.
505 if (!cport_id_valid(hd, cport_id)) {
506 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
510 /* Find a free urb */
511 urb = next_free_urb(es2, gfp_mask);
515 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
516 message->hcpriv = urb;
517 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
519 /* Pack the cport id into the message header */
520 gb_message_cport_pack(message->header, cport_id);
522 buffer_size = sizeof(*message->header) + message->payload_size;
524 ep_pair = cport_to_ep_pair(es2, cport_id);
525 usb_fill_bulk_urb(urb, udev,
526 usb_sndbulkpipe(udev,
527 es2->cport_out[ep_pair].endpoint),
528 message->buffer, buffer_size,
529 cport_out_callback, message);
530 urb->transfer_flags |= URB_ZERO_PACKET;
532 trace_gb_message_submit(message);
534 retval = usb_submit_urb(urb, gfp_mask);
536 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
538 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
539 message->hcpriv = NULL;
540 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
543 gb_message_cport_clear(message->header);
552 * Can not be called in atomic context.
554 static void message_cancel(struct gb_message *message)
556 struct gb_host_device *hd = message->operation->connection->hd;
557 struct es2_ap_dev *es2 = hd_to_es2(hd);
563 spin_lock_irq(&es2->cport_out_urb_lock);
564 urb = message->hcpriv;
566 /* Prevent dynamically allocated urb from being deallocated. */
569 /* Prevent pre-allocated urb from being reused. */
570 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
571 if (urb == es2->cport_out_urb[i]) {
572 es2->cport_out_urb_cancelled[i] = true;
576 spin_unlock_irq(&es2->cport_out_urb_lock);
580 if (i < NUM_CPORT_OUT_URB) {
581 spin_lock_irq(&es2->cport_out_urb_lock);
582 es2->cport_out_urb_cancelled[i] = false;
583 spin_unlock_irq(&es2->cport_out_urb_lock);
589 static int cport_reset(struct gb_host_device *hd, u16 cport_id)
591 struct es2_ap_dev *es2 = hd_to_es2(hd);
592 struct usb_device *udev = es2->usb_dev;
596 case GB_SVC_CPORT_ID:
597 case ES2_CPORT_CDSI0:
598 case ES2_CPORT_CDSI1:
602 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
603 GB_APB_REQUEST_RESET_CPORT,
604 USB_DIR_OUT | USB_TYPE_VENDOR |
605 USB_RECIP_INTERFACE, cport_id, 0,
606 NULL, 0, ES2_TIMEOUT);
608 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
616 static int es2_cport_allocate(struct gb_host_device *hd, int cport_id,
619 struct es2_ap_dev *es2 = hd_to_es2(hd);
620 struct ida *id_map = &hd->cport_id_map;
621 int ida_start, ida_end;
624 case ES2_CPORT_CDSI0:
625 case ES2_CPORT_CDSI1:
626 dev_err(&hd->dev, "cport %d not available\n", cport_id);
630 if (flags & GB_CONNECTION_FLAG_OFFLOADED &&
631 flags & GB_CONNECTION_FLAG_CDSI1) {
632 if (es2->cdsi1_in_use) {
633 dev_err(&hd->dev, "CDSI1 already in use\n");
637 es2->cdsi1_in_use = true;
639 return ES2_CPORT_CDSI1;
644 ida_end = hd->num_cports;
645 } else if (cport_id < hd->num_cports) {
646 ida_start = cport_id;
647 ida_end = cport_id + 1;
649 dev_err(&hd->dev, "cport %d not available\n", cport_id);
653 return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
656 static void es2_cport_release(struct gb_host_device *hd, u16 cport_id)
658 struct es2_ap_dev *es2 = hd_to_es2(hd);
661 case ES2_CPORT_CDSI1:
662 es2->cdsi1_in_use = false;
666 ida_simple_remove(&hd->cport_id_map, cport_id);
669 static int cport_enable(struct gb_host_device *hd, u16 cport_id,
672 struct es2_ap_dev *es2 = hd_to_es2(hd);
673 struct usb_device *udev = es2->usb_dev;
674 struct gb_apb_request_cport_flags *req;
677 req = kzalloc(sizeof(*req), GFP_KERNEL);
681 if (flags & GB_CONNECTION_FLAG_CONTROL)
682 req->flags |= GB_APB_CPORT_FLAG_CONTROL;
683 if (flags & GB_CONNECTION_FLAG_HIGH_PRIO)
684 req->flags |= GB_APB_CPORT_FLAG_HIGH_PRIO;
686 dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__,
687 cport_id, req->flags);
689 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
690 GB_APB_REQUEST_CPORT_FLAGS,
691 USB_DIR_OUT | USB_TYPE_VENDOR |
692 USB_RECIP_INTERFACE, cport_id, 0,
693 req, sizeof(*req), ES2_TIMEOUT);
694 if (ret != sizeof(*req)) {
695 dev_err(&udev->dev, "failed to set cport flags for port %d\n",
710 static int cport_disable(struct gb_host_device *hd, u16 cport_id)
714 retval = cport_reset(hd, cport_id);
721 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
724 struct es2_ap_dev *es2 = hd_to_es2(hd);
725 struct usb_device *udev = es2->usb_dev;
727 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
728 GB_APB_REQUEST_LATENCY_TAG_EN,
729 USB_DIR_OUT | USB_TYPE_VENDOR |
730 USB_RECIP_INTERFACE, cport_id, 0, NULL,
734 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
739 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
742 struct es2_ap_dev *es2 = hd_to_es2(hd);
743 struct usb_device *udev = es2->usb_dev;
745 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
746 GB_APB_REQUEST_LATENCY_TAG_DIS,
747 USB_DIR_OUT | USB_TYPE_VENDOR |
748 USB_RECIP_INTERFACE, cport_id, 0, NULL,
752 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
757 static int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
760 struct es2_ap_dev *es2 = hd_to_es2(hd);
761 struct usb_device *udev = es2->usb_dev;
763 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
764 GB_APB_REQUEST_CPORT_FEAT_EN,
765 USB_DIR_OUT | USB_TYPE_VENDOR |
766 USB_RECIP_INTERFACE, cport_id, 0, NULL,
769 dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
774 static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
777 struct es2_ap_dev *es2 = hd_to_es2(hd);
778 struct usb_device *udev = es2->usb_dev;
780 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
781 GB_APB_REQUEST_CPORT_FEAT_DIS,
782 USB_DIR_OUT | USB_TYPE_VENDOR |
783 USB_RECIP_INTERFACE, cport_id, 0, NULL,
787 "Cannot disable CPort features for cport %u: %d\n",
792 static int timesync_enable(struct gb_host_device *hd, u8 count,
793 u64 frame_time, u32 strobe_delay, u32 refclk)
796 struct es2_ap_dev *es2 = hd_to_es2(hd);
797 struct usb_device *udev = es2->usb_dev;
798 struct gb_control_timesync_enable_request *request;
800 request = kzalloc(sizeof(*request), GFP_KERNEL);
804 request->count = count;
805 request->frame_time = cpu_to_le64(frame_time);
806 request->strobe_delay = cpu_to_le32(strobe_delay);
807 request->refclk = cpu_to_le32(refclk);
808 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
809 REQUEST_TIMESYNC_ENABLE,
810 USB_DIR_OUT | USB_TYPE_VENDOR |
811 USB_RECIP_INTERFACE, 0, 0, request,
812 sizeof(*request), ES2_TIMEOUT);
814 dev_err(&udev->dev, "Cannot enable timesync %d\n", retval);
820 static int timesync_disable(struct gb_host_device *hd)
823 struct es2_ap_dev *es2 = hd_to_es2(hd);
824 struct usb_device *udev = es2->usb_dev;
826 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
827 REQUEST_TIMESYNC_DISABLE,
828 USB_DIR_OUT | USB_TYPE_VENDOR |
829 USB_RECIP_INTERFACE, 0, 0, NULL,
832 dev_err(&udev->dev, "Cannot disable timesync %d\n", retval);
837 static int timesync_authoritative(struct gb_host_device *hd, u64 *frame_time)
840 struct es2_ap_dev *es2 = hd_to_es2(hd);
841 struct usb_device *udev = es2->usb_dev;
842 struct timesync_authoritative_request *request;
844 request = kzalloc(sizeof(*request), GFP_KERNEL);
848 for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
849 request->frame_time[i] = cpu_to_le64(frame_time[i]);
851 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
852 REQUEST_TIMESYNC_AUTHORITATIVE,
853 USB_DIR_OUT | USB_TYPE_VENDOR |
854 USB_RECIP_INTERFACE, 0, 0, request,
855 sizeof(*request), ES2_TIMEOUT);
857 dev_err(&udev->dev, "Cannot timesync authoritative out %d\n", retval);
863 static int timesync_get_last_event(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 __le64 *response_frame_time;
870 response_frame_time = kzalloc(sizeof(*response_frame_time), GFP_KERNEL);
871 if (!response_frame_time)
874 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
875 REQUEST_TIMESYNC_GET_LAST_EVENT,
876 USB_DIR_IN | USB_TYPE_VENDOR |
877 USB_RECIP_INTERFACE, 0, 0, response_frame_time,
878 sizeof(*response_frame_time), ES2_TIMEOUT);
880 if (retval != sizeof(*response_frame_time)) {
881 dev_err(&udev->dev, "Cannot get last TimeSync event: %d\n",
889 *frame_time = le64_to_cpu(*response_frame_time);
892 kfree(response_frame_time);
896 static struct gb_hd_driver es2_driver = {
897 .hd_priv_size = sizeof(struct es2_ap_dev),
898 .message_send = message_send,
899 .message_cancel = message_cancel,
900 .cport_allocate = es2_cport_allocate,
901 .cport_release = es2_cport_release,
902 .cport_enable = cport_enable,
903 .cport_disable = cport_disable,
904 .latency_tag_enable = latency_tag_enable,
905 .latency_tag_disable = latency_tag_disable,
907 .cport_features_enable = cport_features_enable,
908 .cport_features_disable = cport_features_disable,
909 .timesync_enable = timesync_enable,
910 .timesync_disable = timesync_disable,
911 .timesync_authoritative = timesync_authoritative,
912 .timesync_get_last_event = timesync_get_last_event,
915 /* Common function to report consistent warnings based on URB status */
916 static int check_urb_status(struct urb *urb)
918 struct device *dev = &urb->dev->dev;
919 int status = urb->status;
926 dev_err(dev, "%s: overflow actual length is %d\n",
927 __func__, urb->actual_length);
933 /* device is gone, stop sending */
936 dev_err(dev, "%s: unknown status %d\n", __func__, status);
941 static void es2_destroy(struct es2_ap_dev *es2)
943 struct usb_device *udev;
947 debugfs_remove(es2->apb_log_enable_dentry);
948 usb_log_disable(es2);
950 /* Tear down everything! */
951 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
952 struct urb *urb = es2->cport_out_urb[i];
958 es2->cport_out_urb[i] = NULL;
959 es2->cport_out_urb_busy[i] = false; /* just to be anal */
962 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
963 struct urb *urb = es2->arpc_urb[i];
968 kfree(es2->arpc_buffer[i]);
969 es2->arpc_buffer[i] = NULL;
972 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
973 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
975 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
976 struct urb *urb = cport_in->urb[i];
981 kfree(cport_in->buffer[i]);
982 cport_in->buffer[i] = NULL;
986 kfree(es2->cport_to_ep);
988 /* release reserved CDSI0 and CDSI1 cports */
989 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
990 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);
998 static void cport_in_callback(struct urb *urb)
1000 struct gb_host_device *hd = urb->context;
1001 struct device *dev = &urb->dev->dev;
1002 struct gb_operation_msg_hdr *header;
1003 int status = check_urb_status(urb);
1008 if ((status == -EAGAIN) || (status == -EPROTO))
1011 /* The urb is being unlinked */
1012 if (status == -ENOENT || status == -ESHUTDOWN)
1015 dev_err(dev, "urb cport in error %d (dropped)\n", status);
1019 if (urb->actual_length < sizeof(*header)) {
1020 dev_err(dev, "short message received\n");
1024 /* Extract the CPort id, which is packed in the message header */
1025 header = urb->transfer_buffer;
1026 cport_id = gb_message_cport_unpack(header);
1028 if (cport_id_valid(hd, cport_id)) {
1029 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
1030 urb->actual_length);
1032 dev_err(dev, "invalid cport id %u received\n", cport_id);
1035 /* put our urb back in the request pool */
1036 retval = usb_submit_urb(urb, GFP_ATOMIC);
1038 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
1041 static void cport_out_callback(struct urb *urb)
1043 struct gb_message *message = urb->context;
1044 struct gb_host_device *hd = message->operation->connection->hd;
1045 struct es2_ap_dev *es2 = hd_to_es2(hd);
1046 int status = check_urb_status(urb);
1047 unsigned long flags;
1049 gb_message_cport_clear(message->header);
1051 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
1052 message->hcpriv = NULL;
1053 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
1056 * Tell the submitter that the message send (attempt) is
1057 * complete, and report the status.
1059 greybus_message_sent(hd, message, status);
1064 static void arpc_in_callback(struct urb *urb)
1066 struct device *dev = &urb->dev->dev;
1067 int status = check_urb_status(urb);
1071 if ((status == -EAGAIN) || (status == -EPROTO))
1074 /* The urb is being unlinked */
1075 if (status == -ENOENT || status == -ESHUTDOWN)
1078 dev_err(dev, "arpc in-urb error %d (dropped)\n", status);
1083 /* put our urb back in the request pool */
1084 retval = usb_submit_urb(urb, GFP_ATOMIC);
1086 dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval);
1089 #define APB1_LOG_MSG_SIZE 64
1090 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
1094 /* SVC messages go down our control pipe */
1096 retval = usb_control_msg(es2->usb_dev,
1097 usb_rcvctrlpipe(es2->usb_dev, 0),
1099 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1105 kfifo_in(&es2->apb_log_fifo, buf, retval);
1106 } while (retval > 0);
1109 static int apb_log_poll(void *data)
1111 struct es2_ap_dev *es2 = data;
1114 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
1118 while (!kthread_should_stop()) {
1120 apb_log_get(es2, buf);
1128 static ssize_t apb_log_read(struct file *f, char __user *buf,
1129 size_t count, loff_t *ppos)
1131 struct es2_ap_dev *es2 = f->f_inode->i_private;
1136 if (count > APB1_LOG_SIZE)
1137 count = APB1_LOG_SIZE;
1139 tmp_buf = kmalloc(count, GFP_KERNEL);
1143 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
1144 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
1151 static const struct file_operations apb_log_fops = {
1152 .read = apb_log_read,
1155 static void usb_log_enable(struct es2_ap_dev *es2)
1157 if (!IS_ERR_OR_NULL(es2->apb_log_task))
1160 /* get log from APB1 */
1161 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
1162 if (IS_ERR(es2->apb_log_task))
1164 /* XXX We will need to rename this per APB */
1165 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
1166 gb_debugfs_get(), es2,
1170 static void usb_log_disable(struct es2_ap_dev *es2)
1172 if (IS_ERR_OR_NULL(es2->apb_log_task))
1175 debugfs_remove(es2->apb_log_dentry);
1176 es2->apb_log_dentry = NULL;
1178 kthread_stop(es2->apb_log_task);
1179 es2->apb_log_task = NULL;
1182 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
1183 size_t count, loff_t *ppos)
1185 struct es2_ap_dev *es2 = f->f_inode->i_private;
1186 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
1189 sprintf(tmp_buf, "%d\n", enable);
1190 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
1193 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
1194 size_t count, loff_t *ppos)
1198 struct es2_ap_dev *es2 = f->f_inode->i_private;
1200 retval = kstrtoint_from_user(buf, count, 10, &enable);
1205 usb_log_enable(es2);
1207 usb_log_disable(es2);
1212 static const struct file_operations apb_log_enable_fops = {
1213 .read = apb_log_enable_read,
1214 .write = apb_log_enable_write,
1217 static int apb_get_cport_count(struct usb_device *udev)
1220 __le16 *cport_count;
1222 cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
1226 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1227 GB_APB_REQUEST_CPORT_COUNT,
1228 USB_DIR_IN | USB_TYPE_VENDOR |
1229 USB_RECIP_INTERFACE, 0, 0, cport_count,
1230 sizeof(*cport_count), ES2_TIMEOUT);
1231 if (retval != sizeof(*cport_count)) {
1232 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1241 retval = le16_to_cpu(*cport_count);
1243 /* We need to fit a CPort ID in one byte of a message header */
1244 if (retval > U8_MAX) {
1246 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
1255 * The ES2 USB Bridge device has 15 endpoints
1256 * 1 Control - usual USB stuff + AP -> APBridgeA messages
1257 * 7 Bulk IN - CPort data in
1258 * 7 Bulk OUT - CPort data out
1260 static int ap_probe(struct usb_interface *interface,
1261 const struct usb_device_id *id)
1263 struct es2_ap_dev *es2;
1264 struct gb_host_device *hd;
1265 struct usb_device *udev;
1266 struct usb_host_interface *iface_desc;
1267 struct usb_endpoint_descriptor *endpoint;
1274 udev = usb_get_dev(interface_to_usbdev(interface));
1276 num_cports = apb_get_cport_count(udev);
1277 if (num_cports < 0) {
1279 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1284 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
1291 es2 = hd_to_es2(hd);
1293 es2->usb_intf = interface;
1294 es2->usb_dev = udev;
1295 spin_lock_init(&es2->cport_out_urb_lock);
1296 INIT_KFIFO(es2->apb_log_fifo);
1297 usb_set_intfdata(interface, es2);
1300 * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
1303 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
1306 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
1310 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
1312 if (!es2->cport_to_ep) {
1317 /* find all bulk endpoints */
1318 iface_desc = interface->cur_altsetting;
1319 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1320 endpoint = &iface_desc->endpoint[i].desc;
1322 if (usb_endpoint_is_bulk_in(endpoint)) {
1323 if (bulk_in < NUM_BULKS)
1324 es2->cport_in[bulk_in].endpoint =
1325 endpoint->bEndpointAddress;
1327 es2->arpc_endpoint_in =
1328 endpoint->bEndpointAddress;
1330 } else if (usb_endpoint_is_bulk_out(endpoint)) {
1331 es2->cport_out[bulk_out++].endpoint =
1332 endpoint->bEndpointAddress;
1335 "Unknown endpoint type found, address 0x%02x\n",
1336 endpoint->bEndpointAddress);
1339 if (bulk_in != NUM_BULKS_IN || bulk_out != NUM_BULKS_OUT) {
1340 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1345 /* Allocate buffers for our cport in messages */
1346 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1347 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1349 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1353 urb = usb_alloc_urb(0, GFP_KERNEL);
1358 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1364 usb_fill_bulk_urb(urb, udev,
1365 usb_rcvbulkpipe(udev,
1366 cport_in->endpoint),
1367 buffer, ES2_GBUF_MSG_SIZE_MAX,
1368 cport_in_callback, hd);
1369 cport_in->urb[i] = urb;
1370 cport_in->buffer[i] = buffer;
1374 /* Allocate buffers for ARPC in messages */
1375 for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
1379 urb = usb_alloc_urb(0, GFP_KERNEL);
1384 buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL);
1390 usb_fill_bulk_urb(urb, udev,
1391 usb_rcvbulkpipe(udev,
1392 es2->arpc_endpoint_in),
1393 buffer, ARPC_IN_SIZE_MAX,
1394 arpc_in_callback, es2);
1396 es2->arpc_urb[i] = urb;
1397 es2->arpc_buffer[i] = buffer;
1400 /* Allocate urbs for our CPort OUT messages */
1401 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1404 urb = usb_alloc_urb(0, GFP_KERNEL);
1410 es2->cport_out_urb[i] = urb;
1411 es2->cport_out_urb_busy[i] = false; /* just to be anal */
1414 /* XXX We will need to rename this per APB */
1415 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1416 (S_IWUSR | S_IRUGO),
1417 gb_debugfs_get(), es2,
1418 &apb_log_enable_fops);
1420 if (es2_arpc_in_enable(es2))
1423 retval = gb_hd_add(hd);
1425 goto err_disable_arpc_in;
1427 for (i = 0; i < NUM_BULKS; ++i) {
1428 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1430 goto err_disable_cport_in;
1435 err_disable_cport_in:
1436 for (--i; i >= 0; --i)
1437 es2_cport_in_disable(es2, &es2->cport_in[i]);
1439 err_disable_arpc_in:
1440 es2_arpc_in_disable(es2);
1447 static void ap_disconnect(struct usb_interface *interface)
1449 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1454 for (i = 0; i < NUM_BULKS; ++i)
1455 es2_cport_in_disable(es2, &es2->cport_in[i]);
1456 es2_arpc_in_disable(es2);
1461 static struct usb_driver es2_ap_driver = {
1462 .name = "es2_ap_driver",
1464 .disconnect = ap_disconnect,
1465 .id_table = id_table,
1469 module_usb_driver(es2_ap_driver);
1471 MODULE_LICENSE("GPL v2");
1472 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");