-
Notifications
You must be signed in to change notification settings - Fork 40
/
printer.c
845 lines (752 loc) · 21.4 KB
/
printer.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
// SPDX-License-Identifier: Apache-2.0
//
// This example emulates a USB printer. The code handles enumeration and
// allows a Linux host to recognize a printer device (enabling it to read
// from and write to /dev/usb/lp0 if the usblp kernel module is loaded)
//
// Part of the USB Raw Gadget examples.
// See https://github.com/xairy/raw-gadget for details.
//
// Wei Ming Chen <wmchen.aristo@gmail.com>
#include <assert.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/types.h>
#include <linux/usb/ch9.h>
/*----------------------------------------------------------------------*/
#define UDC_NAME_LENGTH_MAX 128
struct usb_raw_init {
__u8 driver_name[UDC_NAME_LENGTH_MAX];
__u8 device_name[UDC_NAME_LENGTH_MAX];
__u8 speed;
};
enum usb_raw_event_type {
USB_RAW_EVENT_INVALID = 0,
USB_RAW_EVENT_CONNECT = 1,
USB_RAW_EVENT_CONTROL = 2,
USB_RAW_EVENT_SUSPEND = 3,
USB_RAW_EVENT_RESUME = 4,
USB_RAW_EVENT_RESET = 5,
USB_RAW_EVENT_DISCONNECT = 6,
};
struct usb_raw_event {
__u32 type;
__u32 length;
__u8 data[];
};
struct usb_raw_ep_io {
__u16 ep;
__u16 flags;
__u32 length;
__u8 data[];
};
#define USB_RAW_EPS_NUM_MAX 30
#define USB_RAW_EP_NAME_MAX 16
#define USB_RAW_EP_ADDR_ANY 0xff
struct usb_raw_ep_caps {
__u32 type_control : 1;
__u32 type_iso : 1;
__u32 type_bulk : 1;
__u32 type_int : 1;
__u32 dir_in : 1;
__u32 dir_out : 1;
};
struct usb_raw_ep_limits {
__u16 maxpacket_limit;
__u16 max_streams;
__u32 reserved;
};
struct usb_raw_ep_info {
__u8 name[USB_RAW_EP_NAME_MAX];
__u32 addr;
struct usb_raw_ep_caps caps;
struct usb_raw_ep_limits limits;
};
struct usb_raw_eps_info {
struct usb_raw_ep_info eps[USB_RAW_EPS_NUM_MAX];
};
#define USB_RAW_IOCTL_INIT _IOW('U', 0, struct usb_raw_init)
#define USB_RAW_IOCTL_RUN _IO('U', 1)
#define USB_RAW_IOCTL_EVENT_FETCH _IOR('U', 2, struct usb_raw_event)
#define USB_RAW_IOCTL_EP0_WRITE _IOW('U', 3, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_EP0_READ _IOWR('U', 4, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_EP_ENABLE _IOW('U', 5, struct usb_endpoint_descriptor)
#define USB_RAW_IOCTL_EP_DISABLE _IOW('U', 6, __u32)
#define USB_RAW_IOCTL_EP_WRITE _IOW('U', 7, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_EP_READ _IOWR('U', 8, struct usb_raw_ep_io)
#define USB_RAW_IOCTL_CONFIGURE _IO('U', 9)
#define USB_RAW_IOCTL_VBUS_DRAW _IOW('U', 10, __u32)
#define USB_RAW_IOCTL_EPS_INFO _IOR('U', 11, struct usb_raw_eps_info)
#define USB_RAW_IOCTL_EP0_STALL _IO('U', 12)
#define USB_RAW_IOCTL_EP_SET_HALT _IOW('U', 13, __u32)
#define USB_RAW_IOCTL_EP_CLEAR_HALT _IOW('U', 14, __u32)
#define USB_RAW_IOCTL_EP_SET_WEDGE _IOW('U', 15, __u32)
/*----------------------------------------------------------------------*/
int usb_raw_open() {
int fd = open("/dev/raw-gadget", O_RDWR);
if (fd < 0) {
perror("open()");
exit(EXIT_FAILURE);
}
return fd;
}
void usb_raw_init(int fd, enum usb_device_speed speed,
const char *driver, const char *device) {
struct usb_raw_init arg;
strcpy((char *)&arg.driver_name[0], driver);
strcpy((char *)&arg.device_name[0], device);
arg.speed = speed;
int rv = ioctl(fd, USB_RAW_IOCTL_INIT, &arg);
if (rv < 0) {
perror("ioctl(USB_RAW_IOCTL_INIT)");
exit(EXIT_FAILURE);
}
}
void usb_raw_run(int fd) {
int rv = ioctl(fd, USB_RAW_IOCTL_RUN, 0);
if (rv < 0) {
perror("ioctl(USB_RAW_IOCTL_RUN)");
exit(EXIT_FAILURE);
}
}
void usb_raw_event_fetch(int fd, struct usb_raw_event *event) {
int rv = ioctl(fd, USB_RAW_IOCTL_EVENT_FETCH, event);
if (rv < 0) {
perror("ioctl(USB_RAW_IOCTL_EVENT_FETCH)");
exit(EXIT_FAILURE);
}
}
int usb_raw_ep0_read(int fd, struct usb_raw_ep_io *io) {
int rv = ioctl(fd, USB_RAW_IOCTL_EP0_READ, io);
if (rv < 0) {
perror("ioctl(USB_RAW_IOCTL_EP0_READ)");
exit(EXIT_FAILURE);
}
return rv;
}
int usb_raw_ep0_write(int fd, struct usb_raw_ep_io *io) {
int rv = ioctl(fd, USB_RAW_IOCTL_EP0_WRITE, io);
if (rv < 0) {
perror("ioctl(USB_RAW_IOCTL_EP0_WRITE)");
exit(EXIT_FAILURE);
}
return rv;
}
int usb_raw_ep_enable(int fd, struct usb_endpoint_descriptor *desc) {
int rv = ioctl(fd, USB_RAW_IOCTL_EP_ENABLE, desc);
if (rv < 0) {
perror("ioctl(USB_RAW_IOCTL_EP_ENABLE)");
exit(EXIT_FAILURE);
}
return rv;
}
int usb_raw_ep_read(int fd, struct usb_raw_ep_io *io) {
int rv = ioctl(fd, USB_RAW_IOCTL_EP_READ, io);
if (rv < 0) {
perror("ioctl(USB_RAW_IOCTL_EP_READ)");
exit(EXIT_FAILURE);
}
return rv;
}
int usb_raw_ep_write(int fd, struct usb_raw_ep_io *io) {
int rv = ioctl(fd, USB_RAW_IOCTL_EP_WRITE, io);
if (rv < 0) {
perror("ioctl(USB_RAW_IOCTL_EP_WRITE)");
exit(EXIT_FAILURE);
}
return rv;
}
void usb_raw_configure(int fd) {
int rv = ioctl(fd, USB_RAW_IOCTL_CONFIGURE, 0);
if (rv < 0) {
perror("ioctl(USB_RAW_IOCTL_CONFIGURED)");
exit(EXIT_FAILURE);
}
}
void usb_raw_vbus_draw(int fd, uint32_t power) {
int rv = ioctl(fd, USB_RAW_IOCTL_VBUS_DRAW, power);
if (rv < 0) {
perror("ioctl(USB_RAW_IOCTL_VBUS_DRAW)");
exit(EXIT_FAILURE);
}
}
int usb_raw_eps_info(int fd, struct usb_raw_eps_info *info) {
int rv = ioctl(fd, USB_RAW_IOCTL_EPS_INFO, info);
if (rv < 0) {
perror("ioctl(USB_RAW_IOCTL_EPS_INFO)");
exit(EXIT_FAILURE);
}
return rv;
}
void usb_raw_ep0_stall(int fd) {
int rv = ioctl(fd, USB_RAW_IOCTL_EP0_STALL, 0);
if (rv < 0) {
perror("ioctl(USB_RAW_IOCTL_EP0_STALL)");
exit(EXIT_FAILURE);
}
}
void usb_raw_ep_set_halt(int fd, int ep) {
int rv = ioctl(fd, USB_RAW_IOCTL_EP_SET_HALT, ep);
if (rv < 0) {
perror("ioctl(USB_RAW_IOCTL_EP_SET_HALT)");
exit(EXIT_FAILURE);
}
}
/*----------------------------------------------------------------------*/
#define GET_DEVICE_ID 0
#define GET_PORT_STATUS 1
#define SOFT_RESET 2
void log_control_request(struct usb_ctrlrequest *ctrl) {
printf(" bRequestType: 0x%x (%s), bRequest: 0x%x, wValue: 0x%x,"
" wIndex: 0x%x, wLength: %d\n", ctrl->bRequestType,
(ctrl->bRequestType & USB_DIR_IN) ? "IN" : "OUT",
ctrl->bRequest, ctrl->wValue, ctrl->wIndex, ctrl->wLength);
switch (ctrl->bRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
printf(" type = USB_TYPE_STANDARD\n");
break;
case USB_TYPE_CLASS:
printf(" type = USB_TYPE_CLASS\n");
break;
case USB_TYPE_VENDOR:
printf(" type = USB_TYPE_VENDOR\n");
break;
default:
printf(" type = unknown = %d\n", (int)ctrl->bRequestType);
break;
}
switch (ctrl->bRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
switch (ctrl->bRequest) {
case USB_REQ_GET_DESCRIPTOR:
printf(" req = USB_REQ_GET_DESCRIPTOR\n");
switch (ctrl->wValue >> 8) {
case USB_DT_DEVICE:
printf(" desc = USB_DT_DEVICE\n");
break;
case USB_DT_CONFIG:
printf(" desc = USB_DT_CONFIG\n");
break;
case USB_DT_STRING:
printf(" desc = USB_DT_STRING\n");
break;
case USB_DT_INTERFACE:
printf(" desc = USB_DT_INTERFACE\n");
break;
case USB_DT_ENDPOINT:
printf(" desc = USB_DT_ENDPOINT\n");
break;
case USB_DT_DEVICE_QUALIFIER:
printf(" desc = USB_DT_DEVICE_QUALIFIER\n");
break;
case USB_DT_OTHER_SPEED_CONFIG:
printf(" desc = USB_DT_OTHER_SPEED_CONFIG\n");
break;
case USB_DT_INTERFACE_POWER:
printf(" desc = USB_DT_INTERFACE_POWER\n");
break;
case USB_DT_OTG:
printf(" desc = USB_DT_OTG\n");
break;
case USB_DT_DEBUG:
printf(" desc = USB_DT_DEBUG\n");
break;
case USB_DT_INTERFACE_ASSOCIATION:
printf(" desc = USB_DT_INTERFACE_ASSOCIATION\n");
break;
case USB_DT_SECURITY:
printf(" desc = USB_DT_SECURITY\n");
break;
case USB_DT_KEY:
printf(" desc = USB_DT_KEY\n");
break;
case USB_DT_ENCRYPTION_TYPE:
printf(" desc = USB_DT_ENCRYPTION_TYPE\n");
break;
case USB_DT_BOS:
printf(" desc = USB_DT_BOS\n");
break;
case USB_DT_DEVICE_CAPABILITY:
printf(" desc = USB_DT_DEVICE_CAPABILITY\n");
break;
case USB_DT_WIRELESS_ENDPOINT_COMP:
printf(" desc = USB_DT_WIRELESS_ENDPOINT_COMP\n");
break;
case USB_DT_PIPE_USAGE:
printf(" desc = USB_DT_PIPE_USAGE\n");
break;
case USB_DT_SS_ENDPOINT_COMP:
printf(" desc = USB_DT_SS_ENDPOINT_COMP\n");
break;
default:
printf(" desc = unknown = 0x%x\n",
ctrl->wValue >> 8);
break;
}
break;
case USB_REQ_SET_CONFIGURATION:
printf(" req = USB_REQ_SET_CONFIGURATION\n");
break;
case USB_REQ_GET_CONFIGURATION:
printf(" req = USB_REQ_GET_CONFIGURATION\n");
break;
case USB_REQ_SET_INTERFACE:
printf(" req = USB_REQ_SET_INTERFACE\n");
break;
case USB_REQ_GET_INTERFACE:
printf(" req = USB_REQ_GET_INTERFACE\n");
break;
case USB_REQ_GET_STATUS:
printf(" req = USB_REQ_GET_STATUS\n");
break;
case USB_REQ_CLEAR_FEATURE:
printf(" req = USB_REQ_CLEAR_FEATURE\n");
break;
case USB_REQ_SET_FEATURE:
printf(" req = USB_REQ_SET_FEATURE\n");
break;
default:
printf(" req = unknown = 0x%x\n", ctrl->bRequest);
break;
}
break;
case USB_TYPE_CLASS:
switch (ctrl->bRequest) {
case GET_DEVICE_ID:
printf(" req = GET_DEVICE_ID\n");
break;
case GET_PORT_STATUS:
printf(" req = GET_PORT_STATUS\n");
break;
case SOFT_RESET:
printf(" req = SOFT_RESET\n");
break;
default:
printf(" req = unknown = 0x%x\n", ctrl->bRequest);
break;
}
break;
default:
printf(" req = unknown = 0x%x\n", ctrl->bRequest);
break;
}
}
void log_event(struct usb_raw_event *event) {
switch (event->type) {
case USB_RAW_EVENT_CONNECT:
printf("event: connect, length: %u\n", event->length);
break;
case USB_RAW_EVENT_CONTROL:
printf("event: control, length: %u\n", event->length);
log_control_request((struct usb_ctrlrequest *)&event->data[0]);
break;
case USB_RAW_EVENT_SUSPEND:
printf("event: suspend");
break;
case USB_RAW_EVENT_RESUME:
printf("event: resume");
break;
case USB_RAW_EVENT_RESET:
printf("event: reset");
break;
case USB_RAW_EVENT_DISCONNECT:
printf("event: disconnect");
break;
default:
printf("event: %d (unknown), length: %u\n", event->type, event->length);
}
}
/*----------------------------------------------------------------------*/
#define BCD_USB 0x0200
// Pretend to be Linux Gadget Printer.
#define USB_VENDOR 0x0525
#define USB_PRODUCT 0xa4a8
#define STRING_ID_MANUFACTURER 0
#define STRING_ID_PRODUCT 1
#define STRING_ID_SERIAL 2
#define STRING_ID_CONFIG 3
#define STRING_ID_INTERFACE 4
#define EP_MAX_PACKET_CONTROL 64
#define EP_MAX_PACKET_BULK 512
// Assigned dynamically.
#define EP_NUM_BULK_OUT 0x0
#define EP_NUM_BULK_IN 0x0
struct usb_device_descriptor usb_device = {
.bLength = USB_DT_DEVICE_SIZE,
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = __constant_cpu_to_le16(BCD_USB),
.bDeviceClass = 0,
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.bMaxPacketSize0 = EP_MAX_PACKET_CONTROL,
.idVendor = __constant_cpu_to_le16(USB_VENDOR),
.idProduct = __constant_cpu_to_le16(USB_PRODUCT),
.bcdDevice = 0,
.iManufacturer = STRING_ID_MANUFACTURER,
.iProduct = STRING_ID_PRODUCT,
.iSerialNumber = STRING_ID_SERIAL,
.bNumConfigurations = 1,
};
struct usb_qualifier_descriptor usb_qualifier = {
.bLength = sizeof(struct usb_qualifier_descriptor),
.bDescriptorType = USB_DT_DEVICE_QUALIFIER,
.bcdUSB = __constant_cpu_to_le16(BCD_USB),
.bDeviceClass = 0,
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.bMaxPacketSize0 = EP_MAX_PACKET_CONTROL,
.bNumConfigurations = 1,
.bRESERVED = 0,
};
struct usb_config_descriptor usb_config = {
.bLength = USB_DT_CONFIG_SIZE,
.bDescriptorType = USB_DT_CONFIG,
.wTotalLength = 0, // computed later
.bNumInterfaces = 1,
.bConfigurationValue = 1,
.iConfiguration = STRING_ID_CONFIG,
.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
.bMaxPower = 0x32,
};
struct usb_interface_descriptor usb_interface = {
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 2,
.bInterfaceClass = USB_CLASS_PRINTER,
.bInterfaceSubClass = 1,
.bInterfaceProtocol = 2,
.iInterface = STRING_ID_INTERFACE,
};
struct usb_endpoint_descriptor usb_endpoint_bulk_out = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT | EP_NUM_BULK_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = EP_MAX_PACKET_BULK,
};
struct usb_endpoint_descriptor usb_endpoint_bulk_in = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN | EP_NUM_BULK_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = EP_MAX_PACKET_BULK,
};
struct usb_bos_descriptor usb_bos = {
.bLength = USB_DT_BOS_SIZE,
.bDescriptorType = USB_DT_BOS,
.wTotalLength = USB_DT_BOS_SIZE,
.bNumDeviceCaps = 0,
};
int build_config(char *data, int length, bool other_speed) {
struct usb_config_descriptor *config =
(struct usb_config_descriptor *)data;
int total_length = 0;
assert(length >= sizeof(usb_config));
memcpy(data, &usb_config, sizeof(usb_config));
data += sizeof(usb_config);
length -= sizeof(usb_config);
total_length += sizeof(usb_config);
assert(length >= sizeof(usb_interface));
memcpy(data, &usb_interface, sizeof(usb_interface));
data += sizeof(usb_interface);
length -= sizeof(usb_interface);
total_length += sizeof(usb_interface);
assert(length >= USB_DT_ENDPOINT_SIZE);
memcpy(data, &usb_endpoint_bulk_out, USB_DT_ENDPOINT_SIZE);
data += USB_DT_ENDPOINT_SIZE;
length -= USB_DT_ENDPOINT_SIZE;
total_length += USB_DT_ENDPOINT_SIZE;
assert(length >= USB_DT_ENDPOINT_SIZE);
memcpy(data, &usb_endpoint_bulk_in, USB_DT_ENDPOINT_SIZE);
data += USB_DT_ENDPOINT_SIZE;
length -= USB_DT_ENDPOINT_SIZE;
total_length += USB_DT_ENDPOINT_SIZE;
config->wTotalLength = __cpu_to_le16(total_length);
printf("config->wTotalLength: %d\n", total_length);
return total_length;
}
/*----------------------------------------------------------------------*/
bool assign_ep_address(struct usb_raw_ep_info *info,
struct usb_endpoint_descriptor *ep) {
if (usb_endpoint_num(ep) != 0)
return false; // Already assigned.
if (usb_endpoint_dir_in(ep) && !info->caps.dir_in)
return false;
if (usb_endpoint_dir_out(ep) && !info->caps.dir_out)
return false;
if (usb_endpoint_maxp(ep) > info->limits.maxpacket_limit)
return false;
switch (usb_endpoint_type(ep)) {
case USB_ENDPOINT_XFER_BULK:
if (!info->caps.type_bulk)
return false;
break;
case USB_ENDPOINT_XFER_INT:
if (!info->caps.type_int)
return false;
break;
default:
assert(false);
}
if (info->addr == USB_RAW_EP_ADDR_ANY) {
static int addr = 1;
ep->bEndpointAddress |= addr++;
} else
ep->bEndpointAddress |= info->addr;
return true;
}
void process_eps_info(int fd) {
struct usb_raw_eps_info info;
memset(&info, 0, sizeof(info));
int num = usb_raw_eps_info(fd, &info);
for (int i = 0; i < num; i++) {
printf("ep #%d:\n", i);
printf(" name: %s\n", &info.eps[i].name[0]);
printf(" addr: %u\n", info.eps[i].addr);
printf(" type: %s %s %s\n",
info.eps[i].caps.type_iso ? "iso" : "___",
info.eps[i].caps.type_bulk ? "blk" : "___",
info.eps[i].caps.type_int ? "int" : "___");
printf(" dir : %s %s\n",
info.eps[i].caps.dir_in ? "in " : "___",
info.eps[i].caps.dir_out ? "out" : "___");
printf(" maxpacket_limit: %u\n",
info.eps[i].limits.maxpacket_limit);
printf(" max_streams: %u\n", info.eps[i].limits.max_streams);
}
for (int i = 0; i < num; i++) {
if (assign_ep_address(&info.eps[i], &usb_endpoint_bulk_out))
continue;
if (assign_ep_address(&info.eps[i], &usb_endpoint_bulk_in))
continue;
}
int bulk_out_addr = usb_endpoint_num(&usb_endpoint_bulk_out);
assert(bulk_out_addr != 0);
printf("bulk_out: addr = %u\n", bulk_out_addr);
int bulk_in_addr = usb_endpoint_num(&usb_endpoint_bulk_in);
assert(bulk_in_addr != 0);
printf("bulk_in: addr = %u\n", bulk_in_addr);
}
/*----------------------------------------------------------------------*/
struct usb_raw_control_event {
struct usb_raw_event inner;
struct usb_ctrlrequest ctrl;
};
struct usb_raw_control_io {
struct usb_raw_ep_io inner;
char data[EP_MAX_PACKET_CONTROL];
};
struct usb_raw_bulk_io {
struct usb_raw_ep_io inner;
char data[EP_MAX_PACKET_BULK];
};
int ep_bulk_out = -1;
int ep_bulk_in = -1;
pthread_t ep_bulk_out_thread;
pthread_t ep_bulk_in_thread;
void *ep_bulk_out_loop(void *arg) {
int fd = (int)(long)arg;
struct usb_raw_bulk_io io;
while (true) {
assert(ep_bulk_out != -1);
io.inner.ep = ep_bulk_out;
io.inner.flags = 0;
io.inner.length = sizeof(io.data);
int rv = usb_raw_ep_read(fd, (struct usb_raw_ep_io *)&io);
printf("bulk_out: read %d bytes\n", rv);
}
return NULL;
}
void *ep_bulk_in_loop(void *arg) {
int fd = (int)(long)arg;
struct usb_raw_bulk_io io;
while (true) {
assert(ep_bulk_in != -1);
io.inner.ep = ep_bulk_in;
io.inner.flags = 0;
io.inner.length = sizeof(io.data);
for (int i = 0; i < sizeof(io.data); i++)
io.data[i] = (i % EP_MAX_PACKET_BULK) % 63;
int rv = usb_raw_ep_write(fd, (struct usb_raw_ep_io *)&io);
printf("bulk_in: wrote %d bytes\n", rv);
sleep(1);
}
return NULL;
}
bool ep0_request(int fd, struct usb_raw_control_event *event,
struct usb_raw_control_io *io) {
switch (event->ctrl.bRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
switch (event->ctrl.bRequest) {
case USB_REQ_GET_DESCRIPTOR:
switch (event->ctrl.wValue >> 8) {
case USB_DT_DEVICE:
memcpy(&io->data[0], &usb_device,
sizeof(usb_device));
io->inner.length = sizeof(usb_device);
return true;
case USB_DT_DEVICE_QUALIFIER:
memcpy(&io->data[0], &usb_qualifier,
sizeof(usb_qualifier));
io->inner.length = sizeof(usb_qualifier);
return true;
case USB_DT_CONFIG:
io->inner.length =
build_config(&io->data[0],
sizeof(io->data), false);
return true;
case USB_DT_STRING:
io->data[0] = 4;
io->data[1] = USB_DT_STRING;
if ((event->ctrl.wValue & 0xff) == 0) {
io->data[2] = 0x09;
io->data[3] = 0x04;
} else {
io->data[2] = 'x';
io->data[3] = 0x00;
}
io->inner.length = 4;
return true;
default:
printf("fail: no response\n");
exit(EXIT_FAILURE);
}
break;
case USB_REQ_GET_CONFIGURATION:
io->inner.length =
build_config(&io->data[0],
sizeof(io->data), false);
return true;
case USB_REQ_SET_CONFIGURATION:
// If CUPS is installed on the host PC, this request
// will be sent again. So don't enable endpoints nor
// create threads twice.
if (ep_bulk_out == -1) {
ep_bulk_out = usb_raw_ep_enable(fd,
&usb_endpoint_bulk_out);
printf("bulk_out: ep = #%d\n", ep_bulk_out);
}
if (ep_bulk_in == -1) {
ep_bulk_in = usb_raw_ep_enable(fd,
&usb_endpoint_bulk_in);
printf("bulk_in: ep = #%d\n", ep_bulk_in);
}
if (!ep_bulk_out_thread)
pthread_create(&ep_bulk_out_thread, 0,
ep_bulk_out_loop, (void *)(long)fd);
if (!ep_bulk_in_thread)
pthread_create(&ep_bulk_in_thread, 0,
ep_bulk_in_loop, (void *)(long)fd);
usb_raw_vbus_draw(fd, usb_config.bMaxPower);
usb_raw_configure(fd);
io->inner.length = 0;
return true;
case USB_REQ_SET_INTERFACE:
// Do nothing, as the device only has one interface.
io->inner.length = 0;
return true;
case USB_REQ_GET_INTERFACE:
io->data[0] = usb_interface.bAlternateSetting;
io->inner.length = 1;
return true;
default:
printf("fail: no response\n");
exit(EXIT_FAILURE);
}
break;
case USB_TYPE_CLASS:
switch (event->ctrl.bRequest) {
case GET_DEVICE_ID:
{
// Copy from linux/drivers/usb/gadget/legacy/printer.c
static char *pnp_string = "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
int value = strlen(pnp_string);
memcpy(&io->data[0] + 2, pnp_string, value);
value += 2;
io->data[0] = (value >> 8) & 0xFF;
io->data[1] = value & 0xFF;
io->inner.length = value;
return true;
}
case GET_PORT_STATUS:
return true;
case SOFT_RESET:
return true;
default:
printf("fail: no response\n");
exit(EXIT_FAILURE);
}
break;
case USB_TYPE_VENDOR:
switch (event->ctrl.bRequest) {
default:
printf("fail: no response\n");
exit(EXIT_FAILURE);
}
break;
default:
printf("fail: no response\n");
exit(EXIT_FAILURE);
}
}
void ep0_loop(int fd) {
while (true) {
struct usb_raw_control_event event;
event.inner.type = 0;
event.inner.length = sizeof(event.ctrl);
usb_raw_event_fetch(fd, (struct usb_raw_event *)&event);
log_event((struct usb_raw_event *)&event);
if (event.inner.type == USB_RAW_EVENT_CONNECT) {
process_eps_info(fd);
continue;
}
if (event.inner.type != USB_RAW_EVENT_CONTROL)
continue;
struct usb_raw_control_io io;
io.inner.ep = 0;
io.inner.flags = 0;
io.inner.length = 0;
bool reply = ep0_request(fd, &event, &io);
if (!reply) {
printf("ep0: stalling\n");
usb_raw_ep0_stall(fd);
continue;
}
if (event.ctrl.wLength < io.inner.length)
io.inner.length = event.ctrl.wLength;
if (event.ctrl.bRequestType & USB_DIR_IN) {
int rv = usb_raw_ep0_write(fd, (struct usb_raw_ep_io *)&io);
printf("ep0: transferred %d bytes (in)\n", rv);
} else {
int rv = usb_raw_ep0_read(fd, (struct usb_raw_ep_io *)&io);
printf("ep0: transferred %d bytes (out)\n", rv);
}
}
}
int main(int argc, char **argv) {
const char *device = "dummy_udc.0";
const char *driver = "dummy_udc";
if (argc >= 2)
device = argv[1];
if (argc >= 3)
driver = argv[2];
int fd = usb_raw_open();
usb_raw_init(fd, USB_SPEED_HIGH, driver, device);
usb_raw_run(fd);
ep0_loop(fd);
close(fd);
return 0;
}