cypress_m8.c 39 KB

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