zd_usb.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. /* ZD1211 USB-WLAN driver for Linux
  2. *
  3. * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
  4. * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
  5. * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/firmware.h>
  24. #include <linux/device.h>
  25. #include <linux/errno.h>
  26. #include <linux/slab.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/usb.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/module.h>
  31. #include <net/mac80211.h>
  32. #include <asm/unaligned.h>
  33. #include "zd_def.h"
  34. #include "zd_mac.h"
  35. #include "zd_usb.h"
  36. static struct usb_device_id usb_ids[] = {
  37. /* ZD1211 */
  38. { USB_DEVICE(0x0105, 0x145f), .driver_info = DEVICE_ZD1211 },
  39. { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 },
  40. { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 },
  41. { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 },
  42. { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 },
  43. { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },
  44. { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
  45. { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
  46. { USB_DEVICE(0x0ace, 0xa211), .driver_info = DEVICE_ZD1211 },
  47. { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 },
  48. { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 },
  49. { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 },
  50. { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
  51. { USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 },
  52. { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
  53. { USB_DEVICE(0x129b, 0x1666), .driver_info = DEVICE_ZD1211 },
  54. { USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 },
  55. { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 },
  56. { USB_DEVICE(0x14ea, 0xab10), .driver_info = DEVICE_ZD1211 },
  57. { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 },
  58. { USB_DEVICE(0x157e, 0x300a), .driver_info = DEVICE_ZD1211 },
  59. { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
  60. { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },
  61. { USB_DEVICE(0x157e, 0x3207), .driver_info = DEVICE_ZD1211 },
  62. { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },
  63. { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
  64. /* ZD1211B */
  65. { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B },
  66. { USB_DEVICE(0x0409, 0x0248), .driver_info = DEVICE_ZD1211B },
  67. { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B },
  68. { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B },
  69. { USB_DEVICE(0x0471, 0x1237), .driver_info = DEVICE_ZD1211B },
  70. { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },
  71. { USB_DEVICE(0x054c, 0x0257), .driver_info = DEVICE_ZD1211B },
  72. { USB_DEVICE(0x0586, 0x340a), .driver_info = DEVICE_ZD1211B },
  73. { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B },
  74. { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B },
  75. { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B },
  76. { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B },
  77. { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
  78. { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211B },
  79. { USB_DEVICE(0x07fa, 0x1196), .driver_info = DEVICE_ZD1211B },
  80. { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B },
  81. { USB_DEVICE(0x083a, 0xe501), .driver_info = DEVICE_ZD1211B },
  82. { USB_DEVICE(0x083a, 0xe503), .driver_info = DEVICE_ZD1211B },
  83. { USB_DEVICE(0x083a, 0xe506), .driver_info = DEVICE_ZD1211B },
  84. { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
  85. { USB_DEVICE(0x0ace, 0xb215), .driver_info = DEVICE_ZD1211B },
  86. { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B },
  87. { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B },
  88. { USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B },
  89. { USB_DEVICE(0x0df6, 0x0036), .driver_info = DEVICE_ZD1211B },
  90. { USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B },
  91. { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B },
  92. { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
  93. { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
  94. { USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B },
  95. { USB_DEVICE(0x2019, 0xed01), .driver_info = DEVICE_ZD1211B },
  96. /* "Driverless" devices that need ejecting */
  97. { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
  98. { USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER },
  99. {}
  100. };
  101. MODULE_LICENSE("GPL");
  102. MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
  103. MODULE_AUTHOR("Ulrich Kunitz");
  104. MODULE_AUTHOR("Daniel Drake");
  105. MODULE_VERSION("1.0");
  106. MODULE_DEVICE_TABLE(usb, usb_ids);
  107. #define FW_ZD1211_PREFIX "zd1211/zd1211_"
  108. #define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
  109. static bool check_read_regs(struct zd_usb *usb, struct usb_req_read_regs *req,
  110. unsigned int count);
  111. /* USB device initialization */
  112. static void int_urb_complete(struct urb *urb);
  113. static int request_fw_file(
  114. const struct firmware **fw, const char *name, struct device *device)
  115. {
  116. int r;
  117. dev_dbg_f(device, "fw name %s\n", name);
  118. r = request_firmware(fw, name, device);
  119. if (r)
  120. dev_err(device,
  121. "Could not load firmware file %s. Error number %d\n",
  122. name, r);
  123. return r;
  124. }
  125. static inline u16 get_bcdDevice(const struct usb_device *udev)
  126. {
  127. return le16_to_cpu(udev->descriptor.bcdDevice);
  128. }
  129. enum upload_code_flags {
  130. REBOOT = 1,
  131. };
  132. /* Ensures that MAX_TRANSFER_SIZE is even. */
  133. #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
  134. static int upload_code(struct usb_device *udev,
  135. const u8 *data, size_t size, u16 code_offset, int flags)
  136. {
  137. u8 *p;
  138. int r;
  139. /* USB request blocks need "kmalloced" buffers.
  140. */
  141. p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
  142. if (!p) {
  143. dev_err(&udev->dev, "out of memory\n");
  144. r = -ENOMEM;
  145. goto error;
  146. }
  147. size &= ~1;
  148. while (size > 0) {
  149. size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
  150. size : MAX_TRANSFER_SIZE;
  151. dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
  152. memcpy(p, data, transfer_size);
  153. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  154. USB_REQ_FIRMWARE_DOWNLOAD,
  155. USB_DIR_OUT | USB_TYPE_VENDOR,
  156. code_offset, 0, p, transfer_size, 1000 /* ms */);
  157. if (r < 0) {
  158. dev_err(&udev->dev,
  159. "USB control request for firmware upload"
  160. " failed. Error number %d\n", r);
  161. goto error;
  162. }
  163. transfer_size = r & ~1;
  164. size -= transfer_size;
  165. data += transfer_size;
  166. code_offset += transfer_size/sizeof(u16);
  167. }
  168. if (flags & REBOOT) {
  169. u8 ret;
  170. /* Use "DMA-aware" buffer. */
  171. r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  172. USB_REQ_FIRMWARE_CONFIRM,
  173. USB_DIR_IN | USB_TYPE_VENDOR,
  174. 0, 0, p, sizeof(ret), 5000 /* ms */);
  175. if (r != sizeof(ret)) {
  176. dev_err(&udev->dev,
  177. "control request firmeware confirmation failed."
  178. " Return value %d\n", r);
  179. if (r >= 0)
  180. r = -ENODEV;
  181. goto error;
  182. }
  183. ret = p[0];
  184. if (ret & 0x80) {
  185. dev_err(&udev->dev,
  186. "Internal error while downloading."
  187. " Firmware confirm return value %#04x\n",
  188. (unsigned int)ret);
  189. r = -ENODEV;
  190. goto error;
  191. }
  192. dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
  193. (unsigned int)ret);
  194. }
  195. r = 0;
  196. error:
  197. kfree(p);
  198. return r;
  199. }
  200. static u16 get_word(const void *data, u16 offset)
  201. {
  202. const __le16 *p = data;
  203. return le16_to_cpu(p[offset]);
  204. }
  205. static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size,
  206. const char* postfix)
  207. {
  208. scnprintf(buffer, size, "%s%s",
  209. usb->is_zd1211b ?
  210. FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
  211. postfix);
  212. return buffer;
  213. }
  214. static int handle_version_mismatch(struct zd_usb *usb,
  215. const struct firmware *ub_fw)
  216. {
  217. struct usb_device *udev = zd_usb_to_usbdev(usb);
  218. const struct firmware *ur_fw = NULL;
  219. int offset;
  220. int r = 0;
  221. char fw_name[128];
  222. r = request_fw_file(&ur_fw,
  223. get_fw_name(usb, fw_name, sizeof(fw_name), "ur"),
  224. &udev->dev);
  225. if (r)
  226. goto error;
  227. r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);
  228. if (r)
  229. goto error;
  230. offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));
  231. r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
  232. E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);
  233. /* At this point, the vendor driver downloads the whole firmware
  234. * image, hacks around with version IDs, and uploads it again,
  235. * completely overwriting the boot code. We do not do this here as
  236. * it is not required on any tested devices, and it is suspected to
  237. * cause problems. */
  238. error:
  239. release_firmware(ur_fw);
  240. return r;
  241. }
  242. static int upload_firmware(struct zd_usb *usb)
  243. {
  244. int r;
  245. u16 fw_bcdDevice;
  246. u16 bcdDevice;
  247. struct usb_device *udev = zd_usb_to_usbdev(usb);
  248. const struct firmware *ub_fw = NULL;
  249. const struct firmware *uph_fw = NULL;
  250. char fw_name[128];
  251. bcdDevice = get_bcdDevice(udev);
  252. r = request_fw_file(&ub_fw,
  253. get_fw_name(usb, fw_name, sizeof(fw_name), "ub"),
  254. &udev->dev);
  255. if (r)
  256. goto error;
  257. fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);
  258. if (fw_bcdDevice != bcdDevice) {
  259. dev_info(&udev->dev,
  260. "firmware version %#06x and device bootcode version "
  261. "%#06x differ\n", fw_bcdDevice, bcdDevice);
  262. if (bcdDevice <= 0x4313)
  263. dev_warn(&udev->dev, "device has old bootcode, please "
  264. "report success or failure\n");
  265. r = handle_version_mismatch(usb, ub_fw);
  266. if (r)
  267. goto error;
  268. } else {
  269. dev_dbg_f(&udev->dev,
  270. "firmware device id %#06x is equal to the "
  271. "actual device id\n", fw_bcdDevice);
  272. }
  273. r = request_fw_file(&uph_fw,
  274. get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"),
  275. &udev->dev);
  276. if (r)
  277. goto error;
  278. r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);
  279. if (r) {
  280. dev_err(&udev->dev,
  281. "Could not upload firmware code uph. Error number %d\n",
  282. r);
  283. }
  284. /* FALL-THROUGH */
  285. error:
  286. release_firmware(ub_fw);
  287. release_firmware(uph_fw);
  288. return r;
  289. }
  290. MODULE_FIRMWARE(FW_ZD1211B_PREFIX "ur");
  291. MODULE_FIRMWARE(FW_ZD1211_PREFIX "ur");
  292. MODULE_FIRMWARE(FW_ZD1211B_PREFIX "ub");
  293. MODULE_FIRMWARE(FW_ZD1211_PREFIX "ub");
  294. MODULE_FIRMWARE(FW_ZD1211B_PREFIX "uphr");
  295. MODULE_FIRMWARE(FW_ZD1211_PREFIX "uphr");
  296. /* Read data from device address space using "firmware interface" which does
  297. * not require firmware to be loaded. */
  298. int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len)
  299. {
  300. int r;
  301. struct usb_device *udev = zd_usb_to_usbdev(usb);
  302. u8 *buf;
  303. /* Use "DMA-aware" buffer. */
  304. buf = kmalloc(len, GFP_KERNEL);
  305. if (!buf)
  306. return -ENOMEM;
  307. r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  308. USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0,
  309. buf, len, 5000);
  310. if (r < 0) {
  311. dev_err(&udev->dev,
  312. "read over firmware interface failed: %d\n", r);
  313. goto exit;
  314. } else if (r != len) {
  315. dev_err(&udev->dev,
  316. "incomplete read over firmware interface: %d/%d\n",
  317. r, len);
  318. r = -EIO;
  319. goto exit;
  320. }
  321. r = 0;
  322. memcpy(data, buf, len);
  323. exit:
  324. kfree(buf);
  325. return r;
  326. }
  327. #define urb_dev(urb) (&(urb)->dev->dev)
  328. static inline void handle_regs_int_override(struct urb *urb)
  329. {
  330. struct zd_usb *usb = urb->context;
  331. struct zd_usb_interrupt *intr = &usb->intr;
  332. spin_lock(&intr->lock);
  333. if (atomic_read(&intr->read_regs_enabled)) {
  334. atomic_set(&intr->read_regs_enabled, 0);
  335. intr->read_regs_int_overridden = 1;
  336. complete(&intr->read_regs.completion);
  337. }
  338. spin_unlock(&intr->lock);
  339. }
  340. static inline void handle_regs_int(struct urb *urb)
  341. {
  342. struct zd_usb *usb = urb->context;
  343. struct zd_usb_interrupt *intr = &usb->intr;
  344. int len;
  345. u16 int_num;
  346. ZD_ASSERT(in_interrupt());
  347. spin_lock(&intr->lock);
  348. int_num = le16_to_cpu(*(__le16 *)(urb->transfer_buffer+2));
  349. if (int_num == CR_INTERRUPT) {
  350. struct zd_mac *mac = zd_hw_mac(zd_usb_to_hw(urb->context));
  351. spin_lock(&mac->lock);
  352. memcpy(&mac->intr_buffer, urb->transfer_buffer,
  353. USB_MAX_EP_INT_BUFFER);
  354. spin_unlock(&mac->lock);
  355. schedule_work(&mac->process_intr);
  356. } else if (atomic_read(&intr->read_regs_enabled)) {
  357. len = urb->actual_length;
  358. intr->read_regs.length = urb->actual_length;
  359. if (len > sizeof(intr->read_regs.buffer))
  360. len = sizeof(intr->read_regs.buffer);
  361. memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
  362. /* Sometimes USB_INT_ID_REGS is not overridden, but comes after
  363. * USB_INT_ID_RETRY_FAILED. Read-reg retry then gets this
  364. * delayed USB_INT_ID_REGS, but leaves USB_INT_ID_REGS of
  365. * retry unhandled. Next read-reg command then might catch
  366. * this wrong USB_INT_ID_REGS. Fix by ignoring wrong reads.
  367. */
  368. if (!check_read_regs(usb, intr->read_regs.req,
  369. intr->read_regs.req_count))
  370. goto out;
  371. atomic_set(&intr->read_regs_enabled, 0);
  372. intr->read_regs_int_overridden = 0;
  373. complete(&intr->read_regs.completion);
  374. goto out;
  375. }
  376. out:
  377. spin_unlock(&intr->lock);
  378. /* CR_INTERRUPT might override read_reg too. */
  379. if (int_num == CR_INTERRUPT && atomic_read(&intr->read_regs_enabled))
  380. handle_regs_int_override(urb);
  381. }
  382. static void int_urb_complete(struct urb *urb)
  383. {
  384. int r;
  385. struct usb_int_header *hdr;
  386. struct zd_usb *usb;
  387. struct zd_usb_interrupt *intr;
  388. switch (urb->status) {
  389. case 0:
  390. break;
  391. case -ESHUTDOWN:
  392. case -EINVAL:
  393. case -ENODEV:
  394. case -ENOENT:
  395. case -ECONNRESET:
  396. case -EPIPE:
  397. dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
  398. return;
  399. default:
  400. dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
  401. goto resubmit;
  402. }
  403. if (urb->actual_length < sizeof(hdr)) {
  404. dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
  405. goto resubmit;
  406. }
  407. hdr = urb->transfer_buffer;
  408. if (hdr->type != USB_INT_TYPE) {
  409. dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
  410. goto resubmit;
  411. }
  412. /* USB_INT_ID_RETRY_FAILED triggered by tx-urb submit can override
  413. * pending USB_INT_ID_REGS causing read command timeout.
  414. */
  415. usb = urb->context;
  416. intr = &usb->intr;
  417. if (hdr->id != USB_INT_ID_REGS && atomic_read(&intr->read_regs_enabled))
  418. handle_regs_int_override(urb);
  419. switch (hdr->id) {
  420. case USB_INT_ID_REGS:
  421. handle_regs_int(urb);
  422. break;
  423. case USB_INT_ID_RETRY_FAILED:
  424. zd_mac_tx_failed(urb);
  425. break;
  426. default:
  427. dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
  428. (unsigned int)hdr->id);
  429. goto resubmit;
  430. }
  431. resubmit:
  432. r = usb_submit_urb(urb, GFP_ATOMIC);
  433. if (r) {
  434. dev_dbg_f(urb_dev(urb), "error: resubmit urb %p err code %d\n",
  435. urb, r);
  436. /* TODO: add worker to reset intr->urb */
  437. }
  438. return;
  439. }
  440. static inline int int_urb_interval(struct usb_device *udev)
  441. {
  442. switch (udev->speed) {
  443. case USB_SPEED_HIGH:
  444. return 4;
  445. case USB_SPEED_LOW:
  446. return 10;
  447. case USB_SPEED_FULL:
  448. default:
  449. return 1;
  450. }
  451. }
  452. static inline int usb_int_enabled(struct zd_usb *usb)
  453. {
  454. unsigned long flags;
  455. struct zd_usb_interrupt *intr = &usb->intr;
  456. struct urb *urb;
  457. spin_lock_irqsave(&intr->lock, flags);
  458. urb = intr->urb;
  459. spin_unlock_irqrestore(&intr->lock, flags);
  460. return urb != NULL;
  461. }
  462. int zd_usb_enable_int(struct zd_usb *usb)
  463. {
  464. int r;
  465. struct usb_device *udev = zd_usb_to_usbdev(usb);
  466. struct zd_usb_interrupt *intr = &usb->intr;
  467. struct urb *urb;
  468. dev_dbg_f(zd_usb_dev(usb), "\n");
  469. urb = usb_alloc_urb(0, GFP_KERNEL);
  470. if (!urb) {
  471. r = -ENOMEM;
  472. goto out;
  473. }
  474. ZD_ASSERT(!irqs_disabled());
  475. spin_lock_irq(&intr->lock);
  476. if (intr->urb) {
  477. spin_unlock_irq(&intr->lock);
  478. r = 0;
  479. goto error_free_urb;
  480. }
  481. intr->urb = urb;
  482. spin_unlock_irq(&intr->lock);
  483. r = -ENOMEM;
  484. intr->buffer = usb_alloc_coherent(udev, USB_MAX_EP_INT_BUFFER,
  485. GFP_KERNEL, &intr->buffer_dma);
  486. if (!intr->buffer) {
  487. dev_dbg_f(zd_usb_dev(usb),
  488. "couldn't allocate transfer_buffer\n");
  489. goto error_set_urb_null;
  490. }
  491. usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
  492. intr->buffer, USB_MAX_EP_INT_BUFFER,
  493. int_urb_complete, usb,
  494. intr->interval);
  495. urb->transfer_dma = intr->buffer_dma;
  496. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  497. dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
  498. r = usb_submit_urb(urb, GFP_KERNEL);
  499. if (r) {
  500. dev_dbg_f(zd_usb_dev(usb),
  501. "Couldn't submit urb. Error number %d\n", r);
  502. goto error;
  503. }
  504. return 0;
  505. error:
  506. usb_free_coherent(udev, USB_MAX_EP_INT_BUFFER,
  507. intr->buffer, intr->buffer_dma);
  508. error_set_urb_null:
  509. spin_lock_irq(&intr->lock);
  510. intr->urb = NULL;
  511. spin_unlock_irq(&intr->lock);
  512. error_free_urb:
  513. usb_free_urb(urb);
  514. out:
  515. return r;
  516. }
  517. void zd_usb_disable_int(struct zd_usb *usb)
  518. {
  519. unsigned long flags;
  520. struct usb_device *udev = zd_usb_to_usbdev(usb);
  521. struct zd_usb_interrupt *intr = &usb->intr;
  522. struct urb *urb;
  523. void *buffer;
  524. dma_addr_t buffer_dma;
  525. spin_lock_irqsave(&intr->lock, flags);
  526. urb = intr->urb;
  527. if (!urb) {
  528. spin_unlock_irqrestore(&intr->lock, flags);
  529. return;
  530. }
  531. intr->urb = NULL;
  532. buffer = intr->buffer;
  533. buffer_dma = intr->buffer_dma;
  534. intr->buffer = NULL;
  535. spin_unlock_irqrestore(&intr->lock, flags);
  536. usb_kill_urb(urb);
  537. dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
  538. usb_free_urb(urb);
  539. if (buffer)
  540. usb_free_coherent(udev, USB_MAX_EP_INT_BUFFER,
  541. buffer, buffer_dma);
  542. }
  543. static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
  544. unsigned int length)
  545. {
  546. int i;
  547. const struct rx_length_info *length_info;
  548. if (length < sizeof(struct rx_length_info)) {
  549. /* It's not a complete packet anyhow. */
  550. dev_dbg_f(zd_usb_dev(usb), "invalid, small RX packet : %d\n",
  551. length);
  552. return;
  553. }
  554. length_info = (struct rx_length_info *)
  555. (buffer + length - sizeof(struct rx_length_info));
  556. /* It might be that three frames are merged into a single URB
  557. * transaction. We have to check for the length info tag.
  558. *
  559. * While testing we discovered that length_info might be unaligned,
  560. * because if USB transactions are merged, the last packet will not
  561. * be padded. Unaligned access might also happen if the length_info
  562. * structure is not present.
  563. */
  564. if (get_unaligned_le16(&length_info->tag) == RX_LENGTH_INFO_TAG)
  565. {
  566. unsigned int l, k, n;
  567. for (i = 0, l = 0;; i++) {
  568. k = get_unaligned_le16(&length_info->length[i]);
  569. if (k == 0)
  570. return;
  571. n = l+k;
  572. if (n > length)
  573. return;
  574. zd_mac_rx(zd_usb_to_hw(usb), buffer+l, k);
  575. if (i >= 2)
  576. return;
  577. l = (n+3) & ~3;
  578. }
  579. } else {
  580. zd_mac_rx(zd_usb_to_hw(usb), buffer, length);
  581. }
  582. }
  583. static void rx_urb_complete(struct urb *urb)
  584. {
  585. int r;
  586. struct zd_usb *usb;
  587. struct zd_usb_rx *rx;
  588. const u8 *buffer;
  589. unsigned int length;
  590. switch (urb->status) {
  591. case 0:
  592. break;
  593. case -ESHUTDOWN:
  594. case -EINVAL:
  595. case -ENODEV:
  596. case -ENOENT:
  597. case -ECONNRESET:
  598. case -EPIPE:
  599. dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
  600. return;
  601. default:
  602. dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
  603. goto resubmit;
  604. }
  605. buffer = urb->transfer_buffer;
  606. length = urb->actual_length;
  607. usb = urb->context;
  608. rx = &usb->rx;
  609. tasklet_schedule(&rx->reset_timer_tasklet);
  610. if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
  611. /* If there is an old first fragment, we don't care. */
  612. dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
  613. ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
  614. spin_lock(&rx->lock);
  615. memcpy(rx->fragment, buffer, length);
  616. rx->fragment_length = length;
  617. spin_unlock(&rx->lock);
  618. goto resubmit;
  619. }
  620. spin_lock(&rx->lock);
  621. if (rx->fragment_length > 0) {
  622. /* We are on a second fragment, we believe */
  623. ZD_ASSERT(length + rx->fragment_length <=
  624. ARRAY_SIZE(rx->fragment));
  625. dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
  626. memcpy(rx->fragment+rx->fragment_length, buffer, length);
  627. handle_rx_packet(usb, rx->fragment,
  628. rx->fragment_length + length);
  629. rx->fragment_length = 0;
  630. spin_unlock(&rx->lock);
  631. } else {
  632. spin_unlock(&rx->lock);
  633. handle_rx_packet(usb, buffer, length);
  634. }
  635. resubmit:
  636. r = usb_submit_urb(urb, GFP_ATOMIC);
  637. if (r)
  638. dev_dbg_f(urb_dev(urb), "urb %p resubmit error %d\n", urb, r);
  639. }
  640. static struct urb *alloc_rx_urb(struct zd_usb *usb)
  641. {
  642. struct usb_device *udev = zd_usb_to_usbdev(usb);
  643. struct urb *urb;
  644. void *buffer;
  645. urb = usb_alloc_urb(0, GFP_KERNEL);
  646. if (!urb)
  647. return NULL;
  648. buffer = usb_alloc_coherent(udev, USB_MAX_RX_SIZE, GFP_KERNEL,
  649. &urb->transfer_dma);
  650. if (!buffer) {
  651. usb_free_urb(urb);
  652. return NULL;
  653. }
  654. usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
  655. buffer, USB_MAX_RX_SIZE,
  656. rx_urb_complete, usb);
  657. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  658. return urb;
  659. }
  660. static void free_rx_urb(struct urb *urb)
  661. {
  662. if (!urb)
  663. return;
  664. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  665. urb->transfer_buffer, urb->transfer_dma);
  666. usb_free_urb(urb);
  667. }
  668. static int __zd_usb_enable_rx(struct zd_usb *usb)
  669. {
  670. int i, r;
  671. struct zd_usb_rx *rx = &usb->rx;
  672. struct urb **urbs;
  673. dev_dbg_f(zd_usb_dev(usb), "\n");
  674. r = -ENOMEM;
  675. urbs = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL);
  676. if (!urbs)
  677. goto error;
  678. for (i = 0; i < RX_URBS_COUNT; i++) {
  679. urbs[i] = alloc_rx_urb(usb);
  680. if (!urbs[i])
  681. goto error;
  682. }
  683. ZD_ASSERT(!irqs_disabled());
  684. spin_lock_irq(&rx->lock);
  685. if (rx->urbs) {
  686. spin_unlock_irq(&rx->lock);
  687. r = 0;
  688. goto error;
  689. }
  690. rx->urbs = urbs;
  691. rx->urbs_count = RX_URBS_COUNT;
  692. spin_unlock_irq(&rx->lock);
  693. for (i = 0; i < RX_URBS_COUNT; i++) {
  694. r = usb_submit_urb(urbs[i], GFP_KERNEL);
  695. if (r)
  696. goto error_submit;
  697. }
  698. return 0;
  699. error_submit:
  700. for (i = 0; i < RX_URBS_COUNT; i++) {
  701. usb_kill_urb(urbs[i]);
  702. }
  703. spin_lock_irq(&rx->lock);
  704. rx->urbs = NULL;
  705. rx->urbs_count = 0;
  706. spin_unlock_irq(&rx->lock);
  707. error:
  708. if (urbs) {
  709. for (i = 0; i < RX_URBS_COUNT; i++)
  710. free_rx_urb(urbs[i]);
  711. }
  712. return r;
  713. }
  714. int zd_usb_enable_rx(struct zd_usb *usb)
  715. {
  716. int r;
  717. struct zd_usb_rx *rx = &usb->rx;
  718. mutex_lock(&rx->setup_mutex);
  719. r = __zd_usb_enable_rx(usb);
  720. mutex_unlock(&rx->setup_mutex);
  721. zd_usb_reset_rx_idle_timer(usb);
  722. return r;
  723. }
  724. static void __zd_usb_disable_rx(struct zd_usb *usb)
  725. {
  726. int i;
  727. unsigned long flags;
  728. struct urb **urbs;
  729. unsigned int count;
  730. struct zd_usb_rx *rx = &usb->rx;
  731. spin_lock_irqsave(&rx->lock, flags);
  732. urbs = rx->urbs;
  733. count = rx->urbs_count;
  734. spin_unlock_irqrestore(&rx->lock, flags);
  735. if (!urbs)
  736. return;
  737. for (i = 0; i < count; i++) {
  738. usb_kill_urb(urbs[i]);
  739. free_rx_urb(urbs[i]);
  740. }
  741. kfree(urbs);
  742. spin_lock_irqsave(&rx->lock, flags);
  743. rx->urbs = NULL;
  744. rx->urbs_count = 0;
  745. spin_unlock_irqrestore(&rx->lock, flags);
  746. }
  747. void zd_usb_disable_rx(struct zd_usb *usb)
  748. {
  749. struct zd_usb_rx *rx = &usb->rx;
  750. mutex_lock(&rx->setup_mutex);
  751. __zd_usb_disable_rx(usb);
  752. mutex_unlock(&rx->setup_mutex);
  753. tasklet_kill(&rx->reset_timer_tasklet);
  754. cancel_delayed_work_sync(&rx->idle_work);
  755. }
  756. static void zd_usb_reset_rx(struct zd_usb *usb)
  757. {
  758. bool do_reset;
  759. struct zd_usb_rx *rx = &usb->rx;
  760. unsigned long flags;
  761. mutex_lock(&rx->setup_mutex);
  762. spin_lock_irqsave(&rx->lock, flags);
  763. do_reset = rx->urbs != NULL;
  764. spin_unlock_irqrestore(&rx->lock, flags);
  765. if (do_reset) {
  766. __zd_usb_disable_rx(usb);
  767. __zd_usb_enable_rx(usb);
  768. }
  769. mutex_unlock(&rx->setup_mutex);
  770. if (do_reset)
  771. zd_usb_reset_rx_idle_timer(usb);
  772. }
  773. /**
  774. * zd_usb_disable_tx - disable transmission
  775. * @usb: the zd1211rw-private USB structure
  776. *
  777. * Frees all URBs in the free list and marks the transmission as disabled.
  778. */
  779. void zd_usb_disable_tx(struct zd_usb *usb)
  780. {
  781. struct zd_usb_tx *tx = &usb->tx;
  782. unsigned long flags;
  783. atomic_set(&tx->enabled, 0);
  784. /* kill all submitted tx-urbs */
  785. usb_kill_anchored_urbs(&tx->submitted);
  786. spin_lock_irqsave(&tx->lock, flags);
  787. WARN_ON(!skb_queue_empty(&tx->submitted_skbs));
  788. WARN_ON(tx->submitted_urbs != 0);
  789. tx->submitted_urbs = 0;
  790. spin_unlock_irqrestore(&tx->lock, flags);
  791. /* The stopped state is ignored, relying on ieee80211_wake_queues()
  792. * in a potentionally following zd_usb_enable_tx().
  793. */
  794. }
  795. /**
  796. * zd_usb_enable_tx - enables transmission
  797. * @usb: a &struct zd_usb pointer
  798. *
  799. * This function enables transmission and prepares the &zd_usb_tx data
  800. * structure.
  801. */
  802. void zd_usb_enable_tx(struct zd_usb *usb)
  803. {
  804. unsigned long flags;
  805. struct zd_usb_tx *tx = &usb->tx;
  806. spin_lock_irqsave(&tx->lock, flags);
  807. atomic_set(&tx->enabled, 1);
  808. tx->submitted_urbs = 0;
  809. ieee80211_wake_queues(zd_usb_to_hw(usb));
  810. tx->stopped = 0;
  811. spin_unlock_irqrestore(&tx->lock, flags);
  812. }
  813. static void tx_dec_submitted_urbs(struct zd_usb *usb)
  814. {
  815. struct zd_usb_tx *tx = &usb->tx;
  816. unsigned long flags;
  817. spin_lock_irqsave(&tx->lock, flags);
  818. --tx->submitted_urbs;
  819. if (tx->stopped && tx->submitted_urbs <= ZD_USB_TX_LOW) {
  820. ieee80211_wake_queues(zd_usb_to_hw(usb));
  821. tx->stopped = 0;
  822. }
  823. spin_unlock_irqrestore(&tx->lock, flags);
  824. }
  825. static void tx_inc_submitted_urbs(struct zd_usb *usb)
  826. {
  827. struct zd_usb_tx *tx = &usb->tx;
  828. unsigned long flags;
  829. spin_lock_irqsave(&tx->lock, flags);
  830. ++tx->submitted_urbs;
  831. if (!tx->stopped && tx->submitted_urbs > ZD_USB_TX_HIGH) {
  832. ieee80211_stop_queues(zd_usb_to_hw(usb));
  833. tx->stopped = 1;
  834. }
  835. spin_unlock_irqrestore(&tx->lock, flags);
  836. }
  837. /**
  838. * tx_urb_complete - completes the execution of an URB
  839. * @urb: a URB
  840. *
  841. * This function is called if the URB has been transferred to a device or an
  842. * error has happened.
  843. */
  844. static void tx_urb_complete(struct urb *urb)
  845. {
  846. int r;
  847. struct sk_buff *skb;
  848. struct ieee80211_tx_info *info;
  849. struct zd_usb *usb;
  850. struct zd_usb_tx *tx;
  851. skb = (struct sk_buff *)urb->context;
  852. info = IEEE80211_SKB_CB(skb);
  853. /*
  854. * grab 'usb' pointer before handing off the skb (since
  855. * it might be freed by zd_mac_tx_to_dev or mac80211)
  856. */
  857. usb = &zd_hw_mac(info->rate_driver_data[0])->chip.usb;
  858. tx = &usb->tx;
  859. switch (urb->status) {
  860. case 0:
  861. break;
  862. case -ESHUTDOWN:
  863. case -EINVAL:
  864. case -ENODEV:
  865. case -ENOENT:
  866. case -ECONNRESET:
  867. case -EPIPE:
  868. dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
  869. break;
  870. default:
  871. dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
  872. goto resubmit;
  873. }
  874. free_urb:
  875. skb_unlink(skb, &usb->tx.submitted_skbs);
  876. zd_mac_tx_to_dev(skb, urb->status);
  877. usb_free_urb(urb);
  878. tx_dec_submitted_urbs(usb);
  879. return;
  880. resubmit:
  881. usb_anchor_urb(urb, &tx->submitted);
  882. r = usb_submit_urb(urb, GFP_ATOMIC);
  883. if (r) {
  884. usb_unanchor_urb(urb);
  885. dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
  886. goto free_urb;
  887. }
  888. }
  889. /**
  890. * zd_usb_tx: initiates transfer of a frame of the device
  891. *
  892. * @usb: the zd1211rw-private USB structure
  893. * @skb: a &struct sk_buff pointer
  894. *
  895. * This function tranmits a frame to the device. It doesn't wait for
  896. * completion. The frame must contain the control set and have all the
  897. * control set information available.
  898. *
  899. * The function returns 0 if the transfer has been successfully initiated.
  900. */
  901. int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb)
  902. {
  903. int r;
  904. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  905. struct usb_device *udev = zd_usb_to_usbdev(usb);
  906. struct urb *urb;
  907. struct zd_usb_tx *tx = &usb->tx;
  908. if (!atomic_read(&tx->enabled)) {
  909. r = -ENOENT;
  910. goto out;
  911. }
  912. urb = usb_alloc_urb(0, GFP_ATOMIC);
  913. if (!urb) {
  914. r = -ENOMEM;
  915. goto out;
  916. }
  917. usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
  918. skb->data, skb->len, tx_urb_complete, skb);
  919. info->rate_driver_data[1] = (void *)jiffies;
  920. skb_queue_tail(&tx->submitted_skbs, skb);
  921. usb_anchor_urb(urb, &tx->submitted);
  922. r = usb_submit_urb(urb, GFP_ATOMIC);
  923. if (r) {
  924. dev_dbg_f(zd_usb_dev(usb), "error submit urb %p %d\n", urb, r);
  925. usb_unanchor_urb(urb);
  926. skb_unlink(skb, &tx->submitted_skbs);
  927. goto error;
  928. }
  929. tx_inc_submitted_urbs(usb);
  930. return 0;
  931. error:
  932. usb_free_urb(urb);
  933. out:
  934. return r;
  935. }
  936. static bool zd_tx_timeout(struct zd_usb *usb)
  937. {
  938. struct zd_usb_tx *tx = &usb->tx;
  939. struct sk_buff_head *q = &tx->submitted_skbs;
  940. struct sk_buff *skb, *skbnext;
  941. struct ieee80211_tx_info *info;
  942. unsigned long flags, trans_start;
  943. bool have_timedout = false;
  944. spin_lock_irqsave(&q->lock, flags);
  945. skb_queue_walk_safe(q, skb, skbnext) {
  946. info = IEEE80211_SKB_CB(skb);
  947. trans_start = (unsigned long)info->rate_driver_data[1];
  948. if (time_is_before_jiffies(trans_start + ZD_TX_TIMEOUT)) {
  949. have_timedout = true;
  950. break;
  951. }
  952. }
  953. spin_unlock_irqrestore(&q->lock, flags);
  954. return have_timedout;
  955. }
  956. static void zd_tx_watchdog_handler(struct work_struct *work)
  957. {
  958. struct zd_usb *usb =
  959. container_of(work, struct zd_usb, tx.watchdog_work.work);
  960. struct zd_usb_tx *tx = &usb->tx;
  961. if (!atomic_read(&tx->enabled) || !tx->watchdog_enabled)
  962. goto out;
  963. if (!zd_tx_timeout(usb))
  964. goto out;
  965. /* TX halted, try reset */
  966. dev_warn(zd_usb_dev(usb), "TX-stall detected, resetting device...");
  967. usb_queue_reset_device(usb->intf);
  968. /* reset will stop this worker, don't rearm */
  969. return;
  970. out:
  971. queue_delayed_work(zd_workqueue, &tx->watchdog_work,
  972. ZD_TX_WATCHDOG_INTERVAL);
  973. }
  974. void zd_tx_watchdog_enable(struct zd_usb *usb)
  975. {
  976. struct zd_usb_tx *tx = &usb->tx;
  977. if (!tx->watchdog_enabled) {
  978. dev_dbg_f(zd_usb_dev(usb), "\n");
  979. queue_delayed_work(zd_workqueue, &tx->watchdog_work,
  980. ZD_TX_WATCHDOG_INTERVAL);
  981. tx->watchdog_enabled = 1;
  982. }
  983. }
  984. void zd_tx_watchdog_disable(struct zd_usb *usb)
  985. {
  986. struct zd_usb_tx *tx = &usb->tx;
  987. if (tx->watchdog_enabled) {
  988. dev_dbg_f(zd_usb_dev(usb), "\n");
  989. tx->watchdog_enabled = 0;
  990. cancel_delayed_work_sync(&tx->watchdog_work);
  991. }
  992. }
  993. static void zd_rx_idle_timer_handler(struct work_struct *work)
  994. {
  995. struct zd_usb *usb =
  996. container_of(work, struct zd_usb, rx.idle_work.work);
  997. struct zd_mac *mac = zd_usb_to_mac(usb);
  998. if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
  999. return;
  1000. dev_dbg_f(zd_usb_dev(usb), "\n");
  1001. /* 30 seconds since last rx, reset rx */
  1002. zd_usb_reset_rx(usb);
  1003. }
  1004. static void zd_usb_reset_rx_idle_timer_tasklet(unsigned long param)
  1005. {
  1006. struct zd_usb *usb = (struct zd_usb *)param;
  1007. zd_usb_reset_rx_idle_timer(usb);
  1008. }
  1009. void zd_usb_reset_rx_idle_timer(struct zd_usb *usb)
  1010. {
  1011. struct zd_usb_rx *rx = &usb->rx;
  1012. cancel_delayed_work(&rx->idle_work);
  1013. queue_delayed_work(zd_workqueue, &rx->idle_work, ZD_RX_IDLE_INTERVAL);
  1014. }
  1015. static inline void init_usb_interrupt(struct zd_usb *usb)
  1016. {
  1017. struct zd_usb_interrupt *intr = &usb->intr;
  1018. spin_lock_init(&intr->lock);
  1019. intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
  1020. init_completion(&intr->read_regs.completion);
  1021. atomic_set(&intr->read_regs_enabled, 0);
  1022. intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT);
  1023. }
  1024. static inline void init_usb_rx(struct zd_usb *usb)
  1025. {
  1026. struct zd_usb_rx *rx = &usb->rx;
  1027. spin_lock_init(&rx->lock);
  1028. mutex_init(&rx->setup_mutex);
  1029. if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
  1030. rx->usb_packet_size = 512;
  1031. } else {
  1032. rx->usb_packet_size = 64;
  1033. }
  1034. ZD_ASSERT(rx->fragment_length == 0);
  1035. INIT_DELAYED_WORK(&rx->idle_work, zd_rx_idle_timer_handler);
  1036. rx->reset_timer_tasklet.func = zd_usb_reset_rx_idle_timer_tasklet;
  1037. rx->reset_timer_tasklet.data = (unsigned long)usb;
  1038. }
  1039. static inline void init_usb_tx(struct zd_usb *usb)
  1040. {
  1041. struct zd_usb_tx *tx = &usb->tx;
  1042. spin_lock_init(&tx->lock);
  1043. atomic_set(&tx->enabled, 0);
  1044. tx->stopped = 0;
  1045. skb_queue_head_init(&tx->submitted_skbs);
  1046. init_usb_anchor(&tx->submitted);
  1047. tx->submitted_urbs = 0;
  1048. tx->watchdog_enabled = 0;
  1049. INIT_DELAYED_WORK(&tx->watchdog_work, zd_tx_watchdog_handler);
  1050. }
  1051. void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw,
  1052. struct usb_interface *intf)
  1053. {
  1054. memset(usb, 0, sizeof(*usb));
  1055. usb->intf = usb_get_intf(intf);
  1056. usb_set_intfdata(usb->intf, hw);
  1057. init_usb_anchor(&usb->submitted_cmds);
  1058. init_usb_interrupt(usb);
  1059. init_usb_tx(usb);
  1060. init_usb_rx(usb);
  1061. }
  1062. void zd_usb_clear(struct zd_usb *usb)
  1063. {
  1064. usb_set_intfdata(usb->intf, NULL);
  1065. usb_put_intf(usb->intf);
  1066. ZD_MEMCLEAR(usb, sizeof(*usb));
  1067. /* FIXME: usb_interrupt, usb_tx, usb_rx? */
  1068. }
  1069. static const char *speed(enum usb_device_speed speed)
  1070. {
  1071. switch (speed) {
  1072. case USB_SPEED_LOW:
  1073. return "low";
  1074. case USB_SPEED_FULL:
  1075. return "full";
  1076. case USB_SPEED_HIGH:
  1077. return "high";
  1078. default:
  1079. return "unknown speed";
  1080. }
  1081. }
  1082. static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
  1083. {
  1084. return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
  1085. le16_to_cpu(udev->descriptor.idVendor),
  1086. le16_to_cpu(udev->descriptor.idProduct),
  1087. get_bcdDevice(udev),
  1088. speed(udev->speed));
  1089. }
  1090. int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
  1091. {
  1092. struct usb_device *udev = interface_to_usbdev(usb->intf);
  1093. return scnprint_id(udev, buffer, size);
  1094. }
  1095. #ifdef DEBUG
  1096. static void print_id(struct usb_device *udev)
  1097. {
  1098. char buffer[40];
  1099. scnprint_id(udev, buffer, sizeof(buffer));
  1100. buffer[sizeof(buffer)-1] = 0;
  1101. dev_dbg_f(&udev->dev, "%s\n", buffer);
  1102. }
  1103. #else
  1104. #define print_id(udev) do { } while (0)
  1105. #endif
  1106. static int eject_installer(struct usb_interface *intf)
  1107. {
  1108. struct usb_device *udev = interface_to_usbdev(intf);
  1109. struct usb_host_interface *iface_desc = &intf->altsetting[0];
  1110. struct usb_endpoint_descriptor *endpoint;
  1111. unsigned char *cmd;
  1112. u8 bulk_out_ep;
  1113. int r;
  1114. /* Find bulk out endpoint */
  1115. for (r = 1; r >= 0; r--) {
  1116. endpoint = &iface_desc->endpoint[r].desc;
  1117. if (usb_endpoint_dir_out(endpoint) &&
  1118. usb_endpoint_xfer_bulk(endpoint)) {
  1119. bulk_out_ep = endpoint->bEndpointAddress;
  1120. break;
  1121. }
  1122. }
  1123. if (r == -1) {
  1124. dev_err(&udev->dev,
  1125. "zd1211rw: Could not find bulk out endpoint\n");
  1126. return -ENODEV;
  1127. }
  1128. cmd = kzalloc(31, GFP_KERNEL);
  1129. if (cmd == NULL)
  1130. return -ENODEV;
  1131. /* USB bulk command block */
  1132. cmd[0] = 0x55; /* bulk command signature */
  1133. cmd[1] = 0x53; /* bulk command signature */
  1134. cmd[2] = 0x42; /* bulk command signature */
  1135. cmd[3] = 0x43; /* bulk command signature */
  1136. cmd[14] = 6; /* command length */
  1137. cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
  1138. cmd[19] = 0x2; /* eject disc */
  1139. dev_info(&udev->dev, "Ejecting virtual installer media...\n");
  1140. r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
  1141. cmd, 31, NULL, 2000);
  1142. kfree(cmd);
  1143. if (r)
  1144. return r;
  1145. /* At this point, the device disconnects and reconnects with the real
  1146. * ID numbers. */
  1147. usb_set_intfdata(intf, NULL);
  1148. return 0;
  1149. }
  1150. int zd_usb_init_hw(struct zd_usb *usb)
  1151. {
  1152. int r;
  1153. struct zd_mac *mac = zd_usb_to_mac(usb);
  1154. dev_dbg_f(zd_usb_dev(usb), "\n");
  1155. r = upload_firmware(usb);
  1156. if (r) {
  1157. dev_err(zd_usb_dev(usb),
  1158. "couldn't load firmware. Error number %d\n", r);
  1159. return r;
  1160. }
  1161. r = usb_reset_configuration(zd_usb_to_usbdev(usb));
  1162. if (r) {
  1163. dev_dbg_f(zd_usb_dev(usb),
  1164. "couldn't reset configuration. Error number %d\n", r);
  1165. return r;
  1166. }
  1167. r = zd_mac_init_hw(mac->hw);
  1168. if (r) {
  1169. dev_dbg_f(zd_usb_dev(usb),
  1170. "couldn't initialize mac. Error number %d\n", r);
  1171. return r;
  1172. }
  1173. usb->initialized = 1;
  1174. return 0;
  1175. }
  1176. static int probe(struct usb_interface *intf, const struct usb_device_id *id)
  1177. {
  1178. int r;
  1179. struct usb_device *udev = interface_to_usbdev(intf);
  1180. struct zd_usb *usb;
  1181. struct ieee80211_hw *hw = NULL;
  1182. print_id(udev);
  1183. if (id->driver_info & DEVICE_INSTALLER)
  1184. return eject_installer(intf);
  1185. switch (udev->speed) {
  1186. case USB_SPEED_LOW:
  1187. case USB_SPEED_FULL:
  1188. case USB_SPEED_HIGH:
  1189. break;
  1190. default:
  1191. dev_dbg_f(&intf->dev, "Unknown USB speed\n");
  1192. r = -ENODEV;
  1193. goto error;
  1194. }
  1195. r = usb_reset_device(udev);
  1196. if (r) {
  1197. dev_err(&intf->dev,
  1198. "couldn't reset usb device. Error number %d\n", r);
  1199. goto error;
  1200. }
  1201. hw = zd_mac_alloc_hw(intf);
  1202. if (hw == NULL) {
  1203. r = -ENOMEM;
  1204. goto error;
  1205. }
  1206. usb = &zd_hw_mac(hw)->chip.usb;
  1207. usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0;
  1208. r = zd_mac_preinit_hw(hw);
  1209. if (r) {
  1210. dev_dbg_f(&intf->dev,
  1211. "couldn't initialize mac. Error number %d\n", r);
  1212. goto error;
  1213. }
  1214. r = ieee80211_register_hw(hw);
  1215. if (r) {
  1216. dev_dbg_f(&intf->dev,
  1217. "couldn't register device. Error number %d\n", r);
  1218. goto error;
  1219. }
  1220. dev_dbg_f(&intf->dev, "successful\n");
  1221. dev_info(&intf->dev, "%s\n", wiphy_name(hw->wiphy));
  1222. return 0;
  1223. error:
  1224. usb_reset_device(interface_to_usbdev(intf));
  1225. if (hw) {
  1226. zd_mac_clear(zd_hw_mac(hw));
  1227. ieee80211_free_hw(hw);
  1228. }
  1229. return r;
  1230. }
  1231. static void disconnect(struct usb_interface *intf)
  1232. {
  1233. struct ieee80211_hw *hw = zd_intf_to_hw(intf);
  1234. struct zd_mac *mac;
  1235. struct zd_usb *usb;
  1236. /* Either something really bad happened, or we're just dealing with
  1237. * a DEVICE_INSTALLER. */
  1238. if (hw == NULL)
  1239. return;
  1240. mac = zd_hw_mac(hw);
  1241. usb = &mac->chip.usb;
  1242. dev_dbg_f(zd_usb_dev(usb), "\n");
  1243. ieee80211_unregister_hw(hw);
  1244. /* Just in case something has gone wrong! */
  1245. zd_usb_disable_tx(usb);
  1246. zd_usb_disable_rx(usb);
  1247. zd_usb_disable_int(usb);
  1248. /* If the disconnect has been caused by a removal of the
  1249. * driver module, the reset allows reloading of the driver. If the
  1250. * reset will not be executed here, the upload of the firmware in the
  1251. * probe function caused by the reloading of the driver will fail.
  1252. */
  1253. usb_reset_device(interface_to_usbdev(intf));
  1254. zd_mac_clear(mac);
  1255. ieee80211_free_hw(hw);
  1256. dev_dbg(&intf->dev, "disconnected\n");
  1257. }
  1258. static void zd_usb_resume(struct zd_usb *usb)
  1259. {
  1260. struct zd_mac *mac = zd_usb_to_mac(usb);
  1261. int r;
  1262. dev_dbg_f(zd_usb_dev(usb), "\n");
  1263. r = zd_op_start(zd_usb_to_hw(usb));
  1264. if (r < 0) {
  1265. dev_warn(zd_usb_dev(usb), "Device resume failed "
  1266. "with error code %d. Retrying...\n", r);
  1267. if (usb->was_running)
  1268. set_bit(ZD_DEVICE_RUNNING, &mac->flags);
  1269. usb_queue_reset_device(usb->intf);
  1270. return;
  1271. }
  1272. if (mac->type != NL80211_IFTYPE_UNSPECIFIED) {
  1273. r = zd_restore_settings(mac);
  1274. if (r < 0) {
  1275. dev_dbg(zd_usb_dev(usb),
  1276. "failed to restore settings, %d\n", r);
  1277. return;
  1278. }
  1279. }
  1280. }
  1281. static void zd_usb_stop(struct zd_usb *usb)
  1282. {
  1283. dev_dbg_f(zd_usb_dev(usb), "\n");
  1284. zd_op_stop(zd_usb_to_hw(usb));
  1285. zd_usb_disable_tx(usb);
  1286. zd_usb_disable_rx(usb);
  1287. zd_usb_disable_int(usb);
  1288. usb->initialized = 0;
  1289. }
  1290. static int pre_reset(struct usb_interface *intf)
  1291. {
  1292. struct ieee80211_hw *hw = usb_get_intfdata(intf);
  1293. struct zd_mac *mac;
  1294. struct zd_usb *usb;
  1295. if (!hw || intf->condition != USB_INTERFACE_BOUND)
  1296. return 0;
  1297. mac = zd_hw_mac(hw);
  1298. usb = &mac->chip.usb;
  1299. usb->was_running = test_bit(ZD_DEVICE_RUNNING, &mac->flags);
  1300. zd_usb_stop(usb);
  1301. mutex_lock(&mac->chip.mutex);
  1302. return 0;
  1303. }
  1304. static int post_reset(struct usb_interface *intf)
  1305. {
  1306. struct ieee80211_hw *hw = usb_get_intfdata(intf);
  1307. struct zd_mac *mac;
  1308. struct zd_usb *usb;
  1309. if (!hw || intf->condition != USB_INTERFACE_BOUND)
  1310. return 0;
  1311. mac = zd_hw_mac(hw);
  1312. usb = &mac->chip.usb;
  1313. mutex_unlock(&mac->chip.mutex);
  1314. if (usb->was_running)
  1315. zd_usb_resume(usb);
  1316. return 0;
  1317. }
  1318. static struct usb_driver driver = {
  1319. .name = KBUILD_MODNAME,
  1320. .id_table = usb_ids,
  1321. .probe = probe,
  1322. .disconnect = disconnect,
  1323. .pre_reset = pre_reset,
  1324. .post_reset = post_reset,
  1325. };
  1326. struct workqueue_struct *zd_workqueue;
  1327. static int __init usb_init(void)
  1328. {
  1329. int r;
  1330. pr_debug("%s usb_init()\n", driver.name);
  1331. zd_workqueue = create_singlethread_workqueue(driver.name);
  1332. if (zd_workqueue == NULL) {
  1333. printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
  1334. return -ENOMEM;
  1335. }
  1336. r = usb_register(&driver);
  1337. if (r) {
  1338. destroy_workqueue(zd_workqueue);
  1339. printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
  1340. driver.name, r);
  1341. return r;
  1342. }
  1343. pr_debug("%s initialized\n", driver.name);
  1344. return 0;
  1345. }
  1346. static void __exit usb_exit(void)
  1347. {
  1348. pr_debug("%s usb_exit()\n", driver.name);
  1349. usb_deregister(&driver);
  1350. destroy_workqueue(zd_workqueue);
  1351. }
  1352. module_init(usb_init);
  1353. module_exit(usb_exit);
  1354. static int zd_ep_regs_out_msg(struct usb_device *udev, void *data, int len,
  1355. int *actual_length, int timeout)
  1356. {
  1357. /* In USB 2.0 mode EP_REGS_OUT endpoint is interrupt type. However in
  1358. * USB 1.1 mode endpoint is bulk. Select correct type URB by endpoint
  1359. * descriptor.
  1360. */
  1361. struct usb_host_endpoint *ep;
  1362. unsigned int pipe;
  1363. pipe = usb_sndintpipe(udev, EP_REGS_OUT);
  1364. ep = usb_pipe_endpoint(udev, pipe);
  1365. if (!ep)
  1366. return -EINVAL;
  1367. if (usb_endpoint_xfer_int(&ep->desc)) {
  1368. return usb_interrupt_msg(udev, pipe, data, len,
  1369. actual_length, timeout);
  1370. } else {
  1371. pipe = usb_sndbulkpipe(udev, EP_REGS_OUT);
  1372. return usb_bulk_msg(udev, pipe, data, len, actual_length,
  1373. timeout);
  1374. }
  1375. }
  1376. static int usb_int_regs_length(unsigned int count)
  1377. {
  1378. return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
  1379. }
  1380. static void prepare_read_regs_int(struct zd_usb *usb,
  1381. struct usb_req_read_regs *req,
  1382. unsigned int count)
  1383. {
  1384. struct zd_usb_interrupt *intr = &usb->intr;
  1385. spin_lock_irq(&intr->lock);
  1386. atomic_set(&intr->read_regs_enabled, 1);
  1387. intr->read_regs.req = req;
  1388. intr->read_regs.req_count = count;
  1389. INIT_COMPLETION(intr->read_regs.completion);
  1390. spin_unlock_irq(&intr->lock);
  1391. }
  1392. static void disable_read_regs_int(struct zd_usb *usb)
  1393. {
  1394. struct zd_usb_interrupt *intr = &usb->intr;
  1395. spin_lock_irq(&intr->lock);
  1396. atomic_set(&intr->read_regs_enabled, 0);
  1397. spin_unlock_irq(&intr->lock);
  1398. }
  1399. static bool check_read_regs(struct zd_usb *usb, struct usb_req_read_regs *req,
  1400. unsigned int count)
  1401. {
  1402. int i;
  1403. struct zd_usb_interrupt *intr = &usb->intr;
  1404. struct read_regs_int *rr = &intr->read_regs;
  1405. struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
  1406. /* The created block size seems to be larger than expected.
  1407. * However results appear to be correct.
  1408. */
  1409. if (rr->length < usb_int_regs_length(count)) {
  1410. dev_dbg_f(zd_usb_dev(usb),
  1411. "error: actual length %d less than expected %d\n",
  1412. rr->length, usb_int_regs_length(count));
  1413. return false;
  1414. }
  1415. if (rr->length > sizeof(rr->buffer)) {
  1416. dev_dbg_f(zd_usb_dev(usb),
  1417. "error: actual length %d exceeds buffer size %zu\n",
  1418. rr->length, sizeof(rr->buffer));
  1419. return false;
  1420. }
  1421. for (i = 0; i < count; i++) {
  1422. struct reg_data *rd = &regs->regs[i];
  1423. if (rd->addr != req->addr[i]) {
  1424. dev_dbg_f(zd_usb_dev(usb),
  1425. "rd[%d] addr %#06hx expected %#06hx\n", i,
  1426. le16_to_cpu(rd->addr),
  1427. le16_to_cpu(req->addr[i]));
  1428. return false;
  1429. }
  1430. }
  1431. return true;
  1432. }
  1433. static int get_results(struct zd_usb *usb, u16 *values,
  1434. struct usb_req_read_regs *req, unsigned int count,
  1435. bool *retry)
  1436. {
  1437. int r;
  1438. int i;
  1439. struct zd_usb_interrupt *intr = &usb->intr;
  1440. struct read_regs_int *rr = &intr->read_regs;
  1441. struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
  1442. spin_lock_irq(&intr->lock);
  1443. r = -EIO;
  1444. /* Read failed because firmware bug? */
  1445. *retry = !!intr->read_regs_int_overridden;
  1446. if (*retry)
  1447. goto error_unlock;
  1448. if (!check_read_regs(usb, req, count)) {
  1449. dev_dbg_f(zd_usb_dev(usb), "error: invalid read regs\n");
  1450. goto error_unlock;
  1451. }
  1452. for (i = 0; i < count; i++) {
  1453. struct reg_data *rd = &regs->regs[i];
  1454. values[i] = le16_to_cpu(rd->value);
  1455. }
  1456. r = 0;
  1457. error_unlock:
  1458. spin_unlock_irq(&intr->lock);
  1459. return r;
  1460. }
  1461. int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
  1462. const zd_addr_t *addresses, unsigned int count)
  1463. {
  1464. int r, i, req_len, actual_req_len, try_count = 0;
  1465. struct usb_device *udev;
  1466. struct usb_req_read_regs *req = NULL;
  1467. unsigned long timeout;
  1468. bool retry = false;
  1469. if (count < 1) {
  1470. dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
  1471. return -EINVAL;
  1472. }
  1473. if (count > USB_MAX_IOREAD16_COUNT) {
  1474. dev_dbg_f(zd_usb_dev(usb),
  1475. "error: count %u exceeds possible max %u\n",
  1476. count, USB_MAX_IOREAD16_COUNT);
  1477. return -EINVAL;
  1478. }
  1479. if (in_atomic()) {
  1480. dev_dbg_f(zd_usb_dev(usb),
  1481. "error: io in atomic context not supported\n");
  1482. return -EWOULDBLOCK;
  1483. }
  1484. if (!usb_int_enabled(usb)) {
  1485. dev_dbg_f(zd_usb_dev(usb),
  1486. "error: usb interrupt not enabled\n");
  1487. return -EWOULDBLOCK;
  1488. }
  1489. ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
  1490. BUILD_BUG_ON(sizeof(struct usb_req_read_regs) + USB_MAX_IOREAD16_COUNT *
  1491. sizeof(__le16) > sizeof(usb->req_buf));
  1492. BUG_ON(sizeof(struct usb_req_read_regs) + count * sizeof(__le16) >
  1493. sizeof(usb->req_buf));
  1494. req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
  1495. req = (void *)usb->req_buf;
  1496. req->id = cpu_to_le16(USB_REQ_READ_REGS);
  1497. for (i = 0; i < count; i++)
  1498. req->addr[i] = cpu_to_le16((u16)addresses[i]);
  1499. retry_read:
  1500. try_count++;
  1501. udev = zd_usb_to_usbdev(usb);
  1502. prepare_read_regs_int(usb, req, count);
  1503. r = zd_ep_regs_out_msg(udev, req, req_len, &actual_req_len, 50 /*ms*/);
  1504. if (r) {
  1505. dev_dbg_f(zd_usb_dev(usb),
  1506. "error in zd_ep_regs_out_msg(). Error number %d\n", r);
  1507. goto error;
  1508. }
  1509. if (req_len != actual_req_len) {
  1510. dev_dbg_f(zd_usb_dev(usb), "error in zd_ep_regs_out_msg()\n"
  1511. " req_len %d != actual_req_len %d\n",
  1512. req_len, actual_req_len);
  1513. r = -EIO;
  1514. goto error;
  1515. }
  1516. timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
  1517. msecs_to_jiffies(50));
  1518. if (!timeout) {
  1519. disable_read_regs_int(usb);
  1520. dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
  1521. r = -ETIMEDOUT;
  1522. goto error;
  1523. }
  1524. r = get_results(usb, values, req, count, &retry);
  1525. if (retry && try_count < 20) {
  1526. dev_dbg_f(zd_usb_dev(usb), "read retry, tries so far: %d\n",
  1527. try_count);
  1528. goto retry_read;
  1529. }
  1530. error:
  1531. return r;
  1532. }
  1533. static void iowrite16v_urb_complete(struct urb *urb)
  1534. {
  1535. struct zd_usb *usb = urb->context;
  1536. if (urb->status && !usb->cmd_error)
  1537. usb->cmd_error = urb->status;
  1538. if (!usb->cmd_error &&
  1539. urb->actual_length != urb->transfer_buffer_length)
  1540. usb->cmd_error = -EIO;
  1541. }
  1542. static int zd_submit_waiting_urb(struct zd_usb *usb, bool last)
  1543. {
  1544. int r = 0;
  1545. struct urb *urb = usb->urb_async_waiting;
  1546. if (!urb)
  1547. return 0;
  1548. usb->urb_async_waiting = NULL;
  1549. if (!last)
  1550. urb->transfer_flags |= URB_NO_INTERRUPT;
  1551. usb_anchor_urb(urb, &usb->submitted_cmds);
  1552. r = usb_submit_urb(urb, GFP_KERNEL);
  1553. if (r) {
  1554. usb_unanchor_urb(urb);
  1555. dev_dbg_f(zd_usb_dev(usb),
  1556. "error in usb_submit_urb(). Error number %d\n", r);
  1557. goto error;
  1558. }
  1559. /* fall-through with r == 0 */
  1560. error:
  1561. usb_free_urb(urb);
  1562. return r;
  1563. }
  1564. void zd_usb_iowrite16v_async_start(struct zd_usb *usb)
  1565. {
  1566. ZD_ASSERT(usb_anchor_empty(&usb->submitted_cmds));
  1567. ZD_ASSERT(usb->urb_async_waiting == NULL);
  1568. ZD_ASSERT(!usb->in_async);
  1569. ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
  1570. usb->in_async = 1;
  1571. usb->cmd_error = 0;
  1572. usb->urb_async_waiting = NULL;
  1573. }
  1574. int zd_usb_iowrite16v_async_end(struct zd_usb *usb, unsigned int timeout)
  1575. {
  1576. int r;
  1577. ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
  1578. ZD_ASSERT(usb->in_async);
  1579. /* Submit last iowrite16v URB */
  1580. r = zd_submit_waiting_urb(usb, true);
  1581. if (r) {
  1582. dev_dbg_f(zd_usb_dev(usb),
  1583. "error in zd_submit_waiting_usb(). "
  1584. "Error number %d\n", r);
  1585. usb_kill_anchored_urbs(&usb->submitted_cmds);
  1586. goto error;
  1587. }
  1588. if (timeout)
  1589. timeout = usb_wait_anchor_empty_timeout(&usb->submitted_cmds,
  1590. timeout);
  1591. if (!timeout) {
  1592. usb_kill_anchored_urbs(&usb->submitted_cmds);
  1593. if (usb->cmd_error == -ENOENT) {
  1594. dev_dbg_f(zd_usb_dev(usb), "timed out");
  1595. r = -ETIMEDOUT;
  1596. goto error;
  1597. }
  1598. }
  1599. r = usb->cmd_error;
  1600. error:
  1601. usb->in_async = 0;
  1602. return r;
  1603. }
  1604. int zd_usb_iowrite16v_async(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
  1605. unsigned int count)
  1606. {
  1607. int r;
  1608. struct usb_device *udev;
  1609. struct usb_req_write_regs *req = NULL;
  1610. int i, req_len;
  1611. struct urb *urb;
  1612. struct usb_host_endpoint *ep;
  1613. ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
  1614. ZD_ASSERT(usb->in_async);
  1615. if (count == 0)
  1616. return 0;
  1617. if (count > USB_MAX_IOWRITE16_COUNT) {
  1618. dev_dbg_f(zd_usb_dev(usb),
  1619. "error: count %u exceeds possible max %u\n",
  1620. count, USB_MAX_IOWRITE16_COUNT);
  1621. return -EINVAL;
  1622. }
  1623. if (in_atomic()) {
  1624. dev_dbg_f(zd_usb_dev(usb),
  1625. "error: io in atomic context not supported\n");
  1626. return -EWOULDBLOCK;
  1627. }
  1628. udev = zd_usb_to_usbdev(usb);
  1629. ep = usb_pipe_endpoint(udev, usb_sndintpipe(udev, EP_REGS_OUT));
  1630. if (!ep)
  1631. return -ENOENT;
  1632. urb = usb_alloc_urb(0, GFP_KERNEL);
  1633. if (!urb)
  1634. return -ENOMEM;
  1635. req_len = sizeof(struct usb_req_write_regs) +
  1636. count * sizeof(struct reg_data);
  1637. req = kmalloc(req_len, GFP_KERNEL);
  1638. if (!req) {
  1639. r = -ENOMEM;
  1640. goto error;
  1641. }
  1642. req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
  1643. for (i = 0; i < count; i++) {
  1644. struct reg_data *rw = &req->reg_writes[i];
  1645. rw->addr = cpu_to_le16((u16)ioreqs[i].addr);
  1646. rw->value = cpu_to_le16(ioreqs[i].value);
  1647. }
  1648. /* In USB 2.0 mode endpoint is interrupt type. However in USB 1.1 mode
  1649. * endpoint is bulk. Select correct type URB by endpoint descriptor.
  1650. */
  1651. if (usb_endpoint_xfer_int(&ep->desc))
  1652. usb_fill_int_urb(urb, udev, usb_sndintpipe(udev, EP_REGS_OUT),
  1653. req, req_len, iowrite16v_urb_complete, usb,
  1654. ep->desc.bInterval);
  1655. else
  1656. usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
  1657. req, req_len, iowrite16v_urb_complete, usb);
  1658. urb->transfer_flags |= URB_FREE_BUFFER;
  1659. /* Submit previous URB */
  1660. r = zd_submit_waiting_urb(usb, false);
  1661. if (r) {
  1662. dev_dbg_f(zd_usb_dev(usb),
  1663. "error in zd_submit_waiting_usb(). "
  1664. "Error number %d\n", r);
  1665. goto error;
  1666. }
  1667. /* Delay submit so that URB_NO_INTERRUPT flag can be set for all URBs
  1668. * of currect batch except for very last.
  1669. */
  1670. usb->urb_async_waiting = urb;
  1671. return 0;
  1672. error:
  1673. usb_free_urb(urb);
  1674. return r;
  1675. }
  1676. int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
  1677. unsigned int count)
  1678. {
  1679. int r;
  1680. zd_usb_iowrite16v_async_start(usb);
  1681. r = zd_usb_iowrite16v_async(usb, ioreqs, count);
  1682. if (r) {
  1683. zd_usb_iowrite16v_async_end(usb, 0);
  1684. return r;
  1685. }
  1686. return zd_usb_iowrite16v_async_end(usb, 50 /* ms */);
  1687. }
  1688. int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
  1689. {
  1690. int r;
  1691. struct usb_device *udev;
  1692. struct usb_req_rfwrite *req = NULL;
  1693. int i, req_len, actual_req_len;
  1694. u16 bit_value_template;
  1695. if (in_atomic()) {
  1696. dev_dbg_f(zd_usb_dev(usb),
  1697. "error: io in atomic context not supported\n");
  1698. return -EWOULDBLOCK;
  1699. }
  1700. if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
  1701. dev_dbg_f(zd_usb_dev(usb),
  1702. "error: bits %d are smaller than"
  1703. " USB_MIN_RFWRITE_BIT_COUNT %d\n",
  1704. bits, USB_MIN_RFWRITE_BIT_COUNT);
  1705. return -EINVAL;
  1706. }
  1707. if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
  1708. dev_dbg_f(zd_usb_dev(usb),
  1709. "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
  1710. bits, USB_MAX_RFWRITE_BIT_COUNT);
  1711. return -EINVAL;
  1712. }
  1713. #ifdef DEBUG
  1714. if (value & (~0UL << bits)) {
  1715. dev_dbg_f(zd_usb_dev(usb),
  1716. "error: value %#09x has bits >= %d set\n",
  1717. value, bits);
  1718. return -EINVAL;
  1719. }
  1720. #endif /* DEBUG */
  1721. dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
  1722. r = zd_usb_ioread16(usb, &bit_value_template, ZD_CR203);
  1723. if (r) {
  1724. dev_dbg_f(zd_usb_dev(usb),
  1725. "error %d: Couldn't read ZD_CR203\n", r);
  1726. return r;
  1727. }
  1728. bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
  1729. ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
  1730. BUILD_BUG_ON(sizeof(struct usb_req_rfwrite) +
  1731. USB_MAX_RFWRITE_BIT_COUNT * sizeof(__le16) >
  1732. sizeof(usb->req_buf));
  1733. BUG_ON(sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16) >
  1734. sizeof(usb->req_buf));
  1735. req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
  1736. req = (void *)usb->req_buf;
  1737. req->id = cpu_to_le16(USB_REQ_WRITE_RF);
  1738. /* 1: 3683a, but not used in ZYDAS driver */
  1739. req->value = cpu_to_le16(2);
  1740. req->bits = cpu_to_le16(bits);
  1741. for (i = 0; i < bits; i++) {
  1742. u16 bv = bit_value_template;
  1743. if (value & (1 << (bits-1-i)))
  1744. bv |= RF_DATA;
  1745. req->bit_values[i] = cpu_to_le16(bv);
  1746. }
  1747. udev = zd_usb_to_usbdev(usb);
  1748. r = zd_ep_regs_out_msg(udev, req, req_len, &actual_req_len, 50 /*ms*/);
  1749. if (r) {
  1750. dev_dbg_f(zd_usb_dev(usb),
  1751. "error in zd_ep_regs_out_msg(). Error number %d\n", r);
  1752. goto out;
  1753. }
  1754. if (req_len != actual_req_len) {
  1755. dev_dbg_f(zd_usb_dev(usb), "error in zd_ep_regs_out_msg()"
  1756. " req_len %d != actual_req_len %d\n",
  1757. req_len, actual_req_len);
  1758. r = -EIO;
  1759. goto out;
  1760. }
  1761. /* FALL-THROUGH with r == 0 */
  1762. out:
  1763. return r;
  1764. }