io_edgeport.c 99 KB

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