ftdi-elan.c 78 KB

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