summaryrefslogtreecommitdiffstats
path: root/master/motors
blob: 9a9851d1d62a19bb0f1a86e449766fa2044b051c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
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
diff --git a/feeds/packages/multimedia/mjpg-streamer/Makefile b/feeds/packages/multimedia/mjpg-streamer/Makefile
index e1e0e20..ff98b4d 100644
--- a/feeds/packages/multimedia/mjpg-streamer/Makefile
+++ b/feeds/packages/multimedia/mjpg-streamer/Makefile
@@ -88,6 +88,7 @@ ifeq ($(CONFIG_MJPG_STREAMER_INPUT_FILE),y)
 endif
 ifeq ($(CONFIG_MJPG_STREAMER_INPUT_UVC),y)
 	$(CP) $(PKG_BUILD_DIR)/input_uvc.so $(1)/usr/lib
+	$(CP) $(PKG_BUILD_DIR)/input_motors.so $(1)/usr/lib
 endif
 ifeq ($(CONFIG_MJPG_STREAMER_INPUT_TESTPICTURE),y)
 	$(CP) $(PKG_BUILD_DIR)/input_testpicture.so $(1)/usr/lib
diff --git a/feeds/packages/multimedia/mjpg-streamer/files/mjpg-streamer.init b/feeds/packages/multimedia/mjpg-streamer/files/mjpg-streamer.init
index 06630d4..ea83303 100644
--- a/feeds/packages/multimedia/mjpg-streamer/files/mjpg-streamer.init
+++ b/feeds/packages/multimedia/mjpg-streamer/files/mjpg-streamer.init
@@ -83,7 +83,7 @@ start_instance() {
 	fi
 
 	procd_open_instance
-	procd_set_param command "$PROG" --input "$input_arg" --output "$output_arg"
+	procd_set_param command "$PROG" --input "$input_arg" --input "input_motors.so" --output "$output_arg"
 	procd_close_instance
 }
 
