io_edgeport.c 98 KB

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