1 /*
2  * USB Descriptor parsing functions
3  *
4  * Copyright (c) 2000-2001 Johannes Erdfelt <johannes@erdfelt.com>
5  * Copyright 2003, Chaskiel Grundman <cg2v@andrew.cmu.edu>
6  *
7  * This header file is purely internal to OpenCT.
8  */
9 #ifndef __USB_DESCRIPTORS_H__
10 #define __USB_DESCRIPTORS_H__
11 
12 #include <openct/types.h>
13 
14 /*
15  * Descriptor types
16  */
17 #define IFD_USB_DT_DEVICE                   0x01
18 #define IFD_USB_DT_CONFIG                   0x02
19 #define IFD_USB_DT_STRING                   0x03
20 #define IFD_USB_DT_INTERFACE                0x04
21 #define IFD_USB_DT_ENDPOINT                 0x05
22 
23 /*
24  * Descriptor sizes per descriptor type
25  */
26 #define IFD_USB_DT_DEVICE_SIZE              18
27 #define IFD_USB_DT_CONFIG_SIZE              9
28 #define IFD_USB_DT_INTERFACE_SIZE           9
29 #define IFD_USB_DT_ENDPOINT_SIZE            7
30 #define IFD_USB_DT_ENDPOINT_AUDIO_SIZE      9	/* Audio extension */
31 #define IFD_USB_DT_HUB_NONVAR_SIZE          7
32 
33 struct ifd_usb_descriptor_header {
34 	uint8_t bLength;
35 	uint8_t bDescriptorType;
36 };
37 
38 /* String descriptor */
39 struct ifd_usb_string_descriptor {
40 	uint8_t bLength;
41 	uint8_t bDescriptorType;
42 	uint16_t wData[1];
43 };
44 
45 /* Endpoint descriptor */
46 #define IFD_USB_MAXENDPOINTS        32
47 struct ifd_usb_endpoint_descriptor {
48 	uint8_t bLength;
49 	uint8_t bDescriptorType;
50 	uint8_t bEndpointAddress;
51 	uint8_t bmAttributes;
52 	uint16_t wMaxPacketSize;
53 	uint8_t bInterval;
54 	uint8_t bRefresh;
55 	uint8_t bSynchAddress;
56 
57 	unsigned char *extra;	/* Extra descriptors */
58 	int extralen;
59 };
60 #define IFD_USB_ENDPOINT_ADDRESS_MASK       0x0f	/* in bEndpointAddress */
61 #define IFD_USB_ENDPOINT_DIR_MASK           0x80
62 
63 #define IFD_USB_ENDPOINT_TYPE_MASK          0x03	/* in bmAttributes */
64 #define IFD_USB_ENDPOINT_TYPE_CONTROL       0
65 #define IFD_USB_ENDPOINT_TYPE_ISOCHRONOUS   1
66 #define IFD_USB_ENDPOINT_TYPE_BULK          2
67 #define IFD_USB_ENDPOINT_TYPE_INTERRUPT     3
68 
69 /* Interface descriptor */
70 #define IFD_USB_MAXINTERFACES       32
71 struct ifd_usb_interface_descriptor {
72 	uint8_t bLength;
73 	uint8_t bDescriptorType;
74 	uint8_t bInterfaceNumber;
75 	uint8_t bAlternateSetting;
76 	uint8_t bNumEndpoints;
77 	uint8_t bInterfaceClass;
78 	uint8_t bInterfaceSubClass;
79 	uint8_t bInterfaceProtocol;
80 	uint8_t iInterface;
81 
82 	struct ifd_usb_endpoint_descriptor *endpoint;
83 
84 	unsigned char *extra;	/* Extra descriptors */
85 	int extralen;
86 };
87 
88 #define IFD_USB_MAXALTSETTING       128	/* Hard limit */
89 struct ifd_usb_interface {
90 	struct ifd_usb_interface_descriptor *altsetting;
91 
92 	int num_altsetting;
93 };
94 
95 /* Configuration descriptor information.. */
96 #define IFD_USB_MAXCONFIG           8
97 struct ifd_usb_config_descriptor {
98 	uint8_t bLength;
99 	uint8_t bDescriptorType;
100 	uint16_t wTotalLength;
101 	uint8_t bNumInterfaces;
102 	uint8_t bConfigurationValue;
103 	uint8_t iConfiguration;
104 	uint8_t bmAttributes;
105 	uint8_t MaxPower;
106 
107 	struct ifd_usb_interface *interface;
108 
109 	unsigned char *extra;	/* Extra descriptors */
110 	int extralen;
111 };
112 
113 /* Device descriptor */
114 struct ifd_usb_device_descriptor {
115 	uint8_t bLength;
116 	uint8_t bDescriptorType;
117 	uint16_t bcdUSB;
118 	uint8_t bDeviceClass;
119 	uint8_t bDeviceSubClass;
120 	uint8_t bDeviceProtocol;
121 	uint8_t bMaxPacketSize0;
122 	uint16_t idVendor;
123 	uint16_t idProduct;
124 	uint16_t bcdDevice;
125 	uint8_t iManufacturer;
126 	uint8_t iProduct;
127 	uint8_t iSerialNumber;
128 	uint8_t bNumConfigurations;
129 };
130 #define IFD_USB_REQ_GET_DESCRIPTOR          0x06
131 
132 #define IFD_USB_TYPE_STANDARD               (0x00 << 5)
133 #define IFD_USB_TYPE_CLASS                  (0x01 << 5)
134 #define IFD_USB_TYPE_VENDOR                 (0x02 << 5)
135 #define IFD_USB_TYPE_RESERVED               (0x03 << 5)
136 
137 #define IFD_USB_RECIP_DEVICE                0x00
138 #define IFD_USB_RECIP_INTERFACE             0x01
139 #define IFD_USB_RECIP_ENDPOINT              0x02
140 #define IFD_USB_RECIP_OTHER                 0x03
141 
142 #define IFD_USB_ENDPOINT_IN                 0x80
143 #define IFD_USB_ENDPOINT_OUT                0x00
144 
145 #define IFD_USB_LE16_TO_CPU(x) do { unsigned char *y=(unsigned char *)(&x); x = (y[1]  << 8) | y[0] ; } while(0)
146 
147 extern int ifd_usb_get_device(ifd_device_t * dev,
148 			      struct ifd_usb_device_descriptor *d);
149 
150 extern int ifd_usb_get_config(ifd_device_t * dev,
151 			      int n, struct ifd_usb_config_descriptor *c);
152 extern void ifd_usb_free_configuration(struct ifd_usb_config_descriptor *c);
153 
154 #endif
155