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 "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 net_device_stats *stats = &dev->stats;
  347. if (ctx->state != EZUSB_CTX_COMPLETE)
  348. stats->tx_errors++;
  349. else
  350. stats->tx_packets++;
  351. netif_wake_queue(dev);
  352. }
  353. ezusb_complete_all(&ctx->done);
  354. ezusb_request_context_put(ctx);
  355. break;
  356. default:
  357. spin_unlock_irqrestore(&upriv->req_lock, flags);
  358. if (!upriv->udev) {
  359. /* This is normal, as all request contexts get flushed
  360. * when the device is disconnected */
  361. err("Called, CTX not terminating, but device gone");
  362. ezusb_complete_all(&ctx->done);
  363. ezusb_request_context_put(ctx);
  364. break;
  365. }
  366. err("Called, CTX not in terminating state.");
  367. /* Things are really bad if this happens. Just leak
  368. * the CTX because it may still be linked to the
  369. * queue or the OUT urb may still be active.
  370. * Just leaking at least prevents an Oops or Panic.
  371. */
  372. break;
  373. }
  374. }
  375. /**
  376. * ezusb_req_queue_run:
  377. * Description:
  378. * Note: Only one active CTX at any one time, because there's no
  379. * other (reliable) way to match the response URB to the correct
  380. * CTX.
  381. **/
  382. static void ezusb_req_queue_run(struct ezusb_priv *upriv)
  383. {
  384. unsigned long flags;
  385. struct request_context *ctx;
  386. int result;
  387. spin_lock_irqsave(&upriv->req_lock, flags);
  388. if (!list_empty(&upriv->req_active))
  389. goto unlock;
  390. if (list_empty(&upriv->req_pending))
  391. goto unlock;
  392. ctx =
  393. list_entry(upriv->req_pending.next, struct request_context,
  394. list);
  395. if (!ctx->upriv->udev)
  396. goto unlock;
  397. /* We need to split this off to avoid a race condition */
  398. list_move_tail(&ctx->list, &upriv->req_active);
  399. if (ctx->state == EZUSB_CTX_QUEUED) {
  400. atomic_inc(&ctx->refcount);
  401. result = usb_submit_urb(ctx->outurb, GFP_ATOMIC);
  402. if (result) {
  403. ctx->state = EZUSB_CTX_REQSUBMIT_FAIL;
  404. spin_unlock_irqrestore(&upriv->req_lock, flags);
  405. err("Fatal, failed to submit command urb."
  406. " error=%d\n", result);
  407. ezusb_ctx_complete(ctx);
  408. ezusb_request_context_put(ctx);
  409. goto done;
  410. }
  411. ctx->state = EZUSB_CTX_REQ_SUBMITTED;
  412. ezusb_mod_timer(ctx->upriv, &ctx->timer,
  413. jiffies + DEF_TIMEOUT);
  414. }
  415. unlock:
  416. spin_unlock_irqrestore(&upriv->req_lock, flags);
  417. done:
  418. return;
  419. }
  420. static void ezusb_req_enqueue_run(struct ezusb_priv *upriv,
  421. struct request_context *ctx)
  422. {
  423. unsigned long flags;
  424. spin_lock_irqsave(&upriv->req_lock, flags);
  425. if (!ctx->upriv->udev) {
  426. spin_unlock_irqrestore(&upriv->req_lock, flags);
  427. goto done;
  428. }
  429. atomic_inc(&ctx->refcount);
  430. list_add_tail(&ctx->list, &upriv->req_pending);
  431. spin_unlock_irqrestore(&upriv->req_lock, flags);
  432. ctx->state = EZUSB_CTX_QUEUED;
  433. ezusb_req_queue_run(upriv);
  434. done:
  435. return;
  436. }
  437. static void ezusb_request_out_callback(struct urb *urb)
  438. {
  439. unsigned long flags;
  440. enum ezusb_state state;
  441. struct request_context *ctx = urb->context;
  442. struct ezusb_priv *upriv = ctx->upriv;
  443. spin_lock_irqsave(&upriv->req_lock, flags);
  444. del_timer(&ctx->timer);
  445. if (ctx->killed) {
  446. spin_unlock_irqrestore(&upriv->req_lock, flags);
  447. pr_warn("interrupt called with dead ctx\n");
  448. goto out;
  449. }
  450. state = ctx->state;
  451. if (urb->status == 0) {
  452. switch (state) {
  453. case EZUSB_CTX_REQ_SUBMITTED:
  454. if (ctx->in_rid) {
  455. ctx->state = EZUSB_CTX_REQ_COMPLETE;
  456. /* reply URB still pending */
  457. ezusb_mod_timer(upriv, &ctx->timer,
  458. jiffies + DEF_TIMEOUT);
  459. spin_unlock_irqrestore(&upriv->req_lock,
  460. flags);
  461. break;
  462. }
  463. /* fall through */
  464. case EZUSB_CTX_RESP_RECEIVED:
  465. /* IN already received before this OUT-ACK */
  466. ctx->state = EZUSB_CTX_COMPLETE;
  467. spin_unlock_irqrestore(&upriv->req_lock, flags);
  468. ezusb_ctx_complete(ctx);
  469. break;
  470. default:
  471. spin_unlock_irqrestore(&upriv->req_lock, flags);
  472. err("Unexpected state(0x%x, %d) in OUT URB",
  473. state, urb->status);
  474. break;
  475. }
  476. } else {
  477. /* If someone cancels the OUT URB then its status
  478. * should be either -ECONNRESET or -ENOENT.
  479. */
  480. switch (state) {
  481. case EZUSB_CTX_REQ_SUBMITTED:
  482. case EZUSB_CTX_RESP_RECEIVED:
  483. ctx->state = EZUSB_CTX_REQ_FAILED;
  484. /* fall through */
  485. case EZUSB_CTX_REQ_FAILED:
  486. case EZUSB_CTX_REQ_TIMEOUT:
  487. spin_unlock_irqrestore(&upriv->req_lock, flags);
  488. ezusb_ctx_complete(ctx);
  489. break;
  490. default:
  491. spin_unlock_irqrestore(&upriv->req_lock, flags);
  492. err("Unexpected state(0x%x, %d) in OUT URB",
  493. state, urb->status);
  494. break;
  495. }
  496. }
  497. out:
  498. ezusb_request_context_put(ctx);
  499. }
  500. static void ezusb_request_in_callback(struct ezusb_priv *upriv,
  501. struct urb *urb)
  502. {
  503. struct ezusb_packet *ans = urb->transfer_buffer;
  504. struct request_context *ctx = NULL;
  505. enum ezusb_state state;
  506. unsigned long flags;
  507. /* Find the CTX on the active queue that requested this URB */
  508. spin_lock_irqsave(&upriv->req_lock, flags);
  509. if (upriv->udev) {
  510. struct list_head *item;
  511. list_for_each(item, &upriv->req_active) {
  512. struct request_context *c;
  513. int reply_count;
  514. c = list_entry(item, struct request_context, list);
  515. reply_count =
  516. ezusb_reply_inc(c->buf->req_reply_count);
  517. if ((ans->ans_reply_count == reply_count)
  518. && (le16_to_cpu(ans->hermes_rid) == c->in_rid)) {
  519. ctx = c;
  520. break;
  521. }
  522. netdev_dbg(upriv->dev, "Skipped (0x%x/0x%x) (%d/%d)\n",
  523. le16_to_cpu(ans->hermes_rid), c->in_rid,
  524. ans->ans_reply_count, reply_count);
  525. }
  526. }
  527. if (ctx == NULL) {
  528. spin_unlock_irqrestore(&upriv->req_lock, flags);
  529. err("%s: got unexpected RID: 0x%04X", __func__,
  530. le16_to_cpu(ans->hermes_rid));
  531. ezusb_req_queue_run(upriv);
  532. return;
  533. }
  534. /* The data we want is in the in buffer, exchange */
  535. urb->transfer_buffer = ctx->buf;
  536. ctx->buf = (void *) ans;
  537. ctx->buf_length = urb->actual_length;
  538. state = ctx->state;
  539. switch (state) {
  540. case EZUSB_CTX_REQ_SUBMITTED:
  541. /* We have received our response URB before
  542. * our request has been acknowledged. Do NOT
  543. * destroy our CTX yet, because our OUT URB
  544. * is still alive ...
  545. */
  546. ctx->state = EZUSB_CTX_RESP_RECEIVED;
  547. spin_unlock_irqrestore(&upriv->req_lock, flags);
  548. /* Let the machine continue running. */
  549. break;
  550. case EZUSB_CTX_REQ_COMPLETE:
  551. /* This is the usual path: our request
  552. * has already been acknowledged, and
  553. * we have now received the reply.
  554. */
  555. ctx->state = EZUSB_CTX_COMPLETE;
  556. /* Stop the intimer */
  557. del_timer(&ctx->timer);
  558. spin_unlock_irqrestore(&upriv->req_lock, flags);
  559. /* Call the completion handler */
  560. ezusb_ctx_complete(ctx);
  561. break;
  562. default:
  563. spin_unlock_irqrestore(&upriv->req_lock, flags);
  564. pr_warn("Matched IN URB, unexpected context state(0x%x)\n",
  565. state);
  566. /* Throw this CTX away and try submitting another */
  567. del_timer(&ctx->timer);
  568. ctx->outurb->transfer_flags |= URB_ASYNC_UNLINK;
  569. usb_unlink_urb(ctx->outurb);
  570. ezusb_req_queue_run(upriv);
  571. break;
  572. } /* switch */
  573. }
  574. static void ezusb_req_ctx_wait(struct ezusb_priv *upriv,
  575. struct request_context *ctx)
  576. {
  577. switch (ctx->state) {
  578. case EZUSB_CTX_QUEUED:
  579. case EZUSB_CTX_REQ_SUBMITTED:
  580. case EZUSB_CTX_REQ_COMPLETE:
  581. case EZUSB_CTX_RESP_RECEIVED:
  582. if (in_softirq()) {
  583. /* If we get called from a timer, timeout timers don't
  584. * get the chance to run themselves. So we make sure
  585. * that we don't sleep for ever */
  586. int msecs = DEF_TIMEOUT * (1000 / HZ);
  587. while (!ctx->done.done && msecs--)
  588. udelay(1000);
  589. } else {
  590. wait_event_interruptible(ctx->done.wait,
  591. ctx->done.done);
  592. }
  593. break;
  594. default:
  595. /* Done or failed - nothing to wait for */
  596. break;
  597. }
  598. }
  599. static inline u16 build_crc(struct ezusb_packet *data)
  600. {
  601. u16 crc = 0;
  602. u8 *bytes = (u8 *)data;
  603. int i;
  604. for (i = 0; i < 8; i++)
  605. crc = (crc << 1) + bytes[i];
  606. return crc;
  607. }
  608. /**
  609. * ezusb_fill_req:
  610. *
  611. * if data == NULL and length > 0 the data is assumed to be already in
  612. * the target buffer and only the header is filled.
  613. *
  614. */
  615. static int ezusb_fill_req(struct ezusb_packet *req, u16 length, u16 rid,
  616. const void *data, u16 frame_type, u8 reply_count)
  617. {
  618. int total_size = sizeof(*req) + length;
  619. BUG_ON(total_size > BULK_BUF_SIZE);
  620. req->magic = cpu_to_le16(EZUSB_MAGIC);
  621. req->req_reply_count = reply_count;
  622. req->ans_reply_count = 0;
  623. req->frame_type = cpu_to_le16(frame_type);
  624. req->size = cpu_to_le16(length + 4);
  625. req->crc = cpu_to_le16(build_crc(req));
  626. req->hermes_len = cpu_to_le16(HERMES_BYTES_TO_RECLEN(length));
  627. req->hermes_rid = cpu_to_le16(rid);
  628. if (data)
  629. memcpy(req->data, data, length);
  630. return total_size;
  631. }
  632. static int ezusb_submit_in_urb(struct ezusb_priv *upriv)
  633. {
  634. int retval = 0;
  635. void *cur_buf = upriv->read_urb->transfer_buffer;
  636. if (upriv->read_urb->status == -EINPROGRESS) {
  637. netdev_dbg(upriv->dev, "urb busy, not resubmiting\n");
  638. retval = -EBUSY;
  639. goto exit;
  640. }
  641. usb_fill_bulk_urb(upriv->read_urb, upriv->udev, upriv->read_pipe,
  642. cur_buf, BULK_BUF_SIZE,
  643. ezusb_bulk_in_callback, upriv);
  644. upriv->read_urb->transfer_flags = 0;
  645. retval = usb_submit_urb(upriv->read_urb, GFP_ATOMIC);
  646. if (retval)
  647. err("%s submit failed %d", __func__, retval);
  648. exit:
  649. return retval;
  650. }
  651. static inline int ezusb_8051_cpucs(struct ezusb_priv *upriv, int reset)
  652. {
  653. int ret;
  654. u8 *res_val = NULL;
  655. if (!upriv->udev) {
  656. err("%s: !upriv->udev", __func__);
  657. return -EFAULT;
  658. }
  659. res_val = kmalloc(sizeof(*res_val), GFP_KERNEL);
  660. if (!res_val)
  661. return -ENOMEM;
  662. *res_val = reset; /* avoid argument promotion */
  663. ret = usb_control_msg(upriv->udev,
  664. usb_sndctrlpipe(upriv->udev, 0),
  665. EZUSB_REQUEST_FW_TRANS,
  666. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  667. USB_DIR_OUT, EZUSB_CPUCS_REG, 0, res_val,
  668. sizeof(*res_val), DEF_TIMEOUT);
  669. kfree(res_val);
  670. return ret;
  671. }
  672. static int ezusb_firmware_download(struct ezusb_priv *upriv,
  673. struct ez_usb_fw *fw)
  674. {
  675. u8 *fw_buffer;
  676. int retval, addr;
  677. int variant_offset;
  678. fw_buffer = kmalloc(FW_BUF_SIZE, GFP_KERNEL);
  679. if (!fw_buffer) {
  680. printk(KERN_ERR PFX "Out of memory for firmware buffer.\n");
  681. return -ENOMEM;
  682. }
  683. /*
  684. * This byte is 1 and should be replaced with 0. The offset is
  685. * 0x10AD in version 0.0.6. The byte in question should follow
  686. * the end of the code pointed to by the jump in the beginning
  687. * of the firmware. Also, it is read by code located at 0x358.
  688. */
  689. variant_offset = be16_to_cpup((__be16 *) &fw->code[FW_VAR_OFFSET_PTR]);
  690. if (variant_offset >= fw->size) {
  691. printk(KERN_ERR PFX "Invalid firmware variant offset: "
  692. "0x%04x\n", variant_offset);
  693. retval = -EINVAL;
  694. goto fail;
  695. }
  696. retval = ezusb_8051_cpucs(upriv, 1);
  697. if (retval < 0)
  698. goto fail;
  699. for (addr = 0; addr < fw->size; addr += FW_BUF_SIZE) {
  700. /* 0x100-0x300 should be left alone, it contains card
  701. * specific data, like USB enumeration information */
  702. if ((addr >= FW_HOLE_START) && (addr < FW_HOLE_END))
  703. continue;
  704. memcpy(fw_buffer, &fw->code[addr], FW_BUF_SIZE);
  705. if (variant_offset >= addr &&
  706. variant_offset < addr + FW_BUF_SIZE) {
  707. netdev_dbg(upriv->dev,
  708. "Patching card_variant byte at 0x%04X\n",
  709. variant_offset);
  710. fw_buffer[variant_offset - addr] = FW_VAR_VALUE;
  711. }
  712. retval = usb_control_msg(upriv->udev,
  713. usb_sndctrlpipe(upriv->udev, 0),
  714. EZUSB_REQUEST_FW_TRANS,
  715. USB_TYPE_VENDOR | USB_RECIP_DEVICE
  716. | USB_DIR_OUT,
  717. addr, 0x0,
  718. fw_buffer, FW_BUF_SIZE,
  719. DEF_TIMEOUT);
  720. if (retval < 0)
  721. goto fail;
  722. }
  723. retval = ezusb_8051_cpucs(upriv, 0);
  724. if (retval < 0)
  725. goto fail;
  726. goto exit;
  727. fail:
  728. printk(KERN_ERR PFX "Firmware download failed, error %d\n",
  729. retval);
  730. exit:
  731. kfree(fw_buffer);
  732. return retval;
  733. }
  734. static int ezusb_access_ltv(struct ezusb_priv *upriv,
  735. struct request_context *ctx,
  736. u16 length, const void *data, u16 frame_type,
  737. void *ans_buff, unsigned ans_size, u16 *ans_length)
  738. {
  739. int req_size;
  740. int retval = 0;
  741. enum ezusb_state state;
  742. BUG_ON(in_irq());
  743. if (!upriv->udev) {
  744. retval = -ENODEV;
  745. goto exit;
  746. }
  747. if (upriv->read_urb->status != -EINPROGRESS)
  748. err("%s: in urb not pending", __func__);
  749. /* protect upriv->reply_count, guarantee sequential numbers */
  750. spin_lock_bh(&upriv->reply_count_lock);
  751. req_size = ezusb_fill_req(ctx->buf, length, ctx->out_rid, data,
  752. frame_type, upriv->reply_count);
  753. usb_fill_bulk_urb(ctx->outurb, upriv->udev, upriv->write_pipe,
  754. ctx->buf, req_size,
  755. ezusb_request_out_callback, ctx);
  756. if (ctx->in_rid)
  757. upriv->reply_count = ezusb_reply_inc(upriv->reply_count);
  758. ezusb_req_enqueue_run(upriv, ctx);
  759. spin_unlock_bh(&upriv->reply_count_lock);
  760. if (ctx->in_rid)
  761. ezusb_req_ctx_wait(upriv, ctx);
  762. state = ctx->state;
  763. switch (state) {
  764. case EZUSB_CTX_COMPLETE:
  765. retval = ctx->outurb->status;
  766. break;
  767. case EZUSB_CTX_QUEUED:
  768. case EZUSB_CTX_REQ_SUBMITTED:
  769. if (!ctx->in_rid)
  770. break;
  771. default:
  772. err("%s: Unexpected context state %d", __func__,
  773. state);
  774. /* fall though */
  775. case EZUSB_CTX_REQ_TIMEOUT:
  776. case EZUSB_CTX_REQ_FAILED:
  777. case EZUSB_CTX_RESP_TIMEOUT:
  778. case EZUSB_CTX_REQSUBMIT_FAIL:
  779. printk(KERN_ERR PFX "Access failed, resetting (state %d,"
  780. " reply_count %d)\n", state, upriv->reply_count);
  781. upriv->reply_count = 0;
  782. if (state == EZUSB_CTX_REQ_TIMEOUT
  783. || state == EZUSB_CTX_RESP_TIMEOUT) {
  784. printk(KERN_ERR PFX "ctx timed out\n");
  785. retval = -ETIMEDOUT;
  786. } else {
  787. printk(KERN_ERR PFX "ctx failed\n");
  788. retval = -EFAULT;
  789. }
  790. goto exit;
  791. }
  792. if (ctx->in_rid) {
  793. struct ezusb_packet *ans = ctx->buf;
  794. unsigned exp_len;
  795. if (ans->hermes_len != 0)
  796. exp_len = le16_to_cpu(ans->hermes_len) * 2 + 12;
  797. else
  798. exp_len = 14;
  799. if (exp_len != ctx->buf_length) {
  800. err("%s: length mismatch for RID 0x%04x: "
  801. "expected %d, got %d", __func__,
  802. ctx->in_rid, exp_len, ctx->buf_length);
  803. retval = -EIO;
  804. goto exit;
  805. }
  806. if (ans_buff)
  807. memcpy(ans_buff, ans->data, min(exp_len, ans_size));
  808. if (ans_length)
  809. *ans_length = le16_to_cpu(ans->hermes_len);
  810. }
  811. exit:
  812. ezusb_request_context_put(ctx);
  813. return retval;
  814. }
  815. static int ezusb_write_ltv(struct hermes *hw, int bap, u16 rid,
  816. u16 length, const void *data)
  817. {
  818. struct ezusb_priv *upriv = hw->priv;
  819. u16 frame_type;
  820. struct request_context *ctx;
  821. if (length == 0)
  822. return -EINVAL;
  823. length = HERMES_RECLEN_TO_BYTES(length);
  824. /* On memory mapped devices HERMES_RID_CNFGROUPADDRESSES can be
  825. * set to be empty, but the USB bridge doesn't like it */
  826. if (length == 0)
  827. return 0;
  828. ctx = ezusb_alloc_ctx(upriv, rid, EZUSB_RID_ACK);
  829. if (!ctx)
  830. return -ENOMEM;
  831. if (rid == EZUSB_RID_TX)
  832. frame_type = EZUSB_FRAME_DATA;
  833. else
  834. frame_type = EZUSB_FRAME_CONTROL;
  835. return ezusb_access_ltv(upriv, ctx, length, data, frame_type,
  836. NULL, 0, NULL);
  837. }
  838. static int ezusb_read_ltv(struct hermes *hw, int bap, u16 rid,
  839. unsigned bufsize, u16 *length, void *buf)
  840. {
  841. struct ezusb_priv *upriv = hw->priv;
  842. struct request_context *ctx;
  843. if (bufsize % 2)
  844. return -EINVAL;
  845. ctx = ezusb_alloc_ctx(upriv, rid, rid);
  846. if (!ctx)
  847. return -ENOMEM;
  848. return ezusb_access_ltv(upriv, ctx, 0, NULL, EZUSB_FRAME_CONTROL,
  849. buf, bufsize, length);
  850. }
  851. static int ezusb_doicmd_wait(struct hermes *hw, u16 cmd, u16 parm0, u16 parm1,
  852. u16 parm2, struct hermes_response *resp)
  853. {
  854. struct ezusb_priv *upriv = hw->priv;
  855. struct request_context *ctx;
  856. __le16 data[4] = {
  857. cpu_to_le16(cmd),
  858. cpu_to_le16(parm0),
  859. cpu_to_le16(parm1),
  860. cpu_to_le16(parm2),
  861. };
  862. netdev_dbg(upriv->dev,
  863. "0x%04X, parm0 0x%04X, parm1 0x%04X, parm2 0x%04X\n", cmd,
  864. parm0, parm1, parm2);
  865. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_DOCMD, EZUSB_RID_ACK);
  866. if (!ctx)
  867. return -ENOMEM;
  868. return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  869. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  870. }
  871. static int ezusb_docmd_wait(struct hermes *hw, u16 cmd, u16 parm0,
  872. struct hermes_response *resp)
  873. {
  874. struct ezusb_priv *upriv = hw->priv;
  875. struct request_context *ctx;
  876. __le16 data[4] = {
  877. cpu_to_le16(cmd),
  878. cpu_to_le16(parm0),
  879. 0,
  880. 0,
  881. };
  882. netdev_dbg(upriv->dev, "0x%04X, parm0 0x%04X\n", cmd, parm0);
  883. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_DOCMD, EZUSB_RID_ACK);
  884. if (!ctx)
  885. return -ENOMEM;
  886. return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  887. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  888. }
  889. static int ezusb_bap_pread(struct hermes *hw, int bap,
  890. void *buf, int len, u16 id, u16 offset)
  891. {
  892. struct ezusb_priv *upriv = hw->priv;
  893. struct ezusb_packet *ans = (void *) upriv->read_urb->transfer_buffer;
  894. int actual_length = upriv->read_urb->actual_length;
  895. if (id == EZUSB_RID_RX) {
  896. if ((sizeof(*ans) + offset + len) > actual_length) {
  897. printk(KERN_ERR PFX "BAP read beyond buffer end "
  898. "in rx frame\n");
  899. return -EINVAL;
  900. }
  901. memcpy(buf, ans->data + offset, len);
  902. return 0;
  903. }
  904. if (EZUSB_IS_INFO(id)) {
  905. /* Include 4 bytes for length/type */
  906. if ((sizeof(*ans) + offset + len - 4) > actual_length) {
  907. printk(KERN_ERR PFX "BAP read beyond buffer end "
  908. "in info frame\n");
  909. return -EFAULT;
  910. }
  911. memcpy(buf, ans->data + offset - 4, len);
  912. } else {
  913. printk(KERN_ERR PFX "Unexpected fid 0x%04x\n", id);
  914. return -EINVAL;
  915. }
  916. return 0;
  917. }
  918. static int ezusb_read_pda(struct hermes *hw, __le16 *pda,
  919. u32 pda_addr, u16 pda_len)
  920. {
  921. struct ezusb_priv *upriv = hw->priv;
  922. struct request_context *ctx;
  923. __le16 data[] = {
  924. cpu_to_le16(pda_addr & 0xffff),
  925. cpu_to_le16(pda_len - 4)
  926. };
  927. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_READ_PDA, EZUSB_RID_READ_PDA);
  928. if (!ctx)
  929. return -ENOMEM;
  930. /* wl_lkm does not include PDA size in the PDA area.
  931. * We will pad the information into pda, so other routines
  932. * don't have to be modified */
  933. pda[0] = cpu_to_le16(pda_len - 2);
  934. /* Includes CFG_PROD_DATA but not itself */
  935. pda[1] = cpu_to_le16(0x0800); /* CFG_PROD_DATA */
  936. return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  937. EZUSB_FRAME_CONTROL, &pda[2], pda_len - 4,
  938. NULL);
  939. }
  940. static int ezusb_program_init(struct hermes *hw, u32 entry_point)
  941. {
  942. struct ezusb_priv *upriv = hw->priv;
  943. struct request_context *ctx;
  944. __le32 data = cpu_to_le32(entry_point);
  945. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_INIT, EZUSB_RID_ACK);
  946. if (!ctx)
  947. return -ENOMEM;
  948. return ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  949. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  950. }
  951. static int ezusb_program_end(struct hermes *hw)
  952. {
  953. struct ezusb_priv *upriv = hw->priv;
  954. struct request_context *ctx;
  955. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_END, EZUSB_RID_ACK);
  956. if (!ctx)
  957. return -ENOMEM;
  958. return ezusb_access_ltv(upriv, ctx, 0, NULL,
  959. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  960. }
  961. static int ezusb_program_bytes(struct hermes *hw, const char *buf,
  962. u32 addr, u32 len)
  963. {
  964. struct ezusb_priv *upriv = hw->priv;
  965. struct request_context *ctx;
  966. __le32 data = cpu_to_le32(addr);
  967. int err;
  968. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_SET_ADDR, EZUSB_RID_ACK);
  969. if (!ctx)
  970. return -ENOMEM;
  971. err = ezusb_access_ltv(upriv, ctx, sizeof(data), &data,
  972. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  973. if (err)
  974. return err;
  975. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_PROG_BYTES, EZUSB_RID_ACK);
  976. if (!ctx)
  977. return -ENOMEM;
  978. return ezusb_access_ltv(upriv, ctx, len, buf,
  979. EZUSB_FRAME_CONTROL, NULL, 0, NULL);
  980. }
  981. static int ezusb_program(struct hermes *hw, const char *buf,
  982. u32 addr, u32 len)
  983. {
  984. u32 ch_addr;
  985. u32 ch_len;
  986. int err = 0;
  987. /* We can only send 2048 bytes out of the bulk xmit at a time,
  988. * so we have to split any programming into chunks of <2048
  989. * bytes. */
  990. ch_len = (len < MAX_DL_SIZE) ? len : MAX_DL_SIZE;
  991. ch_addr = addr;
  992. while (ch_addr < (addr + len)) {
  993. pr_debug("Programming subblock of length %d "
  994. "to address 0x%08x. Data @ %p\n",
  995. ch_len, ch_addr, &buf[ch_addr - addr]);
  996. err = ezusb_program_bytes(hw, &buf[ch_addr - addr],
  997. ch_addr, ch_len);
  998. if (err)
  999. break;
  1000. ch_addr += ch_len;
  1001. ch_len = ((addr + len - ch_addr) < MAX_DL_SIZE) ?
  1002. (addr + len - ch_addr) : MAX_DL_SIZE;
  1003. }
  1004. return err;
  1005. }
  1006. static netdev_tx_t ezusb_xmit(struct sk_buff *skb, struct net_device *dev)
  1007. {
  1008. struct orinoco_private *priv = ndev_priv(dev);
  1009. struct net_device_stats *stats = &dev->stats;
  1010. struct ezusb_priv *upriv = priv->card;
  1011. u8 mic[MICHAEL_MIC_LEN + 1];
  1012. int err = 0;
  1013. int tx_control;
  1014. unsigned long flags;
  1015. struct request_context *ctx;
  1016. u8 *buf;
  1017. int tx_size;
  1018. if (!netif_running(dev)) {
  1019. printk(KERN_ERR "%s: Tx on stopped device!\n",
  1020. dev->name);
  1021. return NETDEV_TX_BUSY;
  1022. }
  1023. if (netif_queue_stopped(dev)) {
  1024. printk(KERN_DEBUG "%s: Tx while transmitter busy!\n",
  1025. dev->name);
  1026. return NETDEV_TX_BUSY;
  1027. }
  1028. if (orinoco_lock(priv, &flags) != 0) {
  1029. printk(KERN_ERR
  1030. "%s: ezusb_xmit() called while hw_unavailable\n",
  1031. dev->name);
  1032. return NETDEV_TX_BUSY;
  1033. }
  1034. if (!netif_carrier_ok(dev) ||
  1035. (priv->iw_mode == NL80211_IFTYPE_MONITOR)) {
  1036. /* Oops, the firmware hasn't established a connection,
  1037. silently drop the packet (this seems to be the
  1038. safest approach). */
  1039. goto drop;
  1040. }
  1041. /* Check packet length */
  1042. if (skb->len < ETH_HLEN)
  1043. goto drop;
  1044. ctx = ezusb_alloc_ctx(upriv, EZUSB_RID_TX, 0);
  1045. if (!ctx)
  1046. goto busy;
  1047. memset(ctx->buf, 0, BULK_BUF_SIZE);
  1048. buf = ctx->buf->data;
  1049. tx_control = 0;
  1050. err = orinoco_process_xmit_skb(skb, dev, priv, &tx_control,
  1051. &mic[0]);
  1052. if (err)
  1053. goto drop;
  1054. {
  1055. __le16 *tx_cntl = (__le16 *)buf;
  1056. *tx_cntl = cpu_to_le16(tx_control);
  1057. buf += sizeof(*tx_cntl);
  1058. }
  1059. memcpy(buf, skb->data, skb->len);
  1060. buf += skb->len;
  1061. if (tx_control & HERMES_TXCTRL_MIC) {
  1062. u8 *m = mic;
  1063. /* Mic has been offset so it can be copied to an even
  1064. * address. We're copying eveything anyway, so we
  1065. * don't need to copy that first byte. */
  1066. if (skb->len % 2)
  1067. m++;
  1068. memcpy(buf, m, MICHAEL_MIC_LEN);
  1069. buf += MICHAEL_MIC_LEN;
  1070. }
  1071. /* Finally, we actually initiate the send */
  1072. netif_stop_queue(dev);
  1073. /* The card may behave better if we send evenly sized usb transfers */
  1074. tx_size = ALIGN(buf - ctx->buf->data, 2);
  1075. err = ezusb_access_ltv(upriv, ctx, tx_size, NULL,
  1076. EZUSB_FRAME_DATA, NULL, 0, NULL);
  1077. if (err) {
  1078. netif_start_queue(dev);
  1079. if (net_ratelimit())
  1080. printk(KERN_ERR "%s: Error %d transmitting packet\n",
  1081. dev->name, err);
  1082. goto busy;
  1083. }
  1084. netif_trans_update(dev);
  1085. stats->tx_bytes += skb->len;
  1086. goto ok;
  1087. drop:
  1088. stats->tx_errors++;
  1089. stats->tx_dropped++;
  1090. ok:
  1091. orinoco_unlock(priv, &flags);
  1092. dev_kfree_skb(skb);
  1093. return NETDEV_TX_OK;
  1094. busy:
  1095. orinoco_unlock(priv, &flags);
  1096. return NETDEV_TX_BUSY;
  1097. }
  1098. static int ezusb_allocate(struct hermes *hw, u16 size, u16 *fid)
  1099. {
  1100. *fid = EZUSB_RID_TX;
  1101. return 0;
  1102. }
  1103. static int ezusb_hard_reset(struct orinoco_private *priv)
  1104. {
  1105. struct ezusb_priv *upriv = priv->card;
  1106. int retval = ezusb_8051_cpucs(upriv, 1);
  1107. if (retval < 0) {
  1108. err("Failed to reset");
  1109. return retval;
  1110. }
  1111. retval = ezusb_8051_cpucs(upriv, 0);
  1112. if (retval < 0) {
  1113. err("Failed to unreset");
  1114. return retval;
  1115. }
  1116. netdev_dbg(upriv->dev, "sending control message\n");
  1117. retval = usb_control_msg(upriv->udev,
  1118. usb_sndctrlpipe(upriv->udev, 0),
  1119. EZUSB_REQUEST_TRIGER,
  1120. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  1121. USB_DIR_OUT, 0x0, 0x0, NULL, 0,
  1122. DEF_TIMEOUT);
  1123. if (retval < 0) {
  1124. err("EZUSB_REQUEST_TRIGER failed retval %d", retval);
  1125. return retval;
  1126. }
  1127. #if 0
  1128. dbg("Sending EZUSB_REQUEST_TRIG_AC");
  1129. retval = usb_control_msg(upriv->udev,
  1130. usb_sndctrlpipe(upriv->udev, 0),
  1131. EZUSB_REQUEST_TRIG_AC,
  1132. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  1133. USB_DIR_OUT, 0x00FA, 0x0, NULL, 0,
  1134. DEF_TIMEOUT);
  1135. if (retval < 0) {
  1136. err("EZUSB_REQUEST_TRIG_AC failed retval %d", retval);
  1137. return retval;
  1138. }
  1139. #endif
  1140. return 0;
  1141. }
  1142. static int ezusb_init(struct hermes *hw)
  1143. {
  1144. struct ezusb_priv *upriv = hw->priv;
  1145. int retval;
  1146. BUG_ON(in_interrupt());
  1147. BUG_ON(!upriv);
  1148. upriv->reply_count = 0;
  1149. /* Write the MAGIC number on the simulated registers to keep
  1150. * orinoco.c happy */
  1151. hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
  1152. hermes_write_regn(hw, RXFID, EZUSB_RID_RX);
  1153. usb_kill_urb(upriv->read_urb);
  1154. ezusb_submit_in_urb(upriv);
  1155. retval = ezusb_write_ltv(hw, 0, EZUSB_RID_INIT1,
  1156. HERMES_BYTES_TO_RECLEN(2), "\x10\x00");
  1157. if (retval < 0) {
  1158. printk(KERN_ERR PFX "EZUSB_RID_INIT1 error %d\n", retval);
  1159. return retval;
  1160. }
  1161. retval = ezusb_docmd_wait(hw, HERMES_CMD_INIT, 0, NULL);
  1162. if (retval < 0) {
  1163. printk(KERN_ERR PFX "HERMES_CMD_INIT error %d\n", retval);
  1164. return retval;
  1165. }
  1166. return 0;
  1167. }
  1168. static void ezusb_bulk_in_callback(struct urb *urb)
  1169. {
  1170. struct ezusb_priv *upriv = (struct ezusb_priv *) urb->context;
  1171. struct ezusb_packet *ans = urb->transfer_buffer;
  1172. u16 crc;
  1173. u16 hermes_rid;
  1174. if (upriv->udev == NULL)
  1175. return;
  1176. if (urb->status == -ETIMEDOUT) {
  1177. /* When a device gets unplugged we get this every time
  1178. * we resubmit, flooding the logs. Since we don't use
  1179. * USB timeouts, it shouldn't happen any other time*/
  1180. pr_warn("%s: urb timed out, not resubmitting\n", __func__);
  1181. return;
  1182. }
  1183. if (urb->status == -ECONNABORTED) {
  1184. pr_warn("%s: connection abort, resubmitting urb\n",
  1185. __func__);
  1186. goto resubmit;
  1187. }
  1188. if ((urb->status == -EILSEQ)
  1189. || (urb->status == -ENOENT)
  1190. || (urb->status == -ECONNRESET)) {
  1191. netdev_dbg(upriv->dev, "status %d, not resubmiting\n",
  1192. urb->status);
  1193. return;
  1194. }
  1195. if (urb->status)
  1196. netdev_dbg(upriv->dev, "status: %d length: %d\n",
  1197. urb->status, urb->actual_length);
  1198. if (urb->actual_length < sizeof(*ans)) {
  1199. err("%s: short read, ignoring", __func__);
  1200. goto resubmit;
  1201. }
  1202. crc = build_crc(ans);
  1203. if (le16_to_cpu(ans->crc) != crc) {
  1204. err("CRC error, ignoring packet");
  1205. goto resubmit;
  1206. }
  1207. hermes_rid = le16_to_cpu(ans->hermes_rid);
  1208. if ((hermes_rid != EZUSB_RID_RX) && !EZUSB_IS_INFO(hermes_rid)) {
  1209. ezusb_request_in_callback(upriv, urb);
  1210. } else if (upriv->dev) {
  1211. struct net_device *dev = upriv->dev;
  1212. struct orinoco_private *priv = ndev_priv(dev);
  1213. struct hermes *hw = &priv->hw;
  1214. if (hermes_rid == EZUSB_RID_RX) {
  1215. __orinoco_ev_rx(dev, hw);
  1216. } else {
  1217. hermes_write_regn(hw, INFOFID,
  1218. le16_to_cpu(ans->hermes_rid));
  1219. __orinoco_ev_info(dev, hw);
  1220. }
  1221. }
  1222. resubmit:
  1223. if (upriv->udev)
  1224. ezusb_submit_in_urb(upriv);
  1225. }
  1226. static inline void ezusb_delete(struct ezusb_priv *upriv)
  1227. {
  1228. struct net_device *dev;
  1229. struct list_head *item;
  1230. struct list_head *tmp_item;
  1231. unsigned long flags;
  1232. BUG_ON(in_interrupt());
  1233. BUG_ON(!upriv);
  1234. dev = upriv->dev;
  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. atomic_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->altsetting[0].desc;
  1355. for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
  1356. ep = &interface->altsetting[0].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. err("No firmware to download");
  1416. goto error;
  1417. }
  1418. if (ezusb_hard_reset(priv) < 0) {
  1419. err("Cannot reset the device");
  1420. goto error;
  1421. }
  1422. /* If the firmware is already downloaded orinoco.c will call
  1423. * ezusb_init but if the firmware is not already there, that will make
  1424. * the kernel very unstable, so we try initializing here and quit in
  1425. * case of error */
  1426. if (ezusb_init(hw) < 0) {
  1427. err("Couldn't initialize the device");
  1428. err("Firmware may not be downloaded or may be wrong.");
  1429. goto error;
  1430. }
  1431. /* Initialise the main driver */
  1432. if (orinoco_init(priv) != 0) {
  1433. err("orinoco_init() failed\n");
  1434. goto error;
  1435. }
  1436. if (orinoco_if_add(priv, 0, 0, &ezusb_netdev_ops) != 0) {
  1437. upriv->dev = NULL;
  1438. err("%s: orinoco_if_add() failed", __func__);
  1439. wiphy_unregister(priv_to_wiphy(priv));
  1440. goto error;
  1441. }
  1442. upriv->dev = priv->ndev;
  1443. goto exit;
  1444. error:
  1445. ezusb_delete(upriv);
  1446. if (upriv->dev) {
  1447. /* upriv->dev was 0, so ezusb_delete() didn't free it */
  1448. free_orinocodev(priv);
  1449. }
  1450. upriv = NULL;
  1451. retval = -EFAULT;
  1452. exit:
  1453. if (fw_entry) {
  1454. firmware.code = NULL;
  1455. firmware.size = 0;
  1456. release_firmware(fw_entry);
  1457. }
  1458. usb_set_intfdata(interface, upriv);
  1459. return retval;
  1460. }
  1461. static void ezusb_disconnect(struct usb_interface *intf)
  1462. {
  1463. struct ezusb_priv *upriv = usb_get_intfdata(intf);
  1464. usb_set_intfdata(intf, NULL);
  1465. ezusb_delete(upriv);
  1466. printk(KERN_INFO PFX "Disconnected\n");
  1467. }
  1468. /* usb specific object needed to register this driver with the usb subsystem */
  1469. static struct usb_driver orinoco_driver = {
  1470. .name = DRIVER_NAME,
  1471. .probe = ezusb_probe,
  1472. .disconnect = ezusb_disconnect,
  1473. .id_table = ezusb_table,
  1474. .disable_hub_initiated_lpm = 1,
  1475. };
  1476. module_usb_driver(orinoco_driver);
  1477. MODULE_AUTHOR("Manuel Estrada Sainz");
  1478. MODULE_DESCRIPTION("Driver for Orinoco wireless LAN cards using EZUSB bridge");
  1479. MODULE_LICENSE("Dual MPL/GPL");