ftdi-elan.c 78 KB

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