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 | /** 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 |
|
---|
61 | /**
|
---|
62 | * Data buffer of at least `request_size`. User is responsible for its
|
---|
63 | * allocation.
|
---|
64 | */
|
---|
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 |
|
---|
78 | /**
|
---|
79 | * Optional parameters - user can customize them, but they are
|
---|
80 | * defaulted to some reasonable values.
|
---|
81 | */
|
---|
82 |
|
---|
83 | /** Level of debugging messages from auto polling.
|
---|
84 | * 0 - nothing (default)
|
---|
85 | * 1 - inform about errors and polling start/end
|
---|
86 | * 2 - also dump every retrieved buffer
|
---|
87 | */
|
---|
88 | int debug;
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Maximum number of consecutive errors before polling termination
|
---|
92 | * (default 3).
|
---|
93 | */
|
---|
94 | size_t max_failures;
|
---|
95 |
|
---|
96 | /** Delay between poll requests in milliseconds.
|
---|
97 | * By default, value from endpoint descriptor used.
|
---|
98 | */
|
---|
99 | int delay;
|
---|
100 |
|
---|
101 | /** Whether to automatically try to clear the HALT feature after
|
---|
102 | * the endpoint stalls (true by default).
|
---|
103 | */
|
---|
104 | bool auto_clear_halt;
|
---|
105 |
|
---|
106 | /** Argument to pass to callbacks (default NULL). */
|
---|
107 | void *arg;
|
---|
108 |
|
---|
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);
|
---|
117 |
|
---|
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 | */
|
---|
125 | bool (*on_error)(usb_device_t *dev, errno_t err_code, void *arg);
|
---|
126 |
|
---|
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.
|
---|
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 |
|
---|
147 | errno_t usb_polling_init(usb_polling_t *);
|
---|
148 | void usb_polling_fini(usb_polling_t *);
|
---|
149 |
|
---|
150 | errno_t usb_polling_start(usb_polling_t *);
|
---|
151 | errno_t usb_polling_join(usb_polling_t *);
|
---|
152 |
|
---|
153 | #endif
|
---|
154 | /**
|
---|
155 | * @}
|
---|
156 | */
|
---|