usb.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. /*
  2. * Copyright (c) 2011 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/firmware.h>
  19. #include <linux/usb.h>
  20. #include <linux/vmalloc.h>
  21. #include <brcmu_utils.h>
  22. #include <brcm_hw_ids.h>
  23. #include <brcmu_wifi.h>
  24. #include <dhd_bus.h>
  25. #include <dhd_dbg.h>
  26. #include "firmware.h"
  27. #include "usb_rdl.h"
  28. #include "usb.h"
  29. #define IOCTL_RESP_TIMEOUT 2000
  30. #define BRCMF_USB_RESET_GETVER_SPINWAIT 100 /* in unit of ms */
  31. #define BRCMF_USB_RESET_GETVER_LOOP_CNT 10
  32. #define BRCMF_POSTBOOT_ID 0xA123 /* ID to detect if dongle
  33. has boot up */
  34. #define BRCMF_USB_NRXQ 50
  35. #define BRCMF_USB_NTXQ 50
  36. #define BRCMF_USB_CBCTL_WRITE 0
  37. #define BRCMF_USB_CBCTL_READ 1
  38. #define BRCMF_USB_MAX_PKT_SIZE 1600
  39. #define BRCMF_USB_43143_FW_NAME "brcm/brcmfmac43143.bin"
  40. #define BRCMF_USB_43236_FW_NAME "brcm/brcmfmac43236b.bin"
  41. #define BRCMF_USB_43242_FW_NAME "brcm/brcmfmac43242a.bin"
  42. #define BRCMF_USB_43569_FW_NAME "brcm/brcmfmac43569.bin"
  43. struct brcmf_usb_image {
  44. struct list_head list;
  45. s8 *fwname;
  46. u8 *image;
  47. int image_len;
  48. };
  49. struct brcmf_usbdev_info {
  50. struct brcmf_usbdev bus_pub; /* MUST BE FIRST */
  51. spinlock_t qlock;
  52. struct list_head rx_freeq;
  53. struct list_head rx_postq;
  54. struct list_head tx_freeq;
  55. struct list_head tx_postq;
  56. uint rx_pipe, tx_pipe;
  57. int rx_low_watermark;
  58. int tx_low_watermark;
  59. int tx_high_watermark;
  60. int tx_freecount;
  61. bool tx_flowblock;
  62. spinlock_t tx_flowblock_lock;
  63. struct brcmf_usbreq *tx_reqs;
  64. struct brcmf_usbreq *rx_reqs;
  65. const u8 *image; /* buffer for combine fw and nvram */
  66. int image_len;
  67. struct usb_device *usbdev;
  68. struct device *dev;
  69. int ctl_in_pipe, ctl_out_pipe;
  70. struct urb *ctl_urb; /* URB for control endpoint */
  71. struct usb_ctrlrequest ctl_write;
  72. struct usb_ctrlrequest ctl_read;
  73. u32 ctl_urb_actual_length;
  74. int ctl_urb_status;
  75. int ctl_completed;
  76. wait_queue_head_t ioctl_resp_wait;
  77. ulong ctl_op;
  78. u8 ifnum;
  79. struct urb *bulk_urb; /* used for FW download */
  80. };
  81. static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
  82. struct brcmf_usbreq *req);
  83. static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
  84. {
  85. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  86. return bus_if->bus_priv.usb;
  87. }
  88. static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
  89. {
  90. return brcmf_usb_get_buspub(dev)->devinfo;
  91. }
  92. static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo)
  93. {
  94. return wait_event_timeout(devinfo->ioctl_resp_wait,
  95. devinfo->ctl_completed,
  96. msecs_to_jiffies(IOCTL_RESP_TIMEOUT));
  97. }
  98. static void brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo)
  99. {
  100. if (waitqueue_active(&devinfo->ioctl_resp_wait))
  101. wake_up(&devinfo->ioctl_resp_wait);
  102. }
  103. static void
  104. brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
  105. {
  106. brcmf_dbg(USB, "Enter, status=%d\n", status);
  107. if (unlikely(devinfo == NULL))
  108. return;
  109. if (type == BRCMF_USB_CBCTL_READ) {
  110. if (status == 0)
  111. devinfo->bus_pub.stats.rx_ctlpkts++;
  112. else
  113. devinfo->bus_pub.stats.rx_ctlerrs++;
  114. } else if (type == BRCMF_USB_CBCTL_WRITE) {
  115. if (status == 0)
  116. devinfo->bus_pub.stats.tx_ctlpkts++;
  117. else
  118. devinfo->bus_pub.stats.tx_ctlerrs++;
  119. }
  120. devinfo->ctl_urb_status = status;
  121. devinfo->ctl_completed = true;
  122. brcmf_usb_ioctl_resp_wake(devinfo);
  123. }
  124. static void
  125. brcmf_usb_ctlread_complete(struct urb *urb)
  126. {
  127. struct brcmf_usbdev_info *devinfo =
  128. (struct brcmf_usbdev_info *)urb->context;
  129. brcmf_dbg(USB, "Enter\n");
  130. devinfo->ctl_urb_actual_length = urb->actual_length;
  131. brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ,
  132. urb->status);
  133. }
  134. static void
  135. brcmf_usb_ctlwrite_complete(struct urb *urb)
  136. {
  137. struct brcmf_usbdev_info *devinfo =
  138. (struct brcmf_usbdev_info *)urb->context;
  139. brcmf_dbg(USB, "Enter\n");
  140. brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
  141. urb->status);
  142. }
  143. static int
  144. brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
  145. {
  146. int ret;
  147. u16 size;
  148. brcmf_dbg(USB, "Enter\n");
  149. if (devinfo == NULL || buf == NULL ||
  150. len == 0 || devinfo->ctl_urb == NULL)
  151. return -EINVAL;
  152. size = len;
  153. devinfo->ctl_write.wLength = cpu_to_le16p(&size);
  154. devinfo->ctl_urb->transfer_buffer_length = size;
  155. devinfo->ctl_urb_status = 0;
  156. devinfo->ctl_urb_actual_length = 0;
  157. usb_fill_control_urb(devinfo->ctl_urb,
  158. devinfo->usbdev,
  159. devinfo->ctl_out_pipe,
  160. (unsigned char *) &devinfo->ctl_write,
  161. buf, size,
  162. (usb_complete_t)brcmf_usb_ctlwrite_complete,
  163. devinfo);
  164. ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
  165. if (ret < 0)
  166. brcmf_err("usb_submit_urb failed %d\n", ret);
  167. return ret;
  168. }
  169. static int
  170. brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
  171. {
  172. int ret;
  173. u16 size;
  174. brcmf_dbg(USB, "Enter\n");
  175. if ((devinfo == NULL) || (buf == NULL) || (len == 0)
  176. || (devinfo->ctl_urb == NULL))
  177. return -EINVAL;
  178. size = len;
  179. devinfo->ctl_read.wLength = cpu_to_le16p(&size);
  180. devinfo->ctl_urb->transfer_buffer_length = size;
  181. devinfo->ctl_read.bRequestType = USB_DIR_IN
  182. | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  183. devinfo->ctl_read.bRequest = 1;
  184. usb_fill_control_urb(devinfo->ctl_urb,
  185. devinfo->usbdev,
  186. devinfo->ctl_in_pipe,
  187. (unsigned char *) &devinfo->ctl_read,
  188. buf, size,
  189. (usb_complete_t)brcmf_usb_ctlread_complete,
  190. devinfo);
  191. ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
  192. if (ret < 0)
  193. brcmf_err("usb_submit_urb failed %d\n", ret);
  194. return ret;
  195. }
  196. static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
  197. {
  198. int err = 0;
  199. int timeout = 0;
  200. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  201. brcmf_dbg(USB, "Enter\n");
  202. if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
  203. return -EIO;
  204. if (test_and_set_bit(0, &devinfo->ctl_op))
  205. return -EIO;
  206. devinfo->ctl_completed = false;
  207. err = brcmf_usb_send_ctl(devinfo, buf, len);
  208. if (err) {
  209. brcmf_err("fail %d bytes: %d\n", err, len);
  210. clear_bit(0, &devinfo->ctl_op);
  211. return err;
  212. }
  213. timeout = brcmf_usb_ioctl_resp_wait(devinfo);
  214. clear_bit(0, &devinfo->ctl_op);
  215. if (!timeout) {
  216. brcmf_err("Txctl wait timed out\n");
  217. err = -EIO;
  218. }
  219. return err;
  220. }
  221. static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
  222. {
  223. int err = 0;
  224. int timeout = 0;
  225. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  226. brcmf_dbg(USB, "Enter\n");
  227. if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
  228. return -EIO;
  229. if (test_and_set_bit(0, &devinfo->ctl_op))
  230. return -EIO;
  231. devinfo->ctl_completed = false;
  232. err = brcmf_usb_recv_ctl(devinfo, buf, len);
  233. if (err) {
  234. brcmf_err("fail %d bytes: %d\n", err, len);
  235. clear_bit(0, &devinfo->ctl_op);
  236. return err;
  237. }
  238. timeout = brcmf_usb_ioctl_resp_wait(devinfo);
  239. err = devinfo->ctl_urb_status;
  240. clear_bit(0, &devinfo->ctl_op);
  241. if (!timeout) {
  242. brcmf_err("rxctl wait timed out\n");
  243. err = -EIO;
  244. }
  245. if (!err)
  246. return devinfo->ctl_urb_actual_length;
  247. else
  248. return err;
  249. }
  250. static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
  251. struct list_head *q, int *counter)
  252. {
  253. unsigned long flags;
  254. struct brcmf_usbreq *req;
  255. spin_lock_irqsave(&devinfo->qlock, flags);
  256. if (list_empty(q)) {
  257. spin_unlock_irqrestore(&devinfo->qlock, flags);
  258. return NULL;
  259. }
  260. req = list_entry(q->next, struct brcmf_usbreq, list);
  261. list_del_init(q->next);
  262. if (counter)
  263. (*counter)--;
  264. spin_unlock_irqrestore(&devinfo->qlock, flags);
  265. return req;
  266. }
  267. static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
  268. struct list_head *q, struct brcmf_usbreq *req,
  269. int *counter)
  270. {
  271. unsigned long flags;
  272. spin_lock_irqsave(&devinfo->qlock, flags);
  273. list_add_tail(&req->list, q);
  274. if (counter)
  275. (*counter)++;
  276. spin_unlock_irqrestore(&devinfo->qlock, flags);
  277. }
  278. static struct brcmf_usbreq *
  279. brcmf_usbdev_qinit(struct list_head *q, int qsize)
  280. {
  281. int i;
  282. struct brcmf_usbreq *req, *reqs;
  283. reqs = kcalloc(qsize, sizeof(struct brcmf_usbreq), GFP_ATOMIC);
  284. if (reqs == NULL)
  285. return NULL;
  286. req = reqs;
  287. for (i = 0; i < qsize; i++) {
  288. req->urb = usb_alloc_urb(0, GFP_ATOMIC);
  289. if (!req->urb)
  290. goto fail;
  291. INIT_LIST_HEAD(&req->list);
  292. list_add_tail(&req->list, q);
  293. req++;
  294. }
  295. return reqs;
  296. fail:
  297. brcmf_err("fail!\n");
  298. while (!list_empty(q)) {
  299. req = list_entry(q->next, struct brcmf_usbreq, list);
  300. if (req && req->urb)
  301. usb_free_urb(req->urb);
  302. list_del(q->next);
  303. }
  304. return NULL;
  305. }
  306. static void brcmf_usb_free_q(struct list_head *q, bool pending)
  307. {
  308. struct brcmf_usbreq *req, *next;
  309. int i = 0;
  310. list_for_each_entry_safe(req, next, q, list) {
  311. if (!req->urb) {
  312. brcmf_err("bad req\n");
  313. break;
  314. }
  315. i++;
  316. if (pending) {
  317. usb_kill_urb(req->urb);
  318. } else {
  319. usb_free_urb(req->urb);
  320. list_del_init(&req->list);
  321. }
  322. }
  323. }
  324. static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
  325. struct brcmf_usbreq *req)
  326. {
  327. unsigned long flags;
  328. spin_lock_irqsave(&devinfo->qlock, flags);
  329. list_del_init(&req->list);
  330. spin_unlock_irqrestore(&devinfo->qlock, flags);
  331. }
  332. static void brcmf_usb_tx_complete(struct urb *urb)
  333. {
  334. struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
  335. struct brcmf_usbdev_info *devinfo = req->devinfo;
  336. unsigned long flags;
  337. brcmf_dbg(USB, "Enter, urb->status=%d, skb=%p\n", urb->status,
  338. req->skb);
  339. brcmf_usb_del_fromq(devinfo, req);
  340. brcmf_txcomplete(devinfo->dev, req->skb, urb->status == 0);
  341. req->skb = NULL;
  342. brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
  343. spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
  344. if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
  345. devinfo->tx_flowblock) {
  346. brcmf_txflowblock(devinfo->dev, false);
  347. devinfo->tx_flowblock = false;
  348. }
  349. spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
  350. }
  351. static void brcmf_usb_rx_complete(struct urb *urb)
  352. {
  353. struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
  354. struct brcmf_usbdev_info *devinfo = req->devinfo;
  355. struct sk_buff *skb;
  356. brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
  357. brcmf_usb_del_fromq(devinfo, req);
  358. skb = req->skb;
  359. req->skb = NULL;
  360. /* zero lenght packets indicate usb "failure". Do not refill */
  361. if (urb->status != 0 || !urb->actual_length) {
  362. brcmu_pkt_buf_free_skb(skb);
  363. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  364. return;
  365. }
  366. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) {
  367. skb_put(skb, urb->actual_length);
  368. brcmf_rx_frame(devinfo->dev, skb);
  369. brcmf_usb_rx_refill(devinfo, req);
  370. } else {
  371. brcmu_pkt_buf_free_skb(skb);
  372. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  373. }
  374. return;
  375. }
  376. static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
  377. struct brcmf_usbreq *req)
  378. {
  379. struct sk_buff *skb;
  380. int ret;
  381. if (!req || !devinfo)
  382. return;
  383. skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
  384. if (!skb) {
  385. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  386. return;
  387. }
  388. req->skb = skb;
  389. usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
  390. skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
  391. req);
  392. req->devinfo = devinfo;
  393. brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
  394. ret = usb_submit_urb(req->urb, GFP_ATOMIC);
  395. if (ret) {
  396. brcmf_usb_del_fromq(devinfo, req);
  397. brcmu_pkt_buf_free_skb(req->skb);
  398. req->skb = NULL;
  399. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  400. }
  401. return;
  402. }
  403. static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
  404. {
  405. struct brcmf_usbreq *req;
  406. if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
  407. brcmf_err("bus is not up=%d\n", devinfo->bus_pub.state);
  408. return;
  409. }
  410. while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
  411. brcmf_usb_rx_refill(devinfo, req);
  412. }
  413. static void
  414. brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
  415. {
  416. struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
  417. int old_state;
  418. brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n",
  419. devinfo->bus_pub.state, state);
  420. if (devinfo->bus_pub.state == state)
  421. return;
  422. old_state = devinfo->bus_pub.state;
  423. devinfo->bus_pub.state = state;
  424. /* update state of upper layer */
  425. if (state == BRCMFMAC_USB_STATE_DOWN) {
  426. brcmf_dbg(USB, "DBUS is down\n");
  427. brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_DOWN);
  428. } else if (state == BRCMFMAC_USB_STATE_UP) {
  429. brcmf_dbg(USB, "DBUS is up\n");
  430. brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_DATA);
  431. } else {
  432. brcmf_dbg(USB, "DBUS current state=%d\n", state);
  433. }
  434. }
  435. static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
  436. {
  437. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  438. struct brcmf_usbreq *req;
  439. int ret;
  440. unsigned long flags;
  441. brcmf_dbg(USB, "Enter, skb=%p\n", skb);
  442. if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
  443. ret = -EIO;
  444. goto fail;
  445. }
  446. req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
  447. &devinfo->tx_freecount);
  448. if (!req) {
  449. brcmf_err("no req to send\n");
  450. ret = -ENOMEM;
  451. goto fail;
  452. }
  453. req->skb = skb;
  454. req->devinfo = devinfo;
  455. usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
  456. skb->data, skb->len, brcmf_usb_tx_complete, req);
  457. req->urb->transfer_flags |= URB_ZERO_PACKET;
  458. brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
  459. ret = usb_submit_urb(req->urb, GFP_ATOMIC);
  460. if (ret) {
  461. brcmf_err("brcmf_usb_tx usb_submit_urb FAILED\n");
  462. brcmf_usb_del_fromq(devinfo, req);
  463. req->skb = NULL;
  464. brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
  465. &devinfo->tx_freecount);
  466. goto fail;
  467. }
  468. spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
  469. if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
  470. !devinfo->tx_flowblock) {
  471. brcmf_txflowblock(dev, true);
  472. devinfo->tx_flowblock = true;
  473. }
  474. spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
  475. return 0;
  476. fail:
  477. return ret;
  478. }
  479. static int brcmf_usb_up(struct device *dev)
  480. {
  481. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  482. brcmf_dbg(USB, "Enter\n");
  483. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP)
  484. return 0;
  485. /* Success, indicate devinfo is fully up */
  486. brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP);
  487. if (devinfo->ctl_urb) {
  488. devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
  489. devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
  490. /* CTL Write */
  491. devinfo->ctl_write.bRequestType =
  492. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  493. devinfo->ctl_write.bRequest = 0;
  494. devinfo->ctl_write.wValue = cpu_to_le16(0);
  495. devinfo->ctl_write.wIndex = cpu_to_le16(devinfo->ifnum);
  496. /* CTL Read */
  497. devinfo->ctl_read.bRequestType =
  498. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  499. devinfo->ctl_read.bRequest = 1;
  500. devinfo->ctl_read.wValue = cpu_to_le16(0);
  501. devinfo->ctl_read.wIndex = cpu_to_le16(devinfo->ifnum);
  502. }
  503. brcmf_usb_rx_fill_all(devinfo);
  504. return 0;
  505. }
  506. static void brcmf_usb_down(struct device *dev)
  507. {
  508. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  509. brcmf_dbg(USB, "Enter\n");
  510. if (devinfo == NULL)
  511. return;
  512. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN)
  513. return;
  514. brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN);
  515. if (devinfo->ctl_urb)
  516. usb_kill_urb(devinfo->ctl_urb);
  517. if (devinfo->bulk_urb)
  518. usb_kill_urb(devinfo->bulk_urb);
  519. brcmf_usb_free_q(&devinfo->tx_postq, true);
  520. brcmf_usb_free_q(&devinfo->rx_postq, true);
  521. }
  522. static void
  523. brcmf_usb_sync_complete(struct urb *urb)
  524. {
  525. struct brcmf_usbdev_info *devinfo =
  526. (struct brcmf_usbdev_info *)urb->context;
  527. devinfo->ctl_completed = true;
  528. brcmf_usb_ioctl_resp_wake(devinfo);
  529. }
  530. static int brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
  531. void *buffer, int buflen)
  532. {
  533. int ret;
  534. char *tmpbuf;
  535. u16 size;
  536. if ((!devinfo) || (devinfo->ctl_urb == NULL))
  537. return -EINVAL;
  538. tmpbuf = kmalloc(buflen, GFP_ATOMIC);
  539. if (!tmpbuf)
  540. return -ENOMEM;
  541. size = buflen;
  542. devinfo->ctl_urb->transfer_buffer_length = size;
  543. devinfo->ctl_read.wLength = cpu_to_le16p(&size);
  544. devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
  545. USB_RECIP_INTERFACE;
  546. devinfo->ctl_read.bRequest = cmd;
  547. usb_fill_control_urb(devinfo->ctl_urb,
  548. devinfo->usbdev,
  549. usb_rcvctrlpipe(devinfo->usbdev, 0),
  550. (unsigned char *) &devinfo->ctl_read,
  551. (void *) tmpbuf, size,
  552. (usb_complete_t)brcmf_usb_sync_complete, devinfo);
  553. devinfo->ctl_completed = false;
  554. ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
  555. if (ret < 0) {
  556. brcmf_err("usb_submit_urb failed %d\n", ret);
  557. goto finalize;
  558. }
  559. if (!brcmf_usb_ioctl_resp_wait(devinfo))
  560. ret = -ETIMEDOUT;
  561. else
  562. memcpy(buffer, tmpbuf, buflen);
  563. finalize:
  564. kfree(tmpbuf);
  565. return ret;
  566. }
  567. static bool
  568. brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
  569. {
  570. struct bootrom_id_le id;
  571. u32 chipid, chiprev;
  572. brcmf_dbg(USB, "Enter\n");
  573. if (devinfo == NULL)
  574. return false;
  575. /* Check if firmware downloaded already by querying runtime ID */
  576. id.chip = cpu_to_le32(0xDEAD);
  577. brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
  578. chipid = le32_to_cpu(id.chip);
  579. chiprev = le32_to_cpu(id.chiprev);
  580. if ((chipid & 0x4300) == 0x4300)
  581. brcmf_dbg(USB, "chip %x rev 0x%x\n", chipid, chiprev);
  582. else
  583. brcmf_dbg(USB, "chip %d rev 0x%x\n", chipid, chiprev);
  584. if (chipid == BRCMF_POSTBOOT_ID) {
  585. brcmf_dbg(USB, "firmware already downloaded\n");
  586. brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
  587. return false;
  588. } else {
  589. devinfo->bus_pub.devid = chipid;
  590. devinfo->bus_pub.chiprev = chiprev;
  591. }
  592. return true;
  593. }
  594. static int
  595. brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
  596. {
  597. struct bootrom_id_le id;
  598. u32 loop_cnt;
  599. int err;
  600. brcmf_dbg(USB, "Enter\n");
  601. loop_cnt = 0;
  602. do {
  603. mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT);
  604. loop_cnt++;
  605. id.chip = cpu_to_le32(0xDEAD); /* Get the ID */
  606. err = brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
  607. if ((err) && (err != -ETIMEDOUT))
  608. return err;
  609. if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
  610. break;
  611. } while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT);
  612. if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
  613. brcmf_dbg(USB, "postboot chip 0x%x/rev 0x%x\n",
  614. le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
  615. brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
  616. return 0;
  617. } else {
  618. brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n",
  619. BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt);
  620. return -EINVAL;
  621. }
  622. }
  623. static int
  624. brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
  625. {
  626. int ret;
  627. if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
  628. return -EINVAL;
  629. /* Prepare the URB */
  630. usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
  631. devinfo->tx_pipe, buffer, len,
  632. (usb_complete_t)brcmf_usb_sync_complete, devinfo);
  633. devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
  634. devinfo->ctl_completed = false;
  635. ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
  636. if (ret) {
  637. brcmf_err("usb_submit_urb failed %d\n", ret);
  638. return ret;
  639. }
  640. ret = brcmf_usb_ioctl_resp_wait(devinfo);
  641. return (ret == 0);
  642. }
  643. static int
  644. brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
  645. {
  646. unsigned int sendlen, sent, dllen;
  647. char *bulkchunk = NULL, *dlpos;
  648. struct rdl_state_le state;
  649. u32 rdlstate, rdlbytes;
  650. int err = 0;
  651. brcmf_dbg(USB, "Enter, fw %p, len %d\n", fw, fwlen);
  652. bulkchunk = kmalloc(RDL_CHUNK, GFP_ATOMIC);
  653. if (bulkchunk == NULL) {
  654. err = -ENOMEM;
  655. goto fail;
  656. }
  657. /* 1) Prepare USB boot loader for runtime image */
  658. brcmf_usb_dl_cmd(devinfo, DL_START, &state, sizeof(state));
  659. rdlstate = le32_to_cpu(state.state);
  660. rdlbytes = le32_to_cpu(state.bytes);
  661. /* 2) Check we are in the Waiting state */
  662. if (rdlstate != DL_WAITING) {
  663. brcmf_err("Failed to DL_START\n");
  664. err = -EINVAL;
  665. goto fail;
  666. }
  667. sent = 0;
  668. dlpos = fw;
  669. dllen = fwlen;
  670. /* Get chip id and rev */
  671. while (rdlbytes != dllen) {
  672. /* Wait until the usb device reports it received all
  673. * the bytes we sent */
  674. if ((rdlbytes == sent) && (rdlbytes != dllen)) {
  675. if ((dllen-sent) < RDL_CHUNK)
  676. sendlen = dllen-sent;
  677. else
  678. sendlen = RDL_CHUNK;
  679. /* simply avoid having to send a ZLP by ensuring we
  680. * never have an even
  681. * multiple of 64
  682. */
  683. if (!(sendlen % 64))
  684. sendlen -= 4;
  685. /* send data */
  686. memcpy(bulkchunk, dlpos, sendlen);
  687. if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
  688. sendlen)) {
  689. brcmf_err("send_bulk failed\n");
  690. err = -EINVAL;
  691. goto fail;
  692. }
  693. dlpos += sendlen;
  694. sent += sendlen;
  695. }
  696. err = brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
  697. sizeof(state));
  698. if (err) {
  699. brcmf_err("DL_GETSTATE Failed\n");
  700. goto fail;
  701. }
  702. rdlstate = le32_to_cpu(state.state);
  703. rdlbytes = le32_to_cpu(state.bytes);
  704. /* restart if an error is reported */
  705. if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
  706. brcmf_err("Bad Hdr or Bad CRC state %d\n",
  707. rdlstate);
  708. err = -EINVAL;
  709. goto fail;
  710. }
  711. }
  712. fail:
  713. kfree(bulkchunk);
  714. brcmf_dbg(USB, "Exit, err=%d\n", err);
  715. return err;
  716. }
  717. static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
  718. {
  719. int err;
  720. brcmf_dbg(USB, "Enter\n");
  721. if (devinfo == NULL)
  722. return -EINVAL;
  723. if (devinfo->bus_pub.devid == 0xDEAD)
  724. return -EINVAL;
  725. err = brcmf_usb_dl_writeimage(devinfo, fw, len);
  726. if (err == 0)
  727. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE;
  728. else
  729. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL;
  730. brcmf_dbg(USB, "Exit, err=%d\n", err);
  731. return err;
  732. }
  733. static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
  734. {
  735. struct rdl_state_le state;
  736. brcmf_dbg(USB, "Enter\n");
  737. if (!devinfo)
  738. return -EINVAL;
  739. if (devinfo->bus_pub.devid == 0xDEAD)
  740. return -EINVAL;
  741. /* Check we are runnable */
  742. state.state = 0;
  743. brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state, sizeof(state));
  744. /* Start the image */
  745. if (state.state == cpu_to_le32(DL_RUNNABLE)) {
  746. if (brcmf_usb_dl_cmd(devinfo, DL_GO, &state, sizeof(state)))
  747. return -ENODEV;
  748. if (brcmf_usb_resetcfg(devinfo))
  749. return -ENODEV;
  750. /* The Dongle may go for re-enumeration. */
  751. } else {
  752. brcmf_err("Dongle not runnable\n");
  753. return -EINVAL;
  754. }
  755. brcmf_dbg(USB, "Exit\n");
  756. return 0;
  757. }
  758. static bool brcmf_usb_chip_support(int chipid, int chiprev)
  759. {
  760. switch(chipid) {
  761. case BRCM_CC_43143_CHIP_ID:
  762. return true;
  763. case BRCM_CC_43235_CHIP_ID:
  764. case BRCM_CC_43236_CHIP_ID:
  765. case BRCM_CC_43238_CHIP_ID:
  766. return (chiprev == 3);
  767. case BRCM_CC_43242_CHIP_ID:
  768. return true;
  769. case BRCM_CC_43566_CHIP_ID:
  770. case BRCM_CC_43569_CHIP_ID:
  771. return true;
  772. default:
  773. break;
  774. }
  775. return false;
  776. }
  777. static int
  778. brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
  779. {
  780. int devid, chiprev;
  781. int err;
  782. brcmf_dbg(USB, "Enter\n");
  783. if (devinfo == NULL)
  784. return -ENODEV;
  785. devid = devinfo->bus_pub.devid;
  786. chiprev = devinfo->bus_pub.chiprev;
  787. if (!brcmf_usb_chip_support(devid, chiprev)) {
  788. brcmf_err("unsupported chip %d rev %d\n",
  789. devid, chiprev);
  790. return -EINVAL;
  791. }
  792. if (!devinfo->image) {
  793. brcmf_err("No firmware!\n");
  794. return -ENOENT;
  795. }
  796. err = brcmf_usb_dlstart(devinfo,
  797. (u8 *)devinfo->image, devinfo->image_len);
  798. if (err == 0)
  799. err = brcmf_usb_dlrun(devinfo);
  800. return err;
  801. }
  802. static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
  803. {
  804. brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo);
  805. /* free the URBS */
  806. brcmf_usb_free_q(&devinfo->rx_freeq, false);
  807. brcmf_usb_free_q(&devinfo->tx_freeq, false);
  808. usb_free_urb(devinfo->ctl_urb);
  809. usb_free_urb(devinfo->bulk_urb);
  810. kfree(devinfo->tx_reqs);
  811. kfree(devinfo->rx_reqs);
  812. }
  813. #define TRX_MAGIC 0x30524448 /* "HDR0" */
  814. #define TRX_VERSION 1 /* Version 1 */
  815. #define TRX_MAX_LEN 0x3B0000 /* Max length */
  816. #define TRX_NO_HEADER 1 /* Do not write TRX header */
  817. #define TRX_MAX_OFFSET 3 /* Max number of individual files */
  818. #define TRX_UNCOMP_IMAGE 0x20 /* Trx contains uncompressed image */
  819. struct trx_header_le {
  820. __le32 magic; /* "HDR0" */
  821. __le32 len; /* Length of file including header */
  822. __le32 crc32; /* CRC from flag_version to end of file */
  823. __le32 flag_version; /* 0:15 flags, 16:31 version */
  824. __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of
  825. * header */
  826. };
  827. static int check_file(const u8 *headers)
  828. {
  829. struct trx_header_le *trx;
  830. int actual_len = -1;
  831. brcmf_dbg(USB, "Enter\n");
  832. /* Extract trx header */
  833. trx = (struct trx_header_le *) headers;
  834. if (trx->magic != cpu_to_le32(TRX_MAGIC))
  835. return -1;
  836. headers += sizeof(struct trx_header_le);
  837. if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
  838. actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
  839. return actual_len + sizeof(struct trx_header_le);
  840. }
  841. return -1;
  842. }
  843. static const char *brcmf_usb_get_fwname(struct brcmf_usbdev_info *devinfo)
  844. {
  845. switch (devinfo->bus_pub.devid) {
  846. case BRCM_CC_43143_CHIP_ID:
  847. return BRCMF_USB_43143_FW_NAME;
  848. case BRCM_CC_43235_CHIP_ID:
  849. case BRCM_CC_43236_CHIP_ID:
  850. case BRCM_CC_43238_CHIP_ID:
  851. return BRCMF_USB_43236_FW_NAME;
  852. case BRCM_CC_43242_CHIP_ID:
  853. return BRCMF_USB_43242_FW_NAME;
  854. case BRCM_CC_43566_CHIP_ID:
  855. case BRCM_CC_43569_CHIP_ID:
  856. return BRCMF_USB_43569_FW_NAME;
  857. default:
  858. return NULL;
  859. }
  860. }
  861. static
  862. struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
  863. int nrxq, int ntxq)
  864. {
  865. brcmf_dbg(USB, "Enter\n");
  866. devinfo->bus_pub.nrxq = nrxq;
  867. devinfo->rx_low_watermark = nrxq / 2;
  868. devinfo->bus_pub.devinfo = devinfo;
  869. devinfo->bus_pub.ntxq = ntxq;
  870. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DOWN;
  871. /* flow control when too many tx urbs posted */
  872. devinfo->tx_low_watermark = ntxq / 4;
  873. devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3;
  874. devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE;
  875. /* Initialize other structure content */
  876. init_waitqueue_head(&devinfo->ioctl_resp_wait);
  877. /* Initialize the spinlocks */
  878. spin_lock_init(&devinfo->qlock);
  879. spin_lock_init(&devinfo->tx_flowblock_lock);
  880. INIT_LIST_HEAD(&devinfo->rx_freeq);
  881. INIT_LIST_HEAD(&devinfo->rx_postq);
  882. INIT_LIST_HEAD(&devinfo->tx_freeq);
  883. INIT_LIST_HEAD(&devinfo->tx_postq);
  884. devinfo->tx_flowblock = false;
  885. devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
  886. if (!devinfo->rx_reqs)
  887. goto error;
  888. devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
  889. if (!devinfo->tx_reqs)
  890. goto error;
  891. devinfo->tx_freecount = ntxq;
  892. devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
  893. if (!devinfo->ctl_urb) {
  894. brcmf_err("usb_alloc_urb (ctl) failed\n");
  895. goto error;
  896. }
  897. devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
  898. if (!devinfo->bulk_urb) {
  899. brcmf_err("usb_alloc_urb (bulk) failed\n");
  900. goto error;
  901. }
  902. return &devinfo->bus_pub;
  903. error:
  904. brcmf_err("failed!\n");
  905. brcmf_usb_detach(devinfo);
  906. return NULL;
  907. }
  908. static struct brcmf_bus_ops brcmf_usb_bus_ops = {
  909. .txdata = brcmf_usb_tx,
  910. .stop = brcmf_usb_down,
  911. .txctl = brcmf_usb_tx_ctlpkt,
  912. .rxctl = brcmf_usb_rx_ctlpkt,
  913. };
  914. static int brcmf_usb_bus_setup(struct brcmf_usbdev_info *devinfo)
  915. {
  916. int ret;
  917. /* Attach to the common driver interface */
  918. ret = brcmf_attach(devinfo->dev);
  919. if (ret) {
  920. brcmf_err("brcmf_attach failed\n");
  921. return ret;
  922. }
  923. ret = brcmf_usb_up(devinfo->dev);
  924. if (ret)
  925. goto fail;
  926. ret = brcmf_bus_start(devinfo->dev);
  927. if (ret)
  928. goto fail;
  929. return 0;
  930. fail:
  931. brcmf_detach(devinfo->dev);
  932. return ret;
  933. }
  934. static void brcmf_usb_probe_phase2(struct device *dev,
  935. const struct firmware *fw,
  936. void *nvram, u32 nvlen)
  937. {
  938. struct brcmf_bus *bus = dev_get_drvdata(dev);
  939. struct brcmf_usbdev_info *devinfo;
  940. int ret;
  941. brcmf_dbg(USB, "Start fw downloading\n");
  942. ret = check_file(fw->data);
  943. if (ret < 0) {
  944. brcmf_err("invalid firmware\n");
  945. release_firmware(fw);
  946. goto error;
  947. }
  948. devinfo = bus->bus_priv.usb->devinfo;
  949. devinfo->image = fw->data;
  950. devinfo->image_len = fw->size;
  951. ret = brcmf_usb_fw_download(devinfo);
  952. release_firmware(fw);
  953. if (ret)
  954. goto error;
  955. ret = brcmf_usb_bus_setup(devinfo);
  956. if (ret)
  957. goto error;
  958. return;
  959. error:
  960. brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), ret);
  961. device_release_driver(dev);
  962. }
  963. static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo)
  964. {
  965. struct brcmf_bus *bus = NULL;
  966. struct brcmf_usbdev *bus_pub = NULL;
  967. struct device *dev = devinfo->dev;
  968. int ret;
  969. brcmf_dbg(USB, "Enter\n");
  970. bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
  971. if (!bus_pub)
  972. return -ENODEV;
  973. bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
  974. if (!bus) {
  975. ret = -ENOMEM;
  976. goto fail;
  977. }
  978. bus->dev = dev;
  979. bus_pub->bus = bus;
  980. bus->bus_priv.usb = bus_pub;
  981. dev_set_drvdata(dev, bus);
  982. bus->ops = &brcmf_usb_bus_ops;
  983. bus->proto_type = BRCMF_PROTO_BCDC;
  984. bus->always_use_fws_queue = true;
  985. if (!brcmf_usb_dlneeded(devinfo)) {
  986. ret = brcmf_usb_bus_setup(devinfo);
  987. if (ret)
  988. goto fail;
  989. }
  990. bus->chip = bus_pub->devid;
  991. bus->chiprev = bus_pub->chiprev;
  992. /* request firmware here */
  993. brcmf_fw_get_firmwares(dev, 0, brcmf_usb_get_fwname(devinfo), NULL,
  994. brcmf_usb_probe_phase2);
  995. return 0;
  996. fail:
  997. /* Release resources in reverse order */
  998. kfree(bus);
  999. brcmf_usb_detach(devinfo);
  1000. return ret;
  1001. }
  1002. static void
  1003. brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
  1004. {
  1005. if (!devinfo)
  1006. return;
  1007. brcmf_dbg(USB, "Enter, bus_pub %p\n", devinfo);
  1008. brcmf_detach(devinfo->dev);
  1009. kfree(devinfo->bus_pub.bus);
  1010. brcmf_usb_detach(devinfo);
  1011. }
  1012. static int
  1013. brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1014. {
  1015. struct usb_device *usb = interface_to_usbdev(intf);
  1016. struct brcmf_usbdev_info *devinfo;
  1017. struct usb_interface_descriptor *desc;
  1018. struct usb_endpoint_descriptor *endpoint;
  1019. int ret = 0;
  1020. u32 num_of_eps;
  1021. u8 endpoint_num, ep;
  1022. brcmf_dbg(USB, "Enter 0x%04x:0x%04x\n", id->idVendor, id->idProduct);
  1023. devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
  1024. if (devinfo == NULL)
  1025. return -ENOMEM;
  1026. devinfo->usbdev = usb;
  1027. devinfo->dev = &usb->dev;
  1028. usb_set_intfdata(intf, devinfo);
  1029. /* Check that the device supports only one configuration */
  1030. if (usb->descriptor.bNumConfigurations != 1) {
  1031. brcmf_err("Number of configurations: %d not supported\n",
  1032. usb->descriptor.bNumConfigurations);
  1033. ret = -ENODEV;
  1034. goto fail;
  1035. }
  1036. if ((usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) &&
  1037. (usb->descriptor.bDeviceClass != USB_CLASS_MISC) &&
  1038. (usb->descriptor.bDeviceClass != USB_CLASS_WIRELESS_CONTROLLER)) {
  1039. brcmf_err("Device class: 0x%x not supported\n",
  1040. usb->descriptor.bDeviceClass);
  1041. ret = -ENODEV;
  1042. goto fail;
  1043. }
  1044. desc = &intf->altsetting[0].desc;
  1045. if ((desc->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  1046. (desc->bInterfaceSubClass != 2) ||
  1047. (desc->bInterfaceProtocol != 0xff)) {
  1048. brcmf_err("non WLAN interface %d: 0x%x:0x%x:0x%x\n",
  1049. desc->bInterfaceNumber, desc->bInterfaceClass,
  1050. desc->bInterfaceSubClass, desc->bInterfaceProtocol);
  1051. ret = -ENODEV;
  1052. goto fail;
  1053. }
  1054. num_of_eps = desc->bNumEndpoints;
  1055. for (ep = 0; ep < num_of_eps; ep++) {
  1056. endpoint = &intf->altsetting[0].endpoint[ep].desc;
  1057. endpoint_num = usb_endpoint_num(endpoint);
  1058. if (!usb_endpoint_xfer_bulk(endpoint))
  1059. continue;
  1060. if (usb_endpoint_dir_in(endpoint)) {
  1061. if (!devinfo->rx_pipe)
  1062. devinfo->rx_pipe =
  1063. usb_rcvbulkpipe(usb, endpoint_num);
  1064. } else {
  1065. if (!devinfo->tx_pipe)
  1066. devinfo->tx_pipe =
  1067. usb_sndbulkpipe(usb, endpoint_num);
  1068. }
  1069. }
  1070. if (devinfo->rx_pipe == 0) {
  1071. brcmf_err("No RX (in) Bulk EP found\n");
  1072. ret = -ENODEV;
  1073. goto fail;
  1074. }
  1075. if (devinfo->tx_pipe == 0) {
  1076. brcmf_err("No TX (out) Bulk EP found\n");
  1077. ret = -ENODEV;
  1078. goto fail;
  1079. }
  1080. devinfo->ifnum = desc->bInterfaceNumber;
  1081. if (usb->speed == USB_SPEED_SUPER)
  1082. brcmf_dbg(USB, "Broadcom super speed USB WLAN interface detected\n");
  1083. else if (usb->speed == USB_SPEED_HIGH)
  1084. brcmf_dbg(USB, "Broadcom high speed USB WLAN interface detected\n");
  1085. else
  1086. brcmf_dbg(USB, "Broadcom full speed USB WLAN interface detected\n");
  1087. ret = brcmf_usb_probe_cb(devinfo);
  1088. if (ret)
  1089. goto fail;
  1090. /* Success */
  1091. return 0;
  1092. fail:
  1093. kfree(devinfo);
  1094. usb_set_intfdata(intf, NULL);
  1095. return ret;
  1096. }
  1097. static void
  1098. brcmf_usb_disconnect(struct usb_interface *intf)
  1099. {
  1100. struct brcmf_usbdev_info *devinfo;
  1101. brcmf_dbg(USB, "Enter\n");
  1102. devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
  1103. brcmf_usb_disconnect_cb(devinfo);
  1104. kfree(devinfo);
  1105. brcmf_dbg(USB, "Exit\n");
  1106. }
  1107. /*
  1108. * only need to signal the bus being down and update the state.
  1109. */
  1110. static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
  1111. {
  1112. struct usb_device *usb = interface_to_usbdev(intf);
  1113. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
  1114. brcmf_dbg(USB, "Enter\n");
  1115. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_SLEEP;
  1116. brcmf_detach(&usb->dev);
  1117. return 0;
  1118. }
  1119. /*
  1120. * (re-) start the bus.
  1121. */
  1122. static int brcmf_usb_resume(struct usb_interface *intf)
  1123. {
  1124. struct usb_device *usb = interface_to_usbdev(intf);
  1125. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
  1126. brcmf_dbg(USB, "Enter\n");
  1127. return brcmf_usb_bus_setup(devinfo);
  1128. }
  1129. static int brcmf_usb_reset_resume(struct usb_interface *intf)
  1130. {
  1131. struct usb_device *usb = interface_to_usbdev(intf);
  1132. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
  1133. brcmf_dbg(USB, "Enter\n");
  1134. return brcmf_fw_get_firmwares(&usb->dev, 0,
  1135. brcmf_usb_get_fwname(devinfo), NULL,
  1136. brcmf_usb_probe_phase2);
  1137. }
  1138. #define BRCMF_USB_DEVICE(dev_id) \
  1139. { USB_DEVICE(BRCM_USB_VENDOR_ID_BROADCOM, dev_id) }
  1140. static struct usb_device_id brcmf_usb_devid_table[] = {
  1141. BRCMF_USB_DEVICE(BRCM_USB_43143_DEVICE_ID),
  1142. BRCMF_USB_DEVICE(BRCM_USB_43236_DEVICE_ID),
  1143. BRCMF_USB_DEVICE(BRCM_USB_43242_DEVICE_ID),
  1144. BRCMF_USB_DEVICE(BRCM_USB_43569_DEVICE_ID),
  1145. /* special entry for device with firmware loaded and running */
  1146. BRCMF_USB_DEVICE(BRCM_USB_BCMFW_DEVICE_ID),
  1147. { /* end: all zeroes */ }
  1148. };
  1149. MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
  1150. MODULE_FIRMWARE(BRCMF_USB_43143_FW_NAME);
  1151. MODULE_FIRMWARE(BRCMF_USB_43236_FW_NAME);
  1152. MODULE_FIRMWARE(BRCMF_USB_43242_FW_NAME);
  1153. MODULE_FIRMWARE(BRCMF_USB_43569_FW_NAME);
  1154. static struct usb_driver brcmf_usbdrvr = {
  1155. .name = KBUILD_MODNAME,
  1156. .probe = brcmf_usb_probe,
  1157. .disconnect = brcmf_usb_disconnect,
  1158. .id_table = brcmf_usb_devid_table,
  1159. .suspend = brcmf_usb_suspend,
  1160. .resume = brcmf_usb_resume,
  1161. .reset_resume = brcmf_usb_reset_resume,
  1162. .supports_autosuspend = 1,
  1163. .disable_hub_initiated_lpm = 1,
  1164. };
  1165. static int brcmf_usb_reset_device(struct device *dev, void *notused)
  1166. {
  1167. /* device past is the usb interface so we
  1168. * need to use parent here.
  1169. */
  1170. brcmf_dev_reset(dev->parent);
  1171. return 0;
  1172. }
  1173. void brcmf_usb_exit(void)
  1174. {
  1175. struct device_driver *drv = &brcmf_usbdrvr.drvwrap.driver;
  1176. int ret;
  1177. brcmf_dbg(USB, "Enter\n");
  1178. ret = driver_for_each_device(drv, NULL, NULL,
  1179. brcmf_usb_reset_device);
  1180. usb_deregister(&brcmf_usbdrvr);
  1181. }
  1182. void brcmf_usb_register(void)
  1183. {
  1184. brcmf_dbg(USB, "Enter\n");
  1185. usb_register(&brcmf_usbdrvr);
  1186. }