ftdi-elan.c 113 KB

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