cypress_m8.c 47 KB

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