cypress_m8.c 45 KB

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