‏הצגת רשומות עם תוויות usb. הצג את כל הרשומות
‏הצגת רשומות עם תוויות usb. הצג את כל הרשומות

יום שלישי, 11 ביוני 2013

Enumerate USB Device interfaces

I wanted to enumerate the USB FX2 device interface endpoints.
I used the following code:

  1: struct fx2_device_type
  2: {
  3:   struct usb_interface *interface;   
  4:   struct usb_device * usbdev ;
  5: };
  6: 
  7: struct fx2_device_type *fx2_device;
  8: 
  9: static struct usb_device_id ml_table [] = {
 10:     { USB_DEVICE(ML_VENDOR_ID, ML_PRODUCT_ID) },
 11:     { }
 12: };
 13: 
 14: MODULE_DEVICE_TABLE(usb, ml_table);
 15: 
 16: void printHelper (char *pValueToPrint , char * description )
 17: {
 18: if (pValueToPrint != NULL )
 19:   {
 20:     printk(KERN_INFO "%s :%s",description,pValueToPrint);
 21:   }
 22: }
 23: static int ml_probe(struct usb_interface *interface, const struct usb_device_id  *id)
 24: {
 25:   char *product;
 26:   char *manufacturer;
 27:   char *serial;
 28:     /* called when a USB device is connected to the computer. */
 29:     printk(KERN_INFO "ml_probe");
 30:     struct usb_device *udev = interface_to_usbdev(interface);
 31:     
 32:     /* static strings from the device */
 33:   product = udev->product;
 34:   printHelper (product , "product");
 35:   
 36:   manufacturer = udev->manufacturer;
 37:   printHelper (manufacturer , "manufacturer");
 38:   
 39:   serial = udev->serial;
 40:   printHelper (serial , "serial");
 41:     
 42:   struct usb_host_interface *iface_desc;
 43: 
 44:   struct usb_endpoint_descriptor *endpoint;
 45: 
 46:   int retval = -ENOMEM;
 47: 
 48:   fx2_device = kzalloc(sizeof(struct fx2_device_type), GFP_KERNEL);
 49: 
 50:   fx2_device->interface = interface;
 51: 
 52:   fx2_device->usbdev = usb_get_dev(interface_to_usbdev(interface));
 53: 
 54:   iface_desc = interface->cur_altsetting;
 55: 
 56:   printk("The no of endpoints %d \n" , iface_desc->desc.bNumEndpoints);
 57:    
 58:   int i = 0; 
 59: 
 60:   for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
 61: 
 62:   endpoint = &iface_desc->endpoint[i].desc;
 63: 
 64:   int  theEnpointType = usb_endpoint_type(endpoint);
 65: 
 66:     switch ( theEnpointType )
 67:     {
 68:       case USB_ENDPOINT_XFER_CONTROL:
 69:         printk("Endpoint %d Control\n" , i);    
 70:       break;
 71:       case USB_ENDPOINT_XFER_ISOC:
 72:         printk("Endpoint %d ISOC\n" , i);    
 73:       break;
 74:       case USB_ENDPOINT_XFER_BULK:
 75:         printk("Endpoint %d BULK\n" , i);    
 76:       break;
 77:       case USB_ENDPOINT_XFER_INT  :
 78:         printk("Endpoint %d INT\n" , i);    
 79:       break;
 80:       default:
 81:         printk("Endpoint %d out of range\n" , i);  
 82:       break ;
 83:     }
 84: }

And the Result :
[ 7277.566609] ml_probe
[ 7277.566613] product :OSR USB-FX2 LK
[ 7277.566616] manufacturer :OSR.COM
                      The no of endpoints 3
[ 7277.566621] Endpoint 0 INT
[ 7277.566623] Endpoint 1 BULK
[ 7277.566626] Endpoint 2 BULK

יום שבת, 8 ביוני 2013

interface_to_usbdev

Keep on going with my usb fx2 linux driver
I had the following code to the probe method :

  1: void printHelper (char *pValueToPrint , char * description )
  2: {
  3:   if (pValueToPrint != NULL )
  4:   {
  5:      printk(KERN_INFO "%s :%s",description,pValueToPrint);
  6:   }
  7: }
  8: static int ml_probe(struct usb_interface *interface, const struct usb_device_id  *id)
  9: {
 10:   char *product;
 11:   char *manufacturer;
 12:   char *serial;
 13:     /* called when a USB device is connected to the computer. */
 14:     printk(KERN_INFO "ml_probe");
 15:     struct usb_device *udev = interface_to_usbdev(interface);
 16:     
 17:     
 18:       /* static strings from the device */
 19:   product = udev->product;
 20:   printHelper (product , "product");
 21:   
 22:   manufacturer = udev->manufacturer;
 23:   printHelper (manufacturer , "manufacturer");
 24:   
 25:   serial = udev->serial;
 26:   printHelper (serial , "serial");
 27:   
 28: return 0;
 29: 
 30: }

