io_edgeport.c 96 KB

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