cypress_m8.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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
  15. * driver
  16. *
  17. * See http://geocities.com/i0xox0i for information on this driver and the
  18. * earthmate usb device.
  19. */
  20. /* Thanks to Neil Whelchel for writing the first cypress m8 implementation
  21. for linux. */
  22. /* Thanks to cypress for providing references for the hid reports. */
  23. /* Thanks to Jiang Zhang for providing links and for general help. */
  24. /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
  25. #include <linux/kernel.h>
  26. #include <linux/errno.h>
  27. #include <linux/slab.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_driver.h>
  30. #include <linux/tty_flip.h>
  31. #include <linux/module.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include <linux/serial.h>
  37. #include <linux/kfifo.h>
  38. #include <linux/delay.h>
  39. #include <linux/uaccess.h>
  40. #include <asm/unaligned.h>
  41. #include "cypress_m8.h"
  42. static bool stats;
  43. static int interval;
  44. static bool unstable_bauds;
  45. #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
  46. #define DRIVER_DESC "Cypress USB to Serial Driver"
  47. /* write buffer size defines */
  48. #define CYPRESS_BUF_SIZE 1024
  49. static const struct usb_device_id id_table_earthmate[] = {
  50. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
  51. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
  52. { } /* Terminating entry */
  53. };
  54. static const struct usb_device_id id_table_cyphidcomrs232[] = {
  55. { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
  56. { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
  57. { USB_DEVICE(VENDOR_ID_FRWD, PRODUCT_ID_CYPHIDCOM_FRWD) },
  58. { } /* Terminating entry */
  59. };
  60. static const struct usb_device_id id_table_nokiaca42v2[] = {
  61. { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
  62. { } /* Terminating entry */
  63. };
  64. static const struct usb_device_id id_table_combined[] = {
  65. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
  66. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
  67. { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
  68. { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
  69. { USB_DEVICE(VENDOR_ID_FRWD, PRODUCT_ID_CYPHIDCOM_FRWD) },
  70. { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
  71. { } /* Terminating entry */
  72. };
  73. MODULE_DEVICE_TABLE(usb, id_table_combined);
  74. enum packet_format {
  75. packet_format_1, /* b0:status, b1:payload count */
  76. packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
  77. };
  78. struct cypress_private {
  79. spinlock_t lock; /* private lock */
  80. int chiptype; /* identifier of device, for quirks/etc */
  81. int bytes_in; /* used for statistics */
  82. int bytes_out; /* used for statistics */
  83. int cmd_count; /* used for statistics */
  84. int cmd_ctrl; /* always set this to 1 before issuing a command */
  85. struct kfifo write_fifo; /* write fifo */
  86. int write_urb_in_use; /* write urb in use indicator */
  87. int write_urb_interval; /* interval to use for write urb */
  88. int read_urb_interval; /* interval to use for read urb */
  89. int comm_is_ok; /* true if communication is (still) ok */
  90. int termios_initialized;
  91. __u8 line_control; /* holds dtr / rts value */
  92. __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
  93. __u8 current_config; /* stores the current configuration byte */
  94. __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
  95. enum packet_format pkt_fmt; /* format to use for packet send / receive */
  96. int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
  97. int baud_rate; /* stores current baud rate in
  98. integer form */
  99. int isthrottled; /* if throttled, discard reads */
  100. char prev_status; /* used for TIOCMIWAIT */
  101. /* we pass a pointer to this as the argument sent to
  102. cypress_set_termios old_termios */
  103. struct ktermios tmp_termios; /* stores the old termios settings */
  104. };
  105. /* function prototypes for the Cypress USB to serial device */
  106. static int cypress_earthmate_port_probe(struct usb_serial_port *port);
  107. static int cypress_hidcom_port_probe(struct usb_serial_port *port);
  108. static int cypress_ca42v2_port_probe(struct usb_serial_port *port);
  109. static int cypress_port_remove(struct usb_serial_port *port);
  110. static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
  111. static void cypress_close(struct usb_serial_port *port);
  112. static void cypress_dtr_rts(struct usb_serial_port *port, int on);
  113. static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
  114. const unsigned char *buf, int count);
  115. static void cypress_send(struct usb_serial_port *port);
  116. static int cypress_write_room(struct tty_struct *tty);
  117. static void cypress_set_termios(struct tty_struct *tty,
  118. struct usb_serial_port *port, struct ktermios *old);
  119. static int cypress_tiocmget(struct tty_struct *tty);
  120. static int cypress_tiocmset(struct tty_struct *tty,
  121. unsigned int set, unsigned int clear);
  122. static int cypress_chars_in_buffer(struct tty_struct *tty);
  123. static void cypress_throttle(struct tty_struct *tty);
  124. static void cypress_unthrottle(struct tty_struct *tty);
  125. static void cypress_set_dead(struct usb_serial_port *port);
  126. static void cypress_read_int_callback(struct urb *urb);
  127. static void cypress_write_int_callback(struct urb *urb);
  128. static struct usb_serial_driver cypress_earthmate_device = {
  129. .driver = {
  130. .owner = THIS_MODULE,
  131. .name = "earthmate",
  132. },
  133. .description = "DeLorme Earthmate USB",
  134. .id_table = id_table_earthmate,
  135. .num_ports = 1,
  136. .port_probe = cypress_earthmate_port_probe,
  137. .port_remove = cypress_port_remove,
  138. .open = cypress_open,
  139. .close = cypress_close,
  140. .dtr_rts = cypress_dtr_rts,
  141. .write = cypress_write,
  142. .write_room = cypress_write_room,
  143. .set_termios = cypress_set_termios,
  144. .tiocmget = cypress_tiocmget,
  145. .tiocmset = cypress_tiocmset,
  146. .tiocmiwait = usb_serial_generic_tiocmiwait,
  147. .chars_in_buffer = cypress_chars_in_buffer,
  148. .throttle = cypress_throttle,
  149. .unthrottle = cypress_unthrottle,
  150. .read_int_callback = cypress_read_int_callback,
  151. .write_int_callback = cypress_write_int_callback,
  152. };
  153. static struct usb_serial_driver cypress_hidcom_device = {
  154. .driver = {
  155. .owner = THIS_MODULE,
  156. .name = "cyphidcom",
  157. },
  158. .description = "HID->COM RS232 Adapter",
  159. .id_table = id_table_cyphidcomrs232,
  160. .num_ports = 1,
  161. .port_probe = cypress_hidcom_port_probe,
  162. .port_remove = cypress_port_remove,
  163. .open = cypress_open,
  164. .close = cypress_close,
  165. .dtr_rts = cypress_dtr_rts,
  166. .write = cypress_write,
  167. .write_room = cypress_write_room,
  168. .set_termios = cypress_set_termios,
  169. .tiocmget = cypress_tiocmget,
  170. .tiocmset = cypress_tiocmset,
  171. .tiocmiwait = usb_serial_generic_tiocmiwait,
  172. .chars_in_buffer = cypress_chars_in_buffer,
  173. .throttle = cypress_throttle,
  174. .unthrottle = cypress_unthrottle,
  175. .read_int_callback = cypress_read_int_callback,
  176. .write_int_callback = cypress_write_int_callback,
  177. };
  178. static struct usb_serial_driver cypress_ca42v2_device = {
  179. .driver = {
  180. .owner = THIS_MODULE,
  181. .name = "nokiaca42v2",
  182. },
  183. .description = "Nokia CA-42 V2 Adapter",
  184. .id_table = id_table_nokiaca42v2,
  185. .num_ports = 1,
  186. .port_probe = cypress_ca42v2_port_probe,
  187. .port_remove = cypress_port_remove,
  188. .open = cypress_open,
  189. .close = cypress_close,
  190. .dtr_rts = cypress_dtr_rts,
  191. .write = cypress_write,
  192. .write_room = cypress_write_room,
  193. .set_termios = cypress_set_termios,
  194. .tiocmget = cypress_tiocmget,
  195. .tiocmset = cypress_tiocmset,
  196. .tiocmiwait = usb_serial_generic_tiocmiwait,
  197. .chars_in_buffer = cypress_chars_in_buffer,
  198. .throttle = cypress_throttle,
  199. .unthrottle = cypress_unthrottle,
  200. .read_int_callback = cypress_read_int_callback,
  201. .write_int_callback = cypress_write_int_callback,
  202. };
  203. static struct usb_serial_driver * const serial_drivers[] = {
  204. &cypress_earthmate_device, &cypress_hidcom_device,
  205. &cypress_ca42v2_device, NULL
  206. };
  207. /*****************************************************************************
  208. * Cypress serial helper functions
  209. *****************************************************************************/
  210. /* FRWD Dongle hidcom needs to skip reset and speed checks */
  211. static inline bool is_frwd(struct usb_device *dev)
  212. {
  213. return ((le16_to_cpu(dev->descriptor.idVendor) == VENDOR_ID_FRWD) &&
  214. (le16_to_cpu(dev->descriptor.idProduct) == PRODUCT_ID_CYPHIDCOM_FRWD));
  215. }
  216. static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
  217. {
  218. struct cypress_private *priv;
  219. priv = usb_get_serial_port_data(port);
  220. if (unstable_bauds)
  221. return new_rate;
  222. /* FRWD Dongle uses 115200 bps */
  223. if (is_frwd(port->serial->dev))
  224. return new_rate;
  225. /*
  226. * The general purpose firmware for the Cypress M8 allows for
  227. * a maximum speed of 57600bps (I have no idea whether DeLorme
  228. * chose to use the general purpose firmware or not), if you
  229. * need to modify this speed setting for your own project
  230. * please add your own chiptype and modify the code likewise.
  231. * The Cypress HID->COM device will work successfully up to
  232. * 115200bps (but the actual throughput is around 3kBps).
  233. */
  234. if (port->serial->dev->speed == USB_SPEED_LOW) {
  235. /*
  236. * Mike Isely <isely@pobox.com> 2-Feb-2008: The
  237. * Cypress app note that describes this mechanism
  238. * states the the low-speed part can't handle more
  239. * than 800 bytes/sec, in which case 4800 baud is the
  240. * safest speed for a part like that.
  241. */
  242. if (new_rate > 4800) {
  243. dev_dbg(&port->dev,
  244. "%s - failed setting baud rate, device incapable speed %d\n",
  245. __func__, new_rate);
  246. return -1;
  247. }
  248. }
  249. switch (priv->chiptype) {
  250. case CT_EARTHMATE:
  251. if (new_rate <= 600) {
  252. /* 300 and 600 baud rates are supported under
  253. * the generic firmware, but are not used with
  254. * NMEA and SiRF protocols */
  255. dev_dbg(&port->dev,
  256. "%s - failed setting baud rate, unsupported speed of %d on Earthmate GPS\n",
  257. __func__, new_rate);
  258. return -1;
  259. }
  260. break;
  261. default:
  262. break;
  263. }
  264. return new_rate;
  265. }
  266. /* This function can either set or retrieve the current serial line settings */
  267. static int cypress_serial_control(struct tty_struct *tty,
  268. struct usb_serial_port *port, speed_t baud_rate, int data_bits,
  269. int stop_bits, int parity_enable, int parity_type, int reset,
  270. int cypress_request_type)
  271. {
  272. int new_baudrate = 0, retval = 0, tries = 0;
  273. struct cypress_private *priv;
  274. struct device *dev = &port->dev;
  275. u8 *feature_buffer;
  276. const unsigned int feature_len = 5;
  277. unsigned long flags;
  278. priv = usb_get_serial_port_data(port);
  279. if (!priv->comm_is_ok)
  280. return -ENODEV;
  281. feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
  282. if (!feature_buffer)
  283. return -ENOMEM;
  284. switch (cypress_request_type) {
  285. case CYPRESS_SET_CONFIG:
  286. /* 0 means 'Hang up' so doesn't change the true bit rate */
  287. new_baudrate = priv->baud_rate;
  288. if (baud_rate && baud_rate != priv->baud_rate) {
  289. dev_dbg(dev, "%s - baud rate is changing\n", __func__);
  290. retval = analyze_baud_rate(port, baud_rate);
  291. if (retval >= 0) {
  292. new_baudrate = retval;
  293. dev_dbg(dev, "%s - New baud rate set to %d\n",
  294. __func__, new_baudrate);
  295. }
  296. }
  297. dev_dbg(dev, "%s - baud rate is being sent as %d\n", __func__,
  298. new_baudrate);
  299. /* fill the feature_buffer with new configuration */
  300. put_unaligned_le32(new_baudrate, feature_buffer);
  301. feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
  302. /* 1 bit gap */
  303. feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
  304. feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
  305. feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
  306. /* 1 bit gap */
  307. feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
  308. dev_dbg(dev, "%s - device is being sent this feature report:\n", __func__);
  309. dev_dbg(dev, "%s - %02X - %02X - %02X - %02X - %02X\n", __func__,
  310. feature_buffer[0], feature_buffer[1],
  311. feature_buffer[2], feature_buffer[3],
  312. feature_buffer[4]);
  313. do {
  314. retval = usb_control_msg(port->serial->dev,
  315. usb_sndctrlpipe(port->serial->dev, 0),
  316. HID_REQ_SET_REPORT,
  317. USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  318. 0x0300, 0, feature_buffer,
  319. feature_len, 500);
  320. if (tries++ >= 3)
  321. break;
  322. } while (retval != feature_len &&
  323. retval != -ENODEV);
  324. if (retval != feature_len) {
  325. dev_err(dev, "%s - failed sending serial line settings - %d\n",
  326. __func__, retval);
  327. cypress_set_dead(port);
  328. } else {
  329. spin_lock_irqsave(&priv->lock, flags);
  330. priv->baud_rate = new_baudrate;
  331. priv->current_config = feature_buffer[4];
  332. spin_unlock_irqrestore(&priv->lock, flags);
  333. /* If we asked for a speed change encode it */
  334. if (baud_rate)
  335. tty_encode_baud_rate(tty,
  336. new_baudrate, new_baudrate);
  337. }
  338. break;
  339. case CYPRESS_GET_CONFIG:
  340. if (priv->get_cfg_unsafe) {
  341. /* Not implemented for this device,
  342. and if we try to do it we're likely
  343. to crash the hardware. */
  344. retval = -ENOTTY;
  345. goto out;
  346. }
  347. dev_dbg(dev, "%s - retreiving serial line settings\n", __func__);
  348. do {
  349. retval = usb_control_msg(port->serial->dev,
  350. usb_rcvctrlpipe(port->serial->dev, 0),
  351. HID_REQ_GET_REPORT,
  352. USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  353. 0x0300, 0, feature_buffer,
  354. feature_len, 500);
  355. if (tries++ >= 3)
  356. break;
  357. } while (retval != feature_len
  358. && retval != -ENODEV);
  359. if (retval != feature_len) {
  360. dev_err(dev, "%s - failed to retrieve serial line settings - %d\n",
  361. __func__, retval);
  362. cypress_set_dead(port);
  363. goto out;
  364. } else {
  365. spin_lock_irqsave(&priv->lock, flags);
  366. /* store the config in one byte, and later
  367. use bit masks to check values */
  368. priv->current_config = feature_buffer[4];
  369. priv->baud_rate = get_unaligned_le32(feature_buffer);
  370. spin_unlock_irqrestore(&priv->lock, flags);
  371. }
  372. }
  373. spin_lock_irqsave(&priv->lock, flags);
  374. ++priv->cmd_count;
  375. spin_unlock_irqrestore(&priv->lock, flags);
  376. out:
  377. kfree(feature_buffer);
  378. return retval;
  379. } /* cypress_serial_control */
  380. static void cypress_set_dead(struct usb_serial_port *port)
  381. {
  382. struct cypress_private *priv = usb_get_serial_port_data(port);
  383. unsigned long flags;
  384. spin_lock_irqsave(&priv->lock, flags);
  385. if (!priv->comm_is_ok) {
  386. spin_unlock_irqrestore(&priv->lock, flags);
  387. return;
  388. }
  389. priv->comm_is_ok = 0;
  390. spin_unlock_irqrestore(&priv->lock, flags);
  391. dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
  392. "interval might be too short\n", port->port_number);
  393. }
  394. /*****************************************************************************
  395. * Cypress serial driver functions
  396. *****************************************************************************/
  397. static int cypress_generic_port_probe(struct usb_serial_port *port)
  398. {
  399. struct usb_serial *serial = port->serial;
  400. struct cypress_private *priv;
  401. priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
  402. if (!priv)
  403. return -ENOMEM;
  404. priv->comm_is_ok = !0;
  405. spin_lock_init(&priv->lock);
  406. if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
  407. kfree(priv);
  408. return -ENOMEM;
  409. }
  410. /* Skip reset for FRWD device. It is a workaound:
  411. device hangs if it receives SET_CONFIGURE in Configured
  412. state. */
  413. if (!is_frwd(serial->dev))
  414. usb_reset_configuration(serial->dev);
  415. priv->cmd_ctrl = 0;
  416. priv->line_control = 0;
  417. priv->termios_initialized = 0;
  418. priv->rx_flags = 0;
  419. /* Default packet format setting is determined by packet size.
  420. Anything with a size larger then 9 must have a separate
  421. count field since the 3 bit count field is otherwise too
  422. small. Otherwise we can use the slightly more compact
  423. format. This is in accordance with the cypress_m8 serial
  424. converter app note. */
  425. if (port->interrupt_out_size > 9)
  426. priv->pkt_fmt = packet_format_1;
  427. else
  428. priv->pkt_fmt = packet_format_2;
  429. if (interval > 0) {
  430. priv->write_urb_interval = interval;
  431. priv->read_urb_interval = interval;
  432. dev_dbg(&port->dev, "%s - read & write intervals forced to %d\n",
  433. __func__, interval);
  434. } else {
  435. priv->write_urb_interval = port->interrupt_out_urb->interval;
  436. priv->read_urb_interval = port->interrupt_in_urb->interval;
  437. dev_dbg(&port->dev, "%s - intervals: read=%d write=%d\n",
  438. __func__, priv->read_urb_interval,
  439. priv->write_urb_interval);
  440. }
  441. usb_set_serial_port_data(port, priv);
  442. port->port.drain_delay = 256;
  443. return 0;
  444. }
  445. static int cypress_earthmate_port_probe(struct usb_serial_port *port)
  446. {
  447. struct usb_serial *serial = port->serial;
  448. struct cypress_private *priv;
  449. int ret;
  450. ret = cypress_generic_port_probe(port);
  451. if (ret) {
  452. dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
  453. return ret;
  454. }
  455. priv = usb_get_serial_port_data(port);
  456. priv->chiptype = CT_EARTHMATE;
  457. /* All Earthmate devices use the separated-count packet
  458. format! Idiotic. */
  459. priv->pkt_fmt = packet_format_1;
  460. if (serial->dev->descriptor.idProduct !=
  461. cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
  462. /* The old original USB Earthmate seemed able to
  463. handle GET_CONFIG requests; everything they've
  464. produced since that time crashes if this command is
  465. attempted :-( */
  466. dev_dbg(&port->dev,
  467. "%s - Marking this device as unsafe for GET_CONFIG commands\n",
  468. __func__);
  469. priv->get_cfg_unsafe = !0;
  470. }
  471. return 0;
  472. }
  473. static int cypress_hidcom_port_probe(struct usb_serial_port *port)
  474. {
  475. struct cypress_private *priv;
  476. int ret;
  477. ret = cypress_generic_port_probe(port);
  478. if (ret) {
  479. dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
  480. return ret;
  481. }
  482. priv = usb_get_serial_port_data(port);
  483. priv->chiptype = CT_CYPHIDCOM;
  484. return 0;
  485. }
  486. static int cypress_ca42v2_port_probe(struct usb_serial_port *port)
  487. {
  488. struct cypress_private *priv;
  489. int ret;
  490. ret = cypress_generic_port_probe(port);
  491. if (ret) {
  492. dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
  493. return ret;
  494. }
  495. priv = usb_get_serial_port_data(port);
  496. priv->chiptype = CT_CA42V2;
  497. return 0;
  498. }
  499. static int cypress_port_remove(struct usb_serial_port *port)
  500. {
  501. struct cypress_private *priv;
  502. priv = usb_get_serial_port_data(port);
  503. kfifo_free(&priv->write_fifo);
  504. kfree(priv);
  505. return 0;
  506. }
  507. static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
  508. {
  509. struct cypress_private *priv = usb_get_serial_port_data(port);
  510. struct usb_serial *serial = port->serial;
  511. unsigned long flags;
  512. int result = 0;
  513. if (!priv->comm_is_ok)
  514. return -EIO;
  515. /* clear halts before open */
  516. usb_clear_halt(serial->dev, 0x81);
  517. usb_clear_halt(serial->dev, 0x02);
  518. spin_lock_irqsave(&priv->lock, flags);
  519. /* reset read/write statistics */
  520. priv->bytes_in = 0;
  521. priv->bytes_out = 0;
  522. priv->cmd_count = 0;
  523. priv->rx_flags = 0;
  524. spin_unlock_irqrestore(&priv->lock, flags);
  525. /* Set termios */
  526. cypress_send(port);
  527. if (tty)
  528. cypress_set_termios(tty, port, &priv->tmp_termios);
  529. /* setup the port and start reading from the device */
  530. if (!port->interrupt_in_urb) {
  531. dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
  532. __func__);
  533. return -1;
  534. }
  535. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  536. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  537. port->interrupt_in_urb->transfer_buffer,
  538. port->interrupt_in_urb->transfer_buffer_length,
  539. cypress_read_int_callback, port, priv->read_urb_interval);
  540. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  541. if (result) {
  542. dev_err(&port->dev,
  543. "%s - failed submitting read urb, error %d\n",
  544. __func__, result);
  545. cypress_set_dead(port);
  546. }
  547. return result;
  548. } /* cypress_open */
  549. static void cypress_dtr_rts(struct usb_serial_port *port, int on)
  550. {
  551. struct cypress_private *priv = usb_get_serial_port_data(port);
  552. /* drop dtr and rts */
  553. spin_lock_irq(&priv->lock);
  554. if (on == 0)
  555. priv->line_control = 0;
  556. else
  557. priv->line_control = CONTROL_DTR | CONTROL_RTS;
  558. priv->cmd_ctrl = 1;
  559. spin_unlock_irq(&priv->lock);
  560. cypress_write(NULL, port, NULL, 0);
  561. }
  562. static void cypress_close(struct usb_serial_port *port)
  563. {
  564. struct cypress_private *priv = usb_get_serial_port_data(port);
  565. unsigned long flags;
  566. spin_lock_irqsave(&priv->lock, flags);
  567. kfifo_reset_out(&priv->write_fifo);
  568. spin_unlock_irqrestore(&priv->lock, flags);
  569. dev_dbg(&port->dev, "%s - stopping urbs\n", __func__);
  570. usb_kill_urb(port->interrupt_in_urb);
  571. usb_kill_urb(port->interrupt_out_urb);
  572. if (stats)
  573. dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
  574. priv->bytes_in, priv->bytes_out, priv->cmd_count);
  575. } /* cypress_close */
  576. static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
  577. const unsigned char *buf, int count)
  578. {
  579. struct cypress_private *priv = usb_get_serial_port_data(port);
  580. dev_dbg(&port->dev, "%s - %d bytes\n", __func__, count);
  581. /* line control commands, which need to be executed immediately,
  582. are not put into the buffer for obvious reasons.
  583. */
  584. if (priv->cmd_ctrl) {
  585. count = 0;
  586. goto finish;
  587. }
  588. if (!count)
  589. return count;
  590. count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
  591. finish:
  592. cypress_send(port);
  593. return count;
  594. } /* cypress_write */
  595. static void cypress_send(struct usb_serial_port *port)
  596. {
  597. int count = 0, result, offset, actual_size;
  598. struct cypress_private *priv = usb_get_serial_port_data(port);
  599. struct device *dev = &port->dev;
  600. unsigned long flags;
  601. if (!priv->comm_is_ok)
  602. return;
  603. dev_dbg(dev, "%s - interrupt out size is %d\n", __func__,
  604. port->interrupt_out_size);
  605. spin_lock_irqsave(&priv->lock, flags);
  606. if (priv->write_urb_in_use) {
  607. dev_dbg(dev, "%s - can't write, urb in use\n", __func__);
  608. spin_unlock_irqrestore(&priv->lock, flags);
  609. return;
  610. }
  611. spin_unlock_irqrestore(&priv->lock, flags);
  612. /* clear buffer */
  613. memset(port->interrupt_out_urb->transfer_buffer, 0,
  614. port->interrupt_out_size);
  615. spin_lock_irqsave(&priv->lock, flags);
  616. switch (priv->pkt_fmt) {
  617. default:
  618. case packet_format_1:
  619. /* this is for the CY7C64013... */
  620. offset = 2;
  621. port->interrupt_out_buffer[0] = priv->line_control;
  622. break;
  623. case packet_format_2:
  624. /* this is for the CY7C63743... */
  625. offset = 1;
  626. port->interrupt_out_buffer[0] = priv->line_control;
  627. break;
  628. }
  629. if (priv->line_control & CONTROL_RESET)
  630. priv->line_control &= ~CONTROL_RESET;
  631. if (priv->cmd_ctrl) {
  632. priv->cmd_count++;
  633. dev_dbg(dev, "%s - line control command being issued\n", __func__);
  634. spin_unlock_irqrestore(&priv->lock, flags);
  635. goto send;
  636. } else
  637. spin_unlock_irqrestore(&priv->lock, flags);
  638. count = kfifo_out_locked(&priv->write_fifo,
  639. &port->interrupt_out_buffer[offset],
  640. port->interrupt_out_size - offset,
  641. &priv->lock);
  642. if (count == 0)
  643. return;
  644. switch (priv->pkt_fmt) {
  645. default:
  646. case packet_format_1:
  647. port->interrupt_out_buffer[1] = count;
  648. break;
  649. case packet_format_2:
  650. port->interrupt_out_buffer[0] |= count;
  651. }
  652. dev_dbg(dev, "%s - count is %d\n", __func__, count);
  653. send:
  654. spin_lock_irqsave(&priv->lock, flags);
  655. priv->write_urb_in_use = 1;
  656. spin_unlock_irqrestore(&priv->lock, flags);
  657. if (priv->cmd_ctrl)
  658. actual_size = 1;
  659. else
  660. actual_size = count +
  661. (priv->pkt_fmt == packet_format_1 ? 2 : 1);
  662. usb_serial_debug_data(dev, __func__, port->interrupt_out_size,
  663. port->interrupt_out_urb->transfer_buffer);
  664. usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
  665. usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
  666. port->interrupt_out_buffer, port->interrupt_out_size,
  667. cypress_write_int_callback, port, priv->write_urb_interval);
  668. result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
  669. if (result) {
  670. dev_err_console(port,
  671. "%s - failed submitting write urb, error %d\n",
  672. __func__, result);
  673. priv->write_urb_in_use = 0;
  674. cypress_set_dead(port);
  675. }
  676. spin_lock_irqsave(&priv->lock, flags);
  677. if (priv->cmd_ctrl)
  678. priv->cmd_ctrl = 0;
  679. /* do not count the line control and size bytes */
  680. priv->bytes_out += count;
  681. spin_unlock_irqrestore(&priv->lock, flags);
  682. usb_serial_port_softint(port);
  683. } /* cypress_send */
  684. /* returns how much space is available in the soft buffer */
  685. static int cypress_write_room(struct tty_struct *tty)
  686. {
  687. struct usb_serial_port *port = tty->driver_data;
  688. struct cypress_private *priv = usb_get_serial_port_data(port);
  689. int room = 0;
  690. unsigned long flags;
  691. spin_lock_irqsave(&priv->lock, flags);
  692. room = kfifo_avail(&priv->write_fifo);
  693. spin_unlock_irqrestore(&priv->lock, flags);
  694. dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
  695. return room;
  696. }
  697. static int cypress_tiocmget(struct tty_struct *tty)
  698. {
  699. struct usb_serial_port *port = tty->driver_data;
  700. struct cypress_private *priv = usb_get_serial_port_data(port);
  701. __u8 status, control;
  702. unsigned int result = 0;
  703. unsigned long flags;
  704. spin_lock_irqsave(&priv->lock, flags);
  705. control = priv->line_control;
  706. status = priv->current_status;
  707. spin_unlock_irqrestore(&priv->lock, flags);
  708. result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
  709. | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
  710. | ((status & UART_CTS) ? TIOCM_CTS : 0)
  711. | ((status & UART_DSR) ? TIOCM_DSR : 0)
  712. | ((status & UART_RI) ? TIOCM_RI : 0)
  713. | ((status & UART_CD) ? TIOCM_CD : 0);
  714. dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
  715. return result;
  716. }
  717. static int cypress_tiocmset(struct tty_struct *tty,
  718. unsigned int set, unsigned int clear)
  719. {
  720. struct usb_serial_port *port = tty->driver_data;
  721. struct cypress_private *priv = usb_get_serial_port_data(port);
  722. unsigned long flags;
  723. spin_lock_irqsave(&priv->lock, flags);
  724. if (set & TIOCM_RTS)
  725. priv->line_control |= CONTROL_RTS;
  726. if (set & TIOCM_DTR)
  727. priv->line_control |= CONTROL_DTR;
  728. if (clear & TIOCM_RTS)
  729. priv->line_control &= ~CONTROL_RTS;
  730. if (clear & TIOCM_DTR)
  731. priv->line_control &= ~CONTROL_DTR;
  732. priv->cmd_ctrl = 1;
  733. spin_unlock_irqrestore(&priv->lock, flags);
  734. return cypress_write(tty, port, NULL, 0);
  735. }
  736. static void cypress_set_termios(struct tty_struct *tty,
  737. struct usb_serial_port *port, struct ktermios *old_termios)
  738. {
  739. struct cypress_private *priv = usb_get_serial_port_data(port);
  740. struct device *dev = &port->dev;
  741. int data_bits, stop_bits, parity_type, parity_enable;
  742. unsigned cflag, iflag;
  743. unsigned long flags;
  744. __u8 oldlines;
  745. int linechange = 0;
  746. spin_lock_irqsave(&priv->lock, flags);
  747. /* We can't clean this one up as we don't know the device type
  748. early enough */
  749. if (!priv->termios_initialized) {
  750. if (priv->chiptype == CT_EARTHMATE) {
  751. tty->termios = tty_std_termios;
  752. tty->termios.c_cflag = B4800 | CS8 | CREAD | HUPCL |
  753. CLOCAL;
  754. tty->termios.c_ispeed = 4800;
  755. tty->termios.c_ospeed = 4800;
  756. } else if (priv->chiptype == CT_CYPHIDCOM) {
  757. tty->termios = tty_std_termios;
  758. tty->termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
  759. CLOCAL;
  760. tty->termios.c_ispeed = 9600;
  761. tty->termios.c_ospeed = 9600;
  762. } else if (priv->chiptype == CT_CA42V2) {
  763. tty->termios = tty_std_termios;
  764. tty->termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
  765. CLOCAL;
  766. tty->termios.c_ispeed = 9600;
  767. tty->termios.c_ospeed = 9600;
  768. }
  769. priv->termios_initialized = 1;
  770. }
  771. spin_unlock_irqrestore(&priv->lock, flags);
  772. /* Unsupported features need clearing */
  773. tty->termios.c_cflag &= ~(CMSPAR|CRTSCTS);
  774. cflag = tty->termios.c_cflag;
  775. iflag = tty->termios.c_iflag;
  776. /* check if there are new settings */
  777. if (old_termios) {
  778. spin_lock_irqsave(&priv->lock, flags);
  779. priv->tmp_termios = tty->termios;
  780. spin_unlock_irqrestore(&priv->lock, flags);
  781. }
  782. /* set number of data bits, parity, stop bits */
  783. /* when parity is disabled the parity type bit is ignored */
  784. /* 1 means 2 stop bits, 0 means 1 stop bit */
  785. stop_bits = cflag & CSTOPB ? 1 : 0;
  786. if (cflag & PARENB) {
  787. parity_enable = 1;
  788. /* 1 means odd parity, 0 means even parity */
  789. parity_type = cflag & PARODD ? 1 : 0;
  790. } else
  791. parity_enable = parity_type = 0;
  792. switch (cflag & CSIZE) {
  793. case CS5:
  794. data_bits = 0;
  795. break;
  796. case CS6:
  797. data_bits = 1;
  798. break;
  799. case CS7:
  800. data_bits = 2;
  801. break;
  802. case CS8:
  803. data_bits = 3;
  804. break;
  805. default:
  806. dev_err(dev, "%s - CSIZE was set, but not CS5-CS8\n", __func__);
  807. data_bits = 3;
  808. }
  809. spin_lock_irqsave(&priv->lock, flags);
  810. oldlines = priv->line_control;
  811. if ((cflag & CBAUD) == B0) {
  812. /* drop dtr and rts */
  813. dev_dbg(dev, "%s - dropping the lines, baud rate 0bps\n", __func__);
  814. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  815. } else
  816. priv->line_control = (CONTROL_DTR | CONTROL_RTS);
  817. spin_unlock_irqrestore(&priv->lock, flags);
  818. dev_dbg(dev, "%s - sending %d stop_bits, %d parity_enable, %d parity_type, %d data_bits (+5)\n",
  819. __func__, stop_bits, parity_enable, parity_type, data_bits);
  820. cypress_serial_control(tty, port, tty_get_baud_rate(tty),
  821. data_bits, stop_bits,
  822. parity_enable, parity_type,
  823. 0, CYPRESS_SET_CONFIG);
  824. /* we perform a CYPRESS_GET_CONFIG so that the current settings are
  825. * filled into the private structure this should confirm that all is
  826. * working if it returns what we just set */
  827. cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
  828. /* Here we can define custom tty settings for devices; the main tty
  829. * termios flag base comes from empeg.c */
  830. spin_lock_irqsave(&priv->lock, flags);
  831. if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
  832. dev_dbg(dev, "Using custom termios settings for a baud rate of 4800bps.\n");
  833. /* define custom termios settings for NMEA protocol */
  834. tty->termios.c_iflag /* input modes - */
  835. &= ~(IGNBRK /* disable ignore break */
  836. | BRKINT /* disable break causes interrupt */
  837. | PARMRK /* disable mark parity errors */
  838. | ISTRIP /* disable clear high bit of input char */
  839. | INLCR /* disable translate NL to CR */
  840. | IGNCR /* disable ignore CR */
  841. | ICRNL /* disable translate CR to NL */
  842. | IXON); /* disable enable XON/XOFF flow control */
  843. tty->termios.c_oflag /* output modes */
  844. &= ~OPOST; /* disable postprocess output char */
  845. tty->termios.c_lflag /* line discipline modes */
  846. &= ~(ECHO /* disable echo input characters */
  847. | ECHONL /* disable echo new line */
  848. | ICANON /* disable erase, kill, werase, and rprnt
  849. special characters */
  850. | ISIG /* disable interrupt, quit, and suspend
  851. special characters */
  852. | IEXTEN); /* disable non-POSIX special characters */
  853. } /* CT_CYPHIDCOM: Application should handle this for device */
  854. linechange = (priv->line_control != oldlines);
  855. spin_unlock_irqrestore(&priv->lock, flags);
  856. /* if necessary, set lines */
  857. if (linechange) {
  858. priv->cmd_ctrl = 1;
  859. cypress_write(tty, port, NULL, 0);
  860. }
  861. } /* cypress_set_termios */
  862. /* returns amount of data still left in soft buffer */
  863. static int cypress_chars_in_buffer(struct tty_struct *tty)
  864. {
  865. struct usb_serial_port *port = tty->driver_data;
  866. struct cypress_private *priv = usb_get_serial_port_data(port);
  867. int chars = 0;
  868. unsigned long flags;
  869. spin_lock_irqsave(&priv->lock, flags);
  870. chars = kfifo_len(&priv->write_fifo);
  871. spin_unlock_irqrestore(&priv->lock, flags);
  872. dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
  873. return chars;
  874. }
  875. static void cypress_throttle(struct tty_struct *tty)
  876. {
  877. struct usb_serial_port *port = tty->driver_data;
  878. struct cypress_private *priv = usb_get_serial_port_data(port);
  879. spin_lock_irq(&priv->lock);
  880. priv->rx_flags = THROTTLED;
  881. spin_unlock_irq(&priv->lock);
  882. }
  883. static void cypress_unthrottle(struct tty_struct *tty)
  884. {
  885. struct usb_serial_port *port = tty->driver_data;
  886. struct cypress_private *priv = usb_get_serial_port_data(port);
  887. int actually_throttled, result;
  888. spin_lock_irq(&priv->lock);
  889. actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
  890. priv->rx_flags = 0;
  891. spin_unlock_irq(&priv->lock);
  892. if (!priv->comm_is_ok)
  893. return;
  894. if (actually_throttled) {
  895. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  896. if (result) {
  897. dev_err(&port->dev, "%s - failed submitting read urb, "
  898. "error %d\n", __func__, result);
  899. cypress_set_dead(port);
  900. }
  901. }
  902. }
  903. static void cypress_read_int_callback(struct urb *urb)
  904. {
  905. struct usb_serial_port *port = urb->context;
  906. struct cypress_private *priv = usb_get_serial_port_data(port);
  907. struct device *dev = &urb->dev->dev;
  908. struct tty_struct *tty;
  909. unsigned char *data = urb->transfer_buffer;
  910. unsigned long flags;
  911. char tty_flag = TTY_NORMAL;
  912. int havedata = 0;
  913. int bytes = 0;
  914. int result;
  915. int i = 0;
  916. int status = urb->status;
  917. switch (status) {
  918. case 0: /* success */
  919. break;
  920. case -ECONNRESET:
  921. case -ENOENT:
  922. case -ESHUTDOWN:
  923. /* precursor to disconnect so just go away */
  924. return;
  925. case -EPIPE:
  926. /* Can't call usb_clear_halt while in_interrupt */
  927. /* FALLS THROUGH */
  928. default:
  929. /* something ugly is going on... */
  930. dev_err(dev, "%s - unexpected nonzero read status received: %d\n",
  931. __func__, status);
  932. cypress_set_dead(port);
  933. return;
  934. }
  935. spin_lock_irqsave(&priv->lock, flags);
  936. if (priv->rx_flags & THROTTLED) {
  937. dev_dbg(dev, "%s - now throttling\n", __func__);
  938. priv->rx_flags |= ACTUALLY_THROTTLED;
  939. spin_unlock_irqrestore(&priv->lock, flags);
  940. return;
  941. }
  942. spin_unlock_irqrestore(&priv->lock, flags);
  943. tty = tty_port_tty_get(&port->port);
  944. if (!tty) {
  945. dev_dbg(dev, "%s - bad tty pointer - exiting\n", __func__);
  946. return;
  947. }
  948. spin_lock_irqsave(&priv->lock, flags);
  949. result = urb->actual_length;
  950. switch (priv->pkt_fmt) {
  951. default:
  952. case packet_format_1:
  953. /* This is for the CY7C64013... */
  954. priv->current_status = data[0] & 0xF8;
  955. bytes = data[1] + 2;
  956. i = 2;
  957. if (bytes > 2)
  958. havedata = 1;
  959. break;
  960. case packet_format_2:
  961. /* This is for the CY7C63743... */
  962. priv->current_status = data[0] & 0xF8;
  963. bytes = (data[0] & 0x07) + 1;
  964. i = 1;
  965. if (bytes > 1)
  966. havedata = 1;
  967. break;
  968. }
  969. spin_unlock_irqrestore(&priv->lock, flags);
  970. if (result < bytes) {
  971. dev_dbg(dev,
  972. "%s - wrong packet size - received %d bytes but packet said %d bytes\n",
  973. __func__, result, bytes);
  974. goto continue_read;
  975. }
  976. usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
  977. spin_lock_irqsave(&priv->lock, flags);
  978. /* check to see if status has changed */
  979. if (priv->current_status != priv->prev_status) {
  980. u8 delta = priv->current_status ^ priv->prev_status;
  981. if (delta & UART_MSR_MASK) {
  982. if (delta & UART_CTS)
  983. port->icount.cts++;
  984. if (delta & UART_DSR)
  985. port->icount.dsr++;
  986. if (delta & UART_RI)
  987. port->icount.rng++;
  988. if (delta & UART_CD)
  989. port->icount.dcd++;
  990. wake_up_interruptible(&port->port.delta_msr_wait);
  991. }
  992. priv->prev_status = priv->current_status;
  993. }
  994. spin_unlock_irqrestore(&priv->lock, flags);
  995. /* hangup, as defined in acm.c... this might be a bad place for it
  996. * though */
  997. if (tty && !(tty->termios.c_cflag & CLOCAL) &&
  998. !(priv->current_status & UART_CD)) {
  999. dev_dbg(dev, "%s - calling hangup\n", __func__);
  1000. tty_hangup(tty);
  1001. goto continue_read;
  1002. }
  1003. /* There is one error bit... I'm assuming it is a parity error
  1004. * indicator as the generic firmware will set this bit to 1 if a
  1005. * parity error occurs.
  1006. * I can not find reference to any other error events. */
  1007. spin_lock_irqsave(&priv->lock, flags);
  1008. if (priv->current_status & CYP_ERROR) {
  1009. spin_unlock_irqrestore(&priv->lock, flags);
  1010. tty_flag = TTY_PARITY;
  1011. dev_dbg(dev, "%s - Parity Error detected\n", __func__);
  1012. } else
  1013. spin_unlock_irqrestore(&priv->lock, flags);
  1014. /* process read if there is data other than line status */
  1015. if (bytes > i) {
  1016. tty_insert_flip_string_fixed_flag(&port->port, data + i,
  1017. tty_flag, bytes - i);
  1018. tty_flip_buffer_push(&port->port);
  1019. }
  1020. spin_lock_irqsave(&priv->lock, flags);
  1021. /* control and status byte(s) are also counted */
  1022. priv->bytes_in += bytes;
  1023. spin_unlock_irqrestore(&priv->lock, flags);
  1024. continue_read:
  1025. tty_kref_put(tty);
  1026. /* Continue trying to always read */
  1027. if (priv->comm_is_ok) {
  1028. usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
  1029. usb_rcvintpipe(port->serial->dev,
  1030. port->interrupt_in_endpointAddress),
  1031. port->interrupt_in_urb->transfer_buffer,
  1032. port->interrupt_in_urb->transfer_buffer_length,
  1033. cypress_read_int_callback, port,
  1034. priv->read_urb_interval);
  1035. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  1036. if (result && result != -EPERM) {
  1037. dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
  1038. __func__, result);
  1039. cypress_set_dead(port);
  1040. }
  1041. }
  1042. } /* cypress_read_int_callback */
  1043. static void cypress_write_int_callback(struct urb *urb)
  1044. {
  1045. struct usb_serial_port *port = urb->context;
  1046. struct cypress_private *priv = usb_get_serial_port_data(port);
  1047. struct device *dev = &urb->dev->dev;
  1048. int status = urb->status;
  1049. switch (status) {
  1050. case 0:
  1051. /* success */
  1052. break;
  1053. case -ECONNRESET:
  1054. case -ENOENT:
  1055. case -ESHUTDOWN:
  1056. /* this urb is terminated, clean up */
  1057. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  1058. __func__, status);
  1059. priv->write_urb_in_use = 0;
  1060. return;
  1061. case -EPIPE:
  1062. /* Cannot call usb_clear_halt while in_interrupt */
  1063. /* FALLTHROUGH */
  1064. default:
  1065. dev_err(dev, "%s - unexpected nonzero write status received: %d\n",
  1066. __func__, status);
  1067. cypress_set_dead(port);
  1068. break;
  1069. }
  1070. priv->write_urb_in_use = 0;
  1071. /* send any buffered data */
  1072. cypress_send(port);
  1073. }
  1074. module_usb_serial_driver(serial_drivers, id_table_combined);
  1075. MODULE_AUTHOR(DRIVER_AUTHOR);
  1076. MODULE_DESCRIPTION(DRIVER_DESC);
  1077. MODULE_LICENSE("GPL");
  1078. module_param(stats, bool, S_IRUGO | S_IWUSR);
  1079. MODULE_PARM_DESC(stats, "Enable statistics or not");
  1080. module_param(interval, int, S_IRUGO | S_IWUSR);
  1081. MODULE_PARM_DESC(interval, "Overrides interrupt interval");
  1082. module_param(unstable_bauds, bool, S_IRUGO | S_IWUSR);
  1083. MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");