blob: 175bd00381c7a0aa8d95d81f9337348a2afbdd4b [file] [log] [blame]
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +08001/*
2 * Greybus driver and device API
3 *
4 * Copyright 2014 Google Inc.
5 *
6 * Released under the GPLv2 only.
7 */
8
9#ifndef __LINUX_GREYBUS_H
10#define __LINUX_GREYBUS_H
11
12#ifdef __KERNEL__
13
Alex Eldere1e9dbd2014-10-01 21:54:11 -050014#include <linux/kernel.h>
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070015#include <linux/types.h>
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080016#include <linux/list.h>
Alex Eldere1e9dbd2014-10-01 21:54:11 -050017#include <linux/slab.h>
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080018#include <linux/device.h>
19#include <linux/module.h>
Alex Elder177404b2014-10-03 14:14:24 -050020#include <linux/idr.h>
Alex Eldere1e9dbd2014-10-01 21:54:11 -050021
Alex Elder1bb3c722014-10-02 12:30:03 -050022#include "kernel_ver.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080023#include "greybus_id.h"
Alex Elder05ad1892014-09-09 13:55:03 -050024#include "greybus_manifest.h"
Alex Elderb09c94a2014-10-01 21:54:16 -050025#include "manifest.h"
Alex Eldere1e9dbd2014-10-01 21:54:11 -050026#include "module.h"
Alex Elder8c12cde2014-10-01 21:54:12 -050027#include "interface.h"
Alex Elderc68adb22014-10-01 21:54:14 -050028#include "connection.h"
Alex Elder4ccb6b72014-10-28 19:36:00 -050029#include "protocol.h"
Alex Eldere88afa52014-10-01 21:54:15 -050030#include "operation.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080031
32
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070033/* Matches up with the Greybus Protocol specification document */
Matt Porter52adb562014-09-18 15:25:43 -040034#define GREYBUS_VERSION_MAJOR 0x00
35#define GREYBUS_VERSION_MINOR 0x01
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070036
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080037#define GREYBUS_DEVICE_ID_MATCH_DEVICE \
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -070038 (GREYBUS_DEVICE_ID_MATCH_VENDOR | GREYBUS_DEVICE_ID_MATCH_PRODUCT)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080039
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070040#define GREYBUS_DEVICE(v, p) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080041 .match_flags = GREYBUS_DEVICE_ID_MATCH_DEVICE, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070042 .vendor = (v), \
43 .product = (p),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080044
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070045#define GREYBUS_DEVICE_SERIAL(s) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080046 .match_flags = GREYBUS_DEVICE_ID_MATCH_SERIAL, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070047 .serial_number = (s),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080048
Alex Elder1bb3c722014-10-02 12:30:03 -050049/* XXX I couldn't get my Kconfig file to be noticed for out-of-tree build */
50#ifndef CONFIG_HOST_DEV_CPORT_ID_MAX
51#define CONFIG_HOST_DEV_CPORT_ID_MAX 128
52#endif /* !CONFIG_HOST_DEV_CPORT_ID_MAX */
53
54/* Maximum number of CPorts usable by a host device */
55/* XXX This should really be determined by the AP module manifest */
56#define HOST_DEV_CPORT_ID_MAX CONFIG_HOST_DEV_CPORT_ID_MAX
57#define CPORT_ID_BAD U16_MAX /* UniPro max id is 4095 */
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080058
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -070059/*
60 gbuf
61
62 This is the "main" data structure to send / receive Greybus messages
63
64 There are two different "views" of a gbuf structure:
65 - a greybus driver
66 - a greybus host controller
67
68 A Greybus driver needs to worry about the following:
69 - creating a gbuf
70 - putting data into a gbuf
71 - sending a gbuf to a device
72 - receiving a gbuf from a device
73
74 Creating a gbuf:
75 A greybus driver calls greybus_alloc_gbuf()
76 Putting data into a gbuf:
77 copy data into gbuf->transfer_buffer
78 Send a gbuf:
79 A greybus driver calls greybus_submit_gbuf()
80 The completion function in a gbuf will be called if the gbuf is successful
81 or not. That completion function runs in user context. After the
82 completion function is called, the gbuf must not be touched again as the
83 greybus core "owns" it. But, if a greybus driver wants to "hold on" to a
84 gbuf after the completion function has been called, a reference must be
85 grabbed on the gbuf with a call to greybus_get_gbuf(). When finished with
86 the gbuf, call greybus_free_gbuf() and when the last reference count is
87 dropped, it will be removed from the system.
88 Receive a gbuf:
89 A greybus driver calls gb_register_cport_complete() with a pointer to the
90 callback function to be called for when a gbuf is received from a specific
91 cport and device. That callback will be made in user context with a gbuf
92 when it is received. To stop receiving messages, call
93 gb_deregister_cport_complete() for a specific cport.
94
95
96 Greybus Host controller drivers need to provide
97 - a way to allocate the transfer buffer for a gbuf
98 - a way to free the transfer buffer for a gbuf when it is "finished"
99 - a way to submit gbuf for transmissions
100 - notify the core the gbuf is complete
101 - receive gbuf from the wire and submit them to the core
102 - a way to send and receive svc messages
103 Allocate a transfer buffer
104 the host controller function alloc_gbuf_data is called
105 Free a transfer buffer
106 the host controller function free_gbuf_data is called
107 Submit a gbuf to the hardware
108 the host controller function submit_gbuf is called
109 Notify the gbuf is complete
110 the host controller driver must call greybus_gbuf_finished()
111 Submit a SVC message to the hardware
112 the host controller function send_svc_msg is called
113 Receive gbuf messages
Alex Elder0db32a62014-09-24 05:16:14 -0500114 the host controller driver must call greybus_cport_in() with the data
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -0700115 Reveive SVC messages from the hardware
Alex Elder0db32a62014-09-24 05:16:14 -0500116 The host controller driver must call greybus_svc_in
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -0700117
118
119*/
120
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800121
122struct gbuf;
123
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800124typedef void (*gbuf_complete_t)(struct gbuf *gbuf);
125
126struct gbuf {
127 struct kref kref;
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800128
Alex Elder6eb3f4b2014-10-06 06:53:10 -0500129 struct gb_connection *connection;
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800130 int status;
131 void *transfer_buffer;
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800132 u32 transfer_buffer_length;
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800133
Alex Elder9a6f6312014-10-06 06:53:11 -0500134 bool outbound; /* AP-relative data direction */
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800135
136 void *context;
Greg Kroah-Hartmand8144882014-10-27 13:31:01 +0800137 void *hcd_data; /* for the HCD to track the gbuf */
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800138 gbuf_complete_t complete;
139};
140
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800141
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700142/* For SP1 hardware, we are going to "hardcode" each device to have all logical
143 * blocks in order to be able to address them as one unified "unit". Then
144 * higher up layers will then be able to talk to them as one logical block and
145 * properly know how they are hooked together (i.e. which i2c port is on the
146 * same module as the gpio pins, etc.)
147 *
148 * So, put the "private" data structures here in greybus.h and link to them off
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500149 * of the "main" gb_module structure.
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700150 */
151
152struct gb_i2c_device;
153struct gb_gpio_device;
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700154struct gb_tty;
155struct gb_usb_device;
Greg Kroah-Hartman33ea3a32014-09-07 15:39:34 -0700156struct gb_battery;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700157struct greybus_host_device;
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700158struct svc_msg;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700159
160/* Greybus "Host driver" structure, needed by a host controller driver to be
161 * able to handle both SVC control as well as "real" greybus messages
162 */
163struct greybus_host_driver {
164 size_t hd_priv_size;
165
Alex Elder9a6f6312014-10-06 06:53:11 -0500166 int (*alloc_gbuf_data)(struct gbuf *gbuf, unsigned int size,
167 gfp_t gfp_mask);
Greg Kroah-Hartman3e7736e2014-09-21 17:34:28 -0700168 void (*free_gbuf_data)(struct gbuf *gbuf);
Alex Elder0db32a62014-09-24 05:16:14 -0500169 int (*submit_svc)(struct svc_msg *svc_msg,
Greg Kroah-Hartmanf036e052014-09-19 19:13:33 -0700170 struct greybus_host_device *hd);
Alex Elder61418b92014-10-16 06:35:29 -0500171 int (*submit_gbuf)(struct gbuf *gbuf, gfp_t gfp_mask);
Greg Kroah-Hartman4afbba02014-10-27 14:01:06 +0800172 void (*kill_gbuf)(struct gbuf *gbuf);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700173};
174
175struct greybus_host_device {
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -0700176 struct kref kref;
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -0700177 struct device *parent;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700178 const struct greybus_host_driver *driver;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700179
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500180 struct list_head modules;
Alex Elderee9ebe42014-10-06 06:53:08 -0500181 struct rb_root connections;
Alex Elder177404b2014-10-03 14:14:24 -0500182 struct ida cport_id_map;
Greg Kroah-Hartman25b7b6d2014-10-06 20:29:40 -0700183 spinlock_t cport_id_map_lock;
Matt Porter98f4ab22014-10-21 01:52:27 -0400184 u8 device_id;
Alex Elder1bb3c722014-10-02 12:30:03 -0500185
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700186 /* Private data for the host driver */
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700187 unsigned long hd_priv[0] __aligned(sizeof(s64));
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700188};
189
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700190struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *hd,
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700191 struct device *parent);
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700192void greybus_remove_hd(struct greybus_host_device *hd);
Alex Elder1cfc6672014-09-30 19:25:21 -0500193void greybus_cport_in(struct greybus_host_device *hd, u16 cport_id,
194 u8 *data, size_t length);
Greg Kroah-Hartman9c8d3af2014-09-13 11:09:35 -0700195void greybus_gbuf_finished(struct gbuf *gbuf);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700196
Alex Elder6eb3f4b2014-10-06 06:53:10 -0500197struct gbuf *greybus_alloc_gbuf(struct gb_connection *connection,
Alex Elderecf7d572014-10-01 21:54:10 -0500198 gbuf_complete_t complete, unsigned int size,
Alex Elder9a6f6312014-10-06 06:53:11 -0500199 bool outbound, gfp_t gfp_mask, void *context);
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800200void greybus_free_gbuf(struct gbuf *gbuf);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700201struct gbuf *greybus_get_gbuf(struct gbuf *gbuf);
202#define greybus_put_gbuf greybus_free_gbuf
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800203
204int greybus_submit_gbuf(struct gbuf *gbuf, gfp_t mem_flags);
Greg Kroah-Hartman4afbba02014-10-27 14:01:06 +0800205void greybus_kill_gbuf(struct gbuf *gbuf);
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800206
207
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800208struct greybus_driver {
209 const char *name;
210
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500211 int (*probe)(struct gb_module *gmod,
Greg Kroah-Hartman6584c8a2014-09-01 13:31:31 -0700212 const struct greybus_module_id *id);
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500213 void (*disconnect)(struct gb_module *gmod);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800214
Alex Eldere1e9dbd2014-10-01 21:54:11 -0500215 int (*suspend)(struct gb_module *gmod, pm_message_t message);
216 int (*resume)(struct gb_module *gmod);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800217
Greg Kroah-Hartman6584c8a2014-09-01 13:31:31 -0700218 const struct greybus_module_id *id_table;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800219
220 struct device_driver driver;
221};
222#define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
223
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800224/* Don't call these directly, use the module_greybus_driver() macro instead */
225int greybus_register_driver(struct greybus_driver *driver,
226 struct module *module, const char *mod_name);
227void greybus_deregister(struct greybus_driver *driver);
228
229/* define to get proper THIS_MODULE and KBUILD_MODNAME values */
230#define greybus_register(driver) \
231 greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
232
233/**
234 * module_greybus_driver() - Helper macro for registering a Greybus driver
235 * @__greybus_driver: greybus_driver structure
236 *
237 * Helper macro for Greybus drivers to set up proper module init / exit
238 * functions. Replaces module_init() and module_exit() and keeps people from
239 * printing pointless things to the kernel log when their driver is loaded.
240 */
241#define module_greybus_driver(__greybus_driver) \
242 module_driver(__greybus_driver, greybus_register, greybus_deregister)
243
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800244int greybus_disabled(void);
245
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700246/* Internal functions to gb module, move to internal .h file eventually. */
247
Greg Kroah-Hartmand0cfd102014-09-21 19:10:39 -0700248void gb_add_module(struct greybus_host_device *hd, u8 module_id,
249 u8 *data, int size);
Greg Kroah-Hartman85e00662014-09-21 18:17:12 -0700250void gb_remove_module(struct greybus_host_device *hd, u8 module_id);
251
Alex Elder51c75fd2014-09-26 20:55:35 -0500252int greybus_svc_in(struct greybus_host_device *hd, u8 *data, int length);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700253int gb_ap_init(void);
254void gb_ap_exit(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700255int gb_debugfs_init(void);
256void gb_debugfs_cleanup(void);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700257int gb_gbuf_init(void);
258void gb_gbuf_exit(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700259
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +0800260extern struct bus_type greybus_bus_type;
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700261extern const struct attribute_group *greybus_module_groups[];
262
Alex Elder3689f972014-10-27 06:04:30 -0500263extern struct gb_connection_handler gb_i2c_connection_handler;
264extern struct gb_connection_handler gb_gpio_connection_handler;
265extern struct gb_connection_handler gb_battery_connection_handler;
266extern struct gb_connection_handler gb_uart_connection_handler;
Greg Kroah-Hartmana2f47632014-10-28 10:17:09 +0800267extern struct gb_connection_handler gb_sdio_connection_handler;
Alex Elder574341c2014-10-16 06:35:35 -0500268
Greg Kroah-Hartmanaed0bc62014-10-27 17:32:34 +0800269int gb_uart_device_init(struct gb_connection *connection);
270void gb_uart_device_exit(struct gb_connection *connection);
271
Alex Elder525f1462014-10-22 02:04:31 -0500272int svc_set_route_send(struct gb_interface *interface,
273 struct greybus_host_device *hd);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800274
275#endif /* __KERNEL__ */
276#endif /* __LINUX_GREYBUS_H */