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

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

Merge mainline changes.

Major conflicts in USB HC drivers.
Compiles and UHCI works (qemu).
OHCI has device remove problems.

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[3ae93a8]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 */
[56fd7cf]32
[3ae93a8]33/**
34 * @file
35 * Helper functions.
36 */
37#include <errno.h>
38#include <str_error.h>
39#include <stdlib.h>
40#include <usb_iface.h>
[7d521e24]41#include <usb/dev/pipes.h>
[3ae93a8]42#include <usb/classes/classes.h>
[7d521e24]43#include <usb/dev/recognise.h>
[3ae93a8]44#include "usbmid.h"
[6fe7683]45
46/** Get USB device handle by calling the parent usb_device_t.
[4ca778b]47 *
48 * @param[in] fun Device function the operation is running on.
[6fe7683]49 * @param[out] handle Device handle.
[4ca778b]50 * @return Error code.
51 */
[8e4219ab]52static int usb_iface_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
53{
54 assert(fun);
55 assert(handle);
56 usb_device_t *usb_dev = usb_device_get(ddf_fun_get_dev(fun));
57 *handle = usb_device_get_devman_handle(usb_dev);
58 return EOK;
59}
60
[6fe7683]61/** Callback for DDF USB get interface. */
62static int usb_iface_iface_no(ddf_fun_t *fun, int *iface_no)
[8ba18c6]63{
[56fd7cf]64 usbmid_interface_t *iface = ddf_fun_data_get(fun);
[8ba18c6]65 assert(iface);
66
[6fe7683]67 if (iface_no)
[8ba18c6]68 *iface_no = iface->interface_no;
69
70 return EOK;
71}
72
[6fe7683]73/** DDF interface of the child - USB functions. */
[b68b279]74static usb_iface_t child_usb_iface = {
[3121b5f]75 .get_my_device_handle = usb_iface_device_handle,
[6fe7683]76 .get_my_interface = usb_iface_iface_no,
[b68b279]77};
78
[a6add7a]79/** Operations for children - interface functions. */
[eb1a2f4]80static ddf_dev_ops_t child_device_ops = {
[b68b279]81 .interfaces[USB_DEV_IFACE] = &child_usb_iface
[3ae93a8]82};
83
[38e68ab]84int usbmid_interface_destroy(usbmid_interface_t *mid_iface)
85{
86 assert(mid_iface);
87 assert_link_not_used(&mid_iface->link);
88 const int ret = ddf_fun_unbind(mid_iface->fun);
89 if (ret != EOK) {
90 return ret;
91 }
92 ddf_fun_destroy(mid_iface->fun);
93 return EOK;
94}
[3ae93a8]95
96/** Spawn new child device from one interface.
97 *
98 * @param parent Parent MID device.
[ecb107b]99 * @param iface Interface information.
[3ae93a8]100 * @param device_descriptor Device descriptor.
101 * @param interface_descriptor Interface descriptor.
102 * @return Error code.
103 */
[fcafa04]104int usbmid_spawn_interface_child(usb_device_t *parent,
[9e2132a]105 usbmid_interface_t **iface_ret,
[3ae93a8]106 const usb_standard_device_descriptor_t *device_descriptor,
107 const usb_standard_interface_descriptor_t *interface_descriptor)
108{
[eb1a2f4]109 ddf_fun_t *child = NULL;
[3ae93a8]110 char *child_name = NULL;
111 int rc;
112
113 /*
114 * Name is class name followed by interface number.
115 * The interface number shall provide uniqueness while the
116 * class name something humanly understandable.
117 */
[8be7819]118 rc = asprintf(&child_name, "%s%hhu",
[3ae93a8]119 usb_str_class(interface_descriptor->interface_class),
[8be7819]120 interface_descriptor->interface_number);
[3ae93a8]121 if (rc < 0) {
[359d96f]122 return ENOMEM;
[3ae93a8]123 }
[8ba18c6]124
[eb1a2f4]125 /* Create the device. */
[6785b538]126 child = usb_device_ddf_fun_create(parent, fun_inner, child_name);
[359d96f]127 free(child_name);
[eb1a2f4]128 if (child == NULL) {
[359d96f]129 return ENOMEM;
[eb1a2f4]130 }
131
[56fd7cf]132 match_id_list_t match_ids;
133 init_match_ids(&match_ids);
134
[51f0e410]135 rc = usb_device_create_match_ids_from_interface(device_descriptor,
[56fd7cf]136 interface_descriptor, &match_ids);
[3ae93a8]137 if (rc != EOK) {
[359d96f]138 ddf_fun_destroy(child);
139 return rc;
[3ae93a8]140 }
141
[feeac0d]142 list_foreach(match_ids.ids, link, match_id_t, match_id) {
[56fd7cf]143 rc = ddf_fun_add_match_id(child, match_id->id, match_id->score);
144 if (rc != EOK) {
145 clean_match_ids(&match_ids);
146 ddf_fun_destroy(child);
147 return rc;
148 }
149 }
150 clean_match_ids(&match_ids);
[9e2132a]151 ddf_fun_set_ops(child, &child_device_ops);
152
153 usbmid_interface_t *iface = ddf_fun_data_alloc(child, sizeof(*iface));
154
155 iface->fun = child;
156 iface->interface_no = interface_descriptor->interface_number;
157 link_initialize(&iface->link);
[56fd7cf]158
[eb1a2f4]159 rc = ddf_fun_bind(child);
[3ae93a8]160 if (rc != EOK) {
161 /* This takes care of match_id deallocation as well. */
[eb1a2f4]162 ddf_fun_destroy(child);
[359d96f]163 return rc;
[3ae93a8]164 }
[9e2132a]165 *iface_ret = iface;
[5153b58]166
[359d96f]167 return EOK;
[3ae93a8]168}
169
170/**
171 * @}
172 */
Note: See TracBrowser for help on using the repository browser.