ftdi-elan.c 118 KB

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