cypress_m8.c 50 KB

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