io_ti.c 76 KB

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