cypress_m8.c 44 KB

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