io_ti.c 77 KB

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