Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 14 | #include <linux/list.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/module.h> |
| 17 | #include "greybus_id.h" |
| 18 | |
| 19 | |
| 20 | #define GREYBUS_DEVICE_ID_MATCH_DEVICE \ |
| 21 | (GREYBUS_DEVICE_ID_MATCH_VENDOR | GREYBUS_DEVICE_ID_MATCH_PRODUCT) |
| 22 | |
| 23 | #define GREYBUS_DEVICE(vendor, product) \ |
| 24 | .match_flags = GREYBUS_DEVICE_ID_MATCH_DEVICE, \ |
| 25 | .wVendor = (vendor), \ |
| 26 | .wProduct = (product), |
| 27 | |
| 28 | #define GREYBUS_DEVICE_SERIAL(serial) \ |
| 29 | .match_flags = GREYBUS_DEVICE_ID_MATCH_SERIAL, \ |
| 30 | .lSerial = (serial), |
| 31 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 32 | struct greybus_descriptor { |
| 33 | __u16 wVendor; |
| 34 | __u16 wProduct; |
| 35 | __u64 lSerialNumber; |
| 36 | }; |
| 37 | |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 38 | |
| 39 | struct gbuf; |
| 40 | |
| 41 | struct cport { |
| 42 | u16 number; |
| 43 | // FIXME, what else? |
| 44 | }; |
| 45 | |
| 46 | typedef void (*gbuf_complete_t)(struct gbuf *gbuf); |
| 47 | |
| 48 | struct gbuf { |
| 49 | struct kref kref; |
| 50 | void *hcpriv; |
| 51 | |
| 52 | struct list_head anchor_list; |
| 53 | struct gbuf_anchor *anchor; // FIXME do we need? |
| 54 | |
| 55 | struct greybus_device *gdev; |
| 56 | struct cport *cport; |
| 57 | int status; |
| 58 | void *transfer_buffer; |
| 59 | u32 transfer_flags; /* flags for the transfer buffer */ |
| 60 | u32 transfer_buffer_length; |
| 61 | u32 actual_length; |
| 62 | |
| 63 | struct scatterlist *sg; // FIXME do we need? |
| 64 | int num_sgs; |
| 65 | |
| 66 | void *context; |
| 67 | gbuf_complete_t complete; |
| 68 | }; |
| 69 | |
| 70 | /* |
| 71 | * gbuf->transfer_flags |
| 72 | */ |
| 73 | #define GBUF_FREE_BUFFER BIT(0) /* Free the transfer buffer with the gbuf */ |
| 74 | |
Greg Kroah-Hartman | 199d68d | 2014-08-30 16:20:22 -0700 | [diff] [blame] | 75 | /* For SP1 hardware, we are going to "hardcode" each device to have all logical |
| 76 | * blocks in order to be able to address them as one unified "unit". Then |
| 77 | * higher up layers will then be able to talk to them as one logical block and |
| 78 | * properly know how they are hooked together (i.e. which i2c port is on the |
| 79 | * same module as the gpio pins, etc.) |
| 80 | * |
| 81 | * So, put the "private" data structures here in greybus.h and link to them off |
| 82 | * of the "main" greybus_device structure. |
| 83 | */ |
| 84 | |
| 85 | struct gb_i2c_device; |
| 86 | struct gb_gpio_device; |
| 87 | struct gb_sdio_host; |
| 88 | struct gb_tty; |
| 89 | struct gb_usb_device; |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 90 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 91 | struct greybus_device { |
| 92 | struct device dev; |
| 93 | struct greybus_descriptor descriptor; |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 94 | int num_cport; |
| 95 | struct cport cport[0]; |
Greg Kroah-Hartman | 199d68d | 2014-08-30 16:20:22 -0700 | [diff] [blame] | 96 | |
| 97 | struct gb_i2c_device *gb_i2c_dev; |
| 98 | struct gb_gpio_device *gb_gpio_dev; |
| 99 | struct gb_sdio_host *gb_sdio_host; |
| 100 | struct gb_tty *gb_tty; |
| 101 | struct gb_usb_device *gb_usb_dev; |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 102 | }; |
| 103 | #define to_greybus_device(d) container_of(d, struct greybus_device, dev) |
| 104 | |
Greg Kroah-Hartman | 199d68d | 2014-08-30 16:20:22 -0700 | [diff] [blame] | 105 | /* |
| 106 | * Because we are allocating a data structure per "type" in the greybus device, |
| 107 | * we have static functions for this, not "dynamic" drivers like we really |
| 108 | * should in the end. |
| 109 | */ |
| 110 | int gb_i2c_probe(struct greybus_device *gdev, const struct greybus_device_id *id); |
| 111 | void gb_i2c_disconnect(struct greybus_device *gdev); |
Greg Kroah-Hartman | db6e1fd | 2014-08-30 16:47:26 -0700 | [diff] [blame] | 112 | int gb_gpio_probe(struct greybus_device *gdev, const struct greybus_device_id *id); |
| 113 | void gb_gpio_disconnect(struct greybus_device *gdev); |
| 114 | int gb_sdio_probe(struct greybus_device *gdev, const struct greybus_device_id *id); |
| 115 | void gb_sdio_disconnect(struct greybus_device *gdev); |
| 116 | int gb_tty_probe(struct greybus_device *gdev, const struct greybus_device_id *id); |
| 117 | void gb_tty_disconnect(struct greybus_device *gdev); |
Greg Kroah-Hartman | 199d68d | 2014-08-30 16:20:22 -0700 | [diff] [blame] | 118 | |
Greg Kroah-Hartman | db6e1fd | 2014-08-30 16:47:26 -0700 | [diff] [blame] | 119 | int gb_tty_init(void); |
| 120 | void gb_tty_exit(void); |
Greg Kroah-Hartman | d5d1903 | 2014-08-11 19:03:20 +0800 | [diff] [blame] | 121 | |
| 122 | struct gbuf *greybus_alloc_gbuf(struct greybus_device *gdev, |
| 123 | struct cport *cport, |
| 124 | gfp_t mem_flags); |
| 125 | void greybus_free_gbuf(struct gbuf *gbuf); |
| 126 | |
| 127 | int greybus_submit_gbuf(struct gbuf *gbuf, gfp_t mem_flags); |
| 128 | int greybus_kill_gbuf(struct gbuf *gbuf); |
| 129 | |
| 130 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 131 | struct greybus_driver { |
| 132 | const char *name; |
| 133 | |
Greg Kroah-Hartman | 776f136 | 2014-08-11 17:27:07 +0800 | [diff] [blame] | 134 | int (*probe)(struct greybus_device *gdev, |
| 135 | const struct greybus_device_id *id); |
| 136 | void (*disconnect)(struct greybus_device *gdev); |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 137 | |
Greg Kroah-Hartman | 776f136 | 2014-08-11 17:27:07 +0800 | [diff] [blame] | 138 | int (*suspend)(struct greybus_device *gdev, pm_message_t message); |
| 139 | int (*resume)(struct greybus_device *gdev); |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 140 | |
| 141 | const struct greybus_device_id *id_table; |
| 142 | |
| 143 | struct device_driver driver; |
| 144 | }; |
| 145 | #define to_greybus_driver(d) container_of(d, struct greybus_driver, driver) |
| 146 | |
| 147 | static inline void greybus_set_drvdata(struct greybus_device *gdev, void *data) |
| 148 | { |
Greg Kroah-Hartman | 776f136 | 2014-08-11 17:27:07 +0800 | [diff] [blame] | 149 | dev_set_drvdata(&gdev->dev, data); |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static inline void *greybus_get_drvdata(struct greybus_device *gdev) |
| 153 | { |
Greg Kroah-Hartman | 776f136 | 2014-08-11 17:27:07 +0800 | [diff] [blame] | 154 | return dev_get_drvdata(&gdev->dev); |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* Don't call these directly, use the module_greybus_driver() macro instead */ |
| 158 | int greybus_register_driver(struct greybus_driver *driver, |
| 159 | struct module *module, const char *mod_name); |
| 160 | void greybus_deregister(struct greybus_driver *driver); |
| 161 | |
| 162 | /* define to get proper THIS_MODULE and KBUILD_MODNAME values */ |
| 163 | #define greybus_register(driver) \ |
| 164 | greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME) |
| 165 | |
| 166 | /** |
| 167 | * module_greybus_driver() - Helper macro for registering a Greybus driver |
| 168 | * @__greybus_driver: greybus_driver structure |
| 169 | * |
| 170 | * Helper macro for Greybus drivers to set up proper module init / exit |
| 171 | * functions. Replaces module_init() and module_exit() and keeps people from |
| 172 | * printing pointless things to the kernel log when their driver is loaded. |
| 173 | */ |
| 174 | #define module_greybus_driver(__greybus_driver) \ |
| 175 | module_driver(__greybus_driver, greybus_register, greybus_deregister) |
| 176 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 177 | int greybus_disabled(void); |
| 178 | |
Greg Kroah-Hartman | de536e3 | 2014-08-31 16:17:04 -0700 | [diff] [blame^] | 179 | |
| 180 | /* Internal functions to gb module, move to internal .h file eventually. */ |
| 181 | |
| 182 | int gb_new_ap_msg(u8 *data, int length); |
| 183 | int gb_thread_init(void); |
| 184 | void gb_thread_destroy(void); |
| 185 | int gb_debugfs_init(void); |
| 186 | void gb_debugfs_cleanup(void); |
| 187 | |
| 188 | |
Greg Kroah-Hartman | c8a797a | 2014-08-11 15:30:45 +0800 | [diff] [blame] | 189 | |
| 190 | #endif /* __KERNEL__ */ |
| 191 | #endif /* __LINUX_GREYBUS_H */ |