forked from clearlinux-pkgs/linux-iot-lts2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0013-Integration-of-CBC-line-discipline-kernel-modul.patch
3091 lines (3081 loc) · 85.7 KB
/
0013-Integration-of-CBC-line-discipline-kernel-modul.patch
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
From 008df4900c3f6f6f4a8380bcd0a4ebf7d083d462 Mon Sep 17 00:00:00 2001
From: "G Jaya Kumaran, Vineetha" <[email protected]>
Date: Thu, 24 May 2018 18:45:28 +0800
Subject: [PATCH 1/3] Integration of CBC line discipline kernel module
This patch enables the Carrier Board Communication (CBC)
kernel module, which is implemented as a Linux kernel line
discipline attached to a UART device. The CBC protocol
defines a number of different channels which are provided
as Linux character device nodes.
Change-Id: Ic2a72229df9e9a80e306f2f44da37b1ac8539af3
Signed-off-by: G Jaya Kumaran, Vineetha
---
drivers/tty/Kconfig | 3 +
drivers/tty/Makefile | 1 +
drivers/tty/cbc/Kconfig | 22 +
drivers/tty/cbc/Makefile | 14 +
drivers/tty/cbc/cbc_core.c | 307 +++++++++
drivers/tty/cbc/cbc_core.h | 44 ++
drivers/tty/cbc/cbc_core_public.h | 28 +
drivers/tty/cbc/cbc_device.c | 66 ++
drivers/tty/cbc/cbc_device.h | 107 ++++
drivers/tty/cbc/cbc_device_manager.c | 879 ++++++++++++++++++++++++++
drivers/tty/cbc/cbc_device_manager.h | 66 ++
drivers/tty/cbc/cbc_link_checksum.c | 59 ++
drivers/tty/cbc/cbc_link_checksum.h | 44 ++
drivers/tty/cbc/cbc_link_layer.c | 484 ++++++++++++++
drivers/tty/cbc/cbc_link_layer.h | 38 ++
drivers/tty/cbc/cbc_memory.c | 153 +++++
drivers/tty/cbc/cbc_memory.h | 146 +++++
drivers/tty/cbc/cbc_mux_multiplexer.c | 145 +++++
drivers/tty/cbc/cbc_mux_multiplexer.h | 62 ++
drivers/tty/cbc/cbc_types.h | 137 ++++
include/cbc/cbc-core.h | 24 +
include/uapi/linux/cbc/cbc-core.h | 24 +
include/uapi/linux/major.h | 2 +
include/uapi/linux/tty.h | 5 +-
24 files changed, 2858 insertions(+), 2 deletions(-)
create mode 100644 drivers/tty/cbc/Kconfig
create mode 100644 drivers/tty/cbc/Makefile
create mode 100644 drivers/tty/cbc/cbc_core.c
create mode 100644 drivers/tty/cbc/cbc_core.h
create mode 100644 drivers/tty/cbc/cbc_core_public.h
create mode 100644 drivers/tty/cbc/cbc_device.c
create mode 100644 drivers/tty/cbc/cbc_device.h
create mode 100644 drivers/tty/cbc/cbc_device_manager.c
create mode 100644 drivers/tty/cbc/cbc_device_manager.h
create mode 100644 drivers/tty/cbc/cbc_link_checksum.c
create mode 100644 drivers/tty/cbc/cbc_link_checksum.h
create mode 100644 drivers/tty/cbc/cbc_link_layer.c
create mode 100644 drivers/tty/cbc/cbc_link_layer.h
create mode 100644 drivers/tty/cbc/cbc_memory.c
create mode 100644 drivers/tty/cbc/cbc_memory.h
create mode 100644 drivers/tty/cbc/cbc_mux_multiplexer.c
create mode 100644 drivers/tty/cbc/cbc_mux_multiplexer.h
create mode 100644 drivers/tty/cbc/cbc_types.h
create mode 100644 include/cbc/cbc-core.h
create mode 100644 include/uapi/linux/cbc/cbc-core.h
diff --git a/drivers/tty/Kconfig b/drivers/tty/Kconfig
index 0840d27381ea..7210714592ac 100644
--- a/drivers/tty/Kconfig
+++ b/drivers/tty/Kconfig
@@ -441,4 +441,7 @@ config VCC
depends on SUN_LDOMS
help
Support for Sun logical domain consoles.
+
+source "drivers/tty/cbc/Kconfig"
+
endif # TTY
diff --git a/drivers/tty/Makefile b/drivers/tty/Makefile
index c72cafdf32b4..4ae6744406d4 100644
--- a/drivers/tty/Makefile
+++ b/drivers/tty/Makefile
@@ -33,5 +33,6 @@ obj-$(CONFIG_PPC_EPAPR_HV_BYTECHAN) += ehv_bytechan.o
obj-$(CONFIG_GOLDFISH_TTY) += goldfish.o
obj-$(CONFIG_MIPS_EJTAG_FDC_TTY) += mips_ejtag_fdc.o
obj-$(CONFIG_VCC) += vcc.o
+obj-$(CONFIG_CBC_LDISC) += cbc/
obj-y += ipwireless/
diff --git a/drivers/tty/cbc/Kconfig b/drivers/tty/cbc/Kconfig
new file mode 100644
index 000000000000..c9cde7fa43ca
--- /dev/null
+++ b/drivers/tty/cbc/Kconfig
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# CBC (CarrierBoardCommunication) subsystem configuration
+#
+
+config CBC_LDISC
+ tristate "CBC (Carrier Board Communication) line discipline"
+ depends on TTY
+ help
+ The CBC driver implements a line discipline supporting
+ the proprietary CBC (Carrier Board Communication) protocol.
+
+ The CBC protocol is a serial line protocol with multiplexing
+ intended to be used in automotive IVI platforms for multi-
+ plexed communication between a vehicle IOC and CPU. It is
+ designed to transport small data packets (up to 64 bytes)
+ and features a transport protocol to transport larger data
+ chunks over a point to point connection.
+
+ When initialised the driver presents a number of channels as
+ character devices.
+
diff --git a/drivers/tty/cbc/Makefile b/drivers/tty/cbc/Makefile
new file mode 100644
index 000000000000..c0517817f605
--- /dev/null
+++ b/drivers/tty/cbc/Makefile
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0
+
+
+cbc-ldisc-y := cbc_core.o
+cbc-ldisc-y += cbc_device.o
+cbc-ldisc-y += cbc_device_manager.o
+cbc-ldisc-y += cbc_link_checksum.o
+cbc-ldisc-y += cbc_link_layer.o
+cbc-ldisc-y += cbc_memory.o
+cbc-ldisc-y += cbc_mux_multiplexer.o
+
+obj-$(CONFIG_CBC_LDISC) += cbc-ldisc.o
+
+ccflags-y := -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -fstack-protector
\ No newline at end of file
diff --git a/drivers/tty/cbc/cbc_core.c b/drivers/tty/cbc/cbc_core.c
new file mode 100644
index 000000000000..d9d15fa224ae
--- /dev/null
+++ b/drivers/tty/cbc/cbc_core.c
@@ -0,0 +1,307 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * CBC line discipline kernel module.
+ * Handles Carrier Board Communications (CBC) protocol.
+ *
+ * Copyright (C) 2018 Intel Corporation
+ *
+ * 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.
+ *
+ */
+
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/fs.h>
+#include <linux/kernel.h>
+#include <linux/major.h>
+#include <linux/module.h>
+#include <linux/sched.h>
+#include <linux/tty.h>
+#include <linux/uaccess.h>
+
+#include "cbc_core.h"
+#include "cbc_types.h"
+#include "cbc_device_manager.h"
+#include "cbc_link_layer.h"
+#include "cbc_mux_multiplexer.h"
+
+MODULE_AUTHOR("Gerhard Bocksch. [email protected]");
+MODULE_DESCRIPTION("CBC serial protocol demultiplexer");
+MODULE_LICENSE("GPL");
+
+static int granularity = 4;
+module_param(granularity, int, 0644);
+MODULE_PARM_DESC(granularity,
+ "The granularity of the CBC messages. default: 4");
+
+MODULE_ALIAS_LDISC(N_CBCCORE);
+
+/* Marker to mark the cbc_core as valid. */
+#define CBC_MAGIC 0xAFFEAFFE
+
+static struct cbc_struct cbc_core = { .magic = CBC_MAGIC };
+
+/*
+ * cbc_core_configure_channel - Set the priority and receive
+ * callback for a CBC channel.
+ * @channel_idx: Channel identifier (see cbc_channel_enumeration)
+ * @priority: Priority for channel.
+ * @data: Data associated with channel.
+ * @receive: Receive callback associated with channel.
+ */
+void cbc_core_configure_channel(u32 const channel_idx, const u8 priority,
+ void *data,
+ void (*receive)(void *data, const u16 length,
+ const u8 * const buffer))
+{
+ cbc_mux_configure_data_channel(channel_idx, priority, data, receive);
+}
+EXPORT_SYMBOL_GPL(cbc_core_configure_channel);
+
+/*
+ * cbc_core_send_data - Send to a CBC channel.
+ * @channel_idx: Channel identifier (see cbc_channel_enumeration)
+ * @priority: Priority for channel.
+ * @data: Data associated with channel.
+ * @receive: Receive function associated with channel.
+ */
+void cbc_core_send_data(const u32 channel_idx, const u16 length,
+ const u8 * const buffer)
+{
+ cbc_manager_transmit_data(channel_idx, length, buffer);
+}
+EXPORT_SYMBOL_GPL(cbc_core_send_data);
+
+/*
+ * cbc_ldisc_open - Open the CBC connection to the IOC.
+ * @tty: Handle to tty.
+ *
+ * This function is called by the tty module when the
+ * line discipline is requested. It allocates the memory pool and creates the
+ * CBC devices.
+ * Called in process context serialized from other ldisc calls.
+ *
+ * Return: 0 on success error otherwise.
+ */
+static int cbc_ldisc_open(struct tty_struct *tty)
+{
+ struct cbc_struct *cbc;
+ int err;
+
+ pr_debug("cbc-ldisc open.\n");
+ mutex_lock(&cbc_core.ldisc_mutex);
+
+ if (WARN_ON(tty->ops->write == NULL)) {
+ pr_err("cbc-ldisc open write not supported.\n");
+ err = -EOPNOTSUPP;
+ goto err_exit;
+ }
+
+ cbc = tty->disc_data;
+
+ err = -EEXIST;
+ /* First make sure we're not already connected. */
+ if (WARN_ON(cbc && cbc->magic == CBC_MAGIC)) {
+ pr_err(
+ "cbc-ldisc CBC line discipline already open or CBC magic wrong.\n");
+ goto err_exit;
+ }
+
+ cbc = &cbc_core;
+
+ cbc->tty = tty;
+ tty->disc_data = cbc;
+ tty->receive_room = 65536;
+
+ /* Create class cbc-core. This will appear as /sys/class/cbc* */
+ cbc->cbc_class = class_create(THIS_MODULE, "cbc");
+
+ if (WARN_ON(!cbc->cbc_class)) {
+ pr_err("cbc-ldisc open could not create cbc class.\n");
+ goto err_exit;
+ }
+
+ /* Create memory pool */
+ cbc->memory_pool = cbc_memory_pool_create(
+ CBC_QUEUE_LENGTH * CBC_CHANNEL_MAX_NUMBER);
+ if (WARN_ON(!cbc->memory_pool)) {
+ pr_err("failed to create memory pool.\n");
+ goto err_exit;
+ }
+
+ /* Initialise on every open, so we start with sequence-counters at 0.*/
+ cbc_link_layer_init(cbc->memory_pool);
+
+ /* Register devices here. */
+ err = cbc_register_devices(cbc->cbc_class, cbc->memory_pool);
+ if (err != 0) {
+ pr_err("register devices failed\n");
+ goto err_exit;
+ }
+
+ cbc_link_layer_set_frame_granularity(granularity);
+
+ /* tty layer expects 0 on success */
+ mutex_unlock(&cbc_core.ldisc_mutex);
+ return 0;
+
+err_exit: mutex_unlock(&cbc_core.ldisc_mutex);
+ return err;
+}
+
+/*
+ * cbc_ldisc_close - Close down the CBC communication.
+ * @tty: Handle to tty
+ *
+ * Unregister CBC devices and destroy CBC class.
+ */
+static void cbc_ldisc_close(struct tty_struct *tty)
+{
+ struct cbc_struct *cbc;
+
+ mutex_lock(&cbc_core.ldisc_mutex);
+
+ cbc = (struct cbc_struct *) tty->disc_data;
+ pr_debug("cbc-ldisc close\n");
+
+ if (cbc && cbc->magic == CBC_MAGIC && cbc->tty == tty) {
+ cbc_unregister_devices(cbc->cbc_class);
+ class_destroy(cbc->cbc_class);
+ if (WARN_ON(!cbc_memory_pool_try_free(cbc->memory_pool)))
+ pr_err("could not free memory pool.\n");
+ tty->disc_data = NULL;
+ cbc->tty = NULL;
+ } else {
+ WARN_ON(1);
+ pr_err("ldisc close with wrong CBC magic.\n");
+ }
+
+ mutex_unlock(&cbc_core.ldisc_mutex);
+}
+
+/*
+ * Close line discipline on ldisc hangup.
+ * @tty: Handle to tty
+ */
+static int cbc_ldisc_hangup(struct tty_struct *tty)
+{
+ cbc_ldisc_close(tty);
+ return 0;
+}
+
+/*
+ * cbc_ldisc_receive_buf - .receive call for a line discipline.
+ * @tty: Handle to tty
+ * @cp: Received data buffer.
+ * @fp: Not used
+ * @count: Amount of data received.
+ *
+ * Attempts to read a single CBC frame at a time. Cycles round
+ * until all data has been processed.
+ */
+static void cbc_ldisc_receive_buf(struct tty_struct *tty,
+ const unsigned char *cp, char *fp, int count)
+{
+ struct cbc_struct *cbc;
+ u8 accepted_bytes; /* per cbc_serial_receive call */
+ unsigned int accepted_bytes_sum = 0;
+
+ mutex_lock(&cbc_core.ldisc_mutex);
+ cbc = (struct cbc_struct *) tty->disc_data;
+
+ if (cbc && (cbc->magic == CBC_MAGIC) && (cbc->tty == tty)) {
+ do {
+ u8 chunksize = 254;
+
+ if ((count - accepted_bytes_sum) < 255)
+ chunksize = count - accepted_bytes_sum;
+ accepted_bytes = cbc_core_on_receive_cbc_serial_data(
+ chunksize, cp + accepted_bytes_sum);
+ accepted_bytes_sum += accepted_bytes;
+ cbc_link_layer_rx_handler();
+ } while (accepted_bytes_sum != count);
+ }
+ mutex_unlock(&cbc_core.ldisc_mutex);
+}
+
+/*
+ * Called from ldisc when write is possible. Not used.
+ */
+static void cbc_ldisc_write_wakeup(struct tty_struct *tty)
+{
+ (void) tty;
+}
+
+/* Called to send messages from channel specific device to the IOC */
+enum cbc_error target_specific_send_cbc_uart_data(u16 length,
+ const u8 *raw_buffer)
+{
+ mutex_lock(&cbc_core.ldisc_mutex);
+ if (cbc_core.tty) {
+ set_bit(TTY_DO_WRITE_WAKEUP, &cbc_core.tty->flags);
+
+ cbc_core.tty->ops->write(cbc_core.tty, raw_buffer, length);
+ }
+ mutex_unlock(&cbc_core.ldisc_mutex);
+
+ return CBC_OK;
+}
+
+static struct tty_ldisc_ops cbc_ldisc = {
+ .owner = THIS_MODULE,
+ .name = "cbc-ldisc",
+ .magic = TTY_LDISC_MAGIC,
+ .open = cbc_ldisc_open,
+ .close = cbc_ldisc_close,
+ .hangup = cbc_ldisc_hangup,
+ .receive_buf = cbc_ldisc_receive_buf,
+ .write_wakeup = cbc_ldisc_write_wakeup
+};
+
+/*
+ * cbc_init - Module init call.
+ * Registers this module as TTY line discipline.
+ */
+static int __init cbc_init(void)
+{
+ int status;
+
+ pr_debug("cbc-ldisc init\n");
+ mutex_init(&cbc_core.ldisc_mutex);
+ mutex_lock(&cbc_core.ldisc_mutex);
+
+ cbc_kmod_devices_init();
+
+ /* Fill in line discipline, and register it */
+ status = tty_register_ldisc(N_CBCCORE, &cbc_ldisc);
+ if (status)
+ pr_err("cbc-ldisc: can't register line discipline\n");
+
+ mutex_unlock(&cbc_core.ldisc_mutex);
+ return 0;
+}
+
+/*
+ * cbc_exit - Module exit call.
+ *
+ * De-registers itself as line discipline.
+ */
+static void __exit cbc_exit(void)
+{
+ int i;
+
+ mutex_lock(&cbc_core.ldisc_mutex);
+
+ pr_debug("cbc-ldisc Exit\n");
+
+ i = tty_unregister_ldisc(N_CBCCORE);
+ if (i)
+ pr_err("cbc-ldisc: can't unregister ldisc (err %d).\n", i);
+
+ mutex_unlock(&cbc_core.ldisc_mutex);
+}
+
+module_init(cbc_init);
+module_exit(cbc_exit);
diff --git a/drivers/tty/cbc/cbc_core.h b/drivers/tty/cbc/cbc_core.h
new file mode 100644
index 000000000000..71e07a3f7765
--- /dev/null
+++ b/drivers/tty/cbc/cbc_core.h
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * CBC line discipline kernel module.
+ * Handles Carrier Board Communications (CBC) protocol.
+ *
+ * Copyright (C) 2018 Intel Corporation
+ *
+ * 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.
+ *
+ */
+
+#ifndef CBC_CORE_MOD_H_
+#define CBC_CORE_MOD_H_
+
+#include <linux/cbc/cbc-core.h>
+#include <linux/cdev.h>
+#include <linux/ioctl.h>
+#include <linux/mutex.h>
+#include <linux/tty.h>
+
+#include "cbc_memory.h"
+
+#define CBC_IOCTL_MAGIC 0xf4
+
+/*
+ * struct cbc_struct -
+ *
+ * @magic: Marker to mark the cbc_core as valid.
+ * @tty: tty associated with the CBC driver.
+ * @class: CBC device class
+ * @memory_pool: Memory pool of CBC buffer allocated for used by CBC driver.
+ * @ldisc_mutex: Mutex to avoid unloading while accessing the driver
+ */
+struct cbc_struct {
+ int magic;
+ struct tty_struct *tty;
+ struct class *cbc_class;
+ struct cbc_memory_pool *memory_pool;
+ struct mutex ldisc_mutex;
+};
+
+#endif /* CBC_CORE_MOD_H_ */
diff --git a/drivers/tty/cbc/cbc_core_public.h b/drivers/tty/cbc/cbc_core_public.h
new file mode 100644
index 000000000000..85928f6efc68
--- /dev/null
+++ b/drivers/tty/cbc/cbc_core_public.h
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * CBC line discipline kernel module.
+ * Handles Carrier Board Communications (CBC) protocol.
+ *
+ * Copyright (C) 2018 Intel Corporation
+ *
+ * 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.
+ *
+ */
+
+#ifndef CBC_CORE_PUBLIC_H
+#define CBC_CORE_PUBLIC_H
+
+#include "cbc_types.h"
+
+/* CBC version. */
+#define CBC_VERSION_ID 1
+
+/* The following function needs to be implemented on CM/IOC. */
+enum cbc_error target_specific_send_cbc_uart_data(u16 length,
+ const u8 *raw_buffer);
+
+
+#endif /* CBC_CORE_PUBLIC_H */
+
diff --git a/drivers/tty/cbc/cbc_device.c b/drivers/tty/cbc/cbc_device.c
new file mode 100644
index 000000000000..1933a8527015
--- /dev/null
+++ b/drivers/tty/cbc/cbc_device.c
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * CBC line discipline kernel module.
+ * Handles Carrier Board Communications (CBC) protocol.
+ *
+ * Copyright (C) 2018 Intel Corporation
+ *
+ * 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.
+ *
+ */
+
+#include <linux/sched.h>
+
+#include "cbc_device.h"
+
+
+void cbc_device_init(struct cbc_device_data *cd)
+{
+ if (cd)
+ INIT_LIST_HEAD(&cd->open_files_head);
+}
+
+void cbc_file_init(struct cbc_file_data *file)
+{
+ if (file) {
+ cbc_buffer_queue_init(&file->queue);
+ init_waitqueue_head(&file->wq_read);
+ INIT_LIST_HEAD(&file->list);
+ }
+}
+
+void cbc_file_enqueue(struct cbc_file_data *fd, struct cbc_buffer *buffer)
+{
+ if (fd) {
+ if (cbc_buffer_queue_enqueue(&fd->queue, buffer)) {
+ cbc_buffer_increment_ref(buffer);
+ wake_up_interruptible(&fd->wq_read);
+ }
+ }
+}
+
+struct cbc_buffer *cbc_file_dequeue(struct cbc_file_data *fd)
+{
+ struct cbc_buffer *buffer = NULL;
+
+ if (fd)
+ buffer = cbc_buffer_queue_dequeue(&fd->queue);
+
+ if (buffer && atomic_read(&buffer->refcount) == 0) {
+ buffer = NULL;
+ pr_err("cbc-core: De-queueing an already freed buffer\n");
+ }
+
+ return buffer;
+}
+
+int cbc_file_queue_empty(struct cbc_file_data *fd)
+{
+ if (fd)
+ return (fd->queue.write == fd->queue.read);
+
+ return 1;
+}
+
diff --git a/drivers/tty/cbc/cbc_device.h b/drivers/tty/cbc/cbc_device.h
new file mode 100644
index 000000000000..deb0cd922316
--- /dev/null
+++ b/drivers/tty/cbc/cbc_device.h
@@ -0,0 +1,107 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * CBC line discipline kernel module.
+ * Handles Carrier Board Communications (CBC) protocol.
+ *
+ * Copyright (C) 2018 Intel Corporation
+ *
+ * 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.
+ *
+ */
+
+#ifndef CBC_DEVICE_H_
+#define CBC_DEVICE_H_
+
+#include <linux/cdev.h>
+#include <linux/circ_buf.h>
+#include <linux/device.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/list.h>
+#include <linux/module.h>
+#include <linux/wait.h>
+
+
+#include "cbc_types.h"
+#include "cbc_memory.h"
+
+enum cbc_device_type {
+ CBC_DEVICE_TYPE_DEFAULT,
+ CBC_DEVICE_TYPE_RAW,
+ CBC_DEVICE_TYPE_HIDDEN,
+ CBC_DEVICE_TYPE_DEBUG
+};
+
+/*
+ * struct cbc_device_data -Data for a single channel e.g. /dev/cbc-pmt.
+ * @device_name: Device name.
+ * @device_type: CBC device type.
+ * See c:type 'enum cbc_device_type <cbc_device_type>'.
+ * @device: Pointer to device struct.
+ * @open_files_head: Linked list of open files for this device.
+ *
+ * Configuration for a given CBC device. It will be stored in the device private
+ * data.
+ */
+struct cbc_device_data {
+ char *device_name;
+ enum cbc_device_type device_type;
+ struct device *device;
+ struct list_head open_files_head;
+};
+
+/*
+ * struct cbc_file_data - Data for a CBC device file .
+ * @queue: CBC buffer queue for this device.
+ * @wq_read: wait_queue_head_t fused for waking device on events.
+ * @cbc_device: Device data for this device.
+ * @list: list_head.
+ *
+ */
+struct cbc_file_data {
+ struct cbc_buffer_queue queue;
+ wait_queue_head_t wq_read;
+ struct cbc_device_data *cbc_device;
+ struct list_head list;
+};
+
+/*
+ * cbc_device_init - Initialise CBC device data
+ * @cd: pointer to CBC device data.
+ *
+ * Initialises device's list_head.
+ */
+void cbc_device_init(struct cbc_device_data *cd);
+
+/*
+ * cbc_file_init - Initialise CBC file data
+ * @file: pointer to CBC file data.
+ *
+ * Initialises device file's CBC queue, wait_queue_head_t and list_head.
+ */
+void cbc_file_init(struct cbc_file_data *file);
+
+/*
+ * cbc_file_enqueue -Add CBC buffer to queue.
+ * @fd: CBC device file data.
+ * buffer: Pointer to CBC buffer.
+ * Increases reference count on buffer.
+ */
+void cbc_file_enqueue(struct cbc_file_data *fd, struct cbc_buffer *buffer);
+
+/*
+ * cbc_file_dequeue - Remove buffer from head of queue.
+ * @fd: CBC device file data.
+ *
+ * Does not decrease reference count.
+ */
+struct cbc_buffer *cbc_file_dequeue(struct cbc_file_data *fd);
+
+/*
+ * cbc_file_queue_empty - Is CBC queue empty?
+ */
+int cbc_file_queue_empty(struct cbc_file_data *fd);
+
+#endif /* CBC_DEVICE_H_ */
diff --git a/drivers/tty/cbc/cbc_device_manager.c b/drivers/tty/cbc/cbc_device_manager.c
new file mode 100644
index 000000000000..0e74183d9828
--- /dev/null
+++ b/drivers/tty/cbc/cbc_device_manager.c
@@ -0,0 +1,879 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * CBC line discipline kernel module.
+ * Handles Carrier Board Communications (CBC) protocol.
+ *
+ * Copyright (C) 2018 Intel Corporation
+ */
+
+#include <linux/device.h>
+#include <linux/list.h>
+#include <linux/poll.h>
+#include <linux/sched.h>
+#include <linux/slab.h>
+
+#include "cbc_core.h"
+#include "cbc_device.h"
+#include "cbc_device_manager.h"
+#include "cbc_memory.h"
+#include "cbc_mux_multiplexer.h"
+#include "cbc_types.h"
+
+static int major; /* Default to dynamic major */
+module_param(major, int, 0);
+MODULE_PARM_DESC(major, "Major device number");
+
+/*
+ * Minor start number.
+ * This has to be 0 to allow a lookup of the device structs in an array.
+ */
+#define CBC_MINOR 0
+/* Device-name/driver-name when registering the devices in the linux kernel.*/
+#define DEVICE_NAME "cbc-core"
+
+/* Max number of open files per cbc channel */
+#define MAX_OPEN_FILES 6
+
+static void demuxed_receive(void *void_data, struct cbc_buffer *cbc_buffer);
+
+static int cbc_device_open(struct inode *, struct file *);
+static int cbc_device_release(struct inode *, struct file *);
+static ssize_t cbc_device_read(struct file *, char __user *, size_t,
+ loff_t *);
+static ssize_t cbc_device_write(struct file *, const char __user *, size_t,
+ loff_t *);
+static unsigned int cbc_device_poll(struct file *file, poll_table *wait);
+static long cbc_device_ioctl(struct file *, unsigned int, unsigned long);
+
+static const struct file_operations cbc_dev_file_operations = {
+ .owner = THIS_MODULE,
+ .open = cbc_device_open,
+ .release = cbc_device_release,
+ .read = cbc_device_read,
+ .write = cbc_device_write,
+ .poll = cbc_device_poll,
+ .unlocked_ioctl = cbc_device_ioctl
+};
+
+struct cbc_device_manager {
+ struct cdev cdev;
+ struct cbc_device_data channels[CBC_CHANNEL_MAX_NUMBER];
+ struct mutex send_lock;
+ struct cbc_memory_pool *cbc_memory;
+};
+
+/* Currently, only one CBC per kernel supported.*/
+static struct cbc_device_manager cbc_device_mgr_configuration = {
+ .channels[CBC_CHANNEL_PMT].device_type =
+ CBC_DEVICE_TYPE_HIDDEN,
+ .channels[CBC_CHANNEL_PMT].device_name =
+ "cbc-pmt",
+
+ .channels[CBC_CHANNEL_LIFECYCLE].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_LIFECYCLE].device_name =
+ "cbc-lifecycle",
+
+ .channels[CBC_CHANNEL_SIGNALS].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_SIGNALS].device_name =
+ "cbc-signals",
+
+ .channels[CBC_CHANNEL_EARLY_SIGNALS].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_EARLY_SIGNALS].device_name =
+ "cbc-early-signals",
+
+ .channels[CBC_CHANNEL_DIAGNOSIS].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_DIAGNOSIS].device_name =
+ "cbc-diagnosis",
+
+ .channels[CBC_CHANNEL_DLT].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_DLT].device_name =
+ "cbc-dlt",
+
+ .channels[CBC_CHANNEL_LINDA].device_type =
+ CBC_DEVICE_TYPE_HIDDEN,
+ .channels[CBC_CHANNEL_LINDA].device_name =
+ "cbc-linda",
+
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_0].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_0].device_name =
+ "cbc-raw0",
+
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_1].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_1].device_name =
+ "cbc-raw1",
+
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_2].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_2].device_name =
+ "cbc-raw2",
+
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_3].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_3].device_name =
+ "cbc-raw3",
+
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_4].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_4].device_name =
+ "cbc-raw4",
+
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_5].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_5].device_name =
+ "cbc-raw5",
+
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_6].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_6].device_name =
+ "cbc-raw6",
+
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_7].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_7].device_name =
+ "cbc-raw7",
+
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_8].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_8].device_name =
+ "cbc-raw8",
+
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_9].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_9].device_name =
+ "cbc-raw9",
+
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_10].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_10].device_name =
+ "cbc-raw10",
+
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_11].device_type =
+ CBC_DEVICE_TYPE_DEFAULT,
+ .channels[CBC_CHANNEL_OEM_RAW_CHANNEL_11].device_name =
+ "cbc-raw11",
+
+ .channels[CBC_CHANNEL_DEBUG_OUT].device_type =
+ CBC_DEVICE_TYPE_DEBUG,
+ .channels[CBC_CHANNEL_DEBUG_OUT].device_name =
+ "cbc-debug-out",
+
+ .channels[CBC_CHANNEL_DEBUG_IN].device_type =
+ CBC_DEVICE_TYPE_DEBUG,
+ .channels[CBC_CHANNEL_DEBUG_IN].device_name =
+ "cbc-debug-in",
+};
+
+static struct cbc_mux_channel_configuration cbc_mux_config;
+
+/*
+ * priority_show - Retrieve device attribute priority.
+ * @dev: device (i.e /dev/cbc*)
+ * @attr: Priority attribute
+ * @buf: Buffer to write to.
+ *
+ * Every channel (entry in /dev) has a priority.
+ * This can be set/read by a ioctl or in the sysfs.
+ */
+static ssize_t priority_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct cbc_device_data *chn_data =
+ (struct cbc_device_data *) dev_get_drvdata(dev);
+ int idx = chn_data - &cbc_device_mgr_configuration.channels[0];
+ int prio = cbc_mux_multiplexer_get_priority(idx);
+
+ pr_debug("cbc-core: read priority %i for channel: %i\n", prio, idx);
+ return scnprintf(buf, PAGE_SIZE, "%i\n", prio);
+}
+
+/*
+ * priority_store - Store device attribute priority.
+ * @dev: device (i.e /dev/cbc*)
+ * @attr: Priority attribute
+ * @buf: Buffer to write to.
+ *
+ * Every channel (entry in /dev) has a priority.
+ * This can be set/read by a ioctl or in the sysfs.
+ */
+static ssize_t priority_store(struct device *dev,
+ struct device_attribute *attr, const char *buf, size_t count)
+{
+ struct cbc_device_data *chn_data =
+ (struct cbc_device_data *) dev_get_drvdata(dev);
+ int idx = chn_data - &cbc_device_mgr_configuration.channels[0];
+ u8 tmp = 0;
+ int res = kstrtou8(buf, 0, &tmp);
+
+ if ((res == 0) && (tmp < 8)) {
+ pr_debug("cbc-core: write priority %i to channel %i\n", tmp,
+ idx);
+ cbc_mux_multiplexer_set_priority(idx, tmp);
+ }
+ return count;
+}
+static DEVICE_ATTR_RW(priority);
+
+/*
+ * cbc_device_open - Open CBC char device. Implementation of .open.
+ * @inode:Pointer to inode object for this device.
+ * @file: Pointer to file object for this device.
+ *
+ * Return 0 if device opened successfully, Linux error code otherwise.
+ */
+static int cbc_device_open(struct inode *inode, struct file *file)
+{
+ struct cbc_device_data *device_data =
+ &cbc_device_mgr_configuration.channels[MINOR(
+ inode->i_rdev)];
+ int ret = 0;
+ u32 num_open_files = 0;
+ struct cbc_file_data *file_data = kmalloc(sizeof(struct cbc_file_data),
+ GFP_KERNEL);
+
+ if (!device_data)
+ ret = -EIO;
+
+ if (!file_data)
+ ret = -ENOMEM;
+
+ if (ret == 0) {
+ pr_debug("cbc_core: device_open: %d.%d %s\n",
+ MAJOR(inode->i_rdev), MINOR(inode->i_rdev),
+ device_data->device_name);
+
+ if (MINOR(inode->i_rdev) >= CBC_CHANNEL_MAX_NUMBER) {
+ pr_err("cbc-core: invalid cbc channel number.\n");
+ ret = -ENODEV;
+ }
+ }
+
+ if (ret == 0) {
+ struct list_head *tmp;
+
+ list_for_each(tmp, &device_data->open_files_head)
+ num_open_files++;
+
+ if (num_open_files > MAX_OPEN_FILES)
+ ret = -EBUSY;
+ }
+
+ if (ret == 0) {
+ cbc_file_init(file_data);
+ file_data->cbc_device = device_data;
+ list_add(&file_data->list, &device_data->open_files_head);
+ file->private_data = file_data;
+ } else {
+ kfree(file_data);
+ }
+
+ return ret;
+}
+
+/*
+ * cbc_device_release - Release char device. Implementation of .release