diff --git a/feeds/packages/multimedia/mjpg-streamer/patches/999-motors.patch b/feeds/packages/multimedia/mjpg-streamer/patches/999-motors.patch
new file mode 100644
index 0000000..0ae4b63
--- /dev/null
+++ b/feeds/packages/multimedia/mjpg-streamer/patches/999-motors.patch
@@ -0,0 +1,906 @@
+Index: bar/Makefile
+===================================================================
+--- bar.orig/Makefile
++++ bar/Makefile
+@@ -33,6 +33,7 @@ APP_BINARY = mjpg_streamer
+ 
+ # define the names and targets of the plugins
+ PLUGINS = input_uvc.so
++PLUGINS += input_motors.so
+ PLUGINS += output_file.so
+ #PLUGINS += output_udp.so
+ PLUGINS += output_http.so
+@@ -71,6 +72,9 @@ input_testpicture.so: mjpg_streamer.h ut
+ 	make -C plugins/input_testpicture all
+ 	cp plugins/input_testpicture/input_testpicture.so .
+ 
++input_motors.so: mjpg_streamer.h utils.h
++	make -C plugins/input_motors 
++	cp plugins/input_motors/input_motors.so .
+ 
+ ifeq ($(USE_LIBV4L2),true)
+ input_uvc.so: mjpg_streamer.h utils.h
+Index: bar/plugins/input_motors/input_motors.c
+===================================================================
+--- /dev/null
++++ bar/plugins/input_motors/input_motors.c
+@@ -0,0 +1,708 @@
++/*******************************************************************************
++# Linux-UVC streaming input-plugin for MJPG-streamer                           #
++#                                                                              #
++# This package work with the Logitech UVC based webcams with the mjpeg feature #
++#                                                                              #
++# Copyright (C) 2005 2006 Laurent Pinchart &&  Michel Xhaard                   #
++#                    2007 Lucas van Staden                                     #
++#                    2007 Tom Stöveken                                         #
++#                                                                              #
++# This program is free software; you can redistribute it and/or modify         #
++# it under the terms of the GNU General Public License as published by         #
++# the Free Software Foundation; either version 2 of the License, or            #
++# (at your option) any later version.                                          #
++#                                                                              #
++# 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.                                 #
++#                                                                              #
++# You should have received a copy of the GNU General Public License            #
++# along with this program; if not, write to the Free Software                  #
++# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA    #
++#                                                                              #
++*******************************************************************************/
++
++#include <stdio.h>
++#include <stdlib.h>
++#include <unistd.h>
++#include <string.h>
++#include <fcntl.h>
++#include <sys/ioctl.h>
++#include <errno.h>
++#include <signal.h>
++#include <sys/socket.h>
++#include <arpa/inet.h>
++#include <sys/types.h>
++#include <sys/stat.h>
++#include <getopt.h>
++#include <pthread.h>
++#include <syslog.h>
++
++#include "../../utils.h"
++#include "../../mjpg_streamer.h"
++
++#define INPUT_PLUGIN_NAME "KKMoon motors control"
++
++#define MIN_RES 5
++
++#if 0
++#undef DBG
++#define DBG(a...) do { fprintf(stderr,a); } while (0)
++#endif
++
++
++#define V_MIN -175
++#define V_MAX 455
++
++#define H_MIN -1040
++#define H_MAX 1030
++
++#define H_STEPS (1+(H_MAX)-(H_MIN))
++#define V_STEPS (1+(V_MAX)-(V_MIN))
++
++
++
++typedef struct input_motors
++{
++  int fd;
++  char *device;
++  int min_res;
++} input_motors;
++
++/* private functions and variables to this plugin */
++static pthread_t worker;
++static pthread_mutex_t motor_mutex = PTHREAD_MUTEX_INITIALIZER;
++static pthread_mutex_t worker_mutex = PTHREAD_MUTEX_INITIALIZER;
++static pthread_cond_t worker_cond = PTHREAD_COND_INITIALIZER;
++static input_motors *this;
++static globals *pglobal;
++static int plugin_number;
++
++
++static volatile int worker_pan, worker_tilt;
++
++static void
++motor_cmd (int fd, char m, size_t n)
++{
++  static char buf[8192];
++  size_t l;
++
++  DBG ("motor: %c for %d\n", m, (int) n);
++
++  while (n)
++    {
++      l = (n > sizeof (buf)) ? sizeof (buf) : n;
++      pthread_mutex_lock (&motor_mutex);
++      memset (buf, m, l);
++      write (fd, buf, l);
++      pthread_mutex_unlock (&motor_mutex);
++      n -= l;
++    }
++}
++
++static int pan_valid, pan;
++
++static void
++pan_move (int fd, int m)
++{
++  DBG ("pan_move: %d\n", m);
++  if (m < 0)
++    motor_cmd (fd, 'L', -m);
++  else
++    motor_cmd (fd, 'R', m);
++}
++
++
++static void
++pan_abs (int fd, int p)
++{
++  DBG ("pan_abs: pan_valid=%d pan=%d requested=%d\n", pan_valid, pan, p);
++
++  if (!pan_valid)
++    {
++      pan_move (fd, -H_STEPS);
++      pan_valid = 1;
++      pan = H_MIN;
++    }
++
++  if (p < H_MIN)
++    p = H_MIN;
++  if (p > H_MAX)
++    p = H_MAX;
++
++  pan_move (fd, p - pan);
++
++  pan = p;
++}
++
++static void
++pan_rel (int fd, int m)
++{
++  DBG ("pan_rel: pan_valid=%d pan=%d requested=%d\n", pan_valid, pan, m);
++  if (pan_valid)
++    {
++      pan_abs (fd, pan + m);
++    }
++  else
++    {
++      pan_move (fd, m);
++    }
++}
++
++
++
++static int tilt_valid, tilt;
++
++static void
++tilt_move (int fd, int m)
++{
++  DBG ("tilt_move: %d\n", m);
++  if (m < 0)
++    motor_cmd (fd, 'D', -m);
++  else
++    motor_cmd (fd, 'U', m);
++}
++
++
++static void
++tilt_abs (int fd, int p)
++{
++  DBG ("tilt_abs tilt_valid=%d tilt=%d requested=%d\n", tilt_valid, tilt, p);
++  if (!tilt_valid)
++    {
++      tilt_move (fd, -V_STEPS);
++      tilt_valid = 1;
++      tilt = V_MIN;
++    }
++
++  if (p < V_MIN)
++    p = V_MIN;
++  if (p > V_MAX)
++    p = V_MAX;
++
++  tilt_move (fd, p - tilt);
++
++  tilt = p;
++}
++
++static void
++tilt_rel (int fd, int m)
++{
++  DBG ("tilt_rel tilt_valid=%d tilt=%d requested=%d\n", tilt_valid, tilt, m);
++  if (tilt_valid)
++    {
++      tilt_abs (fd, tilt + m);
++    }
++  else
++    {
++      tilt_move (fd, m);
++    }
++}
++
++
++
++
++static void
++help (void)
++{
++  fprintf (stderr,
++           " ---------------------------------------------------------------\n"
++           " Help for input plugin..: " INPUT_PLUGIN_NAME "\n"
++           " ---------------------------------------------------------------\n"
++           " The following parameters can be passed to this plugin:\n\n"
++           " [-d | --device ]...: video device to open (your camera)\n"
++           " [-m | --min ]......: set the minimum step size in phases (defalt 5)\n");
++}
++
++
++
++
++static void
++worker_cleanup (void *arg)
++{
++  static unsigned char first_run = 1;
++
++  if (!first_run)
++    {
++      DBG ("already cleaned up ressources\n");
++      return;
++    }
++
++  first_run = 0;
++}
++
++
++
++
++static void *
++worker_thread (void *arg)
++{
++  struct timespec ts = { 0 };
++  int err;
++
++
++  /* set cleanup handler to cleanup allocated ressources */
++  pthread_cleanup_push (worker_cleanup, NULL);
++
++  while (!pglobal->stop)
++    {
++
++      clock_gettime (CLOCK_MONOTONIC, &ts);
++
++      ts.tv_sec += 1;
++
++      pthread_mutex_lock (&worker_mutex);
++      pthread_cond_timedwait (&worker_cond, &worker_mutex, &ts);
++      pthread_mutex_unlock (&worker_mutex);
++      while (worker_pan || worker_tilt)
++        {
++          if (worker_pan)
++            pan_rel (this->fd, worker_pan);
++          if (worker_tilt)
++            tilt_rel (this->fd, worker_tilt);
++        }
++
++      pthread_cond_broadcast (&pglobal->in[plugin_number].db_update); //this keeps the output stream alive
++      DBG ("spin loop\n");
++    }
++
++  DBG ("leaving input thread, calling cleanup function now\n");
++  pthread_cleanup_pop (1);
++
++  return NULL;
++}
++
++
++void
++add_control (int plugin_number, control c)
++{
++  if (!pglobal->in[plugin_number].in_parameters)
++    {
++      pglobal->in[plugin_number].in_parameters =
++        (control *) calloc (1, sizeof (control));
++    }
++  else
++    {
++      pglobal->in[plugin_number].in_parameters =
++        (control *) realloc (pglobal->in[plugin_number].in_parameters,
++                             (pglobal->in[plugin_number].parametercount +
++                              1) * sizeof (control));
++    }
++
++  if (pglobal->in[plugin_number].in_parameters == NULL)
++    {
++      DBG ("Calloc failed\n");
++      return;
++    }
++
++  pglobal->in[plugin_number].in_parameters[pglobal->in[plugin_number].
++                                           parametercount] = c;
++
++  pglobal->in[plugin_number].parametercount++;
++
++}
++
++
++
++static void
++add_btn (int plugin_number, unsigned int id, char *name)
++{
++
++  control btn;
++
++  btn.group = IN_CMD_GENERIC;
++  btn.menuitems = NULL;
++  btn.value = 0.0;
++  btn.class_id = 0;
++
++  btn.ctrl.id = id;
++  btn.ctrl.type = V4L2_CTRL_TYPE_BUTTON;
++  strcpy ((char *) btn.ctrl.name, name);
++  btn.ctrl.minimum = 0;
++  btn.ctrl.maximum = 1;
++  btn.ctrl.step = 1;
++  btn.ctrl.default_value = 0;
++  btn.ctrl.flags = V4L2_CTRL_FLAG_VOLATILE;
++
++  add_control (plugin_number, btn);
++
++}
++
++static void
++add_bool (int plugin_number, unsigned int id, char *name)
++{
++
++  control btn;
++
++  btn.group = IN_CMD_GENERIC;
++  btn.menuitems = NULL;
++  btn.value = 0.0;
++  btn.class_id = 0;
++
++  btn.ctrl.id = id;
++  btn.ctrl.type = V4L2_CTRL_TYPE_BOOLEAN;
++  strcpy ((char *) btn.ctrl.name, name);
++  btn.ctrl.minimum = 0;
++  btn.ctrl.maximum = 1;
++  btn.ctrl.step = 1;
++  btn.ctrl.default_value = 0;
++  btn.ctrl.flags = V4L2_CTRL_FLAG_VOLATILE;
++
++  add_control (plugin_number, btn);
++
++}
++
++
++static void
++add_abs (int plugin_number, unsigned int id, char *name, int max)
++{
++
++  control abs;
++
++  abs.group = IN_CMD_GENERIC;
++  abs.menuitems = NULL;
++  abs.value = 0xffffff;
++  abs.class_id = 0;
++
++  abs.ctrl.id = id;
++  abs.ctrl.type = V4L2_CTRL_TYPE_INTEGER;
++  strcpy ((char *) abs.ctrl.name, name);
++  abs.ctrl.minimum = -max / 2;
++  abs.ctrl.maximum = max / 2;
++  abs.ctrl.step = 1;
++  abs.ctrl.default_value = 0;
++  abs.ctrl.flags = V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_SLIDER;
++
++  add_control (plugin_number, abs);
++
++}
++
++
++static void
++add_controls (int plugin_number)
++{
++  add_btn (plugin_number, IN_CMD_RESET_PAN_TILT, "Reset");
++#ifdef USE_BUTTONS
++  add_btn (plugin_number, IN_CMD_PAN_MINUS, "Pan-");
++  add_btn (plugin_number, IN_CMD_PAN_PLUS, "Pan+");
++  add_btn (plugin_number, IN_CMD_TILT_MINUS, "Tilt-");
++  add_btn (plugin_number, IN_CMD_TILT_PLUS, "Tilt+");
++#else
++  add_bool (plugin_number, IN_CMD_PAN_MINUS, "Pan-");
++  add_bool (plugin_number, IN_CMD_PAN_PLUS, "Pan+");
++  add_bool (plugin_number, IN_CMD_TILT_MINUS, "Tilt-");
++  add_bool (plugin_number, IN_CMD_TILT_PLUS, "Tilt+");
++#endif
++  add_abs (plugin_number, IN_CMD_PAN_SET, "Pan", H_STEPS);
++  add_abs (plugin_number, IN_CMD_TILT_SET, "Tilt", V_STEPS);
++
++}
++
++
++
++/*** plugin interface functions ***/
++
++
++int
++input_init (input_parameter * param, int id)
++{
++  int i;
++  char *dev = "/dev/motors";
++
++  plugin_number = id;
++
++  if (pthread_mutex_init (&motor_mutex, NULL) != 0)
++    {
++      IPRINT ("could not initialize mutex variable\n");
++      exit (EXIT_FAILURE);
++    }
++
++  if (pthread_mutex_init (&worker_mutex, NULL) != 0)
++    {
++      IPRINT ("could not initialize mutex variable\n");
++      exit (EXIT_FAILURE);
++    }
++
++
++  {
++    pthread_condattr_t attr;
++    pthread_condattr_init (&attr);
++    pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
++
++    if (pthread_cond_init (&worker_cond, &attr) != 0)
++      {
++        IPRINT ("could not initialize cond variable\n");
++        exit (EXIT_FAILURE);
++      }
++
++  }
++
++
++
++  /* keep a pointer to the global variables */
++  pglobal = param->global;
++
++  /* allocate webcam datastructure */
++  this = malloc (sizeof (struct input_motors));
++  if (this == NULL)
++    {
++      IPRINT ("not enough memory for videoIn\n");
++      exit (EXIT_FAILURE);
++    }
++  memset (this, 0, sizeof (struct input_motors));
++  this->min_res = MIN_RES;
++
++  /* display the parsed values */
++  IPRINT ("Using motors device.: %s\n", dev);
++
++  param->argv[0] = INPUT_PLUGIN_NAME;
++  asprintf (&param->global->in[id].name, "%s", INPUT_PLUGIN_NAME);
++
++  /* show all parameters for DBG purposes */
++  for (i = 0; i < param->argc; i++)
++    {
++      DBG ("argv[%d]=%s\n", i, param->argv[i]);
++    }
++
++  /* parse the parameters */
++  reset_getopt ();
++  while (1)
++    {
++      int option_index = 0, c = 0;
++      static struct option long_options[] = {
++        {"h", no_argument, 0, 0},
++        {"help", no_argument, 0, 0},
++        {"d", required_argument, 0, 0},
++        {"device", required_argument, 0, 0},
++        {"m", required_argument, 0, 0},
++        {"min", required_argument, 0, 0},
++        {0, 0, 0, 0}
++      };
++
++      /* parsing all parameters according to the list above is sufficent */
++      c =
++        getopt_long_only (param->argc, param->argv, "", long_options,
++                          &option_index);
++
++      /* no more options to parse */
++      if (c == -1)
++        break;
++
++      /* unrecognized option */
++      if (c == '?')
++        {
++          help ();
++          return 1;
++        }
++
++      /* dispatch the given options */
++      switch (option_index)
++        {
++          /* h, help */
++        case 0:
++        case 1:
++          DBG ("case 0,1\n");
++          help ();
++          return 1;
++          break;
++
++          /* d, device */
++        case 2:
++        case 3:
++          DBG ("case 2,3\n");
++          dev = strdup (optarg);
++          break;
++
++        case 4:
++        case 5:
++          DBG ("case 4,5\n");
++          this->min_res = atoi (optarg);
++          break;
++
++        default:
++          DBG ("default case\n");
++          help ();
++          return 1;
++        }
++    }
++
++  asprintf (&this->device, "%s", dev);
++
++  if ((this->fd = open (this->device, O_RDWR)) == -1)
++    {
++      perror ("ERROR opening motor interface");
++      return -1;
++    }
++
++
++  add_controls (plugin_number);
++
++  return 0;
++}
++
++/******************************************************************************
++Description.: Stops the execution of worker thread
++Input Value.: -
++Return Value: always 0
++******************************************************************************/
++int
++input_stop (void)
++{
++  DBG ("will cancel input thread\n");
++  pthread_cancel (worker);
++
++  return 0;
++}
++
++/******************************************************************************
++Description.: spins of a worker thread
++Input Value.: -
++Return Value: always 0
++******************************************************************************/
++int
++input_run (void)
++{
++
++  pthread_create (&worker, 0, worker_thread, NULL);
++  pthread_detach (worker);
++
++  return 0;
++}
++
++/******************************************************************************
++Description.: process commands, allows to set certain runtime configurations
++              and settings like pan/tilt, colors, saturation etc.
++Input Value.: * cmd specifies the command, a complete list is maintained in
++                the file "input.h"
++              * value is used for commands that make use of a parameter.
++Return Value: depends in the command, for most cases 0 means no errors and
++              -1 signals an error. This is just rule of thumb, not more!
++******************************************************************************/
++int
++input_cmd_old (in_cmd_type cmd, int value)
++{
++  int res = 0;
++
++
++  switch (cmd)
++    {
++    case IN_CMD_HELLO:
++      fprintf (stderr, "Hello from input plugin\n");
++      break;
++
++    case IN_CMD_RESET_PAN_TILT:
++    case IN_CMD_RESET_PAN_TILT_NO_MUTEX:
++      DBG ("about to set pan/tilt to default position\n");
++
++      pan_abs (this->fd, 0);
++      tilt_abs (this->fd, 0);
++
++      break;
++
++    case IN_CMD_PAN_SET:
++      DBG ("set pan to %d steps\n", value);
++
++      pan_abs (this->fd, value);
++
++      res = value;
++      break;
++
++    case IN_CMD_PAN_PLUS:
++      DBG ("pan + %d\n", value);
++#ifdef USE_BUTTONS
++      pan_rel (this->fd, this->min_res);
++      res = this->min_res;
++#else
++      worker_pan = value ? this->min_res : 0;
++      pthread_cond_broadcast (&worker_cond);
++      res = value;
++#endif
++      break;
++
++    case IN_CMD_PAN_MINUS:
++      DBG ("pan - %d\n", value);
++#ifdef USE_BUTTONS
++      pan_rel (this->fd, -this->min_res);
++      res = this->min_res;
++#else
++      worker_pan = value ? -this->min_res : 0;
++      pthread_cond_broadcast (&worker_cond);
++      res = value;
++#endif
++
++      break;
++
++    case IN_CMD_TILT_SET:
++      DBG ("set tilt to %d degrees\n", value);
++
++      tilt_abs (this->fd, value);
++      res = value;
++      break;
++
++    case IN_CMD_TILT_PLUS:
++      DBG ("tilt + %d\n", value);
++#ifdef USE_BUTTONS
++      tilt_rel (this->fd, this->min_res);
++
++      res = this->min_res;
++#else
++      worker_tilt = value ? this->min_res : 0;
++      pthread_cond_broadcast (&worker_cond);
++      res = value;
++#endif
++
++      break;
++
++    case IN_CMD_TILT_MINUS:
++      DBG ("tilt - %d\n", value);
++
++#ifdef USE_BUTTONS
++      tilt_rel (this->fd, -this->min_res);
++
++      res = this->min_res;
++#else
++      worker_tilt = value ? -this->min_res : 0;
++      pthread_cond_broadcast (&worker_cond);
++      res = value;
++#endif
++
++      break;
++
++    default:
++      DBG ("nothing matched\n");
++      res = -1;
++    }
++
++
++  return res;
++}
++
++
++
++int
++input_cmd (int plugin_number, unsigned int control_id, unsigned int group,
++           int value, char *value_string)
++{
++  int ret = -1;
++  DBG ("Requested cmd (id: %d) for the %d plugin. Group: %d value: %d\n",
++       control_id, plugin_number, group, value);
++
++  if (group != IN_CMD_GENERIC)
++    return -1;
++
++  switch (control_id)
++    {
++    case IN_CMD_PAN_MINUS:
++    case IN_CMD_PAN_PLUS:
++    case IN_CMD_TILT_MINUS:
++    case IN_CMD_TILT_PLUS:
++    case IN_CMD_TILT_SET:
++    case IN_CMD_PAN_SET:
++    case IN_CMD_RESET_PAN_TILT:
++      ret = input_cmd_old (control_id, value);
++      break;
++    }
++
++  return ret;
++}
+Index: bar/plugins/input_motors/Makefile
+===================================================================
+--- /dev/null
++++ bar/plugins/input_motors/Makefile
+@@ -0,0 +1,24 @@
++###############################################################
++#
++# Purpose: Makefile for "M-JPEG Streamer"
++# Author.: Tom Stoeveken (TST)
++# Version: 0.3
++# License: GPL
++#
++###############################################################
++
++CC = gcc
++
++OTHER_HEADERS = ../../mjpg_streamer.h ../../utils.h ../output.h ../input.h
++
++CFLAGS += -O2 -DLINUX -D_GNU_SOURCE -Wall -shared -fPIC
++#CFLAGS += -DDEBUG
++
++all: input_motors.so
++
++clean:
++	rm -f *.a *.o core *~ *.so *.lo
++
++input_motors.so: $(OTHER_HEADERS) 
++	$(CC) $(CFLAGS) $(LFLAGS) -o $@ input_motors.c
++
+Index: bar/www/index.html
+===================================================================
+--- bar.orig/www/index.html
++++ bar/www/index.html
+@@ -3,53 +3,87 @@
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+   <head>
+     <title>MJPG-streamer</title>
+-    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
+-    <link rel="stylesheet" href="style.css" type="text/css" />
++    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
++
++    <link rel="stylesheet" href="index.css" type="text/css" />
++    
+     <!--[if IE 6]>
+     <link rel="stylesheet" href="fix.css" type="text/css" />
+     <![endif]-->
+   </head>
+-
++  
+   <body>
+-    <div id="sidebar">
+-      <h1>MJPG-Streamer Demo Pages</h1>
+-      <h2>a ressource friendly streaming application</h2>
+-
+-      <div id="menu">
+-        <a class="active" href="index.html">Home</a>
+-        <a href="static.html">Static</a>
+-        <a href="stream.html">Stream</a>
+-        <a href="java.html">Java</a>
+-        <a href="javascript.html">Javascript</a>
+-        <a href="videolan.html">VideoLAN</a>
+-        <a target="_blank" onClick="window.open(this.href, '_blank'); return false;" href="control.html">Control</a>
+-		<a target="_blank" onClick="window.open(this.href, '_blank'); return false;" href="control_old.html">Control old</a>
+-      </div>
+-
+-      <h3>Version info:</h3>
+-      <p>v0.1 (Okt 22, 2007)</p>
+-    </div>
+-
+-    <div id="content">
+-      <h1>About</h1>
+-      <h2>Details about the M-JPEG streamer</h2>
+-
+-      <h3>Congratulations</h3>
+-      <p>You sucessfully managed to install this streaming webserver. If you can see this page, you can also access the stream of JPGs, which can originate from your webcam for example. This installation consists of these example pages and you may customize the look and content.</p>
+-
+-      <img src="example.jpg" width="512" height="160" alt="just an image" />
+-      <p>The reason for developing this software was the need of a simple and ressource friendly streaming application for Linux-UVC compatible webcams. The predecessor <i>uvc-streamer</i> is working well, but i wanted to implement a few more ideas. For instance, plugins can be used to process the images. One input plugin copies images to a global variable, multiple output plugins can access those images. For example this webpage is served by the <i>output_http.so</i> plugin.</p>
+-
+-      <a href="static.html"><img src="/?action=snapshot" width="170" height="128" alt="static image example" /></a>
+-      <p>The image displayed here was grabbed by the input plugin. The HTTP request contains the GET parameters <i>action=snapshot</i>. This requests one single picture from the image-input. To display another example, just click on the picture.</p>
+ 
+-      <h3>About the examples</h3>
+-      <p>To view the stream with any browser you may try the <i>javascript</i> or <i>java</i> subpages. Firefox is able to display the M-JPEG-stream directly.</p>
++	<p id="streamwrap" class="xform-p">
++		<img id="streamimage" class="x-rotated-180" src="/?action=stream" />
++	</p>
++
++		<script type="text/javascript"> 
++
++		function send_button(command,value) {
++			action_zone.location='/?action=command_ng&dest=0&plugin=1&id='+command+'&group=0&value='+value;
++		}
++
++	function up_onmousedown()
++	{
++		send_button(9,1);
++	}
++
++	function up_onmouseup()
++	{
++		send_button(9,0);
++	}
++
++
++
++	function down_onmousedown()
++	{
++		send_button(10,1);
++	}
++
++	function down_onmouseup()
++	{
++		send_button(10,0);
++	}
++
++
++
++
++	function left_onmousedown()
++	{
++		send_button(7,1);
++	}
++
++	function left_onmouseup()
++	{
++		send_button(7,0);
++	}
++
++
++	function right_onmousedown()
++	{
++		send_button(6,1);
++	}
++
++	function right_onmouseup()
++	{
++		send_button(6,0);
++	}
++
++	</script>
++	
++	<div id="tabs">
++		<ul id="ul_tabs"></ul>
++	</div>
++
++	<h2>
++	<span onmouseup="left_onmouseup()" onmousedown="left_onmousedown()" style="border:solid 1px red">←</span>
++	<span onmouseup="up_onmouseup()" onmousedown="up_onmousedown()" style="border:solid 1px red">↑</span>
++	<span onmouseup="down_onmouseup()" onmousedown="down_onmousedown()" style="border:solid 1px red">↓</span>
++	<span onmouseup="right_onmouseup()" onmousedown="right_onmousedown()" style="border:solid 1px red">→</span>
++	</h2>
+ 
+-      <h3>About this server</h3>
+-      <p>This server is running a software written for the MJPG-streamer project. The MJPG-streamer developers can not be made responsible for installations of this software.</p>
++	<iframe name="action_zone" style="display:none" bgcolor='#d8d8d8'></iframe>
+ 
+-      <p>&copy; The <a href="http://mjpg-streamer.sf.net">MJPG-streamer team</a> | Design by <a href="http://andreasviklund.com">Andreas Viklund</a></p>
+-    </div>
+   </body>
+ </html>
+Index: bar/www/index.html
+===================================================================
+--- /dev/null
++++ bar/www/index.css
+@@ -0,0 +1,5 @@
++img.x-rotated-180 {
++        -moz-transform: rotate(180deg);
++        -webkit-transform: rotate(180deg);
++}
++
diff --git a/target/linux/ramips/modules.mk b/target/linux/ramips/modules.mk
index 503a1e5..673fa61 100644
--- a/target/linux/ramips/modules.mk
+++ b/target/linux/ramips/modules.mk
@@ -21,6 +21,21 @@ endef
 
 $(eval $(call KernelPackage,sdhci-mt7620))
 
