main.c 19 KB

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