usb.c 40 KB

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