io_ti.c 74 KB

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