io_ti.c 76 KB

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