cypress_m8.c 42 KB

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