cypress_m8.c 47 KB

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