• «
  • 1
  • 2
  • 3
  • »
  • Pages: 1/3     Go
主题 : 在mini2440上使用USB Gadget功能 复制链接 | 浏览器收藏 | 打印
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
楼主  发表于: 2009-05-05 07:57

 在mini2440上使用USB Gadget功能

下面是我在mini2440上使用USB Gadget功能的过程。我将USB Gadget的各个功能编译成内核模块,目前只尝试了g_serial和g_ether功能,有兴趣的兄弟可以试试其余的,别忘了告诉大家结果:)
就像powerpan在帖子http://www.aiothome.com/bbs/read.php?tid-1683.html里提到的,2.6.29内核对于内核模块的处理存在bug,要使用内核模块,必须先给内核打上这个补丁,重新编译内核zImage下载到板子中运行。

这是我编译的所有的Gadget模块

[root@FriendlyARM /]# ls -l /lib/modules/2.6.29-FriendlyARM/kernel/drivers/usb/gadget/
-rw-r--r--    1 root     root        40404 May  4  2009 g_cdc.ko
-rw-r--r--    1 root     root        40331 May  4  2009 g_ether.ko
-rw-r--r--    1 root     root        34559 May  4  2009 g_file_storage.ko
-rw-r--r--    1 root     root        34116 May  4  2009 g_serial.ko
-rw-r--r--    1 root     root        23541 May  4  2009 s3c2410_udc.ko

A. 加载S3C2440A的USB Device (s3c2410_udc.ko)模块:

[root@FriendlyARM /]# modprobe s3c2410_udc
s3c2410_udc: debugfs dir creation failed -19
s3c2440-usbgadget s3c2440-usbgadget: S3C2440: increasing FIFO to 128 bytes
[root@FriendlyARM /]#
[root@FriendlyARM /]#
[root@FriendlyARM /]# lsmod
s3c2410_udc 20360 0 - Live 0xbf000000

B. 加载g_serial (g_serial.ko, 在mini2440上模拟出一个USB串口):

[root@FriendlyARM /]# modprobe g_serial
g_serial gadget: Gadget Serial v2.4
g_serial gadget: g_serial ready
[root@FriendlyARM /]#
[root@FriendlyARM /]# lsmod
g_serial 22056 0 - Live 0xbf00a000
s3c2410_udc 20360 1 g_serial, Live 0xbf000000
[root@FriendlyARM /]#
[root@FriendlyARM /]#
[root@FriendlyARM /]# g_serial gadget: full speed config #2: CDC ACM config

这是在我的PC Ubuntu 9.04上lsusb的输出

Bus 002 Device 006: ID 0525:a4a7 Netchip Technology, Inc. Linux-USB Serial Gadget (CDC ACM mode)
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            2 Communications
  bDeviceSubClass         0
  bDeviceProtocol         0
  bMaxPacketSize0         8
  idVendor           0x0525 Netchip Technology, Inc.
  idProduct          0xa4a7 Linux-USB Serial Gadget (CDC ACM mode)
  bcdDevice           24.12
  iManufacturer           1
  iProduct                2
  iSerial                 0
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           67
    bNumInterfaces          2
    bConfigurationValue     2
    iConfiguration          3
    bmAttributes         0xc0
      Self Powered
    MaxPower                2mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         2 Communications
      bInterfaceSubClass      2 Abstract (modem)
      bInterfaceProtocol      1 AT-commands (v.25ter)
      iInterface              4
      CDC Header:
        bcdCDC               1.10
      CDC Call Management:
        bmCapabilities       0x00
        bDataInterface          1
      CDC ACM:
        bmCapabilities       0x02
          line coding and serial state
      CDC Union:
        bMasterInterface        0
        bSlaveInterface         1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x000a  1x 10 bytes
        bInterval              32
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass        10 CDC Data
      bInterfaceSubClass      0 Unused
      bInterfaceProtocol      0
      iInterface              5
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x02  EP 2 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
can't get device qualifier: Operation not permitted
can't get debug descriptor: Operation not permitted
cannot read device status, Operation not permitted (1)

PC端对应生成的设备是/dev/ttyACM0

C. 加载e_ether模块(g_ether.ko, 由于Gadget功能一次只能加载一个,所以先卸载g_serial)

