Changeset ecb107b in mainline
- Timestamp:
- 2011-03-30T21:21:56Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 9bc276b
- Parents:
- 48d4231
- Location:
- uspace/drv/usbmid
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/usbmid/explore.c
r48d4231 recb107b 48 48 }; 49 49 50 /** Find starting indexes of all interface descriptors in a configuration. 51 * 52 * @param config_descriptor Full configuration descriptor. 53 * @param config_descriptor_size Size of @p config_descriptor in bytes. 54 * @param interface_positions Array where to store indexes of interfaces. 55 * @param interface_count Size of @p interface_positions array. 56 * @return Number of found interfaces. 57 * @retval (size_t)-1 Error occured. 58 */ 59 static size_t find_interface_descriptors(uint8_t *config_descriptor, 60 size_t config_descriptor_size, 61 size_t *interface_positions, size_t interface_count) 50 /** Tell whether given interface is already in the list. 51 * 52 * @param list List of usbmid_interface_t members to be searched. 53 * @param interface_no Interface number caller is looking for. 54 * @return Interface @p interface_no is already present in the list. 55 */ 56 static bool interface_in_list(link_t *list, int interface_no) 62 57 { 63 if (interface_count == 0) { 64 return (size_t) -1; 65 } 66 58 link_t *l; 59 for (l = list->next; l != list; l = l->next) { 60 usbmid_interface_t *iface 61 = list_get_instance(l, usbmid_interface_t, link); 62 if (iface->interface_no == interface_no) { 63 return true; 64 } 65 } 66 67 return false; 68 } 69 70 /** Create list of interfaces from configuration descriptor. 71 * 72 * @param config_descriptor Configuration descriptor. 73 * @param config_descriptor_size Size of configuration descriptor in bytes. 74 * @param list List where to add the interfaces. 75 */ 76 static void create_interfaces(uint8_t *config_descriptor, 77 size_t config_descriptor_size, link_t *list) 78 { 67 79 usb_dp_parser_data_t data = { 68 80 .data = config_descriptor, … … 75 87 }; 76 88 77 uint8_t *interface = usb_dp_get_nested_descriptor(&parser, &data,89 uint8_t *interface_ptr = usb_dp_get_nested_descriptor(&parser, &data, 78 90 data.data); 79 if (interface == NULL) { 80 return (size_t) -1; 81 } 82 if (interface[1] != USB_DESCTYPE_INTERFACE) { 83 return (size_t) -1; 84 } 85 86 size_t found_interfaces = 0; 87 interface_positions[found_interfaces] = interface - config_descriptor; 88 found_interfaces++; 89 90 while (interface != NULL) { 91 interface = usb_dp_get_sibling_descriptor(&parser, &data, 92 data.data, interface); 93 if ((interface != NULL) 94 && (found_interfaces < interface_count) 95 && (interface[1] == USB_DESCTYPE_INTERFACE)) { 96 interface_positions[found_interfaces] 97 = interface - config_descriptor; 98 found_interfaces++; 99 } 100 } 101 102 return found_interfaces; 91 if (interface_ptr == NULL) { 92 return; 93 } 94 95 do { 96 if (interface_ptr[1] != USB_DESCTYPE_INTERFACE) { 97 goto next_descriptor; 98 } 99 100 usb_standard_interface_descriptor_t *interface 101 = (usb_standard_interface_descriptor_t *) interface_ptr; 102 103 /* Skip alternate interfaces. */ 104 if (!interface_in_list(list, interface->interface_number)) { 105 usbmid_interface_t *iface 106 = malloc(sizeof(usbmid_interface_t)); 107 if (iface == NULL) { 108 break; 109 } 110 link_initialize(&iface->link); 111 iface->fun = NULL; 112 iface->interface_no = interface->interface_number; 113 iface->interface = interface; 114 115 list_append(&iface->link, list); 116 } 117 118 /* TODO: add the alternatives and create match ids from them 119 * as well. 120 */ 121 122 next_descriptor: 123 interface_ptr = usb_dp_get_sibling_descriptor(&parser, &data, 124 data.data, interface_ptr); 125 126 } while (interface_ptr != NULL); 127 103 128 } 104 129 … … 130 155 (usb_standard_configuration_descriptor_t *) config_descriptor_raw; 131 156 132 size_t *interface_descriptors133 = malloc(sizeof(size_t) * config_descriptor->interface_count);134 if (interface_descriptors == NULL) {135 usb_log_error("Out of memory (wanted %zuB).\n",136 sizeof(size_t) * config_descriptor->interface_count);137 free(config_descriptor_raw);138 return false;139 }140 size_t interface_descriptors_count141 = find_interface_descriptors(142 config_descriptor_raw, config_descriptor_size,143 interface_descriptors, config_descriptor->interface_count);144 145 if (interface_descriptors_count == (size_t) -1) {146 usb_log_error("Problem parsing configuration descriptor.\n");147 free(interface_descriptors);148 return false;149 }150 151 157 /* Select the first configuration */ 152 158 rc = usb_request_set_configuration(&dev->ctrl_pipe, … … 155 161 usb_log_error("Failed to set device configuration: %s.\n", 156 162 str_error(rc)); 157 free(interface_descriptors); 158 return false; 159 } 160 163 return false; 164 } 161 165 162 166 /* Create control function */ … … 164 168 if (ctl_fun == NULL) { 165 169 usb_log_error("Failed to create control function.\n"); 166 free(interface_descriptors);167 170 return false; 168 171 } … … 174 177 usb_log_error("Failed to bind control function: %s.\n", 175 178 str_error(rc)); 176 free(interface_descriptors); 177 return false; 178 } 179 180 /* Spawn interface children */ 181 size_t i; 182 for (i = 0; i < interface_descriptors_count; i++) { 183 usb_standard_interface_descriptor_t *interface 184 = (usb_standard_interface_descriptor_t *) 185 (config_descriptor_raw + interface_descriptors[i]); 186 usb_log_debug2("Interface descriptor at index %zu (type %d).\n", 187 interface_descriptors[i], (int) interface->descriptor_type); 179 return false; 180 } 181 182 /* Create interface children. */ 183 link_t interface_list; 184 list_initialize(&interface_list); 185 create_interfaces(config_descriptor_raw, config_descriptor_size, 186 &interface_list); 187 188 link_t *link; 189 for (link = interface_list.next; link != &interface_list; 190 link = link->next) { 191 usbmid_interface_t *iface = list_get_instance(link, 192 usbmid_interface_t, link); 193 188 194 usb_log_info("Creating child for interface %d (%s).\n", 189 (int) interface->interface_number, 190 usb_str_class(interface->interface_class)); 191 rc = usbmid_spawn_interface_child(dev, &dev->descriptors.device, 192 interface); 195 (int) iface->interface_no, 196 usb_str_class(iface->interface->interface_class)); 197 198 rc = usbmid_spawn_interface_child(dev, iface, 199 &dev->descriptors.device, iface->interface); 193 200 if (rc != EOK) { 194 201 usb_log_error("Failed to create interface child: %s.\n", -
uspace/drv/usbmid/usbmid.c
r48d4231 recb107b 79 79 }; 80 80 81 /** Create new interface for USB MID device.82 *83 * @param fun Backing generic DDF device function (representing interface).84 * @param iface_no Interface number.85 * @return New interface.86 * @retval NULL Error occured.87 */88 usbmid_interface_t *usbmid_interface_create(ddf_fun_t *fun, int iface_no)89 {90 usbmid_interface_t *iface = malloc(sizeof(usbmid_interface_t));91 if (iface == NULL) {92 usb_log_error("Out of memory (wanted %zuB).\n",93 sizeof(usbmid_interface_t));94 return NULL;95 }96 97 iface->fun = fun;98 iface->interface_no = iface_no;99 100 return iface;101 }102 103 81 104 82 /** Spawn new child device from one interface. 105 83 * 106 84 * @param parent Parent MID device. 85 * @param iface Interface information. 107 86 * @param device_descriptor Device descriptor. 108 87 * @param interface_descriptor Interface descriptor. … … 110 89 */ 111 90 int usbmid_spawn_interface_child(usb_device_t *parent, 91 usbmid_interface_t *iface, 112 92 const usb_standard_device_descriptor_t *device_descriptor, 113 93 const usb_standard_interface_descriptor_t *interface_descriptor) … … 115 95 ddf_fun_t *child = NULL; 116 96 char *child_name = NULL; 117 usbmid_interface_t *child_as_interface = NULL;118 97 int rc; 119 98 … … 137 116 } 138 117 118 iface->fun = child; 139 119 140 141 child_as_interface = usbmid_interface_create(child, 142 (int) interface_descriptor->interface_number); 143 if (child_as_interface == NULL) { 144 rc = ENOMEM; 145 goto error_leave; 146 } 147 148 child->driver_data = child_as_interface; 120 child->driver_data = iface; 149 121 child->ops = &child_device_ops; 150 122 … … 172 144 free(child_name); 173 145 } 174 if (child_as_interface != NULL) {175 free(child_as_interface);176 }177 146 178 147 return rc; -
uspace/drv/usbmid/usbmid.h
r48d4231 recb107b 37 37 #define USBMID_H_ 38 38 39 #include <adt/list.h> 39 40 #include <ddf/driver.h> 40 41 #include <usb/usb.h> … … 49 50 /** Function container. */ 50 51 ddf_fun_t *fun; 51 52 /** Interface descriptor. */ 53 usb_standard_interface_descriptor_t *interface; 52 54 /** Interface number. */ 53 55 int interface_no; 56 /** List link. */ 57 link_t link; 54 58 } usbmid_interface_t; 55 59 56 usbmid_interface_t *usbmid_interface_create(ddf_fun_t *, int);57 60 bool usbmid_explore_device(usb_device_t *); 58 int usbmid_spawn_interface_child(usb_device_t *, 61 int usbmid_spawn_interface_child(usb_device_t *, usbmid_interface_t *, 59 62 const usb_standard_device_descriptor_t *, 60 63 const usb_standard_interface_descriptor_t *);
Note:
See TracChangeset
for help on using the changeset viewer.