io_edgeport.c 97 KB

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