main.c 18 KB

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