io_ti.c 75 KB

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