btusb.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237
  1. /*
  2. *
  3. * Generic Bluetooth USB driver
  4. *
  5. * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/usb.h>
  25. #include <linux/firmware.h>
  26. #include <asm/unaligned.h>
  27. #include <net/bluetooth/bluetooth.h>
  28. #include <net/bluetooth/hci_core.h>
  29. #include "btintel.h"
  30. #include "btbcm.h"
  31. #include "btrtl.h"
  32. #define VERSION "0.8"
  33. static bool disable_scofix;
  34. static bool force_scofix;
  35. static bool reset = true;
  36. static struct usb_driver btusb_driver;
  37. #define BTUSB_IGNORE 0x01
  38. #define BTUSB_DIGIANSWER 0x02
  39. #define BTUSB_CSR 0x04
  40. #define BTUSB_SNIFFER 0x08
  41. #define BTUSB_BCM92035 0x10
  42. #define BTUSB_BROKEN_ISOC 0x20
  43. #define BTUSB_WRONG_SCO_MTU 0x40
  44. #define BTUSB_ATH3012 0x80
  45. #define BTUSB_INTEL 0x100
  46. #define BTUSB_INTEL_BOOT 0x200
  47. #define BTUSB_BCM_PATCHRAM 0x400
  48. #define BTUSB_MARVELL 0x800
  49. #define BTUSB_SWAVE 0x1000
  50. #define BTUSB_INTEL_NEW 0x2000
  51. #define BTUSB_AMP 0x4000
  52. #define BTUSB_QCA_ROME 0x8000
  53. #define BTUSB_BCM_APPLE 0x10000
  54. #define BTUSB_REALTEK 0x20000
  55. #define BTUSB_BCM2045 0x40000
  56. #define BTUSB_IFNUM_2 0x80000
  57. static const struct usb_device_id btusb_table[] = {
  58. /* Generic Bluetooth USB device */
  59. { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
  60. /* Generic Bluetooth AMP device */
  61. { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
  62. /* Generic Bluetooth USB interface */
  63. { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
  64. /* Apple-specific (Broadcom) devices */
  65. { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
  66. .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
  67. /* MediaTek MT76x0E */
  68. { USB_DEVICE(0x0e8d, 0x763f) },
  69. /* Broadcom SoftSailing reporting vendor specific */
  70. { USB_DEVICE(0x0a5c, 0x21e1) },
  71. /* Apple MacBookPro 7,1 */
  72. { USB_DEVICE(0x05ac, 0x8213) },
  73. /* Apple iMac11,1 */
  74. { USB_DEVICE(0x05ac, 0x8215) },
  75. /* Apple MacBookPro6,2 */
  76. { USB_DEVICE(0x05ac, 0x8218) },
  77. /* Apple MacBookAir3,1, MacBookAir3,2 */
  78. { USB_DEVICE(0x05ac, 0x821b) },
  79. /* Apple MacBookAir4,1 */
  80. { USB_DEVICE(0x05ac, 0x821f) },
  81. /* Apple MacBookPro8,2 */
  82. { USB_DEVICE(0x05ac, 0x821a) },
  83. /* Apple MacMini5,1 */
  84. { USB_DEVICE(0x05ac, 0x8281) },
  85. /* AVM BlueFRITZ! USB v2.0 */
  86. { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
  87. /* Bluetooth Ultraport Module from IBM */
  88. { USB_DEVICE(0x04bf, 0x030a) },
  89. /* ALPS Modules with non-standard id */
  90. { USB_DEVICE(0x044e, 0x3001) },
  91. { USB_DEVICE(0x044e, 0x3002) },
  92. /* Ericsson with non-standard id */
  93. { USB_DEVICE(0x0bdb, 0x1002) },
  94. /* Canyon CN-BTU1 with HID interfaces */
  95. { USB_DEVICE(0x0c10, 0x0000) },
  96. /* Broadcom BCM20702A0 */
  97. { USB_DEVICE(0x413c, 0x8197) },
  98. /* Broadcom BCM20702B0 (Dynex/Insignia) */
  99. { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
  100. /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
  101. { USB_DEVICE(0x105b, 0xe065), .driver_info = BTUSB_BCM_PATCHRAM },
  102. /* Foxconn - Hon Hai */
  103. { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
  104. .driver_info = BTUSB_BCM_PATCHRAM },
  105. /* Lite-On Technology - Broadcom based */
  106. { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
  107. .driver_info = BTUSB_BCM_PATCHRAM },
  108. /* Broadcom devices with vendor specific id */
  109. { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
  110. .driver_info = BTUSB_BCM_PATCHRAM },
  111. /* ASUSTek Computer - Broadcom based */
  112. { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
  113. .driver_info = BTUSB_BCM_PATCHRAM },
  114. /* Belkin F8065bf - Broadcom based */
  115. { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
  116. .driver_info = BTUSB_BCM_PATCHRAM },
  117. /* IMC Networks - Broadcom based */
  118. { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
  119. .driver_info = BTUSB_BCM_PATCHRAM },
  120. /* Intel Bluetooth USB Bootloader (RAM module) */
  121. { USB_DEVICE(0x8087, 0x0a5a),
  122. .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
  123. { } /* Terminating entry */
  124. };
  125. MODULE_DEVICE_TABLE(usb, btusb_table);
  126. static const struct usb_device_id blacklist_table[] = {
  127. /* CSR BlueCore devices */
  128. { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
  129. /* Broadcom BCM2033 without firmware */
  130. { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
  131. /* Broadcom BCM2045 devices */
  132. { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
  133. /* Atheros 3011 with sflash firmware */
  134. { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
  135. { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
  136. { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
  137. { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
  138. { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
  139. { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
  140. { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
  141. /* Atheros AR9285 Malbec with sflash firmware */
  142. { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
  143. /* Atheros 3012 with sflash firmware */
  144. { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
  145. { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
  146. { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
  147. { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
  148. { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
  149. { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
  150. { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
  151. { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
  152. { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
  153. { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
  154. { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
  155. { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
  156. { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
  157. { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
  158. { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
  159. { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
  160. { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
  161. { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
  162. { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
  163. { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
  164. { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
  165. { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
  166. { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
  167. { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
  168. { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
  169. { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
  170. { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
  171. { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
  172. { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
  173. { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
  174. { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
  175. { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
  176. { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
  177. { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
  178. { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
  179. { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
  180. { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
  181. { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
  182. { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
  183. { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
  184. { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
  185. { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
  186. { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
  187. /* Atheros AR5BBU12 with sflash firmware */
  188. { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
  189. /* Atheros AR5BBU12 with sflash firmware */
  190. { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
  191. { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
  192. /* QCA ROME chipset */
  193. { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
  194. { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
  195. { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
  196. /* Broadcom BCM2035 */
  197. { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
  198. { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
  199. { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
  200. /* Broadcom BCM2045 */
  201. { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
  202. { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
  203. /* IBM/Lenovo ThinkPad with Broadcom chip */
  204. { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
  205. { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
  206. /* HP laptop with Broadcom chip */
  207. { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
  208. /* Dell laptop with Broadcom chip */
  209. { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
  210. /* Dell Wireless 370 and 410 devices */
  211. { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
  212. { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
  213. /* Belkin F8T012 and F8T013 devices */
  214. { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
  215. { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
  216. /* Asus WL-BTD202 device */
  217. { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
  218. /* Kensington Bluetooth USB adapter */
  219. { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
  220. /* RTX Telecom based adapters with buggy SCO support */
  221. { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
  222. { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
  223. /* CONWISE Technology based adapters with buggy SCO support */
  224. { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
  225. /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
  226. { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
  227. /* Digianswer devices */
  228. { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
  229. { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
  230. /* CSR BlueCore Bluetooth Sniffer */
  231. { USB_DEVICE(0x0a12, 0x0002),
  232. .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
  233. /* Frontline ComProbe Bluetooth Sniffer */
  234. { USB_DEVICE(0x16d3, 0x0002),
  235. .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
  236. /* Marvell Bluetooth devices */
  237. { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
  238. { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
  239. /* Intel Bluetooth devices */
  240. { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
  241. { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
  242. { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
  243. { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
  244. /* Other Intel Bluetooth devices */
  245. { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
  246. .driver_info = BTUSB_IGNORE },
  247. /* Realtek Bluetooth devices */
  248. { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
  249. .driver_info = BTUSB_REALTEK },
  250. /* Additional Realtek 8723AE Bluetooth devices */
  251. { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
  252. { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
  253. /* Additional Realtek 8723BE Bluetooth devices */
  254. { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
  255. { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
  256. { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
  257. { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
  258. { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
  259. /* Additional Realtek 8821AE Bluetooth devices */
  260. { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
  261. { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
  262. { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
  263. { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
  264. { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
  265. /* Silicon Wave based devices */
  266. { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
  267. { } /* Terminating entry */
  268. };
  269. #define BTUSB_MAX_ISOC_FRAMES 10
  270. #define BTUSB_INTR_RUNNING 0
  271. #define BTUSB_BULK_RUNNING 1
  272. #define BTUSB_ISOC_RUNNING 2
  273. #define BTUSB_SUSPENDING 3
  274. #define BTUSB_DID_ISO_RESUME 4
  275. #define BTUSB_BOOTLOADER 5
  276. #define BTUSB_DOWNLOADING 6
  277. #define BTUSB_FIRMWARE_LOADED 7
  278. #define BTUSB_FIRMWARE_FAILED 8
  279. #define BTUSB_BOOTING 9
  280. #define BTUSB_RESET_RESUME 10
  281. #define BTUSB_DIAG_RUNNING 11
  282. struct btusb_data {
  283. struct hci_dev *hdev;
  284. struct usb_device *udev;
  285. struct usb_interface *intf;
  286. struct usb_interface *isoc;
  287. struct usb_interface *diag;
  288. unsigned long flags;
  289. struct work_struct work;
  290. struct work_struct waker;
  291. struct usb_anchor deferred;
  292. struct usb_anchor tx_anchor;
  293. int tx_in_flight;
  294. spinlock_t txlock;
  295. struct usb_anchor intr_anchor;
  296. struct usb_anchor bulk_anchor;
  297. struct usb_anchor isoc_anchor;
  298. struct usb_anchor diag_anchor;
  299. spinlock_t rxlock;
  300. struct sk_buff *evt_skb;
  301. struct sk_buff *acl_skb;
  302. struct sk_buff *sco_skb;
  303. struct usb_endpoint_descriptor *intr_ep;
  304. struct usb_endpoint_descriptor *bulk_tx_ep;
  305. struct usb_endpoint_descriptor *bulk_rx_ep;
  306. struct usb_endpoint_descriptor *isoc_tx_ep;
  307. struct usb_endpoint_descriptor *isoc_rx_ep;
  308. struct usb_endpoint_descriptor *diag_tx_ep;
  309. struct usb_endpoint_descriptor *diag_rx_ep;
  310. __u8 cmdreq_type;
  311. __u8 cmdreq;
  312. unsigned int sco_num;
  313. int isoc_altsetting;
  314. int suspend_count;
  315. int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
  316. int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
  317. int (*setup_on_usb)(struct hci_dev *hdev);
  318. };
  319. static inline void btusb_free_frags(struct btusb_data *data)
  320. {
  321. unsigned long flags;
  322. spin_lock_irqsave(&data->rxlock, flags);
  323. kfree_skb(data->evt_skb);
  324. data->evt_skb = NULL;
  325. kfree_skb(data->acl_skb);
  326. data->acl_skb = NULL;
  327. kfree_skb(data->sco_skb);
  328. data->sco_skb = NULL;
  329. spin_unlock_irqrestore(&data->rxlock, flags);
  330. }
  331. static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
  332. {
  333. struct sk_buff *skb;
  334. int err = 0;
  335. spin_lock(&data->rxlock);
  336. skb = data->evt_skb;
  337. while (count) {
  338. int len;
  339. if (!skb) {
  340. skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
  341. if (!skb) {
  342. err = -ENOMEM;
  343. break;
  344. }
  345. bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
  346. bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
  347. }
  348. len = min_t(uint, bt_cb(skb)->expect, count);
  349. memcpy(skb_put(skb, len), buffer, len);
  350. count -= len;
  351. buffer += len;
  352. bt_cb(skb)->expect -= len;
  353. if (skb->len == HCI_EVENT_HDR_SIZE) {
  354. /* Complete event header */
  355. bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
  356. if (skb_tailroom(skb) < bt_cb(skb)->expect) {
  357. kfree_skb(skb);
  358. skb = NULL;
  359. err = -EILSEQ;
  360. break;
  361. }
  362. }
  363. if (bt_cb(skb)->expect == 0) {
  364. /* Complete frame */
  365. data->recv_event(data->hdev, skb);
  366. skb = NULL;
  367. }
  368. }
  369. data->evt_skb = skb;
  370. spin_unlock(&data->rxlock);
  371. return err;
  372. }
  373. static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
  374. {
  375. struct sk_buff *skb;
  376. int err = 0;
  377. spin_lock(&data->rxlock);
  378. skb = data->acl_skb;
  379. while (count) {
  380. int len;
  381. if (!skb) {
  382. skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
  383. if (!skb) {
  384. err = -ENOMEM;
  385. break;
  386. }
  387. bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
  388. bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
  389. }
  390. len = min_t(uint, bt_cb(skb)->expect, count);
  391. memcpy(skb_put(skb, len), buffer, len);
  392. count -= len;
  393. buffer += len;
  394. bt_cb(skb)->expect -= len;
  395. if (skb->len == HCI_ACL_HDR_SIZE) {
  396. __le16 dlen = hci_acl_hdr(skb)->dlen;
  397. /* Complete ACL header */
  398. bt_cb(skb)->expect = __le16_to_cpu(dlen);
  399. if (skb_tailroom(skb) < bt_cb(skb)->expect) {
  400. kfree_skb(skb);
  401. skb = NULL;
  402. err = -EILSEQ;
  403. break;
  404. }
  405. }
  406. if (bt_cb(skb)->expect == 0) {
  407. /* Complete frame */
  408. hci_recv_frame(data->hdev, skb);
  409. skb = NULL;
  410. }
  411. }
  412. data->acl_skb = skb;
  413. spin_unlock(&data->rxlock);
  414. return err;
  415. }
  416. static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
  417. {
  418. struct sk_buff *skb;
  419. int err = 0;
  420. spin_lock(&data->rxlock);
  421. skb = data->sco_skb;
  422. while (count) {
  423. int len;
  424. if (!skb) {
  425. skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
  426. if (!skb) {
  427. err = -ENOMEM;
  428. break;
  429. }
  430. bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
  431. bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
  432. }
  433. len = min_t(uint, bt_cb(skb)->expect, count);
  434. memcpy(skb_put(skb, len), buffer, len);
  435. count -= len;
  436. buffer += len;
  437. bt_cb(skb)->expect -= len;
  438. if (skb->len == HCI_SCO_HDR_SIZE) {
  439. /* Complete SCO header */
  440. bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
  441. if (skb_tailroom(skb) < bt_cb(skb)->expect) {
  442. kfree_skb(skb);
  443. skb = NULL;
  444. err = -EILSEQ;
  445. break;
  446. }
  447. }
  448. if (bt_cb(skb)->expect == 0) {
  449. /* Complete frame */
  450. hci_recv_frame(data->hdev, skb);
  451. skb = NULL;
  452. }
  453. }
  454. data->sco_skb = skb;
  455. spin_unlock(&data->rxlock);
  456. return err;
  457. }
  458. static void btusb_intr_complete(struct urb *urb)
  459. {
  460. struct hci_dev *hdev = urb->context;
  461. struct btusb_data *data = hci_get_drvdata(hdev);
  462. int err;
  463. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  464. urb->actual_length);
  465. if (!test_bit(HCI_RUNNING, &hdev->flags))
  466. return;
  467. if (urb->status == 0) {
  468. hdev->stat.byte_rx += urb->actual_length;
  469. if (btusb_recv_intr(data, urb->transfer_buffer,
  470. urb->actual_length) < 0) {
  471. BT_ERR("%s corrupted event packet", hdev->name);
  472. hdev->stat.err_rx++;
  473. }
  474. } else if (urb->status == -ENOENT) {
  475. /* Avoid suspend failed when usb_kill_urb */
  476. return;
  477. }
  478. if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
  479. return;
  480. usb_mark_last_busy(data->udev);
  481. usb_anchor_urb(urb, &data->intr_anchor);
  482. err = usb_submit_urb(urb, GFP_ATOMIC);
  483. if (err < 0) {
  484. /* -EPERM: urb is being killed;
  485. * -ENODEV: device got disconnected */
  486. if (err != -EPERM && err != -ENODEV)
  487. BT_ERR("%s urb %p failed to resubmit (%d)",
  488. hdev->name, urb, -err);
  489. usb_unanchor_urb(urb);
  490. }
  491. }
  492. static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
  493. {
  494. struct btusb_data *data = hci_get_drvdata(hdev);
  495. struct urb *urb;
  496. unsigned char *buf;
  497. unsigned int pipe;
  498. int err, size;
  499. BT_DBG("%s", hdev->name);
  500. if (!data->intr_ep)
  501. return -ENODEV;
  502. urb = usb_alloc_urb(0, mem_flags);
  503. if (!urb)
  504. return -ENOMEM;
  505. size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
  506. buf = kmalloc(size, mem_flags);
  507. if (!buf) {
  508. usb_free_urb(urb);
  509. return -ENOMEM;
  510. }
  511. pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
  512. usb_fill_int_urb(urb, data->udev, pipe, buf, size,
  513. btusb_intr_complete, hdev, data->intr_ep->bInterval);
  514. urb->transfer_flags |= URB_FREE_BUFFER;
  515. usb_anchor_urb(urb, &data->intr_anchor);
  516. err = usb_submit_urb(urb, mem_flags);
  517. if (err < 0) {
  518. if (err != -EPERM && err != -ENODEV)
  519. BT_ERR("%s urb %p submission failed (%d)",
  520. hdev->name, urb, -err);
  521. usb_unanchor_urb(urb);
  522. }
  523. usb_free_urb(urb);
  524. return err;
  525. }
  526. static void btusb_bulk_complete(struct urb *urb)
  527. {
  528. struct hci_dev *hdev = urb->context;
  529. struct btusb_data *data = hci_get_drvdata(hdev);
  530. int err;
  531. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  532. urb->actual_length);
  533. if (!test_bit(HCI_RUNNING, &hdev->flags))
  534. return;
  535. if (urb->status == 0) {
  536. hdev->stat.byte_rx += urb->actual_length;
  537. if (data->recv_bulk(data, urb->transfer_buffer,
  538. urb->actual_length) < 0) {
  539. BT_ERR("%s corrupted ACL packet", hdev->name);
  540. hdev->stat.err_rx++;
  541. }
  542. } else if (urb->status == -ENOENT) {
  543. /* Avoid suspend failed when usb_kill_urb */
  544. return;
  545. }
  546. if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
  547. return;
  548. usb_anchor_urb(urb, &data->bulk_anchor);
  549. usb_mark_last_busy(data->udev);
  550. err = usb_submit_urb(urb, GFP_ATOMIC);
  551. if (err < 0) {
  552. /* -EPERM: urb is being killed;
  553. * -ENODEV: device got disconnected */
  554. if (err != -EPERM && err != -ENODEV)
  555. BT_ERR("%s urb %p failed to resubmit (%d)",
  556. hdev->name, urb, -err);
  557. usb_unanchor_urb(urb);
  558. }
  559. }
  560. static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
  561. {
  562. struct btusb_data *data = hci_get_drvdata(hdev);
  563. struct urb *urb;
  564. unsigned char *buf;
  565. unsigned int pipe;
  566. int err, size = HCI_MAX_FRAME_SIZE;
  567. BT_DBG("%s", hdev->name);
  568. if (!data->bulk_rx_ep)
  569. return -ENODEV;
  570. urb = usb_alloc_urb(0, mem_flags);
  571. if (!urb)
  572. return -ENOMEM;
  573. buf = kmalloc(size, mem_flags);
  574. if (!buf) {
  575. usb_free_urb(urb);
  576. return -ENOMEM;
  577. }
  578. pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
  579. usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
  580. btusb_bulk_complete, hdev);
  581. urb->transfer_flags |= URB_FREE_BUFFER;
  582. usb_mark_last_busy(data->udev);
  583. usb_anchor_urb(urb, &data->bulk_anchor);
  584. err = usb_submit_urb(urb, mem_flags);
  585. if (err < 0) {
  586. if (err != -EPERM && err != -ENODEV)
  587. BT_ERR("%s urb %p submission failed (%d)",
  588. hdev->name, urb, -err);
  589. usb_unanchor_urb(urb);
  590. }
  591. usb_free_urb(urb);
  592. return err;
  593. }
  594. static void btusb_isoc_complete(struct urb *urb)
  595. {
  596. struct hci_dev *hdev = urb->context;
  597. struct btusb_data *data = hci_get_drvdata(hdev);
  598. int i, err;
  599. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  600. urb->actual_length);
  601. if (!test_bit(HCI_RUNNING, &hdev->flags))
  602. return;
  603. if (urb->status == 0) {
  604. for (i = 0; i < urb->number_of_packets; i++) {
  605. unsigned int offset = urb->iso_frame_desc[i].offset;
  606. unsigned int length = urb->iso_frame_desc[i].actual_length;
  607. if (urb->iso_frame_desc[i].status)
  608. continue;
  609. hdev->stat.byte_rx += length;
  610. if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
  611. length) < 0) {
  612. BT_ERR("%s corrupted SCO packet", hdev->name);
  613. hdev->stat.err_rx++;
  614. }
  615. }
  616. } else if (urb->status == -ENOENT) {
  617. /* Avoid suspend failed when usb_kill_urb */
  618. return;
  619. }
  620. if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
  621. return;
  622. usb_anchor_urb(urb, &data->isoc_anchor);
  623. err = usb_submit_urb(urb, GFP_ATOMIC);
  624. if (err < 0) {
  625. /* -EPERM: urb is being killed;
  626. * -ENODEV: device got disconnected */
  627. if (err != -EPERM && err != -ENODEV)
  628. BT_ERR("%s urb %p failed to resubmit (%d)",
  629. hdev->name, urb, -err);
  630. usb_unanchor_urb(urb);
  631. }
  632. }
  633. static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
  634. {
  635. int i, offset = 0;
  636. BT_DBG("len %d mtu %d", len, mtu);
  637. for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
  638. i++, offset += mtu, len -= mtu) {
  639. urb->iso_frame_desc[i].offset = offset;
  640. urb->iso_frame_desc[i].length = mtu;
  641. }
  642. if (len && i < BTUSB_MAX_ISOC_FRAMES) {
  643. urb->iso_frame_desc[i].offset = offset;
  644. urb->iso_frame_desc[i].length = len;
  645. i++;
  646. }
  647. urb->number_of_packets = i;
  648. }
  649. static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
  650. {
  651. struct btusb_data *data = hci_get_drvdata(hdev);
  652. struct urb *urb;
  653. unsigned char *buf;
  654. unsigned int pipe;
  655. int err, size;
  656. BT_DBG("%s", hdev->name);
  657. if (!data->isoc_rx_ep)
  658. return -ENODEV;
  659. urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
  660. if (!urb)
  661. return -ENOMEM;
  662. size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
  663. BTUSB_MAX_ISOC_FRAMES;
  664. buf = kmalloc(size, mem_flags);
  665. if (!buf) {
  666. usb_free_urb(urb);
  667. return -ENOMEM;
  668. }
  669. pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
  670. usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
  671. hdev, data->isoc_rx_ep->bInterval);
  672. urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
  673. __fill_isoc_descriptor(urb, size,
  674. le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
  675. usb_anchor_urb(urb, &data->isoc_anchor);
  676. err = usb_submit_urb(urb, mem_flags);
  677. if (err < 0) {
  678. if (err != -EPERM && err != -ENODEV)
  679. BT_ERR("%s urb %p submission failed (%d)",
  680. hdev->name, urb, -err);
  681. usb_unanchor_urb(urb);
  682. }
  683. usb_free_urb(urb);
  684. return err;
  685. }
  686. static void btusb_diag_complete(struct urb *urb)
  687. {
  688. struct hci_dev *hdev = urb->context;
  689. struct btusb_data *data = hci_get_drvdata(hdev);
  690. int err;
  691. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  692. urb->actual_length);
  693. if (urb->status == 0) {
  694. struct sk_buff *skb;
  695. skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
  696. if (skb) {
  697. memcpy(skb_put(skb, urb->actual_length),
  698. urb->transfer_buffer, urb->actual_length);
  699. hci_recv_diag(hdev, skb);
  700. }
  701. } else if (urb->status == -ENOENT) {
  702. /* Avoid suspend failed when usb_kill_urb */
  703. return;
  704. }
  705. if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
  706. return;
  707. usb_anchor_urb(urb, &data->diag_anchor);
  708. usb_mark_last_busy(data->udev);
  709. err = usb_submit_urb(urb, GFP_ATOMIC);
  710. if (err < 0) {
  711. /* -EPERM: urb is being killed;
  712. * -ENODEV: device got disconnected */
  713. if (err != -EPERM && err != -ENODEV)
  714. BT_ERR("%s urb %p failed to resubmit (%d)",
  715. hdev->name, urb, -err);
  716. usb_unanchor_urb(urb);
  717. }
  718. }
  719. static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
  720. {
  721. struct btusb_data *data = hci_get_drvdata(hdev);
  722. struct urb *urb;
  723. unsigned char *buf;
  724. unsigned int pipe;
  725. int err, size = HCI_MAX_FRAME_SIZE;
  726. BT_DBG("%s", hdev->name);
  727. if (!data->diag_rx_ep)
  728. return -ENODEV;
  729. urb = usb_alloc_urb(0, mem_flags);
  730. if (!urb)
  731. return -ENOMEM;
  732. buf = kmalloc(size, mem_flags);
  733. if (!buf) {
  734. usb_free_urb(urb);
  735. return -ENOMEM;
  736. }
  737. pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
  738. usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
  739. btusb_diag_complete, hdev);
  740. urb->transfer_flags |= URB_FREE_BUFFER;
  741. usb_mark_last_busy(data->udev);
  742. usb_anchor_urb(urb, &data->diag_anchor);
  743. err = usb_submit_urb(urb, mem_flags);
  744. if (err < 0) {
  745. if (err != -EPERM && err != -ENODEV)
  746. BT_ERR("%s urb %p submission failed (%d)",
  747. hdev->name, urb, -err);
  748. usb_unanchor_urb(urb);
  749. }
  750. usb_free_urb(urb);
  751. return err;
  752. }
  753. static void btusb_tx_complete(struct urb *urb)
  754. {
  755. struct sk_buff *skb = urb->context;
  756. struct hci_dev *hdev = (struct hci_dev *)skb->dev;
  757. struct btusb_data *data = hci_get_drvdata(hdev);
  758. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  759. urb->actual_length);
  760. if (!test_bit(HCI_RUNNING, &hdev->flags))
  761. goto done;
  762. if (!urb->status)
  763. hdev->stat.byte_tx += urb->transfer_buffer_length;
  764. else
  765. hdev->stat.err_tx++;
  766. done:
  767. spin_lock(&data->txlock);
  768. data->tx_in_flight--;
  769. spin_unlock(&data->txlock);
  770. kfree(urb->setup_packet);
  771. kfree_skb(skb);
  772. }
  773. static void btusb_isoc_tx_complete(struct urb *urb)
  774. {
  775. struct sk_buff *skb = urb->context;
  776. struct hci_dev *hdev = (struct hci_dev *)skb->dev;
  777. BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
  778. urb->actual_length);
  779. if (!test_bit(HCI_RUNNING, &hdev->flags))
  780. goto done;
  781. if (!urb->status)
  782. hdev->stat.byte_tx += urb->transfer_buffer_length;
  783. else
  784. hdev->stat.err_tx++;
  785. done:
  786. kfree(urb->setup_packet);
  787. kfree_skb(skb);
  788. }
  789. static int btusb_open(struct hci_dev *hdev)
  790. {
  791. struct btusb_data *data = hci_get_drvdata(hdev);
  792. int err;
  793. BT_DBG("%s", hdev->name);
  794. /* Patching USB firmware files prior to starting any URBs of HCI path
  795. * It is more safe to use USB bulk channel for downloading USB patch
  796. */
  797. if (data->setup_on_usb) {
  798. err = data->setup_on_usb(hdev);
  799. if (err < 0)
  800. return err;
  801. }
  802. err = usb_autopm_get_interface(data->intf);
  803. if (err < 0)
  804. return err;
  805. data->intf->needs_remote_wakeup = 1;
  806. if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
  807. goto done;
  808. err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
  809. if (err < 0)
  810. goto failed;
  811. err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
  812. if (err < 0) {
  813. usb_kill_anchored_urbs(&data->intr_anchor);
  814. goto failed;
  815. }
  816. set_bit(BTUSB_BULK_RUNNING, &data->flags);
  817. btusb_submit_bulk_urb(hdev, GFP_KERNEL);
  818. if (data->diag) {
  819. if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
  820. set_bit(BTUSB_DIAG_RUNNING, &data->flags);
  821. }
  822. done:
  823. usb_autopm_put_interface(data->intf);
  824. return 0;
  825. failed:
  826. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  827. usb_autopm_put_interface(data->intf);
  828. return err;
  829. }
  830. static void btusb_stop_traffic(struct btusb_data *data)
  831. {
  832. usb_kill_anchored_urbs(&data->intr_anchor);
  833. usb_kill_anchored_urbs(&data->bulk_anchor);
  834. usb_kill_anchored_urbs(&data->isoc_anchor);
  835. usb_kill_anchored_urbs(&data->diag_anchor);
  836. }
  837. static int btusb_close(struct hci_dev *hdev)
  838. {
  839. struct btusb_data *data = hci_get_drvdata(hdev);
  840. int err;
  841. BT_DBG("%s", hdev->name);
  842. cancel_work_sync(&data->work);
  843. cancel_work_sync(&data->waker);
  844. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  845. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  846. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  847. clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
  848. btusb_stop_traffic(data);
  849. btusb_free_frags(data);
  850. err = usb_autopm_get_interface(data->intf);
  851. if (err < 0)
  852. goto failed;
  853. data->intf->needs_remote_wakeup = 0;
  854. usb_autopm_put_interface(data->intf);
  855. failed:
  856. usb_scuttle_anchored_urbs(&data->deferred);
  857. return 0;
  858. }
  859. static int btusb_flush(struct hci_dev *hdev)
  860. {
  861. struct btusb_data *data = hci_get_drvdata(hdev);
  862. BT_DBG("%s", hdev->name);
  863. usb_kill_anchored_urbs(&data->tx_anchor);
  864. btusb_free_frags(data);
  865. return 0;
  866. }
  867. static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
  868. {
  869. struct btusb_data *data = hci_get_drvdata(hdev);
  870. struct usb_ctrlrequest *dr;
  871. struct urb *urb;
  872. unsigned int pipe;
  873. urb = usb_alloc_urb(0, GFP_KERNEL);
  874. if (!urb)
  875. return ERR_PTR(-ENOMEM);
  876. dr = kmalloc(sizeof(*dr), GFP_KERNEL);
  877. if (!dr) {
  878. usb_free_urb(urb);
  879. return ERR_PTR(-ENOMEM);
  880. }
  881. dr->bRequestType = data->cmdreq_type;
  882. dr->bRequest = data->cmdreq;
  883. dr->wIndex = 0;
  884. dr->wValue = 0;
  885. dr->wLength = __cpu_to_le16(skb->len);
  886. pipe = usb_sndctrlpipe(data->udev, 0x00);
  887. usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
  888. skb->data, skb->len, btusb_tx_complete, skb);
  889. skb->dev = (void *)hdev;
  890. return urb;
  891. }
  892. static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
  893. {
  894. struct btusb_data *data = hci_get_drvdata(hdev);
  895. struct urb *urb;
  896. unsigned int pipe;
  897. if (!data->bulk_tx_ep)
  898. return ERR_PTR(-ENODEV);
  899. urb = usb_alloc_urb(0, GFP_KERNEL);
  900. if (!urb)
  901. return ERR_PTR(-ENOMEM);
  902. pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
  903. usb_fill_bulk_urb(urb, data->udev, pipe,
  904. skb->data, skb->len, btusb_tx_complete, skb);
  905. skb->dev = (void *)hdev;
  906. return urb;
  907. }
  908. static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
  909. {
  910. struct btusb_data *data = hci_get_drvdata(hdev);
  911. struct urb *urb;
  912. unsigned int pipe;
  913. if (!data->isoc_tx_ep)
  914. return ERR_PTR(-ENODEV);
  915. urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
  916. if (!urb)
  917. return ERR_PTR(-ENOMEM);
  918. pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
  919. usb_fill_int_urb(urb, data->udev, pipe,
  920. skb->data, skb->len, btusb_isoc_tx_complete,
  921. skb, data->isoc_tx_ep->bInterval);
  922. urb->transfer_flags = URB_ISO_ASAP;
  923. __fill_isoc_descriptor(urb, skb->len,
  924. le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
  925. skb->dev = (void *)hdev;
  926. return urb;
  927. }
  928. static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
  929. {
  930. struct btusb_data *data = hci_get_drvdata(hdev);
  931. int err;
  932. usb_anchor_urb(urb, &data->tx_anchor);
  933. err = usb_submit_urb(urb, GFP_KERNEL);
  934. if (err < 0) {
  935. if (err != -EPERM && err != -ENODEV)
  936. BT_ERR("%s urb %p submission failed (%d)",
  937. hdev->name, urb, -err);
  938. kfree(urb->setup_packet);
  939. usb_unanchor_urb(urb);
  940. } else {
  941. usb_mark_last_busy(data->udev);
  942. }
  943. usb_free_urb(urb);
  944. return err;
  945. }
  946. static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
  947. {
  948. struct btusb_data *data = hci_get_drvdata(hdev);
  949. unsigned long flags;
  950. bool suspending;
  951. spin_lock_irqsave(&data->txlock, flags);
  952. suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
  953. if (!suspending)
  954. data->tx_in_flight++;
  955. spin_unlock_irqrestore(&data->txlock, flags);
  956. if (!suspending)
  957. return submit_tx_urb(hdev, urb);
  958. usb_anchor_urb(urb, &data->deferred);
  959. schedule_work(&data->waker);
  960. usb_free_urb(urb);
  961. return 0;
  962. }
  963. static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  964. {
  965. struct urb *urb;
  966. BT_DBG("%s", hdev->name);
  967. switch (bt_cb(skb)->pkt_type) {
  968. case HCI_COMMAND_PKT:
  969. urb = alloc_ctrl_urb(hdev, skb);
  970. if (IS_ERR(urb))
  971. return PTR_ERR(urb);
  972. hdev->stat.cmd_tx++;
  973. return submit_or_queue_tx_urb(hdev, urb);
  974. case HCI_ACLDATA_PKT:
  975. urb = alloc_bulk_urb(hdev, skb);
  976. if (IS_ERR(urb))
  977. return PTR_ERR(urb);
  978. hdev->stat.acl_tx++;
  979. return submit_or_queue_tx_urb(hdev, urb);
  980. case HCI_SCODATA_PKT:
  981. if (hci_conn_num(hdev, SCO_LINK) < 1)
  982. return -ENODEV;
  983. urb = alloc_isoc_urb(hdev, skb);
  984. if (IS_ERR(urb))
  985. return PTR_ERR(urb);
  986. hdev->stat.sco_tx++;
  987. return submit_tx_urb(hdev, urb);
  988. }
  989. return -EILSEQ;
  990. }
  991. static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
  992. {
  993. struct btusb_data *data = hci_get_drvdata(hdev);
  994. BT_DBG("%s evt %d", hdev->name, evt);
  995. if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
  996. data->sco_num = hci_conn_num(hdev, SCO_LINK);
  997. schedule_work(&data->work);
  998. }
  999. }
  1000. static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
  1001. {
  1002. struct btusb_data *data = hci_get_drvdata(hdev);
  1003. struct usb_interface *intf = data->isoc;
  1004. struct usb_endpoint_descriptor *ep_desc;
  1005. int i, err;
  1006. if (!data->isoc)
  1007. return -ENODEV;
  1008. err = usb_set_interface(data->udev, 1, altsetting);
  1009. if (err < 0) {
  1010. BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
  1011. return err;
  1012. }
  1013. data->isoc_altsetting = altsetting;
  1014. data->isoc_tx_ep = NULL;
  1015. data->isoc_rx_ep = NULL;
  1016. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  1017. ep_desc = &intf->cur_altsetting->endpoint[i].desc;
  1018. if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
  1019. data->isoc_tx_ep = ep_desc;
  1020. continue;
  1021. }
  1022. if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
  1023. data->isoc_rx_ep = ep_desc;
  1024. continue;
  1025. }
  1026. }
  1027. if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
  1028. BT_ERR("%s invalid SCO descriptors", hdev->name);
  1029. return -ENODEV;
  1030. }
  1031. return 0;
  1032. }
  1033. static void btusb_work(struct work_struct *work)
  1034. {
  1035. struct btusb_data *data = container_of(work, struct btusb_data, work);
  1036. struct hci_dev *hdev = data->hdev;
  1037. int new_alts;
  1038. int err;
  1039. if (data->sco_num > 0) {
  1040. if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
  1041. err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
  1042. if (err < 0) {
  1043. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  1044. usb_kill_anchored_urbs(&data->isoc_anchor);
  1045. return;
  1046. }
  1047. set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
  1048. }
  1049. if (hdev->voice_setting & 0x0020) {
  1050. static const int alts[3] = { 2, 4, 5 };
  1051. new_alts = alts[data->sco_num - 1];
  1052. } else {
  1053. new_alts = data->sco_num;
  1054. }
  1055. if (data->isoc_altsetting != new_alts) {
  1056. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  1057. usb_kill_anchored_urbs(&data->isoc_anchor);
  1058. /* When isochronous alternate setting needs to be
  1059. * changed, because SCO connection has been added
  1060. * or removed, a packet fragment may be left in the
  1061. * reassembling state. This could lead to wrongly
  1062. * assembled fragments.
  1063. *
  1064. * Clear outstanding fragment when selecting a new
  1065. * alternate setting.
  1066. */
  1067. spin_lock(&data->rxlock);
  1068. kfree_skb(data->sco_skb);
  1069. data->sco_skb = NULL;
  1070. spin_unlock(&data->rxlock);
  1071. if (__set_isoc_interface(hdev, new_alts) < 0)
  1072. return;
  1073. }
  1074. if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
  1075. if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
  1076. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  1077. else
  1078. btusb_submit_isoc_urb(hdev, GFP_KERNEL);
  1079. }
  1080. } else {
  1081. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  1082. usb_kill_anchored_urbs(&data->isoc_anchor);
  1083. __set_isoc_interface(hdev, 0);
  1084. if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
  1085. usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
  1086. }
  1087. }
  1088. static void btusb_waker(struct work_struct *work)
  1089. {
  1090. struct btusb_data *data = container_of(work, struct btusb_data, waker);
  1091. int err;
  1092. err = usb_autopm_get_interface(data->intf);
  1093. if (err < 0)
  1094. return;
  1095. usb_autopm_put_interface(data->intf);
  1096. }
  1097. static int btusb_setup_bcm92035(struct hci_dev *hdev)
  1098. {
  1099. struct sk_buff *skb;
  1100. u8 val = 0x00;
  1101. BT_DBG("%s", hdev->name);
  1102. skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
  1103. if (IS_ERR(skb))
  1104. BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
  1105. else
  1106. kfree_skb(skb);
  1107. return 0;
  1108. }
  1109. static int btusb_setup_csr(struct hci_dev *hdev)
  1110. {
  1111. struct hci_rp_read_local_version *rp;
  1112. struct sk_buff *skb;
  1113. BT_DBG("%s", hdev->name);
  1114. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  1115. HCI_INIT_TIMEOUT);
  1116. if (IS_ERR(skb)) {
  1117. int err = PTR_ERR(skb);
  1118. BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
  1119. return err;
  1120. }
  1121. if (skb->len != sizeof(struct hci_rp_read_local_version)) {
  1122. BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
  1123. kfree_skb(skb);
  1124. return -EIO;
  1125. }
  1126. rp = (struct hci_rp_read_local_version *)skb->data;
  1127. /* Detect controllers which aren't real CSR ones. */
  1128. if (le16_to_cpu(rp->manufacturer) != 10 ||
  1129. le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
  1130. /* Clear the reset quirk since this is not an actual
  1131. * early Bluetooth 1.1 device from CSR.
  1132. */
  1133. clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  1134. /* These fake CSR controllers have all a broken
  1135. * stored link key handling and so just disable it.
  1136. */
  1137. set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
  1138. }
  1139. kfree_skb(skb);
  1140. return 0;
  1141. }
  1142. static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
  1143. struct intel_version *ver)
  1144. {
  1145. const struct firmware *fw;
  1146. char fwname[64];
  1147. int ret;
  1148. snprintf(fwname, sizeof(fwname),
  1149. "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
  1150. ver->hw_platform, ver->hw_variant, ver->hw_revision,
  1151. ver->fw_variant, ver->fw_revision, ver->fw_build_num,
  1152. ver->fw_build_ww, ver->fw_build_yy);
  1153. ret = request_firmware(&fw, fwname, &hdev->dev);
  1154. if (ret < 0) {
  1155. if (ret == -EINVAL) {
  1156. BT_ERR("%s Intel firmware file request failed (%d)",
  1157. hdev->name, ret);
  1158. return NULL;
  1159. }
  1160. BT_ERR("%s failed to open Intel firmware file: %s(%d)",
  1161. hdev->name, fwname, ret);
  1162. /* If the correct firmware patch file is not found, use the
  1163. * default firmware patch file instead
  1164. */
  1165. snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
  1166. ver->hw_platform, ver->hw_variant);
  1167. if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
  1168. BT_ERR("%s failed to open default Intel fw file: %s",
  1169. hdev->name, fwname);
  1170. return NULL;
  1171. }
  1172. }
  1173. BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
  1174. return fw;
  1175. }
  1176. static int btusb_setup_intel_patching(struct hci_dev *hdev,
  1177. const struct firmware *fw,
  1178. const u8 **fw_ptr, int *disable_patch)
  1179. {
  1180. struct sk_buff *skb;
  1181. struct hci_command_hdr *cmd;
  1182. const u8 *cmd_param;
  1183. struct hci_event_hdr *evt = NULL;
  1184. const u8 *evt_param = NULL;
  1185. int remain = fw->size - (*fw_ptr - fw->data);
  1186. /* The first byte indicates the types of the patch command or event.
  1187. * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
  1188. * in the current firmware buffer doesn't start with 0x01 or
  1189. * the size of remain buffer is smaller than HCI command header,
  1190. * the firmware file is corrupted and it should stop the patching
  1191. * process.
  1192. */
  1193. if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
  1194. BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
  1195. return -EINVAL;
  1196. }
  1197. (*fw_ptr)++;
  1198. remain--;
  1199. cmd = (struct hci_command_hdr *)(*fw_ptr);
  1200. *fw_ptr += sizeof(*cmd);
  1201. remain -= sizeof(*cmd);
  1202. /* Ensure that the remain firmware data is long enough than the length
  1203. * of command parameter. If not, the firmware file is corrupted.
  1204. */
  1205. if (remain < cmd->plen) {
  1206. BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
  1207. return -EFAULT;
  1208. }
  1209. /* If there is a command that loads a patch in the firmware
  1210. * file, then enable the patch upon success, otherwise just
  1211. * disable the manufacturer mode, for example patch activation
  1212. * is not required when the default firmware patch file is used
  1213. * because there are no patch data to load.
  1214. */
  1215. if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
  1216. *disable_patch = 0;
  1217. cmd_param = *fw_ptr;
  1218. *fw_ptr += cmd->plen;
  1219. remain -= cmd->plen;
  1220. /* This reads the expected events when the above command is sent to the
  1221. * device. Some vendor commands expects more than one events, for
  1222. * example command status event followed by vendor specific event.
  1223. * For this case, it only keeps the last expected event. so the command
  1224. * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
  1225. * last expected event.
  1226. */
  1227. while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
  1228. (*fw_ptr)++;
  1229. remain--;
  1230. evt = (struct hci_event_hdr *)(*fw_ptr);
  1231. *fw_ptr += sizeof(*evt);
  1232. remain -= sizeof(*evt);
  1233. if (remain < evt->plen) {
  1234. BT_ERR("%s Intel fw corrupted: invalid evt len",
  1235. hdev->name);
  1236. return -EFAULT;
  1237. }
  1238. evt_param = *fw_ptr;
  1239. *fw_ptr += evt->plen;
  1240. remain -= evt->plen;
  1241. }
  1242. /* Every HCI commands in the firmware file has its correspond event.
  1243. * If event is not found or remain is smaller than zero, the firmware
  1244. * file is corrupted.
  1245. */
  1246. if (!evt || !evt_param || remain < 0) {
  1247. BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
  1248. return -EFAULT;
  1249. }
  1250. skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
  1251. cmd_param, evt->evt, HCI_INIT_TIMEOUT);
  1252. if (IS_ERR(skb)) {
  1253. BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
  1254. hdev->name, cmd->opcode, PTR_ERR(skb));
  1255. return PTR_ERR(skb);
  1256. }
  1257. /* It ensures that the returned event matches the event data read from
  1258. * the firmware file. At fist, it checks the length and then
  1259. * the contents of the event.
  1260. */
  1261. if (skb->len != evt->plen) {
  1262. BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
  1263. le16_to_cpu(cmd->opcode));
  1264. kfree_skb(skb);
  1265. return -EFAULT;
  1266. }
  1267. if (memcmp(skb->data, evt_param, evt->plen)) {
  1268. BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
  1269. hdev->name, le16_to_cpu(cmd->opcode));
  1270. kfree_skb(skb);
  1271. return -EFAULT;
  1272. }
  1273. kfree_skb(skb);
  1274. return 0;
  1275. }
  1276. static int btusb_setup_intel(struct hci_dev *hdev)
  1277. {
  1278. struct sk_buff *skb;
  1279. const struct firmware *fw;
  1280. const u8 *fw_ptr;
  1281. int disable_patch;
  1282. struct intel_version *ver;
  1283. const u8 mfg_enable[] = { 0x01, 0x00 };
  1284. const u8 mfg_disable[] = { 0x00, 0x00 };
  1285. const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
  1286. const u8 mfg_reset_activate[] = { 0x00, 0x02 };
  1287. BT_DBG("%s", hdev->name);
  1288. /* The controller has a bug with the first HCI command sent to it
  1289. * returning number of completed commands as zero. This would stall the
  1290. * command processing in the Bluetooth core.
  1291. *
  1292. * As a workaround, send HCI Reset command first which will reset the
  1293. * number of completed commands and allow normal command processing
  1294. * from now on.
  1295. */
  1296. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  1297. if (IS_ERR(skb)) {
  1298. BT_ERR("%s sending initial HCI reset command failed (%ld)",
  1299. hdev->name, PTR_ERR(skb));
  1300. return PTR_ERR(skb);
  1301. }
  1302. kfree_skb(skb);
  1303. /* Read Intel specific controller version first to allow selection of
  1304. * which firmware file to load.
  1305. *
  1306. * The returned information are hardware variant and revision plus
  1307. * firmware variant, revision and build number.
  1308. */
  1309. skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
  1310. if (IS_ERR(skb)) {
  1311. BT_ERR("%s reading Intel fw version command failed (%ld)",
  1312. hdev->name, PTR_ERR(skb));
  1313. return PTR_ERR(skb);
  1314. }
  1315. if (skb->len != sizeof(*ver)) {
  1316. BT_ERR("%s Intel version event length mismatch", hdev->name);
  1317. kfree_skb(skb);
  1318. return -EIO;
  1319. }
  1320. ver = (struct intel_version *)skb->data;
  1321. BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
  1322. hdev->name, ver->hw_platform, ver->hw_variant,
  1323. ver->hw_revision, ver->fw_variant, ver->fw_revision,
  1324. ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
  1325. ver->fw_patch_num);
  1326. /* fw_patch_num indicates the version of patch the device currently
  1327. * have. If there is no patch data in the device, it is always 0x00.
  1328. * So, if it is other than 0x00, no need to patch the device again.
  1329. */
  1330. if (ver->fw_patch_num) {
  1331. BT_INFO("%s: Intel device is already patched. patch num: %02x",
  1332. hdev->name, ver->fw_patch_num);
  1333. kfree_skb(skb);
  1334. goto complete;
  1335. }
  1336. /* Opens the firmware patch file based on the firmware version read
  1337. * from the controller. If it fails to open the matching firmware
  1338. * patch file, it tries to open the default firmware patch file.
  1339. * If no patch file is found, allow the device to operate without
  1340. * a patch.
  1341. */
  1342. fw = btusb_setup_intel_get_fw(hdev, ver);
  1343. if (!fw) {
  1344. kfree_skb(skb);
  1345. goto complete;
  1346. }
  1347. fw_ptr = fw->data;
  1348. kfree_skb(skb);
  1349. /* This Intel specific command enables the manufacturer mode of the
  1350. * controller.
  1351. *
  1352. * Only while this mode is enabled, the driver can download the
  1353. * firmware patch data and configuration parameters.
  1354. */
  1355. skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
  1356. if (IS_ERR(skb)) {
  1357. BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
  1358. hdev->name, PTR_ERR(skb));
  1359. release_firmware(fw);
  1360. return PTR_ERR(skb);
  1361. }
  1362. kfree_skb(skb);
  1363. disable_patch = 1;
  1364. /* The firmware data file consists of list of Intel specific HCI
  1365. * commands and its expected events. The first byte indicates the
  1366. * type of the message, either HCI command or HCI event.
  1367. *
  1368. * It reads the command and its expected event from the firmware file,
  1369. * and send to the controller. Once __hci_cmd_sync_ev() returns,
  1370. * the returned event is compared with the event read from the firmware
  1371. * file and it will continue until all the messages are downloaded to
  1372. * the controller.
  1373. *
  1374. * Once the firmware patching is completed successfully,
  1375. * the manufacturer mode is disabled with reset and activating the
  1376. * downloaded patch.
  1377. *
  1378. * If the firmware patching fails, the manufacturer mode is
  1379. * disabled with reset and deactivating the patch.
  1380. *
  1381. * If the default patch file is used, no reset is done when disabling
  1382. * the manufacturer.
  1383. */
  1384. while (fw->size > fw_ptr - fw->data) {
  1385. int ret;
  1386. ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
  1387. &disable_patch);
  1388. if (ret < 0)
  1389. goto exit_mfg_deactivate;
  1390. }
  1391. release_firmware(fw);
  1392. if (disable_patch)
  1393. goto exit_mfg_disable;
  1394. /* Patching completed successfully and disable the manufacturer mode
  1395. * with reset and activate the downloaded firmware patches.
  1396. */
  1397. skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
  1398. mfg_reset_activate, HCI_INIT_TIMEOUT);
  1399. if (IS_ERR(skb)) {
  1400. BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
  1401. hdev->name, PTR_ERR(skb));
  1402. return PTR_ERR(skb);
  1403. }
  1404. kfree_skb(skb);
  1405. BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
  1406. hdev->name);
  1407. goto complete;
  1408. exit_mfg_disable:
  1409. /* Disable the manufacturer mode without reset */
  1410. skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
  1411. HCI_INIT_TIMEOUT);
  1412. if (IS_ERR(skb)) {
  1413. BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
  1414. hdev->name, PTR_ERR(skb));
  1415. return PTR_ERR(skb);
  1416. }
  1417. kfree_skb(skb);
  1418. BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
  1419. goto complete;
  1420. exit_mfg_deactivate:
  1421. release_firmware(fw);
  1422. /* Patching failed. Disable the manufacturer mode with reset and
  1423. * deactivate the downloaded firmware patches.
  1424. */
  1425. skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
  1426. mfg_reset_deactivate, HCI_INIT_TIMEOUT);
  1427. if (IS_ERR(skb)) {
  1428. BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
  1429. hdev->name, PTR_ERR(skb));
  1430. return PTR_ERR(skb);
  1431. }
  1432. kfree_skb(skb);
  1433. BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
  1434. hdev->name);
  1435. complete:
  1436. /* Set the event mask for Intel specific vendor events. This enables
  1437. * a few extra events that are useful during general operation.
  1438. */
  1439. btintel_set_event_mask_mfg(hdev, false);
  1440. btintel_check_bdaddr(hdev);
  1441. return 0;
  1442. }
  1443. static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
  1444. {
  1445. struct sk_buff *skb;
  1446. struct hci_event_hdr *hdr;
  1447. struct hci_ev_cmd_complete *evt;
  1448. skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
  1449. if (!skb)
  1450. return -ENOMEM;
  1451. hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
  1452. hdr->evt = HCI_EV_CMD_COMPLETE;
  1453. hdr->plen = sizeof(*evt) + 1;
  1454. evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
  1455. evt->ncmd = 0x01;
  1456. evt->opcode = cpu_to_le16(opcode);
  1457. *skb_put(skb, 1) = 0x00;
  1458. bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
  1459. return hci_recv_frame(hdev, skb);
  1460. }
  1461. static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
  1462. int count)
  1463. {
  1464. /* When the device is in bootloader mode, then it can send
  1465. * events via the bulk endpoint. These events are treated the
  1466. * same way as the ones received from the interrupt endpoint.
  1467. */
  1468. if (test_bit(BTUSB_BOOTLOADER, &data->flags))
  1469. return btusb_recv_intr(data, buffer, count);
  1470. return btusb_recv_bulk(data, buffer, count);
  1471. }
  1472. static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
  1473. unsigned int len)
  1474. {
  1475. const struct intel_bootup *evt = ptr;
  1476. if (len != sizeof(*evt))
  1477. return;
  1478. if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
  1479. smp_mb__after_atomic();
  1480. wake_up_bit(&data->flags, BTUSB_BOOTING);
  1481. }
  1482. }
  1483. static void btusb_intel_secure_send_result(struct btusb_data *data,
  1484. const void *ptr, unsigned int len)
  1485. {
  1486. const struct intel_secure_send_result *evt = ptr;
  1487. if (len != sizeof(*evt))
  1488. return;
  1489. if (evt->result)
  1490. set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
  1491. if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
  1492. test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
  1493. smp_mb__after_atomic();
  1494. wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
  1495. }
  1496. }
  1497. static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
  1498. {
  1499. struct btusb_data *data = hci_get_drvdata(hdev);
  1500. if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
  1501. struct hci_event_hdr *hdr = (void *)skb->data;
  1502. if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
  1503. hdr->plen > 0) {
  1504. const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
  1505. unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
  1506. switch (skb->data[2]) {
  1507. case 0x02:
  1508. /* When switching to the operational firmware
  1509. * the device sends a vendor specific event
  1510. * indicating that the bootup completed.
  1511. */
  1512. btusb_intel_bootup(data, ptr, len);
  1513. break;
  1514. case 0x06:
  1515. /* When the firmware loading completes the
  1516. * device sends out a vendor specific event
  1517. * indicating the result of the firmware
  1518. * loading.
  1519. */
  1520. btusb_intel_secure_send_result(data, ptr, len);
  1521. break;
  1522. }
  1523. }
  1524. }
  1525. return hci_recv_frame(hdev, skb);
  1526. }
  1527. static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
  1528. {
  1529. struct btusb_data *data = hci_get_drvdata(hdev);
  1530. struct urb *urb;
  1531. BT_DBG("%s", hdev->name);
  1532. switch (bt_cb(skb)->pkt_type) {
  1533. case HCI_COMMAND_PKT:
  1534. if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
  1535. struct hci_command_hdr *cmd = (void *)skb->data;
  1536. __u16 opcode = le16_to_cpu(cmd->opcode);
  1537. /* When in bootloader mode and the command 0xfc09
  1538. * is received, it needs to be send down the
  1539. * bulk endpoint. So allocate a bulk URB instead.
  1540. */
  1541. if (opcode == 0xfc09)
  1542. urb = alloc_bulk_urb(hdev, skb);
  1543. else
  1544. urb = alloc_ctrl_urb(hdev, skb);
  1545. /* When the 0xfc01 command is issued to boot into
  1546. * the operational firmware, it will actually not
  1547. * send a command complete event. To keep the flow
  1548. * control working inject that event here.
  1549. */
  1550. if (opcode == 0xfc01)
  1551. inject_cmd_complete(hdev, opcode);
  1552. } else {
  1553. urb = alloc_ctrl_urb(hdev, skb);
  1554. }
  1555. if (IS_ERR(urb))
  1556. return PTR_ERR(urb);
  1557. hdev->stat.cmd_tx++;
  1558. return submit_or_queue_tx_urb(hdev, urb);
  1559. case HCI_ACLDATA_PKT:
  1560. urb = alloc_bulk_urb(hdev, skb);
  1561. if (IS_ERR(urb))
  1562. return PTR_ERR(urb);
  1563. hdev->stat.acl_tx++;
  1564. return submit_or_queue_tx_urb(hdev, urb);
  1565. case HCI_SCODATA_PKT:
  1566. if (hci_conn_num(hdev, SCO_LINK) < 1)
  1567. return -ENODEV;
  1568. urb = alloc_isoc_urb(hdev, skb);
  1569. if (IS_ERR(urb))
  1570. return PTR_ERR(urb);
  1571. hdev->stat.sco_tx++;
  1572. return submit_tx_urb(hdev, urb);
  1573. }
  1574. return -EILSEQ;
  1575. }
  1576. static int btusb_setup_intel_new(struct hci_dev *hdev)
  1577. {
  1578. static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
  1579. 0x00, 0x08, 0x04, 0x00 };
  1580. struct btusb_data *data = hci_get_drvdata(hdev);
  1581. struct sk_buff *skb;
  1582. struct intel_version *ver;
  1583. struct intel_boot_params *params;
  1584. const struct firmware *fw;
  1585. const u8 *fw_ptr;
  1586. u32 frag_len;
  1587. char fwname[64];
  1588. ktime_t calltime, delta, rettime;
  1589. unsigned long long duration;
  1590. int err;
  1591. BT_DBG("%s", hdev->name);
  1592. calltime = ktime_get();
  1593. /* Read the Intel version information to determine if the device
  1594. * is in bootloader mode or if it already has operational firmware
  1595. * loaded.
  1596. */
  1597. skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
  1598. if (IS_ERR(skb)) {
  1599. BT_ERR("%s: Reading Intel version information failed (%ld)",
  1600. hdev->name, PTR_ERR(skb));
  1601. return PTR_ERR(skb);
  1602. }
  1603. if (skb->len != sizeof(*ver)) {
  1604. BT_ERR("%s: Intel version event size mismatch", hdev->name);
  1605. kfree_skb(skb);
  1606. return -EILSEQ;
  1607. }
  1608. ver = (struct intel_version *)skb->data;
  1609. /* The hardware platform number has a fixed value of 0x37 and
  1610. * for now only accept this single value.
  1611. */
  1612. if (ver->hw_platform != 0x37) {
  1613. BT_ERR("%s: Unsupported Intel hardware platform (%u)",
  1614. hdev->name, ver->hw_platform);
  1615. kfree_skb(skb);
  1616. return -EINVAL;
  1617. }
  1618. /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
  1619. * supported by this firmware loading method. This check has been
  1620. * put in place to ensure correct forward compatibility options
  1621. * when newer hardware variants come along.
  1622. */
  1623. if (ver->hw_variant != 0x0b) {
  1624. BT_ERR("%s: Unsupported Intel hardware variant (%u)",
  1625. hdev->name, ver->hw_variant);
  1626. kfree_skb(skb);
  1627. return -EINVAL;
  1628. }
  1629. btintel_version_info(hdev, ver);
  1630. /* The firmware variant determines if the device is in bootloader
  1631. * mode or is running operational firmware. The value 0x06 identifies
  1632. * the bootloader and the value 0x23 identifies the operational
  1633. * firmware.
  1634. *
  1635. * When the operational firmware is already present, then only
  1636. * the check for valid Bluetooth device address is needed. This
  1637. * determines if the device will be added as configured or
  1638. * unconfigured controller.
  1639. *
  1640. * It is not possible to use the Secure Boot Parameters in this
  1641. * case since that command is only available in bootloader mode.
  1642. */
  1643. if (ver->fw_variant == 0x23) {
  1644. kfree_skb(skb);
  1645. clear_bit(BTUSB_BOOTLOADER, &data->flags);
  1646. btintel_check_bdaddr(hdev);
  1647. return 0;
  1648. }
  1649. /* If the device is not in bootloader mode, then the only possible
  1650. * choice is to return an error and abort the device initialization.
  1651. */
  1652. if (ver->fw_variant != 0x06) {
  1653. BT_ERR("%s: Unsupported Intel firmware variant (%u)",
  1654. hdev->name, ver->fw_variant);
  1655. kfree_skb(skb);
  1656. return -ENODEV;
  1657. }
  1658. kfree_skb(skb);
  1659. /* Read the secure boot parameters to identify the operating
  1660. * details of the bootloader.
  1661. */
  1662. skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
  1663. if (IS_ERR(skb)) {
  1664. BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
  1665. hdev->name, PTR_ERR(skb));
  1666. return PTR_ERR(skb);
  1667. }
  1668. if (skb->len != sizeof(*params)) {
  1669. BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
  1670. kfree_skb(skb);
  1671. return -EILSEQ;
  1672. }
  1673. params = (struct intel_boot_params *)skb->data;
  1674. BT_INFO("%s: Device revision is %u", hdev->name,
  1675. le16_to_cpu(params->dev_revid));
  1676. BT_INFO("%s: Secure boot is %s", hdev->name,
  1677. params->secure_boot ? "enabled" : "disabled");
  1678. BT_INFO("%s: OTP lock is %s", hdev->name,
  1679. params->otp_lock ? "enabled" : "disabled");
  1680. BT_INFO("%s: API lock is %s", hdev->name,
  1681. params->api_lock ? "enabled" : "disabled");
  1682. BT_INFO("%s: Debug lock is %s", hdev->name,
  1683. params->debug_lock ? "enabled" : "disabled");
  1684. BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
  1685. params->min_fw_build_nn, params->min_fw_build_cw,
  1686. 2000 + params->min_fw_build_yy);
  1687. /* It is required that every single firmware fragment is acknowledged
  1688. * with a command complete event. If the boot parameters indicate
  1689. * that this bootloader does not send them, then abort the setup.
  1690. */
  1691. if (params->limited_cce != 0x00) {
  1692. BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
  1693. hdev->name, params->limited_cce);
  1694. kfree_skb(skb);
  1695. return -EINVAL;
  1696. }
  1697. /* If the OTP has no valid Bluetooth device address, then there will
  1698. * also be no valid address for the operational firmware.
  1699. */
  1700. if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
  1701. BT_INFO("%s: No device address configured", hdev->name);
  1702. set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  1703. }
  1704. /* With this Intel bootloader only the hardware variant and device
  1705. * revision information are used to select the right firmware.
  1706. *
  1707. * Currently this bootloader support is limited to hardware variant
  1708. * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
  1709. */
  1710. snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
  1711. le16_to_cpu(params->dev_revid));
  1712. err = request_firmware(&fw, fwname, &hdev->dev);
  1713. if (err < 0) {
  1714. BT_ERR("%s: Failed to load Intel firmware file (%d)",
  1715. hdev->name, err);
  1716. kfree_skb(skb);
  1717. return err;
  1718. }
  1719. BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
  1720. /* Save the DDC file name for later use to apply once the firmware
  1721. * downloading is done.
  1722. */
  1723. snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.ddc",
  1724. le16_to_cpu(params->dev_revid));
  1725. kfree_skb(skb);
  1726. if (fw->size < 644) {
  1727. BT_ERR("%s: Invalid size of firmware file (%zu)",
  1728. hdev->name, fw->size);
  1729. err = -EBADF;
  1730. goto done;
  1731. }
  1732. set_bit(BTUSB_DOWNLOADING, &data->flags);
  1733. /* Start the firmware download transaction with the Init fragment
  1734. * represented by the 128 bytes of CSS header.
  1735. */
  1736. err = btintel_secure_send(hdev, 0x00, 128, fw->data);
  1737. if (err < 0) {
  1738. BT_ERR("%s: Failed to send firmware header (%d)",
  1739. hdev->name, err);
  1740. goto done;
  1741. }
  1742. /* Send the 256 bytes of public key information from the firmware
  1743. * as the PKey fragment.
  1744. */
  1745. err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
  1746. if (err < 0) {
  1747. BT_ERR("%s: Failed to send firmware public key (%d)",
  1748. hdev->name, err);
  1749. goto done;
  1750. }
  1751. /* Send the 256 bytes of signature information from the firmware
  1752. * as the Sign fragment.
  1753. */
  1754. err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
  1755. if (err < 0) {
  1756. BT_ERR("%s: Failed to send firmware signature (%d)",
  1757. hdev->name, err);
  1758. goto done;
  1759. }
  1760. fw_ptr = fw->data + 644;
  1761. frag_len = 0;
  1762. while (fw_ptr - fw->data < fw->size) {
  1763. struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
  1764. frag_len += sizeof(*cmd) + cmd->plen;
  1765. /* The parameter length of the secure send command requires
  1766. * a 4 byte alignment. It happens so that the firmware file
  1767. * contains proper Intel_NOP commands to align the fragments
  1768. * as needed.
  1769. *
  1770. * Send set of commands with 4 byte alignment from the
  1771. * firmware data buffer as a single Data fragement.
  1772. */
  1773. if (!(frag_len % 4)) {
  1774. err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
  1775. if (err < 0) {
  1776. BT_ERR("%s: Failed to send firmware data (%d)",
  1777. hdev->name, err);
  1778. goto done;
  1779. }
  1780. fw_ptr += frag_len;
  1781. frag_len = 0;
  1782. }
  1783. }
  1784. set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
  1785. BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
  1786. /* Before switching the device into operational mode and with that
  1787. * booting the loaded firmware, wait for the bootloader notification
  1788. * that all fragments have been successfully received.
  1789. *
  1790. * When the event processing receives the notification, then the
  1791. * BTUSB_DOWNLOADING flag will be cleared.
  1792. *
  1793. * The firmware loading should not take longer than 5 seconds
  1794. * and thus just timeout if that happens and fail the setup
  1795. * of this device.
  1796. */
  1797. err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
  1798. TASK_INTERRUPTIBLE,
  1799. msecs_to_jiffies(5000));
  1800. if (err == 1) {
  1801. BT_ERR("%s: Firmware loading interrupted", hdev->name);
  1802. err = -EINTR;
  1803. goto done;
  1804. }
  1805. if (err) {
  1806. BT_ERR("%s: Firmware loading timeout", hdev->name);
  1807. err = -ETIMEDOUT;
  1808. goto done;
  1809. }
  1810. if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
  1811. BT_ERR("%s: Firmware loading failed", hdev->name);
  1812. err = -ENOEXEC;
  1813. goto done;
  1814. }
  1815. rettime = ktime_get();
  1816. delta = ktime_sub(rettime, calltime);
  1817. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  1818. BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
  1819. done:
  1820. release_firmware(fw);
  1821. if (err < 0)
  1822. return err;
  1823. calltime = ktime_get();
  1824. set_bit(BTUSB_BOOTING, &data->flags);
  1825. skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
  1826. HCI_INIT_TIMEOUT);
  1827. if (IS_ERR(skb))
  1828. return PTR_ERR(skb);
  1829. kfree_skb(skb);
  1830. /* The bootloader will not indicate when the device is ready. This
  1831. * is done by the operational firmware sending bootup notification.
  1832. *
  1833. * Booting into operational firmware should not take longer than
  1834. * 1 second. However if that happens, then just fail the setup
  1835. * since something went wrong.
  1836. */
  1837. BT_INFO("%s: Waiting for device to boot", hdev->name);
  1838. err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
  1839. TASK_INTERRUPTIBLE,
  1840. msecs_to_jiffies(1000));
  1841. if (err == 1) {
  1842. BT_ERR("%s: Device boot interrupted", hdev->name);
  1843. return -EINTR;
  1844. }
  1845. if (err) {
  1846. BT_ERR("%s: Device boot timeout", hdev->name);
  1847. return -ETIMEDOUT;
  1848. }
  1849. rettime = ktime_get();
  1850. delta = ktime_sub(rettime, calltime);
  1851. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  1852. BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
  1853. clear_bit(BTUSB_BOOTLOADER, &data->flags);
  1854. /* Once the device is running in operational mode, it needs to apply
  1855. * the device configuration (DDC) parameters.
  1856. *
  1857. * The device can work without DDC parameters, so even if it fails
  1858. * to load the file, no need to fail the setup.
  1859. */
  1860. btintel_load_ddc_config(hdev, fwname);
  1861. /* Set the event mask for Intel specific vendor events. This enables
  1862. * a few extra events that are useful during general operation. It
  1863. * does not enable any debugging related events.
  1864. *
  1865. * The device will function correctly without these events enabled
  1866. * and thus no need to fail the setup.
  1867. */
  1868. btintel_set_event_mask(hdev, false);
  1869. return 0;
  1870. }
  1871. static int btusb_shutdown_intel(struct hci_dev *hdev)
  1872. {
  1873. struct sk_buff *skb;
  1874. long ret;
  1875. /* Some platforms have an issue with BT LED when the interface is
  1876. * down or BT radio is turned off, which takes 5 seconds to BT LED
  1877. * goes off. This command turns off the BT LED immediately.
  1878. */
  1879. skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
  1880. if (IS_ERR(skb)) {
  1881. ret = PTR_ERR(skb);
  1882. BT_ERR("%s: turning off Intel device LED failed (%ld)",
  1883. hdev->name, ret);
  1884. return ret;
  1885. }
  1886. kfree_skb(skb);
  1887. return 0;
  1888. }
  1889. static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
  1890. const bdaddr_t *bdaddr)
  1891. {
  1892. struct sk_buff *skb;
  1893. u8 buf[8];
  1894. long ret;
  1895. buf[0] = 0xfe;
  1896. buf[1] = sizeof(bdaddr_t);
  1897. memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
  1898. skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
  1899. if (IS_ERR(skb)) {
  1900. ret = PTR_ERR(skb);
  1901. BT_ERR("%s: changing Marvell device address failed (%ld)",
  1902. hdev->name, ret);
  1903. return ret;
  1904. }
  1905. kfree_skb(skb);
  1906. return 0;
  1907. }
  1908. static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
  1909. const bdaddr_t *bdaddr)
  1910. {
  1911. struct sk_buff *skb;
  1912. u8 buf[10];
  1913. long ret;
  1914. buf[0] = 0x01;
  1915. buf[1] = 0x01;
  1916. buf[2] = 0x00;
  1917. buf[3] = sizeof(bdaddr_t);
  1918. memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
  1919. skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
  1920. if (IS_ERR(skb)) {
  1921. ret = PTR_ERR(skb);
  1922. BT_ERR("%s: Change address command failed (%ld)",
  1923. hdev->name, ret);
  1924. return ret;
  1925. }
  1926. kfree_skb(skb);
  1927. return 0;
  1928. }
  1929. #define QCA_DFU_PACKET_LEN 4096
  1930. #define QCA_GET_TARGET_VERSION 0x09
  1931. #define QCA_CHECK_STATUS 0x05
  1932. #define QCA_DFU_DOWNLOAD 0x01
  1933. #define QCA_SYSCFG_UPDATED 0x40
  1934. #define QCA_PATCH_UPDATED 0x80
  1935. #define QCA_DFU_TIMEOUT 3000
  1936. struct qca_version {
  1937. __le32 rom_version;
  1938. __le32 patch_version;
  1939. __le32 ram_version;
  1940. __le32 ref_clock;
  1941. __u8 reserved[4];
  1942. } __packed;
  1943. struct qca_rampatch_version {
  1944. __le16 rom_version;
  1945. __le16 patch_version;
  1946. } __packed;
  1947. struct qca_device_info {
  1948. u32 rom_version;
  1949. u8 rampatch_hdr; /* length of header in rampatch */
  1950. u8 nvm_hdr; /* length of header in NVM */
  1951. u8 ver_offset; /* offset of version structure in rampatch */
  1952. };
  1953. static const struct qca_device_info qca_devices_table[] = {
  1954. { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
  1955. { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
  1956. { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
  1957. { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
  1958. { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
  1959. { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
  1960. };
  1961. static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
  1962. void *data, u16 size)
  1963. {
  1964. struct btusb_data *btdata = hci_get_drvdata(hdev);
  1965. struct usb_device *udev = btdata->udev;
  1966. int pipe, err;
  1967. u8 *buf;
  1968. buf = kmalloc(size, GFP_KERNEL);
  1969. if (!buf)
  1970. return -ENOMEM;
  1971. /* Found some of USB hosts have IOT issues with ours so that we should
  1972. * not wait until HCI layer is ready.
  1973. */
  1974. pipe = usb_rcvctrlpipe(udev, 0);
  1975. err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
  1976. 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
  1977. if (err < 0) {
  1978. BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
  1979. goto done;
  1980. }
  1981. memcpy(data, buf, size);
  1982. done:
  1983. kfree(buf);
  1984. return err;
  1985. }
  1986. static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
  1987. const struct firmware *firmware,
  1988. size_t hdr_size)
  1989. {
  1990. struct btusb_data *btdata = hci_get_drvdata(hdev);
  1991. struct usb_device *udev = btdata->udev;
  1992. size_t count, size, sent = 0;
  1993. int pipe, len, err;
  1994. u8 *buf;
  1995. buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
  1996. if (!buf)
  1997. return -ENOMEM;
  1998. count = firmware->size;
  1999. size = min_t(size_t, count, hdr_size);
  2000. memcpy(buf, firmware->data, size);
  2001. /* USB patches should go down to controller through USB path
  2002. * because binary format fits to go down through USB channel.
  2003. * USB control path is for patching headers and USB bulk is for
  2004. * patch body.
  2005. */
  2006. pipe = usb_sndctrlpipe(udev, 0);
  2007. err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
  2008. 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
  2009. if (err < 0) {
  2010. BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
  2011. goto done;
  2012. }
  2013. sent += size;
  2014. count -= size;
  2015. while (count) {
  2016. size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
  2017. memcpy(buf, firmware->data + sent, size);
  2018. pipe = usb_sndbulkpipe(udev, 0x02);
  2019. err = usb_bulk_msg(udev, pipe, buf, size, &len,
  2020. QCA_DFU_TIMEOUT);
  2021. if (err < 0) {
  2022. BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
  2023. hdev->name, sent, firmware->size, err);
  2024. break;
  2025. }
  2026. if (size != len) {
  2027. BT_ERR("%s: Failed to get bulk buffer", hdev->name);
  2028. err = -EILSEQ;
  2029. break;
  2030. }
  2031. sent += size;
  2032. count -= size;
  2033. }
  2034. done:
  2035. kfree(buf);
  2036. return err;
  2037. }
  2038. static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
  2039. struct qca_version *ver,
  2040. const struct qca_device_info *info)
  2041. {
  2042. struct qca_rampatch_version *rver;
  2043. const struct firmware *fw;
  2044. u32 ver_rom, ver_patch;
  2045. u16 rver_rom, rver_patch;
  2046. char fwname[64];
  2047. int err;
  2048. ver_rom = le32_to_cpu(ver->rom_version);
  2049. ver_patch = le32_to_cpu(ver->patch_version);
  2050. snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
  2051. err = request_firmware(&fw, fwname, &hdev->dev);
  2052. if (err) {
  2053. BT_ERR("%s: failed to request rampatch file: %s (%d)",
  2054. hdev->name, fwname, err);
  2055. return err;
  2056. }
  2057. BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
  2058. rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
  2059. rver_rom = le16_to_cpu(rver->rom_version);
  2060. rver_patch = le16_to_cpu(rver->patch_version);
  2061. BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
  2062. "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
  2063. ver_patch);
  2064. if (rver_rom != ver_rom || rver_patch <= ver_patch) {
  2065. BT_ERR("%s: rampatch file version did not match with firmware",
  2066. hdev->name);
  2067. err = -EINVAL;
  2068. goto done;
  2069. }
  2070. err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
  2071. done:
  2072. release_firmware(fw);
  2073. return err;
  2074. }
  2075. static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
  2076. struct qca_version *ver,
  2077. const struct qca_device_info *info)
  2078. {
  2079. const struct firmware *fw;
  2080. char fwname[64];
  2081. int err;
  2082. snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
  2083. le32_to_cpu(ver->rom_version));
  2084. err = request_firmware(&fw, fwname, &hdev->dev);
  2085. if (err) {
  2086. BT_ERR("%s: failed to request NVM file: %s (%d)",
  2087. hdev->name, fwname, err);
  2088. return err;
  2089. }
  2090. BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
  2091. err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
  2092. release_firmware(fw);
  2093. return err;
  2094. }
  2095. static int btusb_setup_qca(struct hci_dev *hdev)
  2096. {
  2097. const struct qca_device_info *info = NULL;
  2098. struct qca_version ver;
  2099. u32 ver_rom;
  2100. u8 status;
  2101. int i, err;
  2102. err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
  2103. sizeof(ver));
  2104. if (err < 0)
  2105. return err;
  2106. ver_rom = le32_to_cpu(ver.rom_version);
  2107. for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
  2108. if (ver_rom == qca_devices_table[i].rom_version)
  2109. info = &qca_devices_table[i];
  2110. }
  2111. if (!info) {
  2112. BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
  2113. ver_rom);
  2114. return -ENODEV;
  2115. }
  2116. err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
  2117. sizeof(status));
  2118. if (err < 0)
  2119. return err;
  2120. if (!(status & QCA_PATCH_UPDATED)) {
  2121. err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
  2122. if (err < 0)
  2123. return err;
  2124. }
  2125. if (!(status & QCA_SYSCFG_UPDATED)) {
  2126. err = btusb_setup_qca_load_nvm(hdev, &ver, info);
  2127. if (err < 0)
  2128. return err;
  2129. }
  2130. return 0;
  2131. }
  2132. #ifdef CONFIG_BT_HCIBTUSB_BCM
  2133. static inline int __set_diag_interface(struct hci_dev *hdev)
  2134. {
  2135. struct btusb_data *data = hci_get_drvdata(hdev);
  2136. struct usb_interface *intf = data->diag;
  2137. int i;
  2138. if (!data->diag)
  2139. return -ENODEV;
  2140. data->diag_tx_ep = NULL;
  2141. data->diag_rx_ep = NULL;
  2142. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  2143. struct usb_endpoint_descriptor *ep_desc;
  2144. ep_desc = &intf->cur_altsetting->endpoint[i].desc;
  2145. if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
  2146. data->diag_tx_ep = ep_desc;
  2147. continue;
  2148. }
  2149. if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
  2150. data->diag_rx_ep = ep_desc;
  2151. continue;
  2152. }
  2153. }
  2154. if (!data->diag_tx_ep || !data->diag_rx_ep) {
  2155. BT_ERR("%s invalid diagnostic descriptors", hdev->name);
  2156. return -ENODEV;
  2157. }
  2158. return 0;
  2159. }
  2160. static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
  2161. {
  2162. struct btusb_data *data = hci_get_drvdata(hdev);
  2163. struct sk_buff *skb;
  2164. struct urb *urb;
  2165. unsigned int pipe;
  2166. if (!data->diag_tx_ep)
  2167. return ERR_PTR(-ENODEV);
  2168. urb = usb_alloc_urb(0, GFP_KERNEL);
  2169. if (!urb)
  2170. return ERR_PTR(-ENOMEM);
  2171. skb = bt_skb_alloc(2, GFP_KERNEL);
  2172. if (!skb) {
  2173. usb_free_urb(urb);
  2174. return ERR_PTR(-ENOMEM);
  2175. }
  2176. *skb_put(skb, 1) = 0xf0;
  2177. *skb_put(skb, 1) = enable;
  2178. pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
  2179. usb_fill_bulk_urb(urb, data->udev, pipe,
  2180. skb->data, skb->len, btusb_tx_complete, skb);
  2181. skb->dev = (void *)hdev;
  2182. return urb;
  2183. }
  2184. static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
  2185. {
  2186. struct btusb_data *data = hci_get_drvdata(hdev);
  2187. struct urb *urb;
  2188. if (!data->diag)
  2189. return -ENODEV;
  2190. if (!test_bit(HCI_RUNNING, &hdev->flags))
  2191. return -ENETDOWN;
  2192. urb = alloc_diag_urb(hdev, enable);
  2193. if (IS_ERR(urb))
  2194. return PTR_ERR(urb);
  2195. return submit_or_queue_tx_urb(hdev, urb);
  2196. }
  2197. #endif
  2198. static int btusb_probe(struct usb_interface *intf,
  2199. const struct usb_device_id *id)
  2200. {
  2201. struct usb_endpoint_descriptor *ep_desc;
  2202. struct btusb_data *data;
  2203. struct hci_dev *hdev;
  2204. unsigned ifnum_base;
  2205. int i, err;
  2206. BT_DBG("intf %p id %p", intf, id);
  2207. /* interface numbers are hardcoded in the spec */
  2208. if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
  2209. if (!(id->driver_info & BTUSB_IFNUM_2))
  2210. return -ENODEV;
  2211. if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
  2212. return -ENODEV;
  2213. }
  2214. ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
  2215. if (!id->driver_info) {
  2216. const struct usb_device_id *match;
  2217. match = usb_match_id(intf, blacklist_table);
  2218. if (match)
  2219. id = match;
  2220. }
  2221. if (id->driver_info == BTUSB_IGNORE)
  2222. return -ENODEV;
  2223. if (id->driver_info & BTUSB_ATH3012) {
  2224. struct usb_device *udev = interface_to_usbdev(intf);
  2225. /* Old firmware would otherwise let ath3k driver load
  2226. * patch and sysconfig files */
  2227. if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
  2228. return -ENODEV;
  2229. }
  2230. data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
  2231. if (!data)
  2232. return -ENOMEM;
  2233. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  2234. ep_desc = &intf->cur_altsetting->endpoint[i].desc;
  2235. if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
  2236. data->intr_ep = ep_desc;
  2237. continue;
  2238. }
  2239. if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
  2240. data->bulk_tx_ep = ep_desc;
  2241. continue;
  2242. }
  2243. if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
  2244. data->bulk_rx_ep = ep_desc;
  2245. continue;
  2246. }
  2247. }
  2248. if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
  2249. return -ENODEV;
  2250. if (id->driver_info & BTUSB_AMP) {
  2251. data->cmdreq_type = USB_TYPE_CLASS | 0x01;
  2252. data->cmdreq = 0x2b;
  2253. } else {
  2254. data->cmdreq_type = USB_TYPE_CLASS;
  2255. data->cmdreq = 0x00;
  2256. }
  2257. data->udev = interface_to_usbdev(intf);
  2258. data->intf = intf;
  2259. INIT_WORK(&data->work, btusb_work);
  2260. INIT_WORK(&data->waker, btusb_waker);
  2261. init_usb_anchor(&data->deferred);
  2262. init_usb_anchor(&data->tx_anchor);
  2263. spin_lock_init(&data->txlock);
  2264. init_usb_anchor(&data->intr_anchor);
  2265. init_usb_anchor(&data->bulk_anchor);
  2266. init_usb_anchor(&data->isoc_anchor);
  2267. init_usb_anchor(&data->diag_anchor);
  2268. spin_lock_init(&data->rxlock);
  2269. if (id->driver_info & BTUSB_INTEL_NEW) {
  2270. data->recv_event = btusb_recv_event_intel;
  2271. data->recv_bulk = btusb_recv_bulk_intel;
  2272. set_bit(BTUSB_BOOTLOADER, &data->flags);
  2273. } else {
  2274. data->recv_event = hci_recv_frame;
  2275. data->recv_bulk = btusb_recv_bulk;
  2276. }
  2277. hdev = hci_alloc_dev();
  2278. if (!hdev)
  2279. return -ENOMEM;
  2280. hdev->bus = HCI_USB;
  2281. hci_set_drvdata(hdev, data);
  2282. if (id->driver_info & BTUSB_AMP)
  2283. hdev->dev_type = HCI_AMP;
  2284. else
  2285. hdev->dev_type = HCI_BREDR;
  2286. data->hdev = hdev;
  2287. SET_HCIDEV_DEV(hdev, &intf->dev);
  2288. hdev->open = btusb_open;
  2289. hdev->close = btusb_close;
  2290. hdev->flush = btusb_flush;
  2291. hdev->send = btusb_send_frame;
  2292. hdev->notify = btusb_notify;
  2293. if (id->driver_info & BTUSB_BCM2045)
  2294. set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
  2295. if (id->driver_info & BTUSB_BCM92035)
  2296. hdev->setup = btusb_setup_bcm92035;
  2297. #ifdef CONFIG_BT_HCIBTUSB_BCM
  2298. if (id->driver_info & BTUSB_BCM_PATCHRAM) {
  2299. hdev->manufacturer = 15;
  2300. hdev->setup = btbcm_setup_patchram;
  2301. hdev->set_diag = btusb_bcm_set_diag;
  2302. hdev->set_bdaddr = btbcm_set_bdaddr;
  2303. /* Broadcom LM_DIAG Interface numbers are hardcoded */
  2304. data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
  2305. }
  2306. if (id->driver_info & BTUSB_BCM_APPLE) {
  2307. hdev->manufacturer = 15;
  2308. hdev->setup = btbcm_setup_apple;
  2309. hdev->set_diag = btusb_bcm_set_diag;
  2310. /* Broadcom LM_DIAG Interface numbers are hardcoded */
  2311. data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
  2312. }
  2313. #endif
  2314. if (id->driver_info & BTUSB_INTEL) {
  2315. hdev->manufacturer = 2;
  2316. hdev->setup = btusb_setup_intel;
  2317. hdev->shutdown = btusb_shutdown_intel;
  2318. hdev->set_diag = btintel_set_diag_mfg;
  2319. hdev->set_bdaddr = btintel_set_bdaddr;
  2320. set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
  2321. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  2322. set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
  2323. }
  2324. if (id->driver_info & BTUSB_INTEL_NEW) {
  2325. hdev->manufacturer = 2;
  2326. hdev->send = btusb_send_frame_intel;
  2327. hdev->setup = btusb_setup_intel_new;
  2328. hdev->hw_error = btintel_hw_error;
  2329. hdev->set_diag = btintel_set_diag;
  2330. hdev->set_bdaddr = btintel_set_bdaddr;
  2331. set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
  2332. set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
  2333. }
  2334. if (id->driver_info & BTUSB_MARVELL)
  2335. hdev->set_bdaddr = btusb_set_bdaddr_marvell;
  2336. if (id->driver_info & BTUSB_SWAVE) {
  2337. set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
  2338. set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
  2339. }
  2340. if (id->driver_info & BTUSB_INTEL_BOOT) {
  2341. hdev->manufacturer = 2;
  2342. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  2343. }
  2344. if (id->driver_info & BTUSB_ATH3012) {
  2345. hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
  2346. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  2347. set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
  2348. }
  2349. if (id->driver_info & BTUSB_QCA_ROME) {
  2350. data->setup_on_usb = btusb_setup_qca;
  2351. hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
  2352. }
  2353. #ifdef CONFIG_BT_HCIBTUSB_RTL
  2354. if (id->driver_info & BTUSB_REALTEK) {
  2355. hdev->setup = btrtl_setup_realtek;
  2356. /* Realtek devices lose their updated firmware over suspend,
  2357. * but the USB hub doesn't notice any status change.
  2358. * Explicitly request a device reset on resume.
  2359. */
  2360. set_bit(BTUSB_RESET_RESUME, &data->flags);
  2361. }
  2362. #endif
  2363. if (id->driver_info & BTUSB_AMP) {
  2364. /* AMP controllers do not support SCO packets */
  2365. data->isoc = NULL;
  2366. } else {
  2367. /* Interface orders are hardcoded in the specification */
  2368. data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
  2369. }
  2370. if (!reset)
  2371. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  2372. if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
  2373. if (!disable_scofix)
  2374. set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
  2375. }
  2376. if (id->driver_info & BTUSB_BROKEN_ISOC)
  2377. data->isoc = NULL;
  2378. if (id->driver_info & BTUSB_DIGIANSWER) {
  2379. data->cmdreq_type = USB_TYPE_VENDOR;
  2380. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  2381. }
  2382. if (id->driver_info & BTUSB_CSR) {
  2383. struct usb_device *udev = data->udev;
  2384. u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
  2385. /* Old firmware would otherwise execute USB reset */
  2386. if (bcdDevice < 0x117)
  2387. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  2388. /* Fake CSR devices with broken commands */
  2389. if (bcdDevice <= 0x100 || bcdDevice == 0x134)
  2390. hdev->setup = btusb_setup_csr;
  2391. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  2392. }
  2393. if (id->driver_info & BTUSB_SNIFFER) {
  2394. struct usb_device *udev = data->udev;
  2395. /* New sniffer firmware has crippled HCI interface */
  2396. if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
  2397. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  2398. }
  2399. if (id->driver_info & BTUSB_INTEL_BOOT) {
  2400. /* A bug in the bootloader causes that interrupt interface is
  2401. * only enabled after receiving SetInterface(0, AltSetting=0).
  2402. */
  2403. err = usb_set_interface(data->udev, 0, 0);
  2404. if (err < 0) {
  2405. BT_ERR("failed to set interface 0, alt 0 %d", err);
  2406. hci_free_dev(hdev);
  2407. return err;
  2408. }
  2409. }
  2410. if (data->isoc) {
  2411. err = usb_driver_claim_interface(&btusb_driver,
  2412. data->isoc, data);
  2413. if (err < 0) {
  2414. hci_free_dev(hdev);
  2415. return err;
  2416. }
  2417. }
  2418. #ifdef CONFIG_BT_HCIBTUSB_BCM
  2419. if (data->diag) {
  2420. if (!usb_driver_claim_interface(&btusb_driver,
  2421. data->diag, data))
  2422. __set_diag_interface(hdev);
  2423. else
  2424. data->diag = NULL;
  2425. }
  2426. #endif
  2427. err = hci_register_dev(hdev);
  2428. if (err < 0) {
  2429. hci_free_dev(hdev);
  2430. return err;
  2431. }
  2432. usb_set_intfdata(intf, data);
  2433. return 0;
  2434. }
  2435. static void btusb_disconnect(struct usb_interface *intf)
  2436. {
  2437. struct btusb_data *data = usb_get_intfdata(intf);
  2438. struct hci_dev *hdev;
  2439. BT_DBG("intf %p", intf);
  2440. if (!data)
  2441. return;
  2442. hdev = data->hdev;
  2443. usb_set_intfdata(data->intf, NULL);
  2444. if (data->isoc)
  2445. usb_set_intfdata(data->isoc, NULL);
  2446. if (data->diag)
  2447. usb_set_intfdata(data->diag, NULL);
  2448. hci_unregister_dev(hdev);
  2449. if (intf == data->intf) {
  2450. if (data->isoc)
  2451. usb_driver_release_interface(&btusb_driver, data->isoc);
  2452. if (data->diag)
  2453. usb_driver_release_interface(&btusb_driver, data->diag);
  2454. } else if (intf == data->isoc) {
  2455. if (data->diag)
  2456. usb_driver_release_interface(&btusb_driver, data->diag);
  2457. usb_driver_release_interface(&btusb_driver, data->intf);
  2458. } else if (intf == data->diag) {
  2459. usb_driver_release_interface(&btusb_driver, data->intf);
  2460. if (data->isoc)
  2461. usb_driver_release_interface(&btusb_driver, data->isoc);
  2462. }
  2463. hci_free_dev(hdev);
  2464. }
  2465. #ifdef CONFIG_PM
  2466. static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
  2467. {
  2468. struct btusb_data *data = usb_get_intfdata(intf);
  2469. BT_DBG("intf %p", intf);
  2470. if (data->suspend_count++)
  2471. return 0;
  2472. spin_lock_irq(&data->txlock);
  2473. if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
  2474. set_bit(BTUSB_SUSPENDING, &data->flags);
  2475. spin_unlock_irq(&data->txlock);
  2476. } else {
  2477. spin_unlock_irq(&data->txlock);
  2478. data->suspend_count--;
  2479. return -EBUSY;
  2480. }
  2481. cancel_work_sync(&data->work);
  2482. btusb_stop_traffic(data);
  2483. usb_kill_anchored_urbs(&data->tx_anchor);
  2484. /* Optionally request a device reset on resume, but only when
  2485. * wakeups are disabled. If wakeups are enabled we assume the
  2486. * device will stay powered up throughout suspend.
  2487. */
  2488. if (test_bit(BTUSB_RESET_RESUME, &data->flags) &&
  2489. !device_may_wakeup(&data->udev->dev))
  2490. data->udev->reset_resume = 1;
  2491. return 0;
  2492. }
  2493. static void play_deferred(struct btusb_data *data)
  2494. {
  2495. struct urb *urb;
  2496. int err;
  2497. while ((urb = usb_get_from_anchor(&data->deferred))) {
  2498. err = usb_submit_urb(urb, GFP_ATOMIC);
  2499. if (err < 0)
  2500. break;
  2501. data->tx_in_flight++;
  2502. }
  2503. usb_scuttle_anchored_urbs(&data->deferred);
  2504. }
  2505. static int btusb_resume(struct usb_interface *intf)
  2506. {
  2507. struct btusb_data *data = usb_get_intfdata(intf);
  2508. struct hci_dev *hdev = data->hdev;
  2509. int err = 0;
  2510. BT_DBG("intf %p", intf);
  2511. if (--data->suspend_count)
  2512. return 0;
  2513. if (!test_bit(HCI_RUNNING, &hdev->flags))
  2514. goto done;
  2515. if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
  2516. err = btusb_submit_intr_urb(hdev, GFP_NOIO);
  2517. if (err < 0) {
  2518. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  2519. goto failed;
  2520. }
  2521. }
  2522. if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
  2523. err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
  2524. if (err < 0) {
  2525. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  2526. goto failed;
  2527. }
  2528. btusb_submit_bulk_urb(hdev, GFP_NOIO);
  2529. }
  2530. if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
  2531. if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
  2532. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  2533. else
  2534. btusb_submit_isoc_urb(hdev, GFP_NOIO);
  2535. }
  2536. spin_lock_irq(&data->txlock);
  2537. play_deferred(data);
  2538. clear_bit(BTUSB_SUSPENDING, &data->flags);
  2539. spin_unlock_irq(&data->txlock);
  2540. schedule_work(&data->work);
  2541. return 0;
  2542. failed:
  2543. usb_scuttle_anchored_urbs(&data->deferred);
  2544. done:
  2545. spin_lock_irq(&data->txlock);
  2546. clear_bit(BTUSB_SUSPENDING, &data->flags);
  2547. spin_unlock_irq(&data->txlock);
  2548. return err;
  2549. }
  2550. #endif
  2551. static struct usb_driver btusb_driver = {
  2552. .name = "btusb",
  2553. .probe = btusb_probe,
  2554. .disconnect = btusb_disconnect,
  2555. #ifdef CONFIG_PM
  2556. .suspend = btusb_suspend,
  2557. .resume = btusb_resume,
  2558. #endif
  2559. .id_table = btusb_table,
  2560. .supports_autosuspend = 1,
  2561. .disable_hub_initiated_lpm = 1,
  2562. };
  2563. module_usb_driver(btusb_driver);
  2564. module_param(disable_scofix, bool, 0644);
  2565. MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
  2566. module_param(force_scofix, bool, 0644);
  2567. MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
  2568. module_param(reset, bool, 0644);
  2569. MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
  2570. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  2571. MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
  2572. MODULE_VERSION(VERSION);
  2573. MODULE_LICENSE("GPL");