Você está na página 1de 10

8/13/13

Write For Us Submit Tips

Device Drivers, Part 13: Data Transfer to and from USB Devices - LINUX For You
Subscribe to Print Edition Search

HOME

REVIEWS

HOW-TOS

CODING

INTERVIEWS

FEATURES

OVERVIEW

BLOGS

SERIES

IT ADMIN

Device Drivers, Part 13: Data Transfer to and from USB Devices
By Anil Kumar Pugalia on December 29, 2011 in Coding, Developers 64 Comments

Search for:

Search

Get Connected RSS Feed Twitter

This article, which is part of the series on Linux device drivers, continues from the previous two articles. It details the ultimate step of data transfer to and from a USB device, using your first USB driver in Linux.
Pugs continued, To answer your question about how a driver selectively registers or skips a particular interface of a USB device, you need to understand the significance of the return value of the p r o b e ( )callback. Note that the USB core would invoke probe for all the interfaces of a detected device, except the ones which are already registered thus, while doing it for the first time, it will probe for all interfaces. Now, if the probe returns 0, it means the driver has registered for that interface. Returning an error code indicates not registering for it. Thats all. That was simple, commented Shweta. Now, lets talk about the ultimate data transfers to and from a USB device, continued Pugs. But before that, tell me, what is this MODULE_DEVICE_TABLE? This has been bothering me since you explained the USB device ID table macros, asked Shweta, urging Pugs to slow down. Thats trivial stuff. It is mainly for the user-space d e p m o d , he said. Module is another term for a driver, which can be dynamically loaded/unloaded. The macro M O D U L E _ D E V I C E _ T A B L E generates two variables in a modules read-only section, which is extracted by d e p m o dand stored in global map files under / l i b / m o d u l e s / < k e r n e l _ v e r s i o n > . Two such files are
m o d u l e s . u s b m a pand m o d u l e s . p c i m a p , for USB and PCI device drivers, respectively. This

LINUX For You on

Follow

enables auto-loading of these drivers, as we saw the usb-storage driver getting auto-loaded.

+2,490

USB data transfer


Time for USB data transfers. Lets build upon the USB device driver coded in our previous sessions, using the same handy JetFlash pen drive from Transcend, with vendor ID 0 x 0 5 8 fand product ID 0 x 6 3 8 7 , said Pugs, enthusiastically. USB, being a hardware protocol, forms the usual horizontal layer in the kernel space. And hence, for it to provide an interface to user-space, it has to connect through one of the vertical layers. As the character (driver) vertical has already been discussed, it is the current preferred choice

www.linuxforu.com/2011/12/data-transfers-to-from-usb-devices/

1/10

8/13/13
flow.

Device Drivers, Part 13: Data Transfer to and from USB Devices - LINUX For You
Find us on Facebook

for the connection with the USB horizontal, in order to understand the complete data transfer

Open Source For You


Like 252,557 people like Open Source For You.

Also, we do not need to get a free unreserved character major number, but can use the character major number 180, reserved for USB-based character device files. Moreover, to achieve this complete character driver logic with the USB horizontal in one go, the following are the APIs declared in < l i n u x / u s b . h > :
i n tu s b _ r e g i s t e r _ d e v ( s t r u c tu s b _ i n t e r f a c e* i n t f ,s t r u c tu s b _ c l a s s _ d r i v e r* c l a s s _ d r i v e r ) ; v o i du s b _ d e r e g i s t e r _ d e v ( s t r u c tu s b _ i n t e r f a c e* i n t f ,s t r u c tu s b _ c l a s s _ d r i v e r* c l a s s _ d r i v e r ) ;

Usually, we would expect these functions to be invoked in the constructor and the destructor of a module, respectively. However, to achieve the hot-plug-n-play behaviour for the (character) device files corresponding to USB devices, these are instead invoked in the probe and disconnect callbacks, respectively. The first parameter in the above functions is the interface pointer received as the first parameter in both probe and disconnect. The second parameter, s t r u c tu s b _ c l a s s _ d r i v e r , needs to be populated with the suggested device file name and the set of device file operations, before invoking u s b _ r e g i s t e r _ d e v . For the actual usage, refer to the functions p e n _ p r o b eand
p e n _ d i s c o n n e c tin the code listing of p e n _ d r i v e r . cbelow.
Popular Comments Tag cloud
F acebook social plugin

May 6, 2013 5 Comments Priyanka Sarkar

