io_edgeport.c 100 KB

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