main.c 18 KB

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