io_edgeport.c 95 KB

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