cypress_m8.c 49 KB

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