ftdi-elan.c 118 KB

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