cypress_m8.c 49 KB

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