···11+Handling USB control requests
22+=============================
33+44+API overview
55+------------
66+77+ enum usb_control_response {
88+ USB_CONTROL_ACK,
99+ USB_CONTROL_STALL,
1010+ USB_CONTROL_RECEIVE,
1111+ };
1212+1313+ void usb_core_control_request(struct usb_ctrlrequest* req, void* reqdata);
1414+ void usb_core_control_complete(int status);
1515+ void usb_drv_control_response(enum usb_control_response resp,
1616+ void* data, int length);
1717+1818+The two `usb_core` functions are common to all targets with a USB stack and
1919+are implemented in `usb_core.c`. The USB driver calls them to inform the core
2020+when a control request arrives or is completed.
2121+2222+Each USB driver implements `usb_drv_control_response()`. The core calls this
2323+to let the driver know how to respond to each control request.
2424+2525+### Legacy API
2626+2727+ void usb_core_legacy_control_request(struct usb_ctrlrequest* req);
2828+2929+The old control request API is available through this function. Drivers which
3030+don't yet implement the new API can use the legacy API instead. To support
3131+legacy drivers, the USB core implements all functions in the new API and
3232+emulates the old control request handling behavior, bugs included.
3333+3434+This is intended as a stopgap measure so that old drivers keep working as-is.
3535+The core can start using the new API right away, and drivers can be ported
3636+one-by-one as time allows. Once all drivers are ported to the new API, all
3737+legacy driver support can be removed.
3838+3939+Request handling process
4040+------------------------
4141+4242+The driver submits control requests to the USB core one at a time. Once a
4343+request is submitted, it must be completed before the next request can be
4444+submitted. This mirrors normal USB operation.
4545+4646+When the USB driver receives a setup packet from the host, it submits it
4747+to the core to begin handling the control transfer. The driver calls
4848+`usb_core_control_request(req, NULL)`, passing the setup packet in `req`.
4949+The second argument, `reqdata`, is not used at this time and is passed
5050+as `NULL`.
5151+5252+The core processes the setup packet and calls `usb_drv_control_response()`
5353+when it's done. The allowed responses depend on the type of control transfer
5454+being processed.
5555+5656+### Non-data transfers
5757+5858+- `USB_CONTROL_ACK`, to indicate the request was processed successfully.
5959+- `USB_CONTROL_STALL`, if the request is unsupported or cannot be processed.
6060+6161+### Control read transfers
6262+6363+- `USB_CONTROL_ACK`, to indicate the request was processed successfully.
6464+ The core must provide a valid `data` buffer with `length` not exceeding
6565+ the `wLength` field in the setup packet; otherwise, driver behavior is
6666+ undefined. The driver will transfer this data to the host during the
6767+ data phase of the control transfer, and then acknowledge the host's OUT
6868+ packet to complete the transfer successfully.
6969+- `USB_CONTROL_STALL`, if the request is unsupported or cannot be processed.
7070+7171+### Control write transfers
7272+7373+The driver calls `usb_core_control_request()` twice to handle control writes.
7474+The first call allows the core to handle the setup packet, and if the core
7575+decides to accept the data phase, the second call is made when the data has
7676+been received without error.
7777+7878+#### Setup phase
7979+8080+The first call is made at the end of the setup phase, after receiving the
8181+setup packet. The driver passes `reqdata = NULL` to indicate this.
8282+8383+The core can decide whether it wants to receive the data phase:
8484+8585+- `USB_CONTROL_RECEIVE`, if the core wishes to continue to the data phase.
8686+ The core must provide a valid `data` buffer with `length` greater than or
8787+ equal to the `wLength` specified in the setup packet; otherwise, driver
8888+ behavior is undefined. The driver will proceed to the data phase and store
8989+ received data into the provided buffer.
9090+- `USB_CONTROL_STALL`, if the request is unsupported or cannot be processed.
9191+9292+If the core accepts the data phase, the driver will re-submit the request
9393+when the data phase is completed correctly. If any error occurs during the
9494+data phase, the driver will not re-submit the request; instead, it will
9595+call `usb_core_control_complete()` with a non-zero status code.
9696+9797+#### Status phase
9898+9999+The second call to `usb_core_control_request()` is made at the end of the data
100100+phase. The `reqdata` passed by the driver is the same one that the core passed
101101+in its `USB_CONTROL_RECEIVE` response.
102102+103103+The core's allowed responses are:
104104+105105+- `USB_CONTROL_ACK`, to indicate the request was processed successfully.
106106+- `USB_CONTROL_STALL`, if the request is unsupported or cannot be processed.
107107+108108+### Request completion
109109+110110+The driver will notify the core when a request has completed by calling
111111+`usb_core_control_complete()`. A status code of zero means the request was
112112+completed successfully; a non-zero code means it failed. Note that failure
113113+can occur even if the request was successful from the core's perspective.
114114+115115+If the core response is `USB_CONTROL_STALL` at any point, the request is
116116+considered complete. In this case, the driver won't deliver a completion
117117+notification because it would be redundant.
118118+119119+The driver may only complete a request after the core has provided a response
120120+to any pending `usb_core_control_request()` call. Specifically, if the core
121121+has not yet responded to a request, the driver needs to defer the completion
122122+notification until it sees the core's response. If the core's response is a
123123+stall, then the notification should be silently dropped.
124124+125125+### Notes
126126+127127+- Driver behavior is undefined if the core makes an inappropriate response
128128+ to a request, for example, responding with `USB_CONTROL_ACK` in the setup
129129+ phase of a control write or `USB_CONTROL_RECEIVE` to a non-data request.
130130+ The only permissible responses are the documented ones.
131131+132132+- If a response requires a buffer, then `data` must be non-NULL unless the
133133+ `length` is also zero. If a buffer is not required, the core must pass
134134+ `data = NULL` and `length = 0`. Otherwise, driver behavior is undefined.
135135+ There are two responses which require a buffer:
136136+ + `USB_CONTROL_ACK` to a control read
137137+ + `USB_CONTROL_RECEIVE` to the setup phase of a control write
138138+139139+- Drivers must be prepared to accept a setup packet at any time, including
140140+ in the middle of a control request. In such a case, devices are required
141141+ to abort the ongoing request and start handling the new request. (This is
142142+ intended as an error recovery mechanism and should not be abused by hosts
143143+ in normal operation.) The driver must take care to notify the core of the
144144+ current request's failure, and then submit the new request.
···208208 /* acknowledge packet recieved (clear valid) */
209209 M66591_INTSTAT_MAIN &= ~(1<<3);
210210211211- usb_core_control_request(&temp);
211211+ usb_core_legacy_control_request(&temp);
212212}
213213214214/* This is a helper function, it is used to notife the stack that a transfer is