blob: 8ef3a0426a0923a06a3d9af7900ee5d14e9834d7 [file] [log] [blame]
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +08001/*
2 * Greybus driver and device API
3 *
Alex Elder4441f472015-05-22 12:59:16 -05004 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +08006 *
7 * Released under the GPLv2 only.
8 */
9
10#ifndef __LINUX_GREYBUS_H
11#define __LINUX_GREYBUS_H
12
13#ifdef __KERNEL__
14
Alex Eldere1e9dbd2014-10-01 21:54:11 -050015#include <linux/kernel.h>
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070016#include <linux/types.h>
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080017#include <linux/list.h>
Alex Eldere1e9dbd2014-10-01 21:54:11 -050018#include <linux/slab.h>
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080019#include <linux/device.h>
20#include <linux/module.h>
Alex Elder177404b2014-10-03 14:14:24 -050021#include <linux/idr.h>
Alex Eldere1e9dbd2014-10-01 21:54:11 -050022
Alex Elder1bb3c722014-10-02 12:30:03 -050023#include "kernel_ver.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080024#include "greybus_id.h"
Alex Elder05ad1892014-09-09 13:55:03 -050025#include "greybus_manifest.h"
Alex Elder22e17ed2015-05-22 12:35:32 -050026#include "greybus_protocols.h"
Alex Elderb09c94a2014-10-01 21:54:16 -050027#include "manifest.h"
Johan Hovold7bc6faa2015-11-03 18:03:22 +010028#include "hd.h"
Greg Kroah-Hartman0f035ac2015-04-07 20:26:53 +020029#include "endo.h"
Alex Elder30c6d9d2015-05-22 13:02:08 -050030#include "svc.h"
Viresh Kumar90f1b612015-08-12 09:19:33 +053031#include "firmware.h"
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -080032#include "module.h"
Viresh Kumarcdee4f72015-06-22 16:42:26 +053033#include "control.h"
Greg Kroah-Hartmana93938a2014-12-19 14:56:30 -080034#include "interface.h"
Greg Kroah-Hartman3bdec692014-12-12 17:10:16 -050035#include "bundle.h"
Alex Elderc68adb22014-10-01 21:54:14 -050036#include "connection.h"
Alex Elder4ccb6b72014-10-28 19:36:00 -050037#include "protocol.h"
Alex Eldere88afa52014-10-01 21:54:15 -050038#include "operation.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080039
40
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070041/* Matches up with the Greybus Protocol specification document */
Matt Porter52adb562014-09-18 15:25:43 -040042#define GREYBUS_VERSION_MAJOR 0x00
43#define GREYBUS_VERSION_MINOR 0x01
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070044
Johan Hovold358e9402015-11-21 10:51:59 +010045#define GREYBUS_ID_MATCH_DEVICE \
46 (GREYBUS_ID_MATCH_VENDOR | GREYBUS_ID_MATCH_PRODUCT)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080047
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070048#define GREYBUS_DEVICE(v, p) \
Johan Hovold358e9402015-11-21 10:51:59 +010049 .match_flags = GREYBUS_ID_MATCH_DEVICE, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070050 .vendor = (v), \
51 .product = (p),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080052
Johan Hovold3823c612015-11-21 10:52:04 +010053#define GREYBUS_DEVICE_CLASS(c) \
54 .match_flags = GREYBUS_ID_MATCH_CLASS, \
55 .class = (c),
56
Johan Hovold1dc53922015-09-02 18:03:21 +020057/* Maximum number of CPorts */
58#define CPORT_ID_MAX 4095 /* UniPro max id is 4095 */
59#define CPORT_ID_BAD U16_MAX
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080060
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080061struct greybus_driver {
62 const char *name;
63
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +053064 int (*probe)(struct gb_bundle *bundle,
65 const struct greybus_bundle_id *id);
66 void (*disconnect)(struct gb_bundle *bundle);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080067
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +053068 int (*suspend)(struct gb_bundle *bundle, pm_message_t message);
69 int (*resume)(struct gb_bundle *bundle);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080070
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +053071 const struct greybus_bundle_id *id_table;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080072
73 struct device_driver driver;
74};
75#define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
76
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080077/* Don't call these directly, use the module_greybus_driver() macro instead */
78int greybus_register_driver(struct greybus_driver *driver,
79 struct module *module, const char *mod_name);
Alex Elderfd1c2e52015-06-08 12:05:13 -050080void greybus_deregister_driver(struct greybus_driver *driver);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080081
82/* define to get proper THIS_MODULE and KBUILD_MODNAME values */
83#define greybus_register(driver) \
84 greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
Alex Elderfd1c2e52015-06-08 12:05:13 -050085#define greybus_deregister(driver) \
86 greybus_deregister_driver(driver)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080087
88/**
89 * module_greybus_driver() - Helper macro for registering a Greybus driver
90 * @__greybus_driver: greybus_driver structure
91 *
92 * Helper macro for Greybus drivers to set up proper module init / exit
93 * functions. Replaces module_init() and module_exit() and keeps people from
94 * printing pointless things to the kernel log when their driver is loaded.
95 */
96#define module_greybus_driver(__greybus_driver) \
97 module_driver(__greybus_driver, greybus_register, greybus_deregister)
98
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080099int greybus_disabled(void);
100
Greg Kroah-Hartman3d0421e2015-06-11 09:22:51 -0700101void gb_debugfs_init(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700102void gb_debugfs_cleanup(void);
Alexandre Bailone8f824b2015-03-18 15:42:51 +0100103struct dentry *gb_debugfs_get(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700104
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +0800105extern struct bus_type greybus_bus_type;
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700106
Johan Hovold2adaefb2015-11-25 15:59:02 +0100107extern struct device_type greybus_hd_type;
Greg Kroah-Hartman0f035ac2015-04-07 20:26:53 +0200108extern struct device_type greybus_endo_type;
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -0800109extern struct device_type greybus_module_type;
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800110extern struct device_type greybus_interface_type;
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500111extern struct device_type greybus_bundle_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800112
Johan Hovold2adaefb2015-11-25 15:59:02 +0100113static inline int is_gb_host_device(const struct device *dev)
114{
115 return dev->type == &greybus_hd_type;
116}
117
Greg Kroah-Hartman0f035ac2015-04-07 20:26:53 +0200118static inline int is_gb_endo(const struct device *dev)
119{
120 return dev->type == &greybus_endo_type;
121}
122
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -0800123static inline int is_gb_module(const struct device *dev)
124{
125 return dev->type == &greybus_module_type;
126}
127
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800128static inline int is_gb_interface(const struct device *dev)
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800129{
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800130 return dev->type == &greybus_interface_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800131}
132
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500133static inline int is_gb_bundle(const struct device *dev)
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800134{
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500135 return dev->type == &greybus_bundle_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800136}
137
Johan Hovold25376362015-11-03 18:03:23 +0100138static inline bool cport_id_valid(struct gb_host_device *hd, u16 cport_id)
Alex Elder821c6202015-06-13 11:02:07 -0500139{
Fabien Parent144670c2015-09-02 15:50:35 +0200140 return cport_id != CPORT_ID_BAD && cport_id < hd->num_cports;
Alex Elder821c6202015-06-13 11:02:07 -0500141}
142
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800143#endif /* __KERNEL__ */
144#endif /* __LINUX_GREYBUS_H */