ftdi-elan.c 118 KB

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