da9750da46973b997504b788b9c55fd2c8f7558c
[bertos.git] / bertos / drv / usbser.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Andrea Righi <arighi@develer.com>
34  *
35  * \brief Generic USB serial device driver.
36  *
37  */
38
39 #include "cfg/cfg_usbser.h"
40
41 #define LOG_LEVEL  USB_SERIAL_LOG_LEVEL
42 #define LOG_FORMAT USB_SERIAL_LOG_FORMAT
43
44 #include <cfg/log.h>
45 #include <cfg/debug.h>
46 #include <cfg/macros.h>
47
48 #include <cfg/compiler.h>
49 #include <cfg/module.h>
50
51 #include <cpu/irq.h> /* IRQ_DISABLE / IRQ_ENABLE */
52 #include <cpu/power.h> /* cpu_relax() */
53
54 #include <drv/usb.h>
55 #include <drv/usb_endpoint.h>
56
57 #include <string.h> /* memcpy() */
58
59 #include "drv/usbser.h"
60
61 #define USB_SERIAL_VENDOR_ID    0x05f9
62 #define USB_SERIAL_PRODUCT_ID   0xffff
63
64 #define USB_SERIAL_INTERFACES   1
65 #define USB_SERIAL_ENDPOINTS    3
66
67 #define USB_STRING_MANUFACTURER 1
68 #define USB_STRING_PRODUCT      2
69 #define USB_STRING_SERIAL       3
70
71 static UsbDeviceDesc usb_serial_device_descriptor =
72 {
73         .bLength = sizeof(usb_serial_device_descriptor),
74         .bDescriptorType = USB_DT_DEVICE,
75         .bcdUSB = 0x110,
76         .bDeviceClass = USB_CLASS_COMM,
77         .bDeviceSubClass = 0,
78         .bDeviceProtocol = 0,
79         .idVendor = USB_SERIAL_VENDOR_ID,
80         .idProduct = USB_SERIAL_PRODUCT_ID,
81         .bcdDevice = 0,
82         .iManufacturer = USB_STRING_MANUFACTURER,
83         .iProduct = USB_STRING_PRODUCT,
84         .iSerialNumber = USB_STRING_SERIAL,
85         .bNumConfigurations = 1,
86 };
87
88 static const UsbConfigDesc usb_serial_config_descriptor =
89 {
90         .bLength = sizeof(usb_serial_config_descriptor),
91         .bDescriptorType = USB_DT_CONFIG,
92         .bNumInterfaces = USB_SERIAL_INTERFACES,
93         .bConfigurationValue = 1,
94         .iConfiguration = 0,
95         .bmAttributes = USB_CONFIG_ATT_ONE,
96         .bMaxPower = 50, /* 100 mA */
97 };
98
99 static const UsbInterfaceDesc usb_serial_interface_descriptor =
100 {
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,
109         .iInterface = 0,
110 };
111
112 static const UsbEndpointDesc usb_serial_ep_report_descriptor =
113 {
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),
119         .bInterval = 1,
120 };
121
122 static const UsbEndpointDesc usb_serial_ep_in_descriptor =
123 {
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),
129         .bInterval = 0,
130 };
131
132 static const UsbEndpointDesc usb_serial_ep_out_descriptor =
133 {
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),
139         .bInterval = 0,
140 };
141
142 static const UsbDescHeader *usb_serial_config[] =
143 {
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,
149         NULL,
150 };
151
152 static const DEFINE_USB_STRING(language_str, "\x09\x04"); // Language ID: en_US
153 static const DEFINE_USB_STRING(manufacturer_str,
154                 USB_STRING("B", "e", "R", "T", "O", "S"));
155 static const DEFINE_USB_STRING(product_str,
156                 USB_STRING("U", "S", "B", "-", "s", "e", "r", "i", "a", "l"));
157 static const DEFINE_USB_STRING(serial_str,
158                 USB_STRING("0", "0", "1"));
159
160 static const UsbStringDesc *usb_serial_strings[] =
161 {
162         (const UsbStringDesc *)&language_str,
163         (const UsbStringDesc *)&manufacturer_str,
164         (const UsbStringDesc *)&product_str,
165         (const UsbStringDesc *)&serial_str,
166         NULL,
167 };
168
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,
174 };
175
176 /* Low-level usb-serial device initialization */
177 static int usb_serial_hw_init(void)
178 {
179 #if CONFIG_KERN
180         MOD_CHECK(proc);
181 #endif
182         if (usb_deviceRegister(&usb_serial) < 0)
183                 return -1;
184         LOG_INFO("usb-serial: registered new USB interface driver\n");
185         return 0;
186 }
187
188 /**
189  * \brief Write a buffer to a usb-serial port.
190  *
191  * \return number of bytes actually written.
192  */
193 static size_t usb_serial_write(struct KFile *fd,
194                         const void *buf, size_t size)
195 {
196         DB(USBSerial *fds = USB_SERIAL_CAST(fd));
197
198         /* Silent compiler warnings if _DEBUG is not enabled */
199         (void)fd;
200         ASSERT(fds->is_open);
201         return usb_endpointWrite(usb_serial_ep_in_descriptor.bEndpointAddress,
202                                 buf, size);
203 }
204
205 /**
206  * Read at most \a size bytes from a usb-serial port and put them in \a buf
207  *
208  * \return number of bytes actually read.
209  */
210 static size_t usb_serial_read(struct KFile *fd, void *buf, size_t size)
211 {
212         DB(USBSerial *fds = USB_SERIAL_CAST(fd));
213
214         /* Silent compiler warnings if _DEBUG is not enabled */
215         (void)fd;
216         ASSERT(fds->is_open);
217         return usb_endpointRead(usb_serial_ep_out_descriptor.bEndpointAddress,
218                                 buf, size);
219 }
220
221 /**
222  * Return the status of a usb-serial port.
223  *
224  * \todo properly implement usb-serial error handling.
225  */
226 static int usb_serial_error(struct KFile *fd)
227 {
228         USBSerial *fds = USB_SERIAL_CAST(fd);
229         return fds->status;
230 }
231
232 /**
233  * Clear the status of a usb-serial port.
234  *
235  * \todo properly implement usb-serial error handling.
236  */
237 static void usb_serial_clearerr(struct KFile *fd)
238 {
239         USBSerial *fds = USB_SERIAL_CAST(fd);
240         fds->status = 0;
241 }
242
243 /**
244  * Close an USB serial port.
245  */
246 static int usb_serial_close(struct KFile *fd)
247 {
248         DB(USBSerial *fds = USB_SERIAL_CAST(fd));
249
250         /* Silent compiler warnings if _DEBUG is not enabled */
251         (void)fd;
252         ASSERT(fds->is_open);
253         DB(fds->is_open = false);
254         return 0;
255 }
256
257 /**
258  * Initialize an USB serial port.
259  *
260  * \param fds KFile Serial struct interface.
261  * \param unit Serial unit to open.
262  */
263 static int usb_serial_open(struct USBSerial *fds, int unit)
264 {
265         unit = unit;
266         ASSERT(!fds->is_open);
267         /* TODO: only a single usb-serial unit is supported for now */
268         ASSERT(unit == 0);
269
270         /* Initialize usb-serial driver */
271         if (usb_serial_hw_init() < 0)
272                 return -1;
273         /* Clear error flags */
274         fds->status = 0;
275         DB(fds->is_open = true);
276
277         return 0;
278 }
279
280 /**
281  * Reopen a usb-serial port.
282  */
283 static struct KFile *usb_serial_reopen(struct KFile *fd)
284 {
285         USBSerial *fds = USB_SERIAL_CAST(fd);
286
287         usb_serial_close(fd);
288         usb_serial_open(fds, fds->unit);
289         return 0;
290 }
291
292 /**
293  * Init serial driver for a usb-serial port \a unit.
294  *
295  * \return 0 if OK, a negative value in case of error.
296  */
297 int usbser_init(struct USBSerial *fds, int unit)
298 {
299         memset(fds, 0, sizeof(*fds));
300
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;
309
310         return usb_serial_open(fds, unit);
311 }