io_ti.c 72 KB

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