io_edgeport.c 93 KB

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