usb.c 39 KB

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