source: mainline/uspace/drv/usbhub/ports.c@ 2ad98fd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2ad98fd was 2ad98fd, checked in by Matus Dekanek <smekideki@…>, 15 years ago

added ports.h/c files (forgot to add them before)

  • Property mode set to 100644
File size: 11.6 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 drvusbhub
30 * @{
31 */
32/** @file
33 * Hub ports functions.
34 */
35
36#include <bool.h>
37#include <errno.h>
38#include <str_error.h>
39#include <inttypes.h>
40#include <fibril_synch.h>
41
42#include <usb/debug.h>
43
44#include "ports.h"
45#include "usbhub.h"
46#include "usbhub_private.h"
47#include "port_status.h"
48
49
50/** Information for fibril for device discovery. */
51struct add_device_phase1 {
52 usb_hub_info_t *hub;
53 size_t port;
54 usb_speed_t speed;
55};
56
57static void usb_hub_removed_device(
58 usb_hub_info_t * hub, uint16_t port);
59
60static void usb_hub_port_reset_completed(usb_hub_info_t * hub,
61 uint16_t port, uint32_t status);
62
63static void usb_hub_port_over_current(usb_hub_info_t * hub,
64 uint16_t port, uint32_t status);
65
66static int get_port_status(usb_pipe_t *ctrl_pipe, size_t port,
67 usb_port_status_t *status);
68
69static int enable_port_callback(int port_no, void *arg);
70
71static int add_device_phase1_worker_fibril(void *arg);
72
73static int create_add_device_fibril(usb_hub_info_t *hub, size_t port,
74 usb_speed_t speed);
75
76/**
77 * Process interrupts on given hub port
78 *
79 * Accepts connection, over current and port reset change.
80 * @param hub hub representation
81 * @param port port number, starting from 1
82 */
83void usb_hub_process_interrupt(usb_hub_info_t * hub,
84 uint16_t port) {
85 usb_log_debug("interrupt at port %d\n", port);
86 //determine type of change
87 //usb_pipe_t *pipe = hub->control_pipe;
88
89 int opResult;
90
91 usb_port_status_t status;
92 opResult = get_port_status(&hub->usb_device->ctrl_pipe, port, &status);
93 if (opResult != EOK) {
94 usb_log_error("Failed to get port %zu status: %s.\n",
95 port, str_error(opResult));
96 return;
97 }
98 //connection change
99 if (usb_port_is_status(status, USB_HUB_FEATURE_C_PORT_CONNECTION)) {
100 bool device_connected = usb_port_is_status(status,
101 USB_HUB_FEATURE_PORT_CONNECTION);
102 usb_log_debug("Connection change on port %zu: %s.\n", port,
103 device_connected ? "device attached" : "device removed");
104
105 if (device_connected) {
106 opResult = create_add_device_fibril(hub, port,
107 usb_port_speed(status));
108 if (opResult != EOK) {
109 usb_log_error(
110 "Cannot handle change on port %zu: %s.\n",
111 str_error(opResult));
112 }
113 } else {
114 usb_hub_removed_device(hub, port);
115 }
116 }
117 //over current
118 if (usb_port_is_status(status, USB_HUB_FEATURE_C_PORT_OVER_CURRENT)) {
119 //check if it was not auto-resolved
120 usb_log_debug("overcurrent change on port\n");
121 usb_hub_port_over_current(hub, port, status);
122 }
123 //port reset
124 if (usb_port_is_status(status, USB_HUB_FEATURE_C_PORT_RESET)) {
125 usb_hub_port_reset_completed(hub, port, status);
126 }
127 usb_log_debug("status x%x : %d\n ", status, status);
128
129 usb_port_status_set_bit(
130 &status, USB_HUB_FEATURE_C_PORT_CONNECTION,false);
131 usb_port_status_set_bit(
132 &status, USB_HUB_FEATURE_PORT_RESET,false);
133 usb_port_status_set_bit(
134 &status, USB_HUB_FEATURE_C_PORT_RESET,false);
135 usb_port_status_set_bit(
136 &status, USB_HUB_FEATURE_C_PORT_OVER_CURRENT,false);
137 /// \TODO what about port power change?
138 if (status >> 16) {
139 usb_log_info("there was unsupported change on port %d: %X\n",
140 port, status);
141
142 }
143}
144
145
146/**
147 * routine called when a device on port has been removed
148 *
149 * If the device on port had default address, it releases default address.
150 * Otherwise does not do anything, because DDF does not allow to remove device
151 * from it`s device tree.
152 * @param hub hub representation
153 * @param port port number, starting from 1
154 */
155static void usb_hub_removed_device(
156 usb_hub_info_t * hub, uint16_t port) {
157
158 int opResult = usb_hub_clear_port_feature(hub->control_pipe,
159 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
160 if (opResult != EOK) {
161 usb_log_warning("could not clear port-change-connection flag\n");
162 }
163 /** \TODO remove device from device manager - not yet implemented in
164 * devide manager
165 */
166
167 //close address
168 //if (hub->attached_devs[port].address != 0) {
169 if(hub->ports[port].attached_device.address >= 0){
170 /*uncomment this code to use it when DDF allows device removal
171 opResult = usb_hc_unregister_device(
172 &hub->connection,
173 hub->attached_devs[port].address);
174 if(opResult != EOK) {
175 dprintf(USB_LOG_LEVEL_WARNING, "could not release "
176 "address of "
177 "removed device: %d", opResult);
178 }
179 hub->attached_devs[port].address = 0;
180 hub->attached_devs[port].handle = 0;
181 */
182 } else {
183 usb_log_warning("this is strange, disconnected device had "
184 "no address\n");
185 //device was disconnected before it`s port was reset -
186 //return default address
187 usb_hub_release_default_address(hub);
188 }
189}
190
191
192/**
193 * Process port reset change
194 *
195 * After this change port should be enabled, unless some problem occured.
196 * This functions triggers second phase of enabling new device.
197 * @param hub
198 * @param port
199 * @param status
200 */
201static void usb_hub_port_reset_completed(usb_hub_info_t * hub,
202 uint16_t port, uint32_t status){
203 usb_log_debug("Port %zu reset complete.\n", port);
204 if (usb_port_is_status(status, USB_HUB_FEATURE_PORT_ENABLE)) {
205 /* Finalize device adding. */
206 usb_hub_port_t *the_port = hub->ports + port;
207 fibril_mutex_lock(&the_port->reset_mutex);
208 the_port->reset_completed = true;
209 fibril_condvar_broadcast(&the_port->reset_cv);
210 fibril_mutex_unlock(&the_port->reset_mutex);
211 } else {
212 usb_log_warning(
213 "Port %zu reset complete but port not enabled.\n",
214 port);
215 }
216}
217
218/**
219 * Process over current condition on port.
220 *
221 * Turn off the power on the port.
222 *
223 * @param hub hub representation
224 * @param port port number, starting from 1
225 */
226static void usb_hub_port_over_current(usb_hub_info_t * hub,
227 uint16_t port, uint32_t status) {
228 int opResult;
229 if(usb_port_is_status(status, USB_HUB_FEATURE_PORT_OVER_CURRENT)){
230 opResult = usb_hub_clear_port_feature(hub->control_pipe,
231 port, USB_HUB_FEATURE_PORT_POWER);
232 if (opResult != EOK) {
233 usb_log_error("cannot power off port %d; %d\n",
234 port, opResult);
235 }
236 }else{
237 opResult = usb_hub_set_port_feature(hub->control_pipe,
238 port, USB_HUB_FEATURE_PORT_POWER);
239 if (opResult != EOK) {
240 usb_log_error("cannot power on port %d; %d\n",
241 port, opResult);
242 }
243 }
244}
245
246/** Retrieve port status.
247 *
248 * @param[in] ctrl_pipe Control pipe to use.
249 * @param[in] port Port number (starting at 1).
250 * @param[out] status Where to store the port status.
251 * @return Error code.
252 */
253static int get_port_status(usb_pipe_t *ctrl_pipe, size_t port,
254 usb_port_status_t *status)
255{
256 size_t recv_size;
257 usb_device_request_setup_packet_t request;
258 usb_port_status_t status_tmp;
259
260 usb_hub_set_port_status_request(&request, port);
261 int rc = usb_pipe_control_read(ctrl_pipe,
262 &request, sizeof(usb_device_request_setup_packet_t),
263 &status_tmp, sizeof(status_tmp), &recv_size);
264 if (rc != EOK) {
265 return rc;
266 }
267
268 if (recv_size != sizeof (status_tmp)) {
269 return ELIMIT;
270 }
271
272 if (status != NULL) {
273 *status = status_tmp;
274 }
275
276 return EOK;
277}
278
279/** Callback for enabling a specific port.
280 *
281 * We wait on a CV until port is reseted.
282 * That is announced via change on interrupt pipe.
283 *
284 * @param port_no Port number (starting at 1).
285 * @param arg Custom argument, points to @c usb_hub_info_t.
286 * @return Error code.
287 */
288static int enable_port_callback(int port_no, void *arg)
289{
290 usb_hub_info_t *hub = arg;
291 int rc;
292 usb_device_request_setup_packet_t request;
293 usb_hub_port_t *my_port = hub->ports + port_no;
294
295 usb_hub_set_reset_port_request(&request, port_no);
296 rc = usb_pipe_control_write(hub->control_pipe,
297 &request, sizeof(request), NULL, 0);
298 if (rc != EOK) {
299 usb_log_warning("Port reset failed: %s.\n", str_error(rc));
300 return rc;
301 }
302
303 /*
304 * Wait until reset completes.
305 */
306 fibril_mutex_lock(&my_port->reset_mutex);
307 while (!my_port->reset_completed) {
308 fibril_condvar_wait(&my_port->reset_cv, &my_port->reset_mutex);
309 }
310 fibril_mutex_unlock(&my_port->reset_mutex);
311
312 /* Clear the port reset change. */
313 rc = usb_hub_clear_port_feature(hub->control_pipe,
314 port_no, USB_HUB_FEATURE_C_PORT_RESET);
315 if (rc != EOK) {
316 usb_log_error("Failed to clear port %d reset feature: %s.\n",
317 port_no, str_error(rc));
318 return rc;
319 }
320
321 return EOK;
322}
323
324/** Fibril for adding a new device.
325 *
326 * Separate fibril is needed because the port reset completion is announced
327 * via interrupt pipe and thus we cannot block here.
328 *
329 * @param arg Pointer to struct add_device_phase1.
330 * @return 0 Always.
331 */
332static int add_device_phase1_worker_fibril(void *arg)
333{
334 struct add_device_phase1 *data
335 = (struct add_device_phase1 *) arg;
336
337 usb_address_t new_address;
338 devman_handle_t child_handle;
339
340 int rc = usb_hc_new_device_wrapper(data->hub->usb_device->ddf_dev,
341 &data->hub->connection, data->speed,
342 enable_port_callback, (int) data->port, data->hub,
343 &new_address, &child_handle,
344 NULL, NULL, NULL);
345
346 if (rc != EOK) {
347 usb_log_error("Failed registering device on port %zu: %s.\n",
348 data->port, str_error(rc));
349 goto leave;
350 }
351
352 data->hub->ports[data->port].attached_device.handle = child_handle;
353 data->hub->ports[data->port].attached_device.address = new_address;
354
355 usb_log_info("Detected new device on `%s' (port %zu), "
356 "address %d (handle %" PRIun ").\n",
357 data->hub->usb_device->ddf_dev->name, data->port,
358 new_address, child_handle);
359
360leave:
361 free(arg);
362
363 return EOK;
364}
365
366
367/** Start device adding when connection change is detected.
368 *
369 * This fires a new fibril to complete the device addition.
370 *
371 * @param hub Hub where the change occured.
372 * @param port Port index (starting at 1).
373 * @param speed Speed of the device.
374 * @return Error code.
375 */
376static int create_add_device_fibril(usb_hub_info_t *hub, size_t port,
377 usb_speed_t speed)
378{
379 struct add_device_phase1 *data
380 = malloc(sizeof(struct add_device_phase1));
381 if (data == NULL) {
382 return ENOMEM;
383 }
384 data->hub = hub;
385 data->port = port;
386 data->speed = speed;
387
388 usb_hub_port_t *the_port = hub->ports + port;
389
390 fibril_mutex_lock(&the_port->reset_mutex);
391 the_port->reset_completed = false;
392 fibril_mutex_unlock(&the_port->reset_mutex);
393
394 int rc = usb_hub_clear_port_feature(hub->control_pipe, port,
395 USB_HUB_FEATURE_C_PORT_CONNECTION);
396 if (rc != EOK) {
397 free(data);
398 usb_log_warning("Failed to clear port change flag: %s.\n",
399 str_error(rc));
400 return rc;
401 }
402
403 fid_t fibril = fibril_create(add_device_phase1_worker_fibril, data);
404 if (fibril == 0) {
405 free(data);
406 return ENOMEM;
407 }
408 fibril_add_ready(fibril);
409
410 return EOK;
411}
412
413/**
414 * @}
415 */
Note: See TracBrowser for help on using the repository browser.