orinoco_usb.c 43 KB

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