cypress_m8.c 46 KB

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