ftdi-elan.c 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780
  1. /*
  2. * USB FTDI client driver for Elan Digital Systems's Uxxx adapters
  3. *
  4. * Copyright(C) 2006 Elan Digital Systems Limited
  5. * http://www.elandigitalsystems.com
  6. *
  7. * Author and Maintainer - Tony Olech - Elan Digital Systems
  8. * tony.olech@elandigitalsystems.com
  9. *
  10. * This program is free software;you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation, version 2.
  13. *
  14. *
  15. * This driver was written by Tony Olech(tony.olech@elandigitalsystems.com)
  16. * based on various USB client drivers in the 2.6.15 linux kernel
  17. * with constant reference to the 3rd Edition of Linux Device Drivers
  18. * published by O'Reilly
  19. *
  20. * The U132 adapter is a USB to CardBus adapter specifically designed
  21. * for PC cards that contain an OHCI host controller. Typical PC cards
  22. * are the Orange Mobile 3G Option GlobeTrotter Fusion card.
  23. *
  24. * The U132 adapter will *NOT *work with PC cards that do not contain
  25. * an OHCI controller. A simple way to test whether a PC card has an
  26. * OHCI controller as an interface is to insert the PC card directly
  27. * into a laptop(or desktop) with a CardBus slot and if "lspci" shows
  28. * a new USB controller and "lsusb -v" shows a new OHCI Host Controller
  29. * then there is a good chance that the U132 adapter will support the
  30. * PC card.(you also need the specific client driver for the PC card)
  31. *
  32. * Please inform the Author and Maintainer about any PC cards that
  33. * contain OHCI Host Controller and work when directly connected to
  34. * an embedded CardBus slot but do not work when they are connected
  35. * via an ELAN U132 adapter.
  36. *
  37. */
  38. #include <linux/kernel.h>
  39. #include <linux/errno.h>
  40. #include <linux/init.h>
  41. #include <linux/list.h>
  42. #include <linux/ioctl.h>
  43. #include <linux/slab.h>
  44. #include <linux/module.h>
  45. #include <linux/kref.h>
  46. #include <asm/uaccess.h>
  47. #include <linux/usb.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/platform_device.h>
  50. MODULE_AUTHOR("Tony Olech");
  51. MODULE_DESCRIPTION("FTDI ELAN driver");
  52. MODULE_LICENSE("GPL");
  53. #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
  54. extern struct platform_driver u132_platform_driver;
  55. static struct workqueue_struct *status_queue;
  56. static struct workqueue_struct *command_queue;
  57. static struct workqueue_struct *respond_queue;
  58. /*
  59. * ftdi_module_lock exists to protect access to global variables
  60. *
  61. */
  62. static struct semaphore ftdi_module_lock;
  63. static int ftdi_instances = 0;
  64. static struct list_head ftdi_static_list;
  65. /*
  66. * end of the global variables protected by ftdi_module_lock
  67. */
  68. #include "usb_u132.h"
  69. #define TD_DEVNOTRESP 5
  70. /* Define these values to match your devices*/
  71. #define USB_FTDI_ELAN_VENDOR_ID 0x0403
  72. #define USB_FTDI_ELAN_PRODUCT_ID 0xd6ea
  73. /* table of devices that work with this driver*/
  74. static struct usb_device_id ftdi_elan_table[] = {
  75. {USB_DEVICE(USB_FTDI_ELAN_VENDOR_ID, USB_FTDI_ELAN_PRODUCT_ID)},
  76. { /* Terminating entry */ }
  77. };
  78. MODULE_DEVICE_TABLE(usb, ftdi_elan_table);
  79. /* only the jtag(firmware upgrade device) interface requires
  80. * a device file and corresponding minor number, but the
  81. * interface is created unconditionally - I suppose it could
  82. * be configured or not according to a module parameter.
  83. * But since we(now) require one interface per device,
  84. * and since it unlikely that a normal installation would
  85. * require more than a couple of elan-ftdi devices, 8 seems
  86. * like a reasonable limit to have here, and if someone
  87. * really requires more than 8 devices, then they can frig the
  88. * code and recompile
  89. */
  90. #define USB_FTDI_ELAN_MINOR_BASE 192
  91. #define COMMAND_BITS 5
  92. #define COMMAND_SIZE (1<<COMMAND_BITS)
  93. #define COMMAND_MASK (COMMAND_SIZE-1)
  94. struct u132_command {
  95. u8 header;
  96. u16 length;
  97. u8 address;
  98. u8 width;
  99. u32 value;
  100. int follows;
  101. void *buffer;
  102. };
  103. #define RESPOND_BITS 5
  104. #define RESPOND_SIZE (1<<RESPOND_BITS)
  105. #define RESPOND_MASK (RESPOND_SIZE-1)
  106. struct u132_respond {
  107. u8 header;
  108. u8 address;
  109. u32 *value;
  110. int *result;
  111. struct completion wait_completion;
  112. };
  113. struct u132_target {
  114. void *endp;
  115. struct urb *urb;
  116. int toggle_bits;
  117. int error_count;
  118. int condition_code;
  119. int repeat_number;
  120. int halted;
  121. int skipped;
  122. int actual;
  123. int non_null;
  124. int active;
  125. int abandoning;
  126. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  127. int toggle_bits, int error_count, int condition_code,
  128. int repeat_number, int halted, int skipped, int actual,
  129. int non_null);
  130. };
  131. /* Structure to hold all of our device specific stuff*/
  132. struct usb_ftdi {
  133. struct list_head ftdi_list;
  134. struct semaphore u132_lock;
  135. int command_next;
  136. int command_head;
  137. struct u132_command command[COMMAND_SIZE];
  138. int respond_next;
  139. int respond_head;
  140. struct u132_respond respond[RESPOND_SIZE];
  141. struct u132_target target[4];
  142. char device_name[16];
  143. unsigned synchronized:1;
  144. unsigned enumerated:1;
  145. unsigned registered:1;
  146. unsigned initialized:1;
  147. unsigned card_ejected:1;
  148. int function;
  149. int sequence_num;
  150. int disconnected;
  151. int gone_away;
  152. int stuck_status;
  153. int status_queue_delay;
  154. struct semaphore sw_lock;
  155. struct usb_device *udev;
  156. struct usb_interface *interface;
  157. struct usb_class_driver *class;
  158. struct work_struct status_work;
  159. struct work_struct command_work;
  160. struct work_struct respond_work;
  161. struct u132_platform_data platform_data;
  162. struct resource resources[0];
  163. struct platform_device platform_dev;
  164. unsigned char *bulk_in_buffer;
  165. size_t bulk_in_size;
  166. size_t bulk_in_last;
  167. size_t bulk_in_left;
  168. __u8 bulk_in_endpointAddr;
  169. __u8 bulk_out_endpointAddr;
  170. struct kref kref;
  171. u32 controlreg;
  172. u8 response[4 + 1024];
  173. int expected;
  174. int recieved;
  175. int ed_found;
  176. };
  177. #define kref_to_usb_ftdi(d) container_of(d, struct usb_ftdi, kref)
  178. #define platform_device_to_usb_ftdi(d) container_of(d, struct usb_ftdi, \
  179. platform_dev)
  180. static struct usb_driver ftdi_elan_driver;
  181. static void ftdi_elan_delete(struct kref *kref)
  182. {
  183. struct usb_ftdi *ftdi = kref_to_usb_ftdi(kref);
  184. dev_warn(&ftdi->udev->dev, "FREEING ftdi=%p\n", ftdi);
  185. usb_put_dev(ftdi->udev);
  186. ftdi->disconnected += 1;
  187. down(&ftdi_module_lock);
  188. list_del_init(&ftdi->ftdi_list);
  189. ftdi_instances -= 1;
  190. up(&ftdi_module_lock);
  191. kfree(ftdi->bulk_in_buffer);
  192. ftdi->bulk_in_buffer = NULL;
  193. }
  194. static void ftdi_elan_put_kref(struct usb_ftdi *ftdi)
  195. {
  196. kref_put(&ftdi->kref, ftdi_elan_delete);
  197. }
  198. static void ftdi_elan_get_kref(struct usb_ftdi *ftdi)
  199. {
  200. kref_get(&ftdi->kref);
  201. }
  202. static void ftdi_elan_init_kref(struct usb_ftdi *ftdi)
  203. {
  204. kref_init(&ftdi->kref);
  205. }
  206. static void ftdi_status_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
  207. {
  208. if (delta > 0) {
  209. if (queue_delayed_work(status_queue, &ftdi->status_work, delta))
  210. return;
  211. } else if (queue_work(status_queue, &ftdi->status_work))
  212. return;
  213. kref_put(&ftdi->kref, ftdi_elan_delete);
  214. return;
  215. }
  216. static void ftdi_status_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
  217. {
  218. if (delta > 0) {
  219. if (queue_delayed_work(status_queue, &ftdi->status_work, delta))
  220. kref_get(&ftdi->kref);
  221. } else if (queue_work(status_queue, &ftdi->status_work))
  222. kref_get(&ftdi->kref);
  223. return;
  224. }
  225. static void ftdi_status_cancel_work(struct usb_ftdi *ftdi)
  226. {
  227. if (cancel_delayed_work(&ftdi->status_work))
  228. kref_put(&ftdi->kref, ftdi_elan_delete);
  229. }
  230. static void ftdi_command_requeue_work(struct usb_ftdi *ftdi, unsigned int delta)
  231. {
  232. if (delta > 0) {
  233. if (queue_delayed_work(command_queue, &ftdi->command_work,
  234. delta))
  235. return;
  236. } else if (queue_work(command_queue, &ftdi->command_work))
  237. return;
  238. kref_put(&ftdi->kref, ftdi_elan_delete);
  239. return;
  240. }
  241. static void ftdi_command_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
  242. {
  243. if (delta > 0) {
  244. if (queue_delayed_work(command_queue, &ftdi->command_work,
  245. delta))
  246. kref_get(&ftdi->kref);
  247. } else if (queue_work(command_queue, &ftdi->command_work))
  248. kref_get(&ftdi->kref);
  249. return;
  250. }
  251. static void ftdi_command_cancel_work(struct usb_ftdi *ftdi)
  252. {
  253. if (cancel_delayed_work(&ftdi->command_work))
  254. kref_put(&ftdi->kref, ftdi_elan_delete);
  255. }
  256. static void ftdi_response_requeue_work(struct usb_ftdi *ftdi,
  257. unsigned int delta)
  258. {
  259. if (delta > 0) {
  260. if (queue_delayed_work(respond_queue, &ftdi->respond_work,
  261. delta))
  262. return;
  263. } else if (queue_work(respond_queue, &ftdi->respond_work))
  264. return;
  265. kref_put(&ftdi->kref, ftdi_elan_delete);
  266. return;
  267. }
  268. static void ftdi_respond_queue_work(struct usb_ftdi *ftdi, unsigned int delta)
  269. {
  270. if (delta > 0) {
  271. if (queue_delayed_work(respond_queue, &ftdi->respond_work,
  272. delta))
  273. kref_get(&ftdi->kref);
  274. } else if (queue_work(respond_queue, &ftdi->respond_work))
  275. kref_get(&ftdi->kref);
  276. return;
  277. }
  278. static void ftdi_response_cancel_work(struct usb_ftdi *ftdi)
  279. {
  280. if (cancel_delayed_work(&ftdi->respond_work))
  281. kref_put(&ftdi->kref, ftdi_elan_delete);
  282. }
  283. void ftdi_elan_gone_away(struct platform_device *pdev)
  284. {
  285. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  286. ftdi->gone_away += 1;
  287. ftdi_elan_put_kref(ftdi);
  288. }
  289. EXPORT_SYMBOL_GPL(ftdi_elan_gone_away);
  290. static void ftdi_release_platform_dev(struct device *dev)
  291. {
  292. dev->parent = NULL;
  293. }
  294. static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
  295. struct u132_target *target, u8 *buffer, int length);
  296. static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi);
  297. static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi);
  298. static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi);
  299. static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi);
  300. static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi);
  301. static int ftdi_elan_synchronize(struct usb_ftdi *ftdi);
  302. static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi);
  303. static int ftdi_elan_command_engine(struct usb_ftdi *ftdi);
  304. static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi);
  305. static int ftdi_elan_hcd_init(struct usb_ftdi *ftdi)
  306. {
  307. int result;
  308. if (ftdi->platform_dev.dev.parent)
  309. return -EBUSY;
  310. ftdi_elan_get_kref(ftdi);
  311. ftdi->platform_data.potpg = 100;
  312. ftdi->platform_data.reset = NULL;
  313. ftdi->platform_dev.id = ftdi->sequence_num;
  314. ftdi->platform_dev.resource = ftdi->resources;
  315. ftdi->platform_dev.num_resources = ARRAY_SIZE(ftdi->resources);
  316. ftdi->platform_dev.dev.platform_data = &ftdi->platform_data;
  317. ftdi->platform_dev.dev.parent = NULL;
  318. ftdi->platform_dev.dev.release = ftdi_release_platform_dev;
  319. ftdi->platform_dev.dev.dma_mask = NULL;
  320. snprintf(ftdi->device_name, sizeof(ftdi->device_name), "u132_hcd");
  321. ftdi->platform_dev.name = ftdi->device_name;
  322. dev_info(&ftdi->udev->dev, "requesting module '%s'\n", "u132_hcd");
  323. request_module("u132_hcd");
  324. dev_info(&ftdi->udev->dev, "registering '%s'\n",
  325. ftdi->platform_dev.name);
  326. result = platform_device_register(&ftdi->platform_dev);
  327. return result;
  328. }
  329. static void ftdi_elan_abandon_completions(struct usb_ftdi *ftdi)
  330. {
  331. down(&ftdi->u132_lock);
  332. while (ftdi->respond_next > ftdi->respond_head) {
  333. struct u132_respond *respond = &ftdi->respond[RESPOND_MASK &
  334. ftdi->respond_head++];
  335. *respond->result = -ESHUTDOWN;
  336. *respond->value = 0;
  337. complete(&respond->wait_completion);
  338. } up(&ftdi->u132_lock);
  339. }
  340. static void ftdi_elan_abandon_targets(struct usb_ftdi *ftdi)
  341. {
  342. int ed_number = 4;
  343. down(&ftdi->u132_lock);
  344. while (ed_number-- > 0) {
  345. struct u132_target *target = &ftdi->target[ed_number];
  346. if (target->active == 1) {
  347. target->condition_code = TD_DEVNOTRESP;
  348. up(&ftdi->u132_lock);
  349. ftdi_elan_do_callback(ftdi, target, NULL, 0);
  350. down(&ftdi->u132_lock);
  351. }
  352. }
  353. ftdi->recieved = 0;
  354. ftdi->expected = 4;
  355. ftdi->ed_found = 0;
  356. up(&ftdi->u132_lock);
  357. }
  358. static void ftdi_elan_flush_targets(struct usb_ftdi *ftdi)
  359. {
  360. int ed_number = 4;
  361. down(&ftdi->u132_lock);
  362. while (ed_number-- > 0) {
  363. struct u132_target *target = &ftdi->target[ed_number];
  364. target->abandoning = 1;
  365. wait_1:if (target->active == 1) {
  366. int command_size = ftdi->command_next -
  367. ftdi->command_head;
  368. if (command_size < COMMAND_SIZE) {
  369. struct u132_command *command = &ftdi->command[
  370. COMMAND_MASK & ftdi->command_next];
  371. command->header = 0x80 | (ed_number << 5) | 0x4;
  372. command->length = 0x00;
  373. command->address = 0x00;
  374. command->width = 0x00;
  375. command->follows = 0;
  376. command->value = 0;
  377. command->buffer = &command->value;
  378. ftdi->command_next += 1;
  379. ftdi_elan_kick_command_queue(ftdi);
  380. } else {
  381. up(&ftdi->u132_lock);
  382. msleep(100);
  383. down(&ftdi->u132_lock);
  384. goto wait_1;
  385. }
  386. }
  387. wait_2:if (target->active == 1) {
  388. int command_size = ftdi->command_next -
  389. ftdi->command_head;
  390. if (command_size < COMMAND_SIZE) {
  391. struct u132_command *command = &ftdi->command[
  392. COMMAND_MASK & ftdi->command_next];
  393. command->header = 0x90 | (ed_number << 5);
  394. command->length = 0x00;
  395. command->address = 0x00;
  396. command->width = 0x00;
  397. command->follows = 0;
  398. command->value = 0;
  399. command->buffer = &command->value;
  400. ftdi->command_next += 1;
  401. ftdi_elan_kick_command_queue(ftdi);
  402. } else {
  403. up(&ftdi->u132_lock);
  404. msleep(100);
  405. down(&ftdi->u132_lock);
  406. goto wait_2;
  407. }
  408. }
  409. }
  410. ftdi->recieved = 0;
  411. ftdi->expected = 4;
  412. ftdi->ed_found = 0;
  413. up(&ftdi->u132_lock);
  414. }
  415. static void ftdi_elan_cancel_targets(struct usb_ftdi *ftdi)
  416. {
  417. int ed_number = 4;
  418. down(&ftdi->u132_lock);
  419. while (ed_number-- > 0) {
  420. struct u132_target *target = &ftdi->target[ed_number];
  421. target->abandoning = 1;
  422. wait:if (target->active == 1) {
  423. int command_size = ftdi->command_next -
  424. ftdi->command_head;
  425. if (command_size < COMMAND_SIZE) {
  426. struct u132_command *command = &ftdi->command[
  427. COMMAND_MASK & ftdi->command_next];
  428. command->header = 0x80 | (ed_number << 5) | 0x4;
  429. command->length = 0x00;
  430. command->address = 0x00;
  431. command->width = 0x00;
  432. command->follows = 0;
  433. command->value = 0;
  434. command->buffer = &command->value;
  435. ftdi->command_next += 1;
  436. ftdi_elan_kick_command_queue(ftdi);
  437. } else {
  438. up(&ftdi->u132_lock);
  439. msleep(100);
  440. down(&ftdi->u132_lock);
  441. goto wait;
  442. }
  443. }
  444. }
  445. ftdi->recieved = 0;
  446. ftdi->expected = 4;
  447. ftdi->ed_found = 0;
  448. up(&ftdi->u132_lock);
  449. }
  450. static void ftdi_elan_kick_command_queue(struct usb_ftdi *ftdi)
  451. {
  452. ftdi_command_queue_work(ftdi, 0);
  453. return;
  454. }
  455. static void ftdi_elan_command_work(void *data)
  456. {
  457. struct usb_ftdi *ftdi = data;
  458. if (ftdi->disconnected > 0) {
  459. ftdi_elan_put_kref(ftdi);
  460. return;
  461. } else {
  462. int retval = ftdi_elan_command_engine(ftdi);
  463. if (retval == -ESHUTDOWN) {
  464. ftdi->disconnected += 1;
  465. } else if (retval == -ENODEV) {
  466. ftdi->disconnected += 1;
  467. } else if (retval)
  468. dev_err(&ftdi->udev->dev, "command error %d\n", retval);
  469. ftdi_command_requeue_work(ftdi, msecs_to_jiffies(10));
  470. return;
  471. }
  472. }
  473. static void ftdi_elan_kick_respond_queue(struct usb_ftdi *ftdi)
  474. {
  475. ftdi_respond_queue_work(ftdi, 0);
  476. return;
  477. }
  478. static void ftdi_elan_respond_work(void *data)
  479. {
  480. struct usb_ftdi *ftdi = data;
  481. if (ftdi->disconnected > 0) {
  482. ftdi_elan_put_kref(ftdi);
  483. return;
  484. } else {
  485. int retval = ftdi_elan_respond_engine(ftdi);
  486. if (retval == 0) {
  487. } else if (retval == -ESHUTDOWN) {
  488. ftdi->disconnected += 1;
  489. } else if (retval == -ENODEV) {
  490. ftdi->disconnected += 1;
  491. } else if (retval == -EILSEQ) {
  492. ftdi->disconnected += 1;
  493. } else {
  494. ftdi->disconnected += 1;
  495. dev_err(&ftdi->udev->dev, "respond error %d\n", retval);
  496. }
  497. if (ftdi->disconnected > 0) {
  498. ftdi_elan_abandon_completions(ftdi);
  499. ftdi_elan_abandon_targets(ftdi);
  500. }
  501. ftdi_response_requeue_work(ftdi, msecs_to_jiffies(10));
  502. return;
  503. }
  504. }
  505. /*
  506. * the sw_lock is initially held and will be freed
  507. * after the FTDI has been synchronized
  508. *
  509. */
  510. static void ftdi_elan_status_work(void *data)
  511. {
  512. struct usb_ftdi *ftdi = data;
  513. int work_delay_in_msec = 0;
  514. if (ftdi->disconnected > 0) {
  515. ftdi_elan_put_kref(ftdi);
  516. return;
  517. } else if (ftdi->synchronized == 0) {
  518. down(&ftdi->sw_lock);
  519. if (ftdi_elan_synchronize(ftdi) == 0) {
  520. ftdi->synchronized = 1;
  521. ftdi_command_queue_work(ftdi, 1);
  522. ftdi_respond_queue_work(ftdi, 1);
  523. up(&ftdi->sw_lock);
  524. work_delay_in_msec = 100;
  525. } else {
  526. dev_err(&ftdi->udev->dev, "synchronize failed\n");
  527. up(&ftdi->sw_lock);
  528. work_delay_in_msec = 10 *1000;
  529. }
  530. } else if (ftdi->stuck_status > 0) {
  531. if (ftdi_elan_stuck_waiting(ftdi) == 0) {
  532. ftdi->stuck_status = 0;
  533. ftdi->synchronized = 0;
  534. } else if ((ftdi->stuck_status++ % 60) == 1) {
  535. dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
  536. "- please remove\n");
  537. } else
  538. dev_err(&ftdi->udev->dev, "WRONG type of card inserted "
  539. "- checked %d times\n", ftdi->stuck_status);
  540. work_delay_in_msec = 100;
  541. } else if (ftdi->enumerated == 0) {
  542. if (ftdi_elan_enumeratePCI(ftdi) == 0) {
  543. ftdi->enumerated = 1;
  544. work_delay_in_msec = 250;
  545. } else
  546. work_delay_in_msec = 1000;
  547. } else if (ftdi->initialized == 0) {
  548. if (ftdi_elan_setupOHCI(ftdi) == 0) {
  549. ftdi->initialized = 1;
  550. work_delay_in_msec = 500;
  551. } else {
  552. dev_err(&ftdi->udev->dev, "initialized failed - trying "
  553. "again in 10 seconds\n");
  554. work_delay_in_msec = 10 *1000;
  555. }
  556. } else if (ftdi->registered == 0) {
  557. work_delay_in_msec = 10;
  558. if (ftdi_elan_hcd_init(ftdi) == 0) {
  559. ftdi->registered = 1;
  560. } else
  561. dev_err(&ftdi->udev->dev, "register failed\n");
  562. work_delay_in_msec = 250;
  563. } else {
  564. if (ftdi_elan_checkingPCI(ftdi) == 0) {
  565. work_delay_in_msec = 250;
  566. } else if (ftdi->controlreg & 0x00400000) {
  567. if (ftdi->gone_away > 0) {
  568. dev_err(&ftdi->udev->dev, "PCI device eject con"
  569. "firmed platform_dev.dev.parent=%p plat"
  570. "form_dev.dev=%p\n",
  571. ftdi->platform_dev.dev.parent,
  572. &ftdi->platform_dev.dev);
  573. platform_device_unregister(&ftdi->platform_dev);
  574. ftdi->platform_dev.dev.parent = NULL;
  575. ftdi->registered = 0;
  576. ftdi->enumerated = 0;
  577. ftdi->card_ejected = 0;
  578. ftdi->initialized = 0;
  579. ftdi->gone_away = 0;
  580. } else
  581. ftdi_elan_flush_targets(ftdi);
  582. work_delay_in_msec = 250;
  583. } else {
  584. dev_err(&ftdi->udev->dev, "PCI device has disappeared\n"
  585. );
  586. ftdi_elan_cancel_targets(ftdi);
  587. work_delay_in_msec = 500;
  588. ftdi->enumerated = 0;
  589. ftdi->initialized = 0;
  590. }
  591. }
  592. if (ftdi->disconnected > 0) {
  593. ftdi_elan_put_kref(ftdi);
  594. return;
  595. } else {
  596. ftdi_status_requeue_work(ftdi,
  597. msecs_to_jiffies(work_delay_in_msec));
  598. return;
  599. }
  600. }
  601. /*
  602. * file_operations for the jtag interface
  603. *
  604. * the usage count for the device is incremented on open()
  605. * and decremented on release()
  606. */
  607. static int ftdi_elan_open(struct inode *inode, struct file *file)
  608. {
  609. int subminor = iminor(inode);
  610. struct usb_interface *interface = usb_find_interface(&ftdi_elan_driver,
  611. subminor);
  612. if (!interface) {
  613. printk(KERN_ERR "can't find device for minor %d\n", subminor);
  614. return -ENODEV;
  615. } else {
  616. struct usb_ftdi *ftdi = usb_get_intfdata(interface);
  617. if (!ftdi) {
  618. return -ENODEV;
  619. } else {
  620. if (down_interruptible(&ftdi->sw_lock)) {
  621. return -EINTR;
  622. } else {
  623. ftdi_elan_get_kref(ftdi);
  624. file->private_data = ftdi;
  625. return 0;
  626. }
  627. }
  628. }
  629. }
  630. static int ftdi_elan_release(struct inode *inode, struct file *file)
  631. {
  632. struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
  633. if (ftdi == NULL)
  634. return -ENODEV;
  635. up(&ftdi->sw_lock); /* decrement the count on our device */
  636. ftdi_elan_put_kref(ftdi);
  637. return 0;
  638. }
  639. #define FTDI_ELAN_IOC_MAGIC 0xA1
  640. #define FTDI_ELAN_IOCDEBUG _IOC(_IOC_WRITE, FTDI_ELAN_IOC_MAGIC, 1, 132)
  641. static int ftdi_elan_ioctl(struct inode *inode, struct file *file,
  642. unsigned int cmd, unsigned long arg)
  643. {
  644. switch (cmd) {
  645. case FTDI_ELAN_IOCDEBUG:{
  646. char line[132];
  647. int size = strncpy_from_user(line,
  648. (const char __user *)arg, sizeof(line));
  649. if (size < 0) {
  650. return -EINVAL;
  651. } else {
  652. printk(KERN_ERR "TODO: ioctl %s\n", line);
  653. return 0;
  654. }
  655. }
  656. default:
  657. return -EFAULT;
  658. }
  659. }
  660. /*
  661. *
  662. * blocking bulk reads are used to get data from the device
  663. *
  664. */
  665. static ssize_t ftdi_elan_read(struct file *file, char __user *buffer,
  666. size_t count, loff_t *ppos)
  667. {
  668. char data[30 *3 + 4];
  669. char *d = data;
  670. int m = (sizeof(data) - 1) / 3;
  671. int bytes_read = 0;
  672. int retry_on_empty = 10;
  673. int retry_on_timeout = 5;
  674. struct usb_ftdi *ftdi = (struct usb_ftdi *)file->private_data;
  675. if (ftdi->disconnected > 0) {
  676. return -ENODEV;
  677. }
  678. data[0] = 0;
  679. have:if (ftdi->bulk_in_left > 0) {
  680. if (count-- > 0) {
  681. char *p = ++ftdi->bulk_in_last + ftdi->bulk_in_buffer;
  682. ftdi->bulk_in_left -= 1;
  683. if (bytes_read < m) {
  684. d += sprintf(d, " %02X", 0x000000FF & *p);
  685. } else if (bytes_read > m) {
  686. } else
  687. d += sprintf(d, " ..");
  688. if (copy_to_user(buffer++, p, 1)) {
  689. return -EFAULT;
  690. } else {
  691. bytes_read += 1;
  692. goto have;
  693. }
  694. } else
  695. return bytes_read;
  696. }
  697. more:if (count > 0) {
  698. int packet_bytes = 0;
  699. int retval = usb_bulk_msg(ftdi->udev,
  700. usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
  701. ftdi->bulk_in_buffer, ftdi->bulk_in_size,
  702. &packet_bytes, msecs_to_jiffies(50));
  703. if (packet_bytes > 2) {
  704. ftdi->bulk_in_left = packet_bytes - 2;
  705. ftdi->bulk_in_last = 1;
  706. goto have;
  707. } else if (retval == -ETIMEDOUT) {
  708. if (retry_on_timeout-- > 0) {
  709. goto more;
  710. } else if (bytes_read > 0) {
  711. return bytes_read;
  712. } else
  713. return retval;
  714. } else if (retval == 0) {
  715. if (retry_on_empty-- > 0) {
  716. goto more;
  717. } else
  718. return bytes_read;
  719. } else
  720. return retval;
  721. } else
  722. return bytes_read;
  723. }
  724. static void ftdi_elan_write_bulk_callback(struct urb *urb)
  725. {
  726. struct usb_ftdi *ftdi = (struct usb_ftdi *)urb->context;
  727. if (urb->status && !(urb->status == -ENOENT || urb->status ==
  728. -ECONNRESET || urb->status == -ESHUTDOWN)) {
  729. dev_err(&ftdi->udev->dev, "urb=%p write bulk status received: %"
  730. "d\n", urb, urb->status);
  731. }
  732. usb_buffer_free(urb->dev, urb->transfer_buffer_length,
  733. urb->transfer_buffer, urb->transfer_dma);
  734. }
  735. static int fill_buffer_with_all_queued_commands(struct usb_ftdi *ftdi,
  736. char *buf, int command_size, int total_size)
  737. {
  738. int ed_commands = 0;
  739. int b = 0;
  740. int I = command_size;
  741. int i = ftdi->command_head;
  742. while (I-- > 0) {
  743. struct u132_command *command = &ftdi->command[COMMAND_MASK &
  744. i++];
  745. int F = command->follows;
  746. u8 *f = command->buffer;
  747. if (command->header & 0x80) {
  748. ed_commands |= 1 << (0x3 & (command->header >> 5));
  749. }
  750. buf[b++] = command->header;
  751. buf[b++] = (command->length >> 0) & 0x00FF;
  752. buf[b++] = (command->length >> 8) & 0x00FF;
  753. buf[b++] = command->address;
  754. buf[b++] = command->width;
  755. while (F-- > 0) {
  756. buf[b++] = *f++;
  757. }
  758. }
  759. return ed_commands;
  760. }
  761. static int ftdi_elan_total_command_size(struct usb_ftdi *ftdi, int command_size)
  762. {
  763. int total_size = 0;
  764. int I = command_size;
  765. int i = ftdi->command_head;
  766. while (I-- > 0) {
  767. struct u132_command *command = &ftdi->command[COMMAND_MASK &
  768. i++];
  769. total_size += 5 + command->follows;
  770. } return total_size;
  771. }
  772. static int ftdi_elan_command_engine(struct usb_ftdi *ftdi)
  773. {
  774. int retval;
  775. char *buf;
  776. int ed_commands;
  777. int total_size;
  778. struct urb *urb;
  779. int command_size = ftdi->command_next - ftdi->command_head;
  780. if (command_size == 0)
  781. return 0;
  782. total_size = ftdi_elan_total_command_size(ftdi, command_size);
  783. urb = usb_alloc_urb(0, GFP_KERNEL);
  784. if (!urb) {
  785. dev_err(&ftdi->udev->dev, "could not get a urb to write %d comm"
  786. "ands totaling %d bytes to the Uxxx\n", command_size,
  787. total_size);
  788. return -ENOMEM;
  789. }
  790. buf = usb_buffer_alloc(ftdi->udev, total_size, GFP_KERNEL,
  791. &urb->transfer_dma);
  792. if (!buf) {
  793. dev_err(&ftdi->udev->dev, "could not get a buffer to write %d c"
  794. "ommands totaling %d bytes to the Uxxx\n", command_size,
  795. total_size);
  796. usb_free_urb(urb);
  797. return -ENOMEM;
  798. }
  799. ed_commands = fill_buffer_with_all_queued_commands(ftdi, buf,
  800. command_size, total_size);
  801. usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
  802. ftdi->bulk_out_endpointAddr), buf, total_size,
  803. ftdi_elan_write_bulk_callback, ftdi);
  804. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  805. if (ed_commands) {
  806. char diag[40 *3 + 4];
  807. char *d = diag;
  808. int m = total_size;
  809. u8 *c = buf;
  810. int s = (sizeof(diag) - 1) / 3;
  811. diag[0] = 0;
  812. while (s-- > 0 && m-- > 0) {
  813. if (s > 0 || m == 0) {
  814. d += sprintf(d, " %02X", *c++);
  815. } else
  816. d += sprintf(d, " ..");
  817. }
  818. }
  819. retval = usb_submit_urb(urb, GFP_KERNEL);
  820. if (retval) {
  821. dev_err(&ftdi->udev->dev, "failed %d to submit urb %p to write "
  822. "%d commands totaling %d bytes to the Uxxx\n", retval,
  823. urb, command_size, total_size);
  824. usb_buffer_free(ftdi->udev, total_size, buf, urb->transfer_dma);
  825. usb_free_urb(urb);
  826. return retval;
  827. }
  828. usb_free_urb(urb); /* release our reference to this urb,
  829. the USB core will eventually free it entirely */
  830. ftdi->command_head += command_size;
  831. ftdi_elan_kick_respond_queue(ftdi);
  832. return 0;
  833. }
  834. static void ftdi_elan_do_callback(struct usb_ftdi *ftdi,
  835. struct u132_target *target, u8 *buffer, int length)
  836. {
  837. struct urb *urb = target->urb;
  838. int halted = target->halted;
  839. int skipped = target->skipped;
  840. int actual = target->actual;
  841. int non_null = target->non_null;
  842. int toggle_bits = target->toggle_bits;
  843. int error_count = target->error_count;
  844. int condition_code = target->condition_code;
  845. int repeat_number = target->repeat_number;
  846. void (*callback) (void *, struct urb *, u8 *, int, int, int, int, int,
  847. int, int, int, int) = target->callback;
  848. target->active -= 1;
  849. target->callback = NULL;
  850. (*callback) (target->endp, urb, buffer, length, toggle_bits,
  851. error_count, condition_code, repeat_number, halted, skipped,
  852. actual, non_null);
  853. }
  854. static char *have_ed_set_response(struct usb_ftdi *ftdi,
  855. struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
  856. char *b)
  857. {
  858. int payload = (ed_length >> 0) & 0x07FF;
  859. down(&ftdi->u132_lock);
  860. target->actual = 0;
  861. target->non_null = (ed_length >> 15) & 0x0001;
  862. target->repeat_number = (ed_length >> 11) & 0x000F;
  863. if (ed_type == 0x02) {
  864. if (payload == 0 || target->abandoning > 0) {
  865. target->abandoning = 0;
  866. up(&ftdi->u132_lock);
  867. ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
  868. payload);
  869. ftdi->recieved = 0;
  870. ftdi->expected = 4;
  871. ftdi->ed_found = 0;
  872. return ftdi->response;
  873. } else {
  874. ftdi->expected = 4 + payload;
  875. ftdi->ed_found = 1;
  876. up(&ftdi->u132_lock);
  877. return b;
  878. }
  879. } else if (ed_type == 0x03) {
  880. if (payload == 0 || target->abandoning > 0) {
  881. target->abandoning = 0;
  882. up(&ftdi->u132_lock);
  883. ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
  884. payload);
  885. ftdi->recieved = 0;
  886. ftdi->expected = 4;
  887. ftdi->ed_found = 0;
  888. return ftdi->response;
  889. } else {
  890. ftdi->expected = 4 + payload;
  891. ftdi->ed_found = 1;
  892. up(&ftdi->u132_lock);
  893. return b;
  894. }
  895. } else if (ed_type == 0x01) {
  896. target->abandoning = 0;
  897. up(&ftdi->u132_lock);
  898. ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
  899. payload);
  900. ftdi->recieved = 0;
  901. ftdi->expected = 4;
  902. ftdi->ed_found = 0;
  903. return ftdi->response;
  904. } else {
  905. target->abandoning = 0;
  906. up(&ftdi->u132_lock);
  907. ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
  908. payload);
  909. ftdi->recieved = 0;
  910. ftdi->expected = 4;
  911. ftdi->ed_found = 0;
  912. return ftdi->response;
  913. }
  914. }
  915. static char *have_ed_get_response(struct usb_ftdi *ftdi,
  916. struct u132_target *target, u16 ed_length, int ed_number, int ed_type,
  917. char *b)
  918. {
  919. down(&ftdi->u132_lock);
  920. target->condition_code = TD_DEVNOTRESP;
  921. target->actual = (ed_length >> 0) & 0x01FF;
  922. target->non_null = (ed_length >> 15) & 0x0001;
  923. target->repeat_number = (ed_length >> 11) & 0x000F;
  924. up(&ftdi->u132_lock);
  925. if (target->active)
  926. ftdi_elan_do_callback(ftdi, target, NULL, 0);
  927. target->abandoning = 0;
  928. ftdi->recieved = 0;
  929. ftdi->expected = 4;
  930. ftdi->ed_found = 0;
  931. return ftdi->response;
  932. }
  933. /*
  934. * The engine tries to empty the FTDI fifo
  935. *
  936. * all responses found in the fifo data are dispatched thus
  937. * the response buffer can only ever hold a maximum sized
  938. * response from the Uxxx.
  939. *
  940. */
  941. static int ftdi_elan_respond_engine(struct usb_ftdi *ftdi)
  942. {
  943. u8 *b = ftdi->response + ftdi->recieved;
  944. int bytes_read = 0;
  945. int retry_on_empty = 1;
  946. int retry_on_timeout = 3;
  947. int empty_packets = 0;
  948. read:{
  949. int packet_bytes = 0;
  950. int retval = usb_bulk_msg(ftdi->udev,
  951. usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
  952. ftdi->bulk_in_buffer, ftdi->bulk_in_size,
  953. &packet_bytes, msecs_to_jiffies(500));
  954. char diag[30 *3 + 4];
  955. char *d = diag;
  956. int m = packet_bytes;
  957. u8 *c = ftdi->bulk_in_buffer;
  958. int s = (sizeof(diag) - 1) / 3;
  959. diag[0] = 0;
  960. while (s-- > 0 && m-- > 0) {
  961. if (s > 0 || m == 0) {
  962. d += sprintf(d, " %02X", *c++);
  963. } else
  964. d += sprintf(d, " ..");
  965. }
  966. if (packet_bytes > 2) {
  967. ftdi->bulk_in_left = packet_bytes - 2;
  968. ftdi->bulk_in_last = 1;
  969. goto have;
  970. } else if (retval == -ETIMEDOUT) {
  971. if (retry_on_timeout-- > 0) {
  972. dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
  973. "t_bytes = %d with total %d bytes%s\n",
  974. packet_bytes, bytes_read, diag);
  975. goto more;
  976. } else if (bytes_read > 0) {
  977. dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
  978. bytes_read, diag);
  979. return -ENOMEM;
  980. } else {
  981. dev_err(&ftdi->udev->dev, "TIMED OUT with packe"
  982. "t_bytes = %d with total %d bytes%s\n",
  983. packet_bytes, bytes_read, diag);
  984. return -ENOMEM;
  985. }
  986. } else if (retval == -EILSEQ) {
  987. dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
  988. " = %d with total %d bytes%s\n", retval,
  989. packet_bytes, bytes_read, diag);
  990. return retval;
  991. } else if (retval) {
  992. dev_err(&ftdi->udev->dev, "error = %d with packet_bytes"
  993. " = %d with total %d bytes%s\n", retval,
  994. packet_bytes, bytes_read, diag);
  995. return retval;
  996. } else if (packet_bytes == 2) {
  997. unsigned char s0 = ftdi->bulk_in_buffer[0];
  998. unsigned char s1 = ftdi->bulk_in_buffer[1];
  999. empty_packets += 1;
  1000. if (s0 == 0x31 && s1 == 0x60) {
  1001. if (retry_on_empty-- > 0) {
  1002. goto more;
  1003. } else
  1004. return 0;
  1005. } else if (s0 == 0x31 && s1 == 0x00) {
  1006. if (retry_on_empty-- > 0) {
  1007. goto more;
  1008. } else
  1009. return 0;
  1010. } else {
  1011. if (retry_on_empty-- > 0) {
  1012. goto more;
  1013. } else
  1014. return 0;
  1015. }
  1016. } else if (packet_bytes == 1) {
  1017. if (retry_on_empty-- > 0) {
  1018. goto more;
  1019. } else
  1020. return 0;
  1021. } else {
  1022. if (retry_on_empty-- > 0) {
  1023. goto more;
  1024. } else
  1025. return 0;
  1026. }
  1027. }
  1028. more:{
  1029. goto read;
  1030. }
  1031. have:if (ftdi->bulk_in_left > 0) {
  1032. u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
  1033. bytes_read += 1;
  1034. ftdi->bulk_in_left -= 1;
  1035. if (ftdi->recieved == 0 && c == 0xFF) {
  1036. goto have;
  1037. } else
  1038. *b++ = c;
  1039. if (++ftdi->recieved < ftdi->expected) {
  1040. goto have;
  1041. } else if (ftdi->ed_found) {
  1042. int ed_number = (ftdi->response[0] >> 5) & 0x03;
  1043. u16 ed_length = (ftdi->response[2] << 8) |
  1044. ftdi->response[1];
  1045. struct u132_target *target = &ftdi->target[ed_number];
  1046. int payload = (ed_length >> 0) & 0x07FF;
  1047. char diag[30 *3 + 4];
  1048. char *d = diag;
  1049. int m = payload;
  1050. u8 *c = 4 + ftdi->response;
  1051. int s = (sizeof(diag) - 1) / 3;
  1052. diag[0] = 0;
  1053. while (s-- > 0 && m-- > 0) {
  1054. if (s > 0 || m == 0) {
  1055. d += sprintf(d, " %02X", *c++);
  1056. } else
  1057. d += sprintf(d, " ..");
  1058. }
  1059. ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
  1060. payload);
  1061. ftdi->recieved = 0;
  1062. ftdi->expected = 4;
  1063. ftdi->ed_found = 0;
  1064. b = ftdi->response;
  1065. goto have;
  1066. } else if (ftdi->expected == 8) {
  1067. u8 buscmd;
  1068. int respond_head = ftdi->respond_head++;
  1069. struct u132_respond *respond = &ftdi->respond[
  1070. RESPOND_MASK & respond_head];
  1071. u32 data = ftdi->response[7];
  1072. data <<= 8;
  1073. data |= ftdi->response[6];
  1074. data <<= 8;
  1075. data |= ftdi->response[5];
  1076. data <<= 8;
  1077. data |= ftdi->response[4];
  1078. *respond->value = data;
  1079. *respond->result = 0;
  1080. complete(&respond->wait_completion);
  1081. ftdi->recieved = 0;
  1082. ftdi->expected = 4;
  1083. ftdi->ed_found = 0;
  1084. b = ftdi->response;
  1085. buscmd = (ftdi->response[0] >> 0) & 0x0F;
  1086. if (buscmd == 0x00) {
  1087. } else if (buscmd == 0x02) {
  1088. } else if (buscmd == 0x06) {
  1089. } else if (buscmd == 0x0A) {
  1090. } else
  1091. dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) va"
  1092. "lue = %08X\n", buscmd, data);
  1093. goto have;
  1094. } else {
  1095. if ((ftdi->response[0] & 0x80) == 0x00) {
  1096. ftdi->expected = 8;
  1097. goto have;
  1098. } else {
  1099. int ed_number = (ftdi->response[0] >> 5) & 0x03;
  1100. int ed_type = (ftdi->response[0] >> 0) & 0x03;
  1101. u16 ed_length = (ftdi->response[2] << 8) |
  1102. ftdi->response[1];
  1103. struct u132_target *target = &ftdi->target[
  1104. ed_number];
  1105. target->halted = (ftdi->response[0] >> 3) &
  1106. 0x01;
  1107. target->skipped = (ftdi->response[0] >> 2) &
  1108. 0x01;
  1109. target->toggle_bits = (ftdi->response[3] >> 6)
  1110. & 0x03;
  1111. target->error_count = (ftdi->response[3] >> 4)
  1112. & 0x03;
  1113. target->condition_code = (ftdi->response[
  1114. 3] >> 0) & 0x0F;
  1115. if ((ftdi->response[0] & 0x10) == 0x00) {
  1116. b = have_ed_set_response(ftdi, target,
  1117. ed_length, ed_number, ed_type,
  1118. b);
  1119. goto have;
  1120. } else {
  1121. b = have_ed_get_response(ftdi, target,
  1122. ed_length, ed_number, ed_type,
  1123. b);
  1124. goto have;
  1125. }
  1126. }
  1127. }
  1128. } else
  1129. goto more;
  1130. }
  1131. /*
  1132. * create a urb, and a buffer for it, and copy the data to the urb
  1133. *
  1134. */
  1135. static ssize_t ftdi_elan_write(struct file *file,
  1136. const char __user *user_buffer, size_t count,
  1137. loff_t *ppos)
  1138. {
  1139. int retval = 0;
  1140. struct urb *urb;
  1141. char *buf;
  1142. struct usb_ftdi *ftdi = file->private_data;
  1143. if (ftdi->disconnected > 0) {
  1144. return -ENODEV;
  1145. }
  1146. if (count == 0) {
  1147. goto exit;
  1148. }
  1149. urb = usb_alloc_urb(0, GFP_KERNEL);
  1150. if (!urb) {
  1151. retval = -ENOMEM;
  1152. goto error_1;
  1153. }
  1154. buf = usb_buffer_alloc(ftdi->udev, count, GFP_KERNEL,
  1155. &urb->transfer_dma);
  1156. if (!buf) {
  1157. retval = -ENOMEM;
  1158. goto error_2;
  1159. }
  1160. if (copy_from_user(buf, user_buffer, count)) {
  1161. retval = -EFAULT;
  1162. goto error_3;
  1163. }
  1164. usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
  1165. ftdi->bulk_out_endpointAddr), buf, count,
  1166. ftdi_elan_write_bulk_callback, ftdi);
  1167. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1168. retval = usb_submit_urb(urb, GFP_KERNEL);
  1169. if (retval) {
  1170. dev_err(&ftdi->udev->dev, "failed submitting write urb, error %"
  1171. "d\n", retval);
  1172. goto error_3;
  1173. }
  1174. usb_free_urb(urb);
  1175. exit:
  1176. return count;
  1177. error_3:
  1178. usb_buffer_free(ftdi->udev, count, buf, urb->transfer_dma);
  1179. error_2:
  1180. usb_free_urb(urb);
  1181. error_1:
  1182. return retval;
  1183. }
  1184. static struct file_operations ftdi_elan_fops = {
  1185. .owner = THIS_MODULE,
  1186. .llseek = no_llseek,
  1187. .ioctl = ftdi_elan_ioctl,
  1188. .read = ftdi_elan_read,
  1189. .write = ftdi_elan_write,
  1190. .open = ftdi_elan_open,
  1191. .release = ftdi_elan_release,
  1192. };
  1193. /*
  1194. * usb class driver info in order to get a minor number from the usb core,
  1195. * and to have the device registered with the driver core
  1196. */
  1197. static struct usb_class_driver ftdi_elan_jtag_class = {
  1198. .name = "ftdi-%d-jtag",
  1199. .fops = &ftdi_elan_fops,
  1200. .minor_base = USB_FTDI_ELAN_MINOR_BASE,
  1201. };
  1202. /*
  1203. * the following definitions are for the
  1204. * ELAN FPGA state machgine processor that
  1205. * lies on the other side of the FTDI chip
  1206. */
  1207. #define cPCIu132rd 0x0
  1208. #define cPCIu132wr 0x1
  1209. #define cPCIiord 0x2
  1210. #define cPCIiowr 0x3
  1211. #define cPCImemrd 0x6
  1212. #define cPCImemwr 0x7
  1213. #define cPCIcfgrd 0xA
  1214. #define cPCIcfgwr 0xB
  1215. #define cPCInull 0xF
  1216. #define cU132cmd_status 0x0
  1217. #define cU132flash 0x1
  1218. #define cPIDsetup 0x0
  1219. #define cPIDout 0x1
  1220. #define cPIDin 0x2
  1221. #define cPIDinonce 0x3
  1222. #define cCCnoerror 0x0
  1223. #define cCCcrc 0x1
  1224. #define cCCbitstuff 0x2
  1225. #define cCCtoggle 0x3
  1226. #define cCCstall 0x4
  1227. #define cCCnoresp 0x5
  1228. #define cCCbadpid1 0x6
  1229. #define cCCbadpid2 0x7
  1230. #define cCCdataoverrun 0x8
  1231. #define cCCdataunderrun 0x9
  1232. #define cCCbuffoverrun 0xC
  1233. #define cCCbuffunderrun 0xD
  1234. #define cCCnotaccessed 0xF
  1235. static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
  1236. {
  1237. wait:if (ftdi->disconnected > 0) {
  1238. return -ENODEV;
  1239. } else {
  1240. int command_size;
  1241. down(&ftdi->u132_lock);
  1242. command_size = ftdi->command_next - ftdi->command_head;
  1243. if (command_size < COMMAND_SIZE) {
  1244. struct u132_command *command = &ftdi->command[
  1245. COMMAND_MASK & ftdi->command_next];
  1246. command->header = 0x00 | cPCIu132wr;
  1247. command->length = 0x04;
  1248. command->address = 0x00;
  1249. command->width = 0x00;
  1250. command->follows = 4;
  1251. command->value = data;
  1252. command->buffer = &command->value;
  1253. ftdi->command_next += 1;
  1254. ftdi_elan_kick_command_queue(ftdi);
  1255. up(&ftdi->u132_lock);
  1256. return 0;
  1257. } else {
  1258. up(&ftdi->u132_lock);
  1259. msleep(100);
  1260. goto wait;
  1261. }
  1262. }
  1263. }
  1264. static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
  1265. u8 width, u32 data)
  1266. {
  1267. u8 addressofs = config_offset / 4;
  1268. wait:if (ftdi->disconnected > 0) {
  1269. return -ENODEV;
  1270. } else {
  1271. int command_size;
  1272. down(&ftdi->u132_lock);
  1273. command_size = ftdi->command_next - ftdi->command_head;
  1274. if (command_size < COMMAND_SIZE) {
  1275. struct u132_command *command = &ftdi->command[
  1276. COMMAND_MASK & ftdi->command_next];
  1277. command->header = 0x00 | (cPCIcfgwr & 0x0F);
  1278. command->length = 0x04;
  1279. command->address = addressofs;
  1280. command->width = 0x00 | (width & 0x0F);
  1281. command->follows = 4;
  1282. command->value = data;
  1283. command->buffer = &command->value;
  1284. ftdi->command_next += 1;
  1285. ftdi_elan_kick_command_queue(ftdi);
  1286. up(&ftdi->u132_lock);
  1287. return 0;
  1288. } else {
  1289. up(&ftdi->u132_lock);
  1290. msleep(100);
  1291. goto wait;
  1292. }
  1293. }
  1294. }
  1295. static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
  1296. u8 width, u32 data)
  1297. {
  1298. u8 addressofs = mem_offset / 4;
  1299. wait:if (ftdi->disconnected > 0) {
  1300. return -ENODEV;
  1301. } else {
  1302. int command_size;
  1303. down(&ftdi->u132_lock);
  1304. command_size = ftdi->command_next - ftdi->command_head;
  1305. if (command_size < COMMAND_SIZE) {
  1306. struct u132_command *command = &ftdi->command[
  1307. COMMAND_MASK & ftdi->command_next];
  1308. command->header = 0x00 | (cPCImemwr & 0x0F);
  1309. command->length = 0x04;
  1310. command->address = addressofs;
  1311. command->width = 0x00 | (width & 0x0F);
  1312. command->follows = 4;
  1313. command->value = data;
  1314. command->buffer = &command->value;
  1315. ftdi->command_next += 1;
  1316. ftdi_elan_kick_command_queue(ftdi);
  1317. up(&ftdi->u132_lock);
  1318. return 0;
  1319. } else {
  1320. up(&ftdi->u132_lock);
  1321. msleep(100);
  1322. goto wait;
  1323. }
  1324. }
  1325. }
  1326. int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
  1327. u8 width, u32 data)
  1328. {
  1329. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1330. return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
  1331. }
  1332. EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
  1333. static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
  1334. {
  1335. wait:if (ftdi->disconnected > 0) {
  1336. return -ENODEV;
  1337. } else {
  1338. int command_size;
  1339. int respond_size;
  1340. down(&ftdi->u132_lock);
  1341. command_size = ftdi->command_next - ftdi->command_head;
  1342. respond_size = ftdi->respond_next - ftdi->respond_head;
  1343. if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
  1344. {
  1345. struct u132_command *command = &ftdi->command[
  1346. COMMAND_MASK & ftdi->command_next];
  1347. struct u132_respond *respond = &ftdi->respond[
  1348. RESPOND_MASK & ftdi->respond_next];
  1349. int result = -ENODEV;
  1350. respond->result = &result;
  1351. respond->header = command->header = 0x00 | cPCIu132rd;
  1352. command->length = 0x04;
  1353. respond->address = command->address = cU132cmd_status;
  1354. command->width = 0x00;
  1355. command->follows = 0;
  1356. command->value = 0;
  1357. command->buffer = NULL;
  1358. respond->value = data;
  1359. init_completion(&respond->wait_completion);
  1360. ftdi->command_next += 1;
  1361. ftdi->respond_next += 1;
  1362. ftdi_elan_kick_command_queue(ftdi);
  1363. up(&ftdi->u132_lock);
  1364. wait_for_completion(&respond->wait_completion);
  1365. return result;
  1366. } else {
  1367. up(&ftdi->u132_lock);
  1368. msleep(100);
  1369. goto wait;
  1370. }
  1371. }
  1372. }
  1373. static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
  1374. u8 width, u32 *data)
  1375. {
  1376. u8 addressofs = config_offset / 4;
  1377. wait:if (ftdi->disconnected > 0) {
  1378. return -ENODEV;
  1379. } else {
  1380. int command_size;
  1381. int respond_size;
  1382. down(&ftdi->u132_lock);
  1383. command_size = ftdi->command_next - ftdi->command_head;
  1384. respond_size = ftdi->respond_next - ftdi->respond_head;
  1385. if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
  1386. {
  1387. struct u132_command *command = &ftdi->command[
  1388. COMMAND_MASK & ftdi->command_next];
  1389. struct u132_respond *respond = &ftdi->respond[
  1390. RESPOND_MASK & ftdi->respond_next];
  1391. int result = -ENODEV;
  1392. respond->result = &result;
  1393. respond->header = command->header = 0x00 | (cPCIcfgrd &
  1394. 0x0F);
  1395. command->length = 0x04;
  1396. respond->address = command->address = addressofs;
  1397. command->width = 0x00 | (width & 0x0F);
  1398. command->follows = 0;
  1399. command->value = 0;
  1400. command->buffer = NULL;
  1401. respond->value = data;
  1402. init_completion(&respond->wait_completion);
  1403. ftdi->command_next += 1;
  1404. ftdi->respond_next += 1;
  1405. ftdi_elan_kick_command_queue(ftdi);
  1406. up(&ftdi->u132_lock);
  1407. wait_for_completion(&respond->wait_completion);
  1408. return result;
  1409. } else {
  1410. up(&ftdi->u132_lock);
  1411. msleep(100);
  1412. goto wait;
  1413. }
  1414. }
  1415. }
  1416. static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
  1417. u8 width, u32 *data)
  1418. {
  1419. u8 addressofs = mem_offset / 4;
  1420. wait:if (ftdi->disconnected > 0) {
  1421. return -ENODEV;
  1422. } else {
  1423. int command_size;
  1424. int respond_size;
  1425. down(&ftdi->u132_lock);
  1426. command_size = ftdi->command_next - ftdi->command_head;
  1427. respond_size = ftdi->respond_next - ftdi->respond_head;
  1428. if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
  1429. {
  1430. struct u132_command *command = &ftdi->command[
  1431. COMMAND_MASK & ftdi->command_next];
  1432. struct u132_respond *respond = &ftdi->respond[
  1433. RESPOND_MASK & ftdi->respond_next];
  1434. int result = -ENODEV;
  1435. respond->result = &result;
  1436. respond->header = command->header = 0x00 | (cPCImemrd &
  1437. 0x0F);
  1438. command->length = 0x04;
  1439. respond->address = command->address = addressofs;
  1440. command->width = 0x00 | (width & 0x0F);
  1441. command->follows = 0;
  1442. command->value = 0;
  1443. command->buffer = NULL;
  1444. respond->value = data;
  1445. init_completion(&respond->wait_completion);
  1446. ftdi->command_next += 1;
  1447. ftdi->respond_next += 1;
  1448. ftdi_elan_kick_command_queue(ftdi);
  1449. up(&ftdi->u132_lock);
  1450. wait_for_completion(&respond->wait_completion);
  1451. return result;
  1452. } else {
  1453. up(&ftdi->u132_lock);
  1454. msleep(100);
  1455. goto wait;
  1456. }
  1457. }
  1458. }
  1459. int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
  1460. u8 width, u32 *data)
  1461. {
  1462. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1463. if (ftdi->initialized == 0) {
  1464. return -ENODEV;
  1465. } else
  1466. return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
  1467. }
  1468. EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
  1469. static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
  1470. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1471. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1472. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1473. int halted, int skipped, int actual, int non_null))
  1474. {
  1475. u8 ed = ed_number - 1;
  1476. wait:if (ftdi->disconnected > 0) {
  1477. return -ENODEV;
  1478. } else if (ftdi->initialized == 0) {
  1479. return -ENODEV;
  1480. } else {
  1481. int command_size;
  1482. down(&ftdi->u132_lock);
  1483. command_size = ftdi->command_next - ftdi->command_head;
  1484. if (command_size < COMMAND_SIZE) {
  1485. struct u132_target *target = &ftdi->target[ed];
  1486. struct u132_command *command = &ftdi->command[
  1487. COMMAND_MASK & ftdi->command_next];
  1488. command->header = 0x80 | (ed << 5);
  1489. command->length = 0x8007;
  1490. command->address = (toggle_bits << 6) | (ep_number << 2)
  1491. | (address << 0);
  1492. command->width = usb_maxpacket(urb->dev, urb->pipe,
  1493. usb_pipeout(urb->pipe));
  1494. command->follows = 8;
  1495. command->value = 0;
  1496. command->buffer = urb->setup_packet;
  1497. target->callback = callback;
  1498. target->endp = endp;
  1499. target->urb = urb;
  1500. target->active = 1;
  1501. ftdi->command_next += 1;
  1502. ftdi_elan_kick_command_queue(ftdi);
  1503. up(&ftdi->u132_lock);
  1504. return 0;
  1505. } else {
  1506. up(&ftdi->u132_lock);
  1507. msleep(100);
  1508. goto wait;
  1509. }
  1510. }
  1511. }
  1512. int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
  1513. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1514. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1515. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1516. int halted, int skipped, int actual, int non_null))
  1517. {
  1518. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1519. return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
  1520. ep_number, toggle_bits, callback);
  1521. }
  1522. EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
  1523. static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
  1524. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1525. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1526. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1527. int halted, int skipped, int actual, int non_null))
  1528. {
  1529. u8 ed = ed_number - 1;
  1530. wait:if (ftdi->disconnected > 0) {
  1531. return -ENODEV;
  1532. } else if (ftdi->initialized == 0) {
  1533. return -ENODEV;
  1534. } else {
  1535. int command_size;
  1536. down(&ftdi->u132_lock);
  1537. command_size = ftdi->command_next - ftdi->command_head;
  1538. if (command_size < COMMAND_SIZE) {
  1539. struct u132_target *target = &ftdi->target[ed];
  1540. struct u132_command *command = &ftdi->command[
  1541. COMMAND_MASK & ftdi->command_next];
  1542. int remaining_length = urb->transfer_buffer_length -
  1543. urb->actual_length;
  1544. command->header = 0x82 | (ed << 5);
  1545. if (remaining_length == 0) {
  1546. command->length = 0x0000;
  1547. } else if (remaining_length > 1024) {
  1548. command->length = 0x8000 | 1023;
  1549. } else
  1550. command->length = 0x8000 | (remaining_length -
  1551. 1);
  1552. command->address = (toggle_bits << 6) | (ep_number << 2)
  1553. | (address << 0);
  1554. command->width = usb_maxpacket(urb->dev, urb->pipe,
  1555. usb_pipeout(urb->pipe));
  1556. command->follows = 0;
  1557. command->value = 0;
  1558. command->buffer = NULL;
  1559. target->callback = callback;
  1560. target->endp = endp;
  1561. target->urb = urb;
  1562. target->active = 1;
  1563. ftdi->command_next += 1;
  1564. ftdi_elan_kick_command_queue(ftdi);
  1565. up(&ftdi->u132_lock);
  1566. return 0;
  1567. } else {
  1568. up(&ftdi->u132_lock);
  1569. msleep(100);
  1570. goto wait;
  1571. }
  1572. }
  1573. }
  1574. int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
  1575. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1576. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1577. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1578. int halted, int skipped, int actual, int non_null))
  1579. {
  1580. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1581. return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
  1582. ep_number, toggle_bits, callback);
  1583. }
  1584. EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
  1585. static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
  1586. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1587. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1588. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1589. int halted, int skipped, int actual, int non_null))
  1590. {
  1591. u8 ed = ed_number - 1;
  1592. wait:if (ftdi->disconnected > 0) {
  1593. return -ENODEV;
  1594. } else if (ftdi->initialized == 0) {
  1595. return -ENODEV;
  1596. } else {
  1597. int command_size;
  1598. down(&ftdi->u132_lock);
  1599. command_size = ftdi->command_next - ftdi->command_head;
  1600. if (command_size < COMMAND_SIZE) {
  1601. struct u132_target *target = &ftdi->target[ed];
  1602. struct u132_command *command = &ftdi->command[
  1603. COMMAND_MASK & ftdi->command_next];
  1604. command->header = 0x81 | (ed << 5);
  1605. command->length = 0x0000;
  1606. command->address = (toggle_bits << 6) | (ep_number << 2)
  1607. | (address << 0);
  1608. command->width = usb_maxpacket(urb->dev, urb->pipe,
  1609. usb_pipeout(urb->pipe));
  1610. command->follows = 0;
  1611. command->value = 0;
  1612. command->buffer = NULL;
  1613. target->callback = callback;
  1614. target->endp = endp;
  1615. target->urb = urb;
  1616. target->active = 1;
  1617. ftdi->command_next += 1;
  1618. ftdi_elan_kick_command_queue(ftdi);
  1619. up(&ftdi->u132_lock);
  1620. return 0;
  1621. } else {
  1622. up(&ftdi->u132_lock);
  1623. msleep(100);
  1624. goto wait;
  1625. }
  1626. }
  1627. }
  1628. int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
  1629. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1630. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1631. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1632. int halted, int skipped, int actual, int non_null))
  1633. {
  1634. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1635. return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
  1636. ep_number, toggle_bits, callback);
  1637. }
  1638. EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
  1639. static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
  1640. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1641. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1642. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1643. int halted, int skipped, int actual, int non_null))
  1644. {
  1645. u8 ed = ed_number - 1;
  1646. wait:if (ftdi->disconnected > 0) {
  1647. return -ENODEV;
  1648. } else if (ftdi->initialized == 0) {
  1649. return -ENODEV;
  1650. } else {
  1651. int command_size;
  1652. down(&ftdi->u132_lock);
  1653. command_size = ftdi->command_next - ftdi->command_head;
  1654. if (command_size < COMMAND_SIZE) {
  1655. u8 *b;
  1656. u16 urb_size;
  1657. int i = 0;
  1658. char data[30 *3 + 4];
  1659. char *d = data;
  1660. int m = (sizeof(data) - 1) / 3;
  1661. int l = 0;
  1662. struct u132_target *target = &ftdi->target[ed];
  1663. struct u132_command *command = &ftdi->command[
  1664. COMMAND_MASK & ftdi->command_next];
  1665. command->header = 0x81 | (ed << 5);
  1666. command->address = (toggle_bits << 6) | (ep_number << 2)
  1667. | (address << 0);
  1668. command->width = usb_maxpacket(urb->dev, urb->pipe,
  1669. usb_pipeout(urb->pipe));
  1670. command->follows = min(1024,
  1671. urb->transfer_buffer_length -
  1672. urb->actual_length);
  1673. command->value = 0;
  1674. command->buffer = urb->transfer_buffer +
  1675. urb->actual_length;
  1676. command->length = 0x8000 | (command->follows - 1);
  1677. b = command->buffer;
  1678. urb_size = command->follows;
  1679. data[0] = 0;
  1680. while (urb_size-- > 0) {
  1681. if (i > m) {
  1682. } else if (i++ < m) {
  1683. int w = sprintf(d, " %02X", *b++);
  1684. d += w;
  1685. l += w;
  1686. } else
  1687. d += sprintf(d, " ..");
  1688. }
  1689. target->callback = callback;
  1690. target->endp = endp;
  1691. target->urb = urb;
  1692. target->active = 1;
  1693. ftdi->command_next += 1;
  1694. ftdi_elan_kick_command_queue(ftdi);
  1695. up(&ftdi->u132_lock);
  1696. return 0;
  1697. } else {
  1698. up(&ftdi->u132_lock);
  1699. msleep(100);
  1700. goto wait;
  1701. }
  1702. }
  1703. }
  1704. int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
  1705. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1706. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1707. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1708. int halted, int skipped, int actual, int non_null))
  1709. {
  1710. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1711. return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
  1712. ep_number, toggle_bits, callback);
  1713. }
  1714. EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
  1715. static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
  1716. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1717. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1718. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1719. int halted, int skipped, int actual, int non_null))
  1720. {
  1721. u8 ed = ed_number - 1;
  1722. wait:if (ftdi->disconnected > 0) {
  1723. return -ENODEV;
  1724. } else if (ftdi->initialized == 0) {
  1725. return -ENODEV;
  1726. } else {
  1727. int command_size;
  1728. down(&ftdi->u132_lock);
  1729. command_size = ftdi->command_next - ftdi->command_head;
  1730. if (command_size < COMMAND_SIZE) {
  1731. int remaining_length = urb->transfer_buffer_length -
  1732. urb->actual_length;
  1733. struct u132_target *target = &ftdi->target[ed];
  1734. struct u132_command *command = &ftdi->command[
  1735. COMMAND_MASK & ftdi->command_next];
  1736. command->header = 0x83 | (ed << 5);
  1737. if (remaining_length == 0) {
  1738. command->length = 0x0000;
  1739. } else if (remaining_length > 1024) {
  1740. command->length = 0x8000 | 1023;
  1741. } else
  1742. command->length = 0x8000 | (remaining_length -
  1743. 1);
  1744. command->address = (toggle_bits << 6) | (ep_number << 2)
  1745. | (address << 0);
  1746. command->width = usb_maxpacket(urb->dev, urb->pipe,
  1747. usb_pipeout(urb->pipe));
  1748. command->follows = 0;
  1749. command->value = 0;
  1750. command->buffer = NULL;
  1751. target->callback = callback;
  1752. target->endp = endp;
  1753. target->urb = urb;
  1754. target->active = 1;
  1755. ftdi->command_next += 1;
  1756. ftdi_elan_kick_command_queue(ftdi);
  1757. up(&ftdi->u132_lock);
  1758. return 0;
  1759. } else {
  1760. up(&ftdi->u132_lock);
  1761. msleep(100);
  1762. goto wait;
  1763. }
  1764. }
  1765. }
  1766. int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
  1767. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1768. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1769. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1770. int halted, int skipped, int actual, int non_null))
  1771. {
  1772. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1773. return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
  1774. ep_number, toggle_bits, callback);
  1775. }
  1776. EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
  1777. static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
  1778. void *endp)
  1779. {
  1780. u8 ed = ed_number - 1;
  1781. if (ftdi->disconnected > 0) {
  1782. return -ENODEV;
  1783. } else if (ftdi->initialized == 0) {
  1784. return -ENODEV;
  1785. } else {
  1786. struct u132_target *target = &ftdi->target[ed];
  1787. down(&ftdi->u132_lock);
  1788. if (target->abandoning > 0) {
  1789. up(&ftdi->u132_lock);
  1790. return 0;
  1791. } else {
  1792. target->abandoning = 1;
  1793. wait_1:if (target->active == 1) {
  1794. int command_size = ftdi->command_next -
  1795. ftdi->command_head;
  1796. if (command_size < COMMAND_SIZE) {
  1797. struct u132_command *command =
  1798. &ftdi->command[COMMAND_MASK &
  1799. ftdi->command_next];
  1800. command->header = 0x80 | (ed << 5) |
  1801. 0x4;
  1802. command->length = 0x00;
  1803. command->address = 0x00;
  1804. command->width = 0x00;
  1805. command->follows = 0;
  1806. command->value = 0;
  1807. command->buffer = &command->value;
  1808. ftdi->command_next += 1;
  1809. ftdi_elan_kick_command_queue(ftdi);
  1810. } else {
  1811. up(&ftdi->u132_lock);
  1812. msleep(100);
  1813. down(&ftdi->u132_lock);
  1814. goto wait_1;
  1815. }
  1816. }
  1817. up(&ftdi->u132_lock);
  1818. return 0;
  1819. }
  1820. }
  1821. }
  1822. int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
  1823. void *endp)
  1824. {
  1825. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1826. return ftdi_elan_edset_flush(ftdi, ed_number, endp);
  1827. }
  1828. EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
  1829. static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
  1830. {
  1831. int retry_on_empty = 10;
  1832. int retry_on_timeout = 5;
  1833. int retry_on_status = 20;
  1834. more:{
  1835. int packet_bytes = 0;
  1836. int retval = usb_bulk_msg(ftdi->udev,
  1837. usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
  1838. ftdi->bulk_in_buffer, ftdi->bulk_in_size,
  1839. &packet_bytes, msecs_to_jiffies(100));
  1840. if (packet_bytes > 2) {
  1841. char diag[30 *3 + 4];
  1842. char *d = diag;
  1843. int m = (sizeof(diag) - 1) / 3;
  1844. char *b = ftdi->bulk_in_buffer;
  1845. int bytes_read = 0;
  1846. diag[0] = 0;
  1847. while (packet_bytes-- > 0) {
  1848. char c = *b++;
  1849. if (bytes_read < m) {
  1850. d += sprintf(d, " %02X",
  1851. 0x000000FF & c);
  1852. } else if (bytes_read > m) {
  1853. } else
  1854. d += sprintf(d, " ..");
  1855. bytes_read += 1;
  1856. continue;
  1857. }
  1858. goto more;
  1859. } else if (packet_bytes > 1) {
  1860. char s1 = ftdi->bulk_in_buffer[0];
  1861. char s2 = ftdi->bulk_in_buffer[1];
  1862. if (s1 == 0x31 && s2 == 0x60) {
  1863. return 0;
  1864. } else if (retry_on_status-- > 0) {
  1865. goto more;
  1866. } else {
  1867. dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
  1868. "imit reached\n");
  1869. return -EFAULT;
  1870. }
  1871. } else if (packet_bytes > 0) {
  1872. char b1 = ftdi->bulk_in_buffer[0];
  1873. dev_err(&ftdi->udev->dev, "only one byte flushed from F"
  1874. "TDI = %02X\n", b1);
  1875. if (retry_on_status-- > 0) {
  1876. goto more;
  1877. } else {
  1878. dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
  1879. "imit reached\n");
  1880. return -EFAULT;
  1881. }
  1882. } else if (retval == -ETIMEDOUT) {
  1883. if (retry_on_timeout-- > 0) {
  1884. goto more;
  1885. } else {
  1886. dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
  1887. "t reached\n");
  1888. return -ENOMEM;
  1889. }
  1890. } else if (retval == 0) {
  1891. if (retry_on_empty-- > 0) {
  1892. goto more;
  1893. } else {
  1894. dev_err(&ftdi->udev->dev, "empty packet retry l"
  1895. "imit reached\n");
  1896. return -ENOMEM;
  1897. }
  1898. } else {
  1899. dev_err(&ftdi->udev->dev, "error = %d\n", retval);
  1900. return retval;
  1901. }
  1902. }
  1903. return -1;
  1904. }
  1905. /*
  1906. * send the long flush sequence
  1907. *
  1908. */
  1909. static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
  1910. {
  1911. int retval;
  1912. struct urb *urb;
  1913. char *buf;
  1914. int I = 257;
  1915. int i = 0;
  1916. urb = usb_alloc_urb(0, GFP_KERNEL);
  1917. if (!urb) {
  1918. dev_err(&ftdi->udev->dev, "could not alloc a urb for flush sequ"
  1919. "ence\n");
  1920. return -ENOMEM;
  1921. }
  1922. buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
  1923. if (!buf) {
  1924. dev_err(&ftdi->udev->dev, "could not get a buffer for flush seq"
  1925. "uence\n");
  1926. usb_free_urb(urb);
  1927. return -ENOMEM;
  1928. }
  1929. while (I-- > 0)
  1930. buf[i++] = 0x55;
  1931. usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
  1932. ftdi->bulk_out_endpointAddr), buf, i,
  1933. ftdi_elan_write_bulk_callback, ftdi);
  1934. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1935. retval = usb_submit_urb(urb, GFP_KERNEL);
  1936. if (retval) {
  1937. dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
  1938. "flush sequence\n");
  1939. usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
  1940. usb_free_urb(urb);
  1941. return -ENOMEM;
  1942. }
  1943. usb_free_urb(urb);
  1944. return 0;
  1945. }
  1946. /*
  1947. * send the reset sequence
  1948. *
  1949. */
  1950. static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
  1951. {
  1952. int retval;
  1953. struct urb *urb;
  1954. char *buf;
  1955. int I = 4;
  1956. int i = 0;
  1957. urb = usb_alloc_urb(0, GFP_KERNEL);
  1958. if (!urb) {
  1959. dev_err(&ftdi->udev->dev, "could not get a urb for the reset se"
  1960. "quence\n");
  1961. return -ENOMEM;
  1962. }
  1963. buf = usb_buffer_alloc(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
  1964. if (!buf) {
  1965. dev_err(&ftdi->udev->dev, "could not get a buffer for the reset"
  1966. " sequence\n");
  1967. usb_free_urb(urb);
  1968. return -ENOMEM;
  1969. }
  1970. buf[i++] = 0x55;
  1971. buf[i++] = 0xAA;
  1972. buf[i++] = 0x5A;
  1973. buf[i++] = 0xA5;
  1974. usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
  1975. ftdi->bulk_out_endpointAddr), buf, i,
  1976. ftdi_elan_write_bulk_callback, ftdi);
  1977. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1978. retval = usb_submit_urb(urb, GFP_KERNEL);
  1979. if (retval) {
  1980. dev_err(&ftdi->udev->dev, "failed to submit urb containing the "
  1981. "reset sequence\n");
  1982. usb_buffer_free(ftdi->udev, i, buf, urb->transfer_dma);
  1983. usb_free_urb(urb);
  1984. return -ENOMEM;
  1985. }
  1986. usb_free_urb(urb);
  1987. return 0;
  1988. }
  1989. static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
  1990. {
  1991. int retval;
  1992. int long_stop = 10;
  1993. int retry_on_timeout = 5;
  1994. int retry_on_empty = 10;
  1995. int err_count = 0;
  1996. retval = ftdi_elan_flush_input_fifo(ftdi);
  1997. if (retval)
  1998. return retval;
  1999. ftdi->bulk_in_left = 0;
  2000. ftdi->bulk_in_last = -1;
  2001. while (long_stop-- > 0) {
  2002. int read_stop;
  2003. int read_stuck;
  2004. retval = ftdi_elan_synchronize_flush(ftdi);
  2005. if (retval)
  2006. return retval;
  2007. retval = ftdi_elan_flush_input_fifo(ftdi);
  2008. if (retval)
  2009. return retval;
  2010. reset:retval = ftdi_elan_synchronize_reset(ftdi);
  2011. if (retval)
  2012. return retval;
  2013. read_stop = 100;
  2014. read_stuck = 10;
  2015. read:{
  2016. int packet_bytes = 0;
  2017. retval = usb_bulk_msg(ftdi->udev,
  2018. usb_rcvbulkpipe(ftdi->udev,
  2019. ftdi->bulk_in_endpointAddr),
  2020. ftdi->bulk_in_buffer, ftdi->bulk_in_size,
  2021. &packet_bytes, msecs_to_jiffies(500));
  2022. if (packet_bytes > 2) {
  2023. char diag[30 *3 + 4];
  2024. char *d = diag;
  2025. int m = (sizeof(diag) - 1) / 3;
  2026. char *b = ftdi->bulk_in_buffer;
  2027. int bytes_read = 0;
  2028. unsigned char c = 0;
  2029. diag[0] = 0;
  2030. while (packet_bytes-- > 0) {
  2031. c = *b++;
  2032. if (bytes_read < m) {
  2033. d += sprintf(d, " %02X", c);
  2034. } else if (bytes_read > m) {
  2035. } else
  2036. d += sprintf(d, " ..");
  2037. bytes_read += 1;
  2038. continue;
  2039. }
  2040. if (c == 0x7E) {
  2041. return 0;
  2042. } else {
  2043. if (c == 0x55) {
  2044. goto read;
  2045. } else if (read_stop-- > 0) {
  2046. goto read;
  2047. } else {
  2048. dev_err(&ftdi->udev->dev, "retr"
  2049. "y limit reached\n");
  2050. continue;
  2051. }
  2052. }
  2053. } else if (packet_bytes > 1) {
  2054. unsigned char s1 = ftdi->bulk_in_buffer[0];
  2055. unsigned char s2 = ftdi->bulk_in_buffer[1];
  2056. if (s1 == 0x31 && s2 == 0x00) {
  2057. if (read_stuck-- > 0) {
  2058. goto read;
  2059. } else
  2060. goto reset;
  2061. } else if (s1 == 0x31 && s2 == 0x60) {
  2062. if (read_stop-- > 0) {
  2063. goto read;
  2064. } else {
  2065. dev_err(&ftdi->udev->dev, "retr"
  2066. "y limit reached\n");
  2067. continue;
  2068. }
  2069. } else {
  2070. if (read_stop-- > 0) {
  2071. goto read;
  2072. } else {
  2073. dev_err(&ftdi->udev->dev, "retr"
  2074. "y limit reached\n");
  2075. continue;
  2076. }
  2077. }
  2078. } else if (packet_bytes > 0) {
  2079. if (read_stop-- > 0) {
  2080. goto read;
  2081. } else {
  2082. dev_err(&ftdi->udev->dev, "retry limit "
  2083. "reached\n");
  2084. continue;
  2085. }
  2086. } else if (retval == -ETIMEDOUT) {
  2087. if (retry_on_timeout-- > 0) {
  2088. goto read;
  2089. } else {
  2090. dev_err(&ftdi->udev->dev, "TIMED OUT re"
  2091. "try limit reached\n");
  2092. continue;
  2093. }
  2094. } else if (retval == 0) {
  2095. if (retry_on_empty-- > 0) {
  2096. goto read;
  2097. } else {
  2098. dev_err(&ftdi->udev->dev, "empty packet"
  2099. " retry limit reached\n");
  2100. continue;
  2101. }
  2102. } else {
  2103. err_count += 1;
  2104. dev_err(&ftdi->udev->dev, "error = %d\n",
  2105. retval);
  2106. if (read_stop-- > 0) {
  2107. goto read;
  2108. } else {
  2109. dev_err(&ftdi->udev->dev, "retry limit "
  2110. "reached\n");
  2111. continue;
  2112. }
  2113. }
  2114. }
  2115. }
  2116. dev_err(&ftdi->udev->dev, "failed to synchronize\n");
  2117. return -EFAULT;
  2118. }
  2119. static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
  2120. {
  2121. int retry_on_empty = 10;
  2122. int retry_on_timeout = 5;
  2123. int retry_on_status = 50;
  2124. more:{
  2125. int packet_bytes = 0;
  2126. int retval = usb_bulk_msg(ftdi->udev,
  2127. usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
  2128. ftdi->bulk_in_buffer, ftdi->bulk_in_size,
  2129. &packet_bytes, msecs_to_jiffies(1000));
  2130. if (packet_bytes > 2) {
  2131. char diag[30 *3 + 4];
  2132. char *d = diag;
  2133. int m = (sizeof(diag) - 1) / 3;
  2134. char *b = ftdi->bulk_in_buffer;
  2135. int bytes_read = 0;
  2136. diag[0] = 0;
  2137. while (packet_bytes-- > 0) {
  2138. char c = *b++;
  2139. if (bytes_read < m) {
  2140. d += sprintf(d, " %02X",
  2141. 0x000000FF & c);
  2142. } else if (bytes_read > m) {
  2143. } else
  2144. d += sprintf(d, " ..");
  2145. bytes_read += 1;
  2146. continue;
  2147. }
  2148. goto more;
  2149. } else if (packet_bytes > 1) {
  2150. char s1 = ftdi->bulk_in_buffer[0];
  2151. char s2 = ftdi->bulk_in_buffer[1];
  2152. if (s1 == 0x31 && s2 == 0x60) {
  2153. return 0;
  2154. } else if (retry_on_status-- > 0) {
  2155. msleep(5);
  2156. goto more;
  2157. } else
  2158. return -EFAULT;
  2159. } else if (packet_bytes > 0) {
  2160. char b1 = ftdi->bulk_in_buffer[0];
  2161. dev_err(&ftdi->udev->dev, "only one byte flushed from F"
  2162. "TDI = %02X\n", b1);
  2163. if (retry_on_status-- > 0) {
  2164. msleep(5);
  2165. goto more;
  2166. } else {
  2167. dev_err(&ftdi->udev->dev, "STATUS ERROR retry l"
  2168. "imit reached\n");
  2169. return -EFAULT;
  2170. }
  2171. } else if (retval == -ETIMEDOUT) {
  2172. if (retry_on_timeout-- > 0) {
  2173. goto more;
  2174. } else {
  2175. dev_err(&ftdi->udev->dev, "TIMED OUT retry limi"
  2176. "t reached\n");
  2177. return -ENOMEM;
  2178. }
  2179. } else if (retval == 0) {
  2180. if (retry_on_empty-- > 0) {
  2181. goto more;
  2182. } else {
  2183. dev_err(&ftdi->udev->dev, "empty packet retry l"
  2184. "imit reached\n");
  2185. return -ENOMEM;
  2186. }
  2187. } else {
  2188. dev_err(&ftdi->udev->dev, "error = %d\n", retval);
  2189. return -ENOMEM;
  2190. }
  2191. }
  2192. return -1;
  2193. }
  2194. static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
  2195. {
  2196. int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
  2197. if (UxxxStatus)
  2198. return UxxxStatus;
  2199. if (ftdi->controlreg & 0x00400000) {
  2200. if (ftdi->card_ejected) {
  2201. } else {
  2202. ftdi->card_ejected = 1;
  2203. dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = "
  2204. "%08X\n", ftdi->controlreg);
  2205. }
  2206. return -ENODEV;
  2207. } else {
  2208. u8 fn = ftdi->function - 1;
  2209. int activePCIfn = fn << 8;
  2210. u32 pcidata;
  2211. u32 pciVID;
  2212. u32 pciPID;
  2213. int reg = 0;
  2214. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2215. &pcidata);
  2216. if (UxxxStatus)
  2217. return UxxxStatus;
  2218. pciVID = pcidata & 0xFFFF;
  2219. pciPID = (pcidata >> 16) & 0xFFFF;
  2220. if (pciVID == ftdi->platform_data.vendor && pciPID ==
  2221. ftdi->platform_data.device) {
  2222. return 0;
  2223. } else {
  2224. dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X devi"
  2225. "ce=%04X pciPID=%04X\n",
  2226. ftdi->platform_data.vendor, pciVID,
  2227. ftdi->platform_data.device, pciPID);
  2228. return -ENODEV;
  2229. }
  2230. }
  2231. }
  2232. static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
  2233. {
  2234. u32 latence_timer;
  2235. u32 controlreg;
  2236. int UxxxStatus;
  2237. u32 pcidata;
  2238. int reg = 0;
  2239. int foundOHCI = 0;
  2240. u8 fn;
  2241. int activePCIfn = 0;
  2242. u32 pciVID = 0;
  2243. u32 pciPID = 0;
  2244. UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
  2245. if (UxxxStatus)
  2246. return UxxxStatus;
  2247. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
  2248. if (UxxxStatus)
  2249. return UxxxStatus;
  2250. msleep(750);
  2251. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
  2252. if (UxxxStatus)
  2253. return UxxxStatus;
  2254. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
  2255. if (UxxxStatus)
  2256. return UxxxStatus;
  2257. UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
  2258. if (UxxxStatus)
  2259. return UxxxStatus;
  2260. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
  2261. if (UxxxStatus)
  2262. return UxxxStatus;
  2263. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
  2264. if (UxxxStatus)
  2265. return UxxxStatus;
  2266. msleep(250);
  2267. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
  2268. if (UxxxStatus)
  2269. return UxxxStatus;
  2270. UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
  2271. if (UxxxStatus)
  2272. return UxxxStatus;
  2273. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
  2274. if (UxxxStatus)
  2275. return UxxxStatus;
  2276. UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
  2277. if (UxxxStatus)
  2278. return UxxxStatus;
  2279. UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
  2280. if (UxxxStatus)
  2281. return UxxxStatus;
  2282. msleep(1000);
  2283. for (fn = 0; (fn < 4) && (!foundOHCI); fn++) {
  2284. activePCIfn = fn << 8;
  2285. ftdi->function = fn + 1;
  2286. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2287. &pcidata);
  2288. if (UxxxStatus)
  2289. return UxxxStatus;
  2290. pciVID = pcidata & 0xFFFF;
  2291. pciPID = (pcidata >> 16) & 0xFFFF;
  2292. if ((pciVID == 0x1045) && (pciPID == 0xc861)) {
  2293. foundOHCI = 1;
  2294. } else if ((pciVID == 0x1033) && (pciPID == 0x0035)) {
  2295. foundOHCI = 1;
  2296. } else if ((pciVID == 0x10b9) && (pciPID == 0x5237)) {
  2297. foundOHCI = 1;
  2298. } else if ((pciVID == 0x11c1) && (pciPID == 0x5802)) {
  2299. foundOHCI = 1;
  2300. } else if ((pciVID == 0x11AB) && (pciPID == 0x1FA6)) {
  2301. }
  2302. }
  2303. if (foundOHCI == 0) {
  2304. return -ENXIO;
  2305. }
  2306. ftdi->platform_data.vendor = pciVID;
  2307. ftdi->platform_data.device = pciPID;
  2308. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
  2309. if (UxxxStatus)
  2310. return UxxxStatus;
  2311. reg = 16;
  2312. UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
  2313. 0xFFFFFFFF);
  2314. if (UxxxStatus)
  2315. return UxxxStatus;
  2316. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2317. &pcidata);
  2318. if (UxxxStatus)
  2319. return UxxxStatus;
  2320. UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
  2321. 0xF0000000);
  2322. if (UxxxStatus)
  2323. return UxxxStatus;
  2324. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2325. &pcidata);
  2326. if (UxxxStatus)
  2327. return UxxxStatus;
  2328. reg = 12;
  2329. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2330. &latence_timer);
  2331. if (UxxxStatus)
  2332. return UxxxStatus;
  2333. latence_timer &= 0xFFFF00FF;
  2334. latence_timer |= 0x00001600;
  2335. UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
  2336. latence_timer);
  2337. if (UxxxStatus)
  2338. return UxxxStatus;
  2339. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2340. &pcidata);
  2341. if (UxxxStatus)
  2342. return UxxxStatus;
  2343. reg = 4;
  2344. UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
  2345. 0x06);
  2346. if (UxxxStatus)
  2347. return UxxxStatus;
  2348. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2349. &pcidata);
  2350. if (UxxxStatus)
  2351. return UxxxStatus;
  2352. return 0;
  2353. }
  2354. static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
  2355. {
  2356. u32 pcidata;
  2357. int U132Status;
  2358. int reg;
  2359. int reset_repeat = 0;
  2360. do_reset:reg = 8;
  2361. U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x0e, 0x01);
  2362. if (U132Status)
  2363. return U132Status;
  2364. reset_check:{
  2365. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2366. if (U132Status)
  2367. return U132Status;
  2368. if (pcidata & 1) {
  2369. msleep(500);
  2370. if (reset_repeat++ > 100) {
  2371. reset_repeat = 0;
  2372. goto do_reset;
  2373. } else
  2374. goto reset_check;
  2375. }
  2376. }
  2377. goto dump_regs;
  2378. msleep(500);
  2379. reg = 0x28;
  2380. U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x11000000);
  2381. if (U132Status)
  2382. return U132Status;
  2383. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2384. if (U132Status)
  2385. return U132Status;
  2386. reg = 0x40;
  2387. U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x2edf);
  2388. if (U132Status)
  2389. return U132Status;
  2390. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2391. if (U132Status)
  2392. return U132Status;
  2393. reg = 0x34;
  2394. U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x2edf2edf);
  2395. if (U132Status)
  2396. return U132Status;
  2397. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2398. if (U132Status)
  2399. return U132Status;
  2400. reg = 4;
  2401. U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0xA0);
  2402. if (U132Status)
  2403. return U132Status;
  2404. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2405. if (U132Status)
  2406. return U132Status;
  2407. msleep(250);
  2408. reg = 8;
  2409. U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x0e, 0x04);
  2410. if (U132Status)
  2411. return U132Status;
  2412. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2413. if (U132Status)
  2414. return U132Status;
  2415. reg = 0x28;
  2416. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2417. if (U132Status)
  2418. return U132Status;
  2419. reg = 8;
  2420. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2421. if (U132Status)
  2422. return U132Status;
  2423. reg = 0x48;
  2424. U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x00001200);
  2425. if (U132Status)
  2426. return U132Status;
  2427. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2428. if (U132Status)
  2429. return U132Status;
  2430. reg = 0x54;
  2431. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2432. if (U132Status)
  2433. return U132Status;
  2434. reg = 0x58;
  2435. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2436. if (U132Status)
  2437. return U132Status;
  2438. reg = 0x34;
  2439. U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x28002edf);
  2440. if (U132Status)
  2441. return U132Status;
  2442. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2443. if (U132Status)
  2444. return U132Status;
  2445. msleep(100);
  2446. reg = 0x50;
  2447. U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x10000);
  2448. if (U132Status)
  2449. return U132Status;
  2450. reg = 0x54;
  2451. power_check:U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2452. if (U132Status)
  2453. return U132Status;
  2454. if (!(pcidata & 1)) {
  2455. msleep(500);
  2456. goto power_check;
  2457. }
  2458. msleep(3000);
  2459. reg = 0x54;
  2460. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2461. if (U132Status)
  2462. return U132Status;
  2463. reg = 0x58;
  2464. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2465. if (U132Status)
  2466. return U132Status;
  2467. reg = 0x54;
  2468. U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x02);
  2469. if (U132Status)
  2470. return U132Status;
  2471. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2472. if (U132Status)
  2473. return U132Status;
  2474. reg = 0x54;
  2475. U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x10);
  2476. if (U132Status)
  2477. return U132Status;
  2478. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2479. if (U132Status)
  2480. return U132Status;
  2481. msleep(750);
  2482. reg = 0x54;
  2483. if (0) {
  2484. U132Status = ftdi_elan_write_pcimem(ftdi, reg, 0x00, 0x02);
  2485. if (U132Status)
  2486. return U132Status;
  2487. }
  2488. if (0) {
  2489. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2490. if (U132Status)
  2491. return U132Status;
  2492. }
  2493. reg = 0x54;
  2494. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2495. if (U132Status)
  2496. return U132Status;
  2497. reg = 0x58;
  2498. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2499. if (U132Status)
  2500. return U132Status;
  2501. dump_regs:for (reg = 0; reg <= 0x54; reg += 4) {
  2502. U132Status = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2503. if (U132Status)
  2504. return U132Status;
  2505. }
  2506. return 0;
  2507. }
  2508. /*
  2509. * we use only the first bulk-in and bulk-out endpoints
  2510. */
  2511. static int ftdi_elan_probe(struct usb_interface *interface,
  2512. const struct usb_device_id *id)
  2513. {
  2514. struct usb_host_interface *iface_desc;
  2515. struct usb_endpoint_descriptor *endpoint;
  2516. size_t buffer_size;
  2517. int i;
  2518. int retval = -ENOMEM;
  2519. struct usb_ftdi *ftdi = kmalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
  2520. if (ftdi == NULL) {
  2521. printk(KERN_ERR "Out of memory\n");
  2522. return -ENOMEM;
  2523. }
  2524. memset(ftdi, 0x00, sizeof(struct usb_ftdi));
  2525. down(&ftdi_module_lock);
  2526. list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
  2527. ftdi->sequence_num = ++ftdi_instances;
  2528. up(&ftdi_module_lock);
  2529. ftdi_elan_init_kref(ftdi);
  2530. init_MUTEX(&ftdi->sw_lock);
  2531. ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
  2532. ftdi->interface = interface;
  2533. init_MUTEX(&ftdi->u132_lock);
  2534. ftdi->expected = 4;
  2535. iface_desc = interface->cur_altsetting;
  2536. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  2537. endpoint = &iface_desc->endpoint[i].desc;
  2538. if (!ftdi->bulk_in_endpointAddr &&
  2539. usb_endpoint_is_bulk_in(endpoint)) {
  2540. buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
  2541. ftdi->bulk_in_size = buffer_size;
  2542. ftdi->bulk_in_endpointAddr = endpoint->bEndpointAddress;
  2543. ftdi->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
  2544. if (!ftdi->bulk_in_buffer) {
  2545. dev_err(&ftdi->udev->dev, "Could not allocate b"
  2546. "ulk_in_buffer\n");
  2547. retval = -ENOMEM;
  2548. goto error;
  2549. }
  2550. }
  2551. if (!ftdi->bulk_out_endpointAddr &&
  2552. usb_endpoint_is_bulk_out(endpoint)) {
  2553. ftdi->bulk_out_endpointAddr =
  2554. endpoint->bEndpointAddress;
  2555. }
  2556. }
  2557. if (!(ftdi->bulk_in_endpointAddr && ftdi->bulk_out_endpointAddr)) {
  2558. dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk"
  2559. "-out endpoints\n");
  2560. retval = -ENODEV;
  2561. goto error;
  2562. }
  2563. dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
  2564. iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
  2565. ftdi->bulk_out_endpointAddr);
  2566. usb_set_intfdata(interface, ftdi);
  2567. if (iface_desc->desc.bInterfaceNumber == 0 &&
  2568. ftdi->bulk_in_endpointAddr == 0x81 &&
  2569. ftdi->bulk_out_endpointAddr == 0x02) {
  2570. retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
  2571. if (retval) {
  2572. dev_err(&ftdi->udev->dev, "Not able to get a minor for "
  2573. "this device.\n");
  2574. usb_set_intfdata(interface, NULL);
  2575. retval = -ENOMEM;
  2576. goto error;
  2577. } else {
  2578. ftdi->class = &ftdi_elan_jtag_class;
  2579. dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface "
  2580. "%d now attached to ftdi%d\n", ftdi,
  2581. iface_desc->desc.bInterfaceNumber,
  2582. interface->minor);
  2583. return 0;
  2584. }
  2585. } else if (iface_desc->desc.bInterfaceNumber == 1 &&
  2586. ftdi->bulk_in_endpointAddr == 0x83 &&
  2587. ftdi->bulk_out_endpointAddr == 0x04) {
  2588. ftdi->class = NULL;
  2589. dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now a"
  2590. "ctivated\n", ftdi, iface_desc->desc.bInterfaceNumber);
  2591. INIT_WORK(&ftdi->status_work, ftdi_elan_status_work,
  2592. (void *)ftdi);
  2593. INIT_WORK(&ftdi->command_work, ftdi_elan_command_work,
  2594. (void *)ftdi);
  2595. INIT_WORK(&ftdi->respond_work, ftdi_elan_respond_work,
  2596. (void *)ftdi);
  2597. ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
  2598. return 0;
  2599. } else {
  2600. dev_err(&ftdi->udev->dev,
  2601. "Could not find ELAN's U132 device\n");
  2602. retval = -ENODEV;
  2603. goto error;
  2604. }
  2605. error:if (ftdi) {
  2606. ftdi_elan_put_kref(ftdi);
  2607. }
  2608. return retval;
  2609. }
  2610. static void ftdi_elan_disconnect(struct usb_interface *interface)
  2611. {
  2612. struct usb_ftdi *ftdi = usb_get_intfdata(interface);
  2613. ftdi->disconnected += 1;
  2614. if (ftdi->class) {
  2615. int minor = interface->minor;
  2616. struct usb_class_driver *class = ftdi->class;
  2617. usb_set_intfdata(interface, NULL);
  2618. usb_deregister_dev(interface, class);
  2619. dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on min"
  2620. "or %d now disconnected\n", minor);
  2621. } else {
  2622. ftdi_status_cancel_work(ftdi);
  2623. ftdi_command_cancel_work(ftdi);
  2624. ftdi_response_cancel_work(ftdi);
  2625. ftdi_elan_abandon_completions(ftdi);
  2626. ftdi_elan_abandon_targets(ftdi);
  2627. if (ftdi->registered) {
  2628. platform_device_unregister(&ftdi->platform_dev);
  2629. ftdi->synchronized = 0;
  2630. ftdi->enumerated = 0;
  2631. ftdi->registered = 0;
  2632. }
  2633. flush_workqueue(status_queue);
  2634. flush_workqueue(command_queue);
  2635. flush_workqueue(respond_queue);
  2636. ftdi->disconnected += 1;
  2637. usb_set_intfdata(interface, NULL);
  2638. dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller inter"
  2639. "face now disconnected\n");
  2640. }
  2641. ftdi_elan_put_kref(ftdi);
  2642. }
  2643. static struct usb_driver ftdi_elan_driver = {
  2644. .name = "ftdi-elan",
  2645. .probe = ftdi_elan_probe,
  2646. .disconnect = ftdi_elan_disconnect,
  2647. .id_table = ftdi_elan_table,
  2648. };
  2649. static int __init ftdi_elan_init(void)
  2650. {
  2651. int result;
  2652. printk(KERN_INFO "driver %s built at %s on %s\n", ftdi_elan_driver.name,
  2653. __TIME__, __DATE__);
  2654. init_MUTEX(&ftdi_module_lock);
  2655. INIT_LIST_HEAD(&ftdi_static_list);
  2656. status_queue = create_singlethread_workqueue("ftdi-status-control");
  2657. command_queue = create_singlethread_workqueue("ftdi-command-engine");
  2658. respond_queue = create_singlethread_workqueue("ftdi-respond-engine");
  2659. result = usb_register(&ftdi_elan_driver);
  2660. if (result)
  2661. printk(KERN_ERR "usb_register failed. Error number %d\n",
  2662. result);
  2663. return result;
  2664. }
  2665. static void __exit ftdi_elan_exit(void)
  2666. {
  2667. struct usb_ftdi *ftdi;
  2668. struct usb_ftdi *temp;
  2669. usb_deregister(&ftdi_elan_driver);
  2670. printk(KERN_INFO "ftdi_u132 driver deregistered\n");
  2671. list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
  2672. ftdi_status_cancel_work(ftdi);
  2673. ftdi_command_cancel_work(ftdi);
  2674. ftdi_response_cancel_work(ftdi);
  2675. } flush_workqueue(status_queue);
  2676. destroy_workqueue(status_queue);
  2677. status_queue = NULL;
  2678. flush_workqueue(command_queue);
  2679. destroy_workqueue(command_queue);
  2680. command_queue = NULL;
  2681. flush_workqueue(respond_queue);
  2682. destroy_workqueue(respond_queue);
  2683. respond_queue = NULL;
  2684. }
  2685. module_init(ftdi_elan_init);
  2686. module_exit(ftdi_elan_exit);