zd1201.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. /*
  2. * Driver for ZyDAS zd1201 based wireless USB devices.
  3. *
  4. * Copyright (c) 2004, 2005 Jeroen Vreeken (pe1rxq@amsat.org)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * Parts of this driver have been derived from a wlan-ng version
  11. * modified by ZyDAS. They also made documentation available, thanks!
  12. * Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/usb.h>
  16. #include <linux/slab.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/wireless.h>
  20. #include <net/cfg80211.h>
  21. #include <net/iw_handler.h>
  22. #include <linux/string.h>
  23. #include <linux/if_arp.h>
  24. #include <linux/firmware.h>
  25. #include "zd1201.h"
  26. static struct usb_device_id zd1201_table[] = {
  27. {USB_DEVICE(0x0586, 0x3400)}, /* Peabird Wireless USB Adapter */
  28. {USB_DEVICE(0x0ace, 0x1201)}, /* ZyDAS ZD1201 Wireless USB Adapter */
  29. {USB_DEVICE(0x050d, 0x6051)}, /* Belkin F5D6051 usb adapter */
  30. {USB_DEVICE(0x0db0, 0x6823)}, /* MSI UB11B usb adapter */
  31. {USB_DEVICE(0x1044, 0x8004)}, /* Gigabyte GN-WLBZ101 */
  32. {USB_DEVICE(0x1044, 0x8005)}, /* GIGABYTE GN-WLBZ201 usb adapter */
  33. {}
  34. };
  35. static int ap; /* Are we an AP or a normal station? */
  36. #define ZD1201_VERSION "0.15"
  37. MODULE_AUTHOR("Jeroen Vreeken <pe1rxq@amsat.org>");
  38. MODULE_DESCRIPTION("Driver for ZyDAS ZD1201 based USB Wireless adapters");
  39. MODULE_VERSION(ZD1201_VERSION);
  40. MODULE_LICENSE("GPL");
  41. module_param(ap, int, 0);
  42. MODULE_PARM_DESC(ap, "If non-zero Access Point firmware will be loaded");
  43. MODULE_DEVICE_TABLE(usb, zd1201_table);
  44. static int zd1201_fw_upload(struct usb_device *dev, int apfw)
  45. {
  46. const struct firmware *fw_entry;
  47. const char *data;
  48. unsigned long len;
  49. int err;
  50. unsigned char ret;
  51. char *buf;
  52. char *fwfile;
  53. if (apfw)
  54. fwfile = "/*(DEBLOBBED)*/";
  55. else
  56. fwfile = "/*(DEBLOBBED)*/";
  57. err = reject_firmware(&fw_entry, fwfile, &dev->dev);
  58. if (err) {
  59. dev_err(&dev->dev, "Failed to load %s firmware file!\n", fwfile);
  60. dev_err(&dev->dev, "Make sure the hotplug firmware loader is installed.\n");
  61. dev_err(&dev->dev, "Goto http://linux-lc100020.sourceforge.net for more info.\n");
  62. return err;
  63. }
  64. data = fw_entry->data;
  65. len = fw_entry->size;
  66. buf = kmalloc(1024, GFP_ATOMIC);
  67. if (!buf) {
  68. err = -ENOMEM;
  69. goto exit;
  70. }
  71. while (len > 0) {
  72. int translen = (len > 1024) ? 1024 : len;
  73. memcpy(buf, data, translen);
  74. err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 0,
  75. USB_DIR_OUT | 0x40, 0, 0, buf, translen,
  76. ZD1201_FW_TIMEOUT);
  77. if (err < 0)
  78. goto exit;
  79. len -= translen;
  80. data += translen;
  81. }
  82. err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 0x2,
  83. USB_DIR_OUT | 0x40, 0, 0, NULL, 0, ZD1201_FW_TIMEOUT);
  84. if (err < 0)
  85. goto exit;
  86. err = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 0x4,
  87. USB_DIR_IN | 0x40, 0, 0, buf, sizeof(ret), ZD1201_FW_TIMEOUT);
  88. if (err < 0)
  89. goto exit;
  90. memcpy(&ret, buf, sizeof(ret));
  91. if (ret & 0x80) {
  92. err = -EIO;
  93. goto exit;
  94. }
  95. err = 0;
  96. exit:
  97. kfree(buf);
  98. release_firmware(fw_entry);
  99. return err;
  100. }
  101. /*(DEBLOBBED)*/
  102. static void zd1201_usbfree(struct urb *urb)
  103. {
  104. struct zd1201 *zd = urb->context;
  105. switch(urb->status) {
  106. case -EILSEQ:
  107. case -ENODEV:
  108. case -ETIME:
  109. case -ENOENT:
  110. case -EPIPE:
  111. case -EOVERFLOW:
  112. case -ESHUTDOWN:
  113. dev_warn(&zd->usb->dev, "%s: urb failed: %d\n",
  114. zd->dev->name, urb->status);
  115. }
  116. kfree(urb->transfer_buffer);
  117. usb_free_urb(urb);
  118. }
  119. /* cmdreq message:
  120. u32 type
  121. u16 cmd
  122. u16 parm0
  123. u16 parm1
  124. u16 parm2
  125. u8 pad[4]
  126. total: 4 + 2 + 2 + 2 + 2 + 4 = 16
  127. */
  128. static int zd1201_docmd(struct zd1201 *zd, int cmd, int parm0,
  129. int parm1, int parm2)
  130. {
  131. unsigned char *command;
  132. int ret;
  133. struct urb *urb;
  134. command = kmalloc(16, GFP_ATOMIC);
  135. if (!command)
  136. return -ENOMEM;
  137. *((__le32*)command) = cpu_to_le32(ZD1201_USB_CMDREQ);
  138. *((__le16*)&command[4]) = cpu_to_le16(cmd);
  139. *((__le16*)&command[6]) = cpu_to_le16(parm0);
  140. *((__le16*)&command[8]) = cpu_to_le16(parm1);
  141. *((__le16*)&command[10])= cpu_to_le16(parm2);
  142. urb = usb_alloc_urb(0, GFP_ATOMIC);
  143. if (!urb) {
  144. kfree(command);
  145. return -ENOMEM;
  146. }
  147. usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out2),
  148. command, 16, zd1201_usbfree, zd);
  149. ret = usb_submit_urb(urb, GFP_ATOMIC);
  150. if (ret) {
  151. kfree(command);
  152. usb_free_urb(urb);
  153. }
  154. return ret;
  155. }
  156. /* Callback after sending out a packet */
  157. static void zd1201_usbtx(struct urb *urb)
  158. {
  159. struct zd1201 *zd = urb->context;
  160. netif_wake_queue(zd->dev);
  161. }
  162. /* Incoming data */
  163. static void zd1201_usbrx(struct urb *urb)
  164. {
  165. struct zd1201 *zd = urb->context;
  166. int free = 0;
  167. unsigned char *data = urb->transfer_buffer;
  168. struct sk_buff *skb;
  169. unsigned char type;
  170. if (!zd)
  171. return;
  172. switch(urb->status) {
  173. case -EILSEQ:
  174. case -ENODEV:
  175. case -ETIME:
  176. case -ENOENT:
  177. case -EPIPE:
  178. case -EOVERFLOW:
  179. case -ESHUTDOWN:
  180. dev_warn(&zd->usb->dev, "%s: rx urb failed: %d\n",
  181. zd->dev->name, urb->status);
  182. free = 1;
  183. goto exit;
  184. }
  185. if (urb->status != 0 || urb->actual_length == 0)
  186. goto resubmit;
  187. type = data[0];
  188. if (type == ZD1201_PACKET_EVENTSTAT || type == ZD1201_PACKET_RESOURCE) {
  189. memcpy(zd->rxdata, data, urb->actual_length);
  190. zd->rxlen = urb->actual_length;
  191. zd->rxdatas = 1;
  192. wake_up(&zd->rxdataq);
  193. }
  194. /* Info frame */
  195. if (type == ZD1201_PACKET_INQUIRE) {
  196. int i = 0;
  197. unsigned short infotype, framelen, copylen;
  198. framelen = le16_to_cpu(*(__le16*)&data[4]);
  199. infotype = le16_to_cpu(*(__le16*)&data[6]);
  200. if (infotype == ZD1201_INF_LINKSTATUS) {
  201. short linkstatus;
  202. linkstatus = le16_to_cpu(*(__le16*)&data[8]);
  203. switch(linkstatus) {
  204. case 1:
  205. netif_carrier_on(zd->dev);
  206. break;
  207. case 2:
  208. netif_carrier_off(zd->dev);
  209. break;
  210. case 3:
  211. netif_carrier_off(zd->dev);
  212. break;
  213. case 4:
  214. netif_carrier_on(zd->dev);
  215. break;
  216. default:
  217. netif_carrier_off(zd->dev);
  218. }
  219. goto resubmit;
  220. }
  221. if (infotype == ZD1201_INF_ASSOCSTATUS) {
  222. short status = le16_to_cpu(*(__le16*)(data+8));
  223. int event;
  224. union iwreq_data wrqu;
  225. switch (status) {
  226. case ZD1201_ASSOCSTATUS_STAASSOC:
  227. case ZD1201_ASSOCSTATUS_REASSOC:
  228. event = IWEVREGISTERED;
  229. break;
  230. case ZD1201_ASSOCSTATUS_DISASSOC:
  231. case ZD1201_ASSOCSTATUS_ASSOCFAIL:
  232. case ZD1201_ASSOCSTATUS_AUTHFAIL:
  233. default:
  234. event = IWEVEXPIRED;
  235. }
  236. memcpy(wrqu.addr.sa_data, data+10, ETH_ALEN);
  237. wrqu.addr.sa_family = ARPHRD_ETHER;
  238. /* Send event to user space */
  239. wireless_send_event(zd->dev, event, &wrqu, NULL);
  240. goto resubmit;
  241. }
  242. if (infotype == ZD1201_INF_AUTHREQ) {
  243. union iwreq_data wrqu;
  244. memcpy(wrqu.addr.sa_data, data+8, ETH_ALEN);
  245. wrqu.addr.sa_family = ARPHRD_ETHER;
  246. /* There isn't a event that trully fits this request.
  247. We assume that userspace will be smart enough to
  248. see a new station being expired and sends back a
  249. authstation ioctl to authorize it. */
  250. wireless_send_event(zd->dev, IWEVEXPIRED, &wrqu, NULL);
  251. goto resubmit;
  252. }
  253. /* Other infotypes are handled outside this handler */
  254. zd->rxlen = 0;
  255. while (i < urb->actual_length) {
  256. copylen = le16_to_cpu(*(__le16*)&data[i+2]);
  257. /* Sanity check, sometimes we get junk */
  258. if (copylen+zd->rxlen > sizeof(zd->rxdata))
  259. break;
  260. memcpy(zd->rxdata+zd->rxlen, data+i+4, copylen);
  261. zd->rxlen += copylen;
  262. i += 64;
  263. }
  264. if (i >= urb->actual_length) {
  265. zd->rxdatas = 1;
  266. wake_up(&zd->rxdataq);
  267. }
  268. goto resubmit;
  269. }
  270. /* Actual data */
  271. if (data[urb->actual_length-1] == ZD1201_PACKET_RXDATA) {
  272. int datalen = urb->actual_length-1;
  273. unsigned short len, fc, seq;
  274. len = ntohs(*(__be16 *)&data[datalen-2]);
  275. if (len>datalen)
  276. len=datalen;
  277. fc = le16_to_cpu(*(__le16 *)&data[datalen-16]);
  278. seq = le16_to_cpu(*(__le16 *)&data[datalen-24]);
  279. if (zd->monitor) {
  280. if (datalen < 24)
  281. goto resubmit;
  282. if (!(skb = dev_alloc_skb(datalen+24)))
  283. goto resubmit;
  284. memcpy(skb_put(skb, 2), &data[datalen-16], 2);
  285. memcpy(skb_put(skb, 2), &data[datalen-2], 2);
  286. memcpy(skb_put(skb, 6), &data[datalen-14], 6);
  287. memcpy(skb_put(skb, 6), &data[datalen-22], 6);
  288. memcpy(skb_put(skb, 6), &data[datalen-8], 6);
  289. memcpy(skb_put(skb, 2), &data[datalen-24], 2);
  290. memcpy(skb_put(skb, len), data, len);
  291. skb->protocol = eth_type_trans(skb, zd->dev);
  292. zd->dev->stats.rx_packets++;
  293. zd->dev->stats.rx_bytes += skb->len;
  294. netif_rx(skb);
  295. goto resubmit;
  296. }
  297. if ((seq & IEEE80211_SCTL_FRAG) ||
  298. (fc & IEEE80211_FCTL_MOREFRAGS)) {
  299. struct zd1201_frag *frag = NULL;
  300. char *ptr;
  301. if (datalen<14)
  302. goto resubmit;
  303. if ((seq & IEEE80211_SCTL_FRAG) == 0) {
  304. frag = kmalloc(sizeof(*frag), GFP_ATOMIC);
  305. if (!frag)
  306. goto resubmit;
  307. skb = dev_alloc_skb(IEEE80211_MAX_DATA_LEN +14+2);
  308. if (!skb) {
  309. kfree(frag);
  310. goto resubmit;
  311. }
  312. frag->skb = skb;
  313. frag->seq = seq & IEEE80211_SCTL_SEQ;
  314. skb_reserve(skb, 2);
  315. memcpy(skb_put(skb, 12), &data[datalen-14], 12);
  316. memcpy(skb_put(skb, 2), &data[6], 2);
  317. memcpy(skb_put(skb, len), data+8, len);
  318. hlist_add_head(&frag->fnode, &zd->fraglist);
  319. goto resubmit;
  320. }
  321. hlist_for_each_entry(frag, &zd->fraglist, fnode)
  322. if (frag->seq == (seq&IEEE80211_SCTL_SEQ))
  323. break;
  324. if (!frag)
  325. goto resubmit;
  326. skb = frag->skb;
  327. ptr = skb_put(skb, len);
  328. if (ptr)
  329. memcpy(ptr, data+8, len);
  330. if (fc & IEEE80211_FCTL_MOREFRAGS)
  331. goto resubmit;
  332. hlist_del_init(&frag->fnode);
  333. kfree(frag);
  334. } else {
  335. if (datalen<14)
  336. goto resubmit;
  337. skb = dev_alloc_skb(len + 14 + 2);
  338. if (!skb)
  339. goto resubmit;
  340. skb_reserve(skb, 2);
  341. memcpy(skb_put(skb, 12), &data[datalen-14], 12);
  342. memcpy(skb_put(skb, 2), &data[6], 2);
  343. memcpy(skb_put(skb, len), data+8, len);
  344. }
  345. skb->protocol = eth_type_trans(skb, zd->dev);
  346. zd->dev->stats.rx_packets++;
  347. zd->dev->stats.rx_bytes += skb->len;
  348. netif_rx(skb);
  349. }
  350. resubmit:
  351. memset(data, 0, ZD1201_RXSIZE);
  352. urb->status = 0;
  353. urb->dev = zd->usb;
  354. if(usb_submit_urb(urb, GFP_ATOMIC))
  355. free = 1;
  356. exit:
  357. if (free) {
  358. zd->rxlen = 0;
  359. zd->rxdatas = 1;
  360. wake_up(&zd->rxdataq);
  361. kfree(urb->transfer_buffer);
  362. }
  363. }
  364. static int zd1201_getconfig(struct zd1201 *zd, int rid, void *riddata,
  365. unsigned int riddatalen)
  366. {
  367. int err;
  368. int i = 0;
  369. int code;
  370. int rid_fid;
  371. int length;
  372. unsigned char *pdata;
  373. zd->rxdatas = 0;
  374. err = zd1201_docmd(zd, ZD1201_CMDCODE_ACCESS, rid, 0, 0);
  375. if (err)
  376. return err;
  377. wait_event_interruptible(zd->rxdataq, zd->rxdatas);
  378. if (!zd->rxlen)
  379. return -EIO;
  380. code = le16_to_cpu(*(__le16*)(&zd->rxdata[4]));
  381. rid_fid = le16_to_cpu(*(__le16*)(&zd->rxdata[6]));
  382. length = le16_to_cpu(*(__le16*)(&zd->rxdata[8]));
  383. if (length > zd->rxlen)
  384. length = zd->rxlen-6;
  385. /* If access bit is not on, then error */
  386. if ((code & ZD1201_ACCESSBIT) != ZD1201_ACCESSBIT || rid_fid != rid )
  387. return -EINVAL;
  388. /* Not enough buffer for allocating data */
  389. if (riddatalen != (length - 4)) {
  390. dev_dbg(&zd->usb->dev, "riddatalen mismatches, expected=%u, (packet=%u) length=%u, rid=0x%04X, rid_fid=0x%04X\n",
  391. riddatalen, zd->rxlen, length, rid, rid_fid);
  392. return -ENODATA;
  393. }
  394. zd->rxdatas = 0;
  395. /* Issue SetRxRid commnd */
  396. err = zd1201_docmd(zd, ZD1201_CMDCODE_SETRXRID, rid, 0, length);
  397. if (err)
  398. return err;
  399. /* Receive RID record from resource packets */
  400. wait_event_interruptible(zd->rxdataq, zd->rxdatas);
  401. if (!zd->rxlen)
  402. return -EIO;
  403. if (zd->rxdata[zd->rxlen - 1] != ZD1201_PACKET_RESOURCE) {
  404. dev_dbg(&zd->usb->dev, "Packet type mismatch: 0x%x not 0x3\n",
  405. zd->rxdata[zd->rxlen-1]);
  406. return -EINVAL;
  407. }
  408. /* Set the data pointer and received data length */
  409. pdata = zd->rxdata;
  410. length = zd->rxlen;
  411. do {
  412. int actual_length;
  413. actual_length = (length > 64) ? 64 : length;
  414. if (pdata[0] != 0x3) {
  415. dev_dbg(&zd->usb->dev, "Rx Resource packet type error: %02X\n",
  416. pdata[0]);
  417. return -EINVAL;
  418. }
  419. if (actual_length != 64) {
  420. /* Trim the last packet type byte */
  421. actual_length--;
  422. }
  423. /* Skip the 4 bytes header (RID length and RID) */
  424. if (i == 0) {
  425. pdata += 8;
  426. actual_length -= 8;
  427. } else {
  428. pdata += 4;
  429. actual_length -= 4;
  430. }
  431. memcpy(riddata, pdata, actual_length);
  432. riddata += actual_length;
  433. pdata += actual_length;
  434. length -= 64;
  435. i++;
  436. } while (length > 0);
  437. return 0;
  438. }
  439. /*
  440. * resreq:
  441. * byte type
  442. * byte sequence
  443. * u16 reserved
  444. * byte data[12]
  445. * total: 16
  446. */
  447. static int zd1201_setconfig(struct zd1201 *zd, int rid, void *buf, int len, int wait)
  448. {
  449. int err;
  450. unsigned char *request;
  451. int reqlen;
  452. char seq=0;
  453. struct urb *urb;
  454. gfp_t gfp_mask = wait ? GFP_NOIO : GFP_ATOMIC;
  455. len += 4; /* first 4 are for header */
  456. zd->rxdatas = 0;
  457. zd->rxlen = 0;
  458. for (seq=0; len > 0; seq++) {
  459. request = kmalloc(16, gfp_mask);
  460. if (!request)
  461. return -ENOMEM;
  462. urb = usb_alloc_urb(0, gfp_mask);
  463. if (!urb) {
  464. kfree(request);
  465. return -ENOMEM;
  466. }
  467. memset(request, 0, 16);
  468. reqlen = len>12 ? 12 : len;
  469. request[0] = ZD1201_USB_RESREQ;
  470. request[1] = seq;
  471. request[2] = 0;
  472. request[3] = 0;
  473. if (request[1] == 0) {
  474. /* add header */
  475. *(__le16*)&request[4] = cpu_to_le16((len-2+1)/2);
  476. *(__le16*)&request[6] = cpu_to_le16(rid);
  477. memcpy(request+8, buf, reqlen-4);
  478. buf += reqlen-4;
  479. } else {
  480. memcpy(request+4, buf, reqlen);
  481. buf += reqlen;
  482. }
  483. len -= reqlen;
  484. usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb,
  485. zd->endp_out2), request, 16, zd1201_usbfree, zd);
  486. err = usb_submit_urb(urb, gfp_mask);
  487. if (err)
  488. goto err;
  489. }
  490. request = kmalloc(16, gfp_mask);
  491. if (!request)
  492. return -ENOMEM;
  493. urb = usb_alloc_urb(0, gfp_mask);
  494. if (!urb) {
  495. kfree(request);
  496. return -ENOMEM;
  497. }
  498. *((__le32*)request) = cpu_to_le32(ZD1201_USB_CMDREQ);
  499. *((__le16*)&request[4]) =
  500. cpu_to_le16(ZD1201_CMDCODE_ACCESS|ZD1201_ACCESSBIT);
  501. *((__le16*)&request[6]) = cpu_to_le16(rid);
  502. *((__le16*)&request[8]) = cpu_to_le16(0);
  503. *((__le16*)&request[10]) = cpu_to_le16(0);
  504. usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out2),
  505. request, 16, zd1201_usbfree, zd);
  506. err = usb_submit_urb(urb, gfp_mask);
  507. if (err)
  508. goto err;
  509. if (wait) {
  510. wait_event_interruptible(zd->rxdataq, zd->rxdatas);
  511. if (!zd->rxlen || le16_to_cpu(*(__le16*)&zd->rxdata[6]) != rid) {
  512. dev_dbg(&zd->usb->dev, "wrong or no RID received\n");
  513. }
  514. }
  515. return 0;
  516. err:
  517. kfree(request);
  518. usb_free_urb(urb);
  519. return err;
  520. }
  521. static inline int zd1201_getconfig16(struct zd1201 *zd, int rid, short *val)
  522. {
  523. int err;
  524. __le16 zdval;
  525. err = zd1201_getconfig(zd, rid, &zdval, sizeof(__le16));
  526. if (err)
  527. return err;
  528. *val = le16_to_cpu(zdval);
  529. return 0;
  530. }
  531. static inline int zd1201_setconfig16(struct zd1201 *zd, int rid, short val)
  532. {
  533. __le16 zdval = cpu_to_le16(val);
  534. return (zd1201_setconfig(zd, rid, &zdval, sizeof(__le16), 1));
  535. }
  536. static int zd1201_drvr_start(struct zd1201 *zd)
  537. {
  538. int err, i;
  539. short max;
  540. __le16 zdmax;
  541. unsigned char *buffer;
  542. buffer = kzalloc(ZD1201_RXSIZE, GFP_KERNEL);
  543. if (!buffer)
  544. return -ENOMEM;
  545. usb_fill_bulk_urb(zd->rx_urb, zd->usb,
  546. usb_rcvbulkpipe(zd->usb, zd->endp_in), buffer, ZD1201_RXSIZE,
  547. zd1201_usbrx, zd);
  548. err = usb_submit_urb(zd->rx_urb, GFP_KERNEL);
  549. if (err)
  550. goto err_buffer;
  551. err = zd1201_docmd(zd, ZD1201_CMDCODE_INIT, 0, 0, 0);
  552. if (err)
  553. goto err_urb;
  554. err = zd1201_getconfig(zd, ZD1201_RID_CNFMAXTXBUFFERNUMBER, &zdmax,
  555. sizeof(__le16));
  556. if (err)
  557. goto err_urb;
  558. max = le16_to_cpu(zdmax);
  559. for (i=0; i<max; i++) {
  560. err = zd1201_docmd(zd, ZD1201_CMDCODE_ALLOC, 1514, 0, 0);
  561. if (err)
  562. goto err_urb;
  563. }
  564. return 0;
  565. err_urb:
  566. usb_kill_urb(zd->rx_urb);
  567. return err;
  568. err_buffer:
  569. kfree(buffer);
  570. return err;
  571. }
  572. /* Magic alert: The firmware doesn't seem to like the MAC state being
  573. * toggled in promisc (aka monitor) mode.
  574. * (It works a number of times, but will halt eventually)
  575. * So we turn it of before disabling and on after enabling if needed.
  576. */
  577. static int zd1201_enable(struct zd1201 *zd)
  578. {
  579. int err;
  580. if (zd->mac_enabled)
  581. return 0;
  582. err = zd1201_docmd(zd, ZD1201_CMDCODE_ENABLE, 0, 0, 0);
  583. if (!err)
  584. zd->mac_enabled = 1;
  585. if (zd->monitor)
  586. err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 1);
  587. return err;
  588. }
  589. static int zd1201_disable(struct zd1201 *zd)
  590. {
  591. int err;
  592. if (!zd->mac_enabled)
  593. return 0;
  594. if (zd->monitor) {
  595. err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 0);
  596. if (err)
  597. return err;
  598. }
  599. err = zd1201_docmd(zd, ZD1201_CMDCODE_DISABLE, 0, 0, 0);
  600. if (!err)
  601. zd->mac_enabled = 0;
  602. return err;
  603. }
  604. static int zd1201_mac_reset(struct zd1201 *zd)
  605. {
  606. if (!zd->mac_enabled)
  607. return 0;
  608. zd1201_disable(zd);
  609. return zd1201_enable(zd);
  610. }
  611. static int zd1201_join(struct zd1201 *zd, char *essid, int essidlen)
  612. {
  613. int err, val;
  614. char buf[IW_ESSID_MAX_SIZE+2];
  615. err = zd1201_disable(zd);
  616. if (err)
  617. return err;
  618. val = ZD1201_CNFAUTHENTICATION_OPENSYSTEM;
  619. val |= ZD1201_CNFAUTHENTICATION_SHAREDKEY;
  620. err = zd1201_setconfig16(zd, ZD1201_RID_CNFAUTHENTICATION, val);
  621. if (err)
  622. return err;
  623. *(__le16 *)buf = cpu_to_le16(essidlen);
  624. memcpy(buf+2, essid, essidlen);
  625. if (!zd->ap) { /* Normal station */
  626. err = zd1201_setconfig(zd, ZD1201_RID_CNFDESIREDSSID, buf,
  627. IW_ESSID_MAX_SIZE+2, 1);
  628. if (err)
  629. return err;
  630. } else { /* AP */
  631. err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNSSID, buf,
  632. IW_ESSID_MAX_SIZE+2, 1);
  633. if (err)
  634. return err;
  635. }
  636. err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNMACADDR,
  637. zd->dev->dev_addr, zd->dev->addr_len, 1);
  638. if (err)
  639. return err;
  640. err = zd1201_enable(zd);
  641. if (err)
  642. return err;
  643. msleep(100);
  644. return 0;
  645. }
  646. static int zd1201_net_open(struct net_device *dev)
  647. {
  648. struct zd1201 *zd = netdev_priv(dev);
  649. /* Start MAC with wildcard if no essid set */
  650. if (!zd->mac_enabled)
  651. zd1201_join(zd, zd->essid, zd->essidlen);
  652. netif_start_queue(dev);
  653. return 0;
  654. }
  655. static int zd1201_net_stop(struct net_device *dev)
  656. {
  657. netif_stop_queue(dev);
  658. return 0;
  659. }
  660. /*
  661. RFC 1042 encapsulates Ethernet frames in 802.11 frames
  662. by prefixing them with 0xaa, 0xaa, 0x03) followed by a SNAP OID of 0
  663. (0x00, 0x00, 0x00). Zd requires an additional padding, copy
  664. of ethernet addresses, length of the standard RFC 1042 packet
  665. and a command byte (which is nul for tx).
  666. tx frame (from Wlan NG):
  667. RFC 1042:
  668. llc 0xAA 0xAA 0x03 (802.2 LLC)
  669. snap 0x00 0x00 0x00 (Ethernet encapsulated)
  670. type 2 bytes, Ethernet type field
  671. payload (minus eth header)
  672. Zydas specific:
  673. padding 1B if (skb->len+8+1)%64==0
  674. Eth MAC addr 12 bytes, Ethernet MAC addresses
  675. length 2 bytes, RFC 1042 packet length
  676. (llc+snap+type+payload)
  677. zd 1 null byte, zd1201 packet type
  678. */
  679. static netdev_tx_t zd1201_hard_start_xmit(struct sk_buff *skb,
  680. struct net_device *dev)
  681. {
  682. struct zd1201 *zd = netdev_priv(dev);
  683. unsigned char *txbuf = zd->txdata;
  684. int txbuflen, pad = 0, err;
  685. struct urb *urb = zd->tx_urb;
  686. if (!zd->mac_enabled || zd->monitor) {
  687. dev->stats.tx_dropped++;
  688. kfree_skb(skb);
  689. return NETDEV_TX_OK;
  690. }
  691. netif_stop_queue(dev);
  692. txbuflen = skb->len + 8 + 1;
  693. if (txbuflen%64 == 0) {
  694. pad = 1;
  695. txbuflen++;
  696. }
  697. txbuf[0] = 0xAA;
  698. txbuf[1] = 0xAA;
  699. txbuf[2] = 0x03;
  700. txbuf[3] = 0x00; /* rfc1042 */
  701. txbuf[4] = 0x00;
  702. txbuf[5] = 0x00;
  703. skb_copy_from_linear_data_offset(skb, 12, txbuf + 6, skb->len - 12);
  704. if (pad)
  705. txbuf[skb->len-12+6]=0;
  706. skb_copy_from_linear_data(skb, txbuf + skb->len - 12 + 6 + pad, 12);
  707. *(__be16*)&txbuf[skb->len+6+pad] = htons(skb->len-12+6);
  708. txbuf[txbuflen-1] = 0;
  709. usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out),
  710. txbuf, txbuflen, zd1201_usbtx, zd);
  711. err = usb_submit_urb(zd->tx_urb, GFP_ATOMIC);
  712. if (err) {
  713. dev->stats.tx_errors++;
  714. netif_start_queue(dev);
  715. } else {
  716. dev->stats.tx_packets++;
  717. dev->stats.tx_bytes += skb->len;
  718. }
  719. kfree_skb(skb);
  720. return NETDEV_TX_OK;
  721. }
  722. static void zd1201_tx_timeout(struct net_device *dev)
  723. {
  724. struct zd1201 *zd = netdev_priv(dev);
  725. if (!zd)
  726. return;
  727. dev_warn(&zd->usb->dev, "%s: TX timeout, shooting down urb\n",
  728. dev->name);
  729. usb_unlink_urb(zd->tx_urb);
  730. dev->stats.tx_errors++;
  731. /* Restart the timeout to quiet the watchdog: */
  732. dev->trans_start = jiffies; /* prevent tx timeout */
  733. }
  734. static int zd1201_set_mac_address(struct net_device *dev, void *p)
  735. {
  736. struct sockaddr *addr = p;
  737. struct zd1201 *zd = netdev_priv(dev);
  738. int err;
  739. if (!zd)
  740. return -ENODEV;
  741. err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNMACADDR,
  742. addr->sa_data, dev->addr_len, 1);
  743. if (err)
  744. return err;
  745. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  746. return zd1201_mac_reset(zd);
  747. }
  748. static struct iw_statistics *zd1201_get_wireless_stats(struct net_device *dev)
  749. {
  750. struct zd1201 *zd = netdev_priv(dev);
  751. return &zd->iwstats;
  752. }
  753. static void zd1201_set_multicast(struct net_device *dev)
  754. {
  755. struct zd1201 *zd = netdev_priv(dev);
  756. struct netdev_hw_addr *ha;
  757. unsigned char reqbuf[ETH_ALEN*ZD1201_MAXMULTI];
  758. int i;
  759. if (netdev_mc_count(dev) > ZD1201_MAXMULTI)
  760. return;
  761. i = 0;
  762. netdev_for_each_mc_addr(ha, dev)
  763. memcpy(reqbuf + i++ * ETH_ALEN, ha->addr, ETH_ALEN);
  764. zd1201_setconfig(zd, ZD1201_RID_CNFGROUPADDRESS, reqbuf,
  765. netdev_mc_count(dev) * ETH_ALEN, 0);
  766. }
  767. static int zd1201_config_commit(struct net_device *dev,
  768. struct iw_request_info *info, struct iw_point *data, char *essid)
  769. {
  770. struct zd1201 *zd = netdev_priv(dev);
  771. return zd1201_mac_reset(zd);
  772. }
  773. static int zd1201_get_name(struct net_device *dev,
  774. struct iw_request_info *info, char *name, char *extra)
  775. {
  776. strcpy(name, "IEEE 802.11b");
  777. return 0;
  778. }
  779. static int zd1201_set_freq(struct net_device *dev,
  780. struct iw_request_info *info, struct iw_freq *freq, char *extra)
  781. {
  782. struct zd1201 *zd = netdev_priv(dev);
  783. short channel = 0;
  784. int err;
  785. if (freq->e == 0)
  786. channel = freq->m;
  787. else
  788. channel = ieee80211_frequency_to_channel(freq->m);
  789. err = zd1201_setconfig16(zd, ZD1201_RID_CNFOWNCHANNEL, channel);
  790. if (err)
  791. return err;
  792. zd1201_mac_reset(zd);
  793. return 0;
  794. }
  795. static int zd1201_get_freq(struct net_device *dev,
  796. struct iw_request_info *info, struct iw_freq *freq, char *extra)
  797. {
  798. struct zd1201 *zd = netdev_priv(dev);
  799. short channel;
  800. int err;
  801. err = zd1201_getconfig16(zd, ZD1201_RID_CNFOWNCHANNEL, &channel);
  802. if (err)
  803. return err;
  804. freq->e = 0;
  805. freq->m = channel;
  806. return 0;
  807. }
  808. static int zd1201_set_mode(struct net_device *dev,
  809. struct iw_request_info *info, __u32 *mode, char *extra)
  810. {
  811. struct zd1201 *zd = netdev_priv(dev);
  812. short porttype, monitor = 0;
  813. unsigned char buffer[IW_ESSID_MAX_SIZE+2];
  814. int err;
  815. if (zd->ap) {
  816. if (*mode != IW_MODE_MASTER)
  817. return -EINVAL;
  818. return 0;
  819. }
  820. err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 0);
  821. if (err)
  822. return err;
  823. zd->dev->type = ARPHRD_ETHER;
  824. switch(*mode) {
  825. case IW_MODE_MONITOR:
  826. monitor = 1;
  827. zd->dev->type = ARPHRD_IEEE80211;
  828. /* Make sure we are no longer associated with by
  829. setting an 'impossible' essid.
  830. (otherwise we mess up firmware)
  831. */
  832. zd1201_join(zd, "\0-*#\0", 5);
  833. /* Put port in pIBSS */
  834. case 8: /* No pseudo-IBSS in wireless extensions (yet) */
  835. porttype = ZD1201_PORTTYPE_PSEUDOIBSS;
  836. break;
  837. case IW_MODE_ADHOC:
  838. porttype = ZD1201_PORTTYPE_IBSS;
  839. break;
  840. case IW_MODE_INFRA:
  841. porttype = ZD1201_PORTTYPE_BSS;
  842. break;
  843. default:
  844. return -EINVAL;
  845. }
  846. err = zd1201_setconfig16(zd, ZD1201_RID_CNFPORTTYPE, porttype);
  847. if (err)
  848. return err;
  849. if (zd->monitor && !monitor) {
  850. zd1201_disable(zd);
  851. *(__le16 *)buffer = cpu_to_le16(zd->essidlen);
  852. memcpy(buffer+2, zd->essid, zd->essidlen);
  853. err = zd1201_setconfig(zd, ZD1201_RID_CNFDESIREDSSID,
  854. buffer, IW_ESSID_MAX_SIZE+2, 1);
  855. if (err)
  856. return err;
  857. }
  858. zd->monitor = monitor;
  859. /* If monitor mode is set we don't actually turn it on here since it
  860. * is done during mac reset anyway (see zd1201_mac_enable).
  861. */
  862. zd1201_mac_reset(zd);
  863. return 0;
  864. }
  865. static int zd1201_get_mode(struct net_device *dev,
  866. struct iw_request_info *info, __u32 *mode, char *extra)
  867. {
  868. struct zd1201 *zd = netdev_priv(dev);
  869. short porttype;
  870. int err;
  871. err = zd1201_getconfig16(zd, ZD1201_RID_CNFPORTTYPE, &porttype);
  872. if (err)
  873. return err;
  874. switch(porttype) {
  875. case ZD1201_PORTTYPE_IBSS:
  876. *mode = IW_MODE_ADHOC;
  877. break;
  878. case ZD1201_PORTTYPE_BSS:
  879. *mode = IW_MODE_INFRA;
  880. break;
  881. case ZD1201_PORTTYPE_WDS:
  882. *mode = IW_MODE_REPEAT;
  883. break;
  884. case ZD1201_PORTTYPE_PSEUDOIBSS:
  885. *mode = 8;/* No Pseudo-IBSS... */
  886. break;
  887. case ZD1201_PORTTYPE_AP:
  888. *mode = IW_MODE_MASTER;
  889. break;
  890. default:
  891. dev_dbg(&zd->usb->dev, "Unknown porttype: %d\n",
  892. porttype);
  893. *mode = IW_MODE_AUTO;
  894. }
  895. if (zd->monitor)
  896. *mode = IW_MODE_MONITOR;
  897. return 0;
  898. }
  899. static int zd1201_get_range(struct net_device *dev,
  900. struct iw_request_info *info, struct iw_point *wrq, char *extra)
  901. {
  902. struct iw_range *range = (struct iw_range *)extra;
  903. wrq->length = sizeof(struct iw_range);
  904. memset(range, 0, sizeof(struct iw_range));
  905. range->we_version_compiled = WIRELESS_EXT;
  906. range->we_version_source = WIRELESS_EXT;
  907. range->max_qual.qual = 128;
  908. range->max_qual.level = 128;
  909. range->max_qual.noise = 128;
  910. range->max_qual.updated = 7;
  911. range->encoding_size[0] = 5;
  912. range->encoding_size[1] = 13;
  913. range->num_encoding_sizes = 2;
  914. range->max_encoding_tokens = ZD1201_NUMKEYS;
  915. range->num_bitrates = 4;
  916. range->bitrate[0] = 1000000;
  917. range->bitrate[1] = 2000000;
  918. range->bitrate[2] = 5500000;
  919. range->bitrate[3] = 11000000;
  920. range->min_rts = 0;
  921. range->min_frag = ZD1201_FRAGMIN;
  922. range->max_rts = ZD1201_RTSMAX;
  923. range->min_frag = ZD1201_FRAGMAX;
  924. return 0;
  925. }
  926. /* Little bit of magic here: we only get the quality if we poll
  927. * for it, and we never get an actual request to trigger such
  928. * a poll. Therefore we 'assume' that the user will soon ask for
  929. * the stats after asking the bssid.
  930. */
  931. static int zd1201_get_wap(struct net_device *dev,
  932. struct iw_request_info *info, struct sockaddr *ap_addr, char *extra)
  933. {
  934. struct zd1201 *zd = netdev_priv(dev);
  935. unsigned char buffer[6];
  936. if (!zd1201_getconfig(zd, ZD1201_RID_COMMSQUALITY, buffer, 6)) {
  937. /* Unfortunately the quality and noise reported is useless.
  938. they seem to be accumulators that increase until you
  939. read them, unless we poll on a fixed interval we can't
  940. use them
  941. */
  942. /*zd->iwstats.qual.qual = le16_to_cpu(((__le16 *)buffer)[0]);*/
  943. zd->iwstats.qual.level = le16_to_cpu(((__le16 *)buffer)[1]);
  944. /*zd->iwstats.qual.noise = le16_to_cpu(((__le16 *)buffer)[2]);*/
  945. zd->iwstats.qual.updated = 2;
  946. }
  947. return zd1201_getconfig(zd, ZD1201_RID_CURRENTBSSID, ap_addr->sa_data, 6);
  948. }
  949. static int zd1201_set_scan(struct net_device *dev,
  950. struct iw_request_info *info, struct iw_point *srq, char *extra)
  951. {
  952. /* We do everything in get_scan */
  953. return 0;
  954. }
  955. static int zd1201_get_scan(struct net_device *dev,
  956. struct iw_request_info *info, struct iw_point *srq, char *extra)
  957. {
  958. struct zd1201 *zd = netdev_priv(dev);
  959. int err, i, j, enabled_save;
  960. struct iw_event iwe;
  961. char *cev = extra;
  962. char *end_buf = extra + IW_SCAN_MAX_DATA;
  963. /* No scanning in AP mode */
  964. if (zd->ap)
  965. return -EOPNOTSUPP;
  966. /* Scan doesn't seem to work if disabled */
  967. enabled_save = zd->mac_enabled;
  968. zd1201_enable(zd);
  969. zd->rxdatas = 0;
  970. err = zd1201_docmd(zd, ZD1201_CMDCODE_INQUIRE,
  971. ZD1201_INQ_SCANRESULTS, 0, 0);
  972. if (err)
  973. return err;
  974. wait_event_interruptible(zd->rxdataq, zd->rxdatas);
  975. if (!zd->rxlen)
  976. return -EIO;
  977. if (le16_to_cpu(*(__le16*)&zd->rxdata[2]) != ZD1201_INQ_SCANRESULTS)
  978. return -EIO;
  979. for(i=8; i<zd->rxlen; i+=62) {
  980. iwe.cmd = SIOCGIWAP;
  981. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  982. memcpy(iwe.u.ap_addr.sa_data, zd->rxdata+i+6, 6);
  983. cev = iwe_stream_add_event(info, cev, end_buf,
  984. &iwe, IW_EV_ADDR_LEN);
  985. iwe.cmd = SIOCGIWESSID;
  986. iwe.u.data.length = zd->rxdata[i+16];
  987. iwe.u.data.flags = 1;
  988. cev = iwe_stream_add_point(info, cev, end_buf,
  989. &iwe, zd->rxdata+i+18);
  990. iwe.cmd = SIOCGIWMODE;
  991. if (zd->rxdata[i+14]&0x01)
  992. iwe.u.mode = IW_MODE_MASTER;
  993. else
  994. iwe.u.mode = IW_MODE_ADHOC;
  995. cev = iwe_stream_add_event(info, cev, end_buf,
  996. &iwe, IW_EV_UINT_LEN);
  997. iwe.cmd = SIOCGIWFREQ;
  998. iwe.u.freq.m = zd->rxdata[i+0];
  999. iwe.u.freq.e = 0;
  1000. cev = iwe_stream_add_event(info, cev, end_buf,
  1001. &iwe, IW_EV_FREQ_LEN);
  1002. iwe.cmd = SIOCGIWRATE;
  1003. iwe.u.bitrate.fixed = 0;
  1004. iwe.u.bitrate.disabled = 0;
  1005. for (j=0; j<10; j++) if (zd->rxdata[i+50+j]) {
  1006. iwe.u.bitrate.value = (zd->rxdata[i+50+j]&0x7f)*500000;
  1007. cev = iwe_stream_add_event(info, cev, end_buf,
  1008. &iwe, IW_EV_PARAM_LEN);
  1009. }
  1010. iwe.cmd = SIOCGIWENCODE;
  1011. iwe.u.data.length = 0;
  1012. if (zd->rxdata[i+14]&0x10)
  1013. iwe.u.data.flags = IW_ENCODE_ENABLED;
  1014. else
  1015. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1016. cev = iwe_stream_add_point(info, cev, end_buf, &iwe, NULL);
  1017. iwe.cmd = IWEVQUAL;
  1018. iwe.u.qual.qual = zd->rxdata[i+4];
  1019. iwe.u.qual.noise= zd->rxdata[i+2]/10-100;
  1020. iwe.u.qual.level = (256+zd->rxdata[i+4]*100)/255-100;
  1021. iwe.u.qual.updated = 7;
  1022. cev = iwe_stream_add_event(info, cev, end_buf,
  1023. &iwe, IW_EV_QUAL_LEN);
  1024. }
  1025. if (!enabled_save)
  1026. zd1201_disable(zd);
  1027. srq->length = cev - extra;
  1028. srq->flags = 0;
  1029. return 0;
  1030. }
  1031. static int zd1201_set_essid(struct net_device *dev,
  1032. struct iw_request_info *info, struct iw_point *data, char *essid)
  1033. {
  1034. struct zd1201 *zd = netdev_priv(dev);
  1035. if (data->length > IW_ESSID_MAX_SIZE)
  1036. return -EINVAL;
  1037. if (data->length < 1)
  1038. data->length = 1;
  1039. zd->essidlen = data->length;
  1040. memset(zd->essid, 0, IW_ESSID_MAX_SIZE+1);
  1041. memcpy(zd->essid, essid, data->length);
  1042. return zd1201_join(zd, zd->essid, zd->essidlen);
  1043. }
  1044. static int zd1201_get_essid(struct net_device *dev,
  1045. struct iw_request_info *info, struct iw_point *data, char *essid)
  1046. {
  1047. struct zd1201 *zd = netdev_priv(dev);
  1048. memcpy(essid, zd->essid, zd->essidlen);
  1049. data->flags = 1;
  1050. data->length = zd->essidlen;
  1051. return 0;
  1052. }
  1053. static int zd1201_get_nick(struct net_device *dev, struct iw_request_info *info,
  1054. struct iw_point *data, char *nick)
  1055. {
  1056. strcpy(nick, "zd1201");
  1057. data->flags = 1;
  1058. data->length = strlen(nick);
  1059. return 0;
  1060. }
  1061. static int zd1201_set_rate(struct net_device *dev,
  1062. struct iw_request_info *info, struct iw_param *rrq, char *extra)
  1063. {
  1064. struct zd1201 *zd = netdev_priv(dev);
  1065. short rate;
  1066. int err;
  1067. switch (rrq->value) {
  1068. case 1000000:
  1069. rate = ZD1201_RATEB1;
  1070. break;
  1071. case 2000000:
  1072. rate = ZD1201_RATEB2;
  1073. break;
  1074. case 5500000:
  1075. rate = ZD1201_RATEB5;
  1076. break;
  1077. case 11000000:
  1078. default:
  1079. rate = ZD1201_RATEB11;
  1080. break;
  1081. }
  1082. if (!rrq->fixed) { /* Also enable all lower bitrates */
  1083. rate |= rate-1;
  1084. }
  1085. err = zd1201_setconfig16(zd, ZD1201_RID_TXRATECNTL, rate);
  1086. if (err)
  1087. return err;
  1088. return zd1201_mac_reset(zd);
  1089. }
  1090. static int zd1201_get_rate(struct net_device *dev,
  1091. struct iw_request_info *info, struct iw_param *rrq, char *extra)
  1092. {
  1093. struct zd1201 *zd = netdev_priv(dev);
  1094. short rate;
  1095. int err;
  1096. err = zd1201_getconfig16(zd, ZD1201_RID_CURRENTTXRATE, &rate);
  1097. if (err)
  1098. return err;
  1099. switch(rate) {
  1100. case 1:
  1101. rrq->value = 1000000;
  1102. break;
  1103. case 2:
  1104. rrq->value = 2000000;
  1105. break;
  1106. case 5:
  1107. rrq->value = 5500000;
  1108. break;
  1109. case 11:
  1110. rrq->value = 11000000;
  1111. break;
  1112. default:
  1113. rrq->value = 0;
  1114. }
  1115. rrq->fixed = 0;
  1116. rrq->disabled = 0;
  1117. return 0;
  1118. }
  1119. static int zd1201_set_rts(struct net_device *dev, struct iw_request_info *info,
  1120. struct iw_param *rts, char *extra)
  1121. {
  1122. struct zd1201 *zd = netdev_priv(dev);
  1123. int err;
  1124. short val = rts->value;
  1125. if (rts->disabled || !rts->fixed)
  1126. val = ZD1201_RTSMAX;
  1127. if (val > ZD1201_RTSMAX)
  1128. return -EINVAL;
  1129. if (val < 0)
  1130. return -EINVAL;
  1131. err = zd1201_setconfig16(zd, ZD1201_RID_CNFRTSTHRESHOLD, val);
  1132. if (err)
  1133. return err;
  1134. return zd1201_mac_reset(zd);
  1135. }
  1136. static int zd1201_get_rts(struct net_device *dev, struct iw_request_info *info,
  1137. struct iw_param *rts, char *extra)
  1138. {
  1139. struct zd1201 *zd = netdev_priv(dev);
  1140. short rtst;
  1141. int err;
  1142. err = zd1201_getconfig16(zd, ZD1201_RID_CNFRTSTHRESHOLD, &rtst);
  1143. if (err)
  1144. return err;
  1145. rts->value = rtst;
  1146. rts->disabled = (rts->value == ZD1201_RTSMAX);
  1147. rts->fixed = 1;
  1148. return 0;
  1149. }
  1150. static int zd1201_set_frag(struct net_device *dev, struct iw_request_info *info,
  1151. struct iw_param *frag, char *extra)
  1152. {
  1153. struct zd1201 *zd = netdev_priv(dev);
  1154. int err;
  1155. short val = frag->value;
  1156. if (frag->disabled || !frag->fixed)
  1157. val = ZD1201_FRAGMAX;
  1158. if (val > ZD1201_FRAGMAX)
  1159. return -EINVAL;
  1160. if (val < ZD1201_FRAGMIN)
  1161. return -EINVAL;
  1162. if (val & 1)
  1163. return -EINVAL;
  1164. err = zd1201_setconfig16(zd, ZD1201_RID_CNFFRAGTHRESHOLD, val);
  1165. if (err)
  1166. return err;
  1167. return zd1201_mac_reset(zd);
  1168. }
  1169. static int zd1201_get_frag(struct net_device *dev, struct iw_request_info *info,
  1170. struct iw_param *frag, char *extra)
  1171. {
  1172. struct zd1201 *zd = netdev_priv(dev);
  1173. short fragt;
  1174. int err;
  1175. err = zd1201_getconfig16(zd, ZD1201_RID_CNFFRAGTHRESHOLD, &fragt);
  1176. if (err)
  1177. return err;
  1178. frag->value = fragt;
  1179. frag->disabled = (frag->value == ZD1201_FRAGMAX);
  1180. frag->fixed = 1;
  1181. return 0;
  1182. }
  1183. static int zd1201_set_retry(struct net_device *dev,
  1184. struct iw_request_info *info, struct iw_param *rrq, char *extra)
  1185. {
  1186. return 0;
  1187. }
  1188. static int zd1201_get_retry(struct net_device *dev,
  1189. struct iw_request_info *info, struct iw_param *rrq, char *extra)
  1190. {
  1191. return 0;
  1192. }
  1193. static int zd1201_set_encode(struct net_device *dev,
  1194. struct iw_request_info *info, struct iw_point *erq, char *key)
  1195. {
  1196. struct zd1201 *zd = netdev_priv(dev);
  1197. short i;
  1198. int err, rid;
  1199. if (erq->length > ZD1201_MAXKEYLEN)
  1200. return -EINVAL;
  1201. i = (erq->flags & IW_ENCODE_INDEX)-1;
  1202. if (i == -1) {
  1203. err = zd1201_getconfig16(zd,ZD1201_RID_CNFDEFAULTKEYID,&i);
  1204. if (err)
  1205. return err;
  1206. } else {
  1207. err = zd1201_setconfig16(zd, ZD1201_RID_CNFDEFAULTKEYID, i);
  1208. if (err)
  1209. return err;
  1210. }
  1211. if (i < 0 || i >= ZD1201_NUMKEYS)
  1212. return -EINVAL;
  1213. rid = ZD1201_RID_CNFDEFAULTKEY0 + i;
  1214. err = zd1201_setconfig(zd, rid, key, erq->length, 1);
  1215. if (err)
  1216. return err;
  1217. zd->encode_keylen[i] = erq->length;
  1218. memcpy(zd->encode_keys[i], key, erq->length);
  1219. i=0;
  1220. if (!(erq->flags & IW_ENCODE_DISABLED & IW_ENCODE_MODE)) {
  1221. i |= 0x01;
  1222. zd->encode_enabled = 1;
  1223. } else
  1224. zd->encode_enabled = 0;
  1225. if (erq->flags & IW_ENCODE_RESTRICTED & IW_ENCODE_MODE) {
  1226. i |= 0x02;
  1227. zd->encode_restricted = 1;
  1228. } else
  1229. zd->encode_restricted = 0;
  1230. err = zd1201_setconfig16(zd, ZD1201_RID_CNFWEBFLAGS, i);
  1231. if (err)
  1232. return err;
  1233. if (zd->encode_enabled)
  1234. i = ZD1201_CNFAUTHENTICATION_SHAREDKEY;
  1235. else
  1236. i = ZD1201_CNFAUTHENTICATION_OPENSYSTEM;
  1237. err = zd1201_setconfig16(zd, ZD1201_RID_CNFAUTHENTICATION, i);
  1238. if (err)
  1239. return err;
  1240. return zd1201_mac_reset(zd);
  1241. }
  1242. static int zd1201_get_encode(struct net_device *dev,
  1243. struct iw_request_info *info, struct iw_point *erq, char *key)
  1244. {
  1245. struct zd1201 *zd = netdev_priv(dev);
  1246. short i;
  1247. int err;
  1248. if (zd->encode_enabled)
  1249. erq->flags = IW_ENCODE_ENABLED;
  1250. else
  1251. erq->flags = IW_ENCODE_DISABLED;
  1252. if (zd->encode_restricted)
  1253. erq->flags |= IW_ENCODE_RESTRICTED;
  1254. else
  1255. erq->flags |= IW_ENCODE_OPEN;
  1256. i = (erq->flags & IW_ENCODE_INDEX) -1;
  1257. if (i == -1) {
  1258. err = zd1201_getconfig16(zd, ZD1201_RID_CNFDEFAULTKEYID, &i);
  1259. if (err)
  1260. return err;
  1261. }
  1262. if (i<0 || i>= ZD1201_NUMKEYS)
  1263. return -EINVAL;
  1264. erq->flags |= i+1;
  1265. erq->length = zd->encode_keylen[i];
  1266. memcpy(key, zd->encode_keys[i], erq->length);
  1267. return 0;
  1268. }
  1269. static int zd1201_set_power(struct net_device *dev,
  1270. struct iw_request_info *info, struct iw_param *vwrq, char *extra)
  1271. {
  1272. struct zd1201 *zd = netdev_priv(dev);
  1273. short enabled, duration, level;
  1274. int err;
  1275. enabled = vwrq->disabled ? 0 : 1;
  1276. if (enabled) {
  1277. if (vwrq->flags & IW_POWER_PERIOD) {
  1278. duration = vwrq->value;
  1279. err = zd1201_setconfig16(zd,
  1280. ZD1201_RID_CNFMAXSLEEPDURATION, duration);
  1281. if (err)
  1282. return err;
  1283. goto out;
  1284. }
  1285. if (vwrq->flags & IW_POWER_TIMEOUT) {
  1286. err = zd1201_getconfig16(zd,
  1287. ZD1201_RID_CNFMAXSLEEPDURATION, &duration);
  1288. if (err)
  1289. return err;
  1290. level = vwrq->value * 4 / duration;
  1291. if (level > 4)
  1292. level = 4;
  1293. if (level < 0)
  1294. level = 0;
  1295. err = zd1201_setconfig16(zd, ZD1201_RID_CNFPMEPS,
  1296. level);
  1297. if (err)
  1298. return err;
  1299. goto out;
  1300. }
  1301. return -EINVAL;
  1302. }
  1303. out:
  1304. return zd1201_setconfig16(zd, ZD1201_RID_CNFPMENABLED, enabled);
  1305. }
  1306. static int zd1201_get_power(struct net_device *dev,
  1307. struct iw_request_info *info, struct iw_param *vwrq, char *extra)
  1308. {
  1309. struct zd1201 *zd = netdev_priv(dev);
  1310. short enabled, level, duration;
  1311. int err;
  1312. err = zd1201_getconfig16(zd, ZD1201_RID_CNFPMENABLED, &enabled);
  1313. if (err)
  1314. return err;
  1315. err = zd1201_getconfig16(zd, ZD1201_RID_CNFPMEPS, &level);
  1316. if (err)
  1317. return err;
  1318. err = zd1201_getconfig16(zd, ZD1201_RID_CNFMAXSLEEPDURATION, &duration);
  1319. if (err)
  1320. return err;
  1321. vwrq->disabled = enabled ? 0 : 1;
  1322. if (vwrq->flags & IW_POWER_TYPE) {
  1323. if (vwrq->flags & IW_POWER_PERIOD) {
  1324. vwrq->value = duration;
  1325. vwrq->flags = IW_POWER_PERIOD;
  1326. } else {
  1327. vwrq->value = duration * level / 4;
  1328. vwrq->flags = IW_POWER_TIMEOUT;
  1329. }
  1330. }
  1331. if (vwrq->flags & IW_POWER_MODE) {
  1332. if (enabled && level)
  1333. vwrq->flags = IW_POWER_UNICAST_R;
  1334. else
  1335. vwrq->flags = IW_POWER_ALL_R;
  1336. }
  1337. return 0;
  1338. }
  1339. static const iw_handler zd1201_iw_handler[] =
  1340. {
  1341. (iw_handler) zd1201_config_commit, /* SIOCSIWCOMMIT */
  1342. (iw_handler) zd1201_get_name, /* SIOCGIWNAME */
  1343. (iw_handler) NULL, /* SIOCSIWNWID */
  1344. (iw_handler) NULL, /* SIOCGIWNWID */
  1345. (iw_handler) zd1201_set_freq, /* SIOCSIWFREQ */
  1346. (iw_handler) zd1201_get_freq, /* SIOCGIWFREQ */
  1347. (iw_handler) zd1201_set_mode, /* SIOCSIWMODE */
  1348. (iw_handler) zd1201_get_mode, /* SIOCGIWMODE */
  1349. (iw_handler) NULL, /* SIOCSIWSENS */
  1350. (iw_handler) NULL, /* SIOCGIWSENS */
  1351. (iw_handler) NULL, /* SIOCSIWRANGE */
  1352. (iw_handler) zd1201_get_range, /* SIOCGIWRANGE */
  1353. (iw_handler) NULL, /* SIOCSIWPRIV */
  1354. (iw_handler) NULL, /* SIOCGIWPRIV */
  1355. (iw_handler) NULL, /* SIOCSIWSTATS */
  1356. (iw_handler) NULL, /* SIOCGIWSTATS */
  1357. (iw_handler) NULL, /* SIOCSIWSPY */
  1358. (iw_handler) NULL, /* SIOCGIWSPY */
  1359. (iw_handler) NULL, /* -- hole -- */
  1360. (iw_handler) NULL, /* -- hole -- */
  1361. (iw_handler) NULL/*zd1201_set_wap*/, /* SIOCSIWAP */
  1362. (iw_handler) zd1201_get_wap, /* SIOCGIWAP */
  1363. (iw_handler) NULL, /* -- hole -- */
  1364. (iw_handler) NULL, /* SIOCGIWAPLIST */
  1365. (iw_handler) zd1201_set_scan, /* SIOCSIWSCAN */
  1366. (iw_handler) zd1201_get_scan, /* SIOCGIWSCAN */
  1367. (iw_handler) zd1201_set_essid, /* SIOCSIWESSID */
  1368. (iw_handler) zd1201_get_essid, /* SIOCGIWESSID */
  1369. (iw_handler) NULL, /* SIOCSIWNICKN */
  1370. (iw_handler) zd1201_get_nick, /* SIOCGIWNICKN */
  1371. (iw_handler) NULL, /* -- hole -- */
  1372. (iw_handler) NULL, /* -- hole -- */
  1373. (iw_handler) zd1201_set_rate, /* SIOCSIWRATE */
  1374. (iw_handler) zd1201_get_rate, /* SIOCGIWRATE */
  1375. (iw_handler) zd1201_set_rts, /* SIOCSIWRTS */
  1376. (iw_handler) zd1201_get_rts, /* SIOCGIWRTS */
  1377. (iw_handler) zd1201_set_frag, /* SIOCSIWFRAG */
  1378. (iw_handler) zd1201_get_frag, /* SIOCGIWFRAG */
  1379. (iw_handler) NULL, /* SIOCSIWTXPOW */
  1380. (iw_handler) NULL, /* SIOCGIWTXPOW */
  1381. (iw_handler) zd1201_set_retry, /* SIOCSIWRETRY */
  1382. (iw_handler) zd1201_get_retry, /* SIOCGIWRETRY */
  1383. (iw_handler) zd1201_set_encode, /* SIOCSIWENCODE */
  1384. (iw_handler) zd1201_get_encode, /* SIOCGIWENCODE */
  1385. (iw_handler) zd1201_set_power, /* SIOCSIWPOWER */
  1386. (iw_handler) zd1201_get_power, /* SIOCGIWPOWER */
  1387. };
  1388. static int zd1201_set_hostauth(struct net_device *dev,
  1389. struct iw_request_info *info, struct iw_param *rrq, char *extra)
  1390. {
  1391. struct zd1201 *zd = netdev_priv(dev);
  1392. if (!zd->ap)
  1393. return -EOPNOTSUPP;
  1394. return zd1201_setconfig16(zd, ZD1201_RID_CNFHOSTAUTH, rrq->value);
  1395. }
  1396. static int zd1201_get_hostauth(struct net_device *dev,
  1397. struct iw_request_info *info, struct iw_param *rrq, char *extra)
  1398. {
  1399. struct zd1201 *zd = netdev_priv(dev);
  1400. short hostauth;
  1401. int err;
  1402. if (!zd->ap)
  1403. return -EOPNOTSUPP;
  1404. err = zd1201_getconfig16(zd, ZD1201_RID_CNFHOSTAUTH, &hostauth);
  1405. if (err)
  1406. return err;
  1407. rrq->value = hostauth;
  1408. rrq->fixed = 1;
  1409. return 0;
  1410. }
  1411. static int zd1201_auth_sta(struct net_device *dev,
  1412. struct iw_request_info *info, struct sockaddr *sta, char *extra)
  1413. {
  1414. struct zd1201 *zd = netdev_priv(dev);
  1415. unsigned char buffer[10];
  1416. if (!zd->ap)
  1417. return -EOPNOTSUPP;
  1418. memcpy(buffer, sta->sa_data, ETH_ALEN);
  1419. *(short*)(buffer+6) = 0; /* 0==success, 1==failure */
  1420. *(short*)(buffer+8) = 0;
  1421. return zd1201_setconfig(zd, ZD1201_RID_AUTHENTICATESTA, buffer, 10, 1);
  1422. }
  1423. static int zd1201_set_maxassoc(struct net_device *dev,
  1424. struct iw_request_info *info, struct iw_param *rrq, char *extra)
  1425. {
  1426. struct zd1201 *zd = netdev_priv(dev);
  1427. int err;
  1428. if (!zd->ap)
  1429. return -EOPNOTSUPP;
  1430. err = zd1201_setconfig16(zd, ZD1201_RID_CNFMAXASSOCSTATIONS, rrq->value);
  1431. if (err)
  1432. return err;
  1433. return 0;
  1434. }
  1435. static int zd1201_get_maxassoc(struct net_device *dev,
  1436. struct iw_request_info *info, struct iw_param *rrq, char *extra)
  1437. {
  1438. struct zd1201 *zd = netdev_priv(dev);
  1439. short maxassoc;
  1440. int err;
  1441. if (!zd->ap)
  1442. return -EOPNOTSUPP;
  1443. err = zd1201_getconfig16(zd, ZD1201_RID_CNFMAXASSOCSTATIONS, &maxassoc);
  1444. if (err)
  1445. return err;
  1446. rrq->value = maxassoc;
  1447. rrq->fixed = 1;
  1448. return 0;
  1449. }
  1450. static const iw_handler zd1201_private_handler[] = {
  1451. (iw_handler) zd1201_set_hostauth, /* ZD1201SIWHOSTAUTH */
  1452. (iw_handler) zd1201_get_hostauth, /* ZD1201GIWHOSTAUTH */
  1453. (iw_handler) zd1201_auth_sta, /* ZD1201SIWAUTHSTA */
  1454. (iw_handler) NULL, /* nothing to get */
  1455. (iw_handler) zd1201_set_maxassoc, /* ZD1201SIMAXASSOC */
  1456. (iw_handler) zd1201_get_maxassoc, /* ZD1201GIMAXASSOC */
  1457. };
  1458. static const struct iw_priv_args zd1201_private_args[] = {
  1459. { ZD1201SIWHOSTAUTH, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
  1460. IW_PRIV_TYPE_NONE, "sethostauth" },
  1461. { ZD1201GIWHOSTAUTH, IW_PRIV_TYPE_NONE,
  1462. IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethostauth" },
  1463. { ZD1201SIWAUTHSTA, IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1,
  1464. IW_PRIV_TYPE_NONE, "authstation" },
  1465. { ZD1201SIWMAXASSOC, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
  1466. IW_PRIV_TYPE_NONE, "setmaxassoc" },
  1467. { ZD1201GIWMAXASSOC, IW_PRIV_TYPE_NONE,
  1468. IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getmaxassoc" },
  1469. };
  1470. static const struct iw_handler_def zd1201_iw_handlers = {
  1471. .num_standard = ARRAY_SIZE(zd1201_iw_handler),
  1472. .num_private = ARRAY_SIZE(zd1201_private_handler),
  1473. .num_private_args = ARRAY_SIZE(zd1201_private_args),
  1474. .standard = (iw_handler *)zd1201_iw_handler,
  1475. .private = (iw_handler *)zd1201_private_handler,
  1476. .private_args = (struct iw_priv_args *) zd1201_private_args,
  1477. .get_wireless_stats = zd1201_get_wireless_stats,
  1478. };
  1479. static const struct net_device_ops zd1201_netdev_ops = {
  1480. .ndo_open = zd1201_net_open,
  1481. .ndo_stop = zd1201_net_stop,
  1482. .ndo_start_xmit = zd1201_hard_start_xmit,
  1483. .ndo_tx_timeout = zd1201_tx_timeout,
  1484. .ndo_set_rx_mode = zd1201_set_multicast,
  1485. .ndo_set_mac_address = zd1201_set_mac_address,
  1486. .ndo_change_mtu = eth_change_mtu,
  1487. .ndo_validate_addr = eth_validate_addr,
  1488. };
  1489. static int zd1201_probe(struct usb_interface *interface,
  1490. const struct usb_device_id *id)
  1491. {
  1492. struct zd1201 *zd;
  1493. struct net_device *dev;
  1494. struct usb_device *usb;
  1495. int err;
  1496. short porttype;
  1497. char buf[IW_ESSID_MAX_SIZE+2];
  1498. usb = interface_to_usbdev(interface);
  1499. dev = alloc_etherdev(sizeof(*zd));
  1500. if (!dev)
  1501. return -ENOMEM;
  1502. zd = netdev_priv(dev);
  1503. zd->dev = dev;
  1504. zd->ap = ap;
  1505. zd->usb = usb;
  1506. zd->removed = 0;
  1507. init_waitqueue_head(&zd->rxdataq);
  1508. INIT_HLIST_HEAD(&zd->fraglist);
  1509. err = zd1201_fw_upload(usb, zd->ap);
  1510. if (err) {
  1511. dev_err(&usb->dev, "zd1201 firmware upload failed: %d\n", err);
  1512. goto err_zd;
  1513. }
  1514. zd->endp_in = 1;
  1515. zd->endp_out = 1;
  1516. zd->endp_out2 = 2;
  1517. zd->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1518. zd->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1519. if (!zd->rx_urb || !zd->tx_urb) {
  1520. err = -ENOMEM;
  1521. goto err_zd;
  1522. }
  1523. mdelay(100);
  1524. err = zd1201_drvr_start(zd);
  1525. if (err)
  1526. goto err_zd;
  1527. err = zd1201_setconfig16(zd, ZD1201_RID_CNFMAXDATALEN, 2312);
  1528. if (err)
  1529. goto err_start;
  1530. err = zd1201_setconfig16(zd, ZD1201_RID_TXRATECNTL,
  1531. ZD1201_RATEB1 | ZD1201_RATEB2 | ZD1201_RATEB5 | ZD1201_RATEB11);
  1532. if (err)
  1533. goto err_start;
  1534. dev->netdev_ops = &zd1201_netdev_ops;
  1535. dev->wireless_handlers = &zd1201_iw_handlers;
  1536. dev->watchdog_timeo = ZD1201_TX_TIMEOUT;
  1537. strcpy(dev->name, "wlan%d");
  1538. err = zd1201_getconfig(zd, ZD1201_RID_CNFOWNMACADDR,
  1539. dev->dev_addr, dev->addr_len);
  1540. if (err)
  1541. goto err_start;
  1542. /* Set wildcard essid to match zd->essid */
  1543. *(__le16 *)buf = cpu_to_le16(0);
  1544. err = zd1201_setconfig(zd, ZD1201_RID_CNFDESIREDSSID, buf,
  1545. IW_ESSID_MAX_SIZE+2, 1);
  1546. if (err)
  1547. goto err_start;
  1548. if (zd->ap)
  1549. porttype = ZD1201_PORTTYPE_AP;
  1550. else
  1551. porttype = ZD1201_PORTTYPE_BSS;
  1552. err = zd1201_setconfig16(zd, ZD1201_RID_CNFPORTTYPE, porttype);
  1553. if (err)
  1554. goto err_start;
  1555. SET_NETDEV_DEV(dev, &usb->dev);
  1556. err = register_netdev(dev);
  1557. if (err)
  1558. goto err_start;
  1559. dev_info(&usb->dev, "%s: ZD1201 USB Wireless interface\n",
  1560. dev->name);
  1561. usb_set_intfdata(interface, zd);
  1562. zd1201_enable(zd); /* zd1201 likes to startup enabled, */
  1563. zd1201_disable(zd); /* interfering with all the wifis in range */
  1564. return 0;
  1565. err_start:
  1566. /* Leave the device in reset state */
  1567. zd1201_docmd(zd, ZD1201_CMDCODE_INIT, 0, 0, 0);
  1568. err_zd:
  1569. usb_free_urb(zd->tx_urb);
  1570. usb_free_urb(zd->rx_urb);
  1571. free_netdev(dev);
  1572. return err;
  1573. }
  1574. static void zd1201_disconnect(struct usb_interface *interface)
  1575. {
  1576. struct zd1201 *zd = usb_get_intfdata(interface);
  1577. struct hlist_node *node2;
  1578. struct zd1201_frag *frag;
  1579. if (!zd)
  1580. return;
  1581. usb_set_intfdata(interface, NULL);
  1582. hlist_for_each_entry_safe(frag, node2, &zd->fraglist, fnode) {
  1583. hlist_del_init(&frag->fnode);
  1584. kfree_skb(frag->skb);
  1585. kfree(frag);
  1586. }
  1587. if (zd->tx_urb) {
  1588. usb_kill_urb(zd->tx_urb);
  1589. usb_free_urb(zd->tx_urb);
  1590. }
  1591. if (zd->rx_urb) {
  1592. usb_kill_urb(zd->rx_urb);
  1593. usb_free_urb(zd->rx_urb);
  1594. }
  1595. if (zd->dev) {
  1596. unregister_netdev(zd->dev);
  1597. free_netdev(zd->dev);
  1598. }
  1599. }
  1600. #ifdef CONFIG_PM
  1601. static int zd1201_suspend(struct usb_interface *interface,
  1602. pm_message_t message)
  1603. {
  1604. struct zd1201 *zd = usb_get_intfdata(interface);
  1605. netif_device_detach(zd->dev);
  1606. zd->was_enabled = zd->mac_enabled;
  1607. if (zd->was_enabled)
  1608. return zd1201_disable(zd);
  1609. else
  1610. return 0;
  1611. }
  1612. static int zd1201_resume(struct usb_interface *interface)
  1613. {
  1614. struct zd1201 *zd = usb_get_intfdata(interface);
  1615. if (!zd || !zd->dev)
  1616. return -ENODEV;
  1617. netif_device_attach(zd->dev);
  1618. if (zd->was_enabled)
  1619. return zd1201_enable(zd);
  1620. else
  1621. return 0;
  1622. }
  1623. #else
  1624. #define zd1201_suspend NULL
  1625. #define zd1201_resume NULL
  1626. #endif
  1627. static struct usb_driver zd1201_usb = {
  1628. .name = "zd1201",
  1629. .probe = zd1201_probe,
  1630. .disconnect = zd1201_disconnect,
  1631. .id_table = zd1201_table,
  1632. .suspend = zd1201_suspend,
  1633. .resume = zd1201_resume,
  1634. .disable_hub_initiated_lpm = 1,
  1635. };
  1636. module_usb_driver(zd1201_usb);