and the result :


[10871.554417] Initializing USB FX
[10871.554451] usbcore: registered new interface driver myFx2
[10871.554454] THE usb_register RET::0
[11033.435093] usb 1-1.1: new high-speed USB device number 4 using ehci_hcd
[11048.458267] usb 1-1.1: device descriptor read/64, error -110
[11063.585105] usb 1-1.1: device descriptor read/64, error -110
[11063.760533] usb 1-1.1: new high-speed USB device number 5 using ehci_hcd
[11078.783705] usb 1-1.1: device descriptor read/64, error -110
[11088.005908] hub 1-1:1.0: unable to enumerate USB device on port 1
[11127.297879] usb 1-1.1: new high-speed USB device number 6 using ehci_hcd
[11128.846084] ml_probe
[11128.846088] product :OSR USB-FX2 LK
[11128.846090] manufacturer :OSR.COM

The code use the method :interface_to_usbdev in order to get a usb_device form usb_interface object.Using the usb_device object the code get the usb  device static report about the manufacture and the product .

Resources:
http://www.lrr.in.tum.de/Par/arch/usb/download/usbdoc/usbdoc-1.32.pdf


יום שני, 3 ביוני 2013

Getting USB device Interfaces

 

The following code is part of the MissileLauncherActivity of the MissileLauncher api Demo.
The onResume method
The onResume method may be called due to intend invocation caused by the USB device plugin or disconnecting.
The method retrieve the UsbDevice object from the calling intend data. the USBDevice Repents a connected USB device and contains methods to access its identifying information, interfaces, and endpoints. .

  1:     @Override
  2:     public void onResume() {
  3:         super.onResume();
  4:         mSensorManager.registerListener(mGravityListener, mGravitySensor,
  5:                 SensorManager.SENSOR_DELAY_NORMAL);
  6: 
  7:         Intent intent = getIntent();
  8:         Log.d(TAG, "intent: " + intent);
  9:         String action = intent.getAction();
 10: 
 11:         UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
 12:         if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
 13:             setDevice(device);
 14:         } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
 15:             if (mDevice != null && mDevice.equals(device)) {
 16:                 setDevice(null);
 17:             }
 18:         }
 19:     }

The setDevice method
The setDevice method tries to pitch the USB interface out of the usbdevice and tries to fetch the USB interface type interrupt  out of this endpoint.
The method tries to open connection to the device using the UsbManager.

  1:  private void setDevice(UsbDevice device) {
  2:         Log.d(TAG, "setDevice " + device);
  3:         if (device.getInterfaceCount() != 1) {
  4:             Log.e(TAG, "could not find interface");
  5:             return;
  6:         }
  7:         UsbInterface intf = device.getInterface(0);
  8:         // device should have one endpoint
  9:         if (intf.getEndpointCount() != 1) {
 10:             Log.e(TAG, "could not find endpoint");
 11:             return;
 12:         }
 13:         // endpoint should be of type interrupt
 14:         UsbEndpoint ep = intf.getEndpoint(0);
 15:         if (ep.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) {
 16:             Log.e(TAG, "endpoint is not interrupt type");
 17:             return;
 18:         }
 19:         mDevice = device;
 20:         mEndpointIntr = ep;
 21:         if (device != null) {
 22:             UsbDeviceConnection connection = mUsbManager.openDevice(device);
 23:             if (connection != null && connection.claimInterface(intf, true)) {
 24:                 Log.d(TAG, "open SUCCESS");
 25:                 mConnection = connection;
 26:                 Thread thread = new Thread(this);
 27:                 thread.start();
 28: 
 29:             } else {
 30:                 Log.d(TAG, "open FAIL");
 31:                 mConnection = null;
 32:             }
 33:          }
 34:     }

Sources
http://developer.android.com/guide/topics/connectivity/usb/host.html

יום שבת, 1 ביוני 2013

Controlling the USB FX device from Android tablet

In order to control a usb device I used the USB host api I started to learn the topic using the android missilelauncher demo .

Changing the device_filter.xml file to describe the USB FX2 device.

  1: <resources>
  2:     <usb-device vendor-id="1351" product-id="4098" />
  3: </resources>
  4: 

I want to get a callback notification when  the USB FX 2 device get connected to the android device.


Add to the manifest


  1: <intent-filter>
  2:   <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
  3: </intent-filter>
  4: <intent-filter>
  5:  <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
  6: </intent-filter>
  7: <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
  8:     android:resource="@xml/device_filter" />
  9: <meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"
 10:     android:resource="@xml/device_filter" />

