orinoco_usb.c 44 KB

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