[5e07e2b5] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
[338729c] | 3 | * Copyright (c) 2017 Petr Manek
|
---|
[5e07e2b5] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[160b75e] | 30 | /** @addtogroup libusbdev
|
---|
[5e07e2b5] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | * USB device polling functions.
|
---|
| 35 | */
|
---|
[7d521e24] | 36 | #ifndef LIBUSBDEV_POLL_H_
|
---|
| 37 | #define LIBUSBDEV_POLL_H_
|
---|
[5e07e2b5] | 38 |
|
---|
[c01987c] | 39 | #include <usb/usb.h>
|
---|
| 40 | #include <usb/dev/device.h>
|
---|
| 41 | #include <usb/dev/pipes.h>
|
---|
| 42 |
|
---|
| 43 | #include <stdbool.h>
|
---|
[8d2dd7f2] | 44 | #include <stddef.h>
|
---|
| 45 | #include <stdint.h>
|
---|
[8b71f3e] | 46 | #include <fibril_synch.h>
|
---|
[8989f48f] | 47 |
|
---|
[8b71f3e] | 48 | /** USB automated polling. */
|
---|
| 49 | typedef struct usb_polling {
|
---|
| 50 | /** Mandatory parameters - user is expected to configure these. */
|
---|
| 51 |
|
---|
| 52 | /** USB device to poll. */
|
---|
| 53 | usb_device_t *device;
|
---|
| 54 |
|
---|
| 55 | /** Device enpoint mapping to use for polling. */
|
---|
| 56 | usb_endpoint_mapping_t *ep_mapping;
|
---|
| 57 |
|
---|
| 58 | /** Size of the recieved data. */
|
---|
| 59 | size_t request_size;
|
---|
| 60 |
|
---|
[ae3a941] | 61 | /**
|
---|
| 62 | * Data buffer of at least `request_size`. User is responsible for its
|
---|
| 63 | * allocation.
|
---|
| 64 | */
|
---|
[8b71f3e] | 65 | uint8_t *buffer;
|
---|
| 66 |
|
---|
| 67 | /** Callback when data arrives.
|
---|
| 68 | *
|
---|
| 69 | * @param dev Device that was polled.
|
---|
| 70 | * @param data Data buffer (in USB endianness).
|
---|
| 71 | * @param data_size Size of the @p data buffer in bytes.
|
---|
| 72 | * @param arg Custom argument.
|
---|
| 73 | * @return Whether to continue in polling.
|
---|
| 74 | */
|
---|
| 75 | bool (*on_data)(usb_device_t *dev, uint8_t *data, size_t data_size,
|
---|
| 76 | void *arg);
|
---|
| 77 |
|
---|
[ae3a941] | 78 | /**
|
---|
| 79 | * Optional parameters - user can customize them, but they are
|
---|
| 80 | * defaulted to some reasonable values.
|
---|
[8b71f3e] | 81 | */
|
---|
| 82 |
|
---|
[5e168be1] | 83 | /** Level of debugging messages from auto polling.
|
---|
[8b71f3e] | 84 | * 0 - nothing (default)
|
---|
[5e168be1] | 85 | * 1 - inform about errors and polling start/end
|
---|
| 86 | * 2 - also dump every retrieved buffer
|
---|
| 87 | */
|
---|
| 88 | int debug;
|
---|
[71f211f] | 89 |
|
---|
[ae3a941] | 90 | /**
|
---|
| 91 | * Maximum number of consecutive errors before polling termination
|
---|
| 92 | * (default 3).
|
---|
| 93 | */
|
---|
[8989f48f] | 94 | size_t max_failures;
|
---|
[71f211f] | 95 |
|
---|
[8989f48f] | 96 | /** Delay between poll requests in milliseconds.
|
---|
[8b71f3e] | 97 | * By default, value from endpoint descriptor used.
|
---|
[8989f48f] | 98 | */
|
---|
| 99 | int delay;
|
---|
[71f211f] | 100 |
|
---|
[8989f48f] | 101 | /** Whether to automatically try to clear the HALT feature after
|
---|
[8b71f3e] | 102 | * the endpoint stalls (true by default).
|
---|
[8989f48f] | 103 | */
|
---|
| 104 | bool auto_clear_halt;
|
---|
[71f211f] | 105 |
|
---|
[8b71f3e] | 106 | /** Argument to pass to callbacks (default NULL). */
|
---|
| 107 | void *arg;
|
---|
[71f211f] | 108 |
|
---|
[8989f48f] | 109 | /** Callback when polling is terminated.
|
---|
| 110 | *
|
---|
| 111 | * @param dev Device where the polling was terminated.
|
---|
| 112 | * @param due_to_errors Whether polling stopped due to several failures.
|
---|
| 113 | * @param arg Custom argument.
|
---|
| 114 | */
|
---|
| 115 | void (*on_polling_end)(usb_device_t *dev, bool due_to_errors,
|
---|
| 116 | void *arg);
|
---|
[71f211f] | 117 |
|
---|
[8989f48f] | 118 | /** Callback when error occurs.
|
---|
| 119 | *
|
---|
| 120 | * @param dev Device where error occurred.
|
---|
| 121 | * @param err_code Error code (as returned from usb_pipe_read).
|
---|
| 122 | * @param arg Custom argument.
|
---|
| 123 | * @return Whether to continue in polling.
|
---|
| 124 | */
|
---|
[5a6cc679] | 125 | bool (*on_error)(usb_device_t *dev, errno_t err_code, void *arg);
|
---|
[71f211f] | 126 |
|
---|
[ae3a941] | 127 | /**
|
---|
| 128 | * Internal parameters - user is not expected to set them. Messing with
|
---|
| 129 | * them can result in unexpected behavior if you do not know what you
|
---|
| 130 | * are doing.
|
---|
[8b71f3e] | 131 | */
|
---|
| 132 |
|
---|
| 133 | /** Fibril used for polling. */
|
---|
| 134 | fid_t fibril;
|
---|
| 135 |
|
---|
| 136 | /** True if polling is currently in operation. */
|
---|
| 137 | volatile bool running;
|
---|
| 138 |
|
---|
| 139 | /** True if polling should terminate as soon as possible. */
|
---|
| 140 | volatile bool joining;
|
---|
| 141 |
|
---|
| 142 | /** Synchronization primitives for joining polling end. */
|
---|
| 143 | fibril_mutex_t guard;
|
---|
| 144 | fibril_condvar_t cv;
|
---|
| 145 | } usb_polling_t;
|
---|
| 146 |
|
---|
[5a6cc679] | 147 | errno_t usb_polling_init(usb_polling_t *);
|
---|
[8b71f3e] | 148 | void usb_polling_fini(usb_polling_t *);
|
---|
[bb70637] | 149 |
|
---|
[5a6cc679] | 150 | errno_t usb_polling_start(usb_polling_t *);
|
---|
| 151 | errno_t usb_polling_join(usb_polling_t *);
|
---|
[8a0c52a] | 152 |
|
---|
[5e07e2b5] | 153 | #endif
|
---|
| 154 | /**
|
---|
| 155 | * @}
|
---|
| 156 | */
|
---|