io_edgeport.c 92 KB

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