cypress_m8.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  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. *
  20. * Lonnie Mendez <dignome@gmail.com>
  21. * 12-15-2004
  22. * Incorporated write buffering from pl2303 driver. Fixed bug with line
  23. * handling so both lines are raised in cypress_open. (was dropping rts)
  24. * Various code cleanups made as well along with other misc bug fixes.
  25. *
  26. * Lonnie Mendez <dignome@gmail.com>
  27. * 04-10-2004
  28. * Driver modified to support dynamic line settings. Various improvments
  29. * and features.
  30. *
  31. * Neil Whelchel
  32. * 10-2003
  33. * Driver first released.
  34. *
  35. *
  36. * Long Term TODO:
  37. * Improve transfer speeds - both read/write are somewhat slow
  38. * at this point.
  39. * Improve debugging. Show modem line status with debug output and
  40. * implement filtering for certain data as a module parameter.
  41. */
  42. /* Thanks to Neil Whelchel for writing the first cypress m8 implementation for linux. */
  43. /* Thanks to cypress for providing references for the hid reports. */
  44. /* Thanks to Jiang Zhang for providing links and for general help. */
  45. /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others. */
  46. #include <linux/config.h>
  47. #include <linux/kernel.h>
  48. #include <linux/errno.h>
  49. #include <linux/init.h>
  50. #include <linux/slab.h>
  51. #include <linux/tty.h>
  52. #include <linux/tty_driver.h>
  53. #include <linux/tty_flip.h>
  54. #include <linux/module.h>
  55. #include <linux/moduleparam.h>
  56. #include <linux/spinlock.h>
  57. #include <linux/usb.h>
  58. #include <linux/serial.h>
  59. #include <linux/delay.h>
  60. #include <asm/uaccess.h>
  61. #include "usb-serial.h"
  62. #include "cypress_m8.h"
  63. #ifdef CONFIG_USB_SERIAL_DEBUG
  64. static int debug = 1;
  65. #else
  66. static int debug;
  67. #endif
  68. static int stats;
  69. /*
  70. * Version Information
  71. */
  72. #define DRIVER_VERSION "v1.08"
  73. #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
  74. #define DRIVER_DESC "Cypress USB to Serial Driver"
  75. /* write buffer size defines */
  76. #define CYPRESS_BUF_SIZE 1024
  77. #define CYPRESS_CLOSING_WAIT (30*HZ)
  78. static struct usb_device_id id_table_earthmate [] = {
  79. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
  80. { } /* Terminating entry */
  81. };
  82. static struct usb_device_id id_table_cyphidcomrs232 [] = {
  83. { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
  84. { } /* Terminating entry */
  85. };
  86. static struct usb_device_id id_table_combined [] = {
  87. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
  88. { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
  89. { } /* Terminating entry */
  90. };
  91. MODULE_DEVICE_TABLE (usb, id_table_combined);
  92. static struct usb_driver cypress_driver = {
  93. .name = "cypress",
  94. .probe = usb_serial_probe,
  95. .disconnect = usb_serial_disconnect,
  96. .id_table = id_table_combined,
  97. };
  98. struct cypress_private {
  99. spinlock_t lock; /* private lock */
  100. int chiptype; /* identifier of device, for quirks/etc */
  101. int bytes_in; /* used for statistics */
  102. int bytes_out; /* used for statistics */
  103. int cmd_count; /* used for statistics */
  104. int cmd_ctrl; /* always set this to 1 before issuing a command */
  105. struct cypress_buf *buf; /* write buffer */
  106. int write_urb_in_use; /* write urb in use indicator */
  107. int termios_initialized;
  108. __u8 line_control; /* holds dtr / rts value */
  109. __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
  110. __u8 current_config; /* stores the current configuration byte */
  111. __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
  112. int baud_rate; /* stores current baud rate in integer form */
  113. int cbr_mask; /* stores current baud rate in masked form */
  114. int isthrottled; /* if throttled, discard reads */
  115. wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
  116. char prev_status, diff_status; /* used for TIOCMIWAIT */
  117. /* we pass a pointer to this as the arguement sent to cypress_set_termios old_termios */
  118. struct termios tmp_termios; /* stores the old termios settings */
  119. char calledfromopen; /* used when issuing lines on open - fixes rts drop bug */
  120. };
  121. /* write buffer structure */
  122. struct cypress_buf {
  123. unsigned int buf_size;
  124. char *buf_buf;
  125. char *buf_get;
  126. char *buf_put;
  127. };
  128. /* function prototypes for the Cypress USB to serial device */
  129. static int cypress_earthmate_startup (struct usb_serial *serial);
  130. static int cypress_hidcom_startup (struct usb_serial *serial);
  131. static void cypress_shutdown (struct usb_serial *serial);
  132. static int cypress_open (struct usb_serial_port *port, struct file *filp);
  133. static void cypress_close (struct usb_serial_port *port, struct file *filp);
  134. static int cypress_write (struct usb_serial_port *port, const unsigned char *buf, int count);
  135. static void cypress_send (struct usb_serial_port *port);
  136. static int cypress_write_room (struct usb_serial_port *port);
  137. static int cypress_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
  138. static void cypress_set_termios (struct usb_serial_port *port, struct termios * old);
  139. static int cypress_tiocmget (struct usb_serial_port *port, struct file *file);
  140. static int cypress_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
  141. static int cypress_chars_in_buffer (struct usb_serial_port *port);
  142. static void cypress_throttle (struct usb_serial_port *port);
  143. static void cypress_unthrottle (struct usb_serial_port *port);
  144. static void cypress_read_int_callback (struct urb *urb, struct pt_regs *regs);
  145. static void cypress_write_int_callback (struct urb *urb, struct pt_regs *regs);
  146. /* baud helper functions */
  147. static int mask_to_rate (unsigned mask);
  148. static unsigned rate_to_mask (int rate);
  149. /* write buffer functions */
  150. static struct cypress_buf *cypress_buf_alloc(unsigned int size);
  151. static void cypress_buf_free(struct cypress_buf *cb);
  152. static void cypress_buf_clear(struct cypress_buf *cb);
  153. static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
  154. static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
  155. static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
  156. unsigned int count);
  157. static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
  158. 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 retreive 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 i, n_baud_rate = 0, retval = 0;
  219. struct cypress_private *priv;
  220. __u8 feature_buffer[5];
  221. __u8 config;
  222. unsigned long flags;
  223. dbg("%s", __FUNCTION__);
  224. priv = usb_get_serial_port_data(port);
  225. switch(cypress_request_type) {
  226. case CYPRESS_SET_CONFIG:
  227. /*
  228. * The general purpose firmware for the Cypress M8 allows for a maximum speed
  229. * of 57600bps (I have no idea whether DeLorme chose to use the general purpose
  230. * firmware or not), if you need to modify this speed setting for your own
  231. * project please add your own chiptype and modify the code likewise. The
  232. * Cypress HID->COM device will work successfully up to 115200bps.
  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 (default to 4800)",
  241. __FUNCTION__);
  242. n_baud_rate = 4800;
  243. } else if ( (n_baud_rate = mask_to_rate(baud_mask)) == -1) {
  244. err("%s - failed setting baud rate, unsupported speed (default to 4800)",
  245. __FUNCTION__);
  246. n_baud_rate = 4800;
  247. }
  248. } else if (priv->chiptype == CT_CYPHIDCOM) {
  249. if ( (n_baud_rate = mask_to_rate(baud_mask)) == -1) {
  250. err("%s - failed setting baud rate, unsupported speed (default to 4800)",
  251. __FUNCTION__);
  252. n_baud_rate = 4800;
  253. }
  254. } else if (priv->chiptype == CT_GENERIC) {
  255. if ( (n_baud_rate = mask_to_rate(baud_mask)) == -1) {
  256. err("%s - failed setting baud rate, unsupported speed (default to 4800)",
  257. __FUNCTION__);
  258. n_baud_rate = 4800;
  259. }
  260. } else {
  261. info("%s - please define your chiptype, using 4800bps default", __FUNCTION__);
  262. n_baud_rate = 4800;
  263. }
  264. } else { /* baud rate not changing, keep the old */
  265. n_baud_rate = priv->baud_rate;
  266. }
  267. dbg("%s - baud rate is being sent as %d", __FUNCTION__, n_baud_rate);
  268. /*
  269. * This algorithm accredited to Jiang Jay Zhang... thanks for all the help!
  270. */
  271. for (i = 0; i < 4; ++i) {
  272. feature_buffer[i] = ( n_baud_rate >> (i*8) & 0xFF );
  273. }
  274. config = 0; // reset config byte
  275. config |= data_bits; // assign data bits in 2 bit space ( max 3 )
  276. /* 1 bit gap */
  277. config |= (stop_bits << 3); // assign stop bits in 1 bit space
  278. config |= (parity_enable << 4); // assign parity flag in 1 bit space
  279. config |= (parity_type << 5); // assign parity type in 1 bit space
  280. /* 1 bit gap */
  281. config |= (reset << 7); // assign reset at end of byte, 1 bit space
  282. feature_buffer[4] = config;
  283. dbg("%s - device is being sent this feature report:", __FUNCTION__);
  284. dbg("%s - %02X - %02X - %02X - %02X - %02X", __FUNCTION__, feature_buffer[0], feature_buffer[1],
  285. feature_buffer[2], feature_buffer[3], feature_buffer[4]);
  286. retval = usb_control_msg (port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
  287. HID_REQ_SET_REPORT, USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  288. 0x0300, 0, feature_buffer, 5, 500);
  289. if (retval != 5)
  290. err("%s - failed sending serial line settings - %d", __FUNCTION__, retval);
  291. else {
  292. spin_lock_irqsave(&priv->lock, flags);
  293. priv->baud_rate = n_baud_rate;
  294. priv->cbr_mask = baud_mask;
  295. priv->current_config = config;
  296. ++priv->cmd_count;
  297. spin_unlock_irqrestore(&priv->lock, flags);
  298. }
  299. break;
  300. case CYPRESS_GET_CONFIG:
  301. dbg("%s - retreiving serial line settings", __FUNCTION__);
  302. /* reset values in feature buffer */
  303. memset(feature_buffer, 0, 5);
  304. retval = usb_control_msg (port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0),
  305. HID_REQ_GET_REPORT, USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  306. 0x0300, 0, feature_buffer, 5, 500);
  307. if (retval != 5) {
  308. err("%s - failed to retreive serial line settings - %d", __FUNCTION__, retval);
  309. return retval;
  310. } else {
  311. spin_lock_irqsave(&priv->lock, flags);
  312. /* store the config in one byte, and later use bit masks to check values */
  313. priv->current_config = feature_buffer[4];
  314. /* reverse the process above to get the baud_mask value */
  315. n_baud_rate = 0; // reset bits
  316. for (i = 0; i < 4; ++i) {
  317. n_baud_rate |= ( feature_buffer[i] << (i*8) );
  318. }
  319. priv->baud_rate = n_baud_rate;
  320. if ( (priv->cbr_mask = rate_to_mask(n_baud_rate)) == 0x40)
  321. dbg("%s - failed setting the baud mask (not defined)", __FUNCTION__);
  322. ++priv->cmd_count;
  323. spin_unlock_irqrestore(&priv->lock, flags);
  324. }
  325. break;
  326. default:
  327. err("%s - unsupported serial control command issued", __FUNCTION__);
  328. }
  329. return retval;
  330. } /* cypress_serial_control */
  331. /* given a baud mask, it will return speed 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. priv->cmd_ctrl = 0;
  390. priv->line_control = 0;
  391. priv->termios_initialized = 0;
  392. priv->calledfromopen = 0;
  393. priv->rx_flags = 0;
  394. usb_set_serial_port_data(serial->port[0], priv);
  395. return (0);
  396. }
  397. static int cypress_earthmate_startup (struct usb_serial *serial)
  398. {
  399. struct cypress_private *priv;
  400. dbg("%s", __FUNCTION__);
  401. if (generic_startup(serial)) {
  402. dbg("%s - Failed setting up port %d", __FUNCTION__, serial->port[0]->number);
  403. return 1;
  404. }
  405. priv = usb_get_serial_port_data(serial->port[0]);
  406. priv->chiptype = CT_EARTHMATE;
  407. return (0);
  408. } /* cypress_earthmate_startup */
  409. static int cypress_hidcom_startup (struct usb_serial *serial)
  410. {
  411. struct cypress_private *priv;
  412. dbg("%s", __FUNCTION__);
  413. if (generic_startup(serial)) {
  414. dbg("%s - Failed setting up port %d", __FUNCTION__, serial->port[0]->number);
  415. return 1;
  416. }
  417. priv = usb_get_serial_port_data(serial->port[0]);
  418. priv->chiptype = CT_CYPHIDCOM;
  419. return (0);
  420. } /* cypress_hidcom_startup */
  421. static void cypress_shutdown (struct usb_serial *serial)
  422. {
  423. struct cypress_private *priv;
  424. dbg ("%s - port %d", __FUNCTION__, serial->port[0]->number);
  425. /* all open ports are closed at this point */
  426. priv = usb_get_serial_port_data(serial->port[0]);
  427. if (priv) {
  428. cypress_buf_free(priv->buf);
  429. kfree(priv);
  430. usb_set_serial_port_data(serial->port[0], NULL);
  431. }
  432. }
  433. static int cypress_open (struct usb_serial_port *port, struct file *filp)
  434. {
  435. struct cypress_private *priv = usb_get_serial_port_data(port);
  436. struct usb_serial *serial = port->serial;
  437. unsigned long flags;
  438. int result = 0;
  439. dbg("%s - port %d", __FUNCTION__, port->number);
  440. /* clear halts before open */
  441. usb_clear_halt(serial->dev, 0x00);
  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->calledfromopen = 1;
  457. priv->cmd_ctrl = 1;
  458. spin_unlock_irqrestore(&priv->lock, flags);
  459. result = cypress_write(port, NULL, 0);
  460. if (result) {
  461. dev_err(&port->dev, "%s - failed setting the control lines - error %d\n", __FUNCTION__, result);
  462. return result;
  463. } else
  464. dbg("%s - success setting the control lines", __FUNCTION__);
  465. cypress_set_termios(port, &priv->tmp_termios);
  466. /* setup the port and start reading from the device */
  467. if(!port->interrupt_in_urb){
  468. err("%s - interrupt_in_urb is empty!", __FUNCTION__);
  469. return(-1);
  470. }
  471. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  472. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  473. port->interrupt_in_urb->transfer_buffer, port->interrupt_in_urb->transfer_buffer_length,
  474. cypress_read_int_callback, port, port->interrupt_in_urb->interval);
  475. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  476. if (result){
  477. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
  478. }
  479. return result;
  480. } /* cypress_open */
  481. static void cypress_close(struct usb_serial_port *port, struct file * filp)
  482. {
  483. struct cypress_private *priv = usb_get_serial_port_data(port);
  484. unsigned int c_cflag;
  485. unsigned long flags;
  486. int bps;
  487. long timeout;
  488. wait_queue_t wait;
  489. dbg("%s - port %d", __FUNCTION__, port->number);
  490. /* wait for data to drain from buffer */
  491. spin_lock_irqsave(&priv->lock, flags);
  492. timeout = CYPRESS_CLOSING_WAIT;
  493. init_waitqueue_entry(&wait, current);
  494. add_wait_queue(&port->tty->write_wait, &wait);
  495. for (;;) {
  496. set_current_state(TASK_INTERRUPTIBLE);
  497. if (cypress_buf_data_avail(priv->buf) == 0
  498. || timeout == 0 || signal_pending(current)
  499. || !usb_get_intfdata(port->serial->interface))
  500. break;
  501. spin_unlock_irqrestore(&priv->lock, flags);
  502. timeout = schedule_timeout(timeout);
  503. spin_lock_irqsave(&priv->lock, flags);
  504. }
  505. set_current_state(TASK_RUNNING);
  506. remove_wait_queue(&port->tty->write_wait, &wait);
  507. /* clear out any remaining data in the buffer */
  508. cypress_buf_clear(priv->buf);
  509. spin_unlock_irqrestore(&priv->lock, flags);
  510. /* wait for characters to drain from device */
  511. bps = tty_get_baud_rate(port->tty);
  512. if (bps > 1200)
  513. timeout = max((HZ*2560)/bps,HZ/10);
  514. else
  515. timeout = 2*HZ;
  516. set_current_state(TASK_INTERRUPTIBLE);
  517. schedule_timeout(timeout);
  518. dbg("%s - stopping urbs", __FUNCTION__);
  519. usb_kill_urb (port->interrupt_in_urb);
  520. usb_kill_urb (port->interrupt_out_urb);
  521. if (port->tty) {
  522. c_cflag = port->tty->termios->c_cflag;
  523. if (c_cflag & HUPCL) {
  524. /* drop dtr and rts */
  525. priv = usb_get_serial_port_data(port);
  526. spin_lock_irqsave(&priv->lock, flags);
  527. priv->line_control = 0;
  528. priv->cmd_ctrl = 1;
  529. spin_unlock_irqrestore(&priv->lock, flags);
  530. cypress_write(port, NULL, 0);
  531. }
  532. }
  533. if (stats)
  534. dev_info (&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
  535. priv->bytes_in, priv->bytes_out, priv->cmd_count);
  536. } /* cypress_close */
  537. static int cypress_write(struct usb_serial_port *port, const unsigned char *buf, int count)
  538. {
  539. struct cypress_private *priv = usb_get_serial_port_data(port);
  540. unsigned long flags;
  541. dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
  542. /* line control commands, which need to be executed immediately,
  543. are not put into the buffer for obvious reasons.
  544. */
  545. if (priv->cmd_ctrl) {
  546. count = 0;
  547. goto finish;
  548. }
  549. if (!count)
  550. return count;
  551. spin_lock_irqsave(&priv->lock, flags);
  552. count = cypress_buf_put(priv->buf, buf, count);
  553. spin_unlock_irqrestore(&priv->lock, flags);
  554. finish:
  555. cypress_send(port);
  556. return count;
  557. } /* cypress_write */
  558. static void cypress_send(struct usb_serial_port *port)
  559. {
  560. int count = 0, result, offset, actual_size;
  561. struct cypress_private *priv = usb_get_serial_port_data(port);
  562. unsigned long flags;
  563. dbg("%s - port %d", __FUNCTION__, port->number);
  564. dbg("%s - interrupt out size is %d", __FUNCTION__, port->interrupt_out_size);
  565. spin_lock_irqsave(&priv->lock, flags);
  566. if (priv->write_urb_in_use) {
  567. dbg("%s - can't write, urb in use", __FUNCTION__);
  568. spin_unlock_irqrestore(&priv->lock, flags);
  569. return;
  570. }
  571. spin_unlock_irqrestore(&priv->lock, flags);
  572. /* clear buffer */
  573. memset(port->interrupt_out_urb->transfer_buffer, 0, port->interrupt_out_size);
  574. spin_lock_irqsave(&priv->lock, flags);
  575. switch (port->interrupt_out_size) {
  576. case 32:
  577. // this is for the CY7C64013...
  578. offset = 2;
  579. port->interrupt_out_buffer[0] = priv->line_control;
  580. break;
  581. case 8:
  582. // this is for the CY7C63743...
  583. offset = 1;
  584. port->interrupt_out_buffer[0] = priv->line_control;
  585. break;
  586. default:
  587. dbg("%s - wrong packet size", __FUNCTION__);
  588. spin_unlock_irqrestore(&priv->lock, flags);
  589. return;
  590. }
  591. if (priv->line_control & CONTROL_RESET)
  592. priv->line_control &= ~CONTROL_RESET;
  593. if (priv->cmd_ctrl) {
  594. priv->cmd_count++;
  595. dbg("%s - line control command being issued", __FUNCTION__);
  596. spin_unlock_irqrestore(&priv->lock, flags);
  597. goto send;
  598. } else
  599. spin_unlock_irqrestore(&priv->lock, flags);
  600. count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
  601. port->interrupt_out_size-offset);
  602. if (count == 0) {
  603. return;
  604. }
  605. switch (port->interrupt_out_size) {
  606. case 32:
  607. port->interrupt_out_buffer[1] = count;
  608. break;
  609. case 8:
  610. port->interrupt_out_buffer[0] |= count;
  611. }
  612. dbg("%s - count is %d", __FUNCTION__, count);
  613. send:
  614. spin_lock_irqsave(&priv->lock, flags);
  615. priv->write_urb_in_use = 1;
  616. spin_unlock_irqrestore(&priv->lock, flags);
  617. if (priv->cmd_ctrl)
  618. actual_size = 1;
  619. else
  620. actual_size = count + (port->interrupt_out_size == 32 ? 2 : 1);
  621. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, port->interrupt_out_size,
  622. port->interrupt_out_urb->transfer_buffer);
  623. port->interrupt_out_urb->transfer_buffer_length = actual_size;
  624. port->interrupt_out_urb->dev = port->serial->dev;
  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;
  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;
  844. /* toggle CRTSCTS? - don't do this if being called from cypress_open */
  845. if (!priv->calledfromopen) {
  846. if (cflag & CRTSCTS)
  847. priv->line_control |= CONTROL_RTS;
  848. else
  849. priv->line_control &= ~CONTROL_RTS;
  850. }
  851. }
  852. spin_unlock_irqrestore(&priv->lock, flags);
  853. dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, %d data_bits (+5)", __FUNCTION__,
  854. stop_bits, parity_enable, parity_type, data_bits);
  855. cypress_serial_control(port, baud_mask, data_bits, stop_bits, parity_enable,
  856. parity_type, 0, CYPRESS_SET_CONFIG);
  857. msleep(50); /* give some time between change and read (50ms) */
  858. /* we perform a CYPRESS_GET_CONFIG so that the current settings are filled into the private structure
  859. * this should confirm that all is working if it returns what we just set */
  860. cypress_serial_control(port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
  861. /* Here we can define custom tty settings for devices
  862. *
  863. * the main tty termios flag base comes from empeg.c
  864. */
  865. spin_lock_irqsave(&priv->lock, flags);
  866. if ( (priv->chiptype == CT_EARTHMATE) && (priv->baud_rate == 4800) ) {
  867. dbg("Using custom termios settings for a baud rate of 4800bps.");
  868. /* define custom termios settings for NMEA protocol */
  869. tty->termios->c_iflag /* input modes - */
  870. &= ~(IGNBRK /* disable ignore break */
  871. | BRKINT /* disable break causes interrupt */
  872. | PARMRK /* disable mark parity errors */
  873. | ISTRIP /* disable clear high bit of input characters */
  874. | INLCR /* disable translate NL to CR */
  875. | IGNCR /* disable ignore CR */
  876. | ICRNL /* disable translate CR to NL */
  877. | IXON); /* disable enable XON/XOFF flow control */
  878. tty->termios->c_oflag /* output modes */
  879. &= ~OPOST; /* disable postprocess output characters */
  880. tty->termios->c_lflag /* line discipline modes */
  881. &= ~(ECHO /* disable echo input characters */
  882. | ECHONL /* disable echo new line */
  883. | ICANON /* disable erase, kill, werase, and rprnt special characters */
  884. | ISIG /* disable interrupt, quit, and suspend special characters */
  885. | IEXTEN); /* disable non-POSIX special characters */
  886. } else if (priv->chiptype == CT_CYPHIDCOM) {
  887. // Software app handling it for device...
  888. }
  889. linechange = (priv->line_control != oldlines);
  890. spin_unlock_irqrestore(&priv->lock, flags);
  891. /* if necessary, set lines */
  892. if (!priv->calledfromopen && linechange) {
  893. priv->cmd_ctrl = 1;
  894. cypress_write(port, NULL, 0);
  895. }
  896. if (priv->calledfromopen)
  897. priv->calledfromopen = 0;
  898. } /* cypress_set_termios */
  899. /* returns amount of data still left in soft buffer */
  900. static int cypress_chars_in_buffer(struct usb_serial_port *port)
  901. {
  902. struct cypress_private *priv = usb_get_serial_port_data(port);
  903. int chars = 0;
  904. unsigned long flags;
  905. dbg("%s - port %d", __FUNCTION__, port->number);
  906. spin_lock_irqsave(&priv->lock, flags);
  907. chars = cypress_buf_data_avail(priv->buf);
  908. spin_unlock_irqrestore(&priv->lock, flags);
  909. dbg("%s - returns %d", __FUNCTION__, chars);
  910. return chars;
  911. }
  912. static void cypress_throttle (struct usb_serial_port *port)
  913. {
  914. struct cypress_private *priv = usb_get_serial_port_data(port);
  915. unsigned long flags;
  916. dbg("%s - port %d", __FUNCTION__, port->number);
  917. spin_lock_irqsave(&priv->lock, flags);
  918. priv->rx_flags = THROTTLED;
  919. spin_unlock_irqrestore(&priv->lock, flags);
  920. }
  921. static void cypress_unthrottle (struct usb_serial_port *port)
  922. {
  923. struct cypress_private *priv = usb_get_serial_port_data(port);
  924. int actually_throttled, result;
  925. unsigned long flags;
  926. dbg("%s - port %d", __FUNCTION__, port->number);
  927. spin_lock_irqsave(&priv->lock, flags);
  928. actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
  929. priv->rx_flags = 0;
  930. spin_unlock_irqrestore(&priv->lock, flags);
  931. if (actually_throttled) {
  932. port->interrupt_in_urb->dev = port->serial->dev;
  933. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  934. if (result)
  935. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
  936. }
  937. }
  938. static void cypress_read_int_callback(struct urb *urb, struct pt_regs *regs)
  939. {
  940. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  941. struct cypress_private *priv = usb_get_serial_port_data(port);
  942. struct tty_struct *tty;
  943. unsigned char *data = urb->transfer_buffer;
  944. unsigned long flags;
  945. char tty_flag = TTY_NORMAL;
  946. int havedata = 0;
  947. int bytes = 0;
  948. int result;
  949. int i = 0;
  950. dbg("%s - port %d", __FUNCTION__, port->number);
  951. if (urb->status) {
  952. dbg("%s - nonzero read status received: %d", __FUNCTION__, urb->status);
  953. return;
  954. }
  955. spin_lock_irqsave(&priv->lock, flags);
  956. if (priv->rx_flags & THROTTLED) {
  957. dbg("%s - now throttling", __FUNCTION__);
  958. priv->rx_flags |= ACTUALLY_THROTTLED;
  959. spin_unlock_irqrestore(&priv->lock, flags);
  960. return;
  961. }
  962. spin_unlock_irqrestore(&priv->lock, flags);
  963. tty = port->tty;
  964. if (!tty) {
  965. dbg("%s - bad tty pointer - exiting", __FUNCTION__);
  966. return;
  967. }
  968. spin_lock_irqsave(&priv->lock, flags);
  969. switch(urb->actual_length) {
  970. case 32:
  971. // This is for the CY7C64013...
  972. priv->current_status = data[0] & 0xF8;
  973. bytes = data[1]+2;
  974. i=2;
  975. if (bytes > 2)
  976. havedata = 1;
  977. break;
  978. case 8:
  979. // This is for the CY7C63743...
  980. priv->current_status = data[0] & 0xF8;
  981. bytes = (data[0] & 0x07)+1;
  982. i=1;
  983. if (bytes > 1)
  984. havedata = 1;
  985. break;
  986. default:
  987. dbg("%s - wrong packet size - received %d bytes", __FUNCTION__, urb->actual_length);
  988. spin_unlock_irqrestore(&priv->lock, flags);
  989. goto continue_read;
  990. }
  991. spin_unlock_irqrestore(&priv->lock, flags);
  992. usb_serial_debug_data (debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  993. spin_lock_irqsave(&priv->lock, flags);
  994. /* check to see if status has changed */
  995. if (priv != NULL) {
  996. if (priv->current_status != priv->prev_status) {
  997. priv->diff_status |= priv->current_status ^ priv->prev_status;
  998. wake_up_interruptible(&priv->delta_msr_wait);
  999. priv->prev_status = priv->current_status;
  1000. }
  1001. }
  1002. spin_unlock_irqrestore(&priv->lock, flags);
  1003. /* hangup, as defined in acm.c... this might be a bad place for it though */
  1004. if (tty && !(tty->termios->c_cflag & CLOCAL) && !(priv->current_status & UART_CD)) {
  1005. dbg("%s - calling hangup", __FUNCTION__);
  1006. tty_hangup(tty);
  1007. goto continue_read;
  1008. }
  1009. /* There is one error bit... I'm assuming it is a parity error indicator
  1010. * as the generic firmware will set this bit to 1 if a parity error occurs.
  1011. * I can not find reference to any other error events.
  1012. *
  1013. */
  1014. spin_lock_irqsave(&priv->lock, flags);
  1015. if (priv->current_status & CYP_ERROR) {
  1016. spin_unlock_irqrestore(&priv->lock, flags);
  1017. tty_flag = TTY_PARITY;
  1018. dbg("%s - Parity Error detected", __FUNCTION__);
  1019. } else
  1020. spin_unlock_irqrestore(&priv->lock, flags);
  1021. /* process read if there is data other than line status */
  1022. if (tty && (bytes > i)) {
  1023. for (; i < bytes ; ++i) {
  1024. dbg("pushing byte number %d - %d - %c",i,data[i],data[i]);
  1025. if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
  1026. tty_flip_buffer_push(tty);
  1027. }
  1028. tty_insert_flip_char(tty, data[i], tty_flag);
  1029. }
  1030. tty_flip_buffer_push(port->tty);
  1031. }
  1032. spin_lock_irqsave(&priv->lock, flags);
  1033. priv->bytes_in += bytes; /* control and status byte(s) are also counted */
  1034. spin_unlock_irqrestore(&priv->lock, flags);
  1035. continue_read:
  1036. /* Continue trying to always read... unless the port has closed. */
  1037. if (port->open_count > 0) {
  1038. usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
  1039. usb_rcvintpipe(port->serial->dev, port->interrupt_in_endpointAddress),
  1040. port->interrupt_in_urb->transfer_buffer,
  1041. port->interrupt_in_urb->transfer_buffer_length,
  1042. cypress_read_int_callback, port,
  1043. port->interrupt_in_urb->interval);
  1044. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  1045. if (result)
  1046. dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  1047. }
  1048. return;
  1049. } /* cypress_read_int_callback */
  1050. static void cypress_write_int_callback(struct urb *urb, struct pt_regs *regs)
  1051. {
  1052. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  1053. struct cypress_private *priv = usb_get_serial_port_data(port);
  1054. int result;
  1055. dbg("%s - port %d", __FUNCTION__, port->number);
  1056. switch (urb->status) {
  1057. case 0:
  1058. /* success */
  1059. break;
  1060. case -ECONNRESET:
  1061. case -ENOENT:
  1062. case -ESHUTDOWN:
  1063. /* this urb is terminated, clean up */
  1064. dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
  1065. priv->write_urb_in_use = 0;
  1066. return;
  1067. default:
  1068. /* error in the urb, so we have to resubmit it */
  1069. dbg("%s - Overflow in write", __FUNCTION__);
  1070. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  1071. port->interrupt_out_urb->transfer_buffer_length = 1;
  1072. port->interrupt_out_urb->dev = port->serial->dev;
  1073. result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
  1074. if (result)
  1075. dev_err(&urb->dev->dev, "%s - failed resubmitting write urb, error %d\n",
  1076. __FUNCTION__, result);
  1077. else
  1078. return;
  1079. }
  1080. priv->write_urb_in_use = 0;
  1081. /* send any buffered data */
  1082. cypress_send(port);
  1083. }
  1084. /*****************************************************************************
  1085. * Write buffer functions - buffering code from pl2303 used
  1086. *****************************************************************************/
  1087. /*
  1088. * cypress_buf_alloc
  1089. *
  1090. * Allocate a circular buffer and all associated memory.
  1091. */
  1092. static struct cypress_buf *cypress_buf_alloc(unsigned int size)
  1093. {
  1094. struct cypress_buf *cb;
  1095. if (size == 0)
  1096. return NULL;
  1097. cb = (struct cypress_buf *)kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
  1098. if (cb == NULL)
  1099. return NULL;
  1100. cb->buf_buf = kmalloc(size, GFP_KERNEL);
  1101. if (cb->buf_buf == NULL) {
  1102. kfree(cb);
  1103. return NULL;
  1104. }
  1105. cb->buf_size = size;
  1106. cb->buf_get = cb->buf_put = cb->buf_buf;
  1107. return cb;
  1108. }
  1109. /*
  1110. * cypress_buf_free
  1111. *
  1112. * Free the buffer and all associated memory.
  1113. */
  1114. static void cypress_buf_free(struct cypress_buf *cb)
  1115. {
  1116. if (cb) {
  1117. kfree(cb->buf_buf);
  1118. kfree(cb);
  1119. }
  1120. }
  1121. /*
  1122. * cypress_buf_clear
  1123. *
  1124. * Clear out all data in the circular buffer.
  1125. */
  1126. static void cypress_buf_clear(struct cypress_buf *cb)
  1127. {
  1128. if (cb != NULL)
  1129. cb->buf_get = cb->buf_put;
  1130. /* equivalent to a get of all data available */
  1131. }
  1132. /*
  1133. * cypress_buf_data_avail
  1134. *
  1135. * Return the number of bytes of data available in the circular
  1136. * buffer.
  1137. */
  1138. static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
  1139. {
  1140. if (cb != NULL)
  1141. return ((cb->buf_size + cb->buf_put - cb->buf_get) % cb->buf_size);
  1142. else
  1143. return 0;
  1144. }
  1145. /*
  1146. * cypress_buf_space_avail
  1147. *
  1148. * Return the number of bytes of space available in the circular
  1149. * buffer.
  1150. */
  1151. static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
  1152. {
  1153. if (cb != NULL)
  1154. return ((cb->buf_size + cb->buf_get - cb->buf_put - 1) % cb->buf_size);
  1155. else
  1156. return 0;
  1157. }
  1158. /*
  1159. * cypress_buf_put
  1160. *
  1161. * Copy data data from a user buffer and put it into the circular buffer.
  1162. * Restrict to the amount of space available.
  1163. *
  1164. * Return the number of bytes copied.
  1165. */
  1166. static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
  1167. unsigned int count)
  1168. {
  1169. unsigned int len;
  1170. if (cb == NULL)
  1171. return 0;
  1172. len = cypress_buf_space_avail(cb);
  1173. if (count > len)
  1174. count = len;
  1175. if (count == 0)
  1176. return 0;
  1177. len = cb->buf_buf + cb->buf_size - cb->buf_put;
  1178. if (count > len) {
  1179. memcpy(cb->buf_put, buf, len);
  1180. memcpy(cb->buf_buf, buf+len, count - len);
  1181. cb->buf_put = cb->buf_buf + count - len;
  1182. } else {
  1183. memcpy(cb->buf_put, buf, count);
  1184. if (count < len)
  1185. cb->buf_put += count;
  1186. else /* count == len */
  1187. cb->buf_put = cb->buf_buf;
  1188. }
  1189. return count;
  1190. }
  1191. /*
  1192. * cypress_buf_get
  1193. *
  1194. * Get data from the circular buffer and copy to the given buffer.
  1195. * Restrict to the amount of data available.
  1196. *
  1197. * Return the number of bytes copied.
  1198. */
  1199. static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
  1200. unsigned int count)
  1201. {
  1202. unsigned int len;
  1203. if (cb == NULL)
  1204. return 0;
  1205. len = cypress_buf_data_avail(cb);
  1206. if (count > len)
  1207. count = len;
  1208. if (count == 0)
  1209. return 0;
  1210. len = cb->buf_buf + cb->buf_size - cb->buf_get;
  1211. if (count > len) {
  1212. memcpy(buf, cb->buf_get, len);
  1213. memcpy(buf+len, cb->buf_buf, count - len);
  1214. cb->buf_get = cb->buf_buf + count - len;
  1215. } else {
  1216. memcpy(buf, cb->buf_get, count);
  1217. if (count < len)
  1218. cb->buf_get += count;
  1219. else /* count == len */
  1220. cb->buf_get = cb->buf_buf;
  1221. }
  1222. return count;
  1223. }
  1224. /*****************************************************************************
  1225. * Module functions
  1226. *****************************************************************************/
  1227. static int __init cypress_init(void)
  1228. {
  1229. int retval;
  1230. dbg("%s", __FUNCTION__);
  1231. retval = usb_serial_register(&cypress_earthmate_device);
  1232. if (retval)
  1233. goto failed_em_register;
  1234. retval = usb_serial_register(&cypress_hidcom_device);
  1235. if (retval)
  1236. goto failed_hidcom_register;
  1237. retval = usb_register(&cypress_driver);
  1238. if (retval)
  1239. goto failed_usb_register;
  1240. info(DRIVER_DESC " " DRIVER_VERSION);
  1241. return 0;
  1242. failed_usb_register:
  1243. usb_deregister(&cypress_driver);
  1244. failed_hidcom_register:
  1245. usb_serial_deregister(&cypress_hidcom_device);
  1246. failed_em_register:
  1247. usb_serial_deregister(&cypress_earthmate_device);
  1248. return retval;
  1249. }
  1250. static void __exit cypress_exit (void)
  1251. {
  1252. dbg("%s", __FUNCTION__);
  1253. usb_deregister (&cypress_driver);
  1254. usb_serial_deregister (&cypress_earthmate_device);
  1255. usb_serial_deregister (&cypress_hidcom_device);
  1256. }
  1257. module_init(cypress_init);
  1258. module_exit(cypress_exit);
  1259. MODULE_AUTHOR( DRIVER_AUTHOR );
  1260. MODULE_DESCRIPTION( DRIVER_DESC );
  1261. MODULE_VERSION( DRIVER_VERSION );
  1262. MODULE_LICENSE("GPL");
  1263. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1264. MODULE_PARM_DESC(debug, "Debug enabled or not");
  1265. module_param(stats, bool, S_IRUGO | S_IWUSR);
  1266. MODULE_PARM_DESC(stats, "Enable statistics or not");