io_edgeport.c 95 KB

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