io_edgeport.c 92 KB

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