cypress_m8.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  1. /*
  2. * USB Cypress M8 driver
  3. *
  4. * Copyright (C) 2004
  5. * Lonnie Mendez (dignome@gmail.com)
  6. * Copyright (C) 2003,2004
  7. * Neil Whelchel (koyama@firstlight.net)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * See Documentation/usb/usb-serial.txt for more information on using this driver
  15. *
  16. * See http://geocities.com/i0xox0i for information on this driver and the
  17. * earthmate usb device.
  18. *
  19. * Lonnie Mendez <dignome@gmail.com>
  20. * 4-29-2005
  21. * Fixed problem where setting or retreiving the serial config would fail with
  22. * EPIPE. Removed CRTS toggling so the driver behaves more like other usbserial
  23. * adapters. Issued new interval of 1ms instead of the default 10ms. As a
  24. * result, transfer speed has been substantially increased. From avg. 850bps to
  25. * avg. 3300bps. initial termios has also been modified. Cleaned up code and
  26. * formatting issues so it is more readable. Replaced the C++ style comments.
  27. *
  28. * Lonnie Mendez <dignome@gmail.com>
  29. * 12-15-2004
  30. * Incorporated write buffering from pl2303 driver. Fixed bug with line
  31. * handling so both lines are raised in cypress_open. (was dropping rts)
  32. * Various code cleanups made as well along with other misc bug fixes.
  33. *
  34. * Lonnie Mendez <dignome@gmail.com>
  35. * 04-10-2004
  36. * Driver modified to support dynamic line settings. Various improvments
  37. * and features.
  38. *
  39. * Neil Whelchel
  40. * 10-2003
  41. * Driver first released.
  42. *
  43. */
  44. /* Thanks to Neil Whelchel for writing the first cypress m8 implementation for linux. */
  45. /* Thanks to cypress for providing references for the hid reports. */
  46. /* Thanks to Jiang Zhang for providing links and for general help. */
  47. /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others. */
  48. #include <linux/kernel.h>
  49. #include <linux/errno.h>
  50. #include <linux/init.h>
  51. #include <linux/slab.h>
  52. #include <linux/tty.h>
  53. #include <linux/tty_driver.h>
  54. #include <linux/tty_flip.h>
  55. #include <linux/module.h>
  56. #include <linux/moduleparam.h>
  57. #include <linux/spinlock.h>
  58. #include <linux/usb.h>
  59. #include <linux/usb/serial.h>
  60. #include <linux/serial.h>
  61. #include <linux/delay.h>
  62. #include <asm/uaccess.h>
  63. #include "cypress_m8.h"
  64. #ifdef CONFIG_USB_SERIAL_DEBUG
  65. static int debug = 1;
  66. #else
  67. static int debug;
  68. #endif
  69. static int stats;
  70. static int interval;
  71. /*
  72. * Version Information
  73. */
  74. #define DRIVER_VERSION "v1.09"
  75. #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
  76. #define DRIVER_DESC "Cypress USB to Serial Driver"
  77. /* write buffer size defines */
  78. #define CYPRESS_BUF_SIZE 1024
  79. #define CYPRESS_CLOSING_WAIT (30*HZ)
  80. static struct usb_device_id id_table_earthmate [] = {
  81. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
  82. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
  83. { } /* Terminating entry */
  84. };
  85. static struct usb_device_id id_table_cyphidcomrs232 [] = {
  86. { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
  87. { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
  88. { } /* Terminating entry */
  89. };
  90. static struct usb_device_id id_table_nokiaca42v2 [] = {
  91. { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
  92. { } /* Terminating entry */
  93. };
  94. static struct usb_device_id id_table_combined [] = {
  95. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
  96. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
  97. { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
  98. { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
  99. { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
  100. { } /* Terminating entry */
  101. };
  102. MODULE_DEVICE_TABLE (usb, id_table_combined);
  103. static struct usb_driver cypress_driver = {
  104. .name = "cypress",
  105. .probe = usb_serial_probe,
  106. .disconnect = usb_serial_disconnect,
  107. .id_table = id_table_combined,
  108. .no_dynamic_id = 1,
  109. };
  110. enum packet_format {
  111. packet_format_1, /* b0:status, b1:payload count */
  112. packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
  113. };
  114. struct cypress_private {
  115. spinlock_t lock; /* private lock */
  116. int chiptype; /* identifier of device, for quirks/etc */
  117. int bytes_in; /* used for statistics */
  118. int bytes_out; /* used for statistics */
  119. int cmd_count; /* used for statistics */
  120. int cmd_ctrl; /* always set this to 1 before issuing a command */
  121. struct cypress_buf *buf; /* write buffer */
  122. int write_urb_in_use; /* write urb in use indicator */
  123. int write_urb_interval; /* interval to use for write urb */
  124. int read_urb_interval; /* interval to use for read urb */
  125. int comm_is_ok; /* true if communication is (still) ok */
  126. int termios_initialized;
  127. __u8 line_control; /* holds dtr / rts value */
  128. __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
  129. __u8 current_config; /* stores the current configuration byte */
  130. __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
  131. enum packet_format pkt_fmt; /* format to use for packet send / receive */
  132. int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
  133. int baud_rate; /* stores current baud rate in integer form */
  134. int cbr_mask; /* stores current baud rate in masked form */
  135. int isthrottled; /* if throttled, discard reads */
  136. wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
  137. char prev_status, diff_status; /* used for TIOCMIWAIT */
  138. /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
  139. struct ktermios tmp_termios; /* stores the old termios settings */
  140. };
  141. /* write buffer structure */
  142. struct cypress_buf {
  143. unsigned int buf_size;
  144. char *buf_buf;
  145. char *buf_get;
  146. char *buf_put;
  147. };
  148. /* function prototypes for the Cypress USB to serial device */
  149. static int cypress_earthmate_startup (struct usb_serial *serial);
  150. static int cypress_hidcom_startup (struct usb_serial *serial);
  151. static int cypress_ca42v2_startup (struct usb_serial *serial);
  152. static void cypress_shutdown (struct usb_serial *serial);
  153. static int cypress_open (struct usb_serial_port *port, struct file *filp);
  154. static void cypress_close (struct usb_serial_port *port, struct file *filp);
  155. static int cypress_write (struct usb_serial_port *port, const unsigned char *buf, int count);
  156. static void cypress_send (struct usb_serial_port *port);
  157. static int cypress_write_room (struct usb_serial_port *port);
  158. static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
  159. static void cypress_set_termios (struct usb_serial_port *port, struct ktermios * old);
  160. static int cypress_tiocmget (struct usb_serial_port *port, struct file *file);
  161. static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
  162. static int cypress_chars_in_buffer (struct usb_serial_port *port);
  163. static void cypress_throttle (struct usb_serial_port *port);
  164. static void cypress_unthrottle (struct usb_serial_port *port);
  165. static void cypress_set_dead (struct usb_serial_port *port);
  166. static void cypress_read_int_callback (struct urb *urb);
  167. static void cypress_write_int_callback (struct urb *urb);
  168. /* baud helper functions */
  169. static int mask_to_rate (unsigned mask);
  170. static unsigned rate_to_mask (int rate);
  171. /* write buffer functions */
  172. static struct cypress_buf *cypress_buf_alloc(unsigned int size);
  173. static void cypress_buf_free(struct cypress_buf *cb);
  174. static void cypress_buf_clear(struct cypress_buf *cb);
  175. static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
  176. static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
  177. static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf, unsigned int count);
  178. static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf, unsigned int count);
  179. static struct usb_serial_driver cypress_earthmate_device = {
  180. .driver = {
  181. .owner = THIS_MODULE,
  182. .name = "earthmate",
  183. },
  184. .description = "DeLorme Earthmate USB",
  185. .usb_driver = &cypress_driver,
  186. .id_table = id_table_earthmate,
  187. .num_interrupt_in = 1,
  188. .num_interrupt_out = 1,
  189. .num_bulk_in = NUM_DONT_CARE,
  190. .num_bulk_out = NUM_DONT_CARE,
  191. .num_ports = 1,
  192. .attach = cypress_earthmate_startup,
  193. .shutdown = cypress_shutdown,
  194. .open = cypress_open,
  195. .close = cypress_close,
  196. .write = cypress_write,
  197. .write_room = cypress_write_room,
  198. .ioctl = cypress_ioctl,
  199. .set_termios = cypress_set_termios,
  200. .tiocmget = cypress_tiocmget,
  201. .tiocmset = cypress_tiocmset,
  202. .chars_in_buffer = cypress_chars_in_buffer,
  203. .throttle = cypress_throttle,
  204. .unthrottle = cypress_unthrottle,
  205. .read_int_callback = cypress_read_int_callback,
  206. .write_int_callback = cypress_write_int_callback,
  207. };
  208. static struct usb_serial_driver cypress_hidcom_device = {
  209. .driver = {
  210. .owner = THIS_MODULE,
  211. .name = "cyphidcom",
  212. },
  213. .description = "HID->COM RS232 Adapter",
  214. .usb_driver = &cypress_driver,
  215. .id_table = id_table_cyphidcomrs232,
  216. .num_interrupt_in = 1,
  217. .num_interrupt_out = 1,
  218. .num_bulk_in = NUM_DONT_CARE,
  219. .num_bulk_out = NUM_DONT_CARE,
  220. .num_ports = 1,
  221. .attach = cypress_hidcom_startup,
  222. .shutdown = cypress_shutdown,
  223. .open = cypress_open,
  224. .close = cypress_close,
  225. .write = cypress_write,
  226. .write_room = cypress_write_room,
  227. .ioctl = cypress_ioctl,
  228. .set_termios = cypress_set_termios,
  229. .tiocmget = cypress_tiocmget,
  230. .tiocmset = cypress_tiocmset,
  231. .chars_in_buffer = cypress_chars_in_buffer,
  232. .throttle = cypress_throttle,
  233. .unthrottle = cypress_unthrottle,
  234. .read_int_callback = cypress_read_int_callback,
  235. .write_int_callback = cypress_write_int_callback,
  236. };
  237. static struct usb_serial_driver cypress_ca42v2_device = {
  238. .driver = {
  239. .owner = THIS_MODULE,
  240. .name = "nokiaca42v2",
  241. },
  242. .description = "Nokia CA-42 V2 Adapter",
  243. .usb_driver = &cypress_driver,
  244. .id_table = id_table_nokiaca42v2,
  245. .num_interrupt_in = 1,
  246. .num_interrupt_out = 1,
  247. .num_bulk_in = NUM_DONT_CARE,
  248. .num_bulk_out = NUM_DONT_CARE,
  249. .num_ports = 1,
  250. .attach = cypress_ca42v2_startup,
  251. .shutdown = cypress_shutdown,
  252. .open = cypress_open,
  253. .close = cypress_close,
  254. .write = cypress_write,
  255. .write_room = cypress_write_room,
  256. .ioctl = cypress_ioctl,
  257. .set_termios = cypress_set_termios,
  258. .tiocmget = cypress_tiocmget,
  259. .tiocmset = cypress_tiocmset,
  260. .chars_in_buffer = cypress_chars_in_buffer,
  261. .throttle = cypress_throttle,
  262. .unthrottle = cypress_unthrottle,
  263. .read_int_callback = cypress_read_int_callback,
  264. .write_int_callback = cypress_write_int_callback,
  265. };
  266. /*****************************************************************************
  267. * Cypress serial helper functions
  268. *****************************************************************************/
  269. /* This function can either set or retrieve the current serial line settings */
  270. static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_mask, int data_bits, int stop_bits,
  271. int parity_enable, int parity_type, int reset, int cypress_request_type)
  272. {
  273. int new_baudrate = 0, retval = 0, tries = 0;
  274. struct cypress_private *priv;
  275. __u8 feature_buffer[5];
  276. unsigned long flags;
  277. dbg("%s", __FUNCTION__);
  278. priv = usb_get_serial_port_data(port);
  279. if (!priv->comm_is_ok)
  280. return -ENODEV;
  281. switch(cypress_request_type) {
  282. case CYPRESS_SET_CONFIG:
  283. /*
  284. * The general purpose firmware for the Cypress M8 allows for a maximum speed
  285. * of 57600bps (I have no idea whether DeLorme chose to use the general purpose
  286. * firmware or not), if you need to modify this speed setting for your own
  287. * project please add your own chiptype and modify the code likewise. The
  288. * Cypress HID->COM device will work successfully up to 115200bps (but the
  289. * actual throughput is around 3kBps).
  290. */
  291. if (baud_mask != priv->cbr_mask) {
  292. dbg("%s - baud rate is changing", __FUNCTION__);
  293. if ( priv->chiptype == CT_EARTHMATE ) {
  294. /* 300 and 600 baud rates are supported under the generic firmware,
  295. * but are not used with NMEA and SiRF protocols */
  296. if ( (baud_mask == B300) || (baud_mask == B600) ) {
  297. err("%s - failed setting baud rate, unsupported speed",
  298. __FUNCTION__);
  299. new_baudrate = priv->baud_rate;
  300. } else if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
  301. err("%s - failed setting baud rate, unsupported speed",
  302. __FUNCTION__);
  303. new_baudrate = priv->baud_rate;
  304. }
  305. } else if (priv->chiptype == CT_CYPHIDCOM) {
  306. if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
  307. err("%s - failed setting baud rate, unsupported speed",
  308. __FUNCTION__);
  309. new_baudrate = priv->baud_rate;
  310. }
  311. } else if (priv->chiptype == CT_CA42V2) {
  312. if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
  313. err("%s - failed setting baud rate, unsupported speed",
  314. __FUNCTION__);
  315. new_baudrate = priv->baud_rate;
  316. }
  317. } else if (priv->chiptype == CT_GENERIC) {
  318. if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
  319. err("%s - failed setting baud rate, unsupported speed",
  320. __FUNCTION__);
  321. new_baudrate = priv->baud_rate;
  322. }
  323. } else {
  324. info("%s - please define your chiptype", __FUNCTION__);
  325. new_baudrate = priv->baud_rate;
  326. }
  327. } else { /* baud rate not changing, keep the old */
  328. new_baudrate = priv->baud_rate;
  329. }
  330. dbg("%s - baud rate is being sent as %d", __FUNCTION__, new_baudrate);
  331. memset(feature_buffer, 0, sizeof(feature_buffer));
  332. /* fill the feature_buffer with new configuration */
  333. *((u_int32_t *)feature_buffer) = new_baudrate;
  334. feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
  335. /* 1 bit gap */
  336. feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
  337. feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
  338. feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
  339. /* 1 bit gap */
  340. feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
  341. dbg("%s - device is being sent this feature report:", __FUNCTION__);
  342. dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1],
  343. feature_buffer[2], feature_buffer[3], feature_buffer[4]);
  344. do {
  345. retval = usb_control_msg(port->serial->dev,
  346. usb_sndctrlpipe(port->serial->dev, 0),
  347. HID_REQ_SET_REPORT,
  348. USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  349. 0x0300, 0, feature_buffer,
  350. sizeof(feature_buffer), 500);
  351. if (tries++ >= 3)
  352. break;
  353. } while (retval != sizeof(feature_buffer) &&
  354. retval != -ENODEV);
  355. if (retval != sizeof(feature_buffer)) {
  356. err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
  357. cypress_set_dead(port);
  358. } else {
  359. spin_lock_irqsave(&priv->lock, flags);
  360. priv->baud_rate = new_baudrate;
  361. priv->cbr_mask = baud_mask;
  362. priv->current_config = feature_buffer[4];
  363. spin_unlock_irqrestore(&priv->lock, flags);
  364. }
  365. break;
  366. case CYPRESS_GET_CONFIG:
  367. if (priv->get_cfg_unsafe) {
  368. /* Not implemented for this device,
  369. and if we try to do it we're likely
  370. to crash the hardware. */
  371. return -ENOTTY;
  372. }
  373. dbg("%s - retreiving serial line settings", __FUNCTION__);
  374. /* set initial values in feature buffer */
  375. memset(feature_buffer, 0, sizeof(feature_buffer));
  376. do {
  377. retval = usb_control_msg(port->serial->dev,
  378. usb_rcvctrlpipe(port->serial->dev, 0),
  379. HID_REQ_GET_REPORT,
  380. USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  381. 0x0300, 0, feature_buffer,
  382. sizeof(feature_buffer), 500);
  383. if (tries++ >= 3)
  384. break;
  385. } while (retval != sizeof(feature_buffer) &&
  386. retval != -ENODEV);
  387. if (retval != sizeof(feature_buffer)) {
  388. err("%s - failed to retrieve serial line settings - %d", __FUNCTION__, retval);
  389. cypress_set_dead(port);
  390. return retval;
  391. } else {
  392. spin_lock_irqsave(&priv->lock, flags);
  393. /* store the config in one byte, and later use bit masks to check values */
  394. priv->current_config = feature_buffer[4];
  395. priv->baud_rate = *((u_int32_t *)feature_buffer);
  396. if ( (priv->cbr_mask = rate_to_mask(priv->baud_rate)) == 0x40)
  397. dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__);
  398. spin_unlock_irqrestore(&priv->lock, flags);
  399. }
  400. }
  401. spin_lock_irqsave(&priv->lock, flags);
  402. ++priv->cmd_count;
  403. spin_unlock_irqrestore(&priv->lock, flags);
  404. return retval;
  405. } /* cypress_serial_control */
  406. static void cypress_set_dead(struct usb_serial_port *port)
  407. {
  408. struct cypress_private *priv = usb_get_serial_port_data(port);
  409. unsigned long flags;
  410. spin_lock_irqsave(&priv->lock, flags);
  411. if (!priv->comm_is_ok) {
  412. spin_unlock_irqrestore(&priv->lock, flags);
  413. return;
  414. }
  415. priv->comm_is_ok = 0;
  416. spin_unlock_irqrestore(&priv->lock, flags);
  417. err("cypress_m8 suspending failing port %d - interval might be too short",
  418. port->number);
  419. }
  420. /* given a baud mask, it will return integer baud on success */
  421. static int mask_to_rate (unsigned mask)
  422. {
  423. int rate;
  424. switch (mask) {
  425. case B0: rate = 0; break;
  426. case B300: rate = 300; break;
  427. case B600: rate = 600; break;
  428. case B1200: rate = 1200; break;
  429. case B2400: rate = 2400; break;
  430. case B4800: rate = 4800; break;
  431. case B9600: rate = 9600; break;
  432. case B19200: rate = 19200; break;
  433. case B38400: rate = 38400; break;
  434. case B57600: rate = 57600; break;
  435. case B115200: rate = 115200; break;
  436. default: rate = -1;
  437. }
  438. return rate;
  439. }
  440. static unsigned rate_to_mask (int rate)
  441. {
  442. unsigned mask;
  443. switch (rate) {
  444. case 0: mask = B0; break;
  445. case 300: mask = B300; break;
  446. case 600: mask = B600; break;
  447. case 1200: mask = B1200; break;
  448. case 2400: mask = B2400; break;
  449. case 4800: mask = B4800; break;
  450. case 9600: mask = B9600; break;
  451. case 19200: mask = B19200; break;
  452. case 38400: mask = B38400; break;
  453. case 57600: mask = B57600; break;
  454. case 115200: mask = B115200; break;
  455. default: mask = 0x40;
  456. }
  457. return mask;
  458. }
  459. /*****************************************************************************
  460. * Cypress serial driver functions
  461. *****************************************************************************/
  462. static int generic_startup (struct usb_serial *serial)
  463. {
  464. struct cypress_private *priv;
  465. struct usb_serial_port *port = serial->port[0];
  466. dbg("%s - port %d", __FUNCTION__, port->number);
  467. priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL);
  468. if (!priv)
  469. return -ENOMEM;
  470. priv->comm_is_ok = !0;
  471. spin_lock_init(&priv->lock);
  472. priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
  473. if (priv->buf == NULL) {
  474. kfree(priv);
  475. return -ENOMEM;
  476. }
  477. init_waitqueue_head(&priv->delta_msr_wait);
  478. usb_reset_configuration (serial->dev);
  479. priv->cmd_ctrl = 0;
  480. priv->line_control = 0;
  481. priv->termios_initialized = 0;
  482. priv->rx_flags = 0;
  483. priv->cbr_mask = B300;
  484. /* Default packet format setting is determined by packet size.
  485. Anything with a size larger then 9 must have a separate
  486. count field since the 3 bit count field is otherwise too
  487. small. Otherwise we can use the slightly more compact
  488. format. This is in accordance with the cypress_m8 serial
  489. converter app note. */
  490. if (port->interrupt_out_size > 9) {
  491. priv->pkt_fmt = packet_format_1;
  492. } else {
  493. priv->pkt_fmt = packet_format_2;
  494. }
  495. if (interval > 0) {
  496. priv->write_urb_interval = interval;
  497. priv->read_urb_interval = interval;
  498. dbg("%s - port %d read & write intervals forced to %d",
  499. __FUNCTION__,port->number,interval);
  500. } else {
  501. priv->write_urb_interval = port->interrupt_out_urb->interval;
  502. priv->read_urb_interval = port->interrupt_in_urb->interval;
  503. dbg("%s - port %d intervals: read=%d write=%d",
  504. __FUNCTION__,port->number,
  505. priv->read_urb_interval,priv->write_urb_interval);
  506. }
  507. usb_set_serial_port_data(port, priv);
  508. return 0;
  509. }
  510. static int cypress_earthmate_startup (struct usb_serial *serial)
  511. {
  512. struct cypress_private *priv;
  513. struct usb_serial_port *port = serial->port[0];
  514. dbg("%s", __FUNCTION__);
  515. if (generic_startup(serial)) {
  516. dbg("%s - Failed setting up port %d", __FUNCTION__,
  517. port->number);
  518. return 1;
  519. }
  520. priv = usb_get_serial_port_data(port);
  521. priv->chiptype = CT_EARTHMATE;
  522. /* All Earthmate devices use the separated-count packet
  523. format! Idiotic. */
  524. priv->pkt_fmt = packet_format_1;
  525. if (serial->dev->descriptor.idProduct != PRODUCT_ID_EARTHMATEUSB) {
  526. /* The old original USB Earthmate seemed able to
  527. handle GET_CONFIG requests; everything they've
  528. produced since that time crashes if this command is
  529. attempted :-( */
  530. dbg("%s - Marking this device as unsafe for GET_CONFIG "
  531. "commands", __func__);
  532. priv->get_cfg_unsafe = !0;
  533. }
  534. return 0;
  535. } /* cypress_earthmate_startup */
  536. static int cypress_hidcom_startup (struct usb_serial *serial)
  537. {
  538. struct cypress_private *priv;
  539. dbg("%s", __FUNCTION__);
  540. if (generic_startup(serial)) {
  541. dbg("%s - Failed setting up port %d", __FUNCTION__,
  542. serial->port[0]->number);
  543. return 1;
  544. }
  545. priv = usb_get_serial_port_data(serial->port[0]);
  546. priv->chiptype = CT_CYPHIDCOM;
  547. return 0;
  548. } /* cypress_hidcom_startup */
  549. static int cypress_ca42v2_startup (struct usb_serial *serial)
  550. {
  551. struct cypress_private *priv;
  552. dbg("%s", __FUNCTION__);
  553. if (generic_startup(serial)) {
  554. dbg("%s - Failed setting up port %d", __FUNCTION__,
  555. serial->port[0]->number);
  556. return 1;
  557. }
  558. priv = usb_get_serial_port_data(serial->port[0]);
  559. priv->chiptype = CT_CA42V2;
  560. return 0;
  561. } /* cypress_ca42v2_startup */
  562. static void cypress_shutdown (struct usb_serial *serial)
  563. {
  564. struct cypress_private *priv;
  565. dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
  566. /* all open ports are closed at this point */
  567. priv = usb_get_serial_port_data(serial->port[0]);
  568. if (priv) {
  569. cypress_buf_free(priv->buf);
  570. kfree(priv);
  571. usb_set_serial_port_data(serial->port[0], NULL);
  572. }
  573. }
  574. static int cypress_open (struct usb_serial_port *port, struct file *filp)
  575. {
  576. struct cypress_private *priv = usb_get_serial_port_data(port);
  577. struct usb_serial *serial = port->serial;
  578. unsigned long flags;
  579. int result = 0;
  580. dbg("%s - port %d", __FUNCTION__, port->number);
  581. if (!priv->comm_is_ok)
  582. return -EIO;
  583. /* clear halts before open */
  584. usb_clear_halt(serial->dev, 0x81);
  585. usb_clear_halt(serial->dev, 0x02);
  586. spin_lock_irqsave(&priv->lock, flags);
  587. /* reset read/write statistics */
  588. priv->bytes_in = 0;
  589. priv->bytes_out = 0;
  590. priv->cmd_count = 0;
  591. priv->rx_flags = 0;
  592. spin_unlock_irqrestore(&priv->lock, flags);
  593. /* setting to zero could cause data loss */
  594. port->tty->low_latency = 1;
  595. /* raise both lines and set termios */
  596. spin_lock_irqsave(&priv->lock, flags);
  597. priv->line_control = CONTROL_DTR | CONTROL_RTS;
  598. priv->cmd_ctrl = 1;
  599. spin_unlock_irqrestore(&priv->lock, flags);
  600. result = cypress_write(port, NULL, 0);
  601. if (result) {
  602. dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
  603. return result;
  604. } else
  605. dbg("%s - success setting the control lines", __FUNCTION__);
  606. cypress_set_termios(port, &priv->tmp_termios);
  607. /* setup the port and start reading from the device */
  608. if(!port->interrupt_in_urb){
  609. err("%s - interrupt_in_urb is empty!", __FUNCTION__);
  610. return(-1);
  611. }
  612. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  613. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  614. port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
  615. cypress_read_int_callback, port, priv->read_urb_interval);
  616. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  617. if (result){
  618. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
  619. cypress_set_dead(port);
  620. }
  621. return result;
  622. } /* cypress_open */
  623. static void cypress_close(struct usb_serial_port *port, struct file * filp)
  624. {
  625. struct cypress_private *priv = usb_get_serial_port_data(port);
  626. unsigned int c_cflag;
  627. int bps;
  628. long timeout;
  629. wait_queue_t wait;
  630. dbg("%s - port %d", __FUNCTION__, port->number);
  631. /* wait for data to drain from buffer */
  632. spin_lock_irq(&priv->lock);
  633. timeout = CYPRESS_CLOSING_WAIT;
  634. init_waitqueue_entry(&wait, current);
  635. add_wait_queue(&port->tty->write_wait, &wait);
  636. for (;;) {
  637. set_current_state(TASK_INTERRUPTIBLE);
  638. if (cypress_buf_data_avail(priv->buf) == 0
  639. || timeout == 0 || signal_pending(current)
  640. /* without mutex, allowed due to harmless failure mode */
  641. || port->serial->disconnected)
  642. break;
  643. spin_unlock_irq(&priv->lock);
  644. timeout = schedule_timeout(timeout);
  645. spin_lock_irq(&priv->lock);
  646. }
  647. set_current_state(TASK_RUNNING);
  648. remove_wait_queue(&port->tty->write_wait, &wait);
  649. /* clear out any remaining data in the buffer */
  650. cypress_buf_clear(priv->buf);
  651. spin_unlock_irq(&priv->lock);
  652. /* writing is potentially harmful, lock must be taken */
  653. mutex_lock(&port->serial->disc_mutex);
  654. if (port->serial->disconnected) {
  655. mutex_unlock(&port->serial->disc_mutex);
  656. return;
  657. }
  658. /* wait for characters to drain from device */
  659. bps = tty_get_baud_rate(port->tty);
  660. if (bps > 1200)
  661. timeout = max((HZ*2560)/bps,HZ/10);
  662. else
  663. timeout = 2*HZ;
  664. schedule_timeout_interruptible(timeout);
  665. dbg("%s - stopping urbs", __FUNCTION__);
  666. usb_kill_urb (port->interrupt_in_urb);
  667. usb_kill_urb (port->interrupt_out_urb);
  668. if (port->tty) {
  669. c_cflag = port->tty->termios->c_cflag;
  670. if (c_cflag & HUPCL) {
  671. /* drop dtr and rts */
  672. priv = usb_get_serial_port_data(port);
  673. spin_lock_irq(&priv->lock);
  674. priv->line_control = 0;
  675. priv->cmd_ctrl = 1;
  676. spin_unlock_irq(&priv->lock);
  677. cypress_write(port, NULL, 0);
  678. }
  679. }
  680. if (stats)
  681. dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
  682. priv->bytes_in, priv->bytes_out, priv->cmd_count);
  683. mutex_unlock(&port->serial->disc_mutex);
  684. } /* cypress_close */
  685. static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
  686. {
  687. struct cypress_private *priv = usb_get_serial_port_data(port);
  688. unsigned long flags;
  689. dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
  690. /* line control commands, which need to be executed immediately,
  691. are not put into the buffer for obvious reasons.
  692. */
  693. if (priv->cmd_ctrl) {
  694. count = 0;
  695. goto finish;
  696. }
  697. if (!count)
  698. return count;
  699. spin_lock_irqsave(&priv->lock, flags);
  700. count = cypress_buf_put(priv->buf, buf, count);
  701. spin_unlock_irqrestore(&priv->lock, flags);
  702. finish:
  703. cypress_send(port);
  704. return count;
  705. } /* cypress_write */
  706. static void cypress_send(struct usb_serial_port *port)
  707. {
  708. int count = 0, result, offset, actual_size;
  709. struct cypress_private *priv = usb_get_serial_port_data(port);
  710. unsigned long flags;
  711. if (!priv->comm_is_ok)
  712. return;
  713. dbg("%s - port %d", __FUNCTION__, port->number);
  714. dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
  715. spin_lock_irqsave(&priv->lock, flags);
  716. if (priv->write_urb_in_use) {
  717. dbg("%s - can't write, urb in use", __FUNCTION__);
  718. spin_unlock_irqrestore(&priv->lock, flags);
  719. return;
  720. }
  721. spin_unlock_irqrestore(&priv->lock, flags);
  722. /* clear buffer */
  723. memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
  724. spin_lock_irqsave(&priv->lock, flags);
  725. switch (priv->pkt_fmt) {
  726. default:
  727. case packet_format_1:
  728. /* this is for the CY7C64013... */
  729. offset = 2;
  730. port->interrupt_out_buffer[0] = priv->line_control;
  731. break;
  732. case packet_format_2:
  733. /* this is for the CY7C63743... */
  734. offset = 1;
  735. port->interrupt_out_buffer[0] = priv->line_control;
  736. break;
  737. }
  738. if (priv->line_control & CONTROL_RESET)
  739. priv->line_control &= ~CONTROL_RESET;
  740. if (priv->cmd_ctrl) {
  741. priv->cmd_count++;
  742. dbg("%s - line control command being issued", __FUNCTION__);
  743. spin_unlock_irqrestore(&priv->lock, flags);
  744. goto send;
  745. } else
  746. spin_unlock_irqrestore(&priv->lock, flags);
  747. count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
  748. port->interrupt_out_size-offset);
  749. if (count == 0) {
  750. return;
  751. }
  752. switch (priv->pkt_fmt) {
  753. default:
  754. case packet_format_1:
  755. port->interrupt_out_buffer[1] = count;
  756. break;
  757. case packet_format_2:
  758. port->interrupt_out_buffer[0] |= count;
  759. }
  760. dbg("%s - count is %d", __FUNCTION__, count);
  761. send:
  762. spin_lock_irqsave(&priv->lock, flags);
  763. priv->write_urb_in_use = 1;
  764. spin_unlock_irqrestore(&priv->lock, flags);
  765. if (priv->cmd_ctrl)
  766. actual_size = 1;
  767. else
  768. actual_size = count +
  769. (priv->pkt_fmt == packet_format_1 ? 2 : 1);
  770. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
  771. port->interrupt_out_urb->transfer_buffer);
  772. usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
  773. usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
  774. port->interrupt_out_buffer, port->interrupt_out_size,
  775. cypress_write_int_callback, port, priv->write_urb_interval);
  776. result = usb_submit_urb (port->interrupt_out_urb, GFP_ATOMIC);
  777. if (result) {
  778. dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__,
  779. result);
  780. priv->write_urb_in_use = 0;
  781. cypress_set_dead(port);
  782. }
  783. spin_lock_irqsave(&priv->lock, flags);
  784. if (priv->cmd_ctrl) {
  785. priv->cmd_ctrl = 0;
  786. }
  787. priv->bytes_out += count; /* do not count the line control and size bytes */
  788. spin_unlock_irqrestore(&priv->lock, flags);
  789. usb_serial_port_softint(port);
  790. } /* cypress_send */
  791. /* returns how much space is available in the soft buffer */
  792. static int cypress_write_room(struct usb_serial_port *port)
  793. {
  794. struct cypress_private *priv = usb_get_serial_port_data(port);
  795. int room = 0;
  796. unsigned long flags;
  797. dbg("%s - port %d", __FUNCTION__, port->number);
  798. spin_lock_irqsave(&priv->lock, flags);
  799. room = cypress_buf_space_avail(priv->buf);
  800. spin_unlock_irqrestore(&priv->lock, flags);
  801. dbg("%s - returns %d", __FUNCTION__, room);
  802. return room;
  803. }
  804. static int cypress_tiocmget (struct usb_serial_port *port, struct file *file)
  805. {
  806. struct cypress_private *priv = usb_get_serial_port_data(port);
  807. __u8 status, control;
  808. unsigned int result = 0;
  809. unsigned long flags;
  810. dbg("%s - port %d", __FUNCTION__, port->number);
  811. spin_lock_irqsave(&priv->lock, flags);
  812. control = priv->line_control;
  813. status = priv->current_status;
  814. spin_unlock_irqrestore(&priv->lock, flags);
  815. result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
  816. | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
  817. | ((status & UART_CTS) ? TIOCM_CTS : 0)
  818. | ((status & UART_DSR) ? TIOCM_DSR : 0)
  819. | ((status & UART_RI) ? TIOCM_RI : 0)
  820. | ((status & UART_CD) ? TIOCM_CD : 0);
  821. dbg("%s - result = %x", __FUNCTION__, result);
  822. return result;
  823. }
  824. static int cypress_tiocmset (struct usb_serial_port *port, struct file *file,
  825. unsigned int set, unsigned int clear)
  826. {
  827. struct cypress_private *priv = usb_get_serial_port_data(port);
  828. unsigned long flags;
  829. dbg("%s - port %d", __FUNCTION__, port->number);
  830. spin_lock_irqsave(&priv->lock, flags);
  831. if (set & TIOCM_RTS)
  832. priv->line_control |= CONTROL_RTS;
  833. if (set & TIOCM_DTR)
  834. priv->line_control |= CONTROL_DTR;
  835. if (clear & TIOCM_RTS)
  836. priv->line_control &= ~CONTROL_RTS;
  837. if (clear & TIOCM_DTR)
  838. priv->line_control &= ~CONTROL_DTR;
  839. spin_unlock_irqrestore(&priv->lock, flags);
  840. priv->cmd_ctrl = 1;
  841. return cypress_write(port, NULL, 0);
  842. }
  843. static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
  844. {
  845. struct cypress_private *priv = usb_get_serial_port_data(port);
  846. dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
  847. switch (cmd) {
  848. case TIOCGSERIAL:
  849. if (copy_to_user((void __user *)arg, port->tty->termios, sizeof(struct ktermios))) {
  850. return -EFAULT;
  851. }
  852. return (0);
  853. break;
  854. case TIOCSSERIAL:
  855. if (copy_from_user(port->tty->termios, (void __user *)arg, sizeof(struct ktermios))) {
  856. return -EFAULT;
  857. }
  858. /* here we need to call cypress_set_termios to invoke the new settings */
  859. cypress_set_termios(port, &priv->tmp_termios);
  860. return (0);
  861. break;
  862. /* This code comes from drivers/char/serial.c and ftdi_sio.c */
  863. case TIOCMIWAIT:
  864. while (priv != NULL) {
  865. interruptible_sleep_on(&priv->delta_msr_wait);
  866. /* see if a signal did it */
  867. if (signal_pending(current))
  868. return -ERESTARTSYS;
  869. else {
  870. char diff = priv->diff_status;
  871. if (diff == 0) {
  872. return -EIO; /* no change => error */
  873. }
  874. /* consume all events */
  875. priv->diff_status = 0;
  876. /* return 0 if caller wanted to know about these bits */
  877. if ( ((arg & TIOCM_RNG) && (diff & UART_RI)) ||
  878. ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
  879. ((arg & TIOCM_CD) && (diff & UART_CD)) ||
  880. ((arg & TIOCM_CTS) && (diff & UART_CTS)) ) {
  881. return 0;
  882. }
  883. /* otherwise caller can't care less about what happened,
  884. * and so we continue to wait for more events.
  885. */
  886. }
  887. }
  888. return 0;
  889. break;
  890. default:
  891. break;
  892. }
  893. dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __FUNCTION__, cmd);
  894. return -ENOIOCTLCMD;
  895. } /* cypress_ioctl */
  896. static void cypress_set_termios (struct usb_serial_port *port,
  897. struct ktermios *old_termios)
  898. {
  899. struct cypress_private *priv = usb_get_serial_port_data(port);
  900. struct tty_struct *tty;
  901. int data_bits, stop_bits, parity_type, parity_enable;
  902. unsigned cflag, iflag, baud_mask;
  903. unsigned long flags;
  904. __u8 oldlines;
  905. int linechange = 0;
  906. dbg("%s - port %d", __FUNCTION__, port->number);
  907. tty = port->tty;
  908. if ((!tty) || (!tty->termios)) {
  909. dbg("%s - no tty structures", __FUNCTION__);
  910. return;
  911. }
  912. spin_lock_irqsave(&priv->lock, flags);
  913. if (!priv->termios_initialized) {
  914. if (priv->chiptype == CT_EARTHMATE) {
  915. *(tty->termios) = tty_std_termios;
  916. tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
  917. CLOCAL;
  918. } else if (priv->chiptype == CT_CYPHIDCOM) {
  919. *(tty->termios) = tty_std_termios;
  920. tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
  921. CLOCAL;
  922. } else if (priv->chiptype == CT_CA42V2) {
  923. *(tty->termios) = tty_std_termios;
  924. tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
  925. CLOCAL;
  926. }
  927. priv->termios_initialized = 1;
  928. }
  929. spin_unlock_irqrestore(&priv->lock, flags);
  930. cflag = tty->termios->c_cflag;
  931. iflag = tty->termios->c_iflag;
  932. /* check if there are new settings */
  933. if (old_termios) {
  934. if ((cflag != old_termios->c_cflag) ||
  935. (RELEVANT_IFLAG(iflag) !=
  936. RELEVANT_IFLAG(old_termios->c_iflag))) {
  937. dbg("%s - attempting to set new termios settings",
  938. __FUNCTION__);
  939. /* should make a copy of this in case something goes
  940. * wrong in the function, we can restore it */
  941. spin_lock_irqsave(&priv->lock, flags);
  942. priv->tmp_termios = *(tty->termios);
  943. spin_unlock_irqrestore(&priv->lock, flags);
  944. } else {
  945. dbg("%s - nothing to do, exiting", __FUNCTION__);
  946. return;
  947. }
  948. } else
  949. return;
  950. /* set number of data bits, parity, stop bits */
  951. /* when parity is disabled the parity type bit is ignored */
  952. /* 1 means 2 stop bits, 0 means 1 stop bit */
  953. stop_bits = cflag & CSTOPB ? 1 : 0;
  954. if (cflag & PARENB) {
  955. parity_enable = 1;
  956. /* 1 means odd parity, 0 means even parity */
  957. parity_type = cflag & PARODD ? 1 : 0;
  958. } else
  959. parity_enable = parity_type = 0;
  960. if (cflag & CSIZE) {
  961. switch (cflag & CSIZE) {
  962. case CS5:
  963. data_bits = 0;
  964. break;
  965. case CS6:
  966. data_bits = 1;
  967. break;
  968. case CS7:
  969. data_bits = 2;
  970. break;
  971. case CS8:
  972. data_bits = 3;
  973. break;
  974. default:
  975. err("%s - CSIZE was set, but not CS5-CS8",
  976. __FUNCTION__);
  977. data_bits = 3;
  978. }
  979. } else
  980. data_bits = 3;
  981. spin_lock_irqsave(&priv->lock, flags);
  982. oldlines = priv->line_control;
  983. if ((cflag & CBAUD) == B0) {
  984. /* drop dtr and rts */
  985. dbg("%s - dropping the lines, baud rate 0bps", __FUNCTION__);
  986. baud_mask = B0;
  987. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  988. } else {
  989. baud_mask = (cflag & CBAUD);
  990. switch(baud_mask) {
  991. case B300:
  992. dbg("%s - setting baud 300bps", __FUNCTION__);
  993. break;
  994. case B600:
  995. dbg("%s - setting baud 600bps", __FUNCTION__);
  996. break;
  997. case B1200:
  998. dbg("%s - setting baud 1200bps", __FUNCTION__);
  999. break;
  1000. case B2400:
  1001. dbg("%s - setting baud 2400bps", __FUNCTION__);
  1002. break;
  1003. case B4800:
  1004. dbg("%s - setting baud 4800bps", __FUNCTION__);
  1005. break;
  1006. case B9600:
  1007. dbg("%s - setting baud 9600bps", __FUNCTION__);
  1008. break;
  1009. case B19200:
  1010. dbg("%s - setting baud 19200bps", __FUNCTION__);
  1011. break;
  1012. case B38400:
  1013. dbg("%s - setting baud 38400bps", __FUNCTION__);
  1014. break;
  1015. case B57600:
  1016. dbg("%s - setting baud 57600bps", __FUNCTION__);
  1017. break;
  1018. case B115200:
  1019. dbg("%s - setting baud 115200bps", __FUNCTION__);
  1020. break;
  1021. default:
  1022. dbg("%s - unknown masked baud rate", __FUNCTION__);
  1023. }
  1024. priv->line_control = (CONTROL_DTR | CONTROL_RTS);
  1025. }
  1026. spin_unlock_irqrestore(&priv->lock, flags);
  1027. dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
  1028. "%d data_bits (+5)", __FUNCTION__, stop_bits,
  1029. parity_enable, parity_type, data_bits);
  1030. cypress_serial_control(port, baud_mask, data_bits, stop_bits,
  1031. parity_enable, parity_type, 0, CYPRESS_SET_CONFIG);
  1032. /* we perform a CYPRESS_GET_CONFIG so that the current settings are
  1033. * filled into the private structure this should confirm that all is
  1034. * working if it returns what we just set */
  1035. cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
  1036. /* Here we can define custom tty settings for devices; the main tty
  1037. * termios flag base comes from empeg.c */
  1038. spin_lock_irqsave(&priv->lock, flags);
  1039. if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
  1040. dbg("Using custom termios settings for a baud rate of "
  1041. "4800bps.");
  1042. /* define custom termios settings for NMEA protocol */
  1043. tty->termios->c_iflag /* input modes - */
  1044. &= ~(IGNBRK /* disable ignore break */
  1045. | BRKINT /* disable break causes interrupt */
  1046. | PARMRK /* disable mark parity errors */
  1047. | ISTRIP /* disable clear high bit of input char */
  1048. | INLCR /* disable translate NL to CR */
  1049. | IGNCR /* disable ignore CR */
  1050. | ICRNL /* disable translate CR to NL */
  1051. | IXON); /* disable enable XON/XOFF flow control */
  1052. tty->termios->c_oflag /* output modes */
  1053. &= ~OPOST; /* disable postprocess output char */
  1054. tty->termios->c_lflag /* line discipline modes */
  1055. &= ~(ECHO /* disable echo input characters */
  1056. | ECHONL /* disable echo new line */
  1057. | ICANON /* disable erase, kill, werase, and rprnt
  1058. special characters */
  1059. | ISIG /* disable interrupt, quit, and suspend
  1060. special characters */
  1061. | IEXTEN); /* disable non-POSIX special characters */
  1062. } /* CT_CYPHIDCOM: Application should handle this for device */
  1063. linechange = (priv->line_control != oldlines);
  1064. spin_unlock_irqrestore(&priv->lock, flags);
  1065. /* if necessary, set lines */
  1066. if (linechange) {
  1067. priv->cmd_ctrl = 1;
  1068. cypress_write(port, NULL, 0);
  1069. }
  1070. } /* cypress_set_termios */
  1071. /* returns amount of data still left in soft buffer */
  1072. static int cypress_chars_in_buffer(struct usb_serial_port *port)
  1073. {
  1074. struct cypress_private *priv = usb_get_serial_port_data(port);
  1075. int chars = 0;
  1076. unsigned long flags;
  1077. dbg("%s - port %d", __FUNCTION__, port->number);
  1078. spin_lock_irqsave(&priv->lock, flags);
  1079. chars = cypress_buf_data_avail(priv->buf);
  1080. spin_unlock_irqrestore(&priv->lock, flags);
  1081. dbg("%s - returns %d", __FUNCTION__, chars);
  1082. return chars;
  1083. }
  1084. static void cypress_throttle (struct usb_serial_port *port)
  1085. {
  1086. struct cypress_private *priv = usb_get_serial_port_data(port);
  1087. unsigned long flags;
  1088. dbg("%s - port %d", __FUNCTION__, port->number);
  1089. spin_lock_irqsave(&priv->lock, flags);
  1090. priv->rx_flags = THROTTLED;
  1091. spin_unlock_irqrestore(&priv->lock, flags);
  1092. }
  1093. static void cypress_unthrottle (struct usb_serial_port *port)
  1094. {
  1095. struct cypress_private *priv = usb_get_serial_port_data(port);
  1096. int actually_throttled, result;
  1097. unsigned long flags;
  1098. dbg("%s - port %d", __FUNCTION__, port->number);
  1099. spin_lock_irqsave(&priv->lock, flags);
  1100. actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
  1101. priv->rx_flags = 0;
  1102. spin_unlock_irqrestore(&priv->lock, flags);
  1103. if (!priv->comm_is_ok)
  1104. return;
  1105. if (actually_throttled) {
  1106. port->interrupt_in_urb->dev = port->serial->dev;
  1107. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  1108. if (result) {
  1109. dev_err(&port->dev, "%s - failed submitting read urb, "
  1110. "error %d\n", __FUNCTION__, result);
  1111. cypress_set_dead(port);
  1112. }
  1113. }
  1114. }
  1115. static void cypress_read_int_callback(struct urb *urb)
  1116. {
  1117. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  1118. struct cypress_private *priv = usb_get_serial_port_data(port);
  1119. struct tty_struct *tty;
  1120. unsigned char *data = urb->transfer_buffer;
  1121. unsigned long flags;
  1122. char tty_flag = TTY_NORMAL;
  1123. int havedata = 0;
  1124. int bytes = 0;
  1125. int result;
  1126. int i = 0;
  1127. int status = urb->status;
  1128. dbg("%s - port %d", __FUNCTION__, port->number);
  1129. switch (status) {
  1130. case 0: /* success */
  1131. break;
  1132. case -ECONNRESET:
  1133. case -ENOENT:
  1134. case -ESHUTDOWN:
  1135. /* precursor to disconnect so just go away */
  1136. return;
  1137. case -EPIPE:
  1138. usb_clear_halt(port->serial->dev,0x81);
  1139. break;
  1140. default:
  1141. /* something ugly is going on... */
  1142. dev_err(&urb->dev->dev,"%s - unexpected nonzero read status received: %d\n",
  1143. __FUNCTION__, status);
  1144. cypress_set_dead(port);
  1145. return;
  1146. }
  1147. spin_lock_irqsave(&priv->lock, flags);
  1148. if (priv->rx_flags & THROTTLED) {
  1149. dbg("%s - now throttling", __FUNCTION__);
  1150. priv->rx_flags |= ACTUALLY_THROTTLED;
  1151. spin_unlock_irqrestore(&priv->lock, flags);
  1152. return;
  1153. }
  1154. spin_unlock_irqrestore(&priv->lock, flags);
  1155. tty = port->tty;
  1156. if (!tty) {
  1157. dbg("%s - bad tty pointer - exiting", __FUNCTION__);
  1158. return;
  1159. }
  1160. spin_lock_irqsave(&priv->lock, flags);
  1161. result = urb->actual_length;
  1162. switch (priv->pkt_fmt) {
  1163. default:
  1164. case packet_format_1:
  1165. /* This is for the CY7C64013... */
  1166. priv->current_status = data[0] & 0xF8;
  1167. bytes = data[1] + 2;
  1168. i = 2;
  1169. if (bytes > 2)
  1170. havedata = 1;
  1171. break;
  1172. case packet_format_2:
  1173. /* This is for the CY7C63743... */
  1174. priv->current_status = data[0] & 0xF8;
  1175. bytes = (data[0] & 0x07) + 1;
  1176. i = 1;
  1177. if (bytes > 1)
  1178. havedata = 1;
  1179. break;
  1180. }
  1181. spin_unlock_irqrestore(&priv->lock, flags);
  1182. if (result < bytes) {
  1183. dbg("%s - wrong packet size - received %d bytes but packet "
  1184. "said %d bytes", __func__, result, bytes);
  1185. goto continue_read;
  1186. }
  1187. usb_serial_debug_data (debug, &port->dev, __FUNCTION__,
  1188. urb->actual_length, data);
  1189. spin_lock_irqsave(&priv->lock, flags);
  1190. /* check to see if status has changed */
  1191. if (priv != NULL) {
  1192. if (priv->current_status != priv->prev_status) {
  1193. priv->diff_status |= priv->current_status ^
  1194. priv->prev_status;
  1195. wake_up_interruptible(&priv->delta_msr_wait);
  1196. priv->prev_status = priv->current_status;
  1197. }
  1198. }
  1199. spin_unlock_irqrestore(&priv->lock, flags);
  1200. /* hangup, as defined in acm.c... this might be a bad place for it
  1201. * though */
  1202. if (tty && !(tty->termios->c_cflag & CLOCAL) &&
  1203. !(priv->current_status & UART_CD)) {
  1204. dbg("%s - calling hangup", __FUNCTION__);
  1205. tty_hangup(tty);
  1206. goto continue_read;
  1207. }
  1208. /* There is one error bit... I'm assuming it is a parity error
  1209. * indicator as the generic firmware will set this bit to 1 if a
  1210. * parity error occurs.
  1211. * I can not find reference to any other error events. */
  1212. spin_lock_irqsave(&priv->lock, flags);
  1213. if (priv->current_status & CYP_ERROR) {
  1214. spin_unlock_irqrestore(&priv->lock, flags);
  1215. tty_flag = TTY_PARITY;
  1216. dbg("%s - Parity Error detected", __FUNCTION__);
  1217. } else
  1218. spin_unlock_irqrestore(&priv->lock, flags);
  1219. /* process read if there is data other than line status */
  1220. if (tty && (bytes > i)) {
  1221. bytes = tty_buffer_request_room(tty, bytes);
  1222. for (; i < bytes ; ++i) {
  1223. dbg("pushing byte number %d - %d - %c", i, data[i],
  1224. data[i]);
  1225. tty_insert_flip_char(tty, data[i], tty_flag);
  1226. }
  1227. tty_flip_buffer_push(port->tty);
  1228. }
  1229. spin_lock_irqsave(&priv->lock, flags);
  1230. /* control and status byte(s) are also counted */
  1231. priv->bytes_in += bytes;
  1232. spin_unlock_irqrestore(&priv->lock, flags);
  1233. continue_read:
  1234. /* Continue trying to always read... unless the port has closed. */
  1235. if (port->open_count > 0 && priv->comm_is_ok) {
  1236. usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
  1237. usb_rcvintpipe(port->serial->dev,
  1238. port->interrupt_in_endpointAddress),
  1239. port->interrupt_in_urb->transfer_buffer,
  1240. port->interrupt_in_urb->transfer_buffer_length,
  1241. cypress_read_int_callback, port, priv->read_urb_interval);
  1242. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  1243. if (result) {
  1244. dev_err(&urb->dev->dev, "%s - failed resubmitting "
  1245. "read urb, error %d\n", __FUNCTION__,
  1246. result);
  1247. cypress_set_dead(port);
  1248. }
  1249. }
  1250. return;
  1251. } /* cypress_read_int_callback */
  1252. static void cypress_write_int_callback(struct urb *urb)
  1253. {
  1254. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  1255. struct cypress_private *priv = usb_get_serial_port_data(port);
  1256. int result;
  1257. int status = urb->status;
  1258. dbg("%s - port %d", __FUNCTION__, port->number);
  1259. switch (status) {
  1260. case 0:
  1261. /* success */
  1262. break;
  1263. case -ECONNRESET:
  1264. case -ENOENT:
  1265. case -ESHUTDOWN:
  1266. /* this urb is terminated, clean up */
  1267. dbg("%s - urb shutting down with status: %d",
  1268. __FUNCTION__, status);
  1269. priv->write_urb_in_use = 0;
  1270. return;
  1271. case -EPIPE: /* no break needed; clear halt and resubmit */
  1272. if (!priv->comm_is_ok)
  1273. break;
  1274. usb_clear_halt(port->serial->dev, 0x02);
  1275. /* error in the urb, so we have to resubmit it */
  1276. dbg("%s - nonzero write bulk status received: %d",
  1277. __FUNCTION__, status);
  1278. port->interrupt_out_urb->transfer_buffer_length = 1;
  1279. port->interrupt_out_urb->dev = port->serial->dev;
  1280. result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
  1281. if (!result)
  1282. return;
  1283. dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
  1284. __FUNCTION__, result);
  1285. cypress_set_dead(port);
  1286. break;
  1287. default:
  1288. dev_err(&urb->dev->dev,"%s - unexpected nonzero write status received: %d\n",
  1289. __FUNCTION__, status);
  1290. cypress_set_dead(port);
  1291. break;
  1292. }
  1293. priv->write_urb_in_use = 0;
  1294. /* send any buffered data */
  1295. cypress_send(port);
  1296. }
  1297. /*****************************************************************************
  1298. * Write buffer functions - buffering code from pl2303 used
  1299. *****************************************************************************/
  1300. /*
  1301. * cypress_buf_alloc
  1302. *
  1303. * Allocate a circular buffer and all associated memory.
  1304. */
  1305. static struct cypress_buf *cypress_buf_alloc(unsigned int size)
  1306. {
  1307. struct cypress_buf *cb;
  1308. if (size == 0)
  1309. return NULL;
  1310. cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
  1311. if (cb == NULL)
  1312. return NULL;
  1313. cb->buf_buf = kmalloc(size, GFP_KERNEL);
  1314. if (cb->buf_buf == NULL) {
  1315. kfree(cb);
  1316. return NULL;
  1317. }
  1318. cb->buf_size = size;
  1319. cb->buf_get = cb->buf_put = cb->buf_buf;
  1320. return cb;
  1321. }
  1322. /*
  1323. * cypress_buf_free
  1324. *
  1325. * Free the buffer and all associated memory.
  1326. */
  1327. static void cypress_buf_free(struct cypress_buf *cb)
  1328. {
  1329. if (cb) {
  1330. kfree(cb->buf_buf);
  1331. kfree(cb);
  1332. }
  1333. }
  1334. /*
  1335. * cypress_buf_clear
  1336. *
  1337. * Clear out all data in the circular buffer.
  1338. */
  1339. static void cypress_buf_clear(struct cypress_buf *cb)
  1340. {
  1341. if (cb != NULL)
  1342. cb->buf_get = cb->buf_put;
  1343. /* equivalent to a get of all data available */
  1344. }
  1345. /*
  1346. * cypress_buf_data_avail
  1347. *
  1348. * Return the number of bytes of data available in the circular
  1349. * buffer.
  1350. */
  1351. static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
  1352. {
  1353. if (cb != NULL)
  1354. return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
  1355. else
  1356. return 0;
  1357. }
  1358. /*
  1359. * cypress_buf_space_avail
  1360. *
  1361. * Return the number of bytes of space available in the circular
  1362. * buffer.
  1363. */
  1364. static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
  1365. {
  1366. if (cb != NULL)
  1367. return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
  1368. else
  1369. return 0;
  1370. }
  1371. /*
  1372. * cypress_buf_put
  1373. *
  1374. * Copy data data from a user buffer and put it into the circular buffer.
  1375. * Restrict to the amount of space available.
  1376. *
  1377. * Return the number of bytes copied.
  1378. */
  1379. static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
  1380. unsigned int count)
  1381. {
  1382. unsigned int len;
  1383. if (cb == NULL)
  1384. return 0;
  1385. len = cypress_buf_space_avail(cb);
  1386. if (count > len)
  1387. count = len;
  1388. if (count == 0)
  1389. return 0;
  1390. len = cb->buf_buf + cb->buf_size - cb->buf_put;
  1391. if (count > len) {
  1392. memcpy(cb->buf_put, buf, len);
  1393. memcpy(cb->buf_buf, buf+len, count - len);
  1394. cb->buf_put = cb->buf_buf + count - len;
  1395. } else {
  1396. memcpy(cb->buf_put, buf, count);
  1397. if (count < len)
  1398. cb->buf_put += count;
  1399. else /* count == len */
  1400. cb->buf_put = cb->buf_buf;
  1401. }
  1402. return count;
  1403. }
  1404. /*
  1405. * cypress_buf_get
  1406. *
  1407. * Get data from the circular buffer and copy to the given buffer.
  1408. * Restrict to the amount of data available.
  1409. *
  1410. * Return the number of bytes copied.
  1411. */
  1412. static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
  1413. unsigned int count)
  1414. {
  1415. unsigned int len;
  1416. if (cb == NULL)
  1417. return 0;
  1418. len = cypress_buf_data_avail(cb);
  1419. if (count > len)
  1420. count = len;
  1421. if (count == 0)
  1422. return 0;
  1423. len = cb->buf_buf + cb->buf_size - cb->buf_get;
  1424. if (count > len) {
  1425. memcpy(buf, cb->buf_get, len);
  1426. memcpy(buf+len, cb->buf_buf, count - len);
  1427. cb->buf_get = cb->buf_buf + count - len;
  1428. } else {
  1429. memcpy(buf, cb->buf_get, count);
  1430. if (count < len)
  1431. cb->buf_get += count;
  1432. else /* count == len */
  1433. cb->buf_get = cb->buf_buf;
  1434. }
  1435. return count;
  1436. }
  1437. /*****************************************************************************
  1438. * Module functions
  1439. *****************************************************************************/
  1440. static int __init cypress_init(void)
  1441. {
  1442. int retval;
  1443. dbg("%s", __FUNCTION__);
  1444. retval = usb_serial_register(&cypress_earthmate_device);
  1445. if (retval)
  1446. goto failed_em_register;
  1447. retval = usb_serial_register(&cypress_hidcom_device);
  1448. if (retval)
  1449. goto failed_hidcom_register;
  1450. retval = usb_serial_register(&cypress_ca42v2_device);
  1451. if (retval)
  1452. goto failed_ca42v2_register;
  1453. retval = usb_register(&cypress_driver);
  1454. if (retval)
  1455. goto failed_usb_register;
  1456. info(DRIVER_DESC " " DRIVER_VERSION);
  1457. return 0;
  1458. failed_usb_register:
  1459. usb_serial_deregister(&cypress_ca42v2_device);
  1460. failed_ca42v2_register:
  1461. usb_serial_deregister(&cypress_hidcom_device);
  1462. failed_hidcom_register:
  1463. usb_serial_deregister(&cypress_earthmate_device);
  1464. failed_em_register:
  1465. return retval;
  1466. }
  1467. static void __exit cypress_exit (void)
  1468. {
  1469. dbg("%s", __FUNCTION__);
  1470. usb_deregister (&cypress_driver);
  1471. usb_serial_deregister (&cypress_earthmate_device);
  1472. usb_serial_deregister (&cypress_hidcom_device);
  1473. usb_serial_deregister (&cypress_ca42v2_device);
  1474. }
  1475. module_init(cypress_init);
  1476. module_exit(cypress_exit);
  1477. MODULE_AUTHOR( DRIVER_AUTHOR );
  1478. MODULE_DESCRIPTION( DRIVER_DESC );
  1479. MODULE_VERSION( DRIVER_VERSION );
  1480. MODULE_LICENSE("GPL");
  1481. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1482. MODULE_PARM_DESC(debug, "Debug enabled or not");
  1483. module_param(stats, bool, S_IRUGO | S_IWUSR);
  1484. MODULE_PARM_DESC(stats, "Enable statistics or not");
  1485. module_param(interval, int, S_IRUGO | S_IWUSR);
  1486. MODULE_PARM_DESC(interval, "Overrides interrupt interval");