2012-08-07 11:24:59 +02:00
|
|
|
#include "cdc-acm.h"
|
|
|
|
#include "cdc.h"
|
|
|
|
#include "usb-api.h"
|
|
|
|
#include "usb-core.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2016-10-25 15:42:17 +02:00
|
|
|
#include <string.h>
|
2012-08-07 11:24:59 +02:00
|
|
|
|
2012-08-17 15:56:59 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
2012-08-07 11:24:59 +02:00
|
|
|
static uint8_t usb_ctrl_data_buffer[32];
|
|
|
|
|
2012-10-31 19:48:37 +01:00
|
|
|
static struct usb_cdc_line_coding usb_line_coding = { 9600, 0x00, 0x00, 0x08 }; // 9600 baud, 8N1
|
2012-08-17 15:56:59 +02:00
|
|
|
static uint8_t line_state;
|
|
|
|
static uint8_t events;
|
2012-10-31 19:48:37 +01:00
|
|
|
static struct process *cdc_event_process = NULL;
|
2012-08-17 15:56:59 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
notify_user(uint8_t e)
|
|
|
|
{
|
|
|
|
events |= e;
|
|
|
|
if(cdc_event_process) {
|
|
|
|
process_poll(cdc_event_process);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-07 11:24:59 +02:00
|
|
|
static void
|
|
|
|
encapsulated_command(uint8_t *data, unsigned int length)
|
|
|
|
{
|
2012-08-17 15:56:59 +02:00
|
|
|
PRINTF("Got CDC command: length %d\n", length);
|
2012-08-07 11:24:59 +02:00
|
|
|
usb_send_ctrl_status();
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
set_line_encoding(uint8_t *data, unsigned int length)
|
|
|
|
{
|
2012-10-31 19:48:37 +01:00
|
|
|
if(length == 7) {
|
2012-08-17 15:56:59 +02:00
|
|
|
#ifdef DEBUG
|
2012-10-31 19:48:37 +01:00
|
|
|
static const char parity_char[] = { 'N', 'O', 'E', 'M', 'S' };
|
|
|
|
static const char *stop_bits_str[] = { "1", "1.5", "2" };
|
2012-08-07 11:24:59 +02:00
|
|
|
const struct usb_cdc_line_coding *coding =
|
|
|
|
(const struct usb_cdc_line_coding *)usb_ctrl_data_buffer;
|
|
|
|
char parity = ((coding->bParityType > 4)
|
2012-10-31 19:48:37 +01:00
|
|
|
? '?' : parity_char[coding->bParityType]);
|
|
|
|
|
2012-08-07 11:24:59 +02:00
|
|
|
const char *stop_bits = ((coding->bCharFormat > 2)
|
2012-10-31 19:48:37 +01:00
|
|
|
? "?" : stop_bits_str[coding->bCharFormat]);
|
|
|
|
|
2012-08-17 15:56:59 +02:00
|
|
|
PRINTF("Got CDC line coding: %ld/%d/%c/%s\n",
|
2012-10-31 19:48:37 +01:00
|
|
|
coding->dwDTERate, coding->bDataBits, parity, stop_bits);
|
2012-08-17 15:56:59 +02:00
|
|
|
#endif
|
|
|
|
memcpy(&usb_line_coding, data, sizeof(usb_line_coding));
|
|
|
|
notify_user(USB_CDC_ACM_LINE_CODING);
|
2012-08-07 11:24:59 +02:00
|
|
|
usb_send_ctrl_status();
|
|
|
|
} else {
|
|
|
|
usb_error_stall();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
handle_cdc_acm_requests()
|
|
|
|
{
|
2012-10-31 19:48:37 +01:00
|
|
|
PRINTF("CDC request %02x %02x\n", usb_setup_buffer.bmRequestType,
|
|
|
|
usb_setup_buffer.bRequest);
|
|
|
|
switch (usb_setup_buffer.bmRequestType) {
|
2012-08-07 11:24:59 +02:00
|
|
|
case 0x21: /* CDC interface OUT requests */
|
|
|
|
/* Check if it's the right interface */
|
2012-10-31 19:48:37 +01:00
|
|
|
if(usb_setup_buffer.wIndex != 0)
|
|
|
|
return 0;
|
|
|
|
switch (usb_setup_buffer.bRequest) {
|
2012-08-07 11:24:59 +02:00
|
|
|
case SET_CONTROL_LINE_STATE:
|
2012-08-17 15:56:59 +02:00
|
|
|
line_state = usb_setup_buffer.wValue;
|
|
|
|
notify_user(USB_CDC_ACM_LINE_STATE);
|
2012-08-07 11:24:59 +02:00
|
|
|
usb_send_ctrl_status();
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case SEND_ENCAPSULATED_COMMAND:
|
|
|
|
{
|
2012-10-31 19:48:37 +01:00
|
|
|
unsigned int len = usb_setup_buffer.wLength;
|
|
|
|
if(len > sizeof(usb_ctrl_data_buffer))
|
|
|
|
len = sizeof(usb_ctrl_data_buffer);
|
|
|
|
usb_get_ctrl_data(usb_ctrl_data_buffer, len, encapsulated_command);
|
2012-08-07 11:24:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
|
|
case SET_LINE_CODING:
|
|
|
|
{
|
2012-10-31 19:48:37 +01:00
|
|
|
unsigned int len = usb_setup_buffer.wLength;
|
|
|
|
if(len > sizeof(usb_ctrl_data_buffer))
|
|
|
|
len = sizeof(usb_ctrl_data_buffer);
|
|
|
|
usb_get_ctrl_data(usb_ctrl_data_buffer, len, set_line_encoding);
|
2012-08-07 11:24:59 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2012-10-31 19:48:37 +01:00
|
|
|
case 0xa1: /* CDC interface IN requests */
|
|
|
|
if(usb_setup_buffer.wIndex != 0)
|
|
|
|
return 0;
|
|
|
|
switch (usb_setup_buffer.bRequest) {
|
2012-08-07 11:24:59 +02:00
|
|
|
case GET_ENCAPSULATED_RESPONSE:
|
2012-08-17 15:56:59 +02:00
|
|
|
PRINTF("CDC response");
|
2012-08-07 11:24:59 +02:00
|
|
|
usb_send_ctrl_status();
|
|
|
|
return 1;
|
2012-08-17 15:56:59 +02:00
|
|
|
case GET_LINE_CODING:
|
2012-10-31 19:48:37 +01:00
|
|
|
usb_send_ctrl_response((uint8_t *) & usb_line_coding, 7);
|
2012-08-17 15:56:59 +02:00
|
|
|
return 1;
|
2012-08-07 11:24:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-31 19:48:37 +01:00
|
|
|
static const struct USBRequestHandler cdc_acm_request_handler = {
|
|
|
|
0x21, 0x7f,
|
|
|
|
0x00, 0x00,
|
|
|
|
handle_cdc_acm_requests
|
|
|
|
};
|
2012-08-07 11:24:59 +02:00
|
|
|
|
2012-10-31 19:48:37 +01:00
|
|
|
static struct USBRequestHandlerHook cdc_acm_request_hook = {
|
|
|
|
NULL,
|
|
|
|
&cdc_acm_request_handler
|
|
|
|
};
|
2012-08-07 11:24:59 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
usb_cdc_acm_setup()
|
|
|
|
{
|
|
|
|
usb_register_request_handler(&cdc_acm_request_hook);
|
|
|
|
}
|
2012-08-17 15:56:59 +02:00
|
|
|
|
|
|
|
uint8_t
|
|
|
|
usb_cdc_acm_get_events(void)
|
|
|
|
{
|
|
|
|
uint8_t r = events;
|
|
|
|
events = 0;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
|
|
|
usb_cdc_acm_get_line_state(void)
|
|
|
|
{
|
|
|
|
return line_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct usb_cdc_line_coding *
|
|
|
|
usb_cdc_acm_get_line_coding(void)
|
|
|
|
{
|
|
|
|
return &usb_line_coding;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usb_cdc_acm_set_event_process(struct process *p)
|
|
|
|
{
|
|
|
|
cdc_event_process = p;
|
|
|
|
}
|