ftdi-elan.c 80 KB

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