io_edgeport.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195
  1. /*
  2. * Edgeport USB Serial Converter driver
  3. *
  4. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  5. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Supports the following devices:
  13. * Edgeport/4
  14. * Edgeport/4t
  15. * Edgeport/2
  16. * Edgeport/4i
  17. * Edgeport/2i
  18. * Edgeport/421
  19. * Edgeport/21
  20. * Rapidport/4
  21. * Edgeport/8
  22. * Edgeport/2D8
  23. * Edgeport/4D8
  24. * Edgeport/8i
  25. *
  26. * For questions or problems with this driver, contact Inside Out
  27. * Networks technical support, or Peter Berger <pberger@brimson.com>,
  28. * or Al Borchers <alborchers@steinerpoint.com>.
  29. *
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/errno.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/tty.h>
  37. #include <linux/tty_driver.h>
  38. #include <linux/tty_flip.h>
  39. #include <linux/module.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/serial.h>
  42. #include <linux/ioctl.h>
  43. #include <linux/wait.h>
  44. #include <linux/firmware.h>
  45. #include <linux/ihex.h>
  46. #include <linux/uaccess.h>
  47. #include <linux/usb.h>
  48. #include <linux/usb/serial.h>
  49. #include "io_edgeport.h"
  50. #include "io_ionsp.h" /* info for the iosp messages */
  51. #include "io_16654.h" /* 16654 UART defines */
  52. /*
  53. * Version Information
  54. */
  55. #define DRIVER_VERSION "v2.7"
  56. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
  57. #define DRIVER_DESC "Edgeport USB Serial Driver"
  58. #define MAX_NAME_LEN 64
  59. #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
  60. #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
  61. #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
  62. /* receive port state */
  63. enum RXSTATE {
  64. EXPECT_HDR1 = 0, /* Expect header byte 1 */
  65. EXPECT_HDR2 = 1, /* Expect header byte 2 */
  66. EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
  67. EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
  68. };
  69. /* Transmit Fifo
  70. * This Transmit queue is an extension of the edgeport Rx buffer.
  71. * The maximum amount of data buffered in both the edgeport
  72. * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
  73. */
  74. struct TxFifo {
  75. unsigned int head; /* index to head pointer (write) */
  76. unsigned int tail; /* index to tail pointer (read) */
  77. unsigned int count; /* Bytes in queue */
  78. unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
  79. unsigned char *fifo; /* allocated Buffer */
  80. };
  81. /* This structure holds all of the local port information */
  82. struct edgeport_port {
  83. __u16 txCredits; /* our current credits for this port */
  84. __u16 maxTxCredits; /* the max size of the port */
  85. struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
  86. struct urb *write_urb; /* write URB for this port */
  87. bool write_in_progress; /* 'true' while a write URB is outstanding */
  88. spinlock_t ep_lock;
  89. __u8 shadowLCR; /* last LCR value received */
  90. __u8 shadowMCR; /* last MCR value received */
  91. __u8 shadowMSR; /* last MSR value received */
  92. __u8 shadowLSR; /* last LSR value received */
  93. __u8 shadowXonChar; /* last value set as XON char in Edgeport */
  94. __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
  95. __u8 validDataMask;
  96. __u32 baudRate;
  97. bool open;
  98. bool openPending;
  99. bool commandPending;
  100. bool closePending;
  101. bool chaseResponsePending;
  102. wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
  103. wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
  104. wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
  105. wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
  106. struct async_icount icount;
  107. struct usb_serial_port *port; /* loop back to the owner of this object */
  108. };
  109. /* This structure holds all of the individual device information */
  110. struct edgeport_serial {
  111. char name[MAX_NAME_LEN+2]; /* string name of this device */
  112. struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
  113. struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
  114. struct edgeport_product_info product_info; /* Product Info */
  115. struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
  116. int is_epic; /* flag if EPiC device or not */
  117. __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
  118. unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
  119. struct urb *interrupt_read_urb; /* our interrupt urb */
  120. __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
  121. unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
  122. struct urb *read_urb; /* our bulk read urb */
  123. bool read_in_progress;
  124. spinlock_t es_lock;
  125. __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
  126. __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
  127. enum RXSTATE rxState; /* the current state of the bulk receive processor */
  128. __u8 rxHeader1; /* receive header byte 1 */
  129. __u8 rxHeader2; /* receive header byte 2 */
  130. __u8 rxHeader3; /* receive header byte 3 */
  131. __u8 rxPort; /* the port that we are currently receiving data for */
  132. __u8 rxStatusCode; /* the receive status code */
  133. __u8 rxStatusParam; /* the receive status paramater */
  134. __s16 rxBytesRemaining; /* the number of port bytes left to read */
  135. struct usb_serial *serial; /* loop back to the owner of this object */
  136. };
  137. /* baud rate information */
  138. struct divisor_table_entry {
  139. __u32 BaudRate;
  140. __u16 Divisor;
  141. };
  142. /*
  143. * Define table of divisors for Rev A EdgePort/4 hardware
  144. * These assume a 3.6864MHz crystal, the standard /16, and
  145. * MCR.7 = 0.
  146. */
  147. static const struct divisor_table_entry divisor_table[] = {
  148. { 50, 4608},
  149. { 75, 3072},
  150. { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
  151. { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
  152. { 150, 1536},
  153. { 300, 768},
  154. { 600, 384},
  155. { 1200, 192},
  156. { 1800, 128},
  157. { 2400, 96},
  158. { 4800, 48},
  159. { 7200, 32},
  160. { 9600, 24},
  161. { 14400, 16},
  162. { 19200, 12},
  163. { 38400, 6},
  164. { 57600, 4},
  165. { 115200, 2},
  166. { 230400, 1},
  167. };
  168. /* local variables */
  169. static bool debug;
  170. /* Number of outstanding Command Write Urbs */
  171. static atomic_t CmdUrbs = ATOMIC_INIT(0);
  172. /* local function prototypes */
  173. /* function prototypes for all URB callbacks */
  174. static void edge_interrupt_callback(struct urb *urb);
  175. static void edge_bulk_in_callback(struct urb *urb);
  176. static void edge_bulk_out_data_callback(struct urb *urb);
  177. static void edge_bulk_out_cmd_callback(struct urb *urb);
  178. /* function prototypes for the usbserial callbacks */
  179. static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
  180. static void edge_close(struct usb_serial_port *port);
  181. static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
  182. const unsigned char *buf, int count);
  183. static int edge_write_room(struct tty_struct *tty);
  184. static int edge_chars_in_buffer(struct tty_struct *tty);
  185. static void edge_throttle(struct tty_struct *tty);
  186. static void edge_unthrottle(struct tty_struct *tty);
  187. static void edge_set_termios(struct tty_struct *tty,
  188. struct usb_serial_port *port,
  189. struct ktermios *old_termios);
  190. static int edge_ioctl(struct tty_struct *tty,
  191. unsigned int cmd, unsigned long arg);
  192. static void edge_break(struct tty_struct *tty, int break_state);
  193. static int edge_tiocmget(struct tty_struct *tty);
  194. static int edge_tiocmset(struct tty_struct *tty,
  195. unsigned int set, unsigned int clear);
  196. static int edge_get_icount(struct tty_struct *tty,
  197. struct serial_icounter_struct *icount);
  198. static int edge_startup(struct usb_serial *serial);
  199. static void edge_disconnect(struct usb_serial *serial);
  200. static void edge_release(struct usb_serial *serial);
  201. #include "io_tables.h" /* all of the devices that this driver supports */
  202. /* function prototypes for all of our local functions */
  203. static void process_rcvd_data(struct edgeport_serial *edge_serial,
  204. unsigned char *buffer, __u16 bufferLength);
  205. static void process_rcvd_status(struct edgeport_serial *edge_serial,
  206. __u8 byte2, __u8 byte3);
  207. static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
  208. unsigned char *data, int length);
  209. static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
  210. static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
  211. __u8 lsr, __u8 data);
  212. static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
  213. __u8 param);
  214. static int calc_baud_rate_divisor(int baud_rate, int *divisor);
  215. static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
  216. int baudRate);
  217. static void change_port_settings(struct tty_struct *tty,
  218. struct edgeport_port *edge_port,
  219. struct ktermios *old_termios);
  220. static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
  221. __u8 regNum, __u8 regValue);
  222. static int write_cmd_usb(struct edgeport_port *edge_port,
  223. unsigned char *buffer, int writeLength);
  224. static void send_more_port_data(struct edgeport_serial *edge_serial,
  225. struct edgeport_port *edge_port);
  226. static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  227. __u16 length, const __u8 *data);
  228. static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  229. __u16 length, __u8 *data);
  230. static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  231. __u16 length, const __u8 *data);
  232. static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
  233. static void get_boot_desc(struct edgeport_serial *edge_serial);
  234. static void load_application_firmware(struct edgeport_serial *edge_serial);
  235. static void unicode_to_ascii(char *string, int buflen,
  236. __le16 *unicode, int unicode_size);
  237. /* ************************************************************************ */
  238. /* ************************************************************************ */
  239. /* ************************************************************************ */
  240. /* ************************************************************************ */
  241. /************************************************************************
  242. * *
  243. * update_edgeport_E2PROM() Compare current versions of *
  244. * Boot ROM and Manufacture *
  245. * Descriptors with versions *
  246. * embedded in this driver *
  247. * *
  248. ************************************************************************/
  249. static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
  250. {
  251. __u32 BootCurVer;
  252. __u32 BootNewVer;
  253. __u8 BootMajorVersion;
  254. __u8 BootMinorVersion;
  255. __u16 BootBuildNumber;
  256. __u32 Bootaddr;
  257. const struct ihex_binrec *rec;
  258. const struct firmware *fw;
  259. const char *fw_name;
  260. int response;
  261. switch (edge_serial->product_info.iDownloadFile) {
  262. case EDGE_DOWNLOAD_FILE_I930:
  263. fw_name = "edgeport/boot.fw";
  264. break;
  265. case EDGE_DOWNLOAD_FILE_80251:
  266. fw_name = "edgeport/boot2.fw";
  267. break;
  268. default:
  269. return;
  270. }
  271. response = request_ihex_firmware(&fw, fw_name,
  272. &edge_serial->serial->dev->dev);
  273. if (response) {
  274. printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
  275. fw_name, response);
  276. return;
  277. }
  278. rec = (const struct ihex_binrec *)fw->data;
  279. BootMajorVersion = rec->data[0];
  280. BootMinorVersion = rec->data[1];
  281. BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
  282. /* Check Boot Image Version */
  283. BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
  284. (edge_serial->boot_descriptor.MinorVersion << 16) +
  285. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
  286. BootNewVer = (BootMajorVersion << 24) +
  287. (BootMinorVersion << 16) +
  288. BootBuildNumber;
  289. dbg("Current Boot Image version %d.%d.%d",
  290. edge_serial->boot_descriptor.MajorVersion,
  291. edge_serial->boot_descriptor.MinorVersion,
  292. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
  293. if (BootNewVer > BootCurVer) {
  294. dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
  295. edge_serial->boot_descriptor.MajorVersion,
  296. edge_serial->boot_descriptor.MinorVersion,
  297. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
  298. BootMajorVersion, BootMinorVersion, BootBuildNumber);
  299. dbg("Downloading new Boot Image");
  300. for (rec = ihex_next_binrec(rec); rec;
  301. rec = ihex_next_binrec(rec)) {
  302. Bootaddr = be32_to_cpu(rec->addr);
  303. response = rom_write(edge_serial->serial,
  304. Bootaddr >> 16,
  305. Bootaddr & 0xFFFF,
  306. be16_to_cpu(rec->len),
  307. &rec->data[0]);
  308. if (response < 0) {
  309. dev_err(&edge_serial->serial->dev->dev,
  310. "rom_write failed (%x, %x, %d)\n",
  311. Bootaddr >> 16, Bootaddr & 0xFFFF,
  312. be16_to_cpu(rec->len));
  313. break;
  314. }
  315. }
  316. } else {
  317. dbg("Boot Image -- already up to date");
  318. }
  319. release_firmware(fw);
  320. }
  321. #if 0
  322. /************************************************************************
  323. *
  324. * Get string descriptor from device
  325. *
  326. ************************************************************************/
  327. static int get_string_desc(struct usb_device *dev, int Id,
  328. struct usb_string_descriptor **pRetDesc)
  329. {
  330. struct usb_string_descriptor StringDesc;
  331. struct usb_string_descriptor *pStringDesc;
  332. dbg("%s - USB String ID = %d", __func__, Id);
  333. if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
  334. sizeof(StringDesc)))
  335. return 0;
  336. pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
  337. if (!pStringDesc)
  338. return -1;
  339. if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
  340. StringDesc.bLength)) {
  341. kfree(pStringDesc);
  342. return -1;
  343. }
  344. *pRetDesc = pStringDesc;
  345. return 0;
  346. }
  347. #endif
  348. static void dump_product_info(struct edgeport_product_info *product_info)
  349. {
  350. /* Dump Product Info structure */
  351. dbg("**Product Information:");
  352. dbg(" ProductId %x", product_info->ProductId);
  353. dbg(" NumPorts %d", product_info->NumPorts);
  354. dbg(" ProdInfoVer %d", product_info->ProdInfoVer);
  355. dbg(" IsServer %d", product_info->IsServer);
  356. dbg(" IsRS232 %d", product_info->IsRS232);
  357. dbg(" IsRS422 %d", product_info->IsRS422);
  358. dbg(" IsRS485 %d", product_info->IsRS485);
  359. dbg(" RomSize %d", product_info->RomSize);
  360. dbg(" RamSize %d", product_info->RamSize);
  361. dbg(" CpuRev %x", product_info->CpuRev);
  362. dbg(" BoardRev %x", product_info->BoardRev);
  363. dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
  364. product_info->BootMinorVersion,
  365. le16_to_cpu(product_info->BootBuildNumber));
  366. dbg(" FirmwareMajorVersion %d.%d.%d",
  367. product_info->FirmwareMajorVersion,
  368. product_info->FirmwareMinorVersion,
  369. le16_to_cpu(product_info->FirmwareBuildNumber));
  370. dbg(" ManufactureDescDate %d/%d/%d",
  371. product_info->ManufactureDescDate[0],
  372. product_info->ManufactureDescDate[1],
  373. product_info->ManufactureDescDate[2]+1900);
  374. dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
  375. dbg(" EpicVer %d", product_info->EpicVer);
  376. }
  377. static void get_product_info(struct edgeport_serial *edge_serial)
  378. {
  379. struct edgeport_product_info *product_info = &edge_serial->product_info;
  380. memset(product_info, 0, sizeof(struct edgeport_product_info));
  381. product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
  382. product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
  383. product_info->ProdInfoVer = 0;
  384. product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
  385. product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
  386. product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
  387. product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
  388. product_info->BootMajorVersion =
  389. edge_serial->boot_descriptor.MajorVersion;
  390. product_info->BootMinorVersion =
  391. edge_serial->boot_descriptor.MinorVersion;
  392. product_info->BootBuildNumber =
  393. edge_serial->boot_descriptor.BuildNumber;
  394. memcpy(product_info->ManufactureDescDate,
  395. edge_serial->manuf_descriptor.DescDate,
  396. sizeof(edge_serial->manuf_descriptor.DescDate));
  397. /* check if this is 2nd generation hardware */
  398. if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
  399. & ION_DEVICE_ID_80251_NETCHIP)
  400. product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
  401. else
  402. product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
  403. /* Determine Product type and set appropriate flags */
  404. switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
  405. case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
  406. case ION_DEVICE_ID_EDGEPORT_4T:
  407. case ION_DEVICE_ID_EDGEPORT_4:
  408. case ION_DEVICE_ID_EDGEPORT_2:
  409. case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
  410. case ION_DEVICE_ID_EDGEPORT_8:
  411. case ION_DEVICE_ID_EDGEPORT_421:
  412. case ION_DEVICE_ID_EDGEPORT_21:
  413. case ION_DEVICE_ID_EDGEPORT_2_DIN:
  414. case ION_DEVICE_ID_EDGEPORT_4_DIN:
  415. case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
  416. product_info->IsRS232 = 1;
  417. break;
  418. case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
  419. product_info->IsRS422 = 1;
  420. product_info->IsRS485 = 1;
  421. break;
  422. case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
  423. case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
  424. product_info->IsRS422 = 1;
  425. break;
  426. }
  427. dump_product_info(product_info);
  428. }
  429. static int get_epic_descriptor(struct edgeport_serial *ep)
  430. {
  431. int result;
  432. struct usb_serial *serial = ep->serial;
  433. struct edgeport_product_info *product_info = &ep->product_info;
  434. struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
  435. struct edge_compatibility_bits *bits;
  436. ep->is_epic = 0;
  437. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  438. USB_REQUEST_ION_GET_EPIC_DESC,
  439. 0xC0, 0x00, 0x00,
  440. &ep->epic_descriptor,
  441. sizeof(struct edge_compatibility_descriptor),
  442. 300);
  443. dbg("%s result = %d", __func__, result);
  444. if (result > 0) {
  445. ep->is_epic = 1;
  446. memset(product_info, 0, sizeof(struct edgeport_product_info));
  447. product_info->NumPorts = epic->NumPorts;
  448. product_info->ProdInfoVer = 0;
  449. product_info->FirmwareMajorVersion = epic->MajorVersion;
  450. product_info->FirmwareMinorVersion = epic->MinorVersion;
  451. product_info->FirmwareBuildNumber = epic->BuildNumber;
  452. product_info->iDownloadFile = epic->iDownloadFile;
  453. product_info->EpicVer = epic->EpicVer;
  454. product_info->Epic = epic->Supports;
  455. product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
  456. dump_product_info(product_info);
  457. bits = &ep->epic_descriptor.Supports;
  458. dbg("**EPIC descriptor:");
  459. dbg(" VendEnableSuspend: %s", bits->VendEnableSuspend ? "TRUE": "FALSE");
  460. dbg(" IOSPOpen : %s", bits->IOSPOpen ? "TRUE": "FALSE");
  461. dbg(" IOSPClose : %s", bits->IOSPClose ? "TRUE": "FALSE");
  462. dbg(" IOSPChase : %s", bits->IOSPChase ? "TRUE": "FALSE");
  463. dbg(" IOSPSetRxFlow : %s", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
  464. dbg(" IOSPSetTxFlow : %s", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
  465. dbg(" IOSPSetXChar : %s", bits->IOSPSetXChar ? "TRUE": "FALSE");
  466. dbg(" IOSPRxCheck : %s", bits->IOSPRxCheck ? "TRUE": "FALSE");
  467. dbg(" IOSPSetClrBreak : %s", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
  468. dbg(" IOSPWriteMCR : %s", bits->IOSPWriteMCR ? "TRUE": "FALSE");
  469. dbg(" IOSPWriteLCR : %s", bits->IOSPWriteLCR ? "TRUE": "FALSE");
  470. dbg(" IOSPSetBaudRate : %s", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
  471. dbg(" TrueEdgeport : %s", bits->TrueEdgeport ? "TRUE": "FALSE");
  472. }
  473. return result;
  474. }
  475. /************************************************************************/
  476. /************************************************************************/
  477. /* U S B C A L L B A C K F U N C T I O N S */
  478. /* U S B C A L L B A C K F U N C T I O N S */
  479. /************************************************************************/
  480. /************************************************************************/
  481. /*****************************************************************************
  482. * edge_interrupt_callback
  483. * this is the callback function for when we have received data on the
  484. * interrupt endpoint.
  485. *****************************************************************************/
  486. static void edge_interrupt_callback(struct urb *urb)
  487. {
  488. struct edgeport_serial *edge_serial = urb->context;
  489. struct edgeport_port *edge_port;
  490. struct usb_serial_port *port;
  491. struct tty_struct *tty;
  492. unsigned char *data = urb->transfer_buffer;
  493. int length = urb->actual_length;
  494. int bytes_avail;
  495. int position;
  496. int txCredits;
  497. int portNumber;
  498. int result;
  499. int status = urb->status;
  500. dbg("%s", __func__);
  501. switch (status) {
  502. case 0:
  503. /* success */
  504. break;
  505. case -ECONNRESET:
  506. case -ENOENT:
  507. case -ESHUTDOWN:
  508. /* this urb is terminated, clean up */
  509. dbg("%s - urb shutting down with status: %d",
  510. __func__, status);
  511. return;
  512. default:
  513. dbg("%s - nonzero urb status received: %d", __func__, status);
  514. goto exit;
  515. }
  516. /* process this interrupt-read even if there are no ports open */
  517. if (length) {
  518. usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
  519. __func__, length, data);
  520. if (length > 1) {
  521. bytes_avail = data[0] | (data[1] << 8);
  522. if (bytes_avail) {
  523. spin_lock(&edge_serial->es_lock);
  524. edge_serial->rxBytesAvail += bytes_avail;
  525. dbg("%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d", __func__, bytes_avail, edge_serial->rxBytesAvail, edge_serial->read_in_progress);
  526. if (edge_serial->rxBytesAvail > 0 &&
  527. !edge_serial->read_in_progress) {
  528. dbg("%s - posting a read", __func__);
  529. edge_serial->read_in_progress = true;
  530. /* we have pending bytes on the
  531. bulk in pipe, send a request */
  532. result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
  533. if (result) {
  534. dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __func__, result);
  535. edge_serial->read_in_progress = false;
  536. }
  537. }
  538. spin_unlock(&edge_serial->es_lock);
  539. }
  540. }
  541. /* grab the txcredits for the ports if available */
  542. position = 2;
  543. portNumber = 0;
  544. while ((position < length) &&
  545. (portNumber < edge_serial->serial->num_ports)) {
  546. txCredits = data[position] | (data[position+1] << 8);
  547. if (txCredits) {
  548. port = edge_serial->serial->port[portNumber];
  549. edge_port = usb_get_serial_port_data(port);
  550. if (edge_port->open) {
  551. spin_lock(&edge_port->ep_lock);
  552. edge_port->txCredits += txCredits;
  553. spin_unlock(&edge_port->ep_lock);
  554. dbg("%s - txcredits for port%d = %d",
  555. __func__, portNumber,
  556. edge_port->txCredits);
  557. /* tell the tty driver that something
  558. has changed */
  559. tty = tty_port_tty_get(
  560. &edge_port->port->port);
  561. if (tty) {
  562. tty_wakeup(tty);
  563. tty_kref_put(tty);
  564. }
  565. /* Since we have more credit, check
  566. if more data can be sent */
  567. send_more_port_data(edge_serial,
  568. edge_port);
  569. }
  570. }
  571. position += 2;
  572. ++portNumber;
  573. }
  574. }
  575. exit:
  576. result = usb_submit_urb(urb, GFP_ATOMIC);
  577. if (result)
  578. dev_err(&urb->dev->dev,
  579. "%s - Error %d submitting control urb\n",
  580. __func__, result);
  581. }
  582. /*****************************************************************************
  583. * edge_bulk_in_callback
  584. * this is the callback function for when we have received data on the
  585. * bulk in endpoint.
  586. *****************************************************************************/
  587. static void edge_bulk_in_callback(struct urb *urb)
  588. {
  589. struct edgeport_serial *edge_serial = urb->context;
  590. unsigned char *data = urb->transfer_buffer;
  591. int retval;
  592. __u16 raw_data_length;
  593. int status = urb->status;
  594. dbg("%s", __func__);
  595. if (status) {
  596. dbg("%s - nonzero read bulk status received: %d",
  597. __func__, status);
  598. edge_serial->read_in_progress = false;
  599. return;
  600. }
  601. if (urb->actual_length == 0) {
  602. dbg("%s - read bulk callback with no data", __func__);
  603. edge_serial->read_in_progress = false;
  604. return;
  605. }
  606. raw_data_length = urb->actual_length;
  607. usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
  608. __func__, raw_data_length, data);
  609. spin_lock(&edge_serial->es_lock);
  610. /* decrement our rxBytes available by the number that we just got */
  611. edge_serial->rxBytesAvail -= raw_data_length;
  612. dbg("%s - Received = %d, rxBytesAvail %d", __func__,
  613. raw_data_length, edge_serial->rxBytesAvail);
  614. process_rcvd_data(edge_serial, data, urb->actual_length);
  615. /* check to see if there's any more data for us to read */
  616. if (edge_serial->rxBytesAvail > 0) {
  617. dbg("%s - posting a read", __func__);
  618. retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
  619. if (retval) {
  620. dev_err(&urb->dev->dev,
  621. "%s - usb_submit_urb(read bulk) failed, "
  622. "retval = %d\n", __func__, retval);
  623. edge_serial->read_in_progress = false;
  624. }
  625. } else {
  626. edge_serial->read_in_progress = false;
  627. }
  628. spin_unlock(&edge_serial->es_lock);
  629. }
  630. /*****************************************************************************
  631. * edge_bulk_out_data_callback
  632. * this is the callback function for when we have finished sending
  633. * serial data on the bulk out endpoint.
  634. *****************************************************************************/
  635. static void edge_bulk_out_data_callback(struct urb *urb)
  636. {
  637. struct edgeport_port *edge_port = urb->context;
  638. struct tty_struct *tty;
  639. int status = urb->status;
  640. dbg("%s", __func__);
  641. if (status) {
  642. dbg("%s - nonzero write bulk status received: %d",
  643. __func__, status);
  644. }
  645. tty = tty_port_tty_get(&edge_port->port->port);
  646. if (tty && edge_port->open) {
  647. /* let the tty driver wakeup if it has a special
  648. write_wakeup function */
  649. tty_wakeup(tty);
  650. }
  651. tty_kref_put(tty);
  652. /* Release the Write URB */
  653. edge_port->write_in_progress = false;
  654. /* Check if more data needs to be sent */
  655. send_more_port_data((struct edgeport_serial *)
  656. (usb_get_serial_data(edge_port->port->serial)), edge_port);
  657. }
  658. /*****************************************************************************
  659. * BulkOutCmdCallback
  660. * this is the callback function for when we have finished sending a
  661. * command on the bulk out endpoint.
  662. *****************************************************************************/
  663. static void edge_bulk_out_cmd_callback(struct urb *urb)
  664. {
  665. struct edgeport_port *edge_port = urb->context;
  666. struct tty_struct *tty;
  667. int status = urb->status;
  668. dbg("%s", __func__);
  669. atomic_dec(&CmdUrbs);
  670. dbg("%s - FREE URB %p (outstanding %d)", __func__,
  671. urb, atomic_read(&CmdUrbs));
  672. /* clean up the transfer buffer */
  673. kfree(urb->transfer_buffer);
  674. /* Free the command urb */
  675. usb_free_urb(urb);
  676. if (status) {
  677. dbg("%s - nonzero write bulk status received: %d",
  678. __func__, status);
  679. return;
  680. }
  681. /* Get pointer to tty */
  682. tty = tty_port_tty_get(&edge_port->port->port);
  683. /* tell the tty driver that something has changed */
  684. if (tty && edge_port->open)
  685. tty_wakeup(tty);
  686. tty_kref_put(tty);
  687. /* we have completed the command */
  688. edge_port->commandPending = false;
  689. wake_up(&edge_port->wait_command);
  690. }
  691. /*****************************************************************************
  692. * Driver tty interface functions
  693. *****************************************************************************/
  694. /*****************************************************************************
  695. * SerialOpen
  696. * this function is called by the tty driver when a port is opened
  697. * If successful, we return 0
  698. * Otherwise we return a negative error number.
  699. *****************************************************************************/
  700. static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
  701. {
  702. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  703. struct usb_serial *serial;
  704. struct edgeport_serial *edge_serial;
  705. int response;
  706. dbg("%s - port %d", __func__, port->number);
  707. if (edge_port == NULL)
  708. return -ENODEV;
  709. /* see if we've set up our endpoint info yet (can't set it up
  710. in edge_startup as the structures were not set up at that time.) */
  711. serial = port->serial;
  712. edge_serial = usb_get_serial_data(serial);
  713. if (edge_serial == NULL)
  714. return -ENODEV;
  715. if (edge_serial->interrupt_in_buffer == NULL) {
  716. struct usb_serial_port *port0 = serial->port[0];
  717. /* not set up yet, so do it now */
  718. edge_serial->interrupt_in_buffer =
  719. port0->interrupt_in_buffer;
  720. edge_serial->interrupt_in_endpoint =
  721. port0->interrupt_in_endpointAddress;
  722. edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
  723. edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
  724. edge_serial->bulk_in_endpoint =
  725. port0->bulk_in_endpointAddress;
  726. edge_serial->read_urb = port0->read_urb;
  727. edge_serial->bulk_out_endpoint =
  728. port0->bulk_out_endpointAddress;
  729. /* set up our interrupt urb */
  730. usb_fill_int_urb(edge_serial->interrupt_read_urb,
  731. serial->dev,
  732. usb_rcvintpipe(serial->dev,
  733. port0->interrupt_in_endpointAddress),
  734. port0->interrupt_in_buffer,
  735. edge_serial->interrupt_read_urb->transfer_buffer_length,
  736. edge_interrupt_callback, edge_serial,
  737. edge_serial->interrupt_read_urb->interval);
  738. /* set up our bulk in urb */
  739. usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
  740. usb_rcvbulkpipe(serial->dev,
  741. port0->bulk_in_endpointAddress),
  742. port0->bulk_in_buffer,
  743. edge_serial->read_urb->transfer_buffer_length,
  744. edge_bulk_in_callback, edge_serial);
  745. edge_serial->read_in_progress = false;
  746. /* start interrupt read for this edgeport
  747. * this interrupt will continue as long
  748. * as the edgeport is connected */
  749. response = usb_submit_urb(edge_serial->interrupt_read_urb,
  750. GFP_KERNEL);
  751. if (response) {
  752. dev_err(&port->dev,
  753. "%s - Error %d submitting control urb\n",
  754. __func__, response);
  755. }
  756. }
  757. /* initialize our wait queues */
  758. init_waitqueue_head(&edge_port->wait_open);
  759. init_waitqueue_head(&edge_port->wait_chase);
  760. init_waitqueue_head(&edge_port->delta_msr_wait);
  761. init_waitqueue_head(&edge_port->wait_command);
  762. /* initialize our icount structure */
  763. memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
  764. /* initialize our port settings */
  765. edge_port->txCredits = 0; /* Can't send any data yet */
  766. /* Must always set this bit to enable ints! */
  767. edge_port->shadowMCR = MCR_MASTER_IE;
  768. edge_port->chaseResponsePending = false;
  769. /* send a open port command */
  770. edge_port->openPending = true;
  771. edge_port->open = false;
  772. response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
  773. if (response < 0) {
  774. dev_err(&port->dev, "%s - error sending open port command\n",
  775. __func__);
  776. edge_port->openPending = false;
  777. return -ENODEV;
  778. }
  779. /* now wait for the port to be completely opened */
  780. wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
  781. OPEN_TIMEOUT);
  782. if (!edge_port->open) {
  783. /* open timed out */
  784. dbg("%s - open timedout", __func__);
  785. edge_port->openPending = false;
  786. return -ENODEV;
  787. }
  788. /* create the txfifo */
  789. edge_port->txfifo.head = 0;
  790. edge_port->txfifo.tail = 0;
  791. edge_port->txfifo.count = 0;
  792. edge_port->txfifo.size = edge_port->maxTxCredits;
  793. edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
  794. if (!edge_port->txfifo.fifo) {
  795. dbg("%s - no memory", __func__);
  796. edge_close(port);
  797. return -ENOMEM;
  798. }
  799. /* Allocate a URB for the write */
  800. edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  801. edge_port->write_in_progress = false;
  802. if (!edge_port->write_urb) {
  803. dbg("%s - no memory", __func__);
  804. edge_close(port);
  805. return -ENOMEM;
  806. }
  807. dbg("%s(%d) - Initialize TX fifo to %d bytes",
  808. __func__, port->number, edge_port->maxTxCredits);
  809. dbg("%s exited", __func__);
  810. return 0;
  811. }
  812. /************************************************************************
  813. *
  814. * block_until_chase_response
  815. *
  816. * This function will block the close until one of the following:
  817. * 1. Response to our Chase comes from Edgeport
  818. * 2. A timeout of 10 seconds without activity has expired
  819. * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
  820. *
  821. ************************************************************************/
  822. static void block_until_chase_response(struct edgeport_port *edge_port)
  823. {
  824. DEFINE_WAIT(wait);
  825. __u16 lastCredits;
  826. int timeout = 1*HZ;
  827. int loop = 10;
  828. while (1) {
  829. /* Save Last credits */
  830. lastCredits = edge_port->txCredits;
  831. /* Did we get our Chase response */
  832. if (!edge_port->chaseResponsePending) {
  833. dbg("%s - Got Chase Response", __func__);
  834. /* did we get all of our credit back? */
  835. if (edge_port->txCredits == edge_port->maxTxCredits) {
  836. dbg("%s - Got all credits", __func__);
  837. return;
  838. }
  839. }
  840. /* Block the thread for a while */
  841. prepare_to_wait(&edge_port->wait_chase, &wait,
  842. TASK_UNINTERRUPTIBLE);
  843. schedule_timeout(timeout);
  844. finish_wait(&edge_port->wait_chase, &wait);
  845. if (lastCredits == edge_port->txCredits) {
  846. /* No activity.. count down. */
  847. loop--;
  848. if (loop == 0) {
  849. edge_port->chaseResponsePending = false;
  850. dbg("%s - Chase TIMEOUT", __func__);
  851. return;
  852. }
  853. } else {
  854. /* Reset timeout value back to 10 seconds */
  855. dbg("%s - Last %d, Current %d", __func__,
  856. lastCredits, edge_port->txCredits);
  857. loop = 10;
  858. }
  859. }
  860. }
  861. /************************************************************************
  862. *
  863. * block_until_tx_empty
  864. *
  865. * This function will block the close until one of the following:
  866. * 1. TX count are 0
  867. * 2. The edgeport has stopped
  868. * 3. A timeout of 3 seconds without activity has expired
  869. *
  870. ************************************************************************/
  871. static void block_until_tx_empty(struct edgeport_port *edge_port)
  872. {
  873. DEFINE_WAIT(wait);
  874. struct TxFifo *fifo = &edge_port->txfifo;
  875. __u32 lastCount;
  876. int timeout = HZ/10;
  877. int loop = 30;
  878. while (1) {
  879. /* Save Last count */
  880. lastCount = fifo->count;
  881. /* Is the Edgeport Buffer empty? */
  882. if (lastCount == 0) {
  883. dbg("%s - TX Buffer Empty", __func__);
  884. return;
  885. }
  886. /* Block the thread for a while */
  887. prepare_to_wait(&edge_port->wait_chase, &wait,
  888. TASK_UNINTERRUPTIBLE);
  889. schedule_timeout(timeout);
  890. finish_wait(&edge_port->wait_chase, &wait);
  891. dbg("%s wait", __func__);
  892. if (lastCount == fifo->count) {
  893. /* No activity.. count down. */
  894. loop--;
  895. if (loop == 0) {
  896. dbg("%s - TIMEOUT", __func__);
  897. return;
  898. }
  899. } else {
  900. /* Reset timeout value back to seconds */
  901. loop = 30;
  902. }
  903. }
  904. }
  905. /*****************************************************************************
  906. * edge_close
  907. * this function is called by the tty driver when a port is closed
  908. *****************************************************************************/
  909. static void edge_close(struct usb_serial_port *port)
  910. {
  911. struct edgeport_serial *edge_serial;
  912. struct edgeport_port *edge_port;
  913. int status;
  914. dbg("%s - port %d", __func__, port->number);
  915. edge_serial = usb_get_serial_data(port->serial);
  916. edge_port = usb_get_serial_port_data(port);
  917. if (edge_serial == NULL || edge_port == NULL)
  918. return;
  919. /* block until tx is empty */
  920. block_until_tx_empty(edge_port);
  921. edge_port->closePending = true;
  922. if ((!edge_serial->is_epic) ||
  923. ((edge_serial->is_epic) &&
  924. (edge_serial->epic_descriptor.Supports.IOSPChase))) {
  925. /* flush and chase */
  926. edge_port->chaseResponsePending = true;
  927. dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
  928. status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
  929. if (status == 0)
  930. /* block until chase finished */
  931. block_until_chase_response(edge_port);
  932. else
  933. edge_port->chaseResponsePending = false;
  934. }
  935. if ((!edge_serial->is_epic) ||
  936. ((edge_serial->is_epic) &&
  937. (edge_serial->epic_descriptor.Supports.IOSPClose))) {
  938. /* close the port */
  939. dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __func__);
  940. send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
  941. }
  942. /* port->close = true; */
  943. edge_port->closePending = false;
  944. edge_port->open = false;
  945. edge_port->openPending = false;
  946. usb_kill_urb(edge_port->write_urb);
  947. if (edge_port->write_urb) {
  948. /* if this urb had a transfer buffer already
  949. (old transfer) free it */
  950. kfree(edge_port->write_urb->transfer_buffer);
  951. usb_free_urb(edge_port->write_urb);
  952. edge_port->write_urb = NULL;
  953. }
  954. kfree(edge_port->txfifo.fifo);
  955. edge_port->txfifo.fifo = NULL;
  956. dbg("%s exited", __func__);
  957. }
  958. /*****************************************************************************
  959. * SerialWrite
  960. * this function is called by the tty driver when data should be written
  961. * to the port.
  962. * If successful, we return the number of bytes written, otherwise we
  963. * return a negative error number.
  964. *****************************************************************************/
  965. static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
  966. const unsigned char *data, int count)
  967. {
  968. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  969. struct TxFifo *fifo;
  970. int copySize;
  971. int bytesleft;
  972. int firsthalf;
  973. int secondhalf;
  974. unsigned long flags;
  975. dbg("%s - port %d", __func__, port->number);
  976. if (edge_port == NULL)
  977. return -ENODEV;
  978. /* get a pointer to the Tx fifo */
  979. fifo = &edge_port->txfifo;
  980. spin_lock_irqsave(&edge_port->ep_lock, flags);
  981. /* calculate number of bytes to put in fifo */
  982. copySize = min((unsigned int)count,
  983. (edge_port->txCredits - fifo->count));
  984. dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes",
  985. __func__, port->number, count,
  986. edge_port->txCredits - fifo->count, copySize);
  987. /* catch writes of 0 bytes which the tty driver likes to give us,
  988. and when txCredits is empty */
  989. if (copySize == 0) {
  990. dbg("%s - copySize = Zero", __func__);
  991. goto finish_write;
  992. }
  993. /* queue the data
  994. * since we can never overflow the buffer we do not have to check for a
  995. * full condition
  996. *
  997. * the copy is done is two parts -- first fill to the end of the buffer
  998. * then copy the reset from the start of the buffer
  999. */
  1000. bytesleft = fifo->size - fifo->head;
  1001. firsthalf = min(bytesleft, copySize);
  1002. dbg("%s - copy %d bytes of %d into fifo ", __func__,
  1003. firsthalf, bytesleft);
  1004. /* now copy our data */
  1005. memcpy(&fifo->fifo[fifo->head], data, firsthalf);
  1006. usb_serial_debug_data(debug, &port->dev, __func__,
  1007. firsthalf, &fifo->fifo[fifo->head]);
  1008. /* update the index and size */
  1009. fifo->head += firsthalf;
  1010. fifo->count += firsthalf;
  1011. /* wrap the index */
  1012. if (fifo->head == fifo->size)
  1013. fifo->head = 0;
  1014. secondhalf = copySize-firsthalf;
  1015. if (secondhalf) {
  1016. dbg("%s - copy rest of data %d", __func__, secondhalf);
  1017. memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
  1018. usb_serial_debug_data(debug, &port->dev, __func__,
  1019. secondhalf, &fifo->fifo[fifo->head]);
  1020. /* update the index and size */
  1021. fifo->count += secondhalf;
  1022. fifo->head += secondhalf;
  1023. /* No need to check for wrap since we can not get to end of
  1024. * the fifo in this part
  1025. */
  1026. }
  1027. finish_write:
  1028. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1029. send_more_port_data((struct edgeport_serial *)
  1030. usb_get_serial_data(port->serial), edge_port);
  1031. dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __func__,
  1032. copySize, edge_port->txCredits, fifo->count);
  1033. return copySize;
  1034. }
  1035. /************************************************************************
  1036. *
  1037. * send_more_port_data()
  1038. *
  1039. * This routine attempts to write additional UART transmit data
  1040. * to a port over the USB bulk pipe. It is called (1) when new
  1041. * data has been written to a port's TxBuffer from higher layers
  1042. * (2) when the peripheral sends us additional TxCredits indicating
  1043. * that it can accept more Tx data for a given port; and (3) when
  1044. * a bulk write completes successfully and we want to see if we
  1045. * can transmit more.
  1046. *
  1047. ************************************************************************/
  1048. static void send_more_port_data(struct edgeport_serial *edge_serial,
  1049. struct edgeport_port *edge_port)
  1050. {
  1051. struct TxFifo *fifo = &edge_port->txfifo;
  1052. struct urb *urb;
  1053. unsigned char *buffer;
  1054. int status;
  1055. int count;
  1056. int bytesleft;
  1057. int firsthalf;
  1058. int secondhalf;
  1059. unsigned long flags;
  1060. dbg("%s(%d)", __func__, edge_port->port->number);
  1061. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1062. if (edge_port->write_in_progress ||
  1063. !edge_port->open ||
  1064. (fifo->count == 0)) {
  1065. dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d",
  1066. __func__, edge_port->port->number,
  1067. fifo->count, edge_port->write_in_progress);
  1068. goto exit_send;
  1069. }
  1070. /* since the amount of data in the fifo will always fit into the
  1071. * edgeport buffer we do not need to check the write length
  1072. *
  1073. * Do we have enough credits for this port to make it worthwhile
  1074. * to bother queueing a write. If it's too small, say a few bytes,
  1075. * it's better to wait for more credits so we can do a larger write.
  1076. */
  1077. if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
  1078. dbg("%s(%d) Not enough credit - fifo %d TxCredit %d",
  1079. __func__, edge_port->port->number, fifo->count,
  1080. edge_port->txCredits);
  1081. goto exit_send;
  1082. }
  1083. /* lock this write */
  1084. edge_port->write_in_progress = true;
  1085. /* get a pointer to the write_urb */
  1086. urb = edge_port->write_urb;
  1087. /* make sure transfer buffer is freed */
  1088. kfree(urb->transfer_buffer);
  1089. urb->transfer_buffer = NULL;
  1090. /* build the data header for the buffer and port that we are about
  1091. to send out */
  1092. count = fifo->count;
  1093. buffer = kmalloc(count+2, GFP_ATOMIC);
  1094. if (buffer == NULL) {
  1095. dev_err_console(edge_port->port,
  1096. "%s - no more kernel memory...\n", __func__);
  1097. edge_port->write_in_progress = false;
  1098. goto exit_send;
  1099. }
  1100. buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
  1101. - edge_port->port->serial->minor, count);
  1102. buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
  1103. - edge_port->port->serial->minor, count);
  1104. /* now copy our data */
  1105. bytesleft = fifo->size - fifo->tail;
  1106. firsthalf = min(bytesleft, count);
  1107. memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
  1108. fifo->tail += firsthalf;
  1109. fifo->count -= firsthalf;
  1110. if (fifo->tail == fifo->size)
  1111. fifo->tail = 0;
  1112. secondhalf = count-firsthalf;
  1113. if (secondhalf) {
  1114. memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
  1115. secondhalf);
  1116. fifo->tail += secondhalf;
  1117. fifo->count -= secondhalf;
  1118. }
  1119. if (count)
  1120. usb_serial_debug_data(debug, &edge_port->port->dev,
  1121. __func__, count, &buffer[2]);
  1122. /* fill up the urb with all of our data and submit it */
  1123. usb_fill_bulk_urb(urb, edge_serial->serial->dev,
  1124. usb_sndbulkpipe(edge_serial->serial->dev,
  1125. edge_serial->bulk_out_endpoint),
  1126. buffer, count+2,
  1127. edge_bulk_out_data_callback, edge_port);
  1128. /* decrement the number of credits we have by the number we just sent */
  1129. edge_port->txCredits -= count;
  1130. edge_port->icount.tx += count;
  1131. status = usb_submit_urb(urb, GFP_ATOMIC);
  1132. if (status) {
  1133. /* something went wrong */
  1134. dev_err_console(edge_port->port,
  1135. "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
  1136. __func__, status);
  1137. edge_port->write_in_progress = false;
  1138. /* revert the credits as something bad happened. */
  1139. edge_port->txCredits += count;
  1140. edge_port->icount.tx -= count;
  1141. }
  1142. dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d",
  1143. __func__, count, edge_port->txCredits, fifo->count);
  1144. exit_send:
  1145. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1146. }
  1147. /*****************************************************************************
  1148. * edge_write_room
  1149. * this function is called by the tty driver when it wants to know how
  1150. * many bytes of data we can accept for a specific port. If successful,
  1151. * we return the amount of room that we have for this port (the txCredits)
  1152. * otherwise we return a negative error number.
  1153. *****************************************************************************/
  1154. static int edge_write_room(struct tty_struct *tty)
  1155. {
  1156. struct usb_serial_port *port = tty->driver_data;
  1157. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1158. int room;
  1159. unsigned long flags;
  1160. dbg("%s", __func__);
  1161. if (edge_port == NULL)
  1162. return 0;
  1163. if (edge_port->closePending)
  1164. return 0;
  1165. dbg("%s - port %d", __func__, port->number);
  1166. if (!edge_port->open) {
  1167. dbg("%s - port not opened", __func__);
  1168. return 0;
  1169. }
  1170. /* total of both buffers is still txCredit */
  1171. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1172. room = edge_port->txCredits - edge_port->txfifo.count;
  1173. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1174. dbg("%s - returns %d", __func__, room);
  1175. return room;
  1176. }
  1177. /*****************************************************************************
  1178. * edge_chars_in_buffer
  1179. * this function is called by the tty driver when it wants to know how
  1180. * many bytes of data we currently have outstanding in the port (data that
  1181. * has been written, but hasn't made it out the port yet)
  1182. * If successful, we return the number of bytes left to be written in the
  1183. * system,
  1184. * Otherwise we return a negative error number.
  1185. *****************************************************************************/
  1186. static int edge_chars_in_buffer(struct tty_struct *tty)
  1187. {
  1188. struct usb_serial_port *port = tty->driver_data;
  1189. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1190. int num_chars;
  1191. unsigned long flags;
  1192. dbg("%s", __func__);
  1193. if (edge_port == NULL)
  1194. return 0;
  1195. if (edge_port->closePending)
  1196. return 0;
  1197. if (!edge_port->open) {
  1198. dbg("%s - port not opened", __func__);
  1199. return 0;
  1200. }
  1201. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1202. num_chars = edge_port->maxTxCredits - edge_port->txCredits +
  1203. edge_port->txfifo.count;
  1204. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1205. if (num_chars) {
  1206. dbg("%s(port %d) - returns %d", __func__,
  1207. port->number, num_chars);
  1208. }
  1209. return num_chars;
  1210. }
  1211. /*****************************************************************************
  1212. * SerialThrottle
  1213. * this function is called by the tty driver when it wants to stop the data
  1214. * being read from the port.
  1215. *****************************************************************************/
  1216. static void edge_throttle(struct tty_struct *tty)
  1217. {
  1218. struct usb_serial_port *port = tty->driver_data;
  1219. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1220. int status;
  1221. dbg("%s - port %d", __func__, port->number);
  1222. if (edge_port == NULL)
  1223. return;
  1224. if (!edge_port->open) {
  1225. dbg("%s - port not opened", __func__);
  1226. return;
  1227. }
  1228. /* if we are implementing XON/XOFF, send the stop character */
  1229. if (I_IXOFF(tty)) {
  1230. unsigned char stop_char = STOP_CHAR(tty);
  1231. status = edge_write(tty, port, &stop_char, 1);
  1232. if (status <= 0)
  1233. return;
  1234. }
  1235. /* if we are implementing RTS/CTS, toggle that line */
  1236. if (tty->termios->c_cflag & CRTSCTS) {
  1237. edge_port->shadowMCR &= ~MCR_RTS;
  1238. status = send_cmd_write_uart_register(edge_port, MCR,
  1239. edge_port->shadowMCR);
  1240. if (status != 0)
  1241. return;
  1242. }
  1243. }
  1244. /*****************************************************************************
  1245. * edge_unthrottle
  1246. * this function is called by the tty driver when it wants to resume the
  1247. * data being read from the port (called after SerialThrottle is called)
  1248. *****************************************************************************/
  1249. static void edge_unthrottle(struct tty_struct *tty)
  1250. {
  1251. struct usb_serial_port *port = tty->driver_data;
  1252. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1253. int status;
  1254. dbg("%s - port %d", __func__, port->number);
  1255. if (edge_port == NULL)
  1256. return;
  1257. if (!edge_port->open) {
  1258. dbg("%s - port not opened", __func__);
  1259. return;
  1260. }
  1261. /* if we are implementing XON/XOFF, send the start character */
  1262. if (I_IXOFF(tty)) {
  1263. unsigned char start_char = START_CHAR(tty);
  1264. status = edge_write(tty, port, &start_char, 1);
  1265. if (status <= 0)
  1266. return;
  1267. }
  1268. /* if we are implementing RTS/CTS, toggle that line */
  1269. if (tty->termios->c_cflag & CRTSCTS) {
  1270. edge_port->shadowMCR |= MCR_RTS;
  1271. send_cmd_write_uart_register(edge_port, MCR,
  1272. edge_port->shadowMCR);
  1273. }
  1274. }
  1275. /*****************************************************************************
  1276. * SerialSetTermios
  1277. * this function is called by the tty driver when it wants to change
  1278. * the termios structure
  1279. *****************************************************************************/
  1280. static void edge_set_termios(struct tty_struct *tty,
  1281. struct usb_serial_port *port, struct ktermios *old_termios)
  1282. {
  1283. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1284. unsigned int cflag;
  1285. cflag = tty->termios->c_cflag;
  1286. dbg("%s - clfag %08x iflag %08x", __func__,
  1287. tty->termios->c_cflag, tty->termios->c_iflag);
  1288. dbg("%s - old clfag %08x old iflag %08x", __func__,
  1289. old_termios->c_cflag, old_termios->c_iflag);
  1290. dbg("%s - port %d", __func__, port->number);
  1291. if (edge_port == NULL)
  1292. return;
  1293. if (!edge_port->open) {
  1294. dbg("%s - port not opened", __func__);
  1295. return;
  1296. }
  1297. /* change the port settings to the new ones specified */
  1298. change_port_settings(tty, edge_port, old_termios);
  1299. }
  1300. /*****************************************************************************
  1301. * get_lsr_info - get line status register info
  1302. *
  1303. * Purpose: Let user call ioctl() to get info when the UART physically
  1304. * is emptied. On bus types like RS485, the transmitter must
  1305. * release the bus after transmitting. This must be done when
  1306. * the transmit shift register is empty, not be done when the
  1307. * transmit holding register is empty. This functionality
  1308. * allows an RS485 driver to be written in user space.
  1309. *****************************************************************************/
  1310. static int get_lsr_info(struct edgeport_port *edge_port,
  1311. unsigned int __user *value)
  1312. {
  1313. unsigned int result = 0;
  1314. unsigned long flags;
  1315. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1316. if (edge_port->maxTxCredits == edge_port->txCredits &&
  1317. edge_port->txfifo.count == 0) {
  1318. dbg("%s -- Empty", __func__);
  1319. result = TIOCSER_TEMT;
  1320. }
  1321. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1322. if (copy_to_user(value, &result, sizeof(int)))
  1323. return -EFAULT;
  1324. return 0;
  1325. }
  1326. static int edge_tiocmset(struct tty_struct *tty,
  1327. unsigned int set, unsigned int clear)
  1328. {
  1329. struct usb_serial_port *port = tty->driver_data;
  1330. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1331. unsigned int mcr;
  1332. dbg("%s - port %d", __func__, port->number);
  1333. mcr = edge_port->shadowMCR;
  1334. if (set & TIOCM_RTS)
  1335. mcr |= MCR_RTS;
  1336. if (set & TIOCM_DTR)
  1337. mcr |= MCR_DTR;
  1338. if (set & TIOCM_LOOP)
  1339. mcr |= MCR_LOOPBACK;
  1340. if (clear & TIOCM_RTS)
  1341. mcr &= ~MCR_RTS;
  1342. if (clear & TIOCM_DTR)
  1343. mcr &= ~MCR_DTR;
  1344. if (clear & TIOCM_LOOP)
  1345. mcr &= ~MCR_LOOPBACK;
  1346. edge_port->shadowMCR = mcr;
  1347. send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
  1348. return 0;
  1349. }
  1350. static int edge_tiocmget(struct tty_struct *tty)
  1351. {
  1352. struct usb_serial_port *port = tty->driver_data;
  1353. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1354. unsigned int result = 0;
  1355. unsigned int msr;
  1356. unsigned int mcr;
  1357. dbg("%s - port %d", __func__, port->number);
  1358. msr = edge_port->shadowMSR;
  1359. mcr = edge_port->shadowMCR;
  1360. result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  1361. | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  1362. | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  1363. | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
  1364. | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  1365. | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  1366. dbg("%s -- %x", __func__, result);
  1367. return result;
  1368. }
  1369. static int edge_get_icount(struct tty_struct *tty,
  1370. struct serial_icounter_struct *icount)
  1371. {
  1372. struct usb_serial_port *port = tty->driver_data;
  1373. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1374. struct async_icount cnow;
  1375. cnow = edge_port->icount;
  1376. icount->cts = cnow.cts;
  1377. icount->dsr = cnow.dsr;
  1378. icount->rng = cnow.rng;
  1379. icount->dcd = cnow.dcd;
  1380. icount->rx = cnow.rx;
  1381. icount->tx = cnow.tx;
  1382. icount->frame = cnow.frame;
  1383. icount->overrun = cnow.overrun;
  1384. icount->parity = cnow.parity;
  1385. icount->brk = cnow.brk;
  1386. icount->buf_overrun = cnow.buf_overrun;
  1387. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d",
  1388. __func__, port->number, icount->rx, icount->tx);
  1389. return 0;
  1390. }
  1391. static int get_serial_info(struct edgeport_port *edge_port,
  1392. struct serial_struct __user *retinfo)
  1393. {
  1394. struct serial_struct tmp;
  1395. if (!retinfo)
  1396. return -EFAULT;
  1397. memset(&tmp, 0, sizeof(tmp));
  1398. tmp.type = PORT_16550A;
  1399. tmp.line = edge_port->port->serial->minor;
  1400. tmp.port = edge_port->port->number;
  1401. tmp.irq = 0;
  1402. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1403. tmp.xmit_fifo_size = edge_port->maxTxCredits;
  1404. tmp.baud_base = 9600;
  1405. tmp.close_delay = 5*HZ;
  1406. tmp.closing_wait = 30*HZ;
  1407. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1408. return -EFAULT;
  1409. return 0;
  1410. }
  1411. /*****************************************************************************
  1412. * SerialIoctl
  1413. * this function handles any ioctl calls to the driver
  1414. *****************************************************************************/
  1415. static int edge_ioctl(struct tty_struct *tty,
  1416. unsigned int cmd, unsigned long arg)
  1417. {
  1418. struct usb_serial_port *port = tty->driver_data;
  1419. DEFINE_WAIT(wait);
  1420. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1421. struct async_icount cnow;
  1422. struct async_icount cprev;
  1423. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  1424. switch (cmd) {
  1425. case TIOCSERGETLSR:
  1426. dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
  1427. return get_lsr_info(edge_port, (unsigned int __user *) arg);
  1428. case TIOCGSERIAL:
  1429. dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
  1430. return get_serial_info(edge_port, (struct serial_struct __user *) arg);
  1431. case TIOCMIWAIT:
  1432. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  1433. cprev = edge_port->icount;
  1434. while (1) {
  1435. prepare_to_wait(&edge_port->delta_msr_wait,
  1436. &wait, TASK_INTERRUPTIBLE);
  1437. schedule();
  1438. finish_wait(&edge_port->delta_msr_wait, &wait);
  1439. /* see if a signal did it */
  1440. if (signal_pending(current))
  1441. return -ERESTARTSYS;
  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->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 %p (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");