cypress_m8.c 37 KB

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