cypress_m8.c 47 KB

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