main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2012, Intel Corporation.
  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. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/kernel.h>
  19. #include <linux/device.h>
  20. #include <linux/slab.h>
  21. #include <linux/fs.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/poll.h>
  26. #include <linux/init.h>
  27. #include <linux/ioctl.h>
  28. #include <linux/cdev.h>
  29. #include <linux/sched.h>
  30. #include <linux/uuid.h>
  31. #include <linux/compat.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/mei.h>
  35. #include "mei_dev.h"
  36. #include "client.h"
  37. /**
  38. * mei_open - the open function
  39. *
  40. * @inode: pointer to inode structure
  41. * @file: pointer to file structure
  42. *
  43. * Return: 0 on success, <0 on error
  44. */
  45. static int mei_open(struct inode *inode, struct file *file)
  46. {
  47. struct mei_device *dev;
  48. struct mei_cl *cl;
  49. int err;
  50. dev = container_of(inode->i_cdev, struct mei_device, cdev);
  51. if (!dev)
  52. return -ENODEV;
  53. mutex_lock(&dev->device_lock);
  54. if (dev->dev_state != MEI_DEV_ENABLED) {
  55. dev_dbg(dev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
  56. mei_dev_state_str(dev->dev_state));
  57. err = -ENODEV;
  58. goto err_unlock;
  59. }
  60. cl = mei_cl_alloc_linked(dev, MEI_HOST_CLIENT_ID_ANY);
  61. if (IS_ERR(cl)) {
  62. err = PTR_ERR(cl);
  63. goto err_unlock;
  64. }
  65. file->private_data = cl;
  66. mutex_unlock(&dev->device_lock);
  67. return nonseekable_open(inode, file);
  68. err_unlock:
  69. mutex_unlock(&dev->device_lock);
  70. return err;
  71. }
  72. /**
  73. * mei_release - the release function
  74. *
  75. * @inode: pointer to inode structure
  76. * @file: pointer to file structure
  77. *
  78. * Return: 0 on success, <0 on error
  79. */
  80. static int mei_release(struct inode *inode, struct file *file)
  81. {
  82. struct mei_cl *cl = file->private_data;
  83. struct mei_device *dev;
  84. int rets = 0;
  85. if (WARN_ON(!cl || !cl->dev))
  86. return -ENODEV;
  87. dev = cl->dev;
  88. mutex_lock(&dev->device_lock);
  89. if (cl == &dev->iamthif_cl) {
  90. rets = mei_amthif_release(dev, file);
  91. goto out;
  92. }
  93. if (mei_cl_is_connected(cl)) {
  94. cl->state = MEI_FILE_DISCONNECTING;
  95. cl_dbg(dev, cl, "disconnecting\n");
  96. rets = mei_cl_disconnect(cl);
  97. }
  98. mei_cl_flush_queues(cl, file);
  99. cl_dbg(dev, cl, "removing\n");
  100. mei_cl_unlink(cl);
  101. file->private_data = NULL;
  102. kfree(cl);
  103. out:
  104. mutex_unlock(&dev->device_lock);
  105. return rets;
  106. }
  107. /**
  108. * mei_read - the read function.
  109. *
  110. * @file: pointer to file structure
  111. * @ubuf: pointer to user buffer
  112. * @length: buffer length
  113. * @offset: data offset in buffer
  114. *
  115. * Return: >=0 data length on success , <0 on error
  116. */
  117. static ssize_t mei_read(struct file *file, char __user *ubuf,
  118. size_t length, loff_t *offset)
  119. {
  120. struct mei_cl *cl = file->private_data;
  121. struct mei_device *dev;
  122. struct mei_cl_cb *cb = NULL;
  123. int rets;
  124. int err;
  125. if (WARN_ON(!cl || !cl->dev))
  126. return -ENODEV;
  127. dev = cl->dev;
  128. mutex_lock(&dev->device_lock);
  129. if (dev->dev_state != MEI_DEV_ENABLED) {
  130. rets = -ENODEV;
  131. goto out;
  132. }
  133. if (length == 0) {
  134. rets = 0;
  135. goto out;
  136. }
  137. if (cl == &dev->iamthif_cl) {
  138. rets = mei_amthif_read(dev, file, ubuf, length, offset);
  139. goto out;
  140. }
  141. cb = mei_cl_read_cb(cl, file);
  142. if (cb) {
  143. /* read what left */
  144. if (cb->buf_idx > *offset)
  145. goto copy_buffer;
  146. /* offset is beyond buf_idx we have no more data return 0 */
  147. if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
  148. rets = 0;
  149. goto free;
  150. }
  151. /* Offset needs to be cleaned for contiguous reads*/
  152. if (cb->buf_idx == 0 && *offset > 0)
  153. *offset = 0;
  154. } else if (*offset > 0) {
  155. *offset = 0;
  156. }
  157. err = mei_cl_read_start(cl, length, file);
  158. if (err && err != -EBUSY) {
  159. dev_dbg(dev->dev,
  160. "mei start read failure with status = %d\n", err);
  161. rets = err;
  162. goto out;
  163. }
  164. if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) {
  165. if (file->f_flags & O_NONBLOCK) {
  166. rets = -EAGAIN;
  167. goto out;
  168. }
  169. mutex_unlock(&dev->device_lock);
  170. if (wait_event_interruptible(cl->rx_wait,
  171. (!list_empty(&cl->rd_completed)) ||
  172. (!mei_cl_is_connected(cl)))) {
  173. if (signal_pending(current))
  174. return -EINTR;
  175. return -ERESTARTSYS;
  176. }
  177. mutex_lock(&dev->device_lock);
  178. if (!mei_cl_is_connected(cl)) {
  179. rets = -EBUSY;
  180. goto out;
  181. }
  182. }
  183. cb = mei_cl_read_cb(cl, file);
  184. if (!cb) {
  185. rets = 0;
  186. goto out;
  187. }
  188. copy_buffer:
  189. /* now copy the data to user space */
  190. if (cb->status) {
  191. rets = cb->status;
  192. dev_dbg(dev->dev, "read operation failed %d\n", rets);
  193. goto free;
  194. }
  195. dev_dbg(dev->dev, "buf.size = %d buf.idx= %ld\n",
  196. cb->buf.size, cb->buf_idx);
  197. if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
  198. rets = -EMSGSIZE;
  199. goto free;
  200. }
  201. /* length is being truncated to PAGE_SIZE,
  202. * however buf_idx may point beyond that */
  203. length = min_t(size_t, length, cb->buf_idx - *offset);
  204. if (copy_to_user(ubuf, cb->buf.data + *offset, length)) {
  205. dev_dbg(dev->dev, "failed to copy data to userland\n");
  206. rets = -EFAULT;
  207. goto free;
  208. }
  209. rets = length;
  210. *offset += length;
  211. if ((unsigned long)*offset < cb->buf_idx)
  212. goto out;
  213. free:
  214. mei_io_cb_free(cb);
  215. out:
  216. dev_dbg(dev->dev, "end mei read rets= %d\n", rets);
  217. mutex_unlock(&dev->device_lock);
  218. return rets;
  219. }
  220. /**
  221. * mei_write - the write function.
  222. *
  223. * @file: pointer to file structure
  224. * @ubuf: pointer to user buffer
  225. * @length: buffer length
  226. * @offset: data offset in buffer
  227. *
  228. * Return: >=0 data length on success , <0 on error
  229. */
  230. static ssize_t mei_write(struct file *file, const char __user *ubuf,
  231. size_t length, loff_t *offset)
  232. {
  233. struct mei_cl *cl = file->private_data;
  234. struct mei_me_client *me_cl = NULL;
  235. struct mei_cl_cb *write_cb = NULL;
  236. struct mei_device *dev;
  237. unsigned long timeout = 0;
  238. int rets;
  239. if (WARN_ON(!cl || !cl->dev))
  240. return -ENODEV;
  241. dev = cl->dev;
  242. mutex_lock(&dev->device_lock);
  243. if (dev->dev_state != MEI_DEV_ENABLED) {
  244. rets = -ENODEV;
  245. goto out;
  246. }
  247. me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
  248. if (!me_cl) {
  249. rets = -ENOTTY;
  250. goto out;
  251. }
  252. if (length == 0) {
  253. rets = 0;
  254. goto out;
  255. }
  256. if (length > me_cl->props.max_msg_length) {
  257. rets = -EFBIG;
  258. goto out;
  259. }
  260. if (!mei_cl_is_connected(cl)) {
  261. cl_err(dev, cl, "is not connected");
  262. rets = -ENODEV;
  263. goto out;
  264. }
  265. if (cl == &dev->iamthif_cl) {
  266. write_cb = mei_amthif_find_read_list_entry(dev, file);
  267. if (write_cb) {
  268. timeout = write_cb->read_time +
  269. mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
  270. if (time_after(jiffies, timeout)) {
  271. *offset = 0;
  272. mei_io_cb_free(write_cb);
  273. write_cb = NULL;
  274. }
  275. }
  276. }
  277. *offset = 0;
  278. write_cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file);
  279. if (!write_cb) {
  280. rets = -ENOMEM;
  281. goto out;
  282. }
  283. rets = copy_from_user(write_cb->buf.data, ubuf, length);
  284. if (rets) {
  285. dev_dbg(dev->dev, "failed to copy data from userland\n");
  286. rets = -EFAULT;
  287. goto out;
  288. }
  289. if (cl == &dev->iamthif_cl) {
  290. rets = mei_amthif_write(cl, write_cb);
  291. if (rets) {
  292. dev_err(dev->dev,
  293. "amthif write failed with status = %d\n", rets);
  294. goto out;
  295. }
  296. mei_me_cl_put(me_cl);
  297. mutex_unlock(&dev->device_lock);
  298. return length;
  299. }
  300. rets = mei_cl_write(cl, write_cb, false);
  301. out:
  302. mei_me_cl_put(me_cl);
  303. mutex_unlock(&dev->device_lock);
  304. if (rets < 0)
  305. mei_io_cb_free(write_cb);
  306. return rets;
  307. }
  308. /**
  309. * mei_ioctl_connect_client - the connect to fw client IOCTL function
  310. *
  311. * @file: private data of the file object
  312. * @data: IOCTL connect data, input and output parameters
  313. *
  314. * Locking: called under "dev->device_lock" lock
  315. *
  316. * Return: 0 on success, <0 on failure.
  317. */
  318. static int mei_ioctl_connect_client(struct file *file,
  319. struct mei_connect_client_data *data)
  320. {
  321. struct mei_device *dev;
  322. struct mei_client *client;
  323. struct mei_me_client *me_cl;
  324. struct mei_cl *cl;
  325. int rets;
  326. cl = file->private_data;
  327. dev = cl->dev;
  328. if (dev->dev_state != MEI_DEV_ENABLED)
  329. return -ENODEV;
  330. if (cl->state != MEI_FILE_INITIALIZING &&
  331. cl->state != MEI_FILE_DISCONNECTED)
  332. return -EBUSY;
  333. /* find ME client we're trying to connect to */
  334. me_cl = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
  335. if (!me_cl || me_cl->props.fixed_address) {
  336. dev_dbg(dev->dev, "Cannot connect to FW Client UUID = %pUl\n",
  337. &data->in_client_uuid);
  338. return -ENOTTY;
  339. }
  340. cl->me_client_id = me_cl->client_id;
  341. cl->cl_uuid = me_cl->props.protocol_name;
  342. dev_dbg(dev->dev, "Connect to FW Client ID = %d\n",
  343. cl->me_client_id);
  344. dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n",
  345. me_cl->props.protocol_version);
  346. dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n",
  347. me_cl->props.max_msg_length);
  348. /* if we're connecting to amthif client then we will use the
  349. * existing connection
  350. */
  351. if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) {
  352. dev_dbg(dev->dev, "FW Client is amthi\n");
  353. if (!mei_cl_is_connected(&dev->iamthif_cl)) {
  354. rets = -ENODEV;
  355. goto end;
  356. }
  357. mei_cl_unlink(cl);
  358. kfree(cl);
  359. cl = NULL;
  360. dev->iamthif_open_count++;
  361. file->private_data = &dev->iamthif_cl;
  362. client = &data->out_client_properties;
  363. client->max_msg_length = me_cl->props.max_msg_length;
  364. client->protocol_version = me_cl->props.protocol_version;
  365. rets = dev->iamthif_cl.status;
  366. goto end;
  367. }
  368. /* prepare the output buffer */
  369. client = &data->out_client_properties;
  370. client->max_msg_length = me_cl->props.max_msg_length;
  371. client->protocol_version = me_cl->props.protocol_version;
  372. dev_dbg(dev->dev, "Can connect?\n");
  373. rets = mei_cl_connect(cl, file);
  374. end:
  375. mei_me_cl_put(me_cl);
  376. return rets;
  377. }
  378. /**
  379. * mei_ioctl - the IOCTL function
  380. *
  381. * @file: pointer to file structure
  382. * @cmd: ioctl command
  383. * @data: pointer to mei message structure
  384. *
  385. * Return: 0 on success , <0 on error
  386. */
  387. static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  388. {
  389. struct mei_device *dev;
  390. struct mei_cl *cl = file->private_data;
  391. struct mei_connect_client_data connect_data;
  392. int rets;
  393. if (WARN_ON(!cl || !cl->dev))
  394. return -ENODEV;
  395. dev = cl->dev;
  396. dev_dbg(dev->dev, "IOCTL cmd = 0x%x", cmd);
  397. mutex_lock(&dev->device_lock);
  398. if (dev->dev_state != MEI_DEV_ENABLED) {
  399. rets = -ENODEV;
  400. goto out;
  401. }
  402. switch (cmd) {
  403. case IOCTL_MEI_CONNECT_CLIENT:
  404. dev_dbg(dev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
  405. if (copy_from_user(&connect_data, (char __user *)data,
  406. sizeof(struct mei_connect_client_data))) {
  407. dev_dbg(dev->dev, "failed to copy data from userland\n");
  408. rets = -EFAULT;
  409. goto out;
  410. }
  411. rets = mei_ioctl_connect_client(file, &connect_data);
  412. if (rets)
  413. goto out;
  414. /* if all is ok, copying the data back to user. */
  415. if (copy_to_user((char __user *)data, &connect_data,
  416. sizeof(struct mei_connect_client_data))) {
  417. dev_dbg(dev->dev, "failed to copy data to userland\n");
  418. rets = -EFAULT;
  419. goto out;
  420. }
  421. break;
  422. default:
  423. dev_err(dev->dev, ": unsupported ioctl %d.\n", cmd);
  424. rets = -ENOIOCTLCMD;
  425. }
  426. out:
  427. mutex_unlock(&dev->device_lock);
  428. return rets;
  429. }
  430. /**
  431. * mei_compat_ioctl - the compat IOCTL function
  432. *
  433. * @file: pointer to file structure
  434. * @cmd: ioctl command
  435. * @data: pointer to mei message structure
  436. *
  437. * Return: 0 on success , <0 on error
  438. */
  439. #ifdef CONFIG_COMPAT
  440. static long mei_compat_ioctl(struct file *file,
  441. unsigned int cmd, unsigned long data)
  442. {
  443. return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
  444. }
  445. #endif
  446. /**
  447. * mei_poll - the poll function
  448. *
  449. * @file: pointer to file structure
  450. * @wait: pointer to poll_table structure
  451. *
  452. * Return: poll mask
  453. */
  454. static unsigned int mei_poll(struct file *file, poll_table *wait)
  455. {
  456. unsigned long req_events = poll_requested_events(wait);
  457. struct mei_cl *cl = file->private_data;
  458. struct mei_device *dev;
  459. unsigned int mask = 0;
  460. if (WARN_ON(!cl || !cl->dev))
  461. return POLLERR;
  462. dev = cl->dev;
  463. mutex_lock(&dev->device_lock);
  464. if (dev->dev_state != MEI_DEV_ENABLED ||
  465. !mei_cl_is_connected(cl)) {
  466. mask = POLLERR;
  467. goto out;
  468. }
  469. if (cl == &dev->iamthif_cl) {
  470. mask = mei_amthif_poll(dev, file, wait);
  471. goto out;
  472. }
  473. if (req_events & (POLLIN | POLLRDNORM)) {
  474. poll_wait(file, &cl->rx_wait, wait);
  475. if (!list_empty(&cl->rd_completed))
  476. mask |= POLLIN | POLLRDNORM;
  477. else
  478. mei_cl_read_start(cl, 0, file);
  479. }
  480. out:
  481. mutex_unlock(&dev->device_lock);
  482. return mask;
  483. }
  484. /**
  485. * fw_status_show - mei device attribute show method
  486. *
  487. * @device: device pointer
  488. * @attr: attribute pointer
  489. * @buf: char out buffer
  490. *
  491. * Return: number of the bytes printed into buf or error
  492. */
  493. static ssize_t fw_status_show(struct device *device,
  494. struct device_attribute *attr, char *buf)
  495. {
  496. struct mei_device *dev = dev_get_drvdata(device);
  497. struct mei_fw_status fw_status;
  498. int err, i;
  499. ssize_t cnt = 0;
  500. mutex_lock(&dev->device_lock);
  501. err = mei_fw_status(dev, &fw_status);
  502. mutex_unlock(&dev->device_lock);
  503. if (err) {
  504. dev_err(device, "read fw_status error = %d\n", err);
  505. return err;
  506. }
  507. for (i = 0; i < fw_status.count; i++)
  508. cnt += scnprintf(buf + cnt, PAGE_SIZE - cnt, "%08X\n",
  509. fw_status.status[i]);
  510. return cnt;
  511. }
  512. static DEVICE_ATTR_RO(fw_status);
  513. static struct attribute *mei_attrs[] = {
  514. &dev_attr_fw_status.attr,
  515. NULL
  516. };
  517. ATTRIBUTE_GROUPS(mei);
  518. /*
  519. * file operations structure will be used for mei char device.
  520. */
  521. static const struct file_operations mei_fops = {
  522. .owner = THIS_MODULE,
  523. .read = mei_read,
  524. .unlocked_ioctl = mei_ioctl,
  525. #ifdef CONFIG_COMPAT
  526. .compat_ioctl = mei_compat_ioctl,
  527. #endif
  528. .open = mei_open,
  529. .release = mei_release,
  530. .write = mei_write,
  531. .poll = mei_poll,
  532. .llseek = no_llseek
  533. };
  534. static struct class *mei_class;
  535. static dev_t mei_devt;
  536. #define MEI_MAX_DEVS MINORMASK
  537. static DEFINE_MUTEX(mei_minor_lock);
  538. static DEFINE_IDR(mei_idr);
  539. /**
  540. * mei_minor_get - obtain next free device minor number
  541. *
  542. * @dev: device pointer
  543. *
  544. * Return: allocated minor, or -ENOSPC if no free minor left
  545. */
  546. static int mei_minor_get(struct mei_device *dev)
  547. {
  548. int ret;
  549. mutex_lock(&mei_minor_lock);
  550. ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL);
  551. if (ret >= 0)
  552. dev->minor = ret;
  553. else if (ret == -ENOSPC)
  554. dev_err(dev->dev, "too many mei devices\n");
  555. mutex_unlock(&mei_minor_lock);
  556. return ret;
  557. }
  558. /**
  559. * mei_minor_free - mark device minor number as free
  560. *
  561. * @dev: device pointer
  562. */
  563. static void mei_minor_free(struct mei_device *dev)
  564. {
  565. mutex_lock(&mei_minor_lock);
  566. idr_remove(&mei_idr, dev->minor);
  567. mutex_unlock(&mei_minor_lock);
  568. }
  569. int mei_register(struct mei_device *dev, struct device *parent)
  570. {
  571. struct device *clsdev; /* class device */
  572. int ret, devno;
  573. ret = mei_minor_get(dev);
  574. if (ret < 0)
  575. return ret;
  576. /* Fill in the data structures */
  577. devno = MKDEV(MAJOR(mei_devt), dev->minor);
  578. cdev_init(&dev->cdev, &mei_fops);
  579. dev->cdev.owner = mei_fops.owner;
  580. /* Add the device */
  581. ret = cdev_add(&dev->cdev, devno, 1);
  582. if (ret) {
  583. dev_err(parent, "unable to add device %d:%d\n",
  584. MAJOR(mei_devt), dev->minor);
  585. goto err_dev_add;
  586. }
  587. clsdev = device_create_with_groups(mei_class, parent, devno,
  588. dev, mei_groups,
  589. "mei%d", dev->minor);
  590. if (IS_ERR(clsdev)) {
  591. dev_err(parent, "unable to create device %d:%d\n",
  592. MAJOR(mei_devt), dev->minor);
  593. ret = PTR_ERR(clsdev);
  594. goto err_dev_create;
  595. }
  596. ret = mei_dbgfs_register(dev, dev_name(clsdev));
  597. if (ret) {
  598. dev_err(clsdev, "cannot register debugfs ret = %d\n", ret);
  599. goto err_dev_dbgfs;
  600. }
  601. return 0;
  602. err_dev_dbgfs:
  603. device_destroy(mei_class, devno);
  604. err_dev_create:
  605. cdev_del(&dev->cdev);
  606. err_dev_add:
  607. mei_minor_free(dev);
  608. return ret;
  609. }
  610. EXPORT_SYMBOL_GPL(mei_register);
  611. void mei_deregister(struct mei_device *dev)
  612. {
  613. int devno;
  614. devno = dev->cdev.dev;
  615. cdev_del(&dev->cdev);
  616. mei_dbgfs_deregister(dev);
  617. device_destroy(mei_class, devno);
  618. mei_minor_free(dev);
  619. }
  620. EXPORT_SYMBOL_GPL(mei_deregister);
  621. static int __init mei_init(void)
  622. {
  623. int ret;
  624. mei_class = class_create(THIS_MODULE, "mei");
  625. if (IS_ERR(mei_class)) {
  626. pr_err("couldn't create class\n");
  627. ret = PTR_ERR(mei_class);
  628. goto err;
  629. }
  630. ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei");
  631. if (ret < 0) {
  632. pr_err("unable to allocate char dev region\n");
  633. goto err_class;
  634. }
  635. ret = mei_cl_bus_init();
  636. if (ret < 0) {
  637. pr_err("unable to initialize bus\n");
  638. goto err_chrdev;
  639. }
  640. return 0;
  641. err_chrdev:
  642. unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
  643. err_class:
  644. class_destroy(mei_class);
  645. err:
  646. return ret;
  647. }
  648. static void __exit mei_exit(void)
  649. {
  650. unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
  651. class_destroy(mei_class);
  652. mei_cl_bus_exit();
  653. }
  654. module_init(mei_init);
  655. module_exit(mei_exit);
  656. MODULE_AUTHOR("Intel Corporation");
  657. MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
  658. MODULE_LICENSE("GPL v2");