[root@FriendlyARM /]# rmmod g_serial
[root@FriendlyARM /]# modprobe g_ether
s3c2410_udc: debugfs dir creation failed -19
s3c2440-usbgadget s3c2440-usbgadget: S3C2440: increasing FIFO to 128 bytes
g_ether gadget: using random self ethernet address
g_ether gadget: using random host ethernet address
usb0: MAC 16:1a:1d:a8:3f:97
usb0: HOST MAC e6:62:d2:0d:be:18
g_ether gadget: Ethernet Gadget, version: Memorial Day 2008
g_ether gadget: g_ether ready
[root@FriendlyARM /]#
[root@FriendlyARM /]#
[root@FriendlyARM /]#
[root@FriendlyARM /]#
[root@FriendlyARM /]#
[root@FriendlyARM /]# g_ether gadget: full speed config #1: CDC Ethernet (ECM)

[root@FriendlyARM /]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 08:90:90:90:90:90  
          inet addr:192.168.1.230  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:201 errors:0 dropped:0 overruns:0 frame:0
          TX packets:33 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:25151 (24.5 KiB)  TX bytes:2140 (2.0 KiB)
          Interrupt:51

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:370 (370.0 B)  TX bytes:370 (370.0 B)

usb0      Link encap:Ethernet  HWaddr 16:1A:1D:A8:3F:97  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

[root@FriendlyARM /]# ifconfig usb0 192.168.0.202
[root@FriendlyARM /]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 08:90:90:90:90:90  
          inet addr:192.168.1.230  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:202 errors:0 dropped:0 overruns:0 frame:0
          TX packets:34 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:25215 (24.6 KiB)  TX bytes:2182 (2.1 KiB)
          Interrupt:51

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:370 (370.0 B)  TX bytes:370 (370.0 B)

usb0      Link encap:Ethernet  HWaddr 16:1A:1D:A8:3F:97  
          inet addr:192.168.0.202  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)


对应的PC端也将生成usb0以太网卡,将其IP地址配置为192.168.0.200(我之前已经通过脚本配置,大家可以用ifconfig命令手动配置), 通过ftp功能验证可以连接开发板

alex@alex-desktop:~$ ifconfig -a
eth6      Link encap:Ethernet  HWaddr 00:0c:29:6c:73:0e  
          inet addr:192.168.247.129  Bcast:192.168.247.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe6c:730e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:54558 errors:0 dropped:0 overruns:0 frame:0
          TX packets:105673 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3362020 (3.3 MB)  TX bytes:12912584 (12.9 MB)
          Interrupt:19 Base address:0x2000

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:74 errors:0 dropped:0 overruns:0 frame:0
          TX packets:74 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:5616 (5.6 KB)  TX bytes:5616 (5.6 KB)

pan0      Link encap:Ethernet  HWaddr 0a:d4:ab:6d:db:f3  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

usb0      Link encap:Ethernet  HWaddr e6:62:d2:0d:be:18  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

alex@alex-desktop:~$ sudo ifup usb0
[sudo] password for alex:
WARNING: All config files need .conf: /etc/modprobe.d/lrm-video, it will be ignored in a future release.
WARNING: All config files need .conf: /etc/modprobe.d/ipw3945, it will be ignored in a future release.
WARNING: All config files need .conf: /etc/modprobe.d/nvidia-kernel-nkc, it will be ignored in a future release.
WARNING: All config files need .conf: /etc/modprobe.d/vmware-tools, it will be ignored in a future release.
WARNING: All config files need .conf: /etc/modprobe.d/kqemu, it will be ignored in a future release.
* if-up.d/mountnfs[usb0]: waiting for interface eth0 before doing NFS mounts
* if-up.d/mountnfs[usb0]: waiting for interface ath0 before doing NFS mounts
* if-up.d/mountnfs[usb0]: waiting for interface wlan0 before doing NFS mounts
* Stopping NTP server ntpd
alex@alex-desktop:~$    ...done.