PHP Development: A Smart Career Move


June 20, 2013 3 Comments Priyanka Sarkar

What it Takes to be an Open Source Expert


June 20, 2013 2 Comments sophie-samuel

Moreover, as the file operations (write, read, etc.,) are now provided, that is exactly where we need to do the data transfers to and from the USB device. So, p e n _ w r i t eand p e n _r e a dbelow show the possible calls to u s b _ b u l k _ m s g ( )(prototyped in < l i n u x / u s b . h > ) to do the transfers over the pen drives bulk end-points 001 and 082, respectively. Refer to the E lines of the middle section in Figure 1 for the endpoint number listings of our pen drive.

New and amazing features of Linux


May 6, 2013 1 Comments Deepti Sharma

A Simple guide to building your own Linux Kernel


May 6, 2013 1 Comments Prashant Phatak

Cyber Attacks Explained: Cryptographic Attacks

Figure 1: USB specifications for the pen drive

Refer to the header file < l i n u x / u s b . h >under kernel sources, for the complete list of USB core API prototypes for other endpoint-specific data transfer functions like u s b _ c o n t r o l _ m s g ( ) , u s b _ i n t e r r u p t _ m s g ( ) , etc. u s b _ r c v b u l k p i p e ( ) ,u s b _ s n d b u l k p i p e ( ) , and many such other macros, also defined in < l i n u x / u s b . h > , compute the actual endpoint bit-mask to be passed to the various USB core APIs. Note that a pen drive belongs to a USB mass storage class, which expects a set of SCSI-like commands to be transacted over the bulk endpoints. So, a raw read/write as shown in the code listing below may not really do a data transfer as expected, unless the data is appropriately formatted. But still, this summarises the overall code flow of a USB driver. To get a feel of a real working USB data transfer in a simple and elegant way, one would need some kind of custom USB device, something like the one available here.
1 2 3 4 5 6 7 8 # i n c l u d e< l i n u x / m o d u l e . h > # i n c l u d e< l i n u x / k e r n e l . h > # i n c l u d e< l i n u x / u s b . h > # d e f i n eM I N ( a , b )( ( ( a )< =( b ) )?( a ):( b ) ) # d e f i n eB U L K _ E P _ O U T0 x 0 1 # d e f i n eB U L K _ E P _ I N0 x 8 2 # d e f i n eM A X _ P K T _ S I Z E5 1 2

www.linuxforu.com/2011/12/data-transfers-to-from-usb-devices/

2/10

8/13/13
9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 6 0 6 1 6 2 6 3 6 4 6 5 6 6 6 7 6 8 6 9 7 0 7 1 7 2 7 3 7 4 7 5 7 6 7 7 7 8 7 9 8 0 8 1 8 2 8 3 8 4 8 5 8 6 8 7 8 8 8 9 9 0 9 1 9 2 9 3 9 4 9 5 9 6 9 7 9 8 9 9 1 0 0 1 0 1 1 0 2 1 0 3 1 0 4 1 0 5 1 0 6 1 0 7 1 0 8 1 0 9 1 1 0 1 1 1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7

Device Drivers, Part 13: Data Transfer to and from USB Devices - LINUX For You
s t a t i cs t r u c tu s b _ d e v i c e* d e v i c e ; s t a t i cs t r u c tu s b _ c l a s s _ d r i v e rc l a s s ; s t a t i cu n s i g n e dc h a rb u l k _ b u f [ M A X _ P K T _ S I Z E ] ; s t a t i ci n tp e n _ o p e n ( s t r u c ti n o d e* i ,s t r u c tf i l e* f ) { r e t u r n0 ; } s t a t i ci n tp e n _ c l o s e ( s t r u c ti n o d e* i ,s t r u c tf i l e* f ) { r e t u r n0 ; } s t a t i cs s i z e _ tp e n _ r e a d ( s t r u c tf i l e* f ,c h a r_ _ u s e r* b u f ,s i z e _ tc n t ,l o f f _ t* o f f ) { i n tr e t v a l ; i n tr e a d _ c n t ; / *R e a dt h ed a t af r o mt h eb u l ke n d p o i n t* / r e t v a l=u s b _ b u l k _ m s g ( d e v i c e ,u s b _ r c v b u l k p i p e ( d e v i c e ,B U L K _ E P _ I N ) , b u l k _ b u f ,M A X _ P K T _ S I Z E ,& r e a d _ c n t ,5 0 0 0 ) ; i f( r e t v a l ) { p r i n t k ( K E R N _ E R R" B u l km e s s a g er e t u r n e d% d \ n " ,r e t v a l ) ; r e t u r nr e t v a l ; } i f( c o p y _ t o _ u s e r ( b u f ,b u l k _ b u f ,M I N ( c n t ,r e a d _ c n t ) ) ) { r e t u r nE F A U L T ; } r e t u r nM I N ( c n t ,r e a d _ c n t ) ; } s t a t i cs s i z e _ tp e n _ w r i t e ( s t r u c tf i l e* f ,c o n s tc h a r_ _ u s e r* b u f ,s i z e _ tc n t ,l o f f _ t* o f f ) { i n tr e t v a l ; i n tw r o t e _ c n t=M I N ( c n t ,M A X _ P K T _ S I Z E ) ; i f( c o p y _ f r o m _ u s e r ( b u l k _ b u f ,b u f ,M I N ( c n t ,M A X _ P K T _ S I Z E ) ) ) { r e t u r nE F A U L T ; } / *W r i t et h ed a t ai n t ot h eb u l ke n d p o i n t* / r e t v a l=u s b _ b u l k _ m s g ( d e v i c e ,u s b _ s n d b u l k p i p e ( d e v i c e ,B U L K _ E P _ O U T ) , b u l k _ b u f ,M I N ( c n t ,M A X _ P K T _ S I Z E ) ,& w r o t e _ c n t ,5 0 0 0 ) ; i f( r e t v a l ) { p r i n t k ( K E R N _ E R R" B u l km e s s a g er e t u r n e d% d \ n " ,r e t v a l ) ; r e t u r nr e t v a l ; } } r e t u r nw r o t e _ c n t ;

s t a t i cs t r u c tf i l e _ o p e r a t i o n sf o p s= { . o p e n=p e n _ o p e n , . r e l e a s e=p e n _ c l o s e , . r e a d=p e n _ r e a d , . w r i t e=p e n _ w r i t e , } ; s t a t i ci n tp e n _ p r o b e ( s t r u c tu s b _ i n t e r f a c e* i n t e r f a c e ,c o n s ts t r u c tu s b _ d e v i c e _ i d* i d ) { i n tr e t v a l ; d e v i c e=i n t e r f a c e _ t o _ u s b d e v ( i n t e r f a c e ) ; c l a s s . n a m e=" u s b / p e n % d " ; c l a s s . f o p s=& f o p s ; i f( ( r e t v a l=u s b _ r e g i s t e r _ d e v ( i n t e r f a c e ,& c l a s s ) )<0 ) { / *S o m e t h i n gp r e v e n t e du sf r o mr e g i s t e r i n gt h i sd r i v e r* / e r r ( " N o ta b l et og e tam i n o rf o rt h i sd e v i c e . " ) ; } e l s e { p r i n t k ( K E R N _ I N F O" M i n o ro b t a i n e d :% d \ n " ,i n t e r f a c e > m i n o r ) ; } } r e t u r nr e t v a l ;

