main.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2011, 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/fs.h>
  21. #include <linux/errno.h>
  22. #include <linux/types.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/aio.h>
  25. #include <linux/pci.h>
  26. #include <linux/poll.h>
  27. #include <linux/init.h>
  28. #include <linux/ioctl.h>
  29. #include <linux/cdev.h>
  30. #include <linux/version.h>
  31. #include <linux/sched.h>
  32. #include <linux/uuid.h>
  33. #include <linux/compat.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/interrupt.h>
  36. #include "mei_dev.h"
  37. #include "mei.h"
  38. #include "interface.h"
  39. #include "mei_version.h"
  40. #define MEI_READ_TIMEOUT 45
  41. #define MEI_DRIVER_NAME "mei"
  42. #define MEI_DEV_NAME "mei"
  43. /*
  44. * mei driver strings
  45. */
  46. static char mei_driver_name[] = MEI_DRIVER_NAME;
  47. static const char mei_driver_string[] = "Intel(R) Management Engine Interface";
  48. static const char mei_driver_version[] = MEI_DRIVER_VERSION;
  49. /* mei char device for registration */
  50. static struct cdev mei_cdev;
  51. /* major number for device */
  52. static int mei_major;
  53. /* The device pointer */
  54. /* Currently this driver works as long as there is only a single AMT device. */
  55. static struct pci_dev *mei_device;
  56. static struct class *mei_class;
  57. /* mei_pci_tbl - PCI Device ID Table */
  58. static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = {
  59. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
  60. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
  61. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
  62. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
  63. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
  64. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
  65. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
  66. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
  67. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
  68. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
  69. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
  70. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
  71. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
  72. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
  73. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
  74. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
  75. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
  76. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
  77. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
  78. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
  79. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
  80. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
  81. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
  82. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
  83. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
  84. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
  85. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
  86. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
  87. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
  88. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
  89. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
  90. /* required last entry */
  91. {0, }
  92. };
  93. MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
  94. static DEFINE_MUTEX(mei_mutex);
  95. /**
  96. * mei_probe - Device Initialization Routine
  97. *
  98. * @pdev: PCI device structure
  99. * @ent: entry in kcs_pci_tbl
  100. *
  101. * returns 0 on success, <0 on failure.
  102. */
  103. static int __devinit mei_probe(struct pci_dev *pdev,
  104. const struct pci_device_id *ent)
  105. {
  106. struct mei_device *dev;
  107. int err;
  108. mutex_lock(&mei_mutex);
  109. if (mei_device) {
  110. err = -EEXIST;
  111. goto end;
  112. }
  113. /* enable pci dev */
  114. err = pci_enable_device(pdev);
  115. if (err) {
  116. printk(KERN_ERR "mei: Failed to enable pci device.\n");
  117. goto end;
  118. }
  119. /* set PCI host mastering */
  120. pci_set_master(pdev);
  121. /* pci request regions for mei driver */
  122. err = pci_request_regions(pdev, mei_driver_name);
  123. if (err) {
  124. printk(KERN_ERR "mei: Failed to get pci regions.\n");
  125. goto disable_device;
  126. }
  127. /* allocates and initializes the mei dev structure */
  128. dev = init_mei_device(pdev);
  129. if (!dev) {
  130. err = -ENOMEM;
  131. goto release_regions;
  132. }
  133. /* mapping IO device memory */
  134. dev->mem_addr = pci_iomap(pdev, 0, 0);
  135. if (!dev->mem_addr) {
  136. printk(KERN_ERR "mei: mapping I/O device memory failure.\n");
  137. err = -ENOMEM;
  138. goto free_device;
  139. }
  140. /* request and enable interrupt */
  141. err = request_threaded_irq(pdev->irq,
  142. mei_interrupt_quick_handler,
  143. mei_interrupt_thread_handler,
  144. IRQF_SHARED, mei_driver_name, dev);
  145. if (err) {
  146. printk(KERN_ERR "mei: request_threaded_irq failure. irq = %d\n",
  147. pdev->irq);
  148. goto unmap_memory;
  149. }
  150. INIT_DELAYED_WORK(&dev->wd_work, mei_wd_timer);
  151. if (mei_hw_init(dev)) {
  152. printk(KERN_ERR "mei: Init hw failure.\n");
  153. err = -ENODEV;
  154. goto release_irq;
  155. }
  156. mei_device = pdev;
  157. pci_set_drvdata(pdev, dev);
  158. schedule_delayed_work(&dev->wd_work, HZ);
  159. mutex_unlock(&mei_mutex);
  160. pr_debug("mei: Driver initialization successful.\n");
  161. return 0;
  162. release_irq:
  163. /* disable interrupts */
  164. dev->host_hw_state = mei_hcsr_read(dev);
  165. mei_disable_interrupts(dev);
  166. flush_scheduled_work();
  167. free_irq(pdev->irq, dev);
  168. unmap_memory:
  169. pci_iounmap(pdev, dev->mem_addr);
  170. free_device:
  171. kfree(dev);
  172. release_regions:
  173. pci_release_regions(pdev);
  174. disable_device:
  175. pci_disable_device(pdev);
  176. end:
  177. mutex_unlock(&mei_mutex);
  178. printk(KERN_ERR "mei: Driver initialization failed.\n");
  179. return err;
  180. }
  181. /**
  182. * mei_remove - Device Removal Routine
  183. *
  184. * @pdev: PCI device structure
  185. *
  186. * mei_remove is called by the PCI subsystem to alert the driver
  187. * that it should release a PCI device.
  188. */
  189. static void __devexit mei_remove(struct pci_dev *pdev)
  190. {
  191. struct mei_device *dev;
  192. if (mei_device != pdev)
  193. return;
  194. dev = pci_get_drvdata(pdev);
  195. if (!dev)
  196. return;
  197. mutex_lock(&dev->device_lock);
  198. mei_wd_stop(dev, false);
  199. mei_device = NULL;
  200. if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
  201. dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
  202. mei_disconnect_host_client(dev, &dev->iamthif_cl);
  203. }
  204. if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
  205. dev->wd_cl.state = MEI_FILE_DISCONNECTING;
  206. mei_disconnect_host_client(dev, &dev->wd_cl);
  207. }
  208. /* remove entry if already in list */
  209. dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
  210. mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id);
  211. mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id);
  212. dev->iamthif_current_cb = NULL;
  213. dev->num_mei_me_clients = 0;
  214. mutex_unlock(&dev->device_lock);
  215. flush_scheduled_work();
  216. /* disable interrupts */
  217. mei_disable_interrupts(dev);
  218. free_irq(pdev->irq, dev);
  219. pci_set_drvdata(pdev, NULL);
  220. if (dev->mem_addr)
  221. pci_iounmap(pdev, dev->mem_addr);
  222. kfree(dev);
  223. pci_release_regions(pdev);
  224. pci_disable_device(pdev);
  225. }
  226. /**
  227. * mei_clear_list - removes all callbacks associated with file
  228. * from mei_cb_list
  229. *
  230. * @dev: device structure.
  231. * @file: file structure
  232. * @mei_cb_list: callbacks list
  233. *
  234. * mei_clear_list is called to clear resources associated with file
  235. * when application calls close function or Ctrl-C was pressed
  236. *
  237. * returns true if callback removed from the list, false otherwise
  238. */
  239. static bool mei_clear_list(struct mei_device *dev,
  240. struct file *file, struct list_head *mei_cb_list)
  241. {
  242. struct mei_cl_cb *cb_pos = NULL;
  243. struct mei_cl_cb *cb_next = NULL;
  244. struct file *file_temp;
  245. bool removed = false;
  246. /* list all list member */
  247. list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, cb_list) {
  248. file_temp = (struct file *)cb_pos->file_object;
  249. /* check if list member associated with a file */
  250. if (file_temp == file) {
  251. /* remove member from the list */
  252. list_del(&cb_pos->cb_list);
  253. /* check if cb equal to current iamthif cb */
  254. if (dev->iamthif_current_cb == cb_pos) {
  255. dev->iamthif_current_cb = NULL;
  256. /* send flow control to iamthif client */
  257. mei_send_flow_control(dev, &dev->iamthif_cl);
  258. }
  259. /* free all allocated buffers */
  260. mei_free_cb_private(cb_pos);
  261. cb_pos = NULL;
  262. removed = true;
  263. }
  264. }
  265. return removed;
  266. }
  267. /**
  268. * mei_clear_lists - removes all callbacks associated with file
  269. *
  270. * @dev: device structure
  271. * @file: file structure
  272. *
  273. * mei_clear_lists is called to clear resources associated with file
  274. * when application calls close function or Ctrl-C was pressed
  275. *
  276. * returns true if callback removed from the list, false otherwise
  277. */
  278. static bool mei_clear_lists(struct mei_device *dev, struct file *file)
  279. {
  280. bool removed = false;
  281. /* remove callbacks associated with a file */
  282. mei_clear_list(dev, file, &dev->amthi_cmd_list.mei_cb.cb_list);
  283. if (mei_clear_list(dev, file,
  284. &dev->amthi_read_complete_list.mei_cb.cb_list))
  285. removed = true;
  286. mei_clear_list(dev, file, &dev->ctrl_rd_list.mei_cb.cb_list);
  287. if (mei_clear_list(dev, file, &dev->ctrl_wr_list.mei_cb.cb_list))
  288. removed = true;
  289. if (mei_clear_list(dev, file, &dev->write_waiting_list.mei_cb.cb_list))
  290. removed = true;
  291. if (mei_clear_list(dev, file, &dev->write_list.mei_cb.cb_list))
  292. removed = true;
  293. /* check if iamthif_current_cb not NULL */
  294. if (dev->iamthif_current_cb && !removed) {
  295. /* check file and iamthif current cb association */
  296. if (dev->iamthif_current_cb->file_object == file) {
  297. /* remove cb */
  298. mei_free_cb_private(dev->iamthif_current_cb);
  299. dev->iamthif_current_cb = NULL;
  300. removed = true;
  301. }
  302. }
  303. return removed;
  304. }
  305. /**
  306. * find_read_list_entry - find read list entry
  307. *
  308. * @dev: device structure
  309. * @file: pointer to file structure
  310. *
  311. * returns cb on success, NULL on error
  312. */
  313. static struct mei_cl_cb *find_read_list_entry(
  314. struct mei_device *dev,
  315. struct mei_cl *cl)
  316. {
  317. struct mei_cl_cb *cb_pos = NULL;
  318. struct mei_cl_cb *cb_next = NULL;
  319. struct mei_cl *cl_list_temp;
  320. if (!dev->read_list.status &&
  321. !list_empty(&dev->read_list.mei_cb.cb_list)) {
  322. dev_dbg(&dev->pdev->dev, "remove read_list CB\n");
  323. list_for_each_entry_safe(cb_pos, cb_next,
  324. &dev->read_list.mei_cb.cb_list, cb_list) {
  325. cl_list_temp = (struct mei_cl *)
  326. cb_pos->file_private;
  327. if (cl_list_temp &&
  328. mei_fe_same_id(cl, cl_list_temp))
  329. return cb_pos;
  330. }
  331. }
  332. return NULL;
  333. }
  334. /**
  335. * mei_open - the open function
  336. *
  337. * @inode: pointer to inode structure
  338. * @file: pointer to file structure
  339. *
  340. * returns 0 on success, <0 on error
  341. */
  342. static int mei_open(struct inode *inode, struct file *file)
  343. {
  344. struct mei_cl *cl;
  345. int if_num = iminor(inode), err;
  346. struct mei_device *dev;
  347. err = -ENODEV;
  348. if (!mei_device)
  349. goto out;
  350. dev = pci_get_drvdata(mei_device);
  351. if (if_num != MEI_MINOR_NUMBER || !dev)
  352. goto out;
  353. mutex_lock(&dev->device_lock);
  354. err = -ENOMEM;
  355. cl = mei_alloc_file_private(dev);
  356. if (!cl)
  357. goto out;
  358. err = -ENODEV;
  359. if (dev->mei_state != MEI_ENABLED) {
  360. dev_dbg(&dev->pdev->dev, "mei_state != MEI_ENABLED mei_state= %d\n",
  361. dev->mei_state);
  362. goto out_unlock;
  363. }
  364. err = -EMFILE;
  365. if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT)
  366. goto out_unlock;
  367. cl->host_client_id = find_first_zero_bit(dev->host_clients_map,
  368. MEI_CLIENTS_MAX);
  369. if (cl->host_client_id > MEI_CLIENTS_MAX)
  370. goto out_unlock;
  371. dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
  372. dev->open_handle_count++;
  373. list_add_tail(&cl->link, &dev->file_list);
  374. set_bit(cl->host_client_id, dev->host_clients_map);
  375. cl->state = MEI_FILE_INITIALIZING;
  376. cl->sm_state = 0;
  377. file->private_data = cl;
  378. mutex_unlock(&dev->device_lock);
  379. return 0;
  380. out_unlock:
  381. mutex_unlock(&dev->device_lock);
  382. kfree(cl);
  383. out:
  384. return err;
  385. }
  386. /**
  387. * mei_release - the release function
  388. *
  389. * @inode: pointer to inode structure
  390. * @file: pointer to file structure
  391. *
  392. * returns 0 on success, <0 on error
  393. */
  394. static int mei_release(struct inode *inode, struct file *file)
  395. {
  396. struct mei_cl *cl = file->private_data;
  397. struct mei_cl_cb *cb;
  398. struct mei_device *dev;
  399. int rets = 0;
  400. if (WARN_ON(!cl || !cl->dev))
  401. return -ENODEV;
  402. dev = cl->dev;
  403. mutex_lock(&dev->device_lock);
  404. if (cl != &dev->iamthif_cl) {
  405. if (cl->state == MEI_FILE_CONNECTED) {
  406. cl->state = MEI_FILE_DISCONNECTING;
  407. dev_dbg(&dev->pdev->dev,
  408. "disconnecting client host client = %d, "
  409. "ME client = %d\n",
  410. cl->host_client_id,
  411. cl->me_client_id);
  412. rets = mei_disconnect_host_client(dev, cl);
  413. }
  414. mei_flush_queues(dev, cl);
  415. dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
  416. cl->host_client_id,
  417. cl->me_client_id);
  418. if (dev->open_handle_count > 0) {
  419. clear_bit(cl->host_client_id,
  420. dev->host_clients_map);
  421. dev->open_handle_count--;
  422. }
  423. mei_remove_client_from_file_list(dev, cl->host_client_id);
  424. /* free read cb */
  425. cb = NULL;
  426. if (cl->read_cb) {
  427. cb = find_read_list_entry(dev, cl);
  428. /* Remove entry from read list */
  429. if (cb)
  430. list_del(&cb->cb_list);
  431. cb = cl->read_cb;
  432. cl->read_cb = NULL;
  433. }
  434. file->private_data = NULL;
  435. if (cb) {
  436. mei_free_cb_private(cb);
  437. cb = NULL;
  438. }
  439. kfree(cl);
  440. } else {
  441. if (dev->open_handle_count > 0)
  442. dev->open_handle_count--;
  443. if (dev->iamthif_file_object == file &&
  444. dev->iamthif_state != MEI_IAMTHIF_IDLE) {
  445. dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
  446. dev->iamthif_state);
  447. dev->iamthif_canceled = 1;
  448. if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
  449. dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
  450. run_next_iamthif_cmd(dev);
  451. }
  452. }
  453. if (mei_clear_lists(dev, file))
  454. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  455. }
  456. mutex_unlock(&dev->device_lock);
  457. return rets;
  458. }
  459. /**
  460. * mei_read - the read function.
  461. *
  462. * @file: pointer to file structure
  463. * @ubuf: pointer to user buffer
  464. * @length: buffer length
  465. * @offset: data offset in buffer
  466. *
  467. * returns >=0 data length on success , <0 on error
  468. */
  469. static ssize_t mei_read(struct file *file, char __user *ubuf,
  470. size_t length, loff_t *offset)
  471. {
  472. struct mei_cl *cl = file->private_data;
  473. struct mei_cl_cb *cb_pos = NULL;
  474. struct mei_cl_cb *cb = NULL;
  475. struct mei_device *dev;
  476. int i;
  477. int rets;
  478. int err;
  479. if (WARN_ON(!cl || !cl->dev))
  480. return -ENODEV;
  481. dev = cl->dev;
  482. mutex_lock(&dev->device_lock);
  483. if (dev->mei_state != MEI_ENABLED) {
  484. rets = -ENODEV;
  485. goto out;
  486. }
  487. if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
  488. /* Do not allow to read watchdog client */
  489. i = mei_find_me_client_index(dev, mei_wd_guid);
  490. if (i >= 0) {
  491. struct mei_me_client *me_client = &dev->me_clients[i];
  492. if (cl->me_client_id == me_client->client_id) {
  493. rets = -EBADF;
  494. goto out;
  495. }
  496. }
  497. } else {
  498. cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
  499. }
  500. if (cl == &dev->iamthif_cl) {
  501. rets = amthi_read(dev, file, ubuf, length, offset);
  502. goto out;
  503. }
  504. if (cl->read_cb && cl->read_cb->information > *offset) {
  505. cb = cl->read_cb;
  506. goto copy_buffer;
  507. } else if (cl->read_cb && cl->read_cb->information > 0 &&
  508. cl->read_cb->information <= *offset) {
  509. cb = cl->read_cb;
  510. rets = 0;
  511. goto free;
  512. } else if ((!cl->read_cb || !cl->read_cb->information) &&
  513. *offset > 0) {
  514. /*Offset needs to be cleaned for contingous reads*/
  515. *offset = 0;
  516. rets = 0;
  517. goto out;
  518. }
  519. err = mei_start_read(dev, cl);
  520. if (err && err != -EBUSY) {
  521. dev_dbg(&dev->pdev->dev,
  522. "mei start read failure with status = %d\n", err);
  523. rets = err;
  524. goto out;
  525. }
  526. if (MEI_READ_COMPLETE != cl->reading_state &&
  527. !waitqueue_active(&cl->rx_wait)) {
  528. if (file->f_flags & O_NONBLOCK) {
  529. rets = -EAGAIN;
  530. goto out;
  531. }
  532. mutex_unlock(&dev->device_lock);
  533. if (wait_event_interruptible(cl->rx_wait,
  534. (MEI_READ_COMPLETE == cl->reading_state ||
  535. MEI_FILE_INITIALIZING == cl->state ||
  536. MEI_FILE_DISCONNECTED == cl->state ||
  537. MEI_FILE_DISCONNECTING == cl->state))) {
  538. if (signal_pending(current))
  539. return -EINTR;
  540. return -ERESTARTSYS;
  541. }
  542. mutex_lock(&dev->device_lock);
  543. if (MEI_FILE_INITIALIZING == cl->state ||
  544. MEI_FILE_DISCONNECTED == cl->state ||
  545. MEI_FILE_DISCONNECTING == cl->state) {
  546. rets = -EBUSY;
  547. goto out;
  548. }
  549. }
  550. cb = cl->read_cb;
  551. if (!cb) {
  552. rets = -ENODEV;
  553. goto out;
  554. }
  555. if (cl->reading_state != MEI_READ_COMPLETE) {
  556. rets = 0;
  557. goto out;
  558. }
  559. /* now copy the data to user space */
  560. copy_buffer:
  561. dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
  562. cb->response_buffer.size);
  563. dev_dbg(&dev->pdev->dev, "cb->information - %lu\n",
  564. cb->information);
  565. if (length == 0 || ubuf == NULL || *offset > cb->information) {
  566. rets = -EMSGSIZE;
  567. goto free;
  568. }
  569. /* length is being turncated to PAGE_SIZE, however, */
  570. /* information size may be longer */
  571. length = min_t(size_t, length, (cb->information - *offset));
  572. if (copy_to_user(ubuf,
  573. cb->response_buffer.data + *offset,
  574. length)) {
  575. rets = -EFAULT;
  576. goto free;
  577. }
  578. rets = length;
  579. *offset += length;
  580. if ((unsigned long)*offset < cb->information)
  581. goto out;
  582. free:
  583. cb_pos = find_read_list_entry(dev, cl);
  584. /* Remove entry from read list */
  585. if (cb_pos)
  586. list_del(&cb_pos->cb_list);
  587. mei_free_cb_private(cb);
  588. cl->reading_state = MEI_IDLE;
  589. cl->read_cb = NULL;
  590. cl->read_pending = 0;
  591. out:
  592. dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
  593. mutex_unlock(&dev->device_lock);
  594. return rets;
  595. }
  596. /**
  597. * mei_write - the write function.
  598. *
  599. * @file: pointer to file structure
  600. * @ubuf: pointer to user buffer
  601. * @length: buffer length
  602. * @offset: data offset in buffer
  603. *
  604. * returns >=0 data length on success , <0 on error
  605. */
  606. static ssize_t mei_write(struct file *file, const char __user *ubuf,
  607. size_t length, loff_t *offset)
  608. {
  609. struct mei_cl *cl = file->private_data;
  610. struct mei_cl_cb *write_cb = NULL;
  611. struct mei_msg_hdr mei_hdr;
  612. struct mei_device *dev;
  613. unsigned long timeout = 0;
  614. int rets;
  615. int i;
  616. if (WARN_ON(!cl || !cl->dev))
  617. return -ENODEV;
  618. dev = cl->dev;
  619. mutex_lock(&dev->device_lock);
  620. if (dev->mei_state != MEI_ENABLED) {
  621. mutex_unlock(&dev->device_lock);
  622. return -ENODEV;
  623. }
  624. if (cl == &dev->iamthif_cl) {
  625. write_cb = find_amthi_read_list_entry(dev, file);
  626. if (write_cb) {
  627. timeout = write_cb->read_time +
  628. msecs_to_jiffies(IAMTHIF_READ_TIMER);
  629. if (time_after(jiffies, timeout) ||
  630. cl->reading_state == MEI_READ_COMPLETE) {
  631. *offset = 0;
  632. list_del(&write_cb->cb_list);
  633. mei_free_cb_private(write_cb);
  634. write_cb = NULL;
  635. }
  636. }
  637. }
  638. /* free entry used in read */
  639. if (cl->reading_state == MEI_READ_COMPLETE) {
  640. *offset = 0;
  641. write_cb = find_read_list_entry(dev, cl);
  642. if (write_cb) {
  643. list_del(&write_cb->cb_list);
  644. mei_free_cb_private(write_cb);
  645. write_cb = NULL;
  646. cl->reading_state = MEI_IDLE;
  647. cl->read_cb = NULL;
  648. cl->read_pending = 0;
  649. }
  650. } else if (cl->reading_state == MEI_IDLE &&
  651. !cl->read_pending)
  652. *offset = 0;
  653. write_cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
  654. if (!write_cb) {
  655. mutex_unlock(&dev->device_lock);
  656. return -ENOMEM;
  657. }
  658. write_cb->file_object = file;
  659. write_cb->file_private = cl;
  660. write_cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
  661. rets = -ENOMEM;
  662. if (!write_cb->request_buffer.data)
  663. goto unlock_dev;
  664. dev_dbg(&dev->pdev->dev, "length =%d\n", (int) length);
  665. rets = -EFAULT;
  666. if (copy_from_user(write_cb->request_buffer.data, ubuf, length))
  667. goto unlock_dev;
  668. cl->sm_state = 0;
  669. if (length == 4 &&
  670. ((memcmp(mei_wd_state_independence_msg[0],
  671. write_cb->request_buffer.data, 4) == 0) ||
  672. (memcmp(mei_wd_state_independence_msg[1],
  673. write_cb->request_buffer.data, 4) == 0) ||
  674. (memcmp(mei_wd_state_independence_msg[2],
  675. write_cb->request_buffer.data, 4) == 0)))
  676. cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
  677. INIT_LIST_HEAD(&write_cb->cb_list);
  678. if (cl == &dev->iamthif_cl) {
  679. write_cb->response_buffer.data =
  680. kmalloc(dev->iamthif_mtu, GFP_KERNEL);
  681. if (!write_cb->response_buffer.data) {
  682. rets = -ENOMEM;
  683. goto unlock_dev;
  684. }
  685. if (dev->mei_state != MEI_ENABLED) {
  686. rets = -ENODEV;
  687. goto unlock_dev;
  688. }
  689. for (i = 0; i < dev->num_mei_me_clients; i++) {
  690. if (dev->me_clients[i].client_id ==
  691. dev->iamthif_cl.me_client_id)
  692. break;
  693. }
  694. if (WARN_ON(dev->me_clients[i].client_id != cl->me_client_id)) {
  695. rets = -ENODEV;
  696. goto unlock_dev;
  697. }
  698. if (i == dev->num_mei_me_clients ||
  699. (dev->me_clients[i].client_id !=
  700. dev->iamthif_cl.me_client_id)) {
  701. rets = -ENODEV;
  702. goto unlock_dev;
  703. } else if (length > dev->me_clients[i].props.max_msg_length ||
  704. length <= 0) {
  705. rets = -EMSGSIZE;
  706. goto unlock_dev;
  707. }
  708. write_cb->response_buffer.size = dev->iamthif_mtu;
  709. write_cb->major_file_operations = MEI_IOCTL;
  710. write_cb->information = 0;
  711. write_cb->request_buffer.size = length;
  712. if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
  713. rets = -ENODEV;
  714. goto unlock_dev;
  715. }
  716. if (!list_empty(&dev->amthi_cmd_list.mei_cb.cb_list) ||
  717. dev->iamthif_state != MEI_IAMTHIF_IDLE) {
  718. dev_dbg(&dev->pdev->dev, "amthi_state = %d\n",
  719. (int) dev->iamthif_state);
  720. dev_dbg(&dev->pdev->dev, "add amthi cb to amthi cmd waiting list\n");
  721. list_add_tail(&write_cb->cb_list,
  722. &dev->amthi_cmd_list.mei_cb.cb_list);
  723. rets = length;
  724. } else {
  725. dev_dbg(&dev->pdev->dev, "call amthi write\n");
  726. rets = amthi_write(dev, write_cb);
  727. if (rets) {
  728. dev_dbg(&dev->pdev->dev, "amthi write failed with status = %d\n",
  729. rets);
  730. goto unlock_dev;
  731. }
  732. rets = length;
  733. }
  734. mutex_unlock(&dev->device_lock);
  735. return rets;
  736. }
  737. write_cb->major_file_operations = MEI_WRITE;
  738. /* make sure information is zero before we start */
  739. write_cb->information = 0;
  740. write_cb->request_buffer.size = length;
  741. dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
  742. cl->host_client_id, cl->me_client_id);
  743. if (cl->state != MEI_FILE_CONNECTED) {
  744. rets = -ENODEV;
  745. dev_dbg(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
  746. cl->host_client_id,
  747. cl->me_client_id);
  748. goto unlock_dev;
  749. }
  750. for (i = 0; i < dev->num_mei_me_clients; i++) {
  751. if (dev->me_clients[i].client_id ==
  752. cl->me_client_id)
  753. break;
  754. }
  755. if (WARN_ON(dev->me_clients[i].client_id != cl->me_client_id)) {
  756. rets = -ENODEV;
  757. goto unlock_dev;
  758. }
  759. if (i == dev->num_mei_me_clients) {
  760. rets = -ENODEV;
  761. goto unlock_dev;
  762. }
  763. if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
  764. rets = -EINVAL;
  765. goto unlock_dev;
  766. }
  767. write_cb->file_private = cl;
  768. rets = mei_flow_ctrl_creds(dev, cl);
  769. if (rets < 0)
  770. goto unlock_dev;
  771. if (rets && dev->mei_host_buffer_is_empty) {
  772. rets = 0;
  773. dev->mei_host_buffer_is_empty = 0;
  774. if (length > ((((dev->host_hw_state & H_CBD) >> 24) *
  775. sizeof(u32)) - sizeof(struct mei_msg_hdr))) {
  776. mei_hdr.length =
  777. (((dev->host_hw_state & H_CBD) >> 24) *
  778. sizeof(u32)) -
  779. sizeof(struct mei_msg_hdr);
  780. mei_hdr.msg_complete = 0;
  781. } else {
  782. mei_hdr.length = length;
  783. mei_hdr.msg_complete = 1;
  784. }
  785. mei_hdr.host_addr = cl->host_client_id;
  786. mei_hdr.me_addr = cl->me_client_id;
  787. mei_hdr.reserved = 0;
  788. dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n",
  789. *((u32 *) &mei_hdr));
  790. if (!mei_write_message(dev, &mei_hdr,
  791. (unsigned char *) (write_cb->request_buffer.data),
  792. mei_hdr.length)) {
  793. rets = -ENODEV;
  794. goto unlock_dev;
  795. }
  796. cl->writing_state = MEI_WRITING;
  797. write_cb->information = mei_hdr.length;
  798. if (mei_hdr.msg_complete) {
  799. if (mei_flow_ctrl_reduce(dev, cl)) {
  800. rets = -ENODEV;
  801. goto unlock_dev;
  802. }
  803. list_add_tail(&write_cb->cb_list,
  804. &dev->write_waiting_list.mei_cb.cb_list);
  805. } else {
  806. list_add_tail(&write_cb->cb_list,
  807. &dev->write_list.mei_cb.cb_list);
  808. }
  809. } else {
  810. write_cb->information = 0;
  811. cl->writing_state = MEI_WRITING;
  812. list_add_tail(&write_cb->cb_list,
  813. &dev->write_list.mei_cb.cb_list);
  814. }
  815. mutex_unlock(&dev->device_lock);
  816. return length;
  817. unlock_dev:
  818. mutex_unlock(&dev->device_lock);
  819. mei_free_cb_private(write_cb);
  820. return rets;
  821. }
  822. /**
  823. * mei_ioctl - the IOCTL function
  824. *
  825. * @file: pointer to file structure
  826. * @cmd: ioctl command
  827. * @data: pointer to mei message structure
  828. *
  829. * returns 0 on success , <0 on error
  830. */
  831. static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  832. {
  833. struct mei_device *dev;
  834. struct mei_cl *cl = file->private_data;
  835. struct mei_connect_client_data *connect_data = NULL;
  836. int rets;
  837. if (cmd != IOCTL_MEI_CONNECT_CLIENT)
  838. return -EINVAL;
  839. if (WARN_ON(!cl || !cl->dev))
  840. return -ENODEV;
  841. dev = cl->dev;
  842. dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
  843. mutex_lock(&dev->device_lock);
  844. if (dev->mei_state != MEI_ENABLED) {
  845. rets = -ENODEV;
  846. goto out;
  847. }
  848. dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
  849. connect_data = kzalloc(sizeof(struct mei_connect_client_data),
  850. GFP_KERNEL);
  851. if (!connect_data) {
  852. rets = -ENOMEM;
  853. goto out;
  854. }
  855. dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
  856. if (copy_from_user(connect_data, (char __user *)data,
  857. sizeof(struct mei_connect_client_data))) {
  858. dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
  859. rets = -EFAULT;
  860. goto out;
  861. }
  862. rets = mei_ioctl_connect_client(file, connect_data);
  863. /* if all is ok, copying the data back to user. */
  864. if (rets)
  865. goto out;
  866. dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
  867. if (copy_to_user((char __user *)data, connect_data,
  868. sizeof(struct mei_connect_client_data))) {
  869. dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
  870. rets = -EFAULT;
  871. goto out;
  872. }
  873. out:
  874. kfree(connect_data);
  875. mutex_unlock(&dev->device_lock);
  876. return rets;
  877. }
  878. /**
  879. * mei_compat_ioctl - the compat IOCTL function
  880. *
  881. * @file: pointer to file structure
  882. * @cmd: ioctl command
  883. * @data: pointer to mei message structure
  884. *
  885. * returns 0 on success , <0 on error
  886. */
  887. #ifdef CONFIG_COMPAT
  888. static long mei_compat_ioctl(struct file *file,
  889. unsigned int cmd, unsigned long data)
  890. {
  891. return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
  892. }
  893. #endif
  894. /**
  895. * mei_poll - the poll function
  896. *
  897. * @file: pointer to file structure
  898. * @wait: pointer to poll_table structure
  899. *
  900. * returns poll mask
  901. */
  902. static unsigned int mei_poll(struct file *file, poll_table *wait)
  903. {
  904. struct mei_cl *cl = file->private_data;
  905. struct mei_device *dev;
  906. unsigned int mask = 0;
  907. if (WARN_ON(!cl || !cl->dev))
  908. return mask;
  909. dev = cl->dev;
  910. mutex_lock(&dev->device_lock);
  911. if (dev->mei_state != MEI_ENABLED)
  912. goto out;
  913. if (cl == &dev->iamthif_cl) {
  914. mutex_unlock(&dev->device_lock);
  915. poll_wait(file, &dev->iamthif_cl.wait, wait);
  916. mutex_lock(&dev->device_lock);
  917. if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
  918. dev->iamthif_file_object == file) {
  919. mask |= (POLLIN | POLLRDNORM);
  920. dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
  921. run_next_iamthif_cmd(dev);
  922. }
  923. goto out;
  924. }
  925. mutex_unlock(&dev->device_lock);
  926. poll_wait(file, &cl->tx_wait, wait);
  927. mutex_lock(&dev->device_lock);
  928. if (MEI_WRITE_COMPLETE == cl->writing_state)
  929. mask |= (POLLIN | POLLRDNORM);
  930. out:
  931. mutex_unlock(&dev->device_lock);
  932. return mask;
  933. }
  934. #ifdef CONFIG_PM
  935. static int mei_pci_suspend(struct device *device)
  936. {
  937. struct pci_dev *pdev = to_pci_dev(device);
  938. struct mei_device *dev = pci_get_drvdata(pdev);
  939. int err;
  940. if (!dev)
  941. return -ENODEV;
  942. mutex_lock(&dev->device_lock);
  943. /* Stop watchdog if exists */
  944. err = mei_wd_stop(dev, true);
  945. /* Set new mei state */
  946. if (dev->mei_state == MEI_ENABLED ||
  947. dev->mei_state == MEI_RECOVERING_FROM_RESET) {
  948. dev->mei_state = MEI_POWER_DOWN;
  949. mei_reset(dev, 0);
  950. }
  951. mutex_unlock(&dev->device_lock);
  952. free_irq(pdev->irq, dev);
  953. return err;
  954. }
  955. static int mei_pci_resume(struct device *device)
  956. {
  957. struct pci_dev *pdev = to_pci_dev(device);
  958. struct mei_device *dev;
  959. int err;
  960. dev = pci_get_drvdata(pdev);
  961. if (!dev)
  962. return -ENODEV;
  963. /* request and enable interrupt */
  964. err = request_threaded_irq(pdev->irq,
  965. mei_interrupt_quick_handler,
  966. mei_interrupt_thread_handler,
  967. IRQF_SHARED, mei_driver_name, dev);
  968. if (err) {
  969. printk(KERN_ERR "mei: Request_irq failure. irq = %d\n",
  970. pdev->irq);
  971. return err;
  972. }
  973. mutex_lock(&dev->device_lock);
  974. dev->mei_state = MEI_POWER_UP;
  975. mei_reset(dev, 1);
  976. mutex_unlock(&dev->device_lock);
  977. /* Start watchdog if stopped in suspend */
  978. if (dev->wd_timeout) {
  979. mei_wd_start_setup(dev);
  980. dev->wd_due_counter = 1;
  981. schedule_delayed_work(&dev->wd_work, HZ);
  982. }
  983. return err;
  984. }
  985. static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
  986. #define MEI_PM_OPS (&mei_pm_ops)
  987. #else
  988. #define MEI_PM_OPS NULL
  989. #endif /* CONFIG_PM */
  990. /*
  991. * PCI driver structure
  992. */
  993. static struct pci_driver mei_driver = {
  994. .name = mei_driver_name,
  995. .id_table = mei_pci_tbl,
  996. .probe = mei_probe,
  997. .remove = __devexit_p(mei_remove),
  998. .shutdown = __devexit_p(mei_remove),
  999. .driver.pm = MEI_PM_OPS,
  1000. };
  1001. /*
  1002. * file operations structure will be used for mei char device.
  1003. */
  1004. static const struct file_operations mei_fops = {
  1005. .owner = THIS_MODULE,
  1006. .read = mei_read,
  1007. .unlocked_ioctl = mei_ioctl,
  1008. #ifdef CONFIG_COMPAT
  1009. .compat_ioctl = mei_compat_ioctl,
  1010. #endif
  1011. .open = mei_open,
  1012. .release = mei_release,
  1013. .write = mei_write,
  1014. .poll = mei_poll,
  1015. };
  1016. /**
  1017. * mei_registration_cdev - sets up the cdev structure for mei device.
  1018. *
  1019. * @dev: char device struct
  1020. * @hminor: minor number for registration char device
  1021. * @fops: file operations structure
  1022. *
  1023. * returns 0 on success, <0 on failure.
  1024. */
  1025. static int mei_registration_cdev(struct cdev *dev, int hminor,
  1026. const struct file_operations *fops)
  1027. {
  1028. int ret, devno = MKDEV(mei_major, hminor);
  1029. cdev_init(dev, fops);
  1030. dev->owner = THIS_MODULE;
  1031. ret = cdev_add(dev, devno, 1);
  1032. /* Fail gracefully if need be */
  1033. if (ret)
  1034. printk(KERN_ERR "mei: Error %d registering mei device %d\n",
  1035. ret, hminor);
  1036. return ret;
  1037. }
  1038. /**
  1039. * mei_register_cdev - registers mei char device
  1040. *
  1041. * returns 0 on success, <0 on failure.
  1042. */
  1043. static int mei_register_cdev(void)
  1044. {
  1045. int ret;
  1046. dev_t dev;
  1047. /* registration of char devices */
  1048. ret = alloc_chrdev_region(&dev, MEI_MINORS_BASE, MEI_MINORS_COUNT,
  1049. MEI_DRIVER_NAME);
  1050. if (ret) {
  1051. printk(KERN_ERR "mei: Error allocating char device region.\n");
  1052. return ret;
  1053. }
  1054. mei_major = MAJOR(dev);
  1055. ret = mei_registration_cdev(&mei_cdev, MEI_MINOR_NUMBER,
  1056. &mei_fops);
  1057. if (ret)
  1058. unregister_chrdev_region(MKDEV(mei_major, MEI_MINORS_BASE),
  1059. MEI_MINORS_COUNT);
  1060. return ret;
  1061. }
  1062. /**
  1063. * mei_unregister_cdev - unregisters mei char device
  1064. */
  1065. static void mei_unregister_cdev(void)
  1066. {
  1067. cdev_del(&mei_cdev);
  1068. unregister_chrdev_region(MKDEV(mei_major, MEI_MINORS_BASE),
  1069. MEI_MINORS_COUNT);
  1070. }
  1071. /**
  1072. * mei_sysfs_device_create - adds device entry to sysfs
  1073. *
  1074. * returns 0 on success, <0 on failure.
  1075. */
  1076. static int mei_sysfs_device_create(void)
  1077. {
  1078. struct class *class;
  1079. void *tmphdev;
  1080. int err;
  1081. class = class_create(THIS_MODULE, MEI_DRIVER_NAME);
  1082. if (IS_ERR(class)) {
  1083. err = PTR_ERR(class);
  1084. printk(KERN_ERR "mei: Error creating mei class.\n");
  1085. goto err_out;
  1086. }
  1087. tmphdev = device_create(class, NULL, mei_cdev.dev, NULL,
  1088. MEI_DEV_NAME);
  1089. if (IS_ERR(tmphdev)) {
  1090. err = PTR_ERR(tmphdev);
  1091. goto err_destroy;
  1092. }
  1093. mei_class = class;
  1094. return 0;
  1095. err_destroy:
  1096. class_destroy(class);
  1097. err_out:
  1098. return err;
  1099. }
  1100. /**
  1101. * mei_sysfs_device_remove - unregisters the device entry on sysfs
  1102. */
  1103. static void mei_sysfs_device_remove(void)
  1104. {
  1105. if (IS_ERR_OR_NULL(mei_class))
  1106. return;
  1107. device_destroy(mei_class, mei_cdev.dev);
  1108. class_destroy(mei_class);
  1109. }
  1110. /**
  1111. * mei_init_module - Driver Registration Routine
  1112. *
  1113. * mei_init_module is the first routine called when the driver is
  1114. * loaded. All it does is to register with the PCI subsystem.
  1115. *
  1116. * returns 0 on success, <0 on failure.
  1117. */
  1118. static int __init mei_init_module(void)
  1119. {
  1120. int ret;
  1121. pr_debug("mei: %s - version %s\n",
  1122. mei_driver_string, mei_driver_version);
  1123. /* init pci module */
  1124. ret = pci_register_driver(&mei_driver);
  1125. if (ret < 0) {
  1126. printk(KERN_ERR "mei: Error registering driver.\n");
  1127. goto end;
  1128. }
  1129. ret = mei_register_cdev();
  1130. if (ret)
  1131. goto unregister_pci;
  1132. ret = mei_sysfs_device_create();
  1133. if (ret)
  1134. goto unregister_cdev;
  1135. return ret;
  1136. unregister_cdev:
  1137. mei_unregister_cdev();
  1138. unregister_pci:
  1139. pci_unregister_driver(&mei_driver);
  1140. end:
  1141. return ret;
  1142. }
  1143. module_init(mei_init_module);
  1144. /**
  1145. * mei_exit_module - Driver Exit Cleanup Routine
  1146. *
  1147. * mei_exit_module is called just before the driver is removed
  1148. * from memory.
  1149. */
  1150. static void __exit mei_exit_module(void)
  1151. {
  1152. pci_unregister_driver(&mei_driver);
  1153. mei_sysfs_device_remove();
  1154. mei_unregister_cdev();
  1155. pr_debug("mei: Driver unloaded successfully.\n");
  1156. }
  1157. module_exit(mei_exit_module);
  1158. MODULE_AUTHOR("Intel Corporation");
  1159. MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
  1160. MODULE_LICENSE("GPL v2");
  1161. MODULE_VERSION(MEI_DRIVER_VERSION);