alex@alex-desktop:~$
alex@alex-desktop:~$ ifconfig -a
eth6      Link encap:Ethernet  HWaddr 00:0c:29:6c:73:0e  
          inet addr:192.168.247.129  Bcast:192.168.247.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe6c:730e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:54636 errors:0 dropped:0 overruns:0 frame:0
          TX packets:105745 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3369316 (3.3 MB)  TX bytes:12922223 (12.9 MB)
          Interrupt:19 Base address:0x2000

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:74 errors:0 dropped:0 overruns:0 frame:0
          TX packets:74 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:5616 (5.6 KB)  TX bytes:5616 (5.6 KB)

pan0      Link encap:Ethernet  HWaddr 0a:d4:ab:6d:db:f3  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

usb0      Link encap:Ethernet  HWaddr e6:62:d2:0d:be:18  
          inet addr:192.168.0.200  Bcast:192.168.0.207  Mask:255.255.255.240
          inet6 addr: fe80::e462:d2ff:fe0d:be18/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:21 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:3245 (3.2 KB)

alex@alex-desktop:~$ ping 192.168.0.202
PING 192.168.0.202 (192.168.0.202) 56(84) bytes of data.
64 bytes from 192.168.0.202: icmp_seq=1 ttl=64 time=43.4 ms
64 bytes from 192.168.0.202: icmp_seq=2 ttl=64 time=6.68 ms
64 bytes from 192.168.0.202: icmp_seq=3 ttl=64 time=6.62 ms
^C
--- 192.168.0.202 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2006ms
rtt min/avg/max/mdev = 6.626/18.932/43.492/17.366 ms
alex@alex-desktop:~$
alex@alex-desktop:~$
alex@alex-desktop:~$ ftp 192.168.0.202
Connected to 192.168.0.202.
220 FriendlyARM FTP server (Version 6.4/OpenBSD/Linux-ftpd-0.17) ready.
Name (192.168.0.202:alex):
331 Password required for alex.
Password:
530 Login incorrect.
Login failed.
ftp>

这里是我编译的所有模块,使用时可解压到开发板文件系统的/lib目录下,Gadget模块位于/lib/modules/2.6.29-FriendlyARM/kernel/drivers/usb/gadget目录下。
附件: linux-2.6.29-modules.tar.bz2 (62 K) 下载次数:204
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
1楼  发表于: 2009-05-05 08:03
关于g_serial,详细的信息可以参考内核源代码目录下的Documentation/usb/gadget_serial.txt
关于USB Gadget各个模块的介绍,可参考drivers/usb/gadget/Kconfig中个模块的相关描述
欢迎各位补充
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 侠客
UID: 5071
精华: 0
发帖: 50
金钱: 390 两
威望: 297 点
综合积分: 100 分
注册时间: 2009-04-09
最后登录: 2013-12-20
2楼  发表于: 2009-05-05 09:23
哪位兄弟试过g_file_storage.ko?我这反正是效果不咋地……
级别: 新手上路
UID: 2346
精华: 0
发帖: 38
金钱: 340 两
威望: 294 点
综合积分: 76 分
注册时间: 2008-11-12
最后登录: 2019-05-09
3楼  发表于: 2009-05-07 23:32
就是g_file_storage.ko加载有问题
级别: 新手上路
UID: 5828
精华: 0
发帖: 34
金钱: 250 两
威望: 158 点
综合积分: 68 分
注册时间: 2009-05-10
最后登录: 2013-06-28
4楼  发表于: 2009-06-19 09:49
--- a/drivers/usb/gadget/file_storage.c    2008-09-01 11:13:03.000000000 -0400
+++ b/drivers/usb/gadget/file_storage.c    2008-09-01 11:16:41.000000000 -0400
@@ -1463,7 +1463,8 @@
    if (rc >= 0 && rc != DELAYED_STATUS) {
        rc = min(rc, w_length);
        fsg->ep0req->length = rc;
-        fsg->ep0req->zero = rc < w_length;
+        fsg->ep0req->zero = rc < w_length
+            && (rc % gadget->ep0->maxpacket) == 0;
        fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
                "ep0-in" : "ep0-out");
        rc = ep0_queue(fsg);
级别: 新手上路
UID: 5828
精华: 0
发帖: 34
金钱: 250 两
威望: 158 点
综合积分: 68 分
注册时间: 2009-05-10
最后登录: 2013-06-28
5楼  发表于: 2009-06-19 11:07
This is a patch that seems to make the USB hangs on the S3C2440 go away. At
least a good amount of ping torture didn't make them come back so far.

