Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Intel Management Engine Interface (Intel MEI) Linux driver |
| 3 | * Copyright (c) 2012-2013, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/kernel.h> |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 19 | #include <linux/sched.h> |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 20 | #include <linux/init.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/interrupt.h> |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 25 | #include <linux/mei_cl_bus.h> |
| 26 | |
| 27 | #include "mei_dev.h" |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 28 | #include "client.h" |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 29 | |
| 30 | #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver) |
| 31 | #define to_mei_cl_device(d) container_of(d, struct mei_cl_device, dev) |
| 32 | |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame^] | 33 | static inline uuid_le uuid_le_cast(const __u8 uuid[16]) |
| 34 | { |
| 35 | return *(uuid_le *)uuid; |
| 36 | } |
| 37 | |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 38 | static int mei_cl_device_match(struct device *dev, struct device_driver *drv) |
| 39 | { |
| 40 | struct mei_cl_device *device = to_mei_cl_device(dev); |
| 41 | struct mei_cl_driver *driver = to_mei_cl_driver(drv); |
| 42 | const struct mei_cl_device_id *id; |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame^] | 43 | const uuid_le *uuid; |
| 44 | const char *name; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 45 | |
| 46 | if (!device) |
| 47 | return 0; |
| 48 | |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame^] | 49 | uuid = mei_me_cl_uuid(device->me_cl); |
| 50 | name = device->name; |
| 51 | |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 52 | if (!driver || !driver->id_table) |
| 53 | return 0; |
| 54 | |
| 55 | id = driver->id_table; |
| 56 | |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame^] | 57 | while (uuid_le_cmp(NULL_UUID_LE, uuid_le_cast(id->uuid))) { |
| 58 | |
| 59 | if (!uuid_le_cmp(*uuid, uuid_le_cast(id->uuid))) { |
| 60 | if (id->name[0]) { |
| 61 | if (!strncmp(name, id->name, sizeof(id->name))) |
| 62 | return 1; |
| 63 | } else { |
| 64 | return 1; |
| 65 | } |
| 66 | } |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 67 | |
| 68 | id++; |
| 69 | } |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int mei_cl_device_probe(struct device *dev) |
| 75 | { |
| 76 | struct mei_cl_device *device = to_mei_cl_device(dev); |
| 77 | struct mei_cl_driver *driver; |
| 78 | struct mei_cl_device_id id; |
| 79 | |
| 80 | if (!device) |
| 81 | return 0; |
| 82 | |
| 83 | driver = to_mei_cl_driver(dev->driver); |
| 84 | if (!driver || !driver->probe) |
| 85 | return -ENODEV; |
| 86 | |
| 87 | dev_dbg(dev, "Device probe\n"); |
| 88 | |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame^] | 89 | strlcpy(id.name, device->name, sizeof(id.name)); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 90 | |
| 91 | return driver->probe(device, &id); |
| 92 | } |
| 93 | |
| 94 | static int mei_cl_device_remove(struct device *dev) |
| 95 | { |
| 96 | struct mei_cl_device *device = to_mei_cl_device(dev); |
| 97 | struct mei_cl_driver *driver; |
| 98 | |
| 99 | if (!device || !dev->driver) |
| 100 | return 0; |
| 101 | |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 102 | if (device->event_cb) { |
| 103 | device->event_cb = NULL; |
| 104 | cancel_work_sync(&device->event_work); |
| 105 | } |
| 106 | |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 107 | driver = to_mei_cl_driver(dev->driver); |
| 108 | if (!driver->remove) { |
| 109 | dev->driver = NULL; |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | return driver->remove(device); |
| 115 | } |
| 116 | |
| 117 | static ssize_t modalias_show(struct device *dev, struct device_attribute *a, |
| 118 | char *buf) |
| 119 | { |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame^] | 120 | struct mei_cl_device *device = to_mei_cl_device(dev); |
| 121 | const uuid_le *uuid = mei_me_cl_uuid(device->me_cl); |
| 122 | size_t len; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 123 | |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame^] | 124 | len = snprintf(buf, PAGE_SIZE, "mei:%s:" MEI_CL_UUID_FMT ":", |
| 125 | device->name, MEI_CL_UUID_ARGS(uuid->b)); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 126 | |
| 127 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; |
| 128 | } |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 129 | static DEVICE_ATTR_RO(modalias); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 130 | |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 131 | static struct attribute *mei_cl_dev_attrs[] = { |
| 132 | &dev_attr_modalias.attr, |
| 133 | NULL, |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 134 | }; |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 135 | ATTRIBUTE_GROUPS(mei_cl_dev); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 136 | |
| 137 | static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 138 | { |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame^] | 139 | struct mei_cl_device *device = to_mei_cl_device(dev); |
| 140 | const uuid_le *uuid = mei_me_cl_uuid(device->me_cl); |
| 141 | |
| 142 | if (add_uevent_var(env, "MODALIAS=mei:%s:" MEI_CL_UUID_FMT ":", |
| 143 | device->name, MEI_CL_UUID_ARGS(uuid->b))) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 144 | return -ENOMEM; |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static struct bus_type mei_cl_bus_type = { |
| 150 | .name = "mei", |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 151 | .dev_groups = mei_cl_dev_groups, |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 152 | .match = mei_cl_device_match, |
| 153 | .probe = mei_cl_device_probe, |
| 154 | .remove = mei_cl_device_remove, |
| 155 | .uevent = mei_cl_uevent, |
| 156 | }; |
| 157 | |
| 158 | static void mei_cl_dev_release(struct device *dev) |
| 159 | { |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 160 | struct mei_cl_device *device = to_mei_cl_device(dev); |
| 161 | |
| 162 | if (!device) |
| 163 | return; |
| 164 | |
| 165 | mei_me_cl_put(device->me_cl); |
| 166 | kfree(device); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static struct device_type mei_cl_device_type = { |
| 170 | .release = mei_cl_dev_release, |
| 171 | }; |
| 172 | |
Tomas Winkler | a176c24 | 2014-11-05 18:18:52 +0200 | [diff] [blame] | 173 | struct mei_cl *mei_cl_bus_find_cl_by_uuid(struct mei_device *dev, |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 174 | uuid_le uuid) |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 175 | { |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 176 | struct mei_cl *cl; |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 177 | |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 178 | list_for_each_entry(cl, &dev->device_list, device_link) { |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 179 | if (cl->device && cl->device->me_cl && |
| 180 | !uuid_le_cmp(uuid, *mei_me_cl_uuid(cl->device->me_cl))) |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 181 | return cl; |
| 182 | } |
| 183 | |
| 184 | return NULL; |
| 185 | } |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 186 | |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 187 | struct mei_cl_device *mei_cl_add_device(struct mei_device *dev, |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 188 | struct mei_me_client *me_cl, |
| 189 | struct mei_cl *cl, |
| 190 | char *name, |
Samuel Ortiz | e46980a | 2013-04-09 01:51:38 +0300 | [diff] [blame] | 191 | struct mei_cl_ops *ops) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 192 | { |
| 193 | struct mei_cl_device *device; |
| 194 | int status; |
| 195 | |
| 196 | device = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL); |
| 197 | if (!device) |
| 198 | return NULL; |
| 199 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 200 | device->me_cl = mei_me_cl_get(me_cl); |
| 201 | if (!device->me_cl) { |
| 202 | kfree(device); |
| 203 | return NULL; |
| 204 | } |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 205 | device->cl = cl; |
Samuel Ortiz | e46980a | 2013-04-09 01:51:38 +0300 | [diff] [blame] | 206 | device->ops = ops; |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 207 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 208 | device->dev.parent = dev->dev; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 209 | device->dev.bus = &mei_cl_bus_type; |
| 210 | device->dev.type = &mei_cl_device_type; |
| 211 | |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame^] | 212 | strlcpy(device->name, name, sizeof(device->name)); |
| 213 | |
| 214 | dev_set_name(&device->dev, "mei:%s:%pUl", name, mei_me_cl_uuid(me_cl)); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 215 | |
| 216 | status = device_register(&device->dev); |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 217 | if (status) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 218 | dev_err(dev->dev, "Failed to register MEI device\n"); |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 219 | mei_me_cl_put(device->me_cl); |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 220 | kfree(device); |
| 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | cl->device = device; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 225 | |
| 226 | dev_dbg(&device->dev, "client %s registered\n", name); |
| 227 | |
| 228 | return device; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 229 | } |
| 230 | EXPORT_SYMBOL_GPL(mei_cl_add_device); |
| 231 | |
| 232 | void mei_cl_remove_device(struct mei_cl_device *device) |
| 233 | { |
| 234 | device_unregister(&device->dev); |
| 235 | } |
| 236 | EXPORT_SYMBOL_GPL(mei_cl_remove_device); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 237 | |
| 238 | int __mei_cl_driver_register(struct mei_cl_driver *driver, struct module *owner) |
| 239 | { |
| 240 | int err; |
| 241 | |
| 242 | driver->driver.name = driver->name; |
| 243 | driver->driver.owner = owner; |
| 244 | driver->driver.bus = &mei_cl_bus_type; |
| 245 | |
| 246 | err = driver_register(&driver->driver); |
| 247 | if (err) |
| 248 | return err; |
| 249 | |
| 250 | pr_debug("mei: driver [%s] registered\n", driver->driver.name); |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | EXPORT_SYMBOL_GPL(__mei_cl_driver_register); |
| 255 | |
| 256 | void mei_cl_driver_unregister(struct mei_cl_driver *driver) |
| 257 | { |
| 258 | driver_unregister(&driver->driver); |
| 259 | |
| 260 | pr_debug("mei: driver [%s] unregistered\n", driver->driver.name); |
| 261 | } |
| 262 | EXPORT_SYMBOL_GPL(mei_cl_driver_unregister); |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 263 | |
Tomas Winkler | 39db74c | 2014-11-27 14:07:28 +0200 | [diff] [blame] | 264 | static ssize_t ___mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length, |
Samuel Ortiz | 44d88d9 | 2013-03-27 17:29:58 +0200 | [diff] [blame] | 265 | bool blocking) |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 266 | { |
| 267 | struct mei_device *dev; |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 268 | struct mei_cl_cb *cb = NULL; |
Tomas Winkler | 39db74c | 2014-11-27 14:07:28 +0200 | [diff] [blame] | 269 | ssize_t rets; |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 270 | |
| 271 | if (WARN_ON(!cl || !cl->dev)) |
| 272 | return -ENODEV; |
| 273 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 274 | dev = cl->dev; |
| 275 | |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 276 | mutex_lock(&dev->device_lock); |
Tomas Winkler | f3de9b6 | 2015-03-27 00:27:58 +0200 | [diff] [blame] | 277 | if (!mei_cl_is_connected(cl)) { |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 278 | rets = -ENODEV; |
| 279 | goto out; |
| 280 | } |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 281 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 282 | /* Check if we have an ME client device */ |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 283 | if (!mei_me_cl_is_active(cl->me_cl)) { |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 284 | rets = -ENOTTY; |
| 285 | goto out; |
| 286 | } |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 287 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 288 | if (length > mei_cl_mtu(cl)) { |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 289 | rets = -EFBIG; |
| 290 | goto out; |
| 291 | } |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 292 | |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 293 | cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL); |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 294 | if (!cb) { |
| 295 | rets = -ENOMEM; |
| 296 | goto out; |
| 297 | } |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 298 | |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 299 | memcpy(cb->buf.data, buf, length); |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 300 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 301 | rets = mei_cl_write(cl, cb, blocking); |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 302 | |
Tomas Winkler | 79563db | 2015-01-11 00:07:16 +0200 | [diff] [blame] | 303 | out: |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 304 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 305 | if (rets < 0) |
| 306 | mei_io_cb_free(cb); |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 307 | |
Tomas Winkler | 4234a6d | 2013-04-08 21:56:37 +0300 | [diff] [blame] | 308 | return rets; |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Tomas Winkler | 39db74c | 2014-11-27 14:07:28 +0200 | [diff] [blame] | 311 | ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length) |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 312 | { |
| 313 | struct mei_device *dev; |
| 314 | struct mei_cl_cb *cb; |
| 315 | size_t r_length; |
Tomas Winkler | 39db74c | 2014-11-27 14:07:28 +0200 | [diff] [blame] | 316 | ssize_t rets; |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 317 | |
| 318 | if (WARN_ON(!cl || !cl->dev)) |
| 319 | return -ENODEV; |
| 320 | |
| 321 | dev = cl->dev; |
| 322 | |
| 323 | mutex_lock(&dev->device_lock); |
| 324 | |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 325 | cb = mei_cl_read_cb(cl, NULL); |
| 326 | if (cb) |
| 327 | goto copy; |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 328 | |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 329 | rets = mei_cl_read_start(cl, length, NULL); |
| 330 | if (rets && rets != -EBUSY) |
| 331 | goto out; |
| 332 | |
| 333 | if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) { |
Tomas Winkler | e2b3164 | 2013-09-02 13:29:46 +0300 | [diff] [blame] | 334 | |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 335 | mutex_unlock(&dev->device_lock); |
| 336 | |
| 337 | if (wait_event_interruptible(cl->rx_wait, |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 338 | (!list_empty(&cl->rd_completed)) || |
Tomas Winkler | 6a84d63 | 2015-03-27 00:27:59 +0200 | [diff] [blame] | 339 | (!mei_cl_is_connected(cl)))) { |
Tomas Winkler | e2b3164 | 2013-09-02 13:29:46 +0300 | [diff] [blame] | 340 | |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 341 | if (signal_pending(current)) |
| 342 | return -EINTR; |
| 343 | return -ERESTARTSYS; |
| 344 | } |
| 345 | |
| 346 | mutex_lock(&dev->device_lock); |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 347 | |
Tomas Winkler | 6a84d63 | 2015-03-27 00:27:59 +0200 | [diff] [blame] | 348 | if (!mei_cl_is_connected(cl)) { |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 349 | rets = -EBUSY; |
| 350 | goto out; |
| 351 | } |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 354 | cb = mei_cl_read_cb(cl, NULL); |
| 355 | if (!cb) { |
Tomas Winkler | 39db74c | 2014-11-27 14:07:28 +0200 | [diff] [blame] | 356 | rets = 0; |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 357 | goto out; |
| 358 | } |
| 359 | |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 360 | copy: |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 361 | if (cb->status) { |
| 362 | rets = cb->status; |
| 363 | goto free; |
| 364 | } |
| 365 | |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 366 | r_length = min_t(size_t, length, cb->buf_idx); |
Tomas Winkler | 5db7514 | 2015-02-10 10:39:42 +0200 | [diff] [blame] | 367 | memcpy(buf, cb->buf.data, r_length); |
Tomas Winkler | 39db74c | 2014-11-27 14:07:28 +0200 | [diff] [blame] | 368 | rets = r_length; |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 369 | |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 370 | free: |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 371 | mei_io_cb_free(cb); |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 372 | out: |
| 373 | mutex_unlock(&dev->device_lock); |
| 374 | |
Tomas Winkler | 39db74c | 2014-11-27 14:07:28 +0200 | [diff] [blame] | 375 | return rets; |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Tomas Winkler | 39db74c | 2014-11-27 14:07:28 +0200 | [diff] [blame] | 378 | inline ssize_t __mei_cl_async_send(struct mei_cl *cl, u8 *buf, size_t length) |
Samuel Ortiz | 44d88d9 | 2013-03-27 17:29:58 +0200 | [diff] [blame] | 379 | { |
| 380 | return ___mei_cl_send(cl, buf, length, 0); |
| 381 | } |
| 382 | |
Tomas Winkler | 39db74c | 2014-11-27 14:07:28 +0200 | [diff] [blame] | 383 | inline ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length) |
Samuel Ortiz | 44d88d9 | 2013-03-27 17:29:58 +0200 | [diff] [blame] | 384 | { |
| 385 | return ___mei_cl_send(cl, buf, length, 1); |
| 386 | } |
| 387 | |
Tomas Winkler | 39db74c | 2014-11-27 14:07:28 +0200 | [diff] [blame] | 388 | ssize_t mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length) |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 389 | { |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 390 | struct mei_cl *cl = device->cl; |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 391 | |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 392 | if (cl == NULL) |
| 393 | return -ENODEV; |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 394 | |
| 395 | if (device->ops && device->ops->send) |
| 396 | return device->ops->send(device, buf, length); |
| 397 | |
| 398 | return __mei_cl_send(cl, buf, length); |
| 399 | } |
| 400 | EXPORT_SYMBOL_GPL(mei_cl_send); |
| 401 | |
Tomas Winkler | 39db74c | 2014-11-27 14:07:28 +0200 | [diff] [blame] | 402 | ssize_t mei_cl_recv(struct mei_cl_device *device, u8 *buf, size_t length) |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 403 | { |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 404 | struct mei_cl *cl = device->cl; |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 405 | |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 406 | if (cl == NULL) |
| 407 | return -ENODEV; |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 408 | |
| 409 | if (device->ops && device->ops->recv) |
| 410 | return device->ops->recv(device, buf, length); |
| 411 | |
| 412 | return __mei_cl_recv(cl, buf, length); |
| 413 | } |
| 414 | EXPORT_SYMBOL_GPL(mei_cl_recv); |
| 415 | |
| 416 | static void mei_bus_event_work(struct work_struct *work) |
| 417 | { |
| 418 | struct mei_cl_device *device; |
| 419 | |
| 420 | device = container_of(work, struct mei_cl_device, event_work); |
| 421 | |
| 422 | if (device->event_cb) |
| 423 | device->event_cb(device, device->events, device->event_context); |
| 424 | |
| 425 | device->events = 0; |
| 426 | |
| 427 | /* Prepare for the next read */ |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 428 | mei_cl_read_start(device->cl, 0, NULL); |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | int mei_cl_register_event_cb(struct mei_cl_device *device, |
| 432 | mei_cl_event_cb_t event_cb, void *context) |
| 433 | { |
| 434 | if (device->event_cb) |
| 435 | return -EALREADY; |
| 436 | |
| 437 | device->events = 0; |
| 438 | device->event_cb = event_cb; |
| 439 | device->event_context = context; |
| 440 | INIT_WORK(&device->event_work, mei_bus_event_work); |
| 441 | |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 442 | mei_cl_read_start(device->cl, 0, NULL); |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | EXPORT_SYMBOL_GPL(mei_cl_register_event_cb); |
Samuel Ortiz | cf3baef | 2013-03-27 17:29:57 +0200 | [diff] [blame] | 447 | |
Samuel Ortiz | aa6aef2 | 2013-03-27 17:29:59 +0200 | [diff] [blame] | 448 | void *mei_cl_get_drvdata(const struct mei_cl_device *device) |
| 449 | { |
| 450 | return dev_get_drvdata(&device->dev); |
| 451 | } |
| 452 | EXPORT_SYMBOL_GPL(mei_cl_get_drvdata); |
| 453 | |
| 454 | void mei_cl_set_drvdata(struct mei_cl_device *device, void *data) |
| 455 | { |
| 456 | dev_set_drvdata(&device->dev, data); |
| 457 | } |
| 458 | EXPORT_SYMBOL_GPL(mei_cl_set_drvdata); |
| 459 | |
Samuel Ortiz | e46980a | 2013-04-09 01:51:38 +0300 | [diff] [blame] | 460 | int mei_cl_enable_device(struct mei_cl_device *device) |
| 461 | { |
| 462 | int err; |
| 463 | struct mei_device *dev; |
| 464 | struct mei_cl *cl = device->cl; |
| 465 | |
| 466 | if (cl == NULL) |
| 467 | return -ENODEV; |
| 468 | |
| 469 | dev = cl->dev; |
| 470 | |
| 471 | mutex_lock(&dev->device_lock); |
| 472 | |
Tomas Winkler | 0c53357 | 2015-05-04 09:43:53 +0300 | [diff] [blame] | 473 | if (mei_cl_is_connected(cl)) { |
| 474 | mutex_unlock(&dev->device_lock); |
| 475 | dev_warn(dev->dev, "Already connected"); |
| 476 | return -EBUSY; |
| 477 | } |
| 478 | |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 479 | err = mei_cl_connect(cl, device->me_cl, NULL); |
Samuel Ortiz | e46980a | 2013-04-09 01:51:38 +0300 | [diff] [blame] | 480 | if (err < 0) { |
| 481 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 482 | dev_err(dev->dev, "Could not connect to the ME client"); |
Samuel Ortiz | e46980a | 2013-04-09 01:51:38 +0300 | [diff] [blame] | 483 | |
| 484 | return err; |
| 485 | } |
| 486 | |
| 487 | mutex_unlock(&dev->device_lock); |
| 488 | |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 489 | if (device->event_cb) |
Tomas Winkler | bca67d6 | 2015-02-10 10:39:43 +0200 | [diff] [blame] | 490 | mei_cl_read_start(device->cl, 0, NULL); |
Samuel Ortiz | e46980a | 2013-04-09 01:51:38 +0300 | [diff] [blame] | 491 | |
| 492 | if (!device->ops || !device->ops->enable) |
| 493 | return 0; |
| 494 | |
| 495 | return device->ops->enable(device); |
| 496 | } |
| 497 | EXPORT_SYMBOL_GPL(mei_cl_enable_device); |
| 498 | |
| 499 | int mei_cl_disable_device(struct mei_cl_device *device) |
| 500 | { |
| 501 | int err; |
| 502 | struct mei_device *dev; |
| 503 | struct mei_cl *cl = device->cl; |
| 504 | |
| 505 | if (cl == NULL) |
| 506 | return -ENODEV; |
| 507 | |
| 508 | dev = cl->dev; |
| 509 | |
Tomas Winkler | b3de8e3 | 2015-02-10 10:39:47 +0200 | [diff] [blame] | 510 | if (device->ops && device->ops->disable) |
| 511 | device->ops->disable(device); |
| 512 | |
| 513 | device->event_cb = NULL; |
| 514 | |
Samuel Ortiz | e46980a | 2013-04-09 01:51:38 +0300 | [diff] [blame] | 515 | mutex_lock(&dev->device_lock); |
| 516 | |
Tomas Winkler | f3de9b6 | 2015-03-27 00:27:58 +0200 | [diff] [blame] | 517 | if (!mei_cl_is_connected(cl)) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 518 | dev_err(dev->dev, "Already disconnected"); |
Tomas Winkler | b3de8e3 | 2015-02-10 10:39:47 +0200 | [diff] [blame] | 519 | err = 0; |
| 520 | goto out; |
Samuel Ortiz | e46980a | 2013-04-09 01:51:38 +0300 | [diff] [blame] | 521 | } |
| 522 | |
Samuel Ortiz | e46980a | 2013-04-09 01:51:38 +0300 | [diff] [blame] | 523 | err = mei_cl_disconnect(cl); |
| 524 | if (err < 0) { |
Tomas Winkler | b3de8e3 | 2015-02-10 10:39:47 +0200 | [diff] [blame] | 525 | dev_err(dev->dev, "Could not disconnect from the ME client"); |
| 526 | goto out; |
Samuel Ortiz | e46980a | 2013-04-09 01:51:38 +0300 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* Flush queues and remove any pending read */ |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 530 | mei_cl_flush_queues(cl, NULL); |
Samuel Ortiz | e46980a | 2013-04-09 01:51:38 +0300 | [diff] [blame] | 531 | |
Tomas Winkler | b3de8e3 | 2015-02-10 10:39:47 +0200 | [diff] [blame] | 532 | out: |
Samuel Ortiz | e46980a | 2013-04-09 01:51:38 +0300 | [diff] [blame] | 533 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | b3de8e3 | 2015-02-10 10:39:47 +0200 | [diff] [blame] | 534 | return err; |
Samuel Ortiz | e46980a | 2013-04-09 01:51:38 +0300 | [diff] [blame] | 535 | |
Samuel Ortiz | e46980a | 2013-04-09 01:51:38 +0300 | [diff] [blame] | 536 | } |
| 537 | EXPORT_SYMBOL_GPL(mei_cl_disable_device); |
| 538 | |
Samuel Ortiz | cf3baef | 2013-03-27 17:29:57 +0200 | [diff] [blame] | 539 | void mei_cl_bus_rx_event(struct mei_cl *cl) |
| 540 | { |
| 541 | struct mei_cl_device *device = cl->device; |
| 542 | |
| 543 | if (!device || !device->event_cb) |
| 544 | return; |
| 545 | |
| 546 | set_bit(MEI_CL_EVENT_RX, &device->events); |
| 547 | |
| 548 | schedule_work(&device->event_work); |
| 549 | } |
| 550 | |
Tomas Winkler | 4870569 | 2014-02-17 15:13:19 +0200 | [diff] [blame] | 551 | void mei_cl_bus_remove_devices(struct mei_device *dev) |
| 552 | { |
| 553 | struct mei_cl *cl, *next; |
| 554 | |
| 555 | mutex_lock(&dev->device_lock); |
| 556 | list_for_each_entry_safe(cl, next, &dev->device_list, device_link) { |
| 557 | if (cl->device) |
| 558 | mei_cl_remove_device(cl->device); |
| 559 | |
| 560 | list_del(&cl->device_link); |
| 561 | mei_cl_unlink(cl); |
| 562 | kfree(cl); |
| 563 | } |
| 564 | mutex_unlock(&dev->device_lock); |
| 565 | } |
| 566 | |
Samuel Ortiz | cf3baef | 2013-03-27 17:29:57 +0200 | [diff] [blame] | 567 | int __init mei_cl_bus_init(void) |
| 568 | { |
| 569 | return bus_register(&mei_cl_bus_type); |
| 570 | } |
| 571 | |
| 572 | void __exit mei_cl_bus_exit(void) |
| 573 | { |
| 574 | bus_unregister(&mei_cl_bus_type); |
| 575 | } |