orinoco_usb.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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 "mic.h"
  65. #include "orinoco.h"
  66. #ifndef URB_ASYNC_UNLINK
  67. #define URB_ASYNC_UNLINK 0
  68. #endif
  69. /* 802.2 LLC/SNAP header used for Ethernet encapsulation over 802.11 */
  70. static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
  71. #define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
  72. struct header_struct {
  73. /* 802.3 */
  74. u8 dest[ETH_ALEN];
  75. u8 src[ETH_ALEN];
  76. __be16 len;
  77. /* 802.2 */
  78. u8 dsap;
  79. u8 ssap;
  80. u8 ctrl;
  81. /* SNAP */
  82. u8 oui[3];
  83. __be16 ethertype;
  84. } __packed;
  85. struct ez_usb_fw {
  86. u16 size;
  87. const u8 *code;
  88. };
  89. static struct ez_usb_fw firmware = {
  90. .size = 0,
  91. .code = NULL,
  92. };
  93. /* Debugging macros */
  94. #undef err
  95. #define err(format, arg...) \
  96. do { printk(KERN_ERR PFX format "\n", ## arg); } while (0)
  97. /*(DEBLOBBED)*/
  98. /*
  99. * Under some conditions, the card gets stuck and stops paying attention
  100. * to the world (i.e. data communication stalls) until we do something to
  101. * it. Sending an INQ_TALLIES command seems to be enough and should be
  102. * harmless otherwise. This behaviour has been observed when using the
  103. * driver on a systemimager client during installation. In the past a
  104. * timer was used to send INQ_TALLIES commands when there was no other
  105. * activity, but it was troublesome and was removed.
  106. */
  107. #define USB_COMPAQ_VENDOR_ID 0x049f /* Compaq Computer Corp. */
  108. #define USB_COMPAQ_WL215_ID 0x001f /* Compaq WL215 USB Adapter */
  109. #define USB_COMPAQ_W200_ID 0x0076 /* Compaq W200 USB Adapter */
  110. #define USB_HP_WL215_ID 0x0082 /* Compaq WL215 USB Adapter */
  111. #define USB_MELCO_VENDOR_ID 0x0411
  112. #define USB_BUFFALO_L11_ID 0x0006 /* BUFFALO WLI-USB-L11 */
  113. #define USB_BUFFALO_L11G_WR_ID 0x000B /* BUFFALO WLI-USB-L11G-WR */
  114. #define USB_BUFFALO_L11G_ID 0x000D /* BUFFALO WLI-USB-L11G */
  115. #define USB_LUCENT_VENDOR_ID 0x047E /* Lucent Technologies */
  116. #define USB_LUCENT_ORINOCO_ID 0x0300 /* Lucent/Agere Orinoco USB Client */
  117. #define USB_AVAYA8_VENDOR_ID 0x0D98
  118. #define USB_AVAYAE_VENDOR_ID 0x0D9E
  119. #define USB_AVAYA_WIRELESS_ID 0x0300 /* Avaya Wireless USB Card */
  120. #define USB_AGERE_VENDOR_ID 0x0D4E /* Agere Systems */
  121. #define USB_AGERE_MODEL0801_ID 0x1000 /* Wireless USB Card Model 0801 */
  122. #define USB_AGERE_MODEL0802_ID 0x1001 /* Wireless USB Card Model 0802 */
  123. #define USB_AGERE_REBRANDED_ID 0x047A /* WLAN USB Card */
  124. #define USB_ELSA_VENDOR_ID 0x05CC
  125. #define USB_ELSA_AIRLANCER_ID 0x3100 /* ELSA AirLancer USB-11 */
  126. #define USB_LEGEND_VENDOR_ID 0x0E7C
  127. #define USB_LEGEND_JOYNET_ID 0x0300 /* Joynet WLAN USB Card */
  128. #define USB_SAMSUNG_VENDOR_ID 0x04E8
  129. #define USB_SAMSUNG_SEW2001U1_ID 0x5002 /* Samsung SEW-2001u Card */
  130. #define USB_SAMSUNG_SEW2001U2_ID 0x5B11 /* Samsung SEW-2001u Card */
  131. #define USB_SAMSUNG_SEW2003U_ID 0x7011 /* Samsung SEW-2003U Card */
  132. #define USB_IGATE_VENDOR_ID 0x0681
  133. #define USB_IGATE_IGATE_11M_ID 0x0012 /* I-GATE 11M USB Card */
  134. #define USB_FUJITSU_VENDOR_ID 0x0BF8
  135. #define USB_FUJITSU_E1100_ID 0x1002 /* connect2AIR WLAN E-1100 USB */
  136. #define USB_2WIRE_VENDOR_ID 0x1630
  137. #define USB_2WIRE_WIRELESS_ID 0xff81 /* 2Wire Wireless USB adapter */
  138. #define EZUSB_REQUEST_FW_TRANS 0xA0
  139. #define EZUSB_REQUEST_TRIGER 0xAA
  140. #define EZUSB_REQUEST_TRIG_AC 0xAC
  141. #define EZUSB_CPUCS_REG 0x7F92
  142. #define EZUSB_RID_TX 0x0700
  143. #define EZUSB_RID_RX 0x0701
  144. #define EZUSB_RID_INIT1 0x0702
  145. #define EZUSB_RID_ACK 0x0710
  146. #define EZUSB_RID_READ_PDA 0x0800
  147. #define EZUSB_RID_PROG_INIT 0x0852
  148. #define EZUSB_RID_PROG_SET_ADDR 0x0853
  149. #define EZUSB_RID_PROG_BYTES 0x0854
  150. #define EZUSB_RID_PROG_END 0x0855
  151. #define EZUSB_RID_DOCMD 0x0860
  152. /* Recognize info frames */
  153. #define EZUSB_IS_INFO(id) ((id >= 0xF000) && (id <= 0xF2FF))
  154. #define EZUSB_MAGIC 0x0210
  155. #define EZUSB_FRAME_DATA 1
  156. #define EZUSB_FRAME_CONTROL 2
  157. #define DEF_TIMEOUT (3 * HZ)
  158. #define BULK_BUF_SIZE 2048
  159. #define MAX_DL_SIZE (BULK_BUF_SIZE - sizeof(struct ezusb_packet))
  160. #define FW_BUF_SIZE 64
  161. #define FW_VAR_OFFSET_PTR 0x359
  162. #define FW_VAR_VALUE 0
  163. #define FW_HOLE_START 0x100
  164. #define FW_HOLE_END 0x300
  165. struct ezusb_packet {
  166. __le16 magic; /* 0x0210 */
  167. u8 req_reply_count;
  168. u8 ans_reply_count;
  169. __le16 frame_type; /* 0x01 for data frames, 0x02 otherwise */
  170. __le16 size; /* transport size */
  171. __le16 crc; /* CRC up to here */
  172. __le16 hermes_len;
  173. __le16 hermes_rid;
  174. u8 data[0];
  175. } __packed;
  176. /* Table of devices that work or may work with this driver */
  177. static struct usb_device_id ezusb_table[] = {
  178. {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_COMPAQ_WL215_ID)},
  179. {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_HP_WL215_ID)},
  180. {USB_DEVICE(USB_COMPAQ_VENDOR_ID, USB_COMPAQ_W200_ID)},
  181. {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11_ID)},
  182. {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11G_WR_ID)},
  183. {USB_DEVICE(USB_MELCO_VENDOR_ID, USB_BUFFALO_L11G_ID)},
  184. {USB_DEVICE(USB_LUCENT_VENDOR_ID, USB_LUCENT_ORINOCO_ID)},
  185. {USB_DEVICE(USB_AVAYA8_VENDOR_ID, USB_AVAYA_WIRELESS_ID)},
  186. {USB_DEVICE(USB_AVAYAE_VENDOR_ID, USB_AVAYA_WIRELESS_ID)},
  187. {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_MODEL0801_ID)},
  188. {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_MODEL0802_ID)},
  189. {USB_DEVICE(USB_ELSA_VENDOR_ID, USB_ELSA_AIRLANCER_ID)},
  190. {USB_DEVICE(USB_LEGEND_VENDOR_ID, USB_LEGEND_JOYNET_ID)},
  191. {USB_DEVICE_VER(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2001U1_ID,
  192. 0, 0)},
  193. {USB_DEVICE(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2001U2_ID)},
  194. {USB_DEVICE(USB_SAMSUNG_VENDOR_ID, USB_SAMSUNG_SEW2003U_ID)},
  195. {USB_DEVICE(USB_IGATE_VENDOR_ID, USB_IGATE_IGATE_11M_ID)},
  196. {USB_DEVICE(USB_FUJITSU_VENDOR_ID, USB_FUJITSU_E1100_ID)},
  197. {USB_DEVICE(USB_2WIRE_VENDOR_ID, USB_2WIRE_WIRELESS_ID)},
  198. {USB_DEVICE(USB_AGERE_VENDOR_ID, USB_AGERE_REBRANDED_ID)},
  199. {} /* Terminating entry */
  200. };
  201. MODULE_DEVICE_TABLE(usb, ezusb_table);
  202. /* Structure to hold all of our device specific stuff */
  203. struct ezusb_priv {
  204. struct usb_device *udev;
  205. struct net_device *dev;
  206. struct mutex mtx;
  207. spinlock_t req_lock;
  208. struct list_head req_pending;
  209. struct list_head req_active;
  210. spinlock_t reply_count_lock;
  211. u16 hermes_reg_fake[0x40];
  212. u8 *bap_buf;
  213. struct urb *read_urb;
  214. int read_pipe;
  215. int write_pipe;
  216. u8 reply_count;
  217. };
  218. enum ezusb_state {
  219. EZUSB_CTX_START,
  220. EZUSB_CTX_QUEUED,
  221. EZUSB_CTX_REQ_SUBMITTED,
  222. EZUSB_CTX_REQ_COMPLETE,
  223. EZUSB_CTX_RESP_RECEIVED,
  224. EZUSB_CTX_REQ_TIMEOUT,
  225. EZUSB_CTX_REQ_FAILED,
  226. EZUSB_CTX_RESP_TIMEOUT,
  227. EZUSB_CTX_REQSUBMIT_FAIL,
  228. EZUSB_CTX_COMPLETE,
  229. };
  230. struct request_context {
  231. struct list_head list;
  232. atomic_t refcount;
  233. struct completion done; /* Signals that CTX is dead */
  234. int killed;
  235. struct urb *outurb; /* OUT for req pkt */
  236. struct ezusb_priv *upriv;
  237. struct ezusb_packet *buf;
  238. int buf_length;
  239. struct timer_list timer; /* Timeout handling */
  240. enum ezusb_state state; /* Current state */
  241. /* the RID that we will wait for */
  242. u16 out_rid;
  243. u16 in_rid;
  244. };
  245. /* Forward declarations */
  246. static void ezusb_ctx_complete(struct request_context *ctx);
  247. static void ezusb_req_queue_run(struct ezusb_priv *upriv);
  248. static void ezusb_bulk_in_callback(struct urb *urb);
  249. static inline u8 ezusb_reply_inc(u8 count)
  250. {
  251. if (count < 0x7F)
  252. return count + 1;
  253. else
  254. return 1;
  255. }
  256. static void ezusb_request_context_put(struct request_context *ctx)
  257. {
  258. if (!atomic_dec_and_test(&ctx->refcount))
  259. return;
  260. WARN_ON(!ctx->done.done);
  261. BUG_ON(ctx->outurb->status == -EINPROGRESS);
  262. BUG_ON(timer_pending(&ctx->timer));
  263. usb_free_urb(ctx->outurb);
  264. kfree(ctx->buf);
  265. kfree(ctx);
  266. }
  267. static inline void ezusb_mod_timer(struct ezusb_priv *upriv,
  268. struct timer_list *timer,
  269. unsigned long expire)
  270. {
  271. if (!upriv->udev)
  272. return;
  273. mod_timer(timer, expire);
  274. }
  275. static void ezusb_request_timerfn(u_long _ctx)
  276. {
  277. struct request_context *ctx = (void *) _ctx;
  278. ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK;
  279. if (usb_unlink_urb(ctx->outurb) == -EINPROGRESS) {
  280. ctx->state = EZUSB_CTX_REQ_TIMEOUT;
  281. } else {
  282. ctx->state = EZUSB_CTX_RESP_TIMEOUT;
  283. dev_dbg(&ctx->outurb->dev->dev, "couldn't unlink\n");
  284. atomic_inc(&ctx->refcount);
  285. ctx->killed = 1;
  286. ezusb_ctx_complete(ctx);
  287. ezusb_request_context_put(ctx);
  288. }
  289. };
  290. static struct request_context *ezusb_alloc_ctx(struct ezusb_priv *upriv,
  291. u16 out_rid, u16 in_rid)
  292. {
  293. struct request_context *ctx;
  294. ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
  295. if (!ctx)
  296. return NULL;
  297. ctx->buf = kmalloc(BULK_BUF_SIZE, GFP_ATOMIC);
  298. if (!ctx->buf) {
  299. kfree(ctx);
  300. return NULL;
  301. }
  302. ctx->outurb = usb_alloc_urb(0, GFP_ATOMIC);
  303. if (!ctx->outurb) {
  304. kfree(ctx->buf);
  305. kfree(ctx);
  306. return NULL;
  307. }
  308. ctx->upriv = upriv;
  309. ctx->state = EZUSB_CTX_START;
  310. ctx->out_rid = out_rid;
  311. ctx->in_rid = in_rid;
  312. atomic_set(&ctx->refcount, 1);
  313. init_completion(&ctx->done);
  314. setup_timer(&ctx->timer, ezusb_request_timerfn, (u_long)ctx);
  315. return ctx;
  316. }
  317. /* Hopefully the real complete_all will soon be exported, in the mean
  318. * while this should work. */
  319. static inline void ezusb_complete_all(struct completion *comp)
  320. {
  321. complete(comp);
  322. complete(comp);
  323. complete(comp);
  324. complete(comp);
  325. }
  326. static void ezusb_ctx_complete(struct request_context *ctx)
  327. {
  328. struct ezusb_priv *upriv = ctx->upriv;
  329. unsigned long flags;
  330. spin_lock_irqsave(&upriv->req_lock, flags);
  331. list_del_init(&ctx->list);
  332. if (upriv->udev) {
  333. spin_unlock_irqrestore(&upriv->req_lock, flags);
  334. ezusb_req_queue_run(upriv);
  335. spin_lock_irqsave(&upriv->req_lock, flags);
  336. }
  337. switch (ctx->state) {
  338. case EZUSB_CTX_COMPLETE:
  339. case EZUSB_CTX_REQSUBMIT_FAIL:
  340. case EZUSB_CTX_REQ_FAILED:
  341. case EZUSB_CTX_REQ_TIMEOUT:
  342. case EZUSB_CTX_RESP_TIMEOUT:
  343. spin_unlock_irqrestore(&upriv->req_lock, flags);
  344. if ((ctx->out_rid == EZUSB_RID_TX) && upriv->dev) {
  345. struct net_device *dev = upriv->dev;
  346. struct orinoco_private *priv = ndev_priv(dev);
  347. struct net_device_stats *stats = &priv->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. atomic_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. atomic_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. u8 res_val = reset; /* avoid argument promotion */
  655. if (!upriv->udev) {
  656. err("%s: !upriv->udev", __func__);
  657. return -EFAULT;
  658. }
  659. return usb_control_msg(upriv->udev,
  660. usb_sndctrlpipe(upriv->udev, 0),
  661. EZUSB_REQUEST_FW_TRANS,
  662. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  663. USB_DIR_OUT, EZUSB_CPUCS_REG, 0, &res_val,
  664. sizeof(res_val), DEF_TIMEOUT);
  665. }
  666. static int ezusb_firmware_download(struct ezusb_priv *upriv,
  667. struct ez_usb_fw *fw)
  668. {
  669. u8 *fw_buffer;
  670. int retval, addr;
  671. int variant_offset;
  672. fw_buffer = kmalloc(FW_BUF_SIZE, GFP_KERNEL);
  673. if (!fw_buffer) {
  674. printk(KERN_ERR PFX "Out of memory for firmware buffer.\n");
  675. return -ENOMEM;
  676. }
  677. /*
  678. * This byte is 1 and should be replaced with 0. The offset is
  679. * 0x10AD in version 0.0.6. The byte in question should follow
  680. * the end of the code pointed to by the jump in the beginning
  681. * of the firmware. Also, it is read by code located at 0x358.
  682. */
  683. variant_offset = be16_to_cpup((__be16 *) &fw->code[FW_VAR_OFFSET_PTR]);
  684. if (variant_offset >= fw->size) {
  685. printk(KERN_ERR PFX "Invalid firmware variant offset: "
  686. "0x%04x\n", variant_offset);
  687. retval = -EINVAL;
  688. goto fail;
  689. }
  690. retval = ezusb_8051_cpucs(upriv, 1);
  691. if (retval < 0)
  692. goto fail;
  693. for (addr = 0; addr < fw->size; addr += FW_BUF_SIZE) {
  694. /* 0x100-0x300 should be left alone, it contains card
  695. * specific data, like USB enumeration information */
  696. if ((addr >= FW_HOLE_START) && (addr < FW_HOLE_END))
  697. continue;
  698. memcpy(fw_buffer, &fw->code[addr], FW_BUF_SIZE);
  699. if (variant_offset >= addr &&
  700. variant_offset < addr + FW_BUF_SIZE) {
  701. netdev_dbg(upriv->dev,
  702. "Patching card_variant byte at 0x%04X\n",
  703. variant_offset);
  704. fw_buffer[variant_offset - addr] = FW_VAR_VALUE;
  705. }
  706. retval = usb_control_msg(upriv->udev,
  707. usb_sndctrlpipe(upriv->udev, 0),
  708. EZUSB_REQUEST_FW_TRANS,
  709. USB_TYPE_VENDOR | USB_RECIP_DEVICE
  710. | USB_DIR_OUT,
  711. addr, 0x0,
  712. fw_buffer, FW_BUF_SIZE,
  713. DEF_TIMEOUT);
  714. if (retval < 0)
  715. goto fail;
  716. }
  717. retval = ezusb_8051_cpucs(upriv, 0);
  718. if (retval < 0)
  719. goto fail;
  720. goto exit;
  721. fail:
  722. printk(KERN_ERR PFX "Firmware download failed, error %d\n",
  723. retval);
  724. exit:
  725. kfree(fw_buffer);
  726. return retval;
  727. }
  728. static int ezusb_access_ltv(struct ezusb_priv *upriv,
  729. struct request_context *ctx,
  730. u16 length, const void *data, u16 frame_type,
  731. void *ans_buff, unsigned ans_size, u16 *ans_length)
  732. {
  733. int req_size;
  734. int retval = 0;
  735. enum ezusb_state state;
  736. BUG_ON(in_irq());
  737. if (!upriv->udev) {
  738. retval = -ENODEV;
  739. goto exit;
  740. }
  741. if (upriv->read_urb->status != -EINPROGRESS)
  742. err("%s: in urb not pending", __func__);
  743. /* protect upriv->reply_count, guarantee sequential numbers */
  744. spin_lock_bh(&upriv->reply_count_lock);
  745. req_size = ezusb_fill_req(ctx->buf, length, ctx->out_rid, data,
  746. frame_type, upriv->reply_count);
  747. usb_fill_bulk_urb(ctx->outurb, upriv->udev, upriv->write_pipe,
  748. ctx->buf, req_size,
  749. ezusb_request_out_callback, ctx);
  750. if (ctx->in_rid)
  751. upriv->reply_count = ezusb_reply_inc(upriv->reply_count);
  752. ezusb_req_enqueue_run(upriv, ctx);
  753. spin_unlock_bh(&upriv->reply_count_lock);
  754. if (ctx->in_rid)
  755. ezusb_req_ctx_wait(upriv, ctx);
  756. state = ctx->state;
  757. switch (state) {
  758. case EZUSB_CTX_COMPLETE:
  759. retval = ctx->outurb->status;
  760. break;
  761. case EZUSB_CTX_QUEUED:
  762. case EZUSB_CTX_REQ_SUBMITTED:
  763. if (!ctx->in_rid)
  764. break;
  765. default:
  766. err("%s: Unexpected context state %d", __func__,
  767. state);
  768. /* fall though */
  769. case EZUSB_CTX_REQ_TIMEOUT:
  770. case EZUSB_CTX_REQ_FAILED:
  771. case EZUSB_CTX_RESP_TIMEOUT:
  772. case EZUSB_CTX_REQSUBMIT_FAIL:
  773. printk(KERN_ERR PFX "Access failed, resetting (state %d,"
  774. " reply_count %d)\n", state, upriv->reply_count);
  775. upriv->reply_count = 0;
  776. if (state == EZUSB_CTX_REQ_TIMEOUT
  777. || state == EZUSB_CTX_RESP_TIMEOUT) {
  778. printk(KERN_ERR PFX "ctx timed out\n");
  779. retval = -ETIMEDOUT;
  780. } else {
  781. printk(KERN_ERR PFX "ctx failed\n");
  782. retval = -EFAULT;
  783. }
  784. goto exit;
  785. }
  786. if (ctx->in_rid) {
  787. struct ezusb_packet *ans = ctx->buf;
  788. unsigned exp_len;
  789. if (ans->hermes_len != 0)
  790. exp_len = le16_to_cpu(ans->hermes_len) * 2 + 12;
  791. else
  792. exp_len = 14;
  793. if (exp_len != ctx->buf_length) {
  794. err("%s: length mismatch for RID 0x%04x: "
  795. "expected %d, got %d", __func__,
  796. ctx->in_rid, exp_len, ctx->buf_length);
  797. retval = -EIO;
  798. goto exit;
  799. }
  800. if (ans_buff)
  801. memcpy(ans_buff, ans->data, min(exp_len, ans_size));
  802. if (ans_length)
  803. *ans_length = le16_to_cpu(ans->hermes_len);
  804. }
  805. exit:
  806. ezusb_request_context_put(ctx);
  807. return retval;
  808. }
  809. static int ezusb_write_ltv(struct hermes *hw, int bap, u16 rid,
  810. u16 length, const void *data)
  811. {
  812. struct ezusb_priv *upriv = hw->priv;
  813. u16 frame_type;
  814. struct request_context *ctx;
  815. if (length == 0)
  816. return -EINVAL;
  817. length = HERMES_RECLEN_TO_BYTES(length);
  818. /* On memory mapped devices HERMES_RID_CNFGROUPADDRESSES can be
  819. * set to be empty, but the USB bridge doesn't like it */
  820. if (length == 0)
  821. return 0;
  822. ctx = ezusb_alloc_ctx(upriv, rid, EZUSB_RID_ACK);
  823. if (!ctx)
  824. return -ENOMEM;
  825. if (rid == EZUSB_RID_TX)
  826. frame_type = EZUSB_FRAME_DATA;
  827. else
  828. frame_type = EZUSB_FRAME_CONTROL;
  829. return ezusb_access_ltv(upriv, ctx, length, data, frame_type,
  830. NULL, 0, NULL);
  831. }
  832. static int ezusb_read_ltv(struct hermes *hw, int bap, u16 rid,
  833. unsigned bufsize, u16 *length, void *buf)
  834. {
  835. struct ezusb_priv *upriv = hw->priv;
  836. struct request_context *ctx;
  837. if (bufsize % 2)
  838. return -EINVAL;
  839. ctx = ezusb_alloc_ctx(upriv, rid, rid);
  840. if (!ctx)
  841. return -ENOMEM;
  842. return ezusb_access_ltv(upriv, ctx, 0, NULL, EZUSB_FRAME_CONTROL,
  843. buf, bufsize, length);
  844. }
  845. static int ezusb_doicmd_wait(struct hermes *hw, u16 cmd, u16 parm0, u16 parm1,
  846. u16 parm2, struct hermes_response *resp)
  847. {
  848. struct ezusb_priv *upriv = hw->priv;
  849. struct request_context *ctx;
  850. __le16 data[4] = {
  851. cpu_to_le16(cmd),
  852. cpu_to_le16(parm0),
  853. cpu_to_le16(parm1),
  854. cpu_to_le16(parm2),
  855. };
  856. netdev_dbg(upriv->dev,
  857. "0x%04X, parm0 0x%04X, parm1 0x%04X, parm2 0x%04X\n", cmd,
  858. parm0, parm1, parm2);
  859. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_DOCMD, EZUSB_RID_ACK);
  860. if (!ctx)
  861. return -ENOMEM;
  862. return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  863. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  864. }
  865. static int ezusb_docmd_wait(struct hermes *hw, u16 cmd, u16 parm0,
  866. struct hermes_response *resp)
  867. {
  868. struct ezusb_priv *upriv = hw->priv;
  869. struct request_context *ctx;
  870. __le16 data[4] = {
  871. cpu_to_le16(cmd),
  872. cpu_to_le16(parm0),
  873. 0,
  874. 0,
  875. };
  876. netdev_dbg(upriv->dev, "0x%04X, parm0 0x%04X\n", cmd, parm0);
  877. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_DOCMD, EZUSB_RID_ACK);
  878. if (!ctx)
  879. return -ENOMEM;
  880. return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  881. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  882. }
  883. static int ezusb_bap_pread(struct hermes *hw, int bap,
  884. void *buf, int len, u16 id, u16 offset)
  885. {
  886. struct ezusb_priv *upriv = hw->priv;
  887. struct ezusb_packet *ans = (void *) upriv->read_urb->transfer_buffer;
  888. int actual_length = upriv->read_urb->actual_length;
  889. if (id == EZUSB_RID_RX) {
  890. if ((sizeof(*ans) + offset + len) > actual_length) {
  891. printk(KERN_ERR PFX "BAP read beyond buffer end "
  892. "in rx frame\n");
  893. return -EINVAL;
  894. }
  895. memcpy(buf, ans->data + offset, len);
  896. return 0;
  897. }
  898. if (EZUSB_IS_INFO(id)) {
  899. /* Include 4 bytes for length/type */
  900. if ((sizeof(*ans) + offset + len - 4) > actual_length) {
  901. printk(KERN_ERR PFX "BAP read beyond buffer end "
  902. "in info frame\n");
  903. return -EFAULT;
  904. }
  905. memcpy(buf, ans->data + offset - 4, len);
  906. } else {
  907. printk(KERN_ERR PFX "Unexpected fid 0x%04x\n", id);
  908. return -EINVAL;
  909. }
  910. return 0;
  911. }
  912. static int ezusb_read_pda(struct hermes *hw, __le16 *pda,
  913. u32 pda_addr, u16 pda_len)
  914. {
  915. struct ezusb_priv *upriv = hw->priv;
  916. struct request_context *ctx;
  917. __le16 data[] = {
  918. cpu_to_le16(pda_addr & 0xffff),
  919. cpu_to_le16(pda_len - 4)
  920. };
  921. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_READ_PDA, EZUSB_RID_READ_PDA);
  922. if (!ctx)
  923. return -ENOMEM;
  924. /* wl_lkm does not include PDA size in the PDA area.
  925. * We will pad the information into pda, so other routines
  926. * don't have to be modified */
  927. pda[0] = cpu_to_le16(pda_len - 2);
  928. /* Includes CFG_PROD_DATA but not itself */
  929. pda[1] = cpu_to_le16(0x0800); /* CFG_PROD_DATA */
  930. return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  931. EZUSB_FRAME_CONTROL, &pda[2], pda_len - 4,
  932. NULL);
  933. }
  934. static int ezusb_program_init(struct hermes *hw, u32 entry_point)
  935. {
  936. struct ezusb_priv *upriv = hw->priv;
  937. struct request_context *ctx;
  938. __le32 data = cpu_to_le32(entry_point);
  939. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_INIT, EZUSB_RID_ACK);
  940. if (!ctx)
  941. return -ENOMEM;
  942. return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  943. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  944. }
  945. static int ezusb_program_end(struct hermes *hw)
  946. {
  947. struct ezusb_priv *upriv = hw->priv;
  948. struct request_context *ctx;
  949. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_END, EZUSB_RID_ACK);
  950. if (!ctx)
  951. return -ENOMEM;
  952. return ezusb_access_ltv(upriv, ctx, 0, NULL,
  953. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  954. }
  955. static int ezusb_program_bytes(struct hermes *hw, const char *buf,
  956. u32 addr, u32 len)
  957. {
  958. struct ezusb_priv *upriv = hw->priv;
  959. struct request_context *ctx;
  960. __le32 data = cpu_to_le32(addr);
  961. int err;
  962. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_SET_ADDR, EZUSB_RID_ACK);
  963. if (!ctx)
  964. return -ENOMEM;
  965. err = ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  966. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  967. if (err)
  968. return err;
  969. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_BYTES, EZUSB_RID_ACK);
  970. if (!ctx)
  971. return -ENOMEM;
  972. return ezusb_access_ltv(upriv, ctx, len, buf,
  973. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  974. }
  975. static int ezusb_program(struct hermes *hw, const char *buf,
  976. u32 addr, u32 len)
  977. {
  978. u32 ch_addr;
  979. u32 ch_len;
  980. int err = 0;
  981. /* We can only send 2048 bytes out of the bulk xmit at a time,
  982. * so we have to split any programming into chunks of <2048
  983. * bytes. */
  984. ch_len = (len < MAX_DL_SIZE) ? len : MAX_DL_SIZE;
  985. ch_addr = addr;
  986. while (ch_addr < (addr + len)) {
  987. pr_debug("Programming subblock of length %d "
  988. "to address 0x%08x. Data @ %p\n",
  989. ch_len, ch_addr, &buf[ch_addr - addr]);
  990. err = ezusb_program_bytes(hw, &buf[ch_addr - addr],
  991. ch_addr, ch_len);
  992. if (err)
  993. break;
  994. ch_addr += ch_len;
  995. ch_len = ((addr + len - ch_addr) < MAX_DL_SIZE) ?
  996. (addr + len - ch_addr) : MAX_DL_SIZE;
  997. }
  998. return err;
  999. }
  1000. static netdev_tx_t ezusb_xmit(struct sk_buff *skb, struct net_device *dev)
  1001. {
  1002. struct orinoco_private *priv = ndev_priv(dev);
  1003. struct net_device_stats *stats = &priv->stats;
  1004. struct ezusb_priv *upriv = priv->card;
  1005. u8 mic[MICHAEL_MIC_LEN + 1];
  1006. int err = 0;
  1007. int tx_control;
  1008. unsigned long flags;
  1009. struct request_context *ctx;
  1010. u8 *buf;
  1011. int tx_size;
  1012. if (!netif_running(dev)) {
  1013. printk(KERN_ERR "%s: Tx on stopped device!\n",
  1014. dev->name);
  1015. return NETDEV_TX_BUSY;
  1016. }
  1017. if (netif_queue_stopped(dev)) {
  1018. printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
  1019. dev->name);
  1020. return NETDEV_TX_BUSY;
  1021. }
  1022. if (orinoco_lock(priv, &flags) != 0) {
  1023. printk(KERN_ERR
  1024. "%s: ezusb_xmit() called while hw_unavailable\n",
  1025. dev->name);
  1026. return NETDEV_TX_BUSY;
  1027. }
  1028. if (!netif_carrier_ok(dev) ||
  1029. (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
  1030. /* Oops, the firmware hasn't established a connection,
  1031. silently drop the packet (this seems to be the
  1032. safest approach). */
  1033. goto drop;
  1034. }
  1035. /* Check packet length */
  1036. if (skb->len < ETH_HLEN)
  1037. goto drop;
  1038. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_TX, 0);
  1039. if (!ctx)
  1040. goto busy;
  1041. memset(ctx->buf, 0, BULK_BUF_SIZE);
  1042. buf = ctx->buf->data;
  1043. tx_control = 0;
  1044. err = orinoco_process_xmit_skb(skb, dev, priv, &tx_control,
  1045. &mic[0]);
  1046. if (err)
  1047. goto drop;
  1048. {
  1049. __le16 *tx_cntl = (__le16 *)buf;
  1050. *tx_cntl = cpu_to_le16(tx_control);
  1051. buf += sizeof(*tx_cntl);
  1052. }
  1053. memcpy(buf, skb->data, skb->len);
  1054. buf += skb->len;
  1055. if (tx_control & HERMES_TXCTRL_MIC) {
  1056. u8 *m = mic;
  1057. /* Mic has been offset so it can be copied to an even
  1058. * address. We're copying eveything anyway, so we
  1059. * don't need to copy that first byte. */
  1060. if (skb->len % 2)
  1061. m++;
  1062. memcpy(buf, m, MICHAEL_MIC_LEN);
  1063. buf += MICHAEL_MIC_LEN;
  1064. }
  1065. /* Finally, we actually initiate the send */
  1066. netif_stop_queue(dev);
  1067. /* The card may behave better if we send evenly sized usb transfers */
  1068. tx_size = ALIGN(buf - ctx->buf->data, 2);
  1069. err = ezusb_access_ltv(upriv, ctx, tx_size, NULL,
  1070. EZUSB_FRAME_DATA, NULL, 0, NULL);
  1071. if (err) {
  1072. netif_start_queue(dev);
  1073. if (net_ratelimit())
  1074. printk(KERN_ERR "%s: Error %d transmitting packet\n",
  1075. dev->name, err);
  1076. goto busy;
  1077. }
  1078. dev->trans_start = jiffies;
  1079. stats->tx_bytes += skb->len;
  1080. goto ok;
  1081. drop:
  1082. stats->tx_errors++;
  1083. stats->tx_dropped++;
  1084. ok:
  1085. orinoco_unlock(priv, &flags);
  1086. dev_kfree_skb(skb);
  1087. return NETDEV_TX_OK;
  1088. busy:
  1089. orinoco_unlock(priv, &flags);
  1090. return NETDEV_TX_BUSY;
  1091. }
  1092. static int ezusb_allocate(struct hermes *hw, u16 size, u16 *fid)
  1093. {
  1094. *fid = EZUSB_RID_TX;
  1095. return 0;
  1096. }
  1097. static int ezusb_hard_reset(struct orinoco_private *priv)
  1098. {
  1099. struct ezusb_priv *upriv = priv->card;
  1100. int retval = ezusb_8051_cpucs(upriv, 1);
  1101. if (retval < 0) {
  1102. err("Failed to reset");
  1103. return retval;
  1104. }
  1105. retval = ezusb_8051_cpucs(upriv, 0);
  1106. if (retval < 0) {
  1107. err("Failed to unreset");
  1108. return retval;
  1109. }
  1110. netdev_dbg(upriv->dev, "sending control message\n");
  1111. retval = usb_control_msg(upriv->udev,
  1112. usb_sndctrlpipe(upriv->udev, 0),
  1113. EZUSB_REQUEST_TRIGER,
  1114. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  1115. USB_DIR_OUT, 0x0, 0x0, NULL, 0,
  1116. DEF_TIMEOUT);
  1117. if (retval < 0) {
  1118. err("EZUSB_REQUEST_TRIGER failed retval %d", retval);
  1119. return retval;
  1120. }
  1121. #if 0
  1122. dbg("Sending EZUSB_REQUEST_TRIG_AC");
  1123. retval = usb_control_msg(upriv->udev,
  1124. usb_sndctrlpipe(upriv->udev, 0),
  1125. EZUSB_REQUEST_TRIG_AC,
  1126. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  1127. USB_DIR_OUT, 0x00FA, 0x0, NULL, 0,
  1128. DEF_TIMEOUT);
  1129. if (retval < 0) {
  1130. err("EZUSB_REQUEST_TRIG_AC failed retval %d", retval);
  1131. return retval;
  1132. }
  1133. #endif
  1134. return 0;
  1135. }
  1136. static int ezusb_init(struct hermes *hw)
  1137. {
  1138. struct ezusb_priv *upriv = hw->priv;
  1139. int retval;
  1140. BUG_ON(in_interrupt());
  1141. BUG_ON(!upriv);
  1142. upriv->reply_count = 0;
  1143. /* Write the MAGIC number on the simulated registers to keep
  1144. * orinoco.c happy */
  1145. hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
  1146. hermes_write_regn(hw, RXFID, EZUSB_RID_RX);
  1147. usb_kill_urb(upriv->read_urb);
  1148. ezusb_submit_in_urb(upriv);
  1149. retval = ezusb_write_ltv(hw, 0, EZUSB_RID_INIT1,
  1150. HERMES_BYTES_TO_RECLEN(2), "\x10\x00");
  1151. if (retval < 0) {
  1152. printk(KERN_ERR PFX "EZUSB_RID_INIT1 error %d\n", retval);
  1153. return retval;
  1154. }
  1155. retval = ezusb_docmd_wait(hw, HERMES_CMD_INIT, 0, NULL);
  1156. if (retval < 0) {
  1157. printk(KERN_ERR PFX "HERMES_CMD_INIT error %d\n", retval);
  1158. return retval;
  1159. }
  1160. return 0;
  1161. }
  1162. static void ezusb_bulk_in_callback(struct urb *urb)
  1163. {
  1164. struct ezusb_priv *upriv = (struct ezusb_priv *) urb->context;
  1165. struct ezusb_packet *ans = urb->transfer_buffer;
  1166. u16 crc;
  1167. u16 hermes_rid;
  1168. if (upriv->udev == NULL)
  1169. return;
  1170. if (urb->status == -ETIMEDOUT) {
  1171. /* When a device gets unplugged we get this every time
  1172. * we resubmit, flooding the logs. Since we don't use
  1173. * USB timeouts, it shouldn't happen any other time*/
  1174. pr_warn("%s: urb timed out, not resubmitting\n", __func__);
  1175. return;
  1176. }
  1177. if (urb->status == -ECONNABORTED) {
  1178. pr_warn("%s: connection abort, resubmitting urb\n",
  1179. __func__);
  1180. goto resubmit;
  1181. }
  1182. if ((urb->status == -EILSEQ)
  1183. || (urb->status == -ENOENT)
  1184. || (urb->status == -ECONNRESET)) {
  1185. netdev_dbg(upriv->dev, "status %d, not resubmiting\n",
  1186. urb->status);
  1187. return;
  1188. }
  1189. if (urb->status)
  1190. netdev_dbg(upriv->dev, "status: %d length: %d\n",
  1191. urb->status, urb->actual_length);
  1192. if (urb->actual_length < sizeof(*ans)) {
  1193. err("%s: short read, ignoring", __func__);
  1194. goto resubmit;
  1195. }
  1196. crc = build_crc(ans);
  1197. if (le16_to_cpu(ans->crc) != crc) {
  1198. err("CRC error, ignoring packet");
  1199. goto resubmit;
  1200. }
  1201. hermes_rid = le16_to_cpu(ans->hermes_rid);
  1202. if ((hermes_rid != EZUSB_RID_RX) && !EZUSB_IS_INFO(hermes_rid)) {
  1203. ezusb_request_in_callback(upriv, urb);
  1204. } else if (upriv->dev) {
  1205. struct net_device *dev = upriv->dev;
  1206. struct orinoco_private *priv = ndev_priv(dev);
  1207. struct hermes *hw = &priv->hw;
  1208. if (hermes_rid == EZUSB_RID_RX) {
  1209. __orinoco_ev_rx(dev, hw);
  1210. } else {
  1211. hermes_write_regn(hw, INFOFID,
  1212. le16_to_cpu(ans->hermes_rid));
  1213. __orinoco_ev_info(dev, hw);
  1214. }
  1215. }
  1216. resubmit:
  1217. if (upriv->udev)
  1218. ezusb_submit_in_urb(upriv);
  1219. }
  1220. static inline void ezusb_delete(struct ezusb_priv *upriv)
  1221. {
  1222. struct net_device *dev;
  1223. struct list_head *item;
  1224. struct list_head *tmp_item;
  1225. unsigned long flags;
  1226. BUG_ON(in_interrupt());
  1227. BUG_ON(!upriv);
  1228. dev = upriv->dev;
  1229. mutex_lock(&upriv->mtx);
  1230. upriv->udev = NULL; /* No timer will be rearmed from here */
  1231. usb_kill_urb(upriv->read_urb);
  1232. spin_lock_irqsave(&upriv->req_lock, flags);
  1233. list_for_each_safe(item, tmp_item, &upriv->req_active) {
  1234. struct request_context *ctx;
  1235. int err;
  1236. ctx = list_entry(item, struct request_context, list);
  1237. atomic_inc(&ctx->refcount);
  1238. ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK;
  1239. err = usb_unlink_urb(ctx->outurb);
  1240. spin_unlock_irqrestore(&upriv->req_lock, flags);
  1241. if (err == -EINPROGRESS)
  1242. wait_for_completion(&ctx->done);
  1243. del_timer_sync(&ctx->timer);
  1244. /* FIXME: there is an slight chance for the irq handler to
  1245. * be running */
  1246. if (!list_empty(&ctx->list))
  1247. ezusb_ctx_complete(ctx);
  1248. ezusb_request_context_put(ctx);
  1249. spin_lock_irqsave(&upriv->req_lock, flags);
  1250. }
  1251. spin_unlock_irqrestore(&upriv->req_lock, flags);
  1252. list_for_each_safe(item, tmp_item, &upriv->req_pending)
  1253. ezusb_ctx_complete(list_entry(item,
  1254. struct request_context, list));
  1255. if (upriv->read_urb && upriv->read_urb->status == -EINPROGRESS)
  1256. printk(KERN_ERR PFX "Some URB in progress\n");
  1257. mutex_unlock(&upriv->mtx);
  1258. if (upriv->read_urb) {
  1259. kfree(upriv->read_urb->transfer_buffer);
  1260. usb_free_urb(upriv->read_urb);
  1261. }
  1262. kfree(upriv->bap_buf);
  1263. if (upriv->dev) {
  1264. struct orinoco_private *priv = ndev_priv(upriv->dev);
  1265. orinoco_if_del(priv);
  1266. free_orinocodev(priv);
  1267. }
  1268. }
  1269. static void ezusb_lock_irqsave(spinlock_t *lock,
  1270. unsigned long *flags) __acquires(lock)
  1271. {
  1272. spin_lock_bh(lock);
  1273. }
  1274. static void ezusb_unlock_irqrestore(spinlock_t *lock,
  1275. unsigned long *flags) __releases(lock)
  1276. {
  1277. spin_unlock_bh(lock);
  1278. }
  1279. static void ezusb_lock_irq(spinlock_t *lock) __acquires(lock)
  1280. {
  1281. spin_lock_bh(lock);
  1282. }
  1283. static void ezusb_unlock_irq(spinlock_t *lock) __releases(lock)
  1284. {
  1285. spin_unlock_bh(lock);
  1286. }
  1287. static const struct hermes_ops ezusb_ops = {
  1288. .init = ezusb_init,
  1289. .cmd_wait = ezusb_docmd_wait,
  1290. .init_cmd_wait = ezusb_doicmd_wait,
  1291. .allocate = ezusb_allocate,
  1292. .read_ltv = ezusb_read_ltv,
  1293. .write_ltv = ezusb_write_ltv,
  1294. .bap_pread = ezusb_bap_pread,
  1295. .read_pda = ezusb_read_pda,
  1296. .program_init = ezusb_program_init,
  1297. .program_end = ezusb_program_end,
  1298. .program = ezusb_program,
  1299. .lock_irqsave = ezusb_lock_irqsave,
  1300. .unlock_irqrestore = ezusb_unlock_irqrestore,
  1301. .lock_irq = ezusb_lock_irq,
  1302. .unlock_irq = ezusb_unlock_irq,
  1303. };
  1304. static const struct net_device_ops ezusb_netdev_ops = {
  1305. .ndo_open = orinoco_open,
  1306. .ndo_stop = orinoco_stop,
  1307. .ndo_start_xmit = ezusb_xmit,
  1308. .ndo_set_rx_mode = orinoco_set_multicast_list,
  1309. .ndo_change_mtu = orinoco_change_mtu,
  1310. .ndo_set_mac_address = eth_mac_addr,
  1311. .ndo_validate_addr = eth_validate_addr,
  1312. .ndo_tx_timeout = orinoco_tx_timeout,
  1313. .ndo_get_stats = orinoco_get_stats,
  1314. };
  1315. static int ezusb_probe(struct usb_interface *interface,
  1316. const struct usb_device_id *id)
  1317. {
  1318. struct usb_device *udev = interface_to_usbdev(interface);
  1319. struct orinoco_private *priv;
  1320. struct hermes *hw;
  1321. struct ezusb_priv *upriv = NULL;
  1322. struct usb_interface_descriptor *iface_desc;
  1323. struct usb_endpoint_descriptor *ep;
  1324. const struct firmware *fw_entry = NULL;
  1325. int retval = 0;
  1326. int i;
  1327. priv = alloc_orinocodev(sizeof(*upriv), &udev->dev,
  1328. ezusb_hard_reset, NULL);
  1329. if (!priv) {
  1330. err("Couldn't allocate orinocodev");
  1331. goto exit;
  1332. }
  1333. hw = &priv->hw;
  1334. upriv = priv->card;
  1335. mutex_init(&upriv->mtx);
  1336. spin_lock_init(&upriv->reply_count_lock);
  1337. spin_lock_init(&upriv->req_lock);
  1338. INIT_LIST_HEAD(&upriv->req_pending);
  1339. INIT_LIST_HEAD(&upriv->req_active);
  1340. upriv->udev = udev;
  1341. hw->iobase = (void __force __iomem *) &upriv->hermes_reg_fake;
  1342. hw->reg_spacing = HERMES_16BIT_REGSPACING;
  1343. hw->priv = upriv;
  1344. hw->ops = &ezusb_ops;
  1345. /* set up the endpoint information */
  1346. /* check out the endpoints */
  1347. iface_desc = &interface->altsetting[0].desc;
  1348. for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
  1349. ep = &interface->altsetting[0].endpoint[i].desc;
  1350. if (usb_endpoint_is_bulk_in(ep)) {
  1351. /* we found a bulk in endpoint */
  1352. if (upriv->read_urb != NULL) {
  1353. pr_warn("Found a second bulk in ep, ignored\n");
  1354. continue;
  1355. }
  1356. upriv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  1357. if (!upriv->read_urb) {
  1358. err("No free urbs available");
  1359. goto error;
  1360. }
  1361. if (le16_to_cpu(ep->wMaxPacketSize) != 64)
  1362. pr_warn("bulk in: wMaxPacketSize!= 64\n");
  1363. if (ep->bEndpointAddress != (2 | USB_DIR_IN))
  1364. pr_warn("bulk in: bEndpointAddress: %d\n",
  1365. ep->bEndpointAddress);
  1366. upriv->read_pipe = usb_rcvbulkpipe(udev,
  1367. ep->
  1368. bEndpointAddress);
  1369. upriv->read_urb->transfer_buffer =
  1370. kmalloc(BULK_BUF_SIZE, GFP_KERNEL);
  1371. if (!upriv->read_urb->transfer_buffer) {
  1372. err("Couldn't allocate IN buffer");
  1373. goto error;
  1374. }
  1375. }
  1376. if (usb_endpoint_is_bulk_out(ep)) {
  1377. /* we found a bulk out endpoint */
  1378. if (upriv->bap_buf != NULL) {
  1379. pr_warn("Found a second bulk out ep, ignored\n");
  1380. continue;
  1381. }
  1382. if (le16_to_cpu(ep->wMaxPacketSize) != 64)
  1383. pr_warn("bulk out: wMaxPacketSize != 64\n");
  1384. if (ep->bEndpointAddress != 2)
  1385. pr_warn("bulk out: bEndpointAddress: %d\n",
  1386. ep->bEndpointAddress);
  1387. upriv->write_pipe = usb_sndbulkpipe(udev,
  1388. ep->
  1389. bEndpointAddress);
  1390. upriv->bap_buf = kmalloc(BULK_BUF_SIZE, GFP_KERNEL);
  1391. if (!upriv->bap_buf) {
  1392. err("Couldn't allocate bulk_out_buffer");
  1393. goto error;
  1394. }
  1395. }
  1396. }
  1397. if (!upriv->bap_buf || !upriv->read_urb) {
  1398. err("Didn't find the required bulk endpoints");
  1399. goto error;
  1400. }
  1401. if (reject_firmware(&fw_entry, "/*(DEBLOBBED)*/",
  1402. &interface->dev) == 0) {
  1403. firmware.size = fw_entry->size;
  1404. firmware.code = fw_entry->data;
  1405. }
  1406. if (firmware.size && firmware.code) {
  1407. if (ezusb_firmware_download(upriv, &firmware) < 0)
  1408. goto error;
  1409. } else {
  1410. err("No firmware to download");
  1411. goto error;
  1412. }
  1413. if (ezusb_hard_reset(priv) < 0) {
  1414. err("Cannot reset the device");
  1415. goto error;
  1416. }
  1417. /* If the firmware is already downloaded orinoco.c will call
  1418. * ezusb_init but if the firmware is not already there, that will make
  1419. * the kernel very unstable, so we try initializing here and quit in
  1420. * case of error */
  1421. if (ezusb_init(hw) < 0) {
  1422. err("Couldn't initialize the device");
  1423. err("Firmware may not be downloaded or may be wrong.");
  1424. goto error;
  1425. }
  1426. /* Initialise the main driver */
  1427. if (orinoco_init(priv) != 0) {
  1428. err("orinoco_init() failed\n");
  1429. goto error;
  1430. }
  1431. if (orinoco_if_add(priv, 0, 0, &ezusb_netdev_ops) != 0) {
  1432. upriv->dev = NULL;
  1433. err("%s: orinoco_if_add() failed", __func__);
  1434. goto error;
  1435. }
  1436. upriv->dev = priv->ndev;
  1437. goto exit;
  1438. error:
  1439. ezusb_delete(upriv);
  1440. if (upriv->dev) {
  1441. /* upriv->dev was 0, so ezusb_delete() didn't free it */
  1442. free_orinocodev(priv);
  1443. }
  1444. upriv = NULL;
  1445. retval = -EFAULT;
  1446. exit:
  1447. if (fw_entry) {
  1448. firmware.code = NULL;
  1449. firmware.size = 0;
  1450. release_firmware(fw_entry);
  1451. }
  1452. usb_set_intfdata(interface, upriv);
  1453. return retval;
  1454. }
  1455. static void ezusb_disconnect(struct usb_interface *intf)
  1456. {
  1457. struct ezusb_priv *upriv = usb_get_intfdata(intf);
  1458. usb_set_intfdata(intf, NULL);
  1459. ezusb_delete(upriv);
  1460. printk(KERN_INFO PFX "Disconnected\n");
  1461. }
  1462. /* usb specific object needed to register this driver with the usb subsystem */
  1463. static struct usb_driver orinoco_driver = {
  1464. .name = DRIVER_NAME,
  1465. .probe = ezusb_probe,
  1466. .disconnect = ezusb_disconnect,
  1467. .id_table = ezusb_table,
  1468. .disable_hub_initiated_lpm = 1,
  1469. };
  1470. module_usb_driver(orinoco_driver);
  1471. MODULE_AUTHOR("Manuel Estrada Sainz");
  1472. MODULE_DESCRIPTION("Driver for Orinoco wireless LAN cards using EZUSB bridge");
  1473. MODULE_LICENSE("Dual MPL/GPL");