Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2118734
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2015-05-29 15:33:59

一.协议分析
1. usb描述符流程
  1. //get_status
  2. usb_raw: 0xa3 0x00 0x00 0x00 0x01 0x00 0x04 0x00
  3. usb_raw: 0x01 0x01 0x01 0x00

  4. //clear_feature
  5. usb_raw: 0x23 0x01 0x10 0x00 0x01 0x00 0x00 0x00

  6. //get_status
  7. usb_raw: 0xa3 0x00 0x00 0x00 0x01 0x00 0x04 0x00
  8. usb_raw: 0x01 0x01 0x00 0x00

  9. //get_status
  10. usb_raw: 0xa3 0x00 0x00 0x00 0x01 0x00 0x04 0x00
  11. usb_raw: 0x01 0x01 0x00 0x00

  12. //get_status
  13. usb_raw: 0xa3 0x00 0x00 0x00 0x01 0x00 0x04 0x00
  14. usb_raw: 0x01 0x01 0x00 0x00

  15. //get_status
  16. usb_raw: 0xa3 0x00 0x00 0x00 0x01 0x00 0x04 0x00
  17. usb_raw: 0x01 0x01 0x00 0x00

  18. //get_status
  19. usb_raw: 0xa3 0x00 0x00 0x00 0x01 0x00 0x04 0x00
  20. usb_raw: 0x01 0x01 0x00 0x00

  21. //clear_feature
  22. usb_raw: 0x23 0x03 0x04 0x00 0x01 0x00 0x00 0x00

  23. //get_status
  24. usb_raw: 0xa3 0x00 0x00 0x00 0x01 0x00 0x04 0x00
  25. usb_raw: 0x03 0x01 0x10 0x00

  26. //clear_feature
  27. usb_raw: 0x23 0x01 0x14 0x00 0x01 0x00 0x00 0x00
  28.     -->到此去抖完成,hub知道真的有设备来了

  29. usb 1-1: new full speed USB device using s3c2410-ohci and address 2
  30. //发送get_descriptor获取设备描述符
  31. usb_raw: 0x80 0x06 0x00 0x01 0x00 0x00 0x40 0x00
  32. usb_raw: 0x12 0x01 0x00 0x02 0x00 0x00 0x00 0x40 0x51 0x09 0x43 0x16 0x00 0x01 0x01 0x02 0x03 0x01
  33. //clear_feature
  34. usb_raw: 0x23 0x03 0x04 0x00 0x01 0x00 0x00 0x00

  35. //get_status
  36. usb_raw: 0xa3 0x00 0x00 0x00 0x01 0x00 0x04 0x00
  37. usb_raw: 0x03 0x01 0x10 0x00

  38. //clear_feature
  39. usb_raw: 0x23 0x01 0x14 0x00 0x01 0x00 0x00 0x00

  40. //set addr
  41. usb_raw: 0x00 0x05 0x02 0x00 0x00 0x00 0x00 0x00

  42. //获取设备描述符
  43. usb_raw: 0x80 0x06 0x00 0x01 0x00 0x00 0x12 0x00
  44. usb_raw: 0x12 0x01 0x00 0x02 0x00 0x00 0x00 0x40 0x51 0x09 0x43 0x16 0x00 0x01 0x01 0x02 0x03 0x01

  45. cong:drivers/usb/core/hub.c:hub_port_connect_change[2932]: next usb_new_device
  46. //获取配置描述符-->主要是为了获取长度
  47. usb_raw: 0x80 0x06 0x00 0x02 0x00 0x00 0x09 0x00
  48. usb_raw: 0x09 0x02 0x20 0x00 0x01 0x01 0x00 0x80 0x32

  49. //完整的获取配置描述符--> len=0x20
  50. usb_raw: 0x80 0x06 0x00 0x02 0x00 0x00 0x20 0x00
  51. usb_raw: 0x09 0x02 0x20 0x00 0x01 0x01 0x00 0x80 0x32 0x09 0x04 0x00 0x00 0x02 0x08 0x06 0x50 0x00 0x07 0x05 0x81 0x02 0x40 0x00 0xff 0x07 0x05 0x02 0x02 0x40 0x00 0xff

  52. //获取字符串描述符之语言ID
  53. usb_raw: 0x80 0x06 0x00 0x03 0x00 0x00 0xff 0x00
  54. usb_raw: 0x04 0x03 0x09 0x04

  55. //获取字符串描述符之0x02-->Product-->DataTraveler G3
  56. usb_raw: 0x80 0x06 0x02 0x03 0x09 0x04 0xff 0x00
  57. usb_raw: 0x20 0x03 0x44 0x00 0x61 0x00 0x74 0x00 0x61 0x00 0x54 0x00 0x72 0x00 0x61 0x00 0x76 0x00 0x65 0x00 0x6c 0x00 0x65 0x00 0x72 0x00 0x20 0x00 0x47 0x00 0x33 0x00

  58. //获取字符串描述符之0x01-->Manufacturer-->Kingston
  59. usb_raw: 0x80 0x06 0x01 0x03 0x09 0x04 0xff 0x00
  60. usb_raw: 0x12 0x03 0x4b 0x00 0x69 0x00 0x6e 0x00 0x67 0x00 0x73 0x00 0x74 0x00 0x6f 0x00 0x6e 0x00

  61. //获取字符串描述符之0x03-->SerialNumber-->001CC05FE8ECFBB1B92223DB
  62. usb_raw: 0x80 0x06 0x03 0x03 0x09 0x04 0xff 0x00
  63. usb_raw: 0x32 0x03 0x30 0x00 0x30 0x00 0x31 0x00 0x43 0x00 0x43 0x00 0x30 0x00 0x35 0x00 0x46 0x00 0x45 0x00 0x38 0x00 0x45 0x00 0x43 0x00 0x46 0x00 0x42 0x00 0x42 0x00 0x31 0x00

  64. usb 1-1: New USB device found, idVendor=0951, idProduct=1643
  65. usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
  66. usb 1-1: Product: DataTraveler G3
  67. usb 1-1: Manufacturer: Kingston
  68. usb 1-1: SerialNumber: 001CC05FE8ECFBB1B92223DB
  69. usb 1-1: configuration #1 chosen from 1 choice

  70. //设置配置
  71. usb_raw: 0x00 0x09 0x01 0x00 0x00 0x00 0x00 0x00

  72. scsi0 : SCSI emulation for USB Mass Storage devices
  73. cong:drivers/usb/core/hub.c:hub_port_connect_change[2934]: usb_new device over

  74. //get_status
  75. usb_raw: 0xa3 0x00 0x00 0x00 0x01 0x00 0x04 0x00
  76. usb_raw: 0x03 0x01 0x00 0x00

  77. cong:drivers/usb/core/hub.c:hub_events[3071]: portstatus=0x103,portchange=0x0
  78. cong:drivers/usb/core/hub.c:hub_events[3143]: conntct_change=0
  79. scsi 0:0:0:0: Direct-Access Kingston DataTraveler G3 1.00 PQ: 0 ANSI: 0 CCS
  80. sd 0:0:0:0: [sda] 15644912 512-byte hardware sectors: (8.01 GB/7.45 GiB)
  81. sd 0:0:0:0: [sda] Write Protect is off
  82. sd 0:0:0:0: [sda] Assuming drive cache: write through
  83. sd 0:0:0:0: [sda] Assuming drive cache: write through
  84.  sda: sda1
  85. sd 0:0:0:0: [sda] Attached SCSI removable disk
