xref: /linux/drivers/acpi/acpi_dbg.c (revision cdd38c5f1ce4398ec58fec95904b75824daab7b5)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28cfb0cdfSLv Zheng /*
38cfb0cdfSLv Zheng  * ACPI AML interfacing support
48cfb0cdfSLv Zheng  *
58cfb0cdfSLv Zheng  * Copyright (C) 2015, Intel Corporation
68cfb0cdfSLv Zheng  * Authors: Lv Zheng <lv.zheng@intel.com>
78cfb0cdfSLv Zheng  */
88cfb0cdfSLv Zheng 
98cfb0cdfSLv Zheng /* #define DEBUG */
108cfb0cdfSLv Zheng #define pr_fmt(fmt) "ACPI: AML: " fmt
118cfb0cdfSLv Zheng 
128cfb0cdfSLv Zheng #include <linux/kernel.h>
138cfb0cdfSLv Zheng #include <linux/module.h>
148cfb0cdfSLv Zheng #include <linux/wait.h>
158cfb0cdfSLv Zheng #include <linux/poll.h>
168cfb0cdfSLv Zheng #include <linux/sched.h>
178cfb0cdfSLv Zheng #include <linux/kthread.h>
188cfb0cdfSLv Zheng #include <linux/proc_fs.h>
198cfb0cdfSLv Zheng #include <linux/debugfs.h>
208cfb0cdfSLv Zheng #include <linux/circ_buf.h>
21836d0830SLv Zheng #include <linux/acpi.h>
228cfb0cdfSLv Zheng #include "internal.h"
238cfb0cdfSLv Zheng 
248cfb0cdfSLv Zheng #define ACPI_AML_BUF_ALIGN	(sizeof (acpi_size))
258cfb0cdfSLv Zheng #define ACPI_AML_BUF_SIZE	PAGE_SIZE
268cfb0cdfSLv Zheng 
278cfb0cdfSLv Zheng #define circ_count(circ) \
288cfb0cdfSLv Zheng 	(CIRC_CNT((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
298cfb0cdfSLv Zheng #define circ_count_to_end(circ) \
308cfb0cdfSLv Zheng 	(CIRC_CNT_TO_END((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
318cfb0cdfSLv Zheng #define circ_space(circ) \
328cfb0cdfSLv Zheng 	(CIRC_SPACE((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
338cfb0cdfSLv Zheng #define circ_space_to_end(circ) \
348cfb0cdfSLv Zheng 	(CIRC_SPACE_TO_END((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
358cfb0cdfSLv Zheng 
368cfb0cdfSLv Zheng #define ACPI_AML_OPENED		0x0001
378cfb0cdfSLv Zheng #define ACPI_AML_CLOSED		0x0002
388cfb0cdfSLv Zheng #define ACPI_AML_IN_USER	0x0004 /* user space is writing cmd */
398cfb0cdfSLv Zheng #define ACPI_AML_IN_KERN	0x0008 /* kernel space is reading cmd */
408cfb0cdfSLv Zheng #define ACPI_AML_OUT_USER	0x0010 /* user space is reading log */
418cfb0cdfSLv Zheng #define ACPI_AML_OUT_KERN	0x0020 /* kernel space is writing log */
428cfb0cdfSLv Zheng #define ACPI_AML_USER		(ACPI_AML_IN_USER | ACPI_AML_OUT_USER)
438cfb0cdfSLv Zheng #define ACPI_AML_KERN		(ACPI_AML_IN_KERN | ACPI_AML_OUT_KERN)
448cfb0cdfSLv Zheng #define ACPI_AML_BUSY		(ACPI_AML_USER | ACPI_AML_KERN)
458cfb0cdfSLv Zheng #define ACPI_AML_OPEN		(ACPI_AML_OPENED | ACPI_AML_CLOSED)
468cfb0cdfSLv Zheng 
478cfb0cdfSLv Zheng struct acpi_aml_io {
488cfb0cdfSLv Zheng 	wait_queue_head_t wait;
498cfb0cdfSLv Zheng 	unsigned long flags;
508cfb0cdfSLv Zheng 	unsigned long users;
518cfb0cdfSLv Zheng 	struct mutex lock;
528cfb0cdfSLv Zheng 	struct task_struct *thread;
538cfb0cdfSLv Zheng 	char out_buf[ACPI_AML_BUF_SIZE] __aligned(ACPI_AML_BUF_ALIGN);
548cfb0cdfSLv Zheng 	struct circ_buf out_crc;
558cfb0cdfSLv Zheng 	char in_buf[ACPI_AML_BUF_SIZE] __aligned(ACPI_AML_BUF_ALIGN);
568cfb0cdfSLv Zheng 	struct circ_buf in_crc;
578cfb0cdfSLv Zheng 	acpi_osd_exec_callback function;
588cfb0cdfSLv Zheng 	void *context;
598cfb0cdfSLv Zheng 	unsigned long usages;
608cfb0cdfSLv Zheng };
618cfb0cdfSLv Zheng 
628cfb0cdfSLv Zheng static struct acpi_aml_io acpi_aml_io;
638cfb0cdfSLv Zheng static bool acpi_aml_initialized;
648cfb0cdfSLv Zheng static struct file *acpi_aml_active_reader;
658cfb0cdfSLv Zheng static struct dentry *acpi_aml_dentry;
668cfb0cdfSLv Zheng 
__acpi_aml_running(void)678cfb0cdfSLv Zheng static inline bool __acpi_aml_running(void)
688cfb0cdfSLv Zheng {
698cfb0cdfSLv Zheng 	return acpi_aml_io.thread ? true : false;
708cfb0cdfSLv Zheng }
718cfb0cdfSLv Zheng 
__acpi_aml_access_ok(unsigned long flag)728cfb0cdfSLv Zheng static inline bool __acpi_aml_access_ok(unsigned long flag)
738cfb0cdfSLv Zheng {
748cfb0cdfSLv Zheng 	/*
758cfb0cdfSLv Zheng 	 * The debugger interface is in opened state (OPENED && !CLOSED),
768cfb0cdfSLv Zheng 	 * then it is allowed to access the debugger buffers from either
778cfb0cdfSLv Zheng 	 * user space or the kernel space.
788cfb0cdfSLv Zheng 	 * In addition, for the kernel space, only the debugger thread
798cfb0cdfSLv Zheng 	 * (thread ID matched) is allowed to access.
808cfb0cdfSLv Zheng 	 */
818cfb0cdfSLv Zheng 	if (!(acpi_aml_io.flags & ACPI_AML_OPENED) ||
828cfb0cdfSLv Zheng 	    (acpi_aml_io.flags & ACPI_AML_CLOSED) ||
838cfb0cdfSLv Zheng 	    !__acpi_aml_running())
848cfb0cdfSLv Zheng 		return false;
858cfb0cdfSLv Zheng 	if ((flag & ACPI_AML_KERN) &&
868cfb0cdfSLv Zheng 	    current != acpi_aml_io.thread)
878cfb0cdfSLv Zheng 		return false;
888cfb0cdfSLv Zheng 	return true;
898cfb0cdfSLv Zheng }
908cfb0cdfSLv Zheng 
__acpi_aml_readable(struct circ_buf * circ,unsigned long flag)918cfb0cdfSLv Zheng static inline bool __acpi_aml_readable(struct circ_buf *circ, unsigned long flag)
928cfb0cdfSLv Zheng {
938cfb0cdfSLv Zheng 	/*
948cfb0cdfSLv Zheng 	 * Another read is not in progress and there is data in buffer
958cfb0cdfSLv Zheng 	 * available for read.
968cfb0cdfSLv Zheng 	 */
978cfb0cdfSLv Zheng 	if (!(acpi_aml_io.flags & flag) && circ_count(circ))
988cfb0cdfSLv Zheng 		return true;
998cfb0cdfSLv Zheng 	return false;
1008cfb0cdfSLv Zheng }
1018cfb0cdfSLv Zheng 
__acpi_aml_writable(struct circ_buf * circ,unsigned long flag)1028cfb0cdfSLv Zheng static inline bool __acpi_aml_writable(struct circ_buf *circ, unsigned long flag)
1038cfb0cdfSLv Zheng {
1048cfb0cdfSLv Zheng 	/*
1058cfb0cdfSLv Zheng 	 * Another write is not in progress and there is buffer space
1068cfb0cdfSLv Zheng 	 * available for write.
1078cfb0cdfSLv Zheng 	 */
1088cfb0cdfSLv Zheng 	if (!(acpi_aml_io.flags & flag) && circ_space(circ))
1098cfb0cdfSLv Zheng 		return true;
1108cfb0cdfSLv Zheng 	return false;
1118cfb0cdfSLv Zheng }
1128cfb0cdfSLv Zheng 
__acpi_aml_busy(void)1138cfb0cdfSLv Zheng static inline bool __acpi_aml_busy(void)
1148cfb0cdfSLv Zheng {
1158cfb0cdfSLv Zheng 	if (acpi_aml_io.flags & ACPI_AML_BUSY)
1168cfb0cdfSLv Zheng 		return true;
1178cfb0cdfSLv Zheng 	return false;
1188cfb0cdfSLv Zheng }
1198cfb0cdfSLv Zheng 
__acpi_aml_used(void)1208cfb0cdfSLv Zheng static inline bool __acpi_aml_used(void)
1218cfb0cdfSLv Zheng {
1228cfb0cdfSLv Zheng 	return acpi_aml_io.usages ? true : false;
1238cfb0cdfSLv Zheng }
1248cfb0cdfSLv Zheng 
acpi_aml_running(void)1258cfb0cdfSLv Zheng static inline bool acpi_aml_running(void)
1268cfb0cdfSLv Zheng {
1278cfb0cdfSLv Zheng 	bool ret;
1288cfb0cdfSLv Zheng 
1298cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
1308cfb0cdfSLv Zheng 	ret = __acpi_aml_running();
1318cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
1328cfb0cdfSLv Zheng 	return ret;
1338cfb0cdfSLv Zheng }
1348cfb0cdfSLv Zheng 
acpi_aml_busy(void)1358cfb0cdfSLv Zheng static bool acpi_aml_busy(void)
1368cfb0cdfSLv Zheng {
1378cfb0cdfSLv Zheng 	bool ret;
1388cfb0cdfSLv Zheng 
1398cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
1408cfb0cdfSLv Zheng 	ret = __acpi_aml_busy();
1418cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
1428cfb0cdfSLv Zheng 	return ret;
1438cfb0cdfSLv Zheng }
1448cfb0cdfSLv Zheng 
acpi_aml_used(void)1458cfb0cdfSLv Zheng static bool acpi_aml_used(void)
1468cfb0cdfSLv Zheng {
1478cfb0cdfSLv Zheng 	bool ret;
1488cfb0cdfSLv Zheng 
1498cfb0cdfSLv Zheng 	/*
1508cfb0cdfSLv Zheng 	 * The usage count is prepared to avoid race conditions between the
1518cfb0cdfSLv Zheng 	 * starts and the stops of the debugger thread.
1528cfb0cdfSLv Zheng 	 */
1538cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
1548cfb0cdfSLv Zheng 	ret = __acpi_aml_used();
1558cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
1568cfb0cdfSLv Zheng 	return ret;
1578cfb0cdfSLv Zheng }
1588cfb0cdfSLv Zheng 
acpi_aml_kern_readable(void)1598cfb0cdfSLv Zheng static bool acpi_aml_kern_readable(void)
1608cfb0cdfSLv Zheng {
1618cfb0cdfSLv Zheng 	bool ret;
1628cfb0cdfSLv Zheng 
1638cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
1648cfb0cdfSLv Zheng 	ret = !__acpi_aml_access_ok(ACPI_AML_IN_KERN) ||
1658cfb0cdfSLv Zheng 	      __acpi_aml_readable(&acpi_aml_io.in_crc, ACPI_AML_IN_KERN);
1668cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
1678cfb0cdfSLv Zheng 	return ret;
1688cfb0cdfSLv Zheng }
1698cfb0cdfSLv Zheng 
acpi_aml_kern_writable(void)1708cfb0cdfSLv Zheng static bool acpi_aml_kern_writable(void)
1718cfb0cdfSLv Zheng {
1728cfb0cdfSLv Zheng 	bool ret;
1738cfb0cdfSLv Zheng 
1748cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
1758cfb0cdfSLv Zheng 	ret = !__acpi_aml_access_ok(ACPI_AML_OUT_KERN) ||
1768cfb0cdfSLv Zheng 	      __acpi_aml_writable(&acpi_aml_io.out_crc, ACPI_AML_OUT_KERN);
1778cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
1788cfb0cdfSLv Zheng 	return ret;
1798cfb0cdfSLv Zheng }
1808cfb0cdfSLv Zheng 
acpi_aml_user_readable(void)1818cfb0cdfSLv Zheng static bool acpi_aml_user_readable(void)
1828cfb0cdfSLv Zheng {
1838cfb0cdfSLv Zheng 	bool ret;
1848cfb0cdfSLv Zheng 
1858cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
1868cfb0cdfSLv Zheng 	ret = !__acpi_aml_access_ok(ACPI_AML_OUT_USER) ||
1878cfb0cdfSLv Zheng 	      __acpi_aml_readable(&acpi_aml_io.out_crc, ACPI_AML_OUT_USER);
1888cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
1898cfb0cdfSLv Zheng 	return ret;
1908cfb0cdfSLv Zheng }
1918cfb0cdfSLv Zheng 
acpi_aml_user_writable(void)1928cfb0cdfSLv Zheng static bool acpi_aml_user_writable(void)
1938cfb0cdfSLv Zheng {
1948cfb0cdfSLv Zheng 	bool ret;
1958cfb0cdfSLv Zheng 
1968cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
1978cfb0cdfSLv Zheng 	ret = !__acpi_aml_access_ok(ACPI_AML_IN_USER) ||
1988cfb0cdfSLv Zheng 	      __acpi_aml_writable(&acpi_aml_io.in_crc, ACPI_AML_IN_USER);
1998cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
2008cfb0cdfSLv Zheng 	return ret;
2018cfb0cdfSLv Zheng }
2028cfb0cdfSLv Zheng 
acpi_aml_lock_write(struct circ_buf * circ,unsigned long flag)2038cfb0cdfSLv Zheng static int acpi_aml_lock_write(struct circ_buf *circ, unsigned long flag)
2048cfb0cdfSLv Zheng {
2058cfb0cdfSLv Zheng 	int ret = 0;
2068cfb0cdfSLv Zheng 
2078cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
2088cfb0cdfSLv Zheng 	if (!__acpi_aml_access_ok(flag)) {
2098cfb0cdfSLv Zheng 		ret = -EFAULT;
2108cfb0cdfSLv Zheng 		goto out;
2118cfb0cdfSLv Zheng 	}
2128cfb0cdfSLv Zheng 	if (!__acpi_aml_writable(circ, flag)) {
2138cfb0cdfSLv Zheng 		ret = -EAGAIN;
2148cfb0cdfSLv Zheng 		goto out;
2158cfb0cdfSLv Zheng 	}
2168cfb0cdfSLv Zheng 	acpi_aml_io.flags |= flag;
2178cfb0cdfSLv Zheng out:
2188cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
2198cfb0cdfSLv Zheng 	return ret;
2208cfb0cdfSLv Zheng }
2218cfb0cdfSLv Zheng 
acpi_aml_lock_read(struct circ_buf * circ,unsigned long flag)2228cfb0cdfSLv Zheng static int acpi_aml_lock_read(struct circ_buf *circ, unsigned long flag)
2238cfb0cdfSLv Zheng {
2248cfb0cdfSLv Zheng 	int ret = 0;
2258cfb0cdfSLv Zheng 
2268cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
2278cfb0cdfSLv Zheng 	if (!__acpi_aml_access_ok(flag)) {
2288cfb0cdfSLv Zheng 		ret = -EFAULT;
2298cfb0cdfSLv Zheng 		goto out;
2308cfb0cdfSLv Zheng 	}
2318cfb0cdfSLv Zheng 	if (!__acpi_aml_readable(circ, flag)) {
2328cfb0cdfSLv Zheng 		ret = -EAGAIN;
2338cfb0cdfSLv Zheng 		goto out;
2348cfb0cdfSLv Zheng 	}
2358cfb0cdfSLv Zheng 	acpi_aml_io.flags |= flag;
2368cfb0cdfSLv Zheng out:
2378cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
2388cfb0cdfSLv Zheng 	return ret;
2398cfb0cdfSLv Zheng }
2408cfb0cdfSLv Zheng 
acpi_aml_unlock_fifo(unsigned long flag,bool wakeup)2418cfb0cdfSLv Zheng static void acpi_aml_unlock_fifo(unsigned long flag, bool wakeup)
2428cfb0cdfSLv Zheng {
2438cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
2448cfb0cdfSLv Zheng 	acpi_aml_io.flags &= ~flag;
2458cfb0cdfSLv Zheng 	if (wakeup)
2468cfb0cdfSLv Zheng 		wake_up_interruptible(&acpi_aml_io.wait);
2478cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
2488cfb0cdfSLv Zheng }
2498cfb0cdfSLv Zheng 
acpi_aml_write_kern(const char * buf,int len)2508cfb0cdfSLv Zheng static int acpi_aml_write_kern(const char *buf, int len)
2518cfb0cdfSLv Zheng {
2528cfb0cdfSLv Zheng 	int ret;
2538cfb0cdfSLv Zheng 	struct circ_buf *crc = &acpi_aml_io.out_crc;
2548cfb0cdfSLv Zheng 	int n;
2558cfb0cdfSLv Zheng 	char *p;
2568cfb0cdfSLv Zheng 
2578cfb0cdfSLv Zheng 	ret = acpi_aml_lock_write(crc, ACPI_AML_OUT_KERN);
258287980e4SArnd Bergmann 	if (ret < 0)
2598cfb0cdfSLv Zheng 		return ret;
2608cfb0cdfSLv Zheng 	/* sync tail before inserting logs */
2618cfb0cdfSLv Zheng 	smp_mb();
2628cfb0cdfSLv Zheng 	p = &crc->buf[crc->head];
2638cfb0cdfSLv Zheng 	n = min(len, circ_space_to_end(crc));
2648cfb0cdfSLv Zheng 	memcpy(p, buf, n);
2658cfb0cdfSLv Zheng 	/* sync head after inserting logs */
2668cfb0cdfSLv Zheng 	smp_wmb();
2678cfb0cdfSLv Zheng 	crc->head = (crc->head + n) & (ACPI_AML_BUF_SIZE - 1);
2688cfb0cdfSLv Zheng 	acpi_aml_unlock_fifo(ACPI_AML_OUT_KERN, true);
2698cfb0cdfSLv Zheng 	return n;
2708cfb0cdfSLv Zheng }
2718cfb0cdfSLv Zheng 
acpi_aml_readb_kern(void)2728cfb0cdfSLv Zheng static int acpi_aml_readb_kern(void)
2738cfb0cdfSLv Zheng {
2748cfb0cdfSLv Zheng 	int ret;
2758cfb0cdfSLv Zheng 	struct circ_buf *crc = &acpi_aml_io.in_crc;
2768cfb0cdfSLv Zheng 	char *p;
2778cfb0cdfSLv Zheng 
2788cfb0cdfSLv Zheng 	ret = acpi_aml_lock_read(crc, ACPI_AML_IN_KERN);
279287980e4SArnd Bergmann 	if (ret < 0)
2808cfb0cdfSLv Zheng 		return ret;
2818cfb0cdfSLv Zheng 	/* sync head before removing cmds */
2828cfb0cdfSLv Zheng 	smp_rmb();
2838cfb0cdfSLv Zheng 	p = &crc->buf[crc->tail];
2848cfb0cdfSLv Zheng 	ret = (int)*p;
2858cfb0cdfSLv Zheng 	/* sync tail before inserting cmds */
2868cfb0cdfSLv Zheng 	smp_mb();
2878cfb0cdfSLv Zheng 	crc->tail = (crc->tail + 1) & (ACPI_AML_BUF_SIZE - 1);
2888cfb0cdfSLv Zheng 	acpi_aml_unlock_fifo(ACPI_AML_IN_KERN, true);
2898cfb0cdfSLv Zheng 	return ret;
2908cfb0cdfSLv Zheng }
2918cfb0cdfSLv Zheng 
2928cfb0cdfSLv Zheng /*
2938cfb0cdfSLv Zheng  * acpi_aml_write_log() - Capture debugger output
2948cfb0cdfSLv Zheng  * @msg: the debugger output
2958cfb0cdfSLv Zheng  *
2968cfb0cdfSLv Zheng  * This function should be used to implement acpi_os_printf() to filter out
2978cfb0cdfSLv Zheng  * the debugger output and store the output into the debugger interface
2988cfb0cdfSLv Zheng  * buffer. Return the size of stored logs or errno.
2998cfb0cdfSLv Zheng  */
acpi_aml_write_log(const char * msg)300836d0830SLv Zheng static ssize_t acpi_aml_write_log(const char *msg)
3018cfb0cdfSLv Zheng {
3028cfb0cdfSLv Zheng 	int ret = 0;
3038cfb0cdfSLv Zheng 	int count = 0, size = 0;
3048cfb0cdfSLv Zheng 
3058cfb0cdfSLv Zheng 	if (!acpi_aml_initialized)
3068cfb0cdfSLv Zheng 		return -ENODEV;
3078cfb0cdfSLv Zheng 	if (msg)
3088cfb0cdfSLv Zheng 		count = strlen(msg);
3098cfb0cdfSLv Zheng 	while (count > 0) {
3108cfb0cdfSLv Zheng again:
3118cfb0cdfSLv Zheng 		ret = acpi_aml_write_kern(msg + size, count);
3128cfb0cdfSLv Zheng 		if (ret == -EAGAIN) {
3138cfb0cdfSLv Zheng 			ret = wait_event_interruptible(acpi_aml_io.wait,
3148cfb0cdfSLv Zheng 				acpi_aml_kern_writable());
3158cfb0cdfSLv Zheng 			/*
3168cfb0cdfSLv Zheng 			 * We need to retry when the condition
3178cfb0cdfSLv Zheng 			 * becomes true.
3188cfb0cdfSLv Zheng 			 */
3198cfb0cdfSLv Zheng 			if (ret == 0)
3208cfb0cdfSLv Zheng 				goto again;
3218cfb0cdfSLv Zheng 			break;
3228cfb0cdfSLv Zheng 		}
323287980e4SArnd Bergmann 		if (ret < 0)
3248cfb0cdfSLv Zheng 			break;
3258cfb0cdfSLv Zheng 		size += ret;
3268cfb0cdfSLv Zheng 		count -= ret;
3278cfb0cdfSLv Zheng 	}
3288cfb0cdfSLv Zheng 	return size > 0 ? size : ret;
3298cfb0cdfSLv Zheng }
3308cfb0cdfSLv Zheng 
3318cfb0cdfSLv Zheng /*
3328cfb0cdfSLv Zheng  * acpi_aml_read_cmd() - Capture debugger input
3338cfb0cdfSLv Zheng  * @msg: the debugger input
3348cfb0cdfSLv Zheng  * @size: the size of the debugger input
3358cfb0cdfSLv Zheng  *
3368cfb0cdfSLv Zheng  * This function should be used to implement acpi_os_get_line() to capture
3378cfb0cdfSLv Zheng  * the debugger input commands and store the input commands into the
3388cfb0cdfSLv Zheng  * debugger interface buffer. Return the size of stored commands or errno.
3398cfb0cdfSLv Zheng  */
acpi_aml_read_cmd(char * msg,size_t count)340836d0830SLv Zheng static ssize_t acpi_aml_read_cmd(char *msg, size_t count)
3418cfb0cdfSLv Zheng {
3428cfb0cdfSLv Zheng 	int ret = 0;
3438cfb0cdfSLv Zheng 	int size = 0;
3448cfb0cdfSLv Zheng 
3458cfb0cdfSLv Zheng 	/*
3468cfb0cdfSLv Zheng 	 * This is ensured by the running fact of the debugger thread
3478cfb0cdfSLv Zheng 	 * unless a bug is introduced.
3488cfb0cdfSLv Zheng 	 */
3498cfb0cdfSLv Zheng 	BUG_ON(!acpi_aml_initialized);
3508cfb0cdfSLv Zheng 	while (count > 0) {
3518cfb0cdfSLv Zheng again:
3528cfb0cdfSLv Zheng 		/*
3538cfb0cdfSLv Zheng 		 * Check each input byte to find the end of the command.
3548cfb0cdfSLv Zheng 		 */
3558cfb0cdfSLv Zheng 		ret = acpi_aml_readb_kern();
3568cfb0cdfSLv Zheng 		if (ret == -EAGAIN) {
3578cfb0cdfSLv Zheng 			ret = wait_event_interruptible(acpi_aml_io.wait,
3588cfb0cdfSLv Zheng 				acpi_aml_kern_readable());
3598cfb0cdfSLv Zheng 			/*
3608cfb0cdfSLv Zheng 			 * We need to retry when the condition becomes
3618cfb0cdfSLv Zheng 			 * true.
3628cfb0cdfSLv Zheng 			 */
3638cfb0cdfSLv Zheng 			if (ret == 0)
3648cfb0cdfSLv Zheng 				goto again;
3658cfb0cdfSLv Zheng 		}
366287980e4SArnd Bergmann 		if (ret < 0)
3678cfb0cdfSLv Zheng 			break;
3688cfb0cdfSLv Zheng 		*(msg + size) = (char)ret;
3698cfb0cdfSLv Zheng 		size++;
3708cfb0cdfSLv Zheng 		count--;
3718cfb0cdfSLv Zheng 		if (ret == '\n') {
3728cfb0cdfSLv Zheng 			/*
3738cfb0cdfSLv Zheng 			 * acpi_os_get_line() requires a zero terminated command
3748cfb0cdfSLv Zheng 			 * string.
3758cfb0cdfSLv Zheng 			 */
3768cfb0cdfSLv Zheng 			*(msg + size - 1) = '\0';
3778cfb0cdfSLv Zheng 			break;
3788cfb0cdfSLv Zheng 		}
3798cfb0cdfSLv Zheng 	}
3808cfb0cdfSLv Zheng 	return size > 0 ? size : ret;
3818cfb0cdfSLv Zheng }
3828cfb0cdfSLv Zheng 
acpi_aml_thread(void * unused)383603fadf3SBjorn Helgaas static int acpi_aml_thread(void *unused)
3848cfb0cdfSLv Zheng {
3858cfb0cdfSLv Zheng 	acpi_osd_exec_callback function = NULL;
3868cfb0cdfSLv Zheng 	void *context;
3878cfb0cdfSLv Zheng 
3888cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
3898cfb0cdfSLv Zheng 	if (acpi_aml_io.function) {
3908cfb0cdfSLv Zheng 		acpi_aml_io.usages++;
3918cfb0cdfSLv Zheng 		function = acpi_aml_io.function;
3928cfb0cdfSLv Zheng 		context = acpi_aml_io.context;
3938cfb0cdfSLv Zheng 	}
3948cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
3958cfb0cdfSLv Zheng 
3968cfb0cdfSLv Zheng 	if (function)
3978cfb0cdfSLv Zheng 		function(context);
3988cfb0cdfSLv Zheng 
3998cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
4008cfb0cdfSLv Zheng 	acpi_aml_io.usages--;
4018cfb0cdfSLv Zheng 	if (!__acpi_aml_used()) {
4028cfb0cdfSLv Zheng 		acpi_aml_io.thread = NULL;
4038cfb0cdfSLv Zheng 		wake_up(&acpi_aml_io.wait);
4048cfb0cdfSLv Zheng 	}
4058cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
4068cfb0cdfSLv Zheng 
4078cfb0cdfSLv Zheng 	return 0;
4088cfb0cdfSLv Zheng }
4098cfb0cdfSLv Zheng 
4108cfb0cdfSLv Zheng /*
4118cfb0cdfSLv Zheng  * acpi_aml_create_thread() - Create AML debugger thread
4128cfb0cdfSLv Zheng  * @function: the debugger thread callback
4138cfb0cdfSLv Zheng  * @context: the context to be passed to the debugger thread
4148cfb0cdfSLv Zheng  *
4158cfb0cdfSLv Zheng  * This function should be used to implement acpi_os_execute() which is
4168cfb0cdfSLv Zheng  * used by the ACPICA debugger to create the debugger thread.
4178cfb0cdfSLv Zheng  */
acpi_aml_create_thread(acpi_osd_exec_callback function,void * context)418836d0830SLv Zheng static int acpi_aml_create_thread(acpi_osd_exec_callback function, void *context)
4198cfb0cdfSLv Zheng {
4208cfb0cdfSLv Zheng 	struct task_struct *t;
4218cfb0cdfSLv Zheng 
4228cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
4238cfb0cdfSLv Zheng 	acpi_aml_io.function = function;
4248cfb0cdfSLv Zheng 	acpi_aml_io.context = context;
4258cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
4268cfb0cdfSLv Zheng 
4278cfb0cdfSLv Zheng 	t = kthread_create(acpi_aml_thread, NULL, "aml");
4288cfb0cdfSLv Zheng 	if (IS_ERR(t)) {
4298cfb0cdfSLv Zheng 		pr_err("Failed to create AML debugger thread.\n");
4308cfb0cdfSLv Zheng 		return PTR_ERR(t);
4318cfb0cdfSLv Zheng 	}
4328cfb0cdfSLv Zheng 
4338cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
4348cfb0cdfSLv Zheng 	acpi_aml_io.thread = t;
4358cfb0cdfSLv Zheng 	acpi_set_debugger_thread_id((acpi_thread_id)(unsigned long)t);
4368cfb0cdfSLv Zheng 	wake_up_process(t);
4378cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
4388cfb0cdfSLv Zheng 	return 0;
4398cfb0cdfSLv Zheng }
4408cfb0cdfSLv Zheng 
acpi_aml_wait_command_ready(bool single_step,char * buffer,size_t length)441836d0830SLv Zheng static int acpi_aml_wait_command_ready(bool single_step,
442836d0830SLv Zheng 				       char *buffer, size_t length)
4438cfb0cdfSLv Zheng {
4448cfb0cdfSLv Zheng 	acpi_status status;
4458cfb0cdfSLv Zheng 
446836d0830SLv Zheng 	if (single_step)
4478cfb0cdfSLv Zheng 		acpi_os_printf("\n%1c ", ACPI_DEBUGGER_EXECUTE_PROMPT);
448836d0830SLv Zheng 	else
449836d0830SLv Zheng 		acpi_os_printf("\n%1c ", ACPI_DEBUGGER_COMMAND_PROMPT);
4508cfb0cdfSLv Zheng 
451836d0830SLv Zheng 	status = acpi_os_get_line(buffer, length, NULL);
4528cfb0cdfSLv Zheng 	if (ACPI_FAILURE(status))
4538cfb0cdfSLv Zheng 		return -EINVAL;
4548cfb0cdfSLv Zheng 	return 0;
4558cfb0cdfSLv Zheng }
4568cfb0cdfSLv Zheng 
acpi_aml_notify_command_complete(void)457836d0830SLv Zheng static int acpi_aml_notify_command_complete(void)
4588cfb0cdfSLv Zheng {
4598cfb0cdfSLv Zheng 	return 0;
4608cfb0cdfSLv Zheng }
4618cfb0cdfSLv Zheng 
acpi_aml_open(struct inode * inode,struct file * file)4628cfb0cdfSLv Zheng static int acpi_aml_open(struct inode *inode, struct file *file)
4638cfb0cdfSLv Zheng {
4648cfb0cdfSLv Zheng 	int ret = 0;
4658cfb0cdfSLv Zheng 	acpi_status status;
4668cfb0cdfSLv Zheng 
4678cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
4688cfb0cdfSLv Zheng 	/*
4698cfb0cdfSLv Zheng 	 * The debugger interface is being closed, no new user is allowed
4708cfb0cdfSLv Zheng 	 * during this period.
4718cfb0cdfSLv Zheng 	 */
4728cfb0cdfSLv Zheng 	if (acpi_aml_io.flags & ACPI_AML_CLOSED) {
4738cfb0cdfSLv Zheng 		ret = -EBUSY;
4748cfb0cdfSLv Zheng 		goto err_lock;
4758cfb0cdfSLv Zheng 	}
4768cfb0cdfSLv Zheng 	if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
4778cfb0cdfSLv Zheng 		/*
4788cfb0cdfSLv Zheng 		 * Only one reader is allowed to initiate the debugger
4798cfb0cdfSLv Zheng 		 * thread.
4808cfb0cdfSLv Zheng 		 */
4818cfb0cdfSLv Zheng 		if (acpi_aml_active_reader) {
4828cfb0cdfSLv Zheng 			ret = -EBUSY;
4838cfb0cdfSLv Zheng 			goto err_lock;
4848cfb0cdfSLv Zheng 		} else {
4858cfb0cdfSLv Zheng 			pr_debug("Opening debugger reader.\n");
4868cfb0cdfSLv Zheng 			acpi_aml_active_reader = file;
4878cfb0cdfSLv Zheng 		}
4888cfb0cdfSLv Zheng 	} else {
4898cfb0cdfSLv Zheng 		/*
4908cfb0cdfSLv Zheng 		 * No writer is allowed unless the debugger thread is
4918cfb0cdfSLv Zheng 		 * ready.
4928cfb0cdfSLv Zheng 		 */
4938cfb0cdfSLv Zheng 		if (!(acpi_aml_io.flags & ACPI_AML_OPENED)) {
4948cfb0cdfSLv Zheng 			ret = -ENODEV;
4958cfb0cdfSLv Zheng 			goto err_lock;
4968cfb0cdfSLv Zheng 		}
4978cfb0cdfSLv Zheng 	}
4988cfb0cdfSLv Zheng 	if (acpi_aml_active_reader == file) {
4998cfb0cdfSLv Zheng 		pr_debug("Opening debugger interface.\n");
5008cfb0cdfSLv Zheng 		mutex_unlock(&acpi_aml_io.lock);
5018cfb0cdfSLv Zheng 
5028cfb0cdfSLv Zheng 		pr_debug("Initializing debugger thread.\n");
5038cfb0cdfSLv Zheng 		status = acpi_initialize_debugger();
5048cfb0cdfSLv Zheng 		if (ACPI_FAILURE(status)) {
5058cfb0cdfSLv Zheng 			pr_err("Failed to initialize debugger.\n");
5068cfb0cdfSLv Zheng 			ret = -EINVAL;
50759adb398SLv Zheng 			goto err_exit;
5088cfb0cdfSLv Zheng 		}
5098cfb0cdfSLv Zheng 		pr_debug("Debugger thread initialized.\n");
5108cfb0cdfSLv Zheng 
5118cfb0cdfSLv Zheng 		mutex_lock(&acpi_aml_io.lock);
51273af2d59SLv Zheng 		acpi_aml_io.flags |= ACPI_AML_OPENED;
5138cfb0cdfSLv Zheng 		acpi_aml_io.out_crc.head = acpi_aml_io.out_crc.tail = 0;
5148cfb0cdfSLv Zheng 		acpi_aml_io.in_crc.head = acpi_aml_io.in_crc.tail = 0;
5158cfb0cdfSLv Zheng 		pr_debug("Debugger interface opened.\n");
5168cfb0cdfSLv Zheng 	}
5178cfb0cdfSLv Zheng 	acpi_aml_io.users++;
5188cfb0cdfSLv Zheng err_lock:
519287980e4SArnd Bergmann 	if (ret < 0) {
5208cfb0cdfSLv Zheng 		if (acpi_aml_active_reader == file)
5218cfb0cdfSLv Zheng 			acpi_aml_active_reader = NULL;
5228cfb0cdfSLv Zheng 	}
5238cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
52459adb398SLv Zheng err_exit:
5258cfb0cdfSLv Zheng 	return ret;
5268cfb0cdfSLv Zheng }
5278cfb0cdfSLv Zheng 
acpi_aml_release(struct inode * inode,struct file * file)5288cfb0cdfSLv Zheng static int acpi_aml_release(struct inode *inode, struct file *file)
5298cfb0cdfSLv Zheng {
5308cfb0cdfSLv Zheng 	mutex_lock(&acpi_aml_io.lock);
5318cfb0cdfSLv Zheng 	acpi_aml_io.users--;
5328cfb0cdfSLv Zheng 	if (file == acpi_aml_active_reader) {
5338cfb0cdfSLv Zheng 		pr_debug("Closing debugger reader.\n");
5348cfb0cdfSLv Zheng 		acpi_aml_active_reader = NULL;
5358cfb0cdfSLv Zheng 
5368cfb0cdfSLv Zheng 		pr_debug("Closing debugger interface.\n");
5378cfb0cdfSLv Zheng 		acpi_aml_io.flags |= ACPI_AML_CLOSED;
5388cfb0cdfSLv Zheng 
5398cfb0cdfSLv Zheng 		/*
5408cfb0cdfSLv Zheng 		 * Wake up all user space/kernel space blocked
5418cfb0cdfSLv Zheng 		 * readers/writers.
5428cfb0cdfSLv Zheng 		 */
5438cfb0cdfSLv Zheng 		wake_up_interruptible(&acpi_aml_io.wait);
5448cfb0cdfSLv Zheng 		mutex_unlock(&acpi_aml_io.lock);
5458cfb0cdfSLv Zheng 		/*
5468cfb0cdfSLv Zheng 		 * Wait all user space/kernel space readers/writers to
5478cfb0cdfSLv Zheng 		 * stop so that ACPICA command loop of the debugger thread
5488cfb0cdfSLv Zheng 		 * should fail all its command line reads after this point.
5498cfb0cdfSLv Zheng 		 */
5508cfb0cdfSLv Zheng 		wait_event(acpi_aml_io.wait, !acpi_aml_busy());
5518cfb0cdfSLv Zheng 
5528cfb0cdfSLv Zheng 		/*
5538cfb0cdfSLv Zheng 		 * Then we try to terminate the debugger thread if it is
5548cfb0cdfSLv Zheng 		 * not terminated.
5558cfb0cdfSLv Zheng 		 */
5568cfb0cdfSLv Zheng 		pr_debug("Terminating debugger thread.\n");
5578cfb0cdfSLv Zheng 		acpi_terminate_debugger();
5588cfb0cdfSLv Zheng 		wait_event(acpi_aml_io.wait, !acpi_aml_used());
5598cfb0cdfSLv Zheng 		pr_debug("Debugger thread terminated.\n");
5608cfb0cdfSLv Zheng 
5618cfb0cdfSLv Zheng 		mutex_lock(&acpi_aml_io.lock);
5628cfb0cdfSLv Zheng 		acpi_aml_io.flags &= ~ACPI_AML_OPENED;
5638cfb0cdfSLv Zheng 	}
5648cfb0cdfSLv Zheng 	if (acpi_aml_io.users == 0) {
5658cfb0cdfSLv Zheng 		pr_debug("Debugger interface closed.\n");
5668cfb0cdfSLv Zheng 		acpi_aml_io.flags &= ~ACPI_AML_CLOSED;
5678cfb0cdfSLv Zheng 	}
5688cfb0cdfSLv Zheng 	mutex_unlock(&acpi_aml_io.lock);
5698cfb0cdfSLv Zheng 	return 0;
5708cfb0cdfSLv Zheng }
5718cfb0cdfSLv Zheng 
acpi_aml_read_user(char __user * buf,int len)5728cfb0cdfSLv Zheng static int acpi_aml_read_user(char __user *buf, int len)
5738cfb0cdfSLv Zheng {
5748cfb0cdfSLv Zheng 	int ret;
5758cfb0cdfSLv Zheng 	struct circ_buf *crc = &acpi_aml_io.out_crc;
5768cfb0cdfSLv Zheng 	int n;
5778cfb0cdfSLv Zheng 	char *p;
5788cfb0cdfSLv Zheng 
5798cfb0cdfSLv Zheng 	ret = acpi_aml_lock_read(crc, ACPI_AML_OUT_USER);
580287980e4SArnd Bergmann 	if (ret < 0)
5818cfb0cdfSLv Zheng 		return ret;
5828cfb0cdfSLv Zheng 	/* sync head before removing logs */
5838cfb0cdfSLv Zheng 	smp_rmb();
5848cfb0cdfSLv Zheng 	p = &crc->buf[crc->tail];
5858cfb0cdfSLv Zheng 	n = min(len, circ_count_to_end(crc));
586ec74765dSDan Carpenter 	if (copy_to_user(buf, p, n)) {
587ec74765dSDan Carpenter 		ret = -EFAULT;
5888cfb0cdfSLv Zheng 		goto out;
589ec74765dSDan Carpenter 	}
5908cfb0cdfSLv Zheng 	/* sync tail after removing logs */
5918cfb0cdfSLv Zheng 	smp_mb();
5928cfb0cdfSLv Zheng 	crc->tail = (crc->tail + n) & (ACPI_AML_BUF_SIZE - 1);
5938cfb0cdfSLv Zheng 	ret = n;
5948cfb0cdfSLv Zheng out:
5957e3fd813SLv Zheng 	acpi_aml_unlock_fifo(ACPI_AML_OUT_USER, ret >= 0);
5968cfb0cdfSLv Zheng 	return ret;
5978cfb0cdfSLv Zheng }
5988cfb0cdfSLv Zheng 
acpi_aml_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)5998cfb0cdfSLv Zheng static ssize_t acpi_aml_read(struct file *file, char __user *buf,
6008cfb0cdfSLv Zheng 			     size_t count, loff_t *ppos)
6018cfb0cdfSLv Zheng {
6028cfb0cdfSLv Zheng 	int ret = 0;
6038cfb0cdfSLv Zheng 	int size = 0;
6048cfb0cdfSLv Zheng 
6058cfb0cdfSLv Zheng 	if (!count)
6068cfb0cdfSLv Zheng 		return 0;
60796d4f267SLinus Torvalds 	if (!access_ok(buf, count))
6088cfb0cdfSLv Zheng 		return -EFAULT;
6098cfb0cdfSLv Zheng 
6108cfb0cdfSLv Zheng 	while (count > 0) {
6118cfb0cdfSLv Zheng again:
6128cfb0cdfSLv Zheng 		ret = acpi_aml_read_user(buf + size, count);
6138cfb0cdfSLv Zheng 		if (ret == -EAGAIN) {
6148cfb0cdfSLv Zheng 			if (file->f_flags & O_NONBLOCK)
6158cfb0cdfSLv Zheng 				break;
6168cfb0cdfSLv Zheng 			else {
6178cfb0cdfSLv Zheng 				ret = wait_event_interruptible(acpi_aml_io.wait,
6188cfb0cdfSLv Zheng 					acpi_aml_user_readable());
6198cfb0cdfSLv Zheng 				/*
6208cfb0cdfSLv Zheng 				 * We need to retry when the condition
6218cfb0cdfSLv Zheng 				 * becomes true.
6228cfb0cdfSLv Zheng 				 */
6238cfb0cdfSLv Zheng 				if (ret == 0)
6248cfb0cdfSLv Zheng 					goto again;
6258cfb0cdfSLv Zheng 			}
6268cfb0cdfSLv Zheng 		}
627287980e4SArnd Bergmann 		if (ret < 0) {
6288cfb0cdfSLv Zheng 			if (!acpi_aml_running())
6298cfb0cdfSLv Zheng 				ret = 0;
6308cfb0cdfSLv Zheng 			break;
6318cfb0cdfSLv Zheng 		}
6328cfb0cdfSLv Zheng 		if (ret) {
6338cfb0cdfSLv Zheng 			size += ret;
6348cfb0cdfSLv Zheng 			count -= ret;
6358cfb0cdfSLv Zheng 			*ppos += ret;
6368cfb0cdfSLv Zheng 			break;
6378cfb0cdfSLv Zheng 		}
6388cfb0cdfSLv Zheng 	}
6398cfb0cdfSLv Zheng 	return size > 0 ? size : ret;
6408cfb0cdfSLv Zheng }
6418cfb0cdfSLv Zheng 
acpi_aml_write_user(const char __user * buf,int len)6428cfb0cdfSLv Zheng static int acpi_aml_write_user(const char __user *buf, int len)
6438cfb0cdfSLv Zheng {
6448cfb0cdfSLv Zheng 	int ret;
6458cfb0cdfSLv Zheng 	struct circ_buf *crc = &acpi_aml_io.in_crc;
6468cfb0cdfSLv Zheng 	int n;
6478cfb0cdfSLv Zheng 	char *p;
6488cfb0cdfSLv Zheng 
6498cfb0cdfSLv Zheng 	ret = acpi_aml_lock_write(crc, ACPI_AML_IN_USER);
650287980e4SArnd Bergmann 	if (ret < 0)
6518cfb0cdfSLv Zheng 		return ret;
6528cfb0cdfSLv Zheng 	/* sync tail before inserting cmds */
6538cfb0cdfSLv Zheng 	smp_mb();
6548cfb0cdfSLv Zheng 	p = &crc->buf[crc->head];
6558cfb0cdfSLv Zheng 	n = min(len, circ_space_to_end(crc));
656ec74765dSDan Carpenter 	if (copy_from_user(p, buf, n)) {
657ec74765dSDan Carpenter 		ret = -EFAULT;
6588cfb0cdfSLv Zheng 		goto out;
659ec74765dSDan Carpenter 	}
6608cfb0cdfSLv Zheng 	/* sync head after inserting cmds */
6618cfb0cdfSLv Zheng 	smp_wmb();
6628cfb0cdfSLv Zheng 	crc->head = (crc->head + n) & (ACPI_AML_BUF_SIZE - 1);
6638cfb0cdfSLv Zheng 	ret = n;
6648cfb0cdfSLv Zheng out:
6657e3fd813SLv Zheng 	acpi_aml_unlock_fifo(ACPI_AML_IN_USER, ret >= 0);
6668cfb0cdfSLv Zheng 	return n;
6678cfb0cdfSLv Zheng }
6688cfb0cdfSLv Zheng 
acpi_aml_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)6698cfb0cdfSLv Zheng static ssize_t acpi_aml_write(struct file *file, const char __user *buf,
6708cfb0cdfSLv Zheng 			      size_t count, loff_t *ppos)
6718cfb0cdfSLv Zheng {
6728cfb0cdfSLv Zheng 	int ret = 0;
6738cfb0cdfSLv Zheng 	int size = 0;
6748cfb0cdfSLv Zheng 
6758cfb0cdfSLv Zheng 	if (!count)
6768cfb0cdfSLv Zheng 		return 0;
67796d4f267SLinus Torvalds 	if (!access_ok(buf, count))
6788cfb0cdfSLv Zheng 		return -EFAULT;
6798cfb0cdfSLv Zheng 
6808cfb0cdfSLv Zheng 	while (count > 0) {
6818cfb0cdfSLv Zheng again:
6828cfb0cdfSLv Zheng 		ret = acpi_aml_write_user(buf + size, count);
6838cfb0cdfSLv Zheng 		if (ret == -EAGAIN) {
6848cfb0cdfSLv Zheng 			if (file->f_flags & O_NONBLOCK)
6858cfb0cdfSLv Zheng 				break;
6868cfb0cdfSLv Zheng 			else {
6878cfb0cdfSLv Zheng 				ret = wait_event_interruptible(acpi_aml_io.wait,
6888cfb0cdfSLv Zheng 					acpi_aml_user_writable());
6898cfb0cdfSLv Zheng 				/*
6908cfb0cdfSLv Zheng 				 * We need to retry when the condition
6918cfb0cdfSLv Zheng 				 * becomes true.
6928cfb0cdfSLv Zheng 				 */
6938cfb0cdfSLv Zheng 				if (ret == 0)
6948cfb0cdfSLv Zheng 					goto again;
6958cfb0cdfSLv Zheng 			}
6968cfb0cdfSLv Zheng 		}
697287980e4SArnd Bergmann 		if (ret < 0) {
6988cfb0cdfSLv Zheng 			if (!acpi_aml_running())
6998cfb0cdfSLv Zheng 				ret = 0;
7008cfb0cdfSLv Zheng 			break;
7018cfb0cdfSLv Zheng 		}
7028cfb0cdfSLv Zheng 		if (ret) {
7038cfb0cdfSLv Zheng 			size += ret;
7048cfb0cdfSLv Zheng 			count -= ret;
7058cfb0cdfSLv Zheng 			*ppos += ret;
7068cfb0cdfSLv Zheng 		}
7078cfb0cdfSLv Zheng 	}
7088cfb0cdfSLv Zheng 	return size > 0 ? size : ret;
7098cfb0cdfSLv Zheng }
7108cfb0cdfSLv Zheng 
acpi_aml_poll(struct file * file,poll_table * wait)7110d9b87f3SAl Viro static __poll_t acpi_aml_poll(struct file *file, poll_table *wait)
7128cfb0cdfSLv Zheng {
7130d9b87f3SAl Viro 	__poll_t masks = 0;
7148cfb0cdfSLv Zheng 
7158cfb0cdfSLv Zheng 	poll_wait(file, &acpi_aml_io.wait, wait);
7168cfb0cdfSLv Zheng 	if (acpi_aml_user_readable())
717a9a08845SLinus Torvalds 		masks |= EPOLLIN | EPOLLRDNORM;
7188cfb0cdfSLv Zheng 	if (acpi_aml_user_writable())
719a9a08845SLinus Torvalds 		masks |= EPOLLOUT | EPOLLWRNORM;
7208cfb0cdfSLv Zheng 
7218cfb0cdfSLv Zheng 	return masks;
7228cfb0cdfSLv Zheng }
7238cfb0cdfSLv Zheng 
7248cfb0cdfSLv Zheng static const struct file_operations acpi_aml_operations = {
7258cfb0cdfSLv Zheng 	.read		= acpi_aml_read,
7268cfb0cdfSLv Zheng 	.write		= acpi_aml_write,
7278cfb0cdfSLv Zheng 	.poll		= acpi_aml_poll,
7288cfb0cdfSLv Zheng 	.open		= acpi_aml_open,
7298cfb0cdfSLv Zheng 	.release	= acpi_aml_release,
7308cfb0cdfSLv Zheng 	.llseek		= generic_file_llseek,
7318cfb0cdfSLv Zheng };
7328cfb0cdfSLv Zheng 
733836d0830SLv Zheng static const struct acpi_debugger_ops acpi_aml_debugger = {
734836d0830SLv Zheng 	.create_thread		 = acpi_aml_create_thread,
735836d0830SLv Zheng 	.read_cmd		 = acpi_aml_read_cmd,
736836d0830SLv Zheng 	.write_log		 = acpi_aml_write_log,
737836d0830SLv Zheng 	.wait_command_ready	 = acpi_aml_wait_command_ready,
738836d0830SLv Zheng 	.notify_command_complete = acpi_aml_notify_command_complete,
739836d0830SLv Zheng };
740836d0830SLv Zheng 
acpi_aml_init(void)7418e22c2e4SZheng Zengkai static int __init acpi_aml_init(void)
7428cfb0cdfSLv Zheng {
7436010ce3fSRafael J. Wysocki 	int ret;
744836d0830SLv Zheng 
745*0fada277SJamie Iles 	if (acpi_disabled)
746*0fada277SJamie Iles 		return -ENODEV;
747*0fada277SJamie Iles 
7488cfb0cdfSLv Zheng 	/* Initialize AML IO interface */
7498cfb0cdfSLv Zheng 	mutex_init(&acpi_aml_io.lock);
7508cfb0cdfSLv Zheng 	init_waitqueue_head(&acpi_aml_io.wait);
7518cfb0cdfSLv Zheng 	acpi_aml_io.out_crc.buf = acpi_aml_io.out_buf;
7528cfb0cdfSLv Zheng 	acpi_aml_io.in_crc.buf = acpi_aml_io.in_buf;
7536010ce3fSRafael J. Wysocki 
7548cfb0cdfSLv Zheng 	acpi_aml_dentry = debugfs_create_file("acpidbg",
7558cfb0cdfSLv Zheng 					      S_IFREG | S_IRUGO | S_IWUSR,
7568cfb0cdfSLv Zheng 					      acpi_debugfs_dir, NULL,
7578cfb0cdfSLv Zheng 					      &acpi_aml_operations);
758836d0830SLv Zheng 
7596010ce3fSRafael J. Wysocki 	ret = acpi_register_debugger(THIS_MODULE, &acpi_aml_debugger);
760836d0830SLv Zheng 	if (ret) {
761836d0830SLv Zheng 		debugfs_remove(acpi_aml_dentry);
762836d0830SLv Zheng 		acpi_aml_dentry = NULL;
763836d0830SLv Zheng 		return ret;
7648cfb0cdfSLv Zheng 	}
7658cfb0cdfSLv Zheng 
7666010ce3fSRafael J. Wysocki 	acpi_aml_initialized = true;
7676010ce3fSRafael J. Wysocki 	return 0;
7686010ce3fSRafael J. Wysocki }
7696010ce3fSRafael J. Wysocki 
acpi_aml_exit(void)7708e22c2e4SZheng Zengkai static void __exit acpi_aml_exit(void)
7718cfb0cdfSLv Zheng {
772836d0830SLv Zheng 	if (acpi_aml_initialized) {
773836d0830SLv Zheng 		acpi_unregister_debugger(&acpi_aml_debugger);
7748cfb0cdfSLv Zheng 		debugfs_remove(acpi_aml_dentry);
775836d0830SLv Zheng 		acpi_aml_dentry = NULL;
7768cfb0cdfSLv Zheng 		acpi_aml_initialized = false;
7778cfb0cdfSLv Zheng 	}
778836d0830SLv Zheng }
7798cfb0cdfSLv Zheng 
7808cfb0cdfSLv Zheng module_init(acpi_aml_init);
7818cfb0cdfSLv Zheng module_exit(acpi_aml_exit);
782836d0830SLv Zheng 
783836d0830SLv Zheng MODULE_AUTHOR("Lv Zheng");
784836d0830SLv Zheng MODULE_DESCRIPTION("ACPI debugger userspace IO driver");
785836d0830SLv Zheng MODULE_LICENSE("GPL");
786