linux-old/drivers/usb/hiddev.c
<<
>>
Prefs
   1/*
   2 *  Copyright (c) 2001 Paul Stewart
   3 *  Copyright (c) 2001 Vojtech Pavlik
   4 *
   5 *  HID char devices, giving access to raw HID device events.
   6 *
   7 */
   8
   9/*
  10 * This program is free software; you can redistribute it and/or modify
  11 * it under the terms of the GNU General Public License as published by
  12 * the Free Software Foundation; either version 2 of the License, or
  13 * (at your option) any later version.
  14 *
  15 * This program is distributed in the hope that it will be useful,
  16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18 * GNU General Public License for more details.
  19 *
  20 * You should have received a copy of the GNU General Public License
  21 * along with this program; if not, write to the Free Software
  22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23 *
  24 * Should you need to contact me, the author, you can do so either by
  25 * e-mail - mail your message to Paul Stewart <stewart@wetlogic.net>
  26 */
  27
  28#define HIDDEV_MINOR_BASE       96
  29#define HIDDEV_MINORS           16
  30#define HIDDEV_BUFFER_SIZE      64
  31
  32#include <linux/poll.h>
  33#include <linux/slab.h>
  34#include <linux/module.h>
  35#include <linux/init.h>
  36#include <linux/smp_lock.h>
  37#include <linux/input.h>
  38#include <linux/usb.h>
  39#include "hid.h"
  40#include <linux/hiddev.h>
  41
  42struct hiddev {
  43        int exist;
  44        int open;
  45        int minor;
  46        wait_queue_head_t wait;
  47        devfs_handle_t devfs;
  48        struct hid_device *hid;
  49        struct hiddev_list *list;
  50};
  51
  52struct hiddev_list {
  53        struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
  54        int head;
  55        int tail;
  56        unsigned flags;
  57        struct fasync_struct *fasync;
  58        struct hiddev *hiddev;
  59        struct hiddev_list *next;
  60};
  61
  62static struct hiddev *hiddev_table[HIDDEV_MINORS];
  63static devfs_handle_t hiddev_devfs_handle;
  64
  65/*
  66 * Find a report, given the report's type and ID.  The ID can be specified
  67 * indirectly by REPORT_ID_FIRST (which returns the first report of the given
  68 * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
  69 * given type which follows old_id.
  70 */
  71static struct hid_report *
  72hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
  73{
  74        unsigned flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
  75        struct hid_report_enum *report_enum = NULL;
  76        struct list_head *list;
  77
  78        if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
  79            rinfo->report_type > HID_REPORT_TYPE_MAX) return NULL;
  80
  81        report_enum = hid->report_enum +
  82                (rinfo->report_type - HID_REPORT_TYPE_MIN);
  83              
  84        switch (flags) {
  85        case 0: /* Nothing to do -- report_id is already set correctly */
  86                break;
  87
  88        case HID_REPORT_ID_FIRST:
  89                list = report_enum->report_list.next;
  90                if (list == &report_enum->report_list) return NULL;
  91                rinfo->report_id = ((struct hid_report *) list)->id;
  92                break;
  93                
  94        case HID_REPORT_ID_NEXT:
  95                list = (struct list_head *)
  96                        report_enum->report_id_hash[rinfo->report_id &
  97                                                    HID_REPORT_ID_MASK];
  98                if (list == NULL) return NULL;
  99                list = list->next;
 100                if (list == &report_enum->report_list) return NULL;
 101                rinfo->report_id = ((struct hid_report *) list)->id;
 102                break;
 103                
 104        default:
 105                return NULL;
 106        }
 107
 108        return report_enum->report_id_hash[rinfo->report_id];
 109}
 110
 111/*
 112 * Perform an exhaustive search of the report table for a usage, given its
 113 * type and usage id.
 114 */
 115static struct hid_field *
 116hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
 117{
 118        int i, j;
 119        struct hid_report *report;
 120        struct hid_report_enum *report_enum;
 121        struct list_head *list;
 122        struct hid_field *field;
 123
 124        if (uref->report_type < HID_REPORT_TYPE_MIN ||
 125            uref->report_type > HID_REPORT_TYPE_MAX) return NULL;
 126
 127        report_enum = hid->report_enum +
 128                (uref->report_type - HID_REPORT_TYPE_MIN);
 129        list = report_enum->report_list.next;
 130        while (list != &report_enum->report_list) {
 131                report = (struct hid_report *) list;
 132                for (i = 0; i < report->maxfield; i++) {
 133                        field = report->field[i];
 134                        for (j = 0; j < field->maxusage; j++) {
 135                                if (field->usage[j].hid == uref->usage_code) {
 136                                        uref->report_id = report->id;
 137                                        uref->field_index = i;
 138                                        uref->usage_index = j;
 139                                        return field;
 140                                }
 141                        }
 142                }
 143                list = list->next;
 144        }
 145
 146        return NULL;
 147}
 148
 149static void hiddev_send_event(struct hid_device *hid,
 150                              struct hiddev_usage_ref *uref)
 151{
 152        struct hiddev *hiddev = hid->hiddev;
 153        struct hiddev_list *list = hiddev->list;
 154
 155        while (list) {
 156                if (uref->field_index != HID_FIELD_INDEX_NONE ||
 157                    (list->flags & HIDDEV_FLAG_REPORT) != 0) {
 158                        list->buffer[list->head] = *uref;
 159                        list->head = (list->head + 1) & 
 160                                (HIDDEV_BUFFER_SIZE - 1);
 161                        kill_fasync(&list->fasync, SIGIO, POLL_IN);
 162                }
 163
 164                list = list->next;
 165        }
 166
 167        wake_up_interruptible(&hiddev->wait);
 168}
 169
 170/*
 171 * This is where hid.c calls into hiddev to pass an event that occurred over
 172 * the interrupt pipe
 173 */
 174void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
 175                      struct hid_usage *usage, __s32 value)
 176{
 177        unsigned type = field->report_type;
 178        struct hiddev_usage_ref uref;
 179
 180        uref.report_type = 
 181          (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
 182          ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT : 
 183           ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE:0));
 184        uref.report_id = field->report->id;
 185        uref.field_index = field->index;
 186        uref.usage_index = (usage - field->usage);
 187        uref.usage_code = usage->hid;
 188        uref.value = value;
 189
 190        hiddev_send_event(hid, &uref);
 191}
 192
 193
 194void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
 195{
 196        unsigned type = report->type;
 197        struct hiddev_usage_ref uref;
 198
 199        memset(&uref, 0, sizeof(uref));
 200        uref.report_type = 
 201          (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
 202          ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT : 
 203           ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE:0));
 204        uref.report_id = report->id;
 205        uref.field_index = HID_FIELD_INDEX_NONE;
 206
 207        hiddev_send_event(hid, &uref);
 208}
 209
 210/*
 211 * fasync file op
 212 */
 213static int hiddev_fasync(int fd, struct file *file, int on)
 214{
 215        int retval;
 216        struct hiddev_list *list = file->private_data;
 217        retval = fasync_helper(fd, file, on, &list->fasync);
 218        return retval < 0 ? retval : 0;
 219}
 220
 221/*
 222 * De-allocate a hiddev structure
 223 */
 224static void hiddev_cleanup(struct hiddev *hiddev)
 225{
 226        devfs_unregister(hiddev->devfs);
 227        hiddev_table[hiddev->minor] = NULL;
 228        kfree(hiddev);
 229}
 230
 231/*
 232 * release file op
 233 */
 234static int hiddev_release(struct inode * inode, struct file * file)
 235{
 236        struct hiddev_list *list = file->private_data;
 237        struct hiddev_list **listptr;
 238
 239        listptr = &list->hiddev->list;
 240        hiddev_fasync(-1, file, 0);
 241
 242        while (*listptr && (*listptr != list))
 243                listptr = &((*listptr)->next);
 244        *listptr = (*listptr)->next;
 245
 246        if (!--list->hiddev->open) {
 247                if (list->hiddev->exist) 
 248                        hid_close(list->hiddev->hid);
 249                else
 250                        hiddev_cleanup(list->hiddev);
 251        }
 252
 253        kfree(list);
 254
 255        return 0;
 256}
 257
 258/*
 259 * open file op
 260 */
 261static int hiddev_open(struct inode * inode, struct file * file) {
 262        struct hiddev_list *list;
 263
 264        int i = MINOR(inode->i_rdev) - HIDDEV_MINOR_BASE;
 265
 266        if (i >= HIDDEV_MINORS || !hiddev_table[i])
 267                return -ENODEV;
 268
 269        if (!(list = kmalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
 270                return -ENOMEM;
 271        memset(list, 0, sizeof(struct hiddev_list));
 272
 273        list->hiddev = hiddev_table[i];
 274        list->next = hiddev_table[i]->list;
 275        hiddev_table[i]->list = list;
 276
 277        file->private_data = list;
 278
 279        if (!list->hiddev->open++)
 280                if (list->hiddev->exist)
 281                        hid_open(hiddev_table[i]->hid);
 282
 283        return 0;
 284}
 285
 286/*
 287 * "write" file op
 288 */
 289static ssize_t hiddev_write(struct file * file, const char * buffer,
 290                            size_t count, loff_t *ppos)
 291{
 292        return -EINVAL;
 293}
 294
 295/*
 296 * "read" file op
 297 */
 298static ssize_t hiddev_read(struct file * file, char * buffer, size_t count,
 299                           loff_t *ppos)
 300{
 301        DECLARE_WAITQUEUE(wait, current);
 302        struct hiddev_list *list = file->private_data;
 303        int event_size;
 304        int retval = 0;
 305
 306        event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
 307                sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
 308
 309        if (count < event_size) return 0;
 310
 311        while (retval == 0) {
 312                if (list->head == list->tail) {
 313                        add_wait_queue(&list->hiddev->wait, &wait);
 314                        set_current_state(TASK_INTERRUPTIBLE);
 315                        
 316                        while (list->head == list->tail) {
 317                                if (file->f_flags & O_NONBLOCK) {
 318                                        retval = -EAGAIN;
 319                                        break;
 320                                }
 321                                if (signal_pending(current)) {
 322                                        retval = -ERESTARTSYS;
 323                                        break;
 324                                }
 325                                if (!list->hiddev->exist) {
 326                                        retval = -EIO;
 327                                        break;
 328                                }
 329                                
 330                                schedule();
 331                        }
 332
 333                        set_current_state(TASK_RUNNING);
 334                        remove_wait_queue(&list->hiddev->wait, &wait);
 335                }
 336
 337                if (retval)
 338                        return retval;
 339
 340                while (list->head != list->tail && 
 341                       retval + event_size <= count) {
 342                        if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
 343                                if (list->buffer[list->tail].field_index !=
 344                                    HID_FIELD_INDEX_NONE) {
 345                                        struct hiddev_event event;
 346                                        event.hid = list->buffer[list->tail].usage_code;
 347                                        event.value = list->buffer[list->tail].value;
 348                                        if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event)))
 349                                                return -EFAULT;
 350                                        retval += sizeof(struct hiddev_event);
 351                                }
 352                        } else {
 353                                if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
 354                                    (list->flags & HIDDEV_FLAG_REPORT) != 0) {
 355                                        if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref)))
 356                                                return -EFAULT;
 357                                        retval += sizeof(struct hiddev_usage_ref);
 358                                }
 359                        }
 360                        list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
 361                }
 362
 363        }
 364
 365        return retval;
 366}
 367
 368/*
 369 * "poll" file op
 370 * No kernel lock - fine
 371 */
 372static unsigned int hiddev_poll(struct file *file, poll_table *wait)
 373{
 374        struct hiddev_list *list = file->private_data;
 375        poll_wait(file, &list->hiddev->wait, wait);
 376        if (list->head != list->tail)
 377                return POLLIN | POLLRDNORM;
 378        if (!list->hiddev->exist)
 379                return POLLERR | POLLHUP;
 380        return 0;
 381}
 382
 383#define GET_TIMEOUT 3
 384#define SET_TIMEOUT 3
 385
 386/*
 387 * "ioctl" file op
 388 */
 389static int hiddev_ioctl(struct inode *inode, struct file *file,
 390                        unsigned int cmd, unsigned long arg)
 391{
 392        struct hiddev_list *list = file->private_data;
 393        struct hiddev *hiddev = list->hiddev;
 394        struct hid_device *hid = hiddev->hid;
 395        struct usb_device *dev = hid->dev;
 396        struct hiddev_collection_info cinfo;
 397        struct hiddev_report_info rinfo;
 398        struct hiddev_field_info finfo;
 399        struct hiddev_usage_ref_multi uref_multi;
 400        struct hiddev_usage_ref *uref = &uref_multi.uref;
 401        struct hiddev_devinfo dinfo;
 402        struct hid_report *report;
 403        struct hid_field *field;
 404        int i;
 405
 406        if (!hiddev->exist) return -EIO;
 407
 408        switch (cmd) {
 409
 410        case HIDIOCGVERSION:
 411                return put_user(HID_VERSION, (int *) arg);
 412
 413        case HIDIOCAPPLICATION:
 414                if (arg < 0 || arg >= hid->maxapplication)
 415                        return -EINVAL;
 416
 417                for (i = 0; i < hid->maxcollection; i++)
 418                        if (hid->collection[i].type == 
 419                            HID_COLLECTION_APPLICATION && arg-- == 0)
 420                                break;
 421                
 422                if (i == hid->maxcollection)
 423                        return -EINVAL;
 424
 425                return hid->collection[i].usage;
 426
 427        case HIDIOCGDEVINFO:
 428                dinfo.bustype = BUS_USB;
 429                dinfo.busnum = dev->bus->busnum;
 430                dinfo.devnum = dev->devnum;
 431                dinfo.ifnum = hid->ifnum;
 432                dinfo.vendor = dev->descriptor.idVendor;
 433                dinfo.product = dev->descriptor.idProduct;
 434                dinfo.version = dev->descriptor.bcdDevice;
 435                dinfo.num_applications = hid->maxapplication;
 436                if (copy_to_user((void *) arg, &dinfo, sizeof(dinfo)))
 437                        return -EFAULT;
 438                return 0;
 439
 440        case HIDIOCGFLAG:
 441                return put_user(list->flags, (int *) arg);
 442
 443        case HIDIOCSFLAG:
 444                {
 445                        int newflags;
 446                        if (get_user(newflags, (int *) arg))
 447                                return -EFAULT;
 448
 449                        if ((newflags & ~HIDDEV_FLAGS) != 0 ||
 450                            ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
 451                             (newflags & HIDDEV_FLAG_UREF) == 0))
 452                                return -EINVAL;
 453
 454                        list->flags = newflags;
 455
 456                        return 0;
 457                }
 458
 459        case HIDIOCGSTRING:
 460                {
 461                        int idx, len;
 462                        char *buf;
 463
 464                        if (get_user(idx, (int *) arg))
 465                                return -EFAULT;
 466
 467                        if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
 468                                return -ENOMEM;
 469
 470                        if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
 471                                kfree(buf);
 472                                return -EINVAL;
 473                        }
 474
 475                        if (copy_to_user((void *) (arg+sizeof(int)), buf, len+1)) {
 476                                kfree(buf);
 477                                return -EFAULT;
 478                        }
 479
 480                        kfree(buf);
 481
 482                        return len;
 483                }
 484
 485        case HIDIOCINITREPORT:
 486                hid_init_reports(hid);
 487
 488                return 0;
 489
 490        case HIDIOCGREPORT:
 491                if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
 492                        return -EFAULT;
 493
 494                if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
 495                        return -EINVAL;
 496
 497                if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
 498                        return -EINVAL;
 499
 500                hid_read_report(hid, report);
 501
 502                return 0;
 503
 504        case HIDIOCSREPORT:
 505                if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
 506                        return -EFAULT;
 507
 508                if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
 509                        return -EINVAL;
 510
 511                if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
 512                        return -EINVAL;
 513
 514                hid_write_report(hid, report);
 515
 516                return 0;
 517
 518        case HIDIOCGREPORTINFO:
 519                if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
 520                        return -EFAULT;
 521
 522                if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
 523                        return -EINVAL;
 524
 525                rinfo.num_fields = report->maxfield;
 526
 527                if (copy_to_user((void *) arg, &rinfo, sizeof(rinfo)))
 528                        return -EFAULT;
 529                return 0;
 530
 531        case HIDIOCGFIELDINFO:
 532                if (copy_from_user(&finfo, (void *) arg, sizeof(finfo)))
 533                        return -EFAULT;
 534                rinfo.report_type = finfo.report_type;
 535                rinfo.report_id = finfo.report_id;
 536                if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
 537                        return -EINVAL;
 538
 539                if (finfo.field_index >= report->maxfield)
 540                        return -EINVAL;
 541
 542                field = report->field[finfo.field_index];
 543                memset(&finfo, 0, sizeof(finfo));
 544                finfo.report_type = rinfo.report_type;
 545                finfo.report_id = rinfo.report_id;
 546                finfo.field_index = field->report_count - 1;
 547                finfo.maxusage = field->maxusage;
 548                finfo.flags = field->flags;
 549                finfo.physical = field->physical;
 550                finfo.logical = field->logical;
 551                finfo.application = field->application;
 552                finfo.logical_minimum = field->logical_minimum;
 553                finfo.logical_maximum = field->logical_maximum;
 554                finfo.physical_minimum = field->physical_minimum;
 555                finfo.physical_maximum = field->physical_maximum;
 556                finfo.unit_exponent = field->unit_exponent;
 557                finfo.unit = field->unit;
 558
 559                if (copy_to_user((void *) arg, &finfo, sizeof(finfo)))
 560                        return -EFAULT;
 561                return 0;
 562
 563        case HIDIOCGUCODE:
 564                if (copy_from_user(uref, (void *) arg, sizeof(*uref)))
 565                        return -EFAULT;
 566
 567                rinfo.report_type = uref->report_type;
 568                rinfo.report_id = uref->report_id;
 569                if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
 570                        return -EINVAL;
 571
 572                if (uref->field_index >= report->maxfield)
 573                        return -EINVAL;
 574
 575                field = report->field[uref->field_index];
 576                if (uref->usage_index >= field->maxusage)
 577                        return -EINVAL;
 578
 579                uref->usage_code = field->usage[uref->usage_index].hid;
 580
 581                if (copy_to_user((void *) arg, uref, sizeof(*uref)))
 582                        return -EFAULT;
 583                return 0;
 584
 585        case HIDIOCGUSAGE:
 586        case HIDIOCSUSAGE:
 587        case HIDIOCGUSAGES:
 588        case HIDIOCSUSAGES:
 589        case HIDIOCGCOLLECTIONINDEX:
 590                if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
 591                        if (copy_from_user(&uref_multi, (void *) arg, 
 592                                           sizeof(uref_multi)))
 593                                return -EFAULT;
 594                } else {
 595                        if (copy_from_user(uref, (void *) arg, sizeof(*uref)))
 596                                return -EFAULT;
 597                }
 598
 599                if (cmd != HIDIOCGUSAGE && 
 600                    cmd != HIDIOCGUSAGES &&
 601                    uref->report_type == HID_REPORT_TYPE_INPUT)
 602                        return -EINVAL;
 603
 604                if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
 605                        field = hiddev_lookup_usage(hid, uref);
 606                        if (field == NULL)
 607                                return -EINVAL;
 608                } else {
 609                        rinfo.report_type = uref->report_type;
 610                        rinfo.report_id = uref->report_id;
 611                        if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
 612                                return -EINVAL;
 613
 614                        if (uref->field_index >= report->maxfield)
 615                                return -EINVAL;
 616
 617                        field = report->field[uref->field_index];
 618                        if (uref->usage_index >= field->maxusage)
 619                                return -EINVAL;
 620
 621                        if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
 622                                if (uref_multi.num_values >= HID_MAX_USAGES || 
 623                                    uref->usage_index >= field->maxusage || 
 624                                   (uref->usage_index + uref_multi.num_values) >= field->maxusage)
 625                                        return -EINVAL;
 626                        }
 627                }
 628
 629                switch (cmd) {
 630                case HIDIOCGUSAGE:
 631                        uref->value = field->value[uref->usage_index];
 632                        return copy_to_user((void *) arg, uref, sizeof(*uref));
 633
 634                case HIDIOCSUSAGE:
 635                        field->value[uref->usage_index] = uref->value;
 636                        return 0;
 637
 638                case HIDIOCGCOLLECTIONINDEX:
 639                        return field->usage[uref->usage_index].collection_index;
 640                case HIDIOCGUSAGES:
 641                        for (i = 0; i < uref_multi.num_values; i++)
 642                                uref_multi.values[i] = 
 643                                    field->value[uref->usage_index + i];
 644                        if (copy_to_user((void *) arg, &uref_multi, 
 645                                         sizeof(uref_multi)))
 646                                return -EFAULT;
 647                        return 0;
 648                case HIDIOCSUSAGES:
 649                        for (i = 0; i < uref_multi.num_values; i++)
 650                                field->value[uref->usage_index + i] = 
 651                                    uref_multi.values[i];
 652                        return 0;
 653                }
 654                break;
 655
 656        case HIDIOCGCOLLECTIONINFO:
 657                if (copy_from_user(&cinfo, (void *) arg, sizeof(cinfo)))
 658                        return -EFAULT;
 659
 660                if (cinfo.index >= hid->maxcollection)
 661                        return -EINVAL;
 662
 663                cinfo.type = hid->collection[cinfo.index].type;
 664                cinfo.usage = hid->collection[cinfo.index].usage;
 665                cinfo.level = hid->collection[cinfo.index].level;
 666
 667                if (copy_to_user((void *) arg, &cinfo, sizeof(cinfo)))
 668                        return -EFAULT;
 669                return 0;
 670
 671        default:
 672
 673                if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
 674                        return -EINVAL;
 675
 676                if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
 677                        int len;
 678                        if (!hid->name) return 0;
 679                        len = strlen(hid->name) + 1;
 680                        if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
 681                        return copy_to_user((char *) arg, hid->name, len) ?
 682                                -EFAULT : len;
 683                }
 684        }
 685        return -EINVAL;
 686}
 687
 688static struct file_operations hiddev_fops = {
 689        owner:          THIS_MODULE,
 690        read:           hiddev_read,
 691        write:          hiddev_write,
 692        poll:           hiddev_poll,
 693        open:           hiddev_open,
 694        release:        hiddev_release,
 695        ioctl:          hiddev_ioctl,
 696        fasync:         hiddev_fasync,
 697};
 698
 699/*
 700 * This is where hid.c calls us to connect a hid device to the hiddev driver
 701 */
 702int hiddev_connect(struct hid_device *hid)
 703{
 704        struct hiddev *hiddev;
 705        int minor, i;
 706        char devfs_name[16];
 707
 708
 709
 710        if ((hid->quirks & HID_QUIRK_HIDDEV) == 0) {
 711                for (i = 0; i < hid->maxcollection; i++)
 712                        if (hid->collection[i].type == 
 713                            HID_COLLECTION_APPLICATION &&
 714                            !IS_INPUT_APPLICATION(hid->collection[i].usage))
 715                                break;
 716
 717                if (i == hid->maxcollection)
 718                        return -1;
 719        }
 720
 721        for (minor = 0; minor < HIDDEV_MINORS && hiddev_table[minor]; minor++);
 722        if (minor == HIDDEV_MINORS) {
 723                printk(KERN_ERR "hiddev: no more free hiddev devices\n");
 724                return -1;
 725        }
 726
 727        if (!(hiddev = kmalloc(sizeof(struct hiddev), GFP_KERNEL)))
 728                return -1;
 729        memset(hiddev, 0, sizeof(struct hiddev));
 730
 731        init_waitqueue_head(&hiddev->wait);
 732
 733        hiddev->minor = minor;
 734        hiddev_table[minor] = hiddev;
 735
 736        hiddev->hid = hid;
 737        hiddev->exist = 1;
 738
 739        sprintf(devfs_name, "hiddev%d", minor);
 740        hiddev->devfs = devfs_register(hiddev_devfs_handle, devfs_name,
 741                                       DEVFS_FL_DEFAULT, USB_MAJOR,
 742                                       minor + HIDDEV_MINOR_BASE,
 743                                       S_IFCHR | S_IRUGO | S_IWUSR,
 744                                       &hiddev_fops, NULL);
 745        hid->minor = minor;
 746        hid->hiddev = hiddev;
 747
 748        return 0;
 749}
 750
 751/*
 752 * This is where hid.c calls us to disconnect a hiddev device from the
 753 * corresponding hid device (usually because the usb device has disconnected)
 754 */
 755void hiddev_disconnect(struct hid_device *hid)
 756{
 757        struct hiddev *hiddev = hid->hiddev;
 758
 759        hiddev->exist = 0;
 760
 761        if (hiddev->open) {
 762                hid_close(hiddev->hid);
 763                wake_up_interruptible(&hiddev->wait);
 764        } else {
 765                hiddev_cleanup(hiddev);
 766        }
 767}
 768
 769/* Currently this driver is a USB driver.  It's not a conventional one in
 770 * the sense that it doesn't probe at the USB level.  Instead it waits to
 771 * be connected by HID through the hiddev_connect / hiddev_disconnect
 772 * routines.  The reason to register as a USB device is to gain part of the
 773 * minor number space from the USB major.
 774 *
 775 * In theory, should the HID code be generalized to more than one physical
 776 * medium (say, IEEE 1384), this driver will probably need to register its
 777 * own major number, and in doing so, no longer need to register with USB.
 778 * At that point the probe routine and hiddev_driver struct below will no
 779 * longer be useful.
 780 */
 781
 782
 783/* We never attach in this manner, and rely on HID to connect us.  This
 784 * is why there is no disconnect routine defined in the usb_driver either.
 785 */
 786static void *hiddev_usbd_probe(struct usb_device *dev, unsigned int ifnum,
 787                          const struct usb_device_id *hiddev_info)
 788{
 789        return NULL;
 790}
 791
 792
 793static /* const */ struct usb_driver hiddev_driver = {
 794        name:   "hiddev",
 795        probe:  hiddev_usbd_probe,
 796        fops:   &hiddev_fops,
 797        minor:  HIDDEV_MINOR_BASE
 798};
 799
 800int __init hiddev_init(void)
 801{
 802        hiddev_devfs_handle =
 803                devfs_mk_dir(devfs_find_handle(NULL, "usb", 0, 0, 0, 0), "hid", NULL);
 804        usb_register(&hiddev_driver);
 805        return 0;
 806}
 807
 808void __exit hiddev_exit(void)
 809{
 810        devfs_unregister(hiddev_devfs_handle);
 811        usb_deregister(&hiddev_driver);
 812}
 813
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.