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 */ |
Alexander Usyskin | bb2ef9c | 2015-07-26 09:54:23 +0300 | [diff] [blame] | 225 | if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) |
| 226 | mei_cl_read_start(cldev->cl, 0, NULL); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * mei_cl_bus_notify_event - schedule notify cb on bus client |
| 231 | * |
| 232 | * @cl: host client |
| 233 | */ |
| 234 | void mei_cl_bus_notify_event(struct mei_cl *cl) |
| 235 | { |
| 236 | struct mei_cl_device *cldev = cl->cldev; |
| 237 | |
| 238 | if (!cldev || !cldev->event_cb) |
| 239 | return; |
| 240 | |
| 241 | if (!(cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF))) |
| 242 | return; |
| 243 | |
| 244 | if (!cl->notify_ev) |
| 245 | return; |
| 246 | |
| 247 | set_bit(MEI_CL_EVENT_NOTIF, &cldev->events); |
| 248 | |
| 249 | schedule_work(&cldev->event_work); |
| 250 | |
| 251 | cl->notify_ev = false; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
| 255 | * mei_cl_bus_rx_event - schedule rx evenet |
| 256 | * |
| 257 | * @cl: host client |
| 258 | */ |
| 259 | void mei_cl_bus_rx_event(struct mei_cl *cl) |
| 260 | { |
| 261 | struct mei_cl_device *cldev = cl->cldev; |
| 262 | |
| 263 | if (!cldev || !cldev->event_cb) |
| 264 | return; |
| 265 | |
Alexander Usyskin | bb2ef9c | 2015-07-26 09:54:23 +0300 | [diff] [blame] | 266 | if (!(cldev->events_mask & BIT(MEI_CL_EVENT_RX))) |
| 267 | return; |
| 268 | |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 269 | set_bit(MEI_CL_EVENT_RX, &cldev->events); |
| 270 | |
| 271 | schedule_work(&cldev->event_work); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * mei_cl_register_event_cb - register event callback |
| 276 | * |
| 277 | * @cldev: me client devices |
| 278 | * @event_cb: callback function |
Alexander Usyskin | bb2ef9c | 2015-07-26 09:54:23 +0300 | [diff] [blame] | 279 | * @events_mask: requested events bitmask |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 280 | * @context: driver context data |
| 281 | * |
| 282 | * Return: 0 on success |
| 283 | * -EALREADY if an callback is already registered |
| 284 | * <0 on other errors |
| 285 | */ |
| 286 | int mei_cl_register_event_cb(struct mei_cl_device *cldev, |
Alexander Usyskin | bb2ef9c | 2015-07-26 09:54:23 +0300 | [diff] [blame] | 287 | unsigned long events_mask, |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 288 | mei_cl_event_cb_t event_cb, void *context) |
| 289 | { |
Tomas Winkler | 48168f4 | 2015-07-23 15:08:38 +0300 | [diff] [blame] | 290 | int ret; |
| 291 | |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 292 | if (cldev->event_cb) |
| 293 | return -EALREADY; |
| 294 | |
| 295 | cldev->events = 0; |
Alexander Usyskin | bb2ef9c | 2015-07-26 09:54:23 +0300 | [diff] [blame] | 296 | cldev->events_mask = events_mask; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 297 | cldev->event_cb = event_cb; |
| 298 | cldev->event_context = context; |
| 299 | INIT_WORK(&cldev->event_work, mei_bus_event_work); |
| 300 | |
Alexander Usyskin | bb2ef9c | 2015-07-26 09:54:23 +0300 | [diff] [blame] | 301 | if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) { |
| 302 | ret = mei_cl_read_start(cldev->cl, 0, NULL); |
| 303 | if (ret && ret != -EBUSY) |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | if (cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)) { |
| 308 | mutex_lock(&cldev->cl->dev->device_lock); |
| 309 | ret = mei_cl_notify_request(cldev->cl, NULL, event_cb ? 1 : 0); |
| 310 | mutex_unlock(&cldev->cl->dev->device_lock); |
| 311 | if (ret) |
| 312 | return ret; |
| 313 | } |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | EXPORT_SYMBOL_GPL(mei_cl_register_event_cb); |
| 318 | |
| 319 | /** |
| 320 | * mei_cl_get_drvdata - driver data getter |
| 321 | * |
| 322 | * @cldev: mei client device |
| 323 | * |
| 324 | * Return: driver private data |
| 325 | */ |
| 326 | void *mei_cl_get_drvdata(const struct mei_cl_device *cldev) |
| 327 | { |
| 328 | return dev_get_drvdata(&cldev->dev); |
| 329 | } |
| 330 | EXPORT_SYMBOL_GPL(mei_cl_get_drvdata); |
| 331 | |
| 332 | /** |
| 333 | * mei_cl_set_drvdata - driver data setter |
| 334 | * |
| 335 | * @cldev: mei client device |
| 336 | * @data: data to store |
| 337 | */ |
| 338 | void mei_cl_set_drvdata(struct mei_cl_device *cldev, void *data) |
| 339 | { |
| 340 | dev_set_drvdata(&cldev->dev, data); |
| 341 | } |
| 342 | EXPORT_SYMBOL_GPL(mei_cl_set_drvdata); |
| 343 | |
| 344 | /** |
Tomas Winkler | baeacd0 | 2015-09-10 10:18:02 +0300 | [diff] [blame^] | 345 | * mei_cldev_uuid - return uuid of the underlying me client |
| 346 | * |
| 347 | * @cldev: mei client device |
| 348 | * |
| 349 | * Return: me client uuid |
| 350 | */ |
| 351 | const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev) |
| 352 | { |
| 353 | return mei_me_cl_uuid(cldev->me_cl); |
| 354 | } |
| 355 | EXPORT_SYMBOL_GPL(mei_cldev_uuid); |
| 356 | |
| 357 | /** |
| 358 | * mei_cldev_ver - return protocol version of the underlying me client |
| 359 | * |
| 360 | * @cldev: mei client device |
| 361 | * |
| 362 | * Return: me client protocol version |
| 363 | */ |
| 364 | u8 mei_cldev_ver(const struct mei_cl_device *cldev) |
| 365 | { |
| 366 | return mei_me_cl_ver(cldev->me_cl); |
| 367 | } |
| 368 | EXPORT_SYMBOL_GPL(mei_cldev_ver); |
| 369 | |
| 370 | /** |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 371 | * mei_cl_enable_device - enable me client device |
| 372 | * create connection with me client |
| 373 | * |
| 374 | * @cldev: me client device |
| 375 | * |
| 376 | * Return: 0 on success and < 0 on error |
| 377 | */ |
| 378 | int mei_cl_enable_device(struct mei_cl_device *cldev) |
| 379 | { |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 380 | struct mei_device *bus = cldev->bus; |
| 381 | struct mei_cl *cl; |
| 382 | int ret; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 383 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 384 | cl = cldev->cl; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 385 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 386 | if (!cl) { |
| 387 | mutex_lock(&bus->device_lock); |
| 388 | cl = mei_cl_alloc_linked(bus, MEI_HOST_CLIENT_ID_ANY); |
| 389 | mutex_unlock(&bus->device_lock); |
| 390 | if (IS_ERR(cl)) |
| 391 | return PTR_ERR(cl); |
| 392 | /* update pointers */ |
| 393 | cldev->cl = cl; |
| 394 | cl->cldev = cldev; |
| 395 | } |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 396 | |
| 397 | mutex_lock(&bus->device_lock); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 398 | if (mei_cl_is_connected(cl)) { |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 399 | ret = 0; |
| 400 | goto out; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 401 | } |
| 402 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 403 | if (!mei_me_cl_is_active(cldev->me_cl)) { |
| 404 | dev_err(&cldev->dev, "me client is not active\n"); |
| 405 | ret = -ENOTTY; |
| 406 | goto out; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 407 | } |
| 408 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 409 | ret = mei_cl_connect(cl, cldev->me_cl, NULL); |
| 410 | if (ret < 0) |
| 411 | dev_err(&cldev->dev, "cannot connect\n"); |
| 412 | |
| 413 | out: |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 414 | mutex_unlock(&bus->device_lock); |
| 415 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 416 | return ret; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 417 | } |
| 418 | EXPORT_SYMBOL_GPL(mei_cl_enable_device); |
| 419 | |
| 420 | /** |
| 421 | * mei_cl_disable_device - disable me client device |
| 422 | * disconnect form the me client |
| 423 | * |
| 424 | * @cldev: me client device |
| 425 | * |
| 426 | * Return: 0 on success and < 0 on error |
| 427 | */ |
| 428 | int mei_cl_disable_device(struct mei_cl_device *cldev) |
| 429 | { |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 430 | struct mei_device *bus; |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 431 | struct mei_cl *cl; |
| 432 | int err; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 433 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 434 | if (!cldev || !cldev->cl) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 435 | return -ENODEV; |
| 436 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 437 | cl = cldev->cl; |
| 438 | |
| 439 | bus = cldev->bus; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 440 | |
| 441 | cldev->event_cb = NULL; |
| 442 | |
| 443 | mutex_lock(&bus->device_lock); |
| 444 | |
| 445 | if (!mei_cl_is_connected(cl)) { |
| 446 | dev_err(bus->dev, "Already disconnected"); |
| 447 | err = 0; |
| 448 | goto out; |
| 449 | } |
| 450 | |
| 451 | err = mei_cl_disconnect(cl); |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 452 | if (err < 0) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 453 | dev_err(bus->dev, "Could not disconnect from the ME client"); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 454 | |
| 455 | out: |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 456 | /* Flush queues and remove any pending read */ |
| 457 | mei_cl_flush_queues(cl, NULL); |
| 458 | mei_cl_unlink(cl); |
| 459 | |
| 460 | kfree(cl); |
| 461 | cldev->cl = NULL; |
| 462 | |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 463 | mutex_unlock(&bus->device_lock); |
| 464 | return err; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 465 | } |
| 466 | EXPORT_SYMBOL_GPL(mei_cl_disable_device); |
| 467 | |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame] | 468 | /** |
| 469 | * mei_cl_device_find - find matching entry in the driver id table |
| 470 | * |
| 471 | * @cldev: me client device |
| 472 | * @cldrv: me client driver |
| 473 | * |
| 474 | * Return: id on success; NULL if no id is matching |
| 475 | */ |
| 476 | static const |
| 477 | struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev, |
| 478 | struct mei_cl_driver *cldrv) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 479 | { |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 480 | const struct mei_cl_device_id *id; |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 481 | const uuid_le *uuid; |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 482 | u8 version; |
| 483 | bool match; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 484 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 485 | uuid = mei_me_cl_uuid(cldev->me_cl); |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 486 | version = mei_me_cl_ver(cldev->me_cl); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 487 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 488 | id = cldrv->id_table; |
Greg Kroah-Hartman | b144ce2 | 2015-05-27 17:17:27 -0700 | [diff] [blame] | 489 | while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) { |
Greg Kroah-Hartman | b144ce2 | 2015-05-27 17:17:27 -0700 | [diff] [blame] | 490 | if (!uuid_le_cmp(*uuid, id->uuid)) { |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 491 | match = true; |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame] | 492 | |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 493 | if (cldev->name[0]) |
| 494 | if (strncmp(cldev->name, id->name, |
| 495 | sizeof(id->name))) |
| 496 | match = false; |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame] | 497 | |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 498 | if (id->version != MEI_CL_VERSION_ANY) |
| 499 | if (id->version != version) |
| 500 | match = false; |
| 501 | if (match) |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame] | 502 | return id; |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 503 | } |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 504 | |
| 505 | id++; |
| 506 | } |
| 507 | |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame] | 508 | return NULL; |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * mei_cl_device_match - device match function |
| 513 | * |
| 514 | * @dev: device |
| 515 | * @drv: driver |
| 516 | * |
| 517 | * Return: 1 if matching device was found 0 otherwise |
| 518 | */ |
| 519 | static int mei_cl_device_match(struct device *dev, struct device_driver *drv) |
| 520 | { |
| 521 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 522 | struct mei_cl_driver *cldrv = to_mei_cl_driver(drv); |
| 523 | const struct mei_cl_device_id *found_id; |
| 524 | |
| 525 | if (!cldev) |
| 526 | return 0; |
| 527 | |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 528 | if (!cldev->do_match) |
| 529 | return 0; |
| 530 | |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame] | 531 | if (!cldrv || !cldrv->id_table) |
| 532 | return 0; |
| 533 | |
| 534 | found_id = mei_cl_device_find(cldev, cldrv); |
| 535 | if (found_id) |
| 536 | return 1; |
| 537 | |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 538 | return 0; |
| 539 | } |
| 540 | |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 541 | /** |
| 542 | * mei_cl_device_probe - bus probe function |
| 543 | * |
| 544 | * @dev: device |
| 545 | * |
| 546 | * Return: 0 on success; < 0 otherwise |
| 547 | */ |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 548 | static int mei_cl_device_probe(struct device *dev) |
| 549 | { |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 550 | struct mei_cl_device *cldev; |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 551 | struct mei_cl_driver *cldrv; |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 552 | const struct mei_cl_device_id *id; |
| 553 | |
| 554 | cldev = to_mei_cl_device(dev); |
| 555 | cldrv = to_mei_cl_driver(dev->driver); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 556 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 557 | if (!cldev) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 558 | return 0; |
| 559 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 560 | if (!cldrv || !cldrv->probe) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 561 | return -ENODEV; |
| 562 | |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 563 | id = mei_cl_device_find(cldev, cldrv); |
| 564 | if (!id) |
| 565 | return -ENODEV; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 566 | |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 567 | __module_get(THIS_MODULE); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 568 | |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 569 | return cldrv->probe(cldev, id); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 570 | } |
| 571 | |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 572 | /** |
| 573 | * mei_cl_device_remove - remove device from the bus |
| 574 | * |
| 575 | * @dev: device |
| 576 | * |
| 577 | * Return: 0 on success; < 0 otherwise |
| 578 | */ |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 579 | static int mei_cl_device_remove(struct device *dev) |
| 580 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 581 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 582 | struct mei_cl_driver *cldrv; |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 583 | int ret = 0; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 584 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 585 | if (!cldev || !dev->driver) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 586 | return 0; |
| 587 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 588 | if (cldev->event_cb) { |
| 589 | cldev->event_cb = NULL; |
| 590 | cancel_work_sync(&cldev->event_work); |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 591 | } |
| 592 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 593 | cldrv = to_mei_cl_driver(dev->driver); |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 594 | if (cldrv->remove) |
| 595 | ret = cldrv->remove(cldev); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 596 | |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 597 | module_put(THIS_MODULE); |
| 598 | dev->driver = NULL; |
| 599 | return ret; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 600 | |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 601 | } |
| 602 | |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 603 | static ssize_t name_show(struct device *dev, struct device_attribute *a, |
| 604 | char *buf) |
| 605 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 606 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 607 | size_t len; |
| 608 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 609 | len = snprintf(buf, PAGE_SIZE, "%s", cldev->name); |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 610 | |
| 611 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; |
| 612 | } |
| 613 | static DEVICE_ATTR_RO(name); |
| 614 | |
| 615 | static ssize_t uuid_show(struct device *dev, struct device_attribute *a, |
| 616 | char *buf) |
| 617 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 618 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 619 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 620 | size_t len; |
| 621 | |
| 622 | len = snprintf(buf, PAGE_SIZE, "%pUl", uuid); |
| 623 | |
| 624 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; |
| 625 | } |
| 626 | static DEVICE_ATTR_RO(uuid); |
| 627 | |
Tomas Winkler | 40b7320 | 2015-09-10 10:18:00 +0300 | [diff] [blame] | 628 | static ssize_t version_show(struct device *dev, struct device_attribute *a, |
| 629 | char *buf) |
| 630 | { |
| 631 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 632 | u8 version = mei_me_cl_ver(cldev->me_cl); |
| 633 | size_t len; |
| 634 | |
| 635 | len = snprintf(buf, PAGE_SIZE, "%02X", version); |
| 636 | |
| 637 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; |
| 638 | } |
| 639 | static DEVICE_ATTR_RO(version); |
| 640 | |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 641 | static ssize_t modalias_show(struct device *dev, struct device_attribute *a, |
| 642 | char *buf) |
| 643 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 644 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 645 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 646 | size_t len; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 647 | |
Prarit Bhargava | 59796ed | 2015-09-10 10:17:59 +0300 | [diff] [blame] | 648 | len = snprintf(buf, PAGE_SIZE, "mei:%s:%pUl:", cldev->name, uuid); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 649 | return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; |
| 650 | } |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 651 | static DEVICE_ATTR_RO(modalias); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 652 | |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 653 | static struct attribute *mei_cl_dev_attrs[] = { |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 654 | &dev_attr_name.attr, |
| 655 | &dev_attr_uuid.attr, |
Tomas Winkler | 40b7320 | 2015-09-10 10:18:00 +0300 | [diff] [blame] | 656 | &dev_attr_version.attr, |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 657 | &dev_attr_modalias.attr, |
| 658 | NULL, |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 659 | }; |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 660 | ATTRIBUTE_GROUPS(mei_cl_dev); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 661 | |
Tomas Winkler | 38d3c00 | 2015-07-23 15:08:36 +0300 | [diff] [blame] | 662 | /** |
| 663 | * mei_cl_device_uevent - me client bus uevent handler |
| 664 | * |
| 665 | * @dev: device |
| 666 | * @env: uevent kobject |
| 667 | * |
| 668 | * Return: 0 on success -ENOMEM on when add_uevent_var fails |
| 669 | */ |
| 670 | 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] | 671 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 672 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 673 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); |
Tomas Winkler | 40b7320 | 2015-09-10 10:18:00 +0300 | [diff] [blame] | 674 | u8 version = mei_me_cl_ver(cldev->me_cl); |
| 675 | |
| 676 | if (add_uevent_var(env, "MEI_CL_VERSION=%d", version)) |
| 677 | return -ENOMEM; |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 678 | |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 679 | if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid)) |
| 680 | return -ENOMEM; |
| 681 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 682 | if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name)) |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 683 | return -ENOMEM; |
| 684 | |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 685 | if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:", |
| 686 | cldev->name, uuid, version)) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 687 | return -ENOMEM; |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static struct bus_type mei_cl_bus_type = { |
| 693 | .name = "mei", |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 694 | .dev_groups = mei_cl_dev_groups, |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 695 | .match = mei_cl_device_match, |
| 696 | .probe = mei_cl_device_probe, |
| 697 | .remove = mei_cl_device_remove, |
Tomas Winkler | 38d3c00 | 2015-07-23 15:08:36 +0300 | [diff] [blame] | 698 | .uevent = mei_cl_device_uevent, |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 699 | }; |
| 700 | |
Tomas Winkler | 512f64d | 2015-07-23 15:08:41 +0300 | [diff] [blame] | 701 | static struct mei_device *mei_dev_bus_get(struct mei_device *bus) |
| 702 | { |
| 703 | if (bus) |
| 704 | get_device(bus->dev); |
| 705 | |
| 706 | return bus; |
| 707 | } |
| 708 | |
| 709 | static void mei_dev_bus_put(struct mei_device *bus) |
| 710 | { |
| 711 | if (bus) |
| 712 | put_device(bus->dev); |
| 713 | } |
| 714 | |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 715 | static void mei_cl_dev_release(struct device *dev) |
| 716 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 717 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 718 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 719 | if (!cldev) |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 720 | return; |
| 721 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 722 | mei_me_cl_put(cldev->me_cl); |
Tomas Winkler | 512f64d | 2015-07-23 15:08:41 +0300 | [diff] [blame] | 723 | mei_dev_bus_put(cldev->bus); |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 724 | kfree(cldev); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | static struct device_type mei_cl_device_type = { |
| 728 | .release = mei_cl_dev_release, |
| 729 | }; |
| 730 | |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 731 | /** |
| 732 | * mei_cl_dev_alloc - initialize and allocate mei client device |
| 733 | * |
| 734 | * @bus: mei device |
| 735 | * @me_cl: me client |
| 736 | * |
| 737 | * Return: allocated device structur or NULL on allocation failure |
| 738 | */ |
| 739 | static struct mei_cl_device *mei_cl_dev_alloc(struct mei_device *bus, |
| 740 | struct mei_me_client *me_cl) |
| 741 | { |
| 742 | struct mei_cl_device *cldev; |
| 743 | |
| 744 | cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL); |
| 745 | if (!cldev) |
| 746 | return NULL; |
| 747 | |
| 748 | device_initialize(&cldev->dev); |
| 749 | cldev->dev.parent = bus->dev; |
| 750 | cldev->dev.bus = &mei_cl_bus_type; |
| 751 | cldev->dev.type = &mei_cl_device_type; |
| 752 | cldev->bus = mei_dev_bus_get(bus); |
| 753 | cldev->me_cl = mei_me_cl_get(me_cl); |
| 754 | cldev->is_added = 0; |
| 755 | INIT_LIST_HEAD(&cldev->bus_list); |
| 756 | |
| 757 | return cldev; |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * mei_cl_dev_setup - setup me client device |
| 762 | * run fix up routines and set the device name |
| 763 | * |
| 764 | * @bus: mei device |
| 765 | * @cldev: me client device |
| 766 | * |
| 767 | * Return: true if the device is eligible for enumeration |
| 768 | */ |
| 769 | static bool mei_cl_dev_setup(struct mei_device *bus, |
| 770 | struct mei_cl_device *cldev) |
| 771 | { |
| 772 | cldev->do_match = 1; |
| 773 | mei_cl_dev_fixup(cldev); |
| 774 | |
| 775 | if (cldev->do_match) |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 776 | dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X", |
| 777 | cldev->name, |
| 778 | mei_me_cl_uuid(cldev->me_cl), |
| 779 | mei_me_cl_ver(cldev->me_cl)); |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 780 | |
| 781 | return cldev->do_match == 1; |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * mei_cl_bus_dev_add - add me client devices |
| 786 | * |
| 787 | * @cldev: me client device |
| 788 | * |
| 789 | * Return: 0 on success; < 0 on failre |
| 790 | */ |
| 791 | static int mei_cl_bus_dev_add(struct mei_cl_device *cldev) |
| 792 | { |
| 793 | int ret; |
| 794 | |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 795 | dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n", |
| 796 | mei_me_cl_uuid(cldev->me_cl), |
| 797 | mei_me_cl_ver(cldev->me_cl)); |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 798 | ret = device_add(&cldev->dev); |
| 799 | if (!ret) |
| 800 | cldev->is_added = 1; |
| 801 | |
| 802 | return ret; |
| 803 | } |
| 804 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 805 | /** |
| 806 | * mei_cl_bus_dev_stop - stop the driver |
| 807 | * |
| 808 | * @cldev: me client device |
| 809 | */ |
| 810 | static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev) |
| 811 | { |
| 812 | if (cldev->is_added) |
| 813 | device_release_driver(&cldev->dev); |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * mei_cl_bus_dev_destroy - destroy me client devices object |
| 818 | * |
| 819 | * @cldev: me client device |
| 820 | */ |
| 821 | static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev) |
| 822 | { |
| 823 | if (!cldev->is_added) |
| 824 | return; |
| 825 | |
| 826 | device_del(&cldev->dev); |
| 827 | |
| 828 | mutex_lock(&cldev->bus->cl_bus_lock); |
| 829 | list_del_init(&cldev->bus_list); |
| 830 | mutex_unlock(&cldev->bus->cl_bus_lock); |
| 831 | |
| 832 | cldev->is_added = 0; |
| 833 | put_device(&cldev->dev); |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * mei_cl_bus_remove_device - remove a devices form the bus |
| 838 | * |
| 839 | * @cldev: me client device |
| 840 | */ |
| 841 | static void mei_cl_bus_remove_device(struct mei_cl_device *cldev) |
| 842 | { |
| 843 | mei_cl_bus_dev_stop(cldev); |
| 844 | mei_cl_bus_dev_destroy(cldev); |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * mei_cl_bus_remove_devices - remove all devices form the bus |
| 849 | * |
| 850 | * @bus: mei device |
| 851 | */ |
| 852 | void mei_cl_bus_remove_devices(struct mei_device *bus) |
| 853 | { |
| 854 | struct mei_cl_device *cldev, *next; |
| 855 | |
| 856 | list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list) |
| 857 | mei_cl_bus_remove_device(cldev); |
| 858 | } |
| 859 | |
| 860 | |
| 861 | /** |
| 862 | * mei_cl_dev_init - allocate and initializes an mei client devices |
| 863 | * based on me client |
| 864 | * |
| 865 | * @bus: mei device |
| 866 | * @me_cl: me client |
| 867 | */ |
| 868 | static void mei_cl_dev_init(struct mei_device *bus, struct mei_me_client *me_cl) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 869 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 870 | struct mei_cl_device *cldev; |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 871 | |
| 872 | dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl)); |
| 873 | |
| 874 | if (me_cl->bus_added) |
| 875 | return; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 876 | |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 877 | cldev = mei_cl_dev_alloc(bus, me_cl); |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 878 | if (!cldev) |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 879 | return; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 880 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 881 | mutex_lock(&cldev->bus->cl_bus_lock); |
| 882 | me_cl->bus_added = true; |
| 883 | list_add_tail(&cldev->bus_list, &bus->device_list); |
| 884 | mutex_unlock(&cldev->bus->cl_bus_lock); |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 885 | |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 886 | } |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 887 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 888 | /** |
| 889 | * mei_cl_bus_rescan - scan me clients list and add create |
| 890 | * devices for eligible clients |
| 891 | * |
| 892 | * @bus: mei device |
| 893 | */ |
| 894 | void mei_cl_bus_rescan(struct mei_device *bus) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 895 | { |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 896 | struct mei_cl_device *cldev, *n; |
| 897 | struct mei_me_client *me_cl; |
| 898 | |
| 899 | down_read(&bus->me_clients_rwsem); |
| 900 | list_for_each_entry(me_cl, &bus->me_clients, list) |
| 901 | mei_cl_dev_init(bus, me_cl); |
| 902 | up_read(&bus->me_clients_rwsem); |
| 903 | |
| 904 | mutex_lock(&bus->cl_bus_lock); |
| 905 | list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) { |
| 906 | |
| 907 | if (!mei_me_cl_is_active(cldev->me_cl)) { |
| 908 | mei_cl_bus_remove_device(cldev); |
| 909 | continue; |
| 910 | } |
| 911 | |
| 912 | if (cldev->is_added) |
| 913 | continue; |
| 914 | |
| 915 | if (mei_cl_dev_setup(bus, cldev)) |
| 916 | mei_cl_bus_dev_add(cldev); |
| 917 | else { |
| 918 | list_del_init(&cldev->bus_list); |
| 919 | put_device(&cldev->dev); |
| 920 | } |
| 921 | } |
| 922 | mutex_unlock(&bus->cl_bus_lock); |
| 923 | |
| 924 | dev_dbg(bus->dev, "rescan end"); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 925 | } |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 926 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 927 | 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] | 928 | { |
| 929 | int err; |
| 930 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 931 | cldrv->driver.name = cldrv->name; |
| 932 | cldrv->driver.owner = owner; |
| 933 | cldrv->driver.bus = &mei_cl_bus_type; |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 934 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 935 | err = driver_register(&cldrv->driver); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 936 | if (err) |
| 937 | return err; |
| 938 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 939 | pr_debug("mei: driver [%s] registered\n", cldrv->driver.name); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | EXPORT_SYMBOL_GPL(__mei_cl_driver_register); |
| 944 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 945 | void mei_cl_driver_unregister(struct mei_cl_driver *cldrv) |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 946 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 947 | driver_unregister(&cldrv->driver); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 948 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 949 | pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 950 | } |
| 951 | EXPORT_SYMBOL_GPL(mei_cl_driver_unregister); |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 952 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 953 | |
Samuel Ortiz | cf3baef | 2013-03-27 17:29:57 +0200 | [diff] [blame] | 954 | int __init mei_cl_bus_init(void) |
| 955 | { |
| 956 | return bus_register(&mei_cl_bus_type); |
| 957 | } |
| 958 | |
| 959 | void __exit mei_cl_bus_exit(void) |
| 960 | { |
| 961 | bus_unregister(&mei_cl_bus_type); |
| 962 | } |