io_edgeport.c 96 KB

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