Changeset 0bd2879 in mainline for uspace/drv/uhci/root_hub/port.c


Ignore:
Timestamp:
2011-01-08T21:51:41Z (13 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
977fcea
Parents:
602eac5
Message:

Refactoring move usb generics to usb_device.c

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/uhci/root_hub/port.c

    r602eac5 r0bd2879  
    99#include "port_status.h"
    1010#include "utils/hc_synchronizer.h"
    11 #include "utils/identify.h"
    12 
    13 struct usb_match {
    14         int id_score;
    15         const char *id_string;
    16 };
     11#include "utils/usb_device.h"
    1712
    1813static int uhci_port_new_device(uhci_port_t *port);
     14static int uhci_port_remove_device(uhci_port_t *port);
    1915static int uhci_port_set_enabled(uhci_port_t *port, bool enabled);
    20 static usb_address_t assign_address_to_zero_device( device_t *hc );
    21 static int report_new_device(
    22   device_t *hc, usb_address_t address, int hub_port, devman_handle_t *handle);
     16static usb_address_t assign_address_to_zero_device(device_t *hc);
    2317
    2418/*----------------------------------------------------------------------------*/
     
    4337                if (port_status & STATUS_CONNECTED_CHANGED) {
    4438                        if (port_status & STATUS_CONNECTED) {
    45                                 /* assign address and report new device */
    4639                                uhci_port_new_device(port_instance);
    4740                        } else {
    48                                 /* TODO */
    49                                 /* remove device here */
    50                                 uhci_print_error(
    51                                   "Don't know how to remove device %#x.\n",
    52                                   port_instance->attached_device);
    53                                 uhci_port_set_enabled(port_instance, false);
     41                                uhci_port_remove_device(port_instance);
    5442                        }
    5543                }
     
    7765        usb_address_t address = assign_address_to_zero_device(port->hc);
    7866
     67
     68        if (address <= 0) { /* address assigning went wrong */
     69                uhci_print_error("Failed to assign address to the device.\n");
     70                uhci_port_set_enabled(port, false);
     71                usb_address_keeping_release_default(&uhci_instance->address_manager);
     72                return ENOMEM;
     73        }
     74
    7975        /* release default address */
    8076        usb_address_keeping_release_default(&uhci_instance->address_manager);
    8177
    82         if (address <= 0) { /* address assigning went wrong */
    83                 uhci_port_set_enabled(port, false);
    84                 uhci_print_error("Failed to assign address to the device.\n");
    85                 return ENOMEM;
    86         }
     78        /* communicate and possibly report to devman */
     79        assert(port->attached_device == 0);
    8780
    88         /* communicate and possibly report to devman */
    89         assert( port->attached_device == 0 );
    90         report_new_device(port->hc, address, port->number,
    91                 &port->attached_device);
     81#define CHECK_RET_DELETE_CHILD_RETURN(ret, child, message, args...)\
     82        if (ret < 0) { \
     83                uhci_print_error("Failed(%d) to "message, ret, ##args); \
     84                if (child) { \
     85                        delete_device(child); \
     86                } \
     87                uhci_port_set_enabled(port, false); \
     88                usb_address_keeping_release(&uhci_instance->address_manager, address); \
     89                return ret; \
     90        } else (void)0
    9291
    93         /* bind address */
     92        device_t *child = create_device();
     93
     94        int ret = child ? EOK : ENOMEM;
     95        CHECK_RET_DELETE_CHILD_RETURN(ret, child, "create device.\n" );
     96
     97        ret = usb_device_init(child, port->hc, address, port->number );
     98        CHECK_RET_DELETE_CHILD_RETURN(ret, child, "init usb device.\n" );
     99
     100        ret = child_device_register(child, port->hc);
     101        CHECK_RET_DELETE_CHILD_RETURN(ret, child, "register usb device.\n" );
     102
     103        /* store device and bind address, can not fail */
     104        port->attached_device = child->handle;
    94105        usb_address_keeping_devman_bind(&uhci_instance->address_manager,
    95106          address, port->attached_device);
    96107
     108        return EOK;
     109}
     110/*----------------------------------------------------------------------------*/
     111static int uhci_port_remove_device(uhci_port_t *port)
     112{
     113        uhci_print_error(       "Don't know how to remove device %#x.\n",
     114                port->attached_device);
     115        uhci_port_set_enabled(port, false);
    97116        return EOK;
    98117}
     
    114133        port_status_write( port->address, port_status );
    115134
    116 /*
    117         port_status.status.enabled = enabled;
    118         pio_write_16( port->address, port_status.raw_value );
    119 */
    120 
    121135        uhci_print_info( "%s port %d.\n",
    122136          enabled ? "Enabled" : "Disabled", port->number );
    123         return EOK;
    124 }
    125 /*----------------------------------------------------------------------------*/
    126 static int report_new_device(
    127   device_t *hc, usb_address_t address, int hub_port, devman_handle_t *handle)
    128 {
    129         assert( hc );
    130         assert( address > 0 );
    131         assert( address <= USB11_ADDRESS_MAX );
    132 
    133         device_t *child = create_device();
    134         if (child == NULL)
    135                 { return ENOMEM; }
    136 
    137         int ret = 0;
    138 #define CHECK_RET_DELETE_CHILD_RETURN(ret, child, message, args...)\
    139         if (ret < 0) { \
    140                 uhci_print_error("Failed(%d) to "message, ret, ##args); \
    141                 delete_device(child); \
    142                 return ret; \
    143         } else (void)0
    144 
    145         char *name;
    146 
    147         /* create name */
    148         ret = asprintf(&name, "usbdevice on hc%p/%d(%#x)", hc, hub_port, address);
    149         CHECK_RET_DELETE_CHILD_RETURN(ret, child, "create device name.\n");
    150 
    151         child->name = name;
    152 
    153         /* use descriptors to identify the device */
    154         ret = identify_device(hc, child, address);
    155         CHECK_RET_DELETE_CHILD_RETURN(ret, child, "identify device.\n");
    156 
    157         /* all went well, tell devman */
    158         ret = child_device_register( child, hc );
    159         CHECK_RET_DELETE_CHILD_RETURN(ret, child, "register device.\n");
    160 
    161         if (handle != NULL)
    162                 { *handle = child->handle; }
    163 
    164137        return EOK;
    165138}
     
    171144
    172145        uhci_t *uhci_instance = (uhci_t*)hc->driver_data;
     146
    173147        /* get new address */
    174148        const usb_address_t usb_address =
Note: See TracChangeset for help on using the changeset viewer.