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> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 19 | #include <linux/sched/signal.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 |
Alexander Usyskin | e0cb6b2 | 2016-11-08 18:26:08 +0200 | [diff] [blame] | 39 | * @mode: sending mode |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 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, |
Alexander Usyskin | e0cb6b2 | 2016-11-08 18:26:08 +0200 | [diff] [blame] | 44 | unsigned int mode) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 45 | { |
| 46 | struct mei_device *bus; |
Alexander Usyskin | 6cbb097 | 2016-02-10 23:57:26 +0200 | [diff] [blame] | 47 | struct mei_cl_cb *cb; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 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); |
Alexander Usyskin | 15c13df | 2016-02-07 23:35:32 +0200 | [diff] [blame] | 56 | if (bus->dev_state != MEI_DEV_ENABLED) { |
| 57 | rets = -ENODEV; |
| 58 | goto out; |
| 59 | } |
| 60 | |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 61 | if (!mei_cl_is_connected(cl)) { |
| 62 | rets = -ENODEV; |
| 63 | goto out; |
| 64 | } |
| 65 | |
| 66 | /* Check if we have an ME client device */ |
| 67 | if (!mei_me_cl_is_active(cl->me_cl)) { |
| 68 | rets = -ENOTTY; |
| 69 | goto out; |
| 70 | } |
| 71 | |
| 72 | if (length > mei_cl_mtu(cl)) { |
| 73 | rets = -EFBIG; |
| 74 | goto out; |
| 75 | } |
| 76 | |
| 77 | cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL); |
| 78 | if (!cb) { |
| 79 | rets = -ENOMEM; |
| 80 | goto out; |
| 81 | } |
| 82 | |
Alexander Usyskin | e0cb6b2 | 2016-11-08 18:26:08 +0200 | [diff] [blame] | 83 | cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL); |
| 84 | cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 85 | memcpy(cb->buf.data, buf, length); |
| 86 | |
Alexander Usyskin | e0cb6b2 | 2016-11-08 18:26:08 +0200 | [diff] [blame] | 87 | rets = mei_cl_write(cl, cb); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 88 | |
| 89 | out: |
| 90 | mutex_unlock(&bus->device_lock); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 91 | |
| 92 | return rets; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * __mei_cl_recv - internal client receive (read) |
| 97 | * |
| 98 | * @cl: host client |
Tomas Winkler | df7f544 | 2015-10-13 15:02:40 +0300 | [diff] [blame] | 99 | * @buf: buffer to receive |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 100 | * @length: buffer length |
Alexander Usyskin | 076802d | 2016-12-03 00:15:25 +0200 | [diff] [blame] | 101 | * @mode: io mode |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 102 | * |
| 103 | * Return: read size in bytes of < 0 on error |
| 104 | */ |
Alexander Usyskin | 076802d | 2016-12-03 00:15:25 +0200 | [diff] [blame] | 105 | ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length, |
| 106 | unsigned int mode) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 107 | { |
| 108 | struct mei_device *bus; |
| 109 | struct mei_cl_cb *cb; |
| 110 | size_t r_length; |
| 111 | ssize_t rets; |
Alexander Usyskin | 076802d | 2016-12-03 00:15:25 +0200 | [diff] [blame] | 112 | bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 113 | |
| 114 | if (WARN_ON(!cl || !cl->dev)) |
| 115 | return -ENODEV; |
| 116 | |
| 117 | bus = cl->dev; |
| 118 | |
| 119 | mutex_lock(&bus->device_lock); |
Alexander Usyskin | 15c13df | 2016-02-07 23:35:32 +0200 | [diff] [blame] | 120 | if (bus->dev_state != MEI_DEV_ENABLED) { |
| 121 | rets = -ENODEV; |
| 122 | goto out; |
| 123 | } |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 124 | |
| 125 | cb = mei_cl_read_cb(cl, NULL); |
| 126 | if (cb) |
| 127 | goto copy; |
| 128 | |
| 129 | rets = mei_cl_read_start(cl, length, NULL); |
| 130 | if (rets && rets != -EBUSY) |
| 131 | goto out; |
| 132 | |
Alexander Usyskin | 076802d | 2016-12-03 00:15:25 +0200 | [diff] [blame] | 133 | if (nonblock) { |
| 134 | rets = -EAGAIN; |
| 135 | goto out; |
| 136 | } |
| 137 | |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 138 | /* wait on event only if there is no other waiter */ |
Alexander Usyskin | 1eb5bd4 | 2016-06-16 17:58:54 +0300 | [diff] [blame] | 139 | /* synchronized under device mutex */ |
| 140 | if (!waitqueue_active(&cl->rx_wait)) { |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 141 | |
| 142 | mutex_unlock(&bus->device_lock); |
| 143 | |
| 144 | if (wait_event_interruptible(cl->rx_wait, |
| 145 | (!list_empty(&cl->rd_completed)) || |
| 146 | (!mei_cl_is_connected(cl)))) { |
| 147 | |
| 148 | if (signal_pending(current)) |
| 149 | return -EINTR; |
| 150 | return -ERESTARTSYS; |
| 151 | } |
| 152 | |
| 153 | mutex_lock(&bus->device_lock); |
| 154 | |
| 155 | if (!mei_cl_is_connected(cl)) { |
Tomas Winkler | 2d4d548 | 2016-06-23 00:25:31 +0300 | [diff] [blame] | 156 | rets = -ENODEV; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 157 | goto out; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | cb = mei_cl_read_cb(cl, NULL); |
| 162 | if (!cb) { |
| 163 | rets = 0; |
| 164 | goto out; |
| 165 | } |
| 166 | |
| 167 | copy: |
| 168 | if (cb->status) { |
| 169 | rets = cb->status; |
| 170 | goto free; |
| 171 | } |
| 172 | |
| 173 | r_length = min_t(size_t, length, cb->buf_idx); |
| 174 | memcpy(buf, cb->buf.data, r_length); |
| 175 | rets = r_length; |
| 176 | |
| 177 | free: |
| 178 | mei_io_cb_free(cb); |
| 179 | out: |
| 180 | mutex_unlock(&bus->device_lock); |
| 181 | |
| 182 | return rets; |
| 183 | } |
| 184 | |
| 185 | /** |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 186 | * mei_cldev_send - me device send (write) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 187 | * |
| 188 | * @cldev: me client device |
| 189 | * @buf: buffer to send |
| 190 | * @length: buffer length |
| 191 | * |
| 192 | * Return: written size in bytes or < 0 on error |
| 193 | */ |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 194 | ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 195 | { |
| 196 | struct mei_cl *cl = cldev->cl; |
| 197 | |
Alexander Usyskin | e0cb6b2 | 2016-11-08 18:26:08 +0200 | [diff] [blame] | 198 | return __mei_cl_send(cl, buf, length, MEI_CL_IO_TX_BLOCKING); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 199 | } |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 200 | EXPORT_SYMBOL_GPL(mei_cldev_send); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 201 | |
| 202 | /** |
Alexander Usyskin | 076802d | 2016-12-03 00:15:25 +0200 | [diff] [blame] | 203 | * mei_cldev_recv_nonblock - non block client receive (read) |
| 204 | * |
| 205 | * @cldev: me client device |
| 206 | * @buf: buffer to receive |
| 207 | * @length: buffer length |
| 208 | * |
| 209 | * Return: read size in bytes of < 0 on error |
| 210 | * -EAGAIN if function will block. |
| 211 | */ |
| 212 | ssize_t mei_cldev_recv_nonblock(struct mei_cl_device *cldev, u8 *buf, |
| 213 | size_t length) |
| 214 | { |
| 215 | struct mei_cl *cl = cldev->cl; |
| 216 | |
| 217 | return __mei_cl_recv(cl, buf, length, MEI_CL_IO_RX_NONBLOCK); |
| 218 | } |
| 219 | EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock); |
| 220 | |
| 221 | /** |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 222 | * mei_cldev_recv - client receive (read) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 223 | * |
| 224 | * @cldev: me client device |
Tomas Winkler | df7f544 | 2015-10-13 15:02:40 +0300 | [diff] [blame] | 225 | * @buf: buffer to receive |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 226 | * @length: buffer length |
| 227 | * |
| 228 | * Return: read size in bytes of < 0 on error |
| 229 | */ |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 230 | ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 231 | { |
| 232 | struct mei_cl *cl = cldev->cl; |
| 233 | |
Alexander Usyskin | 076802d | 2016-12-03 00:15:25 +0200 | [diff] [blame] | 234 | return __mei_cl_recv(cl, buf, length, 0); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 235 | } |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 236 | EXPORT_SYMBOL_GPL(mei_cldev_recv); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 237 | |
| 238 | /** |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 239 | * mei_cl_bus_rx_work - dispatch rx event for a bus device |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 240 | * |
| 241 | * @work: work |
| 242 | */ |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 243 | static void mei_cl_bus_rx_work(struct work_struct *work) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 244 | { |
| 245 | struct mei_cl_device *cldev; |
Alexander Usyskin | bc46b45 | 2016-05-03 18:54:21 -0400 | [diff] [blame] | 246 | struct mei_device *bus; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 247 | |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 248 | cldev = container_of(work, struct mei_cl_device, rx_work); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 249 | |
Alexander Usyskin | bc46b45 | 2016-05-03 18:54:21 -0400 | [diff] [blame] | 250 | bus = cldev->bus; |
| 251 | |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 252 | if (cldev->rx_cb) |
| 253 | cldev->rx_cb(cldev); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 254 | |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 255 | mutex_lock(&bus->device_lock); |
| 256 | mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL); |
| 257 | mutex_unlock(&bus->device_lock); |
| 258 | } |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 259 | |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 260 | /** |
| 261 | * mei_cl_bus_notif_work - dispatch FW notif event for a bus device |
| 262 | * |
| 263 | * @work: work |
| 264 | */ |
| 265 | static void mei_cl_bus_notif_work(struct work_struct *work) |
| 266 | { |
| 267 | struct mei_cl_device *cldev; |
| 268 | |
| 269 | cldev = container_of(work, struct mei_cl_device, notif_work); |
| 270 | |
| 271 | if (cldev->notif_cb) |
| 272 | cldev->notif_cb(cldev); |
Alexander Usyskin | bb2ef9c | 2015-07-26 09:54:23 +0300 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
| 276 | * mei_cl_bus_notify_event - schedule notify cb on bus client |
| 277 | * |
| 278 | * @cl: host client |
Tomas Winkler | 850f894 | 2016-02-07 23:35:31 +0200 | [diff] [blame] | 279 | * |
| 280 | * Return: true if event was scheduled |
| 281 | * false if the client is not waiting for event |
Alexander Usyskin | bb2ef9c | 2015-07-26 09:54:23 +0300 | [diff] [blame] | 282 | */ |
Tomas Winkler | 850f894 | 2016-02-07 23:35:31 +0200 | [diff] [blame] | 283 | bool mei_cl_bus_notify_event(struct mei_cl *cl) |
Alexander Usyskin | bb2ef9c | 2015-07-26 09:54:23 +0300 | [diff] [blame] | 284 | { |
| 285 | struct mei_cl_device *cldev = cl->cldev; |
| 286 | |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 287 | if (!cldev || !cldev->notif_cb) |
Tomas Winkler | 850f894 | 2016-02-07 23:35:31 +0200 | [diff] [blame] | 288 | return false; |
Alexander Usyskin | bb2ef9c | 2015-07-26 09:54:23 +0300 | [diff] [blame] | 289 | |
| 290 | if (!cl->notify_ev) |
Tomas Winkler | 850f894 | 2016-02-07 23:35:31 +0200 | [diff] [blame] | 291 | return false; |
Alexander Usyskin | bb2ef9c | 2015-07-26 09:54:23 +0300 | [diff] [blame] | 292 | |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 293 | schedule_work(&cldev->notif_work); |
Alexander Usyskin | bb2ef9c | 2015-07-26 09:54:23 +0300 | [diff] [blame] | 294 | |
| 295 | cl->notify_ev = false; |
Tomas Winkler | 850f894 | 2016-02-07 23:35:31 +0200 | [diff] [blame] | 296 | |
| 297 | return true; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /** |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 301 | * mei_cl_bus_rx_event - schedule rx event |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 302 | * |
| 303 | * @cl: host client |
Tomas Winkler | a1f9ae2 | 2016-02-07 23:35:30 +0200 | [diff] [blame] | 304 | * |
| 305 | * Return: true if event was scheduled |
| 306 | * false if the client is not waiting for event |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 307 | */ |
Tomas Winkler | a1f9ae2 | 2016-02-07 23:35:30 +0200 | [diff] [blame] | 308 | bool mei_cl_bus_rx_event(struct mei_cl *cl) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 309 | { |
| 310 | struct mei_cl_device *cldev = cl->cldev; |
| 311 | |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 312 | if (!cldev || !cldev->rx_cb) |
Tomas Winkler | a1f9ae2 | 2016-02-07 23:35:30 +0200 | [diff] [blame] | 313 | return false; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 314 | |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 315 | schedule_work(&cldev->rx_work); |
Tomas Winkler | a1f9ae2 | 2016-02-07 23:35:30 +0200 | [diff] [blame] | 316 | |
| 317 | return true; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /** |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 321 | * mei_cldev_register_rx_cb - register Rx event callback |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 322 | * |
| 323 | * @cldev: me client devices |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 324 | * @rx_cb: callback function |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 325 | * |
| 326 | * Return: 0 on success |
| 327 | * -EALREADY if an callback is already registered |
| 328 | * <0 on other errors |
| 329 | */ |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 330 | int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 331 | { |
Alexander Usyskin | bc46b45 | 2016-05-03 18:54:21 -0400 | [diff] [blame] | 332 | struct mei_device *bus = cldev->bus; |
Tomas Winkler | 48168f4 | 2015-07-23 15:08:38 +0300 | [diff] [blame] | 333 | int ret; |
| 334 | |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 335 | if (!rx_cb) |
| 336 | return -EINVAL; |
| 337 | if (cldev->rx_cb) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 338 | return -EALREADY; |
| 339 | |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 340 | cldev->rx_cb = rx_cb; |
| 341 | INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 342 | |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 343 | mutex_lock(&bus->device_lock); |
| 344 | ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL); |
| 345 | mutex_unlock(&bus->device_lock); |
| 346 | if (ret && ret != -EBUSY) |
| 347 | return ret; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 348 | |
| 349 | return 0; |
| 350 | } |
Alexander Usyskin | 7c7a607 | 2016-11-16 22:51:29 +0200 | [diff] [blame] | 351 | EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb); |
| 352 | |
| 353 | /** |
| 354 | * mei_cldev_register_notif_cb - register FW notification event callback |
| 355 | * |
| 356 | * @cldev: me client devices |
| 357 | * @notif_cb: callback function |
| 358 | * |
| 359 | * Return: 0 on success |
| 360 | * -EALREADY if an callback is already registered |
| 361 | * <0 on other errors |
| 362 | */ |
| 363 | int mei_cldev_register_notif_cb(struct mei_cl_device *cldev, |
| 364 | mei_cldev_cb_t notif_cb) |
| 365 | { |
| 366 | struct mei_device *bus = cldev->bus; |
| 367 | int ret; |
| 368 | |
| 369 | if (!notif_cb) |
| 370 | return -EINVAL; |
| 371 | |
| 372 | if (cldev->notif_cb) |
| 373 | return -EALREADY; |
| 374 | |
| 375 | cldev->notif_cb = notif_cb; |
| 376 | INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work); |
| 377 | |
| 378 | mutex_lock(&bus->device_lock); |
| 379 | ret = mei_cl_notify_request(cldev->cl, NULL, 1); |
| 380 | mutex_unlock(&bus->device_lock); |
| 381 | if (ret) |
| 382 | return ret; |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 387 | |
| 388 | /** |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 389 | * mei_cldev_get_drvdata - driver data getter |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 390 | * |
| 391 | * @cldev: mei client device |
| 392 | * |
| 393 | * Return: driver private data |
| 394 | */ |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 395 | void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 396 | { |
| 397 | return dev_get_drvdata(&cldev->dev); |
| 398 | } |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 399 | EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 400 | |
| 401 | /** |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 402 | * mei_cldev_set_drvdata - driver data setter |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 403 | * |
| 404 | * @cldev: mei client device |
| 405 | * @data: data to store |
| 406 | */ |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 407 | void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 408 | { |
| 409 | dev_set_drvdata(&cldev->dev, data); |
| 410 | } |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 411 | EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 412 | |
| 413 | /** |
Tomas Winkler | baeacd0 | 2015-09-10 10:18:02 +0300 | [diff] [blame] | 414 | * mei_cldev_uuid - return uuid of the underlying me client |
| 415 | * |
| 416 | * @cldev: mei client device |
| 417 | * |
| 418 | * Return: me client uuid |
| 419 | */ |
| 420 | const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev) |
| 421 | { |
| 422 | return mei_me_cl_uuid(cldev->me_cl); |
| 423 | } |
| 424 | EXPORT_SYMBOL_GPL(mei_cldev_uuid); |
| 425 | |
| 426 | /** |
| 427 | * mei_cldev_ver - return protocol version of the underlying me client |
| 428 | * |
| 429 | * @cldev: mei client device |
| 430 | * |
| 431 | * Return: me client protocol version |
| 432 | */ |
| 433 | u8 mei_cldev_ver(const struct mei_cl_device *cldev) |
| 434 | { |
| 435 | return mei_me_cl_ver(cldev->me_cl); |
| 436 | } |
| 437 | EXPORT_SYMBOL_GPL(mei_cldev_ver); |
| 438 | |
| 439 | /** |
Tomas Winkler | 01a14ed | 2015-09-10 10:18:03 +0300 | [diff] [blame] | 440 | * mei_cldev_enabled - check whether the device is enabled |
| 441 | * |
| 442 | * @cldev: mei client device |
| 443 | * |
| 444 | * Return: true if me client is initialized and connected |
| 445 | */ |
| 446 | bool mei_cldev_enabled(struct mei_cl_device *cldev) |
| 447 | { |
Alexander Usyskin | c110cdb | 2016-11-16 22:51:28 +0200 | [diff] [blame] | 448 | return mei_cl_is_connected(cldev->cl); |
Tomas Winkler | 01a14ed | 2015-09-10 10:18:03 +0300 | [diff] [blame] | 449 | } |
| 450 | EXPORT_SYMBOL_GPL(mei_cldev_enabled); |
| 451 | |
| 452 | /** |
Alexander Usyskin | 5026c9c | 2016-12-14 17:56:51 +0200 | [diff] [blame] | 453 | * mei_cldev_enable - enable me client device |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 454 | * create connection with me client |
| 455 | * |
| 456 | * @cldev: me client device |
| 457 | * |
| 458 | * Return: 0 on success and < 0 on error |
| 459 | */ |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 460 | int mei_cldev_enable(struct mei_cl_device *cldev) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 461 | { |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 462 | struct mei_device *bus = cldev->bus; |
| 463 | struct mei_cl *cl; |
| 464 | int ret; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 465 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 466 | cl = cldev->cl; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 467 | |
Alexander Usyskin | c110cdb | 2016-11-16 22:51:28 +0200 | [diff] [blame] | 468 | if (cl->state == MEI_FILE_UNINITIALIZED) { |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 469 | mutex_lock(&bus->device_lock); |
Alexander Usyskin | c110cdb | 2016-11-16 22:51:28 +0200 | [diff] [blame] | 470 | ret = mei_cl_link(cl); |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 471 | mutex_unlock(&bus->device_lock); |
Alexander Usyskin | c110cdb | 2016-11-16 22:51:28 +0200 | [diff] [blame] | 472 | if (ret) |
| 473 | return ret; |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 474 | /* update pointers */ |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 475 | cl->cldev = cldev; |
| 476 | } |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 477 | |
| 478 | mutex_lock(&bus->device_lock); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 479 | if (mei_cl_is_connected(cl)) { |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 480 | ret = 0; |
| 481 | goto out; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 482 | } |
| 483 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 484 | if (!mei_me_cl_is_active(cldev->me_cl)) { |
| 485 | dev_err(&cldev->dev, "me client is not active\n"); |
| 486 | ret = -ENOTTY; |
| 487 | goto out; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 488 | } |
| 489 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 490 | ret = mei_cl_connect(cl, cldev->me_cl, NULL); |
| 491 | if (ret < 0) |
| 492 | dev_err(&cldev->dev, "cannot connect\n"); |
| 493 | |
| 494 | out: |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 495 | mutex_unlock(&bus->device_lock); |
| 496 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 497 | return ret; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 498 | } |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 499 | EXPORT_SYMBOL_GPL(mei_cldev_enable); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 500 | |
| 501 | /** |
Alexander Usyskin | 57080e8 | 2017-01-27 16:32:40 +0200 | [diff] [blame] | 502 | * mei_cldev_unregister_callbacks - internal wrapper for unregistering |
| 503 | * callbacks. |
| 504 | * |
| 505 | * @cldev: client device |
| 506 | */ |
| 507 | static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev) |
| 508 | { |
| 509 | if (cldev->rx_cb) { |
| 510 | cancel_work_sync(&cldev->rx_work); |
| 511 | cldev->rx_cb = NULL; |
| 512 | } |
| 513 | |
| 514 | if (cldev->notif_cb) { |
| 515 | cancel_work_sync(&cldev->notif_work); |
| 516 | cldev->notif_cb = NULL; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /** |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 521 | * mei_cldev_disable - disable me client device |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 522 | * disconnect form the me client |
| 523 | * |
| 524 | * @cldev: me client device |
| 525 | * |
| 526 | * Return: 0 on success and < 0 on error |
| 527 | */ |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 528 | int mei_cldev_disable(struct mei_cl_device *cldev) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 529 | { |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 530 | struct mei_device *bus; |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 531 | struct mei_cl *cl; |
| 532 | int err; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 533 | |
Alexander Usyskin | c110cdb | 2016-11-16 22:51:28 +0200 | [diff] [blame] | 534 | if (!cldev) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 535 | return -ENODEV; |
| 536 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 537 | cl = cldev->cl; |
| 538 | |
| 539 | bus = cldev->bus; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 540 | |
Alexander Usyskin | 57080e8 | 2017-01-27 16:32:40 +0200 | [diff] [blame] | 541 | mei_cldev_unregister_callbacks(cldev); |
| 542 | |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 543 | mutex_lock(&bus->device_lock); |
| 544 | |
| 545 | if (!mei_cl_is_connected(cl)) { |
Alexander Usyskin | d882039 | 2016-10-30 01:42:19 +0200 | [diff] [blame] | 546 | dev_dbg(bus->dev, "Already disconnected"); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 547 | err = 0; |
| 548 | goto out; |
| 549 | } |
| 550 | |
| 551 | err = mei_cl_disconnect(cl); |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 552 | if (err < 0) |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 553 | dev_err(bus->dev, "Could not disconnect from the ME client"); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 554 | |
| 555 | out: |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 556 | /* Flush queues and remove any pending read */ |
| 557 | mei_cl_flush_queues(cl, NULL); |
| 558 | mei_cl_unlink(cl); |
| 559 | |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 560 | mutex_unlock(&bus->device_lock); |
| 561 | return err; |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 562 | } |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 563 | EXPORT_SYMBOL_GPL(mei_cldev_disable); |
Tomas Winkler | 6238299 | 2015-07-23 15:08:35 +0300 | [diff] [blame] | 564 | |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame] | 565 | /** |
Alexander Usyskin | 5d88246 | 2017-01-27 16:32:39 +0200 | [diff] [blame] | 566 | * mei_cl_bus_module_get - acquire module of the underlying |
| 567 | * hw module. |
| 568 | * |
| 569 | * @cl: host client |
| 570 | * |
| 571 | * Return: true on success; false if the module was removed. |
| 572 | */ |
| 573 | bool mei_cl_bus_module_get(struct mei_cl *cl) |
| 574 | { |
| 575 | struct mei_cl_device *cldev = cl->cldev; |
| 576 | |
| 577 | if (!cldev) |
| 578 | return true; |
| 579 | |
| 580 | return try_module_get(cldev->bus->dev->driver->owner); |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * mei_cl_bus_module_put - release the underlying hw module. |
| 585 | * |
| 586 | * @cl: host client |
| 587 | */ |
| 588 | void mei_cl_bus_module_put(struct mei_cl *cl) |
| 589 | { |
| 590 | struct mei_cl_device *cldev = cl->cldev; |
| 591 | |
| 592 | if (cldev) |
| 593 | module_put(cldev->bus->dev->driver->owner); |
| 594 | } |
| 595 | |
| 596 | /** |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame] | 597 | * mei_cl_device_find - find matching entry in the driver id table |
| 598 | * |
| 599 | * @cldev: me client device |
| 600 | * @cldrv: me client driver |
| 601 | * |
| 602 | * Return: id on success; NULL if no id is matching |
| 603 | */ |
| 604 | static const |
| 605 | struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev, |
| 606 | struct mei_cl_driver *cldrv) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 607 | { |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 608 | const struct mei_cl_device_id *id; |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 609 | const uuid_le *uuid; |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 610 | u8 version; |
| 611 | bool match; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 612 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 613 | uuid = mei_me_cl_uuid(cldev->me_cl); |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 614 | version = mei_me_cl_ver(cldev->me_cl); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 615 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 616 | id = cldrv->id_table; |
Greg Kroah-Hartman | b144ce2 | 2015-05-27 17:17:27 -0700 | [diff] [blame] | 617 | while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) { |
Greg Kroah-Hartman | b144ce2 | 2015-05-27 17:17:27 -0700 | [diff] [blame] | 618 | if (!uuid_le_cmp(*uuid, id->uuid)) { |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 619 | match = true; |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame] | 620 | |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 621 | if (cldev->name[0]) |
| 622 | if (strncmp(cldev->name, id->name, |
| 623 | sizeof(id->name))) |
| 624 | match = false; |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame] | 625 | |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 626 | if (id->version != MEI_CL_VERSION_ANY) |
| 627 | if (id->version != version) |
| 628 | match = false; |
| 629 | if (match) |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame] | 630 | return id; |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 631 | } |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 632 | |
| 633 | id++; |
| 634 | } |
| 635 | |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame] | 636 | return NULL; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * mei_cl_device_match - device match function |
| 641 | * |
| 642 | * @dev: device |
| 643 | * @drv: driver |
| 644 | * |
| 645 | * Return: 1 if matching device was found 0 otherwise |
| 646 | */ |
| 647 | static int mei_cl_device_match(struct device *dev, struct device_driver *drv) |
| 648 | { |
| 649 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 650 | struct mei_cl_driver *cldrv = to_mei_cl_driver(drv); |
| 651 | const struct mei_cl_device_id *found_id; |
| 652 | |
| 653 | if (!cldev) |
| 654 | return 0; |
| 655 | |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 656 | if (!cldev->do_match) |
| 657 | return 0; |
| 658 | |
Tomas Winkler | 688a9cc | 2015-07-23 15:08:39 +0300 | [diff] [blame] | 659 | if (!cldrv || !cldrv->id_table) |
| 660 | return 0; |
| 661 | |
| 662 | found_id = mei_cl_device_find(cldev, cldrv); |
| 663 | if (found_id) |
| 664 | return 1; |
| 665 | |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 666 | return 0; |
| 667 | } |
| 668 | |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 669 | /** |
| 670 | * mei_cl_device_probe - bus probe function |
| 671 | * |
| 672 | * @dev: device |
| 673 | * |
| 674 | * Return: 0 on success; < 0 otherwise |
| 675 | */ |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 676 | static int mei_cl_device_probe(struct device *dev) |
| 677 | { |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 678 | struct mei_cl_device *cldev; |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 679 | struct mei_cl_driver *cldrv; |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 680 | const struct mei_cl_device_id *id; |
Alexey Khoroshilov | b9c7954 | 2016-04-01 23:53:01 +0300 | [diff] [blame] | 681 | int ret; |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 682 | |
| 683 | cldev = to_mei_cl_device(dev); |
| 684 | cldrv = to_mei_cl_driver(dev->driver); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 685 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 686 | if (!cldev) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 687 | return 0; |
| 688 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 689 | if (!cldrv || !cldrv->probe) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 690 | return -ENODEV; |
| 691 | |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 692 | id = mei_cl_device_find(cldev, cldrv); |
| 693 | if (!id) |
| 694 | return -ENODEV; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 695 | |
Alexey Khoroshilov | b9c7954 | 2016-04-01 23:53:01 +0300 | [diff] [blame] | 696 | ret = cldrv->probe(cldev, id); |
| 697 | if (ret) |
| 698 | return ret; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 699 | |
Alexey Khoroshilov | b9c7954 | 2016-04-01 23:53:01 +0300 | [diff] [blame] | 700 | __module_get(THIS_MODULE); |
| 701 | return 0; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 702 | } |
| 703 | |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 704 | /** |
| 705 | * mei_cl_device_remove - remove device from the bus |
| 706 | * |
| 707 | * @dev: device |
| 708 | * |
| 709 | * Return: 0 on success; < 0 otherwise |
| 710 | */ |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 711 | static int mei_cl_device_remove(struct device *dev) |
| 712 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 713 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 714 | struct mei_cl_driver *cldrv; |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 715 | int ret = 0; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 716 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 717 | if (!cldev || !dev->driver) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 718 | return 0; |
| 719 | |
Alexander Usyskin | 9ecb839 | 2017-01-27 16:32:38 +0200 | [diff] [blame] | 720 | cldrv = to_mei_cl_driver(dev->driver); |
| 721 | if (cldrv->remove) |
| 722 | ret = cldrv->remove(cldev); |
| 723 | |
Alexander Usyskin | 57080e8 | 2017-01-27 16:32:40 +0200 | [diff] [blame] | 724 | mei_cldev_unregister_callbacks(cldev); |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 725 | |
Tomas Winkler | feb8cd0 | 2015-07-23 15:08:40 +0300 | [diff] [blame] | 726 | module_put(THIS_MODULE); |
| 727 | dev->driver = NULL; |
| 728 | return ret; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 729 | |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 730 | } |
| 731 | |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 732 | static ssize_t name_show(struct device *dev, struct device_attribute *a, |
| 733 | char *buf) |
| 734 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 735 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 736 | |
Rasmus Villemoes | 423de92 | 2016-03-24 10:38:04 +0200 | [diff] [blame] | 737 | return scnprintf(buf, PAGE_SIZE, "%s", cldev->name); |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 738 | } |
| 739 | static DEVICE_ATTR_RO(name); |
| 740 | |
| 741 | static ssize_t uuid_show(struct device *dev, struct device_attribute *a, |
| 742 | char *buf) |
| 743 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 744 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 745 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 746 | |
Rasmus Villemoes | 423de92 | 2016-03-24 10:38:04 +0200 | [diff] [blame] | 747 | return scnprintf(buf, PAGE_SIZE, "%pUl", uuid); |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 748 | } |
| 749 | static DEVICE_ATTR_RO(uuid); |
| 750 | |
Tomas Winkler | 40b7320 | 2015-09-10 10:18:00 +0300 | [diff] [blame] | 751 | static ssize_t version_show(struct device *dev, struct device_attribute *a, |
| 752 | char *buf) |
| 753 | { |
| 754 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 755 | u8 version = mei_me_cl_ver(cldev->me_cl); |
Tomas Winkler | 40b7320 | 2015-09-10 10:18:00 +0300 | [diff] [blame] | 756 | |
Rasmus Villemoes | 423de92 | 2016-03-24 10:38:04 +0200 | [diff] [blame] | 757 | return scnprintf(buf, PAGE_SIZE, "%02X", version); |
Tomas Winkler | 40b7320 | 2015-09-10 10:18:00 +0300 | [diff] [blame] | 758 | } |
| 759 | static DEVICE_ATTR_RO(version); |
| 760 | |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 761 | static ssize_t modalias_show(struct device *dev, struct device_attribute *a, |
| 762 | char *buf) |
| 763 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 764 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 765 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 766 | |
Rasmus Villemoes | 423de92 | 2016-03-24 10:38:04 +0200 | [diff] [blame] | 767 | return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:", cldev->name, uuid); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 768 | } |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 769 | static DEVICE_ATTR_RO(modalias); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 770 | |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 771 | static struct attribute *mei_cldev_attrs[] = { |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 772 | &dev_attr_name.attr, |
| 773 | &dev_attr_uuid.attr, |
Tomas Winkler | 40b7320 | 2015-09-10 10:18:00 +0300 | [diff] [blame] | 774 | &dev_attr_version.attr, |
Greg Kroah-Hartman | 32f389ec | 2013-08-23 14:24:39 -0700 | [diff] [blame] | 775 | &dev_attr_modalias.attr, |
| 776 | NULL, |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 777 | }; |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 778 | ATTRIBUTE_GROUPS(mei_cldev); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 779 | |
Tomas Winkler | 38d3c00 | 2015-07-23 15:08:36 +0300 | [diff] [blame] | 780 | /** |
| 781 | * mei_cl_device_uevent - me client bus uevent handler |
| 782 | * |
| 783 | * @dev: device |
| 784 | * @env: uevent kobject |
| 785 | * |
| 786 | * Return: 0 on success -ENOMEM on when add_uevent_var fails |
| 787 | */ |
| 788 | 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] | 789 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 790 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
| 791 | const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl); |
Tomas Winkler | 40b7320 | 2015-09-10 10:18:00 +0300 | [diff] [blame] | 792 | u8 version = mei_me_cl_ver(cldev->me_cl); |
| 793 | |
| 794 | if (add_uevent_var(env, "MEI_CL_VERSION=%d", version)) |
| 795 | return -ENOMEM; |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 796 | |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 797 | if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid)) |
| 798 | return -ENOMEM; |
| 799 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 800 | if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name)) |
Tomas Winkler | 007d64e | 2015-05-07 15:54:03 +0300 | [diff] [blame] | 801 | return -ENOMEM; |
| 802 | |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 803 | if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:", |
| 804 | cldev->name, uuid, version)) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 805 | return -ENOMEM; |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | static struct bus_type mei_cl_bus_type = { |
| 811 | .name = "mei", |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 812 | .dev_groups = mei_cldev_groups, |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 813 | .match = mei_cl_device_match, |
| 814 | .probe = mei_cl_device_probe, |
| 815 | .remove = mei_cl_device_remove, |
Tomas Winkler | 38d3c00 | 2015-07-23 15:08:36 +0300 | [diff] [blame] | 816 | .uevent = mei_cl_device_uevent, |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 817 | }; |
| 818 | |
Tomas Winkler | 512f64d | 2015-07-23 15:08:41 +0300 | [diff] [blame] | 819 | static struct mei_device *mei_dev_bus_get(struct mei_device *bus) |
| 820 | { |
| 821 | if (bus) |
| 822 | get_device(bus->dev); |
| 823 | |
| 824 | return bus; |
| 825 | } |
| 826 | |
| 827 | static void mei_dev_bus_put(struct mei_device *bus) |
| 828 | { |
| 829 | if (bus) |
| 830 | put_device(bus->dev); |
| 831 | } |
| 832 | |
Tomas Winkler | ae48d74 | 2015-09-10 10:18:06 +0300 | [diff] [blame] | 833 | static void mei_cl_bus_dev_release(struct device *dev) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 834 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 835 | struct mei_cl_device *cldev = to_mei_cl_device(dev); |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 836 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 837 | if (!cldev) |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 838 | return; |
| 839 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 840 | mei_me_cl_put(cldev->me_cl); |
Tomas Winkler | 512f64d | 2015-07-23 15:08:41 +0300 | [diff] [blame] | 841 | mei_dev_bus_put(cldev->bus); |
Alexander Usyskin | c110cdb | 2016-11-16 22:51:28 +0200 | [diff] [blame] | 842 | kfree(cldev->cl); |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 843 | kfree(cldev); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static struct device_type mei_cl_device_type = { |
Tomas Winkler | ae48d74 | 2015-09-10 10:18:06 +0300 | [diff] [blame] | 847 | .release = mei_cl_bus_dev_release, |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 848 | }; |
| 849 | |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 850 | /** |
Tomas Winkler | 213dd19 | 2015-10-28 14:34:35 +0200 | [diff] [blame] | 851 | * mei_cl_bus_set_name - set device name for me client device |
| 852 | * |
| 853 | * @cldev: me client device |
| 854 | */ |
| 855 | static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev) |
| 856 | { |
| 857 | dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X", |
| 858 | cldev->name, |
| 859 | mei_me_cl_uuid(cldev->me_cl), |
| 860 | mei_me_cl_ver(cldev->me_cl)); |
| 861 | } |
| 862 | |
| 863 | /** |
Tomas Winkler | ae48d74 | 2015-09-10 10:18:06 +0300 | [diff] [blame] | 864 | * mei_cl_bus_dev_alloc - initialize and allocate mei client device |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 865 | * |
| 866 | * @bus: mei device |
| 867 | * @me_cl: me client |
| 868 | * |
| 869 | * Return: allocated device structur or NULL on allocation failure |
| 870 | */ |
Tomas Winkler | ae48d74 | 2015-09-10 10:18:06 +0300 | [diff] [blame] | 871 | static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus, |
| 872 | struct mei_me_client *me_cl) |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 873 | { |
| 874 | struct mei_cl_device *cldev; |
Alexander Usyskin | c110cdb | 2016-11-16 22:51:28 +0200 | [diff] [blame] | 875 | struct mei_cl *cl; |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 876 | |
| 877 | cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL); |
| 878 | if (!cldev) |
| 879 | return NULL; |
| 880 | |
Alexander Usyskin | c110cdb | 2016-11-16 22:51:28 +0200 | [diff] [blame] | 881 | cl = mei_cl_allocate(bus); |
| 882 | if (!cl) { |
| 883 | kfree(cldev); |
| 884 | return NULL; |
| 885 | } |
| 886 | |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 887 | device_initialize(&cldev->dev); |
| 888 | cldev->dev.parent = bus->dev; |
| 889 | cldev->dev.bus = &mei_cl_bus_type; |
| 890 | cldev->dev.type = &mei_cl_device_type; |
| 891 | cldev->bus = mei_dev_bus_get(bus); |
| 892 | cldev->me_cl = mei_me_cl_get(me_cl); |
Alexander Usyskin | c110cdb | 2016-11-16 22:51:28 +0200 | [diff] [blame] | 893 | cldev->cl = cl; |
Tomas Winkler | 213dd19 | 2015-10-28 14:34:35 +0200 | [diff] [blame] | 894 | mei_cl_bus_set_name(cldev); |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 895 | cldev->is_added = 0; |
| 896 | INIT_LIST_HEAD(&cldev->bus_list); |
| 897 | |
| 898 | return cldev; |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * mei_cl_dev_setup - setup me client device |
| 903 | * run fix up routines and set the device name |
| 904 | * |
| 905 | * @bus: mei device |
| 906 | * @cldev: me client device |
| 907 | * |
| 908 | * Return: true if the device is eligible for enumeration |
| 909 | */ |
Tomas Winkler | ae48d74 | 2015-09-10 10:18:06 +0300 | [diff] [blame] | 910 | static bool mei_cl_bus_dev_setup(struct mei_device *bus, |
| 911 | struct mei_cl_device *cldev) |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 912 | { |
| 913 | cldev->do_match = 1; |
Tomas Winkler | ae48d74 | 2015-09-10 10:18:06 +0300 | [diff] [blame] | 914 | mei_cl_bus_dev_fixup(cldev); |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 915 | |
Tomas Winkler | 213dd19 | 2015-10-28 14:34:35 +0200 | [diff] [blame] | 916 | /* the device name can change during fix up */ |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 917 | if (cldev->do_match) |
Tomas Winkler | 213dd19 | 2015-10-28 14:34:35 +0200 | [diff] [blame] | 918 | mei_cl_bus_set_name(cldev); |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 919 | |
| 920 | return cldev->do_match == 1; |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * mei_cl_bus_dev_add - add me client devices |
| 925 | * |
| 926 | * @cldev: me client device |
| 927 | * |
| 928 | * Return: 0 on success; < 0 on failre |
| 929 | */ |
| 930 | static int mei_cl_bus_dev_add(struct mei_cl_device *cldev) |
| 931 | { |
| 932 | int ret; |
| 933 | |
Tomas Winkler | b26864c | 2015-09-10 10:18:01 +0300 | [diff] [blame] | 934 | dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n", |
| 935 | mei_me_cl_uuid(cldev->me_cl), |
| 936 | mei_me_cl_ver(cldev->me_cl)); |
Tomas Winkler | 71ce789 | 2015-07-23 15:08:43 +0300 | [diff] [blame] | 937 | ret = device_add(&cldev->dev); |
| 938 | if (!ret) |
| 939 | cldev->is_added = 1; |
| 940 | |
| 941 | return ret; |
| 942 | } |
| 943 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 944 | /** |
| 945 | * mei_cl_bus_dev_stop - stop the driver |
| 946 | * |
| 947 | * @cldev: me client device |
| 948 | */ |
| 949 | static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev) |
| 950 | { |
| 951 | if (cldev->is_added) |
| 952 | device_release_driver(&cldev->dev); |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * mei_cl_bus_dev_destroy - destroy me client devices object |
| 957 | * |
| 958 | * @cldev: me client device |
Tomas Winkler | 2da55cf | 2015-10-28 14:34:34 +0200 | [diff] [blame] | 959 | * |
| 960 | * Locking: called under "dev->cl_bus_lock" lock |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 961 | */ |
| 962 | static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev) |
| 963 | { |
Tomas Winkler | 2da55cf | 2015-10-28 14:34:34 +0200 | [diff] [blame] | 964 | |
| 965 | WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock)); |
| 966 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 967 | if (!cldev->is_added) |
| 968 | return; |
| 969 | |
| 970 | device_del(&cldev->dev); |
| 971 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 972 | list_del_init(&cldev->bus_list); |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 973 | |
| 974 | cldev->is_added = 0; |
| 975 | put_device(&cldev->dev); |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * mei_cl_bus_remove_device - remove a devices form the bus |
| 980 | * |
| 981 | * @cldev: me client device |
| 982 | */ |
| 983 | static void mei_cl_bus_remove_device(struct mei_cl_device *cldev) |
| 984 | { |
| 985 | mei_cl_bus_dev_stop(cldev); |
| 986 | mei_cl_bus_dev_destroy(cldev); |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * mei_cl_bus_remove_devices - remove all devices form the bus |
| 991 | * |
| 992 | * @bus: mei device |
| 993 | */ |
| 994 | void mei_cl_bus_remove_devices(struct mei_device *bus) |
| 995 | { |
| 996 | struct mei_cl_device *cldev, *next; |
| 997 | |
Tomas Winkler | 2da55cf | 2015-10-28 14:34:34 +0200 | [diff] [blame] | 998 | mutex_lock(&bus->cl_bus_lock); |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 999 | list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list) |
| 1000 | mei_cl_bus_remove_device(cldev); |
Tomas Winkler | 2da55cf | 2015-10-28 14:34:34 +0200 | [diff] [blame] | 1001 | mutex_unlock(&bus->cl_bus_lock); |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | /** |
Tomas Winkler | ae48d74 | 2015-09-10 10:18:06 +0300 | [diff] [blame] | 1006 | * mei_cl_bus_dev_init - allocate and initializes an mei client devices |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 1007 | * based on me client |
| 1008 | * |
| 1009 | * @bus: mei device |
| 1010 | * @me_cl: me client |
Tomas Winkler | 2da55cf | 2015-10-28 14:34:34 +0200 | [diff] [blame] | 1011 | * |
| 1012 | * Locking: called under "dev->cl_bus_lock" lock |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 1013 | */ |
Tomas Winkler | ae48d74 | 2015-09-10 10:18:06 +0300 | [diff] [blame] | 1014 | static void mei_cl_bus_dev_init(struct mei_device *bus, |
| 1015 | struct mei_me_client *me_cl) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 1016 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 1017 | struct mei_cl_device *cldev; |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 1018 | |
Tomas Winkler | 2da55cf | 2015-10-28 14:34:34 +0200 | [diff] [blame] | 1019 | WARN_ON(!mutex_is_locked(&bus->cl_bus_lock)); |
| 1020 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 1021 | dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl)); |
| 1022 | |
| 1023 | if (me_cl->bus_added) |
| 1024 | return; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 1025 | |
Tomas Winkler | ae48d74 | 2015-09-10 10:18:06 +0300 | [diff] [blame] | 1026 | cldev = mei_cl_bus_dev_alloc(bus, me_cl); |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 1027 | if (!cldev) |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 1028 | return; |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 1029 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 1030 | me_cl->bus_added = true; |
| 1031 | list_add_tail(&cldev->bus_list, &bus->device_list); |
Tomas Winkler | c93b76b | 2015-05-07 15:54:02 +0300 | [diff] [blame] | 1032 | |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 1033 | } |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 1034 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 1035 | /** |
| 1036 | * mei_cl_bus_rescan - scan me clients list and add create |
| 1037 | * devices for eligible clients |
| 1038 | * |
| 1039 | * @bus: mei device |
| 1040 | */ |
| 1041 | void mei_cl_bus_rescan(struct mei_device *bus) |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 1042 | { |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 1043 | struct mei_cl_device *cldev, *n; |
| 1044 | struct mei_me_client *me_cl; |
| 1045 | |
Tomas Winkler | 2da55cf | 2015-10-28 14:34:34 +0200 | [diff] [blame] | 1046 | mutex_lock(&bus->cl_bus_lock); |
| 1047 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 1048 | down_read(&bus->me_clients_rwsem); |
| 1049 | list_for_each_entry(me_cl, &bus->me_clients, list) |
Tomas Winkler | ae48d74 | 2015-09-10 10:18:06 +0300 | [diff] [blame] | 1050 | mei_cl_bus_dev_init(bus, me_cl); |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 1051 | up_read(&bus->me_clients_rwsem); |
| 1052 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 1053 | list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) { |
| 1054 | |
| 1055 | if (!mei_me_cl_is_active(cldev->me_cl)) { |
| 1056 | mei_cl_bus_remove_device(cldev); |
| 1057 | continue; |
| 1058 | } |
| 1059 | |
| 1060 | if (cldev->is_added) |
| 1061 | continue; |
| 1062 | |
Tomas Winkler | ae48d74 | 2015-09-10 10:18:06 +0300 | [diff] [blame] | 1063 | if (mei_cl_bus_dev_setup(bus, cldev)) |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 1064 | mei_cl_bus_dev_add(cldev); |
| 1065 | else { |
| 1066 | list_del_init(&cldev->bus_list); |
| 1067 | put_device(&cldev->dev); |
| 1068 | } |
| 1069 | } |
| 1070 | mutex_unlock(&bus->cl_bus_lock); |
| 1071 | |
| 1072 | dev_dbg(bus->dev, "rescan end"); |
Samuel Ortiz | e535410 | 2013-03-27 17:29:53 +0200 | [diff] [blame] | 1073 | } |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 1074 | |
Alexander Usyskin | a816a00 | 2016-02-07 23:35:41 +0200 | [diff] [blame] | 1075 | void mei_cl_bus_rescan_work(struct work_struct *work) |
| 1076 | { |
| 1077 | struct mei_device *bus = |
| 1078 | container_of(work, struct mei_device, bus_rescan_work); |
| 1079 | |
| 1080 | mei_cl_bus_rescan(bus); |
| 1081 | } |
| 1082 | |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 1083 | int __mei_cldev_driver_register(struct mei_cl_driver *cldrv, |
| 1084 | struct module *owner) |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 1085 | { |
| 1086 | int err; |
| 1087 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 1088 | cldrv->driver.name = cldrv->name; |
| 1089 | cldrv->driver.owner = owner; |
| 1090 | cldrv->driver.bus = &mei_cl_bus_type; |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 1091 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 1092 | err = driver_register(&cldrv->driver); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 1093 | if (err) |
| 1094 | return err; |
| 1095 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 1096 | pr_debug("mei: driver [%s] registered\n", cldrv->driver.name); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 1097 | |
| 1098 | return 0; |
| 1099 | } |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 1100 | EXPORT_SYMBOL_GPL(__mei_cldev_driver_register); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 1101 | |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 1102 | void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv) |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 1103 | { |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 1104 | driver_unregister(&cldrv->driver); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 1105 | |
Tomas Winkler | b37719c3 | 2015-07-23 15:08:33 +0300 | [diff] [blame] | 1106 | pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name); |
Samuel Ortiz | 333e4ee | 2013-03-27 17:29:54 +0200 | [diff] [blame] | 1107 | } |
Tomas Winkler | d49dc5e | 2015-09-10 10:18:05 +0300 | [diff] [blame] | 1108 | EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister); |
Samuel Ortiz | 3e83329 | 2013-03-27 17:29:55 +0200 | [diff] [blame] | 1109 | |
Tomas Winkler | 6009595 | 2015-07-23 15:08:47 +0300 | [diff] [blame] | 1110 | |
Samuel Ortiz | cf3baef | 2013-03-27 17:29:57 +0200 | [diff] [blame] | 1111 | int __init mei_cl_bus_init(void) |
| 1112 | { |
| 1113 | return bus_register(&mei_cl_bus_type); |
| 1114 | } |
| 1115 | |
| 1116 | void __exit mei_cl_bus_exit(void) |
| 1117 | { |
| 1118 | bus_unregister(&mei_cl_bus_type); |
| 1119 | } |