aboutsummaryrefslogtreecommitdiff
path: root/drivers/acpi/acpi_dbg.c
blob: 15e4604efba7d298f8a5e1931eb3919452f26dbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/*
 * ACPI AML interfacing support
 *
 * Copyright (C) 2015, Intel Corporation
 * Authors: Lv Zheng <lv.zheng@intel.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

/* #define DEBUG */
#define pr_fmt(fmt) "ACPI : AML: " fmt

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/proc_fs.h>
#include <linux/debugfs.h>
#include <linux/circ_buf.h>
#include <linux/acpi.h>
#include "internal.h"

#define ACPI_AML_BUF_ALIGN	(sizeof (acpi_size))
#define ACPI_AML_BUF_SIZE	PAGE_SIZE

#define circ_count(circ) \
	(CIRC_CNT((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
#define circ_count_to_end(circ) \
	(CIRC_CNT_TO_END((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
#define circ_space(circ) \
	(CIRC_SPACE((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
#define circ_space_to_end(circ) \
	(CIRC_SPACE_TO_END((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))

#define ACPI_AML_OPENED		0x0001
#define ACPI_AML_CLOSED		0x0002
#define ACPI_AML_IN_USER	0x0004 /* user space is writing cmd */
#define ACPI_AML_IN_KERN	0x0008 /* kernel space is reading cmd */
#define ACPI_AML_OUT_USER	0x0010 /* user space is reading log */
#define ACPI_AML_OUT_KERN	0x0020 /* kernel space is writing log */
#define ACPI_AML_USER		(ACPI_AML_IN_USER | ACPI_AML_OUT_USER)
#define ACPI_AML_KERN		(ACPI_AML_IN_KERN | ACPI_AML_OUT_KERN)
#define ACPI_AML_BUSY		(ACPI_AML_USER | ACPI_AML_KERN)
#define ACPI_AML_OPEN		(ACPI_AML_OPENED | ACPI_AML_CLOSED)

struct acpi_aml_io {
	wait_queue_head_t wait;
	unsigned long flags;
	unsigned long users;
	struct mutex lock;
	struct task_struct *thread;
	char out_buf[ACPI_AML_BUF_SIZE] __aligned(ACPI_AML_BUF_ALIGN);
	struct circ_buf out_crc;
	char in_buf[ACPI_AML_BUF_SIZE] __aligned(ACPI_AML_BUF_ALIGN);
	struct circ_buf in_crc;
	acpi_osd_exec_callback function;
	void *context;
	unsigned long usages;
};

static struct acpi_aml_io acpi_aml_io;
static bool acpi_aml_initialized;
static struct file *acpi_aml_active_reader;
static struct dentry *acpi_aml_dentry;

static inline bool __acpi_aml_running(void)
{
	return acpi_aml_io.thread ? true : false;
}

static inline bool __acpi_aml_access_ok(unsigned long flag)
{
	/*
	 * The debugger interface is in opened state (OPENED && !CLOSED),
	 * then it is allowed to access the debugger buffers from either
	 * user space or the kernel space.
	 * In addition, for the kernel space, only the debugger thread
	 * (thread ID matched) is allowed to access.
	 */
	if (!(acpi_aml_io.flags & ACPI_AML_OPENED) ||
	    (acpi_aml_io.flags & ACPI_AML_CLOSED) ||
	    !__acpi_aml_running())
		return false;
	if ((flag & ACPI_AML_KERN) &&
	    current != acpi_aml_io.thread)
		return false;
	return true;
}

static inline bool __acpi_aml_readable(struct circ_buf *circ, unsigned long flag)
{
	/*
	 * Another read is not in progress and there is data in buffer
	 * available for read.
	 */
	if (!(acpi_aml_io.flags & flag) && circ_count(circ))
		return true;
	return false;
}

static inline bool __acpi_aml_writable(struct circ_buf *circ, unsigned long flag)
{
	/*
	 * Another write is not in progress and there is buffer space
	 * available for write.
	 */
	if (!(acpi_aml_io.flags & flag) && circ_space(circ))
		return true;
	return false;
}

static inline bool __acpi_aml_busy(void)
{
	if (acpi_aml_io.flags & ACPI_AML_BUSY)
		return true;
	return false;
}

static inline bool __acpi_aml_opened(void)
{
	if (acpi_aml_io.flags & ACPI_AML_OPEN)
		return true;
	return false;
}

static inline bool __acpi_aml_used(void)
{
	return acpi_aml_io.usages ? true : false;
}

static inline bool acpi_aml_running(void)
{
	bool ret;

	mutex_lock(&acpi_aml_io.lock);
	ret = __acpi_aml_running();
	mutex_unlock(&acpi_aml_io.lock);
	return ret;
}

static bool acpi_aml_busy(void)
{
	bool ret;

	mutex_lock(&acpi_aml_io.lock);
	ret = __acpi_aml_busy();
	mutex_unlock(&acpi_aml_io.lock);
	return ret;
}

static bool acpi_aml_used(void)
{
	bool ret;

	/*
	 * The usage count is prepared to avoid race conditions between the
	 * starts and the stops of the debugger thread.
	 */
	mutex_lock(&acpi_aml_io.lock);
	ret = __acpi_aml_used();
	mutex_unlock(&acpi_aml_io.lock);
	return ret;
}

static bool acpi_aml_kern_readable(void)
{
	bool ret;

	mutex_lock(&acpi_aml_io.lock);
	ret = !__acpi_aml_access_ok(ACPI_AML_IN_KERN) ||
	      __acpi_aml_readable(&acpi_aml_io.in_crc, ACPI_AML_IN_KERN);
	mutex_unlock(&acpi_aml_io.lock);
	return ret;
}

static bool acpi_aml_kern_writable(void)
{
	bool ret;

	mutex_lock(&acpi_aml_io.lock);
	ret = !__acpi_aml_access_ok(ACPI_AML_OUT_KERN) ||
	      __acpi_aml_writable(&acpi_aml_io.out_crc, ACPI_AML_OUT_KERN);
	mutex_unlock(&acpi_aml_io.lock);
	return ret;
}

static bool acpi_aml_user_readable(void)
{
	bool ret;

	mutex_lock(&acpi_aml_io.lock);
	ret = !__acpi_aml_access_ok(ACPI_AML_OUT_USER) ||
	      __acpi_aml_readable(&acpi_aml_io.out_crc, ACPI_AML_OUT_USER);
	mutex_unlock(&acpi_aml_io.lock);
	return ret;
}

static bool acpi_aml_user_writable(void)
{
	bool ret;

	mutex_lock(&acpi_aml_io.lock);
	ret = !__acpi_aml_access_ok(ACPI_AML_IN_USER) ||
	      __acpi_aml_writable(&acpi_aml_io.in_crc, ACPI_AML_IN_USER);
	mutex_unlock(&acpi_aml_io.lock);
	return ret;
}

static int acpi_aml_lock_write(struct circ_buf *circ, unsigned long flag)
{
	int ret = 0;

	mutex_lock(&acpi_aml_io.lock);
	if (!__acpi_aml_access_ok(flag)) {
		ret = -EFAULT;
		goto out;
	}
	if (!__acpi_aml_writable(circ, flag)) {
		ret = -EAGAIN;
		goto out;
	}
	acpi_aml_io.flags |= flag;
out:
	mutex_unlock(&acpi_aml_io.lock);
	return ret;
}

static int acpi_aml_lock_read(struct circ_buf *circ, unsigned long flag)
{
	int ret = 0;

	mutex_lock(&acpi_aml_io.lock);
	if (!__acpi_aml_access_ok(flag)) {
		ret = -EFAULT;
		goto out;
	}
	if (!__acpi_aml_readable(circ, flag)) {
		ret = -EAGAIN;
		goto out;
	}
	acpi_aml_io.flags |= flag;
out:
	mutex_unlock(&acpi_aml_io.lock);
	return ret;
}

static void acpi_aml_unlock_fifo(unsigned long flag, bool wakeup)
{
	mutex_lock(&acpi_aml_io.lock);
	acpi_aml_io.flags &= ~flag;
	if (wakeup)
		wake_up_interruptible(&acpi_aml_io.wait);
	mutex_unlock(&acpi_aml_io.lock);
}

static int acpi_aml_write_kern(const char *buf, int len)
{
	int ret;
	struct circ_buf *crc = &acpi_aml_io.out_crc;
	int n;
	char *p;

	ret = acpi_aml_lock_write(crc, ACPI_AML_OUT_KERN);
	if (IS_ERR_VALUE(ret))
		return ret;
	/* sync tail before inserting logs */
	smp_mb();
	p = &crc->buf[crc->head];
	n = min(len, circ_space_to_end(crc));
	memcpy(p, buf, n);
	/* sync head after inserting logs */
	smp_wmb();
	crc->head = (crc->head + n) & (ACPI_AML_BUF_SIZE - 1);
	acpi_aml_unlock_fifo(ACPI_AML_OUT_KERN, true);
	return n;
}

static int acpi_aml_readb_kern(void)
{
	int ret;
	struct circ_buf *crc = &acpi_aml_io.in_crc;
	char *p;

	ret = acpi_aml_lock_read(crc, ACPI_AML_IN_KERN);
	if (IS_ERR_VALUE(ret))
		return ret;
	/* sync head before removing cmds */
	smp_rmb();
	p = &crc->buf[crc->tail];
	ret = (int)*p;
	/* sync tail before inserting cmds */
	smp_mb();
	crc->tail = (crc->tail + 1) & (ACPI_AML_BUF_SIZE - 1);
	acpi_aml_unlock_fifo(ACPI_AML_IN_KERN, true);
	return ret;
}

/*
 * acpi_aml_write_log() - Capture debugger output
 * @msg: the debugger output
 *
 * This function should be used to implement acpi_os_printf() to filter out
 * the debugger output and store the output into the debugger interface
 * buffer. Return the size of stored logs or errno.
 */
static ssize_t acpi_aml_write_log(const char *msg)
{
	int ret = 0;
	int count = 0, size = 0;

	if (!acpi_aml_initialized)
		return -ENODEV;
	if (msg)
		count = strlen(msg);
	while (count > 0) {
again:
		ret = acpi_aml_write_kern(msg + size, count);
		if (ret == -EAGAIN) {
			ret = wait_event_interruptible(acpi_aml_io.wait,
				acpi_aml_kern_writable());
			/*
			 * We need to retry when the condition
			 * becomes true.
			 */
			if (ret == 0)
				goto again;
			break;
		}
		if (IS_ERR_VALUE(ret))
			break;
		size += ret;
		count -= ret;
	}
	return size > 0 ? size : ret;
}

/*
 * acpi_aml_read_cmd() - Capture debugger input
 * @msg: the debugger input
 * @size: the size of the debugger input
 *
 * This function should be used to implement acpi_os_get_line() to capture
 * the debugger input commands and store the input commands into the
 * debugger interface buffer. Return the size of stored commands or errno.
 */
static ssize_t acpi_aml_read_cmd(char *msg, size_t count)
{
	int ret = 0;
	int size = 0;

	/*
	 * This is ensured by the running fact of the debugger thread
	 * unless a bug is introduced.
	 */
	BUG_ON(!acpi_aml_initialized);
	while (count > 0) {
again:
		/*
		 * Check each input byte to find the end of the command.
		 */
		ret = acpi_aml_readb_kern();
		if (ret == -EAGAIN) {
			ret = wait_event_interruptible(acpi_aml_io.wait,
				acpi_aml_kern_readable());
			/*
			 * We need to retry when the condition becomes
			 * true.
			 */
			if (ret == 0)
				goto again;
		}
		if (IS_ERR_VALUE(ret))
			break;
		*(msg + size) = (char)ret;
		size++;
		count--;
		if (ret == '\n') {
			/*
			 * acpi_os_get_line() requires a zero terminated command
			 * string.
			 */
			*(msg + size - 1) = '\0';
			break;
		}
	}
	return size > 0 ? size : ret;
}

static int acpi_aml_thread(void *unsed)
{
	acpi_osd_exec_callback function = NULL;
	void *context;

	mutex_lock(&acpi_aml_io.lock);
	if (acpi_aml_io.function) {
		acpi_aml_io.usages++;
		function = acpi_aml_io.function;
		context = acpi_aml_io.context;
	}
	mutex_unlock(&acpi_aml_io.lock);

	if (function)
		function(context);

	mutex_lock(&acpi_aml_io.lock);
	acpi_aml_io.usages--;
	if (!__acpi_aml_used()) {
		acpi_aml_io.thread = NULL;
		wake_up(&acpi_aml_io.wait);
	}
	mutex_unlock(&acpi_aml_io.lock);

	return 0;
}

/*
 * acpi_aml_create_thread() - Create AML debugger thread
 * @function: the debugger thread callback
 * @context: the context to be passed to the debugger thread
 *
 * This function should be used to implement acpi_os_execute() which is
 * used by the ACPICA debugger to create the debugger thread.
 */
static int acpi_aml_create_thread(acpi_osd_exec_callback function, void *context)
{
	struct task_struct *t;

	mutex_lock(&acpi_aml_io.lock);
	acpi_aml_io.function = function;
	acpi_aml_io.context = context;
	mutex_unlock(&acpi_aml_io.lock);

	t = kthread_create(acpi_aml_thread, NULL, "aml");
	if (IS_ERR(t)) {
		pr_err("Failed to create AML debugger thread.\n");
		return PTR_ERR(t);
	}

	mutex_lock(&acpi_aml_io.lock);
	acpi_aml_io.thread = t;
	acpi_set_debugger_thread_id((acpi_thread_id)(unsigned long)t);
	wake_up_process(t);
	mutex_unlock(&acpi_aml_io.lock);
	return 0;
}

static int acpi_aml_wait_command_ready(bool single_step,
				       char *buffer, size_t length)
{
	acpi_status status;

	if (single_step)
		acpi_os_printf("\n%1c ", ACPI_DEBUGGER_EXECUTE_PROMPT);
	else
		acpi_os_printf("\n%1c ", ACPI_DEBUGGER_COMMAND_PROMPT);

	status = acpi_os_get_line(buffer, length, NULL);
	if (ACPI_FAILURE(status))
		return -EINVAL;
	return 0;
}

static int acpi_aml_notify_command_complete(void)
{
	return 0;
}

static int acpi_aml_open(struct inode *inode, struct file *file)
{
	int ret = 0;
	acpi_status status;

	mutex_lock(&acpi_aml_io.lock);
	/*
	 * The debugger interface is being closed, no new user is allowed
	 * during this period.
	 */
	if (acpi_aml_io.flags & ACPI_AML_CLOSED) {
		ret = -EBUSY;
		goto err_lock;
	}
	if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
		/*
		 * Only one reader is allowed to initiate the debugger
		 * thread.
		 */
		if (acpi_aml_active_reader) {
			ret = -EBUSY;
			goto err_lock;
		} else {
			pr_debug("Opening debugger reader.\n");
			acpi_aml_active_reader = file;
		}
	} else {
		/*
		 * No writer is allowed unless the debugger thread is
		 * ready.
		 */
		if (!(acpi_aml_io.flags & ACPI_AML_OPENED)) {
			ret = -ENODEV;
			goto err_lock;
		}
	}
	if (acpi_aml_active_reader == file) {
		pr_debug("Opening debugger interface.\n");
		mutex_unlock(&acpi_aml_io.lock);

		pr_debug("Initializing debugger thread.\n");
		status = acpi_initialize_debugger();
		if (ACPI_FAILURE(status)) {
			pr_err("Failed to initialize debugger.\n");
			ret = -EINVAL;
			goto err_exit;
		}
		pr_debug("Debugger thread initialized.\n");

		mutex_lock(&acpi_aml_io.lock);
		acpi_aml_io.flags |= ACPI_AML_OPENED;
		acpi_aml_io.out_crc.head = acpi_aml_io.out_crc.tail = 0;
		acpi_aml_io.in_crc.head = acpi_aml_io.in_crc.tail = 0;
		pr_debug("Debugger interface opened.\n");
	}
	acpi_aml_io.users++;
err_lock:
	if (IS_ERR_VALUE(ret)) {
		if (acpi_aml_active_reader == file)
			acpi_aml_active_reader = NULL;
	}
	mutex_unlock(&acpi_aml_io.lock);
err_exit:
	return ret;
}

static int acpi_aml_release(struct inode *inode, struct file *file)
{
	mutex_lock(&acpi_aml_io.lock);
	acpi_aml_io.users--;
	if (file == acpi_aml_active_reader) {
		pr_debug("Closing debugger reader.\n");
		acpi_aml_active_reader = NULL;

		pr_debug("Closing debugger interface.\n");
		acpi_aml_io.flags |= ACPI_AML_CLOSED;

		/*
		 * Wake up all user space/kernel space blocked
		 * readers/writers.
		 */
		wake_up_interruptible(&acpi_aml_io.wait);
		mutex_unlock(&acpi_aml_io.lock);
		/*
		 * Wait all user space/kernel space readers/writers to
		 * stop so that ACPICA command loop of the debugger thread
		 * should fail all its command line reads after this point.
		 */
		wait_event(acpi_aml_io.wait, !acpi_aml_busy());

		/*
		 * Then we try to terminate the debugger thread if it is
		 * not terminated.
		 */
		pr_debug("Terminating debugger thread.\n");
		acpi_terminate_debugger();
		wait_event(acpi_aml_io.wait, !acpi_aml_used());
		pr_debug("Debugger thread terminated.\n");

		mutex_lock(&acpi_aml_io.lock);
		acpi_aml_io.flags &= ~ACPI_AML_OPENED;
	}
	if (acpi_aml_io.users == 0) {
		pr_debug("Debugger interface closed.\n");
		acpi_aml_io.flags &= ~ACPI_AML_CLOSED;
	}
	mutex_unlock(&acpi_aml_io.lock);
	return 0;
}

static int acpi_aml_read_user(char __user *buf, int len)
{
	int ret;
	struct circ_buf *crc = &acpi_aml_io.out_crc;
	int n;
	char *p;

	ret = acpi_aml_lock_read(crc, ACPI_AML_OUT_USER);
	if (IS_ERR_VALUE(ret))
		return ret;
	/* sync head before removing logs */
	smp_rmb();
	p = &crc->buf[crc->tail];
	n = min(len, circ_count_to_end(crc));
	if (copy_to_user(buf, p, n)) {
		ret = -EFAULT;
		goto out;
	}
	/* sync tail after removing logs */
	smp_mb();
	crc->tail = (crc->tail + n) & (ACPI_AML_BUF_SIZE - 1);
	ret = n;
out:
	acpi_aml_unlock_fifo(ACPI_AML_OUT_USER, !IS_ERR_VALUE(ret));
	return ret;
}

static ssize_t acpi_aml_read(struct file *file, char __user *buf,
			     size_t count, loff_t *ppos)
{
	int ret = 0;
	int size = 0;

	if (!count)
		return 0;
	if (!access_ok(VERIFY_WRITE, buf, count))
		return -EFAULT;

	while (count > 0) {
again:
		ret = acpi_aml_read_user(buf + size, count);
		if (ret == -EAGAIN) {
			if (file->f_flags & O_NONBLOCK)
				break;
			else {
				ret = wait_event_interruptible(acpi_aml_io.wait,
					acpi_aml_user_readable());
				/*
				 * We need to retry when the condition
				 * becomes true.
				 */
				if (ret == 0)
					goto again;
			}
		}
		if (IS_ERR_VALUE(ret)) {
			if (!acpi_aml_running())
				ret = 0;
			break;
		}
		if (ret) {
			size += ret;
			count -= ret;
			*ppos += ret;
			break;
		}
	}
	return size > 0 ? size : ret;
}

static int acpi_aml_write_user(const char __user *buf, int len)
{
	int ret;
	struct circ_buf *crc = &acpi_aml_io.in_crc;
	int n;
	char *p;

	ret = acpi_aml_lock_write(crc, ACPI_AML_IN_USER);
	if (IS_ERR_VALUE(ret))
		return ret;
	/* sync tail before inserting cmds */
	smp_mb();
	p = &crc->buf[crc->head];
	n = min(len, circ_space_to_end(crc));
	if (copy_from_user(p, buf, n)) {
		ret = -EFAULT;
		goto out;
	}
	/* sync head after inserting cmds */
	smp_wmb();
	crc->head = (crc->head + n) & (ACPI_AML_BUF_SIZE - 1);
	ret = n;
out:
	acpi_aml_unlock_fifo(ACPI_AML_IN_USER, !IS_ERR_VALUE(ret));
	return n;
}

static ssize_t acpi_aml_write(struct file *file, const char __user *buf,
			      size_t count, loff_t *ppos)
{
	int ret = 0;
	int size = 0;

	if (!count)
		return 0;
	if (!access_ok(VERIFY_READ, buf, count))
		return -EFAULT;

	while (count > 0) {
again:
		ret = acpi_aml_write_user(buf + size, count);
		if (ret == -EAGAIN) {
			if (file->f_flags & O_NONBLOCK)
				break;
			else {
				ret = wait_event_interruptible(acpi_aml_io.wait,
					acpi_aml_user_writable());
				/*
				 * We need to retry when the condition
				 * becomes true.
				 */
				if (ret == 0)
					goto again;
			}
		}
		if (IS_ERR_VALUE(ret)) {
			if (!acpi_aml_running())
				ret = 0;
			break;
		}
		if (ret) {
			size += ret;
			count -= ret;
			*ppos += ret;
		}
	}
	return size > 0 ? size : ret;
}

static unsigned int acpi_aml_poll(struct file *file, poll_table *wait)
{
	int masks = 0;

	poll_wait(file, &acpi_aml_io.wait, wait);
	if (acpi_aml_user_readable())
		masks |= POLLIN | POLLRDNORM;
	if (acpi_aml_user_writable())
		masks |= POLLOUT | POLLWRNORM;

	return masks;
}

static const struct file_operations acpi_aml_operations = {
	.read		= acpi_aml_read,
	.write		= acpi_aml_write,
	.poll		= acpi_aml_poll,
	.open		= acpi_aml_open,
	.release	= acpi_aml_release,
	.llseek		= generic_file_llseek,
};

static const struct acpi_debugger_ops acpi_aml_debugger = {
	.create_thread		 = acpi_aml_create_thread,
	.read_cmd		 = acpi_aml_read_cmd,
	.write_log		 = acpi_aml_write_log,
	.wait_command_ready	 = acpi_aml_wait_command_ready,
	.notify_command_complete = acpi_aml_notify_command_complete,
};

int __init acpi_aml_init(void)
{
	int ret = 0;

	if (!acpi_debugfs_dir) {
		ret = -ENOENT;
		goto err_exit;
	}

	/* Initialize AML IO interface */
	mutex_init(&acpi_aml_io.lock);
	init_waitqueue_head(&acpi_aml_io.wait);
	acpi_aml_io.out_crc.buf = acpi_aml_io.out_buf;
	acpi_aml_io.in_crc.buf = acpi_aml_io.in_buf;
	acpi_aml_dentry = debugfs_create_file("acpidbg",
					      S_IFREG | S_IRUGO | S_IWUSR,
					      acpi_debugfs_dir, NULL,
					      &acpi_aml_operations);
	if (acpi_aml_dentry == NULL) {
		ret = -ENODEV;
		goto err_exit;
	}
	ret = acpi_register_debugger(THIS_MODULE, &acpi_aml_debugger);
	if (ret)
		goto err_fs;
	acpi_aml_initialized = true;

err_fs:
	if (ret) {
		debugfs_remove(acpi_aml_dentry);
		acpi_aml_dentry = NULL;
	}
err_exit:
	return ret;
}

void __exit acpi_aml_exit(void)
{
	if (acpi_aml_initialized) {
		acpi_unregister_debugger(&acpi_aml_debugger);
		if (acpi_aml_dentry) {
			debugfs_remove(acpi_aml_dentry);
			acpi_aml_dentry = NULL;
		}
		acpi_aml_initialized = false;
	}
}

module_init(acpi_aml_init);
module_exit(acpi_aml_exit);

MODULE_AUTHOR("Lv Zheng");
MODULE_DESCRIPTION("ACPI debugger userspace IO driver");
MODULE_LICENSE("GPL");