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