io_ti.c 71 KB

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