cypress_m8.c 45 KB

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