io_ti.c 72 KB

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