io_ti.c 76 KB

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