cypress_m8.c 38 KB

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