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_usbser.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() */
55 #include <drv/usb_endpoint.h>
57 #include <string.h> /* memcpy() */
59 #include "drv/usbser.h"
61 #define USB_SERIAL_VENDOR_ID 0x05f9
62 #define USB_SERIAL_PRODUCT_ID 0xffff
64 #define USB_SERIAL_INTERFACES 1
65 #define USB_SERIAL_ENDPOINTS 3
67 #define USB_STRING_MANUFACTURER 1
68 #define USB_STRING_PRODUCT 2
69 #define USB_STRING_SERIAL 3
71 static UsbDeviceDesc usb_serial_device_descriptor =
73 .bLength = sizeof(usb_serial_device_descriptor),
74 .bDescriptorType = USB_DT_DEVICE,
76 .bDeviceClass = USB_CLASS_COMM,
79 .idVendor = USB_SERIAL_VENDOR_ID,
80 .idProduct = USB_SERIAL_PRODUCT_ID,
82 .iManufacturer = USB_STRING_MANUFACTURER,
83 .iProduct = USB_STRING_PRODUCT,
84 .iSerialNumber = USB_STRING_SERIAL,
85 .bNumConfigurations = 1,
88 static const UsbConfigDesc usb_serial_config_descriptor =
90 .bLength = sizeof(usb_serial_config_descriptor),
91 .bDescriptorType = USB_DT_CONFIG,
92 .bNumInterfaces = USB_SERIAL_INTERFACES,
93 .bConfigurationValue = 1,
95 .bmAttributes = USB_CONFIG_ATT_ONE,
96 .bMaxPower = 50, /* 100 mA */
99 static const UsbInterfaceDesc usb_serial_interface_descriptor =
101 .bLength = sizeof(usb_serial_interface_descriptor),
102 .bDescriptorType = USB_DT_INTERFACE,
103 .bInterfaceNumber = 0,
104 .bAlternateSetting = 0,
105 .bNumEndpoints = USB_SERIAL_ENDPOINTS,
106 .bInterfaceClass = 0xff,
107 .bInterfaceSubClass = 0,
108 .bInterfaceProtocol = 0,
112 static const UsbEndpointDesc usb_serial_ep_report_descriptor =
114 .bLength = sizeof(usb_serial_ep_report_descriptor),
115 .bDescriptorType = USB_DT_ENDPOINT,
116 .bEndpointAddress = USB_DIR_IN | USB_SERIAL_EP_REPORT,
117 .bmAttributes = USB_ENDPOINT_XFER_INT,
118 .wMaxPacketSize = usb_cpu_to_le16((uint16_t)8),
122 static const UsbEndpointDesc usb_serial_ep_in_descriptor =
124 .bLength = sizeof(usb_serial_ep_in_descriptor),
125 .bDescriptorType = USB_DT_ENDPOINT,
126 .bEndpointAddress = USB_DIR_IN | USB_SERIAL_EP_IN,
127 .bmAttributes = USB_ENDPOINT_XFER_BULK,
128 .wMaxPacketSize = usb_cpu_to_le16((uint16_t)64),
132 static const UsbEndpointDesc usb_serial_ep_out_descriptor =
134 .bLength = sizeof(usb_serial_ep_in_descriptor),
135 .bDescriptorType = USB_DT_ENDPOINT,
136 .bEndpointAddress = USB_DIR_OUT | USB_SERIAL_EP_OUT,
137 .bmAttributes = USB_ENDPOINT_XFER_BULK,
138 .wMaxPacketSize = usb_cpu_to_le16((uint16_t)64),
142 static const UsbDescHeader *usb_serial_config[] =
144 (const UsbDescHeader *)&usb_serial_config_descriptor,
145 (const UsbDescHeader *)&usb_serial_interface_descriptor,
146 (const UsbDescHeader *)&usb_serial_ep_report_descriptor,
147 (const UsbDescHeader *)&usb_serial_ep_in_descriptor,
148 (const UsbDescHeader *)&usb_serial_ep_out_descriptor,
152 static DEFINE_USB_STRING(language_str, "\x09\x04"); // Language ID: en_US
153 static DEFINE_USB_STRING(manufacturer_str,
154 USB_STRING("B", "e", "R", "T", "O", "S"));
155 static DEFINE_USB_STRING(product_str,
156 USB_STRING("U", "S", "B", "-", "s", "e", "r", "i", "a", "l"));
157 static DEFINE_USB_STRING(serial_str,
158 USB_STRING("0", "0", "1"));
160 static const UsbStringDesc *usb_serial_strings[] =
162 (UsbStringDesc *)&language_str,
163 (UsbStringDesc *)&manufacturer_str,
164 (UsbStringDesc *)&product_str,
165 (UsbStringDesc *)&serial_str,
169 /* Global usb-serial descriptor that identifies the usb-serial device */
170 static UsbDevice usb_serial = {
171 .device = &usb_serial_device_descriptor,
172 .config = usb_serial_config,
173 .strings = usb_serial_strings,
176 /* Low-level usb-serial device initialization */
177 static int usb_serial_hw_init(void)
182 if (usb_deviceRegister(&usb_serial) < 0)
184 LOG_INFO("usb-serial: registered new USB interface driver\n");
189 * \brief Write a buffer to a usb-serial port.
191 * \return number of bytes actually written.
193 static size_t usb_serial_write(struct KFile *fd,
194 const void *buf, size_t size)
196 DB(USBSerial *fds = USB_SERIAL_CAST(fd));
198 /* Silent compiler warnings if _DEBUG is not enabled */
200 ASSERT(fds->is_open);
201 return usb_endpointWrite(usb_serial_ep_in_descriptor.bEndpointAddress,
206 * Read at most \a size bytes from a usb-serial port and put them in \a buf
208 * \return number of bytes actually read.
210 static size_t usb_serial_read(struct KFile *fd, void *buf, size_t size)
212 DB(USBSerial *fds = USB_SERIAL_CAST(fd));
214 /* Silent compiler warnings if _DEBUG is not enabled */
216 ASSERT(fds->is_open);
217 return usb_endpointRead(usb_serial_ep_out_descriptor.bEndpointAddress,
222 * Return the status of a usb-serial port.
224 * \todo properly implement usb-serial error handling.
226 static int usb_serial_error(struct KFile *fd)
228 USBSerial *fds = USB_SERIAL_CAST(fd);
233 * Clear the status of a usb-serial port.
235 * \todo properly implement usb-serial error handling.
237 static void usb_serial_clearerr(struct KFile *fd)
239 USBSerial *fds = USB_SERIAL_CAST(fd);
244 * Close an USB serial port.
246 static int usb_serial_close(struct KFile *fd)
248 DB(USBSerial *fds = USB_SERIAL_CAST(fd));
250 /* Silent compiler warnings if _DEBUG is not enabled */
252 ASSERT(fds->is_open);
253 DB(fds->is_open = false);
258 * Initialize an USB serial port.
260 * \param fds KFile Serial struct interface.
261 * \param unit Serial unit to open.
263 static int usb_serial_open(struct USBSerial *fds, int unit)
266 ASSERT(!fds->is_open);
267 /* TODO: only a single usb-serial unit is supported for now */
270 /* Initialize usb-serial driver */
271 if (usb_serial_hw_init() < 0)
273 /* Clear error flags */
275 DB(fds->is_open = true);
281 * Reopen a usb-serial port.
283 static struct KFile *usb_serial_reopen(struct KFile *fd)
285 USBSerial *fds = USB_SERIAL_CAST(fd);
287 usb_serial_close(fd);
288 usb_serial_open(fds, fds->unit);
293 * Init serial driver for a usb-serial port \a unit.
295 * \return 0 if OK, a negative value in case of error.
297 int usbser_init(struct USBSerial *fds, int unit)
299 memset(fds, 0, sizeof(*fds));
301 DB(fds->fd._type = KFT_USB_SERIAL);
302 fds->fd.reopen = usb_serial_reopen;
303 fds->fd.close = usb_serial_close;
304 fds->fd.read = usb_serial_read;
305 fds->fd.write = usb_serial_write;
306 /* TODO: properly implement error handling. */
307 fds->fd.error = usb_serial_error;
308 fds->fd.clearerr = usb_serial_clearerr;
310 return usb_serial_open(fds, unit);