btusb.c 78 KB

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