io_edgeport.c 100 KB

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