ftdi-elan.c 79 KB

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