io_ti.c 76 KB

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