io_ti.c 76 KB

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