ftdi-elan.c 82 KB

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