io_ti.c 84 KB

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