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