cypress_m8.c 38 KB

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