📄 hid-core.c
字号:
switch (urb->status) { case 0: /* success */ hid_input_report(HID_INPUT_REPORT, urb, regs); break; case -ECONNRESET: /* unlink */ case -ENOENT: case -ESHUTDOWN: return; default: /* error */ dbg("nonzero status in input irq %d", urb->status); } status = usb_submit_urb (urb, SLAB_ATOMIC); if (status) err ("can't resubmit intr, %s-%s/input%d, status %d", hid->dev->bus->bus_name, hid->dev->devpath, hid->ifnum, status);}/* * Output the field into the report. */static void hid_output_field(struct hid_field *field, __u8 *data){ unsigned count = field->report_count; unsigned offset = field->report_offset; unsigned size = field->report_size; unsigned n; for (n = 0; n < count; n++) { if (field->logical_minimum < 0) /* signed values */ implement(data, offset + n * size, size, s32ton(field->value[n], size)); else /* unsigned values */ implement(data, offset + n * size, size, field->value[n]); }}/* * Create a report. */static void hid_output_report(struct hid_report *report, __u8 *data){ unsigned n; if (report->id > 0) *data++ = report->id; for (n = 0; n < report->maxfield; n++) hid_output_field(report->field[n], data);}/* * Set a field value. The report this field belongs to has to be * created and transferred to the device, to set this value in the * device. */int hid_set_field(struct hid_field *field, unsigned offset, __s32 value){ unsigned size = field->report_size; hid_dump_input(field->usage + offset, value); if (offset >= field->report_count) { dbg("offset (%d) exceeds report_count (%d)", offset, field->report_count); hid_dump_field(field, 8); return -1; } if (field->logical_minimum < 0) { if (value != snto32(s32ton(value, size), size)) { dbg("value %d is out of range", value); return -1; } } field->value[offset] = value; return 0;}int hid_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field){ struct hid_report_enum *report_enum = hid->report_enum + HID_OUTPUT_REPORT; struct list_head *list = report_enum->report_list.next; int i, j; while (list != &report_enum->report_list) { struct hid_report *report = (struct hid_report *) list; list = list->next; for (i = 0; i < report->maxfield; i++) { *field = report->field[i]; for (j = 0; j < (*field)->maxusage; j++) if ((*field)->usage[j].type == type && (*field)->usage[j].code == code) return j; } } return -1;}/* * Find a report with a specified HID usage. */int hid_find_report_by_usage(struct hid_device *hid, __u32 wanted_usage, struct hid_report **report, int type){ struct hid_report_enum *report_enum = hid->report_enum + type; struct list_head *list = report_enum->report_list.next; int i, j; while (list != &report_enum->report_list) { *report = (struct hid_report *) list; list = list->next; for (i = 0; i < (*report)->maxfield; i++) { struct hid_field *field = (*report)->field[i]; for (j = 0; j < field->maxusage; j++) if (field->logical == wanted_usage) return j; } } return -1;}#if 0static int hid_find_field_in_report(struct hid_report *report, __u32 wanted_usage, struct hid_field **field){ int i, j; for (i = 0; i < report->maxfield; i++) { *field = report->field[i]; for (j = 0; j < (*field)->maxusage; j++) if ((*field)->usage[j].hid == wanted_usage) return j; } return -1;}#endifstatic int hid_submit_out(struct hid_device *hid){ struct hid_report *report; report = hid->out[hid->outtail]; hid_output_report(report, hid->outbuf); hid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0); hid->urbout->dev = hid->dev; dbg("submitting out urb"); if (usb_submit_urb(hid->urbout, GFP_ATOMIC)) { err("usb_submit_urb(out) failed"); return -1; } return 0;}static int hid_submit_ctrl(struct hid_device *hid){ struct hid_report *report; unsigned char dir; int len; report = hid->ctrl[hid->ctrltail].report; dir = hid->ctrl[hid->ctrltail].dir; len = ((report->size - 1) >> 3) + 1 + (report->id > 0); if (dir == USB_DIR_OUT) { hid_output_report(report, hid->ctrlbuf); hid->urbctrl->pipe = usb_sndctrlpipe(hid->dev, 0); hid->urbctrl->transfer_buffer_length = len; } else { int maxpacket, padlen; hid->urbctrl->pipe = usb_rcvctrlpipe(hid->dev, 0); maxpacket = usb_maxpacket(hid->dev, hid->urbctrl->pipe, 0); if (maxpacket > 0) { padlen = (len + maxpacket - 1) / maxpacket; padlen *= maxpacket; if (padlen > HID_BUFFER_SIZE) padlen = HID_BUFFER_SIZE; } else padlen = 0; hid->urbctrl->transfer_buffer_length = padlen; } hid->urbctrl->dev = hid->dev; hid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir; hid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT; hid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id); hid->cr->wIndex = cpu_to_le16(hid->ifnum); hid->cr->wLength = cpu_to_le16(len); dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u", hid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report", hid->cr->wValue, hid->cr->wIndex, hid->cr->wLength); if (usb_submit_urb(hid->urbctrl, GFP_ATOMIC)) { err("usb_submit_urb(ctrl) failed"); return -1; } return 0;}/* * Output interrupt completion handler. */static void hid_irq_out(struct urb *urb, struct pt_regs *regs){ struct hid_device *hid = urb->context; unsigned long flags; if (urb->status) warn("output irq status %d received", urb->status); spin_lock_irqsave(&hid->outlock, flags); hid->outtail = (hid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1); if (hid->outhead != hid->outtail) { hid_submit_out(hid); spin_unlock_irqrestore(&hid->outlock, flags); return; } clear_bit(HID_OUT_RUNNING, &hid->iofl); spin_unlock_irqrestore(&hid->outlock, flags); wake_up(&hid->wait);}/* * Control pipe completion handler. */static void hid_ctrl(struct urb *urb, struct pt_regs *regs){ struct hid_device *hid = urb->context; unsigned long flags; if (urb->status) warn("ctrl urb status %d received", urb->status); spin_lock_irqsave(&hid->ctrllock, flags); if (hid->ctrl[hid->ctrltail].dir == USB_DIR_IN) hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, regs); hid->ctrltail = (hid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1); if (hid->ctrlhead != hid->ctrltail) { hid_submit_ctrl(hid); spin_unlock_irqrestore(&hid->ctrllock, flags); return; } clear_bit(HID_CTRL_RUNNING, &hid->iofl); spin_unlock_irqrestore(&hid->ctrllock, flags); wake_up(&hid->wait);}void hid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir){ int head; unsigned long flags; if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN) return; if (hid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) { spin_lock_irqsave(&hid->outlock, flags); if ((head = (hid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == hid->outtail) { spin_unlock_irqrestore(&hid->outlock, flags); warn("output queue full"); return; } hid->out[hid->outhead] = report; hid->outhead = head; if (!test_and_set_bit(HID_OUT_RUNNING, &hid->iofl)) hid_submit_out(hid); spin_unlock_irqrestore(&hid->outlock, flags); return; } spin_lock_irqsave(&hid->ctrllock, flags); if ((head = (hid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == hid->ctrltail) { spin_unlock_irqrestore(&hid->ctrllock, flags); warn("control queue full"); return; } hid->ctrl[hid->ctrlhead].report = report; hid->ctrl[hid->ctrlhead].dir = dir; hid->ctrlhead = head; if (!test_and_set_bit(HID_CTRL_RUNNING, &hid->iofl)) hid_submit_ctrl(hid); spin_unlock_irqrestore(&hid->ctrllock, flags);}int hid_wait_io(struct hid_device *hid){ DECLARE_WAITQUEUE(wait, current); int timeout = 10*HZ; set_current_state(TASK_UNINTERRUPTIBLE); add_wait_queue(&hid->wait, &wait); while (timeout && (test_bit(HID_CTRL_RUNNING, &hid->iofl) || test_bit(HID_OUT_RUNNING, &hid->iofl))) timeout = schedule_timeout(timeout); set_current_state(TASK_RUNNING); remove_wait_queue(&hid->wait, &wait); if (!timeout) { dbg("timeout waiting for ctrl or out queue to clear"); return -1; } return 0;}static int hid_get_class_descriptor(struct usb_device *dev, int ifnum, unsigned char type, void *buf, int size){ return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN, (type << 8), ifnum, buf, size, HZ * USB_CTRL_GET_TIMEOUT);}int hid_open(struct hid_device *hid){ if (hid->open++) return 0; hid->urbin->dev = hid->dev; if (usb_submit_urb(hid->urbin, GFP_KERNEL)) return -EIO; return 0;}void hid_close(struct hid_device *hid){ if (!--hid->open) usb_unlink_urb(hid->urbin);}/* * Initialize all reports */void hid_init_reports(struct hid_device *hid){ struct hid_report_enum *report_enum; struct hid_report *report; struct list_head *list; int err, ret; /* * The Set_Idle request is supposed to affect only the * "Interrupt In" pipe. Unfortunately, buggy devices such as * the BTC keyboard (ID 046e:5303) the request also affects * Get_Report requests on the control pipe. In the worst * case, if the device was put on idle for an indefinite * amount of time (as we do below) and there are no input * events to report, the Get_Report requests will just hang * until we get a USB timeout. To avoid this, we temporarily * establish a minimal idle time of 1ms. This shouldn't hurt * bugfree devices and will cause a worst-case extra delay of * 1ms for buggy ones. */ usb_control_msg(hid->dev, usb_sndctrlpipe(hid->dev, 0), HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (1 << 8), hid->ifnum, NULL, 0, HZ * USB_CTRL_SET_TIMEOUT); report_enum = hid->report_enum + HID_INPUT_REPORT; list = report_enum->report_list.next; while (list != &report_enum->report_list) { report = (struct hid_report *) list; hid_submit_report(hid, report, USB_DIR_IN); list = list->next; } report_enum = hid->report_enum + HID_FEATURE_REPORT; list = report_enum->report_list.next; while (list != &report_enum->report_list) { report = (struct hid_report *) list; hid_submit_report(hid, report, USB_DIR_IN); list = list->next; } err = 0; ret = hid_wait_io(hid); while (ret) { err |= ret; if (test_bit(HID_CTRL_RUNNING, &hid->iofl)) usb_unlink_urb(hid->urbctrl); if (test_bit(HID_OUT_RUNNING, &hid->iofl)) usb_unlink_urb(hid->urbout); ret = hid_wait_io(hid); } if (err) warn("timeout initializing reports\n"); report_enum = hid->report_enum + HID_INPUT_REPORT; list = report_enum->report_list.next; while (list != &report_enum->report_list) { report = (struct hid_report *) list; usb_control_msg(hid->dev, usb_sndctrlpipe(hid->dev, 0), HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, report->id, hid->ifnum, NULL, 0, HZ * USB_CTRL_SET_TIMEOUT); list = list->next; }}#define USB_VENDOR_ID_WACOM 0x056a#define USB_DEVICE_ID_WACOM_PENPARTNER 0x0000#define USB_DEVICE_ID_WACOM_GRAPHIRE 0x0010#define USB_DEVICE_ID_WACOM_INTUOS 0x0020#define USB_DEVICE_ID_WACOM_PL 0x0030#define USB_DEVICE_ID_WACOM_INTUOS2 0x0040#define USB_DEVICE_ID_WACOM_VOLITO 0x0060#define USB_DEVICE_ID_WACOM_PTU 0x0003#define USB_VENDOR_ID_KBGEAR 0x084e#define USB_DEVICE_ID_KBGEAR_JAMSTUDIO 0x1001#define USB_VENDOR_ID_AIPTEK 0x08ca#define USB_DEVICE_ID_AIPTEK_01 0x0001#define USB_DEVICE_ID_AIPTEK_10 0x0010#define USB_DEVICE_ID_AIPTEK_20 0x0020#define USB_DEVICE_ID_AIPTEK_21 0x0021#define USB_DEVICE_ID_AIPTEK_22 0x0022#define USB_DEVICE_ID_AIPTEK_23 0x0023#define USB_DEVICE_ID_AIPTEK_24 0x0024#define USB_VENDOR_ID_GRIFFIN 0x077d#define USB_DEVICE_ID_POWERMATE 0x0410#define USB_DEVICE_ID_SOUNDKNOB 0x04AA#define USB_VENDOR_ID_ATEN 0x0557 #define USB_DEVICE_ID_ATEN_UC100KM 0x2004#define USB_DEVICE_ID_ATEN_CS124U 0x2202#define USB_DEVICE_ID_ATEN_2PORTKVM 0x2204#define USB_DEVICE_ID_ATEN_4PORTKVM 0x2205
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -