You are on page 1of 35

1 Gary Bisson - ELC 2012

Useful USB Gadgets on Linux


February, 2012
Gary Bisson
Adeneo Embedded
Embedded Linux Conference 2012

2 Gary Bisson - ELC 2012
Agenda

Introduction to USB

USB Gadget !I

Existing Gadgets

"esign your o#n Gadget

"emo

Conc$usion

3 Gary Bisson - ELC 2012
Who am I?

Soft#are engineer at deneo Embedded


%Be$$e&ue, '(

Linux, ndroid

)ain acti&ities*

BS! ada+tation

"ri&er de&e$o+ment

System integration

4 Gary Bisson - ELC 2012
Context and objectives

Genera$ ,no#$edge of t-e !I

Focused on USB not genera$ dri&er de&e$o+ment

.ot-ing on t-e -ost side

Case study

Using a generic embedded de&ice, see -o# #e can


create a USB Gadget

S-o# +otentia$

/o# to fu$fi$$ e&ery need



5 Gary Bisson - ELC 2012
Universal Serial Bus

Industry standard de&e$o+ed in t-e mid01110s

"efines t-e cab$es, connectors and +rotoco$s used for


connection, communication and +o#er su++$y
bet#een com+uters and e$ectronic de&ices

2 bi$$ion USB de&ices #ere so$d eac- year %as of


2002(

6 Gary Bisson - ELC 2012
Universal Serial Bus

Benefits*

3e+$ace $ots of o$d buses

utomatic configuration

)u$ti+$e s+eeds

3e$iab$e

Limits*

"istance

!eer04o0!eer

Broadcasting

7 Gary Bisson - ELC 2012
Universal Serial Bus

rc-itecture*

)aster0S$a&e +rotoco$

U+ to 125 de&ices addressab$e

Can be -ot0+$ugged

Identification to t-e -ost

Su++orts -ig- s+eeds

)u$tifunction de&ice +ossibi$ity



8 Gary Bisson - ELC 2012
Universal Serial Bus

"escri+tion*

9 Gary Bisson - ELC 2012
Universal Serial Bus

End+oints

Source and Sin, of data

Uni6ue$y identifiab$e

