freemyipod r889 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r888‎ | r889 | r890 >
Date:00:42, 3 August 2013
Author:theseven
Status:new
Tags:
Comment:
UMSboot: Some more USB fixes and extensions
Modified paths:
  • /umsboot/src/app/umsboot/ums.c (modified) (history)
  • /umsboot/src/app/umsboot/usbglue.c (modified) (history)
  • /umsboot/src/core/synopsysotg/synopsysotg.c (modified) (history)
  • /umsboot/src/protocol/usb/usb.c (modified) (history)
  • /umsboot/src/protocol/usb/usb.h (modified) (history)

Diff [purge]

Index: umsboot/src/app/umsboot/ums.c
@@ -562,8 +562,7 @@
563563 switch (request->setup.bRequest.raw)
564564 {
565565 case 0xfe: // GET_MAX_LUN
566 - request->raw[0] = 0;
567 - *response = request;
 566+ data->buffer->raw[0] = 0;
568567 size = 1;
569568 break;
570569 case 0xff: // STORAGE_RESET
Index: umsboot/src/app/umsboot/usbglue.c
@@ -134,7 +134,7 @@
135135 usb_config1_descriptors.c1_i0_a0_e1in.wMaxPacketSize = highspeed ? 512 : 64;
136136 }
137137
138 -static struct usb_interface usb_c1_i0 =
 138+static const struct usb_interface usb_c1_i0 =
