1 | /*
|
---|
2 | * Copyright (c) 2011 Jan Vesely
|
---|
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 | /** @addtogroup libusbdev
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /** @file
|
---|
32 | * Common USB types and functions.
|
---|
33 | */
|
---|
34 | #ifndef LIBUSBDEV_DEVICE_CONNECTION_H_
|
---|
35 | #define LIBUSBDEV_DEVICE_CONNECTION_H_
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <devman.h>
|
---|
39 | #include <usb/usb.h>
|
---|
40 | #include <usb/hc.h>
|
---|
41 |
|
---|
42 |
|
---|
43 | /** Abstraction of a physical connection to the device.
|
---|
44 | * This type is an abstraction of the USB wire that connects the host and
|
---|
45 | * the function (device).
|
---|
46 | */
|
---|
47 | typedef struct {
|
---|
48 | /** Connection to the host controller device is connected to. */
|
---|
49 | usb_hc_connection_t *hc_connection;
|
---|
50 | /** Address of the device. */
|
---|
51 | usb_address_t address;
|
---|
52 | } usb_device_connection_t;
|
---|
53 |
|
---|
54 | /** Initialize device connection. Set address and hc connection.
|
---|
55 | * @param instance Structure to initialize.
|
---|
56 | * @param hc_connection. Host controller connection to use.
|
---|
57 | * @param address USB address.
|
---|
58 | * @return Error code.
|
---|
59 | */
|
---|
60 | static inline int usb_device_connection_initialize(
|
---|
61 | usb_device_connection_t *instance, usb_hc_connection_t *hc_connection,
|
---|
62 | usb_address_t address)
|
---|
63 | {
|
---|
64 | assert(instance);
|
---|
65 | if (hc_connection == NULL)
|
---|
66 | return EBADMEM;
|
---|
67 | if ((address < 0) || (address >= USB11_ADDRESS_MAX))
|
---|
68 | return EINVAL;
|
---|
69 |
|
---|
70 | instance->hc_connection = hc_connection;
|
---|
71 | instance->address = address;
|
---|
72 | return EOK;
|
---|
73 | }
|
---|
74 | /*----------------------------------------------------------------------------*/
|
---|
75 | /** Register endpoint on the device.
|
---|
76 | * @param instance device connection structure to use.
|
---|
77 | * @param ep USB endpoint number.
|
---|
78 | * @param type Communication type of the endpoint.
|
---|
79 | * @param direction Communication direction.
|
---|
80 | * @param packet_size Maximum packet size for the endpoint.
|
---|
81 | * @param interval Preferrred interval between communication.
|
---|
82 | * @return Error code.
|
---|
83 | */
|
---|
84 | static inline int usb_device_register_endpoint(
|
---|
85 | usb_device_connection_t *instance, usb_endpoint_t ep,
|
---|
86 | usb_transfer_type_t type, usb_direction_t direction,
|
---|
87 | size_t packet_size, unsigned interval)
|
---|
88 | {
|
---|
89 | assert(instance);
|
---|
90 | return usb_hc_register_endpoint(instance->hc_connection,
|
---|
91 | instance->address, ep, type, direction, packet_size, interval);
|
---|
92 | }
|
---|
93 | /*----------------------------------------------------------------------------*/
|
---|
94 | /** Unregister endpoint on the device.
|
---|
95 | * @param instance device connection structure
|
---|
96 | * @param ep Endpoint number.
|
---|
97 | * @param dir Communication direction.
|
---|
98 | * @return Error code.
|
---|
99 | */
|
---|
100 | static inline int usb_device_unregister_endpoint(
|
---|
101 | usb_device_connection_t *instance, usb_endpoint_t ep, usb_direction_t dir)
|
---|
102 | {
|
---|
103 | assert(instance);
|
---|
104 | return usb_hc_unregister_endpoint(instance->hc_connection,
|
---|
105 | instance->address, ep, dir);
|
---|
106 | }
|
---|
107 | /*----------------------------------------------------------------------------*/
|
---|
108 | /** Get data from the device.
|
---|
109 | * @param[in] instance device connection structure to use.
|
---|
110 | * @param[in] ep target endpoint's number.
|
---|
111 | * @param[in] setup Setup stage data (control transfers).
|
---|
112 | * @param[in] data data buffer.
|
---|
113 | * @param[in] size size of the data buffer.
|
---|
114 | * @param[out] rsize bytes actually copied to the buffer.
|
---|
115 | * @return Error code.
|
---|
116 | */
|
---|
117 | static inline int usb_device_control_read(usb_device_connection_t *instance,
|
---|
118 | usb_endpoint_t ep, uint64_t setup, void *data, size_t size, size_t *rsize)
|
---|
119 | {
|
---|
120 | assert(instance);
|
---|
121 | return usb_hc_read(instance->hc_connection,
|
---|
122 | instance->address, ep, setup, data, size, rsize);
|
---|
123 | }
|
---|
124 | /*----------------------------------------------------------------------------*/
|
---|
125 | /** Send data to the device.
|
---|
126 | * @param instance device connection structure to use.
|
---|
127 | * @param ep target endpoint's number.
|
---|
128 | * @param setup Setup stage data (control transfers).
|
---|
129 | * @param data data buffer.
|
---|
130 | * @param size size of the data buffer.
|
---|
131 | * @return Error code.
|
---|
132 | */
|
---|
133 | static inline int usb_device_control_write(usb_device_connection_t *instance,
|
---|
134 | usb_endpoint_t ep, uint64_t setup, const void *data, size_t size)
|
---|
135 | {
|
---|
136 | assert(instance);
|
---|
137 | return usb_hc_write(instance->hc_connection,
|
---|
138 | instance->address, ep, setup, data, size);
|
---|
139 | }
|
---|
140 | /*----------------------------------------------------------------------------*/
|
---|
141 | /** Wrapper for read calls with no setup stage.
|
---|
142 | * @param[in] instance device connection structure.
|
---|
143 | * @param[in] address USB device address.
|
---|
144 | * @param[in] endpoint USB device endpoint.
|
---|
145 | * @param[in] data Data buffer.
|
---|
146 | * @param[in] size Size of the buffer.
|
---|
147 | * @param[out] real_size Size of the transferred data.
|
---|
148 | * @return Error code.
|
---|
149 | */
|
---|
150 | static inline int usb_device_read(usb_device_connection_t *instance,
|
---|
151 | usb_endpoint_t ep, void *data, size_t size, size_t *real_size)
|
---|
152 | {
|
---|
153 | return usb_device_control_read(instance, ep, 0, data, size, real_size);
|
---|
154 | }
|
---|
155 | /*----------------------------------------------------------------------------*/
|
---|
156 | /** Wrapper for write calls with no setup stage.
|
---|
157 | * @param instance device connection structure.
|
---|
158 | * @param address USB device address.
|
---|
159 | * @param endpoint USB device endpoint.
|
---|
160 | * @param data Data buffer.
|
---|
161 | * @param size Size of the buffer.
|
---|
162 | * @return Error code.
|
---|
163 | */
|
---|
164 | static inline int usb_device_write(usb_device_connection_t *instance,
|
---|
165 | usb_endpoint_t ep, const void *data, size_t size)
|
---|
166 | {
|
---|
167 | return usb_device_control_write(instance, ep, 0, data, size);
|
---|
168 | }
|
---|
169 | #endif
|
---|
170 | /**
|
---|
171 | * @}
|
---|
172 | */
|
---|