ftdi-elan.c 110 KB

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