usb.c 38 KB

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