source: mainline/uspace/drv/bus/usb/usbmid/usbmid.c@ e882e3a

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

usb: Drop unused `handle' parameter.

Rename get_interface ⇒ get_my_interface.

  • Property mode set to 100644
File size: 4.3 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 drvusbmid
30 * @{
31 */
32/**
33 * @file
34 * Helper functions.
35 */
36#include <errno.h>
37#include <str_error.h>
38#include <stdlib.h>
39#include <usb_iface.h>
40#include <usb/ddfiface.h>
41#include <usb/dev/pipes.h>
42#include <usb/classes/classes.h>
43#include <usb/dev/recognise.h>
44#include "usbmid.h"
45
46/** Callback for DDF USB interface. */
47static int usb_iface_get_interface_impl(ddf_fun_t *fun, int *iface_no)
48{
49 assert(fun);
50
51 usbmid_interface_t *iface = fun->driver_data;
52 assert(iface);
53
54 if (iface_no != NULL) {
55 *iface_no = iface->interface_no;
56 }
57
58 return EOK;
59}
60
61/** DDF interface of the child - interface function. */
62static usb_iface_t child_usb_iface = {
63 .get_hc_handle = usb_iface_get_hc_handle_device_impl,
64 .get_my_address = usb_iface_get_my_address_forward_impl,
65 .get_my_interface = usb_iface_get_interface_impl,
66};
67
68/** Operations for children - interface functions. */
69static ddf_dev_ops_t child_device_ops = {
70 .interfaces[USB_DEV_IFACE] = &child_usb_iface
71};
72
73int usbmid_interface_destroy(usbmid_interface_t *mid_iface)
74{
75 assert(mid_iface);
76 assert_link_not_used(&mid_iface->link);
77 const int ret = ddf_fun_unbind(mid_iface->fun);
78 if (ret != EOK) {
79 return ret;
80 }
81 /* NOTE: usbmid->interface points somewhere, but we did not
82 * allocate that space, so don't touch */
83 ddf_fun_destroy(mid_iface->fun);
84 /* NOTE: mid_iface is invalid at this point, it was assigned to
85 * mid_iface->fun->driver_data and freed in ddf_fun_destroy */
86 return EOK;
87}
88
89/** Spawn new child device from one interface.
90 *
91 * @param parent Parent MID device.
92 * @param iface Interface information.
93 * @param device_descriptor Device descriptor.
94 * @param interface_descriptor Interface descriptor.
95 * @return Error code.
96 */
97int usbmid_spawn_interface_child(usb_device_t *parent,
98 usbmid_interface_t *iface,
99 const usb_standard_device_descriptor_t *device_descriptor,
100 const usb_standard_interface_descriptor_t *interface_descriptor)
101{
102 ddf_fun_t *child = NULL;
103 char *child_name = NULL;
104 int rc;
105
106 /*
107 * Name is class name followed by interface number.
108 * The interface number shall provide uniqueness while the
109 * class name something humanly understandable.
110 */
111 rc = asprintf(&child_name, "%s%hhu",
112 usb_str_class(interface_descriptor->interface_class),
113 interface_descriptor->interface_number);
114 if (rc < 0) {
115 return ENOMEM;
116 }
117
118 /* Create the device. */
119 child = ddf_fun_create(parent->ddf_dev, fun_inner, child_name);
120 free(child_name);
121 if (child == NULL) {
122 return ENOMEM;
123 }
124
125 rc = usb_device_create_match_ids_from_interface(device_descriptor,
126 interface_descriptor, &child->match_ids);
127 if (rc != EOK) {
128 ddf_fun_destroy(child);
129 return rc;
130 }
131
132 rc = ddf_fun_bind(child);
133 if (rc != EOK) {
134 /* This takes care of match_id deallocation as well. */
135 ddf_fun_destroy(child);
136 return rc;
137 }
138
139 iface->fun = child;
140 child->driver_data = iface;
141 child->ops = &child_device_ops;
142
143 return EOK;
144}
145
146/**
147 * @}
148 */
Note: See TracBrowser for help on using the repository browser.