ftdi-elan.c 81 KB

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