main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. cb = cl->read_cb;
  161. if (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, file);
  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. copy_buffer:
  213. /* now copy the data to user space */
  214. if (cb->status) {
  215. rets = cb->status;
  216. dev_dbg(dev->dev, "read operation failed %d\n", rets);
  217. goto free;
  218. }
  219. dev_dbg(dev->dev, "buf.size = %d buf.idx= %ld\n",
  220. cb->buf.size, cb->buf_idx);
  221. if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
  222. rets = -EMSGSIZE;
  223. goto free;
  224. }
  225. /* length is being truncated to PAGE_SIZE,
  226. * however buf_idx may point beyond that */
  227. length = min_t(size_t, length, cb->buf_idx - *offset);
  228. if (copy_to_user(ubuf, cb->buf.data + *offset, length)) {
  229. dev_dbg(dev->dev, "failed to copy data to userland\n");
  230. rets = -EFAULT;
  231. goto free;
  232. }
  233. rets = length;
  234. *offset += length;
  235. if ((unsigned long)*offset < cb->buf_idx)
  236. goto out;
  237. free:
  238. cb_pos = mei_cl_find_read_cb(cl);
  239. /* Remove entry from read list */
  240. if (cb_pos)
  241. list_del(&cb_pos->list);
  242. mei_io_cb_free(cb);
  243. cl->reading_state = MEI_IDLE;
  244. cl->read_cb = NULL;
  245. out:
  246. dev_dbg(dev->dev, "end mei read rets= %d\n", rets);
  247. mutex_unlock(&dev->device_lock);
  248. return rets;
  249. }
  250. /**
  251. * mei_write - the write function.
  252. *
  253. * @file: pointer to file structure
  254. * @ubuf: pointer to user buffer
  255. * @length: buffer length
  256. * @offset: data offset in buffer
  257. *
  258. * Return: >=0 data length on success , <0 on error
  259. */
  260. static ssize_t mei_write(struct file *file, const char __user *ubuf,
  261. size_t length, loff_t *offset)
  262. {
  263. struct mei_cl *cl = file->private_data;
  264. struct mei_me_client *me_cl = NULL;
  265. struct mei_cl_cb *write_cb = NULL;
  266. struct mei_device *dev;
  267. unsigned long timeout = 0;
  268. int rets;
  269. if (WARN_ON(!cl || !cl->dev))
  270. return -ENODEV;
  271. dev = cl->dev;
  272. mutex_lock(&dev->device_lock);
  273. if (dev->dev_state != MEI_DEV_ENABLED) {
  274. rets = -ENODEV;
  275. goto out;
  276. }
  277. me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
  278. if (!me_cl) {
  279. rets = -ENOTTY;
  280. goto out;
  281. }
  282. if (length == 0) {
  283. rets = 0;
  284. goto out;
  285. }
  286. if (length > me_cl->props.max_msg_length) {
  287. rets = -EFBIG;
  288. goto out;
  289. }
  290. if (cl->state != MEI_FILE_CONNECTED) {
  291. dev_err(dev->dev, "host client = %d, is not connected to ME client = %d",
  292. cl->host_client_id, cl->me_client_id);
  293. rets = -ENODEV;
  294. goto out;
  295. }
  296. if (cl == &dev->iamthif_cl) {
  297. write_cb = mei_amthif_find_read_list_entry(dev, file);
  298. if (write_cb) {
  299. timeout = write_cb->read_time +
  300. mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
  301. if (time_after(jiffies, timeout) ||
  302. cl->reading_state == MEI_READ_COMPLETE) {
  303. *offset = 0;
  304. list_del(&write_cb->list);
  305. mei_io_cb_free(write_cb);
  306. write_cb = NULL;
  307. }
  308. }
  309. }
  310. /* free entry used in read */
  311. if (cl->reading_state == MEI_READ_COMPLETE) {
  312. *offset = 0;
  313. write_cb = mei_cl_find_read_cb(cl);
  314. if (write_cb) {
  315. list_del(&write_cb->list);
  316. mei_io_cb_free(write_cb);
  317. write_cb = NULL;
  318. cl->reading_state = MEI_IDLE;
  319. cl->read_cb = NULL;
  320. }
  321. } else if (cl->reading_state == MEI_IDLE)
  322. *offset = 0;
  323. write_cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file);
  324. if (!write_cb) {
  325. rets = -ENOMEM;
  326. goto out;
  327. }
  328. rets = copy_from_user(write_cb->buf.data, ubuf, length);
  329. if (rets) {
  330. dev_dbg(dev->dev, "failed to copy data from userland\n");
  331. rets = -EFAULT;
  332. goto out;
  333. }
  334. if (cl == &dev->iamthif_cl) {
  335. rets = mei_amthif_write(cl, write_cb);
  336. if (rets) {
  337. dev_err(dev->dev,
  338. "amthif write failed with status = %d\n", rets);
  339. goto out;
  340. }
  341. mei_me_cl_put(me_cl);
  342. mutex_unlock(&dev->device_lock);
  343. return length;
  344. }
  345. rets = mei_cl_write(cl, write_cb, false);
  346. out:
  347. mei_me_cl_put(me_cl);
  348. mutex_unlock(&dev->device_lock);
  349. if (rets < 0)
  350. mei_io_cb_free(write_cb);
  351. return rets;
  352. }
  353. /**
  354. * mei_ioctl_connect_client - the connect to fw client IOCTL function
  355. *
  356. * @file: private data of the file object
  357. * @data: IOCTL connect data, input and output parameters
  358. *
  359. * Locking: called under "dev->device_lock" lock
  360. *
  361. * Return: 0 on success, <0 on failure.
  362. */
  363. static int mei_ioctl_connect_client(struct file *file,
  364. struct mei_connect_client_data *data)
  365. {
  366. struct mei_device *dev;
  367. struct mei_client *client;
  368. struct mei_me_client *me_cl;
  369. struct mei_cl *cl;
  370. int rets;
  371. cl = file->private_data;
  372. dev = cl->dev;
  373. if (dev->dev_state != MEI_DEV_ENABLED)
  374. return -ENODEV;
  375. if (cl->state != MEI_FILE_INITIALIZING &&
  376. cl->state != MEI_FILE_DISCONNECTED)
  377. return -EBUSY;
  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. return -ENOTTY;
  384. }
  385. cl->me_client_id = me_cl->client_id;
  386. cl->cl_uuid = me_cl->props.protocol_name;
  387. dev_dbg(dev->dev, "Connect to FW Client ID = %d\n",
  388. cl->me_client_id);
  389. dev_dbg(dev->dev, "FW Client - Protocol Version = %d\n",
  390. me_cl->props.protocol_version);
  391. dev_dbg(dev->dev, "FW Client - Max Msg Len = %d\n",
  392. me_cl->props.max_msg_length);
  393. /* if we're connecting to amthif client then we will use the
  394. * existing connection
  395. */
  396. if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) {
  397. dev_dbg(dev->dev, "FW Client is amthi\n");
  398. if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
  399. rets = -ENODEV;
  400. goto end;
  401. }
  402. mei_cl_unlink(cl);
  403. kfree(cl);
  404. cl = NULL;
  405. dev->iamthif_open_count++;
  406. file->private_data = &dev->iamthif_cl;
  407. client = &data->out_client_properties;
  408. client->max_msg_length = me_cl->props.max_msg_length;
  409. client->protocol_version = me_cl->props.protocol_version;
  410. rets = dev->iamthif_cl.status;
  411. goto end;
  412. }
  413. /* prepare the output buffer */
  414. client = &data->out_client_properties;
  415. client->max_msg_length = me_cl->props.max_msg_length;
  416. client->protocol_version = me_cl->props.protocol_version;
  417. dev_dbg(dev->dev, "Can connect?\n");
  418. rets = mei_cl_connect(cl, file);
  419. end:
  420. mei_me_cl_put(me_cl);
  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");