io_ti.c 72 KB

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