io_edgeport.c 98 KB

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