4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \author Andrea Righi <arighi@develer.com>
35 * \brief Generic USB serial device driver.
39 #include "cfg/cfg_usb_serial.h"
41 #define LOG_LEVEL USB_SERIAL_LOG_LEVEL
42 #define LOG_FORMAT USB_SERIAL_LOG_FORMAT
45 #include <cfg/debug.h>
46 #include <cfg/macros.h>
48 #include <cfg/compiler.h>
49 #include <cfg/module.h>
51 #include <cpu/irq.h> /* IRQ_DISABLE / IRQ_ENABLE */
52 #include <cpu/power.h> /* cpu_relax() */
56 #include <string.h> /* memcpy() */
58 #include "drv/usb_serial.h"
60 #define USB_SERIAL_VENDOR_ID 0x05f9
61 #define USB_SERIAL_PRODUCT_ID 0xffff
63 #define USB_SERIAL_INTERFACES 1
64 #define USB_SERIAL_ENDPOINTS 3
66 #define USB_STRING_MANUFACTURER 1
67 #define USB_STRING_PRODUCT 2
68 #define USB_STRING_SERIAL 3
70 static usb_device_descriptor_t usb_serial_device_descriptor =
72 .bLength = sizeof(usb_serial_device_descriptor),
73 .bDescriptorType = USB_DT_DEVICE,
75 .bDeviceClass = USB_CLASS_COMM,
78 .idVendor = USB_SERIAL_VENDOR_ID,
79 .idProduct = USB_SERIAL_PRODUCT_ID,
81 .iManufacturer = USB_STRING_MANUFACTURER,
82 .iProduct = USB_STRING_PRODUCT,
83 .iSerialNumber = USB_STRING_SERIAL,
84 .bNumConfigurations = 1,
87 static usb_config_descriptor_t usb_serial_config_descriptor =
89 .bLength = sizeof(usb_serial_config_descriptor),
90 .bDescriptorType = USB_DT_CONFIG,
91 .bNumInterfaces = USB_SERIAL_INTERFACES,
92 .bConfigurationValue = 1,
94 .bmAttributes = USB_CONFIG_ATT_ONE,
95 .bMaxPower = 50, /* 100 mA */
98 static usb_interface_descriptor_t usb_serial_interface_descriptor =
100 .bLength = sizeof(usb_serial_interface_descriptor),
101 .bDescriptorType = USB_DT_INTERFACE,
102 .bInterfaceNumber = 0,
103 .bAlternateSetting = 0,
104 .bNumEndpoints = USB_SERIAL_ENDPOINTS,
105 .bInterfaceClass = 0xff,
106 .bInterfaceSubClass = 0,
107 .bInterfaceProtocol = 0,
111 static usb_endpoint_descriptor_t usb_serial_ep_report_descriptor =
113 .bLength = sizeof(usb_serial_ep_report_descriptor),
114 .bDescriptorType = USB_DT_ENDPOINT,
115 .bEndpointAddress = USB_DIR_IN | 1,
116 .bmAttributes = USB_ENDPOINT_XFER_INT,
117 .wMaxPacketSize = usb_cpu_to_le16(8),
121 static usb_endpoint_descriptor_t usb_serial_ep_in_descriptor =
123 .bLength = sizeof(usb_serial_ep_in_descriptor),
124 .bDescriptorType = USB_DT_ENDPOINT,
125 .bEndpointAddress = USB_DIR_IN | 3,
126 .bmAttributes = USB_ENDPOINT_XFER_BULK,
127 .wMaxPacketSize = usb_cpu_to_le16(64),
131 static usb_endpoint_descriptor_t usb_serial_ep_out_descriptor =
133 .bLength = sizeof(usb_serial_ep_in_descriptor),
134 .bDescriptorType = USB_DT_ENDPOINT,
135 .bEndpointAddress = USB_DIR_OUT | 2,
136 .bmAttributes = USB_ENDPOINT_XFER_BULK,
137 .wMaxPacketSize = usb_cpu_to_le16(64),
141 static usb_descriptor_header_t *usb_serial_config[] =
143 (usb_descriptor_header_t *)&usb_serial_config_descriptor,
144 (usb_descriptor_header_t *)&usb_serial_interface_descriptor,
145 (usb_descriptor_header_t *)&usb_serial_ep_report_descriptor,
146 (usb_descriptor_header_t *)&usb_serial_ep_in_descriptor,
147 (usb_descriptor_header_t *)&usb_serial_ep_out_descriptor,
151 static DEFINE_USB_STRING(language_str, "\x09\x04"); // Language ID: en_US
152 static DEFINE_USB_STRING(manufacturer_str,
153 USB_STRING("B", "e", "R", "T", "O", "S"));
154 static DEFINE_USB_STRING(product_str,
155 USB_STRING("U", "S", "B", "-", "s", "e", "r", "i", "a", "l"));
156 static DEFINE_USB_STRING(serial_str,
157 USB_STRING("0", "0", "1"));
159 static usb_string_descriptor_t *usb_serial_strings[] =
161 (usb_string_descriptor_t *)&language_str,
162 (usb_string_descriptor_t *)&manufacturer_str,
163 (usb_string_descriptor_t *)&product_str,
164 (usb_string_descriptor_t *)&serial_str,
168 static struct usb_device usb_serial = {
169 .device = &usb_serial_device_descriptor,
170 .config = usb_serial_config,
171 .strings = usb_serial_strings,
174 ssize_t usb_serial_read(void *buffer, ssize_t size)
176 return usb_ep_read(usb_serial_ep_out_descriptor.bEndpointAddress,
180 ssize_t usb_serial_write(const void *buffer, ssize_t size)
182 return usb_ep_write(usb_serial_ep_in_descriptor.bEndpointAddress,
186 int usb_serial_init(void)
191 if (usb_device_register(&usb_serial) < 0)
193 LOG_INFO("usb-serial: registered new USB interface driver\n");