io_edgeport.c 100 KB

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