cypress_m8.c 47 KB

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