io_ti.c 76 KB

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