btusb.c 81 KB

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