cypress_m8.c 45 KB

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