summaryrefslogtreecommitdiff
path: root/firmware/usbstack/drivers/device
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/usbstack/drivers/device')
-rw-r--r--firmware/usbstack/drivers/device/usb_serial.c56
-rw-r--r--firmware/usbstack/drivers/device/usb_storage.c85
-rw-r--r--firmware/usbstack/drivers/device/usb_storage.h1
3 files changed, 99 insertions, 43 deletions
diff --git a/firmware/usbstack/drivers/device/usb_serial.c b/firmware/usbstack/drivers/device/usb_serial.c
index 7750ccb8b7..7299dc6765 100644
--- a/firmware/usbstack/drivers/device/usb_serial.c
+++ b/firmware/usbstack/drivers/device/usb_serial.c
@@ -81,7 +81,7 @@ static struct usb_interface_descriptor serial_bulk_interface_desc = {
81 .iInterface = GS_DATA_STR_ID, 81 .iInterface = GS_DATA_STR_ID,
82}; 82};
83 83
84static struct usb_endpoint_descriptor serial_fullspeed_in_desc = { 84static struct usb_endpoint_descriptor serial_fs_in_desc = {
85 .bLength = USB_DT_ENDPOINT_SIZE, 85 .bLength = USB_DT_ENDPOINT_SIZE,
86 .bDescriptorType = USB_DT_ENDPOINT, 86 .bDescriptorType = USB_DT_ENDPOINT,
87 .bEndpointAddress = USB_DIR_IN, 87 .bEndpointAddress = USB_DIR_IN,
@@ -89,7 +89,7 @@ static struct usb_endpoint_descriptor serial_fullspeed_in_desc = {
89 .wMaxPacketSize = 8, 89 .wMaxPacketSize = 8,
90}; 90};
91 91
92static struct usb_endpoint_descriptor serial_fullspeed_out_desc = { 92static struct usb_endpoint_descriptor serial_fs_out_desc = {
93 .bLength = USB_DT_ENDPOINT_SIZE, 93 .bLength = USB_DT_ENDPOINT_SIZE,
94 .bDescriptorType = USB_DT_ENDPOINT, 94 .bDescriptorType = USB_DT_ENDPOINT,
95 .bEndpointAddress = USB_DIR_OUT, 95 .bEndpointAddress = USB_DIR_OUT,
@@ -110,10 +110,34 @@ static struct usb_qualifier_descriptor serial_qualifier_desc = {
110 .bNumConfigurations = 1, 110 .bNumConfigurations = 1,
111}; 111};
112 112
113struct usb_descriptor_header *serial_bulk_fullspeed_function[] = { 113struct usb_descriptor_header *serial_fs_function[] = {
114 (struct usb_descriptor_header *) &serial_bulk_interface_desc, 114 (struct usb_descriptor_header *) &serial_bulk_interface_desc,
115 (struct usb_descriptor_header *) &serial_fullspeed_in_desc, 115 (struct usb_descriptor_header *) &serial_fs_in_desc,
116 (struct usb_descriptor_header *) &serial_fullspeed_out_desc, 116 (struct usb_descriptor_header *) &serial_fs_out_desc,
117 NULL,
118};
119
120/* USB 2.0 */
121static struct usb_endpoint_descriptor serial_hs_in_desc = {
122 .bLength = USB_DT_ENDPOINT_SIZE,
123 .bDescriptorType = USB_DT_ENDPOINT,
124 .bEndpointAddress = USB_DIR_IN,
125 .bmAttributes = USB_ENDPOINT_XFER_BULK,
126 .wMaxPacketSize = 512,
127};
128
129static struct usb_endpoint_descriptor serial_hs_out_desc = {
130 .bLength = USB_DT_ENDPOINT_SIZE,
131 .bDescriptorType = USB_DT_ENDPOINT,
132 .bEndpointAddress = USB_DIR_IN,
133 .bmAttributes = USB_ENDPOINT_XFER_BULK,
134 .wMaxPacketSize = 512,
135};
136
137struct usb_descriptor_header *serial_hs_function[] = {
138 (struct usb_descriptor_header *) &serial_bulk_interface_desc,
139 (struct usb_descriptor_header *) &serial_hs_in_desc,
140 (struct usb_descriptor_header *) &serial_hs_out_desc,
117 NULL, 141 NULL,
118}; 142};
119 143
@@ -126,11 +150,11 @@ struct usb_response res;
126static int config_buf(uint8_t *buf, uint8_t type, unsigned index); 150static int config_buf(uint8_t *buf, uint8_t type, unsigned index);
127static int set_config(int config); 151static int set_config(int config);
128 152
129
130struct device { 153struct device {
131 struct usb_ep* in; 154 struct usb_ep* in;
132 struct usb_ep* out; 155 struct usb_ep* out;
133 uint32_t used_config; 156 uint32_t used_config;
157 struct usb_descriptor_header** descriptors;
134}; 158};
135 159
136static struct device dev; 160static struct device dev;
@@ -153,14 +177,14 @@ void usb_serial_driver_bind(void* controler_ops)
153 /* serach and asign endpoints */ 177 /* serach and asign endpoints */
154 usb_ep_autoconfig_reset(); 178 usb_ep_autoconfig_reset();
155 179
156 dev.in = usb_ep_autoconfig(&serial_fullspeed_in_desc); 180 dev.in = usb_ep_autoconfig(&serial_fs_in_desc);
157 if (!dev.in) { 181 if (!dev.in) {
158 goto autoconf_fail; 182 goto autoconf_fail;
159 } 183 }
160 dev.in->claimed = true; 184 dev.in->claimed = true;
161 logf("usb serial: in: %s", dev.in->name); 185 logf("usb serial: in: %s", dev.in->name);
162 186
163 dev.out = usb_ep_autoconfig(&serial_fullspeed_out_desc); 187 dev.out = usb_ep_autoconfig(&serial_fs_out_desc);
164 if (!dev.out) { 188 if (!dev.out) {
165 goto autoconf_fail; 189 goto autoconf_fail;
166 } 190 }
@@ -180,7 +204,7 @@ void usb_serial_driver_bind(void* controler_ops)
180 return; 204 return;
181 205
182autoconf_fail: 206autoconf_fail:
183 logf("failed to find endpoiunts"); 207 logf("failed to find endpoints");
184} 208}
185 209
186int usb_serial_driver_request(struct usb_ctrlrequest* request) 210int usb_serial_driver_request(struct usb_ctrlrequest* request)
@@ -252,15 +276,13 @@ int usb_serial_driver_request(struct usb_ctrlrequest* request)
252void usb_serial_driver_speed(enum usb_device_speed speed) 276void usb_serial_driver_speed(enum usb_device_speed speed)
253{ 277{
254 switch (speed) { 278 switch (speed) {
255 case USB_SPEED_LOW:
256 case USB_SPEED_FULL:
257 logf("usb serial: using fullspeed");
258 break;
259 case USB_SPEED_HIGH: 279 case USB_SPEED_HIGH:
260 logf("usb serial: using highspeed"); 280 logf("usb serial: using highspeed");
281 dev.descriptors = serial_hs_function;
261 break; 282 break;
262 default: 283 default:
263 logf("speed: hmm"); 284 logf("usb serial: using fullspeed");
285 dev.descriptors = serial_fs_function;
264 break; 286 break;
265 } 287 }
266} 288}
@@ -274,7 +296,7 @@ static int config_buf(uint8_t *buf, uint8_t type, unsigned index)
274 296
275 /* TODO check index*/ 297 /* TODO check index*/
276 298
277 len = usb_stack_configdesc(&serial_bulk_config_desc, buf, BUFFER_SIZE, serial_bulk_fullspeed_function); 299 len = usb_stack_configdesc(&serial_bulk_config_desc, buf, BUFFER_SIZE, dev.descriptors);
278 if (len < 0) { 300 if (len < 0) {
279 return len; 301 return len;
280 } 302 }
@@ -288,9 +310,9 @@ static int set_config(int config)
288 310
289 /* enable endpoints */ 311 /* enable endpoints */
290 logf("setup %s", dev.in->name); 312 logf("setup %s", dev.in->name);
291 ops->enable(dev.in); 313 ops->enable(dev.in, (struct usb_endpoint_descriptor*)dev.descriptors[1]);
292 logf("setup %s", dev.out->name); 314 logf("setup %s", dev.out->name);
293 ops->enable(dev.out); 315 ops->enable(dev.out, (struct usb_endpoint_descriptor*)dev.descriptors[2]);
294 316
295 /* store config */ 317 /* store config */
296 logf("using config %d", config); 318 logf("using config %d", config);
diff --git a/firmware/usbstack/drivers/device/usb_storage.c b/firmware/usbstack/drivers/device/usb_storage.c
index 38c2b97eb0..3db379c1df 100644
--- a/firmware/usbstack/drivers/device/usb_storage.c
+++ b/firmware/usbstack/drivers/device/usb_storage.c
@@ -31,17 +31,9 @@ struct usb_device_driver usb_storage_driver = {
31 .request = usb_storage_driver_request, 31 .request = usb_storage_driver_request,
32 .suspend = NULL, 32 .suspend = NULL,
33 .resume = NULL, 33 .resume = NULL,
34 .speed = NULL, 34 .speed = usb_storage_driver_speed,
35}; 35};
36 36
37struct device {
38 struct usb_ep* in;
39 struct usb_ep* out;
40 struct usb_ep* intr;
41};
42
43static struct device dev;
44
45/*-------------------------------------------------------------------------*/ 37/*-------------------------------------------------------------------------*/
46 38
47#define PROTO_BULK 0x50 // Bulk only 39#define PROTO_BULK 0x50 // Bulk only
@@ -92,8 +84,31 @@ static struct usb_interface_descriptor storage_interface_desc = {
92 .iInterface = 0, 84 .iInterface = 0,
93}; 85};
94 86
95/* endpoint I -> bulk in */ 87static struct usb_endpoint_descriptor storage_fs_bulk_in_desc = {
96static struct usb_endpoint_descriptor storage_bulk_in_desc = { 88 .bLength = USB_DT_ENDPOINT_SIZE,
89 .bDescriptorType = USB_DT_ENDPOINT,
90 .bEndpointAddress = USB_DIR_IN,
91 .bmAttributes = USB_ENDPOINT_XFER_BULK,
92 .wMaxPacketSize = 64,
93};
94
95static struct usb_endpoint_descriptor storage_fs_bulk_out_desc = {
96 .bLength = USB_DT_ENDPOINT_SIZE,
97 .bDescriptorType = USB_DT_ENDPOINT,
98 .bEndpointAddress = USB_DIR_OUT,
99 .bmAttributes = USB_ENDPOINT_XFER_BULK,
100 .wMaxPacketSize = 64,
101};
102
103struct usb_descriptor_header *storage_fs_function[] = {
104 (struct usb_descriptor_header *) &storage_interface_desc,
105 (struct usb_descriptor_header *) &storage_fs_bulk_in_desc,
106 (struct usb_descriptor_header *) &storage_fs_bulk_out_desc,
107 NULL,
108};
109
110/* USB 2.0 */
111static struct usb_endpoint_descriptor storage_hs_bulk_in_desc = {
97 .bLength = USB_DT_ENDPOINT_SIZE, 112 .bLength = USB_DT_ENDPOINT_SIZE,
98 .bDescriptorType = USB_DT_ENDPOINT, 113 .bDescriptorType = USB_DT_ENDPOINT,
99 .bEndpointAddress = USB_DIR_IN, 114 .bEndpointAddress = USB_DIR_IN,
@@ -101,8 +116,7 @@ static struct usb_endpoint_descriptor storage_bulk_in_desc = {
101 .wMaxPacketSize = 512, 116 .wMaxPacketSize = 512,
102}; 117};
103 118
104/* endpoint II -> bulk out */ 119static struct usb_endpoint_descriptor storage_hs_bulk_out_desc = {
105static struct usb_endpoint_descriptor storage_bulk_out_desc = {
106 .bLength = USB_DT_ENDPOINT_SIZE, 120 .bLength = USB_DT_ENDPOINT_SIZE,
107 .bDescriptorType = USB_DT_ENDPOINT, 121 .bDescriptorType = USB_DT_ENDPOINT,
108 .bEndpointAddress = USB_DIR_OUT, 122 .bEndpointAddress = USB_DIR_OUT,
@@ -110,10 +124,10 @@ static struct usb_endpoint_descriptor storage_bulk_out_desc = {
110 .wMaxPacketSize = 512, 124 .wMaxPacketSize = 512,
111}; 125};
112 126
113struct usb_descriptor_header *storage_fullspeed_function[] = { 127struct usb_descriptor_header *storage_hs_function[] = {
114 (struct usb_descriptor_header *) &storage_interface_desc, 128 (struct usb_descriptor_header *) &storage_interface_desc,
115 (struct usb_descriptor_header *) &storage_bulk_in_desc, 129 (struct usb_descriptor_header *) &storage_hs_bulk_in_desc,
116 (struct usb_descriptor_header *) &storage_bulk_out_desc, 130 (struct usb_descriptor_header *) &storage_hs_bulk_out_desc,
117 NULL, 131 NULL,
118}; 132};
119 133
@@ -126,6 +140,15 @@ struct usb_response res;
126static int config_buf(uint8_t *buf, uint8_t type, unsigned index); 140static int config_buf(uint8_t *buf, uint8_t type, unsigned index);
127static int set_config(int config); 141static int set_config(int config);
128 142
143struct device {
144 struct usb_ep* in;
145 struct usb_ep* out;
146 struct usb_ep* intr;
147 struct usb_descriptor_header** descriptors;
148};
149
150static struct device dev;
151
129/*-------------------------------------------------------------------------*/ 152/*-------------------------------------------------------------------------*/
130 153
131void usb_storage_driver_init(void) 154void usb_storage_driver_init(void)
@@ -144,14 +167,14 @@ void usb_storage_driver_bind(void* controler_ops)
144 /* serach and asign endpoints */ 167 /* serach and asign endpoints */
145 usb_ep_autoconfig_reset(); 168 usb_ep_autoconfig_reset();
146 169
147 dev.in = usb_ep_autoconfig(&storage_bulk_in_desc); 170 dev.in = usb_ep_autoconfig(&storage_fs_bulk_in_desc);
148 if (!dev.in) { 171 if (!dev.in) {
149 goto autoconf_fail; 172 goto autoconf_fail;
150 } 173 }
151 dev.in->claimed = true; 174 dev.in->claimed = true;
152 logf("usb storage: in: %s", dev.in->name); 175 logf("usb storage: in: %s", dev.in->name);
153 176
154 dev.out = usb_ep_autoconfig(&storage_bulk_out_desc); 177 dev.out = usb_ep_autoconfig(&storage_fs_bulk_out_desc);
155 if (!dev.out) { 178 if (!dev.out) {
156 goto autoconf_fail; 179 goto autoconf_fail;
157 } 180 }
@@ -234,6 +257,20 @@ int usb_storage_driver_request(struct usb_ctrlrequest* request)
234 return ret; 257 return ret;
235} 258}
236 259
260void usb_storage_driver_speed(enum usb_device_speed speed)
261{
262 switch (speed) {
263 case USB_SPEED_HIGH:
264 logf("usb storage: using highspeed");
265 dev.descriptors = storage_hs_function;
266 break;
267 default:
268 logf("usb storage: using fullspeed");
269 dev.descriptors = storage_fs_function;
270 break;
271 }
272}
273
237/*-------------------------------------------------------------------------*/ 274/*-------------------------------------------------------------------------*/
238/* S/GET CONFIGURATION helpers */ 275/* S/GET CONFIGURATION helpers */
239 276
@@ -241,12 +278,8 @@ static int config_buf(uint8_t *buf, uint8_t type, unsigned index)
241{ 278{
242 int len; 279 int len;
243 280
244 /* only one configuration */ 281 len = usb_stack_configdesc(&storage_config_desc, buf, BUFFER_SIZE, dev.descriptors);
245 if (index != 0) { 282 logf("result %d", len);
246 return -EINVAL;
247 }
248
249 len = usb_stack_configdesc(&storage_config_desc, buf, BUFFER_SIZE, storage_fullspeed_function);
250 if (len < 0) { 283 if (len < 0) {
251 return len; 284 return len;
252 } 285 }
@@ -258,9 +291,9 @@ static int set_config(int config)
258{ 291{
259 /* enable endpoints */ 292 /* enable endpoints */
260 logf("setup %s", dev.in->name); 293 logf("setup %s", dev.in->name);
261 ops->enable(dev.in); 294 ops->enable(dev.in, (struct usb_endpoint_descriptor*)dev.descriptors[1]);
262 logf("setup %s", dev.out->name); 295 logf("setup %s", dev.out->name);
263 ops->enable(dev.out); 296 ops->enable(dev.out, (struct usb_endpoint_descriptor*)dev.descriptors[2]);
264 297
265 /* setup buffers */ 298 /* setup buffers */
266 299
diff --git a/firmware/usbstack/drivers/device/usb_storage.h b/firmware/usbstack/drivers/device/usb_storage.h
index f148d4228c..ff4b187064 100644
--- a/firmware/usbstack/drivers/device/usb_storage.h
+++ b/firmware/usbstack/drivers/device/usb_storage.h
@@ -27,5 +27,6 @@ void usb_storage_driver_init(void);
27 27
28void usb_storage_driver_bind(void* controller_ops); 28void usb_storage_driver_bind(void* controller_ops);
29int usb_storage_driver_request(struct usb_ctrlrequest* req); 29int usb_storage_driver_request(struct usb_ctrlrequest* req);
30void usb_storage_driver_speed(enum usb_device_speed speed);
30 31
31#endif /*_STORGAGE_H_*/ 32#endif /*_STORGAGE_H_*/