io_edgeport.c 98 KB

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