ftdi-elan.c 118 KB

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