ftdi-elan.c 118 KB

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