2022年驱动实例 .pdf

上传人:Q****o 文档编号:31709896 上传时间:2022-08-08 格式:PDF 页数:21 大小:110.62KB
返回 下载 相关 举报
2022年驱动实例 .pdf_第1页
第1页 / 共21页
2022年驱动实例 .pdf_第2页
第2页 / 共21页
点击查看更多>>
资源描述

《2022年驱动实例 .pdf》由会员分享,可在线阅读,更多相关《2022年驱动实例 .pdf(21页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、Linux USB gadget设备驱动解析( 4)- 编写一个 gadget 驱动作者: 刘洪涛,华清远见嵌入式学院讲师。一、编写计划通过前面几节的基础,本节计划编写一个简单的gadget驱动。重在让大家快速了解gadget驱动结构。上节中简单介绍了zero.c程序。这个程序考虑到了多配置、高速传输、USB OTG 等因素。应该说写的比较清楚,是我们了解gadget驱动架构的一个非常好的途径。但把这些东西都放在一起,对很多初学人员来说还是不能快速理解。那就再把它简化一些,针对S3C2410平台,只实现一个配置、一个接口、一个端点,不考虑高速及 OTG 的情况。只完成单向从host端接收数据的

2、功能,但要把字符设备驱动结合在里面。这需要有一个host 端的驱动,来完成向device端发送数据。关于在主机端编写一个简单的USB 设备驱动程序,有很多的资料。相信大家很快就会完成的。二、功能展示1、PC端编写了一个 usbtransfer.ko ,能够向 device 端发送数据2、对目标平台编写一个gadget驱动,名称是 g_zero.ko 3、测试步骤在目标平台(基于S3C2410 )上加载 gadget驱动# insmod g_zero.ko name=ep1-bulk smdk2410_udc: Pull-up enable # mknod /dev/usb_rcv c 251

3、0 # 在 PC主机上加载驱动usbtransfer.ko #insmod usbtransfer.ko #mknod /dev/usbtransfer c 266 0 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 21 页 - - - - - - - - - 连接设备,目标平台的终端显示:connected 目标平台读取数据# cat /dev/usb_rcv PC端发送数据#echo “12345” /dev/usbtransfer#echo “abcd” /dev

4、/usbtransfer设备端会显示收到的数据# cat /dev/usb_rcv 12345 abcd 三、代码分析下面的代码是在原有的zero.c基础上做了精简、修改的。一些结构的名称还是保留以前的,但含义有所变化。如:loopback_config,不再表示 loopback,而只是单向的接收数据。/* * zero.c - Gadget Zero, for simple USB development * * All rights reserved.*/ /* #define VERBOSE_DEBUG */ #include #include #include 名师资料总结 - -

5、-精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 21 页 - - - - - - - - - #include #include #include gadget_chips.h #include #include #include #include #include #include #include #include #include /* size_t */ #include /* error codes */ #include #include #include /*-*/ static

6、 const char shortname = zero; static const char loopback = loop input to output; static const char longname = Gadget Zero; static const char source_sink = source and sink data; #define STRING_MANUFACTURER 25 #define STRING_PRODUCT 42 #define STRING_SERIAL 101 #define STRING_SOURCE_SINK 250 #define S

7、TRING_LOOPBACK 251 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 21 页 - - - - - - - - - /#define DRIVER_VENDOR_NUM 0 x0525 /* NetChip */ /#define DRIVER_PRODUCT_NUM 0 xa4a0 /* Linux-USB Gadget Zero */ #define DRIVER_VENDOR_NUM 0 x5345 /* NetChip */ #define DRI

8、VER_PRODUCT_NUM 0 x1234 /* Linux-USB Gadget Zero */ static int usb_zero_major = 251; /*-*/ static const char *EP_OUT_NAME; /* sink */ /*-*/ /* big enough to hold our biggest descriptor */ #define USB_BUFSIZ 256 struct zero_dev /zero设备结构 spinlock_t lock; struct usb_gadget *gadget; struct usb_request

9、*req; /* for control responses */ struct usb_ep *out_ep; struct cdev cdev; unsigned char data128; unsigned int data_size; wait_queue_head_t bulkrq; ; #define CONFIG_LOOPBACK 2 static struct usb_device_descriptor device_desc = / 设备描述符 .bLength = sizeof device_desc, .bDescriptorType = USB_DT_DEVICE, .

10、bcdUSB = _constant_cpu_to_le16(0 x0110), .bDeviceClass = USB_CLASS_VENDOR_SPEC, .idVendor = _constant_cpu_to_le16(DRIVER_VENDOR_NUM), 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 21 页 - - - - - - - - - .idProduct = _constant_cpu_to_le16(DRIVER_PRODUCT_NUM), .

11、iManufacturer = STRING_MANUFACTURER, .iProduct = STRING_PRODUCT, .iSerialNumber = STRING_SERIAL, .bNumConfigurations = 1, ; static struct usb_endpoint_descriptor fs_sink_desc = / 端点描述符 .bLength = USB_DT_ENDPOINT_SIZE, .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_DIR_OUT, /对主机端来说,输出 .b

12、mAttributes = USB_ENDPOINT_XFER_BULK, ; static struct usb_config_descriptor loopback_config = / 配置描述符 .bLength = sizeof loopback_config, .bDescriptorType = USB_DT_CONFIG, /* compute wTotalLength on the fly */ .bNumInterfaces = 1, .bConfigurationValue = CONFIG_LOOPBACK, .iConfiguration = STRING_LOOPB

13、ACK, .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, .bMaxPower = 1, /* self-powered */ ; static const struct usb_interface_descriptor loopback_intf = / 接口描述符 .bLength = sizeof loopback_intf, .bDescriptorType = USB_DT_INTERFACE, 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - -

14、 名师精心整理 - - - - - - - 第 5 页,共 21 页 - - - - - - - - - .bNumEndpoints = 1, .bInterfaceClass = USB_CLASS_VENDOR_SPEC, .iInterface = STRING_LOOPBACK, ; /* static strings, in UTF-8 */ #define STRING_MANUFACTURER 25 #define STRING_PRODUCT 42 #define STRING_SERIAL 101 #define STRING_SOURCE_SINK 250 #define

15、 STRING_LOOPBACK 251 static char manufacturer50; /* default serial number takes at least two packets */ static char serial = 0123456789.0123456789.0123456789; static struct usb_string strings = /字符串描述符 STRING_MANUFACTURER, manufacturer, , STRING_PRODUCT, longname, , STRING_SERIAL, serial, , STRING_L

16、OOPBACK, loopback, , STRING_SOURCE_SINK, source_sink, , /* end of list */ ; static struct usb_gadget_strings stringtab = .language = 0 x0409, /* en-us */ .strings = strings, ; static const struct usb_descriptor_header *fs_loopback_function = (struct usb_descriptor_header *) &loopback_intf, 名师资料总结 -

17、- -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 21 页 - - - - - - - - - (struct usb_descriptor_header *) &fs_sink_desc, NULL, ; static int usb_zero_open (struct inode *inode, struct file *file) /打开设备 struct zero_dev *dev = container_of (inode-i_cdev, struct zero_dev, cdev

18、); file-private_data = dev; init_waitqueue_head (&dev-bulkrq); return 0; static int usb_zero_release (struct inode *inode, struct file *file) / 关闭设备 return 0; static void free_ep_req(struct usb_ep *ep, struct usb_request *req) kfree(req-buf); usb_ep_free_request(ep, req); static struct usb_request *

19、alloc_ep_req(struct usb_ep *ep, unsigned length)/分配请求 struct usb_request *req; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 21 页 - - - - - - - - - req = usb_ep_alloc_request(ep, GFP_ATOMIC); if (req) req-length = length; req-buf = kmalloc(length, GFP_ATOMIC);

20、 if (!req-buf) usb_ep_free_request(ep, req); req = NULL; return req; static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)/请求完成函数 struct zero_dev *dev = ep-driver_data; int status = req-status; switch (status) case 0: /* normal completion */ if (ep = dev-out_ep) memcpy(dev-dat

21、a, req-buf, req- actual);/ 返回数据拷贝到 req-buf中, /dev-data_size=req-length; dev-data_size=req-actual; / 实际长度为 req- actual;需要确认 req short_not_ok为 0。参考 gadget.h中关于 usb_request结构的注释 break; /* this endpoint is normally active while were configured */ case -ECONNABORTED: /* hardware forced ep reset */ case -