+KKMOON_MOTORS_MODULES:= \
+  CONFIG_KKMOON_MOTORS:drivers/misc/kkmoon-motors
+
+define KernelPackage/kkmoon-motors
+  $(call i2c_defaults,$(KKMOON_MOTORS_MODULES),59)
+  TITLE:=KKMoon motors
+  DEPENDS:=@TARGET_ramips
+endef
+
+define KernelPackage/kkmoon-motors/description
+ Kernel module for stepper motors on kkmoon camrea
+endef
+
+$(eval $(call KernelPackage,kkmoon-motors))
+
 I2C_RALINK_MODULES:= \
   CONFIG_I2C_RALINK:drivers/i2c/busses/i2c-ralink
 
diff --git a/target/linux/ramips/patches-3.18/9999-kkmoon-motors.patch b/target/linux/ramips/patches-3.18/9999-kkmoon-motors.patch
new file mode 100644
index 0000000..54e9dc3
--- /dev/null
+++ b/target/linux/ramips/patches-3.18/9999-kkmoon-motors.patch
@@ -0,0 +1,277 @@
+Index: linux-3.18.21/drivers/misc/Kconfig
+===================================================================
+--- linux-3.18.21.orig/drivers/misc/Kconfig
++++ linux-3.18.21/drivers/misc/Kconfig
+@@ -515,6 +515,13 @@ config VEXPRESS_SYSCFG
+ 	  bus. System Configuration interface is one of the possible means
+ 	  of generating transactions on this bus.
+ 
++config KKMOON_MOTORS
++	tristate  "kkmoon motor support"
++	depends on GPIOLIB
++	default m
++	help
++		kkmoon motors
++
+ source "drivers/misc/c2port/Kconfig"
+ source "drivers/misc/eeprom/Kconfig"
+ source "drivers/misc/cb710/Kconfig"
+Index: linux-3.18.21/drivers/misc/Makefile
+===================================================================
+--- linux-3.18.21.orig/drivers/misc/Makefile
++++ linux-3.18.21/drivers/misc/Makefile
+@@ -56,3 +56,4 @@ obj-$(CONFIG_GENWQE)		+= genwqe/
+ obj-$(CONFIG_ECHO)		+= echo/
+ obj-$(CONFIG_VEXPRESS_SYSCFG)	+= vexpress-syscfg.o
+ obj-$(CONFIG_CXL_BASE)		+= cxl/
++obj-$(CONFIG_KKMOON_MOTORS)	+= kkmoon-motors.o
+Index: linux-3.18.21/drivers/misc/kkmoon-motors.c
+===================================================================
+--- /dev/null
++++ linux-3.18.21/drivers/misc/kkmoon-motors.c
+@@ -0,0 +1,245 @@
++#include <linux/init.h>
++#include <linux/module.h>
++#include <linux/kernel.h>
++#include <linux/fs.h>
++#include <linux/miscdevice.h>
++#include <linux/delay.h>
++#include <linux/gpio.h>
++#include <linux/jiffies.h>
++#include <linux/sched.h>
++
++ /* A fairly budget driver for some fairly budget hardware 
++  * 4 gpios are connected to a 74259 8 bit addressable latch 
++  * with the LE* pin tied low. The outputs of the latch feed
++  * to a ULN2008 and thence to the motors.
++  *
++  * As we don't control LE, the best we can do is assert 
++  * exactly zero one, all-1 or all outputs of the latch.
++  * So we're restricted to wave-driving the stepper motors
++  *
++  * The device surfaces as a misc char device to which
++  * sending U D L R moves the turret by one step in those
++  * directions.
++  */
++
++MODULE_LICENSE ("GPL");
++MODULE_AUTHOR ("James McKenzie");
++MODULE_DESCRIPTION ("KKMoon motor control");
++MODULE_VERSION ("0.1");
++
++#define GPIO_A0	22
++#define GPIO_A1 23
++#define GPIO_A2 24
++#define GPIO_D  26
++
++
++static int h_phase;
++static int v_phase;
++
++
++static void
++latch_addr (int a)
++{
++  gpio_set_value (GPIO_A0, ! !(a & 1));
++  gpio_set_value (GPIO_A1, ! !(a & 2));
++  gpio_set_value (GPIO_A2, ! !(a & 4));
++  ndelay (100);
++}
++
++
++static void
++latch_off (void)
++{
++  int i;
++  gpio_set_value (GPIO_D, 0);
++  for (i = 0; i < 8; ++i)
++    latch_addr (i);
++}
++
++
++static void
++latch_set (int a)
++{
++  latch_off ();
++  latch_addr (a);
++  gpio_set_value (GPIO_D, 1);
++}
++
++static int
++inc (int p)
++{
++  p++;
++  p %= 4;
++  return p;
++}
++
++
++static int
++dec (int p)
++{
++  p += 3;
++  p %= 4;
++  return p;
++}
++
++static void
++turret_left (void)
++{
++  latch_set (h_phase+4);
++  h_phase = dec (h_phase);
++  latch_set (h_phase+4);
++}
++
++static void
++turret_right (void)
++{
++  latch_set (h_phase+4);
++  h_phase = inc (h_phase);
++  latch_set (h_phase+4);
++}
++
++
++static void
++turret_down (void)
++{
++  latch_set (v_phase);
++  v_phase = inc (v_phase);
++  latch_set (v_phase);
++}
++
++
++
++static void
++turrent_up (void)
++{
++  latch_set (v_phase);
++  v_phase = dec (v_phase);
++  latch_set (v_phase);
++}
++
++void
++pause (void)
++{
++  set_current_state(TASK_UNINTERRUPTIBLE);
++  schedule_timeout (HZ / 50);
++}
++
++static ssize_t
++motors_write (struct file *file, const char __user * data, size_t count,
++              loff_t * ppos)
++{
++  char c;
++  size_t red = 0;
++
++  while (count--)
++    {
++
++      if (copy_from_user (&c, data++, 1))
++		return EFAULT;
++
++      switch (c)
++        {
++        case 'U':
++          turrent_up ();
++          pause ();
++          break;
++        case 'D':
++          turret_down ();
++          pause ();
++          break;
++        case 'L':
++          turret_left ();
++          pause ();
++          break;
++        case 'R':
++          turret_right ();
++          pause ();
++          break;
++
++        }
++
++
++      red++;
++
++    }
++
++  latch_off ();
++
++  *ppos = red;
++
++  return red;
++}
++
++
++static const struct file_operations motors_fops = {
++  .owner = THIS_MODULE,
++  .write = motors_write,
++};
++
++
++static struct miscdevice motors_dev = {
++  MISC_DYNAMIC_MINOR,
++  "motors",
++  &motors_fops
++};
++
++
++
++
++
++
++
++
++static int __init
++motors_init (void)
++{
++  int ret;
++  printk (KERN_INFO "KKMOON_MOTOR: Initializing\n");
++
++  ret = misc_register (&motors_dev);
++  if (ret)
++    return ret;
++
++
++  gpio_request (GPIO_A0, "sysfs");
++  gpio_request (GPIO_A1, "sysfs");
++  gpio_request (GPIO_A2, "sysfs");
++  gpio_request (GPIO_D, "sysfs");
++
++  //gpio_export (GPIO_A0, false);
++  //gpio_export (GPIO_A1, false);
++  //gpio_export (GPIO_A2, false);
++  //gpio_export (GPIO_D, false);
++
++  gpio_direction_output (GPIO_A0, 0);
++  gpio_direction_output (GPIO_A1, 0);
++  gpio_direction_output (GPIO_A2, 0);
++  gpio_direction_output (GPIO_D, 0);
++
++
++  latch_off ();
++
++  return 0;
++}
++
++static void __exit
++motors_exit (void)
++{
++
++  latch_off ();
++
++  //gpio_unexport (GPIO_D);
++  //gpio_unexport (GPIO_A2);
++  //gpio_unexport (GPIO_A1);
++  //gpio_unexport (GPIO_A0);
++
++  gpio_free (GPIO_D);
++  gpio_free (GPIO_A2);
++  gpio_free (GPIO_A1);
++  gpio_free (GPIO_A0);
++
++  misc_deregister (&motors_dev);
++}
++
++module_init (motors_init);
++module_exit (motors_exit);
diff --git a/target/linux/ramips/rt305x/config-3.18 b/target/linux/ramips/rt305x/config-3.18
index 9c03767..7f48c28 100644
--- a/target/linux/ramips/rt305x/config-3.18
+++ b/target/linux/ramips/rt305x/config-3.18
@@ -108,8 +108,8 @@ CONFIG_MODULES_USE_ELF_REL=y
 CONFIG_MTD_CMDLINE_PARTS=y
 CONFIG_MTD_M25P80=y
 CONFIG_MTD_PHYSMAP=y
-CONFIG_MTD_SPLIT_FIRMWARE=y
 CONFIG_MTD_SPI_NOR=y
+CONFIG_MTD_SPLIT_FIRMWARE=y
 CONFIG_MTD_SPLIT_UIMAGE_FW=y
 CONFIG_NEED_DMA_MAP_STATE=y
 CONFIG_NEED_PER_CPU_KM=y