139139 {
140140 .bus_reset = ums_bus_reset,
141141 .ctrl_request = ums_ctrl_request,
@@ -162,7 +162,10 @@
163163
164164 static UMSBOOT_USB_DRIVER_STATE_TYPE usb_driver_state = UMSBOOT_USB_DRIVER_STATE;
165165
166 -static struct usb_state usb_state;
 166+static struct usb_state usb_state =
 167+{
 168+ .interface_altsetting = { 0 },
 169+};
167170
168171 static union usb_ep0_buffer usb_buffer __attribute__((section(".dmabss.usb_buffer"),aligned(CACHEALIGN_SIZE)));
169172
@@ -175,7 +178,6 @@
176179 .buffer = &usb_buffer,
177180 .bus_reset = usbglue_bus_reset,
178181 .ep0_setup_hook = NULL,
179 - .ep0_data_hook = NULL,
180182 .configuration_count = 1,
181183 .stringdescriptor_count = 3,
182184 .devicedescriptor = &usb_devicedescriptor,
Index: umsboot/src/protocol/usb/usb.c
@@ -1,5 +1,6 @@
22 #include "global.h"
33 #include "protocol/usb/usb.h"
 4+#include "sys/util.h"
45
56 void usb_start_rx(const struct usb_instance* data, union usb_endpoint_number ep, void* buf, int size)
67 {
@@ -16,27 +17,35 @@
1718 data->driver->set_stall(data, ep, stall);
1819 }
1920
20 -void usb_ep0_start_rx(const struct usb_instance* data, int non_setup)
 21+void usb_ep0_start_rx(const struct usb_instance* data, int non_setup, bool (*callback)(const struct usb_instance* data, int bytesleft))
2122 {
 23+ data->state->ep0_rx_callback = callback;
2224 data->driver->ep0_start_rx(data, non_setup);
2325 }
2426
25 -void usb_ep0_start_tx(const struct usb_instance* data, const void* buf, int len)
 27+bool usb_ep0_rx_callback(const struct usb_instance* data, int bytesleft)
2628 {
 29+ usb_ep0_start_rx(data, 0, NULL);
 30+ return true;
 31+}
 32+
 33+void usb_ep0_start_tx(const struct usb_instance* data, const void* buf, int len, bool last, bool (*callback)(const struct usb_instance* data, int bytesleft))
 34+{
2735 // Expect zero-length ACK if we are about to actually send data, otherwise expect SETUP.
28 - usb_ep0_start_rx(data, !!len);
 36+ if (last) usb_ep0_start_rx(data, !!len, usb_ep0_rx_callback);
2937
 38+ data->state->ep0_tx_callback = callback;
3039 data->driver->ep0_start_tx(data, buf, len);
3140 }
3241
33 -void usb_ep0_expect_setup(const struct usb_instance* data)
 42+bool usb_ep0_tx_callback(const struct usb_instance* data, int bytesleft)
3443 {
35 - // The next packet needs to be a SETUP, so lock out everything on the IN pipe.
36 - union usb_endpoint_number ep = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_IN };
37 - usb_set_stall(data, ep, 1);
38 -
39 - // Set up the OUT pipe for the SETUP packet, STALLing everything else.
40 - usb_ep0_start_rx(data, 0);
 44+ if (bytesleft || !data->state->ep0_tx_len) return false;
 45+ int len = MIN(64, data->state->ep0_tx_len);
 46+ data->state->ep0_tx_ptr += 64;
 47+ data->state->ep0_tx_len -= len;
 48+ usb_ep0_start_tx(data, data->state->ep0_tx_ptr, len, !!data->state->ep0_tx_len, usb_ep0_tx_callback);
 49+ return true;
4150 }
4251
4352 void usb_unconfigure_ep(const struct usb_instance* data, union usb_endpoint_number ep)
@@ -68,10 +77,10 @@
6978 int i;
7079 for (i = 0; i < configuration->interface_count; i++)
7180 {
72 - struct usb_interface* interface = configuration->interfaces[i];
73 - const struct usb_altsetting* altsetting = interface->altsettings[interface->current_altsetting];
 81+ const struct usb_interface* interface = configuration->interfaces[i];
 82+ const struct usb_altsetting* altsetting = interface->altsettings[data->state->interface_altsetting[i]];
7483 if (altsetting->unset_altsetting)
75 - altsetting->unset_altsetting(data, i, interface->current_altsetting);
 84+ altsetting->unset_altsetting(data, i, data->state->interface_altsetting[i]);
7685 }
7786 if (configuration->unset_configuration)
7887 configuration->unset_configuration(data, configid);
@@ -91,7 +100,7 @@
92101 for (*ifidx = 0; *ifidx < configuration->interface_count; (*ifidx)++)
93102 {
94103 const struct usb_interface* interface = configuration->interfaces[*ifidx];
95 - const struct usb_altsetting* altsetting = interface->altsettings[interface->current_altsetting];
 104+ const struct usb_altsetting* altsetting = interface->altsettings[data->state->interface_altsetting[*ifidx]];
96105 for (*epidx = 0; *epidx < altsetting->endpoint_count; (*epidx)++)
97106 {
98107 const struct usb_endpoint* endpoint = altsetting->endpoints[*epidx];
@@ -108,7 +117,7 @@
109118 // size == -1: try to run default handler, or send STALL if none exists
110119 // size == 0: send ACK
111120 // size > 0: send <size> bytes at <addr>, then expect ACK
112 - const void* addr = NULL;
 121+ const void* addr = data->buffer;
113122 int size = -1;
114123 switch (buffer->setup.bmRequestType.recipient)
115124 {
@@ -125,7 +134,6 @@
126135 || !data->state->current_address || buffer->setup.wValue) break;
127136 data->buffer->raw[0] = 0;
128137 data->buffer->raw[1] = 1;
129 - addr = data->buffer;
130138 size = 2;
131139 break;
132140 case USB_SETUP_BREQUEST_SET_ADDRESS:
@@ -162,7 +170,6 @@
163171 if (buffer->setup.wLength != 1 || buffer->setup.wIndex
164172 || !data->state->current_address || buffer->setup.wValue) break;
165173 data->buffer->raw[0] = data->state->current_configuration;
166 - addr = data->buffer;
167174 size = 1;
168175 break;
169176 case USB_SETUP_BREQUEST_SET_CONFIGURATION:
@@ -180,8 +187,8 @@
181188 int i;
182189 for (i = 0; i < configuration->interface_count; i++)
183190 {
184 - struct usb_interface* interface = configuration->interfaces[i];
185 - interface->current_altsetting = 0;
 191+ const struct usb_interface* interface = configuration->interfaces[i];
 192+ data->state->interface_altsetting[i] = 0;
186193 const struct usb_altsetting* altsetting = interface->altsettings[0];
187194 if (altsetting->set_altsetting) altsetting->set_altsetting(data, i, 0);
188195 }
@@ -198,9 +205,9 @@
199206 if (!data->state->current_configuration) break;
200207 int configid = data->state->current_configuration;
201208 const struct usb_configuration* configuration = data->configurations[configid - 1];
202 - if (buffer->setup.wIndex >= configuration->interface_count) break;
203209 int intfid = buffer->setup.wIndex;
204 - struct usb_interface* interface = configuration->interfaces[intfid];
 210+ if (intfid >= configuration->interface_count) break;
 211+ const struct usb_interface* interface = configuration->interfaces[intfid];
205212 if (interface->ctrl_request) size = interface->ctrl_request(data, intfid, buffer, &addr);
206213 if (size != -1) break;
207214 switch (buffer->setup.bmRequestType.type)
@@ -212,13 +219,11 @@
213220 if (buffer->setup.wLength != 2 || buffer->setup.wValue) break;
214221 data->buffer->raw[0] = 0;
215222 data->buffer->raw[1] = 0;
216 - addr = data->buffer;
217223 size = 2;
218224 break;
219225 case USB_SETUP_BREQUEST_GET_INTERFACE:
220226 if (buffer->setup.wLength != 1 || buffer->setup.wValue) break;
221 - data->buffer->raw[0] = interface->current_altsetting;
222 - addr = data->buffer;
 227+ data->buffer->raw[0] = data->state->interface_altsetting[intfid];
223228 size = 1;
224229 break;
225230 case USB_SETUP_BREQUEST_SET_INTERFACE:
@@ -225,13 +230,13 @@
226231 {
227232 if (buffer->setup.wLength
228233 || buffer->setup.wValue > interface->altsetting_count) break;
229 - const struct usb_altsetting* altsetting = interface->altsettings[interface->current_altsetting];
 234+ const struct usb_altsetting* altsetting = interface->altsettings[data->state->interface_altsetting[intfid]];
230235 if (altsetting->unset_altsetting)
231 - altsetting->unset_altsetting(data, intfid, interface->current_altsetting);
232 - interface->current_altsetting = buffer->setup.wValue;
233 - altsetting = interface->altsettings[interface->current_altsetting];
 236+ altsetting->unset_altsetting(data, intfid, data->state->interface_altsetting[intfid]);
 237+ data->state->interface_altsetting[intfid] = buffer->setup.wValue;
 238+ altsetting = interface->altsettings[data->state->interface_altsetting[intfid]];
234239 if (altsetting->set_altsetting)
235 - altsetting->set_altsetting(data, intfid, interface->current_altsetting);
 240+ altsetting->set_altsetting(data, intfid, data->state->interface_altsetting[intfid]);
236241 break;
237242 }
238243 default: break;
@@ -283,17 +288,24 @@
284289 }
285290 }
286291 // See comment at the top of this function
287 - if (size == 0) usb_ep0_start_tx(data, NULL, 0);
288 - else if (size > 0) usb_ep0_start_tx(data, addr, size);
289 - else if (size >= -2) usb_ep0_expect_setup(data);
 292+ if (size == 0) usb_ep0_start_tx(data, NULL, 0, true, NULL);
 293+ else if (size > 0)
 294+ {
 295+ usb_ep0_start_rx(data, 0, NULL);
 296+ int len = MIN(64, size);
 297+ data->state->ep0_tx_ptr = addr;
 298+ data->state->ep0_tx_len = size - len;
 299+ usb_ep0_start_tx(data, addr, len, !data->state->ep0_tx_len, usb_ep0_tx_callback);
 300+ }
 301+ else if (size >= -2)
 302+ {
 303+ union usb_endpoint_number ep = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_IN };
 304+ usb_set_stall(data, ep, 1);
 305+ ep.direction = USB_ENDPOINT_DIRECTION_OUT;
 306+ usb_set_stall(data, ep, 1);
 307+ }
