ftdi-elan.c 118 KB

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