io_ti.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145
  1. /*
  2. * Edgeport USB Serial Converter driver
  3. *
  4. * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
  5. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Supports the following devices:
  13. * EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
  14. *
  15. * For questions or problems with this driver, contact Inside Out
  16. * Networks technical support, or Peter Berger <pberger@brimson.com>,
  17. * or Al Borchers <alborchers@steinerpoint.com>.
  18. *
  19. * Version history:
  20. *
  21. * July 11, 2002 Removed 4 port device structure since all TI UMP
  22. * chips have only 2 ports
  23. * David Iacovelli (davidi@ionetworks.com)
  24. *
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/errno.h>
  29. #include <linux/init.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 <asm/uaccess.h>
  39. #include <asm/semaphore.h>
  40. #include <linux/usb.h>
  41. #include <linux/usb/serial.h>
  42. #include "io_16654.h"
  43. #include "io_usbvend.h"
  44. #include "io_ti.h"
  45. /*
  46. * Version Information
  47. */
  48. #define DRIVER_VERSION "v0.7mode043006"
  49. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
  50. #define DRIVER_DESC "Edgeport USB Serial Driver"
  51. /* firmware image code */
  52. #define IMAGE_VERSION_NAME PagableOperationalCodeImageVersion
  53. #define IMAGE_ARRAY_NAME PagableOperationalCodeImage
  54. #define IMAGE_SIZE PagableOperationalCodeSize
  55. #include "io_fw_down3.h" /* Define array OperationalCodeImage[] */
  56. #define EPROM_PAGE_SIZE 64
  57. struct edgeport_uart_buf_desc {
  58. __u32 count; // Number of bytes currently in buffer
  59. };
  60. /* different hardware types */
  61. #define HARDWARE_TYPE_930 0
  62. #define HARDWARE_TYPE_TIUMP 1
  63. // IOCTL_PRIVATE_TI_GET_MODE Definitions
  64. #define TI_MODE_CONFIGURING 0 // Device has not entered start device
  65. #define TI_MODE_BOOT 1 // Staying in boot mode
  66. #define TI_MODE_DOWNLOAD 2 // Made it to download mode
  67. #define TI_MODE_TRANSITIONING 3 // Currently in boot mode but transitioning to download mode
  68. /* read urb state */
  69. #define EDGE_READ_URB_RUNNING 0
  70. #define EDGE_READ_URB_STOPPING 1
  71. #define EDGE_READ_URB_STOPPED 2
  72. #define EDGE_LOW_LATENCY 1
  73. #define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
  74. #define EDGE_OUT_BUF_SIZE 1024
  75. /* Product information read from the Edgeport */
  76. struct product_info
  77. {
  78. int TiMode; // Current TI Mode
  79. __u8 hardware_type; // Type of hardware
  80. } __attribute__((packed));
  81. /* circular buffer */
  82. struct edge_buf {
  83. unsigned int buf_size;
  84. char *buf_buf;
  85. char *buf_get;
  86. char *buf_put;
  87. };
  88. struct edgeport_port {
  89. __u16 uart_base;
  90. __u16 dma_address;
  91. __u8 shadow_msr;
  92. __u8 shadow_mcr;
  93. __u8 shadow_lsr;
  94. __u8 lsr_mask;
  95. __u32 ump_read_timeout; /* Number of miliseconds the UMP will
  96. wait without data before completing
  97. a read short */
  98. int baud_rate;
  99. int close_pending;
  100. int lsr_event;
  101. struct edgeport_uart_buf_desc tx;
  102. struct async_icount icount;
  103. wait_queue_head_t delta_msr_wait; /* for handling sleeping while
  104. waiting for msr change to
  105. happen */
  106. struct edgeport_serial *edge_serial;
  107. struct usb_serial_port *port;
  108. __u8 bUartMode; /* Port type, 0: RS232, etc. */
  109. spinlock_t ep_lock;
  110. int ep_read_urb_state;
  111. int ep_write_urb_in_use;
  112. struct edge_buf *ep_out_buf;
  113. };
  114. struct edgeport_serial {
  115. struct product_info product_info;
  116. u8 TI_I2C_Type; // Type of I2C in UMP
  117. u8 TiReadI2C; // Set to TRUE if we have read the I2c in Boot Mode
  118. struct semaphore es_sem;
  119. int num_ports_open;
  120. struct usb_serial *serial;
  121. };
  122. /* Devices that this driver supports */
  123. static struct usb_device_id edgeport_1port_id_table [] = {
  124. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
  125. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
  126. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
  127. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
  128. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
  129. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
  130. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
  131. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
  132. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
  133. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
  134. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
  135. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
  136. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
  137. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
  138. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
  139. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
  140. { }
  141. };
  142. static struct usb_device_id edgeport_2port_id_table [] = {
  143. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
  144. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
  145. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
  146. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
  147. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
  148. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
  149. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
  150. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
  151. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
  152. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
  153. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
  154. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
  155. /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
  156. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
  157. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
  158. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
  159. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
  160. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
  161. { }
  162. };
  163. /* Devices that this driver supports */
  164. static struct usb_device_id id_table_combined [] = {
  165. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
  166. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
  167. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
  168. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
  169. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
  170. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
  171. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
  172. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
  173. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
  174. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
  175. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
  176. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
  177. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
  178. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
  179. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
  180. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
  181. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
  182. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
  183. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
  184. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
  185. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
  186. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
  187. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
  188. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
  189. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
  190. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
  191. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
  192. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
  193. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
  194. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
  195. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
  196. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
  197. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
  198. { }
  199. };
  200. MODULE_DEVICE_TABLE (usb, id_table_combined);
  201. static struct usb_driver io_driver = {
  202. .name = "io_ti",
  203. .probe = usb_serial_probe,
  204. .disconnect = usb_serial_disconnect,
  205. .id_table = id_table_combined,
  206. .no_dynamic_id = 1,
  207. };
  208. static struct EDGE_FIRMWARE_VERSION_INFO OperationalCodeImageVersion;
  209. static int debug;
  210. static int TIStayInBootMode = 0;
  211. static int low_latency = EDGE_LOW_LATENCY;
  212. static int closing_wait = EDGE_CLOSING_WAIT;
  213. static int ignore_cpu_rev = 0;
  214. static int default_uart_mode = 0; /* RS232 */
  215. static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
  216. static void stop_read(struct edgeport_port *edge_port);
  217. static int restart_read(struct edgeport_port *edge_port);
  218. static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios);
  219. static void edge_send(struct usb_serial_port *port);
  220. /* sysfs attributes */
  221. static int edge_create_sysfs_attrs(struct usb_serial_port *port);
  222. static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
  223. /* circular buffer */
  224. static struct edge_buf *edge_buf_alloc(unsigned int size);
  225. static void edge_buf_free(struct edge_buf *eb);
  226. static void edge_buf_clear(struct edge_buf *eb);
  227. static unsigned int edge_buf_data_avail(struct edge_buf *eb);
  228. static unsigned int edge_buf_space_avail(struct edge_buf *eb);
  229. static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
  230. unsigned int count);
  231. static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
  232. unsigned int count);
  233. static int TIReadVendorRequestSync (struct usb_device *dev,
  234. __u8 request,
  235. __u16 value,
  236. __u16 index,
  237. u8 *data,
  238. int size)
  239. {
  240. int status;
  241. status = usb_control_msg (dev,
  242. usb_rcvctrlpipe(dev, 0),
  243. request,
  244. (USB_TYPE_VENDOR |
  245. USB_RECIP_DEVICE |
  246. USB_DIR_IN),
  247. value,
  248. index,
  249. data,
  250. size,
  251. 1000);
  252. if (status < 0)
  253. return status;
  254. if (status != size) {
  255. dbg ("%s - wanted to write %d, but only wrote %d",
  256. __FUNCTION__, size, status);
  257. return -ECOMM;
  258. }
  259. return 0;
  260. }
  261. static int TISendVendorRequestSync (struct usb_device *dev,
  262. __u8 request,
  263. __u16 value,
  264. __u16 index,
  265. u8 *data,
  266. int size)
  267. {
  268. int status;
  269. status = usb_control_msg (dev,
  270. usb_sndctrlpipe(dev, 0),
  271. request,
  272. (USB_TYPE_VENDOR |
  273. USB_RECIP_DEVICE |
  274. USB_DIR_OUT),
  275. value,
  276. index,
  277. data,
  278. size,
  279. 1000);
  280. if (status < 0)
  281. return status;
  282. if (status != size) {
  283. dbg ("%s - wanted to write %d, but only wrote %d",
  284. __FUNCTION__, size, status);
  285. return -ECOMM;
  286. }
  287. return 0;
  288. }
  289. static int TIWriteCommandSync (struct usb_device *dev, __u8 command,
  290. __u8 moduleid, __u16 value, u8 *data,
  291. int size)
  292. {
  293. return TISendVendorRequestSync (dev,
  294. command, // Request
  295. value, // wValue
  296. moduleid, // wIndex
  297. data, // TransferBuffer
  298. size); // TransferBufferLength
  299. }
  300. /* clear tx/rx buffers and fifo in TI UMP */
  301. static int TIPurgeDataSync (struct usb_serial_port *port, __u16 mask)
  302. {
  303. int port_number = port->number - port->serial->minor;
  304. dbg ("%s - port %d, mask %x", __FUNCTION__, port_number, mask);
  305. return TIWriteCommandSync (port->serial->dev,
  306. UMPC_PURGE_PORT,
  307. (__u8)(UMPM_UART1_PORT + port_number),
  308. mask,
  309. NULL,
  310. 0);
  311. }
  312. /**
  313. * TIReadDownloadMemory - Read edgeport memory from TI chip
  314. * @dev: usb device pointer
  315. * @start_address: Device CPU address at which to read
  316. * @length: Length of above data
  317. * @address_type: Can read both XDATA and I2C
  318. * @buffer: pointer to input data buffer
  319. */
  320. static int TIReadDownloadMemory(struct usb_device *dev, int start_address,
  321. int length, __u8 address_type, __u8 *buffer)
  322. {
  323. int status = 0;
  324. __u8 read_length;
  325. __be16 be_start_address;
  326. dbg ("%s - @ %x for %d", __FUNCTION__, start_address, length);
  327. /* Read in blocks of 64 bytes
  328. * (TI firmware can't handle more than 64 byte reads)
  329. */
  330. while (length) {
  331. if (length > 64)
  332. read_length= 64;
  333. else
  334. read_length = (__u8)length;
  335. if (read_length > 1) {
  336. dbg ("%s - @ %x for %d", __FUNCTION__,
  337. start_address, read_length);
  338. }
  339. be_start_address = cpu_to_be16 (start_address);
  340. status = TIReadVendorRequestSync (dev,
  341. UMPC_MEMORY_READ, // Request
  342. (__u16)address_type, // wValue (Address type)
  343. (__force __u16)be_start_address, // wIndex (Address to read)
  344. buffer, // TransferBuffer
  345. read_length); // TransferBufferLength
  346. if (status) {
  347. dbg ("%s - ERROR %x", __FUNCTION__, status);
  348. return status;
  349. }
  350. if (read_length > 1) {
  351. usb_serial_debug_data(debug, &dev->dev, __FUNCTION__,
  352. read_length, buffer);
  353. }
  354. /* Update pointers/length */
  355. start_address += read_length;
  356. buffer += read_length;
  357. length -= read_length;
  358. }
  359. return status;
  360. }
  361. static int TIReadRam (struct usb_device *dev, int start_address, int length, __u8 *buffer)
  362. {
  363. return TIReadDownloadMemory (dev,
  364. start_address,
  365. length,
  366. DTK_ADDR_SPACE_XDATA,
  367. buffer);
  368. }
  369. /* Read edgeport memory to a given block */
  370. static int TIReadBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 * buffer)
  371. {
  372. int status = 0;
  373. int i;
  374. for (i=0; i< length; i++) {
  375. status = TIReadVendorRequestSync (serial->serial->dev,
  376. UMPC_MEMORY_READ, // Request
  377. serial->TI_I2C_Type, // wValue (Address type)
  378. (__u16)(start_address+i), // wIndex
  379. &buffer[i], // TransferBuffer
  380. 0x01); // TransferBufferLength
  381. if (status) {
  382. dbg ("%s - ERROR %x", __FUNCTION__, status);
  383. return status;
  384. }
  385. }
  386. dbg ("%s - start_address = %x, length = %d", __FUNCTION__, start_address, length);
  387. usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer);
  388. serial->TiReadI2C = 1;
  389. return status;
  390. }
  391. /* Write given block to TI EPROM memory */
  392. static int TIWriteBootMemory (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
  393. {
  394. int status = 0;
  395. int i;
  396. __u8 temp;
  397. /* Must do a read before write */
  398. if (!serial->TiReadI2C) {
  399. status = TIReadBootMemory(serial, 0, 1, &temp);
  400. if (status)
  401. return status;
  402. }
  403. for (i=0; i < length; ++i) {
  404. status = TISendVendorRequestSync (serial->serial->dev,
  405. UMPC_MEMORY_WRITE, // Request
  406. buffer[i], // wValue
  407. (__u16)(i+start_address), // wIndex
  408. NULL, // TransferBuffer
  409. 0); // TransferBufferLength
  410. if (status)
  411. return status;
  412. }
  413. dbg ("%s - start_sddr = %x, length = %d", __FUNCTION__, start_address, length);
  414. usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, length, buffer);
  415. return status;
  416. }
  417. /* Write edgeport I2C memory to TI chip */
  418. static int TIWriteDownloadI2C (struct edgeport_serial *serial, int start_address, int length, __u8 address_type, __u8 *buffer)
  419. {
  420. int status = 0;
  421. int write_length;
  422. __be16 be_start_address;
  423. /* We can only send a maximum of 1 aligned byte page at a time */
  424. /* calulate the number of bytes left in the first page */
  425. write_length = EPROM_PAGE_SIZE - (start_address & (EPROM_PAGE_SIZE - 1));
  426. if (write_length > length)
  427. write_length = length;
  428. dbg ("%s - BytesInFirstPage Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
  429. usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer);
  430. /* Write first page */
  431. be_start_address = cpu_to_be16 (start_address);
  432. status = TISendVendorRequestSync (serial->serial->dev,
  433. UMPC_MEMORY_WRITE, // Request
  434. (__u16)address_type, // wValue
  435. (__force __u16)be_start_address, // wIndex
  436. buffer, // TransferBuffer
  437. write_length);
  438. if (status) {
  439. dbg ("%s - ERROR %d", __FUNCTION__, status);
  440. return status;
  441. }
  442. length -= write_length;
  443. start_address += write_length;
  444. buffer += write_length;
  445. /* We should be aligned now -- can write max page size bytes at a time */
  446. while (length) {
  447. if (length > EPROM_PAGE_SIZE)
  448. write_length = EPROM_PAGE_SIZE;
  449. else
  450. write_length = length;
  451. dbg ("%s - Page Write Addr = %x, length = %d", __FUNCTION__, start_address, write_length);
  452. usb_serial_debug_data(debug, &serial->serial->dev->dev, __FUNCTION__, write_length, buffer);
  453. /* Write next page */
  454. be_start_address = cpu_to_be16 (start_address);
  455. status = TISendVendorRequestSync (serial->serial->dev,
  456. UMPC_MEMORY_WRITE, // Request
  457. (__u16)address_type, // wValue
  458. (__force __u16)be_start_address, // wIndex
  459. buffer, // TransferBuffer
  460. write_length); // TransferBufferLength
  461. if (status) {
  462. dev_err (&serial->serial->dev->dev, "%s - ERROR %d\n", __FUNCTION__, status);
  463. return status;
  464. }
  465. length -= write_length;
  466. start_address += write_length;
  467. buffer += write_length;
  468. }
  469. return status;
  470. }
  471. /* Examine the UMP DMA registers and LSR
  472. *
  473. * Check the MSBit of the X and Y DMA byte count registers.
  474. * A zero in this bit indicates that the TX DMA buffers are empty
  475. * then check the TX Empty bit in the UART.
  476. */
  477. static int TIIsTxActive (struct edgeport_port *port)
  478. {
  479. int status;
  480. struct out_endpoint_desc_block *oedb;
  481. __u8 *lsr;
  482. int bytes_left = 0;
  483. oedb = kmalloc (sizeof (* oedb), GFP_KERNEL);
  484. if (!oedb) {
  485. dev_err (&port->port->dev, "%s - out of memory\n", __FUNCTION__);
  486. return -ENOMEM;
  487. }
  488. lsr = kmalloc (1, GFP_KERNEL); /* Sigh, that's right, just one byte,
  489. as not all platforms can do DMA
  490. from stack */
  491. if (!lsr) {
  492. kfree(oedb);
  493. return -ENOMEM;
  494. }
  495. /* Read the DMA Count Registers */
  496. status = TIReadRam (port->port->serial->dev,
  497. port->dma_address,
  498. sizeof( *oedb),
  499. (void *)oedb);
  500. if (status)
  501. goto exit_is_tx_active;
  502. dbg ("%s - XByteCount 0x%X", __FUNCTION__, oedb->XByteCount);
  503. /* and the LSR */
  504. status = TIReadRam (port->port->serial->dev,
  505. port->uart_base + UMPMEM_OFFS_UART_LSR,
  506. 1,
  507. lsr);
  508. if (status)
  509. goto exit_is_tx_active;
  510. dbg ("%s - LSR = 0x%X", __FUNCTION__, *lsr);
  511. /* If either buffer has data or we are transmitting then return TRUE */
  512. if ((oedb->XByteCount & 0x80 ) != 0 )
  513. bytes_left += 64;
  514. if ((*lsr & UMP_UART_LSR_TX_MASK ) == 0 )
  515. bytes_left += 1;
  516. /* We return Not Active if we get any kind of error */
  517. exit_is_tx_active:
  518. dbg ("%s - return %d", __FUNCTION__, bytes_left );
  519. kfree(lsr);
  520. kfree(oedb);
  521. return bytes_left;
  522. }
  523. static void TIChasePort(struct edgeport_port *port, unsigned long timeout, int flush)
  524. {
  525. int baud_rate;
  526. struct tty_struct *tty = port->port->tty;
  527. wait_queue_t wait;
  528. unsigned long flags;
  529. if (!timeout)
  530. timeout = (HZ*EDGE_CLOSING_WAIT)/100;
  531. /* wait for data to drain from the buffer */
  532. spin_lock_irqsave(&port->ep_lock, flags);
  533. init_waitqueue_entry(&wait, current);
  534. add_wait_queue(&tty->write_wait, &wait);
  535. for (;;) {
  536. set_current_state(TASK_INTERRUPTIBLE);
  537. if (edge_buf_data_avail(port->ep_out_buf) == 0
  538. || timeout == 0 || signal_pending(current)
  539. || !usb_get_intfdata(port->port->serial->interface)) /* disconnect */
  540. break;
  541. spin_unlock_irqrestore(&port->ep_lock, flags);
  542. timeout = schedule_timeout(timeout);
  543. spin_lock_irqsave(&port->ep_lock, flags);
  544. }
  545. set_current_state(TASK_RUNNING);
  546. remove_wait_queue(&tty->write_wait, &wait);
  547. if (flush)
  548. edge_buf_clear(port->ep_out_buf);
  549. spin_unlock_irqrestore(&port->ep_lock, flags);
  550. /* wait for data to drain from the device */
  551. timeout += jiffies;
  552. while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
  553. && usb_get_intfdata(port->port->serial->interface)) { /* not disconnected */
  554. if (!TIIsTxActive(port))
  555. break;
  556. msleep(10);
  557. }
  558. /* disconnected */
  559. if (!usb_get_intfdata(port->port->serial->interface))
  560. return;
  561. /* wait one more character time, based on baud rate */
  562. /* (TIIsTxActive doesn't seem to wait for the last byte) */
  563. if ((baud_rate=port->baud_rate) == 0)
  564. baud_rate = 50;
  565. msleep(max(1,(10000+baud_rate-1)/baud_rate));
  566. }
  567. static int TIChooseConfiguration (struct usb_device *dev)
  568. {
  569. // There may be multiple configurations on this device, in which case
  570. // we would need to read and parse all of them to find out which one
  571. // we want. However, we just support one config at this point,
  572. // configuration # 1, which is Config Descriptor 0.
  573. dbg ("%s - Number of Interfaces = %d", __FUNCTION__, dev->config->desc.bNumInterfaces);
  574. dbg ("%s - MAX Power = %d", __FUNCTION__, dev->config->desc.bMaxPower*2);
  575. if (dev->config->desc.bNumInterfaces != 1) {
  576. dev_err (&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __FUNCTION__);
  577. return -ENODEV;
  578. }
  579. return 0;
  580. }
  581. static int TIReadRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
  582. {
  583. int status;
  584. if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
  585. status = TIReadDownloadMemory (serial->serial->dev,
  586. start_address,
  587. length,
  588. serial->TI_I2C_Type,
  589. buffer);
  590. } else {
  591. status = TIReadBootMemory (serial,
  592. start_address,
  593. length,
  594. buffer);
  595. }
  596. return status;
  597. }
  598. static int TIWriteRom (struct edgeport_serial *serial, int start_address, int length, __u8 *buffer)
  599. {
  600. if (serial->product_info.TiMode == TI_MODE_BOOT)
  601. return TIWriteBootMemory (serial,
  602. start_address,
  603. length,
  604. buffer);
  605. if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
  606. return TIWriteDownloadI2C (serial,
  607. start_address,
  608. length,
  609. serial->TI_I2C_Type,
  610. buffer);
  611. return -EINVAL;
  612. }
  613. /* Read a descriptor header from I2C based on type */
  614. static int TIGetDescriptorAddress (struct edgeport_serial *serial, int desc_type, struct ti_i2c_desc *rom_desc)
  615. {
  616. int start_address;
  617. int status;
  618. /* Search for requested descriptor in I2C */
  619. start_address = 2;
  620. do {
  621. status = TIReadRom (serial,
  622. start_address,
  623. sizeof(struct ti_i2c_desc),
  624. (__u8 *)rom_desc );
  625. if (status)
  626. return 0;
  627. if (rom_desc->Type == desc_type)
  628. return start_address;
  629. start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
  630. } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
  631. return 0;
  632. }
  633. /* Validate descriptor checksum */
  634. static int ValidChecksum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
  635. {
  636. __u16 i;
  637. __u8 cs = 0;
  638. for (i=0; i < rom_desc->Size; i++) {
  639. cs = (__u8)(cs + buffer[i]);
  640. }
  641. if (cs != rom_desc->CheckSum) {
  642. dbg ("%s - Mismatch %x - %x", __FUNCTION__, rom_desc->CheckSum, cs);
  643. return -EINVAL;
  644. }
  645. return 0;
  646. }
  647. /* Make sure that the I2C image is good */
  648. static int TiValidateI2cImage (struct edgeport_serial *serial)
  649. {
  650. struct device *dev = &serial->serial->dev->dev;
  651. int status = 0;
  652. struct ti_i2c_desc *rom_desc;
  653. int start_address = 2;
  654. __u8 *buffer;
  655. __u16 ttype;
  656. rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
  657. if (!rom_desc) {
  658. dev_err (dev, "%s - out of memory\n", __FUNCTION__);
  659. return -ENOMEM;
  660. }
  661. buffer = kmalloc (TI_MAX_I2C_SIZE, GFP_KERNEL);
  662. if (!buffer) {
  663. dev_err (dev, "%s - out of memory when allocating buffer\n", __FUNCTION__);
  664. kfree (rom_desc);
  665. return -ENOMEM;
  666. }
  667. // Read the first byte (Signature0) must be 0x52 or 0x10
  668. status = TIReadRom (serial, 0, 1, buffer);
  669. if (status)
  670. goto ExitTiValidateI2cImage;
  671. if (*buffer != UMP5152 && *buffer != UMP3410) {
  672. dev_err (dev, "%s - invalid buffer signature\n", __FUNCTION__);
  673. status = -ENODEV;
  674. goto ExitTiValidateI2cImage;
  675. }
  676. do {
  677. // Validate the I2C
  678. status = TIReadRom (serial,
  679. start_address,
  680. sizeof(struct ti_i2c_desc),
  681. (__u8 *)rom_desc);
  682. if (status)
  683. break;
  684. if ((start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size) > TI_MAX_I2C_SIZE) {
  685. status = -ENODEV;
  686. dbg ("%s - structure too big, erroring out.", __FUNCTION__);
  687. break;
  688. }
  689. dbg ("%s Type = 0x%x", __FUNCTION__, rom_desc->Type);
  690. // Skip type 2 record
  691. ttype = rom_desc->Type & 0x0f;
  692. if ( ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
  693. && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO ) {
  694. // Read the descriptor data
  695. status = TIReadRom(serial,
  696. start_address+sizeof(struct ti_i2c_desc),
  697. rom_desc->Size,
  698. buffer);
  699. if (status)
  700. break;
  701. status = ValidChecksum(rom_desc, buffer);
  702. if (status)
  703. break;
  704. }
  705. start_address = start_address + sizeof(struct ti_i2c_desc) + rom_desc->Size;
  706. } while ((rom_desc->Type != I2C_DESC_TYPE_ION) && (start_address < TI_MAX_I2C_SIZE));
  707. if ((rom_desc->Type != I2C_DESC_TYPE_ION) || (start_address > TI_MAX_I2C_SIZE))
  708. status = -ENODEV;
  709. ExitTiValidateI2cImage:
  710. kfree (buffer);
  711. kfree (rom_desc);
  712. return status;
  713. }
  714. static int TIReadManufDescriptor (struct edgeport_serial *serial, __u8 *buffer)
  715. {
  716. int status;
  717. int start_address;
  718. struct ti_i2c_desc *rom_desc;
  719. struct edge_ti_manuf_descriptor *desc;
  720. rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
  721. if (!rom_desc) {
  722. dev_err (&serial->serial->dev->dev, "%s - out of memory\n", __FUNCTION__);
  723. return -ENOMEM;
  724. }
  725. start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_ION, rom_desc);
  726. if (!start_address) {
  727. dbg ("%s - Edge Descriptor not found in I2C", __FUNCTION__);
  728. status = -ENODEV;
  729. goto exit;
  730. }
  731. // Read the descriptor data
  732. status = TIReadRom (serial,
  733. start_address+sizeof(struct ti_i2c_desc),
  734. rom_desc->Size,
  735. buffer);
  736. if (status)
  737. goto exit;
  738. status = ValidChecksum(rom_desc, buffer);
  739. desc = (struct edge_ti_manuf_descriptor *)buffer;
  740. dbg ( "%s - IonConfig 0x%x", __FUNCTION__, desc->IonConfig );
  741. dbg ( "%s - Version %d", __FUNCTION__, desc->Version );
  742. dbg ( "%s - Cpu/Board 0x%x", __FUNCTION__, desc->CpuRev_BoardRev );
  743. dbg ( "%s - NumPorts %d", __FUNCTION__, desc->NumPorts );
  744. dbg ( "%s - NumVirtualPorts %d", __FUNCTION__, desc->NumVirtualPorts );
  745. dbg ( "%s - TotalPorts %d", __FUNCTION__, desc->TotalPorts );
  746. exit:
  747. kfree (rom_desc);
  748. return status;
  749. }
  750. /* Build firmware header used for firmware update */
  751. static int BuildI2CFirmwareHeader (__u8 *header, struct device *dev)
  752. {
  753. __u8 *buffer;
  754. int buffer_size;
  755. int i;
  756. __u8 cs = 0;
  757. struct ti_i2c_desc *i2c_header;
  758. struct ti_i2c_image_header *img_header;
  759. struct ti_i2c_firmware_rec *firmware_rec;
  760. // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
  761. // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
  762. // will download the latest firmware (padded to 15.5k) into the UMP ram.
  763. // And finally when the device comes back up in download mode the driver will cause
  764. // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
  765. // the record type from 0xf2 to 0x02.
  766. // Allocate a 15.5k buffer + 2 bytes for version number (Firmware Record)
  767. buffer_size = (((1024 * 16) - 512 )+ sizeof(struct ti_i2c_firmware_rec));
  768. buffer = kmalloc (buffer_size, GFP_KERNEL);
  769. if (!buffer) {
  770. dev_err (dev, "%s - out of memory\n", __FUNCTION__);
  771. return -ENOMEM;
  772. }
  773. // Set entire image of 0xffs
  774. memset (buffer, 0xff, buffer_size);
  775. // Copy version number into firmware record
  776. firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
  777. firmware_rec->Ver_Major = OperationalCodeImageVersion.MajorVersion;
  778. firmware_rec->Ver_Minor = OperationalCodeImageVersion.MinorVersion;
  779. // Pointer to fw_down memory image
  780. img_header = (struct ti_i2c_image_header *)&PagableOperationalCodeImage[0];
  781. memcpy (buffer + sizeof(struct ti_i2c_firmware_rec),
  782. &PagableOperationalCodeImage[sizeof(struct ti_i2c_image_header)],
  783. le16_to_cpu(img_header->Length));
  784. for (i=0; i < buffer_size; i++) {
  785. cs = (__u8)(cs + buffer[i]);
  786. }
  787. kfree (buffer);
  788. // Build new header
  789. i2c_header = (struct ti_i2c_desc *)header;
  790. firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
  791. i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
  792. i2c_header->Size = (__u16)buffer_size;
  793. i2c_header->CheckSum = cs;
  794. firmware_rec->Ver_Major = OperationalCodeImageVersion.MajorVersion;
  795. firmware_rec->Ver_Minor = OperationalCodeImageVersion.MinorVersion;
  796. return 0;
  797. }
  798. /* Try to figure out what type of I2c we have */
  799. static int TIGetI2cTypeInBootMode (struct edgeport_serial *serial)
  800. {
  801. int status;
  802. __u8 data;
  803. // Try to read type 2
  804. status = TIReadVendorRequestSync (serial->serial->dev,
  805. UMPC_MEMORY_READ, // Request
  806. DTK_ADDR_SPACE_I2C_TYPE_II, // wValue (Address type)
  807. 0, // wIndex
  808. &data, // TransferBuffer
  809. 0x01); // TransferBufferLength
  810. if (status)
  811. dbg ("%s - read 2 status error = %d", __FUNCTION__, status);
  812. else
  813. dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
  814. if ((!status) && (data == UMP5152 || data == UMP3410)) {
  815. dbg ("%s - ROM_TYPE_II", __FUNCTION__);
  816. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  817. return 0;
  818. }
  819. // Try to read type 3
  820. status = TIReadVendorRequestSync (serial->serial->dev,
  821. UMPC_MEMORY_READ, // Request
  822. DTK_ADDR_SPACE_I2C_TYPE_III, // wValue (Address type)
  823. 0, // wIndex
  824. &data, // TransferBuffer
  825. 0x01); // TransferBufferLength
  826. if (status)
  827. dbg ("%s - read 3 status error = %d", __FUNCTION__, status);
  828. else
  829. dbg ("%s - read 2 data = 0x%x", __FUNCTION__, data);
  830. if ((!status) && (data == UMP5152 || data == UMP3410)) {
  831. dbg ("%s - ROM_TYPE_III", __FUNCTION__);
  832. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
  833. return 0;
  834. }
  835. dbg ("%s - Unknown", __FUNCTION__);
  836. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  837. return -ENODEV;
  838. }
  839. static int TISendBulkTransferSync (struct usb_serial *serial, void *buffer, int length, int *num_sent)
  840. {
  841. int status;
  842. status = usb_bulk_msg (serial->dev,
  843. usb_sndbulkpipe(serial->dev,
  844. serial->port[0]->bulk_out_endpointAddress),
  845. buffer,
  846. length,
  847. num_sent,
  848. 1000);
  849. return status;
  850. }
  851. /* Download given firmware image to the device (IN BOOT MODE) */
  852. static int TIDownloadCodeImage (struct edgeport_serial *serial, __u8 *image, int image_length)
  853. {
  854. int status = 0;
  855. int pos;
  856. int transfer;
  857. int done;
  858. // Transfer firmware image
  859. for (pos = 0; pos < image_length; ) {
  860. // Read the next buffer from file
  861. transfer = image_length - pos;
  862. if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
  863. transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
  864. // Transfer data
  865. status = TISendBulkTransferSync (serial->serial, &image[pos], transfer, &done);
  866. if (status)
  867. break;
  868. // Advance buffer pointer
  869. pos += done;
  870. }
  871. return status;
  872. }
  873. // FIXME!!!
  874. static int TIConfigureBootDevice (struct usb_device *dev)
  875. {
  876. return 0;
  877. }
  878. /**
  879. * DownloadTIFirmware - Download run-time operating firmware to the TI5052
  880. *
  881. * This routine downloads the main operating code into the TI5052, using the
  882. * boot code already burned into E2PROM or ROM.
  883. */
  884. static int TIDownloadFirmware (struct edgeport_serial *serial)
  885. {
  886. struct device *dev = &serial->serial->dev->dev;
  887. int status = 0;
  888. int start_address;
  889. struct edge_ti_manuf_descriptor *ti_manuf_desc;
  890. struct usb_interface_descriptor *interface;
  891. int download_cur_ver;
  892. int download_new_ver;
  893. /* This routine is entered by both the BOOT mode and the Download mode
  894. * We can determine which code is running by the reading the config
  895. * descriptor and if we have only one bulk pipe it is in boot mode
  896. */
  897. serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
  898. /* Default to type 2 i2c */
  899. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  900. status = TIChooseConfiguration (serial->serial->dev);
  901. if (status)
  902. return status;
  903. interface = &serial->serial->interface->cur_altsetting->desc;
  904. if (!interface) {
  905. dev_err (dev, "%s - no interface set, error!\n", __FUNCTION__);
  906. return -ENODEV;
  907. }
  908. // Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
  909. // if we have more than one endpoint we are definitely in download mode
  910. if (interface->bNumEndpoints > 1)
  911. serial->product_info.TiMode = TI_MODE_DOWNLOAD;
  912. else
  913. // Otherwise we will remain in configuring mode
  914. serial->product_info.TiMode = TI_MODE_CONFIGURING;
  915. // Save Download Version Number
  916. OperationalCodeImageVersion.MajorVersion = PagableOperationalCodeImageVersion.MajorVersion;
  917. OperationalCodeImageVersion.MinorVersion = PagableOperationalCodeImageVersion.MinorVersion;
  918. OperationalCodeImageVersion.BuildNumber = PagableOperationalCodeImageVersion.BuildNumber;
  919. /********************************************************************/
  920. /* Download Mode */
  921. /********************************************************************/
  922. if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
  923. struct ti_i2c_desc *rom_desc;
  924. dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN DOWNLOAD MODE>>>>>>>>>>", __FUNCTION__);
  925. status = TiValidateI2cImage (serial);
  926. if (status) {
  927. dbg ("%s - <<<<<<<<<<<<<<<DOWNLOAD MODE -- BAD I2C >>>>>>>>>>",
  928. __FUNCTION__);
  929. return status;
  930. }
  931. /* Validate Hardware version number
  932. * Read Manufacturing Descriptor from TI Based Edgeport
  933. */
  934. ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
  935. if (!ti_manuf_desc) {
  936. dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
  937. return -ENOMEM;
  938. }
  939. status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
  940. if (status) {
  941. kfree (ti_manuf_desc);
  942. return status;
  943. }
  944. // Check version number of ION descriptor
  945. if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
  946. dbg ( "%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__,
  947. TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
  948. kfree (ti_manuf_desc);
  949. return -EINVAL;
  950. }
  951. rom_desc = kmalloc (sizeof (*rom_desc), GFP_KERNEL);
  952. if (!rom_desc) {
  953. dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
  954. kfree (ti_manuf_desc);
  955. return -ENOMEM;
  956. }
  957. // Search for type 2 record (firmware record)
  958. if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc)) != 0) {
  959. struct ti_i2c_firmware_rec *firmware_version;
  960. __u8 record;
  961. dbg ("%s - Found Type FIRMWARE (Type 2) record", __FUNCTION__);
  962. firmware_version = kmalloc (sizeof (*firmware_version), GFP_KERNEL);
  963. if (!firmware_version) {
  964. dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
  965. kfree (rom_desc);
  966. kfree (ti_manuf_desc);
  967. return -ENOMEM;
  968. }
  969. // Validate version number
  970. // Read the descriptor data
  971. status = TIReadRom (serial,
  972. start_address+sizeof(struct ti_i2c_desc),
  973. sizeof(struct ti_i2c_firmware_rec),
  974. (__u8 *)firmware_version);
  975. if (status) {
  976. kfree (firmware_version);
  977. kfree (rom_desc);
  978. kfree (ti_manuf_desc);
  979. return status;
  980. }
  981. // Check version number of download with current version in I2c
  982. download_cur_ver = (firmware_version->Ver_Major << 8) +
  983. (firmware_version->Ver_Minor);
  984. download_new_ver = (OperationalCodeImageVersion.MajorVersion << 8) +
  985. (OperationalCodeImageVersion.MinorVersion);
  986. dbg ("%s - >>>Firmware Versions Device %d.%d Driver %d.%d",
  987. __FUNCTION__,
  988. firmware_version->Ver_Major,
  989. firmware_version->Ver_Minor,
  990. OperationalCodeImageVersion.MajorVersion,
  991. OperationalCodeImageVersion.MinorVersion);
  992. // Check if we have an old version in the I2C and update if necessary
  993. if (download_cur_ver != download_new_ver) {
  994. dbg ("%s - Update I2C Download from %d.%d to %d.%d",
  995. __FUNCTION__,
  996. firmware_version->Ver_Major,
  997. firmware_version->Ver_Minor,
  998. OperationalCodeImageVersion.MajorVersion,
  999. OperationalCodeImageVersion.MinorVersion);
  1000. // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
  1001. // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
  1002. // will download the latest firmware (padded to 15.5k) into the UMP ram.
  1003. // And finally when the device comes back up in download mode the driver will cause
  1004. // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
  1005. // the record type from 0xf2 to 0x02.
  1006. record = I2C_DESC_TYPE_FIRMWARE_BLANK;
  1007. // Change the I2C Firmware record type to 0xf2 to trigger an update
  1008. status = TIWriteRom (serial,
  1009. start_address,
  1010. sizeof(record),
  1011. &record);
  1012. if (status) {
  1013. kfree (firmware_version);
  1014. kfree (rom_desc);
  1015. kfree (ti_manuf_desc);
  1016. return status;
  1017. }
  1018. // verify the write -- must do this in order for write to
  1019. // complete before we do the hardware reset
  1020. status = TIReadRom (serial,
  1021. start_address,
  1022. sizeof(record),
  1023. &record);
  1024. if (status) {
  1025. kfree (firmware_version);
  1026. kfree (rom_desc);
  1027. kfree (ti_manuf_desc);
  1028. return status;
  1029. }
  1030. if (record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
  1031. dev_err (dev, "%s - error resetting device\n", __FUNCTION__);
  1032. kfree (firmware_version);
  1033. kfree (rom_desc);
  1034. kfree (ti_manuf_desc);
  1035. return -ENODEV;
  1036. }
  1037. dbg ("%s - HARDWARE RESET", __FUNCTION__);
  1038. // Reset UMP -- Back to BOOT MODE
  1039. status = TISendVendorRequestSync (serial->serial->dev,
  1040. UMPC_HARDWARE_RESET, // Request
  1041. 0, // wValue
  1042. 0, // wIndex
  1043. NULL, // TransferBuffer
  1044. 0); // TransferBufferLength
  1045. dbg ( "%s - HARDWARE RESET return %d", __FUNCTION__, status);
  1046. /* return an error on purpose. */
  1047. kfree (firmware_version);
  1048. kfree (rom_desc);
  1049. kfree (ti_manuf_desc);
  1050. return -ENODEV;
  1051. }
  1052. kfree (firmware_version);
  1053. }
  1054. // Search for type 0xF2 record (firmware blank record)
  1055. else if ((start_address = TIGetDescriptorAddress (serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
  1056. #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + sizeof(struct ti_i2c_firmware_rec))
  1057. __u8 *header;
  1058. __u8 *vheader;
  1059. header = kmalloc (HEADER_SIZE, GFP_KERNEL);
  1060. if (!header) {
  1061. dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
  1062. kfree (rom_desc);
  1063. kfree (ti_manuf_desc);
  1064. return -ENOMEM;
  1065. }
  1066. vheader = kmalloc (HEADER_SIZE, GFP_KERNEL);
  1067. if (!vheader) {
  1068. dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
  1069. kfree (header);
  1070. kfree (rom_desc);
  1071. kfree (ti_manuf_desc);
  1072. return -ENOMEM;
  1073. }
  1074. dbg ("%s - Found Type BLANK FIRMWARE (Type F2) record", __FUNCTION__);
  1075. // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
  1076. // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
  1077. // will download the latest firmware (padded to 15.5k) into the UMP ram.
  1078. // And finally when the device comes back up in download mode the driver will cause
  1079. // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
  1080. // the record type from 0xf2 to 0x02.
  1081. status = BuildI2CFirmwareHeader(header, dev);
  1082. if (status) {
  1083. kfree (vheader);
  1084. kfree (header);
  1085. kfree (rom_desc);
  1086. kfree (ti_manuf_desc);
  1087. return status;
  1088. }
  1089. // Update I2C with type 0xf2 record with correct size and checksum
  1090. status = TIWriteRom (serial,
  1091. start_address,
  1092. HEADER_SIZE,
  1093. header);
  1094. if (status) {
  1095. kfree (vheader);
  1096. kfree (header);
  1097. kfree (rom_desc);
  1098. kfree (ti_manuf_desc);
  1099. return status;
  1100. }
  1101. // verify the write -- must do this in order for write to
  1102. // complete before we do the hardware reset
  1103. status = TIReadRom (serial,
  1104. start_address,
  1105. HEADER_SIZE,
  1106. vheader);
  1107. if (status) {
  1108. dbg ("%s - can't read header back", __FUNCTION__);
  1109. kfree (vheader);
  1110. kfree (header);
  1111. kfree (rom_desc);
  1112. kfree (ti_manuf_desc);
  1113. return status;
  1114. }
  1115. if (memcmp(vheader, header, HEADER_SIZE)) {
  1116. dbg ("%s - write download record failed", __FUNCTION__);
  1117. kfree (vheader);
  1118. kfree (header);
  1119. kfree (rom_desc);
  1120. kfree (ti_manuf_desc);
  1121. return status;
  1122. }
  1123. kfree (vheader);
  1124. kfree (header);
  1125. dbg ("%s - Start firmware update", __FUNCTION__);
  1126. // Tell firmware to copy download image into I2C
  1127. status = TISendVendorRequestSync (serial->serial->dev,
  1128. UMPC_COPY_DNLD_TO_I2C, // Request
  1129. 0, // wValue
  1130. 0, // wIndex
  1131. NULL, // TransferBuffer
  1132. 0); // TransferBufferLength
  1133. dbg ("%s - Update complete 0x%x", __FUNCTION__, status);
  1134. if (status) {
  1135. dev_err (dev, "%s - UMPC_COPY_DNLD_TO_I2C failed\n", __FUNCTION__);
  1136. kfree (rom_desc);
  1137. kfree (ti_manuf_desc);
  1138. return status;
  1139. }
  1140. }
  1141. // The device is running the download code
  1142. kfree (rom_desc);
  1143. kfree (ti_manuf_desc);
  1144. return 0;
  1145. }
  1146. /********************************************************************/
  1147. /* Boot Mode */
  1148. /********************************************************************/
  1149. dbg ("%s - <<<<<<<<<<<<<<<RUNNING IN BOOT MODE>>>>>>>>>>>>>>>",
  1150. __FUNCTION__);
  1151. // Configure the TI device so we can use the BULK pipes for download
  1152. status = TIConfigureBootDevice (serial->serial->dev);
  1153. if (status)
  1154. return status;
  1155. if (le16_to_cpu(serial->serial->dev->descriptor.idVendor) != USB_VENDOR_ID_ION) {
  1156. dbg ("%s - VID = 0x%x", __FUNCTION__,
  1157. le16_to_cpu(serial->serial->dev->descriptor.idVendor));
  1158. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  1159. goto StayInBootMode;
  1160. }
  1161. // We have an ION device (I2c Must be programmed)
  1162. // Determine I2C image type
  1163. if (TIGetI2cTypeInBootMode(serial)) {
  1164. goto StayInBootMode;
  1165. }
  1166. // Registry variable set?
  1167. if (TIStayInBootMode) {
  1168. dbg ("%s - TIStayInBootMode", __FUNCTION__);
  1169. goto StayInBootMode;
  1170. }
  1171. // Check for ION Vendor ID and that the I2C is valid
  1172. if (!TiValidateI2cImage(serial)) {
  1173. struct ti_i2c_image_header *header;
  1174. int i;
  1175. __u8 cs = 0;
  1176. __u8 *buffer;
  1177. int buffer_size;
  1178. /* Validate Hardware version number
  1179. * Read Manufacturing Descriptor from TI Based Edgeport
  1180. */
  1181. ti_manuf_desc = kmalloc (sizeof (*ti_manuf_desc), GFP_KERNEL);
  1182. if (!ti_manuf_desc) {
  1183. dev_err (dev, "%s - out of memory.\n", __FUNCTION__);
  1184. return -ENOMEM;
  1185. }
  1186. status = TIReadManufDescriptor (serial, (__u8 *)ti_manuf_desc);
  1187. if (status) {
  1188. kfree (ti_manuf_desc);
  1189. goto StayInBootMode;
  1190. }
  1191. // Check for version 2
  1192. if (!ignore_cpu_rev && TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev) < 2) {
  1193. dbg ("%s - Wrong CPU Rev %d (Must be 2)", __FUNCTION__,
  1194. TI_GET_CPU_REVISION(ti_manuf_desc->CpuRev_BoardRev));
  1195. kfree (ti_manuf_desc);
  1196. goto StayInBootMode;
  1197. }
  1198. kfree (ti_manuf_desc);
  1199. // In order to update the I2C firmware we must change the type 2 record to type 0xF2.
  1200. // This will force the UMP to come up in Boot Mode. Then while in boot mode, the driver
  1201. // will download the latest firmware (padded to 15.5k) into the UMP ram.
  1202. // And finally when the device comes back up in download mode the driver will cause
  1203. // the new firmware to be copied from the UMP Ram to I2C and the firmware will update
  1204. // the record type from 0xf2 to 0x02.
  1205. /*
  1206. * Do we really have to copy the whole firmware image,
  1207. * or could we do this in place!
  1208. */
  1209. // Allocate a 15.5k buffer + 3 byte header
  1210. buffer_size = (((1024 * 16) - 512) + sizeof(struct ti_i2c_image_header));
  1211. buffer = kmalloc (buffer_size, GFP_KERNEL);
  1212. if (!buffer) {
  1213. dev_err (dev, "%s - out of memory\n", __FUNCTION__);
  1214. return -ENOMEM;
  1215. }
  1216. // Initialize the buffer to 0xff (pad the buffer)
  1217. memset (buffer, 0xff, buffer_size);
  1218. memcpy (buffer, &PagableOperationalCodeImage[0], PagableOperationalCodeSize);
  1219. for(i = sizeof(struct ti_i2c_image_header); i < buffer_size; i++) {
  1220. cs = (__u8)(cs + buffer[i]);
  1221. }
  1222. header = (struct ti_i2c_image_header *)buffer;
  1223. // update length and checksum after padding
  1224. header->Length = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_i2c_image_header)));
  1225. header->CheckSum = cs;
  1226. // Download the operational code
  1227. dbg ("%s - Downloading operational code image (TI UMP)", __FUNCTION__);
  1228. status = TIDownloadCodeImage (serial, buffer, buffer_size);
  1229. kfree (buffer);
  1230. if (status) {
  1231. dbg ("%s - Error downloading operational code image", __FUNCTION__);
  1232. return status;
  1233. }
  1234. // Device will reboot
  1235. serial->product_info.TiMode = TI_MODE_TRANSITIONING;
  1236. dbg ("%s - Download successful -- Device rebooting...", __FUNCTION__);
  1237. /* return an error on purpose */
  1238. return -ENODEV;
  1239. }
  1240. StayInBootMode:
  1241. // Eprom is invalid or blank stay in boot mode
  1242. dbg ("%s - <<<<<<<<<<<<<<<STAYING IN BOOT MODE>>>>>>>>>>>>", __FUNCTION__);
  1243. serial->product_info.TiMode = TI_MODE_BOOT;
  1244. return 0;
  1245. }
  1246. static int TISetDtr (struct edgeport_port *port)
  1247. {
  1248. int port_number = port->port->number - port->port->serial->minor;
  1249. dbg ("%s", __FUNCTION__);
  1250. port->shadow_mcr |= MCR_DTR;
  1251. return TIWriteCommandSync (port->port->serial->dev,
  1252. UMPC_SET_CLR_DTR,
  1253. (__u8)(UMPM_UART1_PORT + port_number),
  1254. 1, /* set */
  1255. NULL,
  1256. 0);
  1257. }
  1258. static int TIClearDtr (struct edgeport_port *port)
  1259. {
  1260. int port_number = port->port->number - port->port->serial->minor;
  1261. dbg ("%s", __FUNCTION__);
  1262. port->shadow_mcr &= ~MCR_DTR;
  1263. return TIWriteCommandSync (port->port->serial->dev,
  1264. UMPC_SET_CLR_DTR,
  1265. (__u8)(UMPM_UART1_PORT + port_number),
  1266. 0, /* clear */
  1267. NULL,
  1268. 0);
  1269. }
  1270. static int TISetRts (struct edgeport_port *port)
  1271. {
  1272. int port_number = port->port->number - port->port->serial->minor;
  1273. dbg ("%s", __FUNCTION__);
  1274. port->shadow_mcr |= MCR_RTS;
  1275. return TIWriteCommandSync (port->port->serial->dev,
  1276. UMPC_SET_CLR_RTS,
  1277. (__u8)(UMPM_UART1_PORT + port_number),
  1278. 1, /* set */
  1279. NULL,
  1280. 0);
  1281. }
  1282. static int TIClearRts (struct edgeport_port *port)
  1283. {
  1284. int port_number = port->port->number - port->port->serial->minor;
  1285. dbg ("%s", __FUNCTION__);
  1286. port->shadow_mcr &= ~MCR_RTS;
  1287. return TIWriteCommandSync (port->port->serial->dev,
  1288. UMPC_SET_CLR_RTS,
  1289. (__u8)(UMPM_UART1_PORT + port_number),
  1290. 0, /* clear */
  1291. NULL,
  1292. 0);
  1293. }
  1294. static int TISetLoopBack (struct edgeport_port *port)
  1295. {
  1296. int port_number = port->port->number - port->port->serial->minor;
  1297. dbg ("%s", __FUNCTION__);
  1298. return TIWriteCommandSync (port->port->serial->dev,
  1299. UMPC_SET_CLR_LOOPBACK,
  1300. (__u8)(UMPM_UART1_PORT + port_number),
  1301. 1, /* set */
  1302. NULL,
  1303. 0);
  1304. }
  1305. static int TIClearLoopBack (struct edgeport_port *port)
  1306. {
  1307. int port_number = port->port->number - port->port->serial->minor;
  1308. dbg ("%s", __FUNCTION__);
  1309. return TIWriteCommandSync (port->port->serial->dev,
  1310. UMPC_SET_CLR_LOOPBACK,
  1311. (__u8)(UMPM_UART1_PORT + port_number),
  1312. 0, /* clear */
  1313. NULL,
  1314. 0);
  1315. }
  1316. static int TISetBreak (struct edgeport_port *port)
  1317. {
  1318. int port_number = port->port->number - port->port->serial->minor;
  1319. dbg ("%s", __FUNCTION__);
  1320. return TIWriteCommandSync (port->port->serial->dev,
  1321. UMPC_SET_CLR_BREAK,
  1322. (__u8)(UMPM_UART1_PORT + port_number),
  1323. 1, /* set */
  1324. NULL,
  1325. 0);
  1326. }
  1327. static int TIClearBreak (struct edgeport_port *port)
  1328. {
  1329. int port_number = port->port->number - port->port->serial->minor;
  1330. dbg ("%s", __FUNCTION__);
  1331. return TIWriteCommandSync (port->port->serial->dev,
  1332. UMPC_SET_CLR_BREAK,
  1333. (__u8)(UMPM_UART1_PORT + port_number),
  1334. 0, /* clear */
  1335. NULL,
  1336. 0);
  1337. }
  1338. static int TIRestoreMCR (struct edgeport_port *port, __u8 mcr)
  1339. {
  1340. int status = 0;
  1341. dbg ("%s - %x", __FUNCTION__, mcr);
  1342. if (mcr & MCR_DTR)
  1343. status = TISetDtr (port);
  1344. else
  1345. status = TIClearDtr (port);
  1346. if (status)
  1347. return status;
  1348. if (mcr & MCR_RTS)
  1349. status = TISetRts (port);
  1350. else
  1351. status = TIClearRts (port);
  1352. if (status)
  1353. return status;
  1354. if (mcr & MCR_LOOPBACK)
  1355. status = TISetLoopBack (port);
  1356. else
  1357. status = TIClearLoopBack (port);
  1358. return status;
  1359. }
  1360. /* Convert TI LSR to standard UART flags */
  1361. static __u8 MapLineStatus (__u8 ti_lsr)
  1362. {
  1363. __u8 lsr = 0;
  1364. #define MAP_FLAG(flagUmp, flagUart) \
  1365. if (ti_lsr & flagUmp) \
  1366. lsr |= flagUart;
  1367. MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
  1368. MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
  1369. MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
  1370. MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
  1371. MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* receive data available */
  1372. MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* transmit holding register empty */
  1373. #undef MAP_FLAG
  1374. return lsr;
  1375. }
  1376. static void handle_new_msr (struct edgeport_port *edge_port, __u8 msr)
  1377. {
  1378. struct async_icount *icount;
  1379. struct tty_struct *tty;
  1380. dbg ("%s - %02x", __FUNCTION__, msr);
  1381. if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
  1382. icount = &edge_port->icount;
  1383. /* update input line counters */
  1384. if (msr & EDGEPORT_MSR_DELTA_CTS)
  1385. icount->cts++;
  1386. if (msr & EDGEPORT_MSR_DELTA_DSR)
  1387. icount->dsr++;
  1388. if (msr & EDGEPORT_MSR_DELTA_CD)
  1389. icount->dcd++;
  1390. if (msr & EDGEPORT_MSR_DELTA_RI)
  1391. icount->rng++;
  1392. wake_up_interruptible (&edge_port->delta_msr_wait);
  1393. }
  1394. /* Save the new modem status */
  1395. edge_port->shadow_msr = msr & 0xf0;
  1396. tty = edge_port->port->tty;
  1397. /* handle CTS flow control */
  1398. if (tty && C_CRTSCTS(tty)) {
  1399. if (msr & EDGEPORT_MSR_CTS) {
  1400. tty->hw_stopped = 0;
  1401. tty_wakeup(tty);
  1402. } else {
  1403. tty->hw_stopped = 1;
  1404. }
  1405. }
  1406. return;
  1407. }
  1408. static void handle_new_lsr (struct edgeport_port *edge_port, int lsr_data, __u8 lsr, __u8 data)
  1409. {
  1410. struct async_icount *icount;
  1411. __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
  1412. dbg ("%s - %02x", __FUNCTION__, new_lsr);
  1413. edge_port->shadow_lsr = lsr;
  1414. if (new_lsr & LSR_BREAK) {
  1415. /*
  1416. * Parity and Framing errors only count if they
  1417. * occur exclusive of a break being received.
  1418. */
  1419. new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
  1420. }
  1421. /* Place LSR data byte into Rx buffer */
  1422. if (lsr_data && edge_port->port->tty)
  1423. edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
  1424. /* update input line counters */
  1425. icount = &edge_port->icount;
  1426. if (new_lsr & LSR_BREAK)
  1427. icount->brk++;
  1428. if (new_lsr & LSR_OVER_ERR)
  1429. icount->overrun++;
  1430. if (new_lsr & LSR_PAR_ERR)
  1431. icount->parity++;
  1432. if (new_lsr & LSR_FRM_ERR)
  1433. icount->frame++;
  1434. }
  1435. static void edge_interrupt_callback (struct urb *urb)
  1436. {
  1437. struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
  1438. struct usb_serial_port *port;
  1439. struct edgeport_port *edge_port;
  1440. unsigned char *data = urb->transfer_buffer;
  1441. int length = urb->actual_length;
  1442. int port_number;
  1443. int function;
  1444. int retval;
  1445. __u8 lsr;
  1446. __u8 msr;
  1447. int status = urb->status;
  1448. dbg("%s", __FUNCTION__);
  1449. switch (status) {
  1450. case 0:
  1451. /* success */
  1452. break;
  1453. case -ECONNRESET:
  1454. case -ENOENT:
  1455. case -ESHUTDOWN:
  1456. /* this urb is terminated, clean up */
  1457. dbg("%s - urb shutting down with status: %d",
  1458. __FUNCTION__, status);
  1459. return;
  1460. default:
  1461. dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
  1462. "%d\n", __FUNCTION__, status);
  1463. goto exit;
  1464. }
  1465. if (!length) {
  1466. dbg ("%s - no data in urb", __FUNCTION__);
  1467. goto exit;
  1468. }
  1469. usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, length, data);
  1470. if (length != 2) {
  1471. dbg ("%s - expecting packet of size 2, got %d", __FUNCTION__, length);
  1472. goto exit;
  1473. }
  1474. port_number = TIUMP_GET_PORT_FROM_CODE (data[0]);
  1475. function = TIUMP_GET_FUNC_FROM_CODE (data[0]);
  1476. dbg ("%s - port_number %d, function %d, info 0x%x",
  1477. __FUNCTION__, port_number, function, data[1]);
  1478. port = edge_serial->serial->port[port_number];
  1479. edge_port = usb_get_serial_port_data(port);
  1480. if (!edge_port) {
  1481. dbg ("%s - edge_port not found", __FUNCTION__);
  1482. return;
  1483. }
  1484. switch (function) {
  1485. case TIUMP_INTERRUPT_CODE_LSR:
  1486. lsr = MapLineStatus(data[1]);
  1487. if (lsr & UMP_UART_LSR_DATA_MASK) {
  1488. /* Save the LSR event for bulk read completion routine */
  1489. dbg ("%s - LSR Event Port %u LSR Status = %02x",
  1490. __FUNCTION__, port_number, lsr);
  1491. edge_port->lsr_event = 1;
  1492. edge_port->lsr_mask = lsr;
  1493. } else {
  1494. dbg ("%s - ===== Port %d LSR Status = %02x ======",
  1495. __FUNCTION__, port_number, lsr);
  1496. handle_new_lsr (edge_port, 0, lsr, 0);
  1497. }
  1498. break;
  1499. case TIUMP_INTERRUPT_CODE_MSR: // MSR
  1500. /* Copy MSR from UMP */
  1501. msr = data[1];
  1502. dbg ("%s - ===== Port %u MSR Status = %02x ======\n",
  1503. __FUNCTION__, port_number, msr);
  1504. handle_new_msr (edge_port, msr);
  1505. break;
  1506. default:
  1507. dev_err (&urb->dev->dev, "%s - Unknown Interrupt code from UMP %x\n",
  1508. __FUNCTION__, data[1]);
  1509. break;
  1510. }
  1511. exit:
  1512. retval = usb_submit_urb (urb, GFP_ATOMIC);
  1513. if (retval)
  1514. dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
  1515. __FUNCTION__, retval);
  1516. }
  1517. static void edge_bulk_in_callback (struct urb *urb)
  1518. {
  1519. struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
  1520. unsigned char *data = urb->transfer_buffer;
  1521. struct tty_struct *tty;
  1522. int retval = 0;
  1523. int port_number;
  1524. int status = urb->status;
  1525. dbg("%s", __FUNCTION__);
  1526. switch (status) {
  1527. case 0:
  1528. /* success */
  1529. break;
  1530. case -ECONNRESET:
  1531. case -ENOENT:
  1532. case -ESHUTDOWN:
  1533. /* this urb is terminated, clean up */
  1534. dbg("%s - urb shutting down with status: %d",
  1535. __FUNCTION__, status);
  1536. return;
  1537. default:
  1538. dev_err (&urb->dev->dev,"%s - nonzero read bulk status received: %d\n",
  1539. __FUNCTION__, status);
  1540. }
  1541. if (status == -EPIPE)
  1542. goto exit;
  1543. if (status) {
  1544. dev_err(&urb->dev->dev,"%s - stopping read!\n", __FUNCTION__);
  1545. return;
  1546. }
  1547. port_number = edge_port->port->number - edge_port->port->serial->minor;
  1548. if (edge_port->lsr_event) {
  1549. edge_port->lsr_event = 0;
  1550. dbg ("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
  1551. __FUNCTION__, port_number, edge_port->lsr_mask, *data);
  1552. handle_new_lsr (edge_port, 1, edge_port->lsr_mask, *data);
  1553. /* Adjust buffer length/pointer */
  1554. --urb->actual_length;
  1555. ++data;
  1556. }
  1557. tty = edge_port->port->tty;
  1558. if (tty && urb->actual_length) {
  1559. usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, urb->actual_length, data);
  1560. if (edge_port->close_pending) {
  1561. dbg ("%s - close is pending, dropping data on the floor.", __FUNCTION__);
  1562. } else {
  1563. edge_tty_recv(&edge_port->port->dev, tty, data, urb->actual_length);
  1564. }
  1565. edge_port->icount.rx += urb->actual_length;
  1566. }
  1567. exit:
  1568. /* continue read unless stopped */
  1569. spin_lock(&edge_port->ep_lock);
  1570. if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
  1571. urb->dev = edge_port->port->serial->dev;
  1572. retval = usb_submit_urb(urb, GFP_ATOMIC);
  1573. } else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
  1574. edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
  1575. }
  1576. spin_unlock(&edge_port->ep_lock);
  1577. if (retval)
  1578. dev_err (&urb->dev->dev, "%s - usb_submit_urb failed with result %d\n",
  1579. __FUNCTION__, retval);
  1580. }
  1581. static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
  1582. {
  1583. int cnt;
  1584. do {
  1585. cnt = tty_buffer_request_room(tty, length);
  1586. if (cnt < length) {
  1587. dev_err(dev, "%s - dropping data, %d bytes lost\n",
  1588. __FUNCTION__, length - cnt);
  1589. if(cnt == 0)
  1590. break;
  1591. }
  1592. tty_insert_flip_string(tty, data, cnt);
  1593. data += cnt;
  1594. length -= cnt;
  1595. } while (length > 0);
  1596. tty_flip_buffer_push(tty);
  1597. }
  1598. static void edge_bulk_out_callback (struct urb *urb)
  1599. {
  1600. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  1601. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1602. int status = urb->status;
  1603. dbg ("%s - port %d", __FUNCTION__, port->number);
  1604. edge_port->ep_write_urb_in_use = 0;
  1605. switch (status) {
  1606. case 0:
  1607. /* success */
  1608. break;
  1609. case -ECONNRESET:
  1610. case -ENOENT:
  1611. case -ESHUTDOWN:
  1612. /* this urb is terminated, clean up */
  1613. dbg("%s - urb shutting down with status: %d",
  1614. __FUNCTION__, status);
  1615. return;
  1616. default:
  1617. dev_err(&urb->dev->dev, "%s - nonzero write bulk status "
  1618. "received: %d\n", __FUNCTION__, status);
  1619. }
  1620. /* send any buffered data */
  1621. edge_send(port);
  1622. }
  1623. static int edge_open (struct usb_serial_port *port, struct file * filp)
  1624. {
  1625. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1626. struct edgeport_serial *edge_serial;
  1627. struct usb_device *dev;
  1628. struct urb *urb;
  1629. int port_number;
  1630. int status;
  1631. u16 open_settings;
  1632. u8 transaction_timeout;
  1633. dbg("%s - port %d", __FUNCTION__, port->number);
  1634. if (edge_port == NULL)
  1635. return -ENODEV;
  1636. if (port->tty)
  1637. port->tty->low_latency = low_latency;
  1638. port_number = port->number - port->serial->minor;
  1639. switch (port_number) {
  1640. case 0:
  1641. edge_port->uart_base = UMPMEM_BASE_UART1;
  1642. edge_port->dma_address = UMPD_OEDB1_ADDRESS;
  1643. break;
  1644. case 1:
  1645. edge_port->uart_base = UMPMEM_BASE_UART2;
  1646. edge_port->dma_address = UMPD_OEDB2_ADDRESS;
  1647. break;
  1648. default:
  1649. dev_err (&port->dev, "Unknown port number!!!\n");
  1650. return -ENODEV;
  1651. }
  1652. dbg ("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
  1653. __FUNCTION__, port_number, edge_port->uart_base, edge_port->dma_address);
  1654. dev = port->serial->dev;
  1655. memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
  1656. init_waitqueue_head (&edge_port->delta_msr_wait);
  1657. /* turn off loopback */
  1658. status = TIClearLoopBack (edge_port);
  1659. if (status) {
  1660. dev_err(&port->dev,"%s - cannot send clear loopback command, %d\n",
  1661. __FUNCTION__, status);
  1662. return status;
  1663. }
  1664. /* set up the port settings */
  1665. edge_set_termios (port, NULL);
  1666. /* open up the port */
  1667. /* milliseconds to timeout for DMA transfer */
  1668. transaction_timeout = 2;
  1669. edge_port->ump_read_timeout = max (20, ((transaction_timeout * 3) / 2) );
  1670. // milliseconds to timeout for DMA transfer
  1671. open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
  1672. UMP_PIPE_TRANS_TIMEOUT_ENA |
  1673. (transaction_timeout << 2));
  1674. dbg ("%s - Sending UMPC_OPEN_PORT", __FUNCTION__);
  1675. /* Tell TI to open and start the port */
  1676. status = TIWriteCommandSync (dev,
  1677. UMPC_OPEN_PORT,
  1678. (u8)(UMPM_UART1_PORT + port_number),
  1679. open_settings,
  1680. NULL,
  1681. 0);
  1682. if (status) {
  1683. dev_err(&port->dev,"%s - cannot send open command, %d\n", __FUNCTION__, status);
  1684. return status;
  1685. }
  1686. /* Start the DMA? */
  1687. status = TIWriteCommandSync (dev,
  1688. UMPC_START_PORT,
  1689. (u8)(UMPM_UART1_PORT + port_number),
  1690. 0,
  1691. NULL,
  1692. 0);
  1693. if (status) {
  1694. dev_err(&port->dev,"%s - cannot send start DMA command, %d\n", __FUNCTION__, status);
  1695. return status;
  1696. }
  1697. /* Clear TX and RX buffers in UMP */
  1698. status = TIPurgeDataSync (port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
  1699. if (status) {
  1700. dev_err(&port->dev,"%s - cannot send clear buffers command, %d\n", __FUNCTION__, status);
  1701. return status;
  1702. }
  1703. /* Read Initial MSR */
  1704. status = TIReadVendorRequestSync (dev,
  1705. UMPC_READ_MSR, // Request
  1706. 0, // wValue
  1707. (__u16)(UMPM_UART1_PORT + port_number), // wIndex (Address)
  1708. &edge_port->shadow_msr, // TransferBuffer
  1709. 1); // TransferBufferLength
  1710. if (status) {
  1711. dev_err(&port->dev,"%s - cannot send read MSR command, %d\n", __FUNCTION__, status);
  1712. return status;
  1713. }
  1714. dbg ("ShadowMSR 0x%X", edge_port->shadow_msr);
  1715. /* Set Initial MCR */
  1716. edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
  1717. dbg ("ShadowMCR 0x%X", edge_port->shadow_mcr);
  1718. edge_serial = edge_port->edge_serial;
  1719. if (down_interruptible(&edge_serial->es_sem))
  1720. return -ERESTARTSYS;
  1721. if (edge_serial->num_ports_open == 0) {
  1722. /* we are the first port to be opened, let's post the interrupt urb */
  1723. urb = edge_serial->serial->port[0]->interrupt_in_urb;
  1724. if (!urb) {
  1725. dev_err (&port->dev, "%s - no interrupt urb present, exiting\n", __FUNCTION__);
  1726. status = -EINVAL;
  1727. goto up_es_sem;
  1728. }
  1729. urb->complete = edge_interrupt_callback;
  1730. urb->context = edge_serial;
  1731. urb->dev = dev;
  1732. status = usb_submit_urb (urb, GFP_KERNEL);
  1733. if (status) {
  1734. dev_err (&port->dev, "%s - usb_submit_urb failed with value %d\n", __FUNCTION__, status);
  1735. goto up_es_sem;
  1736. }
  1737. }
  1738. /*
  1739. * reset the data toggle on the bulk endpoints to work around bug in
  1740. * host controllers where things get out of sync some times
  1741. */
  1742. usb_clear_halt (dev, port->write_urb->pipe);
  1743. usb_clear_halt (dev, port->read_urb->pipe);
  1744. /* start up our bulk read urb */
  1745. urb = port->read_urb;
  1746. if (!urb) {
  1747. dev_err (&port->dev, "%s - no read urb present, exiting\n", __FUNCTION__);
  1748. status = -EINVAL;
  1749. goto unlink_int_urb;
  1750. }
  1751. edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
  1752. urb->complete = edge_bulk_in_callback;
  1753. urb->context = edge_port;
  1754. urb->dev = dev;
  1755. status = usb_submit_urb (urb, GFP_KERNEL);
  1756. if (status) {
  1757. dev_err (&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __FUNCTION__, status);
  1758. goto unlink_int_urb;
  1759. }
  1760. ++edge_serial->num_ports_open;
  1761. dbg("%s - exited", __FUNCTION__);
  1762. goto up_es_sem;
  1763. unlink_int_urb:
  1764. if (edge_port->edge_serial->num_ports_open == 0)
  1765. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  1766. up_es_sem:
  1767. up(&edge_serial->es_sem);
  1768. return status;
  1769. }
  1770. static void edge_close (struct usb_serial_port *port, struct file *filp)
  1771. {
  1772. struct edgeport_serial *edge_serial;
  1773. struct edgeport_port *edge_port;
  1774. int port_number;
  1775. int status;
  1776. dbg("%s - port %d", __FUNCTION__, port->number);
  1777. edge_serial = usb_get_serial_data(port->serial);
  1778. edge_port = usb_get_serial_port_data(port);
  1779. if ((edge_serial == NULL) || (edge_port == NULL))
  1780. return;
  1781. /* The bulkreadcompletion routine will check
  1782. * this flag and dump add read data */
  1783. edge_port->close_pending = 1;
  1784. /* chase the port close and flush */
  1785. TIChasePort (edge_port, (HZ*closing_wait)/100, 1);
  1786. usb_kill_urb(port->read_urb);
  1787. usb_kill_urb(port->write_urb);
  1788. edge_port->ep_write_urb_in_use = 0;
  1789. /* assuming we can still talk to the device,
  1790. * send a close port command to it */
  1791. dbg("%s - send umpc_close_port", __FUNCTION__);
  1792. port_number = port->number - port->serial->minor;
  1793. status = TIWriteCommandSync (port->serial->dev,
  1794. UMPC_CLOSE_PORT,
  1795. (__u8)(UMPM_UART1_PORT + port_number),
  1796. 0,
  1797. NULL,
  1798. 0);
  1799. down(&edge_serial->es_sem);
  1800. --edge_port->edge_serial->num_ports_open;
  1801. if (edge_port->edge_serial->num_ports_open <= 0) {
  1802. /* last port is now closed, let's shut down our interrupt urb */
  1803. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  1804. edge_port->edge_serial->num_ports_open = 0;
  1805. }
  1806. up(&edge_serial->es_sem);
  1807. edge_port->close_pending = 0;
  1808. dbg("%s - exited", __FUNCTION__);
  1809. }
  1810. static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
  1811. {
  1812. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1813. unsigned long flags;
  1814. dbg("%s - port %d", __FUNCTION__, port->number);
  1815. if (count == 0) {
  1816. dbg("%s - write request of 0 bytes", __FUNCTION__);
  1817. return 0;
  1818. }
  1819. if (edge_port == NULL)
  1820. return -ENODEV;
  1821. if (edge_port->close_pending == 1)
  1822. return -ENODEV;
  1823. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1824. count = edge_buf_put(edge_port->ep_out_buf, data, count);
  1825. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1826. edge_send(port);
  1827. return count;
  1828. }
  1829. static void edge_send(struct usb_serial_port *port)
  1830. {
  1831. int count, result;
  1832. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1833. struct tty_struct *tty = port->tty;
  1834. unsigned long flags;
  1835. dbg("%s - port %d", __FUNCTION__, port->number);
  1836. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1837. if (edge_port->ep_write_urb_in_use) {
  1838. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1839. return;
  1840. }
  1841. count = edge_buf_get(edge_port->ep_out_buf,
  1842. port->write_urb->transfer_buffer,
  1843. port->bulk_out_size);
  1844. if (count == 0) {
  1845. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1846. return;
  1847. }
  1848. edge_port->ep_write_urb_in_use = 1;
  1849. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1850. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, port->write_urb->transfer_buffer);
  1851. /* set up our urb */
  1852. usb_fill_bulk_urb (port->write_urb, port->serial->dev,
  1853. usb_sndbulkpipe (port->serial->dev,
  1854. port->bulk_out_endpointAddress),
  1855. port->write_urb->transfer_buffer, count,
  1856. edge_bulk_out_callback,
  1857. port);
  1858. /* send the data out the bulk port */
  1859. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  1860. if (result) {
  1861. dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
  1862. edge_port->ep_write_urb_in_use = 0;
  1863. // TODO: reschedule edge_send
  1864. } else {
  1865. edge_port->icount.tx += count;
  1866. }
  1867. /* wakeup any process waiting for writes to complete */
  1868. /* there is now more room in the buffer for new writes */
  1869. if (tty) {
  1870. /* let the tty driver wakeup if it has a special write_wakeup function */
  1871. tty_wakeup(tty);
  1872. }
  1873. }
  1874. static int edge_write_room (struct usb_serial_port *port)
  1875. {
  1876. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1877. int room = 0;
  1878. unsigned long flags;
  1879. dbg("%s - port %d", __FUNCTION__, port->number);
  1880. if (edge_port == NULL)
  1881. return -ENODEV;
  1882. if (edge_port->close_pending == 1)
  1883. return -ENODEV;
  1884. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1885. room = edge_buf_space_avail(edge_port->ep_out_buf);
  1886. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1887. dbg("%s - returns %d", __FUNCTION__, room);
  1888. return room;
  1889. }
  1890. static int edge_chars_in_buffer (struct usb_serial_port *port)
  1891. {
  1892. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1893. int chars = 0;
  1894. unsigned long flags;
  1895. dbg("%s - port %d", __FUNCTION__, port->number);
  1896. if (edge_port == NULL)
  1897. return -ENODEV;
  1898. if (edge_port->close_pending == 1)
  1899. return -ENODEV;
  1900. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1901. chars = edge_buf_data_avail(edge_port->ep_out_buf);
  1902. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1903. dbg ("%s - returns %d", __FUNCTION__, chars);
  1904. return chars;
  1905. }
  1906. static void edge_throttle (struct usb_serial_port *port)
  1907. {
  1908. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1909. struct tty_struct *tty;
  1910. int status;
  1911. dbg("%s - port %d", __FUNCTION__, port->number);
  1912. if (edge_port == NULL)
  1913. return;
  1914. tty = port->tty;
  1915. if (!tty) {
  1916. dbg ("%s - no tty available", __FUNCTION__);
  1917. return;
  1918. }
  1919. /* if we are implementing XON/XOFF, send the stop character */
  1920. if (I_IXOFF(tty)) {
  1921. unsigned char stop_char = STOP_CHAR(tty);
  1922. status = edge_write (port, &stop_char, 1);
  1923. if (status <= 0) {
  1924. dev_err(&port->dev, "%s - failed to write stop character, %d\n", __FUNCTION__, status);
  1925. }
  1926. }
  1927. /* if we are implementing RTS/CTS, stop reads */
  1928. /* and the Edgeport will clear the RTS line */
  1929. if (C_CRTSCTS(tty))
  1930. stop_read(edge_port);
  1931. }
  1932. static void edge_unthrottle (struct usb_serial_port *port)
  1933. {
  1934. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1935. struct tty_struct *tty;
  1936. int status;
  1937. dbg("%s - port %d", __FUNCTION__, port->number);
  1938. if (edge_port == NULL)
  1939. return;
  1940. tty = port->tty;
  1941. if (!tty) {
  1942. dbg ("%s - no tty available", __FUNCTION__);
  1943. return;
  1944. }
  1945. /* if we are implementing XON/XOFF, send the start character */
  1946. if (I_IXOFF(tty)) {
  1947. unsigned char start_char = START_CHAR(tty);
  1948. status = edge_write (port, &start_char, 1);
  1949. if (status <= 0) {
  1950. dev_err(&port->dev, "%s - failed to write start character, %d\n", __FUNCTION__, status);
  1951. }
  1952. }
  1953. /* if we are implementing RTS/CTS, restart reads */
  1954. /* are the Edgeport will assert the RTS line */
  1955. if (C_CRTSCTS(tty)) {
  1956. status = restart_read(edge_port);
  1957. if (status)
  1958. dev_err(&port->dev, "%s - read bulk usb_submit_urb failed with value %d\n", __FUNCTION__, status);
  1959. }
  1960. }
  1961. static void stop_read(struct edgeport_port *edge_port)
  1962. {
  1963. unsigned long flags;
  1964. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1965. if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
  1966. edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
  1967. edge_port->shadow_mcr &= ~MCR_RTS;
  1968. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1969. }
  1970. static int restart_read(struct edgeport_port *edge_port)
  1971. {
  1972. struct urb *urb;
  1973. int status = 0;
  1974. unsigned long flags;
  1975. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1976. if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
  1977. urb = edge_port->port->read_urb;
  1978. urb->complete = edge_bulk_in_callback;
  1979. urb->context = edge_port;
  1980. urb->dev = edge_port->port->serial->dev;
  1981. status = usb_submit_urb(urb, GFP_ATOMIC);
  1982. }
  1983. edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
  1984. edge_port->shadow_mcr |= MCR_RTS;
  1985. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1986. return status;
  1987. }
  1988. static void change_port_settings (struct edgeport_port *edge_port, struct ktermios *old_termios)
  1989. {
  1990. struct ump_uart_config *config;
  1991. struct tty_struct *tty;
  1992. int baud;
  1993. unsigned cflag;
  1994. int status;
  1995. int port_number = edge_port->port->number - edge_port->port->serial->minor;
  1996. dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
  1997. tty = edge_port->port->tty;
  1998. if ((!tty) ||
  1999. (!tty->termios)) {
  2000. dbg("%s - no tty structures", __FUNCTION__);
  2001. return;
  2002. }
  2003. config = kmalloc (sizeof (*config), GFP_KERNEL);
  2004. if (!config) {
  2005. dev_err (&edge_port->port->dev, "%s - out of memory\n", __FUNCTION__);
  2006. return;
  2007. }
  2008. cflag = tty->termios->c_cflag;
  2009. config->wFlags = 0;
  2010. /* These flags must be set */
  2011. config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
  2012. config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
  2013. config->bUartMode = (__u8)(edge_port->bUartMode);
  2014. switch (cflag & CSIZE) {
  2015. case CS5:
  2016. config->bDataBits = UMP_UART_CHAR5BITS;
  2017. dbg ("%s - data bits = 5", __FUNCTION__);
  2018. break;
  2019. case CS6:
  2020. config->bDataBits = UMP_UART_CHAR6BITS;
  2021. dbg ("%s - data bits = 6", __FUNCTION__);
  2022. break;
  2023. case CS7:
  2024. config->bDataBits = UMP_UART_CHAR7BITS;
  2025. dbg ("%s - data bits = 7", __FUNCTION__);
  2026. break;
  2027. default:
  2028. case CS8:
  2029. config->bDataBits = UMP_UART_CHAR8BITS;
  2030. dbg ("%s - data bits = 8", __FUNCTION__);
  2031. break;
  2032. }
  2033. if (cflag & PARENB) {
  2034. if (cflag & PARODD) {
  2035. config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
  2036. config->bParity = UMP_UART_ODDPARITY;
  2037. dbg("%s - parity = odd", __FUNCTION__);
  2038. } else {
  2039. config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
  2040. config->bParity = UMP_UART_EVENPARITY;
  2041. dbg("%s - parity = even", __FUNCTION__);
  2042. }
  2043. } else {
  2044. config->bParity = UMP_UART_NOPARITY;
  2045. dbg("%s - parity = none", __FUNCTION__);
  2046. }
  2047. if (cflag & CSTOPB) {
  2048. config->bStopBits = UMP_UART_STOPBIT2;
  2049. dbg("%s - stop bits = 2", __FUNCTION__);
  2050. } else {
  2051. config->bStopBits = UMP_UART_STOPBIT1;
  2052. dbg("%s - stop bits = 1", __FUNCTION__);
  2053. }
  2054. /* figure out the flow control settings */
  2055. if (cflag & CRTSCTS) {
  2056. config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
  2057. config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
  2058. dbg("%s - RTS/CTS is enabled", __FUNCTION__);
  2059. } else {
  2060. dbg("%s - RTS/CTS is disabled", __FUNCTION__);
  2061. tty->hw_stopped = 0;
  2062. restart_read(edge_port);
  2063. }
  2064. /* if we are implementing XON/XOFF, set the start and stop character in the device */
  2065. if (I_IXOFF(tty) || I_IXON(tty)) {
  2066. config->cXon = START_CHAR(tty);
  2067. config->cXoff = STOP_CHAR(tty);
  2068. /* if we are implementing INBOUND XON/XOFF */
  2069. if (I_IXOFF(tty)) {
  2070. config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
  2071. dbg ("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
  2072. __FUNCTION__, config->cXon, config->cXoff);
  2073. } else {
  2074. dbg ("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
  2075. }
  2076. /* if we are implementing OUTBOUND XON/XOFF */
  2077. if (I_IXON(tty)) {
  2078. config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
  2079. dbg ("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
  2080. __FUNCTION__, config->cXon, config->cXoff);
  2081. } else {
  2082. dbg ("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
  2083. }
  2084. }
  2085. /* Round the baud rate */
  2086. baud = tty_get_baud_rate(tty);
  2087. if (!baud) {
  2088. /* pick a default, any default... */
  2089. baud = 9600;
  2090. }
  2091. edge_port->baud_rate = baud;
  2092. config->wBaudRate = (__u16)((461550L + baud/2) / baud);
  2093. dbg ("%s - baud rate = %d, wBaudRate = %d", __FUNCTION__, baud, config->wBaudRate);
  2094. dbg ("wBaudRate: %d", (int)(461550L / config->wBaudRate));
  2095. dbg ("wFlags: 0x%x", config->wFlags);
  2096. dbg ("bDataBits: %d", config->bDataBits);
  2097. dbg ("bParity: %d", config->bParity);
  2098. dbg ("bStopBits: %d", config->bStopBits);
  2099. dbg ("cXon: %d", config->cXon);
  2100. dbg ("cXoff: %d", config->cXoff);
  2101. dbg ("bUartMode: %d", config->bUartMode);
  2102. /* move the word values into big endian mode */
  2103. cpu_to_be16s (&config->wFlags);
  2104. cpu_to_be16s (&config->wBaudRate);
  2105. status = TIWriteCommandSync (edge_port->port->serial->dev,
  2106. UMPC_SET_CONFIG,
  2107. (__u8)(UMPM_UART1_PORT + port_number),
  2108. 0,
  2109. (__u8 *)config,
  2110. sizeof(*config));
  2111. if (status) {
  2112. dbg ("%s - error %d when trying to write config to device",
  2113. __FUNCTION__, status);
  2114. }
  2115. kfree (config);
  2116. return;
  2117. }
  2118. static void edge_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
  2119. {
  2120. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2121. struct tty_struct *tty = port->tty;
  2122. unsigned int cflag;
  2123. if (!port->tty || !port->tty->termios) {
  2124. dbg ("%s - no tty or termios", __FUNCTION__);
  2125. return;
  2126. }
  2127. cflag = tty->termios->c_cflag;
  2128. dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
  2129. tty->termios->c_cflag, tty->termios->c_iflag);
  2130. if (old_termios) {
  2131. dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
  2132. old_termios->c_cflag, old_termios->c_iflag);
  2133. }
  2134. dbg("%s - port %d", __FUNCTION__, port->number);
  2135. if (edge_port == NULL)
  2136. return;
  2137. /* change the port settings to the new ones specified */
  2138. change_port_settings (edge_port, old_termios);
  2139. return;
  2140. }
  2141. static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
  2142. {
  2143. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2144. unsigned int mcr;
  2145. dbg("%s - port %d", __FUNCTION__, port->number);
  2146. mcr = edge_port->shadow_mcr;
  2147. if (set & TIOCM_RTS)
  2148. mcr |= MCR_RTS;
  2149. if (set & TIOCM_DTR)
  2150. mcr |= MCR_DTR;
  2151. if (set & TIOCM_LOOP)
  2152. mcr |= MCR_LOOPBACK;
  2153. if (clear & TIOCM_RTS)
  2154. mcr &= ~MCR_RTS;
  2155. if (clear & TIOCM_DTR)
  2156. mcr &= ~MCR_DTR;
  2157. if (clear & TIOCM_LOOP)
  2158. mcr &= ~MCR_LOOPBACK;
  2159. edge_port->shadow_mcr = mcr;
  2160. TIRestoreMCR (edge_port, mcr);
  2161. return 0;
  2162. }
  2163. static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
  2164. {
  2165. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2166. unsigned int result = 0;
  2167. unsigned int msr;
  2168. unsigned int mcr;
  2169. dbg("%s - port %d", __FUNCTION__, port->number);
  2170. msr = edge_port->shadow_msr;
  2171. mcr = edge_port->shadow_mcr;
  2172. result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  2173. | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  2174. | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  2175. | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
  2176. | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  2177. | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  2178. dbg("%s -- %x", __FUNCTION__, result);
  2179. return result;
  2180. }
  2181. static int get_serial_info (struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
  2182. {
  2183. struct serial_struct tmp;
  2184. if (!retinfo)
  2185. return -EFAULT;
  2186. memset(&tmp, 0, sizeof(tmp));
  2187. tmp.type = PORT_16550A;
  2188. tmp.line = edge_port->port->serial->minor;
  2189. tmp.port = edge_port->port->number;
  2190. tmp.irq = 0;
  2191. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  2192. tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
  2193. tmp.baud_base = 9600;
  2194. tmp.close_delay = 5*HZ;
  2195. tmp.closing_wait = closing_wait;
  2196. // tmp.custom_divisor = state->custom_divisor;
  2197. // tmp.hub6 = state->hub6;
  2198. // tmp.io_type = state->io_type;
  2199. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  2200. return -EFAULT;
  2201. return 0;
  2202. }
  2203. static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
  2204. {
  2205. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2206. struct async_icount cnow;
  2207. struct async_icount cprev;
  2208. dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
  2209. switch (cmd) {
  2210. case TIOCINQ:
  2211. dbg("%s - (%d) TIOCINQ", __FUNCTION__, port->number);
  2212. // return get_number_bytes_avail(edge_port, (unsigned int *) arg);
  2213. break;
  2214. case TIOCSERGETLSR:
  2215. dbg("%s - (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
  2216. // return get_lsr_info(edge_port, (unsigned int *) arg);
  2217. break;
  2218. case TIOCGSERIAL:
  2219. dbg("%s - (%d) TIOCGSERIAL", __FUNCTION__, port->number);
  2220. return get_serial_info(edge_port, (struct serial_struct __user *) arg);
  2221. break;
  2222. case TIOCSSERIAL:
  2223. dbg("%s - (%d) TIOCSSERIAL", __FUNCTION__, port->number);
  2224. break;
  2225. case TIOCMIWAIT:
  2226. dbg("%s - (%d) TIOCMIWAIT", __FUNCTION__, port->number);
  2227. cprev = edge_port->icount;
  2228. while (1) {
  2229. interruptible_sleep_on(&edge_port->delta_msr_wait);
  2230. /* see if a signal did it */
  2231. if (signal_pending(current))
  2232. return -ERESTARTSYS;
  2233. cnow = edge_port->icount;
  2234. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  2235. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  2236. return -EIO; /* no change => error */
  2237. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  2238. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  2239. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  2240. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  2241. return 0;
  2242. }
  2243. cprev = cnow;
  2244. }
  2245. /* not reached */
  2246. break;
  2247. case TIOCGICOUNT:
  2248. dbg ("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
  2249. port->number, edge_port->icount.rx, edge_port->icount.tx);
  2250. if (copy_to_user((void __user *)arg, &edge_port->icount, sizeof(edge_port->icount)))
  2251. return -EFAULT;
  2252. return 0;
  2253. }
  2254. return -ENOIOCTLCMD;
  2255. }
  2256. static void edge_break (struct usb_serial_port *port, int break_state)
  2257. {
  2258. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2259. int status;
  2260. dbg ("%s - state = %d", __FUNCTION__, break_state);
  2261. /* chase the port close */
  2262. TIChasePort (edge_port, 0, 0);
  2263. if (break_state == -1) {
  2264. status = TISetBreak (edge_port);
  2265. } else {
  2266. status = TIClearBreak (edge_port);
  2267. }
  2268. if (status) {
  2269. dbg ("%s - error %d sending break set/clear command.",
  2270. __FUNCTION__, status);
  2271. }
  2272. }
  2273. static int edge_startup (struct usb_serial *serial)
  2274. {
  2275. struct edgeport_serial *edge_serial;
  2276. struct edgeport_port *edge_port;
  2277. struct usb_device *dev;
  2278. int status;
  2279. int i;
  2280. dev = serial->dev;
  2281. /* create our private serial structure */
  2282. edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
  2283. if (edge_serial == NULL) {
  2284. dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
  2285. return -ENOMEM;
  2286. }
  2287. sema_init(&edge_serial->es_sem, 1);
  2288. edge_serial->serial = serial;
  2289. usb_set_serial_data(serial, edge_serial);
  2290. status = TIDownloadFirmware (edge_serial);
  2291. if (status) {
  2292. kfree (edge_serial);
  2293. return status;
  2294. }
  2295. /* set up our port private structures */
  2296. for (i = 0; i < serial->num_ports; ++i) {
  2297. edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
  2298. if (edge_port == NULL) {
  2299. dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
  2300. goto cleanup;
  2301. }
  2302. spin_lock_init(&edge_port->ep_lock);
  2303. edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
  2304. if (edge_port->ep_out_buf == NULL) {
  2305. dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
  2306. kfree(edge_port);
  2307. goto cleanup;
  2308. }
  2309. edge_port->port = serial->port[i];
  2310. edge_port->edge_serial = edge_serial;
  2311. usb_set_serial_port_data(serial->port[i], edge_port);
  2312. edge_port->bUartMode = default_uart_mode;
  2313. }
  2314. return 0;
  2315. cleanup:
  2316. for (--i; i>=0; --i) {
  2317. edge_port = usb_get_serial_port_data(serial->port[i]);
  2318. edge_buf_free(edge_port->ep_out_buf);
  2319. kfree(edge_port);
  2320. usb_set_serial_port_data(serial->port[i], NULL);
  2321. }
  2322. kfree (edge_serial);
  2323. usb_set_serial_data(serial, NULL);
  2324. return -ENOMEM;
  2325. }
  2326. static void edge_shutdown (struct usb_serial *serial)
  2327. {
  2328. int i;
  2329. struct edgeport_port *edge_port;
  2330. dbg ("%s", __FUNCTION__);
  2331. for (i = 0; i < serial->num_ports; ++i) {
  2332. edge_port = usb_get_serial_port_data(serial->port[i]);
  2333. edge_remove_sysfs_attrs(edge_port->port);
  2334. edge_buf_free(edge_port->ep_out_buf);
  2335. kfree(edge_port);
  2336. usb_set_serial_port_data(serial->port[i], NULL);
  2337. }
  2338. kfree(usb_get_serial_data(serial));
  2339. usb_set_serial_data(serial, NULL);
  2340. }
  2341. /* Sysfs Attributes */
  2342. static ssize_t show_uart_mode(struct device *dev,
  2343. struct device_attribute *attr, char *buf)
  2344. {
  2345. struct usb_serial_port *port = to_usb_serial_port(dev);
  2346. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2347. return sprintf(buf, "%d\n", edge_port->bUartMode);
  2348. }
  2349. static ssize_t store_uart_mode(struct device *dev,
  2350. struct device_attribute *attr, const char *valbuf, size_t count)
  2351. {
  2352. struct usb_serial_port *port = to_usb_serial_port(dev);
  2353. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2354. unsigned int v = simple_strtoul(valbuf, NULL, 0);
  2355. dbg("%s: setting uart_mode = %d", __FUNCTION__, v);
  2356. if (v < 256)
  2357. edge_port->bUartMode = v;
  2358. else
  2359. dev_err(dev, "%s - uart_mode %d is invalid\n", __FUNCTION__, v);
  2360. return count;
  2361. }
  2362. static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode, store_uart_mode);
  2363. static int edge_create_sysfs_attrs(struct usb_serial_port *port)
  2364. {
  2365. return device_create_file(&port->dev, &dev_attr_uart_mode);
  2366. }
  2367. static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
  2368. {
  2369. device_remove_file(&port->dev, &dev_attr_uart_mode);
  2370. return 0;
  2371. }
  2372. /* Circular Buffer */
  2373. /*
  2374. * edge_buf_alloc
  2375. *
  2376. * Allocate a circular buffer and all associated memory.
  2377. */
  2378. static struct edge_buf *edge_buf_alloc(unsigned int size)
  2379. {
  2380. struct edge_buf *eb;
  2381. if (size == 0)
  2382. return NULL;
  2383. eb = kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
  2384. if (eb == NULL)
  2385. return NULL;
  2386. eb->buf_buf = kmalloc(size, GFP_KERNEL);
  2387. if (eb->buf_buf == NULL) {
  2388. kfree(eb);
  2389. return NULL;
  2390. }
  2391. eb->buf_size = size;
  2392. eb->buf_get = eb->buf_put = eb->buf_buf;
  2393. return eb;
  2394. }
  2395. /*
  2396. * edge_buf_free
  2397. *
  2398. * Free the buffer and all associated memory.
  2399. */
  2400. static void edge_buf_free(struct edge_buf *eb)
  2401. {
  2402. if (eb) {
  2403. kfree(eb->buf_buf);
  2404. kfree(eb);
  2405. }
  2406. }
  2407. /*
  2408. * edge_buf_clear
  2409. *
  2410. * Clear out all data in the circular buffer.
  2411. */
  2412. static void edge_buf_clear(struct edge_buf *eb)
  2413. {
  2414. if (eb != NULL)
  2415. eb->buf_get = eb->buf_put;
  2416. /* equivalent to a get of all data available */
  2417. }
  2418. /*
  2419. * edge_buf_data_avail
  2420. *
  2421. * Return the number of bytes of data available in the circular
  2422. * buffer.
  2423. */
  2424. static unsigned int edge_buf_data_avail(struct edge_buf *eb)
  2425. {
  2426. if (eb != NULL)
  2427. return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
  2428. else
  2429. return 0;
  2430. }
  2431. /*
  2432. * edge_buf_space_avail
  2433. *
  2434. * Return the number of bytes of space available in the circular
  2435. * buffer.
  2436. */
  2437. static unsigned int edge_buf_space_avail(struct edge_buf *eb)
  2438. {
  2439. if (eb != NULL)
  2440. return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
  2441. else
  2442. return 0;
  2443. }
  2444. /*
  2445. * edge_buf_put
  2446. *
  2447. * Copy data data from a user buffer and put it into the circular buffer.
  2448. * Restrict to the amount of space available.
  2449. *
  2450. * Return the number of bytes copied.
  2451. */
  2452. static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
  2453. unsigned int count)
  2454. {
  2455. unsigned int len;
  2456. if (eb == NULL)
  2457. return 0;
  2458. len = edge_buf_space_avail(eb);
  2459. if (count > len)
  2460. count = len;
  2461. if (count == 0)
  2462. return 0;
  2463. len = eb->buf_buf + eb->buf_size - eb->buf_put;
  2464. if (count > len) {
  2465. memcpy(eb->buf_put, buf, len);
  2466. memcpy(eb->buf_buf, buf+len, count - len);
  2467. eb->buf_put = eb->buf_buf + count - len;
  2468. } else {
  2469. memcpy(eb->buf_put, buf, count);
  2470. if (count < len)
  2471. eb->buf_put += count;
  2472. else /* count == len */
  2473. eb->buf_put = eb->buf_buf;
  2474. }
  2475. return count;
  2476. }
  2477. /*
  2478. * edge_buf_get
  2479. *
  2480. * Get data from the circular buffer and copy to the given buffer.
  2481. * Restrict to the amount of data available.
  2482. *
  2483. * Return the number of bytes copied.
  2484. */
  2485. static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
  2486. unsigned int count)
  2487. {
  2488. unsigned int len;
  2489. if (eb == NULL)
  2490. return 0;
  2491. len = edge_buf_data_avail(eb);
  2492. if (count > len)
  2493. count = len;
  2494. if (count == 0)
  2495. return 0;
  2496. len = eb->buf_buf + eb->buf_size - eb->buf_get;
  2497. if (count > len) {
  2498. memcpy(buf, eb->buf_get, len);
  2499. memcpy(buf+len, eb->buf_buf, count - len);
  2500. eb->buf_get = eb->buf_buf + count - len;
  2501. } else {
  2502. memcpy(buf, eb->buf_get, count);
  2503. if (count < len)
  2504. eb->buf_get += count;
  2505. else /* count == len */
  2506. eb->buf_get = eb->buf_buf;
  2507. }
  2508. return count;
  2509. }
  2510. static struct usb_serial_driver edgeport_1port_device = {
  2511. .driver = {
  2512. .owner = THIS_MODULE,
  2513. .name = "edgeport_ti_1",
  2514. },
  2515. .description = "Edgeport TI 1 port adapter",
  2516. .usb_driver = &io_driver,
  2517. .id_table = edgeport_1port_id_table,
  2518. .num_interrupt_in = 1,
  2519. .num_bulk_in = 1,
  2520. .num_bulk_out = 1,
  2521. .num_ports = 1,
  2522. .open = edge_open,
  2523. .close = edge_close,
  2524. .throttle = edge_throttle,
  2525. .unthrottle = edge_unthrottle,
  2526. .attach = edge_startup,
  2527. .shutdown = edge_shutdown,
  2528. .port_probe = edge_create_sysfs_attrs,
  2529. .ioctl = edge_ioctl,
  2530. .set_termios = edge_set_termios,
  2531. .tiocmget = edge_tiocmget,
  2532. .tiocmset = edge_tiocmset,
  2533. .write = edge_write,
  2534. .write_room = edge_write_room,
  2535. .chars_in_buffer = edge_chars_in_buffer,
  2536. .break_ctl = edge_break,
  2537. .read_int_callback = edge_interrupt_callback,
  2538. .read_bulk_callback = edge_bulk_in_callback,
  2539. .write_bulk_callback = edge_bulk_out_callback,
  2540. };
  2541. static struct usb_serial_driver edgeport_2port_device = {
  2542. .driver = {
  2543. .owner = THIS_MODULE,
  2544. .name = "edgeport_ti_2",
  2545. },
  2546. .description = "Edgeport TI 2 port adapter",
  2547. .usb_driver = &io_driver,
  2548. .id_table = edgeport_2port_id_table,
  2549. .num_interrupt_in = 1,
  2550. .num_bulk_in = 2,
  2551. .num_bulk_out = 2,
  2552. .num_ports = 2,
  2553. .open = edge_open,
  2554. .close = edge_close,
  2555. .throttle = edge_throttle,
  2556. .unthrottle = edge_unthrottle,
  2557. .attach = edge_startup,
  2558. .shutdown = edge_shutdown,
  2559. .port_probe = edge_create_sysfs_attrs,
  2560. .ioctl = edge_ioctl,
  2561. .set_termios = edge_set_termios,
  2562. .tiocmget = edge_tiocmget,
  2563. .tiocmset = edge_tiocmset,
  2564. .write = edge_write,
  2565. .write_room = edge_write_room,
  2566. .chars_in_buffer = edge_chars_in_buffer,
  2567. .break_ctl = edge_break,
  2568. .read_int_callback = edge_interrupt_callback,
  2569. .read_bulk_callback = edge_bulk_in_callback,
  2570. .write_bulk_callback = edge_bulk_out_callback,
  2571. };
  2572. static int __init edgeport_init(void)
  2573. {
  2574. int retval;
  2575. retval = usb_serial_register(&edgeport_1port_device);
  2576. if (retval)
  2577. goto failed_1port_device_register;
  2578. retval = usb_serial_register(&edgeport_2port_device);
  2579. if (retval)
  2580. goto failed_2port_device_register;
  2581. retval = usb_register(&io_driver);
  2582. if (retval)
  2583. goto failed_usb_register;
  2584. info(DRIVER_DESC " " DRIVER_VERSION);
  2585. return 0;
  2586. failed_usb_register:
  2587. usb_serial_deregister(&edgeport_2port_device);
  2588. failed_2port_device_register:
  2589. usb_serial_deregister(&edgeport_1port_device);
  2590. failed_1port_device_register:
  2591. return retval;
  2592. }
  2593. static void __exit edgeport_exit (void)
  2594. {
  2595. usb_deregister (&io_driver);
  2596. usb_serial_deregister (&edgeport_1port_device);
  2597. usb_serial_deregister (&edgeport_2port_device);
  2598. }
  2599. module_init(edgeport_init);
  2600. module_exit(edgeport_exit);
  2601. /* Module information */
  2602. MODULE_AUTHOR(DRIVER_AUTHOR);
  2603. MODULE_DESCRIPTION(DRIVER_DESC);
  2604. MODULE_LICENSE("GPL");
  2605. module_param(debug, bool, S_IRUGO | S_IWUSR);
  2606. MODULE_PARM_DESC(debug, "Debug enabled or not");
  2607. module_param(low_latency, bool, S_IRUGO | S_IWUSR);
  2608. MODULE_PARM_DESC(low_latency, "Low latency enabled or not");
  2609. module_param(closing_wait, int, S_IRUGO | S_IWUSR);
  2610. MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
  2611. module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
  2612. MODULE_PARM_DESC(ignore_cpu_rev, "Ignore the cpu revision when connecting to a device");
  2613. module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
  2614. MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");