io_edgeport.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105
  1. /*
  2. * Edgeport USB Serial Converter driver
  3. *
  4. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  5. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Supports the following devices:
  13. * Edgeport/4
  14. * Edgeport/4t
  15. * Edgeport/2
  16. * Edgeport/4i
  17. * Edgeport/2i
  18. * Edgeport/421
  19. * Edgeport/21
  20. * Rapidport/4
  21. * Edgeport/8
  22. * Edgeport/2D8
  23. * Edgeport/4D8
  24. * Edgeport/8i
  25. *
  26. * For questions or problems with this driver, contact Inside Out
  27. * Networks technical support, or Peter Berger <pberger@brimson.com>,
  28. * or Al Borchers <alborchers@steinerpoint.com>.
  29. *
  30. * Version history:
  31. *
  32. * 2003_04_03 al borchers
  33. * - fixed a bug (that shows up with dosemu) where the tty struct is
  34. * used in a callback after it has been freed
  35. *
  36. * 2.3 2002_03_08 greg kroah-hartman
  37. * - fixed bug when multiple devices were attached at the same time.
  38. *
  39. * 2.2 2001_11_14 greg kroah-hartman
  40. * - fixed bug in edge_close that kept the port from being used more
  41. * than once.
  42. * - fixed memory leak on device removal.
  43. * - fixed potential double free of memory when command urb submitting
  44. * failed.
  45. * - other small cleanups when the device is removed
  46. *
  47. * 2.1 2001_07_09 greg kroah-hartman
  48. * - added support for TIOCMBIS and TIOCMBIC.
  49. *
  50. * (04/08/2001) gb
  51. * - Identify version on module load.
  52. *
  53. * 2.0 2001_03_05 greg kroah-hartman
  54. * - reworked entire driver to fit properly in with the other usb-serial
  55. * drivers. Occasional oopses still happen, but it's a good start.
  56. *
  57. * 1.2.3 (02/23/2001) greg kroah-hartman
  58. * - changed device table to work properly for 2.4.x final format.
  59. * - fixed problem with dropping data at high data rates.
  60. *
  61. * 1.2.2 (11/27/2000) greg kroah-hartman
  62. * - cleaned up more NTisms.
  63. * - Added device table for 2.4.0-test11
  64. *
  65. * 1.2.1 (11/08/2000) greg kroah-hartman
  66. * - Started to clean up NTisms.
  67. * - Fixed problem with dev field of urb for kernels >= 2.4.0-test9
  68. *
  69. * 1.2 (10/17/2000) David Iacovelli
  70. * Remove all EPIC code and GPL source
  71. * Fix RELEVANT_IFLAG macro to include flow control
  72. * changes port configuration changes.
  73. * Fix redefinition of SERIAL_MAGIC
  74. * Change all timeout values to 5 seconds
  75. * Tried to fix the UHCI multiple urb submission, but failed miserably.
  76. * it seems to work fine with OHCI.
  77. * ( Greg take a look at the #if 0 at end of WriteCmdUsb() we must
  78. * find a way to work arount this UHCI bug )
  79. *
  80. * 1.1 (10/11/2000) David Iacovelli
  81. * Fix XON/XOFF flow control to support both IXON and IXOFF
  82. *
  83. * 0.9.27 (06/30/2000) David Iacovelli
  84. * Added transmit queue and now allocate urb for command writes.
  85. *
  86. * 0.9.26 (06/29/2000) David Iacovelli
  87. * Add support for 80251 based edgeport
  88. *
  89. * 0.9.25 (06/27/2000) David Iacovelli
  90. * Do not close the port if it has multiple opens.
  91. *
  92. * 0.9.24 (05/26/2000) David Iacovelli
  93. * Add IOCTLs to support RXTX and JAVA POS
  94. * and first cut at running BlackBox Demo
  95. *
  96. * 0.9.23 (05/24/2000) David Iacovelli
  97. * Add IOCTLs to support RXTX and JAVA POS
  98. *
  99. * 0.9.22 (05/23/2000) David Iacovelli
  100. * fixed bug in enumeration. If epconfig turns on mapping by
  101. * path after a device is already plugged in, we now update
  102. * the mapping correctly
  103. *
  104. * 0.9.21 (05/16/2000) David Iacovelli
  105. * Added BlockUntilChaseResp() to also wait for txcredits
  106. * Updated the way we allocate and handle write URBs
  107. * Add debug code to dump buffers
  108. *
  109. * 0.9.20 (05/01/2000) David Iacovelli
  110. * change driver to use usb/tts/
  111. *
  112. * 0.9.19 (05/01/2000) David Iacovelli
  113. * Update code to compile if DEBUG is off
  114. *
  115. * 0.9.18 (04/28/2000) David Iacovelli
  116. * cleanup and test tty_register with devfs
  117. *
  118. * 0.9.17 (04/27/2000) greg kroah-hartman
  119. * changed tty_register around to be like the way it
  120. * was before, but now it works properly with devfs.
  121. *
  122. * 0.9.16 (04/26/2000) david iacovelli
  123. * Fixed bug in GetProductInfo()
  124. *
  125. * 0.9.15 (04/25/2000) david iacovelli
  126. * Updated enumeration
  127. *
  128. * 0.9.14 (04/24/2000) david iacovelli
  129. * Removed all config/status IOCTLS and
  130. * converted to using /proc/edgeport
  131. * still playing with devfs
  132. *
  133. * 0.9.13 (04/24/2000) david iacovelli
  134. * Removed configuration based on ttyUSB0
  135. * Added support for configuration using /prod/edgeport
  136. * first attempt at using devfs (not working yet!)
  137. * Added IOCTL to GetProductInfo()
  138. * Added support for custom baud rates
  139. * Add support for random port numbers
  140. *
  141. * 0.9.12 (04/18/2000) david iacovelli
  142. * added additional configuration IOCTLs
  143. * use ttyUSB0 for configuration
  144. *
  145. * 0.9.11 (04/17/2000) greg kroah-hartman
  146. * fixed module initialization race conditions.
  147. * made all urbs dynamically allocated.
  148. * made driver devfs compatible. now it only registers the tty device
  149. * when the device is actually plugged in.
  150. *
  151. * 0.9.10 (04/13/2000) greg kroah-hartman
  152. * added proc interface framework.
  153. *
  154. * 0.9.9 (04/13/2000) david iacovelli
  155. * added enumeration code and ioctls to configure the device
  156. *
  157. * 0.9.8 (04/12/2000) david iacovelli
  158. * Change interrupt read start when device is plugged in
  159. * and stop when device is removed
  160. * process interrupt reads when all ports are closed
  161. * (keep value of rxBytesAvail consistent with the edgeport)
  162. * set the USB_BULK_QUEUE flag so that we can shove a bunch
  163. * of urbs at once down the pipe
  164. *
  165. * 0.9.7 (04/10/2000) david iacovelli
  166. * start to add enumeration code.
  167. * generate serial number for epic devices
  168. * add support for kdb
  169. *
  170. * 0.9.6 (03/30/2000) david iacovelli
  171. * add IOCTL to get string, manufacture, and boot descriptors
  172. *
  173. * 0.9.5 (03/14/2000) greg kroah-hartman
  174. * more error checking added to SerialOpen to try to fix UHCI open problem
  175. *
  176. * 0.9.4 (03/09/2000) greg kroah-hartman
  177. * added more error checking to handle oops when data is hanging
  178. * around and tty is abruptly closed.
  179. *
  180. * 0.9.3 (03/09/2000) david iacovelli
  181. * Add epic support for xon/xoff chars
  182. * play with performance
  183. *
  184. * 0.9.2 (03/08/2000) greg kroah-hartman
  185. * changed most "info" calls to "dbg"
  186. * implemented flow control properly in the termios call
  187. *
  188. * 0.9.1 (03/08/2000) david iacovelli
  189. * added EPIC support
  190. * enabled bootloader update
  191. *
  192. * 0.9 (03/08/2000) greg kroah-hartman
  193. * Release to IO networks.
  194. * Integrated changes that David made
  195. * made getting urbs for writing SMP safe
  196. *
  197. * 0.8 (03/07/2000) greg kroah-hartman
  198. * Release to IO networks.
  199. * Fixed problems that were seen in code by David.
  200. * Now both Edgeport/4 and Edgeport/2 works properly.
  201. * Changed most of the functions to use port instead of serial.
  202. *
  203. * 0.7 (02/27/2000) greg kroah-hartman
  204. * Milestone 3 release.
  205. * Release to IO Networks
  206. * ioctl for waiting on line change implemented.
  207. * ioctl for getting statistics implemented.
  208. * multiport support working.
  209. * lsr and msr registers are now handled properly.
  210. * change break now hooked up and working.
  211. * support for all known Edgeport devices.
  212. *
  213. * 0.6 (02/22/2000) greg kroah-hartman
  214. * Release to IO networks.
  215. * CHASE is implemented correctly when port is closed.
  216. * SerialOpen now blocks correctly until port is fully opened.
  217. *
  218. * 0.5 (02/20/2000) greg kroah-hartman
  219. * Release to IO networks.
  220. * Known problems:
  221. * modem status register changes are not sent on to the user
  222. * CHASE is not implemented when the port is closed.
  223. *
  224. * 0.4 (02/16/2000) greg kroah-hartman
  225. * Second cut at the CeBit demo.
  226. * Doesn't leak memory on every write to the port
  227. * Still small leaks on startup.
  228. * Added support for Edgeport/2 and Edgeport/8
  229. *
  230. * 0.3 (02/15/2000) greg kroah-hartman
  231. * CeBit demo release.
  232. * Force the line settings to 4800, 8, 1, e for the demo.
  233. * Warning! This version leaks memory like crazy!
  234. *
  235. * 0.2 (01/30/2000) greg kroah-hartman
  236. * Milestone 1 release.
  237. * Device is found by USB subsystem, enumerated, fimware is downloaded
  238. * and the descriptors are printed to the debug log, config is set, and
  239. * green light starts to blink. Open port works, and data can be sent
  240. * and received at the default settings of the UART. Loopback connector
  241. * and debug log confirms this.
  242. *
  243. * 0.1 (01/23/2000) greg kroah-hartman
  244. * Initial release to help IO Networks try to set up their test system.
  245. * Edgeport4 is recognized, firmware is downloaded, config is set so
  246. * device blinks green light every 3 sec. Port is bound, but opening,
  247. * closing, and sending data do not work properly.
  248. *
  249. */
  250. #include <linux/config.h>
  251. #include <linux/kernel.h>
  252. #include <linux/jiffies.h>
  253. #include <linux/errno.h>
  254. #include <linux/init.h>
  255. #include <linux/slab.h>
  256. #include <linux/tty.h>
  257. #include <linux/tty_driver.h>
  258. #include <linux/tty_flip.h>
  259. #include <linux/module.h>
  260. #include <linux/spinlock.h>
  261. #include <linux/serial.h>
  262. #include <linux/ioctl.h>
  263. #include <linux/wait.h>
  264. #include <asm/uaccess.h>
  265. #include <linux/usb.h>
  266. #include "usb-serial.h"
  267. #include "io_edgeport.h"
  268. #include "io_ionsp.h" /* info for the iosp messages */
  269. #include "io_16654.h" /* 16654 UART defines */
  270. /*
  271. * Version Information
  272. */
  273. #define DRIVER_VERSION "v2.7"
  274. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
  275. #define DRIVER_DESC "Edgeport USB Serial Driver"
  276. /* First, the latest boot code - for first generation edgeports */
  277. #define IMAGE_ARRAY_NAME BootCodeImage_GEN1
  278. #define IMAGE_VERSION_NAME BootCodeImageVersion_GEN1
  279. #include "io_fw_boot.h" /* the bootloader firmware to download to a device, if it needs it */
  280. /* for second generation edgeports */
  281. #define IMAGE_ARRAY_NAME BootCodeImage_GEN2
  282. #define IMAGE_VERSION_NAME BootCodeImageVersion_GEN2
  283. #include "io_fw_boot2.h" /* the bootloader firmware to download to a device, if it needs it */
  284. /* Then finally the main run-time operational code - for first generation edgeports */
  285. #define IMAGE_ARRAY_NAME OperationalCodeImage_GEN1
  286. #define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN1
  287. #include "io_fw_down.h" /* Define array OperationalCodeImage[] */
  288. /* for second generation edgeports */
  289. #define IMAGE_ARRAY_NAME OperationalCodeImage_GEN2
  290. #define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN2
  291. #include "io_fw_down2.h" /* Define array OperationalCodeImage[] */
  292. #define MAX_NAME_LEN 64
  293. #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
  294. #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
  295. #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
  296. /* receive port state */
  297. enum RXSTATE {
  298. EXPECT_HDR1 = 0, /* Expect header byte 1 */
  299. EXPECT_HDR2 = 1, /* Expect header byte 2 */
  300. EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
  301. EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
  302. };
  303. /* Transmit Fifo
  304. * This Transmit queue is an extension of the edgeport Rx buffer.
  305. * The maximum amount of data buffered in both the edgeport
  306. * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
  307. */
  308. struct TxFifo {
  309. unsigned int head; /* index to head pointer (write) */
  310. unsigned int tail; /* index to tail pointer (read) */
  311. unsigned int count; /* Bytes in queue */
  312. unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
  313. unsigned char *fifo; /* allocated Buffer */
  314. };
  315. /* This structure holds all of the local port information */
  316. struct edgeport_port {
  317. __u16 txCredits; /* our current credits for this port */
  318. __u16 maxTxCredits; /* the max size of the port */
  319. struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
  320. struct urb *write_urb; /* write URB for this port */
  321. char write_in_progress; /* TRUE while a write URB is outstanding */
  322. spinlock_t ep_lock;
  323. __u8 shadowLCR; /* last LCR value received */
  324. __u8 shadowMCR; /* last MCR value received */
  325. __u8 shadowMSR; /* last MSR value received */
  326. __u8 shadowLSR; /* last LSR value received */
  327. __u8 shadowXonChar; /* last value set as XON char in Edgeport */
  328. __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
  329. __u8 validDataMask;
  330. __u32 baudRate;
  331. char open;
  332. char openPending;
  333. char commandPending;
  334. char closePending;
  335. char chaseResponsePending;
  336. wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
  337. wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
  338. wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
  339. wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
  340. struct async_icount icount;
  341. struct usb_serial_port *port; /* loop back to the owner of this object */
  342. };
  343. /* This structure holds all of the individual device information */
  344. struct edgeport_serial {
  345. char name[MAX_NAME_LEN+1]; /* string name of this device */
  346. struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
  347. struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
  348. struct edgeport_product_info product_info; /* Product Info */
  349. __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
  350. unsigned char * interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
  351. struct urb * interrupt_read_urb; /* our interrupt urb */
  352. __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
  353. unsigned char * bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
  354. struct urb * read_urb; /* our bulk read urb */
  355. int read_in_progress;
  356. spinlock_t es_lock;
  357. __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
  358. __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
  359. enum RXSTATE rxState; /* the current state of the bulk receive processor */
  360. __u8 rxHeader1; /* receive header byte 1 */
  361. __u8 rxHeader2; /* receive header byte 2 */
  362. __u8 rxHeader3; /* receive header byte 3 */
  363. __u8 rxPort; /* the port that we are currently receiving data for */
  364. __u8 rxStatusCode; /* the receive status code */
  365. __u8 rxStatusParam; /* the receive status paramater */
  366. __s16 rxBytesRemaining; /* the number of port bytes left to read */
  367. struct usb_serial *serial; /* loop back to the owner of this object */
  368. };
  369. /* baud rate information */
  370. struct divisor_table_entry {
  371. __u32 BaudRate;
  372. __u16 Divisor;
  373. };
  374. //
  375. // Define table of divisors for Rev A EdgePort/4 hardware
  376. // These assume a 3.6864MHz crystal, the standard /16, and
  377. // MCR.7 = 0.
  378. //
  379. static struct divisor_table_entry divisor_table[] = {
  380. { 50, 4608},
  381. { 75, 3072},
  382. { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
  383. { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
  384. { 150, 1536},
  385. { 300, 768},
  386. { 600, 384},
  387. { 1200, 192},
  388. { 1800, 128},
  389. { 2400, 96},
  390. { 4800, 48},
  391. { 7200, 32},
  392. { 9600, 24},
  393. { 14400, 16},
  394. { 19200, 12},
  395. { 38400, 6},
  396. { 57600, 4},
  397. { 115200, 2},
  398. { 230400, 1},
  399. };
  400. /* local variables */
  401. static int debug;
  402. static int low_latency = 1; /* tty low latency flag, on by default */
  403. static int CmdUrbs = 0; /* Number of outstanding Command Write Urbs */
  404. /* local function prototypes */
  405. /* function prototypes for all URB callbacks */
  406. static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs);
  407. static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs);
  408. static void edge_bulk_out_data_callback (struct urb *urb, struct pt_regs *regs);
  409. static void edge_bulk_out_cmd_callback (struct urb *urb, struct pt_regs *regs);
  410. /* function prototypes for the usbserial callbacks */
  411. static int edge_open (struct usb_serial_port *port, struct file *filp);
  412. static void edge_close (struct usb_serial_port *port, struct file *filp);
  413. static int edge_write (struct usb_serial_port *port, const unsigned char *buf, int count);
  414. static int edge_write_room (struct usb_serial_port *port);
  415. static int edge_chars_in_buffer (struct usb_serial_port *port);
  416. static void edge_throttle (struct usb_serial_port *port);
  417. static void edge_unthrottle (struct usb_serial_port *port);
  418. static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios);
  419. static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
  420. static void edge_break (struct usb_serial_port *port, int break_state);
  421. static int edge_tiocmget (struct usb_serial_port *port, struct file *file);
  422. static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
  423. static int edge_startup (struct usb_serial *serial);
  424. static void edge_shutdown (struct usb_serial *serial);
  425. #include "io_tables.h" /* all of the devices that this driver supports */
  426. static struct usb_driver io_driver = {
  427. .owner = THIS_MODULE,
  428. .name = "io_edgeport",
  429. .probe = usb_serial_probe,
  430. .disconnect = usb_serial_disconnect,
  431. .id_table = id_table_combined,
  432. };
  433. /* function prototypes for all of our local functions */
  434. static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char *buffer, __u16 bufferLength);
  435. static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3);
  436. static void edge_tty_recv (struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
  437. static void handle_new_msr (struct edgeport_port *edge_port, __u8 newMsr);
  438. static void handle_new_lsr (struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data);
  439. static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param);
  440. static int calc_baud_rate_divisor (int baud_rate, int *divisor);
  441. static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate);
  442. static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios);
  443. static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue);
  444. static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int writeLength);
  445. static void send_more_port_data (struct edgeport_serial *edge_serial, struct edgeport_port *edge_port);
  446. static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
  447. static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
  448. static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
  449. static void get_manufacturing_desc (struct edgeport_serial *edge_serial);
  450. static void get_boot_desc (struct edgeport_serial *edge_serial);
  451. static void load_application_firmware (struct edgeport_serial *edge_serial);
  452. static void unicode_to_ascii (char *string, __le16 *unicode, int unicode_size);
  453. // ************************************************************************
  454. // ************************************************************************
  455. // ************************************************************************
  456. // ************************************************************************
  457. /************************************************************************
  458. * *
  459. * update_edgeport_E2PROM() Compare current versions of *
  460. * Boot ROM and Manufacture *
  461. * Descriptors with versions *
  462. * embedded in this driver *
  463. * *
  464. ************************************************************************/
  465. static void update_edgeport_E2PROM (struct edgeport_serial *edge_serial)
  466. {
  467. __u32 BootCurVer;
  468. __u32 BootNewVer;
  469. __u8 BootMajorVersion;
  470. __u8 BootMinorVersion;
  471. __le16 BootBuildNumber;
  472. __u8 *BootImage;
  473. __u32 BootSize;
  474. struct edge_firmware_image_record *record;
  475. unsigned char *firmware;
  476. int response;
  477. switch (edge_serial->product_info.iDownloadFile) {
  478. case EDGE_DOWNLOAD_FILE_I930:
  479. BootMajorVersion = BootCodeImageVersion_GEN1.MajorVersion;
  480. BootMinorVersion = BootCodeImageVersion_GEN1.MinorVersion;
  481. BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN1.BuildNumber);
  482. BootImage = &BootCodeImage_GEN1[0];
  483. BootSize = sizeof( BootCodeImage_GEN1 );
  484. break;
  485. case EDGE_DOWNLOAD_FILE_80251:
  486. BootMajorVersion = BootCodeImageVersion_GEN2.MajorVersion;
  487. BootMinorVersion = BootCodeImageVersion_GEN2.MinorVersion;
  488. BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN2.BuildNumber);
  489. BootImage = &BootCodeImage_GEN2[0];
  490. BootSize = sizeof( BootCodeImage_GEN2 );
  491. break;
  492. default:
  493. return;
  494. }
  495. // Check Boot Image Version
  496. BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
  497. (edge_serial->boot_descriptor.MinorVersion << 16) +
  498. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
  499. BootNewVer = (BootMajorVersion << 24) +
  500. (BootMinorVersion << 16) +
  501. le16_to_cpu(BootBuildNumber);
  502. dbg("Current Boot Image version %d.%d.%d",
  503. edge_serial->boot_descriptor.MajorVersion,
  504. edge_serial->boot_descriptor.MinorVersion,
  505. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
  506. if (BootNewVer > BootCurVer) {
  507. dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
  508. edge_serial->boot_descriptor.MajorVersion,
  509. edge_serial->boot_descriptor.MinorVersion,
  510. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
  511. BootMajorVersion,
  512. BootMinorVersion,
  513. le16_to_cpu(BootBuildNumber));
  514. dbg("Downloading new Boot Image");
  515. firmware = BootImage;
  516. for (;;) {
  517. record = (struct edge_firmware_image_record *)firmware;
  518. response = rom_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
  519. if (response < 0) {
  520. dev_err(&edge_serial->serial->dev->dev, "rom_write failed (%x, %x, %d)\n", le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len));
  521. break;
  522. }
  523. firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
  524. if (firmware >= &BootImage[BootSize]) {
  525. break;
  526. }
  527. }
  528. } else {
  529. dbg("Boot Image -- already up to date");
  530. }
  531. }
  532. /************************************************************************
  533. * *
  534. * Get string descriptor from device *
  535. * *
  536. ************************************************************************/
  537. static int get_string (struct usb_device *dev, int Id, char *string)
  538. {
  539. struct usb_string_descriptor StringDesc;
  540. struct usb_string_descriptor *pStringDesc;
  541. dbg("%s - USB String ID = %d", __FUNCTION__, Id );
  542. if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
  543. return 0;
  544. }
  545. pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
  546. if (!pStringDesc) {
  547. return 0;
  548. }
  549. if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
  550. kfree(pStringDesc);
  551. return 0;
  552. }
  553. unicode_to_ascii(string, pStringDesc->wData, pStringDesc->bLength/2-1);
  554. kfree(pStringDesc);
  555. return strlen(string);
  556. }
  557. #if 0
  558. /************************************************************************
  559. *
  560. * Get string descriptor from device
  561. *
  562. ************************************************************************/
  563. static int get_string_desc (struct usb_device *dev, int Id, struct usb_string_descriptor **pRetDesc)
  564. {
  565. struct usb_string_descriptor StringDesc;
  566. struct usb_string_descriptor *pStringDesc;
  567. dbg("%s - USB String ID = %d", __FUNCTION__, Id );
  568. if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
  569. return 0;
  570. }
  571. pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
  572. if (!pStringDesc) {
  573. return -1;
  574. }
  575. if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
  576. kfree(pStringDesc);
  577. return -1;
  578. }
  579. *pRetDesc = pStringDesc;
  580. return 0;
  581. }
  582. #endif
  583. static void get_product_info(struct edgeport_serial *edge_serial)
  584. {
  585. struct edgeport_product_info *product_info = &edge_serial->product_info;
  586. memset (product_info, 0, sizeof(struct edgeport_product_info));
  587. product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
  588. product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
  589. product_info->ProdInfoVer = 0;
  590. product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
  591. product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
  592. product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
  593. product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
  594. product_info->BootMajorVersion = edge_serial->boot_descriptor.MajorVersion;
  595. product_info->BootMinorVersion = edge_serial->boot_descriptor.MinorVersion;
  596. product_info->BootBuildNumber = edge_serial->boot_descriptor.BuildNumber;
  597. memcpy(product_info->ManufactureDescDate, edge_serial->manuf_descriptor.DescDate, sizeof(edge_serial->manuf_descriptor.DescDate));
  598. // check if this is 2nd generation hardware
  599. if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ION_DEVICE_ID_80251_NETCHIP) {
  600. product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN2.MajorVersion;
  601. product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN2.MinorVersion;
  602. product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN2.BuildNumber);
  603. product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
  604. } else {
  605. product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN1.MajorVersion;
  606. product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN1.MinorVersion;
  607. product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN1.BuildNumber);
  608. product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
  609. }
  610. // Determine Product type and set appropriate flags
  611. switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
  612. case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
  613. case ION_DEVICE_ID_EDGEPORT_4T:
  614. case ION_DEVICE_ID_EDGEPORT_4:
  615. case ION_DEVICE_ID_EDGEPORT_2:
  616. case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
  617. case ION_DEVICE_ID_EDGEPORT_8:
  618. case ION_DEVICE_ID_EDGEPORT_421:
  619. case ION_DEVICE_ID_EDGEPORT_21:
  620. case ION_DEVICE_ID_EDGEPORT_2_DIN:
  621. case ION_DEVICE_ID_EDGEPORT_4_DIN:
  622. case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
  623. product_info->IsRS232 = 1;
  624. break;
  625. case ION_DEVICE_ID_EDGEPORT_2I: // Edgeport/2 RS422/RS485
  626. product_info->IsRS422 = 1;
  627. product_info->IsRS485 = 1;
  628. break;
  629. case ION_DEVICE_ID_EDGEPORT_8I: // Edgeport/4 RS422
  630. case ION_DEVICE_ID_EDGEPORT_4I: // Edgeport/4 RS422
  631. product_info->IsRS422 = 1;
  632. break;
  633. }
  634. // Dump Product Info structure
  635. dbg("**Product Information:");
  636. dbg(" ProductId %x", product_info->ProductId );
  637. dbg(" NumPorts %d", product_info->NumPorts );
  638. dbg(" ProdInfoVer %d", product_info->ProdInfoVer );
  639. dbg(" IsServer %d", product_info->IsServer);
  640. dbg(" IsRS232 %d", product_info->IsRS232 );
  641. dbg(" IsRS422 %d", product_info->IsRS422 );
  642. dbg(" IsRS485 %d", product_info->IsRS485 );
  643. dbg(" RomSize %d", product_info->RomSize );
  644. dbg(" RamSize %d", product_info->RamSize );
  645. dbg(" CpuRev %x", product_info->CpuRev );
  646. dbg(" BoardRev %x", product_info->BoardRev);
  647. dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
  648. product_info->BootMinorVersion,
  649. le16_to_cpu(product_info->BootBuildNumber));
  650. dbg(" FirmwareMajorVersion %d.%d.%d", product_info->FirmwareMajorVersion,
  651. product_info->FirmwareMinorVersion,
  652. le16_to_cpu(product_info->FirmwareBuildNumber));
  653. dbg(" ManufactureDescDate %d/%d/%d", product_info->ManufactureDescDate[0],
  654. product_info->ManufactureDescDate[1],
  655. product_info->ManufactureDescDate[2]+1900);
  656. dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
  657. }
  658. /************************************************************************/
  659. /************************************************************************/
  660. /* U S B C A L L B A C K F U N C T I O N S */
  661. /* U S B C A L L B A C K F U N C T I O N S */
  662. /************************************************************************/
  663. /************************************************************************/
  664. /*****************************************************************************
  665. * edge_interrupt_callback
  666. * this is the callback function for when we have received data on the
  667. * interrupt endpoint.
  668. *****************************************************************************/
  669. static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs)
  670. {
  671. struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
  672. struct edgeport_port *edge_port;
  673. struct usb_serial_port *port;
  674. unsigned char *data = urb->transfer_buffer;
  675. int length = urb->actual_length;
  676. int bytes_avail;
  677. int position;
  678. int txCredits;
  679. int portNumber;
  680. int result;
  681. dbg("%s", __FUNCTION__);
  682. switch (urb->status) {
  683. case 0:
  684. /* success */
  685. break;
  686. case -ECONNRESET:
  687. case -ENOENT:
  688. case -ESHUTDOWN:
  689. /* this urb is terminated, clean up */
  690. dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
  691. return;
  692. default:
  693. dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
  694. goto exit;
  695. }
  696. // process this interrupt-read even if there are no ports open
  697. if (length) {
  698. usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, length, data);
  699. if (length > 1) {
  700. bytes_avail = data[0] | (data[1] << 8);
  701. if (bytes_avail) {
  702. spin_lock(&edge_serial->es_lock);
  703. edge_serial->rxBytesAvail += bytes_avail;
  704. dbg("%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d", __FUNCTION__, bytes_avail, edge_serial->rxBytesAvail, edge_serial->read_in_progress);
  705. if (edge_serial->rxBytesAvail > 0 &&
  706. !edge_serial->read_in_progress) {
  707. dbg("%s - posting a read", __FUNCTION__);
  708. edge_serial->read_in_progress = TRUE;
  709. /* we have pending bytes on the bulk in pipe, send a request */
  710. edge_serial->read_urb->dev = edge_serial->serial->dev;
  711. result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
  712. if (result) {
  713. dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __FUNCTION__, result);
  714. edge_serial->read_in_progress = FALSE;
  715. }
  716. }
  717. spin_unlock(&edge_serial->es_lock);
  718. }
  719. }
  720. /* grab the txcredits for the ports if available */
  721. position = 2;
  722. portNumber = 0;
  723. while ((position < length) && (portNumber < edge_serial->serial->num_ports)) {
  724. txCredits = data[position] | (data[position+1] << 8);
  725. if (txCredits) {
  726. port = edge_serial->serial->port[portNumber];
  727. edge_port = usb_get_serial_port_data(port);
  728. if (edge_port->open) {
  729. spin_lock(&edge_port->ep_lock);
  730. edge_port->txCredits += txCredits;
  731. spin_unlock(&edge_port->ep_lock);
  732. dbg("%s - txcredits for port%d = %d", __FUNCTION__, portNumber, edge_port->txCredits);
  733. /* tell the tty driver that something has changed */
  734. if (edge_port->port->tty)
  735. tty_wakeup(edge_port->port->tty);
  736. // Since we have more credit, check if more data can be sent
  737. send_more_port_data(edge_serial, edge_port);
  738. }
  739. }
  740. position += 2;
  741. ++portNumber;
  742. }
  743. }
  744. exit:
  745. result = usb_submit_urb (urb, GFP_ATOMIC);
  746. if (result) {
  747. dev_err(&urb->dev->dev, "%s - Error %d submitting control urb\n", __FUNCTION__, result);
  748. }
  749. }
  750. /*****************************************************************************
  751. * edge_bulk_in_callback
  752. * this is the callback function for when we have received data on the
  753. * bulk in endpoint.
  754. *****************************************************************************/
  755. static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs)
  756. {
  757. struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
  758. unsigned char *data = urb->transfer_buffer;
  759. int status;
  760. __u16 raw_data_length;
  761. dbg("%s", __FUNCTION__);
  762. if (urb->status) {
  763. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  764. edge_serial->read_in_progress = FALSE;
  765. return;
  766. }
  767. if (urb->actual_length == 0) {
  768. dbg("%s - read bulk callback with no data", __FUNCTION__);
  769. edge_serial->read_in_progress = FALSE;
  770. return;
  771. }
  772. raw_data_length = urb->actual_length;
  773. usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, raw_data_length, data);
  774. spin_lock(&edge_serial->es_lock);
  775. /* decrement our rxBytes available by the number that we just got */
  776. edge_serial->rxBytesAvail -= raw_data_length;
  777. dbg("%s - Received = %d, rxBytesAvail %d", __FUNCTION__, raw_data_length, edge_serial->rxBytesAvail);
  778. process_rcvd_data (edge_serial, data, urb->actual_length);
  779. /* check to see if there's any more data for us to read */
  780. if (edge_serial->rxBytesAvail > 0) {
  781. dbg("%s - posting a read", __FUNCTION__);
  782. edge_serial->read_urb->dev = edge_serial->serial->dev;
  783. status = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
  784. if (status) {
  785. dev_err(&urb->dev->dev, "%s - usb_submit_urb(read bulk) failed, status = %d\n", __FUNCTION__, status);
  786. edge_serial->read_in_progress = FALSE;
  787. }
  788. } else {
  789. edge_serial->read_in_progress = FALSE;
  790. }
  791. spin_unlock(&edge_serial->es_lock);
  792. }
  793. /*****************************************************************************
  794. * edge_bulk_out_data_callback
  795. * this is the callback function for when we have finished sending serial data
  796. * on the bulk out endpoint.
  797. *****************************************************************************/
  798. static void edge_bulk_out_data_callback (struct urb *urb, struct pt_regs *regs)
  799. {
  800. struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
  801. struct tty_struct *tty;
  802. dbg("%s", __FUNCTION__);
  803. if (urb->status) {
  804. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  805. }
  806. tty = edge_port->port->tty;
  807. if (tty && edge_port->open) {
  808. /* let the tty driver wakeup if it has a special write_wakeup function */
  809. tty_wakeup(tty);
  810. }
  811. // Release the Write URB
  812. edge_port->write_in_progress = FALSE;
  813. // Check if more data needs to be sent
  814. send_more_port_data((struct edgeport_serial *)(usb_get_serial_data(edge_port->port->serial)), edge_port);
  815. }
  816. /*****************************************************************************
  817. * BulkOutCmdCallback
  818. * this is the callback function for when we have finished sending a command
  819. * on the bulk out endpoint.
  820. *****************************************************************************/
  821. static void edge_bulk_out_cmd_callback (struct urb *urb, struct pt_regs *regs)
  822. {
  823. struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
  824. struct tty_struct *tty;
  825. int status = urb->status;
  826. dbg("%s", __FUNCTION__);
  827. CmdUrbs--;
  828. dbg("%s - FREE URB %p (outstanding %d)", __FUNCTION__, urb, CmdUrbs);
  829. /* clean up the transfer buffer */
  830. kfree(urb->transfer_buffer);
  831. /* Free the command urb */
  832. usb_free_urb (urb);
  833. if (status) {
  834. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, status);
  835. return;
  836. }
  837. /* Get pointer to tty */
  838. tty = edge_port->port->tty;
  839. /* tell the tty driver that something has changed */
  840. if (tty && edge_port->open)
  841. tty_wakeup(tty);
  842. /* we have completed the command */
  843. edge_port->commandPending = FALSE;
  844. wake_up(&edge_port->wait_command);
  845. }
  846. /*****************************************************************************
  847. * Driver tty interface functions
  848. *****************************************************************************/
  849. /*****************************************************************************
  850. * SerialOpen
  851. * this function is called by the tty driver when a port is opened
  852. * If successful, we return 0
  853. * Otherwise we return a negative error number.
  854. *****************************************************************************/
  855. static int edge_open (struct usb_serial_port *port, struct file * filp)
  856. {
  857. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  858. struct usb_serial *serial;
  859. struct edgeport_serial *edge_serial;
  860. int response;
  861. dbg("%s - port %d", __FUNCTION__, port->number);
  862. if (edge_port == NULL)
  863. return -ENODEV;
  864. if (port->tty)
  865. port->tty->low_latency = low_latency;
  866. /* see if we've set up our endpoint info yet (can't set it up in edge_startup
  867. as the structures were not set up at that time.) */
  868. serial = port->serial;
  869. edge_serial = usb_get_serial_data(serial);
  870. if (edge_serial == NULL) {
  871. return -ENODEV;
  872. }
  873. if (edge_serial->interrupt_in_buffer == NULL) {
  874. struct usb_serial_port *port0 = serial->port[0];
  875. /* not set up yet, so do it now */
  876. edge_serial->interrupt_in_buffer = port0->interrupt_in_buffer;
  877. edge_serial->interrupt_in_endpoint = port0->interrupt_in_endpointAddress;
  878. edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
  879. edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
  880. edge_serial->bulk_in_endpoint = port0->bulk_in_endpointAddress;
  881. edge_serial->read_urb = port0->read_urb;
  882. edge_serial->bulk_out_endpoint = port0->bulk_out_endpointAddress;
  883. /* set up our interrupt urb */
  884. usb_fill_int_urb(edge_serial->interrupt_read_urb,
  885. serial->dev,
  886. usb_rcvintpipe(serial->dev,
  887. port0->interrupt_in_endpointAddress),
  888. port0->interrupt_in_buffer,
  889. edge_serial->interrupt_read_urb->transfer_buffer_length,
  890. edge_interrupt_callback, edge_serial,
  891. edge_serial->interrupt_read_urb->interval);
  892. /* set up our bulk in urb */
  893. usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
  894. usb_rcvbulkpipe(serial->dev,
  895. port0->bulk_in_endpointAddress),
  896. port0->bulk_in_buffer,
  897. edge_serial->read_urb->transfer_buffer_length,
  898. edge_bulk_in_callback, edge_serial);
  899. edge_serial->read_in_progress = FALSE;
  900. /* start interrupt read for this edgeport
  901. * this interrupt will continue as long as the edgeport is connected */
  902. response = usb_submit_urb (edge_serial->interrupt_read_urb, GFP_KERNEL);
  903. if (response) {
  904. dev_err(&port->dev, "%s - Error %d submitting control urb\n", __FUNCTION__, response);
  905. }
  906. }
  907. /* initialize our wait queues */
  908. init_waitqueue_head(&edge_port->wait_open);
  909. init_waitqueue_head(&edge_port->wait_chase);
  910. init_waitqueue_head(&edge_port->delta_msr_wait);
  911. init_waitqueue_head(&edge_port->wait_command);
  912. /* initialize our icount structure */
  913. memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
  914. /* initialize our port settings */
  915. edge_port->txCredits = 0; /* Can't send any data yet */
  916. edge_port->shadowMCR = MCR_MASTER_IE; /* Must always set this bit to enable ints! */
  917. edge_port->chaseResponsePending = FALSE;
  918. /* send a open port command */
  919. edge_port->openPending = TRUE;
  920. edge_port->open = FALSE;
  921. response = send_iosp_ext_cmd (edge_port, IOSP_CMD_OPEN_PORT, 0);
  922. if (response < 0) {
  923. dev_err(&port->dev, "%s - error sending open port command\n", __FUNCTION__);
  924. edge_port->openPending = FALSE;
  925. return -ENODEV;
  926. }
  927. /* now wait for the port to be completely opened */
  928. wait_event_timeout(edge_port->wait_open, (edge_port->openPending != TRUE), OPEN_TIMEOUT);
  929. if (edge_port->open == FALSE) {
  930. /* open timed out */
  931. dbg("%s - open timedout", __FUNCTION__);
  932. edge_port->openPending = FALSE;
  933. return -ENODEV;
  934. }
  935. /* create the txfifo */
  936. edge_port->txfifo.head = 0;
  937. edge_port->txfifo.tail = 0;
  938. edge_port->txfifo.count = 0;
  939. edge_port->txfifo.size = edge_port->maxTxCredits;
  940. edge_port->txfifo.fifo = kmalloc (edge_port->maxTxCredits, GFP_KERNEL);
  941. if (!edge_port->txfifo.fifo) {
  942. dbg("%s - no memory", __FUNCTION__);
  943. edge_close (port, filp);
  944. return -ENOMEM;
  945. }
  946. /* Allocate a URB for the write */
  947. edge_port->write_urb = usb_alloc_urb (0, GFP_KERNEL);
  948. edge_port->write_in_progress = FALSE;
  949. if (!edge_port->write_urb) {
  950. dbg("%s - no memory", __FUNCTION__);
  951. edge_close (port, filp);
  952. return -ENOMEM;
  953. }
  954. dbg("%s(%d) - Initialize TX fifo to %d bytes", __FUNCTION__, port->number, edge_port->maxTxCredits);
  955. dbg("%s exited", __FUNCTION__);
  956. return 0;
  957. }
  958. /************************************************************************
  959. *
  960. * block_until_chase_response
  961. *
  962. * This function will block the close until one of the following:
  963. * 1. Response to our Chase comes from Edgeport
  964. * 2. A timout of 10 seconds without activity has expired
  965. * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
  966. *
  967. ************************************************************************/
  968. static void block_until_chase_response(struct edgeport_port *edge_port)
  969. {
  970. DEFINE_WAIT(wait);
  971. __u16 lastCredits;
  972. int timeout = 1*HZ;
  973. int loop = 10;
  974. while (1) {
  975. // Save Last credits
  976. lastCredits = edge_port->txCredits;
  977. // Did we get our Chase response
  978. if (edge_port->chaseResponsePending == FALSE) {
  979. dbg("%s - Got Chase Response", __FUNCTION__);
  980. // did we get all of our credit back?
  981. if (edge_port->txCredits == edge_port->maxTxCredits ) {
  982. dbg("%s - Got all credits", __FUNCTION__);
  983. return;
  984. }
  985. }
  986. // Block the thread for a while
  987. prepare_to_wait(&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
  988. schedule_timeout(timeout);
  989. finish_wait(&edge_port->wait_chase, &wait);
  990. if (lastCredits == edge_port->txCredits) {
  991. // No activity.. count down.
  992. loop--;
  993. if (loop == 0) {
  994. edge_port->chaseResponsePending = FALSE;
  995. dbg("%s - Chase TIMEOUT", __FUNCTION__);
  996. return;
  997. }
  998. } else {
  999. // Reset timout value back to 10 seconds
  1000. dbg("%s - Last %d, Current %d", __FUNCTION__, lastCredits, edge_port->txCredits);
  1001. loop = 10;
  1002. }
  1003. }
  1004. }
  1005. /************************************************************************
  1006. *
  1007. * block_until_tx_empty
  1008. *
  1009. * This function will block the close until one of the following:
  1010. * 1. TX count are 0
  1011. * 2. The edgeport has stopped
  1012. * 3. A timout of 3 seconds without activity has expired
  1013. *
  1014. ************************************************************************/
  1015. static void block_until_tx_empty (struct edgeport_port *edge_port)
  1016. {
  1017. DEFINE_WAIT(wait);
  1018. struct TxFifo *fifo = &edge_port->txfifo;
  1019. __u32 lastCount;
  1020. int timeout = HZ/10;
  1021. int loop = 30;
  1022. while (1) {
  1023. // Save Last count
  1024. lastCount = fifo->count;
  1025. // Is the Edgeport Buffer empty?
  1026. if (lastCount == 0) {
  1027. dbg("%s - TX Buffer Empty", __FUNCTION__);
  1028. return;
  1029. }
  1030. // Block the thread for a while
  1031. prepare_to_wait (&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
  1032. schedule_timeout(timeout);
  1033. finish_wait(&edge_port->wait_chase, &wait);
  1034. dbg("%s wait", __FUNCTION__);
  1035. if (lastCount == fifo->count) {
  1036. // No activity.. count down.
  1037. loop--;
  1038. if (loop == 0) {
  1039. dbg("%s - TIMEOUT", __FUNCTION__);
  1040. return;
  1041. }
  1042. } else {
  1043. // Reset timout value back to seconds
  1044. loop = 30;
  1045. }
  1046. }
  1047. }
  1048. /*****************************************************************************
  1049. * edge_close
  1050. * this function is called by the tty driver when a port is closed
  1051. *****************************************************************************/
  1052. static void edge_close (struct usb_serial_port *port, struct file * filp)
  1053. {
  1054. struct edgeport_serial *edge_serial;
  1055. struct edgeport_port *edge_port;
  1056. int status;
  1057. dbg("%s - port %d", __FUNCTION__, port->number);
  1058. edge_serial = usb_get_serial_data(port->serial);
  1059. edge_port = usb_get_serial_port_data(port);
  1060. if ((edge_serial == NULL) || (edge_port == NULL))
  1061. return;
  1062. // block until tx is empty
  1063. block_until_tx_empty(edge_port);
  1064. edge_port->closePending = TRUE;
  1065. /* flush and chase */
  1066. edge_port->chaseResponsePending = TRUE;
  1067. dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
  1068. status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
  1069. if (status == 0) {
  1070. // block until chase finished
  1071. block_until_chase_response(edge_port);
  1072. } else {
  1073. edge_port->chaseResponsePending = FALSE;
  1074. }
  1075. /* close the port */
  1076. dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __FUNCTION__);
  1077. send_iosp_ext_cmd (edge_port, IOSP_CMD_CLOSE_PORT, 0);
  1078. //port->close = TRUE;
  1079. edge_port->closePending = FALSE;
  1080. edge_port->open = FALSE;
  1081. edge_port->openPending = FALSE;
  1082. if (edge_port->write_urb) {
  1083. usb_kill_urb(edge_port->write_urb);
  1084. }
  1085. if (edge_port->write_urb) {
  1086. /* if this urb had a transfer buffer already (old transfer) free it */
  1087. kfree(edge_port->write_urb->transfer_buffer);
  1088. usb_free_urb(edge_port->write_urb);
  1089. edge_port->write_urb = NULL;
  1090. }
  1091. kfree(edge_port->txfifo.fifo);
  1092. edge_port->txfifo.fifo = NULL;
  1093. dbg("%s exited", __FUNCTION__);
  1094. }
  1095. /*****************************************************************************
  1096. * SerialWrite
  1097. * this function is called by the tty driver when data should be written to
  1098. * the port.
  1099. * If successful, we return the number of bytes written, otherwise we return
  1100. * a negative error number.
  1101. *****************************************************************************/
  1102. static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
  1103. {
  1104. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1105. struct TxFifo *fifo;
  1106. int copySize;
  1107. int bytesleft;
  1108. int firsthalf;
  1109. int secondhalf;
  1110. unsigned long flags;
  1111. dbg("%s - port %d", __FUNCTION__, port->number);
  1112. if (edge_port == NULL)
  1113. return -ENODEV;
  1114. // get a pointer to the Tx fifo
  1115. fifo = &edge_port->txfifo;
  1116. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1117. // calculate number of bytes to put in fifo
  1118. copySize = min ((unsigned int)count, (edge_port->txCredits - fifo->count));
  1119. dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes", __FUNCTION__,
  1120. port->number, count, edge_port->txCredits - fifo->count, copySize);
  1121. /* catch writes of 0 bytes which the tty driver likes to give us, and when txCredits is empty */
  1122. if (copySize == 0) {
  1123. dbg("%s - copySize = Zero", __FUNCTION__);
  1124. goto finish_write;
  1125. }
  1126. // queue the data
  1127. // since we can never overflow the buffer we do not have to check for full condition
  1128. // the copy is done is two parts -- first fill to the end of the buffer
  1129. // then copy the reset from the start of the buffer
  1130. bytesleft = fifo->size - fifo->head;
  1131. firsthalf = min (bytesleft, copySize);
  1132. dbg("%s - copy %d bytes of %d into fifo ", __FUNCTION__, firsthalf, bytesleft);
  1133. /* now copy our data */
  1134. memcpy(&fifo->fifo[fifo->head], data, firsthalf);
  1135. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, firsthalf, &fifo->fifo[fifo->head]);
  1136. // update the index and size
  1137. fifo->head += firsthalf;
  1138. fifo->count += firsthalf;
  1139. // wrap the index
  1140. if (fifo->head == fifo->size) {
  1141. fifo->head = 0;
  1142. }
  1143. secondhalf = copySize-firsthalf;
  1144. if (secondhalf) {
  1145. dbg("%s - copy rest of data %d", __FUNCTION__, secondhalf);
  1146. memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
  1147. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, secondhalf, &fifo->fifo[fifo->head]);
  1148. // update the index and size
  1149. fifo->count += secondhalf;
  1150. fifo->head += secondhalf;
  1151. // No need to check for wrap since we can not get to end of fifo in this part
  1152. }
  1153. finish_write:
  1154. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1155. send_more_port_data((struct edgeport_serial *)usb_get_serial_data(port->serial), edge_port);
  1156. dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __FUNCTION__, copySize, edge_port->txCredits, fifo->count);
  1157. return copySize;
  1158. }
  1159. /************************************************************************
  1160. *
  1161. * send_more_port_data()
  1162. *
  1163. * This routine attempts to write additional UART transmit data
  1164. * to a port over the USB bulk pipe. It is called (1) when new
  1165. * data has been written to a port's TxBuffer from higher layers
  1166. * (2) when the peripheral sends us additional TxCredits indicating
  1167. * that it can accept more Tx data for a given port; and (3) when
  1168. * a bulk write completes successfully and we want to see if we
  1169. * can transmit more.
  1170. *
  1171. ************************************************************************/
  1172. static void send_more_port_data(struct edgeport_serial *edge_serial, struct edgeport_port *edge_port)
  1173. {
  1174. struct TxFifo *fifo = &edge_port->txfifo;
  1175. struct urb *urb;
  1176. unsigned char *buffer;
  1177. int status;
  1178. int count;
  1179. int bytesleft;
  1180. int firsthalf;
  1181. int secondhalf;
  1182. unsigned long flags;
  1183. dbg("%s(%d)", __FUNCTION__, edge_port->port->number);
  1184. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1185. if (edge_port->write_in_progress ||
  1186. !edge_port->open ||
  1187. (fifo->count == 0)) {
  1188. dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d", __FUNCTION__, edge_port->port->number, fifo->count, edge_port->write_in_progress);
  1189. goto exit_send;
  1190. }
  1191. // since the amount of data in the fifo will always fit into the
  1192. // edgeport buffer we do not need to check the write length
  1193. // Do we have enough credits for this port to make it worthwhile
  1194. // to bother queueing a write. If it's too small, say a few bytes,
  1195. // it's better to wait for more credits so we can do a larger
  1196. // write.
  1197. if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits,EDGE_FW_BULK_MAX_PACKET_SIZE)) {
  1198. dbg("%s(%d) Not enough credit - fifo %d TxCredit %d", __FUNCTION__, edge_port->port->number, fifo->count, edge_port->txCredits );
  1199. goto exit_send;
  1200. }
  1201. // lock this write
  1202. edge_port->write_in_progress = TRUE;
  1203. // get a pointer to the write_urb
  1204. urb = edge_port->write_urb;
  1205. /* make sure transfer buffer is freed */
  1206. kfree(urb->transfer_buffer);
  1207. urb->transfer_buffer = NULL;
  1208. /* build the data header for the buffer and port that we are about to send out */
  1209. count = fifo->count;
  1210. buffer = kmalloc (count+2, GFP_ATOMIC);
  1211. if (buffer == NULL) {
  1212. dev_err(&edge_port->port->dev, "%s - no more kernel memory...\n", __FUNCTION__);
  1213. edge_port->write_in_progress = FALSE;
  1214. goto exit_send;
  1215. }
  1216. buffer[0] = IOSP_BUILD_DATA_HDR1 (edge_port->port->number - edge_port->port->serial->minor, count);
  1217. buffer[1] = IOSP_BUILD_DATA_HDR2 (edge_port->port->number - edge_port->port->serial->minor, count);
  1218. /* now copy our data */
  1219. bytesleft = fifo->size - fifo->tail;
  1220. firsthalf = min (bytesleft, count);
  1221. memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
  1222. fifo->tail += firsthalf;
  1223. fifo->count -= firsthalf;
  1224. if (fifo->tail == fifo->size) {
  1225. fifo->tail = 0;
  1226. }
  1227. secondhalf = count-firsthalf;
  1228. if (secondhalf) {
  1229. memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail], secondhalf);
  1230. fifo->tail += secondhalf;
  1231. fifo->count -= secondhalf;
  1232. }
  1233. if (count)
  1234. usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, count, &buffer[2]);
  1235. /* fill up the urb with all of our data and submit it */
  1236. usb_fill_bulk_urb (urb, edge_serial->serial->dev,
  1237. usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
  1238. buffer, count+2, edge_bulk_out_data_callback, edge_port);
  1239. /* decrement the number of credits we have by the number we just sent */
  1240. edge_port->txCredits -= count;
  1241. edge_port->icount.tx += count;
  1242. urb->dev = edge_serial->serial->dev;
  1243. status = usb_submit_urb(urb, GFP_ATOMIC);
  1244. if (status) {
  1245. /* something went wrong */
  1246. dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n", __FUNCTION__, status);
  1247. edge_port->write_in_progress = FALSE;
  1248. /* revert the credits as something bad happened. */
  1249. edge_port->txCredits += count;
  1250. edge_port->icount.tx -= count;
  1251. }
  1252. dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d", __FUNCTION__, count, edge_port->txCredits, fifo->count);
  1253. exit_send:
  1254. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1255. }
  1256. /*****************************************************************************
  1257. * edge_write_room
  1258. * this function is called by the tty driver when it wants to know how many
  1259. * bytes of data we can accept for a specific port.
  1260. * If successful, we return the amount of room that we have for this port
  1261. * (the txCredits),
  1262. * Otherwise we return a negative error number.
  1263. *****************************************************************************/
  1264. static int edge_write_room (struct usb_serial_port *port)
  1265. {
  1266. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1267. int room;
  1268. unsigned long flags;
  1269. dbg("%s", __FUNCTION__);
  1270. if (edge_port == NULL)
  1271. return -ENODEV;
  1272. if (edge_port->closePending == TRUE)
  1273. return -ENODEV;
  1274. dbg("%s - port %d", __FUNCTION__, port->number);
  1275. if (!edge_port->open) {
  1276. dbg("%s - port not opened", __FUNCTION__);
  1277. return -EINVAL;
  1278. }
  1279. // total of both buffers is still txCredit
  1280. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1281. room = edge_port->txCredits - edge_port->txfifo.count;
  1282. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1283. dbg("%s - returns %d", __FUNCTION__, room);
  1284. return room;
  1285. }
  1286. /*****************************************************************************
  1287. * edge_chars_in_buffer
  1288. * this function is called by the tty driver when it wants to know how many
  1289. * bytes of data we currently have outstanding in the port (data that has
  1290. * been written, but hasn't made it out the port yet)
  1291. * If successful, we return the number of bytes left to be written in the
  1292. * system,
  1293. * Otherwise we return a negative error number.
  1294. *****************************************************************************/
  1295. static int edge_chars_in_buffer (struct usb_serial_port *port)
  1296. {
  1297. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1298. int num_chars;
  1299. unsigned long flags;
  1300. dbg("%s", __FUNCTION__);
  1301. if (edge_port == NULL)
  1302. return -ENODEV;
  1303. if (edge_port->closePending == TRUE)
  1304. return -ENODEV;
  1305. if (!edge_port->open) {
  1306. dbg("%s - port not opened", __FUNCTION__);
  1307. return -EINVAL;
  1308. }
  1309. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1310. num_chars = edge_port->maxTxCredits - edge_port->txCredits + edge_port->txfifo.count;
  1311. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1312. if (num_chars) {
  1313. dbg("%s(port %d) - returns %d", __FUNCTION__, port->number, num_chars);
  1314. }
  1315. return num_chars;
  1316. }
  1317. /*****************************************************************************
  1318. * SerialThrottle
  1319. * this function is called by the tty driver when it wants to stop the data
  1320. * being read from the port.
  1321. *****************************************************************************/
  1322. static void edge_throttle (struct usb_serial_port *port)
  1323. {
  1324. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1325. struct tty_struct *tty;
  1326. int status;
  1327. dbg("%s - port %d", __FUNCTION__, port->number);
  1328. if (edge_port == NULL)
  1329. return;
  1330. if (!edge_port->open) {
  1331. dbg("%s - port not opened", __FUNCTION__);
  1332. return;
  1333. }
  1334. tty = port->tty;
  1335. if (!tty) {
  1336. dbg ("%s - no tty available", __FUNCTION__);
  1337. return;
  1338. }
  1339. /* if we are implementing XON/XOFF, send the stop character */
  1340. if (I_IXOFF(tty)) {
  1341. unsigned char stop_char = STOP_CHAR(tty);
  1342. status = edge_write (port, &stop_char, 1);
  1343. if (status <= 0) {
  1344. return;
  1345. }
  1346. }
  1347. /* if we are implementing RTS/CTS, toggle that line */
  1348. if (tty->termios->c_cflag & CRTSCTS) {
  1349. edge_port->shadowMCR &= ~MCR_RTS;
  1350. status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
  1351. if (status != 0) {
  1352. return;
  1353. }
  1354. }
  1355. return;
  1356. }
  1357. /*****************************************************************************
  1358. * edge_unthrottle
  1359. * this function is called by the tty driver when it wants to resume the data
  1360. * being read from the port (called after SerialThrottle is called)
  1361. *****************************************************************************/
  1362. static void edge_unthrottle (struct usb_serial_port *port)
  1363. {
  1364. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1365. struct tty_struct *tty;
  1366. int status;
  1367. dbg("%s - port %d", __FUNCTION__, port->number);
  1368. if (edge_port == NULL)
  1369. return;
  1370. if (!edge_port->open) {
  1371. dbg("%s - port not opened", __FUNCTION__);
  1372. return;
  1373. }
  1374. tty = port->tty;
  1375. if (!tty) {
  1376. dbg ("%s - no tty available", __FUNCTION__);
  1377. return;
  1378. }
  1379. /* if we are implementing XON/XOFF, send the start character */
  1380. if (I_IXOFF(tty)) {
  1381. unsigned char start_char = START_CHAR(tty);
  1382. status = edge_write (port, &start_char, 1);
  1383. if (status <= 0) {
  1384. return;
  1385. }
  1386. }
  1387. /* if we are implementing RTS/CTS, toggle that line */
  1388. if (tty->termios->c_cflag & CRTSCTS) {
  1389. edge_port->shadowMCR |= MCR_RTS;
  1390. status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
  1391. if (status != 0) {
  1392. return;
  1393. }
  1394. }
  1395. return;
  1396. }
  1397. /*****************************************************************************
  1398. * SerialSetTermios
  1399. * this function is called by the tty driver when it wants to change the termios structure
  1400. *****************************************************************************/
  1401. static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios)
  1402. {
  1403. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1404. struct tty_struct *tty = port->tty;
  1405. unsigned int cflag;
  1406. if (!port->tty || !port->tty->termios) {
  1407. dbg ("%s - no tty or termios", __FUNCTION__);
  1408. return;
  1409. }
  1410. cflag = tty->termios->c_cflag;
  1411. /* check that they really want us to change something */
  1412. if (old_termios) {
  1413. if (cflag == old_termios->c_cflag &&
  1414. tty->termios->c_iflag == old_termios->c_iflag) {
  1415. dbg("%s - nothing to change", __FUNCTION__);
  1416. return;
  1417. }
  1418. }
  1419. dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
  1420. tty->termios->c_cflag, tty->termios->c_iflag);
  1421. if (old_termios) {
  1422. dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
  1423. old_termios->c_cflag, old_termios->c_iflag);
  1424. }
  1425. dbg("%s - port %d", __FUNCTION__, port->number);
  1426. if (edge_port == NULL)
  1427. return;
  1428. if (!edge_port->open) {
  1429. dbg("%s - port not opened", __FUNCTION__);
  1430. return;
  1431. }
  1432. /* change the port settings to the new ones specified */
  1433. change_port_settings (edge_port, old_termios);
  1434. return;
  1435. }
  1436. /*****************************************************************************
  1437. * get_lsr_info - get line status register info
  1438. *
  1439. * Purpose: Let user call ioctl() to get info when the UART physically
  1440. * is emptied. On bus types like RS485, the transmitter must
  1441. * release the bus after transmitting. This must be done when
  1442. * the transmit shift register is empty, not be done when the
  1443. * transmit holding register is empty. This functionality
  1444. * allows an RS485 driver to be written in user space.
  1445. *****************************************************************************/
  1446. static int get_lsr_info(struct edgeport_port *edge_port, unsigned int __user *value)
  1447. {
  1448. unsigned int result = 0;
  1449. unsigned long flags;
  1450. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1451. if (edge_port->maxTxCredits == edge_port->txCredits &&
  1452. edge_port->txfifo.count == 0) {
  1453. dbg("%s -- Empty", __FUNCTION__);
  1454. result = TIOCSER_TEMT;
  1455. }
  1456. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1457. if (copy_to_user(value, &result, sizeof(int)))
  1458. return -EFAULT;
  1459. return 0;
  1460. }
  1461. static int get_number_bytes_avail(struct edgeport_port *edge_port, unsigned int __user *value)
  1462. {
  1463. unsigned int result = 0;
  1464. struct tty_struct *tty = edge_port->port->tty;
  1465. if (!tty)
  1466. return -ENOIOCTLCMD;
  1467. result = tty->read_cnt;
  1468. dbg("%s(%d) = %d", __FUNCTION__, edge_port->port->number, result);
  1469. if (copy_to_user(value, &result, sizeof(int)))
  1470. return -EFAULT;
  1471. //return 0;
  1472. return -ENOIOCTLCMD;
  1473. }
  1474. static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
  1475. {
  1476. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1477. unsigned int mcr;
  1478. dbg("%s - port %d", __FUNCTION__, port->number);
  1479. mcr = edge_port->shadowMCR;
  1480. if (set & TIOCM_RTS)
  1481. mcr |= MCR_RTS;
  1482. if (set & TIOCM_DTR)
  1483. mcr |= MCR_DTR;
  1484. if (set & TIOCM_LOOP)
  1485. mcr |= MCR_LOOPBACK;
  1486. if (clear & TIOCM_RTS)
  1487. mcr &= ~MCR_RTS;
  1488. if (clear & TIOCM_DTR)
  1489. mcr &= ~MCR_DTR;
  1490. if (clear & TIOCM_LOOP)
  1491. mcr &= ~MCR_LOOPBACK;
  1492. edge_port->shadowMCR = mcr;
  1493. send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
  1494. return 0;
  1495. }
  1496. static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
  1497. {
  1498. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1499. unsigned int result = 0;
  1500. unsigned int msr;
  1501. unsigned int mcr;
  1502. dbg("%s - port %d", __FUNCTION__, port->number);
  1503. msr = edge_port->shadowMSR;
  1504. mcr = edge_port->shadowMCR;
  1505. result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  1506. | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  1507. | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  1508. | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
  1509. | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  1510. | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  1511. dbg("%s -- %x", __FUNCTION__, result);
  1512. return result;
  1513. }
  1514. static int get_serial_info(struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
  1515. {
  1516. struct serial_struct tmp;
  1517. if (!retinfo)
  1518. return -EFAULT;
  1519. memset(&tmp, 0, sizeof(tmp));
  1520. tmp.type = PORT_16550A;
  1521. tmp.line = edge_port->port->serial->minor;
  1522. tmp.port = edge_port->port->number;
  1523. tmp.irq = 0;
  1524. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1525. tmp.xmit_fifo_size = edge_port->maxTxCredits;
  1526. tmp.baud_base = 9600;
  1527. tmp.close_delay = 5*HZ;
  1528. tmp.closing_wait = 30*HZ;
  1529. // tmp.custom_divisor = state->custom_divisor;
  1530. // tmp.hub6 = state->hub6;
  1531. // tmp.io_type = state->io_type;
  1532. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1533. return -EFAULT;
  1534. return 0;
  1535. }
  1536. /*****************************************************************************
  1537. * SerialIoctl
  1538. * this function handles any ioctl calls to the driver
  1539. *****************************************************************************/
  1540. static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
  1541. {
  1542. DEFINE_WAIT(wait);
  1543. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1544. struct async_icount cnow;
  1545. struct async_icount cprev;
  1546. struct serial_icounter_struct icount;
  1547. dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
  1548. switch (cmd) {
  1549. // return number of bytes available
  1550. case TIOCINQ:
  1551. dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
  1552. return get_number_bytes_avail(edge_port, (unsigned int __user *) arg);
  1553. break;
  1554. case TIOCSERGETLSR:
  1555. dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
  1556. return get_lsr_info(edge_port, (unsigned int __user *) arg);
  1557. return 0;
  1558. case TIOCGSERIAL:
  1559. dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
  1560. return get_serial_info(edge_port, (struct serial_struct __user *) arg);
  1561. case TIOCSSERIAL:
  1562. dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
  1563. break;
  1564. case TIOCMIWAIT:
  1565. dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
  1566. cprev = edge_port->icount;
  1567. while (1) {
  1568. prepare_to_wait(&edge_port->delta_msr_wait, &wait, TASK_INTERRUPTIBLE);
  1569. schedule();
  1570. finish_wait(&edge_port->delta_msr_wait, &wait);
  1571. /* see if a signal did it */
  1572. if (signal_pending(current))
  1573. return -ERESTARTSYS;
  1574. cnow = edge_port->icount;
  1575. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1576. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1577. return -EIO; /* no change => error */
  1578. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1579. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1580. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1581. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  1582. return 0;
  1583. }
  1584. cprev = cnow;
  1585. }
  1586. /* NOTREACHED */
  1587. break;
  1588. case TIOCGICOUNT:
  1589. cnow = edge_port->icount;
  1590. memset(&icount, 0, sizeof(icount));
  1591. icount.cts = cnow.cts;
  1592. icount.dsr = cnow.dsr;
  1593. icount.rng = cnow.rng;
  1594. icount.dcd = cnow.dcd;
  1595. icount.rx = cnow.rx;
  1596. icount.tx = cnow.tx;
  1597. icount.frame = cnow.frame;
  1598. icount.overrun = cnow.overrun;
  1599. icount.parity = cnow.parity;
  1600. icount.brk = cnow.brk;
  1601. icount.buf_overrun = cnow.buf_overrun;
  1602. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__, port->number, icount.rx, icount.tx );
  1603. if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
  1604. return -EFAULT;
  1605. return 0;
  1606. }
  1607. return -ENOIOCTLCMD;
  1608. }
  1609. /*****************************************************************************
  1610. * SerialBreak
  1611. * this function sends a break to the port
  1612. *****************************************************************************/
  1613. static void edge_break (struct usb_serial_port *port, int break_state)
  1614. {
  1615. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1616. int status;
  1617. /* flush and chase */
  1618. edge_port->chaseResponsePending = TRUE;
  1619. dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
  1620. status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
  1621. if (status == 0) {
  1622. // block until chase finished
  1623. block_until_chase_response(edge_port);
  1624. } else {
  1625. edge_port->chaseResponsePending = FALSE;
  1626. }
  1627. if (break_state == -1) {
  1628. dbg("%s - Sending IOSP_CMD_SET_BREAK", __FUNCTION__);
  1629. status = send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_BREAK, 0);
  1630. } else {
  1631. dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __FUNCTION__);
  1632. status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CLEAR_BREAK, 0);
  1633. }
  1634. if (status) {
  1635. dbg("%s - error sending break set/clear command.", __FUNCTION__);
  1636. }
  1637. return;
  1638. }
  1639. /*****************************************************************************
  1640. * process_rcvd_data
  1641. * this function handles the data received on the bulk in pipe.
  1642. *****************************************************************************/
  1643. static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char * buffer, __u16 bufferLength)
  1644. {
  1645. struct usb_serial_port *port;
  1646. struct edgeport_port *edge_port;
  1647. struct tty_struct *tty;
  1648. __u16 lastBufferLength;
  1649. __u16 rxLen;
  1650. dbg("%s", __FUNCTION__);
  1651. lastBufferLength = bufferLength + 1;
  1652. while (bufferLength > 0) {
  1653. /* failsafe incase we get a message that we don't understand */
  1654. if (lastBufferLength == bufferLength) {
  1655. dbg("%s - stuck in loop, exiting it.", __FUNCTION__);
  1656. break;
  1657. }
  1658. lastBufferLength = bufferLength;
  1659. switch (edge_serial->rxState) {
  1660. case EXPECT_HDR1:
  1661. edge_serial->rxHeader1 = *buffer;
  1662. ++buffer;
  1663. --bufferLength;
  1664. if (bufferLength == 0) {
  1665. edge_serial->rxState = EXPECT_HDR2;
  1666. break;
  1667. }
  1668. /* otherwise, drop on through */
  1669. case EXPECT_HDR2:
  1670. edge_serial->rxHeader2 = *buffer;
  1671. ++buffer;
  1672. --bufferLength;
  1673. dbg("%s - Hdr1=%02X Hdr2=%02X", __FUNCTION__, edge_serial->rxHeader1, edge_serial->rxHeader2);
  1674. // Process depending on whether this header is
  1675. // data or status
  1676. if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
  1677. // Decode this status header and goto EXPECT_HDR1 (if we
  1678. // can process the status with only 2 bytes), or goto
  1679. // EXPECT_HDR3 to get the third byte.
  1680. edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
  1681. edge_serial->rxStatusCode = IOSP_GET_STATUS_CODE(edge_serial->rxHeader1);
  1682. if (!IOSP_STATUS_IS_2BYTE(edge_serial->rxStatusCode)) {
  1683. // This status needs additional bytes. Save what we have
  1684. // and then wait for more data.
  1685. edge_serial->rxStatusParam = edge_serial->rxHeader2;
  1686. edge_serial->rxState = EXPECT_HDR3;
  1687. break;
  1688. }
  1689. // We have all the header bytes, process the status now
  1690. process_rcvd_status (edge_serial, edge_serial->rxHeader2, 0);
  1691. edge_serial->rxState = EXPECT_HDR1;
  1692. break;
  1693. } else {
  1694. edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
  1695. edge_serial->rxBytesRemaining = IOSP_GET_HDR_DATA_LEN(edge_serial->rxHeader1, edge_serial->rxHeader2);
  1696. dbg("%s - Data for Port %u Len %u", __FUNCTION__, edge_serial->rxPort, edge_serial->rxBytesRemaining);
  1697. //ASSERT( DevExt->RxPort < DevExt->NumPorts );
  1698. //ASSERT( DevExt->RxBytesRemaining < IOSP_MAX_DATA_LENGTH );
  1699. if (bufferLength == 0 ) {
  1700. edge_serial->rxState = EXPECT_DATA;
  1701. break;
  1702. }
  1703. // Else, drop through
  1704. }
  1705. case EXPECT_DATA: // Expect data
  1706. if (bufferLength < edge_serial->rxBytesRemaining) {
  1707. rxLen = bufferLength;
  1708. edge_serial->rxState = EXPECT_DATA; // Expect data to start next buffer
  1709. } else {
  1710. // BufLen >= RxBytesRemaining
  1711. rxLen = edge_serial->rxBytesRemaining;
  1712. edge_serial->rxState = EXPECT_HDR1; // Start another header next time
  1713. }
  1714. bufferLength -= rxLen;
  1715. edge_serial->rxBytesRemaining -= rxLen;
  1716. /* spit this data back into the tty driver if this port is open */
  1717. if (rxLen) {
  1718. port = edge_serial->serial->port[edge_serial->rxPort];
  1719. edge_port = usb_get_serial_port_data(port);
  1720. if (edge_port->open) {
  1721. tty = edge_port->port->tty;
  1722. if (tty) {
  1723. dbg("%s - Sending %d bytes to TTY for port %d", __FUNCTION__, rxLen, edge_serial->rxPort);
  1724. edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
  1725. }
  1726. edge_port->icount.rx += rxLen;
  1727. }
  1728. buffer += rxLen;
  1729. }
  1730. break;
  1731. case EXPECT_HDR3: // Expect 3rd byte of status header
  1732. edge_serial->rxHeader3 = *buffer;
  1733. ++buffer;
  1734. --bufferLength;
  1735. // We have all the header bytes, process the status now
  1736. process_rcvd_status (edge_serial, edge_serial->rxStatusParam, edge_serial->rxHeader3);
  1737. edge_serial->rxState = EXPECT_HDR1;
  1738. break;
  1739. }
  1740. }
  1741. }
  1742. /*****************************************************************************
  1743. * process_rcvd_status
  1744. * this function handles the any status messages received on the bulk in pipe.
  1745. *****************************************************************************/
  1746. static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3)
  1747. {
  1748. struct usb_serial_port *port;
  1749. struct edgeport_port *edge_port;
  1750. __u8 code = edge_serial->rxStatusCode;
  1751. /* switch the port pointer to the one being currently talked about */
  1752. port = edge_serial->serial->port[edge_serial->rxPort];
  1753. edge_port = usb_get_serial_port_data(port);
  1754. if (edge_port == NULL) {
  1755. dev_err(&edge_serial->serial->dev->dev, "%s - edge_port == NULL for port %d\n", __FUNCTION__, edge_serial->rxPort);
  1756. return;
  1757. }
  1758. dbg("%s - port %d", __FUNCTION__, edge_serial->rxPort);
  1759. if (code == IOSP_EXT_STATUS) {
  1760. switch (byte2) {
  1761. case IOSP_EXT_STATUS_CHASE_RSP:
  1762. // we want to do EXT status regardless of port open/closed
  1763. dbg("%s - Port %u EXT CHASE_RSP Data = %02x", __FUNCTION__, edge_serial->rxPort, byte3 );
  1764. // Currently, the only EXT_STATUS is Chase, so process here instead of one more call
  1765. // to one more subroutine. If/when more EXT_STATUS, there'll be more work to do.
  1766. // Also, we currently clear flag and close the port regardless of content of above's Byte3.
  1767. // We could choose to do something else when Byte3 says Timeout on Chase from Edgeport,
  1768. // like wait longer in block_until_chase_response, but for now we don't.
  1769. edge_port->chaseResponsePending = FALSE;
  1770. wake_up (&edge_port->wait_chase);
  1771. return;
  1772. case IOSP_EXT_STATUS_RX_CHECK_RSP:
  1773. dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __FUNCTION__, edge_serial->rxPort, byte3 );
  1774. //Port->RxCheckRsp = TRUE;
  1775. return;
  1776. }
  1777. }
  1778. if (code == IOSP_STATUS_OPEN_RSP) {
  1779. edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
  1780. edge_port->maxTxCredits = edge_port->txCredits;
  1781. dbg("%s - Port %u Open Response Inital MSR = %02x TxBufferSize = %d", __FUNCTION__, edge_serial->rxPort, byte2, edge_port->txCredits);
  1782. handle_new_msr (edge_port, byte2);
  1783. /* send the current line settings to the port so we are in sync with any further termios calls */
  1784. if (edge_port->port->tty)
  1785. change_port_settings (edge_port, edge_port->port->tty->termios);
  1786. /* we have completed the open */
  1787. edge_port->openPending = FALSE;
  1788. edge_port->open = TRUE;
  1789. wake_up(&edge_port->wait_open);
  1790. return;
  1791. }
  1792. // If port is closed, silently discard all rcvd status. We can
  1793. // have cases where buffered status is received AFTER the close
  1794. // port command is sent to the Edgeport.
  1795. if ((!edge_port->open ) || (edge_port->closePending)) {
  1796. return;
  1797. }
  1798. switch (code) {
  1799. // Not currently sent by Edgeport
  1800. case IOSP_STATUS_LSR:
  1801. dbg("%s - Port %u LSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
  1802. handle_new_lsr (edge_port, FALSE, byte2, 0);
  1803. break;
  1804. case IOSP_STATUS_LSR_DATA:
  1805. dbg("%s - Port %u LSR Status = %02x, Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
  1806. // byte2 is LSR Register
  1807. // byte3 is broken data byte
  1808. handle_new_lsr (edge_port, TRUE, byte2, byte3);
  1809. break;
  1810. //
  1811. // case IOSP_EXT_4_STATUS:
  1812. // dbg("%s - Port %u LSR Status = %02x Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
  1813. // break;
  1814. //
  1815. case IOSP_STATUS_MSR:
  1816. dbg("%s - Port %u MSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
  1817. // Process this new modem status and generate appropriate
  1818. // events, etc, based on the new status. This routine
  1819. // also saves the MSR in Port->ShadowMsr.
  1820. handle_new_msr(edge_port, byte2);
  1821. break;
  1822. default:
  1823. dbg("%s - Unrecognized IOSP status code %u\n", __FUNCTION__, code);
  1824. break;
  1825. }
  1826. return;
  1827. }
  1828. /*****************************************************************************
  1829. * edge_tty_recv
  1830. * this function passes data on to the tty flip buffer
  1831. *****************************************************************************/
  1832. static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
  1833. {
  1834. int cnt;
  1835. do {
  1836. if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
  1837. tty_flip_buffer_push(tty);
  1838. if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
  1839. dev_err(dev, "%s - dropping data, %d bytes lost\n",
  1840. __FUNCTION__, length);
  1841. return;
  1842. }
  1843. }
  1844. cnt = min(length, TTY_FLIPBUF_SIZE - tty->flip.count);
  1845. memcpy(tty->flip.char_buf_ptr, data, cnt);
  1846. memset(tty->flip.flag_buf_ptr, 0, cnt);
  1847. tty->flip.char_buf_ptr += cnt;
  1848. tty->flip.flag_buf_ptr += cnt;
  1849. tty->flip.count += cnt;
  1850. data += cnt;
  1851. length -= cnt;
  1852. } while (length > 0);
  1853. tty_flip_buffer_push(tty);
  1854. }
  1855. /*****************************************************************************
  1856. * handle_new_msr
  1857. * this function handles any change to the msr register for a port.
  1858. *****************************************************************************/
  1859. static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
  1860. {
  1861. struct async_icount *icount;
  1862. dbg("%s %02x", __FUNCTION__, newMsr);
  1863. if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
  1864. icount = &edge_port->icount;
  1865. /* update input line counters */
  1866. if (newMsr & EDGEPORT_MSR_DELTA_CTS) {
  1867. icount->cts++;
  1868. }
  1869. if (newMsr & EDGEPORT_MSR_DELTA_DSR) {
  1870. icount->dsr++;
  1871. }
  1872. if (newMsr & EDGEPORT_MSR_DELTA_CD) {
  1873. icount->dcd++;
  1874. }
  1875. if (newMsr & EDGEPORT_MSR_DELTA_RI) {
  1876. icount->rng++;
  1877. }
  1878. wake_up_interruptible(&edge_port->delta_msr_wait);
  1879. }
  1880. /* Save the new modem status */
  1881. edge_port->shadowMSR = newMsr & 0xf0;
  1882. return;
  1883. }
  1884. /*****************************************************************************
  1885. * handle_new_lsr
  1886. * this function handles any change to the lsr register for a port.
  1887. *****************************************************************************/
  1888. static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data)
  1889. {
  1890. __u8 newLsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
  1891. struct async_icount *icount;
  1892. dbg("%s - %02x", __FUNCTION__, newLsr);
  1893. edge_port->shadowLSR = lsr;
  1894. if (newLsr & LSR_BREAK) {
  1895. //
  1896. // Parity and Framing errors only count if they
  1897. // occur exclusive of a break being
  1898. // received.
  1899. //
  1900. newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
  1901. }
  1902. /* Place LSR data byte into Rx buffer */
  1903. if (lsrData && edge_port->port->tty)
  1904. edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
  1905. /* update input line counters */
  1906. icount = &edge_port->icount;
  1907. if (newLsr & LSR_BREAK) {
  1908. icount->brk++;
  1909. }
  1910. if (newLsr & LSR_OVER_ERR) {
  1911. icount->overrun++;
  1912. }
  1913. if (newLsr & LSR_PAR_ERR) {
  1914. icount->parity++;
  1915. }
  1916. if (newLsr & LSR_FRM_ERR) {
  1917. icount->frame++;
  1918. }
  1919. return;
  1920. }
  1921. /****************************************************************************
  1922. * sram_write
  1923. * writes a number of bytes to the Edgeport device's sram starting at the
  1924. * given address.
  1925. * If successful returns the number of bytes written, otherwise it returns
  1926. * a negative error number of the problem.
  1927. ****************************************************************************/
  1928. static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
  1929. {
  1930. int result;
  1931. __u16 current_length;
  1932. unsigned char *transfer_buffer;
  1933. dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
  1934. transfer_buffer = kmalloc (64, GFP_KERNEL);
  1935. if (!transfer_buffer) {
  1936. dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
  1937. return -ENOMEM;
  1938. }
  1939. /* need to split these writes up into 64 byte chunks */
  1940. result = 0;
  1941. while (length > 0) {
  1942. if (length > 64) {
  1943. current_length = 64;
  1944. } else {
  1945. current_length = length;
  1946. }
  1947. // dbg("%s - writing %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
  1948. memcpy (transfer_buffer, data, current_length);
  1949. result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_RAM,
  1950. 0x40, addr, extAddr, transfer_buffer, current_length, 300);
  1951. if (result < 0)
  1952. break;
  1953. length -= current_length;
  1954. addr += current_length;
  1955. data += current_length;
  1956. }
  1957. kfree (transfer_buffer);
  1958. return result;
  1959. }
  1960. /****************************************************************************
  1961. * rom_write
  1962. * writes a number of bytes to the Edgeport device's ROM starting at the
  1963. * given address.
  1964. * If successful returns the number of bytes written, otherwise it returns
  1965. * a negative error number of the problem.
  1966. ****************************************************************************/
  1967. static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
  1968. {
  1969. int result;
  1970. __u16 current_length;
  1971. unsigned char *transfer_buffer;
  1972. // dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
  1973. transfer_buffer = kmalloc (64, GFP_KERNEL);
  1974. if (!transfer_buffer) {
  1975. dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
  1976. return -ENOMEM;
  1977. }
  1978. /* need to split these writes up into 64 byte chunks */
  1979. result = 0;
  1980. while (length > 0) {
  1981. if (length > 64) {
  1982. current_length = 64;
  1983. } else {
  1984. current_length = length;
  1985. }
  1986. // dbg("%s - writing %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
  1987. memcpy (transfer_buffer, data, current_length);
  1988. result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_ROM,
  1989. 0x40, addr, extAddr, transfer_buffer, current_length, 300);
  1990. if (result < 0)
  1991. break;
  1992. length -= current_length;
  1993. addr += current_length;
  1994. data += current_length;
  1995. }
  1996. kfree (transfer_buffer);
  1997. return result;
  1998. }
  1999. /****************************************************************************
  2000. * rom_read
  2001. * reads a number of bytes from the Edgeport device starting at the given
  2002. * address.
  2003. * If successful returns the number of bytes read, otherwise it returns
  2004. * a negative error number of the problem.
  2005. ****************************************************************************/
  2006. static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
  2007. {
  2008. int result;
  2009. __u16 current_length;
  2010. unsigned char *transfer_buffer;
  2011. dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
  2012. transfer_buffer = kmalloc (64, GFP_KERNEL);
  2013. if (!transfer_buffer) {
  2014. dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
  2015. return -ENOMEM;
  2016. }
  2017. /* need to split these reads up into 64 byte chunks */
  2018. result = 0;
  2019. while (length > 0) {
  2020. if (length > 64) {
  2021. current_length = 64;
  2022. } else {
  2023. current_length = length;
  2024. }
  2025. // dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
  2026. result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), USB_REQUEST_ION_READ_ROM,
  2027. 0xC0, addr, extAddr, transfer_buffer, current_length, 300);
  2028. if (result < 0)
  2029. break;
  2030. memcpy (data, transfer_buffer, current_length);
  2031. length -= current_length;
  2032. addr += current_length;
  2033. data += current_length;
  2034. }
  2035. kfree (transfer_buffer);
  2036. return result;
  2037. }
  2038. /****************************************************************************
  2039. * send_iosp_ext_cmd
  2040. * Is used to send a IOSP message to the Edgeport device
  2041. ****************************************************************************/
  2042. static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param)
  2043. {
  2044. unsigned char *buffer;
  2045. unsigned char *currentCommand;
  2046. int length = 0;
  2047. int status = 0;
  2048. dbg("%s - %d, %d", __FUNCTION__, command, param);
  2049. buffer = kmalloc (10, GFP_ATOMIC);
  2050. if (!buffer) {
  2051. dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 10);
  2052. return -ENOMEM;
  2053. }
  2054. currentCommand = buffer;
  2055. MAKE_CMD_EXT_CMD (&currentCommand, &length,
  2056. edge_port->port->number - edge_port->port->serial->minor,
  2057. command, param);
  2058. status = write_cmd_usb (edge_port, buffer, length);
  2059. if (status) {
  2060. /* something bad happened, let's free up the memory */
  2061. kfree(buffer);
  2062. }
  2063. return status;
  2064. }
  2065. /*****************************************************************************
  2066. * write_cmd_usb
  2067. * this function writes the given buffer out to the bulk write endpoint.
  2068. *****************************************************************************/
  2069. static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int length)
  2070. {
  2071. struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
  2072. int status = 0;
  2073. struct urb *urb;
  2074. int timeout;
  2075. usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, length, buffer);
  2076. /* Allocate our next urb */
  2077. urb = usb_alloc_urb (0, GFP_ATOMIC);
  2078. if (!urb)
  2079. return -ENOMEM;
  2080. CmdUrbs++;
  2081. dbg("%s - ALLOCATE URB %p (outstanding %d)", __FUNCTION__, urb, CmdUrbs);
  2082. usb_fill_bulk_urb (urb, edge_serial->serial->dev,
  2083. usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
  2084. buffer, length, edge_bulk_out_cmd_callback, edge_port);
  2085. edge_port->commandPending = TRUE;
  2086. status = usb_submit_urb(urb, GFP_ATOMIC);
  2087. if (status) {
  2088. /* something went wrong */
  2089. dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write command) failed, status = %d\n", __FUNCTION__, status);
  2090. usb_kill_urb(urb);
  2091. usb_free_urb(urb);
  2092. CmdUrbs--;
  2093. return status;
  2094. }
  2095. // wait for command to finish
  2096. timeout = COMMAND_TIMEOUT;
  2097. #if 0
  2098. wait_event (&edge_port->wait_command, (edge_port->commandPending == FALSE));
  2099. if (edge_port->commandPending == TRUE) {
  2100. /* command timed out */
  2101. dbg("%s - command timed out", __FUNCTION__);
  2102. status = -EINVAL;
  2103. }
  2104. #endif
  2105. return status;
  2106. }
  2107. /*****************************************************************************
  2108. * send_cmd_write_baud_rate
  2109. * this function sends the proper command to change the baud rate of the
  2110. * specified port.
  2111. *****************************************************************************/
  2112. static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate)
  2113. {
  2114. unsigned char *cmdBuffer;
  2115. unsigned char *currCmd;
  2116. int cmdLen = 0;
  2117. int divisor;
  2118. int status;
  2119. unsigned char number = edge_port->port->number - edge_port->port->serial->minor;
  2120. dbg("%s - port = %d, baud = %d", __FUNCTION__, edge_port->port->number, baudRate);
  2121. status = calc_baud_rate_divisor (baudRate, &divisor);
  2122. if (status) {
  2123. dev_err(&edge_port->port->dev, "%s - bad baud rate\n", __FUNCTION__);
  2124. return status;
  2125. }
  2126. // Alloc memory for the string of commands.
  2127. cmdBuffer = kmalloc (0x100, GFP_ATOMIC);
  2128. if (!cmdBuffer) {
  2129. dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 0x100);
  2130. return -ENOMEM;
  2131. }
  2132. currCmd = cmdBuffer;
  2133. // Enable access to divisor latch
  2134. MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE );
  2135. // Write the divisor itself
  2136. MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLL, LOW8 (divisor) );
  2137. MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLM, HIGH8(divisor) );
  2138. // Restore original value to disable access to divisor latch
  2139. MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, edge_port->shadowLCR);
  2140. status = write_cmd_usb(edge_port, cmdBuffer, cmdLen );
  2141. if (status) {
  2142. /* something bad happened, let's free up the memory */
  2143. kfree (cmdBuffer);
  2144. }
  2145. return status;
  2146. }
  2147. /*****************************************************************************
  2148. * calc_baud_rate_divisor
  2149. * this function calculates the proper baud rate divisor for the specified
  2150. * baud rate.
  2151. *****************************************************************************/
  2152. static int calc_baud_rate_divisor (int baudrate, int *divisor)
  2153. {
  2154. int i;
  2155. __u16 custom;
  2156. dbg("%s - %d", __FUNCTION__, baudrate);
  2157. for (i = 0; i < NUM_ENTRIES(divisor_table); i++) {
  2158. if ( divisor_table[i].BaudRate == baudrate ) {
  2159. *divisor = divisor_table[i].Divisor;
  2160. return 0;
  2161. }
  2162. }
  2163. // We have tried all of the standard baud rates
  2164. // lets try to calculate the divisor for this baud rate
  2165. // Make sure the baud rate is reasonable
  2166. if (baudrate > 50 && baudrate < 230400) {
  2167. // get divisor
  2168. custom = (__u16)((230400L + baudrate/2) / baudrate);
  2169. *divisor = custom;
  2170. dbg("%s - Baud %d = %d\n", __FUNCTION__, baudrate, custom);
  2171. return 0;
  2172. }
  2173. return -1;
  2174. }
  2175. /*****************************************************************************
  2176. * send_cmd_write_uart_register
  2177. * this function builds up a uart register message and sends to to the device.
  2178. *****************************************************************************/
  2179. static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue)
  2180. {
  2181. unsigned char *cmdBuffer;
  2182. unsigned char *currCmd;
  2183. unsigned long cmdLen = 0;
  2184. int status;
  2185. dbg("%s - write to %s register 0x%02x", (regNum == MCR) ? "MCR" : "LCR", __FUNCTION__, regValue);
  2186. // Alloc memory for the string of commands.
  2187. cmdBuffer = kmalloc (0x10, GFP_ATOMIC);
  2188. if (cmdBuffer == NULL ) {
  2189. return -ENOMEM;
  2190. }
  2191. currCmd = cmdBuffer;
  2192. // Build a cmd in the buffer to write the given register
  2193. MAKE_CMD_WRITE_REG (&currCmd, &cmdLen,
  2194. edge_port->port->number - edge_port->port->serial->minor,
  2195. regNum, regValue);
  2196. status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
  2197. if (status) {
  2198. /* something bad happened, let's free up the memory */
  2199. kfree (cmdBuffer);
  2200. }
  2201. return status;
  2202. }
  2203. /*****************************************************************************
  2204. * change_port_settings
  2205. * This routine is called to set the UART on the device to match the specified
  2206. * new settings.
  2207. *****************************************************************************/
  2208. #ifndef CMSPAR
  2209. #define CMSPAR 0
  2210. #endif
  2211. static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios)
  2212. {
  2213. struct tty_struct *tty;
  2214. int baud;
  2215. unsigned cflag;
  2216. __u8 mask = 0xff;
  2217. __u8 lData;
  2218. __u8 lParity;
  2219. __u8 lStop;
  2220. __u8 rxFlow;
  2221. __u8 txFlow;
  2222. int status;
  2223. dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
  2224. if ((!edge_port->open) &&
  2225. (!edge_port->openPending)) {
  2226. dbg("%s - port not opened", __FUNCTION__);
  2227. return;
  2228. }
  2229. tty = edge_port->port->tty;
  2230. if ((!tty) ||
  2231. (!tty->termios)) {
  2232. dbg("%s - no tty structures", __FUNCTION__);
  2233. return;
  2234. }
  2235. cflag = tty->termios->c_cflag;
  2236. switch (cflag & CSIZE) {
  2237. case CS5: lData = LCR_BITS_5; mask = 0x1f; dbg("%s - data bits = 5", __FUNCTION__); break;
  2238. case CS6: lData = LCR_BITS_6; mask = 0x3f; dbg("%s - data bits = 6", __FUNCTION__); break;
  2239. case CS7: lData = LCR_BITS_7; mask = 0x7f; dbg("%s - data bits = 7", __FUNCTION__); break;
  2240. default:
  2241. case CS8: lData = LCR_BITS_8; dbg("%s - data bits = 8", __FUNCTION__); break;
  2242. }
  2243. lParity = LCR_PAR_NONE;
  2244. if (cflag & PARENB) {
  2245. if (cflag & CMSPAR) {
  2246. if (cflag & PARODD) {
  2247. lParity = LCR_PAR_MARK;
  2248. dbg("%s - parity = mark", __FUNCTION__);
  2249. } else {
  2250. lParity = LCR_PAR_SPACE;
  2251. dbg("%s - parity = space", __FUNCTION__);
  2252. }
  2253. } else if (cflag & PARODD) {
  2254. lParity = LCR_PAR_ODD;
  2255. dbg("%s - parity = odd", __FUNCTION__);
  2256. } else {
  2257. lParity = LCR_PAR_EVEN;
  2258. dbg("%s - parity = even", __FUNCTION__);
  2259. }
  2260. } else {
  2261. dbg("%s - parity = none", __FUNCTION__);
  2262. }
  2263. if (cflag & CSTOPB) {
  2264. lStop = LCR_STOP_2;
  2265. dbg("%s - stop bits = 2", __FUNCTION__);
  2266. } else {
  2267. lStop = LCR_STOP_1;
  2268. dbg("%s - stop bits = 1", __FUNCTION__);
  2269. }
  2270. /* figure out the flow control settings */
  2271. rxFlow = txFlow = 0x00;
  2272. if (cflag & CRTSCTS) {
  2273. rxFlow |= IOSP_RX_FLOW_RTS;
  2274. txFlow |= IOSP_TX_FLOW_CTS;
  2275. dbg("%s - RTS/CTS is enabled", __FUNCTION__);
  2276. } else {
  2277. dbg("%s - RTS/CTS is disabled", __FUNCTION__);
  2278. }
  2279. /* if we are implementing XON/XOFF, set the start and stop character in the device */
  2280. if (I_IXOFF(tty) || I_IXON(tty)) {
  2281. unsigned char stop_char = STOP_CHAR(tty);
  2282. unsigned char start_char = START_CHAR(tty);
  2283. send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_XON_CHAR, start_char);
  2284. send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_XOFF_CHAR, stop_char);
  2285. /* if we are implementing INBOUND XON/XOFF */
  2286. if (I_IXOFF(tty)) {
  2287. rxFlow |= IOSP_RX_FLOW_XON_XOFF;
  2288. dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __FUNCTION__, start_char, stop_char);
  2289. } else {
  2290. dbg("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
  2291. }
  2292. /* if we are implementing OUTBOUND XON/XOFF */
  2293. if (I_IXON(tty)) {
  2294. txFlow |= IOSP_TX_FLOW_XON_XOFF;
  2295. dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __FUNCTION__, start_char, stop_char);
  2296. } else {
  2297. dbg("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
  2298. }
  2299. }
  2300. /* Set flow control to the configured value */
  2301. send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
  2302. send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
  2303. edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  2304. edge_port->shadowLCR |= (lData | lParity | lStop);
  2305. edge_port->validDataMask = mask;
  2306. /* Send the updated LCR value to the EdgePort */
  2307. status = send_cmd_write_uart_register(edge_port, LCR, edge_port->shadowLCR);
  2308. if (status != 0) {
  2309. return;
  2310. }
  2311. /* set up the MCR register and send it to the EdgePort */
  2312. edge_port->shadowMCR = MCR_MASTER_IE;
  2313. if (cflag & CBAUD) {
  2314. edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
  2315. }
  2316. status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
  2317. if (status != 0) {
  2318. return;
  2319. }
  2320. /* Determine divisor based on baud rate */
  2321. baud = tty_get_baud_rate(tty);
  2322. if (!baud) {
  2323. /* pick a default, any default... */
  2324. baud = 9600;
  2325. }
  2326. dbg("%s - baud rate = %d", __FUNCTION__, baud);
  2327. status = send_cmd_write_baud_rate (edge_port, baud);
  2328. return;
  2329. }
  2330. /****************************************************************************
  2331. * unicode_to_ascii
  2332. * Turns a string from Unicode into ASCII.
  2333. * Doesn't do a good job with any characters that are outside the normal
  2334. * ASCII range, but it's only for debugging...
  2335. * NOTE: expects the unicode in LE format
  2336. ****************************************************************************/
  2337. static void unicode_to_ascii (char *string, __le16 *unicode, int unicode_size)
  2338. {
  2339. int i;
  2340. if (unicode_size <= 0)
  2341. return;
  2342. for (i = 0; i < unicode_size; ++i)
  2343. string[i] = (char)(le16_to_cpu(unicode[i]));
  2344. string[unicode_size] = 0x00;
  2345. }
  2346. /****************************************************************************
  2347. * get_manufacturing_desc
  2348. * reads in the manufacturing descriptor and stores it into the serial
  2349. * structure.
  2350. ****************************************************************************/
  2351. static void get_manufacturing_desc (struct edgeport_serial *edge_serial)
  2352. {
  2353. int response;
  2354. dbg("getting manufacturer descriptor");
  2355. response = rom_read (edge_serial->serial, (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
  2356. (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff), EDGE_MANUF_DESC_LEN,
  2357. (__u8 *)(&edge_serial->manuf_descriptor));
  2358. if (response < 1) {
  2359. dev_err(&edge_serial->serial->dev->dev, "error in getting manufacturer descriptor\n");
  2360. } else {
  2361. char string[30];
  2362. dbg("**Manufacturer Descriptor");
  2363. dbg(" RomSize: %dK", edge_serial->manuf_descriptor.RomSize);
  2364. dbg(" RamSize: %dK", edge_serial->manuf_descriptor.RamSize);
  2365. dbg(" CpuRev: %d", edge_serial->manuf_descriptor.CpuRev);
  2366. dbg(" BoardRev: %d", edge_serial->manuf_descriptor.BoardRev);
  2367. dbg(" NumPorts: %d", edge_serial->manuf_descriptor.NumPorts);
  2368. dbg(" DescDate: %d/%d/%d", edge_serial->manuf_descriptor.DescDate[0], edge_serial->manuf_descriptor.DescDate[1], edge_serial->manuf_descriptor.DescDate[2]+1900);
  2369. unicode_to_ascii (string, edge_serial->manuf_descriptor.SerialNumber, edge_serial->manuf_descriptor.SerNumLength/2-1);
  2370. dbg(" SerialNumber: %s", string);
  2371. unicode_to_ascii (string, edge_serial->manuf_descriptor.AssemblyNumber, edge_serial->manuf_descriptor.AssemblyNumLength/2-1);
  2372. dbg(" AssemblyNumber: %s", string);
  2373. unicode_to_ascii (string, edge_serial->manuf_descriptor.OemAssyNumber, edge_serial->manuf_descriptor.OemAssyNumLength/2-1);
  2374. dbg(" OemAssyNumber: %s", string);
  2375. dbg(" UartType: %d", edge_serial->manuf_descriptor.UartType);
  2376. dbg(" IonPid: %d", edge_serial->manuf_descriptor.IonPid);
  2377. dbg(" IonConfig: %d", edge_serial->manuf_descriptor.IonConfig);
  2378. }
  2379. }
  2380. /****************************************************************************
  2381. * get_boot_desc
  2382. * reads in the bootloader descriptor and stores it into the serial
  2383. * structure.
  2384. ****************************************************************************/
  2385. static void get_boot_desc (struct edgeport_serial *edge_serial)
  2386. {
  2387. int response;
  2388. dbg("getting boot descriptor");
  2389. response = rom_read (edge_serial->serial, (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
  2390. (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff), EDGE_BOOT_DESC_LEN,
  2391. (__u8 *)(&edge_serial->boot_descriptor));
  2392. if (response < 1) {
  2393. dev_err(&edge_serial->serial->dev->dev, "error in getting boot descriptor\n");
  2394. } else {
  2395. dbg("**Boot Descriptor:");
  2396. dbg(" BootCodeLength: %d", le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
  2397. dbg(" MajorVersion: %d", edge_serial->boot_descriptor.MajorVersion);
  2398. dbg(" MinorVersion: %d", edge_serial->boot_descriptor.MinorVersion);
  2399. dbg(" BuildNumber: %d", le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
  2400. dbg(" Capabilities: 0x%x", le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
  2401. dbg(" UConfig0: %d", edge_serial->boot_descriptor.UConfig0);
  2402. dbg(" UConfig1: %d", edge_serial->boot_descriptor.UConfig1);
  2403. }
  2404. }
  2405. /****************************************************************************
  2406. * load_application_firmware
  2407. * This is called to load the application firmware to the device
  2408. ****************************************************************************/
  2409. static void load_application_firmware (struct edgeport_serial *edge_serial)
  2410. {
  2411. struct edge_firmware_image_record *record;
  2412. unsigned char *firmware;
  2413. unsigned char *FirmwareImage;
  2414. int ImageSize;
  2415. int response;
  2416. switch (edge_serial->product_info.iDownloadFile) {
  2417. case EDGE_DOWNLOAD_FILE_I930:
  2418. dbg("downloading firmware version (930) %d.%d.%d",
  2419. OperationalCodeImageVersion_GEN1.MajorVersion,
  2420. OperationalCodeImageVersion_GEN1.MinorVersion,
  2421. OperationalCodeImageVersion_GEN1.BuildNumber);
  2422. firmware = &OperationalCodeImage_GEN1[0];
  2423. FirmwareImage = &OperationalCodeImage_GEN1[0];
  2424. ImageSize = sizeof(OperationalCodeImage_GEN1);
  2425. break;
  2426. case EDGE_DOWNLOAD_FILE_80251:
  2427. dbg("downloading firmware version (80251) %d.%d.%d",
  2428. OperationalCodeImageVersion_GEN2.MajorVersion,
  2429. OperationalCodeImageVersion_GEN2.MinorVersion,
  2430. OperationalCodeImageVersion_GEN2.BuildNumber);
  2431. firmware = &OperationalCodeImage_GEN2[0];
  2432. FirmwareImage = &OperationalCodeImage_GEN2[0];
  2433. ImageSize = sizeof(OperationalCodeImage_GEN2);
  2434. break;
  2435. case EDGE_DOWNLOAD_FILE_NONE:
  2436. dbg ("No download file specified, skipping download\n");
  2437. return;
  2438. default:
  2439. return;
  2440. }
  2441. for (;;) {
  2442. record = (struct edge_firmware_image_record *)firmware;
  2443. response = sram_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
  2444. if (response < 0) {
  2445. dev_err(&edge_serial->serial->dev->dev, "sram_write failed (%x, %x, %d)\n", le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len));
  2446. break;
  2447. }
  2448. firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
  2449. if (firmware >= &FirmwareImage[ImageSize]) {
  2450. break;
  2451. }
  2452. }
  2453. dbg("sending exec_dl_code");
  2454. response = usb_control_msg (edge_serial->serial->dev,
  2455. usb_sndctrlpipe(edge_serial->serial->dev, 0),
  2456. USB_REQUEST_ION_EXEC_DL_CODE,
  2457. 0x40, 0x4000, 0x0001, NULL, 0, 3000);
  2458. return;
  2459. }
  2460. /****************************************************************************
  2461. * edge_startup
  2462. ****************************************************************************/
  2463. static int edge_startup (struct usb_serial *serial)
  2464. {
  2465. struct edgeport_serial *edge_serial;
  2466. struct edgeport_port *edge_port;
  2467. struct usb_device *dev;
  2468. int i;
  2469. dev = serial->dev;
  2470. /* create our private serial structure */
  2471. edge_serial = kmalloc (sizeof(struct edgeport_serial), GFP_KERNEL);
  2472. if (edge_serial == NULL) {
  2473. dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
  2474. return -ENOMEM;
  2475. }
  2476. memset (edge_serial, 0, sizeof(struct edgeport_serial));
  2477. spin_lock_init(&edge_serial->es_lock);
  2478. edge_serial->serial = serial;
  2479. usb_set_serial_data(serial, edge_serial);
  2480. /* get the name for the device from the device */
  2481. if ( (i = get_string(dev, dev->descriptor.iManufacturer, &edge_serial->name[0])) != 0) {
  2482. edge_serial->name[i-1] = ' ';
  2483. }
  2484. get_string(dev, dev->descriptor.iProduct, &edge_serial->name[i]);
  2485. dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
  2486. /* get the manufacturing descriptor for this device */
  2487. get_manufacturing_desc (edge_serial);
  2488. /* get the boot descriptor */
  2489. get_boot_desc (edge_serial);
  2490. get_product_info(edge_serial);
  2491. /* set the number of ports from the manufacturing description */
  2492. /* serial->num_ports = serial->product_info.NumPorts; */
  2493. if (edge_serial->product_info.NumPorts != serial->num_ports) {
  2494. warn("%s - Device Reported %d serial ports vs core "
  2495. "thinking we have %d ports, email greg@kroah.com this info.",
  2496. __FUNCTION__, edge_serial->product_info.NumPorts,
  2497. serial->num_ports);
  2498. }
  2499. dbg("%s - time 1 %ld", __FUNCTION__, jiffies);
  2500. /* now load the application firmware into this device */
  2501. load_application_firmware (edge_serial);
  2502. dbg("%s - time 2 %ld", __FUNCTION__, jiffies);
  2503. /* Check current Edgeport EEPROM and update if necessary */
  2504. update_edgeport_E2PROM (edge_serial);
  2505. dbg("%s - time 3 %ld", __FUNCTION__, jiffies);
  2506. /* set the configuration to use #1 */
  2507. // dbg("set_configuration 1");
  2508. // usb_set_configuration (dev, 1);
  2509. /* we set up the pointers to the endpoints in the edge_open function,
  2510. * as the structures aren't created yet. */
  2511. /* set up our port private structures */
  2512. for (i = 0; i < serial->num_ports; ++i) {
  2513. edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
  2514. if (edge_port == NULL) {
  2515. dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
  2516. usb_set_serial_data(serial, NULL);
  2517. kfree(edge_serial);
  2518. return -ENOMEM;
  2519. }
  2520. memset (edge_port, 0, sizeof(struct edgeport_port));
  2521. spin_lock_init(&edge_port->ep_lock);
  2522. edge_port->port = serial->port[i];
  2523. usb_set_serial_port_data(serial->port[i], edge_port);
  2524. }
  2525. return 0;
  2526. }
  2527. /****************************************************************************
  2528. * edge_shutdown
  2529. * This function is called whenever the device is removed from the usb bus.
  2530. ****************************************************************************/
  2531. static void edge_shutdown (struct usb_serial *serial)
  2532. {
  2533. int i;
  2534. dbg("%s", __FUNCTION__);
  2535. /* stop reads and writes on all ports */
  2536. for (i=0; i < serial->num_ports; ++i) {
  2537. kfree (usb_get_serial_port_data(serial->port[i]));
  2538. usb_set_serial_port_data(serial->port[i], NULL);
  2539. }
  2540. kfree (usb_get_serial_data(serial));
  2541. usb_set_serial_data(serial, NULL);
  2542. }
  2543. /****************************************************************************
  2544. * edgeport_init
  2545. * This is called by the module subsystem, or on startup to initialize us
  2546. ****************************************************************************/
  2547. static int __init edgeport_init(void)
  2548. {
  2549. int retval;
  2550. retval = usb_serial_register(&edgeport_2port_device);
  2551. if (retval)
  2552. goto failed_2port_device_register;
  2553. retval = usb_serial_register(&edgeport_4port_device);
  2554. if (retval)
  2555. goto failed_4port_device_register;
  2556. retval = usb_serial_register(&edgeport_8port_device);
  2557. if (retval)
  2558. goto failed_8port_device_register;
  2559. retval = usb_register(&io_driver);
  2560. if (retval)
  2561. goto failed_usb_register;
  2562. info(DRIVER_DESC " " DRIVER_VERSION);
  2563. return 0;
  2564. failed_usb_register:
  2565. usb_serial_deregister(&edgeport_8port_device);
  2566. failed_8port_device_register:
  2567. usb_serial_deregister(&edgeport_4port_device);
  2568. failed_4port_device_register:
  2569. usb_serial_deregister(&edgeport_2port_device);
  2570. failed_2port_device_register:
  2571. return retval;
  2572. }
  2573. /****************************************************************************
  2574. * edgeport_exit
  2575. * Called when the driver is about to be unloaded.
  2576. ****************************************************************************/
  2577. static void __exit edgeport_exit (void)
  2578. {
  2579. usb_deregister (&io_driver);
  2580. usb_serial_deregister (&edgeport_2port_device);
  2581. usb_serial_deregister (&edgeport_4port_device);
  2582. usb_serial_deregister (&edgeport_8port_device);
  2583. }
  2584. module_init(edgeport_init);
  2585. module_exit(edgeport_exit);
  2586. /* Module information */
  2587. MODULE_AUTHOR( DRIVER_AUTHOR );
  2588. MODULE_DESCRIPTION( DRIVER_DESC );
  2589. MODULE_LICENSE("GPL");
  2590. module_param(debug, bool, S_IRUGO | S_IWUSR);
  2591. MODULE_PARM_DESC(debug, "Debug enabled or not");
  2592. module_param(low_latency, bool, S_IRUGO | S_IWUSR);
  2593. MODULE_PARM_DESC(low_latency, "Low latency enabled or not");