cypress_m8.c 38 KB

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