zd1201.c 45 KB

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