io_ti.c 76 KB

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