ftdi-elan.c 118 KB

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