cypress_m8.c 45 KB

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