Changes in / [243cb86:ebb98c5] in mainline
- Location:
- uspace
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/virtusbkbd/Makefile
r243cb86 rebb98c5 33 33 34 34 LIBS = $(LIBUSB_PREFIX)/libusb.a $(LIBUSBVIRT_PREFIX)/libusbvirt.a 35 EXTRA_CFLAGS = -I$(LIBUSB_PREFIX)/include -I$(LIB_PREFIX) -I$(LIBDRV_PREFIX)/include35 EXTRA_CFLAGS = -I$(LIBUSB_PREFIX)/include -I$(LIB_PREFIX) 36 36 37 37 SOURCES = \ -
uspace/drv/usbkbd/main.c
r243cb86 rebb98c5 28 28 #include <usb/usbdrv.h> 29 29 #include <driver.h> 30 #include <ipc/driver.h>31 30 #include <errno.h> 32 #include <fibril.h>33 #include <usb/classes/hid.h>34 #include <usb/classes/hidparser.h>35 #include <usb/devreq.h>36 31 37 32 #define BUFFER_SIZE 32 38 #define NAME "usbkbd"39 33 40 static const usb_endpoint_t CONTROL_EP = 0; 41 42 /* 43 * Callbacks for parser 44 */ 45 static void usbkbd_process_keycodes(const uint16_t *key_codes, size_t count, 46 void *arg) 47 { 48 49 } 50 51 /* 52 * Kbd functions 53 */ 54 static int usbkbd_get_descriptors() 55 { 56 // copy-pasted: 57 58 /* Prepare the setup packet. */ 59 usb_device_request_setup_packet_t setup_packet = { 60 .request_type = 128, 61 .request = USB_DEVREQ_GET_DESCRIPTOR, 62 .index = 0, 63 .length = sizeof(usb_standard_device_descriptor_t) 64 }; 65 66 setup_packet.value_high = USB_DESCTYPE_DEVICE; 67 setup_packet.value_low = 0; 68 69 /* Prepare local descriptor. */ 70 size_t actually_transferred = 0; 71 usb_standard_device_descriptor_t descriptor_tmp; 72 73 /* Perform the control read transaction. */ 74 int rc = usb_drv_psync_control_read(phone, target, 75 &setup_packet, sizeof(setup_packet), 76 &descriptor_tmp, sizeof(descriptor_tmp), &actually_transferred); 77 78 if (rc != EOK) { 79 return rc; 80 } 81 82 // end of copy-paste 83 } 84 85 static usb_hid_dev_kbd_t *usbkbd_init_device(device_t *dev) 86 { 87 usb_hid_dev_kbd_t *kbd_dev = (usb_hid_dev_kbd_t *)malloc( 88 sizeof(usb_hid_dev_kbd_t)); 89 90 if (kbd_dev == NULL) { 91 fprintf(stderr, NAME ": No memory!\n"); 92 return NULL; 93 } 94 95 kbd_dev->device = dev; 96 97 // get phone to my HC and save it as my parent's phone 98 // TODO: maybe not a good idea if DDF will use parent_phone 99 kbd_dev->device->parent_phone = usb_drv_hc_connect(dev, 0); 100 101 kbd_dev->address = usb_drv_get_my_address(dev->parent_phone, 102 dev); 103 104 // doesn't matter now that we have no address 105 // if (kbd_dev->address < 0) { 106 // fprintf(stderr, NAME ": No device address!\n"); 107 // free(kbd_dev); 108 // return NULL; 109 // } 110 111 // default endpoint 112 kbd_dev->default_ep = CONTROL_EP; 113 114 // TODO: get descriptors 115 usbkbd_get_descriptors(); 116 // TODO: parse descriptors and save endpoints 117 118 return kbd_dev; 119 } 120 121 static void usbkbd_process_interrupt_in(usb_hid_dev_kbd_t *kbd_dev, 122 uint8_t *buffer, size_t actual_size) 123 { 124 /* 125 * here, the parser will be called, probably with some callbacks 126 * now only take last 6 bytes and process, i.e. send to kbd 127 */ 128 129 usb_hid_report_in_callbacks_t *callbacks = 130 (usb_hid_report_in_callbacks_t *)malloc( 131 sizeof(usb_hid_report_in_callbacks_t)); 132 callbacks->keyboard = usbkbd_process_keycodes; 133 134 usb_hid_parse_report(kbd_dev->parser, buffer, callbacks, NULL); 135 } 136 137 static void usbkbd_poll_keyboard(usb_hid_dev_kbd_t *kbd_dev) 34 /* Call this periodically to check keyboard status changes. */ 35 static void poll_keyboard(device_t *dev) 138 36 { 139 37 int rc; 140 38 usb_handle_t handle; 141 uint8_tbuffer[BUFFER_SIZE];39 char buffer[BUFFER_SIZE]; 142 40 size_t actual_size; 143 //usb_endpoint_t poll_endpoint = 1;41 usb_endpoint_t poll_endpoint = 1; 144 42 145 //usb_address_t my_address = usb_drv_get_my_address(dev->parent_phone,146 //dev);147 //if (my_address < 0) {148 //return;149 //}43 usb_address_t my_address = usb_drv_get_my_address(dev->parent_phone, 44 dev); 45 if (my_address < 0) { 46 return; 47 } 150 48 151 49 usb_target_t poll_target = { 152 .address = kbd_dev->address,153 .endpoint = kbd_dev->default_ep50 .address = my_address, 51 .endpoint = poll_endpoint 154 52 }; 155 53 156 while (true) { 157 rc = usb_drv_async_interrupt_in(kbd_dev->device->parent_phone, 158 poll_target, buffer, BUFFER_SIZE, &actual_size, &handle); 159 160 if (rc != EOK) { 161 continue; 162 } 163 164 rc = usb_drv_async_wait_for(handle); 165 if (rc != EOK) { 166 continue; 167 } 168 169 /* 170 * If the keyboard answered with NAK, it returned no data. 171 * This implies that no change happened since last query. 172 */ 173 if (actual_size == 0) { 174 continue; 175 } 176 177 /* 178 * TODO: Process pressed keys. 179 */ 180 usbkbd_process_interrupt_in(kbd_dev, buffer, actual_size); 54 rc = usb_drv_async_interrupt_in(dev->parent_phone, poll_target, 55 buffer, BUFFER_SIZE, &actual_size, &handle); 56 if (rc != EOK) { 57 return; 181 58 } 182 59 183 // not reached 184 assert(0); 60 rc = usb_drv_async_wait_for(handle); 61 if (rc != EOK) { 62 return; 63 } 64 65 /* 66 * If the keyboard answered with NAK, it returned no data. 67 * This implies that no change happened since last query. 68 */ 69 if (actual_size == 0) { 70 return; 71 } 72 73 /* 74 * Process pressed keys. 75 */ 185 76 } 186 77 187 static int usbkbd_fibril_device(void *arg) 188 { 189 printf("!!! USB device fibril\n"); 190 191 if (arg == NULL) { 192 printf("No device!\n"); 193 return -1; 194 } 195 196 device_t *dev = (device_t *)arg; 197 198 // initialize device (get and process descriptors, get address, etc.) 199 usb_hid_dev_kbd_t *kbd_dev = usbkbd_init_device(dev); 200 201 usbkbd_poll_keyboard(kbd_dev); 202 203 return EOK; 204 } 205 206 static int usbkbd_add_device(device_t *dev) 78 static int add_kbd_device(device_t *dev) 207 79 { 208 80 /* For now, fail immediately. */ 209 //return ENOTSUP;81 return ENOTSUP; 210 82 211 83 /* 212 84 * When everything is okay, connect to "our" HC. 213 *214 * Not supported yet, skip..215 85 */ 216 //int phone = usb_drv_hc_connect(dev, 0);217 //if (phone < 0) {218 ///*219 //* Connecting to HC failed, roll-back and announce220 //* failure.221 //*/222 //return phone;223 //}86 int phone = usb_drv_hc_connect(dev, 0); 87 if (phone < 0) { 88 /* 89 * Connecting to HC failed, roll-back and announce 90 * failure. 91 */ 92 return phone; 93 } 224 94 225 //dev->parent_phone = phone;95 dev->parent_phone = phone; 226 96 227 97 /* 228 * Create new fibril for handling this keyboard98 * Just for fun ;-). 229 99 */ 230 fid_t fid = fibril_create(usbkbd_fibril_device, dev); 231 if (fid == 0) { 232 printf("%s: failed to start fibril for HID device\n", NAME); 233 return ENOMEM; 234 } 235 fibril_add_ready(fid); 100 poll_keyboard(dev); 236 101 237 102 /* … … 242 107 243 108 static driver_ops_t kbd_driver_ops = { 244 .add_device = usbkbd_add_device,109 .add_device = add_kbd_device, 245 110 }; 246 111 247 112 static driver_t kbd_driver = { 248 .name = NAME,113 .name = "usbkbd", 249 114 .driver_ops = &kbd_driver_ops 250 115 }; -
uspace/lib/usb/include/usb/classes/hid.h
r243cb86 rebb98c5 36 36 #define LIBUSB_HID_H_ 37 37 38 #include <usb/usb.h>39 #include <driver.h>40 #include <usb/classes/hidparser.h>41 42 38 /** USB/HID device requests. */ 43 39 typedef enum { … … 58 54 } usb_hid_protocol_t; 59 55 60 /**61 * @brief USB/HID keyboard device type.62 *63 * Quite dummy right now.64 */65 typedef struct {66 device_t *device;67 usb_address_t address;68 usb_endpoint_t default_ep;69 usb_hid_report_parser_t *parser;70 } usb_hid_dev_kbd_t;71 72 56 #endif 73 57 /** -
uspace/lib/usb/include/usb/classes/hidparser.h
r243cb86 rebb98c5 50 50 * @param arg Custom argument. 51 51 */ 52 void (*keyboard)(const uint 16_t *key_codes, size_t count, void *arg);52 void (*keyboard)(const uint32_t *key_codes, size_t count, void *arg); 53 53 } usb_hid_report_in_callbacks_t; 54 54 -
uspace/lib/usb/src/hidparser.c
r243cb86 rebb98c5 62 62 const usb_hid_report_in_callbacks_t *callbacks, void *arg) 63 63 { 64 int i; 65 66 // TODO: parse report 67 68 uint16_t keys[6]; 69 70 for (i = 0; i < 6; ++i) { 71 keys[i] = data[i]; 72 } 73 74 callbacks->keyboard(keys, 6, arg); 64 return ENOTSUP; 75 65 } 76 66
Note:
See TracChangeset
for help on using the changeset viewer.