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 | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 33 | /** |
| 34 | * __mei_cl_send - internal client send (write) |
| 35 | * |
| 36 | * @cl: host client |
| 37 | * @buf: buffer to send |
| 38 | * @length: buffer length |
| 39 | * @blocking: wait for write completion |
| 40 | * |
| 41 | * Return: written size bytes or < 0 on error |
| 42 | */ |
| 43 | ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length, |
| 44 | bool blocking) |
| 45 | { |
| 46 | struct mei_device *bus; |
| 47 | struct mei_cl_cb *cb = NULL; |
| 48 | ssize_t rets; |
| 49 | |
| 50 | if (WARN_ON(!cl || !cl->dev)) |
| 51 | return -ENODEV; |
| 52 | |
| 53 | bus = cl->dev; |
| 54 | |
| 55 | mutex_lock(&bus->device_lock); |
| 56 | if (!mei_cl_is_connected(cl)) { |
| 57 | rets = -ENODEV; |
| 58 | goto out; |
| 59 | } |
| 60 | |
| 61 | /* Check if we have an ME client device */ |
| 62 | if (!mei_me_cl_is_active(cl->me_cl)) { |
| 63 | rets = -ENOTTY; |
| 64 | goto out; |
| 65 | } |
| 66 | |
| 67 | if (length > mei_cl_mtu(cl)) { |
| 68 | rets = -EFBIG; |
| 69 | goto out; |
| 70 | } |
| 71 | |
| 72 | cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL); |
| 73 | if (!cb) { |
| 74 | rets = -ENOMEM; |
| 75 | goto out; |
| 76 | } |
| 77 | |
| 78 | memcpy(cb->buf.data, buf, length); |
| 79 | |
| 80 | rets = mei_cl_write(cl, cb, blocking); |
| 81 | |
| 82 | out: |
| 83 | mutex_unlock(&bus->device_lock); |
| 84 | if (rets < 0) |
| 85 | mei_io_cb_free(cb); |
| 86 | |
| 87 | return rets; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * __mei_cl_recv - internal client receive (read) |
| 92 | * |
| 93 | * @cl: host client |
| 94 | * @buf: buffer to send |
| 95 | * @length: buffer length |
| 96 | * |
| 97 | * Return: read size in bytes of < 0 on error |
| 98 | */ |
| 99 | ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length) |
| 100 | { |
| 101 | struct mei_device *bus; |
| 102 | struct mei_cl_cb *cb; |
| 103 | size_t r_length; |
| 104 | ssize_t rets; |
| 105 | |
| 106 | if (WARN_ON(!cl || !cl->dev)) |
| 107 | return -ENODEV; |
| 108 | |
| 109 | bus = cl->dev; |
| 110 | |
| 111 | mutex_lock(&bus->device_lock); |
| 112 | |
| 113 | cb = mei_cl_read_cb(cl, NULL); |
| 114 | if (cb) |
| 115 | goto copy; |
| 116 | |
| 117 | rets = mei_cl_read_start(cl, length, NULL); |
| 118 | if (rets && rets != -EBUSY) |
| 119 | goto out; |
| 120 | |
| 121 | /* wait on event only if there is no other waiter */ |
| 122 | if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) { |
| 123 | |
| 124 | mutex_unlock(&bus->device_lock); |
| 125 | |
| 126 | if (wait_event_interruptible(cl->rx_wait, |
| 127 | (!list_empty(&cl->rd_completed)) || |
| 128 | (!mei_cl_is_connected(cl)))) { |
| 129 | |
| 130 | if (signal_pending(current)) |
| 131 | return -EINTR; |
| 132 | return -ERESTARTSYS; |
| 133 | } |
| 134 | |
| 135 | mutex_lock(&bus->device_lock); |
| 136 | |
| 137 | if (!mei_cl_is_connected(cl)) { |
| 138 | rets = -EBUSY; |
| 139 | goto out; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | cb = mei_cl_read_cb(cl, NULL); |
| 144 | if (!cb) { |
| 145 | rets = 0; |
| 146 | goto out; |
| 147 | } |
| 148 | |
| 149 | copy: |
| 150 | if (cb->status) { |
| 151 | rets = cb->status; |
| 152 | goto free; |
| 153 | } |
| 154 | |
| 155 | r_length = min_t(size_t, length, cb->buf_idx); |
| 156 | memcpy(buf, cb->buf.data, r_length); |
| 157 | rets = r_length; |
| 158 | |
| 159 | free: |
| 160 | mei_io_cb_free(cb); |
| 161 | out: |
| 162 | mutex_unlock(&bus->device_lock); |
| 163 | |
| 164 | return rets; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * mei_cl_send - me device send (write) |
| 169 | * |
| 170 | * @cldev: me client device |
| 171 | * @buf: buffer to send |
| 172 | * @length: buffer length |
| 173 | * |
| 174 | * Return: written size in bytes or < 0 on error |
| 175 | */ |
| 176 | ssize_t mei_cl_send(struct mei_cl_device *cldev, u8 *buf, size_t length) |
| 177 | { |
| 178 | struct mei_cl *cl = cldev->cl; |
| 179 | |
| 180 | if (cl == NULL) |
| 181 | return -ENODEV; |
| 182 | |
| 183 | return __mei_cl_send(cl, buf, length, 1); |
| 184 | } |
| 185 | EXPORT_SYMBOL_GPL(mei_cl_send); |
| 186 | |
| 187 | /** |
| 188 | * mei_cl_recv - client receive (read) |
| 189 | * |
| 190 | * @cldev: me client device |
| 191 | * @buf: buffer to send |
| 192 | * @length: buffer length |
| 193 | * |
| 194 | * Return: read size in bytes of < 0 on error |
| 195 | */ |
| 196 | ssize_t mei_cl_recv(struct mei_cl_device *cldev, u8 *buf, size_t length) |
| 197 | { |
| 198 | struct mei_cl *cl = cldev->cl; |
| 199 | |
| 200 | if (cl == NULL) |
| 201 | return -ENODEV; |
| 202 | |
| 203 | return __mei_cl_recv(cl, buf, length); |
| 204 | } |
| 205 | EXPORT_SYMBOL_GPL(mei_cl_recv); |
| 206 | |
| 207 | /** |
| 208 | * mei_bus_event_work - dispatch rx event for a bus device |
| 209 | * and schedule new work |
| 210 | * |
| 211 | * @work: work |
| 212 | */ |
| 213 | static void mei_bus_event_work(struct work_struct *work) |
| 214 | { |
| 215 | struct mei_cl_device *cldev; |
| 216 | |
| 217 | cldev = container_of(work, struct mei_cl_device, event_work); |
| 218 | |
| 219 | if (cldev->event_cb) |
| 220 | cldev->event_cb(cldev, cldev->events, cldev->event_context); |
| 221 | |
| 222 | cldev->events = 0; |
| 223 | |
| 224 | /* Prepare for the next read */ |
| 225 | mei_cl_read_start(cldev->cl, 0, NULL); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * mei_cl_bus_rx_event - schedule rx evenet |
| 230 | * |
| 231 | * @cl: host client |
| 232 | */ |
| 233 | void mei_cl_bus_rx_event(struct mei_cl *cl) |
| 234 | { |
| 235 | struct mei_cl_device *cldev = cl->cldev; |
| 236 | |
| 237 | if (!cldev || !cldev->event_cb) |
| 238 | return; |
| 239 | |
| 240 | set_bit(MEI_CL_EVENT_RX, &cldev->events); |
| 241 | |
| 242 | schedule_work(&cldev->event_work); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * mei_cl_register_event_cb - register event callback |
| 247 | * |
| 248 | * @cldev: me client devices |
| 249 | * @event_cb: callback function |
| 250 | * @context: driver context data |
| 251 | * |
| 252 | * Return: 0 on success |
| 253 | * -EALREADY if an callback is already registered |
| 254 | * <0 on other errors |
| 255 | */ |
| 256 | int mei_cl_register_event_cb(struct mei_cl_device *cldev, |
| 257 | mei_cl_event_cb_t event_cb, void *context) |
| 258 | { |
Tomas Winkler | 48168f4 | 2015-07-23 15:08:38 +0300 | [diff] [blame] | 259 | int ret; |
| 260 | |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 261 | if (cldev->event_cb) |
| 262 | return -EALREADY; |
| 263 | |
| 264 | cldev->events = 0; |
| 265 | cldev->event_cb = event_cb; |
| 266 | cldev->event_context = context; |
| 267 | INIT_WORK(&cldev->event_work, mei_bus_event_work); |
| 268 | |
Tomas Winkler | 48168f4 | 2015-07-23 15:08:38 +0300 | [diff] [blame] | 269 | ret = mei_cl_read_start(cldev->cl, 0, NULL); |
| 270 | if (ret && ret != -EBUSY) |
| 271 | return ret; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | EXPORT_SYMBOL_GPL(mei_cl_register_event_cb); |
| 276 | |
| 277 | /** |
| 278 | * mei_cl_get_drvdata - driver data getter |
| 279 | * |
| 280 | * @cldev: mei client device |
| 281 | * |
| 282 | * Return: driver private data |
| 283 | */ |
| 284 | void *mei_cl_get_drvdata(const struct mei_cl_device *cldev) |
| 285 | { |
| 286 | return dev_get_drvdata(&cldev->dev); |
| 287 | } |
| 288 | EXPORT_SYMBOL_GPL(mei_cl_get_drvdata); |
| 289 | |
| 290 | /** |
| 291 | * mei_cl_set_drvdata - driver data setter |
| 292 | * |
| 293 | * @cldev: mei client device |
| 294 | * @data: data to store |
| 295 | */ |
| 296 | void mei_cl_set_drvdata(struct mei_cl_device *cldev, void *data) |
| 297 | { |
| 298 | dev_set_drvdata(&cldev->dev, data); |
| 299 | } |
| 300 | EXPORT_SYMBOL_GPL(mei_cl_set_drvdata); |
| 301 | |
| 302 | /** |
| 303 | * mei_cl_enable_device - enable me client device |
| 304 | * create connection with me client |
| 305 | * |
| 306 | * @cldev: me client device |
| 307 | * |
| 308 | * Return: 0 on success and < 0 on error |
| 309 | */ |
| 310 | int mei_cl_enable_device(struct mei_cl_device *cldev) |
| 311 | { |
| 312 | int err; |
| 313 | struct mei_device *bus; |
| 314 | struct mei_cl *cl = cldev->cl; |
| 315 | |
| 316 | if (cl == NULL) |
| 317 | return -ENODEV; |
| 318 | |
| 319 | bus = cl->dev; |
| 320 | |
| 321 | mutex_lock(&bus->device_lock); |
| 322 | |
| 323 | if (mei_cl_is_connected(cl)) { |
| 324 | mutex_unlock(&bus->device_lock); |
| 325 | dev_warn(bus->dev, "Already connected"); |
| 326 | return -EBUSY; |
| 327 | } |
| 328 | |
| 329 | err = mei_cl_connect(cl, cldev->me_cl, NULL); |
| 330 | if (err < 0) { |
| 331 | mutex_unlock(&bus->device_lock); |
| 332 | dev_err(bus->dev, "Could not connect to the ME client"); |
| 333 | |
| 334 | return err; |
| 335 | } |
| 336 | |
| 337 | mutex_unlock(&bus->device_lock); |
| 338 | |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | EXPORT_SYMBOL_GPL(mei_cl_enable_device); |
| 342 | |
| 343 | /** |
| 344 | * mei_cl_disable_device - disable me client device |
| 345 | * disconnect form the me client |
| 346 | * |
| 347 | * @cldev: me client device |
| 348 | * |
| 349 | * Return: 0 on success and < 0 on error |
| 350 | */ |
| 351 | int mei_cl_disable_device(struct mei_cl_device *cldev) |
| 352 | { |
| 353 | int err; |
| 354 | struct mei_device *bus; |
| 355 | struct mei_cl *cl = cldev->cl; |
| 356 | |
| 357 | if (cl == NULL) |
| 358 | return -ENODEV; |
| 359 | |
| 360 | bus = cl->dev; |
| 361 | |
| 362 | cldev->event_cb = NULL; |
| 363 | |
| 364 | mutex_lock(&bus->device_lock); |
| 365 | |
| 366 | if (!mei_cl_is_connected(cl)) { |
| 367 | dev_err(bus->dev, "Already disconnected"); |
| 368 | err = 0; |
| 369 | goto out; |
| 370 | } |
| 371 | |
| 372 | err = mei_cl_disconnect(cl); |
| 373 | if (err < 0) { |
| 374 | dev_err(bus->dev, "Could not disconnect from the ME client"); |
| 375 | goto out; |
| 376 | } |
| 377 | |
| 378 | /* Flush queues and remove any pending read */ |
| 379 | mei_cl_flush_queues(cl, NULL); |
| 380 | |
| 381 | out: |
| 382 | mutex_unlock(&bus->device_lock); |
| 383 | return err; |
| 384 | |
| 385 | } |
| 386 | EXPORT_SYMBOL_GPL(mei_cl_disable_device); |
| 387 | |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame^] | 388 | /** |
| 389 | * mei_cl_device_find - find matching entry in the driver id table |
| 390 | * |
| 391 | * @cldev: me client device |
| 392 | * @cldrv: me client driver |
| 393 | * |
| 394 | * Return: id on success; NULL if no id is matching |
| 395 | */ |
| 396 | static const |
| 397 | struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev, |
| 398 | struct mei_cl_driver *cldrv) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 399 | { |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 400 | const struct mei_cl_device_id *id; |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 401 | const uuid_le *uuid; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 402 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 403 | uuid = mei_me_cl_uuid(cldev->me_cl); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 404 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 405 | id = cldrv->id_table; |
Greg Kroah-Hartman | b144ce2 | 2015-05-27 17:17:27 -0700 | [diff] [blame] | 406 | while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) { |
Greg Kroah-Hartman | b144ce2 | 2015-05-27 17:17:27 -0700 | [diff] [blame] | 407 | if (!uuid_le_cmp(*uuid, id->uuid)) { |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame^] | 408 | |
| 409 | if (!cldev->name[0]) |
| 410 | return id; |
| 411 | |
| 412 | if (!strncmp(cldev->name, id->name, sizeof(id->name))) |
| 413 | return id; |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 414 | } |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 415 | |
| 416 | id++; |
| 417 | } |
| 418 | |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame^] | 419 | return NULL; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * mei_cl_device_match - device match function |
| 424 | * |
| 425 | * @dev: device |
| 426 | * @drv: driver |
| 427 | * |
| 428 | * Return: 1 if matching device was found 0 otherwise |
| 429 | */ |
| 430 | static int mei_cl_device_match(struct device *dev, struct device_driver *drv) |
| 431 | { |
| 432 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 433 | struct mei_cl_driver *cldrv = to_mei_cl_driver(drv); |
| 434 | const struct mei_cl_device_id *found_id; |
| 435 | |
| 436 | if (!cldev) |
| 437 | return 0; |
| 438 | |
| 439 | if (!cldrv || !cldrv->id_table) |
| 440 | return 0; |
| 441 | |
| 442 | found_id = mei_cl_device_find(cldev, cldrv); |
| 443 | if (found_id) |
| 444 | return 1; |
| 445 | |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static int mei_cl_device_probe(struct device *dev) |
| 450 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 451 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 452 | struct mei_cl_driver *cldrv; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 453 | struct mei_cl_device_id id; |
| 454 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 455 | if (!cldev) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 456 | return 0; |
| 457 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 458 | cldrv = to_mei_cl_driver(dev->driver); |
| 459 | if (!cldrv || !cldrv->probe) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 460 | return -ENODEV; |
| 461 | |
| 462 | dev_dbg(dev, "Device probe\n"); |
| 463 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 464 | strlcpy(id.name, cldev->name, sizeof(id.name)); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 465 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 466 | return cldrv->probe(cldev, &id); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static int mei_cl_device_remove(struct device *dev) |
| 470 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 471 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 472 | struct mei_cl_driver *cldrv; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 473 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 474 | if (!cldev || !dev->driver) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 475 | return 0; |
| 476 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 477 | if (cldev->event_cb) { |
| 478 | cldev->event_cb = NULL; |
| 479 | cancel_work_sync(&cldev->event_work); |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 482 | cldrv = to_mei_cl_driver(dev->driver); |
| 483 | if (!cldrv->remove) { |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 484 | dev->driver = NULL; |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 489 | return cldrv->remove(cldev); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 490 | } |
| 491 | |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 492 | static ssize_t name_show(struct device *dev, struct device_attribute *a, |
| 493 | char *buf) |
| 494 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 495 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 496 | size_t len; |
| 497 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 498 | len = snprintf(buf, PAGE_SIZE, "%s", cldev->name); |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 499 | |
| 500 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; |
| 501 | } |
| 502 | static DEVICE_ATTR_RO(name); |
| 503 | |
| 504 | static ssize_t uuid_show(struct device *dev, struct device_attribute *a, |
| 505 | char *buf) |
| 506 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 507 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 508 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 509 | size_t len; |
| 510 | |
| 511 | len = snprintf(buf, PAGE_SIZE, "%pUl", uuid); |
| 512 | |
| 513 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; |
| 514 | } |
| 515 | static DEVICE_ATTR_RO(uuid); |
| 516 | |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 517 | static ssize_t modalias_show(struct device *dev, struct device_attribute *a, |
| 518 | char *buf) |
| 519 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 520 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 521 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 522 | size_t len; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 523 | |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 524 | len = snprintf(buf, PAGE_SIZE, "mei:%s:" MEI_CL_UUID_FMT ":", |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 525 | cldev->name, MEI_CL_UUID_ARGS(uuid->b)); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 526 | |
| 527 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; |
| 528 | } |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 529 | static DEVICE_ATTR_RO(modalias); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 530 | |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 531 | static struct attribute *mei_cl_dev_attrs[] = { |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 532 | &dev_attr_name.attr, |
| 533 | &dev_attr_uuid.attr, |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 534 | &dev_attr_modalias.attr, |
| 535 | NULL, |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 536 | }; |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 537 | ATTRIBUTE_GROUPS(mei_cl_dev); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 538 | |
Tomas Winkler | 38d3c00 | 2015-07-23 15:08:36 +0300 | [diff] [blame] | 539 | /** |
| 540 | * mei_cl_device_uevent - me client bus uevent handler |
| 541 | * |
| 542 | * @dev: device |
| 543 | * @env: uevent kobject |
| 544 | * |
| 545 | * Return: 0 on success -ENOMEM on when add_uevent_var fails |
| 546 | */ |
| 547 | static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 548 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 549 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 550 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 551 | |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 552 | if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid)) |
| 553 | return -ENOMEM; |
| 554 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 555 | if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name)) |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 556 | return -ENOMEM; |
| 557 | |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 558 | if (add_uevent_var(env, "MODALIAS=mei:%s:" MEI_CL_UUID_FMT ":", |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 559 | cldev->name, MEI_CL_UUID_ARGS(uuid->b))) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 560 | return -ENOMEM; |
| 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | static struct bus_type mei_cl_bus_type = { |
| 566 | .name = "mei", |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 567 | .dev_groups = mei_cl_dev_groups, |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 568 | .match = mei_cl_device_match, |
| 569 | .probe = mei_cl_device_probe, |
| 570 | .remove = mei_cl_device_remove, |
Tomas Winkler | 38d3c00 | 2015-07-23 15:08:36 +0300 | [diff] [blame] | 571 | .uevent = mei_cl_device_uevent, |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 572 | }; |
| 573 | |
| 574 | static void mei_cl_dev_release(struct device *dev) |
| 575 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 576 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 577 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 578 | if (!cldev) |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 579 | return; |
| 580 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 581 | mei_me_cl_put(cldev->me_cl); |
| 582 | kfree(cldev); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | static struct device_type mei_cl_device_type = { |
| 586 | .release = mei_cl_dev_release, |
| 587 | }; |
| 588 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 589 | struct mei_cl *mei_cl_bus_find_cl_by_uuid(struct mei_device *bus, |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 590 | uuid_le uuid) |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 591 | { |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 592 | struct mei_cl *cl; |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 593 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 594 | list_for_each_entry(cl, &bus->device_list, device_link) { |
| 595 | if (cl->cldev && cl->cldev->me_cl && |
| 596 | !uuid_le_cmp(uuid, *mei_me_cl_uuid(cl->cldev->me_cl))) |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 597 | return cl; |
| 598 | } |
| 599 | |
| 600 | return NULL; |
| 601 | } |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 602 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 603 | struct mei_cl_device *mei_cl_add_device(struct mei_device *bus, |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 604 | struct mei_me_client *me_cl, |
| 605 | struct mei_cl *cl, |
Tomas Winkler | be9b720 | 2015-05-07 15:54:04 +0300 | [diff] [blame] | 606 | char *name) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 607 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 608 | struct mei_cl_device *cldev; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 609 | int status; |
| 610 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 611 | cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL); |
| 612 | if (!cldev) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 613 | return NULL; |
| 614 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 615 | cldev->me_cl = mei_me_cl_get(me_cl); |
| 616 | if (!cldev->me_cl) { |
| 617 | kfree(cldev); |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 618 | return NULL; |
| 619 | } |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 620 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 621 | cldev->cl = cl; |
| 622 | cldev->dev.parent = bus->dev; |
| 623 | cldev->dev.bus = &mei_cl_bus_type; |
| 624 | cldev->dev.type = &mei_cl_device_type; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 625 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 626 | strlcpy(cldev->name, name, sizeof(cldev->name)); |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 627 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 628 | dev_set_name(&cldev->dev, "mei:%s:%pUl", name, mei_me_cl_uuid(me_cl)); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 629 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 630 | status = device_register(&cldev->dev); |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 631 | if (status) { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 632 | dev_err(bus->dev, "Failed to register MEI device\n"); |
| 633 | mei_me_cl_put(cldev->me_cl); |
| 634 | kfree(cldev); |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 635 | return NULL; |
| 636 | } |
| 637 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 638 | cl->cldev = cldev; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 639 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 640 | dev_dbg(&cldev->dev, "client %s registered\n", name); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 641 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 642 | return cldev; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 643 | } |
| 644 | EXPORT_SYMBOL_GPL(mei_cl_add_device); |
| 645 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 646 | void mei_cl_remove_device(struct mei_cl_device *cldev) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 647 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 648 | device_unregister(&cldev->dev); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 649 | } |
| 650 | EXPORT_SYMBOL_GPL(mei_cl_remove_device); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 651 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 652 | int __mei_cl_driver_register(struct mei_cl_driver *cldrv, struct module *owner) |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 653 | { |
| 654 | int err; |
| 655 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 656 | cldrv->driver.name = cldrv->name; |
| 657 | cldrv->driver.owner = owner; |
| 658 | cldrv->driver.bus = &mei_cl_bus_type; |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 659 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 660 | err = driver_register(&cldrv->driver); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 661 | if (err) |
| 662 | return err; |
| 663 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 664 | pr_debug("mei: driver [%s] registered\n", cldrv->driver.name); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | EXPORT_SYMBOL_GPL(__mei_cl_driver_register); |
| 669 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 670 | void mei_cl_driver_unregister(struct mei_cl_driver *cldrv) |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 671 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 672 | driver_unregister(&cldrv->driver); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 673 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 674 | pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 675 | } |
| 676 | EXPORT_SYMBOL_GPL(mei_cl_driver_unregister); |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 677 | |
Samuel Ortiz | cf3baef | 2013-03-27 17:29:57 +0200 | [diff] [blame] | 678 | int __init mei_cl_bus_init(void) |
| 679 | { |
| 680 | return bus_register(&mei_cl_bus_type); |
| 681 | } |
| 682 | |
| 683 | void __exit mei_cl_bus_exit(void) |
| 684 | { |
| 685 | bus_unregister(&mei_cl_bus_type); |
| 686 | } |