cypress_m8.c 40 KB

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