io_edgeport.c 96 KB

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