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>
19 #include "greybus_trace.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
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
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 * es2_ap_dev - ES2 USB Bridge to AP structure
78 * @usb_dev: pointer to the USB device we are.
79 * @usb_intf: pointer to the USB interface we are bound to.
80 * @hd: pointer to our gb_host_device structure
82 * @cport_in: endpoint, urbs and buffer for cport in messages
83 * @cport_out_endpoint: endpoint for for cport out messages
84 * @cport_out_urb: array of urbs for the CPort out messages
85 * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
87 * @cport_out_urb_cancelled: array of flags indicating whether the
88 * corresponding @cport_out_urb is being cancelled
89 * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
91 * @apb_log_task: task pointer for logging thread
92 * @apb_log_dentry: file system entry for the log file interface
93 * @apb_log_enable_dentry: file system entry for enabling logging
94 * @apb_log_fifo: kernel FIFO to carry logged data
95 * @arpc_urb: array of urbs for the ARPC in messages
96 * @arpc_buffer: array of buffers for the @arpc_urb urbs
97 * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC
98 * @arpc_id_cycle: gives an unique id to ARPC
99 * @arpc_lock: locks ARPC list
100 * @arpcs: list of in progress ARPCs
103 struct usb_device
*usb_dev
;
104 struct usb_interface
*usb_intf
;
105 struct gb_host_device
*hd
;
107 struct es2_cport_in cport_in
;
108 __u8 cport_out_endpoint
;
109 struct urb
*cport_out_urb
[NUM_CPORT_OUT_URB
];
110 bool cport_out_urb_busy
[NUM_CPORT_OUT_URB
];
111 bool cport_out_urb_cancelled
[NUM_CPORT_OUT_URB
];
112 spinlock_t cport_out_urb_lock
;
116 struct task_struct
*apb_log_task
;
117 struct dentry
*apb_log_dentry
;
118 struct dentry
*apb_log_enable_dentry
;
119 DECLARE_KFIFO(apb_log_fifo
, char, APB1_LOG_SIZE
);
121 __u8 arpc_endpoint_in
;
122 struct urb
*arpc_urb
[NUM_ARPC_IN_URB
];
123 u8
*arpc_buffer
[NUM_ARPC_IN_URB
];
126 spinlock_t arpc_lock
;
127 struct list_head arpcs
;
131 * timesync_enable_request - Enable timesync in an APBridge
132 * @count: number of TimeSync Pulses to expect
133 * @frame_time: the initial FrameTime at the first TimeSync Pulse
134 * @strobe_delay: the expected delay in microseconds between each TimeSync Pulse
135 * @refclk: The AP mandated reference clock to run FrameTime at
137 struct timesync_enable_request
{
145 * timesync_authoritative_request - Transmit authoritative FrameTime to APBridge
146 * @frame_time: An array of authoritative FrameTimes provided by the SVC
147 * and relayed to the APBridge by the AP
149 struct timesync_authoritative_request
{
150 __le64 frame_time
[GB_TIMESYNC_MAX_STROBES
];
154 struct list_head list
;
155 struct arpc_request_message
*req
;
156 struct arpc_response_message
*resp
;
157 struct completion response_received
;
161 static inline struct es2_ap_dev
*hd_to_es2(struct gb_host_device
*hd
)
163 return (struct es2_ap_dev
*)&hd
->hd_priv
;
166 static void cport_out_callback(struct urb
*urb
);
167 static void usb_log_enable(struct es2_ap_dev
*es2
);
168 static void usb_log_disable(struct es2_ap_dev
*es2
);
169 static int arpc_sync(struct es2_ap_dev
*es2
, u8 type
, void *payload
,
170 size_t size
, int *result
, unsigned int timeout
);
172 static int output_sync(struct es2_ap_dev
*es2
, void *req
, u16 size
, u8 cmd
)
174 struct usb_device
*udev
= es2
->usb_dev
;
178 data
= kmalloc(size
, GFP_KERNEL
);
181 memcpy(data
, req
, size
);
183 retval
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
185 USB_DIR_OUT
| USB_TYPE_VENDOR
|
187 0, 0, data
, size
, ES2_USB_CTRL_TIMEOUT
);
189 dev_err(&udev
->dev
, "%s: return error %d\n", __func__
, retval
);
197 static void ap_urb_complete(struct urb
*urb
)
199 struct usb_ctrlrequest
*dr
= urb
->context
;
205 static int output_async(struct es2_ap_dev
*es2
, void *req
, u16 size
, u8 cmd
)
207 struct usb_device
*udev
= es2
->usb_dev
;
209 struct usb_ctrlrequest
*dr
;
213 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
217 dr
= kmalloc(sizeof(*dr
) + size
, GFP_ATOMIC
);
223 buf
= (u8
*)dr
+ sizeof(*dr
);
224 memcpy(buf
, req
, size
);
227 dr
->bRequestType
= USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
;
230 dr
->wLength
= cpu_to_le16(size
);
232 usb_fill_control_urb(urb
, udev
, usb_sndctrlpipe(udev
, 0),
233 (unsigned char *)dr
, buf
, size
,
234 ap_urb_complete
, dr
);
235 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
243 static int output(struct gb_host_device
*hd
, void *req
, u16 size
, u8 cmd
,
246 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
249 return output_async(es2
, req
, size
, cmd
);
251 return output_sync(es2
, req
, size
, cmd
);
254 static int es2_cport_in_enable(struct es2_ap_dev
*es2
,
255 struct es2_cport_in
*cport_in
)
261 for (i
= 0; i
< NUM_CPORT_IN_URB
; ++i
) {
262 urb
= cport_in
->urb
[i
];
264 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
266 dev_err(&es2
->usb_dev
->dev
,
267 "failed to submit in-urb: %d\n", ret
);
275 for (--i
; i
>= 0; --i
) {
276 urb
= cport_in
->urb
[i
];
283 static void es2_cport_in_disable(struct es2_ap_dev
*es2
,
284 struct es2_cport_in
*cport_in
)
289 for (i
= 0; i
< NUM_CPORT_IN_URB
; ++i
) {
290 urb
= cport_in
->urb
[i
];
295 static int es2_arpc_in_enable(struct es2_ap_dev
*es2
)
301 for (i
= 0; i
< NUM_ARPC_IN_URB
; ++i
) {
302 urb
= es2
->arpc_urb
[i
];
304 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
306 dev_err(&es2
->usb_dev
->dev
,
307 "failed to submit arpc in-urb: %d\n", ret
);
315 for (--i
; i
>= 0; --i
) {
316 urb
= es2
->arpc_urb
[i
];
323 static void es2_arpc_in_disable(struct es2_ap_dev
*es2
)
328 for (i
= 0; i
< NUM_ARPC_IN_URB
; ++i
) {
329 urb
= es2
->arpc_urb
[i
];
334 static struct urb
*next_free_urb(struct es2_ap_dev
*es2
, gfp_t gfp_mask
)
336 struct urb
*urb
= NULL
;
340 spin_lock_irqsave(&es2
->cport_out_urb_lock
, flags
);
342 /* Look in our pool of allocated urbs first, as that's the "fastest" */
343 for (i
= 0; i
< NUM_CPORT_OUT_URB
; ++i
) {
344 if (es2
->cport_out_urb_busy
[i
] == false &&
345 es2
->cport_out_urb_cancelled
[i
] == false) {
346 es2
->cport_out_urb_busy
[i
] = true;
347 urb
= es2
->cport_out_urb
[i
];
351 spin_unlock_irqrestore(&es2
->cport_out_urb_lock
, flags
);
356 * Crap, pool is empty, complain to the syslog and go allocate one
357 * dynamically as we have to succeed.
359 dev_dbg(&es2
->usb_dev
->dev
,
360 "No free CPort OUT urbs, having to dynamically allocate one!\n");
361 return usb_alloc_urb(0, gfp_mask
);
364 static void free_urb(struct es2_ap_dev
*es2
, struct urb
*urb
)
369 * See if this was an urb in our pool, if so mark it "free", otherwise
370 * we need to free it ourselves.
372 spin_lock_irqsave(&es2
->cport_out_urb_lock
, flags
);
373 for (i
= 0; i
< NUM_CPORT_OUT_URB
; ++i
) {
374 if (urb
== es2
->cport_out_urb
[i
]) {
375 es2
->cport_out_urb_busy
[i
] = false;
380 spin_unlock_irqrestore(&es2
->cport_out_urb_lock
, flags
);
382 /* If urb is not NULL, then we need to free this urb */
387 * We (ab)use the operation-message header pad bytes to transfer the
388 * cport id in order to minimise overhead.
391 gb_message_cport_pack(struct gb_operation_msg_hdr
*header
, u16 cport_id
)
393 header
->pad
[0] = cport_id
;
396 /* Clear the pad bytes used for the CPort id */
397 static void gb_message_cport_clear(struct gb_operation_msg_hdr
*header
)
402 /* Extract the CPort id packed into the header, and clear it */
403 static u16
gb_message_cport_unpack(struct gb_operation_msg_hdr
*header
)
405 u16 cport_id
= header
->pad
[0];
407 gb_message_cport_clear(header
);
413 * Returns zero if the message was successfully queued, or a negative errno
416 static int message_send(struct gb_host_device
*hd
, u16 cport_id
,
417 struct gb_message
*message
, gfp_t gfp_mask
)
419 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
420 struct usb_device
*udev
= es2
->usb_dev
;
427 * The data actually transferred will include an indication
428 * of where the data should be sent. Do one last check of
429 * the target CPort id before filling it in.
431 if (!cport_id_valid(hd
, cport_id
)) {
432 dev_err(&udev
->dev
, "invalid cport %u\n", cport_id
);
436 /* Find a free urb */
437 urb
= next_free_urb(es2
, gfp_mask
);
441 spin_lock_irqsave(&es2
->cport_out_urb_lock
, flags
);
442 message
->hcpriv
= urb
;
443 spin_unlock_irqrestore(&es2
->cport_out_urb_lock
, flags
);
445 /* Pack the cport id into the message header */
446 gb_message_cport_pack(message
->header
, cport_id
);
448 buffer_size
= sizeof(*message
->header
) + message
->payload_size
;
450 usb_fill_bulk_urb(urb
, udev
,
451 usb_sndbulkpipe(udev
,
452 es2
->cport_out_endpoint
),
453 message
->buffer
, buffer_size
,
454 cport_out_callback
, message
);
455 urb
->transfer_flags
|= URB_ZERO_PACKET
;
457 trace_gb_message_submit(message
);
459 retval
= usb_submit_urb(urb
, gfp_mask
);
461 dev_err(&udev
->dev
, "failed to submit out-urb: %d\n", retval
);
463 spin_lock_irqsave(&es2
->cport_out_urb_lock
, flags
);
464 message
->hcpriv
= NULL
;
465 spin_unlock_irqrestore(&es2
->cport_out_urb_lock
, flags
);
468 gb_message_cport_clear(message
->header
);
477 * Can not be called in atomic context.
479 static void message_cancel(struct gb_message
*message
)
481 struct gb_host_device
*hd
= message
->operation
->connection
->hd
;
482 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
488 spin_lock_irq(&es2
->cport_out_urb_lock
);
489 urb
= message
->hcpriv
;
491 /* Prevent dynamically allocated urb from being deallocated. */
494 /* Prevent pre-allocated urb from being reused. */
495 for (i
= 0; i
< NUM_CPORT_OUT_URB
; ++i
) {
496 if (urb
== es2
->cport_out_urb
[i
]) {
497 es2
->cport_out_urb_cancelled
[i
] = true;
501 spin_unlock_irq(&es2
->cport_out_urb_lock
);
505 if (i
< NUM_CPORT_OUT_URB
) {
506 spin_lock_irq(&es2
->cport_out_urb_lock
);
507 es2
->cport_out_urb_cancelled
[i
] = false;
508 spin_unlock_irq(&es2
->cport_out_urb_lock
);
514 static int es2_cport_allocate(struct gb_host_device
*hd
, int cport_id
,
517 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
518 struct ida
*id_map
= &hd
->cport_id_map
;
519 int ida_start
, ida_end
;
522 case ES2_CPORT_CDSI0
:
523 case ES2_CPORT_CDSI1
:
524 dev_err(&hd
->dev
, "cport %d not available\n", cport_id
);
528 if (flags
& GB_CONNECTION_FLAG_OFFLOADED
&&
529 flags
& GB_CONNECTION_FLAG_CDSI1
) {
530 if (es2
->cdsi1_in_use
) {
531 dev_err(&hd
->dev
, "CDSI1 already in use\n");
535 es2
->cdsi1_in_use
= true;
537 return ES2_CPORT_CDSI1
;
542 ida_end
= hd
->num_cports
;
543 } else if (cport_id
< hd
->num_cports
) {
544 ida_start
= cport_id
;
545 ida_end
= cport_id
+ 1;
547 dev_err(&hd
->dev
, "cport %d not available\n", cport_id
);
551 return ida_simple_get(id_map
, ida_start
, ida_end
, GFP_KERNEL
);
554 static void es2_cport_release(struct gb_host_device
*hd
, u16 cport_id
)
556 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
559 case ES2_CPORT_CDSI1
:
560 es2
->cdsi1_in_use
= false;
564 ida_simple_remove(&hd
->cport_id_map
, cport_id
);
567 static int cport_enable(struct gb_host_device
*hd
, u16 cport_id
,
570 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
571 struct usb_device
*udev
= es2
->usb_dev
;
572 struct gb_apb_request_cport_flags
*req
;
573 u32 connection_flags
;
576 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
580 connection_flags
= 0;
581 if (flags
& GB_CONNECTION_FLAG_CONTROL
)
582 connection_flags
|= GB_APB_CPORT_FLAG_CONTROL
;
583 if (flags
& GB_CONNECTION_FLAG_HIGH_PRIO
)
584 connection_flags
|= GB_APB_CPORT_FLAG_HIGH_PRIO
;
586 req
->flags
= cpu_to_le32(connection_flags
);
588 dev_dbg(&hd
->dev
, "%s - cport = %u, flags = %02x\n", __func__
,
589 cport_id
, connection_flags
);
591 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
592 GB_APB_REQUEST_CPORT_FLAGS
,
593 USB_DIR_OUT
| USB_TYPE_VENDOR
|
594 USB_RECIP_INTERFACE
, cport_id
, 0,
595 req
, sizeof(*req
), ES2_USB_CTRL_TIMEOUT
);
596 if (ret
!= sizeof(*req
)) {
597 dev_err(&udev
->dev
, "failed to set cport flags for port %d\n",
612 static int es2_cport_connected(struct gb_host_device
*hd
, u16 cport_id
)
614 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
615 struct device
*dev
= &es2
->usb_dev
->dev
;
616 struct arpc_cport_connected_req req
;
619 req
.cport_id
= cpu_to_le16(cport_id
);
620 ret
= arpc_sync(es2
, ARPC_TYPE_CPORT_CONNECTED
, &req
, sizeof(req
),
621 NULL
, ES2_ARPC_CPORT_TIMEOUT
);
623 dev_err(dev
, "failed to set connected state for cport %u: %d\n",
631 static int es2_cport_flush(struct gb_host_device
*hd
, u16 cport_id
)
633 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
634 struct device
*dev
= &es2
->usb_dev
->dev
;
635 struct arpc_cport_flush_req req
;
638 req
.cport_id
= cpu_to_le16(cport_id
);
639 ret
= arpc_sync(es2
, ARPC_TYPE_CPORT_FLUSH
, &req
, sizeof(req
),
640 NULL
, ES2_ARPC_CPORT_TIMEOUT
);
642 dev_err(dev
, "failed to flush cport %u: %d\n", cport_id
, ret
);
649 static int es2_cport_shutdown(struct gb_host_device
*hd
, u16 cport_id
,
650 u8 phase
, unsigned int timeout
)
652 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
653 struct device
*dev
= &es2
->usb_dev
->dev
;
654 struct arpc_cport_shutdown_req req
;
658 if (timeout
> U16_MAX
)
661 req
.cport_id
= cpu_to_le16(cport_id
);
662 req
.timeout
= cpu_to_le16(timeout
);
664 ret
= arpc_sync(es2
, ARPC_TYPE_CPORT_SHUTDOWN
, &req
, sizeof(req
),
665 &result
, ES2_ARPC_CPORT_TIMEOUT
+ timeout
);
667 dev_err(dev
, "failed to send shutdown over cport %u: %d (%d)\n",
668 cport_id
, ret
, result
);
675 static int es2_cport_quiesce(struct gb_host_device
*hd
, u16 cport_id
,
676 size_t peer_space
, unsigned int timeout
)
678 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
679 struct device
*dev
= &es2
->usb_dev
->dev
;
680 struct arpc_cport_quiesce_req req
;
684 if (peer_space
> U16_MAX
)
687 if (timeout
> U16_MAX
)
690 req
.cport_id
= cpu_to_le16(cport_id
);
691 req
.peer_space
= cpu_to_le16(peer_space
);
692 req
.timeout
= cpu_to_le16(timeout
);
693 ret
= arpc_sync(es2
, ARPC_TYPE_CPORT_QUIESCE
, &req
, sizeof(req
),
694 &result
, ES2_ARPC_CPORT_TIMEOUT
+ timeout
);
696 dev_err(dev
, "failed to quiesce cport %u: %d (%d)\n",
697 cport_id
, ret
, result
);
704 static int es2_cport_clear(struct gb_host_device
*hd
, u16 cport_id
)
706 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
707 struct device
*dev
= &es2
->usb_dev
->dev
;
708 struct arpc_cport_clear_req req
;
711 req
.cport_id
= cpu_to_le16(cport_id
);
712 ret
= arpc_sync(es2
, ARPC_TYPE_CPORT_CLEAR
, &req
, sizeof(req
),
713 NULL
, ES2_ARPC_CPORT_TIMEOUT
);
715 dev_err(dev
, "failed to clear cport %u: %d\n", cport_id
, ret
);
722 static int latency_tag_enable(struct gb_host_device
*hd
, u16 cport_id
)
725 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
726 struct usb_device
*udev
= es2
->usb_dev
;
728 retval
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
729 GB_APB_REQUEST_LATENCY_TAG_EN
,
730 USB_DIR_OUT
| USB_TYPE_VENDOR
|
731 USB_RECIP_INTERFACE
, cport_id
, 0, NULL
,
732 0, ES2_USB_CTRL_TIMEOUT
);
735 dev_err(&udev
->dev
, "Cannot enable latency tag for cport %d\n",
740 static int latency_tag_disable(struct gb_host_device
*hd
, u16 cport_id
)
743 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
744 struct usb_device
*udev
= es2
->usb_dev
;
746 retval
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
747 GB_APB_REQUEST_LATENCY_TAG_DIS
,
748 USB_DIR_OUT
| USB_TYPE_VENDOR
|
749 USB_RECIP_INTERFACE
, cport_id
, 0, NULL
,
750 0, ES2_USB_CTRL_TIMEOUT
);
753 dev_err(&udev
->dev
, "Cannot disable latency tag for cport %d\n",
758 static int timesync_enable(struct gb_host_device
*hd
, u8 count
,
759 u64 frame_time
, u32 strobe_delay
, u32 refclk
)
762 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
763 struct usb_device
*udev
= es2
->usb_dev
;
764 struct gb_control_timesync_enable_request
*request
;
766 request
= kzalloc(sizeof(*request
), GFP_KERNEL
);
770 request
->count
= count
;
771 request
->frame_time
= cpu_to_le64(frame_time
);
772 request
->strobe_delay
= cpu_to_le32(strobe_delay
);
773 request
->refclk
= cpu_to_le32(refclk
);
774 retval
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
775 GB_APB_REQUEST_TIMESYNC_ENABLE
,
776 USB_DIR_OUT
| USB_TYPE_VENDOR
|
777 USB_RECIP_INTERFACE
, 0, 0, request
,
778 sizeof(*request
), ES2_USB_CTRL_TIMEOUT
);
780 dev_err(&udev
->dev
, "Cannot enable timesync %d\n", retval
);
786 static int timesync_disable(struct gb_host_device
*hd
)
789 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
790 struct usb_device
*udev
= es2
->usb_dev
;
792 retval
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
793 GB_APB_REQUEST_TIMESYNC_DISABLE
,
794 USB_DIR_OUT
| USB_TYPE_VENDOR
|
795 USB_RECIP_INTERFACE
, 0, 0, NULL
,
796 0, ES2_USB_CTRL_TIMEOUT
);
798 dev_err(&udev
->dev
, "Cannot disable timesync %d\n", retval
);
803 static int timesync_authoritative(struct gb_host_device
*hd
, u64
*frame_time
)
806 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
807 struct usb_device
*udev
= es2
->usb_dev
;
808 struct timesync_authoritative_request
*request
;
810 request
= kzalloc(sizeof(*request
), GFP_KERNEL
);
814 for (i
= 0; i
< GB_TIMESYNC_MAX_STROBES
; i
++)
815 request
->frame_time
[i
] = cpu_to_le64(frame_time
[i
]);
817 retval
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
818 GB_APB_REQUEST_TIMESYNC_AUTHORITATIVE
,
819 USB_DIR_OUT
| USB_TYPE_VENDOR
|
820 USB_RECIP_INTERFACE
, 0, 0, request
,
821 sizeof(*request
), ES2_USB_CTRL_TIMEOUT
);
823 dev_err(&udev
->dev
, "Cannot timesync authoritative out %d\n", retval
);
829 static int timesync_get_last_event(struct gb_host_device
*hd
, u64
*frame_time
)
832 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
833 struct usb_device
*udev
= es2
->usb_dev
;
834 __le64
*response_frame_time
;
836 response_frame_time
= kzalloc(sizeof(*response_frame_time
), GFP_KERNEL
);
837 if (!response_frame_time
)
840 retval
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
841 GB_APB_REQUEST_TIMESYNC_GET_LAST_EVENT
,
842 USB_DIR_IN
| USB_TYPE_VENDOR
|
843 USB_RECIP_INTERFACE
, 0, 0, response_frame_time
,
844 sizeof(*response_frame_time
),
845 ES2_USB_CTRL_TIMEOUT
);
847 if (retval
!= sizeof(*response_frame_time
)) {
848 dev_err(&udev
->dev
, "Cannot get last TimeSync event: %d\n",
856 *frame_time
= le64_to_cpu(*response_frame_time
);
859 kfree(response_frame_time
);
863 static struct gb_hd_driver es2_driver
= {
864 .hd_priv_size
= sizeof(struct es2_ap_dev
),
865 .message_send
= message_send
,
866 .message_cancel
= message_cancel
,
867 .cport_allocate
= es2_cport_allocate
,
868 .cport_release
= es2_cport_release
,
869 .cport_enable
= cport_enable
,
870 .cport_connected
= es2_cport_connected
,
871 .cport_flush
= es2_cport_flush
,
872 .cport_shutdown
= es2_cport_shutdown
,
873 .cport_quiesce
= es2_cport_quiesce
,
874 .cport_clear
= es2_cport_clear
,
875 .latency_tag_enable
= latency_tag_enable
,
876 .latency_tag_disable
= latency_tag_disable
,
878 .timesync_enable
= timesync_enable
,
879 .timesync_disable
= timesync_disable
,
880 .timesync_authoritative
= timesync_authoritative
,
881 .timesync_get_last_event
= timesync_get_last_event
,
884 /* Common function to report consistent warnings based on URB status */
885 static int check_urb_status(struct urb
*urb
)
887 struct device
*dev
= &urb
->dev
->dev
;
888 int status
= urb
->status
;
895 dev_err(dev
, "%s: overflow actual length is %d\n",
896 __func__
, urb
->actual_length
);
902 /* device is gone, stop sending */
905 dev_err(dev
, "%s: unknown status %d\n", __func__
, status
);
910 static void es2_destroy(struct es2_ap_dev
*es2
)
912 struct usb_device
*udev
;
916 debugfs_remove(es2
->apb_log_enable_dentry
);
917 usb_log_disable(es2
);
919 /* Tear down everything! */
920 for (i
= 0; i
< NUM_CPORT_OUT_URB
; ++i
) {
921 urb
= es2
->cport_out_urb
[i
];
924 es2
->cport_out_urb
[i
] = NULL
;
925 es2
->cport_out_urb_busy
[i
] = false; /* just to be anal */
928 for (i
= 0; i
< NUM_ARPC_IN_URB
; ++i
) {
929 usb_free_urb(es2
->arpc_urb
[i
]);
930 kfree(es2
->arpc_buffer
[i
]);
931 es2
->arpc_buffer
[i
] = NULL
;
934 for (i
= 0; i
< NUM_CPORT_IN_URB
; ++i
) {
935 usb_free_urb(es2
->cport_in
.urb
[i
]);
936 kfree(es2
->cport_in
.buffer
[i
]);
937 es2
->cport_in
.buffer
[i
] = NULL
;
940 /* release reserved CDSI0 and CDSI1 cports */
941 gb_hd_cport_release_reserved(es2
->hd
, ES2_CPORT_CDSI1
);
942 gb_hd_cport_release_reserved(es2
->hd
, ES2_CPORT_CDSI0
);
950 static void cport_in_callback(struct urb
*urb
)
952 struct gb_host_device
*hd
= urb
->context
;
953 struct device
*dev
= &urb
->dev
->dev
;
954 struct gb_operation_msg_hdr
*header
;
955 int status
= check_urb_status(urb
);
960 if ((status
== -EAGAIN
) || (status
== -EPROTO
))
963 /* The urb is being unlinked */
964 if (status
== -ENOENT
|| status
== -ESHUTDOWN
)
967 dev_err(dev
, "urb cport in error %d (dropped)\n", status
);
971 if (urb
->actual_length
< sizeof(*header
)) {
972 dev_err(dev
, "short message received\n");
976 /* Extract the CPort id, which is packed in the message header */
977 header
= urb
->transfer_buffer
;
978 cport_id
= gb_message_cport_unpack(header
);
980 if (cport_id_valid(hd
, cport_id
)) {
981 greybus_data_rcvd(hd
, cport_id
, urb
->transfer_buffer
,
984 dev_err(dev
, "invalid cport id %u received\n", cport_id
);
987 /* put our urb back in the request pool */
988 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
990 dev_err(dev
, "failed to resubmit in-urb: %d\n", retval
);
993 static void cport_out_callback(struct urb
*urb
)
995 struct gb_message
*message
= urb
->context
;
996 struct gb_host_device
*hd
= message
->operation
->connection
->hd
;
997 struct es2_ap_dev
*es2
= hd_to_es2(hd
);
998 int status
= check_urb_status(urb
);
1001 gb_message_cport_clear(message
->header
);
1003 spin_lock_irqsave(&es2
->cport_out_urb_lock
, flags
);
1004 message
->hcpriv
= NULL
;
1005 spin_unlock_irqrestore(&es2
->cport_out_urb_lock
, flags
);
1008 * Tell the submitter that the message send (attempt) is
1009 * complete, and report the status.
1011 greybus_message_sent(hd
, message
, status
);
1016 static struct arpc
*arpc_alloc(void *payload
, u16 size
, u8 type
)
1020 if (size
+ sizeof(*rpc
->req
) > ARPC_OUT_SIZE_MAX
)
1023 rpc
= kzalloc(sizeof(*rpc
), GFP_KERNEL
);
1027 INIT_LIST_HEAD(&rpc
->list
);
1028 rpc
->req
= kzalloc(sizeof(*rpc
->req
) + size
, GFP_KERNEL
);
1032 rpc
->resp
= kzalloc(sizeof(*rpc
->resp
), GFP_KERNEL
);
1036 rpc
->req
->type
= type
;
1037 rpc
->req
->size
= cpu_to_le16(sizeof(rpc
->req
) + size
);
1038 memcpy(rpc
->req
->data
, payload
, size
);
1040 init_completion(&rpc
->response_received
);
1052 static void arpc_free(struct arpc
*rpc
)
1059 static struct arpc
*arpc_find(struct es2_ap_dev
*es2
, __le16 id
)
1063 list_for_each_entry(rpc
, &es2
->arpcs
, list
) {
1064 if (rpc
->req
->id
== id
)
1071 static void arpc_add(struct es2_ap_dev
*es2
, struct arpc
*rpc
)
1074 rpc
->req
->id
= cpu_to_le16(es2
->arpc_id_cycle
++);
1075 list_add_tail(&rpc
->list
, &es2
->arpcs
);
1078 static void arpc_del(struct es2_ap_dev
*es2
, struct arpc
*rpc
)
1081 rpc
->active
= false;
1082 list_del(&rpc
->list
);
1086 static int arpc_send(struct es2_ap_dev
*es2
, struct arpc
*rpc
, int timeout
)
1088 struct usb_device
*udev
= es2
->usb_dev
;
1091 retval
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
1092 GB_APB_REQUEST_ARPC_RUN
,
1093 USB_DIR_OUT
| USB_TYPE_VENDOR
|
1094 USB_RECIP_INTERFACE
,
1096 rpc
->req
, le16_to_cpu(rpc
->req
->size
),
1097 ES2_USB_CTRL_TIMEOUT
);
1098 if (retval
!= le16_to_cpu(rpc
->req
->size
)) {
1100 "failed to send ARPC request %d: %d\n",
1101 rpc
->req
->type
, retval
);
1110 static int arpc_sync(struct es2_ap_dev
*es2
, u8 type
, void *payload
,
1111 size_t size
, int *result
, unsigned int timeout
)
1114 unsigned long flags
;
1120 rpc
= arpc_alloc(payload
, size
, type
);
1124 spin_lock_irqsave(&es2
->arpc_lock
, flags
);
1126 spin_unlock_irqrestore(&es2
->arpc_lock
, flags
);
1128 retval
= arpc_send(es2
, rpc
, timeout
);
1132 retval
= wait_for_completion_interruptible_timeout(
1133 &rpc
->response_received
,
1134 msecs_to_jiffies(timeout
));
1137 retval
= -ETIMEDOUT
;
1141 if (rpc
->resp
->result
) {
1142 retval
= -EREMOTEIO
;
1144 *result
= rpc
->resp
->result
;
1150 spin_lock_irqsave(&es2
->arpc_lock
, flags
);
1152 spin_unlock_irqrestore(&es2
->arpc_lock
, flags
);
1155 if (retval
< 0 && retval
!= -EREMOTEIO
) {
1156 dev_err(&es2
->usb_dev
->dev
,
1157 "failed to execute ARPC: %d\n", retval
);
1163 static void arpc_in_callback(struct urb
*urb
)
1165 struct es2_ap_dev
*es2
= urb
->context
;
1166 struct device
*dev
= &urb
->dev
->dev
;
1167 int status
= check_urb_status(urb
);
1169 struct arpc_response_message
*resp
;
1170 unsigned long flags
;
1174 if ((status
== -EAGAIN
) || (status
== -EPROTO
))
1177 /* The urb is being unlinked */
1178 if (status
== -ENOENT
|| status
== -ESHUTDOWN
)
1181 dev_err(dev
, "arpc in-urb error %d (dropped)\n", status
);
1185 if (urb
->actual_length
< sizeof(*resp
)) {
1186 dev_err(dev
, "short aprc response received\n");
1190 resp
= urb
->transfer_buffer
;
1191 spin_lock_irqsave(&es2
->arpc_lock
, flags
);
1192 rpc
= arpc_find(es2
, resp
->id
);
1194 dev_err(dev
, "invalid arpc response id received: %u\n",
1195 le16_to_cpu(resp
->id
));
1196 spin_unlock_irqrestore(&es2
->arpc_lock
, flags
);
1201 memcpy(rpc
->resp
, resp
, sizeof(*resp
));
1202 complete(&rpc
->response_received
);
1203 spin_unlock_irqrestore(&es2
->arpc_lock
, flags
);
1206 /* put our urb back in the request pool */
1207 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
1209 dev_err(dev
, "failed to resubmit arpc in-urb: %d\n", retval
);
1212 #define APB1_LOG_MSG_SIZE 64
1213 static void apb_log_get(struct es2_ap_dev
*es2
, char *buf
)
1218 retval
= usb_control_msg(es2
->usb_dev
,
1219 usb_rcvctrlpipe(es2
->usb_dev
, 0),
1221 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
,
1225 ES2_USB_CTRL_TIMEOUT
);
1227 kfifo_in(&es2
->apb_log_fifo
, buf
, retval
);
1228 } while (retval
> 0);
1231 static int apb_log_poll(void *data
)
1233 struct es2_ap_dev
*es2
= data
;
1236 buf
= kmalloc(APB1_LOG_MSG_SIZE
, GFP_KERNEL
);
1240 while (!kthread_should_stop()) {
1242 apb_log_get(es2
, buf
);
1250 static ssize_t
apb_log_read(struct file
*f
, char __user
*buf
,
1251 size_t count
, loff_t
*ppos
)
1253 struct es2_ap_dev
*es2
= f
->f_inode
->i_private
;
1258 if (count
> APB1_LOG_SIZE
)
1259 count
= APB1_LOG_SIZE
;
1261 tmp_buf
= kmalloc(count
, GFP_KERNEL
);
1265 copied
= kfifo_out(&es2
->apb_log_fifo
, tmp_buf
, count
);
1266 ret
= simple_read_from_buffer(buf
, count
, ppos
, tmp_buf
, copied
);
1273 static const struct file_operations apb_log_fops
= {
1274 .read
= apb_log_read
,
1277 static void usb_log_enable(struct es2_ap_dev
*es2
)
1279 if (!IS_ERR_OR_NULL(es2
->apb_log_task
))
1282 /* get log from APB1 */
1283 es2
->apb_log_task
= kthread_run(apb_log_poll
, es2
, "apb_log");
1284 if (IS_ERR(es2
->apb_log_task
))
1286 /* XXX We will need to rename this per APB */
1287 es2
->apb_log_dentry
= debugfs_create_file("apb_log", S_IRUGO
,
1288 gb_debugfs_get(), es2
,
1292 static void usb_log_disable(struct es2_ap_dev
*es2
)
1294 if (IS_ERR_OR_NULL(es2
->apb_log_task
))
1297 debugfs_remove(es2
->apb_log_dentry
);
1298 es2
->apb_log_dentry
= NULL
;
1300 kthread_stop(es2
->apb_log_task
);
1301 es2
->apb_log_task
= NULL
;
1304 static ssize_t
apb_log_enable_read(struct file
*f
, char __user
*buf
,
1305 size_t count
, loff_t
*ppos
)
1307 struct es2_ap_dev
*es2
= f
->f_inode
->i_private
;
1308 int enable
= !IS_ERR_OR_NULL(es2
->apb_log_task
);
1311 sprintf(tmp_buf
, "%d\n", enable
);
1312 return simple_read_from_buffer(buf
, count
, ppos
, tmp_buf
, 3);
1315 static ssize_t
apb_log_enable_write(struct file
*f
, const char __user
*buf
,
1316 size_t count
, loff_t
*ppos
)
1320 struct es2_ap_dev
*es2
= f
->f_inode
->i_private
;
1322 retval
= kstrtoint_from_user(buf
, count
, 10, &enable
);
1327 usb_log_enable(es2
);
1329 usb_log_disable(es2
);
1334 static const struct file_operations apb_log_enable_fops
= {
1335 .read
= apb_log_enable_read
,
1336 .write
= apb_log_enable_write
,
1339 static int apb_get_cport_count(struct usb_device
*udev
)
1342 __le16
*cport_count
;
1344 cport_count
= kzalloc(sizeof(*cport_count
), GFP_KERNEL
);
1348 retval
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
1349 GB_APB_REQUEST_CPORT_COUNT
,
1350 USB_DIR_IN
| USB_TYPE_VENDOR
|
1351 USB_RECIP_INTERFACE
, 0, 0, cport_count
,
1352 sizeof(*cport_count
), ES2_USB_CTRL_TIMEOUT
);
1353 if (retval
!= sizeof(*cport_count
)) {
1354 dev_err(&udev
->dev
, "Cannot retrieve CPort count: %d\n",
1363 retval
= le16_to_cpu(*cport_count
);
1365 /* We need to fit a CPort ID in one byte of a message header */
1366 if (retval
> U8_MAX
) {
1368 dev_warn(&udev
->dev
, "Limiting number of CPorts to U8_MAX\n");
1377 * The ES2 USB Bridge device has 15 endpoints
1378 * 1 Control - usual USB stuff + AP -> APBridgeA messages
1379 * 7 Bulk IN - CPort data in
1380 * 7 Bulk OUT - CPort data out
1382 static int ap_probe(struct usb_interface
*interface
,
1383 const struct usb_device_id
*id
)
1385 struct es2_ap_dev
*es2
;
1386 struct gb_host_device
*hd
;
1387 struct usb_device
*udev
;
1388 struct usb_host_interface
*iface_desc
;
1389 struct usb_endpoint_descriptor
*endpoint
;
1394 bool bulk_out_found
= false;
1395 bool bulk_in_found
= false;
1396 bool arpc_in_found
= false;
1398 udev
= usb_get_dev(interface_to_usbdev(interface
));
1400 num_cports
= apb_get_cport_count(udev
);
1401 if (num_cports
< 0) {
1403 dev_err(&udev
->dev
, "Cannot retrieve CPort count: %d\n",
1408 hd
= gb_hd_create(&es2_driver
, &udev
->dev
, ES2_GBUF_MSG_SIZE_MAX
,
1415 es2
= hd_to_es2(hd
);
1417 es2
->usb_intf
= interface
;
1418 es2
->usb_dev
= udev
;
1419 spin_lock_init(&es2
->cport_out_urb_lock
);
1420 INIT_KFIFO(es2
->apb_log_fifo
);
1421 usb_set_intfdata(interface
, es2
);
1424 * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
1427 retval
= gb_hd_cport_reserve(hd
, ES2_CPORT_CDSI0
);
1430 retval
= gb_hd_cport_reserve(hd
, ES2_CPORT_CDSI1
);
1434 /* find all bulk endpoints */
1435 iface_desc
= interface
->cur_altsetting
;
1436 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1437 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1438 ep_addr
= endpoint
->bEndpointAddress
;
1440 if (usb_endpoint_is_bulk_in(endpoint
)) {
1441 if (!bulk_in_found
) {
1442 es2
->cport_in
.endpoint
= ep_addr
;
1443 bulk_in_found
= true;
1444 } else if (!arpc_in_found
) {
1445 es2
->arpc_endpoint_in
= ep_addr
;
1446 arpc_in_found
= true;
1448 dev_warn(&udev
->dev
,
1449 "Unused bulk IN endpoint found: 0x%02x\n",
1454 if (usb_endpoint_is_bulk_out(endpoint
)) {
1455 if (!bulk_out_found
) {
1456 es2
->cport_out_endpoint
= ep_addr
;
1457 bulk_out_found
= true;
1459 dev_warn(&udev
->dev
,
1460 "Unused bulk OUT endpoint found: 0x%02x\n",
1465 dev_warn(&udev
->dev
,
1466 "Unknown endpoint type found, address 0x%02x\n",
1469 if (!bulk_in_found
|| !arpc_in_found
|| !bulk_out_found
) {
1470 dev_err(&udev
->dev
, "Not enough endpoints found in device, aborting!\n");
1475 /* Allocate buffers for our cport in messages */
1476 for (i
= 0; i
< NUM_CPORT_IN_URB
; ++i
) {
1480 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1485 es2
->cport_in
.urb
[i
] = urb
;
1487 buffer
= kmalloc(ES2_GBUF_MSG_SIZE_MAX
, GFP_KERNEL
);
1493 usb_fill_bulk_urb(urb
, udev
,
1494 usb_rcvbulkpipe(udev
, es2
->cport_in
.endpoint
),
1495 buffer
, ES2_GBUF_MSG_SIZE_MAX
,
1496 cport_in_callback
, hd
);
1498 es2
->cport_in
.buffer
[i
] = buffer
;
1501 /* Allocate buffers for ARPC in messages */
1502 for (i
= 0; i
< NUM_ARPC_IN_URB
; ++i
) {
1506 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1511 es2
->arpc_urb
[i
] = urb
;
1513 buffer
= kmalloc(ARPC_IN_SIZE_MAX
, GFP_KERNEL
);
1519 usb_fill_bulk_urb(urb
, udev
,
1520 usb_rcvbulkpipe(udev
,
1521 es2
->arpc_endpoint_in
),
1522 buffer
, ARPC_IN_SIZE_MAX
,
1523 arpc_in_callback
, es2
);
1525 es2
->arpc_buffer
[i
] = buffer
;
1528 /* Allocate urbs for our CPort OUT messages */
1529 for (i
= 0; i
< NUM_CPORT_OUT_URB
; ++i
) {
1532 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1538 es2
->cport_out_urb
[i
] = urb
;
1539 es2
->cport_out_urb_busy
[i
] = false; /* just to be anal */
1542 /* XXX We will need to rename this per APB */
1543 es2
->apb_log_enable_dentry
= debugfs_create_file("apb_log_enable",
1544 (S_IWUSR
| S_IRUGO
),
1545 gb_debugfs_get(), es2
,
1546 &apb_log_enable_fops
);
1548 INIT_LIST_HEAD(&es2
->arpcs
);
1549 spin_lock_init(&es2
->arpc_lock
);
1551 retval
= es2_arpc_in_enable(es2
);
1555 retval
= gb_hd_add(hd
);
1557 goto err_disable_arpc_in
;
1559 retval
= es2_cport_in_enable(es2
, &es2
->cport_in
);
1567 err_disable_arpc_in
:
1568 es2_arpc_in_disable(es2
);
1575 static void ap_disconnect(struct usb_interface
*interface
)
1577 struct es2_ap_dev
*es2
= usb_get_intfdata(interface
);
1581 es2_cport_in_disable(es2
, &es2
->cport_in
);
1582 es2_arpc_in_disable(es2
);
1587 static struct usb_driver es2_ap_driver
= {
1588 .name
= "es2_ap_driver",
1590 .disconnect
= ap_disconnect
,
1591 .id_table
= id_table
,
1595 module_usb_driver(es2_ap_driver
);
1597 MODULE_LICENSE("GPL v2");
1598 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");