OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [usb/] [hiddev.c] - Blame information for rev 1275

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
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
 
42
struct 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
 
52
struct 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
 
62
static struct hiddev *hiddev_table[HIDDEV_MINORS];
63
static 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
 */
71
static struct hid_report *
72
hiddev_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
 */
115
static struct hid_field *
116
hiddev_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
 
149
static 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
 */
174
void 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
 
194
void 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
 */
213
static 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
 */
224
static 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
 */
234
static 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
 */
261
static 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
 */
289
static 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
 */
298
static 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
 */
372
static 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
 */
389
static 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
                return copy_to_user((void *) arg, &dinfo, sizeof(dinfo));
437
 
438
        case HIDIOCGFLAG:
439
                return put_user(list->flags, (int *) arg);
440
 
441
        case HIDIOCSFLAG:
442
                {
443
                        int newflags;
444
                        if (get_user(newflags, (int *) arg))
445
                                return -EFAULT;
446
 
447
                        if ((newflags & ~HIDDEV_FLAGS) != 0 ||
448
                            ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
449
                             (newflags & HIDDEV_FLAG_UREF) == 0))
450
                                return -EINVAL;
451
 
452
                        list->flags = newflags;
453
 
454
                        return 0;
455
                }
456
 
457
        case HIDIOCGSTRING:
458
                {
459
                        int idx, len;
460
                        char *buf;
461
 
462
                        if (get_user(idx, (int *) arg))
463
                                return -EFAULT;
464
 
465
                        if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
466
                                return -ENOMEM;
467
 
468
                        if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
469
                                kfree(buf);
470
                                return -EINVAL;
471
                        }
472
 
473
                        if (copy_to_user((void *) (arg+sizeof(int)), buf, len+1)) {
474
                                kfree(buf);
475
                                return -EFAULT;
476
                        }
477
 
478
                        kfree(buf);
479
 
480
                        return len;
481
                }
482
 
483
        case HIDIOCINITREPORT:
484
                hid_init_reports(hid);
485
 
486
                return 0;
487
 
488
        case HIDIOCGREPORT:
489
                if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
490
                        return -EFAULT;
491
 
492
                if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
493
                        return -EINVAL;
494
 
495
                if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
496
                        return -EINVAL;
497
 
498
                hid_read_report(hid, report);
499
 
500
                return 0;
501
 
502
        case HIDIOCSREPORT:
503
                if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
504
                        return -EFAULT;
505
 
506
                if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
507
                        return -EINVAL;
508
 
509
                if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
510
                        return -EINVAL;
511
 
512
                hid_write_report(hid, report);
513
 
514
                return 0;
515
 
516
        case HIDIOCGREPORTINFO:
517
                if (copy_from_user(&rinfo, (void *) arg, sizeof(rinfo)))
518
                        return -EFAULT;
519
 
520
                if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
521
                        return -EINVAL;
522
 
523
                rinfo.num_fields = report->maxfield;
524
 
525
                return copy_to_user((void *) arg, &rinfo, sizeof(rinfo));
526
 
527
        case HIDIOCGFIELDINFO:
528
                if (copy_from_user(&finfo, (void *) arg, sizeof(finfo)))
529
                        return -EFAULT;
530
                rinfo.report_type = finfo.report_type;
531
                rinfo.report_id = finfo.report_id;
532
                if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
533
                        return -EINVAL;
534
 
535
                if (finfo.field_index >= report->maxfield)
536
                        return -EINVAL;
537
 
538
                field = report->field[finfo.field_index];
539
                memset(&finfo, 0, sizeof(finfo));
540
                finfo.report_type = rinfo.report_type;
541
                finfo.report_id = rinfo.report_id;
542
                finfo.field_index = field->report_count - 1;
543
                finfo.maxusage = field->maxusage;
544
                finfo.flags = field->flags;
545
                finfo.physical = field->physical;
546
                finfo.logical = field->logical;
547
                finfo.application = field->application;
548
                finfo.logical_minimum = field->logical_minimum;
549
                finfo.logical_maximum = field->logical_maximum;
550
                finfo.physical_minimum = field->physical_minimum;
551
                finfo.physical_maximum = field->physical_maximum;
552
                finfo.unit_exponent = field->unit_exponent;
553
                finfo.unit = field->unit;
554
 
555
                return copy_to_user((void *) arg, &finfo, sizeof(finfo));
556
 
557
        case HIDIOCGUCODE:
558
                if (copy_from_user(uref, (void *) arg, sizeof(*uref)))
559
                        return -EFAULT;
560
 
