cypress_m8.c 46 KB

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