当前位置:首页 > 芯闻号 > 充电吧
[导读]作者: Sam(甄峰) sam_code@hotmail.com   1. 解读hiddevice probe程序: static int hid_probe(struct usb_interface

作者: Sam(甄峰) sam_code@hotmail.com

 

1. 解读hiddevice probe程序:

static int hid_probe(struct usb_interface*intf, const struct usb_device_id *id)
{
 struct hid_device *hid;
 char path[64];
 int i;
 char *c;

 dbg_hid("HID probecalled for ifnum %dn",
   intf->altsetting->desc.bInterfaceNumber);

 if (!(hid =usb_hid_configure(intf)))
  return -ENODEV;

 usbhid_init_reports(hid);
 hid_dump_device(hid);
 if (hid->quirks &HID_QUIRK_RESET_LEDS)
  usbhid_set_leds(hid);

 if(!hidinput_connect(hid))
  hid->claimed |=HID_CLAIMED_INPUT;
 if (!hiddev_connect(hid))
  hid->claimed |=HID_CLAIMED_HIDDEV;

 usb_set_intfdata(intf,hid);

 if(!hid->claimed) {
  printk ("HID device not claimedby input or hiddevn");
  hid_disconnect(intf);
  return -ENODEV;
 }

 if((hid->claimed &HID_CLAIMED_INPUT))
  hid_ff_init(hid);

 if(hid->quirks &HID_QUIRK_SONY_PS3_CONTROLLER)
  hid_fixup_sony_ps3_controller(interface_to_usbdev(intf),
   intf->cur_altsetting->desc.bInterfaceNumber);

 printk(KERN_INFO);

 if(hid->claimed &HID_CLAIMED_INPUT)
  printk("input");
 if (hid->claimed ==(HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
  printk(",");
 if (hid->claimed &HID_CLAIMED_HIDDEV)
  printk("hiddev%d",hid->minor);

 c = "Device";
 for (i = 0; i maxcollection; i++) {
  if(hid->collection[i].type ==HID_COLLECTION_APPLICATION &&
     (hid->collection[i].usage &HID_USAGE_PAGE) == HID_UP_GENDESK&&
     (hid->collection[i].usage & 0xffff)< ARRAY_SIZE(hid_types)) {
   c =hid_types[hid->collection[i].usage &0xffff];
   break;
  }
 }

 usb_make_path(interface_to_usbdev(intf),path, 63);

 printk(": USB HIDv%x.%02x %s [%s] on %sn",
  hid->version>> 8, hid->version& 0xff, c, hid->name,path);

 return 0;
}

1.1:usb_hid_configure(intf)解读:

从字面来看,它是指配置hid。

static struct hid_device*usb_hid_configure(struct usb_interface *intf)
{
 struct usb_host_interface *interface =intf->cur_altsetting;
 struct usb_device *dev = interface_to_usbdev(intf);
 struct hid_descriptor *hdesc;
 struct hid_device *hid;
 u32 quirks = 0;
 unsigned rsize = 0;
 char *rdesc;
 int n, len, insize = 0;
 struct usbhid_device *usbhid;

 

//得到对应vid,pid的quriks.如果没有,则返回0
 quirks =usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
   le16_to_cpu(dev->descriptor.idProduct));

 

//如果为boot设备且为keyboard或mouse.则quirks=HID_QUIRK_NOGET

 
 if(interface->desc.bInterfaceSubClass ==USB_INTERFACE_SUBCLASS_BOOT) {
  if(interface->desc.bInterfaceProtocol ==USB_INTERFACE_PROTOCOL_KEYBOARD ||
   interface->desc.bInterfaceProtocol== USB_INTERFACE_PROTOCOL_MOUSE)
    quirks|= HID_QUIRK_NOGET;
 }

 

//如果quirks显示要忽略,则退出probe

 if (quirks& HID_QUIRK_IGNORE)
  return NULL;

 

//HID_QUIRK_IGNORE_MOUSE表示如果为mouse,则忽略。

 if ((quirks& HID_QUIRK_IGNORE_MOUSE)&&
  (interface->desc.bInterfaceProtocol== USB_INTERFACE_PROTOCOL_MOUSE))
   returnNULL;

 

//如果interface扩展描述符中没有类型为HID_DT_HID条目,或者interface包含的endpoint数目为0,又或者interfaceendpoint中扩展描述符中没有类型为HID_DT_HID的条目。则退出。
 if(usb_get_extra_descriptor(interface, HID_DT_HID,&hdesc) &&
    (!interface->desc.bNumEndpoints ||
     usb_get_extra_descriptor(&interface->endpoint[0],HID_DT_HID, &hdesc))) {
  dbg_hid("class descriptor notpresentn");
  return NULL;
 }

 

//得到描述符长度:

 for (n = 0; n< hdesc->bNumDescriptors; n++)
  if(hdesc->desc[n].bDescriptorType ==HID_DT_REPORT)
   rsize =le16_to_cpu(hdesc->desc[n].wDescriptorLength);

 

//如果描述符长度不对,则退出

 if (!rsize || rsize> HID_MAX_DESCRIPTOR_SIZE) {
  dbg_hid("weird size of reportdescriptor (%u)n", rsize);
  return NULL;
 }

 

//创建此长度内存空间

 if (!(rdesc =kmalloc(rsize, GFP_KERNEL))) {
  dbg_hid("couldn't allocaterdesc memoryn");
  return NULL;
 }

 

//向dev的endpoint发送HID_REQ_SET_IDLErequest.

 hid_set_idle(dev,interface->desc.bInterfaceNumber, 0, 0);

 

//取得reportdescription的详细信息,放到rdesc中。

 if ((n =hid_get_class_descriptor(dev,interface->desc.bInterfaceNumber, HID_DT_REPORT,rdesc, rsize)) < 0) {
  dbg_hid("reading reportdescriptor failedn");
  kfree(rdesc);
  return NULL;
 }

 usbhid_fixup_report_descriptor(le16_to_cpu(dev->descriptor.idVendor),
   le16_to_cpu(dev->descriptor.idProduct),rdesc,
   rsize,rdesc_quirks_param);

 dbg_hid("reportdescriptor (size %u, read %d) = ", rsize, n);
 for (n = 0; n < rsize; n++)
  dbg_hid_line(" %02x", (unsignedchar) rdesc[n]);
 dbg_hid_line("n");

 

//解析report。并建立hid_device.返回给hid.

 if (!(hid =hid_parse_report(rdesc, n))) {
  dbg_hid("parsing reportdescriptor failedn");
  kfree(rdesc);
  return NULL;
 }

 kfree(rdesc);
 hid->quirks = quirks;

 

 

 if (!(usbhid =kzalloc(sizeof(struct usbhid_device), GFP_KERNEL)))
  goto fail_no_usbhid;

 hid->driver_data =usbhid;
 usbhid->hid = hid;

 usbhid->bufsize =HID_MIN_BUFFER_SIZE;
 hid_find_max_report(hid, HID_INPUT_REPORT,&usbhid->bufsize);
 hid_find_max_report(hid, HID_OUTPUT_REPORT,&usbhid->bufsize);
 hid_find_max_report(hid, HID_FEATURE_REPORT,&usbhid->bufsize);

 if(usbhid->bufsize >HID_MAX_BUFFER_SIZE)
  usbhid->bufsize= HID_MAX_BUFFER_SIZE;

 hid_find_max_report(hid,HID_INPUT_REPORT, &insize);

 if (insize> HID_MAX_BUFFER_SIZE)
  insize =HID_MAX_BUFFER_SIZE;

 if(hid_alloc_buffers(dev, hid)) {
  hid_free_buffers(dev,hid);
  goto fail;
 }

 for (n = 0; n< interface->desc.bNumEndpoints; n++){

  structusb_endpoint_descriptor *endpoint;
  int pipe;
  int interval;

  endpoint =&interface->endpoint[n].desc;
  if((endpoint->bmAttributes & 3) !=3)  
   continue;

  interval =endpoint->bInterval;

  
  if(hid->collection->usage ==HID_GD_MOUSE &&hid_mousepoll_interval > 0)
   interval =hid_mousepoll_interval;

  if(usb_endpoint_dir_in(endpoint)) {
   if(usbhid->urbin)
    continue;
   if(!(usbhid->urbin = usb_alloc_urb(0,GFP_KERNEL)))
    gotofail;
   pipe =usb_rcvintpipe(dev,endpoint->bEndpointAddress);
   usb_fill_int_urb(usbhid->urbin,dev, pipe, usbhid->inbuf, insize,
     hid_irq_in, hid, interval);
   usbhid->urbin->transfer_dma= usbhid->inbuf_dma;
   usbhid->urbin->transfer_flags|= URB_NO_TRANSFER_DMA_MAP;
  } else {
   if(usbhid->urbout)
    continue;
   if(!(usbhid->urbout = usb_alloc_urb(0,GFP_KERNEL)))
    gotofail;
   pipe =usb_sndintpipe(dev,endpoint->bEndpointAddress);
   usb_fill_int_urb(usbhid->urbout,dev, pipe, usbhid->outbuf, 0,
     hid_irq_out, hid, interval);
   usbhid->urbout->transfer_dma= usbhid->outbuf_dma;
   usbhid->urbout->transfer_flags|= URB_NO_TRANSFER_DMA_MAP;
  }
 }

 if(!usbhid->urbin) {
  err_hid("couldn't find an inputinterrupt endpoint");
  goto fail;
 }

 init_waitqueue_head(&hid->wait);

 INIT_WORK(&usbhid->reset_work,hid_reset);
 setup_timer(&usbhid->io_retry,hid_retry_timeout, (unsigned long) hid);

 spin_lock_init(&usbhid->inlock);
 spin_lock_init(&usbhid->outlock);
 spin_lock_init(&usbhid->ctrllock);

 hid->version =le16_to_cpu(hdesc->bcdHID);
 hid->country =hdesc->bCountryCode;
 hid->dev =&intf->dev;
 usbhid->intf = intf;
 usbhid->ifnum =interface->desc.bInterfaceNumber;

 hid->name[0] =0;

 if(dev->manufacturer)
  strlcpy(hid->name,dev->manufacturer,sizeof(hid->name));

 if(dev->product) {
  if(dev->manufacturer)
   strlcat(hid->name," ", sizeof(hid->name));
  strlcat(hid->name,dev->product,sizeof(hid->name));
 }

 if(!strlen(hid->name))
  snprintf(hid->name,sizeof(hid->name), "HID %04x:%04x",
   le16_to_cpu(dev->descriptor.idVendor),
   le16_to_cpu(dev->descriptor.idProduct));

 hid->bus= BUS_USB;
 hid->vendor =le16_to_cpu(dev->descriptor.idVendor);
 hid->product =le16_to_cpu(dev->descriptor.idProduct);

 usb_make_path(dev,hid->phys, sizeof(hid->phys));
 strlcat(hid->phys, "/input",sizeof(hid->phys));
 len = strlen(hid->phys);
 if (len phys) - 1)
  snprintf(hid->phys+ len, sizeof(hid->phys) - len,
    "%d",intf->altsetting[0].desc.bInterfaceNumber);

 if (usb_string(dev,dev->descriptor.iSerialNumber,hid->uniq, 64) <= 0)
  hid->uniq[0] =0;

 usbhid->urbctrl =usb_alloc_urb(0, GFP_KERNEL);
 if (!usbhid->urbctrl)
  goto fail;

 usb_fill_control_urb(usbhid->urbctrl,dev, 0, (void *) usbhid->cr,
       usbhid->ctrlbuf, 1, hid_ctrl, hid);
 usbhid->urbctrl->setup_dma= usbhid->cr_dma;
 usbhid->urbctrl->transfer_dma= usbhid->ctrlbuf_dma;
 usbhid->urbctrl->transfer_flags|= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
 hid->hidinput_input_event =usb_hidinput_input_event;
 hid->hid_open = usbhid_open;
 hid->hid_close =usbhid_close;
#ifdef CONFIG_USB_HIDDEV
 hid->hiddev_hid_event =hiddev_hid_event;
 hid->hiddev_report_event =hiddev_report_event;
#endif
 return hid;

fail:
 usb_free_urb(usbhid->urbin);
 usb_free_urb(usbhid->urbout);
 usb_free_urb(usbhid->urbctrl);
 hid_free_buffers(dev, hid);
 kfree(usbhid);
fail_no_usbhid:
 hid_free_device(hid);

 return NULL;
}

 

1.1.1:usbhid_lookup_quirk(pid,vid)解析:

分别使用usbhid_exists_dquirk(pid,vid),usbhid_exists_squirk(pid,vid)来查看动态和静态的quirks-list.如果有,则返回此quirks.没有,则返回0。

 

1.1.2:usb_get_extra_descriptor(descriptor, type,&hdesc)解析:

它调用__usb_get_extra_descriptor()来解析参数一(interface或endpoint描述符)中的扩展描述符区--extra。看是否有类型为参数二(type)的条目,然后把地址交给参数三。

Sam未经证明的猜测:HID设备中,interface描述符中包含的这个扩展描述符。其中存放的都是HID信息,以hid_descriptor结构存放。

对于HID设备来说,在interface description之后会附加一个hid description, hiddescription中的最后部份包含有Report description或者PhysicalDescriptors的长度.

hid_descriptor->usb_descriptor_header->bDescriptorType为HID_DT_HID

后面描述符(包括它自己)hid_descriptor->hid_class_descriptor->bDescriptorType为HID_DT_REPORT

 

1.1.3:hid_set_idle(structusb_device *dev, int ifnum, int report, intidle):

它简单的调用usb_control_msg() 发送或接收一个usb控制消息。

具体到这个例子中:

usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  HID_REQ_SET_IDLE,USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle<< 8) | report,
  ifnum, NULL, 0,USB_CTRL_SET_TIMEOUT);

