Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usbdrv.h 30KB

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