io_edgeport.c 100 KB

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