Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1638122
  • 博文数量: 311
  • 博客积分: 7778
  • 博客等级: 少将
  • 技术积分: 4186
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-09 19:59
个人简介

蓝点工坊(http://www.bluedrum.cn) 创始人,App和嵌入式产品开发。同时也做相应培训和外包工作。 详细介绍 http://pan.baidu.com/s/1y2g88

文章存档

2012年(3)

2011年(115)

2010年(170)

2009年(23)

分类: 嵌入式

2011-08-06 17:17:06

Andrew Huang 转载请注明作者及网址


 Linux下HID 设备,如果非标准的输入设备(Keypad/MOUSE/JoyStick/input event device).
将会把信息转入hid device的设备结点。

这一点可以参见内核的关于hiddev的文档

 它在 Documentation/usb/hiddev.txt 目录下,大致有这样一个流程,

  usb.c ---> hid-core.c  ----> hid-input.c ----> [keyboard/mouse/joystick/event]
                          |
                          |
                           --> hiddev.c ----> POWER / MONITOR CONTROL 

关于操作非标准的HID设备,可以参见如下文档.
  http://www.wetlogic.net/hiddev/

在设备文件系统中,会产生的/dev/hiddev0这样的结点,这类设备主设备号为 180,从设备号最低为96
mknod /dev/hiddev0 c 180 96
mknod /dev/hiddev1 c 180 97
mknod /dev/hiddev2 c 180 98
mknod /dev/hiddev3 c 180 99
    ...
mknod /dev/hiddev15 c 180 111


对此设备结点的处理有两种接口,一种是read(),另一种是ioctl();

  • read(): This is the event interface. When the HID device performs an interrupt transfer, indicating a change of state, data will be made available at the associated hiddev device with the content of a struct hiddev_event:struct hiddev_event { unsigned hid; signed int value; }; containing the HID usage identifier for the status that changed, and the value that it was changed to.
  • ioctl(): This is the control interface. There are a number of controls:
    HIDIOCGVERSIONint (read)Gets the version code out of the hiddev driver.
    HIDIOCAPPLICATION(none)This ioctl call returns the HID application usage associated with the hid device. The third argument to ioctl() specifies which application index to get. This is useful when the device has more than one application collection. If the index is invalid (greater or equal to the number of application collections this device has) the ioctl returns -1. You can find out beforehand how many application collections the device has from the num_applications field from the hiddev_devinfo structure.
    HIDIOCGDEVINFOstruct hiddev_devinfo (read)Gets a hiddev_devinfo structure which describes the device.
    HIDIOCGSTRINGstruct struct hiddev_string_descriptor (read/write)Gets a string descriptor from the device. The caller must fill in the "index" field to indicate which descriptor should be returned.
    HIDIOCINITREPORT Instructs the kernel to retrieve all input and feature report values from the device. At this point, all the usage structures will contain current values for the device, and will maintain it as the device changes.
    HIDIOCGNAMEstring (variable length)Gets the device name
    HIDIOCGREPORTstruct hiddev_report_info (write)Instructs the kernel to get a feature or input report from the device, in order to selectively update the usage structures (in contrast to INITREPORT).
    HIDIOCSREPORTstruct hiddev_report_info (write)Instructs the kernel to send a report to the device. This report can be filled in by the user throughHIDIOCSUSAGE calls (below) to fill in individual usage values in the report before sending the report in full to the device.
    HIDIOCGREPORTINFOstruct hiddev_report_info (read/write)Fills in a hiddev_report_info structure for the user. The report is looked up by type (input, output or feature) and id, so these fields must be filled in by the user. The ID can be absolute -- the actual report id as reported by the device -- or relative -- HID_REPORT_ID_FIRST for the first report, and (HID_REPORT_ID_NEXT | report_id) for the next report after report_id. Without a-priori information about report ids, the right way to use this ioctl is to use the relative IDs above to enumerate the valid IDs. The ioctl returns non-zero when there is no more next ID. The real report ID is filled into the returned hiddev_report_info structure.
    HIDIOCGFIELDINFOstruct hiddev_field_info (read/write)Returns the field information associated with a report in a hiddev_field_info structure. The user must fill in report_id and report_type in this structure, as above. The field_index should also be filled in, which should be a number from 0 and maxfield-1, as returned from a previous HIDIOCGREPORTINFO call.
    HIDIOCGUCODEstruct hiddev_usage_ref (read/write)Returns the usage_code in a hiddev_usage_ref structure, given that given its report type, report id, field index, and index within the field have already been filled into the structure.
    HIDIOCGUSAGEstruct hiddev_usage_ref (read/write)Returns the value of a usage in a hiddev_usage_ref structure. The usage to be retrieved can be specified as above, or the user can choose to fill in the report_type field and specify the report_id asHID_REPORT_ID_UNKNOWN. In this case, the hiddev_usage_ref will be filled in with the report and field infomation associated with this usage if it is found.
    HIDIOCSUSAGEstruct hiddev_usage_ref (write)Sets the value of a usage in an output report.
测试代码
  1. /*
  2.   Author : Andrew Huang <bluedrum@163.com>
  3.    <<Care and feeding of your Human Interface Devices>>
  4.    http://lxr.free-electrons.com/source/Documentation/usb/hiddev.txt?v=2.6.30
  5.    http://www.wetlogic.net/hiddev/
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <time.h>
  14. #include <sys/ioctl.h>
  15. #include <linux/hiddev.h>

  16. static void showReports(int fd, unsigned report_type);
  17. static void show_all_report(int fd);
  18. static int read_event(int fd);

  19. int main()
  20. {
  21.   char dev_name[64] = "/dev/hiddev0";
  22.   int fd = -1;
  23.   int version;
  24.     char name[100];
  25.     struct hiddev_devinfo dinfo;
  26.   
  27.   
  28.   fd = open(dev_name,O_RDWR);
  29.   if(fd == -1)
  30.     {
  31.        fprintf(stderr,"open %s failure\n",dev_name);
  32.        return -1;
  33.     }
  34.     
  35.     
  36.    printf("%s infor\n",dev_name);
  37.   
  38.        if (ioctl(fd, HIDIOCGVERSION, &version) < 0)
  39.             perror("HIDIOCGVERSION");
  40.         else
  41.         {
  42.             printf("HIDIOCGVERSION: %d.%d\n", (version>>16) & 0xFFFF, version & 0xFFFF);
  43.             if (version != HID_VERSION)
  44.                 printf("WARNING: version does not match compile-time version\n");
  45.         }
  46.         
  47.         if (ioctl(fd, HIDIOCGDEVINFO, &dinfo) < 0)
  48.             perror("HIDIOCGDEVINFO");
  49.         else
  50.         {
  51.             printf("HIDIOCGDEVINFO: bustype=%d busnum=%d devnum=%d ifnum=%d\n"
  52.                 "\tvendor=0x%04hx product=0x%04hx version=0x%04hx\n"
  53.                 "\tnum_applications=%d\n",
  54.                 dinfo.bustype, dinfo.busnum, dinfo.devnum, dinfo.ifnum,
  55.                 dinfo.vendor, dinfo.product, dinfo.version, dinfo.num_applications);
  56.         }
  57.         
  58.             if (ioctl(fd, HIDIOCGNAME(99), name) < 0)
  59.             perror("HIDIOCGNAME");
  60.         else
  61.         {
  62.             name[99] = 0;
  63.             printf("HIDIOCGNAME: %s\n", name);
  64.         }
  65.         
  66.     #if 0    
  67.         printf("\n*** INPUT:\n"); showReports(fd, HID_REPORT_TYPE_INPUT);
  68.             printf("\n*** OUTPUT:\n"); showReports(fd, HID_REPORT_TYPE_OUTPUT);
  69.             printf("\n*** FEATURE:\n"); showReports(fd, HID_REPORT_TYPE_FEATURE);
  70.   #endif
  71.     show_all_report(fd);    

  72.     read_event(fd);
  73.         
  74.         close(fd);
  75.         
  76.         
  77.   
  78. }




  79. static void showReports(int fd, unsigned report_type)
  80. {
  81.     struct hiddev_report_info rinfo;
  82.     struct hiddev_field_info finfo;
  83.     struct hiddev_usage_ref uref;
  84.     int i, j, ret;

  85.     rinfo.report_type = report_type;
  86.     rinfo.report_id = HID_REPORT_ID_FIRST;
  87.     ret = ioctl(fd, HIDIOCGREPORTINFO, &rinfo);
  88.     while (ret >= 0)
  89.     {
  90.         printf("HIDIOCGREPORTINFO: report_id=0x%X (%u fields)\n",
  91.             rinfo.report_id, rinfo.num_fields);
  92.         for (i = 0; i < rinfo.num_fields; i++)
  93.         {
  94.             finfo.report_type = rinfo.report_type;
  95.             finfo.report_id = rinfo.report_id;
  96.             finfo.field_index = i;
  97.             ioctl(fd, HIDIOCGFIELDINFO, &finfo);

  98.             printf("HIDIOCGFIELDINFO: field_index=%u maxusage=%u flags=0x%X\n"
  99.                 "\tphysical=0x%X logical=0x%X application=0x%X\n"
  100.                 "\tlogical_minimum=%d,maximum=%d physical_minimum=%d,maximum=%d\n",
  101.                 finfo.field_index, finfo.maxusage, finfo.flags,
  102.                 finfo.physical, finfo.logical, finfo.application,
  103.                 finfo.logical_minimum, finfo.logical_maximum,
  104.                 finfo.physical_minimum, finfo.physical_maximum);

  105.             for (j = 0; j < finfo.maxusage; j++)
  106.             {
  107.                 uref.report_type = finfo.report_type;
  108.                 uref.report_id = finfo.report_id;
  109.                 uref.field_index = i;
  110.                 uref.usage_index = j;
  111.                 ioctl(fd, HIDIOCGUCODE, &uref);
  112.                 ioctl(fd, HIDIOCGUSAGE, &uref);

  113.                 printf(" >> usage_index=%u usage_code=0x%X () value=%d\n",
  114.                     uref.usage_index,
  115.                     uref.usage_code,
  116.                 //    controlName(uref.usage_code),
  117.                     uref.value);

  118.             }
  119.         }
  120.         printf("\n");

  121.         rinfo.report_id |= HID_REPORT_ID_NEXT;
  122.         ret = ioctl(fd, HIDIOCGREPORTINFO, &rinfo);
  123.     }
  124. }


  125. void show_all_report(int fd)
  126. {
  127.     
  128.     struct hiddev_report_info rinfo;
  129.     struct hiddev_field_info finfo;
  130.     struct hiddev_usage_ref uref;
  131.     int rtype, i, j;
  132.     char *rtype_str;

  133.     for (rtype = HID_REPORT_TYPE_MIN; rtype <= HID_REPORT_TYPE_MAX;
  134.      rtype++) {
  135.      switch (rtype) {
  136.         case HID_REPORT_TYPE_INPUT: rtype_str = "Input"; break;
  137.         case HID_REPORT_TYPE_OUTPUT: rtype_str = "Output"; break;
  138.         case HID_REPORT_TYPE_FEATURE: rtype_str = "Feature"; break;
  139.         default: rtype_str = "Unknown"; break;
  140.      }
  141.      fprintf(stdout, "Reports of type %s (%d):\n", rtype_str, rtype);
  142.      rinfo.report_type = rtype;
  143.      rinfo.report_id = HID_REPORT_ID_FIRST;
  144.      while (ioctl(fd, HIDIOCGREPORTINFO, &rinfo) >= 0) {
  145.         fprintf(stdout, " Report id: %d (%d fields)\n",
  146.          rinfo.report_id, rinfo.num_fields);
  147.         for (i = 0; i < rinfo.num_fields; i++) {
  148.          memset(&finfo, 0, sizeof(finfo));
  149.          finfo.report_type = rinfo.report_type;
  150.          finfo.report_id = rinfo.report_id;
  151.          finfo.field_index = i;
  152.          ioctl(fd, HIDIOCGFIELDINFO, &finfo);
  153.          fprintf(stdout, " Field: %d: app: %04x phys %04x "
  154.              "flags %x (%d usages) unit %x exp %d\n",
  155.              i, finfo.application, finfo.physical, finfo.flags,
  156.              finfo.maxusage, finfo.unit, finfo.unit_exponent);
  157.          memset(&uref, 0, sizeof(uref));
  158.          for (j = 0; j < finfo.maxusage; j++) {
  159.             uref.report_type = finfo.report_type;
  160.             uref.report_id = finfo.report_id;
  161.             uref.field_index = i;
  162.             uref.usage_index = j;
  163.             ioctl(fd, HIDIOCGUCODE, &uref);
  164.             ioctl(fd, HIDIOCGUSAGE, &uref);
  165.             fprintf(stdout, " Usage: %04x val %d\n",
  166.                 uref.usage_code, uref.value);
  167.          }
  168.         }
  169.         rinfo.report_id |= HID_REPORT_ID_NEXT;
  170.      }
  171.     }
  172.     // if (!run_as_daemon)
  173.      fprintf(stdout, "Waiting for events ... (interrupt to exit)\n");
  174.     
  175. }

  176. int read_event(int fd)
  177. {
  178.      struct hiddev_event ev[64];
  179.      int i,rd;
  180.      time_t curr_time;
  181.      char name[100];
  182.     
  183.      while(1)
  184.      {
  185.      rd = read(fd, ev, sizeof(ev));
  186.      if (rd < (int) sizeof(ev[0])) {
  187.         if (rd < 0)
  188.          perror("\nevtest: error reading");
  189.     
  190.          return -1;
  191.      }
  192.     
  193.      for (i = 0; i < rd / sizeof(ev[0]); i++) {
  194.          //int idx = info_idx(ev[i].hid);
  195.          int idx = ev[i].hid;
  196.          curr_time = time(NULL);
  197.          strftime(name, sizeof(name), "%b %d %T",
  198.             localtime(&curr_time));
  199.          fprintf(stdout, "%s: Event: usage %x ( ), value %d\n",
  200.              name, ev[i].hid,
  201.              /* (idx >= 0) ? ups_info[idx].label : "Unknown",*/
  202.              ev[i].value);
  203.         }

  204.   }
  205.   
  206.   return 0;
  207.     
  208. }
阅读(7741) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

fbt0452018-08-17 13:42:53

请问想写数据改怎么实现?