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