io_edgeport.c 97 KB

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