visor.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. /*
  2. * USB HandSpring Visor, Palm m50x, and Sony Clie driver
  3. * (supports all of the Palm OS USB devices)
  4. *
  5. * Copyright (C) 1999 - 2004
  6. * Greg Kroah-Hartman (greg@kroah.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License.
  11. *
  12. * See Documentation/usb/usb-serial.txt for more information on using this driver
  13. *
  14. */
  15. #include <linux/config.h>
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_driver.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/spinlock.h>
  26. #include <asm/uaccess.h>
  27. #include <linux/usb.h>
  28. #include "usb-serial.h"
  29. #include "visor.h"
  30. /*
  31. * Version Information
  32. */
  33. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  34. #define DRIVER_DESC "USB HandSpring Visor / Palm OS driver"
  35. /* function prototypes for a handspring visor */
  36. static int visor_open (struct usb_serial_port *port, struct file *filp);
  37. static void visor_close (struct usb_serial_port *port, struct file *filp);
  38. static int visor_write (struct usb_serial_port *port, const unsigned char *buf, int count);
  39. static int visor_write_room (struct usb_serial_port *port);
  40. static int visor_chars_in_buffer (struct usb_serial_port *port);
  41. static void visor_throttle (struct usb_serial_port *port);
  42. static void visor_unthrottle (struct usb_serial_port *port);
  43. static int visor_probe (struct usb_serial *serial, const struct usb_device_id *id);
  44. static int visor_calc_num_ports(struct usb_serial *serial);
  45. static void visor_shutdown (struct usb_serial *serial);
  46. static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
  47. static void visor_set_termios (struct usb_serial_port *port, struct termios *old_termios);
  48. static void visor_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
  49. static void visor_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
  50. static void visor_read_int_callback (struct urb *urb, struct pt_regs *regs);
  51. static int clie_3_5_startup (struct usb_serial *serial);
  52. static int treo_attach (struct usb_serial *serial);
  53. static int clie_5_attach (struct usb_serial *serial);
  54. static int palm_os_3_probe (struct usb_serial *serial, const struct usb_device_id *id);
  55. static int palm_os_4_probe (struct usb_serial *serial, const struct usb_device_id *id);
  56. /* Parameters that may be passed into the module. */
  57. static int debug;
  58. static __u16 vendor;
  59. static __u16 product;
  60. static struct usb_device_id id_table [] = {
  61. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID),
  62. .driver_info = (kernel_ulong_t)&palm_os_3_probe },
  63. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID),
  64. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  65. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID),
  66. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  67. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID),
  68. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  69. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID),
  70. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  71. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID),
  72. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  73. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID),
  74. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  75. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID),
  76. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  77. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID),
  78. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  79. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID),
  80. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  81. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID),
  82. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  83. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650),
  84. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  85. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID),
  86. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  87. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE31_ID),
  88. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  89. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID),
  90. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  91. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID),
  92. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  93. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID),
  94. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  95. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID),
  96. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  97. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID),
  98. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  99. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID),
  100. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  101. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID),
  102. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  103. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID),
  104. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  105. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID),
  106. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  107. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID),
  108. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  109. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID),
  110. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  111. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID),
  112. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  113. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID),
  114. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  115. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID),
  116. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  117. { }, /* optional parameter entry */
  118. { } /* Terminating entry */
  119. };
  120. static struct usb_device_id clie_id_5_table [] = {
  121. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID),
  122. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  123. { }, /* optional parameter entry */
  124. { } /* Terminating entry */
  125. };
  126. static struct usb_device_id clie_id_3_5_table [] = {
  127. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  128. { } /* Terminating entry */
  129. };
  130. static struct usb_device_id id_table_combined [] = {
  131. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
  132. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
  133. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
  134. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
  135. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
  136. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
  137. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
  138. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
  139. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
  140. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
  141. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
  142. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650) },
  143. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
  144. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE31_ID) },
  145. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
  146. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  147. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
  148. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
  149. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
  150. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
  151. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
  152. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
  153. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
  154. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
  155. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID) },
  156. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID) },
  157. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
  158. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
  159. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID) },
  160. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID) },
  161. { }, /* optional parameter entry */
  162. { } /* Terminating entry */
  163. };
  164. MODULE_DEVICE_TABLE (usb, id_table_combined);
  165. static struct usb_driver visor_driver = {
  166. .owner = THIS_MODULE,
  167. .name = "visor",
  168. .probe = usb_serial_probe,
  169. .disconnect = usb_serial_disconnect,
  170. .id_table = id_table_combined,
  171. };
  172. /* All of the device info needed for the Handspring Visor, and Palm 4.0 devices */
  173. static struct usb_serial_driver handspring_device = {
  174. .driver = {
  175. .owner = THIS_MODULE,
  176. .name = "visor",
  177. },
  178. .description = "Handspring Visor / Palm OS",
  179. .id_table = id_table,
  180. .num_interrupt_in = NUM_DONT_CARE,
  181. .num_bulk_in = 2,
  182. .num_bulk_out = 2,
  183. .num_ports = 2,
  184. .open = visor_open,
  185. .close = visor_close,
  186. .throttle = visor_throttle,
  187. .unthrottle = visor_unthrottle,
  188. .attach = treo_attach,
  189. .probe = visor_probe,
  190. .calc_num_ports = visor_calc_num_ports,
  191. .shutdown = visor_shutdown,
  192. .ioctl = visor_ioctl,
  193. .set_termios = visor_set_termios,
  194. .write = visor_write,
  195. .write_room = visor_write_room,
  196. .chars_in_buffer = visor_chars_in_buffer,
  197. .write_bulk_callback = visor_write_bulk_callback,
  198. .read_bulk_callback = visor_read_bulk_callback,
  199. .read_int_callback = visor_read_int_callback,
  200. };
  201. /* All of the device info needed for the Clie UX50, TH55 Palm 5.0 devices */
  202. static struct usb_serial_driver clie_5_device = {
  203. .driver = {
  204. .owner = THIS_MODULE,
  205. .name = "clie_5",
  206. },
  207. .description = "Sony Clie 5.0",
  208. .id_table = clie_id_5_table,
  209. .num_interrupt_in = NUM_DONT_CARE,
  210. .num_bulk_in = 2,
  211. .num_bulk_out = 2,
  212. .num_ports = 2,
  213. .open = visor_open,
  214. .close = visor_close,
  215. .throttle = visor_throttle,
  216. .unthrottle = visor_unthrottle,
  217. .attach = clie_5_attach,
  218. .probe = visor_probe,
  219. .calc_num_ports = visor_calc_num_ports,
  220. .shutdown = visor_shutdown,
  221. .ioctl = visor_ioctl,
  222. .set_termios = visor_set_termios,
  223. .write = visor_write,
  224. .write_room = visor_write_room,
  225. .chars_in_buffer = visor_chars_in_buffer,
  226. .write_bulk_callback = visor_write_bulk_callback,
  227. .read_bulk_callback = visor_read_bulk_callback,
  228. .read_int_callback = visor_read_int_callback,
  229. };
  230. /* device info for the Sony Clie OS version 3.5 */
  231. static struct usb_serial_driver clie_3_5_device = {
  232. .driver = {
  233. .owner = THIS_MODULE,
  234. .name = "clie_3.5",
  235. },
  236. .description = "Sony Clie 3.5",
  237. .id_table = clie_id_3_5_table,
  238. .num_interrupt_in = 0,
  239. .num_bulk_in = 1,
  240. .num_bulk_out = 1,
  241. .num_ports = 1,
  242. .open = visor_open,
  243. .close = visor_close,
  244. .throttle = visor_throttle,
  245. .unthrottle = visor_unthrottle,
  246. .attach = clie_3_5_startup,
  247. .ioctl = visor_ioctl,
  248. .set_termios = visor_set_termios,
  249. .write = visor_write,
  250. .write_room = visor_write_room,
  251. .chars_in_buffer = visor_chars_in_buffer,
  252. .write_bulk_callback = visor_write_bulk_callback,
  253. .read_bulk_callback = visor_read_bulk_callback,
  254. };
  255. struct visor_private {
  256. spinlock_t lock;
  257. int bytes_in;
  258. int bytes_out;
  259. int outstanding_urbs;
  260. int throttled;
  261. };
  262. /* number of outstanding urbs to prevent userspace DoS from happening */
  263. #define URB_UPPER_LIMIT 42
  264. static int stats;
  265. /******************************************************************************
  266. * Handspring Visor specific driver functions
  267. ******************************************************************************/
  268. static int visor_open (struct usb_serial_port *port, struct file *filp)
  269. {
  270. struct usb_serial *serial = port->serial;
  271. struct visor_private *priv = usb_get_serial_port_data(port);
  272. unsigned long flags;
  273. int result = 0;
  274. dbg("%s - port %d", __FUNCTION__, port->number);
  275. if (!port->read_urb) {
  276. /* this is needed for some brain dead Sony devices */
  277. dev_err(&port->dev, "Device lied about number of ports, please use a lower one.\n");
  278. return -ENODEV;
  279. }
  280. spin_lock_irqsave(&priv->lock, flags);
  281. priv->bytes_in = 0;
  282. priv->bytes_out = 0;
  283. priv->outstanding_urbs = 0;
  284. priv->throttled = 0;
  285. spin_unlock_irqrestore(&priv->lock, flags);
  286. /*
  287. * Force low_latency on so that our tty_push actually forces the data
  288. * through, otherwise it is scheduled, and with high data rates (like
  289. * with OHCI) data can get lost.
  290. */
  291. if (port->tty)
  292. port->tty->low_latency = 1;
  293. /* Start reading from the device */
  294. usb_fill_bulk_urb (port->read_urb, serial->dev,
  295. usb_rcvbulkpipe (serial->dev,
  296. port->bulk_in_endpointAddress),
  297. port->read_urb->transfer_buffer,
  298. port->read_urb->transfer_buffer_length,
  299. visor_read_bulk_callback, port);
  300. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  301. if (result) {
  302. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n",
  303. __FUNCTION__, result);
  304. goto exit;
  305. }
  306. if (port->interrupt_in_urb) {
  307. dbg("%s - adding interrupt input for treo", __FUNCTION__);
  308. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  309. if (result)
  310. dev_err(&port->dev, "%s - failed submitting interrupt urb, error %d\n",
  311. __FUNCTION__, result);
  312. }
  313. exit:
  314. return result;
  315. }
  316. static void visor_close (struct usb_serial_port *port, struct file * filp)
  317. {
  318. struct visor_private *priv = usb_get_serial_port_data(port);
  319. unsigned char *transfer_buffer;
  320. dbg("%s - port %d", __FUNCTION__, port->number);
  321. /* shutdown our urbs */
  322. usb_kill_urb(port->read_urb);
  323. if (port->interrupt_in_urb)
  324. usb_kill_urb(port->interrupt_in_urb);
  325. /* Try to send shutdown message, if the device is gone, this will just fail. */
  326. transfer_buffer = kmalloc (0x12, GFP_KERNEL);
  327. if (transfer_buffer) {
  328. usb_control_msg (port->serial->dev,
  329. usb_rcvctrlpipe(port->serial->dev, 0),
  330. VISOR_CLOSE_NOTIFICATION, 0xc2,
  331. 0x0000, 0x0000,
  332. transfer_buffer, 0x12, 300);
  333. kfree (transfer_buffer);
  334. }
  335. if (stats)
  336. dev_info(&port->dev, "Bytes In = %d Bytes Out = %d\n",
  337. priv->bytes_in, priv->bytes_out);
  338. }
  339. static int visor_write (struct usb_serial_port *port, const unsigned char *buf, int count)
  340. {
  341. struct visor_private *priv = usb_get_serial_port_data(port);
  342. struct usb_serial *serial = port->serial;
  343. struct urb *urb;
  344. unsigned char *buffer;
  345. unsigned long flags;
  346. int status;
  347. dbg("%s - port %d", __FUNCTION__, port->number);
  348. spin_lock_irqsave(&priv->lock, flags);
  349. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  350. spin_unlock_irqrestore(&priv->lock, flags);
  351. dbg("%s - write limit hit\n", __FUNCTION__);
  352. return 0;
  353. }
  354. spin_unlock_irqrestore(&priv->lock, flags);
  355. buffer = kmalloc (count, GFP_ATOMIC);
  356. if (!buffer) {
  357. dev_err(&port->dev, "out of memory\n");
  358. return -ENOMEM;
  359. }
  360. urb = usb_alloc_urb(0, GFP_ATOMIC);
  361. if (!urb) {
  362. dev_err(&port->dev, "no more free urbs\n");
  363. kfree (buffer);
  364. return -ENOMEM;
  365. }
  366. memcpy (buffer, buf, count);
  367. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
  368. usb_fill_bulk_urb (urb, serial->dev,
  369. usb_sndbulkpipe (serial->dev,
  370. port->bulk_out_endpointAddress),
  371. buffer, count,
  372. visor_write_bulk_callback, port);
  373. /* send it down the pipe */
  374. status = usb_submit_urb(urb, GFP_ATOMIC);
  375. if (status) {
  376. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n",
  377. __FUNCTION__, status);
  378. count = status;
  379. kfree (buffer);
  380. } else {
  381. spin_lock_irqsave(&priv->lock, flags);
  382. ++priv->outstanding_urbs;
  383. priv->bytes_out += count;
  384. spin_unlock_irqrestore(&priv->lock, flags);
  385. }
  386. /* we are done with this urb, so let the host driver
  387. * really free it when it is finished with it */
  388. usb_free_urb (urb);
  389. return count;
  390. }
  391. static int visor_write_room (struct usb_serial_port *port)
  392. {
  393. dbg("%s - port %d", __FUNCTION__, port->number);
  394. /*
  395. * We really can take anything the user throws at us
  396. * but let's pick a nice big number to tell the tty
  397. * layer that we have lots of free space
  398. */
  399. return 2048;
  400. }
  401. static int visor_chars_in_buffer (struct usb_serial_port *port)
  402. {
  403. dbg("%s - port %d", __FUNCTION__, port->number);
  404. /*
  405. * We can't really account for how much data we
  406. * have sent out, but hasn't made it through to the
  407. * device, so just tell the tty layer that everything
  408. * is flushed.
  409. */
  410. return 0;
  411. }
  412. static void visor_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
  413. {
  414. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  415. struct visor_private *priv = usb_get_serial_port_data(port);
  416. unsigned long flags;
  417. /* free up the transfer buffer, as usb_free_urb() does not do this */
  418. kfree (urb->transfer_buffer);
  419. dbg("%s - port %d", __FUNCTION__, port->number);
  420. if (urb->status)
  421. dbg("%s - nonzero write bulk status received: %d",
  422. __FUNCTION__, urb->status);
  423. spin_lock_irqsave(&priv->lock, flags);
  424. --priv->outstanding_urbs;
  425. spin_unlock_irqrestore(&priv->lock, flags);
  426. schedule_work(&port->work);
  427. }
  428. static void visor_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
  429. {
  430. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  431. struct visor_private *priv = usb_get_serial_port_data(port);
  432. unsigned char *data = urb->transfer_buffer;
  433. struct tty_struct *tty;
  434. unsigned long flags;
  435. int i;
  436. int throttled;
  437. int result;
  438. dbg("%s - port %d", __FUNCTION__, port->number);
  439. if (urb->status) {
  440. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  441. return;
  442. }
  443. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  444. tty = port->tty;
  445. if (tty && urb->actual_length) {
  446. for (i = 0; i < urb->actual_length ; ++i) {
  447. /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
  448. if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
  449. tty_flip_buffer_push(tty);
  450. }
  451. /* this doesn't actually push the data through unless tty->low_latency is set */
  452. tty_insert_flip_char(tty, data[i], 0);
  453. }
  454. tty_flip_buffer_push(tty);
  455. }
  456. spin_lock_irqsave(&priv->lock, flags);
  457. priv->bytes_in += urb->actual_length;
  458. throttled = priv->throttled;
  459. spin_unlock_irqrestore(&priv->lock, flags);
  460. /* Continue trying to always read if we should */
  461. if (!throttled) {
  462. usb_fill_bulk_urb (port->read_urb, port->serial->dev,
  463. usb_rcvbulkpipe(port->serial->dev,
  464. port->bulk_in_endpointAddress),
  465. port->read_urb->transfer_buffer,
  466. port->read_urb->transfer_buffer_length,
  467. visor_read_bulk_callback, port);
  468. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  469. if (result)
  470. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  471. }
  472. return;
  473. }
  474. static void visor_read_int_callback (struct urb *urb, struct pt_regs *regs)
  475. {
  476. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  477. int result;
  478. switch (urb->status) {
  479. case 0:
  480. /* success */
  481. break;
  482. case -ECONNRESET:
  483. case -ENOENT:
  484. case -ESHUTDOWN:
  485. /* this urb is terminated, clean up */
  486. dbg("%s - urb shutting down with status: %d",
  487. __FUNCTION__, urb->status);
  488. return;
  489. default:
  490. dbg("%s - nonzero urb status received: %d",
  491. __FUNCTION__, urb->status);
  492. goto exit;
  493. }
  494. /*
  495. * This information is still unknown what it can be used for.
  496. * If anyone has an idea, please let the author know...
  497. *
  498. * Rumor has it this endpoint is used to notify when data
  499. * is ready to be read from the bulk ones.
  500. */
  501. usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
  502. urb->actual_length, urb->transfer_buffer);
  503. exit:
  504. result = usb_submit_urb (urb, GFP_ATOMIC);
  505. if (result)
  506. dev_err(&urb->dev->dev, "%s - Error %d submitting interrupt urb\n",
  507. __FUNCTION__, result);
  508. }
  509. static void visor_throttle (struct usb_serial_port *port)
  510. {
  511. struct visor_private *priv = usb_get_serial_port_data(port);
  512. unsigned long flags;
  513. dbg("%s - port %d", __FUNCTION__, port->number);
  514. spin_lock_irqsave(&priv->lock, flags);
  515. priv->throttled = 1;
  516. spin_unlock_irqrestore(&priv->lock, flags);
  517. }
  518. static void visor_unthrottle (struct usb_serial_port *port)
  519. {
  520. struct visor_private *priv = usb_get_serial_port_data(port);
  521. unsigned long flags;
  522. int result;
  523. dbg("%s - port %d", __FUNCTION__, port->number);
  524. spin_lock_irqsave(&priv->lock, flags);
  525. priv->throttled = 0;
  526. spin_unlock_irqrestore(&priv->lock, flags);
  527. port->read_urb->dev = port->serial->dev;
  528. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  529. if (result)
  530. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
  531. }
  532. static int palm_os_3_probe (struct usb_serial *serial, const struct usb_device_id *id)
  533. {
  534. struct device *dev = &serial->dev->dev;
  535. struct visor_connection_info *connection_info;
  536. unsigned char *transfer_buffer;
  537. char *string;
  538. int retval = 0;
  539. int i;
  540. int num_ports = 0;
  541. dbg("%s", __FUNCTION__);
  542. transfer_buffer = kmalloc (sizeof (*connection_info), GFP_KERNEL);
  543. if (!transfer_buffer) {
  544. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __FUNCTION__,
  545. sizeof(*connection_info));
  546. return -ENOMEM;
  547. }
  548. /* send a get connection info request */
  549. retval = usb_control_msg (serial->dev,
  550. usb_rcvctrlpipe(serial->dev, 0),
  551. VISOR_GET_CONNECTION_INFORMATION,
  552. 0xc2, 0x0000, 0x0000, transfer_buffer,
  553. sizeof(*connection_info), 300);
  554. if (retval < 0) {
  555. dev_err(dev, "%s - error %d getting connection information\n",
  556. __FUNCTION__, retval);
  557. goto exit;
  558. }
  559. if (retval == sizeof(*connection_info)) {
  560. connection_info = (struct visor_connection_info *)transfer_buffer;
  561. num_ports = le16_to_cpu(connection_info->num_ports);
  562. for (i = 0; i < num_ports; ++i) {
  563. switch (connection_info->connections[i].port_function_id) {
  564. case VISOR_FUNCTION_GENERIC:
  565. string = "Generic";
  566. break;
  567. case VISOR_FUNCTION_DEBUGGER:
  568. string = "Debugger";
  569. break;
  570. case VISOR_FUNCTION_HOTSYNC:
  571. string = "HotSync";
  572. break;
  573. case VISOR_FUNCTION_CONSOLE:
  574. string = "Console";
  575. break;
  576. case VISOR_FUNCTION_REMOTE_FILE_SYS:
  577. string = "Remote File System";
  578. break;
  579. default:
  580. string = "unknown";
  581. break;
  582. }
  583. dev_info(dev, "%s: port %d, is for %s use\n",
  584. serial->type->description,
  585. connection_info->connections[i].port, string);
  586. }
  587. }
  588. /*
  589. * Handle devices that report invalid stuff here.
  590. */
  591. if (num_ports == 0 || num_ports > 2) {
  592. dev_warn (dev, "%s: No valid connect info available\n",
  593. serial->type->description);
  594. num_ports = 2;
  595. }
  596. dev_info(dev, "%s: Number of ports: %d\n", serial->type->description,
  597. num_ports);
  598. /*
  599. * save off our num_ports info so that we can use it in the
  600. * calc_num_ports callback
  601. */
  602. usb_set_serial_data(serial, (void *)(long)num_ports);
  603. /* ask for the number of bytes available, but ignore the response as it is broken */
  604. retval = usb_control_msg (serial->dev,
  605. usb_rcvctrlpipe(serial->dev, 0),
  606. VISOR_REQUEST_BYTES_AVAILABLE,
  607. 0xc2, 0x0000, 0x0005, transfer_buffer,
  608. 0x02, 300);
  609. if (retval < 0)
  610. dev_err(dev, "%s - error %d getting bytes available request\n",
  611. __FUNCTION__, retval);
  612. retval = 0;
  613. exit:
  614. kfree (transfer_buffer);
  615. return retval;
  616. }
  617. static int palm_os_4_probe (struct usb_serial *serial, const struct usb_device_id *id)
  618. {
  619. struct device *dev = &serial->dev->dev;
  620. struct palm_ext_connection_info *connection_info;
  621. unsigned char *transfer_buffer;
  622. int retval;
  623. dbg("%s", __FUNCTION__);
  624. transfer_buffer = kmalloc (sizeof (*connection_info), GFP_KERNEL);
  625. if (!transfer_buffer) {
  626. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __FUNCTION__,
  627. sizeof(*connection_info));
  628. return -ENOMEM;
  629. }
  630. retval = usb_control_msg (serial->dev,
  631. usb_rcvctrlpipe(serial->dev, 0),
  632. PALM_GET_EXT_CONNECTION_INFORMATION,
  633. 0xc2, 0x0000, 0x0000, transfer_buffer,
  634. sizeof (*connection_info), 300);
  635. if (retval < 0)
  636. dev_err(dev, "%s - error %d getting connection info\n",
  637. __FUNCTION__, retval);
  638. else
  639. usb_serial_debug_data(debug, &serial->dev->dev, __FUNCTION__,
  640. retval, transfer_buffer);
  641. kfree (transfer_buffer);
  642. return 0;
  643. }
  644. static int visor_probe (struct usb_serial *serial, const struct usb_device_id *id)
  645. {
  646. int retval = 0;
  647. int (*startup) (struct usb_serial *serial, const struct usb_device_id *id);
  648. dbg("%s", __FUNCTION__);
  649. if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
  650. err("active config #%d != 1 ??",
  651. serial->dev->actconfig->desc.bConfigurationValue);
  652. return -ENODEV;
  653. }
  654. if (id->driver_info) {
  655. startup = (void *)id->driver_info;
  656. retval = startup(serial, id);
  657. }
  658. return retval;
  659. }
  660. static int visor_calc_num_ports (struct usb_serial *serial)
  661. {
  662. int num_ports = (int)(long)(usb_get_serial_data(serial));
  663. if (num_ports)
  664. usb_set_serial_data(serial, NULL);
  665. return num_ports;
  666. }
  667. static int generic_startup(struct usb_serial *serial)
  668. {
  669. struct visor_private *priv;
  670. int i;
  671. for (i = 0; i < serial->num_ports; ++i) {
  672. priv = kmalloc (sizeof(*priv), GFP_KERNEL);
  673. if (!priv)
  674. return -ENOMEM;
  675. memset (priv, 0x00, sizeof(*priv));
  676. spin_lock_init(&priv->lock);
  677. usb_set_serial_port_data(serial->port[i], priv);
  678. }
  679. return 0;
  680. }
  681. static int clie_3_5_startup (struct usb_serial *serial)
  682. {
  683. struct device *dev = &serial->dev->dev;
  684. int result;
  685. u8 data;
  686. dbg("%s", __FUNCTION__);
  687. /*
  688. * Note that PEG-300 series devices expect the following two calls.
  689. */
  690. /* get the config number */
  691. result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  692. USB_REQ_GET_CONFIGURATION, USB_DIR_IN,
  693. 0, 0, &data, 1, 3000);
  694. if (result < 0) {
  695. dev_err(dev, "%s: get config number failed: %d\n", __FUNCTION__, result);
  696. return result;
  697. }
  698. if (result != 1) {
  699. dev_err(dev, "%s: get config number bad return length: %d\n", __FUNCTION__, result);
  700. return -EIO;
  701. }
  702. /* get the interface number */
  703. result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  704. USB_REQ_GET_INTERFACE,
  705. USB_DIR_IN | USB_RECIP_INTERFACE,
  706. 0, 0, &data, 1, 3000);
  707. if (result < 0) {
  708. dev_err(dev, "%s: get interface number failed: %d\n", __FUNCTION__, result);
  709. return result;
  710. }
  711. if (result != 1) {
  712. dev_err(dev, "%s: get interface number bad return length: %d\n", __FUNCTION__, result);
  713. return -EIO;
  714. }
  715. return generic_startup(serial);
  716. }
  717. static int treo_attach (struct usb_serial *serial)
  718. {
  719. struct usb_serial_port *swap_port;
  720. /* Only do this endpoint hack for the Handspring devices with
  721. * interrupt in endpoints, which for now are the Treo devices. */
  722. if (!((le16_to_cpu(serial->dev->descriptor.idVendor) == HANDSPRING_VENDOR_ID) ||
  723. (le16_to_cpu(serial->dev->descriptor.idVendor) == KYOCERA_VENDOR_ID)) ||
  724. (serial->num_interrupt_in == 0))
  725. goto generic_startup;
  726. dbg("%s", __FUNCTION__);
  727. /*
  728. * It appears that Treos and Kyoceras want to use the
  729. * 1st bulk in endpoint to communicate with the 2nd bulk out endpoint,
  730. * so let's swap the 1st and 2nd bulk in and interrupt endpoints.
  731. * Note that swapping the bulk out endpoints would break lots of
  732. * apps that want to communicate on the second port.
  733. */
  734. #define COPY_PORT(dest, src) \
  735. dest->read_urb = src->read_urb; \
  736. dest->bulk_in_endpointAddress = src->bulk_in_endpointAddress; \
  737. dest->bulk_in_buffer = src->bulk_in_buffer; \
  738. dest->interrupt_in_urb = src->interrupt_in_urb; \
  739. dest->interrupt_in_endpointAddress = src->interrupt_in_endpointAddress; \
  740. dest->interrupt_in_buffer = src->interrupt_in_buffer;
  741. swap_port = kmalloc(sizeof(*swap_port), GFP_KERNEL);
  742. if (!swap_port)
  743. return -ENOMEM;
  744. COPY_PORT(swap_port, serial->port[0]);
  745. COPY_PORT(serial->port[0], serial->port[1]);
  746. COPY_PORT(serial->port[1], swap_port);
  747. kfree(swap_port);
  748. generic_startup:
  749. return generic_startup(serial);
  750. }
  751. static int clie_5_attach (struct usb_serial *serial)
  752. {
  753. dbg("%s", __FUNCTION__);
  754. /* TH55 registers 2 ports.
  755. Communication in from the UX50/TH55 uses bulk_in_endpointAddress from port 0
  756. Communication out to the UX50/TH55 uses bulk_out_endpointAddress from port 1
  757. Lets do a quick and dirty mapping
  758. */
  759. /* some sanity check */
  760. if (serial->num_ports < 2)
  761. return -1;
  762. /* port 0 now uses the modified endpoint Address */
  763. serial->port[0]->bulk_out_endpointAddress = serial->port[1]->bulk_out_endpointAddress;
  764. return generic_startup(serial);
  765. }
  766. static void visor_shutdown (struct usb_serial *serial)
  767. {
  768. dbg("%s", __FUNCTION__);
  769. }
  770. static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
  771. {
  772. dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
  773. return -ENOIOCTLCMD;
  774. }
  775. /* This function is all nice and good, but we don't change anything based on it :) */
  776. static void visor_set_termios (struct usb_serial_port *port, struct termios *old_termios)
  777. {
  778. unsigned int cflag;
  779. dbg("%s - port %d", __FUNCTION__, port->number);
  780. if ((!port->tty) || (!port->tty->termios)) {
  781. dbg("%s - no tty structures", __FUNCTION__);
  782. return;
  783. }
  784. cflag = port->tty->termios->c_cflag;
  785. /* check that they really want us to change something */
  786. if (old_termios) {
  787. if ((cflag == old_termios->c_cflag) &&
  788. (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
  789. dbg("%s - nothing to change...", __FUNCTION__);
  790. return;
  791. }
  792. }
  793. /* get the byte size */
  794. switch (cflag & CSIZE) {
  795. case CS5: dbg("%s - data bits = 5", __FUNCTION__); break;
  796. case CS6: dbg("%s - data bits = 6", __FUNCTION__); break;
  797. case CS7: dbg("%s - data bits = 7", __FUNCTION__); break;
  798. default:
  799. case CS8: dbg("%s - data bits = 8", __FUNCTION__); break;
  800. }
  801. /* determine the parity */
  802. if (cflag & PARENB)
  803. if (cflag & PARODD)
  804. dbg("%s - parity = odd", __FUNCTION__);
  805. else
  806. dbg("%s - parity = even", __FUNCTION__);
  807. else
  808. dbg("%s - parity = none", __FUNCTION__);
  809. /* figure out the stop bits requested */
  810. if (cflag & CSTOPB)
  811. dbg("%s - stop bits = 2", __FUNCTION__);
  812. else
  813. dbg("%s - stop bits = 1", __FUNCTION__);
  814. /* figure out the flow control settings */
  815. if (cflag & CRTSCTS)
  816. dbg("%s - RTS/CTS is enabled", __FUNCTION__);
  817. else
  818. dbg("%s - RTS/CTS is disabled", __FUNCTION__);
  819. /* determine software flow control */
  820. if (I_IXOFF(port->tty))
  821. dbg("%s - XON/XOFF is enabled, XON = %2x, XOFF = %2x",
  822. __FUNCTION__, START_CHAR(port->tty), STOP_CHAR(port->tty));
  823. else
  824. dbg("%s - XON/XOFF is disabled", __FUNCTION__);
  825. /* get the baud rate wanted */
  826. dbg("%s - baud rate = %d", __FUNCTION__, tty_get_baud_rate(port->tty));
  827. return;
  828. }
  829. static int __init visor_init (void)
  830. {
  831. int i, retval;
  832. /* Only if parameters were passed to us */
  833. if ((vendor>0) && (product>0)) {
  834. struct usb_device_id usb_dev_temp[]=
  835. {{USB_DEVICE(vendor, product),
  836. .driver_info = (kernel_ulong_t)&palm_os_4_probe }};
  837. /* Find the last entry in id_table */
  838. for (i=0; ; i++) {
  839. if (id_table[i].idVendor==0) {
  840. id_table[i] = usb_dev_temp[0];
  841. break;
  842. }
  843. }
  844. /* Find the last entry in id_table_combined */
  845. for (i=0; ; i++) {
  846. if (id_table_combined[i].idVendor==0) {
  847. id_table_combined[i] = usb_dev_temp[0];
  848. break;
  849. }
  850. }
  851. info("Untested USB device specified at time of module insertion");
  852. info("Warning: This is not guaranteed to work");
  853. info("Using a newer kernel is preferred to this method");
  854. info("Adding Palm OS protocol 4.x support for unknown device: 0x%x/0x%x",
  855. vendor, product);
  856. }
  857. retval = usb_serial_register(&handspring_device);
  858. if (retval)
  859. goto failed_handspring_register;
  860. retval = usb_serial_register(&clie_3_5_device);
  861. if (retval)
  862. goto failed_clie_3_5_register;
  863. retval = usb_serial_register(&clie_5_device);
  864. if (retval)
  865. goto failed_clie_5_register;
  866. retval = usb_register(&visor_driver);
  867. if (retval)
  868. goto failed_usb_register;
  869. info(DRIVER_DESC);
  870. return 0;
  871. failed_usb_register:
  872. usb_serial_deregister(&clie_5_device);
  873. failed_clie_5_register:
  874. usb_serial_deregister(&clie_3_5_device);
  875. failed_clie_3_5_register:
  876. usb_serial_deregister(&handspring_device);
  877. failed_handspring_register:
  878. return retval;
  879. }
  880. static void __exit visor_exit (void)
  881. {
  882. usb_deregister (&visor_driver);
  883. usb_serial_deregister (&handspring_device);
  884. usb_serial_deregister (&clie_3_5_device);
  885. usb_serial_deregister (&clie_5_device);
  886. }
  887. module_init(visor_init);
  888. module_exit(visor_exit);
  889. MODULE_AUTHOR( DRIVER_AUTHOR );
  890. MODULE_DESCRIPTION( DRIVER_DESC );
  891. MODULE_LICENSE("GPL");
  892. module_param(debug, bool, S_IRUGO | S_IWUSR);
  893. MODULE_PARM_DESC(debug, "Debug enabled or not");
  894. module_param(stats, bool, S_IRUGO | S_IWUSR);
  895. MODULE_PARM_DESC(stats, "Enables statistics or not");
  896. module_param(vendor, ushort, 0);
  897. MODULE_PARM_DESC(vendor, "User specified vendor ID");
  898. module_param(product, ushort, 0);
  899. MODULE_PARM_DESC(product, "User specified product ID");