io_edgeport.c 98 KB

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