561
                rinfo.report_type = uref->report_type;
562
                rinfo.report_id = uref->report_id;
563
                if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
564
                        return -EINVAL;
565
 
566
                if (uref->field_index >= report->maxfield)
567
                        return -EINVAL;
568
 
569
                field = report->field[uref->field_index];
570
                if (uref->usage_index >= field->maxusage)
571
                        return -EINVAL;
572
 
573
                uref->usage_code = field->usage[uref->usage_index].hid;
574
 
575
                return copy_to_user((void *) arg, uref, sizeof(*uref));
576
 
577
        case HIDIOCGUSAGE:
578
        case HIDIOCSUSAGE:
579
        case HIDIOCGUSAGES:
580
        case HIDIOCSUSAGES:
581
        case HIDIOCGCOLLECTIONINDEX:
582
                if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
583
                        if (copy_from_user(&uref_multi, (void *) arg,
584
                                           sizeof(uref_multi)))
585
                                return -EFAULT;
586
                } else {
587
                        if (copy_from_user(uref, (void *) arg, sizeof(*uref)))
588
                                return -EFAULT;
589
                }
590
 
591
                if (cmd != HIDIOCGUSAGE &&
592
                    cmd != HIDIOCGUSAGES &&
593
                    uref->report_type == HID_REPORT_TYPE_INPUT)
594
                        return -EINVAL;
595
 
596
                if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
597
                        field = hiddev_lookup_usage(hid, uref);
598
                        if (field == NULL)
599
                                return -EINVAL;
600
                } else {
601
                        rinfo.report_type = uref->report_type;
602
                        rinfo.report_id = uref->report_id;
603
                        if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
604
                                return -EINVAL;
605
 
606
                        if (uref->field_index >= report->maxfield)
607
                                return -EINVAL;
608
 
609
                        field = report->field[uref->field_index];
610
                        if (uref->usage_index >= field->maxusage)
611
                                return -EINVAL;
612
 
613
                        if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
614
                                if (uref_multi.num_values >= HID_MAX_USAGES ||
615
                                    uref->usage_index >= field->maxusage ||
616
                                   (uref->usage_index + uref_multi.num_values) >= field->maxusage)
617
                                        return -EINVAL;
618
                        }
619
                }
620
 
621
                switch (cmd) {
622
                case HIDIOCGUSAGE:
623
                        uref->value = field->value[uref->usage_index];
624
                        return copy_to_user((void *) arg, uref, sizeof(*uref));
625
 
626
                case HIDIOCSUSAGE:
627
                        field->value[uref->usage_index] = uref->value;
628
                        return 0;
629
 
630
                case HIDIOCGCOLLECTIONINDEX:
631
                        return field->usage[uref->usage_index].collection_index;
632
                case HIDIOCGUSAGES:
633
                        for (i = 0; i < uref_multi.num_values; i++)
634
                                uref_multi.values[i] =
635
                                    field->value[uref->usage_index + i];
636
                        if (copy_to_user((void *) arg, &uref_multi,
637
                                         sizeof(uref_multi)))
638
                                return -EFAULT;
639
                        return 0;
640
                case HIDIOCSUSAGES:
641
                        for (i = 0; i < uref_multi.num_values; i++)
642
                                field->value[uref->usage_index + i] =
643
                                    uref_multi.values[i];
644
                        return 0;
645
                }
646
                break;
647
 
648
        case HIDIOCGCOLLECTIONINFO:
649
                if (copy_from_user(&cinfo, (void *) arg, sizeof(cinfo)))
650
                        return -EFAULT;
651
 
652
                if (cinfo.index >= hid->maxcollection)
653
                        return -EINVAL;
654
 
655
                cinfo.type = hid->collection[cinfo.index].type;
656
                cinfo.usage = hid->collection[cinfo.index].usage;
657
                cinfo.level = hid->collection[cinfo.index].level;
658
 
659
                return copy_to_user((void *) arg, &cinfo, sizeof(cinfo));
660
 
661
        default:
662
 
663
                if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
664
                        return -EINVAL;
665
 
666
                if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
667
                        int len;
668
                        if (!hid->name) return 0;
669
                        len = strlen(hid->name) + 1;
670
                        if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
671
                        return copy_to_user((char *) arg, hid->name, len) ?
672
                                -EFAULT : len;
673
                }
674
        }
675
        return -EINVAL;
676
}
677
 
678
static struct file_operations hiddev_fops = {
679
        owner:          THIS_MODULE,
680
        read:           hiddev_read,
681
        write:          hiddev_write,
682
        poll:           hiddev_poll,
683
        open:           hiddev_open,
684
        release:        hiddev_release,
685
        ioctl:          hiddev_ioctl,
686
        fasync:         hiddev_fasync,
687
};
688
 
