summaryrefslogtreecommitdiff
path: root/drivers/stm/stm_fw.c
blob: 057963c65e7ab87e35c19113a378e5b0e476758b (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
/*
 * stm_fw.c
 *
 * System Trace Module (STM) Framework Driver Implementation
 *
 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
 *
 *
 *  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 Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "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 THE COPYRIGHT
 *  OWNER OR CONTRIBUTORS 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 <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/debugfs.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/version.h>

#include "stm_fw.h"
#include "stm_msg.h"

/*****************************************************************************
 * Local definitions and statics variables
 *****************************************************************************/
#define MAX_SIZE_FILENAME       40
#define MAX_CONFIG_STRING_BYTES 64

struct stm_chn_descriptor {
	void *chn_id;		/* Only valid if write_dentry is not NULL */
	bool chn_enable;
	struct dentry *write_dentry; /* NULL if not a user mode channel */
	struct dentry *config_dentry; /* Always set for any chan type */
	bool  write_protect;	   /* Protect writes to the stm channel */
	struct mutex lock;
	enum msg_format_t msg_format;
	enum msg_timing_t msg_timing;
	bool timestamp_enable;
	bool checksum_enable;
	bool ost_enable;
	uint32_t ost_entity;
	uint32_t ost_protocol;
	long pid;
	struct list_head chn_list_link;
	char chn_filename[MAX_SIZE_FILENAME];
	char cfg_filename[MAX_SIZE_FILENAME];
};

struct stm_device {
	struct mutex lock;
	struct stm_operations *stm_ops;  /* If NULL all elements are invalid */
	struct dentry *parent;
	struct dentry *request;
	struct list_head chn_list_head; /* List HEAD of stm_chn_descriptors */
};

static struct stm_device dev;

/*****************************************************************************
 * Support function prototypes
 *****************************************************************************/
static int8_t cal_checksum(unsigned char *buf, int cnt);

static void rm_chn_descriptor(struct stm_chn_descriptor *chn_descriptor);

static int parse_to_user(char *buf, loff_t buf_pos,
			  char __user *user_buf, size_t count, loff_t *ppos,
			  char *delims);

static int parse_strings_from_string(int num_strings, char *data[],
						    char *kbuf, char *delims);

/*****************************************************************************
 *The following file operation functions are for the STM user mode
 * debugfs message writes.
 *****************************************************************************/

/* Transport over STM user write to file. The function will act as if file is
 * simply being appended to (ppos incremented by count).
 * If count > STM_MAX_XFER_BYTES, break transfers into STM_MAX_XFER_BYTES
 * chuncks. If timestamp added, it gets added to each chunck.
 */
/* TODO: Add support for dealing with messages > STM_MAX_XFER_BYTES where
 *       the next call to stm_file_write is a continuation from the previous
 *       write. If timestamp added, it would only be sent with the last write
 *       when all data is sent. In user and vendor modes will need to add a
 *       flag to the channel descriptor indicating the next write is a
 *       continuation of the previous.
 */
static ssize_t stm_file_write(struct file *filp, const char __user *user_buf,
						    size_t count, loff_t *ppos)
{
	struct stm_chn_descriptor *chn_descriptor = filp->private_data;
	char *buf = NULL;
	char *kbuf = NULL;
	bool uspace = false;
	bool timestamp = false;
	uint msg_cnt = 0;
	int ret = 0;
	int header = 0;
	int checksum = 0;

	if (!chn_descriptor)
		return -EFAULT;

	if (!chn_descriptor->chn_enable)
		return -EPERM;

	if (chn_descriptor->write_protect)
		if (mutex_lock_interruptible(&chn_descriptor->lock))
			return -ERESTARTSYS;

	/* If channel's pid has changed export a notification message */
	if (chn_descriptor->pid != current->pid && dev.stm_ops->write_mark) {
		dev.stm_ops->write_mark(chn_descriptor->chn_id,
				    (char *)&current->pid,
				     sizeof current->pid, uspace,
				     chn_descriptor->timestamp_enable);
		chn_descriptor->pid = current->pid;
	}

	switch (chn_descriptor->msg_format) {
	case USER:
		timestamp = chn_descriptor->timestamp_enable;

		/*determine how much space is needed */
		if (chn_descriptor->checksum_enable)
			checksum = 1;

		if (chn_descriptor->ost_enable)
			header = ((count + checksum) < 256) ? 4 : 8;

		if (count + header + checksum > STM_MAX_XFER_BYTES) {
			count = STM_MAX_XFER_BYTES - (header + checksum);
			timestamp = false;
		}

		/* TODO:: If kmalloc sleeps (low memory case) then unprotected
		 *	messages could get out of order. Consider putting
		 *	in a small buffer (256 bytes) in the chn_descriptor.
		 *	In the unprotected case the user mode client is
		 *	resposnible for using different channels per thread.
		 */

		kbuf = kmalloc(count + header + checksum, GFP_KERNEL);
		if (kbuf == NULL) {
			ret = -ENOMEM;
			goto stm_file_write_exit;
		}

		if (copy_from_user(kbuf + header, user_buf, count)) {
			ret = -EFAULT;
			goto stm_file_write_exit;
		}

		if (chn_descriptor->ost_enable) {
			if (header == 4)
				*(uint32_t *)kbuf =  (uint32_t)(OST_VERSION
					| (chn_descriptor->ost_entity)
					| (chn_descriptor->ost_protocol)
					| (count + checksum));
			else {
				*(uint32_t *)kbuf =  (uint32_t)(OST_VERSION
					| (chn_descriptor->ost_entity)
					| (chn_descriptor->ost_protocol)
					| OST_SHORT_HEADER_LENGTH_LIMIT);
				*(uint32_t *)(kbuf+1) =
						  (uint32_t)(count + checksum);
			}
		}

		if (checksum)
			*(uint8_t *)(kbuf + header + count) =
					    cal_checksum(kbuf, count + header);

		buf = kbuf;
		uspace = false;
		msg_cnt = header + count + checksum;
		break;

	case VENDOR:
		buf = (char *)user_buf;
		uspace = true;
		/* Note module specific driver can treat timestamp as false*/
		timestamp = true;
		msg_cnt = count;
		break;

	case RAW:
		buf = (char *)user_buf;
		uspace = true;
		timestamp = true;
		msg_cnt = count;
		break;

	} /* End of switch */

	ret = dev.stm_ops->write(chn_descriptor->chn_id, buf,
						   msg_cnt, uspace, timestamp);

	/* Check for user mode case */
	if (msg_cnt > count)
		ret = count;

	*ppos += ret;

stm_file_write_exit:

	kfree(kbuf);

	if (chn_descriptor->write_protect)
			mutex_unlock(&chn_descriptor->lock);

	return ret;
}

/* Open STM message write file */
static int stm_file_open(struct inode *inode, struct file *filp)
{
	/* Not muct to do other than move the file descriptor
	 * pointer from the inode to the filp
	 */
	filp->private_data = inode->i_private;
	return 0;
}

static const struct file_operations user_chn_fops = {
	.write =    stm_file_write,
	.open =     stm_file_open,
};

/*****************************************************************************
 *The following file operation functions are for the STM
 * debugfs config read/writes.
 *****************************************************************************/

enum config_id {
	CFGID_CHN_ENABLE,
	CFGID_CHN_DISABLE,
	CFGID_PROTECTED,
	CFGID_UNPROTECTED,
	CFGID_FMT_USER,
	CFGID_FMT_VENDOR,
	CFGID_FMT_RAW,
	CFGID_STRING,
	CFGID_BINARY,
	CFGID_OST_ENABLE,
	CFGID_OST_DISABLE,
	CFGID_TIMESTAMP_ENABLE,
	CFGID_TIMESTAMP_DISABLE,
	CFGID_CHECKSUM_ENABLE,
	CFGID_CHECKSUM_DISABLE,
	CFGID_LAST
};


static char *config_cmds[] = {
	[CFGID_CHN_ENABLE] = "chn_enable",
	[CFGID_CHN_DISABLE] = "chn_disable",
	[CFGID_PROTECTED] = "protected",
	[CFGID_UNPROTECTED] = "unprotected",
	[CFGID_FMT_USER] = "fmt_user",
	[CFGID_FMT_VENDOR] = "fmt_vendor",
	[CFGID_FMT_RAW] = "fmt_raw",
	[CFGID_STRING] = "string",
	[CFGID_BINARY] = "binary",
	[CFGID_OST_ENABLE] = "ost_enable",
	[CFGID_OST_DISABLE] = "ost_disable",
	[CFGID_TIMESTAMP_ENABLE] = "timestamp_enable",
	[CFGID_TIMESTAMP_DISABLE] = "timestamp_disable",
	[CFGID_CHECKSUM_ENABLE] = "checksum_enable",
	[CFGID_CHECKSUM_DISABLE] = "checksum_disable"
};


/* Parse STM channel configuration string from user write to file */
static ssize_t config_file_write(struct file *file, const char __user *user_buf,
				 size_t count, loff_t *ppos)
{
	struct stm_chn_descriptor *chn_descriptor = file->private_data;
	char *kbuf = NULL, kbuffer[MAX_CONFIG_STRING_BYTES];
	enum config_id cfg_id;
	int cfg_len;
	int err = 0;
	bool vendor_pass = false;

	if (count > MAX_CONFIG_STRING_BYTES) {
		err = -EINVAL;
		goto cfg_wrt_exit1;
	}

	/* Get userspace string and assure termination */
	if (count >= sizeof(kbuffer))
		return -EINVAL;

	if (copy_from_user(kbuffer, user_buf, count))
		return -EFAULT;

	kbuffer[count] = 0;

	/*
	 * Allow "echo > request" or "echo -n '' > request"
	 * to clean the filter quietly.
	 */
	kbuf = strstrip(kbuffer);

	/* Check buf against list of supported commands (config_cmds) */
	for (cfg_id = 0; cfg_id < CFGID_LAST; cfg_id++) {
		cfg_len = strlen(config_cmds[cfg_id]);
		if (!strncmp(kbuf, config_cmds[cfg_id], cfg_len))
			break;
	}

	if (mutex_lock_interruptible(&chn_descriptor->lock)) {
		err = -ERESTARTSYS;
		goto cfg_wrt_exit1;
	}

	switch (cfg_id) {
	case CFGID_CHN_ENABLE:
		chn_descriptor->chn_enable = true;
		break;
	case CFGID_CHN_DISABLE:
		chn_descriptor->chn_enable = false;
		break;
	case CFGID_PROTECTED:
		chn_descriptor->write_protect = true;
		break;
	case CFGID_UNPROTECTED:
		chn_descriptor->write_protect = false;
		break;
	case CFGID_TIMESTAMP_ENABLE:
		chn_descriptor->timestamp_enable = true;
		break;
	case CFGID_TIMESTAMP_DISABLE:
		chn_descriptor->timestamp_enable = false;
		break;
	case CFGID_CHECKSUM_ENABLE:
		chn_descriptor->checksum_enable = true;
		break;
	case CFGID_CHECKSUM_DISABLE:
		chn_descriptor->checksum_enable = false;
		break;
	case CFGID_STRING:
		chn_descriptor->ost_entity = OST_ENTITY_ID;
		chn_descriptor->ost_protocol = OST_PROTOCOL_CHARSTREAM;
		vendor_pass = true;
		break;
	case CFGID_BINARY:
		chn_descriptor->ost_entity = OST_ENTITY_ID;
		chn_descriptor->ost_protocol = OST_PROTOCOL_BINSTREAM;
		vendor_pass = true;
		break;
	case CFGID_OST_ENABLE:
		chn_descriptor->ost_enable = true;
		break;
	case CFGID_OST_DISABLE:
		chn_descriptor->ost_enable = false;
		break;
	case CFGID_FMT_USER:
		chn_descriptor->msg_format = USER;
		vendor_pass = true;
		break;
	case CFGID_FMT_VENDOR:
		chn_descriptor->msg_format = VENDOR;
		vendor_pass = true;
		break;
	case CFGID_FMT_RAW:
		chn_descriptor->msg_format = RAW;
		vendor_pass = true;
		break;
	default:
		vendor_pass = true;
	}

	if (vendor_pass)
		err = dev.stm_ops->config_write(chn_descriptor->chn_id, kbuf);

	mutex_unlock(&chn_descriptor->lock);

cfg_wrt_exit1:
	if (err) {
		pr_err("stm_fw: invalid command ""%s"" for %s, error %d\n",
				    kbuf, chn_descriptor->cfg_filename, err);
		count = err;
	}


	return count;
}

/* Provide channel configuration data on user read from file */
static ssize_t config_file_read(struct file *file, char __user *user_buf,
				 size_t count, loff_t *ppos)
{
	struct stm_chn_descriptor *chn_descriptor = file->private_data;
	const int max_buf = 256;
	char buf[max_buf];
	int buf_cnt = 0;
	char delims[] = ",";
	loff_t buf_pos = 0;

	if (!dev.stm_ops->config_read)
		return 0;

	if (mutex_lock_interruptible(&chn_descriptor->lock))
		return -ERESTARTSYS;

	/* config_read puts formatted comma seperated strings in buf */
	buf_cnt = dev.stm_ops->config_read(chn_descriptor->chn_id,
								buf, max_buf);

	mutex_unlock(&chn_descriptor->lock);

	if (buf_cnt < 0)
		return buf_cnt;

	return parse_to_user(buf, buf_pos, user_buf, count, ppos, delims);
}

static int config_file_open(struct inode *inode, struct file *filp)
{
	filp->private_data = inode->i_private;

	return 0;
}

static const struct file_operations config_fops = {
	.write =    config_file_write,
	.read =     config_file_read,
	.open =     config_file_open
};


/*****************************************************************************
 *The following file operation functions are for the STM
 * debugfs request file read/writes.
 *****************************************************************************/

/* Get global state information on read from the request file */
static ssize_t request_file_read(struct file *file, char __user *user_buf,
				 size_t count, loff_t *ppos)
{
	const int max_buf = 256;
	char buf[max_buf];
	int buf_cnt = 0;
	int len = 0;
	loff_t buf_pos = 0;
	char delims[] = ",";
	struct stm_global_info info;

	dev.stm_ops->get_global_info(&info);

	/*Get the fixed info we know we have room for */
	buf_cnt = snprintf(buf, max_buf,
			    "Total Channel Count %d\n",
			    info.total_chn_cnt);
	buf_cnt += snprintf(buf+buf_cnt, max_buf - buf_cnt,
			    "Reserved Channel Count %d\n",
			    info.reserved_chn_cnt);
	buf_cnt += snprintf(buf+buf_cnt, max_buf - buf_cnt,
			    "User Allocated Channels %d\n",
			    info.user_allocated_chn_cnt);
	buf_cnt += snprintf(buf+buf_cnt, max_buf - buf_cnt,
			    "STM Module Clock Enable %d\n",
			    info.stm_module_clock_on);
	buf_cnt += snprintf(buf+buf_cnt, max_buf - buf_cnt,
			    "STM Module Enable %d\n",
			    info.stm_module_enabled);

	if (buf_cnt > max_buf)
		return -ENOBUFS;

	/* Read buf_cnt bytes from kernel buf to user_buf. If successful ppos
	    * advanced by number of bytes read from kernel buf.
	    */
	if (buf_cnt <= count && buf_pos == *ppos) {
		len = simple_read_from_buffer(user_buf, count, ppos,
							buf, buf_cnt);
		if (len < 0)
			return len;

		count -= buf_cnt;
		user_buf += buf_cnt;
		buf_pos = buf_cnt;
	}

	if (info.attribute_list)
		return parse_to_user(info.attribute_list, buf_pos,
					      user_buf, count, ppos, delims);

	return 0; /* EOF */

}

/* Request file write commands */
static char *usecase_cmds[] = {
	[USER_CHANNEL_REQUEST] = "user",
	[PRINTK_CHANNEL_REQUEST] = "printk",
	[TRACEPOINT_CHANEL_REQUEST] = "tracepoint_ftrace",
	[LTTNG_CHANNEL_REQUEST] = "lttng",
	[REMOVE_CHANNEL_REQUEST] = "rm"
};

/* Request the creation of a channel file set (name & name_config) or execution
 * of a global command (effects all channels).
 */
static ssize_t request_file_write(struct file *filp,
				  const char __user *user_buf,
				  size_t count, loff_t *f_pos)
{
	int err = 0;
	char *err_msg = "stm_fw: error creating <debugfs> file ";
	char *kbuf = NULL, kbuffer[MAX_CONFIG_STRING_BYTES];
	enum stm_usecase uc_id;
	char filename[MAX_SIZE_FILENAME];
	struct stm_chn_descriptor *chn_descriptor = NULL;
	char *username = NULL;
	int len;
	struct list_head *ptr;
	bool name_hit = false;

	/* Get userspace string and assure termination */
	if (count >= sizeof(kbuffer))
		return -EINVAL;

	if (copy_from_user(kbuffer, user_buf, count))
		return -EFAULT;

	kbuffer[count] = 0;

	/*
	 * Allow "echo > request" or "echo -n '' > request"
	 * to clean the filter quietly.
	 */

	kbuf = strstrip(kbuffer);

	/* Check buf against the list of supported commands */
	for (uc_id = 0; uc_id < LAST_REQUEST; uc_id++)
		if (!strncmp(kbuf, usecase_cmds[uc_id],
					strlen(usecase_cmds[uc_id])))
			break;

	if (uc_id < LAST_REQUEST) {
		/*
		 * Parse out the filename. For all cases currently supported.
		 * If case not support next switch takes default to call
		 * vendor drvier's write config function.
		 */
		char *delims = " ";
		err = parse_strings_from_string(1, &username, kbuf, delims);
		if (err < 0)
			goto err_exit;
	}

	if (mutex_lock_interruptible(&dev.lock))
		return -ERESTARTSYS;

	switch (uc_id) {
	case USER_CHANNEL_REQUEST:
	case PRINTK_CHANNEL_REQUEST:
	case TRACEPOINT_CHANEL_REQUEST:
	case LTTNG_CHANNEL_REQUEST:

		/*
		 * For these cases try to allocate a STM channel and create
		 * write file (for user case only) and config file.
		 */


		/*
		 * Search other channel descriptors to confirm
		 * filename is unique.
		 */
		list_for_each(ptr, &dev.chn_list_head) {
			chn_descriptor = list_entry(ptr,
						    struct stm_chn_descriptor,
						    chn_list_link);
			if (!strcmp(chn_descriptor->chn_filename, username)) {
				err = -EINVAL;
				goto err_exit;
			}
		}

		/* Malloc the channel descriptor */
		chn_descriptor = kmalloc(sizeof *chn_descriptor, GFP_KERNEL);
		if (!chn_descriptor) {
			err = -ENOMEM;
			goto err_exit;
		}

		/* Initialize chn descriptor */
		chn_descriptor->write_dentry = NULL;
		chn_descriptor->config_dentry = NULL;
		chn_descriptor->chn_enable = true;
		chn_descriptor->write_protect = true;

		mutex_init(&chn_descriptor->lock);
		chn_descriptor->msg_format = VENDOR;
		chn_descriptor->msg_timing = GUARANTEED;
		chn_descriptor->ost_protocol = OST_PROTOCOL_CHARSTREAM;
		chn_descriptor->ost_entity = OST_ENTITY_ID;
		chn_descriptor->timestamp_enable = true;
		chn_descriptor->checksum_enable = false;
		/* set to invlaid id so first write will export pid message */
		chn_descriptor->pid = 0;

		/* Allocate a channel */
		err = dev.stm_ops->allocate_channel(uc_id,
						    &chn_descriptor->chn_id);
		if (err < 0) {
			chn_descriptor->chn_id = NULL;
			goto alloc_err_exit;
		}

		/* If user command create debugfs message write file */
		if (uc_id == USER_CHANNEL_REQUEST) {

			len = strlen(username);
			if (len > MAX_SIZE_FILENAME || !len) {
				err = -EINVAL;
				goto alloc_err_exit;
			}

			strcpy(chn_descriptor->chn_filename, username);

			chn_descriptor->write_dentry = debugfs_create_file(
					username, 0666, dev.parent,
					(void *)chn_descriptor,
					&user_chn_fops);

			if (!chn_descriptor->write_dentry) {
				pr_err("%s %s\n", err_msg, username);
				err = -ENODEV;
				goto alloc_err_exit;

			}
		}

		/* For all other channels create debugfs configuration file */

		len = snprintf(filename, MAX_SIZE_FILENAME, "%s_config",
								    username);
		if (len > MAX_SIZE_FILENAME) {
			err = -ENAMETOOLONG;
			goto alloc_err_exit;
		}

		strcpy(chn_descriptor->cfg_filename, filename);

		chn_descriptor->config_dentry = debugfs_create_file(filename,
				    0666, dev.parent, (void *)chn_descriptor,
					    &config_fops);

		if (!chn_descriptor->config_dentry) {
			pr_err("%s %s\n", err_msg, filename);
			err = -ENODEV;
			goto alloc_err_exit;
		}

		list_add_tail(&chn_descriptor->chn_list_link,
							&dev.chn_list_head);

		break;

	case REMOVE_CHANNEL_REQUEST:

		len = snprintf(filename, MAX_SIZE_FILENAME,
						"%s_config", username);
		if (len > MAX_SIZE_FILENAME || !len) {
			err = -EINVAL;
			goto err_exit;
		}

		list_for_each(ptr, &dev.chn_list_head) {
			chn_descriptor = list_entry(ptr,
						    struct stm_chn_descriptor,
						    chn_list_link);
			if (!strcmp(chn_descriptor->chn_filename, username) ||
			    !strcmp(chn_descriptor->cfg_filename, filename)) {
				name_hit = true;
				break;
			}
		 }

		if (!name_hit) {
			err = -EINVAL;
			goto err_exit;
		}

		rm_chn_descriptor(chn_descriptor);

		list_del(&chn_descriptor->chn_list_link);

		kfree(chn_descriptor);

		break;

	default:
		err = dev.stm_ops->config_write((void *)-1, kbuf);
		if (err < 0) {
			err = -EINVAL;
			goto err_exit;
		}

	} /* End of the switch */

	*f_pos += count;
	mutex_unlock(&dev.lock);
	return count;

alloc_err_exit:
	rm_chn_descriptor(chn_descriptor);
	kfree(chn_descriptor);

err_exit:
	mutex_unlock(&dev.lock);
	return err;
}

static int request_file_mmap(struct file *file, struct vm_area_struct *vma)
{
	if (!dev.stm_ops->mmap)
		return -EACCES;

	return dev.stm_ops->mmap(vma);

}

static int request_file_open(struct inode *inode, struct file *file)
{
	/*
	 * Since we only allow one STM module to be open at any time,
	 * nothing really to do here.
	 */
	return 0;
}

static const struct file_operations request_fop = {
	.write = request_file_write,
	.read =	request_file_read,
	.mmap = request_file_mmap,
	.open =	 request_file_open,
};


/* Register module specific operations with Framework Drvier */
int stm_register(struct stm_operations *module_ops)
{
	int ret = 0;
	char *err_msg = "stm_fw: error creating <debugfs>/stm ";

	/*
	 * if module_ops NULL or mandatory ops not provided do not bother to
	 * create stm dir or request file
	 */
	if (module_ops == NULL ||
	    module_ops->get_global_info == NULL ||
	    module_ops->write == NULL ||
	    module_ops->allocate_channel == NULL ||
	    module_ops->free_channel == NULL ||
	    module_ops->config_write == NULL)
		ret = -EINVAL;

	if (mutex_lock_interruptible(&dev.lock))
		return -ERESTARTSYS;

	/* Check if already registered */
	if (dev.stm_ops) {
		ret = -EPERM;
		goto stm_register_err;
	}

	dev.parent = debugfs_create_dir("stm", NULL);
	if (((int)dev.parent == -ENODEV) || (dev.parent == NULL)) {

		if ((int)dev.parent == -ENODEV)
			pr_err("%s - debugfs not mounted\n", err_msg);
		if (dev.parent == NULL)
			pr_err("%s - error unknown\n", err_msg);
		ret = -ENODEV;
		goto stm_register_err;
	}

	dev.request = debugfs_create_file("request", 0666, dev.parent, NULL,
								 &request_fop);
	if (dev.request == NULL) {
		pr_err("%s/request\n", err_msg);
		debugfs_remove(dev.parent);
		ret = -ENODEV;
		goto stm_register_err;
	}

	pr_info("stm_fw: vendor driver %s registered\n", module_ops->name);

	dev.stm_ops = module_ops;
	INIT_LIST_HEAD(&dev.chn_list_head);
stm_register_err:
	mutex_unlock(&dev.lock);
	return ret;

}
EXPORT_SYMBOL(stm_register);

int stm_unregister()
{
	struct stm_chn_descriptor *chn_descriptor;
	struct list_head *ptr;

	if (mutex_lock_interruptible(&dev.lock))
		return -ERESTARTSYS;

	list_for_each(ptr, &dev.chn_list_head) {
		chn_descriptor = list_entry(ptr, struct stm_chn_descriptor,
								chn_list_link);

		if (mutex_lock_interruptible(&chn_descriptor->lock)) {
			mutex_unlock(&dev.lock);
			return -ERESTARTSYS;
		}

		if (chn_descriptor->write_dentry)
			debugfs_remove(chn_descriptor->write_dentry);

		if (chn_descriptor->config_dentry)
			debugfs_remove(chn_descriptor->config_dentry);

		mutex_unlock(&chn_descriptor->lock);

		kfree(chn_descriptor);

	}

	debugfs_remove_recursive(dev.parent);

	pr_info("stm_fw: vendor driver %s removed\n", dev.stm_ops->name);

	dev.stm_ops = NULL;

	mutex_unlock(&dev.lock);

	return 0;

}
EXPORT_SYMBOL(stm_unregister);

static int stm_fw_init(void)
{
	dev.stm_ops = NULL;
	dev.parent = NULL;
	dev.request = NULL;
	mutex_init(&dev.lock);

	return 0;
}

static void stm_fw_exit(void)
{
	pr_info("stm_fw: driver exit\n");
}

module_init(stm_fw_init);
module_exit(stm_fw_exit);
MODULE_LICENSE("Dual BSD/GPL");

/*****************************************************************************/
/* Support functions							 */
/*****************************************************************************/
static int8_t cal_checksum(unsigned char *buf, int cnt)
{
	int i;
	uint8_t checksum = 0;
	for (i = 0; i < cnt; i++)
		checksum += buf[i];

	return -checksum;
}

static void rm_chn_descriptor(struct stm_chn_descriptor *chn_descriptor)
{
	if (!chn_descriptor)
		return;

	if (chn_descriptor->write_dentry)
		debugfs_remove(chn_descriptor->write_dentry);

	if (chn_descriptor->config_dentry)
		debugfs_remove(chn_descriptor->config_dentry);

	if (chn_descriptor->chn_id)
		dev.stm_ops->free_channel(chn_descriptor->chn_id);

}


/* Parse a comma seperated list of properties/attributes (items) from a
 * string and copy to user_buf.
 */
static int parse_to_user(char *buf, loff_t buf_pos,
			 char __user *user_buf, size_t count, loff_t *ppos,
			 char *delims)
{

	char *pbuf = buf;
	char **next = &pbuf;
	char *item = NULL;
	int item_len = 0;
	/*
	 * If this is not the first write back to
	 * the user buffer buf_pos will be non-zero
	 */
	int total_len = buf_pos;
	int copy_len;

	item = strsep(next, delims);
	do {
		item_len = strlen(item);
		if (item_len <= count && buf_pos == *ppos) {
			loff_t copy_pos = 0;
			/* copy item to user buf */
			copy_len = simple_read_from_buffer(user_buf, count,
						 &copy_pos, item, item_len);

			if (copy_len < 0)
				return copy_len;

			if (copy_len > 0) {
				total_len += copy_len;
				count -= item_len;
				*ppos = total_len;
				user_buf += item_len;
			}

		}
		buf_pos += item_len;
		item = strsep(next, delims);
	} while (item);

	if (!total_len && !item)
		return 0; /* Return EOF */

	return total_len;
}

/* Note, this function assumes first string is a command (which has already
 * been evaluated) is to be thrown away.
 */
static int parse_strings_from_string(int num_strings, char *data[],
						    char *kbuf, char *delims)
{
	/* Parse the set header command */
	int i;
	char **item = &kbuf;
	/* Get command - throw it away */
	char *param = strsep(item, delims);

	/* Get next two parameter values */
	for (i = 0; i < num_strings; i++) {
		param = strsep(item, delims);
		if (!param)
			return -EINVAL;

		data[i] = param;
	}

	return 0;
}