USB: make all the read-only descriptors as const
[bertos.git] / bertos / drv / usb_serial.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_usb_serial.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
56 #include <string.h> /* memcpy() */
57
58 #include "drv/usb_serial.h"
59
60 #define USB_SERIAL_VENDOR_ID    0x05f9
61 #define USB_SERIAL_PRODUCT_ID   0xffff
62
63 #define USB_SERIAL_INTERFACES   1
64 #define USB_SERIAL_ENDPOINTS    3
65
66 #define USB_STRING_MANUFACTURER 1
67 #define USB_STRING_PRODUCT      2
68 #define USB_STRING_SERIAL       3
69
70 static usb_device_descriptor_t usb_serial_device_descriptor =
71 {
72         .bLength = sizeof(usb_serial_device_descriptor),
73         .bDescriptorType = USB_DT_DEVICE,
74         .bcdUSB = 0x110,
75         .bDeviceClass = USB_CLASS_COMM,
76         .bDeviceSubClass = 0,
77         .bDeviceProtocol = 0,
78         .idVendor = USB_SERIAL_VENDOR_ID,
79         .idProduct = USB_SERIAL_PRODUCT_ID,
80         .bcdDevice = 0,
81         .iManufacturer = USB_STRING_MANUFACTURER,
82         .iProduct = USB_STRING_PRODUCT,
83         .iSerialNumber = USB_STRING_SERIAL,
84         .bNumConfigurations = 1,
85 };
86
87 static const usb_config_descriptor_t usb_serial_config_descriptor =
88 {
89         .bLength = sizeof(usb_serial_config_descriptor),
90         .bDescriptorType = USB_DT_CONFIG,
91         .bNumInterfaces = USB_SERIAL_INTERFACES,
92         .bConfigurationValue = 1,
93         .iConfiguration = 0,
94         .bmAttributes = USB_CONFIG_ATT_ONE,
95         .bMaxPower = 50, /* 100 mA */
96 };
97
98 static const usb_interface_descriptor_t usb_serial_interface_descriptor =
99 {
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,
108         .iInterface = 0,
109 };
110
111 static const usb_endpoint_descriptor_t usb_serial_ep_report_descriptor =
112 {
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),
118         .bInterval = 1,
119 };
120
121 static const usb_endpoint_descriptor_t usb_serial_ep_in_descriptor =
122 {
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),
128         .bInterval = 0,
129 };
130
131 static const usb_endpoint_descriptor_t usb_serial_ep_out_descriptor =
132 {
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),
138         .bInterval = 0,
139 };
140
141 static const usb_descriptor_header_t *usb_serial_config[] =
142 {
143         (const usb_descriptor_header_t *)&usb_serial_config_descriptor,
144         (const usb_descriptor_header_t *)&usb_serial_interface_descriptor,
145         (const usb_descriptor_header_t *)&usb_serial_ep_report_descriptor,
146         (const usb_descriptor_header_t *)&usb_serial_ep_in_descriptor,
147         (const usb_descriptor_header_t *)&usb_serial_ep_out_descriptor,
148         NULL,
149 };
150
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"));
158
159 static const usb_string_descriptor_t *usb_serial_strings[] =
160 {
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,
165         NULL,
166 };
167
168 /* Global usb-serial descriptor that identifies the usb-serial device */
169 static struct usb_device usb_serial = {
170         .device = &usb_serial_device_descriptor,
171         .config = usb_serial_config,
172         .strings = usb_serial_strings,
173 };
174
175 /* Low-level usb-serial device initialization */
176 static int usb_serial_hw_init(void)
177 {
178 #if CONFIG_KERN
179         MOD_CHECK(proc);
180 #endif
181         if (usb_device_register(&usb_serial) < 0)
182                 return -1;
183         LOG_INFO("usb-serial: registered new USB interface driver\n");
184         return 0;
185 }
186
187 /**
188  * \brief Write a buffer to a usb-serial port.
189  *
190  * \return number of bytes actually written.
191  */
192 static size_t usb_serial_write(struct KFile *fd,
193                         const void *buf, size_t size)
194 {
195         DB(USBSerial *fds = USB_SERIAL_CAST(fd));
196
197         /* Silent compiler warnings if _DEBUG is not enabled */
198         (void)fd;
199         ASSERT(fds->is_open);
200         return usb_ep_write(usb_serial_ep_in_descriptor.bEndpointAddress,
201                                 buf, size);
202 }
203
204 /**
205  * Read at most \a size bytes from a usb-serial port and put them in \a buf
206  *
207  * \return number of bytes actually read.
208  */
209 static size_t usb_serial_read(struct KFile *fd, void *buf, size_t size)
210 {
211         DB(USBSerial *fds = USB_SERIAL_CAST(fd));
212
213         /* Silent compiler warnings if _DEBUG is not enabled */
214         (void)fd;
215         ASSERT(fds->is_open);
216         return usb_ep_read(usb_serial_ep_out_descriptor.bEndpointAddress,
217                                 buf, size);
218 }
219
220 /**
221  * Return the status of a usb-serial port.
222  *
223  * \todo properly implement usb-serial error handling.
224  */
225 static int usb_serial_error(struct KFile *fd)
226 {
227         USBSerial *fds = USB_SERIAL_CAST(fd);
228         return fds->status;
229 }
230
231 /**
232  * Clear the status of a usb-serial port.
233  *
234  * \todo properly implement usb-serial error handling.
235  */
236 static void usb_serial_clearerr(struct KFile *fd)
237 {
238         USBSerial *fds = USB_SERIAL_CAST(fd);
239         fds->status = 0;
240 }
241
242 /**
243  * Close an USB serial port.
244  */
245 static int usb_serial_close(struct KFile *fd)
246 {
247         DB(USBSerial *fds = USB_SERIAL_CAST(fd));
248
249         /* Silent compiler warnings if _DEBUG is not enabled */
250         (void)fd;
251         ASSERT(fds->is_open);
252         DB(fds->is_open = false);
253         return 0;
254 }
255
256 /**
257  * Initialize an USB serial port.
258  *
259  * \param fds KFile Serial struct interface.
260  * \param unit Serial unit to open.
261  */
262 static int usb_serial_open(struct USBSerial *fds, int unit)
263 {
264         unit = unit;
265         ASSERT(!fds->is_open);
266         /* TODO: only a single usb-serial unit is supported for now */
267         ASSERT(unit == 0);
268
269         /* Initialize usb-serial driver */
270         if (usb_serial_hw_init() < 0)
271                 return -1;
272         /* Clear error flags */
273         fds->status = 0;
274         DB(fds->is_open = true);
275
276         return 0;
277 }
278
279 /**
280  * Reopen a usb-serial port.
281  */
282 static struct KFile *usb_serial_reopen(struct KFile *fd)
283 {
284         USBSerial *fds = USB_SERIAL_CAST(fd);
285
286         usb_serial_close(fd);
287         usb_serial_open(fds, fds->unit);
288         return 0;
289 }
290
291 /**
292  * Init serial driver for a usb-serial port \a unit.
293  *
294  * \return 0 if OK, a negative value in case of error.
295  */
296 int usb_serial_init(struct USBSerial *fds, int unit)
297 {
298         memset(fds, 0, sizeof(*fds));
299
300         DB(fds->fd._type = KFT_USB_SERIAL);
301         fds->fd.reopen = usb_serial_reopen;
302         fds->fd.close = usb_serial_close;
303         fds->fd.read = usb_serial_read;
304         fds->fd.write = usb_serial_write;
305         /* TODO: properly implement error handling. */
306         fds->fd.error = usb_serial_error;
307         fds->fd.clearerr = usb_serial_clearerr;
308
309         return usb_serial_open(fds, unit);
310 }