cypress_m8.c 37 KB

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