usb.c 37 KB

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