io_edgeport.c 98 KB

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