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.
41 #include "cfg/cfg_usbser.h"
43 #define LOG_LEVEL USB_SERIAL_LOG_LEVEL
44 #define LOG_FORMAT USB_SERIAL_LOG_FORMAT
47 #include <cfg/debug.h>
48 #include <cfg/macros.h>
50 #include <cfg/compiler.h>
51 #include <cfg/module.h>
53 #include <cpu/irq.h> /* IRQ_DISABLE / IRQ_ENABLE */
54 #include <cpu/power.h> /* cpu_relax() */
57 #include <drv/usb_endpoint.h>
59 #include <string.h> /* memcpy() */
62 #define USB_SERIAL_INTERFACES 1
63 #define USB_SERIAL_ENDPOINTS 3
65 #define USB_STRING_MANUFACTURER 1
66 #define USB_STRING_PRODUCT 2
67 #define USB_STRING_SERIAL 3
69 static UsbDeviceDesc usb_serial_device_descriptor =
71 .bLength = sizeof(usb_serial_device_descriptor),
72 .bDescriptorType = USB_DT_DEVICE,
74 .bDeviceClass = USB_CLASS_COMM,
77 .idVendor = USB_SERIAL_VENDOR_ID,
78 .idProduct = USB_SERIAL_PRODUCT_ID,
80 .iManufacturer = USB_STRING_MANUFACTURER,
81 .iProduct = USB_STRING_PRODUCT,
82 .iSerialNumber = USB_STRING_SERIAL,
83 .bNumConfigurations = 1,
86 static const UsbConfigDesc usb_serial_config_descriptor =
88 .bLength = sizeof(usb_serial_config_descriptor),
89 .bDescriptorType = USB_DT_CONFIG,
90 .bNumInterfaces = USB_SERIAL_INTERFACES,
91 .bConfigurationValue = 1,
93 .bmAttributes = USB_CONFIG_ATT_ONE,
94 .bMaxPower = 50, /* 100 mA */
97 static const UsbInterfaceDesc usb_serial_interface_descriptor =
99 .bLength = sizeof(usb_serial_interface_descriptor),
100 .bDescriptorType = USB_DT_INTERFACE,
101 .bInterfaceNumber = 0,
102 .bAlternateSetting = 0,
103 .bNumEndpoints = USB_SERIAL_ENDPOINTS,
104 .bInterfaceClass = 0xff,
105 .bInterfaceSubClass = 0,
106 .bInterfaceProtocol = 0,
110 static const UsbEndpointDesc usb_serial_ep_report_descriptor =
112 .bLength = sizeof(usb_serial_ep_report_descriptor),
113 .bDescriptorType = USB_DT_ENDPOINT,
114 .bEndpointAddress = USB_DIR_IN | USB_SERIAL_EP_REPORT,
115 .bmAttributes = USB_ENDPOINT_XFER_INT,
116 .wMaxPacketSize = usb_cpu_to_le16((uint16_t)8),
120 static const UsbEndpointDesc usb_serial_ep_in_descriptor =
122 .bLength = sizeof(usb_serial_ep_in_descriptor),
123 .bDescriptorType = USB_DT_ENDPOINT,
124 .bEndpointAddress = USB_DIR_IN | USB_SERIAL_EP_IN,
125 .bmAttributes = USB_ENDPOINT_XFER_BULK,
126 .wMaxPacketSize = usb_cpu_to_le16((uint16_t)64),
130 static const UsbEndpointDesc usb_serial_ep_out_descriptor =
132 .bLength = sizeof(usb_serial_ep_in_descriptor),
133 .bDescriptorType = USB_DT_ENDPOINT,
134 .bEndpointAddress = USB_DIR_OUT | USB_SERIAL_EP_OUT,
135 .bmAttributes = USB_ENDPOINT_XFER_BULK,
136 .wMaxPacketSize = usb_cpu_to_le16((uint16_t)64),
140 static const UsbDescHeader *usb_serial_config[] =
142 (const UsbDescHeader *)&usb_serial_config_descriptor,
143 (const UsbDescHeader *)&usb_serial_interface_descriptor,
144 (const UsbDescHeader *)&usb_serial_ep_report_descriptor,
145 (const UsbDescHeader *)&usb_serial_ep_in_descriptor,
146 (const UsbDescHeader *)&usb_serial_ep_out_descriptor,
150 static const DEFINE_USB_STRING(language_str, "\x09\x04"); // Language ID: en_US
151 static const DEFINE_USB_STRING(manufacturer_str,
152 USB_STRING("B", "e", "R", "T", "O", "S"));
153 static const DEFINE_USB_STRING(product_str,
154 USB_STRING("U", "S", "B", "-", "s", "e", "r", "i", "a", "l"));
155 static const DEFINE_USB_STRING(serial_str,
156 USB_STRING("0", "0", "1"));
158 static const UsbStringDesc *usb_serial_strings[] =
160 (const UsbStringDesc *)&language_str,
161 (const UsbStringDesc *)&manufacturer_str,
162 (const UsbStringDesc *)&product_str,
163 (const UsbStringDesc *)&serial_str,
167 /* Global usb-serial descriptor that identifies the usb-serial device */
168 static UsbDevice usb_serial = {
169 .device = &usb_serial_device_descriptor,
170 .config = usb_serial_config,
171 .strings = usb_serial_strings,
174 /* Low-level usb-serial device initialization */
175 static int usb_serial_hw_init(void)
180 if (usb_deviceRegister(&usb_serial) < 0)
182 LOG_INFO("usb-serial: registered new USB interface driver\n");
187 * \brief Write a buffer to a usb-serial port.
189 * \return number of bytes actually written.
191 static size_t usb_serial_write(struct KFile *fd,
192 const void *buf, size_t size)
194 DB(USBSerial *fds = USB_SERIAL_CAST(fd));
196 /* Silent compiler warnings if _DEBUG is not enabled */
198 ASSERT(fds->is_open);
199 return usb_endpointWrite(usb_serial_ep_in_descriptor.bEndpointAddress,
204 * Read at most \a size bytes from a usb-serial port and put them in \a buf
206 * \return number of bytes actually read.
208 static size_t usb_serial_read(struct KFile *fd, void *buf, size_t size)
210 DB(USBSerial *fds = USB_SERIAL_CAST(fd));
212 /* Silent compiler warnings if _DEBUG is not enabled */
214 ASSERT(fds->is_open);
215 return usb_endpointRead(usb_serial_ep_out_descriptor.bEndpointAddress,
220 * Return the status of a usb-serial port.
222 * \todo properly implement usb-serial error handling.
224 static int usb_serial_error(struct KFile *fd)
226 USBSerial *fds = USB_SERIAL_CAST(fd);
231 * Clear the status of a usb-serial port.
233 * \todo properly implement usb-serial error handling.
235 static void usb_serial_clearerr(struct KFile *fd)
237 USBSerial *fds = USB_SERIAL_CAST(fd);
242 * Close an USB serial port.
244 static int usb_serial_close(struct KFile *fd)
246 DB(USBSerial *fds = USB_SERIAL_CAST(fd));
248 /* Silent compiler warnings if _DEBUG is not enabled */
250 ASSERT(fds->is_open);
251 DB(fds->is_open = false);
256 * Initialize an USB serial port.
258 * \param fds KFile Serial struct interface.
259 * \param unit Serial unit to open.
261 static int usb_serial_open(struct USBSerial *fds, int unit)
264 ASSERT(!fds->is_open);
265 /* TODO: only a single usb-serial unit is supported for now */
268 /* Initialize usb-serial driver */
269 if (usb_serial_hw_init() < 0)
271 /* Clear error flags */
273 DB(fds->is_open = true);
279 * Reopen a usb-serial port.
281 static struct KFile *usb_serial_reopen(struct KFile *fd)
283 USBSerial *fds = USB_SERIAL_CAST(fd);
285 usb_serial_close(fd);
286 usb_serial_open(fds, fds->unit);
291 * Init serial driver for a usb-serial port \a unit.
293 * \return 0 if OK, a negative value in case of error.
295 int usbser_init(struct USBSerial *fds, int unit)
297 memset(fds, 0, sizeof(*fds));
299 DB(fds->fd._type = KFT_USB_SERIAL);
300 fds->fd.reopen = usb_serial_reopen;
301 fds->fd.close = usb_serial_close;
302 fds->fd.read = usb_serial_read;
303 fds->fd.write = usb_serial_write;
304 /* TODO: properly implement error handling. */
305 fds->fd.error = usb_serial_error;
306 fds->fd.clearerr = usb_serial_clearerr;
308 return usb_serial_open(fds, unit);