acpi_dbg.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ACPI AML interfacing support
  4. *
  5. * Copyright (C) 2015, Intel Corporation
  6. * Authors: Lv Zheng <[email protected]>
  7. */
  8. /* #define DEBUG */
  9. #define pr_fmt(fmt) "ACPI: AML: " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/wait.h>
  13. #include <linux/poll.h>
  14. #include <linux/sched.h>
  15. #include <linux/kthread.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/circ_buf.h>
  19. #include <linux/acpi.h>
  20. #include "internal.h"
  21. #define ACPI_AML_BUF_ALIGN (sizeof (acpi_size))
  22. #define ACPI_AML_BUF_SIZE PAGE_SIZE
  23. #define circ_count(circ) \
  24. (CIRC_CNT((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
  25. #define circ_count_to_end(circ) \
  26. (CIRC_CNT_TO_END((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
  27. #define circ_space(circ) \
  28. (CIRC_SPACE((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
  29. #define circ_space_to_end(circ) \
  30. (CIRC_SPACE_TO_END((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
  31. #define ACPI_AML_OPENED 0x0001
  32. #define ACPI_AML_CLOSED 0x0002
  33. #define ACPI_AML_IN_USER 0x0004 /* user space is writing cmd */
  34. #define ACPI_AML_IN_KERN 0x0008 /* kernel space is reading cmd */
  35. #define ACPI_AML_OUT_USER 0x0010 /* user space is reading log */
  36. #define ACPI_AML_OUT_KERN 0x0020 /* kernel space is writing log */
  37. #define ACPI_AML_USER (ACPI_AML_IN_USER | ACPI_AML_OUT_USER)
  38. #define ACPI_AML_KERN (ACPI_AML_IN_KERN | ACPI_AML_OUT_KERN)
  39. #define ACPI_AML_BUSY (ACPI_AML_USER | ACPI_AML_KERN)
  40. #define ACPI_AML_OPEN (ACPI_AML_OPENED | ACPI_AML_CLOSED)
  41. struct acpi_aml_io {
  42. wait_queue_head_t wait;
  43. unsigned long flags;
  44. unsigned long users;
  45. struct mutex lock;
  46. struct task_struct *thread;
  47. char out_buf[ACPI_AML_BUF_SIZE] __aligned(ACPI_AML_BUF_ALIGN);
  48. struct circ_buf out_crc;
  49. char in_buf[ACPI_AML_BUF_SIZE] __aligned(ACPI_AML_BUF_ALIGN);
  50. struct circ_buf in_crc;
  51. acpi_osd_exec_callback function;
  52. void *context;
  53. unsigned long usages;
  54. };
  55. static struct acpi_aml_io acpi_aml_io;
  56. static bool acpi_aml_initialized;
  57. static struct file *acpi_aml_active_reader;
  58. static struct dentry *acpi_aml_dentry;
  59. static inline bool __acpi_aml_running(void)
  60. {
  61. return acpi_aml_io.thread ? true : false;
  62. }
  63. static inline bool __acpi_aml_access_ok(unsigned long flag)
  64. {
  65. /*
  66. * The debugger interface is in opened state (OPENED && !CLOSED),
  67. * then it is allowed to access the debugger buffers from either
  68. * user space or the kernel space.
  69. * In addition, for the kernel space, only the debugger thread
  70. * (thread ID matched) is allowed to access.
  71. */
  72. if (!(acpi_aml_io.flags & ACPI_AML_OPENED) ||
  73. (acpi_aml_io.flags & ACPI_AML_CLOSED) ||
  74. !__acpi_aml_running())
  75. return false;
  76. if ((flag & ACPI_AML_KERN) &&
  77. current != acpi_aml_io.thread)
  78. return false;
  79. return true;
  80. }
  81. static inline bool __acpi_aml_readable(struct circ_buf *circ, unsigned long flag)
  82. {
  83. /*
  84. * Another read is not in progress and there is data in buffer
  85. * available for read.
  86. */
  87. if (!(acpi_aml_io.flags & flag) && circ_count(circ))
  88. return true;
  89. return false;
  90. }
  91. static inline bool __acpi_aml_writable(struct circ_buf *circ, unsigned long flag)
  92. {
  93. /*
  94. * Another write is not in progress and there is buffer space
  95. * available for write.
  96. */
  97. if (!(acpi_aml_io.flags & flag) && circ_space(circ))
  98. return true;
  99. return false;
  100. }
  101. static inline bool __acpi_aml_busy(void)
  102. {
  103. if (acpi_aml_io.flags & ACPI_AML_BUSY)
  104. return true;
  105. return false;
  106. }
  107. static inline bool __acpi_aml_used(void)
  108. {
  109. return acpi_aml_io.usages ? true : false;
  110. }
  111. static inline bool acpi_aml_running(void)
  112. {
  113. bool ret;
  114. mutex_lock(&acpi_aml_io.lock);
  115. ret = __acpi_aml_running();
  116. mutex_unlock(&acpi_aml_io.lock);
  117. return ret;
  118. }
  119. static bool acpi_aml_busy(void)
  120. {
  121. bool ret;
  122. mutex_lock(&acpi_aml_io.lock);
  123. ret = __acpi_aml_busy();
  124. mutex_unlock(&acpi_aml_io.lock);
  125. return ret;
  126. }
  127. static bool acpi_aml_used(void)
  128. {
  129. bool ret;
  130. /*
  131. * The usage count is prepared to avoid race conditions between the
  132. * starts and the stops of the debugger thread.
  133. */
  134. mutex_lock(&acpi_aml_io.lock);
  135. ret = __acpi_aml_used();
  136. mutex_unlock(&acpi_aml_io.lock);
  137. return ret;
  138. }
  139. static bool acpi_aml_kern_readable(void)
  140. {
  141. bool ret;
  142. mutex_lock(&acpi_aml_io.lock);
  143. ret = !__acpi_aml_access_ok(ACPI_AML_IN_KERN) ||
  144. __acpi_aml_readable(&acpi_aml_io.in_crc, ACPI_AML_IN_KERN);
  145. mutex_unlock(&acpi_aml_io.lock);
  146. return ret;
  147. }
  148. static bool acpi_aml_kern_writable(void)
  149. {
  150. bool ret;
  151. mutex_lock(&acpi_aml_io.lock);
  152. ret = !__acpi_aml_access_ok(ACPI_AML_OUT_KERN) ||
  153. __acpi_aml_writable(&acpi_aml_io.out_crc, ACPI_AML_OUT_KERN);
  154. mutex_unlock(&acpi_aml_io.lock);
  155. return ret;
  156. }
  157. static bool acpi_aml_user_readable(void)
  158. {
  159. bool ret;
  160. mutex_lock(&acpi_aml_io.lock);
  161. ret = !__acpi_aml_access_ok(ACPI_AML_OUT_USER) ||
  162. __acpi_aml_readable(&acpi_aml_io.out_crc, ACPI_AML_OUT_USER);
  163. mutex_unlock(&acpi_aml_io.lock);
  164. return ret;
  165. }
  166. static bool acpi_aml_user_writable(void)
  167. {
  168. bool ret;
  169. mutex_lock(&acpi_aml_io.lock);
  170. ret = !__acpi_aml_access_ok(ACPI_AML_IN_USER) ||
  171. __acpi_aml_writable(&acpi_aml_io.in_crc, ACPI_AML_IN_USER);
  172. mutex_unlock(&acpi_aml_io.lock);
  173. return ret;
  174. }
  175. static int acpi_aml_lock_write(struct circ_buf *circ, unsigned long flag)
  176. {
  177. int ret = 0;
  178. mutex_lock(&acpi_aml_io.lock);
  179. if (!__acpi_aml_access_ok(flag)) {
  180. ret = -EFAULT;
  181. goto out;
  182. }
  183. if (!__acpi_aml_writable(circ, flag)) {
  184. ret = -EAGAIN;
  185. goto out;
  186. }
  187. acpi_aml_io.flags |= flag;
  188. out:
  189. mutex_unlock(&acpi_aml_io.lock);
  190. return ret;
  191. }
  192. static int acpi_aml_lock_read(struct circ_buf *circ, unsigned long flag)
  193. {
  194. int ret = 0;
  195. mutex_lock(&acpi_aml_io.lock);
  196. if (!__acpi_aml_access_ok(flag)) {
  197. ret = -EFAULT;
  198. goto out;
  199. }
  200. if (!__acpi_aml_readable(circ, flag)) {
  201. ret = -EAGAIN;
  202. goto out;
  203. }
  204. acpi_aml_io.flags |= flag;
  205. out:
  206. mutex_unlock(&acpi_aml_io.lock);
  207. return ret;
  208. }
  209. static void acpi_aml_unlock_fifo(unsigned long flag, bool wakeup)
  210. {
  211. mutex_lock(&acpi_aml_io.lock);
  212. acpi_aml_io.flags &= ~flag;
  213. if (wakeup)
  214. wake_up_interruptible(&acpi_aml_io.wait);
  215. mutex_unlock(&acpi_aml_io.lock);
  216. }
  217. static int acpi_aml_write_kern(const char *buf, int len)
  218. {
  219. int ret;
  220. struct circ_buf *crc = &acpi_aml_io.out_crc;
  221. int n;
  222. char *p;
  223. ret = acpi_aml_lock_write(crc, ACPI_AML_OUT_KERN);
  224. if (ret < 0)
  225. return ret;
  226. /* sync tail before inserting logs */
  227. smp_mb();
  228. p = &crc->buf[crc->head];
  229. n = min(len, circ_space_to_end(crc));
  230. memcpy(p, buf, n);
  231. /* sync head after inserting logs */
  232. smp_wmb();
  233. crc->head = (crc->head + n) & (ACPI_AML_BUF_SIZE - 1);
  234. acpi_aml_unlock_fifo(ACPI_AML_OUT_KERN, true);
  235. return n;
  236. }
  237. static int acpi_aml_readb_kern(void)
  238. {
  239. int ret;
  240. struct circ_buf *crc = &acpi_aml_io.in_crc;
  241. char *p;
  242. ret = acpi_aml_lock_read(crc, ACPI_AML_IN_KERN);
  243. if (ret < 0)
  244. return ret;
  245. /* sync head before removing cmds */
  246. smp_rmb();
  247. p = &crc->buf[crc->tail];
  248. ret = (int)*p;
  249. /* sync tail before inserting cmds */
  250. smp_mb();
  251. crc->tail = (crc->tail + 1) & (ACPI_AML_BUF_SIZE - 1);
  252. acpi_aml_unlock_fifo(ACPI_AML_IN_KERN, true);
  253. return ret;
  254. }
  255. /*
  256. * acpi_aml_write_log() - Capture debugger output
  257. * @msg: the debugger output
  258. *
  259. * This function should be used to implement acpi_os_printf() to filter out
  260. * the debugger output and store the output into the debugger interface
  261. * buffer. Return the size of stored logs or errno.
  262. */
  263. static ssize_t acpi_aml_write_log(const char *msg)
  264. {
  265. int ret = 0;
  266. int count = 0, size = 0;
  267. if (!acpi_aml_initialized)
  268. return -ENODEV;
  269. if (msg)
  270. count = strlen(msg);
  271. while (count > 0) {
  272. again:
  273. ret = acpi_aml_write_kern(msg + size, count);
  274. if (ret == -EAGAIN) {
  275. ret = wait_event_interruptible(acpi_aml_io.wait,
  276. acpi_aml_kern_writable());
  277. /*
  278. * We need to retry when the condition
  279. * becomes true.
  280. */
  281. if (ret == 0)
  282. goto again;
  283. break;
  284. }
  285. if (ret < 0)
  286. break;
  287. size += ret;
  288. count -= ret;
  289. }
  290. return size > 0 ? size : ret;
  291. }
  292. /*
  293. * acpi_aml_read_cmd() - Capture debugger input
  294. * @msg: the debugger input
  295. * @size: the size of the debugger input
  296. *
  297. * This function should be used to implement acpi_os_get_line() to capture
  298. * the debugger input commands and store the input commands into the
  299. * debugger interface buffer. Return the size of stored commands or errno.
  300. */
  301. static ssize_t acpi_aml_read_cmd(char *msg, size_t count)
  302. {
  303. int ret = 0;
  304. int size = 0;
  305. /*
  306. * This is ensured by the running fact of the debugger thread
  307. * unless a bug is introduced.
  308. */
  309. BUG_ON(!acpi_aml_initialized);
  310. while (count > 0) {
  311. again:
  312. /*
  313. * Check each input byte to find the end of the command.
  314. */
  315. ret = acpi_aml_readb_kern();
  316. if (ret == -EAGAIN) {
  317. ret = wait_event_interruptible(acpi_aml_io.wait,
  318. acpi_aml_kern_readable());
  319. /*
  320. * We need to retry when the condition becomes
  321. * true.
  322. */
  323. if (ret == 0)
  324. goto again;
  325. }
  326. if (ret < 0)
  327. break;
  328. *(msg + size) = (char)ret;
  329. size++;
  330. count--;
  331. if (ret == '\n') {
  332. /*
  333. * acpi_os_get_line() requires a zero terminated command
  334. * string.
  335. */
  336. *(msg + size - 1) = '\0';
  337. break;
  338. }
  339. }
  340. return size > 0 ? size : ret;
  341. }
  342. static int acpi_aml_thread(void *unused)
  343. {
  344. acpi_osd_exec_callback function = NULL;
  345. void *context;
  346. mutex_lock(&acpi_aml_io.lock);
  347. if (acpi_aml_io.function) {
  348. acpi_aml_io.usages++;
  349. function = acpi_aml_io.function;
  350. context = acpi_aml_io.context;
  351. }
  352. mutex_unlock(&acpi_aml_io.lock);
  353. if (function)
  354. function(context);
  355. mutex_lock(&acpi_aml_io.lock);
  356. acpi_aml_io.usages--;
  357. if (!__acpi_aml_used()) {
  358. acpi_aml_io.thread = NULL;
  359. wake_up(&acpi_aml_io.wait);
  360. }
  361. mutex_unlock(&acpi_aml_io.lock);
  362. return 0;
  363. }
  364. /*
  365. * acpi_aml_create_thread() - Create AML debugger thread
  366. * @function: the debugger thread callback
  367. * @context: the context to be passed to the debugger thread
  368. *
  369. * This function should be used to implement acpi_os_execute() which is
  370. * used by the ACPICA debugger to create the debugger thread.
  371. */
  372. static int acpi_aml_create_thread(acpi_osd_exec_callback function, void *context)
  373. {
  374. struct task_struct *t;
  375. mutex_lock(&acpi_aml_io.lock);
  376. acpi_aml_io.function = function;
  377. acpi_aml_io.context = context;
  378. mutex_unlock(&acpi_aml_io.lock);
  379. t = kthread_create(acpi_aml_thread, NULL, "aml");
  380. if (IS_ERR(t)) {
  381. pr_err("Failed to create AML debugger thread.\n");
  382. return PTR_ERR(t);
  383. }
  384. mutex_lock(&acpi_aml_io.lock);
  385. acpi_aml_io.thread = t;
  386. acpi_set_debugger_thread_id((acpi_thread_id)(unsigned long)t);
  387. wake_up_process(t);
  388. mutex_unlock(&acpi_aml_io.lock);
  389. return 0;
  390. }
  391. static int acpi_aml_wait_command_ready(bool single_step,
  392. char *buffer, size_t length)
  393. {
  394. acpi_status status;
  395. if (single_step)
  396. acpi_os_printf("\n%1c ", ACPI_DEBUGGER_EXECUTE_PROMPT);
  397. else
  398. acpi_os_printf("\n%1c ", ACPI_DEBUGGER_COMMAND_PROMPT);
  399. status = acpi_os_get_line(buffer, length, NULL);
  400. if (ACPI_FAILURE(status))
  401. return -EINVAL;
  402. return 0;
  403. }
  404. static int acpi_aml_notify_command_complete(void)
  405. {
  406. return 0;
  407. }
  408. static int acpi_aml_open(struct inode *inode, struct file *file)
  409. {
  410. int ret = 0;
  411. acpi_status status;
  412. mutex_lock(&acpi_aml_io.lock);
  413. /*
  414. * The debugger interface is being closed, no new user is allowed
  415. * during this period.
  416. */
  417. if (acpi_aml_io.flags & ACPI_AML_CLOSED) {
  418. ret = -EBUSY;
  419. goto err_lock;
  420. }
  421. if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
  422. /*
  423. * Only one reader is allowed to initiate the debugger
  424. * thread.
  425. */
  426. if (acpi_aml_active_reader) {
  427. ret = -EBUSY;
  428. goto err_lock;
  429. } else {
  430. pr_debug("Opening debugger reader.\n");
  431. acpi_aml_active_reader = file;
  432. }
  433. } else {
  434. /*
  435. * No writer is allowed unless the debugger thread is
  436. * ready.
  437. */
  438. if (!(acpi_aml_io.flags & ACPI_AML_OPENED)) {
  439. ret = -ENODEV;
  440. goto err_lock;
  441. }
  442. }
  443. if (acpi_aml_active_reader == file) {
  444. pr_debug("Opening debugger interface.\n");
  445. mutex_unlock(&acpi_aml_io.lock);
  446. pr_debug("Initializing debugger thread.\n");
  447. status = acpi_initialize_debugger();
  448. if (ACPI_FAILURE(status)) {
  449. pr_err("Failed to initialize debugger.\n");
  450. ret = -EINVAL;
  451. goto err_exit;
  452. }
  453. pr_debug("Debugger thread initialized.\n");
  454. mutex_lock(&acpi_aml_io.lock);
  455. acpi_aml_io.flags |= ACPI_AML_OPENED;
  456. acpi_aml_io.out_crc.head = acpi_aml_io.out_crc.tail = 0;
  457. acpi_aml_io.in_crc.head = acpi_aml_io.in_crc.tail = 0;
  458. pr_debug("Debugger interface opened.\n");
  459. }
  460. acpi_aml_io.users++;
  461. err_lock:
  462. if (ret < 0) {
  463. if (acpi_aml_active_reader == file)
  464. acpi_aml_active_reader = NULL;
  465. }
  466. mutex_unlock(&acpi_aml_io.lock);
  467. err_exit:
  468. return ret;
  469. }
  470. static int acpi_aml_release(struct inode *inode, struct file *file)
  471. {
  472. mutex_lock(&acpi_aml_io.lock);
  473. acpi_aml_io.users--;
  474. if (file == acpi_aml_active_reader) {
  475. pr_debug("Closing debugger reader.\n");
  476. acpi_aml_active_reader = NULL;
  477. pr_debug("Closing debugger interface.\n");
  478. acpi_aml_io.flags |= ACPI_AML_CLOSED;
  479. /*
  480. * Wake up all user space/kernel space blocked
  481. * readers/writers.
  482. */
  483. wake_up_interruptible(&acpi_aml_io.wait);
  484. mutex_unlock(&acpi_aml_io.lock);
  485. /*
  486. * Wait all user space/kernel space readers/writers to
  487. * stop so that ACPICA command loop of the debugger thread
  488. * should fail all its command line reads after this point.
  489. */
  490. wait_event(acpi_aml_io.wait, !acpi_aml_busy());
  491. /*
  492. * Then we try to terminate the debugger thread if it is
  493. * not terminated.
  494. */
  495. pr_debug("Terminating debugger thread.\n");
  496. acpi_terminate_debugger();
  497. wait_event(acpi_aml_io.wait, !acpi_aml_used());
  498. pr_debug("Debugger thread terminated.\n");
  499. mutex_lock(&acpi_aml_io.lock);
  500. acpi_aml_io.flags &= ~ACPI_AML_OPENED;
  501. }
  502. if (acpi_aml_io.users == 0) {
  503. pr_debug("Debugger interface closed.\n");
  504. acpi_aml_io.flags &= ~ACPI_AML_CLOSED;
  505. }
  506. mutex_unlock(&acpi_aml_io.lock);
  507. return 0;
  508. }
  509. static int acpi_aml_read_user(char __user *buf, int len)
  510. {
  511. int ret;
  512. struct circ_buf *crc = &acpi_aml_io.out_crc;
  513. int n;
  514. char *p;
  515. ret = acpi_aml_lock_read(crc, ACPI_AML_OUT_USER);
  516. if (ret < 0)
  517. return ret;
  518. /* sync head before removing logs */
  519. smp_rmb();
  520. p = &crc->buf[crc->tail];
  521. n = min(len, circ_count_to_end(crc));
  522. if (copy_to_user(buf, p, n)) {
  523. ret = -EFAULT;
  524. goto out;
  525. }
  526. /* sync tail after removing logs */
  527. smp_mb();
  528. crc->tail = (crc->tail + n) & (ACPI_AML_BUF_SIZE - 1);
  529. ret = n;
  530. out:
  531. acpi_aml_unlock_fifo(ACPI_AML_OUT_USER, ret >= 0);
  532. return ret;
  533. }
  534. static ssize_t acpi_aml_read(struct file *file, char __user *buf,
  535. size_t count, loff_t *ppos)
  536. {
  537. int ret = 0;
  538. int size = 0;
  539. if (!count)
  540. return 0;
  541. if (!access_ok(buf, count))
  542. return -EFAULT;
  543. while (count > 0) {
  544. again:
  545. ret = acpi_aml_read_user(buf + size, count);
  546. if (ret == -EAGAIN) {
  547. if (file->f_flags & O_NONBLOCK)
  548. break;
  549. else {
  550. ret = wait_event_interruptible(acpi_aml_io.wait,
  551. acpi_aml_user_readable());
  552. /*
  553. * We need to retry when the condition
  554. * becomes true.
  555. */
  556. if (ret == 0)
  557. goto again;
  558. }
  559. }
  560. if (ret < 0) {
  561. if (!acpi_aml_running())
  562. ret = 0;
  563. break;
  564. }
  565. if (ret) {
  566. size += ret;
  567. count -= ret;
  568. *ppos += ret;
  569. break;
  570. }
  571. }
  572. return size > 0 ? size : ret;
  573. }
  574. static int acpi_aml_write_user(const char __user *buf, int len)
  575. {
  576. int ret;
  577. struct circ_buf *crc = &acpi_aml_io.in_crc;
  578. int n;
  579. char *p;
  580. ret = acpi_aml_lock_write(crc, ACPI_AML_IN_USER);
  581. if (ret < 0)
  582. return ret;
  583. /* sync tail before inserting cmds */
  584. smp_mb();
  585. p = &crc->buf[crc->head];
  586. n = min(len, circ_space_to_end(crc));
  587. if (copy_from_user(p, buf, n)) {
  588. ret = -EFAULT;
  589. goto out;
  590. }
  591. /* sync head after inserting cmds */
  592. smp_wmb();
  593. crc->head = (crc->head + n) & (ACPI_AML_BUF_SIZE - 1);
  594. ret = n;
  595. out:
  596. acpi_aml_unlock_fifo(ACPI_AML_IN_USER, ret >= 0);
  597. return n;
  598. }
  599. static ssize_t acpi_aml_write(struct file *file, const char __user *buf,
  600. size_t count, loff_t *ppos)
  601. {
  602. int ret = 0;
  603. int size = 0;
  604. if (!count)
  605. return 0;
  606. if (!access_ok(buf, count))
  607. return -EFAULT;
  608. while (count > 0) {
  609. again:
  610. ret = acpi_aml_write_user(buf + size, count);
  611. if (ret == -EAGAIN) {
  612. if (file->f_flags & O_NONBLOCK)
  613. break;
  614. else {
  615. ret = wait_event_interruptible(acpi_aml_io.wait,
  616. acpi_aml_user_writable());
  617. /*
  618. * We need to retry when the condition
  619. * becomes true.
  620. */
  621. if (ret == 0)
  622. goto again;
  623. }
  624. }
  625. if (ret < 0) {
  626. if (!acpi_aml_running())
  627. ret = 0;
  628. break;
  629. }
  630. if (ret) {
  631. size += ret;
  632. count -= ret;
  633. *ppos += ret;
  634. }
  635. }
  636. return size > 0 ? size : ret;
  637. }
  638. static __poll_t acpi_aml_poll(struct file *file, poll_table *wait)
  639. {
  640. __poll_t masks = 0;
  641. poll_wait(file, &acpi_aml_io.wait, wait);
  642. if (acpi_aml_user_readable())
  643. masks |= EPOLLIN | EPOLLRDNORM;
  644. if (acpi_aml_user_writable())
  645. masks |= EPOLLOUT | EPOLLWRNORM;
  646. return masks;
  647. }
  648. static const struct file_operations acpi_aml_operations = {
  649. .read = acpi_aml_read,
  650. .write = acpi_aml_write,
  651. .poll = acpi_aml_poll,
  652. .open = acpi_aml_open,
  653. .release = acpi_aml_release,
  654. .llseek = generic_file_llseek,
  655. };
  656. static const struct acpi_debugger_ops acpi_aml_debugger = {
  657. .create_thread = acpi_aml_create_thread,
  658. .read_cmd = acpi_aml_read_cmd,
  659. .write_log = acpi_aml_write_log,
  660. .wait_command_ready = acpi_aml_wait_command_ready,
  661. .notify_command_complete = acpi_aml_notify_command_complete,
  662. };
  663. static int __init acpi_aml_init(void)
  664. {
  665. int ret;
  666. if (acpi_disabled)
  667. return -ENODEV;
  668. /* Initialize AML IO interface */
  669. mutex_init(&acpi_aml_io.lock);
  670. init_waitqueue_head(&acpi_aml_io.wait);
  671. acpi_aml_io.out_crc.buf = acpi_aml_io.out_buf;
  672. acpi_aml_io.in_crc.buf = acpi_aml_io.in_buf;
  673. acpi_aml_dentry = debugfs_create_file("acpidbg",
  674. S_IFREG | S_IRUGO | S_IWUSR,
  675. acpi_debugfs_dir, NULL,
  676. &acpi_aml_operations);
  677. ret = acpi_register_debugger(THIS_MODULE, &acpi_aml_debugger);
  678. if (ret) {
  679. debugfs_remove(acpi_aml_dentry);
  680. acpi_aml_dentry = NULL;
  681. return ret;
  682. }
  683. acpi_aml_initialized = true;
  684. return 0;
  685. }
  686. static void __exit acpi_aml_exit(void)
  687. {
  688. if (acpi_aml_initialized) {
  689. acpi_unregister_debugger(&acpi_aml_debugger);
  690. debugfs_remove(acpi_aml_dentry);
  691. acpi_aml_dentry = NULL;
  692. acpi_aml_initialized = false;
  693. }
  694. }
  695. module_init(acpi_aml_init);
  696. module_exit(acpi_aml_exit);
  697. MODULE_AUTHOR("Lv Zheng");
  698. MODULE_DESCRIPTION("ACPI debugger userspace IO driver");
  699. MODULE_LICENSE("GPL");