-
Notifications
You must be signed in to change notification settings - Fork 5
/
ptp.c
2962 lines (2524 loc) · 75.2 KB
/
ptp.c
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
/*
* Copyright (C) 2009
* Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*/
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <memory.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
#include <semaphore.h>
#include <iconv.h>
#include <dirent.h>
#include <stdint.h>
#include <glib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/mman.h>
#include <sys/vfs.h>
#include <sys/wait.h>
#include <sys/utsname.h>
#include <sys/inotify.h>
#include <asm/byteorder.h>
#include <linux/types.h>
#include <linux/usb/functionfs.h>
#include <linux/usb/ch9.h>
/*
+ * cpu_to_le16/32 are used when initializing structures, a context where a
+ * function call is not allowed. To solve this, we code cpu_to_le16/32 in a way
+ * that allows them to be used when initializing structures.
+ */
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define cpu_to_le16(x) (x)
#define cpu_to_le32(x) (x)
#else
#define cpu_to_le16(x) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
#define cpu_to_le32(x) \
((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \
(((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
#endif
#define le32_to_cpu(x) le32toh(x)
#define le16_to_cpu(x) le16toh(x)
#define min(a,b) ({ typeof(a) __a = (a); typeof(b) __b = (b); __a < __b ? __a : __b; })
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
static int verbose;
/* Still Image class-specific requests: */
#define USB_REQ_PTP_CANCEL_REQUEST 0x64
#define USB_REQ_PTP_GET_EXTENDED_EVENT_DATA 0x65
#define USB_REQ_PTP_DEVICE_RESET_REQUEST 0x66
#define USB_REQ_PTP_GET_DEVICE_STATUS_REQUEST 0x67
#define DRIVER_VENDOR_NUM 0x1d6b
#define DRIVER_PRODUCT_NUM 0x0100
#define DRIVER_INTERFACE "PTP Interface"
/* Will be used for bcdDevice: remember to update on major changes */
#define MAJOR 1
#define MINOR 1
#define DRIVER_VERSION ((MAJOR << 8) | MINOR)
#define VERSION_STRING __stringify(MAJOR) "." __stringify(MINOR)
#define PTP_MANUFACTURER "Linux Foundation"
#define PTP_MODEL "PTP Gadget"
#define PTP_STORAGE_DESC "SD/MMC"
#define PTP_MODEL_DIR "100LINUX"
#define THUMB_SUPPORT
#undef THUMB_SUPPORT
#define FORMAT_SUPPORT
#undef FORMAT_SUPPORT
/*-------------------------------------------------------------------------*/
/* USB subclass value = the protocol encapsulation */
#define USB_SC_IMAGE_CAPTURE 0x01 /* Still Image Capture Subclass */
#define USB_PR_CB 0x01 /* Control/Bulk w/o interrupt */
#define MAX_PACKET_SIZE_HS 512
#define MAX_PACKET_SIZE_SS 1024
/* some devices can handle other status packet sizes */
/* ATTENTION: somehow automatic ZLP generation is not working.
* make sure that event length, which is 24, is not a multiple of STATUS_MAXPACKET
*/
#define STATUS_MAXPACKET 28
static const struct
{
struct usb_functionfs_descs_head_v2 header;
__le32 fs_count;
__le32 hs_count;
__le32 ss_count;
struct {
struct usb_interface_descriptor intf;
struct usb_endpoint_descriptor_no_audio source;
struct usb_endpoint_descriptor_no_audio sink;
struct usb_endpoint_descriptor_no_audio status;
} __attribute__((packed)) fs_descs, hs_descs;
struct {
struct usb_interface_descriptor intf;
struct usb_endpoint_descriptor_no_audio source;
struct usb_ss_ep_comp_descriptor source_comp;
struct usb_endpoint_descriptor_no_audio sink;
struct usb_ss_ep_comp_descriptor sink_comp;
struct usb_endpoint_descriptor_no_audio status;
struct usb_ss_ep_comp_descriptor status_comp;
} ss_descs;
} __attribute__((packed)) descriptors = {
.header = {
.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
.flags = cpu_to_le32(FUNCTIONFS_HAS_FS_DESC |
FUNCTIONFS_HAS_HS_DESC |
FUNCTIONFS_HAS_SS_DESC),
.length = cpu_to_le32(sizeof descriptors),
},
.fs_count = cpu_to_le32(4),
.fs_descs = {
.intf = {
.bLength = sizeof(descriptors.fs_descs.intf),
.bDescriptorType = USB_DT_INTERFACE,
.bNumEndpoints = 3,
.bInterfaceClass = USB_CLASS_STILL_IMAGE,
.bInterfaceSubClass = USB_SC_IMAGE_CAPTURE,
.bInterfaceProtocol = USB_PR_CB,
.iInterface = 1,
}, .source = {
.bLength = sizeof(descriptors.fs_descs.source),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
}, .sink = {
.bLength = sizeof(descriptors.fs_descs.sink),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 2 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
}, .status = {
.bLength = sizeof(descriptors.fs_descs.status),
.bDescriptorType = USB_DT_ENDPOINT,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.bEndpointAddress = 3 | USB_DIR_IN,
.wMaxPacketSize = __constant_cpu_to_le16(STATUS_MAXPACKET),
.bInterval = 10,
},
},
.hs_count = cpu_to_le32(4),
.hs_descs = {
.intf = {
.bLength = sizeof(descriptors.hs_descs.intf),
.bDescriptorType = USB_DT_INTERFACE,
.bNumEndpoints = 3,
.bInterfaceClass = USB_CLASS_STILL_IMAGE,
.bInterfaceSubClass = USB_SC_IMAGE_CAPTURE,
.bInterfaceProtocol = USB_PR_CB,
.iInterface = 1,
}, .source = {
.bLength = sizeof(descriptors.hs_descs.source),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = __constant_cpu_to_le16(MAX_PACKET_SIZE_HS),
}, .sink = {
.bLength = sizeof(descriptors.hs_descs.sink),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 2 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = __constant_cpu_to_le16(MAX_PACKET_SIZE_HS),
}, .status = {
.bLength = sizeof(descriptors.hs_descs.status),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 3 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.wMaxPacketSize = __constant_cpu_to_le16(STATUS_MAXPACKET),
.bInterval = 10,
},
},
.ss_count = cpu_to_le32(7),
.ss_descs = {
.intf = {
.bLength = sizeof descriptors.ss_descs.intf,
.bDescriptorType = USB_DT_INTERFACE,
.bNumEndpoints = 3,
.bInterfaceClass = USB_CLASS_STILL_IMAGE,
.bInterfaceSubClass = USB_SC_IMAGE_CAPTURE,
.bInterfaceProtocol = USB_PR_CB,
.iInterface = 1,
},
.source = {
.bLength = sizeof descriptors.ss_descs.source,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = __constant_cpu_to_le16(MAX_PACKET_SIZE_SS),
},
.source_comp = {
.bLength = USB_DT_SS_EP_COMP_SIZE,
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
.bMaxBurst = 0,
.bmAttributes = 0,
.wBytesPerInterval = 0,
},
.sink = {
.bLength = sizeof descriptors.ss_descs.sink,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 2 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = __constant_cpu_to_le16(MAX_PACKET_SIZE_SS),
},
.sink_comp = {
.bLength = USB_DT_SS_EP_COMP_SIZE,
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
.bMaxBurst = 0,
.bmAttributes = 0,
.wBytesPerInterval = 0,
},
.status =
{
.bLength = sizeof descriptors.ss_descs.status,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 3 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_INT,
/* .wMaxPacketSize = autoconfiguration (kernel) */
.wMaxPacketSize = __constant_cpu_to_le16(STATUS_MAXPACKET),
.bInterval = 10,
},
.status_comp = {
.bLength = USB_DT_SS_EP_COMP_SIZE,
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
.bMaxBurst = 0,
.bmAttributes = 0,
.wBytesPerInterval = 0,
},
},
};
/*-------------------------------------------------------------------------*/
static const struct
{
struct usb_functionfs_strings_head header;
struct {
__le16 code;
const char str1[sizeof(DRIVER_INTERFACE)];
} __attribute__((packed)) lang0;
} __attribute__((packed)) strings = {
.header = {
.magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
.length = cpu_to_le32(sizeof(strings)),
.str_count = cpu_to_le32(1),
.lang_count = cpu_to_le32(1),
}, .lang0 = {
cpu_to_le16(0x0409), /* en-us */
DRIVER_INTERFACE,
},
};
/*-------------------------------------------------------------------------*/
/* kernel drivers could autoconfigure like this too ... if
* they were willing to waste the relevant code/data space.
*/
#define FFS_PREFIX "/dev/ptp/"
#define FFS_PTP_EP0 FFS_PREFIX"ep0"
#define FFS_PTP_IN FFS_PREFIX"ep1"
#define FFS_PTP_OUT FFS_PREFIX"ep2"
#define FFS_PTP_INT FFS_PREFIX"ep3"
/* gadgetfs currently has no chunking (or O_DIRECT/zerocopy) support
* to turn big requests into lots of smaller ones; so this is "small".
*/
#define USB_BUFSIZE (7 * 1024)
#define CHECK_COUNT(cnt, min, max, op) do { \
if (cnt & 3 || cnt < min || cnt > max) { \
fprintf(stderr, "Wrong " op " size: %u\n", \
(unsigned int)cnt); \
errno = EPIPE; \
return -1; \
} \
} while (0)
#define CHECK_SESSION(s_container, r_container, cnt, ret) do { \
if (session <= 0) { \
make_response(s_container, r_container, \
PIMA15740_RESP_SESSION_NOT_OPEN, \
sizeof(*s_container)); \
*cnt = 0; \
*ret = 0; \
break; \
} \
} while (0)
#define STORE_ID 0x00010001
#define PTP_PARAM_UNUSED 0
#define PTP_PARAM_ANY 0xffffffff
/* All little endian */
struct ptp_container {
uint32_t length;
uint16_t type;
uint16_t code;
uint32_t id;
uint8_t payload[];
} __attribute__ ((packed));
enum ptp_container_type {
PTP_CONTAINER_TYPE_UNDEFINED = 0,
PTP_CONTAINER_TYPE_COMMAND_BLOCK = 1,
PTP_CONTAINER_TYPE_DATA_BLOCK = 2,
PTP_CONTAINER_TYPE_RESPONSE_BLOCK = 3,
PTP_CONTAINER_TYPE_EVENT_BLOCK = 4,
};
enum pima15740_operation_code {
PIMA15740_OP_UNDEFINED = 0x1000,
PIMA15740_OP_GET_DEVICE_INFO = 0x1001,
PIMA15740_OP_OPEN_SESSION = 0x1002,
PIMA15740_OP_CLOSE_SESSION = 0x1003,
PIMA15740_OP_GET_STORAGE_IDS = 0x1004,
PIMA15740_OP_GET_STORAGE_INFO = 0x1005,
PIMA15740_OP_GET_NUM_OBJECTS = 0x1006,
PIMA15740_OP_GET_OBJECT_HANDLES = 0x1007,
PIMA15740_OP_GET_OBJECT_INFO = 0x1008,
PIMA15740_OP_GET_OBJECT = 0x1009,
PIMA15740_OP_GET_THUMB = 0x100a,
PIMA15740_OP_DELETE_OBJECT = 0x100b,
PIMA15740_OP_SEND_OBJECT_INFO = 0x100c,
PIMA15740_OP_SEND_OBJECT = 0x100d,
PIMA15740_OP_INITIATE_CAPTURE = 0x100e,
PIMA15740_OP_FORMAT_STORE = 0x100f,
PIMA15740_OP_RESET_DEVICE = 0x1010,
PIMA15740_OP_SELF_TEST = 0x1011,
PIMA15740_OP_SET_OBJECT_PROTECTION = 0x1012,
PIMA15740_OP_POWER_DOWN = 0x1013,
PIMA15740_OP_GET_DEVICE_PROP_DESC = 0x1014,
PIMA15740_OP_GET_DEVICE_PROP_VALUE = 0x1015,
PIMA15740_OP_SET_DEVICE_PROP_VALUE = 0x1016,
PIMA15740_OP_RESET_DEVICE_PROP_VALUE = 0x1017,
PIMA15740_OP_TERMINATE_OPEN_CAPTURE = 0x1018,
PIMA15740_OP_MOVE_OBJECT = 0x1009,
PIMA15740_OP_COPY_OBJECT = 0x100a,
PIMA15740_OP_GET_PARTIAL_OBJECT = 0x100b,
PIMA15740_OP_INITIATE_OPEN_CAPTURE = 0x100c,
};
enum pima15740_response_code {
PIMA15740_RESP_UNDEFINED = 0x2000,
PIMA15740_RESP_OK = 0x2001,
PIMA15740_RESP_GENERAL_ERROR = 0x2002,
PIMA15740_RESP_SESSION_NOT_OPEN = 0x2003,
PIMA15740_RESP_INVALID_TRANSACTION_ID = 0x2004,
PIMA15740_RESP_OPERATION_NOT_SUPPORTED = 0x2005,
PIMA15740_RESP_PARAMETER_NOT_SUPPORTED = 0x2006,
PIMA15740_RESP_INCOMPLETE_TRANSFER = 0x2007,
PIMA15740_RESP_INVALID_STORAGE_ID = 0x2008,
PIMA15740_RESP_INVALID_OBJECT_HANDLE = 0x2009,
PIMA15740_RESP_DEVICE_PROP_NOT_SUPPORTED = 0x200a,
PIMA15740_RESP_INVALID_OBJECT_FORMAT_CODE = 0x200b,
PIMA15740_RESP_STORE_FULL = 0x200c,
PIMA15740_RESP_OBJECT_WRITE_PROTECTED = 0x200d,
PIMA15740_RESP_STORE_READ_ONLY = 0x200e,
PIMA15740_RESP_ACCESS_DENIED = 0x200f,
PIMA15740_RESP_NO_THUMBNAIL_PRESENT = 0x2010,
PIMA15740_RESP_SELFTEST_FAILED = 0x2011,
PIMA15740_RESP_PARTIAL_DELETION = 0x2012,
PIMA15740_RESP_STORE_NOT_AVAILABLE = 0x2013,
PIMA15740_RESP_SPECIFICATION_BY_FORMAT_NOT_SUPPORTED = 0x2014,
PIMA15740_RESP_NO_VALID_OBJECT_INFO = 0x2015,
PIMA15740_RESP_INVALID_CODE_FORMAT = 0x2016,
PIMA15740_RESP_UNKNOWN_VENDOR_CODE = 0x2017,
PIMA15740_RESP_CAPTURE_ALREADY_TERMINATED = 0x2018,
PIMA15740_RESP_DEVICE_BUSY = 0x2019,
PIMA15740_RESP_INVALID_PARENT_OBJECT = 0x201a,
PIMA15740_RESP_INVALID_DEVICE_PROP_FORMAT = 0x201b,
PIMA15740_RESP_INVALID_DEVICE_PROP_VALUE = 0x201c,
PIMA15740_RESP_INVALID_PARAMETER = 0x201d,
PIMA15740_RESP_SESSION_ALREADY_OPEN = 0x201e,
PIMA15740_RESP_TRANSACTION_CANCELLED = 0x201f,
PIMA15740_RESP_SPECIFICATION_OF_DESTINATION_UNSUPPORTED = 0x2020,
};
enum pima15740_event_code {
PIMA15740_EVENT_UNDEFINED = 0x4000,
PIMA15740_EVENT_CANCEL_TRANSACTION = 0x4001,
PIMA15740_EVENT_OBJECT_ADDED = 0x4002,
PIMA15740_EVENT_OBJECT_REMOVED = 0x4003,
PIMA15740_EVENT_STORE_ADDED = 0x4004,
PIMA15740_EVENT_STORE_REMOVED = 0x4005,
PIMA15740_EVENT_DEVICE_PROP_CHANGED = 0x4006,
PIMA15740_EVENT_OBJECT_INFO_CHANGED = 0x4007,
PIMA15740_EVENT_DEVICE_INFO_CHANGED = 0x4008,
PIMA15740_EVENT_REQUEST_OBJECT_TRANSFER = 0x4009,
PIMA15740_EVENT_STORE_FULL = 0x400a,
PIMA15740_EVENT_DEVICE_RESET = 0x400b,
PIMA15740_EVENT_STORAGE_INFO_CHANGED = 0x400c,
PIMA15740_EVENT_CAPTURE_COMPLETE = 0x400d,
PIMA15740_EVENT_UNREPORTED_STATUS = 0x400e,
};
enum pima15740_data_format {
PIMA15740_FMT_A_UNDEFINED = 0x3000,
PIMA15740_FMT_A_ASSOCIATION = 0x3001,
PIMA15740_FMT_A_TEXT = 0x3004,
PIMA15740_FMT_I_UNDEFINED = 0x3800,
PIMA15740_FMT_I_EXIF_JPEG = 0x3801,
PIMA15740_FMT_I_TIFF_EP = 0x3802,
PIMA15740_FMT_I_JFIF = 0x3808,
PIMA15740_FMT_I_PNG = 0x380b,
PIMA15740_FMT_I_TIFF = 0x380d,
PIMA15740_FMT_I_TIFF_IT = 0x380e,
};
enum pima15740_storage_type {
PIMA15740_STORAGE_UNDEFINED = 0,
PIMA15740_STORAGE_FIXED_ROM = 0x0001,
PIMA15740_STORAGE_REMOVABLE_ROM = 0x0002,
PIMA15740_STORAGE_FIXED_RAM = 0x0003,
PIMA15740_STORAGE_REMOVABLE_RAM = 0x0004,
};
enum pima15740_filesystem_type {
PIMA15740_FILESYSTEM_UNDEFINED = 0,
PIMA15740_FILESYSTEM_GENERIC_FLAT = 0x0001,
PIMA15740_FILESYSTEM_GENERIC_HIERARCH = 0x0002,
PIMA15740_FILESYSTEM_DCF = 0x0003,
};
enum pima15740_access_capability {
PIMA15740_ACCESS_CAP_RW = 0,
PIMA15740_ACCESS_CAP_RO_WITHOUT_DEL = 0x0001,
PIMA15740_ACCESS_CAP_RO_WITH_DEL = 0x0002,
};
static const char manuf[] = PTP_MANUFACTURER;
static const char model[] = PTP_MODEL;
static const char storage_desc[] = PTP_STORAGE_DESC;
#define SUPPORTED_OPERATIONS \
__constant_cpu_to_le16(PIMA15740_OP_GET_DEVICE_INFO), \
__constant_cpu_to_le16(PIMA15740_OP_OPEN_SESSION), \
__constant_cpu_to_le16(PIMA15740_OP_CLOSE_SESSION), \
__constant_cpu_to_le16(PIMA15740_OP_GET_STORAGE_IDS), \
__constant_cpu_to_le16(PIMA15740_OP_GET_STORAGE_INFO), \
__constant_cpu_to_le16(PIMA15740_OP_GET_NUM_OBJECTS), \
__constant_cpu_to_le16(PIMA15740_OP_GET_OBJECT_HANDLES),\
__constant_cpu_to_le16(PIMA15740_OP_GET_OBJECT_INFO), \
__constant_cpu_to_le16(PIMA15740_OP_GET_OBJECT), \
__constant_cpu_to_le16(PIMA15740_OP_GET_THUMB), \
__constant_cpu_to_le16(PIMA15740_OP_DELETE_OBJECT), \
__constant_cpu_to_le16(PIMA15740_OP_SEND_OBJECT_INFO), \
__constant_cpu_to_le16(PIMA15740_OP_SEND_OBJECT),
static uint16_t dummy_supported_operations[] = {
SUPPORTED_OPERATIONS
};
#ifdef FORMAT_SUPPORT
#define SUPPORTED_FORMATS \
__constant_cpu_to_le16(PIMA15740_FMT_A_UNDEFINED), \
__constant_cpu_to_le16(PIMA15740_FMT_A_TEXT), \
__constant_cpu_to_le16(PIMA15740_FMT_I_EXIF_JPEG), \
__constant_cpu_to_le16(PIMA15740_FMT_I_TIFF_EP), \
__constant_cpu_to_le16(PIMA15740_FMT_I_PNG), \
__constant_cpu_to_le16(PIMA15740_FMT_I_TIFF), \
__constant_cpu_to_le16(PIMA15740_FMT_I_TIFF_IT), \
__constant_cpu_to_le16(PIMA15740_FMT_I_JFIF),
#else
#define SUPPORTED_FORMATS \
__constant_cpu_to_le16(PIMA15740_FMT_A_UNDEFINED),
#endif
static uint16_t dummy_supported_formats[] = {
SUPPORTED_FORMATS
};
#define SUPPORTED_EVENTS \
__constant_cpu_to_le16(PIMA15740_EVENT_OBJECT_ADDED), \
__constant_cpu_to_le16(PIMA15740_EVENT_OBJECT_REMOVED), \
__constant_cpu_to_le16(PIMA15740_EVENT_OBJECT_INFO_CHANGED),
static uint16_t dummy_supported_events[] = {
SUPPORTED_EVENTS
};
struct my_device_info {
uint16_t std_ver;
uint32_t vendor_ext_id;
uint16_t vendor_ext_ver;
uint8_t vendor_ext_desc_len;
uint16_t func_mode;
uint32_t operations_n;
uint16_t operations[ARRAY_SIZE(dummy_supported_operations)];
uint32_t events_n;
uint16_t events[ARRAY_SIZE(dummy_supported_events)];
uint32_t device_properties_n;
uint32_t capture_formats_n;
uint32_t image_formats_n;
uint16_t image_formats[ARRAY_SIZE(dummy_supported_formats)];
uint8_t manuf_len;
uint8_t manuf[sizeof(manuf) * 2];
uint8_t model_len;
uint8_t model[sizeof(model) * 2];
uint8_t dev_version_len;
uint8_t serial_num_len;
} __attribute__ ((packed));
struct my_device_info dev_info = {
.std_ver = __constant_cpu_to_le16(100), /* Standard version 1.00 */
.vendor_ext_id = __constant_cpu_to_le32(0),
.vendor_ext_ver = __constant_cpu_to_le16(0),
.vendor_ext_desc_len = __constant_cpu_to_le16(0),
.func_mode = __constant_cpu_to_le16(0),
.operations_n = __constant_cpu_to_le32(ARRAY_SIZE(dummy_supported_operations)),
.operations = {
SUPPORTED_OPERATIONS
},
.events_n = __constant_cpu_to_le32(ARRAY_SIZE(dummy_supported_events)),
.events = {
SUPPORTED_EVENTS
},
.device_properties_n = __constant_cpu_to_le32(0),
.capture_formats_n = __constant_cpu_to_le32(0),
.image_formats_n = __constant_cpu_to_le32(ARRAY_SIZE(dummy_supported_formats)),
.image_formats = {
SUPPORTED_FORMATS
},
.manuf_len = sizeof(manuf),
.model_len = sizeof(model),
.dev_version_len = 0,
.serial_num_len = 0,
};
struct my_storage_info {
uint16_t storage_type;
uint16_t filesystem_type;
uint16_t access_capability;
uint64_t max_capacity;
uint64_t free_space_in_bytes;
uint32_t free_space_in_images;
uint8_t desc_len;
uint8_t desc[sizeof(storage_desc) * 2];
uint8_t volume_label_len;
} __attribute__ ((packed));
static struct my_storage_info storage_info = {
.storage_type = __constant_cpu_to_le16(PIMA15740_STORAGE_REMOVABLE_RAM),
.filesystem_type = __constant_cpu_to_le16(PIMA15740_FILESYSTEM_DCF),
.access_capability = __constant_cpu_to_le16(PIMA15740_ACCESS_CAP_RW),
.desc_len = sizeof(storage_desc),
.volume_label_len = 0,
};
/* full duplex data, with at least three threads: ep0, sink, and source */
static int bulk_in = -ENXIO;
static int bulk_out = -ENXIO;
static int control = -ENXIO;
static int interrupt = -ENXIO;
static int session = -EINVAL;
static int notify_fd = -ENXIO;
static sem_t reset;
static sem_t dbaccess;
static iconv_t ic, uc;
static char *root;
static char *lockdir = "/tmp";
#define NEVENT 5
enum ptp_status {
PTP_WAITCONFIG, /* Waiting to be configured */
PTP_IDLE, /* Waiting for control / bulk-out */
PTP_DATA_OUT, /* Data arrival on bulk-out expected */
PTP_DATA_READY, /* Finished receive, have to process and send (Data and) Response */
PTP_DATA_IN, /* Waiting for bulk-in to become free for more data */
};
static enum ptp_status status = PTP_WAITCONFIG;
static pthread_t bulk_pthread;
static pthread_t inotify_pthread;
#define __stringify_1(x) #x
#define __stringify(x) __stringify_1(x)
#define BUF_SIZE 4096
#ifdef THUMB_SUPPORT
#define THUMB_WIDTH 160
#define THUMB_HEIGHT 120
#define THUMB_SIZE __stringify(THUMB_WIDTH) "x" __stringify(THUMB_HEIGHT)
#define THUMB_LOCATION "/var/cache/ptp/thumb/"
#endif
struct ptp_event_container {
uint32_t length;
uint16_t type;
uint16_t event_code;
uint32_t transaction_id;
uint32_t parameter1;
uint32_t parameter2;
uint32_t parameter3;
} __attribute__ ((packed));
struct ptp_object_info {
uint32_t storage_id;
uint16_t object_format;
uint16_t protection_status;
uint32_t object_compressed_size;
uint16_t thumb_format;
uint32_t thumb_compressed_size;
uint32_t thumb_pix_width;
uint32_t thumb_pix_height;
uint32_t image_pix_width;
uint32_t image_pix_height;
uint32_t image_bit_depth;
uint32_t parent_object;
uint16_t association_type;
uint32_t association_desc;
uint32_t sequence_number;
uint8_t strings[];
} __attribute__ ((packed));
struct obj_list {
struct obj_list *next;
uint32_t handle;
size_t info_size;
char name[256];
struct ptp_object_info info;
};
#define GFOREACH(item, list) for(iterator = list; (item = NULL, 1) && iterator && (item = iterator->data, 1); iterator = g_slist_next(iterator))
static GSList *images;
/* number of objects, including associations - decrement when deleting */
static int last_object_number;
static struct obj_list *object_info_p;
static size_t put_string(iconv_t ic, char *buf, const char *s, size_t len);
static size_t get_string(iconv_t ic, char *buf, const char *s, size_t len);
static void inotify_sync();
static int object_handle_valid(unsigned int h)
{
struct obj_list *obj;
GSList *iterator;
GFOREACH(obj, images) {
if (obj->handle == h)
return 1;
}
return 0;
}
/*-------------------------------------------------------------------------*/
static void make_response(struct ptp_container *s_cntn, struct ptp_container *r_cntn,
enum pima15740_response_code code, size_t len)
{
s_cntn->id = r_cntn->id;
s_cntn->type = __cpu_to_le16(PTP_CONTAINER_TYPE_RESPONSE_BLOCK);
s_cntn->code = __cpu_to_le16(code);
s_cntn->length = __cpu_to_le32(len);
}
static int bulk_write(void *buf, size_t length)
{
size_t count = 0;
int ret;
do {
ret = write(bulk_in, buf + count, length - count);
if (ret < 0) {
if (errno != EINTR)
return ret;
/* Need to wait for control thread to finish reset */
sem_wait(&reset);
} else
count += ret;
} while (count < length);
if (verbose)
fprintf(stderr, "BULK-IN Sent %u bytes\n", (unsigned int)count);
return count;
}
static int bulk_read(void *buf, size_t length)
{
size_t count = 0;
int ret;
do {
ret = read(bulk_out, buf + count, length - count);
if (ret < 0) {
if (errno != EINTR)
return ret;
/* Need to wait for control thread to finish reset */
sem_wait(&reset);
} else
count += ret;
} while (count < length);
if (verbose)
fprintf(stderr, "BULK-OUT Read %u bytes\n", (unsigned int)count);
return count;
}
static int interrupt_write(void *buf, size_t length) {
size_t count = 0;
int ret;
do {
ret = write(interrupt, buf + count, length - count);
if (ret < 0) {
if (errno != EINTR)
return ret;
/* Need to wait for control thread to finish reset */
sem_wait(&reset);
} else
count += ret;
} while (count < length);
if (verbose)
fprintf(stderr, "INTERRUPT sent %u bytes\n", (unsigned int) count);
return count;
}
static int send_event(enum pima15740_event_code code, unsigned int param1) {
struct ptp_event_container event;
int len = 12 + 3 * 4; /* 12 + parameters*4 */
event.length = __cpu_to_le32(len);
event.type = __cpu_to_le16(PTP_CONTAINER_TYPE_EVENT_BLOCK);
event.event_code = __cpu_to_le16(code);
event.transaction_id = __cpu_to_le32(0);
event.parameter1 = __cpu_to_le32(param1);
event.parameter2 = __cpu_to_le32(0);
event.parameter3 = __cpu_to_le32(0);
if (verbose)
fprintf(stderr, "sending event, code: 0x%04X, parameter1: 0x%08X\n", code, param1);
return interrupt_write(&event, len);
}
static int send_object_handles(void *recv_buf, void *send_buf, size_t send_len)
{
struct ptp_container *r_container = recv_buf;
struct ptp_container *s_container = send_buf;
unsigned long length;
uint32_t *param;
uint32_t store_id;
struct obj_list *obj;
GSList *iterator = NULL;
int ret;
uint32_t *handle;
uint32_t format;
int obj_to_send = g_slist_length(images);
length = __le32_to_cpu(r_container->length);
param = (uint32_t *)r_container->payload;
store_id = __le32_to_cpu(*param);
/* supported storage IDs: 0x00010001 - our single storage, 0xffffffff - all stores */
if (store_id != STORE_ID && store_id != PTP_PARAM_ANY) {
make_response(s_container, r_container,
PIMA15740_RESP_INVALID_STORAGE_ID, sizeof(*s_container));
return 0;
}
format = __le32_to_cpu(*(param + 1));
if (length > 16 && format != PTP_PARAM_UNUSED && format != PTP_PARAM_ANY) {
make_response(s_container, r_container,
PIMA15740_RESP_SPECIFICATION_BY_FORMAT_NOT_SUPPORTED,
sizeof(*s_container));
return 0;
}
s_container->type = __cpu_to_le16(PTP_CONTAINER_TYPE_DATA_BLOCK);
*(uint32_t *)s_container->payload = __cpu_to_le32(obj_to_send);
s_container->length = __cpu_to_le32((obj_to_send + 1) * sizeof(uint32_t) +
sizeof(*s_container));
handle = (uint32_t *)s_container->payload + 1;
GFOREACH(obj, images) {
if ((void *)handle == send_buf + send_len) {
ret = bulk_write(send_buf, send_len);
if (ret < 0) {
errno = EPIPE;
return ret;
}
handle = send_buf;
}
*handle++ = __cpu_to_le32(obj->handle);
}
if ((void *)handle > send_buf) {
ret = bulk_write(send_buf, (void *)handle - send_buf);
if (ret < 0) {
errno = EPIPE;
return ret;
}
}
/* Prepare response */
make_response(s_container, r_container, PIMA15740_RESP_OK, sizeof(*s_container));
return 0;
}
static int send_object_info(void *recv_buf, void *send_buf, size_t send_len)
{
struct ptp_container *r_container = recv_buf;
struct ptp_container *s_container = send_buf;
uint32_t *param;
struct obj_list *obj = NULL;
GSList *iterator;
int ret;
uint32_t handle;
size_t count, total, offset;
void *info;
enum pima15740_response_code code = PIMA15740_RESP_OK;
param = (uint32_t *)r_container->payload;
handle = __le32_to_cpu(*param);
GFOREACH(obj, images) {
if (obj->handle == handle)
break;
}
if (!obj) {
code = PIMA15740_RESP_INVALID_OBJECT_HANDLE;
goto send_resp;
}
s_container->type = __cpu_to_le16(PTP_CONTAINER_TYPE_DATA_BLOCK);
total = obj->info_size + sizeof(*s_container);
s_container->length = __cpu_to_le32(total);
offset = sizeof(*s_container);
info = &obj->info;
/* Object Info cannot get > 4096 bytes - four strings make a maximum of 2048
* bytes plus a fixed-size block, but we play safe for the case someone
* makes the buffers smaller */
while (total) {
count = min(total, send_len);
memcpy(send_buf + offset, info, count - offset);
info += count - offset;
ret = bulk_write(send_buf, count);
if (ret < 0) {
errno = EPIPE;
return ret;
}
offset = 0;
total -= count;
}
send_resp:
/* Prepare response */
make_response(s_container, r_container, code, sizeof(*s_container));
return 0;
}
static int send_object_or_thumb(void *recv_buf, void *send_buf, size_t send_len, int thumb)
{
struct ptp_container *r_container = recv_buf;
struct ptp_container *s_container = send_buf;
uint32_t *param;
struct obj_list *obj = NULL;
GSList *iterator;
int ret;
uint32_t handle;
size_t count, total, offset, file_size;
int fd = -1;
char name[256];
unsigned char xferbuf[8*1024];
size_t unused __attribute__((unused));
param = (uint32_t *)r_container->payload;
handle = __le32_to_cpu(*param);
GFOREACH(obj, images) {
if (obj->handle == handle)
break;
}
if (!obj) {
make_response(s_container, r_container, PIMA15740_RESP_INVALID_OBJECT_HANDLE,
sizeof(*s_container));
return 0;
}
s_container->type = __cpu_to_le16(PTP_CONTAINER_TYPE_DATA_BLOCK);
offset = sizeof(*s_container);
#ifdef THUMB_SUPPORT
if (!thumb) {
strncpy(name, obj->name, sizeof(name) - 1);
name[sizeof(name) - 1] = '\0';
ret = chdir(root);
file_size = __le32_to_cpu(obj->info.object_compressed_size);
} else {
char *dot = strrchr(obj->name, '.');
*dot = '\0'; /* We know there is a dot in the name */
snprintf(name, sizeof(name) - 1, "%s.thumb.jpeg", obj->name);
*dot = '.';
name[sizeof(name) - 1] = '\0';
ret = chdir(THUMB_LOCATION);
file_size = __le32_to_cpu(obj->info.thumb_compressed_size);
}
#else
(void)thumb;
strncpy(name, obj->name, sizeof(name) - 1);
name[sizeof(name) - 1] = '\0';
ret = chdir(root);
file_size = __le32_to_cpu(obj->info.object_compressed_size);
#endif
total = file_size + sizeof(*s_container);
if (verbose)
fprintf(stderr, "%s(): total %lu\n", __func__, total);
s_container->length = __cpu_to_le32(total);
if (!ret)
fd = open(name, O_RDONLY);
if (ret < 0 || fd < 0) {
make_response(s_container, r_container, PIMA15740_RESP_INCOMPLETE_TRANSFER,
sizeof(*s_container));
return 0;
}
count = min(total, send_len);
unused = read(fd, send_buf + offset, count - offset);
ret = bulk_write(send_buf, count);
if (ret < 0) {
errno = EPIPE;
goto out;
}
total -= count;
send_len = min((size_t)(8 * 1024), sizeof(xferbuf));
while (total) {
count = min(total, send_len);
unused = read(fd, xferbuf, count);
ret = bulk_write(xferbuf, count);
if (ret < 0) {
errno = EPIPE;
goto out;
}
total -= count;
}
ret = 0;
out:
close(fd);
if (!ret)
/* Prepare response */
make_response(s_container, r_container, PIMA15740_RESP_OK, sizeof(*s_container));
return ret;
}
static int send_storage_ids(void *recv_buf, void *send_buf, size_t send_len)
{
struct ptp_container *s_container = send_buf;