Register the broadcast activities in the onCreate method


  1: //register for USBFX2 attachment
  2: IntentFilter attachedFilter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);     
  3: IntentFilter detachedFilter = new IntentFilter(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
  4: registerReceiver(mUsbFX2AttachedReceiver, attachedFilter);
  5: registerReceiver(mUsbFX2AttachedReceiver, detachedFilter);

declaring the BroadcastReceiver for the attached and detached events:


  1: private final BroadcastReceiver mUsbFX2AttachedReceiver = new BroadcastReceiver()
  2:     {
  3:     @Override
  4:     public void onReceive(Context context, Intent intent)
  5:         {
  6:        String action = intent.getAction();
  7:       
  8:          if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
  9:         Toast.makeText(context, "UsbFX2 has connected", Toast.LENGTH_SHORT).show();                  
 10:          }
 11:          if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
 12:            Toast.makeText(context, "UsbFX2 has detached", Toast.LENGTH_SHORT).show();                  
 13:          }
 14:         }
 15:     };

Sources:


http://stackoverflow.com/questions/11191835/receive-intent-action-usb-device-attached-through-code
http://www.ezequielaceto.com.ar/techblog/?p=396 
http://developer.android.com/guide/topics/connectivity/usb/host.html
http://stackoverflow.com/questions/11638216/how-to-make-an-basic-android-usb-host-application

יום חמישי, 30 במאי 2013

usbfx2 device probing in linux

I wanted to exercise my USB device driver writing in Linux.
I wrote the following Skelton of USB device for my FX2 USB card
usbfx

  1: #include <linux/kernel.h>    /* We're doing kernel work */ 
  2: #include <linux/module.h>    /* Specifically, a module */ 
  3: #include <linux/fs.h> 
  4: #include <asm/uaccess.h>    /* for get_user and put_user */ 
  5: #include  <linux/slab.h> 
  6: #include <linux/timer.h> 
  7: #include <linux/mod_devicetable.h> 
  8: #include <linux/usb.h> 
  9:  
 10: #define SUCCESS 0 
 11: 
 12: #define DEBUG   
 13:   
 14: /*  
 15:  * Is the device open right now? Used to prevent 
 16:  * concurent access into the same device  
 17:  * http://www.osronline.com/hardware/osrfx2_32.pdf 
 18:  */ 
 19:  int Device_Open = 0; 
 20:  
 21: #define ML_VENDOR_ID 0x0547 
 22: #define ML_PRODUCT_ID 0x1002 
 23:   
 24: static struct usb_device_id ml_table [] = { 
 25:     { USB_DEVICE(ML_VENDOR_ID, ML_PRODUCT_ID) }, 
 26:     { } 
 27: }; 
 28: 
 29: MODULE_DEVICE_TABLE(usb, ml_table); 
 30: 
 31: static int ml_probe(struct usb_interface *interface, const struct usb_device_id  *id) 
 32: { 
 33:     /* called when a USB device is connected to the computer. */ 
 34:     printk(KERN_INFO "ml_probe");
 35: 
 36:   return 0;
 37: } 
 38:  
 39: static void ml_disconnect(struct usb_interface *interface) 
 40: { 
 41:     /* called when unplugging a USB device. */  
 42:     printk(KERN_INFO "ml_disconnect"); 
 43: } 
 44:   
 45: static struct usb_driver fx2_table = { 
 46:     .name = "myFx2", 
 47:     .id_table = ml_table, 
 48:     .probe = ml_probe, 
 49:     .disconnect = ml_disconnect, 
 50: }; 
 51:   
 52: /*  
 53:  * Initialize the module - Register the character device  
 54:  */ 
 55: int init_module() 
 56: {
 57:     int retval;
 58:      
 59:   printk(KERN_INFO "Initializing USB FX\n");
 60: 
 61:     retval = usb_register(&fx2_table);
 62:    
 63:     printk(KERN_ALERT"THE usb_register RET::%d\n",retval); 
 64:     
 65:   return 0; 
 66: }     
 67:  
 68: void cleanup_module() 
 69: { 
 70:      printk(KERN_INFO "cleanup_module\n"); 
 71: }
 72: 
 73: MODULE_LICENSE("GPL");

The make file :

  1: obj-m := usbfx.o 
  2: 
  3: KDIR  := /lib/modules/$(shell uname -r)/build
  4: PWD   := $(shell pwd)
  5: 
  6: default:
  7:     $(MAKE) -C $(KDIR) M=$(PWD) modules

After building the project with make I have tried to install it using the insmod command. and I got the following error:
"insmod: error inserting 'usbfx.ko': -1 Unknown symbol in module"
I tried to figure out the problem using dmseg comamnd .

The log entery of the problem:
[ 3569.172900] usbfx: Unknown symbol usb_register_driver (err 0)


The following article explain about the problem of GPL licensee shortcut.
I had line 73 in order to declare the module license as GPL

Trying to install the module now with insmod  then dmseg produce:


[ 4666.501865] Initializing USB FX
[ 4666.501894] usbcore: registered new interface driver myFx2
[ 4666.501897] THE usb_register RET::0
[ 4700.271816] usb 1-1.1: new high-speed USB device number 4 using ehci_hcd


Now connecting my usb fx device  to the usb port
dmseg produce
[ 4701.740804] ml_probe