diff options
Diffstat (limited to 'app/keyboard.c')
| -rw-r--r-- | app/keyboard.c | 168 | 
1 files changed, 95 insertions, 73 deletions
diff --git a/app/keyboard.c b/app/keyboard.c index a20e070..cecbd58 100644 --- a/app/keyboard.c +++ b/app/keyboard.c @@ -30,30 +30,30 @@ static const uint8_t keyboard_report_descriptor[] = {  //  0x2A, 0xff, 0x03,             /*     Usage maximum (1023) */    0x81, 0x00,                   /*   Input (data, array) */ -  0x05, 0x08,			/* Usage page (leds) */ +  0x05, 0x08,                   /* Usage page (leds) */    0x75, 0x01,                   /*     Report size (1) */    0x95, 0x03,                   /*     Report count (3) */    0x19, 0x01,                   /*     Usage minimum (1) */    0x29, 0x03,                   /*     Usage maximum (3) */ -  0x91, 0x02,			/*     Output (data, variable, absolute ) */ +  0x91, 0x02,                   /*     Output (data, variable, absolute ) */    0x75, 0x05,                   /*     Report size (5) */    0x95, 0x01,                   /*     Report count (1) */ -  0x91, 0x01,			/*     Output (constant ) */ - -  0x05, 0x01,		        /*   USAGE_PAGE (Generic Desktop)      */ -  0x09, 0x80,		        /*   USAGE (System Control)            */ - -  0xA1, 0x01,        		/*   COLLECTION (Application)          */ -  0x75, 0x02,        		/*   REPORT_SIZE (2)                   */ -  0x95, 0x01,        		/*   REPORT_COUNT (1)                  */ -  0x15, 0x01,        		/*   LOGICAL_MIN (1)                   */ -  0x25, 0x03,        		/*   LOGICAL_MAX (3)                   */ -  0x09, 0x82,        		/*   USAGE (System Sleep)              */ -  0x09, 0x81,        		/*   USAGE (System Power Down)         */ -  0x09, 0x83,        		/*   USAGE (System Wake Up)            */ -  0x81, 0x60,        		/*   INPUT (Data Ary Abs NPrf Null)    */ -  0x75, 0x06,        		/*   REPORT_SIZE (6)                   */ -  0x81, 0x03,        		/*   INPUT (Cnst Var Abs)              */ +  0x91, 0x01,                   /*     Output (constant ) */ + +  0x05, 0x01,                   /*   USAGE_PAGE (Generic Desktop)      */ +  0x09, 0x80,                   /*   USAGE (System Control)            */ + +  0xA1, 0x01,                   /*   COLLECTION (Application)          */ +  0x75, 0x02,                   /*   REPORT_SIZE (2)                   */ +  0x95, 0x01,                   /*   REPORT_COUNT (1)                  */ +  0x15, 0x01,                   /*   LOGICAL_MIN (1)                   */ +  0x25, 0x03,                   /*   LOGICAL_MAX (3)                   */ +  0x09, 0x82,                   /*   USAGE (System Sleep)              */ +  0x09, 0x81,                   /*   USAGE (System Power Down)         */ +  0x09, 0x83,                   /*   USAGE (System Wake Up)            */ +  0x81, 0x60,                   /*   INPUT (Data Ary Abs NPrf Null)    */ +  0x75, 0x06,                   /*   REPORT_SIZE (6)                   */ +  0x81, 0x03,                   /*   INPUT (Cnst Var Abs)              */    0xc0,                         /*   END COLLECTION                    */ @@ -87,7 +87,7 @@ const struct usb_endpoint_descriptor keyboard_endpoint = {    .bEndpointAddress = KEYBOARD_EP,    .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,    .wMaxPacketSize = 9, -  .bInterval = 0x1 //0x20, +  .bInterval = 0x1              //0x20,  };  const struct usb_interface_descriptor keyboard_iface = { @@ -117,26 +117,30 @@ keyboard_get_descriptor (uint8_t ** buf, uint16_t * len)  } -	 +  static void -keyboard_send (uint8_t modifiers, uint8_t *key_list) +keyboard_send (uint8_t modifiers, uint8_t * key_list)  {    /*Last byte is the power wakeup stuff that we don't yet support */ -  uint8_t buf[KEYBOARD_EP_TXN_SIZE] = { modifiers, 0, key_list[0], key_list[1], key_list[2], key_list[3], key_list[4], key_list[5] ,0}; +  uint8_t buf[KEYBOARD_EP_TXN_SIZE] = +    { modifiers, 0, key_list[0], key_list[1], key_list[2], key_list[3], +key_list[4], key_list[5], 0 }; -  usbd_ep_write_packet (usbd_dev, KEYBOARD_EP, buf, sizeof(buf)); +  usbd_ep_write_packet (usbd_dev, KEYBOARD_EP, buf, sizeof (buf));  } -void keyboard_dispatch(int sc,int updown) +void +keyboard_dispatch (int sc, int updown)  { -static uint8_t modifiers; -static uint8_t key_list[KEY_LIST_LEN]; -int i; +  static uint8_t modifiers; +  static uint8_t key_list[KEY_LIST_LEN]; +  int i;  /*Windows needs the modifiers spliting out, so we do this as per our   * descriptor, others need them in the list so we do that as well */ -switch (sc) { +  switch (sc) +    {      case USB_K_SC_LEFTCTRL:      case USB_K_SC_LEFTSHIFT:      case USB_K_SC_LEFTALT: @@ -145,70 +149,88 @@ switch (sc) {      case USB_K_SC_RIGHTSHIFT:      case USB_K_SC_RIGHTALT:      case USB_K_SC_RIGHTMETA: -	if (updown)  -		modifiers |= 1 <<(sc-USB_K_SC_LEFTCTRL); -	else  -		modifiers &= ~(1 <<(sc-USB_K_SC_LEFTCTRL)); -} +      if (updown) +        modifiers |= 1 << (sc - USB_K_SC_LEFTCTRL); +      else +        modifiers &= ~(1 << (sc - USB_K_SC_LEFTCTRL)); +    }  /* Some oses are picky and need these not to move about  so we make    * a list of the down keys */ -if (updown) { -for (i=0;i<KEY_LIST_LEN;++i) { -	if ((key_list[i]==0) ||(key_list[i]==sc)) { -		key_list[i]=sc; -		break; -	} -} -} else { -for (i=0;i<KEY_LIST_LEN;++i) { -	if (key_list[i]==sc) { -		key_list[i]=0; -		break; -	} -} -} +  if (updown) +    { +      for (i = 0; i < KEY_LIST_LEN; ++i) +        { +          if ((key_list[i] == 0) || (key_list[i] == sc)) +            { +              key_list[i] = sc; +              break; +            } +        } +    } +  else +    { +      for (i = 0; i < KEY_LIST_LEN; ++i) +        { +          if (key_list[i] == sc) +            { +              key_list[i] = 0; +              break; +            } +        } +    }  #ifdef DEBUG -printf("KBD> %02x %02x %02x %02x %02x %02x %02x\r\n",modifiers, key_list[0], key_list[1], key_list[2], key_list[3], key_list[4], key_list[5]); +  printf ("KBD> %02x %02x %02x %02x %02x %02x %02x\r\n", modifiers, +          key_list[0], key_list[1], key_list[2], key_list[3], key_list[4], +          key_list[5]);  #endif -keyboard_send(modifiers,key_list); +  keyboard_send (modifiers, key_list);  } -		 -int keyboard_control_request(usbd_device * usbd_dev, struct usb_setup_data *req, -                     uint8_t ** buf, uint16_t * len, -                     void (**complete) (usbd_device * usbd_dev, -                                        struct usb_setup_data * req)) -{ -uint8_t at_leds=0; -   -  if (req->bmRequestType != (USB_REQ_TYPE_CLASS|USB_REQ_TYPE_INTERFACE)) return 0; -  if (req->bRequest!=USB_REQ_SET_CONFIGURATION) return 0; -  if (req->wValue!=0x200) return 0; -  if (req->wIndex) return 0; -  if (!len) return 0; -  if (*len!=1) return 0; -  if (!buf || !*buf) return 0; - -  if (**buf & USB_LED_CAPS) at_leds|=AT_LED_CAPS; -  if (**buf & USB_LED_NUMLOCK) at_leds|=AT_LED_NUMLOCK; -  if (**buf & USB_LED_SCROLLLOCK) at_leds|=AT_LED_SCROLLLOCK; +int +keyboard_control_request (usbd_device * usbd_dev, struct usb_setup_data *req, +                          uint8_t ** buf, uint16_t * len, +                          void (**complete) (usbd_device * usbd_dev, +                                             struct usb_setup_data * req)) +{ +  uint8_t at_leds = 0; + +  if (req->bmRequestType != (USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE)) +    return 0; +  if (req->bRequest != USB_REQ_SET_CONFIGURATION) +    return 0; +  if (req->wValue != 0x200) +    return 0; +  if (req->wIndex) +    return 0; +  if (!len) +    return 0; +  if (*len != 1) +    return 0; +  if (!buf || !*buf) +    return 0; + + +  if (**buf & USB_LED_CAPS) +    at_leds |= AT_LED_CAPS; +  if (**buf & USB_LED_NUMLOCK) +    at_leds |= AT_LED_NUMLOCK; +  if (**buf & USB_LED_SCROLLLOCK) +    at_leds |= AT_LED_SCROLLLOCK;  #ifdef DEBUG -  printf("LEDS> %x\r\n",at_leds); +  printf ("LEDS> %x\r\n", at_leds);  #endif    atkbd_set_leds (at_leds); - return 1; +  return 1;  } - -  | 
