source: mainline/uspace/lib/usbdev/include/usb/dev/poll.h@ 1d218bf

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1d218bf was 8b71f3e, checked in by Petr Manek <petr.manek@…>, 8 years ago

usbdev: refactor polling more

For clarity, the opaque usb_device_polling_t and its complementary
configuration data structure usb_device_polling_config_t have been
merged into usb_polling_t. All related methods have dropped the
"device" from their prefix as well.

The usage semantics have transitioned to malloc-free model, where the
user is entirely responsible for (de)allocation of the polling structure
and its data buffer, and (de)initialization happens in designated
functions during its lifetime in the system.

In addition, the distinction between mandatory / optional / internal
parameters has been documented. Optional parameters now have default
values, which are set to sensible constants in order to allow dropping
some lines in USB driver implementations.

The drivers usbhid and usbhub were refactored to match the API
changes.

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