USBasp 2006.09.16.
[pub/USBasp.git] / firmware / usbdrv / usbdrv.c
1 /* Name: usbdrv.c
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.c 222 2006-07-17 15:07:34Z cs $
9 */
10
11 #include "iarcompat.h"
12 #ifndef __IAR_SYSTEMS_ICC__
13 # include <avr/io.h>
14 # include <avr/pgmspace.h>
15 #endif
16 #include "usbdrv.h"
17 #include "oddebug.h"
18
19 /*
20 General Description:
21 This module implements the C-part of the USB driver. See usbdrv.h for a
22 documentation of the entire driver.
23 */
24
25 #ifndef IAR_SECTION
26 #define IAR_SECTION(arg)
27 #define __no_init
28 #endif
29 /* The macro IAR_SECTION is a hack to allow IAR-cc compatibility. On gcc, it
30 * is defined to nothing. __no_init is required on IAR.
31 */
32
33 /* ------------------------------------------------------------------------- */
34
35 /* raw USB registers / interface to assembler code: */
36 /* usbRxBuf MUST be in 1 byte addressable range (because usbInputBuf is only 1 byte) */
37 __no_init uchar usbRxBuf[2][USB_BUFSIZE] __attribute__ ((section (USB_BUFFER_SECTION))) IAR_SECTION(USB_BUFFER_SECTION);/* raw RX buffer: PID, 8 bytes data, 2 bytes CRC */
38 uchar usbDeviceAddr; /* assigned during enumeration, defaults to 0 */
39 uchar usbNewDeviceAddr; /* device ID which should be set after status phase */
40 uchar usbConfiguration; /* currently selected configuration. Administered by driver, but not used */
41 uchar usbInputBuf; /* ptr to raw buffer used for receiving */
42 uchar usbAppBuf; /* ptr to raw buffer passed to app for processing */
43 volatile schar usbRxLen; /* = 0; number of bytes in usbAppBuf; 0 means free */
44 uchar usbCurrentTok; /* last token received, if more than 1 rx endpoint: MSb=endpoint */
45 uchar usbRxToken; /* token for data we received; if more than 1 rx endpoint: MSb=endpoint */
46 uchar usbMsgLen = 0xff; /* remaining number of bytes, no msg to send if -1 (see usbMsgPtr) */
47 volatile uchar usbTxLen = USBPID_NAK; /* number of bytes to transmit with next IN token or handshake token */
48 uchar usbTxBuf[USB_BUFSIZE];/* data to transmit with next IN, free if usbTxLen contains handshake token */
49 #if USB_CFG_HAVE_INTRIN_ENDPOINT
50 volatile uchar usbTxLen1 = USBPID_NAK; /* TX count for endpoint 1 */
51 uchar usbTxBuf1[USB_BUFSIZE]; /* TX data for endpoint 1 */
52 #if USB_CFG_HAVE_INTRIN_ENDPOINT3
53 volatile uchar usbTxLen3 = USBPID_NAK; /* TX count for endpoint 1 */
54 uchar usbTxBuf3[USB_BUFSIZE]; /* TX data for endpoint 1 */
55 #endif
56 #endif
57
58 /* USB status registers / not shared with asm code */
59 uchar *usbMsgPtr; /* data to transmit next -- ROM or RAM address */
60 static uchar usbMsgFlags; /* flag values see below */
61 static uchar usbIsReset; /* = 0; USB bus is in reset phase */
62
63 #define USB_FLG_TX_PACKET (1<<0)
64 /* Leave free 6 bits after TX_PACKET. This way we can increment usbMsgFlags to toggle TX_PACKET */
65 #define USB_FLG_MSGPTR_IS_ROM (1<<6)
66 #define USB_FLG_USE_DEFAULT_RW (1<<7)
67
68 /*
69 optimizing hints:
70 - do not post/pre inc/dec integer values in operations
71 - assign value of PRG_RDB() to register variables and don't use side effects in arg
72 - use narrow scope for variables which should be in X/Y/Z register
73 - assign char sized expressions to variables to force 8 bit arithmetics
74 */
75
76 /* ------------------------------------------------------------------------- */
77
78 #if USB_CFG_DESCR_PROPS_STRINGS == 0
79
80 #if USB_CFG_DESCR_PROPS_STRING_0 == 0
81 #undef USB_CFG_DESCR_PROPS_STRING_0
82 #define USB_CFG_DESCR_PROPS_STRING_0 sizeof(usbDescriptorString0)
83 PROGMEM char usbDescriptorString0[] = { /* language descriptor */
84 4, /* sizeof(usbDescriptorString0): length of descriptor in bytes */
85 3, /* descriptor type */
86 0x09, 0x04, /* language index (0x0409 = US-English) */
87 };
88 #endif
89
90 #if USB_CFG_DESCR_PROPS_STRING_VENDOR == 0 && USB_CFG_VENDOR_NAME_LEN
91 #undef USB_CFG_DESCR_PROPS_STRING_VENDOR
92 #define USB_CFG_DESCR_PROPS_STRING_VENDOR sizeof(usbDescriptorStringVendor)
93 PROGMEM int usbDescriptorStringVendor[] = {
94 USB_STRING_DESCRIPTOR_HEADER(USB_CFG_VENDOR_NAME_LEN),
95 USB_CFG_VENDOR_NAME
96 };
97 #endif
98
99 #if USB_CFG_DESCR_PROPS_STRING_DEVICE == 0 && USB_CFG_DEVICE_NAME_LEN
100 #undef USB_CFG_DESCR_PROPS_STRING_DEVICE
101 #define USB_CFG_DESCR_PROPS_STRING_DEVICE sizeof(usbDescriptorStringDevice)
102 PROGMEM int usbDescriptorStringDevice[] = {
103 USB_STRING_DESCRIPTOR_HEADER(USB_CFG_DEVICE_NAME_LEN),
104 USB_CFG_DEVICE_NAME
105 };
106 #endif
107
108 #if USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER == 0 && USB_CFG_SERIAL_NUMBER_LEN
109 #undef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
110 #define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER sizeof(usbDescriptorStringSerialNumber)
111 PROGMEM int usbDescriptorStringSerialNumber[] = {
112 USB_STRING_DESCRIPTOR_HEADER(USB_CFG_SERIAL_NUMBER_LEN),
113 USB_CFG_SERIAL_NUMBER
114 };
115 #endif
116
117 #endif /* USB_CFG_DESCR_PROPS_STRINGS == 0 */
118
119 #if USB_CFG_DESCR_PROPS_DEVICE == 0
120 #undef USB_CFG_DESCR_PROPS_DEVICE
121 #define USB_CFG_DESCR_PROPS_DEVICE sizeof(usbDescriptorDevice)
122 PROGMEM char usbDescriptorDevice[] = { /* USB device descriptor */
123 18, /* sizeof(usbDescriptorDevice): length of descriptor in bytes */
124 USBDESCR_DEVICE, /* descriptor type */
125 0x01, 0x01, /* USB version supported */
126 USB_CFG_DEVICE_CLASS,
127 USB_CFG_DEVICE_SUBCLASS,
128 0, /* protocol */
129 8, /* max packet size */
130 USB_CFG_VENDOR_ID, /* 2 bytes */
131 USB_CFG_DEVICE_ID, /* 2 bytes */
132 USB_CFG_DEVICE_VERSION, /* 2 bytes */
133 USB_CFG_DESCR_PROPS_STRING_VENDOR != 0 ? 1 : 0, /* manufacturer string index */
134 USB_CFG_DESCR_PROPS_STRING_DEVICE != 0 ? 2 : 0, /* product string index */
135 USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER != 0 ? 3 : 0, /* serial number string index */
136 1, /* number of configurations */
137 };
138 #endif
139
140 #if USB_CFG_DESCR_PROPS_HID_REPORT != 0 && USB_CFG_DESCR_PROPS_HID == 0
141 #undef USB_CFG_DESCR_PROPS_HID
142 #define USB_CFG_DESCR_PROPS_HID 9 /* length of HID descriptor in config descriptor below */
143 #endif
144
145 #if USB_CFG_DESCR_PROPS_CONFIGURATION == 0
146 #undef USB_CFG_DESCR_PROPS_CONFIGURATION
147 #define USB_CFG_DESCR_PROPS_CONFIGURATION sizeof(usbDescriptorConfiguration)
148 PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
149 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
150 USBDESCR_CONFIG, /* descriptor type */
151 18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + (USB_CFG_DESCR_PROPS_HID & 0xff), 0,
152 /* total length of data returned (including inlined descriptors) */
153 1, /* number of interfaces in this configuration */
154 1, /* index of this configuration */
155 0, /* configuration name string index */
156 #if USB_CFG_IS_SELF_POWERED
157 USBATTR_SELFPOWER, /* attributes */
158 #else
159 USBATTR_BUSPOWER, /* attributes */
160 #endif
161 USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
162 /* interface descriptor follows inline: */
163 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
164 USBDESCR_INTERFACE, /* descriptor type */
165 0, /* index of this interface */
166 0, /* alternate setting for this interface */
167 USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
168 USB_CFG_INTERFACE_CLASS,
169 USB_CFG_INTERFACE_SUBCLASS,
170 USB_CFG_INTERFACE_PROTOCOL,
171 0, /* string index for interface */
172 #if (USB_CFG_DESCR_PROPS_HID & 0xff) /* HID descriptor */
173 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
174 USBDESCR_HID, /* descriptor type: HID */
175 0x01, 0x01, /* BCD representation of HID version */
176 0x00, /* target country code */
177 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
178 0x22, /* descriptor type: report */
179 USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH, 0, /* total length of report descriptor */
180 #endif
181 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
182 7, /* sizeof(usbDescrEndpoint) */
183 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
184 0x81, /* IN endpoint number 1 */
185 0x03, /* attrib: Interrupt endpoint */
186 8, 0, /* maximum packet size */
187 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
188 #endif
189 };
190 #endif
191
192 /* We don't use prog_int or prog_int16_t for compatibility with various libc
193 * versions. Here's an other compatibility hack:
194 */
195 #ifndef PRG_RDB
196 #define PRG_RDB(addr) pgm_read_byte(addr)
197 #endif
198
199 typedef union{
200 unsigned word;
201 uchar *ptr;
202 uchar bytes[2];
203 }converter_t;
204 /* We use this union to do type conversions. This is better optimized than
205 * type casts in gcc 3.4.3 and much better than using bit shifts to build
206 * ints from chars. Byte ordering is not a problem on an 8 bit platform.
207 */
208
209 /* ------------------------------------------------------------------------- */
210
211 #if USB_CFG_HAVE_INTRIN_ENDPOINT
212 uchar usbTxPacketCnt1;
213
214 void usbSetInterrupt(uchar *data, uchar len)
215 {
216 uchar *p, i;
217
218 #if USB_CFG_IMPLEMENT_HALT
219 if(usbTxLen1 == USBPID_STALL)
220 return;
221 #endif
222 #if 0 /* No runtime checks! Caller is responsible for valid data! */
223 if(len > 8) /* interrupt transfers are limited to 8 bytes */
224 len = 8;
225 #endif
226 i = USBPID_DATA1;
227 if(usbTxPacketCnt1 & 1)
228 i = USBPID_DATA0;
229 if(usbTxLen1 & 0x10){ /* packet buffer was empty */
230 usbTxPacketCnt1++;
231 }else{
232 usbTxLen1 = USBPID_NAK; /* avoid sending incomplete interrupt data */
233 }
234 p = usbTxBuf1;
235 *p++ = i;
236 for(i=len;i--;)
237 *p++ = *data++;
238 usbCrc16Append(&usbTxBuf1[1], len);
239 usbTxLen1 = len + 4; /* len must be given including sync byte */
240 DBG2(0x21, usbTxBuf1, len + 3);
241 }
242 #endif
243
244 #if USB_CFG_HAVE_INTRIN_ENDPOINT3
245 uchar usbTxPacketCnt3;
246
247 void usbSetInterrupt3(uchar *data, uchar len)
248 {
249 uchar *p, i;
250
251 i = USBPID_DATA1;
252 if(usbTxPacketCnt3 & 1)
253 i = USBPID_DATA0;
254 if(usbTxLen3 & 0x10){ /* packet buffer was empty */
255 usbTxPacketCnt3++;
256 }else{
257 usbTxLen3 = USBPID_NAK; /* avoid sending incomplete interrupt data */
258 }
259 p = usbTxBuf3;
260 *p++ = i;
261 for(i=len;i--;)
262 *p++ = *data++;
263 usbCrc16Append(&usbTxBuf3[1], len);
264 usbTxLen3 = len + 4; /* len must be given including sync byte */
265 DBG2(0x23, usbTxBuf3, len + 3);
266 }
267 #endif
268
269
270 static uchar usbRead(uchar *data, uchar len)
271 {
272 #if USB_CFG_IMPLEMENT_FN_READ
273 if(usbMsgFlags & USB_FLG_USE_DEFAULT_RW){
274 #endif
275 uchar i = len, *r = usbMsgPtr;
276 if(usbMsgFlags & USB_FLG_MSGPTR_IS_ROM){ /* ROM data */
277 while(i--){
278 uchar c = PRG_RDB(r); /* assign to char size variable to enforce byte ops */
279 *data++ = c;
280 r++;
281 }
282 }else{ /* RAM data */
283 while(i--)
284 *data++ = *r++;
285 }
286 usbMsgPtr = r;
287 return len;
288 #if USB_CFG_IMPLEMENT_FN_READ
289 }else{
290 if(len != 0) /* don't bother app with 0 sized reads */
291 return usbFunctionRead(data, len);
292 return 0;
293 }
294 #endif
295 }
296
297
298 #define GET_DESCRIPTOR(cfgProp, staticName) \
299 if(cfgProp){ \
300 if((cfgProp) & USB_PROP_IS_RAM) \
301 flags &= ~USB_FLG_MSGPTR_IS_ROM; \
302 if((cfgProp) & USB_PROP_IS_DYNAMIC){ \
303 replyLen = usbFunctionDescriptor(rq); \
304 }else{ \
305 replyData = (uchar *)(staticName); \
306 SET_REPLY_LEN((cfgProp) & 0xff); \
307 } \
308 }
309 /* We use if() instead of #if in the macro above because #if can't be used
310 * in macros and the compiler optimizes constant conditions anyway.
311 */
312
313
314 /* Don't make this function static to avoid inlining.
315 * The entire function would become too large and exceed the range of
316 * relative jumps.
317 * 2006-02-25: Either gcc 3.4.3 is better than the gcc used when the comment
318 * above was written, or other parts of the code have changed. We now get
319 * better results with an inlined function. Test condition: PowerSwitch code.
320 */
321 static void usbProcessRx(uchar *data, uchar len)
322 {
323 usbRequest_t *rq = (void *)data;
324 uchar replyLen = 0, flags = USB_FLG_USE_DEFAULT_RW;
325 /* We use if() cascades because the compare is done byte-wise while switch()
326 * is int-based. The if() cascades are therefore more efficient.
327 */
328 DBG2(0x10 + ((usbRxToken >> 6) & 3), data, len);
329 #if USB_CFG_IMPLEMENT_FN_WRITEOUT
330 if(usbRxToken & 0x80){
331 usbFunctionWriteOut(data, len);
332 return; /* no reply expected, hence no usbMsgPtr, usbMsgFlags, usbMsgLen set */
333 }
334 if(usbRxToken == (uchar)(USBPID_SETUP & 0x7f)){ /* MSb contains endpoint (== 0) */
335 #else
336 if(usbRxToken == (uchar)USBPID_SETUP){
337 #endif
338 if(len == 8){ /* Setup size must be always 8 bytes. Ignore otherwise. */
339 uchar type = rq->bmRequestType & USBRQ_TYPE_MASK;
340 if(type == USBRQ_TYPE_STANDARD){
341 #define SET_REPLY_LEN(len) replyLen = (len); usbMsgPtr = replyData
342 /* This macro ensures that replyLen and usbMsgPtr are always set in the same way.
343 * That allows optimization of common code in if() branches */
344 uchar *replyData = usbTxBuf + 9; /* there is 3 bytes free space at the end of the buffer */
345 replyData[0] = 0; /* common to USBRQ_GET_STATUS and USBRQ_GET_INTERFACE */
346 if(rq->bRequest == USBRQ_GET_STATUS){ /* 0 */
347 uchar __attribute__((__unused__)) recipient = rq->bmRequestType & USBRQ_RCPT_MASK; /* assign arith ops to variables to enforce byte size */
348 #if USB_CFG_IS_SELF_POWERED
349 if(recipient == USBRQ_RCPT_DEVICE)
350 replyData[0] = USB_CFG_IS_SELF_POWERED;
351 #endif
352 #if USB_CFG_HAVE_INTRIN_ENDPOINT && USB_CFG_IMPLEMENT_HALT
353 if(recipient == USBRQ_RCPT_ENDPOINT && rq->wIndex.bytes[0] == 0x81) /* request status for endpoint 1 */
354 replyData[0] = usbTxLen1 == USBPID_STALL;
355 #endif
356 replyData[1] = 0;
357 SET_REPLY_LEN(2);
358 }else if(rq->bRequest == USBRQ_SET_ADDRESS){ /* 5 */
359 usbNewDeviceAddr = rq->wValue.bytes[0];
360 }else if(rq->bRequest == USBRQ_GET_DESCRIPTOR){ /* 6 */
361 flags = USB_FLG_MSGPTR_IS_ROM | USB_FLG_USE_DEFAULT_RW;
362 if(rq->wValue.bytes[1] == USBDESCR_DEVICE){ /* 1 */
363 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_DEVICE, usbDescriptorDevice)
364 }else if(rq->wValue.bytes[1] == USBDESCR_CONFIG){ /* 2 */
365 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_CONFIGURATION, usbDescriptorConfiguration)
366 }else if(rq->wValue.bytes[1] == USBDESCR_STRING){ /* 3 */
367 #if USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC
368 if(USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_RAM)
369 flags &= ~USB_FLG_MSGPTR_IS_ROM;
370 replyLen = usbFunctionDescriptor(rq);
371 #else /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */
372 if(rq->wValue.bytes[0] == 0){ /* descriptor index */
373 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_0, usbDescriptorString0)
374 }else if(rq->wValue.bytes[0] == 1){
375 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_VENDOR, usbDescriptorStringVendor)
376 }else if(rq->wValue.bytes[0] == 2){
377 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_DEVICE, usbDescriptorStringDevice)
378 }else if(rq->wValue.bytes[0] == 3){
379 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER, usbDescriptorStringSerialNumber)
380 }else if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){
381 replyLen = usbFunctionDescriptor(rq);
382 }
383 #endif /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */
384 }else if(rq->wValue.bytes[1] == USBDESCR_HID){ /* 0x21 */
385 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID, usbDescriptorConfiguration + 18)
386 }else if(rq->wValue.bytes[1] == USBDESCR_HID_REPORT){ /* 0x22 */
387 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID_REPORT, usbDescriptorHidReport)
388 }else if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){
389 replyLen = usbFunctionDescriptor(rq);
390 }
391 }else if(rq->bRequest == USBRQ_GET_CONFIGURATION){ /* 8 */
392 replyData = &usbConfiguration; /* send current configuration value */
393 SET_REPLY_LEN(1);
394 }else if(rq->bRequest == USBRQ_SET_CONFIGURATION){ /* 9 */
395 usbConfiguration = rq->wValue.bytes[0];
396 #if USB_CFG_IMPLEMENT_HALT
397 usbTxLen1 = USBPID_NAK;
398 #endif
399 }else if(rq->bRequest == USBRQ_GET_INTERFACE){ /* 10 */
400 SET_REPLY_LEN(1);
401 #if USB_CFG_HAVE_INTRIN_ENDPOINT
402 }else if(rq->bRequest == USBRQ_SET_INTERFACE){ /* 11 */
403 usbTxPacketCnt1 = 0; /* reset data toggling for interrupt endpoint */
404 # if USB_CFG_HAVE_INTRIN_ENDPOINT3
405 usbTxPacketCnt3 = 0; /* reset data toggling for interrupt endpoint */
406 # endif
407 # if USB_CFG_IMPLEMENT_HALT
408 usbTxLen1 = USBPID_NAK;
409 }else if(rq->bRequest == USBRQ_CLEAR_FEATURE || rq->bRequest == USBRQ_SET_FEATURE){ /* 1|3 */
410 if(rq->wValue.bytes[0] == 0 && rq->wIndex.bytes[0] == 0x81){ /* feature 0 == HALT for endpoint == 1 */
411 usbTxLen1 = rq->bRequest == USBRQ_CLEAR_FEATURE ? USBPID_NAK : USBPID_STALL;
412 usbTxPacketCnt1 = 0; /* reset data toggling for interrupt endpoint */
413 # if USB_CFG_HAVE_INTRIN_ENDPOINT3
414 usbTxPacketCnt3 = 0; /* reset data toggling for interrupt endpoint */
415 # endif
416 }
417 # endif
418 #endif
419 }else{
420 /* the following requests can be ignored, send default reply */
421 /* 1: CLEAR_FEATURE, 3: SET_FEATURE, 7: SET_DESCRIPTOR */
422 /* 12: SYNCH_FRAME */
423 }
424 #undef SET_REPLY_LEN
425 }else{ /* not a standard request -- must be vendor or class request */
426 replyLen = usbFunctionSetup(data);
427 }
428 #if USB_CFG_IMPLEMENT_FN_READ || USB_CFG_IMPLEMENT_FN_WRITE
429 if(replyLen == 0xff){ /* use user-supplied read/write function */
430 if((rq->bmRequestType & USBRQ_DIR_MASK) == USBRQ_DIR_DEVICE_TO_HOST){
431 replyLen = rq->wLength.bytes[0]; /* IN transfers only */
432 }
433 flags &= ~USB_FLG_USE_DEFAULT_RW; /* we have no valid msg, use user supplied read/write functions */
434 }else /* The 'else' prevents that we limit a replyLen of 0xff to the maximum transfer len. */
435 #endif
436 if(!rq->wLength.bytes[1] && replyLen > rq->wLength.bytes[0]) /* limit length to max */
437 replyLen = rq->wLength.bytes[0];
438 }
439 /* make sure that data packets which are sent as ACK to an OUT transfer are always zero sized */
440 }else{ /* DATA packet from out request */
441 #if USB_CFG_IMPLEMENT_FN_WRITE
442 if(!(usbMsgFlags & USB_FLG_USE_DEFAULT_RW)){
443 uchar rval = usbFunctionWrite(data, len);
444 replyLen = 0xff;
445 if(rval == 0xff){ /* an error occurred */
446 usbMsgLen = 0xff; /* cancel potentially pending data packet for ACK */
447 usbTxLen = USBPID_STALL;
448 }else if(rval != 0){ /* This was the final package */
449 replyLen = 0; /* answer with a zero-sized data packet */
450 }
451 flags = 0; /* start with a DATA1 package, stay with user supplied write() function */
452 }
453 #endif
454 }
455 usbMsgFlags = flags;
456 usbMsgLen = replyLen;
457 }
458
459 /* ------------------------------------------------------------------------- */
460
461 static void usbBuildTxBlock(void)
462 {
463 uchar wantLen, len, txLen, token;
464
465 wantLen = usbMsgLen;
466 if(wantLen > 8)
467 wantLen = 8;
468 usbMsgLen -= wantLen;
469 token = USBPID_DATA1;
470 if(usbMsgFlags & USB_FLG_TX_PACKET)
471 token = USBPID_DATA0;
472 usbMsgFlags++;
473 len = usbRead(usbTxBuf + 1, wantLen);
474 if(len <= 8){ /* valid data packet */
475 usbCrc16Append(&usbTxBuf[1], len);
476 txLen = len + 4; /* length including sync byte */
477 if(len < 8) /* a partial package identifies end of message */
478 usbMsgLen = 0xff;
479 }else{
480 txLen = USBPID_STALL; /* stall the endpoint */
481 usbMsgLen = 0xff;
482 }
483 usbTxBuf[0] = token;
484 usbTxLen = txLen;
485 DBG2(0x20, usbTxBuf, txLen-1);
486 }
487
488 static inline uchar isNotSE0(void)
489 {
490 uchar rval;
491 /* We want to do
492 * return (USBIN & USBMASK);
493 * here, but the compiler does int-expansion acrobatics.
494 * We can avoid this by assigning to a char-sized variable.
495 */
496 rval = USBIN & USBMASK;
497 return rval;
498 }
499
500 /* ------------------------------------------------------------------------- */
501
502 void usbPoll(void)
503 {
504 uchar len;
505
506 if((len = usbRxLen) > 0){
507 /* We could check CRC16 here -- but ACK has already been sent anyway. If you
508 * need data integrity checks with this driver, check the CRC in your app
509 * code and report errors back to the host. Since the ACK was already sent,
510 * retries must be handled on application level.
511 * unsigned crc = usbCrc16((uchar *)(unsigned)(usbAppBuf + 1), usbRxLen - 3);
512 */
513 len -= 3; /* remove PID and CRC */
514 if(len < 128){ /* no overflow */
515 converter_t appBuf;
516 appBuf.ptr = (uchar *)usbRxBuf;
517 appBuf.bytes[0] = usbAppBuf;
518 appBuf.bytes[0]++;
519 usbProcessRx(appBuf.ptr, len);
520 }
521 #if USB_CFG_HAVE_FLOWCONTROL
522 if(usbRxLen > 0) /* only mark as available if not inactivated */
523 usbRxLen = 0;
524 #else
525 usbRxLen = 0; /* mark rx buffer as available */
526 #endif
527 }
528 if(usbMsgLen != 0xff){ /* transmit data pending? */
529 if(usbTxLen & 0x10) /* transmit system idle */
530 usbBuildTxBlock();
531 }
532 if(isNotSE0()){ /* SE0 state */
533 usbIsReset = 0;
534 }else{
535 /* check whether SE0 lasts for more than 2.5us (3.75 bit times) */
536 if(!usbIsReset){
537 uchar i;
538 for(i=100;i;i--){
539 if(isNotSE0())
540 goto notUsbReset;
541 }
542 usbIsReset = 1;
543 usbNewDeviceAddr = 0;
544 usbDeviceAddr = 0;
545 #if USB_CFG_IMPLEMENT_HALT
546 usbTxLen1 = USBPID_NAK;
547 #if USB_CFG_HAVE_INTRIN_ENDPOINT3
548 usbTxLen3 = USBPID_NAK;
549 #endif
550 #endif
551 DBG1(0xff, 0, 0);
552 notUsbReset:;
553 }
554 }
555 }
556
557 /* ------------------------------------------------------------------------- */
558
559 void usbInit(void)
560 {
561 usbInputBuf = (uchar)usbRxBuf[0];
562 usbAppBuf = (uchar)usbRxBuf[1];
563 #if USB_INTR_CFG_SET != 0
564 USB_INTR_CFG |= USB_INTR_CFG_SET;
565 #endif
566 #if USB_INTR_CFG_CLR != 0
567 USB_INTR_CFG &= ~(USB_INTR_CFG_CLR);
568 #endif
569 USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT);
570 }
571
572 /* ------------------------------------------------------------------------- */