USBasp 2007.07.23.
[pub/USBasp.git] / firmware / usbdrv / usbdrv.h
1 /* Name: usbdrv.h
2 * Project: AVR USB driver
3 * Author: Christian Starkjohann
4 * Creation Date: 2004-12-29
5 * Tabsize: 4
6 * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH
7 * License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt)
8 * This Revision: $Id: usbdrv.h 375 2007-07-07 12:01:52Z cs $
9 */
10
11 #ifndef __usbdrv_h_included__
12 #define __usbdrv_h_included__
13 #include "usbconfig.h"
14 #include "iarcompat.h"
15
16 /*
17 Hardware Prerequisites:
18 =======================
19 USB lines D+ and D- MUST be wired to the same I/O port. D+ must (also) be
20 connected to INT0. D- requires a pullup of 1.5k to +3.5V (and the device
21 must be powered at 3.5V) to identify as low-speed USB device. A pullup of
22 1M SHOULD be connected from D+ to +3.5V to prevent interference when no USB
23 master is connected. We use D+ as interrupt source and not D- because it
24 does not trigger on keep-alive and RESET states.
25
26 As a compile time option, the 1.5k pullup resistor on D- can be made
27 switchable to allow the device to disconnect at will. See the definition of
28 usbDeviceConnect() and usbDeviceDisconnect() further down in this file.
29
30 Please adapt the values in usbconfig.h according to your hardware!
31
32 The device MUST be clocked at exactly 12 MHz or 16 MHz or at 16.5 MHz +/- 1%.
33 See usbconfig-prototype.h for details.
34
35
36 Limitations:
37 ============
38 Robustness with respect to communication errors:
39 The driver assumes error-free communication. It DOES check for errors in
40 the PID, but does NOT check bit stuffing errors, SE0 in middle of a byte,
41 token CRC (5 bit) and data CRC (16 bit). CRC checks can not be performed due
42 to timing constraints: We must start sending a reply within 7 bit times.
43 Bit stuffing and misplaced SE0 would have to be checked in real-time, but CPU
44 performance does not permit that. The driver does not check Data0/Data1
45 toggling, but application software can implement the check.
46
47 Input characteristics:
48 Since no differential receiver circuit is used, electrical interference
49 robustness may suffer. The driver samples only one of the data lines with
50 an ordinary I/O pin's input characteristics. However, since this is only a
51 low speed USB implementation and the specification allows for 8 times the
52 bit rate over the same hardware, we should be on the safe side. Even the spec
53 requires detection of asymmetric states at high bit rate for SE0 detection.
54
55 Number of endpoints:
56 The driver supports up to four endpoints: One control endpoint (endpoint 0),
57 two interrupt-in (or bulk-in) endpoints (endpoint 1 and 3) and one
58 interrupt-out (or bulk-out) endpoint (endpoint 1). Please note that the USB
59 standard forbids bulk endpoints for low speed devices! Most operating systems
60 allow them anyway, but the AVR will spend 90% of the CPU time in the USB
61 interrupt polling for bulk data.
62 By default, only the control endpoint 0 is enabled. To get the other endpoints,
63 define USB_CFG_HAVE_INTRIN_ENDPOINT, USB_CFG_HAVE_INTRIN_ENDPOINT3 and/or
64 USB_CFG_IMPLEMENT_FN_WRITEOUT respectively (see usbconfig-prototype.h for
65 details).
66
67 Maximum data payload:
68 Data payload of control in and out transfers may be up to 254 bytes. In order
69 to accept payload data of out transfers, you need to implement
70 'usbFunctionWrite()'.
71
72 USB Suspend Mode supply current:
73 The USB standard limits power consumption to 500uA when the bus is in suspend
74 mode. This is not a problem for self-powered devices since they don't need
75 bus power anyway. Bus-powered devices can achieve this only by putting the
76 CPU in sleep mode. The driver does not implement suspend handling by itself.
77 However, the application may implement activity monitoring and wakeup from
78 sleep. The host sends regular SE0 states on the bus to keep it active. These
79 SE0 states can be detected by wiring the INT1 pin to D-. It is not necessary
80 to enable the interrupt, checking the interrupt pending flag should suffice.
81 Before entering sleep mode, the application should enable INT1 for a wakeup
82 on the next bus activity.
83
84 Operation without an USB master:
85 The driver behaves neutral without connection to an USB master if D- reads
86 as 1. To avoid spurious interrupts, we recommend a high impedance (e.g. 1M)
87 pullup resistor on D+. If D- becomes statically 0, the driver may block in
88 the interrupt routine.
89
90 Interrupt latency:
91 The application must ensure that the USB interrupt is not disabled for more
92 than 20 cycles. This implies that all interrupt routines must either be
93 declared as "INTERRUPT" instead of "SIGNAL" (see "avr/signal.h") or that they
94 are written in assembler with "sei" as the first instruction.
95
96 Maximum interrupt duration / CPU cycle consumption:
97 The driver handles all USB communication during the interrupt service
98 routine. The routine will not return before an entire USB message is received
99 and the reply is sent. This may be up to ca. 1200 cycles @ 12 MHz (= 100us) if
100 the host conforms to the standard. The driver will consume CPU cycles for all
101 USB messages, even if they address another (low-speed) device on the same bus.
102
103 */
104
105 /* ------------------------------------------------------------------------- */
106 /* --------------------------- Module Interface ---------------------------- */
107 /* ------------------------------------------------------------------------- */
108
109 #define USBDRV_VERSION 20070707
110 /* This define uniquely identifies a driver version. It is a decimal number
111 * constructed from the driver's release date in the form YYYYMMDD. If the
112 * driver's behavior or interface changes, you can use this constant to
113 * distinguish versions. If it is not defined, the driver's release date is
114 * older than 2006-01-25.
115 */
116
117
118 #ifndef USB_PUBLIC
119 #define USB_PUBLIC
120 #endif
121 /* USB_PUBLIC is used as declaration attribute for all functions exported by
122 * the USB driver. The default is no attribute (see above). You may define it
123 * to static either in usbconfig.h or from the command line if you include
124 * usbdrv.c instead of linking against it. Including the C module of the driver
125 * directly in your code saves a couple of bytes in flash memory.
126 */
127
128 #ifndef __ASSEMBLER__
129 #ifndef uchar
130 #define uchar unsigned char
131 #endif
132 #ifndef schar
133 #define schar signed char
134 #endif
135 /* shortcuts for well defined 8 bit integer types */
136
137 struct usbRequest; /* forward declaration */
138
139 USB_PUBLIC void usbInit(void);
140 /* This function must be called before interrupts are enabled and the main
141 * loop is entered.
142 */
143 USB_PUBLIC void usbPoll(void);
144 /* This function must be called at regular intervals from the main loop.
145 * Maximum delay between calls is somewhat less than 50ms (USB timeout for
146 * accepting a Setup message). Otherwise the device will not be recognized.
147 * Please note that debug outputs through the UART take ~ 0.5ms per byte
148 * at 19200 bps.
149 */
150 extern uchar *usbMsgPtr;
151 /* This variable may be used to pass transmit data to the driver from the
152 * implementation of usbFunctionWrite(). It is also used internally by the
153 * driver for standard control requests.
154 */
155 USB_PUBLIC uchar usbFunctionSetup(uchar data[8]);
156 /* This function is called when the driver receives a SETUP transaction from
157 * the host which is not answered by the driver itself (in practice: class and
158 * vendor requests). All control transfers start with a SETUP transaction where
159 * the host communicates the parameters of the following (optional) data
160 * transfer. The SETUP data is available in the 'data' parameter which can
161 * (and should) be casted to 'usbRequest_t *' for a more user-friendly access
162 * to parameters.
163 *
164 * If the SETUP indicates a control-in transfer, you should provide the
165 * requested data to the driver. There are two ways to transfer this data:
166 * (1) Set the global pointer 'usbMsgPtr' to the base of the static RAM data
167 * block and return the length of the data in 'usbFunctionSetup()'. The driver
168 * will handle the rest. Or (2) return 0xff in 'usbFunctionSetup()'. The driver
169 * will then call 'usbFunctionRead()' when data is needed. See the
170 * documentation for usbFunctionRead() for details.
171 *
172 * If the SETUP indicates a control-out transfer, the only way to receive the
173 * data from the host is through the 'usbFunctionWrite()' call. If you
174 * implement this function, you must return 0xff in 'usbFunctionSetup()' to
175 * indicate that 'usbFunctionWrite()' should be used. See the documentation of
176 * this function for more information. If you just want to ignore the data sent
177 * by the host, return 0 in 'usbFunctionSetup()'.
178 *
179 * Note that calls to the functions usbFunctionRead() and usbFunctionWrite()
180 * are only done if enabled by the configuration in usbconfig.h.
181 */
182 USB_PUBLIC uchar usbFunctionDescriptor(struct usbRequest *rq);
183 /* You need to implement this function ONLY if you provide USB descriptors at
184 * runtime (which is an expert feature). It is very similar to
185 * usbFunctionSetup() above, but it is called only to request USB descriptor
186 * data. See the documentation of usbFunctionSetup() above for more info.
187 */
188 #if USB_CFG_HAVE_INTRIN_ENDPOINT
189 USB_PUBLIC void usbSetInterrupt(uchar *data, uchar len);
190 /* This function sets the message which will be sent during the next interrupt
191 * IN transfer. The message is copied to an internal buffer and must not exceed
192 * a length of 8 bytes. The message may be 0 bytes long just to indicate the
193 * interrupt status to the host.
194 * If you need to transfer more bytes, use a control read after the interrupt.
195 */
196 extern volatile uchar usbTxLen1;
197 #define usbInterruptIsReady() (usbTxLen1 & 0x10)
198 /* This macro indicates whether the last interrupt message has already been
199 * sent. If you set a new interrupt message before the old was sent, the
200 * message already buffered will be lost.
201 */
202 #if USB_CFG_HAVE_INTRIN_ENDPOINT3
203 USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len);
204 extern volatile uchar usbTxLen3;
205 #define usbInterruptIsReady3() (usbTxLen3 & 0x10)
206 /* Same as above for endpoint 3 */
207 #endif
208 #endif /* USB_CFG_HAVE_INTRIN_ENDPOINT */
209 #if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* simplified interface for backward compatibility */
210 #define usbHidReportDescriptor usbDescriptorHidReport
211 /* should be declared as: PROGMEM char usbHidReportDescriptor[]; */
212 /* If you implement an HID device, you need to provide a report descriptor.
213 * The HID report descriptor syntax is a bit complex. If you understand how
214 * report descriptors are constructed, we recommend that you use the HID
215 * Descriptor Tool from usb.org, see http://www.usb.org/developers/hidpage/.
216 * Otherwise you should probably start with a working example.
217 */
218 #endif /* USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH */
219 #if USB_CFG_IMPLEMENT_FN_WRITE
220 USB_PUBLIC uchar usbFunctionWrite(uchar *data, uchar len);
221 /* This function is called by the driver to provide a control transfer's
222 * payload data (control-out). It is called in chunks of up to 8 bytes. The
223 * total count provided in the current control transfer can be obtained from
224 * the 'length' property in the setup data. If an error occurred during
225 * processing, return 0xff (== -1). The driver will answer the entire transfer
226 * with a STALL token in this case. If you have received the entire payload
227 * successfully, return 1. If you expect more data, return 0. If you don't
228 * know whether the host will send more data (you should know, the total is
229 * provided in the usbFunctionSetup() call!), return 1.
230 * NOTE: If you return 0xff for STALL, 'usbFunctionWrite()' may still be called
231 * for the remaining data. You must continue to return 0xff for STALL in these
232 * calls.
233 * In order to get usbFunctionWrite() called, define USB_CFG_IMPLEMENT_FN_WRITE
234 * to 1 in usbconfig.h and return 0xff in usbFunctionSetup()..
235 */
236 #endif /* USB_CFG_IMPLEMENT_FN_WRITE */
237 #if USB_CFG_IMPLEMENT_FN_READ
238 USB_PUBLIC uchar usbFunctionRead(uchar *data, uchar len);
239 /* This function is called by the driver to ask the application for a control
240 * transfer's payload data (control-in). It is called in chunks of up to 8
241 * bytes each. You should copy the data to the location given by 'data' and
242 * return the actual number of bytes copied. If you return less than requested,
243 * the control-in transfer is terminated. If you return 0xff, the driver aborts
244 * the transfer with a STALL token.
245 * In order to get usbFunctionRead() called, define USB_CFG_IMPLEMENT_FN_READ
246 * to 1 in usbconfig.h and return 0xff in usbFunctionSetup()..
247 */
248 #endif /* USB_CFG_IMPLEMENT_FN_READ */
249 #if USB_CFG_IMPLEMENT_FN_WRITEOUT
250 USB_PUBLIC void usbFunctionWriteOut(uchar *data, uchar len);
251 /* This function is called by the driver when data on interrupt-out or bulk-
252 * out endpoint 1 is received. You must define USB_CFG_IMPLEMENT_FN_WRITEOUT
253 * to 1 in usbconfig.h to get this function called.
254 */
255 #endif /* USB_CFG_IMPLEMENT_FN_WRITEOUT */
256 #ifdef USB_CFG_PULLUP_IOPORTNAME
257 #define usbDeviceConnect() ((USB_PULLUP_DDR |= (1<<USB_CFG_PULLUP_BIT)), \
258 (USB_PULLUP_OUT |= (1<<USB_CFG_PULLUP_BIT)))
259 /* This macro (intended to look like a function) connects the device to the
260 * USB bus. It is only available if you have defined the constants
261 * USB_CFG_PULLUP_IOPORT and USB_CFG_PULLUP_BIT in usbconfig.h.
262 */
263 #define usbDeviceDisconnect() ((USB_PULLUP_DDR &= ~(1<<USB_CFG_PULLUP_BIT)), \
264 (USB_PULLUP_OUT &= ~(1<<USB_CFG_PULLUP_BIT)))
265 /* This macro (intended to look like a function) disconnects the device from
266 * the USB bus. It is only available if you have defined the constants
267 * USB_CFG_PULLUP_IOPORT and USB_CFG_PULLUP_BIT in usbconfig.h.
268 */
269 #endif /* USB_CFG_PULLUP_IOPORT */
270 extern unsigned usbCrc16(unsigned data, uchar len);
271 #define usbCrc16(data, len) usbCrc16((unsigned)(data), len)
272 /* This function calculates the binary complement of the data CRC used in
273 * USB data packets. The value is used to build raw transmit packets.
274 * You may want to use this function for data checksums or to verify received
275 * data. We enforce 16 bit calling conventions for compatibility with IAR's
276 * tiny memory model.
277 */
278 extern unsigned usbCrc16Append(unsigned data, uchar len);
279 #define usbCrc16Append(data, len) usbCrc16Append((unsigned)(data), len)
280 /* This function is equivalent to usbCrc16() above, except that it appends
281 * the 2 bytes CRC (lowbyte first) in the 'data' buffer after reading 'len'
282 * bytes.
283 */
284 extern uchar usbConfiguration;
285 /* This value contains the current configuration set by the host. The driver
286 * allows setting and querying of this variable with the USB SET_CONFIGURATION
287 * and GET_CONFIGURATION requests, but does not use it otherwise.
288 * You may want to reflect the "configured" status with a LED on the device or
289 * switch on high power parts of the circuit only if the device is configured.
290 */
291 #define USB_STRING_DESCRIPTOR_HEADER(stringLength) ((2*(stringLength)+2) | (3<<8))
292 /* This macro builds a descriptor header for a string descriptor given the
293 * string's length. See usbdrv.c for an example how to use it.
294 */
295 #if USB_CFG_HAVE_FLOWCONTROL
296 extern volatile schar usbRxLen;
297 #define usbDisableAllRequests() usbRxLen = -1
298 /* Must be called from usbFunctionWrite(). This macro disables all data input
299 * from the USB interface. Requests from the host are answered with a NAK
300 * while they are disabled.
301 */
302 #define usbEnableAllRequests() usbRxLen = 0
303 /* May only be called if requests are disabled. This macro enables input from
304 * the USB interface after it has been disabled with usbDisableAllRequests().
305 */
306 #define usbAllRequestsAreDisabled() (usbRxLen < 0)
307 /* Use this macro to find out whether requests are disabled. It may be needed
308 * to ensure that usbEnableAllRequests() is never called when requests are
309 * enabled.
310 */
311 #endif
312
313 #define USB_SET_DATATOKEN1(token) usbTxBuf1[0] = token
314 #define USB_SET_DATATOKEN3(token) usbTxBuf3[0] = token
315 /* These two macros can be used by application software to reset data toggling
316 * for interrupt-in endpoints 1 and 3.
317 */
318
319 #endif /* __ASSEMBLER__ */
320
321
322 /* ------------------------------------------------------------------------- */
323 /* ----------------- Definitions for Descriptor Properties ----------------- */
324 /* ------------------------------------------------------------------------- */
325 /* This is advanced stuff. See usbconfig-prototype.h for more information
326 * about the various methods to define USB descriptors. If you do nothing,
327 * the default descriptors will be used.
328 */
329 #define USB_PROP_IS_DYNAMIC (1 << 8)
330 /* If this property is set for a descriptor, usbFunctionDescriptor() will be
331 * used to obtain the particular descriptor.
332 */
333 #define USB_PROP_IS_RAM (1 << 9)
334 /* If this property is set for a descriptor, the data is read from RAM
335 * memory instead of Flash. The property is used for all methods to provide
336 * external descriptors.
337 */
338 #define USB_PROP_LENGTH(len) ((len) & 0xff)
339 /* If a static external descriptor is used, this is the total length of the
340 * descriptor in bytes.
341 */
342
343 /* all descriptors which may have properties: */
344 #ifndef USB_CFG_DESCR_PROPS_DEVICE
345 #define USB_CFG_DESCR_PROPS_DEVICE 0
346 #endif
347 #ifndef USB_CFG_DESCR_PROPS_CONFIGURATION
348 #define USB_CFG_DESCR_PROPS_CONFIGURATION 0
349 #endif
350 #ifndef USB_CFG_DESCR_PROPS_STRINGS
351 #define USB_CFG_DESCR_PROPS_STRINGS 0
352 #endif
353 #ifndef USB_CFG_DESCR_PROPS_STRING_0
354 #define USB_CFG_DESCR_PROPS_STRING_0 0
355 #endif
356 #ifndef USB_CFG_DESCR_PROPS_STRING_VENDOR
357 #define USB_CFG_DESCR_PROPS_STRING_VENDOR 0
358 #endif
359 #ifndef USB_CFG_DESCR_PROPS_STRING_PRODUCT
360 #define USB_CFG_DESCR_PROPS_STRING_PRODUCT 0
361 #endif
362 #ifndef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
363 #define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER 0
364 #endif
365 #ifndef USB_CFG_DESCR_PROPS_HID
366 #define USB_CFG_DESCR_PROPS_HID 0
367 #endif
368 #if !(USB_CFG_DESCR_PROPS_HID_REPORT)
369 # undef USB_CFG_DESCR_PROPS_HID_REPORT
370 # if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* do some backward compatibility tricks */
371 # define USB_CFG_DESCR_PROPS_HID_REPORT USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH
372 # else
373 # define USB_CFG_DESCR_PROPS_HID_REPORT 0
374 # endif
375 #endif
376 #ifndef USB_CFG_DESCR_PROPS_UNKNOWN
377 #define USB_CFG_DESCR_PROPS_UNKNOWN 0
378 #endif
379
380 /* ------------------ forward declaration of descriptors ------------------- */
381 /* If you use external static descriptors, they must be stored in global
382 * arrays as declared below:
383 */
384 #ifndef __ASSEMBLER__
385 extern
386 #if !(USB_CFG_DESCR_PROPS_DEVICE & USB_PROP_IS_RAM)
387 PROGMEM
388 #endif
389 char usbDescriptorDevice[];
390
391 extern
392 #if !(USB_CFG_DESCR_PROPS_CONFIGURATION & USB_PROP_IS_RAM)
393 PROGMEM
394 #endif
395 char usbDescriptorConfiguration[];
396
397 extern
398 #if !(USB_CFG_DESCR_PROPS_HID_REPORT & USB_PROP_IS_RAM)
399 PROGMEM
400 #endif
401 char usbDescriptorHidReport[];
402
403 extern
404 #if !(USB_CFG_DESCR_PROPS_STRING_0 & USB_PROP_IS_RAM)
405 PROGMEM
406 #endif
407 char usbDescriptorString0[];
408
409 extern
410 #if !(USB_CFG_DESCR_PROPS_STRING_VENDOR & USB_PROP_IS_RAM)
411 PROGMEM
412 #endif
413 int usbDescriptorStringVendor[];
414
415 extern
416 #if !(USB_CFG_DESCR_PROPS_STRING_PRODUCT & USB_PROP_IS_RAM)
417 PROGMEM
418 #endif
419 int usbDescriptorStringDevice[];
420
421 extern
422 #if !(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER & USB_PROP_IS_RAM)
423 PROGMEM
424 #endif
425 int usbDescriptorStringSerialNumber[];
426
427 #endif /* __ASSEMBLER__ */
428
429 /* ------------------------------------------------------------------------- */
430 /* ------------------------ General Purpose Macros ------------------------- */
431 /* ------------------------------------------------------------------------- */
432
433 #define USB_CONCAT(a, b) a ## b
434 #define USB_CONCAT_EXPANDED(a, b) USB_CONCAT(a, b)
435
436 #define USB_OUTPORT(name) USB_CONCAT(PORT, name)
437 #define USB_INPORT(name) USB_CONCAT(PIN, name)
438 #define USB_DDRPORT(name) USB_CONCAT(DDR, name)
439 /* The double-define trick above lets us concatenate strings which are
440 * defined by macros.
441 */
442
443 /* ------------------------------------------------------------------------- */
444 /* ------------------------- Constant definitions -------------------------- */
445 /* ------------------------------------------------------------------------- */
446
447 #if !defined __ASSEMBLER__ && (!defined USB_CFG_VENDOR_ID || !defined USB_CFG_DEVICE_ID)
448 #warning "You should define USB_CFG_VENDOR_ID and USB_CFG_DEVICE_ID in usbconfig.h"
449 /* If the user has not defined IDs, we default to obdev's free IDs.
450 * See USBID-License.txt for details.
451 */
452 #endif
453
454 /* make sure we have a VID and PID defined, byte order is lowbyte, highbyte */
455 #ifndef USB_CFG_VENDOR_ID
456 # define USB_CFG_VENDOR_ID 0xc0, 0x16 /* 5824 in dec, stands for VOTI */
457 #endif
458
459 #ifndef USB_CFG_DEVICE_ID
460 # if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH
461 # define USB_CFG_DEVICE_ID 0xdf, 0x05 /* 1503 in dec, shared PID for HIDs */
462 # elif USB_CFG_INTERFACE_CLASS == 2
463 # define USB_CFG_DEVICE_ID 0xe1, 0x05 /* 1505 in dec, shared PID for CDC Modems */
464 # else
465 # define USB_CFG_DEVICE_ID 0xdc, 0x05 /* 1500 in dec, obdev's free PID */
466 # endif
467 #endif
468
469 /* Derive Output, Input and DataDirection ports from port names */
470 #ifndef USB_CFG_IOPORTNAME
471 #error "You must define USB_CFG_IOPORTNAME in usbconfig.h, see usbconfig-prototype.h"
472 #endif
473
474 #define USBOUT USB_OUTPORT(USB_CFG_IOPORTNAME)
475 #define USB_PULLUP_OUT USB_OUTPORT(USB_CFG_PULLUP_IOPORTNAME)
476 #define USBIN USB_INPORT(USB_CFG_IOPORTNAME)
477 #define USBDDR USB_DDRPORT(USB_CFG_IOPORTNAME)
478 #define USB_PULLUP_DDR USB_DDRPORT(USB_CFG_PULLUP_IOPORTNAME)
479
480 #define USBMINUS USB_CFG_DMINUS_BIT
481 #define USBPLUS USB_CFG_DPLUS_BIT
482 #define USBIDLE (1<<USB_CFG_DMINUS_BIT) /* value representing J state */
483 #define USBMASK ((1<<USB_CFG_DPLUS_BIT) | (1<<USB_CFG_DMINUS_BIT)) /* mask for USB I/O bits */
484
485 /* defines for backward compatibility with older driver versions: */
486 #define USB_CFG_IOPORT USB_OUTPORT(USB_CFG_IOPORTNAME)
487 #ifdef USB_CFG_PULLUP_IOPORTNAME
488 #define USB_CFG_PULLUP_IOPORT USB_OUTPORT(USB_CFG_PULLUP_IOPORTNAME)
489 #endif
490
491
492 #define USB_BUFSIZE 11 /* PID, 8 bytes data, 2 bytes CRC */
493
494 /* ----- Try to find registers and bits responsible for ext interrupt 0 ----- */
495
496 #ifndef USB_INTR_CFG /* allow user to override our default */
497 # if defined EICRA
498 # define USB_INTR_CFG EICRA
499 # else
500 # define USB_INTR_CFG MCUCR
501 # endif
502 #endif
503 #ifndef USB_INTR_CFG_SET /* allow user to override our default */
504 # define USB_INTR_CFG_SET ((1 << ISC00) | (1 << ISC01)) /* cfg for rising edge */
505 #endif
506 #ifndef USB_INTR_CFG_CLR /* allow user to override our default */
507 # define USB_INTR_CFG_CLR 0 /* no bits to clear */
508 #endif
509
510 #ifndef USB_INTR_ENABLE /* allow user to override our default */
511 # if defined GIMSK
512 # define USB_INTR_ENABLE GIMSK
513 # elif defined EIMSK
514 # define USB_INTR_ENABLE EIMSK
515 # else
516 # define USB_INTR_ENABLE GICR
517 # endif
518 #endif
519 #ifndef USB_INTR_ENABLE_BIT /* allow user to override our default */
520 # define USB_INTR_ENABLE_BIT INT0
521 #endif
522
523 #ifndef USB_INTR_PENDING /* allow user to override our default */
524 # if defined EIFR
525 # define USB_INTR_PENDING EIFR
526 # else
527 # define USB_INTR_PENDING GIFR
528 # endif
529 #endif
530 #ifndef USB_INTR_PENDING_BIT /* allow user to override our default */
531 # define USB_INTR_PENDING_BIT INTF0
532 #endif
533
534 /*
535 The defines above don't work for the following chips
536 at90c8534: no ISC0?, no PORTB, can't find a data sheet
537 at86rf401: no PORTB, no MCUCR etc, low clock rate
538 atmega103: no ISC0? (maybe omission in header, can't find data sheet)
539 atmega603: not defined in avr-libc
540 at43usb320, at43usb355, at76c711: have USB anyway
541 at94k: is different...
542
543 at90s1200, attiny11, attiny12, attiny15, attiny28: these have no RAM
544 */
545
546 /* ------------------------------------------------------------------------- */
547 /* ----------------- USB Specification Constants and Types ----------------- */
548 /* ------------------------------------------------------------------------- */
549
550 /* USB Token values */
551 #define USBPID_SETUP 0x2d
552 #define USBPID_OUT 0xe1
553 #define USBPID_IN 0x69
554 #define USBPID_DATA0 0xc3
555 #define USBPID_DATA1 0x4b
556
557 #define USBPID_ACK 0xd2
558 #define USBPID_NAK 0x5a
559 #define USBPID_STALL 0x1e
560
561 #ifndef __ASSEMBLER__
562
563 extern uchar usbTxBuf1[USB_BUFSIZE], usbTxBuf3[USB_BUFSIZE];
564
565 typedef union usbWord{
566 unsigned word;
567 uchar bytes[2];
568 }usbWord_t;
569
570 typedef struct usbRequest{
571 uchar bmRequestType;
572 uchar bRequest;
573 usbWord_t wValue;
574 usbWord_t wIndex;
575 usbWord_t wLength;
576 }usbRequest_t;
577 /* This structure matches the 8 byte setup request */
578 #endif
579
580 /* bmRequestType field in USB setup:
581 * d t t r r r r r, where
582 * d ..... direction: 0=host->device, 1=device->host
583 * t ..... type: 0=standard, 1=class, 2=vendor, 3=reserved
584 * r ..... recipient: 0=device, 1=interface, 2=endpoint, 3=other
585 */
586
587 /* USB setup recipient values */
588 #define USBRQ_RCPT_MASK 0x1f
589 #define USBRQ_RCPT_DEVICE 0
590 #define USBRQ_RCPT_INTERFACE 1
591 #define USBRQ_RCPT_ENDPOINT 2
592
593 /* USB request type values */
594 #define USBRQ_TYPE_MASK 0x60
595 #define USBRQ_TYPE_STANDARD (0<<5)
596 #define USBRQ_TYPE_CLASS (1<<5)
597 #define USBRQ_TYPE_VENDOR (2<<5)
598
599 /* USB direction values: */
600 #define USBRQ_DIR_MASK 0x80
601 #define USBRQ_DIR_HOST_TO_DEVICE (0<<7)
602 #define USBRQ_DIR_DEVICE_TO_HOST (1<<7)
603
604 /* USB Standard Requests */
605 #define USBRQ_GET_STATUS 0
606 #define USBRQ_CLEAR_FEATURE 1
607 #define USBRQ_SET_FEATURE 3
608 #define USBRQ_SET_ADDRESS 5
609 #define USBRQ_GET_DESCRIPTOR 6
610 #define USBRQ_SET_DESCRIPTOR 7
611 #define USBRQ_GET_CONFIGURATION 8
612 #define USBRQ_SET_CONFIGURATION 9
613 #define USBRQ_GET_INTERFACE 10
614 #define USBRQ_SET_INTERFACE 11
615 #define USBRQ_SYNCH_FRAME 12
616
617 /* USB descriptor constants */
618 #define USBDESCR_DEVICE 1
619 #define USBDESCR_CONFIG 2
620 #define USBDESCR_STRING 3
621 #define USBDESCR_INTERFACE 4
622 #define USBDESCR_ENDPOINT 5
623 #define USBDESCR_HID 0x21
624 #define USBDESCR_HID_REPORT 0x22
625 #define USBDESCR_HID_PHYS 0x23
626
627 #define USBATTR_BUSPOWER 0x80
628 #define USBATTR_SELFPOWER 0x40
629 #define USBATTR_REMOTEWAKE 0x20
630
631 /* USB HID Requests */
632 #define USBRQ_HID_GET_REPORT 0x01
633 #define USBRQ_HID_GET_IDLE 0x02
634 #define USBRQ_HID_GET_PROTOCOL 0x03
635 #define USBRQ_HID_SET_REPORT 0x09
636 #define USBRQ_HID_SET_IDLE 0x0a
637 #define USBRQ_HID_SET_PROTOCOL 0x0b
638
639 /* ------------------------------------------------------------------------- */
640
641 #endif /* __usbdrv_h_included__ */