ftdi-elan.c 80 KB

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