io_edgeport.c 98 KB

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