689
/*
690
 * This is where hid.c calls us to connect a hid device to the hiddev driver
691
 */
692
int hiddev_connect(struct hid_device *hid)
693
{
694
        struct hiddev *hiddev;
695
        int minor, i;
696
        char devfs_name[16];
697
 
698
 
699
 
700
        if ((hid->quirks & HID_QUIRK_HIDDEV) == 0) {
701
                for (i = 0; i < hid->maxcollection; i++)
702
                        if (hid->collection[i].type ==
703
                            HID_COLLECTION_APPLICATION &&
704
                            !IS_INPUT_APPLICATION(hid->collection[i].usage))
705
                                break;
706
 
707
                if (i == hid->maxcollection)
708
                        return -1;
709
        }
710
 
711
        for (minor = 0; minor < HIDDEV_MINORS && hiddev_table[minor]; minor++);
712
        if (minor == HIDDEV_MINORS) {
713
                printk(KERN_ERR "hiddev: no more free hiddev devices\n");
714
                return -1;
715
        }
716
 
717
        if (!(hiddev = kmalloc(sizeof(struct hiddev), GFP_KERNEL)))
718
                return -1;
719
        memset(hiddev, 0, sizeof(struct hiddev));
720
 
721
        init_waitqueue_head(&hiddev->wait);
722
 
723
        hiddev->minor = minor;
724
        hiddev_table[minor] = hiddev;
725
 
726
        hiddev->hid = hid;
727
        hiddev->exist = 1;
728
 
729
        sprintf(devfs_name, "hiddev%d", minor);
730
        hiddev->devfs = devfs_register(hiddev_devfs_handle, devfs_name,
731
                                       DEVFS_FL_DEFAULT, USB_MAJOR,
732
                                       minor + HIDDEV_MINOR_BASE,
733
                                       S_IFCHR | S_IRUGO | S_IWUSR,
734
                                       &hiddev_fops, NULL);
735
        hid->minor = minor;
736
        hid->hiddev = hiddev;
737
 
738
        return 0;
739
}
740
 
741
/*
742
 * This is where hid.c calls us to disconnect a hiddev device from the
743
 * corresponding hid device (usually because the usb device has disconnected)
744
 */
745
void hiddev_disconnect(struct hid_device *hid)
746
{
747
        struct hiddev *hiddev = hid->hiddev;
748
 
749
        hiddev->exist = 0;
750
 
751
        if (hiddev->open) {
752
                hid_close(hiddev->hid);
753
                wake_up_interruptible(&hiddev->wait);
754
        } else {
755
                hiddev_cleanup(hiddev);
756
        }
757
}
758
 
759
/* Currently this driver is a USB driver.  It's not a conventional one in
760
 * the sense that it doesn't probe at the USB level.  Instead it waits to
761
 * be connected by HID through the hiddev_connect / hiddev_disconnect
762
 * routines.  The reason to register as a USB device is to gain part of the
763
 * minor number space from the USB major.
764
 *
765
 * In theory, should the HID code be generalized to more than one physical
766
 * medium (say, IEEE 1384), this driver will probably need to register its
767
 * own major number, and in doing so, no longer need to register with USB.
768
 * At that point the probe routine and hiddev_driver struct below will no
769
 * longer be useful.
770
 */
771
 
772
 
773
/* We never attach in this manner, and rely on HID to connect us.  This
774
 * is why there is no disconnect routine defined in the usb_driver either.
775
 */
776
static void *hiddev_usbd_probe(struct usb_device *dev, unsigned int ifnum,
777
                          const struct usb_device_id *hiddev_info)
778
{
779
        return NULL;
780
}
781
 
782
 
783
static /* const */ struct usb_driver hiddev_driver = {
784
        name:   "hiddev",
785
        probe:  hiddev_usbd_probe,
786
        fops:   &hiddev_fops,
787
        minor:  HIDDEV_MINOR_BASE
788
};
789
 
790
int __init hiddev_init(void)
791
{
792
        hiddev_devfs_handle =
793
                devfs_mk_dir(devfs_find_handle(NULL, "usb", 0, 0, 0, 0), "hid", NULL);
794
        usb_register(&hiddev_driver);
795
        return 0;
796
}
797
 
798
void __exit hiddev_exit(void)
799
{
800
        devfs_unregister(hiddev_devfs_handle);
801
        usb_deregister(&hiddev_driver);
802
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.