1 /***************************************************************************
2 * Copyright (C) 2011-2013 by Martin Schmoelzer *
3 * <martin.schmoelzer@student.tuwien.ac.at> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
21 * Defines USB descriptors, interrupt routines and helper functions.
22 * To minimize code size, we make the following assumptions:
23 * - The OpenULINK has exactly one configuration
24 * - and exactly one alternate setting
26 * Therefore, we do not have to support the Set Configuration USB request.
33 /* Also update external declarations in "include/usb.h" if making changes to
35 volatile bool EP2_out;
38 volatile __xdata __at 0x7FE8 struct setup_data setup_data;
40 /* Define number of endpoints (except Control Endpoint 0) in a central place.
41 * Be sure to include the neccessary endpoint descriptors! */
42 #define NUM_ENDPOINTS 2
44 __code struct usb_device_descriptor device_descriptor = {
45 .bLength = sizeof(struct usb_device_descriptor),
46 .bDescriptorType = DESCRIPTOR_TYPE_DEVICE,
47 .bcdUSB = 0x0110, /* BCD: 01.00 (Version 1.0 USB spec) */
48 .bDeviceClass = 0xFF, /* 0xFF = vendor-specific */
49 .bDeviceSubClass = 0xFF,
50 .bDeviceProtocol = 0xFF,
51 .bMaxPacketSize0 = 64,
58 .bNumConfigurations = 1
61 /* WARNING: ALL config, interface and endpoint descriptors MUST be adjacent! */
63 __code struct usb_config_descriptor config_descriptor = {
64 .bLength = sizeof(struct usb_config_descriptor),
65 .bDescriptorType = DESCRIPTOR_TYPE_CONFIGURATION,
66 .wTotalLength = sizeof(struct usb_config_descriptor) +
67 sizeof(struct usb_interface_descriptor) +
68 (NUM_ENDPOINTS * sizeof(struct usb_endpoint_descriptor)),
70 .bConfigurationValue = 1,
71 .iConfiguration = 4, /* String describing this configuration */
72 .bmAttributes = 0x80, /* Only MSB set according to USB spec */
73 .MaxPower = 50 /* 100 mA */
76 __code struct usb_interface_descriptor interface_descriptor00 = {
77 .bLength = sizeof(struct usb_interface_descriptor),
78 .bDescriptorType = DESCRIPTOR_TYPE_INTERFACE,
79 .bInterfaceNumber = 0,
80 .bAlternateSetting = 0,
81 .bNumEndpoints = NUM_ENDPOINTS,
82 .bInterfaceClass = 0xFF,
83 .bInterfaceSubclass = 0xFF,
84 .bInterfaceProtocol = 0xFF,
88 __code struct usb_endpoint_descriptor Bulk_EP2_IN_Endpoint_Descriptor = {
89 .bLength = sizeof(struct usb_endpoint_descriptor),
90 .bDescriptorType = 0x05,
91 .bEndpointAddress = (2 | USB_DIR_IN),
97 __code struct usb_endpoint_descriptor Bulk_EP2_OUT_Endpoint_Descriptor = {
98 .bLength = sizeof(struct usb_endpoint_descriptor),
99 .bDescriptorType = 0x05,
100 .bEndpointAddress = (2 | USB_DIR_OUT),
101 .bmAttributes = 0x02,
102 .wMaxPacketSize = 64,
106 __code struct usb_language_descriptor language_descriptor = {
108 .bDescriptorType = DESCRIPTOR_TYPE_STRING,
109 .wLANGID = {0x0409 /* US English */}
112 __code struct usb_string_descriptor strManufacturer =
113 STR_DESCR(9, 'O', 'p', 'e', 'n', 'U', 'L', 'I', 'N', 'K');
115 __code struct usb_string_descriptor strProduct =
116 STR_DESCR(9, 'O', 'p', 'e', 'n', 'U', 'L', 'I', 'N', 'K');
118 __code struct usb_string_descriptor strSerialNumber =
119 STR_DESCR(6, '0', '0', '0', '0', '0', '1');
121 __code struct usb_string_descriptor strConfigDescr =
122 STR_DESCR(12, 'J', 'T', 'A', 'G', ' ', 'A', 'd', 'a', 'p', 't', 'e', 'r');
124 /* Table containing pointers to string descriptors */
125 __code struct usb_string_descriptor *__code en_string_descriptors[4] = {
132 void sudav_isr(void) __interrupt SUDAV_ISR
136 usb_handle_setup_data();
142 void sof_isr(void) __interrupt SOF_ISR
145 void sutok_isr(void) __interrupt SUTOK_ISR
148 void suspend_isr(void) __interrupt SUSPEND_ISR
151 void usbreset_isr(void) __interrupt USBRESET_ISR
154 void ibn_isr(void) __interrupt IBN_ISR
158 void ep0in_isr(void) __interrupt EP0IN_ISR
161 void ep0out_isr(void) __interrupt EP0OUT_ISR
164 void ep1in_isr(void) __interrupt EP1IN_ISR
167 void ep1out_isr(void) __interrupt EP1OUT_ISR
172 * EP2 IN: called after the transfer from uC->Host has finished: we sent data
174 void ep2in_isr(void) __interrupt EP2IN_ISR
179 IN07IRQ = IN2IR;/* Clear OUT2 IRQ */
183 * EP2 OUT: called after the transfer from Host->uC has finished: we got data
185 void ep2out_isr(void) __interrupt EP2OUT_ISR
190 OUT07IRQ = OUT2IR; /* Clear OUT2 IRQ */
193 void ep3in_isr(void) __interrupt EP3IN_ISR
196 void ep3out_isr(void) __interrupt EP3OUT_ISR
199 void ep4in_isr(void) __interrupt EP4IN_ISR
202 void ep4out_isr(void) __interrupt EP4OUT_ISR
205 void ep5in_isr(void) __interrupt EP5IN_ISR
208 void ep5out_isr(void) __interrupt EP5OUT_ISR
211 void ep6in_isr(void) __interrupt EP6IN_ISR
214 void ep6out_isr(void) __interrupt EP6OUT_ISR
217 void ep7in_isr(void) __interrupt EP7IN_ISR
220 void ep7out_isr(void) __interrupt EP7OUT_ISR
225 * Return the control/status register for an endpoint
227 * @param ep endpoint address
228 * @return on success: pointer to Control & Status register for endpoint
230 * @return on failure: NULL
232 __xdata uint8_t *usb_get_endpoint_cs_reg(uint8_t ep)
234 /* Mask direction bit */
235 uint8_t ep_num = ep & 0x7F;
242 return ep & 0x80 ? &IN1CS : &OUT1CS;
245 return ep & 0x80 ? &IN2CS : &OUT2CS;
248 return ep & 0x80 ? &IN3CS : &OUT3CS;
251 return ep & 0x80 ? &IN4CS : &OUT4CS;
254 return ep & 0x80 ? &IN5CS : &OUT5CS;
257 return ep & 0x80 ? &IN6CS : &OUT6CS;
260 return ep & 0x80 ? &IN7CS : &OUT7CS;
267 void usb_reset_data_toggle(uint8_t ep)
270 +----+-----+-----+------+-----+-------+-------+-------+
271 | Q | S | R | IO | 0 | EP2 | EP1 | EP0 |
272 +----+-----+-----+------+-----+-------+-------+-------+
274 To reset data toggle bits, we have to write the endpoint direction (IN/OUT)
275 to the IO bit and the endpoint number to the EP2..EP0 bits. Then, in a
276 separate write cycle, the R bit needs to be set.
278 uint8_t togctl_value = (ep & 0x80 >> 3) | (ep & 0x7);
280 /* First step: Write EP number and direction bit */
281 TOGCTL = togctl_value;
283 /* Second step: Set R bit */
284 togctl_value |= TOG_R;
285 TOGCTL = togctl_value;
289 * Handle GET_STATUS request.
291 * @return on success: true
292 * @return on failure: false
294 bool usb_handle_get_status(void)
298 switch (setup_data.bmRequestType) {
300 /* Two byte response: Byte 0, Bit 0 = self-powered, Bit 1 = remote wakeup.
301 * Byte 1: reserved, reset to zero */
309 /* Always return two zero bytes according to USB 1.1 spec, p. 191 */
317 /* Get stall bit for endpoint specified in low byte of wIndex */
318 ep_cs = usb_get_endpoint_cs_reg(setup_data.wIndex & 0xff);
320 if (*ep_cs & EPSTALL)
325 /* Second byte sent has to be always zero */
340 * Handle CLEAR_FEATURE request.
342 * @return on success: true
343 * @return on failure: false
345 bool usb_handle_clear_feature(void)
347 __xdata uint8_t *ep_cs;
349 switch (setup_data.bmRequestType) {
351 /* Clear remote wakeup not supported: stall EP0 */
355 if (setup_data.wValue == 0) {
356 /* Unstall the endpoint specified in wIndex */
357 ep_cs = usb_get_endpoint_cs_reg(setup_data.wIndex);
362 /* Unsupported feature, stall EP0 */
367 /* Vendor commands... */
374 * Handle SET_FEATURE request.
376 * @return on success: true
377 * @return on failure: false
379 bool usb_handle_set_feature(void)
381 __xdata uint8_t *ep_cs;
383 switch (setup_data.bmRequestType) {
385 if (setup_data.wValue == 2)
389 if (setup_data.wValue == 0) {
390 /* Stall the endpoint specified in wIndex */
391 ep_cs = usb_get_endpoint_cs_reg(setup_data.wIndex);
396 /* Unsupported endpoint feature */
401 /* Vendor commands... */
409 * Handle GET_DESCRIPTOR request.
411 * @return on success: true
412 * @return on failure: false
414 bool usb_handle_get_descriptor(void)
416 __xdata uint8_t descriptor_type;
417 __xdata uint8_t descriptor_index;
419 descriptor_type = (setup_data.wValue & 0xff00) >> 8;
420 descriptor_index = setup_data.wValue & 0x00ff;
422 switch (descriptor_type) {
423 case DESCRIPTOR_TYPE_DEVICE:
424 SUDPTRH = HI8(&device_descriptor);
425 SUDPTRL = LO8(&device_descriptor);
427 case DESCRIPTOR_TYPE_CONFIGURATION:
428 SUDPTRH = HI8(&config_descriptor);
429 SUDPTRL = LO8(&config_descriptor);
431 case DESCRIPTOR_TYPE_STRING:
432 if (setup_data.wIndex == 0) {
433 /* Supply language descriptor */
434 SUDPTRH = HI8(&language_descriptor);
435 SUDPTRL = LO8(&language_descriptor);
436 } else if (setup_data.wIndex == 0x0409 /* US English */) {
437 /* Supply string descriptor */
438 SUDPTRH = HI8(en_string_descriptors[descriptor_index - 1]);
439 SUDPTRL = LO8(en_string_descriptors[descriptor_index - 1]);
444 /* Unsupported descriptor type */
453 * Handle SET_INTERFACE request.
455 void usb_handle_set_interface(void)
457 /* Reset Data Toggle */
458 usb_reset_data_toggle(USB_DIR_IN | 2);
459 usb_reset_data_toggle(USB_DIR_OUT | 2);
461 /* Unstall & clear busy flag of all valid IN endpoints */
464 /* Unstall all valid OUT endpoints, reset bytecounts */
470 * Handle the arrival of a USB Control Setup Packet.
472 void usb_handle_setup_data(void)
474 switch (setup_data.bRequest) {
476 if (!usb_handle_get_status())
480 if (!usb_handle_clear_feature())
484 /* Reserved values */
488 if (!usb_handle_set_feature())
492 /* Handled by USB core */
495 /* Set Descriptor not supported. */
499 if (!usb_handle_get_descriptor())
502 case GET_CONFIGURATION:
503 /* OpenULINK has only one configuration, return its index */
504 IN0BUF[0] = config_descriptor.bConfigurationValue;
507 case SET_CONFIGURATION:
508 /* OpenULINK has only one configuration -> nothing to do */
511 /* OpenULINK only has one interface, return its number */
512 IN0BUF[0] = interface_descriptor00.bInterfaceNumber;
516 usb_handle_set_interface();
519 /* Isochronous endpoints not used -> nothing to do */
522 /* Any other requests: do nothing */
528 * USB initialization. Configures USB interrupts, endpoints and performs
533 /* Mark endpoint 2 IN & OUT as valid */
537 /* Make sure no isochronous endpoints are marked valid */
541 /* Disable isochronous endpoints. This makes the isochronous data buffers
542 * available as 8051 XDATA memory at address 0x2000 - 0x27FF */
545 /* Enable USB Autovectoring */
548 /* Enable SUDAV interrupt */
551 /* Enable EP2 OUT & IN interrupts */
555 /* Enable USB interrupt (EIE register) */
558 /* Perform ReNumeration */
559 USBCS = DISCON | RENUM;
561 USBCS = DISCOE | RENUM;