ftdi-elan.c 112 KB

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