io_ti.c 84 KB

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