ftdi_sio.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115
  1. /*
  2. * USB FTDI SIO driver
  3. *
  4. * Copyright (C) 1999 - 2001
  5. * Greg Kroah-Hartman (greg@kroah.com)
  6. * Bill Ryder (bryder@sgi.com)
  7. * Copyright (C) 2002
  8. * Kuba Ober (kuba@mareimbrium.org)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * See Documentation/usb/usb-serial.txt for more information on using this driver
  16. *
  17. * See http://ftdi-usb-sio.sourceforge.net for upto date testing info
  18. * and extra documentation
  19. *
  20. * Change entries from 2004 and earlier can be found in versions of this
  21. * file in kernel versions prior to the 2.6.24 release.
  22. *
  23. */
  24. /* Bill Ryder - bryder@sgi.com - wrote the FTDI_SIO implementation */
  25. /* Thanx to FTDI for so kindly providing details of the protocol required */
  26. /* to talk to the device */
  27. /* Thanx to gkh and the rest of the usb dev group for all code I have assimilated :-) */
  28. #include <linux/kernel.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 <asm/uaccess.h>
  38. #include <linux/usb.h>
  39. #include <linux/serial.h>
  40. #include <linux/usb/serial.h>
  41. #include "ftdi_sio.h"
  42. /*
  43. * Version Information
  44. */
  45. #define DRIVER_VERSION "v1.4.3"
  46. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Bill Ryder <bryder@sgi.com>, Kuba Ober <kuba@mareimbrium.org>"
  47. #define DRIVER_DESC "USB FTDI Serial Converters Driver"
  48. static int debug;
  49. static __u16 vendor = FTDI_VID;
  50. static __u16 product;
  51. struct ftdi_private {
  52. ftdi_chip_type_t chip_type;
  53. /* type of the device, either SIO or FT8U232AM */
  54. int baud_base; /* baud base clock for divisor setting */
  55. int custom_divisor; /* custom_divisor kludge, this is for baud_base (different from what goes to the chip!) */
  56. __u16 last_set_data_urb_value ;
  57. /* the last data state set - needed for doing a break */
  58. int write_offset; /* This is the offset in the usb data block to write the serial data -
  59. * it is different between devices
  60. */
  61. int flags; /* some ASYNC_xxxx flags are supported */
  62. unsigned long last_dtr_rts; /* saved modem control outputs */
  63. wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */
  64. char prev_status, diff_status; /* Used for TIOCMIWAIT */
  65. __u8 rx_flags; /* receive state flags (throttling) */
  66. spinlock_t rx_lock; /* spinlock for receive state */
  67. struct delayed_work rx_work;
  68. struct usb_serial_port *port;
  69. int rx_processed;
  70. unsigned long rx_bytes;
  71. __u16 interface; /* FT2232C port interface (0 for FT232/245) */
  72. speed_t force_baud; /* if non-zero, force the baud rate to this value */
  73. int force_rtscts; /* if non-zero, force RTS-CTS to always be enabled */
  74. spinlock_t tx_lock; /* spinlock for transmit state */
  75. unsigned long tx_bytes;
  76. unsigned long tx_outstanding_bytes;
  77. unsigned long tx_outstanding_urbs;
  78. };
  79. /* struct ftdi_sio_quirk is used by devices requiring special attention. */
  80. struct ftdi_sio_quirk {
  81. int (*probe)(struct usb_serial *);
  82. void (*port_probe)(struct ftdi_private *); /* Special settings for probed ports. */
  83. };
  84. static int ftdi_jtag_probe (struct usb_serial *serial);
  85. static int ftdi_mtxorb_hack_setup (struct usb_serial *serial);
  86. static void ftdi_USB_UIRT_setup (struct ftdi_private *priv);
  87. static void ftdi_HE_TIRA1_setup (struct ftdi_private *priv);
  88. static struct ftdi_sio_quirk ftdi_jtag_quirk = {
  89. .probe = ftdi_jtag_probe,
  90. };
  91. static struct ftdi_sio_quirk ftdi_mtxorb_hack_quirk = {
  92. .probe = ftdi_mtxorb_hack_setup,
  93. };
  94. static struct ftdi_sio_quirk ftdi_USB_UIRT_quirk = {
  95. .port_probe = ftdi_USB_UIRT_setup,
  96. };
  97. static struct ftdi_sio_quirk ftdi_HE_TIRA1_quirk = {
  98. .port_probe = ftdi_HE_TIRA1_setup,
  99. };
  100. /*
  101. * The 8U232AM has the same API as the sio except for:
  102. * - it can support MUCH higher baudrates; up to:
  103. * o 921600 for RS232 and 2000000 for RS422/485 at 48MHz
  104. * o 230400 at 12MHz
  105. * so .. 8U232AM's baudrate setting codes are different
  106. * - it has a two byte status code.
  107. * - it returns characters every 16ms (the FTDI does it every 40ms)
  108. *
  109. * the bcdDevice value is used to differentiate FT232BM and FT245BM from
  110. * the earlier FT8U232AM and FT8U232BM. For now, include all known VID/PID
  111. * combinations in both tables.
  112. * FIXME: perhaps bcdDevice can also identify 12MHz FT8U232AM devices,
  113. * but I don't know if those ever went into mass production. [Ian Abbott]
  114. */
  115. static struct usb_device_id id_table_combined [] = {
  116. { USB_DEVICE(FTDI_VID, FTDI_AMC232_PID) },
  117. { USB_DEVICE(FTDI_VID, FTDI_CANUSB_PID) },
  118. { USB_DEVICE(FTDI_VID, FTDI_ACTZWAVE_PID) },
  119. { USB_DEVICE(FTDI_VID, FTDI_IRTRANS_PID) },
  120. { USB_DEVICE(FTDI_VID, FTDI_IPLUS_PID) },
  121. { USB_DEVICE(FTDI_VID, FTDI_IPLUS2_PID) },
  122. { USB_DEVICE(FTDI_VID, FTDI_DMX4ALL) },
  123. { USB_DEVICE(FTDI_VID, FTDI_SIO_PID) },
  124. { USB_DEVICE(FTDI_VID, FTDI_8U232AM_PID) },
  125. { USB_DEVICE(FTDI_VID, FTDI_8U232AM_ALT_PID) },
  126. { USB_DEVICE(FTDI_VID, FTDI_232RL_PID) },
  127. { USB_DEVICE(FTDI_VID, FTDI_8U2232C_PID) },
  128. { USB_DEVICE(FTDI_VID, FTDI_MICRO_CHAMELEON_PID) },
  129. { USB_DEVICE(FTDI_VID, FTDI_RELAIS_PID) },
  130. { USB_DEVICE(FTDI_VID, FTDI_OPENDCC_PID) },
  131. { USB_DEVICE(INTERBIOMETRICS_VID, INTERBIOMETRICS_IOBOARD_PID) },
  132. { USB_DEVICE(INTERBIOMETRICS_VID, INTERBIOMETRICS_MINI_IOBOARD_PID) },
  133. { USB_DEVICE(FTDI_VID, FTDI_XF_632_PID) },
  134. { USB_DEVICE(FTDI_VID, FTDI_XF_634_PID) },
  135. { USB_DEVICE(FTDI_VID, FTDI_XF_547_PID) },
  136. { USB_DEVICE(FTDI_VID, FTDI_XF_633_PID) },
  137. { USB_DEVICE(FTDI_VID, FTDI_XF_631_PID) },
  138. { USB_DEVICE(FTDI_VID, FTDI_XF_635_PID) },
  139. { USB_DEVICE(FTDI_VID, FTDI_XF_640_PID) },
  140. { USB_DEVICE(FTDI_VID, FTDI_XF_642_PID) },
  141. { USB_DEVICE(FTDI_VID, FTDI_DSS20_PID) },
  142. { USB_DEVICE(FTDI_NF_RIC_VID, FTDI_NF_RIC_PID) },
  143. { USB_DEVICE(FTDI_VID, FTDI_VNHCPCUSB_D_PID) },
  144. { USB_DEVICE(FTDI_VID, FTDI_MTXORB_0_PID) },
  145. { USB_DEVICE(FTDI_VID, FTDI_MTXORB_1_PID) },
  146. { USB_DEVICE(FTDI_VID, FTDI_MTXORB_2_PID) },
  147. { USB_DEVICE(FTDI_VID, FTDI_MTXORB_3_PID) },
  148. { USB_DEVICE(FTDI_VID, FTDI_MTXORB_4_PID) },
  149. { USB_DEVICE(FTDI_VID, FTDI_MTXORB_5_PID) },
  150. { USB_DEVICE(FTDI_VID, FTDI_MTXORB_6_PID) },
  151. { USB_DEVICE(MTXORB_VK_VID, MTXORB_VK_PID),
  152. .driver_info = (kernel_ulong_t)&ftdi_mtxorb_hack_quirk },
  153. { USB_DEVICE(FTDI_VID, FTDI_PERLE_ULTRAPORT_PID) },
  154. { USB_DEVICE(FTDI_VID, FTDI_PIEGROUP_PID) },
  155. { USB_DEVICE(FTDI_VID, FTDI_TNC_X_PID) },
  156. { USB_DEVICE(FTDI_VID, FTDI_USBX_707_PID) },
  157. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2101_PID) },
  158. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2102_PID) },
  159. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2103_PID) },
  160. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2104_PID) },
  161. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2106_PID) },
  162. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2201_1_PID) },
  163. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2201_2_PID) },
  164. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2202_1_PID) },
  165. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2202_2_PID) },
  166. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2203_1_PID) },
  167. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2203_2_PID) },
  168. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2401_1_PID) },
  169. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2401_2_PID) },
  170. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2401_3_PID) },
  171. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2401_4_PID) },
  172. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2402_1_PID) },
  173. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2402_2_PID) },
  174. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2402_3_PID) },
  175. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2402_4_PID) },
  176. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2403_1_PID) },
  177. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2403_2_PID) },
  178. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2403_3_PID) },
  179. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2403_4_PID) },
  180. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_1_PID) },
  181. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_2_PID) },
  182. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_3_PID) },
  183. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_4_PID) },
  184. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_5_PID) },
  185. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_6_PID) },
  186. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_7_PID) },
  187. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2801_8_PID) },
  188. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_1_PID) },
  189. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_2_PID) },
  190. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_3_PID) },
  191. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_4_PID) },
  192. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_5_PID) },
  193. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_6_PID) },
  194. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_7_PID) },
  195. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2802_8_PID) },
  196. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_1_PID) },
  197. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_2_PID) },
  198. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_3_PID) },
  199. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_4_PID) },
  200. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_5_PID) },
  201. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_6_PID) },
  202. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_7_PID) },
  203. { USB_DEVICE(SEALEVEL_VID, SEALEVEL_2803_8_PID) },
  204. { USB_DEVICE(IDTECH_VID, IDTECH_IDT1221U_PID) },
  205. { USB_DEVICE(OCT_VID, OCT_US101_PID) },
  206. { USB_DEVICE(FTDI_VID, FTDI_HE_TIRA1_PID),
  207. .driver_info = (kernel_ulong_t)&ftdi_HE_TIRA1_quirk },
  208. { USB_DEVICE(FTDI_VID, FTDI_USB_UIRT_PID),
  209. .driver_info = (kernel_ulong_t)&ftdi_USB_UIRT_quirk },
  210. { USB_DEVICE(FTDI_VID, PROTEGO_SPECIAL_1) },
  211. { USB_DEVICE(FTDI_VID, PROTEGO_R2X0) },
  212. { USB_DEVICE(FTDI_VID, PROTEGO_SPECIAL_3) },
  213. { USB_DEVICE(FTDI_VID, PROTEGO_SPECIAL_4) },
  214. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E808_PID) },
  215. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E809_PID) },
  216. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E80A_PID) },
  217. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E80B_PID) },
  218. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E80C_PID) },
  219. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E80D_PID) },
  220. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E80E_PID) },
  221. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E80F_PID) },
  222. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E888_PID) },
  223. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E889_PID) },
  224. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E88A_PID) },
  225. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E88B_PID) },
  226. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E88C_PID) },
  227. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E88D_PID) },
  228. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E88E_PID) },
  229. { USB_DEVICE(FTDI_VID, FTDI_GUDEADS_E88F_PID) },
  230. { USB_DEVICE(FTDI_VID, FTDI_ELV_UO100_PID) },
  231. { USB_DEVICE(FTDI_VID, FTDI_ELV_UM100_PID) },
  232. { USB_DEVICE(FTDI_VID, FTDI_ELV_UR100_PID) },
  233. { USB_DEVICE(FTDI_VID, FTDI_ELV_ALC8500_PID) },
  234. { USB_DEVICE(FTDI_VID, FTDI_PYRAMID_PID) },
  235. { USB_DEVICE(FTDI_VID, FTDI_ELV_FHZ1000PC_PID) },
  236. { USB_DEVICE(FTDI_VID, FTDI_IBS_US485_PID) },
  237. { USB_DEVICE(FTDI_VID, FTDI_IBS_PICPRO_PID) },
  238. { USB_DEVICE(FTDI_VID, FTDI_IBS_PCMCIA_PID) },
  239. { USB_DEVICE(FTDI_VID, FTDI_IBS_PK1_PID) },
  240. { USB_DEVICE(FTDI_VID, FTDI_IBS_RS232MON_PID) },
  241. { USB_DEVICE(FTDI_VID, FTDI_IBS_APP70_PID) },
  242. { USB_DEVICE(FTDI_VID, FTDI_IBS_PEDO_PID) },
  243. { USB_DEVICE(FTDI_VID, FTDI_IBS_PROD_PID) },
  244. /*
  245. * Due to many user requests for multiple ELV devices we enable
  246. * them by default.
  247. */
  248. { USB_DEVICE(FTDI_VID, FTDI_ELV_CLI7000_PID) },
  249. { USB_DEVICE(FTDI_VID, FTDI_ELV_PPS7330_PID) },
  250. { USB_DEVICE(FTDI_VID, FTDI_ELV_TFM100_PID) },
  251. { USB_DEVICE(FTDI_VID, FTDI_ELV_UDF77_PID) },
  252. { USB_DEVICE(FTDI_VID, FTDI_ELV_UIO88_PID) },
  253. { USB_DEVICE(FTDI_VID, FTDI_ELV_UAD8_PID) },
  254. { USB_DEVICE(FTDI_VID, FTDI_ELV_UDA7_PID) },
  255. { USB_DEVICE(FTDI_VID, FTDI_ELV_USI2_PID) },
  256. { USB_DEVICE(FTDI_VID, FTDI_ELV_T1100_PID) },
  257. { USB_DEVICE(FTDI_VID, FTDI_ELV_PCD200_PID) },
  258. { USB_DEVICE(FTDI_VID, FTDI_ELV_ULA200_PID) },
  259. { USB_DEVICE(FTDI_VID, FTDI_ELV_CSI8_PID) },
  260. { USB_DEVICE(FTDI_VID, FTDI_ELV_EM1000DL_PID) },
  261. { USB_DEVICE(FTDI_VID, FTDI_ELV_PCK100_PID) },
  262. { USB_DEVICE(FTDI_VID, FTDI_ELV_RFP500_PID) },
  263. { USB_DEVICE(FTDI_VID, FTDI_ELV_FS20SIG_PID) },
  264. { USB_DEVICE(FTDI_VID, FTDI_ELV_WS300PC_PID) },
  265. { USB_DEVICE(FTDI_VID, FTDI_ELV_FHZ1300PC_PID) },
  266. { USB_DEVICE(FTDI_VID, FTDI_ELV_EM1010PC_PID) },
  267. { USB_DEVICE(FTDI_VID, FTDI_ELV_WS500_PID) },
  268. { USB_DEVICE(FTDI_VID, LINX_SDMUSBQSS_PID) },
  269. { USB_DEVICE(FTDI_VID, LINX_MASTERDEVEL2_PID) },
  270. { USB_DEVICE(FTDI_VID, LINX_FUTURE_0_PID) },
  271. { USB_DEVICE(FTDI_VID, LINX_FUTURE_1_PID) },
  272. { USB_DEVICE(FTDI_VID, LINX_FUTURE_2_PID) },
  273. { USB_DEVICE(FTDI_VID, FTDI_CCSICDU20_0_PID) },
  274. { USB_DEVICE(FTDI_VID, FTDI_CCSICDU40_1_PID) },
  275. { USB_DEVICE(FTDI_VID, FTDI_CCSMACHX_2_PID) },
  276. { USB_DEVICE(FTDI_VID, INSIDE_ACCESSO) },
  277. { USB_DEVICE(INTREPID_VID, INTREPID_VALUECAN_PID) },
  278. { USB_DEVICE(INTREPID_VID, INTREPID_NEOVI_PID) },
  279. { USB_DEVICE(FALCOM_VID, FALCOM_TWIST_PID) },
  280. { USB_DEVICE(FALCOM_VID, FALCOM_SAMBA_PID) },
  281. { USB_DEVICE(FTDI_VID, FTDI_SUUNTO_SPORTS_PID) },
  282. { USB_DEVICE(TTI_VID, TTI_QL355P_PID) },
  283. { USB_DEVICE(FTDI_VID, FTDI_RM_CANVIEW_PID) },
  284. { USB_DEVICE(BANDB_VID, BANDB_USOTL4_PID) },
  285. { USB_DEVICE(BANDB_VID, BANDB_USTL4_PID) },
  286. { USB_DEVICE(BANDB_VID, BANDB_USO9ML2_PID) },
  287. { USB_DEVICE(FTDI_VID, EVER_ECO_PRO_CDS) },
  288. { USB_DEVICE(FTDI_VID, FTDI_4N_GALAXY_DE_1_PID) },
  289. { USB_DEVICE(FTDI_VID, FTDI_4N_GALAXY_DE_2_PID) },
  290. { USB_DEVICE(FTDI_VID, XSENS_CONVERTER_0_PID) },
  291. { USB_DEVICE(FTDI_VID, XSENS_CONVERTER_1_PID) },
  292. { USB_DEVICE(FTDI_VID, XSENS_CONVERTER_2_PID) },
  293. { USB_DEVICE(FTDI_VID, XSENS_CONVERTER_3_PID) },
  294. { USB_DEVICE(FTDI_VID, XSENS_CONVERTER_4_PID) },
  295. { USB_DEVICE(FTDI_VID, XSENS_CONVERTER_5_PID) },
  296. { USB_DEVICE(FTDI_VID, XSENS_CONVERTER_6_PID) },
  297. { USB_DEVICE(FTDI_VID, XSENS_CONVERTER_7_PID) },
  298. { USB_DEVICE(MOBILITY_VID, MOBILITY_USB_SERIAL_PID) },
  299. { USB_DEVICE(FTDI_VID, FTDI_ACTIVE_ROBOTS_PID) },
  300. { USB_DEVICE(FTDI_VID, FTDI_MHAM_KW_PID) },
  301. { USB_DEVICE(FTDI_VID, FTDI_MHAM_YS_PID) },
  302. { USB_DEVICE(FTDI_VID, FTDI_MHAM_Y6_PID) },
  303. { USB_DEVICE(FTDI_VID, FTDI_MHAM_Y8_PID) },
  304. { USB_DEVICE(FTDI_VID, FTDI_MHAM_IC_PID) },
  305. { USB_DEVICE(FTDI_VID, FTDI_MHAM_DB9_PID) },
  306. { USB_DEVICE(FTDI_VID, FTDI_MHAM_RS232_PID) },
  307. { USB_DEVICE(FTDI_VID, FTDI_MHAM_Y9_PID) },
  308. { USB_DEVICE(FTDI_VID, FTDI_TERATRONIK_VCP_PID) },
  309. { USB_DEVICE(FTDI_VID, FTDI_TERATRONIK_D2XX_PID) },
  310. { USB_DEVICE(EVOLUTION_VID, EVOLUTION_ER1_PID) },
  311. { USB_DEVICE(EVOLUTION_VID, EVO_HYBRID_PID) },
  312. { USB_DEVICE(EVOLUTION_VID, EVO_RCM4_PID) },
  313. { USB_DEVICE(FTDI_VID, FTDI_ARTEMIS_PID) },
  314. { USB_DEVICE(FTDI_VID, FTDI_ATIK_ATK16_PID) },
  315. { USB_DEVICE(FTDI_VID, FTDI_ATIK_ATK16C_PID) },
  316. { USB_DEVICE(FTDI_VID, FTDI_ATIK_ATK16HR_PID) },
  317. { USB_DEVICE(FTDI_VID, FTDI_ATIK_ATK16HRC_PID) },
  318. { USB_DEVICE(FTDI_VID, FTDI_ATIK_ATK16IC_PID) },
  319. { USB_DEVICE(KOBIL_VID, KOBIL_CONV_B1_PID) },
  320. { USB_DEVICE(KOBIL_VID, KOBIL_CONV_KAAN_PID) },
  321. { USB_DEVICE(POSIFLEX_VID, POSIFLEX_PP7000_PID) },
  322. { USB_DEVICE(FTDI_VID, FTDI_TTUSB_PID) },
  323. { USB_DEVICE(FTDI_VID, FTDI_ECLO_COM_1WIRE_PID) },
  324. { USB_DEVICE(FTDI_VID, FTDI_WESTREX_MODEL_777_PID) },
  325. { USB_DEVICE(FTDI_VID, FTDI_WESTREX_MODEL_8900F_PID) },
  326. { USB_DEVICE(FTDI_VID, FTDI_PCDJ_DAC2_PID) },
  327. { USB_DEVICE(FTDI_VID, FTDI_RRCIRKITS_LOCOBUFFER_PID) },
  328. { USB_DEVICE(FTDI_VID, FTDI_ASK_RDR400_PID) },
  329. { USB_DEVICE(ICOM_ID1_VID, ICOM_ID1_PID) },
  330. { USB_DEVICE(PAPOUCH_VID, PAPOUCH_TMU_PID) },
  331. { USB_DEVICE(FTDI_VID, FTDI_ACG_HFDUAL_PID) },
  332. { USB_DEVICE(FTDI_VID, FTDI_YEI_SERVOCENTER31_PID) },
  333. { USB_DEVICE(FTDI_VID, FTDI_THORLABS_PID) },
  334. { USB_DEVICE(TESTO_VID, TESTO_USB_INTERFACE_PID) },
  335. { USB_DEVICE(FTDI_VID, FTDI_GAMMA_SCOUT_PID) },
  336. { USB_DEVICE(FTDI_VID, FTDI_TACTRIX_OPENPORT_13M_PID) },
  337. { USB_DEVICE(FTDI_VID, FTDI_TACTRIX_OPENPORT_13S_PID) },
  338. { USB_DEVICE(FTDI_VID, FTDI_TACTRIX_OPENPORT_13U_PID) },
  339. { USB_DEVICE(ELEKTOR_VID, ELEKTOR_FT323R_PID) },
  340. { USB_DEVICE(TELLDUS_VID, TELLDUS_TELLSTICK_PID) },
  341. { USB_DEVICE(FTDI_VID, FTDI_MAXSTREAM_PID) },
  342. { USB_DEVICE(TML_VID, TML_USB_SERIAL_PID) },
  343. { USB_DEVICE(FTDI_VID, FTDI_ELSTER_UNICOM_PID) },
  344. { USB_DEVICE(OLIMEX_VID, OLIMEX_ARM_USB_OCD_PID),
  345. .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
  346. { USB_DEVICE(FIC_VID, FIC_NEO1973_DEBUG_PID),
  347. .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
  348. { USB_DEVICE(FTDI_VID, FTDI_OOCDLINK_PID),
  349. .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
  350. { }, /* Optional parameter entry */
  351. { } /* Terminating entry */
  352. };
  353. MODULE_DEVICE_TABLE (usb, id_table_combined);
  354. static struct usb_driver ftdi_driver = {
  355. .name = "ftdi_sio",
  356. .probe = usb_serial_probe,
  357. .disconnect = usb_serial_disconnect,
  358. .id_table = id_table_combined,
  359. .no_dynamic_id = 1,
  360. };
  361. static const char *ftdi_chip_name[] = {
  362. [SIO] = "SIO", /* the serial part of FT8U100AX */
  363. [FT8U232AM] = "FT8U232AM",
  364. [FT232BM] = "FT232BM",
  365. [FT2232C] = "FT2232C",
  366. [FT232RL] = "FT232RL",
  367. };
  368. /* Constants for read urb and write urb */
  369. #define BUFSZ 512
  370. #define PKTSZ 64
  371. /* rx_flags */
  372. #define THROTTLED 0x01
  373. #define ACTUALLY_THROTTLED 0x02
  374. /* Used for TIOCMIWAIT */
  375. #define FTDI_STATUS_B0_MASK (FTDI_RS0_CTS | FTDI_RS0_DSR | FTDI_RS0_RI | FTDI_RS0_RLSD)
  376. #define FTDI_STATUS_B1_MASK (FTDI_RS_BI)
  377. /* End TIOCMIWAIT */
  378. #define FTDI_IMPL_ASYNC_FLAGS = (ASYNC_SPD_HI | ASYNC_SPD_VHI \
  379. | ASYNC_SPD_CUST | ASYNC_SPD_SHI | ASYNC_SPD_WARP)
  380. /* function prototypes for a FTDI serial converter */
  381. static int ftdi_sio_probe (struct usb_serial *serial, const struct usb_device_id *id);
  382. static void ftdi_shutdown (struct usb_serial *serial);
  383. static int ftdi_sio_port_probe (struct usb_serial_port *port);
  384. static int ftdi_sio_port_remove (struct usb_serial_port *port);
  385. static int ftdi_open (struct usb_serial_port *port, struct file *filp);
  386. static void ftdi_close (struct usb_serial_port *port, struct file *filp);
  387. static int ftdi_write (struct usb_serial_port *port, const unsigned char *buf, int count);
  388. static int ftdi_write_room (struct usb_serial_port *port);
  389. static int ftdi_chars_in_buffer (struct usb_serial_port *port);
  390. static void ftdi_write_bulk_callback (struct urb *urb);
  391. static void ftdi_read_bulk_callback (struct urb *urb);
  392. static void ftdi_process_read (struct work_struct *work);
  393. static void ftdi_set_termios (struct usb_serial_port *port, struct ktermios * old);
  394. static int ftdi_tiocmget (struct usb_serial_port *port, struct file *file);
  395. static int ftdi_tiocmset (struct usb_serial_port *port, struct file * file, unsigned int set, unsigned int clear);
  396. static int ftdi_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
  397. static void ftdi_break_ctl (struct usb_serial_port *port, int break_state );
  398. static void ftdi_throttle (struct usb_serial_port *port);
  399. static void ftdi_unthrottle (struct usb_serial_port *port);
  400. static unsigned short int ftdi_232am_baud_base_to_divisor (int baud, int base);
  401. static unsigned short int ftdi_232am_baud_to_divisor (int baud);
  402. static __u32 ftdi_232bm_baud_base_to_divisor (int baud, int base);
  403. static __u32 ftdi_232bm_baud_to_divisor (int baud);
  404. static struct usb_serial_driver ftdi_sio_device = {
  405. .driver = {
  406. .owner = THIS_MODULE,
  407. .name = "ftdi_sio",
  408. },
  409. .description = "FTDI USB Serial Device",
  410. .usb_driver = &ftdi_driver ,
  411. .id_table = id_table_combined,
  412. .num_interrupt_in = 0,
  413. .num_bulk_in = 1,
  414. .num_bulk_out = 1,
  415. .num_ports = 1,
  416. .probe = ftdi_sio_probe,
  417. .port_probe = ftdi_sio_port_probe,
  418. .port_remove = ftdi_sio_port_remove,
  419. .open = ftdi_open,
  420. .close = ftdi_close,
  421. .throttle = ftdi_throttle,
  422. .unthrottle = ftdi_unthrottle,
  423. .write = ftdi_write,
  424. .write_room = ftdi_write_room,
  425. .chars_in_buffer = ftdi_chars_in_buffer,
  426. .read_bulk_callback = ftdi_read_bulk_callback,
  427. .write_bulk_callback = ftdi_write_bulk_callback,
  428. .tiocmget = ftdi_tiocmget,
  429. .tiocmset = ftdi_tiocmset,
  430. .ioctl = ftdi_ioctl,
  431. .set_termios = ftdi_set_termios,
  432. .break_ctl = ftdi_break_ctl,
  433. .shutdown = ftdi_shutdown,
  434. };
  435. #define WDR_TIMEOUT 5000 /* default urb timeout */
  436. #define WDR_SHORT_TIMEOUT 1000 /* shorter urb timeout */
  437. /* High and low are for DTR, RTS etc etc */
  438. #define HIGH 1
  439. #define LOW 0
  440. /* number of outstanding urbs to prevent userspace DoS from happening */
  441. #define URB_UPPER_LIMIT 42
  442. /*
  443. * ***************************************************************************
  444. * Utility functions
  445. * ***************************************************************************
  446. */
  447. static unsigned short int ftdi_232am_baud_base_to_divisor(int baud, int base)
  448. {
  449. unsigned short int divisor;
  450. int divisor3 = base / 2 / baud; // divisor shifted 3 bits to the left
  451. if ((divisor3 & 0x7) == 7) divisor3 ++; // round x.7/8 up to x+1
  452. divisor = divisor3 >> 3;
  453. divisor3 &= 0x7;
  454. if (divisor3 == 1) divisor |= 0xc000; else // 0.125
  455. if (divisor3 >= 4) divisor |= 0x4000; else // 0.5
  456. if (divisor3 != 0) divisor |= 0x8000; // 0.25
  457. if (divisor == 1) divisor = 0; /* special case for maximum baud rate */
  458. return divisor;
  459. }
  460. static unsigned short int ftdi_232am_baud_to_divisor(int baud)
  461. {
  462. return(ftdi_232am_baud_base_to_divisor(baud, 48000000));
  463. }
  464. static __u32 ftdi_232bm_baud_base_to_divisor(int baud, int base)
  465. {
  466. static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 };
  467. __u32 divisor;
  468. int divisor3 = base / 2 / baud; // divisor shifted 3 bits to the left
  469. divisor = divisor3 >> 3;
  470. divisor |= (__u32)divfrac[divisor3 & 0x7] << 14;
  471. /* Deal with special cases for highest baud rates. */
  472. if (divisor == 1) divisor = 0; else // 1.0
  473. if (divisor == 0x4001) divisor = 1; // 1.5
  474. return divisor;
  475. }
  476. static __u32 ftdi_232bm_baud_to_divisor(int baud)
  477. {
  478. return(ftdi_232bm_baud_base_to_divisor(baud, 48000000));
  479. }
  480. #define set_mctrl(port, set) update_mctrl((port), (set), 0)
  481. #define clear_mctrl(port, clear) update_mctrl((port), 0, (clear))
  482. static int update_mctrl(struct usb_serial_port *port, unsigned int set, unsigned int clear)
  483. {
  484. struct ftdi_private *priv = usb_get_serial_port_data(port);
  485. char *buf;
  486. unsigned urb_value;
  487. int rv;
  488. if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
  489. dbg("%s - DTR|RTS not being set|cleared", __FUNCTION__);
  490. return 0; /* no change */
  491. }
  492. buf = kmalloc(1, GFP_NOIO);
  493. if (!buf) {
  494. return -ENOMEM;
  495. }
  496. clear &= ~set; /* 'set' takes precedence over 'clear' */
  497. urb_value = 0;
  498. if (clear & TIOCM_DTR)
  499. urb_value |= FTDI_SIO_SET_DTR_LOW;
  500. if (clear & TIOCM_RTS)
  501. urb_value |= FTDI_SIO_SET_RTS_LOW;
  502. if (set & TIOCM_DTR)
  503. urb_value |= FTDI_SIO_SET_DTR_HIGH;
  504. if (set & TIOCM_RTS)
  505. urb_value |= FTDI_SIO_SET_RTS_HIGH;
  506. rv = usb_control_msg(port->serial->dev,
  507. usb_sndctrlpipe(port->serial->dev, 0),
  508. FTDI_SIO_SET_MODEM_CTRL_REQUEST,
  509. FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE,
  510. urb_value, priv->interface,
  511. buf, 0, WDR_TIMEOUT);
  512. kfree(buf);
  513. if (rv < 0) {
  514. err("%s Error from MODEM_CTRL urb: DTR %s, RTS %s",
  515. __FUNCTION__,
  516. (set & TIOCM_DTR) ? "HIGH" :
  517. (clear & TIOCM_DTR) ? "LOW" : "unchanged",
  518. (set & TIOCM_RTS) ? "HIGH" :
  519. (clear & TIOCM_RTS) ? "LOW" : "unchanged");
  520. } else {
  521. dbg("%s - DTR %s, RTS %s", __FUNCTION__,
  522. (set & TIOCM_DTR) ? "HIGH" :
  523. (clear & TIOCM_DTR) ? "LOW" : "unchanged",
  524. (set & TIOCM_RTS) ? "HIGH" :
  525. (clear & TIOCM_RTS) ? "LOW" : "unchanged");
  526. priv->last_dtr_rts = (priv->last_dtr_rts & ~clear) | set;
  527. }
  528. return rv;
  529. }
  530. static __u32 get_ftdi_divisor(struct usb_serial_port * port);
  531. static int change_speed(struct usb_serial_port *port)
  532. {
  533. struct ftdi_private *priv = usb_get_serial_port_data(port);
  534. char *buf;
  535. __u16 urb_value;
  536. __u16 urb_index;
  537. __u32 urb_index_value;
  538. int rv;
  539. buf = kmalloc(1, GFP_NOIO);
  540. if (!buf)
  541. return -ENOMEM;
  542. urb_index_value = get_ftdi_divisor(port);
  543. urb_value = (__u16)urb_index_value;
  544. urb_index = (__u16)(urb_index_value >> 16);
  545. if (priv->interface) { /* FT2232C */
  546. urb_index = (__u16)((urb_index << 8) | priv->interface);
  547. }
  548. rv = usb_control_msg(port->serial->dev,
  549. usb_sndctrlpipe(port->serial->dev, 0),
  550. FTDI_SIO_SET_BAUDRATE_REQUEST,
  551. FTDI_SIO_SET_BAUDRATE_REQUEST_TYPE,
  552. urb_value, urb_index,
  553. buf, 0, WDR_SHORT_TIMEOUT);
  554. kfree(buf);
  555. return rv;
  556. }
  557. static __u32 get_ftdi_divisor(struct usb_serial_port * port)
  558. { /* get_ftdi_divisor */
  559. struct ftdi_private *priv = usb_get_serial_port_data(port);
  560. __u32 div_value = 0;
  561. int div_okay = 1;
  562. int baud;
  563. /*
  564. * The logic involved in setting the baudrate can be cleanly split in 3 steps.
  565. * Obtaining the actual baud rate is a little tricky since unix traditionally
  566. * somehow ignored the possibility to set non-standard baud rates.
  567. * 1. Standard baud rates are set in tty->termios->c_cflag
  568. * 2. If these are not enough, you can set any speed using alt_speed as follows:
  569. * - set tty->termios->c_cflag speed to B38400
  570. * - set your real speed in tty->alt_speed; it gets ignored when
  571. * alt_speed==0, (or)
  572. * - call TIOCSSERIAL ioctl with (struct serial_struct) set as follows:
  573. * flags & ASYNC_SPD_MASK == ASYNC_SPD_[HI, VHI, SHI, WARP], this just
  574. * sets alt_speed to (HI: 57600, VHI: 115200, SHI: 230400, WARP: 460800)
  575. * ** Steps 1, 2 are done courtesy of tty_get_baud_rate
  576. * 3. You can also set baud rate by setting custom divisor as follows
  577. * - set tty->termios->c_cflag speed to B38400
  578. * - call TIOCSSERIAL ioctl with (struct serial_struct) set as follows:
  579. * o flags & ASYNC_SPD_MASK == ASYNC_SPD_CUST
  580. * o custom_divisor set to baud_base / your_new_baudrate
  581. * ** Step 3 is done courtesy of code borrowed from serial.c - I should really
  582. * spend some time and separate+move this common code to serial.c, it is
  583. * replicated in nearly every serial driver you see.
  584. */
  585. /* 1. Get the baud rate from the tty settings, this observes alt_speed hack */
  586. baud = tty_get_baud_rate(port->tty);
  587. dbg("%s - tty_get_baud_rate reports speed %d", __FUNCTION__, baud);
  588. /* 2. Observe async-compatible custom_divisor hack, update baudrate if needed */
  589. if (baud == 38400 &&
  590. ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) &&
  591. (priv->custom_divisor)) {
  592. baud = priv->baud_base / priv->custom_divisor;
  593. dbg("%s - custom divisor %d sets baud rate to %d", __FUNCTION__, priv->custom_divisor, baud);
  594. }
  595. /* 3. Convert baudrate to device-specific divisor */
  596. if (!baud) baud = 9600;
  597. switch(priv->chip_type) {
  598. case SIO: /* SIO chip */
  599. switch(baud) {
  600. case 300: div_value = ftdi_sio_b300; break;
  601. case 600: div_value = ftdi_sio_b600; break;
  602. case 1200: div_value = ftdi_sio_b1200; break;
  603. case 2400: div_value = ftdi_sio_b2400; break;
  604. case 4800: div_value = ftdi_sio_b4800; break;
  605. case 9600: div_value = ftdi_sio_b9600; break;
  606. case 19200: div_value = ftdi_sio_b19200; break;
  607. case 38400: div_value = ftdi_sio_b38400; break;
  608. case 57600: div_value = ftdi_sio_b57600; break;
  609. case 115200: div_value = ftdi_sio_b115200; break;
  610. } /* baud */
  611. if (div_value == 0) {
  612. dbg("%s - Baudrate (%d) requested is not supported", __FUNCTION__, baud);
  613. div_value = ftdi_sio_b9600;
  614. baud = 9600;
  615. div_okay = 0;
  616. }
  617. break;
  618. case FT8U232AM: /* 8U232AM chip */
  619. if (baud <= 3000000) {
  620. div_value = ftdi_232am_baud_to_divisor(baud);
  621. } else {
  622. dbg("%s - Baud rate too high!", __FUNCTION__);
  623. baud = 9600;
  624. div_value = ftdi_232am_baud_to_divisor(9600);
  625. div_okay = 0;
  626. }
  627. break;
  628. case FT232BM: /* FT232BM chip */
  629. case FT2232C: /* FT2232C chip */
  630. case FT232RL:
  631. if (baud <= 3000000) {
  632. div_value = ftdi_232bm_baud_to_divisor(baud);
  633. } else {
  634. dbg("%s - Baud rate too high!", __FUNCTION__);
  635. div_value = ftdi_232bm_baud_to_divisor(9600);
  636. div_okay = 0;
  637. baud = 9600;
  638. }
  639. break;
  640. } /* priv->chip_type */
  641. if (div_okay) {
  642. dbg("%s - Baud rate set to %d (divisor 0x%lX) on chip %s",
  643. __FUNCTION__, baud, (unsigned long)div_value,
  644. ftdi_chip_name[priv->chip_type]);
  645. }
  646. tty_encode_baud_rate(port->tty, baud, baud);
  647. return(div_value);
  648. }
  649. static int get_serial_info(struct usb_serial_port * port, struct serial_struct __user * retinfo)
  650. {
  651. struct ftdi_private *priv = usb_get_serial_port_data(port);
  652. struct serial_struct tmp;
  653. if (!retinfo)
  654. return -EFAULT;
  655. memset(&tmp, 0, sizeof(tmp));
  656. tmp.flags = priv->flags;
  657. tmp.baud_base = priv->baud_base;
  658. tmp.custom_divisor = priv->custom_divisor;
  659. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  660. return -EFAULT;
  661. return 0;
  662. } /* get_serial_info */
  663. static int set_serial_info(struct usb_serial_port * port, struct serial_struct __user * newinfo)
  664. { /* set_serial_info */
  665. struct ftdi_private *priv = usb_get_serial_port_data(port);
  666. struct serial_struct new_serial;
  667. struct ftdi_private old_priv;
  668. if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
  669. return -EFAULT;
  670. old_priv = * priv;
  671. /* Do error checking and permission checking */
  672. if (!capable(CAP_SYS_ADMIN)) {
  673. if (((new_serial.flags & ~ASYNC_USR_MASK) !=
  674. (priv->flags & ~ASYNC_USR_MASK)))
  675. return -EPERM;
  676. priv->flags = ((priv->flags & ~ASYNC_USR_MASK) |
  677. (new_serial.flags & ASYNC_USR_MASK));
  678. priv->custom_divisor = new_serial.custom_divisor;
  679. goto check_and_exit;
  680. }
  681. if ((new_serial.baud_base != priv->baud_base) &&
  682. (new_serial.baud_base < 9600))
  683. return -EINVAL;
  684. /* Make the changes - these are privileged changes! */
  685. priv->flags = ((priv->flags & ~ASYNC_FLAGS) |
  686. (new_serial.flags & ASYNC_FLAGS));
  687. priv->custom_divisor = new_serial.custom_divisor;
  688. port->tty->low_latency = (priv->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  689. check_and_exit:
  690. if ((old_priv.flags & ASYNC_SPD_MASK) !=
  691. (priv->flags & ASYNC_SPD_MASK)) {
  692. if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  693. port->tty->alt_speed = 57600;
  694. else if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  695. port->tty->alt_speed = 115200;
  696. else if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  697. port->tty->alt_speed = 230400;
  698. else if ((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  699. port->tty->alt_speed = 460800;
  700. else
  701. port->tty->alt_speed = 0;
  702. }
  703. if (((old_priv.flags & ASYNC_SPD_MASK) !=
  704. (priv->flags & ASYNC_SPD_MASK)) ||
  705. (((priv->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST) &&
  706. (old_priv.custom_divisor != priv->custom_divisor))) {
  707. change_speed(port);
  708. }
  709. return (0);
  710. } /* set_serial_info */
  711. /* Determine type of FTDI chip based on USB config and descriptor. */
  712. static void ftdi_determine_type(struct usb_serial_port *port)
  713. {
  714. struct ftdi_private *priv = usb_get_serial_port_data(port);
  715. struct usb_serial *serial = port->serial;
  716. struct usb_device *udev = serial->dev;
  717. unsigned version;
  718. unsigned interfaces;
  719. /* Assume it is not the original SIO device for now. */
  720. priv->baud_base = 48000000 / 2;
  721. priv->write_offset = 0;
  722. version = le16_to_cpu(udev->descriptor.bcdDevice);
  723. interfaces = udev->actconfig->desc.bNumInterfaces;
  724. dbg("%s: bcdDevice = 0x%x, bNumInterfaces = %u", __FUNCTION__,
  725. version, interfaces);
  726. if (interfaces > 1) {
  727. int inter;
  728. /* Multiple interfaces. Assume FT2232C. */
  729. priv->chip_type = FT2232C;
  730. /* Determine interface code. */
  731. inter = serial->interface->altsetting->desc.bInterfaceNumber;
  732. if (inter == 0) {
  733. priv->interface = PIT_SIOA;
  734. } else {
  735. priv->interface = PIT_SIOB;
  736. }
  737. /* BM-type devices have a bug where bcdDevice gets set
  738. * to 0x200 when iSerialNumber is 0. */
  739. if (version < 0x500) {
  740. dbg("%s: something fishy - bcdDevice too low for multi-interface device",
  741. __FUNCTION__);
  742. }
  743. } else if (version < 0x200) {
  744. /* Old device. Assume its the original SIO. */
  745. priv->chip_type = SIO;
  746. priv->baud_base = 12000000 / 16;
  747. priv->write_offset = 1;
  748. } else if (version < 0x400) {
  749. /* Assume its an FT8U232AM (or FT8U245AM) */
  750. /* (It might be a BM because of the iSerialNumber bug,
  751. * but it will still work as an AM device.) */
  752. priv->chip_type = FT8U232AM;
  753. } else if (version < 0x600) {
  754. /* Assume its an FT232BM (or FT245BM) */
  755. priv->chip_type = FT232BM;
  756. } else {
  757. /* Assume its an FT232R */
  758. priv->chip_type = FT232RL;
  759. }
  760. info("Detected %s", ftdi_chip_name[priv->chip_type]);
  761. }
  762. /*
  763. * ***************************************************************************
  764. * Sysfs Attribute
  765. * ***************************************************************************
  766. */
  767. static ssize_t show_latency_timer(struct device *dev, struct device_attribute *attr, char *buf)
  768. {
  769. struct usb_serial_port *port = to_usb_serial_port(dev);
  770. struct ftdi_private *priv = usb_get_serial_port_data(port);
  771. struct usb_device *udev = port->serial->dev;
  772. unsigned short latency = 0;
  773. int rv = 0;
  774. dbg("%s",__FUNCTION__);
  775. rv = usb_control_msg(udev,
  776. usb_rcvctrlpipe(udev, 0),
  777. FTDI_SIO_GET_LATENCY_TIMER_REQUEST,
  778. FTDI_SIO_GET_LATENCY_TIMER_REQUEST_TYPE,
  779. 0, priv->interface,
  780. (char*) &latency, 1, WDR_TIMEOUT);
  781. if (rv < 0) {
  782. dev_err(dev, "Unable to read latency timer: %i\n", rv);
  783. return -EIO;
  784. }
  785. return sprintf(buf, "%i\n", latency);
  786. }
  787. /* Write a new value of the latency timer, in units of milliseconds. */
  788. static ssize_t store_latency_timer(struct device *dev, struct device_attribute *attr, const char *valbuf,
  789. size_t count)
  790. {
  791. struct usb_serial_port *port = to_usb_serial_port(dev);
  792. struct ftdi_private *priv = usb_get_serial_port_data(port);
  793. struct usb_device *udev = port->serial->dev;
  794. char buf[1];
  795. int v = simple_strtoul(valbuf, NULL, 10);
  796. int rv = 0;
  797. dbg("%s: setting latency timer = %i", __FUNCTION__, v);
  798. rv = usb_control_msg(udev,
  799. usb_sndctrlpipe(udev, 0),
  800. FTDI_SIO_SET_LATENCY_TIMER_REQUEST,
  801. FTDI_SIO_SET_LATENCY_TIMER_REQUEST_TYPE,
  802. v, priv->interface,
  803. buf, 0, WDR_TIMEOUT);
  804. if (rv < 0) {
  805. dev_err(dev, "Unable to write latency timer: %i\n", rv);
  806. return -EIO;
  807. }
  808. return count;
  809. }
  810. /* Write an event character directly to the FTDI register. The ASCII
  811. value is in the low 8 bits, with the enable bit in the 9th bit. */
  812. static ssize_t store_event_char(struct device *dev, struct device_attribute *attr, const char *valbuf,
  813. size_t count)
  814. {
  815. struct usb_serial_port *port = to_usb_serial_port(dev);
  816. struct ftdi_private *priv = usb_get_serial_port_data(port);
  817. struct usb_device *udev = port->serial->dev;
  818. char buf[1];
  819. int v = simple_strtoul(valbuf, NULL, 10);
  820. int rv = 0;
  821. dbg("%s: setting event char = %i", __FUNCTION__, v);
  822. rv = usb_control_msg(udev,
  823. usb_sndctrlpipe(udev, 0),
  824. FTDI_SIO_SET_EVENT_CHAR_REQUEST,
  825. FTDI_SIO_SET_EVENT_CHAR_REQUEST_TYPE,
  826. v, priv->interface,
  827. buf, 0, WDR_TIMEOUT);
  828. if (rv < 0) {
  829. dbg("Unable to write event character: %i", rv);
  830. return -EIO;
  831. }
  832. return count;
  833. }
  834. static DEVICE_ATTR(latency_timer, S_IWUSR | S_IRUGO, show_latency_timer, store_latency_timer);
  835. static DEVICE_ATTR(event_char, S_IWUSR, NULL, store_event_char);
  836. static int create_sysfs_attrs(struct usb_serial_port *port)
  837. {
  838. struct ftdi_private *priv = usb_get_serial_port_data(port);
  839. int retval = 0;
  840. dbg("%s",__FUNCTION__);
  841. /* XXX I've no idea if the original SIO supports the event_char
  842. * sysfs parameter, so I'm playing it safe. */
  843. if (priv->chip_type != SIO) {
  844. dbg("sysfs attributes for %s", ftdi_chip_name[priv->chip_type]);
  845. retval = device_create_file(&port->dev, &dev_attr_event_char);
  846. if ((!retval) &&
  847. (priv->chip_type == FT232BM ||
  848. priv->chip_type == FT2232C ||
  849. priv->chip_type == FT232RL)) {
  850. retval = device_create_file(&port->dev,
  851. &dev_attr_latency_timer);
  852. }
  853. }
  854. return retval;
  855. }
  856. static void remove_sysfs_attrs(struct usb_serial_port *port)
  857. {
  858. struct ftdi_private *priv = usb_get_serial_port_data(port);
  859. dbg("%s",__FUNCTION__);
  860. /* XXX see create_sysfs_attrs */
  861. if (priv->chip_type != SIO) {
  862. device_remove_file(&port->dev, &dev_attr_event_char);
  863. if (priv->chip_type == FT232BM ||
  864. priv->chip_type == FT2232C ||
  865. priv->chip_type == FT232RL) {
  866. device_remove_file(&port->dev, &dev_attr_latency_timer);
  867. }
  868. }
  869. }
  870. /*
  871. * ***************************************************************************
  872. * FTDI driver specific functions
  873. * ***************************************************************************
  874. */
  875. /* Probe function to check for special devices */
  876. static int ftdi_sio_probe (struct usb_serial *serial, const struct usb_device_id *id)
  877. {
  878. struct ftdi_sio_quirk *quirk = (struct ftdi_sio_quirk *)id->driver_info;
  879. if (quirk && quirk->probe) {
  880. int ret = quirk->probe(serial);
  881. if (ret != 0)
  882. return ret;
  883. }
  884. usb_set_serial_data(serial, (void *)id->driver_info);
  885. return 0;
  886. }
  887. static int ftdi_sio_port_probe(struct usb_serial_port *port)
  888. {
  889. struct ftdi_private *priv;
  890. struct ftdi_sio_quirk *quirk = usb_get_serial_data(port->serial);
  891. dbg("%s",__FUNCTION__);
  892. priv = kzalloc(sizeof(struct ftdi_private), GFP_KERNEL);
  893. if (!priv){
  894. err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct ftdi_private));
  895. return -ENOMEM;
  896. }
  897. spin_lock_init(&priv->rx_lock);
  898. spin_lock_init(&priv->tx_lock);
  899. init_waitqueue_head(&priv->delta_msr_wait);
  900. /* This will push the characters through immediately rather
  901. than queue a task to deliver them */
  902. priv->flags = ASYNC_LOW_LATENCY;
  903. if (quirk && quirk->port_probe)
  904. quirk->port_probe(priv);
  905. /* Increase the size of read buffers */
  906. kfree(port->bulk_in_buffer);
  907. port->bulk_in_buffer = kmalloc (BUFSZ, GFP_KERNEL);
  908. if (!port->bulk_in_buffer) {
  909. kfree (priv);
  910. return -ENOMEM;
  911. }
  912. if (port->read_urb) {
  913. port->read_urb->transfer_buffer = port->bulk_in_buffer;
  914. port->read_urb->transfer_buffer_length = BUFSZ;
  915. }
  916. INIT_DELAYED_WORK(&priv->rx_work, ftdi_process_read);
  917. priv->port = port;
  918. /* Free port's existing write urb and transfer buffer. */
  919. if (port->write_urb) {
  920. usb_free_urb (port->write_urb);
  921. port->write_urb = NULL;
  922. }
  923. kfree(port->bulk_out_buffer);
  924. port->bulk_out_buffer = NULL;
  925. usb_set_serial_port_data(port, priv);
  926. ftdi_determine_type (port);
  927. create_sysfs_attrs(port);
  928. return 0;
  929. }
  930. /* Setup for the USB-UIRT device, which requires hardwired
  931. * baudrate (38400 gets mapped to 312500) */
  932. /* Called from usbserial:serial_probe */
  933. static void ftdi_USB_UIRT_setup (struct ftdi_private *priv)
  934. {
  935. dbg("%s",__FUNCTION__);
  936. priv->flags |= ASYNC_SPD_CUST;
  937. priv->custom_divisor = 77;
  938. priv->force_baud = 38400;
  939. } /* ftdi_USB_UIRT_setup */
  940. /* Setup for the HE-TIRA1 device, which requires hardwired
  941. * baudrate (38400 gets mapped to 100000) and RTS-CTS enabled. */
  942. static void ftdi_HE_TIRA1_setup (struct ftdi_private *priv)
  943. {
  944. dbg("%s",__FUNCTION__);
  945. priv->flags |= ASYNC_SPD_CUST;
  946. priv->custom_divisor = 240;
  947. priv->force_baud = 38400;
  948. priv->force_rtscts = 1;
  949. } /* ftdi_HE_TIRA1_setup */
  950. /*
  951. * First port on JTAG adaptors such as Olimex arm-usb-ocd or the FIC/OpenMoko
  952. * Neo1973 Debug Board is reserved for JTAG interface and can be accessed from
  953. * userspace using openocd.
  954. */
  955. static int ftdi_jtag_probe(struct usb_serial *serial)
  956. {
  957. struct usb_device *udev = serial->dev;
  958. struct usb_interface *interface = serial->interface;
  959. dbg("%s",__FUNCTION__);
  960. if (interface == udev->actconfig->interface[0]) {
  961. info("Ignoring serial port reserved for JTAG");
  962. return -ENODEV;
  963. }
  964. return 0;
  965. }
  966. /*
  967. * The Matrix Orbital VK204-25-USB has an invalid IN endpoint.
  968. * We have to correct it if we want to read from it.
  969. */
  970. static int ftdi_mtxorb_hack_setup(struct usb_serial *serial)
  971. {
  972. struct usb_host_endpoint *ep = serial->dev->ep_in[1];
  973. struct usb_endpoint_descriptor *ep_desc = &ep->desc;
  974. if (ep->enabled && ep_desc->wMaxPacketSize == 0) {
  975. ep_desc->wMaxPacketSize = 0x40;
  976. info("Fixing invalid wMaxPacketSize on read pipe");
  977. }
  978. return 0;
  979. }
  980. /* ftdi_shutdown is called from usbserial:usb_serial_disconnect
  981. * it is called when the usb device is disconnected
  982. *
  983. * usbserial:usb_serial_disconnect
  984. * calls __serial_close for each open of the port
  985. * shutdown is called then (ie ftdi_shutdown)
  986. */
  987. static void ftdi_shutdown (struct usb_serial *serial)
  988. {
  989. dbg("%s", __FUNCTION__);
  990. }
  991. static int ftdi_sio_port_remove(struct usb_serial_port *port)
  992. {
  993. struct ftdi_private *priv = usb_get_serial_port_data(port);
  994. dbg("%s", __FUNCTION__);
  995. remove_sysfs_attrs(port);
  996. /* all open ports are closed at this point
  997. * (by usbserial.c:__serial_close, which calls ftdi_close)
  998. */
  999. if (priv) {
  1000. usb_set_serial_port_data(port, NULL);
  1001. kfree(priv);
  1002. }
  1003. return 0;
  1004. }
  1005. static int ftdi_open (struct usb_serial_port *port, struct file *filp)
  1006. { /* ftdi_open */
  1007. struct usb_device *dev = port->serial->dev;
  1008. struct ftdi_private *priv = usb_get_serial_port_data(port);
  1009. unsigned long flags;
  1010. int result = 0;
  1011. char buf[1]; /* Needed for the usb_control_msg I think */
  1012. dbg("%s", __FUNCTION__);
  1013. spin_lock_irqsave(&priv->tx_lock, flags);
  1014. priv->tx_bytes = 0;
  1015. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1016. spin_lock_irqsave(&priv->rx_lock, flags);
  1017. priv->rx_bytes = 0;
  1018. spin_unlock_irqrestore(&priv->rx_lock, flags);
  1019. if (port->tty)
  1020. port->tty->low_latency = (priv->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  1021. /* No error checking for this (will get errors later anyway) */
  1022. /* See ftdi_sio.h for description of what is reset */
  1023. usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  1024. FTDI_SIO_RESET_REQUEST, FTDI_SIO_RESET_REQUEST_TYPE,
  1025. FTDI_SIO_RESET_SIO,
  1026. priv->interface, buf, 0, WDR_TIMEOUT);
  1027. /* Termios defaults are set by usb_serial_init. We don't change
  1028. port->tty->termios - this would loose speed settings, etc.
  1029. This is same behaviour as serial.c/rs_open() - Kuba */
  1030. /* ftdi_set_termios will send usb control messages */
  1031. if (port->tty)
  1032. ftdi_set_termios(port, port->tty->termios);
  1033. /* FIXME: Flow control might be enabled, so it should be checked -
  1034. we have no control of defaults! */
  1035. /* Turn on RTS and DTR since we are not flow controlling by default */
  1036. set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
  1037. /* Not throttled */
  1038. spin_lock_irqsave(&priv->rx_lock, flags);
  1039. priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
  1040. spin_unlock_irqrestore(&priv->rx_lock, flags);
  1041. /* Start reading from the device */
  1042. priv->rx_processed = 0;
  1043. usb_fill_bulk_urb(port->read_urb, dev,
  1044. usb_rcvbulkpipe(dev, port->bulk_in_endpointAddress),
  1045. port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
  1046. ftdi_read_bulk_callback, port);
  1047. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  1048. if (result)
  1049. err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
  1050. return result;
  1051. } /* ftdi_open */
  1052. /*
  1053. * usbserial:__serial_close only calls ftdi_close if the point is open
  1054. *
  1055. * This only gets called when it is the last close
  1056. *
  1057. *
  1058. */
  1059. static void ftdi_close (struct usb_serial_port *port, struct file *filp)
  1060. { /* ftdi_close */
  1061. unsigned int c_cflag = port->tty->termios->c_cflag;
  1062. struct ftdi_private *priv = usb_get_serial_port_data(port);
  1063. char buf[1];
  1064. dbg("%s", __FUNCTION__);
  1065. mutex_lock(&port->serial->disc_mutex);
  1066. if (c_cflag & HUPCL && !port->serial->disconnected){
  1067. /* Disable flow control */
  1068. if (usb_control_msg(port->serial->dev,
  1069. usb_sndctrlpipe(port->serial->dev, 0),
  1070. FTDI_SIO_SET_FLOW_CTRL_REQUEST,
  1071. FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
  1072. 0, priv->interface, buf, 0,
  1073. WDR_TIMEOUT) < 0) {
  1074. err("error from flowcontrol urb");
  1075. }
  1076. /* drop RTS and DTR */
  1077. clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
  1078. } /* Note change no line if hupcl is off */
  1079. mutex_unlock(&port->serial->disc_mutex);
  1080. /* cancel any scheduled reading */
  1081. cancel_delayed_work(&priv->rx_work);
  1082. flush_scheduled_work();
  1083. /* shutdown our bulk read */
  1084. usb_kill_urb(port->read_urb);
  1085. } /* ftdi_close */
  1086. /* The SIO requires the first byte to have:
  1087. * B0 1
  1088. * B1 0
  1089. * B2..7 length of message excluding byte 0
  1090. *
  1091. * The new devices do not require this byte
  1092. */
  1093. static int ftdi_write (struct usb_serial_port *port,
  1094. const unsigned char *buf, int count)
  1095. { /* ftdi_write */
  1096. struct ftdi_private *priv = usb_get_serial_port_data(port);
  1097. struct urb *urb;
  1098. unsigned char *buffer;
  1099. int data_offset ; /* will be 1 for the SIO and 0 otherwise */
  1100. int status;
  1101. int transfer_size;
  1102. unsigned long flags;
  1103. dbg("%s port %d, %d bytes", __FUNCTION__, port->number, count);
  1104. if (count == 0) {
  1105. dbg("write request of 0 bytes");
  1106. return 0;
  1107. }
  1108. spin_lock_irqsave(&priv->tx_lock, flags);
  1109. if (priv->tx_outstanding_urbs > URB_UPPER_LIMIT) {
  1110. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1111. dbg("%s - write limit hit\n", __FUNCTION__);
  1112. return 0;
  1113. }
  1114. priv->tx_outstanding_urbs++;
  1115. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1116. data_offset = priv->write_offset;
  1117. dbg("data_offset set to %d",data_offset);
  1118. /* Determine total transfer size */
  1119. transfer_size = count;
  1120. if (data_offset > 0) {
  1121. /* Original sio needs control bytes too... */
  1122. transfer_size += (data_offset *
  1123. ((count + (PKTSZ - 1 - data_offset)) /
  1124. (PKTSZ - data_offset)));
  1125. }
  1126. buffer = kmalloc (transfer_size, GFP_ATOMIC);
  1127. if (!buffer) {
  1128. err("%s ran out of kernel memory for urb ...", __FUNCTION__);
  1129. count = -ENOMEM;
  1130. goto error_no_buffer;
  1131. }
  1132. urb = usb_alloc_urb(0, GFP_ATOMIC);
  1133. if (!urb) {
  1134. err("%s - no more free urbs", __FUNCTION__);
  1135. count = -ENOMEM;
  1136. goto error_no_urb;
  1137. }
  1138. /* Copy data */
  1139. if (data_offset > 0) {
  1140. /* Original sio requires control byte at start of each packet. */
  1141. int user_pktsz = PKTSZ - data_offset;
  1142. int todo = count;
  1143. unsigned char *first_byte = buffer;
  1144. const unsigned char *current_position = buf;
  1145. while (todo > 0) {
  1146. if (user_pktsz > todo) {
  1147. user_pktsz = todo;
  1148. }
  1149. /* Write the control byte at the front of the packet*/
  1150. *first_byte = 1 | ((user_pktsz) << 2);
  1151. /* Copy data for packet */
  1152. memcpy (first_byte + data_offset,
  1153. current_position, user_pktsz);
  1154. first_byte += user_pktsz + data_offset;
  1155. current_position += user_pktsz;
  1156. todo -= user_pktsz;
  1157. }
  1158. } else {
  1159. /* No control byte required. */
  1160. /* Copy in the data to send */
  1161. memcpy (buffer, buf, count);
  1162. }
  1163. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, transfer_size, buffer);
  1164. /* fill the buffer and send it */
  1165. usb_fill_bulk_urb(urb, port->serial->dev,
  1166. usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
  1167. buffer, transfer_size,
  1168. ftdi_write_bulk_callback, port);
  1169. status = usb_submit_urb(urb, GFP_ATOMIC);
  1170. if (status) {
  1171. err("%s - failed submitting write urb, error %d", __FUNCTION__, status);
  1172. count = status;
  1173. goto error;
  1174. } else {
  1175. spin_lock_irqsave(&priv->tx_lock, flags);
  1176. priv->tx_outstanding_bytes += count;
  1177. priv->tx_bytes += count;
  1178. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1179. }
  1180. /* we are done with this urb, so let the host driver
  1181. * really free it when it is finished with it */
  1182. usb_free_urb(urb);
  1183. dbg("%s write returning: %d", __FUNCTION__, count);
  1184. return count;
  1185. error:
  1186. usb_free_urb(urb);
  1187. error_no_urb:
  1188. kfree (buffer);
  1189. error_no_buffer:
  1190. spin_lock_irqsave(&priv->tx_lock, flags);
  1191. priv->tx_outstanding_urbs--;
  1192. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1193. return count;
  1194. } /* ftdi_write */
  1195. /* This function may get called when the device is closed */
  1196. static void ftdi_write_bulk_callback (struct urb *urb)
  1197. {
  1198. unsigned long flags;
  1199. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  1200. struct ftdi_private *priv;
  1201. int data_offset; /* will be 1 for the SIO and 0 otherwise */
  1202. unsigned long countback;
  1203. int status = urb->status;
  1204. /* free up the transfer buffer, as usb_free_urb() does not do this */
  1205. kfree (urb->transfer_buffer);
  1206. dbg("%s - port %d", __FUNCTION__, port->number);
  1207. if (status) {
  1208. dbg("nonzero write bulk status received: %d", status);
  1209. return;
  1210. }
  1211. priv = usb_get_serial_port_data(port);
  1212. if (!priv) {
  1213. dbg("%s - bad port private data pointer - exiting", __FUNCTION__);
  1214. return;
  1215. }
  1216. /* account for transferred data */
  1217. countback = urb->actual_length;
  1218. data_offset = priv->write_offset;
  1219. if (data_offset > 0) {
  1220. /* Subtract the control bytes */
  1221. countback -= (data_offset * ((countback + (PKTSZ - 1)) / PKTSZ));
  1222. }
  1223. spin_lock_irqsave(&priv->tx_lock, flags);
  1224. --priv->tx_outstanding_urbs;
  1225. priv->tx_outstanding_bytes -= countback;
  1226. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1227. usb_serial_port_softint(port);
  1228. } /* ftdi_write_bulk_callback */
  1229. static int ftdi_write_room( struct usb_serial_port *port )
  1230. {
  1231. struct ftdi_private *priv = usb_get_serial_port_data(port);
  1232. int room;
  1233. unsigned long flags;
  1234. dbg("%s - port %d", __FUNCTION__, port->number);
  1235. spin_lock_irqsave(&priv->tx_lock, flags);
  1236. if (priv->tx_outstanding_urbs < URB_UPPER_LIMIT) {
  1237. /*
  1238. * We really can take anything the user throws at us
  1239. * but let's pick a nice big number to tell the tty
  1240. * layer that we have lots of free space
  1241. */
  1242. room = 2048;
  1243. } else {
  1244. room = 0;
  1245. }
  1246. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1247. return room;
  1248. } /* ftdi_write_room */
  1249. static int ftdi_chars_in_buffer (struct usb_serial_port *port)
  1250. { /* ftdi_chars_in_buffer */
  1251. struct ftdi_private *priv = usb_get_serial_port_data(port);
  1252. int buffered;
  1253. unsigned long flags;
  1254. dbg("%s - port %d", __FUNCTION__, port->number);
  1255. spin_lock_irqsave(&priv->tx_lock, flags);
  1256. buffered = (int)priv->tx_outstanding_bytes;
  1257. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1258. if (buffered < 0) {
  1259. err("%s outstanding tx bytes is negative!", __FUNCTION__);
  1260. buffered = 0;
  1261. }
  1262. return buffered;
  1263. } /* ftdi_chars_in_buffer */
  1264. static void ftdi_read_bulk_callback (struct urb *urb)
  1265. { /* ftdi_read_bulk_callback */
  1266. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  1267. struct tty_struct *tty;
  1268. struct ftdi_private *priv;
  1269. unsigned long countread;
  1270. unsigned long flags;
  1271. int status = urb->status;
  1272. if (urb->number_of_packets > 0) {
  1273. err("%s transfer_buffer_length %d actual_length %d number of packets %d",__FUNCTION__,
  1274. urb->transfer_buffer_length, urb->actual_length, urb->number_of_packets );
  1275. err("%s transfer_flags %x ", __FUNCTION__,urb->transfer_flags );
  1276. }
  1277. dbg("%s - port %d", __FUNCTION__, port->number);
  1278. if (port->open_count <= 0)
  1279. return;
  1280. tty = port->tty;
  1281. if (!tty) {
  1282. dbg("%s - bad tty pointer - exiting",__FUNCTION__);
  1283. return;
  1284. }
  1285. priv = usb_get_serial_port_data(port);
  1286. if (!priv) {
  1287. dbg("%s - bad port private data pointer - exiting", __FUNCTION__);
  1288. return;
  1289. }
  1290. if (urb != port->read_urb) {
  1291. err("%s - Not my urb!", __FUNCTION__);
  1292. }
  1293. if (status) {
  1294. /* This will happen at close every time so it is a dbg not an err */
  1295. dbg("(this is ok on close) nonzero read bulk status received: "
  1296. "%d", status);
  1297. return;
  1298. }
  1299. /* count data bytes, but not status bytes */
  1300. countread = urb->actual_length;
  1301. countread -= 2 * ((countread + (PKTSZ - 1)) / PKTSZ);
  1302. spin_lock_irqsave(&priv->rx_lock, flags);
  1303. priv->rx_bytes += countread;
  1304. spin_unlock_irqrestore(&priv->rx_lock, flags);
  1305. ftdi_process_read(&priv->rx_work.work);
  1306. } /* ftdi_read_bulk_callback */
  1307. static void ftdi_process_read (struct work_struct *work)
  1308. { /* ftdi_process_read */
  1309. struct ftdi_private *priv =
  1310. container_of(work, struct ftdi_private, rx_work.work);
  1311. struct usb_serial_port *port = priv->port;
  1312. struct urb *urb;
  1313. struct tty_struct *tty;
  1314. char error_flag;
  1315. unsigned char *data;
  1316. int i;
  1317. int result;
  1318. int need_flip;
  1319. int packet_offset;
  1320. unsigned long flags;
  1321. dbg("%s - port %d", __FUNCTION__, port->number);
  1322. if (port->open_count <= 0)
  1323. return;
  1324. tty = port->tty;
  1325. if (!tty) {
  1326. dbg("%s - bad tty pointer - exiting",__FUNCTION__);
  1327. return;
  1328. }
  1329. priv = usb_get_serial_port_data(port);
  1330. if (!priv) {
  1331. dbg("%s - bad port private data pointer - exiting", __FUNCTION__);
  1332. return;
  1333. }
  1334. urb = port->read_urb;
  1335. if (!urb) {
  1336. dbg("%s - bad read_urb pointer - exiting", __FUNCTION__);
  1337. return;
  1338. }
  1339. data = urb->transfer_buffer;
  1340. if (priv->rx_processed) {
  1341. dbg("%s - already processed: %d bytes, %d remain", __FUNCTION__,
  1342. priv->rx_processed,
  1343. urb->actual_length - priv->rx_processed);
  1344. } else {
  1345. /* The first two bytes of every read packet are status */
  1346. if (urb->actual_length > 2) {
  1347. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  1348. } else {
  1349. dbg("Status only: %03oo %03oo",data[0],data[1]);
  1350. }
  1351. }
  1352. /* TO DO -- check for hung up line and handle appropriately: */
  1353. /* send hangup */
  1354. /* See acm.c - you do a tty_hangup - eg tty_hangup(tty) */
  1355. /* if CD is dropped and the line is not CLOCAL then we should hangup */
  1356. need_flip = 0;
  1357. for (packet_offset = priv->rx_processed; packet_offset < urb->actual_length; packet_offset += PKTSZ) {
  1358. int length;
  1359. /* Compare new line status to the old one, signal if different */
  1360. /* N.B. packet may be processed more than once, but differences
  1361. * are only processed once. */
  1362. if (priv != NULL) {
  1363. char new_status = data[packet_offset+0] & FTDI_STATUS_B0_MASK;
  1364. if (new_status != priv->prev_status) {
  1365. priv->diff_status |= new_status ^ priv->prev_status;
  1366. wake_up_interruptible(&priv->delta_msr_wait);
  1367. priv->prev_status = new_status;
  1368. }
  1369. }
  1370. length = min(PKTSZ, urb->actual_length-packet_offset)-2;
  1371. if (length < 0) {
  1372. err("%s - bad packet length: %d", __FUNCTION__, length+2);
  1373. length = 0;
  1374. }
  1375. if (priv->rx_flags & THROTTLED) {
  1376. dbg("%s - throttled", __FUNCTION__);
  1377. break;
  1378. }
  1379. if (tty_buffer_request_room(tty, length) < length) {
  1380. /* break out & wait for throttling/unthrottling to happen */
  1381. dbg("%s - receive room low", __FUNCTION__);
  1382. break;
  1383. }
  1384. /* Handle errors and break */
  1385. error_flag = TTY_NORMAL;
  1386. /* Although the device uses a bitmask and hence can have multiple */
  1387. /* errors on a packet - the order here sets the priority the */
  1388. /* error is returned to the tty layer */
  1389. if ( data[packet_offset+1] & FTDI_RS_OE ) {
  1390. error_flag = TTY_OVERRUN;
  1391. dbg("OVERRRUN error");
  1392. }
  1393. if ( data[packet_offset+1] & FTDI_RS_BI ) {
  1394. error_flag = TTY_BREAK;
  1395. dbg("BREAK received");
  1396. }
  1397. if ( data[packet_offset+1] & FTDI_RS_PE ) {
  1398. error_flag = TTY_PARITY;
  1399. dbg("PARITY error");
  1400. }
  1401. if ( data[packet_offset+1] & FTDI_RS_FE ) {
  1402. error_flag = TTY_FRAME;
  1403. dbg("FRAMING error");
  1404. }
  1405. if (length > 0) {
  1406. for (i = 2; i < length+2; i++) {
  1407. /* Note that the error flag is duplicated for
  1408. every character received since we don't know
  1409. which character it applied to */
  1410. tty_insert_flip_char(tty, data[packet_offset+i], error_flag);
  1411. }
  1412. need_flip = 1;
  1413. }
  1414. #ifdef NOT_CORRECT_BUT_KEEPING_IT_FOR_NOW
  1415. /* if a parity error is detected you get status packets forever
  1416. until a character is sent without a parity error.
  1417. This doesn't work well since the application receives a never
  1418. ending stream of bad data - even though new data hasn't been sent.
  1419. Therefore I (bill) have taken this out.
  1420. However - this might make sense for framing errors and so on
  1421. so I am leaving the code in for now.
  1422. */
  1423. else {
  1424. if (error_flag != TTY_NORMAL){
  1425. dbg("error_flag is not normal");
  1426. /* In this case it is just status - if that is an error send a bad character */
  1427. if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
  1428. tty_flip_buffer_push(tty);
  1429. }
  1430. tty_insert_flip_char(tty, 0xff, error_flag);
  1431. need_flip = 1;
  1432. }
  1433. }
  1434. #endif
  1435. } /* "for(packet_offset=0..." */
  1436. /* Low latency */
  1437. if (need_flip) {
  1438. tty_flip_buffer_push(tty);
  1439. }
  1440. if (packet_offset < urb->actual_length) {
  1441. /* not completely processed - record progress */
  1442. priv->rx_processed = packet_offset;
  1443. dbg("%s - incomplete, %d bytes processed, %d remain",
  1444. __FUNCTION__, packet_offset,
  1445. urb->actual_length - packet_offset);
  1446. /* check if we were throttled while processing */
  1447. spin_lock_irqsave(&priv->rx_lock, flags);
  1448. if (priv->rx_flags & THROTTLED) {
  1449. priv->rx_flags |= ACTUALLY_THROTTLED;
  1450. spin_unlock_irqrestore(&priv->rx_lock, flags);
  1451. dbg("%s - deferring remainder until unthrottled",
  1452. __FUNCTION__);
  1453. return;
  1454. }
  1455. spin_unlock_irqrestore(&priv->rx_lock, flags);
  1456. /* if the port is closed stop trying to read */
  1457. if (port->open_count > 0){
  1458. /* delay processing of remainder */
  1459. schedule_delayed_work(&priv->rx_work, 1);
  1460. } else {
  1461. dbg("%s - port is closed", __FUNCTION__);
  1462. }
  1463. return;
  1464. }
  1465. /* urb is completely processed */
  1466. priv->rx_processed = 0;
  1467. /* if the port is closed stop trying to read */
  1468. if (port->open_count > 0){
  1469. /* Continue trying to always read */
  1470. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  1471. usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress),
  1472. port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
  1473. ftdi_read_bulk_callback, port);
  1474. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1475. if (result)
  1476. err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
  1477. }
  1478. return;
  1479. } /* ftdi_process_read */
  1480. static void ftdi_break_ctl( struct usb_serial_port *port, int break_state )
  1481. {
  1482. struct ftdi_private *priv = usb_get_serial_port_data(port);
  1483. __u16 urb_value = 0;
  1484. char buf[1];
  1485. /* break_state = -1 to turn on break, and 0 to turn off break */
  1486. /* see drivers/char/tty_io.c to see it used */
  1487. /* last_set_data_urb_value NEVER has the break bit set in it */
  1488. if (break_state) {
  1489. urb_value = priv->last_set_data_urb_value | FTDI_SIO_SET_BREAK;
  1490. } else {
  1491. urb_value = priv->last_set_data_urb_value;
  1492. }
  1493. if (usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
  1494. FTDI_SIO_SET_DATA_REQUEST,
  1495. FTDI_SIO_SET_DATA_REQUEST_TYPE,
  1496. urb_value , priv->interface,
  1497. buf, 0, WDR_TIMEOUT) < 0) {
  1498. err("%s FAILED to enable/disable break state (state was %d)", __FUNCTION__,break_state);
  1499. }
  1500. dbg("%s break state is %d - urb is %d", __FUNCTION__,break_state, urb_value);
  1501. }
  1502. /* old_termios contains the original termios settings and tty->termios contains
  1503. * the new setting to be used
  1504. * WARNING: set_termios calls this with old_termios in kernel space
  1505. */
  1506. static void ftdi_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
  1507. { /* ftdi_termios */
  1508. struct usb_device *dev = port->serial->dev;
  1509. struct ftdi_private *priv = usb_get_serial_port_data(port);
  1510. struct ktermios *termios = port->tty->termios;
  1511. unsigned int cflag = termios->c_cflag;
  1512. __u16 urb_value; /* will hold the new flags */
  1513. char buf[1]; /* Perhaps I should dynamically alloc this? */
  1514. // Added for xon/xoff support
  1515. unsigned int iflag = termios->c_iflag;
  1516. unsigned char vstop;
  1517. unsigned char vstart;
  1518. dbg("%s", __FUNCTION__);
  1519. /* Force baud rate if this device requires it, unless it is set to B0. */
  1520. if (priv->force_baud && ((termios->c_cflag & CBAUD) != B0)) {
  1521. dbg("%s: forcing baud rate for this device", __FUNCTION__);
  1522. tty_encode_baud_rate(port->tty, priv->force_baud,
  1523. priv->force_baud);
  1524. }
  1525. /* Force RTS-CTS if this device requires it. */
  1526. if (priv->force_rtscts) {
  1527. dbg("%s: forcing rtscts for this device", __FUNCTION__);
  1528. termios->c_cflag |= CRTSCTS;
  1529. }
  1530. cflag = termios->c_cflag;
  1531. /* FIXME -For this cut I don't care if the line is really changing or
  1532. not - so just do the change regardless - should be able to
  1533. compare old_termios and tty->termios */
  1534. /* NOTE These routines can get interrupted by
  1535. ftdi_sio_read_bulk_callback - need to examine what this
  1536. means - don't see any problems yet */
  1537. /* Set number of data bits, parity, stop bits */
  1538. termios->c_cflag &= ~CMSPAR;
  1539. urb_value = 0;
  1540. urb_value |= (cflag & CSTOPB ? FTDI_SIO_SET_DATA_STOP_BITS_2 :
  1541. FTDI_SIO_SET_DATA_STOP_BITS_1);
  1542. urb_value |= (cflag & PARENB ?
  1543. (cflag & PARODD ? FTDI_SIO_SET_DATA_PARITY_ODD :
  1544. FTDI_SIO_SET_DATA_PARITY_EVEN) :
  1545. FTDI_SIO_SET_DATA_PARITY_NONE);
  1546. if (cflag & CSIZE) {
  1547. switch (cflag & CSIZE) {
  1548. case CS5: urb_value |= 5; dbg("Setting CS5"); break;
  1549. case CS6: urb_value |= 6; dbg("Setting CS6"); break;
  1550. case CS7: urb_value |= 7; dbg("Setting CS7"); break;
  1551. case CS8: urb_value |= 8; dbg("Setting CS8"); break;
  1552. default:
  1553. err("CSIZE was set but not CS5-CS8");
  1554. }
  1555. }
  1556. /* This is needed by the break command since it uses the same command - but is
  1557. * or'ed with this value */
  1558. priv->last_set_data_urb_value = urb_value;
  1559. if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  1560. FTDI_SIO_SET_DATA_REQUEST,
  1561. FTDI_SIO_SET_DATA_REQUEST_TYPE,
  1562. urb_value , priv->interface,
  1563. buf, 0, WDR_SHORT_TIMEOUT) < 0) {
  1564. err("%s FAILED to set databits/stopbits/parity", __FUNCTION__);
  1565. }
  1566. /* Now do the baudrate */
  1567. if ((cflag & CBAUD) == B0 ) {
  1568. /* Disable flow control */
  1569. if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  1570. FTDI_SIO_SET_FLOW_CTRL_REQUEST,
  1571. FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
  1572. 0, priv->interface,
  1573. buf, 0, WDR_TIMEOUT) < 0) {
  1574. err("%s error from disable flowcontrol urb", __FUNCTION__);
  1575. }
  1576. /* Drop RTS and DTR */
  1577. clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
  1578. } else {
  1579. /* set the baudrate determined before */
  1580. if (change_speed(port)) {
  1581. err("%s urb failed to set baudrate", __FUNCTION__);
  1582. }
  1583. /* Ensure RTS and DTR are raised when baudrate changed from 0 */
  1584. if (!old_termios || (old_termios->c_cflag & CBAUD) == B0) {
  1585. set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
  1586. }
  1587. }
  1588. /* Set flow control */
  1589. /* Note device also supports DTR/CD (ugh) and Xon/Xoff in hardware */
  1590. if (cflag & CRTSCTS) {
  1591. dbg("%s Setting to CRTSCTS flow control", __FUNCTION__);
  1592. if (usb_control_msg(dev,
  1593. usb_sndctrlpipe(dev, 0),
  1594. FTDI_SIO_SET_FLOW_CTRL_REQUEST,
  1595. FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
  1596. 0 , (FTDI_SIO_RTS_CTS_HS | priv->interface),
  1597. buf, 0, WDR_TIMEOUT) < 0) {
  1598. err("urb failed to set to rts/cts flow control");
  1599. }
  1600. } else {
  1601. /*
  1602. * Xon/Xoff code
  1603. *
  1604. * Check the IXOFF status in the iflag component of the termios structure
  1605. * if IXOFF is not set, the pre-xon/xoff code is executed.
  1606. */
  1607. if (iflag & IXOFF) {
  1608. dbg("%s request to enable xonxoff iflag=%04x",__FUNCTION__,iflag);
  1609. // Try to enable the XON/XOFF on the ftdi_sio
  1610. // Set the vstart and vstop -- could have been done up above where
  1611. // a lot of other dereferencing is done but that would be very
  1612. // inefficient as vstart and vstop are not always needed
  1613. vstart = termios->c_cc[VSTART];
  1614. vstop = termios->c_cc[VSTOP];
  1615. urb_value=(vstop << 8) | (vstart);
  1616. if (usb_control_msg(dev,
  1617. usb_sndctrlpipe(dev, 0),
  1618. FTDI_SIO_SET_FLOW_CTRL_REQUEST,
  1619. FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
  1620. urb_value , (FTDI_SIO_XON_XOFF_HS
  1621. | priv->interface),
  1622. buf, 0, WDR_TIMEOUT) < 0) {
  1623. err("urb failed to set to xon/xoff flow control");
  1624. }
  1625. } else {
  1626. /* else clause to only run if cfag ! CRTSCTS and iflag ! XOFF */
  1627. /* CHECKME Assuming XON/XOFF handled by tty stack - not by device */
  1628. dbg("%s Turning off hardware flow control", __FUNCTION__);
  1629. if (usb_control_msg(dev,
  1630. usb_sndctrlpipe(dev, 0),
  1631. FTDI_SIO_SET_FLOW_CTRL_REQUEST,
  1632. FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
  1633. 0, priv->interface,
  1634. buf, 0, WDR_TIMEOUT) < 0) {
  1635. err("urb failed to clear flow control");
  1636. }
  1637. }
  1638. }
  1639. return;
  1640. } /* ftdi_termios */
  1641. static int ftdi_tiocmget (struct usb_serial_port *port, struct file *file)
  1642. {
  1643. struct ftdi_private *priv = usb_get_serial_port_data(port);
  1644. unsigned char buf[2];
  1645. int ret;
  1646. dbg("%s TIOCMGET", __FUNCTION__);
  1647. switch (priv->chip_type) {
  1648. case SIO:
  1649. /* Request the status from the device */
  1650. if ((ret = usb_control_msg(port->serial->dev,
  1651. usb_rcvctrlpipe(port->serial->dev, 0),
  1652. FTDI_SIO_GET_MODEM_STATUS_REQUEST,
  1653. FTDI_SIO_GET_MODEM_STATUS_REQUEST_TYPE,
  1654. 0, 0,
  1655. buf, 1, WDR_TIMEOUT)) < 0 ) {
  1656. err("%s Could not get modem status of device - err: %d", __FUNCTION__,
  1657. ret);
  1658. return(ret);
  1659. }
  1660. break;
  1661. case FT8U232AM:
  1662. case FT232BM:
  1663. case FT2232C:
  1664. case FT232RL:
  1665. /* the 8U232AM returns a two byte value (the sio is a 1 byte value) - in the same
  1666. format as the data returned from the in point */
  1667. if ((ret = usb_control_msg(port->serial->dev,
  1668. usb_rcvctrlpipe(port->serial->dev, 0),
  1669. FTDI_SIO_GET_MODEM_STATUS_REQUEST,
  1670. FTDI_SIO_GET_MODEM_STATUS_REQUEST_TYPE,
  1671. 0, priv->interface,
  1672. buf, 2, WDR_TIMEOUT)) < 0 ) {
  1673. err("%s Could not get modem status of device - err: %d", __FUNCTION__,
  1674. ret);
  1675. return(ret);
  1676. }
  1677. break;
  1678. default:
  1679. return -EFAULT;
  1680. break;
  1681. }
  1682. return (buf[0] & FTDI_SIO_DSR_MASK ? TIOCM_DSR : 0) |
  1683. (buf[0] & FTDI_SIO_CTS_MASK ? TIOCM_CTS : 0) |
  1684. (buf[0] & FTDI_SIO_RI_MASK ? TIOCM_RI : 0) |
  1685. (buf[0] & FTDI_SIO_RLSD_MASK ? TIOCM_CD : 0) |
  1686. priv->last_dtr_rts;
  1687. }
  1688. static int ftdi_tiocmset(struct usb_serial_port *port, struct file * file, unsigned int set, unsigned int clear)
  1689. {
  1690. dbg("%s TIOCMSET", __FUNCTION__);
  1691. return update_mctrl(port, set, clear);
  1692. }
  1693. static int ftdi_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
  1694. {
  1695. struct ftdi_private *priv = usb_get_serial_port_data(port);
  1696. dbg("%s cmd 0x%04x", __FUNCTION__, cmd);
  1697. /* Based on code from acm.c and others */
  1698. switch (cmd) {
  1699. case TIOCGSERIAL: /* gets serial port data */
  1700. return get_serial_info(port, (struct serial_struct __user *) arg);
  1701. case TIOCSSERIAL: /* sets serial port data */
  1702. return set_serial_info(port, (struct serial_struct __user *) arg);
  1703. /*
  1704. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1705. * - mask passed in arg for lines of interest
  1706. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1707. * Caller should use TIOCGICOUNT to see which one it was.
  1708. *
  1709. * This code is borrowed from linux/drivers/char/serial.c
  1710. */
  1711. case TIOCMIWAIT:
  1712. while (priv != NULL) {
  1713. interruptible_sleep_on(&priv->delta_msr_wait);
  1714. /* see if a signal did it */
  1715. if (signal_pending(current))
  1716. return -ERESTARTSYS;
  1717. else {
  1718. char diff = priv->diff_status;
  1719. if (diff == 0) {
  1720. return -EIO; /* no change => error */
  1721. }
  1722. /* Consume all events */
  1723. priv->diff_status = 0;
  1724. /* Return 0 if caller wanted to know about these bits */
  1725. if ( ((arg & TIOCM_RNG) && (diff & FTDI_RS0_RI)) ||
  1726. ((arg & TIOCM_DSR) && (diff & FTDI_RS0_DSR)) ||
  1727. ((arg & TIOCM_CD) && (diff & FTDI_RS0_RLSD)) ||
  1728. ((arg & TIOCM_CTS) && (diff & FTDI_RS0_CTS)) ) {
  1729. return 0;
  1730. }
  1731. /*
  1732. * Otherwise caller can't care less about what happened,
  1733. * and so we continue to wait for more events.
  1734. */
  1735. }
  1736. }
  1737. return(0);
  1738. break;
  1739. default:
  1740. break;
  1741. }
  1742. /* This is not necessarily an error - turns out the higher layers will do
  1743. * some ioctls itself (see comment above)
  1744. */
  1745. dbg("%s arg not supported - it was 0x%04x - check /usr/include/asm/ioctls.h", __FUNCTION__, cmd);
  1746. return(-ENOIOCTLCMD);
  1747. } /* ftdi_ioctl */
  1748. static void ftdi_throttle (struct usb_serial_port *port)
  1749. {
  1750. struct ftdi_private *priv = usb_get_serial_port_data(port);
  1751. unsigned long flags;
  1752. dbg("%s - port %d", __FUNCTION__, port->number);
  1753. spin_lock_irqsave(&priv->rx_lock, flags);
  1754. priv->rx_flags |= THROTTLED;
  1755. spin_unlock_irqrestore(&priv->rx_lock, flags);
  1756. }
  1757. static void ftdi_unthrottle (struct usb_serial_port *port)
  1758. {
  1759. struct ftdi_private *priv = usb_get_serial_port_data(port);
  1760. int actually_throttled;
  1761. unsigned long flags;
  1762. dbg("%s - port %d", __FUNCTION__, port->number);
  1763. spin_lock_irqsave(&priv->rx_lock, flags);
  1764. actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
  1765. priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
  1766. spin_unlock_irqrestore(&priv->rx_lock, flags);
  1767. if (actually_throttled)
  1768. schedule_delayed_work(&priv->rx_work, 0);
  1769. }
  1770. static int __init ftdi_init (void)
  1771. {
  1772. int retval;
  1773. dbg("%s", __FUNCTION__);
  1774. if (vendor > 0 && product > 0) {
  1775. /* Add user specified VID/PID to reserved element of table. */
  1776. int i;
  1777. for (i = 0; id_table_combined[i].idVendor; i++)
  1778. ;
  1779. id_table_combined[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  1780. id_table_combined[i].idVendor = vendor;
  1781. id_table_combined[i].idProduct = product;
  1782. }
  1783. retval = usb_serial_register(&ftdi_sio_device);
  1784. if (retval)
  1785. goto failed_sio_register;
  1786. retval = usb_register(&ftdi_driver);
  1787. if (retval)
  1788. goto failed_usb_register;
  1789. info(DRIVER_VERSION ":" DRIVER_DESC);
  1790. return 0;
  1791. failed_usb_register:
  1792. usb_serial_deregister(&ftdi_sio_device);
  1793. failed_sio_register:
  1794. return retval;
  1795. }
  1796. static void __exit ftdi_exit (void)
  1797. {
  1798. dbg("%s", __FUNCTION__);
  1799. usb_deregister (&ftdi_driver);
  1800. usb_serial_deregister (&ftdi_sio_device);
  1801. }
  1802. module_init(ftdi_init);
  1803. module_exit(ftdi_exit);
  1804. MODULE_AUTHOR( DRIVER_AUTHOR );
  1805. MODULE_DESCRIPTION( DRIVER_DESC );
  1806. MODULE_LICENSE("GPL");
  1807. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1808. MODULE_PARM_DESC(debug, "Debug enabled or not");
  1809. module_param(vendor, ushort, 0);
  1810. MODULE_PARM_DESC(vendor, "User specified vendor ID (default="
  1811. __MODULE_STRING(FTDI_VID)")");
  1812. module_param(product, ushort, 0);
  1813. MODULE_PARM_DESC(vendor, "User specified product ID");