io_ti.c 76 KB

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