290308 }
291309
292 -static void usb_handle_ep0_data(const struct usb_instance* data, union usb_ep0_buffer* buffer, int size)
293 -{
294 - // We don't accept any commands that have an OUT payload. Just STALL it, no matter what it was.
295 - usb_ep0_expect_setup(data);
296 -}
297 -
298310 void usb_handle_bus_reset(const struct usb_instance* data, int highspeed)
299311 {
300312 data->state->current_address = 0;
@@ -305,18 +317,20 @@
306318 const struct usb_configuration* configuration = data->configurations[c];
307319 for (i = 0; i < configuration->interface_count; i++)
308320 {
309 - struct usb_interface* interface = configuration->interfaces[i];
 321+ const struct usb_interface* interface = configuration->interfaces[i];
310322 if (interface->bus_reset) interface->bus_reset(data, c, i, highspeed);
311323 }
312324 }
 325+
 326+ // Prime EP0 for the first setup packet.
 327+ usb_ep0_start_rx(data, 0, NULL);
313328 }
314329
315330 void usb_handle_timeout(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft)
316331 {
317 - // Hm, the host didn't fetch our EP0 IN packet, so we feel a bit offended.
318 - // Refuse to accept any transfers, until we get a new SETUP token.
319 - if (!epnum.number) usb_ep0_expect_setup(data);
320 - else
 332+ // If the host doesn't fetch an EP0 IN packet, we can't do much about it.
 333+ // This will be recovered by the next SETUP packet.
 334+ if (epnum.number)
321335 {
322336 int epidx;
323337 int ifidx;
@@ -330,11 +344,18 @@
331345 {
332346 if (!epnum.number)
333347 {
334 - // If this was EP0 IN, stall the pipe. There will only ever be one transfer for each
335 - // SETUP transaction, and the next SETUP packet will clear the stall.
336 - if (epnum.direction == USB_ENDPOINT_DIRECTION_IN) usb_set_stall(data, epnum, 1);
337 - else if (!data->ep0_data_hook || !data->ep0_data_hook(data, data->buffer, 64 - bytesleft))
338 - usb_handle_ep0_data(data, data->buffer, 64 - bytesleft);
 348+ bool (*callback)(const struct usb_instance* data, int size);
 349+ if (epnum.direction == USB_ENDPOINT_DIRECTION_OUT)
 350+ {
 351+ callback = data->state->ep0_rx_callback;
 352+ data->state->ep0_rx_callback = NULL;
 353+ }
 354+ else
 355+ {
 356+ callback = data->state->ep0_tx_callback;
 357+ data->state->ep0_tx_callback = NULL;
 358+ }
 359+ if (callback) callback(data, bytesleft);
339360 }
340361 else
341362 {
@@ -346,16 +367,11 @@
347368 }
348369 }
349370
350 -void usb_handle_setup_received(const struct usb_instance* data, union usb_endpoint_number epnum, int back2back)
 371+void usb_handle_setup_received(const struct usb_instance* data, union usb_endpoint_number epnum)
351372 {
352373 if (!epnum.number)
353374 {
354 - // Figure out the location of the newest SETUP packet if there were
355 - // multiple back to back ones. If there were more than 3 SETUP packets
356 - // in a row, the OTG will take care of it, so we're on the safe side here.
357 - void* addr = &data->buffer->raw[8 * (back2back - 1)];
358 - union usb_ep0_buffer* buffer = (union usb_ep0_buffer*)addr;
359 - if (!data->ep0_setup_hook || !data->ep0_setup_hook(data, buffer)) usb_handle_ep0_setup(data, buffer);
 375+ if (!data->ep0_setup_hook || !data->ep0_setup_hook(data, data->buffer)) usb_handle_ep0_setup(data, data->buffer);
360376 }
361377 else
362378 {
@@ -363,7 +379,7 @@
364380 int ifidx;
365381 const struct usb_endpoint* endpoint = usb_find_endpoint(data, epnum, &epidx, &ifidx);
366382 if (!endpoint) usb_unconfigure_ep(data, epnum);
367 - else if (endpoint->setup_received) endpoint->setup_received(data, ifidx, epidx, back2back);
 383+ else if (endpoint->setup_received) endpoint->setup_received(data, ifidx, epidx);
368384 }
369385 }
370386
Index: umsboot/src/protocol/usb/usb.h
@@ -194,7 +194,7 @@
195195 void (*xfer_complete)(const struct usb_instance* data, int interface, int endpoint, int bytesleft);
196196 union __attribute__((packed))
197197 {
198 - void (*setup_received)(const struct usb_instance* data, int interface, int endpoint, int back2back);
 198+ void (*setup_received)(const struct usb_instance* data, int interface, int endpoint);
199199 void (*timeout)(const struct usb_instance* data, int interface, int endpoint, int bytesleft);
200200 };
201201 };
@@ -216,7 +216,7 @@
217217 int (*ctrl_request)(const struct usb_instance* data, int interface, union usb_ep0_buffer* request, const void** response);
218218 uint8_t reserved1;
219219 uint8_t reserved2;
220 - uint8_t current_altsetting;
 220+ uint8_t reserved3;
221221 uint8_t altsetting_count;
222222 const struct usb_altsetting* altsettings[];
223223 };
@@ -230,15 +230,18 @@
231231 uint8_t reserved2;
232232 uint8_t reserved3;
233233 uint8_t interface_count;
234 - struct usb_interface* interfaces[];
 234+ const struct usb_interface* interfaces[];
