io_edgeport.c 92 KB

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