22、ECONNRESET: /* request dequeued */ case -ESHUTDOWN: /* disconnect from host */ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 21 页 - - - - - - - - - printk(%s gone (%d), %d/%dn, ep-name, status, req-actual, req-length); case -EOVERFLOW: /* buffer overrun on rea

23、d means that * we didnt provide a big enough * buffer. */ default: #if 1 printk(%s complete - %d, %d/%dn, ep-name, status, req-actual, req-length); #endif case -EREMOTEIO: /* short read */ break; free_ep_req(ep, req); wake_up_interruptible (&dev-bulkrq); /唤醒读函数 static struct usb_request *source_sink

24、_start_ep(struct usb_ep *ep)/构造并发送读请求 struct usb_request *req; int status; /printk(in %sn,_FUNCTION_); req = alloc_ep_req(ep, 128); if (!req) return NULL; memset(req-buf, 0, req-length); req-complete = source_sink_complete; / 请求完成函数名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - -

25、- - - - - 第 9 页,共 21 页 - - - - - - - - - status = usb_ep_queue(ep, req, GFP_ATOMIC); /递交请求 if (status) struct zero_dev *dev = ep-driver_data; printk(start %s - %dn, ep-name, status); free_ep_req(ep, req); req = NULL; return req; ssize_t usb_zero_read (struct file * file, const char _user * buf, size

26、_t count,loff_t * f_pos) / 读设备 struct zero_dev *dev =file-private_data; struct usb_request *req; int status; struct usb_ep *ep; struct usb_gadget *gadget = dev-gadget; ssize_t ret = 0; int result; ep=dev-out_ep; source_sink_start_ep(ep);/ 构造、递交读请求 if (count bulkrq);/睡眠,等到请求完成 if (copy_to_user (buf,d

27、ev-data,dev-data_size) / 拷贝读取的数据到用户空间 ret = -EFAULT; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 21 页 - - - - - - - - - else ret = dev-data_size; return ret; struct file_operations usb_zero_fops = .owner = THIS_MODULE, .read = usb_zero_read, .open = usb_zer

28、o_open, .release = usb_zero_release, ; static void usb_zero_setup_cdev (struct zero_dev *dev, int minor)/ 注册字符设备驱动 int err, devno = MKDEV (usb_zero_major, minor); cdev_init(&dev-cdev, &usb_zero_fops); dev-cdev.owner = THIS_MODULE; err = cdev_add (&dev-cdev, devno, 1); if (err) printk (Error adding u

29、sb_rcvn); static void zero_setup_complete(struct usb_ep *ep, struct usb_request *req)/配置端点 0 的请求完成处理 if (req-status | req-actual != req-length) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 21 页 - - - - - - - - - printk(setup complete - %d, %d/%dn, req-status

30、, req-actual, req-length); static void zero_reset_config(struct zero_dev *dev) / 复位配置 usb_ep_disable(dev-out_ep); dev-out_ep = NULL; static void zero_disconnect(struct usb_gadget *gadget)/ 卸载驱动时被调用,做一些注销工作 struct zero_dev *dev = get_gadget_data(gadget); unsigned long flags; unregister_chrdev_region

31、(MKDEV (usb_zero_major, 0), 1); cdev_del (&(dev-cdev); zero_reset_config(dev); printk(in %sn,_FUNCTION_); static int config_buf(struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index) /int is_source_sink; int len; const struct usb_descriptor_header *function; int hs = 0; function =fs_loopback_f

32、unction;/根据 fs_loopback_function,得到长度, / 此处 len=配置( 9)+1个接口( 9)+1 个端点( 7)=25 len = usb_gadget_config_buf(&loopback_config, 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 21 页 - - - - - - - - - buf, USB_BUFSIZ, function); if (len bDescriptorType = type; return

33、len; static int set_loopback_config(struct zero_dev *dev) int result = 0; struct usb_ep *ep; struct usb_gadget *gadget = dev-gadget; ep=dev-out_ep; const struct usb_endpoint_descriptor *d; d = &fs_sink_desc; result = usb_ep_enable(ep, d); / 激活端点 /printk(); if (result = 0) printk(connectedn); /如果成功,打

34、印 “connected” else printk(cant enable %s, result %dn, ep-name, result); return result; static int zero_set_config(struct zero_dev *dev, unsigned number) int result = 0; struct usb_gadget *gadget = dev-gadget; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 21 页

35、 - - - - - - - - - result = set_loopback_config(dev);/ 激活设备 if (result) zero_reset_config(dev); / 复位设备 else char *speed; switch (gadget-speed) case USB_SPEED_LOW: speed = low; break; case USB_SPEED_FULL: speed = full; break; case USB_SPEED_HIGH: speed = high; break; default: speed = ; break; return

36、result; /* zero_setup完成 USB 设置阶段和具体功能相关的交互部分 */ static int zero_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) struct zero_dev *dev = get_gadget_data(gadget); struct usb_request *req = dev-req; int value = -EOPNOTSUPP; u16 w_index = le16_to_cpu(ctrl-wIndex); u16 w_value = le16_

37、to_cpu(ctrl-wValue); u16 w_length = le16_to_cpu(ctrl-wLength); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 21 页 - - - - - - - - - /* usually this stores reply data in the pre-allocated ep0 buffer, * but config change events will reconfigure hardware. */ req

38、-zero = 0; switch (ctrl-bRequest) case USB_REQ_GET_DESCRIPTOR: / 获取描述符 if (ctrl-bRequestType != USB_DIR_IN) goto unknown; switch (w_value 8) case USB_DT_DEVICE: /获取设备描述符 value = min(w_length, (u16) sizeof device_desc); memcpy(req-buf, &device_desc, value); break; case USB_DT_CONFIG: / 获取配置,注意:会根据fs_

39、loopback_function 读取到接口、端点描述符,注意通过config_buf 完成读取数据及数量的统计。 value = config_buf(gadget, req-buf, w_value 8, w_value & 0 xff); if (value = 0) value = min(w_length, (u16) value); break; case USB_DT_STRING: value = usb_gadget_get_string(&stringtab, w_value & 0 xff, req-buf); if (value = 0) value = min(w_

40、length, (u16) value); break; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 21 页 - - - - - - - - - break; case USB_REQ_SET_CONFIGURATION: if (ctrl-bRequestType != 0) goto unknown; spin_lock(&dev-lock); value = zero_set_config(dev, w_value);/ 激活相应的端点 spin_unloc

41、k(&dev-lock); break; default: unknown: printk( unknown control req%02x.%02x v%04x i%04x l%dn, ctrl-bRequestType, ctrl-bRequest, w_value, w_index, w_length); /* respond with data transfer before status phase */ if (value = 0) req-length = value; req-zero = value ep0, req, GFP_ATOMIC);/ 通过端点 0 完成 setu

42、p if (value %dn, value); req-status = 0; zero_setup_complete(gadget-ep0, req); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 21 页 - - - - - - - - - /* device either stalls (value cdev); /* weve already been disconnected . no i/o is active */ if (dev-req) dev-

43、req-length = USB_BUFSIZ; free_ep_req(gadget-ep0, dev-req); kfree(dev); set_gadget_data(gadget, NULL); static int _init zero_bind(struct usb_gadget *gadget) / 绑定过程 struct zero_dev *dev; struct usb_ep *ep; int gcnum; usb_ep_autoconfig_reset(gadget); ep = usb_ep_autoconfig(gadget, &fs_sink_desc);/根据端点描

44、述符及控制器端点情况,分配一个合适的端点。 if (!ep) goto enomem; EP_OUT_NAME = ep-name; /记录名称名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 21 页 - - - - - - - - - gcnum = usb_gadget_controller_number(gadget);/ 获得控制器代号 if (gcnum = 0) device_desc.bcdDevice = cpu_to_le16(0 x0200 + gc

45、num);/ 赋值设备描述符 else pr_warning(%s: controller %s not recognizedn, shortname, gadget-name); device_desc.bcdDevice = _constant_cpu_to_le16(0 x9999); dev = kzalloc(sizeof(*dev), GFP_KERNEL); /分配设备结构体 if (!dev) return -ENOMEM; spin_lock_init(&dev-lock); dev-gadget = gadget; set_gadget_data(gadget, dev);

46、 dev-req = usb_ep_alloc_request(gadget-ep0, GFP_KERNEL);/ 分配一个请求 if (!dev-req) goto enomem; dev-req-buf = kmalloc(USB_BUFSIZ, GFP_KERNEL); if (!dev-req-buf) goto enomem; dev-req-complete = zero_setup_complete; dev-out_ep=ep; / 记录端点(就是接收host端数据的端点) printk(name=%sn,dev-out_ep-name); / 打印出这个端点的名称 ep-dr

47、iver_data=dev; device_desc.bMaxPacketSize0 = gadget-ep0-maxpacket; usb_gadget_set_selfpowered(gadget); gadget-ep0-driver_data = dev; snprintf(manufacturer, sizeof manufacturer, %s %s with %s, 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 18 页,共 21 页 - - - - - - - -

48、- init_utsname()-sysname, init_utsname()-release, gadget-name); /*字符设备注册 */ dev_t usb_zero_dev = MKDEV (usb_zero_major, 0); int result = register_chrdev_region (usb_zero_dev, 1, usb_zero); if (result 0) printk (KERN_NOTICE Unable to get usb_transfer region, error %dn,result); return 0; usb_zero_setu

49、p_cdev (dev, 0); return 0; enomem: zero_unbind(gadget); return -ENOMEM; /*-*/ static struct usb_gadget_driver zero_driver = /gadget 驱动的核心数据结构 #ifdef CONFIG_USB_GADGET_DUALSPEED .speed = USB_SPEED_HIGH, #else .speed = USB_SPEED_FULL, #endif .function = (char *) longname, .bind = zero_bind, .unbind =

50、_exit_p(zero_unbind), .setup = zero_setup, .disconnect = zero_disconnect, 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 19 页,共 21 页 - - - - - - - - - /.suspend = zero_suspend, /不考虑电源管理的功能 /.resume = zero_resume, .driver = .name = (char *) shortname, .owner = THIS_MO

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 技术总结

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