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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1e94e09 was feeac0d, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Simplify use of list_foreach.

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