2. 一个u盘的各个描述符解析
  1. 设备描述符:
  2. send: 0x80 0x06 0x00 0x01 0x00 0x00 0x40 0x00
  3.     0x80
  4.     0x06 --> 请求类型: get_Desc
  5.     0x00 0x01 --> 0x01代表设备描述符
  6.     0x00 0x00
  7.     0x40 0x00 --> 长度为0x40,此时还没有确定ep0的maxPktSize
  8. recv: 0x12 0x01 0x00 0x02 0x00 0x00 0x00 0x40 0x51 0x09 0x43 0x16 0x00 0x01 0x01 0x02 0x03 0x01
  9.     0x12 --> len
  10.     0x01 --> 设备描述符
  11.     0x00 0x02 -->usb2.0
  12.     0x00 0x00 --> class及subclass
  13.     0x00 -->
  14.     0x40 --> MaxPacketSize=64个字节
  15.     0x51 0x09 --> VID
  16.     0x43 0x16 --> PID
  17.     0x00 0x01 -->自定义Version
  18.     0x01 --> iManufacture string字段
  19.     0x02 --> iProduct string字段
  20.     0x03 --> 序列号 string
  21.     0x01 --> bNumConfigruation

  22. //获取配置描述符
  23. usb_raw: 0x80 0x06 0x00 0x02 0x00 0x00 0x09 0x00
  24.     0x80
  25.     0x06 --> 请求类型: get_Desc
  26.     0x00 0x02 --> 0x02代表配置描述符
  27.     0x00 0x00
  28.     0x09 0x00 --> 配置描述符的长度为0x09
  29. usb_raw: 0x09 0x02 0x20 0x00 0x01 0x01 0x00 0x80 0x32
  30.     0x09 --> len
  31.     0x02 --> 配置描述符0x02
  32.     0x20 0x00 --> 配置描述符集合总长充0x20=32B
  33.     0x01 --> 接口数=1
  34.     0x01 --> 该配置的标号=1
  35.     0x00 --> 该配置的字符串索引值
  36.     0x80 --> 该设备的属性
  37.     0x32 --> 电流(0x32*2mA=100mA)
  38. //完整的获取配置描述符--> len=0x20,只看接口描述符
  39. usb_raw: 0x80 0x06 0x00 0x02 0x00 0x00 0x20 0x00
  40. usb_raw: 0x09 0x02 0x20 0x00 0x01 0x01 0x00 0x80 0x32 0x09 0x04 0x00 0x00 0x02 0x08 0x06 0x50 0x00 0x07 0x05 0x81 0x02 0x40 0x00 0xff 0x07 0x05 0x02 0x02 0x40 0x00 0xff
  41.     0x09
  42.     0x04 --> 接口描述符0x04
  43.     0x00 --> 该接口的标号
  44.     0x00 --> 备用标号
  45.     0x02 --> 本接口的端点数=2个端点
  46.     0x08 --> 本接口的类 0x08(大容量存储设备接口类为0x08)
  47.     0x06 --> 本接口的子类 0x06(scsi透明命令子集的子类代码为0x06)
  48.     0x50 --> 本接口的协议为仅批量传输,0x50
  49.     0x00 --> 本接口的字符串索引
  50.     
  51.     0x07
  52.     0x05 --> 端点描述符0x05
  53.     0x81 --> 本端点的地址(8代表输入,1代表ep1)
  54.     0x02 --> 本端点的属性(2: bulk传输)
  55.     0x40 --> 本端点的最大包长度
  56.     0x00
  57.     0xff --> 本端点的查询时间
  58.     
  59.     0x07
  60.     0x05 --> 端点描述符0x05
  61.     0x02 --> 本端点的地址(8代表输入,1代表ep1)
  62.     0x02 --> 本端点的属性(2: bulk传输)
  63.     0x40 --> 本端点的最大包长度
  64.     0x00
  65.     0xff --> 本端点的查询时间
  66. //获取字符串描述符
  67. usb_raw: 0x80 0x06 0x00 0x03 0x00 0x00 0xff 0x00
  68.     0x80
  69.     0x06 --> 请求类型: get_Desc
  70.     0x00 0x03 --> wValue:0x00代表语言ID,0x03代表字符串描述符
  71.     0x00 0x00 --> wIndex:
  72.     0xff 0x00 --> wLentgh
  73. usb_raw: 0x04 0x03 0x09 0x04
  74.     0x04 --> len
  75.     0x03 --> 字符串描述符
  76.     0x09 0x04 --> 0x0409 美式英语ID
  77.     
  78.     
  79. //获取字符串描述符之语言Product-->DataTraveler G3
  80. usb_raw: 0x80 0x06 0x02 0x03 0x09 0x04 0xff 0x00
  81.     0x80
  82.     0x06 --> 请求类型: get_Desc
  83.     0x02 0x03 --> 0x02代表字符串索引2,0x03代表字符串描述符
  84.     0x09 0x04
  85.     0xff 0x00 --> wLentgh
  86. usb_raw: 0x20 0x03 0x44 0x00 0x61 0x00 0x74 0x00 0x61 0x00 0x54 0x00 0x72 0x00 0x61 0x00 0x76 0x00 0x65 0x00 0x6c 0x00 0x65 0x00 0x72 0x00 0x20 0x00 0x47 0x00 0x33 0x00

