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 | static inline int usb_device_connection_initialize(
|
---|
55 | usb_device_connection_t *connection, usb_hc_connection_t *hc_connection,
|
---|
56 | usb_address_t address)
|
---|
57 | {
|
---|
58 | assert(connection);
|
---|
59 | if (hc_connection == NULL)
|
---|
60 | return EBADMEM;
|
---|
61 | if ((address < 0) || (address >= USB11_ADDRESS_MAX))
|
---|
62 | return EINVAL;
|
---|
63 |
|
---|
64 | connection->hc_connection = hc_connection;
|
---|
65 | connection->address = address;
|
---|
66 | return EOK;
|
---|
67 | }
|
---|
68 | /*----------------------------------------------------------------------------*/
|
---|
69 | static inline int usb_device_register_endpoint(usb_device_connection_t *conn,
|
---|
70 | usb_endpoint_t ep, usb_transfer_type_t type, usb_direction_t direction,
|
---|
71 | size_t packet_size, unsigned interval)
|
---|
72 | {
|
---|
73 | assert(conn);
|
---|
74 | return usb_hc_register_endpoint(conn->hc_connection,
|
---|
75 | conn->address, ep, type, direction, packet_size, interval);
|
---|
76 | }
|
---|
77 | /*----------------------------------------------------------------------------*/
|
---|
78 | static inline int usb_device_unregister_endpoint(usb_device_connection_t *conn,
|
---|
79 | usb_endpoint_t ep, usb_direction_t direction)
|
---|
80 | {
|
---|
81 | assert(conn);
|
---|
82 | return usb_hc_unregister_endpoint(conn->hc_connection,
|
---|
83 | conn->address, ep, direction);
|
---|
84 | }
|
---|
85 | /*----------------------------------------------------------------------------*/
|
---|
86 | static inline int usb_device_control_read(usb_device_connection_t *conn,
|
---|
87 | usb_endpoint_t ep, uint64_t setup, void *data, size_t size, size_t *rsize)
|
---|
88 | {
|
---|
89 | assert(conn);
|
---|
90 | return usb_hc_read(conn->hc_connection,
|
---|
91 | conn->address, ep, setup, data, size, rsize);
|
---|
92 | }
|
---|
93 | /*----------------------------------------------------------------------------*/
|
---|
94 | static inline int usb_device_control_write(usb_device_connection_t *conn,
|
---|
95 | usb_endpoint_t ep, uint64_t setup, const void *data, size_t size)
|
---|
96 | {
|
---|
97 | assert(conn);
|
---|
98 | return usb_hc_write(conn->hc_connection,
|
---|
99 | conn->address, ep, setup, data, size);
|
---|
100 | }
|
---|
101 | /*----------------------------------------------------------------------------*/
|
---|
102 | /** Wrapper for read calls with no setup stage.
|
---|
103 | * @param[in] connection hc connection to use.
|
---|
104 | * @param[in] address USB device address.
|
---|
105 | * @param[in] endpoint USB device endpoint.
|
---|
106 | * @param[in] data Data buffer.
|
---|
107 | * @param[in] size Size of the buffer.
|
---|
108 | * @param[out] real_size Size of the transferred data.
|
---|
109 | * @return Error code.
|
---|
110 | */
|
---|
111 | static inline int usb_device_read(usb_device_connection_t *conn,
|
---|
112 | usb_endpoint_t ep, void *data, size_t size, size_t *real_size)
|
---|
113 | {
|
---|
114 | return usb_device_control_read(conn, ep, 0, data, size, real_size);
|
---|
115 | }
|
---|
116 | /*----------------------------------------------------------------------------*/
|
---|
117 | /** Wrapper for write calls with no setup stage.
|
---|
118 | * @param connection hc connection to use.
|
---|
119 | * @param address USB device address.
|
---|
120 | * @param endpoint USB device endpoint.
|
---|
121 | * @param data Data buffer.
|
---|
122 | * @param size Size of the buffer.
|
---|
123 | * @return Error code.
|
---|
124 | */
|
---|
125 | static inline int usb_device_write(usb_device_connection_t *conn,
|
---|
126 | usb_endpoint_t ep, const void *data, size_t size)
|
---|
127 | {
|
---|
128 | return usb_device_control_write(conn, ep, 0, data, size);
|
---|
129 | }
|
---|
130 | #endif
|
---|
131 | /**
|
---|
132 | * @}
|
---|
133 | */
|
---|