usb_sndctrlpipe(dev,0):指出将dev中endpoint 0 设为发送control管道。

HID_REQ_SET_IDLE:此控制消息的Request。

USB_TYPE_CLASS |USB_RECIP_INTERFACE:请求类型.

调用成功才会返回。

 

1.1.4:hid_get_class_descriptor(dev,interface->desc.bInterfaceNumber, HID_DT_REPORT,rdesc, rsize)

也是通过调用usb_control_msg() 发送或接收一个usb控制消息。

发送USB_REQ_GET_DESCRIPTOR,将得到的report descriptor放到rdesc中。

 

1.1.5: hid_parse_report(__u8*start, unsigned size)

参数一:通过usb_control_msg发送request(USB_REQ_GET_DESCRIPTOR)从设备得到的report descriptor.

参数二:是此report Descriptor的长度。

1).首先创建hid_device类型设备。

2).将参数一中数据保存在hid_device->rdesc中。长度保存在hid_device->rsize中。

3). 使用fetch_item(__u8 *start, __u8 *end, struct hid_item*item)得到report description的下一项数据。放到item中。

4). 根据不同的item.type.分别调用

hid_parser_main,
  hid_parser_global,
  hid_parser_local,
  hid_parser_reserved

来处理。

 


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

为了满足日益增长的数据处理需求,铁威马NAS推出了全新的性能巅峰2024年旗舰之作F4-424 Pro,并搭载了最新的操作系统--TOS 6。这款高效办公神器的问世,无疑将为企业和专业人士带来前所未有的便捷与效率。

