cypress_m8.c 42 KB

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