cypress_m8.c 39 KB

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