summaryrefslogtreecommitdiff
path: root/drivers/staging/fsl_pme2/pme2_scan.c
blob: 2c4df402f980b51c9179458fabc311cdc9cdf20c (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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
/* Copyright 2009-2011 Freescale Semiconductor, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Freescale Semiconductor nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 *
 * ALTERNATIVELY, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") as published by the Free Software
 * Foundation, either version 2 of that License or (at your option) any
 * later version.
 *
 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "pme2_private.h"
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/poll.h>
#include <linux/compat.h>

#define WAIT_AND_INTERRUPTABLE	(PME_CTX_OP_WAIT|PME_CTX_OP_WAIT_INT)
#define INPUT_FRM	1
#define OUTPUT_FRM	0
/* Private structure that is allocated for each open that is done on the
 * pme_scan device. */
struct scan_session {
	/* The ctx that is needed to communicate with the pme high level */
	struct pme_ctx ctx;
	/* Locks completed_commands */
	spinlock_t set_subset_lock;
	__u8 set;
	__u16 subset;
	/* For asynchronous processing */
	wait_queue_head_t waiting_for_completion;
	struct list_head completed_commands;
	/* Locks completed_commands */
	spinlock_t completed_commands_lock;
	u32 completed_count;
};

/* Command Token for scan operations. One of these is created for every
 * operation on a context. When the context operation is complete cleanup
 * is done */
struct cmd_token {
	/* pme high level token */
	struct pme_ctx_token hl_token;
	/* The kernels copy of the user op structure */
	struct pme_scan_cmd kernel_op;
	/* Set to non zero if this is a synchronous request */
	u8 synchronous;
	/* data */
	struct qm_fd tx_fd;
	struct qm_sg_entry tx_comp[2];
	struct qm_fd rx_fd;
	void *tx_data;
	size_t tx_size;
	void *rx_data;
	size_t rx_size;
	/* For blocking requests, we need a wait point and condition */
	wait_queue_head_t *queue;
	/* List management for completed async requests */
	struct list_head completed_list;
	u8 done;
	u8 ern;
};

struct ctrl_op {
	struct pme_ctx_ctrl_token ctx_ctr;
	struct completion cb_done;
	enum pme_status cmd_status;
	u8 res_flag;
	u8 ern;
};

#ifdef CONFIG_COMPAT
static void compat_to_scan_cmd(struct pme_scan_cmd *dst,
			struct compat_pme_scan_cmd *src)
{
	dst->flags = src->flags;
	dst->opaque = compat_ptr(src->opaque);
	dst->input.data = compat_ptr(src->input.data);
	dst->input.size = src->input.size;
	dst->output.data = compat_ptr(src->output.data);
	dst->output.size = src->output.size;
}

static void scan_result_to_compat(struct compat_pme_scan_result *dst,
			struct pme_scan_result *src)
{
	dst->flags = src->flags;
	dst->opaque = ptr_to_compat(src->opaque);
	dst->status = src->status;
	dst->output.data = ptr_to_compat(src->output.data);
	dst->output.size = src->output.size;
}

static void compat_to_scan_result(struct pme_scan_result *dst,
			struct compat_pme_scan_result *src)
{
	dst->flags = src->flags;
	dst->opaque = compat_ptr(src->opaque);
	dst->status = src->status;
	dst->output.data = compat_ptr(src->output.data);
	dst->output.size = src->output.size;
}
#endif

static void ctrl_cb(struct pme_ctx *ctx, const struct qm_fd *fd,
		struct pme_ctx_ctrl_token *token)
{
	struct ctrl_op *ctrl = (struct ctrl_op *)token;
	ctrl->cmd_status = pme_fd_res_status(fd);
	ctrl->res_flag = pme_fd_res_flags(fd) & PME_STATUS_UNRELIABLE;
	complete(&ctrl->cb_done);
}

static void ctrl_ern_cb(struct pme_ctx *ctx, const struct qm_mr_entry *mr,
		struct pme_ctx_ctrl_token *token)
{
	struct ctrl_op *ctrl = (struct ctrl_op *)token;
	ctrl->ern = 1;
	complete(&ctrl->cb_done);
}

static inline int scan_data_empty(struct scan_session *session)
{
	return list_empty(&session->completed_commands);
}

/* Cleanup for the execute_cmd method */
static inline void cleanup_token(struct cmd_token *token_p)
{
	kfree(token_p->tx_data);
	kfree(token_p->rx_data);
	return;
}

/* Callback for scan operations */
static void scan_cb(struct pme_ctx *ctx, const struct qm_fd *fd,
				struct pme_ctx_token *ctx_token)
{
	struct cmd_token *token = (struct cmd_token *)ctx_token;
	struct scan_session *session = (struct scan_session *)ctx;

	token->rx_fd = *fd;
	/* If this is a asynchronous command, queue the token */
	if (!token->synchronous) {
		spin_lock(&session->completed_commands_lock);
		list_add_tail(&token->completed_list,
			      &session->completed_commands);
		session->completed_count++;
		spin_unlock(&session->completed_commands_lock);
	}
	/* Wake up the thread that's waiting for us */
	token->done = 1;
	wake_up(token->queue);
	return;
}

static void scan_ern_cb(struct pme_ctx *ctx, const struct qm_mr_entry *mr,
		struct pme_ctx_token *ctx_token)
{
	struct cmd_token *token = (struct cmd_token *)ctx_token;
	struct scan_session *session = (struct scan_session *)ctx;

	token->ern = 1;
	token->rx_fd = mr->ern.fd;
	/* If this is a asynchronous command, queue the token */
	if (!token->synchronous) {
		spin_lock(&session->completed_commands_lock);
		list_add_tail(&token->completed_list,
			      &session->completed_commands);
		session->completed_count++;
		spin_unlock(&session->completed_commands_lock);
	}
	/* Wake up the thread that's waiting for us */
	token->done = 1;
	wake_up(token->queue);
	return;
}

static int process_completed_token(struct file *fp, struct cmd_token *token_p,
					struct pme_scan_result *scan_result)
{
	int ret = 0;
	u32 src_sz, dst_sz;

	memset(scan_result, 0, sizeof(struct pme_scan_result));
	if (token_p->ern) {
		ret = -EIO;
		goto done;
	}
	scan_result->output.data = token_p->kernel_op.output.data;

	if (token_p->rx_fd.format == qm_fd_compound) {
		/* Need to copy  output */
		src_sz = token_p->tx_comp[OUTPUT_FRM].length;
		dst_sz = token_p->kernel_op.output.size;
		scan_result->output.size = min(dst_sz, src_sz);
		/* Doesn't make sense we generated more than available space
		 * should have got truncation.
		 */
		BUG_ON(dst_sz < src_sz);
		if (copy_to_user(scan_result->output.data, token_p->rx_data,
				scan_result->output.size)) {
			pr_err("Error copying to user data\n");
			cleanup_token(token_p);
			return -EFAULT;
		}
	} else if (token_p->rx_fd.format == qm_fd_sg_big)
		scan_result->output.size = 0;
	else
		pr_err("pme2_scan: unexpected frame type received\n");

	scan_result->flags |= pme_fd_res_flags(&token_p->rx_fd);
	scan_result->status |= pme_fd_res_status(&token_p->rx_fd);
done:
	scan_result->opaque = token_p->kernel_op.opaque;
	cleanup_token(token_p);
	return ret;
}

static int getscan_cmd(struct file *fp, struct scan_session *session,
	struct pme_scan_params __user *user_scan_params)
{
	int ret = 0;
	struct pme_flow params;
	struct pme_scan_params local_scan_params;
	struct ctrl_op ctx_ctrl =  {
		.ctx_ctr.cb = ctrl_cb,
		.ctx_ctr.ern_cb = ctrl_ern_cb,
		.cmd_status = 0,
		.res_flag = 0,
		.ern = 0
	};
	init_completion(&ctx_ctrl.cb_done);

	memset(&local_scan_params, 0, sizeof(local_scan_params));

	/* must be enabled */
	if (pme_ctx_is_disabled(&session->ctx)) {
		pr_err("pme2_scan: ctx is disabled\n");
		ret = -EINVAL;
		goto done;
	}
	ret = pme_ctx_ctrl_read_flow(&session->ctx, WAIT_AND_INTERRUPTABLE,
			&params, &ctx_ctrl.ctx_ctr);
	if (ret) {
		PMEPRINFO("read flow error %d\n", ret);
		goto done;
	}
	wait_for_completion(&ctx_ctrl.cb_done);

	if (ctx_ctrl.ern || ctx_ctrl.cmd_status || ctx_ctrl.res_flag) {
		PMEPRINFO("read flow error %d\n", ctx_ctrl.cmd_status);
		ret = -EFAULT;
		goto done;
	}
	local_scan_params.residue.enable = params.ren;
	local_scan_params.residue.length = params.rlen;
	local_scan_params.sre.sessionid = params.sessionid;
	local_scan_params.sre.verbose = params.srvm;
	local_scan_params.sre.esee = params.esee;
	local_scan_params.dxe.clim = params.clim;
	local_scan_params.dxe.mlim = params.mlim;
	spin_lock(&session->set_subset_lock);
	local_scan_params.pattern.set = session->set;
	local_scan_params.pattern.subset = session->subset;
	spin_unlock(&session->set_subset_lock);

	if (copy_to_user(user_scan_params, &local_scan_params,
			sizeof(local_scan_params))) {
		pr_err("Error copying to user data\n");
		ret = -EFAULT;
	}
done:
	return ret;
}

static int setscan_cmd(struct file *fp, struct scan_session *session,
	struct pme_scan_params __user *user_params)
{
	int ret = 0;
	u32 flag = WAIT_AND_INTERRUPTABLE;
	struct ctrl_op ctx_ctrl =  {
		.ctx_ctr.cb = ctrl_cb,
		.ctx_ctr.ern_cb = ctrl_ern_cb,
		.cmd_status = 0,
		.res_flag = 0,
		.ern = 0
	};
	struct pme_flow params;
	struct pme_scan_params local_params;

	pme_sw_flow_init(&params);
	init_completion(&ctx_ctrl.cb_done);
	if (copy_from_user(&local_params, user_params, sizeof(local_params)))
		return -EFAULT;

	/* must be enabled */
	if (pme_ctx_is_disabled(&session->ctx)) {
		ret = -EINVAL;
		goto done;
	}
	/* Only send a flw_ctx_w if PME_SCAN_PARAMS_{RESIDUE, SRE or DXE}
	 * is being done */
	if (local_params.flags == PME_SCAN_PARAMS_PATTERN)
		goto set_subset;
	if (local_params.flags & PME_SCAN_PARAMS_RESIDUE)
		flag |= PME_CMD_FCW_RES;
	if (local_params.flags & PME_SCAN_PARAMS_SRE)
		flag |= PME_CMD_FCW_SRE;
	if (local_params.flags & PME_SCAN_PARAMS_DXE)
		flag |= PME_CMD_FCW_DXE;
	params.ren = local_params.residue.enable;
	params.sessionid = local_params.sre.sessionid;
	params.srvm = local_params.sre.verbose;
	params.esee = local_params.sre.esee;
	params.clim = local_params.dxe.clim;
	params.mlim = local_params.dxe.mlim;

	ret = pme_ctx_ctrl_update_flow(&session->ctx, flag, &params,
			&ctx_ctrl.ctx_ctr);
	if (ret) {
		PMEPRINFO("update flow error %d\n", ret);
		goto done;
	}
	wait_for_completion(&ctx_ctrl.cb_done);
	if (ctx_ctrl.ern || ctx_ctrl.cmd_status || ctx_ctrl.res_flag) {
		PMEPRINFO("update flow err %d\n", ctx_ctrl.cmd_status);
		ret = -EFAULT;
		goto done;
	}

set_subset:
	if (local_params.flags & PME_SCAN_PARAMS_PATTERN) {
		spin_lock(&session->set_subset_lock);
		session->set = local_params.pattern.set;
		session->subset = local_params.pattern.subset;
		spin_unlock(&session->set_subset_lock);
		goto done;
	}
done:
	return ret;
}

static int resetseq_cmd(struct file *fp, struct scan_session *session)
{
	int ret = 0;
	struct pme_flow params;
	struct ctrl_op ctx_ctrl =  {
		.ctx_ctr.cb = ctrl_cb,
		.ctx_ctr.ern_cb = ctrl_ern_cb,
		.cmd_status = 0,
		.res_flag = 0,
		.ern = 0
	};
	init_completion(&ctx_ctrl.cb_done);
	pme_sw_flow_init(&params);

	/* must be enabled */
	if (pme_ctx_is_disabled(&session->ctx)) {
		pr_err("pme2_scan: ctx is disabled\n");
		ret =  -EINVAL;
		goto done;
	}
	pme_flow_seqnum_set64(&params, 0);
	params.sos = 1;

	ret = pme_ctx_ctrl_update_flow(&session->ctx, PME_CMD_FCW_SEQ, &params,
			&ctx_ctrl.ctx_ctr);
	if (ret) {
		pr_err("pme2_scan: update flow error %d\n", ret);
		return ret;
	}
	wait_for_completion(&ctx_ctrl.cb_done);
	if (ctx_ctrl.ern || ctx_ctrl.cmd_status || ctx_ctrl.res_flag) {
		PMEPRINFO("update flow err %d\n", ctx_ctrl.cmd_status);
		ret = -EFAULT;
	}
done:
	return ret;
}

static int resetresidue_cmd(struct file *fp, struct scan_session *session)
{
	int ret = 0;
	struct pme_flow params;
	struct ctrl_op ctx_ctrl =  {
		.ctx_ctr.cb = ctrl_cb,
		.ctx_ctr.ern_cb = ctrl_ern_cb,
		.cmd_status = 0,
		.res_flag = 0,
		.ern = 0
	};

	init_completion(&ctx_ctrl.cb_done);
	pme_sw_flow_init(&params);
	/* must be enabled */
	if (pme_ctx_is_disabled(&session->ctx)) {
		pr_err("pme2_scan: ctx is disabled\n");
		ret =  -EINVAL;
		goto done;
	}
	params.rlen = 0;
	ret = pme_ctx_ctrl_update_flow(&session->ctx,
			WAIT_AND_INTERRUPTABLE | PME_CTX_OP_RESETRESLEN,
			&params, &ctx_ctrl.ctx_ctr);
	if (ret)
		pr_info("pme2_scan: update flow error %d\n", ret);
	wait_for_completion(&ctx_ctrl.cb_done);
	if (ctx_ctrl.ern || ctx_ctrl.cmd_status || ctx_ctrl.res_flag) {
		PMEPRINFO("update flow err %d\n", ctx_ctrl.cmd_status);
		ret = -EFAULT;
	}
done:
	return ret;
}

static int process_scan_cmd(
		struct file *fp,
		struct scan_session *session,
		struct pme_scan_cmd *user_cmd,
		struct pme_scan_result *user_ret,
		u8 synchronous)
{
	int ret = 0;
	struct cmd_token local_token;
	struct cmd_token *token_p = NULL;
	DECLARE_WAIT_QUEUE_HEAD(local_waitqueue);
	u8 scan_flags = 0;

	BUG_ON(synchronous && !user_ret);

	/* If synchronous, use a local token (from the stack)
	 * If asynchronous, allocate a token to use */
	if (synchronous)
		token_p = &local_token;
	else {
		token_p = kmalloc(sizeof(*token_p), GFP_KERNEL);
		if (!token_p)
			return -ENOMEM;
	}
	memset(token_p, 0, sizeof(*token_p));
	/* Copy the command to kernel space */
	memcpy(&token_p->kernel_op, user_cmd, sizeof(struct pme_scan_cmd));

#ifdef CONFIG_FSL_PME_BUG_4K_SCAN_REV_2_1_4
	if (session->ctx.max_scan_size) {
		if (token_p->kernel_op.input.size >
				session->ctx.max_scan_size) {
			if (!synchronous)
				kfree(token_p);
			return -EINVAL;
		}
	}
#endif

	/* Copy the input */
	token_p->synchronous = synchronous;
	token_p->tx_size = token_p->kernel_op.input.size;
	token_p->tx_data = kmalloc(token_p->kernel_op.input.size, GFP_KERNEL);
	if (!token_p->tx_data) {
		pr_err("pme2_scan: Err alloc %zd byte", token_p->tx_size);
		cleanup_token(token_p);
		return -ENOMEM;
	}
	if (copy_from_user(token_p->tx_data,
			token_p->kernel_op.input.data,
			token_p->kernel_op.input.size)) {
		pr_err("Error copying contigous user data\n");
		cleanup_token(token_p);
		return -EFAULT;
	}
	/* Setup input frame */
	token_p->tx_comp[INPUT_FRM].final = 1;
	token_p->tx_comp[INPUT_FRM].length = token_p->tx_size;
	qm_sg_entry_set64(&token_p->tx_comp[INPUT_FRM],
			pme_map(token_p->tx_data));
	/* setup output frame, if output is expected */
	if (token_p->kernel_op.output.size) {
		token_p->rx_size = token_p->kernel_op.output.size;
		PMEPRINFO("pme2_scan: expect output %d\n", token_p->rx_size);
		token_p->rx_data = kmalloc(token_p->rx_size, GFP_KERNEL);
		if (!token_p->rx_data) {
			pr_err("pme2_scan: Err alloc %zd byte",
					token_p->rx_size);
			cleanup_token(token_p);
			return -ENOMEM;
		}
		/* Setup output frame */
		token_p->tx_comp[OUTPUT_FRM].length = token_p->rx_size;
		qm_sg_entry_set64(&token_p->tx_comp[OUTPUT_FRM],
				pme_map(token_p->rx_data));
		token_p->tx_fd.format = qm_fd_compound;
		/* Build compound frame */
		qm_fd_addr_set64(&token_p->tx_fd,
				pme_map(token_p->tx_comp));
	} else {
		token_p->tx_fd.format = qm_fd_sg_big;
		/* Build sg frame */
		qm_fd_addr_set64(&token_p->tx_fd,
				pme_map(&token_p->tx_comp[INPUT_FRM]));
		token_p->tx_fd.length29 = token_p->tx_size;
	}

	/* use the local wait queue if synchronous, the shared
	 * queue if asynchronous */
	if (synchronous)
		token_p->queue = &local_waitqueue;
	else
		token_p->queue = &session->waiting_for_completion;
	token_p->done = 0;

	if (token_p->kernel_op.flags & PME_SCAN_CMD_STARTRESET)
		scan_flags |= PME_CMD_SCAN_SR;
	if (token_p->kernel_op.flags & PME_SCAN_CMD_END)
		scan_flags |= PME_CMD_SCAN_E;
	ret = pme_ctx_scan(&session->ctx, WAIT_AND_INTERRUPTABLE,
		&token_p->tx_fd,
		PME_SCAN_ARGS(scan_flags, session->set, session->subset),
		&token_p->hl_token);
	if (unlikely(ret)) {
		cleanup_token(token_p);
		return ret;
	}

	if (!synchronous)
		/* Don't wait.  The command is away */
		return 0;

	PMEPRINFO("Wait for completion\n");
	/* Wait for the command to complete */
	/* TODO: Should this be wait_event_interruptible ?
	 * If so, will need logic to indicate */
	wait_event(*token_p->queue, token_p->done == 1);
	return process_completed_token(fp, token_p, user_ret);
}

/**
 * fsl_pme2_scan_open - open the driver
 *
 * Open the driver and prepare for requests.
 *
 * Every time an application opens the driver, we create a scan_session object
 * for that file handle.
 */
static int fsl_pme2_scan_open(struct inode *node, struct file *fp)
{
	int ret;
	struct scan_session *session;
	struct pme_flow flow;
	struct ctrl_op ctx_ctrl =  {
		.ctx_ctr.cb = ctrl_cb,
		.ctx_ctr.ern_cb = ctrl_ern_cb,
		.cmd_status = 0,
		.res_flag = 0,
		.ern = 0
	};

	pme_sw_flow_init(&flow);
	init_completion(&ctx_ctrl.cb_done);
	PMEPRINFO("pme2_scan: open %d\n", smp_processor_id());
	fp->private_data = kzalloc(sizeof(*session), GFP_KERNEL);
	if (!fp->private_data)
		return -ENOMEM;
	session = (struct scan_session *)fp->private_data;
	/* Set up the structures used for asynchronous requests */
	init_waitqueue_head(&session->waiting_for_completion);
	INIT_LIST_HEAD(&session->completed_commands);
	spin_lock_init(&session->completed_commands_lock);
	spin_lock_init(&session->set_subset_lock);
	PMEPRINFO("kmalloc session %p\n", fp->private_data);
	session = fp->private_data;
	session->ctx.cb = scan_cb;
	session->ctx.ern_cb = scan_ern_cb;

	/* qosin, qosout should be driver attributes */
	ret = pme_ctx_init(&session->ctx, PME_CTX_FLAG_LOCAL, 0, 4, 4, 0, NULL);
	if (ret) {
		pr_err("pme2_scan: pme_ctx_init %d\n", ret);
		goto exit;
	}
	/* enable the context */
	ret = pme_ctx_enable(&session->ctx);
	if (ret) {
		PMEPRINFO("error enabling ctx %d\n", ret);
		pme_ctx_finish(&session->ctx);
		goto exit;
	}
	/* Update flow to set sane defaults in the flow context */
	ret = pme_ctx_ctrl_update_flow(&session->ctx,
		PME_CTX_OP_WAIT | PME_CMD_FCW_ALL, &flow, &ctx_ctrl.ctx_ctr);
	if (!ret) {
		wait_for_completion(&ctx_ctrl.cb_done);
		if (ctx_ctrl.ern || ctx_ctrl.cmd_status || ctx_ctrl.res_flag)
			ret = -EFAULT;
	}
	if (ret) {
		int my_ret;
		PMEPRINFO("error updating flow ctx %d\n", ret);
		my_ret = pme_ctx_disable(&session->ctx, PME_CTX_OP_WAIT,
					&ctx_ctrl.ctx_ctr);
		if (my_ret > 0)
			wait_for_completion(&ctx_ctrl.cb_done);
		else if (my_ret < 0)
			PMEPRINFO("error disabling ctx %d\n", ret);
		pme_ctx_finish(&session->ctx);
		goto exit;
	}
	/* Set up the structures used for asynchronous requests */
	PMEPRINFO("pme2_scan: Finish pme_scan open %d\n", smp_processor_id());
	return 0;
exit:
	kfree(fp->private_data);
	fp->private_data = NULL;
	return ret;
}

static int fsl_pme2_scan_close(struct inode *node, struct file *fp)
{
	struct ctrl_op ctx_ctrl =  {
		.ctx_ctr.cb = ctrl_cb,
		.ctx_ctr.ern_cb = ctrl_ern_cb,
		.cmd_status = 0,
		.res_flag = 0,
		.ern = 0
	};
	int ret = 0;
	struct scan_session *session = fp->private_data;

	init_completion(&ctx_ctrl.cb_done);
	/* Before disabling check to see if it's already disabled. This can
	 * happen if a pme serious error has occurred for instance.*/
	if (!pme_ctx_is_disabled(&session->ctx)) {
		ret = pme_ctx_disable(&session->ctx, PME_CTX_OP_WAIT,
					&ctx_ctrl.ctx_ctr);
		if (ret > 0) {
			wait_for_completion(&ctx_ctrl.cb_done);
			if (ctx_ctrl.ern)
				PMEPRCRIT("Unexpected ERN\n");
		} else if (ret < 0) {
			pr_err("pme2_scan: Error disabling ctx %d\n", ret);
			return ret;
		}
	}
	pme_ctx_finish(&session->ctx);
	kfree(session);
	PMEPRINFO("pme2_scan: Finish pme_session close\n");
	return 0;
}

static unsigned int fsl_pme2_scan_poll(struct file *fp,
				      struct poll_table_struct *wait)
{
	struct scan_session *session;
	unsigned int mask = POLLOUT | POLLWRNORM;

	if (!fp->private_data)
		return -EINVAL;

	session = (struct scan_session *)fp->private_data;

	poll_wait(fp, &session->waiting_for_completion, wait);

	if (!scan_data_empty(session))
		mask |= (POLLIN | POLLRDNORM);
	return mask;
}


/* Main switch loop for ioctl operations */
static long fsl_pme2_scan_ioctl(struct file *fp, unsigned int cmd,
				unsigned long arg)
{
	struct scan_session *session = fp->private_data;
	int ret = 0;

	switch (cmd) {

	case PMEIO_GETSCAN:
		return getscan_cmd(fp, session, (struct pme_scan_params *)arg);
	break;

	case PMEIO_SETSCAN:
		return setscan_cmd(fp, session, (struct pme_scan_params *)arg);
	break;

	case PMEIO_RESETSEQ:
		return resetseq_cmd(fp, session);
	break;

	case PMEIO_RESETRES:
		return resetresidue_cmd(fp, session);
	break;

	case PMEIO_SCAN:
	{
		int ret;
		struct pme_scan scan;

		if (copy_from_user(&scan, (void __user *)arg, sizeof(scan)))
			return -EFAULT;
		ret = process_scan_cmd(fp, session, &scan.cmd, &scan.result, 1);
		if (!ret) {
			struct pme_scan_result __user *user_result =
				&((struct pme_scan __user *)arg)->result;
			ret = copy_to_user(user_result, &scan.result,
						sizeof(*user_result));
		}
		return ret;
	}
	break;

	case PMEIO_SCAN_W1:
	{
		struct pme_scan_cmd scan_cmd;

		if (copy_from_user(&scan_cmd, (void __user *)arg,
					sizeof(scan_cmd)))
			return -EFAULT;
		return process_scan_cmd(fp, session, &scan_cmd, NULL, 0);
	}
	break;

	case PMEIO_SCAN_R1:
	{
		struct pme_scan_result result;
		struct cmd_token *completed_cmd = NULL;
		struct pme_scan_result __user *ur =
			(struct pme_scan_result __user *)arg;
		int ret;

		if (copy_from_user(&result, (void __user *)arg,
				sizeof(result)))
			return -EFAULT;

		/* Check to see if any results */
		spin_lock(&session->completed_commands_lock);
		if (!list_empty(&session->completed_commands)) {
			completed_cmd = list_first_entry(
					&session->completed_commands,
					struct cmd_token,
					completed_list);
			list_del(&completed_cmd->completed_list);
			session->completed_count--;
		}
		spin_unlock(&session->completed_commands_lock);
		if (completed_cmd) {
			ret = process_completed_token(fp, completed_cmd,
					&result);
			if (!ret)
				ret = copy_to_user(ur, &result, sizeof(result));
			return ret;
		} else
			return -EIO;
	}
	break;

	case PMEIO_SCAN_Wn:
	{
		struct pme_scan_cmds scan_cmds;
		int i, ret = 0;

		/* Copy the command to kernel space */
		if (copy_from_user(&scan_cmds, (void __user *)arg,
				sizeof(scan_cmds)))
			return -EFAULT;
		PMEPRINFO("Received Wn for %d cmds\n", scan_cmds.num);
		for (i = 0; i < scan_cmds.num; i++) {
			struct pme_scan_cmd scan_cmd;

			if (copy_from_user(&scan_cmd, &scan_cmds.cmds[i],
					sizeof(scan_cmd))) {
				pr_err("pme2_scan: Err with %d\n", i);
				scan_cmds.num = i;
				if (copy_to_user((void __user *)arg, &scan_cmds,
						sizeof(scan_cmds))) {
					return -EFAULT;
				}
				return -EFAULT;
			}
			ret = process_scan_cmd(fp, session, &scan_cmd, NULL, 0);
			if (ret) {
				pr_err("pme2_scan: Err with %d cmd %d\n",
					i, ret);
				scan_cmds.num = i;
				if (copy_to_user((void *)arg, &scan_cmds,
						sizeof(scan_cmds))) {
					pr_err("Error copying to user data\n");
					return -EFAULT;
				}
				return -EINTR;
			}
		}
		return ret;
	}
	break;

	case PMEIO_SCAN_Rn:
	{
		struct pme_scan_results results;
		struct pme_scan_result result;
		struct pme_scan_result __user *ur;
		int i = 0, ret = 0;
		struct cmd_token *completed_cmd = NULL;

		/* Copy the command to kernel space */
		if (copy_from_user(&results, (void __user *)arg,
				sizeof(results)))
			return -EFAULT;
		ur = ((struct pme_scan_results __user *)arg)->results
		PMEPRINFO("pme2_scan: Received Rn for %d res\n", results.num);
		if (!results.num)
			return 0;
		do {
			completed_cmd = NULL;
			ret = 0;
			/* Check to see if any results */
			spin_lock(&session->completed_commands_lock);
			if (!list_empty(&session->completed_commands)) {
				/* Move to a different list */
				PMEPRINFO("pme2_scan: Pop response\n");
				completed_cmd = list_first_entry(
						&session->completed_commands,
						struct cmd_token,
						completed_list);
				list_del(&completed_cmd->completed_list);
				session->completed_count--;
			}
			spin_unlock(&session->completed_commands_lock);
			if (completed_cmd) {
				if (copy_from_user(&result, (void __user *)ur+i,
						sizeof(result)))
					return -EFAULT;
				ret = process_completed_token(fp, completed_cmd,
						&result);
				if (!ret)
					ret = copy_to_user(ur, &result,
						sizeof(struct pme_scan_result));
				if (!ret) {
					i++;
					ur++;
				}
			}
		} while (!ret && completed_cmd && (i != results.num));

		if (i != results.num) {
			PMEPRINFO("pme2_scan: Only filled %d responses\n", i);
			results.num = i;
			PMEPRINFO("pme2_scan: results.num = %d\n", results.num);
			if (copy_to_user((void __user *)arg, &results,
					sizeof(struct pme_scan_results))) {
				pr_err("Error copying to user data\n");
				return -EFAULT;
			}
		}
		return ret;
	}
	break;

	case PMEIO_RELEASE_BUFS:
		return -EINVAL;
		break;

#ifdef CONFIG_COMPAT
	case PMEIO_SCAN32:
	{
		int ret;
		struct compat_pme_scan scan32;
		struct compat_pme_scan __user *user_scan = compat_ptr(arg);
		struct pme_scan scan;

		if (copy_from_user(&scan32, user_scan, sizeof(scan32)))
			return -EFAULT;
		/* Convert to 64-bit structs */
		compat_to_scan_cmd(&scan.cmd, &scan32.cmd);
		compat_to_scan_result(&scan.result, &scan32.result);

		ret = process_scan_cmd(fp, session, &scan.cmd, &scan.result, 1);
		if (!ret) {
			struct compat_pme_scan_result __user *user_result =
				&user_scan->result;
			/* Convert to 32-bit struct */
			scan_result_to_compat(&scan32.result, &scan.result);
			ret = copy_to_user(user_result, &scan32.result,
						sizeof(*user_result));
		}
		return ret;
	}
	break;

	case PMEIO_SCAN_W132:
	{
		struct compat_pme_scan_cmd scan_cmd32;
		struct pme_scan_cmd scan_cmd;

		if (copy_from_user(&scan_cmd32, compat_ptr(arg),
				sizeof(scan_cmd32)))
			return -EFAULT;
		/* Convert to 64-bit struct */
		compat_to_scan_cmd(&scan_cmd, &scan_cmd32);
		return process_scan_cmd(fp, session, &scan_cmd, NULL, 0);
	}
	break;

	case PMEIO_SCAN_R132:
	{
		struct compat_pme_scan_result result32;
		struct pme_scan_result result;
		struct cmd_token *completed_cmd = NULL;
		struct compat_pme_scan_result __user *ur = compat_ptr(arg);
		int ret;

		if (copy_from_user(&result32, (void __user *)arg,
				sizeof(result32)))
			return -EFAULT;
		/* copy to 64-bit structure */
		compat_to_scan_result(&result, &result32);

		/* Check to see if any results */
		spin_lock(&session->completed_commands_lock);
		if (!list_empty(&session->completed_commands)) {
			completed_cmd = list_first_entry(
					&session->completed_commands,
					struct cmd_token,
					completed_list);
			list_del(&completed_cmd->completed_list);
			session->completed_count--;
		}
		spin_unlock(&session->completed_commands_lock);
		if (completed_cmd) {
			ret =  process_completed_token(fp, completed_cmd,
					&result);
			scan_result_to_compat(&result32, &result);
			ret = copy_to_user(ur, &result32, sizeof(result32));
		} else
			return -EIO;
	}
	break;

	case PMEIO_SCAN_Wn32:
	{
		struct compat_pme_scan_cmds scan_cmds32;
		int i, ret = 0;

		/* Copy the command to kernel space */
		if (copy_from_user(&scan_cmds32, compat_ptr(arg),
				sizeof(scan_cmds32)))
			return -EFAULT;
		PMEPRINFO("Received Wn for %d cmds\n", scan_cmds32.num);
		for (i = 0; i < scan_cmds32.num; i++) {
			struct pme_scan_cmd scan_cmd;
			struct compat_pme_scan_cmd __user *u_scan_cmd32;
			struct compat_pme_scan_cmd scan_cmd32;

			u_scan_cmd32 = compat_ptr(scan_cmds32.cmds);
			u_scan_cmd32 += i;

			if (copy_from_user(&scan_cmd32, u_scan_cmd32,
					sizeof(scan_cmd32))) {
				pr_err("pme2_scan: Err with %d\n", i);
				scan_cmds32.num = i;
				if (copy_to_user(compat_ptr(arg), &scan_cmds32,
							sizeof(scan_cmds32)))
					return -EFAULT;
				return -EFAULT;
			}
			compat_to_scan_cmd(&scan_cmd, &scan_cmd32);
			ret = process_scan_cmd(fp, session, &scan_cmd, NULL, 0);
			if (ret) {
				pr_err("pme2_scan: Err with %d cmd %d\n",
					i, ret);
				scan_cmds32.num = i;
				if (copy_to_user(compat_ptr(arg), &scan_cmds32,
							sizeof(scan_cmds32)))
					return -EFAULT;
				return -EINTR;
			}
		}
		return ret;
	}
	break;

	case PMEIO_SCAN_Rn32:
	{
		struct compat_pme_scan_results results32;
		int i = 0, ret = 0;
		struct cmd_token *completed_cmd = NULL;
		struct compat_pme_scan_result __user *ur;

		/* Copy the command to kernel space */
		if (copy_from_user(&results32, compat_ptr(arg),
				sizeof(results32)))
			return -EFAULT;
		ur = compat_ptr(results32.results);
		PMEPRINFO("pme2_scan: Rx Rn for %d res\n", results32.num);
		if (!results32.num)
			return 0;
		do {
			completed_cmd = NULL;
			ret = 0;
			/* Check to see if any results */
			spin_lock(&session->completed_commands_lock);
			if (!list_empty(&session->completed_commands)) {
				/* Move to a different list */
				PMEPRINFO("pme2_scan: Pop response\n");
				completed_cmd = list_first_entry(
						&session->completed_commands,
						struct cmd_token,
						completed_list);
				list_del(&completed_cmd->completed_list);
				session->completed_count--;
			}
			spin_unlock(&session->completed_commands_lock);
			if (completed_cmd) {
				struct compat_pme_scan_result l_result32;
				struct pme_scan_result result;

				if (copy_from_user(&l_result32, ur+i,
							sizeof(l_result32)))
						return -EFAULT;
				compat_to_scan_result(&result, &l_result32);
				ret = process_completed_token(fp, completed_cmd,
								&result);
				scan_result_to_compat(&l_result32, &result);
				ret = copy_to_user(ur+i, &l_result32,
							sizeof(l_result32));
				if (!ret)
					i++;
			}
		} while (!ret && completed_cmd && (i != results32.num));

		if (i != results32.num) {
			PMEPRINFO("pme2_scan: Only filled %d responses\n", i);
			results32.num = i;
			PMEPRINFO("pme2_scan: results32.num = %d\n",
				results32.num);
			if (copy_to_user(compat_ptr(arg), &results32,
					sizeof(struct pme_scan_results))) {
				pr_err("Error copying to user data\n");
				return -EFAULT;
			}
		}
		return ret;
	}
	break;
#endif /* CONFIG_COMPAT */

	default:
		pr_err("UNKNOWN IOCTL cmd 0x%x\n", cmd);
		return -EINVAL;
		break;
	}

	return ret;
}

static const struct file_operations fsl_pme2_scan_fops = {
	.owner		= THIS_MODULE,
	.open		= fsl_pme2_scan_open,
	.release	= fsl_pme2_scan_close,
	.poll		= fsl_pme2_scan_poll,
	.unlocked_ioctl = fsl_pme2_scan_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl	= fsl_pme2_scan_ioctl,
#endif
};

static struct miscdevice fsl_pme2_scan_dev = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = PME_DEV_SCAN_NODE,
	.fops = &fsl_pme2_scan_fops
};

static int __init fsl_pme2_scan_init(void)
{
	int err = 0;

	pr_info("Freescale pme2 scan driver\n");
	err = misc_register(&fsl_pme2_scan_dev);
	if (err) {
		pr_err("fsl-pme2-scan: cannot register device\n");
		return err;
	}
	pr_info("fsl-pme2-scan: device %s registered\n",
		fsl_pme2_scan_dev.name);
	return 0;
}

static void __exit fsl_pme2_scan_exit(void)
{
	int err = misc_deregister(&fsl_pme2_scan_dev);
	if (err)
		pr_err("Failed to deregister device %s code %d\n",
			fsl_pme2_scan_dev.name, err);
	pr_info("device %s deregistered\n", fsl_pme2_scan_dev.name);
}

module_init(fsl_pme2_scan_init);
module_exit(fsl_pme2_scan_exit);

MODULE_AUTHOR("Jeffrey Ladouceur <jeffrey.ladouceur@freescale.com>");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION("Freescale PME2 scan driver");