io_edgeport.c 92 KB

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