ftdi_sio.c 67 KB

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