io_edgeport.c 92 KB

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