usb.c 37 KB

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