s t a t i cv o i dp e n _ d i s c o n n e c t ( s t r u c tu s b _ i n t e r f a c e* i n t e r f a c e ) { u s b _ d e r e g i s t e r _ d e v ( i n t e r f a c e ,& c l a s s ) ; } / *T a b l eo fd e v i c e st h a tw o r kw i t ht h i sd r i v e r* / s t a t i cs t r u c tu s b _ d e v i c e _ i dp e n _ t a b l e [ ]= { {U S B _ D E V I C E ( 0 x 0 5 8 F ,0 x 6 3 8 7 )} , { }/ *T e r m i n a t i n ge n t r y* / } ; M O D U L E _ D E V I C E _ T A B L E( u s b ,p e n _ t a b l e ) ; s t a t i cs t r u c tu s b _ d r i v e rp e n _ d r i v e r= { . n a m e=" p e n _ d r i v e r " , . p r o b e=p e n _ p r o b e , . d i s c o n n e c t=p e n _ d i s c o n n e c t , . i d _ t a b l e=p e n _ t a b l e , } ; s t a t i ci n t_ _ i n i tp e n _ i n i t ( v o i d ) { i n tr e s u l t ;

www.linuxforu.com/2011/12/data-transfers-to-from-usb-devices/

3/10

8/13/13
1 1 8 1 1 9 1 2 0 1 2 1 1 2 2 1 2 3 1 2 4 1 2 5 1 2 6 1 2 7 1 2 8 1 2 9 1 3 0 1 3 1 1 3 2 1 3 3 1 3 4 1 3 5 1 3 6 1 3 7

Device Drivers, Part 13: Data Transfer to and from USB Devices - LINUX For You
/ *R e g i s t e rt h i sd r i v e rw i t ht h eU S Bs u b s y s t e m* / i f( ( r e s u l t=u s b _ r e g i s t e r ( & p e n _ d r i v e r ) ) ) { e r r ( " u s b _ r e g i s t e rf a i l e d .E r r o rn u m b e r% d " ,r e s u l t ) ; } r e t u r nr e s u l t ;

s t a t i cv o i d_ _ e x i tp e n _ e x i t ( v o i d ) { / *D e r e g i s t e rt h i sd r i v e rw i t ht h eU S Bs u b s y s t e m* / u s b _ d e r e g i s t e r ( & p e n _ d r i v e r ) ; } m o d u l e _ i n i t ( p e n _ i n i t ) ; m o d u l e _ e x i t ( p e n _ e x i t ) ; M O D U L E _ L I C E N S E ( " G P L " ) ; M O D U L E _ A U T H O R ( " A n i lK u m a rP u g a l i a< e m a i l _ a t _ s a r i k a p u g s _ d o t _ c o m > " ) ; M O D U L E _ D E S C R I P T I O N ( " U S BP e nD e v i c eD r i v e r " ) ;

As a reminder, the usual steps for any Linux device driver may be repeated with the above code, along with the following steps for the pen drive: Build the driver (p e n _ d r i v e r . k o ) by running m a k e . Load the driver using i n s m o dp e n _ d r i v e r . k o . Plug in the pen drive (after making sure that the u s b s t o r a g edriver is not already loaded). Check for the dynamic creation of / d e v / p e n 0(0 being the minor number obtained check d m e s glogs for the value on your system). Possibly try some write/read on / d e v / p e n 0(you most likely will get a connection timeout and/or broken pipe errors, because of non-conforming SCSI commands). Unplug the pen drive and look for / d e v / p e n 0to be gone. Unload the driver using r m m o dp e n _ d r i v e r . Meanwhile, Pugs hooked up his first-of-its-kind creation the Linux device driver kit (LDDK) into his system for a live demonstration of the USB data transfers. Aha! Finally a cool complete working USB driver, quipped Shweta, excited. Want to have more fun? We could do a block driver over it, added Pugs. Oh! Really? asked Shweta, with glee. Yes. But before that, we need to understand the partitioning mechanisms, commented Pugs.

Related Posts:
Device Drivers, Part 12: USB Drivers in Linux Continued Device Drivers, Part 11: USB Drivers in Linux Device Drivers, Part 6: Decoding Character Device File Operations Device Drivers, Part 5: Character Device Files Creation & Operations Device Drivers, Part 4: Linux Character Drivers
Tags: data transfers, depmod, device id, kernel space, LFY December 2011, linux device drivers, Linux Device Drivers Series, pci device drivers, pen drive, product id, SCSI, Universal Serial Bus, USB, usb device driver, usb devices, usb driver, usb storage driver, vendor id

Article written by:


Anil Kumar Pugalia
The author is a freelance trainer in Linux internals, Linux device drivers, embedded Linux and related topics. Prior to this, he had worked at Intel and Nvidia. He has been exploring Linux since 1994. A gold medallist from the Indian Institute of Science, Linux and knowledge-sharing are two of his many passions. Connect with him: Website - Twitter - Facebook - Google+

Previous Post

Next Post

Loading Library Files in C++

Getting Started with SystemTap

AROUND THE WEB

ALSO ON LINUX FOR YOU

What's this?

Pastor Reveals 7 Shocking Biblical Truths on Investing Moneynews Don't Get Alzheimer's: Here's What May Cause It Newsmax Health The impact of having kids at an older age New Republic 'X-Force' Will Be the Next X-Men Movie Stack

OpenMP Schedule Clause Parallel Matrix Multiplication 7 months ago Getting Your First Job Code Sport
1 comment 1 comment

File Systems A Semester Project-II, Part-19 6 comments


4/10

www.linuxforu.com/2011/12/data-transfers-to-from-usb-devices/

8/13/13

Device Drivers, Part 13: Data Transfer to and from USB Devices - LINUX For You
Stack

Movie

Part-19

6 comments

64 comments Leave a message...


Newest S id Community

Share

2 months ago

Sir, I would like to know which of the function gets the access first. .read or .write?
Reply Share

anil_pugalia

> Sid

2 months ago


Midhun

What do you mean by access first? It is which the user calls first.
Reply Share

2 months ago

Hi Anil,thanks for this session of knowledge transfer. I have one query about how the file system inside usb is mounting to linux existing filesystem.Could u help us with that also?
Reply Share

anil_pugalia

> Midhun

2 months ago


jay a

For that you may have to read my article #18 to #24 on file system modules. As of this comment, most of these 7 articles are available only in LFY hard copy.
Reply Share

5 months ago

i have successfully completed all process /dev/pen0 is also there in /dev ... now what is next ... what should i do next to see read and write working .... i tried this .. but error is coming .. root@pcch-ee206270:/home/pcch-ee206270/Desktop/bb/usb_read_write# echo "hi" > /dev/pen0 bash: echo: write error: Invalid argument
Reply Share

anil_pugalia

> jaya

5 months ago


jay a

These various errors are expected and as mentioned in the last para of the article.
Reply Share

5 months ago

PLS someone tell me abt the funda of this usb_class_drive .. its been used many times in above code .. like below .. what this struct actually defines ..?? static struct usb_class_driver class; class.name = "usb/pen%d"; class.fops = &fops; if ((retval = usb_register_dev(interface, &class)) < 0)
Reply Share

anil_pugalia

> jaya

5 months ago


jay a

This is more like giving the suggested device file name & the file operations for the vertical layer.
Reply Share

5 months ago

getting error of permisions inspite of doing chmod command .....pls reply root@pcch-ee206270:/home/pcch-ee206270/Desktop/bb/usb_read_write# chmod 777 /dev/pen0 root@pcch-ee206270:/home/pcch-ee206270/Desktop/bb/usb_read_write# echo "hi" | /dev/pen0 bash: /dev/pen0: Permission denied www.linuxforu.com/2011/12/data-transfers-to-from-usb-devices/

5/10

8/13/13

Device Drivers, Part 13: Data Transfer to and from USB Devices - LINUX For You bash: /dev/pen0: Permission denied root@pcch-ee206270:/home/pcch-ee206270/Desktop/bb/usb_read_write#
Reply Share

anil_pugalia

> jaya

5 months ago


jay a

Please replace | by >, i.e. echo "hi" > /dev/pen0 Moreover, you do not need chmod 777, just 666 should be good enough.
Reply Share

5 months ago

pls tell me differrence btw usb_register_dev(interface, &class)) && usb_register(&pen_driver)) function call i think both are for registering usb with usb subsystem
Reply Share

