cypress_m8.c 47 KB

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