main.c 18 KB

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