-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhcd.c
1738 lines (1450 loc) · 46 KB
/
hcd.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
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 - 2020 DisplayLink (UK) Ltd.
*/
#include "hcd.h"
#include <linux/version.h>
#include "hpal_events.h"
#include "utils.h"
static void mausb_hcd_remove(struct device *dev);
static int mausb_bus_match(struct device *dev, struct device_driver *drv);
struct mausb_hcd *mhcd;
static struct bus_type mausb_bus_type = {
.name = DRIVER_NAME,
.match = mausb_bus_match,
};
static struct device_driver mausb_driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
.bus = &mausb_bus_type,
};
static struct device *mausb_device;
static void mausb_hcd_remove(struct device *dev)
{
struct usb_hcd *hcd;
struct usb_hcd *shared_hcd;
shared_hcd = mhcd->hcd_ss_ctx->hcd;
if (shared_hcd) {
usb_remove_hcd(shared_hcd);
usb_put_hcd(shared_hcd);
mhcd->hcd_ss_ctx = NULL;
}
hcd = mhcd->hcd_hs_ctx->hcd;
if (hcd) {
usb_remove_hcd(hcd);
usb_put_hcd(hcd);
mhcd->hcd_hs_ctx = NULL;
}
kfree(mhcd);
mhcd = NULL;
}
static int mausb_bus_match(struct device *dev, struct device_driver *drv)
{
return !strncmp(dev->bus->name, drv->name, strlen(drv->name));
}
int mausb_host_driver_init(void)
{
int retval = bus_register(&mausb_bus_type);
if (retval)
return retval;
retval = driver_register(&mausb_driver);
if (retval)
goto cleanup_bus;
mausb_device = kzalloc(sizeof(struct device), GFP_KERNEL);
if (!mausb_device) {
retval = -ENOMEM;
goto cleanup_driver;
}
dev_set_name(mausb_device, DEVICE_NAME);
mausb_device->bus = &mausb_bus_type;
mausb_device->release = (void (*)(struct device *))kfree;
retval = device_register(mausb_device);
if (retval) {
put_device(mausb_device);
goto cleanup_driver;
}
retval = mausb_hcd_create_and_add(mausb_device);
if (retval)
goto cleanup;
return retval;
cleanup:
device_unregister(mausb_device);
cleanup_driver:
driver_unregister(&mausb_driver);
cleanup_bus:
bus_unregister(&mausb_bus_type);
return retval;
}
void mausb_host_driver_deinit(void)
{
mausb_hcd_remove(mausb_device);
device_unregister(mausb_device);
driver_unregister(&mausb_driver);
bus_unregister(&mausb_bus_type);
}
void mausb_port_has_changed(const enum mausb_device_type device_type,
const enum mausb_device_speed device_speed,
void *ma_dev)
{
struct usb_hcd *hcd;
unsigned long flags;
struct mausb_device *dev = ma_dev;
u8 port_number = dev->port_number;
spin_lock_irqsave(&mhcd->lock, flags);
if (device_type == USB20HUB || device_speed < SUPER_SPEED) {
mhcd->hcd_hs_ctx->ma_devs[port_number].port_status |=
USB_PORT_STAT_CONNECTION | (1 <<
USB_PORT_FEAT_C_CONNECTION);
if (device_speed == LOW_SPEED) {
mhcd->hcd_hs_ctx->ma_devs[port_number].port_status |=
MAUSB_PORT_20_STATUS_LOW_SPEED;
mhcd->hcd_hs_ctx->ma_devs[port_number].dev_speed =
LOW_SPEED;
} else if (device_speed == HIGH_SPEED) {
mhcd->hcd_hs_ctx->ma_devs[port_number].port_status |=
MAUSB_PORT_20_STATUS_HIGH_SPEED;
mhcd->hcd_hs_ctx->ma_devs[port_number].dev_speed =
HIGH_SPEED;
}
hcd = mhcd->hcd_hs_ctx->hcd;
mhcd->hcd_hs_ctx->ma_devs[port_number].ma_dev = ma_dev;
} else {
mhcd->hcd_ss_ctx->ma_devs[port_number].port_status |=
USB_PORT_STAT_CONNECTION | (1 <<
USB_PORT_FEAT_C_CONNECTION);
mhcd->hcd_ss_ctx->ma_devs[port_number].dev_speed = SUPER_SPEED;
hcd = mhcd->hcd_ss_ctx->hcd;
mhcd->hcd_ss_ctx->ma_devs[port_number].ma_dev = ma_dev;
}
spin_unlock_irqrestore(&mhcd->lock, flags);
usb_hcd_poll_rh_status(hcd);
}
void mausb_hcd_disconnect(const u8 port_number,
const enum mausb_device_type device_type,
const enum mausb_device_speed device_speed)
{
struct usb_hcd *hcd;
unsigned long flags;
if (port_number >= NUMBER_OF_PORTS) {
dev_err(mausb_host_dev.this_device, "port number out of range, port_number=%x",
port_number);
return;
}
spin_lock_irqsave(&mhcd->lock, flags);
if (device_type == USB20HUB || device_speed < SUPER_SPEED) {
mhcd->hcd_hs_ctx->ma_devs[port_number].port_status &=
~(USB_PORT_STAT_CONNECTION);
mhcd->hcd_hs_ctx->ma_devs[port_number].port_status &=
~(USB_PORT_STAT_ENABLE);
mhcd->hcd_hs_ctx->ma_devs[port_number].port_status |=
(1 << USB_PORT_FEAT_C_CONNECTION);
hcd = mhcd->hcd_hs_ctx->hcd;
} else {
mhcd->hcd_ss_ctx->ma_devs[port_number].port_status &=
~(USB_PORT_STAT_CONNECTION);
mhcd->hcd_ss_ctx->ma_devs[port_number].port_status &=
~(USB_PORT_STAT_ENABLE);
mhcd->hcd_ss_ctx->ma_devs[port_number].port_status |=
(1 << USB_PORT_FEAT_C_CONNECTION);
hcd = mhcd->hcd_ss_ctx->hcd;
}
spin_unlock_irqrestore(&mhcd->lock, flags);
if (!hcd)
return;
usb_hcd_poll_rh_status(hcd);
}
static const char driver_name[] = "MA-USB host controller";
static void mausb_get_hub_descriptor(struct usb_hcd *hcd, u16 type_req,
u16 value, u16 index,
char *buff, u16 length);
static void mausb_set_port_feature(struct usb_hcd *hcd, u16 type_req,
u16 value, u16 index, char *buff,
u16 length);
static void mausb_get_port_status(struct usb_hcd *hcd, u16 type_req,
u16 value, u16 index, char *buff,
u16 length);
static void mausb_clear_port_feature(struct usb_hcd *hcd, u16 type_req,
u16 value, u16 index,
char *buff, u16 length);
static void mausb_get_hub_status(struct usb_hcd *hcd, u16 type_req,
u16 value, u16 index, char *buff,
u16 length);
static int mausb_add_endpoint(struct usb_hcd *hcd, struct usb_device *dev,
struct usb_host_endpoint *endpoint);
static int mausb_address_device(struct usb_hcd *hcd, struct usb_device *dev);
static int mausb_alloc_dev(struct usb_hcd *hcd, struct usb_device *dev);
static int mausb_check_bandwidth(struct usb_hcd *hcd, struct usb_device *dev);
static void mausb_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *dev);
static int mausb_drop_endpoint(struct usb_hcd *hcd, struct usb_device *dev,
struct usb_host_endpoint *endpoint);
static int mausb_enable_device(struct usb_hcd *hcd, struct usb_device *dev);
static void mausb_endpoint_disable(struct usb_hcd *hcd,
struct usb_host_endpoint *endpoint);
static void mausb_endpoint_reset(struct usb_hcd *hcd,
struct usb_host_endpoint *endpoint);
static void mausb_free_dev(struct usb_hcd *hcd, struct usb_device *dev);
static int mausb_hcd_bus_resume(struct usb_hcd *hcd);
static int mausb_hcd_bus_suspend(struct usb_hcd *hcd);
static int mausb_hcd_get_frame_number(struct usb_hcd *hcd);
static int mausb_hcd_hub_control(struct usb_hcd *hcd, u16 type_req,
u16 value, u16 index, char *buff,
u16 length);
static int mausb_hcd_hub_status(struct usb_hcd *hcd, char *buff);
static int mausb_hcd_reset(struct usb_hcd *hcd);
static int mausb_hcd_start(struct usb_hcd *hcd);
static void mausb_hcd_stop(struct usb_hcd *hcd);
static int mausb_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
int status);
static int mausb_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
gfp_t mem_flags);
static int mausb_hub_update_device(struct usb_hcd *hcd, struct usb_device *dev,
struct usb_tt *tt, gfp_t mem_flags);
static int mausb_reset_device(struct usb_hcd *hcd, struct usb_device *dev);
static int mausb_update_device(struct usb_hcd *hcd, struct usb_device *dev);
static void mausb_print_urb(struct urb *request)
{
dev_vdbg(&request->dev->dev, "URB: urb=%p, ep_handle=%#x, packet_num=%d, setup_dma=%pad, is_setup_packet=%d, is_ep=%d, is_sg=%d, num_sgs=%d, num_mapped_sgs=%d, status=%d, is_transfer_buffer=%d, transfer_buffer_length=%d, is_transfer_dma=%pad, transfer_flags=%d, is_hcpriv=%d",
request, ((struct mausb_endpoint_ctx *)
request->ep->hcpriv)->ep_handle,
request->number_of_packets, &request->setup_dma,
request->setup_packet ? 1 : 0, request->ep ? 1 : 0,
request->sg ? 1 : 0, request->num_sgs,
request->num_mapped_sgs, request->status,
request->transfer_buffer ? 1 : 0,
request->transfer_buffer_length,
&request->transfer_dma, request->transfer_flags,
(request->ep && request->ep->hcpriv) ? 1 : 0);
}
static const struct hc_driver mausb_hc_driver = {
.description = driver_name,
.product_desc = driver_name,
.flags = HCD_USB3 | HCD_SHARED,
.hcd_priv_size = sizeof(struct hub_ctx),
.reset = mausb_hcd_reset,
.start = mausb_hcd_start,
.stop = mausb_hcd_stop,
.urb_enqueue = mausb_hcd_urb_enqueue,
.urb_dequeue = mausb_hcd_urb_dequeue,
.get_frame_number = mausb_hcd_get_frame_number,
.hub_status_data = mausb_hcd_hub_status,
.hub_control = mausb_hcd_hub_control,
.update_hub_device = mausb_hub_update_device,
.bus_suspend = mausb_hcd_bus_suspend,
.bus_resume = mausb_hcd_bus_resume,
.alloc_dev = mausb_alloc_dev,
.free_dev = mausb_free_dev,
.enable_device = mausb_enable_device,
.update_device = mausb_update_device,
.reset_device = mausb_reset_device,
.add_endpoint = mausb_add_endpoint,
.drop_endpoint = mausb_drop_endpoint,
.check_bandwidth = mausb_check_bandwidth,
.reset_bandwidth = mausb_reset_bandwidth,
.address_device = mausb_address_device,
.endpoint_disable = mausb_endpoint_disable,
.endpoint_reset = mausb_endpoint_reset,
#ifdef ISOCH_CALLBACKS
.sec_event_ring_setup = mausb_sec_event_ring_setup,
.sec_event_ring_cleanup = mausb_sec_event_ring_cleanup,
.get_sec_event_ring_phys_addr = mausb_get_sec_event_ring_phys_addr,
.get_xfer_ring_phys_addr = mausb_get_xfer_ring_phys_addr,
.get_core_id = mausb_get_core_id
#endif /* ISOCH_CALLBACKS */
};
static struct {
struct usb_bos_descriptor bos;
struct usb_ss_cap_descriptor ss_cap;
} usb3_bos_desc = {
.bos = {
.bLength = USB_DT_BOS_SIZE,
.bDescriptorType = USB_DT_BOS,
.wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
.bNumDeviceCaps = 1
},
.ss_cap = {
.bLength = USB_DT_USB_SS_CAP_SIZE,
.bDescriptorType = USB_DT_DEVICE_CAPABILITY,
.bDevCapabilityType = USB_SS_CAP_TYPE,
.wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
.bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION)
}
};
static int get_root_hub_port_number(struct usb_device *dev, u8 *port_number)
{
struct usb_device *first_hub_device = dev;
if (!dev->parent) {
(*port_number) = 0;
return -EINVAL;
}
while (first_hub_device->parent->parent)
first_hub_device = first_hub_device->parent;
(*port_number) = first_hub_device->portnum - 1;
return 0;
}
static int usb_to_mausb_device_speed(u8 speed)
{
switch (speed) {
case USB_SPEED_LOW:
return MA_USB_SPEED_LOW_SPEED;
case USB_SPEED_FULL:
return MA_USB_SPEED_FULL_SPEED;
case USB_SPEED_WIRELESS:
case USB_SPEED_HIGH:
return MA_USB_SPEED_HIGH_SPEED;
case USB_SPEED_SUPER:
return MA_USB_SPEED_SUPER_SPEED;
#if KERNEL_VERSION(4, 6, 0) <= LINUX_VERSION_CODE
case USB_SPEED_SUPER_PLUS:
return MA_USB_SPEED_SUPER_SPEED_PLUS;
#endif /* #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0) */
default:
return -EINVAL;
}
}
static struct mausb_usb_device_ctx *mausb_find_usb_device(struct mausb_dev
*mdevs, void *dev_addr)
{
struct rb_node *node = mdevs->usb_devices.rb_node;
while (node) {
struct mausb_usb_device_ctx *usb_device =
rb_entry(node, struct mausb_usb_device_ctx, rb_node);
if (dev_addr < usb_device->dev_addr)
node = usb_device->rb_node.rb_left;
else if (dev_addr > usb_device->dev_addr)
node = usb_device->rb_node.rb_right;
else
return usb_device;
}
return NULL;
}
static int mausb_insert_usb_device(struct mausb_dev *mdevs,
struct mausb_usb_device_ctx *usb_device)
{
struct rb_node **new_node = &mdevs->usb_devices.rb_node;
struct rb_node *parent = NULL;
struct mausb_usb_device_ctx *current_usb_device = NULL;
while (*new_node) {
parent = *new_node;
current_usb_device = rb_entry(*new_node,
struct mausb_usb_device_ctx,
rb_node);
if (usb_device->dev_addr < current_usb_device->dev_addr)
new_node = &((*new_node)->rb_left);
else if (usb_device->dev_addr > current_usb_device->dev_addr)
new_node = &((*new_node)->rb_right);
else
return -EEXIST;
}
rb_link_node(&usb_device->rb_node, parent, new_node);
rb_insert_color(&usb_device->rb_node, &mdevs->usb_devices);
return 0;
}
static int mausb_hcd_get_frame_number(struct usb_hcd *hcd)
{
return 0;
}
static int mausb_hcd_reset(struct usb_hcd *hcd)
{
if (usb_hcd_is_primary_hcd(hcd)) {
hcd->speed = HCD_USB2;
hcd->self.root_hub->speed = USB_SPEED_HIGH;
} else {
hcd->speed = HCD_USB3;
hcd->self.root_hub->speed = USB_SPEED_SUPER;
}
hcd->self.no_sg_constraint = 1;
hcd->self.sg_tablesize = UINT_MAX;
return 0;
}
static int mausb_hcd_start(struct usb_hcd *hcd)
{
hcd->power_budget = 0;
hcd->uses_new_polling = 1;
return 0;
}
static void mausb_hcd_stop(struct usb_hcd *hcd)
{
dev_vdbg(mausb_host_dev.this_device, "HCD stopped");
}
static int mausb_hcd_hub_status(struct usb_hcd *hcd, char *buff)
{
int retval;
int changed;
int i;
struct hub_ctx *hub;
unsigned long flags;
hub = (struct hub_ctx *)hcd->hcd_priv;
retval = DIV_ROUND_UP(NUMBER_OF_PORTS + 1, 8);
changed = 0;
memset(buff, 0, (unsigned int)retval);
spin_lock_irqsave(&mhcd->lock, flags);
if (!HCD_HW_ACCESSIBLE(hcd)) {
dev_info(mausb_host_dev.this_device, "hcd not accessible, hcd speed=%d",
hcd->speed);
spin_unlock_irqrestore(&mhcd->lock, flags);
return 0;
}
for (i = 0; i < NUMBER_OF_PORTS; ++i) {
if ((hub->ma_devs[i].port_status & PORT_C_MASK)) {
buff[(i + 1) / 8] |= 1 << (i + 1) % 8;
changed = 1;
}
}
dev_info(mausb_host_dev.this_device, "Usb %d.0 : changed=%d, retval=%d",
(hcd->speed == HCD_USB2) ? 2 : 3, changed, retval);
if (hcd->state == HC_STATE_SUSPENDED && changed == 1) {
dev_info(mausb_host_dev.this_device, "hcd state is suspended");
usb_hcd_resume_root_hub(hcd);
}
spin_unlock_irqrestore(&mhcd->lock, flags);
return changed ? retval : 0;
}
static int mausb_hcd_bus_resume(struct usb_hcd *hcd)
{
unsigned long flags;
spin_lock_irqsave(&mhcd->lock, flags);
if (!HCD_HW_ACCESSIBLE(hcd)) {
dev_info(mausb_host_dev.this_device, "hcd not accessible, hcd speed=%d",
hcd->speed);
spin_unlock_irqrestore(&mhcd->lock, flags);
return -ESHUTDOWN;
}
hcd->state = HC_STATE_RUNNING;
spin_unlock_irqrestore(&mhcd->lock, flags);
return 0;
}
static int mausb_hcd_bus_suspend(struct usb_hcd *hcd)
{
unsigned long flags;
spin_lock_irqsave(&mhcd->lock, flags);
hcd->state = HC_STATE_SUSPENDED;
spin_unlock_irqrestore(&mhcd->lock, flags);
return 0;
}
static int mausb_hcd_hub_control(struct usb_hcd *hcd, u16 type_req,
u16 value, u16 index, char *buff,
u16 length)
{
int retval = 0;
unsigned long flags;
bool invalid_rhport = false;
index = ((__u8)(index & 0x00ff));
if (index < 1 || index > NUMBER_OF_PORTS)
invalid_rhport = true;
dev_vdbg(mausb_host_dev.this_device, "TypeReq=%d", type_req);
spin_lock_irqsave(&mhcd->lock, flags);
if (!HCD_HW_ACCESSIBLE(hcd)) {
dev_err(mausb_host_dev.this_device, "hcd not accessible, hcd speed=%d",
hcd->speed);
spin_unlock_irqrestore(&mhcd->lock, flags);
return -ETIMEDOUT;
}
switch (type_req) {
case ClearHubFeature:
break;
case ClearPortFeature:
if (invalid_rhport)
goto invalid_port;
mausb_clear_port_feature(hcd, type_req, value, index, buff,
length);
break;
case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
memcpy(buff, &usb3_bos_desc, sizeof(usb3_bos_desc));
retval = sizeof(usb3_bos_desc);
break;
case GetHubDescriptor:
mausb_get_hub_descriptor(hcd, type_req, value, index, buff,
length);
break;
case GetHubStatus:
mausb_get_hub_status(hcd, type_req, value, index, buff,
length);
break;
case GetPortStatus:
if (invalid_rhport)
goto invalid_port;
mausb_get_port_status(hcd, type_req, value, index, buff,
length);
break;
case SetHubFeature:
retval = -EPIPE;
break;
case SetPortFeature:
if (invalid_rhport)
goto invalid_port;
mausb_set_port_feature(hcd, type_req, value, index, buff,
length);
break;
default:
retval = -EPIPE;
}
invalid_port:
spin_unlock_irqrestore(&mhcd->lock, flags);
return retval;
}
static int mausb_validate_urb(struct urb *urb)
{
if (!urb) {
dev_err(mausb_host_dev.this_device, "urb is NULL");
return -EINVAL;
}
if (!urb->ep->hcpriv) {
dev_err(mausb_host_dev.this_device, "urb->ep->hcpriv is NULL");
return -EINVAL;
}
if (!urb->ep->enabled) {
dev_err(mausb_host_dev.this_device, "Endpoint not enabled");
return -EINVAL;
}
return 0;
}
static int mausb_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
gfp_t mem_flags)
{
struct mausb_endpoint_ctx *endpoint_ctx;
struct mausb_device *ma_dev;
struct mausb_urb_ctx *urb_ctx;
int status = 0;
if (mausb_validate_urb(urb) < 0) {
dev_err(&urb->dev->dev, "Hpal urb enqueue failed");
return -EPROTO;
}
endpoint_ctx = urb->ep->hcpriv;
ma_dev = endpoint_ctx->ma_dev;
if (atomic_read(&ma_dev->unresponsive_client)) {
dev_err(&urb->dev->dev, "Client is not responsive anymore - finish urb immediately");
return -EHOSTDOWN;
}
urb->hcpriv = hcd;
dev_vdbg(&urb->dev->dev, "ep_handle=%#x, dev_handle=%#x, urb_reject=%d",
endpoint_ctx->ep_handle, endpoint_ctx->dev_handle,
atomic_read(&urb->reject));
status = mausb_insert_urb_in_tree(urb, true);
if (status) {
dev_err(&urb->dev->dev, "Hpal urb enqueue failed");
return status;
}
atomic_inc(&urb->use_count);
mausb_print_urb(urb);
status = mausb_data_req_enqueue_event(ma_dev, endpoint_ctx->ep_handle,
urb);
if (status < 0) {
urb_ctx = mausb_unlink_and_delete_urb_from_tree(urb, status);
atomic_dec(&urb->use_count);
if (urb_ctx) {
mausb_uninit_data_iterator(&urb_ctx->iterator);
kfree(urb_ctx);
}
}
return status;
}
static int mausb_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
int status)
{
struct mausb_endpoint_ctx *endpoint_ctx;
struct mausb_device *ma_dev;
struct mausb_urb_ctx *urb_ctx;
dev_info(&urb->dev->dev, "Urb=%p", urb);
urb_ctx = mausb_unlink_and_delete_urb_from_tree(urb, status);
if (!urb_ctx) {
dev_warn(mausb_host_dev.this_device, "Urb=%p is not in tree",
urb);
return 0;
}
endpoint_ctx = urb->ep->hcpriv;
ma_dev = endpoint_ctx->ma_dev;
queue_work(ma_dev->workq, &urb_ctx->work);
return 0;
}
void mausb_hcd_urb_complete(struct urb *urb, u32 actual_length, int status)
{
struct mausb_urb_ctx *urb_ctx =
mausb_unlink_and_delete_urb_from_tree(urb, status);
if (urb_ctx) {
mausb_uninit_data_iterator(&urb_ctx->iterator);
kfree(urb_ctx);
urb->status = status;
urb->actual_length = actual_length;
atomic_dec(&urb->use_count);
usb_hcd_giveback_urb(urb->hcpriv, urb, urb->status);
}
}
int mausb_hcd_create_and_add(struct device *dev)
{
int retval;
struct usb_hcd *hcd_ss;
struct usb_hcd *hcd_hs;
hcd_hs = usb_create_hcd(&mausb_hc_driver, dev, dev_name(dev));
if (!hcd_hs)
return -ENOMEM;
mhcd = kzalloc(sizeof(*mhcd), GFP_ATOMIC);
if (!mhcd) {
retval = -ENOMEM;
goto cleanup_hcd_hs;
}
spin_lock_init(&mhcd->lock);
hcd_hs->has_tt = 1;
mhcd->hcd_hs_ctx = (struct hub_ctx *)hcd_hs->hcd_priv;
mhcd->hcd_hs_ctx->hcd = hcd_hs;
memset(mhcd->hcd_hs_ctx->ma_devs, 0,
sizeof(struct mausb_dev) * NUMBER_OF_PORTS);
retval = usb_add_hcd(hcd_hs, 0, 0);
if (retval) {
dev_err(dev, "usb_add_hcd failed");
goto cleanup_mhcd;
}
hcd_ss = usb_create_shared_hcd(&mausb_hc_driver, dev, dev_name(dev),
hcd_hs);
if (!hcd_ss) {
retval = -ENOMEM;
goto remove_hcd_hs;
}
mhcd->hcd_ss_ctx = (struct hub_ctx *)hcd_ss->hcd_priv;
mhcd->hcd_ss_ctx->hcd = hcd_ss;
memset(mhcd->hcd_ss_ctx->ma_devs, 0,
sizeof(struct mausb_dev) * NUMBER_OF_PORTS);
retval = usb_add_hcd(hcd_ss, 0, 0);
if (retval) {
dev_err(dev, "usb_add_hcd failed");
goto cleanup;
}
return retval;
cleanup:
usb_put_hcd(hcd_ss);
remove_hcd_hs:
usb_remove_hcd(hcd_hs);
cleanup_mhcd:
kfree(mhcd);
mhcd = NULL;
cleanup_hcd_hs:
usb_put_hcd(hcd_hs);
return retval;
}
static void mausb_get_hub_descriptor(struct usb_hcd *hcd, u16 type_req,
u16 value, u16 index,
char *buff, u16 length)
{
u8 width;
struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buff;
memset(desc, 0, sizeof(struct usb_hub_descriptor));
if (hcd->speed == HCD_USB3) {
desc->bDescriptorType = USB_DT_SS_HUB;
desc->bDescLength = 12;
desc->wHubCharacteristics =
cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
desc->bNbrPorts = NUMBER_OF_PORTS;
desc->u.ss.bHubHdrDecLat = 0x04;
desc->u.ss.DeviceRemovable = cpu_to_le16(0xffff);
} else {
desc->bDescriptorType = USB_DT_HUB;
desc->wHubCharacteristics =
cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
desc->bNbrPorts = NUMBER_OF_PORTS;
width = (u8)(desc->bNbrPorts / 8 + 1);
desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * width;
memset(&desc->u.hs.DeviceRemovable[0], 0, width);
memset(&desc->u.hs.DeviceRemovable[width], 0xff, width);
}
}
static void mausb_set_port_feature(struct usb_hcd *hcd, u16 type_req,
u16 value, u16 index, char *buff,
u16 length)
{
struct hub_ctx *hub = (struct hub_ctx *)hcd->hcd_priv;
index = ((__u8)(index & 0x00ff));
switch (value) {
case USB_PORT_FEAT_LINK_STATE:
dev_vdbg(mausb_host_dev.this_device, "USB_PORT_FEAT_LINK_STATE");
if (hcd->speed == HCD_USB3) {
if ((hub->ma_devs[index - 1].port_status &
USB_SS_PORT_STAT_POWER) != 0) {
hub->ma_devs[index - 1].port_status |=
(1U << value);
}
} else {
if ((hub->ma_devs[index - 1].port_status &
USB_PORT_STAT_POWER) != 0) {
hub->ma_devs[index - 1].port_status |=
(1U << value);
}
}
break;
case USB_PORT_FEAT_U1_TIMEOUT:
case USB_PORT_FEAT_U2_TIMEOUT:
break;
case USB_PORT_FEAT_SUSPEND:
break;
case USB_PORT_FEAT_POWER:
dev_vdbg(mausb_host_dev.this_device, "USB_PORT_FEAT_POWER");
if (hcd->speed == HCD_USB3) {
hub->ma_devs[index - 1].port_status |=
USB_SS_PORT_STAT_POWER;
} else {
hub->ma_devs[index - 1].port_status |=
USB_PORT_STAT_POWER;
}
break;
case USB_PORT_FEAT_BH_PORT_RESET:
dev_dbg(mausb_host_dev.this_device, "USB_PORT_FEAT_BH_PORT_RESET");
#if KERNEL_VERSION(5, 4, 0) <= LINUX_VERSION_CODE
fallthrough;
#else
do {} while (0); /* fallthrough */
#endif
case USB_PORT_FEAT_RESET:
dev_vdbg(mausb_host_dev.this_device, "USB_PORT_FEAT_RESET hcd->speed=%d",
hcd->speed);
if (hcd->speed == HCD_USB3) {
hub->ma_devs[index - 1].port_status = 0;
hub->ma_devs[index - 1].port_status =
(USB_SS_PORT_STAT_POWER |
USB_PORT_STAT_CONNECTION | USB_PORT_STAT_RESET);
} else if (hub->ma_devs[index - 1].port_status
& USB_PORT_STAT_ENABLE) {
hub->ma_devs[index - 1].port_status &=
~(USB_PORT_STAT_ENABLE |
USB_PORT_STAT_LOW_SPEED |
USB_PORT_STAT_HIGH_SPEED);
}
#if KERNEL_VERSION(5, 4, 0) <= LINUX_VERSION_CODE
fallthrough;
#else
do {} while (0); /* fallthrough */
#endif
default:
dev_vdbg(mausb_host_dev.this_device, "Default value=%d", value);
if (hcd->speed == HCD_USB3) {
if ((hub->ma_devs[index - 1].port_status &
USB_SS_PORT_STAT_POWER) != 0) {
hub->ma_devs[index - 1].port_status |=
(1U << value);
}
} else {
if ((hub->ma_devs[index - 1].port_status &
USB_PORT_STAT_POWER) != 0) {
hub->ma_devs[index - 1].port_status |=
(1U << value);
}
}
}
}
static void mausb_get_port_status(struct usb_hcd *hcd, u16 type_req,
u16 value, u16 index, char *buff,
u16 length)
{
u8 dev_speed;
struct hub_ctx *hub = (struct hub_ctx *)hcd->hcd_priv;
index = ((__u8)(index & 0x00ff));
if ((hub->ma_devs[index - 1].port_status &
(1 << USB_PORT_FEAT_RESET)) != 0) {
dev_info(mausb_host_dev.this_device, "Finished reset hcd->speed=%d",
hcd->speed);
dev_speed = hub->ma_devs[index - 1].dev_speed;
switch (dev_speed) {
case LOW_SPEED:
hub->ma_devs[index - 1].port_status |=
USB_PORT_STAT_LOW_SPEED;
break;
case HIGH_SPEED:
hub->ma_devs[index - 1].port_status |=
USB_PORT_STAT_HIGH_SPEED;
break;
default:
dev_info(mausb_host_dev.this_device, "Not updating port_status for device speed %d",
dev_speed);
}
hub->ma_devs[index - 1].port_status |=
(1 << USB_PORT_FEAT_C_RESET) | USB_PORT_STAT_ENABLE;
hub->ma_devs[index - 1].port_status &=
~(1 << USB_PORT_FEAT_RESET);
}
((__le16 *)buff)[0] = cpu_to_le16(hub->ma_devs[index - 1].port_status);
((__le16 *)buff)[1] =
cpu_to_le16(hub->ma_devs[index - 1].port_status >> 16);
dev_vdbg(mausb_host_dev.this_device, "port_status=%d",
hub->ma_devs[index - 1].port_status);
}
static void mausb_clear_port_feature(struct usb_hcd *hcd, u16 type_req,
u16 value, u16 index,
char *buff, u16 length)
{
struct hub_ctx *hub = (struct hub_ctx *)hcd->hcd_priv;
index = ((__u8)(index & 0x00ff));
switch (value) {
case USB_PORT_FEAT_SUSPEND:
break;
case USB_PORT_FEAT_POWER:
dev_vdbg(mausb_host_dev.this_device, "USB_PORT_FEAT_POWER");
if (hcd->speed == HCD_USB3) {
hub->ma_devs[index - 1].port_status &=
~USB_SS_PORT_STAT_POWER;
} else {
hub->ma_devs[index - 1].port_status &=
~USB_PORT_STAT_POWER;
}
break;
case USB_PORT_FEAT_RESET:
case USB_PORT_FEAT_C_RESET:
default:
dev_vdbg(mausb_host_dev.this_device, "Default value: %d",
value);
hub->ma_devs[index - 1].port_status &= ~(1 << value);
}
}
static void mausb_get_hub_status(struct usb_hcd *hcd, u16 type_req,
u16 value, u16 index, char *buff,
u16 length)
{
*(__le32 *)buff = cpu_to_le32(0);
}
static int mausb_alloc_dev(struct usb_hcd *hcd, struct usb_device *dev)
{
return 1;
}
static void mausb_free_dev(struct usb_hcd *hcd, struct usb_device *dev)
{
u8 port_number;
s16 dev_handle;
int status;
struct hub_ctx *hub = (struct hub_ctx *)hcd->hcd_priv;
struct mausb_device *ma_dev;
struct mausb_usb_device_ctx *usb_device_ctx;
struct mausb_endpoint_ctx *ep_ctx = dev->ep0.hcpriv;
status = get_root_hub_port_number(dev, &port_number);
if (status < 0 || port_number >= NUMBER_OF_PORTS) {
dev_dbg(mausb_host_dev.this_device, "port_number out of range, port_number=%x",
port_number);
return;
}
ma_dev = hub->ma_devs[port_number].ma_dev;
if (!ma_dev) {
dev_err(mausb_host_dev.this_device, "MAUSB device not found on port_number=%d",
port_number);
return;
}
usb_device_ctx = mausb_find_usb_device(&hub->ma_devs[port_number], dev);
if (!usb_device_ctx) {
dev_warn(mausb_host_dev.this_device, "device_ctx is not found");
return;
}
dev_handle = usb_device_ctx->dev_handle;
if (atomic_read(&ma_dev->unresponsive_client)) {
dev_err(mausb_host_dev.this_device, "Client is not responsive anymore - free usbdevice immediately");
dev->ep0.hcpriv = NULL;
kfree(ep_ctx);
goto free_dev;
}
if (ep_ctx) {
mausb_epinactivate_event_to_user(ma_dev, dev_handle,
ep_ctx->ep_handle);
mausb_cleartransfers_event_to_user(ma_dev, dev_handle,