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