mos7720.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. /*
  2. * mos7720.c
  3. * Controls the Moschip 7720 usb to dual port serial convertor
  4. *
  5. * Copyright 2006 Moschip Semiconductor Tech. Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2 of the License.
  10. *
  11. * Developed by:
  12. * Vijaya Kumar <vijaykumar.gn@gmail.com>
  13. * Ajay Kumar <naanuajay@yahoo.com>
  14. * Gurudeva <ngurudeva@yahoo.com>
  15. *
  16. * Cleaned up from the original by:
  17. * Greg Kroah-Hartman <gregkh@suse.de>
  18. *
  19. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_driver.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/module.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/serial.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include <linux/uaccess.h>
  37. /*
  38. * Version Information
  39. */
  40. #define DRIVER_VERSION "1.0.0.4F"
  41. #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  42. #define DRIVER_DESC "Moschip USB Serial Driver"
  43. /* default urb timeout */
  44. #define MOS_WDR_TIMEOUT (HZ * 5)
  45. #define MOS_PORT1 0x0200
  46. #define MOS_PORT2 0x0300
  47. #define MOS_VENREG 0x0000
  48. #define MOS_MAX_PORT 0x02
  49. #define MOS_WRITE 0x0E
  50. #define MOS_READ 0x0D
  51. /* Interrupt Rotinue Defines */
  52. #define SERIAL_IIR_RLS 0x06
  53. #define SERIAL_IIR_RDA 0x04
  54. #define SERIAL_IIR_CTI 0x0c
  55. #define SERIAL_IIR_THR 0x02
  56. #define SERIAL_IIR_MS 0x00
  57. #define NUM_URBS 16 /* URB Count */
  58. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  59. /* This structure holds all of the local port information */
  60. struct moschip_port {
  61. __u8 shadowLCR; /* last LCR value received */
  62. __u8 shadowMCR; /* last MCR value received */
  63. __u8 shadowMSR; /* last MSR value received */
  64. char open;
  65. struct async_icount icount;
  66. struct usb_serial_port *port; /* loop back to the owner */
  67. struct urb *write_urb_pool[NUM_URBS];
  68. };
  69. /* This structure holds all of the individual serial device information */
  70. struct moschip_serial {
  71. int interrupt_started;
  72. };
  73. static int debug;
  74. #define USB_VENDOR_ID_MOSCHIP 0x9710
  75. #define MOSCHIP_DEVICE_ID_7720 0x7720
  76. #define MOSCHIP_DEVICE_ID_7715 0x7715
  77. static struct usb_device_id moschip_port_id_table [] = {
  78. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
  79. { } /* terminating entry */
  80. };
  81. MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
  82. /*
  83. * mos7720_interrupt_callback
  84. * this is the callback function for when we have received data on the
  85. * interrupt endpoint.
  86. */
  87. static void mos7720_interrupt_callback(struct urb *urb)
  88. {
  89. int result;
  90. int length;
  91. int status = urb->status;
  92. __u8 *data;
  93. __u8 sp1;
  94. __u8 sp2;
  95. dbg("%s", " : Entering\n");
  96. switch (status) {
  97. case 0:
  98. /* success */
  99. break;
  100. case -ECONNRESET:
  101. case -ENOENT:
  102. case -ESHUTDOWN:
  103. /* this urb is terminated, clean up */
  104. dbg("%s - urb shutting down with status: %d", __func__,
  105. status);
  106. return;
  107. default:
  108. dbg("%s - nonzero urb status received: %d", __func__,
  109. status);
  110. goto exit;
  111. }
  112. length = urb->actual_length;
  113. data = urb->transfer_buffer;
  114. /* Moschip get 4 bytes
  115. * Byte 1 IIR Port 1 (port.number is 0)
  116. * Byte 2 IIR Port 2 (port.number is 1)
  117. * Byte 3 --------------
  118. * Byte 4 FIFO status for both */
  119. /* the above description is inverted
  120. * oneukum 2007-03-14 */
  121. if (unlikely(length != 4)) {
  122. dbg("Wrong data !!!");
  123. return;
  124. }
  125. sp1 = data[3];
  126. sp2 = data[2];
  127. if ((sp1 | sp2) & 0x01) {
  128. /* No Interrupt Pending in both the ports */
  129. dbg("No Interrupt !!!");
  130. } else {
  131. switch (sp1 & 0x0f) {
  132. case SERIAL_IIR_RLS:
  133. dbg("Serial Port 1: Receiver status error or address "
  134. "bit detected in 9-bit mode\n");
  135. break;
  136. case SERIAL_IIR_CTI:
  137. dbg("Serial Port 1: Receiver time out");
  138. break;
  139. case SERIAL_IIR_MS:
  140. dbg("Serial Port 1: Modem status change");
  141. break;
  142. }
  143. switch (sp2 & 0x0f) {
  144. case SERIAL_IIR_RLS:
  145. dbg("Serial Port 2: Receiver status error or address "
  146. "bit detected in 9-bit mode");
  147. break;
  148. case SERIAL_IIR_CTI:
  149. dbg("Serial Port 2: Receiver time out");
  150. break;
  151. case SERIAL_IIR_MS:
  152. dbg("Serial Port 2: Modem status change");
  153. break;
  154. }
  155. }
  156. exit:
  157. result = usb_submit_urb(urb, GFP_ATOMIC);
  158. if (result)
  159. dev_err(&urb->dev->dev,
  160. "%s - Error %d submitting control urb\n",
  161. __func__, result);
  162. return;
  163. }
  164. /*
  165. * mos7720_bulk_in_callback
  166. * this is the callback function for when we have received data on the
  167. * bulk in endpoint.
  168. */
  169. static void mos7720_bulk_in_callback(struct urb *urb)
  170. {
  171. int retval;
  172. unsigned char *data ;
  173. struct usb_serial_port *port;
  174. struct moschip_port *mos7720_port;
  175. struct tty_struct *tty;
  176. int status = urb->status;
  177. if (status) {
  178. dbg("nonzero read bulk status received: %d", status);
  179. return;
  180. }
  181. mos7720_port = urb->context;
  182. if (!mos7720_port) {
  183. dbg("%s", "NULL mos7720_port pointer \n");
  184. return ;
  185. }
  186. port = mos7720_port->port;
  187. dbg("Entering...%s", __func__);
  188. data = urb->transfer_buffer;
  189. tty = tty_port_tty_get(&port->port);
  190. if (tty && urb->actual_length) {
  191. tty_buffer_request_room(tty, urb->actual_length);
  192. tty_insert_flip_string(tty, data, urb->actual_length);
  193. tty_flip_buffer_push(tty);
  194. }
  195. tty_kref_put(tty);
  196. if (!port->read_urb) {
  197. dbg("URB KILLED !!!");
  198. return;
  199. }
  200. if (port->read_urb->status != -EINPROGRESS) {
  201. port->read_urb->dev = port->serial->dev;
  202. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  203. if (retval)
  204. dbg("usb_submit_urb(read bulk) failed, retval = %d",
  205. retval);
  206. }
  207. }
  208. /*
  209. * mos7720_bulk_out_data_callback
  210. * this is the callback function for when we have finished sending serial
  211. * data on the bulk out endpoint.
  212. */
  213. static void mos7720_bulk_out_data_callback(struct urb *urb)
  214. {
  215. struct moschip_port *mos7720_port;
  216. struct tty_struct *tty;
  217. int status = urb->status;
  218. if (status) {
  219. dbg("nonzero write bulk status received:%d", status);
  220. return;
  221. }
  222. mos7720_port = urb->context;
  223. if (!mos7720_port) {
  224. dbg("NULL mos7720_port pointer");
  225. return ;
  226. }
  227. dbg("Entering .........");
  228. tty = tty_port_tty_get(&mos7720_port->port->port);
  229. if (tty && mos7720_port->open)
  230. tty_wakeup(tty);
  231. tty_kref_put(tty);
  232. }
  233. /*
  234. * send_mos_cmd
  235. * this function will be used for sending command to device
  236. */
  237. static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
  238. __u16 index, void *data)
  239. {
  240. int status;
  241. unsigned int pipe;
  242. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  243. __u8 requesttype;
  244. __u16 size = 0x0000;
  245. if (value < MOS_MAX_PORT) {
  246. if (product == MOSCHIP_DEVICE_ID_7715)
  247. value = value*0x100+0x100;
  248. else
  249. value = value*0x100+0x200;
  250. } else {
  251. value = 0x0000;
  252. if ((product == MOSCHIP_DEVICE_ID_7715) &&
  253. (index != 0x08)) {
  254. dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
  255. /* index = 0x01 ; */
  256. }
  257. }
  258. if (request == MOS_WRITE) {
  259. request = (__u8)MOS_WRITE;
  260. requesttype = (__u8)0x40;
  261. value = value + (__u16)*((unsigned char *)data);
  262. data = NULL;
  263. pipe = usb_sndctrlpipe(serial->dev, 0);
  264. } else {
  265. request = (__u8)MOS_READ;
  266. requesttype = (__u8)0xC0;
  267. size = 0x01;
  268. pipe = usb_rcvctrlpipe(serial->dev, 0);
  269. }
  270. status = usb_control_msg(serial->dev, pipe, request, requesttype,
  271. value, index, data, size, MOS_WDR_TIMEOUT);
  272. if (status < 0)
  273. dbg("Command Write failed Value %x index %x\n", value, index);
  274. return status;
  275. }
  276. static int mos7720_open(struct tty_struct *tty,
  277. struct usb_serial_port *port, struct file *filp)
  278. {
  279. struct usb_serial *serial;
  280. struct usb_serial_port *port0;
  281. struct urb *urb;
  282. struct moschip_serial *mos7720_serial;
  283. struct moschip_port *mos7720_port;
  284. int response;
  285. int port_number;
  286. char data;
  287. int allocated_urbs = 0;
  288. int j;
  289. serial = port->serial;
  290. mos7720_port = usb_get_serial_port_data(port);
  291. if (mos7720_port == NULL)
  292. return -ENODEV;
  293. port0 = serial->port[0];
  294. mos7720_serial = usb_get_serial_data(serial);
  295. if (mos7720_serial == NULL || port0 == NULL)
  296. return -ENODEV;
  297. usb_clear_halt(serial->dev, port->write_urb->pipe);
  298. usb_clear_halt(serial->dev, port->read_urb->pipe);
  299. /* Initialising the write urb pool */
  300. for (j = 0; j < NUM_URBS; ++j) {
  301. urb = usb_alloc_urb(0, GFP_KERNEL);
  302. mos7720_port->write_urb_pool[j] = urb;
  303. if (urb == NULL) {
  304. dev_err(&port->dev, "No more urbs???\n");
  305. continue;
  306. }
  307. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  308. GFP_KERNEL);
  309. if (!urb->transfer_buffer) {
  310. dev_err(&port->dev,
  311. "%s-out of memory for urb buffers.\n",
  312. __func__);
  313. usb_free_urb(mos7720_port->write_urb_pool[j]);
  314. mos7720_port->write_urb_pool[j] = NULL;
  315. continue;
  316. }
  317. allocated_urbs++;
  318. }
  319. if (!allocated_urbs)
  320. return -ENOMEM;
  321. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  322. *
  323. * Register Index
  324. * 1 : IER
  325. * 2 : FCR
  326. * 3 : LCR
  327. * 4 : MCR
  328. *
  329. * 0x08 : SP1/2 Control Reg
  330. */
  331. port_number = port->number - port->serial->minor;
  332. send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
  333. dbg("SS::%p LSR:%x\n", mos7720_port, data);
  334. dbg("Check:Sending Command ..........");
  335. data = 0x02;
  336. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
  337. data = 0x02;
  338. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
  339. data = 0x00;
  340. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  341. data = 0x00;
  342. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  343. data = 0xCF;
  344. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  345. data = 0x03;
  346. mos7720_port->shadowLCR = data;
  347. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  348. data = 0x0b;
  349. mos7720_port->shadowMCR = data;
  350. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  351. data = 0x0b;
  352. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  353. data = 0x00;
  354. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  355. data = 0x00;
  356. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  357. /* data = 0x00;
  358. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
  359. data = 0x03;
  360. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  361. data = 0x00;
  362. send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT,
  363. port_number + 1, &data);
  364. */
  365. data = 0x00;
  366. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  367. data = data | (port->number - port->serial->minor + 1);
  368. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  369. data = 0x83;
  370. mos7720_port->shadowLCR = data;
  371. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  372. data = 0x0c;
  373. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  374. data = 0x00;
  375. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  376. data = 0x03;
  377. mos7720_port->shadowLCR = data;
  378. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  379. data = 0x0c;
  380. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  381. data = 0x0c;
  382. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  383. /* see if we've set up our endpoint info yet *
  384. * (can't set it up in mos7720_startup as the *
  385. * structures were not set up at that time.) */
  386. if (!mos7720_serial->interrupt_started) {
  387. dbg("Interrupt buffer NULL !!!");
  388. /* not set up yet, so do it now */
  389. mos7720_serial->interrupt_started = 1;
  390. dbg("To Submit URB !!!");
  391. /* set up our interrupt urb */
  392. usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
  393. usb_rcvintpipe(serial->dev,
  394. port->interrupt_in_endpointAddress),
  395. port0->interrupt_in_buffer,
  396. port0->interrupt_in_urb->transfer_buffer_length,
  397. mos7720_interrupt_callback, mos7720_port,
  398. port0->interrupt_in_urb->interval);
  399. /* start interrupt read for this mos7720 this interrupt *
  400. * will continue as long as the mos7720 is connected */
  401. dbg("Submit URB over !!!");
  402. response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
  403. if (response)
  404. dev_err(&port->dev,
  405. "%s - Error %d submitting control urb\n",
  406. __func__, response);
  407. }
  408. /* set up our bulk in urb */
  409. usb_fill_bulk_urb(port->read_urb, serial->dev,
  410. usb_rcvbulkpipe(serial->dev,
  411. port->bulk_in_endpointAddress),
  412. port->bulk_in_buffer,
  413. port->read_urb->transfer_buffer_length,
  414. mos7720_bulk_in_callback, mos7720_port);
  415. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  416. if (response)
  417. dev_err(&port->dev, "%s - Error %d submitting read urb\n",
  418. __func__, response);
  419. /* initialize our icount structure */
  420. memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
  421. /* initialize our port settings */
  422. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  423. /* send a open port command */
  424. mos7720_port->open = 1;
  425. return 0;
  426. }
  427. /*
  428. * mos7720_chars_in_buffer
  429. * this function is called by the tty driver when it wants to know how many
  430. * bytes of data we currently have outstanding in the port (data that has
  431. * been written, but hasn't made it out the port yet)
  432. * If successful, we return the number of bytes left to be written in the
  433. * system,
  434. * Otherwise we return a negative error number.
  435. */
  436. static int mos7720_chars_in_buffer(struct tty_struct *tty)
  437. {
  438. struct usb_serial_port *port = tty->driver_data;
  439. int i;
  440. int chars = 0;
  441. struct moschip_port *mos7720_port;
  442. dbg("%s:entering ...........", __func__);
  443. mos7720_port = usb_get_serial_port_data(port);
  444. if (mos7720_port == NULL) {
  445. dbg("%s:leaving ...........", __func__);
  446. return -ENODEV;
  447. }
  448. for (i = 0; i < NUM_URBS; ++i) {
  449. if (mos7720_port->write_urb_pool[i] &&
  450. mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  451. chars += URB_TRANSFER_BUFFER_SIZE;
  452. }
  453. dbg("%s - returns %d", __func__, chars);
  454. return chars;
  455. }
  456. static void mos7720_close(struct usb_serial_port *port)
  457. {
  458. struct usb_serial *serial;
  459. struct moschip_port *mos7720_port;
  460. char data;
  461. int j;
  462. dbg("mos7720_close:entering...");
  463. serial = port->serial;
  464. mos7720_port = usb_get_serial_port_data(port);
  465. if (mos7720_port == NULL)
  466. return;
  467. for (j = 0; j < NUM_URBS; ++j)
  468. usb_kill_urb(mos7720_port->write_urb_pool[j]);
  469. /* Freeing Write URBs */
  470. for (j = 0; j < NUM_URBS; ++j) {
  471. if (mos7720_port->write_urb_pool[j]) {
  472. kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
  473. usb_free_urb(mos7720_port->write_urb_pool[j]);
  474. }
  475. }
  476. /* While closing port, shutdown all bulk read, write *
  477. * and interrupt read if they exists, otherwise nop */
  478. dbg("Shutdown bulk write");
  479. usb_kill_urb(port->write_urb);
  480. dbg("Shutdown bulk read");
  481. usb_kill_urb(port->read_urb);
  482. mutex_lock(&serial->disc_mutex);
  483. /* these commands must not be issued if the device has
  484. * been disconnected */
  485. if (!serial->disconnected) {
  486. data = 0x00;
  487. send_mos_cmd(serial, MOS_WRITE,
  488. port->number - port->serial->minor, 0x04, &data);
  489. data = 0x00;
  490. send_mos_cmd(serial, MOS_WRITE,
  491. port->number - port->serial->minor, 0x01, &data);
  492. }
  493. mutex_unlock(&serial->disc_mutex);
  494. mos7720_port->open = 0;
  495. dbg("Leaving %s", __func__);
  496. }
  497. static void mos7720_break(struct tty_struct *tty, int break_state)
  498. {
  499. struct usb_serial_port *port = tty->driver_data;
  500. unsigned char data;
  501. struct usb_serial *serial;
  502. struct moschip_port *mos7720_port;
  503. dbg("Entering %s", __func__);
  504. serial = port->serial;
  505. mos7720_port = usb_get_serial_port_data(port);
  506. if (mos7720_port == NULL)
  507. return;
  508. if (break_state == -1)
  509. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  510. else
  511. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  512. mos7720_port->shadowLCR = data;
  513. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  514. 0x03, &data);
  515. return;
  516. }
  517. /*
  518. * mos7720_write_room
  519. * this function is called by the tty driver when it wants to know how many
  520. * bytes of data we can accept for a specific port.
  521. * If successful, we return the amount of room that we have for this port
  522. * Otherwise we return a negative error number.
  523. */
  524. static int mos7720_write_room(struct tty_struct *tty)
  525. {
  526. struct usb_serial_port *port = tty->driver_data;
  527. struct moschip_port *mos7720_port;
  528. int room = 0;
  529. int i;
  530. dbg("%s:entering ...........", __func__);
  531. mos7720_port = usb_get_serial_port_data(port);
  532. if (mos7720_port == NULL) {
  533. dbg("%s:leaving ...........", __func__);
  534. return -ENODEV;
  535. }
  536. /* FIXME: Locking */
  537. for (i = 0; i < NUM_URBS; ++i) {
  538. if (mos7720_port->write_urb_pool[i] &&
  539. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
  540. room += URB_TRANSFER_BUFFER_SIZE;
  541. }
  542. dbg("%s - returns %d", __func__, room);
  543. return room;
  544. }
  545. static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
  546. const unsigned char *data, int count)
  547. {
  548. int status;
  549. int i;
  550. int bytes_sent = 0;
  551. int transfer_size;
  552. struct moschip_port *mos7720_port;
  553. struct usb_serial *serial;
  554. struct urb *urb;
  555. const unsigned char *current_position = data;
  556. dbg("%s:entering ...........", __func__);
  557. serial = port->serial;
  558. mos7720_port = usb_get_serial_port_data(port);
  559. if (mos7720_port == NULL) {
  560. dbg("mos7720_port is NULL");
  561. return -ENODEV;
  562. }
  563. /* try to find a free urb in the list */
  564. urb = NULL;
  565. for (i = 0; i < NUM_URBS; ++i) {
  566. if (mos7720_port->write_urb_pool[i] &&
  567. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
  568. urb = mos7720_port->write_urb_pool[i];
  569. dbg("URB:%d", i);
  570. break;
  571. }
  572. }
  573. if (urb == NULL) {
  574. dbg("%s - no more free urbs", __func__);
  575. goto exit;
  576. }
  577. if (urb->transfer_buffer == NULL) {
  578. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  579. GFP_KERNEL);
  580. if (urb->transfer_buffer == NULL) {
  581. dev_err(&port->dev, "%s no more kernel memory...\n",
  582. __func__);
  583. goto exit;
  584. }
  585. }
  586. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  587. memcpy(urb->transfer_buffer, current_position, transfer_size);
  588. usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
  589. urb->transfer_buffer);
  590. /* fill urb with data and submit */
  591. usb_fill_bulk_urb(urb, serial->dev,
  592. usb_sndbulkpipe(serial->dev,
  593. port->bulk_out_endpointAddress),
  594. urb->transfer_buffer, transfer_size,
  595. mos7720_bulk_out_data_callback, mos7720_port);
  596. /* send it down the pipe */
  597. status = usb_submit_urb(urb, GFP_ATOMIC);
  598. if (status) {
  599. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
  600. "with status = %d\n", __func__, status);
  601. bytes_sent = status;
  602. goto exit;
  603. }
  604. bytes_sent = transfer_size;
  605. exit:
  606. return bytes_sent;
  607. }
  608. static void mos7720_throttle(struct tty_struct *tty)
  609. {
  610. struct usb_serial_port *port = tty->driver_data;
  611. struct moschip_port *mos7720_port;
  612. int status;
  613. dbg("%s- port %d\n", __func__, port->number);
  614. mos7720_port = usb_get_serial_port_data(port);
  615. if (mos7720_port == NULL)
  616. return;
  617. if (!mos7720_port->open) {
  618. dbg("port not opened");
  619. return;
  620. }
  621. dbg("%s: Entering ..........", __func__);
  622. /* if we are implementing XON/XOFF, send the stop character */
  623. if (I_IXOFF(tty)) {
  624. unsigned char stop_char = STOP_CHAR(tty);
  625. status = mos7720_write(tty, port, &stop_char, 1);
  626. if (status <= 0)
  627. return;
  628. }
  629. /* if we are implementing RTS/CTS, toggle that line */
  630. if (tty->termios->c_cflag & CRTSCTS) {
  631. mos7720_port->shadowMCR &= ~UART_MCR_RTS;
  632. status = send_mos_cmd(port->serial, MOS_WRITE,
  633. port->number - port->serial->minor,
  634. UART_MCR, &mos7720_port->shadowMCR);
  635. if (status != 0)
  636. return;
  637. }
  638. }
  639. static void mos7720_unthrottle(struct tty_struct *tty)
  640. {
  641. struct usb_serial_port *port = tty->driver_data;
  642. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  643. int status;
  644. if (mos7720_port == NULL)
  645. return;
  646. if (!mos7720_port->open) {
  647. dbg("%s - port not opened", __func__);
  648. return;
  649. }
  650. dbg("%s: Entering ..........", __func__);
  651. /* if we are implementing XON/XOFF, send the start character */
  652. if (I_IXOFF(tty)) {
  653. unsigned char start_char = START_CHAR(tty);
  654. status = mos7720_write(tty, port, &start_char, 1);
  655. if (status <= 0)
  656. return;
  657. }
  658. /* if we are implementing RTS/CTS, toggle that line */
  659. if (tty->termios->c_cflag & CRTSCTS) {
  660. mos7720_port->shadowMCR |= UART_MCR_RTS;
  661. status = send_mos_cmd(port->serial, MOS_WRITE,
  662. port->number - port->serial->minor,
  663. UART_MCR, &mos7720_port->shadowMCR);
  664. if (status != 0)
  665. return;
  666. }
  667. }
  668. static int set_higher_rates(struct moschip_port *mos7720_port,
  669. unsigned int baud)
  670. {
  671. unsigned char data;
  672. struct usb_serial_port *port;
  673. struct usb_serial *serial;
  674. int port_number;
  675. if (mos7720_port == NULL)
  676. return -EINVAL;
  677. port = mos7720_port->port;
  678. serial = port->serial;
  679. /***********************************************
  680. * Init Sequence for higher rates
  681. ***********************************************/
  682. dbg("Sending Setting Commands ..........");
  683. port_number = port->number - port->serial->minor;
  684. data = 0x000;
  685. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  686. data = 0x000;
  687. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  688. data = 0x0CF;
  689. send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
  690. data = 0x00b;
  691. mos7720_port->shadowMCR = data;
  692. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  693. data = 0x00b;
  694. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  695. data = 0x000;
  696. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  697. data = 0x000;
  698. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  699. /***********************************************
  700. * Set for higher rates *
  701. ***********************************************/
  702. data = baud * 0x10;
  703. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  704. data = 0x003;
  705. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  706. data = 0x003;
  707. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  708. data = 0x02b;
  709. mos7720_port->shadowMCR = data;
  710. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  711. data = 0x02b;
  712. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  713. /***********************************************
  714. * Set DLL/DLM
  715. ***********************************************/
  716. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  717. mos7720_port->shadowLCR = data;
  718. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  719. data = 0x001; /* DLL */
  720. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  721. data = 0x000; /* DLM */
  722. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  723. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  724. mos7720_port->shadowLCR = data;
  725. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  726. return 0;
  727. }
  728. /* baud rate information */
  729. struct divisor_table_entry {
  730. __u32 baudrate;
  731. __u16 divisor;
  732. };
  733. /* Define table of divisors for moschip 7720 hardware *
  734. * These assume a 3.6864MHz crystal, the standard /16, and *
  735. * MCR.7 = 0. */
  736. static struct divisor_table_entry divisor_table[] = {
  737. { 50, 2304},
  738. { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
  739. { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
  740. { 150, 768},
  741. { 300, 384},
  742. { 600, 192},
  743. { 1200, 96},
  744. { 1800, 64},
  745. { 2400, 48},
  746. { 4800, 24},
  747. { 7200, 16},
  748. { 9600, 12},
  749. { 19200, 6},
  750. { 38400, 3},
  751. { 57600, 2},
  752. { 115200, 1},
  753. };
  754. /*****************************************************************************
  755. * calc_baud_rate_divisor
  756. * this function calculates the proper baud rate divisor for the specified
  757. * baud rate.
  758. *****************************************************************************/
  759. static int calc_baud_rate_divisor(int baudrate, int *divisor)
  760. {
  761. int i;
  762. __u16 custom;
  763. __u16 round1;
  764. __u16 round;
  765. dbg("%s - %d", __func__, baudrate);
  766. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  767. if (divisor_table[i].baudrate == baudrate) {
  768. *divisor = divisor_table[i].divisor;
  769. return 0;
  770. }
  771. }
  772. /* After trying for all the standard baud rates *
  773. * Try calculating the divisor for this baud rate */
  774. if (baudrate > 75 && baudrate < 230400) {
  775. /* get the divisor */
  776. custom = (__u16)(230400L / baudrate);
  777. /* Check for round off */
  778. round1 = (__u16)(2304000L / baudrate);
  779. round = (__u16)(round1 - (custom * 10));
  780. if (round > 4)
  781. custom++;
  782. *divisor = custom;
  783. dbg("Baud %d = %d", baudrate, custom);
  784. return 0;
  785. }
  786. dbg("Baud calculation Failed...");
  787. return -EINVAL;
  788. }
  789. /*
  790. * send_cmd_write_baud_rate
  791. * this function sends the proper command to change the baud rate of the
  792. * specified port.
  793. */
  794. static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
  795. int baudrate)
  796. {
  797. struct usb_serial_port *port;
  798. struct usb_serial *serial;
  799. int divisor;
  800. int status;
  801. unsigned char data;
  802. unsigned char number;
  803. if (mos7720_port == NULL)
  804. return -1;
  805. port = mos7720_port->port;
  806. serial = port->serial;
  807. dbg("%s: Entering ..........", __func__);
  808. number = port->number - port->serial->minor;
  809. dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
  810. /* Calculate the Divisor */
  811. status = calc_baud_rate_divisor(baudrate, &divisor);
  812. if (status) {
  813. dev_err(&port->dev, "%s - bad baud rate\n", __func__);
  814. return status;
  815. }
  816. /* Enable access to divisor latch */
  817. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  818. mos7720_port->shadowLCR = data;
  819. send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
  820. /* Write the divisor */
  821. data = ((unsigned char)(divisor & 0xff));
  822. send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
  823. data = ((unsigned char)((divisor & 0xff00) >> 8));
  824. send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
  825. /* Disable access to divisor latch */
  826. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  827. mos7720_port->shadowLCR = data;
  828. send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
  829. return status;
  830. }
  831. /*
  832. * change_port_settings
  833. * This routine is called to set the UART on the device to match
  834. * the specified new settings.
  835. */
  836. static void change_port_settings(struct tty_struct *tty,
  837. struct moschip_port *mos7720_port,
  838. struct ktermios *old_termios)
  839. {
  840. struct usb_serial_port *port;
  841. struct usb_serial *serial;
  842. int baud;
  843. unsigned cflag;
  844. unsigned iflag;
  845. __u8 mask = 0xff;
  846. __u8 lData;
  847. __u8 lParity;
  848. __u8 lStop;
  849. int status;
  850. int port_number;
  851. char data;
  852. if (mos7720_port == NULL)
  853. return ;
  854. port = mos7720_port->port;
  855. serial = port->serial;
  856. port_number = port->number - port->serial->minor;
  857. dbg("%s - port %d", __func__, port->number);
  858. if (!mos7720_port->open) {
  859. dbg("%s - port not opened", __func__);
  860. return;
  861. }
  862. dbg("%s: Entering ..........", __func__);
  863. lData = UART_LCR_WLEN8;
  864. lStop = 0x00; /* 1 stop bit */
  865. lParity = 0x00; /* No parity */
  866. cflag = tty->termios->c_cflag;
  867. iflag = tty->termios->c_iflag;
  868. /* Change the number of bits */
  869. switch (cflag & CSIZE) {
  870. case CS5:
  871. lData = UART_LCR_WLEN5;
  872. mask = 0x1f;
  873. break;
  874. case CS6:
  875. lData = UART_LCR_WLEN6;
  876. mask = 0x3f;
  877. break;
  878. case CS7:
  879. lData = UART_LCR_WLEN7;
  880. mask = 0x7f;
  881. break;
  882. default:
  883. case CS8:
  884. lData = UART_LCR_WLEN8;
  885. break;
  886. }
  887. /* Change the Parity bit */
  888. if (cflag & PARENB) {
  889. if (cflag & PARODD) {
  890. lParity = UART_LCR_PARITY;
  891. dbg("%s - parity = odd", __func__);
  892. } else {
  893. lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
  894. dbg("%s - parity = even", __func__);
  895. }
  896. } else {
  897. dbg("%s - parity = none", __func__);
  898. }
  899. if (cflag & CMSPAR)
  900. lParity = lParity | 0x20;
  901. /* Change the Stop bit */
  902. if (cflag & CSTOPB) {
  903. lStop = UART_LCR_STOP;
  904. dbg("%s - stop bits = 2", __func__);
  905. } else {
  906. lStop = 0x00;
  907. dbg("%s - stop bits = 1", __func__);
  908. }
  909. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  910. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  911. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  912. /* Update the LCR with the correct value */
  913. mos7720_port->shadowLCR &=
  914. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  915. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  916. /* Disable Interrupts */
  917. data = 0x00;
  918. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  919. UART_IER, &data);
  920. data = 0x00;
  921. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  922. data = 0xcf;
  923. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  924. /* Send the updated LCR value to the mos7720 */
  925. data = mos7720_port->shadowLCR;
  926. send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
  927. data = 0x00b;
  928. mos7720_port->shadowMCR = data;
  929. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  930. data = 0x00b;
  931. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  932. /* set up the MCR register and send it to the mos7720 */
  933. mos7720_port->shadowMCR = UART_MCR_OUT2;
  934. if (cflag & CBAUD)
  935. mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
  936. if (cflag & CRTSCTS) {
  937. mos7720_port->shadowMCR |= (UART_MCR_XONANY);
  938. /* To set hardware flow control to the specified *
  939. * serial port, in SP1/2_CONTROL_REG */
  940. if (port->number) {
  941. data = 0x001;
  942. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  943. 0x08, &data);
  944. } else {
  945. data = 0x002;
  946. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  947. 0x08, &data);
  948. }
  949. } else {
  950. mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
  951. }
  952. data = mos7720_port->shadowMCR;
  953. send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
  954. /* Determine divisor based on baud rate */
  955. baud = tty_get_baud_rate(tty);
  956. if (!baud) {
  957. /* pick a default, any default... */
  958. dbg("Picked default baud...");
  959. baud = 9600;
  960. }
  961. if (baud >= 230400) {
  962. set_higher_rates(mos7720_port, baud);
  963. /* Enable Interrupts */
  964. data = 0x0c;
  965. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  966. return;
  967. }
  968. dbg("%s - baud rate = %d", __func__, baud);
  969. status = send_cmd_write_baud_rate(mos7720_port, baud);
  970. /* FIXME: needs to write actual resulting baud back not just
  971. blindly do so */
  972. if (cflag & CBAUD)
  973. tty_encode_baud_rate(tty, baud, baud);
  974. /* Enable Interrupts */
  975. data = 0x0c;
  976. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  977. if (port->read_urb->status != -EINPROGRESS) {
  978. port->read_urb->dev = serial->dev;
  979. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  980. if (status)
  981. dbg("usb_submit_urb(read bulk) failed, status = %d",
  982. status);
  983. }
  984. return;
  985. }
  986. /*
  987. * mos7720_set_termios
  988. * this function is called by the tty driver when it wants to change the
  989. * termios structure.
  990. */
  991. static void mos7720_set_termios(struct tty_struct *tty,
  992. struct usb_serial_port *port, struct ktermios *old_termios)
  993. {
  994. int status;
  995. unsigned int cflag;
  996. struct usb_serial *serial;
  997. struct moschip_port *mos7720_port;
  998. serial = port->serial;
  999. mos7720_port = usb_get_serial_port_data(port);
  1000. if (mos7720_port == NULL)
  1001. return;
  1002. if (!mos7720_port->open) {
  1003. dbg("%s - port not opened", __func__);
  1004. return;
  1005. }
  1006. dbg("%s\n", "setting termios - ASPIRE");
  1007. cflag = tty->termios->c_cflag;
  1008. dbg("%s - cflag %08x iflag %08x", __func__,
  1009. tty->termios->c_cflag,
  1010. RELEVANT_IFLAG(tty->termios->c_iflag));
  1011. dbg("%s - old cflag %08x old iflag %08x", __func__,
  1012. old_termios->c_cflag,
  1013. RELEVANT_IFLAG(old_termios->c_iflag));
  1014. dbg("%s - port %d", __func__, port->number);
  1015. /* change the port settings to the new ones specified */
  1016. change_port_settings(tty, mos7720_port, old_termios);
  1017. if (!port->read_urb) {
  1018. dbg("%s", "URB KILLED !!!!!\n");
  1019. return;
  1020. }
  1021. if (port->read_urb->status != -EINPROGRESS) {
  1022. port->read_urb->dev = serial->dev;
  1023. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1024. if (status)
  1025. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1026. status);
  1027. }
  1028. return;
  1029. }
  1030. /*
  1031. * get_lsr_info - get line status register info
  1032. *
  1033. * Purpose: Let user call ioctl() to get info when the UART physically
  1034. * is emptied. On bus types like RS485, the transmitter must
  1035. * release the bus after transmitting. This must be done when
  1036. * the transmit shift register is empty, not be done when the
  1037. * transmit holding register is empty. This functionality
  1038. * allows an RS485 driver to be written in user space.
  1039. */
  1040. static int get_lsr_info(struct tty_struct *tty,
  1041. struct moschip_port *mos7720_port, unsigned int __user *value)
  1042. {
  1043. int count;
  1044. unsigned int result = 0;
  1045. count = mos7720_chars_in_buffer(tty);
  1046. if (count == 0) {
  1047. dbg("%s -- Empty", __func__);
  1048. result = TIOCSER_TEMT;
  1049. }
  1050. if (copy_to_user(value, &result, sizeof(int)))
  1051. return -EFAULT;
  1052. return 0;
  1053. }
  1054. static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
  1055. unsigned int __user *value)
  1056. {
  1057. unsigned int mcr ;
  1058. unsigned int arg;
  1059. unsigned char data;
  1060. struct usb_serial_port *port;
  1061. if (mos7720_port == NULL)
  1062. return -1;
  1063. port = (struct usb_serial_port *)mos7720_port->port;
  1064. mcr = mos7720_port->shadowMCR;
  1065. if (copy_from_user(&arg, value, sizeof(int)))
  1066. return -EFAULT;
  1067. switch (cmd) {
  1068. case TIOCMBIS:
  1069. if (arg & TIOCM_RTS)
  1070. mcr |= UART_MCR_RTS;
  1071. if (arg & TIOCM_DTR)
  1072. mcr |= UART_MCR_RTS;
  1073. if (arg & TIOCM_LOOP)
  1074. mcr |= UART_MCR_LOOP;
  1075. break;
  1076. case TIOCMBIC:
  1077. if (arg & TIOCM_RTS)
  1078. mcr &= ~UART_MCR_RTS;
  1079. if (arg & TIOCM_DTR)
  1080. mcr &= ~UART_MCR_RTS;
  1081. if (arg & TIOCM_LOOP)
  1082. mcr &= ~UART_MCR_LOOP;
  1083. break;
  1084. case TIOCMSET:
  1085. /* turn off the RTS and DTR and LOOPBACK
  1086. * and then only turn on what was asked to */
  1087. mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
  1088. mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
  1089. mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
  1090. mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
  1091. break;
  1092. }
  1093. mos7720_port->shadowMCR = mcr;
  1094. data = mos7720_port->shadowMCR;
  1095. send_mos_cmd(port->serial, MOS_WRITE,
  1096. port->number - port->serial->minor, UART_MCR, &data);
  1097. return 0;
  1098. }
  1099. static int get_modem_info(struct moschip_port *mos7720_port,
  1100. unsigned int __user *value)
  1101. {
  1102. unsigned int result = 0;
  1103. unsigned int msr = mos7720_port->shadowMSR;
  1104. unsigned int mcr = mos7720_port->shadowMCR;
  1105. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  1106. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  1107. | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  1108. | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */
  1109. | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  1110. | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  1111. dbg("%s -- %x", __func__, result);
  1112. if (copy_to_user(value, &result, sizeof(int)))
  1113. return -EFAULT;
  1114. return 0;
  1115. }
  1116. static int get_serial_info(struct moschip_port *mos7720_port,
  1117. struct serial_struct __user *retinfo)
  1118. {
  1119. struct serial_struct tmp;
  1120. if (!retinfo)
  1121. return -EFAULT;
  1122. memset(&tmp, 0, sizeof(tmp));
  1123. tmp.type = PORT_16550A;
  1124. tmp.line = mos7720_port->port->serial->minor;
  1125. tmp.port = mos7720_port->port->number;
  1126. tmp.irq = 0;
  1127. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1128. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1129. tmp.baud_base = 9600;
  1130. tmp.close_delay = 5*HZ;
  1131. tmp.closing_wait = 30*HZ;
  1132. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1133. return -EFAULT;
  1134. return 0;
  1135. }
  1136. static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
  1137. unsigned int cmd, unsigned long arg)
  1138. {
  1139. struct usb_serial_port *port = tty->driver_data;
  1140. struct moschip_port *mos7720_port;
  1141. struct async_icount cnow;
  1142. struct async_icount cprev;
  1143. struct serial_icounter_struct icount;
  1144. mos7720_port = usb_get_serial_port_data(port);
  1145. if (mos7720_port == NULL)
  1146. return -ENODEV;
  1147. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  1148. switch (cmd) {
  1149. case TIOCSERGETLSR:
  1150. dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
  1151. return get_lsr_info(tty, mos7720_port,
  1152. (unsigned int __user *)arg);
  1153. return 0;
  1154. /* FIXME: These should be using the mode methods */
  1155. case TIOCMBIS:
  1156. case TIOCMBIC:
  1157. case TIOCMSET:
  1158. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
  1159. __func__, port->number);
  1160. return set_modem_info(mos7720_port, cmd,
  1161. (unsigned int __user *)arg);
  1162. case TIOCMGET:
  1163. dbg("%s (%d) TIOCMGET", __func__, port->number);
  1164. return get_modem_info(mos7720_port,
  1165. (unsigned int __user *)arg);
  1166. case TIOCGSERIAL:
  1167. dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
  1168. return get_serial_info(mos7720_port,
  1169. (struct serial_struct __user *)arg);
  1170. case TIOCMIWAIT:
  1171. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  1172. cprev = mos7720_port->icount;
  1173. while (1) {
  1174. if (signal_pending(current))
  1175. return -ERESTARTSYS;
  1176. cnow = mos7720_port->icount;
  1177. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1178. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1179. return -EIO; /* no change => error */
  1180. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1181. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1182. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1183. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1184. return 0;
  1185. }
  1186. cprev = cnow;
  1187. }
  1188. /* NOTREACHED */
  1189. break;
  1190. case TIOCGICOUNT:
  1191. cnow = mos7720_port->icount;
  1192. icount.cts = cnow.cts;
  1193. icount.dsr = cnow.dsr;
  1194. icount.rng = cnow.rng;
  1195. icount.dcd = cnow.dcd;
  1196. icount.rx = cnow.rx;
  1197. icount.tx = cnow.tx;
  1198. icount.frame = cnow.frame;
  1199. icount.overrun = cnow.overrun;
  1200. icount.parity = cnow.parity;
  1201. icount.brk = cnow.brk;
  1202. icount.buf_overrun = cnow.buf_overrun;
  1203. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
  1204. port->number, icount.rx, icount.tx);
  1205. if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
  1206. return -EFAULT;
  1207. return 0;
  1208. }
  1209. return -ENOIOCTLCMD;
  1210. }
  1211. static int mos7720_startup(struct usb_serial *serial)
  1212. {
  1213. struct moschip_serial *mos7720_serial;
  1214. struct moschip_port *mos7720_port;
  1215. struct usb_device *dev;
  1216. int i;
  1217. char data;
  1218. dbg("%s: Entering ..........", __func__);
  1219. if (!serial) {
  1220. dbg("Invalid Handler");
  1221. return -ENODEV;
  1222. }
  1223. dev = serial->dev;
  1224. /* create our private serial structure */
  1225. mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
  1226. if (mos7720_serial == NULL) {
  1227. dev_err(&dev->dev, "%s - Out of memory\n", __func__);
  1228. return -ENOMEM;
  1229. }
  1230. usb_set_serial_data(serial, mos7720_serial);
  1231. /* we set up the pointers to the endpoints in the mos7720_open *
  1232. * function, as the structures aren't created yet. */
  1233. /* set up port private structures */
  1234. for (i = 0; i < serial->num_ports; ++i) {
  1235. mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1236. if (mos7720_port == NULL) {
  1237. dev_err(&dev->dev, "%s - Out of memory\n", __func__);
  1238. usb_set_serial_data(serial, NULL);
  1239. kfree(mos7720_serial);
  1240. return -ENOMEM;
  1241. }
  1242. /* Initialize all port interrupt end point to port 0 int
  1243. * endpoint. Our device has only one interrupt endpoint
  1244. * comman to all ports */
  1245. serial->port[i]->interrupt_in_endpointAddress =
  1246. serial->port[0]->interrupt_in_endpointAddress;
  1247. mos7720_port->port = serial->port[i];
  1248. usb_set_serial_port_data(serial->port[i], mos7720_port);
  1249. dbg("port number is %d", serial->port[i]->number);
  1250. dbg("serial number is %d", serial->minor);
  1251. }
  1252. /* setting configuration feature to one */
  1253. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1254. (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
  1255. /* LSR For Port 1 */
  1256. send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data);
  1257. dbg("LSR:%x", data);
  1258. /* LSR For Port 2 */
  1259. send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data);
  1260. dbg("LSR:%x", data);
  1261. return 0;
  1262. }
  1263. static void mos7720_shutdown(struct usb_serial *serial)
  1264. {
  1265. int i;
  1266. /* free private structure allocated for serial port */
  1267. for (i = 0; i < serial->num_ports; ++i) {
  1268. kfree(usb_get_serial_port_data(serial->port[i]));
  1269. usb_set_serial_port_data(serial->port[i], NULL);
  1270. }
  1271. /* free private structure allocated for serial device */
  1272. kfree(usb_get_serial_data(serial));
  1273. usb_set_serial_data(serial, NULL);
  1274. }
  1275. static struct usb_driver usb_driver = {
  1276. .name = "moschip7720",
  1277. .probe = usb_serial_probe,
  1278. .disconnect = usb_serial_disconnect,
  1279. .id_table = moschip_port_id_table,
  1280. .no_dynamic_id = 1,
  1281. };
  1282. static struct usb_serial_driver moschip7720_2port_driver = {
  1283. .driver = {
  1284. .owner = THIS_MODULE,
  1285. .name = "moschip7720",
  1286. },
  1287. .description = "Moschip 2 port adapter",
  1288. .usb_driver = &usb_driver,
  1289. .id_table = moschip_port_id_table,
  1290. .num_ports = 2,
  1291. .open = mos7720_open,
  1292. .close = mos7720_close,
  1293. .throttle = mos7720_throttle,
  1294. .unthrottle = mos7720_unthrottle,
  1295. .attach = mos7720_startup,
  1296. .shutdown = mos7720_shutdown,
  1297. .ioctl = mos7720_ioctl,
  1298. .set_termios = mos7720_set_termios,
  1299. .write = mos7720_write,
  1300. .write_room = mos7720_write_room,
  1301. .chars_in_buffer = mos7720_chars_in_buffer,
  1302. .break_ctl = mos7720_break,
  1303. .read_bulk_callback = mos7720_bulk_in_callback,
  1304. .read_int_callback = mos7720_interrupt_callback,
  1305. };
  1306. static int __init moschip7720_init(void)
  1307. {
  1308. int retval;
  1309. dbg("%s: Entering ..........", __func__);
  1310. /* Register with the usb serial */
  1311. retval = usb_serial_register(&moschip7720_2port_driver);
  1312. if (retval)
  1313. goto failed_port_device_register;
  1314. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1315. DRIVER_DESC "\n");
  1316. /* Register with the usb */
  1317. retval = usb_register(&usb_driver);
  1318. if (retval)
  1319. goto failed_usb_register;
  1320. return 0;
  1321. failed_usb_register:
  1322. usb_serial_deregister(&moschip7720_2port_driver);
  1323. failed_port_device_register:
  1324. return retval;
  1325. }
  1326. static void __exit moschip7720_exit(void)
  1327. {
  1328. usb_deregister(&usb_driver);
  1329. usb_serial_deregister(&moschip7720_2port_driver);
  1330. }
  1331. module_init(moschip7720_init);
  1332. module_exit(moschip7720_exit);
  1333. /* Module information */
  1334. MODULE_AUTHOR(DRIVER_AUTHOR);
  1335. MODULE_DESCRIPTION(DRIVER_DESC);
  1336. MODULE_LICENSE("GPL");
  1337. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1338. MODULE_PARM_DESC(debug, "Debug enabled or not");