关键字: 存储 Linux 服务器

双系统将是下述内容的主要介绍对象,通过这篇文章,小编希望大家可以对双系统的相关情况以及信息有所认识和了解,详细内容如下。

关键字: 双系统 Windows Linux

安装Linux操作系统并不复杂,下面是一个大致的步骤指南,以帮助您完成安装。1. 下载Linux发行版:首先,您需要从Linux发行版官方网站下载最新的ISO镜像文件。

关键字: Linux 操作系统 ISO镜像

计算机是由一堆硬件组成的,为了有限的控制这些硬件资源,于是就有了操作系统的产生,操作系统是软件子系统的一部分,是硬件基础上的第一层软件。

关键字: Linux 操作系统 计算机

Linux操作系统是一套免费使用和自由传播的类Unix操作系统,通常被称为GNU/Linux。它是由林纳斯·托瓦兹在1991年首次发布的,并基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。Lin...

关键字: Linux 操作系统

所谓进程间通信就是在不同进程之间传播或交换信息,它是一组编程接口,让程序员能够协调不同的进程,使之能在一个操作系统里同时运行,并相互传递、交换信息;还可以让一个程序能够在同一时间里处理许多用户的需求。

关键字: Linux 进程通信 编程接口

串口通信作为一种最传统的通信方式,在工业自动化、通讯、控制等领域得到广泛使用。

关键字: Linux 串口通信 通讯

2023年11月16日: MikroElektronika(MIKROE) ,作为一家通过提供基于成熟标准的创新式硬软件产品来大幅缩短开发时间的嵌入式解决方案公司,今天宣布推出一款基于单线设备的软硬件开源解决方案Cli...

关键字: 嵌入式 Linux 操作系统

Linux是一种免费使用和自由传播的类Unix操作系统,其内核由林纳斯·本纳第克特·托瓦兹于1991年10月5日首次发布。它主要受到Minix和Unix思想的启发,是一个基于POSIX的多用户、多任务、支持多线程和多CP...

关键字: Linux 操作系统

本文中,小编将对嵌入式予以介绍,如果你想对它的详细情况有所认识,或者想要增进对它的了解程度,不妨请看以下内容哦。

关键字: 嵌入式 Linux
关闭
关闭