usb.c 37 KB

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