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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cede6f8 was ae3a941, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usb: cstyle

  • Property mode set to 100644
File size: 4.5 KB
Line 
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. */
50typedef 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 /**
63 * Data buffer of at least `request_size`. User is responsible for its
64 * allocation.
65 */
66 uint8_t *buffer;
67
68 /** Callback when data arrives.
69 *
70 * @param dev Device that was polled.
71 * @param data Data buffer (in USB endianness).
72 * @param data_size Size of the @p data buffer in bytes.
73 * @param arg Custom argument.
74 * @return Whether to continue in polling.
75 */
76 bool (*on_data)(usb_device_t *dev, uint8_t *data, size_t data_size,
77 void *arg);
78
79
80 /**
81 * Optional parameters - user can customize them, but they are
82 * defaulted to some reasonable values.
83 */
84
85 /** Level of debugging messages from auto polling.
86 * 0 - nothing (default)
87 * 1 - inform about errors and polling start/end
88 * 2 - also dump every retrieved buffer
89 */
90 int debug;
91
92 /**
93 * Maximum number of consecutive errors before polling termination
94 * (default 3).
95 */
96 size_t max_failures;
97
98 /** Delay between poll requests in milliseconds.
99 * By default, value from endpoint descriptor used.
100 */
101 int delay;
102
103 /** Whether to automatically try to clear the HALT feature after
104 * the endpoint stalls (true by default).
105 */
106 bool auto_clear_halt;
107
108 /** Argument to pass to callbacks (default NULL). */
109 void *arg;
110
111 /** Callback when polling is terminated.
112 *
113 * @param dev Device where the polling was terminated.
114 * @param due_to_errors Whether polling stopped due to several failures.
115 * @param arg Custom argument.
116 */
117 void (*on_polling_end)(usb_device_t *dev, bool due_to_errors,
118 void *arg);
119
120 /** Callback when error occurs.
121 *
122 * @param dev Device where error occurred.
123 * @param err_code Error code (as returned from usb_pipe_read).
124 * @param arg Custom argument.
125 * @return Whether to continue in polling.
126 */
127 bool (*on_error)(usb_device_t *dev, errno_t err_code, void *arg);
128
129
130 /**
131 * Internal parameters - user is not expected to set them. Messing with
132 * them can result in unexpected behavior if you do not know what you
133 * are doing.
134 */
135
136 /** Fibril used for polling. */
137 fid_t fibril;
138
139 /** True if polling is currently in operation. */
140 volatile bool running;
141
142 /** True if polling should terminate as soon as possible. */
143 volatile bool joining;
144
145 /** Synchronization primitives for joining polling end. */
146 fibril_mutex_t guard;
147 fibril_condvar_t cv;
148} usb_polling_t;
149
150errno_t usb_polling_init(usb_polling_t *);
151void usb_polling_fini(usb_polling_t *);
152
153errno_t usb_polling_start(usb_polling_t *);
154errno_t usb_polling_join(usb_polling_t *);
155
156#endif
157/**
158 * @}
159 */
Note: See TracBrowser for help on using the repository browser.