io_edgeport.c 100 KB

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