this is the Peter Barrett USBHostShell program, a bit modified for being a bit more talkactive, just to let u know how the USB protocol layer is going on, and all the data trasnfers. Also there is a small implementation of HID descriptors, but not functional... yet :S the aim is to at least implement the gamepad HID, and make an array of function pointer to each HID function
AutoEvents.cpp@0:e1e03118b8fe, 2010-09-13 (annotated)
- Committer:
- Sergio
- Date:
- Mon Sep 13 12:40:05 2010 +0000
- Revision:
- 0:e1e03118b8fe
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergio | 0:e1e03118b8fe | 1 | |
Sergio | 0:e1e03118b8fe | 2 | /* |
Sergio | 0:e1e03118b8fe | 3 | Copyright (c) 2010 Peter Barrett |
Sergio | 0:e1e03118b8fe | 4 | |
Sergio | 0:e1e03118b8fe | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
Sergio | 0:e1e03118b8fe | 6 | of this software and associated documentation files (the "Software"), to deal |
Sergio | 0:e1e03118b8fe | 7 | in the Software without restriction, including without limitation the rights |
Sergio | 0:e1e03118b8fe | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Sergio | 0:e1e03118b8fe | 9 | copies of the Software, and to permit persons to whom the Software is |
Sergio | 0:e1e03118b8fe | 10 | furnished to do so, subject to the following conditions: |
Sergio | 0:e1e03118b8fe | 11 | |
Sergio | 0:e1e03118b8fe | 12 | The above copyright notice and this permission notice shall be included in |
Sergio | 0:e1e03118b8fe | 13 | all copies or substantial portions of the Software. |
Sergio | 0:e1e03118b8fe | 14 | |
Sergio | 0:e1e03118b8fe | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Sergio | 0:e1e03118b8fe | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Sergio | 0:e1e03118b8fe | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Sergio | 0:e1e03118b8fe | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Sergio | 0:e1e03118b8fe | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Sergio | 0:e1e03118b8fe | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Sergio | 0:e1e03118b8fe | 21 | THE SOFTWARE. |
Sergio | 0:e1e03118b8fe | 22 | */ |
Sergio | 0:e1e03118b8fe | 23 | |
Sergio | 0:e1e03118b8fe | 24 | #include "mbed.h" |
Sergio | 0:e1e03118b8fe | 25 | #include "USBHost.h" |
Sergio | 0:e1e03118b8fe | 26 | #include "Utils.h" |
Sergio | 0:e1e03118b8fe | 27 | |
Sergio | 0:e1e03118b8fe | 28 | #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol) |
Sergio | 0:e1e03118b8fe | 29 | #define AUTO_KEYBOARD AUTOEVT(CLASS_HID,1,1) |
Sergio | 0:e1e03118b8fe | 30 | #define AUTO_MOUSE AUTOEVT(CLASS_HID,1,2) |
Sergio | 0:e1e03118b8fe | 31 | |
Sergio | 0:e1e03118b8fe | 32 | u8 auto_mouse[4]; // buttons,dx,dy,scroll |
Sergio | 0:e1e03118b8fe | 33 | u8 auto_keyboard[8]; // modifiers,reserved,keycode1..keycode6 |
Sergio | 0:e1e03118b8fe | 34 | u8 auto_joystick[4]; // x,y,buttons,throttle |
Sergio | 0:e1e03118b8fe | 35 | |
Sergio | 0:e1e03118b8fe | 36 | void AutoEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData) |
Sergio | 0:e1e03118b8fe | 37 | { |
Sergio | 0:e1e03118b8fe | 38 | int evt = (int)userData; |
Sergio | 0:e1e03118b8fe | 39 | switch (evt) |
Sergio | 0:e1e03118b8fe | 40 | { |
Sergio | 0:e1e03118b8fe | 41 | case AUTO_KEYBOARD: |
Sergio | 0:e1e03118b8fe | 42 | printf("AUTO_KEYBOARD "); |
Sergio | 0:e1e03118b8fe | 43 | break; |
Sergio | 0:e1e03118b8fe | 44 | case AUTO_MOUSE: |
Sergio | 0:e1e03118b8fe | 45 | printf("AUTO_MOUSE "); |
Sergio | 0:e1e03118b8fe | 46 | break; |
Sergio | 0:e1e03118b8fe | 47 | default: |
Sergio | 0:e1e03118b8fe | 48 | printf("HUH "); |
Sergio | 0:e1e03118b8fe | 49 | } |
Sergio | 0:e1e03118b8fe | 50 | printfBytes("data",data,len); |
Sergio | 0:e1e03118b8fe | 51 | USBInterruptTransfer(device,endpoint,data,len,AutoEventCallback,userData); |
Sergio | 0:e1e03118b8fe | 52 | } |
Sergio | 0:e1e03118b8fe | 53 | |
Sergio | 0:e1e03118b8fe | 54 | // Establish transfers for interrupt events |
Sergio | 0:e1e03118b8fe | 55 | void AddAutoEvent(int device, InterfaceDescriptor* id, EndpointDescriptor* ed) |
Sergio | 0:e1e03118b8fe | 56 | { |
Sergio | 0:e1e03118b8fe | 57 | if ((ed->bmAttributes & 3) != ENDPOINT_INTERRUPT || !(ed->bEndpointAddress & 0x80)) |
Sergio | 0:e1e03118b8fe | 58 | return; |
Sergio | 0:e1e03118b8fe | 59 | |
Sergio | 0:e1e03118b8fe | 60 | // Make automatic interrupt enpoints for known devices |
Sergio | 0:e1e03118b8fe | 61 | u32 evt = AUTOEVT(id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol); |
Sergio | 0:e1e03118b8fe | 62 | u8* dst = 0; |
Sergio | 0:e1e03118b8fe | 63 | int len; |
Sergio | 0:e1e03118b8fe | 64 | switch (evt) |
Sergio | 0:e1e03118b8fe | 65 | { |
Sergio | 0:e1e03118b8fe | 66 | case AUTO_MOUSE: |
Sergio | 0:e1e03118b8fe | 67 | dst = auto_mouse; |
Sergio | 0:e1e03118b8fe | 68 | len = sizeof(auto_mouse); |
Sergio | 0:e1e03118b8fe | 69 | break; |
Sergio | 0:e1e03118b8fe | 70 | case AUTO_KEYBOARD: |
Sergio | 0:e1e03118b8fe | 71 | dst = auto_keyboard; |
Sergio | 0:e1e03118b8fe | 72 | len = sizeof(auto_keyboard); |
Sergio | 0:e1e03118b8fe | 73 | break; |
Sergio | 0:e1e03118b8fe | 74 | default: |
Sergio | 0:e1e03118b8fe | 75 | printf("Interrupt endpoint %02X %08X\n",ed->bEndpointAddress,evt); |
Sergio | 0:e1e03118b8fe | 76 | break; |
Sergio | 0:e1e03118b8fe | 77 | } |
Sergio | 0:e1e03118b8fe | 78 | if (dst) |
Sergio | 0:e1e03118b8fe | 79 | { |
Sergio | 0:e1e03118b8fe | 80 | printf("Auto Event for %02X %08X\n",ed->bEndpointAddress,evt); |
Sergio | 0:e1e03118b8fe | 81 | USBInterruptTransfer(device,ed->bEndpointAddress,dst,len,AutoEventCallback,(void*)evt); |
Sergio | 0:e1e03118b8fe | 82 | } |
Sergio | 0:e1e03118b8fe | 83 | } |
Sergio | 0:e1e03118b8fe | 84 | |
Sergio | 0:e1e03118b8fe | 85 | void PrintString(int device, int i) |
Sergio | 0:e1e03118b8fe | 86 | { |
Sergio | 0:e1e03118b8fe | 87 | u8 buffer[256]; |
Sergio | 0:e1e03118b8fe | 88 | int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,i,buffer,255); |
Sergio | 0:e1e03118b8fe | 89 | if (le < 0) |
Sergio | 0:e1e03118b8fe | 90 | return; |
Sergio | 0:e1e03118b8fe | 91 | char* dst = (char*)buffer; |
Sergio | 0:e1e03118b8fe | 92 | for (int j = 2; j < le; j += 2) |
Sergio | 0:e1e03118b8fe | 93 | *dst++ = buffer[j]; |
Sergio | 0:e1e03118b8fe | 94 | *dst = 0; |
Sergio | 0:e1e03118b8fe | 95 | printf("%d:%s\n",i,(const char*)buffer); |
Sergio | 0:e1e03118b8fe | 96 | } |
Sergio | 0:e1e03118b8fe | 97 | |
Sergio | 0:e1e03118b8fe | 98 | // Walk descriptors and create endpoints for a given device |
Sergio | 0:e1e03118b8fe | 99 | int StartAutoEvent(int device, int configuration, int interfaceNumber) |
Sergio | 0:e1e03118b8fe | 100 | { |
Sergio | 0:e1e03118b8fe | 101 | u8 buffer[255]; |
Sergio | 0:e1e03118b8fe | 102 | int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255); |
Sergio | 0:e1e03118b8fe | 103 | if (err < 0) |
Sergio | 0:e1e03118b8fe | 104 | return err; |
Sergio | 0:e1e03118b8fe | 105 | |
Sergio | 0:e1e03118b8fe | 106 | int len = buffer[2] | (buffer[3] << 8); |
Sergio | 0:e1e03118b8fe | 107 | u8* d = buffer; |
Sergio | 0:e1e03118b8fe | 108 | u8* end = d + len; |
Sergio | 0:e1e03118b8fe | 109 | while (d < end) |
Sergio | 0:e1e03118b8fe | 110 | { |
Sergio | 0:e1e03118b8fe | 111 | if (d[1] == DESCRIPTOR_TYPE_INTERFACE) |
Sergio | 0:e1e03118b8fe | 112 | { |
Sergio | 0:e1e03118b8fe | 113 | InterfaceDescriptor* id = (InterfaceDescriptor*)d; |
Sergio | 0:e1e03118b8fe | 114 | if (id->bInterfaceNumber == interfaceNumber) |
Sergio | 0:e1e03118b8fe | 115 | { |
Sergio | 0:e1e03118b8fe | 116 | d += d[0]; |
Sergio | 0:e1e03118b8fe | 117 | while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE) |
Sergio | 0:e1e03118b8fe | 118 | { |
Sergio | 0:e1e03118b8fe | 119 | if (d[1] == DESCRIPTOR_TYPE_ENDPOINT) |
Sergio | 0:e1e03118b8fe | 120 | AddAutoEvent(device,id,(EndpointDescriptor*)d); |
Sergio | 0:e1e03118b8fe | 121 | d += d[0]; |
Sergio | 0:e1e03118b8fe | 122 | } |
Sergio | 0:e1e03118b8fe | 123 | } |
Sergio | 0:e1e03118b8fe | 124 | } |
Sergio | 0:e1e03118b8fe | 125 | d += d[0]; |
Sergio | 0:e1e03118b8fe | 126 | } |
Sergio | 0:e1e03118b8fe | 127 | return 0; |
Sergio | 0:e1e03118b8fe | 128 | } |
Sergio | 0:e1e03118b8fe | 129 | |
Sergio | 0:e1e03118b8fe | 130 | // Implemented in main.cpp |
Sergio | 0:e1e03118b8fe | 131 | int OnDiskInsert(int device); |
Sergio | 0:e1e03118b8fe | 132 | |
Sergio | 0:e1e03118b8fe | 133 | void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc) |
Sergio | 0:e1e03118b8fe | 134 | { |
Sergio | 0:e1e03118b8fe | 135 | printf("LoadDevice %d %02X:%02X:%02X\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol); |
Sergio | 0:e1e03118b8fe | 136 | char s[128]; |
Sergio | 0:e1e03118b8fe | 137 | for (int i = 1; i < 3; i++) |
Sergio | 0:e1e03118b8fe | 138 | { |
Sergio | 0:e1e03118b8fe | 139 | if (GetString(device,i,s,sizeof(s)) < 0) |
Sergio | 0:e1e03118b8fe | 140 | break; |
Sergio | 0:e1e03118b8fe | 141 | printf("%d: %s\n",i,s); |
Sergio | 0:e1e03118b8fe | 142 | } |
Sergio | 0:e1e03118b8fe | 143 | |
Sergio | 0:e1e03118b8fe | 144 | switch (interfaceDesc->bInterfaceClass) |
Sergio | 0:e1e03118b8fe | 145 | { |
Sergio | 0:e1e03118b8fe | 146 | case CLASS_MASS_STORAGE: |
Sergio | 0:e1e03118b8fe | 147 | if (interfaceDesc->bInterfaceSubClass == 0x06 && interfaceDesc->bInterfaceProtocol == 0x50) |
Sergio | 0:e1e03118b8fe | 148 | OnDiskInsert(device); // it's SCSI! |
Sergio | 0:e1e03118b8fe | 149 | break; |
Sergio | 0:e1e03118b8fe | 150 | default: |
Sergio | 0:e1e03118b8fe | 151 | StartAutoEvent(device,1,0); |
Sergio | 0:e1e03118b8fe | 152 | break; |
Sergio | 0:e1e03118b8fe | 153 | } |
Sergio | 0:e1e03118b8fe | 154 | } |