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
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
|
/* Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
/*
* G-link Packet Driver -- Provides a binary G-link non-muxed packet port
* interface.
*/
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/sched.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
#include <linux/workqueue.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <asm/ioctls.h>
#include <linux/mm.h>
#include <linux/of.h>
#include <linux/ipc_logging.h>
#include <linux/termios.h>
#include <soc/qcom/glink.h>
/* This Limit ensures that auto queue will not exhaust memory on remote side */
#define MAX_PENDING_GLINK_PKT 5
#define MODULE_NAME "msm_glinkpkt"
#define DEVICE_NAME "glinkpkt"
#define WAKEUPSOURCE_TIMEOUT (2000) /* two seconds */
#define CLOSE_WAIT_TIMEOUT 1000 /* one seconds */
#define GLINK_PKT_IOCTL_MAGIC (0xC3)
#define GLINK_PKT_IOCTL_QUEUE_RX_INTENT \
_IOW(GLINK_PKT_IOCTL_MAGIC, 0, unsigned int)
#define SMD_DTR_SIG BIT(31)
#define SMD_CTS_SIG BIT(30)
#define SMD_CD_SIG BIT(29)
#define SMD_RI_SIG BIT(28)
#define map_to_smd_trans_signal(sigs) \
do { \
sigs &= 0x0fff; \
if (sigs & TIOCM_DTR) \
sigs |= SMD_DTR_SIG; \
if (sigs & TIOCM_RTS) \
sigs |= SMD_CTS_SIG; \
if (sigs & TIOCM_CD) \
sigs |= SMD_CD_SIG; \
if (sigs & TIOCM_RI) \
sigs |= SMD_RI_SIG; \
} while (0)
#define map_from_smd_trans_signal(sigs) \
do { \
if (sigs & SMD_DTR_SIG) \
sigs |= TIOCM_DSR; \
if (sigs & SMD_CTS_SIG) \
sigs |= TIOCM_CTS; \
if (sigs & SMD_CD_SIG) \
sigs |= TIOCM_CD; \
if (sigs & SMD_RI_SIG) \
sigs |= TIOCM_RI; \
sigs &= 0x0fff; \
} while (0)
/**
* glink_pkt_dev - G-Link packet device structure
* dev_list: G-Link packets device list.
* open_cfg: Transport configuration used to open Logical channel.
* dev_name: Device node name used by the clients.
* handle: Opaque Channel handle returned by G-Link.
* ch_lock: Per channel lock for synchronization.
* ch_satet: flag used to check the channel state.
* cdev: structure to the internal character device.
* devicep: Pointer to the G-Link pkt class device structure.
* i: Index to this character device.
* ref_cnt: number of references to this device.
* poll_mode: flag to check polling mode.
* ch_read_wait_queue: reader thread wait queue.
* ch_opened_wait_queue: open thread wait queue.
* ch_closed_wait_queue: close thread wait queue.
* pkt_list: The pending Rx packets list.
* pkt_list_lock: Lock to protect @pkt_list.
* pa_ws: Packet arrival Wakeup source.
* packet_arrival_work: Hold the wakeup source worker info.
* pa_spinlock: Packet arrival spinlock.
* ws_locked: flag to check wakeup source state.
* sigs_updated: flag to check signal update.
* open_time_wait: wait time for channel to fully open.
* in_reset: flag to check SSR state.
* link_info: structure to hold link information.
* link_state_handle: handle to get link state events.
* link_up: flag to check link is up or not.
*/
struct glink_pkt_dev {
struct list_head dev_list;
struct glink_open_config open_cfg;
const char *dev_name;
void *handle;
struct mutex ch_lock;
unsigned ch_state;
struct cdev cdev;
struct device *devicep;
int i;
int ref_cnt;
int poll_mode;
wait_queue_head_t ch_read_wait_queue;
wait_queue_head_t ch_opened_wait_queue;
wait_queue_head_t ch_closed_wait_queue;
struct list_head pkt_list;
spinlock_t pkt_list_lock;
struct wakeup_source pa_ws; /* Packet Arrival Wakeup Source */
struct work_struct packet_arrival_work;
spinlock_t pa_spinlock;
int ws_locked;
int sigs_updated;
int open_time_wait;
int in_reset;
struct glink_link_info link_info;
void *link_state_handle;
bool link_up;
bool auto_intent_enabled;
};
/**
* glink_rx_pkt - Pointer to Rx packet
* list: Chain the Rx packets into list.
* data: pointer to the Rx data.
* pkt_ptiv: private pointer to the Rx packet.
* size: The size of received data.
*/
struct glink_rx_pkt {
struct list_head list;
const void *data;
const void *pkt_priv;
size_t size;
};
/**
* queue_rx_intent_work - Work item to Queue Rx intent.
* size: The size of intent to be queued.
* devp: Pointer to the device structure.
* work: Hold the worker function information.
*/
struct queue_rx_intent_work {
size_t intent_size;
struct glink_pkt_dev *devp;
struct work_struct work;
};
/**
* notify_state_work - Work item to notify channel state.
* state: Channel new state.
* devp: Pointer to the device structure.
* work: Hold the worker function information.
*/
struct notify_state_work {
unsigned state;
struct glink_pkt_dev *devp;
void *handle;
struct work_struct work;
};
static DEFINE_MUTEX(glink_pkt_dev_lock_lha1);
static LIST_HEAD(glink_pkt_dev_list);
static DEFINE_MUTEX(glink_pkt_driver_lock_lha1);
static LIST_HEAD(glink_pkt_driver_list);
struct class *glink_pkt_classp;
static dev_t glink_pkt_number;
struct workqueue_struct *glink_pkt_wq;
static int num_glink_pkt_ports;
#define GLINK_PKT_IPC_LOG_PAGE_CNT 2
static void *glink_pkt_ilctxt;
enum {
GLINK_PKT_STATUS = 1U << 0,
};
static int msm_glink_pkt_debug_mask;
module_param_named(debug_mask, msm_glink_pkt_debug_mask,
int, S_IRUGO | S_IWUSR | S_IWGRP);
static void glink_pkt_queue_rx_intent_worker(struct work_struct *work);
static void glink_pkt_notify_state_worker(struct work_struct *work);
static bool glink_pkt_read_avail(struct glink_pkt_dev *devp);
#define DEBUG
#ifdef DEBUG
#define GLINK_PKT_LOG_STRING(x...) \
do { \
if (glink_pkt_ilctxt) \
ipc_log_string(glink_pkt_ilctxt, "<GLINK_PKT>: "x); \
} while (0)
#define GLINK_PKT_INFO(x...) \
do { \
if (msm_glink_pkt_debug_mask & GLINK_PKT_STATUS) \
pr_info("Status: "x); \
GLINK_PKT_LOG_STRING(x); \
} while (0)
#define GLINK_PKT_ERR(x...) \
do { \
pr_err_ratelimited("<GLINK_PKT> err: "x); \
GLINK_PKT_LOG_STRING(x); \
} while (0)
#else
#define GLINK_PKT_INFO(x...) do {} while (0)
#define GLINK_PKT_ERR(x...) do {} while (0)
#endif
static ssize_t open_timeout_store(struct device *d,
struct device_attribute *attr,
const char *buf,
size_t n)
{
struct glink_pkt_dev *devp;
long tmp;
mutex_lock(&glink_pkt_dev_lock_lha1);
list_for_each_entry(devp, &glink_pkt_dev_list, dev_list) {
if (devp->devicep == d) {
if (!kstrtol(buf, 0, &tmp)) {
devp->open_time_wait = tmp;
mutex_unlock(&glink_pkt_dev_lock_lha1);
return n;
} else {
mutex_unlock(&glink_pkt_dev_lock_lha1);
pr_err("%s: unable to convert: %s to an int\n",
__func__, buf);
return -EINVAL;
}
}
}
mutex_unlock(&glink_pkt_dev_lock_lha1);
GLINK_PKT_ERR("%s: unable to match device to valid port\n", __func__);
return -EINVAL;
}
static ssize_t open_timeout_show(struct device *d,
struct device_attribute *attr,
char *buf)
{
struct glink_pkt_dev *devp;
mutex_lock(&glink_pkt_dev_lock_lha1);
list_for_each_entry(devp, &glink_pkt_dev_list, dev_list) {
if (devp->devicep == d) {
mutex_unlock(&glink_pkt_dev_lock_lha1);
return snprintf(buf, PAGE_SIZE, "%d\n",
devp->open_time_wait);
}
}
mutex_unlock(&glink_pkt_dev_lock_lha1);
GLINK_PKT_ERR("%s: unable to match device to valid port\n", __func__);
return -EINVAL;
}
static DEVICE_ATTR(open_timeout, 0664, open_timeout_show, open_timeout_store);
/**
* packet_arrival_worker() - wakeup source timeout worker fn
* work: Work struct queued
*
* This function used to keep the system awake to allow
* userspace client to read the received packet.
*/
static void packet_arrival_worker(struct work_struct *work)
{
struct glink_pkt_dev *devp;
unsigned long flags;
devp = container_of(work, struct glink_pkt_dev,
packet_arrival_work);
mutex_lock(&devp->ch_lock);
spin_lock_irqsave(&devp->pa_spinlock, flags);
if (devp->ws_locked) {
GLINK_PKT_INFO("%s locking glink_pkt_dev id:%d wakeup source\n",
__func__, devp->i);
/*
* Keep system awake long enough to allow userspace client
* to process the packet.
*/
__pm_wakeup_event(&devp->pa_ws, WAKEUPSOURCE_TIMEOUT);
}
spin_unlock_irqrestore(&devp->pa_spinlock, flags);
mutex_unlock(&devp->ch_lock);
}
/**
* glink_pkt_link_state_cb() - Callback to receive link state updates
* @cb_info: Information containing link & its state.
* @priv: Private data passed during the link state registration.
*
* This function is called by the GLINK core to notify the Glink Pkt drivers
* regarding the link state updates. This function is registered with the
* GLINK core by Glink pkt drivers with glink_register_link_state_cb().
*/
static void glink_pkt_link_state_cb(struct glink_link_state_cb_info *cb_info,
void *priv)
{
struct glink_pkt_dev *devp = (struct glink_pkt_dev *)priv;
if (!cb_info)
return;
if (!devp)
return;
if (cb_info->link_state == GLINK_LINK_STATE_UP) {
devp->link_up = true;
wake_up_interruptible(&devp->ch_opened_wait_queue);
} else if (cb_info->link_state == GLINK_LINK_STATE_DOWN) {
devp->link_up = false;
}
}
/**
* glink_pkt_notify_rx() - Rx data Callback from G-Link core layer
* handle: Opaque Channel handle returned by GLink.
* priv: private pointer to the channel.
* pkt_priv: private pointer to the packet.
* ptr: Pointer to the Rx data.
* size: Size of the Rx data.
*
* This callback function is notified on receiving the data from
* remote channel.
*/
void glink_pkt_notify_rx(void *handle, const void *priv,
const void *pkt_priv,
const void *ptr, size_t size)
{
struct glink_rx_pkt *pkt = NULL;
struct glink_pkt_dev *devp = (struct glink_pkt_dev *)priv;
unsigned long flags;
GLINK_PKT_INFO("%s(): priv[%p] data[%p] size[%zu]\n",
__func__, pkt_priv, (char *)ptr, size);
pkt = kzalloc(sizeof(*pkt), GFP_ATOMIC);
if (!pkt) {
GLINK_PKT_ERR("%s: memory allocation failed\n", __func__);
return;
}
pkt->data = ptr;
pkt->pkt_priv = pkt_priv;
pkt->size = size;
spin_lock_irqsave(&devp->pkt_list_lock, flags);
list_add_tail(&pkt->list, &devp->pkt_list);
spin_unlock_irqrestore(&devp->pkt_list_lock, flags);
spin_lock_irqsave(&devp->pa_spinlock, flags);
__pm_stay_awake(&devp->pa_ws);
devp->ws_locked = 1;
spin_unlock_irqrestore(&devp->pa_spinlock, flags);
wake_up(&devp->ch_read_wait_queue);
schedule_work(&devp->packet_arrival_work);
return;
}
/**
* glink_pkt_notify_tx_done() - Tx done callback function
* handle: Opaque Channel handle returned by GLink.
* priv: private pointer to the channel.
* pkt_priv: private pointer to the packet.
* ptr: Pointer to the Tx data.
*
* This callback function is notified when the remote core
* signals the Rx done to the local core.
*/
void glink_pkt_notify_tx_done(void *handle, const void *priv,
const void *pkt_priv, const void *ptr)
{
GLINK_PKT_INFO("%s(): priv[%p] pkt_priv[%p] ptr[%p]\n",
__func__, priv, pkt_priv, ptr);
/* Free Tx buffer allocated in glink_pkt_write */
kfree(ptr);
}
/**
* glink_pkt_notify_state() - state notification callback function
* handle: Opaque Channel handle returned by GLink.
* priv: private pointer to the channel.
* event: channel state
*
* This callback function is notified when the remote channel alters
* the channel state and send the event to local G-Link core.
*/
void glink_pkt_notify_state(void *handle, const void *priv, unsigned event)
{
struct glink_pkt_dev *devp = (struct glink_pkt_dev *)priv;
struct notify_state_work *work_item;
if ((devp->handle != NULL) && (devp->handle != handle)) {
GLINK_PKT_ERR("%s() event[%d] on incorrect channel [%s]\n",
__func__, event, devp->open_cfg.name);
return;
}
GLINK_PKT_INFO("%s(): event[%d] on [%s]\n", __func__, event,
devp->open_cfg.name);
work_item = kzalloc(sizeof(*work_item), GFP_ATOMIC);
if (!work_item) {
GLINK_PKT_ERR("%s() failed allocate work_item\n", __func__);
return;
}
work_item->state = event;
work_item->devp = devp;
work_item->handle = handle;
INIT_WORK(&work_item->work, glink_pkt_notify_state_worker);
queue_work(glink_pkt_wq, &work_item->work);
}
/**
* glink_pkt_rmt_rx_intent_req_cb() - Remote Rx intent request callback
* handle: Opaque Channel handle returned by GLink.
* priv: private pointer to the channel.
* sz: the size of the requested Rx intent
*
* This callback function is notified when remote client
* request the intent from local client.
*/
bool glink_pkt_rmt_rx_intent_req_cb(void *handle, const void *priv, size_t sz)
{
struct queue_rx_intent_work *work_item;
int pending_pkt_count = 0;
struct glink_rx_pkt *pkt = NULL;
unsigned long flags;
struct glink_pkt_dev *devp = (struct glink_pkt_dev *)priv;
GLINK_PKT_INFO("%s(): QUEUE RX INTENT to receive size[%zu]\n",
__func__, sz);
if (devp->auto_intent_enabled) {
spin_lock_irqsave(&devp->pkt_list_lock, flags);
list_for_each_entry(pkt, &devp->pkt_list, list)
pending_pkt_count++;
spin_unlock_irqrestore(&devp->pkt_list_lock, flags);
if (pending_pkt_count > MAX_PENDING_GLINK_PKT) {
GLINK_PKT_ERR("%s failed, max limit reached\n",
__func__);
return false;
}
} else {
return false;
}
work_item = kzalloc(sizeof(*work_item), GFP_ATOMIC);
if (!work_item) {
GLINK_PKT_ERR("%s failed allocate work_item\n", __func__);
return false;
}
work_item->intent_size = sz;
work_item->devp = devp;
INIT_WORK(&work_item->work, glink_pkt_queue_rx_intent_worker);
queue_work(glink_pkt_wq, &work_item->work);
return true;
}
/**
* glink_pkt_notify_rx_sigs() - signals callback
* handle: Opaque Channel handle returned by GLink.
* priv: private pointer to the channel.
* old_sigs: signal before modification
* new_sigs: signal after modification
*
* This callback function is notified when remote client
* updated the signal.
*/
void glink_pkt_notify_rx_sigs(void *handle, const void *priv,
uint32_t old_sigs, uint32_t new_sigs)
{
struct glink_pkt_dev *devp = (struct glink_pkt_dev *)priv;
GLINK_PKT_INFO("%s(): sigs old[%x] new[%x]\n",
__func__, old_sigs, new_sigs);
mutex_lock(&devp->ch_lock);
devp->sigs_updated = true;
mutex_unlock(&devp->ch_lock);
wake_up(&devp->ch_read_wait_queue);
}
/**
* glink_pkt_queue_rx_intent_worker() - Queue Rx worker function
*
* work: Pointer to the work struct
*
* This function is used to queue the RX intent which
* can sleep during allocation of larger buffers.
*/
static void glink_pkt_queue_rx_intent_worker(struct work_struct *work)
{
int ret;
struct queue_rx_intent_work *work_item =
container_of(work,
struct queue_rx_intent_work, work);
struct glink_pkt_dev *devp = work_item->devp;
if (!devp) {
GLINK_PKT_ERR("%s: Invalid device\n", __func__);
kfree(work_item);
return;
}
mutex_lock(&devp->ch_lock);
if (!devp->handle) {
GLINK_PKT_ERR("%s: Invalid device Handle\n", __func__);
mutex_unlock(&devp->ch_lock);
kfree(work_item);
return;
}
ret = glink_queue_rx_intent(devp->handle, devp, work_item->intent_size);
mutex_unlock(&devp->ch_lock);
GLINK_PKT_INFO("%s: Triggered with size[%zu] ret[%d]\n",
__func__, work_item->intent_size, ret);
if (ret)
GLINK_PKT_ERR("%s queue_rx_intent failed\n", __func__);
kfree(work_item);
}
/**
* glink_pkt_notify_state_worker() - Notify state worker function
*
* work: Pointer to the work struct
*
* This function is used to notify the channel state and update the
* internal data structure.
*/
static void glink_pkt_notify_state_worker(struct work_struct *work)
{
struct notify_state_work *work_item =
container_of(work,
struct notify_state_work, work);
struct glink_pkt_dev *devp = work_item->devp;
unsigned event = work_item->state;
void *handle = work_item->handle;
if (!devp) {
GLINK_PKT_ERR("%s: Invalid device Handle\n", __func__);
kfree(work_item);
return;
}
GLINK_PKT_INFO("%s(): event[%d] on [%s]\n", __func__,
event, devp->open_cfg.name);
mutex_lock(&devp->ch_lock);
devp->ch_state = event;
if (event == GLINK_CONNECTED) {
if (!devp->handle) {
GLINK_PKT_ERR("%s: Invalid device handle\n", __func__);
goto exit;
}
devp->in_reset = 0;
wake_up_interruptible(&devp->ch_opened_wait_queue);
} else if (event == GLINK_REMOTE_DISCONNECTED) {
devp->in_reset = 1;
wake_up(&devp->ch_read_wait_queue);
wake_up_interruptible(&devp->ch_opened_wait_queue);
} else if (event == GLINK_LOCAL_DISCONNECTED) {
if (devp->handle == handle)
devp->handle = NULL;
wake_up_interruptible(&devp->ch_closed_wait_queue);
}
exit:
mutex_unlock(&devp->ch_lock);
kfree(work_item);
}
/**
* glink_pkt_read_avail() - check any pending packets to read
* devp: pointer to G-Link packet device.
*
* This function is used to check any pending data packets are
* available to read or not.
*/
static bool glink_pkt_read_avail(struct glink_pkt_dev *devp)
{
bool list_is_empty;
unsigned long flags;
spin_lock_irqsave(&devp->pkt_list_lock, flags);
list_is_empty = list_empty(&devp->pkt_list);
spin_unlock_irqrestore(&devp->pkt_list_lock, flags);
return !list_is_empty;
}
/**
* glink_pkt_read() - read() syscall for the glink_pkt device
* file: Pointer to the file structure.
* buf: Pointer to the userspace buffer.
* count: Number bytes to read from the file.
* ppos: Pointer to the position into the file.
*
* This function is used to Read the data from glink pkt device when
* userspace client do a read() system call. All input arguments are
* validated by the virtual file system before calling this function.
*/
ssize_t glink_pkt_read(struct file *file,
char __user *buf,
size_t count,
loff_t *ppos)
{
int ret = 0;
struct glink_pkt_dev *devp;
struct glink_rx_pkt *pkt = NULL;
unsigned long flags;
devp = file->private_data;
if (!devp) {
GLINK_PKT_ERR("%s on NULL glink_pkt_dev\n", __func__);
return -EINVAL;
}
if (!devp->handle) {
GLINK_PKT_ERR("%s on a closed glink_pkt_dev id:%d\n",
__func__, devp->i);
return -EINVAL;
}
if (devp->in_reset) {
GLINK_PKT_ERR("%s: notifying reset for glink_pkt_dev id:%d\n",
__func__, devp->i);
return -ENETRESET;
}
mutex_lock(&devp->ch_lock);
if (!glink_pkt_read_avail(devp) &&
!glink_rx_intent_exists(devp->handle, count)) {
ret = glink_queue_rx_intent(devp->handle, devp, count);
if (ret) {
GLINK_PKT_ERR("%s: failed to queue intent ret[%d]\n",
__func__, ret);
mutex_unlock(&devp->ch_lock);
return ret;
}
}
mutex_unlock(&devp->ch_lock);
GLINK_PKT_INFO("Begin %s on glink_pkt_dev id:%d buffer_size %zu\n",
__func__, devp->i, count);
ret = wait_event_interruptible(devp->ch_read_wait_queue,
!devp->handle || devp->in_reset ||
glink_pkt_read_avail(devp));
if (devp->in_reset) {
GLINK_PKT_ERR("%s: notifying reset for glink_pkt_dev id:%d\n",
__func__, devp->i);
return -ENETRESET;
}
if (!devp->handle) {
GLINK_PKT_ERR("%s on a closed glink_pkt_dev id:%d\n",
__func__, devp->i);
return -EINVAL;
}
if (ret < 0) {
/* qualify error message */
if (ret != -ERESTARTSYS) {
/* we get this anytime a signal comes in */
GLINK_PKT_ERR("%s: wait on dev id:%d ret %i\n",
__func__, devp->i, ret);
}
return ret;
}
spin_lock_irqsave(&devp->pkt_list_lock, flags);
pkt = list_first_entry(&devp->pkt_list, struct glink_rx_pkt, list);
if (pkt->size > count) {
GLINK_PKT_ERR("%s: Small Buff on dev Id:%d-[%zu > %zu]\n",
__func__, devp->i, pkt->size, count);
spin_unlock_irqrestore(&devp->pkt_list_lock, flags);
return -ETOOSMALL;
}
list_del(&pkt->list);
spin_unlock_irqrestore(&devp->pkt_list_lock, flags);
ret = copy_to_user(buf, pkt->data, pkt->size);
if (ret) {
GLINK_PKT_ERR(
"%s copy_to_user failed ret[%d] on dev id:%d size %zu\n",
__func__, ret, devp->i, pkt->size);
spin_lock_irqsave(&devp->pkt_list_lock, flags);
list_add_tail(&pkt->list, &devp->pkt_list);
spin_unlock_irqrestore(&devp->pkt_list_lock, flags);
return -EFAULT;
}
ret = pkt->size;
glink_rx_done(devp->handle, pkt->data, false);
kfree(pkt);
mutex_lock(&devp->ch_lock);
spin_lock_irqsave(&devp->pa_spinlock, flags);
if (devp->poll_mode && !glink_pkt_read_avail(devp)) {
__pm_relax(&devp->pa_ws);
devp->ws_locked = 0;
devp->poll_mode = 0;
GLINK_PKT_INFO("%s unlocked pkt_dev id:%d wakeup_source\n",
__func__, devp->i);
}
spin_unlock_irqrestore(&devp->pa_spinlock, flags);
mutex_unlock(&devp->ch_lock);
GLINK_PKT_INFO("End %s on glink_pkt_dev id:%d ret[%d]\n",
__func__, devp->i, ret);
return ret;
}
/**
* glink_pkt_write() - write() syscall for the glink_pkt device
* file: Pointer to the file structure.
* buf: Pointer to the userspace buffer.
* count: Number bytes to read from the file.
* ppos: Pointer to the position into the file.
*
* This function is used to write the data to glink pkt device when
* userspace client do a write() system call. All input arguments are
* validated by the virtual file system before calling this function.
*/
ssize_t glink_pkt_write(struct file *file,
const char __user *buf,
size_t count,
loff_t *ppos)
{
int ret = 0;
struct glink_pkt_dev *devp;
void *data;
devp = file->private_data;
if (!count) {
GLINK_PKT_ERR("%s: data count is zero\n", __func__);
return -EINVAL;
}
if (!devp) {
GLINK_PKT_ERR("%s on NULL glink_pkt_dev\n", __func__);
return -EINVAL;
}
if (!devp->handle) {
GLINK_PKT_ERR("%s on a closed glink_pkt_dev id:%d\n",
__func__, devp->i);
return -EINVAL;
}
if (devp->in_reset) {
GLINK_PKT_ERR("%s: notifying reset for glink_pkt_dev id:%d\n",
__func__, devp->i);
return -ENETRESET;
};
GLINK_PKT_INFO("Begin %s on glink_pkt_dev id:%d buffer_size %zu\n",
__func__, devp->i, count);
data = kzalloc(count, GFP_KERNEL);
if (!data) {
GLINK_PKT_ERR("%s buffer allocation failed\n", __func__);
return -ENOMEM;
}
ret = copy_from_user(data, buf, count);
if (ret) {
GLINK_PKT_ERR(
"%s copy_from_user failed ret[%d] on dev id:%d size %zu\n",
__func__, ret, devp->i, count);
kfree(data);
return -EFAULT;
}
ret = glink_tx(devp->handle, data, data, count, GLINK_TX_REQ_INTENT);
if (ret) {
GLINK_PKT_ERR("%s glink_tx failed ret[%d]\n", __func__, ret);
kfree(data);
return ret;
}
GLINK_PKT_INFO("Finished %s on glink_pkt_dev id:%d buffer_size %zu\n",
__func__, devp->i, count);
return count;
}
/**
* glink_pkt_poll() - poll() syscall for the glink_pkt device
* file: Pointer to the file structure.
* wait: pointer to Poll table.
*
* This function is used to poll on the glink pkt device when
* userspace client do a poll() system call. All input arguments are
* validated by the virtual file system before calling this function.
*/
static unsigned int glink_pkt_poll(struct file *file, poll_table *wait)
{
struct glink_pkt_dev *devp;
unsigned int mask = 0;
devp = file->private_data;
if (!devp || !devp->handle) {
GLINK_PKT_ERR("%s: Invalid device handle\n", __func__);
return POLLERR;
}
if (devp->in_reset)
return POLLHUP;
devp->poll_mode = 1;
poll_wait(file, &devp->ch_read_wait_queue, wait);
mutex_lock(&devp->ch_lock);
if (!devp->handle) {
mutex_unlock(&devp->ch_lock);
return POLLERR;
}
if (devp->in_reset) {
mutex_unlock(&devp->ch_lock);
return POLLHUP;
}
if (glink_pkt_read_avail(devp)) {
mask |= POLLIN | POLLRDNORM;
GLINK_PKT_INFO("%s sets POLLIN for glink_pkt_dev id: %d\n",
__func__, devp->i);
}
if (devp->sigs_updated) {
mask |= POLLPRI;
GLINK_PKT_INFO("%s sets POLLPRI for glink_pkt_dev id: %d\n",
__func__, devp->i);
}
mutex_unlock(&devp->ch_lock);
return mask;
}
/**
* glink_pkt_tiocmset() - set the signals for glink_pkt device
* devp: Pointer to the glink_pkt device structure.
* cmd: IOCTL command.
* arg: Arguments to the ioctl call.
*
* This function is used to set the signals on the glink pkt device
* when userspace client do a ioctl() system call with TIOCMBIS,
* TIOCMBIC and TICOMSET.
*/
static int glink_pkt_tiocmset(struct glink_pkt_dev *devp, unsigned int cmd,
unsigned long arg)
{
int ret;
uint32_t sigs;
uint32_t val;
ret = get_user(val, (uint32_t *)arg);
if (ret)
return ret;
map_to_smd_trans_signal(val);
ret = glink_sigs_local_get(devp->handle, &sigs);
if (ret < 0) {
GLINK_PKT_ERR("%s: Get signals failed[%d]\n", __func__, ret);
return ret;
}
switch (cmd) {
case TIOCMBIS:
sigs |= val;
break;
case TIOCMBIC:
sigs &= ~val;
break;
case TIOCMSET:
sigs = val;
break;
}
ret = glink_sigs_set(devp->handle, sigs);
GLINK_PKT_INFO("%s: sigs[0x%x] ret[%d]\n", __func__, sigs, ret);
return ret;
}
/**
* glink_pkt_ioctl() - ioctl() syscall for the glink_pkt device
* file: Pointer to the file structure.
* cmd: IOCTL command.
* arg: Arguments to the ioctl call.
*
* This function is used to ioctl on the glink pkt device when
* userspace client do a ioctl() system call. All input arguments are
* validated by the virtual file system before calling this function.
*/
static long glink_pkt_ioctl(struct file *file, unsigned int cmd,
unsigned long arg)
{
int ret;
struct glink_pkt_dev *devp;
uint32_t size = 0;
uint32_t sigs = 0;
devp = file->private_data;
if (!devp || !devp->handle) {
GLINK_PKT_ERR("%s: Invalid device handle\n", __func__);
return -EINVAL;
}
mutex_lock(&devp->ch_lock);
switch (cmd) {
case TIOCMGET:
devp->sigs_updated = false;
ret = glink_sigs_remote_get(devp->handle, &sigs);
GLINK_PKT_INFO("%s: TIOCMGET ret[%d] sigs[0x%x]\n",
__func__, ret, sigs);
map_from_smd_trans_signal(sigs);
if (!ret)
ret = put_user(sigs, (uint32_t *)arg);
break;
case TIOCMSET:
case TIOCMBIS:
case TIOCMBIC:
ret = glink_pkt_tiocmset(devp, cmd, arg);
break;
case GLINK_PKT_IOCTL_QUEUE_RX_INTENT:
ret = get_user(size, (uint32_t *)arg);
GLINK_PKT_INFO("%s: intent size[%d]\n", __func__, size);
devp->auto_intent_enabled = false;
ret = glink_queue_rx_intent(devp->handle, devp, size);
if (ret) {
GLINK_PKT_ERR("%s: failed to QUEUE_RX_INTENT ret[%d]\n",
__func__, ret);
}
break;
default:
GLINK_PKT_ERR("%s: Unrecognized ioctl command 0x%x\n",
__func__, cmd);
ret = -ENOIOCTLCMD;
break;
}
mutex_unlock(&devp->ch_lock);
return ret;
}
/**
* glink_pkt_open() - open() syscall for the glink_pkt device
* inode: Pointer to the inode structure.
* file: Pointer to the file structure.
*
* This function is used to open the glink pkt device when
* userspace client do a open() system call. All input arguments are
* validated by the virtual file system before calling this function.
*/
int glink_pkt_open(struct inode *inode, struct file *file)
{
int ret = 0;
struct glink_pkt_dev *devp = NULL;
int wait_time_msecs;
devp = container_of(inode->i_cdev, struct glink_pkt_dev, cdev);
if (!devp) {
GLINK_PKT_ERR("%s on NULL device\n", __func__);
return -EINVAL;
}
GLINK_PKT_INFO("Begin %s() on dev id:%d open_time_wait[%d] by [%s]\n",
__func__, devp->i, devp->open_time_wait, current->comm);
file->private_data = devp;
wait_time_msecs = devp->open_time_wait * 1000;
mutex_lock(&devp->ch_lock);
/* waiting for previous close to complete */
if (devp->handle && devp->ref_cnt == 0) {
mutex_unlock(&devp->ch_lock);
if (wait_time_msecs < 0) {
ret = wait_event_interruptible(
devp->ch_opened_wait_queue,
devp->ch_state == GLINK_LOCAL_DISCONNECTED);
} else {
ret = wait_event_interruptible_timeout(
devp->ch_opened_wait_queue,
devp->ch_state == GLINK_LOCAL_DISCONNECTED,
msecs_to_jiffies(wait_time_msecs));
if (ret >= 0)
wait_time_msecs = jiffies_to_msecs(ret);
if (ret == 0)
ret = -ETIMEDOUT;
}
if (ret < 0) {
GLINK_PKT_ERR(
"%s:failed for prev close on dev id:%d rc:%d\n",
__func__, devp->i, ret);
return ret;
}
mutex_lock(&devp->ch_lock);
}
if (!devp->handle) {
mutex_unlock(&devp->ch_lock);
/*
* Wait for the link to be complete up so we know
* the remote is ready enough.
*/
if (wait_time_msecs < 0) {
ret = wait_event_interruptible(
devp->ch_opened_wait_queue,
devp->link_up == true);
} else {
ret = wait_event_interruptible_timeout(
devp->ch_opened_wait_queue,
devp->link_up == true,
msecs_to_jiffies(wait_time_msecs));
if (ret >= 0)
wait_time_msecs = jiffies_to_msecs(ret);
if (ret == 0)
ret = -ETIMEDOUT;
}
mutex_lock(&devp->ch_lock);
if (ret < 0) {
GLINK_PKT_ERR(
"%s: Link not up edge[%s] name[%s] rc:%d\n",
__func__, devp->open_cfg.edge,
devp->open_cfg.name, ret);
devp->handle = NULL;
goto error;
}
devp->handle = glink_open(&devp->open_cfg);
if (IS_ERR_OR_NULL(devp->handle)) {
GLINK_PKT_ERR(
"%s: open failed xprt[%s] edge[%s] name[%s]\n",
__func__, devp->open_cfg.transport,
devp->open_cfg.edge, devp->open_cfg.name);
ret = -ENODEV;
devp->handle = NULL;
goto error;
}
mutex_unlock(&devp->ch_lock);
/*
* Wait for the channel to be complete open state so we know
* the remote client is ready enough.
*/
if (wait_time_msecs < 0) {
ret = wait_event_interruptible(
devp->ch_opened_wait_queue,
devp->ch_state == GLINK_CONNECTED);
} else {
ret = wait_event_interruptible_timeout(
devp->ch_opened_wait_queue,
devp->ch_state == GLINK_CONNECTED,
msecs_to_jiffies(wait_time_msecs));
if (ret == 0)
ret = -ETIMEDOUT;
}
mutex_lock(&devp->ch_lock);
if (ret < 0) {
GLINK_PKT_ERR("%s: open failed on dev id:%d rc:%d\n",
__func__, devp->i, ret);
glink_close(devp->handle);
devp->handle = NULL;
goto error;
}
}
ret = 0;
devp->ref_cnt++;
error:
mutex_unlock(&devp->ch_lock);
GLINK_PKT_INFO("END %s() on dev id:%d ref_cnt[%d] ret[%d]\n",
__func__, devp->i, devp->ref_cnt, ret);
return ret;
}
/**
* pop_rx_pkt() - return first pkt from rx pkt_list
* devp: pointer to G-Link packet device.
*
* This function return first item from rx pkt_list and NULL if list is empty.
*/
struct glink_rx_pkt *pop_rx_pkt(struct glink_pkt_dev *devp)
{
unsigned long flags;
struct glink_rx_pkt *pkt = NULL;
spin_lock_irqsave(&devp->pkt_list_lock, flags);
if (!list_empty(&devp->pkt_list)) {
pkt = list_first_entry(&devp->pkt_list,
struct glink_rx_pkt, list);
list_del(&pkt->list);
}
spin_unlock_irqrestore(&devp->pkt_list_lock, flags);
return pkt;
}
/**
* glink_pkt_release() - release operation on glink_pkt device
* inode: Pointer to the inode structure.
* file: Pointer to the file structure.
*
* This function is used to release the glink pkt device when
* userspace client do a close() system call. All input arguments are
* validated by the virtual file system before calling this function.
*/
int glink_pkt_release(struct inode *inode, struct file *file)
{
int ret = 0;
struct glink_pkt_dev *devp = file->private_data;
unsigned long flags;
struct glink_rx_pkt *pkt;
GLINK_PKT_INFO("%s() on dev id:%d by [%s] ref_cnt[%d]\n",
__func__, devp->i, current->comm, devp->ref_cnt);
mutex_lock(&devp->ch_lock);
if (devp->ref_cnt > 0)
devp->ref_cnt--;
if (devp->handle && devp->ref_cnt == 0) {
while ((pkt = pop_rx_pkt(devp))) {
glink_rx_done(devp->handle, pkt->data, false);
kfree(pkt);
}
wake_up(&devp->ch_read_wait_queue);
wake_up_interruptible(&devp->ch_opened_wait_queue);
ret = glink_close(devp->handle);
devp->handle = NULL;
if (ret) {
GLINK_PKT_ERR("%s: close failed ret[%d]\n",
__func__, ret);
} else {
mutex_unlock(&devp->ch_lock);
ret = wait_event_interruptible_timeout(
devp->ch_closed_wait_queue,
devp->ch_state == GLINK_LOCAL_DISCONNECTED,
msecs_to_jiffies(CLOSE_WAIT_TIMEOUT));
if (ret == 0)
GLINK_PKT_ERR(
"%s(): close TIMEOUT on dev_id[%d]\n",
__func__, devp->i);
mutex_lock(&devp->ch_lock);
}
devp->poll_mode = 0;
spin_lock_irqsave(&devp->pa_spinlock, flags);
if (devp->ws_locked) {
__pm_relax(&devp->pa_ws);
devp->ws_locked = 0;
}
spin_unlock_irqrestore(&devp->pa_spinlock, flags);
devp->sigs_updated = false;
devp->in_reset = 0;
}
mutex_unlock(&devp->ch_lock);
if (flush_work(&devp->packet_arrival_work))
GLINK_PKT_INFO("%s: Flushed work for glink_pkt_dev id:%d\n",
__func__, devp->i);
return ret;
}
static const struct file_operations glink_pkt_fops = {
.owner = THIS_MODULE,
.open = glink_pkt_open,
.release = glink_pkt_release,
.read = glink_pkt_read,
.write = glink_pkt_write,
.poll = glink_pkt_poll,
.unlocked_ioctl = glink_pkt_ioctl,
.compat_ioctl = glink_pkt_ioctl,
};
/**
* glink_pkt_init_add_device() - Initialize G-Link packet device and add cdev
* devp: pointer to G-Link packet device.
* i: index of the G-Link packet device.
*
* return: 0 for success, Standard Linux errors
*/
static int glink_pkt_init_add_device(struct glink_pkt_dev *devp, int i)
{
int ret = 0;
devp->open_cfg.notify_rx = glink_pkt_notify_rx;
devp->open_cfg.notify_tx_done = glink_pkt_notify_tx_done;
devp->open_cfg.notify_state = glink_pkt_notify_state;
devp->open_cfg.notify_rx_intent_req = glink_pkt_rmt_rx_intent_req_cb;
devp->open_cfg.notify_rx_sigs = glink_pkt_notify_rx_sigs;
devp->open_cfg.options |= GLINK_OPT_INITIAL_XPORT;
devp->open_cfg.priv = devp;
devp->link_up = false;
devp->link_info.edge = devp->open_cfg.edge;
devp->link_info.transport = devp->open_cfg.transport;
devp->link_info.glink_link_state_notif_cb =
glink_pkt_link_state_cb;
devp->i = i;
devp->poll_mode = 0;
devp->auto_intent_enabled = true;
devp->ws_locked = 0;
devp->ch_state = GLINK_LOCAL_DISCONNECTED;
/* Default timeout for open wait is 120sec */
devp->open_time_wait = 120;
mutex_init(&devp->ch_lock);
init_waitqueue_head(&devp->ch_read_wait_queue);
init_waitqueue_head(&devp->ch_opened_wait_queue);
init_waitqueue_head(&devp->ch_closed_wait_queue);
spin_lock_init(&devp->pa_spinlock);
INIT_LIST_HEAD(&devp->pkt_list);
spin_lock_init(&devp->pkt_list_lock);
wakeup_source_init(&devp->pa_ws, devp->dev_name);
INIT_WORK(&devp->packet_arrival_work, packet_arrival_worker);
devp->link_state_handle =
glink_register_link_state_cb(&devp->link_info, devp);
if (IS_ERR_OR_NULL(devp->link_state_handle)) {
GLINK_PKT_ERR(
"%s: link state cb reg. failed edge[%s] name[%s]\n",
__func__, devp->open_cfg.edge, devp->open_cfg.name);
ret = PTR_ERR(devp->link_state_handle);
return ret;
}
cdev_init(&devp->cdev, &glink_pkt_fops);
devp->cdev.owner = THIS_MODULE;
ret = cdev_add(&devp->cdev, (glink_pkt_number + i), 1);
if (IS_ERR_VALUE(ret)) {
GLINK_PKT_ERR("%s: cdev_add() failed for dev id:%d ret:%i\n",
__func__, i, ret);
wakeup_source_trash(&devp->pa_ws);
return ret;
}
devp->devicep = device_create(glink_pkt_classp,
NULL,
(glink_pkt_number + i),
NULL,
devp->dev_name);
if (IS_ERR_OR_NULL(devp->devicep)) {
GLINK_PKT_ERR("%s: device_create() failed for dev id:%d\n",
__func__, i);
ret = -ENOMEM;
cdev_del(&devp->cdev);
wakeup_source_trash(&devp->pa_ws);
return ret;
}
if (device_create_file(devp->devicep, &dev_attr_open_timeout))
GLINK_PKT_ERR("%s: device_create_file() failed for id:%d\n",
__func__, i);
mutex_lock(&glink_pkt_dev_lock_lha1);
list_add(&devp->dev_list, &glink_pkt_dev_list);
mutex_unlock(&glink_pkt_dev_lock_lha1);
return ret;
}
/**
* glink_pkt_core_deinit- De-initialization for this module
*
* This function remove all the memory and unregister
* the char device region.
*/
static void glink_pkt_core_deinit(void)
{
struct glink_pkt_dev *glink_pkt_devp;
struct glink_pkt_dev *index;
mutex_lock(&glink_pkt_dev_lock_lha1);
list_for_each_entry_safe(glink_pkt_devp, index, &glink_pkt_dev_list,
dev_list) {
if (glink_pkt_devp->link_state_handle)
glink_unregister_link_state_cb(
glink_pkt_devp->link_state_handle);
cdev_del(&glink_pkt_devp->cdev);
list_del(&glink_pkt_devp->dev_list);
device_destroy(glink_pkt_classp,
MKDEV(MAJOR(glink_pkt_number),
glink_pkt_devp->i));
kfree(glink_pkt_devp);
}
mutex_unlock(&glink_pkt_dev_lock_lha1);
if (!IS_ERR_OR_NULL(glink_pkt_classp))
class_destroy(glink_pkt_classp);
unregister_chrdev_region(MAJOR(glink_pkt_number), num_glink_pkt_ports);
}
/**
* glink_pkt_alloc_chrdev_region() - allocate the char device region
*
* This function allocate memory for G-Link packet character-device region and
* create the class.
*/
static int glink_pkt_alloc_chrdev_region(void)
{
int ret;
ret = alloc_chrdev_region(&glink_pkt_number,
0,
num_glink_pkt_ports,
DEVICE_NAME);
if (IS_ERR_VALUE(ret)) {
GLINK_PKT_ERR("%s: alloc_chrdev_region() failed ret:%i\n",
__func__, ret);
return ret;
}
glink_pkt_classp = class_create(THIS_MODULE, DEVICE_NAME);
if (IS_ERR(glink_pkt_classp)) {
GLINK_PKT_ERR("%s: class_create() failed ENOMEM\n", __func__);
ret = -ENOMEM;
unregister_chrdev_region(MAJOR(glink_pkt_number),
num_glink_pkt_ports);
return ret;
}
return 0;
}
/**
* parse_glinkpkt_devicetree() - parse device tree binding
*
* node: pointer to device tree node
* glink_pkt_devp: pointer to GLINK PACKET device
*
* Return: 0 on success, -ENODEV on failure.
*/
static int parse_glinkpkt_devicetree(struct device_node *node,
struct glink_pkt_dev *glink_pkt_devp)
{
char *key;
key = "qcom,glinkpkt-transport";
glink_pkt_devp->open_cfg.transport = of_get_property(node, key, NULL);
if (!glink_pkt_devp->open_cfg.transport)
goto error;
GLINK_PKT_INFO("%s transport = %s\n", __func__,
glink_pkt_devp->open_cfg.transport);
key = "qcom,glinkpkt-edge";
glink_pkt_devp->open_cfg.edge = of_get_property(node, key, NULL);
if (!glink_pkt_devp->open_cfg.edge)
goto error;
GLINK_PKT_INFO("%s edge = %s\n", __func__,
glink_pkt_devp->open_cfg.edge);
key = "qcom,glinkpkt-ch-name";
glink_pkt_devp->open_cfg.name = of_get_property(node, key, NULL);
if (!glink_pkt_devp->open_cfg.name)
goto error;
GLINK_PKT_INFO("%s ch_name = %s\n", __func__,
glink_pkt_devp->open_cfg.name);
key = "qcom,glinkpkt-dev-name";
glink_pkt_devp->dev_name = of_get_property(node, key, NULL);
if (!glink_pkt_devp->dev_name)
goto error;
GLINK_PKT_INFO("%s dev_name = %s\n", __func__,
glink_pkt_devp->dev_name);
return 0;
error:
GLINK_PKT_ERR("%s: missing key: %s\n", __func__, key);
return -ENODEV;
}
/**
* glink_pkt_devicetree_init() - Initialize the add char device
*
* pdev: Pointer to device tree data.
*
* return: 0 on success, -ENODEV on failure.
*/
static int glink_pkt_devicetree_init(struct platform_device *pdev)
{
int ret;
int i = 0;
struct device_node *node;
struct glink_pkt_dev *glink_pkt_devp;
int subnode_num = 0;
for_each_child_of_node(pdev->dev.of_node, node)
++subnode_num;
if (!subnode_num) {
GLINK_PKT_ERR("%s subnode_num = %d\n", __func__, subnode_num);
return 0;
}
num_glink_pkt_ports = subnode_num;
ret = glink_pkt_alloc_chrdev_region();
if (ret) {
GLINK_PKT_ERR("%s: chrdev_region allocation failed ret:%i\n",
__func__, ret);
return ret;
}
for_each_child_of_node(pdev->dev.of_node, node) {
glink_pkt_devp = kzalloc(sizeof(*glink_pkt_devp),
GFP_KERNEL);
if (IS_ERR_OR_NULL(glink_pkt_devp)) {
GLINK_PKT_ERR("%s: allocation failed id:%d\n",
__func__, i);
ret = -ENOMEM;
goto error_destroy;
}
ret = parse_glinkpkt_devicetree(node, glink_pkt_devp);
if (ret) {
GLINK_PKT_ERR("%s: failed to parse devicetree %d\n",
__func__, i);
kfree(glink_pkt_devp);
goto error_destroy;
}
ret = glink_pkt_init_add_device(glink_pkt_devp, i);
if (ret < 0) {
GLINK_PKT_ERR("%s: add device failed idx:%d ret=%d\n",
__func__, i, ret);
kfree(glink_pkt_devp);
goto error_destroy;
}
i++;
}
GLINK_PKT_INFO("G-Link Packet Port Driver Initialized.\n");
return 0;
error_destroy:
glink_pkt_core_deinit();
return ret;
}
/**
* msm_glink_pkt_probe() - Probe a G-Link packet device
*
* pdev: Pointer to device tree data.
*
* return: 0 on success, standard Linux error codes on error.
*
* This function is called when the underlying device tree driver registers
* a platform device, mapped to a G-Link packet device.
*/
static int msm_glink_pkt_probe(struct platform_device *pdev)
{
int ret;
if (pdev) {
if (pdev->dev.of_node) {
GLINK_PKT_INFO("%s device tree implementation\n",
__func__);
ret = glink_pkt_devicetree_init(pdev);
if (ret)
GLINK_PKT_ERR("%s: device tree init failed\n",
__func__);
}
}
return 0;
}
static struct of_device_id msm_glink_pkt_match_table[] = {
{ .compatible = "qcom,glinkpkt" },
{},
};
static struct platform_driver msm_glink_pkt_driver = {
.probe = msm_glink_pkt_probe,
.driver = {
.name = MODULE_NAME,
.owner = THIS_MODULE,
.of_match_table = msm_glink_pkt_match_table,
},
};
/**
* glink_pkt_init() - Initialization function for this module
*
* returns: 0 on success, standard Linux error code otherwise.
*/
static int __init glink_pkt_init(void)
{
int ret;
INIT_LIST_HEAD(&glink_pkt_dev_list);
INIT_LIST_HEAD(&glink_pkt_driver_list);
ret = platform_driver_register(&msm_glink_pkt_driver);
if (ret) {
GLINK_PKT_ERR("%s: msm_glink_driver register failed %d\n",
__func__, ret);
return ret;
}
glink_pkt_ilctxt = ipc_log_context_create(GLINK_PKT_IPC_LOG_PAGE_CNT,
"glink_pkt", 0);
glink_pkt_wq = create_singlethread_workqueue("glink_pkt_wq");
if (!glink_pkt_wq) {
GLINK_PKT_ERR("%s: Error creating glink_pkt_wq\n", __func__);
return -ENOMEM;
}
return 0;
}
/**
* glink_pkt_cleanup() - Exit function for this module
*
* This function is used to cleanup the module during the exit.
*/
static void __exit glink_pkt_cleanup(void)
{
glink_pkt_core_deinit();
}
module_init(glink_pkt_init);
module_exit(glink_pkt_cleanup);
MODULE_DESCRIPTION("MSM G-Link Packet Port");
MODULE_LICENSE("GPL v2");
|