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 static const struct usb_device_id id_table[] = {
29 { USB_DEVICE(0x18d1, 0x1eaf) },
32 MODULE_DEVICE_TABLE(usb, id_table);
34 #define APB1_LOG_SIZE SZ_16K
36 /* Number of bulk in and bulk out couple */
40 * Number of CPort IN urbs in flight at any point in time.
41 * Adjust if we are having stalls in the USB buffer due to not enough urbs in
44 #define NUM_CPORT_IN_URB 4
46 /* Number of CPort OUT urbs in flight at any point in time.
47 * Adjust if we get messages saying we are out of urbs in the system log.
49 #define NUM_CPORT_OUT_URB (8 * NUM_BULKS)
52 * @endpoint: bulk in endpoint for CPort data
53 * @urb: array of urbs for the CPort in messages
54 * @buffer: array of buffers for the @cport_in_urb urbs
58 struct urb *urb[NUM_CPORT_IN_URB];
59 u8 *buffer[NUM_CPORT_IN_URB];
63 * @endpoint: bulk out endpoint for CPort data
65 struct es2_cport_out {
70 * es2_ap_dev - ES2 USB Bridge to AP structure
71 * @usb_dev: pointer to the USB device we are.
72 * @usb_intf: pointer to the USB interface we are bound to.
73 * @hd: pointer to our gb_host_device structure
75 * @cport_in: endpoint, urbs and buffer for cport in messages
76 * @cport_out: endpoint for for cport out messages
77 * @cport_out_urb: array of urbs for the CPort out messages
78 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
80 * @cport_out_urb_cancelled: array of flags indicating whether the
81 * corresponding @cport_out_urb is being cancelled
82 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
84 * @apb_log_task: task pointer for logging thread
85 * @apb_log_dentry: file system entry for the log file interface
86 * @apb_log_enable_dentry: file system entry for enabling logging
87 * @apb_log_fifo: kernel FIFO to carry logged data
90 struct usb_device *usb_dev;
91 struct usb_interface *usb_intf;
92 struct gb_host_device *hd;
94 struct es2_cport_in cport_in[NUM_BULKS];
95 struct es2_cport_out cport_out[NUM_BULKS];
96 struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
97 bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
98 bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
99 spinlock_t cport_out_urb_lock;
105 struct task_struct *apb_log_task;
106 struct dentry *apb_log_dentry;
107 struct dentry *apb_log_enable_dentry;
108 DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
112 * cport_to_ep - information about cport to endpoints mapping
113 * @cport_id: the id of cport to map to endpoints
114 * @endpoint_in: the endpoint number to use for in transfer
115 * @endpoint_out: he endpoint number to use for out transfer
124 * timesync_enable_request - Enable timesync in an APBridge
125 * @count: number of TimeSync Pulses to expect
126 * @frame_time: the initial FrameTime at the first TimeSync Pulse
127 * @strobe_delay: the expected delay in microseconds between each TimeSync Pulse
128 * @refclk: The AP mandated reference clock to run FrameTime at
130 struct timesync_enable_request {
138 * timesync_authoritative_request - Transmit authoritative FrameTime to APBridge
139 * @frame_time: An array of authoritative FrameTimes provided by the SVC
140 * and relayed to the APBridge by the AP
142 struct timesync_authoritative_request {
143 __le64 frame_time[GB_TIMESYNC_MAX_STROBES];
146 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
148 return (struct es2_ap_dev *)&hd->hd_priv;
151 static void cport_out_callback(struct urb *urb);
152 static void usb_log_enable(struct es2_ap_dev *es2);
153 static void usb_log_disable(struct es2_ap_dev *es2);
155 /* Get the endpoints pair mapped to the cport */
156 static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id)
158 if (cport_id >= es2->hd->num_cports)
160 return es2->cport_to_ep[cport_id];
163 #define ES2_TIMEOUT 500 /* 500 ms for the SVC to do something */
165 /* Disable for now until we work all of this out to keep a warning-free build */
167 /* Test if the endpoints pair is already mapped to a cport */
168 static int ep_pair_in_use(struct es2_ap_dev *es2, int ep_pair)
172 for (i = 0; i < es2->hd->num_cports; i++) {
173 if (es2->cport_to_ep[i] == ep_pair)
179 /* Configure the endpoint mapping and send the request to APBridge */
180 static int map_cport_to_ep(struct es2_ap_dev *es2,
181 u16 cport_id, int ep_pair)
184 struct cport_to_ep *cport_to_ep;
186 if (ep_pair < 0 || ep_pair >= NUM_BULKS)
188 if (cport_id >= es2->hd->num_cports)
190 if (ep_pair && ep_pair_in_use(es2, ep_pair))
193 cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL);
197 es2->cport_to_ep[cport_id] = ep_pair;
198 cport_to_ep->cport_id = cpu_to_le16(cport_id);
199 cport_to_ep->endpoint_in = es2->cport_in[ep_pair].endpoint;
200 cport_to_ep->endpoint_out = es2->cport_out[ep_pair].endpoint;
202 retval = usb_control_msg(es2->usb_dev,
203 usb_sndctrlpipe(es2->usb_dev, 0),
204 GB_APB_REQUEST_EP_MAPPING,
205 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
208 sizeof(*cport_to_ep),
210 if (retval == sizeof(*cport_to_ep))
217 /* Unmap a cport: use the muxed endpoints pair */
218 static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id)
220 return map_cport_to_ep(es2, cport_id, 0);
224 static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
226 struct usb_device *udev = es2->usb_dev;
230 data = kmalloc(size, GFP_KERNEL);
233 memcpy(data, req, size);
235 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
237 USB_DIR_OUT | USB_TYPE_VENDOR |
239 0, 0, data, size, ES2_TIMEOUT);
241 dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
249 static void ap_urb_complete(struct urb *urb)
251 struct usb_ctrlrequest *dr = urb->context;
257 static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
259 struct usb_device *udev = es2->usb_dev;
261 struct usb_ctrlrequest *dr;
265 urb = usb_alloc_urb(0, GFP_ATOMIC);
269 dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
275 buf = (u8 *)dr + sizeof(*dr);
276 memcpy(buf, req, size);
279 dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
282 dr->wLength = cpu_to_le16(size);
284 usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
285 (unsigned char *)dr, buf, size,
286 ap_urb_complete, dr);
287 retval = usb_submit_urb(urb, GFP_ATOMIC);
295 static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
298 struct es2_ap_dev *es2 = hd_to_es2(hd);
301 return output_async(es2, req, size, cmd);
303 return output_sync(es2, req, size, cmd);
306 static int es2_cport_in_enable(struct es2_ap_dev *es2,
307 struct es2_cport_in *cport_in)
313 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
314 urb = cport_in->urb[i];
316 ret = usb_submit_urb(urb, GFP_KERNEL);
318 dev_err(&es2->usb_dev->dev,
319 "failed to submit in-urb: %d\n", ret);
327 for (--i; i >= 0; --i) {
328 urb = cport_in->urb[i];
335 static void es2_cport_in_disable(struct es2_ap_dev *es2,
336 struct es2_cport_in *cport_in)
341 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
342 urb = cport_in->urb[i];
347 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
349 struct urb *urb = NULL;
353 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
355 /* Look in our pool of allocated urbs first, as that's the "fastest" */
356 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
357 if (es2->cport_out_urb_busy[i] == false &&
358 es2->cport_out_urb_cancelled[i] == false) {
359 es2->cport_out_urb_busy[i] = true;
360 urb = es2->cport_out_urb[i];
364 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
369 * Crap, pool is empty, complain to the syslog and go allocate one
370 * dynamically as we have to succeed.
372 dev_dbg(&es2->usb_dev->dev,
373 "No free CPort OUT urbs, having to dynamically allocate one!\n");
374 return usb_alloc_urb(0, gfp_mask);
377 static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
382 * See if this was an urb in our pool, if so mark it "free", otherwise
383 * we need to free it ourselves.
385 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
386 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
387 if (urb == es2->cport_out_urb[i]) {
388 es2->cport_out_urb_busy[i] = false;
393 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
395 /* If urb is not NULL, then we need to free this urb */
400 * We (ab)use the operation-message header pad bytes to transfer the
401 * cport id in order to minimise overhead.
404 gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
406 header->pad[0] = cport_id;
409 /* Clear the pad bytes used for the CPort id */
410 static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
415 /* Extract the CPort id packed into the header, and clear it */
416 static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
418 u16 cport_id = header->pad[0];
420 gb_message_cport_clear(header);
426 * Returns zero if the message was successfully queued, or a negative errno
429 static int message_send(struct gb_host_device *hd, u16 cport_id,
430 struct gb_message *message, gfp_t gfp_mask)
432 struct es2_ap_dev *es2 = hd_to_es2(hd);
433 struct usb_device *udev = es2->usb_dev;
441 * The data actually transferred will include an indication
442 * of where the data should be sent. Do one last check of
443 * the target CPort id before filling it in.
445 if (!cport_id_valid(hd, cport_id)) {
446 dev_err(&udev->dev, "invalid cport %u\n", cport_id);
450 /* Find a free urb */
451 urb = next_free_urb(es2, gfp_mask);
455 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
456 message->hcpriv = urb;
457 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
459 /* Pack the cport id into the message header */
460 gb_message_cport_pack(message->header, cport_id);
462 buffer_size = sizeof(*message->header) + message->payload_size;
464 ep_pair = cport_to_ep_pair(es2, cport_id);
465 usb_fill_bulk_urb(urb, udev,
466 usb_sndbulkpipe(udev,
467 es2->cport_out[ep_pair].endpoint),
468 message->buffer, buffer_size,
469 cport_out_callback, message);
470 urb->transfer_flags |= URB_ZERO_PACKET;
472 trace_gb_message_submit(message);
474 retval = usb_submit_urb(urb, gfp_mask);
476 dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
478 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
479 message->hcpriv = NULL;
480 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
483 gb_message_cport_clear(message->header);
492 * Can not be called in atomic context.
494 static void message_cancel(struct gb_message *message)
496 struct gb_host_device *hd = message->operation->connection->hd;
497 struct es2_ap_dev *es2 = hd_to_es2(hd);
503 spin_lock_irq(&es2->cport_out_urb_lock);
504 urb = message->hcpriv;
506 /* Prevent dynamically allocated urb from being deallocated. */
509 /* Prevent pre-allocated urb from being reused. */
510 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
511 if (urb == es2->cport_out_urb[i]) {
512 es2->cport_out_urb_cancelled[i] = true;
516 spin_unlock_irq(&es2->cport_out_urb_lock);
520 if (i < NUM_CPORT_OUT_URB) {
521 spin_lock_irq(&es2->cport_out_urb_lock);
522 es2->cport_out_urb_cancelled[i] = false;
523 spin_unlock_irq(&es2->cport_out_urb_lock);
529 static int cport_reset(struct gb_host_device *hd, u16 cport_id)
531 struct es2_ap_dev *es2 = hd_to_es2(hd);
532 struct usb_device *udev = es2->usb_dev;
536 case GB_SVC_CPORT_ID:
537 case ES2_CPORT_CDSI0:
538 case ES2_CPORT_CDSI1:
542 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
543 GB_APB_REQUEST_RESET_CPORT,
544 USB_DIR_OUT | USB_TYPE_VENDOR |
545 USB_RECIP_INTERFACE, cport_id, 0,
546 NULL, 0, ES2_TIMEOUT);
548 dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
556 static int es2_cport_allocate(struct gb_host_device *hd, int cport_id,
559 struct es2_ap_dev *es2 = hd_to_es2(hd);
560 struct ida *id_map = &hd->cport_id_map;
561 int ida_start, ida_end;
564 case ES2_CPORT_CDSI0:
565 case ES2_CPORT_CDSI1:
566 dev_err(&hd->dev, "cport %d not available\n", cport_id);
570 if (flags & GB_CONNECTION_FLAG_OFFLOADED &&
571 flags & GB_CONNECTION_FLAG_CDSI1) {
572 if (es2->cdsi1_in_use) {
573 dev_err(&hd->dev, "CDSI1 already in use\n");
577 es2->cdsi1_in_use = true;
579 return ES2_CPORT_CDSI1;
584 ida_end = hd->num_cports;
585 } else if (cport_id < hd->num_cports) {
586 ida_start = cport_id;
587 ida_end = cport_id + 1;
589 dev_err(&hd->dev, "cport %d not available\n", cport_id);
593 return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
596 static void es2_cport_release(struct gb_host_device *hd, u16 cport_id)
598 struct es2_ap_dev *es2 = hd_to_es2(hd);
601 case ES2_CPORT_CDSI1:
602 es2->cdsi1_in_use = false;
606 ida_simple_remove(&hd->cport_id_map, cport_id);
609 static int cport_disable(struct gb_host_device *hd, u16 cport_id)
613 retval = cport_reset(hd, cport_id);
620 static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
623 struct es2_ap_dev *es2 = hd_to_es2(hd);
624 struct usb_device *udev = es2->usb_dev;
626 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
627 GB_APB_REQUEST_LATENCY_TAG_EN,
628 USB_DIR_OUT | USB_TYPE_VENDOR |
629 USB_RECIP_INTERFACE, cport_id, 0, NULL,
633 dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
638 static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
641 struct es2_ap_dev *es2 = hd_to_es2(hd);
642 struct usb_device *udev = es2->usb_dev;
644 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
645 GB_APB_REQUEST_LATENCY_TAG_DIS,
646 USB_DIR_OUT | USB_TYPE_VENDOR |
647 USB_RECIP_INTERFACE, cport_id, 0, NULL,
651 dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
656 static int cport_features_enable(struct gb_host_device *hd, u16 cport_id)
659 struct es2_ap_dev *es2 = hd_to_es2(hd);
660 struct usb_device *udev = es2->usb_dev;
662 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
663 GB_APB_REQUEST_CPORT_FEAT_EN,
664 USB_DIR_OUT | USB_TYPE_VENDOR |
665 USB_RECIP_INTERFACE, cport_id, 0, NULL,
668 dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n",
673 static int cport_features_disable(struct gb_host_device *hd, u16 cport_id)
676 struct es2_ap_dev *es2 = hd_to_es2(hd);
677 struct usb_device *udev = es2->usb_dev;
679 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
680 GB_APB_REQUEST_CPORT_FEAT_DIS,
681 USB_DIR_OUT | USB_TYPE_VENDOR |
682 USB_RECIP_INTERFACE, cport_id, 0, NULL,
686 "Cannot disable CPort features for cport %u: %d\n",
691 static int timesync_enable(struct gb_host_device *hd, u8 count,
692 u64 frame_time, u32 strobe_delay, u32 refclk)
695 struct es2_ap_dev *es2 = hd_to_es2(hd);
696 struct usb_device *udev = es2->usb_dev;
697 struct gb_control_timesync_enable_request *request;
699 request = kzalloc(sizeof(*request), GFP_KERNEL);
703 request->count = count;
704 request->frame_time = cpu_to_le64(frame_time);
705 request->strobe_delay = cpu_to_le32(strobe_delay);
706 request->refclk = cpu_to_le32(refclk);
707 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
708 REQUEST_TIMESYNC_ENABLE,
709 USB_DIR_OUT | USB_TYPE_VENDOR |
710 USB_RECIP_INTERFACE, 0, 0, request,
711 sizeof(*request), ES2_TIMEOUT);
713 dev_err(&udev->dev, "Cannot enable timesync %d\n", retval);
719 static int timesync_disable(struct gb_host_device *hd)
722 struct es2_ap_dev *es2 = hd_to_es2(hd);
723 struct usb_device *udev = es2->usb_dev;
725 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
726 REQUEST_TIMESYNC_DISABLE,
727 USB_DIR_OUT | USB_TYPE_VENDOR |
728 USB_RECIP_INTERFACE, 0, 0, NULL,
731 dev_err(&udev->dev, "Cannot disable timesync %d\n", retval);
736 static int timesync_authoritative(struct gb_host_device *hd, u64 *frame_time)
739 struct es2_ap_dev *es2 = hd_to_es2(hd);
740 struct usb_device *udev = es2->usb_dev;
741 struct timesync_authoritative_request *request;
743 request = kzalloc(sizeof(*request), GFP_KERNEL);
747 for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
748 request->frame_time[i] = cpu_to_le64(frame_time[i]);
750 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
751 REQUEST_TIMESYNC_AUTHORITATIVE,
752 USB_DIR_OUT | USB_TYPE_VENDOR |
753 USB_RECIP_INTERFACE, 0, 0, request,
754 sizeof(*request), ES2_TIMEOUT);
756 dev_err(&udev->dev, "Cannot timesync authoritative out %d\n", retval);
762 static int timesync_get_last_event(struct gb_host_device *hd, u64 *frame_time)
765 struct es2_ap_dev *es2 = hd_to_es2(hd);
766 struct usb_device *udev = es2->usb_dev;
767 __le64 *response_frame_time;
769 response_frame_time = kzalloc(sizeof(*response_frame_time), GFP_KERNEL);
770 if (!response_frame_time)
773 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
774 REQUEST_TIMESYNC_GET_LAST_EVENT,
775 USB_DIR_IN | USB_TYPE_VENDOR |
776 USB_RECIP_INTERFACE, 0, 0, response_frame_time,
777 sizeof(*response_frame_time), ES2_TIMEOUT);
779 if (retval != sizeof(*response_frame_time)) {
780 dev_err(&udev->dev, "Cannot get last TimeSync event: %d\n",
788 *frame_time = le64_to_cpu(*response_frame_time);
791 kfree(response_frame_time);
795 static struct gb_hd_driver es2_driver = {
796 .hd_priv_size = sizeof(struct es2_ap_dev),
797 .message_send = message_send,
798 .message_cancel = message_cancel,
799 .cport_allocate = es2_cport_allocate,
800 .cport_release = es2_cport_release,
801 .cport_disable = cport_disable,
802 .latency_tag_enable = latency_tag_enable,
803 .latency_tag_disable = latency_tag_disable,
805 .cport_features_enable = cport_features_enable,
806 .cport_features_disable = cport_features_disable,
807 .timesync_enable = timesync_enable,
808 .timesync_disable = timesync_disable,
809 .timesync_authoritative = timesync_authoritative,
810 .timesync_get_last_event = timesync_get_last_event,
813 /* Common function to report consistent warnings based on URB status */
814 static int check_urb_status(struct urb *urb)
816 struct device *dev = &urb->dev->dev;
817 int status = urb->status;
824 dev_err(dev, "%s: overflow actual length is %d\n",
825 __func__, urb->actual_length);
831 /* device is gone, stop sending */
834 dev_err(dev, "%s: unknown status %d\n", __func__, status);
839 static void es2_destroy(struct es2_ap_dev *es2)
841 struct usb_device *udev;
845 debugfs_remove(es2->apb_log_enable_dentry);
846 usb_log_disable(es2);
848 /* Tear down everything! */
849 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
850 struct urb *urb = es2->cport_out_urb[i];
856 es2->cport_out_urb[i] = NULL;
857 es2->cport_out_urb_busy[i] = false; /* just to be anal */
860 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
861 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
863 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
864 struct urb *urb = cport_in->urb[i];
869 kfree(cport_in->buffer[i]);
870 cport_in->buffer[i] = NULL;
874 kfree(es2->cport_to_ep);
876 /* release reserved CDSI0 and CDSI1 cports */
877 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
878 gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);
886 static void cport_in_callback(struct urb *urb)
888 struct gb_host_device *hd = urb->context;
889 struct device *dev = &urb->dev->dev;
890 struct gb_operation_msg_hdr *header;
891 int status = check_urb_status(urb);
896 if ((status == -EAGAIN) || (status == -EPROTO))
899 /* The urb is being unlinked */
900 if (status == -ENOENT || status == -ESHUTDOWN)
903 dev_err(dev, "urb cport in error %d (dropped)\n", status);
907 if (urb->actual_length < sizeof(*header)) {
908 dev_err(dev, "short message received\n");
912 /* Extract the CPort id, which is packed in the message header */
913 header = urb->transfer_buffer;
914 cport_id = gb_message_cport_unpack(header);
916 if (cport_id_valid(hd, cport_id)) {
917 greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
920 dev_err(dev, "invalid cport id %u received\n", cport_id);
923 /* put our urb back in the request pool */
924 retval = usb_submit_urb(urb, GFP_ATOMIC);
926 dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
929 static void cport_out_callback(struct urb *urb)
931 struct gb_message *message = urb->context;
932 struct gb_host_device *hd = message->operation->connection->hd;
933 struct es2_ap_dev *es2 = hd_to_es2(hd);
934 int status = check_urb_status(urb);
937 gb_message_cport_clear(message->header);
939 spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
940 message->hcpriv = NULL;
941 spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
944 * Tell the submitter that the message send (attempt) is
945 * complete, and report the status.
947 greybus_message_sent(hd, message, status);
952 #define APB1_LOG_MSG_SIZE 64
953 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
957 /* SVC messages go down our control pipe */
959 retval = usb_control_msg(es2->usb_dev,
960 usb_rcvctrlpipe(es2->usb_dev, 0),
962 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
968 kfifo_in(&es2->apb_log_fifo, buf, retval);
969 } while (retval > 0);
972 static int apb_log_poll(void *data)
974 struct es2_ap_dev *es2 = data;
977 buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
981 while (!kthread_should_stop()) {
983 apb_log_get(es2, buf);
991 static ssize_t apb_log_read(struct file *f, char __user *buf,
992 size_t count, loff_t *ppos)
994 struct es2_ap_dev *es2 = f->f_inode->i_private;
999 if (count > APB1_LOG_SIZE)
1000 count = APB1_LOG_SIZE;
1002 tmp_buf = kmalloc(count, GFP_KERNEL);
1006 copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
1007 ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
1014 static const struct file_operations apb_log_fops = {
1015 .read = apb_log_read,
1018 static void usb_log_enable(struct es2_ap_dev *es2)
1020 if (!IS_ERR_OR_NULL(es2->apb_log_task))
1023 /* get log from APB1 */
1024 es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
1025 if (IS_ERR(es2->apb_log_task))
1027 /* XXX We will need to rename this per APB */
1028 es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO,
1029 gb_debugfs_get(), es2,
1033 static void usb_log_disable(struct es2_ap_dev *es2)
1035 if (IS_ERR_OR_NULL(es2->apb_log_task))
1038 debugfs_remove(es2->apb_log_dentry);
1039 es2->apb_log_dentry = NULL;
1041 kthread_stop(es2->apb_log_task);
1042 es2->apb_log_task = NULL;
1045 static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
1046 size_t count, loff_t *ppos)
1048 struct es2_ap_dev *es2 = f->f_inode->i_private;
1049 int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
1052 sprintf(tmp_buf, "%d\n", enable);
1053 return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
1056 static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
1057 size_t count, loff_t *ppos)
1061 struct es2_ap_dev *es2 = f->f_inode->i_private;
1063 retval = kstrtoint_from_user(buf, count, 10, &enable);
1068 usb_log_enable(es2);
1070 usb_log_disable(es2);
1075 static const struct file_operations apb_log_enable_fops = {
1076 .read = apb_log_enable_read,
1077 .write = apb_log_enable_write,
1080 static int apb_get_cport_count(struct usb_device *udev)
1083 __le16 *cport_count;
1085 cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
1089 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1090 GB_APB_REQUEST_CPORT_COUNT,
1091 USB_DIR_IN | USB_TYPE_VENDOR |
1092 USB_RECIP_INTERFACE, 0, 0, cport_count,
1093 sizeof(*cport_count), ES2_TIMEOUT);
1094 if (retval != sizeof(*cport_count)) {
1095 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1104 retval = le16_to_cpu(*cport_count);
1106 /* We need to fit a CPort ID in one byte of a message header */
1107 if (retval > U8_MAX) {
1109 dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
1118 * The ES2 USB Bridge device has 15 endpoints
1119 * 1 Control - usual USB stuff + AP -> APBridgeA messages
1120 * 7 Bulk IN - CPort data in
1121 * 7 Bulk OUT - CPort data out
1123 static int ap_probe(struct usb_interface *interface,
1124 const struct usb_device_id *id)
1126 struct es2_ap_dev *es2;
1127 struct gb_host_device *hd;
1128 struct usb_device *udev;
1129 struct usb_host_interface *iface_desc;
1130 struct usb_endpoint_descriptor *endpoint;
1137 udev = usb_get_dev(interface_to_usbdev(interface));
1139 num_cports = apb_get_cport_count(udev);
1140 if (num_cports < 0) {
1142 dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
1147 hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
1154 es2 = hd_to_es2(hd);
1156 es2->usb_intf = interface;
1157 es2->usb_dev = udev;
1158 spin_lock_init(&es2->cport_out_urb_lock);
1159 INIT_KFIFO(es2->apb_log_fifo);
1160 usb_set_intfdata(interface, es2);
1163 * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
1166 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
1169 retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
1173 es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep),
1175 if (!es2->cport_to_ep) {
1180 /* find all bulk endpoints */
1181 iface_desc = interface->cur_altsetting;
1182 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1183 endpoint = &iface_desc->endpoint[i].desc;
1185 if (usb_endpoint_is_bulk_in(endpoint)) {
1186 es2->cport_in[bulk_in++].endpoint =
1187 endpoint->bEndpointAddress;
1188 } else if (usb_endpoint_is_bulk_out(endpoint)) {
1189 es2->cport_out[bulk_out++].endpoint =
1190 endpoint->bEndpointAddress;
1193 "Unknown endpoint type found, address 0x%02x\n",
1194 endpoint->bEndpointAddress);
1197 if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
1198 dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
1203 /* Allocate buffers for our cport in messages */
1204 for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
1205 struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
1207 for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
1211 urb = usb_alloc_urb(0, GFP_KERNEL);
1216 buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
1222 usb_fill_bulk_urb(urb, udev,
1223 usb_rcvbulkpipe(udev,
1224 cport_in->endpoint),
1225 buffer, ES2_GBUF_MSG_SIZE_MAX,
1226 cport_in_callback, hd);
1227 cport_in->urb[i] = urb;
1228 cport_in->buffer[i] = buffer;
1232 /* Allocate urbs for our CPort OUT messages */
1233 for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
1236 urb = usb_alloc_urb(0, GFP_KERNEL);
1242 es2->cport_out_urb[i] = urb;
1243 es2->cport_out_urb_busy[i] = false; /* just to be anal */
1246 /* XXX We will need to rename this per APB */
1247 es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
1248 (S_IWUSR | S_IRUGO),
1249 gb_debugfs_get(), es2,
1250 &apb_log_enable_fops);
1252 retval = gb_hd_add(hd);
1256 for (i = 0; i < NUM_BULKS; ++i) {
1257 retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
1259 goto err_disable_cport_in;
1264 err_disable_cport_in:
1265 for (--i; i >= 0; --i)
1266 es2_cport_in_disable(es2, &es2->cport_in[i]);
1274 static void ap_disconnect(struct usb_interface *interface)
1276 struct es2_ap_dev *es2 = usb_get_intfdata(interface);
1281 for (i = 0; i < NUM_BULKS; ++i)
1282 es2_cport_in_disable(es2, &es2->cport_in[i]);
1287 static struct usb_driver es2_ap_driver = {
1288 .name = "es2_ap_driver",
1290 .disconnect = ap_disconnect,
1291 .id_table = id_table,
1295 module_usb_driver(es2_ap_driver);
1297 MODULE_LICENSE("GPL v2");
1298 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");