anil_pugalia

> jaya

5 months ago


ay us h

usb_register registers with the horizontal usb core. usb_register_dev registers the vertical with VFS.
Reply Share

5 months ago

can any one pls tell me what are the parameter passed in this usb_bulk_msg function .. retval = usb_bulk_msg(device, usb_rcvbulkpipe(device, BULK_EP_IN), bulk_buf, MAX_PKT_SIZE, &read_cnt, 5000);
1

Reply

Share

anil_pugalia

> ayush

5 months ago


anon

Check out its prototype in the header file <linux/usb.h>


Reply Share

5 months ago

stuck with the problem - while reading, usb_bulk_msg returns error 22 - Invalid argument. Write operation succeeds.
Reply Share

anil_pugalia

> anon

5 months ago


anon

As mentioned below, check out the endpoint number on your pen drive. If it is different correct it accordingly.
Reply Share

5 months ago

I stuck with the problem - while reading, usb_bulk_msg returns error 22 - Invalid argument. Write operation succeeds.
Reply anon Share

> anon

5 months ago


res que

sorry for spamming, i was confused how to read newest posts.


Reply Share

5 months ago

While reading from pen device cat /dev/pen0 bulk message returned error 22 - Invalid argument
Reply Share

anil_pugalia

> resque

5 months ago


ME LW IN

Possibly the endpoint number on your pen drive is different. Check it out & correct it accordingly.
Reply Share

7 months ago

In the pen_read function the usb_bulk_msg returns an ERROR NO of -32, which is the PIPE BROKEN errno. I ran the same code here. Help !!!
1

Reply

Share

anil_pugalia

> MELWIN

2 months ago

