io_ti.c 77 KB

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