io_edgeport.c 97 KB

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