cypress_m8.c 39 KB

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