io_edgeport.c 93 KB

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