cypress_m8.c 39 KB

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