cypress_m8.c 39 KB

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