io_edgeport.c 100 KB

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