io_ti.c 76 KB

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