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