io_ti.c 74 KB

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