main.c 18 KB

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