| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2011 Vojtech Horky
|
|---|
| 3 | * SPDX-FileCopyrightText: 2017 Petr Manek
|
|---|
| 4 | *
|
|---|
| 5 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | /** @addtogroup libusbdev
|
|---|
| 9 | * @{
|
|---|
| 10 | */
|
|---|
| 11 | /** @file
|
|---|
| 12 | * USB device polling functions.
|
|---|
| 13 | */
|
|---|
| 14 | #ifndef LIBUSBDEV_POLL_H_
|
|---|
| 15 | #define LIBUSBDEV_POLL_H_
|
|---|
| 16 |
|
|---|
| 17 | #include <usb/usb.h>
|
|---|
| 18 | #include <usb/dev/device.h>
|
|---|
| 19 | #include <usb/dev/pipes.h>
|
|---|
| 20 |
|
|---|
| 21 | #include <stdbool.h>
|
|---|
| 22 | #include <stddef.h>
|
|---|
| 23 | #include <stdint.h>
|
|---|
| 24 | #include <fibril_synch.h>
|
|---|
| 25 |
|
|---|
| 26 | /** USB automated polling. */
|
|---|
| 27 | typedef struct usb_polling {
|
|---|
| 28 | /** Mandatory parameters - user is expected to configure these. */
|
|---|
| 29 |
|
|---|
| 30 | /** USB device to poll. */
|
|---|
| 31 | usb_device_t *device;
|
|---|
| 32 |
|
|---|
| 33 | /** Device enpoint mapping to use for polling. */
|
|---|
| 34 | usb_endpoint_mapping_t *ep_mapping;
|
|---|
| 35 |
|
|---|
| 36 | /** Size of the recieved data. */
|
|---|
| 37 | size_t request_size;
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Data buffer of at least `request_size`. User is responsible for its
|
|---|
| 41 | * allocation.
|
|---|
| 42 | */
|
|---|
| 43 | uint8_t *buffer;
|
|---|
| 44 |
|
|---|
| 45 | /** Callback when data arrives.
|
|---|
| 46 | *
|
|---|
| 47 | * @param dev Device that was polled.
|
|---|
| 48 | * @param data Data buffer (in USB endianness).
|
|---|
| 49 | * @param data_size Size of the @p data buffer in bytes.
|
|---|
| 50 | * @param arg Custom argument.
|
|---|
| 51 | * @return Whether to continue in polling.
|
|---|
| 52 | */
|
|---|
| 53 | bool (*on_data)(usb_device_t *dev, uint8_t *data, size_t data_size,
|
|---|
| 54 | void *arg);
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Optional parameters - user can customize them, but they are
|
|---|
| 58 | * defaulted to some reasonable values.
|
|---|
| 59 | */
|
|---|
| 60 |
|
|---|
| 61 | /** Level of debugging messages from auto polling.
|
|---|
| 62 | * 0 - nothing (default)
|
|---|
| 63 | * 1 - inform about errors and polling start/end
|
|---|
| 64 | * 2 - also dump every retrieved buffer
|
|---|
| 65 | */
|
|---|
| 66 | int debug;
|
|---|
| 67 |
|
|---|
| 68 | /**
|
|---|
| 69 | * Maximum number of consecutive errors before polling termination
|
|---|
| 70 | * (default 3).
|
|---|
| 71 | */
|
|---|
| 72 | size_t max_failures;
|
|---|
| 73 |
|
|---|
| 74 | /** Delay between poll requests in milliseconds.
|
|---|
| 75 | * By default, value from endpoint descriptor used.
|
|---|
| 76 | */
|
|---|
| 77 | int delay;
|
|---|
| 78 |
|
|---|
| 79 | /** Whether to automatically try to clear the HALT feature after
|
|---|
| 80 | * the endpoint stalls (true by default).
|
|---|
| 81 | */
|
|---|
| 82 | bool auto_clear_halt;
|
|---|
| 83 |
|
|---|
| 84 | /** Argument to pass to callbacks (default NULL). */
|
|---|
| 85 | void *arg;
|
|---|
| 86 |
|
|---|
| 87 | /** Callback when polling is terminated.
|
|---|
| 88 | *
|
|---|
| 89 | * @param dev Device where the polling was terminated.
|
|---|
| 90 | * @param due_to_errors Whether polling stopped due to several failures.
|
|---|
| 91 | * @param arg Custom argument.
|
|---|
| 92 | */
|
|---|
| 93 | void (*on_polling_end)(usb_device_t *dev, bool due_to_errors,
|
|---|
| 94 | void *arg);
|
|---|
| 95 |
|
|---|
| 96 | /** Callback when error occurs.
|
|---|
| 97 | *
|
|---|
| 98 | * @param dev Device where error occurred.
|
|---|
| 99 | * @param err_code Error code (as returned from usb_pipe_read).
|
|---|
| 100 | * @param arg Custom argument.
|
|---|
| 101 | * @return Whether to continue in polling.
|
|---|
| 102 | */
|
|---|
| 103 | bool (*on_error)(usb_device_t *dev, errno_t err_code, void *arg);
|
|---|
| 104 |
|
|---|
| 105 | /**
|
|---|
| 106 | * Internal parameters - user is not expected to set them. Messing with
|
|---|
| 107 | * them can result in unexpected behavior if you do not know what you
|
|---|
| 108 | * are doing.
|
|---|
| 109 | */
|
|---|
| 110 |
|
|---|
| 111 | /** Fibril used for polling. */
|
|---|
| 112 | fid_t fibril;
|
|---|
| 113 |
|
|---|
| 114 | /** True if polling is currently in operation. */
|
|---|
| 115 | volatile bool running;
|
|---|
| 116 |
|
|---|
| 117 | /** True if polling should terminate as soon as possible. */
|
|---|
| 118 | volatile bool joining;
|
|---|
| 119 |
|
|---|
| 120 | /** Synchronization primitives for joining polling end. */
|
|---|
| 121 | fibril_mutex_t guard;
|
|---|
| 122 | fibril_condvar_t cv;
|
|---|
| 123 | } usb_polling_t;
|
|---|
| 124 |
|
|---|
| 125 | errno_t usb_polling_init(usb_polling_t *);
|
|---|
| 126 | void usb_polling_fini(usb_polling_t *);
|
|---|
| 127 |
|
|---|
| 128 | errno_t usb_polling_start(usb_polling_t *);
|
|---|
| 129 | errno_t usb_polling_join(usb_polling_t *);
|
|---|
| 130 |
|
|---|
| 131 | #endif
|
|---|
| 132 | /**
|
|---|
| 133 | * @}
|
|---|
| 134 | */
|
|---|