orinoco_usb.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  1. /*
  2. * USB Orinoco driver
  3. *
  4. * Copyright (c) 2003 Manuel Estrada Sainz
  5. *
  6. * The contents of this file are subject to the Mozilla Public License
  7. * Version 1.1 (the "License"); you may not use this file except in
  8. * compliance with the License. You may obtain a copy of the License
  9. * at http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS"
  12. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  13. * the License for the specific language governing rights and
  14. * limitations under the License.
  15. *
  16. * Alternatively, the contents of this file may be used under the
  17. * terms of the GNU General Public License version 2 (the "GPL"), in
  18. * which case the provisions of the GPL are applicable instead of the
  19. * above. If you wish to allow the use of your version of this file
  20. * only under the terms of the GPL and not to allow others to use your
  21. * version of this file under the MPL, indicate your decision by
  22. * deleting the provisions above and replace them with the notice and
  23. * other provisions required by the GPL. If you do not delete the
  24. * provisions above, a recipient may use your version of this file
  25. * under either the MPL or the GPL.
  26. *
  27. * Queueing code based on linux-wlan-ng 0.2.1-pre5
  28. *
  29. * Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved.
  30. *
  31. * The license is the same as above.
  32. *
  33. * Initialy based on USB Skeleton driver - 0.7
  34. *
  35. * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
  36. *
  37. * This program is free software; you can redistribute it and/or
  38. * modify it under the terms of the GNU General Public License as
  39. * published by the Free Software Foundation; either version 2 of
  40. * the License, or (at your option) any later version.
  41. *
  42. * NOTE: The original USB Skeleton driver is GPL, but all that code is
  43. * gone so MPL/GPL applies.
  44. */
  45. #define DRIVER_NAME "orinoco_usb"
  46. #define PFX DRIVER_NAME ": "
  47. #include <linux/module.h>
  48. #include <linux/kernel.h>
  49. #include <linux/sched.h>
  50. #include <linux/signal.h>
  51. #include <linux/errno.h>
  52. #include <linux/poll.h>
  53. #include <linux/slab.h>
  54. #include <linux/fcntl.h>
  55. #include <linux/spinlock.h>
  56. #include <linux/list.h>
  57. #include <linux/usb.h>
  58. #include <linux/timer.h>
  59. #include <linux/netdevice.h>
  60. #include <linux/if_arp.h>
  61. #include <linux/etherdevice.h>
  62. #include <linux/wireless.h>
  63. #include <linux/firmware.h>
  64. #include <linux/refcount.h>
  65. #include "mic.h"
  66. #include "orinoco.h"
  67. #ifndef URB_ASYNC_UNLINK
  68. #define URB_ASYNC_UNLINK 0
  69. #endif
  70. /* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
  71. static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
  72. #define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
  73. struct header_struct {
  74. /* 802.3 */
  75. u8 dest[ETH_ALEN];
  76. u8 src[ETH_ALEN];
  77. __be16 len;
  78. /* 802.2 */
  79. u8 dsap;
  80. u8 ssap;
  81. u8 ctrl;
  82. /* SNAP */
  83. u8 oui[3];
  84. __be16 ethertype;
  85. } __packed;
  86. struct ez_usb_fw {
  87. u16 size;
  88. const u8 *code;
  89. };
  90. static struct ez_usb_fw firmware = {
  91. .size = 0,
  92. .code = NULL,
  93. };
  94. /* Debugging macros */
  95. #undef err
  96. #define err(format, arg...) \
  97. do { printk(KERN_ERR PFX format "\n", ## arg); } while (0)
  98. MODULE_FIRMWARE("orinoco_ezusb_fw");
  99. /*
  100. * Under some conditions, the card gets stuck and stops paying attention
  101. * to the world (i.e. data communication stalls) until we do something to
  102. * it. Sending an INQ_TALLIES command seems to be enough and should be
  103. * harmless otherwise. This behaviour has been observed when using the
  104. * driver on a systemimager client during installation. In the past a
  105. * timer was used to send INQ_TALLIES commands when there was no other
  106. * activity, but it was troublesome and was removed.
  107. */
  108. #define USB_COMPAQ_VENDOR_ID 0x049f /* Compaq Computer Corp. */
  109. #define USB_COMPAQ_WL215_ID 0x001f /* Compaq WL215 USB Adapter */
  110. #define USB_COMPAQ_W200_ID 0x0076 /* Compaq W200 USB Adapter */
  111. #define USB_HP_WL215_ID 0x0082 /* Compaq WL215 USB Adapter */
  112. #define USB_MELCO_VENDOR_ID 0x0411
  113. #define USB_BUFFALO_L11_ID 0x0006 /* BUFFALO WLI-USB-L11 */
  114. #define USB_BUFFALO_L11G_WR_ID 0x000B /* BUFFALO WLI-USB-L11G-WR */
  115. #define USB_BUFFALO_L11G_ID 0x000D /* BUFFALO WLI-USB-L11G */
  116. #define USB_LUCENT_VENDOR_ID 0x047E /* Lucent Technologies */
  117. #define USB_LUCENT_ORINOCO_ID 0x0300 /* Lucent/Agere Orinoco USB Client */
  118. #define USB_AVAYA8_VENDOR_ID 0x0D98
  119. #define USB_AVAYAE_VENDOR_ID 0x0D9E
  120. #define USB_AVAYA_WIRELESS_ID 0x0300 /* Avaya Wireless USB Card */
  121. #define USB_AGERE_VENDOR_ID 0x0D4E /* Agere Systems */
  122. #define USB_AGERE_MODEL0801_ID 0x1000 /* Wireless USB Card Model 0801 */
  123. #define USB_AGERE_MODEL0802_ID 0x1001 /* Wireless USB Card Model 0802 */
  124. #define USB_AGERE_REBRANDED_ID 0x047A /* WLAN USB Card */
  125. #define USB_ELSA_VENDOR_ID 0x05CC
  126. #define USB_ELSA_AIRLANCER_ID 0x3100 /* ELSA AirLancer USB-11 */
  127. #define USB_LEGEND_VENDOR_ID 0x0E7C
  128. #define USB_LEGEND_JOYNET_ID 0x0300 /* Joynet WLAN USB Card */
  129. #define USB_SAMSUNG_VENDOR_ID 0x04E8
  130. #define USB_SAMSUNG_SEW2001U1_ID 0x5002 /* Samsung SEW-2001u Card */
  131. #define USB_SAMSUNG_SEW2001U2_ID 0x5B11 /* Samsung SEW-2001u Card */
  132. #define USB_SAMSUNG_SEW2003U_ID 0x7011 /* Samsung SEW-2003U Card */
  133. #define USB_IGATE_VENDOR_ID 0x0681
  134. #define USB_IGATE_IGATE_11M_ID 0x0012 /* I-GATE 11M USB Card */
  135. #define USB_FUJITSU_VENDOR_ID 0x0BF8
  136. #define USB_FUJITSU_E1100_ID 0x1002 /* connect2AIR WLAN E-1100 USB */
  137. #define USB_2WIRE_VENDOR_ID 0x1630
  138. #define USB_2WIRE_WIRELESS_ID 0xff81 /* 2Wire Wireless USB adapter */
  139. #define EZUSB_REQUEST_FW_TRANS 0xA0
  140. #define EZUSB_REQUEST_TRIGER 0xAA
  141. #define EZUSB_REQUEST_TRIG_AC 0xAC
  142. #define EZUSB_CPUCS_REG 0x7F92
  143. #define EZUSB_RID_TX 0x0700
  144. #define EZUSB_RID_RX 0x0701
  145. #define EZUSB_RID_INIT1 0x0702
  146. #define EZUSB_RID_ACK 0x0710
  147. #define EZUSB_RID_READ_PDA 0x0800
  148. #define EZUSB_RID_PROG_INIT 0x0852
  149. #define EZUSB_RID_PROG_SET_ADDR 0x0853
  150. #define EZUSB_RID_PROG_BYTES 0x0854
  151. #define EZUSB_RID_PROG_END 0x0855
  152. #define EZUSB_RID_DOCMD 0x0860
  153. /* Recognize info frames */
  154. #define EZUSB_IS_INFO(id) ((id >= 0xF000) && (id <= 0xF2FF))
  155. #define EZUSB_MAGIC 0x0210
  156. #define EZUSB_FRAME_DATA 1
  157. #define EZUSB_FRAME_CONTROL 2
  158. #define DEF_TIMEOUT (3 * HZ)
  159. #define BULK_BUF_SIZE 2048
  160. #define MAX_DL_SIZE (BULK_BUF_SIZE - sizeof(struct ezusb_packet))
  161. #define FW_BUF_SIZE 64
  162. #define FW_VAR_OFFSET_PTR 0x359
  163. #define FW_VAR_VALUE 0
  164. #define FW_HOLE_START 0x100
  165. #define FW_HOLE_END 0x300
  166. struct ezusb_packet {
  167. __le16 magic; /* 0x0210 */
  168. u8 req_reply_count;
  169. u8 ans_reply_count;
  170. __le16 frame_type; /* 0x01 for data frames, 0x02 otherwise */
  171. __le16 size; /* transport size */
  172. __le16 crc; /* CRC up to here */
  173. __le16 hermes_len;
  174. __le16 hermes_rid;
  175. u8 data[0];
  176. } __packed;
  177. /* Table of devices that work or may work with this driver */
  178. static const struct usb_device_id ezusb_table[] = {
  179. {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_COMPAQ_WL215_ID)},
  180. {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_HP_WL215_ID)},
  181. {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_COMPAQ_W200_ID)},
  182. {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11_ID)},
  183. {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11G_WR_ID)},
  184. {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11G_ID)},
  185. {USB_DEVICE(USB_LUCENT_VENDOR_ID, USB_LUCENT_ORINOCO_ID)},
  186. {USB_DEVICE(USB_AVAYA8_VENDOR_ID, USB_AVAYA_WIRELESS_ID)},
  187. {USB_DEVICE(USB_AVAYAE_VENDOR_ID, USB_AVAYA_WIRELESS_ID)},
  188. {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_MODEL0801_ID)},
  189. {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_MODEL0802_ID)},
  190. {USB_DEVICE(USB_ELSA_VENDOR_ID, USB_ELSA_AIRLANCER_ID)},
  191. {USB_DEVICE(USB_LEGEND_VENDOR_ID, USB_LEGEND_JOYNET_ID)},
  192. {USB_DEVICE_VER(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2001U1_ID,
  193. 0, 0)},
  194. {USB_DEVICE(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2001U2_ID)},
  195. {USB_DEVICE(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2003U_ID)},
  196. {USB_DEVICE(USB_IGATE_VENDOR_ID, USB_IGATE_IGATE_11M_ID)},
  197. {USB_DEVICE(USB_FUJITSU_VENDOR_ID, USB_FUJITSU_E1100_ID)},
  198. {USB_DEVICE(USB_2WIRE_VENDOR_ID, USB_2WIRE_WIRELESS_ID)},
  199. {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_REBRANDED_ID)},
  200. {} /* Terminating entry */
  201. };
  202. MODULE_DEVICE_TABLE(usb, ezusb_table);
  203. /* Structure to hold all of our device specific stuff */
  204. struct ezusb_priv {
  205. struct usb_device *udev;
  206. struct net_device *dev;
  207. struct mutex mtx;
  208. spinlock_t req_lock;
  209. struct list_head req_pending;
  210. struct list_head req_active;
  211. spinlock_t reply_count_lock;
  212. u16 hermes_reg_fake[0x40];
  213. u8 *bap_buf;
  214. struct urb *read_urb;
  215. int read_pipe;
  216. int write_pipe;
  217. u8 reply_count;
  218. };
  219. enum ezusb_state {
  220. EZUSB_CTX_START,
  221. EZUSB_CTX_QUEUED,
  222. EZUSB_CTX_REQ_SUBMITTED,
  223. EZUSB_CTX_REQ_COMPLETE,
  224. EZUSB_CTX_RESP_RECEIVED,
  225. EZUSB_CTX_REQ_TIMEOUT,
  226. EZUSB_CTX_REQ_FAILED,
  227. EZUSB_CTX_RESP_TIMEOUT,
  228. EZUSB_CTX_REQSUBMIT_FAIL,
  229. EZUSB_CTX_COMPLETE,
  230. };
  231. struct request_context {
  232. struct list_head list;
  233. refcount_t refcount;
  234. struct completion done; /* Signals that CTX is dead */
  235. int killed;
  236. struct urb *outurb; /* OUT for req pkt */
  237. struct ezusb_priv *upriv;
  238. struct ezusb_packet *buf;
  239. int buf_length;
  240. struct timer_list timer; /* Timeout handling */
  241. enum ezusb_state state; /* Current state */
  242. /* the RID that we will wait for */
  243. u16 out_rid;
  244. u16 in_rid;
  245. };
  246. /* Forward declarations */
  247. static void ezusb_ctx_complete(struct request_context *ctx);
  248. static void ezusb_req_queue_run(struct ezusb_priv *upriv);
  249. static void ezusb_bulk_in_callback(struct urb *urb);
  250. static inline u8 ezusb_reply_inc(u8 count)
  251. {
  252. if (count < 0x7F)
  253. return count + 1;
  254. else
  255. return 1;
  256. }
  257. static void ezusb_request_context_put(struct request_context *ctx)
  258. {
  259. if (!refcount_dec_and_test(&ctx->refcount))
  260. return;
  261. WARN_ON(!ctx->done.done);
  262. BUG_ON(ctx->outurb->status == -EINPROGRESS);
  263. BUG_ON(timer_pending(&ctx->timer));
  264. usb_free_urb(ctx->outurb);
  265. kfree(ctx->buf);
  266. kfree(ctx);
  267. }
  268. static inline void ezusb_mod_timer(struct ezusb_priv *upriv,
  269. struct timer_list *timer,
  270. unsigned long expire)
  271. {
  272. if (!upriv->udev)
  273. return;
  274. mod_timer(timer, expire);
  275. }
  276. static void ezusb_request_timerfn(struct timer_list *t)
  277. {
  278. struct request_context *ctx = from_timer(ctx, t, timer);
  279. ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK;
  280. if (usb_unlink_urb(ctx->outurb) == -EINPROGRESS) {
  281. ctx->state = EZUSB_CTX_REQ_TIMEOUT;
  282. } else {
  283. ctx->state = EZUSB_CTX_RESP_TIMEOUT;
  284. dev_dbg(&ctx->outurb->dev->dev, "couldn't unlink\n");
  285. refcount_inc(&ctx->refcount);
  286. ctx->killed = 1;
  287. ezusb_ctx_complete(ctx);
  288. ezusb_request_context_put(ctx);
  289. }
  290. };
  291. static struct request_context *ezusb_alloc_ctx(struct ezusb_priv *upriv,
  292. u16 out_rid, u16 in_rid)
  293. {
  294. struct request_context *ctx;
  295. ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
  296. if (!ctx)
  297. return NULL;
  298. ctx->buf = kmalloc(BULK_BUF_SIZE, GFP_ATOMIC);
  299. if (!ctx->buf) {
  300. kfree(ctx);
  301. return NULL;
  302. }
  303. ctx->outurb = usb_alloc_urb(0, GFP_ATOMIC);
  304. if (!ctx->outurb) {
  305. kfree(ctx->buf);
  306. kfree(ctx);
  307. return NULL;
  308. }
  309. ctx->upriv = upriv;
  310. ctx->state = EZUSB_CTX_START;
  311. ctx->out_rid = out_rid;
  312. ctx->in_rid = in_rid;
  313. refcount_set(&ctx->refcount, 1);
  314. init_completion(&ctx->done);
  315. timer_setup(&ctx->timer, ezusb_request_timerfn, 0);
  316. return ctx;
  317. }
  318. /* Hopefully the real complete_all will soon be exported, in the mean
  319. * while this should work. */
  320. static inline void ezusb_complete_all(struct completion *comp)
  321. {
  322. complete(comp);
  323. complete(comp);
  324. complete(comp);
  325. complete(comp);
  326. }
  327. static void ezusb_ctx_complete(struct request_context *ctx)
  328. {
  329. struct ezusb_priv *upriv = ctx->upriv;
  330. unsigned long flags;
  331. spin_lock_irqsave(&upriv->req_lock, flags);
  332. list_del_init(&ctx->list);
  333. if (upriv->udev) {
  334. spin_unlock_irqrestore(&upriv->req_lock, flags);
  335. ezusb_req_queue_run(upriv);
  336. spin_lock_irqsave(&upriv->req_lock, flags);
  337. }
  338. switch (ctx->state) {
  339. case EZUSB_CTX_COMPLETE:
  340. case EZUSB_CTX_REQSUBMIT_FAIL:
  341. case EZUSB_CTX_REQ_FAILED:
  342. case EZUSB_CTX_REQ_TIMEOUT:
  343. case EZUSB_CTX_RESP_TIMEOUT:
  344. spin_unlock_irqrestore(&upriv->req_lock, flags);
  345. if ((ctx->out_rid == EZUSB_RID_TX) && upriv->dev) {
  346. struct net_device *dev = upriv->dev;
  347. struct net_device_stats *stats = &dev->stats;
  348. if (ctx->state != EZUSB_CTX_COMPLETE)
  349. stats->tx_errors++;
  350. else
  351. stats->tx_packets++;
  352. netif_wake_queue(dev);
  353. }
  354. ezusb_complete_all(&ctx->done);
  355. ezusb_request_context_put(ctx);
  356. break;
  357. default:
  358. spin_unlock_irqrestore(&upriv->req_lock, flags);
  359. if (!upriv->udev) {
  360. /* This is normal, as all request contexts get flushed
  361. * when the device is disconnected */
  362. err("Called, CTX not terminating, but device gone");
  363. ezusb_complete_all(&ctx->done);
  364. ezusb_request_context_put(ctx);
  365. break;
  366. }
  367. err("Called, CTX not in terminating state.");
  368. /* Things are really bad if this happens. Just leak
  369. * the CTX because it may still be linked to the
  370. * queue or the OUT urb may still be active.
  371. * Just leaking at least prevents an Oops or Panic.
  372. */
  373. break;
  374. }
  375. }
  376. /**
  377. * ezusb_req_queue_run:
  378. * Description:
  379. * Note: Only one active CTX at any one time, because there's no
  380. * other (reliable) way to match the response URB to the correct
  381. * CTX.
  382. **/
  383. static void ezusb_req_queue_run(struct ezusb_priv *upriv)
  384. {
  385. unsigned long flags;
  386. struct request_context *ctx;
  387. int result;
  388. spin_lock_irqsave(&upriv->req_lock, flags);
  389. if (!list_empty(&upriv->req_active))
  390. goto unlock;
  391. if (list_empty(&upriv->req_pending))
  392. goto unlock;
  393. ctx =
  394. list_entry(upriv->req_pending.next, struct request_context,
  395. list);
  396. if (!ctx->upriv->udev)
  397. goto unlock;
  398. /* We need to split this off to avoid a race condition */
  399. list_move_tail(&ctx->list, &upriv->req_active);
  400. if (ctx->state == EZUSB_CTX_QUEUED) {
  401. refcount_inc(&ctx->refcount);
  402. result = usb_submit_urb(ctx->outurb, GFP_ATOMIC);
  403. if (result) {
  404. ctx->state = EZUSB_CTX_REQSUBMIT_FAIL;
  405. spin_unlock_irqrestore(&upriv->req_lock, flags);
  406. err("Fatal, failed to submit command urb."
  407. " error=%d\n", result);
  408. ezusb_ctx_complete(ctx);
  409. ezusb_request_context_put(ctx);
  410. goto done;
  411. }
  412. ctx->state = EZUSB_CTX_REQ_SUBMITTED;
  413. ezusb_mod_timer(ctx->upriv, &ctx->timer,
  414. jiffies + DEF_TIMEOUT);
  415. }
  416. unlock:
  417. spin_unlock_irqrestore(&upriv->req_lock, flags);
  418. done:
  419. return;
  420. }
  421. static void ezusb_req_enqueue_run(struct ezusb_priv *upriv,
  422. struct request_context *ctx)
  423. {
  424. unsigned long flags;
  425. spin_lock_irqsave(&upriv->req_lock, flags);
  426. if (!ctx->upriv->udev) {
  427. spin_unlock_irqrestore(&upriv->req_lock, flags);
  428. goto done;
  429. }
  430. refcount_inc(&ctx->refcount);
  431. list_add_tail(&ctx->list, &upriv->req_pending);
  432. spin_unlock_irqrestore(&upriv->req_lock, flags);
  433. ctx->state = EZUSB_CTX_QUEUED;
  434. ezusb_req_queue_run(upriv);
  435. done:
  436. return;
  437. }
  438. static void ezusb_request_out_callback(struct urb *urb)
  439. {
  440. unsigned long flags;
  441. enum ezusb_state state;
  442. struct request_context *ctx = urb->context;
  443. struct ezusb_priv *upriv = ctx->upriv;
  444. spin_lock_irqsave(&upriv->req_lock, flags);
  445. del_timer(&ctx->timer);
  446. if (ctx->killed) {
  447. spin_unlock_irqrestore(&upriv->req_lock, flags);
  448. pr_warn("interrupt called with dead ctx\n");
  449. goto out;
  450. }
  451. state = ctx->state;
  452. if (urb->status == 0) {
  453. switch (state) {
  454. case EZUSB_CTX_REQ_SUBMITTED:
  455. if (ctx->in_rid) {
  456. ctx->state = EZUSB_CTX_REQ_COMPLETE;
  457. /* reply URB still pending */
  458. ezusb_mod_timer(upriv, &ctx->timer,
  459. jiffies + DEF_TIMEOUT);
  460. spin_unlock_irqrestore(&upriv->req_lock,
  461. flags);
  462. break;
  463. }
  464. /* fall through */
  465. case EZUSB_CTX_RESP_RECEIVED:
  466. /* IN already received before this OUT-ACK */
  467. ctx->state = EZUSB_CTX_COMPLETE;
  468. spin_unlock_irqrestore(&upriv->req_lock, flags);
  469. ezusb_ctx_complete(ctx);
  470. break;
  471. default:
  472. spin_unlock_irqrestore(&upriv->req_lock, flags);
  473. err("Unexpected state(0x%x, %d) in OUT URB",
  474. state, urb->status);
  475. break;
  476. }
  477. } else {
  478. /* If someone cancels the OUT URB then its status
  479. * should be either -ECONNRESET or -ENOENT.
  480. */
  481. switch (state) {
  482. case EZUSB_CTX_REQ_SUBMITTED:
  483. case EZUSB_CTX_RESP_RECEIVED:
  484. ctx->state = EZUSB_CTX_REQ_FAILED;
  485. /* fall through */
  486. case EZUSB_CTX_REQ_FAILED:
  487. case EZUSB_CTX_REQ_TIMEOUT:
  488. spin_unlock_irqrestore(&upriv->req_lock, flags);
  489. ezusb_ctx_complete(ctx);
  490. break;
  491. default:
  492. spin_unlock_irqrestore(&upriv->req_lock, flags);
  493. err("Unexpected state(0x%x, %d) in OUT URB",
  494. state, urb->status);
  495. break;
  496. }
  497. }
  498. out:
  499. ezusb_request_context_put(ctx);
  500. }
  501. static void ezusb_request_in_callback(struct ezusb_priv *upriv,
  502. struct urb *urb)
  503. {
  504. struct ezusb_packet *ans = urb->transfer_buffer;
  505. struct request_context *ctx = NULL;
  506. enum ezusb_state state;
  507. unsigned long flags;
  508. /* Find the CTX on the active queue that requested this URB */
  509. spin_lock_irqsave(&upriv->req_lock, flags);
  510. if (upriv->udev) {
  511. struct list_head *item;
  512. list_for_each(item, &upriv->req_active) {
  513. struct request_context *c;
  514. int reply_count;
  515. c = list_entry(item, struct request_context, list);
  516. reply_count =
  517. ezusb_reply_inc(c->buf->req_reply_count);
  518. if ((ans->ans_reply_count == reply_count)
  519. && (le16_to_cpu(ans->hermes_rid) == c->in_rid)) {
  520. ctx = c;
  521. break;
  522. }
  523. netdev_dbg(upriv->dev, "Skipped (0x%x/0x%x) (%d/%d)\n",
  524. le16_to_cpu(ans->hermes_rid), c->in_rid,
  525. ans->ans_reply_count, reply_count);
  526. }
  527. }
  528. if (ctx == NULL) {
  529. spin_unlock_irqrestore(&upriv->req_lock, flags);
  530. err("%s: got unexpected RID: 0x%04X", __func__,
  531. le16_to_cpu(ans->hermes_rid));
  532. ezusb_req_queue_run(upriv);
  533. return;
  534. }
  535. /* The data we want is in the in buffer, exchange */
  536. urb->transfer_buffer = ctx->buf;
  537. ctx->buf = (void *) ans;
  538. ctx->buf_length = urb->actual_length;
  539. state = ctx->state;
  540. switch (state) {
  541. case EZUSB_CTX_REQ_SUBMITTED:
  542. /* We have received our response URB before
  543. * our request has been acknowledged. Do NOT
  544. * destroy our CTX yet, because our OUT URB
  545. * is still alive ...
  546. */
  547. ctx->state = EZUSB_CTX_RESP_RECEIVED;
  548. spin_unlock_irqrestore(&upriv->req_lock, flags);
  549. /* Let the machine continue running. */
  550. break;
  551. case EZUSB_CTX_REQ_COMPLETE:
  552. /* This is the usual path: our request
  553. * has already been acknowledged, and
  554. * we have now received the reply.
  555. */
  556. ctx->state = EZUSB_CTX_COMPLETE;
  557. /* Stop the intimer */
  558. del_timer(&ctx->timer);
  559. spin_unlock_irqrestore(&upriv->req_lock, flags);
  560. /* Call the completion handler */
  561. ezusb_ctx_complete(ctx);
  562. break;
  563. default:
  564. spin_unlock_irqrestore(&upriv->req_lock, flags);
  565. pr_warn("Matched IN URB, unexpected context state(0x%x)\n",
  566. state);
  567. /* Throw this CTX away and try submitting another */
  568. del_timer(&ctx->timer);
  569. ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK;
  570. usb_unlink_urb(ctx->outurb);
  571. ezusb_req_queue_run(upriv);
  572. break;
  573. } /* switch */
  574. }
  575. static void ezusb_req_ctx_wait(struct ezusb_priv *upriv,
  576. struct request_context *ctx)
  577. {
  578. switch (ctx->state) {
  579. case EZUSB_CTX_QUEUED:
  580. case EZUSB_CTX_REQ_SUBMITTED:
  581. case EZUSB_CTX_REQ_COMPLETE:
  582. case EZUSB_CTX_RESP_RECEIVED:
  583. if (in_softirq()) {
  584. /* If we get called from a timer, timeout timers don't
  585. * get the chance to run themselves. So we make sure
  586. * that we don't sleep for ever */
  587. int msecs = DEF_TIMEOUT * (1000 / HZ);
  588. while (!ctx->done.done && msecs--)
  589. udelay(1000);
  590. } else {
  591. wait_event_interruptible(ctx->done.wait,
  592. ctx->done.done);
  593. }
  594. break;
  595. default:
  596. /* Done or failed - nothing to wait for */
  597. break;
  598. }
  599. }
  600. static inline u16 build_crc(struct ezusb_packet *data)
  601. {
  602. u16 crc = 0;
  603. u8 *bytes = (u8 *)data;
  604. int i;
  605. for (i = 0; i < 8; i++)
  606. crc = (crc << 1) + bytes[i];
  607. return crc;
  608. }
  609. /**
  610. * ezusb_fill_req:
  611. *
  612. * if data == NULL and length > 0 the data is assumed to be already in
  613. * the target buffer and only the header is filled.
  614. *
  615. */
  616. static int ezusb_fill_req(struct ezusb_packet *req, u16 length, u16 rid,
  617. const void *data, u16 frame_type, u8 reply_count)
  618. {
  619. int total_size = sizeof(*req) + length;
  620. BUG_ON(total_size > BULK_BUF_SIZE);
  621. req->magic = cpu_to_le16(EZUSB_MAGIC);
  622. req->req_reply_count = reply_count;
  623. req->ans_reply_count = 0;
  624. req->frame_type = cpu_to_le16(frame_type);
  625. req->size = cpu_to_le16(length + 4);
  626. req->crc = cpu_to_le16(build_crc(req));
  627. req->hermes_len = cpu_to_le16(HERMES_BYTES_TO_RECLEN(length));
  628. req->hermes_rid = cpu_to_le16(rid);
  629. if (data)
  630. memcpy(req->data, data, length);
  631. return total_size;
  632. }
  633. static int ezusb_submit_in_urb(struct ezusb_priv *upriv)
  634. {
  635. int retval = 0;
  636. void *cur_buf = upriv->read_urb->transfer_buffer;
  637. if (upriv->read_urb->status == -EINPROGRESS) {
  638. netdev_dbg(upriv->dev, "urb busy, not resubmiting\n");
  639. retval = -EBUSY;
  640. goto exit;
  641. }
  642. usb_fill_bulk_urb(upriv->read_urb, upriv->udev, upriv->read_pipe,
  643. cur_buf, BULK_BUF_SIZE,
  644. ezusb_bulk_in_callback, upriv);
  645. upriv->read_urb->transfer_flags = 0;
  646. retval = usb_submit_urb(upriv->read_urb, GFP_ATOMIC);
  647. if (retval)
  648. err("%s submit failed %d", __func__, retval);
  649. exit:
  650. return retval;
  651. }
  652. static inline int ezusb_8051_cpucs(struct ezusb_priv *upriv, int reset)
  653. {
  654. int ret;
  655. u8 *res_val = NULL;
  656. if (!upriv->udev) {
  657. err("%s: !upriv->udev", __func__);
  658. return -EFAULT;
  659. }
  660. res_val = kmalloc(sizeof(*res_val), GFP_KERNEL);
  661. if (!res_val)
  662. return -ENOMEM;
  663. *res_val = reset; /* avoid argument promotion */
  664. ret = usb_control_msg(upriv->udev,
  665. usb_sndctrlpipe(upriv->udev, 0),
  666. EZUSB_REQUEST_FW_TRANS,
  667. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  668. USB_DIR_OUT, EZUSB_CPUCS_REG, 0, res_val,
  669. sizeof(*res_val), DEF_TIMEOUT);
  670. kfree(res_val);
  671. return ret;
  672. }
  673. static int ezusb_firmware_download(struct ezusb_priv *upriv,
  674. struct ez_usb_fw *fw)
  675. {
  676. u8 *fw_buffer;
  677. int retval, addr;
  678. int variant_offset;
  679. fw_buffer = kmalloc(FW_BUF_SIZE, GFP_KERNEL);
  680. if (!fw_buffer) {
  681. printk(KERN_ERR PFX "Out of memory for firmware buffer.\n");
  682. return -ENOMEM;
  683. }
  684. /*
  685. * This byte is 1 and should be replaced with 0. The offset is
  686. * 0x10AD in version 0.0.6. The byte in question should follow
  687. * the end of the code pointed to by the jump in the beginning
  688. * of the firmware. Also, it is read by code located at 0x358.
  689. */
  690. variant_offset = be16_to_cpup((__be16 *) &fw->code[FW_VAR_OFFSET_PTR]);
  691. if (variant_offset >= fw->size) {
  692. printk(KERN_ERR PFX "Invalid firmware variant offset: "
  693. "0x%04x\n", variant_offset);
  694. retval = -EINVAL;
  695. goto fail;
  696. }
  697. retval = ezusb_8051_cpucs(upriv, 1);
  698. if (retval < 0)
  699. goto fail;
  700. for (addr = 0; addr < fw->size; addr += FW_BUF_SIZE) {
  701. /* 0x100-0x300 should be left alone, it contains card
  702. * specific data, like USB enumeration information */
  703. if ((addr >= FW_HOLE_START) && (addr < FW_HOLE_END))
  704. continue;
  705. memcpy(fw_buffer, &fw->code[addr], FW_BUF_SIZE);
  706. if (variant_offset >= addr &&
  707. variant_offset < addr + FW_BUF_SIZE) {
  708. netdev_dbg(upriv->dev,
  709. "Patching card_variant byte at 0x%04X\n",
  710. variant_offset);
  711. fw_buffer[variant_offset - addr] = FW_VAR_VALUE;
  712. }
  713. retval = usb_control_msg(upriv->udev,
  714. usb_sndctrlpipe(upriv->udev, 0),
  715. EZUSB_REQUEST_FW_TRANS,
  716. USB_TYPE_VENDOR | USB_RECIP_DEVICE
  717. | USB_DIR_OUT,
  718. addr, 0x0,
  719. fw_buffer, FW_BUF_SIZE,
  720. DEF_TIMEOUT);
  721. if (retval < 0)
  722. goto fail;
  723. }
  724. retval = ezusb_8051_cpucs(upriv, 0);
  725. if (retval < 0)
  726. goto fail;
  727. goto exit;
  728. fail:
  729. printk(KERN_ERR PFX "Firmware download failed, error %d\n",
  730. retval);
  731. exit:
  732. kfree(fw_buffer);
  733. return retval;
  734. }
  735. static int ezusb_access_ltv(struct ezusb_priv *upriv,
  736. struct request_context *ctx,
  737. u16 length, const void *data, u16 frame_type,
  738. void *ans_buff, unsigned ans_size, u16 *ans_length)
  739. {
  740. int req_size;
  741. int retval = 0;
  742. enum ezusb_state state;
  743. BUG_ON(in_irq());
  744. if (!upriv->udev) {
  745. retval = -ENODEV;
  746. goto exit;
  747. }
  748. if (upriv->read_urb->status != -EINPROGRESS)
  749. err("%s: in urb not pending", __func__);
  750. /* protect upriv->reply_count, guarantee sequential numbers */
  751. spin_lock_bh(&upriv->reply_count_lock);
  752. req_size = ezusb_fill_req(ctx->buf, length, ctx->out_rid, data,
  753. frame_type, upriv->reply_count);
  754. usb_fill_bulk_urb(ctx->outurb, upriv->udev, upriv->write_pipe,
  755. ctx->buf, req_size,
  756. ezusb_request_out_callback, ctx);
  757. if (ctx->in_rid)
  758. upriv->reply_count = ezusb_reply_inc(upriv->reply_count);
  759. ezusb_req_enqueue_run(upriv, ctx);
  760. spin_unlock_bh(&upriv->reply_count_lock);
  761. if (ctx->in_rid)
  762. ezusb_req_ctx_wait(upriv, ctx);
  763. state = ctx->state;
  764. switch (state) {
  765. case EZUSB_CTX_COMPLETE:
  766. retval = ctx->outurb->status;
  767. break;
  768. case EZUSB_CTX_QUEUED:
  769. case EZUSB_CTX_REQ_SUBMITTED:
  770. if (!ctx->in_rid)
  771. break;
  772. default:
  773. err("%s: Unexpected context state %d", __func__,
  774. state);
  775. /* fall though */
  776. case EZUSB_CTX_REQ_TIMEOUT:
  777. case EZUSB_CTX_REQ_FAILED:
  778. case EZUSB_CTX_RESP_TIMEOUT:
  779. case EZUSB_CTX_REQSUBMIT_FAIL:
  780. printk(KERN_ERR PFX "Access failed, resetting (state %d,"
  781. " reply_count %d)\n", state, upriv->reply_count);
  782. upriv->reply_count = 0;
  783. if (state == EZUSB_CTX_REQ_TIMEOUT
  784. || state == EZUSB_CTX_RESP_TIMEOUT) {
  785. printk(KERN_ERR PFX "ctx timed out\n");
  786. retval = -ETIMEDOUT;
  787. } else {
  788. printk(KERN_ERR PFX "ctx failed\n");
  789. retval = -EFAULT;
  790. }
  791. goto exit;
  792. }
  793. if (ctx->in_rid) {
  794. struct ezusb_packet *ans = ctx->buf;
  795. unsigned exp_len;
  796. if (ans->hermes_len != 0)
  797. exp_len = le16_to_cpu(ans->hermes_len) * 2 + 12;
  798. else
  799. exp_len = 14;
  800. if (exp_len != ctx->buf_length) {
  801. err("%s: length mismatch for RID 0x%04x: "
  802. "expected %d, got %d", __func__,
  803. ctx->in_rid, exp_len, ctx->buf_length);
  804. retval = -EIO;
  805. goto exit;
  806. }
  807. if (ans_buff)
  808. memcpy(ans_buff, ans->data, min(exp_len, ans_size));
  809. if (ans_length)
  810. *ans_length = le16_to_cpu(ans->hermes_len);
  811. }
  812. exit:
  813. ezusb_request_context_put(ctx);
  814. return retval;
  815. }
  816. static int ezusb_write_ltv(struct hermes *hw, int bap, u16 rid,
  817. u16 length, const void *data)
  818. {
  819. struct ezusb_priv *upriv = hw->priv;
  820. u16 frame_type;
  821. struct request_context *ctx;
  822. if (length == 0)
  823. return -EINVAL;
  824. length = HERMES_RECLEN_TO_BYTES(length);
  825. /* On memory mapped devices HERMES_RID_CNFGROUPADDRESSES can be
  826. * set to be empty, but the USB bridge doesn't like it */
  827. if (length == 0)
  828. return 0;
  829. ctx = ezusb_alloc_ctx(upriv, rid, EZUSB_RID_ACK);
  830. if (!ctx)
  831. return -ENOMEM;
  832. if (rid == EZUSB_RID_TX)
  833. frame_type = EZUSB_FRAME_DATA;
  834. else
  835. frame_type = EZUSB_FRAME_CONTROL;
  836. return ezusb_access_ltv(upriv, ctx, length, data, frame_type,
  837. NULL, 0, NULL);
  838. }
  839. static int ezusb_read_ltv(struct hermes *hw, int bap, u16 rid,
  840. unsigned bufsize, u16 *length, void *buf)
  841. {
  842. struct ezusb_priv *upriv = hw->priv;
  843. struct request_context *ctx;
  844. if (bufsize % 2)
  845. return -EINVAL;
  846. ctx = ezusb_alloc_ctx(upriv, rid, rid);
  847. if (!ctx)
  848. return -ENOMEM;
  849. return ezusb_access_ltv(upriv, ctx, 0, NULL, EZUSB_FRAME_CONTROL,
  850. buf, bufsize, length);
  851. }
  852. static int ezusb_doicmd_wait(struct hermes *hw, u16 cmd, u16 parm0, u16 parm1,
  853. u16 parm2, struct hermes_response *resp)
  854. {
  855. struct ezusb_priv *upriv = hw->priv;
  856. struct request_context *ctx;
  857. __le16 data[4] = {
  858. cpu_to_le16(cmd),
  859. cpu_to_le16(parm0),
  860. cpu_to_le16(parm1),
  861. cpu_to_le16(parm2),
  862. };
  863. netdev_dbg(upriv->dev,
  864. "0x%04X, parm0 0x%04X, parm1 0x%04X, parm2 0x%04X\n", cmd,
  865. parm0, parm1, parm2);
  866. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_DOCMD, EZUSB_RID_ACK);
  867. if (!ctx)
  868. return -ENOMEM;
  869. return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  870. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  871. }
  872. static int ezusb_docmd_wait(struct hermes *hw, u16 cmd, u16 parm0,
  873. struct hermes_response *resp)
  874. {
  875. struct ezusb_priv *upriv = hw->priv;
  876. struct request_context *ctx;
  877. __le16 data[4] = {
  878. cpu_to_le16(cmd),
  879. cpu_to_le16(parm0),
  880. 0,
  881. 0,
  882. };
  883. netdev_dbg(upriv->dev, "0x%04X, parm0 0x%04X\n", cmd, parm0);
  884. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_DOCMD, EZUSB_RID_ACK);
  885. if (!ctx)
  886. return -ENOMEM;
  887. return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  888. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  889. }
  890. static int ezusb_bap_pread(struct hermes *hw, int bap,
  891. void *buf, int len, u16 id, u16 offset)
  892. {
  893. struct ezusb_priv *upriv = hw->priv;
  894. struct ezusb_packet *ans = (void *) upriv->read_urb->transfer_buffer;
  895. int actual_length = upriv->read_urb->actual_length;
  896. if (id == EZUSB_RID_RX) {
  897. if ((sizeof(*ans) + offset + len) > actual_length) {
  898. printk(KERN_ERR PFX "BAP read beyond buffer end "
  899. "in rx frame\n");
  900. return -EINVAL;
  901. }
  902. memcpy(buf, ans->data + offset, len);
  903. return 0;
  904. }
  905. if (EZUSB_IS_INFO(id)) {
  906. /* Include 4 bytes for length/type */
  907. if ((sizeof(*ans) + offset + len - 4) > actual_length) {
  908. printk(KERN_ERR PFX "BAP read beyond buffer end "
  909. "in info frame\n");
  910. return -EFAULT;
  911. }
  912. memcpy(buf, ans->data + offset - 4, len);
  913. } else {
  914. printk(KERN_ERR PFX "Unexpected fid 0x%04x\n", id);
  915. return -EINVAL;
  916. }
  917. return 0;
  918. }
  919. static int ezusb_read_pda(struct hermes *hw, __le16 *pda,
  920. u32 pda_addr, u16 pda_len)
  921. {
  922. struct ezusb_priv *upriv = hw->priv;
  923. struct request_context *ctx;
  924. __le16 data[] = {
  925. cpu_to_le16(pda_addr & 0xffff),
  926. cpu_to_le16(pda_len - 4)
  927. };
  928. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_READ_PDA, EZUSB_RID_READ_PDA);
  929. if (!ctx)
  930. return -ENOMEM;
  931. /* wl_lkm does not include PDA size in the PDA area.
  932. * We will pad the information into pda, so other routines
  933. * don't have to be modified */
  934. pda[0] = cpu_to_le16(pda_len - 2);
  935. /* Includes CFG_PROD_DATA but not itself */
  936. pda[1] = cpu_to_le16(0x0800); /* CFG_PROD_DATA */
  937. return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  938. EZUSB_FRAME_CONTROL, &pda[2], pda_len - 4,
  939. NULL);
  940. }
  941. static int ezusb_program_init(struct hermes *hw, u32 entry_point)
  942. {
  943. struct ezusb_priv *upriv = hw->priv;
  944. struct request_context *ctx;
  945. __le32 data = cpu_to_le32(entry_point);
  946. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_INIT, EZUSB_RID_ACK);
  947. if (!ctx)
  948. return -ENOMEM;
  949. return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  950. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  951. }
  952. static int ezusb_program_end(struct hermes *hw)
  953. {
  954. struct ezusb_priv *upriv = hw->priv;
  955. struct request_context *ctx;
  956. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_END, EZUSB_RID_ACK);
  957. if (!ctx)
  958. return -ENOMEM;
  959. return ezusb_access_ltv(upriv, ctx, 0, NULL,
  960. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  961. }
  962. static int ezusb_program_bytes(struct hermes *hw, const char *buf,
  963. u32 addr, u32 len)
  964. {
  965. struct ezusb_priv *upriv = hw->priv;
  966. struct request_context *ctx;
  967. __le32 data = cpu_to_le32(addr);
  968. int err;
  969. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_SET_ADDR, EZUSB_RID_ACK);
  970. if (!ctx)
  971. return -ENOMEM;
  972. err = ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  973. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  974. if (err)
  975. return err;
  976. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_BYTES, EZUSB_RID_ACK);
  977. if (!ctx)
  978. return -ENOMEM;
  979. return ezusb_access_ltv(upriv, ctx, len, buf,
  980. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  981. }
  982. static int ezusb_program(struct hermes *hw, const char *buf,
  983. u32 addr, u32 len)
  984. {
  985. u32 ch_addr;
  986. u32 ch_len;
  987. int err = 0;
  988. /* We can only send 2048 bytes out of the bulk xmit at a time,
  989. * so we have to split any programming into chunks of <2048
  990. * bytes. */
  991. ch_len = (len < MAX_DL_SIZE) ? len : MAX_DL_SIZE;
  992. ch_addr = addr;
  993. while (ch_addr < (addr + len)) {
  994. pr_debug("Programming subblock of length %d "
  995. "to address 0x%08x. Data @ %p\n",
  996. ch_len, ch_addr, &buf[ch_addr - addr]);
  997. err = ezusb_program_bytes(hw, &buf[ch_addr - addr],
  998. ch_addr, ch_len);
  999. if (err)
  1000. break;
  1001. ch_addr += ch_len;
  1002. ch_len = ((addr + len - ch_addr) < MAX_DL_SIZE) ?
  1003. (addr + len - ch_addr) : MAX_DL_SIZE;
  1004. }
  1005. return err;
  1006. }
  1007. static netdev_tx_t ezusb_xmit(struct sk_buff *skb, struct net_device *dev)
  1008. {
  1009. struct orinoco_private *priv = ndev_priv(dev);
  1010. struct net_device_stats *stats = &dev->stats;
  1011. struct ezusb_priv *upriv = priv->card;
  1012. u8 mic[MICHAEL_MIC_LEN + 1];
  1013. int err = 0;
  1014. int tx_control;
  1015. unsigned long flags;
  1016. struct request_context *ctx;
  1017. u8 *buf;
  1018. int tx_size;
  1019. if (!netif_running(dev)) {
  1020. printk(KERN_ERR "%s: Tx on stopped device!\n",
  1021. dev->name);
  1022. return NETDEV_TX_BUSY;
  1023. }
  1024. if (netif_queue_stopped(dev)) {
  1025. printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
  1026. dev->name);
  1027. return NETDEV_TX_BUSY;
  1028. }
  1029. if (orinoco_lock(priv, &flags) != 0) {
  1030. printk(KERN_ERR
  1031. "%s: ezusb_xmit() called while hw_unavailable\n",
  1032. dev->name);
  1033. return NETDEV_TX_BUSY;
  1034. }
  1035. if (!netif_carrier_ok(dev) ||
  1036. (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
  1037. /* Oops, the firmware hasn't established a connection,
  1038. silently drop the packet (this seems to be the
  1039. safest approach). */
  1040. goto drop;
  1041. }
  1042. /* Check packet length */
  1043. if (skb->len < ETH_HLEN)
  1044. goto drop;
  1045. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_TX, 0);
  1046. if (!ctx)
  1047. goto busy;
  1048. memset(ctx->buf, 0, BULK_BUF_SIZE);
  1049. buf = ctx->buf->data;
  1050. tx_control = 0;
  1051. err = orinoco_process_xmit_skb(skb, dev, priv, &tx_control,
  1052. &mic[0]);
  1053. if (err)
  1054. goto drop;
  1055. {
  1056. __le16 *tx_cntl = (__le16 *)buf;
  1057. *tx_cntl = cpu_to_le16(tx_control);
  1058. buf += sizeof(*tx_cntl);
  1059. }
  1060. memcpy(buf, skb->data, skb->len);
  1061. buf += skb->len;
  1062. if (tx_control & HERMES_TXCTRL_MIC) {
  1063. u8 *m = mic;
  1064. /* Mic has been offset so it can be copied to an even
  1065. * address. We're copying eveything anyway, so we
  1066. * don't need to copy that first byte. */
  1067. if (skb->len % 2)
  1068. m++;
  1069. memcpy(buf, m, MICHAEL_MIC_LEN);
  1070. buf += MICHAEL_MIC_LEN;
  1071. }
  1072. /* Finally, we actually initiate the send */
  1073. netif_stop_queue(dev);
  1074. /* The card may behave better if we send evenly sized usb transfers */
  1075. tx_size = ALIGN(buf - ctx->buf->data, 2);
  1076. err = ezusb_access_ltv(upriv, ctx, tx_size, NULL,
  1077. EZUSB_FRAME_DATA, NULL, 0, NULL);
  1078. if (err) {
  1079. netif_start_queue(dev);
  1080. if (net_ratelimit())
  1081. printk(KERN_ERR "%s: Error %d transmitting packet\n",
  1082. dev->name, err);
  1083. goto busy;
  1084. }
  1085. netif_trans_update(dev);
  1086. stats->tx_bytes += skb->len;
  1087. goto ok;
  1088. drop:
  1089. stats->tx_errors++;
  1090. stats->tx_dropped++;
  1091. ok:
  1092. orinoco_unlock(priv, &flags);
  1093. dev_kfree_skb(skb);
  1094. return NETDEV_TX_OK;
  1095. busy:
  1096. orinoco_unlock(priv, &flags);
  1097. return NETDEV_TX_BUSY;
  1098. }
  1099. static int ezusb_allocate(struct hermes *hw, u16 size, u16 *fid)
  1100. {
  1101. *fid = EZUSB_RID_TX;
  1102. return 0;
  1103. }
  1104. static int ezusb_hard_reset(struct orinoco_private *priv)
  1105. {
  1106. struct ezusb_priv *upriv = priv->card;
  1107. int retval = ezusb_8051_cpucs(upriv, 1);
  1108. if (retval < 0) {
  1109. err("Failed to reset");
  1110. return retval;
  1111. }
  1112. retval = ezusb_8051_cpucs(upriv, 0);
  1113. if (retval < 0) {
  1114. err("Failed to unreset");
  1115. return retval;
  1116. }
  1117. netdev_dbg(upriv->dev, "sending control message\n");
  1118. retval = usb_control_msg(upriv->udev,
  1119. usb_sndctrlpipe(upriv->udev, 0),
  1120. EZUSB_REQUEST_TRIGER,
  1121. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  1122. USB_DIR_OUT, 0x0, 0x0, NULL, 0,
  1123. DEF_TIMEOUT);
  1124. if (retval < 0) {
  1125. err("EZUSB_REQUEST_TRIGER failed retval %d", retval);
  1126. return retval;
  1127. }
  1128. #if 0
  1129. dbg("Sending EZUSB_REQUEST_TRIG_AC");
  1130. retval = usb_control_msg(upriv->udev,
  1131. usb_sndctrlpipe(upriv->udev, 0),
  1132. EZUSB_REQUEST_TRIG_AC,
  1133. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  1134. USB_DIR_OUT, 0x00FA, 0x0, NULL, 0,
  1135. DEF_TIMEOUT);
  1136. if (retval < 0) {
  1137. err("EZUSB_REQUEST_TRIG_AC failed retval %d", retval);
  1138. return retval;
  1139. }
  1140. #endif
  1141. return 0;
  1142. }
  1143. static int ezusb_init(struct hermes *hw)
  1144. {
  1145. struct ezusb_priv *upriv = hw->priv;
  1146. int retval;
  1147. BUG_ON(in_interrupt());
  1148. BUG_ON(!upriv);
  1149. upriv->reply_count = 0;
  1150. /* Write the MAGIC number on the simulated registers to keep
  1151. * orinoco.c happy */
  1152. hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
  1153. hermes_write_regn(hw, RXFID, EZUSB_RID_RX);
  1154. usb_kill_urb(upriv->read_urb);
  1155. ezusb_submit_in_urb(upriv);
  1156. retval = ezusb_write_ltv(hw, 0, EZUSB_RID_INIT1,
  1157. HERMES_BYTES_TO_RECLEN(2), "\x10\x00");
  1158. if (retval < 0) {
  1159. printk(KERN_ERR PFX "EZUSB_RID_INIT1 error %d\n", retval);
  1160. return retval;
  1161. }
  1162. retval = ezusb_docmd_wait(hw, HERMES_CMD_INIT, 0, NULL);
  1163. if (retval < 0) {
  1164. printk(KERN_ERR PFX "HERMES_CMD_INIT error %d\n", retval);
  1165. return retval;
  1166. }
  1167. return 0;
  1168. }
  1169. static void ezusb_bulk_in_callback(struct urb *urb)
  1170. {
  1171. struct ezusb_priv *upriv = (struct ezusb_priv *) urb->context;
  1172. struct ezusb_packet *ans = urb->transfer_buffer;
  1173. u16 crc;
  1174. u16 hermes_rid;
  1175. if (upriv->udev == NULL)
  1176. return;
  1177. if (urb->status == -ETIMEDOUT) {
  1178. /* When a device gets unplugged we get this every time
  1179. * we resubmit, flooding the logs. Since we don't use
  1180. * USB timeouts, it shouldn't happen any other time*/
  1181. pr_warn("%s: urb timed out, not resubmitting\n", __func__);
  1182. return;
  1183. }
  1184. if (urb->status == -ECONNABORTED) {
  1185. pr_warn("%s: connection abort, resubmitting urb\n",
  1186. __func__);
  1187. goto resubmit;
  1188. }
  1189. if ((urb->status == -EILSEQ)
  1190. || (urb->status == -ENOENT)
  1191. || (urb->status == -ECONNRESET)) {
  1192. netdev_dbg(upriv->dev, "status %d, not resubmiting\n",
  1193. urb->status);
  1194. return;
  1195. }
  1196. if (urb->status)
  1197. netdev_dbg(upriv->dev, "status: %d length: %d\n",
  1198. urb->status, urb->actual_length);
  1199. if (urb->actual_length < sizeof(*ans)) {
  1200. err("%s: short read, ignoring", __func__);
  1201. goto resubmit;
  1202. }
  1203. crc = build_crc(ans);
  1204. if (le16_to_cpu(ans->crc) != crc) {
  1205. err("CRC error, ignoring packet");
  1206. goto resubmit;
  1207. }
  1208. hermes_rid = le16_to_cpu(ans->hermes_rid);
  1209. if ((hermes_rid != EZUSB_RID_RX) && !EZUSB_IS_INFO(hermes_rid)) {
  1210. ezusb_request_in_callback(upriv, urb);
  1211. } else if (upriv->dev) {
  1212. struct net_device *dev = upriv->dev;
  1213. struct orinoco_private *priv = ndev_priv(dev);
  1214. struct hermes *hw = &priv->hw;
  1215. if (hermes_rid == EZUSB_RID_RX) {
  1216. __orinoco_ev_rx(dev, hw);
  1217. } else {
  1218. hermes_write_regn(hw, INFOFID,
  1219. le16_to_cpu(ans->hermes_rid));
  1220. __orinoco_ev_info(dev, hw);
  1221. }
  1222. }
  1223. resubmit:
  1224. if (upriv->udev)
  1225. ezusb_submit_in_urb(upriv);
  1226. }
  1227. static inline void ezusb_delete(struct ezusb_priv *upriv)
  1228. {
  1229. struct list_head *item;
  1230. struct list_head *tmp_item;
  1231. unsigned long flags;
  1232. BUG_ON(in_interrupt());
  1233. BUG_ON(!upriv);
  1234. mutex_lock(&upriv->mtx);
  1235. upriv->udev = NULL; /* No timer will be rearmed from here */
  1236. usb_kill_urb(upriv->read_urb);
  1237. spin_lock_irqsave(&upriv->req_lock, flags);
  1238. list_for_each_safe(item, tmp_item, &upriv->req_active) {
  1239. struct request_context *ctx;
  1240. int err;
  1241. ctx = list_entry(item, struct request_context, list);
  1242. refcount_inc(&ctx->refcount);
  1243. ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK;
  1244. err = usb_unlink_urb(ctx->outurb);
  1245. spin_unlock_irqrestore(&upriv->req_lock, flags);
  1246. if (err == -EINPROGRESS)
  1247. wait_for_completion(&ctx->done);
  1248. del_timer_sync(&ctx->timer);
  1249. /* FIXME: there is an slight chance for the irq handler to
  1250. * be running */
  1251. if (!list_empty(&ctx->list))
  1252. ezusb_ctx_complete(ctx);
  1253. ezusb_request_context_put(ctx);
  1254. spin_lock_irqsave(&upriv->req_lock, flags);
  1255. }
  1256. spin_unlock_irqrestore(&upriv->req_lock, flags);
  1257. list_for_each_safe(item, tmp_item, &upriv->req_pending)
  1258. ezusb_ctx_complete(list_entry(item,
  1259. struct request_context, list));
  1260. if (upriv->read_urb && upriv->read_urb->status == -EINPROGRESS)
  1261. printk(KERN_ERR PFX "Some URB in progress\n");
  1262. mutex_unlock(&upriv->mtx);
  1263. if (upriv->read_urb) {
  1264. kfree(upriv->read_urb->transfer_buffer);
  1265. usb_free_urb(upriv->read_urb);
  1266. }
  1267. kfree(upriv->bap_buf);
  1268. if (upriv->dev) {
  1269. struct orinoco_private *priv = ndev_priv(upriv->dev);
  1270. orinoco_if_del(priv);
  1271. wiphy_unregister(priv_to_wiphy(upriv));
  1272. free_orinocodev(priv);
  1273. }
  1274. }
  1275. static void ezusb_lock_irqsave(spinlock_t *lock,
  1276. unsigned long *flags) __acquires(lock)
  1277. {
  1278. spin_lock_bh(lock);
  1279. }
  1280. static void ezusb_unlock_irqrestore(spinlock_t *lock,
  1281. unsigned long *flags) __releases(lock)
  1282. {
  1283. spin_unlock_bh(lock);
  1284. }
  1285. static void ezusb_lock_irq(spinlock_t *lock) __acquires(lock)
  1286. {
  1287. spin_lock_bh(lock);
  1288. }
  1289. static void ezusb_unlock_irq(spinlock_t *lock) __releases(lock)
  1290. {
  1291. spin_unlock_bh(lock);
  1292. }
  1293. static const struct hermes_ops ezusb_ops = {
  1294. .init = ezusb_init,
  1295. .cmd_wait = ezusb_docmd_wait,
  1296. .init_cmd_wait = ezusb_doicmd_wait,
  1297. .allocate = ezusb_allocate,
  1298. .read_ltv = ezusb_read_ltv,
  1299. .write_ltv = ezusb_write_ltv,
  1300. .bap_pread = ezusb_bap_pread,
  1301. .read_pda = ezusb_read_pda,
  1302. .program_init = ezusb_program_init,
  1303. .program_end = ezusb_program_end,
  1304. .program = ezusb_program,
  1305. .lock_irqsave = ezusb_lock_irqsave,
  1306. .unlock_irqrestore = ezusb_unlock_irqrestore,
  1307. .lock_irq = ezusb_lock_irq,
  1308. .unlock_irq = ezusb_unlock_irq,
  1309. };
  1310. static const struct net_device_ops ezusb_netdev_ops = {
  1311. .ndo_open = orinoco_open,
  1312. .ndo_stop = orinoco_stop,
  1313. .ndo_start_xmit = ezusb_xmit,
  1314. .ndo_set_rx_mode = orinoco_set_multicast_list,
  1315. .ndo_change_mtu = orinoco_change_mtu,
  1316. .ndo_set_mac_address = eth_mac_addr,
  1317. .ndo_validate_addr = eth_validate_addr,
  1318. .ndo_tx_timeout = orinoco_tx_timeout,
  1319. };
  1320. static int ezusb_probe(struct usb_interface *interface,
  1321. const struct usb_device_id *id)
  1322. {
  1323. struct usb_device *udev = interface_to_usbdev(interface);
  1324. struct orinoco_private *priv;
  1325. struct hermes *hw;
  1326. struct ezusb_priv *upriv = NULL;
  1327. struct usb_interface_descriptor *iface_desc;
  1328. struct usb_endpoint_descriptor *ep;
  1329. const struct firmware *fw_entry = NULL;
  1330. int retval = 0;
  1331. int i;
  1332. priv = alloc_orinocodev(sizeof(*upriv), &udev->dev,
  1333. ezusb_hard_reset, NULL);
  1334. if (!priv) {
  1335. err("Couldn't allocate orinocodev");
  1336. retval = -ENOMEM;
  1337. goto exit;
  1338. }
  1339. hw = &priv->hw;
  1340. upriv = priv->card;
  1341. mutex_init(&upriv->mtx);
  1342. spin_lock_init(&upriv->reply_count_lock);
  1343. spin_lock_init(&upriv->req_lock);
  1344. INIT_LIST_HEAD(&upriv->req_pending);
  1345. INIT_LIST_HEAD(&upriv->req_active);
  1346. upriv->udev = udev;
  1347. hw->iobase = (void __force __iomem *) &upriv->hermes_reg_fake;
  1348. hw->reg_spacing = HERMES_16BIT_REGSPACING;
  1349. hw->priv = upriv;
  1350. hw->ops = &ezusb_ops;
  1351. /* set up the endpoint information */
  1352. /* check out the endpoints */
  1353. iface_desc = &interface->altsetting[0].desc;
  1354. for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
  1355. ep = &interface->altsetting[0].endpoint[i].desc;
  1356. if (usb_endpoint_is_bulk_in(ep)) {
  1357. /* we found a bulk in endpoint */
  1358. if (upriv->read_urb != NULL) {
  1359. pr_warn("Found a second bulk in ep, ignored\n");
  1360. continue;
  1361. }
  1362. upriv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  1363. if (!upriv->read_urb)
  1364. goto error;
  1365. if (le16_to_cpu(ep->wMaxPacketSize) != 64)
  1366. pr_warn("bulk in: wMaxPacketSize!= 64\n");
  1367. if (ep->bEndpointAddress != (2 | USB_DIR_IN))
  1368. pr_warn("bulk in: bEndpointAddress: %d\n",
  1369. ep->bEndpointAddress);
  1370. upriv->read_pipe = usb_rcvbulkpipe(udev,
  1371. ep->
  1372. bEndpointAddress);
  1373. upriv->read_urb->transfer_buffer =
  1374. kmalloc(BULK_BUF_SIZE, GFP_KERNEL);
  1375. if (!upriv->read_urb->transfer_buffer) {
  1376. err("Couldn't allocate IN buffer");
  1377. goto error;
  1378. }
  1379. }
  1380. if (usb_endpoint_is_bulk_out(ep)) {
  1381. /* we found a bulk out endpoint */
  1382. if (upriv->bap_buf != NULL) {
  1383. pr_warn("Found a second bulk out ep, ignored\n");
  1384. continue;
  1385. }
  1386. if (le16_to_cpu(ep->wMaxPacketSize) != 64)
  1387. pr_warn("bulk out: wMaxPacketSize != 64\n");
  1388. if (ep->bEndpointAddress != 2)
  1389. pr_warn("bulk out: bEndpointAddress: %d\n",
  1390. ep->bEndpointAddress);
  1391. upriv->write_pipe = usb_sndbulkpipe(udev,
  1392. ep->
  1393. bEndpointAddress);
  1394. upriv->bap_buf = kmalloc(BULK_BUF_SIZE, GFP_KERNEL);
  1395. if (!upriv->bap_buf) {
  1396. err("Couldn't allocate bulk_out_buffer");
  1397. goto error;
  1398. }
  1399. }
  1400. }
  1401. if (!upriv->bap_buf || !upriv->read_urb) {
  1402. err("Didn't find the required bulk endpoints");
  1403. goto error;
  1404. }
  1405. if (request_firmware(&fw_entry, "orinoco_ezusb_fw",
  1406. &interface->dev) == 0) {
  1407. firmware.size = fw_entry->size;
  1408. firmware.code = fw_entry->data;
  1409. }
  1410. if (firmware.size && firmware.code) {
  1411. if (ezusb_firmware_download(upriv, &firmware) < 0)
  1412. goto error;
  1413. } else {
  1414. err("No firmware to download");
  1415. goto error;
  1416. }
  1417. if (ezusb_hard_reset(priv) < 0) {
  1418. err("Cannot reset the device");
  1419. goto error;
  1420. }
  1421. /* If the firmware is already downloaded orinoco.c will call
  1422. * ezusb_init but if the firmware is not already there, that will make
  1423. * the kernel very unstable, so we try initializing here and quit in
  1424. * case of error */
  1425. if (ezusb_init(hw) < 0) {
  1426. err("Couldn't initialize the device");
  1427. err("Firmware may not be downloaded or may be wrong.");
  1428. goto error;
  1429. }
  1430. /* Initialise the main driver */
  1431. if (orinoco_init(priv) != 0) {
  1432. err("orinoco_init() failed\n");
  1433. goto error;
  1434. }
  1435. if (orinoco_if_add(priv, 0, 0, &ezusb_netdev_ops) != 0) {
  1436. upriv->dev = NULL;
  1437. err("%s: orinoco_if_add() failed", __func__);
  1438. wiphy_unregister(priv_to_wiphy(priv));
  1439. goto error;
  1440. }
  1441. upriv->dev = priv->ndev;
  1442. goto exit;
  1443. error:
  1444. ezusb_delete(upriv);
  1445. if (upriv->dev) {
  1446. /* upriv->dev was 0, so ezusb_delete() didn't free it */
  1447. free_orinocodev(priv);
  1448. }
  1449. upriv = NULL;
  1450. retval = -EFAULT;
  1451. exit:
  1452. if (fw_entry) {
  1453. firmware.code = NULL;
  1454. firmware.size = 0;
  1455. release_firmware(fw_entry);
  1456. }
  1457. usb_set_intfdata(interface, upriv);
  1458. return retval;
  1459. }
  1460. static void ezusb_disconnect(struct usb_interface *intf)
  1461. {
  1462. struct ezusb_priv *upriv = usb_get_intfdata(intf);
  1463. usb_set_intfdata(intf, NULL);
  1464. ezusb_delete(upriv);
  1465. printk(KERN_INFO PFX "Disconnected\n");
  1466. }
  1467. /* usb specific object needed to register this driver with the usb subsystem */
  1468. static struct usb_driver orinoco_driver = {
  1469. .name = DRIVER_NAME,
  1470. .probe = ezusb_probe,
  1471. .disconnect = ezusb_disconnect,
  1472. .id_table = ezusb_table,
  1473. .disable_hub_initiated_lpm = 1,
  1474. };
  1475. module_usb_driver(orinoco_driver);
  1476. MODULE_AUTHOR("Manuel Estrada Sainz");
  1477. MODULE_DESCRIPTION("Driver for Orinoco wireless LAN cards using EZUSB bridge");
  1478. MODULE_LICENSE("Dual MPL/GPL");