Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/hid/input/generic/input.c

    r5da7199 r333c233  
    3838
    3939#include <adt/list.h>
    40 #include <bool.h>
    4140#include <ipc/services.h>
    4241#include <ipc/input.h>
     
    4746#include <stdio.h>
    4847#include <ns.h>
     48#include <ns_obsolete.h>
    4949#include <async.h>
     50#include <async_obsolete.h>
    5051#include <errno.h>
    5152#include <adt/fifo.h>
    5253#include <io/console.h>
    5354#include <io/keycode.h>
    54 #include <loc.h>
     55#include <devmap.h>
    5556#include <input.h>
    5657#include <kbd.h>
     
    6162#include <mouse.h>
    6263
     64// FIXME: remove this header
     65#include <kernel/ipc/ipc_methods.h>
     66
     67/* In microseconds */
     68#define DISCOVERY_POLL_INTERVAL  (10 * 1000 * 1000)
     69
    6370#define NUM_LAYOUTS  3
    6471
     
    7279static void kbd_devs_reclaim(void);
    7380
    74 async_sess_t *client_sess = NULL;
     81int client_phone = -1;
    7582
    7683/** List of keyboard devices */
     
    8188
    8289bool irc_service = false;
    83 async_sess_t *irc_sess = NULL;
     90int irc_phone = -1;
    8491
    8592void kbd_push_data(kbd_dev_t *kdev, sysarg_t data)
     
    165172       
    166173        ev.c = layout_parse_ev(kdev->active_layout, &ev);
    167        
    168         async_exch_t *exch = async_exchange_begin(client_sess);
    169         async_msg_4(exch, INPUT_EVENT_KEY, ev.type, ev.key, ev.mods, ev.c);
    170         async_exchange_end(exch);
     174        async_obsolete_msg_4(client_phone, INPUT_EVENT_KEY, ev.type, ev.key,
     175            ev.mods, ev.c);
    171176}
    172177
     
    174179void mouse_push_event_move(mouse_dev_t *mdev, int dx, int dy)
    175180{
    176         async_exch_t *exch = async_exchange_begin(client_sess);
    177         async_msg_2(exch, INPUT_EVENT_MOVE, dx, dy);
    178         async_exchange_end(exch);
     181        async_obsolete_msg_2(client_phone, INPUT_EVENT_MOVE, dx, dy);
    179182}
    180183
     
    182185void mouse_push_event_button(mouse_dev_t *mdev, int bnum, int press)
    183186{
    184         async_exch_t *exch = async_exchange_begin(client_sess);
    185         async_msg_2(exch, INPUT_EVENT_BUTTON, bnum, press);
    186         async_exchange_end(exch);
     187        async_obsolete_msg_2(client_phone, INPUT_EVENT_BUTTON, bnum, press);
    187188}
    188189
    189190static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    190191{
     192        ipc_callid_t callid;
     193        ipc_call_t call;
     194        int retval;
     195       
    191196        async_answer_0(iid, EOK);
    192197       
    193198        while (true) {
    194                 ipc_call_t call;
    195                 ipc_callid_t callid = async_get_call(&call);
     199                callid = async_get_call(&call);
    196200               
    197201                if (!IPC_GET_IMETHOD(call)) {
    198                         if (client_sess != NULL) {
    199                                 async_hangup(client_sess);
    200                                 client_sess = NULL;
     202                        if (client_phone != -1) {
     203                                async_obsolete_hangup(client_phone);
     204                                client_phone = -1;
    201205                        }
    202206                       
     
    205209                }
    206210               
    207                 async_sess_t *sess =
    208                     async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
    209                 if (sess != NULL) {
    210                         if (client_sess == NULL) {
    211                                 client_sess = sess;
    212                                 async_answer_0(callid, EOK);
    213                         } else
    214                                 async_answer_0(callid, ELIMIT);
    215                 } else {
    216                         switch (IPC_GET_IMETHOD(call)) {
    217                         case INPUT_YIELD:
    218                                 kbd_devs_yield();
    219                                 async_answer_0(callid, EOK);
     211                switch (IPC_GET_IMETHOD(call)) {
     212                case IPC_M_CONNECT_TO_ME:
     213                        if (client_phone != -1) {
     214                                retval = ELIMIT;
    220215                                break;
    221                         case INPUT_RECLAIM:
    222                                 kbd_devs_reclaim();
    223                                 async_answer_0(callid, EOK);
    224                                 break;
    225                         default:
    226                                 async_answer_0(callid, EINVAL);
    227216                        }
     217                        client_phone = IPC_GET_ARG5(call);
     218                        retval = 0;
     219                        break;
     220                case INPUT_YIELD:
     221                        kbd_devs_yield();
     222                        retval = 0;
     223                        break;
     224                case INPUT_RECLAIM:
     225                        kbd_devs_reclaim();
     226                        retval = 0;
     227                        break;
     228                default:
     229                        retval = EINVAL;
    228230                }
     231               
     232                async_answer_0(callid, retval);
    229233        }
    230234}
     
    271275        kdev->port_ops = port;
    272276        kdev->ctl_ops = ctl;
    273         kdev->svc_id = 0;
     277        kdev->dev_path = NULL;
    274278       
    275279        /* Initialize port driver. */
     
    299303        mdev->port_ops = port;
    300304        mdev->proto_ops = proto;
    301         mdev->svc_id = 0;
     305        mdev->dev_path = NULL;
    302306       
    303307        /* Initialize port driver. */
     
    320324/** Add new kbdev device.
    321325 *
    322  * @param service_id    Service ID of the keyboard device
     326 * @param dev_path Filesystem path to the device (/dev/class/...)
    323327 *
    324328 */
    325 static int kbd_add_kbdev(service_id_t service_id, kbd_dev_t **kdevp)
     329static int kbd_add_kbdev(const char *dev_path)
    326330{
    327331        kbd_dev_t *kdev = kbd_dev_new();
     
    329333                return -1;
    330334       
    331         kdev->svc_id = service_id;
     335        kdev->dev_path = dev_path;
    332336        kdev->port_ops = NULL;
    333337        kdev->ctl_ops = &kbdev_ctl;
    334        
    335         int rc = loc_service_get_name(service_id, &kdev->svc_name);
    336         if (rc != EOK) {
    337                 kdev->svc_name = NULL;
    338                 goto fail;
    339         }
    340338       
    341339        /* Initialize controller driver. */
     
    345343       
    346344        list_append(&kdev->kbd_devs, &kbd_devs);
    347         *kdevp = kdev;
    348345        return EOK;
    349346       
    350347fail:
    351         if (kdev->svc_name != NULL)
    352                 free(kdev->svc_name);
    353348        free(kdev);
    354349        return -1;
     
    357352/** Add new mousedev device.
    358353 *
    359  * @param service_id    Service ID of the mouse device
     354 * @param dev_path Filesystem path to the device (/dev/class/...)
    360355 *
    361356 */
    362 static int mouse_add_mousedev(service_id_t service_id, mouse_dev_t **mdevp)
     357static int mouse_add_mousedev(const char *dev_path)
    363358{
    364359        mouse_dev_t *mdev = mouse_dev_new();
     
    366361                return -1;
    367362       
    368         mdev->svc_id = service_id;
     363        mdev->dev_path = dev_path;
    369364        mdev->port_ops = NULL;
    370365        mdev->proto_ops = &mousedev_proto;
    371        
    372         int rc = loc_service_get_name(service_id, &mdev->svc_name);
    373         if (rc != EOK) {
    374                 mdev->svc_name = NULL;
    375                 goto fail;
    376         }
    377366       
    378367        /* Initialize controller driver. */
     
    382371       
    383372        list_append(&mdev->mouse_devs, &mouse_devs);
    384         *mdevp = mdev;
    385373        return EOK;
    386374       
     
    492480}
    493481
    494 static int dev_check_new_kbdevs(void)
    495 {
    496         category_id_t keyboard_cat;
    497         service_id_t *svcs;
    498         size_t count, i;
    499         bool already_known;
     482/** Periodically check for new input devices.
     483 *
     484 * Looks under /dev/class/keyboard and /dev/class/mouse.
     485 *
     486 * @param arg Ignored
     487 *
     488 */
     489static int dev_discovery_fibril(void *arg)
     490{
     491        char *dev_path;
     492        size_t kbd_id = 1;
     493        size_t mouse_id = 1;
    500494        int rc;
    501495       
    502         rc = loc_category_get_id("keyboard", &keyboard_cat, IPC_FLAG_BLOCKING);
    503         if (rc != EOK) {
    504                 printf("%s: Failed resolving category 'keyboard'.\n", NAME);
    505                 return ENOENT;
    506         }
    507        
    508         /*
    509          * Check for new keyboard devices
    510          */
    511         rc = loc_category_get_svcs(keyboard_cat, &svcs, &count);
    512         if (rc != EOK) {
    513                 printf("%s: Failed getting list of keyboard devices.\n",
     496        while (true) {
     497                async_usleep(DISCOVERY_POLL_INTERVAL);
     498               
     499                /*
     500                 * Check for new keyboard device
     501                 */
     502                rc = asprintf(&dev_path, "/dev/class/keyboard\\%zu", kbd_id);
     503                if (rc < 0)
     504                        continue;
     505               
     506                if (kbd_add_kbdev(dev_path) == EOK) {
     507                        printf("%s: Connected keyboard device '%s'\n",
     508                            NAME, dev_path);
     509                       
     510                        /* XXX Handle device removal */
     511                        ++kbd_id;
     512                }
     513               
     514                free(dev_path);
     515               
     516                /*
     517                 * Check for new mouse device
     518                 */
     519                rc = asprintf(&dev_path, "/dev/class/mouse\\%zu", mouse_id);
     520                if (rc < 0)
     521                        continue;
     522               
     523                if (mouse_add_mousedev(dev_path) == EOK) {
     524                        printf("%s: Connected mouse device '%s'\n",
     525                            NAME, dev_path);
     526                       
     527                        /* XXX Handle device removal */
     528                        ++mouse_id;
     529                }
     530               
     531                free(dev_path);
     532        }
     533       
     534        return EOK;
     535}
     536
     537/** Start a fibril for discovering new devices. */
     538static void input_start_dev_discovery(void)
     539{
     540        fid_t fid = fibril_create(dev_discovery_fibril, NULL);
     541        if (!fid) {
     542                printf("%s: Failed to create device discovery fibril.\n",
    514543                    NAME);
    515                 return EIO;
    516         }
    517 
    518         for (i = 0; i < count; i++) {
    519                 already_known = false;
    520                
    521                 /* Determine whether we already know this device. */
    522                 list_foreach(kbd_devs, kdev_link) {
    523                         kbd_dev_t *kdev = list_get_instance(kdev_link,
    524                             kbd_dev_t, kbd_devs);
    525                         if (kdev->svc_id == svcs[i]) {
    526                                 already_known = true;
    527                                 break;
    528                         }
    529                 }
    530                
    531                 if (!already_known) {
    532                         kbd_dev_t *kdev;
    533                         if (kbd_add_kbdev(svcs[i], &kdev) == EOK) {
    534                                 printf("%s: Connected keyboard device '%s'\n",
    535                                     NAME, kdev->svc_name);
    536                         }
    537                 }
    538         }
    539        
    540         free(svcs);
    541        
    542         /* XXX Handle device removal */
    543        
    544         return EOK;
    545 }
    546 
    547 static int dev_check_new_mousedevs(void)
    548 {
    549         category_id_t mouse_cat;
    550         service_id_t *svcs;
    551         size_t count, i;
    552         bool already_known;
    553         int rc;
    554        
    555         rc = loc_category_get_id("mouse", &mouse_cat, IPC_FLAG_BLOCKING);
    556         if (rc != EOK) {
    557                 printf("%s: Failed resolving category 'mouse'.\n", NAME);
    558                 return ENOENT;
    559         }
    560        
    561         /*
    562          * Check for new mouse devices
    563          */
    564         rc = loc_category_get_svcs(mouse_cat, &svcs, &count);
    565         if (rc != EOK) {
    566                 printf("%s: Failed getting list of mouse devices.\n",
    567                     NAME);
    568                 return EIO;
    569         }
    570        
    571         for (i = 0; i < count; i++) {
    572                 already_known = false;
    573                
    574                 /* Determine whether we already know this device. */
    575                 list_foreach(mouse_devs, mdev_link) {
    576                         mouse_dev_t *mdev = list_get_instance(mdev_link,
    577                             mouse_dev_t, mouse_devs);
    578                         if (mdev->svc_id == svcs[i]) {
    579                                 already_known = true;
    580                                 break;
    581                         }
    582                 }
    583                
    584                 if (!already_known) {
    585                         mouse_dev_t *mdev;
    586                         if (mouse_add_mousedev(svcs[i], &mdev) == EOK) {
    587                                 printf("%s: Connected mouse device '%s'\n",
    588                                     NAME, mdev->svc_name);
    589                         }
    590                 }
    591         }
    592        
    593         free(svcs);
    594        
    595         /* XXX Handle device removal */
    596        
    597         return EOK;
    598 }
    599 
    600 static int dev_check_new(void)
    601 {
    602         int rc;
    603        
    604         rc = dev_check_new_kbdevs();
    605         if (rc != EOK)
    606                 return rc;
    607        
    608         rc = dev_check_new_mousedevs();
    609         if (rc != EOK)
    610                 return rc;
    611 
    612         return EOK;
    613 }
    614 
    615 static void cat_change_cb(void)
    616 {
    617         dev_check_new();
    618 }
    619 
    620 /** Start listening for new devices. */
    621 static int input_start_dev_discovery(void)
    622 {
    623         int rc;
    624 
    625         rc = loc_register_cat_change_cb(cat_change_cb);
    626         if (rc != EOK) {
    627                 printf("%s: Failed registering callback for device discovery. "
    628                     "(%d)\n", NAME, rc);
    629                 return rc;
    630         }
    631 
    632         return dev_check_new();
     544                return;
     545        }
     546       
     547        fibril_add_ready(fid);
    633548}
    634549
     
    646561       
    647562        if (irc_service) {
    648                 while (irc_sess == NULL)
    649                         irc_sess = service_connect_blocking(EXCHANGE_SERIALIZE,
    650                             SERVICE_IRC, 0, 0);
     563                while (irc_phone < 0)
     564                        irc_phone = service_obsolete_connect_blocking(SERVICE_IRC, 0, 0);
    651565        }
    652566       
     
    658572       
    659573        /* Register driver */
    660         int rc = loc_server_register(NAME, client_connection);
     574        int rc = devmap_driver_register(NAME, client_connection);
    661575        if (rc < 0) {
    662                 printf("%s: Unable to register server (%d)\n", NAME, rc);
     576                printf("%s: Unable to register driver (%d)\n", NAME, rc);
    663577                return -1;
    664578        }
    665579       
    666         char kbd[LOC_NAME_MAXLEN + 1];
    667         snprintf(kbd, LOC_NAME_MAXLEN, "%s/%s", NAMESPACE, NAME);
    668        
    669         service_id_t service_id;
    670         if (loc_service_register(kbd, &service_id) != EOK) {
    671                 printf("%s: Unable to register service %s\n", NAME, kbd);
     580        char kbd[DEVMAP_NAME_MAXLEN + 1];
     581        snprintf(kbd, DEVMAP_NAME_MAXLEN, "%s/%s", NAMESPACE, NAME);
     582       
     583        devmap_handle_t devmap_handle;
     584        if (devmap_device_register(kbd, &devmap_handle) != EOK) {
     585                printf("%s: Unable to register device %s\n", NAME, kbd);
    672586                return -1;
    673587        }
Note: See TracChangeset for help on using the changeset viewer.