orinoco_usb.c 44 KB

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