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