cypress_m8.c 48 KB

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