blob: f5447e7592e9c57f87f078c2c5c3e68ce96cfb0e [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"
Alex Elder30c6d9d2015-05-22 13:02:08 -050029#include "svc.h"
Viresh Kumarcdee4f72015-06-22 16:42:26 +053030#include "control.h"
Johan Hovoldb15d97d2016-04-23 18:47:24 +020031#include "module.h"
Greg Kroah-Hartmana93938a2014-12-19 14:56:30 -080032#include "interface.h"
Greg Kroah-Hartman3bdec692014-12-12 17:10:16 -050033#include "bundle.h"
Alex Elderc68adb22014-10-01 21:54:14 -050034#include "connection.h"
Alex Elder4ccb6b72014-10-28 19:36:00 -050035#include "protocol.h"
Alex Eldere88afa52014-10-01 21:54:15 -050036#include "operation.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080037
38
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070039/* Matches up with the Greybus Protocol specification document */
Matt Porter52adb562014-09-18 15:25:43 -040040#define GREYBUS_VERSION_MAJOR 0x00
41#define GREYBUS_VERSION_MINOR 0x01
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070042
Johan Hovold358e9402015-11-21 10:51:59 +010043#define GREYBUS_ID_MATCH_DEVICE \
44 (GREYBUS_ID_MATCH_VENDOR | GREYBUS_ID_MATCH_PRODUCT)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080045
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070046#define GREYBUS_DEVICE(v, p) \
Johan Hovold358e9402015-11-21 10:51:59 +010047 .match_flags = GREYBUS_ID_MATCH_DEVICE, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070048 .vendor = (v), \
49 .product = (p),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080050
Johan Hovold3823c612015-11-21 10:52:04 +010051#define GREYBUS_DEVICE_CLASS(c) \
52 .match_flags = GREYBUS_ID_MATCH_CLASS, \
53 .class = (c),
54
Johan Hovold1dc53922015-09-02 18:03:21 +020055/* Maximum number of CPorts */
56#define CPORT_ID_MAX 4095 /* UniPro max id is 4095 */
57#define CPORT_ID_BAD U16_MAX
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080058
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080059struct greybus_driver {
60 const char *name;
61
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +053062 int (*probe)(struct gb_bundle *bundle,
63 const struct greybus_bundle_id *id);
64 void (*disconnect)(struct gb_bundle *bundle);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080065
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +053066 int (*suspend)(struct gb_bundle *bundle, pm_message_t message);
67 int (*resume)(struct gb_bundle *bundle);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080068
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +053069 const struct greybus_bundle_id *id_table;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080070
71 struct device_driver driver;
72};
73#define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
74
Johan Hovold13da9e12016-01-08 20:13:43 +010075static inline void greybus_set_drvdata(struct gb_bundle *bundle, void *data)
76{
77 dev_set_drvdata(&bundle->dev, data);
78}
79
80static inline void *greybus_get_drvdata(struct gb_bundle *bundle)
81{
82 return dev_get_drvdata(&bundle->dev);
83}
84
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080085/* Don't call these directly, use the module_greybus_driver() macro instead */
86int greybus_register_driver(struct greybus_driver *driver,
87 struct module *module, const char *mod_name);
Alex Elderfd1c2e52015-06-08 12:05:13 -050088void greybus_deregister_driver(struct greybus_driver *driver);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080089
90/* define to get proper THIS_MODULE and KBUILD_MODNAME values */
91#define greybus_register(driver) \
92 greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
Alex Elderfd1c2e52015-06-08 12:05:13 -050093#define greybus_deregister(driver) \
94 greybus_deregister_driver(driver)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080095
96/**
97 * module_greybus_driver() - Helper macro for registering a Greybus driver
98 * @__greybus_driver: greybus_driver structure
99 *
100 * Helper macro for Greybus drivers to set up proper module init / exit
101 * functions. Replaces module_init() and module_exit() and keeps people from
102 * printing pointless things to the kernel log when their driver is loaded.
103 */
104#define module_greybus_driver(__greybus_driver) \
105 module_driver(__greybus_driver, greybus_register, greybus_deregister)
106
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800107int greybus_disabled(void);
108
Greg Kroah-Hartman3d0421e2015-06-11 09:22:51 -0700109void gb_debugfs_init(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700110void gb_debugfs_cleanup(void);
Alexandre Bailone8f824b2015-03-18 15:42:51 +0100111struct dentry *gb_debugfs_get(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700112
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +0800113extern struct bus_type greybus_bus_type;
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700114
Johan Hovold2adaefb2015-11-25 15:59:02 +0100115extern struct device_type greybus_hd_type;
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200116extern struct device_type greybus_module_type;
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800117extern struct device_type greybus_interface_type;
Johan Hovolda6e5b012016-04-13 19:19:02 +0200118extern struct device_type greybus_control_type;
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500119extern struct device_type greybus_bundle_type;
Johan Hovold88f7b962015-11-25 15:59:08 +0100120extern struct device_type greybus_svc_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800121
Johan Hovold2adaefb2015-11-25 15:59:02 +0100122static inline int is_gb_host_device(const struct device *dev)
123{
124 return dev->type == &greybus_hd_type;
125}
126
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200127static inline int is_gb_module(const struct device *dev)
128{
129 return dev->type == &greybus_module_type;
130}
131
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800132static inline int is_gb_interface(const struct device *dev)
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800133{
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800134 return dev->type == &greybus_interface_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800135}
136
Johan Hovolda6e5b012016-04-13 19:19:02 +0200137static inline int is_gb_control(const struct device *dev)
138{
139 return dev->type == &greybus_control_type;
140}
141
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500142static inline int is_gb_bundle(const struct device *dev)
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800143{
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500144 return dev->type == &greybus_bundle_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800145}
146
Johan Hovold88f7b962015-11-25 15:59:08 +0100147static inline int is_gb_svc(const struct device *dev)
148{
149 return dev->type == &greybus_svc_type;
150}
151
Johan Hovold25376362015-11-03 18:03:23 +0100152static inline bool cport_id_valid(struct gb_host_device *hd, u16 cport_id)
Alex Elder821c6202015-06-13 11:02:07 -0500153{
Fabien Parent144670c2015-09-02 15:50:35 +0200154 return cport_id != CPORT_ID_BAD && cport_id < hd->num_cports;
Alex Elder821c6202015-06-13 11:02:07 -0500155}
156
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800157#endif /* __KERNEL__ */
158#endif /* __LINUX_GREYBUS_H */