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