io_ti.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137
  1. /*
  2. * Edgeport USB Serial Converter driver
  3. *
  4. * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
  5. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Supports the following devices:
  13. * EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
  14. *
  15. * For questions or problems with this driver, contact Inside Out
  16. * Networks technical support, or Peter Berger <pberger@brimson.com>,
  17. * or Al Borchers <alborchers@steinerpoint.com>.
  18. *
  19. * Version history:
  20. *
  21. * July 11, 2002 Removed 4 port device structure since all TI UMP
  22. * chips have only 2 ports
  23. * David Iacovelli (davidi@ionetworks.com)
  24. *
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/errno.h>
  29. #include <linux/init.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/mutex.h>
  37. #include <linux/serial.h>
  38. #include <linux/ioctl.h>
  39. #include <asm/uaccess.h>
  40. #include <linux/usb.h>
  41. #include <linux/usb/serial.h>
  42. #include "io_16654.h"
  43. #include "io_usbvend.h"
  44. #include "io_ti.h"
  45. /*
  46. * Version Information
  47. */
  48. #define DRIVER_VERSION "v0.7mode043006"
  49. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
  50. #define DRIVER_DESC "Edgeport USB Serial Driver"
  51. /* firmware image code */
  52. #define IMAGE_VERSION_NAME PagableOperationalCodeImageVersion
  53. #define IMAGE_ARRAY_NAME PagableOperationalCodeImage
  54. #define IMAGE_SIZE PagableOperationalCodeSize
  55. #include "io_fw_down3.h" /* Define array OperationalCodeImage[] */
  56. #define EPROM_PAGE_SIZE 64
  57. struct edgeport_uart_buf_desc {
  58. __u32 count; // Number of bytes currently in buffer
  59. };
  60. /* different hardware types */
  61. #define HARDWARE_TYPE_930 0
  62. #define HARDWARE_TYPE_TIUMP 1
  63. // IOCTL_PRIVATE_TI_GET_MODE Definitions
  64. #define TI_MODE_CONFIGURING 0 // Device has not entered start device
  65. #define TI_MODE_BOOT 1 // Staying in boot mode
  66. #define TI_MODE_DOWNLOAD 2 // Made it to download mode
  67. #define TI_MODE_TRANSITIONING 3 // Currently in boot mode but transitioning to download mode
  68. /* read urb state */
  69. #define EDGE_READ_URB_RUNNING 0
  70. #define EDGE_READ_URB_STOPPING 1
  71. #define EDGE_READ_URB_STOPPED 2
  72. #define EDGE_LOW_LATENCY 1
  73. #define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
  74. #define EDGE_OUT_BUF_SIZE 1024
  75. /* Product information read from the Edgeport */
  76. struct product_info
  77. {
  78. int TiMode; // Current TI Mode
  79. __u8 hardware_type; // Type of hardware
  80. } __attribute__((packed));
  81. /* circular buffer */
  82. struct edge_buf {
  83. unsigned int buf_size;
  84. char *buf_buf;
  85. char *buf_get;
  86. char *buf_put;
  87. };
  88. struct edgeport_port {
  89. __u16 uart_base;
  90. __u16 dma_address;
  91. __u8 shadow_msr;
  92. __u8 shadow_mcr;
  93. __u8 shadow_lsr;
  94. __u8 lsr_mask;
  95. __u32 ump_read_timeout; /* Number of miliseconds the UMP will
  96. wait without data before completing
  97. a read short */
  98. int baud_rate;
  99. int close_pending;
  100. int lsr_event;
  101. struct edgeport_uart_buf_desc tx;
  102. struct async_icount icount;
  103. wait_queue_head_t delta_msr_wait; /* for handling sleeping while
  104. waiting for msr change to
  105. happen */
  106. struct edgeport_serial *edge_serial;
  107. struct usb_serial_port *port;
  108. __u8 bUartMode; /* Port type, 0: RS232, etc. */
  109. spinlock_t ep_lock;
  110. int ep_read_urb_state;
  111. int ep_write_urb_in_use;
  112. struct edge_buf *ep_out_buf;
  113. };
  114. struct edgeport_serial {
  115. struct product_info product_info;
  116. u8 TI_I2C_Type; // Type of I2C in UMP
  117. u8 TiReadI2C; // Set to TRUE if we have read the I2c in Boot Mode
  118. struct mutex es_lock;
  119. int num_ports_open;
  120. struct usb_serial *serial;
  121. };
  122. /* Devices that this driver supports */
  123. static struct usb_device_id edgeport_1port_id_table [] = {
  124. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
  125. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
  126. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
  127. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
  128. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
  129. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
  130. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
  131. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
  132. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
  133. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
  134. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
  135. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
  136. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
  137. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
  138. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
  139. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
  140. { }
  141. };
  142. static struct usb_device_id edgeport_2port_id_table [] = {
  143. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
  144. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
  145. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
  146. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
  147. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
  148. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
  149. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
  150. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
  151. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
  152. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
  153. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
  154. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
  155. /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
  156. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
  157. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
  158. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
  159. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
  160. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
  161. { }
  162. };
  163. /* Devices that this driver supports */
  164. static struct usb_device_id id_table_combined [] = {
  165. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
  166. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
  167. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
  168. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
  169. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
  170. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
  171. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
  172. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
  173. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
  174. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
  175. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
  176. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
  177. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
  178. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
  179. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
  180. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
  181. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
  182. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
  183. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
  184. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
  185. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
  186. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
  187. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
  188. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
  189. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
  190. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
  191. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
  192. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
  193. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
  194. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
  195. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
  196. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
  197. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
  198. { }
  199. };
  200. MODULE_DEVICE_TABLE (usb, id_table_combined);
  201. static struct usb_driver io_driver = {
  202. .name = "io_ti",
  203. .probe = usb_serial_probe,
  204. .disconnect = usb_serial_disconnect,
  205. .id_table = id_table_combined,
  206. .no_dynamic_id = 1,
  207. };
  208. static struct EDGE_FIRMWARE_VERSION_INFO OperationalCodeImageVersion;
  209. static int debug;
  210. static int TIStayInBootMode = 0;
  211. static int low_latency = EDGE_LOW_LATENCY;
  212. static int closing_wait = EDGE_CLOSING_WAIT;
  213. static int ignore_cpu_rev = 0;
  214. static int default_uart_mode = 0; /* RS232 */
  215. static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
  216. static void stop_read(struct edgeport_port *edge_port);
  217. static int restart_read(struct edgeport_port *edge_port);
  218. static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios);
  219. static void edge_send(struct usb_serial_port *port);
  220. /* sysfs attributes */
  221. static int edge_create_sysfs_attrs(struct usb_serial_port *port);
  222. static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
  223. /* circular buffer */
  224. static struct edge_buf *edge_buf_alloc(unsigned int size);
  225. static void edge_buf_free(struct edge_buf *eb);
  226. static void edge_buf_clear(struct edge_buf *eb);
  227. static unsigned int edge_buf_data_avail(struct edge_buf *eb);
  228. static unsigned int edge_buf_space_avail(struct edge_buf *eb);
  229. static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
  230. unsigned int count);
  231. static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
  232. unsigned int count);
  233. static int TIReadVendorRequestSync (struct usb_device *dev,
  234. __u8 request,
  235. __u16 value,
  236. __u16 index,
  237. u8 *data,
  238. int size)
  239. {
  240. int status;
  241. status = usb_control_msg (dev,
  242. usb_rcvctrlpipe(dev, 0),
  243. request,
  244. (USB_TYPE_VENDOR |
  245. USB_RECIP_DEVICE |
  246. USB_DIR_IN),
  247. value,
  248. index,
  249. data,
  250. size,
  251. 1000);
  252. if (status < 0)
  253. return status;
  254. if (status != size) {
  255. dbg ("%s - wanted to write %d, but only wrote %d",
  256. __func__, size, status);
  257. return -ECOMM;
  258. }
  259. return 0;
  260. }
  261. static int TISendVendorRequestSync (struct usb_device *dev,
  262. __u8 request,
  263. __u16 value,
  264. __u16 index,
  265. u8 *data,
  266. int size)
  267. {
  268. int status;
  269. status = usb_control_msg (dev,
  270. usb_sndctrlpipe(dev, 0),
  271. request,
  272. (USB_TYPE_VENDOR |
  273. USB_RECIP_DEVICE |
  274. USB_DIR_OUT),
  275. value,
  276. index,
  277. data,
  278. size,
  279. 1000);
  280. if (status < 0)
  281. return status;
  282. if (status != size) {
  283. dbg ("%s - wanted to write %d, but only wrote %d",
  284. __func__, size, status);
  285. return -ECOMM;
  286. }
  287. return 0;
  288. }
  289. static int TIWriteCommandSync (struct usb_device *dev, __u8 command,
  290. __u8 moduleid, __u16 value, u8 *data,
  291. int size)
  292. {
  293. return TISendVendorRequestSync (dev,
  294. command, // Request
  295. value, // wValue
  296. moduleid, // wIndex
  297. data, // TransferBuffer
  298. size); // TransferBufferLength
  299. }
  300. /* clear tx/rx buffers and fifo in TI UMP */
  301. static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask)
  302. {
  303. int port_number = port->number - port->serial->minor;
  304. dbg ("%s - port %d, mask %x", __func__, port_number, mask);
  305. return TIWriteCommandSync (port->serial->dev,
  306. UMPC_PURGE_PORT,
  307. (__u8)(UMPM_UART1_PORT + port_number),
  308. mask,
  309. NULL,
  310. 0);
  311. }
  312. /**
  313. * TIReadDownloadMemory - Read edgeport memory from TI chip
  314. * @dev: usb device pointer
  315. * @start_address: Device CPU address at which to read
  316. * @length: Length of above data
  317. * @address_type: Can read both XDATA and I2C
  318. * @buffer: pointer to input data buffer
  319. */
  320. static int TIReadDownloadMemory(struct usb_device *dev, int start_address,
  321. int length, __u8 address_type, __u8 *buffer)
  322. {
  323. int status = 0;
  324. __u8 read_length;
  325. __be16 be_start_address;
  326. dbg ("%s - @ %x for %d", __func__, start_address, length);
  327. /* Read in blocks of 64 bytes
  328. * (TI firmware can't handle more than 64 byte reads)
  329. */
  330. while (length) {
  331. if (length > 64)
  332. read_length= 64;
  333. else
  334. read_length = (__u8)length;
  335. if (read_length > 1) {
  336. dbg ("%s - @ %x for %d", __func__,
  337. start_address, read_length);
  338. }
  339. be_start_address = cpu_to_be16 (start_address);
  340. status = TIReadVendorRequestSync (dev,
  341. UMPC_MEMORY_READ, // Request
  342. (__u16)address_type, // wValue (Address type)
  343. (__force __u16)be_start_address, // wIndex (Address to read)
  344. buffer, // TransferBuffer
  345. read_length); // TransferBufferLength
  346. if (status) {
  347. dbg ("%s - ERROR %x", __func__, status);
  348. return status;
  349. }
  350. if (read_length > 1) {
  351. usb_serial_debug_data(debug, &dev->dev, __func__,
  352. read_length, buffer);
  353. }
  354. /* Update pointers/length */
  355. start_address += read_length;
  356. buffer += read_length;
  357. length -= read_length;
  358. }
  359. return status;
  360. }
  361. static int TIReadRam (struct usb_device *dev, int start_address, int length, __u8 *buffer)
  362. {
  363. return TIReadDownloadMemory (dev,
  364. start_address,
  365. length,
  366. DTK_ADDR_SPACE_XDATA,
  367. buffer);
  368. }
  369. /* Read edgeport memory to a given block */
  370. static int TIReadBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 * buffer)
  371. {
  372. int status = 0;
  373. int i;
  374. for (i=0; i< length; i++) {
  375. status = TIReadVendorRequestSync (serial->serial->dev,
  376. UMPC_MEMORY_READ, // Request
  377. serial->TI_I2C_Type, // wValue (Address type)
  378. (__u16)(start_address+i), // wIndex
  379. &buffer[i], // TransferBuffer
  380. 0x01); // TransferBufferLength
  381. if (status) {
  382. dbg ("%s - ERROR %x", __func__, status);
  383. return status;
  384. }
  385. }
  386. dbg ("%s - start_address = %x, length = %d", __func__, start_address, length);
  387. usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, length, buffer);
  388. serial->TiReadI2C = 1;
  389. return status;
  390. }
  391. /* Write given block to TI EPROM memory */
  392. static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
  393. {
  394. int status = 0;
  395. int i;
  396. __u8 temp;
  397. /* Must do a read before write */
  398. if (!serial->TiReadI2C) {
  399. status = TIReadBootMemory(serial, 0, 1, &temp);
  400. if (status)
  401. return status;
  402. }
  403. for (i=0; i < length; ++i) {
  404. status = TISendVendorRequestSync (serial->serial->dev,
  405. UMPC_MEMORY_WRITE, // Request
  406. buffer[i], // wValue
  407. (__u16)(i+start_address), // wIndex
  408. NULL, // TransferBuffer
  409. 0); // TransferBufferLength
  410. if (status)
  411. return status;
  412. }
  413. dbg ("%s - start_sddr = %x, length = %d", __func__, start_address, length);
  414. usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, length, buffer);
  415. return status;
  416. }
  417. /* Write edgeport I2C memory to TI chip */
  418. static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address, int length, __u8 address_type, __u8 *buffer)
  419. {
  420. int status = 0;
  421. int write_length;
  422. __be16 be_start_address;
  423. /* We can only send a maximum of 1 aligned byte page at a time */
  424. /* calulate the number of bytes left in the first page */
  425. write_length = EPROM_PAGE_SIZE - (start_address & (EPROM_PAGE_SIZE - 1));
  426. if (write_length > length)
  427. write_length = length;
  428. dbg ("%s - BytesInFirstPage Addr = %x, length = %d", __func__, start_address, write_length);
  429. usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, write_length, buffer);
  430. /* Write first page */
  431. be_start_address = cpu_to_be16 (start_address);
  432. status = TISendVendorRequestSync (serial->serial->dev,
  433. UMPC_MEMORY_WRITE, // Request
  434. (__u16)address_type, // wValue
  435. (__force __u16)be_start_address, // wIndex
  436. buffer, // TransferBuffer
  437. write_length);
  438. if (status) {
  439. dbg ("%s - ERROR %d", __func__, status);
  440. return status;
  441. }
  442. length -= write_length;
  443. start_address += write_length;
  444. buffer += write_length;
  445. /* We should be aligned now -- can write max page size bytes at a time */
  446. while (length) {
  447. if (length > EPROM_PAGE_SIZE)
  448. write_length = EPROM_PAGE_SIZE;
  449. else
  450. write_length = length;
  451. dbg ("%s - Page Write Addr = %x, length = %d", __func__, start_address, write_length);
  452. usb_serial_debug_data(debug, &serial->serial->dev->dev, __func__, write_length, buffer);
  453. /* Write next page */
  454. be_start_address = cpu_to_be16 (start_address);
  455. status = TISendVendorRequestSync (serial->serial->dev,
  456. UMPC_MEMORY_WRITE, // Request
  457. (__u16)address_type, // wValue
  458. (__force __u16)be_start_address, // wIndex
  459. buffer, // TransferBuffer
  460. write_length); // TransferBufferLength
  461. if (status) {
  462. dev_err (&serial->serial->dev->dev, "%s - ERROR %d\n", __func__, status);
  463. return status;
  464. }
  465. length -= write_length;
  466. start_address += write_length;
  467. buffer += write_length;
  468. }
  469. return status;
  470. }
  471. /* Examine the UMP DMA registers and LSR
  472. *
  473. * Check the MSBit of the X and Y DMA byte count registers.
  474. * A zero in this bit indicates that the TX DMA buffers are empty
  475. * then check the TX Empty bit in the UART.
  476. */
  477. static int TIIsTxActive (struct edgeport_port *port)
  478. {
  479. int status;
  480. struct out_endpoint_desc_block *oedb;
  481. __u8 *lsr;
  482. int bytes_left = 0;
  483. oedb = kmalloc (sizeof (* oedb), GFP_KERNEL);
  484. if (!oedb) {
  485. dev_err (&port->port->dev, "%s - out of memory\n", __func__);
  486. return -ENOMEM;
  487. }
  488. lsr = kmalloc (1, GFP_KERNEL); /* Sigh, that's right, just one byte,
  489. as not all platforms can do DMA
  490. from stack */
  491. if (!lsr) {
  492. kfree(oedb);
  493. return -ENOMEM;
  494. }
  495. /* Read the DMA Count Registers */
  496. status = TIReadRam (port->port->serial->dev,
  497. port->dma_address,
  498. sizeof( *oedb),
  499. (void *)oedb);
  500. if (status)
  501. goto exit_is_tx_active;
  502. dbg ("%s - XByteCount 0x%X", __func__, oedb->XByteCount);
  503. /* and the LSR */
  504. status = TIReadRam (port->port->serial->dev,
  505. port->uart_base + UMPMEM_OFFS_UART_LSR,
  506. 1,
  507. lsr);
  508. if (status)
  509. goto exit_is_tx_active;
  510. dbg ("%s - LSR = 0x%X", __func__, *lsr);
  511. /* If either buffer has data or we are transmitting then return TRUE */
  512. if ((oedb->XByteCount & 0x80 ) != 0 )
  513. bytes_left += 64;
  514. if ((*lsr & UMP_UART_LSR_TX_MASK ) == 0 )
  515. bytes_left += 1;
  516. /* We return Not Active if we get any kind of error */
  517. exit_is_tx_active:
  518. dbg ("%s - return %d", __func__, bytes_left );
  519. kfree(lsr);
  520. kfree(oedb);
  521. return bytes_left;
  522. }
  523. static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int flush)
  524. {
  525. int baud_rate;
  526. struct tty_struct *tty = port->port->tty;
  527. wait_queue_t wait;
  528. unsigned long flags;
  529. if (!timeout)
  530. timeout = (HZ*EDGE_CLOSING_WAIT)/100;
  531. /* wait for data to drain from the buffer */
  532. spin_lock_irqsave(&port->ep_lock, flags);
  533. init_waitqueue_entry(&wait, current);
  534. add_wait_queue(&tty->write_wait, &wait);
  535. for (;;) {
  536. set_current_state(TASK_INTERRUPTIBLE);
  537. if (edge_buf_data_avail(port->ep_out_buf) == 0
  538. || timeout == 0 || signal_pending(current)
  539. || !usb_get_intfdata(port->port->serial->interface)) /* disconnect */
  540. break;
  541. spin_unlock_irqrestore(&port->ep_lock, flags);
  542. timeout = schedule_timeout(timeout);
  543. spin_lock_irqsave(&port->ep_lock, flags);
  544. }
  545. set_current_state(TASK_RUNNING);
  546. remove_wait_queue(&tty->write_wait, &wait);
  547. if (flush)
  548. edge_buf_clear(port->ep_out_buf);
  549. spin_unlock_irqrestore(&port->ep_lock, flags);
  550. /* wait for data to drain from the device */
  551. timeout += jiffies;
  552. while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
  553. && usb_get_intfdata(port->port->serial->interface)) { /* not disconnected */
  554. if (!TIIsTxActive(port))
  555. break;
  556. msleep(10);
  557. }
  558. /* disconnected */
  559. if (!usb_get_intfdata(port->port->serial->interface))
  560. return;
  561. /* wait one more character time, based on baud rate */
  562. /* (TIIsTxActive doesn't seem to wait for the last byte) */
  563. if ((baud_rate=port->baud_rate) == 0)
  564. baud_rate = 50;
  565. msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
  566. }
  567. static int TIChooseConfiguration (struct usb_device *dev)
  568. {
  569. // There may be multiple configurations on this device, in which case
  570. // we would need to read and parse all of them to find out which one
  571. // we want. However, we just support one config at this point,
  572. // configuration # 1, which is Config Descriptor 0.
  573. dbg ("%s - Number of Interfaces = %d", __func__, dev->config->desc.bNumInterfaces);
  574. dbg ("%s - MAX Power = %d", __func__, dev->config->desc.bMaxPower*2);
  575. if (dev->config->desc.bNumInterfaces != 1) {
  576. dev_err (&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
  577. return -ENODEV;
  578. }
  579. return 0;
  580. }
  581. static int TIReadRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
  582. {
  583. int status;
  584. if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
  585. status = TIReadDownloadMemory (serial->serial->dev,
  586. start_address,
  587. length,
  588. serial->TI_I2C_Type,
  589. buffer);
  590. } else {
  591. status = TIReadBootMemory (serial,
  592. start_address,
  593. length,
  594. buffer);
  595. }
  596. return status;
  597. }
  598. static int TIWriteRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
  599. {
  600. if (serial->product_info.TiMode == TI_MODE_BOOT)
  601. return TIWriteBootMemory (serial,
  602. start_address,
  603. length,
  604. buffer);
  605. if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
  606. return TIWriteDownloadI2C (serial,
  607. start_address,
  608. length,
  609. serial->TI_I2C_Type,
  610. buffer);
  611. return -EINVAL;
  612. }
  613. /* Read a descriptor header from I2C based on type */
  614. static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type, struct ti_i2c_desc *rom_desc)
  615. {
  616. int start_address;
  617. int status;
  618. /* Search for requested descriptor in I2C */
  619. start_address = 2;
  620. do {
  621. status = TIReadRom (serial,
  622. start_address,
  623. sizeof(struct ti_i2c_desc),
  624. (__u8 *)rom_desc );
  625. if (status)
  626. return 0;
  627. if (rom_desc->Type == desc_type)
  628. return start_address;
  629. start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
  630. } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
  631. return 0;
  632. }
  633. /* Validate descriptor checksum */
  634. static int ValidChecksum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
  635. {
  636. __u16 i;
  637. __u8 cs = 0;
  638. for (i=0; i < rom_desc->Size; i++) {
  639. cs = (__u8)(cs + buffer[i]);
  640. }
  641. if (cs != rom_desc->CheckSum) {
  642. dbg ("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
  643. return -EINVAL;
  644. }
  645. return 0;
  646. }
  647. /* Make sure that the I2C image is good */
  648. static int TiValidateI2cImage (struct edgeport_serial *serial)
  649. {
  650. struct device *dev = &serial->serial->dev->dev;
  651. int status = 0;
  652. struct ti_i2c_desc *rom_desc;
  653. int start_address = 2;
  654. __u8 *buffer;
  655. __u16 ttype;
  656. rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
  657. if (!rom_desc) {
  658. dev_err (dev, "%s - out of memory\n", __func__);
  659. return -ENOMEM;
  660. }
  661. buffer = kmalloc (TI_MAX_I2C_SIZE, GFP_KERNEL);
  662. if (!buffer) {
  663. dev_err (dev, "%s - out of memory when allocating buffer\n", __func__);
  664. kfree (rom_desc);
  665. return -ENOMEM;
  666. }
  667. // Read the first byte (Signature0) must be 0x52 or 0x10
  668. status = TIReadRom (serial, 0, 1, buffer);
  669. if (status)
  670. goto ExitTiValidateI2cImage;
  671. if (*buffer != UMP5152 && *buffer != UMP3410) {
  672. dev_err (dev, "%s - invalid buffer signature\n", __func__);
  673. status = -ENODEV;
  674. goto ExitTiValidateI2cImage;
  675. }
  676. do {
  677. // Validate the I2C
  678. status = TIReadRom (serial,
  679. start_address,
  680. sizeof(struct ti_i2c_desc),
  681. (__u8 *)rom_desc);
  682. if (status)
  683. break;
  684. if ((start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size) > TI_MAX_I2C_SIZE) {
  685. status = -ENODEV;
  686. dbg ("%s - structure too big, erroring out.", __func__);
  687. break;
  688. }
  689. dbg ("%s Type = 0x%x", __func__, rom_desc->Type);
  690. // Skip type 2 record
  691. ttype = rom_desc->Type & 0x0f;
  692. if ( ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
  693. && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO ) {
  694. // Read the descriptor data
  695. status = TIReadRom(serial,
  696. start_address+sizeof(struct ti_i2c_desc),
  697. rom_desc->Size,
  698. buffer);
  699. if (status)
  700. break;
  701. status = ValidChecksum(rom_desc, buffer);
  702. if (status)
  703. break;
  704. }
  705. start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
  706. } while ((rom_desc->Type != I2C_DESC_TYPE_ION) && (start_address < TI_MAX_I2C_SIZE));
  707. if ((rom_desc->Type != I2C_DESC_TYPE_ION) || (start_address > TI_MAX_I2C_SIZE))
  708. status = -ENODEV;
  709. ExitTiValidateI2cImage:
  710. kfree (buffer);
  711. kfree (rom_desc);
  712. return status;
  713. }
  714. static int TIReadManufDescriptor (struct edgeport_serial *serial, __u8 *buffer)
  715. {
  716. int status;
  717. int start_address;
  718. struct ti_i2c_desc *rom_desc;
  719. struct edge_ti_manuf_descriptor *desc;
  720. rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
  721. if (!rom_desc) {
  722. dev_err (&serial->serial->dev->dev, "%s - out of memory\n", __func__);
  723. return -ENOMEM;
  724. }
  725. start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_ION, rom_desc);
  726. if (!start_address) {
  727. dbg ("%s - Edge Descriptor not found in I2C", __func__);
  728. status = -ENODEV;
  729. goto exit;
  730. }
  731. // Read the descriptor data
  732. status = TIReadRom (serial,
  733. start_address+sizeof(struct ti_i2c_desc),
  734. rom_desc->Size,
  735. buffer);
  736. if (status)
  737. goto exit;
  738. status = ValidChecksum(rom_desc, buffer);
  739. desc = (struct edge_ti_manuf_descriptor *)buffer;
  740. dbg ( "%s - IonConfig 0x%x", __func__, desc->IonConfig );
  741. dbg ( "%s - Version %d", __func__, desc->Version );
  742. dbg ( "%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev );
  743. dbg ( "%s - NumPorts %d", __func__, desc->NumPorts );
  744. dbg ( "%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts );
  745. dbg ( "%s - TotalPorts %d", __func__, desc->TotalPorts );
  746. exit:
  747. kfree (rom_desc);
  748. return status;
  749. }
  750. /* Build firmware header used for firmware update */
  751. static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
  752. {
  753. __u8 *buffer;
  754. int buffer_size;
  755. int i;
  756. __u8 cs = 0;
  757. struct ti_i2c_desc *i2c_header;
  758. struct ti_i2c_image_header *img_header;
  759. struct ti_i2c_firmware_rec *firmware_rec;
  760. // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
  761. // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
  762. // will download the latest firmware (padded to 15.5k) into the UMP ram.
  763. // And finally when the device comes back up in download mode the driver will cause
  764. // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
  765. // the record type from 0xf2 to 0x02.
  766. // Allocate a 15.5k buffer + 2 bytes for version number (Firmware Record)
  767. buffer_size = (((1024 * 16) - 512 )+ sizeof(struct ti_i2c_firmware_rec));
  768. buffer = kmalloc (buffer_size, GFP_KERNEL);
  769. if (!buffer) {
  770. dev_err (dev, "%s - out of memory\n", __func__);
  771. return -ENOMEM;
  772. }
  773. // Set entire image of 0xffs
  774. memset (buffer, 0xff, buffer_size);
  775. // Copy version number into firmware record
  776. firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
  777. firmware_rec->Ver_Major = OperationalCodeImageVersion.MajorVersion;
  778. firmware_rec->Ver_Minor = OperationalCodeImageVersion.MinorVersion;
  779. // Pointer to fw_down memory image
  780. img_header = (struct ti_i2c_image_header *)&PagableOperationalCodeImage[0];
  781. memcpy (buffer + sizeof(struct ti_i2c_firmware_rec),
  782. &PagableOperationalCodeImage[sizeof(struct ti_i2c_image_header)],
  783. le16_to_cpu(img_header->Length));
  784. for (i=0; i < buffer_size; i++) {
  785. cs = (__u8)(cs + buffer[i]);
  786. }
  787. kfree (buffer);
  788. // Build new header
  789. i2c_header = (struct ti_i2c_desc *)header;
  790. firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
  791. i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
  792. i2c_header->Size = (__u16)buffer_size;
  793. i2c_header->CheckSum = cs;
  794. firmware_rec->Ver_Major = OperationalCodeImageVersion.MajorVersion;
  795. firmware_rec->Ver_Minor = OperationalCodeImageVersion.MinorVersion;
  796. return 0;
  797. }
  798. /* Try to figure out what type of I2c we have */
  799. static int TIGetI2cTypeInBootMode (struct edgeport_serial *serial)
  800. {
  801. int status;
  802. __u8 data;
  803. // Try to read type 2
  804. status = TIReadVendorRequestSync (serial->serial->dev,
  805. UMPC_MEMORY_READ, // Request
  806. DTK_ADDR_SPACE_I2C_TYPE_II, // wValue (Address type)
  807. 0, // wIndex
  808. &data, // TransferBuffer
  809. 0x01); // TransferBufferLength
  810. if (status)
  811. dbg ("%s - read 2 status error = %d", __func__, status);
  812. else
  813. dbg ("%s - read 2 data = 0x%x", __func__, data);
  814. if ((!status) && (data == UMP5152 || data == UMP3410)) {
  815. dbg ("%s - ROM_TYPE_II", __func__);
  816. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  817. return 0;
  818. }
  819. // Try to read type 3
  820. status = TIReadVendorRequestSync (serial->serial->dev,
  821. UMPC_MEMORY_READ, // Request
  822. DTK_ADDR_SPACE_I2C_TYPE_III, // wValue (Address type)
  823. 0, // wIndex
  824. &data, // TransferBuffer
  825. 0x01); // TransferBufferLength
  826. if (status)
  827. dbg ("%s - read 3 status error = %d", __func__, status);
  828. else
  829. dbg ("%s - read 2 data = 0x%x", __func__, data);
  830. if ((!status) && (data == UMP5152 || data == UMP3410)) {
  831. dbg ("%s - ROM_TYPE_III", __func__);
  832. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
  833. return 0;
  834. }
  835. dbg ("%s - Unknown", __func__);
  836. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  837. return -ENODEV;
  838. }
  839. static int TISendBulkTransferSync (struct usb_serial *serial, void *buffer, int length, int *num_sent)
  840. {
  841. int status;
  842. status = usb_bulk_msg (serial->dev,
  843. usb_sndbulkpipe(serial->dev,
  844. serial->port[0]->bulk_out_endpointAddress),
  845. buffer,
  846. length,
  847. num_sent,
  848. 1000);
  849. return status;
  850. }
  851. /* Download given firmware image to the device (IN BOOT MODE) */
  852. static int TIDownloadCodeImage (struct edgeport_serial *serial, __u8 *image, int image_length)
  853. {
  854. int status = 0;
  855. int pos;
  856. int transfer;
  857. int done;
  858. // Transfer firmware image
  859. for (pos = 0; pos < image_length; ) {
  860. // Read the next buffer from file
  861. transfer = image_length - pos;
  862. if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
  863. transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
  864. // Transfer data
  865. status = TISendBulkTransferSync (serial->serial, &image[pos], transfer, &done);
  866. if (status)
  867. break;
  868. // Advance buffer pointer
  869. pos += done;
  870. }
  871. return status;
  872. }
  873. // FIXME!!!
  874. static int TIConfigureBootDevice (struct usb_device *dev)
  875. {
  876. return 0;
  877. }
  878. /**
  879. * DownloadTIFirmware - Download run-time operating firmware to the TI5052
  880. *
  881. * This routine downloads the main operating code into the TI5052, using the
  882. * boot code already burned into E2PROM or ROM.
  883. */
  884. static int TIDownloadFirmware (struct edgeport_serial *serial)
  885. {
  886. struct device *dev = &serial->serial->dev->dev;
  887. int status = 0;
  888. int start_address;
  889. struct edge_ti_manuf_descriptor *ti_manuf_desc;
  890. struct usb_interface_descriptor *interface;
  891. int download_cur_ver;
  892. int download_new_ver;
  893. /* This routine is entered by both the BOOT mode and the Download mode
  894. * We can determine which code is running by the reading the config
  895. * descriptor and if we have only one bulk pipe it is in boot mode
  896. */
  897. serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
  898. /* Default to type 2 i2c */
  899. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  900. status = TIChooseConfiguration (serial->serial->dev);
  901. if (status)
  902. return status;
  903. interface = &serial->serial->interface->cur_altsetting->desc;
  904. if (!interface) {
  905. dev_err (dev, "%s - no interface set, error!\n", __func__);
  906. return -ENODEV;
  907. }
  908. // Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
  909. // if we have more than one endpoint we are definitely in download mode
  910. if (interface->bNumEndpoints > 1)
  911. serial->product_info.TiMode = TI_MODE_DOWNLOAD;
  912. else
  913. // Otherwise we will remain in configuring mode
  914. serial->product_info.TiMode = TI_MODE_CONFIGURING;
  915. // Save Download Version Number
  916. OperationalCodeImageVersion.MajorVersion = PagableOperationalCodeImageVersion.MajorVersion;
  917. OperationalCodeImageVersion.MinorVersion = PagableOperationalCodeImageVersion.MinorVersion;
  918. OperationalCodeImageVersion.BuildNumber = PagableOperationalCodeImageVersion.BuildNumber;
  919. /********************************************************************/
  920. /* Download Mode */
  921. /********************************************************************/
  922. if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
  923. struct ti_i2c_desc *rom_desc;
  924. dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
  925. status = TiValidateI2cImage (serial);
  926. if (status) {
  927. dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
  928. return status;
  929. }
  930. /* Validate Hardware version number
  931. * Read Manufacturing Descriptor from TI Based Edgeport
  932. */
  933. ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
  934. if (!ti_manuf_desc) {
  935. dev_err (dev, "%s - out of memory.\n", __func__);
  936. return -ENOMEM;
  937. }
  938. status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
  939. if (status) {
  940. kfree (ti_manuf_desc);
  941. return status;
  942. }
  943. // Check version number of ION descriptor
  944. if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
  945. dbg ( "%s - Wrong CPU Rev %d (Must be 2)", __func__,
  946. TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
  947. kfree (ti_manuf_desc);
  948. return -EINVAL;
  949. }
  950. rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
  951. if (!rom_desc) {
  952. dev_err (dev, "%s - out of memory.\n", __func__);
  953. kfree (ti_manuf_desc);
  954. return -ENOMEM;
  955. }
  956. // Search for type 2 record (firmware record)
  957. if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc)) != 0) {
  958. struct ti_i2c_firmware_rec *firmware_version;
  959. __u8 record;
  960. dbg ("%s - Found Type FIRMWARE (Type 2) record", __func__);
  961. firmware_version = kmalloc (sizeof (*firmware_version), GFP_KERNEL);
  962. if (!firmware_version) {
  963. dev_err (dev, "%s - out of memory.\n", __func__);
  964. kfree (rom_desc);
  965. kfree (ti_manuf_desc);
  966. return -ENOMEM;
  967. }
  968. // Validate version number
  969. // Read the descriptor data
  970. status = TIReadRom (serial,
  971. start_address+sizeof(struct ti_i2c_desc),
  972. sizeof(struct ti_i2c_firmware_rec),
  973. (__u8 *)firmware_version);
  974. if (status) {
  975. kfree (firmware_version);
  976. kfree (rom_desc);
  977. kfree (ti_manuf_desc);
  978. return status;
  979. }
  980. // Check version number of download with current version in I2c
  981. download_cur_ver = (firmware_version->Ver_Major << 8) +
  982. (firmware_version->Ver_Minor);
  983. download_new_ver = (OperationalCodeImageVersion.MajorVersion << 8) +
  984. (OperationalCodeImageVersion.MinorVersion);
  985. dbg ("%s - >>>Firmware Versions Device %d.%d Driver %d.%d",
  986. __func__,
  987. firmware_version->Ver_Major,
  988. firmware_version->Ver_Minor,
  989. OperationalCodeImageVersion.MajorVersion,
  990. OperationalCodeImageVersion.MinorVersion);
  991. // Check if we have an old version in the I2C and update if necessary
  992. if (download_cur_ver != download_new_ver) {
  993. dbg ("%s - Update I2C Download from %d.%d to %d.%d",
  994. __func__,
  995. firmware_version->Ver_Major,
  996. firmware_version->Ver_Minor,
  997. OperationalCodeImageVersion.MajorVersion,
  998. OperationalCodeImageVersion.MinorVersion);
  999. // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
  1000. // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
  1001. // will download the latest firmware (padded to 15.5k) into the UMP ram.
  1002. // And finally when the device comes back up in download mode the driver will cause
  1003. // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
  1004. // the record type from 0xf2 to 0x02.
  1005. record = I2C_DESC_TYPE_FIRMWARE_BLANK;
  1006. // Change the I2C Firmware record type to 0xf2 to trigger an update
  1007. status = TIWriteRom (serial,
  1008. start_address,
  1009. sizeof(record),
  1010. &record);
  1011. if (status) {
  1012. kfree (firmware_version);
  1013. kfree (rom_desc);
  1014. kfree (ti_manuf_desc);
  1015. return status;
  1016. }
  1017. // verify the write -- must do this in order for write to
  1018. // complete before we do the hardware reset
  1019. status = TIReadRom (serial,
  1020. start_address,
  1021. sizeof(record),
  1022. &record);
  1023. if (status) {
  1024. kfree (firmware_version);
  1025. kfree (rom_desc);
  1026. kfree (ti_manuf_desc);
  1027. return status;
  1028. }
  1029. if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
  1030. dev_err (dev, "%s - error resetting device\n", __func__);
  1031. kfree (firmware_version);
  1032. kfree (rom_desc);
  1033. kfree (ti_manuf_desc);
  1034. return -ENODEV;
  1035. }
  1036. dbg ("%s - HARDWARE RESET", __func__);
  1037. // Reset UMP -- Back to BOOT MODE
  1038. status = TISendVendorRequestSync (serial->serial->dev,
  1039. UMPC_HARDWARE_RESET, // Request
  1040. 0, // wValue
  1041. 0, // wIndex
  1042. NULL, // TransferBuffer
  1043. 0); // TransferBufferLength
  1044. dbg ( "%s - HARDWARE RESET return %d", __func__, status);
  1045. /* return an error on purpose. */
  1046. kfree (firmware_version);
  1047. kfree (rom_desc);
  1048. kfree (ti_manuf_desc);
  1049. return -ENODEV;
  1050. }
  1051. kfree (firmware_version);
  1052. }
  1053. // Search for type 0xF2 record (firmware blank record)
  1054. else if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
  1055. #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + sizeof(struct ti_i2c_firmware_rec))
  1056. __u8 *header;
  1057. __u8 *vheader;
  1058. header = kmalloc (HEADER_SIZE, GFP_KERNEL);
  1059. if (!header) {
  1060. dev_err (dev, "%s - out of memory.\n", __func__);
  1061. kfree (rom_desc);
  1062. kfree (ti_manuf_desc);
  1063. return -ENOMEM;
  1064. }
  1065. vheader = kmalloc (HEADER_SIZE, GFP_KERNEL);
  1066. if (!vheader) {
  1067. dev_err (dev, "%s - out of memory.\n", __func__);
  1068. kfree (header);
  1069. kfree (rom_desc);
  1070. kfree (ti_manuf_desc);
  1071. return -ENOMEM;
  1072. }
  1073. dbg ("%s - Found Type BLANK FIRMWARE (Type F2) record", __func__);
  1074. // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
  1075. // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
  1076. // will download the latest firmware (padded to 15.5k) into the UMP ram.
  1077. // And finally when the device comes back up in download mode the driver will cause
  1078. // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
  1079. // the record type from 0xf2 to 0x02.
  1080. status = BuildI2CFirmwareHeader(header, dev);
  1081. if (status) {
  1082. kfree (vheader);
  1083. kfree (header);
  1084. kfree (rom_desc);
  1085. kfree (ti_manuf_desc);
  1086. return status;
  1087. }
  1088. // Update I2C with type 0xf2 record with correct size and checksum
  1089. status = TIWriteRom (serial,
  1090. start_address,
  1091. HEADER_SIZE,
  1092. header);
  1093. if (status) {
  1094. kfree (vheader);
  1095. kfree (header);
  1096. kfree (rom_desc);
  1097. kfree (ti_manuf_desc);
  1098. return status;
  1099. }
  1100. // verify the write -- must do this in order for write to
  1101. // complete before we do the hardware reset
  1102. status = TIReadRom (serial,
  1103. start_address,
  1104. HEADER_SIZE,
  1105. vheader);
  1106. if (status) {
  1107. dbg ("%s - can't read header back", __func__);
  1108. kfree (vheader);
  1109. kfree (header);
  1110. kfree (rom_desc);
  1111. kfree (ti_manuf_desc);
  1112. return status;
  1113. }
  1114. if (memcmp(vheader, header, HEADER_SIZE)) {
  1115. dbg ("%s - write download record failed", __func__);
  1116. kfree (vheader);
  1117. kfree (header);
  1118. kfree (rom_desc);
  1119. kfree (ti_manuf_desc);
  1120. return status;
  1121. }
  1122. kfree (vheader);
  1123. kfree (header);
  1124. dbg ("%s - Start firmware update", __func__);
  1125. // Tell firmware to copy download image into I2C
  1126. status = TISendVendorRequestSync (serial->serial->dev,
  1127. UMPC_COPY_DNLD_TO_I2C, // Request
  1128. 0, // wValue
  1129. 0, // wIndex
  1130. NULL, // TransferBuffer
  1131. 0); // TransferBufferLength
  1132. dbg ("%s - Update complete 0x%x", __func__, status);
  1133. if (status) {
  1134. dev_err (dev, "%s - UMPC_COPY_DNLD_TO_I2C failed\n", __func__);
  1135. kfree (rom_desc);
  1136. kfree (ti_manuf_desc);
  1137. return status;
  1138. }
  1139. }
  1140. // The device is running the download code
  1141. kfree (rom_desc);
  1142. kfree (ti_manuf_desc);
  1143. return 0;
  1144. }
  1145. /********************************************************************/
  1146. /* Boot Mode */
  1147. /********************************************************************/
  1148. dbg("%s - RUNNING IN BOOT MODE", __func__);
  1149. // Configure the TI device so we can use the BULK pipes for download
  1150. status = TIConfigureBootDevice (serial->serial->dev);
  1151. if (status)
  1152. return status;
  1153. if (le16_to_cpu(serial->serial->dev->descriptor.idVendor) != USB_VENDOR_ID_ION) {
  1154. dbg ("%s - VID = 0x%x", __func__,
  1155. le16_to_cpu(serial->serial->dev->descriptor.idVendor));
  1156. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  1157. goto StayInBootMode;
  1158. }
  1159. // We have an ION device (I2c Must be programmed)
  1160. // Determine I2C image type
  1161. if (TIGetI2cTypeInBootMode(serial)) {
  1162. goto StayInBootMode;
  1163. }
  1164. // Registry variable set?
  1165. if (TIStayInBootMode) {
  1166. dbg ("%s - TIStayInBootMode", __func__);
  1167. goto StayInBootMode;
  1168. }
  1169. // Check for ION Vendor ID and that the I2C is valid
  1170. if (!TiValidateI2cImage(serial)) {
  1171. struct ti_i2c_image_header *header;
  1172. int i;
  1173. __u8 cs = 0;
  1174. __u8 *buffer;
  1175. int buffer_size;
  1176. /* Validate Hardware version number
  1177. * Read Manufacturing Descriptor from TI Based Edgeport
  1178. */
  1179. ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
  1180. if (!ti_manuf_desc) {
  1181. dev_err (dev, "%s - out of memory.\n", __func__);
  1182. return -ENOMEM;
  1183. }
  1184. status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
  1185. if (status) {
  1186. kfree (ti_manuf_desc);
  1187. goto StayInBootMode;
  1188. }
  1189. // Check for version 2
  1190. if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
  1191. dbg ("%s - Wrong CPU Rev %d (Must be 2)", __func__,
  1192. TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
  1193. kfree (ti_manuf_desc);
  1194. goto StayInBootMode;
  1195. }
  1196. kfree (ti_manuf_desc);
  1197. // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
  1198. // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
  1199. // will download the latest firmware (padded to 15.5k) into the UMP ram.
  1200. // And finally when the device comes back up in download mode the driver will cause
  1201. // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
  1202. // the record type from 0xf2 to 0x02.
  1203. /*
  1204. * Do we really have to copy the whole firmware image,
  1205. * or could we do this in place!
  1206. */
  1207. // Allocate a 15.5k buffer + 3 byte header
  1208. buffer_size = (((1024 * 16) - 512) + sizeof(struct ti_i2c_image_header));
  1209. buffer = kmalloc (buffer_size, GFP_KERNEL);
  1210. if (!buffer) {
  1211. dev_err (dev, "%s - out of memory\n", __func__);
  1212. return -ENOMEM;
  1213. }
  1214. // Initialize the buffer to 0xff (pad the buffer)
  1215. memset (buffer, 0xff, buffer_size);
  1216. memcpy (buffer, &PagableOperationalCodeImage[0], PagableOperationalCodeSize);
  1217. for(i = sizeof(struct ti_i2c_image_header); i < buffer_size; i++) {
  1218. cs = (__u8)(cs + buffer[i]);
  1219. }
  1220. header = (struct ti_i2c_image_header *)buffer;
  1221. // update length and checksum after padding
  1222. header->Length = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_i2c_image_header)));
  1223. header->CheckSum = cs;
  1224. // Download the operational code
  1225. dbg ("%s - Downloading operational code image (TI UMP)", __func__);
  1226. status = TIDownloadCodeImage (serial, buffer, buffer_size);
  1227. kfree (buffer);
  1228. if (status) {
  1229. dbg ("%s - Error downloading operational code image", __func__);
  1230. return status;
  1231. }
  1232. // Device will reboot
  1233. serial->product_info.TiMode = TI_MODE_TRANSITIONING;
  1234. dbg ("%s - Download successful -- Device rebooting...", __func__);
  1235. /* return an error on purpose */
  1236. return -ENODEV;
  1237. }
  1238. StayInBootMode:
  1239. // Eprom is invalid or blank stay in boot mode
  1240. dbg("%s - STAYING IN BOOT MODE", __func__);
  1241. serial->product_info.TiMode = TI_MODE_BOOT;
  1242. return 0;
  1243. }
  1244. static int TISetDtr (struct edgeport_port *port)
  1245. {
  1246. int port_number = port->port->number - port->port->serial->minor;
  1247. dbg ("%s", __func__);
  1248. port->shadow_mcr |= MCR_DTR;
  1249. return TIWriteCommandSync (port->port->serial->dev,
  1250. UMPC_SET_CLR_DTR,
  1251. (__u8)(UMPM_UART1_PORT + port_number),
  1252. 1, /* set */
  1253. NULL,
  1254. 0);
  1255. }
  1256. static int TIClearDtr (struct edgeport_port *port)
  1257. {
  1258. int port_number = port->port->number - port->port->serial->minor;
  1259. dbg ("%s", __func__);
  1260. port->shadow_mcr &= ~MCR_DTR;
  1261. return TIWriteCommandSync (port->port->serial->dev,
  1262. UMPC_SET_CLR_DTR,
  1263. (__u8)(UMPM_UART1_PORT + port_number),
  1264. 0, /* clear */
  1265. NULL,
  1266. 0);
  1267. }
  1268. static int TISetRts (struct edgeport_port *port)
  1269. {
  1270. int port_number = port->port->number - port->port->serial->minor;
  1271. dbg ("%s", __func__);
  1272. port->shadow_mcr |= MCR_RTS;
  1273. return TIWriteCommandSync (port->port->serial->dev,
  1274. UMPC_SET_CLR_RTS,
  1275. (__u8)(UMPM_UART1_PORT + port_number),
  1276. 1, /* set */
  1277. NULL,
  1278. 0);
  1279. }
  1280. static int TIClearRts (struct edgeport_port *port)
  1281. {
  1282. int port_number = port->port->number - port->port->serial->minor;
  1283. dbg ("%s", __func__);
  1284. port->shadow_mcr &= ~MCR_RTS;
  1285. return TIWriteCommandSync (port->port->serial->dev,
  1286. UMPC_SET_CLR_RTS,
  1287. (__u8)(UMPM_UART1_PORT + port_number),
  1288. 0, /* clear */
  1289. NULL,
  1290. 0);
  1291. }
  1292. static int TISetLoopBack (struct edgeport_port *port)
  1293. {
  1294. int port_number = port->port->number - port->port->serial->minor;
  1295. dbg ("%s", __func__);
  1296. return TIWriteCommandSync (port->port->serial->dev,
  1297. UMPC_SET_CLR_LOOPBACK,
  1298. (__u8)(UMPM_UART1_PORT + port_number),
  1299. 1, /* set */
  1300. NULL,
  1301. 0);
  1302. }
  1303. static int TIClearLoopBack (struct edgeport_port *port)
  1304. {
  1305. int port_number = port->port->number - port->port->serial->minor;
  1306. dbg ("%s", __func__);
  1307. return TIWriteCommandSync (port->port->serial->dev,
  1308. UMPC_SET_CLR_LOOPBACK,
  1309. (__u8)(UMPM_UART1_PORT + port_number),
  1310. 0, /* clear */
  1311. NULL,
  1312. 0);
  1313. }
  1314. static int TISetBreak (struct edgeport_port *port)
  1315. {
  1316. int port_number = port->port->number - port->port->serial->minor;
  1317. dbg ("%s", __func__);
  1318. return TIWriteCommandSync (port->port->serial->dev,
  1319. UMPC_SET_CLR_BREAK,
  1320. (__u8)(UMPM_UART1_PORT + port_number),
  1321. 1, /* set */
  1322. NULL,
  1323. 0);
  1324. }
  1325. static int TIClearBreak (struct edgeport_port *port)
  1326. {
  1327. int port_number = port->port->number - port->port->serial->minor;
  1328. dbg ("%s", __func__);
  1329. return TIWriteCommandSync (port->port->serial->dev,
  1330. UMPC_SET_CLR_BREAK,
  1331. (__u8)(UMPM_UART1_PORT + port_number),
  1332. 0, /* clear */
  1333. NULL,
  1334. 0);
  1335. }
  1336. static int TIRestoreMCR (struct edgeport_port *port, __u8 mcr)
  1337. {
  1338. int status = 0;
  1339. dbg ("%s - %x", __func__, mcr);
  1340. if (mcr & MCR_DTR)
  1341. status = TISetDtr (port);
  1342. else
  1343. status = TIClearDtr (port);
  1344. if (status)
  1345. return status;
  1346. if (mcr & MCR_RTS)
  1347. status = TISetRts (port);
  1348. else
  1349. status = TIClearRts (port);
  1350. if (status)
  1351. return status;
  1352. if (mcr & MCR_LOOPBACK)
  1353. status = TISetLoopBack (port);
  1354. else
  1355. status = TIClearLoopBack (port);
  1356. return status;
  1357. }
  1358. /* Convert TI LSR to standard UART flags */
  1359. static __u8 MapLineStatus (__u8 ti_lsr)
  1360. {
  1361. __u8 lsr = 0;
  1362. #define MAP_FLAG(flagUmp, flagUart) \
  1363. if (ti_lsr & flagUmp) \
  1364. lsr |= flagUart;
  1365. MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
  1366. MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
  1367. MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
  1368. MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
  1369. MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* receive data available */
  1370. MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* transmit holding register empty */
  1371. #undef MAP_FLAG
  1372. return lsr;
  1373. }
  1374. static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr)
  1375. {
  1376. struct async_icount *icount;
  1377. struct tty_struct *tty;
  1378. dbg ("%s - %02x", __func__, msr);
  1379. if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
  1380. icount = &edge_port->icount;
  1381. /* update input line counters */
  1382. if (msr & EDGEPORT_MSR_DELTA_CTS)
  1383. icount->cts++;
  1384. if (msr & EDGEPORT_MSR_DELTA_DSR)
  1385. icount->dsr++;
  1386. if (msr & EDGEPORT_MSR_DELTA_CD)
  1387. icount->dcd++;
  1388. if (msr & EDGEPORT_MSR_DELTA_RI)
  1389. icount->rng++;
  1390. wake_up_interruptible (&edge_port->delta_msr_wait);
  1391. }
  1392. /* Save the new modem status */
  1393. edge_port->shadow_msr = msr & 0xf0;
  1394. tty = edge_port->port->tty;
  1395. /* handle CTS flow control */
  1396. if (tty && C_CRTSCTS(tty)) {
  1397. if (msr & EDGEPORT_MSR_CTS) {
  1398. tty->hw_stopped = 0;
  1399. tty_wakeup(tty);
  1400. } else {
  1401. tty->hw_stopped = 1;
  1402. }
  1403. }
  1404. return;
  1405. }
  1406. static void handle_new_lsr (struct edgeport_port *edge_port, int lsr_data, __u8 lsr, __u8 data)
  1407. {
  1408. struct async_icount *icount;
  1409. __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
  1410. dbg ("%s - %02x", __func__, new_lsr);
  1411. edge_port->shadow_lsr = lsr;
  1412. if (new_lsr & LSR_BREAK) {
  1413. /*
  1414. * Parity and Framing errors only count if they
  1415. * occur exclusive of a break being received.
  1416. */
  1417. new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
  1418. }
  1419. /* Place LSR data byte into Rx buffer */
  1420. if (lsr_data && edge_port->port->tty)
  1421. edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
  1422. /* update input line counters */
  1423. icount = &edge_port->icount;
  1424. if (new_lsr & LSR_BREAK)
  1425. icount->brk++;
  1426. if (new_lsr & LSR_OVER_ERR)
  1427. icount->overrun++;
  1428. if (new_lsr & LSR_PAR_ERR)
  1429. icount->parity++;
  1430. if (new_lsr & LSR_FRM_ERR)
  1431. icount->frame++;
  1432. }
  1433. static void edge_interrupt_callback (struct urb *urb)
  1434. {
  1435. struct edgeport_serial *edge_serial = urb->context;
  1436. struct usb_serial_port *port;
  1437. struct edgeport_port *edge_port;
  1438. unsigned char *data = urb->transfer_buffer;
  1439. int length = urb->actual_length;
  1440. int port_number;
  1441. int function;
  1442. int retval;
  1443. __u8 lsr;
  1444. __u8 msr;
  1445. int status = urb->status;
  1446. dbg("%s", __func__);
  1447. switch (status) {
  1448. case 0:
  1449. /* success */
  1450. break;
  1451. case -ECONNRESET:
  1452. case -ENOENT:
  1453. case -ESHUTDOWN:
  1454. /* this urb is terminated, clean up */
  1455. dbg("%s - urb shutting down with status: %d",
  1456. __func__, status);
  1457. return;
  1458. default:
  1459. dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
  1460. "%d\n", __func__, status);
  1461. goto exit;
  1462. }
  1463. if (!length) {
  1464. dbg ("%s - no data in urb", __func__);
  1465. goto exit;
  1466. }
  1467. usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __func__, length, data);
  1468. if (length != 2) {
  1469. dbg ("%s - expecting packet of size 2, got %d", __func__, length);
  1470. goto exit;
  1471. }
  1472. port_number = TIUMP_GET_PORT_FROM_CODE (data[0]);
  1473. function = TIUMP_GET_FUNC_FROM_CODE (data[0]);
  1474. dbg ("%s - port_number %d, function %d, info 0x%x",
  1475. __func__, port_number, function, data[1]);
  1476. port = edge_serial->serial->port[port_number];
  1477. edge_port = usb_get_serial_port_data(port);
  1478. if (!edge_port) {
  1479. dbg ("%s - edge_port not found", __func__);
  1480. return;
  1481. }
  1482. switch (function) {
  1483. case TIUMP_INTERRUPT_CODE_LSR:
  1484. lsr = MapLineStatus(data[1]);
  1485. if (lsr & UMP_UART_LSR_DATA_MASK) {
  1486. /* Save the LSR event for bulk read completion routine */
  1487. dbg ("%s - LSR Event Port %u LSR Status = %02x",
  1488. __func__, port_number, lsr);
  1489. edge_port->lsr_event = 1;
  1490. edge_port->lsr_mask = lsr;
  1491. } else {
  1492. dbg ("%s - ===== Port %d LSR Status = %02x ======",
  1493. __func__, port_number, lsr);
  1494. handle_new_lsr (edge_port, 0, lsr, 0);
  1495. }
  1496. break;
  1497. case TIUMP_INTERRUPT_CODE_MSR: // MSR
  1498. /* Copy MSR from UMP */
  1499. msr = data[1];
  1500. dbg ("%s - ===== Port %u MSR Status = %02x ======\n",
  1501. __func__, port_number, msr);
  1502. handle_new_msr (edge_port, msr);
  1503. break;
  1504. default:
  1505. dev_err (&urb->dev->dev, "%s - Unknown Interrupt code from UMP %x\n",
  1506. __func__, data[1]);
  1507. break;
  1508. }
  1509. exit:
  1510. retval = usb_submit_urb (urb, GFP_ATOMIC);
  1511. if (retval)
  1512. dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
  1513. __func__, retval);
  1514. }
  1515. static void edge_bulk_in_callback (struct urb *urb)
  1516. {
  1517. struct edgeport_port *edge_port = urb->context;
  1518. unsigned char *data = urb->transfer_buffer;
  1519. struct tty_struct *tty;
  1520. int retval = 0;
  1521. int port_number;
  1522. int status = urb->status;
  1523. dbg("%s", __func__);
  1524. switch (status) {
  1525. case 0:
  1526. /* success */
  1527. break;
  1528. case -ECONNRESET:
  1529. case -ENOENT:
  1530. case -ESHUTDOWN:
  1531. /* this urb is terminated, clean up */
  1532. dbg("%s - urb shutting down with status: %d",
  1533. __func__, status);
  1534. return;
  1535. default:
  1536. dev_err (&urb->dev->dev,"%s - nonzero read bulk status received: %d\n",
  1537. __func__, status);
  1538. }
  1539. if (status == -EPIPE)
  1540. goto exit;
  1541. if (status) {
  1542. dev_err(&urb->dev->dev,"%s - stopping read!\n", __func__);
  1543. return;
  1544. }
  1545. port_number = edge_port->port->number - edge_port->port->serial->minor;
  1546. if (edge_port->lsr_event) {
  1547. edge_port->lsr_event = 0;
  1548. dbg ("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
  1549. __func__, port_number, edge_port->lsr_mask, *data);
  1550. handle_new_lsr (edge_port, 1, edge_port->lsr_mask, *data);
  1551. /* Adjust buffer length/pointer */
  1552. --urb->actual_length;
  1553. ++data;
  1554. }
  1555. tty = edge_port->port->tty;
  1556. if (tty && urb->actual_length) {
  1557. usb_serial_debug_data(debug, &edge_port->port->dev, __func__, urb->actual_length, data);
  1558. if (edge_port->close_pending) {
  1559. dbg ("%s - close is pending, dropping data on the floor.", __func__);
  1560. } else {
  1561. edge_tty_recv(&edge_port->port->dev, tty, data, urb->actual_length);
  1562. }
  1563. edge_port->icount.rx += urb->actual_length;
  1564. }
  1565. exit:
  1566. /* continue read unless stopped */
  1567. spin_lock(&edge_port->ep_lock);
  1568. if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
  1569. urb->dev = edge_port->port->serial->dev;
  1570. retval = usb_submit_urb(urb, GFP_ATOMIC);
  1571. } else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
  1572. edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
  1573. }
  1574. spin_unlock(&edge_port->ep_lock);
  1575. if (retval)
  1576. dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
  1577. __func__, retval);
  1578. }
  1579. static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
  1580. {
  1581. int cnt;
  1582. do {
  1583. cnt = tty_buffer_request_room(tty, length);
  1584. if (cnt < length) {
  1585. dev_err(dev, "%s - dropping data, %d bytes lost\n",
  1586. __func__, length - cnt);
  1587. if(cnt == 0)
  1588. break;
  1589. }
  1590. tty_insert_flip_string(tty, data, cnt);
  1591. data += cnt;
  1592. length -= cnt;
  1593. } while (length > 0);
  1594. tty_flip_buffer_push(tty);
  1595. }
  1596. static void edge_bulk_out_callback (struct urb *urb)
  1597. {
  1598. struct usb_serial_port *port = urb->context;
  1599. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1600. int status = urb->status;
  1601. dbg ("%s - port %d", __func__, port->number);
  1602. edge_port->ep_write_urb_in_use = 0;
  1603. switch (status) {
  1604. case 0:
  1605. /* success */
  1606. break;
  1607. case -ECONNRESET:
  1608. case -ENOENT:
  1609. case -ESHUTDOWN:
  1610. /* this urb is terminated, clean up */
  1611. dbg("%s - urb shutting down with status: %d",
  1612. __func__, status);
  1613. return;
  1614. default:
  1615. dev_err(&urb->dev->dev, "%s - nonzero write bulk status "
  1616. "received: %d\n", __func__, status);
  1617. }
  1618. /* send any buffered data */
  1619. edge_send(port);
  1620. }
  1621. static int edge_open (struct usb_serial_port *port, struct file * filp)
  1622. {
  1623. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1624. struct edgeport_serial *edge_serial;
  1625. struct usb_device *dev;
  1626. struct urb *urb;
  1627. int port_number;
  1628. int status;
  1629. u16 open_settings;
  1630. u8 transaction_timeout;
  1631. dbg("%s - port %d", __func__, port->number);
  1632. if (edge_port == NULL)
  1633. return -ENODEV;
  1634. port->tty->low_latency = low_latency;
  1635. port_number = port->number - port->serial->minor;
  1636. switch (port_number) {
  1637. case 0:
  1638. edge_port->uart_base = UMPMEM_BASE_UART1;
  1639. edge_port->dma_address = UMPD_OEDB1_ADDRESS;
  1640. break;
  1641. case 1:
  1642. edge_port->uart_base = UMPMEM_BASE_UART2;
  1643. edge_port->dma_address = UMPD_OEDB2_ADDRESS;
  1644. break;
  1645. default:
  1646. dev_err (&port->dev, "Unknown port number!!!\n");
  1647. return -ENODEV;
  1648. }
  1649. dbg ("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
  1650. __func__, port_number, edge_port->uart_base, edge_port->dma_address);
  1651. dev = port->serial->dev;
  1652. memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
  1653. init_waitqueue_head (&edge_port->delta_msr_wait);
  1654. /* turn off loopback */
  1655. status = TIClearLoopBack (edge_port);
  1656. if (status) {
  1657. dev_err(&port->dev,"%s - cannot send clear loopback command, %d\n",
  1658. __func__, status);
  1659. return status;
  1660. }
  1661. /* set up the port settings */
  1662. edge_set_termios (port, port->tty->termios);
  1663. /* open up the port */
  1664. /* milliseconds to timeout for DMA transfer */
  1665. transaction_timeout = 2;
  1666. edge_port->ump_read_timeout = max (20, ((transaction_timeout * 3) / 2) );
  1667. // milliseconds to timeout for DMA transfer
  1668. open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
  1669. UMP_PIPE_TRANS_TIMEOUT_ENA |
  1670. (transaction_timeout << 2));
  1671. dbg ("%s - Sending UMPC_OPEN_PORT", __func__);
  1672. /* Tell TI to open and start the port */
  1673. status = TIWriteCommandSync (dev,
  1674. UMPC_OPEN_PORT,
  1675. (u8)(UMPM_UART1_PORT + port_number),
  1676. open_settings,
  1677. NULL,
  1678. 0);
  1679. if (status) {
  1680. dev_err(&port->dev,"%s - cannot send open command, %d\n", __func__, status);
  1681. return status;
  1682. }
  1683. /* Start the DMA? */
  1684. status = TIWriteCommandSync (dev,
  1685. UMPC_START_PORT,
  1686. (u8)(UMPM_UART1_PORT + port_number),
  1687. 0,
  1688. NULL,
  1689. 0);
  1690. if (status) {
  1691. dev_err(&port->dev,"%s - cannot send start DMA command, %d\n", __func__, status);
  1692. return status;
  1693. }
  1694. /* Clear TX and RX buffers in UMP */
  1695. status = TIPurgeDataSync (port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
  1696. if (status) {
  1697. dev_err(&port->dev,"%s - cannot send clear buffers command, %d\n", __func__, status);
  1698. return status;
  1699. }
  1700. /* Read Initial MSR */
  1701. status = TIReadVendorRequestSync (dev,
  1702. UMPC_READ_MSR, // Request
  1703. 0, // wValue
  1704. (__u16)(UMPM_UART1_PORT + port_number), // wIndex (Address)
  1705. &edge_port->shadow_msr, // TransferBuffer
  1706. 1); // TransferBufferLength
  1707. if (status) {
  1708. dev_err(&port->dev,"%s - cannot send read MSR command, %d\n", __func__, status);
  1709. return status;
  1710. }
  1711. dbg ("ShadowMSR 0x%X", edge_port->shadow_msr);
  1712. /* Set Initial MCR */
  1713. edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
  1714. dbg ("ShadowMCR 0x%X", edge_port->shadow_mcr);
  1715. edge_serial = edge_port->edge_serial;
  1716. if (mutex_lock_interruptible(&edge_serial->es_lock))
  1717. return -ERESTARTSYS;
  1718. if (edge_serial->num_ports_open == 0) {
  1719. /* we are the first port to be opened, let's post the interrupt urb */
  1720. urb = edge_serial->serial->port[0]->interrupt_in_urb;
  1721. if (!urb) {
  1722. dev_err (&port->dev, "%s - no interrupt urb present, exiting\n", __func__);
  1723. status = -EINVAL;
  1724. goto release_es_lock;
  1725. }
  1726. urb->complete = edge_interrupt_callback;
  1727. urb->context = edge_serial;
  1728. urb->dev = dev;
  1729. status = usb_submit_urb (urb, GFP_KERNEL);
  1730. if (status) {
  1731. dev_err (&port->dev, "%s - usb_submit_urb failed with value %d\n", __func__, status);
  1732. goto release_es_lock;
  1733. }
  1734. }
  1735. /*
  1736. * reset the data toggle on the bulk endpoints to work around bug in
  1737. * host controllers where things get out of sync some times
  1738. */
  1739. usb_clear_halt (dev, port->write_urb->pipe);
  1740. usb_clear_halt (dev, port->read_urb->pipe);
  1741. /* start up our bulk read urb */
  1742. urb = port->read_urb;
  1743. if (!urb) {
  1744. dev_err (&port->dev, "%s - no read urb present, exiting\n", __func__);
  1745. status = -EINVAL;
  1746. goto unlink_int_urb;
  1747. }
  1748. edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
  1749. urb->complete = edge_bulk_in_callback;
  1750. urb->context = edge_port;
  1751. urb->dev = dev;
  1752. status = usb_submit_urb (urb, GFP_KERNEL);
  1753. if (status) {
  1754. dev_err (&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __func__, status);
  1755. goto unlink_int_urb;
  1756. }
  1757. ++edge_serial->num_ports_open;
  1758. dbg("%s - exited", __func__);
  1759. goto release_es_lock;
  1760. unlink_int_urb:
  1761. if (edge_port->edge_serial->num_ports_open == 0)
  1762. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  1763. release_es_lock:
  1764. mutex_unlock(&edge_serial->es_lock);
  1765. return status;
  1766. }
  1767. static void edge_close (struct usb_serial_port *port, struct file *filp)
  1768. {
  1769. struct edgeport_serial *edge_serial;
  1770. struct edgeport_port *edge_port;
  1771. int port_number;
  1772. int status;
  1773. dbg("%s - port %d", __func__, port->number);
  1774. edge_serial = usb_get_serial_data(port->serial);
  1775. edge_port = usb_get_serial_port_data(port);
  1776. if ((edge_serial == NULL) || (edge_port == NULL))
  1777. return;
  1778. /* The bulkreadcompletion routine will check
  1779. * this flag and dump add read data */
  1780. edge_port->close_pending = 1;
  1781. /* chase the port close and flush */
  1782. TIChasePort (edge_port, (HZ*closing_wait)/100, 1);
  1783. usb_kill_urb(port->read_urb);
  1784. usb_kill_urb(port->write_urb);
  1785. edge_port->ep_write_urb_in_use = 0;
  1786. /* assuming we can still talk to the device,
  1787. * send a close port command to it */
  1788. dbg("%s - send umpc_close_port", __func__);
  1789. port_number = port->number - port->serial->minor;
  1790. status = TIWriteCommandSync (port->serial->dev,
  1791. UMPC_CLOSE_PORT,
  1792. (__u8)(UMPM_UART1_PORT + port_number),
  1793. 0,
  1794. NULL,
  1795. 0);
  1796. mutex_lock(&edge_serial->es_lock);
  1797. --edge_port->edge_serial->num_ports_open;
  1798. if (edge_port->edge_serial->num_ports_open <= 0) {
  1799. /* last port is now closed, let's shut down our interrupt urb */
  1800. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  1801. edge_port->edge_serial->num_ports_open = 0;
  1802. }
  1803. mutex_unlock(&edge_serial->es_lock);
  1804. edge_port->close_pending = 0;
  1805. dbg("%s - exited", __func__);
  1806. }
  1807. static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
  1808. {
  1809. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1810. unsigned long flags;
  1811. dbg("%s - port %d", __func__, port->number);
  1812. if (count == 0) {
  1813. dbg("%s - write request of 0 bytes", __func__);
  1814. return 0;
  1815. }
  1816. if (edge_port == NULL)
  1817. return -ENODEV;
  1818. if (edge_port->close_pending == 1)
  1819. return -ENODEV;
  1820. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1821. count = edge_buf_put(edge_port->ep_out_buf, data, count);
  1822. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1823. edge_send(port);
  1824. return count;
  1825. }
  1826. static void edge_send(struct usb_serial_port *port)
  1827. {
  1828. int count, result;
  1829. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1830. struct tty_struct *tty = port->tty;
  1831. unsigned long flags;
  1832. dbg("%s - port %d", __func__, port->number);
  1833. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1834. if (edge_port->ep_write_urb_in_use) {
  1835. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1836. return;
  1837. }
  1838. count = edge_buf_get(edge_port->ep_out_buf,
  1839. port->write_urb->transfer_buffer,
  1840. port->bulk_out_size);
  1841. if (count == 0) {
  1842. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1843. return;
  1844. }
  1845. edge_port->ep_write_urb_in_use = 1;
  1846. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1847. usb_serial_debug_data(debug, &port->dev, __func__, count, port->write_urb->transfer_buffer);
  1848. /* set up our urb */
  1849. usb_fill_bulk_urb (port->write_urb, port->serial->dev,
  1850. usb_sndbulkpipe (port->serial->dev,
  1851. port->bulk_out_endpointAddress),
  1852. port->write_urb->transfer_buffer, count,
  1853. edge_bulk_out_callback,
  1854. port);
  1855. /* send the data out the bulk port */
  1856. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  1857. if (result) {
  1858. dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __func__, result);
  1859. edge_port->ep_write_urb_in_use = 0;
  1860. // TODO: reschedule edge_send
  1861. } else {
  1862. edge_port->icount.tx += count;
  1863. }
  1864. /* wakeup any process waiting for writes to complete */
  1865. /* there is now more room in the buffer for new writes */
  1866. if (tty) {
  1867. /* let the tty driver wakeup if it has a special write_wakeup function */
  1868. tty_wakeup(tty);
  1869. }
  1870. }
  1871. static int edge_write_room (struct usb_serial_port *port)
  1872. {
  1873. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1874. int room = 0;
  1875. unsigned long flags;
  1876. dbg("%s - port %d", __func__, port->number);
  1877. if (edge_port == NULL)
  1878. return -ENODEV;
  1879. if (edge_port->close_pending == 1)
  1880. return -ENODEV;
  1881. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1882. room = edge_buf_space_avail(edge_port->ep_out_buf);
  1883. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1884. dbg("%s - returns %d", __func__, room);
  1885. return room;
  1886. }
  1887. static int edge_chars_in_buffer (struct usb_serial_port *port)
  1888. {
  1889. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1890. int chars = 0;
  1891. unsigned long flags;
  1892. dbg("%s - port %d", __func__, port->number);
  1893. if (edge_port == NULL)
  1894. return -ENODEV;
  1895. if (edge_port->close_pending == 1)
  1896. return -ENODEV;
  1897. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1898. chars = edge_buf_data_avail(edge_port->ep_out_buf);
  1899. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1900. dbg ("%s - returns %d", __func__, chars);
  1901. return chars;
  1902. }
  1903. static void edge_throttle (struct usb_serial_port *port)
  1904. {
  1905. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1906. struct tty_struct *tty;
  1907. int status;
  1908. dbg("%s - port %d", __func__, port->number);
  1909. if (edge_port == NULL)
  1910. return;
  1911. tty = port->tty;
  1912. if (!tty) {
  1913. dbg ("%s - no tty available", __func__);
  1914. return;
  1915. }
  1916. /* if we are implementing XON/XOFF, send the stop character */
  1917. if (I_IXOFF(tty)) {
  1918. unsigned char stop_char = STOP_CHAR(tty);
  1919. status = edge_write (port, &stop_char, 1);
  1920. if (status <= 0) {
  1921. dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
  1922. }
  1923. }
  1924. /* if we are implementing RTS/CTS, stop reads */
  1925. /* and the Edgeport will clear the RTS line */
  1926. if (C_CRTSCTS(tty))
  1927. stop_read(edge_port);
  1928. }
  1929. static void edge_unthrottle (struct usb_serial_port *port)
  1930. {
  1931. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1932. struct tty_struct *tty;
  1933. int status;
  1934. dbg("%s - port %d", __func__, port->number);
  1935. if (edge_port == NULL)
  1936. return;
  1937. tty = port->tty;
  1938. if (!tty) {
  1939. dbg ("%s - no tty available", __func__);
  1940. return;
  1941. }
  1942. /* if we are implementing XON/XOFF, send the start character */
  1943. if (I_IXOFF(tty)) {
  1944. unsigned char start_char = START_CHAR(tty);
  1945. status = edge_write (port, &start_char, 1);
  1946. if (status <= 0) {
  1947. dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
  1948. }
  1949. }
  1950. /* if we are implementing RTS/CTS, restart reads */
  1951. /* are the Edgeport will assert the RTS line */
  1952. if (C_CRTSCTS(tty)) {
  1953. status = restart_read(edge_port);
  1954. if (status)
  1955. dev_err(&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __func__, status);
  1956. }
  1957. }
  1958. static void stop_read(struct edgeport_port *edge_port)
  1959. {
  1960. unsigned long flags;
  1961. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1962. if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
  1963. edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
  1964. edge_port->shadow_mcr &= ~MCR_RTS;
  1965. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1966. }
  1967. static int restart_read(struct edgeport_port *edge_port)
  1968. {
  1969. struct urb *urb;
  1970. int status = 0;
  1971. unsigned long flags;
  1972. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1973. if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
  1974. urb = edge_port->port->read_urb;
  1975. urb->complete = edge_bulk_in_callback;
  1976. urb->context = edge_port;
  1977. urb->dev = edge_port->port->serial->dev;
  1978. status = usb_submit_urb(urb, GFP_ATOMIC);
  1979. }
  1980. edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
  1981. edge_port->shadow_mcr |= MCR_RTS;
  1982. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1983. return status;
  1984. }
  1985. static void change_port_settings (struct edgeport_port *edge_port, struct ktermios *old_termios)
  1986. {
  1987. struct ump_uart_config *config;
  1988. struct tty_struct *tty;
  1989. int baud;
  1990. unsigned cflag;
  1991. int status;
  1992. int port_number = edge_port->port->number - edge_port->port->serial->minor;
  1993. dbg("%s - port %d", __func__, edge_port->port->number);
  1994. tty = edge_port->port->tty;
  1995. config = kmalloc (sizeof (*config), GFP_KERNEL);
  1996. if (!config) {
  1997. dev_err (&edge_port->port->dev, "%s - out of memory\n", __func__);
  1998. return;
  1999. }
  2000. cflag = tty->termios->c_cflag;
  2001. config->wFlags = 0;
  2002. /* These flags must be set */
  2003. config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
  2004. config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
  2005. config->bUartMode = (__u8)(edge_port->bUartMode);
  2006. switch (cflag & CSIZE) {
  2007. case CS5:
  2008. config->bDataBits = UMP_UART_CHAR5BITS;
  2009. dbg ("%s - data bits = 5", __func__);
  2010. break;
  2011. case CS6:
  2012. config->bDataBits = UMP_UART_CHAR6BITS;
  2013. dbg ("%s - data bits = 6", __func__);
  2014. break;
  2015. case CS7:
  2016. config->bDataBits = UMP_UART_CHAR7BITS;
  2017. dbg ("%s - data bits = 7", __func__);
  2018. break;
  2019. default:
  2020. case CS8:
  2021. config->bDataBits = UMP_UART_CHAR8BITS;
  2022. dbg ("%s - data bits = 8", __func__);
  2023. break;
  2024. }
  2025. if (cflag & PARENB) {
  2026. if (cflag & PARODD) {
  2027. config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
  2028. config->bParity = UMP_UART_ODDPARITY;
  2029. dbg("%s - parity = odd", __func__);
  2030. } else {
  2031. config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
  2032. config->bParity = UMP_UART_EVENPARITY;
  2033. dbg("%s - parity = even", __func__);
  2034. }
  2035. } else {
  2036. config->bParity = UMP_UART_NOPARITY;
  2037. dbg("%s - parity = none", __func__);
  2038. }
  2039. if (cflag & CSTOPB) {
  2040. config->bStopBits = UMP_UART_STOPBIT2;
  2041. dbg("%s - stop bits = 2", __func__);
  2042. } else {
  2043. config->bStopBits = UMP_UART_STOPBIT1;
  2044. dbg("%s - stop bits = 1", __func__);
  2045. }
  2046. /* figure out the flow control settings */
  2047. if (cflag & CRTSCTS) {
  2048. config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
  2049. config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
  2050. dbg("%s - RTS/CTS is enabled", __func__);
  2051. } else {
  2052. dbg("%s - RTS/CTS is disabled", __func__);
  2053. tty->hw_stopped = 0;
  2054. restart_read(edge_port);
  2055. }
  2056. /* if we are implementing XON/XOFF, set the start and stop character in the device */
  2057. if (I_IXOFF(tty) || I_IXON(tty)) {
  2058. config->cXon = START_CHAR(tty);
  2059. config->cXoff = STOP_CHAR(tty);
  2060. /* if we are implementing INBOUND XON/XOFF */
  2061. if (I_IXOFF(tty)) {
  2062. config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
  2063. dbg ("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
  2064. __func__, config->cXon, config->cXoff);
  2065. } else {
  2066. dbg ("%s - INBOUND XON/XOFF is disabled", __func__);
  2067. }
  2068. /* if we are implementing OUTBOUND XON/XOFF */
  2069. if (I_IXON(tty)) {
  2070. config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
  2071. dbg ("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
  2072. __func__, config->cXon, config->cXoff);
  2073. } else {
  2074. dbg ("%s - OUTBOUND XON/XOFF is disabled", __func__);
  2075. }
  2076. }
  2077. tty->termios->c_cflag &= ~CMSPAR;
  2078. /* Round the baud rate */
  2079. baud = tty_get_baud_rate(tty);
  2080. if (!baud) {
  2081. /* pick a default, any default... */
  2082. baud = 9600;
  2083. } else
  2084. tty_encode_baud_rate(tty, baud, baud);
  2085. edge_port->baud_rate = baud;
  2086. config->wBaudRate = (__u16)((461550L + baud/2) / baud);
  2087. /* FIXME: Recompute actual baud from divisor here */
  2088. dbg ("%s - baud rate = %d, wBaudRate = %d", __func__, baud, config->wBaudRate);
  2089. dbg ("wBaudRate: %d", (int)(461550L / config->wBaudRate));
  2090. dbg ("wFlags: 0x%x", config->wFlags);
  2091. dbg ("bDataBits: %d", config->bDataBits);
  2092. dbg ("bParity: %d", config->bParity);
  2093. dbg ("bStopBits: %d", config->bStopBits);
  2094. dbg ("cXon: %d", config->cXon);
  2095. dbg ("cXoff: %d", config->cXoff);
  2096. dbg ("bUartMode: %d", config->bUartMode);
  2097. /* move the word values into big endian mode */
  2098. cpu_to_be16s (&config->wFlags);
  2099. cpu_to_be16s (&config->wBaudRate);
  2100. status = TIWriteCommandSync (edge_port->port->serial->dev,
  2101. UMPC_SET_CONFIG,
  2102. (__u8)(UMPM_UART1_PORT + port_number),
  2103. 0,
  2104. (__u8 *)config,
  2105. sizeof(*config));
  2106. if (status) {
  2107. dbg ("%s - error %d when trying to write config to device",
  2108. __func__, status);
  2109. }
  2110. kfree (config);
  2111. return;
  2112. }
  2113. static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
  2114. {
  2115. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2116. struct tty_struct *tty = port->tty;
  2117. unsigned int cflag;
  2118. cflag = tty->termios->c_cflag;
  2119. dbg("%s - clfag %08x iflag %08x", __func__,
  2120. tty->termios->c_cflag, tty->termios->c_iflag);
  2121. dbg("%s - old clfag %08x old iflag %08x", __func__,
  2122. old_termios->c_cflag, old_termios->c_iflag);
  2123. dbg("%s - port %d", __func__, port->number);
  2124. if (edge_port == NULL)
  2125. return;
  2126. /* change the port settings to the new ones specified */
  2127. change_port_settings (edge_port, old_termios);
  2128. return;
  2129. }
  2130. static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
  2131. {
  2132. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2133. unsigned int mcr;
  2134. unsigned long flags;
  2135. dbg("%s - port %d", __func__, port->number);
  2136. spin_lock_irqsave(&edge_port->ep_lock, flags);
  2137. mcr = edge_port->shadow_mcr;
  2138. if (set & TIOCM_RTS)
  2139. mcr |= MCR_RTS;
  2140. if (set & TIOCM_DTR)
  2141. mcr |= MCR_DTR;
  2142. if (set & TIOCM_LOOP)
  2143. mcr |= MCR_LOOPBACK;
  2144. if (clear & TIOCM_RTS)
  2145. mcr &= ~MCR_RTS;
  2146. if (clear & TIOCM_DTR)
  2147. mcr &= ~MCR_DTR;
  2148. if (clear & TIOCM_LOOP)
  2149. mcr &= ~MCR_LOOPBACK;
  2150. edge_port->shadow_mcr = mcr;
  2151. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  2152. TIRestoreMCR (edge_port, mcr);
  2153. return 0;
  2154. }
  2155. static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
  2156. {
  2157. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2158. unsigned int result = 0;
  2159. unsigned int msr;
  2160. unsigned int mcr;
  2161. unsigned long flags;
  2162. dbg("%s - port %d", __func__, port->number);
  2163. spin_lock_irqsave(&edge_port->ep_lock, flags);
  2164. msr = edge_port->shadow_msr;
  2165. mcr = edge_port->shadow_mcr;
  2166. result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  2167. | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  2168. | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  2169. | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
  2170. | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  2171. | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  2172. dbg("%s -- %x", __func__, result);
  2173. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  2174. return result;
  2175. }
  2176. static int get_serial_info (struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
  2177. {
  2178. struct serial_struct tmp;
  2179. if (!retinfo)
  2180. return -EFAULT;
  2181. memset(&tmp, 0, sizeof(tmp));
  2182. tmp.type = PORT_16550A;
  2183. tmp.line = edge_port->port->serial->minor;
  2184. tmp.port = edge_port->port->number;
  2185. tmp.irq = 0;
  2186. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  2187. tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
  2188. tmp.baud_base = 9600;
  2189. tmp.close_delay = 5*HZ;
  2190. tmp.closing_wait = closing_wait;
  2191. // tmp.custom_divisor = state->custom_divisor;
  2192. // tmp.hub6 = state->hub6;
  2193. // tmp.io_type = state->io_type;
  2194. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  2195. return -EFAULT;
  2196. return 0;
  2197. }
  2198. static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
  2199. {
  2200. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2201. struct async_icount cnow;
  2202. struct async_icount cprev;
  2203. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  2204. switch (cmd) {
  2205. case TIOCINQ:
  2206. dbg("%s - (%d) TIOCINQ", __func__, port->number);
  2207. // return get_number_bytes_avail(edge_port, (unsigned int *) arg);
  2208. break;
  2209. case TIOCSERGETLSR:
  2210. dbg("%s - (%d) TIOCSERGETLSR", __func__, port->number);
  2211. // return get_lsr_info(edge_port, (unsigned int *) arg);
  2212. break;
  2213. case TIOCGSERIAL:
  2214. dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
  2215. return get_serial_info(edge_port, (struct serial_struct __user *) arg);
  2216. break;
  2217. case TIOCSSERIAL:
  2218. dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
  2219. break;
  2220. case TIOCMIWAIT:
  2221. dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
  2222. cprev = edge_port->icount;
  2223. while (1) {
  2224. interruptible_sleep_on(&edge_port->delta_msr_wait);
  2225. /* see if a signal did it */
  2226. if (signal_pending(current))
  2227. return -ERESTARTSYS;
  2228. cnow = edge_port->icount;
  2229. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  2230. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  2231. return -EIO; /* no change => error */
  2232. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  2233. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  2234. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  2235. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  2236. return 0;
  2237. }
  2238. cprev = cnow;
  2239. }
  2240. /* not reached */
  2241. break;
  2242. case TIOCGICOUNT:
  2243. dbg ("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
  2244. port->number, edge_port->icount.rx, edge_port->icount.tx);
  2245. if (copy_to_user((void __user *)arg, &edge_port->icount, sizeof(edge_port->icount)))
  2246. return -EFAULT;
  2247. return 0;
  2248. }
  2249. return -ENOIOCTLCMD;
  2250. }
  2251. static void edge_break (struct usb_serial_port *port, int break_state)
  2252. {
  2253. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2254. int status;
  2255. dbg ("%s - state = %d", __func__, break_state);
  2256. /* chase the port close */
  2257. TIChasePort (edge_port, 0, 0);
  2258. if (break_state == -1) {
  2259. status = TISetBreak (edge_port);
  2260. } else {
  2261. status = TIClearBreak (edge_port);
  2262. }
  2263. if (status) {
  2264. dbg ("%s - error %d sending break set/clear command.",
  2265. __func__, status);
  2266. }
  2267. }
  2268. static int edge_startup (struct usb_serial *serial)
  2269. {
  2270. struct edgeport_serial *edge_serial;
  2271. struct edgeport_port *edge_port;
  2272. struct usb_device *dev;
  2273. int status;
  2274. int i;
  2275. dev = serial->dev;
  2276. /* create our private serial structure */
  2277. edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
  2278. if (edge_serial == NULL) {
  2279. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  2280. return -ENOMEM;
  2281. }
  2282. mutex_init(&edge_serial->es_lock);
  2283. edge_serial->serial = serial;
  2284. usb_set_serial_data(serial, edge_serial);
  2285. status = TIDownloadFirmware (edge_serial);
  2286. if (status) {
  2287. kfree (edge_serial);
  2288. return status;
  2289. }
  2290. /* set up our port private structures */
  2291. for (i = 0; i < serial->num_ports; ++i) {
  2292. edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
  2293. if (edge_port == NULL) {
  2294. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  2295. goto cleanup;
  2296. }
  2297. spin_lock_init(&edge_port->ep_lock);
  2298. edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
  2299. if (edge_port->ep_out_buf == NULL) {
  2300. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  2301. kfree(edge_port);
  2302. goto cleanup;
  2303. }
  2304. edge_port->port = serial->port[i];
  2305. edge_port->edge_serial = edge_serial;
  2306. usb_set_serial_port_data(serial->port[i], edge_port);
  2307. edge_port->bUartMode = default_uart_mode;
  2308. }
  2309. return 0;
  2310. cleanup:
  2311. for (--i; i>=0; --i) {
  2312. edge_port = usb_get_serial_port_data(serial->port[i]);
  2313. edge_buf_free(edge_port->ep_out_buf);
  2314. kfree(edge_port);
  2315. usb_set_serial_port_data(serial->port[i], NULL);
  2316. }
  2317. kfree (edge_serial);
  2318. usb_set_serial_data(serial, NULL);
  2319. return -ENOMEM;
  2320. }
  2321. static void edge_shutdown (struct usb_serial *serial)
  2322. {
  2323. int i;
  2324. struct edgeport_port *edge_port;
  2325. dbg ("%s", __func__);
  2326. for (i = 0; i < serial->num_ports; ++i) {
  2327. edge_port = usb_get_serial_port_data(serial->port[i]);
  2328. edge_remove_sysfs_attrs(edge_port->port);
  2329. edge_buf_free(edge_port->ep_out_buf);
  2330. kfree(edge_port);
  2331. usb_set_serial_port_data(serial->port[i], NULL);
  2332. }
  2333. kfree(usb_get_serial_data(serial));
  2334. usb_set_serial_data(serial, NULL);
  2335. }
  2336. /* Sysfs Attributes */
  2337. static ssize_t show_uart_mode(struct device *dev,
  2338. struct device_attribute *attr, char *buf)
  2339. {
  2340. struct usb_serial_port *port = to_usb_serial_port(dev);
  2341. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2342. return sprintf(buf, "%d\n", edge_port->bUartMode);
  2343. }
  2344. static ssize_t store_uart_mode(struct device *dev,
  2345. struct device_attribute *attr, const char *valbuf, size_t count)
  2346. {
  2347. struct usb_serial_port *port = to_usb_serial_port(dev);
  2348. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2349. unsigned int v = simple_strtoul(valbuf, NULL, 0);
  2350. dbg("%s: setting uart_mode = %d", __func__, v);
  2351. if (v < 256)
  2352. edge_port->bUartMode = v;
  2353. else
  2354. dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
  2355. return count;
  2356. }
  2357. static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode, store_uart_mode);
  2358. static int edge_create_sysfs_attrs(struct usb_serial_port *port)
  2359. {
  2360. return device_create_file(&port->dev, &dev_attr_uart_mode);
  2361. }
  2362. static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
  2363. {
  2364. device_remove_file(&port->dev, &dev_attr_uart_mode);
  2365. return 0;
  2366. }
  2367. /* Circular Buffer */
  2368. /*
  2369. * edge_buf_alloc
  2370. *
  2371. * Allocate a circular buffer and all associated memory.
  2372. */
  2373. static struct edge_buf *edge_buf_alloc(unsigned int size)
  2374. {
  2375. struct edge_buf *eb;
  2376. if (size == 0)
  2377. return NULL;
  2378. eb = kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
  2379. if (eb == NULL)
  2380. return NULL;
  2381. eb->buf_buf = kmalloc(size, GFP_KERNEL);
  2382. if (eb->buf_buf == NULL) {
  2383. kfree(eb);
  2384. return NULL;
  2385. }
  2386. eb->buf_size = size;
  2387. eb->buf_get = eb->buf_put = eb->buf_buf;
  2388. return eb;
  2389. }
  2390. /*
  2391. * edge_buf_free
  2392. *
  2393. * Free the buffer and all associated memory.
  2394. */
  2395. static void edge_buf_free(struct edge_buf *eb)
  2396. {
  2397. if (eb) {
  2398. kfree(eb->buf_buf);
  2399. kfree(eb);
  2400. }
  2401. }
  2402. /*
  2403. * edge_buf_clear
  2404. *
  2405. * Clear out all data in the circular buffer.
  2406. */
  2407. static void edge_buf_clear(struct edge_buf *eb)
  2408. {
  2409. if (eb != NULL)
  2410. eb->buf_get = eb->buf_put;
  2411. /* equivalent to a get of all data available */
  2412. }
  2413. /*
  2414. * edge_buf_data_avail
  2415. *
  2416. * Return the number of bytes of data available in the circular
  2417. * buffer.
  2418. */
  2419. static unsigned int edge_buf_data_avail(struct edge_buf *eb)
  2420. {
  2421. if (eb != NULL)
  2422. return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
  2423. else
  2424. return 0;
  2425. }
  2426. /*
  2427. * edge_buf_space_avail
  2428. *
  2429. * Return the number of bytes of space available in the circular
  2430. * buffer.
  2431. */
  2432. static unsigned int edge_buf_space_avail(struct edge_buf *eb)
  2433. {
  2434. if (eb != NULL)
  2435. return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
  2436. else
  2437. return 0;
  2438. }
  2439. /*
  2440. * edge_buf_put
  2441. *
  2442. * Copy data data from a user buffer and put it into the circular buffer.
  2443. * Restrict to the amount of space available.
  2444. *
  2445. * Return the number of bytes copied.
  2446. */
  2447. static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
  2448. unsigned int count)
  2449. {
  2450. unsigned int len;
  2451. if (eb == NULL)
  2452. return 0;
  2453. len = edge_buf_space_avail(eb);
  2454. if (count > len)
  2455. count = len;
  2456. if (count == 0)
  2457. return 0;
  2458. len = eb->buf_buf + eb->buf_size - eb->buf_put;
  2459. if (count > len) {
  2460. memcpy(eb->buf_put, buf, len);
  2461. memcpy(eb->buf_buf, buf+len, count - len);
  2462. eb->buf_put = eb->buf_buf + count - len;
  2463. } else {
  2464. memcpy(eb->buf_put, buf, count);
  2465. if (count < len)
  2466. eb->buf_put += count;
  2467. else /* count == len */
  2468. eb->buf_put = eb->buf_buf;
  2469. }
  2470. return count;
  2471. }
  2472. /*
  2473. * edge_buf_get
  2474. *
  2475. * Get data from the circular buffer and copy to the given buffer.
  2476. * Restrict to the amount of data available.
  2477. *
  2478. * Return the number of bytes copied.
  2479. */
  2480. static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
  2481. unsigned int count)
  2482. {
  2483. unsigned int len;
  2484. if (eb == NULL)
  2485. return 0;
  2486. len = edge_buf_data_avail(eb);
  2487. if (count > len)
  2488. count = len;
  2489. if (count == 0)
  2490. return 0;
  2491. len = eb->buf_buf + eb->buf_size - eb->buf_get;
  2492. if (count > len) {
  2493. memcpy(buf, eb->buf_get, len);
  2494. memcpy(buf+len, eb->buf_buf, count - len);
  2495. eb->buf_get = eb->buf_buf + count - len;
  2496. } else {
  2497. memcpy(buf, eb->buf_get, count);
  2498. if (count < len)
  2499. eb->buf_get += count;
  2500. else /* count == len */
  2501. eb->buf_get = eb->buf_buf;
  2502. }
  2503. return count;
  2504. }
  2505. static struct usb_serial_driver edgeport_1port_device = {
  2506. .driver = {
  2507. .owner = THIS_MODULE,
  2508. .name = "edgeport_ti_1",
  2509. },
  2510. .description = "Edgeport TI 1 port adapter",
  2511. .usb_driver = &io_driver,
  2512. .id_table = edgeport_1port_id_table,
  2513. .num_ports = 1,
  2514. .open = edge_open,
  2515. .close = edge_close,
  2516. .throttle = edge_throttle,
  2517. .unthrottle = edge_unthrottle,
  2518. .attach = edge_startup,
  2519. .shutdown = edge_shutdown,
  2520. .port_probe = edge_create_sysfs_attrs,
  2521. .ioctl = edge_ioctl,
  2522. .set_termios = edge_set_termios,
  2523. .tiocmget = edge_tiocmget,
  2524. .tiocmset = edge_tiocmset,
  2525. .write = edge_write,
  2526. .write_room = edge_write_room,
  2527. .chars_in_buffer = edge_chars_in_buffer,
  2528. .break_ctl = edge_break,
  2529. .read_int_callback = edge_interrupt_callback,
  2530. .read_bulk_callback = edge_bulk_in_callback,
  2531. .write_bulk_callback = edge_bulk_out_callback,
  2532. };
  2533. static struct usb_serial_driver edgeport_2port_device = {
  2534. .driver = {
  2535. .owner = THIS_MODULE,
  2536. .name = "edgeport_ti_2",
  2537. },
  2538. .description = "Edgeport TI 2 port adapter",
  2539. .usb_driver = &io_driver,
  2540. .id_table = edgeport_2port_id_table,
  2541. .num_ports = 2,
  2542. .open = edge_open,
  2543. .close = edge_close,
  2544. .throttle = edge_throttle,
  2545. .unthrottle = edge_unthrottle,
  2546. .attach = edge_startup,
  2547. .shutdown = edge_shutdown,
  2548. .port_probe = edge_create_sysfs_attrs,
  2549. .ioctl = edge_ioctl,
  2550. .set_termios = edge_set_termios,
  2551. .tiocmget = edge_tiocmget,
  2552. .tiocmset = edge_tiocmset,
  2553. .write = edge_write,
  2554. .write_room = edge_write_room,
  2555. .chars_in_buffer = edge_chars_in_buffer,
  2556. .break_ctl = edge_break,
  2557. .read_int_callback = edge_interrupt_callback,
  2558. .read_bulk_callback = edge_bulk_in_callback,
  2559. .write_bulk_callback = edge_bulk_out_callback,
  2560. };
  2561. static int __init edgeport_init(void)
  2562. {
  2563. int retval;
  2564. retval = usb_serial_register(&edgeport_1port_device);
  2565. if (retval)
  2566. goto failed_1port_device_register;
  2567. retval = usb_serial_register(&edgeport_2port_device);
  2568. if (retval)
  2569. goto failed_2port_device_register;
  2570. retval = usb_register(&io_driver);
  2571. if (retval)
  2572. goto failed_usb_register;
  2573. info(DRIVER_DESC " " DRIVER_VERSION);
  2574. return 0;
  2575. failed_usb_register:
  2576. usb_serial_deregister(&edgeport_2port_device);
  2577. failed_2port_device_register:
  2578. usb_serial_deregister(&edgeport_1port_device);
  2579. failed_1port_device_register:
  2580. return retval;
  2581. }
  2582. static void __exit edgeport_exit (void)
  2583. {
  2584. usb_deregister (&io_driver);
  2585. usb_serial_deregister (&edgeport_1port_device);
  2586. usb_serial_deregister (&edgeport_2port_device);
  2587. }
  2588. module_init(edgeport_init);
  2589. module_exit(edgeport_exit);
  2590. /* Module information */
  2591. MODULE_AUTHOR(DRIVER_AUTHOR);
  2592. MODULE_DESCRIPTION(DRIVER_DESC);
  2593. MODULE_LICENSE("GPL");
  2594. module_param(debug, bool, S_IRUGO | S_IWUSR);
  2595. MODULE_PARM_DESC(debug, "Debug enabled or not");
  2596. module_param(low_latency, bool, S_IRUGO | S_IWUSR);
  2597. MODULE_PARM_DESC(low_latency, "Low latency enabled or not");
  2598. module_param(closing_wait, int, S_IRUGO | S_IWUSR);
  2599. MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
  2600. module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
  2601. MODULE_PARM_DESC(ignore_cpu_rev, "Ignore the cpu revision when connecting to a device");
  2602. module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
  2603. MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");