io_edgeport.c 100 KB

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