cypress_m8.c 38 KB

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