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