usbdrv.h 30 KB

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