a0d1c57440cadfa852d5d6fa7d02f8a3feeb2b0e
[goodfet] / client / USBEndpoint.py
1 # USBEndpoint.py
2 #
3 # Contains class definition for USBEndpoint.
4
5 class USBEndpoint:
6     direction_out               = 0x00
7     direction_in                = 0x01
8
9     transfer_type_control       = 0x00
10     transfer_type_isochronous   = 0x01
11     transfer_type_bulk          = 0x02
12     transfer_type_interrupt     = 0x03
13
14     sync_type_none              = 0x00
15     sync_type_async             = 0x01
16     sync_type_adaptive          = 0x02
17     sync_type_synchronous       = 0x03
18
19     usage_type_data             = 0x00
20     usage_type_feedback         = 0x01
21     usage_type_implicit_feedback = 0x02
22
23     def __init__(self, number, direction, transfer_type, sync_type,
24             usage_type, max_packet_size, interval, handler):
25
26         self.number             = number
27         self.direction          = direction
28         self.transfer_type      = transfer_type
29         self.sync_type          = sync_type
30         self.usage_type         = usage_type
31         self.max_packet_size    = max_packet_size
32         self.interval           = interval
33         self.handler            = handler
34
35     # see Table 9-13 of USB 2.0 spec (pdf page 297)
36     def get_descriptor(self):
37         address = (self.number & 0x0f) | (self.direction << 7) 
38         attributes = (self.transfer_type & 0x03) \
39                    | ((self.sync_type & 0x03) << 2) \
40                    | ((self.usage_type & 0x03) << 4)
41
42         d = bytearray([
43                 7,          # length of descriptor in bytes
44                 5,          # descriptor type 5 == endpoint
45                 address,
46                 attributes,
47                 (self.max_packet_size >> 8) & 0xff,
48                 self.max_packet_size & 0xff,
49                 self.interval
50         ])
51
52         return d
53