二.调试技巧
2.1 打印usb协议包
  1. int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
  2.          __u8 requesttype, __u16 value, __u16 index, void *data,
  3.          __u16 size, int timeout)
  4. {
  5.     struct usb_ctrlrequest *dr;
  6.     int ret;

  7.     dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
  8.     if (!dr)
  9.         return -ENOMEM;

  10.     dr->bRequestType = requesttype;
  11.     dr->bRequest = request;
  12.     dr->wValue = cpu_to_le16(value);
  13.     dr->wIndex = cpu_to_le16(index);
  14.     dr->wLength = cpu_to_le16(size);

  15.     dump_raw_char((char*)dr,sizeof(struct usb_ctrlrequest));//打印host发给device的数据
  16.     /* dbg("usb_control_msg"); */

  17.     ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);

  18.     if(ret > 0)
  19.         dump_raw_char((char*)data, ret);  //打印device返回给host的命令   
  20.     printk("\n");
  21.     kfree(dr);

  22.     return ret;
  23. }
  24. EXPORT_SYMBOL_GPL(usb_control_msg);
2.2其中dump_raw_char的实现如下,
  1. int dump_raw_char(char* p, int len)
  2. {
  3.     int i;
  4.     printk("usb_raw: ");
  5.     for(i=0; i<len; i++)
  6.         printk("0x%02x ", (unsigned char)p[i]);
  7.     printk("\n");
  8.     return 0;
  9. }
  10. EXPORT_SYMBOL_GPL(dump_raw_char);
2.3 在driver/usb/core/hub.h中添加定义
  1. extern int dump_raw_char(char* p, int len);




阅读(2222) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~