cypress_m8.c 37 KB

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