235235 };
236236
237237 struct __attribute__((packed,aligned(4))) usb_state
238238 {
 239+ bool (*ep0_rx_callback)(const struct usb_instance* data, int bytesleft);
 240+ bool (*ep0_tx_callback)(const struct usb_instance* data, int bytesleft);
 241+ const void* ep0_tx_ptr;
 242+ uint16_t ep0_tx_len;
239243 uint8_t current_address;
240244 uint8_t current_configuration;
241 - uint8_t reserved1;
242 - uint8_t reserved2;
 245+ uint8_t interface_altsetting[];
243246 };
244247
245248 struct __attribute__((packed,aligned(4))) usb_driver
@@ -267,7 +270,6 @@
268271 void (*bus_reset)(const struct usb_instance* data, int highspeed);
269272 int (*ctrl_request)(const struct usb_instance* data, union usb_ep0_buffer* request, const void** response);
270273 int (*ep0_setup_hook)(const struct usb_instance* data, union usb_ep0_buffer* buf);
271 - int (*ep0_data_hook)(const struct usb_instance* data, union usb_ep0_buffer* buf, int size);
272274 uint8_t configuration_count;
273275 uint8_t stringdescriptor_count;
274276 uint8_t reserved1;
@@ -282,9 +284,9 @@
283285 extern void usb_handle_bus_reset(const struct usb_instance* data, int highspeed);
284286 extern void usb_handle_timeout(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft);
285287 extern void usb_handle_xfer_complete(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft);
286 -extern void usb_handle_setup_received(const struct usb_instance* data, union usb_endpoint_number epnum, int back2back);
287 -extern void usb_ep0_start_rx(const struct usb_instance* data, int non_setup);
288 -extern void usb_ep0_start_tx(const struct usb_instance* data, const void* buf, int len);
 288+extern void usb_handle_setup_received(const struct usb_instance* data, union usb_endpoint_number epnum);
 289+extern void usb_ep0_start_rx(const struct usb_instance* data, int non_setup, bool (*callback)(const struct usb_instance* data, int bytesleft));
 290+extern void usb_ep0_start_tx(const struct usb_instance* data, const void* buf, int len, bool last, bool (*callback)(const struct usb_instance* data, int bytesleft));
