usbdrv.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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 769 2009-08-22 11:49:05Z 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 be declared as "INTERRUPT"
  94. instead of "SIGNAL" (see "avr/signal.h") or that they are written in assembler
  95. with "sei" 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 20090822
  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. #ifdef __cplusplus
  144. extern "C"{
  145. #endif
  146. USB_PUBLIC void usbInit(void);
  147. /* This function must be called before interrupts are enabled and the main
  148. * loop is entered. We exepct that the PORT and DDR bits for D+ and D- have
  149. * not been changed from their default status (which is 0). If you have changed
  150. * them, set both back to 0 (configure them as input with no internal pull-up).
  151. */
  152. USB_PUBLIC void usbPoll(void);
  153. /* This function must be called at regular intervals from the main loop.
  154. * Maximum delay between calls is somewhat less than 50ms (USB timeout for
  155. * accepting a Setup message). Otherwise the device will not be recognized.
  156. * Please note that debug outputs through the UART take ~ 0.5ms per byte
  157. * at 19200 bps.
  158. */
  159. #ifdef __cplusplus
  160. } // extern "C"
  161. #endif
  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. #ifdef __cplusplus
  168. extern "C"{
  169. #endif
  170. USB_PUBLIC usbMsgLen_t usbFunctionSetup(uchar data[8]);
  171. #ifdef __cplusplus
  172. } // extern "C"
  173. #endif
  174. /* This function is called when the driver receives a SETUP transaction from
  175. * the host which is not answered by the driver itself (in practice: class and
  176. * vendor requests). All control transfers start with a SETUP transaction where
  177. * the host communicates the parameters of the following (optional) data
  178. * transfer. The SETUP data is available in the 'data' parameter which can
  179. * (and should) be casted to 'usbRequest_t *' for a more user-friendly access
  180. * to parameters.
  181. *
  182. * If the SETUP indicates a control-in transfer, you should provide the
  183. * requested data to the driver. There are two ways to transfer this data:
  184. * (1) Set the global pointer 'usbMsgPtr' to the base of the static RAM data
  185. * block and return the length of the data in 'usbFunctionSetup()'. The driver
  186. * will handle the rest. Or (2) return USB_NO_MSG in 'usbFunctionSetup()'. The
  187. * driver will then call 'usbFunctionRead()' when data is needed. See the
  188. * documentation for usbFunctionRead() for details.
  189. *
  190. * If the SETUP indicates a control-out transfer, the only way to receive the
  191. * data from the host is through the 'usbFunctionWrite()' call. If you
  192. * implement this function, you must return USB_NO_MSG in 'usbFunctionSetup()'
  193. * to indicate that 'usbFunctionWrite()' should be used. See the documentation
  194. * of this function for more information. If you just want to ignore the data
  195. * sent by the host, return 0 in 'usbFunctionSetup()'.
  196. *
  197. * Note that calls to the functions usbFunctionRead() and usbFunctionWrite()
  198. * are only done if enabled by the configuration in usbconfig.h.
  199. */
  200. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq);
  201. /* You need to implement this function ONLY if you provide USB descriptors at
  202. * runtime (which is an expert feature). It is very similar to
  203. * usbFunctionSetup() above, but it is called only to request USB descriptor
  204. * data. See the documentation of usbFunctionSetup() above for more info.
  205. */
  206. #if USB_CFG_HAVE_INTRIN_ENDPOINT
  207. #ifdef __cplusplus
  208. extern "C"{
  209. #endif
  210. USB_PUBLIC void usbSetInterrupt(uchar *data, uchar len);
  211. #ifdef __cplusplus
  212. } // extern "C"
  213. #endif
  214. /* This function sets the message which will be sent during the next interrupt
  215. * IN transfer. The message is copied to an internal buffer and must not exceed
  216. * a length of 8 bytes. The message may be 0 bytes long just to indicate the
  217. * interrupt status to the host.
  218. * If you need to transfer more bytes, use a control read after the interrupt.
  219. */
  220. #define usbInterruptIsReady() (usbTxLen1 & 0x10)
  221. /* This macro indicates whether the last interrupt message has already been
  222. * sent. If you set a new interrupt message before the old was sent, the
  223. * message already buffered will be lost.
  224. */
  225. #if USB_CFG_HAVE_INTRIN_ENDPOINT3
  226. USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len);
  227. #define usbInterruptIsReady3() (usbTxLen3 & 0x10)
  228. /* Same as above for endpoint 3 */
  229. #endif
  230. #endif /* USB_CFG_HAVE_INTRIN_ENDPOINT */
  231. #if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* simplified interface for backward compatibility */
  232. #define usbHidReportDescriptor usbDescriptorHidReport
  233. /* should be declared as: PROGMEM char usbHidReportDescriptor[]; */
  234. /* If you implement an HID device, you need to provide a report descriptor.
  235. * The HID report descriptor syntax is a bit complex. If you understand how
  236. * report descriptors are constructed, we recommend that you use the HID
  237. * Descriptor Tool from usb.org, see http://www.usb.org/developers/hidpage/.
  238. * Otherwise you should probably start with a working example.
  239. */
  240. #endif /* USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH */
  241. #if USB_CFG_IMPLEMENT_FN_WRITE
  242. #ifdef __cplusplus
  243. extern "C"{
  244. #endif
  245. USB_PUBLIC uchar usbFunctionWrite(uchar *data, uchar len);
  246. #ifdef __cplusplus
  247. } // extern "C"
  248. #endif
  249. /* This function is called by the driver to provide a control transfer's
  250. * payload data (control-out). It is called in chunks of up to 8 bytes. The
  251. * total count provided in the current control transfer can be obtained from
  252. * the 'length' property in the setup data. If an error occurred during
  253. * processing, return 0xff (== -1). The driver will answer the entire transfer
  254. * with a STALL token in this case. If you have received the entire payload
  255. * successfully, return 1. If you expect more data, return 0. If you don't
  256. * know whether the host will send more data (you should know, the total is
  257. * provided in the usbFunctionSetup() call!), return 1.
  258. * NOTE: If you return 0xff for STALL, 'usbFunctionWrite()' may still be called
  259. * for the remaining data. You must continue to return 0xff for STALL in these
  260. * calls.
  261. * In order to get usbFunctionWrite() called, define USB_CFG_IMPLEMENT_FN_WRITE
  262. * to 1 in usbconfig.h and return 0xff in usbFunctionSetup()..
  263. */
  264. #endif /* USB_CFG_IMPLEMENT_FN_WRITE */
  265. #if USB_CFG_IMPLEMENT_FN_READ
  266. USB_PUBLIC uchar usbFunctionRead(uchar *data, uchar len);
  267. /* This function is called by the driver to ask the application for a control
  268. * transfer's payload data (control-in). It is called in chunks of up to 8
  269. * bytes each. You should copy the data to the location given by 'data' and
  270. * return the actual number of bytes copied. If you return less than requested,
  271. * the control-in transfer is terminated. If you return 0xff, the driver aborts
  272. * the transfer with a STALL token.
  273. * In order to get usbFunctionRead() called, define USB_CFG_IMPLEMENT_FN_READ
  274. * to 1 in usbconfig.h and return 0xff in usbFunctionSetup()..
  275. */
  276. #endif /* USB_CFG_IMPLEMENT_FN_READ */
  277. extern uchar usbRxToken; /* may be used in usbFunctionWriteOut() below */
  278. #if USB_CFG_IMPLEMENT_FN_WRITEOUT
  279. USB_PUBLIC void usbFunctionWriteOut(uchar *data, uchar len);
  280. /* This function is called by the driver when data is received on an interrupt-
  281. * or bulk-out endpoint. The endpoint number can be found in the global
  282. * variable usbRxToken. You must define USB_CFG_IMPLEMENT_FN_WRITEOUT to 1 in
  283. * usbconfig.h to get this function called.
  284. */
  285. #endif /* USB_CFG_IMPLEMENT_FN_WRITEOUT */
  286. #ifdef USB_CFG_PULLUP_IOPORTNAME
  287. #define usbDeviceConnect() ((USB_PULLUP_DDR |= (1<<USB_CFG_PULLUP_BIT)), \
  288. (USB_PULLUP_OUT |= (1<<USB_CFG_PULLUP_BIT)))
  289. #define usbDeviceDisconnect() ((USB_PULLUP_DDR &= ~(1<<USB_CFG_PULLUP_BIT)), \
  290. (USB_PULLUP_OUT &= ~(1<<USB_CFG_PULLUP_BIT)))
  291. #else /* USB_CFG_PULLUP_IOPORTNAME */
  292. #define usbDeviceConnect() (USBDDR &= ~(1<<USBMINUS))
  293. #define usbDeviceDisconnect() (USBDDR |= (1<<USBMINUS))
  294. #endif /* USB_CFG_PULLUP_IOPORTNAME */
  295. /* The macros usbDeviceConnect() and usbDeviceDisconnect() (intended to look
  296. * like a function) connect resp. disconnect the device from the host's USB.
  297. * If the constants USB_CFG_PULLUP_IOPORT and USB_CFG_PULLUP_BIT are defined
  298. * in usbconfig.h, a disconnect consists of removing the pull-up resisitor
  299. * from D-, otherwise the disconnect is done by brute-force pulling D- to GND.
  300. * This does not conform to the spec, but it works.
  301. * Please note that the USB interrupt must be disabled while the device is
  302. * in disconnected state, or the interrupt handler will hang! You can either
  303. * turn off the USB interrupt selectively with
  304. * USB_INTR_ENABLE &= ~(1 << USB_INTR_ENABLE_BIT)
  305. * or use cli() to disable interrupts globally.
  306. */
  307. extern unsigned usbCrc16(unsigned data, uchar len);
  308. #define usbCrc16(data, len) usbCrc16((unsigned)(data), len)
  309. /* This function calculates the binary complement of the data CRC used in
  310. * USB data packets. The value is used to build raw transmit packets.
  311. * You may want to use this function for data checksums or to verify received
  312. * data. We enforce 16 bit calling conventions for compatibility with IAR's
  313. * tiny memory model.
  314. */
  315. #ifdef __cplusplus
  316. extern "C"{
  317. #endif
  318. extern unsigned usbCrc16Append(unsigned data, uchar len);
  319. #ifdef __cplusplus
  320. } // extern "C"
  321. #endif
  322. #define usbCrc16Append(data, len) usbCrc16Append((unsigned)(data), len)
  323. /* This function is equivalent to usbCrc16() above, except that it appends
  324. * the 2 bytes CRC (lowbyte first) in the 'data' buffer after reading 'len'
  325. * bytes.
  326. */
  327. #if USB_CFG_HAVE_MEASURE_FRAME_LENGTH
  328. extern unsigned usbMeasureFrameLength(void);
  329. /* This function MUST be called IMMEDIATELY AFTER USB reset and measures 1/7 of
  330. * the number of CPU cycles during one USB frame minus one low speed bit
  331. * length. In other words: return value = 1499 * (F_CPU / 10.5 MHz)
  332. * Since this is a busy wait, you MUST disable all interrupts with cli() before
  333. * calling this function.
  334. * This can be used to calibrate the AVR's RC oscillator.
  335. */
  336. #endif
  337. extern uchar usbConfiguration;
  338. /* This value contains the current configuration set by the host. The driver
  339. * allows setting and querying of this variable with the USB SET_CONFIGURATION
  340. * and GET_CONFIGURATION requests, but does not use it otherwise.
  341. * You may want to reflect the "configured" status with a LED on the device or
  342. * switch on high power parts of the circuit only if the device is configured.
  343. */
  344. #if USB_COUNT_SOF
  345. extern volatile uchar usbSofCount;
  346. /* This variable is incremented on every SOF packet. It is only available if
  347. * the macro USB_COUNT_SOF is defined to a value != 0.
  348. */
  349. #endif
  350. #if USB_CFG_CHECK_DATA_TOGGLING
  351. extern uchar usbCurrentDataToken;
  352. /* This variable can be checked in usbFunctionWrite() and usbFunctionWriteOut()
  353. * to ignore duplicate packets.
  354. */
  355. #endif
  356. #define USB_STRING_DESCRIPTOR_HEADER(stringLength) ((2*(stringLength)+2) | (3<<8))
  357. /* This macro builds a descriptor header for a string descriptor given the
  358. * string's length. See usbdrv.c for an example how to use it.
  359. */
  360. #if USB_CFG_HAVE_FLOWCONTROL
  361. extern volatile schar usbRxLen;
  362. #define usbDisableAllRequests() usbRxLen = -1
  363. /* Must be called from usbFunctionWrite(). This macro disables all data input
  364. * from the USB interface. Requests from the host are answered with a NAK
  365. * while they are disabled.
  366. */
  367. #define usbEnableAllRequests() usbRxLen = 0
  368. /* May only be called if requests are disabled. This macro enables input from
  369. * the USB interface after it has been disabled with usbDisableAllRequests().
  370. */
  371. #define usbAllRequestsAreDisabled() (usbRxLen < 0)
  372. /* Use this macro to find out whether requests are disabled. It may be needed
  373. * to ensure that usbEnableAllRequests() is never called when requests are
  374. * enabled.
  375. */
  376. #endif
  377. #define USB_SET_DATATOKEN1(token) usbTxBuf1[0] = token
  378. #define USB_SET_DATATOKEN3(token) usbTxBuf3[0] = token
  379. /* These two macros can be used by application software to reset data toggling
  380. * for interrupt-in endpoints 1 and 3. Since the token is toggled BEFORE
  381. * sending data, you must set the opposite value of the token which should come
  382. * first.
  383. */
  384. #endif /* __ASSEMBLER__ */
  385. /* ------------------------------------------------------------------------- */
  386. /* ----------------- Definitions for Descriptor Properties ----------------- */
  387. /* ------------------------------------------------------------------------- */
  388. /* This is advanced stuff. See usbconfig-prototype.h for more information
  389. * about the various methods to define USB descriptors. If you do nothing,
  390. * the default descriptors will be used.
  391. */
  392. #define USB_PROP_IS_DYNAMIC (1 << 14)
  393. /* If this property is set for a descriptor, usbFunctionDescriptor() will be
  394. * used to obtain the particular descriptor. Data directly returned via
  395. * usbMsgPtr are FLASH data by default, combine (OR) with USB_PROP_IS_RAM to
  396. * return RAM data.
  397. */
  398. #define USB_PROP_IS_RAM (1 << 15)
  399. /* If this property is set for a descriptor, the data is read from RAM
  400. * memory instead of Flash. The property is used for all methods to provide
  401. * external descriptors.
  402. */
  403. #define USB_PROP_LENGTH(len) ((len) & 0x3fff)
  404. /* If a static external descriptor is used, this is the total length of the
  405. * descriptor in bytes.
  406. */
  407. /* all descriptors which may have properties: */
  408. #ifndef USB_CFG_DESCR_PROPS_DEVICE
  409. #define USB_CFG_DESCR_PROPS_DEVICE 0
  410. #endif
  411. #ifndef USB_CFG_DESCR_PROPS_CONFIGURATION
  412. #define USB_CFG_DESCR_PROPS_CONFIGURATION 0
  413. #endif
  414. #ifndef USB_CFG_DESCR_PROPS_STRINGS
  415. #define USB_CFG_DESCR_PROPS_STRINGS 0
  416. #endif
  417. #ifndef USB_CFG_DESCR_PROPS_STRING_0
  418. #define USB_CFG_DESCR_PROPS_STRING_0 0
  419. #endif
  420. #ifndef USB_CFG_DESCR_PROPS_STRING_VENDOR
  421. #define USB_CFG_DESCR_PROPS_STRING_VENDOR 0
  422. #endif
  423. #ifndef USB_CFG_DESCR_PROPS_STRING_PRODUCT
  424. #define USB_CFG_DESCR_PROPS_STRING_PRODUCT 0
  425. #endif
  426. #ifndef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
  427. #define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER 0
  428. #endif
  429. #ifndef USB_CFG_DESCR_PROPS_HID
  430. #define USB_CFG_DESCR_PROPS_HID 0
  431. #endif
  432. #if !(USB_CFG_DESCR_PROPS_HID_REPORT)
  433. # undef USB_CFG_DESCR_PROPS_HID_REPORT
  434. # if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* do some backward compatibility tricks */
  435. # define USB_CFG_DESCR_PROPS_HID_REPORT USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH
  436. # else
  437. # define USB_CFG_DESCR_PROPS_HID_REPORT 0
  438. # endif
  439. #endif
  440. #ifndef USB_CFG_DESCR_PROPS_UNKNOWN
  441. #define USB_CFG_DESCR_PROPS_UNKNOWN 0
  442. #endif
  443. /* ------------------ forward declaration of descriptors ------------------- */
  444. /* If you use external static descriptors, they must be stored in global
  445. * arrays as declared below:
  446. */
  447. #ifndef __ASSEMBLER__
  448. extern
  449. #if !(USB_CFG_DESCR_PROPS_DEVICE & USB_PROP_IS_RAM)
  450. PROGMEM
  451. #endif
  452. const char usbDescriptorDevice[];
  453. extern
  454. #if !(USB_CFG_DESCR_PROPS_CONFIGURATION & USB_PROP_IS_RAM)
  455. PROGMEM
  456. #endif
  457. const char usbDescriptorConfiguration[];
  458. extern
  459. #if !(USB_CFG_DESCR_PROPS_HID_REPORT & USB_PROP_IS_RAM)
  460. PROGMEM
  461. #endif
  462. const char usbDescriptorHidReport[];
  463. extern
  464. #if !(USB_CFG_DESCR_PROPS_STRING_0 & USB_PROP_IS_RAM)
  465. PROGMEM
  466. #endif
  467. const char usbDescriptorString0[];
  468. extern
  469. #if !(USB_CFG_DESCR_PROPS_STRING_VENDOR & USB_PROP_IS_RAM)
  470. PROGMEM
  471. #endif
  472. const int usbDescriptorStringVendor[];
  473. extern
  474. #if !(USB_CFG_DESCR_PROPS_STRING_PRODUCT & USB_PROP_IS_RAM)
  475. PROGMEM
  476. #endif
  477. const int usbDescriptorStringDevice[];
  478. extern
  479. #if !(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER & USB_PROP_IS_RAM)
  480. PROGMEM
  481. #endif
  482. const int usbDescriptorStringSerialNumber[];
  483. #endif /* __ASSEMBLER__ */
  484. /* ------------------------------------------------------------------------- */
  485. /* ------------------------ General Purpose Macros ------------------------- */
  486. /* ------------------------------------------------------------------------- */
  487. #define USB_CONCAT(a, b) a ## b
  488. #define USB_CONCAT_EXPANDED(a, b) USB_CONCAT(a, b)
  489. #define USB_OUTPORT(name) USB_CONCAT(PORT, name)
  490. #define USB_INPORT(name) USB_CONCAT(PIN, name)
  491. #define USB_DDRPORT(name) USB_CONCAT(DDR, name)
  492. /* The double-define trick above lets us concatenate strings which are
  493. * defined by macros.
  494. */
  495. /* ------------------------------------------------------------------------- */
  496. /* ------------------------- Constant definitions -------------------------- */
  497. /* ------------------------------------------------------------------------- */
  498. #if !defined __ASSEMBLER__ && (!defined USB_CFG_VENDOR_ID || !defined USB_CFG_DEVICE_ID)
  499. #warning "You should define USB_CFG_VENDOR_ID and USB_CFG_DEVICE_ID in usbconfig.h"
  500. /* If the user has not defined IDs, we default to obdev's free IDs.
  501. * See USB-IDs-for-free.txt for details.
  502. */
  503. #endif
  504. /* make sure we have a VID and PID defined, byte order is lowbyte, highbyte */
  505. #ifndef USB_CFG_VENDOR_ID
  506. # define USB_CFG_VENDOR_ID 0xc0, 0x16 /* = 0x16c0 = 5824 = voti.nl */
  507. #endif
  508. #ifndef USB_CFG_DEVICE_ID
  509. # if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH
  510. # define USB_CFG_DEVICE_ID 0xdf, 0x05 /* = 0x5df = 1503, shared PID for HIDs */
  511. # elif USB_CFG_INTERFACE_CLASS == 2
  512. # define USB_CFG_DEVICE_ID 0xe1, 0x05 /* = 0x5e1 = 1505, shared PID for CDC Modems */
  513. # else
  514. # define USB_CFG_DEVICE_ID 0xdc, 0x05 /* = 0x5dc = 1500, obdev's free PID */
  515. # endif
  516. #endif
  517. /* Derive Output, Input and DataDirection ports from port names */
  518. #ifndef USB_CFG_IOPORTNAME
  519. #error "You must define USB_CFG_IOPORTNAME in usbconfig.h, see usbconfig-prototype.h"
  520. #endif
  521. #define USBOUT USB_OUTPORT(USB_CFG_IOPORTNAME)
  522. #define USB_PULLUP_OUT USB_OUTPORT(USB_CFG_PULLUP_IOPORTNAME)
  523. #define USBIN USB_INPORT(USB_CFG_IOPORTNAME)
  524. #define USBDDR USB_DDRPORT(USB_CFG_IOPORTNAME)
  525. #define USB_PULLUP_DDR USB_DDRPORT(USB_CFG_PULLUP_IOPORTNAME)
  526. #define USBMINUS USB_CFG_DMINUS_BIT
  527. #define USBPLUS USB_CFG_DPLUS_BIT
  528. #define USBIDLE (1<<USB_CFG_DMINUS_BIT) /* value representing J state */
  529. #define USBMASK ((1<<USB_CFG_DPLUS_BIT) | (1<<USB_CFG_DMINUS_BIT)) /* mask for USB I/O bits */
  530. /* defines for backward compatibility with older driver versions: */
  531. #define USB_CFG_IOPORT USB_OUTPORT(USB_CFG_IOPORTNAME)
  532. #ifdef USB_CFG_PULLUP_IOPORTNAME
  533. #define USB_CFG_PULLUP_IOPORT USB_OUTPORT(USB_CFG_PULLUP_IOPORTNAME)
  534. #endif
  535. #ifndef USB_CFG_EP3_NUMBER /* if not defined in usbconfig.h */
  536. #define USB_CFG_EP3_NUMBER 3
  537. #endif
  538. #ifndef USB_CFG_HAVE_INTRIN_ENDPOINT3
  539. #define USB_CFG_HAVE_INTRIN_ENDPOINT3 0
  540. #endif
  541. #define USB_BUFSIZE 11 /* PID, 8 bytes data, 2 bytes CRC */
  542. /* ----- Try to find registers and bits responsible for ext interrupt 0 ----- */
  543. #ifndef USB_INTR_CFG /* allow user to override our default */
  544. # if defined EICRA
  545. # define USB_INTR_CFG EICRA
  546. # else
  547. # define USB_INTR_CFG MCUCR
  548. # endif
  549. #endif
  550. #ifndef USB_INTR_CFG_SET /* allow user to override our default */
  551. # if defined(USB_COUNT_SOF) || defined(USB_SOF_HOOK)
  552. # define USB_INTR_CFG_SET (1 << ISC01) /* cfg for falling edge */
  553. /* If any SOF logic is used, the interrupt must be wired to D- where
  554. * we better trigger on falling edge
  555. */
  556. # else
  557. # define USB_INTR_CFG_SET ((1 << ISC00) | (1 << ISC01)) /* cfg for rising edge */
  558. # endif
  559. #endif
  560. #ifndef USB_INTR_CFG_CLR /* allow user to override our default */
  561. # define USB_INTR_CFG_CLR 0 /* no bits to clear */
  562. #endif
  563. #ifndef USB_INTR_ENABLE /* allow user to override our default */
  564. # if defined GIMSK
  565. # define USB_INTR_ENABLE GIMSK
  566. # elif defined EIMSK
  567. # define USB_INTR_ENABLE EIMSK
  568. # else
  569. # define USB_INTR_ENABLE GICR
  570. # endif
  571. #endif
  572. #ifndef USB_INTR_ENABLE_BIT /* allow user to override our default */
  573. # define USB_INTR_ENABLE_BIT INT0
  574. #endif
  575. #ifndef USB_INTR_PENDING /* allow user to override our default */
  576. # if defined EIFR
  577. # define USB_INTR_PENDING EIFR
  578. # else
  579. # define USB_INTR_PENDING GIFR
  580. # endif
  581. #endif
  582. #ifndef USB_INTR_PENDING_BIT /* allow user to override our default */
  583. # define USB_INTR_PENDING_BIT INTF0
  584. #endif
  585. /*
  586. The defines above don't work for the following chips
  587. at90c8534: no ISC0?, no PORTB, can't find a data sheet
  588. at86rf401: no PORTB, no MCUCR etc, low clock rate
  589. atmega103: no ISC0? (maybe omission in header, can't find data sheet)
  590. atmega603: not defined in avr-libc
  591. at43usb320, at43usb355, at76c711: have USB anyway
  592. at94k: is different...
  593. at90s1200, attiny11, attiny12, attiny15, attiny28: these have no RAM
  594. */
  595. /* ------------------------------------------------------------------------- */
  596. /* ----------------- USB Specification Constants and Types ----------------- */
  597. /* ------------------------------------------------------------------------- */
  598. /* USB Token values */
  599. #define USBPID_SETUP 0x2d
  600. #define USBPID_OUT 0xe1
  601. #define USBPID_IN 0x69
  602. #define USBPID_DATA0 0xc3
  603. #define USBPID_DATA1 0x4b
  604. #define USBPID_ACK 0xd2
  605. #define USBPID_NAK 0x5a
  606. #define USBPID_STALL 0x1e
  607. #ifndef USB_INITIAL_DATATOKEN
  608. #define USB_INITIAL_DATATOKEN USBPID_DATA1
  609. #endif
  610. #ifndef __ASSEMBLER__
  611. typedef struct usbTxStatus{
  612. volatile uchar len;
  613. uchar buffer[USB_BUFSIZE];
  614. }usbTxStatus_t;
  615. extern usbTxStatus_t usbTxStatus1, usbTxStatus3;
  616. #define usbTxLen1 usbTxStatus1.len
  617. #define usbTxBuf1 usbTxStatus1.buffer
  618. #define usbTxLen3 usbTxStatus3.len
  619. #define usbTxBuf3 usbTxStatus3.buffer
  620. typedef union usbWord{
  621. unsigned word;
  622. uchar bytes[2];
  623. }usbWord_t;
  624. typedef struct usbRequest{
  625. uchar bmRequestType;
  626. uchar bRequest;
  627. usbWord_t wValue;
  628. usbWord_t wIndex;
  629. usbWord_t wLength;
  630. }usbRequest_t;
  631. /* This structure matches the 8 byte setup request */
  632. #endif
  633. /* bmRequestType field in USB setup:
  634. * d t t r r r r r, where
  635. * d ..... direction: 0=host->device, 1=device->host
  636. * t ..... type: 0=standard, 1=class, 2=vendor, 3=reserved
  637. * r ..... recipient: 0=device, 1=interface, 2=endpoint, 3=other
  638. */
  639. /* USB setup recipient values */
  640. #define USBRQ_RCPT_MASK 0x1f
  641. #define USBRQ_RCPT_DEVICE 0
  642. #define USBRQ_RCPT_INTERFACE 1
  643. #define USBRQ_RCPT_ENDPOINT 2
  644. /* USB request type values */
  645. #define USBRQ_TYPE_MASK 0x60
  646. #define USBRQ_TYPE_STANDARD (0<<5)
  647. #define USBRQ_TYPE_CLASS (1<<5)
  648. #define USBRQ_TYPE_VENDOR (2<<5)
  649. /* USB direction values: */
  650. #define USBRQ_DIR_MASK 0x80
  651. #define USBRQ_DIR_HOST_TO_DEVICE (0<<7)
  652. #define USBRQ_DIR_DEVICE_TO_HOST (1<<7)
  653. /* USB Standard Requests */
  654. #define USBRQ_GET_STATUS 0
  655. #define USBRQ_CLEAR_FEATURE 1
  656. #define USBRQ_SET_FEATURE 3
  657. #define USBRQ_SET_ADDRESS 5
  658. #define USBRQ_GET_DESCRIPTOR 6
  659. #define USBRQ_SET_DESCRIPTOR 7
  660. #define USBRQ_GET_CONFIGURATION 8
  661. #define USBRQ_SET_CONFIGURATION 9
  662. #define USBRQ_GET_INTERFACE 10
  663. #define USBRQ_SET_INTERFACE 11
  664. #define USBRQ_SYNCH_FRAME 12
  665. /* USB descriptor constants */
  666. #define USBDESCR_DEVICE 1
  667. #define USBDESCR_CONFIG 2
  668. #define USBDESCR_STRING 3
  669. #define USBDESCR_INTERFACE 4
  670. #define USBDESCR_ENDPOINT 5
  671. #define USBDESCR_HID 0x21
  672. #define USBDESCR_HID_REPORT 0x22
  673. #define USBDESCR_HID_PHYS 0x23
  674. //#define USBATTR_BUSPOWER 0x80 // USB 1.1 does not define this value any more
  675. #define USBATTR_SELFPOWER 0x40
  676. #define USBATTR_REMOTEWAKE 0x20
  677. /* USB HID Requests */
  678. #define USBRQ_HID_GET_REPORT 0x01
  679. #define USBRQ_HID_GET_IDLE 0x02
  680. #define USBRQ_HID_GET_PROTOCOL 0x03
  681. #define USBRQ_HID_SET_REPORT 0x09
  682. #define USBRQ_HID_SET_IDLE 0x0a
  683. #define USBRQ_HID_SET_PROTOCOL 0x0b
  684. /* ------------------------------------------------------------------------- */
  685. #endif /* __usbdrv_h_included__ */