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
|
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kthread.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/netdevice.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/tcp.h>
#include <linux/delay.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include "is-config.h"
#define INADDR_RNDIS 0xc0a82a81 /* 192.168.42.129 */
#define INADDR_ADB INADDR_LOOPBACK
/*
* callback sequence
* connecting: on_accept -> on_connect -> on_open
* disconnecting: on_close
*/
#define ISC_CONNECTED 1
struct is_sock_con {
struct list_head list;
struct socket *sock;
unsigned long flags;
struct is_sock_server *server;
void *priv;
/* IS socket connection ops */
int (*on_open)(struct is_sock_con *con);
void (*on_close)(struct is_sock_con *con);
};
#define IS_SOCK_SERVER_NAME_LEN 32
struct is_sock_server {
char name[IS_SOCK_SERVER_NAME_LEN];
int type;
unsigned int ip;
int port;
bool blocking_accept;
void *priv;
struct socket *sock;
struct task_struct *thread;
atomic_t num_of_cons;
struct list_head cons;
spinlock_t slock;
/* IS socket server ops */
int (*on_accept)(struct socket *newsock);
int (*on_connect)(struct is_sock_con *con);
};
/* IS socket functions */
static int is_sock_create(struct socket **sockp, int type, unsigned int ip, int port)
{
struct sockaddr_in addr;
struct socket *sock;
int opt;
int ret;
ret = sock_create_kern(&init_net, PF_INET, type, 0, &sock);
*sockp = sock;
if (ret) {
err("failed to create socket: %d", ret);
return ret;
}
/* TODO: SO_KEEPALIVE, SO_LINGER */
opt = 1;
ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(char *)&opt, sizeof(opt));
if (ret) {
err("failed to set SO_REUSEADDR for socket: %d", ret);
goto err_so_reuseaddr;
}
if (type == SOCK_STREAM) {
opt = 1;
ret = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
(char *)&opt, sizeof(opt));
if (ret) {
err("failed to set TCP_NODELAY for socket: %d", ret);
goto err_tcp_nodelay;
}
opt = 0;
ret = kernel_setsockopt(sock, SOL_TCP, TCP_CORK,
(char *)&opt, sizeof(opt));
if (ret) {
err("failed to unset TCP_CORK for socket: %d", ret);
goto err_tcp_cork;
}
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = (ip) ? htonl(ip) : htonl(INADDR_ANY);
ret = kernel_bind(sock, (struct sockaddr *)&addr,
sizeof(addr));
if (ret == -EADDRINUSE) {
err("port %d already in use", port);
goto err_addr_used;
}
if (ret) {
err("error trying to bind to port %d: %d",
port, ret);
goto err_bind;
}
return 0;
err_bind:
err_addr_used:
err_so_reuseaddr:
err_tcp_nodelay:
err_tcp_cork:
sock_release(sock);
return ret;
}
static int is_sock_setbuf_size(struct socket *sock, int txsize, int rxsize)
{
int opt;
int ret;
if (txsize) {
opt = txsize;
ret = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
(char *)&opt, sizeof(opt));
if (ret) {
err("failed to set send buffer size: %d", ret);
return ret;
}
}
if (rxsize) {
opt = rxsize;
ret = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
(char *)&opt, sizeof(opt));
if (ret) {
err("failed to set receive buffer size: %d", ret);
return ret;
}
}
return 0;
}
static int is_sock_create_listen(struct socket **sockp, int type, unsigned int ip, int port)
{
int ret;
ret = is_sock_create(sockp, type, ip, port);
if (ret) {
err("can't create socket");
return ret;
}
ret = kernel_listen(*sockp, 5);
if (ret) {
err("failed to listen socket: %d", ret);
sock_release(*sockp);
}
return 0;
}
static int is_sock_send_stream(struct socket *sock, void *buf, int len, int timeout)
{
int ret;
long ticks = timeout * HZ;
unsigned long then;
struct timeval tv;
while (1) {
struct kvec iov = {
.iov_base = buf,
.iov_len = len,
};
struct msghdr msg = {
.msg_flags = (timeout == 0) ? MSG_DONTWAIT : 0,
};
if (timeout) {
tv = (struct timeval) {
.tv_sec = ticks / HZ,
.tv_usec = ((ticks % HZ) * 1000000) / HZ
};
ret = kernel_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,
(char *)&tv, sizeof(tv));
if (ret) {
err("failed to set SO_SNDTIMEO(%ld.%06d) for socket: %d",
(long)tv.tv_sec, (int)tv.tv_usec, ret);
return ret;
}
}
then = jiffies;
ret = kernel_sendmsg(sock, &msg, &iov, 1, len);
if (timeout)
ticks -= jiffies - then;
if (ret == len)
return 0;
if (ret < 0) {
if (ret == -EAGAIN)
continue;
else
return ret;
}
if (ret == 0) {
err("Software caused connection abort");
return -ECONNABORTED;
}
if (timeout && (ticks <= 0))
return -EAGAIN;
buf = ((char *)buf) + ret;
len -= ret;
}
return 0;
}
static int is_sock_recv_stream(struct socket *sock, void *buf, int len, int timeout)
{
int ret;
long ticks = timeout * HZ;
unsigned long then;
struct timeval tv;
while (1) {
struct kvec iov = {
.iov_base = buf,
.iov_len = len,
};
struct msghdr msg = {
.msg_flags = 0,
};
if (timeout) {
tv = (struct timeval) {
.tv_sec = ticks / HZ,
.tv_usec = ((ticks % HZ) * 1000000) / HZ
};
ret = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
(char *)&tv, sizeof(tv));
if (ret) {
err("failed to set SO_RCVTIMEO(%ld.%06d) for socket: %d",
(long)tv.tv_sec, (int)tv.tv_usec, ret);
return ret;
}
}
then = jiffies;
ret = kernel_recvmsg(sock, &msg, &iov, 1, len, 0);
if (timeout)
ticks -= jiffies - then;
if (ret < 0) {
if (ret == -EAGAIN)
continue;
else
return ret;
}
if (ret == 0) {
err("Connection reset by peer");
return -ECONNRESET;
}
buf = ((char *)buf) + ret;
len -= ret;
if (len == 0)
return 0;
if (timeout && (ticks <= 0))
return -ETIMEDOUT;
}
}
/* IS socket connection functions */
static int is_sock_con_open(struct is_sock_con **newconp, struct socket *newsock,
struct is_sock_server *svr)
{
struct is_sock_con *newcon;
int ret;
newcon = kzalloc(sizeof(*newcon), GFP_KERNEL);
if (!newcon) {
err("failed to alloc new connection for %s", svr->name);
return -ENOMEM;
}
newcon->sock = newsock;
newcon->server = svr;
if (svr->on_connect) {
ret = svr->on_connect(newcon);
if (ret) {
err("connect callback error: %d", ret);
kfree(newcon);
return ret;
}
}
spin_lock(&svr->slock);
atomic_inc(&svr->num_of_cons);
list_add_tail(&newcon->list, &svr->cons);
spin_unlock(&svr->slock);
if (newcon->on_open) {
ret = newcon->on_open(newcon);
if (ret) {
err("open callback error: %d", ret);
spin_lock(&svr->slock);
atomic_dec(&svr->num_of_cons);
list_del(&newcon->list);
spin_unlock(&svr->slock);
kfree(newcon);
return ret;
}
}
set_bit(ISC_CONNECTED, &newcon->flags);
*newconp = newcon;
return 0;
}
static void is_sock_con_close(struct is_sock_con *con)
{
if (con && test_and_clear_bit(ISC_CONNECTED, &con->flags)) {
if (con->on_close)
con->on_close(con);
kernel_sock_shutdown(con->sock, SHUT_RDWR);
sock_release(con->sock);
spin_lock(&con->server->slock);
atomic_dec(&con->server->num_of_cons);
list_del(&con->list);
spin_unlock(&con->server->slock);
kfree(con);
}
}
static int is_sock_con_send_stream(struct is_sock_con *con, void *buf, int len, int timeout)
{
int ret;
if (!test_bit(ISC_CONNECTED, &con->flags))
return -ENOTCONN;
ret = is_sock_send_stream(con->sock, buf, len, timeout);
if (ret) {
err("send stream error: %d", ret);
if (ret == -EWOULDBLOCK || ret == -EAGAIN)
return ret;
is_sock_con_close(con);
return ret;
}
return ret;
}
static int is_sock_con_recv_stream(struct is_sock_con *con, void *buf, int len, int timeout)
{
int ret;
if (!test_bit(ISC_CONNECTED, &con->flags))
return -ENOTCONN;
ret = is_sock_recv_stream(con->sock, buf, len, timeout);
if (ret) {
err("recv stream error: %d", ret);
if (ret == -EWOULDBLOCK || ret == -ETIMEDOUT)
return ret;
is_sock_con_close(con);
return ret;
}
return ret;
}
/* IS socket server functions */
static int is_sock_server_accept(struct is_sock_server *svr)
{
struct socket *sock = svr->sock;
struct socket *newsock;
struct is_sock_con *newcon;
int ret;
ret = kernel_accept(sock, &newsock, svr->blocking_accept ? 0 : O_NONBLOCK);
if (ret < 0)
return ret;
if (svr->on_accept) {
ret = svr->on_accept(newsock);
if (ret) {
err("accept callback error: %d", ret);
sock_release(newsock);
return ret;
}
}
ret = is_sock_con_open(&newcon, newsock, svr);
if (ret) {
err("failed to open new connection for %s", svr->name);
sock_release(newsock);
return ret;
}
return ret;
}
static int is_sock_server_thread(void *data)
{
struct is_sock_server *svr = (struct is_sock_server *)data;
int ret;
long timeo = msecs_to_jiffies(200);
ret = is_sock_create_listen(&svr->sock, svr->type, svr->ip, svr->port);
if (ret)
return ret;
while (!kthread_should_stop()) {
ret = is_sock_server_accept(svr);
if (ret == -EAGAIN) {
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(timeo);
set_current_state(TASK_RUNNING);
} else if (ret) {
err("server accept error: %d", ret);
}
}
kernel_sock_shutdown(svr->sock, SHUT_RDWR);
sock_release(svr->sock);
return 0;
}
static int is_sock_server_start(struct is_sock_server *svr)
{
int ret = 0;
svr->thread = kthread_run(is_sock_server_thread,
(void *)svr, "sock-%s", svr->name);
if (IS_ERR(svr->thread)) {
err("failed to start socket server thread for %s", svr->name);
ret = PTR_ERR(svr->thread);
svr->thread = NULL;
goto err_run_server_thread;
}
atomic_set(&svr->num_of_cons, 0);
INIT_LIST_HEAD(&svr->cons);
spin_lock_init(&svr->slock);
err_run_server_thread:
return ret;
}
static int is_sock_server_stop(struct is_sock_server *svr)
{
struct is_sock_con *con, *temp;
int ret;
if (!svr)
return -EINVAL;
if (svr->thread)
ret = kthread_stop(svr->thread);
svr->thread = NULL;
list_for_each_entry_safe(con, temp, &svr->cons, list)
is_sock_con_close(con);
return 0;
}
/* for tuning interface */
#define TUNING_PORT 5672
#define TUNING_BUF_SIZE SZ_512K
#define TUNING_CONTROL 0
#define TUNING_JSON 1
#define TUNING_IMAGE 2
#define TUNING_MAX 3
struct is_sock_server *tuning_svr;
struct mutex ts_mlock;
struct is_tuning_con {
char read_buf[TUNING_BUF_SIZE];
char write_buf[TUNING_BUF_SIZE];
struct file *filp;
};
struct is_tuning_svr {
atomic_t num_of_filp;
struct file *filp[TUNING_MAX];
};
int is_tuning_on_open(struct is_sock_con *con)
{
struct is_tuning_con *itc;
struct is_tuning_svr *its = (struct is_tuning_svr *)tuning_svr->priv;
unsigned int cur_con_idx;
int ret;
itc = vzalloc(sizeof(*itc));
if (!itc) {
err("failed to alloc connection private data");
return -ENOMEM;
}
con->priv = (void *)itc;
ret = is_sock_setbuf_size(con->sock, TUNING_BUF_SIZE, TUNING_BUF_SIZE);
if (ret) {
err("failed to set buf size for %p to %d", con, TUNING_BUF_SIZE);
goto err_set_buf_size;
}
/* a number of connection includes new connecting socket */
cur_con_idx = atomic_read(&con->server->num_of_cons) - 1;
if (cur_con_idx >= TUNING_MAX) {
err("a number of con. is exceed: %d", TUNING_MAX);
ret = -EMFILE;
goto err_too_many_con;
}
if (its->filp[cur_con_idx]) {
itc->filp = its->filp[cur_con_idx];
itc->filp->private_data = con;
} else {
err("there is no valid opened file for %d", cur_con_idx);
ret = -ENOENT;
goto err_invalid_filp;
}
return 0;
err_invalid_filp:
err_too_many_con:
err_set_buf_size:
vfree(itc);
return ret;
}
void is_tuning_on_close(struct is_sock_con *con)
{
struct is_tuning_con *itc;
if (con) {
itc = (struct is_tuning_con *)con->priv;
itc->filp->private_data = NULL;
vfree(itc);
}
}
int is_tuning_on_accept(struct socket *newsock)
{
return 0;
}
int is_tuning_on_connect(struct is_sock_con *newcon)
{
newcon->on_open = is_tuning_on_open;
newcon->on_close = is_tuning_on_close;
return 0;
}
int is_tuning_start(void)
{
struct is_tuning_svr *its;
int ret;
tuning_svr = kzalloc(sizeof(*tuning_svr), GFP_KERNEL);
if (!tuning_svr) {
err("failed to alloc tuning server");
return -ENOMEM;
}
strncpy(tuning_svr->name, "tuning", sizeof(tuning_svr->name) - 1);
tuning_svr->type = SOCK_STREAM;
tuning_svr->ip = INADDR_ADB;
tuning_svr->port = TUNING_PORT;
tuning_svr->blocking_accept = false;
tuning_svr->on_accept = is_tuning_on_accept;
tuning_svr->on_connect = is_tuning_on_connect;
its = kzalloc(sizeof(*its), GFP_KERNEL);
if (!its) {
err("failed to alloc tuning server private data");
kfree(tuning_svr);
return -ENOMEM;
}
tuning_svr->priv = (void *)(its);
atomic_set(&its->num_of_filp, 0);
ret = is_sock_server_start(tuning_svr);
if (ret) {
err("failed to start socket server for %s", tuning_svr->name);
kfree(its);
kfree(tuning_svr);
tuning_svr = NULL;
}
return ret;
}
void is_tuning_stop(void *data)
{
if (tuning_svr) {
is_sock_server_stop(tuning_svr);
kfree(tuning_svr->priv);
kfree(tuning_svr);
tuning_svr = NULL;
}
}
static ssize_t is_tuning_read(struct file *filp, char __user *buf,
size_t len, loff_t *ppos)
{
struct is_sock_con *con = (struct is_sock_con *)filp->private_data;
struct is_sock_con *other_con, *temp;
struct is_tuning_con *itc;
struct is_sock_server *server;
int ret;
if (!con)
return -EINVAL;
if (!test_bit(ISC_CONNECTED, &con->flags))
return -ENOTCONN;
itc = (struct is_tuning_con *)con->priv;
server = con->server;
if (len > TUNING_BUF_SIZE)
len = TUNING_BUF_SIZE;
ret = is_sock_con_recv_stream(con, itc->read_buf, len, 0);
if (ret) {
/*
* We can realize that the client was diconnected with this error.
* Any connected sockets which are waiting for client to receivce
* messages could get this error due to client's disconnection.
* So, we need to reset all connected sockets to restart.
*/
if (ret == -ECONNRESET) {
list_for_each_entry_safe(other_con, temp,
&server->cons, list)
is_sock_con_close(other_con);
}
return ret;
}
ret = copy_to_user(buf, itc->read_buf, len);
if (ret) {
err("failed to copy to user: %d", ret);
return -EPERM;
}
return len;
}
static ssize_t is_tuning_write(struct file *filp, const char __user *buf,
size_t len, loff_t *ppos)
{
struct is_sock_con *con = (struct is_sock_con *)filp->private_data;
struct is_tuning_con *itc;
int ret;
if (!con)
return -EINVAL;
if (!test_bit(ISC_CONNECTED, &con->flags))
return -ENOTCONN;
itc = (struct is_tuning_con *)con->priv;
if (len > TUNING_BUF_SIZE)
len = TUNING_BUF_SIZE;
ret = copy_from_user(itc->write_buf, buf, len);
if (ret) {
err("failed to copy from user: %d", ret);
return -EPERM;
}
ret = is_sock_con_send_stream(con, itc->write_buf, len, 0);
if (ret)
return ret;
return len;
}
static int is_tuning_open(struct inode *inode, struct file *filp)
{
struct is_tuning_svr *its;
unsigned int num_of_filp;
int ret;
mutex_lock(&ts_mlock);
if (!tuning_svr) {
ret = is_tuning_start();
if (ret) {
err("failed to start tuning server: %d", ret);
mutex_unlock(&ts_mlock);
return ret;
}
}
its = (struct is_tuning_svr *)tuning_svr->priv;
num_of_filp = atomic_read(&its->num_of_filp);
if (num_of_filp >= TUNING_MAX) {
mutex_unlock(&ts_mlock);
return -EINVAL;
}
its->filp[num_of_filp] = filp;
atomic_inc(&its->num_of_filp);
filp->private_data = NULL;
mutex_unlock(&ts_mlock);
return 0;
}
static int is_tuning_close(struct inode *inodep, struct file *filp)
{
struct is_sock_con *con = (struct is_sock_con *)filp->private_data;
struct is_tuning_svr *its = (struct is_tuning_svr *)tuning_svr->priv;
mutex_lock(&ts_mlock);
if (con)
is_sock_con_close(con);
if (!atomic_dec_return(&its->num_of_filp))
is_tuning_stop(NULL);
mutex_unlock(&ts_mlock);
return 0;
}
static const struct file_operations is_tuning_fops = {
.owner = THIS_MODULE,
.read = is_tuning_read,
.write = is_tuning_write,
.open = is_tuning_open,
.release = is_tuning_close,
.llseek = no_llseek,
};
struct miscdevice is_tuning_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "is_tuning_socket",
.fops = &is_tuning_fops,
};
static int __init is_sock_init(void)
{
int ret;
/* tuning misc device */
ret = misc_register(&is_tuning_device);
if (ret)
err("failed to register is_tuning_socket devices");
mutex_init(&ts_mlock);
return ret;
}
static void __exit is_sock_exit(void)
{
/* tuning misc device */
misc_deregister(&is_tuning_device);
}
/* init and cleanup functions */
module_init(is_sock_init);
module_exit(is_sock_exit);
MODULE_AUTHOR("Jeongtae Park <jtp.park@samsung.com>");
MODULE_DESCRIPTION("socket layer for image subsystem in Exynos");
MODULE_LICENSE("GPL");
|