source: mainline/uspace/drv/uhci/root_hub/port.c@ b00163f

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

code-style

  • Property mode set to 100644
File size: 3.5 KB
Line 
1
2#include <errno.h>
3//#include <usb/devreq.h> /* for usb_device_request_setup_packet_t */
4#include <usb/usb.h>
5#include <usb/usbdrv.h>
6
7#include "debug.h"
8#include "uhci.h"
9#include "port.h"
10#include "port_status.h"
11
12static int uhci_port_new_device(uhci_port_t *port);
13static int uhci_port_remove_device(uhci_port_t *port);
14static int uhci_port_set_enabled(uhci_port_t *port, bool enabled);
15
16/*----------------------------------------------------------------------------*/
17int uhci_port_check(void *port)
18{
19 uhci_port_t *port_instance = port;
20 assert(port_instance);
21 port_instance->hc_phone = devman_device_connect(port_instance->hc->handle, 0);
22
23 while (1) {
24 uhci_print_info("Port(%d) status address %p:\n",
25 port_instance->number, port_instance->address);
26
27 /* read register value */
28 port_status_t port_status =
29 port_status_read(port_instance->address);
30
31 /* debug print */
32 uhci_print_info("Port(%d) status %#.4x:\n",
33 port_instance->number, port_status);
34 print_port_status(port_status);
35
36 if (port_status & STATUS_CONNECTED_CHANGED) {
37 if (port_status & STATUS_CONNECTED) {
38 uhci_port_new_device(port_instance);
39 } else {
40 uhci_port_remove_device(port_instance);
41 }
42 }
43 async_usleep(port_instance->wait_period_usec);
44 }
45 return EOK;
46}
47/*----------------------------------------------------------------------------*/
48static int uhci_port_new_device(uhci_port_t *port)
49{
50 assert(port);
51 assert(port->hc);
52
53 uhci_print_info("Adding new device on port %d.\n", port->number);
54
55 uhci_t *uhci_instance = (uhci_t*)(port->hc->driver_data);
56
57 /* get default address */
58 usb_address_keeping_reserve_default(&uhci_instance->address_manager);
59
60 const usb_address_t usb_address =
61 usb_address_keeping_request(&uhci_instance->address_manager);
62
63 if (usb_address <= 0) {
64 return usb_address;
65 }
66
67 /* enable port */
68 uhci_port_set_enabled(port, true);
69
70 /* assign address to device */
71 int ret = usb_drv_req_set_address(port->hc_phone, 0, usb_address);
72
73
74 if (ret != EOK) { /* address assigning went wrong */
75 uhci_print_error("Failed(%d) to assign address to the device.\n", ret);
76 uhci_port_set_enabled(port, false);
77 usb_address_keeping_release_default(&uhci_instance->address_manager);
78 return ENOMEM;
79 }
80
81 /* release default address */
82 usb_address_keeping_release_default(&uhci_instance->address_manager);
83
84 /* communicate and possibly report to devman */
85 assert(port->attached_device == 0);
86
87 ret = usb_drv_register_child_in_devman(port->hc_phone, port->hc, usb_address,
88 &port->attached_device);
89
90 if (ret != EOK) { /* something went wrong */
91 uhci_print_error("Failed(%d) in usb_drv_register_child.\n", ret);
92 uhci_port_set_enabled(port, false);
93 return ENOMEM;
94 }
95
96 return EOK;
97}
98/*----------------------------------------------------------------------------*/
99static int uhci_port_remove_device(uhci_port_t *port)
100{
101 uhci_print_error("Don't know how to remove device %#x.\n",
102 port->attached_device);
103 uhci_port_set_enabled(port, false);
104 return EOK;
105}
106/*----------------------------------------------------------------------------*/
107static int uhci_port_set_enabled(uhci_port_t *port, bool enabled)
108{
109 assert(port);
110
111 /* read register value */
112 port_status_t port_status
113 = port_status_read(port->address);
114
115 /* enable port: register write */
116 if (enabled) {
117 port_status |= STATUS_ENABLED;
118 } else {
119 port_status &= ~STATUS_ENABLED;
120 }
121 port_status_write(port->address, port_status);
122
123 uhci_print_info("%s port %d.\n",
124 enabled ? "Enabled" : "Disabled", port->number);
125 return EOK;
126}
127/*----------------------------------------------------------------------------*/
Note: See TracBrowser for help on using the repository browser.