www.linuxforu.com/2011/12/data-transfers-to-from-usb-devices/

6/10

8/13/13

Device Drivers, Part 13: Data Transfer to and from USB Devices - LINUX For You


jay as ant os h

That's as expected. Refer to the article.


Reply Share

7 months ago

not required to recompile the kernel as anil describe . just compile your module in the name of usb-storage and replace the .ko file in the folder /lib/modules/{your kernel version}/kernel/drivers/usb/storage/usb-storage.ko with your usb-storage.ko file.(note pls take backup of kernel usb-storage.ko)
4

Reply

Share

MenDuong

8 months ago

Dear, Why I insmod pen_driver.ko appear error message "insmod: error inserting 'pen_driver.ko': -1 Device or resource busy". Thanks
1 Reply Share

anil_pugalia

> MenDuong
Share

8 months ago

Please paste the output of "dmesg | tail -20", to be able to decode that error.
Reply

MenDuong

> anil_pugalia
Share

8 months ago


aaa

Okays. I resolved that error. Cause I haven't removed pen_info module previous. Thanks!
Reply

9 months ago

Sir, probe is called per interface and it gets called by the usb-core . But In a multifunctional device , how to distinguish between different interfaces becoz what I observed is , we are just mentioning the vendor id and device id in the usb driver structure , which will be same for all the interfaces becoz the device is same . My question is how will the kernel know , which interface has to be called in a multifunctional device ? how does the kernel distinguish between different interfaces of the same device ?
1

Reply

Share

anil_pugalia

> aaa

9 months ago

Note that the probe function has the first parameter as the pointer to the interface structure, which contains the interface number. So, on probe invocation, we know as for which interface has this probe been called.
1

Reply

Share

manas jy ot i s armah

9 months ago

Sir, Please explain the read() and write() methods.I am unable to implement them.
1

Reply

Share

anil_pugalia

> manas jyoti sarmah


Share

9 months ago

Please share your problem in implementation, and we'll try to solve that.

Reply

A vijit S ur

10 months ago

Very nicely explained!!! Could you please start another chapter on "partitioning mechanisms" ?
1

Reply

Share

anil_pugalia

> Avijit Sur

9 months ago

Check out the 14th article in the series. Here goes, the link: http://www.linuxforu.com/2012/...
1 plr

Reply

Share

11 months ago

hi sir, To answer your question about how a driver selectively registers or skips a particular interface of a USB device, you need to understand the significance of the return value of the probe() callback. Note that the USB core would invoke probe for all the interfaces of a detected device, except the ones which are already registered thus, www.linuxforu.com/2011/12/data-transfers-to-from-usb-devices/

7/10

8/13/13

Device Drivers, Part 13: Data Transfer to and from USB Devices - LINUX For You detected device, except the ones which are already registered thus, while doing it for the first time, it will probe for all interfaces. Now, if the probe returns 0, it means the driver has registered for that interface. Returning an error code indicates not registering for it. Thats all. That was simple, commented Shweta.

I dint understand this correctly. What I have understood is , an usb device can be multifunctional and for each functionality there will be a corresponding interface . So a driver is written for an interface rather than the device itself . Now when the device is plugged in , there would be multiple drivers written for multiple interfaces . So the probe function of all the interfaces is called ? This is ok . But there can be different vertical drivers assciated with different interfaces and these verticals can have thier own operations defined (fops in case of charcter vertical) . So when a open is called , how will the kernel know ,open is associated with which particular interface ?
Reply Share

A nil P ugalia

> plr

11 months ago

Kernel would know about the open, as per the registration done by the vertical with the VFS
Reply Share

anil_pugalia

> Anil Pugalia

9 months ago


s hiavng

And in the driver, we would register the corresponding minor, as to which interface we are going to handle (as per the USB device data sheet). So, there would always be a unique mapping.
Reply Share

a year ago

very nice explanation and it gives motivation to those who are learning device driver... very admiring work....keep world more technical through sharing such information and innovation on drivers.
Reply Share

anil_pugalia

> shiavng

9 months ago


rk art hik reddy

Thanks for your admiration.


Reply

Share

a year ago

where can i get linux DDK ? i found a link to a web site for downloading DDK but the server is down!
Reply Share

anil_pugalia

> rkarthikreddy
Share

9 months ago

You can get it here: http://lddk.esrijan.com


Reply Marjan

> anil_pugalia

2 months ago

