ftdi-elan.c 112 KB

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