io_ti.c 74 KB

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