io_ti.c 71 KB

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