Mercurial > ecos-v3_0-branch
annotate packages/io/usb/serial/slave/current/src/usbs_serial.c @ 2729:74dbf4c3f2e1 after-copyright-change-20090129
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
| author | jlarmour |
|---|---|
| date | Thu, 29 Jan 2009 17:47:46 +0000 |
| parents | de43e04cf3a5 |
| children |
| rev | line source |
|---|---|
| 2493 | 1 //========================================================================== |
| 2 // | |
| 3 // usbs_serial.c | |
| 4 // | |
| 5 // Support for slave-side USB serial devices. | |
| 6 // | |
| 7 //========================================================================== | |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
8 // ####ECOSGPLCOPYRIGHTBEGIN#### |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
9 // ------------------------------------------- |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
10 // This file is part of eCos, the Embedded Configurable Operating System. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
11 // Copyright (C) 2008 Free Software Foundation, Inc. |
| 2493 | 12 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
13 // eCos is free software; you can redistribute it and/or modify it under |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
14 // the terms of the GNU General Public License as published by the Free |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
15 // Software Foundation; either version 2 or (at your option) any later |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
16 // version. |
| 2493 | 17 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
18 // eCos is distributed in the hope that it will be useful, but WITHOUT |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
19 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
21 // for more details. |
| 2493 | 22 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
23 // You should have received a copy of the GNU General Public License |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
24 // along with eCos; if not, write to the Free Software Foundation, Inc., |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
25 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 2493 | 26 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
27 // As a special exception, if other files instantiate templates or use |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
28 // macros or inline functions from this file, or you compile this file |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
29 // and link it with other works to produce a work based on this file, |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
30 // this file does not by itself cause the resulting work to be covered by |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
31 // the GNU General Public License. However the source code for this file |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
32 // must still be made available in accordance with section (3) of the GNU |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
33 // General Public License v2. |
| 2493 | 34 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
35 // This exception does not invalidate any other reasons why a work based |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
36 // on this file might be covered by the GNU General Public License. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
37 // ------------------------------------------- |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
2577
diff
changeset
|
38 // ####ECOSGPLCOPYRIGHTEND#### |
| 2493 | 39 //========================================================================== |
| 40 //#####DESCRIPTIONBEGIN#### | |
| 41 // | |
| 42 // Author(s): Frank M. Pagliughi (fmp), SoRo Systems, Inc. | |
| 43 // Date: 2008-06-02 | |
| 44 // | |
| 45 //####DESCRIPTIONEND#### | |
| 46 // | |
| 47 //========================================================================== | |
| 48 | |
| 49 #include <cyg/infra/cyg_type.h> | |
| 50 #include <cyg/infra/cyg_ass.h> | |
| 51 #include <cyg/infra/cyg_trac.h> | |
| 52 #include <cyg/infra/diag.h> | |
| 53 #include <cyg/hal/hal_arch.h> | |
| 54 #include <cyg/hal/drv_api.h> | |
| 55 #include <cyg/kernel/kapi.h> | |
| 56 | |
| 57 #include <pkgconf/io_usb_slave_serial.h> | |
| 58 #include <cyg/io/usb/usbs_serial.h> | |
| 59 #include <string.h> | |
| 60 | |
| 61 #if defined(CYGBLD_IO_USB_SLAVE_SERIAL_DEBUG) | |
| 62 #define DBG diag_printf | |
| 63 #else | |
| 64 #define DBG (1) ? (void)0 : diag_printf | |
| 65 #endif | |
| 66 | |
| 67 #define EP0_MAX_PACKET_SIZE CYGNUM_IO_USB_SLAVE_SERIAL_EP0_MAX_PACKET_SIZE | |
| 68 | |
| 69 extern usbs_control_endpoint CYGDAT_IO_USB_SLAVE_SERIAL_EP0; | |
| 70 extern usbs_tx_endpoint CYGDAT_IO_USB_SLAVE_SERIAL_TX_EP; | |
| 71 extern usbs_rx_endpoint CYGDAT_IO_USB_SLAVE_SERIAL_RX_EP; | |
| 72 | |
| 73 #define TX_EP_NUM CYGNUM_IO_USB_SLAVE_SERIAL_TX_EP_NUM | |
| 74 #define RX_EP_NUM CYGNUM_IO_USB_SLAVE_SERIAL_RX_EP_NUM | |
| 75 #define INTR_EP_NUM CYGNUM_IO_USB_SLAVE_SERIAL_INTR_EP_NUM | |
| 76 #define EP0 (&CYGDAT_IO_USB_SLAVE_SERIAL_EP0) | |
| 77 #define TX_EP (&CYGDAT_IO_USB_SLAVE_SERIAL_TX_EP) | |
| 78 #define RX_EP (&CYGDAT_IO_USB_SLAVE_SERIAL_RX_EP) | |
| 79 #define INTR_EP (&CYGDAT_IO_USB_SLAVE_SERIAL_INTR_EP) | |
| 80 | |
| 81 | |
| 82 #define VENDOR_ID CYGNUM_IO_USB_SLAVE_SERIAL_VENDOR_ID | |
| 83 #define PRODUCT_ID CYGNUM_IO_USB_SLAVE_SERIAL_PRODUCT_ID | |
| 84 | |
| 85 #define USB_MAX_STR_LEN 256 | |
| 86 | |
| 87 #define LO_BYTE_16(word16) ((cyg_uint8) ((word16) & 0xFF)) | |
| 88 #define HI_BYTE_16(word16) ((cyg_uint8) (((word16) >> 8) & 0xFF)) | |
| 89 | |
| 90 #define BYTE0_32(word32) ((cyg_uint8) ((word32) & 0xFF)) | |
| 91 #define BYTE1_32(word32) ((cyg_uint8) (((word32) >> 8) & 0xFF)) | |
| 92 #define BYTE2_32(word32) ((cyg_uint8) (((word32) >> 16) & 0xFF)) | |
| 93 #define BYTE3_32(word32) ((cyg_uint8) (((word32) >> 24) & 0xFF)) | |
| 94 | |
| 95 | |
| 96 #define MFG_STR_INDEX '\x01' | |
| 97 #define PRODUCT_STR_INDEX '\x02' | |
| 98 | |
| 99 #define USB_CONFIGURATION_DESCRIPTOR_TOTAL_LENGTH(interfaces, endpoints) \ | |
| 100 (USB_CONFIGURATION_DESCRIPTOR_LENGTH + \ | |
| 101 ((interfaces) * USB_INTERFACE_DESCRIPTOR_LENGTH) + \ | |
| 102 ((endpoints) * USB_ENDPOINT_DESCRIPTOR_LENGTH)) | |
| 103 | |
| 104 | |
| 105 #ifdef CYGDAT_IO_USB_SLAVE_CLASS_TYPE_ACM | |
| 106 #define USBS_SERIAL_DEVICE_CLASS 2 | |
| 107 #define USBS_SERIAL_NUM_IFACE 2 | |
| 108 #define USBS_SERIAL_NUM_ENDP 3 | |
| 109 #define USBS_SERIAL_DATA_IFACE_CLASS 0x0A // Data | |
| 110 #else | |
| 111 #define USBS_SERIAL_DEVICE_CLASS 0 | |
| 112 #define USBS_SERIAL_NUM_IFACE 1 | |
| 113 #define USBS_SERIAL_NUM_ENDP 2 | |
| 114 #define USBS_SERIAL_DATA_IFACE_CLASS 0xFF // Vendor | |
| 115 #endif | |
| 116 | |
| 117 // ----- Configuration Descriptor ----- | |
| 118 | |
| 119 static const usb_configuration_descriptor usb_configuration = { | |
| 120 length: sizeof(usb_configuration_descriptor), | |
| 121 type: USB_CONFIGURATION_DESCRIPTOR_TYPE, | |
| 122 total_length_lo: | |
| 123 USB_CONFIGURATION_DESCRIPTOR_TOTAL_LENGTH_LO(USBS_SERIAL_NUM_IFACE, | |
| 124 USBS_SERIAL_NUM_ENDP), | |
| 125 total_length_hi: | |
| 126 USB_CONFIGURATION_DESCRIPTOR_TOTAL_LENGTH_HI(USBS_SERIAL_NUM_IFACE, | |
| 127 USBS_SERIAL_NUM_ENDP), | |
| 128 number_interfaces: USBS_SERIAL_NUM_IFACE, | |
| 129 configuration_id: 1, | |
| 130 configuration_str: 0, | |
| 2577 | 131 #ifdef CYGOPT_IO_USB_SLAVE_SERIAL_BUSPOWERED |
| 132 attributes: (USB_CONFIGURATION_DESCRIPTOR_ATTR_REQUIRED), | |
| 133 #else | |
| 2493 | 134 attributes: (USB_CONFIGURATION_DESCRIPTOR_ATTR_REQUIRED | |
| 135 USB_CONFIGURATION_DESCRIPTOR_ATTR_SELF_POWERED), | |
| 2577 | 136 #endif |
| 137 max_power: (CYGNUM_IO_USB_SLAVE_SERIAL_CURRENTDRAW+1)/2 | |
| 2493 | 138 }; |
| 139 | |
| 140 // ----- Interface Descriptor ----- | |
| 141 | |
| 142 static const usb_interface_descriptor usb_interface[] = { | |
| 143 | |
| 144 #ifdef CYGDAT_IO_USB_SLAVE_CLASS_TYPE_ACM | |
| 145 { | |
| 146 length: sizeof(usb_interface_descriptor), | |
| 147 type: USB_INTERFACE_DESCRIPTOR_TYPE, | |
| 148 interface_id: 0, | |
| 149 alternate_setting: 0, | |
| 150 number_endpoints: 1, | |
| 151 interface_class: 0x02, // Comm class | |
| 152 interface_subclass: 0x02, | |
| 153 interface_protocol: 0x01, | |
| 154 interface_str: 0x00 | |
| 155 }, | |
| 156 { | |
| 157 length: sizeof(usb_interface_descriptor), | |
| 158 type: USB_INTERFACE_DESCRIPTOR_TYPE, | |
| 159 interface_id: 1, | |
| 160 alternate_setting: 0, | |
| 161 number_endpoints: 2, | |
| 162 interface_class: USBS_SERIAL_DATA_IFACE_CLASS, | |
| 163 interface_subclass: 0x00, | |
| 164 interface_protocol: 0x00, | |
| 165 interface_str: 0x00 | |
| 166 } | |
| 167 #else | |
| 168 { | |
| 169 length: sizeof(usb_interface_descriptor), | |
| 170 type: USB_INTERFACE_DESCRIPTOR_TYPE, | |
| 171 interface_id: 0, | |
| 172 alternate_setting: 0, | |
| 173 number_endpoints: 2, | |
| 174 interface_class: USBS_SERIAL_DATA_IFACE_CLASS, | |
| 175 interface_subclass: 0x00, | |
| 176 interface_protocol: 0x00, | |
| 177 interface_str: 0x00 | |
| 178 } | |
| 179 #endif | |
| 180 }; | |
| 181 | |
| 182 // ----- Endpoint Descriptors ----- | |
| 183 | |
| 184 static const usb_endpoint_descriptor usb_endpoints[] = | |
| 185 { | |
| 186 #ifdef CYGDAT_IO_USB_SLAVE_CLASS_TYPE_ACM | |
| 187 // Interrupt in endpoint | |
| 188 { | |
| 189 sizeof(usb_endpoint_descriptor), | |
| 190 USB_ENDPOINT_DESCRIPTOR_TYPE, | |
| 191 USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN | INTR_EP_NUM, | |
| 192 USB_ENDPOINT_DESCRIPTOR_ATTR_INTERRUPT, | |
| 193 0x40, | |
| 194 0, | |
| 195 255 | |
| 196 }, | |
| 197 #endif | |
| 198 | |
| 199 // Tx (Bulk IN) Endpoint Descriptor | |
| 200 { | |
| 201 sizeof(usb_endpoint_descriptor), | |
| 202 USB_ENDPOINT_DESCRIPTOR_TYPE, | |
| 203 USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN | TX_EP_NUM, | |
| 204 USB_ENDPOINT_DESCRIPTOR_ATTR_BULK, | |
| 205 0x40, | |
| 206 0, | |
| 207 0 | |
| 208 }, | |
| 209 | |
| 210 // Rx (Bulk OUT) Endpoint Descriptor | |
| 211 { | |
| 212 sizeof(usb_endpoint_descriptor), | |
| 213 USB_ENDPOINT_DESCRIPTOR_TYPE, | |
| 214 USB_ENDPOINT_DESCRIPTOR_ENDPOINT_OUT | RX_EP_NUM, | |
| 215 USB_ENDPOINT_DESCRIPTOR_ATTR_BULK, | |
| 216 0x40, | |
| 217 0, | |
| 218 0 | |
| 219 } | |
| 220 }; | |
| 221 | |
| 222 // ----- String Descriptors ----- | |
| 223 | |
| 224 static char mfg_str_descr[USB_MAX_STR_LEN], | |
| 225 product_str_descr[USB_MAX_STR_LEN]; | |
| 226 | |
| 227 | |
| 228 static const char* usb_strings[] = { | |
| 229 "\x04\x03\x09\x04", | |
| 230 mfg_str_descr, | |
| 231 product_str_descr | |
| 232 }; | |
| 233 | |
| 234 // ----- Enumeration Data w/ Device Descriptor ----- | |
| 235 | |
| 236 static usbs_enumeration_data usb_enum_data = { | |
| 237 { | |
| 238 length: sizeof(usb_device_descriptor), | |
| 239 type: USB_DEVICE_DESCRIPTOR_TYPE, | |
| 240 usb_spec_lo: 0x00, | |
| 241 usb_spec_hi: 0x02, | |
| 242 device_class: USBS_SERIAL_DEVICE_CLASS, | |
| 243 device_subclass: 0, | |
| 244 device_protocol: 0, | |
| 245 max_packet_size: EP0_MAX_PACKET_SIZE, | |
| 246 vendor_lo: LO_BYTE_16(VENDOR_ID), | |
| 247 vendor_hi: HI_BYTE_16(VENDOR_ID), | |
| 248 product_lo: LO_BYTE_16(PRODUCT_ID), | |
| 249 product_hi: HI_BYTE_16(PRODUCT_ID), | |
| 250 device_lo: 0x00, | |
| 251 device_hi: 0x00, | |
| 252 manufacturer_str: MFG_STR_INDEX, | |
| 253 product_str: PRODUCT_STR_INDEX, | |
| 254 serial_number_str: 0, | |
| 255 number_configurations: 1 | |
| 256 }, | |
| 257 | |
| 258 total_number_interfaces: USBS_SERIAL_NUM_IFACE, | |
| 259 total_number_endpoints: USBS_SERIAL_NUM_ENDP, | |
| 260 total_number_strings: 3, | |
| 261 configurations: &usb_configuration, | |
| 262 interfaces: usb_interface, | |
| 263 endpoints: usb_endpoints, | |
| 264 strings: (const unsigned char **) usb_strings | |
| 265 }; | |
| 266 | |
| 267 // -------------------------------------------------------------------------- | |
| 268 // USBS Serial Data | |
| 269 // -------------------------------------------------------------------------- | |
| 270 | |
| 271 usbs_control_endpoint* usbs_serial_ep0 = EP0; | |
| 272 | |
| 273 // Lock for the state. | |
| 274 cyg_mutex_t usbs_serial_lock; | |
| 275 | |
| 276 // Condition variable for state changes | |
| 277 cyg_cond_t usbs_serial_state_cond; | |
| 278 | |
| 279 int usbs_serial_state; | |
| 280 | |
| 281 usbs_serial usbs_ser0 = { | |
| 282 tx_ep: TX_EP, | |
| 283 rx_ep: RX_EP, | |
| 284 tx_result: 0, | |
| 285 rx_result: 0, | |
| 286 }; | |
| 287 | |
| 288 static void (*usbs_serial_app_state_change_fn)(struct usbs_control_endpoint*, | |
| 289 void*, usbs_state_change, int) | |
| 290 = 0; | |
| 291 | |
| 292 // -------------------------------------------------------------------------- | |
| 293 // Create a USB String Descriptor from a C string. | |
| 294 | |
| 295 void | |
| 296 usbs_serial_create_str_descriptor(char descr[], const char *str) | |
| 297 { | |
| 298 int i, n = strlen(str); | |
| 299 | |
| 300 if (n > (USB_MAX_STR_LEN/2 - 2)) | |
| 301 n = USB_MAX_STR_LEN/2 - 2; | |
| 302 | |
| 303 descr[0] = (cyg_uint8) (2*n + 2); | |
| 304 descr[1] = USB_DEVREQ_DESCRIPTOR_TYPE_STRING; | |
| 305 | |
| 306 for (i=0; i<n; i++) { | |
| 307 descr[i*2+2] = str[i]; | |
| 308 descr[i*2+3] = '\x00'; | |
| 309 } | |
| 310 } | |
| 311 | |
| 312 // -------------------------------------------------------------------------- | |
| 313 // ACM Class Handler | |
| 314 // | |
| 315 // For a Windows host, the device must, at least, respond to a SetLineCoding | |
| 316 // request (0x20), otherwise Windows will report that it's unable to open the | |
| 317 // port. This request normally sets the standard serial parameters: | |
| 318 // baud rate, # stop bits, parity, and # data bits | |
| 319 // If we're just making believe that we're a serial port to communicate with | |
| 320 // the host via USB, then these values don't matter. So we ACK the request, | |
| 321 // but ignore the parameters. | |
| 322 // If we were actually creating a USB-serial converter, then we would need to | |
| 323 // read these values and configure the serial port accordingly. | |
| 324 // | |
| 325 // Similarly, the host can request the current settings through a | |
| 326 // GetLineCoding request (0x21). Since we're just faking it, we return some | |
| 327 // arbitrary values: 38400,1,N,8 | |
| 328 | |
| 329 #ifdef CYGDAT_IO_USB_SLAVE_CLASS_TYPE_ACM | |
| 330 | |
| 331 static usbs_control_return | |
| 332 usbs_serial_acm_class_handler(usbs_control_endpoint* ep0, void* data) | |
| 333 { | |
| 334 usbs_control_return result = USBS_CONTROL_RETURN_UNKNOWN; | |
| 335 usb_devreq *req = (usb_devreq *) ep0->control_buffer; | |
| 336 static cyg_uint8 rsp_buf[32]; | |
| 337 cyg_uint32 baud = 38400; // Arbitrary, fake value to return to the host. | |
| 338 | |
| 339 DBG("USB Serial ACM Class Handler: "); | |
| 340 | |
| 341 switch (req->request) { | |
| 342 | |
| 343 case USBS_SERIAL_SET_LINE_CODING : | |
| 344 DBG("ACM Request: Set Line Coding\n"); | |
| 345 result = USBS_CONTROL_RETURN_HANDLED; | |
| 346 break; | |
| 347 | |
| 348 case USBS_SERIAL_GET_LINE_CODING : | |
| 349 DBG("ACM Request: Get Line Coding\n"); | |
| 350 rsp_buf[0] = BYTE0_32(baud); | |
| 351 rsp_buf[1] = BYTE1_32(baud); | |
| 352 rsp_buf[2] = BYTE2_32(baud); | |
| 353 rsp_buf[3] = BYTE3_32(baud); | |
| 354 rsp_buf[4] = 0; // One stop bit | |
| 355 rsp_buf[5] = 0; // No parity | |
| 356 rsp_buf[6] = 8; // 8 data bits | |
| 357 ep0->buffer = rsp_buf; | |
| 358 ep0->buffer_size = 7; | |
| 359 result = USBS_CONTROL_RETURN_HANDLED; | |
| 360 break; | |
| 361 | |
| 362 default : | |
| 363 DBG("*** Unhandled ACM Request: 0x%02X ***\n", | |
| 364 (unsigned) req->request); | |
| 365 } | |
| 366 | |
| 367 return result; | |
| 368 } | |
| 369 #endif // CYGDAT_IO_USB_SLAVE_CLASS_TYPE_ACM | |
| 370 | |
| 371 // -------------------------------------------------------------------------- | |
| 372 // Callback for a USB state change | |
| 373 | |
| 374 void | |
| 375 usbs_serial_state_change_handler(usbs_control_endpoint* ep, void* data, | |
| 376 usbs_state_change change, int prev_state) | |
| 377 { | |
| 378 #if defined(CYGBLD_IO_USB_SLAVE_SERIAL_DEBUG) | |
| 379 const char *STATE_CHG_STR[] = { "Detached", "Attached", "Powered", "Reset", | |
| 380 "Addressed", "Configured", "Deconfigured", | |
| 381 "Suspended", "Resumed" }; | |
| 382 | |
| 383 if (change > 0) { | |
| 384 DBG("### %d:%s ###\n", change, STATE_CHG_STR[(int) change-1]); | |
| 385 } | |
| 386 #endif | |
| 387 | |
| 388 // Called from DSR, cond broadcast should be ok without mutex lock | |
| 389 usbs_serial_state = usbs_serial_ep0->state; | |
| 390 cyg_cond_broadcast(&usbs_serial_state_cond); | |
| 391 | |
| 392 if (usbs_serial_app_state_change_fn) | |
| 393 (*usbs_serial_app_state_change_fn)(ep, data, change, prev_state); | |
| 394 } | |
| 395 | |
| 396 // -------------------------------------------------------------------------- | |
| 397 // Block the calling thread until the USB is configured. | |
| 398 | |
| 399 void | |
| 400 usbs_serial_wait_until_configured(void) | |
| 401 { | |
| 402 cyg_mutex_lock(&usbs_serial_lock); | |
| 403 while (usbs_serial_state != USBS_STATE_CONFIGURED) | |
| 404 cyg_cond_wait(&usbs_serial_state_cond); | |
| 405 cyg_mutex_unlock(&usbs_serial_lock); | |
| 406 } | |
| 407 | |
| 408 // -------------------------------------------------------------------------- | |
| 409 // Determine if the device is currently configured. | |
| 410 | |
| 411 cyg_bool | |
| 412 usbs_serial_is_configured(void) | |
| 413 { | |
| 414 return usbs_serial_state == USBS_STATE_CONFIGURED; | |
| 415 } | |
| 416 | |
| 417 // -------------------------------------------------------------------------- | |
| 418 // Callback for when a transmit is complete | |
| 419 | |
| 420 static void | |
| 421 usbs_serial_tx_complete(void *p, int result) | |
| 422 { | |
| 423 usbs_serial* ser = (usbs_serial*) p; | |
| 424 ser->tx_result = result; | |
| 425 cyg_semaphore_post(&ser->tx_ready); | |
| 426 } | |
| 427 | |
| 428 // -------------------------------------------------------------------------- | |
| 429 // Callback for when a receive is complete | |
| 430 | |
| 431 static void | |
| 432 usbs_serial_rx_complete(void *p, int result) | |
| 433 { | |
| 434 usbs_serial* ser = (usbs_serial*) p; | |
| 435 ser->rx_result = result; | |
| 436 cyg_semaphore_post(&ser->rx_ready); | |
| 437 } | |
| 438 | |
| 439 // -------------------------------------------------------------------------- | |
| 440 // Start an asynchronous transmit of a buffer. | |
| 441 // | |
| 442 | |
| 443 void | |
| 444 usbs_serial_start_tx(usbs_serial* ser, const void* buf, int n) | |
| 445 { | |
| 446 usbs_start_tx_buffer(ser->tx_ep, (unsigned char*) buf, n, | |
| 447 usbs_serial_tx_complete, ser); | |
| 448 } | |
| 449 | |
| 450 // -------------------------------------------------------------------------- | |
| 451 // Block the caller until the transmit is complete | |
| 452 | |
| 453 int | |
| 454 usbs_serial_wait_for_tx(usbs_serial* ser) | |
| 455 { | |
| 456 cyg_semaphore_wait(&ser->tx_ready); | |
| 457 return ser->tx_result; | |
| 458 } | |
| 459 | |
| 460 // -------------------------------------------------------------------------- | |
| 461 // Perform a synchronous transmit and wait for it to complete. | |
| 462 | |
| 463 int | |
| 464 usbs_serial_tx(usbs_serial* ser, const void* buf, int n) | |
| 465 { | |
| 466 usbs_serial_start_tx(ser, buf, n); | |
| 467 return usbs_serial_wait_for_tx(ser); | |
| 468 } | |
| 469 | |
| 470 // -------------------------------------------------------------------------- | |
| 471 // Start an asynchronous receive of a buffer. | |
| 472 | |
| 473 void | |
| 474 usbs_serial_start_rx(usbs_serial* ser, void* buf, int n) | |
| 475 { | |
| 476 usbs_start_rx_buffer(ser->rx_ep, (unsigned char*) buf, n, | |
| 477 usbs_serial_rx_complete, ser); | |
| 478 } | |
| 479 | |
| 480 // -------------------------------------------------------------------------- | |
| 481 // Block the caller until the receive is complete | |
| 482 | |
| 483 int | |
| 484 usbs_serial_wait_for_rx(usbs_serial* ser) | |
| 485 { | |
| 486 cyg_semaphore_wait(&ser->rx_ready); | |
| 487 return ser->rx_result; | |
| 488 } | |
| 489 | |
| 490 // -------------------------------------------------------------------------- | |
| 491 // Perform a synchronous receive and wait for it to complete. | |
| 492 | |
| 493 int | |
| 494 usbs_serial_rx(usbs_serial* ser, void* buf, int n) | |
| 495 { | |
| 496 usbs_serial_start_rx(ser, buf, n); | |
| 497 return usbs_serial_wait_for_rx(ser); | |
| 498 } | |
| 499 | |
| 500 // -------------------------------------------------------------------------- | |
| 501 // Initialize a serial port structure. | |
| 502 | |
| 503 void | |
| 504 usbs_serial_init(usbs_serial* ser, usbs_tx_endpoint* tx_ep, | |
| 505 usbs_rx_endpoint* rx_ep) | |
| 506 { | |
| 507 ser->tx_ep = tx_ep; | |
| 508 ser->rx_ep = rx_ep; | |
| 509 | |
| 510 cyg_semaphore_init(&ser->tx_ready, 0); | |
| 511 cyg_semaphore_init(&ser->rx_ready, 0); | |
| 512 } | |
| 513 | |
| 514 // -------------------------------------------------------------------------- | |
| 515 // Start the USB subsystem | |
| 516 | |
| 517 void | |
| 518 usbs_serial_start(void) | |
| 519 { | |
| 520 usbs_serial_init(&usbs_ser0, TX_EP, RX_EP); | |
| 521 | |
| 522 cyg_mutex_init(&usbs_serial_lock); | |
| 523 cyg_cond_init(&usbs_serial_state_cond, &usbs_serial_lock); | |
| 524 | |
| 525 // Make the mfg & product names into USB string descriptors | |
| 526 | |
| 527 usbs_serial_create_str_descriptor(mfg_str_descr, | |
| 528 CYGDAT_IO_USB_SLAVE_SERIAL_MFG_STR); | |
| 529 usbs_serial_create_str_descriptor(product_str_descr, | |
| 530 CYGDAT_IO_USB_SLAVE_SERIAL_PRODUCT_STR); | |
| 531 | |
| 532 // ----- Set up enumeration & USB callbacks ----- | |
| 533 | |
| 534 usbs_serial_state = usbs_serial_ep0->state; | |
| 535 | |
| 536 usbs_serial_ep0->enumeration_data = &usb_enum_data; | |
| 537 | |
| 538 if (usbs_serial_ep0->state_change_fn) | |
| 539 usbs_serial_app_state_change_fn = usbs_serial_ep0->state_change_fn; | |
| 540 | |
| 541 usbs_serial_ep0->state_change_fn = usbs_serial_state_change_handler; | |
| 542 | |
| 543 #ifdef CYGDAT_IO_USB_SLAVE_CLASS_TYPE_ACM | |
| 544 if (!usbs_serial_ep0->class_control_fn) | |
| 545 usbs_serial_ep0->class_control_fn = usbs_serial_acm_class_handler; | |
| 546 #endif | |
| 547 | |
| 548 // ----- Start USB subsystem ----- | |
| 549 | |
| 550 usbs_start(usbs_serial_ep0); | |
| 551 } |