Uni6ue direction %exce+t setu+(



10 Gary Bisson - ELC 2012
Universal Serial Bus

7 transfer ty+es*

Contro$

Configuration and contro$ information

Interru+t

Sma$$ 6uantities time0sensiti&e data

Bu$,

Large 6uantities time0insensiti&e data

Isoc-ronous

3ea$0time data at +redictab$e bit rates



11 Gary Bisson - ELC 2012
!"ical #evice #river

"e&ice Firm#are "ri&er

/ard#are s+ecific routines

USB interru+ts8e&ents

C-a+ter 1

Enumeration +rocess

4ransfer data to u++er $ayer

USB C$ass "ri&er

"efines t-e be-a&ior

!ro&ides configuration

12 Gary Bisson - ELC 2012
Gadget A$I

!ro&ides essentia$ infrastructure

Simi$ar to C-a+ter 1 in ty+ica$ USB de&ice soft#are

/and$es USB +rotoco$ s+ecific re6uirements

F$exib$e enoug- to ex+ose more com+$ex USB de&ice


ca+abi$ities

13 Gary Bisson - ELC 2012
Gadget A$I vs% Linux&USB A$I

Simi$arities

S-are common definitions for t-e standard USB messages,


structures and constants

Use 6ueues of re6uest ob9ects to +ac,age I8: buffers

Bot- !Is bind and unbind dri&ers to de&ices

"ifferences

Contro$ transfers

Configuration management
;< 4-an,s to simi$arities, Gadget !I su++orts :4G

14 Gary Bisson - ELC 2012
Gadget A$I
Lo#er boundary*

-and$ing setu+ re6uests %e+0 +rotoco$ res+onses(


+ossib$y inc$uding c$ass0s+ecific functiona$ity

returning configuration and string descri+tors

%re(setting configurations and interface a$tsettings,


inc$uding enab$ing and configuring end+oints

-and$ing $ife cyc$e e&ents, suc- as managing bindings


to -ard#are, USB sus+end8resume, remote #a,eu+,
and disconnection from t-e USB -ost

managing I. and :U4 transfers on a$$ current$y


enab$ed end+oints

15 Gary Bisson - ELC 2012
Gadget A$I
U++er $ayer*

user mode code, using generic %gadgetfs( or a++$ication


s+ecific fi$es in 8de&

net#or,ing subsystem %for net#or, gadgets, $i,e t-e


C"C Et-ernet )ode$ gadget dri&er(

data ca+ture dri&ers, +er-a+s &ideo7Linux or a scanner


dri&er= or test and measurement -ard#are

in+ut subsystem %for /I" gadgets(

sound subsystem %for audio gadgets(

fi$e system %for !4! gadgets(

b$oc, i8o subsystem %for usb0storage gadgets(



16 Gary Bisson - ELC 2012
Gadget A$I ' (ain structures
struct usb_gadget > re+resents a gadget de&ice

usb_gadget_ops > contains ca$$bac,s for -ard#are o+erations


struct usb_ep > de&ice -ard#are management

usb_ep_ops > contains ca$$bac,s for end+oints o+erations


struct usb_gadget_driver > de&ice functions
management %bind, unbind, sus+end etc???(
struct usb_request > USB transfers management

17 Gary Bisson - ELC 2012
Gadget A$I ' (ain functions
Genera$ o+erations %usb_gadget_x()(*
probe_driver / unregister_driver
set_selfpowered / clear_selfpowered
vbus_connect / vbus_disconnect
connect / disconnect
frame_number
End+oint o+erations %usb_ep_x()(*
autoconf / autoconf_reset
enable / disable
alloc / free
queue / dequeue
set_halt / clear_halt
fifo_status / fifo_flush
"ecri+tor o+erations*
usb_descriptor_fillbuf
usb_gadget_config_buf

18 Gary Bisson - ELC 2012
Gadget A$I
"ri&er $ife cyc$e*

3egister dri&er for a +articu$ar de&ice contro$$er

3egister gadget dri&er %bind(

/ard#are +o#ered, enumeration starts

Gadget dri&er returns descri+tors %setu+(

Gadget dri&er returns interfaces configuration

"o rea$ #or, %data transfer( unti$ disconnect

Gadget dri&er un$oaded %unbind(



19 Gary Bisson - ELC 2012
)xisting Gadgets

Et-ernet

Enumerate to t-e -ost as an Et-ernet de&ice

Can easi$y be bridging, routing, or fire#a$$ing


access to ot-er net#or,s

Intero+erabi$ity #it- -osts running Linux, )S0


'indo#s among ot-ers

!ossibi$ity to set +arameters suc- as )C address,


I! configuration or "/C! use t-an,s to t-e
bootargs if using a boot firm#are $i,e U0Boot

20 Gary Bisson - ELC 2012
)xisting Gadgets

GadgetFS

!ro&ides User0)ode !I

Eac- end+oint +resented as sing$e I8: fi$e


descri+tor

.orma$ read%( and #rite%( ca$$s

sync I8: su++orted

Configuration and descri+tors #ritten into fi$es


.ote t-at user mode gadget dri&ers do not necesari$y need to
be $icensed according to t-e G!L?

21 Gary Bisson - ELC 2012
)xisting Gadgets

Fi$e0bac,ed Storage

Im+$ements t-e USB )ass Storage C$ass

U+ to 2 dis, dri&es can be set

Store fi$e or b$oc, de&ice is ca$$ed t-e @bac,ing


storageA

Bac,ing storage re6uires +re+aration

If a fi$e is used, it must created #it- its desired siBe before


$aunc-ing t-e dri&er

If a b$oc, de&ice, it must matc- -ost rea6uirements %":S


+artition for )S0'indo#s -ost(

4-e bac,ing storage must not c-ange #-i$e FBS is


running, on$y t-e -ost s-ou$d access it

22 Gary Bisson - ELC 2012
)xisting Gadgets

'ebcam

cts as a com+osite USB udio and Cideo C$ass


de&ice

!ro&ides a users+ace !I to +rocess UCC contro$


re6uests and stream &ideo data

Seria$ Gadget

Usefu$ for 44D sty$e o+eration

Su++orts a C"C0C) modu$e o+tion



23 Gary Bisson - ELC 2012
)xisting Gadgets

)I"I

Ex+oses an LS )I"I interface

Bot- recording and +$aybac, su++ort

GadgetEero

Usefu$ to test de&ice contro$$er dri&er

/e$+s &erify t-at t-e dri&er stac, +ass USB0IF %for


USB branding(

:n t-e -ost side, usefu$ to test USB stac,



24 Gary Bisson - ELC 2012
#esign !our o*n Gadget

F main o+erations to consider

/ard#are

Functiona$

End+oints

25 Gary Bisson - ELC 2012
#esign !our o*n Gadget

First im+$ement t-e register8unregister functions

usb_gadget_probe_driver

3esgistration of t-e usb_gadget_driver

3es+onsib$e for binding t-e gadget dri&er and +o#ering u+


t-e de&ice

usb_gadget_unregister_driver

3es+onsib$e for unbinding t-e gadget from t-e functiona$


dri&er and +o#ering do#n t-e de&ice

4-en define ca$$bac,s -ard#are re$ated

Fi$$ usb_ep_ops and usb_gadget_ops

.ot necessary to su++ort a$$ functions



26 Gary Bisson - ELC 2012
#esign !our o*n Gadget

Im+$ement t-e contro$ re6uest -and$es %e+0(

Gadget dri&er -and$es on$y a +art of it

4-e rest is routed to t-e c$ass dri&er



27 Gary Bisson - ELC 2012
#esign !our o*n Gadget

!o#er )anagement re6uests

Comes from t-e !"C to t-e Gadget

4-e Gadget must +ass t-e e&ents to t-e c$ass dri&er

:nce enumeration is done, c$ass dri&er re6uests


usb_request structure for I.8:U4 transfers

Gadget recei&es data in interru+t routine %:U4(

:n$y #-en t-e ex+ected amount is recei&ed t-e Gadget


ca$$s t-e complete function

Gadget sends data to t-e !"C %I.(

'ait send com+$etion to inform t-e c$ass dri&er



28 Gary Bisson - ELC 2012
#esign !our o*n Gadget

29 Gary Bisson - ELC 2012
#emo+ ,ard*are
BeagleBoard x(

3)G CortexG02 1000 )/B

USB connecti&ity*

7 -ost +orts

1 :4G +ort %used as de&ice(



30 Gary Bisson - ELC 2012
#emo+ Soft*are

Boot$oader

U0boot 2011?12 r7

Herne$

F?0?15 r11Ic

3oot fi$esystem

Conso$e image

Custom reci+e %$ig-tt+d(

dditiona$ modu$es

31 Gary Bisson - ELC 2012
Conclusion

Easy to im+$ement

/ard#are inde+endent

Sca$abi$ity

Large +ane$ of existing gadgets

#areness of $imitations

32 Gary Bisson - ELC 2012
-uestions?

33 Gary Bisson - ELC 2012
A""endix+ .iles
4-e fi$es used for t-is ex+eriment s-ou$d be
attac-ed #it- t-e +resentation

3ootfs*

Custom reci+e +ro&ided if rebui$d is necessary

dditiona$ modu$es*

Instructions to recom+i$e t-em

"emo scri+t

Lig-tt+d configuration fi$e



34 Gary Bisson - ELC 2012
A""endix+ /eferences

Linux0USB Gadget !I Frame#or,* Genera$


+resentation?

USB Gadget !I for Linux* Fu$$ descri+tion of t-e !I?

Essential Linux Device Drivers (Sreekrishnan


Venkateswaran) * Genera$ de&ice dri&er boo,
containing a usefu$ USB section?

Bootstrap Yoursel with Linux!"SB Stack (#a$aram


#e%upath&)* Cery detai$ed and easy0to0read boo, about
Linux0USB?

You might also like