Commit Graph

103 Commits

Author SHA1 Message Date
Karl Palsson df15b263d2 usb: hid: add a stub file for doxygen
Unlike with the doxygen source generation, we can't autoguess which of
the class files are eligible automatically.  Instead, make a stub hid
file, (which we can now start adding to, if desired) and include it in
all builds that include other class stubs.
2019-12-25 21:04:17 +00:00
Karl Palsson 1f359e0cb8 doc: efm32: tag USB files for doxygen generation 2019-06-25 21:15:19 +00:00
Karl Palsson 26ab78a710 usb: fix race between setup complete and endpoint nak
See https://github.com/libopencm3/libopencm3/issues/873

Commentary describing this patch originally by zyp:

```
After looking further into it, I've concluded that my preliminary
analysis looks correct. The problem is that setting CNAK before
the SETUP complete event is received causes a race condition. The
SETUP callback is called when the SETUP packet event is received,
which means that setting CNAK from the callback is too early.

Originally the problem was that CNAK was set by ep_read() which is
called by the callback. #672 solved this by moving CNAK out of
ep_read() and calling it after the SETUP complete event is received
instead.

The regression by #785 is caused by the introduction of flow control
calls into the SETUP callback. They also set CNAK.

To solve this properly, I propose changing the event handling code
to only call the SETUP callback after the SETUP complete event is
received. Unfortunately, this implies that the callback can't call
ep_read() itself anymore, because the packet has to be read out of
the FIFO before the SETUP complete event arrives. This implies a
change of the API between the hardware drivers and _usbd_control_setup().
```

L1 (st_usbfs) works and passes tests as before change
F4 (dwc_otg_fs) works and now passes tests. (yay)
LM4f still compiles, and has had the same style of implementation as
st_usbfs, however has not been tested on any hardware.
2018-08-27 15:11:32 +00:00
David Lamparter ebcf197810 usb: make usb_disconnect() weak
usb_disconnect() is board/platform specific for chips that don't have
built-in handling for this.  Allow the application to provide its own
version of usb_disconnect() to handle this.

See https://github.com/libopencm3/libopencm3/pull/924
2018-08-27 15:11:32 +00:00
David Lamparter 343cff4675 usb: make strings "const char * const *"
This allows the pointer table to be in Flash, by using
"static const char * const strings[] = { ... };"

See https://github.com/libopencm3/libopencm3/pull/924
2018-08-27 13:34:52 +00:00
Karl Palsson f4bbe7c5bb usb: prevent registering duplicate config callbacks
Originally discussed at https://github.com/libopencm3/libopencm3/pull/722
2018-08-17 00:15:01 +00:00
kbob 5160d7996c efm32: Split efm32/lg into /lg and /common.
Somewhat replaces some earlier work done by hg/lg, but much more
complete, so we kept it as is, because it's bringing in even more parts
after this.

Reviewed-by: Karl Palsson <karlp@tweak.net.au>
2018-04-27 21:36:42 +00:00
Karl Palsson 54b117c5a5 usb: Use enumerated return codes
The enum usbd_request_return_codes has been available for some time.  It
should be used internally, not just by users of this code.
2018-03-02 22:42:05 +00:00
Sebastian Holzapfel 64a6f362b8 usb-msc: fix write acknowledgement bug
Fixes https://github.com/libopencm3/libopencm3/issues/409
2018-03-02 22:42:05 +00:00
Sebastian Holzapfel f871e539ed usb-standard: fix alignment assumption for Cortex-M0 platforms 2018-03-02 22:42:05 +00:00
Sebastian Holzapfel 995d19ebfd efm32hg: usb: add usb support 2018-03-02 22:42:05 +00:00
Sebastian Holzapfel a0669421e1 usb-dwc: handle any-aligned buffers
This is required to support armv6m targets using this code.
2018-03-02 22:42:05 +00:00
Sebastian Holzapfel a2ee90fbfe usb: stm32fx07 -> usb_dwc_common
The stm32fx07 is common DesignWare IP, used in both STM32 and EFM32 chips.
Rename the files to make this more clear, and easier to use in other
targets.
2018-03-02 22:42:05 +00:00
Vegard Storheil Eriksen 58f2ee34fa usb: Ensure control events are handled in order
Use EP0 OUT flow control to NAK OUT packets when we're not yet expecting
any. This prevents the status OUT event from arriving while the control
state machine is still expecting the data IN completion event.
2017-06-08 23:01:45 +00:00
Jochen Hoenicke 8b99b56c59 stmfx07: usb: Clean up FIFO read/write macro usage.
Use REBASE(OTG_FIFO(endpoint)) to access the FIFO.
For the receive FIFO do not use the endpoint. There
is only one receive FIFO so giving the endpoint is
a no-op.
Get rid of REBASE_FIFO macro.
2017-03-30 21:48:07 +00:00
Jochen Hoenicke 0c1ff686ad stmfx07: usb: Enable ctrl_out only after done rcvd.
This fixes the problem where the first four bytes were skipped in
the fifo.
2017-03-30 21:48:07 +00:00
Jochen Hoenicke 94f92d54d6 stmfx07: usb: keep better track of rxbcnt
When reading a portion of the packet that is not divisible by 4 and
not equal to rxbcnt the count could get off, since 4 bytes are read
from the fifo in the last step but rxbcnt was only updated by the
number of bytes the caller requested.

