io_edgeport.c 96 KB

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