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