cypress_m8.c 38 KB

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