Commit | Line | Data |
---|---|---|
ab841160 OW |
1 | /* |
2 | * | |
3 | * Intel Management Engine Interface (Intel MEI) Linux driver | |
733ba91c | 4 | * Copyright (c) 2003-2012, Intel Corporation. |
ab841160 OW |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms and conditions of the GNU General Public License, | |
8 | * version 2, as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | */ | |
ab841160 OW |
16 | #include <linux/module.h> |
17 | #include <linux/moduleparam.h> | |
18 | #include <linux/kernel.h> | |
19 | #include <linux/device.h> | |
1f180359 | 20 | #include <linux/slab.h> |
ab841160 OW |
21 | #include <linux/fs.h> |
22 | #include <linux/errno.h> | |
23 | #include <linux/types.h> | |
24 | #include <linux/fcntl.h> | |
25 | #include <linux/aio.h> | |
ab841160 OW |
26 | #include <linux/poll.h> |
27 | #include <linux/init.h> | |
28 | #include <linux/ioctl.h> | |
29 | #include <linux/cdev.h> | |
ab841160 OW |
30 | #include <linux/sched.h> |
31 | #include <linux/uuid.h> | |
32 | #include <linux/compat.h> | |
33 | #include <linux/jiffies.h> | |
34 | #include <linux/interrupt.h> | |
35 | ||
4f3afe1d | 36 | #include <linux/mei.h> |
47a73801 TW |
37 | |
38 | #include "mei_dev.h" | |
90e0b5f1 | 39 | #include "client.h" |
ab841160 | 40 | |
ab841160 OW |
41 | /** |
42 | * mei_open - the open function | |
43 | * | |
44 | * @inode: pointer to inode structure | |
45 | * @file: pointer to file structure | |
83ce0741 | 46 | * |
a8605ea2 | 47 | * Return: 0 on success, <0 on error |
ab841160 OW |
48 | */ |
49 | static int mei_open(struct inode *inode, struct file *file) | |
50 | { | |
ab841160 | 51 | struct mei_device *dev; |
f3d8e878 | 52 | struct mei_cl *cl; |
2703d4b2 | 53 | |
6f37aca8 | 54 | int err; |
ab841160 | 55 | |
f3d8e878 | 56 | dev = container_of(inode->i_cdev, struct mei_device, cdev); |
5b881e3c | 57 | if (!dev) |
e036cc57 | 58 | return -ENODEV; |
ab841160 OW |
59 | |
60 | mutex_lock(&dev->device_lock); | |
e036cc57 TW |
61 | |
62 | cl = NULL; | |
ab841160 OW |
63 | |
64 | err = -ENODEV; | |
b210d750 | 65 | if (dev->dev_state != MEI_DEV_ENABLED) { |
2bf94cab | 66 | dev_dbg(dev->dev, "dev_state != MEI_ENABLED dev_state = %s\n", |
b210d750 | 67 | mei_dev_state_str(dev->dev_state)); |
e036cc57 | 68 | goto err_unlock; |
1b812947 | 69 | } |
ab841160 | 70 | |
e036cc57 TW |
71 | err = -ENOMEM; |
72 | cl = mei_cl_allocate(dev); | |
73 | if (!cl) | |
74 | goto err_unlock; | |
75 | ||
76 | /* open_handle_count check is handled in the mei_cl_link */ | |
781d0d89 TW |
77 | err = mei_cl_link(cl, MEI_HOST_CLIENT_ID_ANY); |
78 | if (err) | |
e036cc57 | 79 | goto err_unlock; |
ab841160 OW |
80 | |
81 | file->private_data = cl; | |
e036cc57 | 82 | |
ab841160 OW |
83 | mutex_unlock(&dev->device_lock); |
84 | ||
5b881e3c | 85 | return nonseekable_open(inode, file); |
ab841160 | 86 | |
e036cc57 | 87 | err_unlock: |
ab841160 OW |
88 | mutex_unlock(&dev->device_lock); |
89 | kfree(cl); | |
ab841160 OW |
90 | return err; |
91 | } | |
92 | ||
93 | /** | |
94 | * mei_release - the release function | |
95 | * | |
96 | * @inode: pointer to inode structure | |
97 | * @file: pointer to file structure | |
98 | * | |
a8605ea2 | 99 | * Return: 0 on success, <0 on error |
ab841160 OW |
100 | */ |
101 | static int mei_release(struct inode *inode, struct file *file) | |
102 | { | |
103 | struct mei_cl *cl = file->private_data; | |
104 | struct mei_cl_cb *cb; | |
105 | struct mei_device *dev; | |
106 | int rets = 0; | |
107 | ||
108 | if (WARN_ON(!cl || !cl->dev)) | |
109 | return -ENODEV; | |
110 | ||
111 | dev = cl->dev; | |
112 | ||
113 | mutex_lock(&dev->device_lock); | |
a562d5c2 TW |
114 | if (cl == &dev->iamthif_cl) { |
115 | rets = mei_amthif_release(dev, file); | |
116 | goto out; | |
117 | } | |
118 | if (cl->state == MEI_FILE_CONNECTED) { | |
119 | cl->state = MEI_FILE_DISCONNECTING; | |
46922186 | 120 | cl_dbg(dev, cl, "disconnecting\n"); |
90e0b5f1 | 121 | rets = mei_cl_disconnect(cl); |
a562d5c2 TW |
122 | } |
123 | mei_cl_flush_queues(cl); | |
46922186 | 124 | cl_dbg(dev, cl, "removing\n"); |
a562d5c2 | 125 | |
90e0b5f1 | 126 | mei_cl_unlink(cl); |
a562d5c2 | 127 | |
781d0d89 | 128 | |
a562d5c2 TW |
129 | /* free read cb */ |
130 | cb = NULL; | |
131 | if (cl->read_cb) { | |
90e0b5f1 | 132 | cb = mei_cl_find_read_cb(cl); |
a562d5c2 TW |
133 | /* Remove entry from read list */ |
134 | if (cb) | |
135 | list_del(&cb->list); | |
136 | ||
137 | cb = cl->read_cb; | |
138 | cl->read_cb = NULL; | |
139 | } | |
ab841160 | 140 | |
a562d5c2 | 141 | file->private_data = NULL; |
ab841160 | 142 | |
a9c8a17a | 143 | mei_io_cb_free(cb); |
a562d5c2 TW |
144 | |
145 | kfree(cl); | |
146 | out: | |
ab841160 OW |
147 | mutex_unlock(&dev->device_lock); |
148 | return rets; | |
149 | } | |
150 | ||
151 | ||
152 | /** | |
153 | * mei_read - the read function. | |
154 | * | |
155 | * @file: pointer to file structure | |
156 | * @ubuf: pointer to user buffer | |
157 | * @length: buffer length | |
158 | * @offset: data offset in buffer | |
159 | * | |
a8605ea2 | 160 | * Return: >=0 data length on success , <0 on error |
ab841160 OW |
161 | */ |
162 | static ssize_t mei_read(struct file *file, char __user *ubuf, | |
441ab50f | 163 | size_t length, loff_t *offset) |
ab841160 OW |
164 | { |
165 | struct mei_cl *cl = file->private_data; | |
166 | struct mei_cl_cb *cb_pos = NULL; | |
167 | struct mei_cl_cb *cb = NULL; | |
168 | struct mei_device *dev; | |
ab841160 OW |
169 | int rets; |
170 | int err; | |
171 | ||
172 | ||
173 | if (WARN_ON(!cl || !cl->dev)) | |
174 | return -ENODEV; | |
175 | ||
176 | dev = cl->dev; | |
177 | ||
dd5de1f1 | 178 | |
ab841160 | 179 | mutex_lock(&dev->device_lock); |
b210d750 | 180 | if (dev->dev_state != MEI_DEV_ENABLED) { |
ab841160 OW |
181 | rets = -ENODEV; |
182 | goto out; | |
183 | } | |
184 | ||
dd5de1f1 TW |
185 | if (length == 0) { |
186 | rets = 0; | |
187 | goto out; | |
188 | } | |
189 | ||
ab841160 | 190 | if (cl == &dev->iamthif_cl) { |
19838fb8 | 191 | rets = mei_amthif_read(dev, file, ubuf, length, offset); |
ab841160 OW |
192 | goto out; |
193 | } | |
194 | ||
139aacf7 | 195 | if (cl->read_cb) { |
ab841160 | 196 | cb = cl->read_cb; |
139aacf7 TW |
197 | /* read what left */ |
198 | if (cb->buf_idx > *offset) | |
199 | goto copy_buffer; | |
200 | /* offset is beyond buf_idx we have no more data return 0 */ | |
201 | if (cb->buf_idx > 0 && cb->buf_idx <= *offset) { | |
202 | rets = 0; | |
203 | goto free; | |
204 | } | |
205 | /* Offset needs to be cleaned for contiguous reads*/ | |
206 | if (cb->buf_idx == 0 && *offset > 0) | |
207 | *offset = 0; | |
208 | } else if (*offset > 0) { | |
ab841160 | 209 | *offset = 0; |
ab841160 OW |
210 | } |
211 | ||
fcb136e1 | 212 | err = mei_cl_read_start(cl, length); |
ab841160 | 213 | if (err && err != -EBUSY) { |
2bf94cab | 214 | dev_dbg(dev->dev, |
ab841160 OW |
215 | "mei start read failure with status = %d\n", err); |
216 | rets = err; | |
217 | goto out; | |
218 | } | |
219 | ||
220 | if (MEI_READ_COMPLETE != cl->reading_state && | |
221 | !waitqueue_active(&cl->rx_wait)) { | |
222 | if (file->f_flags & O_NONBLOCK) { | |
223 | rets = -EAGAIN; | |
224 | goto out; | |
225 | } | |
226 | ||
227 | mutex_unlock(&dev->device_lock); | |
228 | ||
229 | if (wait_event_interruptible(cl->rx_wait, | |
e2b31644 TW |
230 | MEI_READ_COMPLETE == cl->reading_state || |
231 | mei_cl_is_transitioning(cl))) { | |
232 | ||
ab841160 OW |
233 | if (signal_pending(current)) |
234 | return -EINTR; | |
235 | return -ERESTARTSYS; | |
236 | } | |
237 | ||
238 | mutex_lock(&dev->device_lock); | |
e2b31644 | 239 | if (mei_cl_is_transitioning(cl)) { |
ab841160 OW |
240 | rets = -EBUSY; |
241 | goto out; | |
242 | } | |
243 | } | |
244 | ||
245 | cb = cl->read_cb; | |
246 | ||
247 | if (!cb) { | |
248 | rets = -ENODEV; | |
249 | goto out; | |
250 | } | |
251 | if (cl->reading_state != MEI_READ_COMPLETE) { | |
252 | rets = 0; | |
253 | goto out; | |
254 | } | |
255 | /* now copy the data to user space */ | |
256 | copy_buffer: | |
2bf94cab | 257 | dev_dbg(dev->dev, "buf.size = %d buf.idx= %ld\n", |
fcb136e1 | 258 | cb->response_buffer.size, cb->buf_idx); |
ebb108ef | 259 | if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) { |
ab841160 OW |
260 | rets = -EMSGSIZE; |
261 | goto free; | |
262 | } | |
263 | ||
ebb108ef TW |
264 | /* length is being truncated to PAGE_SIZE, |
265 | * however buf_idx may point beyond that */ | |
266 | length = min_t(size_t, length, cb->buf_idx - *offset); | |
ab841160 | 267 | |
441ab50f | 268 | if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) { |
2bf94cab | 269 | dev_dbg(dev->dev, "failed to copy data to userland\n"); |
ab841160 OW |
270 | rets = -EFAULT; |
271 | goto free; | |
272 | } | |
273 | ||
274 | rets = length; | |
275 | *offset += length; | |
ebb108ef | 276 | if ((unsigned long)*offset < cb->buf_idx) |
ab841160 OW |
277 | goto out; |
278 | ||
279 | free: | |
90e0b5f1 | 280 | cb_pos = mei_cl_find_read_cb(cl); |
ab841160 OW |
281 | /* Remove entry from read list */ |
282 | if (cb_pos) | |
fb601adb | 283 | list_del(&cb_pos->list); |
601a1efa | 284 | mei_io_cb_free(cb); |
ab841160 OW |
285 | cl->reading_state = MEI_IDLE; |
286 | cl->read_cb = NULL; | |
ab841160 | 287 | out: |
2bf94cab | 288 | dev_dbg(dev->dev, "end mei read rets= %d\n", rets); |
ab841160 OW |
289 | mutex_unlock(&dev->device_lock); |
290 | return rets; | |
291 | } | |
ab841160 OW |
292 | /** |
293 | * mei_write - the write function. | |
294 | * | |
295 | * @file: pointer to file structure | |
296 | * @ubuf: pointer to user buffer | |
297 | * @length: buffer length | |
298 | * @offset: data offset in buffer | |
299 | * | |
a8605ea2 | 300 | * Return: >=0 data length on success , <0 on error |
ab841160 OW |
301 | */ |
302 | static ssize_t mei_write(struct file *file, const char __user *ubuf, | |
441ab50f | 303 | size_t length, loff_t *offset) |
ab841160 OW |
304 | { |
305 | struct mei_cl *cl = file->private_data; | |
d320832f | 306 | struct mei_me_client *me_cl; |
ab841160 | 307 | struct mei_cl_cb *write_cb = NULL; |
ab841160 OW |
308 | struct mei_device *dev; |
309 | unsigned long timeout = 0; | |
310 | int rets; | |
ab841160 OW |
311 | |
312 | if (WARN_ON(!cl || !cl->dev)) | |
313 | return -ENODEV; | |
314 | ||
315 | dev = cl->dev; | |
316 | ||
317 | mutex_lock(&dev->device_lock); | |
318 | ||
b210d750 | 319 | if (dev->dev_state != MEI_DEV_ENABLED) { |
75f0ee15 | 320 | rets = -ENODEV; |
4234a6de | 321 | goto out; |
ab841160 OW |
322 | } |
323 | ||
d880f329 | 324 | me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id); |
d320832f | 325 | if (!me_cl) { |
7ca96aa2 | 326 | rets = -ENOTTY; |
4234a6de | 327 | goto out; |
75f0ee15 | 328 | } |
dd5de1f1 TW |
329 | |
330 | if (length == 0) { | |
331 | rets = 0; | |
332 | goto out; | |
333 | } | |
334 | ||
d320832f | 335 | if (length > me_cl->props.max_msg_length) { |
dd5de1f1 | 336 | rets = -EFBIG; |
4234a6de | 337 | goto out; |
75f0ee15 TW |
338 | } |
339 | ||
340 | if (cl->state != MEI_FILE_CONNECTED) { | |
2bf94cab | 341 | dev_err(dev->dev, "host client = %d, is not connected to ME client = %d", |
75f0ee15 | 342 | cl->host_client_id, cl->me_client_id); |
4234a6de TW |
343 | rets = -ENODEV; |
344 | goto out; | |
75f0ee15 | 345 | } |
ab841160 | 346 | if (cl == &dev->iamthif_cl) { |
19838fb8 | 347 | write_cb = mei_amthif_find_read_list_entry(dev, file); |
ab841160 OW |
348 | |
349 | if (write_cb) { | |
350 | timeout = write_cb->read_time + | |
3870c320 | 351 | mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER); |
ab841160 OW |
352 | |
353 | if (time_after(jiffies, timeout) || | |
75f0ee15 TW |
354 | cl->reading_state == MEI_READ_COMPLETE) { |
355 | *offset = 0; | |
fb601adb | 356 | list_del(&write_cb->list); |
601a1efa | 357 | mei_io_cb_free(write_cb); |
75f0ee15 | 358 | write_cb = NULL; |
ab841160 OW |
359 | } |
360 | } | |
361 | } | |
362 | ||
363 | /* free entry used in read */ | |
364 | if (cl->reading_state == MEI_READ_COMPLETE) { | |
365 | *offset = 0; | |
90e0b5f1 | 366 | write_cb = mei_cl_find_read_cb(cl); |
ab841160 | 367 | if (write_cb) { |
fb601adb | 368 | list_del(&write_cb->list); |
601a1efa | 369 | mei_io_cb_free(write_cb); |
ab841160 OW |
370 | write_cb = NULL; |
371 | cl->reading_state = MEI_IDLE; | |
372 | cl->read_cb = NULL; | |
ab841160 | 373 | } |
d91aaed3 | 374 | } else if (cl->reading_state == MEI_IDLE) |
ab841160 OW |
375 | *offset = 0; |
376 | ||
377 | ||
33d28c92 | 378 | write_cb = mei_io_cb_init(cl, file); |
ab841160 | 379 | if (!write_cb) { |
33d28c92 | 380 | rets = -ENOMEM; |
4234a6de | 381 | goto out; |
ab841160 | 382 | } |
33d28c92 TW |
383 | rets = mei_io_cb_alloc_req_buf(write_cb, length); |
384 | if (rets) | |
4234a6de | 385 | goto out; |
ab841160 | 386 | |
75f0ee15 | 387 | rets = copy_from_user(write_cb->request_buffer.data, ubuf, length); |
d8b29efa | 388 | if (rets) { |
2bf94cab | 389 | dev_dbg(dev->dev, "failed to copy data from userland\n"); |
d8b29efa | 390 | rets = -EFAULT; |
4234a6de | 391 | goto out; |
d8b29efa | 392 | } |
ab841160 | 393 | |
ab841160 | 394 | if (cl == &dev->iamthif_cl) { |
ab5c4a56 | 395 | rets = mei_amthif_write(dev, write_cb); |
ab841160 | 396 | |
ab5c4a56 | 397 | if (rets) { |
2bf94cab | 398 | dev_err(dev->dev, |
1a1aca42 | 399 | "amthif write failed with status = %d\n", rets); |
4234a6de | 400 | goto out; |
ab841160 OW |
401 | } |
402 | mutex_unlock(&dev->device_lock); | |
75f0ee15 | 403 | return length; |
ab841160 OW |
404 | } |
405 | ||
4234a6de | 406 | rets = mei_cl_write(cl, write_cb, false); |
b0d0cf77 | 407 | out: |
ab841160 | 408 | mutex_unlock(&dev->device_lock); |
4234a6de TW |
409 | if (rets < 0) |
410 | mei_io_cb_free(write_cb); | |
ab841160 OW |
411 | return rets; |
412 | } | |
413 | ||
9f81abda TW |
414 | /** |
415 | * mei_ioctl_connect_client - the connect to fw client IOCTL function | |
416 | * | |
9f81abda | 417 | * @file: private data of the file object |
a8605ea2 | 418 | * @data: IOCTL connect data, input and output parameters |
9f81abda TW |
419 | * |
420 | * Locking: called under "dev->device_lock" lock | |
421 | * | |
a8605ea2 | 422 | * Return: 0 on success, <0 on failure. |
9f81abda TW |
423 | */ |
424 | static int mei_ioctl_connect_client(struct file *file, | |
425 | struct mei_connect_client_data *data) | |
426 | { | |
427 | struct mei_device *dev; | |
428 | struct mei_client *client; | |
d320832f | 429 | struct mei_me_client *me_cl; |
9f81abda | 430 | struct mei_cl *cl; |
9f81abda TW |
431 | int rets; |
432 | ||
433 | cl = file->private_data; | |
9f81abda TW |
434 | dev = cl->dev; |
435 | ||
436 | if (dev->dev_state != MEI_DEV_ENABLED) { | |
437 | rets = -ENODEV; | |
438 | goto end; | |
439 | } | |
440 | ||
441 | if (cl->state != MEI_FILE_INITIALIZING && | |
442 | cl->state != MEI_FILE_DISCONNECTED) { | |
443 | rets = -EBUSY; | |
444 | goto end; | |
445 | } | |
446 | ||
447 | /* find ME client we're trying to connect to */ | |
d320832f TW |
448 | me_cl = mei_me_cl_by_uuid(dev, &data->in_client_uuid); |
449 | if (!me_cl || me_cl->props.fixed_address) { | |
2bf94cab | 450 | dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n", |
80fe6361 | 451 | &data->in_client_uuid); |
285e2996 | 452 | rets = -ENOTTY; |
80fe6361 | 453 | goto end; |
9f81abda TW |
454 | } |
455 | ||
d320832f | 456 | cl->me_client_id = me_cl->client_id; |
d880f329 | 457 | cl->cl_uuid = me_cl->props.protocol_name; |
80fe6361 | 458 | |
2bf94cab | 459 | dev_dbg(dev->dev, "Connect to FW Client ID = %d\n", |
9f81abda | 460 | cl->me_client_id); |
2bf94cab | 461 | dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n", |
d320832f | 462 | me_cl->props.protocol_version); |
2bf94cab | 463 | dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n", |
d320832f | 464 | me_cl->props.max_msg_length); |
9f81abda | 465 | |
1a1aca42 | 466 | /* if we're connecting to amthif client then we will use the |
9f81abda TW |
467 | * existing connection |
468 | */ | |
1a1aca42 | 469 | if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) { |
2bf94cab | 470 | dev_dbg(dev->dev, "FW Client is amthi\n"); |
9f81abda TW |
471 | if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) { |
472 | rets = -ENODEV; | |
473 | goto end; | |
474 | } | |
9f81abda TW |
475 | mei_cl_unlink(cl); |
476 | ||
477 | kfree(cl); | |
478 | cl = NULL; | |
22f96a0e | 479 | dev->iamthif_open_count++; |
9f81abda TW |
480 | file->private_data = &dev->iamthif_cl; |
481 | ||
482 | client = &data->out_client_properties; | |
d320832f TW |
483 | client->max_msg_length = me_cl->props.max_msg_length; |
484 | client->protocol_version = me_cl->props.protocol_version; | |
9f81abda TW |
485 | rets = dev->iamthif_cl.status; |
486 | ||
487 | goto end; | |
488 | } | |
489 | ||
9f81abda TW |
490 | |
491 | /* prepare the output buffer */ | |
492 | client = &data->out_client_properties; | |
d320832f TW |
493 | client->max_msg_length = me_cl->props.max_msg_length; |
494 | client->protocol_version = me_cl->props.protocol_version; | |
2bf94cab | 495 | dev_dbg(dev->dev, "Can connect?\n"); |
9f81abda TW |
496 | |
497 | ||
498 | rets = mei_cl_connect(cl, file); | |
499 | ||
500 | end: | |
9f81abda TW |
501 | return rets; |
502 | } | |
503 | ||
ab841160 OW |
504 | /** |
505 | * mei_ioctl - the IOCTL function | |
506 | * | |
507 | * @file: pointer to file structure | |
508 | * @cmd: ioctl command | |
509 | * @data: pointer to mei message structure | |
510 | * | |
a8605ea2 | 511 | * Return: 0 on success , <0 on error |
ab841160 OW |
512 | */ |
513 | static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data) | |
514 | { | |
515 | struct mei_device *dev; | |
516 | struct mei_cl *cl = file->private_data; | |
154eb18f | 517 | struct mei_connect_client_data connect_data; |
ab841160 OW |
518 | int rets; |
519 | ||
ab841160 OW |
520 | |
521 | if (WARN_ON(!cl || !cl->dev)) | |
522 | return -ENODEV; | |
523 | ||
524 | dev = cl->dev; | |
525 | ||
2bf94cab | 526 | dev_dbg(dev->dev, "IOCTL cmd = 0x%x", cmd); |
ab841160 OW |
527 | |
528 | mutex_lock(&dev->device_lock); | |
b210d750 | 529 | if (dev->dev_state != MEI_DEV_ENABLED) { |
ab841160 OW |
530 | rets = -ENODEV; |
531 | goto out; | |
532 | } | |
533 | ||
4f046e7b TW |
534 | switch (cmd) { |
535 | case IOCTL_MEI_CONNECT_CLIENT: | |
2bf94cab | 536 | dev_dbg(dev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n"); |
154eb18f | 537 | if (copy_from_user(&connect_data, (char __user *)data, |
4f046e7b | 538 | sizeof(struct mei_connect_client_data))) { |
2bf94cab | 539 | dev_dbg(dev->dev, "failed to copy data from userland\n"); |
4f046e7b TW |
540 | rets = -EFAULT; |
541 | goto out; | |
542 | } | |
ab841160 | 543 | |
154eb18f | 544 | rets = mei_ioctl_connect_client(file, &connect_data); |
4f046e7b TW |
545 | if (rets) |
546 | goto out; | |
ab841160 | 547 | |
4f046e7b | 548 | /* if all is ok, copying the data back to user. */ |
154eb18f | 549 | if (copy_to_user((char __user *)data, &connect_data, |
ab841160 | 550 | sizeof(struct mei_connect_client_data))) { |
2bf94cab | 551 | dev_dbg(dev->dev, "failed to copy data to userland\n"); |
4f046e7b TW |
552 | rets = -EFAULT; |
553 | goto out; | |
554 | } | |
555 | ||
556 | break; | |
154eb18f | 557 | |
4f046e7b | 558 | default: |
2bf94cab | 559 | dev_err(dev->dev, ": unsupported ioctl %d.\n", cmd); |
4f046e7b | 560 | rets = -ENOIOCTLCMD; |
ab841160 OW |
561 | } |
562 | ||
563 | out: | |
ab841160 OW |
564 | mutex_unlock(&dev->device_lock); |
565 | return rets; | |
566 | } | |
567 | ||
568 | /** | |
569 | * mei_compat_ioctl - the compat IOCTL function | |
570 | * | |
571 | * @file: pointer to file structure | |
572 | * @cmd: ioctl command | |
573 | * @data: pointer to mei message structure | |
574 | * | |
a8605ea2 | 575 | * Return: 0 on success , <0 on error |
ab841160 OW |
576 | */ |
577 | #ifdef CONFIG_COMPAT | |
578 | static long mei_compat_ioctl(struct file *file, | |
441ab50f | 579 | unsigned int cmd, unsigned long data) |
ab841160 OW |
580 | { |
581 | return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data)); | |
582 | } | |
583 | #endif | |
584 | ||
585 | ||
586 | /** | |
587 | * mei_poll - the poll function | |
588 | * | |
589 | * @file: pointer to file structure | |
590 | * @wait: pointer to poll_table structure | |
591 | * | |
a8605ea2 | 592 | * Return: poll mask |
ab841160 OW |
593 | */ |
594 | static unsigned int mei_poll(struct file *file, poll_table *wait) | |
595 | { | |
596 | struct mei_cl *cl = file->private_data; | |
597 | struct mei_device *dev; | |
598 | unsigned int mask = 0; | |
599 | ||
600 | if (WARN_ON(!cl || !cl->dev)) | |
b950ac1d | 601 | return POLLERR; |
ab841160 OW |
602 | |
603 | dev = cl->dev; | |
604 | ||
605 | mutex_lock(&dev->device_lock); | |
606 | ||
b950ac1d TW |
607 | if (!mei_cl_is_connected(cl)) { |
608 | mask = POLLERR; | |
ab841160 OW |
609 | goto out; |
610 | } | |
611 | ||
612 | mutex_unlock(&dev->device_lock); | |
b950ac1d TW |
613 | |
614 | ||
615 | if (cl == &dev->iamthif_cl) | |
616 | return mei_amthif_poll(dev, file, wait); | |
617 | ||
ab841160 | 618 | poll_wait(file, &cl->tx_wait, wait); |
b950ac1d | 619 | |
ab841160 | 620 | mutex_lock(&dev->device_lock); |
b950ac1d TW |
621 | |
622 | if (!mei_cl_is_connected(cl)) { | |
623 | mask = POLLERR; | |
624 | goto out; | |
625 | } | |
626 | ||
34ec4366 | 627 | mask |= (POLLIN | POLLRDNORM); |
ab841160 OW |
628 | |
629 | out: | |
630 | mutex_unlock(&dev->device_lock); | |
631 | return mask; | |
632 | } | |
633 | ||
5b881e3c OW |
634 | /* |
635 | * file operations structure will be used for mei char device. | |
636 | */ | |
637 | static const struct file_operations mei_fops = { | |
638 | .owner = THIS_MODULE, | |
639 | .read = mei_read, | |
640 | .unlocked_ioctl = mei_ioctl, | |
641 | #ifdef CONFIG_COMPAT | |
642 | .compat_ioctl = mei_compat_ioctl, | |
643 | #endif | |
644 | .open = mei_open, | |
645 | .release = mei_release, | |
646 | .write = mei_write, | |
647 | .poll = mei_poll, | |
648 | .llseek = no_llseek | |
649 | }; | |
650 | ||
f3d8e878 AU |
651 | static struct class *mei_class; |
652 | static dev_t mei_devt; | |
653 | #define MEI_MAX_DEVS MINORMASK | |
654 | static DEFINE_MUTEX(mei_minor_lock); | |
655 | static DEFINE_IDR(mei_idr); | |
656 | ||
657 | /** | |
658 | * mei_minor_get - obtain next free device minor number | |
659 | * | |
660 | * @dev: device pointer | |
661 | * | |
a8605ea2 | 662 | * Return: allocated minor, or -ENOSPC if no free minor left |
5b881e3c | 663 | */ |
f3d8e878 AU |
664 | static int mei_minor_get(struct mei_device *dev) |
665 | { | |
666 | int ret; | |
667 | ||
668 | mutex_lock(&mei_minor_lock); | |
669 | ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL); | |
670 | if (ret >= 0) | |
671 | dev->minor = ret; | |
672 | else if (ret == -ENOSPC) | |
2bf94cab | 673 | dev_err(dev->dev, "too many mei devices\n"); |
5b881e3c | 674 | |
f3d8e878 AU |
675 | mutex_unlock(&mei_minor_lock); |
676 | return ret; | |
677 | } | |
30e53bb8 | 678 | |
f3d8e878 AU |
679 | /** |
680 | * mei_minor_free - mark device minor number as free | |
681 | * | |
682 | * @dev: device pointer | |
683 | */ | |
684 | static void mei_minor_free(struct mei_device *dev) | |
9a123f19 | 685 | { |
f3d8e878 AU |
686 | mutex_lock(&mei_minor_lock); |
687 | idr_remove(&mei_idr, dev->minor); | |
688 | mutex_unlock(&mei_minor_lock); | |
689 | } | |
690 | ||
691 | int mei_register(struct mei_device *dev, struct device *parent) | |
692 | { | |
693 | struct device *clsdev; /* class device */ | |
694 | int ret, devno; | |
695 | ||
696 | ret = mei_minor_get(dev); | |
697 | if (ret < 0) | |
30e53bb8 TW |
698 | return ret; |
699 | ||
f3d8e878 AU |
700 | /* Fill in the data structures */ |
701 | devno = MKDEV(MAJOR(mei_devt), dev->minor); | |
702 | cdev_init(&dev->cdev, &mei_fops); | |
703 | dev->cdev.owner = mei_fops.owner; | |
704 | ||
705 | /* Add the device */ | |
706 | ret = cdev_add(&dev->cdev, devno, 1); | |
707 | if (ret) { | |
708 | dev_err(parent, "unable to add device %d:%d\n", | |
709 | MAJOR(mei_devt), dev->minor); | |
710 | goto err_dev_add; | |
711 | } | |
712 | ||
713 | clsdev = device_create(mei_class, parent, devno, | |
714 | NULL, "mei%d", dev->minor); | |
715 | ||
716 | if (IS_ERR(clsdev)) { | |
717 | dev_err(parent, "unable to create device %d:%d\n", | |
718 | MAJOR(mei_devt), dev->minor); | |
719 | ret = PTR_ERR(clsdev); | |
720 | goto err_dev_create; | |
721 | } | |
722 | ||
723 | ret = mei_dbgfs_register(dev, dev_name(clsdev)); | |
724 | if (ret) { | |
725 | dev_err(clsdev, "cannot register debugfs ret = %d\n", ret); | |
726 | goto err_dev_dbgfs; | |
727 | } | |
30e53bb8 TW |
728 | |
729 | return 0; | |
f3d8e878 AU |
730 | |
731 | err_dev_dbgfs: | |
732 | device_destroy(mei_class, devno); | |
733 | err_dev_create: | |
734 | cdev_del(&dev->cdev); | |
735 | err_dev_add: | |
736 | mei_minor_free(dev); | |
737 | return ret; | |
5b881e3c | 738 | } |
40e0b67b | 739 | EXPORT_SYMBOL_GPL(mei_register); |
5b881e3c | 740 | |
30e53bb8 | 741 | void mei_deregister(struct mei_device *dev) |
5b881e3c | 742 | { |
f3d8e878 AU |
743 | int devno; |
744 | ||
745 | devno = dev->cdev.dev; | |
746 | cdev_del(&dev->cdev); | |
747 | ||
30e53bb8 | 748 | mei_dbgfs_deregister(dev); |
f3d8e878 AU |
749 | |
750 | device_destroy(mei_class, devno); | |
751 | ||
752 | mei_minor_free(dev); | |
5b881e3c | 753 | } |
40e0b67b | 754 | EXPORT_SYMBOL_GPL(mei_deregister); |
ab841160 | 755 | |
cf3baefb SO |
756 | static int __init mei_init(void) |
757 | { | |
f3d8e878 AU |
758 | int ret; |
759 | ||
760 | mei_class = class_create(THIS_MODULE, "mei"); | |
761 | if (IS_ERR(mei_class)) { | |
762 | pr_err("couldn't create class\n"); | |
763 | ret = PTR_ERR(mei_class); | |
764 | goto err; | |
765 | } | |
766 | ||
767 | ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei"); | |
768 | if (ret < 0) { | |
769 | pr_err("unable to allocate char dev region\n"); | |
770 | goto err_class; | |
771 | } | |
772 | ||
773 | ret = mei_cl_bus_init(); | |
774 | if (ret < 0) { | |
775 | pr_err("unable to initialize bus\n"); | |
776 | goto err_chrdev; | |
777 | } | |
778 | ||
779 | return 0; | |
780 | ||
781 | err_chrdev: | |
782 | unregister_chrdev_region(mei_devt, MEI_MAX_DEVS); | |
783 | err_class: | |
784 | class_destroy(mei_class); | |
785 | err: | |
786 | return ret; | |
cf3baefb SO |
787 | } |
788 | ||
789 | static void __exit mei_exit(void) | |
790 | { | |
f3d8e878 AU |
791 | unregister_chrdev_region(mei_devt, MEI_MAX_DEVS); |
792 | class_destroy(mei_class); | |
cf3baefb SO |
793 | mei_cl_bus_exit(); |
794 | } | |
795 | ||
796 | module_init(mei_init); | |
797 | module_exit(mei_exit); | |
798 | ||
40e0b67b TW |
799 | MODULE_AUTHOR("Intel Corporation"); |
800 | MODULE_DESCRIPTION("Intel(R) Management Engine Interface"); | |
827eef51 | 801 | MODULE_LICENSE("GPL v2"); |
ab841160 | 802 |