io_ti.c 75 KB

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