The issue is that, if there are several back-to-back packets,
sometimes no interrupt is generated for one of them. This
seems to be caused by the mysterious dual packet mode, which
the USB hardware enters automatically if the endpoint size is
half that of the FIFO. (On the 2440, this is the normal
situation for bulk data endpoints.)

There is also a timing factor in this. I think what happens is
that the USB hardware automatically sends an acknowledgement
if there is only one packet in the FIFO (the FIFO has space
for two). If another packet arrives before the host has
retrieved and acknowledged the previous one, no interrupt is
generated for that second one.

However, there may be an indication. There is one undocumented
bit (none of the 244x manuals document it), OUT_CRS1_REG[1],
that seems to be set suspiciously often when this condition
occurs. There is also CLR_DATA_TOGGLE, OUT_CRS1_REG[7], which
may have a function related to this. (The Samsung manual is
rather terse on that, as usual.)

This needs to be examined further. For now, the patch seems to do the
trick.

Note that this is not a clean solution by any means, because we
might potentially get stuck in that interrupt for quite a while.

Index: linux-2.6.28.8/drivers/usb/gadget/s3c2410_udc.c
===================================================================
--- linux-2.6.28.8.orig/drivers/usb/gadget/s3c2410_udc.c    2009-03-17 21:14:28.000000000 +0100
+++ linux-2.6.28.8/drivers/usb/gadget/s3c2410_udc.c    2009-03-17 21:14:46.000000000 +0100
@@ -843,6 +843,7 @@
     u32            ep_csr1;
     u32            idx;

+handle_ep_again:
     if (likely (!list_empty(&ep->queue)))
         req = list_entry(ep->queue.next,
                 struct s3c2410_request, queue);
@@ -882,6 +883,8 @@

         if ((ep_csr1 & S3C2410_UDC_OCSR1_PKTRDY) && req) {
             s3c2410_udc_read_fifo(ep,req);
+            if (s3c2410_udc_fifo_count_out())
+                goto handle_ep_again;
         }
     }
}
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
6楼  发表于: 2009-06-19 11:59

 回 5楼(lpc2292) 的帖子

谢谢分享
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 5828
精华: 0
发帖: 34
金钱: 250 两
威望: 158 点
综合积分: 68 分
注册时间: 2009-05-10
最后登录: 2013-06-28
7楼  发表于: 2009-06-19 16:20
mini2440HOST口接U盘DEVICE口接PC机,传输26.2M 文件用了1分钟,400多k的速率
级别: 新手上路
UID: 5828
精华: 0
发帖: 34
金钱: 250 两
威望: 158 点
综合积分: 68 分
注册时间: 2009-05-10
最后登录: 2013-06-28
8楼  发表于: 2009-06-19 16:33
// drivers/usb/gadget/s3c2410_udc.c
static int __init udc_init(void)
{
        int retval;
        ……..
#if 0
        s3c2410_udc_debugfs_root = debugfs_create_dir(gadget_name, NULL);
        if (IS_ERR(s3c2410_udc_debugfs_root)) {
                printk(KERN_ERR "%s: debugfs dir creation failed %ld\n",
                        gadget_name, PTR_ERR(s3c2410_udc_debugfs_root));
                s3c2410_udc_debugfs_root = NULL;
        }      
#endif
        ………
}
级别: 侠客
UID: 3595
精华: 4
发帖: 53
金钱: 425 两
威望: 213 点
综合积分: 186 分
注册时间: 2009-01-20
最后登录: 2011-10-18
9楼  发表于: 2010-01-12 09:07
请问楼主,我按楼主的方法,终于在友善的2.6.29.4的内核中,加入了g_file_storage.ko的功能。现在,windows可以正常识别到U盘。但是,对它进行格式化,却报不能完成格式化,偶而能完成,但也无法拷贝文件进去?我用/dev/mtdblock2,/devmtdblock3均试过,都不行。又按照网上的方法,在内存中开了一片区域,同样也不行,情况相同。使用内存中的区域时,格式化基本能完成,但不能拷贝数据。不知何故?楼主遇到过这样的情况吗?
  • «
  • 1
  • 2
  • 3
  • »
  • Pages: 1/3     Go