aboutsummaryrefslogtreecommitdiff
path: root/lib/usb
diff options
context:
space:
mode:
authorMartin Mueller2010-11-04 00:41:49 +0100
committerMartin Mueller2010-11-04 00:44:47 +0100
commitd6eacce827a8ebffb5e82b48d4c88eb097594c1e (patch)
treed8991b96fc575925ee6f632bf7e5925d46b5cfc3 /lib/usb
parent6e090ccee16582f0c152c95238753562732788e3 (diff)
add standard request
Diffstat (limited to 'lib/usb')
-rw-r--r--lib/usb/usb_control.c16
-rw-r--r--lib/usb/usb_f103.c17
-rw-r--r--lib/usb/usb_private.h5
-rw-r--r--lib/usb/usb_standard.c195
4 files changed, 178 insertions, 55 deletions
diff --git a/lib/usb/usb_control.c b/lib/usb/usb_control.c
index d656a0c..950e491 100644
--- a/lib/usb/usb_control.c
+++ b/lib/usb/usb_control.c
@@ -106,6 +106,10 @@ static void usb_control_setup_nodata(struct usb_setup_data *req)
{
int result = 0;
+ /* Buffer unused */
+ control_state.ctrl_buf = _usbd_device.ctrl_buf;
+ control_state.ctrl_len = 0;
+
/* Call user command hook function */
if(_usbd_device.user_callback_control_command)
result = _usbd_device.user_callback_control_command(req,
@@ -113,7 +117,9 @@ static void usb_control_setup_nodata(struct usb_setup_data *req)
/* Try standard command if not already handled */
if(!result)
- result = _usbd_standard_request_command(req);
+ result = _usbd_standard_request(req,
+ &control_state.ctrl_buf,
+ &control_state.ctrl_len);
if(result) {
/* Go to status stage if handled */
@@ -141,7 +147,7 @@ static void usb_control_setup_read(struct usb_setup_data *req)
/* Try standard request if not already handled */
if(!result)
- result = _usbd_standard_request_read(req,
+ result = _usbd_standard_request(req,
&control_state.ctrl_buf,
&control_state.ctrl_len);
@@ -218,10 +224,10 @@ void _usbd_control_out(uint8_t ea)
&control_state.complete);
if(!result)
- result = _usbd_standard_request_write(
+ result = _usbd_standard_request(
&control_state.req,
- control_state.ctrl_buf,
- control_state.ctrl_len);
+ &control_state.ctrl_buf,
+ &control_state.ctrl_len);
if(result) {
usbd_ep_write_packet(0, NULL, 0);
diff --git a/lib/usb/usb_f103.c b/lib/usb/usb_f103.c
index e4cf06e..1af4493 100644
--- a/lib/usb/usb_f103.c
+++ b/lib/usb/usb_f103.c
@@ -116,7 +116,22 @@ void usbd_ep_stall(u8 addr)
if(addr & 0x80)
USB_SET_EP_TX_STAT(addr, USB_EP_TX_STAT_STALL);
else
- USB_SET_EP_RX_STAT(addr & 0x7F, USB_EP_RX_STAT_STALL);
+ USB_SET_EP_RX_STAT(addr&0x7F, USB_EP_RX_STAT_STALL);
+}
+
+u8 usbd_get_ep_stall(u8 addr)
+{
+ if(addr == 0)
+ if ((*USB_EP_REG(addr) & USB_EP_TX_STAT) == USB_EP_TX_STAT_STALL)
+ return 1;
+ if(addr & 0x80) {
+ if ((*USB_EP_REG(addr) & USB_EP_TX_STAT) == USB_EP_TX_STAT_STALL)
+ return 1;
+ } else {
+ if ((*USB_EP_REG(addr&0x7F) & USB_EP_RX_STAT) == USB_EP_RX_STAT_STALL)
+ return 1;
+ }
+ return 0;
}
/** Copy a data buffer to Packet Memory.
diff --git a/lib/usb/usb_private.h b/lib/usb/usb_private.h
index f0c6a89..6cbc576 100644
--- a/lib/usb/usb_private.h
+++ b/lib/usb/usb_private.h
@@ -67,11 +67,8 @@ void _usbd_control_in(uint8_t ea);
void _usbd_control_out(uint8_t ea);
void _usbd_control_setup(uint8_t ea);
-int _usbd_standard_request_command(struct usb_setup_data *req);
-int _usbd_standard_request_read(struct usb_setup_data *req,
+int _usbd_standard_request(struct usb_setup_data *req,
uint8_t **buf, uint16_t *len);
-int _usbd_standard_request_write(struct usb_setup_data *req,
- uint8_t *buf, uint16_t len);
void _usbd_reset(void);
diff --git a/lib/usb/usb_standard.c b/lib/usb/usb_standard.c
index 0bfc7d8..ae6c2ee 100644
--- a/lib/usb/usb_standard.c
+++ b/lib/usb/usb_standard.c
@@ -115,8 +115,13 @@ static int usb_standard_get_descriptor(struct usb_setup_data *req,
return 0;
}
-static int usb_standard_set_address(struct usb_setup_data *req)
+static int usb_standard_set_address(struct usb_setup_data *req,
+ uint8_t **buf, uint16_t *len)
{
+ (void)req;
+ (void)buf;
+ (void)len;
+
/* The actual address is only latched at the STATUS IN stage */
if((req->bmRequestType != 0) || (req->wValue >= 128)) return 0;
@@ -125,8 +130,13 @@ static int usb_standard_set_address(struct usb_setup_data *req)
return 1;
}
-static int usb_standard_set_configuration(struct usb_setup_data *req)
+static int usb_standard_set_configuration(struct usb_setup_data *req,
+ uint8_t **buf, uint16_t *len)
{
+ (void)req;
+ (void)buf;
+ (void)len;
+
/* Is this correct, or should we reset alternate settings */
if(req->wValue == _usbd_device.current_config) return 1;
@@ -152,19 +162,38 @@ static int usb_standard_get_configuration(struct usb_setup_data *req,
return 1;
}
-static int usb_standard_set_interface(struct usb_setup_data *req)
+static int usb_standard_set_interface(struct usb_setup_data *req,
+ uint8_t **buf, uint16_t *len)
{
(void)req;
- /* FIXME: Do something meaningful here: call app */
+ (void)buf;
+
+ /* FIXME: adapt if we have more than one interface */
+ if(req->wValue != 0) return 0;
+ *len = 0;
+
return 1;
}
-static int usb_standard_get_status(struct usb_setup_data *req, uint8_t **buf,
- uint16_t *len)
+static int usb_standard_get_interface(struct usb_setup_data *req,
+ uint8_t **buf, uint16_t *len)
{
(void)req;
- /* FIXME: Return some meaningful status */
+ (void)buf;
+
+ /* FIXME: adapt if we have more than one interface */
+ *len = 1;
+ (*buf)[0] = 0;
+
+ return 1;
+}
+static int usb_standard_device_get_status(struct usb_setup_data *req,
+ uint8_t **buf, uint16_t *len)
+{
+ (void)req;
+ /* bit 0: self powered */
+ /* bit 1: remote wakeup */
if(*len > 2) *len = 2;
(*buf)[0] = 0;
(*buf)[1] = 0;
@@ -172,18 +201,68 @@ static int usb_standard_get_status(struct usb_setup_data *req, uint8_t **buf,
return 1;
}
-int _usbd_standard_request_command(struct usb_setup_data *req)
+static int usb_standard_interface_get_status(struct usb_setup_data *req,
+ uint8_t **buf, uint16_t *len)
{
- int (*command)(struct usb_setup_data *req) = NULL;
+ (void)req;
+ /* not defined */
- if((req->bmRequestType & 0x60) != USB_REQ_TYPE_STANDARD)
- return 0;
+ if(*len > 2) *len = 2;
+ (*buf)[0] = 0;
+ (*buf)[1] = 0;
+
+ return 1;
+}
+
+static int usb_standard_endpoint_get_status(struct usb_setup_data *req,
+ uint8_t **buf, uint16_t *len)
+{
+ (void)req;
+
+ if(*len > 2) *len = 2;
+ (*buf)[0] = usbd_get_ep_stall(req->wIndex);
+ (*buf)[1] = 0;
+
+ return 1;
+}
+
+static int usb_standard_endpoint_stall(struct usb_setup_data *req,
+ uint8_t **buf, uint16_t *len)
+{
+ (void)buf;
+ (void)len;
+
+ usbd_ep_stall(req->wIndex);
+
+ return 1;
+}
+
+static int usb_standard_endpoint_unstall(struct usb_setup_data *req,
+ uint8_t **buf, uint16_t *len)
+{
+ (void)buf;
+ (void)len;
+
+ usbd_ep_stall(req->wIndex);
+
+ return 1;
+}
+
+int _usbd_standard_request_device(struct usb_setup_data *req, uint8_t **buf,
+ uint16_t *len)
+{
+ int (*command)(struct usb_setup_data *req, uint8_t **buf,
+ uint16_t *len) = NULL;
switch(req->bRequest) {
case USB_REQ_CLEAR_FEATURE:
- /* FIXME: Implement CLEAR_FEATURE */
- /* TODO: Check what standard features are.
- * Maybe this is the application's responsibility. */
+ case USB_REQ_SET_FEATURE:
+ if (req->wValue == USB_FEAT_DEVICE_REMOTE_WAKEUP) {
+ /* device wakeup code goes here */
+ }
+ if (req->wValue == USB_FEAT_TEST_MODE) {
+ /* test mode code goes here */
+ }
break;
case USB_REQ_SET_ADDRESS:
/* SET ADDRESS is an exception.
@@ -193,45 +272,46 @@ int _usbd_standard_request_command(struct usb_setup_data *req)
case USB_REQ_SET_CONFIGURATION:
command = usb_standard_set_configuration;
break;
- case USB_REQ_SET_FEATURE:
- /* FIXME: Implement SET_FEATURE */
- /* TODO: Check what standard features are.
- * Maybe this is the application's responsibility. */
+ case USB_REQ_GET_CONFIGURATION:
+ command = usb_standard_get_configuration;
break;
- case USB_REQ_SET_INTERFACE:
- command = usb_standard_set_interface;
+ case USB_REQ_GET_DESCRIPTOR:
+ command = usb_standard_get_descriptor;
+ break;
+ case USB_REQ_GET_STATUS:
+ /* GET_STATUS always responds with zero reply.
+ * The application may override this behaviour. */
+ command = usb_standard_device_get_status;
+ break;
+ case USB_REQ_SET_DESCRIPTOR:
+ /* SET_DESCRIPTOR is optional and not implemented. */
break;
}
if(!command) return 0;
- return command(req);
+ return command(req, buf, len);
}
-int _usbd_standard_request_read(struct usb_setup_data *req, uint8_t **buf,
+int _usbd_standard_request_interface(struct usb_setup_data *req, uint8_t **buf,
uint16_t *len)
{
int (*command)(struct usb_setup_data *req, uint8_t **buf,
uint16_t *len) = NULL;
- /* Handle standard requests */
- if((req->bmRequestType & 0x60) != USB_REQ_TYPE_STANDARD)
- return 0;
-
switch(req->bRequest) {
- case USB_REQ_GET_CONFIGURATION:
- command = usb_standard_get_configuration;
- break;
- case USB_REQ_GET_DESCRIPTOR:
- command = usb_standard_get_descriptor;
+ case USB_REQ_CLEAR_FEATURE:
+ case USB_REQ_SET_FEATURE:
+ /* not defined */
break;
case USB_REQ_GET_INTERFACE:
- /* FIXME: Implement GET_INTERFACE */
+ command = usb_standard_get_interface;
+ break;
+ case USB_REQ_SET_INTERFACE:
+ command = usb_standard_set_interface;
break;
case USB_REQ_GET_STATUS:
- /* GET_STATUS always responds with zero reply.
- * The application may override this behaviour. */
- command = usb_standard_get_status;
+ command = usb_standard_interface_get_status;
break;
}
@@ -240,19 +320,25 @@ int _usbd_standard_request_read(struct usb_setup_data *req, uint8_t **buf,
return command(req, buf, len);
}
-int _usbd_standard_request_write(struct usb_setup_data *req, uint8_t *buf,
- uint16_t len)
+int _usbd_standard_request_endpoint(struct usb_setup_data *req, uint8_t **buf,
+ uint16_t *len)
{
- int (*command)(struct usb_setup_data *req, uint8_t *buf, uint16_t len)
- = NULL;
-
- /* Handle standard requests */
- if((req->bmRequestType & 0x60) != USB_REQ_TYPE_STANDARD)
- return 0;
+ int (*command)(struct usb_setup_data *req, uint8_t **buf,
+ uint16_t *len) = NULL;
switch(req->bRequest) {
- case USB_REQ_SET_DESCRIPTOR:
- /* SET_DESCRIPTOR is optional and not implemented. */
+ case USB_REQ_CLEAR_FEATURE:
+ if (req->wValue == USB_FEAT_ENDPOINT_HALT) {
+ command = usb_standard_endpoint_stall;
+ }
+ break;
+ case USB_REQ_SET_FEATURE:
+ if (req->wValue == USB_FEAT_ENDPOINT_HALT) {
+ command = usb_standard_endpoint_unstall;
+ }
+ break;
+ case USB_REQ_GET_STATUS:
+ command = usb_standard_endpoint_get_status;
break;
case USB_REQ_SET_SYNCH_FRAME:
/* FIXME: SYNCH_FRAME is not implemented. */
@@ -266,3 +352,22 @@ int _usbd_standard_request_write(struct usb_setup_data *req, uint8_t *buf,
return command(req, buf, len);
}
+int _usbd_standard_request(struct usb_setup_data *req, uint8_t **buf,
+ uint16_t *len)
+{
+ /* FIXME: have class/vendor requests as well */
+ if((req->bmRequestType & USB_REQ_TYPE_TYPE) != USB_REQ_TYPE_STANDARD)
+ return 0;
+
+ switch (req->bmRequestType & USB_REQ_TYPE_RECIPIENT) {
+ case USB_REQ_TYPE_DEVICE:
+ return _usbd_standard_request_device(req, buf, len);
+ case USB_REQ_TYPE_INTERFACE:
+ return _usbd_standard_request_interface(req, buf, len);
+ case USB_REQ_TYPE_ENDPOINT:
+ return _usbd_standard_request_endpoint(req, buf, len);
+ default:
+ return 0;
+ }
+}
+