orinoco_usb.c 44 KB

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