]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/USBDevice/USBDevice/USBDevice.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / USBDevice / USBDevice / USBDevice.h
1 /* Copyright (c) 2010-2011 mbed.org, MIT License
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 */
18
19 #ifndef USBDEVICE_H
20 #define USBDEVICE_H
21
22 #include "mbed.h"
23 #include "USBDevice_Types.h"
24 #include "USBHAL.h"
25
26 class USBDevice: public USBHAL
27 {
28 public:
29     USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
30
31     /*
32     * Check if the device is configured
33     *
34     * @returns true if configured, false otherwise
35     */
36     bool configured(void);
37
38     /*
39     * Connect a device
40     *
41     * @param blocking: block if not configured
42     */
43     void connect(bool blocking = true);
44
45     /*
46     * Disconnect a device
47     */
48     void disconnect(void);
49
50     /*
51     * Add an endpoint
52     *
53     * @param endpoint endpoint which will be added
54     * @param maxPacket Maximum size of a packet which can be sent for this endpoint
55     * @returns true if successful, false otherwise
56     */
57     bool addEndpoint(uint8_t endpoint, uint32_t maxPacket);
58
59     /*
60     * Start a reading on a certain endpoint.
61     * You can access the result of the reading by USBDevice_read
62     *
63     * @param endpoint endpoint which will be read
64     * @param maxSize the maximum length that can be read
65     * @return true if successful
66     */
67     bool readStart(uint8_t endpoint, uint32_t maxSize);
68
69     /*
70     * Read a certain endpoint. Before calling this function, USBUSBDevice_readStart
71     * must be called.
72     *
73     * Warning: blocking
74     *
75     * @param endpoint endpoint which will be read
76     * @param buffer buffer will be filled with the data received
77     * @param size the number of bytes read will be stored in *size
78     * @param maxSize the maximum length that can be read
79     * @returns true if successful
80     */
81     bool readEP(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
82
83     /*
84     * Read a certain endpoint.
85     *
86     * Warning: non blocking
87     *
88     * @param endpoint endpoint which will be read
89     * @param buffer buffer will be filled with the data received (if data are available)
90     * @param size the number of bytes read will be stored in *size
91     * @param maxSize the maximum length that can be read
92     * @returns true if successful
93     */
94     bool readEP_NB(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
95
96     /*
97     * Write a certain endpoint.
98     *
99     * Warning: blocking
100     *
101     * @param endpoint endpoint to write
102     * @param buffer data contained in buffer will be write
103     * @param size the number of bytes to write
104     * @param maxSize the maximum length that can be written on this endpoint
105     */
106     bool write(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
107
108
109     /*
110     * Write a certain endpoint.
111     *
112     * Warning: non blocking
113     *
114     * @param endpoint endpoint to write
115     * @param buffer data contained in buffer will be write
116     * @param size the number of bytes to write
117     * @param maxSize the maximum length that can be written on this endpoint
118     */
119     bool writeNB(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
120
121
122     /*
123     * Called by USBDevice layer on bus reset. Warning: Called in ISR context
124     *
125     * May be used to reset state
126     */
127     virtual void USBCallback_busReset(void) {};
128
129     /*
130     * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
131     * This is used to handle extensions to standard requests
132     * and class specific requests
133     *
134     * @returns true if class handles this request
135     */
136     virtual bool USBCallback_request() { return false; };
137
138     /*
139     * Called by USBDevice on Endpoint0 request completion
140     * if the 'notify' flag has been set to true. Warning: Called in ISR context
141     *
142     * In this case it is used to indicate that a HID report has
143     * been received from the host on endpoint 0
144     *
145     * @param buf buffer received on endpoint 0
146     * @param length length of this buffer
147     */
148     virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length) {};
149
150     /*
151     * Called by USBDevice layer. Set configuration of the device.
152     * For instance, you can add all endpoints that you need on this function.
153     *
154     * @param configuration Number of the configuration
155     */
156     virtual bool USBCallback_setConfiguration(uint8_t configuration) { return false; };
157
158     /*
159      * Called by USBDevice layer. Set interface/alternate of the device.
160      *
161      * @param interface Number of the interface to be configured
162      * @param alternate Number of the alternate to be configured
163      * @returns true if class handles this request
164      */
165     virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate) { return false; };
166
167     /*
168     * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
169     *
170     * @returns pointer to the device descriptor
171     */
172     virtual uint8_t * deviceDesc();
173
174     /*
175     * Get configuration descriptor
176     *
177     * @returns pointer to the configuration descriptor
178     */
179     virtual uint8_t * configurationDesc(){return NULL;};
180
181     /*
182     * Get string lang id descriptor
183     *
184     * @return pointer to the string lang id descriptor
185     */
186     virtual uint8_t * stringLangidDesc();
187
188     /*
189     * Get string manufacturer descriptor
190     *
191     * @returns pointer to the string manufacturer descriptor
192     */
193     virtual uint8_t * stringImanufacturerDesc();
194
195     /*
196     * Get string product descriptor
197     *
198     * @returns pointer to the string product descriptor
199     */
200     virtual uint8_t * stringIproductDesc();
201
202     /*
203     * Get string serial descriptor
204     *
205     * @returns pointer to the string serial descriptor
206     */
207     virtual uint8_t * stringIserialDesc();
208
209     /*
210     * Get string configuration descriptor
211     *
212     * @returns pointer to the string configuration descriptor
213     */
214     virtual uint8_t * stringIConfigurationDesc();
215
216     /*
217     * Get string interface descriptor
218     *
219     * @returns pointer to the string interface descriptor
220     */
221     virtual uint8_t * stringIinterfaceDesc();
222
223     /*
224     * Get the length of the report descriptor
225     *
226     * @returns length of the report descriptor
227     */
228     virtual uint16_t reportDescLength() { return 0; };
229
230
231
232 protected:
233     virtual void busReset(void);
234     virtual void EP0setupCallback(void);
235     virtual void EP0out(void);
236     virtual void EP0in(void);
237     virtual void connectStateChanged(unsigned int connected);
238     virtual void suspendStateChanged(unsigned int suspended);
239     uint8_t * findDescriptor(uint8_t descriptorType);
240     CONTROL_TRANSFER * getTransferPtr(void);
241
242     uint16_t VENDOR_ID;
243     uint16_t PRODUCT_ID;
244     uint16_t PRODUCT_RELEASE;
245
246 private:
247     bool addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket);
248     bool requestGetDescriptor(void);
249     bool controlOut(void);
250     bool controlIn(void);
251     bool requestSetAddress(void);
252     bool requestSetConfiguration(void);
253     bool requestSetFeature(void);
254     bool requestClearFeature(void);
255     bool requestGetStatus(void);
256     bool requestSetup(void);
257     bool controlSetup(void);
258     void decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet);
259     bool requestGetConfiguration(void);
260     bool requestGetInterface(void);
261     bool requestSetInterface(void);
262
263     CONTROL_TRANSFER transfer;
264     USB_DEVICE device;
265
266     uint16_t currentInterface;
267     uint8_t currentAlternate;
268 };
269
270
271 #endif