Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Greybus driver and device API |
| 3 | * |
Alex Elder | 4441f47 | 2015-05-22 12:59:16 -0500 | [diff] [blame] | 4 | * Copyright 2014-2015 Google Inc. |
| 5 | * Copyright 2014-2015 Linaro Ltd. |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
| 10 | #ifndef __LINUX_GREYBUS_H |
| 11 | #define __LINUX_GREYBUS_H |
| 12 | |
| 13 | #ifdef __KERNEL__ |
| 14 | |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 15 | #include <linux/kernel.h> |
Greg Kroah-Hartman | 6dca7b9 | 2014-09-01 13:42:43 -0700 | [diff] [blame] | 16 | #include <linux/types.h> |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 17 | #include <linux/list.h> |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 18 | #include <linux/slab.h> |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 19 | #include <linux/device.h> |
| 20 | #include <linux/module.h> |
David Lin | 30a3bf7 | 2016-07-14 15:13:00 -0500 | [diff] [blame] | 21 | #include <linux/pm_runtime.h> |
Alex Elder | 177404b | 2014-10-03 14:14:24 -0500 | [diff] [blame] | 22 | #include <linux/idr.h> |
Alex Elder | e1e9dbd | 2014-10-01 21:54:11 -0500 | [diff] [blame] | 23 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 24 | #include "greybus_id.h" |
Alex Elder | 05ad189 | 2014-09-09 13:55:03 -0500 | [diff] [blame] | 25 | #include "greybus_manifest.h" |
Alex Elder | 22e17ed | 2015-05-22 12:35:32 -0500 | [diff] [blame] | 26 | #include "greybus_protocols.h" |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 27 | #include "manifest.h" |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 28 | #include "hd.h" |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 29 | #include "svc.h" |
Viresh Kumar | cdee4f7 | 2015-06-22 16:42:26 +0530 | [diff] [blame] | 30 | #include "control.h" |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 31 | #include "module.h" |
Greg Kroah-Hartman | a93938a | 2014-12-19 14:56:30 -0800 | [diff] [blame] | 32 | #include "interface.h" |
Greg Kroah-Hartman | 3bdec69 | 2014-12-12 17:10:16 -0500 | [diff] [blame] | 33 | #include "bundle.h" |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 34 | #include "connection.h" |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 35 | #include "operation.h" |
Bryan O'Donoghue | 970dc85 | 2016-06-05 14:03:26 +0100 | [diff] [blame] | 36 | #include "timesync.h" |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 37 | |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 38 | /* Matches up with the Greybus Protocol specification document */ |
Matt Porter | 52adb56 | 2014-09-18 15:25:43 -0400 | [diff] [blame] | 39 | #define GREYBUS_VERSION_MAJOR 0x00 |
| 40 | #define GREYBUS_VERSION_MINOR 0x01 |
Greg Kroah-Hartman | 8c53e07 | 2014-09-12 20:47:11 -0700 | [diff] [blame] | 41 | |
Johan Hovold | 358e940 | 2015-11-21 10:51:59 +0100 | [diff] [blame] | 42 | #define GREYBUS_ID_MATCH_DEVICE \ |
| 43 | (GREYBUS_ID_MATCH_VENDOR | GREYBUS_ID_MATCH_PRODUCT) |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 44 | |
Greg Kroah-Hartman | 6dca7b9 | 2014-09-01 13:42:43 -0700 | [diff] [blame] | 45 | #define GREYBUS_DEVICE(v, p) \ |
Johan Hovold | 358e940 | 2015-11-21 10:51:59 +0100 | [diff] [blame] | 46 | .match_flags = GREYBUS_ID_MATCH_DEVICE, \ |
Greg Kroah-Hartman | 6dca7b9 | 2014-09-01 13:42:43 -0700 | [diff] [blame] | 47 | .vendor = (v), \ |
| 48 | .product = (p), |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 49 | |
Johan Hovold | 3823c61 | 2015-11-21 10:52:04 +0100 | [diff] [blame] | 50 | #define GREYBUS_DEVICE_CLASS(c) \ |
| 51 | .match_flags = GREYBUS_ID_MATCH_CLASS, \ |
| 52 | .class = (c), |
| 53 | |
Johan Hovold | 1dc5392 | 2015-09-02 18:03:21 +0200 | [diff] [blame] | 54 | /* Maximum number of CPorts */ |
| 55 | #define CPORT_ID_MAX 4095 /* UniPro max id is 4095 */ |
| 56 | #define CPORT_ID_BAD U16_MAX |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 57 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 58 | struct greybus_driver { |
| 59 | const char *name; |
| 60 | |
Viresh Kumar | 9f5f30e712 | 2015-04-01 20:32:04 +0530 | [diff] [blame] | 61 | int (*probe)(struct gb_bundle *bundle, |
| 62 | const struct greybus_bundle_id *id); |
| 63 | void (*disconnect)(struct gb_bundle *bundle); |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 64 | |
Viresh Kumar | 9f5f30e712 | 2015-04-01 20:32:04 +0530 | [diff] [blame] | 65 | const struct greybus_bundle_id *id_table; |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 66 | |
| 67 | struct device_driver driver; |
| 68 | }; |
| 69 | #define to_greybus_driver(d) container_of(d, struct greybus_driver, driver) |
| 70 | |
Johan Hovold | 13da9e1 | 2016-01-08 20:13:43 +0100 | [diff] [blame] | 71 | static inline void greybus_set_drvdata(struct gb_bundle *bundle, void *data) |
| 72 | { |
| 73 | dev_set_drvdata(&bundle->dev, data); |
| 74 | } |
| 75 | |
| 76 | static inline void *greybus_get_drvdata(struct gb_bundle *bundle) |
| 77 | { |
| 78 | return dev_get_drvdata(&bundle->dev); |
| 79 | } |
| 80 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 81 | /* Don't call these directly, use the module_greybus_driver() macro instead */ |
| 82 | int greybus_register_driver(struct greybus_driver *driver, |
| 83 | struct module *module, const char *mod_name); |
Alex Elder | fd1c2e5 | 2015-06-08 12:05:13 -0500 | [diff] [blame] | 84 | void greybus_deregister_driver(struct greybus_driver *driver); |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 85 | |
| 86 | /* define to get proper THIS_MODULE and KBUILD_MODNAME values */ |
| 87 | #define greybus_register(driver) \ |
| 88 | greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME) |
Alex Elder | fd1c2e5 | 2015-06-08 12:05:13 -0500 | [diff] [blame] | 89 | #define greybus_deregister(driver) \ |
| 90 | greybus_deregister_driver(driver) |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * module_greybus_driver() - Helper macro for registering a Greybus driver |
| 94 | * @__greybus_driver: greybus_driver structure |
| 95 | * |
| 96 | * Helper macro for Greybus drivers to set up proper module init / exit |
| 97 | * functions. Replaces module_init() and module_exit() and keeps people from |
| 98 | * printing pointless things to the kernel log when their driver is loaded. |
| 99 | */ |
| 100 | #define module_greybus_driver(__greybus_driver) \ |
| 101 | module_driver(__greybus_driver, greybus_register, greybus_deregister) |
| 102 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 103 | int greybus_disabled(void); |
| 104 | |
Greg Kroah-Hartman | 3d0421e | 2015-06-11 09:22:51 -0700 | [diff] [blame] | 105 | void gb_debugfs_init(void); |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 106 | void gb_debugfs_cleanup(void); |
Alexandre Bailon | e8f824b | 2015-03-18 15:42:51 +0100 | [diff] [blame] | 107 | struct dentry *gb_debugfs_get(void); |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame] | 108 | |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 109 | extern struct bus_type greybus_bus_type; |
Greg Kroah-Hartman | 06340ef | 2014-09-01 19:05:54 -0700 | [diff] [blame] | 110 | |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 111 | extern struct device_type greybus_hd_type; |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 112 | extern struct device_type greybus_module_type; |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 113 | extern struct device_type greybus_interface_type; |
Johan Hovold | a6e5b01 | 2016-04-13 19:19:02 +0200 | [diff] [blame] | 114 | extern struct device_type greybus_control_type; |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 115 | extern struct device_type greybus_bundle_type; |
Johan Hovold | 88f7b96 | 2015-11-25 15:59:08 +0100 | [diff] [blame] | 116 | extern struct device_type greybus_svc_type; |
Greg Kroah-Hartman | 0ac5a83 | 2014-11-15 12:12:16 -0800 | [diff] [blame] | 117 | |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 118 | static inline int is_gb_host_device(const struct device *dev) |
| 119 | { |
| 120 | return dev->type == &greybus_hd_type; |
| 121 | } |
| 122 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 123 | static inline int is_gb_module(const struct device *dev) |
| 124 | { |
| 125 | return dev->type == &greybus_module_type; |
| 126 | } |
| 127 | |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 128 | static inline int is_gb_interface(const struct device *dev) |
Greg Kroah-Hartman | 0ac5a83 | 2014-11-15 12:12:16 -0800 | [diff] [blame] | 129 | { |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 130 | return dev->type == &greybus_interface_type; |
Greg Kroah-Hartman | 0ac5a83 | 2014-11-15 12:12:16 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Johan Hovold | a6e5b01 | 2016-04-13 19:19:02 +0200 | [diff] [blame] | 133 | static inline int is_gb_control(const struct device *dev) |
| 134 | { |
| 135 | return dev->type == &greybus_control_type; |
| 136 | } |
| 137 | |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 138 | static inline int is_gb_bundle(const struct device *dev) |
Greg Kroah-Hartman | 0ac5a83 | 2014-11-15 12:12:16 -0800 | [diff] [blame] | 139 | { |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 140 | return dev->type == &greybus_bundle_type; |
Greg Kroah-Hartman | 0ac5a83 | 2014-11-15 12:12:16 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Johan Hovold | 88f7b96 | 2015-11-25 15:59:08 +0100 | [diff] [blame] | 143 | static inline int is_gb_svc(const struct device *dev) |
| 144 | { |
| 145 | return dev->type == &greybus_svc_type; |
| 146 | } |
| 147 | |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 148 | static inline bool cport_id_valid(struct gb_host_device *hd, u16 cport_id) |
Alex Elder | 821c620 | 2015-06-13 11:02:07 -0500 | [diff] [blame] | 149 | { |
Fabien Parent | 144670c | 2015-09-02 15:50:35 +0200 | [diff] [blame] | 150 | return cport_id != CPORT_ID_BAD && cport_id < hd->num_cports; |
Alex Elder | 821c620 | 2015-06-13 11:02:07 -0500 | [diff] [blame] | 151 | } |
| 152 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 153 | #endif /* __KERNEL__ */ |
| 154 | #endif /* __LINUX_GREYBUS_H */ |