Changeset 93fb170c in mainline for uspace/drv


Ignore:
Timestamp:
2011-01-08T18:51:31Z (15 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
15be932
Parents:
8f748215 (diff), a523af4 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from main branch

Location:
uspace/drv
Files:
5 added
16 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/ns8250/ns8250.c

    r8f748215 r93fb170c  
    342342                printf(NAME ": failed to connect to the parent driver of the "
    343343                    "device %s.\n", dev->name);
    344                 ret = EPARTY;   /* FIXME: use another EC */
     344                ret = dev->parent_phone;
    345345                goto failed;
    346346        }
    347347       
    348348        /* Get hw resources. */
    349         if (!get_hw_resources(dev->parent_phone, &hw_resources)) {
     349        ret = get_hw_resources(dev->parent_phone, &hw_resources);
     350        if (ret != EOK) {
    350351                printf(NAME ": failed to get hw resources for the device "
    351352                    "%s.\n", dev->name);
    352                 ret = EPARTY;   /* FIXME: use another EC */
    353353                goto failed;
    354354        }
     
    374374                                printf(NAME ": i/o range assigned to the device "
    375375                                    "%s is too small.\n", dev->name);
    376                                 ret = EPARTY;   /* FIXME: use another EC */
     376                                ret = ELIMIT;
    377377                                goto failed;
    378378                        }
     
    390390                printf(NAME ": missing hw resource(s) for the device %s.\n",
    391391                    dev->name);
    392                 ret = EPARTY;   /* FIXME: use another EC */
     392                ret = ENOENT;
    393393                goto failed;
    394394        }
  • uspace/drv/pciintel/pci.c

    r8f748215 r93fb170c  
    452452static int pci_add_device(device_t *dev)
    453453{
     454        int rc;
     455
    454456        printf(NAME ": pci_add_device\n");
    455457       
     
    466468                    "parent's driver.\n");
    467469                delete_pci_bus_data(bus_data);
    468                 return EPARTY;  /* FIXME: use another EC */
     470                return dev->parent_phone;
    469471        }
    470472       
    471473        hw_resource_list_t hw_resources;
    472474       
    473         if (!get_hw_resources(dev->parent_phone, &hw_resources)) {
     475        rc = get_hw_resources(dev->parent_phone, &hw_resources);
     476        if (rc != EOK) {
    474477                printf(NAME ": pci_add_device failed to get hw resources for "
    475478                    "the device.\n");
    476479                delete_pci_bus_data(bus_data);
    477480                ipc_hangup(dev->parent_phone);
    478                 return EPARTY;  /* FIXME: use another EC */
     481                return rc;
    479482        }       
    480483       
  • uspace/drv/root/root.c

    r8f748215 r93fb170c  
    4747#include <macros.h>
    4848#include <inttypes.h>
     49#include <sysinfo.h>
    4950
    5051#include <driver.h>
     
    5556
    5657#define PLATFORM_DEVICE_NAME "hw"
    57 #define PLATFORM_DEVICE_MATCH_ID STRING(UARCH)
     58#define PLATFORM_DEVICE_MATCH_ID_FMT "platform/%s"
    5859#define PLATFORM_DEVICE_MATCH_SCORE 100
    5960
     
    100101static int add_platform_child(device_t *parent)
    101102{
     103        char *match_id;
     104        char *platform;
     105        size_t platform_size;
     106        int res;
     107
     108        /* Get platform name from sysinfo. */
     109
     110        platform = sysinfo_get_data("platform", &platform_size);
     111        if (platform == NULL) {
     112                printf(NAME ": Failed to obtain platform name.\n");
     113                return ENOENT;
     114        }
     115
     116        /* Null-terminate string. */
     117        platform = realloc(platform, platform_size + 1);
     118        if (platform == NULL) {
     119                printf(NAME ": Memory allocation failed.\n");
     120                return ENOMEM;
     121        }
     122
     123        platform[platform_size] = '\0';
     124
     125        /* Construct match ID. */
     126
     127        if (asprintf(&match_id, PLATFORM_DEVICE_MATCH_ID_FMT, platform) == -1) {
     128                printf(NAME ": Memory allocation failed.\n");
     129                return ENOMEM;
     130        }
     131
     132        /* Add child. */
     133
    102134        printf(NAME ": adding new child for platform device.\n");
    103135        printf(NAME ":   device node is `%s' (%d %s)\n", PLATFORM_DEVICE_NAME,
    104             PLATFORM_DEVICE_MATCH_SCORE, PLATFORM_DEVICE_MATCH_ID);
    105        
    106         int res = child_device_register_wrapper(parent, PLATFORM_DEVICE_NAME,
    107             PLATFORM_DEVICE_MATCH_ID, PLATFORM_DEVICE_MATCH_SCORE,
    108             NULL);
     136            PLATFORM_DEVICE_MATCH_SCORE, match_id);
     137
     138        res = child_device_register_wrapper(parent, PLATFORM_DEVICE_NAME,
     139            match_id, PLATFORM_DEVICE_MATCH_SCORE, NULL);
    109140
    110141        return res;
  • uspace/drv/rootpc/rootpc.c

    r8f748215 r93fb170c  
    2828
    2929/**
    30  * @defgroup root_pc Root HW device driver for ia32 and amd64 platform.
    31  * @brief HelenOS root HW device driver for ia32 and amd64 platform.
     30 * @defgroup root_pc PC platform driver.
     31 * @brief HelenOS PC platform driver.
    3232 * @{
    3333 */
     
    182182        /* Register child devices. */
    183183        if (!rootpc_add_children(dev)) {
    184                 printf(NAME ": failed to add child devices for platform "
    185                     "ia32.\n");
     184                printf(NAME ": failed to add child devices for PC platform.\n");
    186185        }
    187186       
     
    196195int main(int argc, char *argv[])
    197196{
    198         printf(NAME ": HelenOS rootpc device driver\n");
     197        printf(NAME ": HelenOS PC platform driver\n");
    199198        root_pc_init();
    200199        return driver_main(&rootpc_driver);
  • uspace/drv/rootpc/rootpc.ma

    r8f748215 r93fb170c  
    1 10 ia32
    2 10 amd64
     110 platform/pc
  • uspace/drv/rootvirt/devices.def

    r8f748215 r93fb170c  
    1717        .match_id = "virtual&test2"
    1818},
     19{
     20        .name = "null",
     21        .match_id = "virtual&test1"
     22},
    1923#endif
    2024/* Virtual USB host controller. */
  • uspace/drv/test1/Makefile

    r8f748215 r93fb170c  
    3333
    3434SOURCES = \
     35        char.c \
    3536        test1.c
    3637
  • uspace/drv/test1/test1.c

    r8f748215 r93fb170c  
    3434#include <errno.h>
    3535#include <str_error.h>
    36 #include <driver.h>
    37 
    38 #define NAME "test1"
     36#include "test1.h"
    3937
    4038static int add_device(device_t *dev);
     
    9896        add_device_to_class(dev, "virtual");
    9997
    100         if (dev->parent == NULL) {
     98        if (str_cmp(dev->name, "null") == 0) {
     99                dev->ops = &char_device_ops;
     100                add_device_to_class(dev, "virt-null");
     101        } else if (dev->parent == NULL) {
    101102                register_child_verbose(dev, "cloning myself ;-)", "clone",
    102103                    "virtual&test1", 10);
     
    117118}
    118119
    119 
  • uspace/drv/uhci/main.c

    r8f748215 r93fb170c  
    2727 */
    2828#include <usb/hcdhubd.h>
     29#include <usb_iface.h>
    2930#include <errno.h>
    3031
     
    3435#include "uhci.h"
    3536
     37static int usb_iface_get_hc_handle(device_t *dev, devman_handle_t *handle)
     38{
     39        /* This shall be called only for the UHCI itself. */
     40        assert(dev->parent == NULL);
     41
     42        *handle = dev->handle;
     43        return EOK;
     44}
     45
     46static usb_iface_t hc_usb_iface = {
     47        .get_hc_handle = usb_iface_get_hc_handle
     48};
    3649
    3750static device_ops_t uhci_ops = {
    38         .interfaces[USBHC_DEV_IFACE] = &uhci_iface,
     51        .interfaces[USB_DEV_IFACE] = &hc_usb_iface,
     52        .interfaces[USBHC_DEV_IFACE] = &uhci_iface
    3953};
    4054
  • uspace/drv/usbhub/Makefile

    r8f748215 r93fb170c  
    3434SOURCES = \
    3535        main.c \
    36         utils.c
     36        utils.c \
     37        usbhub.c \
     38        usblist.c
    3739
    3840include $(USPACE_PREFIX)/Makefile.common
  • uspace/drv/usbhub/main.c

    r8f748215 r93fb170c  
    3232
    3333#include <usb/usbdrv.h>
     34
    3435#include "usbhub.h"
    3536#include "usbhub_private.h"
     
    3738
    3839usb_general_list_t usb_hub_list;
     40futex_t usb_hub_list_lock;
    3941
    4042static driver_ops_t hub_driver_ops = {
     
    5860int main(int argc, char *argv[])
    5961{
     62        usb_dprintf_enable(NAME,1);
     63        futex_initialize(&usb_hub_list_lock, 0);
    6064        usb_lst_init(&usb_hub_list);
     65        futex_up(&usb_hub_list_lock);
    6166        fid_t fid = fibril_create(usb_hub_control_loop, NULL);
    6267        if (fid == 0) {
    63                 printf("%s: failed to start fibril for HUB devices\n", NAME);
     68                dprintf(1, "failed to start fibril for HUB devices");
     69                //printf("%s: failed to start fibril for HUB devices\n", NAME);
    6470                return ENOMEM;
    6571        }
  • uspace/drv/usbhub/usbhub_private.h

    r8f748215 r93fb170c  
    3838
    3939#include "usbhub.h"
     40#include "usblist.h"
     41
    4042#include <adt/list.h>
    4143#include <bool.h>
    4244#include <driver.h>
     45#include <futex.h>
     46
    4347#include <usb/usb.h>
     48#include <usb/usbdrv.h>
    4449#include <usb/classes/hub.h>
    4550#include <usb/devreq.h>
     51#include <usb/debug.h>
    4652
    4753//************
     
    5561//************
    5662//
    57 // My private list implementation; I did not like the original helenos list
    58 //
    59 // This one does not depend on the structure of stored data
     63// convenience debug printf
    6064//
    6165//************
    62 
    63 /** general list structure */
    64 
    65 
    66 typedef struct usb_general_list{
    67         void * data;
    68         struct usb_general_list * prev, * next;
    69 } usb_general_list_t;
    70 
    71 /** create head of usb general list */
    72 usb_general_list_t * usb_lst_create(void);
    73 
    74 /** initialize head of usb general list */
    75 void usb_lst_init(usb_general_list_t * lst);
    76 
    77 
    78 /** is the list empty? */
    79 static inline bool usb_lst_empty(usb_general_list_t * lst){
    80         return lst?(lst->next==lst):true;
    81 }
    82 
    83 /** append data behind item */
    84 void usb_lst_append(usb_general_list_t * lst, void * data);
    85 
    86 /** prepend data beore item */
    87 void usb_lst_prepend(usb_general_list_t * lst, void * data);
    88 
    89 /** remove list item from list */
    90 void usb_lst_remove(usb_general_list_t * item);
    91 
    92 /** get data o specified type from list item */
    93 #define usb_lst_get_data(item, type)  (type *) (item->data)
    94 
    95 /** get usb_hub_info_t data from list item */
    96 static inline usb_hub_info_t * usb_hub_lst_get_data(usb_general_list_t * item) {
    97         return usb_lst_get_data(item,usb_hub_info_t);
    98 }
     66#define dprintf(level, format, ...) \
     67        usb_dprintf(NAME, (level), format "\n", ##__VA_ARGS__)
    9968
    10069/**
    101  * @brief create hub structure instance
     70 * create hub structure instance
    10271 *
    10372 * Set the address and port count information most importantly.
     
    11281extern usb_general_list_t usb_hub_list;
    11382
     83/** lock for hub list*/
     84extern futex_t usb_hub_list_lock;
     85
    11486
    11587/**
    116  * @brief perform complete control read transaction
     88 * perform complete control read transaction
    11789 *
    11890 * manages all three steps of transaction: setup, read and finalize
     
    132104
    133105/**
    134  * @brief perform complete control write transaction
     106 * perform complete control write transaction
    135107 *
    136  * maanges all three steps of transaction: setup, write and finalize
     108 * manages all three steps of transaction: setup, write and finalize
    137109 * @param phone
    138110 * @param target
     
    147119    void * sent_buffer, size_t sent_size
    148120);
    149 
    150 
    151 /**
    152  * set the device request to be a set address request
    153  * @param request
    154  * @param addr
    155  * \TODO this will be obsolete see usb/dev_req.h
    156  */
    157 static inline void usb_hub_set_set_address_request(
    158 usb_device_request_setup_packet_t * request, uint16_t addr
    159 ){
    160         request->index = 0;
    161         request->request_type = 0;/// \TODO this is not very nice sollution, we ned constant
    162         request->request = USB_DEVREQ_SET_ADDRESS;
    163         request->value = addr;
    164         request->length = 0;
    165 }
    166121
    167122/**
  • uspace/drv/usbhub/utils.c

    r8f748215 r93fb170c  
    3131 */
    3232/** @file
    33  * @brief Hub driver.
     33 * @brief various utilities
    3434 */
    3535#include <driver.h>
     
    9090
    9191        if (sdescriptor[1] != USB_DESCTYPE_HUB) {
    92                 printf("[usb_hub] wrong descriptor %x\n",sdescriptor[1]);
     92                dprintf(1,"[usb_hub] wrong descriptor %x\n",sdescriptor[1]);
    9393                return NULL;
    9494        }
     
    102102        result->pwr_on_2_good_time = sdescriptor[5];
    103103        result->current_requirement = sdescriptor[6];
    104         size_t var_size = result->ports_count / 8 + ((result->ports_count % 8 > 0) ? 1 : 0);
     104        size_t var_size = result->ports_count / 8 + ((result->ports_count % 8 > 0)
     105                        ? 1 : 0);
    105106        result->devices_removable = (uint8_t*) malloc(var_size);
    106107        //printf("[usb_hub] getting removable devices data \n");
     
    198199}
    199200
    200 //list implementation
    201 
    202 usb_general_list_t * usb_lst_create(void) {
    203         usb_general_list_t* result = usb_new(usb_general_list_t);
    204         usb_lst_init(result);
    205         return result;
    206 }
    207 
    208 void usb_lst_init(usb_general_list_t * lst) {
    209         lst->prev = lst;
    210         lst->next = lst;
    211         lst->data = NULL;
    212 }
    213 
    214 void usb_lst_prepend(usb_general_list_t* item, void* data) {
    215         usb_general_list_t* appended = usb_new(usb_general_list_t);
    216         appended->data = data;
    217         appended->next = item;
    218         appended->prev = item->prev;
    219         item->prev->next = appended;
    220         item->prev = appended;
    221 }
    222 
    223 void usb_lst_append(usb_general_list_t* item, void* data) {
    224         usb_general_list_t* appended = usb_new(usb_general_list_t);
    225         appended->data = data;
    226         appended->next = item->next;
    227         appended->prev = item;
    228         item->next->prev = appended;
    229         item->next = appended;
    230 }
    231 
    232 void usb_lst_remove(usb_general_list_t* item) {
    233         item->next->prev = item->prev;
    234         item->prev->next = item->next;
    235 }
    236 
    237 static void usb_hub_test_port_status(void) {
    238         printf("[usb_hub] -------------port status test---------\n");
    239         usb_port_status_t status = 0;
    240 
    241         //printf("original status %d (should be 0)\n",(uint32_t)status);
    242         usb_port_set_bit(&status, 1, 1);
    243         //printf("%d =?= 2\n",(uint32_t)status);
    244         if (status != 2) {
    245                 printf("[usb_port_status] test failed: wrong set of bit 1\n");
    246                 return;
    247         }
    248         usb_port_set_bit(&status, 3, 1);
    249         if (status != 10) {
    250                 printf("[usb_port_status] test failed: wrong set of bit 3\n");
    251                 return;
    252         }
    253 
    254         usb_port_set_bit(&status, 15, 1);
    255         if (status != 10 + (1 << 15)) {
    256                 printf("[usb_port_status] test failed: wrong set of bit 15\n");
    257                 return;
    258         }
    259         usb_port_set_bit(&status, 1, 0);
    260         if (status != 8 + (1 << 15)) {
    261                 printf("[usb_port_status] test failed: wrong unset of bit 1\n");
    262                 return;
    263         }
    264         int i;
    265         for (i = 0; i < 32; ++i) {
    266                 if (i == 3 || i == 15) {
    267                         if (!usb_port_get_bit(&status, i)) {
    268                                 printf("[usb_port_status] test failed: wrong bit at %d\n", i);
    269                         }
    270                 } else {
    271                         if (usb_port_get_bit(&status, i)) {
    272                                 printf("[usb_port_status] test failed: wrong bit at %d\n", i);
    273                         }
    274                 }
    275         }
    276 
    277         printf("test ok\n");
    278 
    279 
    280         //printf("%d =?= 10\n",(uint32_t)status);
    281 
    282         //printf("this should be 0: %d \n",usb_port_get_bit(&status,0));
    283         //printf("this should be 1: %d \n",usb_port_get_bit(&status,1));
    284         //printf("this should be 0: %d \n",usb_port_get_bit(&status,2));
    285         //printf("this should be 1: %d \n",usb_port_get_bit(&status,3));
    286         //printf("this should be 0: %d \n",usb_port_get_bit(&status,4));
    287 
    288 
    289 
    290 
    291 }
    292 
    293 //*********************************************
    294 //
    295 //  hub driver code, initialization
    296 //
    297 //*********************************************
    298 
    299 usb_hub_info_t * usb_create_hub_info(device_t * device, int hc) {
    300         usb_hub_info_t* result = usb_new(usb_hub_info_t);
    301         //result->device = device;
    302         result->port_count = -1;
    303         /// \TODO is this correct? is the device stored?
    304         result->device = device;
    305 
    306 
    307         //printf("[usb_hub] phone to hc = %d\n", hc);
    308         if (hc < 0) {
    309                 return result;
    310         }
    311         //get some hub info
    312         usb_address_t addr = usb_drv_get_my_address(hc, device);
    313         printf("[usb_hub] addres of newly created hub = %d\n", addr);
    314         /*if(addr<0){
    315                 //return result;
    316                
    317         }*/
    318 
    319         result->usb_device = usb_new(usb_hcd_attached_device_info_t);
    320         result->usb_device->address = addr;
    321 
    322         // get hub descriptor
    323         usb_target_t target;
    324         target.address = addr;
    325         target.endpoint = 0;
    326         usb_device_request_setup_packet_t request;
    327         //printf("[usb_hub] creating descriptor request\n");
    328         usb_hub_set_descriptor_request(&request);
    329 
    330         //printf("[usb_hub] creating serialized descriptor\n");
    331         void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
    332         usb_hub_descriptor_t * descriptor;
    333         size_t received_size;
    334         int opResult;
    335         //printf("[usb_hub] starting control transaction\n");
    336         opResult = usb_drv_sync_control_read(
    337                         hc, target, &request, serialized_descriptor,
    338                         USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
    339         if (opResult != EOK) {
    340                 printf("[usb_hub] failed when receiving hub descriptor, badcode = %d\n",opResult);
    341                 ///\TODO memory leak will occur here!
    342                 return result;
    343         }
    344         //printf("[usb_hub] deserializing descriptor\n");
    345         descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
    346         if(descriptor==NULL){
    347                 printf("[usb_hub] could not deserialize descriptor \n");
    348                 result->port_count = 1;///\TODO this code is only for debug!!!
    349                 return result;
    350         }
    351         //printf("[usb_hub] setting port count to %d\n",descriptor->ports_count);
    352         result->port_count = descriptor->ports_count;
    353         result->attached_devs = (usb_hub_attached_device_t*)
    354             malloc((result->port_count+1) * sizeof(usb_hub_attached_device_t));
    355         int i;
    356         for(i=0;i<result->port_count+1;++i){
    357                 result->attached_devs[i].devman_handle=0;
    358                 result->attached_devs[i].address=0;
    359         }
    360         //printf("[usb_hub] freeing data\n");
    361         free(serialized_descriptor);
    362         free(descriptor->devices_removable);
    363         free(descriptor);
    364 
    365         //finish
    366 
    367         printf("[usb_hub] hub info created\n");
    368 
    369         return result;
    370 }
    371 
    372 int usb_add_hub_device(device_t *dev) {
    373         printf(NAME ": add_hub_device(handle=%d)\n", (int) dev->handle);
    374         printf("[usb_hub] hub device\n");
    375 
    376         /*
    377          * We are some (probably deeply nested) hub.
    378          * Thus, assign our own operations and explore already
    379          * connected devices.
    380          */
    381 
    382         //create the hub structure
    383         //get hc connection
    384         /// \TODO correct params
    385         int hc = usb_drv_hc_connect(dev, 0);
    386 
    387         usb_hub_info_t * hub_info = usb_create_hub_info(dev, hc);
    388         int port;
    389         int opResult;
    390         usb_device_request_setup_packet_t request;
    391         usb_target_t target;
    392         target.address = hub_info->usb_device->address;
    393         target.endpoint = 0;
    394 
    395         //get configuration descriptor
    396         // this is not fully correct - there are more configurations
    397         // and all should be checked
    398         usb_standard_device_descriptor_t std_descriptor;
    399         opResult = usb_drv_req_get_device_descriptor(hc, target.address,
    400     &std_descriptor);
    401         if(opResult!=EOK){
    402                 printf("[usb_hub] could not get device descriptor, %d\n",opResult);
    403                 return 1;///\TODO some proper error code needed
    404         }
    405         printf("[usb_hub] hub has %d configurations\n",std_descriptor.configuration_count);
    406         if(std_descriptor.configuration_count<1){
    407                 printf("[usb_hub] THERE ARE NO CONFIGURATIONS AVAILABLE\n");
    408         }
    409         usb_standard_configuration_descriptor_t config_descriptor;
    410         opResult = usb_drv_req_get_bare_configuration_descriptor(hc,
    411         target.address, 0,
    412         &config_descriptor);
    413         if(opResult!=EOK){
    414                 printf("[usb_hub] could not get configuration descriptor, %d\n",opResult);
    415                 return 1;///\TODO some proper error code needed
    416         }
    417         //set configuration
    418         request.request_type = 0;
    419         request.request = USB_DEVREQ_SET_CONFIGURATION;
    420         request.index=0;
    421         request.length=0;
    422         request.value_high=0;
    423         request.value_low = config_descriptor.configuration_number;
    424         opResult = usb_drv_sync_control_write(hc, target, &request, NULL, 0);
    425         if (opResult != EOK) {
    426                 printf("[usb_hub]something went wrong when setting hub`s configuration, %d\n", opResult);
    427         }
    428 
    429 
    430         for (port = 1; port < hub_info->port_count+1; ++port) {
    431                 usb_hub_set_power_port_request(&request, port);
    432                 opResult = usb_drv_sync_control_write(hc, target, &request, NULL, 0);
    433                 printf("[usb_hub] powering port %d\n",port);
    434                 if (opResult != EOK) {
    435                         printf("[usb_hub]something went wrong when setting hub`s %dth port\n", port);
    436                 }
    437         }
    438         //ports powered, hub seems to be enabled
    439        
    440 
    441         ipc_hangup(hc);
    442 
    443         //add the hub to list
    444         usb_lst_append(&usb_hub_list, hub_info);
    445         printf("[usb_hub] hub info added to list\n");
    446         //(void)hub_info;
    447         usb_hub_check_hub_changes();
    448 
    449         /// \TODO start the check loop, if not already started...
    450 
    451         //this is just a test for port status bitmap type
    452         usb_hub_test_port_status();
    453 
    454         printf("[usb_hub] hub dev added\n");
    455         printf("\taddress %d, has %d ports \n",
    456                         hub_info->usb_device->address,
    457                         hub_info->port_count);
    458         printf("\tused configuration %d\n",config_descriptor.configuration_number);
    459 
    460         return EOK;
    461         //return ENOTSUP;
    462 }
    463 
    464 //*********************************************
    465 //
    466 //  hub driver code, main loop
    467 //
    468 //*********************************************
    469 
    470 /**
    471  * reset the port with new device and reserve the default address
    472  * @param hc
    473  * @param port
    474  * @param target
    475  */
    476 static void usb_hub_init_add_device(int hc, uint16_t port, usb_target_t target) {
    477         usb_device_request_setup_packet_t request;
    478         int opResult;
    479         printf("[usb_hub] some connection changed\n");
    480         //get default address
    481         opResult = usb_drv_reserve_default_address(hc);
    482         if (opResult != EOK) {
    483                 printf("[usb_hub] cannot assign default address, it is probably used\n");
    484                 return;
    485         }
    486         //reset port
    487         usb_hub_set_reset_port_request(&request, port);
    488         opResult = usb_drv_sync_control_write(
    489                         hc, target,
    490                         &request,
    491                         NULL, 0
    492                         );
    493         if (opResult != EOK) {
    494                 //continue;
    495                 printf("[usb_hub] something went wrong when reseting a port\n");
    496         }
    497 }
    498 
    499 /**
    500  * finalize adding new device after port reset
    501  * @param hc
    502  * @param port
    503  * @param target
    504  */
    505 static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
    506                 int hc, uint16_t port, usb_target_t target) {
    507 
    508         int opResult;
    509         printf("[usb_hub] finalizing add device\n");
    510         opResult = usb_hub_clear_port_feature(hc, target.address,
    511             port, USB_HUB_FEATURE_C_PORT_RESET);
    512         if (opResult != EOK) {
    513                 goto release;
    514         }
    515 
    516         /* Request address at from host controller. */
    517         usb_address_t new_device_address = usb_drv_request_address(hc);
    518         if (new_device_address < 0) {
    519                 printf("[usb_hub] failed to get free USB address\n");
    520                 opResult = new_device_address;
    521                 goto release;
    522         }
    523         printf("[usb_hub] setting new address\n");
    524         opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
    525             new_device_address);
    526 
    527         if (opResult != EOK) {
    528                 printf("[usb_hub] could not set address for new device\n");
    529                 goto release;
    530         }
    531 
    532 release:
    533         printf("[usb_hub] releasing default address\n");
    534         usb_drv_release_default_address(hc);
    535         if (opResult != EOK) {
    536                 return;
    537         }
    538 
    539         devman_handle_t child_handle;
    540         opResult = usb_drv_register_child_in_devman(hc, hub->device,
    541             new_device_address, &child_handle);
    542         if (opResult != EOK) {
    543                 printf("[usb_hub] could not start driver for new device \n");
    544                 return;
    545         }
    546         hub->attached_devs[port].devman_handle = child_handle;
    547         hub->attached_devs[port].address = new_device_address;
    548 
    549         opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
    550         if (opResult != EOK) {
    551                 printf("[usb_hub] could not assign address of device in hcd \n");
    552                 return;
    553         }
    554         printf("[usb_hub] new device address %d, handle %zu\n",
    555             new_device_address, child_handle);
    556        
    557 }
    558 
    559 /**
    560  * unregister device address in hc, close the port
    561  * @param hc
    562  * @param port
    563  * @param target
    564  */
    565 static void usb_hub_removed_device(
    566     usb_hub_info_t * hub, int hc, uint16_t port, usb_target_t target) {
    567         //usb_device_request_setup_packet_t request;
    568         int opResult;
    569         //disable port
    570         /*usb_hub_set_disable_port_request(&request, port);
    571         opResult = usb_drv_sync_control_write(
    572                         hc, target,
    573                         &request,
    574                         NULL, 0
    575                         );
    576         if (opResult != EOK) {
    577                 //continue;
    578                 printf("[usb_hub] something went wrong when disabling a port\n");
    579         }*/
    580         /// \TODO remove device
    581 
    582         hub->attached_devs[port].devman_handle=0;
    583         //close address
    584         if(hub->attached_devs[port].address!=0){
    585                 opResult = usb_drv_release_address(hc,hub->attached_devs[port].address);
    586                 if(opResult != EOK) {
    587                         printf("[usb_hub] could not release address of removed device: %d\n",opResult);
    588                 }
    589                 hub->attached_devs[port].address = 0;
    590         }else{
    591                 printf("[usb_hub] this is strange, disconnected device had no address\n");
    592                 //device was disconnected before it`s port was reset - return default address
    593                 usb_drv_release_default_address(hc);
    594         }
    595 }
    596 
    597 /**
    598  * process interrupts on given hub port
    599  * @param hc
    600  * @param port
    601  * @param target
    602  */
    603 static void usb_hub_process_interrupt(usb_hub_info_t * hub, int hc,
    604         uint16_t port, usb_address_t address) {
    605         printf("[usb_hub] interrupt at port %d\n", port);
    606         //determine type of change
    607         usb_target_t target;
    608         target.address=address;
    609         target.endpoint=0;
    610         usb_port_status_t status;
    611         size_t rcvd_size;
    612         usb_device_request_setup_packet_t request;
    613         int opResult;
    614         usb_hub_set_port_status_request(&request, port);
    615         //endpoint 0
    616 
    617         opResult = usb_drv_sync_control_read(
    618                         hc, target,
    619                         &request,
    620                         &status, 4, &rcvd_size
    621                         );
    622         if (opResult != EOK) {
    623                 printf("[usb_hub] ERROR: could not get port status\n");
    624                 return;
    625         }
    626         if (rcvd_size != sizeof (usb_port_status_t)) {
    627                 printf("[usb_hub] ERROR: received status has incorrect size\n");
    628                 return;
    629         }
    630         //something connected/disconnected
    631         if (usb_port_connect_change(&status)) {
    632                 opResult = usb_hub_clear_port_feature(hc, target.address,
    633                     port, USB_HUB_FEATURE_C_PORT_CONNECTION);
    634                 // TODO: check opResult
    635                 if (usb_port_dev_connected(&status)) {
    636                         printf("[usb_hub] some connection changed\n");
    637                         usb_hub_init_add_device(hc, port, target);
    638                 } else {
    639                         usb_hub_removed_device(hub, hc, port, target);
    640                 }
    641         }
    642         //port reset
    643         if (usb_port_reset_completed(&status)) {
    644                 printf("[usb_hub] port reset complete\n");
    645                 if (usb_port_enabled(&status)) {
    646                         usb_hub_finalize_add_device(hub, hc, port, target);
    647                 } else {
    648                         printf("[usb_hub] ERROR: port reset, but port still not enabled\n");
    649                 }
    650         }
    651 
    652         usb_port_set_connect_change(&status, false);
    653         usb_port_set_reset(&status, false);
    654         usb_port_set_reset_completed(&status, false);
    655         usb_port_set_dev_connected(&status, false);
    656         if (status) {
    657                 printf("[usb_hub]there was some unsupported change on port %d\n",port);
    658         }
    659         /// \TODO handle other changes
    660         /// \TODO debug log for various situations
    661 
    662 
    663 
    664         /*
    665         //configure device
    666         usb_drv_reserve_default_address(hc);
    667 
    668         usb_address_t new_device_address = usb_drv_request_address(hc);
    669 
    670 
    671         usb_drv_release_default_address(hc);
    672          * */
    673 }
    674 
    675 /** Check changes on all known hubs.
    676  */
    677 void usb_hub_check_hub_changes(void) {
    678         /*
    679          * Iterate through all hubs.
    680          */
    681         usb_general_list_t * lst_item;
    682         for (lst_item = usb_hub_list.next;
    683                         lst_item != &usb_hub_list;
    684                         lst_item = lst_item->next) {
    685                 usb_hub_info_t * hub_info = ((usb_hub_info_t*)lst_item->data);
    686                 /*
    687                  * Check status change pipe of this hub.
    688                  */
    689 
    690                 usb_target_t target;
    691                 target.address = hub_info->usb_device->address;
    692                 target.endpoint = 1;/// \TODO get from endpoint descriptor
    693                 printf("[usb_hub] checking changes for hub at addr %d\n",
    694                     target.address);
    695 
    696                 size_t port_count = hub_info->port_count;
    697 
    698                 /*
    699                  * Connect to respective HC.
    700                  */
    701                 int hc = usb_drv_hc_connect(hub_info->device, 0);
    702                 if (hc < 0) {
    703                         continue;
    704                 }
    705 
    706                 // FIXME: count properly
    707                 size_t byte_length = ((port_count+1) / 8) + 1;
    708 
    709                 void *change_bitmap = malloc(byte_length);
    710                 size_t actual_size;
    711                 usb_handle_t handle;
    712 
    713                 /*
    714                  * Send the request.
    715                  */
    716                 int opResult = usb_drv_async_interrupt_in(hc, target,
    717                                 change_bitmap, byte_length, &actual_size,
    718                                 &handle);
    719 
    720                 usb_drv_async_wait_for(handle);
    721 
    722                 if (opResult != EOK) {
    723                         printf("[usb_hub] something went wrong while getting status of hub\n");
    724                         continue;
    725                 }
    726                 unsigned int port;
    727                 for (port = 1; port < port_count+1; ++port) {
    728                         bool interrupt = (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
    729                         if (interrupt) {
    730                                 usb_hub_process_interrupt(
    731                                         hub_info, hc, port, hub_info->usb_device->address);
    732                         }
    733                 }
    734 
    735 
    736                 /*
    737                  * TODO: handle the changes.
    738                  */
    739 
    740                 /*
    741                  * WARNING: sample code, will not work out of the box.
    742                  * And does not contain code for checking for errors.
    743                  */
    744 #if 0
    745                 /*
    746                  * Before opening the port, we must acquire the default
    747                  * address.
    748                  */
    749                 usb_drv_reserve_default_address(hc);
    750 
    751                 usb_address_t new_device_address = usb_drv_request_address(hc);
    752 
    753                 // TODO: open the port
    754 
    755                 // TODO: send request for setting address to new_device_address
    756 
    757                 /*
    758                  * Once new address is set, we can release the default
    759                  * address.
    760                  */
    761                 usb_drv_release_default_address(hc);
    762 
    763                 /*
    764                  * Obtain descriptors and create match ids for devman.
    765                  */
    766 
    767                 // TODO: get device descriptors
    768 
    769                 // TODO: create match ids
    770 
    771                 // TODO: add child device
    772 
    773                 // child_device_register sets the device handle
    774                 // TODO: store it here
    775                 devman_handle_t new_device_handle = 0;
    776 
    777                 /*
    778                  * Inform the HC that the new device has devman handle
    779                  * assigned.
    780                  */
    781                 usb_drv_bind_address(hc, new_device_address, new_device_handle);
    782 
    783                 /*
    784                  * That's all.
    785                  */
    786 #endif
    787 
    788 
    789                 /*
    790                  * Hang-up the HC-connected phone.
    791                  */
    792                 ipc_hangup(hc);
    793         }
    794 }
     201
     202
     203
    795204
    796205/**
  • uspace/drv/usbkbd/main.c

    r8f748215 r93fb170c  
    2929#include <driver.h>
    3030#include <ipc/driver.h>
     31#include <ipc/kbd.h>
     32#include <io/keycode.h>
     33#include <io/console.h>
    3134#include <errno.h>
    3235#include <fibril.h>
     
    4144#define GUESSED_POLL_ENDPOINT 1
    4245
     46static void default_connection_handler(device_t *, ipc_callid_t, ipc_call_t *);
     47static device_ops_t keyboard_ops = {
     48        .default_handler = default_connection_handler
     49};
     50
     51static int console_callback_phone = -1;
     52
     53/** Default handler for IPC methods not handled by DDF.
     54 *
     55 * @param dev Device handling the call.
     56 * @param icallid Call id.
     57 * @param icall Call data.
     58 */
     59void default_connection_handler(device_t *dev,
     60    ipc_callid_t icallid, ipc_call_t *icall)
     61{
     62        sysarg_t method = IPC_GET_IMETHOD(*icall);
     63
     64        if (method == IPC_M_CONNECT_TO_ME) {
     65                int callback = IPC_GET_ARG5(*icall);
     66
     67                if (console_callback_phone != -1) {
     68                        ipc_answer_0(icallid, ELIMIT);
     69                        return;
     70                }
     71
     72                console_callback_phone = callback;
     73                ipc_answer_0(icallid, EOK);
     74                return;
     75        }
     76
     77        ipc_answer_0(icallid, EINVAL);
     78}
     79
     80static void send_key(int key, int type, wchar_t c) {
     81        async_msg_4(console_callback_phone, KBD_EVENT, type, key,
     82            KM_NUM_LOCK, c);
     83}
     84
     85static void send_alnum(int key, wchar_t c) {
     86        printf(NAME ": sending key '%lc' to console\n", (wint_t) c);
     87        send_key(key, KEY_PRESS, c);
     88        send_key(key, KEY_RELEASE, c);
     89}
     90
    4391/*
    4492 * Callbacks for parser
     
    142190        // get phone to my HC and save it as my parent's phone
    143191        // TODO: maybe not a good idea if DDF will use parent_phone
    144         kbd_dev->device->parent_phone = usb_drv_hc_connect(dev, 0);
     192        kbd_dev->device->parent_phone = usb_drv_hc_connect_auto(dev, 0);
    145193
    146194        kbd_dev->address = usb_drv_get_my_address(dev->parent_phone,
     
    183231                sizeof(usb_hid_report_in_callbacks_t));
    184232        callbacks->keyboard = usbkbd_process_keycodes;
     233
     234        if (console_callback_phone != -1) {
     235                static size_t counter = 0;
     236                counter++;
     237                if (counter > 3) {
     238                        counter = 0;
     239                        send_alnum(KC_A, L'a');
     240                }
     241        }
    185242
    186243        usb_hid_parse_report(kbd_dev->parser, buffer, actual_size, callbacks,
     
    268325         * Not supported yet, skip..
    269326         */
    270 //      int phone = usb_drv_hc_connect(dev, 0);
     327//      int phone = usb_drv_hc_connect_auto(dev, 0);
    271328//      if (phone < 0) {
    272329//              /*
     
    289346        fibril_add_ready(fid);
    290347
     348        dev->ops = &keyboard_ops;
     349
     350        add_device_to_class(dev, "keyboard");
     351
    291352        /*
    292353         * Hurrah, device is initialized.
  • uspace/drv/vhc/hcd.c

    r8f748215 r93fb170c  
    4646
    4747#include <usb/usb.h>
     48#include <usb_iface.h>
    4849#include "vhcd.h"
    4950#include "hc.h"
     
    5253#include "conn.h"
    5354
     55static int usb_iface_get_hc_handle(device_t *dev, devman_handle_t *handle)
     56{
     57        /* This shall be called only for VHC device. */
     58        assert(dev->parent == NULL);
     59
     60        *handle = dev->handle;
     61        return EOK;
     62}
     63
     64static usb_iface_t hc_usb_iface = {
     65        .get_hc_handle = usb_iface_get_hc_handle
     66};
     67
    5468static device_ops_t vhc_ops = {
    5569        .interfaces[USBHC_DEV_IFACE] = &vhc_iface,
     70        .interfaces[USB_DEV_IFACE] = &hc_usb_iface,
    5671        .default_handler = default_connection_handler
    5772};
     
    7085
    7186        dev->ops = &vhc_ops;
    72 
    73         /*
    74          * Initialize address management.
    75          */
    76         address_init();
    7787
    7888        /*
     
    108118        printf(NAME ": virtual USB host controller driver.\n");
    109119
     120        /*
     121         * Initialize address management.
     122         */
     123        address_init();
     124
     125        /*
     126         * Run the transfer scheduler.
     127         */
    110128        hc_manager();
    111129
     130        /*
     131         * We are also a driver within devman framework.
     132         */
    112133        return driver_main(&vhc_driver);
    113134}
  • uspace/drv/vhc/hub.c

    r8f748215 r93fb170c  
    7979        device_t *hc_dev = (device_t *) arg;
    8080
    81         int hc = usb_drv_hc_connect(hc_dev, IPC_FLAG_BLOCKING);
     81        int hc = usb_drv_hc_connect(hc_dev, hc_dev->handle, IPC_FLAG_BLOCKING);
    8282        if (hc < 0) {
    8383                printf(NAME ": failed to register root hub\n");
Note: See TracChangeset for help on using the changeset viewer.