We fix this by always subtracting four bytes (the number of bytes
read from the fifo) when we read a word from the fifo.  Care has
to be taken in the last step so that rxbcnt doesn't underflow (it
is an unsigned number).

Note that reading in several small chunks not divisible by 4 doesn't
work as the extra bytes read in the last step are always discarded.
2017-03-30 21:48:07 +00:00
Jochen Hoenicke a4f1568b7d stmfx07: usb: flush txfifo if not empty after SETUP
After a SETUP packet on a control endpoint the transmit FIFO
should usually be empty.  If something bad happened, e.g. PC
and device got out of sync, we want to clear the fifo.
This should fix #668.
2017-03-30 21:48:07 +00:00
Jochen Hoenicke 973efabddb stmfx07: usb: poll DIEPINT before RXFLVL
This is to interrupt for setup sequences on IN packet before
checking for OUT packet received.  This fixes the problem that
usb_control_out stalls because we are not yet in STATUS_OUT phase.
Related to #668.
2017-03-30 21:48:07 +00:00
David Sidrane cf80e2bd5e stm32f4: USB support for newer OTG cores
Support for the  conflicting bit definitions for vbus sensing on core id
version 0x2000+

Reviewed-by: Karl Palsson <karlp@tweak.net.au>
2017-01-10 22:32:20 +00:00
Karl Palsson 825b51a6a8 usb: dwc_otg: fix missing includes
Went awol merging something :(
(Yes, dwc_otg is theoretically shared with efm and co, but at present
it's still pretty much the stm32 version)

Fixes: 422d708384
2016-11-21 17:27:43 +00:00
Karl Palsson 422d708384 usb: dwc_otg: enable clocks automatically.
ST usbfs-v1, v2, EFM32 USB, lm4f usb all automatically enable the clock
in their _init routine.  Do the same for the dwc_otg drivers to be
consistent.
2016-11-21 17:17:32 +00:00
Anatol Pomozov ae41782e1a Fix misspellings using codespell tool 2016-03-08 08:52:54 -08:00
svo d7f09d1178 usb: Return configuration = 0 in Addressed state.
This is as per USB 2.0 chapter 9 specifications, and
enables passing of USB-IF Chapter 9 tests.
2016-01-27 00:13:05 +00:00
Piotr Esden-Tempski b1049f9a6f [Style] Stylefix sweep over the whole codebase. 2015-12-14 22:57:15 +01:00
Kuldeep Singh Dhaka 77354cb371 Inital support for EFM32LG 2015-12-13 19:56:19 +01:00
fenugrec c899273c62 usb: Improved comments of USB API
usbd.h: more comments for bmRequestType and bmAttributes bitfield macros
usbd.h: migrated and added API doxyblocks from implementations (*.c)
2015-10-14 20:48:26 +00:00
Karl Palsson 3ed12b6fd9 usb: short control IN might need a ZLP
Control transfers can transfer less than was requested by the host in the
wLength field.  if this short transfer is a multiple of the endpoint's packet
size, a zero length packet must be sent.

Adds tests for a range of control transfer IN requests, and properly supports
this in the core.  Based heavily on work by Kuldeep Dhaka.

See https://github.com/libopencm3/libopencm3/pull/505
and https://github.com/libopencm3/libopencm3/pull/194 for original discussion.

Tested with stm32f4, stm32f103 and stm32l053.
2015-10-11 18:43:11 +00:00
Karl Palsson e121243ce2 usb: extract ST USB FS peripheral core. [BREAKING CHANGE]
The breaking changes here changes in header location, and changes in driver
name passed down to the usb stack.

Changes affect: stm32f102/f103, stm32l1, and some f3 parts

* instead of the confusingly generic "usb" use the name "st_usbfs" for the USB
  Full speed peripheral ST provides in a variety of their stm32 products.
  Include directives should change as:
      #include <libopencm3/stm32/usb.h> => <libopencm3/stm32/st_usbfs.h>

* instead of the confusingly specific "f103" name for the driver, use
  "st_usbfs_v1"  [BREAKING_CHANGE]

  Instead of:
    usbd_init(&stm32f103_usb_driver, .....) ==>
    usbd_init(&st_usbfs_v1_usb_driver, .....) ==>

The purpose of these changes is to reduce some confusion around naming, but
primarily to prepare for the "v2" peripheral available on stm32f0/l0 and some
f3 devices.

Work by Frantisek Burian, Kuldeep Singh Dhaka, Robin Kreis, fenugrec and zyp
on irc, and all those forgotten.
2015-10-03 01:32:34 +00:00
Karl Palsson f49cbee683 usb: otg-dev: disable and flush endpoints on reset
Only resetting the fifo memory pointers can result in corrupt data.

Tested on f4 disco board with the gadget0 test suite.
2015-09-22 22:28:02 +00:00
Karl Palsson 53c1d2c8d9 usb: get_configuration should return bConfigurationValue
Rather than the index of the config. This happens to be the same
thing if you have zero based sequential configurations.
2015-09-22 22:28:02 +00:00
Kuldeep Singh Dhaka 7cd7212577 usb: Validate and accept set_configuration properly
Setting the same configuration again should act as a lightweight reset, not be
ignored.  This resulted in data toggle bits not being reset and alternet
settings not being reset.

Further, completely invalid configurations were accepted, when they should have
result in a stall. (Section 9.4.7 of USB 2.0 spec)

fixes Github issue #302

Tested-by: Karl Palsson <karlp@tweak.net.au>
2015-09-22 22:28:02 +00:00
Kuldeep Singh Dhaka e7eb61fe33 usb: Move ti usb_lm4f.c from lm4f/ to usb/
Management of usb code is easier if everything is at one place
2015-08-17 19:07:30 +00:00
Amir Hammad 6357630a90 stm32/usb: otg_fs and otg_hs register definitions
* USB host register definitions added.
* Extracted common register and bitfield definitions
	from 'otg_fs.h' and 'otg_hs.h'
	into new file 'otg_common.h'.
	Modified usb low-level drivers to adopt to new style of bitfields.
* Fixed typo OTG_GOTGIN -> OTG_GOTGINT (according to the datasheet)

Signed-off-by: Amir Hammad <amir.hammad@hotmail.com>
2015-08-16 22:13:53 +00:00
Kuldeep Singh Dhaka afefa6888c stm32: usb: Rename USB_DADDR_ENABLE flag per ref manual
Use the standard REG_BIT name from the reference manuals, as specified
in HACKING  All the other bits have the correct naming.
2015-07-29 21:22:32 +00:00
Kuldeep Singh Dhaka 4b892724cf usb: provide typedefs for all the function callbacks.
This makes it easier to read for most people, and makes it substantially
easier to review changes in the function signatures themselves at a
later date.
2015-07-29 13:52:00 +00:00
Karl Palsson e11b7d5d6d usb: fx07: correctly REBASE for both usb cores.
Regression from 0cc0134f21

When operating on registers in code that is common for both usb cores,
make sure to use the REBASE macros to operate on the correct peripheral.

Reported by: kuldeep
Fixes github issue: #495
2015-07-29 12:46:12 +00:00
Karl Palsson bc212bba45 usb: correctly document return type.
The three existing usb drivers have no possible path that doesn't return
the object here, so I've left that comment, but it is plausible that
future drivers might have some reason that allows failing to init.  We
should strive to avoid that though.

Fixes github issue: #494
2015-07-29 12:37:40 +00:00
Karl Palsson e6e4f1388d usb_f103: fix typo in bad merge for SOF callback.
too many pulls :|
2015-07-15 12:07:45 +00:00
Frantisek Burian 0cc0134f21 usb: Only enable the SOF interrupt if user_callback_sof is given.
Otherwise we get 1 kHz of SOF interrupts even if we don't need them.

Originally reported by Uwe in https://github.com/libopencm3/libopencm3/pull/274
but BuFran provided a reworked implementation.
2015-07-15 00:22:18 +00:00
Frantisek Burian 13004f81ee usb_f103: Clear ISR bit as soon as possible, as for the other IRQs.
Reported by Uwe in https://github.com/libopencm3/libopencm3/pull/274 but
expanded and provided by BuFran finally.
2015-07-15 00:18:57 +00:00
Kuldeep Singh Dhaka 7dbb93c784 usb: fetch endpoint 0 max size from descriptor
Instead of hardcoding.

Fixes #303

Signed-off-by: Karl Palsson <karlp@tweak.net.au>
2015-03-30 18:41:27 +00:00
Andrey Smirnov db58d5ae82 Add provisions to support multiple altsettings
This commit implements the support for one interface to have multiple
altsettings. It also adds hook that user can use to perform actions
when the alsetting switch is performed by host.

Changes:
* For backward compatibility, placed a pointer instead of allocating memory for whole interface struct.
* Always execute callback (even if the current interface alternate-settings matches).
* Multiple configuration support.

Signed-off-by: Kuldeep Singh Dhaka <kuldeepdhaka9@gmail.com>
2015-03-30 00:07:32 +00:00
Felix Ruess 922d8f7079 [usb] fix user_callback_set_config
USB using control callbacks was broken since d6bad27735
Properly check if user usb control callback are registered,
while here make sure user_callback_set_config callback pointers are initialized to NULL.
2014-12-29 17:10:04 +01:00
Daniel Thompson d3aa579e0a usb: Support for class specific endpoint descriptors.
Extends struct usb_endpoint_descriptor to make it possible to provide
the USB host with class-specific extensions to endpoint descriptors.

The approach taken, based on extra and extralen and removing the sizeof()
from USB_DT_ENDPOINT_SIZE, is identical to the approach used to add
class-specific extensions to interface descriptors. All libopencm3-examples
use the USB_DT_ENDPOINT_SIZE (rather than directly using sizeof) so there
should be no compatibility problems resulting from this change.
2014-12-18 22:50:27 +00:00
Daniel Thompson 0b63408260 usb: Prevent memcpy() being called with NULL arguments
If there is no additional iface data then iface->extra is NULL and
iface->extralen is zero. Passing NULL to memcpy is undefined behaviour
even if the length of data to copy is zero. In other words a conforming
(debug) memcpy implementation is permitted to assert(dst && src) without
checking the value of n.

Add an extra branch to avoid this.
2014-12-18 22:50:27 +00:00
Franck Jullien d6bad27735 Allow more than one usbd_register_set_config_callback
In a composite device if one want to separate code
for each interface, usbd_register_set_config_callback
can now register more than one callback.

Signed-off-by: Franck Jullien <franck.jullien@gmail.com>
2014-07-14 14:04:58 +02:00
Pavol Rusnak 806ebb18fa add MSC (Mass Storage Class) support 2014-07-03 18:46:55 +02:00
Karl Palsson a277abe2c1 [usb-f103] Use the "modern" /rcc include instead of f1.
The f103 usb core should be usable on the l1 as well, and the /f1/rcc
isn't explicitly required anymore.  This is an important change to be
able to share the driver.
2013-09-05 17:35:07 +00:00
Alexandru Gagniuc 0819a49411 usb_control: Mark the control pipe as IDLE after a STALL
After a STALL handshake is transmitted, a control pipe becomes idle. Not
marking the pipe as idle did not affect the STM32 family. Since it
distinguishes between OUT and SETUP tokens, it calls the setup handler
on a SETUP token, regardless of the state of the pipe.

Other families, such as LM4F do not distinguish in software between IN and
SETUP tokens, and need to decide which handler to call based on the state
of the pipe. On these chips, SETUP transactions will not be handled
properly after a transfer was STALLED, as the state machine of the pipe is
b0rked. Unb0rk it.

Signed-off-by: Alexandru Gagniuc <mr.nuke.me@gmail.com>
2013-07-07 17:04:38 -07:00