ftdi-elan.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821
  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. int empty_packets = 0;
  868. read:{
  869. int packet_bytes = 0;
  870. int retval = usb_bulk_msg(ftdi->udev,
  871. usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
  872. ftdi->bulk_in_buffer, ftdi->bulk_in_size,
  873. &packet_bytes, 500);
  874. char diag[30 *3 + 4];
  875. char *d = diag;
  876. int m = packet_bytes;
  877. u8 *c = ftdi->bulk_in_buffer;
  878. int s = (sizeof(diag) - 1) / 3;
  879. diag[0] = 0;
  880. while (s-- > 0 && m-- > 0) {
  881. if (s > 0 || m == 0) {
  882. d += sprintf(d, " %02X", *c++);
  883. } else
  884. d += sprintf(d, " ..");
  885. }
  886. if (packet_bytes > 2) {
  887. ftdi->bulk_in_left = packet_bytes - 2;
  888. ftdi->bulk_in_last = 1;
  889. goto have;
  890. } else if (retval == -ETIMEDOUT) {
  891. if (retry_on_timeout-- > 0) {
  892. dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
  893. packet_bytes, bytes_read, diag);
  894. goto more;
  895. } else if (bytes_read > 0) {
  896. dev_err(&ftdi->udev->dev, "ONLY %d bytes%s\n",
  897. bytes_read, diag);
  898. return -ENOMEM;
  899. } else {
  900. dev_err(&ftdi->udev->dev, "TIMED OUT with packet_bytes = %d with total %d bytes%s\n",
  901. packet_bytes, bytes_read, diag);
  902. return -ENOMEM;
  903. }
  904. } else if (retval == -EILSEQ) {
  905. dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
  906. retval, packet_bytes, bytes_read, diag);
  907. return retval;
  908. } else if (retval) {
  909. dev_err(&ftdi->udev->dev, "error = %d with packet_bytes = %d with total %d bytes%s\n",
  910. retval, packet_bytes, bytes_read, diag);
  911. return retval;
  912. } else if (packet_bytes == 2) {
  913. unsigned char s0 = ftdi->bulk_in_buffer[0];
  914. unsigned char s1 = ftdi->bulk_in_buffer[1];
  915. empty_packets += 1;
  916. if (s0 == 0x31 && s1 == 0x60) {
  917. if (retry_on_empty-- > 0) {
  918. goto more;
  919. } else
  920. return 0;
  921. } else if (s0 == 0x31 && s1 == 0x00) {
  922. if (retry_on_empty-- > 0) {
  923. goto more;
  924. } else
  925. return 0;
  926. } else {
  927. if (retry_on_empty-- > 0) {
  928. goto more;
  929. } else
  930. return 0;
  931. }
  932. } else if (packet_bytes == 1) {
  933. if (retry_on_empty-- > 0) {
  934. goto more;
  935. } else
  936. return 0;
  937. } else {
  938. if (retry_on_empty-- > 0) {
  939. goto more;
  940. } else
  941. return 0;
  942. }
  943. }
  944. more:{
  945. goto read;
  946. }
  947. have:if (ftdi->bulk_in_left > 0) {
  948. u8 c = ftdi->bulk_in_buffer[++ftdi->bulk_in_last];
  949. bytes_read += 1;
  950. ftdi->bulk_in_left -= 1;
  951. if (ftdi->received == 0 && c == 0xFF) {
  952. goto have;
  953. } else
  954. *b++ = c;
  955. if (++ftdi->received < ftdi->expected) {
  956. goto have;
  957. } else if (ftdi->ed_found) {
  958. int ed_number = (ftdi->response[0] >> 5) & 0x03;
  959. u16 ed_length = (ftdi->response[2] << 8) |
  960. ftdi->response[1];
  961. struct u132_target *target = &ftdi->target[ed_number];
  962. int payload = (ed_length >> 0) & 0x07FF;
  963. char diag[30 *3 + 4];
  964. char *d = diag;
  965. int m = payload;
  966. u8 *c = 4 + ftdi->response;
  967. int s = (sizeof(diag) - 1) / 3;
  968. diag[0] = 0;
  969. while (s-- > 0 && m-- > 0) {
  970. if (s > 0 || m == 0) {
  971. d += sprintf(d, " %02X", *c++);
  972. } else
  973. d += sprintf(d, " ..");
  974. }
  975. ftdi_elan_do_callback(ftdi, target, 4 + ftdi->response,
  976. payload);
  977. ftdi->received = 0;
  978. ftdi->expected = 4;
  979. ftdi->ed_found = 0;
  980. b = ftdi->response;
  981. goto have;
  982. } else if (ftdi->expected == 8) {
  983. u8 buscmd;
  984. int respond_head = ftdi->respond_head++;
  985. struct u132_respond *respond = &ftdi->respond[
  986. RESPOND_MASK & respond_head];
  987. u32 data = ftdi->response[7];
  988. data <<= 8;
  989. data |= ftdi->response[6];
  990. data <<= 8;
  991. data |= ftdi->response[5];
  992. data <<= 8;
  993. data |= ftdi->response[4];
  994. *respond->value = data;
  995. *respond->result = 0;
  996. complete(&respond->wait_completion);
  997. ftdi->received = 0;
  998. ftdi->expected = 4;
  999. ftdi->ed_found = 0;
  1000. b = ftdi->response;
  1001. buscmd = (ftdi->response[0] >> 0) & 0x0F;
  1002. if (buscmd == 0x00) {
  1003. } else if (buscmd == 0x02) {
  1004. } else if (buscmd == 0x06) {
  1005. } else if (buscmd == 0x0A) {
  1006. } else
  1007. dev_err(&ftdi->udev->dev, "Uxxx unknown(%0X) value = %08X\n",
  1008. buscmd, data);
  1009. goto have;
  1010. } else {
  1011. if ((ftdi->response[0] & 0x80) == 0x00) {
  1012. ftdi->expected = 8;
  1013. goto have;
  1014. } else {
  1015. int ed_number = (ftdi->response[0] >> 5) & 0x03;
  1016. int ed_type = (ftdi->response[0] >> 0) & 0x03;
  1017. u16 ed_length = (ftdi->response[2] << 8) |
  1018. ftdi->response[1];
  1019. struct u132_target *target = &ftdi->target[
  1020. ed_number];
  1021. target->halted = (ftdi->response[0] >> 3) &
  1022. 0x01;
  1023. target->skipped = (ftdi->response[0] >> 2) &
  1024. 0x01;
  1025. target->toggle_bits = (ftdi->response[3] >> 6)
  1026. & 0x03;
  1027. target->error_count = (ftdi->response[3] >> 4)
  1028. & 0x03;
  1029. target->condition_code = (ftdi->response[
  1030. 3] >> 0) & 0x0F;
  1031. if ((ftdi->response[0] & 0x10) == 0x00) {
  1032. b = have_ed_set_response(ftdi, target,
  1033. ed_length, ed_number, ed_type,
  1034. b);
  1035. goto have;
  1036. } else {
  1037. b = have_ed_get_response(ftdi, target,
  1038. ed_length, ed_number, ed_type,
  1039. b);
  1040. goto have;
  1041. }
  1042. }
  1043. }
  1044. } else
  1045. goto more;
  1046. }
  1047. /*
  1048. * create a urb, and a buffer for it, and copy the data to the urb
  1049. *
  1050. */
  1051. static ssize_t ftdi_elan_write(struct file *file,
  1052. const char __user *user_buffer, size_t count,
  1053. loff_t *ppos)
  1054. {
  1055. int retval = 0;
  1056. struct urb *urb;
  1057. char *buf;
  1058. struct usb_ftdi *ftdi = file->private_data;
  1059. if (ftdi->disconnected > 0) {
  1060. return -ENODEV;
  1061. }
  1062. if (count == 0) {
  1063. goto exit;
  1064. }
  1065. urb = usb_alloc_urb(0, GFP_KERNEL);
  1066. if (!urb) {
  1067. retval = -ENOMEM;
  1068. goto error_1;
  1069. }
  1070. buf = usb_alloc_coherent(ftdi->udev, count, GFP_KERNEL,
  1071. &urb->transfer_dma);
  1072. if (!buf) {
  1073. retval = -ENOMEM;
  1074. goto error_2;
  1075. }
  1076. if (copy_from_user(buf, user_buffer, count)) {
  1077. retval = -EFAULT;
  1078. goto error_3;
  1079. }
  1080. usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
  1081. ftdi->bulk_out_endpointAddr), buf, count,
  1082. ftdi_elan_write_bulk_callback, ftdi);
  1083. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1084. retval = usb_submit_urb(urb, GFP_KERNEL);
  1085. if (retval) {
  1086. dev_err(&ftdi->udev->dev,
  1087. "failed submitting write urb, error %d\n", retval);
  1088. goto error_3;
  1089. }
  1090. usb_free_urb(urb);
  1091. exit:
  1092. return count;
  1093. error_3:
  1094. usb_free_coherent(ftdi->udev, count, buf, urb->transfer_dma);
  1095. error_2:
  1096. usb_free_urb(urb);
  1097. error_1:
  1098. return retval;
  1099. }
  1100. static const struct file_operations ftdi_elan_fops = {
  1101. .owner = THIS_MODULE,
  1102. .llseek = no_llseek,
  1103. .read = ftdi_elan_read,
  1104. .write = ftdi_elan_write,
  1105. .open = ftdi_elan_open,
  1106. .release = ftdi_elan_release,
  1107. };
  1108. /*
  1109. * usb class driver info in order to get a minor number from the usb core,
  1110. * and to have the device registered with the driver core
  1111. */
  1112. static struct usb_class_driver ftdi_elan_jtag_class = {
  1113. .name = "ftdi-%d-jtag",
  1114. .fops = &ftdi_elan_fops,
  1115. .minor_base = USB_FTDI_ELAN_MINOR_BASE,
  1116. };
  1117. /*
  1118. * the following definitions are for the
  1119. * ELAN FPGA state machgine processor that
  1120. * lies on the other side of the FTDI chip
  1121. */
  1122. #define cPCIu132rd 0x0
  1123. #define cPCIu132wr 0x1
  1124. #define cPCIiord 0x2
  1125. #define cPCIiowr 0x3
  1126. #define cPCImemrd 0x6
  1127. #define cPCImemwr 0x7
  1128. #define cPCIcfgrd 0xA
  1129. #define cPCIcfgwr 0xB
  1130. #define cPCInull 0xF
  1131. #define cU132cmd_status 0x0
  1132. #define cU132flash 0x1
  1133. #define cPIDsetup 0x0
  1134. #define cPIDout 0x1
  1135. #define cPIDin 0x2
  1136. #define cPIDinonce 0x3
  1137. #define cCCnoerror 0x0
  1138. #define cCCcrc 0x1
  1139. #define cCCbitstuff 0x2
  1140. #define cCCtoggle 0x3
  1141. #define cCCstall 0x4
  1142. #define cCCnoresp 0x5
  1143. #define cCCbadpid1 0x6
  1144. #define cCCbadpid2 0x7
  1145. #define cCCdataoverrun 0x8
  1146. #define cCCdataunderrun 0x9
  1147. #define cCCbuffoverrun 0xC
  1148. #define cCCbuffunderrun 0xD
  1149. #define cCCnotaccessed 0xF
  1150. static int ftdi_elan_write_reg(struct usb_ftdi *ftdi, u32 data)
  1151. {
  1152. wait:if (ftdi->disconnected > 0) {
  1153. return -ENODEV;
  1154. } else {
  1155. int command_size;
  1156. mutex_lock(&ftdi->u132_lock);
  1157. command_size = ftdi->command_next - ftdi->command_head;
  1158. if (command_size < COMMAND_SIZE) {
  1159. struct u132_command *command = &ftdi->command[
  1160. COMMAND_MASK & ftdi->command_next];
  1161. command->header = 0x00 | cPCIu132wr;
  1162. command->length = 0x04;
  1163. command->address = 0x00;
  1164. command->width = 0x00;
  1165. command->follows = 4;
  1166. command->value = data;
  1167. command->buffer = &command->value;
  1168. ftdi->command_next += 1;
  1169. ftdi_elan_kick_command_queue(ftdi);
  1170. mutex_unlock(&ftdi->u132_lock);
  1171. return 0;
  1172. } else {
  1173. mutex_unlock(&ftdi->u132_lock);
  1174. msleep(100);
  1175. goto wait;
  1176. }
  1177. }
  1178. }
  1179. static int ftdi_elan_write_config(struct usb_ftdi *ftdi, int config_offset,
  1180. u8 width, u32 data)
  1181. {
  1182. u8 addressofs = config_offset / 4;
  1183. wait:if (ftdi->disconnected > 0) {
  1184. return -ENODEV;
  1185. } else {
  1186. int command_size;
  1187. mutex_lock(&ftdi->u132_lock);
  1188. command_size = ftdi->command_next - ftdi->command_head;
  1189. if (command_size < COMMAND_SIZE) {
  1190. struct u132_command *command = &ftdi->command[
  1191. COMMAND_MASK & ftdi->command_next];
  1192. command->header = 0x00 | (cPCIcfgwr & 0x0F);
  1193. command->length = 0x04;
  1194. command->address = addressofs;
  1195. command->width = 0x00 | (width & 0x0F);
  1196. command->follows = 4;
  1197. command->value = data;
  1198. command->buffer = &command->value;
  1199. ftdi->command_next += 1;
  1200. ftdi_elan_kick_command_queue(ftdi);
  1201. mutex_unlock(&ftdi->u132_lock);
  1202. return 0;
  1203. } else {
  1204. mutex_unlock(&ftdi->u132_lock);
  1205. msleep(100);
  1206. goto wait;
  1207. }
  1208. }
  1209. }
  1210. static int ftdi_elan_write_pcimem(struct usb_ftdi *ftdi, int mem_offset,
  1211. u8 width, u32 data)
  1212. {
  1213. u8 addressofs = mem_offset / 4;
  1214. wait:if (ftdi->disconnected > 0) {
  1215. return -ENODEV;
  1216. } else {
  1217. int command_size;
  1218. mutex_lock(&ftdi->u132_lock);
  1219. command_size = ftdi->command_next - ftdi->command_head;
  1220. if (command_size < COMMAND_SIZE) {
  1221. struct u132_command *command = &ftdi->command[
  1222. COMMAND_MASK & ftdi->command_next];
  1223. command->header = 0x00 | (cPCImemwr & 0x0F);
  1224. command->length = 0x04;
  1225. command->address = addressofs;
  1226. command->width = 0x00 | (width & 0x0F);
  1227. command->follows = 4;
  1228. command->value = data;
  1229. command->buffer = &command->value;
  1230. ftdi->command_next += 1;
  1231. ftdi_elan_kick_command_queue(ftdi);
  1232. mutex_unlock(&ftdi->u132_lock);
  1233. return 0;
  1234. } else {
  1235. mutex_unlock(&ftdi->u132_lock);
  1236. msleep(100);
  1237. goto wait;
  1238. }
  1239. }
  1240. }
  1241. int usb_ftdi_elan_write_pcimem(struct platform_device *pdev, int mem_offset,
  1242. u8 width, u32 data)
  1243. {
  1244. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1245. return ftdi_elan_write_pcimem(ftdi, mem_offset, width, data);
  1246. }
  1247. EXPORT_SYMBOL_GPL(usb_ftdi_elan_write_pcimem);
  1248. static int ftdi_elan_read_reg(struct usb_ftdi *ftdi, u32 *data)
  1249. {
  1250. wait:if (ftdi->disconnected > 0) {
  1251. return -ENODEV;
  1252. } else {
  1253. int command_size;
  1254. int respond_size;
  1255. mutex_lock(&ftdi->u132_lock);
  1256. command_size = ftdi->command_next - ftdi->command_head;
  1257. respond_size = ftdi->respond_next - ftdi->respond_head;
  1258. if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
  1259. {
  1260. struct u132_command *command = &ftdi->command[
  1261. COMMAND_MASK & ftdi->command_next];
  1262. struct u132_respond *respond = &ftdi->respond[
  1263. RESPOND_MASK & ftdi->respond_next];
  1264. int result = -ENODEV;
  1265. respond->result = &result;
  1266. respond->header = command->header = 0x00 | cPCIu132rd;
  1267. command->length = 0x04;
  1268. respond->address = command->address = cU132cmd_status;
  1269. command->width = 0x00;
  1270. command->follows = 0;
  1271. command->value = 0;
  1272. command->buffer = NULL;
  1273. respond->value = data;
  1274. init_completion(&respond->wait_completion);
  1275. ftdi->command_next += 1;
  1276. ftdi->respond_next += 1;
  1277. ftdi_elan_kick_command_queue(ftdi);
  1278. mutex_unlock(&ftdi->u132_lock);
  1279. wait_for_completion(&respond->wait_completion);
  1280. return result;
  1281. } else {
  1282. mutex_unlock(&ftdi->u132_lock);
  1283. msleep(100);
  1284. goto wait;
  1285. }
  1286. }
  1287. }
  1288. static int ftdi_elan_read_config(struct usb_ftdi *ftdi, int config_offset,
  1289. u8 width, u32 *data)
  1290. {
  1291. u8 addressofs = config_offset / 4;
  1292. wait:if (ftdi->disconnected > 0) {
  1293. return -ENODEV;
  1294. } else {
  1295. int command_size;
  1296. int respond_size;
  1297. mutex_lock(&ftdi->u132_lock);
  1298. command_size = ftdi->command_next - ftdi->command_head;
  1299. respond_size = ftdi->respond_next - ftdi->respond_head;
  1300. if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
  1301. {
  1302. struct u132_command *command = &ftdi->command[
  1303. COMMAND_MASK & ftdi->command_next];
  1304. struct u132_respond *respond = &ftdi->respond[
  1305. RESPOND_MASK & ftdi->respond_next];
  1306. int result = -ENODEV;
  1307. respond->result = &result;
  1308. respond->header = command->header = 0x00 | (cPCIcfgrd &
  1309. 0x0F);
  1310. command->length = 0x04;
  1311. respond->address = command->address = addressofs;
  1312. command->width = 0x00 | (width & 0x0F);
  1313. command->follows = 0;
  1314. command->value = 0;
  1315. command->buffer = NULL;
  1316. respond->value = data;
  1317. init_completion(&respond->wait_completion);
  1318. ftdi->command_next += 1;
  1319. ftdi->respond_next += 1;
  1320. ftdi_elan_kick_command_queue(ftdi);
  1321. mutex_unlock(&ftdi->u132_lock);
  1322. wait_for_completion(&respond->wait_completion);
  1323. return result;
  1324. } else {
  1325. mutex_unlock(&ftdi->u132_lock);
  1326. msleep(100);
  1327. goto wait;
  1328. }
  1329. }
  1330. }
  1331. static int ftdi_elan_read_pcimem(struct usb_ftdi *ftdi, int mem_offset,
  1332. u8 width, u32 *data)
  1333. {
  1334. u8 addressofs = mem_offset / 4;
  1335. wait:if (ftdi->disconnected > 0) {
  1336. return -ENODEV;
  1337. } else {
  1338. int command_size;
  1339. int respond_size;
  1340. mutex_lock(&ftdi->u132_lock);
  1341. command_size = ftdi->command_next - ftdi->command_head;
  1342. respond_size = ftdi->respond_next - ftdi->respond_head;
  1343. if (command_size < COMMAND_SIZE && respond_size < RESPOND_SIZE)
  1344. {
  1345. struct u132_command *command = &ftdi->command[
  1346. COMMAND_MASK & ftdi->command_next];
  1347. struct u132_respond *respond = &ftdi->respond[
  1348. RESPOND_MASK & ftdi->respond_next];
  1349. int result = -ENODEV;
  1350. respond->result = &result;
  1351. respond->header = command->header = 0x00 | (cPCImemrd &
  1352. 0x0F);
  1353. command->length = 0x04;
  1354. respond->address = command->address = addressofs;
  1355. command->width = 0x00 | (width & 0x0F);
  1356. command->follows = 0;
  1357. command->value = 0;
  1358. command->buffer = NULL;
  1359. respond->value = data;
  1360. init_completion(&respond->wait_completion);
  1361. ftdi->command_next += 1;
  1362. ftdi->respond_next += 1;
  1363. ftdi_elan_kick_command_queue(ftdi);
  1364. mutex_unlock(&ftdi->u132_lock);
  1365. wait_for_completion(&respond->wait_completion);
  1366. return result;
  1367. } else {
  1368. mutex_unlock(&ftdi->u132_lock);
  1369. msleep(100);
  1370. goto wait;
  1371. }
  1372. }
  1373. }
  1374. int usb_ftdi_elan_read_pcimem(struct platform_device *pdev, int mem_offset,
  1375. u8 width, u32 *data)
  1376. {
  1377. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1378. if (ftdi->initialized == 0) {
  1379. return -ENODEV;
  1380. } else
  1381. return ftdi_elan_read_pcimem(ftdi, mem_offset, width, data);
  1382. }
  1383. EXPORT_SYMBOL_GPL(usb_ftdi_elan_read_pcimem);
  1384. static int ftdi_elan_edset_setup(struct usb_ftdi *ftdi, u8 ed_number,
  1385. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1386. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1387. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1388. int halted, int skipped, int actual, int non_null))
  1389. {
  1390. u8 ed = ed_number - 1;
  1391. wait:if (ftdi->disconnected > 0) {
  1392. return -ENODEV;
  1393. } else if (ftdi->initialized == 0) {
  1394. return -ENODEV;
  1395. } else {
  1396. int command_size;
  1397. mutex_lock(&ftdi->u132_lock);
  1398. command_size = ftdi->command_next - ftdi->command_head;
  1399. if (command_size < COMMAND_SIZE) {
  1400. struct u132_target *target = &ftdi->target[ed];
  1401. struct u132_command *command = &ftdi->command[
  1402. COMMAND_MASK & ftdi->command_next];
  1403. command->header = 0x80 | (ed << 5);
  1404. command->length = 0x8007;
  1405. command->address = (toggle_bits << 6) | (ep_number << 2)
  1406. | (address << 0);
  1407. command->width = usb_maxpacket(urb->dev, urb->pipe,
  1408. usb_pipeout(urb->pipe));
  1409. command->follows = 8;
  1410. command->value = 0;
  1411. command->buffer = urb->setup_packet;
  1412. target->callback = callback;
  1413. target->endp = endp;
  1414. target->urb = urb;
  1415. target->active = 1;
  1416. ftdi->command_next += 1;
  1417. ftdi_elan_kick_command_queue(ftdi);
  1418. mutex_unlock(&ftdi->u132_lock);
  1419. return 0;
  1420. } else {
  1421. mutex_unlock(&ftdi->u132_lock);
  1422. msleep(100);
  1423. goto wait;
  1424. }
  1425. }
  1426. }
  1427. int usb_ftdi_elan_edset_setup(struct platform_device *pdev, u8 ed_number,
  1428. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1429. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1430. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1431. int halted, int skipped, int actual, int non_null))
  1432. {
  1433. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1434. return ftdi_elan_edset_setup(ftdi, ed_number, endp, urb, address,
  1435. ep_number, toggle_bits, callback);
  1436. }
  1437. EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_setup);
  1438. static int ftdi_elan_edset_input(struct usb_ftdi *ftdi, u8 ed_number,
  1439. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1440. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1441. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1442. int halted, int skipped, int actual, int non_null))
  1443. {
  1444. u8 ed = ed_number - 1;
  1445. wait:if (ftdi->disconnected > 0) {
  1446. return -ENODEV;
  1447. } else if (ftdi->initialized == 0) {
  1448. return -ENODEV;
  1449. } else {
  1450. int command_size;
  1451. mutex_lock(&ftdi->u132_lock);
  1452. command_size = ftdi->command_next - ftdi->command_head;
  1453. if (command_size < COMMAND_SIZE) {
  1454. struct u132_target *target = &ftdi->target[ed];
  1455. struct u132_command *command = &ftdi->command[
  1456. COMMAND_MASK & ftdi->command_next];
  1457. u32 remaining_length = urb->transfer_buffer_length -
  1458. urb->actual_length;
  1459. command->header = 0x82 | (ed << 5);
  1460. if (remaining_length == 0) {
  1461. command->length = 0x0000;
  1462. } else if (remaining_length > 1024) {
  1463. command->length = 0x8000 | 1023;
  1464. } else
  1465. command->length = 0x8000 | (remaining_length -
  1466. 1);
  1467. command->address = (toggle_bits << 6) | (ep_number << 2)
  1468. | (address << 0);
  1469. command->width = usb_maxpacket(urb->dev, urb->pipe,
  1470. usb_pipeout(urb->pipe));
  1471. command->follows = 0;
  1472. command->value = 0;
  1473. command->buffer = NULL;
  1474. target->callback = callback;
  1475. target->endp = endp;
  1476. target->urb = urb;
  1477. target->active = 1;
  1478. ftdi->command_next += 1;
  1479. ftdi_elan_kick_command_queue(ftdi);
  1480. mutex_unlock(&ftdi->u132_lock);
  1481. return 0;
  1482. } else {
  1483. mutex_unlock(&ftdi->u132_lock);
  1484. msleep(100);
  1485. goto wait;
  1486. }
  1487. }
  1488. }
  1489. int usb_ftdi_elan_edset_input(struct platform_device *pdev, u8 ed_number,
  1490. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1491. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1492. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1493. int halted, int skipped, int actual, int non_null))
  1494. {
  1495. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1496. return ftdi_elan_edset_input(ftdi, ed_number, endp, urb, address,
  1497. ep_number, toggle_bits, callback);
  1498. }
  1499. EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_input);
  1500. static int ftdi_elan_edset_empty(struct usb_ftdi *ftdi, u8 ed_number,
  1501. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1502. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1503. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1504. int halted, int skipped, int actual, int non_null))
  1505. {
  1506. u8 ed = ed_number - 1;
  1507. wait:if (ftdi->disconnected > 0) {
  1508. return -ENODEV;
  1509. } else if (ftdi->initialized == 0) {
  1510. return -ENODEV;
  1511. } else {
  1512. int command_size;
  1513. mutex_lock(&ftdi->u132_lock);
  1514. command_size = ftdi->command_next - ftdi->command_head;
  1515. if (command_size < COMMAND_SIZE) {
  1516. struct u132_target *target = &ftdi->target[ed];
  1517. struct u132_command *command = &ftdi->command[
  1518. COMMAND_MASK & ftdi->command_next];
  1519. command->header = 0x81 | (ed << 5);
  1520. command->length = 0x0000;
  1521. command->address = (toggle_bits << 6) | (ep_number << 2)
  1522. | (address << 0);
  1523. command->width = usb_maxpacket(urb->dev, urb->pipe,
  1524. usb_pipeout(urb->pipe));
  1525. command->follows = 0;
  1526. command->value = 0;
  1527. command->buffer = NULL;
  1528. target->callback = callback;
  1529. target->endp = endp;
  1530. target->urb = urb;
  1531. target->active = 1;
  1532. ftdi->command_next += 1;
  1533. ftdi_elan_kick_command_queue(ftdi);
  1534. mutex_unlock(&ftdi->u132_lock);
  1535. return 0;
  1536. } else {
  1537. mutex_unlock(&ftdi->u132_lock);
  1538. msleep(100);
  1539. goto wait;
  1540. }
  1541. }
  1542. }
  1543. int usb_ftdi_elan_edset_empty(struct platform_device *pdev, u8 ed_number,
  1544. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1545. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1546. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1547. int halted, int skipped, int actual, int non_null))
  1548. {
  1549. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1550. return ftdi_elan_edset_empty(ftdi, ed_number, endp, urb, address,
  1551. ep_number, toggle_bits, callback);
  1552. }
  1553. EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_empty);
  1554. static int ftdi_elan_edset_output(struct usb_ftdi *ftdi, u8 ed_number,
  1555. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1556. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1557. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1558. int halted, int skipped, int actual, int non_null))
  1559. {
  1560. u8 ed = ed_number - 1;
  1561. wait:if (ftdi->disconnected > 0) {
  1562. return -ENODEV;
  1563. } else if (ftdi->initialized == 0) {
  1564. return -ENODEV;
  1565. } else {
  1566. int command_size;
  1567. mutex_lock(&ftdi->u132_lock);
  1568. command_size = ftdi->command_next - ftdi->command_head;
  1569. if (command_size < COMMAND_SIZE) {
  1570. u8 *b;
  1571. u16 urb_size;
  1572. int i = 0;
  1573. char data[30 *3 + 4];
  1574. char *d = data;
  1575. int m = (sizeof(data) - 1) / 3 - 1;
  1576. int l = 0;
  1577. struct u132_target *target = &ftdi->target[ed];
  1578. struct u132_command *command = &ftdi->command[
  1579. COMMAND_MASK & ftdi->command_next];
  1580. command->header = 0x81 | (ed << 5);
  1581. command->address = (toggle_bits << 6) | (ep_number << 2)
  1582. | (address << 0);
  1583. command->width = usb_maxpacket(urb->dev, urb->pipe,
  1584. usb_pipeout(urb->pipe));
  1585. command->follows = min_t(u32, 1024,
  1586. urb->transfer_buffer_length -
  1587. urb->actual_length);
  1588. command->value = 0;
  1589. command->buffer = urb->transfer_buffer +
  1590. urb->actual_length;
  1591. command->length = 0x8000 | (command->follows - 1);
  1592. b = command->buffer;
  1593. urb_size = command->follows;
  1594. data[0] = 0;
  1595. while (urb_size-- > 0) {
  1596. if (i > m) {
  1597. } else if (i++ < m) {
  1598. int w = sprintf(d, " %02X", *b++);
  1599. d += w;
  1600. l += w;
  1601. } else
  1602. d += sprintf(d, " ..");
  1603. }
  1604. target->callback = callback;
  1605. target->endp = endp;
  1606. target->urb = urb;
  1607. target->active = 1;
  1608. ftdi->command_next += 1;
  1609. ftdi_elan_kick_command_queue(ftdi);
  1610. mutex_unlock(&ftdi->u132_lock);
  1611. return 0;
  1612. } else {
  1613. mutex_unlock(&ftdi->u132_lock);
  1614. msleep(100);
  1615. goto wait;
  1616. }
  1617. }
  1618. }
  1619. int usb_ftdi_elan_edset_output(struct platform_device *pdev, u8 ed_number,
  1620. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1621. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1622. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1623. int halted, int skipped, int actual, int non_null))
  1624. {
  1625. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1626. return ftdi_elan_edset_output(ftdi, ed_number, endp, urb, address,
  1627. ep_number, toggle_bits, callback);
  1628. }
  1629. EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_output);
  1630. static int ftdi_elan_edset_single(struct usb_ftdi *ftdi, u8 ed_number,
  1631. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1632. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1633. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1634. int halted, int skipped, int actual, int non_null))
  1635. {
  1636. u8 ed = ed_number - 1;
  1637. wait:if (ftdi->disconnected > 0) {
  1638. return -ENODEV;
  1639. } else if (ftdi->initialized == 0) {
  1640. return -ENODEV;
  1641. } else {
  1642. int command_size;
  1643. mutex_lock(&ftdi->u132_lock);
  1644. command_size = ftdi->command_next - ftdi->command_head;
  1645. if (command_size < COMMAND_SIZE) {
  1646. u32 remaining_length = urb->transfer_buffer_length -
  1647. urb->actual_length;
  1648. struct u132_target *target = &ftdi->target[ed];
  1649. struct u132_command *command = &ftdi->command[
  1650. COMMAND_MASK & ftdi->command_next];
  1651. command->header = 0x83 | (ed << 5);
  1652. if (remaining_length == 0) {
  1653. command->length = 0x0000;
  1654. } else if (remaining_length > 1024) {
  1655. command->length = 0x8000 | 1023;
  1656. } else
  1657. command->length = 0x8000 | (remaining_length -
  1658. 1);
  1659. command->address = (toggle_bits << 6) | (ep_number << 2)
  1660. | (address << 0);
  1661. command->width = usb_maxpacket(urb->dev, urb->pipe,
  1662. usb_pipeout(urb->pipe));
  1663. command->follows = 0;
  1664. command->value = 0;
  1665. command->buffer = NULL;
  1666. target->callback = callback;
  1667. target->endp = endp;
  1668. target->urb = urb;
  1669. target->active = 1;
  1670. ftdi->command_next += 1;
  1671. ftdi_elan_kick_command_queue(ftdi);
  1672. mutex_unlock(&ftdi->u132_lock);
  1673. return 0;
  1674. } else {
  1675. mutex_unlock(&ftdi->u132_lock);
  1676. msleep(100);
  1677. goto wait;
  1678. }
  1679. }
  1680. }
  1681. int usb_ftdi_elan_edset_single(struct platform_device *pdev, u8 ed_number,
  1682. void *endp, struct urb *urb, u8 address, u8 ep_number, u8 toggle_bits,
  1683. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  1684. int toggle_bits, int error_count, int condition_code, int repeat_number,
  1685. int halted, int skipped, int actual, int non_null))
  1686. {
  1687. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1688. return ftdi_elan_edset_single(ftdi, ed_number, endp, urb, address,
  1689. ep_number, toggle_bits, callback);
  1690. }
  1691. EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_single);
  1692. static int ftdi_elan_edset_flush(struct usb_ftdi *ftdi, u8 ed_number,
  1693. void *endp)
  1694. {
  1695. u8 ed = ed_number - 1;
  1696. if (ftdi->disconnected > 0) {
  1697. return -ENODEV;
  1698. } else if (ftdi->initialized == 0) {
  1699. return -ENODEV;
  1700. } else {
  1701. struct u132_target *target = &ftdi->target[ed];
  1702. mutex_lock(&ftdi->u132_lock);
  1703. if (target->abandoning > 0) {
  1704. mutex_unlock(&ftdi->u132_lock);
  1705. return 0;
  1706. } else {
  1707. target->abandoning = 1;
  1708. wait_1:if (target->active == 1) {
  1709. int command_size = ftdi->command_next -
  1710. ftdi->command_head;
  1711. if (command_size < COMMAND_SIZE) {
  1712. struct u132_command *command =
  1713. &ftdi->command[COMMAND_MASK &
  1714. ftdi->command_next];
  1715. command->header = 0x80 | (ed << 5) |
  1716. 0x4;
  1717. command->length = 0x00;
  1718. command->address = 0x00;
  1719. command->width = 0x00;
  1720. command->follows = 0;
  1721. command->value = 0;
  1722. command->buffer = &command->value;
  1723. ftdi->command_next += 1;
  1724. ftdi_elan_kick_command_queue(ftdi);
  1725. } else {
  1726. mutex_unlock(&ftdi->u132_lock);
  1727. msleep(100);
  1728. mutex_lock(&ftdi->u132_lock);
  1729. goto wait_1;
  1730. }
  1731. }
  1732. mutex_unlock(&ftdi->u132_lock);
  1733. return 0;
  1734. }
  1735. }
  1736. }
  1737. int usb_ftdi_elan_edset_flush(struct platform_device *pdev, u8 ed_number,
  1738. void *endp)
  1739. {
  1740. struct usb_ftdi *ftdi = platform_device_to_usb_ftdi(pdev);
  1741. return ftdi_elan_edset_flush(ftdi, ed_number, endp);
  1742. }
  1743. EXPORT_SYMBOL_GPL(usb_ftdi_elan_edset_flush);
  1744. static int ftdi_elan_flush_input_fifo(struct usb_ftdi *ftdi)
  1745. {
  1746. int retry_on_empty = 10;
  1747. int retry_on_timeout = 5;
  1748. int retry_on_status = 20;
  1749. more:{
  1750. int packet_bytes = 0;
  1751. int retval = usb_bulk_msg(ftdi->udev,
  1752. usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
  1753. ftdi->bulk_in_buffer, ftdi->bulk_in_size,
  1754. &packet_bytes, 100);
  1755. if (packet_bytes > 2) {
  1756. char diag[30 *3 + 4];
  1757. char *d = diag;
  1758. int m = (sizeof(diag) - 1) / 3 - 1;
  1759. char *b = ftdi->bulk_in_buffer;
  1760. int bytes_read = 0;
  1761. diag[0] = 0;
  1762. while (packet_bytes-- > 0) {
  1763. char c = *b++;
  1764. if (bytes_read < m) {
  1765. d += sprintf(d, " %02X",
  1766. 0x000000FF & c);
  1767. } else if (bytes_read > m) {
  1768. } else
  1769. d += sprintf(d, " ..");
  1770. bytes_read += 1;
  1771. continue;
  1772. }
  1773. goto more;
  1774. } else if (packet_bytes > 1) {
  1775. char s1 = ftdi->bulk_in_buffer[0];
  1776. char s2 = ftdi->bulk_in_buffer[1];
  1777. if (s1 == 0x31 && s2 == 0x60) {
  1778. return 0;
  1779. } else if (retry_on_status-- > 0) {
  1780. goto more;
  1781. } else {
  1782. dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
  1783. return -EFAULT;
  1784. }
  1785. } else if (packet_bytes > 0) {
  1786. char b1 = ftdi->bulk_in_buffer[0];
  1787. dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n",
  1788. b1);
  1789. if (retry_on_status-- > 0) {
  1790. goto more;
  1791. } else {
  1792. dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
  1793. return -EFAULT;
  1794. }
  1795. } else if (retval == -ETIMEDOUT) {
  1796. if (retry_on_timeout-- > 0) {
  1797. goto more;
  1798. } else {
  1799. dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
  1800. return -ENOMEM;
  1801. }
  1802. } else if (retval == 0) {
  1803. if (retry_on_empty-- > 0) {
  1804. goto more;
  1805. } else {
  1806. dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
  1807. return -ENOMEM;
  1808. }
  1809. } else {
  1810. dev_err(&ftdi->udev->dev, "error = %d\n", retval);
  1811. return retval;
  1812. }
  1813. }
  1814. return -1;
  1815. }
  1816. /*
  1817. * send the long flush sequence
  1818. *
  1819. */
  1820. static int ftdi_elan_synchronize_flush(struct usb_ftdi *ftdi)
  1821. {
  1822. int retval;
  1823. struct urb *urb;
  1824. char *buf;
  1825. int I = 257;
  1826. int i = 0;
  1827. urb = usb_alloc_urb(0, GFP_KERNEL);
  1828. if (!urb)
  1829. return -ENOMEM;
  1830. buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
  1831. if (!buf) {
  1832. dev_err(&ftdi->udev->dev, "could not get a buffer for flush sequence\n");
  1833. usb_free_urb(urb);
  1834. return -ENOMEM;
  1835. }
  1836. while (I-- > 0)
  1837. buf[i++] = 0x55;
  1838. usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
  1839. ftdi->bulk_out_endpointAddr), buf, i,
  1840. ftdi_elan_write_bulk_callback, ftdi);
  1841. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1842. retval = usb_submit_urb(urb, GFP_KERNEL);
  1843. if (retval) {
  1844. dev_err(&ftdi->udev->dev, "failed to submit urb containing the flush sequence\n");
  1845. usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
  1846. usb_free_urb(urb);
  1847. return -ENOMEM;
  1848. }
  1849. usb_free_urb(urb);
  1850. return 0;
  1851. }
  1852. /*
  1853. * send the reset sequence
  1854. *
  1855. */
  1856. static int ftdi_elan_synchronize_reset(struct usb_ftdi *ftdi)
  1857. {
  1858. int retval;
  1859. struct urb *urb;
  1860. char *buf;
  1861. int I = 4;
  1862. int i = 0;
  1863. urb = usb_alloc_urb(0, GFP_KERNEL);
  1864. if (!urb)
  1865. return -ENOMEM;
  1866. buf = usb_alloc_coherent(ftdi->udev, I, GFP_KERNEL, &urb->transfer_dma);
  1867. if (!buf) {
  1868. dev_err(&ftdi->udev->dev, "could not get a buffer for the reset sequence\n");
  1869. usb_free_urb(urb);
  1870. return -ENOMEM;
  1871. }
  1872. buf[i++] = 0x55;
  1873. buf[i++] = 0xAA;
  1874. buf[i++] = 0x5A;
  1875. buf[i++] = 0xA5;
  1876. usb_fill_bulk_urb(urb, ftdi->udev, usb_sndbulkpipe(ftdi->udev,
  1877. ftdi->bulk_out_endpointAddr), buf, i,
  1878. ftdi_elan_write_bulk_callback, ftdi);
  1879. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1880. retval = usb_submit_urb(urb, GFP_KERNEL);
  1881. if (retval) {
  1882. dev_err(&ftdi->udev->dev, "failed to submit urb containing the reset sequence\n");
  1883. usb_free_coherent(ftdi->udev, i, buf, urb->transfer_dma);
  1884. usb_free_urb(urb);
  1885. return -ENOMEM;
  1886. }
  1887. usb_free_urb(urb);
  1888. return 0;
  1889. }
  1890. static int ftdi_elan_synchronize(struct usb_ftdi *ftdi)
  1891. {
  1892. int retval;
  1893. int long_stop = 10;
  1894. int retry_on_timeout = 5;
  1895. int retry_on_empty = 10;
  1896. int err_count = 0;
  1897. retval = ftdi_elan_flush_input_fifo(ftdi);
  1898. if (retval)
  1899. return retval;
  1900. ftdi->bulk_in_left = 0;
  1901. ftdi->bulk_in_last = -1;
  1902. while (long_stop-- > 0) {
  1903. int read_stop;
  1904. int read_stuck;
  1905. retval = ftdi_elan_synchronize_flush(ftdi);
  1906. if (retval)
  1907. return retval;
  1908. retval = ftdi_elan_flush_input_fifo(ftdi);
  1909. if (retval)
  1910. return retval;
  1911. reset:retval = ftdi_elan_synchronize_reset(ftdi);
  1912. if (retval)
  1913. return retval;
  1914. read_stop = 100;
  1915. read_stuck = 10;
  1916. read:{
  1917. int packet_bytes = 0;
  1918. retval = usb_bulk_msg(ftdi->udev,
  1919. usb_rcvbulkpipe(ftdi->udev,
  1920. ftdi->bulk_in_endpointAddr),
  1921. ftdi->bulk_in_buffer, ftdi->bulk_in_size,
  1922. &packet_bytes, 500);
  1923. if (packet_bytes > 2) {
  1924. char diag[30 *3 + 4];
  1925. char *d = diag;
  1926. int m = (sizeof(diag) - 1) / 3 - 1;
  1927. char *b = ftdi->bulk_in_buffer;
  1928. int bytes_read = 0;
  1929. unsigned char c = 0;
  1930. diag[0] = 0;
  1931. while (packet_bytes-- > 0) {
  1932. c = *b++;
  1933. if (bytes_read < m) {
  1934. d += sprintf(d, " %02X", c);
  1935. } else if (bytes_read > m) {
  1936. } else
  1937. d += sprintf(d, " ..");
  1938. bytes_read += 1;
  1939. continue;
  1940. }
  1941. if (c == 0x7E) {
  1942. return 0;
  1943. } else {
  1944. if (c == 0x55) {
  1945. goto read;
  1946. } else if (read_stop-- > 0) {
  1947. goto read;
  1948. } else {
  1949. dev_err(&ftdi->udev->dev, "retry limit reached\n");
  1950. continue;
  1951. }
  1952. }
  1953. } else if (packet_bytes > 1) {
  1954. unsigned char s1 = ftdi->bulk_in_buffer[0];
  1955. unsigned char s2 = ftdi->bulk_in_buffer[1];
  1956. if (s1 == 0x31 && s2 == 0x00) {
  1957. if (read_stuck-- > 0) {
  1958. goto read;
  1959. } else
  1960. goto reset;
  1961. } else if (s1 == 0x31 && s2 == 0x60) {
  1962. if (read_stop-- > 0) {
  1963. goto read;
  1964. } else {
  1965. dev_err(&ftdi->udev->dev, "retry limit reached\n");
  1966. continue;
  1967. }
  1968. } else {
  1969. if (read_stop-- > 0) {
  1970. goto read;
  1971. } else {
  1972. dev_err(&ftdi->udev->dev, "retry limit reached\n");
  1973. continue;
  1974. }
  1975. }
  1976. } else if (packet_bytes > 0) {
  1977. if (read_stop-- > 0) {
  1978. goto read;
  1979. } else {
  1980. dev_err(&ftdi->udev->dev, "retry limit reached\n");
  1981. continue;
  1982. }
  1983. } else if (retval == -ETIMEDOUT) {
  1984. if (retry_on_timeout-- > 0) {
  1985. goto read;
  1986. } else {
  1987. dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
  1988. continue;
  1989. }
  1990. } else if (retval == 0) {
  1991. if (retry_on_empty-- > 0) {
  1992. goto read;
  1993. } else {
  1994. dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
  1995. continue;
  1996. }
  1997. } else {
  1998. err_count += 1;
  1999. dev_err(&ftdi->udev->dev, "error = %d\n",
  2000. retval);
  2001. if (read_stop-- > 0) {
  2002. goto read;
  2003. } else {
  2004. dev_err(&ftdi->udev->dev, "retry limit reached\n");
  2005. continue;
  2006. }
  2007. }
  2008. }
  2009. }
  2010. dev_err(&ftdi->udev->dev, "failed to synchronize\n");
  2011. return -EFAULT;
  2012. }
  2013. static int ftdi_elan_stuck_waiting(struct usb_ftdi *ftdi)
  2014. {
  2015. int retry_on_empty = 10;
  2016. int retry_on_timeout = 5;
  2017. int retry_on_status = 50;
  2018. more:{
  2019. int packet_bytes = 0;
  2020. int retval = usb_bulk_msg(ftdi->udev,
  2021. usb_rcvbulkpipe(ftdi->udev, ftdi->bulk_in_endpointAddr),
  2022. ftdi->bulk_in_buffer, ftdi->bulk_in_size,
  2023. &packet_bytes, 1000);
  2024. if (packet_bytes > 2) {
  2025. char diag[30 *3 + 4];
  2026. char *d = diag;
  2027. int m = (sizeof(diag) - 1) / 3 - 1;
  2028. char *b = ftdi->bulk_in_buffer;
  2029. int bytes_read = 0;
  2030. diag[0] = 0;
  2031. while (packet_bytes-- > 0) {
  2032. char c = *b++;
  2033. if (bytes_read < m) {
  2034. d += sprintf(d, " %02X",
  2035. 0x000000FF & c);
  2036. } else if (bytes_read > m) {
  2037. } else
  2038. d += sprintf(d, " ..");
  2039. bytes_read += 1;
  2040. continue;
  2041. }
  2042. goto more;
  2043. } else if (packet_bytes > 1) {
  2044. char s1 = ftdi->bulk_in_buffer[0];
  2045. char s2 = ftdi->bulk_in_buffer[1];
  2046. if (s1 == 0x31 && s2 == 0x60) {
  2047. return 0;
  2048. } else if (retry_on_status-- > 0) {
  2049. msleep(5);
  2050. goto more;
  2051. } else
  2052. return -EFAULT;
  2053. } else if (packet_bytes > 0) {
  2054. char b1 = ftdi->bulk_in_buffer[0];
  2055. dev_err(&ftdi->udev->dev, "only one byte flushed from FTDI = %02X\n", b1);
  2056. if (retry_on_status-- > 0) {
  2057. msleep(5);
  2058. goto more;
  2059. } else {
  2060. dev_err(&ftdi->udev->dev, "STATUS ERROR retry limit reached\n");
  2061. return -EFAULT;
  2062. }
  2063. } else if (retval == -ETIMEDOUT) {
  2064. if (retry_on_timeout-- > 0) {
  2065. goto more;
  2066. } else {
  2067. dev_err(&ftdi->udev->dev, "TIMED OUT retry limit reached\n");
  2068. return -ENOMEM;
  2069. }
  2070. } else if (retval == 0) {
  2071. if (retry_on_empty-- > 0) {
  2072. goto more;
  2073. } else {
  2074. dev_err(&ftdi->udev->dev, "empty packet retry limit reached\n");
  2075. return -ENOMEM;
  2076. }
  2077. } else {
  2078. dev_err(&ftdi->udev->dev, "error = %d\n", retval);
  2079. return -ENOMEM;
  2080. }
  2081. }
  2082. return -1;
  2083. }
  2084. static int ftdi_elan_checkingPCI(struct usb_ftdi *ftdi)
  2085. {
  2086. int UxxxStatus = ftdi_elan_read_reg(ftdi, &ftdi->controlreg);
  2087. if (UxxxStatus)
  2088. return UxxxStatus;
  2089. if (ftdi->controlreg & 0x00400000) {
  2090. if (ftdi->card_ejected) {
  2091. } else {
  2092. ftdi->card_ejected = 1;
  2093. dev_err(&ftdi->udev->dev, "CARD EJECTED - controlreg = %08X\n",
  2094. ftdi->controlreg);
  2095. }
  2096. return -ENODEV;
  2097. } else {
  2098. u8 fn = ftdi->function - 1;
  2099. int activePCIfn = fn << 8;
  2100. u32 pcidata;
  2101. u32 pciVID;
  2102. u32 pciPID;
  2103. int reg = 0;
  2104. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2105. &pcidata);
  2106. if (UxxxStatus)
  2107. return UxxxStatus;
  2108. pciVID = pcidata & 0xFFFF;
  2109. pciPID = (pcidata >> 16) & 0xFFFF;
  2110. if (pciVID == ftdi->platform_data.vendor && pciPID ==
  2111. ftdi->platform_data.device) {
  2112. return 0;
  2113. } else {
  2114. dev_err(&ftdi->udev->dev, "vendor=%04X pciVID=%04X device=%04X pciPID=%04X\n",
  2115. ftdi->platform_data.vendor, pciVID,
  2116. ftdi->platform_data.device, pciPID);
  2117. return -ENODEV;
  2118. }
  2119. }
  2120. }
  2121. #define ftdi_read_pcimem(ftdi, member, data) ftdi_elan_read_pcimem(ftdi, \
  2122. offsetof(struct ohci_regs, member), 0, data);
  2123. #define ftdi_write_pcimem(ftdi, member, data) ftdi_elan_write_pcimem(ftdi, \
  2124. offsetof(struct ohci_regs, member), 0, data);
  2125. #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
  2126. #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
  2127. OHCI_INTR_WDH)
  2128. static int ftdi_elan_check_controller(struct usb_ftdi *ftdi, int quirk)
  2129. {
  2130. int devices = 0;
  2131. int retval;
  2132. u32 hc_control;
  2133. int num_ports;
  2134. u32 control;
  2135. u32 rh_a = -1;
  2136. u32 status;
  2137. u32 fminterval;
  2138. u32 hc_fminterval;
  2139. u32 periodicstart;
  2140. u32 cmdstatus;
  2141. u32 roothub_a;
  2142. int mask = OHCI_INTR_INIT;
  2143. int sleep_time = 0;
  2144. int reset_timeout = 30; /* ... allow extra time */
  2145. int temp;
  2146. retval = ftdi_write_pcimem(ftdi, intrdisable, OHCI_INTR_MIE);
  2147. if (retval)
  2148. return retval;
  2149. retval = ftdi_read_pcimem(ftdi, control, &control);
  2150. if (retval)
  2151. return retval;
  2152. retval = ftdi_read_pcimem(ftdi, roothub.a, &rh_a);
  2153. if (retval)
  2154. return retval;
  2155. num_ports = rh_a & RH_A_NDP;
  2156. retval = ftdi_read_pcimem(ftdi, fminterval, &hc_fminterval);
  2157. if (retval)
  2158. return retval;
  2159. hc_fminterval &= 0x3fff;
  2160. if (hc_fminterval != FI) {
  2161. }
  2162. hc_fminterval |= FSMP(hc_fminterval) << 16;
  2163. retval = ftdi_read_pcimem(ftdi, control, &hc_control);
  2164. if (retval)
  2165. return retval;
  2166. switch (hc_control & OHCI_CTRL_HCFS) {
  2167. case OHCI_USB_OPER:
  2168. sleep_time = 0;
  2169. break;
  2170. case OHCI_USB_SUSPEND:
  2171. case OHCI_USB_RESUME:
  2172. hc_control &= OHCI_CTRL_RWC;
  2173. hc_control |= OHCI_USB_RESUME;
  2174. sleep_time = 10;
  2175. break;
  2176. default:
  2177. hc_control &= OHCI_CTRL_RWC;
  2178. hc_control |= OHCI_USB_RESET;
  2179. sleep_time = 50;
  2180. break;
  2181. }
  2182. retval = ftdi_write_pcimem(ftdi, control, hc_control);
  2183. if (retval)
  2184. return retval;
  2185. retval = ftdi_read_pcimem(ftdi, control, &control);
  2186. if (retval)
  2187. return retval;
  2188. msleep(sleep_time);
  2189. retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
  2190. if (retval)
  2191. return retval;
  2192. if (!(roothub_a & RH_A_NPS)) { /* power down each port */
  2193. for (temp = 0; temp < num_ports; temp++) {
  2194. retval = ftdi_write_pcimem(ftdi,
  2195. roothub.portstatus[temp], RH_PS_LSDA);
  2196. if (retval)
  2197. return retval;
  2198. }
  2199. }
  2200. retval = ftdi_read_pcimem(ftdi, control, &control);
  2201. if (retval)
  2202. return retval;
  2203. retry:retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
  2204. if (retval)
  2205. return retval;
  2206. retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_HCR);
  2207. if (retval)
  2208. return retval;
  2209. extra:{
  2210. retval = ftdi_read_pcimem(ftdi, cmdstatus, &status);
  2211. if (retval)
  2212. return retval;
  2213. if (0 != (status & OHCI_HCR)) {
  2214. if (--reset_timeout == 0) {
  2215. dev_err(&ftdi->udev->dev, "USB HC reset timed out!\n");
  2216. return -ENODEV;
  2217. } else {
  2218. msleep(5);
  2219. goto extra;
  2220. }
  2221. }
  2222. }
  2223. if (quirk & OHCI_QUIRK_INITRESET) {
  2224. retval = ftdi_write_pcimem(ftdi, control, hc_control);
  2225. if (retval)
  2226. return retval;
  2227. retval = ftdi_read_pcimem(ftdi, control, &control);
  2228. if (retval)
  2229. return retval;
  2230. }
  2231. retval = ftdi_write_pcimem(ftdi, ed_controlhead, 0x00000000);
  2232. if (retval)
  2233. return retval;
  2234. retval = ftdi_write_pcimem(ftdi, ed_bulkhead, 0x11000000);
  2235. if (retval)
  2236. return retval;
  2237. retval = ftdi_write_pcimem(ftdi, hcca, 0x00000000);
  2238. if (retval)
  2239. return retval;
  2240. retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
  2241. if (retval)
  2242. return retval;
  2243. retval = ftdi_write_pcimem(ftdi, fminterval,
  2244. ((fminterval & FIT) ^ FIT) | hc_fminterval);
  2245. if (retval)
  2246. return retval;
  2247. retval = ftdi_write_pcimem(ftdi, periodicstart,
  2248. ((9 *hc_fminterval) / 10) & 0x3fff);
  2249. if (retval)
  2250. return retval;
  2251. retval = ftdi_read_pcimem(ftdi, fminterval, &fminterval);
  2252. if (retval)
  2253. return retval;
  2254. retval = ftdi_read_pcimem(ftdi, periodicstart, &periodicstart);
  2255. if (retval)
  2256. return retval;
  2257. if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
  2258. if (!(quirk & OHCI_QUIRK_INITRESET)) {
  2259. quirk |= OHCI_QUIRK_INITRESET;
  2260. goto retry;
  2261. } else
  2262. dev_err(&ftdi->udev->dev, "init err(%08x %04x)\n",
  2263. fminterval, periodicstart);
  2264. } /* start controller operations */
  2265. hc_control &= OHCI_CTRL_RWC;
  2266. hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
  2267. retval = ftdi_write_pcimem(ftdi, control, hc_control);
  2268. if (retval)
  2269. return retval;
  2270. retval = ftdi_write_pcimem(ftdi, cmdstatus, OHCI_BLF);
  2271. if (retval)
  2272. return retval;
  2273. retval = ftdi_read_pcimem(ftdi, cmdstatus, &cmdstatus);
  2274. if (retval)
  2275. return retval;
  2276. retval = ftdi_read_pcimem(ftdi, control, &control);
  2277. if (retval)
  2278. return retval;
  2279. retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_DRWE);
  2280. if (retval)
  2281. return retval;
  2282. retval = ftdi_write_pcimem(ftdi, intrstatus, mask);
  2283. if (retval)
  2284. return retval;
  2285. retval = ftdi_write_pcimem(ftdi, intrdisable,
  2286. OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
  2287. OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
  2288. OHCI_INTR_SO);
  2289. if (retval)
  2290. return retval; /* handle root hub init quirks ... */
  2291. retval = ftdi_read_pcimem(ftdi, roothub.a, &roothub_a);
  2292. if (retval)
  2293. return retval;
  2294. roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
  2295. if (quirk & OHCI_QUIRK_SUPERIO) {
  2296. roothub_a |= RH_A_NOCP;
  2297. roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
  2298. retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
  2299. if (retval)
  2300. return retval;
  2301. } else if ((quirk & OHCI_QUIRK_AMD756) || distrust_firmware) {
  2302. roothub_a |= RH_A_NPS;
  2303. retval = ftdi_write_pcimem(ftdi, roothub.a, roothub_a);
  2304. if (retval)
  2305. return retval;
  2306. }
  2307. retval = ftdi_write_pcimem(ftdi, roothub.status, RH_HS_LPSC);
  2308. if (retval)
  2309. return retval;
  2310. retval = ftdi_write_pcimem(ftdi, roothub.b,
  2311. (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
  2312. if (retval)
  2313. return retval;
  2314. retval = ftdi_read_pcimem(ftdi, control, &control);
  2315. if (retval)
  2316. return retval;
  2317. mdelay((roothub_a >> 23) & 0x1fe);
  2318. for (temp = 0; temp < num_ports; temp++) {
  2319. u32 portstatus;
  2320. retval = ftdi_read_pcimem(ftdi, roothub.portstatus[temp],
  2321. &portstatus);
  2322. if (retval)
  2323. return retval;
  2324. if (1 & portstatus)
  2325. devices += 1;
  2326. }
  2327. return devices;
  2328. }
  2329. static int ftdi_elan_setup_controller(struct usb_ftdi *ftdi, int fn)
  2330. {
  2331. u32 latence_timer;
  2332. int UxxxStatus;
  2333. u32 pcidata;
  2334. int reg = 0;
  2335. int activePCIfn = fn << 8;
  2336. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
  2337. if (UxxxStatus)
  2338. return UxxxStatus;
  2339. reg = 16;
  2340. UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
  2341. 0xFFFFFFFF);
  2342. if (UxxxStatus)
  2343. return UxxxStatus;
  2344. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2345. &pcidata);
  2346. if (UxxxStatus)
  2347. return UxxxStatus;
  2348. UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
  2349. 0xF0000000);
  2350. if (UxxxStatus)
  2351. return UxxxStatus;
  2352. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2353. &pcidata);
  2354. if (UxxxStatus)
  2355. return UxxxStatus;
  2356. reg = 12;
  2357. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2358. &latence_timer);
  2359. if (UxxxStatus)
  2360. return UxxxStatus;
  2361. latence_timer &= 0xFFFF00FF;
  2362. latence_timer |= 0x00001600;
  2363. UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
  2364. latence_timer);
  2365. if (UxxxStatus)
  2366. return UxxxStatus;
  2367. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2368. &pcidata);
  2369. if (UxxxStatus)
  2370. return UxxxStatus;
  2371. reg = 4;
  2372. UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
  2373. 0x06);
  2374. if (UxxxStatus)
  2375. return UxxxStatus;
  2376. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2377. &pcidata);
  2378. if (UxxxStatus)
  2379. return UxxxStatus;
  2380. for (reg = 0; reg <= 0x54; reg += 4) {
  2381. UxxxStatus = ftdi_elan_read_pcimem(ftdi, reg, 0, &pcidata);
  2382. if (UxxxStatus)
  2383. return UxxxStatus;
  2384. }
  2385. return 0;
  2386. }
  2387. static int ftdi_elan_close_controller(struct usb_ftdi *ftdi, int fn)
  2388. {
  2389. u32 latence_timer;
  2390. int UxxxStatus;
  2391. u32 pcidata;
  2392. int reg = 0;
  2393. int activePCIfn = fn << 8;
  2394. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x2800);
  2395. if (UxxxStatus)
  2396. return UxxxStatus;
  2397. reg = 16;
  2398. UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
  2399. 0xFFFFFFFF);
  2400. if (UxxxStatus)
  2401. return UxxxStatus;
  2402. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2403. &pcidata);
  2404. if (UxxxStatus)
  2405. return UxxxStatus;
  2406. UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0,
  2407. 0x00000000);
  2408. if (UxxxStatus)
  2409. return UxxxStatus;
  2410. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2411. &pcidata);
  2412. if (UxxxStatus)
  2413. return UxxxStatus;
  2414. reg = 12;
  2415. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2416. &latence_timer);
  2417. if (UxxxStatus)
  2418. return UxxxStatus;
  2419. latence_timer &= 0xFFFF00FF;
  2420. latence_timer |= 0x00001600;
  2421. UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
  2422. latence_timer);
  2423. if (UxxxStatus)
  2424. return UxxxStatus;
  2425. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2426. &pcidata);
  2427. if (UxxxStatus)
  2428. return UxxxStatus;
  2429. reg = 4;
  2430. UxxxStatus = ftdi_elan_write_config(ftdi, activePCIfn | reg, 0x00,
  2431. 0x00);
  2432. if (UxxxStatus)
  2433. return UxxxStatus;
  2434. return ftdi_elan_read_config(ftdi, activePCIfn | reg, 0, &pcidata);
  2435. }
  2436. static int ftdi_elan_found_controller(struct usb_ftdi *ftdi, int fn, int quirk)
  2437. {
  2438. int result;
  2439. int UxxxStatus;
  2440. UxxxStatus = ftdi_elan_setup_controller(ftdi, fn);
  2441. if (UxxxStatus)
  2442. return UxxxStatus;
  2443. result = ftdi_elan_check_controller(ftdi, quirk);
  2444. UxxxStatus = ftdi_elan_close_controller(ftdi, fn);
  2445. if (UxxxStatus)
  2446. return UxxxStatus;
  2447. return result;
  2448. }
  2449. static int ftdi_elan_enumeratePCI(struct usb_ftdi *ftdi)
  2450. {
  2451. u32 controlreg;
  2452. u8 sensebits;
  2453. int UxxxStatus;
  2454. UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
  2455. if (UxxxStatus)
  2456. return UxxxStatus;
  2457. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000000L);
  2458. if (UxxxStatus)
  2459. return UxxxStatus;
  2460. msleep(750);
  2461. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x100);
  2462. if (UxxxStatus)
  2463. return UxxxStatus;
  2464. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x00000200L | 0x500);
  2465. if (UxxxStatus)
  2466. return UxxxStatus;
  2467. UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
  2468. if (UxxxStatus)
  2469. return UxxxStatus;
  2470. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020CL | 0x000);
  2471. if (UxxxStatus)
  2472. return UxxxStatus;
  2473. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020DL | 0x000);
  2474. if (UxxxStatus)
  2475. return UxxxStatus;
  2476. msleep(250);
  2477. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000020FL | 0x000);
  2478. if (UxxxStatus)
  2479. return UxxxStatus;
  2480. UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
  2481. if (UxxxStatus)
  2482. return UxxxStatus;
  2483. UxxxStatus = ftdi_elan_write_reg(ftdi, 0x0000025FL | 0x800);
  2484. if (UxxxStatus)
  2485. return UxxxStatus;
  2486. UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
  2487. if (UxxxStatus)
  2488. return UxxxStatus;
  2489. UxxxStatus = ftdi_elan_read_reg(ftdi, &controlreg);
  2490. if (UxxxStatus)
  2491. return UxxxStatus;
  2492. msleep(1000);
  2493. sensebits = (controlreg >> 16) & 0x000F;
  2494. if (0x0D == sensebits)
  2495. return 0;
  2496. else
  2497. return - ENXIO;
  2498. }
  2499. static int ftdi_elan_setupOHCI(struct usb_ftdi *ftdi)
  2500. {
  2501. int UxxxStatus;
  2502. u32 pcidata;
  2503. int reg = 0;
  2504. u8 fn;
  2505. int activePCIfn = 0;
  2506. int max_devices = 0;
  2507. int controllers = 0;
  2508. int unrecognized = 0;
  2509. ftdi->function = 0;
  2510. for (fn = 0; (fn < 4); fn++) {
  2511. u32 pciVID = 0;
  2512. u32 pciPID = 0;
  2513. int devices = 0;
  2514. activePCIfn = fn << 8;
  2515. UxxxStatus = ftdi_elan_read_config(ftdi, activePCIfn | reg, 0,
  2516. &pcidata);
  2517. if (UxxxStatus)
  2518. return UxxxStatus;
  2519. pciVID = pcidata & 0xFFFF;
  2520. pciPID = (pcidata >> 16) & 0xFFFF;
  2521. if ((pciVID == PCI_VENDOR_ID_OPTI) && (pciPID == 0xc861)) {
  2522. devices = ftdi_elan_found_controller(ftdi, fn, 0);
  2523. controllers += 1;
  2524. } else if ((pciVID == PCI_VENDOR_ID_NEC) && (pciPID == 0x0035))
  2525. {
  2526. devices = ftdi_elan_found_controller(ftdi, fn, 0);
  2527. controllers += 1;
  2528. } else if ((pciVID == PCI_VENDOR_ID_AL) && (pciPID == 0x5237)) {
  2529. devices = ftdi_elan_found_controller(ftdi, fn, 0);
  2530. controllers += 1;
  2531. } else if ((pciVID == PCI_VENDOR_ID_ATT) && (pciPID == 0x5802))
  2532. {
  2533. devices = ftdi_elan_found_controller(ftdi, fn, 0);
  2534. controllers += 1;
  2535. } else if (pciVID == PCI_VENDOR_ID_AMD && pciPID == 0x740c) {
  2536. devices = ftdi_elan_found_controller(ftdi, fn,
  2537. OHCI_QUIRK_AMD756);
  2538. controllers += 1;
  2539. } else if (pciVID == PCI_VENDOR_ID_COMPAQ && pciPID == 0xa0f8) {
  2540. devices = ftdi_elan_found_controller(ftdi, fn,
  2541. OHCI_QUIRK_ZFMICRO);
  2542. controllers += 1;
  2543. } else if (0 == pcidata) {
  2544. } else
  2545. unrecognized += 1;
  2546. if (devices > max_devices) {
  2547. max_devices = devices;
  2548. ftdi->function = fn + 1;
  2549. ftdi->platform_data.vendor = pciVID;
  2550. ftdi->platform_data.device = pciPID;
  2551. }
  2552. }
  2553. if (ftdi->function > 0) {
  2554. return ftdi_elan_setup_controller(ftdi, ftdi->function - 1);
  2555. } else if (controllers > 0) {
  2556. return -ENXIO;
  2557. } else if (unrecognized > 0) {
  2558. return -ENXIO;
  2559. } else {
  2560. ftdi->enumerated = 0;
  2561. return -ENXIO;
  2562. }
  2563. }
  2564. /*
  2565. * we use only the first bulk-in and bulk-out endpoints
  2566. */
  2567. static int ftdi_elan_probe(struct usb_interface *interface,
  2568. const struct usb_device_id *id)
  2569. {
  2570. struct usb_host_interface *iface_desc;
  2571. struct usb_endpoint_descriptor *bulk_in, *bulk_out;
  2572. int retval;
  2573. struct usb_ftdi *ftdi;
  2574. ftdi = kzalloc(sizeof(struct usb_ftdi), GFP_KERNEL);
  2575. if (!ftdi)
  2576. return -ENOMEM;
  2577. mutex_lock(&ftdi_module_lock);
  2578. list_add_tail(&ftdi->ftdi_list, &ftdi_static_list);
  2579. ftdi->sequence_num = ++ftdi_instances;
  2580. mutex_unlock(&ftdi_module_lock);
  2581. ftdi_elan_init_kref(ftdi);
  2582. sema_init(&ftdi->sw_lock, 1);
  2583. ftdi->udev = usb_get_dev(interface_to_usbdev(interface));
  2584. ftdi->interface = interface;
  2585. mutex_init(&ftdi->u132_lock);
  2586. ftdi->expected = 4;
  2587. iface_desc = interface->cur_altsetting;
  2588. retval = usb_find_common_endpoints(iface_desc,
  2589. &bulk_in, &bulk_out, NULL, NULL);
  2590. if (retval) {
  2591. dev_err(&ftdi->udev->dev, "Could not find both bulk-in and bulk-out endpoints\n");
  2592. goto error;
  2593. }
  2594. ftdi->bulk_in_size = usb_endpoint_maxp(bulk_in);
  2595. ftdi->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
  2596. ftdi->bulk_in_buffer = kmalloc(ftdi->bulk_in_size, GFP_KERNEL);
  2597. if (!ftdi->bulk_in_buffer) {
  2598. retval = -ENOMEM;
  2599. goto error;
  2600. }
  2601. ftdi->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
  2602. dev_info(&ftdi->udev->dev, "interface %d has I=%02X O=%02X\n",
  2603. iface_desc->desc.bInterfaceNumber, ftdi->bulk_in_endpointAddr,
  2604. ftdi->bulk_out_endpointAddr);
  2605. usb_set_intfdata(interface, ftdi);
  2606. if (iface_desc->desc.bInterfaceNumber == 0 &&
  2607. ftdi->bulk_in_endpointAddr == 0x81 &&
  2608. ftdi->bulk_out_endpointAddr == 0x02) {
  2609. retval = usb_register_dev(interface, &ftdi_elan_jtag_class);
  2610. if (retval) {
  2611. dev_err(&ftdi->udev->dev, "Not able to get a minor for this device\n");
  2612. usb_set_intfdata(interface, NULL);
  2613. retval = -ENOMEM;
  2614. goto error;
  2615. } else {
  2616. ftdi->class = &ftdi_elan_jtag_class;
  2617. dev_info(&ftdi->udev->dev, "USB FDTI=%p JTAG interface %d now attached to ftdi%d\n",
  2618. ftdi, iface_desc->desc.bInterfaceNumber,
  2619. interface->minor);
  2620. return 0;
  2621. }
  2622. } else if (iface_desc->desc.bInterfaceNumber == 1 &&
  2623. ftdi->bulk_in_endpointAddr == 0x83 &&
  2624. ftdi->bulk_out_endpointAddr == 0x04) {
  2625. ftdi->class = NULL;
  2626. dev_info(&ftdi->udev->dev, "USB FDTI=%p ELAN interface %d now activated\n",
  2627. ftdi, iface_desc->desc.bInterfaceNumber);
  2628. INIT_DELAYED_WORK(&ftdi->status_work, ftdi_elan_status_work);
  2629. INIT_DELAYED_WORK(&ftdi->command_work, ftdi_elan_command_work);
  2630. INIT_DELAYED_WORK(&ftdi->respond_work, ftdi_elan_respond_work);
  2631. ftdi_status_queue_work(ftdi, msecs_to_jiffies(3 *1000));
  2632. return 0;
  2633. } else {
  2634. dev_err(&ftdi->udev->dev,
  2635. "Could not find ELAN's U132 device\n");
  2636. retval = -ENODEV;
  2637. goto error;
  2638. }
  2639. error:if (ftdi) {
  2640. ftdi_elan_put_kref(ftdi);
  2641. }
  2642. return retval;
  2643. }
  2644. static void ftdi_elan_disconnect(struct usb_interface *interface)
  2645. {
  2646. struct usb_ftdi *ftdi = usb_get_intfdata(interface);
  2647. ftdi->disconnected += 1;
  2648. if (ftdi->class) {
  2649. int minor = interface->minor;
  2650. struct usb_class_driver *class = ftdi->class;
  2651. usb_set_intfdata(interface, NULL);
  2652. usb_deregister_dev(interface, class);
  2653. dev_info(&ftdi->udev->dev, "USB FTDI U132 jtag interface on minor %d now disconnected\n",
  2654. minor);
  2655. } else {
  2656. ftdi_status_cancel_work(ftdi);
  2657. ftdi_command_cancel_work(ftdi);
  2658. ftdi_response_cancel_work(ftdi);
  2659. ftdi_elan_abandon_completions(ftdi);
  2660. ftdi_elan_abandon_targets(ftdi);
  2661. if (ftdi->registered) {
  2662. platform_device_unregister(&ftdi->platform_dev);
  2663. ftdi->synchronized = 0;
  2664. ftdi->enumerated = 0;
  2665. ftdi->initialized = 0;
  2666. ftdi->registered = 0;
  2667. }
  2668. ftdi->disconnected += 1;
  2669. usb_set_intfdata(interface, NULL);
  2670. dev_info(&ftdi->udev->dev, "USB FTDI U132 host controller interface now disconnected\n");
  2671. }
  2672. ftdi_elan_put_kref(ftdi);
  2673. }
  2674. static struct usb_driver ftdi_elan_driver = {
  2675. .name = "ftdi-elan",
  2676. .probe = ftdi_elan_probe,
  2677. .disconnect = ftdi_elan_disconnect,
  2678. .id_table = ftdi_elan_table,
  2679. };
  2680. static int __init ftdi_elan_init(void)
  2681. {
  2682. int result;
  2683. pr_info("driver %s\n", ftdi_elan_driver.name);
  2684. mutex_init(&ftdi_module_lock);
  2685. INIT_LIST_HEAD(&ftdi_static_list);
  2686. result = usb_register(&ftdi_elan_driver);
  2687. if (result) {
  2688. pr_err("usb_register failed. Error number %d\n", result);
  2689. }
  2690. return result;
  2691. }
  2692. static void __exit ftdi_elan_exit(void)
  2693. {
  2694. struct usb_ftdi *ftdi;
  2695. struct usb_ftdi *temp;
  2696. usb_deregister(&ftdi_elan_driver);
  2697. pr_info("ftdi_u132 driver deregistered\n");
  2698. list_for_each_entry_safe(ftdi, temp, &ftdi_static_list, ftdi_list) {
  2699. ftdi_status_cancel_work(ftdi);
  2700. ftdi_command_cancel_work(ftdi);
  2701. ftdi_response_cancel_work(ftdi);
  2702. }
  2703. }
  2704. module_init(ftdi_elan_init);
  2705. module_exit(ftdi_elan_exit);