io_ti.c 76 KB

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