source: mainline/uspace/lib/usbdev/include/usb/dev/usb_device_connection.h@ c804484

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c804484 was 1561e8b, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

libusbdev: Use provided usb_device_connection_t wrappers.

  • Property mode set to 100644
File size: 5.4 KB
Line 
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 */
47typedef 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
54static 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
60 if (hc_connection == NULL) {
61 return EBADMEM;
62 }
63
64 if ((address < 0) || (address >= USB11_ADDRESS_MAX)) {
65 return EINVAL;
66 }
67
68 connection->hc_connection = hc_connection;
69 connection->address = address;
70 return EOK;
71}
72/*----------------------------------------------------------------------------*/
73/** Initialize connection to USB device on default address.
74 *
75 * @param dev_connection Device connection structure to be initialized.
76 * @param hc_connection Initialized connection to host controller.
77 * @return Error code.
78 */
79static inline int usb_device_connection_initialize_on_default_address(
80 usb_device_connection_t *connection, usb_hc_connection_t *hc_conn)
81{
82 return usb_device_connection_initialize(connection, hc_conn, 0);
83}
84
85/*----------------------------------------------------------------------------*/
86static inline int usb_device_register_endpoint(usb_device_connection_t *conn,
87 usb_endpoint_t ep, usb_transfer_type_t type, usb_direction_t direction,
88 size_t packet_size, unsigned interval)
89{
90 assert(conn);
91 return usb_hc_register_endpoint(conn->hc_connection,
92 conn->address, ep, type, direction, packet_size, interval);
93}
94/*----------------------------------------------------------------------------*/
95static inline int usb_device_unregister_endpoint(usb_device_connection_t *conn,
96 usb_endpoint_t ep, usb_direction_t direction)
97{
98 assert(conn);
99 return usb_hc_unregister_endpoint(conn->hc_connection,
100 conn->address, ep, direction);
101}
102/*----------------------------------------------------------------------------*/
103static inline int usb_device_control_read(usb_device_connection_t *conn,
104 usb_endpoint_t ep, uint64_t setup, void *data, size_t size, size_t *rsize)
105{
106 assert(conn);
107 return usb_hc_read(conn->hc_connection,
108 conn->address, ep, setup, data, size, rsize);
109}
110/*----------------------------------------------------------------------------*/
111static inline int usb_device_control_write(usb_device_connection_t *conn,
112 usb_endpoint_t ep, uint64_t setup, const void *data, size_t size)
113{
114 assert(conn);
115 return usb_hc_write(conn->hc_connection,
116 conn->address, ep, setup, data, size);
117}
118/*----------------------------------------------------------------------------*/
119/** Wrapper for read calls with no setup stage.
120 * @param[in] connection hc connection to use.
121 * @param[in] address USB device address.
122 * @param[in] endpoint USB device endpoint.
123 * @param[in] data Data buffer.
124 * @param[in] size Size of the buffer.
125 * @param[out] real_size Size of the transferred data.
126 * @return Error code.
127 */
128static inline int usb_device_read(usb_device_connection_t *conn,
129 usb_endpoint_t ep, void *data, size_t size, size_t *real_size)
130{
131 return usb_device_control_read(conn, ep, 0, data, size, real_size);
132}
133/*----------------------------------------------------------------------------*/
134/** Wrapper for write calls with no setup stage.
135 * @param connection hc connection to use.
136 * @param address USB device address.
137 * @param endpoint USB device endpoint.
138 * @param data Data buffer.
139 * @param size Size of the buffer.
140 * @return Error code.
141 */
142static inline int usb_device_write(usb_device_connection_t *conn,
143 usb_endpoint_t ep, const void *data, size_t size)
144{
145 return usb_device_control_write(conn, ep, 0, data, size);
146}
147#endif
148/**
149 * @}
150 */
Note: See TracBrowser for help on using the repository browser.