289291 extern void usb_ep0_expect_setup(const struct usb_instance* data);
290292 extern void usb_start_rx(const struct usb_instance* data, union usb_endpoint_number ep, void* buf, int size);
291293 extern void usb_start_tx(const struct usb_instance* data, union usb_endpoint_number ep, const void* buf, int size);
Index: umsboot/src/core/synopsysotg/synopsysotg.c
@@ -229,10 +229,6 @@
230230 const struct synopsysotg_config* data = (const struct synopsysotg_config*)instance->driver_config;
231231 struct synopsysotg_state* state = (struct synopsysotg_state*)instance->driver_state;
232232
233 - // If we don't expect a non-SETUP packet, we can stall the OUT pipe,
234 - // SETUP packets will ignore that.
235 - if (!non_setup) data->core->outep_regs[0].doepctl.b.stall = 1;
236 -
237233 // Set up data destination
238234 if (data->use_dma) data->core->outep_regs[0].doepdma = instance->buffer;
239235 else state->endpoints[0].rxaddr = (uint32_t*)instance->buffer;
@@ -245,7 +241,7 @@
246242 // Enable the endpoint
247243 union synopsysotg_depctl depctl = data->core->outep_regs[0].doepctl;
248244 depctl.b.epena = 1;
249 - depctl.b.cnak = 1;
 245+ depctl.b.cnak = non_setup;
250246 data->core->outep_regs[0].doepctl = depctl;
251247 }
252248
@@ -295,9 +291,6 @@
296292 union synopsysotg_depctl depctl = { .b = { .usbactep = 1, .nextep = data->core->inep_regs[0].diepctl.b.nextep } };
297293 data->core->outep_regs[0].doepctl = depctl;
298294 data->core->inep_regs[0].diepctl = depctl;
299 -
300 - // Prime EP0 for the first setup packet.
301 - usb_ep0_expect_setup(instance);
302295 }
303296
304297 void synopsysotg_irq(const struct usb_instance* instance)
@@ -315,8 +308,8 @@
316309
317310 if (gintsts.b.enumdone)
318311 {
 312+ usb_handle_bus_reset(instance, data->core->dregs.dsts.b.enumspd == 0);
319313 synopsysotg_ep0_init(instance);
320 - usb_handle_bus_reset(instance, data->core->dregs.dsts.b.enumspd == 0);
321314 }
322315
323316 if (gintsts.b.rxstsqlvl)
@@ -389,10 +382,8 @@
390383 if (epints.b.setup)
391384 {
392385 if (data->use_dma) invalidate_dcache(instance->buffer, sizeof(instance->buffer));
393 - union synopsysotg_dep0xfrsiz deptsiz = { .d32 = data->core->outep_regs[0].doeptsiz.d32 };
394 - int back2back = 3 - deptsiz.b.supcnt;
395386 synopsysotg_flush_in_endpoint(instance, ep);
396 - usb_handle_setup_received(instance, epnum, back2back);
 387+ usb_handle_setup_received(instance, epnum);
397388 }
398389 else if (epints.b.xfercompl)
399390 {