1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Greybus connections
4 *
5 * Copyright 2014 Google Inc.
6 * Copyright 2014 Linaro Ltd.
7 */
8
9#ifndef __CONNECTION_H
10#define __CONNECTION_H
11
12#include <linux/bits.h>
13#include <linux/list.h>
14#include <linux/kfifo.h>
15#include <linux/kref.h>
16#include <linux/workqueue.h>
17
18#define GB_CONNECTION_FLAG_CSD		BIT(0)
19#define GB_CONNECTION_FLAG_NO_FLOWCTRL	BIT(1)
20#define GB_CONNECTION_FLAG_OFFLOADED	BIT(2)
21#define GB_CONNECTION_FLAG_CDSI1	BIT(3)
22#define GB_CONNECTION_FLAG_CONTROL	BIT(4)
23#define GB_CONNECTION_FLAG_HIGH_PRIO	BIT(5)
24
25#define GB_CONNECTION_FLAG_CORE_MASK	GB_CONNECTION_FLAG_CONTROL
26
27enum gb_connection_state {
28	GB_CONNECTION_STATE_DISABLED		= 0,
29	GB_CONNECTION_STATE_ENABLED_TX		= 1,
30	GB_CONNECTION_STATE_ENABLED		= 2,
31	GB_CONNECTION_STATE_DISCONNECTING	= 3,
32};
33
34struct gb_operation;
35
36typedef int (*gb_request_handler_t)(struct gb_operation *);
37
38struct gb_connection {
39	struct gb_host_device		*hd;
40	struct gb_interface		*intf;
41	struct gb_bundle		*bundle;
42	struct kref			kref;
43	u16				hd_cport_id;
44	u16				intf_cport_id;
45
46	struct list_head		hd_links;
47	struct list_head		bundle_links;
48
49	gb_request_handler_t		handler;
50	unsigned long			flags;
51
52	struct mutex			mutex;
53	spinlock_t			lock;
54	enum gb_connection_state	state;
55	struct list_head		operations;
56
57	char				name[16];
58	struct workqueue_struct		*wq;
59
60	atomic_t			op_cycle;
61
62	void				*private;
63
64	bool				mode_switch;
65};
66
67struct gb_connection *gb_connection_create_static(struct gb_host_device *hd,
68				u16 hd_cport_id, gb_request_handler_t handler);
69struct gb_connection *gb_connection_create_control(struct gb_interface *intf);
70struct gb_connection *gb_connection_create(struct gb_bundle *bundle,
71				u16 cport_id, gb_request_handler_t handler);
72struct gb_connection *gb_connection_create_flags(struct gb_bundle *bundle,
73				u16 cport_id, gb_request_handler_t handler,
74				unsigned long flags);
75struct gb_connection *gb_connection_create_offloaded(struct gb_bundle *bundle,
76				u16 cport_id, unsigned long flags);
77void gb_connection_destroy(struct gb_connection *connection);
78
79static inline bool gb_connection_is_static(struct gb_connection *connection)
80{
81	return !connection->intf;
82}
83
84int gb_connection_enable(struct gb_connection *connection);
85int gb_connection_enable_tx(struct gb_connection *connection);
86void gb_connection_disable_rx(struct gb_connection *connection);
87void gb_connection_disable(struct gb_connection *connection);
88void gb_connection_disable_forced(struct gb_connection *connection);
89
90void gb_connection_mode_switch_prepare(struct gb_connection *connection);
91void gb_connection_mode_switch_complete(struct gb_connection *connection);
92
93void greybus_data_rcvd(struct gb_host_device *hd, u16 cport_id,
94		       u8 *data, size_t length);
95
96void gb_connection_latency_tag_enable(struct gb_connection *connection);
97void gb_connection_latency_tag_disable(struct gb_connection *connection);
98
99static inline bool gb_connection_e2efc_enabled(struct gb_connection *connection)
100{
101	return !(connection->flags & GB_CONNECTION_FLAG_CSD);
102}
103
104static inline bool
105gb_connection_flow_control_disabled(struct gb_connection *connection)
106{
107	return connection->flags & GB_CONNECTION_FLAG_NO_FLOWCTRL;
108}
109
110static inline bool gb_connection_is_offloaded(struct gb_connection *connection)
111{
112	return connection->flags & GB_CONNECTION_FLAG_OFFLOADED;
113}
114
115static inline bool gb_connection_is_control(struct gb_connection *connection)
116{
117	return connection->flags & GB_CONNECTION_FLAG_CONTROL;
118}
119
120static inline void *gb_connection_get_data(struct gb_connection *connection)
121{
122	return connection->private;
123}
124
125static inline void gb_connection_set_data(struct gb_connection *connection,
126					  void *data)
127{
128	connection->private = data;
129}
130
131#endif /* __CONNECTION_H */
132