There is an error in source! You should not allocate buffer globally. Microchip FSUSB demo board did not work by me. I allocated buffer in write_msg function. Then, it worked.
Reply Share

anil_pugalia

> Marjan

2 months ago

Can you explain as what do you mean by "not working" with global buffer? I assume that when you allocated the buffer in write_msg, you freed it also, there itself.
Reply Share

Marjan

> anil_pugalia

a month ago

One more thing: this was kernel 3.5.0. What happened was this: static buffer contained correct data from userspace. Device however received corrupted data. When I moved this buffer variable into red/write rutines, it fixed the problem. www.linuxforu.com/2011/12/data-transfers-to-from-usb-devices/

8/10

8/13/13

Device Drivers, Part 13: Data Transfer to and from USB Devices - LINUX For You the problem.

It was just a char array. One more question: how does kernel distinguish between identical devices (literally cloned; same deviceID, same vendor ID, same serial, BCD code, etc.)? Kernel assigns different minor numbers. But I have a problem "how to send data to correct device (or read from it)" when device file read/write in userspace happens.
Reply Share

anil_pugalia

> Marjan

a month ago

Possibly a race condition of simultaneous access of the buffer. Your probe will get called multiple times for each interface of the multiple devices, and accordingly distinct minors should be allotted to each. Also, in each probe the corresponding device pointer should be preserved into the private data of the corresponding interface, and then possibly the struct file, in case of system calls. With that the system calls would access the correct device, from their struct file's private data.
Reply Share

Marjan

> anil_pugalia

a month ago

There was one single access to driver at the time (as far as I am aware of), and this was my test program. So I guess I'll have to implement this multiple devices handling mechanysm inside of the driver by myself.
Reply Share

anil_pugalia

> Marjan

a month ago

Agreed that you had only single access from the user space but there could be other accesses from within the kernel, itself. And yes, support for handling multiple devices has to be provided from within the driver only. Once the support is there, then it is left to the applications, what do they do with that.
Reply Share

Haris h Nanjas et t y

> anil_pugalia

a month ago

Hi sir, I tried with program i was sucessful with but i faced problems implementing use program.(using read and right calls). with this can i transfer a entire file and read file how is the criteria for efficient read and write to drive harry_mysooru@yahoo.com
Reply Share

anil_pugalia

> Harish Nanjasetty


Share

a month ago

This doesn't implement the complete usb storage protocol - so you may not be able to do an actual read/write.
Reply

Haris h Nanjas et t y

> anil_pugalia

a month ago

Thanks for u reply Sir i want about high mem,and low mem. kmalloc allocates memory in low mem i.e after linux image sits? what is high mem, entire 896> to 4GB is high mem how different is this memory from 4 GB process address space

How exactly CFS works


Reply Share

anil_pugalia

> Harish Nanjasetty

a month ago

high mem - 640K to 4GB is DOS days concept. The high mem in Linux kernel context is a very different thing. In kernel, low mem & high mem is just the differentiation whether the memory is directly mapped & accessed or indirectly mapped through a window. 4GB process address space could map to any one or
www.linuxforu.com/2011/12/data-transfers-to-from-usb-devices/ 9/10

8/13/13

Device Drivers, Part 13: Data Transfer to and from USB Devices - LINUX For You window. 4GB process address space could map to any one or both of them, but typically done with low mem.

For working of CFS, check out the CFS code.


Reply Share

Haris h Nanjas et t y

> anil_pugalia

a month ago

Please explain more clearly on this line 4GB process address space could map to any one or both of them, but typically done with low mem.
Reply Share

Load more comments

C o m m e n t fe e d

Su b s cri b e vi a e m a i l

Reviews

How-Tos

Coding

Interviews

Features

Overview

Blogs

Search
Popular tags
Linux , ubuntu, Java, MySQL, Google, python, Fedora, Android, PHP, C, html, w eb applications , India, Microsoft, unix , Window s , Red Hat, Oracle, Security , Apache, xml, LFY April 2012, FOSS, GNOME, http, JavaScript, LFY June 2011, open source, RAM, operating systems

For You & Me Developers Sysadmins Open Gurus CXOs Columns

All published articles are released under Creative Commons Attribution-NonCommercial 3.0 Unported License, unless otherw ise noted. LINUX For You is pow ered by WordPress, w hich gladly sits on top of a CentOS-based LEMP stack.

www.linuxforu.com/2011/12/data-transfers-to-from-usb-devices/

10/10

Você também pode gostar