io_ti.c 77 KB

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